mirror of https://github.com/ARMmbed/mbed-os.git
				
				
				
			Merge pull request #12763 from kivaisan/fix_athandler_read_string
Cellular: Fix ATHandler::read_string to handle delimiter inside stringpull/12778/head
						commit
						8926ea0983
					
				| 
						 | 
					@ -763,6 +763,31 @@ TEST_F(TestATHandler, test_ATHandler_read_string)
 | 
				
			||||||
    EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == at.get_last_error());
 | 
					    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)
 | 
					TEST_F(TestATHandler, test_ATHandler_read_hex_string)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    EventQueue que;
 | 
					    EventQueue que;
 | 
				
			||||||
| 
						 | 
					@ -852,7 +877,7 @@ TEST_F(TestATHandler, test_ATHandler_read_int)
 | 
				
			||||||
    EXPECT_TRUE(-1 == ret);
 | 
					    EXPECT_TRUE(-1 == ret);
 | 
				
			||||||
    at.clear_error();
 | 
					    at.clear_error();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    char table[] = "\",\"OK\r\n\0";
 | 
					    char table[] = ",OK\r\n\0";
 | 
				
			||||||
    filehandle_stub_table = table;
 | 
					    filehandle_stub_table = table;
 | 
				
			||||||
    filehandle_stub_table_pos = 0;
 | 
					    filehandle_stub_table_pos = 0;
 | 
				
			||||||
    mbed_poll_stub::revents_value = POLLIN;
 | 
					    mbed_poll_stub::revents_value = POLLIN;
 | 
				
			||||||
| 
						 | 
					@ -865,7 +890,7 @@ TEST_F(TestATHandler, test_ATHandler_read_int)
 | 
				
			||||||
    at.flush();
 | 
					    at.flush();
 | 
				
			||||||
    at.clear_error();
 | 
					    at.clear_error();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    char table2[] = "\"2,\"OK\r\n\0";
 | 
					    char table2[] = "2,OK\r\n\0";
 | 
				
			||||||
    filehandle_stub_table = table2;
 | 
					    filehandle_stub_table = table2;
 | 
				
			||||||
    filehandle_stub_table_pos = 0;
 | 
					    filehandle_stub_table_pos = 0;
 | 
				
			||||||
    mbed_poll_stub::revents_value = POLLIN;
 | 
					    mbed_poll_stub::revents_value = POLLIN;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -512,19 +512,21 @@ ssize_t ATHandler::read_string(char *buf, size_t size, bool read_even_stop_tag)
 | 
				
			||||||
    unsigned int len = 0;
 | 
					    unsigned int len = 0;
 | 
				
			||||||
    size_t match_pos = 0;
 | 
					    size_t match_pos = 0;
 | 
				
			||||||
    bool delimiter_found = false;
 | 
					    bool delimiter_found = false;
 | 
				
			||||||
 | 
					    bool inside_quotes = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (; len < (size - 1 + match_pos); len++) {
 | 
					    for (; len < (size - 1 + match_pos); len++) {
 | 
				
			||||||
        int c = get_char();
 | 
					        int c = get_char();
 | 
				
			||||||
        if (c == -1) {
 | 
					        if (c == -1) {
 | 
				
			||||||
            set_error(NSAPI_ERROR_DEVICE_ERROR);
 | 
					            set_error(NSAPI_ERROR_DEVICE_ERROR);
 | 
				
			||||||
            return -1;
 | 
					            return -1;
 | 
				
			||||||
        } else if (c == _delimiter) {
 | 
					        } else if (c == _delimiter && !inside_quotes) {
 | 
				
			||||||
            buf[len] = '\0';
 | 
					            buf[len] = '\0';
 | 
				
			||||||
            delimiter_found = true;
 | 
					            delimiter_found = true;
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
        } else if (c == '\"') {
 | 
					        } else if (c == '\"') {
 | 
				
			||||||
            match_pos = 0;
 | 
					            match_pos = 0;
 | 
				
			||||||
            len--;
 | 
					            len--;
 | 
				
			||||||
 | 
					            inside_quotes = !inside_quotes;
 | 
				
			||||||
            continue;
 | 
					            continue;
 | 
				
			||||||
        } else if (_stop_tag->len && c == _stop_tag->tag[match_pos]) {
 | 
					        } else if (_stop_tag->len && c == _stop_tag->tag[match_pos]) {
 | 
				
			||||||
            match_pos++;
 | 
					            match_pos++;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue