From 77c9ff73ae0a4a060fb0e63c3c03c129b8b08c90 Mon Sep 17 00:00:00 2001 From: ivmarkov Date: Fri, 29 Nov 2024 12:46:41 +0000 Subject: [PATCH] Persist sugar --- .cargo/config.toml | 5 +++++ Cargo.toml | 2 +- examples/light.rs | 7 +++---- examples/light_eth.rs | 5 ++--- src/persist.rs | 45 ++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 55 insertions(+), 9 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index dff2ea8..af6c6e5 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -5,18 +5,23 @@ target = "xtensa-esp32-espidf" #target = "xtensa-esp32s3-espidf" [target.xtensa-esp32-espidf] +rustflags = ["--cfg", "espidf_time64"] linker = "ldproxy" [target.xtensa-esp32s2-espidf] +rustflags = ["--cfg", "espidf_time64"] linker = "ldproxy" [target.xtensa-esp32s3-espidf] +rustflags = ["--cfg", "espidf_time64"] linker = "ldproxy" [target.riscv32imc-esp-espidf] +rustflags = ["--cfg", "espidf_time64"] linker = "ldproxy" [target.riscv32imac-esp-espidf] +rustflags = ["--cfg", "espidf_time64"] linker = "ldproxy" [unstable] diff --git a/Cargo.toml b/Cargo.toml index ac4a50d..d2b47c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,7 @@ opt-level = "z" [features] #default = ["std", "rs-matter-stack"] -default = ["std", "rs-matter-stack", "async-io-mini"] +default = ["std", "rs-matter-stack", "async-io-mini", "examples"] async-io-mini = ["std", "edge-nal-std/async-io-mini"] std = ["esp-idf-svc/std", "edge-nal-std", "rs-matter-stack?/std"] examples = ["default", "esp-idf-svc/binstart", "esp-idf-svc/critical-section"] # Enable only when building the examples diff --git a/examples/light.rs b/examples/light.rs index f8fe7c0..92257a0 100644 --- a/examples/light.rs +++ b/examples/light.rs @@ -15,6 +15,7 @@ use core::pin::pin; use embassy_futures::select::select; use embassy_time::{Duration, Timer}; +use esp_idf_matter::persist; use esp_idf_matter::{init_async_io, EspMatterBle, EspMatterWifi, EspWifiNCMatterStack}; use esp_idf_svc::eventloop::EspSystemEventLoop; @@ -35,7 +36,6 @@ use rs_matter::utils::init::InitMaybeUninit; use rs_matter::utils::select::Coalesce; use rs_matter::BasicCommData; -use rs_matter_stack::persist::DummyPersist; use static_cell::StaticCell; @@ -141,12 +141,11 @@ async fn matter() -> Result<(), anyhow::Error> { // The Matter stack needs the Wifi modem peripheral EspMatterWifi::new(&mut wifi_modem, sysloop, timers, nvs.clone()), // The Matter stack needs the BT modem peripheral - EspMatterBle::new(&mut bt_modem, nvs, stack), + EspMatterBle::new(&mut bt_modem, nvs.clone(), stack), // The Matter stack needs a persister to store its state // `EspPersist`+`EspKvBlobStore` saves to a user-supplied NVS partition // under namespace `esp-idf-matter` - DummyPersist, - //EspPersist::new_wifi_ble(EspKvBlobStore::new_default(nvs.clone())?, stack), + persist::new_default(nvs, stack)?, // Our `AsyncHandler` + `AsyncMetadata` impl (NODE, handler), // No user future to run diff --git a/examples/light_eth.rs b/examples/light_eth.rs index 9d112f1..ff4d94e 100644 --- a/examples/light_eth.rs +++ b/examples/light_eth.rs @@ -13,6 +13,7 @@ use embassy_futures::select::select; use embassy_time::{Duration, Timer}; use esp_idf_matter::netif::EspMatterNetif; +use esp_idf_matter::persist; use esp_idf_matter::{init_async_io, EspEthMatterStack}; use esp_idf_svc::eventloop::EspSystemEventLoop; @@ -36,7 +37,6 @@ use rs_matter::utils::init::InitMaybeUninit; use rs_matter::utils::select::Coalesce; use rs_matter::BasicCommData; -use rs_matter_stack::persist::DummyPersist; use static_cell::StaticCell; @@ -163,8 +163,7 @@ async fn matter() -> Result<(), anyhow::Error> { // The Matter stack needs a persister to store its state // `EspPersist`+`EspKvBlobStore` saves to a user-supplied NVS partition // under namespace `esp-idf-matter` - DummyPersist, - //EspPersist::new_eth(EspKvBlobStore::new_default(nvs.clone())?, stack), + persist::new_default(nvs, stack)?, // Our `AsyncHandler` + `AsyncMetadata` impl (NODE, handler), // No user future to run diff --git a/src/persist.rs b/src/persist.rs index d5cb882..fc68fa9 100644 --- a/src/persist.rs +++ b/src/persist.rs @@ -5,13 +5,56 @@ use log::info; use rs_matter::error::Error; -use rs_matter_stack::persist::{Key, KvBlobStore, KvPersist}; +use rs_matter_stack::network::{Embedding, Network}; +use rs_matter_stack::persist::{Key, KvBlobBuf, KvBlobStore, KvPersist}; +use rs_matter_stack::MatterStack; use crate::error::to_persist_error; /// A type alias for a `KvPersist` instance that uses the ESP IDF NVS API pub type EspMatterPersist<'a, T, C> = KvPersist<'a, EspKvBlobStore, C>; +/// Create a new ESP-IDF Matter persist instance that would persist in namespace `esp-idf-matter`. +/// +/// # Arguments +/// - `nvs`: The NVS partition to use for persisting data. +/// - `stack`: The Matter stack instance. +pub fn new_default<'a, T, N, Q>( + nvs: EspNvsPartition, + stack: &'a MatterStack<'a, N>, +) -> Result>, EspError> +where + T: NvsPartitionId, + N: Network>, + Q: Embedding + 'a, +{ + new(nvs, "esp-idf-matter", stack) +} + +/// Create a new ESP-IDF Matter persist instance. +/// +/// # Arguments +/// - `nvs`: The NVS partition to use for persisting data. +/// - `namespace`: The namespace to use for persisting data. +/// - `stack`: The Matter stack instance. +pub fn new<'a, T, N, Q>( + nvs: EspNvsPartition, + namespace: &str, + stack: &'a MatterStack<'a, N>, +) -> Result>, EspError> +where + T: NvsPartitionId, + N: Network>, + Q: Embedding + 'a, +{ + Ok(EspMatterPersist::wrap( + EspKvBlobStore::new(nvs, namespace)?, + stack.network().embedding().buf(), + stack.matter(), + stack.network().persist_context(), + )) +} + /// A `KvBlobStore`` implementation that uses the ESP IDF NVS API /// to store and load the BLOBs. ///