From 0562c7742f7515ba84c12ed728779fea37c3e5fd Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Thu, 26 Nov 2026 10:29:39 -0500 Subject: [PATCH 1/3] Home Wi-Fi: keep the car's Wi-Fi checksum in step, so the client starts The stock Wi-Fi library (seen on an Ioniq 5 US 260507) starts the car's Wi-Fi client only when /data/misc/wifi/wpa_supplicant_crc32.conf holds the CRC-32 of wpa_supplicant.conf (8 upper-case hex digits). wifi.sh's join and forget edited the networks file and left it stale: logcat said "Wi-Fi will not be enabled" and home Wi-Fi stayed "searching". WifiAp crc writes it (only when it differs, owned system:wifi 660); wifi.sh runs it after every edit and before switching the client on. The test checks it against the pair the car's library wrote for its own template. A first network no longer needs printf (not on the unit): the config starts from the car's own template (/system/etc/wifi/wpa_supplicant.conf), as its library does. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 10 ++++++ core/runtime/homewifi-test.sh | 9 +++++ core/runtime/java/hmgapi/engine/HomeWifi.java | 14 +++++++- core/runtime/java/hmgapi/engine/WifiAp.java | 35 +++++++++++++++++++ .../test/hmgapi/engine/WifiApTest.java | 28 +++++++++++++++ 5 files changed, 95 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4168555..5f62794b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## Unreleased + +- **Home Wi-Fi joins on cars whose Wi-Fi library checks a checksum** (Ioniq 5 US 260507). The + stock library starts the car's Wi-Fi client only if `wpa_supplicant_crc32.conf` holds the + CRC-32 of `wpa_supplicant.conf`; HMG-API's saved networks didn't update it, so the client + never started ("Wi-Fi will not be enabled") and home Wi-Fi stayed "searching". Adding or + forgetting a network now updates it, and it's checked before each switch to home Wi-Fi. + A first network no longer needs `printf` (not on the unit): the config starts from the car's + own template. + ## 1.1.0-rc.10 (2026-11-25) - **No owner-facing way to force an unknown `app_process`, for now.** An empty `HMGAPI/FORCE` diff --git a/core/runtime/homewifi-test.sh b/core/runtime/homewifi-test.sh index a7e7d4ea..e36e521d 100644 --- a/core/runtime/homewifi-test.sh +++ b/core/runtime/homewifi-test.sh @@ -209,6 +209,7 @@ case "$3 $4" in fi ;; "client on") : > "$FW/client"; echo running > "$FW/props/init.svc.wpa_supplicant"; echo "client on" ;; "client off") rm -f "$FW/client"; echo stopped > "$FW/props/init.svc.wpa_supplicant"; echo "client off" ;; + "crc "*) echo crc >> "$FW/crc.log"; echo 01234567 ;; esac S printf '#!/bin/sh\n:\n' > "$F/bin/chown" @@ -255,7 +256,9 @@ out=$(HMGAPI_WIFI_PSK='has"quote' verb join "Cascadia" || true) has "join: a key that could break the config refused" "can't be used" "$out" out=$(HMGAPI_WIFI_PSK= verb join 'Bad"Name' || true) has "join: a name that could break the config refused" "can't be used" "$out" +: > "$F/fw/crc.log" check "join: saved" "saved: Cascadia" "$(HMGAPI_WIFI_PSK=cascadia-key verb join Cascadia)" +check "... the checksum the car's Wi-Fi library checks, updated" crc "$(cat "$F/fw/crc.log")" check "... both now" "Willows Cascadia" "$(verb saved)" has "... Android's own settings kept" "p2p_disabled=1" "$(cat "$NET")" @@ -268,12 +271,18 @@ check "forget" "forgotten: Cafe" "$(verb forget Cafe)" check "... gone, the others kept" "Cascadia Willows" "$(verb saved | sort)" check "seen: nothing before home Wi-Fi was on" "" "$(verb seen)" +mv "$NET" "$NET.keep"; mkdir -p "$X/system/etc/wifi"; printf 'update_config=1\nctrl_interface=wlan0\npmf=1\n' > "$X/system/etc/wifi/wpa_supplicant.conf" +HMGAPI_WIFI_PSK=first-net-key verb join First >/dev/null +has "join with no config yet: started from the car's template" "pmf=1" "$(cat "$NET")" +has "... with the network" 'ssid="First"' "$(cat "$NET")" +mv "$NET.keep" "$NET" # parked: the hotspot off, the car's client on echo "wpa_state=SCANNING" > "$F/fw/wpa.status" : > "$F/fw/apps.log" log=$(wk) has "parked: switched at once" "the car off: the hotspot off, the car's Wi-Fi client on" "$log" +has "... the checksum checked before the client goes on" crc "$(cat "$F/fw/crc.log")" check "... the hotspot off" no "$([ -f "$F/fw/ap" ] && echo yes || echo no)" has "... through Android's Wi-Fi service" "hmgapi.engine.WifiAp client on" "$(cat "$F/fw/apps.log")" check "... looking" searching "$(state)" diff --git a/core/runtime/java/hmgapi/engine/HomeWifi.java b/core/runtime/java/hmgapi/engine/HomeWifi.java index 58372e77..adbe2282 100644 --- a/core/runtime/java/hmgapi/engine/HomeWifi.java +++ b/core/runtime/java/hmgapi/engine/HomeWifi.java @@ -208,6 +208,10 @@ static List describe(Engine e, Map> json, Se "status() { echo \"{\\\"state\\\": \\\"$1\\\", \\\"rssi\\\": ${2:-0}, \\\"freq\\\": ${3:-0}}\" > $PUB.tmp && chmod 644 $PUB.tmp && mv $PUB.tmp $PUB; }", "reading() { hmgapid vehicle \"$1\" 2>/dev/null | while read -r k v; do [ \"$k\" = \"$1\" ] && echo \"$v\"; done; }", "tool() { CLASSPATH=$JAR app_process /system/bin hmgapi.engine.WifiAp \"$@\" 2>&1; }", + "# the car's Wi-Fi library starts the client only if wpa_supplicant_crc32.conf holds the networks", + "# file's CRC-32 (WifiAp.crc): after every edit here, and before switching the client on", + "crc_fix() { [ -f $NET ] && tool crc $NET >/dev/null; }", + "TEMPLATE=$X/system/etc/wifi/wpa_supplicant.conf # what the car's library starts a new config from", "boot() { cat $X/proc/sys/kernel/random/boot_id 2>/dev/null; }", "wants() { [ -f $ASKS ] && while read -r w rest; do [ \"$w\" = \"$1\" ] && { echo \"${rest:-$w}\"; }; done < $ASKS; }", "wifi_svc() { case $(service check wifi 2>/dev/null) in *\": found\"*) true ;; *) false ;; esac; }", @@ -242,6 +246,7 @@ static List describe(Engine e, Map> json, Se " echo -n \"$1\" > $NET.hmgapi", " chown wifi:wifi $NET.hmgapi 2>/dev/null; chmod 660 $NET.hmgapi", " mv $NET.hmgapi $NET", + " crc_fix", "}", "join() {", " s=$1 k=$HMGAPI_WIFI_PSK", @@ -249,7 +254,9 @@ static List describe(Engine e, Map> json, Se " safe \"$k\" || { echo \"that key can't be used (quotes, backslashes)\"; return 2; }", " if [ -n \"$k\" ] && [ ${#k} -lt 8 -o ${#k} -gt 63 ]; then echo \"a Wi-Fi key is 8 to 63 characters\"; return 2; fi", " client_mode && { echo \"home Wi-Fi is on right now (parked): add networks while the car is on\"; return 1; }", - " [ -f $NET ] || printf 'ctrl_interface=wlan0\\nupdate_config=1\\n' > $NET", + " if [ ! -f $NET ]; then # no printf on the unit; the car's own template, as its library does", + " if [ -f $TEMPLATE ]; then cat $TEMPLATE > $NET; else { echo ctrl_interface=wlan0; echo update_config=1; } > $NET; fi", + " fi", " c=$(without \"$s\")", " c=\"$c${nl}network={$nl\tssid=\\\"$s\\\"$nl\"", " if [ -n \"$k\" ]; then c=\"$c\tpsk=\\\"$k\\\"$nl\tkey_mgmt=WPA-PSK$nl\"; else c=\"$c\tkey_mgmt=NONE$nl\"; fi", @@ -268,6 +275,10 @@ static List describe(Engine e, Map> json, Se "}", "", "# ---- while joined: the car's own traffic on mobile data, the mods that asked on home Wi-Fi", + "# table 95 is never empty: while home Wi-Fi isn't up it holds an unreachable default, so traffic", + "# marked for home Wi-Fi (the mods that asked, HMG-API's own downloads) fails instead of falling", + "# through to the main table and the car's mobile data", + "no_route() { [ -n \"$(ip route show table $T 2>/dev/null)\" ] || ip route add unreachable default table $T 2>/dev/null; }", "route_up() {", " gw=", " for r in $(ip route show dev $I 2>/dev/null | while read -r a b c rest; do [ \"$a\" = default ] && [ \"$b\" = via ] && echo $c; done); do gw=$r; done", @@ -309,6 +320,7 @@ static List describe(Engine e, Map> json, Se " [ \"$m\" = 1 ] || [ \"$m\" = 2 ] || m=2 # the hotspot wasn't on: the projection one when it comes back", " echo $m > $W/apmode", " boot > $W/mode", + " crc_fix", " r=$(tool client on)", " if manual && [ \"$pw\" = on ]; then say \"on request: the hotspot off, the car's Wi-Fi client on ($r)\"", " else say \"the car off: the hotspot off, the car's Wi-Fi client on ($r)\"; fi", diff --git a/core/runtime/java/hmgapi/engine/WifiAp.java b/core/runtime/java/hmgapi/engine/WifiAp.java index 46163ae8..3e4912ab 100644 --- a/core/runtime/java/hmgapi/engine/WifiAp.java +++ b/core/runtime/java/hmgapi/engine/WifiAp.java @@ -85,12 +85,47 @@ private static boolean clientSavedOn(Class type, Object wifi) throws Reflecti } } + /** + * crc : the checksum the car's own Wi-Fi library checks before it starts + * the client. The stock libhardware_legacy (Ioniq 5 US 260507, and other Gen5W builds) keeps + * wpa_supplicant_crc32.conf next to the networks file: the file's CRC-32, 8 upper-case hex + * digits, no newline. If it doesn't match (any edit not made through Android's Wi-Fi settings), + * the library logs "Wi-Fi will not be enabled" and the client never starts. Written only when + * it differs, owned as the library makes it (system:wifi 660). Prints the checksum. + */ + static int crc(java.io.File conf) throws Exception { + if (!conf.isFile()) { + System.out.println("no " + conf); + return 1; + } + java.util.zip.CRC32 c = new java.util.zip.CRC32(); + c.update(Engine.readAll(new java.io.FileInputStream(conf))); + String want = String.format("%08X", c.getValue()); + java.io.File f = new java.io.File(conf.getParentFile(), "wpa_supplicant_crc32.conf"); + String have = f.isFile() ? Engine.read(f).trim() : null; + if (!want.equals(have)) { + java.io.File t = new java.io.File(f.getPath() + ".hmgapi"); + java.io.FileOutputStream o = new java.io.FileOutputStream(t); + try { + o.write(want.getBytes("US-ASCII")); + } finally { + o.close(); + } + Engine.child(new ProcessBuilder("chown", "system:wifi", t.getPath())).start().waitFor(); + Engine.child(new ProcessBuilder("chmod", "660", t.getPath())).start().waitFor(); + if (!t.renameTo(f)) throw new java.io.IOException("can't write " + f); + } + System.out.println(want); + return 0; + } + /** * restart: off and on again, the same mode. stop: off, printing the mode it had ("2" the projection * AP, "1" the plain hotspot, "0" it wasn't on). start : on again in that mode. */ public static void main(String[] args) throws Exception { String v = args.length > 0 ? args[0] : ""; + if ("crc".equals(v) && args.length == 2) System.exit(crc(new java.io.File(args[1]))); boolean client = "client".equals(v) && args.length == 2 && ("on".equals(args[1]) || "off".equals(args[1])); boolean ensure = "ensure-projection".equals(v) && args.length == 1; if (!(("restart".equals(v) || "stop".equals(v)) && args.length == 1) && !("start".equals(v) && args.length == 2) && !client && !ensure) { diff --git a/core/runtime/test/hmgapi/engine/WifiApTest.java b/core/runtime/test/hmgapi/engine/WifiApTest.java index e72eb35c..8890a797 100644 --- a/core/runtime/test/hmgapi/engine/WifiApTest.java +++ b/core/runtime/test/hmgapi/engine/WifiApTest.java @@ -77,7 +77,35 @@ private static void attach(String name, Manager m, String home, String says, int System.out.println("ok " + name); } + /** WifiAp.crc: the checksum file the car's Wi-Fi library checks (Ioniq 5 US 260507: its own pair below). */ + private static void crcChecks() throws Exception { + java.io.File d = java.nio.file.Files.createTempDirectory("crc").toFile(); + java.io.File conf = new java.io.File(d, "wpa_supplicant.conf"), crc = new java.io.File(d, "wpa_supplicant_crc32.conf"); + // the car's template (/system/etc/wifi/wpa_supplicant.conf, 98 bytes) and the checksum its library wrote for it + String template = "update_config=1\nctrl_interface=wlan0\neapol_version=1\nap_scan=1\nfast_reauth=1\np2p_disabled=1\npmf=1\n"; + java.nio.file.Files.write(conf.toPath(), template.getBytes("US-ASCII")); + java.io.PrintStream was = System.out; + System.setOut(new java.io.PrintStream(new java.io.ByteArrayOutputStream())); + try { + if (WifiAp.crc(conf) != 0) throw new AssertionError("crc: failed"); + String got = Engine.read(crc); + if (!"54931C7F".equals(got)) throw new AssertionError("crc: the car's library wrote 54931C7F for its template, got [" + got + "]"); + long t = crc.lastModified(); + crc.setLastModified(t - 100000); + WifiAp.crc(conf); + if (crc.lastModified() != t - 100000) throw new AssertionError("crc: rewritten though it matched"); + java.nio.file.Files.write(conf.toPath(), (template + "\nnetwork={\n\tssid=\"x\"\n\tkey_mgmt=NONE\n}\n").getBytes("US-ASCII")); + WifiAp.crc(conf); + if ("54931C7F".equals(Engine.read(crc)) || Engine.read(crc).length() != 8) throw new AssertionError("crc: not updated after an edit"); + if (WifiAp.crc(new java.io.File(d, "none")) == 0) throw new AssertionError("crc: no networks file is not ok"); + } finally { + System.setOut(was); + } + System.out.println("ok WifiAp crc: the car's checksum, 8 upper-case digits, written only when it changed"); + } + public static void main(String[] args) throws Exception { + crcChecks(); attach("attach: projection AP already up is left alone", new Manager(true, 2), null, "already up", 0); attach("attach: AP off is started in mode 2", new Manager(false, 0), "off", "started in mode 2", 1); attach("attach: plain hotspot is switched to mode 2 (as ensureHotspot did)", new Manager(true, 1), null, "started in mode 2", 1); From a4258bc9b74e948c51f6baf99663ad8dc8ba93c5 Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Fri, 27 Nov 2026 09:09:17 -0500 Subject: [PATCH 2/3] Home Wi-Fi: a checksum that can't be written is said; join writes once crc_fix no longer hides WifiAp's output and status: a failure is said in the keeper's log and in join's and forget's answer. WifiAp.crc checks chown and chmod, names the one that failed, and leaves the old checksum file as it was. join builds the whole config text (from the existing file, the car's template, or the two default lines) before put_net, instead of first writing the template to wpa_supplicant.conf as root. Tested off-car only (homewifi-test.sh, bash; no mksh here). Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 5 ++- core/runtime/homewifi-test.sh | 27 ++++++++++++-- core/runtime/java/hmgapi/engine/HomeWifi.java | 19 ++++++---- core/runtime/java/hmgapi/engine/WifiAp.java | 35 ++++++++++++++++--- .../test/hmgapi/engine/WifiApTest.java | 15 ++++++++ 5 files changed, 86 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f62794b..8f9219fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,10 @@ never started ("Wi-Fi will not be enabled") and home Wi-Fi stayed "searching". Adding or forgetting a network now updates it, and it's checked before each switch to home Wi-Fi. A first network no longer needs `printf` (not on the unit): the config starts from the car's - own template. + own template, and the whole text is built before the one write that sets Android's owner and + mode. A checksum that can't be written (its `chown` or `chmod` failing) is said in the + keeper's log and in the answer to adding or forgetting a network, instead of home Wi-Fi only + staying "searching". ## 1.1.0-rc.10 (2026-11-25) diff --git a/core/runtime/homewifi-test.sh b/core/runtime/homewifi-test.sh index e36e521d..193a545c 100644 --- a/core/runtime/homewifi-test.sh +++ b/core/runtime/homewifi-test.sh @@ -12,7 +12,11 @@ UNIT_SH=${UNIT_SH:-$(command -v mksh || command -v bash)} CP=$(mktemp -d) sh tools/engine-classes.sh "$CP" javac --release 8 -cp "$CP" -d "$CP" core/runtime/test/hmgapi/engine/WifiApTest.java -java -cp "$CP" hmgapi.engine.WifiApTest +# WifiAp.crc's chown system:wifi: a stand-in that fails only for a "chownfail" path (the test's failure case) +CB=$(mktemp -d) +printf '#!/bin/sh\ncase "$*" in *chownfail*) echo "chown: not permitted" >&2; exit 1 ;; esac\nexit 0\n' > "$CB/chown"; chmod +x "$CB/chown" +PATH=$CB:$PATH java -cp "$CP" hmgapi.engine.WifiApTest +rm -rf "$CB" T=$(mktemp -d) cleanup() { r=$?; [ $r != 0 ] && [ -z "$done" ] && echo "FAIL stopped (exit $r) after: ${last:-the start}"; [ -f "$T/fake/fw/wpa.sleep" ] && kill "$(cat "$T/fake/fw/wpa.sleep")" 2>/dev/null || true; rm -rf "$T" "$CP"; } trap cleanup EXIT @@ -209,7 +213,9 @@ case "$3 $4" in fi ;; "client on") : > "$FW/client"; echo running > "$FW/props/init.svc.wpa_supplicant"; echo "client on" ;; "client off") rm -f "$FW/client"; echo stopped > "$FW/props/init.svc.wpa_supplicant"; echo "client off" ;; - "crc "*) echo crc >> "$FW/crc.log"; echo 01234567 ;; + "crc "*) echo crc >> "$FW/crc.log" + [ -f "$FW/crc.fail" ] && { echo "checksum not written: chown system:wifi: exit 1"; exit 1; } + echo 01234567 ;; esac S printf '#!/bin/sh\n:\n' > "$F/bin/chown" @@ -271,18 +277,33 @@ check "forget" "forgotten: Cafe" "$(verb forget Cafe)" check "... gone, the others kept" "Cascadia Willows" "$(verb saved | sort)" check "seen: nothing before home Wi-Fi was on" "" "$(verb seen)" +: > "$F/fw/crc.fail" +out=$(HMGAPI_WIFI_PSK= verb join Cafe) +has "join: a checksum that can't be written is said" "the car's Wi-Fi checksum not updated, so its Wi-Fi client may not start: checksum not written: chown" "$out" +has "... the network still saved" "saved: Cafe" "$out" +verb forget Cafe >/dev/null; rm "$F/fw/crc.fail" mv "$NET" "$NET.keep"; mkdir -p "$X/system/etc/wifi"; printf 'update_config=1\nctrl_interface=wlan0\npmf=1\n' > "$X/system/etc/wifi/wpa_supplicant.conf" -HMGAPI_WIFI_PSK=first-net-key verb join First >/dev/null +# put_net's chown says whether a config was already there: join writes nothing of its own before put_net +printf '#!/bin/sh\n[ -n "$NETCHK" ] && { [ -e "$NETCHK" ] && echo there || echo none; } >> "$FW/net.before"\nexit 0\n' > "$F/bin/chown" +: > "$F/fw/net.before" +NETCHK=$NET HMGAPI_WIFI_PSK=first-net-key verb join First >/dev/null +check "join with no config yet: no config of ours before put_net" none "$(cat "$F/fw/net.before")" +printf '#!/bin/sh\n:\n' > "$F/bin/chown" has "join with no config yet: started from the car's template" "pmf=1" "$(cat "$NET")" has "... with the network" 'ssid="First"' "$(cat "$NET")" +check "... the template's lines first, as the car's library writes them" "update_config=1" "$(sed -n 1p "$NET")" +check "... Android's mode" "-rw-rw----" "$(ls -l "$NET" | cut -c1-10)" mv "$NET.keep" "$NET" # parked: the hotspot off, the car's client on echo "wpa_state=SCANNING" > "$F/fw/wpa.status" : > "$F/fw/apps.log" +: > "$F/fw/crc.fail" log=$(wk) +rm "$F/fw/crc.fail" has "parked: switched at once" "the car off: the hotspot off, the car's Wi-Fi client on" "$log" has "... the checksum checked before the client goes on" crc "$(cat "$F/fw/crc.log")" +has "... a checksum that can't be written: in the keeper's log, not just \"searching\"" "the car's Wi-Fi checksum not updated" "$log" check "... the hotspot off" no "$([ -f "$F/fw/ap" ] && echo yes || echo no)" has "... through Android's Wi-Fi service" "hmgapi.engine.WifiAp client on" "$(cat "$F/fw/apps.log")" check "... looking" searching "$(state)" diff --git a/core/runtime/java/hmgapi/engine/HomeWifi.java b/core/runtime/java/hmgapi/engine/HomeWifi.java index adbe2282..04602127 100644 --- a/core/runtime/java/hmgapi/engine/HomeWifi.java +++ b/core/runtime/java/hmgapi/engine/HomeWifi.java @@ -209,8 +209,14 @@ static List describe(Engine e, Map> json, Se "reading() { hmgapid vehicle \"$1\" 2>/dev/null | while read -r k v; do [ \"$k\" = \"$1\" ] && echo \"$v\"; done; }", "tool() { CLASSPATH=$JAR app_process /system/bin hmgapi.engine.WifiAp \"$@\" 2>&1; }", "# the car's Wi-Fi library starts the client only if wpa_supplicant_crc32.conf holds the networks", - "# file's CRC-32 (WifiAp.crc): after every edit here, and before switching the client on", - "crc_fix() { [ -f $NET ] && tool crc $NET >/dev/null; }", + "# file's CRC-32 (WifiAp.crc): after every edit here, and before switching the client on. A failure", + "# is said (the keeper's log; join's and forget's answer): otherwise home Wi-Fi only stays \"searching\"", + "crc_fix() {", + " [ -f $NET ] || return 0", + " r=$(tool crc $NET) && return 0", + " say \"the car's Wi-Fi checksum not updated, so its Wi-Fi client may not start: $r\"", + " return 1", + "}", "TEMPLATE=$X/system/etc/wifi/wpa_supplicant.conf # what the car's library starts a new config from", "boot() { cat $X/proc/sys/kernel/random/boot_id 2>/dev/null; }", "wants() { [ -f $ASKS ] && while read -r w rest; do [ \"$w\" = \"$1\" ] && { echo \"${rest:-$w}\"; }; done < $ASKS; }", @@ -254,10 +260,11 @@ static List describe(Engine e, Map> json, Se " safe \"$k\" || { echo \"that key can't be used (quotes, backslashes)\"; return 2; }", " if [ -n \"$k\" ] && [ ${#k} -lt 8 -o ${#k} -gt 63 ]; then echo \"a Wi-Fi key is 8 to 63 characters\"; return 2; fi", " client_mode && { echo \"home Wi-Fi is on right now (parked): add networks while the car is on\"; return 1; }", - " if [ ! -f $NET ]; then # no printf on the unit; the car's own template, as its library does", - " if [ -f $TEMPLATE ]; then cat $TEMPLATE > $NET; else { echo ctrl_interface=wlan0; echo update_config=1; } > $NET; fi", - " fi", - " c=$(without \"$s\")", + " # the text first, then one put_net: with no config yet, the car's own template (as its library", + " # does; no printf on the unit), never a file of ours written before put_net sets its owner", + " if [ -f $NET ]; then c=$(without \"$s\")", + " elif [ -f $TEMPLATE ]; then c=$(cat $TEMPLATE)", + " else c=\"ctrl_interface=wlan0${nl}update_config=1\"; fi", " c=\"$c${nl}network={$nl\tssid=\\\"$s\\\"$nl\"", " if [ -n \"$k\" ]; then c=\"$c\tpsk=\\\"$k\\\"$nl\tkey_mgmt=WPA-PSK$nl\"; else c=\"$c\tkey_mgmt=NONE$nl\"; fi", " c=\"$c\tpriority=1$nl}$nl\"", diff --git a/core/runtime/java/hmgapi/engine/WifiAp.java b/core/runtime/java/hmgapi/engine/WifiAp.java index 3e4912ab..d87d7374 100644 --- a/core/runtime/java/hmgapi/engine/WifiAp.java +++ b/core/runtime/java/hmgapi/engine/WifiAp.java @@ -91,7 +91,8 @@ private static boolean clientSavedOn(Class type, Object wifi) throws Reflecti * wpa_supplicant_crc32.conf next to the networks file: the file's CRC-32, 8 upper-case hex * digits, no newline. If it doesn't match (any edit not made through Android's Wi-Fi settings), * the library logs "Wi-Fi will not be enabled" and the client never starts. Written only when - * it differs, owned as the library makes it (system:wifi 660). Prints the checksum. + * it differs, owned as the library makes it (system:wifi 660); a chown or chmod that fails is an + * error and leaves the old file as it was. Prints the checksum. */ static int crc(java.io.File conf) throws Exception { if (!conf.isFile()) { @@ -111,21 +112,45 @@ static int crc(java.io.File conf) throws Exception { } finally { o.close(); } - Engine.child(new ProcessBuilder("chown", "system:wifi", t.getPath())).start().waitFor(); - Engine.child(new ProcessBuilder("chmod", "660", t.getPath())).start().waitFor(); - if (!t.renameTo(f)) throw new java.io.IOException("can't write " + f); + try { + run("chown", "system:wifi", t.getPath()); + run("chmod", "660", t.getPath()); + if (!t.renameTo(f)) throw new java.io.IOException("can't write " + f); + } finally { + t.delete(); // gone already once renamed + } } System.out.println(want); return 0; } + /** a tool the checksum file needs (chown, chmod): its failure, with what it said, is an IOException */ + private static void run(String... cmd) throws Exception { + Process p = Engine.child(new ProcessBuilder(cmd).redirectErrorStream(true)).start(); + String said = new String(Engine.readAll(p.getInputStream()), "UTF-8").trim(); + int rc = p.waitFor(); + if (rc == 0) return; + StringBuilder b = new StringBuilder(); + for (String c : cmd) b.append(b.length() == 0 ? "" : " ").append(c); + throw new java.io.IOException(b + ": exit " + rc + (said.length() == 0 ? "" : " (" + said + ")")); + } + /** * restart: off and on again, the same mode. stop: off, printing the mode it had ("2" the projection * AP, "1" the plain hotspot, "0" it wasn't on). start : on again in that mode. */ public static void main(String[] args) throws Exception { String v = args.length > 0 ? args[0] : ""; - if ("crc".equals(v) && args.length == 2) System.exit(crc(new java.io.File(args[1]))); + if ("crc".equals(v) && args.length == 2) { + int rc; + try { + rc = crc(new java.io.File(args[1])); + } catch (Exception x) { // one line for the keeper's log, not a stack trace + System.out.println("checksum not written: " + x.getMessage()); + rc = 1; + } + System.exit(rc); + } boolean client = "client".equals(v) && args.length == 2 && ("on".equals(args[1]) || "off".equals(args[1])); boolean ensure = "ensure-projection".equals(v) && args.length == 1; if (!(("restart".equals(v) || "stop".equals(v)) && args.length == 1) && !("start".equals(v) && args.length == 2) && !client && !ensure) { diff --git a/core/runtime/test/hmgapi/engine/WifiApTest.java b/core/runtime/test/hmgapi/engine/WifiApTest.java index 8890a797..9b0c7cdb 100644 --- a/core/runtime/test/hmgapi/engine/WifiApTest.java +++ b/core/runtime/test/hmgapi/engine/WifiApTest.java @@ -98,10 +98,25 @@ private static void crcChecks() throws Exception { WifiAp.crc(conf); if ("54931C7F".equals(Engine.read(crc)) || Engine.read(crc).length() != 8) throw new AssertionError("crc: not updated after an edit"); if (WifiAp.crc(new java.io.File(d, "none")) == 0) throw new AssertionError("crc: no networks file is not ok"); + // homewifi-test.sh's chown fails for a path with "chownfail" in it + java.io.File bad = new java.io.File(d, "chownfail"); + bad.mkdir(); + java.io.File badConf = new java.io.File(bad, "wpa_supplicant.conf"); + java.nio.file.Files.write(badConf.toPath(), template.getBytes("US-ASCII")); + boolean threw = false; + try { + WifiAp.crc(badConf); + } catch (java.io.IOException x) { + threw = x.getMessage().contains("chown"); + } + if (!threw) throw new AssertionError("crc: a chown that fails is an error that names it"); + String[] left = bad.list(); + if (left.length != 1) throw new AssertionError("crc: after a failure only the networks file, got " + java.util.Arrays.toString(left)); } finally { System.setOut(was); } System.out.println("ok WifiAp crc: the car's checksum, 8 upper-case digits, written only when it changed"); + System.out.println("ok WifiAp crc: a chown that fails is said, nothing left behind"); } public static void main(String[] args) throws Exception { From f77477aa772e02f3bf4a7da16ea5271c11215153 Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Mon, 30 Nov 2026 16:36:16 -0500 Subject: [PATCH 3/3] Home Wi-Fi: no_route() goes with #420, which first calls it (review) Co-Authored-By: Claude Opus 5.5 --- core/runtime/java/hmgapi/engine/HomeWifi.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/runtime/java/hmgapi/engine/HomeWifi.java b/core/runtime/java/hmgapi/engine/HomeWifi.java index 04602127..4f7c33e7 100644 --- a/core/runtime/java/hmgapi/engine/HomeWifi.java +++ b/core/runtime/java/hmgapi/engine/HomeWifi.java @@ -282,10 +282,6 @@ static List describe(Engine e, Map> json, Se "}", "", "# ---- while joined: the car's own traffic on mobile data, the mods that asked on home Wi-Fi", - "# table 95 is never empty: while home Wi-Fi isn't up it holds an unreachable default, so traffic", - "# marked for home Wi-Fi (the mods that asked, HMG-API's own downloads) fails instead of falling", - "# through to the main table and the car's mobile data", - "no_route() { [ -n \"$(ip route show table $T 2>/dev/null)\" ] || ip route add unreachable default table $T 2>/dev/null; }", "route_up() {", " gw=", " for r in $(ip route show dev $I 2>/dev/null | while read -r a b c rest; do [ \"$a\" = default ] && [ \"$b\" = via ] && echo $c; done); do gw=$r; done",