modem: prevent duplicate SIM PIN entry dialog in dual-SIM mode

This commit is contained in:
Luxferre committed 2026-10-28 08:32:05 +02:00
1 parent 2e2adcc7f2
commit 4aa087f372
6 files changed
+161 -52

No files matched your search

@@ -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)" 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 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" 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 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 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/^ //')" newstate="$(mmcli -m any -K 2>/dev/null | grep "^modem.generic.state " | cut -d':' -f2 | sed 's/^ //')"
Binary file not shown.
@@ -544,7 +544,7 @@ provision_slot() {
eval "other_app_state=\$SLOT${other_slot}_APP_STATE" eval "other_app_state=\$SLOT${other_slot}_APP_STATE"
need_prim=1 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 need_prim=0
fi fi
@@ -574,7 +574,7 @@ provision_slot() {
# Dual-SIM Dual Standby (DSDS): provision the second present SIM slot to secondary-gw-provisioning # Dual-SIM Dual Standby (DSDS): provision the second present SIM slot to secondary-gw-provisioning
if [ "$other_state" = "present" ]; then if [ "$other_state" = "present" ]; then
need_sec=1 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 need_sec=0
fi fi
if [ "$need_sec" = "1" ]; then if [ "$need_sec" = "1" ]; then
@@ -948,10 +948,15 @@ PY
exit 1 exit 1
fi fi
prepare_slot_for_pin_op "$slot" 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}" eval "before_ret=\${SLOT${slot}_PIN1_RETRIES:-3}"
verified=0 verified=0
wrong_pin=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)" 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 if [ -n "$sim_id" ]; then
mm_pin_out="$(timeout 5 mmcli -i "$sim_id" --pin="$pin" 2>&1)" && verified=1 || { mm_pin_out="$(timeout 5 mmcli -i "$sim_id" --pin="$pin" 2>&1)" && verified=1 || {
@@ -17,6 +17,13 @@ sxmo_debug "$2 (old: $1 [reason: $3])"
open_sim_unlock_ui() { 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 i=0
while [ "$i" -lt 40 ] && [ ! -p /tmp/dwm_plugin_modem.fifo ]; do while [ "$i" -lt 40 ] && [ ! -p /tmp/dwm_plugin_modem.fifo ]; do
sleep 0.2 sleep 0.2
Binary file not shown.
+141 -49
View File
@@ -136,11 +136,12 @@ struct ModemStatusData {
std::string regStatusLabel() const { std::string regStatusLabel() const {
if (slots[0].needsPuk() || slots[1].needsPuk() || if (slots[0].needsPuk() || slots[1].needsPuk() ||
mmUnlockRequired == "sim-puk") { (!qmiAvailable && mmUnlockRequired == "sim-puk")) {
return "SIM PUK Required"; return "SIM PUK Required";
} }
if (slots[0].needsPin() || slots[1].needsPin() || if (slots[0].needsPin() || slots[1].needsPin() ||
mmUnlockRequired == "sim-pin" || mmState == "locked") { (!qmiAvailable &&
(mmUnlockRequired == "sim-pin" || mmState == "locked"))) {
return "SIM PIN Required"; return "SIM PIN Required";
} }
if (mmRegState == "home" || if (mmRegState == "home" ||
@@ -432,6 +433,7 @@ ModemStatusData queryModemAndSimStatus() {
actIdx = std::clamp(d.preferredSlot, 1, 2) - 1; actIdx = std::clamp(d.preferredSlot, 1, 2) - 1;
} }
if (actIdx >= 0 && actIdx < 2) { if (actIdx >= 0 && actIdx < 2) {
if (!d.qmiAvailable) {
if (d.mmUnlockRequired == "sim-pin" && if (d.mmUnlockRequired == "sim-pin" &&
d.slots[actIdx].pin1State == "not-initialized") { d.slots[actIdx].pin1State == "not-initialized") {
d.slots[actIdx].state = "present"; d.slots[actIdx].state = "present";
@@ -440,6 +442,7 @@ ModemStatusData queryModemAndSimStatus() {
d.slots[actIdx].state = "present"; d.slots[actIdx].state = "present";
d.slots[actIdx].pin1State = "blocked"; d.slots[actIdx].pin1State = "blocked";
} }
}
if (d.mmPinRetries >= 0 && d.slots[actIdx].pin1Retries == 0) { if (d.mmPinRetries >= 0 && d.slots[actIdx].pin1Retries == 0) {
d.slots[actIdx].pin1Retries = d.mmPinRetries; d.slots[actIdx].pin1Retries = d.mmPinRetries;
} }
@@ -591,6 +594,11 @@ int runModemApp(int argc, char** argv) {
int64_t statusBannerUntilMs = 0; int64_t statusBannerUntilMs = 0;
bool visible = false; 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 openStartMs = 0;
int64_t startupIgnoreUntilMs = 0; int64_t startupIgnoreUntilMs = 0;
int64_t lastRefreshMs = 0; int64_t lastRefreshMs = 0;
@@ -605,6 +613,19 @@ int runModemApp(int argc, char** argv) {
auto refreshData = [&](bool reloadLog = false) { auto refreshData = [&](bool reloadLog = false) {
status = queryModemAndSimStatus(); 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) { if (reloadLog || activeTab == TabId::ModemLog) {
book.load(); book.load();
logRows = loadRecentModemLog(book); logRows = loadRecentModemLog(book);
@@ -612,13 +633,27 @@ int runModemApp(int argc, char** argv) {
logRowSel = std::max(0, static_cast<int>(logRows.size()) - 1); logRowSel = std::max(0, static_cast<int>(logRows.size()) - 1);
} }
} }
lastRefreshMs = nowMs(); lastRefreshMs = now;
if (visible && !isAnotherPluginActive("modem")) { if (visible && !isAnotherPluginActive("modem")) {
XRaiseWindow(dpy, win); XRaiseWindow(dpy, win);
grabPluginKeyboard(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 { auto defaultActiveOrPresentSlot = [&]() -> int {
if (status.slots[0].needsPin() || status.slots[0].needsPuk()) return 1; if (status.slots[0].needsPin() || status.slots[0].needsPuk()) return 1;
if (status.slots[1].needsPin() || status.slots[1].needsPuk()) return 2; if (status.slots[1].needsPin() || status.slots[1].needsPuk()) return 2;
@@ -648,7 +683,8 @@ int runModemApp(int argc, char** argv) {
inputPin2.clear(); inputPin2.clear();
inputPin3.clear(); inputPin3.clear();
modalError.clear(); modalError.clear();
if (s.needsPuk() || status.mmUnlockRequired == "sim-puk") { if (s.needsPuk() ||
(!status.qmiAvailable && status.mmUnlockRequired == "sim-puk")) {
modal = ModalMode::UnblockPuk; modal = ModalMode::UnblockPuk;
} else { } else {
modal = ModalMode::VerifyPin; modal = ModalMode::VerifyPin;
@@ -662,7 +698,9 @@ int runModemApp(int argc, char** argv) {
activeTab = TabId::SimCards; activeTab = TabId::SimCards;
int lockSlot = defaultActiveOrPresentSlot(); int lockSlot = defaultActiveOrPresentSlot();
simRowSel = lockSlot - 1; simRowSel = lockSlot - 1;
if (anySimNeedsUnlock()) {
openPinOrPukModalForSlot(lockSlot); openPinOrPukModalForSlot(lockSlot);
}
} else if (a == "info" || a == "status" || a == "radio") { } else if (a == "info" || a == "status" || a == "radio") {
activeTab = TabId::RadioInfo; activeTab = TabId::RadioInfo;
} else if (a == "log" || a == "logs") { } else if (a == "log" || a == "logs") {
@@ -1261,41 +1299,6 @@ int runModemApp(int argc, char** argv) {
grabPluginKeyboard(dpy, win); 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<int>(::getpid()));
closeOtherPlugins("modem");
XRaiseWindow(dpy, win);
grabKeys();
render();
return;
}
writeIntFile(activePidPath, static_cast<int>(::getpid()));
closeOtherPlugins("modem");
if (!visible) {
XMapRaised(dpy, win);
visible = true;
} else {
XRaiseWindow(dpy, win);
}
grabKeys();
render();
refreshData(true);
applyOpenArg(arg);
writeIntFile(activePidPath, static_cast<int>(::getpid()));
closeOtherPlugins("modem");
XRaiseWindow(dpy, win);
openStartMs = nowMs();
startupIgnoreUntilMs = openStartMs + 180;
initialSoftkeysReleased = (querySoftkeysDown(dpy) == 0);
softkeyPending = 0;
grabKeys();
render();
};
auto hideWindow = [&]() { auto hideWindow = [&]() {
if (!visible) return; if (!visible) return;
int xfdLocal = ConnectionNumber(dpy); int xfdLocal = ConnectionNumber(dpy);
@@ -1315,10 +1318,65 @@ int runModemApp(int argc, char** argv) {
XUnmapWindow(dpy, win); XUnmapWindow(dpy, win);
XFlush(dpy); XFlush(dpy);
visible = false; visible = false;
openedForUnlock = false;
modal = ModalMode::None; modal = ModalMode::None;
::unlink(activePidPath.c_str()); ::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<int>(::getpid()));
closeOtherPlugins("modem");
XRaiseWindow(dpy, win);
grabKeys();
render();
return;
}
if (isUnlockReq) {
refreshData(false);
if (!anySimNeedsUnlock()) {
if (visible && openedForUnlock) {
hideWindow();
}
return;
}
}
writeIntFile(activePidPath, static_cast<int>(::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<int>(::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) { auto switchSimSlotNow = [&](int slotNum, bool swapGw = false) {
slotNum = std::clamp(slotNum, 1, 2); slotNum = std::clamp(slotNum, 1, 2);
if (swapGw) { if (swapGw) {
@@ -1365,11 +1423,27 @@ int runModemApp(int argc, char** argv) {
runCaptureCmd("doas -n /usr/libexec/msm-modem-uim-selection verify-pin " + runCaptureCmd("doas -n /usr/libexec/msm-modem-uim-selection verify-pin " +
std::to_string(targetSlot) + " " + shellQuote(inputPin1) + std::to_string(targetSlot) + " " + shellQuote(inputPin1) +
" >/dev/null 2>&1", &rc); " >/dev/null 2>&1", &rc);
if (rc == 0) {
unlockedSlotMask |= (1 << (std::clamp(targetSlot, 1, 2) - 1));
lastUnlockSuccessMs = nowMs();
}
refreshData(false); refreshData(false);
runAsyncCmd("sxmo_hook_statusbar.sh modem >/dev/null 2>&1"); runAsyncCmd("sxmo_hook_statusbar.sh modem >/dev/null 2>&1");
if (rc == 0) { if (rc == 0) {
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; modal = ModalMode::None;
setBanner("SIM " + std::to_string(targetSlot) + " unlocked"); setBanner("SIM " + std::to_string(targetSlot) + " unlocked");
if (openedForUnlock) {
hideWindow();
}
}
} else { } else {
inputPin1.clear(); inputPin1.clear();
modalError = "Incorrect PIN"; modalError = "Incorrect PIN";
@@ -1401,11 +1475,27 @@ int runModemApp(int argc, char** argv) {
runCaptureCmd("doas -n /usr/libexec/msm-modem-uim-selection unblock-puk " + runCaptureCmd("doas -n /usr/libexec/msm-modem-uim-selection unblock-puk " +
std::to_string(targetSlot) + " " + shellQuote(inputPin1) + std::to_string(targetSlot) + " " + shellQuote(inputPin1) +
" " + shellQuote(inputPin2) + " >/dev/null 2>&1", &rc); " " + shellQuote(inputPin2) + " >/dev/null 2>&1", &rc);
if (rc == 0) {
unlockedSlotMask |= (1 << (std::clamp(targetSlot, 1, 2) - 1));
lastUnlockSuccessMs = nowMs();
}
refreshData(false); refreshData(false);
runAsyncCmd("sxmo_hook_statusbar.sh modem >/dev/null 2>&1"); runAsyncCmd("sxmo_hook_statusbar.sh modem >/dev/null 2>&1");
if (rc == 0) { if (rc == 0) {
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; modal = ModalMode::None;
setBanner("SIM unblocked and new PIN set"); setBanner("SIM unblocked and new PIN set");
if (openedForUnlock) {
hideWindow();
}
}
} else { } else {
wizardStep = 0; wizardStep = 0;
inputPin1.clear(); inputPin1.clear();
@@ -1556,7 +1646,11 @@ int runModemApp(int argc, char** argv) {
} }
switchSimSlotNow(otherGwSlot, true); switchSimSlotNow(otherGwSlot, true);
} else if (row == 4) { } else if (row == 4) {
if (anySimNeedsUnlock()) {
openPinOrPukModalForSlot(defaultActiveOrPresentSlot()); openPinOrPukModalForSlot(defaultActiveOrPresentSlot());
} else {
setBanner("SIM cards are already unlocked");
}
} else if (row == 5) { } else if (row == 5) {
targetSlot = defaultActiveOrPresentSlot(); targetSlot = defaultActiveOrPresentSlot();
wizardStep = 0; wizardStep = 0;
@@ -1668,6 +1762,9 @@ int runModemApp(int argc, char** argv) {
runAsyncCmd("mmcli -m any --3gpp-ussd-cancel >/dev/null 2>&1"); runAsyncCmd("mmcli -m any --3gpp-ussd-cancel >/dev/null 2>&1");
} }
modal = ModalMode::None; modal = ModalMode::None;
if (openedForUnlock) {
hideWindow();
}
return; return;
} }
hideWindow(); hideWindow();
@@ -1744,9 +1841,6 @@ int runModemApp(int argc, char** argv) {
} }
}; };
int autoCheckStage = isPreload ? 0 : 2;
int64_t nextAutoCheckMs = nowMs() + 2500;
if (!isPreload) { if (!isPreload) {
showWindow(openArg); showWindow(openArg);
} }
@@ -1847,7 +1941,7 @@ int runModemApp(int argc, char** argv) {
} }
if (sym == XK_Return || sym == XK_KP_Enter) { if (sym == XK_Return || sym == XK_KP_Enter) {
submitModalInput(); submitModalInput();
render(); if (visible) render();
continue; continue;
} }
continue; continue;
@@ -2069,7 +2163,7 @@ int runModemApp(int argc, char** argv) {
sym == XF86XK_Send || sym == XF86XK_Phone || sym == XF86XK_Send || sym == XF86XK_Phone ||
sym == XF86XK_PickupPhone) { sym == XF86XK_PickupPhone) {
handleEnterKey(); handleEnterKey();
render(); if (visible) render();
} }
} }
} }
@@ -2088,10 +2182,10 @@ int runModemApp(int argc, char** argv) {
sendPluginCommand("xdrawer", "TOGGLE"); sendPluginCommand("xdrawer", "TOGGLE");
} else if (pend == 1) { } else if (pend == 1) {
handleLeftSoftkey(); handleLeftSoftkey();
render(); if (visible) render();
} else if (pend == 2) { } else if (pend == 2) {
handleRightSoftkey(); handleRightSoftkey();
render(); if (visible) render();
} }
} else { } else {
timeoutMs = static_cast<int>(rem); timeoutMs = static_cast<int>(rem);
@@ -2134,9 +2228,7 @@ int runModemApp(int argc, char** argv) {
++autoCheckStage; ++autoCheckStage;
nextAutoCheckMs = now + 4500; nextAutoCheckMs = now + 4500;
refreshData(false); refreshData(false);
if (status.slots[0].needsPin() || status.slots[0].needsPuk() || if (anySimNeedsUnlock()) {
status.slots[1].needsPin() || status.slots[1].needsPuk() ||
status.mmState == "locked") {
autoCheckStage = 2; autoCheckStage = 2;
showWindow("unlock"); showWindow("unlock");
} else if ((status.slots[0].state == "present" || } else if ((status.slots[0].state == "present" ||