Skip to content

Commit

Permalink
Merge pull request #18 from angelybo/libmdns_linux
Browse files Browse the repository at this point in the history
Use libdmns on Linux
  • Loading branch information
kedars authored Feb 9, 2023
2 parents 89d9737 + 77a0e3d commit 72f40e0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 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"
42 changes: 34 additions & 8 deletions matter/src/sys/sys_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,43 @@
*/

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

#[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]],
name: &str,
regtype: &str,
port: u16,
txt_kvs: &[[&str; 2]],
) -> Result<SysMdnsService, Error> {
error!("Linux is not yet supported for MDNS Service");
Ok(SysMdnsService {})
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 72f40e0

Please sign in to comment.