refactor: moved the wifi code to a wifi service
This commit is contained in:
+125
@@ -0,0 +1,125 @@
|
||||
use defmt::{error, info};
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_futures::select::Either::First;
|
||||
use embassy_futures::select::select;
|
||||
use embassy_net::{Runner, Stack};
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::channel::{Channel, Sender};
|
||||
use embassy_time::{Duration, Timer};
|
||||
use esp_hal::peripherals::WIFI;
|
||||
use esp_radio::wifi::sta::StationConfig;
|
||||
use esp_radio::wifi::{ControllerConfig, Interface, WifiController, WifiError};
|
||||
|
||||
pub enum WifiCommand<'a> {
|
||||
SetStationConfig(
|
||||
esp_radio::wifi::Config,
|
||||
Sender<'a, CriticalSectionRawMutex, Result<(), WifiError>, 1>,
|
||||
),
|
||||
SetDhcpConfig(embassy_net::DhcpConfig),
|
||||
}
|
||||
|
||||
pub static WIFI_CH: Channel<CriticalSectionRawMutex, WifiCommand, 4> = Channel::new();
|
||||
|
||||
pub fn start_wifi(spawner: Spawner, wifi: WIFI<'static>) -> Stack<'static> {
|
||||
let wifi_config = esp_radio::wifi::Config::Station(StationConfig::default());
|
||||
|
||||
let (wifi_controller, wifi_interfaces) = esp_radio::wifi::new(
|
||||
wifi,
|
||||
ControllerConfig::default().with_initial_config(wifi_config),
|
||||
)
|
||||
.expect("Failed to initialize Wi-Fi controller");
|
||||
|
||||
let mut dhcp_config = embassy_net::DhcpConfig::default();
|
||||
dhcp_config.hostname = Some("cover-theif".try_into().unwrap());
|
||||
let net_config = embassy_net::Config::dhcpv4(dhcp_config);
|
||||
|
||||
let rng = esp_hal::rng::Rng::new();
|
||||
let seed = (rng.random() as u64) << 32 | rng.random() as u64;
|
||||
|
||||
let (net_stack, net_runner) = embassy_net::new(
|
||||
wifi_interfaces.station,
|
||||
net_config,
|
||||
crate::mk_static!(
|
||||
embassy_net::StackResources<3>,
|
||||
embassy_net::StackResources::<3>::new()
|
||||
),
|
||||
seed,
|
||||
);
|
||||
|
||||
spawner.spawn(net_task(net_runner).unwrap());
|
||||
spawner.spawn(handler(wifi_controller, net_stack).unwrap());
|
||||
|
||||
net_stack
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn handler(mut controller: WifiController<'static>, net_stack: Stack<'static>) {
|
||||
let receiver = WIFI_CH.receiver();
|
||||
loop {
|
||||
match select(receiver.receive(), connection(&mut controller, net_stack)).await {
|
||||
First(WifiCommand::SetStationConfig(config, res)) => {
|
||||
res.send(controller.set_config(&config)).await;
|
||||
}
|
||||
First(WifiCommand::SetDhcpConfig(dhcp_config)) => {
|
||||
net_stack.set_config_v4(embassy_net::ConfigV4::Dhcp(dhcp_config));
|
||||
}
|
||||
embassy_futures::select::Either::Second(_) => {
|
||||
unreachable!("connection function should be infinite loop")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn connection(controller: &mut WifiController<'static>, net_stack: Stack<'static>) {
|
||||
info!("start connection task");
|
||||
|
||||
select(
|
||||
async {
|
||||
loop {
|
||||
info!("Connecting to WiFi...");
|
||||
|
||||
match controller.connect_async().await {
|
||||
Ok(info) => {
|
||||
info!("Wifi connected to {:?}", info);
|
||||
|
||||
// wait until we're no longer connected
|
||||
let info = controller.wait_for_disconnect_async().await.ok();
|
||||
info!("Disconnected: {:?}", info);
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to connect to wifi: {:?}", e);
|
||||
}
|
||||
}
|
||||
|
||||
Timer::after(Duration::from_millis(5000)).await
|
||||
}
|
||||
},
|
||||
async {
|
||||
loop {
|
||||
info!("Getting IP address...");
|
||||
net_stack.wait_config_up().await;
|
||||
|
||||
match net_stack.config_v4() {
|
||||
Some(info) => {
|
||||
info!("Got IP: {:?}", info);
|
||||
|
||||
// wait until we're no longer connected
|
||||
net_stack.wait_config_down().await;
|
||||
info!("Address no longer configured");
|
||||
}
|
||||
None => {
|
||||
error!("Failed to configure an address");
|
||||
}
|
||||
}
|
||||
|
||||
Timer::after(Duration::from_millis(5000)).await
|
||||
}
|
||||
},
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn net_task(mut runner: Runner<'static, Interface<'static>>) {
|
||||
runner.run().await
|
||||
}
|
||||
Reference in New Issue
Block a user