Skip to content

Cellular: Move cellular event queue thread ownership to CellularDevice #12193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions features/cellular/framework/API/CellularDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -504,6 +509,10 @@ class CellularDevice {
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
};

/**
Expand Down
15 changes: 14 additions & 1 deletion features/cellular/framework/device/CellularDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "CellularUtil.h"
#include "CellularLog.h"
#include "events/EventQueue.h"
#include "mbed_shared_queues.h"

namespace mbed {

Expand All @@ -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()
Expand Down
34 changes: 3 additions & 31 deletions features/cellular/framework/device/CellularStateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 == 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;
}
if (_event_id != -1) {
tr_warn("Canceling ongoing event (%d)", _event_id);
_queue.cancel(_event_id);
}

_event_id = -1;
#else
_queue.chain(mbed_event_queue());
#endif
return NSAPI_ERROR_OK;
}

Expand Down
11 changes: 0 additions & 11 deletions features/cellular/framework/device/CellularStateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@
#include "CellularCommon.h"
#include "PlatformMutex.h"

#ifdef MBED_CONF_RTOS_PRESENT
namespace rtos {
class Thread;
}
#endif

namespace mbed {

class CellularDevice;
Expand Down Expand Up @@ -162,11 +156,6 @@ class CellularStateMachine {
void change_timeout(const int &timeout);

private:

#ifdef MBED_CONF_RTOS_PRESENT
rtos::Thread *_queue_thread;
#endif

CellularDevice &_cellularDevice;
CellularState _state;
CellularState _next_state;
Expand Down