Skip to content

Commit

Permalink
Use linux libmdns based MDNS implementation from kedars/matter-rs#18.
Browse files Browse the repository at this point in the history
Use patched libmdns to respond properly to All (255) querie types.
  • Loading branch information
angelybo committed Feb 8, 2023
1 parent 89d9737 commit 7e1dc7d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
6 changes: 6 additions & 0 deletions matter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ qrcode = { version = "0.12", default-features = false }
[target.'cfg(target_os = "macos")'.dependencies]
astro-dnssd = "0.3"

# MDNS support
[target.'cfg(target_os = "linux")'.dependencies]
lazy_static = "1.4.0"
# Patched copy of libmdns
libmdns = { git = "https://github.com/angelybo/libmdns.git", branch = "return_all_on_query" }

[[example]]
name = "onoff_light"
path = "../examples/onoff_light/src/main.rs"
53 changes: 43 additions & 10 deletions matter/src/sys/sys_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,50 @@
*/

use crate::error::Error;
use log::error;
use std::vec::Vec;
use log::info;
use lazy_static::lazy_static;
use std::sync::{Arc, Mutex};
use libmdns::{Service, Responder};

#[allow(dead_code)]
pub struct SysMdnsService {}
pub struct SysMdnsService {
service: Service,
}

lazy_static!{
static ref RESPONDER: Arc<Mutex<Responder>> = Arc::new(Mutex::new(Responder::new().unwrap()));
}



/// Publish a mDNS service
/// name - can be a service name (comma separate subtypes may follow)
/// regtype - registration type (e.g. _matter_.tcp etc)
/// port - the port
pub fn sys_publish_service(
_name: &str,
_regtype: &str,
_port: u16,
_txt_kvs: &[[&str; 2]],
) -> Result<SysMdnsService, Error> {
error!("Linux is not yet supported for MDNS Service");
Ok(SysMdnsService {})
}
name: &str,
regtype: &str,
port: u16,
txt_kvs: &[[&str; 2]],
) -> Result<SysMdnsService, Error> {
info!("mDNS Registration Type {}", regtype);
info!("mDNS properties {:?}", txt_kvs);

let mut properties = Vec::new();
for kvs in txt_kvs {
info!("mDNS TXT key {} val {}", kvs[0], kvs[1]);
properties.push(format!("{}={}", kvs[0], kvs[1]));
}
let properties: Vec<&str> = properties.iter().map(|entry| entry.as_str()).collect();

let responder = RESPONDER.lock().map_err(|_| Error::MdnsError)?;
let service = responder.register(
regtype.to_owned(),
name.to_owned(),
port,
&properties,
);

Ok(SysMdnsService {service})
}

0 comments on commit 7e1dc7d

Please sign in to comment.