Fix TDIS-1012 issue(APN setting)
[platform/core/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 {
902                                         if ((resp_read_msg.dataInfo.smsData.msgLength > 0)
903                                                 && (resp_read_msg.dataInfo.smsData.msgLength <= SMS_SMDATA_SIZE_MAX)) {
904                                                 memcpy(resp_read_msg.dataInfo.smsData.sca, (char *)byte_pdu, (sca_length+1));
905                                                 memcpy(resp_read_msg.dataInfo.smsData.tpduData, &byte_pdu[sca_length+1], resp_read_msg.dataInfo.smsData.msgLength);
906
907                                                 util_hex_dump("    ", SMS_SMSP_ADDRESS_LEN, (void *)resp_read_msg.dataInfo.smsData.sca);
908                                                 util_hex_dump("    ", (SMS_SMDATA_SIZE_MAX + 1), (void *)resp_read_msg.dataInfo.smsData.tpduData);
909                                                 util_hex_dump("    ", sizeof(byte_pdu), (void *)byte_pdu);
910
911                                                 resp_read_msg.result = SMS_SUCCESS;
912                                         } else {
913                                                 dbg("Invalid Message Length");
914                                                 resp_read_msg.result = SMS_INVALID_PARAMETER_FORMAT;
915                                         }
916                                 }
917                                 g_free(byte_pdu);
918                         }else {
919                                 dbg("NULL PDU");
920                         }
921                 }else {
922                         dbg("No lines");
923                 }
924         } else {
925                 err("Response NOK");
926         }
927
928         tcore_user_request_send_response(user_req, TRESP_SMS_READ_MSG, sizeof(resp_read_msg), &resp_read_msg);
929
930         dbg("Exit");
931         return;
932 }
933
934 static void on_response_get_msg_indices(TcorePending *pending, int data_len, const void *data, void *user_data)
935 {
936         const TcoreATResponse *at_response = data;
937         struct tresp_sms_get_storedMsgCnt resp_stored_msg_cnt;
938         UserRequest *user_req = NULL;
939         struct tresp_sms_get_storedMsgCnt *resp_stored_msg_cnt_prev = NULL;
940
941         GSList *tokens = NULL;
942         char *gslist_line = NULL, *line_token = NULL;
943         int gslist_line_count = 0, ctr_loop = 0;
944
945         dbg("Entry");
946
947         resp_stored_msg_cnt_prev = (struct tresp_sms_get_storedMsgCnt *)user_data;
948         user_req = tcore_pending_ref_user_request(pending);
949
950         memset(&resp_stored_msg_cnt, 0x00, sizeof(resp_stored_msg_cnt));
951         resp_stored_msg_cnt.result = SMS_DEVICE_FAILURE;
952
953         if (at_response->success) {
954                 dbg("Response OK");
955                 if (at_response->lines) {
956                         gslist_line_count = g_slist_length(at_response->lines);
957
958                         if (gslist_line_count > SMS_GSM_SMS_MSG_NUM_MAX)
959                                 gslist_line_count = SMS_GSM_SMS_MSG_NUM_MAX;
960
961                         dbg("Number of lines: [%d]", gslist_line_count);
962                         g_slist_foreach(at_response->lines, print_glib_list_elem, NULL); //for debug log
963
964                         for (ctr_loop = 0; ctr_loop < gslist_line_count; ctr_loop++) {
965                                 gslist_line = (char *)g_slist_nth_data(at_response->lines, ctr_loop); /* Fetch Line i */
966
967                                 dbg("gslist_line [%d] is [%s]", ctr_loop, gslist_line);
968
969                                 if (NULL != gslist_line) {
970                                         tokens = tcore_at_tok_new(gslist_line);
971
972                                         g_slist_foreach(tokens, print_glib_list_elem, NULL); //for debug log
973
974                                         line_token = g_slist_nth_data(tokens, 0);
975                                         if (NULL != line_token) {
976                                                 resp_stored_msg_cnt.storedMsgCnt.indexList[ctr_loop] = atoi(line_token);
977                                                 resp_stored_msg_cnt.result = SMS_SENDSMS_SUCCESS;
978                                         } else {
979                                                 dbg("line_token of gslist_line [%d] is NULL", ctr_loop);
980                                                 continue;
981                                         }
982                                         tcore_at_tok_free(tokens);
983                                 } else {
984                                         dbg("gslist_line [%d] is NULL", ctr_loop);
985                                         continue;
986                                 }
987                         }
988                 } else {
989                         dbg("No lines.");
990                         if (resp_stored_msg_cnt_prev->storedMsgCnt.usedCount == 0) { // Check if used count is zero
991                                 resp_stored_msg_cnt.result = SMS_SENDSMS_SUCCESS;
992                         }
993                 }
994         } else {
995                 dbg("Respnose NOK");
996         }
997
998         resp_stored_msg_cnt.storedMsgCnt.totalCount = resp_stored_msg_cnt_prev->storedMsgCnt.totalCount;
999         resp_stored_msg_cnt.storedMsgCnt.usedCount = resp_stored_msg_cnt_prev->storedMsgCnt.usedCount;
1000
1001         util_sms_free_memory(resp_stored_msg_cnt_prev);
1002
1003         dbg("total: [%d], used: [%d], result: [%d]", resp_stored_msg_cnt.storedMsgCnt.totalCount, resp_stored_msg_cnt.storedMsgCnt.usedCount, resp_stored_msg_cnt.result);
1004         for (ctr_loop = 0; ctr_loop < gslist_line_count; ctr_loop++) {
1005                 dbg("index: [%d]", resp_stored_msg_cnt.storedMsgCnt.indexList[ctr_loop]);
1006         }
1007
1008         tcore_user_request_send_response(user_req, TRESP_SMS_GET_STORED_MSG_COUNT, sizeof(resp_stored_msg_cnt), &resp_stored_msg_cnt);
1009
1010         dbg("Exit");
1011         return;
1012 }
1013
1014 static void on_response_get_stored_msg_cnt(TcorePending *pending, int data_len, const void *data, void *user_data)
1015 {
1016         UserRequest *ur = NULL, *ur_dup = NULL;
1017         struct tresp_sms_get_storedMsgCnt *respStoredMsgCnt = NULL;
1018         const TcoreATResponse *atResp = data;
1019         GSList *tokens=NULL;
1020         char *line = NULL , *pResp = NULL , *cmd_str = NULL;
1021         TcoreATRequest *atReq = NULL;
1022         int usedCnt = 0, totalCnt = 0, result = 0;
1023
1024         TcorePending *pending_new = NULL;
1025         CoreObject *o = NULL;
1026
1027         dbg("Entered");
1028
1029         respStoredMsgCnt = malloc(sizeof(struct tresp_sms_get_storedMsgCnt));
1030         result = SMS_DEVICE_FAILURE;
1031
1032         ur = tcore_pending_ref_user_request(pending);
1033         ur_dup = tcore_user_request_ref(ur);
1034         o = tcore_pending_ref_core_object(pending);
1035
1036         if (atResp->success > 0) {
1037                 dbg("Response OK");
1038                 if (NULL != atResp->lines) {
1039                         line = (char *)atResp->lines->data;
1040                         dbg("line is %s",line);
1041
1042                         tokens = tcore_at_tok_new(line);
1043                         pResp = g_slist_nth_data(tokens, 0);
1044
1045                         if (pResp) {
1046                                 usedCnt =atoi(pResp);
1047                                 dbg("used cnt is %d",usedCnt);
1048                         }
1049
1050                         pResp = g_slist_nth_data(tokens, 1);
1051                         if (pResp) {
1052                                 totalCnt =atoi(pResp);
1053                                 result = SMS_SENDSMS_SUCCESS;
1054
1055                                 respStoredMsgCnt->storedMsgCnt.usedCount = usedCnt;
1056                                 respStoredMsgCnt->storedMsgCnt.totalCount = totalCnt;
1057                                 respStoredMsgCnt->result = result;
1058
1059                                 dbg("used %d, total %d, result %d",usedCnt, totalCnt,result);
1060
1061                                 pending_new = tcore_pending_new(o, 0);
1062                                 //Get all messages information
1063                                 cmd_str = g_strdup_printf("AT+CMGL=4");
1064                                 atReq = tcore_at_request_new((const char *)cmd_str, "+CMGL", TCORE_AT_MULTILINE);
1065
1066                                 dbg("cmd str is %s",cmd_str);
1067
1068                                 tcore_pending_set_request_data(pending_new, 0,atReq);
1069                                 tcore_pending_set_response_callback(pending_new, on_response_get_msg_indices, (void *)respStoredMsgCnt);
1070                                 tcore_pending_link_user_request(pending_new, ur_dup);
1071                                 tcore_pending_set_send_callback(pending_new, on_confirmation_sms_message_send, NULL);
1072                                 tcore_hal_send_request(tcore_object_get_hal(o), pending_new);
1073
1074                                 //free the consumed token
1075                                 tcore_at_tok_free(tokens);
1076
1077                                 g_free(cmd_str);
1078
1079                                 dbg("Exit");
1080                                 return;
1081                         }
1082                         //free the consumed token
1083                         if (tokens)
1084                         tcore_at_tok_free(tokens);
1085                 } else {
1086                         dbg("No data");
1087                 }
1088         } else {
1089                 err("Response NOK");
1090         }
1091         respStoredMsgCnt->result = result;
1092         tcore_user_request_send_response(ur, TRESP_SMS_GET_STORED_MSG_COUNT, sizeof(struct tresp_sms_get_storedMsgCnt), &respStoredMsgCnt);
1093
1094
1095         dbg("Exit");
1096         return;
1097 }
1098
1099 static void on_response_get_sca(TcorePending *pending, int data_len, const void *data, void *user_data)
1100 {
1101         const TcoreATResponse *at_response = data;
1102         struct tresp_sms_get_sca respGetSca;
1103         UserRequest *user_req = NULL;
1104
1105         GSList *tokens = NULL;
1106         char *gslist_line = NULL, *sca_addr = NULL, *sca_toa = NULL;
1107
1108         dbg("Entry");
1109
1110         memset(&respGetSca, 0, sizeof(respGetSca));
1111         respGetSca.result = SMS_DEVICE_FAILURE;
1112
1113         user_req = tcore_pending_ref_user_request(pending);
1114
1115         if (at_response->success) {
1116                 dbg("Response OK");
1117                 if (at_response->lines) {
1118                         gslist_line = (char *)at_response->lines->data;
1119
1120                         tokens = tcore_at_tok_new(gslist_line);
1121                         sca_addr = g_slist_nth_data(tokens, 0);
1122                         sca_toa = g_slist_nth_data(tokens, 1);
1123
1124                         if ((NULL != sca_addr)
1125                                 && (NULL != sca_toa)) {
1126                                 dbg("sca_addr: [%s]. sca_toa: [%s]", sca_addr, sca_toa);
1127
1128                                 respGetSca.scaAddress.dialNumLen = strlen(sca_addr);
1129
1130                                 if (145 == atoi(sca_toa)) {
1131                                         respGetSca.scaAddress.typeOfNum = SIM_TON_INTERNATIONAL;
1132                                 } else {
1133                                         respGetSca.scaAddress.typeOfNum = SIM_TON_NATIONAL;
1134                                 }
1135
1136                                 respGetSca.scaAddress.numPlanId = 0;
1137
1138                                 memcpy(respGetSca.scaAddress.diallingNum, sca_addr, strlen(sca_addr));
1139
1140                                 dbg("len [%d], sca_addr [%s], TON [%d], NPI [%d]", respGetSca.scaAddress.dialNumLen, respGetSca.scaAddress.diallingNum, respGetSca.scaAddress.typeOfNum, respGetSca.scaAddress.numPlanId);
1141
1142                                 respGetSca.result = SMS_SENDSMS_SUCCESS;
1143                         } else {
1144                                 err("sca_addr OR sca_toa NULL");
1145                         }
1146                 } else {
1147                         dbg("NO Lines");
1148                 }
1149         } else {
1150                 dbg("Response NOK");
1151         }
1152
1153         tcore_user_request_send_response(user_req, TRESP_SMS_GET_SCA, sizeof(respGetSca), &respGetSca);
1154
1155         if(tokens)
1156                 tcore_at_tok_free(tokens);
1157
1158         dbg("Exit");
1159         return;
1160 }
1161
1162 static void on_response_set_sca(TcorePending *pending, int data_len, const void *data, void *user_data)
1163 {
1164         /*
1165         Response is expected in this format
1166         OK
1167         or
1168         +CMS ERROR: <err>
1169         */
1170         UserRequest *ur;
1171         //copies the AT response data to resp
1172         const TcoreATResponse *atResp = data;
1173         struct tresp_sms_set_sca respSetSca;
1174
1175         memset(&respSetSca, 0, sizeof(struct tresp_sms_set_sca));
1176
1177         ur = tcore_pending_ref_user_request(pending);
1178         if (!ur) {
1179                 dbg("no user_request");
1180                 return;
1181         }
1182
1183         if (atResp->success > 0) {
1184                 dbg("RESPONSE OK");
1185                 respSetSca.result = SMS_SUCCESS;
1186         } else {
1187                 dbg("RESPONSE NOK");
1188                 respSetSca.result = SMS_DEVICE_FAILURE;
1189         }
1190
1191         tcore_user_request_send_response(ur, TRESP_SMS_SET_SCA, sizeof(struct tresp_sms_set_sca), &respSetSca);
1192
1193         return;
1194 }
1195
1196 static void on_response_get_cb_config(TcorePending *p, int data_len, const void *data, void *user_data)
1197 {
1198         UserRequest *ur;
1199         struct tresp_sms_get_cb_config respGetCbConfig;
1200         const TcoreATResponse *atResp = data;
1201         GSList *tokens=NULL;
1202         int i = 0, mode =0;
1203         char *pResp = NULL, *line = NULL;
1204         char delim[] = "-";
1205
1206         memset(&respGetCbConfig, 0, sizeof(struct tresp_sms_get_cb_config));
1207         respGetCbConfig.result = SMS_DEVICE_FAILURE;
1208
1209         ur = tcore_pending_ref_user_request(p);
1210         if (!ur) {
1211                 dbg("no user_request");
1212                 return;
1213         }
1214
1215         respGetCbConfig.cbConfig.net3gppType = SMS_NETTYPE_3GPP;
1216
1217         if (atResp->success) {
1218                 dbg("Response OK");
1219                 if (atResp->lines) {
1220                         line = (char*)atResp->lines->data;
1221                         if (line != NULL) {
1222                                 dbg("line is %s",line);
1223                                 tokens = tcore_at_tok_new(line);
1224                                 pResp = g_slist_nth_data(tokens, 0);
1225                                 if (pResp) {
1226                                         mode = atoi(pResp);
1227                                         respGetCbConfig.cbConfig.cbEnabled = mode;
1228
1229                                         pResp = g_slist_nth_data(tokens, 1);
1230                                         if (pResp) {
1231                                                 GSList *cb_tokens = NULL;
1232                                                 char *cb_mid_str = NULL;
1233                                                 int num_cb_tokens = 0;
1234                                                 char *mid_tok = NULL;
1235                                                 char *first_tok = NULL, *second_tok = NULL;
1236
1237                                                 // 0,1,5,320-478,922
1238                                                 cb_mid_str = util_removeQuotes(pResp);
1239                                                 cb_tokens = tcore_at_tok_new((const char *) cb_mid_str);
1240
1241                                                 g_free(cb_mid_str);
1242
1243                                                 num_cb_tokens = g_slist_length(cb_tokens);
1244                                                 dbg("num_cb_tokens = %d", num_cb_tokens);
1245
1246                                                 if (num_cb_tokens == 0) {
1247                                                         if (mode == 1) { // Enable all CBs
1248                                                                 respGetCbConfig.cbConfig.msgIdRangeCount = 1;
1249                                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.fromMsgId = 0x0000;
1250                                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.toMsgId = SMS_GSM_SMS_CBMI_LIST_SIZE_MAX + 1;
1251                                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.selected = TRUE;
1252                                                                         respGetCbConfig.result = SMS_SENDSMS_SUCCESS;
1253                                                         } else { // all CBs disabled
1254                                                                 respGetCbConfig.cbConfig.msgIdRangeCount = 0;
1255                                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.selected = FALSE;
1256                                                                 respGetCbConfig.result = SMS_SENDSMS_SUCCESS;
1257                                                         }
1258                                                 } else {
1259                                                         respGetCbConfig.cbConfig.msgIdRangeCount = 0;
1260                                                         respGetCbConfig.cbConfig.msgIDs[0].net3gpp.selected = FALSE;
1261                                                         respGetCbConfig.result = SMS_SENDSMS_SUCCESS;
1262                                                 }
1263
1264                                                 for (i = 0; i < num_cb_tokens; i++) {
1265                                                         respGetCbConfig.cbConfig.msgIDs[i].net3gpp.selected = TRUE;
1266                                                         respGetCbConfig.cbConfig.msgIdRangeCount++;
1267
1268                                                         mid_tok = tcore_at_tok_nth(cb_tokens, i);
1269                                                         first_tok = strtok(mid_tok, delim);
1270                                                         second_tok = strtok(NULL, delim);
1271
1272                                                         if ((first_tok != NULL) && (second_tok != NULL)) { // mids in range (320-478)
1273                                                                 dbg("inside if mid_range");
1274                                                                 respGetCbConfig.cbConfig.msgIDs[i].net3gpp.fromMsgId = atoi(first_tok);
1275                                                                 respGetCbConfig.cbConfig.msgIDs[i].net3gpp.toMsgId = atoi(second_tok);
1276                                                         } // single mid value (0,1,5, 922)
1277                                                         else {
1278                                                                 respGetCbConfig.cbConfig.msgIDs[i].net3gpp.fromMsgId = atoi(mid_tok);
1279                                                                 respGetCbConfig.cbConfig.msgIDs[i].net3gpp.toMsgId = atoi(mid_tok);
1280                                                         }
1281                                                 }
1282                                         }
1283                                         }else {
1284                                                 if (mode == 1) {
1285                                                 respGetCbConfig.cbConfig.msgIdRangeCount = 1;
1286                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.fromMsgId = 0x0000;
1287                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.toMsgId = SMS_GSM_SMS_CBMI_LIST_SIZE_MAX + 1;
1288                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.selected = TRUE;
1289                                                 respGetCbConfig.result = SMS_SENDSMS_SUCCESS;
1290                     } else {
1291                                                 respGetCbConfig.cbConfig.msgIdRangeCount = 0;
1292                                                 respGetCbConfig.cbConfig.msgIDs[0].net3gpp.selected = FALSE;
1293                                                 respGetCbConfig.result = SMS_SENDSMS_SUCCESS;
1294                                         }
1295                                 }
1296                         } else {
1297                                         dbg("line is NULL");
1298                         }
1299                 } else {
1300                         dbg("atresp->lines is NULL");
1301                 }
1302         } else {
1303                 dbg("RESPONSE NOK");
1304         }
1305
1306         tcore_user_request_send_response(ur, TRESP_SMS_GET_CB_CONFIG, sizeof(struct tresp_sms_get_cb_config), &respGetCbConfig);
1307
1308         if(tokens)
1309                 tcore_at_tok_free(tokens);
1310
1311         return;
1312 }
1313
1314 static void on_response_set_cb_config(TcorePending *pending, int data_len, const void *data, void *user_data)
1315 {
1316         /*
1317         Response is expected in this format
1318         OK
1319         or
1320         +CMS ERROR: <err>
1321         */
1322
1323         UserRequest *ur;
1324         const TcoreATResponse *resp = data;
1325         int response = 0;
1326         const char *line = NULL;
1327         GSList *tokens=NULL;
1328
1329         struct tresp_sms_set_cb_config respSetCbConfig = {0,};
1330
1331         memset(&respSetCbConfig, 0, sizeof(struct tresp_sms_set_cb_config));
1332
1333         ur = tcore_pending_ref_user_request(pending);
1334         respSetCbConfig.result = SMS_SENDSMS_SUCCESS;
1335
1336         if (resp->success > 0) {
1337                 dbg("RESPONSE OK");
1338         } else {
1339                 dbg("RESPONSE NOK");
1340                 line = (const char*)resp->final_response;
1341                 tokens = tcore_at_tok_new(line);
1342
1343                 if (g_slist_length(tokens) < 1) {
1344                         dbg("err cause not specified or string corrupted");
1345                         respSetCbConfig.result = SMS_DEVICE_FAILURE;
1346                 } else {
1347                         response = atoi(g_slist_nth_data(tokens, 0));
1348                         /* TODO: CMEE error mapping is required. */
1349                         respSetCbConfig.result = SMS_DEVICE_FAILURE;
1350                 }
1351         }
1352         if (!ur) {
1353                 dbg("no user_request");
1354                 return;
1355         }
1356
1357         tcore_user_request_send_response(ur, TRESP_SMS_SET_CB_CONFIG, sizeof(struct tresp_sms_set_cb_config), &respSetCbConfig);
1358
1359         if(tokens)
1360                 tcore_at_tok_free(tokens);
1361
1362         return;
1363 }
1364
1365 static void on_response_set_mem_status(TcorePending *p, int data_len, const void *data, void *user_data)
1366 {
1367         UserRequest *ur;
1368         struct tresp_sms_set_mem_status respSetMemStatus = {0,};
1369         const TcoreATResponse *resp = data;
1370
1371         memset(&respSetMemStatus, 0, sizeof(struct tresp_sms_set_mem_status));
1372
1373         if (resp->success > 0) {
1374                 dbg("RESPONSE OK");
1375                 respSetMemStatus.result = SMS_SENDSMS_SUCCESS;
1376         } else {
1377                 dbg("RESPONSE NOK");
1378                 respSetMemStatus.result = SMS_DEVICE_FAILURE;
1379         }
1380
1381         ur = tcore_pending_ref_user_request(p);
1382         if (!ur) {
1383                 dbg("no user_request");
1384                 return;
1385         }
1386
1387         tcore_user_request_send_response(ur, TRESP_SMS_SET_MEM_STATUS, sizeof(struct tresp_sms_set_mem_status), &respSetMemStatus);
1388
1389         return;
1390 }
1391
1392 static void on_response_set_msg_status(TcorePending *pending, int data_len, const void *data, void *user_data)
1393 {
1394         UserRequest *ur;
1395         struct tresp_sms_set_msg_status respMsgStatus = {0, };
1396         const TcoreATResponse *atResp = data;
1397         int response = 0, sw1 = 0, sw2 = 0;
1398         const char *line = NULL;
1399         char *pResp = NULL;
1400         GSList *tokens = NULL;
1401
1402         dbg("Entry");
1403
1404         memset(&respMsgStatus, 0, sizeof(struct tresp_sms_set_msg_status));
1405         respMsgStatus.result = SMS_DEVICE_FAILURE;
1406
1407         ur = tcore_pending_ref_user_request(pending);
1408
1409         if (atResp->success > 0) {
1410                 dbg("RESPONSE OK");
1411
1412                 if (atResp->lines) {
1413                         line = (const char *) atResp->lines->data;
1414                         tokens = tcore_at_tok_new(line);
1415                         pResp = g_slist_nth_data(tokens, 0);
1416                         if (pResp != NULL) {
1417                                 sw1 = atoi(pResp);
1418                         } else {
1419                                 dbg("sw1 is NULL");
1420                         }
1421                         pResp = g_slist_nth_data(tokens, 1);
1422                         if (pResp != NULL) {
1423                                 sw2 = atoi(pResp);
1424                                 if ((sw1 == AT_SW1_SUCCESS) && (sw2 == 0)) {
1425                                         respMsgStatus.result = SMS_SENDSMS_SUCCESS;
1426                                 }
1427                         } else {
1428                                 dbg("sw2 is NULL");
1429                         }
1430                         pResp = g_slist_nth_data(tokens, 3);
1431
1432                         if (pResp != NULL) {
1433                                 response = atoi(pResp);
1434                                 dbg("response is %s", response);
1435                         }
1436                 } else {
1437                         dbg("No lines");
1438                 }
1439         } else {
1440                 dbg("RESPONSE NOK");
1441         }
1442
1443         tcore_user_request_send_response(ur, TRESP_SMS_SET_MSG_STATUS , sizeof(struct tresp_sms_set_msg_status), &respMsgStatus);
1444
1445         if(tokens)
1446                 tcore_at_tok_free(tokens);
1447
1448         dbg("Exit");
1449         return;
1450 }
1451
1452 static void on_response_get_sms_params(TcorePending *pending, int data_len, const void *data, void *user_data)
1453 {
1454         UserRequest *ur;
1455         struct tresp_sms_get_params respGetParams ;
1456         const TcoreATResponse *atResp = data;
1457         int sw1 = 0, sw2 = 0;
1458         const char *line = NULL;
1459         char *pResp = NULL;
1460         GSList *tokens=NULL;
1461         char *hexData = NULL;
1462     char *recordData = NULL;
1463     int i = 0;
1464
1465         memset(&respGetParams, 0, sizeof(struct tresp_sms_get_params));
1466         respGetParams.result = SMS_DEVICE_FAILURE;
1467
1468         ur = tcore_pending_ref_user_request(pending);
1469
1470         if (atResp->success > 0) {
1471                 dbg("RESPONSE OK");
1472
1473                 if (atResp->lines) {
1474                         line = (const char *) atResp->lines->data;
1475                         tokens = tcore_at_tok_new(line);
1476                         pResp = g_slist_nth_data(tokens, 0);
1477                         if (pResp != NULL) {
1478                                 sw1 = atoi(pResp);
1479                                 dbg("sw1 is %d", sw1);
1480                         } else {
1481                                 dbg("sw1 is NULL");
1482                         }
1483                         pResp = g_slist_nth_data(tokens, 1);
1484                         if (pResp != NULL) {
1485                                 sw2 = atoi(pResp);
1486                                 dbg("sw2 is %d", sw2);
1487                                 if ((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
1488                                         respGetParams.result = SMS_SENDSMS_SUCCESS;
1489                                 }
1490                         } else {
1491                                 dbg("sw2 is NULL");
1492                         }
1493                         pResp = g_slist_nth_data(tokens, 2);
1494                         if (pResp != NULL) {
1495                                 hexData = util_removeQuotes(pResp);
1496
1497                                 recordData = util_hexStringToBytes(hexData);
1498                                 util_hex_dump("    ", strlen(hexData) / 2, recordData);
1499
1500                                 respGetParams.paramsInfo.recordLen = strlen(hexData) / 2;
1501
1502                                 util_sms_decode_smsParameters((unsigned char *) recordData, strlen(hexData) / 2, &(respGetParams.paramsInfo));
1503                                 respGetParams.result = SMS_SENDSMS_SUCCESS;
1504
1505                                 for (i = 0; i < (int) respGetParams.paramsInfo.tpSvcCntrAddr.dialNumLen; i++)
1506                                         dbg("SCAddr = %d [%02x]", i, respGetParams.paramsInfo.tpSvcCntrAddr.diallingNum[i]);
1507
1508                                 g_free(recordData);
1509                                 g_free(hexData);
1510                         } else {
1511                                 dbg("No response");
1512                         }
1513                         tcore_at_tok_free(tokens);
1514                 }
1515         } else {
1516                 dbg("RESPONSE NOK");
1517         }
1518
1519         tcore_user_request_send_response(ur, TRESP_SMS_GET_PARAMS, sizeof(struct tresp_sms_get_params), &respGetParams);
1520
1521         dbg("Exit");
1522         return;
1523 }
1524
1525 static void on_response_set_sms_params(TcorePending *pending, int data_len, const void *data, void *user_data)
1526 {
1527         UserRequest *ur;
1528         struct tresp_sms_set_params respSetParams = {0, };
1529         const TcoreATResponse *atResp = data;
1530         int sw1 =0 , sw2 = 0;
1531         const char *line = NULL;
1532         char *pResp = NULL;
1533         GSList *tokens=NULL;
1534
1535
1536         memset(&respSetParams, 0, sizeof(struct tresp_sms_set_params));
1537         ur = tcore_pending_ref_user_request(pending);
1538
1539         respSetParams.result = SMS_DEVICE_FAILURE;
1540
1541         if (atResp->success > 0) {
1542                 dbg("RESPONSE OK");
1543
1544                 if (atResp->lines) {
1545                         line = (const char *) atResp->lines->data;
1546                         tokens = tcore_at_tok_new(line);
1547                         pResp = g_slist_nth_data(tokens, 0);
1548                         if (pResp != NULL) {
1549                                 sw1 = atoi(pResp);
1550                         } else {
1551                                 dbg("sw1 is NULL");
1552                         }
1553
1554                         pResp = g_slist_nth_data(tokens, 1);
1555                         if (pResp != NULL) {
1556                                 sw2 = atoi(pResp);
1557                                 if (((sw1 == AT_SW1_SUCCESS) && (sw2 == AT_SW2_SUCCESS)) || (sw1 == 0x91)) {
1558                                         respSetParams.result = SMS_SENDSMS_SUCCESS;
1559                                 }
1560                         } else {
1561                                 dbg("sw2 is NULL");
1562                         }
1563                 } else {
1564                         dbg("No lines");
1565                 }
1566         } else {
1567                 dbg("RESPONSE NOK");
1568         }
1569
1570         tcore_user_request_send_response(ur, TRESP_SMS_SET_PARAMS , sizeof(struct tresp_sms_set_params), &respSetParams);
1571
1572         if(tokens)
1573                 tcore_at_tok_free(tokens);
1574
1575         dbg("Exit");
1576         return;
1577 }
1578
1579 static void on_response_get_paramcnt(TcorePending *p, int data_len, const void *data, void *user_data)
1580 {
1581         UserRequest *ur = NULL;
1582         struct tresp_sms_get_paramcnt respGetParamCnt = {0, };
1583         const TcoreATResponse *atResp = data;
1584         char *line = NULL , *pResp = NULL;
1585         int sw1 = 0 , sw2 = 0, *smsp_record_len = NULL;
1586         int sim_type = 0;
1587         GSList *tokens=NULL;
1588         CoreObject *co_sim = NULL;  //need this to get the sim type GSM/USIM
1589         TcorePlugin *plugin = NULL;
1590
1591         dbg("Entry");
1592
1593         ur = tcore_pending_ref_user_request(p);
1594         respGetParamCnt.result = SMS_DEVICE_FAILURE;
1595
1596         if (atResp->success > 0) {
1597                 dbg("RESPONSE OK");
1598
1599                 if (atResp->lines) {
1600                         line = (char *) atResp->lines->data;
1601
1602                         dbg("line is %s", line);
1603
1604                         tokens = tcore_at_tok_new(line);
1605                         pResp = g_slist_nth_data(tokens, 0);
1606                         if (pResp != NULL) {
1607                                 sw1 = atoi(pResp);
1608                         } else {
1609                                 dbg("sw1 is NULL");
1610                         }
1611                         pResp = g_slist_nth_data(tokens, 1);
1612                         if (pResp != NULL) {
1613                                 sw2 = atoi(pResp);
1614                                 if ((sw1 == 144) && (sw2 == 0)) {
1615                                         respGetParamCnt.result = SMS_SENDSMS_SUCCESS;
1616                                 }
1617                         } else {
1618                                 dbg("sw2 is NULL");
1619                         }
1620                         pResp = g_slist_nth_data(tokens, 2);
1621                         if (pResp != NULL) {
1622                                 char *hexData = NULL;
1623                                 char *recordData = NULL;
1624                                 hexData = util_removeQuotes(pResp);
1625
1626                                 /*1. SIM access success case*/
1627                                 if ((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
1628                                         unsigned char tag_len = 0; /*   1 or 2 bytes ??? */
1629                                         int record_len = 0;
1630                                         char num_of_records = 0;
1631                                         unsigned char file_id_len = 0;
1632                                         unsigned short file_id = 0;
1633                                         unsigned short file_size = 0;
1634                                         unsigned short file_type = 0;
1635                                         unsigned short arr_file_id = 0;
1636                                         int arr_file_id_rec_num = 0;
1637
1638                                         /*      handling only last 3 bits */
1639                                         unsigned char file_type_tag = 0x07;
1640                                         unsigned char *ptr_data;
1641
1642                                         recordData = util_hexStringToBytes(hexData);
1643                                         util_hex_dump("    ", strlen(hexData)/2, recordData);
1644
1645                                         ptr_data = (unsigned char *)recordData;
1646
1647                                         co_sim = tcore_plugin_ref_core_object(tcore_pending_ref_plugin(p), CORE_OBJECT_TYPE_SIM);
1648                                         sim_type = tcore_sim_get_type(co_sim);
1649                                         dbg("sim type is %d",sim_type);
1650
1651                                         if (sim_type ==  SIM_TYPE_USIM) {
1652                                                 /*
1653                                                  ETSI TS 102 221 v7.9.0
1654                                                         - Response Data
1655                                                          '62'   FCP template tag
1656                                                          - Response for an EF
1657                                                          '82'   M       File Descriptor
1658                                                          '83'   M       File Identifier
1659                                                         'A5'    O       Proprietary information
1660                                                          '8A'   M       Life Cycle Status Integer
1661                                                          '8B', '8C' or 'AB'     C1      Security attributes
1662                                                         '80'    M       File size
1663                                                          '81'   O       Total file size
1664                                                          '88'   O       Short File Identifier (SFI)
1665                                                 */
1666
1667                                                 /* rsim.res_len  has complete data length received  */
1668
1669                                                 /* FCP template tag - File Control Parameters tag*/
1670                                                 if (*ptr_data == 0x62) {
1671                                                         /* parse complete FCP tag*/
1672                                                         /* increment to next byte */
1673                                                         ptr_data++;
1674                                                         tag_len = *ptr_data++;
1675                                                         /* FCP file descriptor - file type, accessibility, DF, ADF etc*/
1676                                                         if (*ptr_data == 0x82) {
1677                                                                         /* increment to next byte */
1678                                                                         ptr_data++;
1679                                                                         /*2 or 5 value*/
1680                                                                         ptr_data++;
1681                                                         /*      unsigned char file_desc_len = *ptr_data++;*/
1682                                                         /*      dbg("file descriptor length: [%d]", file_desc_len);*/
1683                                                         /* TBD:  currently capture only file type : ignore sharable, non sharable, working, internal etc*/
1684                                                         /* consider only last 3 bits*/
1685                                                         file_type_tag = file_type_tag & (*ptr_data);
1686
1687                                                         switch (file_type_tag) {
1688                                                                 /* increment to next byte */
1689                                                                 ptr_data++;
1690
1691                                                                 case 0x1:
1692                                                                         dbg("Getting FileType: [Transparent file type]");
1693                                                                         /* increment to next byte */
1694                                                                         ptr_data++;
1695                                                                         file_type = 0x01;       //SIM_FTYPE_TRANSPARENT
1696                                                                         /*      data coding byte - value 21 */
1697                                                                         ptr_data++;
1698                                                                         break;
1699
1700                                                                 case 0x2:
1701                                                                         dbg("Getting FileType: [Linear fixed file type]");
1702                                                                         /* increment to next byte */
1703                                                                         ptr_data++;
1704                                                                         /*      data coding byte - value 21 */
1705                                                                         ptr_data++;
1706                                                                         /*      2bytes */
1707                                                                         memcpy(&record_len, ptr_data, 2);
1708                                                                         /* swap bytes */
1709                                                                         record_len = SMS_SWAPBYTES16(record_len);
1710                                                                         ptr_data = ptr_data + 2;
1711                                                                         num_of_records = *ptr_data++;
1712                                                                         /* Data lossy conversation from enum (int) to unsigned char */
1713                                                                         file_type = 0x02;       // SIM_FTYPE_LINEAR_FIXED
1714                                                                         break;
1715
1716                                                                 case 0x6:
1717                                                                         dbg(" Cyclic fixed file type");
1718                                                                         /* increment to next byte */
1719                                                                         ptr_data++;
1720                                                                         /*      data coding byte - value 21 */
1721                                                                         ptr_data++;
1722                                                                         /*      2bytes */
1723                                                                         memcpy(&record_len, ptr_data, 2);
1724                                                                         /* swap bytes  */
1725                                                                         record_len = SMS_SWAPBYTES16(record_len);
1726                                                                         ptr_data = ptr_data + 2;
1727                                                                         num_of_records = *ptr_data++;
1728                                                                         file_type = 0x04;       //SIM_FTYPE_CYCLIC
1729                                                                         break;
1730
1731                                                                 default:
1732                                                                         dbg("not handled file type [0x%x]", *ptr_data);
1733                                                                         break;
1734                                                                 }
1735                                                         } else {
1736                                                                 dbg("INVALID FCP received - DEbug!");
1737                                                                 g_free(hexData);
1738                                                                 g_free(recordData);
1739                                                                 tcore_at_tok_free(tokens);
1740                                                                 return;
1741                                                         }
1742
1743                                                         /*File identifier - file id?? */ // 0x84,0x85,0x86 etc are currently ignored and not handled
1744                                                         if (*ptr_data == 0x83) {
1745                                                                 /* increment to next byte */
1746                                                                 ptr_data++;
1747                                                                 file_id_len = *ptr_data++;
1748                                                                 memcpy(&file_id, ptr_data, file_id_len);
1749                                                                 /* swap bytes    */
1750                                                                 file_id = SMS_SWAPBYTES16(file_id);
1751                                                                 ptr_data = ptr_data + 2;
1752                                                                 dbg("Getting FileID=[0x%x]", file_id);
1753                                                         } else {
1754                                                                 dbg("INVALID FCP received - DEbug!");
1755                                                                 g_free(hexData);
1756                                                                 g_free(recordData);
1757                                                                 tcore_at_tok_free(tokens);
1758                                                                 return;
1759                                                         }
1760
1761                                                         /*      proprietary information  */
1762                                                         if (*ptr_data == 0xA5) {
1763                                                                 unsigned short prop_len;
1764                                                                 /* increment to next byte */
1765                                                                 ptr_data++;
1766                                                                 /* length */
1767                                                                 prop_len = *ptr_data;
1768                                                                 /* skip data */
1769                                                                 ptr_data = ptr_data + prop_len + 1;
1770                                                         } else {
1771                                                                 dbg("INVALID FCP received - DEbug!");
1772                                                         }
1773
1774                                                         /* life cycle status integer [8A][length:0x01][status]*/
1775                                                         /*
1776                                                          status info b8~b1
1777                                                          00000000 : No information given
1778                                                          00000001 : creation state
1779                                                          00000011 : initialization state
1780                                                          000001-1 : operation state -activated
1781                                                          000001-0 : operation state -deactivated
1782                                                          000011-- : Termination state
1783                                                          b8~b5 !=0, b4~b1=X : Proprietary
1784                                                          Any other value : RFU
1785                                                          */
1786                                                         if (*ptr_data == 0x8A) {
1787                                                                 /* increment to next byte */
1788                                                                 ptr_data++;
1789                                                                 /* length - value 1 */
1790                                                                 ptr_data++;
1791
1792                                                                 switch (*ptr_data) {
1793                                                                         case 0x04:
1794                                                                         case 0x06:
1795                                                                                 dbg("[RX] Operation State: DEACTIVATED");
1796                                                                                 ptr_data++;
1797                                                                                 break;
1798
1799                                                                         case 0x05:
1800                                                                         case 0x07:
1801                                                                                 dbg("[RX] Operation State: ACTIVATED");
1802                                                                                 ptr_data++;
1803                                                                                 break;
1804
1805                                                                         default:
1806                                                                                 dbg("[RX] DEBUG! LIFE CYCLE STATUS: [0x%x]",*ptr_data);
1807                                                                                 ptr_data++;
1808                                                                                 break;
1809                                                                 }
1810                                                         }
1811
1812                                                         /* related to security attributes : currently not handled*/
1813                                                         if (*ptr_data == 0x86 || *ptr_data == 0x8B || *ptr_data == 0x8C || *ptr_data == 0xAB) {
1814                                                                 /* increment to next byte */
1815                                                                 ptr_data++;
1816                                                                 /* if tag length is 3 */
1817                                                                 if (*ptr_data == 0x03) {
1818                                                                         /* increment to next byte */
1819                                                                         ptr_data++;
1820                                                                         /* EFARR file id */
1821                                                                         memcpy(&arr_file_id, ptr_data, 2);
1822                                                                         /* swap byes */
1823                                                                         arr_file_id = SMS_SWAPBYTES16(arr_file_id);
1824                                                                         ptr_data = ptr_data + 2;
1825                                                                         arr_file_id_rec_num = *ptr_data++;
1826                                                                 } else {
1827                                                                         /* if tag length is not 3 */
1828                                                                         /* ignoring bytes       */
1829                                                                         //      ptr_data = ptr_data + 4;
1830                                                                         dbg("Useless security attributes, so jump to next tag");
1831                                                                         ptr_data = ptr_data + (*ptr_data + 1);
1832                                                                 }
1833                                                         } else {
1834                                                                 dbg("INVALID FCP received[0x%x] - DEbug!", *ptr_data);
1835                                                                 g_free(hexData);
1836                                                                 g_free(recordData);
1837                                                                 tcore_at_tok_free(tokens);
1838                                                                 return;
1839                                                         }
1840
1841                                                         dbg("Current ptr_data value is [%x]", *ptr_data);
1842
1843                                                         /* file size excluding structural info*/
1844                                                         if (*ptr_data == 0x80) {
1845                                                                 /* for EF file size is body of file and for Linear or cyclic it is
1846                                                                  * number of recXsizeof(one record)
1847                                                                  */
1848                                                                 /* increment to next byte */
1849                                                                 ptr_data++;
1850                                                                 /* length is 1 byte - value is 2 bytes or more */
1851                                                                 ptr_data++;
1852                                                                 memcpy(&file_size, ptr_data, 2);
1853                                                                 /* swap bytes */
1854                                                                 file_size = SMS_SWAPBYTES16(file_size);
1855                                                                 ptr_data = ptr_data + 2;
1856                                                         } else {
1857                                                                 dbg("INVALID FCP received - DEbug!");
1858                                                                 g_free(hexData);
1859                                                                 g_free(recordData);
1860                                                                 tcore_at_tok_free(tokens);
1861                                                                 return;
1862                                                         }
1863
1864                                                         /* total file size including structural info*/
1865                                                         if (*ptr_data == 0x81) {
1866                                                                 int len;
1867                                                                 /* increment to next byte */
1868                                                                 ptr_data++;
1869                                                                 /* length */
1870                                                                 len = *ptr_data;
1871                                                                 /* ignored bytes */
1872                                                                 ptr_data = ptr_data + 3;
1873                                                         } else {
1874                                                                 dbg("INVALID FCP received - DEbug!");
1875                                                                 /* 0x81 is optional tag?? check out! so do not return -1 from here! */
1876                                                                 /* return -1; */
1877                                                         }
1878                                                         /*short file identifier ignored*/
1879                                                         if (*ptr_data == 0x88) {
1880                                                                 dbg("0x88: Do Nothing");
1881                                                                 /*DO NOTHING*/
1882                                                         }
1883                                                 } else {
1884                                                         dbg("INVALID FCP received - DEbug!");
1885                                                         g_free(hexData);
1886                                                         g_free(recordData);
1887                                                         tcore_at_tok_free(tokens);
1888                                                         return;
1889                                                 }
1890                                         } else if (sim_type == SIM_TYPE_GSM) {
1891                                                 unsigned char gsm_specific_file_data_len = 0;
1892                                                 /*      ignore RFU byte1 and byte2 */
1893                                                 ptr_data++;
1894                                                 ptr_data++;
1895                                                 /*      file size */
1896                                                 //file_size = p_info->response_len;
1897                                                 memcpy(&file_size, ptr_data, 2);
1898                                                 /* swap bytes */
1899                                                 file_size = SMS_SWAPBYTES16(file_size);
1900                                                 /*      parsed file size */
1901                                                 ptr_data = ptr_data + 2;
1902                                                 /*  file id  */
1903                                                 memcpy(&file_id, ptr_data, 2);
1904                                                 file_id = SMS_SWAPBYTES16(file_id);
1905                                                 dbg(" FILE id --> [%x]", file_id);
1906                                                 ptr_data = ptr_data + 2;
1907                                                 /* save file type - transparent, linear fixed or cyclic */
1908                                                 file_type_tag = (*(ptr_data + 7));
1909
1910                                                 switch (*ptr_data) {
1911                                                         case 0x0:
1912                                                                 /* RFU file type */
1913                                                                 dbg(" RFU file type- not handled - Debug!");
1914                                                                 break;
1915
1916                                                         case 0x1:
1917                                                                 /* MF file type */
1918                                                                 dbg(" MF file type - not handled - Debug!");
1919                                                                 break;
1920
1921                                                         case 0x2:
1922                                                                 /* DF file type */
1923                                                                 dbg(" DF file type - not handled - Debug!");
1924                                                                 break;
1925
1926                                                         case 0x4:
1927                                                                 /* EF file type */
1928                                                                 dbg(" EF file type [%d] ", file_type_tag);
1929                                                                 /*      increment to next byte */
1930                                                                 ptr_data++;
1931
1932                                                                 if (file_type_tag == 0x00 || file_type_tag == 0x01) {
1933                                                                         /* increament to next byte as this byte is RFU */
1934                                                                         ptr_data++;
1935                                                                         file_type =
1936                                                                                         (file_type_tag == 0x00) ? 0x01 : 0x02; // SIM_FTYPE_TRANSPARENT:SIM_FTYPE_LINEAR_FIXED;
1937                                                                 } else {
1938                                                                         /* increment to next byte */
1939                                                                         ptr_data++;
1940                                                                         /*      For a cyclic EF all bits except bit 7 are RFU; b7=1 indicates that */
1941                                                                         /* the INCREASE command is allowed on the selected cyclic file. */
1942                                                                         file_type = 0x04;       // SIM_FTYPE_CYCLIC;
1943                                                                 }
1944                                                                 /* bytes 9 to 11 give SIM file access conditions */
1945                                                                 ptr_data++;
1946                                                                 /* byte 10 has one nibble that is RF U and another for INCREASE which is not used currently */
1947                                                                 ptr_data++;
1948                                                                 /* byte 11 is invalidate and rehabilate nibbles */
1949                                                                 ptr_data++;
1950                                                                 /* byte 12 - file status */
1951                                                                 ptr_data++;
1952                                                                 /* byte 13 - GSM specific data */
1953                                                                 gsm_specific_file_data_len = *ptr_data;
1954                                                                 ptr_data++;
1955                                                                 /*      byte 14 - structure of EF - transparent or linear or cyclic , already saved above */
1956                                                                 ptr_data++;
1957                                                                 /* byte 15 - length of record for linear and cyclic , for transparent it is set to 0x00. */
1958                                                                 record_len = *ptr_data;
1959                                                                 dbg("record length[%d], file size[%d]", record_len, file_size);
1960
1961                                                                 if (record_len != 0)
1962                                                                         num_of_records = (file_size / record_len);
1963
1964                                                                 dbg("Number of records [%d]", num_of_records);
1965                                                                 break;
1966
1967                                                         default:
1968                                                                 dbg(" not handled file type");
1969                                                                 break;
1970                                                 }
1971                                         } else {
1972                                                 dbg(" Card Type - UNKNOWN  [%d]", sim_type);
1973                                         }
1974
1975                                         dbg("EF[0x%x] size[%ld] Type[0x%x] NumOfRecords[%ld] RecordLen[%ld]", file_id, file_size, file_type, num_of_records, record_len);
1976
1977                                         respGetParamCnt.recordCount = num_of_records;
1978                                         respGetParamCnt.result = SMS_SUCCESS;
1979
1980                                         //TO Store smsp record length in the property
1981                                         plugin = tcore_pending_ref_plugin(p);
1982                                         smsp_record_len = tcore_plugin_ref_property(plugin, "SMSPRECORDLEN");
1983                                         memcpy(smsp_record_len, &record_len, sizeof(int));
1984
1985                                         g_free(recordData);
1986                                         g_free(hexData);
1987                                 } else {
1988                                         /*2. SIM access fail case*/
1989                                         dbg("SIM access fail");
1990                                         respGetParamCnt.result = SMS_UNKNOWN;
1991                                 }
1992                         } else {
1993                                 dbg("presp is NULL");
1994                         }
1995                 } else {
1996                         dbg("line is blank");
1997                 }
1998         } else {
1999                 dbg("RESPONSE NOK");
2000         }
2001
2002         tcore_user_request_send_response(ur, TRESP_SMS_GET_PARAMCNT, sizeof(struct tresp_sms_get_paramcnt), &respGetParamCnt);
2003
2004         if(tokens)
2005                 tcore_at_tok_free(tokens);
2006
2007         dbg("Exit");
2008         return;
2009 }
2010
2011 static void _response_get_efsms_data(TcorePending *p, int data_len, const void *data, void *user_data)
2012 {
2013         UserRequest *ur = NULL;
2014         UserRequest *dup_ur = NULL;
2015         struct tresp_sms_set_msg_status resp_msg_status = {0,};
2016         const struct treq_sms_set_msg_status *req_msg_status = NULL ;
2017
2018         const TcoreATResponse *resp = data;
2019         char *encoded_data = NULL;
2020         char msg_status = 0;
2021         char *pResp = NULL;
2022         GSList *tokens=NULL;
2023         const char *line = NULL;
2024         int sw1 = 0;
2025         int sw2 = 0;
2026
2027         TcoreHal *hal = NULL;
2028         TcoreATRequest *atreq = NULL;
2029         TcorePending *pending = NULL;
2030         gchar *cmd_str = NULL;
2031
2032         ur = tcore_pending_ref_user_request(p);
2033
2034         req_msg_status = tcore_user_request_ref_data(ur, NULL);
2035
2036         resp_msg_status.result = SMS_DEVICE_FAILURE;
2037
2038         hal = tcore_object_get_hal(tcore_pending_ref_core_object(pending));
2039         dbg("msgStatus: [%x], index [%x]", req_msg_status->msgStatus, req_msg_status->index);
2040
2041         if (resp->success <= 0) {
2042                 goto OUT;
2043         }
2044
2045         {
2046                 dbg("RESPONSE OK");
2047                 if (resp->lines) {
2048                         line = (const char *) resp->lines->data;
2049                         tokens = tcore_at_tok_new(line);
2050                         if (g_slist_length(tokens) != 3) {
2051                                 msg("invalid message");
2052                                 goto OUT;
2053                         }
2054                 }
2055                 sw1 = atoi(g_slist_nth_data(tokens, 0));
2056                 sw2 = atoi(g_slist_nth_data(tokens, 1));
2057                 pResp = g_slist_nth_data(tokens, 2);
2058
2059                 if ((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
2060                         switch (req_msg_status->msgStatus) {
2061                                 case SMS_STATUS_READ:
2062                                         msg_status = 0x01;
2063                                         break;
2064
2065                                 case SMS_STATUS_UNREAD:
2066                                         msg_status = 0x03;
2067                                         break;
2068
2069                                 case SMS_STATUS_UNSENT:
2070                                         msg_status = 0x07;
2071                                         break;
2072
2073                                 case SMS_STATUS_SENT:
2074                                         msg_status = 0x05;
2075                                         break;
2076
2077                                 case SMS_STATUS_DELIVERED:
2078                                         msg_status = 0x1D;
2079                                         break;
2080
2081                                 case SMS_STATUS_DELIVERY_UNCONFIRMED:
2082                                         msg_status = 0xD;
2083                                         break;
2084
2085                                 case SMS_STATUS_MESSAGE_REPLACED:
2086                                 case SMS_STATUS_RESERVED:
2087                                 default:
2088                                         msg_status = 0x03;
2089                                         break;
2090                         }
2091
2092                         encoded_data = util_removeQuotes(pResp);
2093
2094                         //overwrite Status byte information
2095                         util_byte_to_hex((const char *)&msg_status, (char *)encoded_data, 1);
2096
2097                         //Update EF-SMS with just status byte overwritten, rest 175 bytes are same as received in read information
2098                         cmd_str = g_strdup_printf("AT+CRSM=220,28476,%d, 4, %d, \"%s\"", (req_msg_status->index+1), PDU_LEN_MAX, encoded_data);
2099                         atreq = tcore_at_request_new((const char *)cmd_str, "+CRSM", TCORE_AT_SINGLELINE);
2100                         pending = tcore_pending_new(tcore_pending_ref_core_object(pending), 0);
2101                         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2102                                 err("Out of memory. Unable to proceed");
2103                                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2104
2105                                 //free memory we own
2106                                 g_free(cmd_str);
2107                                 g_free(encoded_data);
2108                                 util_sms_free_memory(atreq);
2109                                 util_sms_free_memory(pending);
2110
2111                                 goto OUT;
2112                         }
2113
2114                         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2115
2116                         dup_ur = tcore_user_request_ref(ur);
2117
2118                         tcore_pending_set_request_data(pending, 0, atreq);
2119                         tcore_pending_set_response_callback(pending, on_response_set_msg_status, NULL);
2120                         tcore_pending_link_user_request(pending, dup_ur);
2121                         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2122                         tcore_hal_send_request(hal, pending);
2123
2124                         g_free(cmd_str);
2125                         g_free(encoded_data);
2126                 }
2127         }
2128
2129 OUT:
2130         if(tokens)
2131                 tcore_at_tok_free(tokens);
2132
2133         tcore_user_request_send_response(ur, TRESP_SMS_SET_MSG_STATUS , sizeof(struct tresp_sms_set_msg_status), &msg_status);
2134
2135         dbg("Exit");
2136
2137         return;
2138 }
2139
2140 /*=============================================================
2141                                                         Requests
2142 ==============================================================*/
2143 static TReturn send_umts_msg(CoreObject *co_sms, UserRequest *ur)
2144 {
2145         const struct treq_sms_send_umts_msg *send_msg;
2146         const unsigned char *tpdu_byte_data, *sca_byte_data;
2147         int tpdu_byte_len, pdu_byte_len;
2148         char buf[HEX_PDU_LEN_MAX];
2149         char pdu[PDU_LEN_MAX];
2150         char *cmd_str;
2151         int pdu_hex_len, mms;
2152         TReturn ret;
2153
2154         dbg("Enter");
2155
2156         send_msg = tcore_user_request_ref_data(ur, NULL);
2157
2158         tpdu_byte_data = send_msg->msgDataPackage.tpduData;
2159         sca_byte_data = send_msg->msgDataPackage.sca;
2160
2161
2162         /* TPDU length is in byte */
2163         tpdu_byte_len = send_msg->msgDataPackage.msgLength;
2164
2165         /* Use same Radio Resource Channel */
2166         mms = send_msg->more;
2167
2168         dbg("TDPU length: [%d]", tpdu_byte_len);
2169         dbg("SCA semi-octet length: [%d]", sca_byte_data[0]);
2170
2171         /* Prepare PDU for hex encoding */
2172         pdu_byte_len = tcore_util_pdu_encode(sca_byte_data, tpdu_byte_data,
2173                                                 tpdu_byte_len, pdu);
2174
2175         pdu_hex_len = (int) tcore_util_encode_hex((unsigned char *) pdu,
2176                                                 pdu_byte_len, buf);
2177
2178         dbg("PDU hexadecimal length: [%d]", pdu_hex_len);
2179
2180         if (mms > 0) {
2181                 cmd_str = g_strdup_printf("AT+CMMS=%d", mms);
2182
2183                 ret = tcore_prepare_and_send_at_request(co_sms, cmd_str, NULL,
2184                                         TCORE_AT_NO_RESULT, NULL, NULL, NULL,
2185                                         on_confirmation_sms_message_send,
2186                                         NULL);
2187                 if (ret != TCORE_RETURN_SUCCESS) {
2188                         err("Failed to prepare and send AT request");
2189                         goto error;
2190                 }
2191
2192                 g_free(cmd_str);
2193         }
2194
2195         cmd_str = g_strdup_printf("AT+CMGS=%d\r%s\x1A", tpdu_byte_len, buf);
2196
2197         ret = tcore_prepare_and_send_at_request(co_sms, cmd_str, "+CMGS:",
2198                                 TCORE_AT_SINGLELINE, ur,
2199                                 on_response_send_umts_msg, NULL,
2200                                 on_confirmation_sms_message_send, NULL);
2201         if (ret != TCORE_RETURN_SUCCESS)
2202                 err("Failed to prepare and send AT request");
2203
2204 error:
2205         g_free(cmd_str);
2206
2207         dbg("Exit");
2208
2209         return ret;
2210 }
2211
2212 static TReturn read_msg(CoreObject *obj, UserRequest *ur)
2213 {
2214         gchar *cmd_str = NULL;
2215         TcoreHal *hal = NULL;
2216         TcoreATRequest *atreq = NULL;
2217         TcorePending *pending = NULL;
2218         const struct treq_sms_read_msg *readMsg = NULL;
2219
2220         dbg("Entry");
2221
2222         readMsg = tcore_user_request_ref_data(ur, NULL);
2223         hal = tcore_object_get_hal(obj);
2224         if (NULL == readMsg || NULL == hal) {
2225                 err("NULL input. Unable to proceed");
2226                 dbg("readMsg: [%p], hal: [%p]", readMsg, hal);
2227
2228                 dbg("Exit");
2229                 return TCORE_RETURN_EINVAL;
2230         }
2231
2232         if(FALSE == tcore_hal_get_power_state(hal)){
2233                 dbg("cp not ready/n");
2234                 return TCORE_RETURN_ENOSYS;
2235         }
2236         dbg("index: [%d]", readMsg->index);
2237
2238         cmd_str = g_strdup_printf("AT+CMGR=%d", (readMsg->index + 1)); //IMC index is one ahead of TAPI
2239         atreq = tcore_at_request_new((const char *)cmd_str, "+CMGR", TCORE_AT_PDU);
2240         pending = tcore_pending_new(obj, 0);
2241
2242         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2243                 err("Out of memory. Unable to proceed");
2244                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2245
2246                 //free memory we own
2247                 g_free(cmd_str);
2248                 util_sms_free_memory(atreq);
2249                 util_sms_free_memory(pending);
2250
2251                 dbg("Exit");
2252                 return TCORE_RETURN_ENOMEM;
2253         }
2254
2255         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2256
2257         tcore_pending_set_request_data(pending, 0, atreq);
2258         tcore_pending_set_response_callback(pending, on_response_read_msg, (void *)(uintptr_t)(readMsg->index)); //storing index as user data for response
2259         tcore_pending_link_user_request(pending, ur);
2260         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2261         tcore_hal_send_request(hal, pending);
2262
2263         g_free(cmd_str);
2264
2265         dbg("Exit");
2266         return TCORE_RETURN_SUCCESS;
2267 }
2268
2269 static TReturn save_msg(CoreObject *obj, UserRequest *ur)
2270 {
2271         gchar *cmd_str = NULL;
2272         TcoreHal *hal = NULL;
2273         TcoreATRequest *atreq = NULL;
2274         TcorePending *pending = NULL;
2275         const struct treq_sms_save_msg *saveMsg = NULL;
2276         int ScLength = 0, pdu_len = 0, stat = 0;
2277         char buf[2*(SMS_SMSP_ADDRESS_LEN+SMS_SMDATA_SIZE_MAX)+1] = {0};
2278         char *hex_pdu = NULL;
2279
2280         dbg("Entry");
2281
2282         saveMsg = tcore_user_request_ref_data(ur, NULL);
2283         hal = tcore_object_get_hal(obj);
2284         if (NULL == saveMsg || NULL == hal) {
2285                 err("NULL input. Unable to proceed");
2286                 dbg("saveMsg: [%p], hal: [%p]", saveMsg, hal);
2287
2288                 dbg("Exit");
2289                 return TCORE_RETURN_EINVAL;
2290         }
2291         if(FALSE == tcore_hal_get_power_state(hal)){
2292                 dbg("cp not ready/n");
2293                 return TCORE_RETURN_ENOSYS;
2294         }
2295
2296         dbg("msgStatus: %x, msgLength: [%d]", saveMsg->msgStatus, saveMsg->msgDataPackage.msgLength);
2297         util_hex_dump("    ", (SMS_SMDATA_SIZE_MAX+1), (void *)saveMsg->msgDataPackage.tpduData);
2298         util_hex_dump("    ", SMS_SMSP_ADDRESS_LEN, (void *)saveMsg->msgDataPackage.sca);
2299
2300         switch (saveMsg->msgStatus) {
2301                 case SMS_STATUS_READ:
2302                         stat = AT_REC_READ;
2303                         break;
2304
2305                 case SMS_STATUS_UNREAD:
2306                         stat = AT_REC_UNREAD;
2307                         break;
2308
2309                 case SMS_STATUS_SENT:
2310                         stat = AT_STO_SENT;
2311                         break;
2312
2313                 case SMS_STATUS_UNSENT:
2314                         stat = AT_STO_UNSENT;
2315                         break;
2316
2317                 default:
2318                         err("Invalid msgStatus");
2319                         dbg("Exit");
2320                         return TCORE_RETURN_EINVAL;
2321         }
2322
2323         if ((saveMsg->msgDataPackage.msgLength > 0)
2324                 && (saveMsg->msgDataPackage.msgLength <= SMS_SMDATA_SIZE_MAX)) {
2325                 ScLength = (int)saveMsg->msgDataPackage.sca[0];
2326
2327                 buf[0] = ScLength;
2328                 dbg("ScLength = %d", ScLength);
2329
2330                 if(ScLength == 0) {
2331                         buf[0] = 0;
2332                 } else {
2333                         memcpy(&buf[1],  saveMsg->msgDataPackage.sca, ScLength);
2334                 }
2335
2336                 memcpy(&buf[ScLength+1],  saveMsg->msgDataPackage.tpduData, saveMsg->msgDataPackage.msgLength);
2337
2338                 pdu_len= saveMsg->msgDataPackage.msgLength + ScLength + 1;
2339                 dbg("pdu_len: [%d]", pdu_len);
2340
2341                 hex_pdu = malloc(pdu_len * 2 + 1);
2342                 util_hex_dump("    ", sizeof(buf), (void *)buf);
2343
2344                 memset (hex_pdu, 0x00, pdu_len * 2 + 1);
2345
2346                 util_byte_to_hex((const char *)buf, (char *)hex_pdu, pdu_len);
2347
2348                 //AT+CMGW=<length>[,<stat>]<CR>PDU is given<ctrl-Z/ESC>
2349                 cmd_str = g_strdup_printf("AT+CMGW=%d,%d%s%s\x1A", saveMsg->msgDataPackage.msgLength, stat, "\r", hex_pdu);
2350                 pending = tcore_pending_new(obj, 0);
2351                 atreq = tcore_at_request_new((const char *)cmd_str, "+CMGW", TCORE_AT_SINGLELINE);
2352
2353                 if(NULL == cmd_str || NULL == atreq || NULL == pending) {
2354                         err("Out of memory. Unable to proceed");
2355                         dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2356
2357                         //free memory we own
2358                         g_free(cmd_str);
2359                         util_sms_free_memory(atreq);
2360                         util_sms_free_memory(pending);
2361                         util_sms_free_memory(hex_pdu);
2362
2363                         dbg("Exit");
2364                         return TCORE_RETURN_ENOMEM;
2365                 }
2366
2367                 util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2368
2369                 tcore_pending_set_request_data(pending, 0, atreq);
2370                 tcore_pending_set_response_callback(pending, on_response_sms_save_msg, NULL);
2371                 tcore_pending_link_user_request(pending, ur);
2372                 tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2373                 tcore_hal_send_request(hal, pending);
2374
2375                 g_free(cmd_str);
2376                 free(hex_pdu);
2377
2378                 dbg("Exit");
2379                 return TCORE_RETURN_SUCCESS;
2380         }
2381
2382         err("Invalid Data len");
2383         dbg("Exit");
2384         return TCORE_RETURN_SMS_INVALID_DATA_LEN;
2385 }
2386
2387 static TReturn delete_msg(CoreObject *obj, UserRequest *ur)
2388 {
2389         gchar *cmd_str = NULL;
2390         TcoreHal *hal = NULL;
2391         TcoreATRequest *atreq = NULL;
2392         TcorePending *pending = NULL;
2393         const struct treq_sms_delete_msg *delete_msg = NULL;
2394
2395         dbg("Entry");
2396
2397         delete_msg = tcore_user_request_ref_data(ur, NULL);
2398         hal = tcore_object_get_hal(obj);
2399         if (NULL == delete_msg || NULL == hal) {
2400                 err("NULL input. Unable to proceed");
2401                 dbg("deleteMsg: [%p], hal: [%p]", delete_msg, hal);
2402
2403                 dbg("Exit");
2404                 return TCORE_RETURN_EINVAL;
2405         }
2406
2407         if(FALSE == tcore_hal_get_power_state(hal)){
2408                 dbg("cp not ready/n");
2409                 return TCORE_RETURN_ENOSYS;
2410         }
2411
2412         dbg("index: %d", delete_msg->index);
2413
2414         if (delete_msg->index == -1) {
2415                 cmd_str = g_strdup_printf("AT+CMGD=0,4"); // Delete All Messages
2416         } else {
2417                 cmd_str = g_strdup_printf("AT+CMGD=%d,0", delete_msg->index + 1); // Delete specified index
2418         }
2419
2420         pending = tcore_pending_new(obj, 0);
2421         atreq = tcore_at_request_new((const char *)cmd_str, NULL, TCORE_AT_NO_RESULT);
2422         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2423                 err("Out of memory. Unable to proceed");
2424                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2425
2426                 //free memory we own
2427                 g_free(cmd_str);
2428                 util_sms_free_memory(atreq);
2429                 util_sms_free_memory(pending);
2430
2431                 dbg("Exit");
2432                 return TCORE_RETURN_ENOMEM;
2433         }
2434
2435         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2436
2437         tcore_pending_set_request_data(pending, 0, atreq);
2438         tcore_pending_set_response_callback(pending, on_response_sms_delete_msg, (void *) (uintptr_t) (delete_msg->index)); // storing index as user data for response
2439         tcore_pending_link_user_request(pending, ur);
2440         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2441         tcore_hal_send_request(hal, pending);
2442
2443         g_free(cmd_str);
2444
2445         dbg("Exit");
2446         return TCORE_RETURN_SUCCESS;
2447 }
2448
2449 static TReturn get_stored_msg_cnt(CoreObject *obj, UserRequest *ur)
2450 {
2451         gchar *cmd_str = NULL;
2452         TcoreHal *hal = NULL;
2453         TcoreATRequest *atreq = NULL;
2454         TcorePending *pending = NULL;
2455
2456         dbg("Entry");
2457
2458         hal = tcore_object_get_hal(obj);
2459         if (NULL == hal) {
2460                 err("NULL HAL. Unable to proceed");
2461
2462                 dbg("Exit");
2463                 return TCORE_RETURN_EINVAL;
2464         }
2465
2466         if(FALSE == tcore_hal_get_power_state(hal)){
2467                 dbg("cp not ready/n");
2468                 return TCORE_RETURN_ENOSYS;
2469         }
2470
2471         cmd_str = g_strdup_printf("AT+CPMS=\"SM\"");
2472         pending = tcore_pending_new(obj, 0);
2473         atreq = tcore_at_request_new((const char *)cmd_str, "+CPMS", TCORE_AT_SINGLELINE);
2474
2475         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2476                 err("Out of memory. Unable to proceed");
2477                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2478
2479                 //free memory we own
2480                 g_free(cmd_str);
2481                 util_sms_free_memory(atreq);
2482                 util_sms_free_memory(pending);
2483
2484                 dbg("Exit");
2485                 return TCORE_RETURN_ENOMEM;
2486         }
2487
2488         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2489
2490         tcore_pending_set_request_data(pending, 0, atreq);
2491         tcore_pending_set_response_callback(pending, on_response_get_stored_msg_cnt, NULL);
2492         tcore_pending_link_user_request(pending, ur);
2493         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2494         tcore_hal_send_request(hal, pending);
2495
2496         g_free(cmd_str);
2497
2498         dbg("Exit");
2499         return TCORE_RETURN_SUCCESS;
2500 }
2501
2502 static TReturn get_sca(CoreObject *obj, UserRequest *ur)
2503 {
2504         gchar * cmd_str = NULL;
2505         TcoreHal *hal = NULL;
2506         TcoreATRequest *atreq = NULL;
2507         TcorePending *pending = NULL;
2508
2509         dbg("Entry");
2510
2511         hal = tcore_object_get_hal(obj);
2512         if (NULL == hal) {
2513                 err("HAL NULL. Unable to proceed");
2514
2515                 dbg("Exit");
2516                 return TCORE_RETURN_EINVAL;
2517         }
2518         if(FALSE == tcore_hal_get_power_state(hal)){
2519                 dbg("cp not ready/n");
2520                 return TCORE_RETURN_ENOSYS;
2521         }
2522
2523         cmd_str = g_strdup_printf("AT+CSCA?");
2524         pending = tcore_pending_new(obj, 0);
2525         atreq = tcore_at_request_new((const char *)cmd_str, "+CSCA", TCORE_AT_SINGLELINE);
2526
2527         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2528                 err("Out of memory. Unable to proceed");
2529                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2530
2531                 //free memory we own
2532                 g_free(cmd_str);
2533                 util_sms_free_memory(atreq);
2534                 util_sms_free_memory(pending);
2535
2536                 dbg("Exit");
2537                 return TCORE_RETURN_ENOMEM;
2538         }
2539
2540         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2541
2542         tcore_pending_set_request_data(pending, 0, atreq);
2543         tcore_pending_set_response_callback(pending, on_response_get_sca, NULL);
2544         tcore_pending_link_user_request(pending, ur);
2545         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2546         tcore_hal_send_request(hal, pending);
2547
2548         g_free(cmd_str);
2549
2550         dbg("Exit");
2551         return TCORE_RETURN_SUCCESS;
2552 }
2553
2554 static TReturn set_sca(CoreObject *obj, UserRequest *ur)
2555 {
2556         gchar *cmd_str = NULL;
2557         TcoreHal *hal = NULL;
2558         TcoreATRequest *atreq = NULL;
2559         TcorePending *pending = NULL;
2560         const struct treq_sms_set_sca *setSca = NULL;
2561         int addrType = 0;
2562
2563         dbg("Entry");
2564
2565         setSca = tcore_user_request_ref_data(ur, NULL);
2566         hal = tcore_object_get_hal(obj);
2567         if (NULL == setSca || NULL == hal) {
2568                 err("NULL input. Unable to proceed");
2569                 dbg("setSca: [%p], hal: [%p]", setSca, hal);
2570
2571                 dbg("Exit");
2572                 return TCORE_RETURN_EINVAL;
2573         }
2574         if(FALSE == tcore_hal_get_power_state(hal)){
2575                 dbg("cp not ready/n");
2576                 return TCORE_RETURN_ENOSYS;
2577         }
2578
2579         dbg("dialNumLen: %u, typeOfNum: %d, numPlanId: %d, ", setSca->scaInfo.dialNumLen, setSca->scaInfo.typeOfNum, setSca->scaInfo.numPlanId);
2580
2581         util_hex_dump("    ", (SMS_SMSP_ADDRESS_LEN+1), (void *)setSca->scaInfo.diallingNum);
2582
2583         addrType = ((setSca->scaInfo.typeOfNum << 4) | setSca->scaInfo.numPlanId) | 0x80;
2584
2585         cmd_str = g_strdup_printf("AT+CSCA=\"%s\",%d", setSca->scaInfo.diallingNum, addrType);
2586         pending = tcore_pending_new(obj, 0);
2587         atreq = tcore_at_request_new((const char *)cmd_str, NULL, TCORE_AT_NO_RESULT);
2588
2589         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2590                 err("Out of memory. Unable to proceed");
2591                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2592
2593                 //free memory we own
2594                 g_free(cmd_str);
2595                 util_sms_free_memory(atreq);
2596                 util_sms_free_memory(pending);
2597
2598                 dbg("Exit");
2599                 return TCORE_RETURN_ENOMEM;
2600         }
2601
2602         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2603
2604         tcore_pending_set_request_data(pending, 0, atreq);
2605         tcore_pending_set_response_callback(pending, on_response_set_sca, NULL);
2606         tcore_pending_link_user_request(pending, ur);
2607         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2608         tcore_hal_send_request(hal, pending);
2609
2610         g_free(cmd_str);
2611
2612         dbg("Exit");
2613         return TCORE_RETURN_SUCCESS;
2614 }
2615
2616 static TReturn get_cb_config(CoreObject *obj, UserRequest *ur)
2617 {
2618         gchar *cmd_str = NULL;
2619         TcoreHal *hal = NULL;
2620         TcoreATRequest *atreq = NULL;
2621         TcorePending *pending = NULL;
2622
2623         dbg("Entry");
2624
2625         hal = tcore_object_get_hal(obj);
2626         if (NULL == hal) {
2627                 err("NULL HAL. Unable to proceed");
2628
2629                 dbg("Exit");
2630                 return TCORE_RETURN_EINVAL;
2631         }
2632         if(FALSE == tcore_hal_get_power_state(hal)){
2633                 dbg("cp not ready/n");
2634                 return TCORE_RETURN_ENOSYS;
2635         }
2636
2637         cmd_str = g_strdup_printf("AT+CSCB?");
2638         pending = tcore_pending_new(obj, 0);
2639         atreq = tcore_at_request_new((const char *)cmd_str, "+CSCB", TCORE_AT_SINGLELINE);
2640         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2641                 err("Out of memory. Unable to proceed");
2642                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2643
2644                 //free memory we own
2645                 g_free(cmd_str);
2646                 util_sms_free_memory(atreq);
2647                 util_sms_free_memory(pending);
2648
2649                 dbg("Exit");
2650                 return TCORE_RETURN_ENOMEM;
2651         }
2652
2653         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2654
2655         tcore_pending_set_request_data(pending, 0, atreq);
2656         tcore_pending_set_response_callback(pending, on_response_get_cb_config, NULL);
2657         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2658         tcore_pending_link_user_request(pending, ur);
2659         tcore_hal_send_request(hal, pending);
2660
2661         g_free(cmd_str);
2662
2663         dbg("Exit");
2664         return TCORE_RETURN_SUCCESS;
2665 }
2666
2667 static TReturn set_cb_config(CoreObject *obj, UserRequest *ur)
2668 {
2669         gchar *cmd_str = NULL;
2670         gchar *mids_str = NULL;
2671         GString *mids_GString = NULL;
2672
2673         TcoreHal *hal = NULL;
2674         TcoreATRequest *atreq = NULL;
2675         TcorePending *pending = NULL;
2676         const struct treq_sms_set_cb_config *setCbConfig = NULL;
2677         int ctr1= 0, ctr2 =0;
2678         unsigned short appendMsgId = 0;
2679
2680         dbg("Entry");
2681
2682         setCbConfig = tcore_user_request_ref_data(ur, NULL);
2683         hal = tcore_object_get_hal(obj);
2684         if (NULL == setCbConfig || NULL == hal) {
2685                 err("NULL input. Unable to proceed");
2686                 dbg("setCbConfig: [%p], hal: [%p]", setCbConfig, hal);
2687
2688                 dbg("Exit");
2689                 return TCORE_RETURN_EINVAL;
2690         }
2691         if(FALSE == tcore_hal_get_power_state(hal)){
2692                 dbg("cp not ready/n");
2693                 return TCORE_RETURN_ENOSYS;
2694         }
2695
2696         dbg("bCBEnabled: %d,  msgIdCount: %d", setCbConfig->cbEnabled, setCbConfig->msgIdRangeCount);
2697
2698         if (setCbConfig->cbEnabled == 2) { //Enable all CBS
2699                 cmd_str = g_strdup_printf("AT+CSCB=1");
2700         } else if ((setCbConfig->cbEnabled == 1) && (setCbConfig->msgIdRangeCount == 0)) { // Special case: Enable all CBS
2701                 cmd_str = g_strdup_printf("AT+CSCB=1");
2702         } else if (setCbConfig->cbEnabled == 0) {//AT+CSCB=0: Disable CBS
2703                 cmd_str = g_strdup_printf("AT+CSCB=0");
2704         } else {
2705                 mids_GString = g_string_new("AT+CSCB=0,\"");
2706
2707                 for(ctr1 = 0; ctr1 < setCbConfig->msgIdRangeCount; ctr1++ ) {
2708                         if( setCbConfig->msgIDs[ctr1].net3gpp.selected == FALSE )
2709                                 continue;
2710
2711                         if( SMS_GSM_SMS_CBMI_LIST_SIZE_MAX <= (setCbConfig->msgIDs[ctr1].net3gpp.toMsgId - setCbConfig->msgIDs[ctr1].net3gpp.fromMsgId) ) {
2712                                 mids_GString = g_string_new("AT+CSCB=1");
2713                                 break;
2714                         }
2715
2716                         appendMsgId = setCbConfig->msgIDs[ctr1].net3gpp.fromMsgId;
2717
2718                         for( ctr2 = 0; (ctr2 <= ((setCbConfig->msgIDs[ctr1].net3gpp.toMsgId) - (setCbConfig->msgIDs[ctr1].net3gpp.fromMsgId))); ctr2++ ) {
2719                                 dbg( "%x", appendMsgId);
2720                                 mids_GString = g_string_append(mids_GString, g_strdup_printf("%d", appendMsgId));
2721
2722                                 if (ctr2 == ((setCbConfig->msgIDs[ctr1].net3gpp.toMsgId) - (setCbConfig->msgIDs[ctr1].net3gpp.fromMsgId))) {
2723                                         mids_GString = g_string_append(mids_GString, "\""); //Mids string termination
2724                                 } else {
2725                                         mids_GString = g_string_append(mids_GString, ",");
2726                                 }
2727
2728                                 appendMsgId++;
2729                         }
2730                 }
2731                 mids_str = g_string_free(mids_GString, FALSE);
2732                 cmd_str = g_strdup_printf("%s", mids_str);
2733                 g_free(mids_str);
2734         }
2735
2736         pending = tcore_pending_new(obj, 0);
2737         atreq = tcore_at_request_new((const char *)cmd_str, NULL, TCORE_AT_NO_RESULT);
2738         if(NULL == cmd_str || NULL == atreq || NULL == pending) {
2739                 err("Out of memory. Unable to proceed");
2740                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2741
2742                 //free memory we own
2743                 g_free(cmd_str);
2744                 util_sms_free_memory(atreq);
2745                 util_sms_free_memory(pending);
2746
2747                 dbg("Exit");
2748                 return TCORE_RETURN_ENOMEM;
2749         }
2750
2751         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2752
2753         tcore_pending_set_request_data(pending, 0, atreq);
2754         tcore_pending_set_response_callback(pending, on_response_set_cb_config, NULL);
2755         tcore_pending_link_user_request(pending, ur);
2756         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2757         tcore_hal_send_request(hal, pending);
2758
2759         g_free(cmd_str);
2760
2761         dbg("Exit");
2762         return TCORE_RETURN_SUCCESS;
2763 }
2764
2765 static TReturn set_mem_status(CoreObject *obj, UserRequest *ur)
2766 {
2767         gchar *cmd_str = NULL;
2768         TcoreHal *hal = NULL;
2769         TcoreATRequest *atreq = NULL;
2770         TcorePending *pending = NULL;
2771         const struct treq_sms_set_mem_status *setMemStatus = NULL;
2772         int memoryStatus = 0;
2773
2774         dbg("Entry");
2775
2776         setMemStatus = tcore_user_request_ref_data(ur, NULL);
2777         hal = tcore_object_get_hal(obj);
2778         if (NULL == setMemStatus || NULL == hal) {
2779                 err("NULL input. Unable to proceed");
2780                 dbg("setMemStatus: [%p], hal: [%p]", setMemStatus, hal);
2781
2782                 dbg("Exit");
2783                 return TCORE_RETURN_EINVAL;
2784         }
2785         if(FALSE == tcore_hal_get_power_state(hal)){
2786                 dbg("cp not ready/n");
2787                 return TCORE_RETURN_ENOSYS;
2788         }
2789
2790         dbg("memory_status: %d", setMemStatus->memory_status);
2791
2792         if(setMemStatus->memory_status < SMS_PDA_MEMORY_STATUS_AVAILABLE
2793                 || setMemStatus->memory_status > SMS_PDA_MEMORY_STATUS_FULL) {
2794                 err("Invalid memory_status");
2795
2796                 dbg("Exit");
2797                 return TCORE_RETURN_EINVAL;
2798         }
2799
2800         switch (setMemStatus->memory_status) {
2801                 case SMS_PDA_MEMORY_STATUS_AVAILABLE:
2802                         memoryStatus = AT_MEMORY_AVAILABLE;
2803                         break;
2804
2805                 case SMS_PDA_MEMORY_STATUS_FULL:
2806                         memoryStatus = AT_MEMORY_FULL;
2807                         break;
2808
2809                 default:
2810                         err("Invalid memory_status");
2811                         dbg("Exit");
2812                         return TCORE_RETURN_EINVAL;
2813         }
2814
2815         cmd_str = g_strdup_printf("AT+XTESM=%d", memoryStatus);
2816         pending = tcore_pending_new(obj, 0);
2817         atreq = tcore_at_request_new((const char *)cmd_str, NULL, TCORE_AT_NO_RESULT);
2818
2819         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2820                 err("Out of memory. Unable to proceed");
2821                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2822
2823                 //free memory we own
2824                 g_free(cmd_str);
2825                 util_sms_free_memory(atreq);
2826                 util_sms_free_memory(pending);
2827
2828                 dbg("Exit");
2829                 return TCORE_RETURN_ENOMEM;
2830         }
2831
2832         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2833
2834         tcore_pending_set_request_data(pending, 0, atreq);
2835         tcore_pending_set_response_callback(pending, on_response_set_mem_status, NULL);
2836         tcore_pending_link_user_request(pending, ur);
2837         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2838         tcore_hal_send_request(hal, pending);
2839
2840         g_free(cmd_str);
2841
2842         dbg("Exit");
2843         return TCORE_RETURN_SUCCESS;
2844 }
2845
2846 static TReturn set_delivery_report(CoreObject *obj, UserRequest *ur)
2847 {
2848         struct tresp_sms_set_delivery_report respSetDeliveryReport = {0,};
2849
2850         respSetDeliveryReport.result = SMS_SUCCESS;
2851
2852         dbg("Entry");
2853         if(FALSE == tcore_hal_get_power_state(tcore_object_get_hal(obj))){
2854                 dbg("cp not ready/n");
2855                 return TCORE_RETURN_ENOSYS;
2856         }
2857
2858         dbg("CP takes care of sending SMS ack to network for all classes of SMS. Sending default success.");
2859
2860         tcore_user_request_send_response(ur, TRESP_SMS_SET_DELIVERY_REPORT, sizeof(struct tresp_sms_set_delivery_report), &respSetDeliveryReport);
2861
2862         dbg("Exit");
2863         return TCORE_RETURN_SUCCESS;
2864 }
2865
2866 static TReturn set_msg_status(CoreObject *obj, UserRequest *ur)
2867 {
2868         gchar *cmd_str = NULL;
2869         TcoreHal *hal = NULL;
2870         TcoreATRequest *atreq = NULL;
2871         TcorePending *pending = NULL;
2872         const struct treq_sms_set_msg_status *msg_status = NULL;
2873
2874         dbg("Entry");
2875         hal = tcore_object_get_hal(obj);
2876         if(FALSE == tcore_hal_get_power_state(hal)){
2877                 dbg("cp not ready/n");
2878                 return TCORE_RETURN_ENOSYS;
2879         }
2880         msg_status = tcore_user_request_ref_data(ur, NULL);
2881
2882         cmd_str = g_strdup_printf("AT+CRSM=178,28476,%d,4,%d", (msg_status->index+1), PDU_LEN_MAX);
2883         atreq = tcore_at_request_new((const char *)cmd_str, "+CRSM", TCORE_AT_SINGLELINE);
2884         pending = tcore_pending_new(obj, 0);
2885         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2886                 err("Out of memory. Unable to proceed");
2887                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2888
2889                 //free memory we own
2890                 g_free(cmd_str);
2891                 util_sms_free_memory(atreq);
2892                 util_sms_free_memory(pending);
2893
2894                 dbg("Exit");
2895                 return TCORE_RETURN_ENOMEM;
2896         }
2897
2898         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2899
2900         tcore_pending_set_request_data(pending, 0, atreq);
2901         tcore_pending_set_response_callback(pending, _response_get_efsms_data, NULL);
2902         tcore_pending_link_user_request(pending, ur);
2903         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2904         tcore_hal_send_request(hal, pending);
2905
2906         g_free(cmd_str);
2907
2908         dbg("Exit");
2909         return TCORE_RETURN_SUCCESS;
2910 }
2911
2912 static TReturn get_sms_params(CoreObject *obj, UserRequest *ur)
2913 {
2914         gchar *cmd_str = NULL;
2915         TcoreHal *hal = NULL;
2916         TcoreATRequest *atreq = NULL;
2917         TcorePending *pending = NULL;
2918         const struct treq_sms_get_params *getSmsParams = NULL;
2919         int record_len = 0 , *smsp_record_len = NULL;
2920
2921         dbg("Entry");
2922
2923         getSmsParams = tcore_user_request_ref_data(ur, NULL);
2924         hal = tcore_object_get_hal(obj);
2925         if (NULL == getSmsParams || NULL == hal) {
2926                 err("NULL input. Unable to proceed");
2927                 dbg("getSmsParams: [%p], hal: [%p]", getSmsParams, hal);
2928
2929                 dbg("Exit");
2930                 return TCORE_RETURN_EINVAL;
2931         }
2932         if(FALSE == tcore_hal_get_power_state(hal)){
2933                 dbg("cp not ready/n");
2934                 return TCORE_RETURN_ENOSYS;
2935         }
2936
2937         smsp_record_len = tcore_plugin_ref_property(tcore_object_ref_plugin(obj), "SMSPRECORDLEN");
2938         record_len = *smsp_record_len;
2939         dbg("record len from property %d", record_len);
2940
2941         //AT+CRSM=command>[,<fileid>[,<P1>,<P2>,<P3>[,<data>[,<pathid>]]]]
2942         cmd_str = g_strdup_printf("AT+CRSM=178,28482,%d,4,%d", (getSmsParams->index + 1), record_len);
2943
2944         dbg("cmd_str is %s",cmd_str);
2945
2946         atreq = tcore_at_request_new((const char *)cmd_str, "+CRSM", TCORE_AT_SINGLELINE);
2947         pending = tcore_pending_new(obj, 0);
2948         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
2949                 err("Out of memory. Unable to proceed");
2950                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
2951
2952                 //free memory we own
2953                 g_free(cmd_str);
2954                 util_sms_free_memory(atreq);
2955                 util_sms_free_memory(pending);
2956
2957                 dbg("Exit");
2958                 return TCORE_RETURN_ENOMEM;
2959         }
2960
2961         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
2962
2963         tcore_pending_set_request_data(pending, 0, atreq);
2964         tcore_pending_set_response_callback(pending, on_response_get_sms_params, NULL);
2965         tcore_pending_link_user_request(pending, ur);
2966         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
2967         tcore_hal_send_request(hal, pending);
2968
2969         g_free(cmd_str);
2970
2971         dbg("Exit");
2972         return TCORE_RETURN_SUCCESS;
2973 }
2974
2975 static TReturn set_sms_params(CoreObject *obj, UserRequest *ur)
2976 {
2977         gchar *cmd_str = NULL;
2978         char *encoded_data = NULL;
2979         unsigned char *temp_data = NULL;
2980         int SMSPRecordLen = 0;
2981
2982         TcoreHal *hal = NULL;
2983         TcoreATRequest *atreq = NULL;
2984         TcorePending *pending = NULL;
2985         const struct treq_sms_set_params *setSmsParams = NULL;
2986         int encoded_data_len = 0;
2987
2988         dbg("Entry");
2989
2990         setSmsParams = tcore_user_request_ref_data(ur, NULL);
2991         hal = tcore_object_get_hal(obj);
2992         if (NULL == setSmsParams || NULL == hal) {
2993                 err("NULL input. Unable to proceed");
2994                 dbg("setSmsParams: [%p], hal: [%p]", setSmsParams, hal);
2995                 return FALSE;
2996         }
2997         if(FALSE == tcore_hal_get_power_state(hal)){
2998                 dbg("cp not ready/n");
2999                 return TCORE_RETURN_ENOSYS;
3000         }
3001
3002         //EFsmsp file size is 28 +Y bytes (Y is alpha id size)
3003         SMSPRecordLen = 28 + setSmsParams->params.alphaIdLen;
3004         temp_data = calloc(SMSPRecordLen,1);
3005         encoded_data = calloc(SMSPRecordLen*2 + 1,1);
3006
3007         _tcore_util_sms_encode_smsParameters(&(setSmsParams->params), temp_data, SMSPRecordLen);
3008
3009         util_byte_to_hex((const char *)temp_data, (char *)encoded_data,SMSPRecordLen);
3010
3011         encoded_data_len = ((SMSPRecordLen) * 2);
3012
3013         hal = tcore_object_get_hal(obj);
3014         pending = tcore_pending_new(obj, 0);
3015
3016         dbg("alpha id len %d encoded data %s. Encoded data len %d",setSmsParams->params.alphaIdLen,encoded_data, encoded_data_len);
3017         cmd_str = g_strdup_printf("AT+CRSM=220,28482,%d,4,%d,\"%s\"",(setSmsParams->params.recordIndex+1),SMSPRecordLen,encoded_data);
3018
3019         dbg("cmd str is %s",cmd_str);
3020         atreq = tcore_at_request_new(cmd_str, "+CRSM:", TCORE_AT_SINGLELINE);
3021
3022         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
3023                 err("Out of memory. Unable to proceed");
3024                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
3025
3026                 //free memory we own
3027                 g_free(cmd_str);
3028                 util_sms_free_memory(atreq);
3029                 util_sms_free_memory(pending);
3030
3031                 util_sms_free_memory(temp_data);
3032                 util_sms_free_memory(encoded_data);
3033
3034                 dbg("Exit");
3035                 return TCORE_RETURN_ENOMEM;
3036         }
3037
3038         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
3039
3040         tcore_pending_set_request_data(pending, 0,atreq);
3041         tcore_pending_set_response_callback(pending, on_response_set_sms_params, NULL);
3042         tcore_pending_link_user_request(pending, ur);
3043         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
3044         tcore_hal_send_request(hal, pending);
3045
3046         g_free(cmd_str);
3047         util_sms_free_memory(temp_data);
3048         util_sms_free_memory(encoded_data);
3049
3050         return TCORE_RETURN_SUCCESS;
3051 }
3052
3053 static TReturn get_paramcnt(CoreObject *obj, UserRequest *ur)
3054 {
3055         gchar *cmd_str = NULL;
3056         TcoreHal *hal = NULL;
3057         TcoreATRequest *atreq = NULL;
3058         TcorePending *pending = NULL;
3059
3060         dbg("Entry");
3061
3062         hal = tcore_object_get_hal(obj);
3063         if (NULL == hal) {
3064                 err("NULL HAL. Unable to proceed");
3065
3066                 dbg("Exit");
3067                 return TCORE_RETURN_EINVAL;
3068         }
3069         if(FALSE == tcore_hal_get_power_state(hal)){
3070                 dbg("cp not ready/n");
3071                 return TCORE_RETURN_ENOSYS;
3072         }
3073
3074         //AT+CRSM=command>[,<fileid>[,<P1>,<P2>,<P3>[,<data>[,<pathid>]]]]
3075         cmd_str = g_strdup_printf("AT+CRSM=192,28482");
3076         atreq = tcore_at_request_new((const char *)cmd_str, "+CRSM", TCORE_AT_SINGLELINE);
3077         pending = tcore_pending_new(obj, 0);
3078
3079         if (NULL == cmd_str || NULL == atreq || NULL == pending) {
3080                 err("NULL pointer. Unable to proceed");
3081                 dbg("cmd_str: [%p], atreq: [%p], pending: [%p]", cmd_str, atreq, pending);
3082
3083                 //free memory we own
3084                 g_free(cmd_str);
3085                 util_sms_free_memory(atreq);
3086                 util_sms_free_memory(pending);
3087
3088                 dbg("Exit");
3089                 return TCORE_RETURN_FAILURE;
3090         }
3091
3092         util_hex_dump("    ", strlen(cmd_str), (void *)cmd_str);
3093
3094         tcore_pending_set_request_data(pending, 0, atreq);
3095         tcore_pending_set_response_callback(pending, on_response_get_paramcnt, NULL);
3096         tcore_pending_link_user_request(pending, ur);
3097         tcore_pending_set_send_callback(pending, on_confirmation_sms_message_send, NULL);
3098         tcore_hal_send_request(hal, pending);
3099
3100         g_free(cmd_str);
3101
3102         dbg("Exit");
3103         return TCORE_RETURN_SUCCESS;
3104 }
3105
3106 static struct tcore_sms_operations sms_ops = {
3107         .send_umts_msg = send_umts_msg,
3108         .read_msg = read_msg,
3109         .save_msg = save_msg,
3110         .delete_msg = delete_msg,
3111         .get_stored_msg_cnt = get_stored_msg_cnt,
3112         .get_sca = get_sca,
3113         .set_sca = set_sca,
3114         .get_cb_config = get_cb_config,
3115         .set_cb_config = set_cb_config,
3116         .set_mem_status = set_mem_status,
3117         .get_pref_brearer = NULL,
3118         .set_pref_brearer = NULL,
3119         .set_delivery_report = set_delivery_report,
3120         .set_msg_status = set_msg_status,
3121         .get_sms_params = get_sms_params,
3122         .set_sms_params = set_sms_params,
3123         .get_paramcnt = get_paramcnt,
3124 };
3125
3126 gboolean s_sms_init(TcorePlugin *cp, CoreObject *co_sms)
3127 {
3128         int *smsp_record_len;
3129         dbg("Entry");
3130
3131         /* Override SMS Operations */
3132         tcore_sms_override_ops(co_sms, &sms_ops);
3133
3134         /* Registering for SMS notifications */
3135         tcore_object_override_callback(co_sms, "\e+CMTI", on_event_class2_sms_incom_msg, NULL);
3136         tcore_object_override_callback(co_sms, "\e+CMT", on_event_sms_incom_msg, NULL);
3137
3138         tcore_object_override_callback(co_sms, "\e+CDS", on_event_sms_incom_msg, NULL);
3139         tcore_object_override_callback(co_sms, "+XSMSMMSTAT", on_event_sms_memory_status, NULL);
3140         tcore_object_override_callback(co_sms, "+CMS", on_event_sms_memory_status, NULL);
3141
3142         tcore_object_override_callback(co_sms, "\e+CBMI", on_event_sms_cb_incom_msg, NULL);
3143         tcore_object_override_callback(co_sms, "\e+CBM", on_event_sms_cb_incom_msg, NULL);
3144
3145         /* Storing SMSP record length */
3146         smsp_record_len = g_new0(int, 1);
3147         tcore_plugin_link_property(cp, "SMSPRECORDLEN", smsp_record_len);
3148
3149         dbg("Exit");
3150         return TRUE;
3151 }
3152
3153 void s_sms_exit(TcorePlugin *cp, CoreObject *co_sms)
3154 {
3155         int *smsp_record_len;
3156
3157         smsp_record_len = tcore_plugin_ref_property(cp, "SMSPRECORDLEN");
3158         g_free(smsp_record_len);
3159
3160         dbg("Exit");
3161 }