Skip to content

Commit

Permalink
Twist persist API to be a better match for baremetal
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Oct 6, 2024
1 parent 6bd0005 commit ba27ed0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 23 deletions.
7 changes: 7 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ pub fn to_net_error(_err: EspError) -> Error {
// TODO: Capture the backtrace and the original error
ErrorCode::NoNetworkInterface.into()
}

/// Converts an ESP persistence error to an `rs-matter` error
pub fn to_persist_error(_err: EspError) -> Error {
// TODO: The `rs-matter` error code is too generic
// TODO: Capture the backtrace and the original error
ErrorCode::StdIoError.into()
}
4 changes: 4 additions & 0 deletions src/eth.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use rs_matter_stack::{persist::KvBlobBuf, Eth, MatterStack};

/// A type alias for an ESP-IDF Matter stack running over an Ethernet network (or any other network not managed by Matter).
pub type EspEthMatterStack<'a, E> = MatterStack<'a, EspEth<E>>;

/// A type alias for an ESP-IDF implementation of the `Network` trait for a Matter stack running over
/// an Ethernet network (or any other network not managed by Matter).
pub type EspEth<E> = Eth<KvBlobBuf<E>>;
63 changes: 40 additions & 23 deletions src/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ use esp_idf_svc::sys::EspError;

use log::info;

use rs_matter::error::{Error, ErrorCode};
use rs_matter::error::Error;

use rs_matter_stack::persist::{Key, KvBlobStore, KvPersist};

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>;

/// A `KvBlobStore`` implementation that uses the ESP IDF NVS API
Expand All @@ -21,42 +24,57 @@ impl<T> EspKvBlobStore<T>
where
T: NvsPartitionId,
{
/// Create a new PSM instance that would persist in namespace `esp-idf-matter`.
/// Create a new KV BLOB store instance that would persist in namespace `esp-idf-matter`.
pub fn new_default(nvs: EspNvsPartition<T>) -> Result<Self, EspError> {
Self::new(nvs, "esp-idf-matter")
}

/// Create a new PSM instance.
/// Create a new KV BLOB store instance.
pub fn new(nvs: EspNvsPartition<T>, namespace: &str) -> Result<Self, EspError> {
Ok(Self(EspNvs::new(nvs, namespace, true)?))
}

fn load_blob<'b>(&self, key: Key, buf: &'b mut [u8]) -> Result<Option<&'b [u8]>, EspError> {
fn load<F>(&self, key: Key, buf: &mut [u8], cb: F) -> Result<(), Error>
where
F: FnOnce(Option<&[u8]>) -> Result<(), Error>,
{
// TODO: Not really async

let data = self.0.get_blob(key.as_ref(), buf)?;
let data = self
.0
.get_blob(key.as_ref(), buf)
.map_err(to_persist_error)?;

info!(
"Blob {key}: loaded {:?} bytes {data:?}",
data.map(|data| data.len())
);

Ok(data)
cb(data)
}

fn store_blob(&mut self, key: Key, data: &[u8]) -> Result<(), EspError> {
fn store<F>(&mut self, key: Key, buf: &mut [u8], cb: F) -> Result<(), Error>
where
F: FnOnce(&mut [u8]) -> Result<usize, Error>,
{
// TODO: Not really async

self.0.set_blob(key.as_ref(), data)?;
let len = cb(buf)?;
let data = &buf[..len];

self.0
.set_blob(key.as_ref(), data)
.map_err(to_persist_error)?;

info!("Blob {key}: stored {} bytes {data:?}", data.len());

Ok(())
}

fn remove_blob(&mut self, key: Key) -> Result<(), EspError> {
fn remove(&mut self, key: Key, _buf: &mut [u8]) -> Result<(), Error> {
// TODO: Not really async

self.0.remove(key.as_ref())?;
self.0.remove(key.as_ref()).map_err(to_persist_error)?;

info!("Blob {key}: removed");

Expand All @@ -68,22 +86,21 @@ impl<T> KvBlobStore for EspKvBlobStore<T>
where
T: NvsPartitionId,
{
async fn load<'a>(&mut self, key: Key, buf: &'a mut [u8]) -> Result<Option<&'a [u8]>, Error> {
Ok(self
.load_blob(key, buf)
.map_err(|_| ErrorCode::StdIoError)?) // TODO: We need a dedicated PersistError code here
async fn load<F>(&mut self, key: Key, buf: &mut [u8], cb: F) -> Result<(), Error>
where
F: FnOnce(Option<&[u8]>) -> Result<(), Error>,
{
EspKvBlobStore::load(self, key, buf, cb)
}

async fn store(&mut self, key: Key, value: &[u8]) -> Result<(), Error> {
self.store_blob(key, value)
.map_err(|_| ErrorCode::StdIoError)?;

Ok(())
async fn store<F>(&mut self, key: Key, buf: &mut [u8], cb: F) -> Result<(), Error>
where
F: FnOnce(&mut [u8]) -> Result<usize, Error>,
{
EspKvBlobStore::store(self, key, buf, cb)
}

async fn remove(&mut self, key: Key) -> Result<(), Error> {
self.remove_blob(key).map_err(|_| ErrorCode::StdIoError)?;

Ok(())
async fn remove(&mut self, key: Key, buf: &mut [u8]) -> Result<(), Error> {
EspKvBlobStore::remove(self, key, buf)
}
}

0 comments on commit ba27ed0

Please sign in to comment.