Struct NetworkManager

Source
pub struct NetworkManager { /* private fields */ }

Implementations§

Source§

impl NetworkManager

Source

pub fn new() -> Self

Examples found in repository?
examples/hotspot.rs (line 64)
52fn run() -> Result<()> {
53    let Options {
54        interface,
55        ssid,
56        password,
57    } = parse_options();
58
59    let pass_str = match password {
60        Some(ref s) => Some(s as &str),
61        None => None,
62    };
63
64    let manager = NetworkManager::new();
65
66    let device = find_device(&manager, interface)?;
67    let wifi_device = device.as_wifi_device().unwrap();
68
69    wifi_device.create_hotspot(&ssid as &str, pass_str, None)?;
70
71    Ok(())
72}
More examples
Hide additional examples
examples/create.rs (line 54)
46fn run() -> Result<()> {
47    let args: Vec<String> = env::args().collect();
48
49    if args.len() != 3 {
50        println!("USAGE: create SSID PASSWORD");
51        process::exit(1);
52    }
53
54    let manager = NetworkManager::new();
55
56    let device = find_device(&manager)?;
57
58    let wifi_device = device.as_wifi_device().unwrap();
59
60    let access_points = wifi_device.get_access_points()?;
61
62    let ap_index = find_access_point(&access_points, &args[1] as &str)?;
63
64    let credentials = AccessPointCredentials::Wpa {
65        passphrase: args[2].clone(),
66    };
67
68    wifi_device.connect(&access_points[ap_index], &credentials)?;
69
70    Ok(())
71}
Source

pub fn with_method_timeout(timeout: u64) -> Self

Source

pub fn start_service(timeout: u64) -> Result<ServiceState>

Starts the Network Manager service.

§Examples
use network_manager::NetworkManager;
let state = NetworkManager::start_service(10).unwrap();
println!("{:?}", state);
Source

pub fn stop_service(timeout: u64) -> Result<ServiceState>

Stops the Network Manager service.

§Examples
use network_manager::NetworkManager;
let state = NetworkManager::stop_service(10).unwrap();
println!("{:?}", state);
Source

pub fn get_service_state() -> Result<ServiceState>

Gets the state of the Network Manager service.

§Examples
use network_manager::NetworkManager;
let state = NetworkManager::get_service_state().unwrap();
println!("{:?}", state);
Source

pub fn get_connections(&self) -> Result<Vec<Connection>>

Get a list of Network Manager connections sorted by path.

§Examples
use network_manager::NetworkManager;
let manager = NetworkManager::new();
let connections = manager.get_connections().unwrap();
println!("{:?}", connections);
Source

pub fn get_active_connections(&self) -> Result<Vec<Connection>>

Source

pub fn get_devices(&self) -> Result<Vec<Device>>

Get a list of Network Manager devices.

§Examples
use network_manager::NetworkManager;
let manager = NetworkManager::new();
let devices = manager.get_devices().unwrap();
println!("{:?}", devices);
Examples found in repository?
examples/create.rs (line 74)
73fn find_device(manager: &NetworkManager) -> Result<Device> {
74    let devices = manager.get_devices()?;
75
76    let index = devices
77        .iter()
78        .position(|d| *d.device_type() == DeviceType::WiFi);
79
80    if let Some(index) = index {
81        Ok(devices[index].clone())
82    } else {
83        bail!(ErrorKind::Runtime("Cannot find a WiFi device".into()))
84    }
85}
More examples
Hide additional examples
examples/hotspot.rs (line 124)
111fn find_device(manager: &NetworkManager, interface: Option<String>) -> Result<Device> {
112    if let Some(interface) = interface {
113        let device = manager.get_device_by_interface(&interface)?;
114
115        if *device.device_type() == DeviceType::WiFi {
116            Ok(device)
117        } else {
118            bail!(ErrorKind::Runtime(format!(
119                "{} is not a WiFi device",
120                interface
121            )))
122        }
123    } else {
124        let devices = manager.get_devices()?;
125
126        let index = devices
127            .iter()
128            .position(|d| *d.device_type() == DeviceType::WiFi);
129
130        if let Some(index) = index {
131            Ok(devices[index].clone())
132        } else {
133            bail!(ErrorKind::Runtime("Cannot find a WiFi device".into()))
134        }
135    }
136}
Source

pub fn get_device_by_interface(&self, interface: &str) -> Result<Device>

Examples found in repository?
examples/hotspot.rs (line 113)
111fn find_device(manager: &NetworkManager, interface: Option<String>) -> Result<Device> {
112    if let Some(interface) = interface {
113        let device = manager.get_device_by_interface(&interface)?;
114
115        if *device.device_type() == DeviceType::WiFi {
116            Ok(device)
117        } else {
118            bail!(ErrorKind::Runtime(format!(
119                "{} is not a WiFi device",
120                interface
121            )))
122        }
123    } else {
124        let devices = manager.get_devices()?;
125
126        let index = devices
127            .iter()
128            .position(|d| *d.device_type() == DeviceType::WiFi);
129
130        if let Some(index) = index {
131            Ok(devices[index].clone())
132        } else {
133            bail!(ErrorKind::Runtime("Cannot find a WiFi device".into()))
134        }
135    }
136}
Source

pub fn get_state(&self) -> Result<NetworkManagerState>

Source

pub fn get_connectivity(&self) -> Result<Connectivity>

Source

pub fn is_networking_enabled(&self) -> Result<bool>

Source

pub fn is_wireless_enabled(&self) -> Result<bool>

Trait Implementations§

Source§

impl Default for NetworkManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.