Network module: Wi-Fi and wired status, connect to Wi-Fi networks, Wi-Fi and airplane switches
CI / test (push) Failing after 18s

- New `network` panel module (in the default panel, before the battery and the clock): signal bars and the
  Wi-Fi network's name, a plug when wired, crossed-out bars offline or in airplane mode. Its
  popup shows the status and the last action's outcome ("Couldn't connect to X: wrong
  password?"), switches for Wi-Fi and airplane mode, and the networks in range (strongest
  access point per name, the one in use first). Click a saved or open network to connect, a new
  secured one to type its password in a field under it (Enter or Connect), the one in use to
  disconnect, or "Forget" on a saved one. Opening the popup asks for a scan.
- compositor/network: NetworkManager over D-Bus through a small D-Bus client of our own
  (wire format, EXTERNAL auth, async calls) on the existing @sys socket calls, so neither host
  needed new imports. One ObjectManager.GetManagedObjects + GetAll every 2 s; GetSettings once
  per saved connection; RequestScan, Device.Disconnect, WirelessEnabled / WwanEnabled,
  ActivateConnection (saved), AddAndActivateConnection (new: open, WPA/WPA2 PSK, WPA3 SAE) and
  Settings.Connection.Delete. Without NetworkManager it falls back to read-only status from
  /proc/net/dev, /proc/net/wireless and /sys/class/net (no network names there), and looks for
  NetworkManager again every 30 s.
- New `network` capability (wit/capabilities.wit): status, wifi-networks, scan, connect,
  disconnect, forget, set-wifi-enabled, set-airplane-mode. Served from host tables 8/9 and a
  `network(network-request)` request by the wasmer/wasmtime stub (compositor/wasm), host-wasm
  and the MoonBit wasmhost. The stub's build_list learned bool fields; request strings are
  wiped after use (Wi-Fi passwords).
- Running out of module instances now says "too many modules loaded".
- Deferred: VPN, hotspot, hidden networks, WEP and 802.1X (enterprise) networks, IP settings,
  mobile broadband beyond the airplane switch, D-Bus signals (state is polled).
- Tests: D-Bus wire format (incl. the well-known Hello bytes), NetworkManager objects -> status
  and list, security settings, /proc parsing; the module through the shell against a fake
  backend (list, connect/forget/disconnect, password entry, switches, read-only); a host-wasm
  test; and scripts/test-network-dbus.sh, which runs the backend against python-dbusmock's
  NetworkManager on a private bus.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hizqNXcNDuef3gxa7D
This commit is contained in:
Claude committed 2026-11-25 20:02:36 +00:00
1 parent 88915842c2
commit 1f4b686087
43 files changed
+4101 -32

No files matched your search

