From 9aa54c8ceacd3b1c74305019cb19a3734dded561 Mon Sep 17 00:00:00 2001 From: Mathias Koch Date: Tue, 26 Sep 2023 12:22:11 +0200 Subject: [PATCH] Only handle URCs in one place (#84) --- ublox-cellular/src/client.rs | 74 +++++++++++++++++--- ublox-cellular/src/network.rs | 90 ++----------------------- ublox-cellular/src/services/data/mod.rs | 4 -- 3 files changed, 71 insertions(+), 97 deletions(-) diff --git a/ublox-cellular/src/client.rs b/ublox-cellular/src/client.rs index c1c6509..1f7dacb 100644 --- a/ublox-cellular/src/client.rs +++ b/ublox-cellular/src/client.rs @@ -37,7 +37,7 @@ use crate::{ network::{AtTx, Network}, power::PowerState, registration::ConnectionState, - services::data::ContextState, + services::data::{ContextState, PROFILE_ID}, UbloxCellularBuffers, UbloxCellularIngress, UbloxCellularUrcChannel, }; use ip_transport_layer::{types::HexMode, SetHexMode}; @@ -525,8 +525,10 @@ where } fn handle_urc_internal(&mut self) -> Result<(), Error> { + let mut ctx_state = self.network.context_state; if let Some(ref mut sockets) = self.sockets.as_deref_mut() { - self.network + let res = self + .network .at_tx .handle_urc(|urc| { match urc { @@ -551,11 +553,69 @@ where sock.set_available_data(length); } } - _ => return false, + Urc::NetworkDetach => { + warn!("Network Detach URC!"); + } + Urc::MobileStationDetach => { + warn!("ME Detach URC!"); + } + Urc::NetworkDeactivate => { + warn!("Network Deactivate URC!"); + } + Urc::MobileStationDeactivate => { + warn!("ME Deactivate URC!"); + } + Urc::NetworkPDNDeactivate => { + warn!("Network PDN Deactivate URC!"); + } + Urc::MobileStationPDNDeactivate => { + warn!("ME PDN Deactivate URC!"); + } + Urc::ExtendedPSNetworkRegistration( + psn::urc::ExtendedPSNetworkRegistration { state }, + ) => { + info!("[URC] ExtendedPSNetworkRegistration {:?}", state); + } + // FIXME: Currently `atat` is unable to distinguish `xREG` family of + // commands from URC's + + // Urc::GPRSNetworkRegistration(reg_params) => { + // new_reg_params.replace(reg_params.into()); + // } + // Urc::EPSNetworkRegistration(reg_params) => { + // new_reg_params.replace(reg_params.into()); + // } + // Urc::NetworkRegistration(reg_params) => { + // new_reg_params.replace(reg_params.into()); + // } + Urc::DataConnectionActivated(psn::urc::DataConnectionActivated { + result, + ip_addr: _, + }) => { + info!("[URC] DataConnectionActivated {}", result); + if result == 0 { + ctx_state = ContextState::Active; + } else { + ctx_state = ContextState::Setup; + } + } + Urc::DataConnectionDeactivated(psn::urc::DataConnectionDeactivated { + profile_id, + }) => { + info!("[URC] DataConnectionDeactivated {:?}", profile_id); + if profile_id == PROFILE_ID { + ctx_state = ContextState::Activating; + } + } + Urc::MessageWaitingIndication(_) => { + info!("[URC] MessageWaitingIndication"); + } + _ => {} } - true }) - .map_err(Error::Network) + .map_err(Error::Network); + self.network.context_state = ctx_state; + res } else { Ok(()) } @@ -578,8 +638,4 @@ where result => result.map_err(Error::from), } } - - pub fn handle_urc bool>(&mut self, f: F) -> Result<(), Error> { - self.network.at_tx.handle_urc(f).map_err(Error::Network) - } } diff --git a/ublox-cellular/src/network.rs b/ublox-cellular/src/network.rs index 044dd02..07bf0bc 100644 --- a/ublox-cellular/src/network.rs +++ b/ublox-cellular/src/network.rs @@ -118,7 +118,7 @@ impl<'sub, AtCl: AtatClient> AtTx<'sub, AtCl> { }) } - pub fn handle_urc bool>(&mut self, f: F) -> Result<(), Error> { + pub fn handle_urc(&mut self, f: F) -> Result<(), Error> { if let Some(urc) = self.urc_subscription.try_next_message_pure() { f(urc); } @@ -161,7 +161,6 @@ where return Err(Error::Generic(GenericError::Timeout)); } - self.handle_urc().ok(); // Ignore errors self.check_registration_state(); self.intervene_registration()?; self.check_running_imsi().ok(); // Ignore errors @@ -429,83 +428,6 @@ where Ok(()) } - pub(crate) fn handle_urc(&mut self) -> Result<(), Error> { - // TODO: How to do this cleaner? - let mut ctx_state = self.context_state; - // let mut new_reg_params: Option = None; - - self.at_tx.handle_urc(|urc| { - match urc { - Urc::NetworkDetach => { - warn!("Network Detach URC!"); - } - Urc::MobileStationDetach => { - warn!("ME Detach URC!"); - } - Urc::NetworkDeactivate => { - warn!("Network Deactivate URC!"); - } - Urc::MobileStationDeactivate => { - warn!("ME Deactivate URC!"); - } - Urc::NetworkPDNDeactivate => { - warn!("Network PDN Deactivate URC!"); - } - Urc::MobileStationPDNDeactivate => { - warn!("ME PDN Deactivate URC!"); - } - Urc::ExtendedPSNetworkRegistration(psn::urc::ExtendedPSNetworkRegistration { - state, - }) => { - info!("[URC] ExtendedPSNetworkRegistration {:?}", state); - } - // FIXME: Currently `atat` is unable to distinguish `xREG` family of - // commands from URC's - - // Urc::GPRSNetworkRegistration(reg_params) => { - // new_reg_params.replace(reg_params.into()); - // } - // Urc::EPSNetworkRegistration(reg_params) => { - // new_reg_params.replace(reg_params.into()); - // } - // Urc::NetworkRegistration(reg_params) => { - // new_reg_params.replace(reg_params.into()); - // } - Urc::DataConnectionActivated(psn::urc::DataConnectionActivated { - result, - ip_addr: _, - }) => { - info!("[URC] DataConnectionActivated {}", result); - if result == 0 { - ctx_state = ContextState::Active; - } else { - ctx_state = ContextState::Setup; - } - } - Urc::DataConnectionDeactivated(psn::urc::DataConnectionDeactivated { - profile_id, - }) => { - info!("[URC] DataConnectionDeactivated {:?}", profile_id); - if profile_id == PROFILE_ID { - ctx_state = ContextState::Activating; - } - } - Urc::MessageWaitingIndication(_) => { - info!("[URC] MessageWaitingIndication"); - } - _ => return false, - }; - true - })?; - - // if let Some(reg_params) = new_reg_params { - // self.status.compare_and_set(reg_params) - // } - - self.context_state = ctx_state; - Ok(()) - } - pub(crate) fn send_internal( &mut self, req: &A, @@ -514,11 +436,11 @@ where where A: atat::AtatCmd, { - if check_urc { - if let Err(e) = self.handle_urc() { - error!("Failed handle URC {:?}", &e); - } - } + // if check_urc { + // if let Err(e) = self.handle_urc() { + // error!("Failed handle URC {:?}", &e); + // } + // } self.at_tx.send(req) } diff --git a/ublox-cellular/src/services/data/mod.rs b/ublox-cellular/src/services/data/mod.rs index efc1c47..b95c73f 100644 --- a/ublox-cellular/src/services/data/mod.rs +++ b/ublox-cellular/src/services/data/mod.rs @@ -504,10 +504,6 @@ where Ok(self.network.send_internal(cmd, true)?) } - pub fn handle_urc bool>(&mut self, f: F) -> Result<(), Error> { - self.network.at_tx.handle_urc(f).map_err(Error::Network) - } - fn socket_ingress_all(&mut self) -> Result<(), Error> { if let Some(ref mut sockets) = self.sockets { let network = &mut self.network;