Merge pull request #12763 from kivaisan/fix_athandler_read_string

Cellular: Fix ATHandler::read_string to handle delimiter inside string
pull/12778/head
Martin Kojtal 2020-04-15 09:27:40 +02:00 committed by GitHub
commit 8926ea0983
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View File

@ -763,6 +763,31 @@ TEST_F(TestATHandler, test_ATHandler_read_string)
EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == at.get_last_error());
}
TEST_F(TestATHandler, test_ATHandler_read_string_with_delimiter)
{
EventQueue que;
FileHandle_stub fh1;
filehandle_stub_table = NULL;
filehandle_stub_table_pos = 0;
ATHandler at(&fh1, que, 0, ",");
char table1[] = "+CCLK:\"20/04/05,15:38:57+12\"\r\nOK\r\n";
at.flush();
filehandle_stub_table = table1;
filehandle_stub_table_pos = 0;
mbed_poll_stub::revents_value = POLLIN;
mbed_poll_stub::int_value = 1;
char buf1[64];
at.clear_error();
at.resp_start("+CCLK:");
// Device error because empty buffer and attempt to fill_buffer by consume_char('\"')
EXPECT_EQ(20, at.read_string(buf1, sizeof(buf1)));
EXPECT_STREQ("20/04/05,15:38:57+12", buf1);
}
TEST_F(TestATHandler, test_ATHandler_read_hex_string)
{
EventQueue que;
@ -852,7 +877,7 @@ TEST_F(TestATHandler, test_ATHandler_read_int)
EXPECT_TRUE(-1 == ret);
at.clear_error();
char table[] = "\",\"OK\r\n\0";
char table[] = ",OK\r\n\0";
filehandle_stub_table = table;
filehandle_stub_table_pos = 0;
mbed_poll_stub::revents_value = POLLIN;
@ -865,7 +890,7 @@ TEST_F(TestATHandler, test_ATHandler_read_int)
at.flush();
at.clear_error();
char table2[] = "\"2,\"OK\r\n\0";
char table2[] = "2,OK\r\n\0";
filehandle_stub_table = table2;
filehandle_stub_table_pos = 0;
mbed_poll_stub::revents_value = POLLIN;

View File

@ -512,19 +512,21 @@ ssize_t ATHandler::read_string(char *buf, size_t size, bool read_even_stop_tag)
unsigned int len = 0;
size_t match_pos = 0;
bool delimiter_found = false;
bool inside_quotes = false;
for (; len < (size - 1 + match_pos); len++) {
int c = get_char();
if (c == -1) {
set_error(NSAPI_ERROR_DEVICE_ERROR);
return -1;
} else if (c == _delimiter) {
} else if (c == _delimiter && !inside_quotes) {
buf[len] = '\0';
delimiter_found = true;
break;
} else if (c == '\"') {
match_pos = 0;
len--;
inside_quotes = !inside_quotes;
continue;
} else if (_stop_tag->len && c == _stop_tag->tag[match_pos]) {
match_pos++;