From 8c2a337c0d8760841bfcdff1536d7de94685cd6d Mon Sep 17 00:00:00 2001 From: Luxferre Date: Thu, 29 Oct 2026 22:32:09 +0200 Subject: [PATCH] Fix multipart incoming SMS QMI WMS routing and CSMS reassembly --- CHANGELOG.md | 4 +- overlay/rootfs/usr/bin/sxmo_modem.sh | 5 +- .../usr/libexec/msm-modem-uim-selection | 141 ++++++++++++------ 3 files changed, 102 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91ea86d..ce9248c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,8 +31,8 @@ Release versions follow the `
` format (where `2026` is `a`, - Rewrote `sxmo_soundmonitor.sh` to monitor kernel `udev` `sound` jack events and `/dev/input/event*` `SW_HEADPHONE_INSERT` events (plus initial boot state), automatically switching audio output to `Headphones` (`RX1`/`RX2` HPHL/HPHR DAC) when 3.5mm headphones are plugged in and restoring `Speaker` on unplug. - Added `Earpiece` as a first-class routable audio output device alongside `Speaker` and `Headphones` in `sxmo_audio.sh`, `dmenu` Audio menu, and `xdrawer`. - Made volume controls strictly linear (`1%`–`100%` mapped directly without cubic `wpctl` distortion) across `sxmo_audio.sh`, `xdrawer`, and `dmenu`. -- **Incoming SMS regression fix**: - - Restored execute permissions (`0755`) on `/usr/share/sxmo/default_hooks/sxmo_hook_smslog.sh`, added active SIM/NV message storage polling (`wms-list-messages`) in `msm-modem-uim-selection dsds-daemon` for modem firmware that suppresses `WMS Event Report` indications, and deduplicated incoming SMS delivery between `dsds-daemon` and `sxmo_modem.sh`. +- **Incoming and multipart SMS delivery and reassembly**: + - Restored execute permissions (`0755`) on `/usr/share/sxmo/default_hooks/sxmo_hook_smslog.sh`, configured `QMI_WMS_SET_ROUTES` (`0x0032`) on Secondary GW (`SUB 1`) in `msm-modem-uim-selection dsds-daemon` to `STORE_AND_NOTIFY` / `TRANSFER_AND_ACK` so the Qualcomm baseband immediately acknowledges (`RP-ACK`) every incoming segment in hardware, added multi-pass SIM/NV message storage harvesting (`wms-list-messages`), and implemented full 8-bit (`IEI 0x00`) and 16-bit (`IEI 0x08`) concatenated SMS (CSMS) reassembly across both GSM-7 septet and UCS-2 UTF-16BE (including split surrogate pairs) with segment deduplication. - **Battery and idle power optimizations**: - Added VM writeback (`dirty_writeback_centisecs=6000`), `printk` throttling, `sched_autogroup_enabled=0`, Wi-Fi powersave (`iw dev wlan0 set power_save on`), and USB/platform runtime PM (`power/control = auto`) in `/etc/local.d/10-power-save.start` and `/etc/sysctl.d/10-power.conf`. - Increased `conky` desktop clock refresh interval to `15.0s` and froze `conky` (`SIGSTOP`/`SIGCONT`) whenever the screen is off or locked, replaced polling in `sxmo_modemmonitor.sh` with event-driven `dbus-monitor`, skipped redundant `sxmo_hook_statusbar.sh` updates while `screenoff`, and drained queued X11 `UnmapNotify` events in `neoui_hide()` so all 22 resident DWM plugins sleep at `0%` CPU when hidden. diff --git a/overlay/rootfs/usr/bin/sxmo_modem.sh b/overlay/rootfs/usr/bin/sxmo_modem.sh index e0ced9f..4404bef 100755 --- a/overlay/rootfs/usr/bin/sxmo_modem.sh +++ b/overlay/rootfs/usr/bin/sxmo_modem.sh @@ -235,7 +235,7 @@ checkfornewtexts() { exec 3<> "$XDG_RUNTIME_DIR/sxmo_modem.checkfornewtexts.lock" flock -x 3 wait_parts=0 - while [ "$wait_parts" -lt 10 ]; do + while [ "$wait_parts" -lt 25 ]; do SMS_LIST="$(mmcli -m any --messaging-list-sms 2>/dev/null || true)" if printf "%s\n" "$SMS_LIST" | grep -qE '/SMS/[0-9]+ \(receiving\)'; then sleep 1 @@ -244,6 +244,9 @@ checkfornewtexts() { break fi done + if printf "%s\n" "$SMS_LIST" | grep -qE '/SMS/[0-9]+ \(receiving\)'; then + (sleep 10 && sxmo_modem.sh checkfornewtexts) >/dev/null 2>&1 & + fi TEXTIDS="$( printf "%s\n" "$SMS_LIST" | grep -Eo '/SMS/[0-9]+ \(received\)' | diff --git a/overlay/rootfs/usr/libexec/msm-modem-uim-selection b/overlay/rootfs/usr/libexec/msm-modem-uim-selection index f828041..c211ab7 100755 --- a/overlay/rootfs/usr/libexec/msm-modem-uim-selection +++ b/overlay/rootfs/usr/libexec/msm-modem-uim-selection @@ -2251,7 +2251,7 @@ def _parse_deliver_tpdu(pdu, pos): pos += 1 oa_digits = pdu[pos] oa_ton_npi = pdu[pos + 1] - if oa_digits > 20 or (oa_ton_npi & 0x80) == 0: + if oa_digits > 24 or (oa_ton_npi & 0x80) == 0: return None oa_byte_len = (oa_digits + 1) // 2 if pos + 2 + oa_byte_len + 10 > len(pdu): @@ -2303,6 +2303,10 @@ def _parse_deliver_tpdu(pdu, pos): elif iei == 0x08 and len(iedata) >= 4: csms_ref = (iedata[0] << 8) | iedata[1] csms_total, csms_seq = iedata[2], iedata[3] + if csms_ref is not None and (csms_total < 1 or csms_seq < 1 or csms_seq > csms_total): + csms_ref = None + csms_total = 1 + csms_seq = 1 # Determine alphabet from 3GPP TS 23.038 DCS if (dcs & 0xC0) in (0x00, 0x40): @@ -2323,7 +2327,7 @@ def _parse_deliver_tpdu(pdu, pos): payload = ud[udh_bytes : end_byte] if len(payload) % 2 == 1: payload = payload[:-1] - text = payload.decode("utf-16-be", errors="replace") + text = payload.decode("utf-16-be", errors="surrogatepass") elif alphabet == 1: end_byte = min(udl, len(ud)) payload = ud[udh_bytes : end_byte] @@ -2386,6 +2390,13 @@ def decode_sms_deliver(pdu, only_tpdu=False): return _parse_deliver_tpdu(pdu, 0) multipart_buf = {} +recent_sms_dedup = {} + +def _normalize_sms_text(s): + try: + return s.encode("utf-16", "surrogatepass").decode("utf-16", "replace") + except Exception: + return s BLOCK_FILE = "/home/user/.config/sxmo/block.tsv" BLOCK_DIR = "/home/user/.local/share/sxmo/block" @@ -2455,58 +2466,84 @@ def deliver_sms_to_sxmo(sender, iso_ts, text, sim_slot): def handle_decoded_sms(info, sim_slot): if not info: return + now = time.time() + for k, ts in list(recent_sms_dedup.items()): + if now - ts > 120.0: + del recent_sms_dedup[k] + seg_key = ("seg", info["sender"], info["time"], info["ref"], info["total"], info["seq"], info["text"]) + if seg_key in recent_sms_dedup and now - recent_sms_dedup[seg_key] < 15.0: + return + recent_sms_dedup[seg_key] = now if info["ref"] is not None and info["total"] > 1: + done_key = ("csms_done", info["sender"], info["ref"], info["total"]) + if done_key in recent_sms_dedup: + return key = (info["sender"], info["ref"], info["total"]) - parts = multipart_buf.setdefault(key, {"ts": time.time(), "time": info["time"], "slot": sim_slot, "segs": {}}) - parts["ts"] = time.time() - parts["time"] = info["time"] + parts = multipart_buf.setdefault(key, {"ts": now, "time": info["time"], "slot": sim_slot, "segs": {}}) + parts["ts"] = now parts["slot"] = sim_slot parts["segs"][info["seq"]] = info["text"] if len(parts["segs"]) >= info["total"]: - full_text = "".join(parts["segs"].get(i, "") for i in range(1, info["total"] + 1)) + full_text = _normalize_sms_text("".join(parts["segs"].get(i, "") for i in range(1, info["total"] + 1))) + msg_time = parts.get("time") or info["time"] del multipart_buf[key] - deliver_sms_to_sxmo(info["sender"], info["time"], full_text, sim_slot) + recent_sms_dedup[done_key] = now + deliver_sms_to_sxmo(info["sender"], msg_time, full_text, sim_slot) else: - deliver_sms_to_sxmo(info["sender"], info["time"], info["text"], sim_slot) + deliver_sms_to_sxmo(info["sender"], info["time"], _normalize_sms_text(info["text"]), sim_slot) -def flush_stale_multipart(): +def flush_stale_multipart(wms_sock=None, wms_addr=None, txn=1, sim_slot=2): now = time.time() + has_expired = any(now - parts["ts"] >= 45.0 for parts in multipart_buf.values()) + if has_expired and wms_sock is not None and wms_addr is not None: + txn = poll_stored_sms(wms_sock, wms_addr, txn, sim_slot) + now = time.time() for key, parts in list(multipart_buf.items()): - if now - parts["ts"] >= 30.0: - sender, _, total = key - full_text = "".join(parts["segs"].get(i, "") for i in range(1, total + 1)) + if now - parts["ts"] >= 45.0: + sender, ref, total = key + full_text = _normalize_sms_text("".join(parts["segs"].get(i, "") for i in range(1, total + 1))) del multipart_buf[key] + recent_sms_dedup[("csms_done", sender, ref, total)] = now if full_text: - deliver_sms_to_sxmo(sender, parts.get("time", ""), full_text, parts.get("slot", 2)) + deliver_sms_to_sxmo(sender, parts.get("time", ""), full_text, parts.get("slot", sim_slot)) + return txn def poll_stored_sms(wms_sock, wms_addr, txn, sim_slot): - for stype in (0, 1): - # WMSListMessages (0x0031): TLV 0x01 = storage_type, TLV 0x11 = message_mode (1 = GW) - tlvs = qmi_req(wms_sock, wms_addr, txn, 0x0031, struct.pack(" len(raw_list): - break - idx, tag = struct.unpack("= 4: - dlen = struct.unpack(" len(raw_list): + break + idx, tag = struct.unpack("= 4: + dlen = struct.unpack(" UIM + TRANSFER_AND_ACK (0x03), Class 1/2/3/None -> UIM + STORE_AND_NOTIFY (0x01) + # so the baseband sends 3GPP RP-ACK in hardware for every multipart segment immediately. + route_list = bytes.fromhex("060000000003000100010002000100030001000400010005ff02") + route_tlvs = struct.pack("