Skip to content

Commit 8926ea0

Browse files
authored
Merge pull request #12763 from kivaisan/fix_athandler_read_string
Cellular: Fix ATHandler::read_string to handle delimiter inside string
2 parents d4da298 + 584d54e commit 8926ea0

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,31 @@ TEST_F(TestATHandler, test_ATHandler_read_string)
763763
EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == at.get_last_error());
764764
}
765765

766+
TEST_F(TestATHandler, test_ATHandler_read_string_with_delimiter)
767+
{
768+
EventQueue que;
769+
FileHandle_stub fh1;
770+
filehandle_stub_table = NULL;
771+
filehandle_stub_table_pos = 0;
772+
773+
ATHandler at(&fh1, que, 0, ",");
774+
775+
char table1[] = "+CCLK:\"20/04/05,15:38:57+12\"\r\nOK\r\n";
776+
at.flush();
777+
filehandle_stub_table = table1;
778+
filehandle_stub_table_pos = 0;
779+
mbed_poll_stub::revents_value = POLLIN;
780+
mbed_poll_stub::int_value = 1;
781+
782+
char buf1[64];
783+
784+
at.clear_error();
785+
at.resp_start("+CCLK:");
786+
// Device error because empty buffer and attempt to fill_buffer by consume_char('\"')
787+
EXPECT_EQ(20, at.read_string(buf1, sizeof(buf1)));
788+
EXPECT_STREQ("20/04/05,15:38:57+12", buf1);
789+
}
790+
766791
TEST_F(TestATHandler, test_ATHandler_read_hex_string)
767792
{
768793
EventQueue que;
@@ -852,7 +877,7 @@ TEST_F(TestATHandler, test_ATHandler_read_int)
852877
EXPECT_TRUE(-1 == ret);
853878
at.clear_error();
854879

855-
char table[] = "\",\"OK\r\n\0";
880+
char table[] = ",OK\r\n\0";
856881
filehandle_stub_table = table;
857882
filehandle_stub_table_pos = 0;
858883
mbed_poll_stub::revents_value = POLLIN;
@@ -865,7 +890,7 @@ TEST_F(TestATHandler, test_ATHandler_read_int)
865890
at.flush();
866891
at.clear_error();
867892

868-
char table2[] = "\"2,\"OK\r\n\0";
893+
char table2[] = "2,OK\r\n\0";
869894
filehandle_stub_table = table2;
870895
filehandle_stub_table_pos = 0;
871896
mbed_poll_stub::revents_value = POLLIN;

features/cellular/framework/device/ATHandler.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,19 +512,21 @@ ssize_t ATHandler::read_string(char *buf, size_t size, bool read_even_stop_tag)
512512
unsigned int len = 0;
513513
size_t match_pos = 0;
514514
bool delimiter_found = false;
515+
bool inside_quotes = false;
515516

516517
for (; len < (size - 1 + match_pos); len++) {
517518
int c = get_char();
518519
if (c == -1) {
519520
set_error(NSAPI_ERROR_DEVICE_ERROR);
520521
return -1;
521-
} else if (c == _delimiter) {
522+
} else if (c == _delimiter && !inside_quotes) {
522523
buf[len] = '\0';
523524
delimiter_found = true;
524525
break;
525526
} else if (c == '\"') {
526527
match_pos = 0;
527528
len--;
529+
inside_quotes = !inside_quotes;
528530
continue;
529531
} else if (_stop_tag->len && c == _stop_tag->tag[match_pos]) {
530532
match_pos++;

0 commit comments

Comments
 (0)