Skip to content

Commit 723e913

Browse files
author
Mirela Chirica
committed
Cellular: AT handler read string up to delimiter or stop tag
1 parent fd4f47d commit 723e913

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ TEST_F(TestATHandler, test_ATHandler_read_string)
579579
EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == at.get_last_error());
580580
at.clear_error();
581581
// Device error because empty buffer and attempt to fill_buffer by consume_char('\"')
582-
EXPECT_TRUE(-1 == at.read_string(buf1, 1));
582+
EXPECT_TRUE(0 == at.read_string(buf1, 1));
583583

584584
// *** 1 BYTE ***
585585
at.clear_error();
@@ -599,7 +599,7 @@ TEST_F(TestATHandler, test_ATHandler_read_string)
599599

600600
// *** CRLF ***
601601
at.clear_error();
602-
char table3[] = "\r\ns\r\n\0";
602+
char table3[] = "\r\n,s\r\n\0";
603603
at.flush();
604604
filehandle_stub_table = table3;
605605
filehandle_stub_table_pos = 0;
@@ -670,13 +670,9 @@ TEST_F(TestATHandler, test_ATHandler_read_string)
670670
at.resp_start();
671671
// TO read 1 byte from: "s"OK\r\n -> read "
672672
at.read_bytes(buf5, 1);
673-
// TO read max 1 byte from: s"OK\r\n -> read s
673+
// TO read max 1 byte from: s"OK\r\n -> read s + read to stop_tag(OKCRLF)
674674
EXPECT_TRUE(1 == at.read_string(buf4, 1 + 1/*for NULL*/));
675675

676-
// *** Consume " and run into OKCRLF ***
677-
// TO read max 1 byte from: "OK\r\n -> consume " and find stop tag OKCRLF
678-
EXPECT_TRUE(0 == at.read_string(buf4, 1 + 1/*for NULL*/));
679-
680676
// *** Try to read after stop tag was found ***
681677
// stop tag found do not read further
682678
EXPECT_TRUE(-1 == at.read_string(buf4, 1 + 1/*for NULL*/));
@@ -706,7 +702,7 @@ TEST_F(TestATHandler, test_ATHandler_read_string)
706702
mbed_poll_stub::revents_value = POLLIN;
707703
mbed_poll_stub::int_value = 1;
708704
at.resp_start("s");
709-
// TO read from buffer having only " -> consume " -> trying to read when nothing in buffer
705+
// TO read from buffer having only " -> trying to find delimiter or stop_tag(OKCRLF)
710706
EXPECT_TRUE(-1 == at.read_string(buf4, 5));
711707
EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == at.get_last_error());
712708

@@ -739,6 +735,21 @@ TEST_F(TestATHandler, test_ATHandler_read_string)
739735
// TO read from
740736
EXPECT_TRUE(6 == at.read_string(buf9, 6 + 1/*for NULL*/));
741737

738+
at.clear_error();
739+
char table11[] = "\"1016\",\"39AB\",9\r\n\0";
740+
mbed_poll_stub::int_value = 0;
741+
at.flush();
742+
filehandle_stub_table = table11;
743+
filehandle_stub_table_pos = 0;
744+
mbed_poll_stub::revents_value = POLLIN;
745+
mbed_poll_stub::int_value = 1;
746+
at.resp_start();
747+
EXPECT_TRUE(4 == at.read_string(buf4, 4 + 1/*for NULL*/));
748+
EXPECT_TRUE(!strncmp(buf4, "1016", 4));
749+
EXPECT_TRUE(4 == at.read_string(buf4, 4 + 1/*for NULL*/));
750+
EXPECT_TRUE(!strncmp(buf4, "39AB", 4));
751+
EXPECT_TRUE(9 == at.read_int());
752+
742753
// *** CRLF part of the string ***
743754
at.clear_error();
744755
char table10[] = "\"s\"\r\nOK\r\n\0";

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -460,14 +460,9 @@ ssize_t ATHandler::read_string(char *buf, size_t size, bool read_even_stop_tag)
460460
return -1;
461461
}
462462

463-
consume_char('\"');
464-
465-
if (_last_err) {
466-
return -1;
467-
}
468-
469-
size_t len = 0;
463+
int len = 0;
470464
size_t match_pos = 0;
465+
bool delimiter_found = false;
471466

472467
for (; len < (size - 1 + match_pos); len++) {
473468
int c = get_char();
@@ -476,6 +471,7 @@ ssize_t ATHandler::read_string(char *buf, size_t size, bool read_even_stop_tag)
476471
return -1;
477472
} else if (c == _delimiter) {
478473
buf[len] = '\0';
474+
delimiter_found = true;
479475
break;
480476
} else if (c == '\"') {
481477
match_pos = 0;
@@ -501,6 +497,26 @@ ssize_t ATHandler::read_string(char *buf, size_t size, bool read_even_stop_tag)
501497
buf[len] = '\0';
502498
}
503499

500+
// Consume to delimiter or stop_tag
501+
if (!delimiter_found && !_stop_tag->found) {
502+
match_pos = 0;
503+
while(1) {
504+
int c = get_char();
505+
if (c == -1) {
506+
set_error(NSAPI_ERROR_DEVICE_ERROR);
507+
break;
508+
} else if (c == _delimiter) {
509+
break;
510+
} else if (_stop_tag->len && c == _stop_tag->tag[match_pos]) {
511+
match_pos++;
512+
if (match_pos == _stop_tag->len) {
513+
_stop_tag->found = true;
514+
break;
515+
}
516+
}
517+
}
518+
}
519+
504520
return len;
505521
}
506522

@@ -830,6 +846,7 @@ void ATHandler::resp_start(const char *prefix, bool stop)
830846
return;
831847
}
832848

849+
set_scope(NotSet);
833850
// Try get as much data as possible
834851
rewind_buffer();
835852
(void)fill_buffer(false);

0 commit comments

Comments
 (0)