Skip to content

At handler fixes #8401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ TEST_F(TestATHandler, test_ATHandler_remove_urc_handler)
at.set_urc_handler(ch, cb);

//This does nothing!!!
at.remove_urc_handler(ch, cb);
at.remove_urc_handler(ch);
}

TEST_F(TestATHandler, test_ATHandler_get_last_error)
Expand Down Expand Up @@ -460,7 +460,7 @@ TEST_F(TestATHandler, test_ATHandler_skip_param)
fh1.short_value = POLLIN;
at.resp_start();
at.skip_param();
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_DEVICE_ERROR);
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_OK);

char table1[] = "ss,sssssssssssss,sssssssssssOK\r\n\0";
filehandle_stub_table = table1;
Expand Down Expand Up @@ -935,15 +935,88 @@ TEST_F(TestATHandler, test_ATHandler_resp_start)
filehandle_stub_table_pos = 0;
at.resp_start();

char table7[] = "ssssss\0";
char table7[] = "urc: info\r\nresponseOK\r\n\0";
at.flush();
at.clear_error();
filehandle_stub_table = table7;
filehandle_stub_table_pos = 0;

at.set_urc_handler("urc: ", NULL);
at.resp_start(); // recv_buff: "responseOK\r\n\0"
at.resp_stop(); // consumes to OKCRLF -> OK
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_OK);

char table8[] = "urc: info\r\nresponse\0";
at.flush();
at.clear_error();
filehandle_stub_table = table8;
filehandle_stub_table_pos = 0;

at.set_urc_handler("urc: ", NULL);
at.resp_start();
at.resp_stop();
// No stop tag(OKCRLF) found
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_DEVICE_ERROR);

char table9[] = "urc: prefix: infoOK\r\n\0";
at.flush();
at.clear_error();
at.set_urc_handler("ss", NULL);
filehandle_stub_table = table9;
filehandle_stub_table_pos = 0;

at.set_urc_handler("urc: ", NULL);
at.resp_start();
// Match URC consumes to CRLF -> nothing to read after that -> ERROR
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_DEVICE_ERROR);

char table10[] = "urc: info\r\ngarbage\r\nprefix: info\r\nOK\r\n\0";
at.flush();
at.clear_error();
filehandle_stub_table = table10;
filehandle_stub_table_pos = 0;

at.set_urc_handler("urc: ", NULL);
at.resp_start("prefix"); // match URC -> consumes to first CRLF -> consumes the garbage because there is expected prefix and no match found -> then prefix match
at.resp_stop(); //ends the info scope -> consumes to CRLF -> ends the resp scope -> consumes to OKCRLF
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_OK);

// No stop tag(OKCRLF) will be found because, after match URC consumed everything to CRLF, rest of buffer
// is consumed to next/last CRLF because there is expected prefix and no match found
// -> nothing to read after that -> ERROR
char table11[] = "urc: info\r\ngarbageprefix: infoOK\r\n\0";
at.flush();
at.clear_error();
filehandle_stub_table = table11;
filehandle_stub_table_pos = 0;

at.set_urc_handler("urc: ", NULL);
at.resp_start("prefix");
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_DEVICE_ERROR);

// After URC match no prefix match -> try to read more -> no more to read
char table12[] = "urc: infoprefix: info\0";
at.flush();
at.clear_error();
filehandle_stub_table = table12;
filehandle_stub_table_pos = 0;

at.set_urc_handler("urc: ", NULL);
at.resp_start("prefix");
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_DEVICE_ERROR);

// Will run into mem_str check of identical strings
char table13[] = "\r\n\r\n\0";
at.flush();
at.clear_error();
filehandle_stub_table = table13;
filehandle_stub_table_pos = 0;

char buf[3];
at.resp_start();
EXPECT_TRUE(2 == at.read_string(buf, 3));
EXPECT_TRUE(!strncmp(buf, "\r\n", 2));
// Consume to delimiter or stop_tag OKCRLF fails -> ERROR
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_DEVICE_ERROR);
}

TEST_F(TestATHandler, test_ATHandler_resp_stop)
Expand Down
2 changes: 1 addition & 1 deletion UNITTESTS/stubs/ATHandler_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ nsapi_error_t ATHandler::set_urc_handler(const char *urc, mbed::Callback<void()>
return ATHandler_stub::nsapi_error_value;
}

