Cellular: fix unit test valgrind warnings

Stub files did have some memory leaks and using
unintialized variables.
pull/10623/head
Teppo Järvelin 2019-05-21 14:23:21 +03:00
parent 344ad4372c
commit d559338b3b
5 changed files with 72 additions and 68 deletions

View File

@ -62,9 +62,6 @@ protected:
ATHandler_stub::read_string_table[kRead_string_table_size]; ATHandler_stub::read_string_table[kRead_string_table_size];
ATHandler_stub::resp_stop_success_count = kResp_stop_count_default; ATHandler_stub::resp_stop_success_count = kResp_stop_count_default;
CellularDevice_stub::connect_counter = 2; CellularDevice_stub::connect_counter = 2;
for (int i=0; i < kATHandler_urc_table_max_size; i++) {
ATHandler_stub::callback[i] = NULL;
}
} }
void TearDown() void TearDown()

View File

@ -567,6 +567,9 @@ TEST_F(Test_LoRaMac, clear_tx_pipe)
conn.connection_u.otaa.nb_trials = 2; conn.connection_u.otaa.nb_trials = 2;
object->prepare_join(&conn, true); object->prepare_join(&conn, true);
channel_params_t params[] = {868300000, 0, { ((DR_5 << 4) | DR_0) }, 1};
LoRaPHY_stub::channel_params_ptr = params;
LoRaWANTimer_stub::call_cb_immediately = true;
EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize(NULL, my_cb)); EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize(NULL, my_cb));
EventQueue_stub::int_value = 0; EventQueue_stub::int_value = 0;
EXPECT_EQ(LORAWAN_STATUS_BUSY, object->clear_tx_pipe()); EXPECT_EQ(LORAWAN_STATUS_BUSY, object->clear_tx_pipe());

View File

