Skip to content

Commit

Permalink
Add support to query DNS
Browse files Browse the repository at this point in the history
  • Loading branch information
zargony committed Sep 24, 2024
1 parent 095561d commit a36214d
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions firmware/src/wifi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::fmt;
use embassy_executor::{task, Spawner};
use embassy_net::{Config, DhcpConfig, Stack, StackResources, StaticConfigV4};
use embassy_net::dns::{self, DnsQueryType};
use embassy_net::{Config, DhcpConfig, IpAddress, Stack, StackResources, StaticConfigV4};
use embassy_time::{Duration, Timer};
use esp_hal::clock::Clocks;
use esp_hal::peripherals;
Expand Down Expand Up @@ -173,7 +174,8 @@ impl Wifi {
self.stack.is_link_up() && self.stack.is_config_up()
}

/// Wait for network stack to come up (Wifi connected and IP address obtained)
/// Wait for network stack to come up (Wifi connected and IP address obtained). This function
/// can potentially take forever, e.g. if Wifi credentials are wrong or DHCP doesn't work.
pub async fn wait_up(&self) {
if self.is_up() {
return;
Expand All @@ -195,6 +197,25 @@ impl Wifi {
None => panic!("Failed to retrieve IPv4 network configuration"),
}
}

/// Query DNS for IP address of given name
#[allow(dead_code)]
pub async fn dns_query(&self, name: &str) -> Result<IpAddress, dns::Error> {
match self.stack.dns_query(name, DnsQueryType::A).await {
Ok(addrs) if addrs.is_empty() => {
warn!("Wifi: DNS query {} returned empty result", name);
Err(dns::Error::Failed)
}
Ok(addrs) => {
debug!("Wifi: DNS query {}: {}", name, DisplayList(&addrs));
Ok(addrs[0])
}
Err(err) => {
warn!("Wifi: DNS query {} error: {:?}", name, err);
Err(err)
}
}
}
}

/// Task for handling Wifi connection events
Expand Down

0 comments on commit a36214d

Please sign in to comment.