diff --git a/UNITTESTS/features/cellular/framework/device/cellulardevice/unittest.cmake b/UNITTESTS/features/cellular/framework/device/cellulardevice/unittest.cmake index c8c7b7d614..fcd1611e5c 100644 --- a/UNITTESTS/features/cellular/framework/device/cellulardevice/unittest.cmake +++ b/UNITTESTS/features/cellular/framework/device/cellulardevice/unittest.cmake @@ -35,6 +35,7 @@ set(unittest-test-sources stubs/CellularContext_stub.cpp stubs/ConditionVariable_stub.cpp stubs/Mutex_stub.cpp + stubs/mbed_shared_queues_stub.cpp ) set(unittest-test-flags diff --git a/UNITTESTS/features/cellular/framework/device/cellularstatemachine/cellularstatemachinetest.cpp b/UNITTESTS/features/cellular/framework/device/cellularstatemachine/cellularstatemachinetest.cpp index cb019c0d25..8f4ad47e62 100644 --- a/UNITTESTS/features/cellular/framework/device/cellularstatemachine/cellularstatemachinetest.cpp +++ b/UNITTESTS/features/cellular/framework/device/cellularstatemachine/cellularstatemachinetest.cpp @@ -222,13 +222,6 @@ TEST_F(TestCellularStateMachine, test_start_dispatch) ASSERT_EQ(NSAPI_ERROR_OK, err); ut.delete_state_machine(); - Thread_stub::osStatus_value = osErrorNoMemory; - stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network()); - EXPECT_TRUE(stm); - err = ut.start_dispatch(); - ASSERT_EQ(NSAPI_ERROR_NO_MEMORY, err); - ut.delete_state_machine(); - delete dev; dev = NULL; } diff --git a/features/cellular/framework/API/CellularDevice.h b/features/cellular/framework/API/CellularDevice.h index f193fbcde6..b6f4e89d8f 100644 --- a/features/cellular/framework/API/CellularDevice.h +++ b/features/cellular/framework/API/CellularDevice.h @@ -21,10 +21,15 @@ #include "CellularStateMachine.h" #include "Callback.h" #include "ATHandler.h" + #if (DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY) #include "UARTSerial.h" #endif // #if DEVICE_SERIAL +#ifdef MBED_CONF_RTOS_PRESENT +#include "Thread.h" +#endif // MBED_CONF_RTOS_PRESENT + /** @file CellularDevice.h * @brief Class CellularDevice * @@ -504,6 +509,10 @@ private: //Member variables char _sim_pin[MAX_PIN_SIZE + 1]; char _plmn[MAX_PLMN_SIZE + 1]; PlatformMutex _mutex; + +#ifdef MBED_CONF_RTOS_PRESENT + rtos::Thread _queue_thread; +#endif }; /** diff --git a/features/cellular/framework/device/CellularDevice.cpp b/features/cellular/framework/device/CellularDevice.cpp index 0537160444..602a3e38a1 100644 --- a/features/cellular/framework/device/CellularDevice.cpp +++ b/features/cellular/framework/device/CellularDevice.cpp @@ -20,6 +20,7 @@ #include "CellularUtil.h" #include "CellularLog.h" #include "events/EventQueue.h" +#include "mbed_shared_queues.h" namespace mbed { @@ -33,16 +34,28 @@ MBED_WEAK CellularDevice *CellularDevice::get_target_default_instance() return NULL; } -CellularDevice::CellularDevice(FileHandle *fh) : _network_ref_count(0), +CellularDevice::CellularDevice(FileHandle *fh) : + _network_ref_count(0), #if MBED_CONF_CELLULAR_USE_SMS _sms_ref_count(0), #endif //MBED_CONF_CELLULAR_USE_SMS _info_ref_count(0), _fh(fh), _queue(10 * EVENTS_EVENT_SIZE), _state_machine(0), _status_cb(0), _nw(0) +#ifdef MBED_CONF_RTOS_PRESENT + , _queue_thread(osPriorityNormal, 2048, NULL, "cellular_queue") +#endif // MBED_CONF_RTOS_PRESENT { MBED_ASSERT(fh); set_sim_pin(NULL); set_plmn(NULL); + +#ifdef MBED_CONF_RTOS_PRESENT + if (_queue_thread.start(callback(&_queue, &events::EventQueue::dispatch_forever)) != osOK) { + tr_error("Failed to start thread"); + } +#else + _queue.chain(mbed_event_queue()); +#endif } CellularDevice::~CellularDevice() diff --git a/features/cellular/framework/device/CellularStateMachine.cpp b/features/cellular/framework/device/CellularStateMachine.cpp index 08b779773b..dd8696e8cd 100644 --- a/features/cellular/framework/device/CellularStateMachine.cpp +++ b/features/cellular/framework/device/CellularStateMachine.cpp @@ -18,8 +18,6 @@ #include "CellularStateMachine.h" #include "CellularDevice.h" #include "CellularLog.h" -#include "Thread.h" -#include "mbed_shared_queues.h" #ifndef MBED_TRACE_MAX_LEVEL #define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_INFO @@ -51,9 +49,6 @@ const int DEVICE_READY = 0x04; namespace mbed { CellularStateMachine::CellularStateMachine(CellularDevice &device, events::EventQueue &queue, CellularNetwork &nw) : -#ifdef MBED_CONF_RTOS_PRESENT - _queue_thread(0), -#endif _cellularDevice(device), _state(STATE_INIT), _next_state(_state), _target_state(_state), _event_status_cb(0), _network(nw), _queue(queue), _sim_pin(0), _retry_count(0), _event_timeout(-1), _event_id(-1), _plmn(0), _command_success(false), @@ -106,16 +101,6 @@ void CellularStateMachine::reset() void CellularStateMachine::stop() { tr_debug("CellularStateMachine stop"); -#ifdef MBED_CONF_RTOS_PRESENT - if (_queue_thread) { - _queue_thread->terminate(); - delete _queue_thread; - _queue_thread = NULL; - } -#else - _queue.chain(NULL); -#endif - reset(); _event_id = STM_STOPPED; } @@ -643,24 +628,11 @@ void CellularStateMachine::event() nsapi_error_t CellularStateMachine::start_dispatch() { -#ifdef MBED_CONF_RTOS_PRESENT - if (!_queue_thread) { - _queue_thread = new rtos::Thread(osPriorityNormal, 2048, NULL, "stm_queue"); - _event_id = STM_STOPPED; + if (_event_id != -1) { + tr_warn("Canceling ongoing event (%d)", _event_id); + _queue.cancel(_event_id); } - - if (_event_id == STM_STOPPED) { - if (_queue_thread->start(callback(&_queue, &events::EventQueue::dispatch_forever)) != osOK) { - report_failure("Failed to start thread."); - stop(); - return NSAPI_ERROR_NO_MEMORY; - } - } - _event_id = -1; -#else - _queue.chain(mbed_event_queue()); -#endif return NSAPI_ERROR_OK; } diff --git a/features/cellular/framework/device/CellularStateMachine.h b/features/cellular/framework/device/CellularStateMachine.h index 6fda9abe39..edae8c069a 100644 --- a/features/cellular/framework/device/CellularStateMachine.h +++ b/features/cellular/framework/device/CellularStateMachine.h @@ -22,12 +22,6 @@ #include "CellularCommon.h" #include "PlatformMutex.h" -#ifdef MBED_CONF_RTOS_PRESENT -namespace rtos { -class Thread; -} -#endif - namespace mbed { class CellularDevice; @@ -162,11 +156,6 @@ private: void change_timeout(const int &timeout); private: - -#ifdef MBED_CONF_RTOS_PRESENT - rtos::Thread *_queue_thread; -#endif - CellularDevice &_cellularDevice; CellularState _state; CellularState _next_state;