28
28
#define TIMEOUT_POWER_ON (1 *1000 )
29
29
#define TIMEOUT_SIM_PIN (1 *1000 )
30
30
#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
+ */
31
36
#define TIMEOUT_CONNECT (60 *1000 )
32
37
#define TIMEOUT_REGISTRATION (180 *1000 )
33
38
36
41
37
42
#define RETRY_COUNT_DEFAULT 3
38
43
44
+
39
45
const int STM_STOPPED = -99 ;
40
46
const int ACTIVE_PDP_CONTEXT = 0x01 ;
41
47
const int ATTACHED_TO_NETWORK = 0x02 ;
@@ -68,6 +74,12 @@ CellularStateMachine::CellularStateMachine(CellularDevice &device, events::Event
68
74
_retry_timeout_array[8 ] = 600 ;
69
75
_retry_timeout_array[9 ] = TIMEOUT_NETWORK_MAX;
70
76
_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;
71
83
}
72
84
73
85
CellularStateMachine::~CellularStateMachine ()
@@ -285,21 +297,23 @@ void CellularStateMachine::enter_to_state(CellularState state)
285
297
286
298
void CellularStateMachine::retry_state_or_fail ()
287
299
{
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 );
300
+ if (_retry_count < _retry_array_length ) {
301
+ tr_debug (" %s: retry %d/%d" , get_state_string (_state), _retry_count, _retry_array_length );
290
302
_event_timeout = _retry_timeout_array[_retry_count];
291
303
_is_retry = true ;
292
304
_cb_data.error = NSAPI_ERROR_OK;
305
+ _retry_count++;
293
306
} else {
307
+ _cb_data.final_try = true ;
294
308
report_failure (get_state_string (_state));
295
309
return ;
296
310
}
297
311
}
298
312
299
313
void CellularStateMachine::state_init ()
300
314
{
301
- _cellularDevice.set_timeout (TIMEOUT_POWER_ON );
302
- tr_info (" Start connecting (timeout %d s )" , TIMEOUT_POWER_ON / 1000 );
315
+ _cellularDevice.set_timeout (_state_timeout_power_on );
316
+ tr_info (" Start connecting (timeout %d ms )" , _state_timeout_power_on );
303
317
_cb_data.error = _cellularDevice.is_ready ();
304
318
_status = _cb_data.error ? 0 : DEVICE_READY;
305
319
if (_cb_data.error != NSAPI_ERROR_OK) {
@@ -315,8 +329,8 @@ void CellularStateMachine::state_init()
315
329
316
330
void CellularStateMachine::state_power_on ()
317
331
{
318
- _cellularDevice.set_timeout (TIMEOUT_POWER_ON );
319
- tr_info (" Modem power ON (timeout %d s )" , TIMEOUT_POWER_ON / 1000 );
332
+ _cellularDevice.set_timeout (_state_timeout_power_on );
333
+ tr_info (" Modem power ON (timeout %d ms )" , _state_timeout_power_on );
320
334
if (power_on ()) {
321
335
enter_to_state (STATE_DEVICE_READY);
322
336
} else {
@@ -353,7 +367,7 @@ bool CellularStateMachine::device_ready()
353
367
354
368
void CellularStateMachine::state_device_ready ()
355
369
{
356
- _cellularDevice.set_timeout (TIMEOUT_POWER_ON );
370
+ _cellularDevice.set_timeout (_state_timeout_power_on );
357
371
if (!(_status & DEVICE_READY)) {
358
372
tr_debug (" Device was not ready, calling soft_power_on()" );
359
373
_cb_data.error = _cellularDevice.soft_power_on ();
@@ -377,8 +391,8 @@ void CellularStateMachine::state_device_ready()
377
391
378
392
void CellularStateMachine::state_sim_pin ()
379
393
{
380
- _cellularDevice.set_timeout (TIMEOUT_SIM_PIN );
381
- tr_info (" Setup SIM (timeout %d s )" , TIMEOUT_SIM_PIN / 1000 );
394
+ _cellularDevice.set_timeout (_state_timeout_sim_pin );
395
+ tr_info (" Setup SIM (timeout %d ms )" , _state_timeout_sim_pin );
382
396
if (open_sim ()) {
383
397
bool success = false ;
384
398
for (int type = 0 ; type < CellularNetwork::C_MAX; type++) {
@@ -419,8 +433,7 @@ void CellularStateMachine::state_sim_pin()
419
433
420
434
void CellularStateMachine::state_registering ()
421
435
{
422
- _cellularDevice.set_timeout (TIMEOUT_NETWORK);
423
- tr_info (" Network registration (timeout %d s)" , TIMEOUT_REGISTRATION / 1000 );
436
+ _cellularDevice.set_timeout (_state_timeout_network);
424
437
if (is_registered ()) {
425
438
if (_cb_data.status_data != CellularNetwork::RegisteredHomeNetwork &&
426
439
_cb_data.status_data != CellularNetwork::RegisteredRoaming && _status) {
@@ -432,7 +445,8 @@ void CellularStateMachine::state_registering()
432
445
// we are already registered, go to attach
433
446
enter_to_state (STATE_ATTACHING_NETWORK);
434
447
} 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);
436
450
if (!_command_success && !_plmn) { // don't call set_registration twice for manual registration
437
451
_cb_data.error = _network->set_registration (_plmn);
438
452
_command_success = (_cb_data.error == NSAPI_ERROR_OK);
@@ -443,9 +457,9 @@ void CellularStateMachine::state_registering()
443
457
444
458
void CellularStateMachine::state_attaching ()
445
459
{
446
- _cellularDevice.set_timeout (TIMEOUT_CONNECT);
447
- tr_info (" Attaching network (timeout %d s)" , TIMEOUT_CONNECT / 1000 );
448
460
if (_status != ATTACHED_TO_NETWORK) {
461
+ _cellularDevice.set_timeout (_state_timeout_connect);
462
+ tr_info (" Attaching network (timeout %d ms)" , _state_timeout_connect);
449
463
_cb_data.error = _network->set_attach ();
450
464
}
451
465
if (_cb_data.error == NSAPI_ERROR_OK) {
@@ -683,11 +697,10 @@ void CellularStateMachine::device_ready_cb()
683
697
void CellularStateMachine::set_retry_timeout_array (const uint16_t timeout[], int array_len)
684
698
{
685
699
if (!timeout || array_len <= 0 ) {
686
- tr_warn ( " set_retry_timeout_array, timeout array null or invalid length " ) ;
700
+ _retry_array_length = 0 ;
687
701
return ;
688
702
}
689
703
_retry_array_length = array_len > CELLULAR_RETRY_ARRAY_SIZE ? CELLULAR_RETRY_ARRAY_SIZE : array_len;
690
-
691
704
for (int i = 0 ; i < _retry_array_length; i++) {
692
705
_retry_timeout_array[i] = timeout[i];
693
706
}
@@ -701,5 +714,14 @@ void CellularStateMachine::get_retry_timeout_array(uint16_t *timeout, int &array
701
714
array_len = _retry_array_length;
702
715
}
703
716
717
+ void CellularStateMachine::set_timeout (int timeout)
718
+ {
719
+ _state_timeout_power_on = timeout;
720
+ _state_timeout_sim_pin = timeout;
721
+ _state_timeout_registration = timeout;
722
+ _state_timeout_network = timeout;
723
+ _state_timeout_connect = timeout;
724
+ }
725
+
704
726
} // namespace
705
727
0 commit comments