mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-04-19 19:34:24 +02:00
feat(networkmanager): implement WiFi strenght levels
Also collect SSID information. Althought nothing is being done with it for now. This was done by interfacing with `NetworkManager.Device.Wifi` and `NetworkManager.AccessPoint` dbus interfaces.
This commit is contained in:
parent
e1945d1e93
commit
4fe03031dd
4 changed files with 100 additions and 10 deletions
|
@ -69,6 +69,28 @@ trait DeviceDbus {
|
||||||
fn state(&self) -> Result<DeviceState>;
|
fn state(&self) -> Result<DeviceState>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[dbus_proxy(
|
||||||
|
default_service = "org.freedesktop.NetworkManager",
|
||||||
|
interface = "org.freedesktop.NetworkManager.Device.Wireless"
|
||||||
|
)]
|
||||||
|
trait DeviceWirelessDbus {
|
||||||
|
#[dbus_proxy(property)]
|
||||||
|
fn active_access_point(&self) -> zbus::Result<zbus::zvariant::OwnedObjectPath>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// based on code generated by `zbus-xmlgen system org.freedesktop.NetworkManager /org/freedesktop/NetworkManager/AccessPoint/1`
|
||||||
|
#[dbus_proxy(
|
||||||
|
default_service = "org.freedesktop.NetworkManager",
|
||||||
|
interface = "org.freedesktop.NetworkManager.AccessPoint"
|
||||||
|
)]
|
||||||
|
trait AccessPointDbus {
|
||||||
|
#[dbus_proxy(property)]
|
||||||
|
fn ssid(&self) -> zbus::Result<Vec<u8>>;
|
||||||
|
|
||||||
|
#[dbus_proxy(property)]
|
||||||
|
fn strength(&self) -> Result<u8>;
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, OwnedValue, PartialEq)]
|
#[derive(Clone, Debug, OwnedValue, PartialEq)]
|
||||||
#[repr(u32)]
|
#[repr(u32)]
|
||||||
pub(super) enum DeviceType {
|
pub(super) enum DeviceType {
|
||||||
|
|
|
@ -64,7 +64,10 @@ impl Client {
|
||||||
($client:ident) => {
|
($client:ident) => {
|
||||||
$client.state.set(State {
|
$client.state.set(State {
|
||||||
wired: determine_wired_state(&read_lock!($client.devices))?,
|
wired: determine_wired_state(&read_lock!($client.devices))?,
|
||||||
wifi: determine_wifi_state(&read_lock!($client.devices))?,
|
wifi: determine_wifi_state(
|
||||||
|
&$client.dbus_connection,
|
||||||
|
&read_lock!($client.devices),
|
||||||
|
)?,
|
||||||
cellular: determine_cellular_state(&read_lock!($client.devices))?,
|
cellular: determine_cellular_state(&read_lock!($client.devices))?,
|
||||||
vpn: $client.state.get_cloned().vpn,
|
vpn: $client.state.get_cloned().vpn,
|
||||||
});
|
});
|
||||||
|
@ -178,7 +181,7 @@ impl Client {
|
||||||
});
|
});
|
||||||
self.0.state.set(State {
|
self.0.state.set(State {
|
||||||
wired: determine_wired_state(&read_lock!(self.0.devices))?,
|
wired: determine_wired_state(&read_lock!(self.0.devices))?,
|
||||||
wifi: determine_wifi_state(&read_lock!(self.0.devices))?,
|
wifi: determine_wifi_state(&self.0.dbus_connection, &read_lock!(self.0.devices))?,
|
||||||
cellular: determine_cellular_state(&read_lock!(self.0.devices))?,
|
cellular: determine_cellular_state(&read_lock!(self.0.devices))?,
|
||||||
vpn: determine_vpn_state(&read_lock!(self.0.active_connections))?,
|
vpn: determine_vpn_state(&read_lock!(self.0.active_connections))?,
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
|
|
||||||
use crate::clients::networkmanager::dbus::{
|
use crate::clients::networkmanager::dbus::{
|
||||||
ActiveConnectionDbusProxyBlocking, DeviceDbusProxyBlocking, DeviceState, DeviceType,
|
AccessPointDbusProxyBlocking, ActiveConnectionDbusProxyBlocking, DeviceDbusProxyBlocking,
|
||||||
|
DeviceState, DeviceType, DeviceWirelessDbusProxyBlocking,
|
||||||
};
|
};
|
||||||
use crate::clients::networkmanager::PathMap;
|
use crate::clients::networkmanager::PathMap;
|
||||||
|
|
||||||
|
@ -33,6 +34,8 @@ pub enum WifiState {
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct WifiConnectedState {
|
pub struct WifiConnectedState {
|
||||||
pub ssid: String,
|
pub ssid: String,
|
||||||
|
/// Strength in percentage, from 0 to 100.
|
||||||
|
pub strength: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
@ -82,29 +85,43 @@ pub(super) fn determine_wired_state(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn determine_wifi_state(
|
pub(super) fn determine_wifi_state(
|
||||||
|
dbus_connection: &zbus::blocking::Connection,
|
||||||
devices: &PathMap<DeviceDbusProxyBlocking>,
|
devices: &PathMap<DeviceDbusProxyBlocking>,
|
||||||
) -> Result<WifiState> {
|
) -> Result<WifiState> {
|
||||||
let mut present = false;
|
let mut present = false;
|
||||||
let mut enabled = false;
|
let mut enabled = false;
|
||||||
let mut connected = false;
|
let mut connected = None;
|
||||||
|
|
||||||
for device in devices.values() {
|
for device in devices.values() {
|
||||||
if device.device_type()? == DeviceType::Wifi {
|
if device.device_type()? == DeviceType::Wifi {
|
||||||
present = true;
|
present = true;
|
||||||
if device.state()?.is_enabled() {
|
if device.state()?.is_enabled() {
|
||||||
enabled = true;
|
enabled = true;
|
||||||
if device.state()? == DeviceState::Activated {
|
|
||||||
connected = true;
|
let wireless_device = DeviceWirelessDbusProxyBlocking::builder(dbus_connection)
|
||||||
|
.path(device.path().clone())?
|
||||||
|
.build()?;
|
||||||
|
let primary_access_point_path = wireless_device.active_access_point()?;
|
||||||
|
if primary_access_point_path.as_str() != "/" {
|
||||||
|
connected = Some(
|
||||||
|
AccessPointDbusProxyBlocking::builder(dbus_connection)
|
||||||
|
.path(primary_access_point_path)?
|
||||||
|
.build()?,
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if connected {
|
if let Some(access_point) = connected {
|
||||||
|
let ssid = access_point
|
||||||
|
.ssid()
|
||||||
|
.map(|x| String::from_utf8_lossy(&x).to_string())
|
||||||
|
.unwrap_or_else(|_| "unkown".into());
|
||||||
Ok(WifiState::Connected(WifiConnectedState {
|
Ok(WifiState::Connected(WifiConnectedState {
|
||||||
// TODO: Implement obtaining SSID
|
ssid,
|
||||||
ssid: "unknown".into(),
|
strength: access_point.strength().unwrap_or(0),
|
||||||
}))
|
}))
|
||||||
} else if enabled {
|
} else if enabled {
|
||||||
Ok(WifiState::Disconnected)
|
Ok(WifiState::Disconnected)
|
||||||
|
|
|
@ -111,7 +111,17 @@ impl Module<GtkBox> for NetworkManagerModule {
|
||||||
WiredState::NotPresent | WiredState::Unknown => "",
|
WiredState::NotPresent | WiredState::Unknown => "",
|
||||||
});
|
});
|
||||||
update_icon!(wifi_icon, wifi, {
|
update_icon!(wifi_icon, wifi, {
|
||||||
WifiState::Connected(_) => "icon:network-wireless-connected-symbolic",
|
WifiState::Connected(state) => {
|
||||||
|
let icons = [
|
||||||
|
"icon:network-wireless-signal-none-symbolic",
|
||||||
|
"icon:network-wireless-signal-weak-symbolic",
|
||||||
|
"icon:network-wireless-signal-ok-symbolic",
|
||||||
|
"icon:network-wireless-signal-good-symbolic",
|
||||||
|
"icon:network-wireless-signal-excellent-symbolic",
|
||||||
|
];
|
||||||
|
let n = strengh_to_level(state.strength, icons.len());
|
||||||
|
icons[n]
|
||||||
|
},
|
||||||
WifiState::Disconnected => "icon:network-wireless-offline-symbolic",
|
WifiState::Disconnected => "icon:network-wireless-offline-symbolic",
|
||||||
WifiState::Disabled => "icon:network-wireless-hardware-disabled-symbolic",
|
WifiState::Disabled => "icon:network-wireless-hardware-disabled-symbolic",
|
||||||
WifiState::NotPresent | WifiState::Unknown => "",
|
WifiState::NotPresent | WifiState::Unknown => "",
|
||||||
|
@ -133,3 +143,41 @@ impl Module<GtkBox> for NetworkManagerModule {
|
||||||
|
|
||||||
module_impl!("networkmanager");
|
module_impl!("networkmanager");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convert strength level (from 0-100), to a level (from 0 to `number_of_levels-1`).
|
||||||
|
const fn strengh_to_level(strength: u8, number_of_levels: usize) -> usize {
|
||||||
|
// Strength levels based for the one show by [`nmcli dev wifi list`](https://github.com/NetworkManager/NetworkManager/blob/83a259597000a88217f3ccbdfe71c8114242e7a6/src/libnmc-base/nm-client-utils.c#L700-L727):
|
||||||
|
// match strength {
|
||||||
|
// 0..=4 => 0,
|
||||||
|
// 5..=29 => 1,
|
||||||
|
// 30..=54 => 2,
|
||||||
|
// 55..=79 => 3,
|
||||||
|
// 80.. => 4,
|
||||||
|
// }
|
||||||
|
|
||||||
|
// to make it work with a custom number of levels, we approach the logic above with the logic
|
||||||
|
// below (0 for < 5, and a linear interpolation for 5 to 105).
|
||||||
|
// TODO: if there are more than 20 levels, the last level will be out of scale, and never be
|
||||||
|
// reach.
|
||||||
|
if strength < 5 {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
(strength as usize - 5) * (number_of_levels - 1) / 100 + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Just to make sure my implementation still follow the original logic
|
||||||
|
#[cfg(test)]
|
||||||
|
#[test]
|
||||||
|
fn test_strength_to_level() {
|
||||||
|
assert_eq!(strengh_to_level(0, 5), 0);
|
||||||
|
assert_eq!(strengh_to_level(4, 5), 0);
|
||||||
|
assert_eq!(strengh_to_level(5, 5), 1);
|
||||||
|
assert_eq!(strengh_to_level(6, 5), 1);
|
||||||
|
assert_eq!(strengh_to_level(29, 5), 1);
|
||||||
|
assert_eq!(strengh_to_level(30, 5), 2);
|
||||||
|
assert_eq!(strengh_to_level(54, 5), 2);
|
||||||
|
assert_eq!(strengh_to_level(55, 5), 3);
|
||||||
|
assert_eq!(strengh_to_level(79, 5), 3);
|
||||||
|
assert_eq!(strengh_to_level(80, 5), 4);
|
||||||
|
assert_eq!(strengh_to_level(100, 5), 4);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue