Compare commits
5
Commits
8d2d69ffc1
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b97bf144dc
|
||
|
|
27a280536e
|
||
|
|
4154bb8450
|
||
|
|
83b0b85fee
|
||
|
|
a4acfc0915
|
Generated
+22
-22
@@ -95,11 +95,11 @@
|
|||||||
},
|
},
|
||||||
"crane": {
|
"crane": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1777335812,
|
"lastModified": 1790899423,
|
||||||
"narHash": "sha256-bEg5xoAxAwsyfnGhkEX7RJViTIBIYPd8ISg4O1c0HFc=",
|
"narHash": "sha256-THGtz65UGJU+sonZidRboyCMk+9DclpMF7pwL4uDV4M=",
|
||||||
"owner": "ipetkov",
|
"owner": "ipetkov",
|
||||||
"repo": "crane",
|
"repo": "crane",
|
||||||
"rev": "5e0fb2f64edff2822249f21293b8304dedaaf676",
|
"rev": "47b6b27ed9a3a9181415e4367d0c30ab2a0e0250",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -113,11 +113,11 @@
|
|||||||
"nixpkgs-lib": "nixpkgs-lib"
|
"nixpkgs-lib": "nixpkgs-lib"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1778716662,
|
"lastModified": 1790898470,
|
||||||
"narHash": "sha256-m1Yf0wZ8j1OHjTc2UwHwyQRSnNeSgLJOd7q5Y45hzi4=",
|
"narHash": "sha256-zTwuwezD0w3hpga6a6P8emidzAZFyWqlNeRBlpWOOl8=",
|
||||||
"owner": "hercules-ci",
|
"owner": "hercules-ci",
|
||||||
"repo": "flake-parts",
|
"repo": "flake-parts",
|
||||||
"rev": "f7c1a2d347e4c52d5fb8d10cb4d94b5884e546fb",
|
"rev": "024633cd702b10285db5cb19b40ad48d2399ba60",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -146,11 +146,11 @@
|
|||||||
},
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1780749050,
|
"lastModified": 1791260994,
|
||||||
"narHash": "sha256-3av0pIjlOWQ6rDbNOmpUSvbNnJkGORQKKjb4LtCZsIY=",
|
"narHash": "sha256-Miqqk/ammqnTUxaoCyvtwoPeLbI1ksFyLxi/CazZpWY=",
|
||||||
"owner": "nixos",
|
"owner": "nixos",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "a799d3e3886da994fa307f817a6bc705ae538eeb",
|
"rev": "151fa4e8ddfdd8dd25d945ad94ed54a13de9f6e4",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -162,11 +162,11 @@
|
|||||||
},
|
},
|
||||||
"nixpkgs-lib": {
|
"nixpkgs-lib": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1777168982,
|
"lastModified": 1790477367,
|
||||||
"narHash": "sha256-GOkGPcboWE9BmGCRMLX3worL4EMnsnG8MyKmXNeYuhQ=",
|
"narHash": "sha256-oujbxmVKDhci2E4EM3KyAbRUgp5/IsCoiA/LfEF7OOk=",
|
||||||
"owner": "nix-community",
|
"owner": "nix-community",
|
||||||
"repo": "nixpkgs.lib",
|
"repo": "nixpkgs.lib",
|
||||||
"rev": "f5901329dade4a6ea039af1433fb087bd9c1fe14",
|
"rev": "f7cd230690d9fc982d06129f9189b101fddcfa3b",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -238,11 +238,11 @@
|
|||||||
"treefmt-nix": "treefmt-nix"
|
"treefmt-nix": "treefmt-nix"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1780887496,
|
"lastModified": 1791419199,
|
||||||
"narHash": "sha256-4mDOPU6Ey3SMsUsUeeNLkXC9uSdn0vpyzJIumw01HV8=",
|
"narHash": "sha256-eQx167Svp11qqWgwFrvV+tELtWZ+Wu2+zgKsAl8fx0M=",
|
||||||
"ref": "refs/heads/master",
|
"ref": "refs/heads/master",
|
||||||
"rev": "0d1a37eef3e3954cc698b577edbc37eaa736b859",
|
"rev": "00ab3b0148e915f22ef6815a9fe24de5d8dde295",
|
||||||
"revCount": 45,
|
"revCount": 51,
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://git.krantz.one/reed/project-templates"
|
"url": "https://git.krantz.one/reed/project-templates"
|
||||||
},
|
},
|
||||||
@@ -298,11 +298,11 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1780197589,
|
"lastModified": 1791276535,
|
||||||
"narHash": "sha256-FVCr2Ij/jKf59a4LW481eeOF6rJRreOBrVgW/aUBTrw=",
|
"narHash": "sha256-iwqAmHTX1IgzTumJR3JayNAQAo5xEmmOuGzB6b2pT/Q=",
|
||||||
"owner": "oxalica",
|
"owner": "oxalica",
|
||||||
"repo": "rust-overlay",
|
"repo": "rust-overlay",
|
||||||
"rev": "21632e942d89bf1cce4e5a63d7e58a215a0cbfcc",
|
"rev": "b962e5d0c8cd97196c6a882d659fe451b52ad989",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -334,11 +334,11 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1775636079,
|
"lastModified": 1791203292,
|
||||||
"narHash": "sha256-pc20NRoMdiar8oPQceQT47UUZMBTiMdUuWrYu2obUP0=",
|
"narHash": "sha256-vuzVwvjeucSnwKwMBpyP8r5IzdrMKCHEdwTrRFwUN7I=",
|
||||||
"owner": "numtide",
|
"owner": "numtide",
|
||||||
"repo": "treefmt-nix",
|
"repo": "treefmt-nix",
|
||||||
"rev": "790751ff7fd3801feeaf96d7dc416a8d581265ba",
|
"rev": "03d8ee1bcbc8f9638907bb79edb9673151d0e22b",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
|||||||
+59
-25
@@ -13,19 +13,49 @@ use sntpc_time_embassy::EmbassyTimestampGenerator;
|
|||||||
// NTP server (pool.ntp.org is the usual choice)
|
// NTP server (pool.ntp.org is the usual choice)
|
||||||
const NTP_SERVER: &str = "pool.ntp.org";
|
const NTP_SERVER: &str = "pool.ntp.org";
|
||||||
const NTP_PORT: u16 = 123;
|
const NTP_PORT: u16 = 123;
|
||||||
|
// Unix epoch is 1900-01-01 + 70 years = 2_208_988_800 seconds.
|
||||||
|
const NTP_UNIX_OFFSET: u64 = 2_208_988_800;
|
||||||
|
|
||||||
|
static NTP_BOOT_OFFSET: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
|
#[allow(
|
||||||
|
clippy::large_stack_frames,
|
||||||
|
reason = "it's not unusual to allocate larger objects in service's start functions"
|
||||||
|
)]
|
||||||
|
pub fn start_ntp_sync(spawner: Spawner, net_stack: Stack<'static>) {
|
||||||
|
let service = crate::mk_static!(
|
||||||
|
NtpClient,
|
||||||
|
NtpClient {
|
||||||
|
net_stack,
|
||||||
|
|
||||||
|
rx_meta: [PacketMetadata::EMPTY; 16],
|
||||||
|
rx_buf: [0u8; 48],
|
||||||
|
tx_meta: [PacketMetadata::EMPTY; 16],
|
||||||
|
tx_buf: [0u8; 48],
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
spawner.spawn(handler(service).unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
pub fn now() -> embassy_time::Instant {
|
pub fn now() -> embassy_time::Instant {
|
||||||
embassy_time::Instant::now() + Duration::from_secs(NTP_BOOT_OFFSET.load(Ordering::Relaxed))
|
embassy_time::Instant::now() + Duration::from_secs(NTP_BOOT_OFFSET.load(Ordering::Relaxed))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start_ntp_sync(spawner: Spawner, net_stack: Stack<'static>) {
|
|
||||||
spawner.spawn(handler(net_stack).unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
async fn handler(net_stack: Stack<'static>) {
|
async fn handler(service: &'static mut NtpClient) {
|
||||||
|
let address = loop {
|
||||||
|
match service.fetch_ntp_address(NTP_SERVER, NTP_PORT).await {
|
||||||
|
Ok(addr) => break addr,
|
||||||
|
Err(err) => {
|
||||||
|
defmt::error!("Failed to get the NTP server's address: {}", err);
|
||||||
|
Timer::after(Duration::from_secs(10)).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match fetch_ntp_time(net_stack).await {
|
match service.fetch_ntp_time(address).await {
|
||||||
Ok(unix_secs) => {
|
Ok(unix_secs) => {
|
||||||
NTP_BOOT_OFFSET.store(
|
NTP_BOOT_OFFSET.store(
|
||||||
unix_secs - embassy_time::Instant::now().as_secs(),
|
unix_secs - embassy_time::Instant::now().as_secs(),
|
||||||
@@ -41,46 +71,50 @@ async fn handler(net_stack: Stack<'static>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static NTP_BOOT_OFFSET: AtomicU64 = AtomicU64::new(0);
|
struct NtpClient {
|
||||||
|
net_stack: Stack<'static>,
|
||||||
|
|
||||||
pub async fn fetch_ntp_time(net_stack: Stack<'static>) -> Result<u64, &'static str> {
|
rx_meta: [PacketMetadata; 16],
|
||||||
|
rx_buf: [u8; 48],
|
||||||
|
tx_meta: [PacketMetadata; 16],
|
||||||
|
tx_buf: [u8; 48],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NtpClient {
|
||||||
|
async fn fetch_ntp_address(&self, server: &str, port: u16) -> Result<SocketAddr, &'static str> {
|
||||||
// Resolve the NTP server hostname
|
// Resolve the NTP server hostname
|
||||||
let server_addr = net_stack
|
let server_addr = self
|
||||||
.dns_query(NTP_SERVER, embassy_net::dns::DnsQueryType::A)
|
.net_stack
|
||||||
|
.dns_query(server, embassy_net::dns::DnsQueryType::A)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| "DNS resolution failed")?
|
.map_err(|_| "DNS resolution failed")?
|
||||||
.first()
|
.first()
|
||||||
.copied()
|
.copied()
|
||||||
.ok_or("No DNS result")?;
|
.ok_or("No DNS result")?;
|
||||||
|
|
||||||
let socket_addr = SocketAddr::new(server_addr.into(), NTP_PORT);
|
Ok(SocketAddr::new(server_addr.into(), port))
|
||||||
|
}
|
||||||
// Allocate UDP buffers (can be small — SNTP packets are tiny)
|
|
||||||
let mut rx_meta = [PacketMetadata::EMPTY; 16];
|
|
||||||
let mut rx_buf = [0u8; 48];
|
|
||||||
let mut tx_meta = [PacketMetadata::EMPTY; 16];
|
|
||||||
let mut tx_buf = [0u8; 48];
|
|
||||||
|
|
||||||
|
async fn fetch_ntp_time(&mut self, address: SocketAddr) -> Result<u64, &'static str> {
|
||||||
let mut socket = UdpSocket::new(
|
let mut socket = UdpSocket::new(
|
||||||
net_stack,
|
self.net_stack,
|
||||||
&mut rx_meta,
|
&mut self.rx_meta,
|
||||||
&mut rx_buf,
|
&mut self.rx_buf,
|
||||||
&mut tx_meta,
|
&mut self.tx_meta,
|
||||||
&mut tx_buf,
|
&mut self.tx_buf,
|
||||||
);
|
);
|
||||||
socket.bind(0).map_err(|_| "bind failed")?;
|
socket.bind(0).map_err(|_| "bind failed")?;
|
||||||
|
|
||||||
let wrapped = UdpSocketWrapper::from(socket);
|
let wrapped = UdpSocketWrapper::from(socket);
|
||||||
let ctx = NtpContext::new(EmbassyTimestampGenerator::default());
|
let ctx = NtpContext::new(EmbassyTimestampGenerator::default());
|
||||||
|
|
||||||
let result = get_time(socket_addr, &wrapped, ctx)
|
let result = get_time(address, &wrapped, ctx)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| "SNTP request failed")?;
|
.map_err(|_| "SNTP request failed")?;
|
||||||
|
|
||||||
// result.sec is the NTP timestamp in seconds since 1900-01-01.
|
// result.sec is the NTP timestamp in seconds since 1900-01-01.
|
||||||
// Unix epoch is 1900-01-01 + 70 years = 2_208_988_800 seconds.
|
|
||||||
const NTP_UNIX_OFFSET: u64 = 2_208_988_800;
|
|
||||||
let unix_secs = result.sec().saturating_sub(NTP_UNIX_OFFSET);
|
let unix_secs = result.sec().saturating_sub(NTP_UNIX_OFFSET);
|
||||||
|
|
||||||
Ok(unix_secs)
|
Ok(unix_secs)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
+13
-19
@@ -34,7 +34,7 @@ pub async fn set_wifi_creds(value: WifiCredentials) -> Result<(), ConfigError> {
|
|||||||
recv.await?
|
recv.await?
|
||||||
}
|
}
|
||||||
|
|
||||||
static CONFIG_CHANNEL: Channel<CriticalSectionRawMutex, ConfigOperationMessage, 1> = Channel::new();
|
static CONFIG_CHANNEL: Channel<CriticalSectionRawMutex, ConfigOperationMessage, 5> = Channel::new();
|
||||||
|
|
||||||
enum ConfigOperationMessage {
|
enum ConfigOperationMessage {
|
||||||
SetWifiCreds(WifiCredentials, Sender<Result<(), ConfigError>>),
|
SetWifiCreds(WifiCredentials, Sender<Result<(), ConfigError>>),
|
||||||
@@ -63,11 +63,6 @@ async fn handler(
|
|||||||
Cache<Uncached, Uncached, Uncached, ConfigKey>,
|
Cache<Uncached, Uncached, Uncached, ConfigKey>,
|
||||||
>,
|
>,
|
||||||
) -> ! {
|
) -> ! {
|
||||||
let resp_fail = |_| {
|
|
||||||
// TODO: make tracing work so if this code is reached you know where the error originated
|
|
||||||
error!("failed to respond");
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut data_buffer = [0u8; 256];
|
let mut data_buffer = [0u8; 256];
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@@ -75,26 +70,25 @@ async fn handler(
|
|||||||
|
|
||||||
match operation {
|
match operation {
|
||||||
ConfigOperationMessage::SetWifiCreds(value, resp) => {
|
ConfigOperationMessage::SetWifiCreds(value, resp) => {
|
||||||
|
let mut f = async || {
|
||||||
let mut buf = [0u8; 256];
|
let mut buf = [0u8; 256];
|
||||||
if let Err(err) = to_slice(&value, &mut buf) {
|
to_slice(&value, &mut buf)?;
|
||||||
let _ = resp.send(Err(err.into())).map_err(resp_fail);
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Err(err) = storage
|
storage
|
||||||
.store_item(&mut data_buffer, &ConfigKey::Wifi, &buf)
|
.store_item(&mut data_buffer, &ConfigKey::Wifi, &buf)
|
||||||
.await
|
.await?;
|
||||||
{
|
|
||||||
let _ = resp.send(Err(err.into())).map_err(resp_fail);
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: send the change to the wifi service
|
// TODO: send the change to the wifi service
|
||||||
// WIFI_SERVICE
|
// WIFI_SERVICE
|
||||||
// .call(|s| s.set_station_config(esp_radio::wifi::Config::Station(value.into())))
|
// .call(|s| s.set_station_config(esp_radio::wifi::Config::Station(value.into())))
|
||||||
// .await;
|
// .await;
|
||||||
|
|
||||||
let _ = resp.send(Ok(())).map_err(resp_fail);
|
Ok(())
|
||||||
|
};
|
||||||
|
if resp.send(f().await).is_err() {
|
||||||
|
// TODO: find a way to not hard-code symbols in a string
|
||||||
|
error!("ConfigOperationMessage::SetWifiCreds failed to respond")
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,7 +112,7 @@ impl Key for ConfigKey {
|
|||||||
let val = buffer
|
let val = buffer
|
||||||
.get_mut(0)
|
.get_mut(0)
|
||||||
.ok_or(sequential_storage::map::SerializationError::BufferTooSmall)?;
|
.ok_or(sequential_storage::map::SerializationError::BufferTooSmall)?;
|
||||||
*val = self.clone().into();
|
*val = (*self).into();
|
||||||
Ok(1)
|
Ok(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +120,7 @@ impl Key for ConfigKey {
|
|||||||
buffer: &[u8],
|
buffer: &[u8],
|
||||||
) -> Result<(Self, usize), sequential_storage::map::SerializationError> {
|
) -> Result<(Self, usize), sequential_storage::map::SerializationError> {
|
||||||
let val = *buffer
|
let val = *buffer
|
||||||
.get(0)
|
.first()
|
||||||
.ok_or(sequential_storage::map::SerializationError::BufferTooSmall)?;
|
.ok_or(sequential_storage::map::SerializationError::BufferTooSmall)?;
|
||||||
Ok((
|
Ok((
|
||||||
val.try_into()
|
val.try_into()
|
||||||
|
|||||||
+18
-20
@@ -14,19 +14,30 @@ impl AppBuilder for HttpApiProps {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(
|
||||||
|
clippy::large_stack_frames,
|
||||||
|
reason = "it's not unusual to allocate larger objects in service's start functions"
|
||||||
|
)]
|
||||||
pub fn start_http_api(spawner: Spawner, net_stack: Stack<'static>) {
|
pub fn start_http_api(spawner: Spawner, net_stack: Stack<'static>) {
|
||||||
spawner.spawn(
|
let service = crate::mk_static!(
|
||||||
handler(HttpApiApp::new(
|
HttpApiApp<'static, HttpApiProps>,
|
||||||
|
HttpApiApp {
|
||||||
|
config: Default::default(),
|
||||||
|
|
||||||
net_stack,
|
net_stack,
|
||||||
Default::default(),
|
app: HttpApiProps.build_app(),
|
||||||
Default::default(),
|
|
||||||
))
|
tcp_rx_buffer: [0; 1024],
|
||||||
.unwrap(),
|
tcp_tx_buffer: [0; 1024],
|
||||||
|
http_buffer: [0; 2048],
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
spawner.spawn(handler(service).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
async fn handler(mut service: HttpApiApp<'static, HttpApiProps>) {
|
async fn handler(service: &'static mut HttpApiApp<'static, HttpApiProps>) {
|
||||||
service.run().await;
|
service.run().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,19 +67,6 @@ pub struct HttpApiApp<'a, AB: AppBuilder> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, AB: AppBuilder> HttpApiApp<'a, AB> {
|
impl<'a, AB: AppBuilder> HttpApiApp<'a, AB> {
|
||||||
pub fn new(net_stack: Stack<'a>, config: HttpApiConfig, app: AB) -> Self {
|
|
||||||
Self {
|
|
||||||
config: config,
|
|
||||||
|
|
||||||
net_stack: net_stack,
|
|
||||||
app: app.build_app(),
|
|
||||||
|
|
||||||
tcp_rx_buffer: [0; 1024],
|
|
||||||
tcp_tx_buffer: [0; 1024],
|
|
||||||
http_buffer: [0; 2048],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn run(&mut self) {
|
pub async fn run(&mut self) {
|
||||||
picoserve::Server::new(
|
picoserve::Server::new(
|
||||||
&self.app,
|
&self.app,
|
||||||
|
|||||||
@@ -7,6 +7,8 @@
|
|||||||
#![deny(clippy::large_stack_frames)]
|
#![deny(clippy::large_stack_frames)]
|
||||||
#![feature(impl_trait_in_assoc_type)]
|
#![feature(impl_trait_in_assoc_type)]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
pub mod http_api;
|
pub mod http_api;
|
||||||
|
|
||||||
pub mod clock;
|
pub mod clock;
|
||||||
|
|||||||
+15
-11
@@ -1,3 +1,4 @@
|
|||||||
|
use alloc::boxed::Box;
|
||||||
use defmt::{error, info};
|
use defmt::{error, info};
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_futures::select::{Either, select};
|
use embassy_futures::select::{Either, select};
|
||||||
@@ -11,6 +12,10 @@ use esp_radio::wifi::sta::StationConfig;
|
|||||||
use esp_radio::wifi::{ControllerConfig, Interface, WifiController};
|
use esp_radio::wifi::{ControllerConfig, Interface, WifiController};
|
||||||
use oneshot::{RecvError, Sender};
|
use oneshot::{RecvError, Sender};
|
||||||
|
|
||||||
|
#[allow(
|
||||||
|
clippy::large_stack_frames,
|
||||||
|
reason = "it's not unusual to allocate larger objects in service's start functions"
|
||||||
|
)]
|
||||||
pub fn start_wifi(spawner: Spawner, wifi: WIFI<'static>) -> Stack<'static> {
|
pub fn start_wifi(spawner: Spawner, wifi: WIFI<'static>) -> Stack<'static> {
|
||||||
let wifi_config = esp_radio::wifi::Config::Station(StationConfig::default());
|
let wifi_config = esp_radio::wifi::Config::Station(StationConfig::default());
|
||||||
|
|
||||||
@@ -45,7 +50,7 @@ pub fn start_wifi(spawner: Spawner, wifi: WIFI<'static>) -> Stack<'static> {
|
|||||||
net_stack
|
net_stack
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_station_config(config: esp_radio::wifi::Config) -> Result<(), RecvError> {
|
pub async fn set_station_config(config: Box<esp_radio::wifi::Config>) -> Result<(), RecvError> {
|
||||||
let (send, recv) = oneshot::channel();
|
let (send, recv) = oneshot::channel();
|
||||||
WIFI_CHANNEL
|
WIFI_CHANNEL
|
||||||
.send(WifiOperationMessage::SetStationConfig(config, send))
|
.send(WifiOperationMessage::SetStationConfig(config, send))
|
||||||
@@ -53,7 +58,7 @@ pub async fn set_station_config(config: esp_radio::wifi::Config) -> Result<(), R
|
|||||||
recv.await
|
recv.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_dhcp_config(config: embassy_net::DhcpConfig) -> Result<(), RecvError> {
|
pub async fn set_dhcp_config(config: Box<embassy_net::DhcpConfig>) -> Result<(), RecvError> {
|
||||||
let (send, recv) = oneshot::channel();
|
let (send, recv) = oneshot::channel();
|
||||||
WIFI_CHANNEL
|
WIFI_CHANNEL
|
||||||
.send(WifiOperationMessage::SetDhcpConfig(config, send))
|
.send(WifiOperationMessage::SetDhcpConfig(config, send))
|
||||||
@@ -61,11 +66,11 @@ pub async fn set_dhcp_config(config: embassy_net::DhcpConfig) -> Result<(), Recv
|
|||||||
recv.await
|
recv.await
|
||||||
}
|
}
|
||||||
|
|
||||||
static WIFI_CHANNEL: Channel<CriticalSectionRawMutex, WifiOperationMessage, 1> = Channel::new();
|
static WIFI_CHANNEL: Channel<CriticalSectionRawMutex, WifiOperationMessage, 5> = Channel::new();
|
||||||
|
|
||||||
enum WifiOperationMessage {
|
enum WifiOperationMessage {
|
||||||
SetStationConfig(esp_radio::wifi::Config, Sender<()>),
|
SetStationConfig(Box<esp_radio::wifi::Config>, Sender<()>),
|
||||||
SetDhcpConfig(embassy_net::DhcpConfig, Sender<()>),
|
SetDhcpConfig(Box<embassy_net::DhcpConfig>, Sender<()>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
@@ -73,15 +78,15 @@ async fn handler(net_stack: Stack<'static>) -> ! {
|
|||||||
loop {
|
loop {
|
||||||
match WIFI_CHANNEL.receive().await {
|
match WIFI_CHANNEL.receive().await {
|
||||||
WifiOperationMessage::SetStationConfig(config, resp) => {
|
WifiOperationMessage::SetStationConfig(config, resp) => {
|
||||||
WIFI_HANDLER_RPC.signal(config);
|
WIFI_HANDLER_RPC.signal(*config);
|
||||||
if let Err(_) = resp.send(()) {
|
if resp.send(()).is_err() {
|
||||||
// TODO: find a way to not hard-code symbols in a string
|
// TODO: find a way to not hard-code symbols in a string
|
||||||
error!("WifiOperationMessage::SetStationConfig failed to respond")
|
error!("WifiOperationMessage::SetStationConfig failed to respond")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
WifiOperationMessage::SetDhcpConfig(config, resp) => {
|
WifiOperationMessage::SetDhcpConfig(config, resp) => {
|
||||||
net_stack.set_config_v4(embassy_net::ConfigV4::Dhcp(config));
|
net_stack.set_config_v4(embassy_net::ConfigV4::Dhcp(*config));
|
||||||
if let Err(_) = resp.send(()) {
|
if resp.send(()).is_err() {
|
||||||
// TODO: find a way to not hard-code symbols in a string
|
// TODO: find a way to not hard-code symbols in a string
|
||||||
error!("WifiOperationMessage::SetDhcpConfig failed to respond")
|
error!("WifiOperationMessage::SetDhcpConfig failed to respond")
|
||||||
};
|
};
|
||||||
@@ -114,13 +119,12 @@ async fn connection_handler(mut controller: WifiController<'static>) -> ! {
|
|||||||
loop {
|
loop {
|
||||||
if let Either::First(req) =
|
if let Either::First(req) =
|
||||||
select(WIFI_HANDLER_RPC.wait(), connection(&mut controller)).await
|
select(WIFI_HANDLER_RPC.wait(), connection(&mut controller)).await
|
||||||
|
&& let Some(err) = controller.set_config(&req).err()
|
||||||
{
|
{
|
||||||
if let Some(err) = controller.set_config(&req).err() {
|
|
||||||
error!("Wifi controller failed to set a new config: {:?}", err)
|
error!("Wifi controller failed to set a new config: {:?}", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
async fn ip_logger(net_stack: Stack<'static>) -> ! {
|
async fn ip_logger(net_stack: Stack<'static>) -> ! {
|
||||||
|
|||||||
Reference in New Issue
Block a user