Skip to content

Commit

Permalink
Example program takes user input for selecting interface (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
lthiery authored Jun 7, 2023
1 parent e7a50df commit f2388b2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ tempfile = "3"
tokio = { version = "1", default-features = false, features = ["net", "rt", "sync", "macros", "time"] }

[dev-dependencies]
tokio = { version = "1", features = ["rt-multi-thread"] }
env_logger = "0"
network-interface = "1"
tokio = { version = "1", features = ["rt-multi-thread", "io-std", "io-util"] }
tokio-util ={ version = "0", features = ["codec"] }
futures = "0"
29 changes: 27 additions & 2 deletions examples/wifi-sta.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
use env_logger::Env;
use log::{error, info};
use network_interface::{NetworkInterface, NetworkInterfaceConfig};
use tokio::io;
use wifi_ctrl::{sta, Result};

#[tokio::main]
async fn main() -> Result {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
info!("Starting wifi-sta example");

let mut network_interfaces = NetworkInterface::show().unwrap();
network_interfaces.sort_by(|a, b| a.index.cmp(&b.index));
for (i, itf) in network_interfaces.iter().enumerate() {
info!("[{:?}] {:?}", i, itf.name);
}
let user_input = read_until_break().await;
let index = user_input.trim().parse::<usize>()?;
let mut setup = sta::WifiSetup::new()?;
// Use something like ifconfig to figure out the name of your WiFi interface
setup.set_socket_path("/var/run/wpa_supplicant/wlp2s0");

let proposed_path = format!("/var/run/wpa_supplicant/{}", network_interfaces[index].name);
info!("Connect to \"{proposed_path}\"? Type full new path or just press enter to accept.");

let user_input = read_until_break().await;
if user_input.trim().len() == 0 {
setup.set_socket_path(proposed_path);
} else {
setup.set_socket_path(user_input.trim().to_string());
}

let broadcast = setup.get_broadcast_receiver();
let requester = setup.get_request_client();
Expand Down Expand Up @@ -51,3 +68,11 @@ async fn broadcast_listener(mut broadcast_receiver: sta::BroadcastReceiver) -> R
}
Ok(())
}

async fn read_until_break() -> String {
use futures::stream::StreamExt;
use tokio_util::codec::{FramedRead, LinesCodec};
let stdin = io::stdin();
let mut reader = FramedRead::new(stdin, LinesCodec::new());
reader.next().await.unwrap().unwrap()
}

0 comments on commit f2388b2

Please sign in to comment.