Skip to content

Commit 2d3c402

Browse files
authored
Merge pull request ARMmbed#12193 from kivaisan/move_event_thread_to_cellulardevice
Cellular: Move cellular event queue thread ownership to CellularDevice
2 parents 39daa5f + 6c64710 commit 2d3c402

File tree

6 files changed

+27
-50
lines changed

6 files changed

+27
-50
lines changed

UNITTESTS/features/cellular/framework/device/cellulardevice/unittest.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ set(unittest-test-sources
3535
stubs/CellularContext_stub.cpp
3636
stubs/ConditionVariable_stub.cpp
3737
stubs/Mutex_stub.cpp
38+
stubs/mbed_shared_queues_stub.cpp
3839
)
3940

4041
set(unittest-test-flags

UNITTESTS/features/cellular/framework/device/cellularstatemachine/cellularstatemachinetest.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,6 @@ TEST_F(TestCellularStateMachine, test_start_dispatch)
222222
ASSERT_EQ(NSAPI_ERROR_OK, err);
223223
ut.delete_state_machine();
224224

225-
Thread_stub::osStatus_value = osErrorNoMemory;
226-
stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
227-
EXPECT_TRUE(stm);
228-
err = ut.start_dispatch();
229-
ASSERT_EQ(NSAPI_ERROR_NO_MEMORY, err);
230-
ut.delete_state_machine();
231-
232225
delete dev;
233226
dev = NULL;
234227
}

features/cellular/framework/API/CellularDevice.h

+9
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@
2121
#include "CellularStateMachine.h"
2222
#include "Callback.h"
2323
#include "ATHandler.h"
24+
2425
#if (DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
2526
#include "UARTSerial.h"
2627
#endif // #if DEVICE_SERIAL
2728

29+
#ifdef MBED_CONF_RTOS_PRESENT
30+
#include "Thread.h"
31+
#endif // MBED_CONF_RTOS_PRESENT
32+
2833
/** @file CellularDevice.h
2934
* @brief Class CellularDevice
3035
*
@@ -504,6 +509,10 @@ class CellularDevice {
504509
char _sim_pin[MAX_PIN_SIZE + 1];
505510
char _plmn[MAX_PLMN_SIZE + 1];
506511
PlatformMutex _mutex;
512+
513+
#ifdef MBED_CONF_RTOS_PRESENT
514+
rtos::Thread _queue_thread;
515+
#endif
507516
};
508517

509518
/**

features/cellular/framework/device/CellularDevice.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "CellularUtil.h"
2121
#include "CellularLog.h"
2222
#include "events/EventQueue.h"
23+
#include "mbed_shared_queues.h"
2324

2425
namespace mbed {
2526

@@ -33,16 +34,28 @@ MBED_WEAK CellularDevice *CellularDevice::get_target_default_instance()
3334
return NULL;
3435
}
3536

36-
CellularDevice::CellularDevice(FileHandle *fh) : _network_ref_count(0),
37+
CellularDevice::CellularDevice(FileHandle *fh) :
38+
_network_ref_count(0),
3739
#if MBED_CONF_CELLULAR_USE_SMS
3840
_sms_ref_count(0),
3941
#endif //MBED_CONF_CELLULAR_USE_SMS
4042
_info_ref_count(0), _fh(fh), _queue(10 * EVENTS_EVENT_SIZE), _state_machine(0),
4143
_status_cb(0), _nw(0)
44+
#ifdef MBED_CONF_RTOS_PRESENT
45+
, _queue_thread(osPriorityNormal, 2048, NULL, "cellular_queue")
46+
#endif // MBED_CONF_RTOS_PRESENT
4247
{
4348
MBED_ASSERT(fh);
4449
set_sim_pin(NULL);
4550
set_plmn(NULL);
51+
52+
#ifdef MBED_CONF_RTOS_PRESENT
53+
if (_queue_thread.start(callback(&_queue, &events::EventQueue::dispatch_forever)) != osOK) {
54+
tr_error("Failed to start thread");
55+
}
56+
#else
57+
_queue.chain(mbed_event_queue());
58+
#endif
4659
}
4760

4861
CellularDevice::~CellularDevice()

features/cellular/framework/device/CellularStateMachine.cpp

+3-31
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
#include "CellularStateMachine.h"
1919
#include "CellularDevice.h"
2020
#include "CellularLog.h"
21-
#include "Thread.h"
22-
#include "mbed_shared_queues.h"
2321

2422
#ifndef MBED_TRACE_MAX_LEVEL
2523
#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_INFO
@@ -51,9 +49,6 @@ const int DEVICE_READY = 0x04;
5149
namespace mbed {
5250

5351
CellularStateMachine::CellularStateMachine(CellularDevice &device, events::EventQueue &queue, CellularNetwork &nw) :
54-
#ifdef MBED_CONF_RTOS_PRESENT
55-
_queue_thread(0),
56-
#endif
5752
_cellularDevice(device), _state(STATE_INIT), _next_state(_state), _target_state(_state),
5853
_event_status_cb(0), _network(nw), _queue(queue), _sim_pin(0), _retry_count(0),
5954
_event_timeout(-1), _event_id(-1), _plmn(0), _command_success(false),
@@ -106,16 +101,6 @@ void CellularStateMachine::reset()
106101
void CellularStateMachine::stop()
107102
{
108103
tr_debug("CellularStateMachine stop");
109-
#ifdef MBED_CONF_RTOS_PRESENT
110-
if (_queue_thread) {
111-
_queue_thread->terminate();
112-
delete _queue_thread;
113-
_queue_thread = NULL;
114-
}
115-
#else
116-
_queue.chain(NULL);
117-
#endif
118-
119104
reset();
120105
_event_id = STM_STOPPED;
121106
}
@@ -643,24 +628,11 @@ void CellularStateMachine::event()
643628

644629
nsapi_error_t CellularStateMachine::start_dispatch()
645630
{
646-
#ifdef MBED_CONF_RTOS_PRESENT
647-
if (!_queue_thread) {
648-
_queue_thread = new rtos::Thread(osPriorityNormal, 2048, NULL, "stm_queue");
649-
_event_id = STM_STOPPED;
650-
}
651-
652-
if (_event_id == STM_STOPPED) {
653-
if (_queue_thread->start(callback(&_queue, &events::EventQueue::dispatch_forever)) != osOK) {
654-
report_failure("Failed to start thread.");
655-
stop();
656-
return NSAPI_ERROR_NO_MEMORY;
657-
}
631+
if (_event_id != -1) {
632+
tr_warn("Canceling ongoing event (%d)", _event_id);
633+
_queue.cancel(_event_id);
658634
}
659-
660635
_event_id = -1;
661-
#else
662-
_queue.chain(mbed_event_queue());
663-
#endif
664636
return NSAPI_ERROR_OK;
665637
}
666638

features/cellular/framework/device/CellularStateMachine.h

-11
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@
2222
#include "CellularCommon.h"
2323
#include "PlatformMutex.h"
2424

25-
#ifdef MBED_CONF_RTOS_PRESENT
26-
namespace rtos {
27-
class Thread;
28-
}
29-
#endif
30-
3125
namespace mbed {
3226

3327
class CellularDevice;
@@ -162,11 +156,6 @@ class CellularStateMachine {
162156
void change_timeout(const int &timeout);
163157

164158
private:
165-
166-
#ifdef MBED_CONF_RTOS_PRESENT
167-
rtos::Thread *_queue_thread;
168-
#endif
169-
170159
CellularDevice &_cellularDevice;
171160
CellularState _state;
172161
CellularState _next_state;

0 commit comments

Comments
 (0)