Skip to content

Commit

Permalink
Seal even more traits
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Oct 5, 2024
1 parent 4e0805f commit 23caf4a
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 26 deletions.
3 changes: 3 additions & 0 deletions src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -39,6 +40,8 @@ pub struct Eth<E = ()> {
embedding: E,
}

impl<E> Sealed for Eth<E> {}

impl<E> Network for Eth<E>
where
E: Embedding + 'static,
Expand Down
8 changes: 7 additions & 1 deletion src/netif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down
5 changes: 4 additions & 1 deletion src/network.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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.
Expand Down
30 changes: 7 additions & 23 deletions src/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand All @@ -37,30 +42,9 @@ pub trait NetworkPersist {
async fn wait_state_changed(&self);
}

impl<T> 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<Option<&'a [u8]>, 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> {
Expand Down
1 change: 1 addition & 0 deletions src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(pub T);

impl<T> NetworkSend for Udp<T>
Expand Down
10 changes: 10 additions & 0 deletions src/wireless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -117,6 +118,15 @@ where
}
}

impl<M, T, E> Sealed for WirelessBle<M, T, E>
where
M: RawMutex,
T: WirelessConfig,
<T::Data as WirelessData>::NetworkCredentials: Clone + for<'a> FromTLV<'a> + ToTLV,
E: Embedding,
{
}

impl<M, T, E> Network for WirelessBle<M, T, E>
where
M: RawMutex + 'static,
Expand Down
9 changes: 9 additions & 0 deletions src/wireless/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -275,6 +276,14 @@ where
}
}

impl<const N: usize, M, T> Sealed for &NetworkContext<N, M, T>
where
M: RawMutex,
T: WirelessData,
T::NetworkCredentials: Clone + for<'a> FromTLV<'a> + ToTLV,
{
}

impl<const N: usize, M, T> NetworkPersist for &NetworkContext<N, M, T>
where
M: RawMutex,
Expand Down
2 changes: 1 addition & 1 deletion src/wireless/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 23caf4a

Please sign in to comment.