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 ()
@@ -273,8 +285,8 @@ void CellularStateMachine::enter_to_state(CellularState state)
273
285
274
286
void CellularStateMachine::retry_state_or_fail ()
275
287
{
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 );
278
290
// send info to application/driver about error logic so it can implement proper error logic
279
291
_cb_data.status_data = _current_event;
280
292
_cb_data.data = &_retry_count;
@@ -284,15 +296,17 @@ void CellularStateMachine::retry_state_or_fail()
284
296
_event_timeout = _retry_timeout_array[_retry_count];
285
297
_is_retry = true ;
286
298
_cb_data.error = NSAPI_ERROR_OK;
299
+ _retry_count++;
287
300
} else {
301
+ _cb_data.final_try = true ;
288
302
report_failure (get_state_string (_state));
289
303
}
290
304
}
291
305
292
306
void CellularStateMachine::state_init ()
293
307
{
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 );
296
310
_cb_data.error = _cellularDevice.is_ready ();
297
311
_status = _cb_data.error ? 0 : DEVICE_READY;
298
312
if (_cb_data.error != NSAPI_ERROR_OK) {
@@ -308,8 +322,8 @@ void CellularStateMachine::state_init()
308
322
309
323
void CellularStateMachine::state_power_on ()
310
324
{
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 );
313
327
if (power_on ()) {
314
328
enter_to_state (STATE_DEVICE_READY);
315
329
} else {
@@ -340,7 +354,7 @@ bool CellularStateMachine::device_ready()
340
354
341
355
void CellularStateMachine::state_device_ready ()
342
356
{
343
- _cellularDevice.set_timeout (TIMEOUT_POWER_ON );
357
+ _cellularDevice.set_timeout (_state_timeout_power_on );
344
358
if (!(_status & DEVICE_READY)) {
345
359
tr_debug (" Device was not ready, calling soft_power_on()" );
346
360
_cb_data.error = _cellularDevice.soft_power_on ();
@@ -364,8 +378,8 @@ void CellularStateMachine::state_device_ready()
364
378
365
379
void CellularStateMachine::state_sim_pin ()
366
380
{
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 );
369
383
if (open_sim ()) {
370
384
bool success = false ;
371
385
for (int type = 0 ; type < CellularNetwork::C_MAX; type++) {
@@ -419,8 +433,7 @@ void CellularStateMachine::state_signal_quality()
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) {
@@ -695,11 +709,10 @@ void CellularStateMachine::device_ready_cb()
695
709
void CellularStateMachine::set_retry_timeout_array (const uint16_t timeout[], int array_len)
696
710
{
697
711
if (!timeout || array_len <= 0 ) {
698
- tr_warn ( " set_retry_timeout_array, timeout array null or invalid length " ) ;
712
+ _retry_array_length = 0 ;
699
713
return ;
700
714
}
701
715
_retry_array_length = array_len > CELLULAR_RETRY_ARRAY_SIZE ? CELLULAR_RETRY_ARRAY_SIZE : array_len;
702
-
703
716
for (int i = 0 ; i < _retry_array_length; i++) {
704
717
_retry_timeout_array[i] = timeout[i];
705
718
}
@@ -713,5 +726,14 @@ void CellularStateMachine::get_retry_timeout_array(uint16_t *timeout, int &array
713
726
array_len = _retry_array_length;
714
727
}
715
728
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
+
716
738
} // namespace
717
739
0 commit comments