Cellular: Move cellular event queue thread ownership to CellularDevice

Earlier CellularDevice has owned event queue used by cellular (state machine and ATHandler for example),
but the thread used to dispatch the queue has been owned by state machine.

This commit moves the event queue thread to CellularDevice so now the ownership of cellular event queue
is in one place.
pull/12193/head
Kimmo Vaisanen 2019-12-18 10:09:13 +02:00
parent 7fd637b66b
commit 6c647104c7
6 changed files with 27 additions and 50 deletions

View File

@ -35,6 +35,7 @@ set(unittest-test-sources
stubs/CellularContext_stub.cpp stubs/CellularContext_stub.cpp
stubs/ConditionVariable_stub.cpp stubs/ConditionVariable_stub.cpp
stubs/Mutex_stub.cpp stubs/Mutex_stub.cpp
stubs/mbed_shared_queues_stub.cpp
) )
set(unittest-test-flags set(unittest-test-flags

View File

@ -222,13 +222,6 @@ TEST_F(TestCellularStateMachine, test_start_dispatch)
ASSERT_EQ(NSAPI_ERROR_OK, err); ASSERT_EQ(NSAPI_ERROR_OK, err);
ut.delete_state_machine(); 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; delete dev;
dev = NULL; dev = NULL;
} }

View File

@ -21,10 +21,15 @@
#include "CellularStateMachine.h" #include "CellularStateMachine.h"
#include "Callback.h" #include "Callback.h"
#include "ATHandler.h" #include "ATHandler.h"
#if (DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY) #if (DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
#include "UARTSerial.h" #include "UARTSerial.h"
#endif // #if DEVICE_SERIAL #endif // #if DEVICE_SERIAL
#ifdef MBED_CONF_RTOS_PRESENT
#include "Thread.h"
#endif // MBED_CONF_RTOS_PRESENT
/** @file CellularDevice.h /** @file CellularDevice.h
* @brief Class CellularDevice * @brief Class CellularDevice
* *
@ -504,6 +509,10 @@ private: //Member variables
char _sim_pin[MAX_PIN_SIZE + 1]; char _sim_pin[MAX_PIN_SIZE + 1];
char _plmn[MAX_PLMN_SIZE + 1]; char _plmn[MAX_PLMN_SIZE + 1];
PlatformMutex _mutex; PlatformMutex _mutex;
#ifdef MBED_CONF_RTOS_PRESENT
rtos::Thread _queue_thread;
#endif
}; };
/** /**

View File

@ -20,6 +20,7 @@
#include "CellularUtil.h" #include "CellularUtil.h"
#include "CellularLog.h" #include "CellularLog.h"
#include "events/EventQueue.h" #include "events/EventQueue.h"
#include "mbed_shared_queues.h"
namespace mbed { namespace mbed {
@ -33,16 +34,28 @@ MBED_WEAK CellularDevice *CellularDevice::get_target_default_instance()
return NULL; return NULL;
} }
CellularDevice::CellularDevice(FileHandle *fh) : _network_ref_count(0), CellularDevice::CellularDevice(FileHandle *fh) :
_network_ref_count(0),
#if MBED_CONF_CELLULAR_USE_SMS #if MBED_CONF_CELLULAR_USE_SMS
_sms_ref_count(0), _sms_ref_count(0),
#endif //MBED_CONF_CELLULAR_USE_SMS #endif //MBED_CONF_CELLULAR_USE_SMS
_info_ref_count(0), _fh(fh), _queue(10 * EVENTS_EVENT_SIZE), _state_machine(0), _info_ref_count(0), _fh(fh), _queue(10 * EVENTS_EVENT_SIZE), _state_machine(0),
_status_cb(0), _nw(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); MBED_ASSERT(fh);
set_sim_pin(NULL); set_sim_pin(NULL);
set_plmn(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() CellularDevice::~CellularDevice()

View File

@ -18,8 +18,6 @@
#include "CellularStateMachine.h" #include "CellularStateMachine.h"
#include "CellularDevice.h" #include "CellularDevice.h"
#include "CellularLog.h" #include "CellularLog.h"
#include "Thread.h"
#include "mbed_shared_queues.h"
#ifndef MBED_TRACE_MAX_LEVEL #ifndef MBED_TRACE_MAX_LEVEL
#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_INFO #define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_INFO
@ -51,9 +49,6 @@ const int DEVICE_READY = 0x04;
namespace mbed { namespace mbed {
CellularStateMachine::CellularStateMachine(CellularDevice &device, events::EventQueue &queue, CellularNetwork &nw) : 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), _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_status_cb(0), _network(nw), _queue(queue), _sim_pin(0), _retry_count(0),
_event_timeout(-1), _event_id(-1), _plmn(0), _command_success(false), _event_timeout(-1), _event_id(-1), _plmn(0), _command_success(false),
@ -106,16 +101,6 @@ void CellularStateMachine::reset()
void CellularStateMachine::stop() void CellularStateMachine::stop()
{ {
tr_debug("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(); reset();
_event_id = STM_STOPPED; _event_id = STM_STOPPED;
} }
@ -643,24 +628,11 @@ void CellularStateMachine::event()
nsapi_error_t CellularStateMachine::start_dispatch() nsapi_error_t CellularStateMachine::start_dispatch()
{ {
#ifdef MBED_CONF_RTOS_PRESENT if (_event_id != -1) {
if (!_queue_thread) { tr_warn("Canceling ongoing event (%d)", _event_id);
_queue_thread = new rtos::Thread(osPriorityNormal, 2048, NULL, "stm_queue"); _queue.cancel(_event_id);
_event_id = STM_STOPPED;
} }
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; _event_id = -1;
#else
_queue.chain(mbed_event_queue());
#endif
return NSAPI_ERROR_OK; return NSAPI_ERROR_OK;
} }

View File

@ -22,12 +22,6 @@
#include "CellularCommon.h" #include "CellularCommon.h"
#include "PlatformMutex.h" #include "PlatformMutex.h"
#ifdef MBED_CONF_RTOS_PRESENT
namespace rtos {
class Thread;
}
#endif
namespace mbed { namespace mbed {
class CellularDevice; class CellularDevice;
@ -162,11 +156,6 @@ private:
void change_timeout(const int &timeout); void change_timeout(const int &timeout);
private: private:
#ifdef MBED_CONF_RTOS_PRESENT
rtos::Thread *_queue_thread;
#endif
CellularDevice &_cellularDevice; CellularDevice &_cellularDevice;
CellularState _state; CellularState _state;
CellularState _next_state; CellularState _next_state;