Skip to content

Commit 742bfd3

Browse files
author
Ari Parkkila
committed
Cellular: Fix statemachine stop
1 parent 7a5e4ca commit 742bfd3

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

features/cellular/framework/device/CellularDevice.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,20 @@ nsapi_error_t CellularDevice::create_state_machine()
124124
_nw->attach(callback(this, &CellularDevice::stm_callback));
125125
_state_machine = new CellularStateMachine(*this, *get_queue(), *_nw);
126126
_state_machine->set_cellular_callback(callback(this, &CellularDevice::stm_callback));
127-
err = _state_machine->start_dispatch();
128-
if (err) {
129-
tr_error("Start state machine failed.");
130-
delete _state_machine;
131-
_state_machine = NULL;
132-
}
133-
134127
if (strlen(_plmn)) {
135128
_state_machine->set_plmn(_plmn);
136129
}
137130
if (strlen(_sim_pin)) {
138131
_state_machine->set_sim_pin(_sim_pin);
139132
}
140133
}
134+
err = _state_machine->start_dispatch();
135+
if (err) {
136+
tr_error("Start state machine failed.");
137+
delete _state_machine;
138+
_state_machine = NULL;
139+
return err;
140+
}
141141
return err;
142142
}
143143

features/cellular/framework/device/CellularStateMachine.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ void CellularStateMachine::stop()
103103
{
104104
tr_debug("CellularStateMachine stop");
105105
if (_queue_thread) {
106-
_queue.break_dispatch();
107106
_queue_thread->terminate();
108107
delete _queue_thread;
109108
_queue_thread = NULL;
@@ -366,6 +365,9 @@ void CellularStateMachine::state_device_ready()
366365
_status = 0;
367366
enter_to_state(STATE_SIM_PIN);
368367
}
368+
} else {
369+
_status = 0;
370+
enter_to_state(STATE_INIT);
369371
}
370372
}
371373
if (_cb_data.error != NSAPI_ERROR_OK) {
@@ -546,7 +548,7 @@ bool CellularStateMachine::get_current_status(CellularStateMachine::CellularStat
546548
void CellularStateMachine::event()
547549
{
548550
// Don't send Signal quality when in signal quality state or it can confuse callback functions when running retry logic
549-
if (_state != STATE_SIGNAL_QUALITY) {
551+
if (_state > STATE_SIGNAL_QUALITY) {
550552
_cb_data.error = _network.get_signal_quality(_signal_quality.rssi, &_signal_quality.ber);
551553
_cb_data.data = &_signal_quality;
552554

@@ -624,15 +626,21 @@ void CellularStateMachine::event()
624626

625627
nsapi_error_t CellularStateMachine::start_dispatch()
626628
{
627-
MBED_ASSERT(!_queue_thread);
629+
if (!_queue_thread) {
630+
_queue_thread = new rtos::Thread(osPriorityNormal, 2048, NULL, "stm_queue");
631+
_event_id = STM_STOPPED;
632+
}
628633

629-
_queue_thread = new rtos::Thread(osPriorityNormal, 2048, NULL, "stm_queue");
630-
if (_queue_thread->start(callback(&_queue, &events::EventQueue::dispatch_forever)) != osOK) {
631-
report_failure("Failed to start thread.");
632-
stop();
633-
return NSAPI_ERROR_NO_MEMORY;
634+
if (_event_id == STM_STOPPED) {
635+
if (_queue_thread->start(callback(&_queue, &events::EventQueue::dispatch_forever)) != osOK) {
636+
report_failure("Failed to start thread.");
637+
stop();
638+
return NSAPI_ERROR_NO_MEMORY;
639+
}
634640
}
635641

642+
_event_id = -1;
643+
636644
return NSAPI_ERROR_OK;
637645
}
638646

0 commit comments

Comments
 (0)