From 298f657810d8bfd01f904930b4e06e6ca6377b05 Mon Sep 17 00:00:00 2001 From: Mathias Date: Wed, 30 Aug 2023 14:09:30 +0200 Subject: [PATCH] Use latest atat, with new urc channel --- Cargo.toml | 2 +- ublox-short-range/Cargo.toml | 7 +- ublox-short-range/src/client.rs | 376 ++++++++++++----------- ublox-short-range/src/config.rs | 10 - ublox-short-range/src/lib.rs | 19 +- ublox-short-range/src/test_helper.rs | 22 -- ublox-short-range/src/wifi/ap.rs | 7 +- ublox-short-range/src/wifi/dns.rs | 7 +- ublox-short-range/src/wifi/supplicant.rs | 9 +- ublox-short-range/src/wifi/tcp_stack.rs | 25 +- ublox-short-range/src/wifi/tls.rs | 7 +- ublox-short-range/src/wifi/udp_stack.rs | 32 +- 12 files changed, 261 insertions(+), 262 deletions(-) delete mode 100644 ublox-short-range/src/test_helper.rs diff --git a/Cargo.toml b/Cargo.toml index bf3e86f..c73ac94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,4 @@ members = [ "ublox-short-range" ] [patch.crates-io] -atat = { git = "https://github.com/BlackbirdHQ/atat", rev = "70283be" } +atat = { git = "https://github.com/BlackbirdHQ/atat", rev = "c5caaf7" } diff --git a/ublox-short-range/Cargo.toml b/ublox-short-range/Cargo.toml index 0f6e4a3..497d6c9 100644 --- a/ublox-short-range/Cargo.toml +++ b/ublox-short-range/Cargo.toml @@ -16,16 +16,19 @@ doctest = false [dependencies] # atat = { version = "0.18.0", features = ["derive", "defmt", "bytes"] } -atat = { git = "https://github.com/BlackbirdHQ/atat", rev = "70283be", features = ["derive", "defmt", "bytes"] } +atat = { git = "https://github.com/BlackbirdHQ/atat", rev = "c5caaf7", features = ["derive", "defmt", "bytes"] } heapless = { version = "^0.7", features = ["serde", "defmt-impl"] } no-std-net = { version = "^0.5", features = ["serde"] } serde = { version = "^1", default-features = false, features = ["derive"] } -ublox-sockets = { version = "0.5", features = ["defmt"] } +# ublox-sockets = { version = "0.5", features = ["defmt"] } +ublox-sockets = { git = "https://github.com/BlackbirdHQ/ublox-sockets", rev = "b1ff942", features = ["defmt"] } + hash32 = "^0.2.1" hash32-derive = "^0.1.0" defmt = { version = "0.3" } embedded-hal = "=1.0.0-rc.1" +embedded-io = "0.5" embedded-nal = "0.6.0" fugit = { version = "0.3", features = ["defmt"] } fugit-timer = "0.1.2" diff --git a/ublox-short-range/src/client.rs b/ublox-short-range/src/client.rs index 35428f4..c87f8c8 100644 --- a/ublox-short-range/src/client.rs +++ b/ublox-short-range/src/client.rs @@ -2,6 +2,7 @@ use core::str::FromStr; use crate::{ command::{ + custom_digest::EdmDigester, data_mode::{ types::{IPProtocol, PeerConfigParameter}, SetPeerConfiguration, @@ -29,7 +30,9 @@ use crate::{ supplicant::Supplicant, SocketMap, }, + UbloxWifiBuffers, UbloxWifiIngress, UbloxWifiUrcChannel, }; +use atat::{blocking::AtatClient, AtatUrcChannel, UrcSubscription}; use defmt::{debug, error, trace}; use embedded_hal::digital::OutputPin; use embedded_nal::{nb, IpAddr, Ipv4Addr, SocketAddr}; @@ -136,9 +139,7 @@ pub struct SecurityCredentials { /// Creates new socket numbers /// Properly not Async safe -pub fn new_socket_num( - sockets: &SocketSet, -) -> Result { +pub fn new_socket_num(sockets: &SocketSet) -> Result { let mut num = 0; while sockets.socket_type(SocketHandle(num)).is_some() { num += 1; @@ -149,9 +150,20 @@ pub fn new_socket_num( Ok(num) } -pub struct UbloxClient -where - C: atat::blocking::AtatClient, +pub(crate) const URC_CAPACITY: usize = 1 + 3; +pub(crate) const URC_SUBSCRIBERS: usize = 1; + +pub struct UbloxClient< + 'buf, + 'sub, + AtCl, + AtUrcCh, + CLK, + RST, + const TIMER_HZ: u32, + const N: usize, + const L: usize, +> where CLK: fugit_timer::Timer, RST: OutputPin, { @@ -161,24 +173,76 @@ where pub dns_table: DNSTable, pub(crate) wifi_connection: Option, pub(crate) wifi_config_active_on_startup: Option, - pub(crate) client: C, + pub(crate) client: AtCl, pub(crate) config: Config, - pub(crate) sockets: Option<&'static mut SocketSet>, - pub(crate) urc_attempts: u8, + pub(crate) sockets: Option<&'static mut SocketSet>, pub(crate) security_credentials: SecurityCredentials, pub(crate) timer: CLK, pub(crate) socket_map: SocketMap, pub(crate) udp_listener: UdpListener<4, N>, + urc_subscription: UrcSubscription<'sub, EdmEvent, URC_CAPACITY, URC_SUBSCRIBERS>, + urc_channel: &'buf AtUrcCh, +} + +impl< + 'buf, + 'sub, + W, + CLK, + RST, + const TIMER_HZ: u32, + const INGRESS_BUF_SIZE: usize, + const N: usize, + const L: usize, + > + UbloxClient< + 'buf, + 'sub, + atat::blocking::Client<'buf, W, INGRESS_BUF_SIZE>, + UbloxWifiUrcChannel, + CLK, + RST, + TIMER_HZ, + N, + L, + > +where + 'buf: 'sub, + W: embedded_io::Write, + CLK: fugit_timer::Timer, + RST: OutputPin, +{ + /// Create new u-blox device + /// + /// Look for [`data_service`](Device::data_service) how to handle data connection automatically. + /// + pub fn from_buffers( + buffers: &'buf UbloxWifiBuffers, + tx: W, + timer: CLK, + config: Config, + ) -> (UbloxWifiIngress, Self) { + let (ingress, client) = + buffers.split_blocking(tx, EdmDigester::default(), atat::Config::default()); + + ( + ingress, + UbloxClient::new(client, &buffers.urc_channel, timer, config), + ) + } } -impl - UbloxClient +impl<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, const TIMER_HZ: u32, const N: usize, const L: usize> + UbloxClient<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, TIMER_HZ, N, L> where - C: atat::blocking::AtatClient, + 'buf: 'sub, + AtCl: AtatClient, + AtUrcCh: AtatUrcChannel, CLK: fugit_timer::Timer, RST: OutputPin, { - pub fn new(client: C, timer: CLK, config: Config) -> Self { + pub fn new(client: AtCl, urc_channel: &'buf AtUrcCh, timer: CLK, config: Config) -> Self { + let urc_subscription = urc_channel.subscribe().unwrap(); UbloxClient { module_started: false, initialized: false, @@ -189,20 +253,30 @@ where client, config, sockets: None, - urc_attempts: 0, security_credentials: SecurityCredentials::default(), timer, socket_map: SocketMap::default(), udp_listener: UdpListener::new(), + urc_subscription, + urc_channel, } } +} - pub fn set_socket_storage(&mut self, socket_set: &'static mut SocketSet) { +impl<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, const TIMER_HZ: u32, const N: usize, const L: usize> + UbloxClient<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, TIMER_HZ, N, L> +where + 'buf: 'sub, + AtCl: AtatClient, + CLK: fugit_timer::Timer, + RST: OutputPin, +{ + pub fn set_socket_storage(&mut self, socket_set: &'static mut SocketSet) { socket_set.prune(); self.sockets.replace(socket_set); } - pub fn take_socket_storage(&mut self) -> Option<&'static mut SocketSet> { + pub fn take_socket_storage(&mut self) -> Option<&'static mut SocketSet> { self.sockets.take() } @@ -227,7 +301,7 @@ where self.send_internal(&SwitchToEdmCommand, true).ok(); self.timer.start(100.millis()).map_err(|_| Error::Timer)?; nb::block!(self.timer.wait()).map_err(|_| Error::Timer)?; - while self.handle_urc()? {} + self.handle_urc()?; } // TODO: handle EDM settings quirk see EDM datasheet: 2.2.5.1 AT Request Serial settings @@ -267,7 +341,7 @@ where self.send_internal(&SwitchToEdmCommand, true).ok(); self.timer.start(100.millis()).map_err(|_| Error::Timer)?; nb::block!(self.timer.wait()).map_err(|_| Error::Timer)?; - while self.handle_urc()? {} + self.handle_urc()?; } if self.firmware_version()? < FirmwareVersion::new(8, 0, 0) { @@ -343,7 +417,6 @@ where self.module_started = false; self.wifi_connection = None; self.wifi_config_active_on_startup = None; - self.urc_attempts = 0; self.security_credentials = SecurityCredentials::default(); self.socket_map = SocketMap::default(); self.udp_listener = UdpListener::new(); @@ -381,7 +454,6 @@ where self.module_started = false; self.wifi_connection = None; self.wifi_config_active_on_startup = None; - self.urc_attempts = 0; self.security_credentials = SecurityCredentials::default(); self.socket_map = SocketMap::default(); self.udp_listener = UdpListener::new(); @@ -426,7 +498,7 @@ where return Err(Error::Uninitialized); } - while self.handle_urc()? {} + self.handle_urc()?; self.connected_to_network()?; @@ -458,20 +530,9 @@ where }) } - fn handle_urc(&mut self) -> Result { - let mut ran = false; - let socket_set = self.sockets.as_deref_mut(); - let socket_map = &mut self.socket_map; - let udp_listener = &mut self.udp_listener; - let wifi_connection = &mut self.wifi_connection; - let ts = self.timer.now(); - - let mut a = self.urc_attempts; - let max = self.config.max_urc_attempts; - - self.client.try_read_urc_with::(|edm_urc, _| { - ran = true; - let res = match edm_urc { + fn handle_urc(&mut self) -> Result<(), Error> { + if let Some(edm_urc) = self.urc_subscription.try_next_message_pure() { + match edm_urc { EdmEvent::ATEvent(urc) => { match urc { Urc::StartUp => { @@ -479,7 +540,6 @@ where self.module_started = true; self.initialized = false; self.serial_mode = SerialMode::Cmd; - true } Urc::PeerConnected(event) => { debug!("[URC] PeerConnected"); @@ -493,8 +553,7 @@ where // `udp.update_handle(*socket);` here, to make // sure that part also works without EDM mode - - if let Some(sockets) = socket_set { + if let Some(sockets) = self.sockets.as_deref_mut() { let remote_ip = Ipv4Addr::from_str( core::str::from_utf8(event.remote_address.as_slice()).unwrap(), ) @@ -502,32 +561,31 @@ where let remote = SocketAddr::new(remote_ip.into(), event.remote_port); - if let Some(queue) = udp_listener.incoming(event.local_port) { + if let Some(queue) = self.udp_listener.incoming(event.local_port) { trace!("[UDP Server] Server socket incomming"); - let mut handled = true; 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. // TODO Is this correct? - if !sockets.recycle(self.timer.now()) { - handled = false; - } + if !sockets.recycle() {} } let peer_handle = event.handle; - let socket_handle = SocketHandle(new_socket_num(sockets).unwrap()); + let socket_handle = + SocketHandle(new_socket_num(sockets).unwrap()); let mut new_socket = UdpSocket::new(socket_handle.0); new_socket.set_state(UdpState::Established); - if new_socket.bind(remote).is_err(){ + if new_socket.bind(remote).is_err() { error!("[UDP_URC] Binding connecting socket Error"); - handled = false } - if sockets.add(new_socket).is_err(){ + if sockets.add(new_socket).is_err() { error!("[UDP_URC] Opening socket Error: Socket set full"); - handled = false; } - if socket_map.insert_peer(peer_handle, socket_handle).is_err(){ + if self + .socket_map + .insert_peer(peer_handle, socket_handle) + .is_err() + { error!("[UDP_URC] Opening socket Error: Socket Map full"); - handled = false; } debug!( "[URC] Binding remote {=[u8]:a} to UDP server on port: {:?} with handle: {:?}", @@ -535,10 +593,7 @@ where event.local_port, socket_handle ); - if queue.enqueue((socket_handle, remote)).is_err(){ - handled = false - } - handled + queue.enqueue((socket_handle, remote)).ok(); } else { match event.protocol { IPProtocol::TCP => { @@ -556,7 +611,7 @@ where } IPProtocol::UDP => { // if let Ok(mut udp) = - // sockets.get::>(event.handle) + // sockets.get::>(event.handle) // { // debug!( // "Binding remote {=[u8]:a} to UDP socket {:?}", @@ -569,27 +624,24 @@ where // } } } - true } - } else { - true } } Urc::PeerDisconnected(msg) => { debug!("[URC] PeerDisconnected"); - if let Some(sockets) = socket_set { - if let Some(handle) = socket_map.peer_to_socket(&msg.handle) { + if let Some(sockets) = self.sockets.as_deref_mut() { + if let Some(handle) = self.socket_map.peer_to_socket(&msg.handle) { match sockets.socket_type(*handle) { Some(SocketType::Tcp) => { if let Ok(mut tcp) = - sockets.get::>(*handle) + sockets.get::>(*handle) { - tcp.closed_by_remote(ts); + tcp.closed_by_remote(); } } Some(SocketType::Udp) => { if let Ok(mut udp) = - sockets.get::>(*handle) + sockets.get::>(*handle) { udp.close(); } @@ -597,20 +649,19 @@ where } _ => {} } - socket_map.remove_peer(&msg.handle); + self.socket_map.remove_peer(&msg.handle); } } - true } Urc::WifiLinkConnected(msg) => { debug!("[URC] WifiLinkConnected"); - if let Some(ref mut con) = wifi_connection { + if let Some(ref mut con) = self.wifi_connection { con.wifi_state = WiFiState::Connected; con.network.bssid = msg.bssid; con.network.channel = msg.channel; } else { debug!("[URC] Active network config discovered"); - wifi_connection.replace( + self.wifi_connection.replace( WifiConnection::new( WifiNetwork { bssid: msg.bssid, @@ -628,11 +679,10 @@ where ).activate() ); } - true } Urc::WifiLinkDisconnected(msg) => { debug!("[URC] WifiLinkDisconnected"); - if let Some(con) = wifi_connection { + if let Some(ref mut con) = self.wifi_connection { match msg.reason { DisconnectReason::NetworkDisabled => { con.wifi_state = WiFiState::Inactive; @@ -645,38 +695,31 @@ where } } } - true } Urc::WifiAPUp(_) => { debug!("[URC] WifiAPUp"); - true } Urc::WifiAPDown(_) => { debug!("[URC] WifiAPDown"); - true } Urc::WifiAPStationConnected(client) => { debug!( "[URC] WifiAPStationConnected {=[u8]:a}", client.mac_addr.into_inner() ); - true } Urc::WifiAPStationDisconnected(_) => { debug!("[URC] WifiAPStationDisconnected"); - true } Urc::EthernetLinkUp(_) => { debug!("[URC] EthernetLinkUp"); - true } Urc::EthernetLinkDown(_) => { debug!("[URC] EthernetLinkDown"); - true } Urc::NetworkUp(_) => { debug!("[URC] NetworkUp"); - if let Some(con) = wifi_connection { + if let Some(ref mut con) = self.wifi_connection { if self.config.network_up_bug { match con.network_state { NetworkState::Attached => (), @@ -691,27 +734,25 @@ where con.network_state = NetworkState::Attached; } } - true } Urc::NetworkDown(_) => { debug!("[URC] NetworkDown"); - if let Some(con) = wifi_connection { + if let Some(ref mut con) = self.wifi_connection { con.network_state = NetworkState::Unattached; } - true } Urc::NetworkError(_) => { debug!("[URC] NetworkError"); - true } Urc::PingResponse(resp) => { debug!("[URC] PingResponse"); - self.dns_table.upsert(DNSTableEntry { domain_name: resp.hostname, state: DNSState::Resolved(resp.ip) }); - true + self.dns_table.upsert(DNSTableEntry { + domain_name: resp.hostname, + state: DNSState::Resolved(resp.ip), + }); } Urc::PingErrorResponse(resp) => { debug!("[URC] PingErrorResponse: {:?}", resp.error); - true } } } // end match urc @@ -719,7 +760,6 @@ where debug!("[EDM_URC] STARTUP"); self.module_started = true; self.serial_mode = SerialMode::ExtendedData; - true } EdmEvent::IPv4ConnectEvent(event) => { debug!( @@ -727,45 +767,43 @@ where event.channel_id ); - if let Some(sockets) = socket_set { + if let Some(sockets) = self.sockets.as_deref_mut() { let endpoint = SocketAddr::new(event.remote_ip.into(), event.remote_port); // This depends upon Connected AT-URC to arrive first. - if let Some(queue) = udp_listener.incoming(event.local_port) { - if let Some((socket_handle, _ )) = queue.into_iter().find(|(_, remote)| remote == &endpoint) { - socket_map.insert_channel(event.channel_id, *socket_handle).is_ok() - } else { - false + if let Some(queue) = self.udp_listener.incoming(event.local_port) { + if let Some((socket_handle, _)) = + queue.into_iter().find(|(_, remote)| remote == &endpoint) + { + self.socket_map + .insert_channel(event.channel_id, *socket_handle) + .ok(); } } else { - sockets - .iter_mut() - .find_map(|(h, s)| { - match event.protocol { - Protocol::TCP => { - let mut tcp = TcpSocket::downcast(s).ok()?; - if tcp.endpoint() == Some(endpoint) { - socket_map.insert_channel(event.channel_id, h).ok(); - tcp.set_state(TcpState::Connected(endpoint)); - return Some(true); - } + for (h, s) in sockets.iter_mut() { + match event.protocol { + Protocol::TCP => { + let mut tcp = TcpSocket::downcast(s)?; + if tcp.endpoint() == Some(endpoint) { + self.socket_map + .insert_channel(event.channel_id, h) + .ok(); + tcp.set_state(TcpState::Connected(endpoint)); } - Protocol::UDP => { - let mut udp = UdpSocket::downcast(s).ok()?; - if udp.endpoint() == Some(endpoint) { - socket_map.insert_channel(event.channel_id, h).ok(); - udp.set_state(UdpState::Established); - return Some(true); - } + } + Protocol::UDP => { + let mut udp = UdpSocket::downcast(s)?; + if udp.endpoint() == Some(endpoint) { + self.socket_map + .insert_channel(event.channel_id, h) + .ok(); + udp.set_state(UdpState::Established); } - _ => {} } - None - }) - .is_some() + _ => {} + } + } } - } else { - true } } EdmEvent::IPv6ConnectEvent(event) => { @@ -774,118 +812,88 @@ where event.channel_id ); - if let Some(sockets) = socket_set { + if let Some(sockets) = self.sockets.as_deref_mut() { let endpoint = SocketAddr::new(event.remote_ip.into(), event.remote_port); // This depends upon Connected AT-URC to arrive first. - if let Some(queue) = udp_listener.incoming(event.local_port) { - if let Some((socket_handle, _ )) = queue.into_iter().find(|(_, remote)| remote == &endpoint) { - socket_map.insert_channel(event.channel_id, *socket_handle).is_ok() - } else { - false + if let Some(queue) = self.udp_listener.incoming(event.local_port) { + if let Some((socket_handle, _)) = + queue.into_iter().find(|(_, remote)| remote == &endpoint) + { + self.socket_map + .insert_channel(event.channel_id, *socket_handle) + .ok(); } } else { - sockets - .iter_mut() - .find_map(|(h, s)| { - match event.protocol { - Protocol::TCP => { - let mut tcp = TcpSocket::downcast(s).ok()?; - if tcp.endpoint() == Some(endpoint) { - socket_map.insert_channel(event.channel_id, h).ok(); - tcp.set_state(TcpState::Connected(endpoint)); - return Some(true); - } + for (h, s) in sockets.iter_mut() { + match event.protocol { + Protocol::TCP => { + let mut tcp = TcpSocket::downcast(s)?; + if tcp.endpoint() == Some(endpoint) { + self.socket_map + .insert_channel(event.channel_id, h) + .ok(); + tcp.set_state(TcpState::Connected(endpoint)); } - Protocol::UDP => { - let mut udp = UdpSocket::downcast(s).ok()?; - if udp.endpoint() == Some(endpoint) { - socket_map.insert_channel(event.channel_id, h).ok(); - udp.set_state(UdpState::Established); - return Some(true); - } + } + Protocol::UDP => { + let mut udp = UdpSocket::downcast(s)?; + if udp.endpoint() == Some(endpoint) { + self.socket_map + .insert_channel(event.channel_id, h) + .ok(); + udp.set_state(UdpState::Established); } - _ => {} } - None - }) - .is_some() + _ => {} + } + } } - } else { - true } } EdmEvent::BluetoothConnectEvent(_) => { debug!("[EDM_URC] BluetoothConnectEvent"); - true } EdmEvent::DisconnectEvent(channel_id) => { debug!("[EDM_URC] DisconnectEvent! Channel_id: {:?}", channel_id); - socket_map.remove_channel(&channel_id); - true + self.socket_map.remove_channel(&channel_id); } EdmEvent::DataEvent(event) => { debug!("[EDM_URC] DataEvent! Channel_id: {:?}", event.channel_id); - if let Some(sockets) = socket_set { + if let Some(sockets) = self.sockets.as_deref_mut() { if !event.data.is_empty() { if let Some(socket_handle) = - socket_map.channel_to_socket(&event.channel_id) + self.socket_map.channel_to_socket(&event.channel_id) { match sockets.socket_type(*socket_handle) { Some(SocketType::Tcp) => { // Handle tcp socket - let mut tcp = sockets - .get::>(*socket_handle) - .unwrap(); + let mut tcp = + sockets.get::>(*socket_handle).unwrap(); if tcp.can_recv() { tcp.rx_enqueue_slice(&event.data); - true - } else { - false } } Some(SocketType::Udp) => { // Handle udp socket - let mut udp = sockets - .get::>(*socket_handle) - .unwrap(); + let mut udp = + sockets.get::>(*socket_handle).unwrap(); if udp.can_recv() { udp.rx_enqueue_slice(&event.data); - true - } else { - false } } _ => { error!("SocketNotFound {:?}", socket_handle); - false } } - } else { - false } - } else { - false } - } else { - true } } - }; // end match edm-urc - if !res { - if a < max { - error!("[EDM_URC] URC handeling failed"); - a += 1; - return false; - } - error!("[EDM_URC] URC thrown away"); - } - a = 0; - true - }); - self.urc_attempts = a; - Ok(ran) + }; + }; + Ok(()) } /// Send AT command @@ -903,7 +911,7 @@ where } } - pub fn supplicant(&mut self) -> Result, Error> { + pub fn supplicant(&mut self) -> Result, Error> { // TODO: better solution if !self.initialized { return Err(Error::Uninitialized); diff --git a/ublox-short-range/src/config.rs b/ublox-short-range/src/config.rs index ccba3a5..2002734 100644 --- a/ublox-short-range/src/config.rs +++ b/ublox-short-range/src/config.rs @@ -23,7 +23,6 @@ pub struct Config { pub(crate) hostname: Option>, pub(crate) tls_in_buffer_size: Option, pub(crate) tls_out_buffer_size: Option, - pub(crate) max_urc_attempts: u8, pub(crate) network_up_bug: bool, } @@ -34,7 +33,6 @@ impl Default for Config { hostname: None, tls_in_buffer_size: None, tls_out_buffer_size: None, - max_urc_attempts: 5, network_up_bug: true, } } @@ -50,7 +48,6 @@ where hostname: None, tls_in_buffer_size: None, tls_out_buffer_size: None, - max_urc_attempts: 5, network_up_bug: true, } } @@ -69,13 +66,6 @@ where } } - pub fn max_urc_attempts(self, max_attempts: u8) -> Self { - Config { - max_urc_attempts: max_attempts, - ..self - } - } - /// Experimental use of undocumented setting for TLS buffers /// /// For Odin: diff --git a/ublox-short-range/src/lib.rs b/ublox-short-range/src/lib.rs index b8305db..692b0aa 100644 --- a/ublox-short-range/src/lib.rs +++ b/ublox-short-range/src/lib.rs @@ -5,14 +5,27 @@ mod hex; pub use atat; pub use client::UbloxClient; +use client::{URC_CAPACITY, URC_SUBSCRIBERS}; pub mod command; pub mod config; pub mod error; pub mod wifi; -#[cfg(test)] -mod test_helper; - +use command::edm::urc::EdmEvent; #[cfg(any(feature = "socket-udp", feature = "socket-tcp"))] pub use wifi::tls::TLS; + +pub type UbloxWifiBuffers = + atat::Buffers; + +pub type UbloxWifiIngress<'a, const INGRESS_BUF_SIZE: usize> = atat::Ingress< + 'a, + command::custom_digest::EdmDigester, + EdmEvent, + INGRESS_BUF_SIZE, + URC_CAPACITY, + URC_SUBSCRIBERS, +>; + +pub type UbloxWifiUrcChannel = atat::UrcChannel; diff --git a/ublox-short-range/src/test_helper.rs b/ublox-short-range/src/test_helper.rs deleted file mode 100644 index f370d06..0000000 --- a/ublox-short-range/src/test_helper.rs +++ /dev/null @@ -1,22 +0,0 @@ -//! This module is required in order to satisfy the requirements of defmt, while running tests. -//! Note that this will cause all log `defmt::` log statements to be thrown away. - -#[defmt::global_logger] -struct Logger; - -unsafe impl defmt::Logger for Logger { - fn acquire() {} - - unsafe fn flush() {} - - unsafe fn release() {} - - unsafe fn write(_bytes: &[u8]) {} -} - -defmt::timestamp!(""); - -#[export_name = "_defmt_panic"] -fn panic() -> ! { - panic!() -} diff --git a/ublox-short-range/src/wifi/ap.rs b/ublox-short-range/src/wifi/ap.rs index 54b0184..028267a 100644 --- a/ublox-short-range/src/wifi/ap.rs +++ b/ublox-short-range/src/wifi/ap.rs @@ -23,10 +23,11 @@ use embedded_hal::digital::OutputPin; use super::connection::{WiFiState, WifiConnection}; -impl - UbloxClient +impl<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, const TIMER_HZ: u32, const N: usize, const L: usize> + UbloxClient<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, TIMER_HZ, N, L> where - C: AtatClient, + 'buf: 'sub, + AtCl: AtatClient, CLK: fugit_timer::Timer, RST: OutputPin, { diff --git a/ublox-short-range/src/wifi/dns.rs b/ublox-short-range/src/wifi/dns.rs index abd83a7..6437811 100644 --- a/ublox-short-range/src/wifi/dns.rs +++ b/ublox-short-range/src/wifi/dns.rs @@ -7,10 +7,11 @@ use heapless::String; use crate::{command::ping::*, UbloxClient}; use ublox_sockets::Error; -impl Dns - for UbloxClient +impl<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, const TIMER_HZ: u32, const N: usize, const L: usize> Dns + for UbloxClient<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, TIMER_HZ, N, L> where - C: atat::blocking::AtatClient, + 'buf: 'sub, + AtCl: atat::blocking::AtatClient, CLK: fugit_timer::Timer, RST: OutputPin, { diff --git a/ublox-short-range/src/wifi/supplicant.rs b/ublox-short-range/src/wifi/supplicant.rs index 8946690..7da607b 100644 --- a/ublox-short-range/src/wifi/supplicant.rs +++ b/ublox-short-range/src/wifi/supplicant.rs @@ -1,3 +1,4 @@ +use atat::blocking::AtatClient; use heapless::Vec; use crate::{ @@ -50,15 +51,15 @@ use defmt::debug; /// supplicant.remove_connection(0) /// /// -pub struct Supplicant<'a, C, const N: usize> { - pub(crate) client: &'a mut C, +pub struct Supplicant<'a, AtCl, const N: usize> { + pub(crate) client: &'a mut AtCl, pub(crate) wifi_connection: &'a mut Option, pub(crate) active_on_startup: &'a mut Option, } -impl<'a, C, const N: usize> Supplicant<'a, C, N> +impl<'a, AtCl, const N: usize> Supplicant<'a, AtCl, N> where - C: atat::blocking::AtatClient, + AtCl: AtatClient, { fn send_at, const LEN: usize>( &mut self, diff --git a/ublox-short-range/src/wifi/tcp_stack.rs b/ublox-short-range/src/wifi/tcp_stack.rs index 3f88f11..fcf5151 100644 --- a/ublox-short-range/src/wifi/tcp_stack.rs +++ b/ublox-short-range/src/wifi/tcp_stack.rs @@ -14,10 +14,11 @@ use ublox_sockets::{Error, SocketHandle, TcpSocket, TcpState}; use super::EGRESS_CHUNK_SIZE; -impl TcpClientStack - for UbloxClient +impl<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, const TIMER_HZ: u32, const N: usize, const L: usize> + TcpClientStack for UbloxClient<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, TIMER_HZ, N, L> where - C: atat::blocking::AtatClient, + 'buf: 'sub, + AtCl: atat::blocking::AtatClient, CLK: fugit_timer::Timer, RST: OutputPin, { @@ -35,7 +36,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() { return Err(Error::SocketSetFull); } } @@ -88,7 +89,7 @@ where .sockets .as_mut() .unwrap() - .get::>(*socket) + .get::>(*socket) .map_err(Self::Error::from)?; tcp.set_state(TcpState::WaitingForConnect(remote)); @@ -106,7 +107,7 @@ where .sockets .as_mut() .unwrap() - .get::>(*socket) + .get::>(*socket) .map_err(Self::Error::from)?; tcp.set_state(TcpState::Created); return Err(nb::Error::Other(e)); @@ -122,7 +123,7 @@ where self.sockets .as_mut() .unwrap() - .get::>(*socket) + .get::>(*socket) .map_err(Self::Error::from)? .state(), TcpState::WaitingForConnect(_) @@ -137,7 +138,7 @@ where fn is_connected(&mut self, socket: &Self::TcpSocket) -> Result { self.connected_to_network().map_err(|_| Error::Illegal)?; if let Some(ref mut sockets) = self.sockets { - let tcp = sockets.get::>(*socket)?; + let tcp = sockets.get::>(*socket)?; Ok(tcp.is_connected()) } else { Err(Error::Illegal) @@ -154,7 +155,7 @@ where self.connected_to_network().map_err(|_| Error::Illegal)?; if let Some(ref mut sockets) = self.sockets { let tcp = sockets - .get::>(*socket) + .get::>(*socket) .map_err(nb::Error::Other)?; if !tcp.is_connected() { @@ -191,10 +192,10 @@ where self.spin().map_err(|_| nb::Error::Other(Error::Illegal))?; if let Some(ref mut sockets) = self.sockets { // Enable detecting closed socket from receive function - sockets.recycle(self.timer.now()); + sockets.recycle(); let mut tcp = sockets - .get::>(*socket) + .get::>(*socket) .map_err(Self::Error::from)?; Ok(tcp.recv_slice(buffer).map_err(Self::Error::from)?) @@ -208,7 +209,7 @@ where if let Some(ref mut sockets) = self.sockets { defmt::debug!("[TCP] Closing socket: {:?}", socket); // If the socket is not found it is already removed - if let Ok(ref tcp) = sockets.get::>(socket) { + if let Ok(ref tcp) = sockets.get::>(socket) { // If socket is not closed that means a connection excists which has to be closed if !matches!( tcp.state(), diff --git a/ublox-short-range/src/wifi/tls.rs b/ublox-short-range/src/wifi/tls.rs index 2d0b04d..fc14636 100644 --- a/ublox-short-range/src/wifi/tls.rs +++ b/ublox-short-range/src/wifi/tls.rs @@ -18,10 +18,11 @@ pub trait TLS { ) -> Result<(), Error>; } -impl TLS - for UbloxClient +impl<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, const TIMER_HZ: u32, const N: usize, const L: usize> TLS + for UbloxClient<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, TIMER_HZ, N, L> where - C: atat::blocking::AtatClient, + 'buf: 'sub, + AtCl: atat::blocking::AtatClient, CLK: fugit_timer::Timer, RST: OutputPin, { diff --git a/ublox-short-range/src/wifi/udp_stack.rs b/ublox-short-range/src/wifi/udp_stack.rs index beea659..3682ef7 100644 --- a/ublox-short-range/src/wifi/udp_stack.rs +++ b/ublox-short-range/src/wifi/udp_stack.rs @@ -16,10 +16,11 @@ use ublox_sockets::{Error, SocketHandle, UdpSocket, UdpState}; use super::EGRESS_CHUNK_SIZE; -impl UdpClientStack - for UbloxClient +impl<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, const TIMER_HZ: u32, const N: usize, const L: usize> + UdpClientStack for UbloxClient<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, TIMER_HZ, N, L> where - C: atat::blocking::AtatClient, + 'buf: 'sub, + AtCl: atat::blocking::AtatClient, CLK: fugit_timer::Timer, RST: OutputPin, { @@ -36,7 +37,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() { return Err(Error::SocketSetFull); } } @@ -77,7 +78,7 @@ where .sockets .as_mut() .unwrap() - .get::>(*socket)?; + .get::>(*socket)?; udp.bind(remote)?; // Then connect modem @@ -95,7 +96,7 @@ where .sockets .as_mut() .unwrap() - .get::>(*socket)?; + .get::>(*socket)?; udp.close(); return Err(e); } @@ -104,7 +105,7 @@ where .sockets .as_mut() .unwrap() - .get::>(*socket)? + .get::>(*socket)? .state() == UdpState::Closed { @@ -123,7 +124,7 @@ where } let udp = sockets - .get::>(*socket) + .get::>(*socket) .map_err(Self::Error::from)?; if !udp.is_open() { @@ -175,7 +176,7 @@ where if let Some(ref mut sockets) = self.sockets { let mut udp = sockets - .get::>(*connection_handle) + .get::>(*connection_handle) .map_err(|_| Self::Error::InvalidSocket)?; let bytes = udp.recv_slice(buffer).map_err(Self::Error::from)?; @@ -187,7 +188,7 @@ where // Handle reciving for udp normal sockets } else if let Some(ref mut sockets) = self.sockets { let mut udp = sockets - .get::>(*socket) + .get::>(*socket) .map_err(Self::Error::from)?; let bytes = udp.recv_slice(buffer).map_err(Self::Error::from)?; @@ -255,7 +256,7 @@ where } else if let Some(ref mut sockets) = self.sockets { defmt::debug!("[UDP] Closing socket: {:?}", socket); // If no sockets exists, nothing to close. - if let Ok(ref mut udp) = sockets.get::>(socket) { + if let Ok(ref mut udp) = sockets.get::>(socket) { defmt::trace!("[UDP] Closing socket state: {:?}", udp.state()); match udp.state() { UdpState::Closed => { @@ -293,10 +294,11 @@ where /// - The driver has to call send_to after reciving data, to release the socket bound by remote host, /// even if just sending no bytes. Else these sockets will be held open until closure of server socket. /// -impl UdpFullStack - for UbloxClient +impl<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, const TIMER_HZ: u32, const N: usize, const L: usize> + UdpFullStack for UbloxClient<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, TIMER_HZ, N, L> where - C: atat::blocking::AtatClient, + 'buf: 'sub, + AtCl: atat::blocking::AtatClient, CLK: fugit_timer::Timer, RST: OutputPin, { @@ -352,7 +354,7 @@ where } let udp = sockets - .get::>(connection_socket) + .get::>(connection_socket) .map_err(Self::Error::from)?; if !udp.is_open() {