Skip to content

Commit

Permalink
Persist sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Nov 29, 2024
1 parent e1c6f1b commit 77c9ff7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions examples/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions examples/light_eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down
45 changes: 44 additions & 1 deletion src/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>, 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<T>,
stack: &'a MatterStack<'a, N>,
) -> Result<EspMatterPersist<'a, T, N::PersistContext<'a>>, EspError>
where
T: NvsPartitionId,
N: Network<Embedding = KvBlobBuf<Q>>,
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<T>,
namespace: &str,
stack: &'a MatterStack<'a, N>,
) -> Result<EspMatterPersist<'a, T, N::PersistContext<'a>>, EspError>
where
T: NvsPartitionId,
N: Network<Embedding = KvBlobBuf<Q>>,
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.
///
Expand Down

0 comments on commit 77c9ff7

Please sign in to comment.