attempt removing option

This commit is contained in:
Ulf Lilleengen 2023-02-10 18:30:17 +01:00
parent 7ae47cb1d8
commit 6e68353a93
Failed to extract signature

View file

@ -53,7 +53,7 @@ const MAX_QUERIES: usize = 2;
pub struct StackResources<const SOCK: usize> { pub struct StackResources<const SOCK: usize> {
sockets: [SocketStorage<'static>; SOCK], sockets: [SocketStorage<'static>; SOCK],
#[cfg(feature = "dns")] #[cfg(feature = "dns")]
queries: Option<[Option<dns::DnsQuery>; MAX_QUERIES]>, queries: [Option<dns::DnsQuery>; MAX_QUERIES],
} }
impl<const SOCK: usize> StackResources<SOCK> { impl<const SOCK: usize> StackResources<SOCK> {
@ -63,7 +63,7 @@ impl<const SOCK: usize> StackResources<SOCK> {
Self { Self {
sockets: [SocketStorage::EMPTY; SOCK], sockets: [SocketStorage::EMPTY; SOCK],
#[cfg(feature = "dns")] #[cfg(feature = "dns")]
queries: Some([INIT; MAX_QUERIES]), queries: [INIT; MAX_QUERIES],
} }
} }
} }
@ -117,7 +117,7 @@ struct Inner<D: Driver> {
#[cfg(feature = "dhcpv4")] #[cfg(feature = "dhcpv4")]
dhcp_socket: Option<SocketHandle>, dhcp_socket: Option<SocketHandle>,
#[cfg(feature = "dns")] #[cfg(feature = "dns")]
dns_socket: Option<SocketHandle>, dns_socket: SocketHandle,
} }
pub(crate) struct SocketStack { pub(crate) struct SocketStack {
@ -156,15 +156,7 @@ impl<D: Driver + 'static> Stack<D> {
let next_local_port = (random_seed % (LOCAL_PORT_MAX - LOCAL_PORT_MIN) as u64) as u16 + LOCAL_PORT_MIN; let next_local_port = (random_seed % (LOCAL_PORT_MAX - LOCAL_PORT_MIN) as u64) as u16 + LOCAL_PORT_MIN;
let mut inner = Inner {
device,
link_up: false,
config: None,
#[cfg(feature = "dhcpv4")]
dhcp_socket: None,
#[cfg(feature = "dns")]
dns_socket: None,
};
let mut socket = SocketStack { let mut socket = SocketStack {
sockets, sockets,
iface, iface,
@ -172,12 +164,15 @@ impl<D: Driver + 'static> Stack<D> {
next_local_port, next_local_port,
}; };
#[cfg(feature = "dns")] let mut inner = Inner {
{ device,
if let Some(queries) = resources.queries.take() { link_up: false,
inner.dns_socket = Some(socket.sockets.add(dns::Socket::new(&[], queries))); config: None,
} #[cfg(feature = "dhcpv4")]
} dhcp_socket: None,
#[cfg(feature = "dns")]
dns_socket: socket.sockets.add(dns::Socket::new(&[], &mut resources.queries)),
};
match config { match config {
Config::Static(config) => { Config::Static(config) => {
@ -235,42 +230,29 @@ impl<D: Driver + 'static> Stack<D> {
#[cfg(feature = "dns")] #[cfg(feature = "dns")]
pub async fn dns_query(&self, name: &str, qtype: dns::DnsQueryType) -> Result<Vec<IpAddress, 1>, dns::Error> { pub async fn dns_query(&self, name: &str, qtype: dns::DnsQueryType) -> Result<Vec<IpAddress, 1>, dns::Error> {
let query = self.with_mut(|s, i| { let query = self.with_mut(|s, i| {
if let Some(dns_handle) = i.dns_socket { let socket = s.sockets.get_mut::<dns::Socket>(i.dns_socket);
let socket = s.sockets.get_mut::<dns::Socket>(dns_handle); socket.start_query(s.iface.context(), name, qtype)
match socket.start_query(s.iface.context(), name, qtype) {
Ok(handle) => Ok(handle),
Err(e) => Err(e.into()),
}
} else {
Err(dns::Error::Failed)
}
})?; })?;
use embassy_hal_common::drop::OnDrop; use embassy_hal_common::drop::OnDrop;
let drop = OnDrop::new(|| { let drop = OnDrop::new(|| {
self.with_mut(|s, i| { self.with_mut(|s, i| {
if let Some(dns_handle) = i.dns_socket { let socket = s.sockets.get_mut::<dns::Socket>(i.dns_socket);
let socket = s.sockets.get_mut::<dns::Socket>(dns_handle); socket.cancel_query(query);
socket.cancel_query(query); s.waker.wake();
s.waker.wake();
}
}) })
}); });
let res = poll_fn(|cx| { let res = poll_fn(|cx| {
self.with_mut(|s, i| { self.with_mut(|s, i| {
if let Some(dns_handle) = i.dns_socket { let socket = s.sockets.get_mut::<dns::Socket>(i.dns_socket);
let socket = s.sockets.get_mut::<dns::Socket>(dns_handle); match socket.get_query_result(query) {
match socket.get_query_result(query) { Ok(addrs) => Poll::Ready(Ok(addrs)),
Ok(addrs) => Poll::Ready(Ok(addrs)), Err(dns::GetQueryResultError::Pending) => {
Err(dns::GetQueryResultError::Pending) => { socket.register_query_waker(query, cx.waker());
socket.register_query_waker(query, cx.waker()); Poll::Pending
Poll::Pending
}
Err(e) => Poll::Ready(Err(e.into())),
} }
} else { Err(e) => Poll::Ready(Err(e.into())),
Poll::Ready(Err(dns::Error::Failed))
} }
}) })
}) })
@ -322,11 +304,9 @@ impl<D: Driver + 'static> Inner<D> {
} }
#[cfg(feature = "dns")] #[cfg(feature = "dns")]
if let Some(dns_socket) = self.dns_socket { let socket = s.sockets.get_mut::<smoltcp::socket::dns::Socket>(self.dns_socket);
let socket = s.sockets.get_mut::<smoltcp::socket::dns::Socket>(dns_socket); let servers: Vec<IpAddress, 3> = config.dns_servers.iter().map(|c| IpAddress::Ipv4(*c)).collect();
let servers: Vec<IpAddress, 3> = config.dns_servers.iter().map(|c| IpAddress::Ipv4(*c)).collect(); socket.update_servers(&servers[..]);
socket.update_servers(&servers[..]);
}
self.config = Some(config) self.config = Some(config)
} }