Updated minor code changes -
[platform/core/telephony/libtapi.git] / test_src / sms.c
1 /*
2  * libslp-tapi
3  *
4  * Copyright (c) 2014 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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/time.h>
25 #include <unistd.h>
26 #include <glib.h>
27
28 #include <arpa/inet.h>
29 #include <tapi_common.h>
30 #include <TelSms.h>
31 #include <TapiUtility.h>
32 #include <ITapiNetText.h>
33
34 #include "menu.h"
35 #include "sms.h"
36 #include "sms_util.h"
37 #include "sms_util_cdma.h"
38
39 /* Global Variables */
40 int dcs_pos = 0;
41 unsigned int SmsMsgId = 0;
42 unsigned char tp_mr = 0x00;
43
44 #define SMS_PTP_MSG 0x0
45
46 #define DIGIT_MODE_BIT 0x80
47 #define NUMBER_MODE_BIT 0x40
48 #define DIGIT_MODE_OFFSET 7
49 #define NUMBER_MODE_OFFSET 6
50 #define NIBBLE_BIT_LENGTH 4
51 #define SUBPARAM_LEN_VAL 0x03
52
53 #define u16 uint16_t
54 #define u8 uint8_t
55
56 const char *cmdString[] = {
57         "TAPI_SERVICE_SMS_SEND_UMTS_MSG",
58         "TAPI_SERVICE_SMS_READ_MSG",
59         "TAPI_SERVICE_SMS_SAVE_MSG",
60         "TAPI_SERVICE_SMS_DELETE_MSG",
61         "TAPI_SERVICE_SMS_GET_COUNT",
62         "TAPI_SERVICE_SMS_GET_SCA",
63         "TAPI_SERVICE_SMS_SET_SCA",
64         "TAPI_SERVICE_SMS_GET_CB_CONFIG",
65         "TAPI_SERVICE_SMS_SET_CB_CONFIG",
66         "TAPI_SERVICE_SMS_SET_MEM_STATUS",
67         "TAPI_SERVICE_SMS_GET_PREF_BEARER",
68         "TAPI_SERVICE_SMS_SET_PREF_BEARER",
69         "TAPI_SERVICE_SMS_SET_DELIVERY_REPORT",
70         "TAPI_SERVICE_SMS_SET_MSG_STATUS",
71         "TAPI_SERVICE_SMS_GET_PARAMS",
72         "TAPI_SERVICE_SMS_SET_PARAMS",
73         "TAPI_SERVICE_SMS_GET_PARAMCNT",
74         "TAPI_SERVICE_SMS_SEND_CDMA_MSG"
75 };
76
77 static void on_resp_send_msg(TapiHandle *handle, int result, void *data,
78                 void *user_data);
79 static void on_resp_send_cdma_msg(TapiHandle *handle, int result, void *data,
80                 void *user_data);
81 static void on_resp_read_msg(TapiHandle *handle, int result, void *data,
82                 void *user_data);
83 static void on_resp_save_msg(TapiHandle *handle, int result, void *data,
84                 void *user_data);
85 static void on_resp_DeliverReport_msg(TapiHandle *handle, int result,
86                 void *data, void *user_data);
87 int  _util_hexstring_to_asciistring(const unsigned char *hex_string, unsigned char *ascii_string, int hex_len);
88
89 unsigned int SmsUtilEncodeSca(TelSmsAddressInfo_t *sca, unsigned char *sca_num);
90
91 static int CdmaCbSetting(MManager *mm, struct menu_data *menu);
92
93 int  _util_hexstring_to_asciistring(const unsigned char *hex_string, unsigned char *ascii_string, int hex_len)
94 {
95         int i, ascii_len;
96
97         if (hex_string[hex_len - 1]>>4 == 0x0f)
98                 ascii_len = (hex_len * 2) - 1;
99         else
100                 ascii_len = (hex_len * 2);
101
102         for (i = 0; i < ascii_len; i++) {
103                 if (i % 2 == 0)
104                         ascii_string[i] = (hex_string[i/2] & 0x0f) + '0';
105                 else
106                         ascii_string[i] = ((hex_string[i/2]   >> 4)) + '0';
107         }
108         ascii_string[ascii_len] = '\0';
109         return ascii_len;
110 }
111
112 unsigned int SmsUtilEncodeSca(TelSmsAddressInfo_t *sca, unsigned char *sca_num)
113 {
114         unsigned int sca_length = 0;
115         unsigned int additional_len = 0;
116         unsigned local_index, j;
117
118         sca->Npi = TAPI_SIM_NPI_ISDN_TEL;
119         sca->Ton = TAPI_SIM_TON_UNKNOWN;
120         if (sca_num[0] == '+') {
121                 sca->Ton = TAPI_SIM_TON_INTERNATIONAL;
122                 additional_len = 1;
123         }
124         sca_length = strlen((char *)sca_num) - additional_len;
125         if (sca_length % 2 == 0) {
126                 for (local_index = additional_len, j = 0; local_index < sca_length; local_index += 2, j++)
127                         sca->szDiallingNum[j] = ((sca_num[local_index+1] & 0X0F) << 4) | (sca_num[local_index] & 0x0F);
128         } else {
129                 for (local_index = additional_len, j = 0; local_index < sca_length; local_index += 2, j++) {
130                         if (local_index == (sca_length - 1))
131                                 sca->szDiallingNum[j] = ((0xF0) | (sca_num[local_index] & 0x0F));
132                         else
133                                 sca->szDiallingNum[j] = ((sca_num[local_index+1] & 0X0F) << 4) | (sca_num[local_index] & 0x0F);
134                 }
135         }
136         sca->szDiallingNum[(sca_length+1)/2] = '\0';
137         sca->DialNumLen = (sca_length+1)/2;
138
139         return sca->DialNumLen;
140 }
141
142
143 static BOOL EncodeCdmaSmsParamTeleservice(int teleservice_id , ParamTeleserv_t *ParamTeleserv)
144 {
145         BOOL ret = FALSE;
146
147         if (ParamTeleserv == NULL) {
148                 printf("[EncodeCdmaSmsParamTeleservice] NULL pointer  \n");
149                 return ret;
150         }
151
152         switch (teleservice_id) {
153         case SMS_3GPP2_TELESERVICE_ID_CPT:
154         case SMS_3GPP2_TELESERVICE_ID_CCMT:
155         case SMS_3GPP2_TELESERVICE_ID_VMN:
156         case SMS_3GPP2_TELESERVICE_ID_WAP:
157                 ret = TRUE;
158         break;
159
160         default:
161                 ret = FALSE;
162         }
163
164         if (ret == TRUE) {
165                 ParamTeleserv->param_id = SMS_3GPP2_PARAM_ID_TELESERVICE_ID;
166                 ParamTeleserv->param_len = 2;
167                 ParamTeleserv->teleserv_id = teleservice_id;
168                 printf("ParamTeleserv->teleserv_id[%d]\n", ParamTeleserv->teleserv_id);
169         }
170
171         return ret;
172 }
173 static BOOL EncodeCdmaSmsParamMsgId(int type, ParamMsgId_t *ParamMsg)
174 {
175         BOOL ret = FALSE;
176
177         if (ParamMsg == NULL) {
178                 printf("[EncodeCdmaSmsParamMsgId] NULL pointer  \n");
179                 return ret;
180         }
181
182         ParamMsg->param_id = SMS_3GPP2_BSUB_ID_MESSAGE_IDENTIFIER;
183         ParamMsg->param_len = 3;
184         ParamMsg->type = type;
185         ParamMsg->msg_id = SmsMsgId++;
186
187         ret = TRUE;
188
189         return ret;
190 }
191
192 static BOOL EncodeCdmaSmsParamUserData(char *SendTxt, ParamUserData_t *ParamUser)
193 {
194         BOOL ret = FALSE;
195         int length = 0;
196
197         if (ParamUser == NULL || SendTxt == NULL) {
198                 printf("[EncodeCdmaSmsParamUserData] NULL pointer  \n");
199                 return ret;
200         }
201
202         length = strlen(SendTxt);
203         if (length > MAXLENGTH_SMS_USER_DATA) {
204                 printf("[EncodeCdmaSmsParamUserData] Text is too long \n");
205                 return ret;
206         }
207
208         ParamUser->param_id = SMS_3GPP2_BSUB_ID_USER_DATA;
209         ParamUser->encoding = SMS_3GPP2_BSUB_UD_ASCII7BIT_ENCODING;
210         strncpy((char *)ParamUser->CHARi, SendTxt, length);
211
212         ParamUser->len = length;
213         length += 2;;
214         ParamUser->param_len = length;
215
216         ret = TRUE;
217
218         return ret;
219 }
220
221 static BOOL EncodeCdmaSmsSubmitTpdu(MManager *mm,
222         char *diallingNum, int dialNumLen, char *msgTxt, int msg_len)
223 {
224         int ret, index = 0;
225         unsigned char *encoded_msg, digit_mode_val = 0, number_mode_val = 0;
226         unsigned char hex_string[SMS_ADDRESS_LEN_MAX + 1] = { 0, };
227         TapiHandle *handle;
228         TelSmsDatapackageInfo_t data_package;
229         u16 data;
230
231         ParamTeleserv_t param_teleservice = {0, };
232         ParamMsgId_t param_msg = {0, };
233         ParamUserData_t param_user = {0, };
234
235         msg("EncodeCdmaSmsSubmitTpdu:Enter");
236
237         handle = menu_manager_ref_user_data(mm);
238         encoded_msg = g_try_malloc0(sizeof(TelSmsDatapackageInfo_t));
239         if (NULL == encoded_msg) {
240                 msg("Memory Allocation Failed for CDMA Data Package");
241                 return FALSE;
242         }
243
244         /* Converting Destination number from ASCII to hex*/
245         if (__sms_asciistring_to_hex(diallingNum, hex_string, dialNumLen) == FALSE) {
246                 msg("__sms_asciistring_to_hex() failed.");
247                 g_free(encoded_msg);
248                 return FALSE;
249         }
250
251         msg("Dump of hex string");
252         __util_hex_dump("    ", dialNumLen/2, hex_string);
253
254         /* First byte specifies Messaging Type*/
255         encoded_msg[index++] = SMS_PTP_MSG;
256
257         /* 3GPP2 C.S0015-A section:3.4.3.1 Teleservice Identifier*/
258         /* 1 TeleService Identifier */
259         {
260                 EncodeCdmaSmsParamTeleservice(SMS_3GPP2_TELESERVICE_ID_CCMT, &param_teleservice);
261                 encoded_msg[index++] = param_teleservice.param_id;  /*TeleService Identifier Tag e.g SMS_PARAMID_TELESERVICE_ID*/
262                 encoded_msg[index++] = param_teleservice.param_len;  /*TeleService Identifier Length*/
263                 data =  htons(param_teleservice.teleserv_id);  /*Conversion of Little Endian*/
264                 memcpy(encoded_msg + index, &(data), sizeof(unsigned short));
265                 index += sizeof(unsigned short);
266         }
267
268         /* 3GPP2 C.S0015-A section:3.4.3.3 Address Parameters*/
269         /* 1 Destination Address */
270         {
271                 int dest_addr_offset = 2, previous_addr_index;
272                 unsigned char num_fields[SMS_ADDRESS_LEN_MAX + 1] = { 0, };
273
274                 encoded_msg[index++] = SMS_3GPP2_PARAM_ID_DESTINATION_ADDRESS;
275                 /* Fill the length value at the end */
276                 unsigned char *addr_len = &encoded_msg[index++]; /* param length */
277                 previous_addr_index = index;
278
279                 encoded_msg[index]  = ((digit_mode_val << (DIGIT_MODE_OFFSET)) & DIGIT_MODE_BIT);
280                 encoded_msg[index]  = ((number_mode_val << (NUMBER_MODE_OFFSET)) & NUMBER_MODE_BIT);
281
282                 bitarray_copy(num_fields, 0, dialNumLen * CHAR_BIT, &encoded_msg[index], dest_addr_offset);
283                 index++;
284
285                 bitarray_copy(hex_string, 0, dialNumLen * NIBBLE_BIT_LENGTH, &encoded_msg[index], dest_addr_offset);
286
287                 index += (dest_addr_offset + dialNumLen * NIBBLE_BIT_LENGTH) / 8 +
288                         ((dest_addr_offset + dialNumLen * NIBBLE_BIT_LENGTH) % 8 ? 1 : 0);
289
290                 /* Updating the Address Length */
291                 *addr_len = index - previous_addr_index;
292
293                 msg("Dest addr final Index[%d] ", index);
294                 __util_hex_dump("    ", index, encoded_msg);
295         }
296
297         /* Bearer Data Options: */
298         int bearer_data_previous_index;
299         encoded_msg[index++] = SMS_3GPP2_PARAM_ID_BEARER_DATA;
300         unsigned char *bearer_data_length = &encoded_msg[index++];
301         bearer_data_previous_index = index;
302         {
303                 /* 1  Message Identifier */
304                 {
305                         u8 offset = 4;
306                         EncodeCdmaSmsParamMsgId(SMS_3GPP2_BSUB_TELSERVICE_MSG_TYPE_SUBMIT, &param_msg);
307                         encoded_msg[index++] = param_msg.param_id;
308                         encoded_msg[index++] = SUBPARAM_LEN_VAL;
309
310
311                         /*Copying the Message ID*/
312                         bitarray_copy((&param_msg.type), offset, NIBBLE_BIT_LENGTH, &encoded_msg[index], 0);
313                         param_msg.msg_id = htons(param_msg.msg_id);
314
315                         bitarray_copy((unsigned char *)(&param_msg.msg_id), 0, sizeof(param_msg.msg_id) * CHAR_BIT, &encoded_msg[index], offset);
316                         param_msg.msg_id = ntohs(param_msg.msg_id);
317
318                         index += (offset + sizeof(param_msg.msg_id)*CHAR_BIT)/8 +
319                                 (((offset + sizeof(param_msg.msg_id)*CHAR_BIT)%8) ? 1 : 0);
320
321                         msg("Message Identifier final Index[%d] ", index);
322                         __util_hex_dump("    ", index, encoded_msg);
323                 }
324
325                 /*3GPP2 C.S0015-A section 4.5.2 User Data*/
326                 /* 1 User Data */
327                 {
328                         int message_type = 0, offset = 5, prev_userdata_index;
329                         int current_index, i, initial_offset, index_data;
330                         EncodeCdmaSmsParamUserData(msgTxt, &param_user);
331                         encoded_msg[index++] = param_user.param_id;
332
333                         unsigned char *user_data_len = &encoded_msg[index++];
334
335                         prev_userdata_index = index;
336
337                         if (param_user.encoding == 0x01) {
338                                 encoded_msg[index] = param_user.encoding << 3;
339                                 encoded_msg[index] = (encoded_msg[index]  & (0xF8)) | (message_type >> 5);
340                                 index++;
341                                 encoded_msg[index] = message_type << 3;
342                         } else {
343                                 encoded_msg[index] = param_user.encoding << 3;
344                         }
345
346                         encoded_msg[index] = (encoded_msg[index] & 0xF8) | (param_user.len >> 5);
347                         index++;
348                         encoded_msg[index] = (param_user.len << 3);
349
350
351                         current_index = index;
352                         initial_offset = offset;
353                         index_data = 0;
354                         for (i = 1; i <= param_user.len; i++) {
355                                 bitarray_copy((const unsigned char *)&param_user.CHARi[i-1], 1, 7, (unsigned char *)&encoded_msg[current_index+index_data], offset);
356                                 offset = (offset + 7) % 8;
357                                 index_data = (initial_offset + i * 7) / 8;
358                         }
359                         index = index_data + current_index;
360                         index++;
361
362                         msg("User Data Index[%d ]:", index);
363                         __util_hex_dump("    ", index, encoded_msg);
364
365                         *user_data_len = index - prev_userdata_index;
366                 }
367                 *bearer_data_length = index - bearer_data_previous_index;
368                 __util_hex_dump("    ", index, encoded_msg);
369         }
370
371         memset(&data_package, 0x0, sizeof(TelSmsDatapackageInfo_t));
372
373         data_package.format = TAPI_NETTEXT_NETTYPE_3GPP2;
374         data_package.MsgLength = index;
375
376         data_package.szData[data_package.MsgLength + 1] = '\0';
377
378         memcpy(&(data_package.szData), encoded_msg, data_package.MsgLength);
379
380         msg("Message Length[%d] ", data_package.MsgLength);
381
382         __util_hex_dump("    ", data_package.MsgLength, data_package.szData);
383
384         ret = tel_send_sms(handle, &data_package, 0x0, on_resp_send_cdma_msg, NULL);
385         msg("Return Status of CDMA tel_send_sms[%d]", ret);
386
387         g_free(encoded_msg);
388         return TRUE;
389 }
390
391 static BOOL DecodeCellBroadcastMsg(int length, char *pPDU)
392 {
393         int msgLength;
394         int sn = 0; /* serial number */
395         int msgID; /* message identifier */
396         Sms_coding_scheme dcs;
397
398         unsigned char page[2];
399         int contents_length;
400         char contents[SMS_CB_SIZE_MAX];
401
402         unsigned char gs;
403         unsigned char msgCode;
404         unsigned char updateNumber;
405
406         if (pPDU == NULL) {
407                 msg("pData is Null");
408                 return FALSE;
409         }
410
411         msg("cb msg type is %x", pPDU[0]);
412         msg("cb msg len is %x", pPDU[1]);
413
414         msg("$$$$$$$$$$$$$$$length of cb msg is %d", length);
415         sn = (pPDU[2] << 8) | pPDU[1];
416
417         gs = (pPDU[2] & 0xC0) >> 6;  /* bit 7, bit 6 */
418
419         msgCode = (((pPDU[2] & 0x3F) << 4) | pPDU[3]) & 0xF0;
420
421         updateNumber = pPDU[3] & 0x0F;  /* bit3, bit2, bit1, bit0 */
422
423         msgID = pPDU[4] << 4 | pPDU[3];
424
425         SmsUtilDecodeDCS(&dcs, pPDU[6]);
426
427         page[0] = pPDU[7] & 0x0F; /* total page */
428         page[1] = (pPDU[7] & 0xF0) >> 4; /* current page */
429
430         msgLength = length - 2;
431
432         contents_length = msgLength - 6;
433
434         if (dcs.alphabet_type == SMS_ALPHABET_DEFAULT) {
435                 unsigned char inData[SMS_CB_SIZE_MAX + 1];
436
437                 memset(inData, 0x00, SMS_CB_SIZE_MAX + 1);
438
439                 memcpy((void *)inData, (void *)&pPDU[8], contents_length);
440                 SmsUtilUnpackGSMCode(contents, &pPDU[8], msgLength);
441         } else {
442                 memcpy(contents, &pPDU[8], msgLength);
443                 contents[msgLength] = 0;
444         }
445
446         printf("*************************************\n");
447         printf("serical number : 0x%04x \n", sn);
448         printf("Geographical Scope: 0x%x\n", gs);
449         printf("message code : 0x%x\n", msgCode);
450         printf("update Number : 0x%x\n", updateNumber);
451         printf("message identifier :  0x%x\n", msgID);
452         printf("DCS-Commpressed?: %d (1: true)\n", dcs.bCompressed);
453         printf("DCS-coding_group_type:  %x\n", dcs.coding_group_type);
454         printf("contents bytes size : %d\n", contents_length);
455         printf("page : (%d /%d) (page /total) \n", page[1], page[0]);
456         printf("contents: %s\n", contents);
457         printf("*************************************\n");
458
459         return TRUE;
460 }
461
462 static BOOL EncodeSmsSubmitTpdu(MManager *mm, char *diallingNum,
463                 int dialNumLen, char *msg, int msg_len)
464 {
465         TapiHandle *handle = menu_manager_ref_user_data(mm);
466
467         TPDU_SMS_SUBMIT tpdu_submit;
468
469         unsigned char packet[TAPI_NETTEXT_MSG_SIZE_MAX];
470
471         unsigned char SCA[TAPI_NETTEXT_ADDRESS_LEN_MAX + 1];
472         int ret = 0;
473
474         tp_mr++;
475         unsigned char tp_pid = 0; /* replace type (65) */
476         unsigned char tp_dcs; /* class_1(17), class_2(18) */
477         unsigned char tp_vp;
478
479         int local_index = 0, ScIndex = 0;
480         int i;
481         int requestId = 0;
482         unsigned char sca_len = 0;
483         TapiResult_t returnStatus;
484
485         TelSmsDatapackageInfo_t *pDataPackage = NULL;
486
487         memset(packet, 0, TAPI_NETTEXT_MSG_SIZE_MAX);
488
489         /************SCA ADDRESS GET START**************/
490         /*
491          returnStatus = TelTapiSmsGetSCA(&sc_addr, 0);
492          printf("Service center addr returned from TAPI SCA address %s returnStatus %d\n", sc_addr.szDiallingNum, returnStatus);
493          SmsUtilDecodeAddrField(decodeScaAddr, &sc_addr.szDiallingNum[0], &sc_addr.Ton, &sc_addr.Npi);
494          sca_len = strlen(decodeScaAddr);
495
496          memcpy(SCA, decodeScaAddr, sca_len);
497          SCA[sca_len] = '\0';
498          */
499         msg("Enter a Valid Service Centre Number  or Just Enter:");
500         memset(SCA, 0, sizeof(SCA));
501
502         ret = read(0, SCA, sizeof(SCA) - 1);
503         if (ret <= 0) {
504                 msg(" NULL msg can NOT be sent ");
505                 return -1;
506         }
507         SCA[ret] = '\0';
508
509         pDataPackage = malloc(sizeof(TelSmsDatapackageInfo_t));
510         if (!pDataPackage)
511                 return -1;
512
513         memset(pDataPackage, 0, sizeof(TelSmsDatapackageInfo_t));
514
515         sca_len = strlen((char *)SCA) - 1;
516
517         msg("Decode SCA Address =========");
518         msg("SCA Number :%s, SCA Length :%d ", SCA, sca_len);
519         msg("========================");
520
521         /* SCA ADDRESS GET END */
522
523         ScIndex = SmsUtilEncodeAddrField((unsigned char *)pDataPackage->Sca,
524                         (char *)SCA, sca_len, 0x00, 0x01);
525
526         msg("ScIndex is %d", ScIndex);
527
528         /* TP-MTI, TP-RD, TP-VPF, TP-SRR, TP_UDHI, TP-RP */
529         tpdu_submit.msg_type = SMS_TPDU_SUBMIT;
530         tpdu_submit.rd = FALSE; /* false :accept duplicate msg , true: reject duplicate msg */
531         tpdu_submit.vpf = SMS_VP_RELATIVE;
532         tpdu_submit.srr = FALSE; /* false :A status report is not requested , true: A status report is requested */
533         tpdu_submit.udhi = FALSE; /* false: contains only the short message , true :a Header in addition to the short message */
534         tpdu_submit.rp = FALSE; /* false :TP Reply Path parameter is not set in this SMS SUBMIT, true : TP Reply Path parameter is set in this SMS SUBMIT */
535
536         packet[local_index] = tpdu_submit.msg_type; /* SUBMIT: bits 0, 1 */
537         packet[local_index] |= tpdu_submit.rd ? 0x04 : 0; /* bit 2 */
538         packet[local_index] |= tpdu_submit.vpf << 3; /* bits 3, 4 */
539         packet[local_index] |= tpdu_submit.srr ? 0x20 : 0; /* bit 5 */
540         packet[local_index] |= tpdu_submit.udhi ? 0x40 : 0; /* bit 6 */
541         packet[local_index] |= tpdu_submit.rp ? 0x80 : 0; /* bit 7 */
542
543         local_index++;
544         msg("local_index of message ref is %d, msgref is %d", local_index, tp_mr);
545         /* TP-MR */
546         packet[local_index++] = tp_mr;
547
548         /* TP-DA */
549         local_index += SmsUtilEncodeAddrField(packet + local_index, diallingNum, dialNumLen, 0x01, 0x01);
550
551         /* TP-PID */
552         packet[local_index++] = tp_pid;
553
554         /* TP_DCS (Data Coding Scheme) */
555         tpdu_submit.dcs.bCompressed = FALSE;
556         tpdu_submit.dcs.bmsg_class_set = TRUE;
557         tpdu_submit.dcs.coding_group_type = SMS_CODGRP_SM_GENERAL_DCS;
558         tpdu_submit.dcs.alphabet_type = SMS_ALPHABET_DEFAULT;
559         tpdu_submit.dcs.class_type = SMS_CLASS_1;
560
561         SmsUtilEncodeDCS(&tp_dcs, &tpdu_submit.dcs);
562
563         packet[local_index++] = tp_dcs;
564
565         /* TP-VP */
566         tpdu_submit.vp.vp_rel_type = SMS_VP_REL_6H;
567         tpdu_submit.vp.vp_type = SMS_VP_RELATIVE;
568         tpdu_submit.vp.vpValue = 0xff;
569
570         SmsUtilEncodeValidity(&tp_vp, &tpdu_submit.vp);
571
572         packet[local_index++] = tp_vp;
573
574         /* UDL */
575         packet[local_index++] = msg_len;
576
577         /* TP-UD */
578         if (tpdu_submit.dcs.alphabet_type == SMS_ALPHABET_DEFAULT) {
579                 int pos;
580                 pos = SmsUtilPackGSMCode(packet + local_index, msg, msg_len);
581                 local_index += pos;
582         }
583
584         msg("Send packet to phonseSever[%d] -----", local_index);
585
586         for (i = 0; i < local_index; i++) {
587                 msg("[%02x]", packet[i]);
588                 if (i % 10 == 9)
589                         msg("\n");
590         }
591         msg("\n");
592
593         memcpy(pDataPackage->szData, packet, local_index);
594         pDataPackage->MsgLength = local_index;
595
596         msg("***Sending the Message (Api:SendMsg)***");
597
598         msg("Length : %d", pDataPackage->MsgLength);
599
600         pDataPackage->format = TAPI_NETTEXT_NETTYPE_3GPP;
601         msg("Format : %d", pDataPackage->format);
602
603         returnStatus = tel_send_sms(handle, pDataPackage, 0, on_resp_send_msg, NULL);
604
605         msg("SendMsg : returnStatus=%d requestId=%d", returnStatus, requestId);
606
607         free(pDataPackage);
608
609         return TRUE;
610
611 }
612
613
614 static int DecodeSmsDeliverTpdu(int tpdu_len, char *pTPDU)
615 {
616         TPDU_SMS_DELIVER tpdu_deliver;
617         TmDateTime SCTimeStamp;
618         int orgAddr_len = 0;
619         char diallingNum[SMS_ADDRESS_LEN_MAX + 1];
620         char orgAddr[SMS_ADDRESS_LEN_MAX + 1]; /* originating number */
621         int org_ton, org_npi;
622         int position = 0;
623
624         /* TP-MTI, TP-MMS, TP-SRI, TP_UDHI, TP-RP */
625         tpdu_deliver.msg_type = SMS_TPDU_DELIVER;
626         tpdu_deliver.mms = (pTPDU[position] & 0x04) ? TRUE : FALSE; /* bit 2 (Message Type Indicator) */
627         tpdu_deliver.sri = (pTPDU[position] & 0x20) ? TRUE : FALSE; /* bit 5 (Status Report Indication) */
628         tpdu_deliver.udhi = (pTPDU[position] & 0x40) ? TRUE : FALSE; /* bit 6 (User Data Header Indicator) */
629         tpdu_deliver.rp = (pTPDU[position] & 0x80) ? TRUE : FALSE; /* bit 7 (Reply Path) */
630
631         position++;
632
633         /* TP-OA */
634         memset(diallingNum, 0, sizeof(diallingNum));
635
636         orgAddr_len = SmsUtilDecodeAddrField(diallingNum, &pTPDU[position],
637                         &org_ton, &org_npi);
638         if (orgAddr_len > SMS_ADDRESS_LEN_MAX - 1)
639                 orgAddr_len = SMS_ADDRESS_LEN_MAX - 1;
640
641         position += 2;
642
643         if (orgAddr_len % 2)
644                 position += orgAddr_len / 2 + 1;
645         else
646                 position += orgAddr_len / 2;
647
648         msg("pos = %d, length = %d", position, orgAddr_len);
649
650         if (org_ton == SMS_TON_INTERNATIONAL) {
651                 orgAddr[0] = '+';
652                 memcpy(&orgAddr[1], diallingNum, orgAddr_len - 1);
653                 orgAddr[orgAddr_len + 1] = '\0';
654         } else {
655                 memcpy(orgAddr, diallingNum, orgAddr_len);
656                 orgAddr[orgAddr_len] = '\0';
657         }
658
659         /* TP-PID */
660         tpdu_deliver.pId = pTPDU[position];
661
662         position++;
663
664         /* TP-DCS */
665         SmsUtilDecodeDCS(&tpdu_deliver.dcs, pTPDU[position]);
666         dcs_pos = position;
667         position++;
668
669         /* TP-SCTS */
670         SmsUtilDecodeTimeStamp(&pTPDU[position], &SCTimeStamp);
671
672         position += 7;
673
674         /* TP-UDL */
675         tpdu_deliver.udl = pTPDU[position];
676
677         if (tpdu_deliver.udl > SMS_SMDATA_SIZE_MAX)
678                 tpdu_deliver.udl = SMS_SMDATA_SIZE_MAX;
679
680         position++;
681
682         /* TP-UD */
683         tpdu_deliver.userData = malloc(sizeof(unsigned char) * (SMS_SMDATA_SIZE_MAX + 1));
684         if (!tpdu_deliver.userData)
685                 return 0;
686
687         memset(tpdu_deliver.userData, 0,
688                 sizeof(unsigned char) * (SMS_SMDATA_SIZE_MAX + 1));
689
690         if (tpdu_deliver.dcs.alphabet_type == SMS_ALPHABET_DEFAULT) {
691                 char inData[SMS_SMDATA_SIZE_MAX + 1];
692
693                 memset(inData, 0x00, SMS_SMDATA_SIZE_MAX + 1);
694
695                 memcpy((void *)inData, (void *)&pTPDU[position], tpdu_deliver.udl);
696
697                 SmsUtilUnpackGSMCode(tpdu_deliver.userData, inData,
698                                 (unsigned char) tpdu_deliver.udl);
699         }
700
701         msg("*************************************");
702         msg("Msg Type: TPDU_DELIVER");
703
704         if (tpdu_deliver.rp)
705                 msg("RP: Set Reply Path");
706         else if (!tpdu_deliver.rp)
707                 msg("RP: Not set Reply Path");
708
709         if (tpdu_deliver.sri)
710                 msg("SRI: A status report shall be returned to the SME");
711         else if (!tpdu_deliver.sri)
712                 msg("SRI: A status report shall be not returned to the SME");
713
714         if (tpdu_deliver.mms)
715                 msg("MMS: No more messages are waiting for the MS in this SC");
716         else if (!tpdu_deliver.mms)
717                 msg("MMS: More messages are waiting for the MS in this SC");
718
719         if (tpdu_deliver.udhi)
720                 msg("UDHI: Contains a Header in addition to the short message.");
721         else if (!tpdu_deliver.udhi)
722                 msg("UDHI: Only short message.");
723
724         msg("DcsClass : %x (4 means Class None) ", tpdu_deliver.dcs.class_type);
725
726         msg("From : %10s", orgAddr);
727         msg("Time : %d-%d-%d  , %d:%d:%d",
728                 SCTimeStamp.year, SCTimeStamp.month,
729                 SCTimeStamp.day, SCTimeStamp.hour, SCTimeStamp.minute, SCTimeStamp.second);
730         msg("Message : %s ", tpdu_deliver.userData);
731         msg("*************************************");
732
733         if (tpdu_deliver.userData)
734                 free(tpdu_deliver.userData);
735
736         return dcs_pos;
737 }
738
739 static int DecodeSmsStatusReportTpdu(int tpdu_len, char *pTPDU)
740 {
741         TPDU_SMS_STATUS_REPORT status_report;
742
743         TmDateTime SCTimeStamp;
744         TmDateTime DischargeTime;
745
746         int rcpAddr_len = 0;
747         char diallingNum[SMS_ADDRESS_LEN_MAX];
748
749         char rcpAddr[SMS_ADDRESS_LEN_MAX]; /* recipient number */
750         int rcp_ton, rcp_npi;
751         int position = 0;
752         unsigned int mask;
753
754         /* TP-MTI, TP MMS, TP SRQ, TP_UDHI */
755
756         status_report.msg_type = pTPDU[position] & 0x03; /* bit 0, 1 */
757         status_report.mms = pTPDU[position] & 0x04 ? TRUE : FALSE; /* bit 2 */
758         status_report.srq = pTPDU[position] & 0x20 ? TRUE : FALSE; /* bit 5 */
759         status_report.udhi = pTPDU[position] & 0x40 ? TRUE : FALSE; /* bit 6 */
760
761         position++;
762
763         /* TP-MR */
764         status_report.mr = pTPDU[position];
765         position++;
766
767         /* TP-RA */
768         memset(diallingNum, 0, sizeof(diallingNum));
769
770         SmsUtilDecodeAddrField(diallingNum, &pTPDU[position], &rcp_ton, &rcp_npi);
771
772         position += 2;
773
774         rcpAddr_len = strlen((char *)diallingNum);
775         if (rcpAddr_len % 2)
776                 position += rcpAddr_len / 2 + 1;
777         else
778                 position += rcpAddr_len / 2;
779
780         if (rcp_ton == SMS_TON_INTERNATIONAL) {
781                 rcpAddr[0] = '+';
782                 memcpy(&rcpAddr[1], diallingNum, rcpAddr_len);
783                 rcpAddr[rcpAddr_len + 1] = '\0';
784         } else {
785                 memcpy(rcpAddr, diallingNum, rcpAddr_len);
786                 rcpAddr[rcpAddr_len] = '\0';
787         }
788
789         status_report.rcpAddr.DialNumLen = rcpAddr_len;
790         memcpy(status_report.rcpAddr.szDiallingNum, rcpAddr, rcpAddr_len);
791         status_report.rcpAddr.Npi = rcp_npi;
792         status_report.rcpAddr.Ton = rcp_ton;
793
794         /* TP-SCTS */
795         SmsUtilDecodeTimeStamp(&pTPDU[position], &SCTimeStamp);
796         position += 7;
797
798         /* TP-DT */
799         SmsUtilDecodeTimeStamp(&pTPDU[position], &DischargeTime);
800         position += 7;
801
802         /* TP-ST */
803         status_report.status = pTPDU[position];
804         position++;
805
806         /* TP-PI */
807         status_report.paraInd = pTPDU[position];
808
809         mask = status_report.paraInd;
810
811         if ((mask != 0xFF) && (mask != 0)) {
812                 /* Increment only if mask is valid */
813                 position++;
814
815                 /* TP-PID */
816                 if (mask & 0x01) {
817                         status_report.pId = pTPDU[position];
818                         position++;
819                 }
820
821                 /* TP-DCS */
822                 if (mask & 0x02) {
823                         SmsUtilDecodeDCS(&status_report.dcs, pTPDU[position]);
824                         position++;
825                 }
826
827                 if (mask & 0x04) {
828                         /* TP-UDL */
829                         status_report.udl = pTPDU[position];
830                         position++;
831
832                         /* TP-UD */
833
834                         status_report.userData = malloc(sizeof(unsigned char) * (SMS_SMDATA_SIZE_MAX + 1));
835                         if (!status_report.userData)
836                                 return -1;
837
838                         memset(status_report.userData, 0,
839                                 sizeof(unsigned char) * (SMS_SMDATA_SIZE_MAX + 1));
840
841                         if (status_report.dcs.alphabet_type == SMS_ALPHABET_DEFAULT) {
842                                 char inData[SMS_SMDATA_SIZE_MAX + 1];
843
844                                 memset(inData, 0x00, SMS_SMDATA_SIZE_MAX + 1);
845
846                                 memcpy((void *)inData, (void *)&pTPDU[position],
847                                                 status_report.udl);
848
849                                 SmsUtilUnpackGSMCode(status_report.userData, inData,
850                                                 (unsigned char)status_report.udl);
851                         }
852
853                 }
854         }
855
856         msg("*************************************");
857         msg("Msg Type: STATUS_REPORT");
858         if (status_report.mms)
859                 msg("MMS: No more messages are waiting for the MS in this SC");
860         else if (!status_report.mms)
861                 msg("MMS: More messages are waiting for the MS in this SC");
862
863         if (status_report.srq)
864                 msg("SRQ: the result of an SMS COMMAND ");
865         else if (!status_report.srq)
866                 msg("SRQ: the result of a SMS SUBMIT.");
867
868         if (status_report.udhi)
869                 msg("UDHI: Contains a Header in addition to the short message.");
870         else if (!status_report.udhi)
871                 msg("UDHI: Only Short Message");
872
873         msg("STATUS:%x", status_report.status);
874         if (status_report.status == 0x00)
875                 msg("STATUS : Short message received by the SME");
876         else if (status_report.status == 0x01)
877                 msg("STATUS : Short message forwarded by the SC to the SMEbut the SC is unable to confirm delivery");
878         else if (status_report.status == 0x02)
879                 msg("STATUS : Short message replaced by the SC");
880
881         msg("Recipient Number : %s", rcpAddr);
882         msg("SC Time Stamp : %d-%d-%d  , %d:%d:%d",
883                 SCTimeStamp.year, SCTimeStamp.month, SCTimeStamp.day,
884                 SCTimeStamp.hour, SCTimeStamp.minute, SCTimeStamp.second);
885         msg("Discharge Time : %d-%d-%d  , %d:%d:%d",
886                 DischargeTime.year, DischargeTime.month, DischargeTime.day,
887                 DischargeTime.hour, DischargeTime.minute, DischargeTime.second);
888
889         if (mask & 0x04)
890                 msg("Message : %s ", status_report.userData);
891
892         msg("*************************************");
893
894         if (status_report.userData)
895                 free(status_report.userData);
896
897         return 1;
898 }
899
900 static void __on_noti_3gpp_sms_incoming_msg(TapiHandle *handle, TelSmsDatapackageInfo_t *datapackage,
901         void *user_data)
902 {
903         int scaAddr_len = 0;
904         char *pTPDU;
905         int tpdu_len = 0;
906         unsigned char diallingNum[TAPI_NETTEXT_ADDRESS_LEN_MAX + 1] = { 0, };
907         unsigned char scaAddr[TAPI_NETTEXT_SCADDRESS_LEN_MAX + 2] = { 0, }; /* Service Center Address */
908         int sca_ton, sca_npi;
909         int position;
910         int offset = 0;
911
912         unsigned char MTI = 0;
913         unsigned char MMS = 0;
914         unsigned char SRI = 0;
915         unsigned char UDHI = 0;
916         unsigned char RP = 0;
917
918         Sms_coding_scheme dcs;
919         TapiResult_t returnStatus;
920         int sca_length;
921         position = 0;
922
923         msg("");
924         msgb("event(%s) receive !!", TAPI_NOTI_SMS_INCOM_MSG);
925
926         memset(diallingNum, 0, sizeof(diallingNum));
927
928         sca_length = datapackage->Sca[0];
929
930         sca_ton = (datapackage->Sca[offset + 1] & 0x70) >> 4;
931         sca_npi = datapackage->Sca[offset + 1] & 0x0F;
932
933         msg("TON %d", sca_ton);
934         msg("NPI %d", sca_npi);
935
936         /* Modem Send the hex encoded SCA Service center digits*/
937         scaAddr_len = _util_hexstring_to_asciistring(&datapackage->Sca[2], (unsigned char *)diallingNum, sca_length-1);
938
939         msg("SCA ascii length%d", scaAddr_len);
940         msg("SCA Number:[%s]", diallingNum);
941
942         if (scaAddr_len > TAPI_NETTEXT_SCADDRESS_LEN_MAX)
943                 scaAddr_len = TAPI_NETTEXT_SCADDRESS_LEN_MAX;
944
945         if (sca_ton == SMS_TON_INTERNATIONAL) {
946                 scaAddr[0] = '+';
947                 memcpy(&scaAddr[1], diallingNum, scaAddr_len);
948         } else {
949                 memcpy(scaAddr, diallingNum, scaAddr_len);
950         }
951
952         msg("Sc address in test app is %s", scaAddr);
953
954         RP = datapackage->szData[position] & 0x80;
955         UDHI = datapackage->szData[position] & 0x40;
956         SRI = datapackage->szData[position] & 0x20;
957         MMS = datapackage->szData[position] & 0x04;
958         MTI = datapackage->szData[position] & 0x03;
959
960         msg("RP [%x]", RP);
961         msg("UDHI [%x]", UDHI);
962         msg("SRI [%x]", SRI);
963         msg("MMS [%x]", MMS);
964         msg("MTI [%02x]", MTI);
965
966         tpdu_len = datapackage->MsgLength;
967
968         pTPDU = malloc(sizeof(unsigned char) * tpdu_len);
969         if (pTPDU == NULL)
970                 return;
971
972         memcpy(pTPDU, &datapackage->szData[position], tpdu_len);
973
974         if (MTI == SMS_TPDU_DELIVER) {
975                 DecodeSmsDeliverTpdu(tpdu_len, pTPDU);
976
977                 msg("dcs_pos : %d", dcs_pos);
978
979                 SmsUtilDecodeDCS(&dcs, datapackage->szData[position + dcs_pos]);
980
981                 /* Simulating the behaviour of MSG_SERVICE:
982                   * Storing only the Class two sms
983                   *     @todo:refactoring
984                   */
985                 if (dcs.class_type == SMS_CLASS_2) {
986                         TelSmsData_t WriteData;
987
988                         msg("dcs type is of class2");
989
990                         memset(&WriteData, 0, sizeof(TelSmsData_t));
991
992                         memcpy(WriteData.SmsData.Sca, datapackage->Sca, sca_length+1);
993
994                         WriteData.SmsData.MsgLength = datapackage->MsgLength;
995
996                         memcpy(WriteData.SmsData.szData, datapackage->szData, datapackage->MsgLength);
997
998                         WriteData.MsgStatus = TAPI_NETTEXT_STATUS_READ;
999
1000                         returnStatus = tel_write_sms_in_sim(handle, &WriteData, on_resp_save_msg, NULL);
1001
1002                         msg("returnStatus for tel_write_sms_in_sim(): 0x%x", returnStatus);
1003                 }
1004
1005         } else if (MTI == SMS_TPDU_STATUS_REPORT) {
1006                 DecodeSmsStatusReportTpdu(tpdu_len, pTPDU);
1007         }
1008
1009         TelSmsDatapackageInfo_t *del_report = NULL;
1010
1011         del_report = malloc(sizeof(TelSmsDatapackageInfo_t));
1012         if (del_report == NULL) {
1013                 free(pTPDU);
1014                 return;
1015         }
1016         memset(del_report, 0, sizeof(TelSmsDatapackageInfo_t));
1017
1018         memcpy(del_report->Sca, datapackage->Sca, TAPI_SIM_SMSP_ADDRESS_LEN);
1019
1020         del_report->szData[0] = SMS_TPDU_DELIVER_REPORT; /* TP-UDHI[bit6] : 0 */
1021         del_report->szData[1] = 0; /* TP-PI = 0; bit2: TP-UDL bit1: TP-DCS bit0: TP-PID (No option field) */
1022         del_report->MsgLength = 2;
1023         del_report->format = TAPI_NETTEXT_NETTYPE_3GPP;
1024
1025         msg("***receive message (Api:SendDeliverreport)****");
1026
1027         returnStatus = tel_send_sms_deliver_report(handle, del_report,
1028                         TAPI_NETTEXT_SENDSMS_SUCCESS, on_resp_DeliverReport_msg, NULL);
1029
1030         msg("returnStatus for tel_send_sms_deliver_report(): 0x%x", returnStatus);
1031
1032         free(del_report);
1033
1034         free(pTPDU);
1035 }
1036
1037 static void __on_noti_3gpp2_sms_incoming_msg(TapiHandle *handle,
1038         TelSmsDatapackageInfo_t *datapackage, void *user_data)
1039 {
1040         TelSmsDatapackageInfo_t del_report;
1041         TapiResult_t returnStatus;
1042
1043         sms_util_decode_3gpp2(datapackage->MsgLength, datapackage->szData);
1044
1045         memset(&del_report, 0x00, sizeof(del_report));
1046         del_report.format = TAPI_NETTEXT_NETTYPE_3GPP2;
1047         returnStatus = tel_send_sms_deliver_report(handle, &del_report,
1048                         TAPI_NETTEXT_SENDSMS_SUCCESS, on_resp_DeliverReport_msg, NULL);
1049
1050         msg("returnStatus for tel_send_sms_deliver_report(): 0x%x", returnStatus);
1051 }
1052
1053 static void on_noti_sms_incom_msg(TapiHandle *handle, const char *noti_id,
1054                 void *data, void *user_data)
1055 {
1056         TelSmsDatapackageInfo_t *datapackage = data;
1057         if (datapackage == NULL)
1058                 return;
1059
1060         if (datapackage->format == TAPI_NETTEXT_NETTYPE_3GPP)
1061                 __on_noti_3gpp_sms_incoming_msg(handle, datapackage, user_data);
1062         else if (datapackage->format == TAPI_NETTEXT_NETTYPE_3GPP2)
1063                 __on_noti_3gpp2_sms_incoming_msg(handle, datapackage, user_data);
1064         else
1065                 msg("Invalid msg format: %d", datapackage->format);
1066 }
1067
1068 static void on_noti_sms_cb_incom_msg(TapiHandle *handle, const char *noti_id,
1069                 void *data, void *user_data)
1070 {
1071         TelSmsCbMsg_t *cbMsg = data;
1072         int length = 0;
1073         char pPDU[SMS_CB_SIZE_MAX + 3] = { 0, };
1074
1075         msg("");
1076         msgb("event(%s) receive !!", TAPI_NOTI_SMS_CB_INCOM_MSG);
1077
1078         length = cbMsg->Length;
1079         pPDU[0] = cbMsg->CbMsgType;
1080         pPDU[1] = length;
1081         memcpy(&(pPDU[2]), cbMsg->szMsgData, SMS_CB_SIZE_MAX + 1);
1082
1083         DecodeCellBroadcastMsg(length, pPDU);
1084 }
1085
1086 static void on_noti_sms_etws_incom_msg(TapiHandle *handle, const char *noti_id,
1087                 void *data, void *user_data)
1088 {
1089         TelSmsEtwsMsg_t *etwsMsg = data;
1090         int length = 0;
1091         char pPDU[TAPI_NETTEXT_ETWS_SIZE_MAX + 3] = { 0, };
1092
1093         msg("");
1094         msgb("event(%s) receive !!", TAPI_NOTI_SMS_ETWS_INCOM_MSG);
1095
1096         length = etwsMsg->Length;
1097         pPDU[0] = etwsMsg->EtwsMsgType;
1098         pPDU[1] = length;
1099         memcpy(&(pPDU[2]), etwsMsg->szMsgData, TAPI_NETTEXT_ETWS_SIZE_MAX + 1);
1100 }
1101
1102 static void on_noti_sms_memory_status(TapiHandle *handle, const char *noti_id,
1103                 void *data, void *user_data)
1104 {
1105         int *memory_status = data;
1106
1107         msg("");
1108         msgb("event(%s) receive !!", TAPI_NOTI_SMS_MEMORY_STATUS);
1109         msg(" - memory_status = 0x%x", *memory_status);
1110 }
1111
1112 static void on_noti_sms_ready_status(TapiHandle *handle, const char *noti_id,
1113                 void *data, void *user_data)
1114 {
1115         int *device_ready = data;
1116
1117         msg("");
1118         msgb("event(%s) receive !!", TAPI_NOTI_SMS_DEVICE_READY);
1119         msg(" - device_ready = %d", *device_ready);
1120
1121         if (TAPI_NETTEXT_READY_STATUS_3GPP == *device_ready)
1122                 msg("TAPI_NETTEXT_READY_STATUS_3GPP");
1123         else if (TAPI_NETTEXT_READY_STATUS_3GPP2 == *device_ready)
1124                 msg("TAPI_NETTEXT_READY_STATUS_3GPP");
1125         else if (TAPI_NETTEXT_READY_STATUS_3GPP_AND_3GPP2 == *device_ready)
1126                 msg("TAPI_NETTEXT_READY_STATUS_3GPP_AND_3GPP2");
1127         else
1128                 msg("TAPI_NETTEXT_READY_STATUS_NONE");
1129 }
1130
1131 static void on_resp_send_msg(TapiHandle *handle, int result, void *data,
1132                 void *user_data)
1133 {
1134         msg("");
1135         msgb("tel_send_sms() response receive");
1136         msg(" - result = 0x%x", result);
1137 }
1138
1139 static void on_resp_send_cdma_msg(TapiHandle *handle, int result, void *data,
1140                 void *user_data)
1141 {
1142         msg("");
1143         msgb("CDMA tel_send_sms() response received");
1144         msg(" - result = 0x%x", result);
1145 }
1146
1147 static void on_resp_read_msg(TapiHandle *handle, int result, void *data,
1148                 void *user_data)
1149 {
1150         TelSmsData_t *sim_data;
1151         int scaAddr_len = 0;
1152         char *pTPDU;
1153         int tpdu_len = 0;
1154         char diallingNum[TAPI_NETTEXT_ADDRESS_LEN_MAX + 1] = { 0, };
1155         char scaAddr[TAPI_NETTEXT_SCADDRESS_LEN_MAX + 2] = { 0, }; /* Service Center Address */
1156         int sca_ton, sca_npi;
1157         int position;
1158
1159         if (data == NULL) {
1160                 msg("data is Null");
1161                 return;
1162         }
1163
1164         sim_data = (TelSmsData_t *)data;
1165         msg("SIM Index [%d]", sim_data->SimIndex);
1166
1167         if (sim_data->MsgStatus == TAPI_NETTEXT_STATUS_UNREAD)
1168                 msg("Msg Staus : received unread msg");
1169         else if (sim_data->MsgStatus == TAPI_NETTEXT_STATUS_READ)
1170                 msg("Msg Staus : received read msg");
1171         else if (sim_data->MsgStatus == TAPI_NETTEXT_STATUS_UNSENT)
1172                 msg("Msg Staus : unsent msg");
1173         else
1174                 msg("Msg Staus : [%d]", sim_data->MsgStatus);
1175
1176         msg("First Data [%x]", sim_data->SmsData.szData[0]);
1177         msg("Second Data [%x]", sim_data->SmsData.szData[1]);
1178         msg("Third Data [%x]", sim_data->SmsData.szData[2]);
1179         msg("Fourth Data [%x]", sim_data->SmsData.szData[3]);
1180
1181         position = 0;
1182         /* SCA_ADDR */
1183         memset(diallingNum, 0, sizeof(diallingNum));
1184
1185         SmsUtilDecodeAddrField(diallingNum, (char *)sim_data->SmsData.Sca,
1186                         &sca_ton, &sca_npi);
1187
1188         position += 2;  /* include Address-Length, Type of Address */
1189
1190         scaAddr_len = strlen((char *)diallingNum);
1191         if (scaAddr_len > TAPI_NETTEXT_SCADDRESS_LEN_MAX - 1)
1192                 scaAddr_len = TAPI_NETTEXT_SCADDRESS_LEN_MAX - 1;
1193
1194         if (scaAddr_len % 2)
1195                 position += scaAddr_len / 2 + 1;
1196         else
1197                 position += scaAddr_len / 2;
1198
1199         if (sca_ton == SMS_TON_INTERNATIONAL) {
1200                 scaAddr[0] = '+';
1201                 memcpy(&scaAddr[1], diallingNum, scaAddr_len);
1202         } else {
1203                 memcpy(scaAddr, diallingNum, scaAddr_len);
1204         }
1205
1206         tpdu_len = sim_data->SmsData.MsgLength;
1207
1208         msg("SCA Number : %s tpdu_len is %d", scaAddr, tpdu_len);
1209
1210         pTPDU = malloc(sizeof(unsigned char) * tpdu_len);
1211         if (!pTPDU)
1212                 return;
1213
1214         msg("bfor memcopy position is %d", position);
1215         memcpy(pTPDU, & (sim_data->SmsData.szData[0]), tpdu_len);
1216         msg("after memcpy");
1217
1218         DecodeSmsDeliverTpdu(tpdu_len, pTPDU);
1219
1220         free(pTPDU);
1221 }
1222
1223 static void on_resp_save_msg(TapiHandle *handle, int result, void *data,
1224                 void *user_data)
1225 {
1226         int *local_index = data;
1227
1228         msg("");
1229         msgb("tel_write_sms_in_sim() response receive");
1230         msg(" - result = 0x%x", result);
1231         msg(" - local_index = %d", *local_index);
1232 }
1233
1234 static void on_resp_delete_msg(TapiHandle *handle, int result, void *data,
1235                 void *user_data)
1236 {
1237         int *local_index = data;
1238
1239         msg("");
1240         msgb("tel_delete_sms_in_sim() response receive");
1241         msg(" - result = 0x%x", result);
1242         msg(" - local_index = %d", *local_index);
1243 }
1244
1245 static void on_resp_DeliverReport_msg(TapiHandle *handle, int result,
1246                 void *data, void *user_data)
1247 {
1248         int *local_index = data;
1249
1250         msg("");
1251         msgb("tel_send_sms_deliver_report() response receive");
1252         msg(" - result = 0x%x", result);
1253         msg(" - local_index = %d", *local_index);
1254 }
1255
1256 static void on_resp_set_sms_sca(TapiHandle *handle, int result, void *data,
1257                 void *user_data)
1258 {
1259         msg("");
1260         msgb("tel_set_sms_sca() response receive");
1261         msg(" - result = 0x%x", result);
1262 }
1263
1264 static void on_resp_set_sms_cb_config(TapiHandle *handle, int result,
1265                 void *data, void *user_data)
1266 {
1267         msg("");
1268         msgb("tel_set_sms_cb_config() response receive");
1269         msg(" - result = 0x%x", result);
1270 }
1271
1272 static void on_resp_set_sms_params(TapiHandle *handle, int result, void *data,
1273                 void *user_data)
1274 {
1275         msg("");
1276         msgb("tel_set_sms_params() response receive");
1277         msg(" - result = 0x%x", result);
1278 }
1279
1280 static void on_resp_set_mem_status(TapiHandle *handle, int result, void *data,
1281                 void *user_data)
1282 {
1283         msg("");
1284         msgb("tel_set_sms_mem_status() response receive");
1285         msg(" - result = 0x%x", result);
1286 }
1287
1288 static void on_resp_set_msg_status(TapiHandle *handle, int result, void *data,
1289                 void *user_data)
1290 {
1291         msg("");
1292         msgb("tel_set_sms_message_status() response receive");
1293         msg(" - result = 0x%x", result);
1294 }
1295
1296 static void on_resp_get_sms_sca(TapiHandle *handle, int result, void *data,
1297                 void *user_data)
1298 {
1299         TelSmsAddressInfo_t *scaInfo = data;
1300         unsigned int i = 0;
1301
1302         msg("");
1303         msgb("tel_get_sms_sca() response receive");
1304         msg(" - result = 0x%x", result);
1305         msg(" - TON = %d", scaInfo->Ton);
1306         msg(" - NPI = %d", scaInfo->Npi);
1307         msg(" - DialNumLen = %d", scaInfo->DialNumLen);
1308         msg(" - SCA Num");
1309
1310         for (i = 0; i < scaInfo->DialNumLen; i++) {
1311                 msg("[%02x]", scaInfo->szDiallingNum[i]);
1312                 if (i % 10 == 9)
1313                         msg("\n");
1314         }
1315 }
1316
1317 static void on_resp_get_cb_config(TapiHandle *handle, int result, void *data,
1318                 void *user_data)
1319 {
1320         TelSmsCbConfig_t *CBConfig;
1321         int i = 0;
1322
1323         if (data == NULL) {
1324                 msg("pData is Null");
1325                 return;
1326         }
1327
1328         CBConfig = (TelSmsCbConfig_t *)data;
1329
1330         msg("");
1331         msgb("tel_get_sms_cb_config() response receive");
1332         msg(" - result = 0x%x", result);
1333
1334         msg("=========CB Configuration=========");
1335
1336         /*** CB Enable/Diable ***/
1337         if (CBConfig->CBEnabled == TRUE)
1338                 msg("Cell Broadcast Msg Enabled...");
1339         else
1340                 msg("Cell Broadcast Msg Disabled...");
1341
1342         /*** CBMI  Count ***/
1343         msg("CBMI Range Count: %d", CBConfig->MsgIdRangeCount);
1344
1345         /*** CBMI  List ***/
1346         if (CBConfig->Net3gppType == 0x01) {
1347                 msg("Network type is 3gpp\n");
1348                 if (CBConfig->MsgIdRangeCount != 0) {
1349                         msg("----- CBMI List -----");
1350                         for (i = 0; i < CBConfig->MsgIdRangeCount; i++) {
1351                                 msg("From No.%d - [0x%04x]", i,
1352                                         CBConfig->MsgIDs[i].Net3gpp.FromMsgId);
1353                                 msg("To No.%d - [0x%04x]", i, CBConfig->MsgIDs[i].Net3gpp.ToMsgId);
1354                         }
1355                 }
1356         } else if (CBConfig->Net3gppType == 0x02) {
1357                 msg("Network type is CDMA\n");
1358                 if (CBConfig->MsgIdRangeCount != 0) {
1359                         msg("----- CBMI List -----");
1360                         for (i = 0; i < CBConfig->MsgIdRangeCount; i++) {
1361                                 msg("No.%d Service Category - [0x%04x]", i, CBConfig->MsgIDs[i].Net3gpp2.CBCategory);
1362                                 msg("No.%d Language - [0x%02x]", i, CBConfig->MsgIDs[i].Net3gpp2.CBLanguage);
1363                                 msg("No.%d Selected - [0x%02x]", i, CBConfig->MsgIDs[i].Net3gpp2.Selected);
1364                         }
1365                 }
1366         }
1367
1368         msg("==================================");
1369 }
1370
1371 static void on_resp_get_sms_parameters(TapiHandle *handle, int result,
1372                 void *data, void *user_data)
1373 {
1374         TelSmsParams_t *smsp_param;
1375         int i = 0;
1376
1377         if (data == NULL) {
1378                 msg("data is Null");
1379                 return;
1380         }
1381         smsp_param = (TelSmsParams_t *)data;
1382
1383         msg("");
1384         msgb("tel_get_sms_parameters() response receive");
1385         msg(" - result = 0x%x", result);
1386
1387         msg("record index is 0x%x", smsp_param->RecordIndex);
1388         msg("record len  is 0x%x", smsp_param->RecordLen);
1389         msg("alpha_id len  is 0x%x ", (int)smsp_param->AlphaIdLen);
1390         msg("alpha_id is %s ", smsp_param->szAlphaId);
1391         msg("param indicator is  0x%x", smsp_param->ParamIndicator);
1392
1393         for (i = 0; i < (int) smsp_param->TpDestAddr.DialNumLen; i++)
1394                 msg("DestAddr = %d [%02x]", i, smsp_param->TpDestAddr.szDiallingNum[i]);
1395
1396         for (i = 0; i < (int) smsp_param->TpSvcCntrAddr.DialNumLen; i++)
1397                 msg("SCAddr = %d [%02x]", i, smsp_param->TpSvcCntrAddr.szDiallingNum[i]);
1398
1399         msg("pid 0x%x", smsp_param->TpProtocolId);
1400         msg("dcs is 0x%x", smsp_param->TpDataCodingScheme);
1401         msg("validity is 0x%x", smsp_param->TpValidityPeriod);
1402 }
1403
1404 static void on_resp_get_paramcnt(TapiHandle *handle, int result, void *data,
1405                 void *user_data)
1406 {
1407         int *RecordCount;
1408
1409         if (data == NULL) {
1410                 msg("data is Null");
1411                 return;
1412         }
1413
1414         RecordCount = (int *)data;
1415
1416         msg("");
1417         msgb("tel_get_sms_parameter_count() response receive");
1418         msg(" - result = 0x%x", result);
1419         msg(" - In param_count_noti the record count is %d", *RecordCount);
1420 }
1421
1422 static void on_resp_get_sms_count(TapiHandle *handle, int result, void *data,
1423                 void *user_data)
1424 {
1425         static TelSmsStoredMsgCountInfo_t *countInfo;
1426         int loop_counter = 0;
1427         TapiResult_t returnStatus = TAPI_API_SUCCESS;
1428
1429         if (data == NULL) {
1430                 msg("data is Null");
1431                 return;
1432         }
1433
1434         countInfo = (TelSmsStoredMsgCountInfo_t *)data;
1435
1436         msg("");
1437         msgb("tel_get_sms_count() response receive");
1438         msg(" - result = 0x%x", result);
1439
1440         if (countInfo->UsedCount != 0x00) {     /* if used count is not zero */
1441                 msg("Index LIST..........");
1442                 for (loop_counter = 0; loop_counter < countInfo->UsedCount;
1443                                 loop_counter++) {
1444                         msg("[%02x]", countInfo->IndexList[loop_counter]);
1445                         returnStatus = tel_read_sms_in_sim(handle,
1446                                         countInfo->IndexList[loop_counter], on_resp_read_msg,
1447                                         NULL);
1448                         msg("After read msg: returnstatus %d", returnStatus);
1449                 }
1450                 msg("In MsgCountNotification total cnt is %d, usedcnt is %dapi_err %d",
1451                                 countInfo->TotalCount, countInfo->UsedCount, returnStatus);
1452         }
1453
1454         msg("In MsgCountNotification total cnt is %d, usedcnt is %d",
1455                         countInfo->TotalCount, countInfo->UsedCount);
1456
1457 }
1458
1459 static int SendMessage(MManager *mm, struct menu_data *menu)
1460 {
1461         int ret;
1462         int msg_len = 0;
1463         char message[512] = { 0, };
1464         char diallingNum[SMS_ADDRESS_LEN_MAX + 1] = { 0, };
1465         int diallingNum_len = 0;
1466
1467         printf("\n");
1468         printf("Enter destination Number:\n>> ");
1469         fflush(stdout);
1470         ret = read(0, diallingNum, sizeof(diallingNum) - 1);
1471         if (ret < 0) {
1472                 if (errno == EINTR)
1473                         perror("read(1)");
1474                 return -1;
1475         } else if (ret == 0) {
1476                 printf("No destination Number!!\n");
1477                 return -1;
1478         }
1479         diallingNum_len = ret - 1;
1480         diallingNum[diallingNum_len] = '\0';
1481
1482         printf("Enter Message\n>> ");
1483         fflush(stdout);
1484         ret = read(0, message, sizeof(message) - 1);
1485         if (ret < 0) {
1486                 if (errno == EINTR)
1487                         perror("read(1)");
1488                 return -1;
1489         } else if (ret == 0) {
1490                 printf("No Message!!\n");
1491                 return -1;
1492         }
1493         msg_len = ret - 1;
1494         message[msg_len] = '\0';
1495
1496         printf("===========================\n");
1497         printf("To: [%s] (len: %d)\n", diallingNum, diallingNum_len);
1498         printf("Message: [%s]\nMsg Length: [%d]\n", message, msg_len);
1499         printf("===========================\n\n");
1500
1501         EncodeSmsSubmitTpdu(mm, diallingNum, diallingNum_len, message, msg_len);
1502         return 1;
1503
1504 }
1505
1506 static int SendMessageCDMA(MManager *mm, struct menu_data *menu)
1507 {
1508         int ret;
1509         int msg_len = 0;
1510         char message[512] = { 0, };
1511         char diallingNum[SMS_ADDRESS_LEN_MAX + 1] = { 0, };
1512         int diallingNum_len = 0;
1513
1514         printf("\n");
1515         printf("Enter destination Number:\n>> ");
1516         fflush(stdout);
1517         ret = read(0, diallingNum, sizeof(diallingNum) - 1);
1518         if (ret < 0) {
1519                 if (errno == EINTR)
1520                         perror("read(1)");
1521                 return -1;
1522         } else if (ret == 0) {
1523                 printf("No destination Number!!\n");
1524                 return -1;
1525         }
1526         diallingNum_len = ret - 1;
1527         diallingNum[diallingNum_len] = '\0';
1528
1529         printf("Enter Message\n>> ");
1530         fflush(stdout);
1531         ret = read(0, message, sizeof(message) - 1);
1532         if (ret < 0) {
1533                 if (errno == EINTR)
1534                         perror("read(1)");
1535                 return -1;
1536         } else if (ret == 0) {
1537                 printf("No Message!!\n");
1538                 return -1;
1539         }
1540         msg_len = ret - 1;
1541         message[msg_len] = '\0';
1542
1543         printf("===========================\n");
1544         printf("To: [%s] (len: %d)\n", diallingNum, diallingNum_len);
1545         printf("Message: [%s]\nMsg Length: [%d]\n", message, msg_len);
1546         printf("===========================\n\n");
1547
1548         if (EncodeCdmaSmsSubmitTpdu(mm, diallingNum, diallingNum_len, message, msg_len) == FALSE)
1549                 msg("EncodeCdmaSmsSubmitTpdu() failed.");
1550
1551         return 1;
1552 }
1553
1554 static int ReadMessage(MManager *mm, struct menu_data *menu)
1555 {
1556         TapiHandle *handle = menu_manager_ref_user_data(mm);
1557         int selectedNo;
1558         int ret;
1559         char buf[100];
1560         int returnStatus = 0;
1561
1562         memset(buf, 0, sizeof(buf));
1563
1564         msg("*************************************");
1565         msg("1. SIM Message/RUIM Message");
1566
1567         msg("Select Number:");
1568
1569         ret = read(0, buf, sizeof(buf));
1570
1571         if (ret < 0) {
1572                 if (errno == EINTR)
1573                         perror("read(1)");
1574                 return -1;
1575         } else if (ret == 0)
1576                 return ret;
1577
1578         selectedNo = atoi(buf);
1579         msg("Selected Num in read message(value is 1) is %d ", selectedNo);
1580         switch (selectedNo) {
1581         case 1:
1582                 msg("***Reading the message in SIM***");
1583                 returnStatus = tel_get_sms_count(handle, on_resp_get_sms_count, NULL);
1584                 msg("tel_get_sms_count():  0x%x", returnStatus);
1585         break;
1586
1587         default:
1588                 msg("Not supported Menu(%d) !!!", selectedNo);
1589         break;
1590         }
1591
1592         return 1;
1593 }
1594
1595 static int DeleteMessage(MManager *mm, struct menu_data *menu)
1596 {
1597         TapiHandle *handle = menu_manager_ref_user_data(mm);
1598
1599         int ret, selected_num, index;
1600         char buf[100];
1601
1602         TapiResult_t returnStatus;
1603
1604         memset(buf, 0, sizeof(buf));
1605
1606         msg("***Deleting the message(Api:DeleteMsg, GetMsgCount)***");
1607
1608         msg("Select Number:");
1609         msg("1. Delete message at a particular index");
1610         msg("2. Delete all messages");
1611
1612         msg("Select Number:");
1613
1614         ret = read(0, buf, sizeof(buf));
1615
1616         selected_num = atoi(buf);
1617         switch (selected_num) {
1618         case 1: {
1619                 msg("Enter delete index >>");
1620                 ret = read(0, buf, sizeof(buf));
1621                 if (ret < 0) {
1622                         if (errno == EINTR)
1623                                 perror("read(1)");
1624                         return -1;
1625                 } else if (ret == 0)
1626                         return ret;
1627                 index = atoi(buf);
1628         }
1629         break;
1630
1631         case 2: {
1632                 index = -1;
1633         }
1634         break;
1635
1636         default:
1637                 msg("Invalid Number Selected");
1638                 return -1;
1639         }
1640         msg("index value :%d", index);
1641
1642         returnStatus = tel_delete_sms_in_sim(handle, index, on_resp_delete_msg, NULL);
1643         msg("returnstatus  tel_delete_sms_in_sim()  is for delete%d ", returnStatus);
1644
1645         msg("Getting SMS Count after Deleting SMS");
1646         returnStatus = tel_get_sms_count(handle, on_resp_get_sms_count, NULL);
1647         msg("returnStatus for tel_get_sms_count()  %d", returnStatus);
1648
1649         return 1;
1650
1651 }
1652
1653 static int CdmaGetting(MManager *mm, struct menu_data *menu)
1654 {
1655         TapiHandle *handle = menu_manager_ref_user_data(mm);
1656         int selectedNo;
1657         int returnStatus = 0;
1658         int ready_status = 0;
1659
1660         selectedNo = atoi(menu->key);
1661         switch (selectedNo) {
1662         case 1:
1663                 msg("***Getting the CB Configuration(Api: GetCBConfig)***");
1664                 returnStatus = tel_get_sms_cb_config(handle, on_resp_get_cb_config, NULL);
1665                 msg("ReturnStatus[%d] ", returnStatus);
1666         break;
1667
1668         case 3:
1669                 msg("***Getting the SmsCount(Api:GetSmsCount)***");
1670                 returnStatus = tel_get_sms_count(handle, on_resp_get_sms_count, NULL);
1671                 msg("ReturnStatus[%d]", returnStatus);
1672         break;
1673
1674         case 4:
1675                 msg("***Getting the Sms Ready status(Api:Check_sms_device_status)***");
1676                 returnStatus = tel_check_sms_device_status(handle, &ready_status);
1677                 msg("API ReturnStatus[%d]", returnStatus);
1678                 msg("Ready status = %d", ready_status);
1679
1680                 if (TAPI_NETTEXT_READY_STATUS_3GPP == ready_status)
1681                         msg("TAPI_NETTEXT_READY_STATUS_3GPP");
1682                 else if (TAPI_NETTEXT_READY_STATUS_3GPP2 == ready_status)
1683                         msg("TAPI_NETTEXT_READY_STATUS_3GPP");
1684                 else if (TAPI_NETTEXT_READY_STATUS_3GPP_AND_3GPP2 == ready_status)
1685                         msg("TAPI_NETTEXT_READY_STATUS_3GPP_AND_3GPP2");
1686                 else
1687                         msg("TAPI_NETTEXT_READY_STATUS_NONE");
1688         break;
1689
1690         default:
1691                 return -1;
1692         }
1693
1694         return 1;
1695 }
1696
1697 static int Getting(MManager *mm, struct menu_data *menu)
1698 {
1699         TapiHandle *handle = menu_manager_ref_user_data(mm);
1700         int selectedNo;
1701         int returnStatus = 0;
1702         int ready_status = 0;
1703
1704         selectedNo = atoi(menu->key);
1705
1706         switch (selectedNo) {
1707         case 1:
1708                 msg("***Getting the SCA(Api: GetSCA)****");
1709                 returnStatus = tel_get_sms_sca(handle, 0, on_resp_get_sms_sca, NULL);
1710                 msg("returnstatus for tel_get_sms_sca(): 0x %x", returnStatus);
1711         break;
1712
1713         case 2:
1714                 msg("***Getting the CB Configuration(Api: GetCBConfig)***");
1715                 returnStatus = tel_get_sms_cb_config(handle, on_resp_get_cb_config, NULL);
1716                 msg("ReturnStatus[%d] ", returnStatus);
1717         break;
1718
1719         case 3:
1720                 msg("****Getting the parameter(Api:GetParameter)****");
1721                 returnStatus = tel_get_sms_parameters(handle, 0, on_resp_get_sms_parameters, NULL);
1722                 msg("ReturnStatus[%d]]", returnStatus);
1723         break;
1724
1725         case 5:
1726                 msg("***Getting the parameterCount(Api:GetParameterCount)***");
1727                 returnStatus = tel_get_sms_parameter_count(handle, on_resp_get_paramcnt, NULL);
1728                 msg("ReturnStatus[%d]", returnStatus);
1729         break;
1730
1731         case 6:
1732                 msg("***Getting the SmsCount(Api:GetSmsCount)***");
1733                 returnStatus = tel_get_sms_count(handle, on_resp_get_sms_count, NULL);
1734                 msg("ReturnStatus[%d]", returnStatus);
1735         break;
1736
1737         case 7:
1738                 msg("***Getting the Sms Ready status(Api:Check_sms_device_status)***");
1739                 returnStatus = tel_check_sms_device_status(handle, &ready_status);
1740                 msg("ReturnStatus[%d]", returnStatus);
1741                 msg("Ready status = %d", ready_status);
1742
1743                 if (TAPI_NETTEXT_READY_STATUS_3GPP == ready_status)
1744                         msg("TAPI_NETTEXT_READY_STATUS_3GPP");
1745                 else if (TAPI_NETTEXT_READY_STATUS_3GPP2 == ready_status)
1746                         msg("TAPI_NETTEXT_READY_STATUS_3GPP");
1747                 else if (TAPI_NETTEXT_READY_STATUS_3GPP_AND_3GPP2 == ready_status)
1748                         msg("TAPI_NETTEXT_READY_STATUS_3GPP_AND_3GPP2");
1749                 else
1750                         msg("TAPI_NETTEXT_READY_STATUS_NONE");
1751         break;
1752
1753         default:
1754                 return -1;
1755
1756         }
1757
1758         return 1;
1759 }
1760
1761 static int _get_int()
1762 {
1763         char buf[255];
1764         int ret;
1765
1766         memset(buf, 0, 255);
1767         ret = read(0, buf, 254);
1768         if (ret <= 0)
1769                 return 0;
1770
1771         return atoi(buf);
1772 }
1773
1774 static int CdmaSetting(MManager *mm, struct menu_data *menu)
1775 {
1776         TapiHandle *handle = menu_manager_ref_user_data(mm);
1777
1778         int settingMenu;
1779         int ret;
1780
1781         char MemoryStatus[255] = {0, };
1782
1783         TapiResult_t returnStatus;
1784
1785         settingMenu = atoi(menu->key);
1786
1787         switch (settingMenu) {
1788         case 2: { /* Set CB Enable/Disable */
1789                 msg("****Setting the CB configuration(Api:SetCbConfig)****");
1790                 CdmaCbSetting(mm, menu);
1791         }
1792         break;
1793
1794         case 3: { /* Set Memory Full Notification */
1795                 msg("Enter Memory Status to be set(1:Memory Available, 2:Memory Full)");
1796                 memset(MemoryStatus, 0, sizeof(MemoryStatus));
1797                 ret = read(0, MemoryStatus, sizeof(MemoryStatus));
1798                 if (ret <= 0) {
1799                         msg(" NULL msg can NOT be sent ");
1800                         return -1;
1801                 }
1802
1803                 msg("Memory Status type is %d ", atoi(MemoryStatus));
1804                 returnStatus = tel_set_sms_memory_status(handle,
1805                                 atoi(MemoryStatus), on_resp_set_mem_status, NULL); /* Set to full 0x02-Full, 0x01-available */
1806                 msg("api err after memstatus set is is %d", returnStatus);
1807         }
1808         break;
1809
1810         case 4: { /* Set Stored MsgStaus */
1811                 msg("Not suppored in this Test App !!!");
1812         }
1813         break;
1814
1815         default:
1816                 return -1;
1817         }
1818
1819         return 1;
1820 }
1821
1822 static int Setting(MManager *mm, struct menu_data *menu)
1823 {
1824         TapiHandle *handle = menu_manager_ref_user_data(mm);
1825
1826         int settingMenu;
1827         int ret;
1828         int i;
1829
1830         char MemoryStatus[255] = {0, };
1831         char msg_status[10] = {0, };
1832         char index[10] = {0, };
1833
1834         TelSmsCbConfig_t *pCBConfig;
1835
1836         TelSmsParams_t smsParameters = { 0, };
1837         TapiResult_t returnStatus;
1838
1839         settingMenu = atoi(menu->key);
1840
1841         switch (settingMenu) {
1842         case 1: { /* Set Service Center Number */
1843                 TelSmsAddressInfo_t sca = {0, };
1844                 unsigned char sca_num[TAPI_SIM_SMSP_ADDRESS_LEN + 1];
1845                 unsigned int sca_length = 0;
1846                 unsigned int additional_len = 0;
1847                 unsigned local_index, j;
1848
1849                 msg("*** Setting SCA (API: tel_set_sms_sca()) ****");
1850
1851 RETRY:
1852                 msg("Enter the SCA NUMBER:");
1853                 ret = scanf("%s", sca_num);
1854                 if (ret > TAPI_SIM_SMSP_ADDRESS_LEN) {
1855                         msg("Entered SCA is INVALID - SCA length cannot be greater than %d", TAPI_SIM_SMSP_ADDRESS_LEN);
1856                         goto RETRY;
1857                 }
1858
1859                 sca.Npi = TAPI_SIM_NPI_ISDN_TEL;
1860                 sca.Ton = TAPI_SIM_TON_UNKNOWN;
1861                 if (sca_num[0] == '+') {
1862                         sca.Ton = TAPI_SIM_TON_INTERNATIONAL;
1863                         additional_len = 1;
1864                 }
1865
1866                 sca_length = strlen((char *)sca_num) - additional_len;
1867                 msg("Sca Length:[%d]", sca_length);
1868
1869                 if (sca_length % 2 == 0) {
1870                         for (local_index = additional_len, j = 0; local_index < sca_length; local_index += 2, j++)
1871                                 sca.szDiallingNum[j] = ((sca_num[local_index+1] & 0X0F) << 4) | (sca_num[local_index] & 0x0F);
1872                         sca.szDiallingNum[(sca_length/2)] = '\0';
1873                         sca.DialNumLen = (sca_length/2);
1874                 } else {
1875                         for (local_index = additional_len, j = 0; local_index < sca_length+1; local_index += 2, j++) {
1876                                 if (local_index == (sca_length - 1))
1877                                         sca.szDiallingNum[j] = ((0xF0) | (sca_num[local_index] & 0x0F));
1878                                 else
1879                                         sca.szDiallingNum[j] = ((sca_num[local_index+1] & 0X0F) << 4) | (sca_num[local_index] & 0x0F);
1880                         }
1881                         sca.szDiallingNum[(sca_length/2)] = sca.szDiallingNum[(sca_length/2)] | 0xF0;
1882                         sca.DialNumLen = (sca_length/2)+1;
1883                 }
1884                 msg("%d", sca.DialNumLen);
1885                 returnStatus = tel_set_sms_sca(handle, &sca, 0, on_resp_set_sms_sca, NULL);
1886                 msg("Return status: [%d]", returnStatus);
1887         }
1888         break;
1889
1890         case 3: { /* Set Deliver Report */
1891                 msg("****Setting  Deliver Report(Api:SetDeliverReport)****");
1892         }
1893         break;
1894
1895         case 4: { /* Set CB Enable/Disable */
1896                 msg("****Setting the CB configuration(Api:SetCbConfig)****");
1897
1898                 pCBConfig = calloc(1, sizeof(TelSmsCbConfig_t));
1899                 if (!pCBConfig)
1900                         return -1;
1901
1902                 msg("Enter CB ON/OFF (1: Enable, 0:Disable): ");
1903                 pCBConfig->CBEnabled = _get_int();
1904
1905                 msg("Enter Max ID Count: ");
1906                 pCBConfig->MsgIdMaxCount = _get_int();
1907
1908                 msg("Enter MsgIdRangeCount (< 10): ");
1909                 pCBConfig->MsgIdRangeCount = _get_int();
1910
1911                 if (pCBConfig->MsgIdRangeCount <= 0
1912                                 || pCBConfig->MsgIdRangeCount >= TAPI_NETTEXT_SMS_CBMI_LIST_SIZE_MAX) {
1913                         msg("Bad Range value");
1914                         free(pCBConfig);
1915                         return -1;
1916                 }
1917
1918                 for (i = 0; i < pCBConfig->MsgIdRangeCount; i++) {
1919                         msg("Enter %d FromMsgId : ", i + 1);
1920                         pCBConfig->MsgIDs[i].Net3gpp.FromMsgId = _get_int();
1921
1922                         msg("Enter %d ToMsgId : ", i + 1);
1923                         pCBConfig->MsgIDs[i].Net3gpp.ToMsgId = _get_int();
1924
1925                         msg("Enter %d Selected : ", i + 1);
1926                         pCBConfig->MsgIDs[i].Net3gpp.Selected = _get_int();
1927                 }
1928
1929                 pCBConfig->Net3gppType = 0x01;
1930
1931                 msg("from sms test setting the cb configuration:CBEnabled:%d, Net3gppType:%d, msgIdRangeCount:%d, From: %d\t To: %d\t Selected: %d",
1932                         pCBConfig->CBEnabled, pCBConfig->Net3gppType,
1933                         pCBConfig->MsgIdRangeCount,
1934                         pCBConfig->MsgIDs[0].Net3gpp.FromMsgId,
1935                         pCBConfig->MsgIDs[0].Net3gpp.ToMsgId,
1936                         pCBConfig->MsgIDs[0].Net3gpp.Selected);
1937
1938                 returnStatus = tel_set_sms_cb_config(handle, pCBConfig,
1939                                 on_resp_set_sms_cb_config, NULL);
1940                 msg("returnStatus after cbconfig set is is [%d]", returnStatus);
1941
1942                 free(pCBConfig);
1943         }
1944         break;
1945
1946         case 5: {
1947                 unsigned int sca_length;
1948                 unsigned char sca_num[TAPI_SIM_SMSP_ADDRESS_LEN + 1];
1949                 const char *name = "AlphaID";
1950
1951                 smsParameters.RecordIndex = 0x00;
1952
1953                 /* Alpha Id */
1954                 smsParameters.AlphaIdLen = strlen(name);
1955                 memcpy(&smsParameters.szAlphaId, name, strlen(name));
1956
1957                 /* Param Indicator*/
1958                 smsParameters.ParamIndicator = 0xe1;
1959
1960                 /* Destination Number */
1961                 memset(&smsParameters.TpDestAddr, 0x0, sizeof(TelSmsAddressInfo_t));
1962
1963                 smsParameters.TpProtocolId = 3;/* PID */
1964                 smsParameters.TpDataCodingScheme = 2;/* DCS */
1965                 smsParameters.TpValidityPeriod = 1;/* VP */
1966
1967                 do {
1968                         msg("Enter the SCA NUMBER:");
1969                         ret = scanf("%s", sca_num);
1970                         if (ret > TAPI_SIM_SMSP_ADDRESS_LEN)
1971                                 msg("Entered SCA is INVALID - SCA length cannot be greater than %d", TAPI_SIM_SMSP_ADDRESS_LEN);
1972                 } while (ret > TAPI_SIM_SMSP_ADDRESS_LEN);
1973
1974                 sca_length = SmsUtilEncodeSca(&(smsParameters.TpSvcCntrAddr), sca_num);
1975                 msg("Encoded SCA Address Length[%d]", sca_length);
1976
1977                 returnStatus = tel_set_sms_parameters(handle, &smsParameters, on_resp_set_sms_params, NULL);
1978                 msg("returnstatus after sparam set  is %d", returnStatus);
1979         }
1980         break;
1981
1982         case 6: { /* Set Memory Full Notification */
1983                 msg("Enter Memory Status to be set(1:Memory Available, 2:Memory Full)");
1984                 memset(MemoryStatus, 0, sizeof(MemoryStatus));
1985                 ret = read(0, MemoryStatus, sizeof(MemoryStatus));
1986                 if (ret <= 0) {
1987                         msg(" NULL msg can NOT be sent ");
1988                         return -1;
1989                 }
1990                 msg("Memory Status type is %d ", atoi(MemoryStatus));
1991                 returnStatus = tel_set_sms_memory_status(handle,
1992                                 atoi(MemoryStatus), on_resp_set_mem_status, NULL); /* Set to full 0x02-Full, 0x01-available */
1993                 msg("api err after memstatus set is is %d", returnStatus);
1994         }
1995         break;
1996
1997         case 7: { /* Set Stored MsgStaus */
1998                 printf("Enter Message index to bet set:\n>> ");
1999                 fflush(stdout);
2000                 ret = read(0, index, sizeof(index) - 1);
2001                 if (ret < 0) {
2002                         if (errno == EINTR)
2003                                 perror("read(1)");
2004                         return -1;
2005                 } else if (ret == 0) {
2006                         printf("No index!!!\n");
2007                 }
2008
2009                 printf("Enter Message status to bet set (0:Read, 1:Unread, "
2010                         "2:Sent, 3:Unsent, 4:Delivered, 5:Delivery Unconfirmed, 6:Message Replaced):\n>> ");
2011                 fflush(stdout);
2012                 ret = read(0, msg_status, sizeof(msg_status) - 1);
2013                 if (ret < 0) {
2014                         if (errno == EINTR)
2015                                 perror("read(1)");
2016                         return -1;
2017                 } else if (ret == 0) {
2018                         printf("No msg_status!!!\n");
2019                 }
2020
2021                 msg("Message status set!!! index[%d] msg_status[%d]", atoi(index), atoi(msg_status));
2022                 tel_set_sms_message_status(handle, atoi(index), atoi(msg_status), on_resp_set_msg_status, NULL);
2023         }
2024         break;
2025
2026         case 8: {
2027                 msg("Not suppored in this Test App !!!");
2028         }
2029         break;
2030
2031         case 9: {
2032                 msg("Not suppored in this Test App !!!");
2033         }
2034         break;
2035
2036         default:
2037                 return -1;
2038         }
2039
2040         return 1;
2041 }
2042
2043 static int CdmaCbSetting(MManager *mm, struct menu_data *menu)
2044 {
2045         TapiHandle *handle = menu_manager_ref_user_data(mm);
2046         TelSmsCbConfig_t *cb_config;
2047         int i;
2048         int return_status = 0;
2049
2050         msg("****Setting the CB configuration(Api:SetCbConfig)****");
2051
2052         cb_config = calloc(1, sizeof(TelSmsCbConfig_t));
2053         if (!cb_config)
2054                 return -1;
2055
2056         msg("Enter CB ON/OFF (1: Enable, 0:Disable): ");
2057         cb_config->CBEnabled = _get_int();
2058
2059         msg("Enter Max ID Count: ");
2060         cb_config->MsgIdMaxCount = _get_int();
2061
2062         msg("Enter MsgIdRangeCount (< 10): ");
2063         cb_config->MsgIdRangeCount = _get_int();
2064
2065         if (cb_config->MsgIdRangeCount <= 0 || cb_config->MsgIdRangeCount >= TAPI_NETTEXT_SMS_CBMI_LIST_SIZE_MAX) {
2066                 msg("Bad Range value");
2067                 free(cb_config);
2068                 return -1;
2069         }
2070
2071         for (i = 0; i < cb_config->MsgIdRangeCount; i++) {
2072                 msg("Enter %d Service Category : ", i + 1);
2073                 cb_config->MsgIDs[i].Net3gpp2.CBCategory = _get_int();/*3GPP2 C.R1001-D v1.0 section 9.3 Service Category Assignments*/
2074
2075                 msg("Enter %d Language : ", i + 1);
2076                 cb_config->MsgIDs[i].Net3gpp2.CBLanguage = _get_int();
2077
2078                 msg("Enter %d Selected : ", i + 1);
2079                 cb_config->MsgIDs[i].Net3gpp2.Selected = _get_int();
2080         }
2081
2082         cb_config->Net3gppType = 0x02; /* CDMA */
2083
2084         msg("CBEnabled:%d, Net3gppType:%d, msgIdRangeCount:%d",
2085                         cb_config->CBEnabled, cb_config->Net3gppType, cb_config->MsgIdRangeCount);
2086
2087         return_status = tel_set_sms_cb_config(handle, cb_config, on_resp_set_sms_cb_config, NULL);
2088         msg("returnStatus after cbconfig set is [%d]", return_status);
2089
2090         free(cb_config);
2091         return 1;
2092 }
2093
2094 static struct menu_data menu_sms_getting[] = {
2095                 { "1", "Get Service Center Number", NULL, Getting, NULL },
2096                 { "2", "Get CB Setting Information", NULL, Getting, NULL },
2097                 { "3", "Get SMS Parameters", NULL, Getting, NULL },
2098                 { "4", "Get SMS PreferredBearer Information", NULL, Getting, NULL },
2099                 { "5", "Get SMS Parameter Count", NULL, Getting, NULL },
2100                 { "6", "Get Message Count", NULL, Getting, NULL },
2101                 { "7", "Get SMS ready status", NULL, Getting, NULL }, { NULL, NULL , }, };
2102
2103 static struct menu_data menu_cdma_sms_getting[] = {
2104                 { "1", "Get CB Setting Information", NULL, CdmaGetting, NULL },
2105                 { "2", "Get SMS PreferredBearer Information", NULL, CdmaGetting, NULL },
2106                 { "3", "Get Message Count in RUIM", NULL, CdmaGetting, NULL },
2107                 { "4", "Get SMS ready status", NULL, CdmaGetting, NULL }, { NULL, NULL , }, };
2108
2109 static struct menu_data menu_sms_setting[] = {
2110                 { "1", "Set Service Center Number", NULL, Setting, NULL},
2111                 { "2", "Set Preferred Bearer Type", NULL, Setting, NULL},
2112                 { "4", "Set CB Enable/Disable", NULL, Setting, NULL},
2113                 { "5", "Set SMS Parameters", NULL, Setting, NULL},
2114                 { "6", "Set Memory Status", NULL, Setting, NULL},
2115                 { "7", "Set Message Status", NULL, Setting, NULL}, { NULL, NULL , }, };
2116
2117 static struct menu_data menu_cdma_sms_setting[] = {
2118                 { "1", "Set PreferredBearer Type", NULL, CdmaSetting, NULL },
2119                 { "2", "Set CB Enable/Disable", NULL, CdmaSetting, NULL },
2120                 { "3", "Set Memory Status", NULL, CdmaSetting, NULL },
2121                 { "4", "Set Stored MsgStaus", NULL, CdmaSetting, NULL },
2122                 { NULL, NULL , }, };
2123
2124 static struct menu_data menu_sms_wcdma[] = {
2125                 { "1", "Send Message", NULL, SendMessage, NULL},
2126                 { "2", "Read Message", NULL, ReadMessage, NULL},
2127                 { "3", "Delete Message", NULL, DeleteMessage, NULL},
2128                 { "4", "Setting", menu_sms_setting, NULL, NULL },
2129                 { "5", "Getting SMS Information", menu_sms_getting, NULL, NULL },
2130                 { NULL, NULL , }, };
2131
2132 static struct menu_data menu_sms_cdma[] = {
2133                 { "1", "Send CDMA Message", NULL, SendMessageCDMA, NULL },
2134                 { "2", "Read CDMA Message in RUIM", NULL, ReadMessage, NULL },
2135                 { "3", "Delete CDMA Message in RUIM", NULL, DeleteMessage, NULL },
2136                 { "4", "Getting CDMA SMS Information", menu_cdma_sms_getting, NULL, NULL },
2137                 { "5", "Setting CDMA SMS Information", menu_cdma_sms_setting, NULL, NULL },
2138                 { NULL, NULL , }, };
2139
2140 struct menu_data menu_sms[] = { { "1", "3GPP[GSM/WCDMA]", menu_sms_wcdma, NULL, NULL }, {
2141                 "2", "3GPP2[CDMA/1xEV-DO]", menu_sms_cdma, NULL, NULL }, { NULL, NULL , }, };
2142
2143 void register_sms_event(TapiHandle *handle)
2144 {
2145         int ret;
2146
2147         /* SMS */
2148         ret = tel_register_noti_event(handle, TAPI_NOTI_SMS_INCOM_MSG,
2149                         on_noti_sms_incom_msg, NULL);
2150         if (ret != TAPI_API_SUCCESS)
2151                 msg("event register failed(%d)", ret);
2152
2153         ret = tel_register_noti_event(handle, TAPI_NOTI_SMS_CB_INCOM_MSG,
2154                         on_noti_sms_cb_incom_msg, NULL);
2155         if (ret != TAPI_API_SUCCESS)
2156                 msg("event register failed(%d)", ret);
2157
2158         ret = tel_register_noti_event(handle, TAPI_NOTI_SMS_ETWS_INCOM_MSG,
2159                         on_noti_sms_etws_incom_msg, NULL);
2160         if (ret != TAPI_API_SUCCESS)
2161                 msg("event register failed(%d)", ret);
2162
2163         ret = tel_register_noti_event(handle, TAPI_NOTI_SMS_MEMORY_STATUS,
2164                         on_noti_sms_memory_status, NULL);
2165         if (ret != TAPI_API_SUCCESS)
2166                 msg("event register failed(%d)", ret);
2167
2168         ret = tel_register_noti_event(handle, TAPI_NOTI_SMS_DEVICE_READY,
2169                         on_noti_sms_ready_status, NULL);
2170         if (ret != TAPI_API_SUCCESS)
2171                 msg("event register failed(%d)", ret);
2172 }