diff --git a/device/cheetah/MODEM-CLIENT.md b/device/cheetah/MODEM-CLIENT.md index 95a809fb2..64e15f6da 100644 --- a/device/cheetah/MODEM-CLIENT.md +++ b/device/cheetah/MODEM-CLIENT.md @@ -36,6 +36,8 @@ Two packages: | `sit.py` | ported | The single-owner IPC0 transport: framing, token matching, indication queue (signal and physical-channel snapshots coalesced so telemetry can't crowd out an SMS reply). Two guards: the allow-list of requests this stack sends, and our do-not-send list (NV, identity, carrier lock, AT forwarding, VSIM, satellite) checked first, so an edit to the allow-list can't let one through. Dial and SMS sends need the owner's flags | | `sit_names.py` | ours | The 602 message names, for logs | | `bringup.py` | ported, carrier values moved to the config | The start-up order (section 1.1) | +| `carrier_apn.py` | ours | The SIM's carrier settings from its home network code (GET_IMSI's MCC/MNC, the rest dropped): the package's `apn-overrides.conf` first, then mobile-broadband-provider-info's `serviceproviders.xml`; the internet and attach APNs, `ims_apn`, `ims_first`, `internet_cid` and the data profiles, each only where `[network]` leaves it `auto` or empty (0.3.29, TELEPHONY-APPS-PLAN 7.6) | +| `omamobile-sit-config` | ours | The pkexec helper behind the shell's Mobile network page (polkit action `org.omamobile.modem-sit.configure`): `set apn\|attach_apn\|roaming` and `get`, nothing else in `modem-sit.conf`; an APN must be a plain carrier APN, and the file is rewritten atomically with its comments kept | | `data_call.py` | ported, APN from the config | Setup (983 bytes; no PDU session ID on cellular, as stock), initial attach (251), data profile (246); bearer ownership recorded in RAM before each setup, never retried blindly | | `ims.py` | ported (identity strings ours) | IMS stack start, hidden-menu configuration, ADD_PDN_INFO from the IMS bearer, the registration indication | | `radio_settings.py` | ported | NR mode byte (3 -> 0 keeps LTE; the original kept in RAM) | @@ -103,8 +105,16 @@ registration indication), unless `ims_required = yes`. - **No NV writes from Linux, by construction.** The RIL speaks only SIT on `umts_ipc0` and fixed AT on `umts_router`; there is no raw-request path. NV lives in rfsd's RAM copies (MODEM-PLAN M3). -- **No identity reads.** GET_IMEI/IMSI/SIM I/O, PIN and APDU requests are - not on the allow-list; GET_IMEI/IMEISV are on the do-not-send list too. +- **No identity reads, but one.** GET_IMEI, SIM I/O, PIN and APDU requests + are not on the allow-list; GET_IMEI/IMEISV are on the do-not-send list too. + From 0.3.29 GET_IMSI is, sent once at start-up when any of `[network]` + `apn`, `attach_apn`, `ims_apn`, `ims_first` or `internet_cid` is `auto` + or empty. Those are the shipped defaults, so by default it is sent at + every boot, and typing `apn` and `attach_apn` alone still sends it (for + the bearer order); only typing all five reads no IMSI. Only its MCC/MNC + is used, to choose the APNs, the bearer order and the data profiles + (`carrier_apn.py`, TELEPHONY-APPS-PLAN 7.6); the IMSI is never logged, + stored or published, and the simulator tests fail if it reaches the log. - **Logs.** Requests by name and status code; SIM card/app state; data-call status/CID/type; IMS registration state/features. Never numbers, message text, addresses or the IMSI. The simulator tests fail if a number or a diff --git a/docs/TELEPHONY-APPS-PLAN.md b/docs/TELEPHONY-APPS-PLAN.md index 148163675..164c0305d 100644 --- a/docs/TELEPHONY-APPS-PLAN.md +++ b/docs/TELEPHONY-APPS-PLAN.md @@ -389,6 +389,28 @@ An "Emergency call" button on the lock plugin's keypad page (patch 0003 added th - **Quick settings.** "Mobile data" (in the roadmap today, waiting on M4) toggles NM's `gsm` connection (`nmcli`) from T3, or `omamobile-sit data on|off` before that. A long press opens a sheet with the operator, signal, APN and a roaming switch. "Airplane mode" also turns the radio off (`Modem.Enable(false)`). - **SIM PIN.** If the SIM asks for a PIN, the shell shows an unlock sheet (`Sim.SendPin`). Until T8 the RIL can't do this, so the SIM's PIN lock must stay off (as M4.2 already says). +### 7.6 SIM setup with no APN to type (2026-11-02) + +On 2026-11-02 a reseller SIM on Verizon's network needed its APN typed into `modem-sit.conf` before sitd would start ("No APN configured"), and then registered only once `vzwims` was the attach APN. A phone's SIM should just work. Built offline in `omamobile-modem-sit` 0.3.12, `omamobile-mmd` 0.1.1 and `omamobile-shell` 0.2.93, and merged with main on 2026-11-03 as `omamobile-modem-sit` 0.3.29, `omamobile-mmd` 0.1.2 and `omamobile-shell` 0.2.99 (main's 0.3.24-0.3.28, #384, #387 and #364, brought `attach_apn = none`, `ims_first`, `internet_cid`, `[profiles]`, `carrier_manifest` and the unconfirmed-hangup stop; main's shell 0.2.97 and #397's 0.2.98 come before this). Nothing below has met the phone yet as the automatic path; the values it produces for Verizon's codes are the ones set by hand on the 7 Pro on 2026-11-03 (phone checks at the end). + +**What 2026-11-03 changed** ([cheetah NOTES](../device/cheetah/NOTES.md) 2026-11-03, [IMS-SHANNON](../device/cheetah/IMS-SHANNON.md) sections 11 and 12): IMS registered and carried an SMS on the 7 Pro once sitd did what stock's RIL does on a Verizon network: no initial attach APN at all (`attach_apn = none`; the modem attaches with its carrier profile, and its attach bearer carries IMS), `ims_apn = IMS`, the IMS bearer first on CID 2 and the internet on CID 3 (`ims_first = yes`, `internet_cid = 3`), and stock's four data profiles (`[profiles]` 0 VZWINTERNET default,supl,dun ipv4v6; 2 IMS ia,ims ipv4v6, roaming ipv6; 3 VZWADMIN fota; 4 VZWAPP cbs,mms). The 11-02 value `attach_apn = vzwims` registered but never got IMS (our attach replaced the profile's attach and its P-CSCF request), so it is wrong now. The carrier lookup therefore fills all of these: an override section carries `attach_apn` (an APN, or `none`), `ims_apn`, `ims_first`, `internet_cid` and `profile_` lines with the `[profiles]` meaning, and `like = MCC/MNC` lets a carrier's codes share one section. `[network] ims_apn`, `ims_first` and `internet_cid` default to `auto` (the override's value, else `ims`, `no` and `1`), `[profiles]` empty takes the override's set and any typed line replaces it, and a typed value always wins. The carrier manifest stays the owner's (`carrier_manifest`): it names a file in the owner's own modem chroot. + +- **Where the APN comes from.** `[network] apn` and `attach_apn` are `auto` by default (empty means the same). With `auto`, sitd sends GET_IMSI (0x0400) once after the USIM is READY, with the USIM's AID from GET_SIM_STATUS (MODEM-SIT-RESEARCH 3.2), keeps only the MCC and the MNC candidates (three digits first in the three-digit countries of E.212, else two), and looks them up: first in the project's `apn-overrides.conf` (`/usr/share/omamobile/modem-sit/`), then in the system's `mobile-broadband-provider-info` (`serviceproviders.xml`, GNOME, **CC-PDDC** public-domain dedication, `arch=any`, in Arch Linux ARM's extra and already pulled in by NetworkManager; now a dependency of `omamobile-modem-sit`). Among providers sharing a code, `primary="true"` wins, then the provider listing more network codes (the operator over its resellers). Its first internet-capable APN (`internet`, `mms-internet-hipri*` or no usage) is the internet APN, never one named `ims`. A value typed in the config always wins, and a typed internet APN with `attach_apn = auto` still takes the carrier's attach APN. +- **The attach APN** (SET_INITIAL_ATTACH_APN) is the override's `attach_apn` (an APN, or `none`: not sent), else a database APN typed `ia`, else the internet APN (what a phone does when the carrier lists none). The database has no `ia` entries today, so the overrides carry the carriers that need another one. The only override so far is Verizon's codes (the database's own list): internet `VZWINTERNET` (the database lists `vzwims` first; Verizon's page gives VZWINTERNET for internet, [knowledge-base-240220](https://www.verizon.com/support/knowledge-base-240220/)), attach `none`, IMS APN `IMS`, IMS first on CID 2 with the internet on CID 3, and stock's four profiles (the 2026-11-03 result above; the 11-02 captures' `vzwims` is superseded). Other carriers worldwide get the database's internet APN as both, the older bearer order (internet on CID 1, then IMS) and profile 0 alone. **Merged:** main's `[network] attach_apn` (#356, #384) has the same meaning (empty: the internet APN; `none`: not sent); this PR adds `auto` and the per-carrier choice, and `start()` was merged by hand on 2026-11-03. +- **No APN found** (unknown code, GET_IMSI refused, no database): sitd no longer stops before the radio. It skips the attach APN and the default data profile, registers, skips the internet bearer and goes on to IMS, so calls and SMS work; status reports `apn.source = none` and `data_available = false`. A configured APN still fails start-up as before if the modem refuses the bearer. +- **What is logged.** Only the source ("APN chosen from the carrier database", "attach APN chosen from the project's carrier overrides"). Never the IMSI, the MCC/MNC or an APN: together with a log excerpt in a plan they would name the maintainer's carrier. The status socket and `state.json` carry the APNs in use (`modem.apn`: `value`, `source`, `attach`, `attach_source`) and the SIM's PIN1 state (`sim.pin_lock`, from the application record's +57), for the page; no IMSI. +- **Mobile network page** (`omamobile.mobilenetwork`): Setup > Mobile network (only where `/usr/lib/omamobile-sit/omamobile-sit-config` exists) and the Mobile data tile's chevron. Carrier, registration and technology, signal (ModemManager's `SignalQuality` when recent, else "Not reported yet"), Mobile data and Data roaming switches, the APN in use with its source and a field to type one (or go back to the SIM's), Advanced with the attach APN and its own field, and the SIM PIN lock. "No modem" without the stack. Backend `omamobile-mobile-network` (reads `state.json`, the config and MM; never a number or identifier). Changes go through `omamobile-sit-config` under `pkexec` (polkit `org.omamobile.modem-sit.configure`, `allow_active=yes`), which changes only `apn`, `attach_apn` and `[data] roaming`, checks each value and keeps the file's comments. **APN changes apply after a restart of the phone** (the page offers one): sitd's unit says a second start-up on the same CP isn't supported, so nothing restarts `omamobile-sitd`, and never `omamobile-modem`. Data roaming applies at once: `omamobile-sit-data` reads it on every pass and drops the cellular route while the network reports roaming (default `yes`, the behaviour so far). +- **Setup** (`omamobile-onboarding`): with the stack running and a SIM, it names the carrier and says mobile data is set up automatically; a SIM without an APN is sent to Setup > Mobile network; nothing is shown without the stack. +- **Not done:** SPN or GID1 to tell resellers on a shared code apart (needs SIM_IO, not on the allow-list); such a reseller gets the operator's APN, which works for the big US resellers on their host's network. + +**Phone checks** (attended, modem tests on the charger; each is one boot, read-only first; quote no APN, code or identifier from the journal into this plan): + +1. *Low risk.* Install the three packages; keep a copy of `/etc/omamobile/modem-sit.conf`. `pacman -Q mobile-broadband-provider-info` is installed. **Pass:** `omamobile-sit-config get` (as root) prints the current values; the `.pacnew` is reviewed. **Stop** if the package or polkit action is missing. +2. *Medium (a boot with the modem).* Set `apn = auto`, `attach_apn = auto`, `ims_apn = auto`, `ims_first = auto`, `internet_cid = auto` and an empty `[profiles]` (keep `carrier_manifest`), reboot. **Expect:** journal `Startup: APN chosen from ...`, `attach APN chosen from ... (none: not sent)`, `bearer order and data profiles from ...`, `SIT GET_IMSI: error=0`, four `data profile N accepted` lines, `initial attach APN not sent`, IMS on CID 2 before the internet on CID 3, registration within `register_timeout`, IMS registered, `omamobile-sit status` data bearer up: the same lines as the 2026-11-03 boot with the values typed. **Stop** if GET_IMSI is refused (the log says "could not be read") or registration times out: GET_IMSI hasn't met this firmware, and the AID layout comes from stock's builder only. **Rollback:** the typed values of IMS-SHANNON section 12, reboot. +3. *Medium.* The no-APN path: `apn = auto` with a temporary `OMAMOBILE_SIT_PROVIDERS`/`OMAMOBILE_SIT_APN_OVERRIDES` pointing at empty files (a `systemctl edit omamobile-sitd` drop-in), reboot. **Expect:** registration without an attach APN or data profile, IMS registered, `data_available: false`, an SMS to your own number arrives. **Stop** if registration fails: then a no-APN SIM needs another attach APN (the IMS APN is the next candidate). **Rollback:** remove the drop-in, reboot. +4. *Low.* The page: Setup > Mobile network and the tile's chevron open it; carrier, LTE, Mobile data and roaming switches follow `omamobile-sit status`; type an APN, Save asks nothing (active session), the config line changes, "Restart now" reboots; "Use the APN from the SIM" restores `auto`. Turn roaming off: nothing changes at home (the route stays). Announce the restart to whoever holds the phone. +5. *Low.* Setup's line: run `omamobile-onboarding` and check the "Mobile network: ... set up automatically" line. + --- ## 8. The other apps: reuse or build diff --git a/packages/omamobile-mmd/lib/bridge.py b/packages/omamobile-mmd/lib/bridge.py index 462c7e52b..7ea950002 100644 --- a/packages/omamobile-mmd/lib/bridge.py +++ b/packages/omamobile-mmd/lib/bridge.py @@ -219,7 +219,7 @@ def static_props(self, name): SupportedBands=[mm.BAND_UNKNOWN], CurrentBands=[mm.BAND_UNKNOWN], SupportedIpFamilies=mm.IP_V4 | mm.IP_V6 | mm.IP_V4V6) if name == mm.MODEM_3GPP: - apn, ip_type = self.settings.get('apn', ''), self.settings.get('ip_type', 'ipv4v6') + apn, ip_type = self.apn_settings(attach=True) settings = {'apn': GLib.Variant('s', apn), 'ip-type': GLib.Variant('u', IP_TYPES.get(ip_type, mm.IP_V4V6))} if apn else {} return dict(InitialEpsBearerSettings=settings) @@ -250,6 +250,27 @@ def dynamic_props(self, name, d): return dict(self.signal) return {} + def apn_settings(self, attach=False): + """The APN sitd's start-up used (sitd 0.3.29 reports it: typed, or + chosen from the SIM), else the config's value; "auto" is no APN. + attach: the initial attach APN (InitialEpsBearerSettings) instead of + the internet APN; the config's attach_apn, when empty, is the APN, + and "none" (no attach APN sent) leaves the settings empty.""" + used = ((self.status or {}).get('modem') or {}).get('apn') or {} + known = isinstance(used, dict) and bool(used.get('source')) + if known: + apn = used.get('attach', '') if attach and 'attach' in used else used.get('value', '') + else: + apn = self.settings.get('apn', '') + if attach: + typed = self.settings.get('attach_apn', '') + apn = typed if typed and typed.lower() != 'auto' else apn + # "auto" is chosen from the SIM (no APN until sitd reports it); + # "none" (sitd 0.3.24+) means no initial attach APN is sent at all. + if apn.lower() in ('auto', 'none'): + apn = '' + return apn, self.settings.get('ip_type', 'ipv4v6') + def create_sim(self, d): self.sim = self.path('SIM') self.x.add(self.sim, {mm.SIM: dict(Active=True, SimType=mm.SIM_TYPE_PHYSICAL, @@ -264,7 +285,7 @@ def sim_dynamic(self, d): def create_bearer(self, d): self.bearer = self.path('Bearer') - apn, ip_type = self.settings.get('apn', ''), self.settings.get('ip_type', 'ipv4v6') + apn, ip_type = self.apn_settings() self.x.add(self.bearer, {mm.BEARER: dict( Interface='rmnet0', Connected=d['data'], IpTimeout=20, BearerType=mm.BEARER_DEFAULT, ProfileId=-1, Properties={'apn': GLib.Variant('s', apn), diff --git a/packages/omamobile-mmd/lib/main.py b/packages/omamobile-mmd/lib/main.py index 499d05be0..170348afe 100644 --- a/packages/omamobile-mmd/lib/main.py +++ b/packages/omamobile-mmd/lib/main.py @@ -31,7 +31,9 @@ def sit_settings(path): """The APN and IP type from sitd's own config, for - Modem3gpp.InitialEpsBearerSettings and the bearer's properties.""" + Modem3gpp.InitialEpsBearerSettings and the bearer's properties, until + sitd's status names the APN its start-up used ("auto" is chosen from + the SIM, so it is no APN here).""" parser = configparser.ConfigParser(interpolation=None) try: parser.read(path) @@ -40,6 +42,7 @@ def sit_settings(path): if not parser.has_section('network'): return {} return dict(apn=parser['network'].get('apn', '').strip(), + attach_apn=parser['network'].get('attach_apn', '').strip(), ip_type=parser['network'].get('ip_type', 'ipv4v6').strip()) diff --git a/packages/omamobile-mmd/test/test_bridge.py b/packages/omamobile-mmd/test/test_bridge.py index e3bcedaf4..0f9b18cd9 100644 --- a/packages/omamobile-mmd/test/test_bridge.py +++ b/packages/omamobile-mmd/test/test_bridge.py @@ -22,6 +22,35 @@ def call(sid, state, direction='incoming', number=SENDER, reason=''): multiparty=False, since=0) +class ApnSettings(unittest.TestCase): + """The APN shown on the bearer: the one sitd's start-up used, else the + config's; "auto" (chosen from the SIM) is never shown as an APN.""" + def apn(self, settings, used=None, attach=False): + b = bridge.Bridge(None, None, settings) + b.status = dict(modem=dict(apn=used)) if used is not None else None + return b.apn_settings(attach=attach)[0] + + def test_attach(self): + used = dict(value='vzwinternet', source='override', attach='vzwims', attach_source='override') + self.assertEqual(self.apn(dict(apn='auto'), used, attach=True), 'vzwims') + self.assertEqual(self.apn(dict(apn='auto'), used), 'vzwinternet') + self.assertEqual(self.apn(dict(apn='internet.example', attach_apn='auto'), attach=True), 'internet.example') + self.assertEqual(self.apn(dict(apn='internet.example', attach_apn='ims'), attach=True), 'ims') + # attach_apn = none (sitd 0.3.24+): no initial attach APN is sent. + used = dict(value='VZWINTERNET', source='override', attach='none', attach_source='override') + self.assertEqual(self.apn(dict(apn='auto'), used, attach=True), '') + self.assertEqual(self.apn(dict(apn='auto'), used), 'VZWINTERNET') + self.assertEqual(self.apn(dict(apn='internet.example', attach_apn='none'), attach=True), '') + + def test_sources(self): + self.assertEqual(self.apn(dict(apn='internet.example')), 'internet.example') + self.assertEqual(self.apn(dict(apn='auto')), '') + self.assertEqual(self.apn(dict(apn='auto'), dict(value='vzwinternet', source='override')), 'vzwinternet') + self.assertEqual(self.apn(dict(apn='auto'), dict(value='', source='none')), '') + # Before start-up has chosen (source empty), the config's value. + self.assertEqual(self.apn(dict(apn='internet.example'), dict(value='', source='')), 'internet.example') + + class Derive(unittest.TestCase): def test_states(self): cases = [ diff --git a/packages/omamobile-mmd/version b/packages/omamobile-mmd/version index 17e51c385..d917d3e26 100644 --- a/packages/omamobile-mmd/version +++ b/packages/omamobile-mmd/version @@ -1 +1 @@ -0.1.1 +0.1.2 diff --git a/packages/omamobile-modem-sit/PKGBUILD b/packages/omamobile-modem-sit/PKGBUILD index 24355e6da..c73e40211 100644 --- a/packages/omamobile-modem-sit/PKGBUILD +++ b/packages/omamobile-modem-sit/PKGBUILD @@ -7,7 +7,11 @@ pkgdesc="Omarchy Mobile SIT RIL for the Pixel 7 Pro's S5300: LTE registration, I arch=(aarch64) url="https://github.com/derekross/omarchy-mobile" license=(MIT) -depends=(python iproute2 iputils systemd alsa-lib alsa-utils omamobile-modem) +# mobile-broadband-provider-info: the carrier database the automatic APN +# reads (GNOME, CC-PDDC; arch "any", NetworkManager already pulls it in). +# polkit: the Mobile network page's helper runs through pkexec. +depends=(python iproute2 iputils systemd alsa-lib alsa-utils omamobile-modem + mobile-broadband-provider-info polkit) backup=(etc/omamobile/modem-sit.conf) install=omamobile-modem-sit.install diff --git a/packages/omamobile-modem-sit/etc/omamobile/modem-sit.conf b/packages/omamobile-modem-sit/etc/omamobile/modem-sit.conf index ea7dcb84b..eaf47b35e 100644 --- a/packages/omamobile-modem-sit/etc/omamobile/modem-sit.conf +++ b/packages/omamobile-modem-sit/etc/omamobile/modem-sit.conf @@ -3,30 +3,43 @@ # the next start of omamobile-sitd, which needs a fresh CP: reboot. [network] -# The carrier's internet APN, as the carrier publishes it. Required: without -# it the start-up stops before the radio is switched on. -apn = +# The carrier's internet APN. "auto" (or empty): chosen from the SIM, by its +# home network code, in the project's carrier overrides and then the system's +# carrier database (mobile-broadband-provider-info). A carrier's APN typed +# here always wins. With no APN found, the phone still registers for calls +# and SMS, and reports mobile data as unavailable. Settings > Mobile network +# changes this line (omamobile-sit-config); it applies after a restart. +# The SIM is read with one GET_IMSI at start-up whenever any of apn, +# attach_apn, ims_apn, ims_first or internet_cid is "auto" or empty (the +# defaults, so by default at every boot; since 0.3.29). Only its home +# network code is used, never logged or stored. To read no IMSI at all, +# type all five. +apn = auto # The APN of the initial LTE attach, if the carrier attaches on another APN # than its internet APN (some attach only on their IMS APN, e.g. "ims"; the -# internet bearer still uses apn). Empty: the same as apn. +# internet bearer still uses apn). "auto" (or empty): the carrier's attach +# APN from the overrides or the database when it has one, else the internet +# APN. Typed here, it always wins. Applies after a restart. # none: no initial attach APN is sent at all, and the modem attaches with # its carrier profile's own settings (carrier_manifest below), as stock does -# on a Verizon network. Its attach bearer, listed with the network's P-CSCF -# addresses, then carries IMS: no IMS bearer of our own is set up, and the -# attach bearer is never released by the RIL. If none is listed within 20 s -# of registration, the RIL sets up its own IMS bearer on ims_apn as usual. -# Without an initial attach APN some modems refuse data setups (status 36): -# use none only with the carrier's manifest. +# on a Verizon network (the overrides say so for Verizon's codes). Its +# attach bearer, listed with the network's P-CSCF addresses, then carries +# IMS: no IMS bearer of our own is set up, and the attach bearer is never +# released by the RIL. If none is listed within 20 s of registration, the +# RIL sets up its own IMS bearer on ims_apn as usual. Without an initial +# attach APN some modems refuse data setups (status 36): use none only with +# the carrier's manifest. # The attach APN, the data profiles ([profiles] below) and the NR mode are # sent before the radio is switched on, or with it on if it already is: the # radio is never cycled. -attach_apn = +attach_apn = auto # The internet bearer's IP type: ipv4v6, ipv4 or ipv6. The cellular route # (omamobile-sit-data) is IPv4 only for now. ip_type = ipv4v6 -# IMS (VoLTE calls and SMS over IMS). The IMS APN is "ims" on most carriers. +# IMS (VoLTE calls and SMS over IMS). The IMS APN is "ims" on most carriers; +# "auto" takes the carrier override's IMS APN when it names one, else "ims". ims = yes -ims_apn = ims +ims_apn = auto # If IMS doesn't register within ims_timeout seconds (the IMS values were # proven on one carrier only), the RIL keeps serving the internet bearer # without SMS or calls. ims_required = yes ends start-up instead. @@ -42,13 +55,14 @@ ims_pcscf_required = yes # host's resolver. ims_pcscf_from_isim = yes # ims_first = yes sets the IMS bearer up (and announces it) before the -# internet bearer, as stock does; no keeps today's order. internet_cid is the -# CID of sitd's internet bearer (IMS is always 2). Stock keeps CID 1 for the -# modem's attach context and never sets a bearer up there; on a network that -# attaches on the IMS APN, use 3 (and ims_first = yes) so the attach PDN -# keeps its P-CSCF. -ims_first = no -internet_cid = 1 +# internet bearer, as stock does; no keeps the older order. internet_cid is +# the CID of sitd's internet bearer (IMS is always 2). Stock keeps CID 1 for +# the modem's attach context and never sets a bearer up there; on a network +# that attaches on the IMS APN, use 3 (and ims_first = yes) so the attach +# PDN keeps its P-CSCF. "auto" (the default) takes the carrier override's +# values when it has them (Verizon's codes: yes and 3), else no and 1. +ims_first = auto +internet_cid = auto # The modem's carrier profile: the 40-character cfg.db manifest ID for this # SIM's carrier (a file name under the modem chroot's # /vendor/firmware/carrierconfig/manifests). Set, it answers the modem's @@ -67,8 +81,12 @@ register_timeout = 180 [profiles] # Extra data profiles the modem manages itself (SET_DATA_PROFILE), sent with # the attach APN before the radio is switched on, as stock sends its -# carrier's "modem-cognitive" APNs. Empty (the default): only profile 0, the -# internet APN above. One line per profile: +# carrier's "modem-cognitive" APNs. Empty (the default): the carrier +# override's profiles when the SIM was read (a [network] value above at +# "auto" or empty) and it has them (Verizon's codes: stock's four, as in +# the example below), else only profile 0, the internet APN above; with all +# five [network] values typed, the SIM isn't read and empty means profile 0 +# only. Any line here replaces the override's set. One line per profile: # # ID = APN; types=TYPE,TYPE; ip=IP; roaming_ip=IP # @@ -82,7 +100,9 @@ register_timeout = 180 # 300 s, no wait, enabled, any bearer. # # Example, a Verizon-network SIM (Verizon and the carriers on its network), -# as stock sets it up. In [network] above: +# as stock sets it up, and what "auto" chooses for Verizon's codes from the +# carrier overrides (apn-overrides.conf), all but the manifest, which is the +# owner's. Typed by hand, in [network] above: # apn = VZWINTERNET # attach_apn = none # ims_apn = IMS @@ -97,6 +117,9 @@ register_timeout = 180 [data] # Reserved: the route's metric is fixed at 700 (Wi-Fi's default is 600). metric = 700 +# Mobile data while the network reports roaming. "no" takes the cellular +# route away while roaming (calls and SMS stay); read at once, no restart. +roaming = yes [control] # Members of this group may use the calls/SMS socket (root always can). diff --git a/packages/omamobile-modem-sit/install b/packages/omamobile-modem-sit/install index 66b8c0625..c0db4b484 100755 --- a/packages/omamobile-modem-sit/install +++ b/packages/omamobile-modem-sit/install @@ -8,6 +8,11 @@ d=${1:?Usage: ./install DESTDIR} [[ $d == /* && $d != "/" ]] || { echo "Use an absolute staging directory" >&2; exit 1; } for f in bin/*; do install -Dm755 "$f" "$d/usr/bin/${f#bin/}"; done for f in lib/*.py; do install -Dm644 "$f" "$d/usr/lib/omamobile-sit/${f#lib/}"; done +# The Mobile network page's helper (pkexec, polkit action below) and the +# carrier overrides the automatic APN reads before the carrier database. +install -Dm755 lib/omamobile-sit-config "$d/usr/lib/omamobile-sit/omamobile-sit-config" +install -Dm644 polkit/org.omamobile.modem-sit.policy "$d/usr/share/polkit-1/actions/org.omamobile.modem-sit.policy" +install -Dm644 share/apn-overrides.conf "$d/usr/share/omamobile/modem-sit/apn-overrides.conf" for f in systemd/*.service; do install -Dm644 "$f" "$d/usr/lib/systemd/system/${f#systemd/}"; done install -Dm644 networkmanager/90-omamobile-rmnet.conf "$d/usr/lib/NetworkManager/conf.d/90-omamobile-rmnet.conf" install -Dm644 etc/omamobile/modem-sit.conf "$d/etc/omamobile/modem-sit.conf" diff --git a/packages/omamobile-modem-sit/lib/bringup.py b/packages/omamobile-modem-sit/lib/bringup.py index aab270263..50b86e868 100644 --- a/packages/omamobile-modem-sit/lib/bringup.py +++ b/packages/omamobile-modem-sit/lib/bringup.py @@ -4,13 +4,17 @@ 1. wait for the USIM application to be READY (type 2, state 5); a PIN, PUK or network lock ends start-up at once (no PIN entry here); - 2. read the radio state (AT+CFUN?); then, with the radio as found, the - initial attach APN ([network] attach_apn, else apn; none: not sent, the + 2. the carrier's settings: [network] apn, attach_apn, ims_apn, ims_first + and internet_cid when typed, else the SIM's own (GET_IMSI's home + network code looked up in the carrier overrides and database, + carrier_apn.py; the overrides also carry a carrier's data profiles). + Then read the radio state (AT+CFUN?) and, with the radio as found, send + the initial attach APN (attach_apn, else apn; none: not sent, the modem attaches with its carrier profile, as stock's RIL did on a - Verizon network), every data - profile (profile 0 from apn unless [profiles] has its own, then the - rest of [profiles]) and NR mode 0 ([network] nr = off: stay on LTE; NR - registered but carried no traffic on this phone); + Verizon network; no APN known: not sent either), every data profile + (profile 0 from apn unless [profiles], or the override, has its own, + then the rest; none without an APN) and NR mode 0 ([network] nr = off: + stay on LTE; NR registered but carried no traffic on this phone); 3. radio on (AT+CFUN=1) once, unless it was already on: stock's order, with no radio cycle (device/cheetah/MODEM-PROVIDER-PROFILES.md section 2). An unreadable radio state falls back to 0.3.14's cycle @@ -19,7 +23,10 @@ With attach_apn = none, wait up to ATTACH_BEARER_WAIT s for the modem's own attach bearer (listed with its P-CSCF; every bearer listed then is recorded as modem-owned and never released); - 4. the internet bearer (CID 1; its rmnet device is found by omamobile-sit-data); + 4. the internet bearer (internet_cid, 1 unless the carrier attaches on + its IMS APN; its rmnet device is found by omamobile-sit-data), when an + APN is known: without one the phone registers for calls and SMS and + reports mobile data unavailable; 5. IMS: stack start, then ADD_PDN_INFO for the modem's attach bearer when one with a P-CSCF is listed (no IMS setup of our own, as stock); else the IMS bearer (CID 2; IPv6, or the IMS profile's @@ -38,6 +45,7 @@ import time from at import radio, radio_state +import carrier_apn from data_call import APN_TYPES, activate, attach_bearer, configured_profiles, entries, initial_attach from data_call import owned_ids, provision_profiles, record_modem_owned, summary import data_call @@ -122,6 +130,107 @@ def sim_lock(payload): return LOCKS.get(app[1]) +def usim_aid(payload): + """The READY USIM application's AID (kept in memory for GET_IMSI, never + logged), or None.""" + if len(payload) < 3 or payload[2] > 8 or len(payload) < 3 + payload[2] * 63: + return None + for i in range(3, 3 + payload[2] * 63, 63): + if payload[i:i + 2] == bytes([2, 5]) and 1 <= payload[i + 3] <= 16: + return payload[i + 4:i + 4 + payload[i + 3]] + return None + + +def read_imsi(client, aid): + """GET_IMSI (MODEM-SIT-RESEARCH 3.2: u8 AID length, 16-byte AID; the + answer is u8 length, then ASCII digits). The IMSI goes straight to the + carrier lookup and is never logged, stored or published.""" + request = bytes([len(aid)]) + aid.ljust(16, b'\0') + answer = client.request(0x0400, request, timeout=10) + if not answer or not 6 <= answer[0] <= 15 or len(answer) < 1 + answer[0]: + raise ValueError('Unexpected IMSI layout') + digits = answer[1:1 + answer[0]].decode('ascii', 'replace') + if not digits.isdigit(): + raise ValueError('Unexpected IMSI layout') + return digits + + +def typed(net, key): + """A value typed in [network], or '' for empty and "auto".""" + value = net.get(key, '').strip() + return '' if value.lower() == 'auto' else value + + +def yes_no(value, key): + value = value.strip().lower() + if value in ('yes', 'true', 'on', '1'): + return True + if value in ('no', 'false', 'off', '0'): + return False + raise ValueError(f'[network] {key} must be yes, no or auto') + + +def choose_apn(client, net, sim_payload): + """The carrier settings for this start-up and where they came from. + + apn: the internet APN ('' when none is known: no internet bearer); + attach_apn: the APN the modem registers with ('' means the internet + APN, "none" means that no attach APN is sent); ims_apn; ims_first and + internet_cid (the [network] keys' meaning); profiles: a carrier + override's data profile lines, for data_call.configured_profiles. + source and attach_source: 'manual' ([network] apn, attach_apn), + 'override' or 'carrier-database' (the SIM's home network code), 'none' + (no internet APN) or 'same' (attach with the internet APN). + + A typed value always wins. The SIM is read for whatever isn't typed: a + typed internet APN still takes the carrier's attach APN, bearer order + and profiles. Logs sources only: never the IMSI, the network code or an + APN (an APN can tell which carrier a phone uses).""" + manual = {key: typed(net, key) for key in ('apn', 'attach_apn', 'ims_apn', 'ims_first', 'internet_cid')} + found = None + if not all(manual.values()): + aid = usim_aid(sim_payload) + try: + if aid is None: + raise ValueError('no USIM application') + found = carrier_apn.lookup(read_imsi(client, aid)) + except (ModemError, TimeoutError, ValueError) as error: + print("Startup: the SIM's home network could not be read (" + type(error).__name__ + ')', flush=True) + found = found or {} + labels = {'override': "the project's carrier overrides", 'carrier-database': 'the carrier database'} + apn = manual['apn'] or found.get('apn', '') + source = 'manual' if manual['apn'] else (found['source'] if apn else 'none') + attach = manual['attach_apn'] or found.get('attach_apn', '') + attach_source = 'manual' if manual['attach_apn'] else (found['source'] if attach else 'same') + if manual['ims_first']: + ims_first = yes_no(manual['ims_first'], 'ims_first') + else: + ims_first = found.get('ims_first') is True + if manual['internet_cid']: + if not manual['internet_cid'].isdigit(): + raise ValueError('[network] internet_cid must be 1..15 and not 2 (IMS), or auto') + internet_cid = int(manual['internet_cid']) + else: + internet_cid = found.get('internet_cid') or 1 + if source in labels: + print(f'Startup: APN chosen from {labels[source]}', flush=True) + if attach_source in labels: + print(f'Startup: attach APN chosen from {labels[attach_source]}' + + (' (none: not sent)' if attach.lower() == 'none' else ''), flush=True) + if found.get('refused'): + print("Startup: the project's carrier override for this SIM has values that don't check, " + 'left out: ' + ', '.join(found['refused']), flush=True) + if found.get('source') == 'override' and (found.get('profiles') or found.get('ims_first') is not None + or found.get('internet_cid')): + print(f"Startup: bearer order and data profiles from {labels['override']}" + + (' (typed values kept)' if any(manual.values()) else ''), flush=True) + if not apn: + print('Startup: no APN for this SIM in the carrier database; registering for calls and SMS ' + 'without mobile data (set [network] apn in /etc/omamobile/modem-sit.conf)', flush=True) + return dict(apn=apn, attach_apn=attach, ims_apn=manual['ims_apn'] or found.get('ims_apn') or 'ims', + ims_first=ims_first, internet_cid=internet_cid, profiles=list(found.get('profiles') or []), + source=source, attach_source=attach_source) + def data_snapshot(client, label): """Registration and the data-call list, as the modem reports them at @@ -230,34 +339,22 @@ def wait_for(service, predicate, seconds, stopped): def start(service, settings, stopped=lambda: False): client = service.client net = settings['network'] - cid = net.getint('internet_cid', 1) - if not 1 <= cid <= 15 or cid == data_call.IMS_CID: - raise ValueError('[network] internet_cid must be 1..15 and not 2 (IMS)') - data_call.INTERNET_CID = cid - apn, ip_type = net.get('apn', ''), net.get('ip_type', 'ipv4v6') - if not apn: - raise RuntimeError('No APN configured: set [network] apn in /etc/omamobile/modem-sit.conf') - # Some networks attach only on their IMS APN (2026-11-02, cheetah: with - # the internet APN as the attach APN the modem searched for 13 minutes - # with no reject; with the IMS APN it registered on LTE in 34 s). The - # data profile and the internet bearer keep apn. Empty: the same as apn. - attach_apn = net.get('attach_apn', '').strip() or apn - # none: no SET_INITIAL_ATTACH_APN at all, as stock's RIL on a Verizon - # network (2026-11-02): the modem attaches with its carrier profile's - # settings, and its attach bearer (with the P-CSCF) carries IMS. - attach_none = attach_apn.lower() == 'none' + ip_type = net.get('ip_type', 'ipv4v6') + # A bad [profiles] line stops here, before the SIM and the radio; the + # set is built again below with the APN chosen for this SIM. try: - profiles = configured_profiles(settings) + configured_profiles(settings, '') except ValueError as error: raise RuntimeError(f'Bad data profile in /etc/omamobile/modem-sit.conf: {error}') from None - began = time.monotonic() previous = None locked = None + last = b'' def usim_ready(): - nonlocal previous, locked + nonlocal previous, locked, last payload = client.request(0x0200) ready = sim_ready(payload) + last = payload # Card state and application type/state only: never AIDs or labels. state = (payload[0], tuple((payload[i], payload[i + 1]) for i in range(3, 3 + payload[2] * 63, 63))) @@ -283,6 +380,27 @@ def usim_ready(): card = {None: 'no answer', 0: 'no SIM card detected', 1: 'card present, USIM not ready', 2: 'card error'}.get(previous[0] if previous else None, 'unknown card state') raise TimeoutError(f'No ready USIM within {timeout} s ({card})') from None + chosen = choose_apn(client, net, last) + apn, ims_apn = chosen['apn'], chosen['ims_apn'] + # The APN the modem registers with: the carrier's attach APN when it has + # one, else the internet APN; "none" sends no SET_INITIAL_ATTACH_APN at + # all, as stock's RIL on a Verizon network (2026-11-02): the modem + # attaches with its carrier profile's settings, and its attach bearer + # (with the P-CSCF) carries IMS. Without any APN, the modem keeps its own. + attach_apn = chosen['attach_apn'] or apn + attach_none = attach_apn.lower() == 'none' + cid = chosen['internet_cid'] + if not 1 <= cid <= 15 or cid == data_call.IMS_CID: + raise ValueError('[network] internet_cid must be 1..15 and not 2 (IMS)') + data_call.INTERNET_CID = cid + try: + profiles = configured_profiles(settings, apn, chosen['profiles']) + except ValueError as error: + raise RuntimeError(f'Bad data profile (/etc/omamobile/modem-sit.conf [profiles], or the carrier ' + f'overrides): {error}') from None + service.apn = dict(value=apn, source=chosen['source'], attach=attach_apn, + attach_source=chosen['attach_source'] if chosen['attach_apn'] else 'same') + began = time.monotonic() service.stage('attach') level = radio_state() if level is None: @@ -298,17 +416,18 @@ def usim_ready(): if attach_none: print('initial attach APN not sent (attach_apn = none): the modem attaches ' 'with its carrier profile', flush=True) - else: + elif attach_apn: ims_attach = (attach_apn != apn and net.getboolean('ims', True) - and attach_apn == net.get('ims_apn', 'ims')) + and attach_apn.lower() == ims_apn.lower()) if attach_apn != apn: print('initial attach on attach_apn, not the internet APN' + (' (the IMS APN: P-CSCF requested)' if ims_attach else ''), flush=True) initial_attach(client, attach_apn, ip_type, ims=ims_attach) - if profiles[0].ident == 0 and profiles[0].apn != apn: - print('data profile 0 from [profiles], with another APN than [network] apn ' - '(the internet bearer uses apn)', flush=True) - provision_profiles(client, profiles) + if profiles and profiles[0].ident == 0 and profiles[0].apn != apn: + print('data profile 0 from the profile lines, with another APN than the internet APN ' + '(the internet bearer uses the internet APN)', flush=True) + if profiles: + provision_profiles(client, profiles) lte = net.get('nr', 'off') == 'off' nr_after_radio = False if lte: @@ -367,14 +486,15 @@ def registered(): # for the attach bearer with its P-CSCF. wait = ATTACH_BEARER_WAIT if net.getboolean('ims', True) else 0 attach = find_attach_bearer(service, registered_at + wait, stopped, adopt_unknown=True) - ims_first = net.getboolean('ims', True) and net.getboolean('ims_first', False) - if not ims_first: + ims_first = net.getboolean('ims', True) and chosen['ims_first'] + if not ims_first and apn: service.stage('data') activate(client, apn, ip_type=ip_type, on_event=service.event) service.pump() if not net.getboolean('ims', True): service.ims_state = 'off' - print('Startup: registered with data; IMS off ([network] ims = no)', flush=True) + print('Startup: registered' + (' with data' if apn else ', no mobile data') + + '; IMS off ([network] ims = no)', flush=True) return service.ims_state = 'pending' service.stage('ims') @@ -385,7 +505,7 @@ def internet(): # other bearer), the internet comes up after the IMS setup, whether # that succeeded or not. nonlocal internet_up - if internet_up: + if internet_up or not apn: return service.stage('data') activate(client, apn, ip_type=ip_type, on_event=service.event) @@ -414,7 +534,6 @@ def internet(): # With the modem's attach bearer, no IMS setup of our own: it carries # IMS. Otherwise sitd's own IMS bearer on CID 2, as before. if attach is None: - ims_apn = net.get('ims_apn', 'ims') ims_ip = ims_ip_type(profiles, ims_apn) print('IMS bearer PDP type', ims_ip, '(from the IMS profile)' if ims_ip != 'ipv6' else '', flush=True) activate(client, ims_apn, ims=True, ip_type=ims_ip, @@ -445,7 +564,7 @@ def fallback(bearer): if net.getboolean('ims_required', False): raise print('Startup: IMS did not register (' + type(error).__name__ + ': ' + str(error) + - '); serving data only, no SMS or calls', flush=True) + ')' + ('; serving data only, no SMS or calls' if apn else '; no SMS, calls or mobile data'), flush=True) return service.ims_state = 'registered' print('Startup: LTE, IMS voice and SMS registered', flush=True) diff --git a/packages/omamobile-modem-sit/lib/carrier_apn.py b/packages/omamobile-modem-sit/lib/carrier_apn.py new file mode 100644 index 000000000..b6b532cb4 --- /dev/null +++ b/packages/omamobile-modem-sit/lib/carrier_apn.py @@ -0,0 +1,193 @@ +# SPDX-License-Identifier: MIT +"""The internet APN, and the initial-attach APN, for a SIM, chosen from its +home network code, so that a SIM works without anyone typing an APN. + +The attach APN is the one the modem registers with (SET_INITIAL_ATTACH_APN). +It comes from an override's attach_apn, else from a database APN typed "ia"; +empty means "the internet APN", the usual case, and "none" means that no +attach APN is sent at all: the modem attaches with its own carrier profile, +as stock's RIL does on a Verizon network (device/cheetah/IMS-SHANNON.md +sections 11 and 12, 2026-11-03). + +An override can carry the rest of what a carrier needs beyond its APNs, with +the same meaning as the keys in modem-sit.conf: ims_apn, ims_first, +internet_cid and the modem-managed data profiles (profile_ = the +[profiles] line). A value typed in the config always wins over an override. + +The SIM's home network (MCC and MNC) is the start of its IMSI. It is looked +up first in the project's overrides (apn-overrides.conf: carriers the +database gets wrong, each with the carrier's public APN page), then in the +system's carrier database, mobile-broadband-provider-info's +serviceproviders.xml (GNOME, CC-PDDC public-domain dedication; the same file +NetworkManager and ModemManager use). + +Only the MCC and MNC leave home_plmn(): the rest of the IMSI identifies the +subscriber and is dropped there. Nothing here logs; the caller logs where +the APN came from, never the IMSI, the network code or the APN itself. +""" +import configparser +import os +import re +import xml.etree.ElementTree as ElementTree + +DATABASE = os.environ.get('OMAMOBILE_SIT_PROVIDERS', + '/usr/share/mobile-broadband-provider-info/serviceproviders.xml') +OVERRIDES = os.environ.get('OMAMOBILE_SIT_APN_OVERRIDES', + '/usr/share/omamobile/modem-sit/apn-overrides.conf') + +# Countries whose MNCs are three digits (ITU-T E.212 lists; North America, +# most of the Caribbean and Latin America, and India's 405 block in part). +# Elsewhere the MNC is two digits. The SIM's EF_AD says which, but reading +# it needs SIM I/O, which the RIL doesn't send; both lengths are tried, the +# country's usual one first. +THREE_DIGIT_MNC = frozenset({ + '302', '310', '311', '312', '313', '314', '315', '316', '334', '338', '342', '344', + '346', '348', '354', '356', '358', '360', '365', '366', '376', '405', '708', '722', + '732', '750'}) +# Usages that carry the internet in the database's DTD (serviceproviders.2.dtd); +# an APN without a usage element is an internet APN too. +INTERNET_USAGES = ('internet', 'mms-internet-hipri', 'mms-internet-hipri-fota') +APN = re.compile(r'[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*') + + +def home_plmn(imsi): + """(MCC, [MNC candidates, likelier first]) from an IMSI's digits. The + subscriber part is never returned.""" + if not isinstance(imsi, str) or not imsi.isdigit() or not 6 <= len(imsi) <= 15: + raise ValueError('not an IMSI') + mcc = imsi[:3] + two, three = imsi[3:5], imsi[3:6] + return mcc, ([three, two] if mcc in THREE_DIGIT_MNC else [two, three]) + + +def plain_apn(value): + value = (value or '').strip() + return value if APN.fullmatch(value) and len(value) <= 99 else '' + + +YES_NO = {'yes': True, 'no': False, 'true': True, 'false': False, 'on': True, 'off': False} + + +IMS_CID = 2 + + +def override_values(section): + """One override section's values, checked: apn (required), attach_apn + (an APN, "none", or '' for the internet APN), ims_apn, ims_first + (yes/no or None), internet_cid (1..15 but not 2, the IMS bearer's, or + None) and profiles, the profile_ lines as (ID, line) pairs for + data_call.parse_profile. A value that doesn't check is left out, never + guessed, and its key is listed in 'refused' so the caller can say that + the override, not the config, had it.""" + apn = plain_apn(section.get('apn')) + if not apn: + return None + refused = [] + ims_first_text = (section.get('ims_first') or '').strip() + ims_first = YES_NO.get(ims_first_text.lower()) + if ims_first_text and ims_first is None: + refused.append('ims_first') + cid = (section.get('internet_cid') or '').strip() + internet_cid = int(cid) if cid.isdigit() and 1 <= int(cid) <= 15 and int(cid) != IMS_CID else None + if cid and internet_cid is None: + refused.append('internet_cid (1..15, not 2: the IMS bearer)') + profiles = [] + for key in section: + if key.startswith('profile_') and key[8:].isdigit(): + profiles.append((key[8:], section[key].strip())) + return dict(apn=apn, attach_apn=plain_apn(section.get('attach_apn')), + ims_apn=plain_apn(section.get('ims_apn')), ims_first=ims_first, + internet_cid=internet_cid, profiles=sorted(profiles, key=lambda p: int(p[0])), + carrier=section.get('name', '').strip(), source='override', refused=refused) + + +def from_overrides(mcc, mncs, path=None): + parser = configparser.ConfigParser(interpolation=None, default_section='-no-defaults-') + try: + if not parser.read(path or OVERRIDES): + return None + except configparser.Error: + return None + for mnc in mncs: + key = f'{mcc}/{mnc}' + if not parser.has_section(key): + continue + section = dict(parser[key]) + # "like = MCC/MNC": the same as another section (one hop; the + # section's own keys win), so a carrier's codes are listed once. + like = section.pop('like', '').strip() + if like and parser.has_section(like): + section = {**dict(parser[like]), **section} + section.pop('like', None) + found = override_values(section) + if found: + return found + return None + + +def provider_rank(provider, nets): + """Prefer the network's own operator over the resellers that share its + code: primary="true", then no attribute, then primary="false"; then the + provider listing more network codes (the operator lists all of its own, + a reseller usually one).""" + primary = {'true': 0, None: 1, 'false': 2}.get(provider.get('primary'), 1) + return (primary, -len(nets)) + + +def attach_apn(provider): + """The provider's initial-attach APN: one typed "ia" in the database + (serviceproviders.2.dtd), or '' (attach with the internet APN, as a + phone does when a carrier lists no attach APN).""" + for apn in provider.iter('apn'): + if any(u.get('type') == 'ia' for u in apn.iter('usage')): + value = plain_apn(apn.get('value')) + if value: + return value + return '' + + +def internet_apn(provider): + for apn in provider.iter('apn'): + usages = [u.get('type') for u in apn.iter('usage')] + if usages and not any(u in INTERNET_USAGES for u in usages): + continue + value = plain_apn(apn.get('value')) + # An IMS APN filed as internet (the database lists one for at least + # one carrier) never carries internet traffic. + if value and value.lower() != 'ims': + return value + return '' + + +def from_database(mcc, mncs, path=None): + try: + root = ElementTree.parse(path or DATABASE).getroot() + except (OSError, ElementTree.ParseError): + return None + for mnc in mncs: + found = [] + for index, provider in enumerate(root.iter('provider')): + nets = {(n.get('mcc'), n.get('mnc')) for n in provider.iter('network-id')} + if (mcc, mnc) not in nets: + continue + apn = internet_apn(provider) + if apn: + found.append((provider_rank(provider, nets), index, provider, apn)) + if found: + _, _, provider, apn = min(found, key=lambda f: f[:2]) + return dict(apn=apn, attach_apn=attach_apn(provider), ims_apn='', ims_first=None, + internet_cid=None, profiles=[], + carrier=(provider.findtext('name') or '').strip(), source='carrier-database') + return None + + +def lookup(imsi, database=None, overrides=None): + """The internet APN, the attach APN ('' for the internet APN, "none" for + no attach APN at all), and, when an override names them, the IMS APN, + ims_first, internet_cid and the data profiles, for the SIM with this + IMSI, or None. The result holds no part of the IMSI.""" + try: + mcc, mncs = home_plmn(imsi) + except ValueError: + return None + return from_overrides(mcc, mncs, overrides) or from_database(mcc, mncs, database) diff --git a/packages/omamobile-modem-sit/lib/data_call.py b/packages/omamobile-modem-sit/lib/data_call.py index 6c9f8b3f4..ebe75f0c4 100644 --- a/packages/omamobile-modem-sit/lib/data_call.py +++ b/packages/omamobile-modem-sit/lib/data_call.py @@ -354,22 +354,28 @@ def parse_profile(key, value): return Profile(ident, apn, types, **options) -def configured_profiles(settings): - """The profiles to send, in profile ID order: [profiles] as written, and - profile 0 from [network] apn unless [profiles] has its own 0. Raises - ValueError on any bad line (start-up then stops before the radio).""" +def configured_profiles(settings, apn=None, extra=()): + """The profiles to send, in profile ID order: [profiles] as written when + it has any line, else extra (a carrier override's (ID, line) pairs, from + carrier_apn), and profile 0 from the internet APN (apn, else [network] + apn) unless the set has its own 0. No APN and no 0: no profile 0, as a + SIM with no known APN gets no internet bearer either. Raises ValueError + on any bad line (start-up then stops before the radio).""" net = settings['network'] - found = {} + if apn is None: + apn = net.get('apn', '') + lines = [] if settings.has_section('profiles'): - for key, value in settings.items('profiles', raw=True): - if key in settings.defaults(): - continue - profile = parse_profile(key, value) - if profile.ident in found: - raise ValueError(f'profile {profile.ident} given twice') - found[profile.ident] = profile - if 0 not in found: - found[0] = default_profile(net.get('apn', ''), net.get('ip_type', 'ipv4v6')) + lines = [(key, value) for key, value in settings.items('profiles', raw=True) + if key not in settings.defaults()] + found = {} + for key, value in lines or list(extra): + profile = parse_profile(key, value) + if profile.ident in found: + raise ValueError(f'profile {profile.ident} given twice') + found[profile.ident] = profile + if 0 not in found and apn: + found[0] = default_profile(apn, net.get('ip_type', 'ipv4v6')) return [found[i] for i in sorted(found)] diff --git a/packages/omamobile-modem-sit/lib/data_network.py b/packages/omamobile-modem-sit/lib/data_network.py index 740c4e4ab..be7b4fd35 100644 --- a/packages/omamobile-modem-sit/lib/data_network.py +++ b/packages/omamobile-modem-sit/lib/data_network.py @@ -150,6 +150,22 @@ def resolver_for(servers, dev): return Resolver(RESOLVER) +def roaming_blocked(config=None, state=None): + """Data roaming is off ([data] roaming = no) and the network says the + phone is roaming: then rmnet0 gets no default route. Read on every pass, + so the Mobile network page's switch applies without a restart.""" + try: + if paths.config(config)['data'].getboolean('roaming', True): + return False + except (ValueError, OSError): + return False + try: + status = json.loads(Path(state or paths.RUN / 'state.json').read_text()) + except (OSError, ValueError): + return False + return (status.get('modem') or {}).get('registration') == 'roaming' + + def candidates(cid): """rmnet (stock's name for the bearer) first, then the others.""" first = cid - 1 @@ -263,6 +279,9 @@ def stop(*_): dev = find_device(address, data[2], stopped=lambda: stopping) addressed = True resolver = resolver_for(dns, dev) + if roaming_blocked(): + run('ip', 'route', 'del', 'default', 'dev', dev, 'metric', '700') + print('Roaming with data roaming off; the cellular default route is not set', flush=True) print('Cellular fallback ready; Wi-Fi remains preferred', flush=True) last = None seen = counters(json.loads(run('ip', '-j', '-s', 'link', 'show', 'dev', dev))) @@ -273,6 +292,13 @@ def watch(): routes = json.loads(run('ip', '-j', 'route', 'show', 'default')) routed = any(r.get('dev') == dev for r in routes) others = [r for r in routes if r.get('dev') != dev] + if roaming_blocked(): + if routed: + run('ip', 'route', 'del', 'default', 'dev', dev, 'metric', '700') + print('Roaming with data roaming off; the cellular default route is removed', flush=True) + resolver.update(False, dns) + last = None + return # With Wi-Fi up but rated limited, the carrier's DNS would keep # NetworkManager's own check failing and Wi-Fi penalised. cellular = routed and not others diff --git a/packages/omamobile-modem-sit/lib/omamobile-sit-config b/packages/omamobile-modem-sit/lib/omamobile-sit-config new file mode 100755 index 000000000..4cff4fe98 --- /dev/null +++ b/packages/omamobile-modem-sit/lib/omamobile-sit-config @@ -0,0 +1,128 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: MIT +"""omamobile-sit-config: the settings the shell's Mobile network page +changes in /etc/omamobile/modem-sit.conf, run as root through pkexec +(polkit action org.omamobile.modem-sit.configure: the active local user). + + omamobile-sit-config set apn auto|APN the internet APN ("auto": from the SIM) + omamobile-sit-config set attach_apn auto|APN the APN the modem registers with + omamobile-sit-config set roaming on|off mobile data while roaming + omamobile-sit-config get both, as JSON + +Nothing else in the file can be changed through it, and every value is +checked first: an APN is a plain carrier APN (letters, digits, '-' and +dots), never anything that could add a line or a section. Comments and the +other settings are kept as they are. + +The APN applies at the next start-up of omamobile-sitd, which needs a fresh +modem: a restart of the phone. Nothing is restarted here (a second start-up +on the same modem isn't supported, and the modem unit is never restarted in +place). Roaming applies at once: omamobile-sit-data reads it on every pass. +""" +import configparser +import json +import os +import re +import sys +import tempfile + +CONFIG = os.environ.get('OMAMOBILE_SIT_CONFIG', '/etc/omamobile/modem-sit.conf') +APN = re.compile(r'[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*') +KEYS = {'apn': ('network', 'apn'), 'attach_apn': ('network', 'attach_apn'), 'roaming': ('data', 'roaming')} + + +def value_for(key, value): + if key in ('apn', 'attach_apn'): + if value.lower() == 'auto': + return 'auto' + if not APN.fullmatch(value) or len(value) > 99: + raise ValueError('not a plain carrier APN') + return value + if key == 'roaming' and value in ('on', 'off'): + return 'yes' if value == 'on' else 'no' + raise ValueError('unsupported value') + + +def set_key(text, section, key, value): + """The file with section.key set to value, every other line untouched.""" + lines = text.splitlines(keepends=True) + if lines and not lines[-1].endswith('\n'): + lines[-1] += '\n' + header = re.compile(r'\s*\[([^\]]+)\]\s*$') + setting = re.compile(r'\s*' + re.escape(key) + r'\s*[=:]') + current, start = None, None + for index, line in enumerate(lines): + match = header.match(line) + if match: + current = match.group(1).strip() + if current == section and start is None: + start = index + continue + if current == section and setting.match(line): + lines[index] = f'{key} = {value}\n' + return ''.join(lines) + if start is None: + return ''.join(lines) + f'\n[{section}]\n{key} = {value}\n' + lines.insert(start + 1, f'{key} = {value}\n') + return ''.join(lines) + + +def current(path): + parser = configparser.ConfigParser(interpolation=None) + parser.read(path) + apn = parser.get('network', 'apn', fallback='').strip() or 'auto' + roaming = parser.get('data', 'roaming', fallback='yes').strip().lower() not in ('no', 'off', 'false', '0') + attach = parser.get('network', 'attach_apn', fallback='').strip() or 'auto' + return dict(apn=apn, attach_apn=attach, roaming=roaming) + + +def write(path, text): + directory = os.path.dirname(path) or '.' + fd, temporary = tempfile.mkstemp(prefix='.modem-sit.conf.', dir=directory) + try: + with os.fdopen(fd, 'w') as out: + out.write(text) + out.flush() + os.fsync(out.fileno()) + os.chmod(temporary, 0o644) + os.replace(temporary, path) + except BaseException: + if os.path.exists(temporary): + os.unlink(temporary) + raise + + +def main(argv): + if argv == ['get']: + print(json.dumps(current(CONFIG))) + return 0 + if len(argv) != 3 or argv[0] != 'set' or argv[1] not in KEYS: + print(__doc__, file=sys.stderr) + return 2 + key = argv[1] + try: + value = value_for(key, argv[2].strip()) + except ValueError as error: + print('omamobile-sit-config:', error, file=sys.stderr) + return 1 + section, name = KEYS[key] + try: + with open(CONFIG) as f: + text = f.read() + except FileNotFoundError: + text = '' + updated = set_key(text, section, name, value) + # The result must still parse, with the value where it was meant to go. + parser = configparser.ConfigParser(interpolation=None) + parser.read_string(updated) + if parser.get(section, name) != value: + print('omamobile-sit-config: the file could not be updated safely', file=sys.stderr) + return 1 + if updated != text: + write(CONFIG, updated) + print(json.dumps(current(CONFIG))) + return 0 + + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) diff --git a/packages/omamobile-modem-sit/lib/paths.py b/packages/omamobile-modem-sit/lib/paths.py index c18909b06..5b4c96eb0 100644 --- a/packages/omamobile-modem-sit/lib/paths.py +++ b/packages/omamobile-modem-sit/lib/paths.py @@ -27,14 +27,14 @@ BOOT = os.environ.get('OMAMOBILE_SIT_BOOT', '/dev/umts_boot0') DEFAULTS = { - 'network': {'apn': '', 'attach_apn': '', 'ip_type': 'ipv4v6', 'ims': 'yes', 'ims_apn': 'ims', + 'network': {'apn': 'auto', 'attach_apn': 'auto', 'ip_type': 'ipv4v6', 'ims': 'yes', 'ims_apn': 'auto', 'ims_required': 'no', 'ims_pcscf_required': 'yes', - 'ims_pcscf_from_isim': 'yes', 'ims_first': 'no', 'internet_cid': '1', + 'ims_pcscf_from_isim': 'yes', 'ims_first': 'auto', 'internet_cid': 'auto', 'carrier_manifest': '', 'carrier_manifest_timeout': '60', 'ims_timeout': '120', 'nr': 'off', 'sim_timeout': '120', 'register_timeout': '180'}, # Extra modem-managed data profiles, one per line (data_call.parse_profile). 'profiles': {}, - 'data': {'route': 'yes', 'metric': '700'}, + 'data': {'route': 'yes', 'metric': '700', 'roaming': 'yes'}, 'control': {'group': 'wheel'}, 'audio': {'card': 'none', 'mic_boost_db': '6', 'earpiece': 'yes'}, } diff --git a/packages/omamobile-modem-sit/lib/service.py b/packages/omamobile-modem-sit/lib/service.py index ab181f61c..8a92cc9c9 100644 --- a/packages/omamobile-modem-sit/lib/service.py +++ b/packages/omamobile-modem-sit/lib/service.py @@ -140,7 +140,11 @@ def sim_status(payload): return dict(state='detected') status = dict(state=APP_STATES.get(app[1], 'unknown')) if status['state'] in ('pin', 'puk', 'ready'): - status.update(pin_retries=app[59], puk_retries=app[60]) + # PIN1 state (+57, AOSP RIL_PinState): whether the SIM asks for its + # PIN at power-on. The RIL has no PIN entry, so "on" means the SIM + # works here only once the lock is turned off in another phone. + status.update(pin_retries=app[59], puk_retries=app[60], + pin_lock={1: 'on', 2: 'on', 3: 'off', 4: 'on', 5: 'on'}.get(app[57], 'unknown')) return status @@ -232,6 +236,11 @@ def __init__(self, client, db, settings=None): self.current_stage = 'starting' # IMS as start-up left it: unknown (no start-up), off, pending, failed, registered. self.ims_state = 'unknown' + # The internet APN start-up used and where it came from (manual, + # override, carrier-database or none), and the attach APN (attach; + # attach_source "same" when it is the internet APN). Empty before + # start-up. + self.apn = dict(value='', source='', attach='', attach_source='') self.next_poll = 0 self.published = None self.subscribers = [] @@ -560,6 +569,7 @@ def status(self, *, public=False): signal=self.signal if self.net['reg'] in ('home', 'roaming') else 0, audio=True, radio=self.radio, sim=dict(self.sim), ims='registered' if voice or sms else self.ims_state, + apn=dict(self.apn), data_available=self.apn.get('source') != 'none', emergency_numbers=list(EMERGENCY + (EMERGENCY_NO_SIM if self.sim['state'] == 'absent' else ()))) calls = [{k: v for k, v in c.items() if k != 'modem_id'} for c in self.calls.values()] if public: diff --git a/packages/omamobile-modem-sit/lib/sit.py b/packages/omamobile-modem-sit/lib/sit.py index 371764c10..bf05981b1 100644 --- a/packages/omamobile-modem-sit/lib/sit.py +++ b/packages/omamobile-modem-sit/lib/sit.py @@ -23,7 +23,9 @@ # Data/radio/IMS lifecycle, display state, read-only status, and incoming SMS # ACK. No SIM I/O (but the one ISIM file below), PIN, subscription, APDU or vendor NV requests. -ALLOWED = {0x0000, 0x0102, 0x0108, 0x0200, 0x0600, 0x0601, 0x0602, 0x0603, 0x0613, +# GET_IMSI (0x0400, read-only, from 0.3.29) is sent once at start-up for the SIM's home +# network code, to choose the carrier settings; its answer is never logged or kept. +ALLOWED = {0x0000, 0x0102, 0x0108, 0x0200, 0x0400, 0x0600, 0x0601, 0x0602, 0x0603, 0x0613, 0x061a, 0x061b, 0x0625, 0x0700, 0x0701, 0x0702, 0x0703, 0x0704, 0x070a, 0x070b, 0x0710, 0x0711, 0x0800, 0x0801, 0x0748, 0x0749, 0x0750, 0x0902, 0x091b, 0x0928, 0x0930, 0x0953, 0x0956, 0x0957, 0x0d07, 0x0d08, 0x0d1d, diff --git a/packages/omamobile-modem-sit/omamobile-modem-sit.install b/packages/omamobile-modem-sit/omamobile-modem-sit.install index 63a2520dd..1a0e69f48 100644 --- a/packages/omamobile-modem-sit/omamobile-modem-sit.install +++ b/packages/omamobile-modem-sit/omamobile-modem-sit.install @@ -1,8 +1,8 @@ # SPDX-License-Identifier: MIT post_install() { echo "omamobile-modem-sit: omamobile-sitd is installed but not enabled. It needs the" - echo " modem ONLINE first (omamobile-modem.service, MODEM-PLAN M3.4) and the" - echo " carrier's APN in /etc/omamobile/modem-sit.conf; then MODEM-PLAN M4." + echo " modem ONLINE first (omamobile-modem.service, MODEM-PLAN M3.4); then MODEM-PLAN M4." + echo " The APN is chosen from the SIM (apn = auto in /etc/omamobile/modem-sit.conf)." } post_upgrade() { systemctl daemon-reload 2>/dev/null || true @@ -14,6 +14,12 @@ post_upgrade() { echo " earpiece mode exists; with a card, audio is refused above the earpiece ceiling." echo " New settings: [network] ims_required, ims_timeout. Check modem-sit.conf.pacnew if listed." fi + if [[ $(vercmp "$2" 0.3.29) -lt 0 ]]; then + echo "omamobile-modem-sit 0.3.29: an empty or \"auto\" [network] apn, attach_apn, ims_apn," + echo " ims_first and internet_cid are chosen from the SIM (Verizon's codes get the 2026-11-03" + echo " setup: no attach APN, IMS first on CID 2, the internet on CID 3, stock's profiles);" + echo " values you typed keep working. New: [data] roaming (.pacnew)." + fi } pre_remove() { systemctl disable --now omamobile-sit-data.service omamobile-sitd.service 2>/dev/null || true diff --git a/packages/omamobile-modem-sit/polkit/org.omamobile.modem-sit.policy b/packages/omamobile-modem-sit/polkit/org.omamobile.modem-sit.policy new file mode 100644 index 000000000..ea9b3eed4 --- /dev/null +++ b/packages/omamobile-modem-sit/polkit/org.omamobile.modem-sit.policy @@ -0,0 +1,21 @@ + + + + + + Omarchy Mobile + https://github.com/derekross/omarchy-mobile + + Change the mobile network's APN and data roaming + Authentication is required to change the mobile network settings + + no + no + yes + + /usr/lib/omamobile-sit/omamobile-sit-config + + diff --git a/packages/omamobile-modem-sit/share/apn-overrides.conf b/packages/omamobile-modem-sit/share/apn-overrides.conf new file mode 100644 index 000000000..b0df2be24 --- /dev/null +++ b/packages/omamobile-modem-sit/share/apn-overrides.conf @@ -0,0 +1,81 @@ +# SPDX-License-Identifier: MIT +# Carriers the system's carrier database (mobile-broadband-provider-info) +# gets wrong for a phone, by the SIM's home network code (MCC/MNC). sitd +# reads this before the database when a [network] value is auto or empty; a +# value in /etc/omamobile/modem-sit.conf always wins over both. +# +# One section per network code, with the keys of modem-sit.conf [network] +# and the same meaning: apn (the internet APN, required), attach_apn (the +# APN the modem registers with when that isn't the internet APN; "none": +# no attach APN is sent and the modem attaches with its carrier profile), +# ims_apn, ims_first (yes/no), internet_cid (1..15, not 2), and the +# modem-managed data profiles as profile_ = the [profiles] line +# ("APN; types=...; ip=...; roaming_ip=..."). "like = MCC/MNC" takes another +# section's values (the section's own keys win), so a carrier's codes are +# listed once. Every entry names its source: the carrier's own public page, +# or our own capture on a phone; add codes only with one. Carriers not +# listed get the database's APNs, with the internet APN as the attach APN +# unless the database types one "ia". + +# Verizon, and the resellers on its network whose SIMs carry Verizon's codes. +# The database lists "vzwims" first among Verizon's internet APNs; Verizon's +# own page gives VZWINTERNET for internet access (VZWIMS is for messaging): +# https://www.verizon.com/support/knowledge-base-240220/ +# The rest is what registered IMS and carried calls and texts on a Pixel 7 +# Pro with a reseller SIM on this network on 2026-11-03 +# (device/cheetah/NOTES.md, IMS-SHANNON.md sections 11 and 12), as stock's +# RIL does it: no initial attach APN (the modem attaches with its carrier +# profile; its attach bearer, listed with the network's P-CSCFs, carries +# IMS), the IMS bearer first on CID 2 and the internet on CID 3 (CID 1 is +# the modem's attach context), and stock's four data profiles. An earlier +# entry here gave "vzwims" as the attach APN from the 2026-11-02 captures; +# that registered, but IMS never did (the attach replaced the carrier +# profile's attach and its P-CSCF request). +# The codes are the database's own list for Verizon. +[311/480] +name = Verizon +apn = VZWINTERNET +attach_apn = none +ims_apn = IMS +ims_first = yes +internet_cid = 3 +profile_0 = VZWINTERNET; types=default,supl,dun; ip=ipv4v6 +profile_2 = IMS; types=ia,ims; ip=ipv4v6; roaming_ip=ipv6 +profile_3 = VZWADMIN; types=fota +profile_4 = VZWAPP; types=cbs,mms + +[310/010] +like = 311/480 + +[310/011] +like = 311/480 + +[310/012] +like = 311/480 + +[311/481] +like = 311/480 + +[311/482] +like = 311/480 + +[311/483] +like = 311/480 + +[311/484] +like = 311/480 + +[311/485] +like = 311/480 + +[311/486] +like = 311/480 + +[311/487] +like = 311/480 + +[311/488] +like = 311/480 + +[311/489] +like = 311/480 diff --git a/packages/omamobile-modem-sit/test/all b/packages/omamobile-modem-sit/test/all index 3e1f74bc1..bba38d704 100755 --- a/packages/omamobile-modem-sit/test/all +++ b/packages/omamobile-modem-sit/test/all @@ -13,7 +13,7 @@ pass() { printf 'ok - %s\n' "$1"; } export PYTHONDONTWRITEBYTECODE=1 bash -n install || fail "install parses" -for f in bin/* lib/*.py test/*.py; do +for f in bin/* lib/*.py lib/omamobile-sit-config test/*.py; do python3 -c 'import ast, sys; ast.parse(open(sys.argv[1]).read(), sys.argv[1])' "$f" || fail "$f parses" done pass "scripts parse" @@ -30,6 +30,9 @@ for f in usr/bin/omamobile-sitd usr/bin/omamobile-sit usr/bin/omamobile-sit-data usr/lib/omamobile-sit/service.py usr/lib/omamobile-sit/sit.py usr/lib/omamobile-sit/sit_names.py \ usr/lib/systemd/system/omamobile-sitd.service usr/lib/systemd/system/omamobile-sit-data.service \ usr/lib/NetworkManager/conf.d/90-omamobile-rmnet.conf etc/omamobile/modem-sit.conf \ + usr/lib/omamobile-sit/omamobile-sit-config usr/lib/omamobile-sit/carrier_apn.py \ + usr/share/polkit-1/actions/org.omamobile.modem-sit.policy \ + usr/share/omamobile/modem-sit/apn-overrides.conf \ usr/share/licenses/omamobile-modem-sit/LICENSE; do [[ -e $stage/$f ]] || fail "staged $f" done diff --git a/packages/omamobile-modem-sit/test/test_bringup.py b/packages/omamobile-modem-sit/test/test_bringup.py index 794e79bce..5d3eec664 100644 --- a/packages/omamobile-modem-sit/test/test_bringup.py +++ b/packages/omamobile-modem-sit/test/test_bringup.py @@ -1,12 +1,27 @@ # SPDX-License-Identifier: MIT import env # noqa: F401 (paths and sys.path first) +import contextlib +import io +import tempfile +from pathlib import Path import unittest from unittest.mock import Mock, patch import bringup import paths +from sit import ModemError SETTINGS = paths.config('/nonexistent') SETTINGS['network']['apn'] = 'internet.example' +SETTINGS['network']['attach_apn'] = 'internet.example' +OVERRIDES = str(Path(__file__).resolve().parent.parent / 'share' / 'apn-overrides.conf') + + +def sim_with_aid(): + """A READY USIM with a made-up 8-byte AID.""" + record = bytearray(63) + record[0], record[1], record[3] = 2, 5, 8 + record[4:12] = bytes(range(0xa0, 0xa8)) + return bytes([1, 0, 1]) + bytes(record) def sim(card, *apps): @@ -61,6 +76,164 @@ def test_failed_data_setup_never_attempts_ims(self): register.assert_not_called() service.client.request.assert_not_called() + def run_start(self, settings, imsi_reply, overrides=OVERRIDES): + """start() past the SIM stage with every modem step mocked; GET_IMSI + answers imsi_reply (bytes, or an exception to raise).""" + service = Mock() + sim_payload = sim_with_aid() + + def request(ident, payload=b'', timeout=8): + if ident == 0x0400: + self.assertEqual(payload[0], 8) + self.assertEqual(len(payload), 17) + if isinstance(imsi_reply, Exception): + raise imsi_reply + return imsi_reply + return sim_payload + service.client.request.side_effect = request + out = io.StringIO() + + def fake_wait(service_, predicate, seconds, stopped): + if seconds == settings['network'].getint('sim_timeout', 120): + predicate() + with patch.object(bringup, 'wait_for', side_effect=fake_wait), patch.object(bringup, 'radio') as radio, \ + patch.object(bringup, 'radio_state', return_value=0), \ + patch.object(bringup, 'initial_attach') as attach, \ + patch.object(bringup, 'provision_profiles') as profiles, \ + patch.object(bringup, 'nr_mode'), patch.object(bringup, 'activate') as activate, \ + patch.object(bringup, 'data_snapshot'), patch.object(bringup, 'find_attach_bearer', return_value=None), \ + patch.object(bringup, 'start_stack'), patch.object(bringup, 'register'), \ + patch.object(bringup.carrier_apn, 'OVERRIDES', overrides), \ + patch.object(bringup.carrier_apn, 'DATABASE', '/nonexistent.xml'), \ + contextlib.redirect_stdout(out): + bringup.start(service, settings) + return service, out.getvalue(), radio, attach, profiles, activate + + def tearDown(self): + bringup.data_call.INTERNET_CID = 1 + + def test_auto_apn_comes_from_the_sims_home_network(self): + """A Verizon-network SIM: what registered IMS and carried calls and + texts on the 7 Pro on 2026-11-03 (NOTES, IMS-SHANNON 11-12): no + attach APN, stock's four profiles, IMS first on CID 2, the internet + on CID 3.""" + settings = paths.config('/nonexistent') + fake = '311480' + '0' * 9 + service, log, radio, attach, profiles, activate = self.run_start( + settings, bytes([15]) + fake.encode()) + attach.assert_not_called() + sent = profiles.call_args.args[1] + self.assertEqual([(p.ident, p.apn, p.type_names(), p.ip, p.roaming_ip) for p in sent], + [(0, 'VZWINTERNET', 'default,supl,dun', 'ipv4v6', 'ipv4v6'), + (2, 'IMS', 'ims,ia', 'ipv4v6', 'ipv6'), + (3, 'VZWADMIN', 'fota', 'ipv4v6', 'ipv4v6'), + (4, 'VZWAPP', 'mms,cbs', 'ipv4v6', 'ipv4v6')]) + # IMS first (CID 2, the IMS profile's types), then the internet on CID 3. + self.assertEqual([(c.args[1], c.kwargs.get('ims', False)) for c in activate.call_args_list], + [('IMS', True), ('VZWINTERNET', False)]) + self.assertEqual(activate.call_args_list[0].kwargs['apn_types'], 0x140) + self.assertEqual(bringup.data_call.INTERNET_CID, 3) + self.assertEqual(service.apn, dict(value='VZWINTERNET', source='override', + attach='none', attach_source='override')) + self.assertIn('APN chosen from', log) + self.assertIn('attach APN chosen from', log) + self.assertIn('not sent (attach_apn = none)', log) + self.assertIn('bearer order and data profiles from', log) + for secret in (fake, '311', '480', 'vzwinternet', 'vzwadmin', 'vzwapp'): + self.assertNotIn(secret, log.lower()) + + def test_typed_values_win_and_the_imsi_is_never_read(self): + settings = paths.config('/nonexistent') + settings['network'].update(apn='internet.example', attach_apn='internet.example', ims_apn='ims', + ims_first='no', internet_cid='1') + service, _, _, attach, profiles, activate = self.run_start(settings, AssertionError('GET_IMSI sent')) + attach.assert_called_once_with(service.client, 'internet.example', 'ipv4v6', ims=False) + self.assertEqual([p.apn for p in profiles.call_args.args[1]], ['internet.example']) + self.assertEqual([c.args[1] for c in activate.call_args_list], ['internet.example', 'ims']) + self.assertEqual((service.apn['source'], service.apn['attach_source']), ('manual', 'manual')) + + def test_a_typed_apn_still_takes_the_carriers_attach_and_bearer_order(self): + settings = paths.config('/nonexistent') + settings['network']['apn'] = 'vzwinternet' + service, log, _, attach, profiles, activate = self.run_start(settings, bytes([15]) + b'311480000000000') + attach.assert_not_called() + # The typed internet APN is the internet bearer's; the carrier's + # profiles (profile 0 included) and its bearer order still apply. + self.assertEqual([(p.ident, p.apn) for p in profiles.call_args.args[1]], + [(0, 'VZWINTERNET'), (2, 'IMS'), (3, 'VZWADMIN'), (4, 'VZWAPP')]) + self.assertEqual([c.args[1] for c in activate.call_args_list], ['IMS', 'vzwinternet']) + self.assertEqual(bringup.data_call.INTERNET_CID, 3) + self.assertEqual((service.apn['source'], service.apn['attach_source']), ('manual', 'override')) + self.assertIn('typed values kept', log) + + def test_typed_profiles_replace_the_carriers(self): + settings = paths.config('/nonexistent') + settings.read_string('[profiles]\n3 = fota.example; types=fota\n') + _, _, _, _, profiles, _ = self.run_start(settings, bytes([15]) + b'311480000000000') + self.assertEqual([(p.ident, p.apn) for p in profiles.call_args.args[1]], + [(0, 'VZWINTERNET'), (3, 'fota.example')]) + + def test_without_a_carrier_attach_apn_the_internet_apn_attaches(self): + settings = paths.config('/nonexistent') + settings['network']['apn'] = 'internet.example' + service, _, _, attach, _, activate = self.run_start(settings, bytes([15]) + b'001010000000000') + attach.assert_called_once_with(service.client, 'internet.example', 'ipv4v6', ims=False) + self.assertEqual(service.apn['attach_source'], 'same') + self.assertEqual([c.args[1] for c in activate.call_args_list], ['internet.example', 'ims']) + self.assertEqual(bringup.data_call.INTERNET_CID, 1) + + def test_no_apn_found_registers_for_calls_and_sms_without_data(self): + settings = paths.config('/nonexistent') + settings['network']['apn'] = '' + service, log, radio, attach, profiles, activate = self.run_start(settings, ModemError(2, b'')) + radio.assert_any_call(True) + attach.assert_not_called() + profiles.assert_not_called() + # Only the IMS bearer: no internet bearer without an APN. + self.assertEqual([c.kwargs.get('ims') for c in activate.call_args_list], [True]) + self.assertEqual(service.apn, dict(value='', source='none', attach='', attach_source='same')) + self.assertIn('without mobile data', log) + + def test_unknown_network_code_is_no_apn(self): + settings = paths.config('/nonexistent') + _, _, _, attach, _, _ = self.run_start(settings, bytes([15]) + b'001010000000000') + attach.assert_not_called() + + def test_bad_typed_bearer_keys_stop_startup(self): + for key, value in (('ims_first', 'maybe'), ('internet_cid', '2'), ('internet_cid', 'x'), ('internet_cid', '16')): + settings = paths.config('/nonexistent') + settings['network'].update(apn='internet.example', attach_apn='internet.example') + settings['network'][key] = value + with self.subTest(key=key, value=value), self.assertRaises(ValueError): + self.run_start(settings, bytes([15]) + b'001010000000000') + + def test_an_overrides_ims_cid_is_refused_by_the_override_not_the_config(self): + """internet_cid = 2 in an override is left out there (CID 1 stays), + and the log names the override: never "[network] internet_cid".""" + with tempfile.NamedTemporaryFile('w', suffix='.conf', delete=False) as f: + f.write('[001/01]\nname = Test\napn = internet.example\ninternet_cid = 2\n') + try: + settings = paths.config('/nonexistent') + _, log, _, _, _, _ = self.run_start(settings, bytes([15]) + b'001010000000000', overrides=f.name) + finally: + Path(f.name).unlink() + self.assertEqual(bringup.data_call.INTERNET_CID, 1) + self.assertIn("carrier override for this SIM has values that don't check, left out: " + 'internet_cid (1..15, not 2: the IMS bearer)', log) + self.assertNotIn('[network]', log) + + def test_imsi_layout_is_checked(self): + client = Mock() + for reply in (b'', bytes([20]) + b'1' * 20, bytes([15]) + b'31148000000000x', bytes([15]) + b'1'): + client.request.return_value = reply + with self.assertRaises(ValueError): + bringup.read_imsi(client, b'\xa0' * 8) + + def test_usim_aid_only_from_a_ready_usim(self): + self.assertEqual(bringup.usim_aid(sim_with_aid()), bytes(range(0xa0, 0xa8))) + self.assertIsNone(bringup.usim_aid(sim(1, (2, 2)))) + self.assertIsNone(bringup.usim_aid(b'')) + def attach_run(self, attach_apn): settings = paths.config('/nonexistent') settings['network']['apn'] = 'internet.example' @@ -84,7 +257,7 @@ def test_attach_apn_is_used_only_for_the_initial_attach(self): self.assertNotIn('ims', activate.call_args.kwargs) def test_attach_on_the_ims_apn_requests_the_pcscf(self): - attach, _, _ = self.attach_run('ims') # ims_apn defaults to "ims" + attach, _, _ = self.attach_run('ims') # ims_apn auto: "ims" without an override self.assertEqual(attach.call_args.kwargs, {'ims': True}) attach, _, _ = self.attach_run('other.example') self.assertEqual(attach.call_args.kwargs, {'ims': False}) @@ -92,21 +265,15 @@ def test_attach_on_the_ims_apn_requests_the_pcscf(self): self.assertEqual(attach.call_args.kwargs, {'ims': False}) def test_empty_attach_apn_attaches_on_the_internet_apn(self): - for value in ('', ' '): + for value in ('', ' ', 'auto'): attach, profile, _ = self.attach_run(value) self.assertEqual(attach.call_args.args[1], 'internet.example') self.assertEqual([p.apn for p in profile.call_args.args[1]], ['internet.example']) - def test_attach_apn_defaults_to_empty(self): - self.assertEqual(paths.config('/nonexistent')['network']['attach_apn'], '') - - def test_no_apn_stops_before_the_radio(self): - settings = paths.config('/nonexistent') - with patch.object(bringup, 'radio') as radio, patch.object(bringup, 'radio_state') as state, \ - self.assertRaises(RuntimeError): - bringup.start(Mock(), settings) - radio.assert_not_called() - state.assert_not_called() + def test_apns_default_to_auto(self): + net = paths.config('/nonexistent')['network'] + self.assertEqual((net['apn'], net['attach_apn'], net['ims_apn'], net['ims_first'], net['internet_cid']), + ('auto', 'auto', 'auto', 'auto', 'auto')) def test_wait_keeps_handling_incoming_sms(self): service = Mock() diff --git a/packages/omamobile-modem-sit/test/test_carrier_apn.py b/packages/omamobile-modem-sit/test/test_carrier_apn.py new file mode 100644 index 000000000..8a3832d82 --- /dev/null +++ b/packages/omamobile-modem-sit/test/test_carrier_apn.py @@ -0,0 +1,187 @@ +# SPDX-License-Identifier: MIT +"""The automatic APN: the SIM's home network code looked up in the project's +overrides, then in the carrier database. Every IMSI here is a made-up one: +a carrier's public network code followed by zeros.""" +import env # noqa: F401 (paths and sys.path first) +import os +from pathlib import Path +import tempfile +import unittest + +import carrier_apn + +HERE = Path(__file__).resolve().parent +OVERRIDES = HERE.parent / 'share' / 'apn-overrides.conf' +SYSTEM_DB = Path('/usr/share/mobile-broadband-provider-info/serviceproviders.xml') + +# Shaped like serviceproviders.xml (serviceproviders.2.dtd), with the cases +# that matter: an operator and resellers sharing a code, primary="true", +# an IMS APN filed as internet, MMS-only APNs and a two-digit-MNC country. +FAKE_DB = ''' + + + Example Reseller + + + Example Operator + + + + Example IMS First + + + + Example MVNO + + + Example Wholesale + + + + + + Example UK + + + + +''' + + +def imsi(mcc, mnc): + return mcc + mnc + '0' * (15 - len(mcc) - len(mnc)) + + +class CarrierApnTests(unittest.TestCase): + def setUp(self): + self.dir = tempfile.mkdtemp(prefix='carrier-apn-') + self.db = os.path.join(self.dir, 'serviceproviders.xml') + Path(self.db).write_text(FAKE_DB) + self.none = os.path.join(self.dir, 'absent.conf') + + def tearDown(self): + for name in os.listdir(self.dir): + os.unlink(os.path.join(self.dir, name)) + os.rmdir(self.dir) + + def look(self, mcc, mnc, overrides=None): + return carrier_apn.lookup(imsi(mcc, mnc), self.db, overrides or self.none) + + def test_home_plmn_keeps_only_the_network_code(self): + self.assertEqual(carrier_apn.home_plmn(imsi('311', '480')), ('311', ['480', '48'])) + self.assertEqual(carrier_apn.home_plmn(imsi('234', '15')), ('234', ['15', '150'])) + for bad in ('', '31148', '3114800000000000', '31148x000000000', None): + with self.assertRaises(ValueError): + carrier_apn.home_plmn(bad) + + def test_the_operator_wins_over_a_reseller_on_its_code(self): + found = self.look('310', '410') + self.assertEqual((found['apn'], found['attach_apn'], found['source']), + ('operator.example', 'attach.example', 'carrier-database')) + # No "ia" APN: attach with the internet APN (''). + self.assertEqual(self.look('310', '260')['attach_apn'], '') + + def test_primary_true_and_internet_usage(self): + self.assertEqual(self.look('310', '260')['apn'], 'wholesale.example') + + def test_an_ims_apn_is_never_the_internet_apn(self): + self.assertEqual(self.look('311', '480')['apn'], 'first.example') + + def test_two_digit_mnc_and_an_apn_without_usage(self): + self.assertEqual(self.look('234', '15')['apn'], 'uk.example') + + def test_unknown_code_and_missing_database(self): + self.assertIsNone(self.look('001', '01')) + self.assertIsNone(carrier_apn.lookup(imsi('310', '410'), os.path.join(self.dir, 'none.xml'), self.none)) + Path(self.db).write_text(' Default # Agent): Omarchy shows it after the first update, a phone at first login. +# With a SIM and the modem stack running, setup names the carrier and says +# mobile data is set up automatically (nothing to type). # # omamobile-onboarding open a terminal and run it # omamobile-onboarding --nudge at login: if anything is missing, say so @@ -101,6 +103,39 @@ offer_fingerprint() { fp_mark_offered } +# The SIM, when the modem stack runs: the carrier, and that mobile data is +# set up on its own (sitd picks the APN from the SIM). Nothing to type; the +# rare SIM without an APN is pointed at Setup > Mobile network. Silent with +# no modem stack (the release image has none yet) or before it has started. +mobile_network_line() { + command -v omamobile-mobile-network >/dev/null || return 0 + timeout 10 omamobile-mobile-network status 2>/dev/null | python3 -c ' +import json, sys +try: + s = json.load(sys.stdin) +except ValueError: + sys.exit(0) +if not s.get("installed") or not s.get("running"): + sys.exit(0) +sim = (s.get("sim") or {}).get("state") +apn = (s.get("apn") or {}).get("source", "") +Q = chr(39) +carrier = s.get("carrier") or "" +if sim == "absent": + print("No SIM card. Put one in and restart the phone for calls, texts and mobile data.") +elif sim in ("pin", "puk", "network-locked"): + print("Your SIM is locked, and this phone cannot unlock it yet: turn the SIM PIN off in another phone, then restart.") +elif sim == "ready": + head = "Mobile network: " + carrier + "." if carrier else "Your SIM is ready." + if apn == "none": + print(head + " Calls and texts work. Mobile data needs your carrier" + Q + "s APN: Setup > Mobile network.") + elif apn == "manual": + print(head + " Mobile data uses the APN set in Setup > Mobile network.") + else: + print(head + " Mobile data is set up automatically from your SIM; there" + Q + "s nothing to type.") +' 2>/dev/null || true +} + case "${1:-}" in --nudge) # one nudge at a time: the agent waits until the phone itself is set up @@ -138,6 +173,11 @@ case "${1:-}" in else omamobile-set-pin --inner fi + line=$(mobile_network_line) + if [[ -n $line ]]; then + echo + echo "$line" + fi offer_fingerprint invite_agent ;; diff --git a/packages/omamobile-shell/bin/omamobile-setup-user b/packages/omamobile-shell/bin/omamobile-setup-user index d17ee6898..6d8bc308b 100755 --- a/packages/omamobile-shell/bin/omamobile-setup-user +++ b/packages/omamobile-shell/bin/omamobile-setup-user @@ -433,6 +433,18 @@ fi if omarchy-done ensure omamobile-voice-plugin; then add_plugin omamobile.voice fi +# The Mobile network page (omamobile.mobilenetwork): carrier, mobile data, +# roaming, the APN and the SIM PIN lock; and Setup > Mobile network in +# Omarchy's menu, shown only where the modem stack is installed. Once each. +if omarchy-done ensure omamobile-mobilenetwork-plugin; then + add_plugin omamobile.mobilenetwork +fi +if omarchy-done ensure omamobile-menu-mobilenetwork; then + extension=~/.config/omarchy/extensions/omarchy-menu.jsonc + if [[ -f $extension ]] && ! grep -q '"setup.mobile-network"' "$extension"; then + sed -i '0,/^{/s//{\n "setup.mobile-network": {"icon":"\xf3\xb0\x9e\x83","label":"Mobile network","description":"Carrier, mobile data, roaming and the APN","when":"test -x \/usr\/lib\/omamobile-sit\/omamobile-sit-config","action":"omarchy-shell shell summon omamobile.mobilenetwork '"'"'{}'"'"'"},/' "$extension" + fi +fi # Setup > Voice in Omarchy's menu opens the voice settings (omamobile.voice), # through the menu extension file. Added once. if omarchy-done ensure omamobile-menu-voice; then diff --git a/packages/omamobile-shell/plugins/README.md b/packages/omamobile-shell/plugins/README.md index 6293caa19..ae01df09e 100644 --- a/packages/omamobile-shell/plugins/README.md +++ b/packages/omamobile-shell/plugins/README.md @@ -23,6 +23,7 @@ logical at scale 3). | `omamobile.voice` | `panel` (keepLoaded) | The voice bar, through Voxtype: hold the pill, or the keyboard's Mic key; it types, sends to an agent, or asks it | | `omamobile.access` | `panel` (keepLoaded) | The permission sheet: xdg-desktop-portal's "Allow Camera to use the camera?", in place of GTK's desktop dialog | | `omamobile.back` | `panel` (keepLoaded) | The back gesture: swipe in from the left or right edge for Escape | +| `omamobile.mobilenetwork` | `panel` (keepLoaded) | The Mobile network page: carrier, signal, mobile data, data roaming, the APN and (under Advanced) the attach APN, the SIM PIN lock; Setup > Mobile network or the Mobile data tile's chevron. "No modem" without the modem stack (docs/TELEPHONY-APPS-PLAN.md 7.6) | | lockpad | none | Not built; see [Lock screen](#lock-screen-pin-pad) for why and what upstream needs | ## Enabling diff --git a/packages/omamobile-shell/plugins/omamobile.mobilenetwork/MobileNetwork.qml b/packages/omamobile-shell/plugins/omamobile.mobilenetwork/MobileNetwork.qml new file mode 100644 index 000000000..273392bcb --- /dev/null +++ b/packages/omamobile-shell/plugins/omamobile.mobilenetwork/MobileNetwork.qml @@ -0,0 +1,657 @@ +// SPDX-License-Identifier: MIT +import QtQuick +import Quickshell +import Quickshell.Io +import Quickshell.Wayland +import qs.Commons +import qs.Commons as Commons +import qs.Ui +import "MobileNetworkModel.js" as Model +import "Readable.js" as Readable + +// The Mobile network page: the carrier, signal and registration, mobile +// data and data roaming switches, the APN in use with a field to type one, +// the attach APN (the one the modem registers with) under Advanced, and +// whether the SIM's PIN lock is on. Opened from Setup > Mobile network +// in Omarchy's menu, the Mobile data tile's chevron, or +// `omarchy-shell shell summon omamobile.mobilenetwork '{}'`. +// +// Everything goes through omamobile-mobile-network (bin/): `status` while +// the page is open (every 3 s), `data on|off` (omamobile-sit data), +// `roaming on|off`, `apn auto|APN` and `attach-apn auto|APN` +// (omamobile-sit-config under pkexec, +// polkit: the active user). An APN applies when the modem service next +// starts, which needs a restart of the phone: the page says so and offers +// one. Without the modem stack (the release image has none yet) the page +// says "No modem" and nothing else. +// +// The window sits on the top layer with normal exclusion, like the update +// sheet, so the APN field stays above the on-screen keyboard. +Item { + id: root + + // Injected by the host. + property var shell: null + property var manifest: null + + property bool opened: false + property var status: null + property string apnMessage: "" + property bool apnError: false + property bool apnSaved: false + property bool busy: false + property bool advancedOpen: false + // Switches flip at once and settle on the next status read. + property var optimistic: ({}) + + readonly property var page: Model.view(status) + readonly property int touchSize: Math.max(44, Style.space(48)) + readonly property int pad: Style.spacing.panelPadding + readonly property color foreground: Commons.Color.popups.text + readonly property string pluginId: (manifest && manifest.id) || "omamobile.mobilenetwork" + + // ---------------------------------------------------------------- host API + + function open(payloadJson) { + opened = true + apnMessage = "" + apnError = false + apnSaved = false + advancedOpen = false + optimistic = ({}) + refresh() + } + + function close() { + if (!opened) return + opened = false + apnField.focus = false + attachField.focus = false + Quickshell.execDetached(["qs", "ipc", "-p", Quickshell.shellDir, "call", "shell", "call", + "omamobile.keyboard", "hideKeyboard", ""]) + } + + function dismiss() { + if (root.shell && typeof root.shell.hide === "function") root.shell.hide(root.pluginId) + else root.close() + } + + // `shell call omamobile.mobilenetwork snapshot ''`: what the page shows + // (no numbers or SIM identifiers exist in it). + function snapshot() { + return JSON.stringify({ opened: opened, state: page.state, title: page.title, + data: page.dataText, apnSource: page.apnSource || "" }) + } + + // ---------------------------------------------------------------- actions + + function refresh() { + if (!statusProc.running) statusProc.running = true + } + + function value(name, actual) { + return optimistic[name] !== undefined ? optimistic[name] : actual + } + + function setOptimistic(name, on) { + var next = Object.assign({}, optimistic) + next[name] = on + optimistic = next + } + + function setData(on) { + if (actionProc.running || !page.dataAvailable) return + setOptimistic("data", on) + actionProc.command = Model.dataArgv(on) + actionProc.running = true + } + + function setRoaming(on) { + if (actionProc.running) return + setOptimistic("roaming", on) + actionProc.command = Model.roamingArgv(on) + actionProc.running = true + } + + // attach: the attach APN rather than the internet APN. + function saveApn(text, attach) { + var v = Model.apnValue(text) + if (v === null) { + apnError = true + apnMessage = "That isn't an APN: letters, digits, dots and dashes only." + return + } + if (actionProc.running) return + apnError = false + apnMessage = "Saving..." + apnField.focus = false + attachField.focus = false + actionProc.apn = v + actionProc.attach = attach === true + actionProc.command = attach === true ? Model.attachArgv(v) : Model.apnArgv(v) + actionProc.running = true + } + + // ---------------------------------------------------------------- processes + + Process { + id: statusProc + command: Model.STATUS_ARGV + stdout: StdioCollector { id: statusOut; waitForEnd: true } + onExited: { + var parsed = Model.parseJson(statusOut.text) + root.status = parsed || { installed: false } + root.optimistic = ({}) + } + } + + Process { + id: actionProc + property string apn: "" + property bool attach: false + onExited: function(exitCode) { + if (actionProc.apn !== "") { + root.apnError = exitCode !== 0 + root.apnSaved = exitCode === 0 + root.apnMessage = exitCode === 0 ? Model.apnSavedText(actionProc.apn, actionProc.attach) : "Not saved (permission refused or the value was rejected)." + if (exitCode === 0) { apnField.text = ""; attachField.text = "" } + actionProc.apn = "" + } + root.refresh() + } + } + + Timer { + interval: 3000 + running: root.opened + repeat: true + onTriggered: root.refresh() + } + + // ---------------------------------------------------------------- surface + + property real reveal: opened ? 1 : 0 + Behavior on reveal { NumberAnimation { duration: 200; easing.type: Easing.OutCubic } } + + PanelWindow { + id: sheetWindow + + visible: root.opened || root.reveal > 0 + anchors { top: true; bottom: true; left: true; right: true } + exclusionMode: ExclusionMode.Normal + color: "transparent" + WlrLayershell.namespace: "omamobile-mobilenetwork" + WlrLayershell.layer: WlrLayer.Top + // OnDemand, not Exclusive, or the on-screen keyboard gets no touches. + WlrLayershell.keyboardFocus: root.opened ? WlrKeyboardFocus.OnDemand : WlrKeyboardFocus.None + + Rectangle { + anchors.fill: parent + color: Commons.Color.menu.scrim + opacity: root.reveal + } + + MouseArea { + anchors.fill: parent + onClicked: root.dismiss() + } + + BorderSurface { + id: card + objectName: "mobileNetworkCard" + + x: Style.gapsOut + width: parent.width - Style.gapsOut * 2 + height: parent.height - Style.gapsOut * 2 + y: Style.gapsOut + Math.round((1 - root.reveal) * Style.space(60)) + opacity: root.reveal + color: Commons.Color.popups.background + borderSpec: Border.surfaceSpec("popups", "border", Commons.Color.popups.border, Math.max(1, Style.space(2))) + radius: Style.cornerRadius + padding: root.pad + + focus: root.opened + Keys.onEscapePressed: root.dismiss() + + MouseArea { anchors.fill: parent } + + Item { + anchors.fill: parent + anchors.topMargin: card.contentTopInset + anchors.rightMargin: card.contentRightInset + anchors.bottomMargin: card.contentBottomInset + anchors.leftMargin: card.contentLeftInset + + Item { + id: headerRow + anchors.top: parent.top + anchors.left: parent.left + anchors.right: parent.right + height: root.touchSize + + Text { + anchors.left: parent.left + anchors.right: closeButton.left + anchors.verticalCenter: parent.verticalCenter + textFormat: Text.PlainText + text: "Mobile network" + color: root.foreground + font.family: Style.font.family + font.pixelSize: Style.font.title + font.bold: true + elide: Text.ElideRight + } + + GlyphTap { + id: closeButton + anchors.right: parent.right + anchors.verticalCenter: parent.verticalCenter + glyph: "󰅖" + onTapped: root.dismiss() + } + } + + Flickable { + id: flick + anchors.top: headerRow.bottom + anchors.topMargin: Style.spacing.lg + anchors.left: parent.left + anchors.right: parent.right + anchors.bottom: parent.bottom + contentHeight: column.implicitHeight + clip: true + boundsBehavior: Flickable.StopAtBounds + + Column { + id: column + width: flick.width + spacing: Style.spacing.xl + + // ---------- The carrier and the registration + Row { + width: parent.width + spacing: Style.spacing.lg + + Text { + id: bigGlyph + textFormat: Text.PlainText + text: root.page.state === "ready" ? ["󰞃", "󰢼", "󰢽", "󰢾", "󰣺"][Math.max(0, root.page.signalBars)] : "󰞃" + color: root.page.state === "ready" ? Commons.Color.accent : root.foreground + opacity: root.page.state === "ready" ? 1 : 0.6 + font.family: Style.font.family + font.pixelSize: Style.font.iconLarge * 1.5 + } + + Column { + width: parent.width - bigGlyph.width - parent.spacing + anchors.verticalCenter: bigGlyph.verticalCenter + Text { + width: parent.width + objectName: "mobileNetworkTitle" + textFormat: Text.PlainText + text: root.page.title + color: root.foreground + font.family: Style.font.family + font.pixelSize: Style.font.title + font.bold: true + wrapMode: Text.Wrap + } + Text { + width: parent.width + visible: text !== "" + textFormat: Text.PlainText + text: root.page.body || "" + color: root.foreground + opacity: 0.8 + font.family: Style.font.family + font.pixelSize: Style.font.body + wrapMode: Text.Wrap + } + } + } + + InfoRow { + width: parent.width + visible: root.page.state === "ready" + glyph: "󰣺" + text: "Signal: " + (root.page.signal || "") + } + + // ---------- Switches + SettingRow { + objectName: "setting-mobileData" + visible: root.page.state === "ready" + enabled: root.page.dataAvailable === true + title: "Mobile data" + note: root.page.dataText || "" + checked: root.value("data", root.page.dataOn === true) + onToggled: root.setData(!checked) + } + + SettingRow { + objectName: "setting-roaming" + visible: root.page.state === "ready" + title: "Data roaming" + note: root.page.roamingText || "" + checked: root.value("roaming", root.page.roamingAllowed === true) + onToggled: root.setRoaming(!checked) + } + + // ---------- APN + SectionLabel { + visible: root.page.state === "ready" + text: "Access point (APN)" + } + + Text { + width: parent.width + visible: root.page.state === "ready" + textFormat: Text.PlainText + text: root.page.apnInUse + ? root.page.apnInUse + (root.page.apnSource ? " · " + root.page.apnSource : "") + : (root.page.apnSource || "Not chosen yet") + color: root.foreground + font.family: Style.font.family + font.pixelSize: Style.font.body + wrapMode: Text.Wrap + } + + Item { + width: parent.width + height: root.touchSize + visible: root.page.state === "ready" + + TextField { + id: apnField + anchors.left: parent.left + anchors.right: saveButton.left + anchors.rightMargin: Style.spacing.lg + anchors.top: parent.top + anchors.bottom: parent.bottom + verticalAlignment: TextInput.AlignVCenter + font.pixelSize: Style.font.body + foreground: root.foreground + placeholderText: root.page.apnConfigured ? "Typed: " + root.page.apnConfigured : "Automatic (from the SIM)" + inputMethodHints: Qt.ImhNoPredictiveText | Qt.ImhNoAutoUppercase | Qt.ImhPreferLowercase | Qt.ImhUrlCharactersOnly + onAccepted: root.saveApn(text) + onTextEdited: { root.apnMessage = ""; root.apnError = false } + } + + SheetAction { + id: saveButton + anchors.right: parent.right + anchors.verticalCenter: parent.verticalCenter + width: Style.space(96) + label: "Save" + primary: true + enabled: apnField.text.trim().length > 0 + onTapped: root.saveApn(apnField.text) + } + } + + SheetAction { + width: parent.width + visible: root.page.state === "ready" && root.page.apnConfigured !== "" + label: "Use the APN from the SIM" + onTapped: root.saveApn("auto") + } + + Text { + width: parent.width + visible: text !== "" + textFormat: Text.PlainText + text: root.apnMessage + color: root.apnError ? Commons.Color.urgent : root.foreground + font.family: Style.font.family + font.pixelSize: Readable.px(Style.font.caption) + wrapMode: Text.Wrap + } + + SheetAction { + width: parent.width + visible: root.apnSaved + label: "Restart now" + onTapped: Quickshell.execDetached(Model.REBOOT_ARGV) + } + + // ---------- Advanced: the attach APN + SettingRow { + objectName: "setting-advanced" + visible: root.page.state === "ready" + title: "Advanced" + note: "The APN the modem registers with" + checked: root.advancedOpen + onToggled: root.advancedOpen = !root.advancedOpen + } + + Text { + width: parent.width + visible: root.page.state === "ready" && root.advancedOpen + textFormat: Text.PlainText + text: "Attach APN: " + (root.page.attachText || "") + color: root.foreground + font.family: Style.font.family + font.pixelSize: Style.font.body + wrapMode: Text.Wrap + } + + Item { + width: parent.width + height: visible ? root.touchSize : 0 + visible: root.page.state === "ready" && root.advancedOpen + + TextField { + id: attachField + anchors.left: parent.left + anchors.right: attachSave.left + anchors.rightMargin: Style.spacing.lg + anchors.top: parent.top + anchors.bottom: parent.bottom + verticalAlignment: TextInput.AlignVCenter + font.pixelSize: Style.font.body + foreground: root.foreground + placeholderText: root.page.attachConfigured ? "Typed: " + root.page.attachConfigured : "Automatic (from the SIM)" + inputMethodHints: Qt.ImhNoPredictiveText | Qt.ImhNoAutoUppercase | Qt.ImhPreferLowercase | Qt.ImhUrlCharactersOnly + onAccepted: root.saveApn(text, true) + onTextEdited: { root.apnMessage = ""; root.apnError = false } + } + + SheetAction { + id: attachSave + anchors.right: parent.right + anchors.verticalCenter: parent.verticalCenter + width: Style.space(96) + label: "Save" + primary: true + enabled: attachField.text.trim().length > 0 + onTapped: root.saveApn(attachField.text, true) + } + } + + SheetAction { + width: parent.width + visible: root.page.state === "ready" && root.advancedOpen && root.page.attachConfigured !== "" + label: "Use the attach APN from the SIM" + onTapped: root.saveApn("auto", true) + } + + // ---------- SIM + InfoRow { + width: parent.width + visible: root.page.state === "ready" + glyph: "󰌾" + text: "SIM PIN lock: " + (root.page.pinText || "") + } + } + } + } + } + } + + // ---------------------------------------------------------------- pieces + + // Inline components have their own scope: no `root` in here. + component SectionLabel: Text { + width: parent ? parent.width : 0 + topPadding: Style.spacing.md + textFormat: Text.PlainText + color: Commons.Color.popups.text + opacity: 0.7 + font.family: Style.font.family + font.pixelSize: Readable.px(Style.font.caption) + font.bold: true + } + + component SettingRow: Item { + id: settingItem + property string title: "" + property string note: "" + property bool checked: false + signal toggled() + width: parent ? parent.width : 0 + height: visible ? Math.max(Math.max(44, Style.space(48)), settingText.implicitHeight + Style.spacing.md * 2) : 0 + opacity: enabled ? 1 : 0.55 + Accessible.role: Accessible.CheckBox + Accessible.name: settingItem.title + Accessible.checked: settingItem.checked + + Column { + id: settingText + anchors.left: parent.left + anchors.right: settingSwitch.left + anchors.rightMargin: Style.spacing.lg + anchors.verticalCenter: parent.verticalCenter + Text { + width: parent.width + textFormat: Text.PlainText + text: settingItem.title + color: Commons.Color.popups.text + font.family: Style.font.family + font.pixelSize: Style.font.body + wrapMode: Text.Wrap + } + Text { + width: parent.width + visible: settingItem.note !== "" + textFormat: Text.PlainText + text: settingItem.note + color: Commons.Color.popups.text + opacity: 0.7 + font.family: Style.font.family + font.pixelSize: Readable.px(Style.font.caption) + wrapMode: Text.Wrap + } + } + + // The whole row toggles, as on Android; the switch shows the state. + MouseArea { + anchors.fill: parent + enabled: settingItem.enabled + onClicked: settingItem.toggled() + } + + ToggleSwitch { + id: settingSwitch + anchors.right: parent.right + anchors.verticalCenter: parent.verticalCenter + checked: settingItem.checked + interactive: false + } + } + + component GlyphTap: Item { + id: glyphItem + property string glyph: "" + property color foreground: Commons.Color.popups.text + readonly property int touchSize: Math.max(44, Style.space(48)) + signal tapped() + width: glyphItem.touchSize + height: glyphItem.touchSize + + Rectangle { + anchors.fill: parent + radius: Style.cornerRadius + color: glyphTouch.pressed ? Style.pressedFillFor(glyphItem.foreground, Commons.Color.accent) : "transparent" + } + + Text { + anchors.centerIn: parent + textFormat: Text.PlainText + text: glyphItem.glyph + color: glyphItem.foreground + font.family: Style.font.family + font.pixelSize: Style.font.iconLarge + } + + MouseArea { + id: glyphTouch + anchors.fill: parent + onClicked: glyphItem.tapped() + } + } + + component SheetAction: BorderSurface { + id: actionItem + property string label: "" + property bool primary: false + property color foreground: Commons.Color.popups.text + readonly property int touchSize: Math.max(44, Style.space(48)) + signal tapped() + height: visible ? actionItem.touchSize : 0 + radius: Style.cornerRadius + opacity: enabled ? 1 : 0.45 + color: actionTouch.pressed ? Style.pressedFillFor(actionItem.foreground, Commons.Color.accent) + : actionItem.primary ? Util.alpha(Commons.Color.accent, Style.selectedFillAlpha) + : Style.normalFillFor(actionItem.foreground, Commons.Color.accent) + borderSpec: Border.controlSpec(actionItem.primary ? "selected" : "normal", actionItem.foreground, Commons.Color.accent) + + Text { + anchors.centerIn: parent + width: parent.width - Style.spacing.lg * 2 + horizontalAlignment: Text.AlignHCenter + textFormat: Text.PlainText + text: actionItem.label + color: actionItem.primary ? Commons.Color.accent : actionItem.foreground + font.family: Style.font.family + font.pixelSize: Style.font.subtitle + font.bold: true + elide: Text.ElideRight + } + + MouseArea { + id: actionTouch + anchors.fill: parent + enabled: actionItem.enabled + onClicked: actionItem.tapped() + } + } + + component InfoRow: Row { + id: infoItem + property string glyph: "" + property string text: "" + property bool urgent: false + property color foreground: Commons.Color.popups.text + spacing: Style.spacing.lg + + Text { + id: infoGlyph + textFormat: Text.PlainText + text: infoItem.glyph + color: infoItem.urgent ? Commons.Color.urgent : infoItem.foreground + font.family: Style.font.family + font.pixelSize: Style.font.iconLarge + } + + Text { + width: infoItem.width - infoGlyph.width - infoItem.spacing + anchors.verticalCenter: infoGlyph.verticalCenter + textFormat: Text.PlainText + text: infoItem.text + color: infoItem.urgent ? Commons.Color.urgent : infoItem.foreground + font.family: Style.font.family + font.pixelSize: Style.font.body + wrapMode: Text.Wrap + } + } +} diff --git a/packages/omamobile-shell/plugins/omamobile.mobilenetwork/MobileNetworkModel.js b/packages/omamobile-shell/plugins/omamobile.mobilenetwork/MobileNetworkModel.js new file mode 100644 index 000000000..a6ea45b14 --- /dev/null +++ b/packages/omamobile-shell/plugins/omamobile.mobilenetwork/MobileNetworkModel.js @@ -0,0 +1,129 @@ +// SPDX-License-Identifier: MIT +// The Mobile network page's pure parts: omamobile-mobile-network's status +// in, the rows the page shows out. No QML here, so test/mobile-network-model.js +// runs it under Node through the module.exports below. + +var STATUS_ARGV = ["omamobile-mobile-network", "status"] +var REBOOT_ARGV = ["omarchy-system-reboot"] +var APN = /^[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*$/ + +function parseJson(text) { + try { + var value = JSON.parse(String(text || "")) + return value && typeof value === "object" ? value : null + } catch (e) { + return null + } +} + +function dataArgv(on) { return ["omamobile-mobile-network", "data", on ? "on" : "off"] } +function roamingArgv(on) { return ["omamobile-mobile-network", "roaming", on ? "on" : "off"] } +function apnArgv(value) { return ["omamobile-mobile-network", "apn", value] } +function attachArgv(value) { return ["omamobile-mobile-network", "attach-apn", value] } + +// What the override field accepts: "" or "auto" (from the SIM), or a plain +// carrier APN (the helper checks again as root). +function apnValue(text) { + var s = String(text || "").trim() + if (s === "" || s.toLowerCase() === "auto") return "auto" + return APN.test(s) && s.length <= 99 ? s : null +} + +var REGISTRATION = { + "home": "Connected", "roaming": "Roaming", "searching": "Searching for a network", + "not-registered": "Not registered", "denied": "Registration refused by the network", + "unknown": "Starting" +} +var SIM = { + "ready": "", "absent": "No SIM card", "pin": "The SIM asks for its PIN", + "puk": "The SIM is PUK-blocked", "network-locked": "The SIM is locked to another network", + "error": "SIM card error", "detected": "Reading the SIM", "unknown": "Reading the SIM" +} +var SOURCES = { + "manual": "Typed in settings", "override": "Chosen from your SIM", + "carrier-database": "Chosen from your SIM", "none": "None found for this SIM" +} + +// The page as one object: whether it has a modem at all, a headline, and +// the rows. s: omamobile-mobile-network status (or null while it loads). +function view(s) { + if (!s) return { state: "loading", title: "Mobile network", body: "Reading the modem..." } + if (!s.installed) + return { state: "none", title: "No modem", body: "This phone's modem software isn't installed." } + if (!s.running) + return { state: "off", title: "Mobile network is off", + body: "The modem service isn't running. It starts with the phone once the modem is set up." } + var sim = s.sim || {} + var simProblem = SIM[sim.state] !== undefined ? SIM[sim.state] : "Reading the SIM" + var registered = s.registration === "home" || s.registration === "roaming" + var title = registered ? (s.carrier || "Mobile network") : (simProblem || REGISTRATION[s.registration] || "Starting") + var status = registered ? (REGISTRATION[s.registration] + (s.tech ? " · " + s.tech : "")) : "" + if (s.stage && s.stage !== "running" && !registered) status = "Setting up the modem" + var apn = s.apn || {} + var data = s.data || {} + var dataText + if (data.available === false) dataText = "Unavailable: no APN for this SIM. Type your carrier's APN below." + else if (!data.enabled) dataText = "Off" + else dataText = data.up ? "On" : "Starting" + return { + state: "ready", + title: title, + body: status, + signal: signalText(s.signal, registered), + signalBars: registered && s.signal >= 0 ? Math.min(4, Math.floor(s.signal / 25) + (s.signal > 0 ? 1 : 0)) : -1, + dataOn: !!data.enabled, + dataAvailable: data.available !== false, + dataText: dataText, + roamingAllowed: !s.roaming || s.roaming.allowed !== false, + roamingText: s.roaming && s.roaming.now + ? (s.roaming.allowed === false ? "Roaming now: mobile data paused" : "Roaming now") + : "Use mobile data when roaming", + apnInUse: apn.in_use || "", + apnSource: SOURCES[apn.source] || "", + apnConfigured: apn.configured && apn.configured !== "auto" ? apn.configured : "", + attachText: attachText(apn), + attachConfigured: apn.attach_configured && apn.attach_configured !== "auto" ? apn.attach_configured : "", + pinText: pinText(sim) + } +} + +// The attach APN (the one the modem registers with), for the Advanced part. +// "none" (sitd's attach_apn = none): no attach APN is sent, and the modem +// attaches with its carrier profile, as on Verizon's network. +function attachText(apn) { + if (apn.attach_source === "same" || (!apn.attach_source && apn.attach_in_use && apn.attach_in_use === apn.in_use)) + return "Same as the internet APN" + if (!apn.attach_in_use) return apn.source === "none" ? "The modem's own" : "Not chosen yet" + var source = SOURCES[apn.attach_source] ? " · " + SOURCES[apn.attach_source] : "" + if (String(apn.attach_in_use).toLowerCase() === "none") return "Not sent: the carrier profile's own" + source + return apn.attach_in_use + source +} + +function signalText(signal, registered) { + if (!registered) return "No signal" + if (typeof signal !== "number" || signal < 0) return "Not reported yet" + return signal + "%" +} + +function pinText(sim) { + if (sim.state === "pin" || sim.pin_lock === "on") + return "On. This phone can't enter a SIM PIN yet: turn the lock off in another phone." + if (sim.pin_lock === "off") return "Off" + return "Unknown" +} + +// After an APN change: it applies at the next start of the modem service, +// which needs a restart of the phone. +function apnSavedText(value, attach) { + var what = attach ? "attach APN" : "APN" + return value === "auto" + ? "Saved: the " + what + " comes from your SIM after a restart." + : "Saved: " + value + " is the " + what + " after a restart." +} + +if (typeof module !== "undefined") { + module.exports = { STATUS_ARGV: STATUS_ARGV, REBOOT_ARGV: REBOOT_ARGV, parseJson: parseJson, + dataArgv: dataArgv, roamingArgv: roamingArgv, apnArgv: apnArgv, attachArgv: attachArgv, apnValue: apnValue, + attachText: attachText, + view: view, signalText: signalText, pinText: pinText, apnSavedText: apnSavedText } +} diff --git a/packages/omamobile-shell/plugins/omamobile.mobilenetwork/Readable.js b/packages/omamobile-shell/plugins/omamobile.mobilenetwork/Readable.js new file mode 100644 index 000000000..2a7811cd2 --- /dev/null +++ b/packages/omamobile-shell/plugins/omamobile.mobilenetwork/Readable.js @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: MIT +// The smallest size for text that has to be read. Omarchy's type scale is a +// desktop's (caption 10 px, body 12 px); on the phone's 480 px wide screen, +// held at arm's length, nothing read goes under 12 px (docs/TOUCH-PLAN.md +// 2.1). A floor here rather than theme font overrides, which a theme change +// would drop. Plugins are separate directories with no shared import, so +// each one that needs it carries this same file (test/all checks they match). + +var MIN_PX = 12 + +function px(size) { return Math.max(MIN_PX, size) } + +if (typeof module !== "undefined") { + module.exports = { MIN_PX: MIN_PX, px: px } +} diff --git a/packages/omamobile-shell/plugins/omamobile.mobilenetwork/manifest.json b/packages/omamobile-shell/plugins/omamobile.mobilenetwork/manifest.json new file mode 100644 index 000000000..0397fb85c --- /dev/null +++ b/packages/omamobile-shell/plugins/omamobile.mobilenetwork/manifest.json @@ -0,0 +1,14 @@ +{ + "schemaVersion": 1, + "id": "omamobile.mobilenetwork", + "name": "Mobile network", + "version": "0.1.0", + "author": "Omarchy Mobile", + "license": "MIT", + "description": "The phone's Mobile network page: carrier, signal and registration, mobile data and data roaming, the APN in use with a field to type one, and the SIM PIN lock. Reads and changes them through omamobile-mobile-network; says \"No modem\" without the modem stack.", + "kinds": ["panel"], + "keepLoaded": true, + "entryPoints": { + "panel": "MobileNetwork.qml" + } +} diff --git a/packages/omamobile-shell/plugins/omamobile.quicksettings/QuickSettings.qml b/packages/omamobile-shell/plugins/omamobile.quicksettings/QuickSettings.qml index 76249c085..d7bdd17dd 100644 --- a/packages/omamobile-shell/plugins/omamobile.quicksettings/QuickSettings.qml +++ b/packages/omamobile-shell/plugins/omamobile.quicksettings/QuickSettings.qml @@ -1003,6 +1003,14 @@ Item { readonly property string status: !available ? "No modem" : !enabled ? "Off" : (root.probed.data_status || "On") function toggle() { if (available) root.runTileAction("mobile-data", !enabled) } + // The chevron (and a long press): the Mobile network page + // (omamobile.mobilenetwork), through the CLI, since a plugin only + // summons its own panels. Shown with a modem only. + readonly property bool details: available + function openDetails() { + root.dismiss() + Quickshell.execDetached(["omarchy-shell", "shell", "summon", "omamobile.mobilenetwork", "{}"]) + } } // Rotation lock over omamobile-autorotate (omamobile-sensors): it rotates diff --git a/packages/omamobile-shell/profile/shell.json b/packages/omamobile-shell/profile/shell.json index 3dd102dbd..001b26942 100644 --- a/packages/omamobile-shell/profile/shell.json +++ b/packages/omamobile-shell/profile/shell.json @@ -50,6 +50,9 @@ }, { "id": "omamobile.voice" + }, + { + "id": "omamobile.mobilenetwork" } ] } diff --git a/packages/omamobile-shell/test/all b/packages/omamobile-shell/test/all index 25efdf2e2..4a242065b 100755 --- a/packages/omamobile-shell/test/all +++ b/packages/omamobile-shell/test/all @@ -1005,6 +1005,45 @@ fi bash test/osk-guard.sh || fail "omamobile-osk-guard" bash -n bin/omamobile-onboarding || fail "omamobile-onboarding parses" bash test/onboarding-fingerprint.sh || fail "omamobile-onboarding's fingerprint offer" +bash test/onboarding-mobile.sh || fail "omamobile-onboarding's SIM line" + +# The Mobile network page (omamobile.mobilenetwork): on for new and existing +# users, Setup > Mobile network only where the modem stack is, the Mobile +# data tile's chevron opens it, and its backend and model behave. +m=plugins/omamobile.mobilenetwork/MobileNetwork.qml +python3 -c 'import json; assert {"id": "omamobile.mobilenetwork"} in json.load(open("profile/shell.json"))["plugins"]' \ + || fail "new users get the Mobile network page" +grep -c 'add_plugin omamobile.mobilenetwork' bin/omamobile-setup-user >/dev/null || fail "existing users get the Mobile network page" +grep -c '"setup.mobile-network".*"when":"test -x \\/usr\\/lib\\/omamobile-sit\\/omamobile-sit-config".*summon omamobile.mobilenetwork' bin/omamobile-setup-user >/dev/null || + fail "Setup > Mobile network opens the page, only with the modem stack" +grep -c '"omarchy-shell", "shell", "summon", "omamobile.mobilenetwork", "{}"' plugins/omamobile.quicksettings/QuickSettings.qml >/dev/null || + fail "the Mobile data tile's chevron opens the page" +grep -c 'exclusionMode: ExclusionMode.Normal' $m >/dev/null || fail "the APN field stays above the on-screen keyboard" +if grep -nE 'execDetached\(\[.*(pkexec|omamobile-sit-config)' $m; then fail "the page changes settings only through omamobile-mobile-network"; fi +if grep -nE 'systemctl.*(restart|stop).*omamobile-(modem|sitd)' $m bin/omamobile-mobile-network; then fail "the page never restarts the modem services"; fi +python3 -B -c 'import ast,sys; ast.parse(open(sys.argv[1]).read())' bin/omamobile-mobile-network || fail "omamobile-mobile-network parses" +python3 -B test/mobile-network.py >/dev/null 2>&1 || { python3 -B test/mobile-network.py; fail "omamobile-mobile-network"; } +if command -v node >/dev/null; then + node test/mobile-network-model.js >/dev/null || { node test/mobile-network-model.js; fail "Mobile network page model"; } +fi +grep -c 'Model.attachArgv(v)' $m >/dev/null && grep -c 'id: attachField' $m >/dev/null || + fail "the attach APN has its own field under Advanced" +python3 -B - <<'PY' || fail "the Mobile network page lints clean" +import importlib.machinery, importlib.util, os, sys +loader = importlib.machinery.SourceFileLoader("omamobile_widget", "bin/omamobile-widget") +spec = importlib.util.spec_from_loader("omamobile_widget", loader) +tool = importlib.util.module_from_spec(spec) +loader.exec_module(tool) +if not tool.qmllint_path() or not os.path.isdir(os.path.join(tool.OMARCHY_PATH, "shell")): + sys.exit(0) +lint = tool.run_qmllint([os.path.abspath("plugins/omamobile.mobilenetwork/MobileNetwork.qml")]) +noise = ("is not creatable", "QProcess::ExitStatus") +bad = lint["errors"] + [w for w in lint["warnings"] if not any(n in w for n in noise)] +for line in bad: + print(" " + line) +sys.exit(1 if bad else 0) +PY +pass "Mobile network page" bash -n bin/omamobile-sleepd || fail "omamobile-sleepd parses" bash test/sleepd.sh || fail "omamobile-sleepd" python3 -B -c 'import ast,sys; ast.parse(open(sys.argv[1]).read())' bin/omamobile-weather || fail "omamobile-weather parses" diff --git a/packages/omamobile-shell/test/mobile-network-model.js b/packages/omamobile-shell/test/mobile-network-model.js new file mode 100644 index 000000000..f35014826 --- /dev/null +++ b/packages/omamobile-shell/test/mobile-network-model.js @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: MIT +// Node tests for plugins/omamobile.mobilenetwork/MobileNetworkModel.js. Run by test/all. +"use strict" +const assert = require("assert") +const path = require("path") +const M = require(path.join(__dirname, "../plugins/omamobile.mobilenetwork/MobileNetworkModel.js")) + +const tests = [] +const test = (name, fn) => tests.push([name, fn]) + +const ready = (over) => Object.assign({ + installed: true, running: true, stage: "running", carrier: "Example", registration: "home", tech: "LTE", + signal: -1, sim: { state: "ready", pin_lock: "off" }, data: { enabled: true, up: true, available: true }, + roaming: { now: false, allowed: true }, apn: { in_use: "internet.example", source: "carrier-database", configured: "auto", + attach_in_use: "internet.example", attach_source: "same", attach_configured: "auto" } +}, over || {}) + +test("no modem stack: the page says so and nothing else", () => { + assert.strictEqual(M.view({ installed: false }).state, "none") + assert.strictEqual(M.view({ installed: false }).title, "No modem") + assert.strictEqual(M.view(null).state, "loading") + assert.strictEqual(M.view({ installed: true, running: false }).state, "off") +}) + +test("registered: carrier, LTE, data on, APN from the SIM", () => { + const v = M.view(ready()) + assert.strictEqual(v.title, "Example") + assert.strictEqual(v.body, "Connected · LTE") + assert.strictEqual(v.dataText, "On") + assert.strictEqual(v.apnSource, "Chosen from your SIM") + assert.strictEqual(v.apnConfigured, "") + assert.strictEqual(v.signal, "Not reported yet") + assert.strictEqual(v.pinText, "Off") +}) + +test("no APN for the SIM: data unavailable, pointed at the field", () => { + const v = M.view(ready({ data: { enabled: false, up: false, available: false }, apn: { in_use: "", source: "none", configured: "auto" } })) + assert.strictEqual(v.dataAvailable, false) + assert.ok(v.dataText.indexOf("Type your carrier's APN") >= 0) + assert.strictEqual(v.apnSource, "None found for this SIM") +}) + +test("SIM states, roaming and signal", () => { + assert.strictEqual(M.view(ready({ registration: "searching", sim: { state: "absent" } })).title, "No SIM card") + assert.ok(M.view(ready({ sim: { state: "ready", pin_lock: "on" } })).pinText.indexOf("another phone") >= 0) + const r = M.view(ready({ registration: "roaming", roaming: { now: true, allowed: false } })) + assert.strictEqual(r.roamingText, "Roaming now: mobile data paused") + assert.strictEqual(r.roamingAllowed, false) + assert.strictEqual(M.view(ready({ signal: 80 })).signal, "80%") + assert.strictEqual(M.view(ready({ signal: 80 })).signalBars, 4) + assert.strictEqual(M.view(ready({ registration: "searching" })).signal, "No signal") +}) + +test("APN field: auto, plain APNs, nothing else", () => { + assert.strictEqual(M.apnValue(""), "auto") + assert.strictEqual(M.apnValue(" Auto "), "auto") + assert.strictEqual(M.apnValue("internet.example"), "internet.example") + assert.strictEqual(M.apnValue("bad apn"), null) + assert.strictEqual(M.apnValue("x\n[control]"), null) + assert.strictEqual(M.apnValue("a".repeat(100)), null) + assert.deepStrictEqual(M.apnArgv("auto"), ["omamobile-mobile-network", "apn", "auto"]) + assert.deepStrictEqual(M.dataArgv(true), ["omamobile-mobile-network", "data", "on"]) + assert.deepStrictEqual(M.roamingArgv(false), ["omamobile-mobile-network", "roaming", "off"]) + assert.ok(M.apnSavedText("auto").indexOf("restart") >= 0) + assert.ok(M.apnSavedText("ims", true).indexOf("attach APN") >= 0) + // attach_apn = none: no attach APN is sent (Verizon's network). + assert.equal(M.attachText({ in_use: "VZWINTERNET", source: "override", attach_in_use: "none", attach_source: "override" }), + "Not sent: the carrier profile's own \u00b7 Chosen from your SIM") + assert.equal(M.attachText({ in_use: "a.example", attach_in_use: "ims.example", attach_source: "manual" }), + "ims.example \u00b7 Typed in settings") + assert.deepStrictEqual(M.attachArgv("ims"), ["omamobile-mobile-network", "attach-apn", "ims"]) +}) + +test("attach APN: same as internet, or the carrier's", () => { + assert.strictEqual(M.view(ready()).attachText, "Same as the internet APN") + const v = M.view(ready({ apn: { in_use: "vzwinternet", source: "override", configured: "auto", + attach_in_use: "vzwims", attach_source: "override", attach_configured: "auto" } })) + assert.strictEqual(v.attachText, "vzwims · Chosen from your SIM") + assert.strictEqual(v.attachConfigured, "") + assert.strictEqual(M.view(ready({ apn: { attach_configured: "ims", attach_in_use: "ims", attach_source: "manual" } })).attachConfigured, "ims") +}) + +let failed = 0 +for (const [name, fn] of tests) { + try { fn(); console.log("ok - " + name) } catch (e) { failed++; console.log("not ok - " + name + "\n " + e.message) } +} +process.exit(failed ? 1 : 0) diff --git a/packages/omamobile-shell/test/mobile-network.py b/packages/omamobile-shell/test/mobile-network.py new file mode 100644 index 000000000..05d80f20b --- /dev/null +++ b/packages/omamobile-shell/test/mobile-network.py @@ -0,0 +1,122 @@ +# SPDX-License-Identifier: MIT +"""omamobile-mobile-network (the Mobile network page's backend) against +made-up sitd states and settings: the view it prints, "No modem" without +the stack, and that changes go to the right commands. Run by test/all.""" +import importlib.machinery +import importlib.util +import json +import os +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest + +HERE = Path(__file__).resolve().parent +TOOL = HERE.parent / 'bin' / 'omamobile-mobile-network' +loader = importlib.machinery.SourceFileLoader('omamobile_mobile_network', str(TOOL)) +spec = importlib.util.spec_from_loader('omamobile_mobile_network', loader) +mn = importlib.util.module_from_spec(spec) +loader.exec_module(mn) + +CONF = dict(apn='auto', attach_apn='auto', roaming=True) + + +def state(**modem): + base = dict(stage='running', registration='home', tech='LTE', operator='Example', plmn='00101', + operator_name='Example', voice=True, sms=True, data=True, + sim=dict(state='ready', pin_retries=3, puk_retries=10, pin_lock='off'), + apn=dict(value='internet.example', source='carrier-database', attach='internet.example', + attach_source='same'), data_available=True) + base.update(modem) + return dict(modem=base, calls=[], sms=dict(unread=0)) + + +class Describe(unittest.TestCase): + def test_registered(self): + v = mn.describe(True, state(), CONF, True) + self.assertEqual((v['carrier'], v['registration'], v['tech'], v['signal']), ('Example', 'home', 'LTE', -1)) + self.assertEqual(v['data'], dict(enabled=True, up=True, available=True)) + self.assertEqual(v['apn'], dict(in_use='internet.example', source='carrier-database', configured='auto', + attach_in_use='internet.example', attach_source='same', + attach_configured='auto')) + self.assertEqual(v['sim'], dict(state='ready', pin_lock='off')) + self.assertEqual(v['roaming'], dict(now=False, allowed=True)) + + def test_no_stack_and_not_running(self): + self.assertFalse(mn.describe(False, None, CONF, False)['installed']) + v = mn.describe(True, None, CONF, False) + self.assertEqual((v['installed'], v['running']), (True, False)) + + def test_signal_from_modemmanager_only_when_recent(self): + self.assertEqual(mn.describe(True, state(), CONF, True, dict(SignalQuality=(70, True)))['signal'], 70) + self.assertEqual(mn.describe(True, state(), CONF, True, dict(SignalQuality=(70, False)))['signal'], -1) + + def test_unregistered_shows_no_carrier_and_no_apn_means_unavailable(self): + v = mn.describe(True, state(registration='searching', data=False, data_available=False, + apn=dict(value='', source='none')), dict(apn='auto', attach_apn='auto', roaming=False), False) + self.assertEqual((v['carrier'], v['tech'], v['data']['available'], v['roaming']['allowed']), + ('', '', False, False)) + + def test_no_identifiers_in_the_view(self): + text = json.dumps(mn.describe(True, state(), CONF, True)) + self.assertNotIn('00101', text) + self.assertNotIn('retries', text) + + +class Cli(unittest.TestCase): + def setUp(self): + self.dir = tempfile.TemporaryDirectory() + d = Path(self.dir.name) + (d / 'run').mkdir() + (d / 'bin').mkdir() + self.log = d / 'log' + for name in ('systemctl', 'pkexec', 'omamobile-sit'): + fake = d / 'bin' / name + fake.write_text(f'#!/bin/sh\necho "{name} $*" >> "{self.log}"\n' + + ('exit 1\n' if name == 'systemctl' else '')) + fake.chmod(0o755) + self.helper = d / 'omamobile-sit-config' + self.helper.write_text('#!/bin/sh\n') + self.helper.chmod(0o755) + (d / 'modem-sit.conf').write_text('[network]\napn = internet.example\n[data]\nroaming = no\n') + self.env = dict(os.environ, OMAMOBILE_SIT_RUN=str(d / 'run'), OMAMOBILE_SIT_CONFIG=str(d / 'modem-sit.conf'), + OMAMOBILE_SIT_CONFIG_HELPER=str(self.helper), OMAMOBILE_MM='none', + PATH=str(d / 'bin') + ':/usr/bin:/bin') + self.d = d + + def tearDown(self): + self.dir.cleanup() + + def run_tool(self, *args): + return subprocess.run([sys.executable, str(TOOL), *args], env=self.env, capture_output=True, + text=True, timeout=20) + + def test_status(self): + (self.d / 'run' / 'state.json').write_text(json.dumps(state())) + v = json.loads(self.run_tool('status').stdout) + self.assertEqual((v['installed'], v['running'], v['data']['enabled']), (True, True, False)) + self.assertEqual((v['apn']['configured'], v['roaming']['allowed']), ('internet.example', False)) + + def test_not_installed(self): + self.helper.unlink() + v = json.loads(self.run_tool('status').stdout) + self.assertFalse(v['installed']) + + def test_changes(self): + if os.geteuid() == 0: + self.skipTest('runs the helper directly as root') + self.run_tool('data', 'on') + self.run_tool('roaming', 'off') + self.run_tool('apn', 'internet.example') + self.run_tool('attach-apn', 'ims') + self.assertEqual(self.log.read_text().splitlines(), [ + 'omamobile-sit data on', + f'pkexec {self.helper} set roaming off', + f'pkexec {self.helper} set apn internet.example', + f'pkexec {self.helper} set attach_apn ims']) + self.assertEqual(self.run_tool('data', 'maybe').returncode, 2) + + +if __name__ == '__main__': + unittest.main() diff --git a/packages/omamobile-shell/test/onboarding-mobile.sh b/packages/omamobile-shell/test/onboarding-mobile.sh new file mode 100644 index 000000000..c3401553b --- /dev/null +++ b/packages/omamobile-shell/test/onboarding-mobile.sh @@ -0,0 +1,40 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# omamobile-onboarding's SIM line against a fake omamobile-mobile-network: +# the carrier and "set up automatically" with a SIM, a pointer to Setup > +# Mobile network when no APN was found, and silence without the modem stack. +set -euo pipefail +cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." +fail() { printf 'not ok - %s\n' "$1" >&2; exit 1; } +pass() { printf 'ok - %s\n' "$1"; } +t=$(mktemp -d); trap 'rm -rf "$t"' EXIT +F=$t/fakes; mkdir -p "$F" "$t/home" +fake() { printf '#!/bin/bash\n%s\n' "$2" >"$F/$1"; chmod +x "$F/$1"; } +fake git 'echo set' +fake unix_chkpwd 'exit 1' +fake gum 'case $1 in input) echo ;; confirm) exit 1 ;; esac' +fake omamobile-set-pin 'case $1 in --type) echo PIN ;; esac' +fake timeout 'shift; "$@"' +fake omamobile-mobile-network '[[ -e $T/status ]] && cat "$T/status"; true' +ob() { + env -u XDG_STATE_HOME T="$t" HOME="$t/home" USER=alice OMARCHY_PATH="$t/none" OMAMOBILE_FP_RUNNER="$t/none" \ + PATH="$F:/usr/bin:/bin" bash bin/omamobile-onboarding --inner >"$t/out" 2>&1 +} +status() { + printf '{"installed": true, "running": true, "carrier": "%s", "sim": {"state": "%s"}, "apn": {"source": "%s"}}\n' "$1" "$2" "$3" >"$t/status" +} + +status Example ready carrier-database; ob +grep -c "^Mobile network: Example. Mobile data is set up automatically from your SIM; there's nothing to type.$" "$t/out" >/dev/null || + { cat "$t/out"; fail "a SIM: the carrier, and nothing to type"; } +status "" ready override; ob +grep -c "^Your SIM is ready. Mobile data is set up automatically" "$t/out" >/dev/null || fail "not registered yet: still automatic" +status Example ready none; ob +grep -c "Mobile data needs your carrier's APN: Setup > Mobile network." "$t/out" >/dev/null || fail "no APN found: points at the page" +status "" absent ""; ob +grep -c "No SIM card" "$t/out" >/dev/null || fail "no SIM: says so" +rm -f "$t/status"; ob +grep -cE "Mobile network:|Your SIM|No SIM|Mobile data" "$t/out" >/dev/null && fail "no modem stack: nothing about the SIM" +printf '{"installed": false}\n' >"$t/status"; ob +grep -cE "Mobile network:|Your SIM|No SIM|Mobile data" "$t/out" >/dev/null && fail "not installed: nothing about the SIM" +pass "setup: the SIM's carrier and automatic mobile data, nothing to type" diff --git a/packages/omamobile-shell/version b/packages/omamobile-shell/version index c2f0a830c..556f9e2c8 100644 --- a/packages/omamobile-shell/version +++ b/packages/omamobile-shell/version @@ -1 +1 @@ -0.2.98 +0.2.99 diff --git a/tools/sit-sim b/tools/sit-sim index e47f9bba8..f00031a32 100755 --- a/tools/sit-sim +++ b/tools/sit-sim @@ -25,6 +25,10 @@ fictional 555-01xx ones, addresses are from the documentation ranges. tools/sit-sim --ipc PATH --router PATH [--transcript FILE] [--search-polls N] [--incoming-sms] [--no-sim] [--reject-setup CAUSE] [--no-ims-registration] [--unconfirmed-hangup] [--control PATH] + [--imsi-plmn MCCMNC] [--no-imsi] + +GET_IMSI answers a home network code (--imsi-plmn, default the test +network 00101) and a made-up subscriber part; --no-imsi refuses it. --no-ims-registration accepts ADD_PDN_INFO but never registers IMS (a carrier whose IMS values differ). --unconfirmed-hangup acknowledges hangup @@ -54,6 +58,10 @@ TEST_SENDER = '+12025550123' # fictional # identity the registration indication carries. TEST_AID = bytes.fromhex('a0000000871002ff') + b'8901260000000000017'[:8] TEST_LABEL = b'SIMSIM-LABEL' +# GET_IMSI's default answer: the test network 001/01 (in no carrier +# database) and a made-up subscriber part. --imsi-plmn gives another home +# network code (a carrier's public MCC/MNC) with the same made-up tail. +TEST_MSIN = '000000017' TEST_IMS_URI = b'sip:001010000000017@ims.mnc001.mcc001.3gppnetwork.org;tel:+12025550100' # The do-not-send list (MODEM-SIT-RESEARCH section 5), by ID. DENIED = {0x0947, 0x0937, 0x0948, 0x0907, 0x0908, 0x0910, 0x093A, 0x0942, 0x0940, 0x0406, @@ -63,7 +71,7 @@ DENIED_FAMILIES = (0x0E, 0x13) # Expected request lengths (the stock builders'). LENGTHS = {0x0600: 983, 0x0603: 251, 0x0613: 246, 0x0d00: 12 + 679, 0x0d1b: 272, 0x0d08: 12 + 679, 0x0102: 21, 0x0d1d: 21, 0x0748: 13, 0x0710: 13, 0x0625: 14, - 0x061b: 16, 0x0d39: 14, 0x0d03: 15, 0x0d02: 18, 0x0d0a: 15, 0x0902: 16, 0x0928: 16} + 0x061b: 16, 0x0d39: 14, 0x0d03: 15, 0x0d02: 18, 0x0d0a: 15, 0x0902: 16, 0x0928: 16, 0x0400: 29} def addr(text): @@ -148,7 +156,11 @@ class Sim: def ok(self, mid, token, p): self.respond(mid, token) - r_0603 = r_0613 = r_0625 = r_0d39 = r_0d07 = r_0d1d = r_0102 = r_0902 = r_0928 = ok + r_0613 = r_0625 = r_0d39 = r_0d07 = r_0d1d = r_0102 = r_0902 = r_0928 = ok + + def r_0603(self, mid, token, p): # SET_INITIAL_ATTACH_APN + self.log(event='initial_attach', apn=p[4:0xd9 - 12].split(b'\0')[0].decode('ascii', 'replace')) + self.respond(mid, token) def r_0200(self, mid, token, p): # GET_SIM_STATUS if self.a.no_sim: @@ -157,9 +169,20 @@ class Sim: app[0], app[1] = 2, 5 # USIM, READY app[3], app[4:4 + len(TEST_AID)] = len(TEST_AID), TEST_AID app[20], app[21:21 + len(TEST_LABEL)] = len(TEST_LABEL), TEST_LABEL + app[57] = 3 # PIN1 disabled (no PIN lock) app[59], app[60] = 3, 10 # PIN and PUK tries left self.respond(mid, token, 0, bytes([1, 0, 1]) + bytes(app)) + def r_0400(self, mid, token, p): # GET_IMSI + aid = p[1:1 + p[0]] if p and p[0] <= 16 else b'' + if aid != TEST_AID: + self.log(violation='GET_IMSI without the USIM AID') + return self.respond(mid, token, 2) + if self.a.no_imsi: + return self.respond(mid, token, 2) + imsi = (self.a.imsi_plmn + TEST_MSIN)[:15].encode() + self.respond(mid, token, 0, bytes([len(imsi)]) + imsi.ljust(15, b'\0')) + def registered(self): return self.radio == 1 and self.polls > self.a.search_polls and not self.a.no_sim @@ -346,6 +369,8 @@ def main(): p.add_argument('--no-ims-registration', action='store_true') p.add_argument('--unconfirmed-hangup', action='store_true') p.add_argument('--control') + p.add_argument('--imsi-plmn', default='00101') + p.add_argument('--no-imsi', action='store_true') p.add_argument('-v', '--verbose', action='store_true') a = p.parse_args() ipc = listen(a.ipc, socket.SOCK_SEQPACKET)