Files
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

147 lines
4.0 KiB
MoonBit

///| Read-only status without NetworkManager: interfaces from /proc/net/dev, whether each is a
/// physical Ethernet or Wi-Fi device and up from /sys/class/net/<if>, and Wi-Fi link quality from
/// /proc/net/wireless. The network's name isn't there (that needs nl80211), so the interface's
/// name stands in. Nothing can be changed.
///| One interface's state, as read.
pub(all) struct IfState {
name : String
wireless : Bool
physical : Bool
up : Bool
/// Link quality 0..100 (Wi-Fi), -1 unknown.
quality : Int
} derive(Eq, Debug)
///|
fn read_text(path : String) -> String {
@utf8.decode_lossy(@sys.read_proc_bytes(path)).trim(chars=" \n\t").to_string()
}
///| Interface names in /proc/net/dev.
pub fn proc_net_dev_names(text : String) -> Array[String] {
let out : Array[String] = []
for line in text.split("\n") {
match line.find(":") {
Some(i) => {
let name = line[:i].trim(chars=" ").to_string()
if name != "" && !name.contains("|") {
out.push(name)
}
}
None => ()
}
}
out
}
///| Link quality per interface from /proc/net/wireless ("wlan0: 0000 54. -56. -256 ..."),
/// scaled from the usual 0..70 to 0..100.
pub fn proc_net_wireless(text : String) -> Map[String, Int] {
let out : Map[String, Int] = {}
for line in text.split("\n") {
match line.find(":") {
Some(i) => {
let name = line[:i].trim(chars=" ").to_string()
let rest = line[i + 1:].to_string()
let fs = rest.split(" ").filter(f => f != "").map(f => f.to_string()).collect()
if name != "" && fs.length() >= 2 && !name.contains("|") {
let mut q = 0
for c in fs[1] {
if c >= '0' && c <= '9' {
q = q * 10 + (c.to_int() - 48)
} else {
break
}
}
out.set(name, if q >= 70 { 100 } else { q * 100 / 70 })
}
}
None => ()
}
}
out
}
///| What the interfaces say about the connection: Ethernet up wins, then Wi-Fi up.
pub fn sysfs_status(ifs : Array[IfState]) -> NetStatus {
let has_wifi = ifs.iter().any(i => i.wireless)
let base = { ..NetStatus::offline(), has_wifi, wifi_enabled: has_wifi }
for i in ifs {
if i.up && i.physical && !i.wireless {
return { ..base, state: Wired, name: i.name }
}
}
for i in ifs {
if i.up && i.wireless {
return { ..base, state: Wifi, name: i.name, signal: if i.quality < 0 { 0 } else { i.quality } }
}
}
base
}
///| The read-only backend.
pub struct SysfsBackend {
mut status : NetStatus
mut next : UInt64
}
///|
pub fn SysfsBackend::new() -> SysfsBackend {
{ status: NetStatus::offline(), next: 0UL }
}
///| Read the interfaces now.
pub fn read_interfaces() -> Array[IfState] {
let quality = proc_net_wireless(read_text("/proc/net/wireless"))
let out : Array[IfState] = []
for name in proc_net_dev_names(read_text("/proc/net/dev")) {
if name == "lo" {
continue
}
let base = "/sys/class/net/" + name
let wireless = quality.contains(name) || read_text(base + "/phy80211/name") != ""
// Physical devices have a device link (veth, bridges, docker0, tun don't).
let physical = read_text(base + "/device/uevent") != ""
let oper = read_text(base + "/operstate")
let carrier = read_text(base + "/carrier")
out.push({
name,
wireless,
physical,
up: oper == "up" || (oper == "unknown" && carrier == "1"),
quality: quality.get(name).unwrap_or(-1),
})
}
out
}
///|
pub impl NetBackend for SysfsBackend with tick(self, now) {
if now >= self.next {
self.next = now + 3000UL
let s = sysfs_status(read_interfaces())
self.status = { ..s, message: "NetworkManager isn't running: status only" }
}
}
///|
pub impl NetBackend for SysfsBackend with status(self) {
self.status
}
///|
pub impl NetBackend for SysfsBackend with networks(_self) {
[]
}
///|
pub impl NetBackend for SysfsBackend with act(_self, _a) {
()
}
///|
pub impl NetBackend for SysfsBackend with alive(_self) {
true
}