Initialize Tizen 2.3
[framework/telephony/libslp-tapi.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 <tapi_common.h>
29 #include <TelSms.h>
30 #include <TapiUtility.h>
31 #include <ITapiNetText.h>
32
33 #include "menu.h"
34 #include "sms.h"
35 #include "sms_util.h"
36
37 // Global Variabl
38 int dcs_pos = 0;
39 unsigned int SmsMsgId = 0;
40 unsigned char tp_mr = 0x00;
41
42 const char *cmdString[] = { "TAPI_SERVICE_SMS_SEND_UMTS_MSG",
43                 "TAPI_SERVICE_SMS_READ_MSG", "TAPI_SERVICE_SMS_SAVE_MSG",
44                 "TAPI_SERVICE_SMS_DELETE_MSG", "TAPI_SERVICE_SMS_GET_COUNT",
45                 "TAPI_SERVICE_SMS_GET_SCA", "TAPI_SERVICE_SMS_SET_SCA",
46                 "TAPI_SERVICE_SMS_GET_CB_CONFIG", "TAPI_SERVICE_SMS_SET_CB_CONFIG",
47                 "TAPI_SERVICE_SMS_SET_MEM_STATUS", "TAPI_SERVICE_SMS_GET_PREF_BEARER",
48                 "TAPI_SERVICE_SMS_SET_PREF_BEARER",
49                 "TAPI_SERVICE_SMS_SET_DELIVERY_REPORT",
50                 "TAPI_SERVICE_SMS_SET_MSG_STATUS", "TAPI_SERVICE_SMS_GET_PARAMS",
51                 "TAPI_SERVICE_SMS_SET_PARAMS", "TAPI_SERVICE_SMS_GET_PARAMCNT",
52                 "TAPI_SERVICE_SMS_SEND_CDMA_MSG" };
53
54 static void on_resp_send_msg (TapiHandle *handle, int result, void *data,
55                 void *user_data);
56 static void on_resp_read_msg (TapiHandle *handle, int result, void *data,
57                 void *user_data);
58 static void on_resp_save_msg (TapiHandle *handle, int result, void *data,
59                 void *user_data);
60 static void on_resp_DeliverReport_msg (TapiHandle *handle, int result,
61                 void *data, void *user_data);
62 int  _util_hexstring_to_asciistring(const unsigned char *hex_string, unsigned char *ascii_string,int hex_len);
63
64 unsigned int SmsUtilEncodeSca(TelSmsAddressInfo_t *sca, unsigned char *sca_num);
65
66 int  _util_hexstring_to_asciistring(const unsigned char *hex_string, unsigned char *ascii_string,int hex_len)
67 {
68         int i,ascii_len;
69
70         if(hex_string[hex_len-1]>>4 == 0x0f ) {
71                 ascii_len = (hex_len * 2 ) -1;
72         }else {
73                 ascii_len = (hex_len * 2 );
74         }
75
76         for (i =0; i < ascii_len; i++ ) {
77                 if (i % 2 == 0) {
78                         ascii_string[i]= (hex_string[i/2] & 0x0f)+ '0';
79                 }else{
80                         ascii_string[i]=((hex_string[i/2]   >> 4 ))+ '0';
81                 }
82         }
83         ascii_string[ascii_len]='\0';
84         return ascii_len;
85 }
86
87 unsigned int SmsUtilEncodeSca(TelSmsAddressInfo_t *sca, unsigned char *sca_num)
88 {
89         unsigned int sca_length = 0;
90         unsigned int additional_len = 0;
91         unsigned local_index, j;
92
93         sca->Npi = TAPI_SIM_NPI_ISDN_TEL;
94         sca->Ton = TAPI_SIM_TON_UNKNOWN;
95         if (sca_num[0] == '+') {
96                 sca->Ton = TAPI_SIM_TON_INTERNATIONAL;
97                 additional_len = 1;
98         }
99         sca_length = strlen((char *)sca_num) - additional_len;
100         if (sca_length % 2 == 0) {
101                 for (local_index = additional_len, j = 0; local_index < sca_length; local_index += 2, j++)
102                         sca->szDiallingNum[j] = ((sca_num[local_index+1] & 0X0F) << 4) | (sca_num[local_index] & 0x0F);
103         } else {
104                 for (local_index = additional_len, j = 0; local_index < sca_length; local_index += 2, j++) {
105                         if (local_index == (sca_length - 1))
106                                 sca->szDiallingNum[j] = ((0xF0) | (sca_num[local_index] & 0x0F));
107                         else
108                                 sca->szDiallingNum[j] = ((sca_num[local_index+1] & 0X0F) << 4) | (sca_num[local_index] & 0x0F);
109                 }
110         }
111         sca->szDiallingNum[(sca_length+1)/2] = '\0';
112         sca->DialNumLen = (sca_length+1)/2;
113
114         return sca->DialNumLen;
115 }
116
117
118 static BOOL EncodeCdmaSmsSubmitTpdu (char* diallingNum, int dialNumLen,
119                 char* msgTxt, int msg_len)
120 {
121         return TRUE;
122 }
123
124 static BOOL DecodeCellBroadcastMsg (int length, char * pPDU)
125 {
126         int msgLength;
127         int sn = 0; //serial number
128         int msgID; //message identifier
129         Sms_coding_scheme dcs;
130
131         unsigned char page[2];
132         int contents_length;
133         char contents[SMS_CB_SIZE_MAX];
134
135         unsigned char gs;
136         unsigned char msgCode;
137         unsigned char updateNumber;
138
139         if (pPDU == NULL ) {
140                 msg("pData is Null")
141                 return FALSE;
142         }
143
144         msg("cb msg type is %x", pPDU[0]);
145         msg("cb msg len is %x", pPDU[1]);
146
147         msg("$$$$$$$$$$$$$$$length of cb msg is %d", length);
148         sn = (pPDU[2] << 8) | pPDU[1];
149
150         gs = (pPDU[2] & 0xC0) >> 6;  //bit 7, bit 6
151
152         msgCode = ( ( (pPDU[2] & 0x3F) << 4) | pPDU[3]) & 0xF0;
153
154         updateNumber = pPDU[3] & 0x0F;  //bit3, bit2, bit1, bit0
155
156         msgID = pPDU[4] << 4 | pPDU[3];
157
158         SmsUtilDecodeDCS (&dcs, pPDU[6]);
159
160         page[0] = pPDU[7] & 0x0F; //total page
161         page[1] = (pPDU[7] & 0xF0) >> 4; //current page
162
163         msgLength = length - 2;
164
165         contents_length = msgLength - 6;
166
167         if (dcs.alphabet_type == SMS_ALPHABET_DEFAULT) {
168                 unsigned char inData[SMS_CB_SIZE_MAX + 1];
169
170                 memset (inData, 0x00, SMS_CB_SIZE_MAX + 1);
171
172                 memcpy ((void*) inData, (void*) &pPDU[8], contents_length);
173                 SmsUtilUnpackGSMCode (contents, &pPDU[8], msgLength);
174         }
175         else {
176                 memcpy (contents, &pPDU[8], msgLength);
177                 contents[msgLength] = 0;
178         }
179
180         printf ("*************************************\n");
181         printf ("serical number : 0x%04x \n", sn);
182         printf ("Geographical Scope: 0x%x\n", gs);
183         printf ("message code : 0x%x\n", msgCode);
184         printf ("update Number : 0x%x\n", updateNumber);
185         printf ("message identifier :  0x%x\n", msgID);
186         printf ("DCS-Commpressed?: %d (1: true)\n", dcs.bCompressed);
187         printf ("DCS-coding_group_type:  %x\n", dcs.coding_group_type);
188         printf ("contents bytes size : %d\n", contents_length);
189         printf ("page : (%d /%d) (page /total ) \n", page[1], page[0]);
190         printf ("contents: %s\n", contents);
191         printf ("*************************************\n");
192
193         return TRUE;
194 }
195
196 static BOOL EncodeSmsSubmitTpdu (MManager *mm, char* diallingNum,
197                 int dialNumLen, char* msg, int msg_len)
198 {
199         TapiHandle *handle = menu_manager_ref_user_data (mm);
200
201         TPDU_SMS_SUBMIT tpdu_submit;
202
203         unsigned char packet[TAPI_NETTEXT_MSG_SIZE_MAX];
204
205         unsigned char SCA[TAPI_NETTEXT_SCADDRESS_LEN_MAX + 1];
206         int ret = 0;
207         unsigned char tp_pid = 0; //reaplace type (65)
208         unsigned char tp_dcs; //=17; //class_1(17),class_2(18)
209         unsigned char tp_vp;
210
211         int local_index = 0, ScIndex = 0;
212         int i;
213         int requestId = 0;
214         unsigned char sca_len = 0;
215         TapiResult_t returnStatus;
216
217         TelSmsDatapackageInfo_t *pDataPackage = NULL;
218
219         tp_mr++;
220
221         memset (packet, 0, TAPI_NETTEXT_MSG_SIZE_MAX);
222
223         /************SCA ADDRESS GET START**************/
224         /*
225          returnStatus = TelTapiSmsGetSCA(&sc_addr,0);
226          printf("Service center addr returned from TAPI SCA address %s returnStatus %d\n",sc_addr.szDiallingNum,returnStatus);
227          SmsUtilDecodeAddrField(decodeScaAddr, &sc_addr.szDiallingNum[0],&sc_addr.Ton, &sc_addr.Npi);
228          sca_len = strlen( decodeScaAddr );
229
230          memcpy( SCA, decodeScaAddr, sca_len);
231          SCA[sca_len] = '\0';
232          */
233         msg("Enter SCA: ");
234         memset (SCA, 0, sizeof (SCA));
235
236         ret = read (0, SCA, sizeof (SCA) - 1);
237         if (ret <= 0) {
238                 msg(" NULL msg can NOT be sent ");
239                 return -1;
240         }
241         SCA[ret] = '\0';
242
243         pDataPackage = malloc (sizeof(TelSmsDatapackageInfo_t));
244         memset (pDataPackage, 0, sizeof(TelSmsDatapackageInfo_t));
245
246         sca_len = strlen ((char *) SCA) - 1;
247
248         msg("Decode SCA Address =========");
249         msg("SCA Number :%s, SCA Length :%d ", SCA, sca_len);
250         msg("========================");
251
252         //SCA ADDRESS GET END
253
254         //SCA
255
256         //TODO : SCA
257
258         ScIndex = SmsUtilEncodeAddrField ((unsigned char*) pDataPackage->Sca,
259                         (char *) SCA, sca_len, 0x01, 0x01);
260
261         msg("ScIndex is %d", ScIndex);
262
263         // TP-MTI, TP-RD, TP-VPF, TP-SRR, TP_UDHI, TP-RP
264         tpdu_submit.msg_type = SMS_TPDU_SUBMIT;
265         tpdu_submit.rd = FALSE; //false :accept duplicate msg , true: reject duplicate msg
266         tpdu_submit.vpf = SMS_VP_RELATIVE;
267         tpdu_submit.srr = FALSE; //false :A status report is not requested , true: A status report is requested
268         tpdu_submit.udhi = FALSE; //false: contains only the short message , true :a Header in addition to the short message
269         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
270
271         packet[local_index] = tpdu_submit.msg_type;                                     // SUBMIT: bits 0, 1
272         packet[local_index] |= tpdu_submit.rd ? 0x04 : 0;                                               // bit 2
273         packet[local_index] |= tpdu_submit.vpf << 3;    // bits 3, 4
274         packet[local_index] |= tpdu_submit.srr ? 0x20 : 0;                                              // bit 5
275         packet[local_index] |= tpdu_submit.udhi ? 0x40 : 0;                                             // bit 6
276         packet[local_index] |= tpdu_submit.rp ? 0x80 : 0;                                               // bit 7
277
278         local_index++;
279         msg("local_index of message ref is %d, msgref is %d", local_index, tp_mr);
280         /* TP-MR */
281         packet[local_index++] = tp_mr;
282
283         // TP-DA
284         local_index += SmsUtilEncodeAddrField (packet + local_index, diallingNum, dialNumLen, 0x01, 0x01);
285
286         //msg("DESTINATTION  ADDR Encoded =========");
287
288         //TP-PID
289         packet[local_index++] = tp_pid;
290
291         // TP_DCS (Data Coding Scheme)
292
293         tpdu_submit.dcs.bCompressed = FALSE;
294         tpdu_submit.dcs.bmsg_class_set = TRUE;
295         tpdu_submit.dcs.coding_group_type = SMS_CODGRP_SM_GENERAL_DCS;
296         tpdu_submit.dcs.alphabet_type = SMS_ALPHABET_DEFAULT;
297         tpdu_submit.dcs.class_type = SMS_CLASS_2;
298
299         SmsUtilEncodeDCS (&tp_dcs, &tpdu_submit.dcs);
300
301         packet[local_index++] = tp_dcs;
302
303         //msg("DCS Encoded:tp_dcs =%d =========",tp_dcs);
304
305         //TP-VP
306
307         tpdu_submit.vp.vp_rel_type = SMS_VP_REL_6H;
308         tpdu_submit.vp.vp_type = SMS_VP_RELATIVE;
309         tpdu_submit.vp.vpValue = 0xff;
310
311         SmsUtilEncodeValidity (&tp_vp, &tpdu_submit.vp);
312
313         //tp_vp =65;
314         packet[local_index++] = tp_vp;
315         //msg("Validity Encoded :tp_vp =%d =========",tp_vp);
316
317         // UDL
318         packet[local_index++] = msg_len;
319
320         // TP-UD
321
322         if (tpdu_submit.dcs.alphabet_type == SMS_ALPHABET_DEFAULT) {
323                 int pos;
324                 pos = SmsUtilPackGSMCode (packet + local_index, msg, msg_len);
325                 local_index += pos;
326         }
327
328         msg("Send packet to phonseSever[%d] -----", local_index);
329
330         for (i = 0; i < local_index; i++) {
331                 msg("[%02x]", packet[i]);
332                 if (i % 10 == 9)
333                         msg("\n");
334         }
335         msg("\n");
336
337         memcpy (pDataPackage->szData, packet, local_index);
338         pDataPackage->MsgLength = local_index;
339
340         msg("***Sending the Message (Api:SendMsg)***");
341
342         msg("Length : %d", pDataPackage->MsgLength);
343
344         returnStatus = tel_send_sms (handle, pDataPackage, 0, on_resp_send_msg,
345                         NULL );
346
347         msg("SendMsg : returnStatus=%d requestId=%d", returnStatus, requestId);
348
349         free (pDataPackage);
350
351         return TRUE;
352
353 }
354
355
356 static int DecodeSmsDeliverTpdu (int tpdu_len, char* pTPDU)
357 {
358         TPDU_SMS_DELIVER tpdu_deliver;
359         TmDateTime SCTimeStamp;
360         int orgAddr_len = 0;
361         char diallingNum[SMS_ADDRESS_LEN_MAX + 1];
362         char orgAddr[SMS_ADDRESS_LEN_MAX + 1]; //originating number
363         int org_ton, org_npi;
364         int position = 0;
365
366         /* TP-MTI, TP-MMS, TP-SRI, TP_UDHI, TP-RP */
367         tpdu_deliver.msg_type = SMS_TPDU_DELIVER;
368         tpdu_deliver.mms = (pTPDU[position] & 0x04) ? TRUE : FALSE; // bit 2 (Message Type Indicator)
369         tpdu_deliver.sri = (pTPDU[position] & 0x20) ? TRUE : FALSE;     // bit 5 (Status Report Indication)
370         tpdu_deliver.udhi = (pTPDU[position] & 0x40) ? TRUE : FALSE;// bit 6 (User Data Header Indicator)
371         tpdu_deliver.rp = (pTPDU[position] & 0x80) ? TRUE : FALSE;// bit 7 (Reply Path)
372
373         position++;
374
375         /* TP-OA */
376
377         memset (diallingNum, 0, sizeof (diallingNum));
378
379         orgAddr_len = SmsUtilDecodeAddrField (diallingNum, &pTPDU[position],
380                         &org_ton, &org_npi);
381         if (orgAddr_len > SMS_ADDRESS_LEN_MAX - 1)
382                 orgAddr_len = SMS_ADDRESS_LEN_MAX - 1;
383
384         position += 2;
385
386         if (orgAddr_len % 2)
387                 position += orgAddr_len / 2 + 1;
388         else
389                 position += orgAddr_len / 2;
390
391         msg("pos = %d, length = %d", position, orgAddr_len);
392
393         if (org_ton == SMS_TON_INTERNATIONAL) {
394                 orgAddr[0] = '+';
395                 memcpy (&orgAddr[1], diallingNum, orgAddr_len - 1);
396                 orgAddr[orgAddr_len + 1] = '\0';
397         }
398         else {
399                 memcpy (orgAddr, diallingNum, orgAddr_len);
400                 orgAddr[orgAddr_len] = '\0';
401         }
402
403         /* TP-PID */
404         tpdu_deliver.pId = pTPDU[position];
405
406         position++;
407
408         /* TP-DCS */
409         SmsUtilDecodeDCS (&tpdu_deliver.dcs, pTPDU[position]);
410         dcs_pos = position;
411         position++;
412
413         /* TP-SCTS */
414         SmsUtilDecodeTimeStamp (&pTPDU[position], &SCTimeStamp);
415
416         position += 7;
417
418         /* TP-UDL */
419         tpdu_deliver.udl = pTPDU[position];
420
421         if (tpdu_deliver.udl > SMS_SMDATA_SIZE_MAX)
422                 tpdu_deliver.udl = SMS_SMDATA_SIZE_MAX;
423
424         position++;
425
426         /* TP-UD */
427         tpdu_deliver.userData = malloc (
428                         sizeof(unsigned char) * (SMS_SMDATA_SIZE_MAX + 1));
429
430         memset (tpdu_deliver.userData, 0,
431                         sizeof(unsigned char) * (SMS_SMDATA_SIZE_MAX + 1));
432
433         if (tpdu_deliver.dcs.alphabet_type == SMS_ALPHABET_DEFAULT) {
434                 char inData[SMS_SMDATA_SIZE_MAX + 1];
435
436                 memset (inData, 0x00, SMS_SMDATA_SIZE_MAX + 1);
437
438                 memcpy ((void*) inData, (void*) &pTPDU[position], tpdu_deliver.udl);
439
440                 SmsUtilUnpackGSMCode (tpdu_deliver.userData, inData,
441                                 (unsigned char) tpdu_deliver.udl);
442         }
443
444         msg("*************************************");
445         msg("Msg Type: TPDU_DELIVER");
446
447         if (tpdu_deliver.rp) {
448                 msg("RP: Set Reply Path")
449         }
450         else if (!tpdu_deliver.rp) {
451                 msg("RP: Not set Reply Path")
452         }
453
454         if (tpdu_deliver.sri) {
455                 msg("SRI: A status report shall be returned to the SME")
456         }
457         else if (!tpdu_deliver.sri) {
458                 msg("SRI: A status report shall be not returned to the SME")
459         }
460
461         if (tpdu_deliver.mms) {
462                 msg("MMS: No more messages are waiting for the MS in this SC")
463         }
464         else if (!tpdu_deliver.mms) {
465                 msg("MMS: More messages are waiting for the MS in this SC")
466         }
467
468         if (tpdu_deliver.udhi) {
469                 msg("UDHI: Contains a Header in addition to the short message.")
470         }
471         else if (!tpdu_deliver.udhi) {
472                 msg("UDHI: Only short message.")
473         }
474
475         msg("DcsClass : %x (4 means Class None) ", tpdu_deliver.dcs.class_type);
476
477         msg("From : %10s", orgAddr);
478         msg("Time : %d-%d-%d  , %d:%d:%d", SCTimeStamp.year, SCTimeStamp.month,
479                         SCTimeStamp.day, SCTimeStamp.hour, SCTimeStamp.minute,
480                         SCTimeStamp.second);
481         msg("Message : %s ", tpdu_deliver.userData);
482         msg("*************************************");
483
484         return dcs_pos;
485 }
486
487
488
489 static int DecodeSmsStatusReportTpdu (int tpdu_len, char* pTPDU)
490 {
491         TPDU_SMS_STATUS_REPORT status_report;
492
493         TmDateTime SCTimeStamp;
494         TmDateTime DischargeTime;
495
496         int rcpAddr_len = 0;
497         char diallingNum[SMS_ADDRESS_LEN_MAX];
498
499         char rcpAddr[SMS_ADDRESS_LEN_MAX]; //recipient number
500         int rcp_ton, rcp_npi;
501         int position = 0;
502         unsigned int mask;
503
504         /* TP-MTI, TP MMS, TP SRQ, TP_UDHI */
505
506         status_report.msg_type = pTPDU[position] & 0x03; // bit 0,1
507         status_report.mms = pTPDU[position] & 0x04 ? TRUE : FALSE; // bit 2
508         status_report.srq = pTPDU[position] & 0x20 ? TRUE : FALSE; //bit 5
509         status_report.udhi = pTPDU[position] & 0x40 ? TRUE : FALSE;      //bit 6
510
511         position++;
512
513         /* TP-MR */
514         status_report.mr = pTPDU[position];
515         position++;
516
517         /* TP-RA */
518
519         memset (diallingNum, 0, sizeof (diallingNum));
520
521         SmsUtilDecodeAddrField (diallingNum, &pTPDU[position], &rcp_ton, &rcp_npi);
522
523         position += 2;
524
525         rcpAddr_len = strlen ((char*) diallingNum);
526         if (rcpAddr_len % 2)
527                 position += rcpAddr_len / 2 + 1;
528         else
529                 position += rcpAddr_len / 2;
530
531         if (rcp_ton == SMS_TON_INTERNATIONAL) {
532                 rcpAddr[0] = '+';
533                 memcpy (&rcpAddr[1], diallingNum, rcpAddr_len);
534                 rcpAddr[rcpAddr_len + 1] = '\0';
535         }
536         else {
537                 memcpy (rcpAddr, diallingNum, rcpAddr_len);
538                 rcpAddr[rcpAddr_len] = '\0';
539         }
540
541         status_report.rcpAddr.DialNumLen = rcpAddr_len;
542         memcpy (status_report.rcpAddr.szDiallingNum, rcpAddr, rcpAddr_len);
543         status_report.rcpAddr.Npi = rcp_npi;
544         status_report.rcpAddr.Ton = rcp_ton;
545
546         /* TP-SCTS */
547         SmsUtilDecodeTimeStamp (&pTPDU[position], &SCTimeStamp);
548         position += 7;
549
550         /* TP-DT */
551         SmsUtilDecodeTimeStamp (&pTPDU[position], &DischargeTime);
552         position += 7;
553
554         /* TP-ST */
555         status_report.status = pTPDU[position];
556         position++;
557
558         /* TP-PI */
559         status_report.paraInd = pTPDU[position];
560
561         mask = status_report.paraInd;
562
563         if ( (mask != 0xFF) && (mask != 0)) {
564                 /* Increment only if mask is valid */
565                 position++;
566
567                 /* TP-PID */
568                 if (mask & 0x01) {
569                         status_report.pId = pTPDU[position];
570                         position++;
571                 }
572                 /* TP-DCS */
573                 if (mask & 0x02) {
574                         SmsUtilDecodeDCS (&status_report.dcs, pTPDU[position]);
575                         position++;
576                 }
577                 if (mask & 0x04) {
578                         /* TP-UDL */
579                         status_report.udl = pTPDU[position];
580                         position++;
581
582                         /* TP-UD */
583
584                         status_report.userData = malloc (
585                                         sizeof(unsigned char) * (SMS_SMDATA_SIZE_MAX + 1));
586
587                         memset (status_report.userData, 0,
588                                         sizeof(unsigned char) * (SMS_SMDATA_SIZE_MAX + 1));
589
590                         if (status_report.dcs.alphabet_type == SMS_ALPHABET_DEFAULT) {
591                                 char inData[SMS_SMDATA_SIZE_MAX + 1];
592
593                                 memset (inData, 0x00, SMS_SMDATA_SIZE_MAX + 1);
594
595                                 memcpy ((void*) inData, (void*) &pTPDU[position],
596                                                 status_report.udl);
597
598                                 SmsUtilUnpackGSMCode (status_report.userData, inData,
599                                                 (unsigned char) status_report.udl);
600                         }
601
602                 }
603         }
604
605         msg("*************************************");
606         msg("Msg Type: STATUS_REPORT");
607         if (status_report.mms) {
608                 msg("MMS: No more messages are waiting for the MS in this SC")
609         }
610         else if (!status_report.mms) {
611                 msg("MMS: More messages are waiting for the MS in this SC")
612         }
613
614         if (status_report.srq) {
615                 msg("SRQ: the result of an SMS COMMAND ")
616         }
617         else if (!status_report.srq) {
618                 msg("SRQ: the result of a SMS SUBMIT.")
619         }
620
621         if (status_report.udhi) {
622                 msg("UDHI: Contains a Header in addition to the short message.")
623         }
624         else if (!status_report.udhi) {
625                 msg("UDHI: Only Short Message")
626         }
627
628         msg("STATUS:%x", status_report.status);
629         if (status_report.status == 0x00) {
630                 msg("STATUS : Short message received by the SME")
631         }
632         else if (status_report.status == 0x01) {
633                 msg(
634                                 "STATUS : Short message forwarded by the SC to the SMEbut the SC is unable to confirm delivery")
635         }
636         else if (status_report.status == 0x02) {
637                 msg("STATUS : Short message replaced by the SC")
638         }
639
640         msg("Recipient Number : %s", rcpAddr);
641         msg("SC Time Stamp : %d-%d-%d  , %d:%d:%d", SCTimeStamp.year,
642                         SCTimeStamp.month, SCTimeStamp.day, SCTimeStamp.hour,
643                         SCTimeStamp.minute, SCTimeStamp.second);
644         msg("Discharge Time : %d-%d-%d  , %d:%d:%d", DischargeTime.year,
645                         DischargeTime.month, DischargeTime.day, DischargeTime.hour,
646                         DischargeTime.minute, DischargeTime.second);
647
648         if (mask & 0x04)
649                 msg("Message : %s ", status_report.userData);
650
651         msg("*************************************");
652
653         return 1;
654 }
655
656
657 static void on_noti_sms_incom_msg (TapiHandle *handle, const char *noti_id,
658                 void *data, void *user_data)
659 {
660         TelSmsDatapackageInfo_t *datapackage = data;
661
662         int scaAddr_len = 0;
663         char * pTPDU;
664         int tpdu_len = 0;
665         char diallingNum[TAPI_NETTEXT_ADDRESS_LEN_MAX + 1] = { 0, };
666         char scaAddr[TAPI_NETTEXT_SCADDRESS_LEN_MAX + 2] = { 0, }; //service center address
667         int sca_ton, sca_npi;
668         int position = 0;
669         int sca_length;
670         int offset = 0;
671
672         unsigned char MTI = 0;
673         unsigned char MMS = 0;
674         unsigned char SRI = 0;
675         unsigned char UDHI = 0;
676         unsigned char RP = 0;
677
678         Sms_coding_scheme dcs;
679         TapiResult_t returnStatus;
680         TelSmsData_t WriteData;
681         TelSmsDatapackageInfo_t * del_report = NULL;
682
683         if (datapackage == NULL) {
684                 return;
685         }
686
687         msg("");
688         msgb("event(%s) receive !!", TAPI_NOTI_SMS_INCOM_MSG);
689
690         memset (diallingNum, 0, sizeof (diallingNum));
691
692         sca_length = datapackage->Sca[0];
693
694         sca_ton = (datapackage->Sca[offset +1] & 0x70) >> 4;
695         sca_npi = datapackage->Sca[offset +1] & 0x0F;
696
697         msg("TON %d", sca_ton);
698         msg("NPI %d", sca_npi);
699
700         /* Modem Send the hex encoded SCA Service center digits*/
701         scaAddr_len = _util_hexstring_to_asciistring(&datapackage->Sca[2], (unsigned char *) diallingNum, sca_length-1);
702
703         msg("SCA ascii length%d", scaAddr_len);
704         msg("SCA Number:[%s]", diallingNum);
705
706         if (scaAddr_len > TAPI_NETTEXT_SCADDRESS_LEN_MAX)
707                 scaAddr_len = TAPI_NETTEXT_SCADDRESS_LEN_MAX;
708
709         if (sca_ton == SMS_TON_INTERNATIONAL) {
710                 scaAddr[0] = '+';
711                 memcpy (&scaAddr[1], diallingNum, scaAddr_len - 1);
712         }
713         else {
714                 memcpy (scaAddr, diallingNum, scaAddr_len);
715         }
716
717         msg("Sc address in test app is %s", scaAddr);
718
719         RP = datapackage->szData[position] & 0x80;
720         UDHI = datapackage->szData[position] & 0x40;
721         SRI = datapackage->szData[position] & 0x20;
722         MMS = datapackage->szData[position] & 0x04;
723         MTI = datapackage->szData[position] & 0x03;
724
725         msg("RP [%x]", RP);
726         msg("UDHI [%x]", UDHI);
727         msg("SRI [%x]", SRI);
728         msg("MMS [%x]", MMS);
729         msg("MTI [%02x]", MTI);
730
731         tpdu_len = datapackage->MsgLength;
732
733         pTPDU = malloc (sizeof(unsigned char) * tpdu_len);
734
735         memcpy (pTPDU, &datapackage->szData[position], tpdu_len);
736
737         if (MTI == SMS_TPDU_DELIVER) {
738                 DecodeSmsDeliverTpdu (tpdu_len, pTPDU);
739
740                 msg("dcs_pos : %d", dcs_pos);
741
742                 SmsUtilDecodeDCS (&dcs, datapackage->szData[position + dcs_pos]);
743
744                 if (dcs.class_type == SMS_CLASS_2) {
745                         msg("dcs type is of class2");
746
747                         memset (&WriteData, 0, sizeof(TelSmsData_t));
748
749                         memcpy (WriteData.SmsData.Sca, datapackage->Sca,
750                                         TAPI_SIM_SMSP_ADDRESS_LEN);
751
752                         WriteData.SmsData.MsgLength = datapackage->MsgLength;
753
754                         memcpy (WriteData.SmsData.szData, datapackage->szData,
755                                         datapackage->MsgLength);
756
757                         WriteData.MsgStatus = TAPI_NETTEXT_STATUS_READ;
758                         WriteData.SmsData.format = TAPI_NETTEXT_NETTYPE_3GPP;
759
760                         returnStatus = tel_write_sms_in_sim (handle, &WriteData,
761                                         on_resp_save_msg, NULL );
762
763                         msg("returnStatus for tel_write_sms_in_sim(): 0x%x", returnStatus);
764                 }
765
766         }
767         else if (MTI == SMS_TPDU_STATUS_REPORT) {
768                 DecodeSmsStatusReportTpdu (tpdu_len, pTPDU);
769         }
770         del_report = malloc (sizeof(TelSmsDatapackageInfo_t));
771
772         memset (del_report, 0, sizeof(TelSmsDatapackageInfo_t));
773
774         memcpy (del_report->Sca, datapackage->Sca, TAPI_SIM_SMSP_ADDRESS_LEN);
775
776         del_report->szData[0] = SMS_TPDU_DELIVER_REPORT;        //TP-UDHI[bit6] : 0
777         del_report->szData[1] = 0;// TP-PI = 0; bit2: TP-UDL bit1: TP-DCS bit0: TP-PID (No option field)
778         del_report->MsgLength = 2;
779         del_report->format = TAPI_NETTEXT_NETTYPE_3GPP;
780
781         msg("***receive message (Api:SendDeliverreport)****");
782
783         returnStatus = tel_send_sms_deliver_report (handle, del_report,
784                         TAPI_NETTEXT_SENDSMS_SUCCESS, on_resp_DeliverReport_msg, NULL );
785
786         msg("returnStatus for tel_send_sms_deliver_report(): 0x%x", returnStatus);
787
788         free (del_report);
789
790         free (pTPDU);
791
792 }
793
794 static void on_noti_sms_cb_incom_msg (TapiHandle *handle, const char *noti_id,
795                 void *data, void *user_data)
796 {
797         TelSmsCbMsg_t *cbMsg = data;
798         int length = 0;
799         char pPDU[SMS_CB_SIZE_MAX + 3] = { 0, };
800
801         msg("");
802         msgb("event(%s) receive !!", TAPI_NOTI_SMS_CB_INCOM_MSG);
803
804         length = cbMsg->Length;
805         pPDU[0] = cbMsg->CbMsgType;
806         pPDU[1] = length;
807         memcpy (& (pPDU[2]), cbMsg->szMsgData, SMS_CB_SIZE_MAX + 1);
808
809         DecodeCellBroadcastMsg (length, pPDU);
810 }
811
812 static void on_noti_sms_etws_incom_msg (TapiHandle *handle, const char *noti_id,
813                 void *data, void *user_data)
814 {
815         TelSmsEtwsMsg_t *etwsMsg = data;
816         int length = 0;
817         char pPDU[TAPI_NETTEXT_ETWS_SIZE_MAX + 3] = { 0, };
818
819         msg("");
820         msgb("event(%s) receive !!", TAPI_NOTI_SMS_ETWS_INCOM_MSG);
821
822         length = etwsMsg->Length;
823         pPDU[0] = etwsMsg->EtwsMsgType;
824         pPDU[1] = length;
825         memcpy (& (pPDU[2]), etwsMsg->szMsgData, TAPI_NETTEXT_ETWS_SIZE_MAX + 1);
826 }
827
828
829
830 static void on_noti_sms_memory_status (TapiHandle *handle, const char *noti_id,
831                 void *data, void *user_data)
832 {
833         int *memory_status = data;
834
835         msg("");
836         msgb("event(%s) receive !!", TAPI_NOTI_SMS_MEMORY_STATUS);
837         msg(" - memory_status = 0x%x", *memory_status);
838 }
839
840 static void on_noti_sms_ready_status (TapiHandle *handle, const char *noti_id,
841                 void *data, void *user_data)
842 {
843         gboolean *device_ready = data;
844
845         msg("");
846         msgb("event(%s) receive !!", TAPI_NOTI_SMS_DEVICE_READY);
847         msg(" - device_ready = %s", *device_ready ? "TRUE" : "FALSE");
848 }
849
850 static void on_resp_send_msg (TapiHandle *handle, int result, void *data,
851                 void *user_data)
852 {
853         msg("");
854         msgb("tel_send_sms() response receive");
855         msg(" - result = 0x%x", result);
856 }
857
858 static void on_resp_read_msg (TapiHandle *handle, int result, void *data,
859                 void *user_data)
860 {
861         TelSmsData_t * sim_data;
862         int scaAddr_len = 0;
863         char * pTPDU;
864         int tpdu_len = 0;
865         char diallingNum[TAPI_NETTEXT_ADDRESS_LEN_MAX + 1] = { 0, };
866         char scaAddr[TAPI_NETTEXT_SCADDRESS_LEN_MAX + 2] = { 0, }; //service center address
867         int sca_ton, sca_npi;
868         int position;
869
870         if (data == NULL ) {
871                 msg("data is Null");
872                 return;
873         }
874
875         sim_data = (TelSmsData_t *) data;
876
877         if (sim_data->MsgStatus == TAPI_NETTEXT_STATUS_UNREAD)
878                 msg("Msg Staus : received unread msg")
879         else if (sim_data->MsgStatus == TAPI_NETTEXT_STATUS_READ)
880                 msg("Msg Staus : received read msg")
881         else if (sim_data->MsgStatus == TAPI_NETTEXT_STATUS_UNSENT)
882                 msg("Msg Staus : unsent msg")
883         else
884                 msg("Msg Staus : [%d]", sim_data->MsgStatus)
885
886         msg("First Data [%x]", sim_data->SmsData.szData[0]);
887         msg("Second Data [%x]", sim_data->SmsData.szData[1]);
888         msg("Third Data [%x]", sim_data->SmsData.szData[2]);
889         msg("Fourth Data [%x]", sim_data->SmsData.szData[3]);
890
891         position = 0;
892         // SCA_ADDR
893         memset (diallingNum, 0, sizeof (diallingNum));
894
895         SmsUtilDecodeAddrField (diallingNum, (char *) sim_data->SmsData.Sca,
896                         &sca_ton, &sca_npi);
897
898         position += 2;  //include Address-Length, Type of Address
899
900         scaAddr_len = strlen ((char *) diallingNum);
901         if (scaAddr_len > TAPI_NETTEXT_SCADDRESS_LEN_MAX - 1)
902                 scaAddr_len = TAPI_NETTEXT_SCADDRESS_LEN_MAX - 1;
903
904         if (scaAddr_len % 2)
905                 position += scaAddr_len / 2 + 1;
906         else
907                 position += scaAddr_len / 2;
908
909         if (sca_ton == SMS_TON_INTERNATIONAL) {
910                 scaAddr[0] = '+';
911                 memcpy (&scaAddr[1], diallingNum, scaAddr_len);
912         }
913         else {
914                 memcpy (scaAddr, diallingNum, scaAddr_len);
915         }
916
917         tpdu_len = sim_data->SmsData.MsgLength;
918
919         msg("SCA Number : %s tpdu_len is %d", scaAddr, tpdu_len);
920
921         pTPDU = malloc (sizeof(unsigned char) * tpdu_len);
922         if (!pTPDU)
923                 return;
924
925         msg("bfor memcopy position is %d", position);
926         memcpy (pTPDU, & (sim_data->SmsData.szData[0]), tpdu_len);
927         msg("after memcpy");
928
929         DecodeSmsDeliverTpdu (tpdu_len, pTPDU);
930
931         free (pTPDU);
932 }
933
934 static void on_resp_save_msg (TapiHandle *handle, int result, void *data,
935                 void *user_data)
936 {
937         int *local_index = data;
938
939         msg("");
940         msgb("tel_write_sms_in_sim() response receive");
941         msg(" - result = 0x%x", result);
942         msg(" - local_index = %d", *local_index);
943 }
944
945 static void on_resp_delete_msg (TapiHandle *handle, int result, void *data,
946                 void *user_data)
947 {
948         int *local_index = data;
949
950         msg("");
951         msgb("tel_delete_sms_in_sim() response receive");
952         msg(" - result = 0x%x", result);
953         msg(" - local_index = %d", *local_index);
954 }
955
956 static void on_resp_DeliverReport_msg (TapiHandle *handle, int result,
957                 void *data, void *user_data)
958 {
959         int *local_index = data;
960
961         msg("");
962         msgb("tel_send_sms_deliver_report() response receive");
963         msg(" - result = 0x%x", result);
964         msg(" - local_index = %d", *local_index);
965 }
966
967 static void on_resp_set_sms_sca (TapiHandle *handle, int result, void *data,
968                 void *user_data)
969 {
970         msg("");
971         msgb("tel_set_sms_sca() response receive");
972         msg(" - result = 0x%x", result);
973 }
974
975 static void on_resp_set_sms_cb_config (TapiHandle *handle, int result,
976                 void *data, void *user_data)
977 {
978         msg("");
979         msgb("tel_set_sms_cb_config() response receive");
980         msg(" - result = 0x%x", result);
981 }
982
983 static void on_resp_set_sms_params (TapiHandle *handle, int result, void *data,
984                 void *user_data)
985 {
986         msg("");
987         msgb("tel_set_sms_params() response receive");
988         msg(" - result = 0x%x", result);
989 }
990
991 static void on_resp_set_mem_status (TapiHandle *handle, int result, void *data,
992                 void *user_data)
993 {
994         msg("");
995         msgb("tel_set_sms_mem_status() response receive");
996         msg(" - result = 0x%x", result);
997 }
998
999 static void on_resp_get_sms_sca (TapiHandle *handle, int result, void *data,
1000                 void *user_data)
1001 {
1002         TelSmsAddressInfo_t *scaInfo = data;
1003         unsigned int i = 0;
1004
1005         msg("");
1006         msgb("tel_get_sms_sca() response receive");
1007         msg(" - result = 0x%x", result);
1008         msg(" - TON = %d", scaInfo->Ton);
1009         msg(" - NPI = %d", scaInfo->Npi);
1010         msg(" - DialNumLen = %d", scaInfo->DialNumLen);
1011         msg(" - SCA Num");
1012
1013         for (i = 0; i < scaInfo->DialNumLen; i++) {
1014                 msg("[%02x]", scaInfo->szDiallingNum[i]);
1015                 if (i % 10 == 9)
1016                         msg("\n");
1017         }
1018 }
1019
1020 static void on_resp_get_cb_config (TapiHandle *handle, int result, void *data,
1021                 void *user_data)
1022 {
1023         TelSmsCbConfig_t * CBConfig;
1024         int i = 0;
1025
1026         if (data == NULL ) {
1027                 msg("pData is Null")
1028                 return;
1029         }
1030
1031         CBConfig = (TelSmsCbConfig_t *) data;
1032
1033         msg("");
1034         msgb("tel_get_sms_cb_config() response receive");
1035         msg(" - result = 0x%x", result);
1036
1037         msg("=========CB Configuration=========");
1038
1039         /*** CB Enable/Diable ***/
1040         if (CBConfig->CBEnabled == TRUE)
1041                 msg("Cell Broadcast Msg Enabled...")
1042         else
1043                 msg("Cell Broadcast Msg Disabled...")
1044
1045         /*** Selected ID ***/
1046         if (CBConfig->Net3gppType == 0x01)
1047                 msg("Network type is 3gpp ")
1048         else if (CBConfig->Net3gppType == 0x02)
1049                 msg("Network type is CDMA");
1050
1051         /*** CBMI  Count ***/
1052         msg("CBMI Range Count: %d \n", CBConfig->MsgIdRangeCount);
1053
1054         /*** CBMI  List ***/
1055         if (CBConfig->MsgIdRangeCount != 0) {
1056                 msg("----- CBMI List -----");
1057                 for (i = 0; i < CBConfig->MsgIdRangeCount; i++) {
1058                         msg("From No.%d - [0x%04x]", i,
1059                                         CBConfig->MsgIDs[i].Net3gpp.FromMsgId);
1060                         msg("To No.%d - [0x%04x]", i, CBConfig->MsgIDs[i].Net3gpp.ToMsgId);
1061                 }
1062         }
1063         msg("==================================");
1064 }
1065
1066 static void on_resp_get_sms_parameters (TapiHandle *handle, int result,
1067                 void *data, void *user_data)
1068 {
1069         TelSmsParams_t *smsp_param;
1070         int i = 0;
1071
1072         if (data == NULL ) {
1073                 msg("data is Null")
1074                 return;
1075         }
1076         smsp_param = (TelSmsParams_t *) data;
1077
1078         msg("");
1079         msgb("tel_get_sms_parameters() response receive");
1080         msg(" - result = 0x%x", result);
1081
1082         msg("record index is 0x%x", smsp_param->RecordIndex);
1083         msg("record len  is 0x%x", smsp_param->RecordLen);
1084         msg("alpha_id len  is 0x%x ", (int )smsp_param->AlphaIdLen);
1085         msg("alpha_id is %s ", smsp_param->szAlphaId);
1086         msg("param indicator is  0x%x", smsp_param->ParamIndicator);
1087
1088         for (i = 0; i < (int) smsp_param->TpDestAddr.DialNumLen; i++)
1089                 msg("DestAddr = %d [%02x]", i, smsp_param->TpDestAddr.szDiallingNum[i]);
1090
1091         for (i = 0; i < (int) smsp_param->TpSvcCntrAddr.DialNumLen; i++)
1092                 msg("SCAddr = %d [%02x]", i, smsp_param->TpSvcCntrAddr.szDiallingNum[i]);
1093
1094         msg("pid 0x%x", smsp_param->TpProtocolId);
1095         msg("dcs is 0x%x", smsp_param->TpDataCodingScheme);
1096         msg("validity is 0x%x", smsp_param->TpValidityPeriod);
1097 }
1098
1099 static void on_resp_get_paramcnt (TapiHandle *handle, int result, void *data,
1100                 void *user_data)
1101 {
1102         int * RecordCount;
1103
1104         if (data == NULL ) {
1105                 msg("data is Null")
1106                 return;
1107         }
1108
1109         RecordCount = (int *) data;
1110
1111         msg("");
1112         msgb("tel_get_sms_parameter_count() response receive");
1113         msg(" - result = 0x%x", result);
1114         msg(" - In param_count_noti the record count is %d", *RecordCount);
1115 }
1116
1117 static void on_resp_get_sms_count (TapiHandle *handle, int result, void *data,
1118                 void *user_data)
1119 {
1120         static TelSmsStoredMsgCountInfo_t *countInfo;
1121         int loop_counter = 0;
1122         TapiResult_t returnStatus = TAPI_API_SUCCESS;
1123
1124         if (data == NULL ) {
1125                 msg("data is Null")
1126                 return;
1127         }
1128
1129         countInfo = (TelSmsStoredMsgCountInfo_t*) data;
1130
1131         msg("");
1132         msgb("tel_get_sms_count() response receive");
1133         msg(" - result = 0x%x", result);
1134
1135         if (countInfo->UsedCount != 0x00)       //if used count is not zero
1136                         {
1137                 msg("Index LIST..........");
1138                 for (loop_counter = 0; loop_counter < countInfo->UsedCount;
1139                                 loop_counter++) {
1140                         msg("[%02x]", countInfo->IndexList[loop_counter]);
1141                         returnStatus = tel_read_sms_in_sim (handle,
1142                                         countInfo->IndexList[loop_counter], on_resp_read_msg,
1143                                         NULL );
1144                         msg("After read msg: returnstatus %d", returnStatus);
1145                 }
1146                 msg("In MsgCountNotification total cnt is %d, usedcnt is %dapi_err %d",
1147                                 countInfo->TotalCount, countInfo->UsedCount, returnStatus);
1148         }
1149
1150         msg("In MsgCountNotification total cnt is %d, usedcnt is %d",
1151                         countInfo->TotalCount, countInfo->UsedCount);
1152
1153 }       //Madhavi
1154
1155 static int SendMessage (MManager *mm, struct menu_data *menu)
1156 {
1157         int ret;
1158         int msg_len = 0;
1159         char buf[SMS_ADDRESS_LEN_MAX];
1160         char message[512];
1161         char diallingNum[SMS_ADDRESS_LEN_MAX];
1162         int diallingNum_len = 0;
1163
1164         memset (buf, 0, sizeof (buf));
1165         memset (diallingNum, 0, sizeof (diallingNum));
1166         diallingNum_len = 0;
1167
1168         msg("Enter destination Number: ");
1169
1170         ret = read (0, buf, sizeof (buf));
1171         if (ret < 0) {
1172                 if (errno == EINTR)
1173                         perror ("read(1)");
1174                 return -1;
1175         }
1176         else if (ret == 0)
1177                 return ret;
1178
1179         buf[SMS_ADDRESS_LEN_MAX - 1] = '\0';
1180         diallingNum_len = strlen (diallingNum);
1181         memcpy (&diallingNum[diallingNum_len], buf, strlen (buf));
1182
1183         diallingNum_len = strlen (diallingNum); //recalculate
1184         msg("dialling num %s and dialling num len is %d", diallingNum,
1185                         diallingNum_len);
1186         diallingNum[diallingNum_len] = 0;
1187         diallingNum_len = diallingNum_len - 1;
1188
1189         msg("Enter Message: ");
1190         memset (message, 0, sizeof (message));
1191
1192         ret = read (0, message, sizeof (message) - 1);
1193
1194         if (ret <= 0) {
1195                 msg(" NULL msg can NOT be sent ");
1196                 return -1;
1197         }
1198         message[sizeof (message) - 1] = '\0';
1199
1200         msg_len = strlen (message);
1201         message[--msg_len] = 0;
1202
1203         msg("==========================");
1204         msg("To :%s", diallingNum);
1205         msg("Message: %sMsg Length:%d", message, msg_len);
1206         msg("Dialling number Length : %d", diallingNum_len);
1207         msg("==========================\n");
1208
1209         EncodeSmsSubmitTpdu (mm, diallingNum, diallingNum_len, message, msg_len);
1210         return 1;
1211
1212 }
1213
1214 static int SendMessageCDMA ()
1215 {
1216         int ret;
1217         int msg_len = 0;
1218         char buf[512] = { 0, };
1219         char message[512] = { 0, };
1220         char diallingNum[SMS_ADDRESS_LEN_MAX + 1] = { 0, };
1221         int diallingNum_len = 0;
1222
1223         printf ("\n");
1224         printf ("Enter destination Number:\n>> ");
1225         fflush (stdout);
1226
1227         ret = read (0, buf, sizeof (buf));
1228         if (ret < 0) {
1229                 if (errno == EINTR)
1230                         perror ("read(1)");
1231                 return -1;
1232         }
1233         else if (ret == 0)
1234                 return ret;
1235
1236         buf[SMS_ADDRESS_LEN_MAX - 1] = '\0';
1237         memcpy (diallingNum, buf, sizeof (diallingNum));
1238         diallingNum_len = strlen (diallingNum);
1239
1240         printf ("dialling num [%s], dialling num len [%d]\n", diallingNum,
1241                         diallingNum_len);
1242         printf ("Enter Message\n>> ");
1243         fflush (stdout);
1244
1245         ret = read (0, message, sizeof (message) - 1);
1246         if (ret <= 0) {
1247                 printf (" NULL msg can NOT be sent \n");
1248                 return -1;
1249         }
1250         message[sizeof (message) - 1] = '\0';
1251
1252         msg_len = strlen (message);
1253
1254         printf ("===========================\n");
1255         printf ("To: [%s] (len: %d)\n", diallingNum, diallingNum_len);
1256         printf ("Message: [%s]\nMsg Length: [%d]\n", message, msg_len);
1257         printf ("===========================\n\n");
1258
1259         EncodeCdmaSmsSubmitTpdu (diallingNum, diallingNum_len, message, msg_len);
1260         //EncodeSmsSubmitTpdu(diallingNum, diallingNum_len,message, msg_len) ;
1261
1262         return 1;
1263 }
1264
1265 static int ReadMessage (MManager *mm, struct menu_data *menu)
1266 {
1267         TapiHandle *handle = menu_manager_ref_user_data (mm);
1268         int selectedNo;
1269         int ret;
1270         char buf[100];
1271         int returnStatus = 0;
1272
1273         memset (buf, 0, sizeof (buf));
1274
1275         msg("*************************************");
1276         msg("1. SIM Message");
1277
1278         msg("Select Number:");
1279
1280         ret = read (0, buf, sizeof (buf));
1281
1282         if (ret < 0) {
1283                 if (errno == EINTR)
1284                         perror ("read(1)");
1285                 return -1;
1286         }
1287         else if (ret == 0)
1288                 return ret;
1289
1290         selectedNo = atoi (buf);
1291         msg("Selected Num in read message(value is 1) is %d ", selectedNo);
1292         switch (selectedNo) {
1293                 case 1:
1294
1295                         msg("***Reading the message in SIM***")
1296                         ;
1297                         returnStatus = tel_get_sms_count (handle, on_resp_get_sms_count,
1298                                         NULL );
1299                         msg("tel_get_sms_count():  0x%x", returnStatus)
1300                         ;
1301
1302                         break;
1303                 default:
1304                         msg("Not supported Menu(%d) !!!", selectedNo)
1305                         ;
1306                         break;
1307         }
1308
1309         return 1;
1310 }
1311
1312 static int DeleteMessage (MManager *mm, struct menu_data *menu)
1313 {
1314         TapiHandle *handle = menu_manager_ref_user_data (mm);
1315
1316         int ret;
1317         int local_index;
1318         char buf[100];
1319
1320         TapiResult_t returnStatus;
1321
1322         memset (buf, 0, sizeof (buf));
1323
1324         msg("Enter delete index >>");
1325
1326         ret = read (0, buf, sizeof (buf));
1327         if (ret < 0) {
1328                 if (errno == EINTR)
1329                         perror ("read(1)");
1330                 return -1;
1331         }
1332         else if (ret == 0)
1333                 return ret;
1334
1335         local_index = atoi (buf);
1336         msg("index value :%d", local_index);
1337
1338         msg("***Deleting the message(Api:DeleteMsg,GetMsgCount)***");
1339
1340         returnStatus = tel_delete_sms_in_sim (handle, local_index, on_resp_delete_msg,
1341                         NULL );
1342         msg("returnstatus  tel_delete_sms_in_sim()  is for delete%d ", returnStatus);
1343         returnStatus = tel_get_sms_count (handle, on_resp_get_sms_count, NULL );
1344         msg("returnStatus for tel_get_sms_count()  %d", returnStatus);
1345
1346         return 1;
1347
1348 }
1349
1350 static int Getting (MManager *mm, struct menu_data *menu)
1351 {
1352         TapiHandle *handle = menu_manager_ref_user_data (mm);
1353         int selectedNo;
1354         int returnStatus = 0;
1355         gboolean bReadyStatus = FALSE;
1356
1357         selectedNo = atoi (menu->key);
1358
1359         switch (selectedNo) {
1360                 case 1:
1361                         msg("***Getting the SCA(Api: GetSCA)****")
1362                         ;
1363                         returnStatus = tel_get_sms_sca (handle, 0, on_resp_get_sms_sca,
1364                                         NULL );
1365                         msg("returnstatus for tel_get_sms_sca(): 0x %x", returnStatus)
1366                         ;
1367
1368                         break;
1369
1370                 case 2:
1371                         msg("***Getting the CB Configuration(Api: GetCBConfig)***")
1372                         ;
1373                         returnStatus = tel_get_sms_cb_config (handle, on_resp_get_cb_config,
1374                                         NULL );
1375                         msg("ReturnStatus[%d] ", returnStatus)
1376                         ;
1377
1378                         break;
1379
1380                 case 3:
1381                         msg("****Getting the parameter(Api:GetParameter)****")
1382                         ;
1383                         returnStatus = tel_get_sms_parameters (handle, 0,
1384                                         on_resp_get_sms_parameters, NULL );
1385                         msg("ReturnStatus[%d]]", returnStatus)
1386                         ;
1387
1388                         break;
1389
1390                 case 5:
1391                         msg("***Getting the parameterCount(Api:GetParameterCount)***")
1392                         ;
1393                         returnStatus = tel_get_sms_parameter_count (handle,
1394                                         on_resp_get_paramcnt, NULL );
1395                         msg("ReturnStatus[%d]", returnStatus)
1396                         ;
1397                         break;
1398
1399                 case 6:
1400                         msg("***Getting the SmsCount(Api:GetSmsCount)***")
1401                         ;
1402                         returnStatus = tel_get_sms_count (handle, on_resp_get_sms_count,
1403                                         NULL );
1404                         msg("ReturnStatus[%d]", returnStatus)
1405                         ;
1406
1407                         break;
1408
1409                 case 7:
1410                         msg(
1411                                         "***Getting the Sms Ready status(Api:Check_sms_device_status)***")
1412                         ;
1413                         returnStatus = tel_check_sms_device_status (handle, &bReadyStatus);
1414                         msg("ReturnStatus[%d]", returnStatus)
1415                         ;
1416                         msg("Ready status = %s", bReadyStatus ? "TRUE" : "FALSE")
1417                         ;
1418
1419                         break;
1420
1421                 default:
1422                         return -1;
1423
1424         }
1425
1426         return 1;
1427 }
1428
1429 static int _get_int()
1430 {
1431         char buf[255];
1432         int ret;
1433
1434         memset (buf, 0, 255);
1435         ret = read (0, buf, 254);
1436         if (ret <= 0)
1437                 return 0;
1438
1439         return atoi(buf);
1440 }
1441
1442 static int Setting (MManager *mm, struct menu_data *menu)
1443 {
1444         TapiHandle *handle = menu_manager_ref_user_data (mm);
1445
1446         int settingMenu;
1447         int ret;
1448         int i;
1449
1450         char MemoryStatus[255] = { 0, }; //2006/8/8
1451
1452         TelSmsCbConfig_t *pCBConfig;
1453
1454         TelSmsParams_t smsParameters = { 0, };
1455         TapiResult_t returnStatus;
1456
1457         settingMenu = atoi (menu->key);
1458
1459         switch (settingMenu) {
1460                 case 1:  //Set Service Center Number
1461                 {
1462                         TelSmsAddressInfo_t sca = {0, };
1463                         unsigned char sca_num[TAPI_SIM_SMSP_ADDRESS_LEN + 1];
1464                         unsigned int sca_length = 0;
1465                         unsigned int additional_len = 0;
1466                         unsigned local_index, j;
1467
1468                         msg("*** Setting SCA (API: tel_set_sms_sca()) ****");
1469
1470                         RETRY:
1471                         msg("Enter the SCA NUMBER:");
1472                         ret = scanf("%s", sca_num);
1473                         if (ret > TAPI_SIM_SMSP_ADDRESS_LEN) {
1474                                 msg("Entered SCA is INVALID - SCA length cannot be greater than %d", TAPI_SIM_SMSP_ADDRESS_LEN);
1475                                 goto RETRY;
1476                         }
1477                         sca.Npi = TAPI_SIM_NPI_ISDN_TEL;
1478                         sca.Ton = TAPI_SIM_TON_UNKNOWN;
1479                         if (sca_num[0] == '+') {
1480                                 sca.Ton = TAPI_SIM_TON_INTERNATIONAL;
1481                                 additional_len = 1;
1482                         }
1483                         sca_length = strlen((char *)sca_num) - additional_len;
1484                         msg("Sca Length:[%d]", sca_length);
1485
1486                         if (sca_length % 2 == 0) {
1487                                 for (local_index = additional_len, j = 0; local_index < sca_length; local_index += 2, j++)
1488                                         sca.szDiallingNum[j] = ((sca_num[local_index+1] & 0X0F) << 4) | (sca_num[local_index] & 0x0F);
1489                                 sca.szDiallingNum[(sca_length/2)] = '\0';
1490                                 sca.DialNumLen = (sca_length/2);
1491                         } else {
1492                                 for (local_index = additional_len, j = 0; local_index < sca_length+1; local_index += 2, j++) {
1493                                         if (local_index == (sca_length - 1))
1494                                                 sca.szDiallingNum[j] = ((0xF0) | (sca_num[local_index] & 0x0F));
1495                                         else
1496                                                 sca.szDiallingNum[j] = ((sca_num[local_index+1] & 0X0F) << 4) | (sca_num[local_index] & 0x0F);
1497                                 }
1498                                 sca.szDiallingNum[(sca_length/2)] = sca.szDiallingNum[(sca_length/2)] | 0xF0;
1499                                 sca.DialNumLen = (sca_length/2)+1;
1500                         }
1501                         msg("%d", sca.DialNumLen);
1502                         returnStatus = tel_set_sms_sca (handle, &sca, 0, on_resp_set_sms_sca, NULL);
1503                         msg("Return status: [%d]", returnStatus);
1504                 break;
1505                 }
1506
1507                 case 3: //Set Deliver Report
1508                 {
1509                         msg("****Setting  Deliver Report(Api:SetDeliverReport)****");
1510                         //EncodeSmsDeliverReportTpdu();
1511                         break;
1512                 }
1513
1514                 case 4: //Set CB Enable/Disable
1515                 {
1516                         msg("****Setting the CB configuration(Api:SetCbConfig)****");
1517
1518                         pCBConfig = calloc (1, sizeof(TelSmsCbConfig_t));
1519                         if (!pCBConfig)
1520                                 return -1;
1521
1522                         msg("Enter CB ON/OFF (1: Enable, 0:Disable): ");
1523                         pCBConfig->CBEnabled = _get_int();
1524
1525                         msg("Enter Max ID Count: ");
1526                         pCBConfig->MsgIdMaxCount = _get_int();
1527
1528                         msg("Enter MsgIdRangeCount ( < 10 ): ");
1529                         pCBConfig->MsgIdRangeCount = _get_int();
1530
1531                         if (pCBConfig->MsgIdRangeCount <= 0
1532                                         || pCBConfig->MsgIdRangeCount >= TAPI_NETTEXT_SMS_CBMI_LIST_SIZE_MAX) {
1533                                 msg("Bad Range value");
1534                                 free (pCBConfig);
1535                                 return -1;
1536                         }
1537
1538                         for (i = 0; i < pCBConfig->MsgIdRangeCount; i++) {
1539                                 msg("Enter %d FromMsgId : ", i + 1);
1540                                 pCBConfig->MsgIDs[i].Net3gpp.FromMsgId = _get_int();
1541
1542                                 msg("Enter %d ToMsgId : ", i + 1);
1543                                 pCBConfig->MsgIDs[i].Net3gpp.ToMsgId = _get_int();
1544
1545                                 msg("Enter %d Selected : ", i + 1);
1546                                 pCBConfig->MsgIDs[i].Net3gpp.Selected = _get_int();
1547                         }
1548
1549                         pCBConfig->Net3gppType = 0x01;
1550
1551                         msg(
1552                                         "from sms test setting the cb configuration:CBEnabled:%d,Net3gppType:%d,msgIdRangeCount:%d,From: %d\t To: %d\t Selected: %d",
1553                                         pCBConfig->CBEnabled, pCBConfig->Net3gppType,
1554                                         pCBConfig->MsgIdRangeCount,
1555                                         pCBConfig->MsgIDs[0].Net3gpp.FromMsgId,
1556                                         pCBConfig->MsgIDs[0].Net3gpp.ToMsgId,
1557                                         pCBConfig->MsgIDs[0].Net3gpp.Selected);
1558
1559                         returnStatus = tel_set_sms_cb_config (handle, pCBConfig,
1560                                         on_resp_set_sms_cb_config, NULL );
1561                         msg("returnStatus after cbconfig set is is [%d]", returnStatus);
1562
1563                         free (pCBConfig);
1564
1565                         break;
1566                 }
1567                 case 5: {
1568                         unsigned int sca_length;
1569                         unsigned char sca_num[TAPI_SIM_SMSP_ADDRESS_LEN + 1];
1570                         const char* name = "AlphaID";
1571
1572                         smsParameters.RecordIndex = 0x00;
1573
1574                         /* Alpha Id */
1575                         smsParameters.AlphaIdLen = strlen(name);
1576                         memcpy(&smsParameters.szAlphaId, name, strlen(name));
1577
1578                         /* Param Indicator*/
1579                         smsParameters.ParamIndicator = 0xe1;
1580
1581                         /* Destination Number */
1582                         memset(&smsParameters.TpDestAddr, 0x0, sizeof(TelSmsAddressInfo_t));
1583
1584                         smsParameters.TpProtocolId = 3;/* PID */
1585                         smsParameters.TpDataCodingScheme = 2;/* DCS */
1586                         smsParameters.TpValidityPeriod = 1;/* VP */
1587
1588                         do {
1589                                 msg("Enter the SCA NUMBER:");
1590                                 ret = scanf("%s", sca_num);
1591                                 if (ret > TAPI_SIM_SMSP_ADDRESS_LEN)
1592                                         msg("Entered SCA is INVALID - SCA length cannot be greater than %d", TAPI_SIM_SMSP_ADDRESS_LEN);
1593                         } while(ret > TAPI_SIM_SMSP_ADDRESS_LEN);
1594
1595                         sca_length = SmsUtilEncodeSca(&(smsParameters.TpSvcCntrAddr), sca_num);
1596                         msg("Encoded SCA Address Length[%d]", sca_length);
1597
1598                         returnStatus = tel_set_sms_parameters (handle, &smsParameters, on_resp_set_sms_params, NULL );
1599                         msg("returnstatus after sparam set  is %d", returnStatus);
1600
1601                         break;
1602                 }
1603                 case 6: //Set Memory Full Notification
1604                 {
1605                         msg(
1606                                         "Enter Memory Status to be set(1:Memory Available, 2:Memory Full)");
1607                         memset (MemoryStatus, 0, sizeof (MemoryStatus));
1608                         ret = read (0, MemoryStatus, sizeof (MemoryStatus));
1609                         if (ret <= 0) {
1610                                 msg(" NULL msg can NOT be sent ");
1611                                 return -1;
1612                         }
1613                         msg("Memory Status type is %d ", atoi (MemoryStatus));
1614                         returnStatus = tel_set_sms_memory_status (handle,
1615                                         atoi (MemoryStatus), on_resp_set_mem_status, NULL ); //Set to full 0x02-Full, 0x01-available
1616                         msg("api err after memstatus set is is %d", returnStatus);
1617
1618                         break;
1619                 }
1620                 case 7: //Set Stored MsgStaus
1621                 {
1622                         msg("Not suppored in this Test App !!!");
1623                         break;
1624                 }
1625                 case 8: {
1626 // JYGU                         returnStatus =tel_set_sms_device_status();
1627                         msg("Not suppored in this Test App !!!");
1628                         break;
1629                 }
1630                 case 9: {
1631                         msg("Not suppored in this Test App !!!");
1632                         break;
1633                 }
1634                 default:
1635                         return -1;
1636         }
1637         return 1;
1638 }
1639
1640 static struct menu_data menu_sms_getting[] = { { "1",
1641                 "Get Service Center Number", NULL, Getting, NULL }, { "2",
1642                 "Get CB Setting Information", NULL, Getting, NULL }, { "3",
1643                 "Get SMS Parameters", NULL, Getting, NULL }, { "4",
1644                 "Get SMS PreferredBearer Information (Not supported)", NULL, Getting,
1645                 NULL }, { "5", "Get SMS Parameter Count", NULL, Getting, NULL }, { "6",
1646                 "Get Message Count", NULL, Getting, NULL }, { "7",
1647                 "Get SMS ready status", NULL, Getting, NULL }, { NULL, NULL , }, };
1648
1649 static struct menu_data menu_sms_setting[] =
1650                 { { "1", "Set Service Center Number", NULL, Setting, NULL }, { "2",
1651                                 "Set PreferredBearer Type (Not supported)", NULL, Setting, NULL }, { "3",
1652                                 "Set Deliver Report", NULL, Setting, NULL }, { "4",
1653                                 "Set CB Enable/Disable", NULL, Setting, NULL }, { "5",
1654                                 "Set SMS Parameters", NULL, Setting, NULL }, { "6",
1655                                 "Set Memory Status", NULL, Setting, NULL }, { "7",
1656                                 "Set Stored MsgStaus", NULL, Setting, NULL }, { "8",
1657                                 "Set Device Ready", NULL, Setting, NULL }, { "9",
1658                                 "Check Device Status", NULL, Setting, NULL }, { NULL, NULL , }, };
1659
1660 static struct menu_data menu_sms_wcdma[] = { { "1", "Send Message", NULL,
1661                 SendMessage, NULL }, { "2", "Read Message", NULL, ReadMessage, NULL }, {
1662                 "3", "Delete Message", NULL, DeleteMessage, NULL }, { "4",
1663                 "Delete All Message", NULL, NULL, NULL },       //DeleteAllMessage
1664                 { "5", "Setting", menu_sms_setting, NULL, NULL }, { "6",
1665                                 "Getting SMS Information", menu_sms_getting, NULL, NULL }, {
1666                                 NULL, NULL , }, };
1667
1668 static struct menu_data menu_sms_cdma[] = { { "1", "Send Message", NULL,
1669                 SendMessageCDMA, NULL }, { NULL, NULL , }, };
1670
1671 struct menu_data menu_sms[] = { { "1", "WCDMA", menu_sms_wcdma, NULL, NULL }, {
1672                 "2", "CDMA", menu_sms_cdma, NULL, NULL }, { NULL, NULL , }, };
1673
1674 void register_sms_event (TapiHandle *handle)
1675 {
1676         int ret;
1677
1678         /* SMS */
1679         ret = tel_register_noti_event (handle, TAPI_NOTI_SMS_INCOM_MSG,
1680                         on_noti_sms_incom_msg, NULL );
1681         if (ret != TAPI_API_SUCCESS) {
1682                 msg("event register failed(%d)", ret);
1683         }
1684
1685         ret = tel_register_noti_event (handle, TAPI_NOTI_SMS_CB_INCOM_MSG,
1686                         on_noti_sms_cb_incom_msg, NULL );
1687         if (ret != TAPI_API_SUCCESS) {
1688                 msg("event register failed(%d)", ret);
1689         }
1690
1691         ret = tel_register_noti_event (handle, TAPI_NOTI_SMS_ETWS_INCOM_MSG,
1692                         on_noti_sms_etws_incom_msg, NULL );
1693         if (ret != TAPI_API_SUCCESS) {
1694                 msg("event register failed(%d)", ret);
1695         }
1696
1697 //        ret = tel_register_noti_event(handle, TAPI_NOTI_SMS_INCOM_EX_MSG, on_noti_sms_incom_ex_msg, NULL);
1698 //        ret = tel_register_noti_event(handle, TAPI_NOTI_SMS_CB_INCOM_EX_MSG, on_noti_sms_cb_incom_ex_msg, NULL);
1699
1700         ret = tel_register_noti_event (handle, TAPI_NOTI_SMS_MEMORY_STATUS,
1701                         on_noti_sms_memory_status, NULL );
1702         if (ret != TAPI_API_SUCCESS) {
1703                 msg("event register failed(%d)", ret);
1704         }
1705
1706         ret = tel_register_noti_event (handle, TAPI_NOTI_SMS_DEVICE_READY,
1707                         on_noti_sms_ready_status, NULL );
1708         if (ret != TAPI_API_SUCCESS) {
1709                 msg("event register failed(%d)", ret);
1710         }
1711 }