diff --git a/src/eth.rs b/src/eth.rs index 1aa2fc5..7af3e05 100644 --- a/src/eth.rs +++ b/src/eth.rs @@ -23,6 +23,7 @@ use rs_matter::utils::select::Coalesce; use crate::netif::Netif; use crate::network::{Embedding, Network}; use crate::persist::Persist; +use crate::private::Sealed; use crate::MatterStack; /// An implementation of the `Network` trait for Ethernet. @@ -39,6 +40,8 @@ pub struct Eth { embedding: E, } +impl Sealed for Eth {} + impl Network for Eth where E: Embedding + 'static, diff --git a/src/netif.rs b/src/netif.rs index 64ae6d3..30045df 100644 --- a/src/netif.rs +++ b/src/netif.rs @@ -46,7 +46,13 @@ where } } -/// A trait for running a network interface +/// A trait for running a network interface and possible the L2 layer below it +/// +/// Used by the Matter stack only when it "owns" the operational network, i.e. +/// when the operational network is wireless and is instantiated by the stack itself. +/// +/// Network instantiation by the Matter stack is mandatory for non-concurrent +/// commissioning and optional for concurrent commissioning. pub trait NetifRun { /// Run the network interface async fn run(&self) -> Result<(), Error>; diff --git a/src/network.rs b/src/network.rs index 8a27aba..ae98712 100644 --- a/src/network.rs +++ b/src/network.rs @@ -1,6 +1,7 @@ use rs_matter::utils::init::{init_from_closure, Init}; use crate::persist::NetworkPersist; +use crate::private::Sealed; /// User data that can be embedded in the stack network pub trait Embedding { @@ -19,7 +20,9 @@ impl Embedding for () { /// A trait modeling a specific network type. /// `MatterStack` is parameterized by a network type implementing this trait. -pub trait Network { +/// +/// The trait is sealed and has only two implementations: `Eth` and `WirelessBle`. +pub trait Network: Sealed { const INIT: Self; /// The network peristence context to be used by the `Persist` trait. diff --git a/src/persist.rs b/src/persist.rs index d8edbe9..6e5e46d 100644 --- a/src/persist.rs +++ b/src/persist.rs @@ -7,13 +7,18 @@ use rs_matter::utils::storage::pooled::{BufferAccess, PooledBuffers}; use rs_matter::Matter; use crate::network::{Embedding, Network}; +use crate::private::Sealed; use crate::MatterStack; #[cfg(feature = "std")] pub use file::DirKvBlobStore; /// A perist API that needs to be implemented by the network impl which is used in the Matter stack. -pub trait NetworkPersist { +/// +/// The trait is sealed and has only two implementations: +/// - `()` - which is used with the `Eth` network +/// - `&NetworkContext` - which is used with the `WirelessBle` network. +pub trait NetworkPersist: Sealed { /// Reset all networks, removing all stored data from the memory async fn reset(&mut self) -> Result<(), Error>; @@ -37,30 +42,9 @@ pub trait NetworkPersist { async fn wait_state_changed(&self); } -impl NetworkPersist for &mut T -where - T: NetworkPersist, -{ - async fn reset(&mut self) -> Result<(), Error> { - T::reset(self).await - } - - async fn load(&mut self, data: &[u8]) -> Result<(), Error> { - T::load(self, data).await - } - - async fn store<'a>(&mut self, buf: &'a mut [u8]) -> Result, Error> { - T::store(self, buf).await - } - - async fn wait_state_changed(&self) { - T::wait_state_changed(self).await - } -} - /// A no-op implementation of the `NetworksPersist` trait. /// -/// Useful when the Matter stack is configured for Ethernet, as in that case +/// Used when the Matter stack is configured for Ethernet, as in that case /// there is no network state that needs to be saved impl NetworkPersist for () { async fn reset(&mut self) -> Result<(), Error> { diff --git a/src/udp.rs b/src/udp.rs index f6d0976..a6993a0 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -5,6 +5,7 @@ use edge_nal::{Readable, UdpReceive, UdpSend}; use rs_matter::error::{Error, ErrorCode}; use rs_matter::transport::network::{Address, NetworkReceive, NetworkSend}; +/// UDP transport implementation for edge-nal pub struct Udp(pub T); impl NetworkSend for Udp diff --git a/src/wireless.rs b/src/wireless.rs index 4a7de51..ed5f193 100644 --- a/src/wireless.rs +++ b/src/wireless.rs @@ -31,6 +31,7 @@ use traits::{ use crate::netif::{Netif, NetifRun}; use crate::network::{Embedding, Network}; use crate::persist::Persist; +use crate::private::Sealed; use crate::utils::futures::IntoFaillble; use crate::wireless::mgmt::WirelessManager; use crate::wireless::store::NetworkContext; @@ -117,6 +118,15 @@ where } } +impl Sealed for WirelessBle +where + M: RawMutex, + T: WirelessConfig, + ::NetworkCredentials: Clone + for<'a> FromTLV<'a> + ToTLV, + E: Embedding, +{ +} + impl Network for WirelessBle where M: RawMutex + 'static, diff --git a/src/wireless/store.rs b/src/wireless/store.rs index 07fb7e5..3845c85 100644 --- a/src/wireless/store.rs +++ b/src/wireless/store.rs @@ -15,6 +15,7 @@ use rs_matter::utils::sync::blocking::Mutex; use rs_matter::utils::sync::Notification; use crate::persist::NetworkPersist; +use crate::private::Sealed; use super::proxy::ControllerProxy; use super::traits::WirelessData; @@ -275,6 +276,14 @@ where } } +impl Sealed for &NetworkContext +where + M: RawMutex, + T: WirelessData, + T::NetworkCredentials: Clone + for<'a> FromTLV<'a> + ToTLV, +{ +} + impl NetworkPersist for &NetworkContext where M: RawMutex, diff --git a/src/wireless/traits.rs b/src/wireless/traits.rs index 3145186..beddd2a 100644 --- a/src/wireless/traits.rs +++ b/src/wireless/traits.rs @@ -18,7 +18,7 @@ use crate::private::Sealed; /// A trait representing the credentials of a wireless network (Wifi or Thread). /// -/// The trait is sealed and has only two implemewntations: `WifiCredentials` and `ThreadCredentials`. +/// The trait is sealed and has only two implementations: `WifiCredentials` and `ThreadCredentials`. pub trait NetworkCredentials: Sealed + for<'a> TryFrom<&'a AddWifiNetworkRequest<'a>, Error = Error>