feat(wifi): use the embassy-sync RpcService for the wifi service
This commit is contained in:
+19
-36
@@ -2,30 +2,29 @@ use defmt::{error, info};
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_futures::select::{Either, select};
|
||||
use embassy_net::{Runner, Stack};
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::signal::Signal;
|
||||
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, rpc_service::RpcService};
|
||||
use embassy_time::{Duration, Timer};
|
||||
use embedded_rpc::{RequestDroppedError, RpcService};
|
||||
use esp_hal::peripherals::WIFI;
|
||||
use esp_radio::wifi::sta::StationConfig;
|
||||
use esp_radio::wifi::{ControllerConfig, Interface, WifiController};
|
||||
|
||||
pub enum WifiCommand {
|
||||
SetStationConfig(esp_radio::wifi::Config),
|
||||
SetDhcpConfig(embassy_net::DhcpConfig),
|
||||
pub static WIFI_SERVICE: RpcService<CriticalSectionRawMutex, WifiService, 64> = RpcService::new();
|
||||
|
||||
pub struct WifiService {
|
||||
pub net_stack: Stack<'static>
|
||||
}
|
||||
|
||||
impl WifiCommand {
|
||||
pub async fn set_station_config(
|
||||
config: esp_radio::wifi::Config,
|
||||
) -> Result<(), RequestDroppedError> {
|
||||
WIFI_SERVICE
|
||||
.request(WifiCommand::SetStationConfig(config))
|
||||
.await
|
||||
impl WifiService {
|
||||
pub async fn set_station_config(config: esp_radio::wifi::Config) {
|
||||
WIFI_HANDLER_RPC.signal(config);
|
||||
}
|
||||
|
||||
pub async fn set_dhcp_config(&self, config: embassy_net::DhcpConfig) {
|
||||
self.net_stack.set_config_v4(embassy_net::ConfigV4::Dhcp(config));
|
||||
}
|
||||
}
|
||||
|
||||
pub static WIFI_SERVICE: RpcService<CriticalSectionRawMutex, WifiCommand, ()> = RpcService::new();
|
||||
|
||||
pub fn start_wifi(spawner: Spawner, wifi: WIFI<'static>) -> Stack<'static> {
|
||||
let wifi_config = esp_radio::wifi::Config::Station(StationConfig::default());
|
||||
|
||||
@@ -62,26 +61,10 @@ pub fn start_wifi(spawner: Spawner, wifi: WIFI<'static>) -> Stack<'static> {
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn handler(net_stack: Stack<'static>) {
|
||||
loop {
|
||||
let (req, served) = WIFI_SERVICE.serve().await;
|
||||
|
||||
match req {
|
||||
WifiCommand::SetStationConfig(config) => {
|
||||
if WIFI_HANDLER_RPC.request(config).await.is_err() {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
WifiCommand::SetDhcpConfig(dhcp_config) => {
|
||||
net_stack.set_config_v4(embassy_net::ConfigV4::Dhcp(dhcp_config));
|
||||
}
|
||||
}
|
||||
|
||||
served.respond(());
|
||||
}
|
||||
WIFI_SERVICE.run(&mut WifiService {net_stack: net_stack}).await;
|
||||
}
|
||||
|
||||
static WIFI_HANDLER_RPC: RpcService<CriticalSectionRawMutex, esp_radio::wifi::Config, ()> =
|
||||
RpcService::new();
|
||||
static WIFI_HANDLER_RPC: Signal<CriticalSectionRawMutex, esp_radio::wifi::Config> = Signal::new();
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn wifi_handler(mut controller: WifiController<'static>) {
|
||||
@@ -103,11 +86,11 @@ async fn wifi_handler(mut controller: WifiController<'static>) {
|
||||
};
|
||||
|
||||
loop {
|
||||
if let Either::First((req, served)) =
|
||||
select(WIFI_HANDLER_RPC.serve(), connection(&mut controller)).await
|
||||
if let Either::First(req) =
|
||||
select(WIFI_HANDLER_RPC.wait(), connection(&mut controller)).await
|
||||
{
|
||||
if controller.set_config(&req).is_ok() {
|
||||
served.respond(());
|
||||
if let Some(err) = controller.set_config(&req).err() {
|
||||
error!("Wifi controller failed to set a new config: {:?}", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user