forked from ARMmbed/mbed-os
/
AT_CellularSMS.cpp
1237 lines (1091 loc) · 39.3 KB
/
AT_CellularSMS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2017, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include "ThisThread.h"
#include "AT_CellularSMS.h"
#include "CellularUtil.h"
#include "CellularLog.h"
using namespace mbed_cellular_util;
using namespace mbed;
using namespace std;
using namespace rtos;
#define CTRL_Z "\x1a"
#define ESC "\x1b"
const uint8_t SMS_STATUS_SIZE = 12 + 1;
const uint8_t FIRST_OCTET_DELIVER_SUBMIT = 17;
const uint8_t TP_VALIDITY_PERIOD_24_HOURS = 167;
const uint8_t TP_PROTOCOL_IDENTIFIER = 0;
const uint8_t SMS_DATA_CODING_SCHEME = 0;
const uint8_t SMS_MAX_8BIT_CONCATENATED_SINGLE_SMS_SIZE = 134;
const uint8_t SMS_MAX_GSM7_CONCATENATED_SINGLE_SMS_SIZE = 153;
#define NVAM '?' // Not Valid ascii, ISO-8859-1 mark
// mapping table from 7-bit GSM to ascii (ISO-8859-1)
static const unsigned char gsm_to_ascii[] = {
64, // 0
163, // 1
36, // 2
165, // 3
232, // 4
233, // 5
249, // 6
236, // 7
242, // 8
199, // 9
10, // 10
216, // 11
248, // 12
13, // 13
197, // 14
229, // 15
NVAM, // 16
95, // 17
NVAM, // 18
NVAM, // 19
NVAM, // 20
NVAM, // 21
NVAM, // 22
NVAM, // 23
NVAM, // 24
NVAM, // 25
NVAM, // 26
27, // 27
198, // 28
230, // 29
223, // 30
201, // 31
32, // 32
33, // 33
34, // 34
35, // 35
164, // 36
37, // 37
38, // 38
39, // 39
40, // 40
41, // 41
42, // 42
43, // 43
44, // 44
45, // 45
46, // 46
47, // 47
48, // 48
49, // 49
50, // 50
51, // 51
52, // 52
53, // 53
54, // 54
55, // 55
56, // 56
57, // 57
58, // 58
59, // 59
60, // 60
61, // 61
62, // 62
63, // 63
161, // 64
65, // 65
66, // 66
67, // 67
68, // 68
69, // 69
70, // 70
71, // 71
72, // 72
73, // 73
74, // 74
75, // 75
76, // 76
77, // 77
78, // 78
79, // 79
80, // 80
81, // 81
82, // 82
83, // 83
84, // 84
85, // 85
86, // 86
87, // 87
88, // 88
89, // 89
90, // 90
196, // 91
214, // 92
209, // 93
220, // 94
167, // 95
191, // 96
97, // 97
98, // 98
99, // 99
100, // 100
101, // 101
102, // 102
103, // 103
104, // 104
105, // 105
106, // 106
107, // 107
108, // 108
109, // 109
110, // 110
111, // 111
112, // 112
113, // 113
114, // 114
115, // 115
116, // 116
117, // 117
118, // 118
119, // 119
120, // 120
121, // 121
122, // 122
228, // 123
246, // 124
241, // 125
252, // 126
224 // 127
};
const int GSM_TO_ASCII_TABLE_SIZE = sizeof(gsm_to_ascii) / sizeof(gsm_to_ascii[0]);
AT_CellularSMS::AT_CellularSMS(ATHandler &at) : AT_CellularBase(at), _cb(0), _mode(CellularSMSMmodeText),
_use_8bit_encoding(false), _sim_wait_time(0), _sms_message_ref_number(1), _sms_info(NULL)
{
}
AT_CellularSMS::~AT_CellularSMS()
{
}
void AT_CellularSMS::cmt_urc()
{
tr_debug("CMT_URC called");
//+CMT: <oa>,[<alpha>],<scts>[,<tooa>,<fo>,<pid>,<dcs>,<sca>,<tosca>,<length>]<CR><LF><data>
_at.consume_to_stop_tag();
// call user defined callback function
if (_cb) {
_cb();
} else {
tr_warn("cmt_urc, no user defined callback for receiving sms!");
}
}
void AT_CellularSMS::cmti_urc()
{
//+CMTI: <mem>,<index>,
tr_debug("CMTI_URC called");
// call user defined callback function
if (_cb) {
_cb();
} else {
tr_warn("cmti_urc, no user defined callback for receiving sms!");
}
}
nsapi_error_t AT_CellularSMS::set_cnmi()
{
if (!get_property(PROPERTY_AT_CNMI)) {
return NSAPI_ERROR_UNSUPPORTED;
}
return _at.at_cmd_discard("+CNMI", "=2,1");
}
nsapi_error_t AT_CellularSMS::set_cmgf(int msg_format)
{
if (!get_property(PROPERTY_AT_CMGF)) {
return NSAPI_ERROR_UNSUPPORTED;
}
return _at.at_cmd_discard("+CMGF", "=", "%d", msg_format);
}
nsapi_error_t AT_CellularSMS::set_csmp(int fo, int vp, int pid, int dcs)
{
if (!get_property(PROPERTY_AT_CSMP)) {
return NSAPI_ERROR_UNSUPPORTED;
}
return _at.at_cmd_discard("+CSMP", "=", "%d%d%d%d", fo, vp, pid, dcs);
}
nsapi_error_t AT_CellularSMS::set_csdh(int show_header)
{
if (!get_property(PROPERTY_AT_CSDH)) {
return NSAPI_ERROR_UNSUPPORTED;
}
return _at.at_cmd_discard("+CSDH", "=", "%d", show_header);
}
nsapi_error_t AT_CellularSMS::initialize(CellularSMSMmode mode)
{
_at.set_urc_handler("+CMTI:", callback(this, &AT_CellularSMS::cmti_urc));
_at.set_urc_handler("+CMT:", callback(this, &AT_CellularSMS::cmt_urc));
_at.lock();
set_cnmi(); //set new SMS indication
set_cmgf(mode); //set message format/PDU
if (mode == CellularSMSMmodeText) {
set_csmp(FIRST_OCTET_DELIVER_SUBMIT, TP_VALIDITY_PERIOD_24_HOURS, TP_PROTOCOL_IDENTIFIER,
SMS_DATA_CODING_SCHEME); //set Set Text Mode Parameters(default values for SMS-SUBMIT and RECEIVE)
set_csdh(1);//set header extra info as it's needed
}
_mode = mode;
return _at.unlock_return_error();
}
void AT_CellularSMS::set_extra_sim_wait_time(int sim_wait_time)
{
_sim_wait_time = sim_wait_time;
}
char *AT_CellularSMS::create_pdu(const char *phone_number, const char *message, uint8_t message_length, uint8_t msg_parts,
uint8_t msg_part_number, uint8_t &header_size)
{
int totalPDULength = 0;
int number_len = strlen(phone_number);
totalPDULength += number_len;
if (number_len & 0x01) { // if phone number length is not even length we must pad it and so +1
totalPDULength += 1;
}
totalPDULength += 16; // all other than phone number and message length
if (msg_parts > 1) {// add more space for UDH
totalPDULength += 12;
}
// there might be need for padding so some more space
totalPDULength += 2;
// message 7-bit padded and it will be converted to hex so it will take twice as much space
totalPDULength += (message_length - (message_length / 8)) * 2;
char *pdu = new char[totalPDULength];
memset(pdu, 0, totalPDULength);
int x = 0;
// See more how to create PDU from 3GPP specification 23040
// first two define that we use service center number which is set with +CSCA
pdu[x++] = '0';
pdu[x++] = '0';
// First Octet of the TPDU. 41 means SMS SUBMIT, no validity period, no status report, use User Data Header.
// 01 means SMS SUBMIT, no validity period, no status report, NO User Data Header.
if (msg_parts > 1) { // concatenated, must use UDH
pdu[x++] = '4';
} else {
pdu[x++] = '0';
}
pdu[x++] = '1';
// assign a message reference automatically. We have defined TP-RD bit as 0 so duplicates are not rejected.
pdu[x++] = '0';
pdu[x++] = '0';
// [6] and [7] Length of the Destination Phone Number
int_to_hex_str(number_len, pdu + x);
x += 2;
// Type of the Destination Phone Number
pdu[x++] = '8';
pdu[x++] = '1';
// phone number as reverse nibble encoded
int i = 0;
for (; i < number_len; i += 2) {
if (i + 1 == number_len) {
pdu[x++] = 'f';
} else {
pdu[x++] = phone_number[i + 1];
}
pdu[x++] = phone_number[i];
}
// Protocol Identifier
pdu[x++] = '0';
pdu[x++] = '0';
// Data Coding Scheme, GSM 7-bit default alphabet = '00', 8-bit '04'
pdu[x++] = '0';
if (_use_8bit_encoding) {
pdu[x++] = '4';
} else {
pdu[x++] = '0';
}
uint8_t udhlen = 0;
// Length can be update after we have created PDU, store position for later use.
int lengthPos = x;
x += 2;
int paddingBits = 0;
if (msg_parts > 1) { // concatenated, must use UDH
// user data header length in chars
pdu[x++] = '0';
udhlen = 6; // udh length in chars (5) + udhl length in chars
pdu[x++] = '5';
// Information element identifier
pdu[x++] = '0';
pdu[x++] = '0';
// Information element data length
pdu[x++] = '0';
pdu[x++] = '3';
// A reference number (must be the same for all parts of the same larger messages)
int_to_hex_str(_sms_message_ref_number & 0xFF, pdu + x);
x += 2;
// How many parts does this message have?
int_to_hex_str(msg_parts, pdu + x);
x += 2;
// this is a part number
int_to_hex_str(msg_part_number, pdu + x);
x += 2;
// if there is padding bits then udhlen is octet bigger as we need to keep septet boundary
paddingBits = (udhlen * 8) % 7;
if (paddingBits) {
paddingBits = 7 - paddingBits;
udhlen += 1;
}
}
if (_use_8bit_encoding) {
char_str_to_hex_str(message, message_length, pdu + x);
} else {
// we might need to send zero length sms
if (message_length) {
if (pack_7_bit_gsm_and_hex(message, message_length, pdu + x, paddingBits) == 0) {
delete [] pdu;
return NULL;
}
}
}
// now we know the correct length of the UDL (User Data Length)
int_to_hex_str(message_length + udhlen, pdu + lengthPos);
header_size = x;
return pdu;
}
nsapi_size_or_error_t AT_CellularSMS::send_sms(const char *phone_number, const char *message, int msg_len)
{
int single_sms_max_length = _use_8bit_encoding ?
SMS_MAX_SIZE_8BIT_SINGLE_SMS_SIZE :
SMS_MAX_SIZE_GSM7_SINGLE_SMS_SIZE;
if ((_mode == CellularSMSMmodeText && msg_len > single_sms_max_length) || !phone_number) {
return NSAPI_ERROR_PARAMETER;
}
_at.lock();
int write_size = 0;
int remove_plus_sign = (phone_number[0] == '+') ? 1 : 0;
ThisThread::sleep_for(_sim_wait_time);
if (_mode == CellularSMSMmodeText) {
_at.cmd_start_stop("+CMGS", "=", "%s", phone_number + remove_plus_sign);
ThisThread::sleep_for(_sim_wait_time);
_at.resp_start("> ", true);
if (_at.get_last_error() == NSAPI_ERROR_OK) {
write_size = _at.write_bytes((uint8_t *)message, msg_len);
if (write_size < msg_len) {
// sending can be cancelled by giving <ESC> character (IRA 27).
_at.cmd_start(ESC);
_at.cmd_stop();
_at.unlock();
return write_size;
}
// <ctrl-Z> (IRA 26) must be used to indicate the ending of the message body.
_at.cmd_start(CTRL_Z);
_at.cmd_stop();
_at.resp_start("+CMGS:");
_at.resp_stop();
}
} else {
// supports uncompressed 8 bit data and GSM 7 bit default alphabet data. Current implementation uses only
// GSM 7 bit default but support is done for 8 bit data.
int sms_count;
int concatenated_sms_length = _use_8bit_encoding ?
SMS_MAX_8BIT_CONCATENATED_SINGLE_SMS_SIZE :
SMS_MAX_GSM7_CONCATENATED_SINGLE_SMS_SIZE;
if (msg_len <= single_sms_max_length) {
// single message
sms_count = 1;
} else {
// concatenated message
sms_count = msg_len / concatenated_sms_length;
if (msg_len % concatenated_sms_length != 0) {
sms_count++;
}
}
int remaining_len = msg_len;
int pdu_len;
int msg_write_len = 0;
uint8_t header_len;
char *pdu_str;
for (int i = 0; i < sms_count; i++) {
header_len = 0;
if (sms_count == 1) {
pdu_len = msg_len;
} else {
pdu_len = remaining_len > concatenated_sms_length ? concatenated_sms_length : remaining_len;
}
pdu_str = create_pdu(phone_number + remove_plus_sign, message + i * concatenated_sms_length, pdu_len,
sms_count, i + 1, header_len);
if (!pdu_str) {
_at.unlock();
return NSAPI_ERROR_NO_MEMORY;
}
pdu_len = strlen(pdu_str);
// specification says that service center number should not be included so we subtract -2 from pdu_len as we use '00' for automatic service center number
_at.cmd_start_stop("+CMGS", "=", "%d", (pdu_len - 2) / 2);
ThisThread::sleep_for(_sim_wait_time);
_at.resp_start("> ", true);
if (_at.get_last_error() == NSAPI_ERROR_OK) {
write_size = _at.write_bytes((uint8_t *)pdu_str, pdu_len);
if (write_size < pdu_len) {
// calculate exact size of what we have send
if (write_size <= header_len) {
// managed only to write header or some of it so actual msg write size in this iteration is 0
write_size = 0;
} else {
write_size = (write_size - header_len) / 2; // as hex encoded so divide by two
}
msg_write_len += write_size;
// sending can be cancelled by giving <ESC> character (IRA 27).
_at.cmd_start(ESC);
_at.cmd_stop();
_at.unlock();
delete [] pdu_str;
return msg_write_len;
}
// <ctrl-Z> (IRA 26) must be used to indicate the ending of the message body.
_at.cmd_start(CTRL_Z);
_at.cmd_stop();
_at.resp_start("+CMGS:");
_at.resp_stop();
}
delete [] pdu_str;
remaining_len -= concatenated_sms_length;
if (_at.get_last_error() != NSAPI_ERROR_OK) {
return _at.unlock_return_error();
}
}
}
_sms_message_ref_number++;
nsapi_error_t ret = _at.get_last_error();
_at.unlock();
return (ret == NSAPI_ERROR_OK) ? msg_len : ret;
}
void AT_CellularSMS::set_sms_callback(Callback<void()> func)
{
_cb = func;
}
nsapi_error_t AT_CellularSMS::set_cpms(const char *memr, const char *memw, const char *mems)
{
return _at.at_cmd_discard("+CPMS", "=", "%s%s%s", memr, memw, mems);
}
nsapi_error_t AT_CellularSMS::set_csca(const char *sca, int type)
{
return _at.at_cmd_discard("+CSCA", "=", "%s%d", sca, type);
}
nsapi_size_or_error_t AT_CellularSMS::set_cscs(const char *chr_set)
{
return _at.at_cmd_discard("+CSCS", "=", "%s", chr_set);
}
nsapi_error_t AT_CellularSMS::delete_sms(sms_info_t *sms)
{
_at.lock();
for (int i = 0; i < sms->parts; i++) {
_at.at_cmd_discard("+CMGD", "=", "%d", sms->msg_index[i]);
}
return _at.unlock_return_error();
}
// we need this as for example concatenated sms can get different sms reference numbers
// if for example last part take much more time to arrive. This situation happened while testing.
// What this means that after this we can't read another sms because we always read the oldest sms
// that was corrupted. So we need to have delete all messages.
nsapi_error_t AT_CellularSMS::delete_all_messages()
{
return _at.at_cmd_discard("+CMGD", "=1,4");
}
// read msg in text mode
nsapi_size_or_error_t AT_CellularSMS::read_sms_from_index(int msg_index, char *buf, uint16_t len, char *phone_num,
char *time_stamp)
{
/*
* +CMGR: <stat>,<oa>,<alpha>,<scts>[,<tooa>,<fo>,<pid>,<dcs>,<sca>,<tosca>,<length>]<CR><LF><data><CR><LF>OK<CR><LF>
*/
ThisThread::sleep_for(_sim_wait_time);
_at.cmd_start_stop("+CMGR", "=", "%d", msg_index);
// TODO: NOTE: If the selected <mem1> can contain different types of SMs (e.g. SMS-DELIVERs, SMS-SUBMITs, SMS-STATUS-REPORTs and SMS-COMMANDs),
// the response may be a mix of the responses of different SM types. TE application can recognize the response format by examining the third response parameter.
// for now we support sms reading of received messages
int buf_len = 0;
if (_at.get_last_error() == NSAPI_ERROR_OK) {
char status[SMS_STATUS_SIZE];
// first we read msg status and with that we can decide how the rest of message format
_at.resp_start("+CMGR:");
if (_at.info_resp()) {
_at.read_string(status, SMS_STATUS_SIZE);
uint16_t status_len = strlen(status);
if (((status_len == sizeof("REC READ") - 1) && memcmp("REC READ", status, status_len) == 0)
|| ((status_len == sizeof("REC UNREAD") - 1) && memcmp("REC UNREAD", status, status_len) == 0)) {
// Received message
if (phone_num) {
_at.read_string(phone_num, SMS_MAX_PHONE_NUMBER_SIZE);
} else {
_at.skip_param(); // <oa>,<alpha>
}
_at.skip_param(); // <alpha>
if (time_stamp) {
int len = _at.read_string(time_stamp, SMS_MAX_TIME_STAMP_SIZE);
if (len < (SMS_MAX_TIME_STAMP_SIZE - 2)) {
time_stamp[len++] = ',';
_at.read_string(&time_stamp[len], SMS_MAX_TIME_STAMP_SIZE - len);
}
}
(void)_at.consume_to_stop_tag(); // consume until <CR><LF>
if (buf) {
_at.read_string(buf, len);
buf_len = strlen(buf);
}
}
}
_at.resp_stop();
}
return (_at.get_last_error() == NSAPI_ERROR_OK) ? buf_len : _at.get_last_error();
}
// read msg in PDU mode
nsapi_size_or_error_t AT_CellularSMS::read_sms(sms_info_t *sms, char *buf, char *phone_num, char *time_stamp)
{
// +CMGR: <stat>,[<alpha>],<length><CR><LF><pdu>
int index;
if (sms->parts == sms->parts_added) {
char *pdu; // we need a temp buffer as payload is hexencoded ---> can't use buf as it might be enough for message but not hexenconded pdu.
int status;
int msg_len;
index = 0;
int pduSize;
for (int i = 0; i < sms->parts; i++) {
ThisThread::sleep_for(_sim_wait_time);
_at.cmd_start_stop("+CMGR", "=", "%d", sms->msg_index[i]);
_at.resp_start("+CMGR:");
if (_at.info_resp()) {
status = _at.read_int();
_at.skip_param(); // <alpha>
if ((_at.get_last_error() == NSAPI_ERROR_OK) && (status == 0 || status == 1)) {
msg_len = _at.read_int();
if (msg_len > 0) {
pduSize = msg_len * 2 + 20; // *2 as it's hex encoded and +20 as service center number is not included in size given by CMGR
pdu = new char[pduSize];
memset(pdu, 0, pduSize);
if (!pdu) {
_at.resp_stop();
return NSAPI_ERROR_NO_MEMORY;
}
_at.read_string(pdu, pduSize, true);
if (_at.get_last_error() == NSAPI_ERROR_OK) {
msg_len = get_data_from_pdu(pdu, NULL, NULL, phone_num, buf + index);
if (msg_len >= 0) { // we need to allow zero length messages
index += msg_len;
} else {
delete [] pdu;
_at.resp_stop();
return -1;
}
}
delete [] pdu;
}
}
}
_at.resp_stop();
}
if (_at.get_last_error() == NSAPI_ERROR_OK) {
if (time_stamp) {
strcpy(time_stamp, sms->date);
}
buf[index] = '\0';
}
} else {
tr_warn("NOT all concatenated parts were received...");
index = SMS_ERROR_MULTIPART_ALL_PARTS_NOT_READ;
}
return index;
}
nsapi_size_or_error_t AT_CellularSMS::get_sms(char *buf, uint16_t len, char *phone_num, uint16_t phone_len,
char *time_stamp, uint16_t time_len, int *buf_size)
{
// validate buffer sizes already here to avoid any necessary function calls and locking of _at
if ((phone_num && phone_len < SMS_MAX_PHONE_NUMBER_SIZE) || (time_stamp && time_len < SMS_MAX_TIME_STAMP_SIZE) ||
buf == NULL) {
return NSAPI_ERROR_PARAMETER;
}
_at.lock();
nsapi_size_or_error_t err = list_messages();
if (err == NSAPI_ERROR_OK) {
// we return the oldest sms and delete it after successful read
sms_info_t *info = get_oldest_sms_index();
if (info) {
if (info->msg_size > len) {
tr_warn("Given buf too small, len is: %d but is must be: %d", len, info->msg_size);
if (buf_size) {
*buf_size = info->msg_size;
}
free_linked_list();
_at.unlock();
return NSAPI_ERROR_PARAMETER;
}
if (_mode == CellularSMSMmodePDU) {
err = read_sms(info, buf, phone_num, time_stamp);
} else {
err = read_sms_from_index(info->msg_index[0], buf, len, phone_num, time_stamp);
}
if (err > 0) {
int delerr = delete_sms(info);
if (delerr) {
err = delerr;
}
}
} else {
// No messages were found, return -1
err = -1;
}
}
free_linked_list();
_at.unlock();
// update error only when there really was an error, otherwise we return the length
if (_at.get_last_error()) {
err = _at.get_last_error();
}
return err;
}
nsapi_size_or_error_t AT_CellularSMS::get_data_from_pdu(const char *pdu, sms_info_t *info, int *part_number,
char *phone_number, char *msg)
{
int index = 0;
int tmp;
bool userDataHeader;
int oaLength;
int dataScheme;
nsapi_size_or_error_t err = NSAPI_ERROR_OK;
// read Length of the SMSC information
oaLength = hex_str_to_int(pdu, 2);
index += 2; // length we just read
index += oaLength * 2; // skip service center number
// read first the lower part of first octet as there is message type
index++;
tmp = hex_str_to_int(pdu + index, 1);
//ThisThread::sleep_for(200);
if ((tmp & 0x03) == 0) {// SMS-DELIVER type, last two bits should be zero
// UDH present? Check from first octets higher part
tmp = hex_str_to_int(pdu + (--index), 1);
userDataHeader = ((tmp & 0x04) == 0) ? false : true;
index += 2; // we just read the high bits of first octet so move +2
// originating address length
oaLength = hex_str_to_int(pdu + index, 2);
index += 2; // add index over address length
int type = hex_str_to_int(pdu + index, 1);
index += 2; // add index over type
if (phone_number) {
// phone number as reverse nibble encoded
int a = 0, field_length = oaLength;
if (type == 9) {
//add the plus sign in case of international number (23.040 chapter address fields)
phone_number[a++] = '+';
field_length++;
}
for (; a < field_length; a += 2) {
if ((a + 1) == field_length) {
phone_number[a] = pdu[index + 1];
index++;
} else {
phone_number[a] = pdu[index + 1];
phone_number[a + 1] = pdu[index];
index += 2;
}
}
phone_number[field_length] = '\0';
} else {
index += oaLength;
}
if (oaLength & 0x01) { // if phone number length is odd then it has padded F so skip that
index++;
}
index += 2; // skip TP-Protocol identifier
dataScheme = hex_str_to_int(pdu + index, 2);
index += 2; // skip TP-Data-Coding-Scheme
// next one is date, it's length is 7 octets according to 3GPP TS 23.040
// create time string
if (info) {
int i = 0;
// year
info->date[i++] = pdu[index + 1];
info->date[i++] = pdu[index];
index += 2;
info->date[i++] = '/';
// month
info->date[i++] = pdu[index + 1];
info->date[i++] = pdu[index];
index += 2;
info->date[i++] = '/';
// Day
info->date[i++] = pdu[index + 1];
info->date[i++] = pdu[index];
index += 2;
info->date[i++] = ',';
// Hour
info->date[i++] = pdu[index + 1];
info->date[i++] = pdu[index];
index += 2;
info->date[i++] = ':';
// Minute
info->date[i++] = pdu[index + 1];
info->date[i++] = pdu[index];
index += 2;
info->date[i++] = ':';
// Second
info->date[i++] = pdu[index + 1];
info->date[i++] = pdu[index];
index += 2;
// timezone related to GMT. pdu[index+1] most significant bit indicates the sign related to gmt
tmp = hex_str_to_int(pdu + index + 1, 1);
if (tmp & 0x08) {
info->date[i++] = '-';
} else {
info->date[i++] = '+';
}
// pdu[index+1 & 0x07 is the most significant bits of the timezone
// pdu [index] is the least significant bits
info->date[i++] = '0' + (tmp & 0x07);
info->date[i++] = pdu[index];
info->date[i] = '\0';
index += 2;
} else {
index += 14;
}
int udl = hex_str_to_int(pdu + index, 2);
index += 2;
int paddingBits = 0;
int partnro = 1;
if (userDataHeader) {
// we need to read User Defined Header to know what part number this message is.
index += read_udh_from_pdu(pdu + index, info, partnro, paddingBits);
}
if (part_number) {
*part_number = partnro;
}
if (msg) {
// we are reading the message
err = read_pdu_payload(pdu + index, udl, dataScheme, msg, paddingBits);
} else {
if (dataScheme == 0x00) {
// when listing messages we need to calculated length. Other way would be unpacking the whole message.
err = strlen(pdu + index) >> 1;
err *= 8;
err /= 7;
} else if (dataScheme == 0x04) {
err = strlen(pdu + index) >> 1;
} else {
return NSAPI_ERROR_UNSUPPORTED;
}
}
return err;
} else {
// message was not DELIVER so discard it
return NSAPI_ERROR_UNSUPPORTED;
}
}
// read params from User Defined Header
int AT_CellularSMS::read_udh_from_pdu(const char *pdu, sms_info_t *info, int &part_number, int &padding_bits)
{
int index = 0;
int udhLength = hex_str_to_int(pdu, 2);
index += 2;
// if there is padding bits then udhlen is octet bigger as we need to keep septet boundary
padding_bits = ((udhLength + 1) * 8) % 7; // +1 is for udhLength itself
if (padding_bits) {
padding_bits = 7 - padding_bits;
} else {
padding_bits = 0;
}
int tmp = hex_str_to_int(pdu + index, 2);
index += 4;
if (tmp == 0) { // 8-bit reference number
if (info) {
info->msg_ref_number = (uint16_t)hex_str_to_int(pdu + index, 2);
}
index += 2;
} else { // 16-bit reference number
if (info) {
info->msg_ref_number = (uint16_t)hex_str_to_int(pdu + index + 2, 2);
tmp = hex_str_to_int(pdu + index, 2);
info->msg_ref_number |= (tmp << 8);
}
index += 4;
}
if (info) {
info->parts = hex_str_to_int(pdu + index, 2);
}
index += 2;
part_number = hex_str_to_int(pdu + index, 2);
index += 2;
return (udhLength * 2 + 2); // udh in hex and udhl
}
nsapi_size_or_error_t AT_CellularSMS::read_pdu_payload(const char *pdu, int msg_len, int scheme, char *msg, int padding_bits)
{
if (scheme == 0x00) {
// 7 bit gsm encoding, must do the conversions from hex to 7-bit encoding and to ascii
return unpack_7_bit_gsm_to_str(pdu, strlen(pdu) / 2, msg, padding_bits, msg_len);
} else if (scheme == 0x04) {
// 8bit scheme so just convert hexstring to charstring
return hex_str_to_char_str(pdu, strlen(pdu), msg);
} else {
tr_error("Received unsupported data coding scheme: 0x%02x", scheme);
return NSAPI_ERROR_UNSUPPORTED;
}
}
void AT_CellularSMS::free_linked_list()
{
sms_info_t *info = _sms_info;
sms_info_t *old;
while (info) {
old = info;
info = info->next_info;
delete old;
}
_sms_info = NULL;
}
void AT_CellularSMS::add_info(sms_info_t *info, int index, int part_number)
{
// check for same message reference id. If found, update it and delete the given info.
// if NOT found then add to the end of the list.
if (!_sms_info) {
info->msg_index[part_number - 1] = index; // part numbering starts from 1 so -1 to put to right index
_sms_info = info;
return;
}
sms_info_t *current = _sms_info;
sms_info_t *prev = NULL;
bool found_msg = false;
while (current) {
prev = current;
// sms messages can have same reference number so additional checks are needed.
// TODO: should we include phone number also?
if (current->msg_ref_number == info->msg_ref_number && current->parts > current->parts_added &&
info->parts > info->parts_added) {
// multipart sms, update msg size and index
current->msg_size += info->msg_size;
current->msg_index[part_number - 1] = index; // part numbering starts from 1 so -1 to put to right index
current->parts_added++;
// update oldest part as date
if (compare_time_strings(info->date, current->date) == -1) {
strcpy(current->date, info->date);
}
found_msg = true;
break;
}
current = current->next_info;
}
if (found_msg) {
// info was added to existing item in linked list, must be deleted
delete info;
} else {
// message not found, add to linked list
info->msg_index[part_number - 1] = index;
prev->next_info = info;
}
}
// reads all the messages to the linked list AT_CellularSMS::_sms_info
nsapi_error_t AT_CellularSMS::list_messages()
{
// TODO: NOTE: If the selected <mem1> can contain different types of SMs (e.g. SMS-DELIVERs, SMS-SUBMITs, SMS-STATUS-REPORTs and SMS-COMMANDs),
// the response may be a mix of the responses of different SM types. TE application can recognize the response format by examining the third response parameter.