Skip to content

Cellular: Check for URC during AT response stop #10341

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 1 commit into from
Apr 9, 2019
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 @@ -29,8 +29,11 @@
using namespace mbed;
using namespace events;

uint8_t urc_callback_count;

void urc_callback()
{
urc_callback_count++;
}

void urc2_callback()
Expand All @@ -44,6 +47,7 @@ class TestATHandler : public testing::Test {

void SetUp()
{
urc_callback_count = 0;
}

void TearDown()
Expand Down Expand Up @@ -954,16 +958,19 @@ TEST_F(TestATHandler, test_ATHandler_resp_start)
filehandle_stub_table_pos = 0;
at.resp_start();

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

at.set_urc_handler("urc: ", NULL);
mbed::Callback<void()> cb1(&urc_callback);
at.set_urc_handler("urc: ", cb1);
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);
EXPECT_TRUE(urc_callback_count == 1);
urc_callback_count = 0;

char table8[] = "urc: info\r\nresponse\0";
at.flush();
Expand Down Expand Up @@ -1060,8 +1067,21 @@ TEST_F(TestATHandler, test_ATHandler_resp_stop)
filehandle_stub_table = table3;
filehandle_stub_table_pos = 0;
at.resp_start();
at.resp_stop();

// Set stop tag for response to CRLF -> resp stop should stop on first CRLF
char table6[] = "line1\r\nline2\r\nOK\r\n";
filehandle_stub_table = table6;
filehandle_stub_table_pos = 0;

at.flush();
at.clear_error();
filehandle_stub_table_pos = 0;

at.resp_start();
at.set_stop_tag("\r\n");
at.resp_stop();
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_OK);

char table7[] = "ssssss\0";
filehandle_stub_table = table7;
Expand All @@ -1072,6 +1092,38 @@ TEST_F(TestATHandler, test_ATHandler_resp_stop)
filehandle_stub_table_pos = 0;
at.resp_start("ss", false);
at.resp_stop();

// prefix + URC line + some other line + URC line + URC line + OKCRLF
char table4[] = "line1\r\nline2abcd\r\nline3abcd\r\nline4\r\n\r\nline3\r\nline3\r\nOK\r\n";
filehandle_stub_table = table4;
filehandle_stub_table_pos = 0;

at.flush();
at.clear_error();
filehandle_stub_table_pos = 0;
mbed::Callback<void()> cb1(&urc_callback);
at.set_urc_handler("line3", cb1);

at.resp_start("line2");
at.resp_stop();
EXPECT_TRUE(urc_callback_count == 3);
urc_callback_count = 0;

// URC line + prefix + URC line + some other line + URC line + URC line + some other line + OKCRLF
char table5[] = "line1\r\nline3\r\nline2abcd\r\nline3abcd\r\nline4\r\n\r\nline3\r\nline3\r\nline4\r\nOK\r\n";
filehandle_stub_table = table5;
filehandle_stub_table_pos = 0;

at.flush();
at.clear_error();
filehandle_stub_table_pos = 0;
mbed::Callback<void()> cb2(&urc_callback);
at.set_urc_handler("line3", cb2);

at.resp_start("line2");
at.resp_stop();
EXPECT_TRUE(urc_callback_count == 4);
urc_callback_count = 0;
}

TEST_F(TestATHandler, test_ATHandler_info_resp)
Expand Down
53 changes: 42 additions & 11 deletions features/cellular/framework/AT/ATHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1058,22 +1058,53 @@ bool ATHandler::consume_to_stop_tag()

void ATHandler::resp_stop()
{
// Do not return on error so that we can consume whatever there is in the buffer
if (_is_fh_usable) {
// Do not return on error so that we can consume whatever there is in the buffer

if (_current_scope == ElemType) {
information_response_element_stop();
set_scope(InfoType);
}
if (_current_scope == ElemType) {
information_response_element_stop();
set_scope(InfoType);
}

if (_current_scope == InfoType) {
information_response_stop();
}
if (_current_scope == InfoType) {
information_response_stop();
}

// Go for response stop_tag
if (consume_to_stop_tag()) {
set_scope(NotSet);
// Go for response stop_tag
if (_stop_tag && !_stop_tag->found && !_error_found) {
// Check for URC for every new line
while (!get_last_error()) {

if (match(_stop_tag->tag, _stop_tag->len)) {
break;
}

if (match_urc()) {
continue;
}

// If no URC nor stop_tag found, look for CRLF and consume everything up to and including CRLF
if (mem_str(_recv_buff, _recv_len, CRLF, CRLF_LENGTH)) {
consume_to_tag(CRLF, true);
// If stop tag is CRLF we have to stop reading/consuming the buffer
if (!strncmp(CRLF, _stop_tag->tag, _stop_tag->len)) {
break;
}
// If no URC nor CRLF nor stop_tag -> fill buffer
} else {
if (!fill_buffer()) {
// if we don't get any match and no data within timeout, set an error to indicate need for recovery
set_error(NSAPI_ERROR_DEVICE_ERROR);
}
}
}
}
} else {
_last_err = NSAPI_ERROR_BUSY;
}

set_scope(NotSet);

// Restore stop tag to OK
set_tag(&_resp_stop, OK);
// Reset info resp prefix
Expand Down
4 changes: 4 additions & 0 deletions features/cellular/framework/AT/ATHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@ class ATHandler {
/** Ends all scopes starting from current scope.
* Consumes everything until the scope's stop tag is found, then
* goes to next scope until response scope is ending.
* URC match is checked during response scope ending,
* for every new line / CRLF.
*
*
* Possible sequence:
* element scope -> information response scope -> response scope
*/
Expand Down