Skip to content

Commit 22e193c

Browse files
author
Ari Parkkila
committed
Cellular: Fix CellularStateMachine timeout configurations
1 parent 0066ba9 commit 22e193c

File tree

7 files changed

+75
-16
lines changed

7 files changed

+75
-16
lines changed

features/cellular/framework/API/CellularDevice.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,26 @@ class CellularDevice {
331331
virtual void close_information() = 0;
332332

333333
/** Set the default response timeout.
334+
*
335+
* @remark CellularStateMachine timeouts for all states are also changed to `timeout`.
334336
*
335337
* @param timeout milliseconds to wait response from modem
336338
*/
337339
virtual void set_timeout(int timeout) = 0;
338340

341+
/** Set an array of timeouts to wait before CellularStateMachine retries after failure.
342+
* To disable retry behavior completely use `set_retry_timeout_array(NULL, 0)`.
343+
* CellularContext callback event `cell_callback_data_t.final_try` indicates true when all retries have failed.
344+
*
345+
* @remark Use `set_retry_timeout_array` for CellularStateMachine to wait before it retries again after failure.
346+
* Use `set_timeout` for timeout how long to wait for a response from modem.
347+
*
348+
*
349+
* @param timeout timeout array using seconds
350+
* @param array_len length of the array
351+
*/
352+
void set_retry_timeout_array(const uint16_t timeout[], int array_len);
353+
339354
/** Turn modem debug traces on
340355
*
341356
* @param on set true to enable debug traces

features/cellular/framework/AT/AT_CellularContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,10 @@ void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr)
903903
cell_callback_data_t *data = (cell_callback_data_t *)ptr;
904904
cellular_connection_status_t st = (cellular_connection_status_t)ev;
905905
_cb_data.error = data->error;
906+
_cb_data.final_try = data->final_try;
907+
if (data->final_try) {
908+
_semaphore.release();
909+
}
906910
#if USE_APN_LOOKUP
907911
if (st == CellularSIMStatusChanged && data->status_data == CellularDevice::SimStateReady &&
908912
_cb_data.error == NSAPI_ERROR_OK) {

features/cellular/framework/AT/AT_CellularDevice.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ void AT_CellularDevice::set_timeout(int timeout)
346346
_default_timeout = timeout;
347347

348348
ATHandler::set_at_timeout_list(_default_timeout, true);
349+
350+
if (_state_machine) {
351+
_state_machine->set_timeout(_default_timeout);
352+
}
349353
}
350354

351355
uint16_t AT_CellularDevice::get_send_delay() const

features/cellular/framework/device/CellularContext.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ CellularDevice *CellularContext::get_device() const
7070

7171
void CellularContext::do_connect_with_retry()
7272
{
73+
if (_cb_data.final_try) {
74+
return;
75+
}
7376
do_connect();
7477
if (_cb_data.error == NSAPI_ERROR_OK) {
7578
return;

features/cellular/framework/device/CellularDevice.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,11 @@ nsapi_error_t CellularDevice::shutdown()
235235
return NSAPI_ERROR_OK;
236236
}
237237

238+
void CellularDevice::set_retry_timeout_array(const uint16_t timeout[], int array_len)
239+
{
240+
if (create_state_machine() == NSAPI_ERROR_OK) {
241+
_state_machine->set_retry_timeout_array(timeout, array_len);
242+
}
243+
}
244+
238245
} // namespace mbed

features/cellular/framework/device/CellularStateMachine.cpp

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#define RETRY_COUNT_DEFAULT 3
3838

39+
3940
const int STM_STOPPED = -99;
4041
const int ACTIVE_PDP_CONTEXT = 0x01;
4142
const int ATTACHED_TO_NETWORK = 0x02;
@@ -68,6 +69,12 @@ CellularStateMachine::CellularStateMachine(CellularDevice &device, events::Event
6869
_retry_timeout_array[8] = 600;
6970
_retry_timeout_array[9] = TIMEOUT_NETWORK_MAX;
7071
_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;
7178
}
7279

7380
CellularStateMachine::~CellularStateMachine()
@@ -285,21 +292,23 @@ void CellularStateMachine::enter_to_state(CellularState state)
285292

286293
void CellularStateMachine::retry_state_or_fail()
287294
{
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);
290297
_event_timeout = _retry_timeout_array[_retry_count];
291298
_is_retry = true;
292299
_cb_data.error = NSAPI_ERROR_OK;
300+
_retry_count++;
293301
} else {
302+
_cb_data.final_try = true;
294303
report_failure(get_state_string(_state));
295304
return;
296305
}
297306
}
298307

299308
void CellularStateMachine::state_init()
300309
{
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);
303312
_cb_data.error = _cellularDevice.is_ready();
304313
_status = _cb_data.error ? 0 : DEVICE_READY;
305314
if (_cb_data.error != NSAPI_ERROR_OK) {
@@ -315,8 +324,8 @@ void CellularStateMachine::state_init()
315324

316325
void CellularStateMachine::state_power_on()
317326
{
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);
320329
if (power_on()) {
321330
enter_to_state(STATE_DEVICE_READY);
322331
} else {
@@ -353,7 +362,7 @@ bool CellularStateMachine::device_ready()
353362

354363
void CellularStateMachine::state_device_ready()
355364
{
356-
_cellularDevice.set_timeout(TIMEOUT_POWER_ON);
365+
_cellularDevice.set_timeout(_state_timeout_power_on);
357366
if (!(_status & DEVICE_READY)) {
358367
tr_debug("Device was not ready, calling soft_power_on()");
359368
_cb_data.error = _cellularDevice.soft_power_on();
@@ -377,8 +386,8 @@ void CellularStateMachine::state_device_ready()
377386

378387
void CellularStateMachine::state_sim_pin()
379388
{
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);
382391
if (open_sim()) {
383392
bool success = false;
384393
for (int type = 0; type < CellularNetwork::C_MAX; type++) {
@@ -419,8 +428,7 @@ void CellularStateMachine::state_sim_pin()
419428

420429
void CellularStateMachine::state_registering()
421430
{
422-
_cellularDevice.set_timeout(TIMEOUT_NETWORK);
423-
tr_info("Network registration (timeout %d s)", TIMEOUT_REGISTRATION / 1000);
431+
_cellularDevice.set_timeout(_state_timeout_network);
424432
if (is_registered()) {
425433
if (_cb_data.status_data != CellularNetwork::RegisteredHomeNetwork &&
426434
_cb_data.status_data != CellularNetwork::RegisteredRoaming && _status) {
@@ -432,7 +440,8 @@ void CellularStateMachine::state_registering()
432440
// we are already registered, go to attach
433441
enter_to_state(STATE_ATTACHING_NETWORK);
434442
} 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);
436445
if (!_command_success && !_plmn) { // don't call set_registration twice for manual registration
437446
_cb_data.error = _network->set_registration(_plmn);
438447
_command_success = (_cb_data.error == NSAPI_ERROR_OK);
@@ -443,8 +452,8 @@ void CellularStateMachine::state_registering()
443452

444453
void CellularStateMachine::state_attaching()
445454
{
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);
448457
if (_status != ATTACHED_TO_NETWORK) {
449458
_cb_data.error = _network->set_attach();
450459
}
@@ -683,11 +692,10 @@ void CellularStateMachine::device_ready_cb()
683692
void CellularStateMachine::set_retry_timeout_array(const uint16_t timeout[], int array_len)
684693
{
685694
if (!timeout || array_len <= 0) {
686-
tr_warn("set_retry_timeout_array, timeout array null or invalid length");
695+
_retry_array_length = 0;
687696
return;
688697
}
689698
_retry_array_length = array_len > CELLULAR_RETRY_ARRAY_SIZE ? CELLULAR_RETRY_ARRAY_SIZE : array_len;
690-
691699
for (int i = 0; i < _retry_array_length; i++) {
692700
_retry_timeout_array[i] = timeout[i];
693701
}
@@ -701,5 +709,14 @@ void CellularStateMachine::get_retry_timeout_array(uint16_t *timeout, int &array
701709
array_len = _retry_array_length;
702710
}
703711

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+
704721
} // namespace
705722

features/cellular/framework/device/CellularStateMachine.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ class CellularStateMachine {
180180
nsapi_event_t _current_event;
181181
int _status;
182182
PlatformMutex _mutex;
183+
184+
// Cellular state timeouts
185+
int _state_timeout_power_on;
186+
int _state_timeout_sim_pin;
187+
int _state_timeout_registration;
188+
int _state_timeout_network;
189+
int _state_timeout_connect;
190+
// Change all cellular state timeouts to `timeout`
191+
void set_timeout(int timeout);
183192
};
184193

185194
} // namespace

0 commit comments

Comments
 (0)