on_off_light: Save ACLs and Fabrics to PSM
This commit is contained in:
		
							parent
							
								
									dab51ce911
								
							
						
					
					
						commit
						84f7b5f5de
					
				
					 4 changed files with 60 additions and 36 deletions
				
			
		| 
						 | 
					@ -18,7 +18,7 @@
 | 
				
			||||||
use core::borrow::Borrow;
 | 
					use core::borrow::Borrow;
 | 
				
			||||||
use core::pin::pin;
 | 
					use core::pin::pin;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use embassy_futures::select::select;
 | 
					use embassy_futures::select::select3;
 | 
				
			||||||
use log::info;
 | 
					use log::info;
 | 
				
			||||||
use matter::core::{CommissioningData, Matter};
 | 
					use matter::core::{CommissioningData, Matter};
 | 
				
			||||||
use matter::data_model::cluster_basic_information::BasicInfoConfig;
 | 
					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::data_model::system_model::descriptor;
 | 
				
			||||||
use matter::error::Error;
 | 
					use matter::error::Error;
 | 
				
			||||||
use matter::mdns::{DefaultMdns, DefaultMdnsRunner};
 | 
					use matter::mdns::{DefaultMdns, DefaultMdnsRunner};
 | 
				
			||||||
 | 
					use matter::persist::FilePsm;
 | 
				
			||||||
use matter::secure_channel::spake2p::VerifierData;
 | 
					use matter::secure_channel::spake2p::VerifierData;
 | 
				
			||||||
use matter::transport::network::{Ipv4Addr, Ipv6Addr};
 | 
					use matter::transport::network::{Ipv4Addr, Ipv6Addr};
 | 
				
			||||||
use matter::transport::runner::{RxBuf, TransportRunner, TxBuf};
 | 
					use matter::transport::runner::{RxBuf, TransportRunner, TxBuf};
 | 
				
			||||||
