Fix dual-SIM operator/MSISDN parsing, secondary SIM USSD, and callscreen auto-close
This commit is contained in:
1 parent
46f1255905
commit
9a079e1068
16 files changed
+503
-173
No files matched your search
@@ -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("<IIIII", 10, 9, 0, 0, 0), (node, 0xfffffffe))
|
||||
v_addr = None
|
||||
while True:
|
||||
r, _, _ = select.select([ls], [], [], 0.12)
|
||||
if not r: break
|
||||
d, _ = ls.recvfrom(256)
|
||||
if len(d) >= 20:
|
||||
cmd, srv, inst, n, p = struct.unpack("<IIIII", d[:20])
|
||||
if cmd == 4 and srv == 9 and p != 0: v_addr = (n, p)
|
||||
if cmd == 4 and srv == 0 and n == 0 and p == 0: break
|
||||
ls.close()
|
||||
if v_addr:
|
||||
for sub in (0, 1):
|
||||
s = socket.socket(AF_QIPCRTR, socket.SOCK_DGRAM)
|
||||
s.sendto(struct.pack("<BHHH", 0, 1, 0x0044, 4) + struct.pack("<BHB", 1, 1, sub), v_addr)
|
||||
select.select([s], [], [], 0.1)
|
||||
s.sendto(struct.pack("<BHHH", 0, 2, 0x003C, 0), v_addr)
|
||||
select.select([s], [], [], 0.1)
|
||||
s.close()
|
||||
PY
|
||||
fi
|
||||
mmcli -m any --3gpp-ussd-cancel >/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("<BHHH", 0x00, 1, 0x0044, 4) + struct.pack("<BHB", 0x01, 1, 1), v_addr)
|
||||
s.sendto(struct.pack("<BHHH", 0x00, 1, 0x0044, 4) + struct.pack("<BHB", 0x01, 1, sub), v_addr)
|
||||
select.select([s], [], [], 0.2)
|
||||
try:
|
||||
s.recvfrom(512)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Register for USSD indications (0x0003, TLV 0x16 = 1)
|
||||
ind_tlv = struct.pack("<BHB", 0x16, 1, 1)
|
||||
s.sendto(struct.pack("<BHHH", 0x00, 2, 0x0003, len(ind_tlv)) + ind_tlv, v_addr)
|
||||
select.select([s], [], [], 0.2)
|
||||
try:
|
||||
s.recvfrom(512)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if mode != "respond":
|
||||
s.sendto(struct.pack("<BHHH", 0x00, 3, 0x003C, 0), v_addr)
|
||||
select.select([s], [], [], 0.15)
|
||||
try:
|
||||
s.recvfrom(512)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
cb = code.encode("ascii", errors="ignore")
|
||||
uss_data = struct.pack("<BB", 0x01, len(cb)) + cb
|
||||
tlv = struct.pack("<BH", 0x01, len(uss_data)) + uss_data
|
||||
msg_id = 0x003B if mode == "respond" else 0x003A
|
||||
s.sendto(struct.pack("<BHHH", 0x00, 2, msg_id, len(tlv)) + tlv, v_addr)
|
||||
s.sendto(struct.pack("<BHHH", 0x00, 4, msg_id, len(tlv)) + tlv, v_addr)
|
||||
|
||||
deadline = time.time() + 12.0
|
||||
def decode_uss_bytes(val):
|
||||
if len(val) < 3:
|
||||
return ""
|
||||
dcs = val[0]
|
||||
ulen = val[1]
|
||||
raw = val[2:2+ulen]
|
||||
if dcs in (2, 0x48) or (len(raw) >= 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("<BHHH", pkt[:7])
|
||||
if mid in (0x003A, 0x003B, 0x003E):
|
||||
pos = 7
|
||||
while pos + 3 <= len(pkt):
|
||||
tt, tl = struct.unpack("<BH", pkt[pos:pos+3])
|
||||
pos += 3
|
||||
val = pkt[pos:pos+tl]
|
||||
pos += tl
|
||||
if tt in (0x10, 0x11, 0x13) and len(val) >= 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("<BHHH", pkt[:7])
|
||||
tlvs = {}
|
||||
pos = 7
|
||||
while pos + 3 <= len(pkt):
|
||||
tt, tl = struct.unpack("<BH", pkt[pos:pos+3])
|
||||
pos += 3
|
||||
tlvs[tt] = pkt[pos:pos+tl]
|
||||
pos += tl
|
||||
|
||||
if mid == 0x003E:
|
||||
user_action = tlvs.get(0x01, b"\x01")[0] if 0x01 in tlvs and len(tlvs[0x01]) >= 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("<HH", tlvs[0x02][:4])
|
||||
if res != 0:
|
||||
write_session_state(False)
|
||||
break
|
||||
txt = ""
|
||||
if 0x16 in tlvs:
|
||||
txt = decode_utf16le_tlv(tlvs[0x16])
|
||||
if not txt and 0x12 in tlvs:
|
||||
txt = decode_uss_bytes(tlvs[0x12])
|
||||
if not txt and 0x11 in tlvs:
|
||||
txt = decode_uss_bytes(tlvs[0x11])
|
||||
if txt:
|
||||
write_session_state(False)
|
||||
print(txt)
|
||||
s.close()
|
||||
sys.exit(0)
|
||||
elif mid == 0x003D:
|
||||
write_session_state(False)
|
||||
s.close()
|
||||
sys.exit(1)
|
||||
PY
|
||||
@@ -1761,7 +1852,7 @@ def qmi_req(s, txn, msg_id, tlvs=b"", timeout=3.0):
|
||||
|
||||
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
|
||||
@@ -1771,7 +1862,7 @@ def parse_call_info(tlvs):
|
||||
cid, cstate, ctype, cdir, cmode, _, _ = struct.unpack("<BBBBBbB", raw_ci[off:off+7])
|
||||
off += 7
|
||||
calls[cid] = {"id": cid, "state": cstate, "dir": cdir, "num": ""}
|
||||
raw_num = tlvs.get(0x11) or tlvs.get(0x10 if 0x01 in tlvs else 0xFF) or b""
|
||||
raw_num = tlvs.get(0x10, b"") if 0x01 in tlvs else tlvs.get(0x11, b"")
|
||||
if len(raw_num) >= 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("<BBBBBbB", raw_ci[off:off+7])
|
||||
off += 7
|
||||
calls[cid] = {"id": cid, "state": cstate, "dir": cdir, "num": ""}
|
||||
raw_num = tlvs.get(0x11) or (tlvs.get(0x10) if 0x01 in tlvs else b"") or b""
|
||||
raw_num = tlvs.get(0x10, b"") if 0x01 in tlvs else tlvs.get(0x11, b"")
|
||||
if len(raw_num) >= 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("<HHHHH", st_val[:10]) if len(st_val) >= 10 else (0, 0, 0, 0, 0xFFFF)
|
||||
if len(st_val) >= 8:
|
||||
_, _, sec_gw_idx, _ = struct.unpack("<HHHH", st_val[:8])
|
||||
if sec_gw_idx != 0xFFFF:
|
||||
sec_slot = ((sec_gw_idx >> 8) & 0xFF) + 1
|
||||
|
||||
@@ -2322,13 +2439,18 @@ while True:
|
||||
|
||||
qmi_req(vs, voice_addr, v_txn, 0x0044, struct.pack("<BHB", 0x01, 1, 1))
|
||||
v_txn += 1
|
||||
# Register for Voice Call Notification Events (TLV 0x13 = 1)
|
||||
qmi_req(vs, voice_addr, v_txn, 0x0003, struct.pack("<BHB", 0x13, 1, 1))
|
||||
v_txn += 1
|
||||
|
||||
w_txn = poll_stored_sms(ws, wms_addr, w_txn, sec_slot)
|
||||
last_poll = time.time()
|
||||
|
||||
try:
|
||||
while True:
|
||||
r, _, _ = select.select([ws, vs], [], [], 10.0)
|
||||
poll_timeout = 1.0 if (active_calls or has_sub1_call_files()) else 10.0
|
||||
r, _, _ = select.select([ws, vs], [], [], poll_timeout)
|
||||
got_voice_ind = False
|
||||
for sock in r:
|
||||
pkt, _ = sock.recvfrom(4096)
|
||||
if len(pkt) < 7:
|
||||
@@ -2349,6 +2471,7 @@ while True:
|
||||
w_txn = poll_stored_sms(ws, wms_addr, w_txn, sec_slot)
|
||||
elif sock is vs and mid == 0x002E:
|
||||
# VoiceAllCallStatusIndication
|
||||
got_voice_ind = True
|
||||
calls = parse_call_info(tlvs)
|
||||
if not any(c["num"] for c in calls.values()):
|
||||
full_tlvs = qmi_req(vs, voice_addr, v_txn, 0x002F)
|
||||
@@ -2359,6 +2482,11 @@ while True:
|
||||
calls[cid]["num"] = fc["num"]
|
||||
sync_sub1_calls(calls, sec_slot)
|
||||
|
||||
if not got_voice_ind and (active_calls or has_sub1_call_files()):
|
||||
full_tlvs = qmi_req(vs, voice_addr, v_txn, 0x002F)
|
||||
v_txn = (v_txn % 60000) + 1
|
||||
sync_sub1_calls(parse_call_info(full_tlvs), sec_slot)
|
||||
|
||||
if time.time() - last_poll >= 15.0:
|
||||
w_txn = poll_stored_sms(ws, wms_addr, w_txn, sec_slot)
|
||||
last_poll = time.time()
|
||||
|
||||
Reference in new issue
Block a user