void ATHandler::remove_urc_handler(const char *prefix, mbed::Callback<void()> callback)
void ATHandler::remove_urc_handler(const char *prefix)
{
}

Expand Down
2 changes: 1 addition & 1 deletion UNITTESTS/stubs/FileHandle_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class FileHandle_stub : public FileHandle {
if (size < ret) {
ret = size;
}
memcpy(buffer, filehandle_stub_table, ret);
memcpy(buffer, filehandle_stub_table + filehandle_stub_table_pos, ret);
filehandle_stub_table_pos += ret;
return ret;
}
Expand Down
25 changes: 19 additions & 6 deletions features/cellular/framework/AT/ATHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char
_max_resp_length(MAX_RESP_LENGTH),
_debug_on(MBED_CONF_CELLULAR_DEBUG_AT),
_cmd_start(false),
_use_delimiter(true),
_start_time(0)
{
clear_error();
Expand Down Expand Up @@ -159,7 +160,7 @@ void ATHandler::set_is_filehandle_usable(bool usable)

nsapi_error_t ATHandler::set_urc_handler(const char *prefix, mbed::Callback<void()> callback)
{
if (find_urc_handler(prefix, &callback)) {
if (find_urc_handler(prefix)) {
tr_warn("URC already added with prefix: %s", prefix);
return NSAPI_ERROR_OK;
}
Expand All @@ -186,12 +187,12 @@ nsapi_error_t ATHandler::set_urc_handler(const char *prefix, mbed::Callback<void
return NSAPI_ERROR_OK;
}

void ATHandler::remove_urc_handler(const char *prefix, mbed::Callback<void()> callback)
void ATHandler::remove_urc_handler(const char *prefix)
{
struct oob_t *current = _oobs;
struct oob_t *prev = NULL;
while (current) {
if (strcmp(prefix, current->prefix) == 0 && current->cb == callback) {
if (strcmp(prefix, current->prefix) == 0) {
if (prev) {
prev->next = current->next;
} else {
Expand All @@ -205,11 +206,11 @@ void ATHandler::remove_urc_handler(const char *prefix, mbed::Callback<void()> ca
}
}

bool ATHandler::find_urc_handler(const char *prefix, mbed::Callback<void()> *callback)
bool ATHandler::find_urc_handler(const char *prefix)
{
struct oob_t *oob = _oobs;
while (oob) {
if (strcmp(prefix, oob->prefix) == 0 && oob->cb == *callback) {
if (strcmp(prefix, oob->prefix) == 0) {
return true;
}
oob = oob->next;
Expand Down Expand Up @@ -612,6 +613,11 @@ void ATHandler::set_default_delimiter()
_delimiter = DEFAULT_DELIMITER;
}

void ATHandler::use_delimiter(bool use_delimiter)
{
_use_delimiter = use_delimiter;
}

void ATHandler::set_tag(tag_t *tag_dst, const char *tag_seq)
{
if (tag_seq) {
Expand Down Expand Up @@ -814,6 +820,8 @@ void ATHandler::resp(const char *prefix, bool check_urc)

if (check_urc && match_urc()) {
_urc_matched = true;
clear_error();
continue;
}

// If no match found, look for CRLF and consume everything up to and including CRLF
Expand Down Expand Up @@ -1029,7 +1037,7 @@ void ATHandler::set_string(char *dest, const char *src, size_t src_len)

const char *ATHandler::mem_str(const char *dest, size_t dest_len, const char *src, size_t src_len)
{
if (dest_len > src_len) {
if (dest_len >= src_len) {
for (size_t i = 0; i < dest_len - src_len + 1; ++i) {
if (memcmp(dest + i, src, src_len) == 0) {
return dest + i;
Expand Down Expand Up @@ -1147,6 +1155,11 @@ bool ATHandler::check_cmd_send()
return false;
}

// Don't write delimiter if flag was set so
if (!_use_delimiter) {
return true;
}

// Don't write delimiter if this is the first subparameter
if (_cmd_start) {
_cmd_start = false;
Expand Down
12 changes: 9 additions & 3 deletions features/cellular/framework/AT/ATHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ class ATHandler {
/** Remove urc handler from linked list of urc's
*
* @param prefix Register urc prefix for callback. Urc could be for example "+CMTI: "
* @param callback Callback, which is called if urc is found in AT response
*/
void remove_urc_handler(const char *prefix, mbed::Callback<void()> callback);
void remove_urc_handler(const char *prefix);

ATHandler *_nextATHandler; // linked list

Expand Down Expand Up @@ -282,6 +281,12 @@ class ATHandler {
*/
void set_default_delimiter();

/** Defines behaviour for using or ignoring the delimiter within an AT command
*
* @param use_delimiter indicating if delimiter should be used or not
*/
void use_delimiter(bool use_delimiter);

/** Consumes the reading buffer up to the delimiter or stop_tag
*
* @param count number of parameters to be skipped
Expand Down Expand Up @@ -436,6 +441,7 @@ class ATHandler {
char _info_resp_prefix[BUFF_SIZE];
bool _debug_on;
bool _cmd_start;
bool _use_delimiter;

// time when a command or an URC processing was started
uint64_t _start_time;
Expand Down Expand Up @@ -515,7 +521,7 @@ class ATHandler {
const char *mem_str(const char *dest, size_t dest_len, const char *src, size_t src_len);

// check is urc is already added
bool find_urc_handler(const char *prefix, mbed::Callback<void()> *callback);
bool find_urc_handler(const char *prefix);

// print contents of a buffer to trace log
void debug_print(char *p, int len);
Expand Down
16 changes: 9 additions & 7 deletions features/cellular/framework/AT/AT_CellularNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ AT_CellularNetwork::~AT_CellularNetwork()

for (int type = 0; type < CellularNetwork::C_MAX; type++) {
if (has_registration((RegistrationType)type) != RegistrationModeDisable) {
_at.remove_urc_handler(at_reg[type].urc_prefix, _urc_funcs[type]);
_at.remove_urc_handler(at_reg[type].urc_prefix);
}
}

_at.remove_urc_handler("NO CARRIER", callback(this, &AT_CellularNetwork::urc_no_carrier));
_at.remove_urc_handler("+CGEV:", callback(this, &AT_CellularNetwork::urc_cgev));
_at.remove_urc_handler("NO CARRIER");
_at.remove_urc_handler("+CGEV:");
free_credentials();
}

Expand Down Expand Up @@ -386,9 +386,11 @@ nsapi_error_t AT_CellularNetwork::open_data_channel()
_at.write_int(_cid);
} else {
MBED_ASSERT(_cid >= 0 && _cid <= 99);
char cmd_buf[sizeof("ATD*99***xx#")];
std::sprintf(cmd_buf, "ATD*99***%d#", _cid);
_at.cmd_start(cmd_buf);
_at.cmd_start("ATD*99***");
_at.use_delimiter(false);
_at.write_int(_cid);
_at.write_string("#", false);
_at.use_delimiter(true);
}
_at.cmd_stop();

Expand Down Expand Up @@ -456,7 +458,7 @@ nsapi_error_t AT_CellularNetwork::disconnect()

_at.restore_at_timeout();

_at.remove_urc_handler("+CGEV:", callback(this, &AT_CellularNetwork::urc_cgev));
_at.remove_urc_handler("+CGEV:");
call_network_cb(NSAPI_STATUS_DISCONNECTED);

return _at.unlock_return_error();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ GEMALTO_CINTERION_CellularStack::GEMALTO_CINTERION_CellularStack(ATHandler &atHa

GEMALTO_CINTERION_CellularStack::~GEMALTO_CINTERION_CellularStack()
{
_at.remove_urc_handler("^SIS:", mbed::Callback<void()>(this, &GEMALTO_CINTERION_CellularStack::urc_sis));
_at.remove_urc_handler("^SISW:", mbed::Callback<void()>(this, &GEMALTO_CINTERION_CellularStack::urc_sisw));
_at.remove_urc_handler("^SISR:", mbed::Callback<void()>(this, &GEMALTO_CINTERION_CellularStack::urc_sisr));
_at.remove_urc_handler("^SIS:");
_at.remove_urc_handler("^SISW:");
_at.remove_urc_handler("^SISR:");
}

GEMALTO_CINTERION_CellularStack::CellularSocket *GEMALTO_CINTERION_CellularStack::find_socket(int sock_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ nsapi_error_t QUECTEL_BG96_CellularPower::set_device_ready_urc_cb(mbed::Callback

void QUECTEL_BG96_CellularPower::remove_device_ready_urc_cb(mbed::Callback<void()> callback)
{
_at.remove_urc_handler(DEVICE_READY_URC, callback);
_at.remove_urc_handler(DEVICE_READY_URC);
}