From 15e94cf9935085d34af8527ee5de88fd5a30d714 Mon Sep 17 00:00:00 2001 From: ivmarkov Date: Mon, 27 May 2024 09:53:54 +0000 Subject: [PATCH] A buffer for kc blob stores --- src/persist.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/persist.rs b/src/persist.rs index 62eca90..5f12272 100644 --- a/src/persist.rs +++ b/src/persist.rs @@ -1,8 +1,11 @@ use embassy_futures::select::select; use embassy_sync::blocking_mutex::raw::{NoopRawMutex, RawMutex}; -use rs_matter::error::Error; use rs_matter::Matter; +use rs_matter::{ + error::Error, + utils::buf::{BufferAccess, PooledBuffer, PooledBuffers}, +}; use crate::{network::Embedding, wifi::WifiContext, Eth, MatterStack, WifiBle, MAX_WIFI_NETWORKS}; @@ -338,3 +341,53 @@ impl KvBlobStore for DirKvStore { DirKvStore::remove(self, key) } } + +const KV_BLOB_BUF_SIZE: usize = 4096; + +/// A buffer for the `KvBlobStore` trait. +pub type KvBlobBuffer = heapless::Vec; + +/// An embedding of the buffer necessary for the `KvBlobStore` trait. +/// Allows the memory of this buffer to be statically allocated and cost-initialized. +/// +/// Usage: +/// ```no_run +/// MatterStack>>::new(); +/// ``` +/// or: +/// ```no_run +/// MatterStack>>::new(); +/// ``` +/// +/// ... where `E` can be a next-level, user-supplied embedding or just `()` if the user does not need to embed anything. +pub struct KvBlobBuf { + buf: PooledBuffers<1, NoopRawMutex, KvBlobBuffer>, + embedding: E, +} + +impl KvBlobBuf +where + E: Embedding, +{ + const fn new() -> Self { + Self { + buf: PooledBuffers::new(0), + embedding: E::INIT, + } + } + + pub async fn buf(&self) -> PooledBuffer<'_, 1, NoopRawMutex, KvBlobBuffer> { + self.buf.get().await.unwrap() + } + + pub fn embedding(&self) -> &E { + &self.embedding + } +} + +impl Embedding for KvBlobBuf +where + E: Embedding, +{ + const INIT: Self = Self::new(); +}