From e13a7e1fe42af1cc1edb10ed5ebbc27e674acc83 Mon Sep 17 00:00:00 2001 From: David Alonso de la Torre Date: Sat, 30 Dec 2023 11:52:12 +0100 Subject: [PATCH] Fix AT_CellularSMS::list_messages breaking in text mode when CRLF is contained in SMS payload text When parsing SMS, it can happen that we receive CRLF in the SMS payload (happened to me when receiving provider texts). As an example, we can receive: """ Hello World! """ With previous implementation, second consume_to_stop_tag was stopping in and rest of the code was failing for obvious reasons. With this commit we consume the full payload as bytes. --- .../cellular/framework/API/ATHandler.h | 13 ++++++ .../source/framework/AT/AT_CellularSMS.cpp | 14 ++++++- .../source/framework/device/ATHandler.cpp | 40 +++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/connectivity/cellular/include/cellular/framework/API/ATHandler.h b/connectivity/cellular/include/cellular/framework/API/ATHandler.h index da2a6ea124af..4c70dc2fa7a8 100644 --- a/connectivity/cellular/include/cellular/framework/API/ATHandler.h +++ b/connectivity/cellular/include/cellular/framework/API/ATHandler.h @@ -318,6 +318,13 @@ class ATHandler { */ void skip_param(ssize_t len, uint32_t count); + /** Consumes the given length from the reading buffer even if the stop tag has been found + * + * @param len length to be consumed from reading buffer + * @param count number of parameters to be skipped + */ + void skip_param_bytes(ssize_t len, uint32_t count); + /** Reads given number of bytes from receiving buffer without checking any subparameter delimiters, such as comma. * * @param buf output buffer for the read @@ -408,6 +415,12 @@ class ATHandler { */ bool consume_to_stop_tag(); + /** Consumes the received content until current stop tag is found even if stop tag has been found previously + * + * @return true if stop tag is found, false otherwise + */ + bool consume_to_stop_tag_even_found(); + /** Return the last 3GPP error code. * @return last 3GPP error code */ diff --git a/connectivity/cellular/source/framework/AT/AT_CellularSMS.cpp b/connectivity/cellular/source/framework/AT/AT_CellularSMS.cpp index 949bf17cfb74..761b976fcdfe 100644 --- a/connectivity/cellular/source/framework/AT/AT_CellularSMS.cpp +++ b/connectivity/cellular/source/framework/AT/AT_CellularSMS.cpp @@ -1058,8 +1058,18 @@ nsapi_error_t AT_CellularSMS::list_messages() // +CMGL: ,,,[],[][,,][ // +CMGL: ,,,[],[][,,][...]] index = _at.read_int(); - (void)_at.consume_to_stop_tag(); // consume until - (void)_at.consume_to_stop_tag(); // consume until + _at.read_string(buffer, SMS_STATUS_SIZE); + _at.read_string(buffer, SMS_MAX_PHONE_NUMBER_SIZE); + _at.skip_param(); // + _at.read_string(buffer, SMS_MAX_TIME_STAMP_SIZE); + _at.read_int(); + int size = _at.read_int(); // length + _at.consume_to_stop_tag(); // consume until end of header + if (size > 0) { + // we can not use skip param because we already consumed stop tag + _at.skip_param_bytes(size, 1); + } + _at.consume_to_stop_tag_even_found(); // consume until -> data } if (index >= 0) { diff --git a/connectivity/cellular/source/framework/device/ATHandler.cpp b/connectivity/cellular/source/framework/device/ATHandler.cpp index 034d586a142d..7388bd4eb4ab 100644 --- a/connectivity/cellular/source/framework/device/ATHandler.cpp +++ b/connectivity/cellular/source/framework/device/ATHandler.cpp @@ -484,6 +484,26 @@ void ATHandler::skip_param(ssize_t len, uint32_t count) return; } +void ATHandler::skip_param_bytes(ssize_t len, uint32_t count) +{ + if (!ok_to_proceed()) { + return; + } + + for (uint32_t i = 0; i < count; i++) { + ssize_t read_len = 0; + while (read_len < len) { + int c = get_char(); + if (c == -1) { + set_error(NSAPI_ERROR_DEVICE_ERROR); + return; + } + read_len++; + } + } + return; +} + ssize_t ATHandler::read_bytes(uint8_t *buf, size_t len) { if (!ok_to_proceed()) { @@ -1093,6 +1113,26 @@ bool ATHandler::consume_to_stop_tag() return false; } + +bool ATHandler::consume_to_stop_tag_even_found() +{ + if (_error_found) { + return true; + } + + if (!_is_fh_usable) { + _last_err = NSAPI_ERROR_BUSY; + return true; + } + + if (consume_to_tag((const char *)_stop_tag->tag, true)) { + return true; + } + + clear_error(); + return false; +} + // consume by size needed? void ATHandler::resp_stop()