Fix instant QMI call hangup, eliminate post-call dialer/callscreen popups and desktop clock flicker

This commit is contained in:
Luxferre committed 2026-11-05 09:51:53 +02:00
1 parent 1d71bc81d8
commit c9c870c3bb
27 files changed
+867 -308

No files matched your search

@@ -74,12 +74,12 @@ detect_qmicli_modem() {
return 0
fi
fi
if timeout 3 qmicli --silent -pd qrtr://0 --uim-noop >/dev/null 2>&1; then
QMICLI_MODEM="timeout 4 qmicli --silent -pd qrtr://0"
return 0
elif timeout 3 qmicli --silent -d qrtr://0 --uim-noop >/dev/null 2>&1; then
if timeout 3 qmicli --silent -d qrtr://0 --uim-noop >/dev/null 2>&1; then
QMICLI_MODEM="timeout 4 qmicli --silent -d qrtr://0"
return 0
elif timeout 3 qmicli --silent -pd qrtr://0 --uim-noop >/dev/null 2>&1; then
QMICLI_MODEM="timeout 4 qmicli --silent -pd qrtr://0"
return 0
elif [ -e "/dev/modem" ]; then
if timeout 3 qmicli --silent -pd /dev/modem --uim-noop >/dev/null 2>&1; then
QMICLI_MODEM="timeout 4 qmicli --silent -pd /dev/modem"
@@ -938,7 +938,7 @@ PY
fi
SIM_WAIT_TIME=2
provision_slot "$target" 4
pkill -f "msm-modem-uim-selection dsds-daemon" 2>/dev/null || true
pkill -f "dsds-daemon" 2>/dev/null || true
if [ "${3:-}" = "--swap-gw" ]; then
spawn_enable_and_register "$target" 2
else
@@ -1929,22 +1929,108 @@ PY
exit $?
;;
slots)
python3 - "$PREFERRED_SIM_SLOT" << 'PY'
import select, socket, struct, sys
pref_slot = int(sys.argv[1]) if len(sys.argv) > 1 and sys.argv[1].isdigit() else 1
prim_slot = pref_slot
sec_slot = 0
slot1_present = 0
slot2_present = 0
AF_QIPCRTR = 42
QRTR_PORT_CTRL = 0xfffffffe
QRTR_TYPE_NEW_SERVER = 4
QRTR_TYPE_NEW_LOOKUP = 10
QMI_SERVICE_UIM = 11
try:
ls = socket.socket(AF_QIPCRTR, socket.SOCK_DGRAM)
node, _ = ls.getsockname()
ls.sendto(struct.pack("<IIIII", QRTR_TYPE_NEW_LOOKUP, QMI_SERVICE_UIM, 0, 0, 0), (node, QRTR_PORT_CTRL))
u_addr = None
while True:
r, _, _ = select.select([ls], [], [], 0.08)
if not r:
break
data, _ = ls.recvfrom(256)
if len(data) >= 20:
cmd, srv, inst, srv_node, srv_port = struct.unpack("<IIIII", data[:20])
if cmd == QRTR_TYPE_NEW_SERVER:
if srv == QMI_SERVICE_UIM and srv_port != 0:
u_addr = (srv_node, srv_port)
if srv == 0 and srv_node == 0 and srv_port == 0:
break
ls.close()
if u_addr:
us = socket.socket(AF_QIPCRTR, socket.SOCK_DGRAM)
us.sendto(struct.pack("<BHHH", 0x00, 1, 0x002F, 0), u_addr)
r, _, _ = select.select([us], [], [], 0.1)
if r:
resp, _ = us.recvfrom(2048)
pos = 7
while pos + 3 <= len(resp):
tt, tl = struct.unpack("<BH", resp[pos:pos+3])
pos += 3
val = resp[pos:pos+tl]
pos += tl
if tt == 0x10 and len(val) >= 9:
gw_pri, _, gw_sec, _ = struct.unpack("<HHHH", val[:8])
if gw_pri != 0xFFFF:
prim_slot = ((gw_pri >> 8) & 0xFF) + 1
if gw_sec != 0xFFFF:
sec_slot = ((gw_sec >> 8) & 0xFF) + 1
num_slots = val[8]
off = 9
for s in range(min(num_slots, 2)):
if off + 6 > len(val):
break
cstate = val[off]
if s == 0 and cstate == 1:
slot1_present = 1
elif s == 1 and cstate == 1:
slot2_present = 1
num_apps = val[off + 5]
off += 6
for _ in range(num_apps):
if off + 7 > len(val):
break
aid_len = val[off + 6]
if off + 14 + aid_len > len(val):
break
off += 14 + aid_len
us.close()
except Exception:
pass
dual_active = 1 if (slot1_present and slot2_present and prim_slot in (1, 2) and sec_slot in (1, 2)) else 0
out = (
f"PREFERRED_SLOT={pref_slot}\n"
f"PRIMARY_GW_SLOT={prim_slot}\n"
f"SECONDARY_GW_SLOT={sec_slot}\n"
f"SLOT1_PRESENT={slot1_present}\n"
f"SLOT2_PRESENT={slot2_present}\n"
f"DUAL_SIM_ACTIVE={dual_active}\n"
)
sys.stdout.write(out)
try:
with open("/tmp/sxmo_sim_slots", "w") as f:
f.write(out)
except Exception:
pass
PY
exit 0
;;
call-dial|call-accept|call-hangup|call-dtmf|call-list)
sub_cmd="$cmd"
arg1="${2:-}"
arg2="${3:-}"
prim_slot="$PREFERRED_SIM_SLOT"
sec_slot="0"
if detect_qmicli_modem; then
QMI_CARDS="$($QMICLI_MODEM --uim-get-card-status 2>/dev/null || true)"
if [ -n "$QMI_CARDS" ]; then
eval "$(parse_uim_card_status "$QMI_CARDS" "$PREFERRED_SIM_SLOT")"
prim_slot="${PRIMARY_GW_SLOT:-$PREFERRED_SIM_SLOT}"
sec_slot="${SECONDARY_GW_SLOT:-0}"
fi
fi
python3 - "$sub_cmd" "$arg1" "$arg2" "$prim_slot" "$sec_slot" << 'PY'
import os, select, socket, struct, sys
import os, select, socket, struct, sys, time
sub_cmd = sys.argv[1]
arg1 = sys.argv[2]
@@ -1957,27 +2043,57 @@ QRTR_PORT_CTRL = 0xfffffffe
QRTR_TYPE_NEW_SERVER = 4
QRTR_TYPE_NEW_LOOKUP = 10
QMI_SERVICE_VOICE = 9
QMI_SERVICE_UIM = 11
ls = socket.socket(AF_QIPCRTR, socket.SOCK_DGRAM)
node, _ = ls.getsockname()
ls.sendto(struct.pack("<IIIII", QRTR_TYPE_NEW_LOOKUP, QMI_SERVICE_VOICE, 0, 0, 0), (node, QRTR_PORT_CTRL))
v_addr = None
while True:
r, _, _ = select.select([ls], [], [], 0.15)
if not r:
break
data, _ = ls.recvfrom(256)
if len(data) >= 20:
cmd, srv, inst, srv_node, srv_port = struct.unpack("<IIIII", data[:20])
if cmd == QRTR_TYPE_NEW_SERVER:
if srv == QMI_SERVICE_VOICE and srv_port != 0:
v_addr = (srv_node, srv_port)
if srv == 0 and srv_node == 0 and srv_port == 0:
break
ls.close()
def lookup_qmi_service(srv_id):
ls = socket.socket(AF_QIPCRTR, socket.SOCK_DGRAM)
node, _ = ls.getsockname()
ls.sendto(struct.pack("<IIIII", QRTR_TYPE_NEW_LOOKUP, srv_id, 0, 0, 0), (node, QRTR_PORT_CTRL))
addr = None
while True:
r, _, _ = select.select([ls], [], [], 0.08)
if not r:
break
data, _ = ls.recvfrom(256)
if len(data) >= 20:
cmd, srv, inst, srv_node, srv_port = struct.unpack("<IIIII", data[:20])
if cmd == QRTR_TYPE_NEW_SERVER:
if srv == srv_id and srv_port != 0:
addr = (srv_node, srv_port)
if srv == 0 and srv_node == 0 and srv_port == 0:
break
ls.close()
return addr
v_addr = lookup_qmi_service(QMI_SERVICE_VOICE)
if not v_addr:
sys.exit(1)
if sub_cmd in ("call-dial", "call-list"):
u_addr = lookup_qmi_service(QMI_SERVICE_UIM)
if u_addr:
try:
us = socket.socket(AF_QIPCRTR, socket.SOCK_DGRAM)
us.sendto(struct.pack("<BHHH", 0x00, 1, 0x002F, 0), u_addr)
r, _, _ = select.select([us], [], [], 0.08)
if r:
resp, _ = us.recvfrom(2048)
pos = 7
while pos + 3 <= len(resp):
tt, tl = struct.unpack("<BH", resp[pos:pos+3])
pos += 3
val = resp[pos:pos+tl]
pos += tl
if tt == 0x10 and len(val) >= 8:
gw_pri, _, gw_sec, _ = struct.unpack("<HHHH", val[:8])
if gw_pri != 0xFFFF:
prim_slot = ((gw_pri >> 8) & 0xFF) + 1
if gw_sec != 0xFFFF:
sec_slot = ((gw_sec >> 8) & 0xFF) + 1
us.close()
except Exception:
pass
def parse_tlvs(resp):
res = {}
pos = 7
@@ -2040,7 +2156,15 @@ STATE_MAP = {
10: "setup",
}
def is_hangup_requested():
try:
return (time.time() - os.path.getmtime("/tmp/sxmo_hangup_requested")) < 3.5
except Exception:
return False
if sub_cmd == "call-dial":
if is_hangup_requested():
sys.exit(1)
req_slot = int(arg1) if arg1.isdigit() else prim_slot
sub = 1 if (req_slot == sec_slot and sec_slot != 0) else 0
num = "".join(c for c in arg2 if c in "+0123456789*#").encode("ascii")
@@ -2057,12 +2181,21 @@ if sub_cmd == "call-dial":
s = socket.socket(AF_QIPCRTR, socket.SOCK_DGRAM)
qmi_req(s, 1, 0x0044, struct.pack("<BHB", 0x01, 1, sub))
tlvs = qmi_req(s, 2, 0x0020, struct.pack("<BH", 0x01, len(num)) + num, timeout=5.0)
s.close()
res_tlv = tlvs.get(0x02, b"")
cid = tlvs.get(0x10, b"\x01")[0]
if is_hangup_requested():
qmi_req(s, 3, 0x0021, struct.pack("<BHB", 0x01, 1, cid), timeout=0.5)
s.close()
try:
if os.path.exists(dial_marker):
os.remove(dial_marker)
except Exception:
pass
sys.exit(1)
s.close()
if len(res_tlv) >= 4 and struct.unpack("<HH", res_tlv[:4]) == (0, 0):
import subprocess
subprocess.run(["dbus-send", "--system", "--type=signal", "/", "org.ofono.VoiceCallManager.CallAdded"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
cid = tlvs.get(0x10, b"\x01")[0]
prefix = f"sub{sub}_"
try:
cid_marker = os.path.join(calls_dir, f"{prefix}{cid}.initiatedcall")
@@ -2088,17 +2221,25 @@ elif sub_cmd in ("call-accept", "call-hangup", "call-dtmf"):
txn = 1
for sub in (1, 0):
s = socket.socket(AF_QIPCRTR, socket.SOCK_DGRAM)
qmi_req(s, txn, 0x0044, struct.pack("<BHB", 0x01, 1, sub))
qmi_req(s, txn, 0x0044, struct.pack("<BHB", 0x01, 1, sub), timeout=0.4)
txn += 1
calls = parse_call_info(qmi_req(s, txn, 0x002F))
calls = parse_call_info(qmi_req(s, txn, 0x002F, timeout=0.5))
txn += 1
ended_on_sub = False
for cid, c in calls.items():
if c["state"] not in (8, 9):
tlvs = qmi_req(s, txn, 0x0021, struct.pack("<BHB", 0x01, 1, cid))
tlvs = qmi_req(s, txn, 0x0021, struct.pack("<BHB", 0x01, 1, cid), timeout=0.5)
txn += 1
ended_on_sub = True
res_tlv = tlvs.get(0x02, b"")
if len(res_tlv) >= 4 and struct.unpack("<HH", res_tlv[:4]) == (0, 0):
any_ended = True
if not ended_on_sub:
tlvs = qmi_req(s, txn, 0x0021, struct.pack("<BHB", 0x01, 1, 1), timeout=0.3)
txn += 1
res_tlv = tlvs.get(0x02, b"")
if len(res_tlv) >= 4 and struct.unpack("<HH", res_tlv[:4]) == (0, 0):
any_ended = True
s.close()
if any_ended:
subprocess.run(["dbus-send", "--system", "--type=signal", "/", "org.ofono.VoiceCallManager.CallRemoved"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
@@ -2124,9 +2265,10 @@ elif sub_cmd in ("call-accept", "call-hangup", "call-dtmf"):
target_cid = int(arg1)
s = socket.socket(AF_QIPCRTR, socket.SOCK_DGRAM)
qmi_req(s, 1, 0x0044, struct.pack("<BHB", 0x01, 1, target_sub))
calls = parse_call_info(qmi_req(s, 2, 0x002F))
qmi_req(s, 1, 0x0044, struct.pack("<BHB", 0x01, 1, target_sub), timeout=0.4)
calls = parse_call_info(qmi_req(s, 2, 0x002F, timeout=0.5))
if target_cid == 0 or (sub_cmd == "call-hangup" and target_cid not in calls):
fallback_cid = target_cid if target_cid > 0 else 1
target_cid = 0
for cid, c in calls.items():
if sub_cmd == "call-accept" and c["state"] in (2, 7):
@@ -2135,6 +2277,8 @@ elif sub_cmd in ("call-accept", "call-hangup", "call-dtmf"):
elif sub_cmd != "call-accept" and c["state"] not in (8, 9):
target_cid = cid
break
if target_cid == 0 and sub_cmd == "call-hangup":
target_cid = fallback_cid
if target_cid == 0:
s.close()
sys.exit(1)
@@ -2175,8 +2319,18 @@ PY
;;
dsds-daemon)
exec python3 - << 'PY'
import datetime, os, select, socket, struct, subprocess, sys, time
exec python3 - dsds-daemon << 'PY'
import datetime, fcntl, os, select, socket, struct, subprocess, sys, time
lock_fd = open("/tmp/dsds_daemon.lock", "w")
try:
fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except Exception:
sys.exit(0)
try:
os.chmod("/tmp/dsds_daemon.lock", 0o666)
except Exception:
pass
try:
while os.waitpid(-1, os.WNOHANG)[0] > 0:
@@ -2776,9 +2930,44 @@ def has_mm_call_files():
pass
return False
def sync_sub1_calls(calls, sim_slot):
def is_hangup_requested():
try:
return (time.time() - os.path.getmtime("/tmp/sxmo_hangup_requested")) < 3.5
except Exception:
return False
def query_sec_slot(uim_addr, fallback=2):
if not uim_addr:
return fallback
try:
us = socket.socket(AF_QIPCRTR, socket.SOCK_DGRAM)
ut = qmi_req(us, uim_addr, 1, 0x002F, timeout=0.08)
us.close()
st_val = ut.get(0x10, b"")
if len(st_val) >= 8:
_, _, sec_gw_idx, _ = struct.unpack("<HHHH", st_val[:8])
if sec_gw_idx != 0xFFFF:
return ((sec_gw_idx >> 8) & 0xFF) + 1
except Exception:
pass
return fallback
def sync_sub1_calls(calls, sim_slot, vs=None, voice_addr=None):
global active_calls
now_str = datetime.datetime.now().astimezone().isoformat(timespec="seconds")
hangup_req = is_hangup_requested()
sent_ended = False
if hangup_req:
for cid, c in list(calls.items()):
if c["state"] not in (8, 9):
if vs is not None and voice_addr is not None:
try:
qmi_req(vs, voice_addr, 55000 + (cid % 100), 0x0021, struct.pack("<BHB", 0x01, 1, cid), timeout=0.2)
except Exception:
pass
c["state"] = 9
for old_cid, old_info in list(active_calls.items()):
if old_cid not in calls:
if old_info.get("state") == "terminated":
@@ -2794,14 +2983,14 @@ def sync_sub1_calls(calls, sim_slot):
for cid, c in list(calls.items()):
st = STATE_MAP.get(c["state"], "unknown")
if st == "setup":
if c["dir"] == 1 and has_sub1_call_files():
if c["dir"] == 1 and has_sub1_call_files() and not hangup_req:
st = "dialing"
else:
continue
# Skip calls that are already managed by ModemManager / sxmo_modem.sh on Primary GW
if cid not in active_calls:
if st == "terminated":
if st == "terminated" or hangup_req:
continue
if c["dir"] == 1 and not has_sub1_call_files():
continue
@@ -2824,7 +3013,7 @@ def sync_sub1_calls(calls, sim_slot):
os.remove(os.path.join(CALLS_DIR, fn))
except Exception:
pass
if st != "terminated":
if st != "terminated" and not hangup_req:
try:
open(os.path.join(CALLS_DIR, f"{call_key}.{st}"), "w").close()
chown_user(os.path.join(CALLS_DIR, f"{call_key}.{st}"))
@@ -2863,7 +3052,9 @@ def sync_sub1_calls(calls, sim_slot):
stop_ringing_user()
evt = "call_missed" if prev == "ringing-in" else "call_hangup"
append_modemlog(f"{now_str}\t{evt}\t{num}\t[SIM {sim_slot}]\n")
send_fifo("/tmp/dwm_plugin_callscreen.fifo", "ENDED CALL ENDED")
if not hangup_req:
send_fifo("/tmp/dwm_plugin_callscreen.fifo", "ENDED CALL ENDED")
sent_ended = True
has_live = any(v["state"] in ("dialing", "ringing-out", "active", "held", "ringing-in", "waiting") for v in active_calls.values())
if not has_live:
@@ -2877,7 +3068,8 @@ def sync_sub1_calls(calls, sim_slot):
pass
if removed_stale:
stop_ringing_user()
send_fifo("/tmp/dwm_plugin_callscreen.fifo", "ENDED CALL ENDED")
if not sent_ended and not hangup_req:
send_fifo("/tmp/dwm_plugin_callscreen.fifo", "ENDED CALL ENDED")
if audio_opened:
set_q6voiced_audio(False)
@@ -2889,16 +3081,7 @@ while True:
time.sleep(3)
continue
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) >= 8:
_, _, sec_gw_idx, _ = struct.unpack("<HHHH", st_val[:8])
if sec_gw_idx != 0xFFFF:
sec_slot = ((sec_gw_idx >> 8) & 0xFF) + 1
sec_slot = query_sec_slot(uim_addr, 2)
ws = socket.socket(AF_QIPCRTR, socket.SOCK_DGRAM)
vs = socket.socket(AF_QIPCRTR, socket.SOCK_DGRAM)
@@ -2955,6 +3138,7 @@ while True:
continue
tlvs = parse_tlvs(pkt)
if sock is ws and mid == 0x0001:
sec_slot = query_sec_slot(uim_addr, sec_slot)
# WMSEventReport indication
if 0x11 in tlvs and len(tlvs[0x11]) >= 8:
# Transfer-route MT message: [ack_ind(1), txn_id(4), format(1), len(2), data...]
@@ -2980,6 +3164,7 @@ while True:
elif sock is vs and mid == 0x002E:
# VoiceAllCallStatusIndication
got_voice_ind = True
sec_slot = query_sec_slot(uim_addr, sec_slot)
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)
@@ -2988,17 +3173,18 @@ while True:
for cid, fc in full_calls.items():
if cid in calls and fc["num"]:
calls[cid]["num"] = fc["num"]
sync_sub1_calls(calls, sec_slot)
sync_sub1_calls(calls, sec_slot, vs, voice_addr)
if multipart_buf:
w_txn = poll_stored_sms(ws, wms_addr, w_txn, sec_slot)
w_txn = flush_stale_multipart(ws, wms_addr, w_txn, sec_slot)
if not got_voice_ind and (active_calls or has_sub1_call_files()):
sec_slot = query_sec_slot(uim_addr, sec_slot)
full_tlvs = qmi_req(vs, voice_addr, v_txn, 0x002F)
v_txn = (v_txn % 60000) + 1
if full_tlvs:
sync_sub1_calls(parse_call_info(full_tlvs), sec_slot)
sync_sub1_calls(parse_call_info(full_tlvs), sec_slot, vs, voice_addr)
if not os.path.exists("/tmp/bananarchy_screen_locked") and time.time() - last_poll >= 600.0:
w_txn = poll_stored_sms(ws, wms_addr, w_txn, sec_slot)