Skip to content

Commit 11ee33c

Browse files
committed
SMS: Increase timeout for send and get. Fix text SMS parsing when CRLF is contained in the text.
1 parent baf6a30 commit 11ee33c

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
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_stop_tag_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: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ nsapi_size_or_error_t AT_CellularSMS::send_sms(const char *phone_number, const c
419419
return NSAPI_ERROR_PARAMETER;
420420
}
421421

422+
_at.set_at_timeout(10s);
422423
_at.lock();
423424

424425
int write_size = 0;
@@ -438,6 +439,7 @@ nsapi_size_or_error_t AT_CellularSMS::send_sms(const char *phone_number, const c
438439
_at.cmd_start(ESC);
439440
_at.cmd_stop();
440441
_at.unlock();
442+
_at.restore_at_timeout();
441443
return write_size;
442444
}
443445
// <ctrl-Z> (IRA 26) must be used to indicate the ending of the message body.
@@ -483,6 +485,7 @@ nsapi_size_or_error_t AT_CellularSMS::send_sms(const char *phone_number, const c
483485
sms_count, i + 1, header_len);
484486
if (!pdu_str) {
485487
_at.unlock();
488+
_at.restore_at_timeout();
486489
return NSAPI_ERROR_NO_MEMORY;
487490
}
488491
pdu_len = strlen(pdu_str);
@@ -510,6 +513,7 @@ nsapi_size_or_error_t AT_CellularSMS::send_sms(const char *phone_number, const c
510513
_at.cmd_start(ESC);
511514
_at.cmd_stop();
512515
_at.unlock();
516+
_at.restore_at_timeout();
513517
delete [] pdu_str;
514518
return msg_write_len;
515519
}
@@ -523,14 +527,19 @@ nsapi_size_or_error_t AT_CellularSMS::send_sms(const char *phone_number, const c
523527
delete [] pdu_str;
524528
remaining_len -= concatenated_sms_length;
525529
if (_at.get_last_error() != NSAPI_ERROR_OK) {
526-
return _at.unlock_return_error();
530+
nsapi_error_t ret = _at.get_last_error();
531+
_at.unlock();
532+
_at.restore_at_timeout();
533+
return ret;
527534
}
528535
}
529536
}
530537

531538
_sms_message_ref_number++;
532539
nsapi_error_t ret = _at.get_last_error();
533540
_at.unlock();
541+
_at.restore_at_timeout();
542+
_at.clear_error();
534543

535544
return (ret == NSAPI_ERROR_OK) ? msg_len : ret;
536545
}
@@ -696,6 +705,7 @@ nsapi_size_or_error_t AT_CellularSMS::get_sms(char *buf, uint16_t len, char *pho
696705
return NSAPI_ERROR_PARAMETER;
697706
}
698707

708+
_at.set_at_timeout(10s);
699709
_at.lock();
700710

701711
nsapi_size_or_error_t err = list_messages();
@@ -735,6 +745,7 @@ nsapi_size_or_error_t AT_CellularSMS::get_sms(char *buf, uint16_t len, char *pho
735745
free_linked_list();
736746

737747
_at.unlock();
748+
_at.restore_at_timeout();
738749

739750
// update error only when there really was an error, otherwise we return the length
740751
if (_at.get_last_error()) {
@@ -1027,6 +1038,7 @@ nsapi_error_t AT_CellularSMS::list_messages()
10271038
int index = 0;
10281039
int length = 0;
10291040
char *pdu = NULL;
1041+
char buffer[32]; // 32 > SMS_STATUS_SIZE, SMS_MAX_PHONE_NUMBER_SIZE, SMS_MAX_TIME_STAMP_SIZE
10301042

10311043
_at.resp_start("+CMGL:");
10321044
while (_at.info_resp()) {
@@ -1049,8 +1061,18 @@ nsapi_error_t AT_CellularSMS::list_messages()
10491061
// +CMGL: <index>,<stat>,<oa/da>,[<alpha>],[<scts>][,<tooa/toda>,<length>]<CR><LF><data>[<CR><LF>
10501062
// +CMGL: <index>,<stat>,<da/oa>,[<alpha>],[<scts>][,<tooa/toda>,<length>]<CR><LF><data>[...]]
10511063
index = _at.read_int();
1052-
(void)_at.consume_to_stop_tag(); // consume until <CR><LF>
1053-
(void)_at.consume_to_stop_tag(); // consume until <CR><LF>
1064+
_at.read_string(buffer, SMS_STATUS_SIZE);
1065+
_at.read_string(buffer, SMS_MAX_PHONE_NUMBER_SIZE);
1066+
_at.skip_param(); // <alpha>
1067+
_at.read_string(buffer, SMS_MAX_TIME_STAMP_SIZE);
1068+
_at.read_int();
1069+
int size = _at.read_int(); // length
1070+
_at.consume_to_stop_tag(); // consume until <CR><LF> end of header
1071+
if (size > 0) {
1072+
// we can not use skip param because we already consumed stop tag
1073+
_at.skip_param_bytes(size, 1);
1074+
}
1075+
_at.consume_to_stop_tag_even_stop_tag_found(); // consume until <CR><LF> -> data
10541076
}
10551077

10561078
if (index >= 0) {

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

Lines changed: 39 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,25 @@ bool ATHandler::consume_to_stop_tag()
10931113
return false;
10941114
}
10951115

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

10981137
void ATHandler::resp_stop()

0 commit comments

Comments
 (0)