Skip to content

Commit ac4b7ba

Browse files
committed
Add an mDNS trait to align with the new mDNS responder metaphor in rs-matter
1 parent 9e279a0 commit ac4b7ba

File tree

11 files changed

+542
-393
lines changed

11 files changed

+542
-393
lines changed

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ rs-matter = { git = "https://github.com/ivmarkov/rs-matter", branch = "next" }
2121
#edge-mdns = { git = "https://github.com/ivmarkov/edge-net" }
2222

2323
[features]
24-
default = ["examples"]
24+
default = []
2525
log = ["dep:log", "rs-matter/log", "embedded-svc/log", "edge-mdns?/log"]
2626
defmt = ["dep:defmt", "rs-matter/defmt", "embedded-svc/defmt", "heapless/defmt-03", "edge-mdns?/defmt"]
27+
zbus = ["rs-matter/zbus"]
2728
zeroconf = ["os", "rs-matter/zeroconf"]
29+
astro-dnssd = ["os", "rs-matter/astro-dnssd"]
2830
rustcrypto = ["rs-matter/rustcrypto"]
2931
os = ["backtrace", "rs-matter/os", "rustcrypto", "embassy-time/std"]
3032
backtrace = ["std", "rs-matter/backtrace"]
3133
async-io-mini = ["std", "edge-nal-std/async-io-mini"]
3234
std = ["alloc", "rs-matter/std", "edge-nal-std"]
3335
alloc = ["embedded-svc/alloc"]
34-
examples = ["log", "os", "nix", "embassy-time-queue-utils/generic-queue-64"]
36+
examples = ["log", "os", "nix", "embassy-time-queue-utils/generic-queue-64", "zeroconf"]
3537

3638
[dependencies]
3739
log = { version = "0.4", default-features = false, optional = true }

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ use rs_matter_stack::matter::error::Error;
8383
use rs_matter_stack::matter::utils::init::InitMaybeUninit;
8484
use rs_matter_stack::matter::utils::select::Coalesce;
8585
use rs_matter_stack::matter::{clusters, devices};
86+
use rs_matter_stack::mdns::ZeroconfMdns;
8687
use rs_matter_stack::persist::DirKvBlobStore;
8788

