Skip to content

Commit b9927e5

Browse files
authored
Merge pull request #8795 from TeemuKultala/cellular_connect_status_fix
cellular: registration status change fix
2 parents fa858a5 + e767255 commit b9927e5

File tree

5 files changed

+130
-4
lines changed

5 files changed

+130
-4
lines changed

UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class TestAT_CellularContext : public testing::Test {
5353
ATHandler_stub::bool_value = false;
5454
ATHandler_stub::uint8_value = 0;
5555
ATHandler_stub::fh_value = NULL;
56-
ATHandler_stub::callback = NULL;
5756
ATHandler_stub::call_immediately = false;
5857
ATHandler_stub::resp_info_true_counter = false;
5958
ATHandler_stub::info_elem_true_counter = false;
@@ -63,6 +62,9 @@ class TestAT_CellularContext : public testing::Test {
6362
ATHandler_stub::read_string_table[kRead_string_table_size];
6463
ATHandler_stub::resp_stop_success_count = kResp_stop_count_default;
6564
CellularDevice_stub::connect_counter = 2;
65+
for (int i=0; i < kATHandler_urc_table_max_size; i++) {
66+
ATHandler_stub::callback[i] = NULL;
67+
}
6668
}
6769

6870
void TearDown()

UNITTESTS/features/cellular/framework/AT/at_cellularnetwork/at_cellularnetworktest.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,71 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_registration_params)
211211
EXPECT_TRUE(reg_params_check._cell_id == -1 && reg_params_check._active_time == -1 && reg_params_check._periodic_tau == -1);
212212
}
213213

