Skip to content

Commit c0d1b85

Browse files
committed
Default mDns impl
1 parent de3d3de commit c0d1b85

File tree

2 files changed

+116
-70
lines changed

2 files changed

+116
-70
lines changed

examples/onoff_light/src/main.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use matter::data_model::root_endpoint;
3030
use matter::data_model::system_model::descriptor;
3131
use matter::error::Error;
3232
use matter::interaction_model::core::InteractionModel;
33-
use matter::mdns::builtin::{Mdns, MdnsRxBuf, MdnsTxBuf};
33+
use matter::mdns::{DefaultMdns, DefaultMdnsRunner};
3434
use matter::persist;
3535
use matter::secure_channel::spake2p::VerifierData;
3636
use matter::transport::network::{Address, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
@@ -64,23 +64,21 @@ fn run() -> Result<(), Error> {
6464

6565
info!(
6666
"Matter memory: mDNS={}, Matter={}, Transport={}",
67-
core::mem::size_of::<Mdns>(),
67+
core::mem::size_of::<DefaultMdns>(),
6868
core::mem::size_of::<Matter>(),
6969
core::mem::size_of::<Transport>(),
7070
);
7171

7272
let (ipv4_addr, ipv6_addr) = initialize_network()?;
7373

74-
let mut mdns = matter::mdns::builtin::Mdns::new(
74+
let mdns = DefaultMdns::new(
7575
0,
7676
"matter-demo",
7777
ipv4_addr.octets(),
7878
Some(ipv6_addr.octets()),
7979
);
8080

81-
let (mdns, mut mdns_runner) = mdns.split();
82-
//let (mut mdns, mdns_runner) = (matter::mdns::astro::AstroMdns::new()?, core::future::pending::pending());
83-
//let (mut mdns, mdns_runner) = (matter::mdns::DummyMdns {}, core::future::pending::pending());
81+
let mut mdns_runner = DefaultMdnsRunner::new(&mdns);
8482

8583
let dev_att = dev_att::HardCodedDevAtt::new();
8684

@@ -191,14 +189,9 @@ fn run() -> Result<(), Error> {
191189
Ok::<_, matter::error::Error>(())
192190
});
193191

194-
let mut tx_buf = MdnsTxBuf::uninit();
195-
let mut rx_buf = MdnsRxBuf::uninit();
196-
let tx_buf = &mut tx_buf;
197-
let rx_buf = &mut rx_buf;
198-
199-
let mut mdns_fut = pin!(async move { mdns_runner.run_udp(tx_buf, rx_buf).await });
192+
let mut mdns_fut = pin!(async move { mdns_runner.run_udp().await });
200193

201-
let mut fut = pin!(async move { select(&mut io_fut, &mut mdns_fut,).await.unwrap() });
194+
let mut fut = pin!(async move { select(&mut io_fut, &mut mdns_fut).await.unwrap() });
202195

203196
smol::block_on(&mut fut)?;
204197

matter/src/mdns.rs

Lines changed: 110 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,39 @@ where
5454
}
5555
}
5656

57+
impl<T> Mdns for &T
58+
where
59+
T: Mdns,
60+
{
61+
fn add(
62+
&self,
63+
name: &str,
64+
service: &str,
65+
protocol: &str,
66+
port: u16,
67+
service_subtypes: &[&str],
68+
txt_kvs: &[(&str, &str)],
69+
) -> Result<(), Error> {
70+
(**self).add(name, service, protocol, port, service_subtypes, txt_kvs)
71+
}
72+
73+
fn remove(&self, name: &str, service: &str, protocol: &str, port: u16) -> Result<(), Error> {
74+
(**self).remove(name, service, protocol, port)
75+
}
76+
}
77+
78+
#[cfg(all(feature = "std", feature = "astro-dnssd"))]
79+
pub type DefaultMdns = astro::Mdns;
80+
81+
#[cfg(all(feature = "std", feature = "astro-dnssd"))]
82+
pub type DefaultMdnsRunner<'a> = astro::MdnsRunner<'a>;
83+
84+
#[cfg(not(all(feature = "std", feature = "astro-dnssd")))]
85+
pub type DefaultMdns<'a> = builtin::Mdns<'a>;
86+
87+
#[cfg(not(all(feature = "std", feature = "astro-dnssd")))]
88+
pub type DefaultMdnsRunner<'a> = builtin::MdnsRunner<'a>;
89+
5790
pub struct DummyMdns;
5891

