Skip to content

Commit e99c18c

Browse files
Teppo JärvelinAri Parkkila
authored andcommitted
Fixed SMS receive in PDU: certain length sms last char was missing.
1 parent 6ccbb59 commit e99c18c

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

features/cellular/framework/AT/AT_CellularSMS.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -895,13 +895,14 @@ nsapi_size_or_error_t AT_CellularSMS::get_sms(char* buf, uint16_t len, char* pho
895895
index+=14;
896896
}
897897

898+
int udl = hex_str_to_int(pdu+index, 2);
898899
index +=2;
900+
899901
int paddingBits = 0;
900-
int parts = 1;
901902
int partnro = 1;
902903
if (userDataHeader) {
903904
// we need to read User Defined Header to know what part number this message is.
904-
index += read_udh_from_pdu(pdu+index, info, partnro, parts, paddingBits);
905+
index += read_udh_from_pdu(pdu+index, info, partnro, paddingBits);
905906
}
906907

907908
if (part_number) {
@@ -910,7 +911,7 @@ nsapi_size_or_error_t AT_CellularSMS::get_sms(char* buf, uint16_t len, char* pho
910911

911912
if (msg) {
912913
// we are reading the message
913-
err = read_pdu_payload(pdu+index, dataScheme, msg, paddingBits, partnro == parts);
914+
err = read_pdu_payload(pdu+index, udl, dataScheme, msg, paddingBits);
914915
}
915916
else {
916917
if (dataScheme == 0x00) {
@@ -933,16 +934,16 @@ nsapi_size_or_error_t AT_CellularSMS::get_sms(char* buf, uint16_t len, char* pho
933934
}
934935
}
935936

936-
// read params from User DEfined Header
937-
int AT_CellularSMS::read_udh_from_pdu(const char* pdu, sms_info_t *info, int &part_number, int &parts,
938-
int &padding_bits) {
937+
// read params from User Defined Header
938+
int AT_CellularSMS::read_udh_from_pdu(const char* pdu, sms_info_t *info, int &part_number, int &padding_bits) {
939939

940940
int index = 0;
941941
int udhLength = hex_str_to_int(pdu, 2);
942942
index +=2;
943943

944944
// if there is padding bits then udhlen is octet bigger as we need to keep septet boundary
945945
padding_bits = ((udhLength+1) * 8 ) % 7; // +1 is for udhLength itself
946+
946947
if (padding_bits) {
947948
padding_bits = 7 - padding_bits;
948949
} else {
@@ -966,9 +967,8 @@ int AT_CellularSMS::read_udh_from_pdu(const char* pdu, sms_info_t *info, int &pa
966967
index +=4;
967968
}
968969

969-
parts = hex_str_to_int(pdu+index, 2);
970970
if (info) {
971-
info->parts = parts;
971+
info->parts = hex_str_to_int(pdu+index, 2);
972972
}
973973
index +=2;
974974

@@ -978,13 +978,11 @@ int AT_CellularSMS::read_udh_from_pdu(const char* pdu, sms_info_t *info, int &pa
978978
return (udhLength*2 + 2); // udh in hex and udhl
979979
}
980980

981-
nsapi_size_or_error_t AT_CellularSMS::read_pdu_payload(const char* pdu, int scheme, char *msg, int padding_bits,
982-
bool last_part)
981+
nsapi_size_or_error_t AT_CellularSMS::read_pdu_payload(const char* pdu, int msg_len, int scheme, char *msg, int padding_bits)
983982
{
984983
if (scheme == 0x00) {
985984
// 7 bit gsm encoding, must do the conversions from hex to 7-bit encoding and to ascii
986-
return unpack_7_bit_gsm_to_str(pdu, strlen(pdu)/2, msg, padding_bits, last_part);
987-
985+
return unpack_7_bit_gsm_to_str(pdu, strlen(pdu)/2, msg, padding_bits, msg_len);
988986
} else if (scheme == 0x04) {
989987
// 8bit scheme so just convert hexstring to charstring
990988
return hex_str_to_char_str(pdu, strlen(pdu), msg);
@@ -1261,7 +1259,7 @@ uint16_t AT_CellularSMS::pack_7_bit_gsm_and_hex(const char* str, uint16_t len, c
12611259
}
12621260

12631261
uint16_t AT_CellularSMS::unpack_7_bit_gsm_to_str(const char* str, int len, char *buf, int padding_bits,
1264-
bool last_part)
1262+
int msg_len)
12651263
{
12661264
int strCount = 0;
12671265
uint16_t decodedCount = 0;
@@ -1284,9 +1282,7 @@ uint16_t AT_CellularSMS::pack_7_bit_gsm_and_hex(const char* str, uint16_t len, c
12841282
} else if (shift == 6) {
12851283
hex_str_to_char_str(str + (strCount-1)*2, 2, &tmp1);
12861284
buf[decodedCount] = gsm_to_ascii[(((tmp1>>2)) | (tmp << 6)) & 0x7F];
1287-
// we are unpacking the last byte and so tmp is not complete as it's not completed by the next byte.
1288-
// unless this is a multipart message and not the last part.
1289-
if (!((strCount+1 == len) && last_part)) {
1285+
if (decodedCount+1 < msg_len) {
12901286
hex_str_to_char_str(str + strCount*2, 2, &tmp);
12911287
decodedCount++;
12921288
buf[decodedCount] = gsm_to_ascii[(tmp>>1) & 0x7F];

features/cellular/framework/AT/AT_CellularSMS.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ class AT_CellularSMS: public CellularSMS, public AT_CellularBase
200200
int read_sms_params(char *, char *);
201201
void free_linked_list();
202202
void add_info(sms_info_t* info, int index, int part_number);
203-
int read_udh_from_pdu(const char* pdu, sms_info_t *info, int &part_number, int &parts, int &padding_bits);
203+
int read_udh_from_pdu(const char* pdu, sms_info_t *info, int &part_number, int &padding_bits);
204204
nsapi_size_or_error_t get_data_from_pdu(const char* pdu, sms_info_t *info, int *part_number,
205205
char *phone_number = NULL, char *msg = NULL);
206-
nsapi_size_or_error_t read_pdu_payload(const char* pdu, int scheme, char *msg, int padding_bits, bool last_part);
206+
nsapi_size_or_error_t read_pdu_payload(const char* pdu, int msg_len, int scheme, char *msg, int padding_bits);
207207
sms_info_t* get_oldest_sms_index();
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);
@@ -229,12 +229,12 @@ class AT_CellularSMS: public CellularSMS, public AT_CellularBase
229229
* @param len length of the str divided by two as str is hexencoded
230230
* @param buf preallocated destination buffer
231231
* @param padding_bits number of padding bits which were needed to hold the octet boundary
232-
* @param last_part true is last part of the encoded message
232+
* @param msg_len Length of the received message which is coded in str
233233
* @return length of the destination buffer buf
234234
*
235235
*/
236236
uint16_t unpack_7_bit_gsm_to_str(const char* str, int len, char *buf, int padding_bits,
237-
bool last_part);
237+
int msg_len);
238238
};
239239

240240
} // namespace mbed

0 commit comments

Comments
 (0)