2023-01-11 09:57:03 +01:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020-2022 Project CHIP Authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-02-02 18:22:21 +00:00
|
|
|
use core::fmt::Write;
|
2022-12-27 09:32:52 +05:30
|
|
|
|
2023-02-02 18:22:21 +00:00
|
|
|
use crate::error::Error;
|
2022-12-27 09:32:52 +05:30
|
|
|
|
2023-02-02 18:22:21 +00:00
|
|
|
pub trait Mdns {
|
|
|
|
fn add(
|
2023-06-10 14:01:35 +00:00
|
|
|
&self,
|
2023-02-02 18:22:21 +00:00
|
|
|
name: &str,
|
2023-04-24 21:41:16 +00:00
|
|
|
service: &str,
|
|
|
|
protocol: &str,
|
2023-02-02 18:22:21 +00:00
|
|
|
port: u16,
|
2023-04-24 19:27:17 +00:00
|
|
|
service_subtypes: &[&str],
|
2023-02-02 18:22:21 +00:00
|
|
|
txt_kvs: &[(&str, &str)],
|
|
|
|
) -> Result<(), Error>;
|
|
|
|
|
2023-06-10 14:01:35 +00:00
|
|
|
fn remove(&self, name: &str, service: &str, protocol: &str, port: u16) -> Result<(), Error>;
|
2022-12-27 09:32:52 +05:30
|
|
|
}
|
|
|
|
|
2023-02-02 18:22:21 +00:00
|
|
|
impl<T> Mdns for &mut T
|
|
|
|
where
|
|
|
|
T: Mdns,
|
|
|
|
{
|
|
|
|
fn add(
|
2023-06-10 14:01:35 +00:00
|
|
|
&self,
|
2023-02-02 18:22:21 +00:00
|
|
|
name: &str,
|
2023-04-24 21:41:16 +00:00
|
|
|
service: &str,
|
|
|
|
protocol: &str,
|
2023-02-02 18:22:21 +00:00
|
|
|
port: u16,
|
2023-04-24 19:27:17 +00:00
|
|
|
service_subtypes: &[&str],
|
2023-02-02 18:22:21 +00:00
|
|
|
txt_kvs: &[(&str, &str)],
|
|
|
|
) -> Result<(), Error> {
|
2023-04-24 21:41:16 +00:00
|
|
|
(**self).add(name, service, protocol, port, service_subtypes, txt_kvs)
|
2023-02-02 18:22:21 +00:00
|
|
|
}
|
|
|
|
|
2023-06-10 14:01:35 +00:00
|
|
|
fn remove(&self, name: &str, service: &str, protocol: &str, port: u16) -> Result<(), Error> {
|
2023-04-24 21:41:16 +00:00
|
|
|
(**self).remove(name, service, protocol, port)
|
2023-02-02 18:22:21 +00:00
|
|
|
}
|
2022-12-27 09:32:52 +05:30
|
|
|
}
|
|
|
|
|
2023-06-10 18:47:21 +00:00
|
|
|
impl<T> Mdns for &T
|
|
|
|
where
|
|
|
|
T: Mdns,
|
|
|
|
{
|
|
|
|
fn add(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
service: &str,
|
|
|
|
protocol: &str,
|
|
|
|
port: u16,
|
|
|
|
service_subtypes: &[&str],
|
|
|
|
txt_kvs: &[(&str, &str)],
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
(**self).add(name, service, protocol, port, service_subtypes, txt_kvs)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove(&self, name: &str, service: &str, protocol: &str, port: u16) -> Result<(), Error> {
|
|
|
|
(**self).remove(name, service, protocol, port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(feature = "std", feature = "astro-dnssd"))]
|
|
|
|
pub type DefaultMdns = astro::Mdns;
|
|
|
|
|
|
|
|
#[cfg(all(feature = "std", feature = "astro-dnssd"))]
|
|
|
|
pub type DefaultMdnsRunner<'a> = astro::MdnsRunner<'a>;
|
|
|
|
|
|
|
|
#[cfg(not(all(feature = "std", feature = "astro-dnssd")))]
|
|
|
|
pub type DefaultMdns<'a> = builtin::Mdns<'a>;
|
|
|
|
|
|
|
|
#[cfg(not(all(feature = "std", feature = "astro-dnssd")))]
|
|
|
|
pub type DefaultMdnsRunner<'a> = builtin::MdnsRunner<'a>;
|
|
|
|
|
2023-02-02 18:22:21 +00:00
|
|
|
pub struct DummyMdns;
|
|
|
|
|
|
|
|
impl Mdns for DummyMdns {
|
|
|
|
fn add(
|
2023-06-10 14:01:35 +00:00
|
|
|
&self,
|
2023-02-02 18:22:21 +00:00
|
|
|
_name: &str,
|
2023-04-24 21:41:16 +00:00
|
|
|
_service: &str,
|
|
|
|
_protocol: &str,
|
2023-02-02 18:22:21 +00:00
|
|
|
_port: u16,
|
2023-04-24 19:27:17 +00:00
|
|
|
_service_subtypes: &[&str],
|
2023-02-02 18:22:21 +00:00
|
|
|
_txt_kvs: &[(&str, &str)],
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-12-27 09:32:52 +05:30
|
|
|
|
2023-04-24 21:41:16 +00:00
|
|
|
fn remove(
|
2023-06-10 14:01:35 +00:00
|
|
|
&self,
|
2023-04-24 21:41:16 +00:00
|
|
|
_name: &str,
|
|
|
|
_service: &str,
|
|
|
|
_protocol: &str,
|
|
|
|
_port: u16,
|
|
|
|
) -> Result<(), Error> {
|
2023-02-02 18:22:21 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 09:32:52 +05:30
|
|
|
|
|
|
|
pub enum ServiceMode {
|
2023-01-08 11:13:12 +05:30
|
|
|
/// The commissioned state
|
2022-12-27 09:32:52 +05:30
|
|
|
Commissioned,
|
2023-01-08 11:13:12 +05:30
|
|
|
/// The commissionable state with the discriminator that should be used
|
|
|
|
Commissionable(u16),
|
2022-12-27 09:32:52 +05:30
|
|
|
}
|
|
|
|
|
2023-02-02 18:22:21 +00:00
|
|
|
/// The mDNS service handler
|
|
|
|
pub struct MdnsMgr<'a> {
|
|
|
|
/// Vendor ID
|
|
|
|
vid: u16,
|
|
|
|
/// Product ID
|
|
|
|
pid: u16,
|
|
|
|
/// Device name
|
2023-06-10 14:01:35 +00:00
|
|
|
device_name: &'a str,
|
2023-02-02 18:22:21 +00:00
|
|
|
/// Matter port
|
|
|
|
matter_port: u16,
|
|
|
|
/// mDns service
|
2023-06-10 14:01:35 +00:00
|
|
|
pub(crate) mdns: &'a dyn Mdns,
|
2023-02-02 18:22:21 +00:00
|
|
|
}
|
2022-12-27 09:32:52 +05:30
|
|
|
|
2023-02-02 18:22:21 +00:00
|
|
|
impl<'a> MdnsMgr<'a> {
|
2023-05-24 10:07:11 +00:00
|
|
|
#[inline(always)]
|
2023-02-02 18:22:21 +00:00
|
|
|
pub fn new(
|
|
|
|
vid: u16,
|
|
|
|
pid: u16,
|
2023-06-10 14:01:35 +00:00
|
|
|
device_name: &'a str,
|
2023-02-02 18:22:21 +00:00
|
|
|
matter_port: u16,
|
2023-06-10 14:01:35 +00:00
|
|
|
mdns: &'a dyn Mdns,
|
2023-02-02 18:22:21 +00:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
vid,
|
|
|
|
pid,
|
2023-06-10 14:01:35 +00:00
|
|
|
device_name,
|
2023-02-02 18:22:21 +00:00
|
|
|
matter_port,
|
|
|
|
mdns,
|
2022-12-27 09:32:52 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-02 18:22:21 +00:00
|
|
|
/// Publish an mDNS service
|
2022-12-27 09:32:52 +05:30
|
|
|
/// name - is the service name (comma separated subtypes may follow)
|
|
|
|
/// mode - the current service mode
|
2023-01-10 13:14:20 +01:00
|
|
|
#[allow(clippy::needless_pass_by_value)]
|
2023-06-10 14:01:35 +00:00
|
|
|
pub fn publish_service(&self, name: &str, mode: ServiceMode) -> Result<(), Error> {
|
2022-12-27 09:32:52 +05:30
|
|
|
match mode {
|
2023-04-24 19:27:17 +00:00
|
|
|
ServiceMode::Commissioned => {
|
|
|
|
self.mdns
|
2023-04-24 21:41:16 +00:00
|
|
|
.add(name, "_matter", "_tcp", self.matter_port, &[], &[])
|
2023-04-24 19:27:17 +00:00
|
|
|
}
|
2023-01-11 13:35:25 +01:00
|
|
|
ServiceMode::Commissionable(discriminator) => {
|
2023-02-02 18:22:21 +00:00
|
|
|
let discriminator_str = Self::get_discriminator_str(discriminator);
|
|
|
|
let vp = self.get_vp();
|
2022-12-27 09:32:52 +05:30
|
|
|
|
2023-01-10 13:14:20 +01:00
|
|
|
let txt_kvs = [
|
2023-02-02 18:22:21 +00:00
|
|
|
("D", discriminator_str.as_str()),
|
|
|
|
("CM", "1"),
|
2023-06-10 14:01:35 +00:00
|
|
|
("DN", self.device_name),
|
2023-02-02 18:22:21 +00:00
|
|
|
("VP", &vp),
|
|
|
|
("SII", "5000"), /* Sleepy Idle Interval */
|
|
|
|
("SAI", "300"), /* Sleepy Active Interval */
|
|
|
|
("PH", "33"), /* Pairing Hint */
|
|
|
|
("PI", ""), /* Pairing Instruction */
|
2023-01-10 13:14:20 +01:00
|
|
|
];
|
2023-04-24 19:27:17 +00:00
|
|
|
|
|
|
|
self.mdns.add(
|
|
|
|
name,
|
2023-04-24 21:41:16 +00:00
|
|
|
"_matterc",
|
|
|
|
"_udp",
|
2023-04-24 19:27:17 +00:00
|
|
|
self.matter_port,
|
|
|
|
&[
|
|
|
|
&self.get_long_service_subtype(discriminator),
|
|
|
|
&self.get_short_service_type(discriminator),
|
|
|
|
],
|
|
|
|
&txt_kvs,
|
|
|
|
)
|
2023-02-02 18:22:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-10 14:01:35 +00:00
|
|
|
pub fn unpublish_service(&self, name: &str, mode: ServiceMode) -> Result<(), Error> {
|
2023-02-02 18:22:21 +00:00
|
|
|
match mode {
|
2023-04-24 21:41:16 +00:00
|
|
|
ServiceMode::Commissioned => {
|
|
|
|
self.mdns.remove(name, "_matter", "_tcp", self.matter_port)
|
|
|
|
}
|
2023-04-24 19:27:17 +00:00
|
|
|
ServiceMode::Commissionable(_) => {
|
2023-04-24 21:41:16 +00:00
|
|
|
self.mdns.remove(name, "_matterc", "_udp", self.matter_port)
|
2022-12-27 09:32:52 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-10 13:14:20 +01:00
|
|
|
|
2023-04-24 19:27:17 +00:00
|
|
|
fn get_long_service_subtype(&self, discriminator: u16) -> heapless::String<32> {
|
2023-02-02 18:22:21 +00:00
|
|
|
let mut serv_type = heapless::String::new();
|
2023-04-24 19:27:17 +00:00
|
|
|
write!(&mut serv_type, "_L{}", discriminator).unwrap();
|
2023-02-02 18:22:21 +00:00
|
|
|
|
2023-04-24 19:27:17 +00:00
|
|
|
serv_type
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_short_service_type(&self, discriminator: u16) -> heapless::String<32> {
|
|
|
|
let short = Self::compute_short_discriminator(discriminator);
|
|
|
|
|
|
|
|
let mut serv_type = heapless::String::new();
|
|
|
|
write!(&mut serv_type, "_S{}", short).unwrap();
|
2023-02-02 18:22:21 +00:00
|
|
|
|
|
|
|
serv_type
|
|
|
|
}
|
|
|
|
|
2023-04-24 19:27:17 +00:00
|
|
|
fn get_discriminator_str(discriminator: u16) -> heapless::String<5> {
|
|
|
|
discriminator.into()
|
|
|
|
}
|
|
|
|
|
2023-02-02 18:22:21 +00:00
|
|
|
fn get_vp(&self) -> heapless::String<11> {
|
|
|
|
let mut vp = heapless::String::new();
|
|
|
|
|
|
|
|
write!(&mut vp, "{}+{}", self.vid, self.pid).unwrap();
|
|
|
|
|
|
|
|
vp
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_short_discriminator(discriminator: u16) -> u16 {
|
|
|
|
const SHORT_DISCRIMINATOR_MASK: u16 = 0xF00;
|
|
|
|
const SHORT_DISCRIMINATOR_SHIFT: u16 = 8;
|
|
|
|
|
|
|
|
(discriminator & SHORT_DISCRIMINATOR_MASK) >> SHORT_DISCRIMINATOR_SHIFT
|
|
|
|
}
|
2023-01-10 13:14:20 +01:00
|
|
|
}
|
|
|
|
|
2023-05-24 10:07:11 +00:00
|
|
|
pub mod builtin {
|
|
|
|
use core::cell::RefCell;
|
|
|
|
use core::fmt::Write;
|
2023-06-10 14:01:35 +00:00
|
|
|
use core::mem::MaybeUninit;
|
2023-05-24 10:07:11 +00:00
|
|
|
use core::pin::pin;
|
|
|
|
use core::str::FromStr;
|
|
|
|
|
|
|
|
use domain::base::header::Flags;
|
|
|
|
use domain::base::iana::Class;
|
|
|
|
use domain::base::octets::{Octets256, Octets64, OctetsBuilder};
|
|
|
|
use domain::base::{Dname, MessageBuilder, Record, ShortBuf};
|
|
|
|
use domain::rdata::{Aaaa, Ptr, Srv, Txt, A};
|
2023-06-10 14:01:35 +00:00
|
|
|
use embassy_futures::select::{select, select3};
|
2023-05-24 10:07:11 +00:00
|
|
|
use embassy_time::{Duration, Timer};
|
|
|
|
use log::info;
|
2023-04-24 19:27:17 +00:00
|
|
|
|
2023-04-29 19:38:01 +03:00
|
|
|
use crate::error::{Error, ErrorCode};
|
2023-06-10 14:01:35 +00:00
|
|
|
use crate::transport::network::{Address, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
|
|
|
use crate::transport::packet::{MAX_RX_BUF_SIZE, MAX_TX_BUF_SIZE};
|
|
|
|
use crate::transport::pipe::{Chunk, Pipe};
|
2023-05-24 10:07:11 +00:00
|
|
|
use crate::transport::udp::UdpListener;
|
2023-06-10 14:01:35 +00:00
|
|
|
use crate::utils::select::{EitherUnwrap, Notification};
|
2023-04-24 19:27:17 +00:00
|
|
|
|
2023-05-28 11:04:46 +00:00
|
|
|
const IP_BROADCAST_ADDRS: [(IpAddr, u16); 2] = [
|
|
|
|
(IpAddr::V4(Ipv4Addr::new(224, 0, 0, 251)), 5353),
|
|
|
|
(
|
2023-05-24 10:07:11 +00:00
|
|
|
IpAddr::V6(Ipv6Addr::new(0xff02, 0, 0, 0, 0, 0, 0, 0x00fb)),
|
|
|
|
5353,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
2023-05-28 11:04:46 +00:00
|
|
|
const IP_BIND_ADDR: (IpAddr, u16) = (IpAddr::V6(Ipv6Addr::UNSPECIFIED), 5353);
|
2023-05-24 10:07:11 +00:00
|
|
|
|
2023-06-10 18:47:21 +00:00
|
|
|
type MdnsTxBuf = MaybeUninit<[u8; MAX_TX_BUF_SIZE]>;
|
|
|
|
type MdnsRxBuf = MaybeUninit<[u8; MAX_RX_BUF_SIZE]>;
|
2023-06-10 14:01:35 +00:00
|
|
|
|
2023-05-28 11:13:02 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2023-05-24 10:07:11 +00:00
|
|
|
pub fn create_record(
|
|
|
|
id: u16,
|
|
|
|
hostname: &str,
|
|
|
|
ip: [u8; 4],
|
|
|
|
ipv6: Option<[u8; 16]>,
|
|
|
|
|
|
|
|
ttl_sec: u32,
|
|
|
|
|
|
|
|
name: &str,
|
|
|
|
service: &str,
|
|
|
|
protocol: &str,
|
2023-04-24 19:27:17 +00:00
|
|
|
port: u16,
|
2023-05-24 10:07:11 +00:00
|
|
|
service_subtypes: &[&str],
|
|
|
|
txt_kvs: &[(&str, &str)],
|
|
|
|
|
|
|
|
buffer: &mut [u8],
|
|
|
|
) -> Result<usize, ShortBuf> {
|
|
|
|
let target = domain::base::octets::Octets2048::new();
|
|
|
|
let message = MessageBuilder::from_target(target)?;
|
|
|
|
|
|
|
|
let mut message = message.answer();
|
|
|
|
|
|
|
|
let mut ptr_str = heapless::String::<40>::new();
|
|
|
|
write!(ptr_str, "{}.{}.local", service, protocol).unwrap();
|
|
|
|
|
|
|
|
let mut dname = heapless::String::<60>::new();
|
|
|
|
write!(dname, "{}.{}.{}.local", name, service, protocol).unwrap();
|
|
|
|
|
|
|
|
let mut hname = heapless::String::<40>::new();
|
|
|
|
write!(hname, "{}.local", hostname).unwrap();
|
|
|
|
|
|
|
|
let ptr: Dname<Octets64> = Dname::from_str(&ptr_str).unwrap();
|
|
|
|
let record: Record<Dname<Octets64>, Ptr<_>> = Record::new(
|
|
|
|
Dname::from_str("_services._dns-sd._udp.local").unwrap(),
|
|
|
|
Class::In,
|
|
|
|
ttl_sec,
|
|
|
|
Ptr::new(ptr),
|
|
|
|
);
|
|
|
|
message.push(record)?;
|
|
|
|
|
|
|
|
let t: Dname<Octets64> = Dname::from_str(&dname).unwrap();
|
|
|
|
let record: Record<Dname<Octets64>, Ptr<_>> = Record::new(
|
|
|
|
Dname::from_str(&ptr_str).unwrap(),
|
|
|
|
Class::In,
|
|
|
|
ttl_sec,
|
|
|
|
Ptr::new(t),
|
|
|
|
);
|
|
|
|
message.push(record)?;
|
|
|
|
|
|
|
|
for sub_srv in service_subtypes {
|
|
|
|
let mut ptr_str = heapless::String::<40>::new();
|
|
|
|
write!(ptr_str, "{}._sub.{}.{}.local", sub_srv, service, protocol).unwrap();
|
|
|
|
|
|
|
|
let ptr: Dname<Octets64> = Dname::from_str(&ptr_str).unwrap();
|
|
|
|
let record: Record<Dname<Octets64>, Ptr<_>> = Record::new(
|
|
|
|
Dname::from_str("_services._dns-sd._udp.local").unwrap(),
|
|
|
|
Class::In,
|
|
|
|
ttl_sec,
|
|
|
|
Ptr::new(ptr),
|
|
|
|
);
|
|
|
|
message.push(record)?;
|
|
|
|
|
|
|
|
let t: Dname<Octets64> = Dname::from_str(&dname).unwrap();
|
|
|
|
let record: Record<Dname<Octets64>, Ptr<_>> = Record::new(
|
|
|
|
Dname::from_str(&ptr_str).unwrap(),
|
|
|
|
Class::In,
|
|
|
|
ttl_sec,
|
|
|
|
Ptr::new(t),
|
|
|
|
);
|
|
|
|
message.push(record)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let target: Dname<Octets64> = Dname::from_str(&hname).unwrap();
|
|
|
|
let record: Record<Dname<Octets64>, Srv<_>> = Record::new(
|
|
|
|
Dname::from_str(&dname).unwrap(),
|
|
|
|
Class::In,
|
|
|
|
ttl_sec,
|
|
|
|
Srv::new(0, 0, port, target),
|
|
|
|
);
|
|
|
|
message.push(record)?;
|
|
|
|
|
|
|
|
// only way I found to create multiple parts in a Txt
|
|
|
|
// each slice is the length and then the data
|
|
|
|
let mut octets = Octets256::new();
|
|
|
|
//octets.append_slice(&[1u8, b'X']).unwrap();
|
|
|
|
//octets.append_slice(&[2u8, b'A', b'B']).unwrap();
|
|
|
|
//octets.append_slice(&[0u8]).unwrap();
|
|
|
|
for (k, v) in txt_kvs {
|
|
|
|
octets
|
|
|
|
.append_slice(&[(k.len() + v.len() + 1) as u8])
|
|
|
|
.unwrap();
|
|
|
|
octets.append_slice(k.as_bytes()).unwrap();
|
|
|
|
octets.append_slice(&[b'=']).unwrap();
|
|
|
|
octets.append_slice(v.as_bytes()).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let txt = Txt::from_octets(&mut octets).unwrap();
|
|
|
|
|
|
|
|
let record: Record<Dname<Octets64>, Txt<_>> =
|
|
|
|
Record::new(Dname::from_str(&dname).unwrap(), Class::In, ttl_sec, txt);
|
|
|
|
message.push(record)?;
|
|
|
|
|
|
|
|
let record: Record<Dname<Octets64>, A> = Record::new(
|
|
|
|
Dname::from_str(&hname).unwrap(),
|
|
|
|
Class::In,
|
|
|
|
ttl_sec,
|
|
|
|
A::from_octets(ip[0], ip[1], ip[2], ip[3]),
|
|
|
|
);
|
|
|
|
message.push(record)?;
|
|
|
|
|
|
|
|
if let Some(ipv6) = ipv6 {
|
|
|
|
let record: Record<Dname<Octets64>, Aaaa> = Record::new(
|
|
|
|
Dname::from_str(&hname).unwrap(),
|
|
|
|
Class::In,
|
|
|
|
ttl_sec,
|
|
|
|
Aaaa::new(ipv6.into()),
|
|
|
|
);
|
|
|
|
message.push(record)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let headerb = message.header_mut();
|
|
|
|
headerb.set_id(id);
|
|
|
|
headerb.set_opcode(domain::base::iana::Opcode::Query);
|
|
|
|
headerb.set_rcode(domain::base::iana::Rcode::NoError);
|
|
|
|
|
|
|
|
let mut flags = Flags::new();
|
|
|
|
flags.qr = true;
|
|
|
|
flags.aa = true;
|
|
|
|
headerb.set_flags(flags);
|
|
|
|
|
|
|
|
let target = message.finish();
|
|
|
|
|
|
|
|
buffer[..target.len()].copy_from_slice(target.as_ref());
|
|
|
|
|
|
|
|
Ok(target.len())
|
2023-04-24 19:27:17 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 10:07:11 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct MdnsEntry {
|
|
|
|
key: heapless::String<64>,
|
|
|
|
record: heapless::Vec<u8, 1024>,
|
2023-04-24 19:27:17 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 10:07:11 +00:00
|
|
|
impl MdnsEntry {
|
|
|
|
#[inline(always)]
|
2023-06-01 04:59:01 +00:00
|
|
|
const fn new(key: heapless::String<64>) -> Self {
|
2023-05-24 10:07:11 +00:00
|
|
|
Self {
|
2023-06-01 04:59:01 +00:00
|
|
|
key,
|
2023-05-24 10:07:11 +00:00
|
|
|
record: heapless::Vec::new(),
|
|
|
|
}
|
2023-04-24 19:27:17 +00:00
|
|
|
}
|
2023-05-24 10:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Mdns<'a> {
|
|
|
|
id: u16,
|
|
|
|
hostname: &'a str,
|
|
|
|
ip: [u8; 4],
|
|
|
|
ipv6: Option<[u8; 16]>,
|
|
|
|
entries: RefCell<heapless::Vec<MdnsEntry, 4>>,
|
|
|
|
notification: Notification,
|
|
|
|
}
|
2023-04-24 19:27:17 +00:00
|
|
|
|
2023-05-24 10:07:11 +00:00
|
|
|
impl<'a> Mdns<'a> {
|
|
|
|
#[inline(always)]
|
|
|
|
pub const fn new(id: u16, hostname: &'a str, ip: [u8; 4], ipv6: Option<[u8; 16]>) -> Self {
|
|
|
|
Self {
|
|
|
|
id,
|
|
|
|
hostname,
|
|
|
|
ip,
|
|
|
|
ipv6,
|
|
|
|
entries: RefCell::new(heapless::Vec::new()),
|
|
|
|
notification: Notification::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 19:27:17 +00:00
|
|
|
pub fn add(
|
2023-05-24 10:07:11 +00:00
|
|
|
&self,
|
2023-04-24 19:27:17 +00:00
|
|
|
name: &str,
|
2023-04-24 21:41:16 +00:00
|
|
|
service: &str,
|
|
|
|
protocol: &str,
|
2023-04-24 19:27:17 +00:00
|
|
|
port: u16,
|
|
|
|
service_subtypes: &[&str],
|
|
|
|
txt_kvs: &[(&str, &str)],
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
info!(
|
2023-05-24 10:07:11 +00:00
|
|
|
"Registering mDNS service {}/{}.{} [{:?}]/{}, keys [{:?}]",
|
|
|
|
name, service, protocol, service_subtypes, port, txt_kvs
|
2023-04-24 19:27:17 +00:00
|
|
|
);
|
|
|
|
|
2023-06-10 18:47:21 +00:00
|
|
|
let key = self.key(name, service, protocol, port);
|
2023-04-24 19:27:17 +00:00
|
|
|
|
2023-06-10 18:47:21 +00:00
|
|
|
let mut entries = self.entries.borrow_mut();
|
2023-04-24 19:27:17 +00:00
|
|
|
|
2023-05-24 10:07:11 +00:00
|
|
|
entries.retain(|entry| entry.key != key);
|
|
|
|
entries
|
2023-06-01 04:59:01 +00:00
|
|
|
.push(MdnsEntry::new(key))
|
2023-05-24 10:07:11 +00:00
|
|
|
.map_err(|_| ErrorCode::NoSpace)?;
|
2023-04-24 19:27:17 +00:00
|
|
|
|
2023-05-24 10:07:11 +00:00
|
|
|
let entry = entries.iter_mut().last().unwrap();
|
|
|
|
entry
|
|
|
|
.record
|
|
|
|
.resize(1024, 0)
|
|
|
|
.map_err(|_| ErrorCode::NoSpace)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
match create_record(
|
2023-06-10 18:47:21 +00:00
|
|
|
self.id,
|
|
|
|
self.hostname,
|
|
|
|
self.ip,
|
|
|
|
self.ipv6,
|
2023-05-24 10:07:11 +00:00
|
|
|
60, /*ttl_sec*/
|
|
|
|
name,
|
|
|
|
service,
|
|
|
|
protocol,
|
|
|
|
port,
|
|
|
|
service_subtypes,
|
|
|
|
txt_kvs,
|
|
|
|
&mut entry.record,
|
|
|
|
) {
|
|
|
|
Ok(len) => entry.record.truncate(len),
|
|
|
|
Err(_) => {
|
|
|
|
entries.pop();
|
|
|
|
Err(ErrorCode::NoSpace)?;
|
|
|
|
}
|
2023-04-24 19:27:17 +00:00
|
|
|
}
|
|
|
|
|
2023-06-10 18:47:21 +00:00
|
|
|
self.notification.signal(());
|
2023-04-24 19:27:17 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-24 21:41:16 +00:00
|
|
|
pub fn remove(
|
2023-05-24 10:07:11 +00:00
|
|
|
&self,
|
2023-04-24 21:41:16 +00:00
|
|
|
name: &str,
|
|
|
|
service: &str,
|
|
|
|
protocol: &str,
|
|
|
|
port: u16,
|
|
|
|
) -> Result<(), Error> {
|
2023-05-24 10:07:11 +00:00
|
|
|
info!(
|
|
|
|
"Deregistering mDNS service {}/{}.{}/{}",
|
|
|
|
name, service, protocol, port
|
|
|
|
);
|
2023-04-24 19:27:17 +00:00
|
|
|
|
2023-06-10 18:47:21 +00:00
|
|
|
let key = self.key(name, service, protocol, port);
|
2023-05-24 10:07:11 +00:00
|
|
|
|
2023-06-10 18:47:21 +00:00
|
|
|
let mut entries = self.entries.borrow_mut();
|
2023-05-24 10:07:11 +00:00
|
|
|
|
|
|
|
let old_len = entries.len();
|
|
|
|
|
|
|
|
entries.retain(|entry| entry.key != key);
|
|
|
|
|
|
|
|
if entries.len() != old_len {
|
2023-06-10 18:47:21 +00:00
|
|
|
self.notification.signal(());
|
2023-04-24 19:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-06-10 18:47:21 +00:00
|
|
|
|
|
|
|
fn key(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
service: &str,
|
|
|
|
protocol: &str,
|
|
|
|
port: u16,
|
|
|
|
) -> heapless::String<64> {
|
|
|
|
let mut key = heapless::String::new();
|
|
|
|
|
|
|
|
write!(&mut key, "{name}.{service}.{protocol}.{port}").unwrap();
|
|
|
|
|
|
|
|
key
|
|
|
|
}
|
2023-04-24 19:27:17 +00:00
|
|
|
}
|
|
|
|
|
2023-06-10 18:47:21 +00:00
|
|
|
pub struct MdnsRunner<'a>(&'a Mdns<'a>);
|
2023-05-24 10:07:11 +00:00
|
|
|
|
2023-06-10 18:47:21 +00:00
|
|
|
impl<'a> MdnsRunner<'a> {
|
|
|
|
pub const fn new(mdns: &'a Mdns<'a>) -> Self {
|
|
|
|
Self(mdns)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_udp(&mut self) -> Result<(), Error> {
|
|
|
|
let mut tx_buf = MdnsTxBuf::uninit();
|
|
|
|
let mut rx_buf = MdnsRxBuf::uninit();
|
|
|
|
|
|
|
|
let tx_buf = &mut tx_buf;
|
|
|
|
let rx_buf = &mut rx_buf;
|
2023-06-10 14:01:35 +00:00
|
|
|
|
|
|
|
let tx_pipe = Pipe::new(unsafe { tx_buf.assume_init_mut() });
|
|
|
|
let rx_pipe = Pipe::new(unsafe { rx_buf.assume_init_mut() });
|
|
|
|
|
|
|
|
let tx_pipe = &tx_pipe;
|
|
|
|
let rx_pipe = &rx_pipe;
|
2023-06-10 18:47:21 +00:00
|
|
|
|
|
|
|
let udp = UdpListener::new(SocketAddr::new(IP_BIND_ADDR.0, IP_BIND_ADDR.1)).await?;
|
2023-06-10 14:01:35 +00:00
|
|
|
let udp = &udp;
|
|
|
|
|
|
|
|
let mut tx = pin!(async move {
|
|
|
|
loop {
|
|
|
|
{
|
|
|
|
let mut data = tx_pipe.data.lock().await;
|
|
|
|
|
|
|
|
if let Some(chunk) = data.chunk {
|
|
|
|
udp.send(chunk.addr.unwrap_udp(), &data.buf[chunk.start..chunk.end])
|
|
|
|
.await?;
|
|
|
|
data.chunk = None;
|
|
|
|
tx_pipe.data_consumed_notification.signal(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tx_pipe.data_supplied_notification.wait().await;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut rx = pin!(async move {
|
|
|
|
loop {
|
|
|
|
{
|
|
|
|
let mut data = rx_pipe.data.lock().await;
|
|
|
|
|
|
|
|
if data.chunk.is_none() {
|
|
|
|
let (len, addr) = udp.recv(data.buf).await?;
|
|
|
|
|
|
|
|
data.chunk = Some(Chunk {
|
|
|
|
start: 0,
|
|
|
|
end: len,
|
|
|
|
addr: Address::Udp(addr),
|
|
|
|
});
|
|
|
|
rx_pipe.data_supplied_notification.signal(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rx_pipe.data_consumed_notification.wait().await;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut run = pin!(async move { self.run(tx_pipe, rx_pipe).await });
|
|
|
|
|
|
|
|
select3(&mut tx, &mut rx, &mut run).await.unwrap()
|
|
|
|
}
|
|
|
|
|
2023-06-10 18:47:21 +00:00
|
|
|
pub async fn run(&self, tx_pipe: &Pipe<'_>, rx_pipe: &Pipe<'_>) -> Result<(), Error> {
|
2023-06-10 14:01:35 +00:00
|
|
|
let mut broadcast = pin!(self.broadcast(tx_pipe));
|
|
|
|
let mut respond = pin!(self.respond(rx_pipe, tx_pipe));
|
2023-05-24 10:07:11 +00:00
|
|
|
|
|
|
|
select(&mut broadcast, &mut respond).await.unwrap()
|
|
|
|
}
|
|
|
|
|
2023-05-28 11:13:02 +00:00
|
|
|
#[allow(clippy::await_holding_refcell_ref)]
|
2023-06-10 14:01:35 +00:00
|
|
|
async fn broadcast(&self, tx_pipe: &Pipe<'_>) -> Result<(), Error> {
|
2023-05-24 10:07:11 +00:00
|
|
|
loop {
|
|
|
|
select(
|
|
|
|
self.0.notification.wait(),
|
|
|
|
Timer::after(Duration::from_secs(30)),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let mut index = 0;
|
|
|
|
|
2023-06-10 14:01:35 +00:00
|
|
|
'outer: loop {
|
|
|
|
for (addr, port) in IP_BROADCAST_ADDRS {
|
|
|
|
loop {
|
|
|
|
{
|
|
|
|
let mut data = tx_pipe.data.lock().await;
|
|
|
|
|
|
|
|
if data.chunk.is_none() {
|
|
|
|
let entries = self.0.entries.borrow();
|
|
|
|
let entry = entries.get(index);
|
|
|
|
|
|
|
|
if let Some(entry) = entry {
|
|
|
|
info!(
|
|
|
|
"Broadasting mDNS entry {} on {}:{}",
|
|
|
|
&entry.key, addr, port
|
|
|
|
);
|
|
|
|
|
|
|
|
let len = entry.record.len();
|
|
|
|
data.buf[..len].copy_from_slice(&entry.record);
|
|
|
|
drop(entries);
|
|
|
|
|
|
|
|
data.chunk = Some(Chunk {
|
|
|
|
start: 0,
|
|
|
|
end: len,
|
|
|
|
addr: Address::Udp(SocketAddr::new(addr, port)),
|
|
|
|
});
|
|
|
|
|
|
|
|
tx_pipe.data_supplied_notification.signal(());
|
|
|
|
} else {
|
|
|
|
break 'outer;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tx_pipe.data_consumed_notification.wait().await;
|
2023-05-28 11:13:02 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-10 14:01:35 +00:00
|
|
|
|
|
|
|
index += 1;
|
2023-05-24 10:07:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-28 11:13:02 +00:00
|
|
|
#[allow(clippy::await_holding_refcell_ref)]
|
2023-06-10 14:01:35 +00:00
|
|
|
async fn respond(&self, rx_pipe: &Pipe<'_>, _tx_pipe: &Pipe<'_>) -> Result<(), Error> {
|
2023-05-24 10:07:11 +00:00
|
|
|
loop {
|
2023-06-10 14:01:35 +00:00
|
|
|
{
|
|
|
|
let mut data = rx_pipe.data.lock().await;
|
2023-05-24 10:07:11 +00:00
|
|
|
|
2023-06-10 14:01:35 +00:00
|
|
|
if let Some(_chunk) = data.chunk {
|
|
|
|
// TODO: Process the incoming packed and only answer what we are being queried about
|
2023-05-24 10:07:11 +00:00
|
|
|
|
2023-06-10 14:01:35 +00:00
|
|
|
data.chunk = None;
|
|
|
|
rx_pipe.data_consumed_notification.signal(());
|
2023-05-24 10:07:11 +00:00
|
|
|
|
2023-06-10 14:01:35 +00:00
|
|
|
self.0.notification.signal(());
|
|
|
|
}
|
|
|
|
}
|
2023-05-24 10:07:11 +00:00
|
|
|
|
2023-06-10 14:01:35 +00:00
|
|
|
rx_pipe.data_supplied_notification.wait().await;
|
2023-05-24 10:07:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-10 18:47:21 +00:00
|
|
|
impl<'a, 'b> super::Mdns for Mdns<'a> {
|
2023-04-24 19:27:17 +00:00
|
|
|
fn add(
|
2023-06-10 14:01:35 +00:00
|
|
|
&self,
|
2023-04-24 19:27:17 +00:00
|
|
|
name: &str,
|
2023-04-24 21:41:16 +00:00
|
|
|
service: &str,
|
|
|
|
protocol: &str,
|
2023-04-24 19:27:17 +00:00
|
|
|
port: u16,
|
|
|
|
service_subtypes: &[&str],
|
|
|
|
txt_kvs: &[(&str, &str)],
|
|
|
|
) -> Result<(), Error> {
|
2023-06-10 18:47:21 +00:00
|
|
|
Mdns::add(
|
2023-04-24 21:41:16 +00:00
|
|
|
self,
|
|
|
|
name,
|
|
|
|
service,
|
|
|
|
protocol,
|
|
|
|
port,
|
|
|
|
service_subtypes,
|
|
|
|
txt_kvs,
|
|
|
|
)
|
2023-04-24 19:27:17 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 21:41:16 +00:00
|
|
|
fn remove(
|
2023-06-10 14:01:35 +00:00
|
|
|
&self,
|
2023-04-24 21:41:16 +00:00
|
|
|
name: &str,
|
|
|
|
service: &str,
|
|
|
|
protocol: &str,
|
|
|
|
port: u16,
|
|
|
|
) -> Result<(), Error> {
|
2023-06-10 18:47:21 +00:00
|
|
|
Mdns::remove(self, name, service, protocol, port)
|
2023-04-24 19:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 10:07:11 +00:00
|
|
|
#[cfg(all(feature = "std", feature = "astro-dnssd"))]
|
|
|
|
pub mod astro {
|
2023-06-10 14:01:35 +00:00
|
|
|
use core::cell::RefCell;
|
2023-05-24 10:07:11 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2023-06-10 18:47:21 +00:00
|
|
|
use crate::{
|
|
|
|
error::{Error, ErrorCode},
|
|
|
|
transport::pipe::Pipe,
|
|
|
|
};
|
2023-05-24 10:07:11 +00:00
|
|
|
use astro_dnssd::{DNSServiceBuilder, RegisteredDnsService};
|
2023-04-24 19:27:17 +00:00
|
|
|
use log::info;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
2023-06-10 18:47:21 +00:00
|
|
|
struct ServiceId {
|
2023-04-24 19:27:17 +00:00
|
|
|
name: String,
|
2023-04-24 21:41:16 +00:00
|
|
|
service: String,
|
|
|
|
protocol: String,
|
2023-04-24 19:27:17 +00:00
|
|
|
port: u16,
|
|
|
|
}
|
|
|
|
|
2023-06-10 18:47:21 +00:00
|
|
|
pub struct Mdns {
|
2023-06-10 14:01:35 +00:00
|
|
|
services: RefCell<HashMap<ServiceId, RegisteredDnsService>>,
|
2023-04-24 19:27:17 +00:00
|
|
|
}
|
|
|
|
|
2023-06-10 18:47:21 +00:00
|
|
|
impl Mdns {
|
|
|
|
pub fn new(_id: u16, _hostname: &str, _ip: [u8; 4], _ipv6: Option<[u8; 16]>) -> Self {
|
|
|
|
Self::native_new()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn native_new() -> Self {
|
|
|
|
Self {
|
2023-06-10 14:01:35 +00:00
|
|
|
services: RefCell::new(HashMap::new()),
|
2023-06-10 18:47:21 +00:00
|
|
|
}
|
2023-04-24 19:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add(
|
2023-06-10 14:01:35 +00:00
|
|
|
&self,
|
2023-04-24 19:27:17 +00:00
|
|
|
name: &str,
|
2023-04-24 21:41:16 +00:00
|
|
|
service: &str,
|
|
|
|
protocol: &str,
|
2023-04-24 19:27:17 +00:00
|
|
|
port: u16,
|
2023-05-24 10:07:11 +00:00
|
|
|
service_subtypes: &[&str],
|
2023-04-24 19:27:17 +00:00
|
|
|
txt_kvs: &[(&str, &str)],
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
info!(
|
2023-05-24 10:07:11 +00:00
|
|
|
"Registering mDNS service {}/{}.{} [{:?}]/{}",
|
|
|
|
name, service, protocol, service_subtypes, port
|
2023-04-24 19:27:17 +00:00
|
|
|
);
|
|
|
|
|
2023-04-24 21:41:16 +00:00
|
|
|
let _ = self.remove(name, service, protocol, port);
|
2023-04-24 19:27:17 +00:00
|
|
|
|
2023-05-24 10:07:11 +00:00
|
|
|
let composite_service_type = if !service_subtypes.is_empty() {
|
|
|
|
format!("{}.{},{}", service, protocol, service_subtypes.join(","))
|
|
|
|
} else {
|
|
|
|
format!("{}.{}", service, protocol)
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut builder = DNSServiceBuilder::new(&composite_service_type, port).with_name(name);
|
|
|
|
|
2023-04-24 19:27:17 +00:00
|
|
|
for kvs in txt_kvs {
|
|
|
|
info!("mDNS TXT key {} val {}", kvs.0, kvs.1);
|
2023-05-24 10:07:11 +00:00
|
|
|
builder = builder.with_key_value(kvs.0.to_string(), kvs.1.to_string());
|
2023-04-24 19:27:17 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 10:07:11 +00:00
|
|
|
let svc = builder.register().map_err(|_| ErrorCode::MdnsError)?;
|
2023-04-24 19:27:17 +00:00
|
|
|
|
2023-06-10 14:01:35 +00:00
|
|
|
self.services.borrow_mut().insert(
|
2023-04-24 19:27:17 +00:00
|
|
|
ServiceId {
|
|
|
|
name: name.into(),
|
2023-04-24 21:41:16 +00:00
|
|
|
service: service.into(),
|
|
|
|
protocol: protocol.into(),
|
2023-04-24 19:27:17 +00:00
|
|
|
port,
|
|
|
|
},
|
2023-04-24 21:41:16 +00:00
|
|
|
svc,
|
2023-04-24 19:27:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-24 21:41:16 +00:00
|
|
|
pub fn remove(
|
2023-06-10 14:01:35 +00:00
|
|
|
&self,
|
2023-04-24 21:41:16 +00:00
|
|
|
name: &str,
|
|
|
|
service: &str,
|
|
|
|
protocol: &str,
|
|
|
|
port: u16,
|
|
|
|
) -> Result<(), Error> {
|
2023-04-24 19:27:17 +00:00
|
|
|
let id = ServiceId {
|
|
|
|
name: name.into(),
|
2023-04-24 21:41:16 +00:00
|
|
|
service: service.into(),
|
|
|
|
protocol: protocol.into(),
|
2023-04-24 19:27:17 +00:00
|
|
|
port,
|
|
|
|
};
|
|
|
|
|
2023-06-10 14:01:35 +00:00
|
|
|
if self.services.borrow_mut().remove(&id).is_some() {
|
2023-04-24 19:27:17 +00:00
|
|
|
info!(
|
2023-04-24 21:41:16 +00:00
|
|
|
"Deregistering mDNS service {}/{}.{}/{}",
|
|
|
|
name, service, protocol, port
|
2023-04-24 19:27:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-10 18:47:21 +00:00
|
|
|
pub struct MdnsRunner<'a>(&'a Mdns);
|
|
|
|
|
|
|
|
impl<'a> MdnsRunner<'a> {
|
|
|
|
pub const fn new(mdns: &'a Mdns) -> Self {
|
|
|
|
Self(mdns)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_udp(&mut self) -> Result<(), Error> {
|
|
|
|
core::future::pending::<Result<(), Error>>().await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run(&self, _tx_pipe: &Pipe<'_>, _rx_pipe: &Pipe<'_>) -> Result<(), Error> {
|
|
|
|
core::future::pending::<Result<(), Error>>().await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl super::Mdns for Mdns {
|
2023-04-24 19:27:17 +00:00
|
|
|
fn add(
|
2023-06-10 14:01:35 +00:00
|
|
|
&self,
|
2023-04-24 19:27:17 +00:00
|
|
|
name: &str,
|
2023-04-24 21:41:16 +00:00
|
|
|
service: &str,
|
|
|
|
protocol: &str,
|
2023-04-24 19:27:17 +00:00
|
|
|
port: u16,
|
2023-05-24 10:07:11 +00:00
|
|
|
service_subtypes: &[&str],
|
2023-04-24 19:27:17 +00:00
|
|
|
txt_kvs: &[(&str, &str)],
|
|
|
|
) -> Result<(), Error> {
|
2023-06-10 18:47:21 +00:00
|
|
|
Mdns::add(
|
2023-05-24 10:07:11 +00:00
|
|
|
self,
|
|
|
|
name,
|
|
|
|
service,
|
|
|
|
protocol,
|
|
|
|
port,
|
|
|
|
service_subtypes,
|
|
|
|
txt_kvs,
|
|
|
|
)
|
2023-04-24 19:27:17 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 21:41:16 +00:00
|
|
|
fn remove(
|
2023-06-10 14:01:35 +00:00
|
|
|
&self,
|
2023-04-24 21:41:16 +00:00
|
|
|
name: &str,
|
|
|
|
service: &str,
|
|
|
|
protocol: &str,
|
|
|
|
port: u16,
|
|
|
|
) -> Result<(), Error> {
|
2023-06-10 18:47:21 +00:00
|
|
|
Mdns::remove(self, name, service, protocol, port)
|
2023-04-24 19:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 13:14:20 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_compute_short_discriminator() {
|
|
|
|
let discriminator: u16 = 0b0000_1111_0000_0000;
|
2023-02-02 18:22:21 +00:00
|
|
|
let short = MdnsMgr::compute_short_discriminator(discriminator);
|
2023-01-10 13:14:20 +01:00
|
|
|
assert_eq!(short, 0b1111);
|
|
|
|
|
|
|
|
let discriminator: u16 = 840;
|
2023-02-02 18:22:21 +00:00
|
|
|
let short = MdnsMgr::compute_short_discriminator(discriminator);
|
2023-01-10 13:14:20 +01:00
|
|
|
assert_eq!(short, 3);
|
|
|
|
}
|
|
|
|
}
|