Skip to content

Commit acbf534

Browse files
author
Ari Parkkila
committed
Cellular: Make CellularStateMachine timeouts configurable
1 parent b29b55a commit acbf534

File tree

3 files changed

+53
-20
lines changed

3 files changed

+53
-20
lines changed

features/cellular/framework/API/CellularDevice.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class CellularDevice {
131131
* The driver may repeat this if the modem is not responsive to AT commands.
132132
*
133133
* @remark CellularStateMachine calls this when requested to connect.
134+
* Configure response timeout with `cellular.timeout-power-on` in mbed_lib.json.
134135
* If you are not using CellularStateMachine then you need to call this function yourself.
135136
*
136137
* @post You must call init to setup the modem.
@@ -162,6 +163,9 @@ class CellularDevice {
162163
virtual nsapi_error_t set_pin(const char *sim_pin) = 0;
163164

164165
/** Get SIM card's state
166+
*
167+
* @remark CellularStateMachine calls this when requested to connect and SIM pin is set.
168+
* Configure response timeout with `cellular.timeout-sim-pin` in mbed_lib.json.
165169
*
166170
* @param state current state of SIM
167171
* @return NSAPI_ERROR_OK on success
@@ -330,7 +334,9 @@ class CellularDevice {
330334
*/
331335
virtual void close_information() = 0;
332336

333-
/** Set the default response timeout.
337+
/** Set the default response timeout from the modem.
338+
*
339+
* @remark Timeout is overwritten by CellularStateMachine if defaults are defined, see cellular `mbed_lib.json`.
334340
*
335341
* @param timeout milliseconds to wait response from modem
336342
*/

features/cellular/framework/device/CellularStateMachine.cpp

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@
2424
#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_INFO
2525
#endif
2626

27-
// timeout to wait for AT responses
28-
#define TIMEOUT_POWER_ON (1*1000)
29-
#define TIMEOUT_SIM_PIN (1*1000)
30-
#define TIMEOUT_NETWORK (10*1000)
31-
#define TIMEOUT_CONNECT (60*1000)
32-
#define TIMEOUT_REGISTRATION (180*1000)
33-
3427
// maximum time when retrying network register, attach and connect in seconds ( 20minutes )
3528
#define TIMEOUT_NETWORK_MAX (20*60)
3629

@@ -298,8 +291,10 @@ void CellularStateMachine::retry_state_or_fail()
298291

299292
void CellularStateMachine::state_init()
300293
{
301-
_cellularDevice.set_timeout(TIMEOUT_POWER_ON);
302-
tr_info("Start connecting (timeout %d s)", TIMEOUT_POWER_ON / 1000);
294+
#ifdef MBED_CONF_CELLULAR_TIMEOUT_POWER_ON
295+
_cellularDevice.set_timeout(MBED_CONF_CELLULAR_TIMEOUT_POWER_ON);
296+
tr_debug("Wait for modem response (timeout %d s)", MBED_CONF_CELLULAR_TIMEOUT_POWER_ON / 1000);
297+
#endif
303298
_cb_data.error = _cellularDevice.is_ready();
304299
_status = _cb_data.error ? 0 : DEVICE_READY;
305300
if (_cb_data.error != NSAPI_ERROR_OK) {
@@ -315,8 +310,10 @@ void CellularStateMachine::state_init()
315310

316311
void CellularStateMachine::state_power_on()
317312
{
318-
_cellularDevice.set_timeout(TIMEOUT_POWER_ON);
319-
tr_info("Modem power ON (timeout %d s)", TIMEOUT_POWER_ON / 1000);
313+
#ifdef MBED_CONF_CELLULAR_TIMEOUT_POWER_ON
314+
_cellularDevice.set_timeout(MBED_CONF_CELLULAR_TIMEOUT_POWER_ON);
315+
tr_debug("Modem power ON (timeout %d s)", MBED_CONF_CELLULAR_TIMEOUT_POWER_ON / 1000);
316+
#endif
320317
if (power_on()) {
321318
enter_to_state(STATE_DEVICE_READY);
322319
} else {
@@ -353,7 +350,9 @@ bool CellularStateMachine::device_ready()
353350

354351
void CellularStateMachine::state_device_ready()
355352
{
356-
_cellularDevice.set_timeout(TIMEOUT_POWER_ON);
353+
#ifdef MBED_CONF_CELLULAR_TIMEOUT_POWER_ON
354+
_cellularDevice.set_timeout(MBED_CONF_CELLULAR_TIMEOUT_POWER_ON);
355+
#endif
357356
if (!(_status & DEVICE_READY)) {
358357
tr_debug("Device was not ready, calling soft_power_on()");
359358
_cb_data.error = _cellularDevice.soft_power_on();
@@ -377,8 +376,10 @@ void CellularStateMachine::state_device_ready()
377376

378377
void CellularStateMachine::state_sim_pin()
379378
{
380-
_cellularDevice.set_timeout(TIMEOUT_SIM_PIN);
381-
tr_info("Setup SIM (timeout %d s)", TIMEOUT_SIM_PIN / 1000);
379+
#ifdef MBED_CONF_CELLULAR_TIMEOUT_SIM_PIN
380+
_cellularDevice.set_timeout(MBED_CONF_CELLULAR_TIMEOUT_SIM_PIN);
381+
tr_debug("Setup SIM (timeout %d s)", MBED_CONF_CELLULAR_TIMEOUT_SIM_PIN / 1000);
382+
#endif
382383
if (open_sim()) {
383384
bool success = false;
384385
for (int type = 0; type < CellularNetwork::C_MAX; type++) {
@@ -419,8 +420,9 @@ void CellularStateMachine::state_sim_pin()
419420

420421
void CellularStateMachine::state_registering()
421422
{
422-
_cellularDevice.set_timeout(TIMEOUT_NETWORK);
423-
tr_info("Network registration (timeout %d s)", TIMEOUT_REGISTRATION / 1000);
423+
#ifdef MBED_CONF_CELLULAR_TIMEOUT_NETWORK_ATTACH
424+
_cellularDevice.set_timeout(MBED_CONF_CELLULAR_TIMEOUT_NETWORK_ATTACH);
425+
#endif
424426
if (is_registered()) {
425427
if (_cb_data.status_data != CellularNetwork::RegisteredHomeNetwork &&
426428
_cb_data.status_data != CellularNetwork::RegisteredRoaming && _status) {
@@ -432,7 +434,10 @@ void CellularStateMachine::state_registering()
432434
// we are already registered, go to attach
433435
enter_to_state(STATE_ATTACHING_NETWORK);
434436
} else {
435-
_cellularDevice.set_timeout(TIMEOUT_REGISTRATION);
437+
#ifdef MBED_CONF_CELLULAR_TIMEOUT_NETWORK_SELECTION
438+
_cellularDevice.set_timeout(MBED_CONF_CELLULAR_TIMEOUT_NETWORK_SELECTION);
439+
tr_debug("Network selection (timeout %d s)", MBED_CONF_CELLULAR_TIMEOUT_NETWORK_SELECTION / 1000);
440+
#endif
436441
if (!_command_success && !_plmn) { // don't call set_registration twice for manual registration
437442
_cb_data.error = _network->set_registration(_plmn);
438443
_command_success = (_cb_data.error == NSAPI_ERROR_OK);
@@ -443,8 +448,10 @@ void CellularStateMachine::state_registering()
443448

444449
void CellularStateMachine::state_attaching()
445450
{
446-
_cellularDevice.set_timeout(TIMEOUT_CONNECT);
447-
tr_info("Attaching network (timeout %d s)", TIMEOUT_CONNECT / 1000);
451+
#ifdef MBED_CONF_CELLULAR_TIMEOUT_NETWORK_CONNECT
452+
_cellularDevice.set_timeout(MBED_CONF_CELLULAR_TIMEOUT_NETWORK_CONNECT);
453+
tr_debug("Attaching network (timeout %d s)", MBED_CONF_CELLULAR_TIMEOUT_NETWORK_CONNECT / 1000);
454+
#endif
448455
if (_status != ATTACHED_TO_NETWORK) {
449456
_cb_data.error = _network->set_attach();
450457
}

features/cellular/mbed_lib.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@
2020
"control-plane-opt": {
2121
"help": "Enables control plane CIoT EPS optimization",
2222
"value": false
23+
},
24+
"timeout-power-on": {
25+
"help": "Timeout for device power on, null to ignore",
26+
"value": 1000
27+
},
28+
"timeout-sim-pin": {
29+
"help": "Timeout for SIM pin query response from the modem, null to ignore",
30+
"value": 1000
31+
},
32+
"timeout-network-selection": {
33+
"help": "Timeout for cellular selection response from the modem, null to ignore",
34+
"value": 180000
35+
},
36+
"timeout-network-attach": {
37+
"help": "Timeout for packet network attach response from the modem, null to ignore",
38+
"value": 10000
39+
},
40+
"timeout-network-connect": {
41+
"help": "Timeout for network connect and PDP context activation response from the modem, null to ignore",
42+
"value": 60000
2343
}
2444
}
2545
}

0 commit comments

Comments
 (0)