diff --git a/overlay/rootfs/usr/bin/sxmo_modemmonitor.sh b/overlay/rootfs/usr/bin/sxmo_modemmonitor.sh index a395c07..34ee04a 100755 --- a/overlay/rootfs/usr/bin/sxmo_modemmonitor.sh +++ b/overlay/rootfs/usr/bin/sxmo_modemmonitor.sh @@ -43,6 +43,11 @@ mainloop() { uim_st="$(doas -n /usr/libexec/msm-modem-uim-selection status 2>/dev/null || /usr/libexec/msm-modem-uim-selection status 2>/dev/null || true)" if printf "%s\n" "$uim_st" | grep -qE "SLOT[12]_PIN1_STATE=(enabled-not-verified|blocked)|SLOT[12]_APP_STATE=(pin1|puk1)"; then newstate="locked" + elif [ "$newstate" = "locked" ] && printf "%s\n" "$uim_st" | grep -q "^MODEM_AVAILABLE=1$"; then + newstate="$(mmcli -m any -K 2>/dev/null | grep "^modem.generic.state " | cut -d':' -f2 | sed 's/^ //')" + if [ "$newstate" = "locked" ]; then + newstate="disabled" + fi elif printf "%s\n" "$uim_st" | grep -qE "SLOT[12]_STATE=present" && printf "%s\n" "$uim_st" | grep -q "^PRIMARY_GW_SLOT=0$"; then doas -n /usr/libexec/msm-modem-uim-selection boot >/dev/null 2>&1 || true newstate="$(mmcli -m any -K 2>/dev/null | grep "^modem.generic.state " | cut -d':' -f2 | sed 's/^ //')" diff --git a/overlay/rootfs/usr/lib/dwm/plugins/modem.so b/overlay/rootfs/usr/lib/dwm/plugins/modem.so index e66f1d9..c7ba3fe 100755 Binary files a/overlay/rootfs/usr/lib/dwm/plugins/modem.so and b/overlay/rootfs/usr/lib/dwm/plugins/modem.so differ diff --git a/overlay/rootfs/usr/libexec/msm-modem-uim-selection b/overlay/rootfs/usr/libexec/msm-modem-uim-selection index 4c3d984..e31be47 100755 --- a/overlay/rootfs/usr/libexec/msm-modem-uim-selection +++ b/overlay/rootfs/usr/libexec/msm-modem-uim-selection @@ -544,7 +544,7 @@ provision_slot() { eval "other_app_state=\$SLOT${other_slot}_APP_STATE" need_prim=1 - if [ "$PRIMARY_GW_SLOT" = "$chosen_slot" ] && { [ "$chosen_app_state" = "ready" ] || [ "$chosen_app_state" = "pin1-or-upin-pin-required" ] || [ "$chosen_app_state" = "puk1-or-upin-puk-required" ] || [ "$chosen_app_state" = "pin1_or_upin_pin_required" ] || [ "$chosen_app_state" = "puk1_or_upin_puk_required" ]; }; then + if [ "$PRIMARY_GW_SLOT" = "$chosen_slot" ] && [ -n "$chosen_app_state" ] && [ "$chosen_app_state" != "none" ] && [ "$chosen_app_state" != "illegal" ]; then need_prim=0 fi @@ -574,7 +574,7 @@ provision_slot() { # Dual-SIM Dual Standby (DSDS): provision the second present SIM slot to secondary-gw-provisioning if [ "$other_state" = "present" ]; then need_sec=1 - if [ "${SECONDARY_GW_SLOT:-0}" = "$other_slot" ] && { [ "$other_app_state" = "ready" ] || [ "$other_app_state" = "pin1-or-upin-pin-required" ] || [ "$other_app_state" = "puk1-or-upin-puk-required" ] || [ "$other_app_state" = "pin1_or_upin_pin_required" ] || [ "$other_app_state" = "puk1_or_upin_puk_required" ]; }; then + if [ "${SECONDARY_GW_SLOT:-0}" = "$other_slot" ] && [ -n "$other_app_state" ] && [ "$other_app_state" != "none" ] && [ "$other_app_state" != "illegal" ]; then need_sec=0 fi if [ "$need_sec" = "1" ]; then @@ -948,10 +948,15 @@ PY exit 1 fi prepare_slot_for_pin_op "$slot" + eval "before_pin1=\${SLOT${slot}_PIN1_STATE:-}" + eval "before_app=\${SLOT${slot}_APP_STATE:-}" eval "before_ret=\${SLOT${slot}_PIN1_RETRIES:-3}" verified=0 wrong_pin=0 - if [ "${PIN_OP_SESSION:-primary-gw-provisioning}" = "primary-gw-provisioning" ]; then + if [ "$before_pin1" = "enabled-verified" ] || [ "$before_pin1" = "disabled" ] || [ "$before_app" = "ready" ]; then + verified=1 + fi + if [ "$verified" = "0" ] && [ "${PIN_OP_SESSION:-primary-gw-provisioning}" = "primary-gw-provisioning" ]; then sim_id="$(timeout 3 mmcli -m any 2>/dev/null | grep -oE 'SIM/[0-9]+' | head -n1 | cut -d'/' -f2 || true)" if [ -n "$sim_id" ]; then mm_pin_out="$(timeout 5 mmcli -i "$sim_id" --pin="$pin" 2>&1)" && verified=1 || { diff --git a/overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_modem.sh b/overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_modem.sh index e1125ac..5db5ce8 100755 --- a/overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_modem.sh +++ b/overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_modem.sh @@ -17,6 +17,13 @@ sxmo_debug "$2 (old: $1 [reason: $3])" open_sim_unlock_ui() { ( + uim_st="$(doas -n /usr/libexec/msm-modem-uim-selection status 2>/dev/null || /usr/libexec/msm-modem-uim-selection status 2>/dev/null || true)" + if printf "%s\n" "$uim_st" | grep -q "^MODEM_AVAILABLE=1$"; then + if ! printf "%s\n" "$uim_st" | grep -qE "SLOT[12]_PIN1_STATE=(enabled-not-verified|blocked)|SLOT[12]_APP_STATE=(pin1|puk1)"; then + sxmo_log "QMI UIM reports no SIM requires PIN/PUK; skipping unlock UI." + exit 0 + fi + fi i=0 while [ "$i" -lt 40 ] && [ ! -p /tmp/dwm_plugin_modem.fifo ]; do sleep 0.2 diff --git a/prebuilt/sxmo-x11/usr/lib/dwm/plugins/modem.so b/prebuilt/sxmo-x11/usr/lib/dwm/plugins/modem.so index e66f1d9..c7ba3fe 100755 Binary files a/prebuilt/sxmo-x11/usr/lib/dwm/plugins/modem.so and b/prebuilt/sxmo-x11/usr/lib/dwm/plugins/modem.so differ diff --git a/src/dwm-plugins/modem_plugin.cpp b/src/dwm-plugins/modem_plugin.cpp index 2d7bb4c..f2dc020 100644 --- a/src/dwm-plugins/modem_plugin.cpp +++ b/src/dwm-plugins/modem_plugin.cpp @@ -136,11 +136,12 @@ struct ModemStatusData { std::string regStatusLabel() const { if (slots[0].needsPuk() || slots[1].needsPuk() || - mmUnlockRequired == "sim-puk") { + (!qmiAvailable && mmUnlockRequired == "sim-puk")) { return "SIM PUK Required"; } if (slots[0].needsPin() || slots[1].needsPin() || - mmUnlockRequired == "sim-pin" || mmState == "locked") { + (!qmiAvailable && + (mmUnlockRequired == "sim-pin" || mmState == "locked"))) { return "SIM PIN Required"; } if (mmRegState == "home" || @@ -432,13 +433,15 @@ ModemStatusData queryModemAndSimStatus() { actIdx = std::clamp(d.preferredSlot, 1, 2) - 1; } if (actIdx >= 0 && actIdx < 2) { - if (d.mmUnlockRequired == "sim-pin" && - d.slots[actIdx].pin1State == "not-initialized") { - d.slots[actIdx].state = "present"; - d.slots[actIdx].pin1State = "enabled-not-verified"; - } else if (d.mmUnlockRequired == "sim-puk") { - d.slots[actIdx].state = "present"; - d.slots[actIdx].pin1State = "blocked"; + if (!d.qmiAvailable) { + if (d.mmUnlockRequired == "sim-pin" && + d.slots[actIdx].pin1State == "not-initialized") { + d.slots[actIdx].state = "present"; + d.slots[actIdx].pin1State = "enabled-not-verified"; + } else if (d.mmUnlockRequired == "sim-puk") { + d.slots[actIdx].state = "present"; + d.slots[actIdx].pin1State = "blocked"; + } } if (d.mmPinRetries >= 0 && d.slots[actIdx].pin1Retries == 0) { d.slots[actIdx].pin1Retries = d.mmPinRetries; @@ -591,6 +594,11 @@ int runModemApp(int argc, char** argv) { int64_t statusBannerUntilMs = 0; bool visible = false; + bool openedForUnlock = false; + int unlockedSlotMask = 0; + int64_t lastUnlockSuccessMs = 0; + int autoCheckStage = isPreload ? 0 : 2; + int64_t nextAutoCheckMs = nowMs() + 2500; int64_t openStartMs = 0; int64_t startupIgnoreUntilMs = 0; int64_t lastRefreshMs = 0; @@ -605,6 +613,19 @@ int runModemApp(int argc, char** argv) { auto refreshData = [&](bool reloadLog = false) { status = queryModemAndSimStatus(); + int64_t now = nowMs(); + if (lastUnlockSuccessMs > 0 && (now - lastUnlockSuccessMs) < 30000) { + for (int i = 0; i < 2; ++i) { + if (unlockedSlotMask & (1 << i)) { + if (status.slots[i].pin1State == "enabled-not-verified") { + status.slots[i].pin1State = "enabled-verified"; + } + if (status.slots[i].appState.rfind("pin1", 0) == 0) { + status.slots[i].appState = "ready"; + } + } + } + } if (reloadLog || activeTab == TabId::ModemLog) { book.load(); logRows = loadRecentModemLog(book); @@ -612,13 +633,27 @@ int runModemApp(int argc, char** argv) { logRowSel = std::max(0, static_cast(logRows.size()) - 1); } } - lastRefreshMs = nowMs(); + lastRefreshMs = now; if (visible && !isAnotherPluginActive("modem")) { XRaiseWindow(dpy, win); grabPluginKeyboard(dpy, win); } }; + auto anySimNeedsUnlock = [&]() -> bool { + if (status.slots[0].needsPin() || status.slots[0].needsPuk() || + status.slots[1].needsPin() || status.slots[1].needsPuk()) { + return true; + } + if (!status.qmiAvailable && + (lastUnlockSuccessMs == 0 || (nowMs() - lastUnlockSuccessMs) >= 30000) && + (status.mmState == "locked" || status.mmUnlockRequired == "sim-pin" || + status.mmUnlockRequired == "sim-puk")) { + return true; + } + return false; + }; + auto defaultActiveOrPresentSlot = [&]() -> int { if (status.slots[0].needsPin() || status.slots[0].needsPuk()) return 1; if (status.slots[1].needsPin() || status.slots[1].needsPuk()) return 2; @@ -648,7 +683,8 @@ int runModemApp(int argc, char** argv) { inputPin2.clear(); inputPin3.clear(); modalError.clear(); - if (s.needsPuk() || status.mmUnlockRequired == "sim-puk") { + if (s.needsPuk() || + (!status.qmiAvailable && status.mmUnlockRequired == "sim-puk")) { modal = ModalMode::UnblockPuk; } else { modal = ModalMode::VerifyPin; @@ -662,7 +698,9 @@ int runModemApp(int argc, char** argv) { activeTab = TabId::SimCards; int lockSlot = defaultActiveOrPresentSlot(); simRowSel = lockSlot - 1; - openPinOrPukModalForSlot(lockSlot); + if (anySimNeedsUnlock()) { + openPinOrPukModalForSlot(lockSlot); + } } else if (a == "info" || a == "status" || a == "radio") { activeTab = TabId::RadioInfo; } else if (a == "log" || a == "logs") { @@ -1261,41 +1299,6 @@ int runModemApp(int argc, char** argv) { grabPluginKeyboard(dpy, win); }; - auto showWindow = [&](const std::string& arg = "") { - std::string a = toLowerStr(trimStr(arg)); - if (visible && - (modal == ModalMode::VerifyPin || modal == ModalMode::UnblockPuk) && - (a == "unlock" || a == "pin" || a == "puk")) { - writeIntFile(activePidPath, static_cast(::getpid())); - closeOtherPlugins("modem"); - XRaiseWindow(dpy, win); - grabKeys(); - render(); - return; - } - writeIntFile(activePidPath, static_cast(::getpid())); - closeOtherPlugins("modem"); - if (!visible) { - XMapRaised(dpy, win); - visible = true; - } else { - XRaiseWindow(dpy, win); - } - grabKeys(); - render(); - refreshData(true); - applyOpenArg(arg); - writeIntFile(activePidPath, static_cast(::getpid())); - closeOtherPlugins("modem"); - XRaiseWindow(dpy, win); - openStartMs = nowMs(); - startupIgnoreUntilMs = openStartMs + 180; - initialSoftkeysReleased = (querySoftkeysDown(dpy) == 0); - softkeyPending = 0; - grabKeys(); - render(); - }; - auto hideWindow = [&]() { if (!visible) return; int xfdLocal = ConnectionNumber(dpy); @@ -1315,10 +1318,65 @@ int runModemApp(int argc, char** argv) { XUnmapWindow(dpy, win); XFlush(dpy); visible = false; + openedForUnlock = false; modal = ModalMode::None; ::unlink(activePidPath.c_str()); }; + auto showWindow = [&](const std::string& arg = "") { + std::string a = toLowerStr(trimStr(arg)); + bool isUnlockReq = (a == "unlock" || a == "pin" || a == "puk"); + if (isUnlockReq) { + autoCheckStage = 2; + } + if (visible && + (modal == ModalMode::VerifyPin || modal == ModalMode::UnblockPuk) && + isUnlockReq) { + writeIntFile(activePidPath, static_cast(::getpid())); + closeOtherPlugins("modem"); + XRaiseWindow(dpy, win); + grabKeys(); + render(); + return; + } + if (isUnlockReq) { + refreshData(false); + if (!anySimNeedsUnlock()) { + if (visible && openedForUnlock) { + hideWindow(); + } + return; + } + } + writeIntFile(activePidPath, static_cast(::getpid())); + closeOtherPlugins("modem"); + if (!visible) { + openedForUnlock = isUnlockReq; + XMapRaised(dpy, win); + visible = true; + } else { + if (!isUnlockReq) { + openedForUnlock = false; + } + XRaiseWindow(dpy, win); + } + grabKeys(); + render(); + if (!isUnlockReq) { + refreshData(true); + } + applyOpenArg(arg); + writeIntFile(activePidPath, static_cast(::getpid())); + closeOtherPlugins("modem"); + XRaiseWindow(dpy, win); + openStartMs = nowMs(); + startupIgnoreUntilMs = openStartMs + 180; + initialSoftkeysReleased = (querySoftkeysDown(dpy) == 0); + softkeyPending = 0; + grabKeys(); + render(); + }; + auto switchSimSlotNow = [&](int slotNum, bool swapGw = false) { slotNum = std::clamp(slotNum, 1, 2); if (swapGw) { @@ -1365,11 +1423,27 @@ int runModemApp(int argc, char** argv) { runCaptureCmd("doas -n /usr/libexec/msm-modem-uim-selection verify-pin " + std::to_string(targetSlot) + " " + shellQuote(inputPin1) + " >/dev/null 2>&1", &rc); + if (rc == 0) { + unlockedSlotMask |= (1 << (std::clamp(targetSlot, 1, 2) - 1)); + lastUnlockSuccessMs = nowMs(); + } refreshData(false); runAsyncCmd("sxmo_hook_statusbar.sh modem >/dev/null 2>&1"); if (rc == 0) { - modal = ModalMode::None; - setBanner("SIM " + std::to_string(targetSlot) + " unlocked"); + int otherSlot = (targetSlot == 1) ? 2 : 1; + const auto& otherSim = status.slots[otherSlot - 1]; + if (otherSim.needsPin() || otherSim.needsPuk()) { + simRowSel = otherSlot - 1; + openPinOrPukModalForSlot(otherSlot); + setBanner("SIM " + std::to_string(targetSlot) + + " unlocked; unlock SIM " + std::to_string(otherSlot)); + } else { + modal = ModalMode::None; + setBanner("SIM " + std::to_string(targetSlot) + " unlocked"); + if (openedForUnlock) { + hideWindow(); + } + } } else { inputPin1.clear(); modalError = "Incorrect PIN"; @@ -1401,11 +1475,27 @@ int runModemApp(int argc, char** argv) { runCaptureCmd("doas -n /usr/libexec/msm-modem-uim-selection unblock-puk " + std::to_string(targetSlot) + " " + shellQuote(inputPin1) + " " + shellQuote(inputPin2) + " >/dev/null 2>&1", &rc); + if (rc == 0) { + unlockedSlotMask |= (1 << (std::clamp(targetSlot, 1, 2) - 1)); + lastUnlockSuccessMs = nowMs(); + } refreshData(false); runAsyncCmd("sxmo_hook_statusbar.sh modem >/dev/null 2>&1"); if (rc == 0) { - modal = ModalMode::None; - setBanner("SIM unblocked and new PIN set"); + int otherSlot = (targetSlot == 1) ? 2 : 1; + const auto& otherSim = status.slots[otherSlot - 1]; + if (otherSim.needsPin() || otherSim.needsPuk()) { + simRowSel = otherSlot - 1; + openPinOrPukModalForSlot(otherSlot); + setBanner("SIM " + std::to_string(targetSlot) + + " unblocked; unlock SIM " + std::to_string(otherSlot)); + } else { + modal = ModalMode::None; + setBanner("SIM unblocked and new PIN set"); + if (openedForUnlock) { + hideWindow(); + } + } } else { wizardStep = 0; inputPin1.clear(); @@ -1556,7 +1646,11 @@ int runModemApp(int argc, char** argv) { } switchSimSlotNow(otherGwSlot, true); } else if (row == 4) { - openPinOrPukModalForSlot(defaultActiveOrPresentSlot()); + if (anySimNeedsUnlock()) { + openPinOrPukModalForSlot(defaultActiveOrPresentSlot()); + } else { + setBanner("SIM cards are already unlocked"); + } } else if (row == 5) { targetSlot = defaultActiveOrPresentSlot(); wizardStep = 0; @@ -1668,6 +1762,9 @@ int runModemApp(int argc, char** argv) { runAsyncCmd("mmcli -m any --3gpp-ussd-cancel >/dev/null 2>&1"); } modal = ModalMode::None; + if (openedForUnlock) { + hideWindow(); + } return; } hideWindow(); @@ -1744,9 +1841,6 @@ int runModemApp(int argc, char** argv) { } }; - int autoCheckStage = isPreload ? 0 : 2; - int64_t nextAutoCheckMs = nowMs() + 2500; - if (!isPreload) { showWindow(openArg); } @@ -1847,7 +1941,7 @@ int runModemApp(int argc, char** argv) { } if (sym == XK_Return || sym == XK_KP_Enter) { submitModalInput(); - render(); + if (visible) render(); continue; } continue; @@ -2069,7 +2163,7 @@ int runModemApp(int argc, char** argv) { sym == XF86XK_Send || sym == XF86XK_Phone || sym == XF86XK_PickupPhone) { handleEnterKey(); - render(); + if (visible) render(); } } } @@ -2088,10 +2182,10 @@ int runModemApp(int argc, char** argv) { sendPluginCommand("xdrawer", "TOGGLE"); } else if (pend == 1) { handleLeftSoftkey(); - render(); + if (visible) render(); } else if (pend == 2) { handleRightSoftkey(); - render(); + if (visible) render(); } } else { timeoutMs = static_cast(rem); @@ -2134,9 +2228,7 @@ int runModemApp(int argc, char** argv) { ++autoCheckStage; nextAutoCheckMs = now + 4500; refreshData(false); - if (status.slots[0].needsPin() || status.slots[0].needsPuk() || - status.slots[1].needsPin() || status.slots[1].needsPuk() || - status.mmState == "locked") { + if (anySimNeedsUnlock()) { autoCheckStage = 2; showWindow("unlock"); } else if ((status.slots[0].state == "present" ||