Skip to content

Commit 9895280

Browse files
committed
NetStack
1 parent 2b0cf13 commit 9895280

File tree

6 files changed

+295
-122
lines changed

6 files changed

+295
-122
lines changed

src/eth.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use core::pin::pin;
22

3-
use edge_nal::UdpBind;
4-
53
use embassy_futures::select::{select, select3};
64

75
use rs_matter::dm::clusters::gen_comm::CommPolicy;
@@ -16,6 +14,7 @@ use rs_matter::transport::network::NoNetwork;
1614
use rs_matter::utils::init::{init, Init};
1715
use rs_matter::utils::select::Coalesce;
1816

17+
use crate::nal::NetStack;
1918
use crate::network::{Embedding, Network};
2019
use crate::persist::{KvBlobStore, SharedKvBlobStore};
2120
use crate::private::Sealed;
@@ -63,25 +62,25 @@ where
6362
pub type EthMatterStack<'a, E = ()> = MatterStack<'a, Eth<E>>;
6463

6564
/// A trait representing a task that needs access to the operational Ethernet interface
66-
/// (Netif and UDP stack) to perform its work.
65+
/// (Network stack and Netif) to perform its work.
6766
pub trait EthernetTask {
68-
/// Run the task with the given network interface and UDP stack
69-
async fn run<U, N>(&mut self, udp: U, netif: N) -> Result<(), Error>
67+
/// Run the task with the given network stack and network interface
68+
async fn run<S, N>(&mut self, net_stack: S, netif: N) -> Result<(), Error>
7069
where
71-
U: UdpBind,
70+
S: NetStack,
7271
N: NetifDiag + NetChangeNotif;
7372
}
7473

7574
impl<T> EthernetTask for &mut T
7675
where
7776
T: EthernetTask,
7877
{
79-
async fn run<U, N>(&mut self, udp: U, netif: N) -> Result<(), Error>
78+
async fn run<S, N>(&mut self, net_stack: S, netif: N) -> Result<(), Error>
8079
where
81-
U: UdpBind,
80+
S: NetStack,
8281
N: NetifDiag + NetChangeNotif,
8382
{
84-
(*self).run(udp, netif).await
83+
(*self).run(net_stack, netif).await
8584
}
8685
}
8786

@@ -107,28 +106,28 @@ where
107106

