From b6fdb3f367dee6e4b862958182cebf2b6b99c0a4 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Thu, 21 Jul 2022 11:55:37 -0700 Subject: [PATCH] use rmp-serde to store config in NVS --- sphinx-key/Cargo.toml | 1 + sphinx-key/src/main.rs | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/sphinx-key/Cargo.toml b/sphinx-key/Cargo.toml index 49325bd..9ce3505 100644 --- a/sphinx-key/Cargo.toml +++ b/sphinx-key/Cargo.toml @@ -35,6 +35,7 @@ serde_urlencoded = "0.7.1" serde = { version = "1.0.137", default-features = false } serde_json = { version = "1.0.81", default-features = false } hex = "0.4.3" +rmp-serde = "1.1.0" [patch.crates-io] # updates the "rand" create to use esp RNG diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index 854d30a..c2338f5 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -13,7 +13,7 @@ use std::sync::{mpsc, Arc}; use std::thread; use std::time::Duration; -use embedded_svc::storage::StorageBase; +use embedded_svc::storage::RawStorage; use esp_idf_hal::peripherals::Peripherals; use esp_idf_svc::nvs::*; use esp_idf_svc::nvs_storage::EspNvsStorage; @@ -49,8 +49,11 @@ fn main() -> Result<()> { let default_nvs = Arc::new(EspDefaultNvs::new()?); let mut store = EspNvsStorage::new_default(default_nvs.clone(), "sphinx", true).expect("no storage"); - let existing: Option = store.get("config").expect("failed"); - if let Some(exist) = existing { + let mut buf = [0u8; 250]; + // let existing: Option = store.get_raw("config", buf).expect("failed"); + let existing = store.get_raw("config", &mut buf).expect("failed"); + if let Some((exist_bytes, _)) = existing { + let exist: Config = rmp_serde::from_slice(exist_bytes).expect("failed to parse Config"); println!( "=============> START CLIENT NOW <============== {:?}", exist @@ -82,8 +85,9 @@ fn main() -> Result<()> { led_tx.send(Status::WifiAccessPoint).unwrap(); println!("=============> START SERVER NOW AND WAIT <=============="); if let Ok((wifi, config)) = start_config_server_and_wait(default_nvs.clone()) { + let conf = rmp_serde::to_vec(&config).expect("couldnt rmp Config"); store - .put("config", &config) + .put_raw("config", &conf[..]) .expect("could not store config"); println!("CONFIG SAVED"); drop(wifi);