Skip to content

Commit e7b8ccf

Browse files
authored
Merge pull request #10511 from AriParkkila/cell-statemachine-timeout
Cellular: Fix setting of statemachine timeout
2 parents 35db06e + 830b423 commit e7b8ccf

File tree

7 files changed

+45
-8
lines changed

7 files changed

+45
-8
lines changed

UNITTESTS/stubs/AT_CellularDevice_stub.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,7 @@ nsapi_error_t AT_CellularDevice::soft_power_off()
242242
{
243243
return NSAPI_ERROR_OK;
244244
}
245+
246+
void AT_CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr, CellularContext *ctx)
247+
{
248+
}

features/cellular/framework/API/CellularDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ class CellularDevice {
450450
* This method will broadcast to every interested classes:
451451
* CellularContext (might be many) and CellularStateMachine if available.
452452
*/
453-
void cellular_callback(nsapi_event_t ev, intptr_t ptr, CellularContext *ctx = NULL);
453+
virtual void cellular_callback(nsapi_event_t ev, intptr_t ptr, CellularContext *ctx = NULL);
454454
void stm_callback(nsapi_event_t ev, intptr_t ptr);
455455
int _network_ref_count;
456456
int _sms_ref_count;

features/cellular/framework/AT/AT_CellularDevice.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,19 @@ nsapi_error_t AT_CellularDevice::set_power_save_mode(int periodic_time, int acti
601601

602602
return _at->unlock_return_error();
603603
}
604+
605+
void AT_CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr, CellularContext *ctx)
606+
{
607+
if (ev >= NSAPI_EVENT_CELLULAR_STATUS_BASE && ev <= NSAPI_EVENT_CELLULAR_STATUS_END) {
608+
cellular_connection_status_t cell_ev = (cellular_connection_status_t)ev;
609+
if (cell_ev == CellularDeviceTimeout) {
610+
cell_callback_data_t *data = (cell_callback_data_t *)ptr;
611+
int timeout = *(int *)data->data;
612+
if (_default_timeout != timeout) {
613+
_default_timeout = timeout;
614+
ATHandler::set_at_timeout_list(_default_timeout, true);
615+
}
616+
}
617+
}
618+
CellularDevice::cellular_callback(ev, ptr, ctx);
619+
}