+93 -5
View File
@@ -57,13 +57,16 @@ enum {
K_APP_ICON,
K_SHOT_TAKE, K_SHOT_RECORD, K_SHOT_STOP, K_SHOT_IS_REC, K_SHOT_PTR, K_SHOT_SET_PTR,
K_PW_SCREEN, K_PW_SET_SCREEN, K_PW_SUSP, K_PW_SET_SUSP, K_PW_SUSPEND, K_PW_OFF, K_PW_REBOOT,
K_BAT_READ
K_BAT_READ,
K_NET_STATUS, K_NET_LIST, K_NET_SCAN, K_NET_CONNECT, K_NET_DISCONNECT, K_NET_FORGET, K_NET_SET_WIFI,
K_NET_SET_AIRPLANE
};
typedef struct Env { Inst *inst; int kind; } Env;
/* ---- host data the shell publishes before calling a guest ----
* A table is [u32 rows] then rows of fields, each field a LE i32 ('i') or a u32 byte length + UTF-8 ('s'). */
enum { T_APPS, T_WINDOWS, T_LAYOUTS, T_MODULES, T_INSTANCES, T_WALLPAPERS, T_SESSION_SETTINGS, T_ICONS, T_N };
enum { T_APPS, T_WINDOWS, T_LAYOUTS, T_MODULES, T_INSTANCES, T_WALLPAPERS, T_SESSION_SETTINGS, T_ICONS,
T_NET_STATUS, T_NET_NETWORKS, T_N };
typedef struct { uint8_t *data; size_t len; } Blob;
static Blob tables[T_N];
static int32_t host_ints[12] = {[7] = -1}; /* 0 layout mode, 1 inspector on, 2 current wallpaper, 3 session settings on (bit i: row i),
@@ -186,12 +189,64 @@ static char *guest_str(Inst *in, int32_t ptr, int32_t units) {
static uint32_t rd_u32(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return v; }
/* Lower table `t` (row layout `spec`: 'i' or 's' per field) as a list<record> and write (ptr,len) at `retptr`. */
/* Lower one row of table `t` (from byte `*pos`) as a record at guest address `off`: 'i' an i32,
* 's' a string, 'b' a bool (an i32 in the table, one byte in the record; bools go last). 0 when
* the table ends early. */
static int lower_row(Inst *in, Blob *b, size_t *pos, const char *spec, int32_t off) {
for (const char *c = spec; *c; c++) {
if (*pos + 4 > b->len) return 0;
uint32_t v = rd_u32(b->data + *pos);
*pos += 4;
if (*c == 'i') { w32(in, off, (int32_t)v); off += 4; }
else if (*c == 'b') { uint8_t one = v ? 1 : 0; mem_write(in, off, &one, 1); off += 1; }
else {
if (*pos + v > b->len) return 0;
put_str(in, off, b->data + *pos, v);
*pos += v;
off += 8;
}
}
return 1;
}
/* A record's size for `spec` ('s' 8, 'i' 4, 'b' 1), rounded up to its 4-byte alignment. */
static size_t record_size(const char *spec) {
size_t esz = 0;
for (const char *c = spec; *c; c++) esz += *c == 's' ? 8 : *c == 'b' ? 1 : 4;
return (esz + 3) & ~(size_t)3;
}
/* Lower row 0 of table `t` as a record at `retptr` (zeros when the table is empty). */
static void build_record(Inst *in, int t, const char *spec, int32_t retptr) {
Blob *b = &tables[t];
size_t pos = 4;
uint32_t n = b->len >= 4 ? rd_u32(b->data) : 0;
if (n == 0 || !lower_row(in, b, &pos, spec, retptr)) {
/* strings still need real (empty) allocations: the guest frees what it lifts */
int32_t off = retptr;
for (const char *c = spec; *c; c++) {
if (*c == 's') { put_str(in, off, (const uint8_t *)"", 0); off += 8; }
else if (*c == 'b') { uint8_t z = 0; mem_write(in, off, &z, 1); off += 1; }
else { w32(in, off, 0); off += 4; }
}
}
}
/* Lower table `t` (row layout `spec`, see lower_row) as a list<record> and write (ptr,len) at `retptr`. */
static void build_list(Inst *in, int t, const char *spec, int32_t retptr) {
Blob *b = &tables[t];
uint32_t n = b->len >= 4 ? rd_u32(b->data) : 0;
size_t esz = 0;
for (const char *c = spec; *c; c++) esz += *c == 's' ? 8 : 4;
size_t esz = record_size(spec);
if (strchr(spec, 'b')) {
int32_t base = guest_alloc(in, (int32_t)(esz * (n ? n : 1)), 4);
if (base < 0) { w32(in, retptr, 0); w32(in, retptr + 4, 0); return; }
size_t pos = 4;
uint32_t r = 0;
while (r < n && lower_row(in, b, &pos, spec, base + (int32_t)(r * esz))) r++;
w32(in, retptr, base);
w32(in, retptr + 4, (int32_t)r);
return;
}
int32_t base = guest_alloc(in, (int32_t)(esz * (n ? n : 1)), 4);
if (base < 0) { w32(in, retptr, 0); w32(in, retptr + 4, 0); return; }
size_t pos = 4;
@@ -334,6 +389,30 @@ static wasm_trap_t *host_import(void *env, const wasm_val_vec_t *args, wasm_val_
case K_PW_SUSPEND: push_req(15, 0, NULL); break; /* 15: power action n (0 suspend, 1 off, 2 restart) */
case K_PW_OFF: push_req(15, 1, NULL); break;
case K_PW_REBOOT: push_req(15, 2, NULL); break;
/* Network: status and the Wi-Fi list from tables 8/9; request 16 carries the action (n) and
* "ssid\x1fpassword" (see wit/host/plasmoids.wit network-request). */
case K_NET_STATUS: build_record(in, T_NET_STATUS, "iissbbbb", a0); break;
case K_NET_LIST: build_list(in, T_NET_NETWORKS, "sibbb", a0); break;
case K_NET_SCAN: push_req(16, 0, NULL); break;
case K_NET_DISCONNECT: push_req(16, 1, NULL); break;
case K_NET_SET_WIFI: push_req(16, a0 ? 3 : 2, NULL); break;
case K_NET_SET_AIRPLANE: push_req(16, a0 ? 5 : 4, NULL); break;
case K_NET_CONNECT:
case K_NET_FORGET: {
int32_t a3 = args->size >= 4 ? args->data[3].of.i32 : 0;
char *ssid = guest_str(in, a0, a1);
char *pw = e->kind == K_NET_CONNECT ? guest_str(in, a2, a3) : strdup("");
size_t ls = strlen(ssid), lp = strlen(pw);
char *both = (char *)malloc(ls + lp + 2);
memcpy(both, ssid, ls);
both[ls] = 0x1f;
memcpy(both + ls + 1, pw, lp + 1);
push_req(16, e->kind == K_NET_CONNECT ? 6 : 7, both);
memset(pw, 0, lp); /* don't leave the password lying around in freed memory */
memset(both, 0, ls + lp + 1);
free(ssid); free(pw); free(both);
break;
}
case K_SHOT_IS_REC:
if (results->size >= 1) results->data[0] = (wasm_val_t)WASM_I32_VAL(host_ints[4] & 1);
break;
@@ -507,6 +586,14 @@ static int classify_import(const wasm_name_t *m, const wasm_name_t *f) {
if (!strcmp(iface, "power") && !strcmp(fn, "suspend")) return K_PW_SUSPEND;
if (!strcmp(iface, "power") && !strcmp(fn, "power-off")) return K_PW_OFF;
if (!strcmp(iface, "power") && !strcmp(fn, "reboot")) return K_PW_REBOOT;
if (!strcmp(iface, "network") && !strcmp(fn, "status")) return K_NET_STATUS;
if (!strcmp(iface, "network") && !strcmp(fn, "wifi-networks")) return K_NET_LIST;
if (!strcmp(iface, "network") && !strcmp(fn, "scan")) return K_NET_SCAN;
if (!strcmp(iface, "network") && !strcmp(fn, "connect")) return K_NET_CONNECT;
if (!strcmp(iface, "network") && !strcmp(fn, "disconnect")) return K_NET_DISCONNECT;
if (!strcmp(iface, "network") && !strcmp(fn, "forget")) return K_NET_FORGET;
if (!strcmp(iface, "network") && !strcmp(fn, "set-wifi-enabled")) return K_NET_SET_WIFI;
if (!strcmp(iface, "network") && !strcmp(fn, "set-airplane-mode")) return K_NET_SET_AIRPLANE;
if (!strcmp(iface, "layout-control") && !strcmp(fn, "layout-modes")) return K_LAYOUTS;
if (!strcmp(iface, "layout-control") && !strcmp(fn, "current-layout")) return K_LAYOUT_CUR;
if (!strcmp(iface, "layout-control") && !strcmp(fn, "set-layout")) return K_SET_LAYOUT;
@@ -772,6 +859,7 @@ void mp_host_set_int(int32_t which, int32_t v) {
/* Pop the next guest request: returns its kind (0 = none). Fetch its arguments with req_int / req_str. */
MOONBIT_FFI_EXPORT
int32_t mp_host_req_pop(void) {
if (cur_req.s) memset(cur_req.s, 0, strlen(cur_req.s)); /* may hold a Wi-Fi password */
free(cur_req.s);
memset(&cur_req, 0, sizeof(cur_req));
if (req_head >= req_tail) return 0;