Skip to content

Commit 32a1015

Browse files
author
Teppo Järvelin
committed
Cellular: new state machine state and better info from stm
Added new state (signal quality) and more information about progress so application/driver can build recovery logic.
1 parent d1da622 commit 32a1015

File tree

11 files changed

+136
-98
lines changed

11 files changed

+136
-98
lines changed

UNITTESTS/features/cellular/framework/device/cellulardevice/cellulardevicetest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ TEST_F(TestCellularDevice, test_set_sim_ready)
118118
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
119119
ASSERT_EQ(dev->set_sim_ready(), NSAPI_ERROR_OK);
120120

121-
CellularStateMachine_stub::get_current_current_state = STATE_MANUAL_REGISTERING_NETWORK;
121+
CellularStateMachine_stub::get_current_current_state = STATE_REGISTERING_NETWORK;
122122
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
123123
ASSERT_EQ(dev->set_sim_ready(), NSAPI_ERROR_ALREADY);
124124

125125
CellularStateMachine_stub::bool_value = true;
126-
CellularStateMachine_stub::get_current_target_state = STATE_MANUAL_REGISTERING_NETWORK;
126+
CellularStateMachine_stub::get_current_target_state = STATE_REGISTERING_NETWORK;
127127
CellularStateMachine_stub::get_current_current_state = STATE_POWER_ON;
128128
ASSERT_EQ(dev->set_sim_ready(), NSAPI_ERROR_IN_PROGRESS);
129129
delete dev;

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ enum UT_CellularState {
3333
UT_STATE_POWER_ON,
3434
UT_STATE_DEVICE_READY,
3535
UT_STATE_SIM_PIN,
36+
UT_STATE_SIGNAL_QUALITY,
3637
UT_STATE_REGISTERING_NETWORK,
3738
UT_STATE_ATTACHING_NETWORK,
3839
UT_STATE_MAX_FSM_STATE
@@ -73,9 +74,9 @@ class UT_CellularStateMachine {
7374
_state_machine = NULL;
7475
}
7576

76-
CellularStateMachine *create_state_machine(CellularDevice &device, events::EventQueue &queue)
77+
CellularStateMachine *create_state_machine(CellularDevice &device, events::EventQueue &queue, CellularNetwork &nw)
7778
{
78-
_state_machine = new CellularStateMachine(device, queue);
79+
_state_machine = new CellularStateMachine(device, queue, nw);
7980
return _state_machine;
8081
}
8182

@@ -171,7 +172,7 @@ TEST_F(TestCellularStateMachine, test_create_delete)
171172
CellularDevice *dev = new myCellularDevice(&fh1);
172173
EXPECT_TRUE(dev);
173174

174-
CellularStateMachine *stm = ut.create_state_machine(*dev, *dev->get_queue());
175+
CellularStateMachine *stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
175176
EXPECT_TRUE(stm);
176177
ut.delete_state_machine();
177178

@@ -187,7 +188,7 @@ TEST_F(TestCellularStateMachine, test_setters)
187188
CellularDevice *dev = new myCellularDevice(&fh1);
188189
EXPECT_TRUE(dev);
189190

190-
CellularStateMachine *stm = ut.create_state_machine(*dev, *dev->get_queue());
191+
CellularStateMachine *stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
191192
EXPECT_TRUE(stm);
192193
ut.set_cellular_callback(&cellular_callback);
193194

@@ -215,14 +216,14 @@ TEST_F(TestCellularStateMachine, test_start_dispatch)
215216
CellularDevice *dev = new myCellularDevice(&fh1);
216217
EXPECT_TRUE(dev);
217218

218-
CellularStateMachine *stm = ut.create_state_machine(*dev, *dev->get_queue());
219+
CellularStateMachine *stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
219220
EXPECT_TRUE(stm);
220221
nsapi_error_t err = ut.start_dispatch();
221222
ASSERT_EQ(NSAPI_ERROR_OK, err);
222223
ut.delete_state_machine();
223224

224225
Thread_stub::osStatus_value = osErrorNoMemory;
225-
stm = ut.create_state_machine(*dev, *dev->get_queue());
226+
stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
226227
EXPECT_TRUE(stm);
227228
err = ut.start_dispatch();
228229
ASSERT_EQ(NSAPI_ERROR_NO_MEMORY, err);
@@ -240,21 +241,21 @@ TEST_F(TestCellularStateMachine, test_stop)
240241
CellularDevice *dev = new AT_CellularDevice(&fh1);
241242
EXPECT_TRUE(dev);
242243

243-
CellularStateMachine *stm = ut.create_state_machine(*dev, *dev->get_queue());
244+
CellularStateMachine *stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
244245
EXPECT_TRUE(stm);
245246

246247
ut.stop(); // nothing created, run through
247248
ut.delete_state_machine();
248249

249-
stm = ut.create_state_machine(*dev, *dev->get_queue());
250+
stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
250251
EXPECT_TRUE(stm);
251252
nsapi_error_t err = ut.start_dispatch();
252253
ASSERT_EQ(NSAPI_ERROR_OK, err);
253254

254255
ut.stop(); // thread is created, now stop will delete it
255256
ut.delete_state_machine();
256257

257-
stm = ut.create_state_machine(*dev, *dev->get_queue());
258+
stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
258259
EXPECT_TRUE(stm);
259260
err = ut.start_dispatch();
260261
ASSERT_EQ(NSAPI_ERROR_OK, err);
@@ -270,7 +271,7 @@ TEST_F(TestCellularStateMachine, test_stop)
270271
ut.stop(); // thread and power are created, now stop will delete them
271272
ut.delete_state_machine();
272273

273-
stm = ut.create_state_machine(*dev, *dev->get_queue());
274+
stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
274275
EXPECT_TRUE(stm);
275276
err = ut.start_dispatch();
276277
ASSERT_EQ(NSAPI_ERROR_OK, err);
@@ -294,7 +295,7 @@ TEST_F(TestCellularStateMachine, test_run_to_state)
294295
CellularDevice *dev = new AT_CellularDevice(&fh1);
295296
EXPECT_TRUE(dev);
296297

297-
CellularStateMachine *stm = ut.create_state_machine(*dev, *dev->get_queue());
298+
CellularStateMachine *stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
298299
EXPECT_TRUE(stm);
299300

300301
nsapi_error_t err = ut.start_dispatch();

UNITTESTS/stubs/CellularStateMachine_stub.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ CellularStubState CellularStateMachine_stub::get_current_target_state = STATE_IN
2525
CellularStubState CellularStateMachine_stub::get_current_current_state = STATE_INIT;
2626
bool CellularStateMachine_stub::bool_value = false;
2727

28-
CellularStateMachine::CellularStateMachine(CellularDevice &device, events::EventQueue &queue) :
29-
_cellularDevice(device), _queue(queue)
28+
CellularStateMachine::CellularStateMachine(CellularDevice &device, events::EventQueue &queue, CellularNetwork &nw) :
29+
_cellularDevice(device), _network(nw), _queue(queue)
3030
{
3131
}
3232

UNITTESTS/stubs/CellularStateMachine_stub.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ enum CellularStubState {
2424
STATE_POWER_ON,
2525
STATE_DEVICE_READY,
2626
STATE_SIM_PIN,
27+
STATE_SIGNAL_QUALITY,
2728
STATE_REGISTERING_NETWORK,
28-
STATE_MANUAL_REGISTERING_NETWORK,
2929
STATE_ATTACHING_NETWORK,
3030
STATE_MAX_FSM_STATE
3131
};

UNITTESTS/target_h/myCellularDevice.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ class myCellularDevice : public CellularDevice {
7272

7373
virtual CellularNetwork *open_network(FileHandle *fh = NULL)
7474
{
75+
if (_network) {
76+
return _network;
77+
}
7578
EventQueue que;
7679
FileHandle_stub fh1;
7780
ATHandler at(&fh1, que, 0, ",");

features/cellular/framework/AT/AT_CellularContext.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ nsapi_error_t AT_CellularContext::check_operation(nsapi_error_t err, ContextOper
196196
tr_warning("No cellular connection");
197197
return NSAPI_ERROR_TIMEOUT;
198198
}
199-
return NSAPI_ERROR_OK;
199+
return _cb_data.error;// callback might have been completed with an error, must return that error here
200200
}
201201
}
202202

@@ -902,7 +902,9 @@ void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr)
902902
if (ev >= NSAPI_EVENT_CELLULAR_STATUS_BASE && ev <= NSAPI_EVENT_CELLULAR_STATUS_END) {
903903
cell_callback_data_t *data = (cell_callback_data_t *)ptr;
904904
cellular_connection_status_t st = (cellular_connection_status_t)ev;
905-
_cb_data.error = data->error;
905+
if (ev != CellularStateRetryEvent) { // retry event may report errors but it's a retry so don't care about the error
906+
_cb_data.error = data->error;
907+
}
906908
#if USE_APN_LOOKUP
907909
if (st == CellularSIMStatusChanged && data->status_data == CellularDevice::SimStateReady &&
908910
_cb_data.error == NSAPI_ERROR_OK) {
@@ -932,11 +934,11 @@ void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr)
932934
}
933935
#endif // USE_APN_LOOKUP
934936

