Skip to content

Commit 7a8da22

Browse files
Teppo JärvelinAri Parkkila
authored andcommitted
Fixed SMS send in pdu mode could give wrong size as return value.
1 parent 427674d commit 7a8da22

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

features/cellular/framework/AT/AT_CellularSMS.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ void AT_CellularSMS::set_extra_sim_wait_time(int sim_wait_time)
279279
}
280280

281281
char* AT_CellularSMS::create_pdu(const char* phone_number, const char* message, uint8_t message_length, uint8_t msg_parts,
282-
uint8_t msg_part_number)
282+
uint8_t msg_part_number, uint8_t& header_size)
283283
{
284284
int totalPDULength = 0;
285285
int number_len = strlen(phone_number);
@@ -418,6 +418,7 @@ char* AT_CellularSMS::create_pdu(const char* phone_number, const char* message,
418418

419419
// now we know the correct length of the UDL (User Data Length)
420420
int_to_hex_str(message_length + udhlen, pdu+lengthPos);
421+
header_size = x;
421422

422423
return pdu;
423424
}
@@ -480,17 +481,20 @@ nsapi_size_or_error_t AT_CellularSMS::send_sms(const char* phone_number, const c
480481

481482
int remaining_len = msg_len;
482483
int pdu_len;
484+
int msg_write_len = 0;
485+
uint8_t header_len;
483486
char *pdu_str;
484-
for (int i = 0; i< sms_count; i++) {
487+
for (int i = 0; i < sms_count; i++) {
485488

489+
header_len = 0;
486490
if (sms_count == 1) {
487491
pdu_len = msg_len;
488492
} else {
489493
pdu_len = remaining_len > concatenated_sms_length ? concatenated_sms_length : remaining_len;
490494
}
491495

492496
pdu_str = create_pdu(phone_number+remove_plus_sign, message + i*concatenated_sms_length, pdu_len,
493-
sms_count, i+1);
497+
sms_count, i+1, header_len);
494498
if (!pdu_str) {
495499
_at.unlock();
496500
return NSAPI_ERROR_NO_MEMORY;
@@ -508,13 +512,21 @@ nsapi_size_or_error_t AT_CellularSMS::send_sms(const char* phone_number, const c
508512
if (_at.get_last_error() == NSAPI_ERROR_OK) {
509513
write_size = _at.write_bytes((uint8_t*)pdu_str, pdu_len);
510514
if (write_size < pdu_len) {
515+
// calculate exact size of what we have send
516+
if (write_size <= header_len) {
517+
// managed only to write header or some of it so actual msg write size in this iteration is 0
518+
write_size = 0;
519+
} else {
520+
write_size = (write_size - header_len)/2; // as hex encoded so divide by two
521+
}
522+
msg_write_len += write_size;
523+
511524
// sending can be cancelled by giving <ESC> character (IRA 27).
512525
_at.cmd_start(ESC);
513526
_at.cmd_stop();
514527
_at.unlock();
515528
free(pdu_str);
516-
//TODO: Fix this (might be bigger value than msg_len!)
517-
return write_size;
529+
return msg_write_len;
518530
}
519531

520532
// <ctrl-Z> (IRA 26) must be used to indicate the ending of the message body.
@@ -535,8 +547,7 @@ nsapi_size_or_error_t AT_CellularSMS::send_sms(const char* phone_number, const c
535547
nsapi_error_t ret = _at.get_last_error();
536548
_at.unlock();
537549

538-
//TODO: fix this also: msg_len should be returned instead of write_size!
539-
return (ret == NSAPI_ERROR_OK) ? write_size : ret;
550+
return (ret == NSAPI_ERROR_OK) ? msg_len : ret;
540551
}
541552

542553
void AT_CellularSMS::set_sms_callback(Callback<void()> func)

features/cellular/framework/AT/AT_CellularSMS.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class AT_CellularSMS: public CellularSMS, public AT_CellularBase
208208
bool create_time(const char* time_string, time_t* time);
209209
int compare_time_strings(const char* time_string_1, const char* time_string_2);
210210
char* create_pdu(const char* phone_number, const char* message, uint8_t message_length, uint8_t msg_parts,
211-
uint8_t msg_part_number);
211+
uint8_t msg_part_number, uint8_t& header_size);
212212
nsapi_size_or_error_t read_sms_from_index(int msg_index, char* buf, uint16_t len, char* phone_num,
213213
char* time_stamp);
214214
nsapi_size_or_error_t read_sms(sms_info_t* sms, char* buf, char* phone_num, char* time_stamp);

0 commit comments

Comments
 (0)