Skip to content
Draft
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
30 changes: 28 additions & 2 deletions src/kernel/mdns.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
use std::time::Duration;
use std::{net::IpAddr, time::Duration};

use anyhow::{anyhow, Result};
use esp_idf_svc::mdns::{EspMdns, Interface, Protocol, QueryResult};
use esp_idf_sys::ESP_ERR_NOT_FOUND;

#[derive(Debug, Clone)]
pub struct Service {
pub hostname: String,
pub addr: IpAddr,
pub port: u16,
}

pub fn query_mdns(mdns: &EspMdns, service: &str, proto: &str) -> anyhow::Result<Option<Service>> {
pub fn query_mdns_address(mdns: &EspMdns, server: &str, port: u16) -> Result<Option<Service>> {
let response = mdns.query_a(server, Duration::from_secs(5));
log::info!("MDNS query result: {:?}", response);
match response {
Ok(addr) => {
let addr = IpAddr::V4(addr);
Ok(Some(Service {
hostname: server.to_string(),
addr,
port,
}))
}
Err(e) => {
if e.code() == ESP_ERR_NOT_FOUND {
Ok(None)
} else {
Err(anyhow!(e.to_string()))
}
}
}
}

pub fn query_mdns(mdns: &EspMdns, service: &str, proto: &str) -> Result<Option<Service>> {
let mut results = [QueryResult {
instance_name: None,
hostname: None,
Expand All @@ -23,6 +48,7 @@ pub fn query_mdns(mdns: &EspMdns, service: &str, proto: &str) -> anyhow::Result<
let result = results[0].clone();
Ok(result.hostname.map(|hostname| Service {
hostname: format!("{}.local", hostname),
addr: result.addr[0],
port: result.port,
}))
}