Skip to content

Commit eb48a1f

Browse files
committed
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 <CR><LF> World! """ With previous implementation, second consume_to_stop_tag was stopping in <CR><LF> and rest of the code was failing for obvious reasons. With this commit we consume the full payload as bytes.
1 parent d676084 commit eb48a1f

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

connectivity/cellular/include/cellular/framework/API/ATHandler.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,13 @@ class ATHandler {
318318
*/
319319
void skip_param(ssize_t len, uint32_t count);
320320

321+
/** Consumes the given length from the reading buffer even if the stop tag has been found
322+
*
323+
* @param len length to be consumed from reading buffer
324+
* @param count number of parameters to be skipped
325+
*/
326+
void skip_param_bytes(ssize_t len, uint32_t count);
327+
321328
/** Reads given number of bytes from receiving buffer without checking any subparameter delimiters, such as comma.
322329
*
323330
* @param buf output buffer for the read
@@ -408,6 +415,12 @@ class ATHandler {
408415
*/
409416
bool consume_to_stop_tag();
410417

418+
/** Consumes the received content until current stop tag is found even if stop tag has been found previously
419+
*
420+
* @return true if stop tag is found, false otherwise
421+
*/
422+
bool consume_to_stop_tag_even_found();
423+
411424
/** Return the last 3GPP error code.
412425
* @return last 3GPP error code
413426
*/

connectivity/cellular/source/framework/AT/AT_CellularSMS.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,7 @@ nsapi_error_t AT_CellularSMS::list_messages()
10361036
int index = 0;
10371037
int length = 0;
10381038
char *pdu = NULL;
1039+
char buffer[32]; // 32 > SMS_STATUS_SIZE, SMS_MAX_PHONE_NUMBER_SIZE, SMS_MAX_TIME_STAMP_SIZE
10391040

10401041
_at.resp_start("+CMGL:");
10411042
while (_at.info_resp()) {
@@ -1058,8 +1059,18 @@ nsapi_error_t AT_CellularSMS::list_messages()
10581059
// +CMGL: <index>,<stat>,<oa/da>,[<alpha>],[<scts>][,<tooa/toda>,<length>]<CR><LF><data>[<CR><LF>
10591060
// +CMGL: <index>,<stat>,<da/oa>,[<alpha>],[<scts>][,<tooa/toda>,<length>]<CR><LF><data>[...]]
10601061
index = _at.read_int();
1061-
(void)_at.consume_to_stop_tag(); // consume until <CR><LF>
1062-
(void)_at.consume_to_stop_tag(); // consume until <CR><LF>
1062+
_at.read_string(buffer, SMS_STATUS_SIZE);
1063+
_at.read_string(buffer, SMS_MAX_PHONE_NUMBER_SIZE);
1064+
_at.skip_param(); // <alpha>
1065+
_at.read_string(buffer, SMS_MAX_TIME_STAMP_SIZE);
1066+
_at.read_int();
1067+
int size = _at.read_int(); // length
1068+
_at.consume_to_stop_tag(); // consume until <CR><LF> end of header
1069+
if (size > 0) {
1070+
// we can not use skip param because we already consumed stop tag
1071+
_at.skip_param_bytes(size, 1);
1072+
}
1073+
_at.consume_to_stop_tag_even_found(); // consume until <CR><LF> -> data
10631074
}
10641075

10651076
if (index >= 0) {

connectivity/cellular/source/framework/device/ATHandler.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,26 @@ void ATHandler::skip_param(ssize_t len, uint32_t count)
484484
return;
485485
}
486486

487+
void ATHandler::skip_param_bytes(ssize_t len, uint32_t count)
488+
{
489+
if (!ok_to_proceed()) {
490+
return;
491+
}
492+
493+
for (uint32_t i = 0; i < count; i++) {
494+
ssize_t read_len = 0;
495+
while (read_len < len) {
496+
int c = get_char();
497+
if (c == -1) {
498+
set_error(NSAPI_ERROR_DEVICE_ERROR);
499+
return;
500+
}
501+
read_len++;
502+
}
503+
}
504+
return;
505+
}
506+
487507
ssize_t ATHandler::read_bytes(uint8_t *buf, size_t len)
488508
{
489509
if (!ok_to_proceed()) {
@@ -1093,6 +1113,26 @@ bool ATHandler::consume_to_stop_tag()
10931113
return false;
10941114
}
10951115

1116+
1117+
bool ATHandler::consume_to_stop_tag_even_found()
1118+
{
1119+
if (_error_found) {
1120+
return true;
1121+
}
1122+
1123+
if (!_is_fh_usable) {
1124+
_last_err = NSAPI_ERROR_BUSY;
1125+
return true;
1126+
}
1127+
1128+
if (consume_to_tag((const char *)_stop_tag->tag, true)) {
1129+
return true;
1130+
}
1131+
1132+
clear_error();
1133+
return false;
1134+
}
1135+
10961136
// consume by size needed?
10971137

10981138
void ATHandler::resp_stop()

0 commit comments

Comments
 (0)