2828#define TIMEOUT_POWER_ON (1 *1000 )
2929#define TIMEOUT_SIM_PIN (1 *1000 )
3030#define TIMEOUT_NETWORK (10 *1000 )
31+ /* * CellularStateMachine does connecting up to packet service attach, and
32+ * after that it's up to CellularContext::connect() to connect to PDN.
33+ * If CellularContext or an application does not set timeout (via `CellularDevice::set_timeout`)
34+ * then TIMEOUT_CONNECT is used also for connecting to PDN and also for socket operations.
35+ */
3136#define TIMEOUT_CONNECT (60 *1000 )
3237#define TIMEOUT_REGISTRATION (180 *1000 )
3338
3641
3742#define RETRY_COUNT_DEFAULT 3
3843
44+
3945const int STM_STOPPED = -99 ;
4046const int ACTIVE_PDP_CONTEXT = 0x01 ;
4147const int ATTACHED_TO_NETWORK = 0x02 ;
@@ -68,6 +74,12 @@ CellularStateMachine::CellularStateMachine(CellularDevice &device, events::Event
6874 _retry_timeout_array[8 ] = 600 ;
6975 _retry_timeout_array[9 ] = TIMEOUT_NETWORK_MAX;
7076 _retry_array_length = CELLULAR_RETRY_ARRAY_SIZE;
77+
78+ _state_timeout_power_on = TIMEOUT_POWER_ON;
79+ _state_timeout_sim_pin = TIMEOUT_SIM_PIN;
80+ _state_timeout_registration = TIMEOUT_REGISTRATION;
81+ _state_timeout_network = TIMEOUT_NETWORK;
82+ _state_timeout_connect = TIMEOUT_CONNECT;
7183}
7284
7385CellularStateMachine::~CellularStateMachine ()
@@ -273,8 +285,8 @@ void CellularStateMachine::enter_to_state(CellularState state)
273285
274286void CellularStateMachine::retry_state_or_fail ()
275287{
276- if (++ _retry_count < CELLULAR_RETRY_ARRAY_SIZE ) {
277- tr_debug (" %s: retry %d/%d" , get_state_string (_state), _retry_count, CELLULAR_RETRY_ARRAY_SIZE );
288+ if (_retry_count < _retry_array_length ) {
289+ tr_debug (" %s: retry %d/%d" , get_state_string (_state), _retry_count, _retry_array_length );
278290 // send info to application/driver about error logic so it can implement proper error logic
279291 _cb_data.status_data = _current_event;
280292 _cb_data.data = &_retry_count;
@@ -284,15 +296,17 @@ void CellularStateMachine::retry_state_or_fail()
284296 _event_timeout = _retry_timeout_array[_retry_count];
285297 _is_retry = true ;
286298 _cb_data.error = NSAPI_ERROR_OK;
299+ _retry_count++;
287300 } else {
301+ _cb_data.final_try = true ;
288302 report_failure (get_state_string (_state));
289303 }
290304}
291305
292306void CellularStateMachine::state_init ()
293307{
294- _cellularDevice.set_timeout (TIMEOUT_POWER_ON );
295- tr_info (" Start connecting (timeout %d s )" , TIMEOUT_POWER_ON / 1000 );
308+ _cellularDevice.set_timeout (_state_timeout_power_on );
309+ tr_info (" Start connecting (timeout %d ms )" , _state_timeout_power_on );
296310 _cb_data.error = _cellularDevice.is_ready ();
297311 _status = _cb_data.error ? 0 : DEVICE_READY;
298312 if (_cb_data.error != NSAPI_ERROR_OK) {
@@ -308,8 +322,8 @@ void CellularStateMachine::state_init()
308322
309323void CellularStateMachine::state_power_on ()
310324{
311- _cellularDevice.set_timeout (TIMEOUT_POWER_ON );
312- tr_info (" Modem power ON (timeout %d s )" , TIMEOUT_POWER_ON / 1000 );
325+ _cellularDevice.set_timeout (_state_timeout_power_on );
326+ tr_info (" Modem power ON (timeout %d ms )" , _state_timeout_power_on );
313327 if (power_on ()) {
314328 enter_to_state (STATE_DEVICE_READY);
315329 } else {
@@ -340,7 +354,7 @@ bool CellularStateMachine::device_ready()
340354
341355void CellularStateMachine::state_device_ready ()
342356{
343- _cellularDevice.set_timeout (TIMEOUT_POWER_ON );
357+ _cellularDevice.set_timeout (_state_timeout_power_on );
344358 if (!(_status & DEVICE_READY)) {
345359 tr_debug (" Device was not ready, calling soft_power_on()" );
346360 _cb_data.error = _cellularDevice.soft_power_on ();
@@ -364,8 +378,8 @@ void CellularStateMachine::state_device_ready()
364378
365379void CellularStateMachine::state_sim_pin ()
366380{
367- _cellularDevice.set_timeout (TIMEOUT_SIM_PIN );
368- tr_info (" Setup SIM (timeout %d s )" , TIMEOUT_SIM_PIN / 1000 );
381+ _cellularDevice.set_timeout (_state_timeout_sim_pin );
382+ tr_info (" Setup SIM (timeout %d ms )" , _state_timeout_sim_pin );
369383 if (open_sim ()) {
370384 bool success = false ;
371385 for (int type = 0 ; type < CellularNetwork::C_MAX; type++) {
@@ -419,8 +433,7 @@ void CellularStateMachine::state_signal_quality()
419433
420434void CellularStateMachine::state_registering ()
421435{
422- _cellularDevice.set_timeout (TIMEOUT_NETWORK);
423- tr_info (" Network registration (timeout %d s)" , TIMEOUT_REGISTRATION / 1000 );
436+ _cellularDevice.set_timeout (_state_timeout_network);
424437 if (is_registered ()) {
425438 if (_cb_data.status_data != CellularNetwork::RegisteredHomeNetwork &&
426439 _cb_data.status_data != CellularNetwork::RegisteredRoaming && _status) {
@@ -432,7 +445,8 @@ void CellularStateMachine::state_registering()
432445 // we are already registered, go to attach
433446 enter_to_state (STATE_ATTACHING_NETWORK);
434447 } else {
435- _cellularDevice.set_timeout (TIMEOUT_REGISTRATION);
448+ tr_info (" Network registration (timeout %d ms)" , _state_timeout_registration);
449+ _cellularDevice.set_timeout (_state_timeout_registration);
436450 if (!_command_success && !_plmn) { // don't call set_registration twice for manual registration
437451 _cb_data.error = _network.set_registration (_plmn);
438452 _command_success = (_cb_data.error == NSAPI_ERROR_OK);
@@ -443,9 +457,9 @@ void CellularStateMachine::state_registering()
443457
444458void CellularStateMachine::state_attaching ()
445459{
446- _cellularDevice.set_timeout (TIMEOUT_CONNECT);
447- tr_info (" Attaching network (timeout %d s)" , TIMEOUT_CONNECT / 1000 );
448460 if (_status != ATTACHED_TO_NETWORK) {
461+ _cellularDevice.set_timeout (_state_timeout_connect);
462+ tr_info (" Attaching network (timeout %d ms)" , _state_timeout_connect);
449463 _cb_data.error = _network.set_attach ();
450464 }
451465 if (_cb_data.error == NSAPI_ERROR_OK) {
@@ -695,11 +709,10 @@ void CellularStateMachine::device_ready_cb()
695709void CellularStateMachine::set_retry_timeout_array (const uint16_t timeout[], int array_len)
696710{
697711 if (!timeout || array_len <= 0 ) {
698- tr_warn ( " set_retry_timeout_array, timeout array null or invalid length " ) ;
712+ _retry_array_length = 0 ;
699713 return ;
700714 }
701715 _retry_array_length = array_len > CELLULAR_RETRY_ARRAY_SIZE ? CELLULAR_RETRY_ARRAY_SIZE : array_len;
702-
703716 for (int i = 0 ; i < _retry_array_length; i++) {
704717 _retry_timeout_array[i] = timeout[i];
705718 }
@@ -713,5 +726,14 @@ void CellularStateMachine::get_retry_timeout_array(uint16_t *timeout, int &array
713726 array_len = _retry_array_length;
714727}
715728
729+ void CellularStateMachine::set_timeout (int timeout)
730+ {
731+ _state_timeout_power_on = timeout;
732+ _state_timeout_sim_pin = timeout;
733+ _state_timeout_registration = timeout;
734+ _state_timeout_network = timeout;
735+ _state_timeout_connect = timeout;
736+ }
737+
716738} // namespace
717739
0 commit comments