feat: initialize the config service

This commit is contained in:
2026-08-17 21:10:15 -05:00
parent 41647f8d39
commit 69669f2011
4 changed files with 154 additions and 1 deletions
Generated
+51
View File
@@ -158,6 +158,15 @@ dependencies = [
"generic-array",
]
[[package]]
name = "cobs"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1"
dependencies = [
"thiserror",
]
[[package]]
name = "const-default"
version = "1.0.0"
@@ -186,6 +195,7 @@ version = "0.1.0"
dependencies = [
"critical-section",
"defmt 1.1.1",
"embassy-embedded-hal",
"embassy-executor",
"embassy-futures",
"embassy-net",
@@ -199,8 +209,12 @@ dependencies = [
"esp-println",
"esp-radio",
"esp-rtos",
"esp-storage",
"picoserve",
"portable-atomic",
"postcard",
"sequential-storage",
"serde",
"sntpc",
"sntpc-net-embassy",
"sntpc-time-embassy",
@@ -431,9 +445,11 @@ version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0641612053b2f34fc250bb63f6630ae75de46e02ade7f457268447081d709ce"
dependencies = [
"defmt 1.1.1",
"embassy-futures",
"embassy-hal-internal",
"embassy-sync 0.8.0",
"embassy-time",
"embedded-hal 0.2.7",
"embedded-hal 1.0.0",
"embedded-hal-async",
@@ -1021,6 +1037,22 @@ dependencies = [
"xtensa-lx",
]
[[package]]
name = "esp-storage"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cf2b3f00c9f94b27c62a4f5296b19470c3a083c687c8146e554a459f9bee596"
dependencies = [
"defmt 1.1.1",
"document-features",
"embedded-storage",
"esp-hal",
"esp-hal-procmacros",
"esp-metadata-generated",
"esp-rom-sys",
"esp-sync",
]
[[package]]
name = "esp-sync"
version = "0.2.1"
@@ -1660,6 +1692,16 @@ dependencies = [
"syn 2.0.117",
]
[[package]]
name = "postcard"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24"
dependencies = [
"cobs",
"serde",
]
[[package]]
name = "proc-macro-crate"
version = "3.5.0"
@@ -1819,6 +1861,15 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "sequential-storage"
version = "8.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e750f14f4f6e5a81277c66311713a56106c8df0196df62a634d1fc806556832b"
dependencies = [
"embedded-storage-async",
]
[[package]]
name = "serde"
version = "1.0.228"
+6
View File
@@ -33,6 +33,7 @@ esp-println = { version = "0.17.0", features = ["defmt-espflash", "esp32c6"] }
embassy-sync = { version = "0.8.0", features = ["defmt"] }
embassy-futures = { version = "0.1.2", features = ["defmt"] }
embassy-embedded-hal = { version = "0.6.0", features = ["defmt", "time"] }
embedded-rpc = "0.2.0"
critical-section = "1.2.0"
@@ -61,6 +62,11 @@ portable-atomic = "1.15.0"
picoserve = { version = "0.19", features = ["defmt", "embassy", "json"] }
sequential-storage = "8.0"
esp-storage = { version = "0.9", features = ["defmt", "esp32c6"] }
postcard = { version = "1.1", default-features = false }
serde = { version = "1.0", default-features = false, features = ["derive"] }
# For fine tuning these settings, please refer to https://doc.rust-lang.org/cargo/reference/profiles.html
[profile.dev]
+94
View File
@@ -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
View File
@@ -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 {