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