Network module: Wi-Fi and wired status, connect to Wi-Fi networks, Wi-Fi and airplane switches
CI / test (push) Failing after 18s
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:
43 files changed
+4101
-32
No files matched your search
@@ -256,6 +256,9 @@ impl chandra::host::plasmoids::Host for State {
|
||||
Req::SetScreenOff(m) => R::SetScreenOff(m),
|
||||
Req::SetSuspendAfter(m) => R::SetSuspendAfter(m),
|
||||
Req::PowerAction(a) => R::PowerAction(a),
|
||||
Req::Network(action, ssid, password) => {
|
||||
R::Network(chandra::host::plasmoids::NetworkRequest { action, ssid, password })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+143
-2
@@ -52,6 +52,9 @@ pub enum Req {
|
||||
SetSuspendAfter(i32),
|
||||
/// 0 suspend, 1 power off, 2 restart.
|
||||
PowerAction(i32),
|
||||
/// A network action (0 scan, 1 disconnect, 2/3 Wi-Fi off/on, 4/5 airplane mode off/on,
|
||||
/// 6 connect, 7 forget), the network's name and the password.
|
||||
Network(i32, String, String),
|
||||
}
|
||||
|
||||
pub const T_APPS: usize = 0;
|
||||
@@ -63,11 +66,15 @@ pub const T_WALLPAPERS: usize = 5;
|
||||
pub const T_SESSION_SETTINGS: usize = 6;
|
||||
/// App id -> raster image handle (`raster.load-image`), behind `icons.app-icon`.
|
||||
pub const T_ICONS: usize = 7;
|
||||
/// One row (iissiiii): state signal name message wifi-enabled airplane can-manage has-wifi.
|
||||
pub const T_NET_STATUS: usize = 8;
|
||||
/// Wi-Fi networks in range (siiii): ssid signal secured active saved.
|
||||
pub const T_NET_NETWORKS: usize = 9;
|
||||
|
||||
/// Host data every module reads from, shared by all loaded modules.
|
||||
#[derive(Default)]
|
||||
pub struct Shared {
|
||||
pub tables: [Vec<u8>; 8],
|
||||
pub tables: [Vec<u8>; 10],
|
||||
pub layout_mode: i32,
|
||||
pub wallpaper: i32,
|
||||
/// Which session settings are on: bit i for row i of the session settings table.
|
||||
@@ -365,6 +372,71 @@ impl d::screenshot::Host for PState {
|
||||
}
|
||||
}
|
||||
|
||||
impl d::network::Host for PState {
|
||||
fn status(&mut self) -> d::network::NetStatus {
|
||||
let rows = self.rows(T_NET_STATUS, "iissiiii");
|
||||
match rows.first() {
|
||||
Some(r) if r.len() == 8 => d::network::NetStatus {
|
||||
state: i(&r[0]) as u32,
|
||||
signal: i(&r[1]) as u32,
|
||||
name: s(&r[2]),
|
||||
message: s(&r[3]),
|
||||
wifi_enabled: i(&r[4]) != 0,
|
||||
airplane: i(&r[5]) != 0,
|
||||
can_manage: i(&r[6]) != 0,
|
||||
has_wifi: i(&r[7]) != 0,
|
||||
},
|
||||
_ => d::network::NetStatus {
|
||||
state: 0,
|
||||
signal: 0,
|
||||
name: String::new(),
|
||||
message: String::new(),
|
||||
wifi_enabled: false,
|
||||
airplane: false,
|
||||
can_manage: false,
|
||||
has_wifi: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
fn wifi_networks(&mut self) -> Vec<d::network::WifiNetwork> {
|
||||
self.rows(T_NET_NETWORKS, "siiii")
|
||||
.iter()
|
||||
.filter(|r| r.len() == 5)
|
||||
.map(|r| d::network::WifiNetwork {
|
||||
ssid: s(&r[0]),
|
||||
signal: i(&r[1]) as u32,
|
||||
secured: i(&r[2]) != 0,
|
||||
active: i(&r[3]) != 0,
|
||||
saved: i(&r[4]) != 0,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
fn scan(&mut self) {
|
||||
self.net(0, String::new(), String::new());
|
||||
}
|
||||
fn connect(&mut self, ssid: String, password: String) {
|
||||
self.net(6, ssid, password);
|
||||
}
|
||||
fn disconnect(&mut self) {
|
||||
self.net(1, String::new(), String::new());
|
||||
}
|
||||
fn forget(&mut self, ssid: String) {
|
||||
self.net(7, ssid, String::new());
|
||||
}
|
||||
fn set_wifi_enabled(&mut self, on: bool) {
|
||||
self.net(if on { 3 } else { 2 }, String::new(), String::new());
|
||||
}
|
||||
fn set_airplane_mode(&mut self, on: bool) {
|
||||
self.net(if on { 5 } else { 4 }, String::new(), String::new());
|
||||
}
|
||||
}
|
||||
|
||||
impl PState {
|
||||
fn net(&mut self, action: i32, ssid: String, password: String) {
|
||||
self.shared.borrow_mut().requests.push_back(Req::Network(action, ssid, password));
|
||||
}
|
||||
}
|
||||
|
||||
impl d::session_settings::Host for PState {
|
||||
fn settings(&mut self) -> Vec<String> {
|
||||
self.rows(T_SESSION_SETTINGS, "s").iter().map(|r| s(&r[0])).collect()
|
||||
@@ -901,4 +973,73 @@ mod tests {
|
||||
assert!(rt.popup(h).is_none());
|
||||
rt.unload(h);
|
||||
}
|
||||
}
|
||||
/// Table bytes from rows of fields ('i' a number, 's' a string).
|
||||
enum F<'a> {
|
||||
I(i32),
|
||||
S(&'a str),
|
||||
}
|
||||
fn rows_table(rows: &[Vec<F>]) -> Vec<u8> {
|
||||
let mut t = (rows.len() as u32).to_le_bytes().to_vec();
|
||||
for r in rows {
|
||||
for f in r {
|
||||
match f {
|
||||
F::I(v) => t.extend(v.to_le_bytes()),
|
||||
F::S(s) => {
|
||||
t.extend((s.len() as u32).to_le_bytes());
|
||||
t.extend(s.as_bytes());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
t
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn network_module_shows_the_connection_and_connects_with_a_password() {
|
||||
let mut rt = runtime();
|
||||
let l = rt.load(&module("network")).expect("network loads");
|
||||
assert!(l.imports.contains(&"network".to_string()));
|
||||
let h = l.handle;
|
||||
{
|
||||
let mut sh = rt.shared.borrow_mut();
|
||||
// Wi-Fi "Home" at 72%, NetworkManager there, Wi-Fi on.
|
||||
sh.tables[T_NET_STATUS] = rows_table(&[vec![
|
||||
F::I(2),
|
||||
F::I(72),
|
||||
F::S("Home"),
|
||||
F::S(""),
|
||||
F::I(1),
|
||||
F::I(0),
|
||||
F::I(1),
|
||||
F::I(1),
|
||||
]]);
|
||||
sh.tables[T_NET_NETWORKS] = rows_table(&[
|
||||
vec![F::S("Home"), F::I(72), F::I(1), F::I(1), F::I(1)],
|
||||
vec![F::S("Office"), F::I(55), F::I(1), F::I(0), F::I(0)],
|
||||
]);
|
||||
}
|
||||
assert!(texts(&rt.render(h, Size { w: 130, h: 34 })).contains(&"Home".to_string()));
|
||||
// Opening the popup asks for a scan.
|
||||
assert!(rt.on_event(h, false, InputEvent::PointerDown(Point { x: 5, y: 5 })));
|
||||
assert!(matches!(rt.shared.borrow_mut().requests.pop_front(), Some(Req::Network(0, _, _))));
|
||||
let p = rt.popup(h).expect("popup open");
|
||||
for n in ["Connected to Home (72%)", "Wi-Fi", "Airplane mode", "Office", "Secured"] {
|
||||
assert!(texts(&p.ops).contains(&n.to_string()), "{n} listed");
|
||||
}
|
||||
// Row 5 (Office) opens the password field; typing and Enter connects.
|
||||
let row = |i: i32| Point { x: 60, y: 6 + i * 30 + 14 };
|
||||
assert!(rt.on_event(h, true, InputEvent::PointerDown(row(5))));
|
||||
assert!(texts(&rt.popup(h).unwrap().ops).contains(&"Password|".to_string()));
|
||||
for k in ["p", "w", "Enter"] {
|
||||
rt.on_event(h, true, InputEvent::KeyDown(k.to_string()));
|
||||
}
|
||||
match rt.shared.borrow_mut().requests.pop_front() {
|
||||
Some(Req::Network(6, ssid, pw)) => assert_eq!((ssid.as_str(), pw.as_str()), ("Office", "pw")),
|
||||
_ => panic!("connect asked for"),
|
||||
}
|
||||
// The Wi-Fi switch (row 1).
|
||||
assert!(rt.on_event(h, true, InputEvent::PointerDown(row(1))));
|
||||
assert!(matches!(rt.shared.borrow_mut().requests.pop_front(), Some(Req::Network(2, _, _))));
|
||||
rt.unload(h);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user