diff --git a/overlay/rootfs/usr/bin/sxmo_modemcall.sh b/overlay/rootfs/usr/bin/sxmo_modemcall.sh index 3e3bced..8daa111 100755 --- a/overlay/rootfs/usr/bin/sxmo_modemcall.sh +++ b/overlay/rootfs/usr/bin/sxmo_modemcall.sh @@ -125,7 +125,6 @@ hangup() { case "$CALLID" in sub1_*|sub0_*) rm -f "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}."* 2>/dev/null || true - touch "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.hangedupcall" doas -n /usr/libexec/msm-modem-uim-selection call-hangup "$CALLID" >/dev/null 2>&1 || \ /usr/libexec/msm-modem-uim-selection call-hangup "$CALLID" >/dev/null 2>&1 || true doas -n dbus-send --system --type=signal / org.ofono.VoiceCallManager.CallRemoved >/dev/null 2>&1 || true diff --git a/overlay/rootfs/usr/lib/dwm/plugins/calllog.so b/overlay/rootfs/usr/lib/dwm/plugins/calllog.so index f35bf7d..c9b1d65 100755 Binary files a/overlay/rootfs/usr/lib/dwm/plugins/calllog.so and b/overlay/rootfs/usr/lib/dwm/plugins/calllog.so differ diff --git a/overlay/rootfs/usr/lib/dwm/plugins/callscreen.so b/overlay/rootfs/usr/lib/dwm/plugins/callscreen.so index f26ee00..164f9e1 100755 Binary files a/overlay/rootfs/usr/lib/dwm/plugins/callscreen.so and b/overlay/rootfs/usr/lib/dwm/plugins/callscreen.so differ diff --git a/overlay/rootfs/usr/lib/dwm/plugins/contacts.so b/overlay/rootfs/usr/lib/dwm/plugins/contacts.so index 16d6ba8..6161887 100755 Binary files a/overlay/rootfs/usr/lib/dwm/plugins/contacts.so and b/overlay/rootfs/usr/lib/dwm/plugins/contacts.so differ diff --git a/overlay/rootfs/usr/lib/dwm/plugins/dialer.so b/overlay/rootfs/usr/lib/dwm/plugins/dialer.so index ebe7a33..685dd04 100755 Binary files a/overlay/rootfs/usr/lib/dwm/plugins/dialer.so and b/overlay/rootfs/usr/lib/dwm/plugins/dialer.so differ diff --git a/overlay/rootfs/usr/lib/dwm/plugins/modem.so b/overlay/rootfs/usr/lib/dwm/plugins/modem.so index 26155d4..e39db99 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/lib/dwm/plugins/sms.so b/overlay/rootfs/usr/lib/dwm/plugins/sms.so index 8e2c40f..163a4d8 100755 Binary files a/overlay/rootfs/usr/lib/dwm/plugins/sms.so and b/overlay/rootfs/usr/lib/dwm/plugins/sms.so differ diff --git a/overlay/rootfs/usr/libexec/msm-modem-uim-selection b/overlay/rootfs/usr/libexec/msm-modem-uim-selection index b9d8770..d48c01a 100755 --- a/overlay/rootfs/usr/libexec/msm-modem-uim-selection +++ b/overlay/rootfs/usr/libexec/msm-modem-uim-selection @@ -1272,6 +1272,34 @@ PY fi ussd_code="$*" if [ "$ussd_mode" = "cancel" ]; then + rm -f /tmp/sxmo_ussd_session_state + if command -v python3 >/dev/null 2>&1; then + python3 - << 'PY' 2>/dev/null || true +import socket, struct, select +AF_QIPCRTR = 42 +ls = socket.socket(AF_QIPCRTR, socket.SOCK_DGRAM) +node, _ = ls.getsockname() +ls.sendto(struct.pack("= 20: + cmd, srv, inst, n, p = struct.unpack("/dev/null 2>&1 || true "$0" at auto 'AT+CUSD=2' >/dev/null 2>&1 || true echo "USSD session cancelled." @@ -1291,13 +1319,14 @@ PY sec_slot="${SECONDARY_GW_SLOT:-0}" fi fi - # If requested SIM is Secondary GW, try QMI VOICE USSD on sub=1 or secondary AT port first + # If requested SIM is Secondary GW, use QMI VOICE USSD on sub=1 if [ "$req_sim" = "$sec_slot" ] && [ "$sec_slot" != "0" ] && command -v python3 >/dev/null 2>&1; then - qmi_ussd="$(python3 - "$ussd_mode" "$ussd_code" << 'PY' 2>/dev/null || true -import socket, struct, select, sys, time + qmi_ussd="$(python3 - "$ussd_mode" "$ussd_code" "1" << 'PY' 2>/dev/null || true +import os, socket, struct, select, sys, time mode = sys.argv[1] code = sys.argv[2] +sub = int(sys.argv[3]) if len(sys.argv) > 3 and sys.argv[3].isdigit() else 1 AF_QIPCRTR = 42 QRTR_PORT_CTRL = 0xfffffffe QRTR_TYPE_NEW_SERVER = 4 @@ -1325,20 +1354,61 @@ if not v_addr: sys.exit(1) s = socket.socket(AF_QIPCRTR, socket.SOCK_DGRAM) -s.sendto(struct.pack("= 2 and raw[0] == 0x04): + return raw.decode("utf-16-be", errors="ignore").strip() + return raw.decode("utf-8", errors="ignore").strip() + +def decode_utf16le_tlv(val): + if len(val) < 3: + return "" + nchars = val[0] + raw = val[1:1 + nchars * 2] + return raw.decode("utf-16-le", errors="ignore").strip() + +def write_session_state(active): + try: + with open("/tmp/sxmo_ussd_session_state", "w") as f: + f.write("user-response\n" if active else "idle\n") + except Exception: + pass + +deadline = time.time() + 14.0 while time.time() < deadline: r, _, _ = select.select([s], [], [], 0.5) if not r: @@ -1346,26 +1416,47 @@ while time.time() < deadline: pkt, _ = s.recvfrom(2048) if len(pkt) < 7: continue - _, txn, mid, _ = struct.unpack("= 3: - dcs = val[0] - ulen = val[1] - raw = val[2:2+ulen] - if dcs in (2, 0x48) or (len(raw) >= 2 and raw[0] == 0x04): - txt = raw.decode("utf-16-be", errors="ignore").strip() - else: - txt = raw.decode("utf-8", errors="ignore").strip() - if txt: - print(txt) - s.close() - sys.exit(0) + flags, txn, mid, _ = struct.unpack("= 1 else 1 + txt = "" + if 0x11 in tlvs: + txt = decode_utf16le_tlv(tlvs[0x11]) + if not txt and 0x10 in tlvs: + txt = decode_uss_bytes(tlvs[0x10]) + if txt: + write_session_state(user_action == 2) + print(txt) + s.close() + sys.exit(0) + elif mid in (0x003A, 0x003B): + if 0x02 in tlvs and len(tlvs[0x02]) >= 4: + res, err = struct.unpack("= 1: cnt = raw_ci[0] off = 1 @@ -1771,7 +1862,7 @@ def parse_call_info(tlvs): cid, cstate, ctype, cdir, cmode, _, _ = struct.unpack("= 1: cnt = raw_num[0] off = 1 @@ -2180,7 +2271,7 @@ def poll_stored_sms(wms_sock, wms_addr, txn, sim_slot): def parse_call_info(tlvs): calls = {} - raw_ci = tlvs.get(0x10) or tlvs.get(0x01) or b"" + raw_ci = tlvs[0x01] if 0x01 in tlvs else tlvs.get(0x10, b"") if len(raw_ci) >= 1: cnt = raw_ci[0] off = 1 @@ -2190,7 +2281,7 @@ def parse_call_info(tlvs): cid, cstate, ctype, cdir, cmode, _, _ = struct.unpack("= 1: cnt = raw_num[0] off = 1 @@ -2232,9 +2323,24 @@ def set_q6voiced_audio(enable): subprocess.run(["dbus-send", "--system", "--type=signal", "/", "org.ofono.VoiceCallManager.CallRemoved"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) run_user_cmd("sxmo_modemaudio.sh teardown") +def has_sub1_call_files(): + try: + return any(fn.startswith("sub1_") for fn in os.listdir(CALLS_DIR)) + except Exception: + return False + def sync_sub1_calls(calls, sim_slot): global active_calls now_str = datetime.datetime.now().astimezone().isoformat(timespec="seconds") + for old_cid, old_info in list(active_calls.items()): + if old_cid not in calls: + calls[old_cid] = { + "id": old_cid, + "state": 9, + "dir": old_info.get("dir", 1), + "num": old_info.get("num", ""), + } + for cid, c in calls.items(): st = STATE_MAP.get(c["state"], "unknown") num = c["num"] or active_calls.get(cid, {}).get("num") or "Unknown" @@ -2283,12 +2389,23 @@ def sync_sub1_calls(calls, sim_slot): evt = "call_missed" if prev == "ringing-in" else "call_hangup" with open(os.path.join(LOG_DIR, "modemlog.tsv"), "a", encoding="utf-8") as f: f.write(f"{now_str}\t{evt}\t{num}\t[SIM {sim_slot}]\n") - send_fifo("/tmp/dwm_plugin_callscreen.fifo", f"HANGUP {call_key}") + send_fifo("/tmp/dwm_plugin_callscreen.fifo", "ENDED CALL ENDED") del active_calls[cid] - has_live = any(v["state"] in ("dialing", "ringing-out", "active", "held") for v in active_calls.values()) - if not has_live and audio_opened: - set_q6voiced_audio(False) + has_live = any(v["state"] in ("dialing", "ringing-out", "active", "held", "ringing-in", "waiting") for v in active_calls.values()) + if not has_live: + removed_stale = False + for fn in os.listdir(CALLS_DIR): + if fn.startswith("sub1_"): + try: + os.remove(os.path.join(CALLS_DIR, fn)) + removed_stale = True + except Exception: + pass + if removed_stale: + send_fifo("/tmp/dwm_plugin_callscreen.fifo", "ENDED CALL ENDED") + if audio_opened: + set_q6voiced_audio(False) while True: wms_addr = lookup_srv(QMI_SERVICE_WMS) @@ -2298,14 +2415,14 @@ while True: time.sleep(3) continue - sec_slot = 1 + sec_slot = 2 if uim_addr: us = socket.socket(AF_QIPCRTR, socket.SOCK_DGRAM) ut = qmi_req(us, uim_addr, 1, 0x002F) us.close() st_val = ut.get(0x10, b"") - if len(st_val) >= 6: - _, _, _, _, sec_gw_idx = struct.unpack("= 10 else (0, 0, 0, 0, 0xFFFF) + if len(st_val) >= 8: + _, _, sec_gw_idx, _ = struct.unpack("> 8) & 0xFF) + 1 @@ -2322,13 +2439,18 @@ while True: qmi_req(vs, voice_addr, v_txn, 0x0044, struct.pack("= 15.0: w_txn = poll_stored_sms(ws, wms_addr, w_txn, sec_slot) last_poll = time.time() diff --git a/prebuilt/sxmo-x11/usr/lib/dwm/plugins/calllog.so b/prebuilt/sxmo-x11/usr/lib/dwm/plugins/calllog.so index f35bf7d..c9b1d65 100755 Binary files a/prebuilt/sxmo-x11/usr/lib/dwm/plugins/calllog.so and b/prebuilt/sxmo-x11/usr/lib/dwm/plugins/calllog.so differ diff --git a/prebuilt/sxmo-x11/usr/lib/dwm/plugins/callscreen.so b/prebuilt/sxmo-x11/usr/lib/dwm/plugins/callscreen.so index f26ee00..164f9e1 100755 Binary files a/prebuilt/sxmo-x11/usr/lib/dwm/plugins/callscreen.so and b/prebuilt/sxmo-x11/usr/lib/dwm/plugins/callscreen.so differ diff --git a/prebuilt/sxmo-x11/usr/lib/dwm/plugins/contacts.so b/prebuilt/sxmo-x11/usr/lib/dwm/plugins/contacts.so index 16d6ba8..6161887 100755 Binary files a/prebuilt/sxmo-x11/usr/lib/dwm/plugins/contacts.so and b/prebuilt/sxmo-x11/usr/lib/dwm/plugins/contacts.so differ diff --git a/prebuilt/sxmo-x11/usr/lib/dwm/plugins/dialer.so b/prebuilt/sxmo-x11/usr/lib/dwm/plugins/dialer.so index ebe7a33..685dd04 100755 Binary files a/prebuilt/sxmo-x11/usr/lib/dwm/plugins/dialer.so and b/prebuilt/sxmo-x11/usr/lib/dwm/plugins/dialer.so differ diff --git a/prebuilt/sxmo-x11/usr/lib/dwm/plugins/modem.so b/prebuilt/sxmo-x11/usr/lib/dwm/plugins/modem.so index 26155d4..e39db99 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/prebuilt/sxmo-x11/usr/lib/dwm/plugins/sms.so b/prebuilt/sxmo-x11/usr/lib/dwm/plugins/sms.so index 8e2c40f..163a4d8 100755 Binary files a/prebuilt/sxmo-x11/usr/lib/dwm/plugins/sms.so and b/prebuilt/sxmo-x11/usr/lib/dwm/plugins/sms.so differ diff --git a/src/dwm-plugins/callscreen_plugin.cpp b/src/dwm-plugins/callscreen_plugin.cpp index 0db06f9..a53a335 100644 --- a/src/dwm-plugins/callscreen_plugin.cpp +++ b/src/dwm-plugins/callscreen_plugin.cpp @@ -474,16 +474,6 @@ int callscreen_run_main(int argc, char** argv) { return spk ? (68 + (c * 26) / 100) : (68 + (c * 24) / 100); }; - auto hwVolToPct = [](long hwVol, bool spk) -> int { - if (hwVol < 68) { - return 85; - } - int span = spk ? 26 : 24; - int pct = static_cast(std::lround(((hwVol - 68) * 100.0) / span)); - pct = std::clamp(pct, 5, 100); - return std::max(5, ((pct + 2) / 5) * 5); - }; - auto refreshHardwareAudioState = [&]() { unsigned int dec1 = alsa.getEnum("DEC1 MUX", 1); if (dec1 > 0) { @@ -1288,68 +1278,235 @@ int callscreen_run_main(int argc, char** argv) { render(); }; + auto queryQmiVoiceCalls = [&](std::string& outId, std::string& outDir, + std::string& outState, std::string& outNum, + bool& outSawTerminated) -> bool { + constexpr int kAfQipcrtr = 42; + constexpr uint32_t kQrtrPortCtrl = 0xfffffffeu; + constexpr uint32_t kQrtrTypeNewServer = 4; + constexpr uint32_t kQrtrTypeNewLookup = 10; + constexpr uint32_t kQmiServiceVoice = 9; + + struct SockAddrQrtr { + unsigned short sq_family; + uint32_t sq_node; + uint32_t sq_port; + }; + struct QrtrCtrlPkt { + uint32_t cmd; + uint32_t service; + uint32_t instance; + uint32_t node; + uint32_t port; + } __attribute__((packed)); + + int lfd = ::socket(kAfQipcrtr, SOCK_DGRAM | SOCK_CLOEXEC, 0); + if (lfd < 0) { + return false; + } + SockAddrQrtr localSq {}; + socklen_t sl = sizeof(localSq); + if (::getsockname(lfd, reinterpret_cast(&localSq), &sl) < 0) { + ::close(lfd); + return false; + } + SockAddrQrtr ctrlSq { kAfQipcrtr, localSq.sq_node, kQrtrPortCtrl }; + QrtrCtrlPkt lookup { kQrtrTypeNewLookup, kQmiServiceVoice, 0, 0, 0 }; + if (::sendto(lfd, &lookup, sizeof(lookup), 0, + reinterpret_cast(&ctrlSq), + sizeof(ctrlSq)) < 0) { + ::close(lfd); + return false; + } + uint32_t vNode = 0, vPort = 0; + bool foundSrv = false; + for (int i = 0; i < 8; ++i) { + struct pollfd pfd { lfd, POLLIN, 0 }; + if (::poll(&pfd, 1, 40) <= 0) break; + QrtrCtrlPkt pkt {}; + ssize_t n = ::recv(lfd, &pkt, sizeof(pkt), 0); + if (n >= static_cast(sizeof(pkt)) && pkt.cmd == kQrtrTypeNewServer) { + if (pkt.service == kQmiServiceVoice && pkt.port != 0) { + vNode = pkt.node; + vPort = pkt.port; + foundSrv = true; + break; + } + if (pkt.service == 0 && pkt.node == 0 && pkt.port == 0) break; + } + } + ::close(lfd); + if (!foundSrv) { + return false; + } + + SockAddrQrtr vSq { kAfQipcrtr, vNode, vPort }; + for (int sub : {1, 0}) { + int sfd = ::socket(kAfQipcrtr, SOCK_DGRAM | SOCK_CLOEXEC, 0); + if (sfd < 0) continue; + uint8_t bindReq[11] = { + 0x00, 0x01, 0x00, 0x44, 0x00, 0x04, 0x00, + 0x01, 0x01, 0x00, static_cast(sub) + }; + ::sendto(sfd, bindReq, sizeof(bindReq), 0, + reinterpret_cast(&vSq), sizeof(vSq)); + struct pollfd pfd { sfd, POLLIN, 0 }; + if (::poll(&pfd, 1, 40) > 0) { + uint8_t tmp[256]; + ::recv(sfd, tmp, sizeof(tmp), 0); + } + uint8_t infoReq[7] = { 0x00, 0x02, 0x00, 0x2F, 0x00, 0x00, 0x00 }; + ::sendto(sfd, infoReq, sizeof(infoReq), 0, + reinterpret_cast(&vSq), sizeof(vSq)); + if (::poll(&pfd, 1, 50) > 0) { + uint8_t buf[1024]; + ssize_t rlen = ::recv(sfd, buf, sizeof(buf), 0); + if (rlen >= 7 && buf[0] == 0x02 && buf[3] == 0x2F) { + const uint8_t* rawCi = nullptr; + uint16_t lenCi = 0; + const uint8_t* rawNum = nullptr; + uint16_t lenNum = 0; + size_t pos = 7; + while (pos + 3 <= static_cast(rlen)) { + uint8_t tt = buf[pos]; + uint16_t tl = static_cast(buf[pos + 1]) | + (static_cast(buf[pos + 2]) << 8); + pos += 3; + if (pos + tl > static_cast(rlen)) break; + if (tt == 0x10) { + rawCi = buf + pos; + lenCi = tl; + } else if (tt == 0x11) { + rawNum = buf + pos; + lenNum = tl; + } + pos += tl; + } + if (rawCi && lenCi >= 1) { + uint8_t cnt = rawCi[0]; + size_t off = 1; + for (uint8_t i = 0; i < cnt && off + 7 <= lenCi; ++i, off += 7) { + uint8_t cid = rawCi[off]; + uint8_t cstate = rawCi[off + 1]; + uint8_t cdir = rawCi[off + 3]; + if (cstate == 8 || cstate == 9) { + outSawTerminated = true; + continue; + } + std::string st; + if (cstate == 3) st = "active"; + else if (cstate == 2 || cstate == 7) st = "ringing-in"; + else if (cstate == 5) st = "ringing-out"; + else if (cstate == 1 || cstate == 4 || cstate == 10) st = "dialing"; + else if (cstate == 6) st = "held"; + if (!st.empty() && outId.empty()) { + outId = (sub == 1) ? ("sub1_" + std::to_string(cid)) + : std::to_string(cid); + outState = st; + outDir = (cdir == 2) ? "incoming" : "outgoing"; + if (rawNum && lenNum >= 1) { + uint8_t ncnt = rawNum[0]; + size_t noff = 1; + for (uint8_t ni = 0; ni < ncnt && noff + 3 <= lenNum; ++ni) { + uint8_t ncid = rawNum[noff]; + uint8_t nlen = rawNum[noff + 2]; + noff += 3; + if (noff + nlen > lenNum) break; + if (ncid == cid && nlen > 0) { + outNum.assign(reinterpret_cast(rawNum + noff), nlen); + outNum = trimStr(outNum); + } + noff += nlen; + } + } + } + } + } + } + } + ::close(sfd); + } + return true; + }; + auto syncModemCalls = [&]() { if (callId == "sim" || callId == "test") { return; } - FILE* fp = ::popen("mmcli -m any --voice-list-calls -a 2>/dev/null", "r"); - if (!fp) { - return; - } - char line[512]; std::string foundId; std::string foundDir; std::string foundState; + std::string foundNum; + bool sawTerminated = false; - while (std::fgets(line, sizeof(line), fp)) { - std::string s(line); - if (s.find("terminated") != std::string::npos || - s.find("No calls") != std::string::npos) { - continue; + FILE* fp = ::popen("mmcli -m any --voice-list-calls -a 2>/dev/null", "r"); + if (fp) { + char line[512]; + while (std::fgets(line, sizeof(line), fp)) { + std::string s(line); + if (s.find("No calls") != std::string::npos) { + continue; + } + if (s.find("terminated") != std::string::npos) { + sawTerminated = true; + continue; + } + size_t callPos = s.find("/Call/"); + if (callPos == std::string::npos) { + continue; + } + size_t idStart = callPos + 6; + size_t idEnd = idStart; + while (idEnd < s.size() && std::isdigit(static_cast(s[idEnd]))) { + ++idEnd; + } + if (idEnd == idStart) { + continue; + } + foundId = s.substr(idStart, idEnd - idStart); + if (s.find("incoming") != std::string::npos) { + foundDir = "incoming"; + } else if (s.find("outgoing") != std::string::npos) { + foundDir = "outgoing"; + } + size_t lp = s.find('('); + size_t rp = s.find(')', lp == std::string::npos ? 0 : lp); + if (lp != std::string::npos && rp != std::string::npos && rp > lp + 1) { + foundState = s.substr(lp + 1, rp - lp - 1); + } + break; } - size_t callPos = s.find("/Call/"); - if (callPos == std::string::npos) { - continue; - } - size_t idStart = callPos + 6; - size_t idEnd = idStart; - while (idEnd < s.size() && std::isdigit(static_cast(s[idEnd]))) { - ++idEnd; - } - if (idEnd == idStart) { - continue; - } - foundId = s.substr(idStart, idEnd - idStart); - if (s.find("incoming") != std::string::npos) { - foundDir = "incoming"; - } else if (s.find("outgoing") != std::string::npos) { - foundDir = "outgoing"; - } - size_t lp = s.find('('); - size_t rp = s.find(')', lp == std::string::npos ? 0 : lp); - if (lp != std::string::npos && rp != std::string::npos && rp > lp + 1) { - foundState = s.substr(lp + 1, rp - lp - 1); - } - break; + ::pclose(fp); + } + + if (sawTerminated) { + runAsyncCmd("sxmo_modem.sh checkforfinishedcalls >/dev/null 2>&1"); + } + + if (foundId.empty()) { + queryQmiVoiceCalls(foundId, foundDir, foundState, foundNum, sawTerminated); } - ::pclose(fp); if (!foundId.empty()) { if (callId.empty() || callId == "-") { callId = foundId; } if (callNumber.empty()) { - char numCmd[160]; - std::snprintf(numCmd, sizeof(numCmd), - "sxmo_modemcall.sh vid_to_number '%s' 2>/dev/null", - foundId.c_str()); - FILE* nfp = ::popen(numCmd, "r"); - if (nfp) { - char nbuf[128]; - if (std::fgets(nbuf, sizeof(nbuf), nfp)) { - updateCallerDetails(foundId, trimStr(nbuf)); + if (!foundNum.empty()) { + updateCallerDetails(foundId, foundNum); + } else { + char numCmd[160]; + std::snprintf(numCmd, sizeof(numCmd), + "sxmo_modemcall.sh vid_to_number '%s' 2>/dev/null", + foundId.c_str()); + FILE* nfp = ::popen(numCmd, "r"); + if (nfp) { + char nbuf[128]; + if (std::fgets(nbuf, sizeof(nbuf), nfp)) { + updateCallerDetails(foundId, trimStr(nbuf)); + } + ::pclose(nfp); } - ::pclose(nfp); } } if (foundState == "active" && callState != CallState::Active) { @@ -1377,26 +1534,22 @@ int callscreen_run_main(int argc, char** argv) { render(); } } else if (callState == CallState::Active || callState == CallState::Incoming || - (callState == CallState::Outgoing && nowMs() - callStartMs > 6000)) { - if (!isCallActiveOrRingingFast() || ::access("/tmp/sxmo_active_call", F_OK) == 0) { - // Verify sxmo_calls dir is empty before auto-ending - std::string callsDir = getRuntimeFile("sxmo_calls"); - bool anyFile = false; - DIR* d = ::opendir(callsDir.c_str()); - if (d) { - struct dirent* ent; - while ((ent = ::readdir(d)) != nullptr) { - if (ent->d_name[0] != '.' && ent->d_name[0] != '\0') { - anyFile = true; - break; - } + sawTerminated || + (callState == CallState::Outgoing && + ((!callId.empty() && callId != "-") || nowMs() - callStartMs > 3500))) { + std::string callsDir = getRuntimeFile("sxmo_calls"); + DIR* d = ::opendir(callsDir.c_str()); + if (d) { + struct dirent* ent; + while ((ent = ::readdir(d)) != nullptr) { + if (ent->d_name[0] != '.' && ent->d_name[0] != '\0') { + std::string fpPath = callsDir + "/" + ent->d_name; + ::unlink(fpPath.c_str()); } - ::closedir(d); - } - if (!anyFile) { - transitionToEnded("CALL ENDED"); } + ::closedir(d); } + transitionToEnded("CALL ENDED"); } }; diff --git a/src/dwm-plugins/phone_data.h b/src/dwm-plugins/phone_data.h index c0cb505..bde53be 100644 --- a/src/dwm-plugins/phone_data.h +++ b/src/dwm-plugins/phone_data.h @@ -1579,6 +1579,37 @@ inline DualSimInfo queryDualSimInfo(bool forceRefresh = false) { bool hasNas = lookupService(kQmiServiceNas, nasNode, nasPort); bool hasDms = lookupService(kQmiServiceDms, dmsNode, dmsPort); + auto recvQmiResp = [](int sfd, uint16_t expTxn, uint16_t expMsg, + uint8_t* outBuf, size_t maxLen, int timeoutMs) -> ssize_t { + int64_t deadline = nowMs() + timeoutMs; + while (nowMs() < deadline) { + int rem = static_cast(std::max(5, deadline - nowMs())); + struct pollfd pfd { sfd, POLLIN, 0 }; + if (::poll(&pfd, 1, rem) <= 0) break; + ssize_t n = ::recv(sfd, outBuf, maxLen, 0); + if (n >= 7 && outBuf[0] == 0x02) { + uint16_t rTxn = static_cast(outBuf[1]) | + (static_cast(outBuf[2]) << 8); + uint16_t rMsg = static_cast(outBuf[3]) | + (static_cast(outBuf[4]) << 8); + if (rTxn == expTxn && rMsg == expMsg) { + return n; + } + } + } + return -1; + }; + + auto cleanPrintable = [](const std::string& s) -> std::string { + std::string out; + for (unsigned char c : s) { + if (c >= 32 && c < 127) { + out.push_back(static_cast(c)); + } + } + return trimStr(out); + }; + auto querySubMeta = [&](int slot, int sub, std::string& outOp, std::string& outRat, std::string& outNum) { if (sub < 0) return; @@ -1586,45 +1617,59 @@ inline DualSimInfo queryDualSimInfo(bool forceRefresh = false) { int sfd = ::socket(kAfQipcrtr, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sfd >= 0) { SockAddrQrtr nasSq { kAfQipcrtr, nasNode, nasPort }; + uint8_t buf[1024]; uint8_t bindReq[11] = { 0x00, 0x01, 0x00, 0x45, 0x00, 0x04, 0x00, 0x01, 0x01, 0x00, static_cast(sub) }; ::sendto(sfd, bindReq, sizeof(bindReq), 0, reinterpret_cast(&nasSq), sizeof(nasSq)); - struct pollfd pfd { sfd, POLLIN, 0 }; - if (::poll(&pfd, 1, 40) > 0) { - uint8_t tmp[256]; - ::recv(sfd, tmp, sizeof(tmp), 0); - } - uint8_t ssReq[7] = { 0x00, 0x02, 0x00, 0x24, 0x00, 0x00, 0x00 }; - ::sendto(sfd, ssReq, sizeof(ssReq), 0, + recvQmiResp(sfd, 1, 0x0045, buf, sizeof(buf), 60); + + uint8_t sysReq[7] = { 0x00, 0x02, 0x00, 0x4D, 0x00, 0x00, 0x00 }; + ::sendto(sfd, sysReq, sizeof(sysReq), 0, reinterpret_cast(&nasSq), sizeof(nasSq)); - if (::poll(&pfd, 1, 50) > 0) { - uint8_t buf[1024]; - ssize_t rlen = ::recv(sfd, buf, sizeof(buf), 0); - if (rlen >= 7 && buf[0] == 0x02 && buf[3] == 0x24) { - size_t pos = 7; - while (pos + 3 <= static_cast(rlen)) { - uint8_t tt = buf[pos]; - uint16_t tl = static_cast(buf[pos + 1]) | - (static_cast(buf[pos + 2]) << 8); - pos += 3; - if (pos + tl > static_cast(rlen)) break; - if (tt == 0x01 && tl >= 5 && buf[pos + 3] >= 1) { - uint8_t rb = buf[pos + 4]; - if (rb == 8) outRat = "4G"; - else if (rb == 5) outRat = "3G"; - else if (rb == 4) outRat = "2G"; - } else if (tt == 0x12 && tl >= 5) { - uint8_t dlen = buf[pos + 4]; - if (5 + dlen <= tl && dlen > 0) { - outOp.assign(reinterpret_cast(buf + pos + 5), dlen); - outOp = trimStr(outOp); - } - } - pos += tl; + ssize_t rlen = recvQmiResp(sfd, 2, 0x004D, buf, sizeof(buf), 60); + if (rlen >= 7) { + size_t pos = 7; + while (pos + 3 <= static_cast(rlen)) { + uint8_t tt = buf[pos]; + uint16_t tl = static_cast(buf[pos + 1]) | + (static_cast(buf[pos + 2]) << 8); + pos += 3; + if (pos + tl > static_cast(rlen)) break; + if (tl >= 1 && buf[pos] == 2) { + if (tt == 0x14) outRat = "4G"; + else if (tt == 0x13 && outRat.empty()) outRat = "3G"; + else if (tt == 0x12 && outRat.empty()) outRat = "2G"; } + pos += tl; + } + } + + uint8_t opReq[7] = { 0x00, 0x03, 0x00, 0x39, 0x00, 0x00, 0x00 }; + ::sendto(sfd, opReq, sizeof(opReq), 0, + reinterpret_cast(&nasSq), sizeof(nasSq)); + rlen = recvQmiResp(sfd, 3, 0x0039, buf, sizeof(buf), 60); + if (rlen >= 7) { + size_t pos = 7; + while (pos + 3 <= static_cast(rlen)) { + uint8_t tt = buf[pos]; + uint16_t tl = static_cast(buf[pos + 1]) | + (static_cast(buf[pos + 2]) << 8); + pos += 3; + if (pos + tl > static_cast(rlen)) break; + if (tt == 0x10 && tl >= 2) { + uint8_t slen = buf[pos + 1]; + if (2 + slen <= tl && slen > 0) { + std::string raw(reinterpret_cast(buf + pos + 2), slen); + outOp = cleanPrintable(raw); + } + } else if (tt == 0x13 && tl > 0 && outOp.empty()) { + std::string raw(reinterpret_cast(buf + pos), tl); + outOp = cleanPrintable(raw); + } + pos += tl; } } ::close(sfd); @@ -1634,40 +1679,40 @@ inline DualSimInfo queryDualSimInfo(bool forceRefresh = false) { int sfd = ::socket(kAfQipcrtr, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sfd >= 0) { SockAddrQrtr dmsSq { kAfQipcrtr, dmsNode, dmsPort }; - uint8_t bindReq[11] = { - 0x00, 0x01, 0x00, 0x54, 0x00, 0x04, 0x00, - 0x01, 0x01, 0x00, static_cast(sub) + uint8_t buf[512]; + uint8_t bindReq[14] = { + 0x00, 0x01, 0x00, 0x54, 0x00, 0x07, 0x00, + 0x01, 0x04, 0x00, static_cast(sub + 1), 0x00, 0x00, 0x00 }; ::sendto(sfd, bindReq, sizeof(bindReq), 0, reinterpret_cast(&dmsSq), sizeof(dmsSq)); - struct pollfd pfd { sfd, POLLIN, 0 }; - if (::poll(&pfd, 1, 40) > 0) { - uint8_t tmp[256]; - ::recv(sfd, tmp, sizeof(tmp), 0); - } + recvQmiResp(sfd, 1, 0x0054, buf, sizeof(buf), 60); + uint8_t msReq[7] = { 0x00, 0x02, 0x00, 0x24, 0x00, 0x00, 0x00 }; ::sendto(sfd, msReq, sizeof(msReq), 0, reinterpret_cast(&dmsSq), sizeof(dmsSq)); - if (::poll(&pfd, 1, 50) > 0) { - uint8_t buf[512]; - ssize_t rlen = ::recv(sfd, buf, sizeof(buf), 0); - if (rlen >= 7 && buf[0] == 0x02 && buf[3] == 0x24) { - size_t pos = 7; - while (pos + 3 <= static_cast(rlen)) { - uint8_t tt = buf[pos]; - uint16_t tl = static_cast(buf[pos + 1]) | - (static_cast(buf[pos + 2]) << 8); - pos += 3; - if (pos + tl > static_cast(rlen)) break; - if (tt == 0x01 && tl > 0) { - std::string raw(reinterpret_cast(buf + pos), tl); - raw = trimStr(raw); - if (!raw.empty()) { - outNum = (raw.front() == '+') ? raw : ("+" + raw); + ssize_t rlen = recvQmiResp(sfd, 2, 0x0024, buf, sizeof(buf), 60); + if (rlen >= 7) { + size_t pos = 7; + while (pos + 3 <= static_cast(rlen)) { + uint8_t tt = buf[pos]; + uint16_t tl = static_cast(buf[pos + 1]) | + (static_cast(buf[pos + 2]) << 8); + pos += 3; + if (pos + tl > static_cast(rlen)) break; + if (tt == 0x01 && tl > 0) { + std::string raw(reinterpret_cast(buf + pos), tl); + std::string digits; + for (char c : raw) { + if ((c >= '0' && c <= '9') || (c == '+' && digits.empty())) { + digits.push_back(c); } } - pos += tl; + if (!digits.empty()) { + outNum = (digits.front() == '+') ? digits : ("+" + digits); + } } + pos += tl; } } ::close(sfd); @@ -1684,6 +1729,7 @@ inline DualSimInfo queryDualSimInfo(bool forceRefresh = false) { auto formatSlotStrings = [](int s, const std::string& op, const std::string& rat, const std::string& num, std::string& outLabel, std::string& outShort) { + (void)num; std::string base = "SIM " + std::to_string(s); outShort = op.empty() ? base : (base + " (" + op + ")"); outLabel = base; @@ -1693,9 +1739,6 @@ inline DualSimInfo queryDualSimInfo(bool forceRefresh = false) { if (!rat.empty()) { outLabel += " [" + rat + "]"; } - if (!num.empty()) { - outLabel += " " + num; - } }; formatSlotStrings(1, info.slot1Operator, info.slot1Rat, info.slot1Number, @@ -2369,10 +2412,17 @@ inline std::string sendModemUssd(const std::string& input, } if (outSessionActive) { - std::string stOut = runCaptureCmd( - "mmcli -m any --3gpp-ussd-status -K 2>/dev/null | grep -E 'modem\\.3gpp\\.ussd\\.(status|state)'"); - if (stOut.find("user-response") != std::string::npos) { + std::ifstream stIn("/tmp/sxmo_ussd_session_state"); + std::string qmiSt; + if (stIn.is_open() && std::getline(stIn, qmiSt) && + qmiSt.find("user-response") != std::string::npos) { *outSessionActive = true; + } else { + std::string stOut = runCaptureCmd( + "mmcli -m any --3gpp-ussd-status -K 2>/dev/null | grep -E 'modem\\.3gpp\\.ussd\\.(status|state)'"); + if (stOut.find("user-response") != std::string::npos) { + *outSessionActive = true; + } } }