36
36
37
37
#define RETRY_COUNT_DEFAULT 3
38
38
39
+
39
40
const int STM_STOPPED = -99 ;
40
41
const int ACTIVE_PDP_CONTEXT = 0x01 ;
41
42
const int ATTACHED_TO_NETWORK = 0x02 ;
@@ -68,6 +69,12 @@ CellularStateMachine::CellularStateMachine(CellularDevice &device, events::Event
68
69
_retry_timeout_array[8 ] = 600 ;
69
70
_retry_timeout_array[9 ] = TIMEOUT_NETWORK_MAX;
70
71
_retry_array_length = CELLULAR_RETRY_ARRAY_SIZE;
72
+
73
+ _state_timeout_power_on = TIMEOUT_POWER_ON;
74
+ _state_timeout_sim_pin = TIMEOUT_SIM_PIN;
75
+ _state_timeout_registration = TIMEOUT_REGISTRATION;
76
+ _state_timeout_network = TIMEOUT_NETWORK;
77
+ _state_timeout_connect = TIMEOUT_CONNECT;
71
78
}
72
79
73
80
CellularStateMachine::~CellularStateMachine ()
@@ -285,21 +292,23 @@ void CellularStateMachine::enter_to_state(CellularState state)
285
292
286
293
void CellularStateMachine::retry_state_or_fail ()
287
294
{
288
- if (++ _retry_count < CELLULAR_RETRY_ARRAY_SIZE ) {
289
- tr_debug (" %s: retry %d/%d" , get_state_string (_state), _retry_count, CELLULAR_RETRY_ARRAY_SIZE );
295
+ if (_retry_count < _retry_array_length ) {
296
+ tr_debug (" %s: retry %d/%d" , get_state_string (_state), _retry_count, _retry_array_length );
290
297
_event_timeout = _retry_timeout_array[_retry_count];
291
298
_is_retry = true ;
292
299
_cb_data.error = NSAPI_ERROR_OK;
300
+ _retry_count++;
293
301
} else {
302
+ _cb_data.final_try = true ;
294
303
report_failure (get_state_string (_state));
295
304
return ;
296
305
}
297
306
}
298
307
299
308
void CellularStateMachine::state_init ()
300
309
{
301
- _cellularDevice.set_timeout (TIMEOUT_POWER_ON );
302
- tr_info (" Start connecting (timeout %d s )" , TIMEOUT_POWER_ON / 1000 );
310
+ _cellularDevice.set_timeout (_state_timeout_power_on );
311
+ tr_info (" Start connecting (timeout %d ms )" , _state_timeout_power_on );
303
312
_cb_data.error = _cellularDevice.is_ready ();
304
313
_status = _cb_data.error ? 0 : DEVICE_READY;
305
314
if (_cb_data.error != NSAPI_ERROR_OK) {
@@ -315,8 +324,8 @@ void CellularStateMachine::state_init()
315
324
316
325
void CellularStateMachine::state_power_on ()
317
326
{
318
- _cellularDevice.set_timeout (TIMEOUT_POWER_ON );
319
- tr_info (" Modem power ON (timeout %d s )" , TIMEOUT_POWER_ON / 1000 );
327
+ _cellularDevice.set_timeout (_state_timeout_power_on );
328
+ tr_info (" Modem power ON (timeout %d ms )" , _state_timeout_power_on );
320
329
if (power_on ()) {
321
330
enter_to_state (STATE_DEVICE_READY);
322
331
} else {
@@ -353,7 +362,7 @@ bool CellularStateMachine::device_ready()
353
362
354
363
void CellularStateMachine::state_device_ready ()
355
364
{
356
- _cellularDevice.set_timeout (TIMEOUT_POWER_ON );
365
+ _cellularDevice.set_timeout (_state_timeout_power_on );
357
366
if (!(_status & DEVICE_READY)) {
358
367
tr_debug (" Device was not ready, calling soft_power_on()" );
359
368
_cb_data.error = _cellularDevice.soft_power_on ();
@@ -377,8 +386,8 @@ void CellularStateMachine::state_device_ready()
377
386
378
387
void CellularStateMachine::state_sim_pin ()
379
388
{
380
- _cellularDevice.set_timeout (TIMEOUT_SIM_PIN );
381
- tr_info (" Setup SIM (timeout %d s )" , TIMEOUT_SIM_PIN / 1000 );
389
+ _cellularDevice.set_timeout (_state_timeout_sim_pin );
390
+ tr_info (" Setup SIM (timeout %d ms )" , _state_timeout_sim_pin );
382
391
if (open_sim ()) {
383
392
bool success = false ;
384
393
for (int type = 0 ; type < CellularNetwork::C_MAX; type++) {
@@ -419,8 +428,7 @@ void CellularStateMachine::state_sim_pin()
419
428
420
429
void CellularStateMachine::state_registering ()
421
430
{
422
- _cellularDevice.set_timeout (TIMEOUT_NETWORK);
423
- tr_info (" Network registration (timeout %d s)" , TIMEOUT_REGISTRATION / 1000 );
431
+ _cellularDevice.set_timeout (_state_timeout_network);
424
432
if (is_registered ()) {
425
433
if (_cb_data.status_data != CellularNetwork::RegisteredHomeNetwork &&
426
434
_cb_data.status_data != CellularNetwork::RegisteredRoaming && _status) {
@@ -432,7 +440,8 @@ void CellularStateMachine::state_registering()
432
440
// we are already registered, go to attach
433
441
enter_to_state (STATE_ATTACHING_NETWORK);
434
442
} else {
435
- _cellularDevice.set_timeout (TIMEOUT_REGISTRATION);
443
+ tr_info (" Network registration (timeout %d ms)" , _state_timeout_registration);
444
+ _cellularDevice.set_timeout (_state_timeout_registration);
436
445
if (!_command_success && !_plmn) { // don't call set_registration twice for manual registration
437
446
_cb_data.error = _network->set_registration (_plmn);
438
447
_command_success = (_cb_data.error == NSAPI_ERROR_OK);
@@ -443,8 +452,8 @@ void CellularStateMachine::state_registering()
443
452
444
453
void CellularStateMachine::state_attaching ()
445
454
{
446
- _cellularDevice.set_timeout (TIMEOUT_CONNECT );
447
- tr_info (" Attaching network (timeout %d s )" , TIMEOUT_CONNECT / 1000 );
455
+ _cellularDevice.set_timeout (_state_timeout_connect );
456
+ tr_info (" Attaching network (timeout %d ms )" , _state_timeout_connect );
448
457
if (_status != ATTACHED_TO_NETWORK) {
449
458
_cb_data.error = _network->set_attach ();
450
459
}
@@ -683,11 +692,10 @@ void CellularStateMachine::device_ready_cb()
683
692
void CellularStateMachine::set_retry_timeout_array (const uint16_t timeout[], int array_len)
684
693
{
685
694
if (!timeout || array_len <= 0 ) {
686
- tr_warn ( " set_retry_timeout_array, timeout array null or invalid length " ) ;
695
+ _retry_array_length = 0 ;
687
696
return ;
688
697
}
689
698
_retry_array_length = array_len > CELLULAR_RETRY_ARRAY_SIZE ? CELLULAR_RETRY_ARRAY_SIZE : array_len;
690
-
691
699
for (int i = 0 ; i < _retry_array_length; i++) {
692
700
_retry_timeout_array[i] = timeout[i];
693
701
}
@@ -701,5 +709,14 @@ void CellularStateMachine::get_retry_timeout_array(uint16_t *timeout, int &array
701
709
array_len = _retry_array_length;
702
710
}
703
711
712
+ void CellularStateMachine::set_timeout (int timeout)
713
+ {
714
+ _state_timeout_power_on = timeout;
715
+ _state_timeout_sim_pin = timeout;
716
+ _state_timeout_registration = timeout;
717
+ _state_timeout_network = timeout;
718
+ _state_timeout_connect = timeout;
719
+ }
720
+
704
721
} // namespace
705
722
0 commit comments