Skip to content

Commit

Permalink
Only handle URCs in one place (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasKoch authored Sep 26, 2023
1 parent d72694a commit 9aa54c8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 97 deletions.
74 changes: 65 additions & 9 deletions ublox-cellular/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand All @@ -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(())
}
Expand All @@ -578,8 +638,4 @@ where
result => result.map_err(Error::from),
}
}

pub fn handle_urc<F: FnOnce(Urc) -> bool>(&mut self, f: F) -> Result<(), Error> {
self.network.at_tx.handle_urc(f).map_err(Error::Network)
}
}
90 changes: 6 additions & 84 deletions ublox-cellular/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<'sub, AtCl: AtatClient> AtTx<'sub, AtCl> {
})
}

pub fn handle_urc<F: FnOnce(Urc) -> bool>(&mut self, f: F) -> Result<(), Error> {
pub fn handle_urc<F: FnOnce(Urc)>(&mut self, f: F) -> Result<(), Error> {
if let Some(urc) = self.urc_subscription.try_next_message_pure() {
f(urc);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<RegistrationParams> = 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<A, const LEN: usize>(
&mut self,
req: &A,
Expand All @@ -514,11 +436,11 @@ where
where
A: atat::AtatCmd<LEN>,
{
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)
}
Expand Down
4 changes: 0 additions & 4 deletions ublox-cellular/src/services/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,6 @@ where
Ok(self.network.send_internal(cmd, true)?)
}

pub fn handle_urc<F: FnOnce(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;
Expand Down

0 comments on commit 9aa54c8

Please sign in to comment.