Files
chandra/compositor/network/model.mbt
Claude 1f4b686087
CI / test (push) Failing after 18s
Network module: Wi-Fi and wired status, connect to Wi-Fi networks, Wi-Fi and airplane switches
- 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
2026-11-25 20:02:36 +00:00

189 lines
5.1 KiB
MoonBit

///| Network status and control for the `network` capability (the network panel module). The shell
/// owns a `NetService`; it picks a backend: NetworkManager over D-Bus (nm.mbt) when it answers,
/// else read-only status from /sys/class/net and /proc/net (sysfs.mbt), trying NetworkManager
/// again now and then. Tests plug in their own backend.
///| How the machine is connected now.
pub(all) enum NetState {
Offline
Wired
Wifi
Connecting
} derive(Eq, Debug)
///| The number the capability uses: 0 offline, 1 wired, 2 Wi-Fi, 3 connecting.
pub fn NetState::code(self : NetState) -> Int {
match self {
Offline => 0
Wired => 1
Wifi => 2
Connecting => 3
}
}
///| What the panel shows.
pub(all) struct NetStatus {
state : NetState
/// The Wi-Fi network's name, else the connection's (e.g. "Wired connection 1"); "" offline.
name : String
/// Signal of the Wi-Fi network in use, 0..100 (0 when not on Wi-Fi).
signal : Int
/// Something to tell the user: an action's outcome or why it failed ("" for nothing).
message : String
wifi_enabled : Bool
airplane : Bool
/// The network can be changed from here (NetworkManager answers); false: status only.
managed : Bool
/// There is a Wi-Fi device.
has_wifi : Bool
} derive(Eq, Debug)
///|
pub fn NetStatus::offline() -> NetStatus {
{
state: Offline,
name: "",
signal: 0,
message: "",
wifi_enabled: false,
airplane: false,
managed: false,
has_wifi: false,
}
}
///| A Wi-Fi network in range.
pub(all) struct WifiNetwork {
ssid : String
signal : Int
secured : Bool
/// The one in use.
active : Bool
/// There is a saved connection for it (no password needed).
saved : Bool
} derive(Eq, Debug)
///| What the module can ask for.
pub(all) enum NetAction {
Scan
Disconnect
SetWifi(Bool)
SetAirplane(Bool)
/// Connect to the network named `ssid`: a saved one as saved, a new one with `password`
/// ("" for an open network).
Connect(String, String)
/// Delete the saved connections for a network.
Forget(String)
} derive(Eq, Debug)
///| The capability's action codes (wit/host/plasmoids.wit `network-request`): 0 scan,
/// 1 disconnect, 2/3 Wi-Fi off/on, 4/5 airplane mode off/on, 6 connect, 7 forget.
pub fn NetAction::from_code(code : Int, ssid : String, password : String) -> NetAction? {
match code {
0 => Some(Scan)
1 => Some(Disconnect)
2 => Some(SetWifi(false))
3 => Some(SetWifi(true))
4 => Some(SetAirplane(false))
5 => Some(SetAirplane(true))
6 => if ssid == "" { None } else { Some(Connect(ssid, password)) }
7 => if ssid == "" { None } else { Some(Forget(ssid)) }
_ => None
}
}
///| Where status comes from and where actions go.
pub(open) trait NetBackend {
/// Called every frame with monotonic ms: read replies, refresh when due.
tick(Self, UInt64) -> Unit
status(Self) -> NetStatus
networks(Self) -> Array[WifiNetwork]
act(Self, NetAction) -> Unit
/// False once the backend stopped working (NetworkManager went away).
alive(Self) -> Bool
/// Let go of what it holds (a bus connection).
close(Self) -> Unit = _
}
///|
impl NetBackend with close(_self) {
()
}
///| The network service the shell holds.
pub struct NetService {
mut backend : &NetBackend
mut on_nm : Bool
mut retry_at : UInt64
/// Look for NetworkManager (false with a test backend).
probe : Bool
}
///| How often to look for NetworkManager again while it isn't there.
let nm_retry_ms : UInt64 = 30000UL
///| NetworkManager if it answers, else /sys status.
pub fn NetService::new() -> NetService {
match NmBackend::connect() {
Some(nm) => { backend: nm as &NetBackend, on_nm: true, retry_at: 0UL, probe: true }
None => {
backend: SysfsBackend::new() as &NetBackend,
on_nm: false,
retry_at: @sys.monotonic_ms() + nm_retry_ms,
probe: true,
}
}
}
///| A service over `b` (tests).
pub fn NetService::with_backend(b : &NetBackend) -> NetService {
{ backend: b, on_nm: false, retry_at: 0UL, probe: false }
}
///|
pub fn NetService::tick(self : NetService, now : UInt64) -> Unit {
self.backend.tick(now)
if self.on_nm && !self.backend.alive() {
self.backend.close()
self.backend = SysfsBackend::new() as &NetBackend
self.on_nm = false
self.retry_at = now + nm_retry_ms
self.backend.tick(now)
} else if !self.on_nm && self.probe && now >= self.retry_at {
self.retry_at = now + nm_retry_ms
match NmBackend::connect() {
Some(nm) => {
self.backend = nm as &NetBackend
self.on_nm = true
self.backend.tick(now)
}
None => ()
}
}
}
///| Whether this service looks for NetworkManager (false with a test backend).
pub fn NetService::probe(self : NetService) -> Bool {
self.probe
}
///| Let go of the bus connection.
pub fn NetService::close(self : NetService) -> Unit {
self.backend.close()
}
///|
pub fn NetService::status(self : NetService) -> NetStatus {
self.backend.status()
}
///|
pub fn NetService::networks(self : NetService) -> Array[WifiNetwork] {
self.backend.networks()
}
///|
pub fn NetService::act(self : NetService, a : NetAction) -> Unit {
self.backend.act(a)
}