From 84f7b5f5de417c4edf966b915f26273386f0665f Mon Sep 17 00:00:00 2001 From: Kedar Sovani Date: Thu, 20 Jul 2023 10:13:46 +0530 Subject: [PATCH] on_off_light: Save ACLs and Fabrics to PSM --- examples/onoff_light/src/main.rs | 58 ++++++++++++++++++++++---------- matter/src/core.rs | 12 +++++++ matter/src/transport/core.rs | 22 +++--------- matter/src/transport/runner.rs | 4 +-- 4 files changed, 60 insertions(+), 36 deletions(-) diff --git a/examples/onoff_light/src/main.rs b/examples/onoff_light/src/main.rs index ecfc71e..627eda6 100644 --- a/examples/onoff_light/src/main.rs +++ b/examples/onoff_light/src/main.rs @@ -18,7 +18,7 @@ use core::borrow::Borrow; use core::pin::pin; -use embassy_futures::select::select; +use embassy_futures::select::select3; use log::info; use matter::core::{CommissioningData, Matter}; use matter::data_model::cluster_basic_information::BasicInfoConfig; @@ -29,6 +29,7 @@ use matter::data_model::root_endpoint; use matter::data_model::system_model::descriptor; use matter::error::Error; use matter::mdns::{DefaultMdns, DefaultMdnsRunner}; +use matter::persist::FilePsm; use matter::secure_channel::spake2p::VerifierData; use matter::transport::network::{Ipv4Addr, Ipv6Addr}; use matter::transport::runner::{RxBuf, TransportRunner, TxBuf}; @@ -73,6 +74,12 @@ fn run() -> Result<(), Error> { device_name: "OnOff Light", }; + let psm_path = std::env::temp_dir().join("matter-iot"); + info!("Persisting from/to {}", psm_path.display()); + + #[cfg(all(feature = "std", not(target_os = "espidf")))] + let psm = matter::persist::FilePsm::new(psm_path)?; + let (ipv4_addr, ipv6_addr, interface) = initialize_network()?; let mdns = DefaultMdns::new( @@ -124,16 +131,18 @@ fn run() -> Result<(), Error> { let mut tx_buf = TxBuf::uninit(); let mut rx_buf = RxBuf::uninit(); - // #[cfg(all(feature = "std", not(target_os = "espidf")))] - // { - // if let Some(data) = psm.load("acls", buf)? { - // matter.load_acls(data)?; - // } + #[cfg(all(feature = "std", not(target_os = "espidf")))] + { + let mut buf = [0; 4096]; + let buf = &mut buf; + if let Some(data) = psm.load("acls", buf)? { + matter.load_acls(data)?; + } - // if let Some(data) = psm.load("fabrics", buf)? { - // matter.load_fabrics(data)?; - // } - // } + if let Some(data) = psm.load("fabrics", buf)? { + matter.load_fabrics(data)?; + } + } let node = Node { id: 0, @@ -179,13 +188,8 @@ fn run() -> Result<(), Error> { // connect the pipes of the `run` method with your own UDP stack let mut mdns = pin!(mdns_runner.run_udp()); - select( - &mut transport, - &mut mdns, - //save(transport, &psm), - ) - .await - .unwrap() + let mut save = pin!(save(matter, &psm)); + select3(&mut transport, &mut mdns, &mut save).await.unwrap() }); // NOTE: For no_std, replace with your own no_std way of polling the future @@ -299,6 +303,26 @@ fn initialize_network() -> Result<(Ipv4Addr, Ipv6Addr, u32), Error> { Ok((ip, ipv6, 0 as _)) } +#[cfg(all(feature = "std", not(target_os = "espidf")))] +#[inline(never)] +async fn save(matter: &Matter<'_>, psm: &FilePsm) -> Result<(), Error> { + let mut buf = [0; 4096]; + let buf = &mut buf; + + loop { + matter.wait_changed().await; + if matter.is_changed() { + if let Some(data) = matter.store_acls(buf)? { + psm.store("acls", data)?; + } + + if let Some(data) = matter.store_fabrics(buf)? { + psm.store("fabrics", data)?; + } + } + } +} + #[cfg(target_os = "espidf")] #[inline(never)] fn initialize_logger() { diff --git a/matter/src/core.rs b/matter/src/core.rs index 35c8677..13c0930 100644 --- a/matter/src/core.rs +++ b/matter/src/core.rs @@ -28,6 +28,7 @@ use crate::{ mdns::Mdns, pairing::{print_pairing_code_and_qr, DiscoveryCapabilities}, secure_channel::{pake::PaseMgr, spake2p::VerifierData}, + transport::exchange::Notification, utils::{epoch::Epoch, rand::Rand}, }; @@ -48,6 +49,7 @@ pub struct Matter<'a> { pub acl_mgr: RefCell, pub pase_mgr: RefCell, pub failsafe: RefCell, + pub persist_notification: Notification, pub mdns: &'a dyn Mdns, pub epoch: Epoch, pub rand: Rand, @@ -91,6 +93,7 @@ impl<'a> Matter<'a> { acl_mgr: RefCell::new(AclMgr::new()), pase_mgr: RefCell::new(PaseMgr::new(epoch, rand)), failsafe: RefCell::new(FailSafe::new()), + persist_notification: Notification::new(), mdns, epoch, rand, @@ -157,6 +160,15 @@ impl<'a> Matter<'a> { Ok(false) } } + pub fn notify_changed(&self) { + if self.is_changed() { + self.persist_notification.signal(()); + } + } + + pub async fn wait_changed(&self) { + self.persist_notification.wait().await + } } impl<'a> Borrow> for Matter<'a> { diff --git a/matter/src/transport/core.rs b/matter/src/transport/core.rs index 2a54b4a..98c2fba 100644 --- a/matter/src/transport/core.rs +++ b/matter/src/transport/core.rs @@ -58,7 +58,6 @@ pub struct Transport<'a> { matter: &'a Matter<'a>, pub(crate) exchanges: RefCell>, pub(crate) send_notification: Notification, - pub(crate) persist_notification: Notification, pub session_mgr: RefCell, } @@ -72,7 +71,6 @@ impl<'a> Transport<'a> { matter, exchanges: RefCell::new(heapless::Vec::new()), send_notification: Notification::new(), - persist_notification: Notification::new(), session_mgr: RefCell::new(SessionMgr::new(epoch, rand)), } } @@ -128,7 +126,7 @@ impl<'a> Transport<'a> { } } - self.notify_changed(); + self.matter().notify_changed(); } } @@ -142,7 +140,7 @@ impl<'a> Transport<'a> { construction_notification, }; - self.notify_changed(); + self.matter().notify_changed(); Ok(Some(constructor)) } else if src_rx.proto.proto_id == PROTO_ID_SECURE_CHANNEL @@ -169,7 +167,7 @@ impl<'a> Transport<'a> { } } - self.notify_changed(); + self.matter().notify_changed(); Ok(None) } @@ -232,7 +230,7 @@ impl<'a> Transport<'a> { }); if let Some(ctx) = ctx { - self.notify_changed(); + self.matter().notify_changed(); let state = &mut ctx.state; @@ -291,7 +289,7 @@ impl<'a> Transport<'a> { dest_tx.log("Sending packet"); self.pre_send(ctx, dest_tx)?; - self.notify_changed(); + self.matter().notify_changed(); return Ok(true); } @@ -414,14 +412,4 @@ impl<'a> Transport<'a> { ) -> Option<&'r mut ExchangeCtx> { exchanges.iter_mut().find(|exchange| exchange.id == *id) } - - pub fn notify_changed(&self) { - if self.matter().is_changed() { - self.persist_notification.signal(()); - } - } - - pub async fn wait_changed(&self) { - self.persist_notification.wait().await - } } diff --git a/matter/src/transport/runner.rs b/matter/src/transport/runner.rs index f94e819..c4cd4ed 100644 --- a/matter/src/transport/runner.rs +++ b/matter/src/transport/runner.rs @@ -370,7 +370,7 @@ impl<'a> TransportRunner<'a> { sc.handle(&mut exchange, &mut rx, &mut tx).await?; - transport.notify_changed(); + transport.matter().notify_changed(); } PROTO_ID_INTERACTION_MODEL => { let dm = DataModel::new(handler); @@ -380,7 +380,7 @@ impl<'a> TransportRunner<'a> { dm.handle(&mut exchange, &mut rx, &mut tx, &mut rx_status) .await?; - transport.notify_changed(); + transport.matter().notify_changed(); } other => { error!("Unknown Proto-ID: {}", other);