Skip to content

Commit

Permalink
Merge pull request #209 from ivmarkov/chiptool-e2e-compat
Browse files Browse the repository at this point in the history
Compatibility with C++ SDK E2E Tests
  • Loading branch information
kedars authored Sep 26, 2024
2 parents 850e02a + 5741625 commit ec62ca9
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 47 deletions.
5 changes: 3 additions & 2 deletions examples/onoff_light/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,10 @@ async fn run_mdns(matter: &Matter<'_>) -> Result<(), Error> {
&Host {
id: 0,
hostname: "rs-matter-demo",
ip: ipv4_addr.octets(),
ipv6: Some(ipv6_addr.octets()),
ip: ipv4_addr,
ipv6: ipv6_addr,
},
Some(ipv4_addr),
Some(interface),
)
.await
Expand Down
5 changes: 3 additions & 2 deletions examples/onoff_light_bt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,10 @@ async fn run_mdns(matter: &Matter<'_>) -> Result<(), Error> {
&Host {
id: 0,
hostname: "rs-matter-demo",
ip: ipv4_addr.octets(),
ipv6: Some(ipv6_addr.octets()),
ip: ipv4_addr,
ipv6: ipv6_addr,
},
Some(ipv4_addr),
Some(interface),
)
.await
Expand Down
5 changes: 3 additions & 2 deletions rs-matter/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,15 @@ impl<'a> Matter<'a> {
send: S,
recv: R,
host: &crate::mdns::Host<'_>,
interface: Option<u32>,
ipv4_interface: Option<core::net::Ipv4Addr>,
ipv6_interface: Option<u32>,
) -> Result<(), Error>
where
S: NetworkSend,
R: NetworkReceive,
{
self.transport_mgr
.run_builtin_mdns(send, recv, host, interface)
.run_builtin_mdns(send, recv, host, ipv4_interface, ipv6_interface)
.await
}

Expand Down
6 changes: 3 additions & 3 deletions rs-matter/src/data_model/sdm/group_key_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ pub const CLUSTER: Cluster<'static> = Cluster {
),
Attribute::new(
AttributesDiscriminants::GroupTable as u16,
Access::RF,
Access::RF.union(Access::NEED_VIEW),
Quality::NONE,
),
Attribute::new(
AttributesDiscriminants::MaxGroupsPerFabric as u16,
Access::READ,
Access::RV,
Quality::FIXED,
),
Attribute::new(
AttributesDiscriminants::MaxGroupKeysPerFabric as u16,
Access::READ,
Access::RV,
Quality::FIXED,
),
],
Expand Down
18 changes: 15 additions & 3 deletions rs-matter/src/mdns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

use core::fmt::Write;

use log::info;

use crate::data_model::cluster_basic_information::BasicInfoConfig;
use crate::error::Error;
use crate::utils::init::{init, Init};
Expand Down Expand Up @@ -152,15 +154,25 @@ impl<'a> Mdns for MdnsImpl<'a> {
MdnsService::Disabled => Ok(()),
MdnsService::Builtin => self.builtin.add(service, mode),
MdnsService::Provided(mdns) => mdns.add(service, mode),
}
}?;

// Do not remove this logging line or change its formatting.
// C++ E2E tests rely on this log line to determine when the mDNS service is published
info!("mDNS service published: {service}::{mode:?}");

Ok(())
}

fn remove(&self, service: &str) -> Result<(), Error> {
match self.service {
MdnsService::Disabled => Ok(()),
MdnsService::Builtin => self.builtin.remove(service),
MdnsService::Provided(mdns) => mdns.remove(service),
}
}?;

info!("mDNS service removed: {service}");

Ok(())
}
}

