Skip to content

Commit 516a214

Browse files
authored
Merge pull request #10623 from jarvte/fix_cellular_valgrind_ut
Cellular: fix unit test valgrind warnings
2 parents 06cf787 + d559338 commit 516a214

File tree

5 files changed

+71
-67
lines changed

5 files changed

+71
-67
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ class TestAT_CellularContext : public testing::Test {
6262
ATHandler_stub::read_string_table[kRead_string_table_size];
6363
ATHandler_stub::resp_stop_success_count = kResp_stop_count_default;
6464
CellularDevice_stub::connect_counter = 2;
65-
for (int i=0; i < kATHandler_urc_table_max_size; i++) {
66-
ATHandler_stub::callback[i] = NULL;
67-
}
6865
}
6966

7067
void TearDown()

UNITTESTS/features/lorawan/loramac/Test_LoRaMac.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,9 @@ TEST_F(Test_LoRaMac, clear_tx_pipe)
567567
conn.connection_u.otaa.nb_trials = 2;
568568
object->prepare_join(&conn, true);
569569

570+
channel_params_t params[] = {868300000, 0, { ((DR_5 << 4) | DR_0) }, 1};
571+
LoRaPHY_stub::channel_params_ptr = params;
572+
LoRaWANTimer_stub::call_cb_immediately = true;
570573
EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize(NULL, my_cb));
571574
EventQueue_stub::int_value = 0;
572575
EXPECT_EQ(LORAWAN_STATUS_BUSY, object->clear_tx_pipe());

UNITTESTS/stubs/ATHandler_stub.cpp

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#include <ctype.h>
1919
#include "nsapi_types.h"
20-
#include "ATHandler.h"
2120
#include "events/EventQueue.h"
2221
#include "ATHandler_stub.h"
2322

@@ -27,6 +26,7 @@ using namespace events;
2726
#include "CellularLog.h"
2827

2928
const int DEFAULT_AT_TIMEOUT = 1000; // at default timeout in milliseconds
29+
const uint8_t MAX_RESP_LENGTH = 7;
3030

3131
nsapi_error_t ATHandler_stub::nsapi_error_value = 0;
3232
uint8_t ATHandler_stub::nsapi_error_ok_counter = 0;
@@ -55,9 +55,6 @@ bool ATHandler_stub::process_oob_urc = false;
5555
int ATHandler_stub::read_string_index = kRead_string_table_size;
5656
const char *ATHandler_stub::read_string_table[kRead_string_table_size] = {'\0'};
5757
int ATHandler_stub::resp_stop_success_count = kResp_stop_count_default;
58-
int ATHandler_stub::urc_amount = 0;
59-
mbed::Callback<void()> ATHandler_stub::callback[kATHandler_urc_table_max_size];
60-
char *ATHandler_stub::urc_string_table[kATHandler_urc_table_max_size] = {NULL};
6158

6259
bool ATHandler_stub::get_debug_flag = false;
6360
uint8_t ATHandler_stub::set_debug_call_count = 0;
@@ -86,17 +83,14 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, uint32_t timeout, const
8683
_nextATHandler(0),
8784
_fileHandle(fh),
8885
_queue(queue),
89-
_ref_count(1)
86+
_ref_count(1),
87+
_oob_string_max_length(0),
88+
_oobs(NULL),
89+
_max_resp_length(MAX_RESP_LENGTH)
9090
{
9191
ATHandler_stub::ref_count = 1;
9292

9393
ATHandler_stub::process_oob_urc = false;
94-
ATHandler_stub::urc_amount = 0;
95-
int i = 0;
96-
while (i < kATHandler_urc_table_max_size) {
97-
ATHandler_stub::callback[i] = NULL;
98-
ATHandler_stub::urc_string_table[i++] = NULL;
99-
}
10094
}
10195

