feat: initialize the config service
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
use embassy_embedded_hal::adapter::BlockingAsync;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embedded_rpc::RpcService;
|
||||
use esp_hal::peripherals::FLASH;
|
||||
use esp_storage::FlashStorage;
|
||||
use sequential_storage::{
|
||||
cache::Cache,
|
||||
map::{Key, MapConfig, MapStorage, SerializationError},
|
||||
};
|
||||
|
||||
pub enum ConfigCommand {
|
||||
SetStationCreds(),
|
||||
}
|
||||
|
||||
impl ConfigCommand {
|
||||
pub async fn set_station_creds() {}
|
||||
}
|
||||
|
||||
pub static CONFIG_SERVICE: RpcService<CriticalSectionRawMutex, ConfigCommand, ()> =
|
||||
RpcService::new();
|
||||
|
||||
pub fn start_config(spawner: Spawner, flash: FLASH<'static>) {
|
||||
spawner.spawn(config_handler(flash).unwrap());
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn config_handler(flash: FLASH<'static>) {
|
||||
let mut storage = MapStorage::<ConfigKey, _, _>::new(
|
||||
BlockingAsync::new(FlashStorage::new(flash)),
|
||||
const { MapConfig::new(0x3F_0000..0x40_0000) },
|
||||
Cache::new_uncached(),
|
||||
);
|
||||
|
||||
let mut data_buffer = [0u8; 256];
|
||||
|
||||
loop {
|
||||
let (req, served) = CONFIG_SERVICE.serve().await;
|
||||
|
||||
match req {
|
||||
ConfigCommand::SetStationCreds() => {
|
||||
todo!()
|
||||
// if storage
|
||||
// .store_item(&mut data_buffer, &ConfigKey::Wifi, )
|
||||
// .await
|
||||
// .is_ok()
|
||||
// {
|
||||
// served.respond(());
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
enum ConfigKey {
|
||||
Wifi,
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for ConfigKey {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
const WIFI: u8 = ConfigKey::Wifi as u8;
|
||||
match value {
|
||||
WIFI => Ok(ConfigKey::Wifi),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ConfigKey> for u8 {
|
||||
fn from(c: ConfigKey) -> u8 {
|
||||
c as u8
|
||||
}
|
||||
}
|
||||
|
||||
impl Key for ConfigKey {
|
||||
fn get_len(_: &[u8]) -> Result<usize, SerializationError> {
|
||||
Ok(1)
|
||||
}
|
||||
|
||||
fn serialize_into(&self, buffer: &mut [u8]) -> Result<usize, SerializationError> {
|
||||
let val = buffer.get_mut(0).ok_or(SerializationError::BufferTooSmall)?;
|
||||
*val = self.clone().into();
|
||||
Ok(1)
|
||||
}
|
||||
|
||||
fn deserialize_from(buffer: &[u8]) -> Result<(Self, usize), SerializationError> {
|
||||
let val = *buffer.get(0).ok_or(SerializationError::BufferTooSmall)?;
|
||||
Ok((val.try_into().map_err(|_| {SerializationError::InvalidData})?, 1))
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -12,7 +12,9 @@ pub mod http_api;
|
||||
pub mod clock;
|
||||
|
||||
pub mod wifi;
|
||||
//
|
||||
|
||||
pub mod config;
|
||||
|
||||
// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html
|
||||
#[macro_export]
|
||||
macro_rules! mk_static {
|
||||
|
||||
Reference in New Issue
Block a user