Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# 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, 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 (2026-11-28)

1.1.0-rc.10's code: only the version, this heading and the release notes changed. It is the
Expand Down
32 changes: 31 additions & 1 deletion core/runtime/homewifi-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -209,6 +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"
[ -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"
Expand Down Expand Up @@ -255,7 +262,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")"
Expand All @@ -268,12 +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"
# 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)"
Expand Down
19 changes: 17 additions & 2 deletions core/runtime/java/hmgapi/engine/HomeWifi.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ static List<String> describe(Engine e, Map<String, Map<String, Object>> 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. 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; }",
"wifi_svc() { case $(service check wifi 2>/dev/null) in *\": found\"*) true ;; *) false ;; esac; }",
Expand Down Expand Up @@ -242,15 +252,19 @@ static List<String> describe(Engine e, Map<String, Map<String, Object>> 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",
" [ -n \"$s\" ] && [ ${#s} -le 32 ] && safe \"$s\" || { echo \"that network name can't be used\"; return 2; }",
" 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",
" 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\"",
Expand Down Expand Up @@ -309,6 +323,7 @@ static List<String> describe(Engine e, Map<String, Map<String, Object>> 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",
Expand Down
60 changes: 60 additions & 0 deletions core/runtime/java/hmgapi/engine/WifiAp.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,72 @@ private static boolean clientSavedOn(Class<?> type, Object wifi) throws Reflecti
}
}

/**
* crc <wpa_supplicant.conf>: 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); 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()) {
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();
}
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 <mode>: 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) {
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) {
Expand Down
43 changes: 43 additions & 0 deletions core/runtime/test/hmgapi/engine/WifiApTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,50 @@ 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");
// 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 {
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);
Expand Down
Loading