-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WifiAp example program and allow attach options (#8)
- Loading branch information
Showing
7 changed files
with
106 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#![allow(clippy::result_large_err)] | ||
use env_logger::Env; | ||
use log::{error, info}; | ||
use network_interface::{NetworkInterface, NetworkInterfaceConfig}; | ||
use tokio::io; | ||
use wifi_ctrl::{ap, Result}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result { | ||
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); | ||
info!("Starting wifi-ap 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 = ap::WifiSetup::new()?; | ||
|
||
let proposed_path = format!("/var/run/hostapd/{}", 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().is_empty() { | ||
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(); | ||
let runtime = setup.complete(); | ||
|
||
let (_runtime, _app, _broadcast) = tokio::join!( | ||
async move { | ||
if let Err(e) = runtime.run().await { | ||
error!("Error: {e}"); | ||
} | ||
}, | ||
app(requester), | ||
broadcast_listener(broadcast), | ||
); | ||
Ok(()) | ||
} | ||
|
||
async fn app(_: ap::RequestClient) -> Result { | ||
Ok(()) | ||
} | ||
|
||
async fn broadcast_listener(mut broadcast_receiver: ap::BroadcastReceiver) -> Result { | ||
while let Ok(broadcast) = broadcast_receiver.recv().await { | ||
info!("Broadcast: {:?}", broadcast); | ||
} | ||
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters