Skip to content

Commit 762438c

Browse files
kedarsivmarkov
authored andcommitted
on_off_light: Save ACLs and Fabrics to PSM
1 parent 9576fd8 commit 762438c

File tree

4 files changed

+61
-37
lines changed

4 files changed

+61
-37
lines changed

examples/onoff_light/src/main.rs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use core::borrow::Borrow;
1919
use core::pin::pin;
2020

21-
use embassy_futures::select::select;
21+
use embassy_futures::select::select3;
2222
use log::info;
2323
use matter::core::{CommissioningData, Matter};
2424
use matter::data_model::cluster_basic_information::BasicInfoConfig;
@@ -29,6 +29,7 @@ use matter::data_model::root_endpoint;
2929
use matter::data_model::system_model::descriptor;
3030
use matter::error::Error;
3131
use matter::mdns::{DefaultMdns, DefaultMdnsRunner};
32+
use matter::persist::FilePsm;
3233
use matter::secure_channel::spake2p::VerifierData;
3334
use matter::transport::network::{Ipv4Addr, Ipv6Addr};
3435
use matter::transport::runner::{RxBuf, TransportRunner, TxBuf};
@@ -73,6 +74,12 @@ fn run() -> Result<(), Error> {
7374
device_name: "OnOff Light",
7475
};
7576

77+
let psm_path = std::env::temp_dir().join("matter-iot");
78+
info!("Persisting from/to {}", psm_path.display());
79+
80+
#[cfg(all(feature = "std", not(target_os = "espidf")))]
81+
let psm = matter::persist::FilePsm::new(psm_path)?;
82+
7683
let (ipv4_addr, ipv6_addr, interface) = initialize_network()?;
7784

7885
let mdns = DefaultMdns::new(
@@ -124,16 +131,18 @@ fn run() -> Result<(), Error> {
124131
let mut tx_buf = TxBuf::uninit();
125132
let mut rx_buf = RxBuf::uninit();
126133

127-
// #[cfg(all(feature = "std", not(target_os = "espidf")))]
128-
// {
129-
// if let Some(data) = psm.load("acls", buf)? {
130-
// matter.load_acls(data)?;
131-
// }
132-
133-
// if let Some(data) = psm.load("fabrics", buf)? {
134-
// matter.load_fabrics(data)?;
135-
// }
136-
// }
134+
#[cfg(all(feature = "std", not(target_os = "espidf")))]
135+
{
136+
let mut buf = [0; 4096];
137+
let buf = &mut buf;
138+
if let Some(data) = psm.load("acls", buf)? {
139+
matter.load_acls(data)?;
140+
}
141+
142+
if let Some(data) = psm.load("fabrics", buf)? {
143+
matter.load_fabrics(data)?;
144+
}
145+
}
137146

138147
let node = Node {
139148
id: 0,
@@ -179,13 +188,8 @@ fn run() -> Result<(), Error> {
179188
// connect the pipes of the `run` method with your own UDP stack
180189
let mut mdns = pin!(mdns_runner.run_udp());
181190

182-
select(
183-
&mut transport,
184-
&mut mdns,
185-
//save(transport, &psm),
186-
)
187-
.await
188-
.unwrap()
191+
let mut save = pin!(save(matter, &psm));
192+
select3(&mut transport, &mut mdns, &mut save).await.unwrap()
189193
});
190194

191195
// 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> {
299303
Ok((ip, ipv6, 0 as _))
300304
}
301305

306+
#[cfg(all(feature = "std", not(target_os = "espidf")))]
307+
#[inline(never)]
308+
async fn save(matter: &Matter<'_>, psm: &FilePsm) -> Result<(), Error> {
309+
let mut buf = [0; 4096];
310+
let buf = &mut buf;
311+
312+
loop {
313+
matter.wait_changed().await;
314+
if matter.is_changed() {
315+
if let Some(data) = matter.store_acls(buf)? {
316+
psm.store("acls", data)?;
317+
}
318+
319+
if let Some(data) = matter.store_fabrics(buf)? {
320+
psm.store("fabrics", data)?;
321+
}
322+
}
323+
}
324+
}
325+
302326
#[cfg(target_os = "espidf")]
303327
#[inline(never)]
304328
fn initialize_logger() {

matter/src/core.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::{
2828
mdns::Mdns,
2929
pairing::{print_pairing_code_and_qr, DiscoveryCapabilities},
3030
secure_channel::{pake::PaseMgr, spake2p::VerifierData},
31+
transport::exchange::Notification,
3132
utils::{epoch::Epoch, rand::Rand},
3233
};
3334

@@ -48,6 +49,7 @@ pub struct Matter<'a> {
4849
pub acl_mgr: RefCell<AclMgr>,
4950
pub pase_mgr: RefCell<PaseMgr>,
5051
pub failsafe: RefCell<FailSafe>,
52+
pub persist_notification: Notification,
5153
pub mdns: &'a dyn Mdns,
5254
pub epoch: Epoch,
5355
pub rand: Rand,
@@ -91,6 +93,7 @@ impl<'a> Matter<'a> {
9193
acl_mgr: RefCell::new(AclMgr::new()),
9294
pase_mgr: RefCell::new(PaseMgr::new(epoch, rand)),
9395
failsafe: RefCell::new(FailSafe::new()),
96+
persist_notification: Notification::new(),
9497
mdns,
9598
epoch,
9699
rand,
@@ -157,6 +160,15 @@ impl<'a> Matter<'a> {
157160
Ok(false)
158161
}
159162
}
163+
pub fn notify_changed(&self) {
164+
if self.is_changed() {
165+
self.persist_notification.signal(());
166+
}
167+
}
168+
169+
pub async fn wait_changed(&self) {
170+
self.persist_notification.wait().await
171+
}
160172
}
161173

162174
impl<'a> Borrow<RefCell<FabricMgr>> for Matter<'a> {

matter/src/transport/core.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ pub struct Transport<'a> {
5858
matter: &'a Matter<'a>,
5959
pub(crate) exchanges: RefCell<heapless::Vec<ExchangeCtx, MAX_EXCHANGES>>,
6060
pub(crate) send_notification: Notification,
61-
pub(crate) persist_notification: Notification,
6261
pub session_mgr: RefCell<SessionMgr>,
6362
}
6463

@@ -72,7 +71,6 @@ impl<'a> Transport<'a> {
7271
matter,
7372
exchanges: RefCell::new(heapless::Vec::new()),
7473
send_notification: Notification::new(),
75-
persist_notification: Notification::new(),
7674
session_mgr: RefCell::new(SessionMgr::new(epoch, rand)),
7775
}
7876
}
@@ -128,7 +126,7 @@ impl<'a> Transport<'a> {
128126
}
129127
}
130128

