From f2388b25f0fc986480ff8370bc0c1451d2bc822e Mon Sep 17 00:00:00 2001 From: Louis Thiery Date: Wed, 7 Jun 2023 12:44:19 -0700 Subject: [PATCH] Example program takes user input for selecting interface (#7) --- Cargo.toml | 5 ++++- examples/wifi-sta.rs | 29 +++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6c2964d..05a8f6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/examples/wifi-sta.rs b/examples/wifi-sta.rs index 852c86a..8a37ddd 100644 --- a/examples/wifi-sta.rs +++ b/examples/wifi-sta.rs @@ -1,5 +1,7 @@ use env_logger::Env; use log::{error, info}; +use network_interface::{NetworkInterface, NetworkInterfaceConfig}; +use tokio::io; use wifi_ctrl::{sta, Result}; #[tokio::main] @@ -7,9 +9,24 @@ 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::()?; 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(); @@ -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() +}