10296
void ATHandler::set_debug(bool debug_on)
@@ -115,15 +109,10 @@ bool ATHandler::get_debug() const
115109
ATHandler::~ATHandler()
116110
{
117111
ATHandler_stub::ref_count = kATHandler_destructor_ref_ount;
118-
119-
int i = 0;
120-
while (i < kATHandler_urc_table_max_size) {
121-
if (ATHandler_stub::urc_string_table[i]) {
122-
delete [] ATHandler_stub::urc_string_table[i];
123-
i++;
124-
} else {
125-
break;
126-
}
112+
while (_oobs) {
113+
struct oob_t *oob = _oobs;
114+
_oobs = oob->next;
115+
delete oob;
127116
}
128117
}
129118

@@ -154,46 +143,66 @@ void ATHandler::set_file_handle(FileHandle *fh)
154143
{
155144
}
156145

146+
bool ATHandler::find_urc_handler(const char *prefix)
147+
{
148+
struct oob_t *oob = _oobs;
149+
while (oob) {
150+
if (strcmp(prefix, oob->prefix) == 0) {
151+
return true;
152+
}
153+
oob = oob->next;
154+
}
155+
156+
return false;
157+
}
158+
157159
void ATHandler::set_urc_handler(const char *urc, mbed::Callback<void()> cb)
158160
{
159161
if (!cb) {
160162
remove_urc_handler(urc);
161163
return;
162164
}
163165

164-
if (ATHandler_stub::urc_amount < kATHandler_urc_table_max_size) {
165-
ATHandler_stub::callback[ATHandler_stub::urc_amount] = cb;
166-
if (urc) {
167-
ATHandler_stub::urc_string_table[ATHandler_stub::urc_amount] = new char[kATHandler_urc_string_max_size];
168-
memset(ATHandler_stub::urc_string_table[ATHandler_stub::urc_amount], 0, sizeof(ATHandler_stub::urc_string_table[ATHandler_stub::urc_amount]));
169-
int bytes_to_copy = strlen(urc) < kATHandler_urc_string_max_size ? strlen(urc) : kATHandler_urc_string_max_size;
170-
memcpy(ATHandler_stub::urc_string_table[ATHandler_stub::urc_amount], urc, bytes_to_copy);
166+
if (find_urc_handler(urc)) {
167+
return;
168+
}
169+
170+
struct oob_t *oob = new struct oob_t;
171+
size_t prefix_len = strlen(urc);
172+
if (prefix_len > _oob_string_max_length) {
173+
_oob_string_max_length = prefix_len;
174+
if (_oob_string_max_length > _max_resp_length) {
175+
_max_resp_length = _oob_string_max_length;
171176
}
172-
ATHandler_stub::urc_amount++;
173-
} else {
174-
ATHandler_stub::callback[0] = cb;
175-
MBED_ASSERT("ATHandler URC amount limit reached");
176177
}
178+
179+
oob->prefix = urc;
180+
oob->prefix_len = prefix_len;
181+
oob->cb = cb;
182+
oob->next = _oobs;
183+
_oobs = oob;
184+
177185
if (ATHandler_stub::call_immediately) {
178186
cb();
179187
}
180188
}
181189

182190
void ATHandler::remove_urc_handler(const char *prefix)
183191
{
184-
bool found_urc = false;
185-
for (int i = 0; i < ATHandler_stub::urc_amount; i++) {
186-
if (found_urc && i < 0) {
187-
ATHandler_stub::urc_string_table[i - 1] = ATHandler_stub::urc_string_table[i];
188-
ATHandler_stub::urc_string_table[i] = 0;
189-
} else if (ATHandler_stub::urc_string_table[i] && strcmp(prefix, ATHandler_stub::urc_string_table[i]) == 0) {
190-
delete [] ATHandler_stub::urc_string_table[i];
191-
ATHandler_stub::urc_string_table[i] = 0;
192-
found_urc = true;
192+
struct oob_t *current = _oobs;
193+
struct oob_t *prev = NULL;
194+
while (current) {
195+
if (strcmp(prefix, current->prefix) == 0) {
196+
if (prev) {
197+
prev->next = current->next;
198+
} else {
199+
_oobs = current->next;
200+
}
201+
delete current;
202+
break;
193203
}
194-
}
195-
if (found_urc) {
196-
ATHandler_stub::urc_amount--;
204+
prev = current;
205+
current = prev->next;
197206
}
198207
}
199208

@@ -232,21 +241,13 @@ void ATHandler::restore_at_timeout()
232241
void ATHandler::process_oob()
233242
{
234243
if (ATHandler_stub::process_oob_urc) {
235-
int i = 0;
236-
while (i < ATHandler_stub::urc_amount) {
237-
if (ATHandler_stub::read_string_index >= 0) {
238-
int len = 0;
239-
if (ATHandler_stub::urc_string_table[i]) {
240-
len = strlen(ATHandler_stub::urc_string_table[i]);
241-
}
242-
if (!memcmp(ATHandler_stub::urc_string_table[i],
243-
ATHandler_stub::read_string_table[ATHandler_stub::read_string_index],
244-
len)) {
245-
ATHandler_stub::callback[i]();
246-
break;
247-
}
244+
size_t prefix_len = 0;
245+
for (struct oob_t *oob = _oobs; oob; oob = oob->next) {
246+
prefix_len = oob->prefix_len;
247+
if (!memcmp(oob->prefix, ATHandler_stub::read_string_table[ATHandler_stub::read_string_index], prefix_len)) {
248+
oob->cb();
249+
break;
248250
}
249-
i++;
250251
}
251252
}
252253
}

UNITTESTS/stubs/ATHandler_stub.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,13 @@ extern uint8_t info_elem_true_counter;
5656
extern uint8_t uint8_value;
5757
extern mbed::FileHandle_stub *fh_value;
5858
extern mbed::device_err_t device_err_value;
59-
extern mbed::Callback<void()> callback[kATHandler_urc_table_max_size];
6059
extern bool call_immediately;
6160
extern const char *read_string_table[kRead_string_table_size];
6261
extern int read_string_index;
6362
extern int int_valid_count_table[kRead_int_table_size];
6463
extern int int_count;
6564
extern int resp_stop_success_count;
6665
extern bool process_oob_urc;
67-
extern int urc_amount;
68-
extern char *urc_string_table[kATHandler_urc_table_max_size];
6966

7067
extern bool get_debug_flag;
7168
bool is_get_debug_run();

UNITTESTS/stubs/AT_CellularDevice_stub.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ AT_CellularDevice::AT_CellularDevice(FileHandle *fh) : CellularDevice(fh), _netw
3838

3939
AT_CellularDevice::~AT_CellularDevice()
4040
{
41-
delete _network;
41+
close_network();
4242
}
4343

4444
ATHandler *AT_CellularDevice::get_at_handler(FileHandle *fileHandle)
@@ -75,6 +75,9 @@ void delete_context(CellularContext *context)
7575

7676
CellularNetwork *AT_CellularDevice::open_network(FileHandle *fh)
7777
{
78+
if (_network) {
79+
return _network;
80+
}
7881
_network = new AT_CellularNetwork(*ATHandler::get_instance(fh,
7982
_queue,
8083
_default_timeout,
@@ -96,9 +99,12 @@ CellularInformation *AT_CellularDevice::open_information(FileHandle *fh)
9699

97100
void AT_CellularDevice::close_network()
98101
{
99-
delete _network;
100-
101-
_network = NULL;
102+
if (_network) {
103+
ATHandler *atHandler = &_network->get_at_handler();
104+
delete _network;
105+
_network = NULL;
106+
release_at_handler(atHandler);
107+
}
102108
}
103109

104110
void AT_CellularDevice::close_sms()

0 commit comments

Comments
 (0)