| 
						 | 
					@ -73,6 +74,12 @@ fn run() -> Result<(), Error> {
 | 
				
			||||||
        device_name: "OnOff Light",
 | 
					        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 (ipv4_addr, ipv6_addr, interface) = initialize_network()?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mdns = DefaultMdns::new(
 | 
					    let mdns = DefaultMdns::new(
 | 
				
			||||||
| 
						 | 
					@ -124,16 +131,18 @@ fn run() -> Result<(), Error> {
 | 
				
			||||||
    let mut tx_buf = TxBuf::uninit();
 | 
					    let mut tx_buf = TxBuf::uninit();
 | 
				
			||||||
    let mut rx_buf = RxBuf::uninit();
 | 
					    let mut rx_buf = RxBuf::uninit();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // #[cfg(all(feature = "std", not(target_os = "espidf")))]
 | 
					    #[cfg(all(feature = "std", not(target_os = "espidf")))]
 | 
				
			||||||
    // {
 | 
					    {
 | 
				
			||||||
    //     if let Some(data) = psm.load("acls", buf)? {
 | 
					        let mut buf = [0; 4096];
 | 
				
			||||||
    //         matter.load_acls(data)?;
 | 
					        let buf = &mut buf;
 | 
				
			||||||
    //     }
 | 
					        if let Some(data) = psm.load("acls", buf)? {
 | 
				
			||||||
 | 
					            matter.load_acls(data)?;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //     if let Some(data) = psm.load("fabrics", buf)? {
 | 
					        if let Some(data) = psm.load("fabrics", buf)? {
 | 
				
			||||||
    //         matter.load_fabrics(data)?;
 | 
					            matter.load_fabrics(data)?;
 | 
				
			||||||
    //     }
 | 
					        }
 | 
				
			||||||
    // }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let node = Node {
 | 
					    let node = Node {
 | 
				
			||||||
        id: 0,
 | 
					        id: 0,
 | 
				
			||||||
| 
						 | 
					@ -179,13 +188,8 @@ fn run() -> Result<(), Error> {
 | 
				
			||||||
        // connect the pipes of the `run` method with your own UDP stack
 | 
					        // connect the pipes of the `run` method with your own UDP stack
 | 
				
			||||||
        let mut mdns = pin!(mdns_runner.run_udp());
 | 
					        let mut mdns = pin!(mdns_runner.run_udp());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        select(
 | 
					        let mut save = pin!(save(matter, &psm));
 | 
				
			||||||
            &mut transport,
 | 
					        select3(&mut transport, &mut mdns, &mut save).await.unwrap()
 | 
				
			||||||
            &mut mdns,
 | 
					 | 
				
			||||||
            //save(transport, &psm),
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
        .await
 | 
					 | 
				
			||||||
        .unwrap()
 | 
					 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // NOTE: For no_std, replace with your own no_std way of polling the future
 | 
					    // 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 _))
 | 
					    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")]
 | 
					#[cfg(target_os = "espidf")]
 | 
				
			||||||
#[inline(never)]
 | 
					#[inline(never)]
 | 
				
			||||||
fn initialize_logger() {
 | 
					fn initialize_logger() {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -28,6 +28,7 @@ use crate::{
 | 
				
			||||||
    mdns::Mdns,
 | 
					    mdns::Mdns,
 | 
				
			||||||
    pairing::{print_pairing_code_and_qr, DiscoveryCapabilities},
 | 
					    pairing::{print_pairing_code_and_qr, DiscoveryCapabilities},
 | 
				
			||||||
    secure_channel::{pake::PaseMgr, spake2p::VerifierData},
 | 
					    secure_channel::{pake::PaseMgr, spake2p::VerifierData},
 | 
				
			||||||
 | 
					    transport::exchange::Notification,
 | 
				
			||||||
    utils::{epoch::Epoch, rand::Rand},
 | 
					    utils::{epoch::Epoch, rand::Rand},
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -48,6 +49,7 @@ pub struct Matter<'a> {
 | 
				
			||||||
    pub acl_mgr: RefCell<AclMgr>,
 | 
					    pub acl_mgr: RefCell<AclMgr>,
 | 
				
			||||||
    pub pase_mgr: RefCell<PaseMgr>,
 | 
					    pub pase_mgr: RefCell<PaseMgr>,
 | 
				
			||||||
    pub failsafe: RefCell<FailSafe>,
 | 
					    pub failsafe: RefCell<FailSafe>,
 | 
				
			||||||
 | 
					    pub persist_notification: Notification,
 | 
				
			||||||
    pub mdns: &'a dyn Mdns,
 | 
					    pub mdns: &'a dyn Mdns,
 | 
				
			||||||
    pub epoch: Epoch,
 | 
					    pub epoch: Epoch,
 | 
				
			||||||
    pub rand: Rand,
 | 
					    pub rand: Rand,
 | 
				
			||||||
| 
						 | 
					@ -91,6 +93,7 @@ impl<'a> Matter<'a> {
 | 
				
			||||||
            acl_mgr: RefCell::new(AclMgr::new()),
 | 
					            acl_mgr: RefCell::new(AclMgr::new()),
 | 
				
			||||||
            pase_mgr: RefCell::new(PaseMgr::new(epoch, rand)),
 | 
					            pase_mgr: RefCell::new(PaseMgr::new(epoch, rand)),
 | 
				
			||||||
            failsafe: RefCell::new(FailSafe::new()),
 | 
					            failsafe: RefCell::new(FailSafe::new()),
 | 
				
			||||||
 | 
					            persist_notification: Notification::new(),
 | 
				
			||||||
            mdns,
 | 
					            mdns,
 | 
				
			||||||
            epoch,
 | 
					            epoch,
 | 
				
			||||||
            rand,
 | 
					            rand,
 | 
				
			||||||
| 
						 | 
					@ -157,6 +160,15 @@ impl<'a> Matter<'a> {
 | 
				
			||||||
            Ok(false)
 | 
					            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<RefCell<FabricMgr>> for Matter<'a> {
 | 
					impl<'a> Borrow<RefCell<FabricMgr>> for Matter<'a> {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -58,7 +58,6 @@ pub struct Transport<'a> {
 | 
				
			||||||
    matter: &'a Matter<'a>,
 | 
					    matter: &'a Matter<'a>,
 | 
				
			||||||
    pub(crate) exchanges: RefCell<heapless::Vec<ExchangeCtx, MAX_EXCHANGES>>,
 | 
					    pub(crate) exchanges: RefCell<heapless::Vec<ExchangeCtx, MAX_EXCHANGES>>,
 | 
				
			||||||
    pub(crate) send_notification: Notification,
 | 
					    pub(crate) send_notification: Notification,
 | 
				
			||||||
    pub(crate) persist_notification: Notification,
 | 
					 | 
				
			||||||
    pub session_mgr: RefCell<SessionMgr>,
 | 
					    pub session_mgr: RefCell<SessionMgr>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -72,7 +71,6 @@ impl<'a> Transport<'a> {
 | 
				
			||||||
            matter,
 | 
					            matter,
 | 
				
			||||||
            exchanges: RefCell::new(heapless::Vec::new()),
 | 
					            exchanges: RefCell::new(heapless::Vec::new()),
 | 
				
			||||||
            send_notification: Notification::new(),
 | 
					            send_notification: Notification::new(),
 | 
				
			||||||
            persist_notification: Notification::new(),
 | 
					 | 
				
			||||||
            session_mgr: RefCell::new(SessionMgr::new(epoch, rand)),
 | 
					            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,
 | 
					                construction_notification,
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            self.notify_changed();
 | 
					            self.matter().notify_changed();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Ok(Some(constructor))
 | 
					            Ok(Some(constructor))
 | 
				
			||||||
        } else if src_rx.proto.proto_id == PROTO_ID_SECURE_CHANNEL
 | 
					        } 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)
 | 
					            Ok(None)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					@ -232,7 +230,7 @@ impl<'a> Transport<'a> {
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if let Some(ctx) = ctx {
 | 
					        if let Some(ctx) = ctx {
 | 
				
			||||||
            self.notify_changed();
 | 
					            self.matter().notify_changed();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            let state = &mut ctx.state;
 | 
					            let state = &mut ctx.state;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -291,7 +289,7 @@ impl<'a> Transport<'a> {
 | 
				
			||||||
                dest_tx.log("Sending packet");
 | 
					                dest_tx.log("Sending packet");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                self.pre_send(ctx, dest_tx)?;
 | 
					                self.pre_send(ctx, dest_tx)?;
 | 
				
			||||||
                self.notify_changed();
 | 
					                self.matter().notify_changed();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                return Ok(true);
 | 
					                return Ok(true);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
| 
						 | 
					@ -414,14 +412,4 @@ impl<'a> Transport<'a> {
 | 
				
			||||||
    ) -> Option<&'r mut ExchangeCtx> {
 | 
					    ) -> Option<&'r mut ExchangeCtx> {
 | 
				
			||||||
        exchanges.iter_mut().find(|exchange| exchange.id == *id)
 | 
					        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
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -370,7 +370,7 @@ impl<'a> TransportRunner<'a> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                sc.handle(&mut exchange, &mut rx, &mut tx).await?;
 | 
					                sc.handle(&mut exchange, &mut rx, &mut tx).await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                transport.notify_changed();
 | 
					                transport.matter().notify_changed();
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            PROTO_ID_INTERACTION_MODEL => {
 | 
					            PROTO_ID_INTERACTION_MODEL => {
 | 
				
			||||||
                let dm = DataModel::new(handler);
 | 
					                let dm = DataModel::new(handler);
 | 
				
			||||||
| 
						 | 
					@ -380,7 +380,7 @@ impl<'a> TransportRunner<'a> {
 | 
				
			||||||
                dm.handle(&mut exchange, &mut rx, &mut tx, &mut rx_status)
 | 
					                dm.handle(&mut exchange, &mut rx, &mut tx, &mut rx_status)
 | 
				
			||||||
                    .await?;
 | 
					                    .await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                transport.notify_changed();
 | 
					                transport.matter().notify_changed();
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            other => {
 | 
					            other => {
 | 
				
			||||||
                error!("Unknown Proto-ID: {}", other);
 | 
					                error!("Unknown Proto-ID: {}", other);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue