Skip to content

Commit f100cd5

Browse files
author
Mirela Chirica
committed
Cellular: Check for URC during AT response stop
1 parent 71c84e8 commit f100cd5

File tree

3 files changed

+100
-13
lines changed

3 files changed

+100
-13
lines changed

UNITTESTS/features/cellular/framework/AT/athandler/athandlertest.cpp

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@
2929
using namespace mbed;
3030
using namespace events;
3131

32+
uint8_t urc_callback_count;
33+
3234
void urc_callback()
3335
{
36+
urc_callback_count++;
3437
}
3538

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

4548
void SetUp()
4649
{
50+
urc_callback_count = 0;
4751
}
4852

4953
void TearDown()
@@ -954,16 +958,19 @@ TEST_F(TestATHandler, test_ATHandler_resp_start)
954958
filehandle_stub_table_pos = 0;
955959
at.resp_start();
956960

957-
char table7[] = "urc: info\r\nresponseOK\r\n\0";
961+
char table7[] = "urc: info\r\nresponse\r\nOK\r\n\0";
958962
at.flush();
959963
at.clear_error();
960964
filehandle_stub_table = table7;
961965
filehandle_stub_table_pos = 0;
962966

963-
at.set_urc_handler("urc: ", NULL);
967+
mbed::Callback<void()> cb1(&urc_callback);
968+
at.set_urc_handler("urc: ", cb1);
964969
at.resp_start(); // recv_buff: "responseOK\r\n\0"
965970
at.resp_stop(); // consumes to OKCRLF -> OK
966971
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_OK);
972+
EXPECT_TRUE(urc_callback_count == 1);
973+
urc_callback_count = 0;
967974

968975
char table8[] = "urc: info\r\nresponse\0";
969976
at.flush();
@@ -1060,8 +1067,21 @@ TEST_F(TestATHandler, test_ATHandler_resp_stop)
10601067
filehandle_stub_table = table3;
10611068
filehandle_stub_table_pos = 0;
10621069
at.resp_start();
1070+
at.resp_stop();
1071+
1072+
// Set stop tag for response to CRLF -> resp stop should stop on first CRLF
1073+
char table6[] = "line1\r\nline2\r\nOK\r\n";
1074+
filehandle_stub_table = table6;
1075+
filehandle_stub_table_pos = 0;
10631076

1077+
at.flush();
1078+
at.clear_error();
1079+
filehandle_stub_table_pos = 0;
1080+
1081+
at.resp_start();
1082+
at.set_stop_tag("\r\n");
10641083
at.resp_stop();
1084+
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_OK);
10651085

10661086
char table7[] = "ssssss\0";
10671087
filehandle_stub_table = table7;
@@ -1072,6 +1092,38 @@ TEST_F(TestATHandler, test_ATHandler_resp_stop)
10721092
filehandle_stub_table_pos = 0;
10731093
at.resp_start("ss", false);
10741094
at.resp_stop();
1095+
1096+
// prefix + URC line + some other line + URC line + URC line + OKCRLF
1097+
char table4[] = "line1\r\nline2abcd\r\nline3abcd\r\nline4\r\n\r\nline3\r\nline3\r\nOK\r\n";
1098+
filehandle_stub_table = table4;
1099+
filehandle_stub_table_pos = 0;
1100+
1101+
at.flush();
1102+
at.clear_error();
1103+
filehandle_stub_table_pos = 0;
1104+
mbed::Callback<void()> cb1(&urc_callback);
1105+
at.set_urc_handler("line3", cb1);
1106+
1107+
at.resp_start("line2");
1108+
at.resp_stop();
1109+
EXPECT_TRUE(urc_callback_count == 3);
1110+
urc_callback_count = 0;
1111+
1112+
// URC line + prefix + URC line + some other line + URC line + URC line + some other line + OKCRLF
1113+
char table5[] = "line1\r\nline3\r\nline2abcd\r\nline3abcd\r\nline4\r\n\r\nline3\r\nline3\r\nline4\r\nOK\r\n";
1114+
filehandle_stub_table = table5;
1115+
filehandle_stub_table_pos = 0;
1116+
1117+
at.flush();
1118+
at.clear_error();
1119+
filehandle_stub_table_pos = 0;
1120+
mbed::Callback<void()> cb2(&urc_callback);
1121+
at.set_urc_handler("line3", cb2);
1122+
1123+
at.resp_start("line2");
1124+
at.resp_stop();
1125+
EXPECT_TRUE(urc_callback_count == 4);
1126+
urc_callback_count = 0;
10751127
}
10761128

10771129
TEST_F(TestATHandler, test_ATHandler_info_resp)

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,22 +1058,53 @@ bool ATHandler::consume_to_stop_tag()
10581058

10591059
void ATHandler::resp_stop()
10601060
{
1061-
// Do not return on error so that we can consume whatever there is in the buffer
1061+
if (_is_fh_usable) {
1062+
// Do not return on error so that we can consume whatever there is in the buffer
10621063

1063-
if (_current_scope == ElemType) {
1064-
information_response_element_stop();
1065-
set_scope(InfoType);
1066-
}
1064+
if (_current_scope == ElemType) {
1065+
information_response_element_stop();
1066+
set_scope(InfoType);
1067+
}
10671068

1068-
if (_current_scope == InfoType) {
1069-
information_response_stop();
1070-
}
1069+
if (_current_scope == InfoType) {
1070+
information_response_stop();
1071+
}
10711072

1072-
// Go for response stop_tag
1073-
if (consume_to_stop_tag()) {
1074-
set_scope(NotSet);
1073+
// Go for response stop_tag
1074+
if (_stop_tag && !_stop_tag->found && !_error_found) {
1075+
// Check for URC for every new line
1076+
while (!get_last_error()) {
1077+
1078+
if (match(_stop_tag->tag, _stop_tag->len)) {
1079+
break;
1080+
}
1081+
1082+
if (match_urc()) {
1083+
continue;
1084+
}
1085+
1086+
// If no URC nor stop_tag found, look for CRLF and consume everything up to and including CRLF
1087+
if (mem_str(_recv_buff, _recv_len, CRLF, CRLF_LENGTH)) {
1088+
consume_to_tag(CRLF, true);
1089+
// If stop tag is CRLF we have to stop reading/consuming the buffer
1090+
if (!strncmp(CRLF, _stop_tag->tag, _stop_tag->len)) {
1091+
break;
1092+
}
1093+
// If no URC nor CRLF nor stop_tag -> fill buffer
1094+
} else {
1095+
if (!fill_buffer()) {
1096+
// if we don't get any match and no data within timeout, set an error to indicate need for recovery
1097+
set_error(NSAPI_ERROR_DEVICE_ERROR);
1098+
}
1099+
}
1100+
}
1101+
}
1102+
} else {
1103+
_last_err = NSAPI_ERROR_BUSY;
10751104
}
10761105

1106+
set_scope(NotSet);
1107+
10771108
// Restore stop tag to OK
10781109
set_tag(&_resp_stop, OK);
10791110
// Reset info resp prefix

features/cellular/framework/AT/ATHandler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,10 @@ class ATHandler {
381381
/** Ends all scopes starting from current scope.
382382
* Consumes everything until the scope's stop tag is found, then
383383
* goes to next scope until response scope is ending.
384+
* URC match is checked during response scope ending,
385+
* for every new line / CRLF.
386+
*
387+
*
384388
* Possible sequence:
385389
* element scope -> information response scope -> response scope
386390
*/

0 commit comments

Comments
 (0)