From 21510554e078bb7759e7bb82732ee502453548d5 Mon Sep 17 00:00:00 2001 From: diekleinekuh Date: Wed, 4 Oct 2023 09:38:12 +0200 Subject: [PATCH] avoid stack overflow from power_on calling hard_reset --- ublox-cellular/src/client.rs | 2 ++ ublox-cellular/src/power.rs | 54 ++++++++++++++++-------------------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/ublox-cellular/src/client.rs b/ublox-cellular/src/client.rs index d49e818..a62a8f9 100644 --- a/ublox-cellular/src/client.rs +++ b/ublox-cellular/src/client.rs @@ -448,6 +448,7 @@ where self.network.at_tx.reset()?; if self.power_on().is_err() { self.hard_reset()?; + self.power_on()?; } self.power_state = PowerState::On; @@ -612,6 +613,7 @@ where // as well as consecutive AT timeouts and do a hard reset. Err(crate::network::Error::Generic(GenericError::Timeout)) => { self.hard_reset()?; + self.power_on()?; Err(Error::Generic(GenericError::Timeout)) } result => result.map_err(Error::from), diff --git a/ublox-cellular/src/power.rs b/ublox-cellular/src/power.rs index 7f19be0..72e054f 100644 --- a/ublox-cellular/src/power.rs +++ b/ublox-cellular/src/power.rs @@ -67,6 +67,7 @@ where if self.soft_reset(true).is_err() { self.hard_reset()?; + self.power_on()?; } Ok(()) @@ -133,8 +134,6 @@ where self.power_state = PowerState::Off; - self.power_on()?; - Ok(()) } @@ -147,38 +146,33 @@ where if self.power_state()? != PowerState::On { trace!("Powering modem on."); - match self.config.pwr_pin { + if let Some(ref mut pwr) = self.config.pwr_pin { // Apply Low pulse on PWR_ON for 50 microseconds to power on - Some(ref mut pwr) => { - pwr.set_low().ok(); - self.network - .status - .timer - .start(50.micros()) - .map_err(from_clock)?; - nb::block!(self.network.status.timer.wait()).map_err(from_clock)?; + pwr.set_low().ok(); + self.network + .status + .timer + .start(50.micros()) + .map_err(from_clock)?; + nb::block!(self.network.status.timer.wait()).map_err(from_clock)?; - pwr.set_high().ok(); - self.network - .status - .timer - .start(1.secs()) - .map_err(from_clock)?; - nb::block!(self.network.status.timer.wait()).map_err(from_clock)?; + pwr.set_high().ok(); + self.network + .status + .timer + .start(1.secs()) + .map_err(from_clock)?; + nb::block!(self.network.status.timer.wait()).map_err(from_clock)?; - if let Err(e) = self.wait_power_state(PowerState::On, 25.secs()) { - error!("Failed to power on modem"); - return Err(e); - } else { - trace!("Modem powered on"); - } - } - _ => { - // Software restart - if self.soft_reset(false).is_err() { - self.hard_reset()?; - } + if let Err(e) = self.wait_power_state(PowerState::On, 25.secs()) { + error!("Failed to power on modem"); + return Err(e); + } else { + trace!("Modem powered on"); } + } else { + error!("Cannot power on modem, pin disabled"); + return Err(Error::Generic(GenericError::Timeout)); } } else { debug!("module is already on");