Expand All @@ -173,7 +185,7 @@ pub struct Service<'a> {
pub txt_kvs: &'a [(&'a str, &'a str)],
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ServiceMode {
/// The commissioned state
Commissioned,
Expand Down
41 changes: 26 additions & 15 deletions rs-matter/src/mdns/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ impl<'a> MdnsImpl<'a> {
tx_buf: SB,
rx_buf: RB,
host: &Host<'_>,
interface: Option<u32>,
ipv4_interface: Option<Ipv4Addr>,
ipv6_interface: Option<u32>,
rand: Rand,
) -> Result<(), Error>
where
Expand All @@ -126,8 +127,18 @@ impl<'a> MdnsImpl<'a> {
{
let send = Mutex::<NoopRawMutex, _>::new(send);

let mut broadcast = pin!(self.broadcast(&send, &tx_buf, host, interface));
let mut respond = pin!(self.respond(&send, recv, &tx_buf, &rx_buf, host, interface, rand));
let mut broadcast =
pin!(self.broadcast(&send, &tx_buf, host, ipv4_interface, ipv6_interface));
let mut respond = pin!(self.respond(
&send,
recv,
&tx_buf,
&rx_buf,
host,
ipv4_interface,
ipv6_interface,
rand
));

select(&mut broadcast, &mut respond).coalesce().await
}
Expand All @@ -137,7 +148,8 @@ impl<'a> MdnsImpl<'a> {
send: &Mutex<impl RawMutex, S>,
buffer: B,
host: &Host<'_>,
interface: Option<u32>,
ipv4_interface: Option<Ipv4Addr>,
ipv6_interface: Option<u32>,
) -> Result<(), Error>
where
S: NetworkSend,
Expand All @@ -150,11 +162,10 @@ impl<'a> MdnsImpl<'a> {
select(&mut notification, &mut timeout).await;

for addr in Iterator::chain(
core::iter::once(SocketAddr::V4(SocketAddrV4::new(
MDNS_IPV4_BROADCAST_ADDR,
MDNS_PORT,
))),
interface
ipv4_interface
.map(|_| SocketAddr::V4(SocketAddrV4::new(MDNS_IPV4_BROADCAST_ADDR, MDNS_PORT)))
.into_iter(),
ipv6_interface
.map(|interface| {
SocketAddr::V6(SocketAddrV6::new(
MDNS_IPV6_BROADCAST_ADDR,
Expand Down Expand Up @@ -186,7 +197,8 @@ impl<'a> MdnsImpl<'a> {
tx_buf: SB,
rx_buf: RB,
host: &Host<'_>,
interface: Option<u32>,
ipv4_interface: Option<Ipv4Addr>,
ipv6_interface: Option<u32>,
rand: Rand,
) -> Result<(), Error>
where
Expand Down Expand Up @@ -220,12 +232,11 @@ impl<'a> MdnsImpl<'a> {
.unwrap_or(true);

let reply_addr = if ipv4 {
Some(SocketAddr::V4(SocketAddrV4::new(
MDNS_IPV4_BROADCAST_ADDR,
MDNS_PORT,
)))
ipv4_interface.map(|_| {
SocketAddr::V4(SocketAddrV4::new(MDNS_IPV4_BROADCAST_ADDR, MDNS_PORT))
})
} else {
interface.map(|interface| {
ipv6_interface.map(|interface| {
SocketAddr::V6(SocketAddrV6::new(
MDNS_IPV6_BROADCAST_ADDR,
MDNS_PORT,
Expand Down
41 changes: 24 additions & 17 deletions rs-matter/src/mdns/proto.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::fmt::Write;
use core::net::{Ipv4Addr, Ipv6Addr};

use bitflags::bitflags;

Expand Down Expand Up @@ -100,8 +101,8 @@ bitflags! {
pub struct Host<'a> {
pub id: u16,
pub hostname: &'a str,
pub ip: [u8; 4],
pub ipv6: Option<[u8; 16]>,
pub ip: Ipv4Addr,
pub ipv6: Ipv6Addr,
}

impl<'a> Host<'a> {
Expand Down Expand Up @@ -422,29 +423,35 @@ impl<'a> Host<'a> {
R: RecordSectionBuilder<T>,
T: Composer,
{
answer.push((
Self::host_fqdn(self.hostname, false).unwrap(),
dns_class_with_flush(Class::IN),
ttl_sec,
A::from_octets(self.ip[0], self.ip[1], self.ip[2], self.ip[3]),
))
if !self.ip.is_unspecified() {
let octets = self.ip.octets();

answer.push((
Self::host_fqdn(self.hostname, false).unwrap(),
dns_class_with_flush(Class::IN),
ttl_sec,
A::from_octets(octets[0], octets[1], octets[2], octets[3]),
))?;
}

Ok(())
}

fn add_ipv6<R, T>(&self, answer: &mut R, ttl_sec: u32) -> Result<(), PushError>
where
R: RecordSectionBuilder<T>,
T: Composer,
{
if let Some(ip) = &self.ipv6 {
if !self.ipv6.is_unspecified() {
answer.push((
Self::host_fqdn(self.hostname, false).unwrap(),
dns_class_with_flush(Class::IN),
ttl_sec,
Aaaa::new((*ip).into()),
))
} else {
Ok(())
Aaaa::new(self.ipv6.octets().into()),
))?;
}

Ok(())
}

fn host_fqdn(hostname: &str, suffix: bool) -> Result<impl ToName, FromStrError> {
Expand Down Expand Up @@ -715,8 +722,8 @@ mod tests {
host: Host {
id: 0,
hostname: "foo",
ip: [192, 168, 0, 1],
ipv6: None,
ip: Ipv4Addr::new(192, 168, 0, 1),
ipv6: Ipv6Addr::UNSPECIFIED,
},
services: &[],

Expand Down Expand Up @@ -760,8 +767,8 @@ mod tests {
host: Host {
id: 1,
hostname: "foo",
ip: [192, 168, 0, 1],
ipv6: Some(Ipv6Addr::new(0xfb, 0, 0, 0, 0, 0, 0, 1).octets()),
ip: Ipv4Addr::new(192, 168, 0, 1),
ipv6: Ipv6Addr::new(0xfb, 0, 0, 0, 0, 0, 0, 1),
},
services: &[
Service {
Expand Down
4 changes: 3 additions & 1 deletion rs-matter/src/pairing/qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ pub enum CommissionningFlowType {
}

pub fn print_qr_code(qr_code_text: &str, buf: &mut [u8]) -> Result<(), Error> {
info!("QR Code Text: {}", qr_code_text);
// Do not remove this logging line or change its formatting.
// C++ E2E tests rely on this log line to grep the QR code
info!("SetupQRCode: [{}]", qr_code_text);

let (tmp_buf, out_buf) = buf.split_at_mut(buf.len() / 2);

Expand Down
6 changes: 4 additions & 2 deletions rs-matter/src/transport/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ impl<'m> TransportMgr<'m> {
send: S,
recv: R,
host: &crate::mdns::Host<'_>,
interface: Option<u32>,
ipv4_interface: Option<core::net::Ipv4Addr>,
ipv6_interface: Option<u32>,
) -> Result<(), Error>
where
S: NetworkSend,
Expand All @@ -303,7 +304,8 @@ impl<'m> TransportMgr<'m> {
&PacketBufferExternalAccess(&self.tx),
&PacketBufferExternalAccess(&self.rx),
host,
interface,
ipv4_interface,
ipv6_interface,
self.rand,
)
.await
Expand Down

0 comments on commit ec62ca9

Please sign in to comment.