/
le_sms.c
5300 lines (4611 loc) · 166 KB
/
le_sms.c
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
/** @file le_sms.c
*
* Legato @ref c_sms implementation.
*
* The Modem is initialized to operate in PDU mode: all messages are sent and received in PDU
* format.
*
* The SMS module's initialization installs an internal handler for message reception. This handler
* decodes the PDU message, then it creates and populates a new message object and finally notifies
* all the registered client's handlers.
*
* All the messages are stored in a le_sms_Msg_t data structure. The message object is always queued
* to the main 'MsgList' list.
* In case of listing the received messages (see le_sms_CreateRxMsgList()), the message objects
* are queued to the 'StoredRxMsgList' as well.
*
* The sending case:
* The message object must be created by the client. The client can populate the message with the
* 'setters functions' like le_sms_SetDestination() and le_sms_SetText().
* Then, the client must call le_sms_Send() to actually send the message. le_sms_Send()
* first verifies the consistency of the main elements of the object like the telephone number or
* the message length, then it encodes the message in PDU if it is a Text or a Binary message, and
* finally it forwards the message to the modem for sending.
*
* Copyright (C) Sierra Wireless Inc.
*
*/
#include "legato.h"
#include "interfaces.h"
#include "pa_sms.h"
#include "pa_sim.h"
#include "smsPdu.h"
#include "time.h"
#include "mdmCfgEntries.h"
#include "le_ms_local.h"
#include "watchdogChain.h"
//--------------------------------------------------------------------------------------------------
// Symbols and enums.
//--------------------------------------------------------------------------------------------------
#ifdef MK_CONFIG_SMS_LIGHT
#ifndef MK_CONFIG_DISABLE_SMS_INDICATION
//--------------------------------------------------------------------------------------------------
/**
* Maximum number of session objects we expect to have at one time.
*/
//--------------------------------------------------------------------------------------------------
#define SMS_MAX_SESSION 1
//--------------------------------------------------------------------------------------------------
/**
* HandlerCtx structure
*
*/
//--------------------------------------------------------------------------------------------------
typedef struct
{
le_sms_RxMessageHandlerRef_t handlerRef; ///< Handler reference.
le_sms_RxMessageHandlerFunc_t handlerFuncPtr; ///< Handler function.
void* userContext; ///< handler context.
}
HandlerCtx_t;
//--------------------------------------------------------------------------------------------------
/**
* Static memory pool for handlers context.
*/
//--------------------------------------------------------------------------------------------------
LE_MEM_DEFINE_STATIC_POOL(Handler, SMS_MAX_SESSION, sizeof(HandlerCtx_t));
//--------------------------------------------------------------------------------------------------
/**
* Memory Pool for RX handlers context.
*
*/
//--------------------------------------------------------------------------------------------------
static le_mem_PoolRef_t HandlerPool;
//--------------------------------------------------------------------------------------------------
/**
* Message Ref
*
*/
//--------------------------------------------------------------------------------------------------
static le_sms_MsgRef_t MsgRef;
//--------------------------------------------------------------------------------------------------
/**
* The handler's context.
*
*/
//--------------------------------------------------------------------------------------------------
static HandlerCtx_t* HandlerCtxPtr = NULL;
#endif // MK_CONFIG_DISABLE_SMS_INDICATION
#else // MK_CONFIG_SMS_LIGHT
//--------------------------------------------------------------------------------------------------
/**
* Maximum Message IDs returned by the List SMS messages command.
*
*/
//--------------------------------------------------------------------------------------------------
#define MAX_NUM_OF_SMS_MSG_IN_STORAGE 256
//--------------------------------------------------------------------------------------------------
/**
* Maximum number of Message objects we expect to have at one time.
* GSM SMS in the SIM and memory, CDMA SMS in the SIM and memory.
*/
//--------------------------------------------------------------------------------------------------
#define MAX_NUM_OF_SMS_MSG (MAX_NUM_OF_SMS_MSG_IN_STORAGE*4)
//--------------------------------------------------------------------------------------------------
/**
* Maximum number of Message List objects we expect to have at one time.
*/
//--------------------------------------------------------------------------------------------------
#define MAX_NUM_OF_LIST 128
//--------------------------------------------------------------------------------------------------
/**
* Maximum number of session objects we expect to have at one time.
*/
//--------------------------------------------------------------------------------------------------
#define SMS_MAX_SESSION 5
//--------------------------------------------------------------------------------------------------
/**
* SMS command Type.
*
*/
//--------------------------------------------------------------------------------------------------
typedef enum
{
LE_SMS_CMD_TYPE_SEND = 0 ///< Pool and send SMS message.
}
CmdType_t;
//--------------------------------------------------------------------------------------------------
// Data structures.
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
/**
* Message structure.
* Objects of this type are used to define a message.
*
* Note: I keep both PDU and UserData since PDU can be requested by the client's app even after
* message decoding.
*
*/
//--------------------------------------------------------------------------------------------------
typedef struct le_sms_Msg
{
bool readonly; ///< Flag for Read-Only message.
bool inAList; ///< Is the message belong to a list?
le_sms_Format_t format; ///< SMS Message Format.
le_sms_Type_t type; ///< SMS Message Type.
uint32_t storageIdx; ///< SMS Message index in storage.
char tel[LE_MDMDEFS_PHONE_NUM_MAX_BYTES]; ///< Telephone number of the message
///< (in text mode),
///< or NULL (in PDU mode).
char timestamp[LE_SMS_TIMESTAMP_MAX_BYTES]; ///< SMS time stamp (in text mode).
pa_sms_Pdu_t pdu; ///< SMS PDU.
bool pduReady; ///< Is the PDU value ready?
union
{
char text[LE_SMS_TEXT_MAX_BYTES]; ///< SMS text.
uint8_t binary[LE_SMS_BINARY_MAX_BYTES]; ///< SMS binary.
};
size_t userdataLen; ///< Length of data associated with SMS.
/// formats text or binary
pa_sms_Protocol_t protocol; ///< SMS Protocol (GSM or CDMA).
int32_t smsUserCount; ///< Current sms user counter.
bool delAsked; ///< Whether the SMS deletion is asked.
pa_sms_Storage_t storage; ///< SMS storage location.
/// SMS Cell Broadcast parameters
uint16_t messageId; ///< SMS Cell Broadcast message Id.
uint16_t messageSerialNumber; ///< SMS Cell Broadcast message Serial
///< Number.
/// SMS callback parameters
void* callBackPtr; ///< Callback response.
void* ctxPtr; ///< Context.
le_msg_SessionRef_t sessionRef; ///< Client session reference.
/// SMS Status Report parameters
uint8_t messageReference; ///< TP Message Reference
uint8_t typeOfAddress; ///< Type of Address
char dischargeTime[LE_SMS_TIMESTAMP_MAX_BYTES]; ///< TP Discharge Time
uint8_t status; ///< TP Status
}le_sms_Msg_t;
//--------------------------------------------------------------------------------------------------
/**
* Data structure to keep a list of the references created with 'CreateRxMsgList' function.
*
*/
//--------------------------------------------------------------------------------------------------
typedef struct le_sms_MsgReference
{
le_sms_MsgRef_t msgRef; ///< The message reference.
le_dls_Link_t listLink; ///< Object node link (for msg listing).
}le_sms_MsgReference_t;
//--------------------------------------------------------------------------------------------------
/**
* List message structure.
*
*/
//--------------------------------------------------------------------------------------------------
typedef struct le_sms_MsgList
{
le_sms_MsgListRef_t msgListRef; ///< Message list reference.
le_msg_SessionRef_t sessionRef; ///< Client session reference.
le_dls_List_t list; ///< Link list to insert new message object.
le_dls_Link_t* currentLink; ///< Link list pointed to current message object.
}le_sms_List_t;
//--------------------------------------------------------------------------------------------------
/**
* Sms message sending command structure.
*
*/
//--------------------------------------------------------------------------------------------------
typedef struct
{
CmdType_t command; ///< The command.
le_sms_MsgRef_t msgRef; ///< The message reference.
} CmdRequest_t;
//--------------------------------------------------------------------------------------------------
/**
* Data structure for message statistics.
*/
//--------------------------------------------------------------------------------------------------
typedef struct
{
bool counting; ///< Is message counting activated.
int32_t rxCount; ///< Number of messages successfully received.
int32_t rxCbCount; ///< Number of broadcast messages successfully received.
int32_t txCount; ///< Number of messages successfully sent.
}
le_sms_MsgStats_t;
//--------------------------------------------------------------------------------------------------
/**
* session context node structure used for the SessionCtxList list.
*
* @note The goal is to create a sms database to be able to find all sms created or allocated for a
* dedicated app and delete them in case of an app crash.
*/
//--------------------------------------------------------------------------------------------------
typedef struct
{
le_msg_SessionRef_t sessionRef; ///< Client sessionRef.
le_dls_List_t msgRefList; ///< Message reference list.
le_dls_List_t handlerList; ///< Handler list.
le_dls_Link_t link; ///< Link for SessionCtxList.
}
SessionCtxNode_t;
//--------------------------------------------------------------------------------------------------
/**
* HandlerCtx node structure used for the handlerList list.
*
*/
//--------------------------------------------------------------------------------------------------
typedef struct
{
le_sms_RxMessageHandlerRef_t handlerRef; ///< Handler reference.
le_sms_RxMessageHandlerFunc_t handlerFuncPtr; ///< Handler function.
void* userContext; ///< User context.
SessionCtxNode_t* sessionCtxPtr; ///< Session context relative to this handler ctx.
le_dls_Link_t link; ///< Link for handlerList.
}
HandlerCtxNode_t;
//--------------------------------------------------------------------------------------------------
/**
* msgRef node structure used for the msgRefList list.
*
*/
//--------------------------------------------------------------------------------------------------
typedef struct
{
le_sms_MsgRef_t msgRef; ///< The message reference.
le_dls_Link_t link; ///< Link for msgRefList.
}
MsgRefNode_t;
//--------------------------------------------------------------------------------------------------
// Static declarations
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
/**
* Static memory pool for SMS messages.
*/
//--------------------------------------------------------------------------------------------------
LE_MEM_DEFINE_STATIC_POOL(SmsMsg, MAX_NUM_OF_SMS_MSG, sizeof(le_sms_Msg_t));
//--------------------------------------------------------------------------------------------------
/**
* Memory Pool for SMS messages.
*
*/
//--------------------------------------------------------------------------------------------------
static le_mem_PoolRef_t MsgPool;
//--------------------------------------------------------------------------------------------------
/**
* Static safe Reference Map for Message objects.
*
*/
//--------------------------------------------------------------------------------------------------
LE_REF_DEFINE_STATIC_MAP(SmsMsgMap, MAX_NUM_OF_SMS_MSG);
//--------------------------------------------------------------------------------------------------
/**
* Safe Reference Map for Message objects.
*
*/
//--------------------------------------------------------------------------------------------------
static le_ref_MapRef_t MsgRefMap;
//--------------------------------------------------------------------------------------------------
/**
* Static memory pool for listed SMS messages.
*/
//--------------------------------------------------------------------------------------------------
LE_MEM_DEFINE_STATIC_POOL(ListSms, MAX_NUM_OF_LIST, sizeof(le_sms_List_t));
//--------------------------------------------------------------------------------------------------
/**
* Memory Pool for Listed SMS messages.
*
*/
//--------------------------------------------------------------------------------------------------
static le_mem_PoolRef_t ListPool;
//--------------------------------------------------------------------------------------------------
/**
* Static safe Reference Map for List objects.
*
*/
//--------------------------------------------------------------------------------------------------
LE_REF_DEFINE_STATIC_MAP(ListSmsMap, MAX_NUM_OF_LIST);
//--------------------------------------------------------------------------------------------------
/**
* Safe Reference Map for List objects.
*
*/
//--------------------------------------------------------------------------------------------------
static le_ref_MapRef_t ListRefMap;
//--------------------------------------------------------------------------------------------------
/**
* Static pool for message references.
*/
//--------------------------------------------------------------------------------------------------
LE_MEM_DEFINE_STATIC_POOL(SmsReference,
MAX_NUM_OF_SMS_MSG,
sizeof(le_sms_MsgReference_t));
//--------------------------------------------------------------------------------------------------
/**
* Memory Pool for message references.
*
*/
//--------------------------------------------------------------------------------------------------
static le_mem_PoolRef_t ReferencePool;
#ifndef MK_CONFIG_DISABLE_SMS_INDICATION
//--------------------------------------------------------------------------------------------------
/**
* Static memory pool for handlers context.
*/
//--------------------------------------------------------------------------------------------------
LE_MEM_DEFINE_STATIC_POOL(Handler,
SMS_MAX_SESSION,
sizeof(HandlerCtxNode_t));
//--------------------------------------------------------------------------------------------------
/**
* Memory Pool for handlers context.
*
*/
//--------------------------------------------------------------------------------------------------
static le_mem_PoolRef_t HandlerPool;
#endif
//--------------------------------------------------------------------------------------------------
/**
* Static memory pool for sessions context.
*/
//--------------------------------------------------------------------------------------------------
LE_MEM_DEFINE_STATIC_POOL(SessionCtx,
SMS_MAX_SESSION,
sizeof(SessionCtxNode_t));
//--------------------------------------------------------------------------------------------------
/**
* Memory Pool for sessions context.
*
*/
//--------------------------------------------------------------------------------------------------
static le_mem_PoolRef_t SessionCtxPool;
//--------------------------------------------------------------------------------------------------
/**
* Static pool for msgRef context.
*
*/
//--------------------------------------------------------------------------------------------------
LE_MEM_DEFINE_STATIC_POOL(MsgRef,
SMS_MAX_SESSION*MAX_NUM_OF_SMS_MSG,
sizeof(MsgRefNode_t));
//--------------------------------------------------------------------------------------------------
/**
* Memory Pool for msgRef context.
*
*/
//--------------------------------------------------------------------------------------------------
static le_mem_PoolRef_t MsgRefPool;
#ifndef MK_CONFIG_DISABLE_SMS_INDICATION
//--------------------------------------------------------------------------------------------------
/**
* Static safe Reference Map for handlers objects.
*
*/
//--------------------------------------------------------------------------------------------------
LE_REF_DEFINE_STATIC_MAP(HandlerRefMap, SMS_MAX_SESSION);
//--------------------------------------------------------------------------------------------------
/**
* Safe Reference Map for handlers objects.
*
*/
//--------------------------------------------------------------------------------------------------
static le_ref_MapRef_t HandlerRefMap;
#endif
//--------------------------------------------------------------------------------------------------
/**
* Event ID for SMS storage message notification.
*
*/
//--------------------------------------------------------------------------------------------------
static le_event_Id_t StorageStatusEventId;
//--------------------------------------------------------------------------------------------------
/**
* Event ID for message sending commands.
*/
//--------------------------------------------------------------------------------------------------
static le_event_Id_t SmsCommandEventId;
//--------------------------------------------------------------------------------------------------
/**
* Semaphore to synchronize threads.
*/
//--------------------------------------------------------------------------------------------------
static le_sem_Ref_t SmsSem;
//--------------------------------------------------------------------------------------------------
/**
* Structure for message statistics.
*/
//--------------------------------------------------------------------------------------------------
static le_sms_MsgStats_t MessageStats;
//--------------------------------------------------------------------------------------------------
/**
* SMS Status Report activation state.
*/
//--------------------------------------------------------------------------------------------------
static bool StatusReportActivation = false;
//--------------------------------------------------------------------------------------------------
/**
* list of session context.
*
*/
//--------------------------------------------------------------------------------------------------
static le_dls_List_t SessionCtxList;
//--------------------------------------------------------------------------------------------------
/**
* Read the message counting state
*/
//--------------------------------------------------------------------------------------------------
static bool GetCountingState
(
void
)
{
#ifndef LE_CONFIG_ENABLE_CONFIG_TREE
return false;
#else
bool countingState;
le_cfg_IteratorRef_t iteratorRef;
iteratorRef = le_cfg_CreateReadTxn(CFG_MODEMSERVICE_SMS_PATH);
countingState = le_cfg_GetBool(iteratorRef, CFG_NODE_COUNTING, true);
le_cfg_CancelTxn(iteratorRef);
LE_DEBUG("Retrieved counting state: %d", countingState);
return countingState;
#endif
}
//--------------------------------------------------------------------------------------------------
/**
* Write the message counting state
*/
//--------------------------------------------------------------------------------------------------
static void SetCountingState
(
bool countState ///< New message counting state
)
{
#ifdef LE_CONFIG_ENABLE_CONFIG_TREE
le_cfg_IteratorRef_t iteratorRef;
LE_DEBUG("New message counting state: %d", countState);
iteratorRef = le_cfg_CreateWriteTxn(CFG_MODEMSERVICE_SMS_PATH);
le_cfg_SetBool(iteratorRef, CFG_NODE_COUNTING, countState);
le_cfg_CommitTxn(iteratorRef);
MessageStats.counting = countState;
#endif
}
//--------------------------------------------------------------------------------------------------
/**
* Read the message count for a message type
*/
//--------------------------------------------------------------------------------------------------
static le_result_t GetMessageCount
(
le_sms_Type_t messageType, ///< [IN] Message type
int32_t* messageCountPtr ///< [OUT] Message count pointer
)
{
#ifndef LE_CONFIG_ENABLE_CONFIG_TREE
return LE_FAULT;
#else
le_cfg_IteratorRef_t iteratorRef;
char countPath[LE_CFG_STR_LEN_BYTES];
switch (messageType)
{
case LE_SMS_TYPE_RX:
snprintf(countPath, sizeof(countPath), "%s", CFG_NODE_RX_COUNT);
break;
case LE_SMS_TYPE_TX:
snprintf(countPath, sizeof(countPath), "%s", CFG_NODE_TX_COUNT);
break;
case LE_SMS_TYPE_BROADCAST_RX:
snprintf(countPath, sizeof(countPath), "%s", CFG_NODE_RX_CB_COUNT);
break;
default:
LE_ERROR("Unknown message type %d", messageType);
return LE_FAULT;
}
iteratorRef = le_cfg_CreateReadTxn(CFG_MODEMSERVICE_SMS_PATH);
*messageCountPtr = le_cfg_GetInt(iteratorRef, countPath, 0);
le_cfg_CancelTxn(iteratorRef);
LE_DEBUG("Type=%d, count=%d", messageType, *messageCountPtr);
return LE_OK;
#endif
}
//--------------------------------------------------------------------------------------------------
/**
* Write the message count for a message type
*/
//--------------------------------------------------------------------------------------------------
static le_result_t SetMessageCount
(
le_sms_Type_t messageType, ///< [IN] Message type
int32_t messageCount ///< [IN] New message count
)
{
#ifndef LE_CONFIG_ENABLE_CONFIG_TREE
return LE_FAULT;
#else
le_cfg_IteratorRef_t iteratorRef;
char countPath[LE_CFG_STR_LEN_BYTES];
switch (messageType)
{
case LE_SMS_TYPE_RX:
MessageStats.rxCount = messageCount;
snprintf(countPath, sizeof(countPath), "%s", CFG_NODE_RX_COUNT);
break;
case LE_SMS_TYPE_TX:
MessageStats.txCount = messageCount;
snprintf(countPath, sizeof(countPath), "%s", CFG_NODE_TX_COUNT);
break;
case LE_SMS_TYPE_BROADCAST_RX:
MessageStats.rxCbCount = messageCount;
snprintf(countPath, sizeof(countPath), "%s", CFG_NODE_RX_CB_COUNT);
break;
default:
LE_ERROR("Unknown message type %d", messageType);
return LE_FAULT;
}
iteratorRef = le_cfg_CreateWriteTxn(CFG_MODEMSERVICE_SMS_PATH);
le_cfg_SetInt(iteratorRef, countPath, messageCount);
le_cfg_CommitTxn(iteratorRef);
LE_DEBUG("Type=%d, count=%d", messageType, messageCount);
return LE_OK;
#endif
}
//--------------------------------------------------------------------------------------------------
/**
* Initialize message statistics structure
*/
//--------------------------------------------------------------------------------------------------
static void InitializeMessageStatistics
(
void
)
{
MessageStats.counting = GetCountingState();
if (LE_OK != GetMessageCount(LE_SMS_TYPE_RX, &MessageStats.rxCount))
{
LE_ERROR("Unable to retrieve received message count");
}
if (LE_OK != GetMessageCount(LE_SMS_TYPE_TX, &MessageStats.txCount))
{
LE_ERROR("Unable to retrieve sent message count");
}
if (LE_OK != GetMessageCount(LE_SMS_TYPE_BROADCAST_RX, &MessageStats.rxCbCount))
{
LE_ERROR("Unable to retrieve received broadcast message count");
}
}
//--------------------------------------------------------------------------------------------------
/**
* Read the SMS Status Report activation state
*/
//--------------------------------------------------------------------------------------------------
static bool GetStatusReportState
(
void
)
{
#ifndef LE_CONFIG_ENABLE_CONFIG_TREE
return false;
#else
bool statusReportState;
le_cfg_IteratorRef_t iteratorRef;
iteratorRef = le_cfg_CreateReadTxn(CFG_MODEMSERVICE_SMS_PATH);
statusReportState = le_cfg_GetBool(iteratorRef, CFG_NODE_STATUS_REPORT, false);
le_cfg_CancelTxn(iteratorRef);
LE_DEBUG("Retrieved Status Report state: %d", statusReportState);
return statusReportState;
#endif
}
//--------------------------------------------------------------------------------------------------
/**
* Write the SMS Status Report activation state
*/
//--------------------------------------------------------------------------------------------------
static void SetStatusReportState
(
bool statusReportState ///< New SMS Status Report activation state
)
{
#ifdef LE_CONFIG_ENABLE_CONFIG_TREE
le_cfg_IteratorRef_t iteratorRef;
LE_DEBUG("New Status Report state: %d", statusReportState);
iteratorRef = le_cfg_CreateWriteTxn(CFG_MODEMSERVICE_SMS_PATH);
le_cfg_SetBool(iteratorRef, CFG_NODE_STATUS_REPORT, statusReportState);
le_cfg_CommitTxn(iteratorRef);
StatusReportActivation = statusReportState;
#endif
}
//--------------------------------------------------------------------------------------------------
/**
* Re-initialize a List.
*
*/
//--------------------------------------------------------------------------------------------------
static void ReInitializeList
(
le_dls_List_t* msgListPtr
)
{
le_sms_MsgReference_t* nodePtr;
le_dls_Link_t *linkPtr;
linkPtr = le_dls_Pop(msgListPtr);
if (linkPtr != NULL)
{
do
{
// Get the node from MsgList.
nodePtr = CONTAINER_OF(linkPtr, le_sms_MsgReference_t, listLink);
le_sms_Msg_t* msgPtr = le_ref_Lookup(MsgRefMap, nodePtr->msgRef);
if (msgPtr == NULL)
{
LE_CRIT("Invalid reference (%p) provided!", nodePtr);
return;
}
LE_DEBUG("ReInitializeList node 0x%p, obj 0X%p, ref 0x%p, flag %c cpt = %d",
nodePtr, msgPtr, nodePtr->msgRef,
(msgPtr->delAsked ? 'Y' : 'N'), msgPtr->smsUserCount);
if (msgPtr->delAsked)
{
le_sms_DeleteFromStorage(nodePtr->msgRef);
}
msgPtr->smsUserCount --;
// Invalidate the Safe Reference.
le_ref_DeleteRef(MsgRefMap, nodePtr->msgRef);
// release the message object.
le_mem_Release(msgPtr);
// Move to the next node.
linkPtr = le_dls_Pop(msgListPtr);
// Release the node.
le_mem_Release(nodePtr);
} while (linkPtr != NULL);
}
}
//--------------------------------------------------------------------------------------------------
/**
* Create and Populate a new message object from an unknown PDU encoding.
*
*/
//--------------------------------------------------------------------------------------------------
static le_sms_Msg_t* CreateMessage
(
uint32_t storageIdx, ///< [IN] Storage index.
pa_sms_Pdu_t* pduMsgPtr ///< [IN] PDU message.
)
{
// Create and populate the SMS message object (it is Read Only).
le_sms_Msg_t *newSmsMsgObjPtr;
// Create the message node.
newSmsMsgObjPtr = (le_sms_Msg_t*)le_mem_ForceAlloc(MsgPool);
memset(newSmsMsgObjPtr, 0, sizeof(le_sms_Msg_t));
newSmsMsgObjPtr->pdu.status = LE_SMS_STATUS_UNKNOWN;
newSmsMsgObjPtr->pdu.errorCode.code3GPP2 = LE_SMS_ERROR_3GPP2_MAX;
newSmsMsgObjPtr->pdu.errorCode.rp = LE_SMS_ERROR_3GPP_MAX;
newSmsMsgObjPtr->pdu.errorCode.tp = LE_SMS_ERROR_3GPP_MAX;
newSmsMsgObjPtr->readonly = true;
newSmsMsgObjPtr->storageIdx = storageIdx;
newSmsMsgObjPtr->type = LE_SMS_TYPE_RX;
newSmsMsgObjPtr->format = LE_SMS_FORMAT_PDU;
/* Save the protocol */
newSmsMsgObjPtr->protocol = pduMsgPtr->protocol;
/* Copy PDU */
memcpy(&(newSmsMsgObjPtr->pdu), pduMsgPtr, sizeof(pa_sms_Pdu_t));
newSmsMsgObjPtr->pduReady = true;
return (newSmsMsgObjPtr);
}
//--------------------------------------------------------------------------------------------------
/**
* Populate a new message object from a SMS-DELIVER PDU.
*/
//--------------------------------------------------------------------------------------------------
static le_result_t PopulateSmsDeliver
(
le_sms_Msg_t* newSmsMsgObjPtr, ///< [IN] Message object pointer.
pa_sms_Pdu_t* pduMsgPtr, ///< [IN] PDU message.
pa_sms_Message_t* decodedMsgPtr ///< [IN] Decoded Message.
)
{
newSmsMsgObjPtr->type = LE_SMS_TYPE_RX;
newSmsMsgObjPtr->format = decodedMsgPtr->smsDeliver.format;
switch (newSmsMsgObjPtr->format)
{
case LE_SMS_FORMAT_PDU:
LE_WARN_IF((pduMsgPtr->dataLen > LE_SMS_PDU_MAX_BYTES),
"pduMsgPtr->dataLen=%d > LE_SMS_PDU_MAX_BYTES=%d",
pduMsgPtr->dataLen, LE_SMS_PDU_MAX_BYTES);
break;
case LE_SMS_FORMAT_BINARY:
if (decodedMsgPtr->smsDeliver.dataLen > LE_SMS_BINARY_MAX_BYTES)
{
LE_WARN("smsDeliver.dataLen=%d > LE_SMS_BINARY_MAX_BYTES=%d",
decodedMsgPtr->smsDeliver.dataLen, LE_SMS_BINARY_MAX_BYTES);
newSmsMsgObjPtr->userdataLen = LE_SMS_BINARY_MAX_BYTES;
}
else
{
newSmsMsgObjPtr->userdataLen = decodedMsgPtr->smsDeliver.dataLen;
}
memcpy(newSmsMsgObjPtr->binary,
decodedMsgPtr->smsDeliver.data,
newSmsMsgObjPtr->userdataLen);
break;
case LE_SMS_FORMAT_TEXT:
if (decodedMsgPtr->smsDeliver.dataLen > LE_SMS_TEXT_MAX_BYTES)
{
LE_WARN("smsDeliver.dataLen=%d > LE_SMS_TEXT_MAX_BYTES=%d",
decodedMsgPtr->smsDeliver.dataLen, LE_SMS_TEXT_MAX_BYTES);
newSmsMsgObjPtr->userdataLen = LE_SMS_TEXT_MAX_BYTES;
}
else
{
newSmsMsgObjPtr->userdataLen = decodedMsgPtr->smsDeliver.dataLen;
}
memcpy(newSmsMsgObjPtr->text,
decodedMsgPtr->smsDeliver.data,
newSmsMsgObjPtr->userdataLen);
break;
case LE_SMS_FORMAT_UCS2:
if (decodedMsgPtr->smsDeliver.dataLen > LE_SMS_UCS2_MAX_BYTES)
{
LE_WARN("smsDeliver.dataLen=%d > LE_SMS_UCS2_MAX_BYTES=%d",
decodedMsgPtr->smsDeliver.dataLen, LE_SMS_UCS2_MAX_BYTES);
newSmsMsgObjPtr->userdataLen = LE_SMS_UCS2_MAX_BYTES;
}
else
{
newSmsMsgObjPtr->userdataLen = decodedMsgPtr->smsDeliver.dataLen;
}
memcpy(newSmsMsgObjPtr->binary,
decodedMsgPtr->smsDeliver.data,
newSmsMsgObjPtr->userdataLen);
break;
default:
LE_CRIT("Unknown SMS format %d", newSmsMsgObjPtr->format);
return LE_FAULT;
}
if (newSmsMsgObjPtr->format != LE_SMS_FORMAT_PDU)
{
if (decodedMsgPtr->smsDeliver.option & PA_SMS_OPTIONMASK_OA)
{
memcpy(newSmsMsgObjPtr->tel,
decodedMsgPtr->smsDeliver.oa,
LE_MDMDEFS_PHONE_NUM_MAX_BYTES);
}
else
{
newSmsMsgObjPtr->tel[0] = '\0';
}
if (decodedMsgPtr->smsDeliver.option & PA_SMS_OPTIONMASK_SCTS)
{
memcpy(newSmsMsgObjPtr->timestamp,
decodedMsgPtr->smsDeliver.scts,
LE_SMS_TIMESTAMP_MAX_BYTES);
}
else
{
newSmsMsgObjPtr->timestamp[0] = '\0';
}
}
return LE_OK;
}
//--------------------------------------------------------------------------------------------------
/**
* Populate a new message object from a Cell Broadcast PDU.
*/
//--------------------------------------------------------------------------------------------------
static le_result_t PopulateSmsCellBroadcast
(
le_sms_Msg_t* newSmsMsgObjPtr, ///< [IN] Message object pointer.
pa_sms_Pdu_t* pduMsgPtr, ///< [IN] PDU message.
pa_sms_Message_t* decodedMsgPtr ///< [IN] Decoded Message.
)
{
newSmsMsgObjPtr->type = LE_SMS_TYPE_BROADCAST_RX;
newSmsMsgObjPtr->format = decodedMsgPtr->cellBroadcast.format;
memcpy(newSmsMsgObjPtr->pdu.data, pduMsgPtr->data, pduMsgPtr->dataLen);
newSmsMsgObjPtr->messageId = decodedMsgPtr->cellBroadcast.mId;
newSmsMsgObjPtr->messageSerialNumber = decodedMsgPtr->cellBroadcast.serialNum;
newSmsMsgObjPtr->pduReady = true;
switch (newSmsMsgObjPtr->format)
{
case LE_SMS_FORMAT_PDU:
LE_WARN_IF((pduMsgPtr->dataLen > LE_SMS_PDU_MAX_BYTES),
"pduMsgPtr->dataLen=%d > LE_SMS_PDU_MAX_BYTES=%d",
pduMsgPtr->dataLen, LE_SMS_PDU_MAX_BYTES);
break;
case LE_SMS_FORMAT_BINARY:
if (decodedMsgPtr->cellBroadcast.dataLen > LE_SMS_BINARY_MAX_BYTES)
{
LE_WARN("cellBroadcast.dataLen=%d > LE_SMS_BINARY_MAX_BYTES=%d",
decodedMsgPtr->cellBroadcast.dataLen, LE_SMS_BINARY_MAX_BYTES);
newSmsMsgObjPtr->userdataLen = LE_SMS_BINARY_MAX_BYTES;
}
else
{
newSmsMsgObjPtr->userdataLen = decodedMsgPtr->cellBroadcast.dataLen;
}
memcpy(newSmsMsgObjPtr->binary,
decodedMsgPtr->cellBroadcast.data,
newSmsMsgObjPtr->userdataLen);
break;
case LE_SMS_FORMAT_TEXT:
if (decodedMsgPtr->cellBroadcast.dataLen > LE_SMS_TEXT_MAX_BYTES)
{
LE_WARN("cellBroadcast.dataLen=%d > LE_SMS_TEXT_MAX_BYTES=%d",
decodedMsgPtr->cellBroadcast.dataLen, LE_SMS_TEXT_MAX_BYTES);
newSmsMsgObjPtr->userdataLen = LE_SMS_TEXT_MAX_BYTES;
}
else
{
newSmsMsgObjPtr->userdataLen = decodedMsgPtr->cellBroadcast.dataLen;
}
memcpy(newSmsMsgObjPtr->text,
decodedMsgPtr->cellBroadcast.data,
newSmsMsgObjPtr->userdataLen);
break;
case LE_SMS_FORMAT_UCS2:
if (decodedMsgPtr->cellBroadcast.dataLen > LE_SMS_UCS2_MAX_BYTES)
{
LE_WARN("cellBroadcast.dataLen=%d > LE_SMS_UCS2_MAX_BYTES=%d",
decodedMsgPtr->cellBroadcast.dataLen, LE_SMS_UCS2_MAX_BYTES);
newSmsMsgObjPtr->userdataLen = LE_SMS_UCS2_MAX_BYTES;
}
else
{
newSmsMsgObjPtr->userdataLen = decodedMsgPtr->cellBroadcast.dataLen;
}
memcpy(newSmsMsgObjPtr->binary,
decodedMsgPtr->cellBroadcast.data,