@ -17,7 +17,6 @@
#include <ctype.h> #include <ctype.h>
#include "nsapi_types.h" #include "nsapi_types.h"
#include "ATHandler.h"
#include "events/EventQueue.h" #include "events/EventQueue.h"
#include "ATHandler_stub.h" #include "ATHandler_stub.h"
@ -27,6 +26,7 @@ using namespace events;
#include "CellularLog.h" #include "CellularLog.h"
const int DEFAULT_AT_TIMEOUT = 1000; // at default timeout in milliseconds const int DEFAULT_AT_TIMEOUT = 1000; // at default timeout in milliseconds
const uint8_t MAX_RESP_LENGTH = 7;
nsapi_error_t ATHandler_stub::nsapi_error_value = 0; nsapi_error_t ATHandler_stub::nsapi_error_value = 0;
uint8_t ATHandler_stub::nsapi_error_ok_counter = 0; uint8_t ATHandler_stub::nsapi_error_ok_counter = 0;
@ -55,9 +55,6 @@ bool ATHandler_stub::process_oob_urc = false;
int ATHandler_stub::read_string_index = kRead_string_table_size; int ATHandler_stub::read_string_index = kRead_string_table_size;
const char *ATHandler_stub::read_string_table[kRead_string_table_size] = {'\0'}; const char *ATHandler_stub::read_string_table[kRead_string_table_size] = {'\0'};
int ATHandler_stub::resp_stop_success_count = kResp_stop_count_default; int ATHandler_stub::resp_stop_success_count = kResp_stop_count_default;
int ATHandler_stub::urc_amount = 0;
mbed::Callback<void()> ATHandler_stub::callback[kATHandler_urc_table_max_size];
char *ATHandler_stub::urc_string_table[kATHandler_urc_table_max_size] = {NULL};
bool ATHandler_stub::get_debug_flag = false; bool ATHandler_stub::get_debug_flag = false;
uint8_t ATHandler_stub::set_debug_call_count = 0; uint8_t ATHandler_stub::set_debug_call_count = 0;
@ -86,17 +83,14 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, uint32_t timeout, const
_nextATHandler(0), _nextATHandler(0),
_fileHandle(fh), _fileHandle(fh),
_queue(queue), _queue(queue),
_ref_count(1) _ref_count(1),
_oob_string_max_length(0),
_oobs(NULL),
_max_resp_length(MAX_RESP_LENGTH)
{ {
ATHandler_stub::ref_count = 1; ATHandler_stub::ref_count = 1;
ATHandler_stub::process_oob_urc = false; ATHandler_stub::process_oob_urc = false;
ATHandler_stub::urc_amount = 0;
int i = 0;
while (i < kATHandler_urc_table_max_size) {
ATHandler_stub::callback[i] = NULL;
ATHandler_stub::urc_string_table[i++] = NULL;
}
} }
void ATHandler::set_debug(bool debug_on) void ATHandler::set_debug(bool debug_on)
@ -115,15 +109,10 @@ bool ATHandler::get_debug() const
ATHandler::~ATHandler() ATHandler::~ATHandler()
{ {
ATHandler_stub::ref_count = kATHandler_destructor_ref_ount; ATHandler_stub::ref_count = kATHandler_destructor_ref_ount;
while (_oobs) {
int i = 0; struct oob_t *oob = _oobs;
while (i < kATHandler_urc_table_max_size) { _oobs = oob->next;
if (ATHandler_stub::urc_string_table[i]) { delete oob;
delete [] ATHandler_stub::urc_string_table[i];
i++;
} else {
break;
}
} }
} }
@ -154,6 +143,19 @@ void ATHandler::set_file_handle(FileHandle *fh)
{ {
} }
bool ATHandler::find_urc_handler(const char *prefix)
{
struct oob_t *oob = _oobs;
while (oob) {
if (strcmp(prefix, oob->prefix) == 0) {
return true;
}
oob = oob->next;
}
return false;
}
void ATHandler::set_urc_handler(const char *urc, mbed::Callback<void()> cb) void ATHandler::set_urc_handler(const char *urc, mbed::Callback<void()> cb)
{ {
if (!cb) { if (!cb) {
@ -161,19 +163,25 @@ void ATHandler::set_urc_handler(const char *urc, mbed::Callback<void()> cb)
return; return;
} }
if (ATHandler_stub::urc_amount < kATHandler_urc_table_max_size) { if (find_urc_handler(urc)) {
ATHandler_stub::callback[ATHandler_stub::urc_amount] = cb; return;
if (urc) {
ATHandler_stub::urc_string_table[ATHandler_stub::urc_amount] = new char[kATHandler_urc_string_max_size];
memset(ATHandler_stub::urc_string_table[ATHandler_stub::urc_amount], 0, sizeof(ATHandler_stub::urc_string_table[ATHandler_stub::urc_amount]));
int bytes_to_copy = strlen(urc) < kATHandler_urc_string_max_size ? strlen(urc) : kATHandler_urc_string_max_size;
memcpy(ATHandler_stub::urc_string_table[ATHandler_stub::urc_amount], urc, bytes_to_copy);
} }
ATHandler_stub::urc_amount++;
} else { struct oob_t *oob = new struct oob_t;
ATHandler_stub::callback[0] = cb; size_t prefix_len = strlen(urc);
MBED_ASSERT("ATHandler URC amount limit reached"); if (prefix_len > _oob_string_max_length) {
_oob_string_max_length = prefix_len;
if (_oob_string_max_length > _max_resp_length) {
_max_resp_length = _oob_string_max_length;
} }
}
oob->prefix = urc;
oob->prefix_len = prefix_len;
oob->cb = cb;
oob->next = _oobs;
_oobs = oob;
if (ATHandler_stub::call_immediately) { if (ATHandler_stub::call_immediately) {
cb(); cb();
} }
@ -181,19 +189,20 @@ void ATHandler::set_urc_handler(const char *urc, mbed::Callback<void()> cb)
void ATHandler::remove_urc_handler(const char *prefix) void ATHandler::remove_urc_handler(const char *prefix)
{ {
bool found_urc = false; struct oob_t *current = _oobs;
for (int i = 0; i < ATHandler_stub::urc_amount; i++) { struct oob_t *prev = NULL;
if (found_urc && i < 0) { while (current) {
ATHandler_stub::urc_string_table[i - 1] = ATHandler_stub::urc_string_table[i]; if (strcmp(prefix, current->prefix) == 0) {
ATHandler_stub::urc_string_table[i] = 0; if (prev) {
} else if (ATHandler_stub::urc_string_table[i] && strcmp(prefix, ATHandler_stub::urc_string_table[i]) == 0) { prev->next = current->next;
delete [] ATHandler_stub::urc_string_table[i]; } else {
ATHandler_stub::urc_string_table[i] = 0; _oobs = current->next;
found_urc = true;
} }
delete current;
break;
} }
if (found_urc) { prev = current;
ATHandler_stub::urc_amount--; current = prev->next;
} }
} }
@ -232,22 +241,14 @@ void ATHandler::restore_at_timeout()
void ATHandler::process_oob() void ATHandler::process_oob()
{ {
if (ATHandler_stub::process_oob_urc) { if (ATHandler_stub::process_oob_urc) {
int i = 0; size_t prefix_len = 0;
while (i < ATHandler_stub::urc_amount) { for (struct oob_t *oob = _oobs; oob; oob = oob->next) {
if (ATHandler_stub::read_string_index >= 0) { prefix_len = oob->prefix_len;
int len = 0; if (!memcmp(oob->prefix, ATHandler_stub::read_string_table[ATHandler_stub::read_string_index], prefix_len)) {
if (ATHandler_stub::urc_string_table[i]) { oob->cb();
len = strlen(ATHandler_stub::urc_string_table[i]);
}
if (!memcmp(ATHandler_stub::urc_string_table[i],
ATHandler_stub::read_string_table[ATHandler_stub::read_string_index],
len)) {
ATHandler_stub::callback[i]();
break; break;
} }
} }
i++;
}
} }
} }

View File

@ -56,7 +56,6 @@ extern uint8_t info_elem_true_counter;
extern uint8_t uint8_value; extern uint8_t uint8_value;
extern mbed::FileHandle_stub *fh_value; extern mbed::FileHandle_stub *fh_value;
extern mbed::device_err_t device_err_value; extern mbed::device_err_t device_err_value;
extern mbed::Callback<void()> callback[kATHandler_urc_table_max_size];
extern bool call_immediately; extern bool call_immediately;
extern const char *read_string_table[kRead_string_table_size]; extern const char *read_string_table[kRead_string_table_size];
extern int read_string_index; extern int read_string_index;
@ -64,8 +63,6 @@ extern int int_valid_count_table[kRead_int_table_size];
extern int int_count; extern int int_count;
extern int resp_stop_success_count; extern int resp_stop_success_count;
extern bool process_oob_urc; extern bool process_oob_urc;
extern int urc_amount;
extern char *urc_string_table[kATHandler_urc_table_max_size];
extern bool get_debug_flag; extern bool get_debug_flag;
bool is_get_debug_run(); bool is_get_debug_run();

View File

@ -38,7 +38,7 @@ AT_CellularDevice::AT_CellularDevice(FileHandle *fh) : CellularDevice(fh), _netw
AT_CellularDevice::~AT_CellularDevice() AT_CellularDevice::~AT_CellularDevice()
{ {
delete _network; close_network();
} }
ATHandler *AT_CellularDevice::get_at_handler(FileHandle *fileHandle) ATHandler *AT_CellularDevice::get_at_handler(FileHandle *fileHandle)
@ -75,6 +75,9 @@ void delete_context(CellularContext *context)
CellularNetwork *AT_CellularDevice::open_network(FileHandle *fh) CellularNetwork *AT_CellularDevice::open_network(FileHandle *fh)
{ {
if (_network) {
return _network;
}
_network = new AT_CellularNetwork(*ATHandler::get_instance(fh, _network = new AT_CellularNetwork(*ATHandler::get_instance(fh,
_queue, _queue,
_default_timeout, _default_timeout,
@ -96,9 +99,12 @@ CellularInformation *AT_CellularDevice::open_information(FileHandle *fh)
void AT_CellularDevice::close_network() void AT_CellularDevice::close_network()
{ {
if (_network) {
ATHandler *atHandler = &_network->get_at_handler();
delete _network; delete _network;
_network = NULL; _network = NULL;
release_at_handler(atHandler);
}
} }
void AT_CellularDevice::close_sms() void AT_CellularDevice::close_sms()