Initialize Tizen 2.3
[framework/telephony/libslp-tapi.git] / wearable / test_src / sms_util.h
1 /*
2  * Telephony test application
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/time.h>
24 #include <pthread.h>     /* pthread functions and data structures */
25 #include <signal.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include "assert.h"
29 #include "glib.h"
30
31 #define MAX_UDH_LEN                     137
32 #define UDH_MAX                                 7
33
34 #define SMS_SMSP_ALPHA_ID_LEN_MAX                       128             /* EF-SMSP alpha id length */
35
36 #define SMS_MSG_SIZE_MAX                                918
37 #define SMS_CB_SIZE_MAX                         93
38 #define SMS_ADDRESS_LEN_MAX                     20
39 #define SMS_SCADDRESS_LEN_MAX           18
40
41 #define SMS_CB_PAGE_SIZE_MAX                            9               /**< CB maximum page size*/
42 #define SMS_GSM_SMS_MSG_NUM_MAX                 255             /**< Maximum GSM SMS message number*/
43 #define SMS_GSM_SMS_CBMI_LIST_SIZE_MAX          50              /**< Maximum GSM SMS CBMI list size*/
44 #define SMS_SMDATA_SIZE_MAX                                     165             /**< Maximum SMS data size that can be stored*/
45 #define SMS_MAX_SMS_SERVICE_CENTER_ADDR         12              /**<Maximum SMS service center address*/
46 #define SMS_MAX_INDEX                                           25              /**< Maximum index value for SMS */
47
48 #define FAIL                    0x00
49 #define SUCCESS         0x01
50
51 typedef enum
52 {
53         SMS_STATUS_READ,                                                /**< MT message, stored and read */
54         SMS_STATUS_UNREAD,                                      /**< MT message, stored and unread */
55         SMS_STATUS_SENT,                                                /**< MO message, stored and  sent */
56         SMS_STATUS_UNSENT,                                      /**< MO message, stored but not sent */
57         SMS_STATUS_DELIVERED,                                   /**< delivered destination */
58         SMS_STATUS_DELIVERY_UNCONFIRMED,        /**< Service centre forwarded message but is unable to confirm delivery*/
59         SMS_STATUS_MESSAGE_REPLACED,            /**< Message has been replaced*/
60         SMS_STATUS_RESERVED                                     /**< Reserved for future use*/
61 } SMS_MsgStatus;
62
63 typedef enum
64 {
65         SMS_SENDSMS_SUCCESS = 0x00,                                     /**<Message send success*/
66         SMS_ROUTING_NOT_AVAILABLE,                                      /**< Message routing not available*/
67         SMS_INVALID_PARAMETER,                                          /**< Invalid parameter present in TPDU*/
68         SMS_DEVICE_FAILURE,                                                     /**< Device failure*/
69         SMS_SERVICE_RESERVED,                                                   /**< Reserved Service*/
70         SMS_INVALID_LOCATION,                                                   /**< Invalid location*/
71         SMS_NO_SIM,                                                             /**< No SIM error*/
72         SMS_SIM_NOT_READY,                                                      /**< SIM not ready error*/
73         SMS_NO_NETWORK_RESP,                                                    /**< No response from network*/
74         SMS_DEST_ADDRESS_FDN_RESTRICTED,                        /**< Destination address restricted*/
75         SMS_SCADDRESS_FDN_RESTRICTED,                           /**< Service center address restricted*/
76         SMS_RESEND_ALREADY_DONE,                                        /**< Resend a operation already done*/
77         SMS_SCADDRESS_NOT_AVAILABLE,                            /**< SCA address not available*/
78         SMS_UNASSIGNED_NUMBER = 0x8001,                         /**< Unassigned number*/
79         SMS_OPERATOR_DETERMINED_BARRING = 0x8008,       /**< Operator determined barring*/
80         SMS_CALL_BARRED = 0x800A,                                               /**< Call barred*/
81         SMS_MESSAGE_TRANSFER_REJECTED = 0x8015,         /**< Message transfer rejected*/
82         SMS_MEMORY_CAPACITY_EXCEEDED = 0x8016,          /**< Memory capacity exceeded/memory full*/
83         SMS_DESTINAITION_OUTOFSERVICE = 0x801B,         /**< Destination number/address out of service*/
84         SMS_UNSPECIFIED_SUBSCRIBER = 0x801C,                    /**< Unspecified subscriber*/
85         SMS_FACILITY_REJECTED = 0x801D,                         /**< Facility rejected*/
86         SMS_UNKNOWN_SUBSCRIBER = 0x801E,                                /**< Unknown subscriber*/
87         SMS_NETWORK_OUTOFORDER = 0x8026,                        /**< Network out of order*/
88         SMS_TEMPORARY_FAILURE = 0x8029,                         /**< Temporary failure*/
89         SMS_CONGESTION = 0x802A,                                                /**< Congestion happened*/
90         SMS_RESOURCES_UNAVAILABLE = 0x802F,                     /**< Resource unavailable*/
91         SMS_FACILITY_NOT_SUBSCRIBED = 0x8032,                   /**< Facility not subscribed by the user*/
92         SMS_FACILITY_NOT_IMPLEMENTED = 0x8045,          /**< Facility not implemented*/
93         SMS_INVALID_REF_VALUE = 0x8051,                         /**< Invalid reference value*/
94         SMS_INVALID_MSG = 0x805F,                                               /**< Invalid message*/
95         SMS_INVALID_MANDATORY_INFO = 0x8060,                    /**< Invalid Mandatory information*/
96         SMS_MESSAGE_TYPE_NOT_IMPLEMENTED = 0x8061,      /**< Message type not implemented*/
97         SMS_MESSAGE_NOT_COMPAT_PROTOCOL = 0x8062,       /**< Message not compact protocol*/
98         SMS_IE_NOT_IMPLEMENTED = 0x8063,                         /**< Information element not implemented*/
99         SMS_PROTOCOL_ERROR = 0x806F,                                    /**< Protocol error*/
100         SMS_INTERWORKING = 0x807F,                                      /**< Networking error*/
101         SMS_ME_FULL = 0x8080,                                                   /**< SMS ME FULL */
102         SMS_SIM_FULL = 0x8081,                                                  /**< SMS SIM FULL */
103         SMS_TIMEOUT                                                                     /**< Timeout error */
104 } Sms_Response;
105
106 //////////////////////////////////TPDU type////////////////////////////////
107 typedef enum
108 {
109         SMS_TPDU_DELIVER                                = 0x00, // sc -> ms
110         SMS_TPDU_DELIVER_REPORT         = 0x00, // ms -> sc
111         SMS_TPDU_SUBMIT                         = 0x01, // ms -> sc
112         SMS_TPDU_SUBMIT_REPORT          = 0x01, // sc -> ms
113         SMS_TPDU_STATUS_REPORT          = 0x02, // sc -> ms
114         SMS_TPDU_COMMAND                                = 0x02, // ms -> sc
115         SMS_PDU_INVALID_TYPE
116
117 } SmsTPDUType;
118
119 typedef enum
120 {
121         SMS_DAEMON_UNKNOWN_MESSAGE,
122         SMS_DAEMON_RECEIVED_UNREAD_MESSAGE,
123         SMS_DAEMON_RECEIVED_READ_MESSAGE,
124         SMS_DAEMON_UNSENT_MESSAGE,
125         SMS_DAEMON_STORED_SENT_MESSAGE,
126
127 } sd_message_status;
128
129 typedef enum 
130 {
131         SMS_TON_UNKNOWN = 0, /**< unknown */
132         SMS_TON_INTERNATIONAL=1, /**< international number */
133         SMS_TON_NATIONAL = 2, /**< national number */
134         SMS_TON_NETWORK_SPECIFIC = 3, /**< network specific number */
135         SMS_TON_DEDICATED_ACCESS = 4, /**< subscriber number */
136         SMS_TON_ALPHA_NUMERIC = 5, /**< alphanumeric, GSM 7-bit default alphabet) */
137         SMS_TON_ABBREVIATED_NUMBER = 6, /**< abbreviated number */
138         SMS_TON_RESERVED_FOR_EXT = 7 /**< reserved for extension */
139 } SimTypeOfNum_t;
140
141 typedef enum 
142 {
143         SMS_NPI_UNKNOWN = 0, /**< Unknown */
144         SMS_NPI_ISDN_TEL = 1, /**< ISDN/Telephone numbering plan */
145         SMS_NPI_DATA_NUMBERING_PLAN = 3, /**< Data numbering plan */
146         SMS_NPI_TELEX = 4, /**< Telex numbering plan */
147         SMS_NPI_SVC_CNTR_SPECIFIC_PLAN = 5, /**< Service Center Specific plan */
148         SMS_NPI_SVC_CNTR_SPECIFIC_PLAN2 = 6, /**< Service Center Specific plan */
149         SMS_NPI_NATIONAL = 8, /**< National numbering plan */
150         SMS_NPI_PRIVATE = 9, /**< Private numbering plan */
151         SMS_NPI_ERMES_NUMBERING_PLAN = 10, /**< ERMES numbering plan */
152         SMS_NPI_RESERVED_FOR_EXT = 0xF /**< Reserved for extension */
153 } SimNumberingPlanIdentity_t;
154
155 typedef struct {
156         unsigned int DialNumLen; /**< length of address. If Service center address is not set, then this will be zero */
157         SimTypeOfNum_t Ton; /**< Type of number*/
158         SimNumberingPlanIdentity_t Npi; /**<Numbering plan identification*/
159         unsigned char szDiallingNum[SMS_ADDRESS_LEN_MAX + 1]; /**< destination address. If Address not set, then set to 0xFF */
160 } SmsAddressInfo_t;
161
162 /*
163  * Sms_vp_type
164  * This enumeration defines the validity period type.
165  */
166 typedef enum
167 {
168         SMS_VP_NOT_USED = 0,            /**< validity period not used */
169         SMS_VP_RELATIVE = 2,            /**< after a predetermined time period after the service center has received the message */
170         SMS_VP_ABSOLUTE = 3,
171         SMS_VP_ENHANCED = 1,
172
173         SMS_VP_MAX
174 } Sms_vp_type;
175 /*
176  * Sms_coding_group_type
177  This enum defined the different sms coding group types
178  */
179 typedef enum
180 {
181         SMS_CODGRP_SM_GENERAL_DCS,                      /**< Bits 7..4 00xx */
182         SMS_CODGRP_CB_GENERAL_LANG,             /**< 0000 */
183         SMS_CODGRP_CB_ALPHA,                            /**< 0001 */
184         SMS_CODGRP_CB_RES1,                             /**< 0010 */
185         SMS_CODGRP_CB_RES2,                             /**< 0011 */
186         SMS_CODGRP_SM_AUTO_DELETION,            /**< 01xx Bits 5..0 are coded the same as SM_GENERAL_DCS */
187         SMS_CODGRP_CB_GENERAL_DCS,                      /**< 01xx behavior is as SM_GENERAL_DCS */
188         SMS_CODGRP_SM_WAITING_DISCARD,          /**< 1100 */
189         SMS_CODGRP_SM_WAITING_STORE,            /**< 1101 */
190         SMS_CODGRP_SM_WAITING_STORE_UCS2,       /**< 1110 */
191         SMS_CODGRP_CB_WAP,                                      /**< 1110 Cell Broadcast */
192         SMS_CODGRP_SM_CLASS_CODING,             /**< 1111 */
193         SMS_CODGRP_CB_CLASS_CODING,             /**< 1111 Cell Broadcast */
194
195         SMS_CODGRP_MAX
196 } Sms_coding_group_type;
197 /*
198  *  Sms_waiting_type
199  Defines different types of waiting indicators
200  */
201 typedef enum
202 {
203         SMS_WAITING_VOICE_MSG,          /**< voice message waiting */
204         SMS_WAITING_FAX_MSG,            /**< FAX message waiting */
205         SMS_WAITING_EMAIL_MSG,          /**< E-mail waiting */
206         SMS_WAITING_OTHER_MSG,          /**< Other message waiting */
207
208         SMS_WAITING_MAX
209 } Sms_waiting_type;
210 /*
211  * Sms_alphabet_type
212  Defines different alphabet types of an sms
213  */
214 typedef enum
215 {
216         SMS_ALPHABET_DEFAULT,   /**< GSM alphabet default 7bit */
217         SMS_ALPHABET_8BIT,              /**< 8bit */
218         SMS_ALPHABET_UCS2,              /**< 16bit ucs2 */
219
220         SMS_ALPHABET_KSC5601,   /**< for Korean */
221
222         SMS_ALPHABET_MAX
223 } Sms_alphabet_type;
224 /*
225  * Sms_class_type
226  This enum defines the different classes of SMS which indicates the
227  type of storage of the sms.
228  */
229 typedef enum
230 {
231         SMS_CLASS_0,    /**< display immediately */
232         SMS_CLASS_1,    /**< may be stored */
233         SMS_CLASS_2,    /**< store in SIM */
234         SMS_CLASS_3,    /**< transfer to TE */
235         SMS_CLASS_NONE,
236
237         SMS_CLASS_MAX
238 } Sms_class_type;
239
240 /*
241 This structure contains the information related to sms coding schemes
242
243 */
244 typedef struct
245 {
246         int                                                     bCompressed;            /**< if text is compressed this is TRUE */
247         int                                                     bmsg_class_set;         /**< message class indication. if TRUE = bits 1..0, if FALSE = reserved */
248         int                                                     bmsg_ind_active;                /**< message indication group. if TRUE = on, FALSE = off */
249         Sms_coding_group_type           coding_group_type;      /**< Coding group, GSM 03.38 */
250         Sms_waiting_type                        waiting_type;           /**< Type of message waiting */
251         Sms_alphabet_type                       alphabet_type;          /**< How to encode a message. */
252         Sms_class_type                  class_type;                     /**< The message class */
253 } Sms_coding_scheme;
254
255 /*
256  *  Sms_vp_rel_Value
257  * This structure defines the validity period types.
258  */
259 typedef enum
260 {
261         SMS_VP_REL_1H,          /**< One hour */
262         SMS_VP_REL_6H,          /**< Six hours */
263         SMS_VP_REL_12H, /**< Twelve hours */
264         SMS_VP_REL_1D,          /**< One day */
265         SMS_VP_REL_3D,          /**< Three days */
266         SMS_VP_REL_1W,          /**< One week */
267         SMS_VP_REL_1M,          /**< One Month */
268         SMS_VP_REL_MAX          /**< Max = 63 weeks */
269 } Sms_vp_rel_Value;
270 /*
271 This structure contains all the information
272 related to validity period of a message.
273
274 */
275 typedef struct
276 {
277         Sms_vp_type vp_type;                      /**< Validity period format */
278         Sms_vp_rel_Value vp_rel_type; /**< Validity period value duration the message must retain in the service center while application attempts delivery */
279         unsigned long vpValue;                                            /** Validity period value */
280 } Sms_vp;
281
282
283 /* This structure contains the details of delete status notification, which comes after calling
284 TelTapiSmsDeleteMsg*/
285 typedef struct
286 {
287         unsigned short result;
288         unsigned char index;
289 }TelTapiSmsDeleteMsg_status_ind;
290
291
292 /* This structure contains the details of save status notification, which comes after calling
293 TelTapiSmsWriteMsg*/
294
295 typedef struct
296 {
297         unsigned short result;
298         unsigned char index;
299 }Sms_save_status_ind;
300
301
302 typedef struct
303 {
304         int             year;
305         int             month;
306         int             day;
307         int             hour;
308         int             minute;
309         int             second;
310
311 } TmDateTime;
312
313
314 #define BASE_YEAR               70L                                     /* 1970 is the base year */
315 #define MAX_YEAR                        130L                                    /* 2030 is the max year */
316
317
318 #define SIM_SMSP_ADDRESS_LEN                                    20      // digit length
319
320 #define SET_TON_NPI(dest, ton, npi) {   \
321         dest = 0x80;                                            \
322         dest |= (ton & 0x07) << 4;                      \
323         dest |= npi & 0x0F;                                     \
324 }
325
326
327
328
329 typedef struct
330 {
331         /************HEADER************/
332
333         // message type [M]
334         SmsTPDUType  msg_type ;   //bit 0 ,1
335
336         // reject duplicates [M]
337         int rd ;  // bit 2
338
339         // validity period format [M]
340         Sms_vp_type vpf ; //  bits 3, 4
341
342         // reply path [M]
343         int rp ; //bit 7
344
345         // user data header indication
346         int udhi ;   //bit 6
347
348         // status report request
349         int  srr ;  // bit 5
350
351         /************BODY************/
352         //message reference [M]
353         unsigned int mr;
354
355         // destination address [M]
356         SmsAddressInfo_t desAddr;
357
358         // protocol identifier [M]
359         unsigned char pId;
360
361         // data coding scheme [M]
362         Sms_coding_scheme dcs;
363
364         // validity period
365         Sms_vp vp;
366
367         // user data length [M]
368         unsigned int udl;
369
370         // user data
371         char *userData;
372
373
374 }TPDU_SMS_SUBMIT;
375
376 typedef struct
377 {
378         /************HEADER************/
379
380         // message type [M]
381         SmsTPDUType  msg_type;  //bit 0 ,1
382
383         // user data header indication
384         int udhi ;   //bit 6
385
386         /************BODY************/
387
388         // failure Cause [M]
389         unsigned int    fcs;
390
391         // parameter indicator [M]
392         unsigned char paraInd;
393
394         // service centre time stamp [M]
395         TmDateTime scts;
396
397         // protocol identifier [M]
398         unsigned char pId;
399
400         // data coding scheme
401         Sms_coding_scheme dcs;
402
403         // user data length [M]
404         unsigned int udl;
405
406         // user data
407         char*  userData;
408
409 }TPDU_SMS_SUBMIT_REPORT;
410
411
412 typedef struct
413 {
414         /************HEADER************/
415         SmsTPDUType msg_type;   //bit 0 ,1
416
417         int mms; // bit 2
418
419         int rp; // bit 7
420
421         int udhi;  //bit 6
422
423         int sri; // bit , 5status_report_indication
424
425         /************BODY************/
426
427         SmsAddressInfo_t orgAddr;
428
429         unsigned char  pId;
430
431         TmDateTime scts;
432
433         Sms_coding_scheme dcs;
434
435         unsigned int udl;
436
437         char  * userData;
438
439 }TPDU_SMS_DELIVER;
440
441 typedef struct
442 {
443         /************HEADER************/
444         SmsTPDUType msg_type ;   //bit 0 ,1
445
446         int udhi ;  //bit 6
447
448         /************BODY************/
449
450         unsigned int fcs;
451
452         unsigned char paraInd;
453
454         unsigned char pId;
455
456         Sms_coding_scheme dcs;
457
458         unsigned int udl;
459
460         unsigned char * userData;
461
462 }TPDU_SMS_DELIVER_REPORT;
463
464
465 typedef struct
466 {
467         /************HEADER************/
468         SmsTPDUType msg_type ;   //bit 0 ,1
469
470         int udhi ; //bit 6
471
472         int srr; //bit 5, status_report_request
473
474         /************BODY************/
475
476         unsigned int  mr; //message_ref
477
478         unsigned char pId;
479
480         unsigned char cmdType;
481
482         unsigned char msgNum;
483
484         SmsAddressInfo_t destAddr;
485
486         unsigned int udl;
487
488         unsigned char * userData;
489
490 }TPDU_SMS_COMMAND;
491
492
493 typedef struct
494 {
495         /************HEADER************/
496
497         SmsTPDUType msg_type ;   //bit 0 ,1
498
499         int  udhi ; //bit 6
500
501         int mms ; //bit 2
502
503         int srq; //bit 5, status_report_qualifier
504
505         /************BODY************/
506
507         unsigned int  mr;
508
509         SmsAddressInfo_t rcpAddr; //recipient_addr
510
511         TmDateTime scts;
512
513         TmDateTime dt; //discharge_time
514
515         unsigned char  status;
516
517         unsigned char paraInd;
518
519         unsigned char pId;
520
521         Sms_coding_scheme dcs;
522
523         unsigned int udl;
524
525         char * userData;
526
527 }TPDU_SMS_STATUS_REPORT;
528
529 /***************************************************************************************/
530 void reverse(char* x, int len);
531 char* AcItoa(int n, char* str, int b) ;
532 int AcToupper(int ch);
533
534 char* SmsUtilUnpackGSMCode(char* szData, const char* pIn, int in_len );
535 int SmsUtilPackGSMCode( unsigned char* pOut, const char* szData, int in_len );
536
537 void SmsUtilConvertBCD2Digit( char* pDigits, char* pBCD, int digitLen );
538 void SmsUtilConvertDigit2BCD( char* pBCD, char* pDigits, int digitLen );
539
540 TmDateTime* SmsUtilDecodeTimeStamp(char* pTimeStamp, TmDateTime *tmDateTime );
541 unsigned char* SmsUtilEncodeTimeStamp( TmDateTime* tmDateTime, unsigned char* pTimeStamp );
542
543
544 int  SmsUtilDecodeAddrField(char *diallingNum, char * pAddrField, int *result_ton, int *result_npi );
545 int SmsUtilEncodeAddrField( unsigned char* pAddrField, char* diallingNum, int dialnumLen, int ton, int npi );
546
547 int SmsUtilDecodeScAddrField( SmsAddressInfo_t* pSmsAddrField, unsigned char* pAddrField );
548  int  SmsUtilEncodeScAddrField( unsigned char* pAddrField, SmsAddressInfo_t * pSmsAddrField );
549
550 void SmsUtilDecodeDCS( Sms_coding_scheme* pCodingScheme,   unsigned char dcs );
551 void SmsUtilEncodeDCS( unsigned char* pDCS, Sms_coding_scheme* pCodingScheme);
552
553  unsigned char SmsUtilEncodeValidity( unsigned char* pValidity, Sms_vp* pVP );
554
555
556
557