8889
use static_cell::StaticCell;
@@ -136,6 +137,8 @@ fn main() -> Result<(), Error> {
136137
edge_nal_std::Stack::new(),
137138
// Will try to find a default network interface
138139
UnixNetifs,
140+
// Will use the mDNS implementation based on the `zeroconf` crate
141+
ZeroconfMdns::new(stack.matter()),
139142
// Will persist in `<tmp-dir>/rs-matter`
140143
&store,
141144
// Our `AsyncHandler` + `AsyncMetadata` impl

examples/light.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use rs_matter_stack::matter::utils::init::InitMaybeUninit;
3030
use rs_matter_stack::matter::utils::select::Coalesce;
3131
use rs_matter_stack::matter::utils::sync::blocking::raw::StdRawMutex;
3232
use rs_matter_stack::matter::{clusters, devices};
33+
use rs_matter_stack::mdns::ZeroconfMdns;
3334
use rs_matter_stack::persist::DirKvBlobStore;
3435
use rs_matter_stack::wireless::PreexistingWireless;
3536
use rs_matter_stack::wireless::WifiMatterStack;
@@ -78,10 +79,14 @@ fn main() -> Result<(), Error> {
7879
let store = stack.create_shared_store(DirKvBlobStore::new_default());
7980
let mut matter = pin!(stack.run(
8081
PreexistingWireless::new(
82+
// The Matter stack needs UDP sockets to communicate with other Matter devices
8183
edge_nal_std::Stack::new(),
84+
// Will try to find a default network interface
8285
UnixNetifs,
8386
// A dummy wireless controller that does nothing
8487
NoopWirelessNetCtl::new(NetworkType::Wifi),
88+
// Will use the mDNS implementation based on the `zeroconf` crate
89+
ZeroconfMdns::new(stack.matter()),
8590
BuiltinGattPeripheral::new(None),
8691
),
8792
// Will persist in `<tmp-dir>/rs-matter`

examples/light_eth.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use rs_matter_stack::matter::error::Error;
3131
use rs_matter_stack::matter::utils::init::InitMaybeUninit;
3232
use rs_matter_stack::matter::utils::select::Coalesce;
3333
use rs_matter_stack::matter::{clusters, devices};
34+
use rs_matter_stack::mdns::ZeroconfMdns;
3435
use rs_matter_stack::persist::DirKvBlobStore;
3536

3637
use static_cell::StaticCell;
@@ -84,6 +85,8 @@ fn main() -> Result<(), Error> {
8485
edge_nal_std::Stack::new(),
8586
// Will try to find a default network interface
8687
UnixNetifs,
88+
// Will use the mDNS implementation based on the `zeroconf` crate
89+
ZeroconfMdns::new(stack.matter()),
8790
// Will persist in `<tmp-dir>/rs-matter`
8891
&store,
8992
// Our `AsyncHandler` + `AsyncMetadata` impl

src/eth.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rs_matter::transport::network::NoNetwork;
1414
use rs_matter::utils::init::{init, Init};
1515
use rs_matter::utils::select::Coalesce;
1616

17+
use crate::mdns::Mdns;
1718
use crate::nal::NetStack;
1819
use crate::network::{Embedding, Network};
1920
use crate::persist::{KvBlobStore, SharedKvBlobStore};
@@ -64,23 +65,25 @@ pub type EthMatterStack<'a, E = ()> = MatterStack<'a, Eth<E>>;
6465
/// A trait representing a task that needs access to the operational Ethernet interface
6566
/// (Network stack and Netif) to perform its work.
6667
pub trait EthernetTask {
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>
68+
/// Run the task with the given network stack, network interface and mDNS
69+
async fn run<S, N, M>(&mut self, net_stack: S, netif: N, mdns: M) -> Result<(), Error>
6970
where
7071
S: NetStack,
71-
N: NetifDiag + NetChangeNotif;
72+
N: NetifDiag + NetChangeNotif,
73+
M: Mdns;
7274
}
7375

7476
impl<T> EthernetTask for &mut T
7577
where
7678
T: EthernetTask,
7779
{
78-
async fn run<S, N>(&mut self, net_stack: S, netif: N) -> Result<(), Error>
80+
async fn run<S, N, M>(&mut self, net_stack: S, netif: N, mdns: M) -> Result<(), Error>
7981
where
8082
S: NetStack,
8183
N: NetifDiag + NetChangeNotif,
84+
M: Mdns,
8285
{
83-
(*self).run(net_stack, netif).await
86+
(*self).run(net_stack, netif, mdns).await
8487
}
8588
}
8689

@@ -106,28 +109,30 @@ where
106109

107110
/// A utility type for running an ethernet task with a pre-existing ethernet interface
108111
/// rather than bringing up / tearing down the ethernet interface for the task.
109-
pub struct PreexistingEthernet<S, N> {
112+
pub struct PreexistingEthernet<S, N, M> {
110113
stack: S,
111114
netif: N,
115+
mdns: M,
112116
}
113117

114-
impl<S, N> PreexistingEthernet<S, N> {
115-
/// Create a new `PreexistingEthernet` instance with the given network interface and UDP stack.
116-
pub const fn new(stack: S, netif: N) -> Self {
117-
Self { stack, netif }
118+
impl<S, N, M> PreexistingEthernet<S, N, M> {
119+
/// Create a new `PreexistingEthernet` instance with the given network interface, UDP stack and mDNS.
120+
pub const fn new(stack: S, netif: N, mdns: M) -> Self {
121+
Self { stack, netif, mdns }
118122
}
119123
}
120124

121-
impl<S, N> Ethernet for PreexistingEthernet<S, N>
125+
impl<S, N, M> Ethernet for PreexistingEthernet<S, N, M>
122126
where
123127
S: NetStack,
124128
N: NetifDiag + NetChangeNotif,
129+
M: Mdns,
125130
{
126131
async fn run<T>(&mut self, mut task: T) -> Result<(), Error>
127132
where
128133
T: EthernetTask,
129134
{
130-
task.run(&self.stack, &self.netif).await
135+
task.run(&self.stack, &self.netif, &mut self.mdns).await
131136
}
132137
}
133138

@@ -172,26 +177,29 @@ where
172177
/// Parameters:
173178
/// - `netif` - a user-provided `Netif` implementation for the Ethernet network
174179
/// - `net_stack` - a user-provided network stack implementation
180+
/// - `mdns` - a user-provided mDNS implementation
175181
/// - `persist` - a user-provided `Persist` implementation
176182
/// - `handler` - a user-provided DM handler implementation
177183
/// - `user` - a user-provided future that will be polled only when the netif interface is up
178-
pub async fn run_preex<U, N, S, H, X>(
184+
pub async fn run_preex<U, N, M, S, H, X>(
179185
&self,
180186
net_stack: U,
181187
netif: N,
188+
mdns: M,
182189
store: &SharedKvBlobStore<'_, S>,
183190
handler: H,
184191
user: X,
185192
) -> Result<(), Error>
186193
where
187194
U: NetStack,
188195
N: NetifDiag + NetChangeNotif,
196+
M: Mdns,
189197
S: KvBlobStore,
190198
H: AsyncHandler + AsyncMetadata,
191199
X: UserTask,
192200
{
193201
self.run(
194-
PreexistingEthernet::new(net_stack, netif),
202+
PreexistingEthernet::new(net_stack, netif, mdns),
195203
store,
196204
handler,
197205
user,
@@ -261,16 +269,18 @@ where
261269
H: AsyncMetadata + AsyncHandler,
262270
X: UserTask,
263271
{
264-
async fn run<S, C>(&mut self, net_stack: S, netif: C) -> Result<(), Error>
272+
async fn run<S, C, M>(&mut self, net_stack: S, netif: C, mut mdns: M) -> Result<(), Error>
265273
where
266274
S: NetStack,
267275
C: NetifDiag + NetChangeNotif,
276+
M: Mdns,
268277
{
269278
info!("Ethernet driver started");
270279

271280
let mut net_task = pin!(self.0.run_oper_net(
272281
&net_stack,
273282
&netif,
283+
&mut mdns,
274284
core::future::pending(),
275285
Option::<(NoNetwork, NoNetwork)>::None,
276286
));

0 commit comments

Comments
 (0)