131-
self.notify_changed();
129+
self.matter().notify_changed();
132130
}
133131
}
134132

@@ -142,7 +140,7 @@ impl<'a> Transport<'a> {
142140
construction_notification,
143141
};
144142

145-
self.notify_changed();
143+
self.matter().notify_changed();
146144

147145
Ok(Some(constructor))
148146
} else if src_rx.proto.proto_id == PROTO_ID_SECURE_CHANNEL
@@ -169,7 +167,7 @@ impl<'a> Transport<'a> {
169167
}
170168
}
171169

172-
self.notify_changed();
170+
self.matter().notify_changed();
173171

174172
Ok(None)
175173
}
@@ -232,7 +230,7 @@ impl<'a> Transport<'a> {
232230
});
233231

234232
if let Some(ctx) = ctx {
235-
self.notify_changed();
233+
self.matter().notify_changed();
236234

237235
let state = &mut ctx.state;
238236

@@ -291,7 +289,7 @@ impl<'a> Transport<'a> {
291289
dest_tx.log("Sending packet");
292290

293291
self.pre_send(ctx, dest_tx)?;
294-
self.notify_changed();
292+
self.matter().notify_changed();
295293

296294
return Ok(true);
297295
}
@@ -414,14 +412,4 @@ impl<'a> Transport<'a> {
414412
) -> Option<&'r mut ExchangeCtx> {
415413
exchanges.iter_mut().find(|exchange| exchange.id == *id)
416414
}
417-
418-
pub fn notify_changed(&self) {
419-
if self.matter().is_changed() {
420-
self.persist_notification.signal(());
421-
}
422-
}
423-
424-
pub async fn wait_changed(&self) {
425-
self.persist_notification.wait().await
426-
}
427415
}

matter/src/transport/runner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ impl<'a> TransportRunner<'a> {
370370

371371
sc.handle(&mut exchange, &mut rx, &mut tx).await?;
372372

373-
transport.notify_changed();
373+
transport.matter().notify_changed();
374374
}
375375
PROTO_ID_INTERACTION_MODEL => {
376376
let dm = DataModel::new(handler);
@@ -380,7 +380,7 @@ impl<'a> TransportRunner<'a> {
380380
dm.handle(&mut exchange, &mut rx, &mut tx, &mut rx_status)
381381
.await?;
382382

383-
transport.notify_changed();
383+
transport.matter().notify_changed();
384384
}
385385
other => {
386386
error!("Unknown Proto-ID: {}", other);

0 commit comments

Comments
 (0)