From 31f153af7da180e3e2ca38a1131b927b688f1885 Mon Sep 17 00:00:00 2001 From: Mirela Chirica Date: Wed, 10 Oct 2018 10:59:23 +0300 Subject: [PATCH] Cellular: Fix for AT handler consume to tag If sequence from buffer contains tag but symbol before tag is same as first symbol of the tag, then the tag wasn't detected. For example, "\r\n" tag was not found from "\r\r\nOK" sequence. --- .../framework/AT/athandler/athandlertest.cpp | 21 +++++++++++++++++++ features/cellular/framework/AT/ATHandler.cpp | 6 +++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/UNITTESTS/features/cellular/framework/AT/athandler/athandlertest.cpp b/UNITTESTS/features/cellular/framework/AT/athandler/athandlertest.cpp index 7a49a70c42..45bbc1f727 100644 --- a/UNITTESTS/features/cellular/framework/AT/athandler/athandlertest.cpp +++ b/UNITTESTS/features/cellular/framework/AT/athandler/athandlertest.cpp @@ -1052,6 +1052,27 @@ TEST_F(TestATHandler, test_ATHandler_consume_to_stop_tag) ATHandler at(&fh1, que, 0, ","); EXPECT_TRUE(at.consume_to_stop_tag()); + + at.clear_error(); + char table1[] = "\r\n\r\r\r\nOOK\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[6]; + at.resp_start(); + EXPECT_TRUE(at.consume_to_stop_tag()); + + at.clear_error(); + char table2[] = "OKOK\r\n"; + at.flush(); + filehandle_stub_table = table2; + filehandle_stub_table_pos = 0; + mbed_poll_stub::revents_value = POLLIN; + mbed_poll_stub::int_value = 1; + char buf2[6]; + EXPECT_TRUE(at.consume_to_stop_tag()); } TEST_F(TestATHandler, test_ATHandler_set_debug) diff --git a/features/cellular/framework/AT/ATHandler.cpp b/features/cellular/framework/AT/ATHandler.cpp index e3c6cdd39d..a8987ce8ee 100644 --- a/features/cellular/framework/AT/ATHandler.cpp +++ b/features/cellular/framework/AT/ATHandler.cpp @@ -926,7 +926,9 @@ bool ATHandler::consume_to_tag(const char *tag, bool consume_tag) int c = get_char(); if (c == -1) { break; - } else if (c == tag[match_pos]) { + // compares c against tag at current position and if this match fails + // compares c against tag[0] and also resets match_pos to 0 + } else if (c == tag[match_pos] || ((match_pos = 1) && (c == tag[--match_pos]))) { match_pos++; if (match_pos == strlen(tag)) { if (!consume_tag) { @@ -934,8 +936,6 @@ bool ATHandler::consume_to_tag(const char *tag, bool consume_tag) } return true; } - } else if (match_pos) { - match_pos = 0; } } tr_debug("consume_to_tag not found");