Skip to content

Commit

Permalink
Merge pull request #133 from card-io-ecg/wifi
Browse files Browse the repository at this point in the history
Wifi driver cleanup
  • Loading branch information
bugadani authored Oct 9, 2023
2 parents 43ee7b4 + f3c6a3d commit d66b2b7
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 270 deletions.
19 changes: 9 additions & 10 deletions src/board/initialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ impl Board {
return None;
}

self.wifi.initialize(&self.clocks);

let sta = self
.wifi
.configure_sta(NetConfig::dhcpv4(Default::default()))
.configure_sta(NetConfig::dhcpv4(Default::default()), &self.clocks)
.await;

sta.update_known_networks(&self.config.known_networks).await;
Expand Down Expand Up @@ -99,15 +97,16 @@ impl Board {
return None;
}

self.wifi.initialize(&self.clocks);

let ap = self
.wifi
.configure_ap(NetConfig::ipv4_static(StaticConfigV4 {
address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 2, 1), 24),
gateway: Some(Ipv4Address::from_bytes(&[192, 168, 2, 1])),
dns_servers: Default::default(),
}))
.configure_ap(
NetConfig::ipv4_static(StaticConfigV4 {
address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 2, 1), 24),
gateway: Some(Ipv4Address::from_bytes(&[192, 168, 2, 1])),
dns_servers: Default::default(),
}),
&self.clocks,
)
.await;

Some(ap)
Expand Down
89 changes: 39 additions & 50 deletions src/board/wifi/ap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::{boxed::Box, rc::Rc};
use alloc::rc::Rc;
use core::sync::atomic::{AtomicU32, Ordering};
use gui::widgets::wifi::WifiState;

Expand Down Expand Up @@ -64,9 +64,8 @@ impl Ap {

pub(super) struct ApState {
init: EspWifiInitialization,
controller: Option<Box<WifiController<'static>>>,
stack: Rc<StackWrapper>,
connection_task_control: Option<TaskController<(), ApTaskResources>>,
connection_task_control: TaskController<(), ApTaskResources>,
net_task_control: TaskController<!>,
client_count: Rc<AtomicU32>,
}
Expand All @@ -77,81 +76,71 @@ impl ApState {
config: Config,
wifi: &'static mut Wifi,
rng: Rng,
spawner: Spawner,
) -> Self {
info!("Configuring AP");

let (wifi_interface, controller) =
unwrap!(esp_wifi::wifi::new_with_mode(&init, wifi, WifiMode::Ap));

Self {
init,
controller: Some(Box::new(controller)),
stack: Rc::new(StackWrapper::new(wifi_interface, config, rng)),
connection_task_control: None,
net_task_control: TaskController::new(),
client_count: Rc::new(AtomicU32::new(0)),
}
}
info!("Starting AP");

pub(super) fn unwrap(self) -> EspWifiInitialization {
self.init
}
let stack = Rc::new(StackWrapper::new(wifi_interface, config, rng));
let net_task_control = TaskController::new();
let client_count = Rc::new(AtomicU32::new(0));

pub(super) async fn start(&mut self) -> Ap {
if let Some(controller) = self.controller.take() {
info!("Starting AP");
let spawner = Spawner::for_current_executor().await;
let connection_task_control =
TaskController::from_resources(ApTaskResources { controller });

let task_control = TaskController::from_resources(ApTaskResources { controller });
info!("Starting AP task");
spawner.must_spawn(ap_task(
connection_task_control.token(),
client_count.clone(),
));

info!("Starting AP task");
spawner.must_spawn(ap_task(task_control.token(), self.client_count.clone()));
info!("Starting NET task");
spawner.must_spawn(net_task(self.stack.clone(), self.net_task_control.token()));
info!("Starting NET task");
spawner.must_spawn(net_task(stack.clone(), net_task_control.token()));

self.connection_task_control = Some(task_control)
Self {
init,
stack,
net_task_control,
client_count,
connection_task_control,
}

self.handle_unchecked()
}

pub(super) async fn stop(&mut self) {
if let Some(task_control) = self.connection_task_control.take() {
info!("Stopping AP");
let _ = join(task_control.stop(), self.net_task_control.stop()).await;
pub(super) async fn stop(self) -> EspWifiInitialization {
info!("Stopping AP");
let _ = join(
self.connection_task_control.stop(),
self.net_task_control.stop(),
)
.await;

let mut controller = task_control.unwrap().controller;
if matches!(controller.is_started(), Ok(true)) {
unwrap!(controller.stop().await);
}
let mut controller = self.connection_task_control.unwrap().controller;
if matches!(controller.is_started(), Ok(true)) {
unwrap!(controller.stop().await);
}

self.controller = Some(controller);
info!("Stopped AP");

info!("Stopped AP");
}
self.init
}

pub(super) fn is_running(&self) -> bool {
if self.net_task_control.has_exited() {
return false;
}

if let Some(connection_task) = &self.connection_task_control {
if connection_task.has_exited() {
return false;
}
if self.connection_task_control.has_exited() {
return false;
}

true
}

pub(crate) fn handle(&self) -> Option<Ap> {
self.connection_task_control
.as_ref()
.map(|_| self.handle_unchecked())
}

fn handle_unchecked(&self) -> Ap {
pub(crate) fn handle(&self) -> Ap {
Ap {
stack: self.stack.clone(),
client_count: self.client_count.clone(),
Expand All @@ -160,7 +149,7 @@ impl ApState {
}

struct ApTaskResources {
controller: Box<WifiController<'static>>,
controller: WifiController<'static>,
}

#[cardio::task]
Expand Down
Loading

0 comments on commit d66b2b7

Please sign in to comment.