features/cellular/framework/AT/AT_CellularDevice.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ class AT_CellularDevice : public CellularDevice {
141141
bool _modem_debug_on;
142142
ATHandler *_at;
143143

144+
protected:
145+
virtual void cellular_callback(nsapi_event_t ev, intptr_t ptr, CellularContext *ctx = NULL);
146+
144147
private:
145148
void urc_nw_deact();
146149
void urc_pdn_deact();

features/cellular/framework/common/CellularCommon.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ typedef enum cellular_event_status {
6565
CellularSignalQuality = NSAPI_EVENT_CELLULAR_STATUS_BASE + 8, /* cell_callback_data_t.error will contains return value when signal quality was queried. data will hold the pointer to cell_signal_quality struct. See possible values from ../API/CellularNetwork.h*/
6666
CellularStateRetryEvent = NSAPI_EVENT_CELLULAR_STATUS_BASE + 9, /* cell_callback_data_t.error contain an error if any. cell_callback_data_t.status_data contains cellular_event_status and it specifies the operation which is retried.
6767
cellular_event_status.data contains current retrycount */
68+
CellularDeviceTimeout = NSAPI_EVENT_CELLULAR_STATUS_BASE + 10,/* cell_callback_data_t.error contain an error or NSAPI_ERROR_OK,
69+
cell_callback_data_t.status_data contains the current cellular_connection_status_t,
70+
cellular_event_status.data contains new timeout value in milliseconds */
6871
} cellular_connection_status_t;
6972

7073
#endif // CELLULAR_COMMON_

features/cellular/framework/device/CellularStateMachine.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ void CellularStateMachine::retry_state_or_fail()
305305

306306
void CellularStateMachine::state_init()
307307
{
308-
_cellularDevice.set_timeout(_state_timeout_power_on);
308+
change_timeout(_state_timeout_power_on);
309309
tr_info("Start connecting (timeout %d ms)", _state_timeout_power_on);
310310
_cb_data.error = _cellularDevice.is_ready();
311311
_status = _cb_data.error ? 0 : DEVICE_READY;
@@ -322,7 +322,7 @@ void CellularStateMachine::state_init()
322322

323323
void CellularStateMachine::state_power_on()
324324
{
325-
_cellularDevice.set_timeout(_state_timeout_power_on);
325+
change_timeout(_state_timeout_power_on);
326326
tr_info("Modem power ON (timeout %d ms)", _state_timeout_power_on);
327327
if (power_on()) {
328328
enter_to_state(STATE_DEVICE_READY);
@@ -354,7 +354,7 @@ bool CellularStateMachine::device_ready()
354354

355355
void CellularStateMachine::state_device_ready()
356356
{
357-
_cellularDevice.set_timeout(_state_timeout_power_on);
357+
change_timeout(_state_timeout_power_on);
358358
if (!(_status & DEVICE_READY)) {
359359
tr_debug("Device was not ready, calling soft_power_on()");
360360
_cb_data.error = _cellularDevice.soft_power_on();
@@ -378,7 +378,7 @@ void CellularStateMachine::state_device_ready()
378378

379379
void CellularStateMachine::state_sim_pin()
380380
{
381-
_cellularDevice.set_timeout(_state_timeout_sim_pin);
381+
change_timeout(_state_timeout_sim_pin);
382382
tr_info("Setup SIM (timeout %d ms)", _state_timeout_sim_pin);
383383
if (open_sim()) {
384384
bool success = false;
@@ -433,7 +433,7 @@ void CellularStateMachine::state_signal_quality()
433433

434434
void CellularStateMachine::state_registering()
435435
{
436-
_cellularDevice.set_timeout(_state_timeout_network);
436+
change_timeout(_state_timeout_network);
437437
if (is_registered()) {
438438
if (_cb_data.status_data != CellularNetwork::RegisteredHomeNetwork &&
439439
_cb_data.status_data != CellularNetwork::RegisteredRoaming && _status) {
@@ -446,7 +446,7 @@ void CellularStateMachine::state_registering()
446446
enter_to_state(STATE_ATTACHING_NETWORK);
447447
} else {
448448
tr_info("Network registration (timeout %d ms)", _state_timeout_registration);
449-
_cellularDevice.set_timeout(_state_timeout_registration);
449+
change_timeout(_state_timeout_registration);
450450
if (!_command_success && !_plmn) { // don't call set_registration twice for manual registration
451451
_cb_data.error = _network.set_registration(_plmn);
452452
_command_success = (_cb_data.error == NSAPI_ERROR_OK);
@@ -458,7 +458,7 @@ void CellularStateMachine::state_registering()
458458
void CellularStateMachine::state_attaching()
459459
{
460460
if (_status != ATTACHED_TO_NETWORK) {
461-
_cellularDevice.set_timeout(_state_timeout_connect);
461+
change_timeout(_state_timeout_connect);
462462
tr_info("Attaching network (timeout %d ms)", _state_timeout_connect);
463463
_cb_data.error = _network.set_attach();
464464
}
@@ -648,6 +648,16 @@ void CellularStateMachine::send_event_cb(cellular_connection_status_t status)
648648
}
649649
}
650650

651+
void CellularStateMachine::change_timeout(const int &timeout)
652+
{
653+
_cb_data.status_data = _current_event;
654+
_cb_data.data = &timeout;
655+
_cb_data.error = NSAPI_ERROR_OK;
656+
// event callback is a preferred method to communicate to CellularDevice,
657+
// for example calling CellularDevice::set_timeout would call back to this class
658+
send_event_cb(CellularDeviceTimeout);
659+
}
660+
651661
bool CellularStateMachine::check_is_target_reached()
652662
{
653663
if (((_target_state == _state || _target_state < _next_state) && _cb_data.error == NSAPI_ERROR_OK && !_is_retry) ||

features/cellular/framework/device/CellularStateMachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class CellularStateMachine {
157157
void pre_event(CellularState state);
158158
bool check_is_target_reached();
159159
void send_event_cb(cellular_connection_status_t status);
160+
void change_timeout(const int &timeout);
160161

161162
CellularDevice &_cellularDevice;
162163
CellularState _state;

0 commit comments

Comments
 (0)