Skip to content

Commit 56955aa

Browse files
author
Ari Parkkila
committed
Cellular: Make CellularStateMachine timeouts configurable
1 parent 76c5272 commit 56955aa

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
@@ -129,6 +129,7 @@ class CellularDevice {
129129
* The driver may repeat this if the modem is not responsive to AT commands.
130130
*
131131
* @remark CellularStateMachine calls this when requested to connect.
132+
* Configure response timeout with `cellular.timeout-power-on` in mbed_lib.json.
132133
* If you are not using CellularStateMachine then you need to call this function yourself.
133134
*
134135
* @post You must call init to setup the modem.
@@ -160,6 +161,9 @@ class CellularDevice {
160161
virtual nsapi_error_t set_pin(const char *sim_pin) = 0;
161162

162163
/** Get SIM card's state
164+
*
165+
* @remark CellularStateMachine calls this when requested to connect and SIM pin is set.
166+
* Configure response timeout with `cellular.timeout-sim-pin` in mbed_lib.json.
163167
*
164168
* @param state current state of SIM
165169
* @return NSAPI_ERROR_OK on success
@@ -326,7 +330,9 @@ class CellularDevice {
326330
*/
327331
virtual void close_information() = 0;
328332

329-
/** Set the default response timeout.
333+
/** Set the default response timeout from the modem.
334+
*
335+
* @remark Timeout is overwritten by CellularStateMachine if defaults are defined, see cellular `mbed_lib.json`.
330336
*
331337
* @param timeout milliseconds to wait response from modem
332338
*/

features/cellular/framework/device/CellularStateMachine.cpp

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

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

@@ -299,8 +292,10 @@ void CellularStateMachine::retry_state_or_fail()
299292

300293
void CellularStateMachine::state_init()
301294
{
302-
_cellularDevice.set_timeout(TIMEOUT_POWER_ON);
303-
tr_info("Start connecting (timeout %d s)", TIMEOUT_POWER_ON / 1000);
295+
#if MBED_CONF_CELLULAR_TIMEOUT_POWER_ON
296+
_cellularDevice.set_timeout(MBED_CONF_CELLULAR_TIMEOUT_POWER_ON);
297+
tr_debug("Wait for modem response (timeout %d s)", MBED_CONF_CELLULAR_TIMEOUT_POWER_ON / 1000);
298+
#endif
304299
_cb_data.error = _cellularDevice.is_ready();
305300
_status = _cb_data.error ? 0 : DEVICE_READY;
306301
if (_cb_data.error != NSAPI_ERROR_OK) {
@@ -316,8 +311,10 @@ void CellularStateMachine::state_init()
316311

317312
void CellularStateMachine::state_power_on()
318313
{
319-
_cellularDevice.set_timeout(TIMEOUT_POWER_ON);
320-
tr_info("Modem power ON (timeout %d s)", TIMEOUT_POWER_ON / 1000);
314+
#if MBED_CONF_CELLULAR_TIMEOUT_POWER_ON
315+
_cellularDevice.set_timeout(MBED_CONF_CELLULAR_TIMEOUT_POWER_ON);
316+
tr_debug("Modem power ON (timeout %d s)", MBED_CONF_CELLULAR_TIMEOUT_POWER_ON / 1000);
317+
#endif
321318
if (power_on()) {
322319
enter_to_state(STATE_DEVICE_READY);
323320
} else {
@@ -354,7 +351,9 @@ bool CellularStateMachine::device_ready()
354351

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

379378
void CellularStateMachine::state_sim_pin()
380379
{
381-
_cellularDevice.set_timeout(TIMEOUT_SIM_PIN);
382-
tr_info("Setup SIM (timeout %d s)", TIMEOUT_SIM_PIN / 1000);
380+
#if MBED_CONF_CELLULAR_TIMEOUT_SIM_PIN
381+
_cellularDevice.set_timeout(MBED_CONF_CELLULAR_TIMEOUT_SIM_PIN);
382+
tr_debug("Setup SIM (timeout %d s)", MBED_CONF_CELLULAR_TIMEOUT_SIM_PIN / 1000);
383+
#endif
383384
if (open_sim()) {
384385
bool success = false;
385386
for (int type = 0; type < CellularNetwork::C_MAX; type++) {
@@ -412,8 +413,9 @@ void CellularStateMachine::state_sim_pin()
412413

413414
void CellularStateMachine::state_registering()
414415
{
415-
_cellularDevice.set_timeout(TIMEOUT_NETWORK);
416-
tr_info("Network registration (timeout %d s)", TIMEOUT_REGISTRATION / 1000);
416+
#if MBED_CONF_CELLULAR_TIMEOUT_NETWORK_ATTACH
417+
_cellularDevice.set_timeout(MBED_CONF_CELLULAR_TIMEOUT_NETWORK_ATTACH);
418+
#endif
417419
if (is_registered()) {
418420
if (_cb_data.status_data != CellularNetwork::RegisteredHomeNetwork &&
419421
_cb_data.status_data != CellularNetwork::RegisteredRoaming && _status) {
@@ -425,7 +427,10 @@ void CellularStateMachine::state_registering()
425427
// we are already registered, go to attach
426428
enter_to_state(STATE_ATTACHING_NETWORK);
427429
} else {
428-
_cellularDevice.set_timeout(TIMEOUT_REGISTRATION);
430+
#if MBED_CONF_CELLULAR_TIMEOUT_NETWORK_SELECTION
431+
_cellularDevice.set_timeout(MBED_CONF_CELLULAR_TIMEOUT_NETWORK_SELECTION);
432+
tr_debug("Network selection (timeout %d s)", MBED_CONF_CELLULAR_TIMEOUT_NETWORK_SELECTION / 1000);
433+
#endif
429434
if (!_command_success && !_plmn) { // don't call set_registration twice for manual registration
430435
_cb_data.error = _network->set_registration(_plmn);
431436
_command_success = (_cb_data.error == NSAPI_ERROR_OK);
@@ -436,8 +441,10 @@ void CellularStateMachine::state_registering()
436441

437442
void CellularStateMachine::state_attaching()
438443
{
439-
_cellularDevice.set_timeout(TIMEOUT_CONNECT);
440-
tr_info("Attaching network (timeout %d s)", TIMEOUT_CONNECT / 1000);
444+
#if MBED_CONF_CELLULAR_TIMEOUT_NETWORK_CONNECT
445+
_cellularDevice.set_timeout(MBED_CONF_CELLULAR_TIMEOUT_NETWORK_CONNECT);
446+
tr_debug("Attaching network (timeout %d s)", MBED_CONF_CELLULAR_TIMEOUT_NETWORK_CONNECT / 1000);
447+
#endif
441448
if (_status != ATTACHED_TO_NETWORK) {
442449
_cb_data.error = _network->set_attach();
443450
}

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, 0 to not change",
26+
"value": 1000
27+
},
28+
"timeout-sim-pin": {
29+
"help": "Timeout for SIM pin query response from the modem, 0 to not change",
30+
"value": 1000
31+
},
32+
"timeout-network-selection": {
33+
"help": "Timeout for cellular selection response from the modem, 0 to not change",
34+
"value": 180000
35+
},
36+
"timeout-network-attach": {
37+
"help": "Timeout for packet network attach response from the modem, 0 to not change",
38+
"value": 10000
39+
},
40+
"timeout-network-connect": {
41+
"help": "Timeout for network connect and PDP context activation response from the modem, 0 to not change",
42+
"value": 60000
2343
}
2444
}
2545
}

0 commit comments

Comments
 (0)