///| 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) }