5992
impl Mdns for DummyMdns {
@@ -239,8 +272,8 @@ pub mod builtin {
239272

240273
const IP_BIND_ADDR: (IpAddr, u16) = (IpAddr::V6(Ipv6Addr::UNSPECIFIED), 5353);
241274

242-
pub type MdnsTxBuf = MaybeUninit<[u8; MAX_TX_BUF_SIZE]>;
243-
pub type MdnsRxBuf = MaybeUninit<[u8; MAX_RX_BUF_SIZE]>;
275+
type MdnsTxBuf = MaybeUninit<[u8; MAX_TX_BUF_SIZE]>;
276+
type MdnsRxBuf = MaybeUninit<[u8; MAX_RX_BUF_SIZE]>;
244277

245278
#[allow(clippy::too_many_arguments)]
246279
pub fn create_record(
@@ -418,28 +451,6 @@ pub mod builtin {
418451
}
419452
}
420453

421-
pub fn split(&mut self) -> (MdnsApi<'_, 'a>, MdnsRunner<'_, 'a>) {
422-
(MdnsApi(&*self), MdnsRunner(&*self))
423-
}
424-
425-
fn key(
426-
&self,
427-
name: &str,
428-
service: &str,
429-
protocol: &str,
430-
port: u16,
431-
) -> heapless::String<64> {
432-
let mut key = heapless::String::new();
433-
434-
write!(&mut key, "{name}.{service}.{protocol}.{port}").unwrap();
435-
436-
key
437-
}
438-
}
439-
440-
pub struct MdnsApi<'a, 'b>(&'a Mdns<'b>);
441-
442-
impl<'a, 'b> MdnsApi<'a, 'b> {
443454
pub fn add(
444455
&self,
445456
name: &str,
@@ -454,9 +465,9 @@ pub mod builtin {
454465
name, service, protocol, service_subtypes, port, txt_kvs
455466
);
456467

457-
let key = self.0.key(name, service, protocol, port);
468+
let key = self.key(name, service, protocol, port);
458469

459-
let mut entries = self.0.entries.borrow_mut();
470+
let mut entries = self.entries.borrow_mut();
460471

461472
entries.retain(|entry| entry.key != key);
462473
entries
@@ -471,10 +482,10 @@ pub mod builtin {
471482
.unwrap();
472483

473484
match create_record(
474-
self.0.id,
475-
self.0.hostname,
476-
self.0.ip,
477-
self.0.ipv6,
485+
self.id,
486+
self.hostname,
487+
self.ip,
488+
self.ipv6,
478489
60, /*ttl_sec*/
479490
name,
480491
service,
@@ -491,7 +502,7 @@ pub mod builtin {
491502
}
492503
}
493504

494-
self.0.notification.signal(());
505+
self.notification.signal(());
495506

496507
Ok(())
497508
}
@@ -508,37 +519,57 @@ pub mod builtin {
508519
name, service, protocol, port
509520
);
510521

511-
let key = self.0.key(name, service, protocol, port);
522+
let key = self.key(name, service, protocol, port);
512523

513-
let mut entries = self.0.entries.borrow_mut();
524+
let mut entries = self.entries.borrow_mut();
514525

515526
let old_len = entries.len();
516527

517528
entries.retain(|entry| entry.key != key);
518529

519530
if entries.len() != old_len {
520-
self.0.notification.signal(());
531+
self.notification.signal(());
521532
}
522533

523534
Ok(())
524535
}
536+
537+
fn key(
538+
&self,
539+
name: &str,
540+
service: &str,
541+
protocol: &str,
542+
port: u16,
543+
) -> heapless::String<64> {
544+
let mut key = heapless::String::new();
545+
546+
write!(&mut key, "{name}.{service}.{protocol}.{port}").unwrap();
547+
548+
key
549+
}
525550
}
526551

527-
pub struct MdnsRunner<'a, 'b>(&'a Mdns<'b>);
552+
pub struct MdnsRunner<'a>(&'a Mdns<'a>);
528553

529-
impl<'a, 'b> MdnsRunner<'a, 'b> {
530-
pub async fn run_udp(
531-
&mut self,
532-
tx_buf: &mut MdnsTxBuf,
533-
rx_buf: &mut MdnsRxBuf,
534-
) -> Result<(), Error> {
535-
let udp = UdpListener::new(SocketAddr::new(IP_BIND_ADDR.0, IP_BIND_ADDR.1)).await?;
554+
impl<'a> MdnsRunner<'a> {
555+
pub const fn new(mdns: &'a Mdns<'a>) -> Self {
556+
Self(mdns)
557+
}
558+
559+
pub async fn run_udp(&mut self) -> Result<(), Error> {
560+
let mut tx_buf = MdnsTxBuf::uninit();
561+
let mut rx_buf = MdnsRxBuf::uninit();
562+
563+
let tx_buf = &mut tx_buf;
564+
let rx_buf = &mut rx_buf;
536565

537566
let tx_pipe = Pipe::new(unsafe { tx_buf.assume_init_mut() });
538567
let rx_pipe = Pipe::new(unsafe { rx_buf.assume_init_mut() });
539568

540569
let tx_pipe = &tx_pipe;
541570
let rx_pipe = &rx_pipe;
571+
572+
let udp = UdpListener::new(SocketAddr::new(IP_BIND_ADDR.0, IP_BIND_ADDR.1)).await?;
542573
let udp = &udp;
543574

544575
let mut tx = pin!(async move {
@@ -584,7 +615,7 @@ pub mod builtin {
584615
select3(&mut tx, &mut rx, &mut run).await.unwrap()
585616
}
586617

587-
pub async fn run(&mut self, tx_pipe: &Pipe<'_>, rx_pipe: &Pipe<'_>) -> Result<(), Error> {
618+
pub async fn run(&self, tx_pipe: &Pipe<'_>, rx_pipe: &Pipe<'_>) -> Result<(), Error> {
588619
let mut broadcast = pin!(self.broadcast(tx_pipe));
589620
let mut respond = pin!(self.respond(rx_pipe, tx_pipe));
590621

@@ -667,7 +698,7 @@ pub mod builtin {
667698
}
668699
}
669700

670-
impl<'a, 'b> super::Mdns for MdnsApi<'a, 'b> {
701+
impl<'a, 'b> super::Mdns for Mdns<'a> {
671702
fn add(
672703
&self,
673704
name: &str,
@@ -677,7 +708,7 @@ pub mod builtin {
677708
service_subtypes: &[&str],
678709
txt_kvs: &[(&str, &str)],
679710
) -> Result<(), Error> {
680-
MdnsApi::add(
711+
Mdns::add(
681712
self,
682713
name,
683714
service,
@@ -695,7 +726,7 @@ pub mod builtin {
695726
protocol: &str,
696727
port: u16,
697728
) -> Result<(), Error> {
698-
MdnsApi::remove(self, name, service, protocol, port)
729+
Mdns::remove(self, name, service, protocol, port)
699730
}
700731
}
701732
}
@@ -705,28 +736,34 @@ pub mod astro {
705736
use core::cell::RefCell;
706737
use std::collections::HashMap;
707738

708-
use super::Mdns;
709-
use crate::error::{Error, ErrorCode};
739+
use crate::{
740+
error::{Error, ErrorCode},
741+
transport::pipe::Pipe,
742+
};
710743
use astro_dnssd::{DNSServiceBuilder, RegisteredDnsService};
711744
use log::info;
712745

713746
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
714-
pub struct ServiceId {
747+
struct ServiceId {
715748
name: String,
716749
service: String,
717750
protocol: String,
718751
port: u16,
719752
}
720753

721-
pub struct AstroMdns {
754+
pub struct Mdns {
722755
services: RefCell<HashMap<ServiceId, RegisteredDnsService>>,
723756
}
724757

725-
impl AstroMdns {
726-
pub fn new() -> Result<Self, Error> {
727-
Ok(Self {
758+
impl Mdns {
759+
pub fn new(_id: u16, _hostname: &str, _ip: [u8; 4], _ipv6: Option<[u8; 16]>) -> Self {
760+
Self::native_new()
761+
}
762+
763+
pub fn native_new() -> Self {
764+
Self {
728765
services: RefCell::new(HashMap::new()),
729-
})
766+
}
730767
}
731768

732769
pub fn add(
@@ -798,7 +835,23 @@ pub mod astro {
798835
}
799836
}
800837

801-
impl Mdns for AstroMdns {
838+
pub struct MdnsRunner<'a>(&'a Mdns);
839+
840+
impl<'a> MdnsRunner<'a> {
841+
pub const fn new(mdns: &'a Mdns) -> Self {
842+
Self(mdns)
843+
}
844+
845+
pub async fn run_udp(&mut self) -> Result<(), Error> {
846+
core::future::pending::<Result<(), Error>>().await
847+
}
848+
849+
pub async fn run(&self, _tx_pipe: &Pipe<'_>, _rx_pipe: &Pipe<'_>) -> Result<(), Error> {
850+
core::future::pending::<Result<(), Error>>().await
851+
}
852+
}
853+
854+
impl super::Mdns for Mdns {
802855
fn add(
803856
&self,
804857
name: &str,
@@ -808,7 +861,7 @@ pub mod astro {
808861
service_subtypes: &[&str],
809862
txt_kvs: &[(&str, &str)],
810863
) -> Result<(), Error> {
811-
AstroMdns::add(
864+
Mdns::add(
812865
self,
813866
name,
814867
service,
@@ -826,7 +879,7 @@ pub mod astro {
826879
protocol: &str,
827880
port: u16,
828881
) -> Result<(), Error> {
829-
AstroMdns::remove(self, name, service, protocol, port)
882+
Mdns::remove(self, name, service, protocol, port)
830883
}
831884
}
832885
}

0 commit comments

Comments
 (0)