[SMS] Fix telephony crash when SIM has invalid message
[framework/telephony/tel-plugin-imc.git] / src / s_sms.c
1 /*
2  * tel-plugin-imc
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Madhavi Akella <madhavi.a@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 <stdint.h>
25
26 #include <glib.h>
27
28 #include <tcore.h>
29 #include <hal.h>
30 #include <core_object.h>
31 #include <plugin.h>
32 #include <queue.h>
33 #include <co_sms.h>
34 #include <co_sim.h>
35 #include <user_request.h>
36 #include <storage.h>
37 #include <server.h>
38 #include <at.h>
39 #include <plugin.h>
40
41 #include <util.h>
42
43 #include "common/TelErr.h"
44 #include "s_common.h"
45 #include "s_sms.h"
46
47 /*=============================================================
48                                                         GSM-SMS Size
49 ==============================================================*/
50 #define MAX_GSM_SMS_TPDU_SIZE                                           244
51 #define MAX_GSM_SMS_MSG_NUM                                                     255
52 #define MAX_GSM_SMS_SERVICE_CENTER_ADDR                         12              /* Maximum number of bytes of service center address */
53 #define MAX_GSM_SMS_CBMI_LIST_SIZE                                      100             /* Maximum number of CBMI list size for CBS 30*2=60  */
54 #define MAX_GSM_SMS_PARAM_RECORD_SIZE                           156             /* Maximum number of bytes SMSP Record size (Y + 28), y : 0 ~ 128 */
55 #define MAX_GSM_SMS_STATUS_FILE_SIZE                                    2               /* Last Used TP-MR + SMS "Memory Cap. Exceeded" Noti Flag */
56 #define TAPI_SIM_SMSP_ADDRESS_LEN                                       20
57
58 /*=============================================================
59                                                         Device Ready
60 ==============================================================*/
61 #define SMS_DEVICE_READY                                1               /* Telephony device ready */
62 #define SMS_DEVICE_NOT_READY                    0               /* Telephony device not ready */
63
64 /*=============================================================
65                                                         CBMI Selection
66 ==============================================================*/
67 #define SMS_CBMI_SELECTED_SOME          0x02    /* Some CBMIs are selected */
68 #define SMS_CBMI_SELECTED_ALL                   0x01    /* All CBMIs are selected */
69
70 /*=============================================================
71                                                         Message Status
72 ==============================================================*/
73 #define AT_REC_UNREAD                                   0               /* Received and Unread */
74 #define AT_REC_READ                                     1               /* Received and Read */
75 #define AT_STO_UNSENT                                   2               /* Unsent */
76 #define AT_STO_SENT                                     3               /* Sent */
77 #define AT_ALL                                                  4               /* Unknown */
78
79 /*=============================================================
80                                                         Memory Status
81 ==============================================================*/
82 #define AT_MEMORY_AVAILABLE                     0               /* Memory Available */
83 #define AT_MEMORY_FULL                          1               /* Memory Full */
84
85 /*=============================================================
86                 SIM CRSM SW1 and Sw2 Error definitions */
87
88 #define AT_SW1_SUCCESS 0x90
89 #define AT_SW2_SUCCESS 0
90 #define AT_SW1_LEN_RESP 0x9F
91
92 #define AT_MAX_RECORD_LEN 256
93  /* SCA 12 bytes long and TDPU is 164 bytes long */
94 #define PDU_LEN_MAX 176
95 #define HEX_PDU_LEN_MAX                 ((PDU_LEN_MAX * 2) + 1)
96
97 /*=============================================================
98                                                         String Preprocessor
99 ==============================================================*/
100 #define CR              '\r'            /* Carriage Return */
101
102 /*=============================================================
103                                                         Developer
104 ==============================================================*/
105 #define SMS_SWAPBYTES16(x) (((x) & 0xffff0000) | (((x) & 0x0000ff00) >> 8) | (((x) & 0x000000ff) << 8))
106
107 void print_glib_list_elem(gpointer data, gpointer user_data);
108
109 static void on_response_class2_read_msg(TcorePending *pending, int data_len, const void *data, void *user_data);
110
111
112 gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes);
113
114 void print_glib_list_elem(gpointer data, gpointer user_data)
115 {
116         char *item = (char *)data;
117
118         dbg("item: [%s]", item);
119 }
120
121 /*=============================================================
122                                                         Send Callback
123 ==============================================================*/
124 static void on_confirmation_sms_message_send(TcorePending *p, gboolean result, void *user_data)
125 {
126         dbg("Entered Function. Request message out from queue");
127
128         dbg("TcorePending: [%p]", p);
129         dbg("result: [%02x]", result);
130         dbg("user_data: [%p]", user_data);
131
132         if (result == TRUE) {
133                 dbg("SEND OK");
134         } else { /* Failed */
135                 dbg("SEND NOK");
136         }
137
138         dbg("Exiting Function. Nothing to return");
139 }
140
141 /*=============================================================
142                                                         Utilities
143 ==============================================================*/
144 static void util_sms_free_memory(void *sms_ptr)
145 {
146         dbg("Entry");
147
148         if (NULL != sms_ptr) {
149                 dbg("Freeing memory location: [%p]", sms_ptr);
150                 free(sms_ptr);
151                 sms_ptr = NULL;
152         } else {
153                 err("Invalid memory location. Nothing to do.");
154         }
155
156         dbg("Exit");
157 }
158
159
160 static int util_sms_decode_smsParameters(unsigned char *incoming, unsigned int length, struct telephony_sms_Params *params)
161 {
162         int alpha_id_len = 0;
163         int i = 0;
164         int nOffset = 0;
165
166         dbg(" RecordLen = %d", length);
167
168         if(incoming == NULL || params == NULL)
169                 return FALSE;
170
171         alpha_id_len = length -SMS_SMSP_PARAMS_MAX_LEN;
172
173         if (alpha_id_len > 0) {
174                 if (alpha_id_len > SMS_SMSP_ALPHA_ID_LEN_MAX) {
175                         alpha_id_len = SMS_SMSP_ALPHA_ID_LEN_MAX;
176                 }
177
178                 for (i = 0; i < alpha_id_len; i++) {
179                         if (0xff == incoming[i]) {
180                                 dbg(" found");
181                                 break;
182                         }
183                 }
184
185                 memcpy(params->szAlphaId, incoming, i);
186
187                 params->alphaIdLen = i;
188
189                 dbg(" Alpha id length = %d", i);
190         } else {
191                 params->alphaIdLen = 0;
192                 dbg(" Alpha id length is zero");
193         }
194
195         params->paramIndicator = incoming[alpha_id_len];
196
197         dbg(" Param Indicator = %02x", params->paramIndicator);
198
199         if ((params->paramIndicator & SMSPValidDestAddr) == 0) {
200                 nOffset = nDestAddrOffset;
201
202                 if (0x00 == incoming[alpha_id_len + nOffset] || 0xff == incoming[alpha_id_len + nOffset]) {
203                         params->tpDestAddr.dialNumLen = 0;
204
205                         dbg("DestAddr Length is 0");
206                 } else {
207                         if (0 < (int) incoming[alpha_id_len + nOffset]) {
208                                 params->tpDestAddr.dialNumLen = (int) (incoming[alpha_id_len + nOffset] - 1);
209
210                                 if (params->tpDestAddr.dialNumLen > SMS_SMSP_ADDRESS_LEN)
211                                         params->tpDestAddr.dialNumLen = SMS_SMSP_ADDRESS_LEN;
212                         } else {
213                                 params->tpDestAddr.dialNumLen = 0;
214                         }
215
216                         params->tpDestAddr.numPlanId = incoming[alpha_id_len + (++nOffset)] & 0x0f;
217                         params->tpDestAddr.typeOfNum = (incoming[alpha_id_len + nOffset] & 0x70) >> 4;
218
219                         memcpy(params->tpDestAddr.diallingNum, &incoming[alpha_id_len + (++nOffset)], (params->tpDestAddr.dialNumLen));
220
221                         dbg("Dest TON is %d", params->tpDestAddr.typeOfNum);
222                         dbg("Dest NPI is %d", params->tpDestAddr.numPlanId);
223                         dbg("Dest Length = %d", params->tpDestAddr.dialNumLen);
224                         dbg("Dest Addr = %s", params->tpDestAddr.diallingNum);
225                 }
226         } else {
227                 params->tpDestAddr.dialNumLen = 0;
228         }
229
230         if ((params->paramIndicator & SMSPValidSvcAddr) == 0) {
231                 nOffset = nSCAAddrOffset;
232
233                 if (0x00 == (int) incoming[alpha_id_len + nOffset] || 0xff == (int) incoming[alpha_id_len + nOffset]) {
234                         params->tpSvcCntrAddr.dialNumLen = 0;
235
236                         dbg(" SCAddr Length is 0");
237                 } else {
238                         if (0 < (int) incoming[alpha_id_len + nOffset]) {
239                                 params->tpSvcCntrAddr.dialNumLen = (int) (incoming[alpha_id_len + nOffset] - 1);
240
241                                 if (params->tpSvcCntrAddr.dialNumLen > SMS_SMSP_ADDRESS_LEN)
242                                         params->tpSvcCntrAddr.dialNumLen = SMS_SMSP_ADDRESS_LEN;
243
244                                 params->tpSvcCntrAddr.numPlanId = incoming[alpha_id_len + (++nOffset)] & 0x0f;
245                                 params->tpSvcCntrAddr.typeOfNum = (incoming[alpha_id_len + nOffset] & 0x70) >> 4;
246
247                                 memcpy(params->tpSvcCntrAddr.diallingNum, &incoming[alpha_id_len + (++nOffset)], (params->tpSvcCntrAddr.dialNumLen));
248
249                                 dbg("SCAddr Length = %d ", params->tpSvcCntrAddr.dialNumLen);
250                                 dbg("SCAddr TON is %d", params->tpSvcCntrAddr.typeOfNum);
251                                 dbg("SCAddr NPI is %d", params->tpSvcCntrAddr.numPlanId);
252
253                                 for (i = 0; i < (int) params->tpSvcCntrAddr.dialNumLen; i++)
254                                         dbg("SCAddr = %d [%02x]", i, params->tpSvcCntrAddr.diallingNum[i]);
255                         } else {
256                                 params->tpSvcCntrAddr.dialNumLen = 0;
257                         }
258                 }
259         } else if ((0x00 < (int) incoming[alpha_id_len + nSCAAddrOffset] && (int) incoming[alpha_id_len + nSCAAddrOffset] <= 12)
260                            || 0xff != (int) incoming[alpha_id_len + nSCAAddrOffset]) {
261                 nOffset = nSCAAddrOffset;
262
263                 if (0x00 == (int) incoming[alpha_id_len + nOffset] || 0xff == (int) incoming[alpha_id_len + nOffset]) {
264                         params->tpSvcCntrAddr.dialNumLen = 0;
265                         dbg("SCAddr Length is 0");
266                 } else {
267                         if (0 < (int) incoming[alpha_id_len + nOffset]) {
268                                 params->tpSvcCntrAddr.dialNumLen = (int) (incoming[alpha_id_len + nOffset] - 1);
269
270                                 params->tpSvcCntrAddr.dialNumLen = incoming[alpha_id_len + nOffset] - 1;
271
272                                 if (params->tpSvcCntrAddr.dialNumLen > SMS_SMSP_ADDRESS_LEN)
273                                         params->tpSvcCntrAddr.dialNumLen = SMS_SMSP_ADDRESS_LEN;
274
275                                 params->tpSvcCntrAddr.numPlanId = incoming[alpha_id_len + (++nOffset)] & 0x0f;
276                                 params->tpSvcCntrAddr.typeOfNum = (incoming[alpha_id_len + nOffset] & 0x70) >> 4;
277
278                                 memcpy(params->tpSvcCntrAddr.diallingNum, &incoming[alpha_id_len + (++nOffset)],
279                                            (params->tpSvcCntrAddr.dialNumLen));
280
281                                 dbg("SCAddr Length = %d ", params->tpSvcCntrAddr.dialNumLen);
282                                 dbg("SCAddr TON is %d", params->tpSvcCntrAddr.typeOfNum);
283                                 dbg("SCAddr NPI is %d", params->tpSvcCntrAddr.numPlanId);
284
285                                 for (i = 0; i < (int) params->tpSvcCntrAddr.dialNumLen; i++)
286                                         dbg("SCAddr = %d [%02x]", i, params->tpSvcCntrAddr.diallingNum[i]);
287                         } else {
288                                 params->tpSvcCntrAddr.dialNumLen = 0;
289                         }
290                 }
291         } else {
292                         params->tpSvcCntrAddr.dialNumLen = 0;
293         }
294
295         if ((params->paramIndicator & SMSPValidPID) == 0 && (alpha_id_len + nPIDOffset) < MAX_GSM_SMS_PARAM_RECORD_SIZE) {
296                 params->tpProtocolId = incoming[alpha_id_len + nPIDOffset];
297         }
298         if ((params->paramIndicator & SMSPValidDCS) == 0 && (alpha_id_len + nDCSOffset) < MAX_GSM_SMS_PARAM_RECORD_SIZE) {
299                 params->tpDataCodingScheme = incoming[alpha_id_len + nDCSOffset];
300         }
301         if ((params->paramIndicator & SMSPValidVP) == 0 && (alpha_id_len + nVPOffset) < MAX_GSM_SMS_PARAM_RECORD_SIZE) {
302                 params->tpValidityPeriod = incoming[alpha_id_len + nVPOffset];
303         }
304
305         dbg(" Alpha Id(Len) = %d", (int) params->alphaIdLen);
306
307         for (i = 0; i < (int) params->alphaIdLen; i++) {
308                 dbg(" Alpha Id = [%d] [%c]", i, params->szAlphaId[i]);
309         }
310         dbg(" PID = %d",params->tpProtocolId);
311         dbg(" DCS = %d",params->tpDataCodingScheme);
312         dbg(" VP = %d",params->tpValidityPeriod);
313
314         return TRUE;
315 }
316
317 /*=============================================================
318                                                         Notifications
319 ==============================================================*/
320 static gboolean on_event_class2_sms_incom_msg(CoreObject *obj,
321                                                                         const void *event_info, void *user_data)
322 {
323         //+CMTI: <mem>,<index>
324
325         GSList *tokens = NULL , *lines = NULL;
326         char *line = NULL, *cmd_str = NULL;
327         int index = 0, mem_type = 0;
328         TcoreHal *hal = NULL;
329         TcoreATRequest *atreq = NULL;
330         TcorePending *pending = NULL;
331
332         dbg("Entered Function");
333
334         lines = (GSList *)event_info;
335         line = (char *)g_slist_nth_data(lines, 0); /* Fetch Line 1 */
336
337         dbg("Line 1: [%s]", line);
338
339         if (!line) {
340                 err("Line 1 is invalid");
341                 return FALSE;
342         }
343
344         tokens = tcore_at_tok_new(line); /* Split Line 1 into tokens */
345         mem_type = atoi(g_slist_nth_data(tokens, 0));       // Type of Memory stored
346         index = atoi((char *) g_slist_nth_data(tokens, 1));
347
348         hal = tcore_object_get_hal(obj);
349         if (NULL == hal) {
350                 err("NULL input. Unable to proceed");
351                 dbg("readMsg: hal: [%p]", hal);
352
353                 dbg("Exit");
354                 return TCORE_RETURN_EINVAL;
355         }
356
357         dbg("index: [%d]", index);
358
359         cmd_str = g_strdup_printf("AT+CMGR=%d", index);
360         atreq     = tcore_at_request_new((const char *)cmd_str, "+CMGR", TCORE_AT_PDU);
361         pending = tcore_pending_new(obj, 0);
362
363         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
364                 err("Out of memory. Unable to proceed");
365                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
366
367                 //free memory we own
368                 g_free(cmd_str);
369                 util_sms_free_memory(atreq);
370                 util_sms_free_memory(pending);
371
372                 dbg("Exit");
373                 return TCORE_RETURN_ENOMEM;
374         }
375
376         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
377
378         tcore_pending_set_request_data(pending, 0, atreq);
379         tcore_pending_set_response_callback(pending, on_response_class2_read_msg, (void *)(uintptr_t)index); //storing index as user data for response
380         tcore_pending_link_user_request(pending, NULL);
381         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
382         tcore_hal_send_request(hal, pending);
383         g_free(cmd_str);
384
385         if(tokens)
386                 tcore_at_tok_free(tokens);
387
388         return TRUE;
389 }
390
391 static gboolean on_event_sms_incom_msg(CoreObject *o, const void *event_info, void *user_data)
392 {
393         //+CMT: [<alpha>],<length><CR><LF><pdu> (PDU mode enabled);
394
395         int rtn = -1;
396         GSList *tokens = NULL;
397         GSList *lines = NULL;
398         char *line = NULL;
399         int pdu_len = 0, no_of_tokens = 0;
400         unsigned char *bytePDU = NULL;
401         struct tnoti_sms_umts_msg gsmMsgInfo;
402         int sca_length = 0;
403
404         dbg("Entered Function");
405
406         lines = (GSList *)event_info;
407         memset(&gsmMsgInfo, 0x00, sizeof(struct tnoti_sms_umts_msg));
408
409         if (2 != g_slist_length(lines)) {
410                 err("Invalid number of lines for +CMT. Must be 2");
411                 return FALSE;
412         }
413
414         line = (char *)g_slist_nth_data(lines, 0); /* Fetch Line 1 */
415
416         dbg("Line 1: [%s]", line);
417
418         if (!line) {
419                 err("Line 1 is invalid");
420                 return FALSE;
421         }
422
423         tokens = tcore_at_tok_new(line); /* Split Line 1 into tokens */
424
425         no_of_tokens = g_slist_length(tokens);
426
427         if (no_of_tokens == 2) { // in case of incoming SMS +CMT
428                 dbg("Alpha ID: [%02x]", g_slist_nth_data(tokens, 0)); /* 0: Alpha ID */
429                 pdu_len = atoi((char *)g_slist_nth_data(tokens, 1));
430                 dbg("pdu_len: [%d]", pdu_len);  /* 1: PDU Length */
431         } else if (no_of_tokens == 1) { // in case of incoming status report +CDS
432                 pdu_len = atoi((char *)g_slist_nth_data(tokens, 0));
433                 dbg("pdu_len: [%d]", pdu_len);  /* 1: PDU Length */
434         }
435
436         line = (char *)g_slist_nth_data(lines, 1); /* Fetch Line 2 */
437
438         dbg("Line 2: [%s]", line);
439
440         if (!line) {
441                 err("Line 2 is invalid");
442                 return FALSE;
443         }
444
445         /* Convert to Bytes */
446         bytePDU = (unsigned char *)util_hexStringToBytes(line);
447
448         sca_length = bytePDU[0];
449
450         dbg("SCA length = %d", sca_length);
451
452         gsmMsgInfo.msgInfo.msgLength = pdu_len;
453
454         if (sca_length == 0) {
455                 memcpy(gsmMsgInfo.msgInfo.tpduData, &bytePDU[1], gsmMsgInfo.msgInfo.msgLength);
456         } else {
457                 memcpy(gsmMsgInfo.msgInfo.sca, &bytePDU[1], sca_length);
458                 memcpy(gsmMsgInfo.msgInfo.tpduData, &bytePDU[sca_length+1], gsmMsgInfo.msgInfo.msgLength);
459         }
460
461         util_hex_dump("      ", strlen(line)/2, bytePDU);
462         util_hex_dump("      ", sca_length, gsmMsgInfo.msgInfo.sca);
463         util_hex_dump("      ", gsmMsgInfo.msgInfo.msgLength,gsmMsgInfo.msgInfo.tpduData);
464
465         rtn = tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_SMS_INCOM_MSG, sizeof(struct tnoti_sms_umts_msg), &gsmMsgInfo);
466
467         if(tokens)
468                 tcore_at_tok_free(tokens);
469
470         g_free(bytePDU);
471
472         return TRUE;
473 }
474
475
476
477 static gboolean on_event_sms_memory_status(CoreObject *o, const void *event_info, void *user_data)
478 {
479         struct tnoti_sms_memory_status memStatusInfo = {0,};
480
481         int rtn = -1 ,memoryStatus = -1;
482         GSList *tokens=NULL;
483         GSList *lines=NULL;
484         char *line = NULL , *pResp = NULL;
485
486         dbg(" Entry");
487
488         lines = (GSList *)event_info;
489         if (1 != g_slist_length(lines)) {
490                 dbg("unsolicited msg but multiple line");
491         }
492
493         line = (char*)(lines->data);
494
495         if (line) {
496                 dbg("Response OK");
497                 tokens = tcore_at_tok_new(line);
498                 pResp = g_slist_nth_data(tokens, 0);
499
500                 if (pResp) {
501                         memoryStatus = atoi(pResp);
502                         dbg("memoryStatus is %d",memoryStatus);
503                         if (memoryStatus == 0) {//SIM Full condition
504                                 memStatusInfo.status = SMS_PHONE_MEMORY_STATUS_FULL;
505                         }
506                         rtn = tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_SMS_MEMORY_STATUS, sizeof(struct tnoti_sms_memory_status), &memStatusInfo);
507                 }
508                 tcore_at_tok_free(tokens);
509         }else {
510                 dbg("Response NOK");
511         }
512
513         dbg(" Exit ");
514         return TRUE;
515 }
516
517 static gboolean on_event_sms_cb_incom_msg(CoreObject *o, const void *event_info, void *user_data)
518 {
519         //+CBM: <length><CR><LF><pdu>
520
521         struct tnoti_sms_cellBroadcast_msg cbMsgInfo;
522
523         int rtn = -1 , length = 0;
524         char * line = NULL, *pdu = NULL, *pResp = NULL;
525         GSList *tokens = NULL;
526         GSList *lines = NULL;
527
528         dbg(" Func Entrance");
529
530         lines = (GSList *)event_info;
531
532         memset(&cbMsgInfo, 0, sizeof(struct tnoti_sms_cellBroadcast_msg));
533
534         line = (char *)(lines->data);
535
536         if (line != NULL) {
537                 dbg("Response OK");
538                 dbg("Noti line is %s",line);
539                 tokens = tcore_at_tok_new(line); /* Split Line 1 into tokens */
540
541                 pResp = g_slist_nth_data(tokens, 0);
542                 if (pResp) {
543                         length = atoi(pResp);
544                 } else {
545                         dbg("token 0 is null");
546                 }
547
548                 pdu = g_slist_nth_data(lines, 1);
549                 if (pdu != NULL) {
550                         cbMsgInfo.cbMsg.length = length;
551                         cbMsgInfo.cbMsg.cbMsgType = SMS_CB_MSG_GSM;
552
553                         dbg("CB Msg LENGTH [%2x]", length);
554
555                         if ((cbMsgInfo.cbMsg.length >0) && (SMS_CB_SIZE_MAX >= cbMsgInfo.cbMsg.length)) {
556                                 unsigned char *byte_pdu = NULL;
557
558                                 byte_pdu = (unsigned char *)util_hexStringToBytes(pdu);
559
560                                 memcpy(cbMsgInfo.cbMsg.msgData, (char*)byte_pdu, cbMsgInfo.cbMsg.length);
561                                 rtn = tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_SMS_CB_INCOM_MSG, sizeof(struct tnoti_sms_cellBroadcast_msg), &cbMsgInfo);
562                                 g_free(byte_pdu);
563                         } else {
564                                 dbg("Invalid Message Length");
565                         }
566                 } else {
567                         dbg("Recieved NULL pdu");
568                 }
569         } else {
570                 dbg("Response NOK");
571         }
572
573         dbg(" Return value [%d]",rtn);
574
575         if(tokens)
576                 tcore_at_tok_free(tokens);
577
578         return TRUE;
579 }
580
581
582 /*=============================================================
583                                                         Responses
584 ==============================================================*/
585 static void on_response_sms_delete_msg(TcorePending *p, int data_len, const void *data, void *user_data)
586 {
587         struct tresp_sms_delete_msg delMsgInfo = {0,};
588         UserRequest *ur = NULL;
589         const TcoreATResponse *atResp = data;
590
591         int rtn = -1;
592         int index = (int) user_data;
593
594         dbg(" Func Entrance");
595
596         ur = tcore_pending_ref_user_request(p);
597         if (atResp->success) {
598                 dbg("Response OK");
599                 delMsgInfo.index = index;
600                 delMsgInfo.result = SMS_SENDSMS_SUCCESS;
601         } else {
602                 dbg("Response NOK");
603                 delMsgInfo.index = index;
604                 delMsgInfo.result = SMS_DEVICE_FAILURE;
605         }
606
607         rtn = tcore_user_request_send_response(ur, TRESP_SMS_DELETE_MSG, sizeof(struct tresp_sms_delete_msg), &delMsgInfo);
608
609         return;
610 }
611
612 static void on_response_sms_save_msg(TcorePending *p, int data_len, const void *data, void *user_data)
613 {
614         struct tresp_sms_save_msg saveMsgInfo = {0,};
615         UserRequest *ur = NULL;
616         const TcoreATResponse *atResp = data;
617         GSList *tokens = NULL;
618         char *line = NULL;
619         char *pResp = NULL;
620         int rtn = -1;
621
622         ur = tcore_pending_ref_user_request(p);
623         if (atResp->success) {
624                 dbg("Response OK");
625                 if (atResp->lines) {
626                         line = (char *)atResp->lines->data;
627                         tokens = tcore_at_tok_new(line);
628                         pResp = g_slist_nth_data(tokens, 0);
629                         if (pResp) {
630                                 dbg("0: %s", pResp);
631                                 saveMsgInfo.index = (atoi(pResp) - 1); /* IMC index starts from 1 */
632                                 saveMsgInfo.result = SMS_SENDSMS_SUCCESS;
633                         } else {
634                                 dbg("No Tokens");
635                                 saveMsgInfo.index = -1;
636                                 saveMsgInfo.result = SMS_DEVICE_FAILURE;
637                         }
638                         tcore_at_tok_free(tokens);
639                 }
640         } else {
641                 dbg("Response NOK");
642                 saveMsgInfo.index = -1;
643                 saveMsgInfo.result = SMS_DEVICE_FAILURE;
644         }
645
646         rtn = tcore_user_request_send_response(ur, TRESP_SMS_SAVE_MSG, sizeof(struct tresp_sms_save_msg), &saveMsgInfo);
647         dbg("Return value [%d]", rtn);
648         return;
649 }
650
651 static void on_response_send_umts_msg(TcorePending *pending, int data_len, const void *data, void *user_data)
652 {
653         const TcoreATResponse *at_response = data;
654         struct tresp_sms_send_umts_msg resp_umts;
655         UserRequest *user_req = NULL;
656
657         int msg_ref = 0;
658         GSList *tokens = NULL;
659         char *gslist_line = NULL, *line_token = NULL;
660
661         dbg("Entry");
662
663         user_req = tcore_pending_ref_user_request(pending);
664
665         if (NULL == user_req) {
666                 err("No user request");
667
668                 dbg("Exit");
669                 return;
670         }
671
672         memset(&resp_umts, 0x00, sizeof(resp_umts));
673         resp_umts.result = SMS_DEVICE_FAILURE;
674
675         if (at_response->success > 0) { /* SUCCESS */
676                 dbg("Response OK");
677                 if (at_response->lines) { // lines present in at_response
678                         gslist_line = (char *)at_response->lines->data;
679                         dbg("gslist_line: [%s]", gslist_line);
680
681                         tokens = tcore_at_tok_new(gslist_line); //extract tokens
682
683                         line_token = g_slist_nth_data(tokens, 0);
684                         if (line_token != NULL) {
685                                 msg_ref = atoi(line_token);
686                                 dbg("Message Reference: [%d]", msg_ref);
687
688                                 resp_umts.result = SMS_SENDSMS_SUCCESS;
689                         } else {
690                                 dbg("No Message Reference received");
691                         }
692                         tcore_at_tok_free(tokens);
693                 } else { // no lines in at_response
694                         dbg("No lines");
695                 }
696         } else { // failure
697                 dbg("Response NOK");
698         }
699
700         tcore_user_request_send_response(user_req, TRESP_SMS_SEND_UMTS_MSG, sizeof(resp_umts), &resp_umts);
701
702         dbg("Exit");
703         return;
704 }
705
706 static void on_response_class2_read_msg(TcorePending *pending, int data_len, const void *data, void *user_data)
707 {
708         const TcoreATResponse *at_response = data;
709         GSList *tokens=NULL;
710         char *gslist_line = NULL, *line_token = NULL, *hex_pdu = NULL;
711         int  pdu_len = 0, rtn = 0;
712         unsigned char *bytePDU = NULL;
713         struct tnoti_sms_umts_msg gsmMsgInfo;
714         int sca_length= 0;
715
716         dbg("Entry");
717         dbg("lines: [%p]", at_response->lines);
718         g_slist_foreach(at_response->lines, print_glib_list_elem, NULL); //for debug log
719
720         if (at_response->success > 0) {
721                 dbg("Response OK");
722                 if (at_response->lines) {
723                         //fetch first line
724                         gslist_line = (char *)at_response->lines->data;
725
726                         dbg("gslist_line: [%s]", gslist_line);
727
728                         tokens = tcore_at_tok_new(gslist_line);
729                         dbg("Number of tokens: [%d]", g_slist_length(tokens));
730                         g_slist_foreach(tokens, print_glib_list_elem, NULL); //for debug log
731
732                         line_token = g_slist_nth_data(tokens, 2); //Third Token: Length
733                         if (line_token != NULL) {
734                                 pdu_len = atoi(line_token);
735                                 dbg("Length: [%d]", pdu_len);
736                         }
737
738                         //fetch second line
739                         gslist_line = (char *)at_response->lines->next->data;
740
741                         dbg("gslist_line: [%s]", gslist_line);
742
743                         //free the consumed token
744                         tcore_at_tok_free(tokens);
745
746                         tokens = tcore_at_tok_new(gslist_line);
747                         dbg("Number of tokens: [%d]", g_slist_length(tokens));
748                         g_slist_foreach(tokens, print_glib_list_elem, NULL); //for debug log
749
750                         hex_pdu = g_slist_nth_data(tokens, 0); //Fetch SMS PDU
751
752                         //free the consumed token
753                         tcore_at_tok_free(tokens);
754                 } else {
755                         dbg("No lines");
756                 }
757         } else {
758                 err("Response NOK");
759         }
760
761         /* Convert to Bytes */
762         bytePDU = (unsigned char *)util_hexStringToBytes(hex_pdu);
763
764         sca_length = bytePDU[0];
765
766         dbg("SCA length = %d", sca_length);
767
768         gsmMsgInfo.msgInfo.msgLength = pdu_len;
769
770         if (sca_length == 0) {
771                 memcpy(gsmMsgInfo.msgInfo.tpduData, &bytePDU[1], gsmMsgInfo.msgInfo.msgLength);
772         } else {
773                 memcpy(gsmMsgInfo.msgInfo.sca, bytePDU, sca_length);
774                 memcpy(gsmMsgInfo.msgInfo.tpduData, &bytePDU[sca_length+1], gsmMsgInfo.msgInfo.msgLength);
775         }
776
777         util_hex_dump("      ", strlen(hex_pdu)/2, bytePDU);
778         util_hex_dump("      ", sca_length, gsmMsgInfo.msgInfo.sca);
779         util_hex_dump("      ", gsmMsgInfo.msgInfo.msgLength,gsmMsgInfo.msgInfo.tpduData);
780
781         rtn = tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(tcore_pending_ref_core_object(pending))), tcore_pending_ref_core_object(pending), TNOTI_SMS_INCOM_MSG, sizeof(struct tnoti_sms_umts_msg), &gsmMsgInfo);
782
783         g_free(bytePDU);
784
785         dbg("Exit");
786         return;
787 }
788
789 static void on_response_read_msg(TcorePending *pending, int data_len, const void *data, void *user_data)
790 {
791         const TcoreATResponse *at_response = data;
792         struct tresp_sms_read_msg resp_read_msg;
793         UserRequest *user_req = NULL;
794
795         GSList *tokens=NULL;
796         char *gslist_line = NULL, *line_token = NULL, *byte_pdu = NULL, *hex_pdu = NULL;
797         int sca_length = 0;
798         int msg_status = 0, alpha_id = 0, pdu_len = 0;
799         int index = (int)(uintptr_t)user_data;
800
801         dbg("Entry");
802         dbg("index: [%d]", index);
803         g_slist_foreach(at_response->lines, print_glib_list_elem, NULL); //for debug log
804
805         user_req = tcore_pending_ref_user_request(pending);
806         if (NULL == user_req) {
807                 err("No user request");
808
809                 dbg("Exit");
810                 return;
811         }
812
813         memset(&resp_read_msg, 0x00, sizeof(resp_read_msg));
814         resp_read_msg.result = SMS_PHONE_FAILURE;
815
816         if (at_response->success > 0) {
817                 dbg("Response OK");
818                 if (at_response->lines) {
819                         //fetch first line
820                         gslist_line = (char *)at_response->lines->data;
821
822                         dbg("gslist_line: [%s]", gslist_line);
823
824                         tokens = tcore_at_tok_new(gslist_line);
825                         dbg("Number of tokens: [%d]", g_slist_length(tokens));
826                         g_slist_foreach(tokens, print_glib_list_elem, NULL); //for debug log
827
828                         line_token = g_slist_nth_data(tokens, 0); //First Token: Message Status
829                         if (line_token != NULL) {
830                                 msg_status = atoi(line_token);
831                                 dbg("msg_status is %d",msg_status);
832                                 switch (msg_status) {
833                                         case AT_REC_UNREAD:
834                                                 resp_read_msg.dataInfo.msgStatus = SMS_STATUS_UNREAD;
835                                                 break;
836
837                                         case AT_REC_READ:
838                                                 resp_read_msg.dataInfo.msgStatus = SMS_STATUS_READ;
839                                                 break;
840
841                                         case AT_STO_UNSENT:
842                                                 resp_read_msg.dataInfo.msgStatus = SMS_STATUS_UNSENT;
843                                                 break;
844
845                                         case AT_STO_SENT:
846                                                 resp_read_msg.dataInfo.msgStatus = SMS_STATUS_SENT;
847                                                 break;
848
849                                         case AT_ALL: //Fall Through
850                                         default: //Fall Through
851                                                 resp_read_msg.dataInfo.msgStatus = SMS_STATUS_RESERVED;
852                                                 break;
853                                 }
854                         }
855
856                         line_token = g_slist_nth_data(tokens, 1); //Second Token: AlphaID
857                         if (line_token != NULL) {
858                                 alpha_id = atoi(line_token);
859                                 dbg("AlphaID: [%d]", alpha_id);
860                         }
861
862                         line_token = g_slist_nth_data(tokens, 2); //Third Token: Length
863                         if (line_token != NULL) {
864                                 pdu_len = atoi(line_token);
865                                 dbg("Length: [%d]", pdu_len);
866                         }
867
868                         //fetch second line
869                         hex_pdu = (char *) at_response->lines->next->data;
870
871                         dbg("EF-SMS PDU: [%s]", hex_pdu);
872
873                         //free the consumed token
874                         tcore_at_tok_free(tokens);
875
876                         if (NULL != hex_pdu) {
877                                 util_hex_dump("    ", sizeof(hex_pdu), (void *)hex_pdu);
878
879                                 byte_pdu = util_hexStringToBytes(hex_pdu);
880
881                                 sca_length = (int)byte_pdu[0];
882
883                                 resp_read_msg.dataInfo.simIndex = index; //Retrieving index stored as user_data
884
885                                 dbg("SCA Length : %d", sca_length);
886
887                                 resp_read_msg.dataInfo.smsData.msgLength = pdu_len;
888                                 dbg("msgLength: [%d]", resp_read_msg.dataInfo.smsData.msgLength);
889
890                                 if(0 == sca_length) {
891                                         if ((resp_read_msg.dataInfo.smsData.msgLength > 0)
892                                                 && (resp_read_msg.dataInfo.smsData.msgLength <= SMS_SMDATA_SIZE_MAX))   {
893                                                 memset(resp_read_msg.dataInfo.smsData.sca, 0, TAPI_SIM_SMSP_ADDRESS_LEN);
894                                                 memcpy(resp_read_msg.dataInfo.smsData.tpduData, &byte_pdu[1], resp_read_msg.dataInfo.smsData.msgLength);
895
896                                                 resp_read_msg.result = SMS_SUCCESS;
897                                         } else {
898                                                 dbg("Invalid Message Length");
899                                                 resp_read_msg.result = SMS_INVALID_PARAMETER_FORMAT;
900                                         }
901                                 } else if (sca_length > SMS_ENCODED_SCA_LEN_MAX) {
902                                         dbg("Invalid Message Length");
903                                         resp_read_msg.result = SMS_INVALID_PARAMETER_FORMAT;
904                                 } else {
905                                         if ((resp_read_msg.dataInfo.smsData.msgLength > 0)
906                                                 && (resp_read_msg.dataInfo.smsData.msgLength <= SMS_SMDATA_SIZE_MAX)) {
907                                                 memcpy(resp_read_msg.dataInfo.smsData.sca, (char *)byte_pdu, (sca_length+1));
908                                                 memcpy(resp_read_msg.dataInfo.smsData.tpduData, &byte_pdu[sca_length+1], resp_read_msg.dataInfo.smsData.msgLength);
909
910                                                 util_hex_dump("    ", SMS_SMSP_ADDRESS_LEN, (void *)resp_read_msg.dataInfo.smsData.sca);
911                                                 util_hex_dump("    ", (SMS_SMDATA_SIZE_MAX + 1), (void *)resp_read_msg.dataInfo.smsData.tpduData);
912                                                 util_hex_dump("    ", sizeof(byte_pdu), (void *)byte_pdu);
913
914                                                 resp_read_msg.result = SMS_SUCCESS;
915                                         } else {
916                                                 dbg("Invalid Message Length");
917                                                 resp_read_msg.result = SMS_INVALID_PARAMETER_FORMAT;
918                                         }
919                                 }
920                                 g_free(byte_pdu);
921                         }else {
922                                 dbg("NULL PDU");
923                         }
924                 }else {
925                         dbg("No lines");
926                 }
927         } else {
928                 err("Response NOK");
929         }
930
931         tcore_user_request_send_response(user_req, TRESP_SMS_READ_MSG, sizeof(resp_read_msg), &resp_read_msg);
932
933         dbg("Exit");
934         return;
935 }
936
937 static void on_response_get_msg_indices(TcorePending *pending, int data_len, const void *data, void *user_data)
938 {
939         const TcoreATResponse *at_response = data;
940         struct tresp_sms_get_storedMsgCnt resp_stored_msg_cnt;
941         UserRequest *user_req = NULL;
942         struct tresp_sms_get_storedMsgCnt *resp_stored_msg_cnt_prev = NULL;
943
944         GSList *tokens = NULL;
945         char *gslist_line = NULL, *line_token = NULL;
946         int gslist_line_count = 0, ctr_loop = 0;
947
948         dbg("Entry");
949
950         resp_stored_msg_cnt_prev = (struct tresp_sms_get_storedMsgCnt *)user_data;
951         user_req = tcore_pending_ref_user_request(pending);
952
953         memset(&resp_stored_msg_cnt, 0x00, sizeof(resp_stored_msg_cnt));
954         resp_stored_msg_cnt.result = SMS_DEVICE_FAILURE;
955
956         if (at_response->success) {
957                 dbg("Response OK");
958                 if (at_response->lines) {
959                         gslist_line_count = g_slist_length(at_response->lines);
960
961                         if (gslist_line_count > SMS_GSM_SMS_MSG_NUM_MAX)
962                                 gslist_line_count = SMS_GSM_SMS_MSG_NUM_MAX;
963
964                         dbg("Number of lines: [%d]", gslist_line_count);
965                         g_slist_foreach(at_response->lines, print_glib_list_elem, NULL); //for debug log
966
967                         for (ctr_loop = 0; ctr_loop < gslist_line_count; ctr_loop++) {
968                                 gslist_line = (char *)g_slist_nth_data(at_response->lines, ctr_loop); /* Fetch Line i */
969
970                                 dbg("gslist_line [%d] is [%s]", ctr_loop, gslist_line);
971
972                                 if (NULL != gslist_line) {
973                                         tokens = tcore_at_tok_new(gslist_line);
974
975                                         g_slist_foreach(tokens, print_glib_list_elem, NULL); //for debug log
976
977                                         line_token = g_slist_nth_data(tokens, 0);
978                                         if (NULL != line_token) {
979                                                 resp_stored_msg_cnt.storedMsgCnt.indexList[ctr_loop] = atoi(line_token);
980                                                 resp_stored_msg_cnt.result = SMS_SENDSMS_SUCCESS;
981                                         } else {
982                                                 dbg("line_token of gslist_line [%d] is NULL", ctr_loop);
983                                                 continue;
984                                         }
985                                         tcore_at_tok_free(tokens);
986                                 } else {
987                                         dbg("gslist_line [%d] is NULL", ctr_loop);
988                                         continue;
989                                 }
990                         }
991                 } else {
992                         dbg("No lines.");
993                         if (resp_stored_msg_cnt_prev->storedMsgCnt.usedCount == 0) { // Check if used count is zero
994                                 resp_stored_msg_cnt.result = SMS_SENDSMS_SUCCESS;
995                         }
996                 }
997         } else {
998                 dbg("Respnose NOK");
999         }
1000
1001         resp_stored_msg_cnt.storedMsgCnt.totalCount = resp_stored_msg_cnt_prev->storedMsgCnt.totalCount;
1002         resp_stored_msg_cnt.storedMsgCnt.usedCount = resp_stored_msg_cnt_prev->storedMsgCnt.usedCount;
1003
1004         util_sms_free_memory(resp_stored_msg_cnt_prev);
1005
1006         dbg("total: [%d], used: [%d], result: [%d]", resp_stored_msg_cnt.storedMsgCnt.totalCount, resp_stored_msg_cnt.storedMsgCnt.usedCount, resp_stored_msg_cnt.result);
1007         for (ctr_loop = 0; ctr_loop < gslist_line_count; ctr_loop++) {
1008                 dbg("index: [%d]", resp_stored_msg_cnt.storedMsgCnt.indexList[ctr_loop]);
1009         }
1010
1011         tcore_user_request_send_response(user_req, TRESP_SMS_GET_STORED_MSG_COUNT, sizeof(resp_stored_msg_cnt), &resp_stored_msg_cnt);
1012
1013         dbg("Exit");
1014         return;
1015 }
1016
1017 static void on_response_get_stored_msg_cnt(TcorePending *pending, int data_len, const void *data, void *user_data)
1018 {
1019         UserRequest *ur = NULL, *ur_dup = NULL;
1020         struct tresp_sms_get_storedMsgCnt *respStoredMsgCnt = NULL;
1021         const TcoreATResponse *atResp = data;
1022         GSList *tokens=NULL;
1023         char *line = NULL , *pResp = NULL , *cmd_str = NULL;
1024         TcoreATRequest *atReq = NULL;
1025         int usedCnt = 0, totalCnt = 0, result = 0;
1026
1027         TcorePending *pending_new = NULL;
1028         CoreObject *o = NULL;
1029
1030         dbg("Entered");
1031
1032         respStoredMsgCnt = malloc(sizeof(struct tresp_sms_get_storedMsgCnt));
1033         result = SMS_DEVICE_FAILURE;
1034
1035         ur = tcore_pending_ref_user_request(pending);
1036         ur_dup = tcore_user_request_ref(ur);
1037         o = tcore_pending_ref_core_object(pending);
1038
1039         if (atResp->success > 0) {
1040                 dbg("Response OK");
1041                 if (NULL != atResp->lines) {
1042                         line = (char *)atResp->lines->data;
1043                         dbg("line is %s",line);
1044
1045                         tokens = tcore_at_tok_new(line);
1046                         pResp = g_slist_nth_data(tokens, 0);
1047
1048                         if (pResp) {
1049                                 usedCnt =atoi(pResp);
1050                                 dbg("used cnt is %d",usedCnt);
1051                         }
1052
1053                         pResp = g_slist_nth_data(tokens, 1);
1054                         if (pResp) {
1055                                 totalCnt =atoi(pResp);
1056                                 result = SMS_SENDSMS_SUCCESS;
1057
1058                                 respStoredMsgCnt->storedMsgCnt.usedCount = usedCnt;
1059                                 respStoredMsgCnt->storedMsgCnt.totalCount = totalCnt;
1060                                 respStoredMsgCnt->result = result;
1061
1062                                 dbg("used %d, total %d, result %d",usedCnt, totalCnt,result);
1063
1064                                 pending_new = tcore_pending_new(o, 0);
1065                                 //Get all messages information
1066                                 cmd_str = g_strdup_printf("AT+CMGL=4");
1067                                 atReq = tcore_at_request_new((const char *)cmd_str, "+CMGL", TCORE_AT_MULTILINE);
1068
1069                                 dbg("cmd str is %s",cmd_str);
1070
1071                                 tcore_pending_set_request_data(pending_new, 0,atReq);
1072                                 tcore_pending_set_response_callback(pending_new, on_response_get_msg_indices, (void *)respStoredMsgCnt);
1073                                 tcore_pending_link_user_request(pending_new, ur_dup);
1074                                 tcore_pending_set_send_callback(pending_new, on_confirmation_sms_message_send, NULL);
1075                                 tcore_hal_send_request(tcore_object_get_hal(o), pending_new);
1076
1077                                 //free the consumed token
1078                                 tcore_at_tok_free(tokens);
1079
1080                                 g_free(cmd_str);
1081
1082                                 dbg("Exit");
1083                                 return;
1084                         }
1085                         //free the consumed token
1086                         if (tokens)
1087                         tcore_at_tok_free(tokens);
1088                 } else {
1089                         dbg("No data");
1090                 }
1091         } else {
1092                 err("Response NOK");
1093         }
1094         respStoredMsgCnt->result = result;
1095         tcore_user_request_send_response(ur, TRESP_SMS_GET_STORED_MSG_COUNT, sizeof(struct tresp_sms_get_storedMsgCnt), &respStoredMsgCnt);
1096
1097
1098         dbg("Exit");
1099         return;
1100 }
1101
1102 static void on_response_get_sca(TcorePending *pending, int data_len, const void *data, void *user_data)
1103 {
1104         const TcoreATResponse *at_response = data;
1105         struct tresp_sms_get_sca respGetSca;
1106         UserRequest *user_req = NULL;
1107
1108         GSList *tokens = NULL;
1109         const char *sca_tok_addr;
1110         char *gslist_line = NULL, *sca_addr = NULL, *sca_toa = NULL;
1111
1112         dbg("Entry");
1113
1114         memset(&respGetSca, 0, sizeof(respGetSca));
1115         respGetSca.result = SMS_DEVICE_FAILURE;
1116
1117         user_req = tcore_pending_ref_user_request(pending);
1118
1119         if (at_response->success) {
1120                 dbg("Response OK");
1121                 if (at_response->lines) {
1122                         gslist_line = (char *)at_response->lines->data;
1123
1124                         tokens = tcore_at_tok_new(gslist_line);
1125                         sca_tok_addr = g_slist_nth_data(tokens, 0);
1126                         sca_toa = g_slist_nth_data(tokens, 1);
1127
1128                         sca_addr = tcore_at_tok_extract(sca_tok_addr);
1129                         if ((NULL != sca_addr)
1130                                 && (NULL != sca_toa)) {
1131                                 dbg("sca_addr: [%s]. sca_toa: [%s]", sca_addr, sca_toa);
1132
1133                                 respGetSca.scaAddress.dialNumLen = strlen(sca_addr);
1134
1135                                 if (145 == atoi(sca_toa)) {
1136                                         respGetSca.scaAddress.typeOfNum = SIM_TON_INTERNATIONAL;
1137                                 } else {
1138                                         respGetSca.scaAddress.typeOfNum = SIM_TON_NATIONAL;
1139                                 }
1140
1141                                 respGetSca.scaAddress.numPlanId = 0;
1142
1143                                 memcpy(respGetSca.scaAddress.diallingNum, sca_addr, strlen(sca_addr));
1144
1145                                 dbg("len [%d], sca_addr [%s], TON [%d], NPI [%d]", respGetSca.scaAddress.dialNumLen, respGetSca.scaAddress.diallingNum, respGetSca.scaAddress.typeOfNum, respGetSca.scaAddress.numPlanId);
1146
1147                                 respGetSca.result = SMS_SENDSMS_SUCCESS;
1148                         } else {
1149                                 err("sca_addr OR sca_toa NULL");
1150                         }
1151                 } else {
1152                         dbg("NO Lines");
1153                 }
1154         } else {
1155                 dbg("Response NOK");
1156         }
1157
1158         tcore_user_request_send_response(user_req, TRESP_SMS_GET_SCA, sizeof(respGetSca), &respGetSca);
1159
1160         tcore_at_tok_free(tokens);
1161         g_free(sca_addr);
1162
1163         dbg("Exit");
1164         return;
1165 }
1166
1167 static void on_response_set_sca(TcorePending *pending, int data_len, const void *data, void *user_data)
1168 {
1169         /*
1170         Response is expected in this format
1171         OK
1172         or
1173         +CMS ERROR: <err>
1174         */
1175         UserRequest *ur;
1176         //copies the AT response data to resp
1177         const TcoreATResponse *atResp = data;
1178         struct tresp_sms_set_sca respSetSca;
1179
1180         memset(&respSetSca, 0, sizeof(struct tresp_sms_set_sca));
1181
1182         ur = tcore_pending_ref_user_request(pending);
1183         if (!ur) {
1184                 dbg("no user_request");
1185                 return;
1186         }
1187
1188         if (atResp->success > 0) {
1189                 dbg("RESPONSE OK");
1190                 respSetSca.result = SMS_SUCCESS;
1191         } else {
1192                 dbg("RESPONSE NOK");
1193                 respSetSca.result = SMS_DEVICE_FAILURE;
1194         }
1195
1196         tcore_user_request_send_response(ur, TRESP_SMS_SET_SCA, sizeof(struct tresp_sms_set_sca), &respSetSca);
1197
1198         return;
1199 }
1200
1201 static void on_response_get_cb_config(TcorePending *p, int data_len, const void *data, void *user_data)
1202 {
1203         UserRequest *ur;
1204         struct tresp_sms_get_cb_config respGetCbConfig;
1205         const TcoreATResponse *atResp = data;
1206         GSList *tokens=NULL;
1207         int i = 0, mode =0;
1208         char *pResp = NULL, *line = NULL;
1209         char delim[] = "-";
1210
1211         memset(&respGetCbConfig, 0, sizeof(struct tresp_sms_get_cb_config));
1212         respGetCbConfig.result = SMS_DEVICE_FAILURE;
1213
1214         ur = tcore_pending_ref_user_request(p);
1215         if (!ur) {
1216                 dbg("no user_request");
1217                 return;
1218         }
1219
1220         respGetCbConfig.cbConfig.net3gppType = SMS_NETTYPE_3GPP;
1221
1222         if (atResp->success) {
1223                 dbg("Response OK");
1224                 if (atResp->lines) {
1225                         line = (char*)atResp->lines->data;
1226                         if (line != NULL) {
1227                                 dbg("line is %s",line);
1228                                 tokens = tcore_at_tok_new(line);
1229                                 pResp = g_slist_nth_data(tokens, 0);
1230                                 if (pResp) {
1231                                         mode = atoi(pResp);
1232                                         respGetCbConfig.cbConfig.cbEnabled = mode;
1233
1234                                         pResp = g_slist_nth_data(tokens, 1);
1235                                         if (pResp) {
1236                                                 GSList *cb_tokens = NULL;
1237                                                 char *cb_mid_str = NULL;
1238                                                 int num_cb_tokens = 0;
1239                                                 char *mid_tok = NULL;
1240                                                 char *first_tok = NULL, *second_tok = NULL;
1241
1242                                                 // 0,1,5,320-478,922
1243                                                 cb_mid_str = util_removeQuotes(pResp);
1244                                                 cb_tokens = tcore_at_tok_new((const char *) cb_mid_str);
1245
1246                                                 g_free(cb_mid_str);
1247
1248                                                 num_cb_tokens = g_slist_length(cb_tokens);
1249                                                 dbg("num_cb_tokens = %d", num_cb_tokens);
1250
1251                                                 if (num_cb_tokens == 0) {
1252                                                         if (mode == 1) { // Enable all CBs
1253                                                                 respGetCbConfig.cbConfig.msgIdRangeCount = 1;
1254                                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.fromMsgId = 0x0000;
1255                                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.toMsgId = SMS_GSM_SMS_CBMI_LIST_SIZE_MAX + 1;
1256                                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.selected = TRUE;
1257                                                                         respGetCbConfig.result = SMS_SENDSMS_SUCCESS;
1258                                                         } else { // all CBs disabled
1259                                                                 respGetCbConfig.cbConfig.msgIdRangeCount = 0;
1260                                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.selected = FALSE;
1261                                                                 respGetCbConfig.result = SMS_SENDSMS_SUCCESS;
1262                                                         }
1263                                                 } else {
1264                                                         respGetCbConfig.cbConfig.msgIdRangeCount = 0;
1265                                                         respGetCbConfig.cbConfig.msgIDs[0].net3gpp.selected = FALSE;
1266                                                         respGetCbConfig.result = SMS_SENDSMS_SUCCESS;
1267                                                 }
1268
1269                                                 for (i = 0; i < num_cb_tokens; i++) {
1270                                                         respGetCbConfig.cbConfig.msgIDs[i].net3gpp.selected = TRUE;
1271                                                         respGetCbConfig.cbConfig.msgIdRangeCount++;
1272
1273                                                         mid_tok = tcore_at_tok_nth(cb_tokens, i);
1274                                                         first_tok = strtok(mid_tok, delim);
1275                                                         second_tok = strtok(NULL, delim);
1276
1277                                                         if ((first_tok != NULL) && (second_tok != NULL)) { // mids in range (320-478)
1278                                                                 dbg("inside if mid_range");
1279                                                                 respGetCbConfig.cbConfig.msgIDs[i].net3gpp.fromMsgId = atoi(first_tok);
1280                                                                 respGetCbConfig.cbConfig.msgIDs[i].net3gpp.toMsgId = atoi(second_tok);
1281                                                         } // single mid value (0,1,5, 922)
1282                                                         else {
1283                                                                 respGetCbConfig.cbConfig.msgIDs[i].net3gpp.fromMsgId = atoi(mid_tok);
1284                                                                 respGetCbConfig.cbConfig.msgIDs[i].net3gpp.toMsgId = atoi(mid_tok);
1285                                                         }
1286                                                 }
1287                                         }
1288                                         }else {
1289                                                 if (mode == 1) {
1290                                                 respGetCbConfig.cbConfig.msgIdRangeCount = 1;
1291                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.fromMsgId = 0x0000;
1292                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.toMsgId = SMS_GSM_SMS_CBMI_LIST_SIZE_MAX + 1;
1293                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.selected = TRUE;
1294                                                 respGetCbConfig.result = SMS_SENDSMS_SUCCESS;
1295                     } else {
1296                                                 respGetCbConfig.cbConfig.msgIdRangeCount = 0;
1297                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.selected = FALSE;
1298                                                 respGetCbConfig.result = SMS_SENDSMS_SUCCESS;
1299                                         }
1300                                 }
1301                         } else {
1302                                         dbg("line is NULL");
1303                         }
1304                 } else {
1305                         dbg("atresp->lines is NULL");
1306                 }
1307         } else {
1308                 dbg("RESPONSE NOK");
1309         }
1310
1311         tcore_user_request_send_response(ur, TRESP_SMS_GET_CB_CONFIG, sizeof(struct tresp_sms_get_cb_config), &respGetCbConfig);
1312
1313         if(tokens)
1314                 tcore_at_tok_free(tokens);
1315
1316         return;
1317 }
1318
1319 static void on_response_set_cb_config(TcorePending *pending, int data_len, const void *data, void *user_data)
1320 {
1321         /*
1322         Response is expected in this format
1323         OK
1324         or
1325         +CMS ERROR: <err>
1326         */
1327
1328         UserRequest *ur;
1329         const TcoreATResponse *resp = data;
1330         int response = 0;
1331         const char *line = NULL;
1332         GSList *tokens=NULL;
1333
1334         struct tresp_sms_set_cb_config respSetCbConfig = {0,};
1335
1336         memset(&respSetCbConfig, 0, sizeof(struct tresp_sms_set_cb_config));
1337
1338         ur = tcore_pending_ref_user_request(pending);
1339         respSetCbConfig.result = SMS_SENDSMS_SUCCESS;
1340
1341         if (resp->success > 0) {
1342                 dbg("RESPONSE OK");
1343         } else {
1344                 dbg("RESPONSE NOK");
1345                 line = (const char*)resp->final_response;
1346                 tokens = tcore_at_tok_new(line);
1347
1348                 if (g_slist_length(tokens) < 1) {
1349                         dbg("err cause not specified or string corrupted");
1350                         respSetCbConfig.result = SMS_DEVICE_FAILURE;
1351                 } else {
1352                         response = atoi(g_slist_nth_data(tokens, 0));
1353                         /* TODO: CMEE error mapping is required. */
1354                         respSetCbConfig.result = SMS_DEVICE_FAILURE;
1355                 }
1356         }
1357         if (!ur) {
1358                 dbg("no user_request");
1359                 return;
1360         }
1361
1362         tcore_user_request_send_response(ur, TRESP_SMS_SET_CB_CONFIG, sizeof(struct tresp_sms_set_cb_config), &respSetCbConfig);
1363
1364         if(tokens)
1365                 tcore_at_tok_free(tokens);
1366
1367         return;
1368 }
1369
1370 static void on_response_set_mem_status(TcorePending *p, int data_len, const void *data, void *user_data)
1371 {
1372         UserRequest *ur;
1373         struct tresp_sms_set_mem_status respSetMemStatus = {0,};
1374         const TcoreATResponse *resp = data;
1375
1376         memset(&respSetMemStatus, 0, sizeof(struct tresp_sms_set_mem_status));
1377
1378         if (resp->success > 0) {
1379                 dbg("RESPONSE OK");
1380                 respSetMemStatus.result = SMS_SENDSMS_SUCCESS;
1381         } else {
1382                 dbg("RESPONSE NOK");
1383                 respSetMemStatus.result = SMS_DEVICE_FAILURE;
1384         }
1385
1386         ur = tcore_pending_ref_user_request(p);
1387         if (!ur) {
1388                 dbg("no user_request");
1389                 return;
1390         }
1391
1392         tcore_user_request_send_response(ur, TRESP_SMS_SET_MEM_STATUS, sizeof(struct tresp_sms_set_mem_status), &respSetMemStatus);
1393
1394         return;
1395 }
1396
1397 static void on_response_set_msg_status(TcorePending *pending, int data_len, const void *data, void *user_data)
1398 {
1399         UserRequest *ur;
1400         struct tresp_sms_set_msg_status respMsgStatus = {0, };
1401         const TcoreATResponse *atResp = data;
1402         int response = 0, sw1 = 0, sw2 = 0;
1403         const char *line = NULL;
1404         char *pResp = NULL;
1405         GSList *tokens = NULL;
1406
1407         dbg("Entry");
1408
1409         memset(&respMsgStatus, 0, sizeof(struct tresp_sms_set_msg_status));
1410         respMsgStatus.result = SMS_DEVICE_FAILURE;
1411
1412         ur = tcore_pending_ref_user_request(pending);
1413
1414         if (atResp->success > 0) {
1415                 dbg("RESPONSE OK");
1416
1417                 if (atResp->lines) {
1418                         line = (const char *) atResp->lines->data;
1419                         tokens = tcore_at_tok_new(line);
1420                         pResp = g_slist_nth_data(tokens, 0);
1421                         if (pResp != NULL) {
1422                                 sw1 = atoi(pResp);
1423                         } else {
1424                                 dbg("sw1 is NULL");
1425                         }
1426                         pResp = g_slist_nth_data(tokens, 1);
1427                         if (pResp != NULL) {
1428                                 sw2 = atoi(pResp);
1429                                 if ((sw1 == AT_SW1_SUCCESS) && (sw2 == 0)) {
1430                                         respMsgStatus.result = SMS_SENDSMS_SUCCESS;
1431                                 }
1432                         } else {
1433                                 dbg("sw2 is NULL");
1434                         }
1435                         pResp = g_slist_nth_data(tokens, 3);
1436
1437                         if (pResp != NULL) {
1438                                 response = atoi(pResp);
1439                                 dbg("response is %s", response);
1440                         }
1441                 } else {
1442                         dbg("No lines");
1443                 }
1444         } else {
1445                 dbg("RESPONSE NOK");
1446         }
1447
1448         tcore_user_request_send_response(ur, TRESP_SMS_SET_MSG_STATUS , sizeof(struct tresp_sms_set_msg_status), &respMsgStatus);
1449
1450         if(tokens)
1451                 tcore_at_tok_free(tokens);
1452
1453         dbg("Exit");
1454         return;
1455 }
1456
1457 static void on_response_get_sms_params(TcorePending *pending, int data_len, const void *data, void *user_data)
1458 {
1459         UserRequest *ur;
1460         struct tresp_sms_get_params respGetParams ;
1461         const TcoreATResponse *atResp = data;
1462         int sw1 = 0, sw2 = 0;
1463         const char *line = NULL;
1464         char *pResp = NULL;
1465         GSList *tokens=NULL;
1466         char *hexData = NULL;
1467     char *recordData = NULL;
1468     int i = 0;
1469
1470         memset(&respGetParams, 0, sizeof(struct tresp_sms_get_params));
1471         respGetParams.result = SMS_DEVICE_FAILURE;
1472
1473         ur = tcore_pending_ref_user_request(pending);
1474
1475         if (atResp->success > 0) {
1476                 dbg("RESPONSE OK");
1477
1478                 if (atResp->lines) {
1479                         line = (const char *) atResp->lines->data;
1480                         tokens = tcore_at_tok_new(line);
1481                         pResp = g_slist_nth_data(tokens, 0);
1482                         if (pResp != NULL) {
1483                                 sw1 = atoi(pResp);
1484                                 dbg("sw1 is %d", sw1);
1485                         } else {
1486                                 dbg("sw1 is NULL");
1487                         }
1488                         pResp = g_slist_nth_data(tokens, 1);
1489                         if (pResp != NULL) {
1490                                 sw2 = atoi(pResp);
1491                                 dbg("sw2 is %d", sw2);
1492                                 if ((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
1493                                         respGetParams.result = SMS_SENDSMS_SUCCESS;
1494                                 }
1495                         } else {
1496                                 dbg("sw2 is NULL");
1497                         }
1498                         pResp = g_slist_nth_data(tokens, 2);
1499                         if (pResp != NULL) {
1500                                 hexData = util_removeQuotes(pResp);
1501
1502                                 recordData = util_hexStringToBytes(hexData);
1503                                 util_hex_dump("    ", strlen(hexData) / 2, recordData);
1504
1505                                 respGetParams.paramsInfo.recordLen = strlen(hexData) / 2;
1506
1507                                 util_sms_decode_smsParameters((unsigned char *) recordData, strlen(hexData) / 2, &(respGetParams.paramsInfo));
1508                                 respGetParams.result = SMS_SENDSMS_SUCCESS;
1509
1510                                 for (i = 0; i < (int) respGetParams.paramsInfo.tpSvcCntrAddr.dialNumLen; i++)
1511                                         dbg("SCAddr = %d [%02x]", i, respGetParams.paramsInfo.tpSvcCntrAddr.diallingNum[i]);
1512
1513                                 g_free(recordData);
1514                                 g_free(hexData);
1515                         } else {
1516                                 dbg("No response");
1517                         }
1518                         tcore_at_tok_free(tokens);
1519                 }
1520         } else {
1521                 dbg("RESPONSE NOK");
1522         }
1523
1524         tcore_user_request_send_response(ur, TRESP_SMS_GET_PARAMS, sizeof(struct tresp_sms_get_params), &respGetParams);
1525
1526         dbg("Exit");
1527         return;
1528 }
1529
1530 static void on_response_set_sms_params(TcorePending *pending, int data_len, const void *data, void *user_data)
1531 {
1532         UserRequest *ur;
1533         struct tresp_sms_set_params respSetParams = {0, };
1534         const TcoreATResponse *atResp = data;
1535         int sw1 =0 , sw2 = 0;
1536         const char *line = NULL;
1537         char *pResp = NULL;
1538         GSList *tokens=NULL;
1539
1540
1541         memset(&respSetParams, 0, sizeof(struct tresp_sms_set_params));
1542         ur = tcore_pending_ref_user_request(pending);
1543
1544         respSetParams.result = SMS_DEVICE_FAILURE;
1545
1546         if (atResp->success > 0) {
1547                 dbg("RESPONSE OK");
1548
1549                 if (atResp->lines) {
1550                         line = (const char *) atResp->lines->data;
1551                         tokens = tcore_at_tok_new(line);
1552                         pResp = g_slist_nth_data(tokens, 0);
1553                         if (pResp != NULL) {
1554                                 sw1 = atoi(pResp);
1555                         } else {
1556                                 dbg("sw1 is NULL");
1557                         }
1558
1559                         pResp = g_slist_nth_data(tokens, 1);
1560                         if (pResp != NULL) {
1561                                 sw2 = atoi(pResp);
1562                                 if (((sw1 == AT_SW1_SUCCESS) && (sw2 == AT_SW2_SUCCESS)) || (sw1 == 0x91)) {
1563                                         respSetParams.result = SMS_SENDSMS_SUCCESS;
1564                                 }
1565                         } else {
1566                                 dbg("sw2 is NULL");
1567                         }
1568                 } else {
1569                         dbg("No lines");
1570                 }
1571         } else {
1572                 dbg("RESPONSE NOK");
1573         }
1574
1575         tcore_user_request_send_response(ur, TRESP_SMS_SET_PARAMS , sizeof(struct tresp_sms_set_params), &respSetParams);
1576
1577         if(tokens)
1578                 tcore_at_tok_free(tokens);
1579
1580         dbg("Exit");
1581         return;
1582 }
1583
1584 static void on_response_get_paramcnt(TcorePending *p, int data_len, const void *data, void *user_data)
1585 {
1586         UserRequest *ur = NULL;
1587         struct tresp_sms_get_paramcnt respGetParamCnt = {0, };
1588         const TcoreATResponse *atResp = data;
1589         char *line = NULL , *pResp = NULL;
1590         int sw1 = 0 , sw2 = 0, *smsp_record_len = NULL;
1591         int sim_type = 0;
1592         GSList *tokens=NULL;
1593         CoreObject *co_sim = NULL;  //need this to get the sim type GSM/USIM
1594         TcorePlugin *plugin = NULL;
1595
1596         dbg("Entry");
1597
1598         ur = tcore_pending_ref_user_request(p);
1599         respGetParamCnt.result = SMS_DEVICE_FAILURE;
1600
1601         if (atResp->success > 0) {
1602                 dbg("RESPONSE OK");
1603
1604                 if (atResp->lines) {
1605                         line = (char *) atResp->lines->data;
1606
1607                         dbg("line is %s", line);
1608
1609                         tokens = tcore_at_tok_new(line);
1610                         pResp = g_slist_nth_data(tokens, 0);
1611                         if (pResp != NULL) {
1612                                 sw1 = atoi(pResp);
1613                         } else {
1614                                 dbg("sw1 is NULL");
1615                         }
1616                         pResp = g_slist_nth_data(tokens, 1);
1617                         if (pResp != NULL) {
1618                                 sw2 = atoi(pResp);
1619                                 if ((sw1 == 144) && (sw2 == 0)) {
1620                                         respGetParamCnt.result = SMS_SENDSMS_SUCCESS;
1621                                 }
1622                         } else {
1623                                 dbg("sw2 is NULL");
1624                         }
1625                         pResp = g_slist_nth_data(tokens, 2);
1626                         if (pResp != NULL) {
1627                                 char *hexData = NULL;
1628                                 char *recordData = NULL;
1629                                 hexData = util_removeQuotes(pResp);
1630
1631                                 /*1. SIM access success case*/
1632                                 if ((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
1633                                         unsigned char tag_len = 0; /*   1 or 2 bytes ??? */
1634                                         int record_len = 0;
1635                                         char num_of_records = 0;
1636                                         unsigned char file_id_len = 0;
1637                                         unsigned short file_id = 0;
1638                                         unsigned short file_size = 0;
1639                                         unsigned short file_type = 0;
1640                                         unsigned short arr_file_id = 0;
1641                                         int arr_file_id_rec_num = 0;
1642
1643                                         /*      handling only last 3 bits */
1644                                         unsigned char file_type_tag = 0x07;
1645                                         unsigned char *ptr_data;
1646
1647                                         recordData = util_hexStringToBytes(hexData);
1648                                         util_hex_dump("    ", strlen(hexData)/2, recordData);
1649
1650                                         ptr_data = (unsigned char *)recordData;
1651
1652                                         co_sim = tcore_plugin_ref_core_object(tcore_pending_ref_plugin(p), CORE_OBJECT_TYPE_SIM);
1653                                         sim_type = tcore_sim_get_type(co_sim);
1654                                         dbg("sim type is %d",sim_type);
1655
1656                                         if (sim_type ==  SIM_TYPE_USIM) {
1657                                                 /*
1658                                                  ETSI TS 102 221 v7.9.0
1659                                                         - Response Data
1660                                                          '62'   FCP template tag
1661                                                          - Response for an EF
1662                                                          '82'   M       File Descriptor
1663                                                          '83'   M       File Identifier
1664                                                         'A5'    O       Proprietary information
1665                                                          '8A'   M       Life Cycle Status Integer
1666                                                          '8B', '8C' or 'AB'     C1      Security attributes
1667                                                         '80'    M       File size
1668                                                          '81'   O       Total file size
1669                                                          '88'   O       Short File Identifier (SFI)
1670                                                 */
1671
1672                                                 /* rsim.res_len  has complete data length received  */
1673
1674                                                 /* FCP template tag - File Control Parameters tag*/
1675                                                 if (*ptr_data == 0x62) {
1676                                                         /* parse complete FCP tag*/
1677                                                         /* increment to next byte */
1678                                                         ptr_data++;
1679                                                         tag_len = *ptr_data++;
1680                                                         /* FCP file descriptor - file type, accessibility, DF, ADF etc*/
1681                                                         if (*ptr_data == 0x82) {
1682                                                                         /* increment to next byte */
1683                                                                         ptr_data++;
1684                                                                         /*2 or 5 value*/
1685                                                                         ptr_data++;
1686                                                         /*      unsigned char file_desc_len = *ptr_data++;*/
1687                                                         /*      dbg("file descriptor length: [%d]", file_desc_len);*/
1688                                                         /* TBD:  currently capture only file type : ignore sharable, non sharable, working, internal etc*/
1689                                                         /* consider only last 3 bits*/
1690                                                         file_type_tag = file_type_tag & (*ptr_data);
1691
1692                                                         switch (file_type_tag) {
1693                                                                 /* increment to next byte */
1694                                                                 ptr_data++;
1695
1696                                                                 case 0x1:
1697                                                                         dbg("Getting FileType: [Transparent file type]");
1698                                                                         /* increment to next byte */
1699                                                                         ptr_data++;
1700                                                                         file_type = 0x01;       //SIM_FTYPE_TRANSPARENT
1701                                                                         /*      data coding byte - value 21 */
1702                                                                         ptr_data++;
1703                                                                         break;
1704
1705                                                                 case 0x2:
1706                                                                         dbg("Getting FileType: [Linear fixed file type]");
1707                                                                         /* increment to next byte */
1708                                                                         ptr_data++;
1709                                                                         /*      data coding byte - value 21 */
1710                                                                         ptr_data++;
1711                                                                         /*      2bytes */
1712                                                                         memcpy(&record_len, ptr_data, 2);
1713                                                                         /* swap bytes */
1714                                                                         record_len = SMS_SWAPBYTES16(record_len);
1715                                                                         ptr_data = ptr_data + 2;
1716                                                                         num_of_records = *ptr_data++;
1717                                                                         /* Data lossy conversation from enum (int) to unsigned char */
1718                                                                         file_type = 0x02;       // SIM_FTYPE_LINEAR_FIXED
1719                                                                         break;
1720
1721                                                                 case 0x6:
1722                                                                         dbg(" Cyclic fixed file type");
1723                                                                         /* increment to next byte */
1724                                                                         ptr_data++;
1725                                                                         /*      data coding byte - value 21 */
1726                                                                         ptr_data++;
1727                                                                         /*      2bytes */
1728                                                                         memcpy(&record_len, ptr_data, 2);
1729                                                                         /* swap bytes  */
1730                                                                         record_len = SMS_SWAPBYTES16(record_len);
1731                                                                         ptr_data = ptr_data + 2;
1732                                                                         num_of_records = *ptr_data++;
1733                                                                         file_type = 0x04;       //SIM_FTYPE_CYCLIC
1734                                                                         break;
1735
1736                                                                 default:
1737                                                                         dbg("not handled file type [0x%x]", *ptr_data);
1738                                                                         break;
1739                                                                 }
1740                                                         } else {
1741                                                                 dbg("INVALID FCP received - DEbug!");
1742                                                                 g_free(hexData);
1743                                                                 g_free(recordData);
1744                                                                 tcore_at_tok_free(tokens);
1745                                                                 return;
1746                                                         }
1747
1748                                                         /*File identifier - file id?? */ // 0x84,0x85,0x86 etc are currently ignored and not handled
1749                                                         if (*ptr_data == 0x83) {
1750                                                                 /* increment to next byte */
1751                                                                 ptr_data++;
1752                                                                 file_id_len = *ptr_data++;
1753                                                                 memcpy(&file_id, ptr_data, file_id_len);
1754                                                                 /* swap bytes    */
1755                                                                 file_id = SMS_SWAPBYTES16(file_id);
1756                                                                 ptr_data = ptr_data + 2;
1757                                                                 dbg("Getting FileID=[0x%x]", file_id);
1758                                                         } else {
1759                                                                 dbg("INVALID FCP received - DEbug!");
1760                                                                 g_free(hexData);
1761                                                                 g_free(recordData);
1762                                                                 tcore_at_tok_free(tokens);
1763                                                                 return;
1764                                                         }
1765
1766                                                         /*      proprietary information  */
1767                                                         if (*ptr_data == 0xA5) {
1768                                                                 unsigned short prop_len;
1769                                                                 /* increment to next byte */
1770                                                                 ptr_data++;
1771                                                                 /* length */
1772                                                                 prop_len = *ptr_data;
1773                                                                 /* skip data */
1774                                                                 ptr_data = ptr_data + prop_len + 1;
1775                                                         } else {
1776                                                                 dbg("INVALID FCP received - DEbug!");
1777                                                         }
1778
1779                                                         /* life cycle status integer [8A][length:0x01][status]*/
1780                                                         /*
1781                                                          status info b8~b1
1782                                                          00000000 : No information given
1783                                                          00000001 : creation state
1784                                                          00000011 : initialization state
1785                                                          000001-1 : operation state -activated
1786                                                          000001-0 : operation state -deactivated
1787                                                          000011-- : Termination state
1788                                                          b8~b5 !=0, b4~b1=X : Proprietary
1789                                                          Any other value : RFU
1790                                                          */
1791                                                         if (*ptr_data == 0x8A) {
1792                                                                 /* increment to next byte */
1793                                                                 ptr_data++;
1794                                                                 /* length - value 1 */
1795                                                                 ptr_data++;
1796
1797                                                                 switch (*ptr_data) {
1798                                                                         case 0x04:
1799                                                                         case 0x06:
1800                                                                                 dbg("[RX] Operation State: DEACTIVATED");
1801                                                                                 ptr_data++;
1802                                                                                 break;
1803
1804                                                                         case 0x05:
1805                                                                         case 0x07:
1806                                                                                 dbg("[RX] Operation State: ACTIVATED");
1807                                                                                 ptr_data++;
1808                                                                                 break;
1809
1810                                                                         default:
1811                                                                                 dbg("[RX] DEBUG! LIFE CYCLE STATUS: [0x%x]",*ptr_data);
1812                                                                                 ptr_data++;
1813                                                                                 break;
1814                                                                 }
1815                                                         }
1816
1817                                                         /* related to security attributes : currently not handled*/
1818                                                         if (*ptr_data == 0x86 || *ptr_data == 0x8B || *ptr_data == 0x8C || *ptr_data == 0xAB) {
1819                                                                 /* increment to next byte */
1820                                                                 ptr_data++;
1821                                                                 /* if tag length is 3 */
1822                                                                 if (*ptr_data == 0x03) {
1823                                                                         /* increment to next byte */
1824                                                                         ptr_data++;
1825                                                                         /* EFARR file id */
1826                                                                         memcpy(&arr_file_id, ptr_data, 2);
1827                                                                         /* swap byes */
1828                                                                         arr_file_id = SMS_SWAPBYTES16(arr_file_id);
1829                                                                         ptr_data = ptr_data + 2;
1830                                                                         arr_file_id_rec_num = *ptr_data++;
1831                                                                 } else {
1832                                                                         /* if tag length is not 3 */
1833                                                                         /* ignoring bytes       */
1834                                                                         //      ptr_data = ptr_data + 4;
1835                                                                         dbg("Useless security attributes, so jump to next tag");
1836                                                                         ptr_data = ptr_data + (*ptr_data + 1);
1837                                                                 }
1838                                                         } else {
1839                                                                 dbg("INVALID FCP received[0x%x] - DEbug!", *ptr_data);
1840                                                                 g_free(hexData);
1841                                                                 g_free(recordData);
1842                                                                 tcore_at_tok_free(tokens);
1843                                                                 return;
1844                                                         }
1845
1846                                                         dbg("Current ptr_data value is [%x]", *ptr_data);
1847
1848                                                         /* file size excluding structural info*/
1849                                                         if (*ptr_data == 0x80) {
1850                                                                 /* for EF file size is body of file and for Linear or cyclic it is
1851                                                                  * number of recXsizeof(one record)
1852                                                                  */
1853                                                                 /* increment to next byte */
1854                                                                 ptr_data++;
1855                                                                 /* length is 1 byte - value is 2 bytes or more */
1856                                                                 ptr_data++;
1857                                                                 memcpy(&file_size, ptr_data, 2);
1858                                                                 /* swap bytes */
1859                                                                 file_size = SMS_SWAPBYTES16(file_size);
1860                                                                 ptr_data = ptr_data + 2;
1861                                                         } else {
1862                                                                 dbg("INVALID FCP received - DEbug!");
1863                                                                 g_free(hexData);
1864                                                                 g_free(recordData);
1865                                                                 tcore_at_tok_free(tokens);
1866                                                                 return;
1867                                                         }
1868
1869                                                         /* total file size including structural info*/
1870                                                         if (*ptr_data == 0x81) {
1871                                                                 int len;
1872                                                                 /* increment to next byte */
1873                                                                 ptr_data++;
1874                                                                 /* length */
1875                                                                 len = *ptr_data;
1876                                                                 /* ignored bytes */
1877                                                                 ptr_data = ptr_data + 3;
1878                                                         } else {
1879                                                                 dbg("INVALID FCP received - DEbug!");
1880                                                                 /* 0x81 is optional tag?? check out! so do not return -1 from here! */
1881                                                                 /* return -1; */
1882                                                         }
1883                                                         /*short file identifier ignored*/
1884                                                         if (*ptr_data == 0x88) {
1885                                                                 dbg("0x88: Do Nothing");
1886                                                                 /*DO NOTHING*/
1887                                                         }
1888                                                 } else {
1889                                                         dbg("INVALID FCP received - DEbug!");
1890                                                         g_free(hexData);
1891                                                         g_free(recordData);
1892                                                         tcore_at_tok_free(tokens);
1893                                                         return;
1894                                                 }
1895                                         } else if (sim_type == SIM_TYPE_GSM) {
1896                                                 unsigned char gsm_specific_file_data_len = 0;
1897                                                 /*      ignore RFU byte1 and byte2 */
1898                                                 ptr_data++;
1899                                                 ptr_data++;
1900                                                 /*      file size */
1901                                                 //file_size = p_info->response_len;
1902                                                 memcpy(&file_size, ptr_data, 2);
1903                                                 /* swap bytes */
1904                                                 file_size = SMS_SWAPBYTES16(file_size);
1905                                                 /*      parsed file size */
1906                                                 ptr_data = ptr_data + 2;
1907                                                 /*  file id  */
1908                                                 memcpy(&file_id, ptr_data, 2);
1909                                                 file_id = SMS_SWAPBYTES16(file_id);
1910                                                 dbg(" FILE id --> [%x]", file_id);
1911                                                 ptr_data = ptr_data + 2;
1912                                                 /* save file type - transparent, linear fixed or cyclic */
1913                                                 file_type_tag = (*(ptr_data + 7));
1914
1915                                                 switch (*ptr_data) {
1916                                                         case 0x0:
1917                                                                 /* RFU file type */
1918                                                                 dbg(" RFU file type- not handled - Debug!");
1919                                                                 break;
1920
1921                                                         case 0x1:
1922                                                                 /* MF file type */
1923                                                                 dbg(" MF file type - not handled - Debug!");
1924                                                                 break;
1925
1926                                                         case 0x2:
1927                                                                 /* DF file type */
1928                                                                 dbg(" DF file type - not handled - Debug!");
1929                                                                 break;
1930
1931                                                         case 0x4:
1932                                                                 /* EF file type */
1933                                                                 dbg(" EF file type [%d] ", file_type_tag);
1934                                                                 /*      increment to next byte */
1935                                                                 ptr_data++;
1936
1937                                                                 if (file_type_tag == 0x00 || file_type_tag == 0x01) {
1938                                                                         /* increament to next byte as this byte is RFU */
1939                                                                         ptr_data++;
1940                                                                         file_type =
1941                                                                                         (file_type_tag == 0x00) ? 0x01 : 0x02; // SIM_FTYPE_TRANSPARENT:SIM_FTYPE_LINEAR_FIXED;
1942                                                                 } else {
1943                                                                         /* increment to next byte */
1944                                                                         ptr_data++;
1945                                                                         /*      For a cyclic EF all bits except bit 7 are RFU; b7=1 indicates that */
1946                                                                         /* the INCREASE command is allowed on the selected cyclic file. */
1947                                                                         file_type = 0x04;       // SIM_FTYPE_CYCLIC;
1948                                                                 }
1949                                                                 /* bytes 9 to 11 give SIM file access conditions */
1950                                                                 ptr_data++;
1951                                                                 /* byte 10 has one nibble that is RF U and another for INCREASE which is not used currently */
1952                                                                 ptr_data++;
1953                                                                 /* byte 11 is invalidate and rehabilate nibbles */
1954                                                                 ptr_data++;
1955                                                                 /* byte 12 - file status */
1956                                                                 ptr_data++;
1957                                                                 /* byte 13 - GSM specific data */
1958                                                                 gsm_specific_file_data_len = *ptr_data;
1959                                                                 ptr_data++;
1960                                                                 /*      byte 14 - structure of EF - transparent or linear or cyclic , already saved above */
1961                                                                 ptr_data++;
1962                                                                 /* byte 15 - length of record for linear and cyclic , for transparent it is set to 0x00. */
1963                                                                 record_len = *ptr_data;
1964                                                                 dbg("record length[%d], file size[%d]", record_len, file_size);
1965
1966                                                                 if (record_len != 0)
1967                                                                         num_of_records = (file_size / record_len);
1968
1969                                                                 dbg("Number of records [%d]", num_of_records);
1970                                                                 break;
1971
1972                                                         default:
1973                                                                 dbg(" not handled file type");
1974                                                                 break;
1975                                                 }
1976                                         } else {
1977                                                 dbg(" Card Type - UNKNOWN  [%d]", sim_type);
1978                                         }
1979
1980                                         dbg("EF[0x%x] size[%ld] Type[0x%x] NumOfRecords[%ld] RecordLen[%ld]", file_id, file_size, file_type, num_of_records, record_len);
1981
1982                                         respGetParamCnt.recordCount = num_of_records;
1983                                         respGetParamCnt.result = SMS_SUCCESS;
1984
1985                                         //TO Store smsp record length in the property
1986                                         plugin = tcore_pending_ref_plugin(p);
1987                                         smsp_record_len = tcore_plugin_ref_property(plugin, "SMSPRECORDLEN");
1988                                         memcpy(smsp_record_len, &record_len, sizeof(int));
1989
1990                                         g_free(recordData);
1991                                         g_free(hexData);
1992                                 } else {
1993                                         /*2. SIM access fail case*/
1994                                         dbg("SIM access fail");
1995                                         respGetParamCnt.result = SMS_UNKNOWN;
1996                                 }
1997                         } else {
1998                                 dbg("presp is NULL");
1999                         }
2000                 } else {
2001                         dbg("line is blank");
2002                 }
2003         } else {
2004                 dbg("RESPONSE NOK");
2005         }
2006
2007         tcore_user_request_send_response(ur, TRESP_SMS_GET_PARAMCNT, sizeof(struct tresp_sms_get_paramcnt), &respGetParamCnt);
2008
2009         if(tokens)
2010                 tcore_at_tok_free(tokens);
2011
2012         dbg("Exit");
2013         return;
2014 }
2015
2016 static void _response_get_efsms_data(TcorePending *p, int data_len, const void *data, void *user_data)
2017 {
2018         UserRequest *ur = NULL;
2019         UserRequest *dup_ur = NULL;
2020         struct tresp_sms_set_msg_status resp_msg_status = {0,};
2021         const struct treq_sms_set_msg_status *req_msg_status = NULL ;
2022
2023         const TcoreATResponse *resp = data;
2024         char *encoded_data = NULL;
2025         char msg_status = 0;
2026         char *pResp = NULL;
2027         GSList *tokens=NULL;
2028         const char *line = NULL;
2029         int sw1 = 0;
2030         int sw2 = 0;
2031
2032         TcoreHal *hal = NULL;
2033         TcoreATRequest *atreq = NULL;
2034         TcorePending *pending = NULL;
2035         gchar *cmd_str = NULL;
2036
2037         ur = tcore_pending_ref_user_request(p);
2038
2039         req_msg_status = tcore_user_request_ref_data(ur, NULL);
2040
2041         resp_msg_status.result = SMS_DEVICE_FAILURE;
2042
2043         hal = tcore_object_get_hal(tcore_pending_ref_core_object(pending));
2044         dbg("msgStatus: [%x], index [%x]", req_msg_status->msgStatus, req_msg_status->index);
2045
2046         if (resp->success <= 0) {
2047                 goto OUT;
2048         }
2049
2050         {
2051                 dbg("RESPONSE OK");
2052                 if (resp->lines) {
2053                         line = (const char *) resp->lines->data;
2054                         tokens = tcore_at_tok_new(line);
2055                         if (g_slist_length(tokens) != 3) {
2056                                 msg("invalid message");
2057                                 goto OUT;
2058                         }
2059                 }
2060                 sw1 = atoi(g_slist_nth_data(tokens, 0));
2061                 sw2 = atoi(g_slist_nth_data(tokens, 1));
2062                 pResp = g_slist_nth_data(tokens, 2);
2063
2064                 if ((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
2065                         switch (req_msg_status->msgStatus) {
2066                                 case SMS_STATUS_READ:
2067                                         msg_status = 0x01;
2068                                         break;
2069
2070                                 case SMS_STATUS_UNREAD:
2071                                         msg_status = 0x03;
2072                                         break;
2073
2074                                 case SMS_STATUS_UNSENT:
2075                                         msg_status = 0x07;
2076                                         break;
2077
2078                                 case SMS_STATUS_SENT:
2079                                         msg_status = 0x05;
2080                                         break;
2081
2082                                 case SMS_STATUS_DELIVERED:
2083                                         msg_status = 0x1D;
2084                                         break;
2085
2086                                 case SMS_STATUS_DELIVERY_UNCONFIRMED:
2087                                         msg_status = 0xD;
2088                                         break;
2089
2090                                 case SMS_STATUS_MESSAGE_REPLACED:
2091                                 case SMS_STATUS_RESERVED:
2092                                 default:
2093                                         msg_status = 0x03;
2094                                         break;
2095                         }
2096
2097                         encoded_data = util_removeQuotes(pResp);
2098
2099                         //overwrite Status byte information
2100                         util_byte_to_hex((const char *)&msg_status, (char *)encoded_data, 1);
2101
2102                         //Update EF-SMS with just status byte overwritten, rest 175 bytes are same as received in read information
2103                         cmd_str = g_strdup_printf("AT+CRSM=220,28476,%d, 4, %d, \"%s\"", (req_msg_status->index+1), PDU_LEN_MAX, encoded_data);
2104                         atreq = tcore_at_request_new((const char *)cmd_str, "+CRSM", TCORE_AT_SINGLELINE);
2105                         pending = tcore_pending_new(tcore_pending_ref_core_object(pending), 0);
2106                         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2107                                 err("Out of memory. Unable to proceed");
2108                                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2109
2110                                 //free memory we own
2111                                 g_free(cmd_str);
2112                                 g_free(encoded_data);
2113                                 util_sms_free_memory(atreq);
2114                                 util_sms_free_memory(pending);
2115
2116                                 goto OUT;
2117                         }
2118
2119                         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2120
2121                         dup_ur = tcore_user_request_ref(ur);
2122
2123                         tcore_pending_set_request_data(pending, 0, atreq);
2124                         tcore_pending_set_response_callback(pending, on_response_set_msg_status, NULL);
2125                         tcore_pending_link_user_request(pending, dup_ur);
2126                         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2127                         tcore_hal_send_request(hal, pending);
2128
2129                         g_free(cmd_str);
2130                         g_free(encoded_data);
2131                 }
2132         }
2133
2134 OUT:
2135         if(tokens)
2136                 tcore_at_tok_free(tokens);
2137
2138         tcore_user_request_send_response(ur, TRESP_SMS_SET_MSG_STATUS , sizeof(struct tresp_sms_set_msg_status), &msg_status);
2139
2140         dbg("Exit");
2141
2142         return;
2143 }
2144
2145 /*=============================================================
2146                                                         Requests
2147 ==============================================================*/
2148 static TReturn send_umts_msg(CoreObject *co_sms, UserRequest *ur)
2149 {
2150         const struct treq_sms_send_umts_msg *send_msg;
2151         const unsigned char *tpdu_byte_data, *sca_byte_data;
2152         int tpdu_byte_len, pdu_byte_len;
2153         char buf[HEX_PDU_LEN_MAX];
2154         char pdu[PDU_LEN_MAX];
2155         char *cmd_str;
2156         int pdu_hex_len, mms;
2157         TReturn ret;
2158
2159         dbg("Enter");
2160
2161         send_msg = tcore_user_request_ref_data(ur, NULL);
2162
2163         tpdu_byte_data = send_msg->msgDataPackage.tpduData;
2164         sca_byte_data = send_msg->msgDataPackage.sca;
2165
2166
2167         /* TPDU length is in byte */
2168         tpdu_byte_len = send_msg->msgDataPackage.msgLength;
2169
2170         /* Use same Radio Resource Channel */
2171         mms = send_msg->more;
2172
2173         dbg("TDPU length: [%d]", tpdu_byte_len);
2174         dbg("SCA semi-octet length: [%d]", sca_byte_data[0]);
2175
2176         /* Prepare PDU for hex encoding */
2177         pdu_byte_len = tcore_util_pdu_encode(sca_byte_data, tpdu_byte_data,
2178                                                 tpdu_byte_len, pdu);
2179
2180         pdu_hex_len = (int) tcore_util_encode_hex((unsigned char *) pdu,
2181                                                 pdu_byte_len, buf);
2182
2183         dbg("PDU hexadecimal length: [%d]", pdu_hex_len);
2184
2185         if (mms > 0) {
2186                 cmd_str = g_strdup_printf("AT+CMMS=%d", mms);
2187
2188                 ret = tcore_prepare_and_send_at_request(co_sms, cmd_str, NULL,
2189                                         TCORE_AT_NO_RESULT, NULL, NULL, NULL,
2190                                         on_confirmation_sms_message_send,
2191                                         NULL);
2192                 if (ret != TCORE_RETURN_SUCCESS) {
2193                         err("Failed to prepare and send AT request");
2194                         goto error;
2195                 }
2196
2197                 g_free(cmd_str);
2198         }
2199
2200         cmd_str = g_strdup_printf("AT+CMGS=%d\r%s\x1A", tpdu_byte_len, buf);
2201
2202         ret = tcore_prepare_and_send_at_request(co_sms, cmd_str, "+CMGS:",
2203                                 TCORE_AT_SINGLELINE, ur,
2204                                 on_response_send_umts_msg, NULL,
2205                                 on_confirmation_sms_message_send, NULL);
2206         if (ret != TCORE_RETURN_SUCCESS)
2207                 err("Failed to prepare and send AT request");
2208
2209 error:
2210         g_free(cmd_str);
2211
2212         dbg("Exit");
2213
2214         return ret;
2215 }
2216
2217 static TReturn read_msg(CoreObject *obj, UserRequest *ur)
2218 {
2219         gchar *cmd_str = NULL;
2220         TcoreHal *hal = NULL;
2221         TcoreATRequest *atreq = NULL;
2222         TcorePending *pending = NULL;
2223         const struct treq_sms_read_msg *readMsg = NULL;
2224
2225         dbg("Entry");
2226
2227         readMsg = tcore_user_request_ref_data(ur, NULL);
2228         hal = tcore_object_get_hal(obj);
2229         if (NULL == readMsg || NULL == hal) {
2230                 err("NULL input. Unable to proceed");
2231                 dbg("readMsg: [%p], hal: [%p]", readMsg, hal);
2232
2233                 dbg("Exit");
2234                 return TCORE_RETURN_EINVAL;
2235         }
2236
2237         if(FALSE == tcore_hal_get_power_state(hal)){
2238                 dbg("cp not ready/n");
2239                 return TCORE_RETURN_ENOSYS;
2240         }
2241         dbg("index: [%d]", readMsg->index);
2242
2243         cmd_str = g_strdup_printf("AT+CMGR=%d", readMsg->index); //IMC index is one ahead of TAPI
2244         atreq = tcore_at_request_new((const char *)cmd_str, "+CMGR", TCORE_AT_PDU);
2245         pending = tcore_pending_new(obj, 0);
2246
2247         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2248                 err("Out of memory. Unable to proceed");
2249                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2250
2251                 //free memory we own
2252                 g_free(cmd_str);
2253                 util_sms_free_memory(atreq);
2254                 util_sms_free_memory(pending);
2255
2256                 dbg("Exit");
2257                 return TCORE_RETURN_ENOMEM;
2258         }
2259
2260         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2261
2262         tcore_pending_set_request_data(pending, 0, atreq);
2263         tcore_pending_set_response_callback(pending, on_response_read_msg, (void *)(uintptr_t)(readMsg->index)); //storing index as user data for response
2264         tcore_pending_link_user_request(pending, ur);
2265         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2266         tcore_hal_send_request(hal, pending);
2267
2268         g_free(cmd_str);
2269
2270         dbg("Exit");
2271         return TCORE_RETURN_SUCCESS;
2272 }
2273
2274 static TReturn save_msg(CoreObject *obj, UserRequest *ur)
2275 {
2276         gchar *cmd_str = NULL;
2277         TcoreHal *hal = NULL;
2278         TcoreATRequest *atreq = NULL;
2279         TcorePending *pending = NULL;
2280         const struct treq_sms_save_msg *saveMsg = NULL;
2281         int ScLength = 0, pdu_len = 0, stat = 0;
2282         char buf[2*(SMS_SMSP_ADDRESS_LEN+SMS_SMDATA_SIZE_MAX)+1] = {0};
2283         char *hex_pdu = NULL;
2284
2285         dbg("Entry");
2286
2287         saveMsg = tcore_user_request_ref_data(ur, NULL);
2288         hal = tcore_object_get_hal(obj);
2289         if (NULL == saveMsg || NULL == hal) {
2290                 err("NULL input. Unable to proceed");
2291                 dbg("saveMsg: [%p], hal: [%p]", saveMsg, hal);
2292
2293                 dbg("Exit");
2294                 return TCORE_RETURN_EINVAL;
2295         }
2296         if(FALSE == tcore_hal_get_power_state(hal)){
2297                 dbg("cp not ready/n");
2298                 return TCORE_RETURN_ENOSYS;
2299         }
2300
2301         dbg("msgStatus: %x, msgLength: [%d]", saveMsg->msgStatus, saveMsg->msgDataPackage.msgLength);
2302         util_hex_dump("    ", (SMS_SMDATA_SIZE_MAX+1), (void *)saveMsg->msgDataPackage.tpduData);
2303         util_hex_dump("    ", SMS_SMSP_ADDRESS_LEN, (void *)saveMsg->msgDataPackage.sca);
2304
2305         switch (saveMsg->msgStatus) {
2306                 case SMS_STATUS_READ:
2307                         stat = AT_REC_READ;
2308                         break;
2309
2310                 case SMS_STATUS_UNREAD:
2311                         stat = AT_REC_UNREAD;
2312                         break;
2313
2314                 case SMS_STATUS_SENT:
2315                         stat = AT_STO_SENT;
2316                         break;
2317
2318                 case SMS_STATUS_UNSENT:
2319                         stat = AT_STO_UNSENT;
2320                         break;
2321
2322                 default:
2323                         err("Invalid msgStatus");
2324                         dbg("Exit");
2325                         return TCORE_RETURN_EINVAL;
2326         }
2327
2328         if ((saveMsg->msgDataPackage.msgLength > 0)
2329                 && (saveMsg->msgDataPackage.msgLength <= SMS_SMDATA_SIZE_MAX)) {
2330                 ScLength = (int)saveMsg->msgDataPackage.sca[0];
2331
2332                 buf[0] = ScLength;
2333                 dbg("ScLength = %d", ScLength);
2334
2335                 if(ScLength == 0) {
2336                         buf[0] = 0;
2337                 } else {
2338                         memcpy(&buf[1],  saveMsg->msgDataPackage.sca, ScLength);
2339                 }
2340
2341                 memcpy(&buf[ScLength+1],  saveMsg->msgDataPackage.tpduData, saveMsg->msgDataPackage.msgLength);
2342
2343                 pdu_len= saveMsg->msgDataPackage.msgLength + ScLength + 1;
2344                 dbg("pdu_len: [%d]", pdu_len);
2345
2346                 hex_pdu = malloc(pdu_len * 2 + 1);
2347                 util_hex_dump("    ", sizeof(buf), (void *)buf);
2348
2349                 memset (hex_pdu, 0x00, pdu_len * 2 + 1);
2350
2351                 util_byte_to_hex((const char *)buf, (char *)hex_pdu, pdu_len);
2352
2353                 //AT+CMGW=<length>[,<stat>]<CR>PDU is given<ctrl-Z/ESC>
2354                 cmd_str = g_strdup_printf("AT+CMGW=%d,%d%s%s\x1A", saveMsg->msgDataPackage.msgLength, stat, "\r", hex_pdu);
2355                 pending = tcore_pending_new(obj, 0);
2356                 atreq = tcore_at_request_new((const char *)cmd_str, "+CMGW", TCORE_AT_SINGLELINE);
2357
2358                 if(NULL == cmd_str || NULL == atreq || NULL == pending) {
2359                         err("Out of memory. Unable to proceed");
2360                         dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2361
2362                         //free memory we own
2363                         g_free(cmd_str);
2364                         util_sms_free_memory(atreq);
2365                         util_sms_free_memory(pending);
2366                         util_sms_free_memory(hex_pdu);
2367
2368                         dbg("Exit");
2369                         return TCORE_RETURN_ENOMEM;
2370                 }
2371
2372                 util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2373
2374                 tcore_pending_set_request_data(pending, 0, atreq);
2375                 tcore_pending_set_response_callback(pending, on_response_sms_save_msg, NULL);
2376                 tcore_pending_link_user_request(pending, ur);
2377                 tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2378                 tcore_hal_send_request(hal, pending);
2379
2380                 g_free(cmd_str);
2381                 free(hex_pdu);
2382
2383                 dbg("Exit");
2384                 return TCORE_RETURN_SUCCESS;
2385         }
2386
2387         err("Invalid Data len");
2388         dbg("Exit");
2389         return TCORE_RETURN_SMS_INVALID_DATA_LEN;
2390 }
2391
2392 static TReturn delete_msg(CoreObject *obj, UserRequest *ur)
2393 {
2394         gchar *cmd_str = NULL;
2395         TcoreHal *hal = NULL;
2396         TcoreATRequest *atreq = NULL;
2397         TcorePending *pending = NULL;
2398         const struct treq_sms_delete_msg *delete_msg = NULL;
2399
2400         dbg("Entry");
2401
2402         delete_msg = tcore_user_request_ref_data(ur, NULL);
2403         hal = tcore_object_get_hal(obj);
2404         if (NULL == delete_msg || NULL == hal) {
2405                 err("NULL input. Unable to proceed");
2406                 dbg("deleteMsg: [%p], hal: [%p]", delete_msg, hal);
2407
2408                 dbg("Exit");
2409                 return TCORE_RETURN_EINVAL;
2410         }
2411
2412         if(FALSE == tcore_hal_get_power_state(hal)){
2413                 dbg("cp not ready/n");
2414                 return TCORE_RETURN_ENOSYS;
2415         }
2416
2417         dbg("index: %d", delete_msg->index);
2418
2419         if (delete_msg->index == -1) {
2420                 cmd_str = g_strdup_printf("AT+CMGD=0,4"); // Delete All Messages
2421         } else {
2422                 cmd_str = g_strdup_printf("AT+CMGD=%d,0", delete_msg->index + 1); // Delete specified index
2423         }
2424
2425         pending = tcore_pending_new(obj, 0);
2426         atreq = tcore_at_request_new((const char *)cmd_str, NULL, TCORE_AT_NO_RESULT);
2427         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2428                 err("Out of memory. Unable to proceed");
2429                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2430
2431                 //free memory we own
2432                 g_free(cmd_str);
2433                 util_sms_free_memory(atreq);
2434                 util_sms_free_memory(pending);
2435
2436                 dbg("Exit");
2437                 return TCORE_RETURN_ENOMEM;
2438         }
2439
2440         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2441
2442         tcore_pending_set_request_data(pending, 0, atreq);
2443         tcore_pending_set_response_callback(pending, on_response_sms_delete_msg, (void *) (uintptr_t) (delete_msg->index)); // storing index as user data for response
2444         tcore_pending_link_user_request(pending, ur);
2445         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2446         tcore_hal_send_request(hal, pending);
2447
2448         g_free(cmd_str);
2449
2450         dbg("Exit");
2451         return TCORE_RETURN_SUCCESS;
2452 }
2453
2454 static TReturn get_stored_msg_cnt(CoreObject *obj, UserRequest *ur)
2455 {
2456         gchar *cmd_str = NULL;
2457         TcoreHal *hal = NULL;
2458         TcoreATRequest *atreq = NULL;
2459         TcorePending *pending = NULL;
2460
2461         dbg("Entry");
2462
2463         hal = tcore_object_get_hal(obj);
2464         if (NULL == hal) {
2465                 err("NULL HAL. Unable to proceed");
2466
2467                 dbg("Exit");
2468                 return TCORE_RETURN_EINVAL;
2469         }
2470
2471         if(FALSE == tcore_hal_get_power_state(hal)){
2472                 dbg("cp not ready/n");
2473                 return TCORE_RETURN_ENOSYS;
2474         }
2475
2476         cmd_str = g_strdup_printf("AT+CPMS=\"SM\"");
2477         pending = tcore_pending_new(obj, 0);
2478         atreq = tcore_at_request_new((const char *)cmd_str, "+CPMS", TCORE_AT_SINGLELINE);
2479
2480         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2481                 err("Out of memory. Unable to proceed");
2482                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2483
2484                 //free memory we own
2485                 g_free(cmd_str);
2486                 util_sms_free_memory(atreq);
2487                 util_sms_free_memory(pending);
2488
2489                 dbg("Exit");
2490                 return TCORE_RETURN_ENOMEM;
2491         }
2492
2493         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2494
2495         tcore_pending_set_request_data(pending, 0, atreq);
2496         tcore_pending_set_response_callback(pending, on_response_get_stored_msg_cnt, NULL);
2497         tcore_pending_link_user_request(pending, ur);
2498         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2499         tcore_hal_send_request(hal, pending);
2500
2501         g_free(cmd_str);
2502
2503         dbg("Exit");
2504         return TCORE_RETURN_SUCCESS;
2505 }
2506
2507 static TReturn get_sca(CoreObject *obj, UserRequest *ur)
2508 {
2509         gchar * cmd_str = NULL;
2510         TcoreHal *hal = NULL;
2511         TcoreATRequest *atreq = NULL;
2512         TcorePending *pending = NULL;
2513
2514         dbg("Entry");
2515
2516         hal = tcore_object_get_hal(obj);
2517         if (NULL == hal) {
2518                 err("HAL NULL. Unable to proceed");
2519
2520                 dbg("Exit");
2521                 return TCORE_RETURN_EINVAL;
2522         }
2523         if(FALSE == tcore_hal_get_power_state(hal)){
2524                 dbg("cp not ready/n");
2525                 return TCORE_RETURN_ENOSYS;
2526         }
2527
2528         cmd_str = g_strdup_printf("AT+CSCA?");
2529         pending = tcore_pending_new(obj, 0);
2530         atreq = tcore_at_request_new((const char *)cmd_str, "+CSCA", TCORE_AT_SINGLELINE);
2531
2532         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2533                 err("Out of memory. Unable to proceed");
2534                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2535
2536                 //free memory we own
2537                 g_free(cmd_str);
2538                 util_sms_free_memory(atreq);
2539                 util_sms_free_memory(pending);
2540
2541                 dbg("Exit");
2542                 return TCORE_RETURN_ENOMEM;
2543         }
2544
2545         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2546
2547         tcore_pending_set_request_data(pending, 0, atreq);
2548         tcore_pending_set_response_callback(pending, on_response_get_sca, NULL);
2549         tcore_pending_link_user_request(pending, ur);
2550         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2551         tcore_hal_send_request(hal, pending);
2552
2553         g_free(cmd_str);
2554
2555         dbg("Exit");
2556         return TCORE_RETURN_SUCCESS;
2557 }
2558
2559 static TReturn set_sca(CoreObject *obj, UserRequest *ur)
2560 {
2561         gchar *cmd_str = NULL;
2562         TcoreHal *hal = NULL;
2563         TcoreATRequest *atreq = NULL;
2564         TcorePending *pending = NULL;
2565         const struct treq_sms_set_sca *setSca = NULL;
2566         int addrType = 0;
2567
2568         dbg("Entry");
2569
2570         setSca = tcore_user_request_ref_data(ur, NULL);
2571         hal = tcore_object_get_hal(obj);
2572         if (NULL == setSca || NULL == hal) {
2573                 err("NULL input. Unable to proceed");
2574                 dbg("setSca: [%p], hal: [%p]", setSca, hal);
2575
2576                 dbg("Exit");
2577                 return TCORE_RETURN_EINVAL;
2578         }
2579         if(FALSE == tcore_hal_get_power_state(hal)){
2580                 dbg("cp not ready/n");
2581                 return TCORE_RETURN_ENOSYS;
2582         }
2583
2584         dbg("dialNumLen: %u, typeOfNum: %d, numPlanId: %d, ", setSca->scaInfo.dialNumLen, setSca->scaInfo.typeOfNum, setSca->scaInfo.numPlanId);
2585
2586         util_hex_dump("    ", (SMS_SMSP_ADDRESS_LEN+1), (void *)setSca->scaInfo.diallingNum);
2587
2588         addrType = ((setSca->scaInfo.typeOfNum << 4) | setSca->scaInfo.numPlanId) | 0x80;
2589
2590         cmd_str = g_strdup_printf("AT+CSCA=\"%s\",%d", setSca->scaInfo.diallingNum, addrType);
2591         pending = tcore_pending_new(obj, 0);
2592         atreq = tcore_at_request_new((const char *)cmd_str, NULL, TCORE_AT_NO_RESULT);
2593
2594         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2595                 err("Out of memory. Unable to proceed");
2596                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2597
2598                 //free memory we own
2599                 g_free(cmd_str);
2600                 util_sms_free_memory(atreq);
2601                 util_sms_free_memory(pending);
2602
2603                 dbg("Exit");
2604                 return TCORE_RETURN_ENOMEM;
2605         }
2606
2607         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2608
2609         tcore_pending_set_request_data(pending, 0, atreq);
2610         tcore_pending_set_response_callback(pending, on_response_set_sca, NULL);
2611         tcore_pending_link_user_request(pending, ur);
2612         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2613         tcore_hal_send_request(hal, pending);
2614
2615         g_free(cmd_str);
2616
2617         dbg("Exit");
2618         return TCORE_RETURN_SUCCESS;
2619 }
2620
2621 static TReturn get_cb_config(CoreObject *obj, UserRequest *ur)
2622 {
2623         gchar *cmd_str = NULL;
2624         TcoreHal *hal = NULL;
2625         TcoreATRequest *atreq = NULL;
2626         TcorePending *pending = NULL;
2627
2628         dbg("Entry");
2629
2630         hal = tcore_object_get_hal(obj);
2631         if (NULL == hal) {
2632                 err("NULL HAL. Unable to proceed");
2633
2634                 dbg("Exit");
2635                 return TCORE_RETURN_EINVAL;
2636         }
2637         if(FALSE == tcore_hal_get_power_state(hal)){
2638                 dbg("cp not ready/n");
2639                 return TCORE_RETURN_ENOSYS;
2640         }
2641
2642         cmd_str = g_strdup_printf("AT+CSCB?");
2643         pending = tcore_pending_new(obj, 0);
2644         atreq = tcore_at_request_new((const char *)cmd_str, "+CSCB", TCORE_AT_SINGLELINE);
2645         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2646                 err("Out of memory. Unable to proceed");
2647                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2648
2649                 //free memory we own
2650                 g_free(cmd_str);
2651                 util_sms_free_memory(atreq);
2652                 util_sms_free_memory(pending);
2653
2654                 dbg("Exit");
2655                 return TCORE_RETURN_ENOMEM;
2656         }
2657
2658         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2659
2660         tcore_pending_set_request_data(pending, 0, atreq);
2661         tcore_pending_set_response_callback(pending, on_response_get_cb_config, NULL);
2662         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2663         tcore_pending_link_user_request(pending, ur);
2664         tcore_hal_send_request(hal, pending);
2665
2666         g_free(cmd_str);
2667
2668         dbg("Exit");
2669         return TCORE_RETURN_SUCCESS;
2670 }
2671
2672 static TReturn set_cb_config(CoreObject *obj, UserRequest *ur)
2673 {
2674         gchar *cmd_str = NULL;
2675         gchar *mids_str = NULL;
2676         GString *mids_GString = NULL;
2677
2678         TcoreHal *hal = NULL;
2679         TcoreATRequest *atreq = NULL;
2680         TcorePending *pending = NULL;
2681         const struct treq_sms_set_cb_config *setCbConfig = NULL;
2682         int ctr1= 0, ctr2 =0;
2683         unsigned short appendMsgId = 0;
2684
2685         dbg("Entry");
2686
2687         setCbConfig = tcore_user_request_ref_data(ur, NULL);
2688         hal = tcore_object_get_hal(obj);
2689         if (NULL == setCbConfig || NULL == hal) {
2690                 err("NULL input. Unable to proceed");
2691                 dbg("setCbConfig: [%p], hal: [%p]", setCbConfig, hal);
2692
2693                 dbg("Exit");
2694                 return TCORE_RETURN_EINVAL;
2695         }
2696         if(FALSE == tcore_hal_get_power_state(hal)){
2697                 dbg("cp not ready/n");
2698                 return TCORE_RETURN_ENOSYS;
2699         }
2700
2701         dbg("bCBEnabled: %d,  msgIdCount: %d", setCbConfig->cbEnabled, setCbConfig->msgIdRangeCount);
2702
2703         if (setCbConfig->cbEnabled == 2) { //Enable all CBS
2704                 cmd_str = g_strdup_printf("AT+CSCB=1");
2705         } else if ((setCbConfig->cbEnabled == 1) && (setCbConfig->msgIdRangeCount == 0)) { // Special case: Enable all CBS
2706                 cmd_str = g_strdup_printf("AT+CSCB=1");
2707         } else if (setCbConfig->cbEnabled == 0) {//AT+CSCB=0: Disable CBS
2708                 cmd_str = g_strdup_printf("AT+CSCB=0");
2709         } else {
2710                 mids_GString = g_string_new("AT+CSCB=0,\"");
2711
2712                 for(ctr1 = 0; ctr1 < setCbConfig->msgIdRangeCount; ctr1++ ) {
2713                         if( setCbConfig->msgIDs[ctr1].net3gpp.selected == FALSE )
2714                                 continue;
2715
2716                         if( SMS_GSM_SMS_CBMI_LIST_SIZE_MAX <= (setCbConfig->msgIDs[ctr1].net3gpp.toMsgId - setCbConfig->msgIDs[ctr1].net3gpp.fromMsgId) ) {
2717                                 mids_GString = g_string_new("AT+CSCB=1");
2718                                 break;
2719                         }
2720
2721                         appendMsgId = setCbConfig->msgIDs[ctr1].net3gpp.fromMsgId;
2722
2723                         for( ctr2 = 0; (ctr2 <= ((setCbConfig->msgIDs[ctr1].net3gpp.toMsgId) - (setCbConfig->msgIDs[ctr1].net3gpp.fromMsgId))); ctr2++ ) {
2724                                 dbg( "%x", appendMsgId);
2725                                 mids_GString = g_string_append(mids_GString, g_strdup_printf("%d", appendMsgId));
2726
2727                                 if (ctr2 == ((setCbConfig->msgIDs[ctr1].net3gpp.toMsgId) - (setCbConfig->msgIDs[ctr1].net3gpp.fromMsgId))) {
2728                                         mids_GString = g_string_append(mids_GString, "\""); //Mids string termination
2729                                 } else {
2730                                         mids_GString = g_string_append(mids_GString, ",");
2731                                 }
2732
2733                                 appendMsgId++;
2734                         }
2735                 }
2736                 mids_str = g_string_free(mids_GString, FALSE);
2737                 cmd_str = g_strdup_printf("%s", mids_str);
2738                 g_free(mids_str);
2739         }
2740
2741         pending = tcore_pending_new(obj, 0);
2742         atreq = tcore_at_request_new((const char *)cmd_str, NULL, TCORE_AT_NO_RESULT);
2743         if(NULL == cmd_str || NULL == atreq || NULL == pending) {
2744                 err("Out of memory. Unable to proceed");
2745                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2746
2747                 //free memory we own
2748                 g_free(cmd_str);
2749                 util_sms_free_memory(atreq);
2750                 util_sms_free_memory(pending);
2751
2752                 dbg("Exit");
2753                 return TCORE_RETURN_ENOMEM;
2754         }
2755
2756         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2757
2758         tcore_pending_set_request_data(pending, 0, atreq);
2759         tcore_pending_set_response_callback(pending, on_response_set_cb_config, NULL);
2760         tcore_pending_link_user_request(pending, ur);
2761         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2762         tcore_hal_send_request(hal, pending);
2763
2764         g_free(cmd_str);
2765
2766         dbg("Exit");
2767         return TCORE_RETURN_SUCCESS;
2768 }
2769
2770 static TReturn set_mem_status(CoreObject *obj, UserRequest *ur)
2771 {
2772         gchar *cmd_str = NULL;
2773         TcoreHal *hal = NULL;
2774         TcoreATRequest *atreq = NULL;
2775         TcorePending *pending = NULL;
2776         const struct treq_sms_set_mem_status *setMemStatus = NULL;
2777         int memoryStatus = 0;
2778
2779         dbg("Entry");
2780
2781         setMemStatus = tcore_user_request_ref_data(ur, NULL);
2782         hal = tcore_object_get_hal(obj);
2783         if (NULL == setMemStatus || NULL == hal) {
2784                 err("NULL input. Unable to proceed");
2785                 dbg("setMemStatus: [%p], hal: [%p]", setMemStatus, hal);
2786
2787                 dbg("Exit");
2788                 return TCORE_RETURN_EINVAL;
2789         }
2790         if(FALSE == tcore_hal_get_power_state(hal)){
2791                 dbg("cp not ready/n");
2792                 return TCORE_RETURN_ENOSYS;
2793         }
2794
2795         dbg("memory_status: %d", setMemStatus->memory_status);
2796
2797         if(setMemStatus->memory_status < SMS_PDA_MEMORY_STATUS_AVAILABLE
2798                 || setMemStatus->memory_status > SMS_PDA_MEMORY_STATUS_FULL) {
2799                 err("Invalid memory_status");
2800
2801                 dbg("Exit");
2802                 return TCORE_RETURN_EINVAL;
2803         }
2804
2805         switch (setMemStatus->memory_status) {
2806                 case SMS_PDA_MEMORY_STATUS_AVAILABLE:
2807                         memoryStatus = AT_MEMORY_AVAILABLE;
2808                         break;
2809
2810                 case SMS_PDA_MEMORY_STATUS_FULL:
2811                         memoryStatus = AT_MEMORY_FULL;
2812                         break;
2813
2814                 default:
2815                         err("Invalid memory_status");
2816                         dbg("Exit");
2817                         return TCORE_RETURN_EINVAL;
2818         }
2819
2820         cmd_str = g_strdup_printf("AT+XTESM=%d", memoryStatus);
2821         pending = tcore_pending_new(obj, 0);
2822         atreq = tcore_at_request_new((const char *)cmd_str, NULL, TCORE_AT_NO_RESULT);
2823
2824         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2825                 err("Out of memory. Unable to proceed");
2826                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2827
2828                 //free memory we own
2829                 g_free(cmd_str);
2830                 util_sms_free_memory(atreq);
2831                 util_sms_free_memory(pending);
2832
2833                 dbg("Exit");
2834                 return TCORE_RETURN_ENOMEM;
2835         }
2836
2837         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2838
2839         tcore_pending_set_request_data(pending, 0, atreq);
2840         tcore_pending_set_response_callback(pending, on_response_set_mem_status, NULL);
2841         tcore_pending_link_user_request(pending, ur);
2842         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2843         tcore_hal_send_request(hal, pending);
2844
2845         g_free(cmd_str);
2846
2847         dbg("Exit");
2848         return TCORE_RETURN_SUCCESS;
2849 }
2850
2851 static TReturn set_delivery_report(CoreObject *obj, UserRequest *ur)
2852 {
2853         struct tresp_sms_set_delivery_report respSetDeliveryReport = {0,};
2854
2855         respSetDeliveryReport.result = SMS_SUCCESS;
2856
2857         dbg("Entry");
2858         if(FALSE == tcore_hal_get_power_state(tcore_object_get_hal(obj))){
2859                 dbg("cp not ready/n");
2860                 return TCORE_RETURN_ENOSYS;
2861         }
2862
2863         dbg("CP takes care of sending SMS ack to network for all classes of SMS. Sending default success.");
2864
2865         tcore_user_request_send_response(ur, TRESP_SMS_SET_DELIVERY_REPORT, sizeof(struct tresp_sms_set_delivery_report), &respSetDeliveryReport);
2866
2867         dbg("Exit");
2868         return TCORE_RETURN_SUCCESS;
2869 }
2870
2871 static TReturn set_msg_status(CoreObject *obj, UserRequest *ur)
2872 {
2873         gchar *cmd_str = NULL;
2874         TcoreHal *hal = NULL;
2875         TcoreATRequest *atreq = NULL;
2876         TcorePending *pending = NULL;
2877         const struct treq_sms_set_msg_status *msg_status = NULL;
2878
2879         dbg("Entry");
2880         hal = tcore_object_get_hal(obj);
2881         if(FALSE == tcore_hal_get_power_state(hal)){
2882                 dbg("cp not ready/n");
2883                 return TCORE_RETURN_ENOSYS;
2884         }
2885         msg_status = tcore_user_request_ref_data(ur, NULL);
2886
2887         cmd_str = g_strdup_printf("AT+CRSM=178,28476,%d,4,%d", (msg_status->index+1), PDU_LEN_MAX);
2888         atreq = tcore_at_request_new((const char *)cmd_str, "+CRSM", TCORE_AT_SINGLELINE);
2889         pending = tcore_pending_new(obj, 0);
2890         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2891                 err("Out of memory. Unable to proceed");
2892                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2893
2894                 //free memory we own
2895                 g_free(cmd_str);
2896                 util_sms_free_memory(atreq);
2897                 util_sms_free_memory(pending);
2898
2899                 dbg("Exit");
2900                 return TCORE_RETURN_ENOMEM;
2901         }
2902
2903         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2904
2905         tcore_pending_set_request_data(pending, 0, atreq);
2906         tcore_pending_set_response_callback(pending, _response_get_efsms_data, NULL);
2907         tcore_pending_link_user_request(pending, ur);
2908         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2909         tcore_hal_send_request(hal, pending);
2910
2911         g_free(cmd_str);
2912
2913         dbg("Exit");
2914         return TCORE_RETURN_SUCCESS;
2915 }
2916
2917 static TReturn get_sms_params(CoreObject *obj, UserRequest *ur)
2918 {
2919         gchar *cmd_str = NULL;
2920         TcoreHal *hal = NULL;
2921         TcoreATRequest *atreq = NULL;
2922         TcorePending *pending = NULL;
2923         const struct treq_sms_get_params *getSmsParams = NULL;
2924         int record_len = 0 , *smsp_record_len = NULL;
2925
2926         dbg("Entry");
2927
2928         getSmsParams = tcore_user_request_ref_data(ur, NULL);
2929         hal = tcore_object_get_hal(obj);
2930         if (NULL == getSmsParams || NULL == hal) {
2931                 err("NULL input. Unable to proceed");
2932                 dbg("getSmsParams: [%p], hal: [%p]", getSmsParams, hal);
2933
2934                 dbg("Exit");
2935                 return TCORE_RETURN_EINVAL;
2936         }
2937         if(FALSE == tcore_hal_get_power_state(hal)){
2938                 dbg("cp not ready/n");
2939                 return TCORE_RETURN_ENOSYS;
2940         }
2941
2942         smsp_record_len = tcore_plugin_ref_property(tcore_object_ref_plugin(obj), "SMSPRECORDLEN");
2943         record_len = *smsp_record_len;
2944         dbg("record len from property %d", record_len);
2945
2946         //AT+CRSM=command>[,<fileid>[,<P1>,<P2>,<P3>[,<data>[,<pathid>]]]]
2947         cmd_str = g_strdup_printf("AT+CRSM=178,28482,%d,4,%d", (getSmsParams->index + 1), record_len);
2948
2949         dbg("cmd_str is %s",cmd_str);
2950
2951         atreq = tcore_at_request_new((const char *)cmd_str, "+CRSM", TCORE_AT_SINGLELINE);
2952         pending = tcore_pending_new(obj, 0);
2953         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2954                 err("Out of memory. Unable to proceed");
2955                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2956
2957                 //free memory we own
2958                 g_free(cmd_str);
2959                 util_sms_free_memory(atreq);
2960                 util_sms_free_memory(pending);
2961
2962                 dbg("Exit");
2963                 return TCORE_RETURN_ENOMEM;
2964         }
2965
2966         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2967
2968         tcore_pending_set_request_data(pending, 0, atreq);
2969         tcore_pending_set_response_callback(pending, on_response_get_sms_params, NULL);
2970         tcore_pending_link_user_request(pending, ur);
2971         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2972         tcore_hal_send_request(hal, pending);
2973
2974         g_free(cmd_str);
2975
2976         dbg("Exit");
2977         return TCORE_RETURN_SUCCESS;
2978 }
2979
2980 static TReturn set_sms_params(CoreObject *obj, UserRequest *ur)
2981 {
2982         gchar *cmd_str = NULL;
2983         char *encoded_data = NULL;
2984         unsigned char *temp_data = NULL;
2985         int SMSPRecordLen = 0;
2986         int *smsp_record_len;
2987
2988         TcoreHal *hal = NULL;
2989         TcoreATRequest *atreq = NULL;
2990         TcorePending *pending = NULL;
2991         const struct treq_sms_set_params *setSmsParams = NULL;
2992         int encoded_data_len = 0;
2993
2994         dbg("Entry");
2995
2996         setSmsParams = tcore_user_request_ref_data(ur, NULL);
2997         hal = tcore_object_get_hal(obj);
2998         if (NULL == setSmsParams || NULL == hal) {
2999                 err("NULL input. Unable to proceed");
3000                 dbg("setSmsParams: [%p], hal: [%p]", setSmsParams, hal);
3001                 return FALSE;
3002         }
3003         if(FALSE == tcore_hal_get_power_state(hal)){
3004                 dbg("cp not ready/n");
3005                 return TCORE_RETURN_ENOSYS;
3006         }
3007
3008         //EFsmsp file size is 28 +Y bytes (Y is alpha id size)
3009         smsp_record_len = tcore_plugin_ref_property(tcore_object_ref_plugin(obj), "SMSPRECORDLEN");
3010         SMSPRecordLen = *smsp_record_len;
3011         if (SMSPRecordLen < nDefaultSMSPWithoutAlphaId)
3012                 return FALSE;
3013
3014         temp_data = calloc(SMSPRecordLen,1);
3015         encoded_data = calloc(SMSPRecordLen*2 + 1,1);
3016
3017         _tcore_util_sms_encode_smsParameters(&(setSmsParams->params), temp_data, SMSPRecordLen);
3018
3019         util_byte_to_hex((const char *)temp_data, (char *)encoded_data,SMSPRecordLen);
3020
3021         encoded_data_len = ((SMSPRecordLen) * 2);
3022
3023         hal = tcore_object_get_hal(obj);
3024         pending = tcore_pending_new(obj, 0);
3025
3026         dbg("alpha id len %d encoded data %s. Encoded data len %d",setSmsParams->params.alphaIdLen,encoded_data, encoded_data_len);
3027         cmd_str = g_strdup_printf("AT+CRSM=220,28482,%d,4,%d,\"%s\"",(setSmsParams->params.recordIndex+1),SMSPRecordLen,encoded_data);
3028
3029         dbg("cmd str is %s",cmd_str);
3030         atreq = tcore_at_request_new(cmd_str, "+CRSM:", TCORE_AT_SINGLELINE);
3031
3032         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
3033                 err("Out of memory. Unable to proceed");
3034                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
3035
3036                 //free memory we own
3037                 g_free(cmd_str);
3038                 util_sms_free_memory(atreq);
3039                 util_sms_free_memory(pending);
3040
3041                 util_sms_free_memory(temp_data);
3042                 util_sms_free_memory(encoded_data);
3043
3044                 dbg("Exit");
3045                 return TCORE_RETURN_ENOMEM;
3046         }
3047
3048         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
3049
3050         tcore_pending_set_request_data(pending, 0,atreq);
3051         tcore_pending_set_response_callback(pending, on_response_set_sms_params, NULL);
3052         tcore_pending_link_user_request(pending, ur);
3053         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
3054         tcore_hal_send_request(hal, pending);
3055
3056         g_free(cmd_str);
3057         util_sms_free_memory(temp_data);
3058         util_sms_free_memory(encoded_data);
3059
3060         return TCORE_RETURN_SUCCESS;
3061 }
3062
3063 static TReturn get_paramcnt(CoreObject *obj, UserRequest *ur)
3064 {
3065         gchar *cmd_str = NULL;
3066         TcoreHal *hal = NULL;
3067         TcoreATRequest *atreq = NULL;
3068         TcorePending *pending = NULL;
3069
3070         dbg("Entry");
3071
3072         hal = tcore_object_get_hal(obj);
3073         if (NULL == hal) {
3074                 err("NULL HAL. Unable to proceed");
3075
3076                 dbg("Exit");
3077                 return TCORE_RETURN_EINVAL;
3078         }
3079         if(FALSE == tcore_hal_get_power_state(hal)){
3080                 dbg("cp not ready/n");
3081                 return TCORE_RETURN_ENOSYS;
3082         }
3083
3084         //AT+CRSM=command>[,<fileid>[,<P1>,<P2>,<P3>[,<data>[,<pathid>]]]]
3085         cmd_str = g_strdup_printf("AT+CRSM=192,28482");
3086         atreq = tcore_at_request_new((const char *)cmd_str, "+CRSM", TCORE_AT_SINGLELINE);
3087         pending = tcore_pending_new(obj, 0);
3088
3089         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
3090                 err("NULL pointer. Unable to proceed");
3091                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
3092
3093                 //free memory we own
3094                 g_free(cmd_str);
3095                 util_sms_free_memory(atreq);
3096                 util_sms_free_memory(pending);
3097
3098                 dbg("Exit");
3099                 return TCORE_RETURN_FAILURE;
3100         }
3101
3102         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
3103
3104         tcore_pending_set_request_data(pending, 0, atreq);
3105         tcore_pending_set_response_callback(pending, on_response_get_paramcnt, NULL);
3106         tcore_pending_link_user_request(pending, ur);
3107         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
3108         tcore_hal_send_request(hal, pending);
3109
3110         g_free(cmd_str);
3111
3112         dbg("Exit");
3113         return TCORE_RETURN_SUCCESS;
3114 }
3115
3116 static struct tcore_sms_operations sms_ops = {
3117         .send_umts_msg = send_umts_msg,
3118         .read_msg = read_msg,
3119         .save_msg = save_msg,
3120         .delete_msg = delete_msg,
3121         .get_stored_msg_cnt = get_stored_msg_cnt,
3122         .get_sca = get_sca,
3123         .set_sca = set_sca,
3124         .get_cb_config = get_cb_config,
3125         .set_cb_config = set_cb_config,
3126         .set_mem_status = set_mem_status,
3127         .get_pref_brearer = NULL,
3128         .set_pref_brearer = NULL,
3129         .set_delivery_report = set_delivery_report,
3130         .set_msg_status = set_msg_status,
3131         .get_sms_params = get_sms_params,
3132         .set_sms_params = set_sms_params,
3133         .get_paramcnt = get_paramcnt,
3134 };
3135
3136 gboolean s_sms_init(TcorePlugin *cp, CoreObject *co_sms)
3137 {
3138         int *smsp_record_len;
3139         dbg("Entry");
3140
3141         /* Override SMS Operations */
3142         tcore_sms_override_ops(co_sms, &sms_ops);
3143
3144         /* Registering for SMS notifications */
3145         tcore_object_override_callback(co_sms, "+CMTI:", on_event_class2_sms_incom_msg, NULL);
3146         tcore_object_override_callback(co_sms, "\e+CMT:", on_event_sms_incom_msg, NULL);
3147
3148         tcore_object_override_callback(co_sms, "\e+CDS", on_event_sms_incom_msg, NULL);
3149         tcore_object_override_callback(co_sms, "+XSMSMMSTAT", on_event_sms_memory_status, NULL);
3150         tcore_object_override_callback(co_sms, "+CMS", on_event_sms_memory_status, NULL);
3151
3152         tcore_object_override_callback(co_sms, "+CBMI:", on_event_sms_cb_incom_msg, NULL);
3153         tcore_object_override_callback(co_sms, "\e+CBM:", on_event_sms_cb_incom_msg, NULL);
3154
3155         /* Storing SMSP record length */
3156         smsp_record_len = g_new0(int, 1);
3157         tcore_plugin_link_property(cp, "SMSPRECORDLEN", smsp_record_len);
3158
3159         dbg("Exit");
3160         return TRUE;
3161 }
3162
3163 void s_sms_exit(TcorePlugin *cp, CoreObject *co_sms)
3164 {
3165         int *smsp_record_len;
3166
3167         smsp_record_len = tcore_plugin_ref_property(cp, "SMSPRECORDLEN");
3168         g_free(smsp_record_len);
3169
3170         dbg("Exit");
3171 }