108107
/// A utility type for running an ethernet task with a pre-existing ethernet interface
109108
/// rather than bringing up / tearing down the ethernet interface for the task.
110-
pub struct PreexistingEthernet<U, N> {
111-
udp: U,
109+
pub struct PreexistingEthernet<S, N> {
110+
stack: S,
112111
netif: N,
113112
}
114113

115-
impl<N, U> PreexistingEthernet<U, N> {
114+
impl<S, N> PreexistingEthernet<S, N> {
116115
/// Create a new `PreexistingEthernet` instance with the given network interface and UDP stack.
117-
pub const fn new(udp: U, netif: N) -> Self {
118-
Self { udp, netif }
116+
pub const fn new(stack: S, netif: N) -> Self {
117+
Self { stack, netif }
119118
}
120119
}
121120

122-
impl<U, N> Ethernet for PreexistingEthernet<U, N>
121+
impl<S, N> Ethernet for PreexistingEthernet<S, N>
123122
where
124-
U: UdpBind,
123+
S: NetStack,
125124
N: NetifDiag + NetChangeNotif,
126125
{
127126
async fn run<T>(&mut self, mut task: T) -> Result<(), Error>
128127
where
129128
T: EthernetTask,
130129
{
131-
task.run(&mut self.udp, &self.netif).await
130+
task.run(&self.stack, &self.netif).await
132131
}
133132
}
134133

@@ -172,27 +171,32 @@ where
172171
///
173172
/// Parameters:
174173
/// - `netif` - a user-provided `Netif` implementation for the Ethernet network
175-
/// - `udp` - a user-provided `UdpBind` implementation
174+
/// - `net_stack` - a user-provided network stack implementation
176175
/// - `persist` - a user-provided `Persist` implementation
177176
/// - `handler` - a user-provided DM handler implementation
178177
/// - `user` - a user-provided future that will be polled only when the netif interface is up
179178
pub async fn run_preex<U, N, S, H, X>(
180179
&self,
181-
udp: U,
180+
net_stack: U,
182181
netif: N,
183182
store: &SharedKvBlobStore<'_, S>,
184183
handler: H,
185184
user: X,
186185
) -> Result<(), Error>
187186
where
187+
U: NetStack,
188188
N: NetifDiag + NetChangeNotif,
189-
U: UdpBind,
190189
S: KvBlobStore,
191190
H: AsyncHandler + AsyncMetadata,
192191
X: UserTask,
193192
{
194-
self.run(PreexistingEthernet::new(udp, netif), store, handler, user)
195-
.await
193+
self.run(
194+
PreexistingEthernet::new(net_stack, netif),
195+
store,
196+
handler,
197+
user,
198+
)
199+
.await
196200
}
197201

198202
/// Run the Matter stack for an Ethernet network.
@@ -257,15 +261,15 @@ where
257261
H: AsyncMetadata + AsyncHandler,
258262
X: UserTask,
259263
{
260-
async fn run<U, C>(&mut self, udp: U, netif: C) -> Result<(), Error>
264+
async fn run<S, C>(&mut self, net_stack: S, netif: C) -> Result<(), Error>
261265
where
262-
U: UdpBind,
266+
S: NetStack,
263267
C: NetifDiag + NetChangeNotif,
264268
{
265269
info!("Ethernet driver started");
266270

267271
let mut net_task = pin!(self.0.run_oper_net(
268-
&udp,
272+
&net_stack,
269273
&netif,
270274
core::future::pending(),
271275
Option::<(NoNetwork, NoNetwork)>::None,
@@ -274,7 +278,7 @@ where
274278
let handler = self.0.root_handler(&(), &true, &netif, &self.1);
275279
let mut handler_task = pin!(self.0.run_handler((&self.1, handler)));
276280

277-
let mut user_task = pin!(self.2.run(&udp, &netif));
281+
let mut user_task = pin!(self.2.run(&net_stack, &netif));
278282

279283
select3(&mut net_task, &mut handler_task, &mut user_task)
280284
.coalesce()

src/lib.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use core::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV6};
1414
use core::pin::pin;
1515

1616
use edge_nal::{UdpBind, UdpSplit};
17-
1817
use embassy_futures::select::{select4, Either4};
1918
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
2019

@@ -36,6 +35,7 @@ use rs_matter::utils::rand::Rand;
3635
use rs_matter::utils::storage::pooled::PooledBuffers;
3736
use rs_matter::{BasicCommData, Matter, MATTER_PORT};
3837

38+
use crate::nal::NetStack;
3939
use crate::network::Network;
4040
use crate::persist::SharedKvBlobStore;
4141

@@ -341,20 +341,21 @@ where
341341
/// the main UDP transport and the mDNS service when the netif goes up/down or changes its IP addresses.
342342
///
343343
/// Parameters:
344+
/// - `net_stack` - a user-provided network stack that implements `UdpBind`, `UdpConnect`, `TcpBind`, `TcpConnect`, and `Dns`
344345
/// - `netif` - a user-provided `Netif` implementation
345346
/// - `until` - the method will return once this future becomes ready
346347
/// - `comm` - a tuple of additional and optional `NetworkReceive` and `NetworkSend` transport implementations
347348
/// (useful when a second transport needs to run in parallel with the operational Matter transport,
348349
/// i.e. when using concurrent commissisoning)
349350
async fn run_oper_net<U, I, X, R, S>(
350351
&self,
351-
udp: U,
352+
net_stack: U,
352353
netif: I,
353354
until: X,
354355
mut comm: Option<(R, S)>,
355356
) -> Result<(), Error>
356357
where
357-
U: UdpBind,
358+
U: NetStack,
358359
I: NetifDiag + NetChangeNotif,
359360
X: Future<Output = Result<(), Error>>,
360361
R: NetworkReceive,
@@ -446,7 +447,9 @@ where
446447
let result = if cur_netif.operational {
447448
info!("Netif up: {:?}", cur_netif);
448449

449-
let mut socket = udp
450+
let udp_bind = unwrap!(net_stack.udp_bind());
451+
452+
let mut socket = udp_bind
450453
.bind(SocketAddr::V6(SocketAddrV6::new(
451454
Ipv6Addr::UNSPECIFIED,
452455
MATTER_PORT,
@@ -459,7 +462,7 @@ where
459462
let (recv, send) = socket.split();
460463

461464
let mut mdns_task = pin!(self.run_builtin_mdns(
462-
&udp,
465+
&udp_bind,
463466
&cur_netif.mac,
464467
cur_netif.ipv4,
465468
cur_netif.ipv6,
@@ -682,37 +685,37 @@ where
682685
}
683686

684687
/// A trait representing a user task that needs access to the operational network interface
685-
/// (Netif and UDP stack) to perform its work.
688+
/// (Netif and net stack) to perform its work.
686689
///
687690
/// Note that the task would be started only when `rs-matter`
688691
/// brings up the operational interface (eth, wifi or thread)
689692
/// and if the interface goes down, the user task would be stopped.
690693
/// Upon re-connection, the task would be started again.
691694
pub trait UserTask {
692-
/// Run the task with the given network interface and UDP stack
693-
async fn run<U, N>(&mut self, udp: U, netif: N) -> Result<(), Error>
695+
/// Run the task with the given network stack and network interface
696+
async fn run<S, N>(&mut self, net_stack: S, netif: N) -> Result<(), Error>
694697
where
695-
U: UdpBind,
698+
S: NetStack,
696699
N: NetifDiag + NetChangeNotif;
697700
}
698701

699702
impl<T> UserTask for &mut T
700703
where
701704
T: UserTask,
702705
{
703-
async fn run<U, N>(&mut self, udp: U, netif: N) -> Result<(), Error>
706+
async fn run<S, N>(&mut self, net_stack: S, netif: N) -> Result<(), Error>
704707
where
705-
U: UdpBind,
708+
S: NetStack,
706709
N: NetifDiag + NetChangeNotif,
707710
{
708-
(*self).run(udp, netif).await
711+
(*self).run(net_stack, netif).await
709712
}
710713
}
711714

712715
impl UserTask for () {
713-
async fn run<U, N>(&mut self, _udp: U, _netif: N) -> Result<(), Error>
716+
async fn run<S, N>(&mut self, _net_stack: S, _netif: N) -> Result<(), Error>
714717
where
715-
U: UdpBind,
718+
S: NetStack,
716719
N: NetifDiag + NetChangeNotif,
717720
{
718721
core::future::pending::<()>().await;

0 commit comments

Comments
 (0)