214+
static int disconnect_cb_count = 0;
215+
static bool disconnect_cb_param_check = false;
216+
static void disconnect_cb(nsapi_event_t ev, intptr_t intptr)
217+
{
218+
disconnect_cb_count++;
219+
220+
if (disconnect_cb_count == 3 && disconnect_cb_param_check) {
221+
EXPECT_TRUE(ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE);
222+
EXPECT_TRUE(intptr == NSAPI_STATUS_DISCONNECTED);
223+
}
224+
}
225+
226+
TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_registration_status_change)
227+
{
228+
EventQueue que;
229+
FileHandle_stub fh1;
230+
ATHandler at(&fh1, que, 0, ",");
231+
232+
AT_CellularNetwork cn(at);
233+
234+
235+
cn.attach(&disconnect_cb);
236+
237+
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
238+
ATHandler_stub::process_oob_urc = true;
239+
240+
ATHandler_stub::read_string_index = 2;
241+
ATHandler_stub::read_string_table[2] = "+CREG:";
242+
ATHandler_stub::read_string_table[1] = "00C3";
243+
ATHandler_stub::read_string_table[0] = "A13F";
244+
ATHandler_stub::int_count = 2;
245+
//ATHandler_stub::int_valid_count_table[2] = 1; //URC status is skipped
246+
ATHandler_stub::int_valid_count_table[1] = 0; //not registered
247+
ATHandler_stub::int_valid_count_table[0] = 1;
248+
at.process_oob();
249+
250+
disconnect_cb_count = 0;
251+
disconnect_cb_param_check = true;
252+
ATHandler_stub::read_string_index = 4;
253+
ATHandler_stub::read_string_table[4] = "+CREG:";
254+
ATHandler_stub::read_string_table[3] = "00C3";
255+
ATHandler_stub::read_string_table[2] = "A13F";
256+
ATHandler_stub::read_string_table[1] = "FF";
257+
ATHandler_stub::read_string_table[0] = "FF";
258+
ATHandler_stub::int_count = 2;
259+
//ATHandler_stub::int_valid_count_table[2] = 1; //URC status is skipped
260+
ATHandler_stub::int_valid_count_table[1] = 1; //registered, home network
261+
ATHandler_stub::int_valid_count_table[0] = 1;
262+
at.process_oob();
263+
264+
ATHandler_stub::read_string_index = 5;
265+
ATHandler_stub::int_count = 3;
266+
ATHandler_stub::read_string_index = 4;
267+
ATHandler_stub::read_string_table[4] = "+CREG:";
268+
ATHandler_stub::read_string_table[3] = "00C3";
269+
ATHandler_stub::read_string_table[2] = "A13F";
270+
ATHandler_stub::read_string_table[1] = "FF";
271+
ATHandler_stub::read_string_table[0] = "FF";
272+
ATHandler_stub::int_count = 2;
273+
//ATHandler_stub::int_valid_count_table[2] = 1; //URC status is skipped
274+
ATHandler_stub::int_valid_count_table[1] = 4; //unknown registration status
275+
ATHandler_stub::int_valid_count_table[0] = 1;
276+
at.process_oob();
277+
}
278+
214279
TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_network_registering_mode)
215280
{
216281
EventQueue que;

UNITTESTS/stubs/ATHandler_stub.cpp

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,34 @@ bool ATHandler_stub::bool_value = false;
4343
uint8_t ATHandler_stub::uint8_value = 0;
4444
FileHandle_stub *ATHandler_stub::fh_value = NULL;
4545
device_err_t ATHandler_stub::device_err_value;
46-
Callback<void()> ATHandler_stub::callback = NULL;
4746
bool ATHandler_stub::call_immediately = false;
4847
uint8_t ATHandler_stub::resp_info_true_counter = false;
4948
uint8_t ATHandler_stub::info_elem_true_counter = false;
5049
int ATHandler_stub::int_valid_count_table[kRead_int_table_size];
5150
int ATHandler_stub::int_count = kRead_int_table_size;
51+
bool ATHandler_stub::process_oob_urc = false;
5252

5353
int ATHandler_stub::read_string_index = kRead_string_table_size;
5454
const char *ATHandler_stub::read_string_table[kRead_string_table_size];
5555
int ATHandler_stub::resp_stop_success_count = kResp_stop_count_default;
56+
int ATHandler_stub::urc_amount = 0;
57+
mbed::Callback<void()> ATHandler_stub::callback[kATHandler_urc_table_max_size];
58+
char *ATHandler_stub::urc_string_table[kATHandler_urc_table_max_size];
5659

5760
ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char *output_delimiter, uint16_t send_delay) :
5861
_nextATHandler(0),
5962
_fileHandle(fh),
6063
_queue(queue)
6164
{
6265
ATHandler_stub::ref_count = 1;
66+
67+
ATHandler_stub::process_oob_urc = false;
68+
ATHandler_stub::urc_amount = 0;
69+
int i = 0;
70+
while (i < kATHandler_urc_table_max_size) {
71+
ATHandler_stub::callback[i] = NULL;
72+
ATHandler_stub::urc_string_table[i++] = NULL;
73+
}
6374
}
6475

6576
void ATHandler::set_debug(bool debug_on)
@@ -70,6 +81,16 @@ void ATHandler::set_debug(bool debug_on)
7081
ATHandler::~ATHandler()
7182
{
7283
ATHandler_stub::ref_count = kATHandler_destructor_ref_ount;
84+
85+
int i = 0;
86+
while (i < kATHandler_urc_table_max_size) {
87+
if (ATHandler_stub::urc_string_table[i]) {
88+
delete [] ATHandler_stub::urc_string_table[i];
89+
i++;
90+
} else {
91+
break;
92+
}
93+
}
7394
}
7495

7596
void ATHandler::inc_ref_count()
@@ -98,7 +119,18 @@ void ATHandler::set_file_handle(FileHandle *fh)
98119