935-
if (!_nw && st == CellularDeviceReady && data->error == NSAPI_ERROR_OK) {
937+
if (!_nw && st == CellularDeviceReady && _cb_data.error == NSAPI_ERROR_OK) {
936938
_nw = _device->open_network(_fh);
937939
}
938940

939-
if (_cp_req && !_cp_in_use && (data->error == NSAPI_ERROR_OK) &&
941+
if (_cp_req && !_cp_in_use && (_cb_data.error == NSAPI_ERROR_OK) &&
940942
(st == CellularSIMStatusChanged && data->status_data == CellularDevice::SimStateReady)) {
941943
if (setup_control_plane_opt() != NSAPI_ERROR_OK) {
942944
tr_error("Control plane SETUP failed!");
@@ -946,7 +948,7 @@ void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr)
946948
}
947949

948950
if (_is_blocking) {
949-
if (data->error != NSAPI_ERROR_OK) {
951+
if (_cb_data.error != NSAPI_ERROR_OK) {
950952
// operation failed, release semaphore
951953
_current_op = OP_INVALID;
952954
_semaphore.release();
@@ -971,7 +973,7 @@ void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr)
971973
}
972974
} else {
973975
// non blocking
974-
if (st == CellularAttachNetwork && _current_op == OP_CONNECT && data->error == NSAPI_ERROR_OK &&
976+
if (st == CellularAttachNetwork && _current_op == OP_CONNECT && _cb_data.error == NSAPI_ERROR_OK &&
975977
data->status_data == CellularNetwork::Attached) {
976978
_current_op = OP_INVALID;
977979
// forward to application

features/cellular/framework/AT/AT_CellularDevice.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ AT_CellularDevice::AT_CellularDevice(FileHandle *fh) : CellularDevice(fh), _netw
4747

4848
AT_CellularDevice::~AT_CellularDevice()
4949
{
50-
delete _state_machine;
51-
5250
// make sure that all is deleted even if somewhere close was not called and reference counting is messed up.
5351
_network_ref_count = 1;
5452
_sms_ref_count = 1;

features/cellular/framework/common/CellularCommon.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,23 @@ struct cell_callback_data_t {
2727
nsapi_error_t error; /* possible error code */
2828
int status_data; /* cellular_event_status related enum or other info in int format. Check cellular_event_status comments.*/
2929
bool final_try; /* This flag is true if state machine is used and this was the last try. State machine does goes to idle. */
30-
30+
const void *data; /* possible extra data in any form. Format specified in cellular_connection_status_t per event if any. */
3131
cell_callback_data_t()
3232
{
3333
error = NSAPI_ERROR_OK;
3434
status_data = -1;
3535
final_try = false;
36+
data = NULL;
37+
}
38+
};
39+
40+
struct cell_signal_quality {
41+
int rssi; /* received signal strength */
42+
int ber; /* channel bit error rate */
43+
cell_signal_quality()
44+
{
45+
rssi = -1;
46+
ber = -1;
3647
}
3748
};
3849

@@ -51,6 +62,9 @@ typedef enum cellular_event_status {
5162
CellularRadioAccessTechnologyChanged = NSAPI_EVENT_CELLULAR_STATUS_BASE + 5, /* Network roaming status have changed. cell_callback_data_t.status_data will be enum RadioAccessTechnology See enum RadioAccessTechnology in ../API/CellularNetwork.h*/
5263
CellularAttachNetwork = NSAPI_EVENT_CELLULAR_STATUS_BASE + 6, /* cell_callback_data_t.status_data will be enum AttachStatus. See enum AttachStatus in ../API/CellularNetwork.h */
5364
CellularActivatePDPContext = NSAPI_EVENT_CELLULAR_STATUS_BASE + 7, /* NSAPI_ERROR_OK in cell_callback_data_t.error on successfully PDP Context activated or negative error */
65+
CellularSignalQuality = NSAPI_EVENT_CELLULAR_STATUS_BASE + 8, /* cell_callback_data_t.error will contains return value when signal quality was queried. data will hold the pointer to cell_signal_quality struct. See possible values from ../API/CellularNetwork.h*/
66+
CellularStateRetryEvent = NSAPI_EVENT_CELLULAR_STATUS_BASE + 9, /* cell_callback_data_t.error contain an error if any. cell_callback_data_t.status_data contains cellular_event_status and it specifies the operation which is retried.
67+
cellular_event_status.data contains current retrycount */
5468
} cellular_connection_status_t;
5569

5670
#endif // CELLULAR_COMMON_

features/cellular/framework/device/CellularDevice.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ CellularDevice::CellularDevice(FileHandle *fh) : _network_ref_count(0), _sms_ref
4444
CellularDevice::~CellularDevice()
4545
{
4646
tr_debug("CellularDevice destruct");
47+
delete _state_machine;
4748
}
4849

4950
void CellularDevice::stop()
@@ -116,9 +117,13 @@ nsapi_error_t CellularDevice::attach_to_network()
116117

117118
nsapi_error_t CellularDevice::create_state_machine()
118119
{
120+
_nw = open_network(_fh);
121+
// Attach to network so we can get update status from the network
122+
_nw->attach(callback(this, &CellularDevice::stm_callback));
123+
119124
nsapi_error_t err = NSAPI_ERROR_OK;
120125
if (!_state_machine) {
121-
_state_machine = new CellularStateMachine(*this, *get_queue());
126+
_state_machine = new CellularStateMachine(*this, *get_queue(), *_nw);
122127
_state_machine->set_cellular_callback(callback(this, &CellularDevice::stm_callback));
123128
err = _state_machine->start_dispatch();
124129
if (err) {
@@ -178,19 +183,17 @@ void CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr, CellularC
178183
{
179184
if (ev >= NSAPI_EVENT_CELLULAR_STATUS_BASE && ev <= NSAPI_EVENT_CELLULAR_STATUS_END) {
180185
cell_callback_data_t *ptr_data = (cell_callback_data_t *)ptr;
181-
tr_debug("callback: %d, err: %d, data: %d", ev, ptr_data->error, ptr_data->status_data);
182186
cellular_connection_status_t cell_ev = (cellular_connection_status_t)ev;
187+
if (cell_ev == CellularStateRetryEvent) {
188+
tr_debug("callback: CellularStateRetryEvent, err: %d, data: %d, retrycount: %d", ptr_data->error, ptr_data->status_data, *(const int *)ptr_data->data);
189+
} else {
190+
tr_debug("callback: %d, err: %d, data: %d", ev, ptr_data->error, ptr_data->status_data);
191+
}
192+
183193
if (cell_ev == CellularRegistrationStatusChanged && _state_machine) {
184194
// broadcast only network registration changes to state machine
185195
_state_machine->cellular_event_changed(ev, ptr);
186196
}
187-
if (cell_ev == CellularDeviceReady && ptr_data->error == NSAPI_ERROR_OK) {
188-
// Here we can create mux and give new filehandles as mux reserves the one what was in use.
189-
// if mux we would need to set new filehandle:_state_machine->set_filehandle( get fh from mux);
190-
_nw = open_network(_fh);
191-
// Attach to network so we can get update status from the network
192-
_nw->attach(callback(this, &CellularDevice::stm_callback));
193-
}
194197
} else {
195198
tr_debug("callback: %d, ptr: %d", ev, ptr);
196199
if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_DISCONNECTED) {

0 commit comments

Comments
 (0)