Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sys_publish_service for Linux based on libmdns #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions matter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ safemem = "0.3.3"
chrono = { version = "0.4.19", default-features = false, features = ["clock", "std"] }
async-channel = "1.6"

[target.'cfg(target_os = "linux")'.dependencies]
libmdns = "0.7"
lazy_static = "1.4.0"

[target.'cfg(target_os = "macos")'.dependencies]
astro-dnssd = "0.3"

Expand Down
47 changes: 39 additions & 8 deletions matter/src/sys/sys_linux.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
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]],
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})
}