99120
nsapi_error_t ATHandler::set_urc_handler(const char *urc, mbed::Callback<void()> cb)
100121
{
101-
ATHandler_stub::callback = cb;
122+
if (ATHandler_stub::urc_amount < kATHandler_urc_table_max_size) {
123+
ATHandler_stub::callback[ATHandler_stub::urc_amount] = cb;
124+
ATHandler_stub::urc_string_table[ATHandler_stub::urc_amount] = new char[kATHandler_urc_string_max_size];
125+
if (urc) {
126+
int bytes_to_copy = strlen(urc) < kATHandler_urc_string_max_size ? strlen(urc) : kATHandler_urc_string_max_size;
127+
memcpy(ATHandler_stub::urc_string_table[ATHandler_stub::urc_amount], urc, bytes_to_copy);
128+
}
129+
ATHandler_stub::urc_amount++;
130+
} else {
131+
ATHandler_stub::callback[0] = cb;
132+
MBED_ASSERT("ATHandler URC amount limit reached");
133+
}
102134
if (ATHandler_stub::call_immediately) {
103135
cb();
104136
}
@@ -143,6 +175,20 @@ void ATHandler::restore_at_timeout()
143175

144176
void ATHandler::process_oob()
145177
{
178+
if (ATHandler_stub::process_oob_urc) {
179+
int i = 0;
180+
while (i < ATHandler_stub::urc_amount) {
181+
if (ATHandler_stub::read_string_index >= 0) {
182+
if (!memcmp(ATHandler_stub::urc_string_table[i],
183+
ATHandler_stub::read_string_table[ATHandler_stub::read_string_index],
184+
strlen(ATHandler_stub::urc_string_table[i]))) {
185+
ATHandler_stub::callback[i]();
186+
break;
187+
}
188+
}
189+
i++;
190+
}
191+
}
146192
}
147193

148194
void ATHandler::clear_error()

UNITTESTS/stubs/ATHandler_stub.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ static const int kRead_int_table_size = 100;
3131
static const int kResp_stop_count_default = 100;
3232
// set reference count to -909 to separate it from zero so we can test that ATHandler is really deleted.
3333
static const int kATHandler_destructor_ref_ount = -909;
34+
static const int kATHandler_urc_table_max_size = 10;
35+
static const int kATHandler_urc_string_max_size = 16;
3436

3537
namespace ATHandler_stub {
3638
extern nsapi_error_t nsapi_error_value;
@@ -50,13 +52,16 @@ extern uint8_t info_elem_true_counter;
5052
extern uint8_t uint8_value;
5153
extern mbed::FileHandle_stub *fh_value;
5254
extern mbed::device_err_t device_err_value;
53-
extern mbed::Callback<void()> callback;
55+
extern mbed::Callback<void()> callback[kATHandler_urc_table_max_size];
5456
extern bool call_immediately;
5557
extern const char *read_string_table[kRead_string_table_size];
5658
extern int read_string_index;
5759
extern int int_valid_count_table[kRead_int_table_size];
5860
extern int int_count;
5961
extern int resp_stop_success_count;
62+
extern bool process_oob_urc;
63+
extern int urc_amount;
64+
extern char *urc_string_table[kATHandler_urc_table_max_size];
6065
}
6166

6267
#endif

features/cellular/framework/AT/AT_CellularNetwork.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,17 @@ void AT_CellularNetwork::read_reg_params_and_compare(RegistrationType type)
161161
_connection_status_cb((nsapi_event_t)CellularRadioAccessTechnologyChanged, (intptr_t)&data);
162162
}
163163
if (reg_params._status != _reg_params._status) {
164+
RegistrationStatus previous_registration_status = _reg_params._status;
164165
_reg_params._status = reg_params._status;
165166
data.status_data = reg_params._status;
166167
_connection_status_cb((nsapi_event_t)CellularRegistrationStatusChanged, (intptr_t)&data);
168+
if (!(reg_params._status == RegisteredHomeNetwork ||
169+
reg_params._status == RegisteredRoaming)) {
170+
if (previous_registration_status == RegisteredHomeNetwork ||
171+
previous_registration_status == RegisteredRoaming) {
172+
_connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED);
173+
}
174+
}
167175
}
168176
if (reg_params._cell_id != -1 && reg_params._cell_id != _reg_params._cell_id) {
169177
_reg_params._cell_id = reg_params._cell_id;

0 commit comments

Comments
 (0)