diff --git a/ublox-short-range/src/client.rs b/ublox-short-range/src/client.rs index 7afc4ce..478dbd7 100644 --- a/ublox-short-range/src/client.rs +++ b/ublox-short-range/src/client.rs @@ -53,6 +53,23 @@ pub enum DNSState { Error(PingError), } +pub struct DNSTable{ + pub table: heapless::LinearMap,16> +} + +impl DNSTable { + fn new() -> Self{ + Self { table: heapless::LinearMap::new() } + } + pub fn insert(&mut self, ip: IpAddr, hostname: heapless::String<256>,) -> Result<(), ()>{ + self.table.insert(ip, hostname).ok(); + Ok(()) + } + pub fn get_hostname_by_ip(&self, ip: &IpAddr) -> Option<&heapless::String<256>>{ + self.table.get(ip) + } +} + #[derive(PartialEq, Clone, Default)] pub struct SecurityCredentials { pub ca_cert_name: Option>, @@ -84,6 +101,7 @@ where pub(crate) module_started: bool, pub(crate) initialized: bool, serial_mode: SerialMode, + pub dns_table: DNSTable, pub(crate) wifi_connection: Option, pub(crate) wifi_config_active_on_startup: Option, pub(crate) client: C, @@ -109,6 +127,7 @@ where module_started: false, initialized: false, serial_mode: SerialMode::Cmd, + dns_table: DNSTable::new(), wifi_connection: None, wifi_config_active_on_startup: None, client, diff --git a/ublox-short-range/src/wifi/dns.rs b/ublox-short-range/src/wifi/dns.rs index b75efd8..861c68b 100644 --- a/ublox-short-range/src/wifi/dns.rs +++ b/ublox-short-range/src/wifi/dns.rs @@ -44,7 +44,11 @@ where } match self.dns_state { - DNSState::Resolved(ip) => Ok(ip), + DNSState::Resolved(ip) => { + //Insert hostname and ip into dns table + self.dns_table.insert(ip, heapless::String::from(hostname)).ok(); + Ok(ip) + }, _ => Err(nb::Error::Other(Error::Illegal)), } } diff --git a/ublox-short-range/src/wifi/tcp_stack.rs b/ublox-short-range/src/wifi/tcp_stack.rs index 1c8f7f1..0142cf0 100644 --- a/ublox-short-range/src/wifi/tcp_stack.rs +++ b/ublox-short-range/src/wifi/tcp_stack.rs @@ -35,7 +35,7 @@ where if sockets.len() >= sockets.capacity() { // Check if there are any sockets closed by remote, and close it // if it has exceeded its timeout, in order to recycle it. - if sockets.recycle(self.timer.now()) { + if !sockets.recycle(self.timer.now()) { return Err(Error::SocketSetFull); } } @@ -65,11 +65,19 @@ where defmt::debug!("[TCP] Connect socket"); self.connected_to_network().map_err(|_| Error::Illegal)?; - let url = PeerUrlBuilder::new() - .address(&remote) - .creds(self.security_credentials.clone()) + let mut url = PeerUrlBuilder::new(); + + //Connect with hostname if seen before + if let Some(hostname) = self.dns_table.get_hostname_by_ip(&remote.ip()){ + url.hostname(hostname).port(remote.port()); + }else { + url.address(&remote); + } + + let url = url.creds(self.security_credentials.clone()) .tcp() .map_err(|_| Error::Unaddressable)?; + defmt::debug!("[TCP] Connecting socket: {:?} to url: {=str}", socket, url); // If no socket is found we stop here let mut tcp = self @@ -101,7 +109,7 @@ where } } - defmt::trace!("[TCP] Connecting socket: {:?} to url: {=str}", socket, url); + defmt::debug!("[TCP] Connecting socket: {:?} to url: {=str}", socket, url); // TODO: Timeout? // TODO: Fix the fact that it doesen't wait for both connect messages