mdns: Discriminator is a property of the 'commissionable' state

This commit is contained in:
Kedar Sovani 2023-01-08 11:13:12 +05:30
parent f135e2dbf8
commit e9ea342bf7
3 changed files with 22 additions and 15 deletions

View file

@ -62,7 +62,7 @@ impl Matter {
dev_comm: CommissioningData, dev_comm: CommissioningData,
) -> Result<Box<Matter>, Error> { ) -> Result<Box<Matter>, Error> {
let mdns = Mdns::get()?; let mdns = Mdns::get()?;
mdns.set_values(dev_det.vid, dev_det.pid, dev_comm.discriminator); mdns.set_values(dev_det.vid, dev_det.pid);
let fabric_mgr = Arc::new(FabricMgr::new()?); let fabric_mgr = Arc::new(FabricMgr::new()?);
let acl_mgr = Arc::new(AclMgr::new()?); let acl_mgr = Arc::new(AclMgr::new()?);
@ -78,7 +78,11 @@ impl Matter {
matter.transport_mgr.register_protocol(interaction_model)?; matter.transport_mgr.register_protocol(interaction_model)?;
let mut secure_channel = Box::new(SecureChannel::new(matter.fabric_mgr.clone())); let mut secure_channel = Box::new(SecureChannel::new(matter.fabric_mgr.clone()));
if open_comm_window { if open_comm_window {
secure_channel.open_comm_window(&dev_comm.salt, dev_comm.passwd)?; secure_channel.open_comm_window(
&dev_comm.salt,
dev_comm.passwd,
dev_comm.discriminator,
)?;
} }
matter.transport_mgr.register_protocol(secure_channel)?; matter.transport_mgr.register_protocol(secure_channel)?;

View file

@ -30,8 +30,6 @@ pub struct MdnsInner {
vid: u16, vid: u16,
/// Product ID /// Product ID
pid: u16, pid: u16,
/// Discriminator
discriminator: u16,
} }
pub struct Mdns { pub struct Mdns {
@ -45,8 +43,10 @@ static mut G_MDNS: Option<Arc<Mdns>> = None;
static INIT: Once = Once::new(); static INIT: Once = Once::new();
pub enum ServiceMode { pub enum ServiceMode {
/// The commissioned state
Commissioned, Commissioned,
Commissionable, /// The commissionable state with the discriminator that should be used
Commissionable(u16),
} }
impl Mdns { impl Mdns {
@ -71,11 +71,10 @@ impl Mdns {
/// Set mDNS service specific values /// Set mDNS service specific values
/// Values like vid, pid, discriminator etc /// Values like vid, pid, discriminator etc
// TODO: More things like device-type etc can be added here // TODO: More things like device-type etc can be added here
pub fn set_values(&self, vid: u16, pid: u16, discriminator: u16) { pub fn set_values(&self, vid: u16, pid: u16) {
let mut inner = self.inner.lock().unwrap(); let mut inner = self.inner.lock().unwrap();
inner.vid = vid; inner.vid = vid;
inner.pid = pid; inner.pid = pid;
inner.discriminator = discriminator;
} }
/// Publish a mDNS service /// Publish a mDNS service
@ -86,13 +85,11 @@ impl Mdns {
ServiceMode::Commissioned => { ServiceMode::Commissioned => {
sys_publish_service(name, "_matter._tcp", MATTER_PORT, &[]) sys_publish_service(name, "_matter._tcp", MATTER_PORT, &[])
} }
ServiceMode::Commissionable => { ServiceMode::Commissionable(discriminator) => {
let inner = self.inner.lock().unwrap(); let short = (discriminator & SHORT_DISCRIMINATOR_MASK) >> SHORT_DISCRIMINATOR_SHIFT;
let short = let serv_type = format!("_matterc._udp,_S{},_L{}", short, discriminator);
(inner.discriminator & SHORT_DISCRIMINATOR_MASK) >> SHORT_DISCRIMINATOR_SHIFT;
let serv_type = format!("_matterc._udp,_S{},_L{}", short, inner.discriminator);
let str_discriminator = format!("{}", inner.discriminator); let str_discriminator = format!("{}", discriminator);
let txt_kvs = [["D", &str_discriminator], ["CM", "1"]]; let txt_kvs = [["D", &str_discriminator], ["CM", "1"]];
sys_publish_service(name, &serv_type, MATTER_PORT, &txt_kvs) sys_publish_service(name, &serv_type, MATTER_PORT, &txt_kvs)
} }

View file

@ -48,10 +48,16 @@ impl SecureChannel {
} }
} }
pub fn open_comm_window(&mut self, salt: &[u8; 16], passwd: u32) -> Result<(), Error> { pub fn open_comm_window(
&mut self,
salt: &[u8; 16],
passwd: u32,
discriminator: u16,
) -> Result<(), Error> {
let name: u64 = rand::thread_rng().gen_range(0..0xFFFFFFFFFFFFFFFF); let name: u64 = rand::thread_rng().gen_range(0..0xFFFFFFFFFFFFFFFF);
let name = format!("{:016X}", name); let name = format!("{:016X}", name);
let mdns = Mdns::get()?.publish_service(&name, mdns::ServiceMode::Commissionable)?; let mdns = Mdns::get()?
.publish_service(&name, mdns::ServiceMode::Commissionable(discriminator))?;
self.pake = Some((PAKE::new(salt, passwd), mdns)); self.pake = Some((PAKE::new(salt, passwd), mdns));
Ok(()) Ok(())
} }