s_phonebook: Fix phonebook crash
[platform/core/telephony/tel-plugin-imc.git] / src / s_sim.c
1 /*
2  * tel-plugin-imc
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ankit Jogi <ankit.jogi@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 <glib.h>
25 #include <tcore.h>
26 #include <hal.h>
27 #include <core_object.h>
28 #include <plugin.h>
29 #include <queue.h>
30 #include <co_sim.h>
31 #include <co_sms.h>
32 #include <storage.h>
33 #include <user_request.h>
34 #include <server.h>
35 #include <at.h>
36
37 #include "s_common.h"
38 #include "s_sms.h"
39 #include "s_sim.h"
40
41 #define ID_RESERVED_AT 0x0229
42
43 #define SWAPBYTES16(x) \
44         { \
45                 unsigned short int data = *(unsigned short int *) &(x); \
46                 data = ((data & 0xff00) >> 8) |    \
47                            ((data & 0x00ff) << 8);         \
48                 *(unsigned short int *) &(x) = data;      \
49         }
50
51 enum s_sim_file_type_e {
52         SIM_FTYPE_DEDICATED = 0x00, /**< Dedicated */
53         SIM_FTYPE_TRANSPARENT = 0x01, /**< Transparent -binary type*/
54         SIM_FTYPE_LINEAR_FIXED = 0x02, /**< Linear fixed - record type*/
55         SIM_FTYPE_CYCLIC = 0x04, /**< Cyclic - record type*/
56         SIM_FTYPE_INVALID_TYPE = 0xFF /**< Invalid type */
57 };
58
59 enum s_sim_sec_op_e {
60         SEC_PIN1_VERIFY,
61         SEC_PIN2_VERIFY,
62         SEC_PUK1_VERIFY,
63         SEC_PUK2_VERIFY,
64         SEC_SIM_VERIFY,
65         SEC_ADM_VERIFY,
66         SEC_PIN1_CHANGE,
67         SEC_PIN2_CHANGE,
68         SEC_PIN1_ENABLE,
69         SEC_PIN1_DISABLE,
70         SEC_PIN2_ENABLE,
71         SEC_PIN2_DISABLE, // 10
72         SEC_SIM_ENABLE,
73         SEC_SIM_DISABLE,
74         SEC_NET_ENABLE,
75         SEC_NET_DISABLE,
76         SEC_NS_ENABLE,
77         SEC_NS_DISABLE,
78         SEC_SP_ENABLE,
79         SEC_SP_DISABLE,
80         SEC_CP_ENABLE,
81         SEC_CP_DISABLE, // 20
82         SEC_FDN_ENABLE,
83         SEC_FDN_DISABLE,
84         SEC_PIN1_STATUS,
85         SEC_PIN2_STATUS,
86         SEC_FDN_STATUS,
87         SEC_NET_STATUS,
88         SEC_NS_STATUS,
89         SEC_SP_STATUS,
90         SEC_CP_STATUS,
91         SEC_SIM_STATUS,
92         SEC_SIM_UNKNOWN = 0xff
93 };
94
95 struct s_sim_property {
96         gboolean b_valid; /**< Valid or not */
97         enum tel_sim_file_id file_id; /**< File identifier */
98         enum s_sim_file_type_e file_type; /**< File type and structure */
99         int rec_length; /**< Length of one record in file */
100         int rec_count; /**< Number of records in file */
101         int data_size; /**< File size */
102         int current_index; /**< current index to read */
103         enum s_sim_sec_op_e current_sec_op; /**< current index to read */
104         struct tel_sim_mbi_list mbi_list;
105         struct tel_sim_mb_number mb_list[SIM_MSP_CNT_MAX*5];
106         struct tresp_sim_read files;
107 };
108
109 static void _next_from_get_file_info(CoreObject *o, UserRequest *ur, enum tel_sim_file_id ef, enum tel_sim_access_result rt);
110 static void _next_from_get_file_data(CoreObject *o, UserRequest *ur, enum tel_sim_access_result rt, int decode_ret);
111 static gboolean _get_sim_type(CoreObject *o);
112 static TReturn _get_file_info(CoreObject *o, UserRequest *ur, const enum tel_sim_file_id ef);
113 static gboolean _get_file_data(CoreObject *o, UserRequest *ur, const enum tel_sim_file_id ef, const int offset, const int length);
114 static gboolean _get_file_record(CoreObject *o, UserRequest *ur, const enum tel_sim_file_id ef, const int index, const int length);
115 static void _sim_status_update(CoreObject *o, enum tel_sim_status sim_status);
116 extern gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes);
117
118 static void sim_prepare_and_send_pending_request(CoreObject *co, const char *at_cmd, const char *prefix, enum tcore_at_command_type at_cmd_type, TcorePendingResponseCallback callback)
119 {
120         TcoreATRequest *req = NULL;
121         TcoreHal *hal = NULL;
122         TcorePending *pending = NULL;
123         TReturn ret;
124
125
126         hal = tcore_object_get_hal(co);
127         dbg("hal: %p", hal);
128
129         pending = tcore_pending_new(co, 0);
130         if (!pending)
131                 dbg("Pending is NULL");
132         req = tcore_at_request_new(at_cmd, prefix, at_cmd_type);
133
134         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
135
136         tcore_pending_set_request_data(pending, 0, req);
137         tcore_pending_set_response_callback(pending, callback, NULL);
138         tcore_pending_link_user_request(pending, NULL); // set user request to NULL - this is internal request
139         ret = tcore_hal_send_request(hal, pending);
140         return;
141 }
142
143
144 static enum tcore_response_command _find_resp_command(UserRequest *ur)
145 {
146         enum tcore_request_command command;
147
148         command = tcore_user_request_get_command(ur);
149         switch (command) {
150         case TREQ_SIM_VERIFY_PINS:
151                 return TRESP_SIM_VERIFY_PINS;
152                 break;
153
154         case TREQ_SIM_VERIFY_PUKS:
155                 return TRESP_SIM_VERIFY_PUKS;
156                 break;
157
158         case TREQ_SIM_CHANGE_PINS:
159                 return TRESP_SIM_CHANGE_PINS;
160                 break;
161
162         case TREQ_SIM_GET_FACILITY_STATUS:
163                 return TRESP_SIM_GET_FACILITY_STATUS;
164                 break;
165
166         case TREQ_SIM_DISABLE_FACILITY:
167                 return TRESP_SIM_DISABLE_FACILITY;
168                 break;
169
170         case TREQ_SIM_ENABLE_FACILITY:
171                 return TRESP_SIM_ENABLE_FACILITY;
172                 break;
173
174         case TREQ_SIM_GET_LOCK_INFO:
175                 return TRESP_SIM_GET_LOCK_INFO;
176                 break;
177
178         case TREQ_SIM_TRANSMIT_APDU:
179                 return TRESP_SIM_TRANSMIT_APDU;
180                 break;
181
182         case TREQ_SIM_GET_ATR:
183                 return TRESP_SIM_GET_ATR;
184                 break;
185
186         case TREQ_SIM_GET_ECC:
187                 return TRESP_SIM_GET_ECC;
188                 break;
189
190         case TREQ_SIM_GET_LANGUAGE:
191                 return TRESP_SIM_GET_LANGUAGE;
192                 break;
193
194         case TREQ_SIM_SET_LANGUAGE:
195                 return TRESP_SIM_SET_LANGUAGE;
196                 break;
197
198         case TREQ_SIM_GET_ICCID:
199                 return TRESP_SIM_GET_ICCID;
200                 break;
201
202         case TREQ_SIM_GET_MAILBOX:
203                 return TRESP_SIM_GET_MAILBOX;
204                 break;
205
206         case TREQ_SIM_GET_CALLFORWARDING:
207                 return TRESP_SIM_GET_CALLFORWARDING;
208                 break;
209
210         case TREQ_SIM_SET_CALLFORWARDING:
211                 return TRESP_SIM_SET_CALLFORWARDING;
212                 break;
213
214         case TREQ_SIM_GET_MESSAGEWAITING:
215                 return TRESP_SIM_GET_MESSAGEWAITING;
216                 break;
217
218         case TREQ_SIM_GET_CPHS_INFO:
219                 return TRESP_SIM_GET_CPHS_INFO;
220                 break;
221
222         case TREQ_SIM_GET_MSISDN:
223                 return TRESP_SIM_GET_MSISDN;
224                 break;
225
226         case TREQ_SIM_GET_SPN:
227                 return TRESP_SIM_GET_SPN;
228                 break;
229
230         case TREQ_SIM_GET_SPDI:
231                 return TRESP_SIM_GET_SPDI;
232                 break;
233
234         case TREQ_SIM_GET_OPL:
235                 return TRESP_SIM_GET_OPL;
236                 break;
237
238         case TREQ_SIM_GET_PNN:
239                 return TRESP_SIM_GET_PNN;
240                 break;
241
242         case TREQ_SIM_GET_CPHS_NETNAME:
243                 return TRESP_SIM_GET_CPHS_NETNAME;
244                 break;
245
246         case TREQ_SIM_GET_OPLMNWACT:
247                 return TRESP_SIM_GET_OPLMNWACT;
248                 break;
249
250         case TREQ_SIM_REQ_AUTHENTICATION:
251                 return TRESP_SIM_REQ_AUTHENTICATION;
252                 break;
253
254         default:
255                 break;
256         }
257         return TRESP_UNKNOWN;
258 }
259
260 static int _sim_get_current_pin_facility(enum s_sim_sec_op_e op)
261 {
262         int ret_type = 0;
263
264         dbg("current sec_op[%d]", op);
265
266         switch (op) {
267         case SEC_PIN1_VERIFY:
268         case SEC_PIN1_CHANGE:
269                 ret_type = SIM_PTYPE_PIN1;
270                 break;
271
272         case SEC_PIN2_VERIFY:
273         case SEC_PIN2_CHANGE:
274                 ret_type = SIM_PTYPE_PIN2;
275                 break;
276
277         case SEC_PUK1_VERIFY:
278                 ret_type = SIM_PTYPE_PUK1;
279                 break;
280
281         case SEC_PUK2_VERIFY:
282                 ret_type = SIM_PTYPE_PUK2;
283                 break;
284
285         case SEC_SIM_VERIFY:
286                 ret_type = SIM_PTYPE_SIM;
287                 break;
288
289         case SEC_ADM_VERIFY:
290                 ret_type = SIM_PTYPE_ADM;
291                 break;
292
293         case SEC_PIN1_ENABLE:
294         case SEC_PIN1_DISABLE:
295         case SEC_PIN1_STATUS:
296                 ret_type = SIM_FACILITY_SC;
297                 break;
298
299         case SEC_SIM_ENABLE:
300         case SEC_SIM_DISABLE:
301         case SEC_SIM_STATUS:
302                 ret_type = SIM_FACILITY_PS;
303                 break;
304
305         case SEC_NET_ENABLE:
306         case SEC_NET_DISABLE:
307         case SEC_NET_STATUS:
308                 ret_type = SIM_FACILITY_PN;
309                 break;
310
311         case SEC_NS_ENABLE:
312         case SEC_NS_DISABLE:
313         case SEC_NS_STATUS:
314                 ret_type = SIM_FACILITY_PU;
315                 break;
316
317         case SEC_SP_ENABLE:
318         case SEC_SP_DISABLE:
319         case SEC_SP_STATUS:
320                 ret_type = SIM_FACILITY_PP;
321                 break;
322
323         case SEC_CP_ENABLE:
324         case SEC_CP_DISABLE:
325         case SEC_CP_STATUS:
326                 ret_type = SIM_FACILITY_PC;
327                 break;
328
329         case SEC_FDN_ENABLE:
330         case SEC_FDN_DISABLE:
331         case SEC_FDN_STATUS:
332                 ret_type = SIM_FACILITY_FD;
333                 break;
334
335         default:
336                 dbg("not handled current sec op[%d]", op)
337                 break;
338         }
339         return ret_type;
340 }
341
342 static enum tel_sim_access_result _decode_status_word(unsigned short status_word1, unsigned short status_word2)
343 {
344         enum tel_sim_access_result rst = SIM_ACCESS_FAILED;
345
346         if (status_word1 == 0x93 && status_word2 == 0x00) {
347                 rst = SIM_ACCESS_FAILED;
348                 /*Failed SIM request command*/
349                 dbg(" error - SIM application toolkit busy [%x][%x]", status_word1, status_word2);
350         } else if (status_word1 == 0x94 && status_word2 == 0x00) {
351                 rst = SIM_ACCESS_FAILED;
352                 /*Failed SIM request command*/
353                 dbg(" error - No EF Selected [%x][%x]", status_word1, status_word2);
354         } else if (status_word1 == 0x94 && status_word2 == 0x02) {
355                 rst = SIM_ACCESS_FAILED;
356                 /*Failed SIM request command*/
357                 dbg("error - Out of Range - Invalid address or record number[%x][%x]",
358                         status_word1, status_word2);
359         } else if (status_word1 == 0x94 && status_word2 == 0x04) {
360                 rst = SIM_ACCESS_FILE_NOT_FOUND;
361                 /*Failed SIM request command*/
362                 dbg(" error - File ID not found [%x][%x]", status_word1, status_word2);
363         } else if (status_word1 == 0x94 && status_word2 == 0x08) {
364                 rst = SIM_ACCESS_FAILED; /* MOdem not support */
365                 /*Failed SIM request command*/
366                 dbg(" error - File is inconsistent with command - Modem not support or USE IPC [%x][%x]",
367                         status_word1, status_word2);
368         } else if (status_word1 == 0x98 && status_word2 == 0x02) {
369                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
370                 /*Failed SIM request command*/
371                 dbg(" error - CHV not initialized [%x][%x]", status_word1, status_word2);
372         } else if (status_word1 == 0x98 && status_word2 == 0x04) {
373                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
374                 /*Failed SIM request command*/
375                 dbg(" error - Access condition not fullfilled [%x][%x]", status_word1, status_word2);
376                 dbg(" error -Unsuccessful CHV verification - at least one attempt left [%x][%x]",
377                         status_word1, status_word2);
378                 dbg(" error - Unsuccessful Unblock CHV - at least one attempt left [%x][%x]",
379                         status_word1, status_word2);
380                 dbg(" error - Authentication failure [%x][%x]", status_word1, status_word2);
381         } else if (status_word1 == 0x98 && status_word2 == 0x08) {
382                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
383                 /*Failed SIM request command*/
384                 dbg(" error - Contradiction with CHV status [%x][%x]", status_word1, status_word2);
385         } else if (status_word1 == 0x98 && status_word2 == 0x10) {
386                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
387                 /*Failed SIM request command*/
388                 dbg(" error - Contradiction with invalidation  status [%x][%x]",
389                         status_word1, status_word2);
390         } else if (status_word1 == 0x98 && status_word2 == 0x40) {
391                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
392                 /*Failed SIM request command*/
393                 dbg(" error -Unsuccessful CHV verification - no attempt left [%x][%x]",
394                         status_word1, status_word2);
395                 dbg(" error - Unsuccessful Unblock CHV - no attempt left [%x][%x]",
396                         status_word1, status_word2);
397                 dbg(" error - CHV blocked [%x][%x]", status_word1, status_word2);
398         } else if (status_word1 == 0x67 && status_word2 == 0x00) {
399                 rst = SIM_ACCESS_FAILED;
400                 dbg(" error -Incorrect Parameter 3 [%x][%x]", status_word1, status_word2);
401         } else if (status_word1 == 0x6B && status_word2 == 0x00) {
402                 rst = SIM_ACCESS_FAILED;
403                 dbg(" error -Incorrect Parameter 1 or 2 [%x][%x]", status_word1, status_word2);
404         } else if (status_word1 == 0x6D && status_word2 == 0x00) {
405                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
406                 dbg(" error -Unknown instruction given as command [%x][%x]", status_word1, status_word2);
407         } else if (status_word1 == 0x6E && status_word2 == 0x00) {
408                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
409                 dbg(" error -Unknown instruction given as command [%x][%x]", status_word1, status_word2);
410         } else if (status_word1 == 0x69 && status_word2 == 0x82) {
411                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
412                 dbg(" error -Access denied [%x][%x]", status_word1, status_word2);
413         } else if (status_word1 == 0x6A && status_word2 == 0x87) {
414                 rst = SIM_ACCESS_FAILED;
415                 dbg(" error -Incorrect parameters [%x][%x]", status_word1, status_word2);
416         } else if (status_word1 == 0x6A && status_word2 == 0x82) {
417                 rst = SIM_ACCESS_FILE_NOT_FOUND; // not sure of the SW1 and SW2 meaning here
418                 dbg(" error -File Not found [%x][%x]", status_word1, status_word2);
419         } else if (status_word1 == 0x6A && status_word2 == 0x83) {
420                 rst = SIM_ACCESS_FILE_NOT_FOUND; // not sure of the SW1 and SW2 meaning here
421                 dbg(" error -Record Not found [%x][%x]", status_word1, status_word2);
422         } else {
423                 rst = SIM_ACCESS_CARD_ERROR;
424                 dbg(" error -Unknown state [%x][%x]", status_word1, status_word2);
425         }
426         return rst;
427 }
428
429 static gboolean _sim_check_identity(CoreObject *o, struct tel_sim_imsi *imsi)
430 {
431         Server *s = NULL;
432         Storage *strg = NULL;
433         char *old_imsi = NULL;
434         char new_imsi[15 + 1] = {0, };
435
436         s = tcore_plugin_ref_server(tcore_object_ref_plugin(o));
437         if (!s) {
438                 dbg("there is no valid server at this point");
439                 return FALSE;
440         }
441         strg = (Storage *) tcore_server_find_storage(s, "vconf");
442         if (!strg) {
443                 dbg("there is no valid storage plugin");
444                 return FALSE;
445         }
446         memcpy(&new_imsi, imsi->plmn, strlen(imsi->plmn));
447         memcpy(&new_imsi[strlen(imsi->plmn)], imsi->msin, strlen(imsi->msin));
448         new_imsi[strlen(imsi->plmn) + strlen(imsi->msin)] = '\0';
449
450         old_imsi = tcore_storage_get_string(strg, STORAGE_KEY_TELEPHONY_IMSI);
451         dbg("old_imsi[%s],newImsi[%s]", old_imsi, new_imsi);
452
453         if (old_imsi != NULL) {
454                 if (strncmp(old_imsi, new_imsi, 15) != 0) {
455                         dbg("NEW SIM");
456                         if (tcore_storage_set_string(strg, STORAGE_KEY_TELEPHONY_IMSI, (const char *) &new_imsi) == FALSE) {
457                                 dbg("[FAIL] UPDATE STORAGE_KEY_TELEPHONY_IMSI");
458                         }
459                         tcore_sim_set_identification(o, TRUE);
460                 } else {
461                         dbg("SAME SIM");
462                         tcore_sim_set_identification(o, FALSE);
463                 }
464         } else {
465                 dbg("OLD SIM VALUE IS NULL. NEW SIM");
466                 if (tcore_storage_set_string(strg, STORAGE_KEY_TELEPHONY_IMSI, (const char *) &new_imsi) == FALSE) {
467                         dbg("[FAIL] UPDATE STORAGE_KEY_TELEPHONY_IMSI");
468                 }
469                 tcore_sim_set_identification(o, TRUE);
470         }
471         return 1;
472 }
473
474 static void _next_from_get_file_info(CoreObject *o, UserRequest *ur, enum tel_sim_file_id ef, enum tel_sim_access_result rt)
475 {
476         struct tresp_sim_read resp = {0, };
477         struct s_sim_property *file_meta = NULL;
478
479         dbg("EF[0x%x] access Result[%d]", ef, rt);
480
481         resp.result = rt;
482         memset(&resp.data, 0x00, sizeof(resp.data));
483         file_meta = (struct s_sim_property *) tcore_user_request_ref_metainfo(ur, NULL);
484
485         if ((ef != SIM_EF_ELP && ef != SIM_EF_LP && ef != SIM_EF_USIM_PL && ef != SIM_EF_CPHS_CPHS_INFO)
486                 && (rt != SIM_ACCESS_SUCCESS)) {
487                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_read), &resp);
488                 return;
489         }
490
491         switch (ef) {
492         case SIM_EF_ELP:
493                 if (rt == SIM_ACCESS_SUCCESS) {
494                         dbg("[SIM DATA] exist EFELP/PL(0x2F05)");
495                         /*                              if (po->language_file == 0x00)
496                          po->language_file = SIM_EF_ELP;*/
497                         _get_file_data(o, ur, ef, 0, file_meta->data_size);
498                 } else {
499                         if (tcore_sim_get_type(o) == SIM_TYPE_GSM) {
500                                 dbg(" [SIM DATA]SIM_EF_ELP(2F05) access fail. Request SIM_EF_LP(0x6F05) info");
501                                 /* The ME requests the Language Preference (EFLP) if EFELP is not available  */
502                                 _get_file_info(o, ur, SIM_EF_LP);
503                         } else if (tcore_sim_get_type(o) == SIM_TYPE_USIM) {
504                                 dbg(
505                                         " [SIM DATA]fail to get Language information in USIM(EF-LI(6F05),EF-PL(2F05)). Request SIM_EF_ECC(0x6FB7) info");
506                                 /* EFELPand EFLI not present at this point. */
507                                 /*                                      po->language.lang_cnt = 0;*/
508                                 tcore_user_request_send_response(ur, _find_resp_command(ur),
509                                                                                                  sizeof(struct tresp_sim_read), &resp);
510                                 return;
511                         }
512                 }
513                 break;
514
515         case SIM_EF_LP:     // same with SIM_EF_USIM_LI
516                 if (rt == SIM_ACCESS_SUCCESS) {
517                         dbg("[SIM DATA] exist EFLP/LI(0x6F05)");
518                         _get_file_data(o, ur, ef, 0, file_meta->data_size);
519                 } else {
520                         dbg("[SIM DATA]SIM_EF_LP/LI(6F05) access fail. Current CardType[%d]",
521                                 tcore_sim_get_type(o));
522                         if (tcore_sim_get_type(o) == SIM_TYPE_GSM) {
523                                 tcore_user_request_send_response(ur, _find_resp_command(ur),
524                                                                                                  sizeof(struct tresp_sim_read), &resp);
525                                 return;
526                         }
527                         /*  if EFLI is not present, then the language selection shall be as defined in EFPL at the MF level     */
528                         else if (tcore_sim_get_type(o) == SIM_TYPE_USIM) {
529                                 dbg("[SIM DATA] try USIM EFPL(0x2F05)");
530                                 _get_file_info(o, ur, SIM_EF_ELP);
531                         }
532                 }
533                 break;
534
535         case SIM_EF_USIM_PL:
536                 if (rt == SIM_ACCESS_SUCCESS) {
537                         dbg("[SIM DATA] exist EFELP/PL(0x2F05)");
538                         _get_file_data(o, ur, SIM_EF_ELP, 0, file_meta->data_size);
539                 } else {
540                         /* EFELIand EFPL not present, so set language count as zero and select ECC */
541                         dbg(
542                                 " [SIM DATA]SIM_EF_USIM_PL(2A05) access fail. Request SIM_EF_ECC(0x6FB7) info");
543                         tcore_user_request_send_response(ur, _find_resp_command(ur),
544                                                                                          sizeof(struct tresp_sim_read), &resp);
545                         return;
546                 }
547                 break;
548
549         case SIM_EF_ECC:
550                 if (tcore_sim_get_type(o) == SIM_TYPE_GSM) {
551                         _get_file_data(o, ur, ef, 0, file_meta->data_size);
552                 } else if (tcore_sim_get_type(o) == SIM_TYPE_USIM) {
553                         if (file_meta->rec_count > SIM_ECC_RECORD_CNT_MAX) {
554                                 file_meta->rec_count = SIM_ECC_RECORD_CNT_MAX;
555                         }
556
557                         file_meta->current_index++;
558                         _get_file_record(o, ur, ef, file_meta->current_index, file_meta->rec_length);
559                 }
560                 break;
561
562         case SIM_EF_ICCID:
563         case SIM_EF_IMSI:
564         case SIM_EF_SST:
565         case SIM_EF_SPN:
566         case SIM_EF_SPDI:
567         case SIM_EF_CPHS_CALL_FORWARD_FLAGS:
568         case SIM_EF_CPHS_VOICE_MSG_WAITING:
569         case SIM_EF_CPHS_OPERATOR_NAME_STRING:
570         case SIM_EF_CPHS_OPERATOR_NAME_SHORT_FORM_STRING:
571         case SIM_EF_CPHS_DYNAMICFLAGS:
572         case SIM_EF_CPHS_DYNAMIC2FLAG:
573         case SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE:
574         case SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE_LINE2:
575                 _get_file_data(o, ur, ef, 0, file_meta->data_size);
576                 break;
577
578         case SIM_EF_CPHS_CPHS_INFO:
579                 if (rt == SIM_ACCESS_SUCCESS) {
580                         tcore_sim_set_cphs_status(o, TRUE);
581                         if (!tcore_user_request_ref_communicator(ur)) {
582                                 dbg("internal CPHS INFO request before sim status update");
583                                 _sim_status_update(o, SIM_STATUS_INIT_COMPLETED);
584                         } else {
585                                 dbg("external CPHS INFO request");
586                                 _get_file_data(o, ur, ef, 0, file_meta->data_size);
587                         }
588                 } else {
589                         tcore_sim_set_cphs_status(o, FALSE);
590                         if (!tcore_user_request_ref_communicator(ur)) {
591                                 dbg("internal CPHS INFO request before sim status update");
592                                 _sim_status_update(o, SIM_STATUS_INIT_COMPLETED);
593                         } else {
594                                 dbg("external CPHS INFO request");
595                                 tcore_user_request_send_response(ur, _find_resp_command(ur),
596                                                                                                  sizeof(struct tresp_sim_read), &resp);
597                         }
598                 }
599                 break;
600
601
602         case SIM_EF_USIM_CFIS:
603                 if (file_meta->rec_count > SIM_CF_RECORD_CNT_MAX) {
604                         file_meta->rec_count = SIM_CF_RECORD_CNT_MAX;
605                 }
606                 file_meta->current_index++;
607                 _get_file_record(o, ur, ef, file_meta->current_index, file_meta->rec_length);
608                 break;
609
610         case SIM_EF_OPL:
611         case SIM_EF_PNN:
612         case SIM_EF_USIM_MWIS:
613         case SIM_EF_USIM_MBI:
614         case SIM_EF_MBDN:
615         case SIM_EF_CPHS_MAILBOX_NUMBERS:
616         case SIM_EF_CPHS_INFORMATION_NUMBERS:
617         case SIM_EF_MSISDN:
618                 file_meta->current_index++;
619                 _get_file_record(o, ur, ef, file_meta->current_index, file_meta->rec_length);
620                 break;
621
622         default:
623                 dbg("error - File id for get file info [0x%x]", ef);
624                 break;
625         }
626         return;
627 }
628
629 static void _next_from_get_file_data(CoreObject *o, UserRequest *ur, enum tel_sim_access_result rt, int decode_ret)
630 {
631         struct s_sim_property *file_meta = NULL;
632
633         dbg("Entry");
634
635         file_meta = (struct s_sim_property *) tcore_user_request_ref_metainfo(ur, NULL);
636         dbg("[SIM]EF[0x%x] read rt[%d] Decode rt[%d]", file_meta->file_id, rt, decode_ret);
637         switch (file_meta->file_id) {
638         case SIM_EF_ELP:
639         case SIM_EF_USIM_PL:
640         case SIM_EF_LP:
641         case SIM_EF_USIM_LI:
642                 if (decode_ret == TRUE) {
643                         if (file_meta->file_id == SIM_EF_LP || file_meta->file_id == SIM_EF_USIM_LI) {
644 /*                                      po->language_file = SIM_EF_LP;*/
645                         } else if (file_meta->file_id == SIM_EF_ELP || file_meta->file_id == SIM_EF_USIM_PL) {
646 /*                                      po->language_file = SIM_EF_ELP;*/
647                         }
648                         tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_read), &file_meta->files);
649                 } else {
650                         /* 2G */
651                         /*  The ME requests the Extended Language Preference. The ME only requests the Language Preference (EFLP) if at least one of the following conditions holds:
652                          -      EFELP is not available;
653                          -      EFELP does not contain an entry corresponding to a language specified in ISO 639[30];
654                          -      the ME does not support any of the languages in EFELP.
655                          */
656                         /* 3G */
657                         /*  The ME only requests the Language Preference (EFPL) if at least one of the following conditions holds:
658                          -      if the EFLI has the value 'FFFF' in its highest priority position
659                          -      if the ME does not support any of the language codes indicated in EFLI , or if EFLI is not present
660                          */
661                         if (tcore_sim_get_type(o) == SIM_TYPE_GSM) {
662                                 if (file_meta->file_id == SIM_EF_LP) {
663                                         tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_read), &file_meta->files);
664                                 } else {
665                                         _get_file_info(o, ur, SIM_EF_LP);
666                                 }
667                         } else if (tcore_sim_get_type(o) == SIM_TYPE_USIM) {
668                                 if (file_meta->file_id == SIM_EF_LP || file_meta->file_id == SIM_EF_USIM_LI) {
669                                         _get_file_info(o, ur, SIM_EF_ELP);
670                                 } else {
671                                         tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_read), &file_meta->files);
672                                 }
673                         }
674                 }
675                 break;
676
677         case SIM_EF_ECC:
678                 if (tcore_sim_get_type(o) == SIM_TYPE_USIM) {
679                         if (file_meta->current_index == file_meta->rec_count) {
680                                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_read), &file_meta->files);
681                         } else {
682                                 file_meta->current_index++;
683                                 _get_file_record(o, ur, file_meta->file_id, file_meta->current_index, file_meta->rec_length);
684                         }
685                 } else if (tcore_sim_get_type(o) == SIM_TYPE_GSM) {
686                         tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_read), &file_meta->files);
687                 } else {
688                         dbg("[SIM DATA]Invalid CardType[%d] Unable to handle", tcore_sim_get_type(o));
689                 }
690                 break;
691
692         case SIM_EF_IMSI:
693                 ur = tcore_user_request_new(NULL, NULL);     // this is for using ur metainfo set/ref functionality.
694                 _get_file_info(o, ur, SIM_EF_CPHS_CPHS_INFO);
695                 break;
696
697         case SIM_EF_MSISDN:
698                 if (file_meta->current_index == file_meta->rec_count) {
699                         tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_read), &file_meta->files);
700                 } else {
701                         file_meta->current_index++;
702                         _get_file_record(o, ur, file_meta->file_id, file_meta->current_index, file_meta->rec_length);
703                 }
704                 break;
705
706         case SIM_EF_OPL:
707                 if (file_meta->current_index == file_meta->rec_count) {
708                         tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_read), &file_meta->files);
709                 } else {
710                         file_meta->current_index++;
711                         _get_file_record(o, ur, file_meta->file_id, file_meta->current_index, file_meta->rec_length);
712                 }
713                 break;
714
715         case SIM_EF_PNN:
716                 if (file_meta->current_index == file_meta->rec_count) {
717                         tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_read), &file_meta->files);
718                 } else {
719                         file_meta->current_index++;
720                         _get_file_record(o, ur, file_meta->file_id, file_meta->current_index, file_meta->rec_length);
721                 }
722                 break;
723
724         case SIM_EF_USIM_CFIS:
725         case SIM_EF_USIM_MWIS:
726         case SIM_EF_USIM_MBI:
727         case SIM_EF_MBDN:
728         case SIM_EF_CPHS_MAILBOX_NUMBERS:
729         case SIM_EF_CPHS_INFORMATION_NUMBERS:
730                 if (file_meta->current_index == file_meta->rec_count) {
731                         tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_read), &file_meta->files);
732                 } else {
733                         file_meta->current_index++;
734                         _get_file_record(o, ur, file_meta->file_id, file_meta->current_index, file_meta->rec_length);
735                 }
736                 break;
737
738         case SIM_EF_CPHS_OPERATOR_NAME_STRING:
739                 file_meta->files.result = rt;
740                 if (decode_ret == TRUE && rt == SIM_ACCESS_SUCCESS) {
741                         memcpy(file_meta->files.data.cphs_net.full_name, file_meta->files.data.cphs_net.full_name, strlen((char *) file_meta->files.data.cphs_net.full_name));
742                 }
743                 _get_file_info(o, ur, SIM_EF_CPHS_OPERATOR_NAME_SHORT_FORM_STRING);
744                 break;
745
746         case SIM_EF_CPHS_OPERATOR_NAME_SHORT_FORM_STRING:
747                 if (file_meta->files.result == SIM_ACCESS_SUCCESS || file_meta->files.result == SIM_ACCESS_SUCCESS) {
748                         file_meta->files.result = SIM_ACCESS_SUCCESS;
749                 }
750                 if (strlen((char *) file_meta->files.data.cphs_net.full_name)) {
751                         memcpy(&file_meta->files.data.cphs_net.full_name, &file_meta->files.data.cphs_net.full_name, strlen((char *) file_meta->files.data.cphs_net.full_name));
752                 }
753                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_read), &file_meta->files);
754                 break;
755
756         case SIM_EF_ICCID:
757         case SIM_EF_SST:
758         case SIM_EF_SPN:
759         case SIM_EF_SPDI:
760         case SIM_EF_OPLMN_ACT:
761         case SIM_EF_CPHS_CPHS_INFO:
762         case SIM_EF_CPHS_CALL_FORWARD_FLAGS:
763         case SIM_EF_CPHS_VOICE_MSG_WAITING:
764         case SIM_EF_CPHS_DYNAMICFLAGS:
765         case SIM_EF_CPHS_DYNAMIC2FLAG:
766         case SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE:
767         case SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE_LINE2:
768                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_read), &file_meta->files);
769                 break;
770
771         default:
772                 dbg("File id not handled [0x%x]", file_meta->file_id);
773                 break;
774         }
775 }
776
777 static void _sim_status_update(CoreObject *o, enum tel_sim_status sim_status)
778 {
779         struct tnoti_sim_status noti_data = {0, };
780
781         if (sim_status != tcore_sim_get_status(o)) {
782                 dbg("tcore_sim_set_status and send noti w/ [%d]", sim_status);
783                 tcore_sim_set_status(o, sim_status);
784                 noti_data.sim_status = sim_status;
785                 tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_SIM_STATUS,
786                                                                            sizeof(struct tnoti_sim_status), &noti_data);
787         }
788 }
789
790 static void _response_get_sim_type(TcorePending *p, int data_len, const void *data, void *user_data)
791 {
792         const TcoreATResponse *resp = data;
793         UserRequest *ur = NULL;
794         CoreObject *co_sim = NULL;
795         struct s_sim_property *sp = NULL;
796         GSList *tokens = NULL;
797         enum tel_sim_type sim_type = SIM_TYPE_UNKNOWN;
798         const char *line;
799         int state;
800
801         dbg(" Function entry ");
802
803         co_sim = tcore_pending_ref_core_object(p);
804         sp = tcore_sim_ref_userdata(co_sim);
805         ur = tcore_pending_ref_user_request(p);
806
807         if (resp->success > 0) {
808                 dbg("RESPONSE OK");
809                 if (resp->lines) {
810                         line = (const char *) resp->lines->data;
811                         tokens = tcore_at_tok_new(line);
812                         if (g_slist_length(tokens) != 1) {
813                                 msg("invalid message");
814                                 tcore_at_tok_free(tokens);
815                                 return;
816                         }
817                 }
818                 state = atoi(g_slist_nth_data(tokens, 0));
819                 dbg("SIM Type is %d", state);
820
821                 if (state == 0) {
822                         sim_type = SIM_TYPE_GSM;
823                 } else if (state == 1) {
824                         sim_type = SIM_TYPE_USIM;
825                 } else {
826                         sim_type = SIM_TYPE_UNKNOWN;
827                 }
828         } else {
829                 dbg("RESPONSE NOK");
830                 sim_type = SIM_TYPE_UNKNOWN;
831         }
832
833         tcore_sim_set_type(co_sim, sim_type);
834
835         if (sim_type != SIM_TYPE_UNKNOWN) {
836                 /* set user request for using ur metainfo set/ref functionality */
837                 ur = tcore_user_request_new(NULL, NULL);
838                 _get_file_info(co_sim, ur, SIM_EF_IMSI);
839         }
840
841         tcore_at_tok_free(tokens);
842         dbg(" Function exit");
843 }
844
845 static void _response_get_file_info(TcorePending *p, int data_len, const void *data, void *user_data)
846 {
847         const TcoreATResponse *resp = data;
848         UserRequest *ur = NULL;
849         CoreObject *co_sim = NULL;
850         struct s_sim_property *file_meta = NULL;
851         GSList *tokens = NULL;
852         enum tel_sim_access_result rt;
853         const char *line = NULL;
854         int sw1 = 0;
855         int sw2 = 0;
856
857         dbg(" Function entry ");
858
859         co_sim = tcore_pending_ref_core_object(p);
860         ur = tcore_pending_ref_user_request(p);
861         file_meta = (struct s_sim_property *) tcore_user_request_ref_metainfo(ur, NULL);
862
863         if (resp->success > 0) {
864                 dbg("RESPONSE OK");
865                 if (resp->lines) {
866                         line = (const char *) resp->lines->data;
867                         tokens = tcore_at_tok_new(line);
868                         if (g_slist_length(tokens) < 2) {
869                                 err("invalid message");
870                                 tcore_at_tok_free(tokens);
871                                 return;
872                         }
873                 }
874                 sw1 = atoi(g_slist_nth_data(tokens, 0));
875                 sw2 = atoi(g_slist_nth_data(tokens, 1));
876
877                 /*1. SIM access success case*/
878                 if ((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
879                         unsigned char tag_len = 0; /*   1 or 2 bytes ??? */
880                         unsigned short record_len = 0;
881                         char num_of_records = 0;
882                         unsigned char file_id_len = 0;
883                         unsigned short file_id = 0;
884                         unsigned short file_size = 0;
885                         unsigned short file_type = 0;
886                         unsigned short arr_file_id = 0;
887                         int arr_file_id_rec_num = 0;
888
889                         /*      handling only last 3 bits */
890                         unsigned char file_type_tag = 0x07;
891                         unsigned char *ptr_data;
892
893                         char *hexData;
894                         char *tmp;
895                         char *recordData = NULL;
896                         hexData = g_slist_nth_data(tokens, 2);
897                         dbg("hexData: %s", hexData);
898                         dbg("hexData: %s", hexData + 1);
899
900                         tmp = util_removeQuotes(hexData);
901                         recordData = util_hexStringToBytes(tmp);
902                         util_hex_dump("    ", strlen(hexData) / 2, recordData);
903                         free(tmp);
904
905                         ptr_data = (unsigned char *) recordData;
906                         if (tcore_sim_get_type(co_sim) == SIM_TYPE_USIM) {
907                                 /*
908                                  ETSI TS 102 221 v7.9.0
909                                  - Response Data
910                                  '62'   FCP template tag
911                                  - Response for an EF
912                                  '82'   M       File Descriptor
913                                  '83'   M       File Identifier
914                                  'A5'   O       Proprietary information
915                                  '8A'   M       Life Cycle Status Integer
916                                  '8B', '8C' or 'AB'     C1      Security attributes
917                                  '80'   M       File size
918                                  '81'   O       Total file size
919                                  '88'   O       Short File Identifier (SFI)
920                                  */
921
922                                 /* rsim.res_len  has complete data length received  */
923
924                                 /* FCP template tag - File Control Parameters tag*/
925                                 if (*ptr_data == 0x62) {
926                                         /* parse complete FCP tag*/
927                                         /* increment to next byte */
928                                         ptr_data++;
929                                         tag_len = *ptr_data++;
930                                         dbg("tag_len: %02x", tag_len);
931                                         /* FCP file descriptor - file type, accessibility, DF, ADF etc*/
932                                         if (*ptr_data == 0x82) {
933                                                 /* increment to next byte */
934                                                 ptr_data++;
935                                                 /* 2 or 5 value*/
936                                                 ptr_data++;
937                                                 /*      unsigned char file_desc_len = *ptr_data++;*/
938                                                 /*      dbg("file descriptor length: [%d]", file_desc_len);*/
939                                                 /* TBD:  currently capture only file type : ignore sharable, non sharable, working, internal etc*/
940                                                 /* consider only last 3 bits*/
941                                                 dbg("file_type_tag: %02x", file_type_tag);
942                                                 file_type_tag = file_type_tag & (*ptr_data);
943                                                 dbg("file_type_tag: %02x", file_type_tag);
944
945                                                 switch (file_type_tag) {
946                                                 /* increment to next byte */
947                                                 // ptr_data++;
948                                                 case 0x1:
949                                                         dbg("Getting FileType: [Transparent file type]");
950                                                         file_type = SIM_FTYPE_TRANSPARENT;
951
952                                                         /* increment to next byte */
953                                                         ptr_data++;
954                                                         /* increment to next byte */
955                                                         ptr_data++;
956                                                         break;
957
958                                                 case 0x2:
959                                                         dbg("Getting FileType: [Linear fixed file type]");
960                                                         /* increment to next byte */
961                                                         ptr_data++;
962                                                         /*      data coding byte - value 21 */
963                                                         ptr_data++;
964                                                         /*      2bytes */
965                                                         memcpy(&record_len, ptr_data, 2);
966                                                         /* swap bytes */
967                                                         SWAPBYTES16(record_len);
968                                                         ptr_data = ptr_data + 2;
969                                                         num_of_records = *ptr_data++;
970                                                         /* Data lossy conversation from enum (int) to unsigned char */
971                                                         file_type = SIM_FTYPE_LINEAR_FIXED;
972                                                         break;
973
974                                                 case 0x6:
975                                                         dbg(" Cyclic fixed file type");
976                                                         /* increment to next byte */
977                                                         ptr_data++;
978                                                         /*      data coding byte - value 21 */
979                                                         ptr_data++;
980                                                         /*      2bytes */
981                                                         memcpy(&record_len, ptr_data, 2);
982                                                         /* swap bytes  */
983                                                         SWAPBYTES16(record_len);
984                                                         ptr_data = ptr_data + 2;
985                                                         num_of_records = *ptr_data++;
986                                                         file_type = SIM_FTYPE_CYCLIC;
987                                                         break;
988
989                                                 default:
990                                                         dbg("not handled file type [0x%x]", *ptr_data);
991                                                         break;
992                                                 }
993                                         } else {
994                                                 dbg("INVALID FCP received - DEbug!");
995                                                 tcore_at_tok_free(tokens);
996                                                 free(recordData);
997                                                 return;
998                                         }
999
1000                                         /*File identifier - file id?? */ // 0x84,0x85,0x86 etc are currently ignored and not handled
1001                                         if (*ptr_data == 0x83) {
1002                                                 /* increment to next byte */
1003                                                 ptr_data++;
1004                                                 file_id_len = *ptr_data++;
1005                                                 dbg("file_id_len: %02x", file_id_len);
1006
1007                                                 memcpy(&file_id, ptr_data, file_id_len);
1008                                                 dbg("file_id: %x", file_id);
1009
1010                                                 /* swap bytes    */
1011                                                 SWAPBYTES16(file_id);
1012                                                 dbg("file_id: %x", file_id);
1013
1014                                                 ptr_data = ptr_data + 2;
1015                                                 dbg("Getting FileID=[0x%x]", file_id);
1016                                         } else {
1017                                                 dbg("INVALID FCP received - DEbug!");
1018                                                 tcore_at_tok_free(tokens);
1019                                                 free(recordData);
1020                                                 return;
1021                                         }
1022
1023                                         /*      proprietary information  */
1024                                         if (*ptr_data == 0xA5) {
1025                                                 unsigned short prop_len;
1026                                                 /* increment to next byte */
1027                                                 ptr_data++;
1028
1029                                                 /* length */
1030                                                 prop_len = *ptr_data;
1031                                                 dbg("prop_len: %02x", prop_len);
1032
1033                                                 /* skip data */
1034                                                 ptr_data = ptr_data + prop_len + 1;
1035                                         } else {
1036                                                 dbg("INVALID FCP received - DEbug!");
1037                                         }
1038
1039                                         /* life cycle status integer [8A][length:0x01][status]*/
1040                                         /*
1041                                          status info b8~b1
1042                                          00000000 : No information given
1043                                          00000001 : creation state
1044                                          00000011 : initialization state
1045                                          000001-1 : operation state -activated
1046                                          000001-0 : operation state -deactivated
1047                                          000011-- : Termination state
1048                                          b8~b5 !=0, b4~b1=X : Proprietary
1049                                          Any other value : RFU
1050                                          */
1051                                         if (*ptr_data == 0x8A) {
1052                                                 /* increment to next byte */
1053                                                 ptr_data++;
1054                                                 /* length - value 1 */
1055                                                 ptr_data++;
1056
1057                                                 switch (*ptr_data) {
1058                                                 case 0x04:
1059                                                 case 0x06:
1060                                                         dbg("<RX> operation state -deactivated");
1061                                                         ptr_data++;
1062                                                         break;
1063
1064                                                 case 0x05:
1065                                                 case 0x07:
1066                                                         dbg("<RX> operation state -activated");
1067                                                         ptr_data++;
1068                                                         break;
1069
1070                                                 default:
1071                                                         dbg("<RX> DEBUG! LIFE CYCLE STATUS =[0x%x]", *ptr_data);
1072                                                         ptr_data++;
1073                                                         break;
1074                                                 }
1075                                         }
1076
1077                                         /* related to security attributes : currently not handled*/
1078                                         if (*ptr_data == 0x86 || *ptr_data == 0x8B || *ptr_data == 0x8C || *ptr_data == 0xAB) {
1079                                                 /* increment to next byte */
1080                                                 ptr_data++;
1081                                                 /* if tag length is 3 */
1082                                                 if (*ptr_data == 0x03) {
1083                                                         /* increment to next byte */
1084                                                         ptr_data++;
1085                                                         /* EFARR file id */
1086                                                         memcpy(&arr_file_id, ptr_data, 2);
1087                                                         /* swap byes */
1088                                                         SWAPBYTES16(arr_file_id);
1089                                                         ptr_data = ptr_data + 2;
1090                                                         arr_file_id_rec_num = *ptr_data++;
1091                                                 } else {
1092                                                         /* if tag length is not 3 */
1093                                                         /* ignoring bytes       */
1094                                                         // ptr_data = ptr_data + 4;
1095                                                         dbg("Useless security attributes, so jump to next tag");
1096                                                         ptr_data = ptr_data + (*ptr_data + 1);
1097                                                 }
1098                                         } else {
1099                                                 dbg("INVALID FCP received[0x%x] - DEbug!", *ptr_data);
1100                                                 tcore_at_tok_free(tokens);
1101                                                 free(recordData);
1102                                                 return;
1103                                         }
1104
1105                                         dbg("Current ptr_data value is [%x]", *ptr_data);
1106
1107                                         /* file size excluding structural info*/
1108                                         if (*ptr_data == 0x80) {
1109                                                 /* for EF file size is body of file and for Linear or cyclic it is
1110                                                  * number of recXsizeof(one record)
1111                                                  */
1112                                                 /* increment to next byte */
1113                                                 ptr_data++;
1114                                                 /* length is 1 byte - value is 2 bytes or more */
1115                                                 ptr_data++;
1116                                                 memcpy(&file_size, ptr_data, 2);
1117                                                 /* swap bytes */
1118                                                 SWAPBYTES16(file_size);
1119                                                 ptr_data = ptr_data + 2;
1120                                         } else {
1121                                                 dbg("INVALID FCP received - DEbug!");
1122                                                 tcore_at_tok_free(tokens);
1123                                                 free(recordData);
1124                                                 return;
1125                                         }
1126
1127                                         /* total file size including structural info*/
1128                                         if (*ptr_data == 0x81) {
1129                                                 int len;
1130                                                 /* increment to next byte */
1131                                                 ptr_data++;
1132                                                 /* length */
1133                                                 len = *ptr_data;
1134                                                 /* ignored bytes */
1135                                                 ptr_data = ptr_data + 3;
1136                                         } else {
1137                                                 dbg("INVALID FCP received - DEbug!");
1138                                                 /* 0x81 is optional tag?? check out! so do not return -1 from here! */
1139                                                 /* return -1; */
1140                                         }
1141                                         /*short file identifier ignored*/
1142                                         if (*ptr_data == 0x88) {
1143                                                 dbg("0x88: Do Nothing");
1144                                                 /*DO NOTHING*/
1145                                         }
1146                                 } else {
1147                                         dbg("INVALID FCP received - DEbug!");
1148                                         tcore_at_tok_free(tokens);
1149                                         free(recordData);
1150                                         return;
1151                                 }
1152                         } else if (tcore_sim_get_type(co_sim) == SIM_TYPE_GSM) {
1153                                 unsigned char gsm_specific_file_data_len = 0;
1154                                 /*      ignore RFU byte1 and byte2 */
1155                                 ptr_data++;
1156                                 ptr_data++;
1157                                 /*      file size */
1158                                 // file_size = p_info->response_len;
1159                                 memcpy(&file_size, ptr_data, 2);
1160                                 /* swap bytes */
1161                                 SWAPBYTES16(file_size);
1162                                 /*      parsed file size */
1163                                 ptr_data = ptr_data + 2;
1164                                 /*  file id  */
1165                                 memcpy(&file_id, ptr_data, 2);
1166                                 SWAPBYTES16(file_id);
1167                                 dbg(" FILE id --> [%x]", file_id);
1168                                 ptr_data = ptr_data + 2;
1169                                 /* save file type - transparent, linear fixed or cyclic */
1170                                 file_type_tag = (*(ptr_data + 7));
1171
1172                                 switch (*ptr_data) {
1173                                 case 0x0:
1174                                         /* RFU file type */
1175                                         dbg(" RFU file type- not handled - Debug!");
1176                                         break;
1177
1178                                 case 0x1:
1179                                         /* MF file type */
1180                                         dbg(" MF file type - not handled - Debug!");
1181                                         break;
1182
1183                                 case 0x2:
1184                                         /* DF file type */
1185                                         dbg(" DF file type - not handled - Debug!");
1186                                         break;
1187
1188                                 case 0x4:
1189                                         /* EF file type */
1190                                         dbg(" EF file type [%d] ", file_type_tag);
1191                                         /*      increment to next byte */
1192                                         ptr_data++;
1193
1194                                         if (file_type_tag == 0x00 || file_type_tag == 0x01) {
1195                                                 /* increament to next byte as this byte is RFU */
1196                                                 ptr_data++;
1197                                                 file_type =
1198                                                         (file_type_tag == 0x00) ? SIM_FTYPE_TRANSPARENT : SIM_FTYPE_LINEAR_FIXED;
1199                                         } else {
1200                                                 /* increment to next byte */
1201                                                 ptr_data++;
1202                                                 /*      For a cyclic EF all bits except bit 7 are RFU; b7=1 indicates that */
1203                                                 /* the INCREASE command is allowed on the selected cyclic file. */
1204                                                 file_type = SIM_FTYPE_CYCLIC;
1205                                         }
1206                                         /* bytes 9 to 11 give SIM file access conditions */
1207                                         ptr_data++;
1208                                         /* byte 10 has one nibble that is RF U and another for INCREASE which is not used currently */
1209                                         ptr_data++;
1210                                         /* byte 11 is invalidate and rehabilate nibbles */
1211                                         ptr_data++;
1212                                         /* byte 12 - file status */
1213                                         ptr_data++;
1214                                         /* byte 13 - GSM specific data */
1215                                         gsm_specific_file_data_len = *ptr_data;
1216                                         ptr_data++;
1217                                         /*      byte 14 - structure of EF - transparent or linear or cyclic , already saved above */
1218                                         ptr_data++;
1219                                         /* byte 15 - length of record for linear and cyclic , for transparent it is set to 0x00. */
1220                                         record_len = *ptr_data;
1221                                         dbg("record length[%d], file size[%d]", record_len, file_size);
1222
1223                                         if (record_len != 0)
1224                                                 num_of_records = (file_size / record_len);
1225
1226                                         dbg("Number of records [%d]", num_of_records);
1227                                         break;
1228
1229                                 default:
1230                                         dbg(" not handled file type");
1231                                         break;
1232                                 }
1233                         } else {
1234                                 dbg(" Card Type - UNKNOWN  [%d]", tcore_sim_get_type(co_sim));
1235                         }
1236
1237                         dbg("req ef[0x%x] resp ef[0x%x] size[%ld] Type[0x%x] NumOfRecords[%ld] RecordLen[%ld]",
1238                                 file_meta->file_id, file_id, file_size, file_type, num_of_records, record_len);
1239
1240                         file_meta->file_type = file_type;
1241                         file_meta->data_size = file_size;
1242                         file_meta->rec_length = record_len;
1243                         file_meta->rec_count = num_of_records;
1244                         file_meta->current_index = 0; // reset for new record type EF
1245                         rt = SIM_ACCESS_SUCCESS;
1246                         free(recordData);
1247                 } else {
1248                         /*2. SIM access fail case*/
1249                         dbg("error to get ef[0x%x]", file_meta->file_id);
1250                         dbg("error to get ef[0x%x] (file_meta->file_id) ", file_meta->file_id);
1251                         rt = _decode_status_word(sw1, sw2);
1252                 }
1253                 ur = tcore_user_request_ref(ur);
1254
1255                 dbg("Calling _next_from_get_file_info");
1256                 _next_from_get_file_info(co_sim, ur, file_meta->file_id, rt);
1257                 tcore_at_tok_free(tokens);
1258         } else {
1259                 dbg("RESPONSE NOK");
1260                 dbg("error to get ef[0x%x]", file_meta->file_id);
1261                 dbg("error to get ef[0x%x] (file_meta->file_id) ", file_meta->file_id);
1262                 rt = SIM_ACCESS_FAILED;
1263
1264                 ur = tcore_user_request_ref(ur);
1265                 _next_from_get_file_info(co_sim, ur, file_meta->file_id, rt);
1266         }
1267         dbg(" Function exit");
1268 }
1269
1270 static void _response_get_file_data(TcorePending *p, int data_len, const void *data, void *user_data)
1271 {
1272         const TcoreATResponse *resp = data;
1273         UserRequest *ur = NULL;
1274         CoreObject *co_sim = NULL;
1275         struct s_sim_property *file_meta = NULL;
1276         GSList *tokens = NULL;
1277         enum tel_sim_access_result rt;
1278         struct tel_sim_imsi *imsi = NULL;
1279         struct tel_sim_service_table *svct = NULL;
1280         struct tel_sim_ecc *ecc = NULL;
1281         struct tel_sim_msisdn *msisdn = NULL;
1282         struct tel_sim_opl *opl = NULL;
1283         struct tel_sim_pnn *pnn = NULL;
1284         struct tel_sim_cfis *cf = NULL;
1285         struct tel_sim_mbi *mbi = NULL;
1286         struct tel_sim_mw *mw = NULL;
1287         gboolean dr = FALSE;
1288         const char *line = NULL;
1289         char *res = NULL;
1290         char *tmp = NULL;
1291         int res_len;
1292         int sw1 = 0;
1293         int sw2 = 0;
1294
1295         dbg(" Function entry ");
1296
1297         co_sim = tcore_pending_ref_core_object(p);
1298         ur = tcore_pending_ref_user_request(p);
1299         file_meta = (struct s_sim_property *) tcore_user_request_ref_metainfo(ur, NULL);
1300
1301         if (resp->success > 0) {
1302                 dbg("RESPONSE OK");
1303                 if (resp->lines) {
1304                         line = (const char *) resp->lines->data;
1305                         tokens = tcore_at_tok_new(line);
1306                         if (g_slist_length(tokens) != 3) {
1307                                 msg("invalid message");
1308                                 tcore_at_tok_free(tokens);
1309                                 return;
1310                         }
1311                 }
1312                 sw1 = atoi(g_slist_nth_data(tokens, 0));
1313                 sw2 = atoi(g_slist_nth_data(tokens, 1));
1314                 res = g_slist_nth_data(tokens, 2);
1315
1316                 tmp = util_removeQuotes(res);
1317                 res = util_hexStringToBytes(tmp);
1318                 res_len = strlen((const char *) res);
1319                 dbg("res: %s res_len: %d", res, res_len);
1320
1321                 if ((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
1322                         rt = SIM_ACCESS_SUCCESS;
1323                         file_meta->files.result = rt;
1324                         dbg("file_meta->file_id : %x", file_meta->file_id);
1325
1326                         switch (file_meta->file_id) {
1327                         case SIM_EF_IMSI:
1328                         {
1329                                 dbg("res: %s", res);
1330                                 imsi = calloc(sizeof(struct tel_sim_imsi),1);
1331                                 dr = tcore_sim_decode_imsi(imsi, (unsigned char *) res, res_len);
1332                                 if (dr == FALSE) {
1333                                         dbg("imsi decoding failed");
1334                                 } else {
1335                                         _sim_check_identity(co_sim, imsi);
1336                                         tcore_sim_set_imsi(co_sim, imsi);
1337                                 }
1338                                 if(imsi)
1339                                         free(imsi);
1340                                 break;
1341                         }
1342
1343                         case SIM_EF_ICCID:
1344                                 dr = tcore_sim_decode_iccid(&file_meta->files.data.iccid, (unsigned char *) res, res_len);
1345                                 break;
1346
1347                         case SIM_EF_ELP:    /*  2G EF -  2 bytes decoding*/
1348                         case SIM_EF_USIM_LI:     /* 3G EF - 2 bytes decoding*/
1349                         case SIM_EF_USIM_PL:    /*  3G EF - same as EFELP, so 2  byte decoding*/
1350                         case SIM_EF_LP:    /*  1 byte encoding*/
1351                                 if (tcore_sim_get_type(co_sim) == SIM_TYPE_GSM && file_meta->file_id == SIM_EF_LP) {
1352                                         /*2G LP(0x6F05) has 1 byte for each language*/
1353                                         dr = tcore_sim_decode_lp(&file_meta->files.data.language, (unsigned char *) res, res_len);
1354                                 } else {
1355                                         /*3G LI(0x6F05)/PL(0x2F05), 2G ELP(0x2F05) has 2 bytes for each language*/
1356                                         dr = tcore_sim_decode_li(file_meta->file_id, &file_meta->files.data.language, (unsigned char *) res, res_len);
1357                                 }
1358                                 break;
1359
1360                         case SIM_EF_SPN:
1361                                 dr = tcore_sim_decode_spn(&file_meta->files.data.spn, (unsigned char *) res, res_len);
1362                                 break;
1363
1364                         case SIM_EF_SPDI:
1365                                 dr = tcore_sim_decode_spdi(&file_meta->files.data.spdi, (unsigned char *) res, res_len);
1366                                 break;
1367
1368                         case SIM_EF_SST: //EF UST has same address
1369                                 svct = calloc(sizeof(struct tel_sim_service_table),1);
1370                                 if(tcore_sim_get_type(co_sim) == SIM_TYPE_GSM) {
1371                                         dr = tcore_sim_decode_sst(&svct->sst , (unsigned char *) res, res_len);
1372                                 }else if(tcore_sim_get_type(co_sim) == SIM_TYPE_USIM) {
1373                                         dr = tcore_sim_decode_ust(&svct->ust , (unsigned char *) res, res_len);
1374                                 } else {
1375                                         dbg("err not handled tcore_sim_get_type(o)[%d] in here",tcore_sim_get_type(co_sim));
1376                                 }
1377                                 if (dr == FALSE) {
1378                                         dbg("SST/UST decoding failed");
1379                                 } else {
1380                                         tcore_sim_set_service_table(co_sim, svct);
1381                                 }
1382                                 if(svct)
1383                                         free(svct);
1384                                 break;
1385
1386                         case SIM_EF_ECC:
1387                                 if (tcore_sim_get_type(co_sim) == SIM_TYPE_GSM) {
1388                                         dr = tcore_sim_decode_ecc(&file_meta->files.data.ecc, (unsigned char *) res, res_len);
1389                                 } else if (tcore_sim_get_type(co_sim) == SIM_TYPE_USIM) {
1390                                         ecc = calloc(sizeof(struct tel_sim_ecc),1);
1391                                         dbg("decode w/ index [%d]", file_meta->current_index);
1392                                         dr = tcore_sim_decode_uecc(ecc, (unsigned char *) res, res_len);
1393                                         if (dr == TRUE) {
1394                                                 memcpy(&file_meta->files.data.ecc.ecc[file_meta->files.data.ecc.ecc_count], ecc, sizeof(struct tel_sim_ecc));
1395                                                 file_meta->files.data.ecc.ecc_count++;
1396                                         }
1397                                         if(ecc)
1398                                                 free(ecc);
1399                                 } else {
1400                                         dbg("err not handled tcore_sim_get_type(o)[%d] in here", tcore_sim_get_type(co_sim));
1401                                 }
1402                                 break;
1403
1404                         case SIM_EF_MSISDN:
1405                                 dbg("decode w/ index [%d]", file_meta->current_index);
1406                                 msisdn = calloc(sizeof(struct tel_sim_msisdn),1);
1407                                 dr = tcore_sim_decode_msisdn(msisdn, (unsigned char *) res, res_len);
1408                                 if (dr == TRUE) {
1409                                         memcpy(&file_meta->files.data.msisdn_list.msisdn[file_meta->files.data.msisdn_list.count], msisdn, sizeof(struct tel_sim_msisdn));
1410                                         file_meta->files.data.msisdn_list.count++;
1411                                 }
1412                                 if(msisdn)
1413                                         free(msisdn);
1414                                 break;
1415
1416                         case SIM_EF_OPL:
1417                                 dbg("decode w/ index [%d]", file_meta->current_index);
1418                                 opl = calloc(sizeof(struct tel_sim_opl),1);
1419                                 dr = tcore_sim_decode_opl(opl, (unsigned char *) res, res_len);
1420                                 if (dr == TRUE) {
1421                                         memcpy(&file_meta->files.data.opl.opl[file_meta->files.data.opl.opl_count], opl, sizeof(struct tel_sim_opl));
1422                                         file_meta->files.data.opl.opl_count++;
1423                                 }
1424                                 break;
1425
1426                         case SIM_EF_PNN:
1427                                 dbg("decode w/ index [%d]", file_meta->current_index);
1428                                 pnn = calloc(sizeof(struct tel_sim_pnn),1);
1429                                 dr = tcore_sim_decode_pnn(pnn, (unsigned char *) res, res_len);
1430                                 if (dr == TRUE) {
1431                                         memcpy(&file_meta->files.data.pnn.pnn[file_meta->files.data.pnn.pnn_count], pnn, sizeof(struct tel_sim_pnn));
1432                                         file_meta->files.data.pnn.pnn_count++;
1433                                 }
1434                                 if(pnn)
1435                                         free(pnn);
1436                                 break;
1437
1438                         case SIM_EF_OPLMN_ACT:
1439                                 dr = tcore_sim_decode_oplmnwact(&file_meta->files.data.opwa, (unsigned char *) res, res_len);
1440                                 break;
1441
1442                         case SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE:
1443 /*                                      dr = tcore_sim_decode_csp(&po->p_cphs->csp, p_data->response, p_data->response_len);*/
1444                                 break;
1445
1446                         case SIM_EF_USIM_MBI: //linear type
1447                                 mbi = calloc(sizeof(struct tel_sim_mbi),1);
1448                                 dr = tcore_sim_decode_mbi(mbi,  (unsigned char *) res, res_len);
1449                                 if (dr == TRUE) {
1450                                         memcpy( &file_meta->mbi_list.mbi[file_meta->mbi_list.profile_count], mbi, sizeof(struct tel_sim_mbi) );
1451                                         file_meta->mbi_list.profile_count++;
1452                                         dbg("mbi count[%d]", file_meta->mbi_list.profile_count);
1453                                         dbg("voice_index[%d]", file_meta->mbi_list.mbi[file_meta->mbi_list.profile_count -1].voice_index);
1454                                         dbg("fax_index[%d]", file_meta->mbi_list.mbi[file_meta->mbi_list.profile_count -1].fax_index);
1455                                         dbg("email_index[%d]", file_meta->mbi_list.mbi[file_meta->mbi_list.profile_count -1].email_index);
1456                                         dbg("other_index[%d]", file_meta->mbi_list.mbi[file_meta->mbi_list.profile_count -1].other_index);
1457                                         dbg("video_index[%d]", file_meta->mbi_list.mbi[file_meta->mbi_list.profile_count -1].video_index);
1458                                 }
1459                                 if(mbi)
1460                                         free(mbi);
1461                                 break;
1462
1463                         case SIM_EF_CPHS_MAILBOX_NUMBERS: // linear type
1464                         case SIM_EF_MBDN: //linear type
1465                                 dr = tcore_sim_decode_xdn(&file_meta->mb_list[file_meta->current_index-1].number_info,  (unsigned char *) res, res_len);
1466                                 file_meta->mb_list[file_meta->current_index-1].rec_index = file_meta->current_index;
1467                                 break;
1468
1469                         case SIM_EF_CPHS_VOICE_MSG_WAITING: // transparent type
1470                                 dr = tcore_sim_decode_vmwf(&file_meta->files.data.mw.cphs_mw,  (unsigned char *) res, res_len);
1471                                 break;
1472
1473                         case SIM_EF_USIM_MWIS: //linear type
1474                                 mw = calloc(sizeof(struct tel_sim_mw),1);
1475                                 dr = tcore_sim_decode_mwis(mw,  (unsigned char *) res, res_len);
1476                                 if (dr == TRUE) {
1477                                         memcpy( &file_meta->files.data.mw.mw_list.mw[file_meta->files.data.mw.mw_list.profile_count], mw, sizeof(struct tel_sim_mw) );
1478                                         file_meta->files.data.mw.mw_list.mw[file_meta->files.data.mw.mw_list.profile_count].rec_index = file_meta->current_index;
1479                                         file_meta->files.data.mw.mw_list.profile_count++;
1480                                 }
1481                                 if(mw)
1482                                         free(mw);
1483                                 break;
1484
1485                         case SIM_EF_CPHS_CALL_FORWARD_FLAGS: //transparent type
1486                                 dr = tcore_sim_decode_cff(&file_meta->files.data.cf.cphs_cf,  (unsigned char *) res, res_len);
1487                                 break;
1488
1489                         case SIM_EF_USIM_CFIS: //linear type
1490                                 cf = calloc(sizeof(struct tel_sim_cfis),1);
1491                                 dr = tcore_sim_decode_cfis(cf,  (unsigned char *) res, res_len);
1492                                 if (dr == TRUE) {
1493                                         memcpy( &file_meta->files.data.cf.cf_list.cf[file_meta->files.data.cf.cf_list.profile_count], cf, sizeof(struct tel_sim_cfis) );
1494                                         file_meta->files.data.cf.cf_list.cf[file_meta->files.data.cf.cf_list.profile_count].rec_index = file_meta->current_index;
1495                                         file_meta->files.data.cf.cf_list.profile_count++;
1496                                 }
1497                                 if(cf)
1498                                         free(cf);
1499                                 break;
1500
1501                         case SIM_EF_CPHS_SERVICE_STRING_TABLE:
1502                                 dbg(" not handled -SIM_EF_CPHS_SERVICE_STRING_TABLE ");
1503                                 break;
1504
1505                         case SIM_EF_CPHS_OPERATOR_NAME_STRING:
1506                                 dr = tcore_sim_decode_ons((unsigned char*)&file_meta->files.data.cphs_net.full_name,  (unsigned char *) res, res_len);
1507                                 dbg(" file_meta->files.result[%d],file_meta->files.data.cphs_net.full_name[%s]", file_meta->files.result, file_meta->files.data.cphs_net.full_name);
1508                                 break;
1509
1510                         case SIM_EF_CPHS_DYNAMICFLAGS:
1511 /*                                      dr = tcore_sim_decode_dynamic_flag(&po->p_cphs->dflagsinfo, p_data->response, p_data->response_len);*/
1512                                 break;
1513
1514                         case SIM_EF_CPHS_DYNAMIC2FLAG:
1515 /*                                      dr = tcore_sim_decode_dynamic2_flag(&po->p_cphs->d2flagsinfo, p_data->response,         p_data->response_len);*/
1516                                 break;
1517
1518                         case SIM_EF_CPHS_CPHS_INFO:
1519                                 dr = tcore_sim_decode_cphs_info(&file_meta->files.data.cphs,  (unsigned char *) res, res_len);
1520                                 break;
1521
1522                         case SIM_EF_CPHS_OPERATOR_NAME_SHORT_FORM_STRING:
1523                                 dr = tcore_sim_decode_short_ons((unsigned char*)&file_meta->files.data.cphs_net.short_name,  (unsigned char *) res, res_len);
1524                                 break;
1525
1526                         case SIM_EF_CPHS_INFORMATION_NUMBERS:
1527 /*                                      dr = tcore_sim_decode_information_number(&po->p_cphs->infn, p_data->response, p_data->response_len);*/
1528                                 break;
1529
1530                         default:
1531                                 dbg("File Decoding Failed - not handled File[0x%x]", file_meta->file_id);
1532                                 dr = 0;
1533                                 break;
1534                         }
1535                 } else {
1536                         rt = _decode_status_word(sw1, sw2);
1537                         file_meta->files.result = rt;
1538                 }
1539                 free(tmp);
1540                 free(res);
1541                 tcore_at_tok_free(tokens);
1542         } else {
1543                 dbg("RESPONSE NOK");
1544                 dbg("error to get ef[0x%x]", file_meta->file_id);
1545                 rt = SIM_ACCESS_FAILED;
1546         }
1547         ur = tcore_user_request_ref(ur);
1548
1549         dbg("Calling _next_from_get_file_data");
1550         _next_from_get_file_data(tcore_pending_ref_core_object(p), ur, rt, dr);
1551         dbg(" Function exit");
1552 }
1553
1554 static void _on_response_get_retry_count(TcorePending *p, int data_len, const void *data, void *user_data)
1555 {
1556         const TcoreATResponse *resp = data;
1557         UserRequest *ur = NULL;
1558         CoreObject *co_sim = NULL;
1559         struct s_sim_property *sp = NULL;
1560         GSList *tokens = NULL;
1561         const char *line = NULL;
1562         struct tresp_sim_verify_pins v_pin = {0, };
1563         struct tresp_sim_verify_puks v_puk = {0, };
1564         struct tresp_sim_change_pins change_pin = {0, };
1565         struct tresp_sim_disable_facility dis_facility = {0, };
1566         struct tresp_sim_enable_facility en_facility = {0, };
1567         int lock_type = 0;
1568         int attempts_left = 0;
1569         int time_penalty = 0;
1570
1571         dbg(" Function entry ");
1572
1573         co_sim = tcore_pending_ref_core_object(p);
1574         sp = tcore_sim_ref_userdata(co_sim);
1575         ur = tcore_pending_ref_user_request(p);
1576
1577         if (resp->success > 0) {
1578                 dbg("RESPONSE OK");
1579                 if (resp->lines) {
1580                         line = (const char *) resp->lines->data;
1581                         tokens = tcore_at_tok_new(line);
1582                         if (g_slist_length(tokens) < 3) {
1583                                 msg("invalid message");
1584                                 tcore_at_tok_free(tokens);
1585                                 return;
1586                         }
1587                 }
1588                 lock_type = atoi(g_slist_nth_data(tokens, 0));
1589                 attempts_left = atoi(g_slist_nth_data(tokens, 1));
1590                 time_penalty = atoi(g_slist_nth_data(tokens, 2));
1591
1592                 dbg("lock_type = %d, attempts_left = %d, time_penalty = %d",
1593                         lock_type, attempts_left, time_penalty);
1594
1595                 switch (sp->current_sec_op) {
1596                 case SEC_PIN1_VERIFY:
1597                 case SEC_PIN2_VERIFY:
1598                 case SEC_SIM_VERIFY:
1599                 case SEC_ADM_VERIFY:
1600                         v_pin.result = SIM_INCORRECT_PASSWORD;
1601                         v_pin.pin_type = _sim_get_current_pin_facility(sp->current_sec_op);
1602                         v_pin.retry_count = attempts_left;
1603                         tcore_user_request_send_response(ur, _find_resp_command(ur),
1604                                                                                          sizeof(struct tresp_sim_verify_pins), &v_pin);
1605                         break;
1606
1607                 case SEC_PUK1_VERIFY:
1608                 case SEC_PUK2_VERIFY:
1609                         v_puk.result = SIM_INCORRECT_PASSWORD;
1610                         v_puk.pin_type = _sim_get_current_pin_facility(sp->current_sec_op);
1611                         v_puk.retry_count = attempts_left;
1612                         tcore_user_request_send_response(ur, _find_resp_command(ur),
1613                                                                                          sizeof(struct tresp_sim_verify_puks), &v_puk);
1614                         break;
1615
1616                 case SEC_PIN1_CHANGE:
1617                 case SEC_PIN2_CHANGE:
1618                         change_pin.result = SIM_INCORRECT_PASSWORD;
1619                         change_pin.pin_type = _sim_get_current_pin_facility(sp->current_sec_op);
1620                         change_pin.retry_count = attempts_left;
1621                         tcore_user_request_send_response(ur, _find_resp_command(ur),
1622                                                                                          sizeof(struct tresp_sim_change_pins), &change_pin);
1623                         break;
1624
1625                 case SEC_PIN1_DISABLE:
1626                 case SEC_PIN2_DISABLE:
1627                 case SEC_FDN_DISABLE:
1628                 case SEC_SIM_DISABLE:
1629                 case SEC_NET_DISABLE:
1630                 case SEC_NS_DISABLE:
1631                 case SEC_SP_DISABLE:
1632                 case SEC_CP_DISABLE:
1633                         dis_facility.result = SIM_INCORRECT_PASSWORD;
1634                         dis_facility.type = _sim_get_current_pin_facility(sp->current_sec_op);
1635                         dis_facility.retry_count = attempts_left;
1636                         tcore_user_request_send_response(ur, _find_resp_command(ur),
1637                                                                                          sizeof(struct tresp_sim_disable_facility), &dis_facility);
1638                         break;
1639
1640                 case SEC_PIN1_ENABLE:
1641                 case SEC_PIN2_ENABLE:
1642                 case SEC_FDN_ENABLE:
1643                 case SEC_SIM_ENABLE:
1644                 case SEC_NET_ENABLE:
1645                 case SEC_NS_ENABLE:
1646                 case SEC_SP_ENABLE:
1647                 case SEC_CP_ENABLE:
1648                         en_facility.result = SIM_INCORRECT_PASSWORD;
1649                         en_facility.type = _sim_get_current_pin_facility(sp->current_sec_op);
1650                         en_facility.retry_count = attempts_left;
1651                         tcore_user_request_send_response(ur, _find_resp_command(ur),
1652                                                                                          sizeof(struct tresp_sim_enable_facility), &en_facility);
1653                         break;
1654
1655                 default:
1656                         dbg("not handled sec op[%d]", sp->current_sec_op);
1657                         break;
1658                 }
1659                 tcore_at_tok_free(tokens);
1660         }
1661         dbg(" Function exit");
1662 }
1663
1664 static gboolean _get_sim_type(CoreObject *o)
1665 {
1666         TcoreHal *hal = NULL;
1667         TcoreATRequest *req = NULL;
1668         TcorePending *pending = NULL;
1669         UserRequest *ur = NULL;
1670         char *cmd_str = NULL;
1671
1672         dbg(" Function entry ");
1673
1674         hal = tcore_object_get_hal(o);
1675         pending = tcore_pending_new(o, 0);
1676
1677         cmd_str = g_strdup_printf("AT+XUICC?");
1678         req = tcore_at_request_new(cmd_str, "+XUICC:", TCORE_AT_SINGLELINE);
1679
1680         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
1681
1682         tcore_pending_set_request_data(pending, 0, req);
1683         tcore_pending_set_response_callback(pending, _response_get_sim_type, hal);
1684         tcore_pending_link_user_request(pending, ur);
1685         tcore_hal_send_request(hal, pending);
1686
1687         free(cmd_str);
1688         dbg(" Function exit");
1689         return TRUE;
1690 }
1691
1692 static TReturn _get_file_info(CoreObject *o, UserRequest *ur, const enum tel_sim_file_id ef)
1693 {
1694         TcoreHal *hal = NULL;
1695         TcorePending *pending = NULL;
1696         struct s_sim_property file_meta = {0, };
1697         char *cmd_str = NULL;
1698         TReturn ret = TCORE_RETURN_FAILURE;
1699         int trt = 0;
1700
1701         dbg(" Function entry ");
1702
1703         file_meta.file_id = ef;
1704         dbg("file_meta.file_id: [0x%02x]", file_meta.file_id);
1705         hal = tcore_object_get_hal(o);
1706         dbg("hal: %x", hal);
1707
1708         trt = tcore_user_request_set_metainfo(ur, sizeof(struct s_sim_property), &file_meta);
1709         dbg("trt[%d]", trt);
1710         cmd_str = g_strdup_printf("AT+CRSM=192, %d", ef);           /*command - 192 : GET RESPONSE*/
1711         dbg("cmd_str: %s", cmd_str);
1712
1713         pending = tcore_at_pending_new(o, cmd_str, "+CRSM:", TCORE_AT_SINGLELINE, _response_get_file_info, NULL);
1714         tcore_pending_link_user_request(pending, ur);
1715         ret = tcore_hal_send_request(hal, pending);
1716         if (TCORE_RETURN_SUCCESS != ret) {
1717                 tcore_user_request_free(ur);
1718         }
1719         free(cmd_str);
1720         dbg(" Function exit");
1721         return TCORE_RETURN_SUCCESS;
1722 }
1723
1724 static gboolean _get_file_data(CoreObject *o, UserRequest *ur, const enum tel_sim_file_id ef, const int offset, const int length)
1725 {
1726         TcoreHal *hal = NULL;
1727         TcoreATRequest *req = NULL;
1728         TcorePending *pending = NULL;
1729         char *cmd_str = NULL;
1730         int p1 = 0;
1731         int p2 = 0;
1732         int p3 = 0;
1733
1734         dbg(" Function entry ");
1735         hal = tcore_object_get_hal(o);
1736         pending = tcore_pending_new(o, 0);
1737
1738         dbg("file_id: %x", ef);
1739
1740         p1 = (unsigned char) (offset & 0xFF00) >> 8;
1741         p2 = (unsigned char) offset & 0x00FF; // offset low
1742         p3 = (unsigned char) length;
1743
1744         cmd_str = g_strdup_printf("AT+CRSM=176, %d, %d, %d, %d", ef, p1, p2, p3);          /*command - 176 : READ BINARY*/
1745
1746         req = tcore_at_request_new(cmd_str, "+CRSM:", TCORE_AT_SINGLELINE);
1747
1748         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
1749
1750         tcore_pending_set_request_data(pending, 0, req);
1751         tcore_pending_set_response_callback(pending, _response_get_file_data, hal);
1752         tcore_pending_link_user_request(pending, ur);
1753         tcore_hal_send_request(hal, pending);
1754
1755         free(cmd_str);
1756         dbg(" Function exit");
1757         return TRUE;
1758 }
1759
1760 static gboolean _get_file_record(CoreObject *o, UserRequest *ur, const enum tel_sim_file_id ef, const int index, const int length)
1761 {
1762         TcoreHal *hal = NULL;
1763         TcoreATRequest *req = NULL;
1764         TcorePending *pending = NULL;
1765         char *cmd_str = NULL;
1766         int p1 = 0;
1767         int p2 = 0;
1768         int p3 = 0;
1769
1770         dbg(" Function entry ");
1771
1772         hal = tcore_object_get_hal(o);
1773         pending = tcore_pending_new(o, 0);
1774
1775         p1 = (unsigned char) index;
1776         p2 = (unsigned char) 0x04;       /* 0x4 for absolute mode  */
1777         p3 = (unsigned char) length;
1778
1779         cmd_str = g_strdup_printf("AT+CRSM=178, %d, %d, %d, %d", ef, p1, p2, p3);          /*command - 178 : READ RECORD*/
1780
1781         req = tcore_at_request_new(cmd_str, "+CRSM:", TCORE_AT_SINGLELINE);
1782
1783         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
1784
1785         tcore_pending_set_request_data(pending, 0, req);
1786         tcore_pending_set_response_callback(pending, _response_get_file_data, hal);
1787         tcore_pending_link_user_request(pending, ur);
1788         tcore_hal_send_request(hal, pending);
1789
1790         free(cmd_str);
1791         dbg(" Function exit");
1792         return TRUE;
1793 }
1794
1795 static TReturn _get_retry_count(CoreObject *o, UserRequest *ur)
1796 {
1797         TcoreHal *hal = NULL;
1798         TcoreATRequest *req = NULL;
1799         TcorePending *pending = NULL;
1800         char *cmd_str = NULL;
1801         int lock_type = 0;
1802         struct s_sim_property *sp = NULL;
1803         const struct treq_sim_get_lock_info *req_data = NULL;
1804
1805         dbg(" Function entry ");
1806
1807         hal = tcore_object_get_hal(o);
1808         pending = tcore_pending_new(o, 0);
1809         req_data = tcore_user_request_ref_data(ur, NULL);
1810         sp = tcore_sim_ref_userdata(o);
1811
1812         switch (sp->current_sec_op) {
1813         case SEC_PIN1_VERIFY:
1814         case SEC_PIN1_CHANGE:
1815         case SEC_PIN1_ENABLE:
1816         case SEC_PIN1_DISABLE:
1817                 lock_type = 1;
1818                 break;
1819
1820         case SEC_PIN2_VERIFY:
1821         case SEC_PIN2_CHANGE:
1822         case SEC_PIN2_ENABLE:
1823         case SEC_PIN2_DISABLE:
1824         case SEC_FDN_ENABLE:
1825         case SEC_FDN_DISABLE:
1826                 lock_type = 2;
1827                 break;
1828
1829         case SEC_PUK1_VERIFY:
1830                 lock_type = 3;
1831                 break;
1832
1833         case SEC_PUK2_VERIFY:
1834                 lock_type = 4;
1835                 break;
1836
1837         case SEC_NET_ENABLE:
1838         case SEC_NET_DISABLE:
1839                 lock_type = 5;
1840                 break;
1841
1842         case SEC_NS_ENABLE:
1843         case SEC_NS_DISABLE:
1844                 lock_type = 6;
1845                 break;
1846
1847         case SEC_SP_ENABLE:
1848         case SEC_SP_DISABLE:
1849                 lock_type = 7;
1850                 break;
1851
1852         case SEC_CP_ENABLE:
1853         case SEC_CP_DISABLE:
1854                 lock_type = 8;
1855                 break;
1856
1857         case SEC_ADM_VERIFY:
1858                 lock_type = 9;
1859                 break;
1860
1861         default:
1862                 break;
1863         }
1864
1865         cmd_str = g_strdup_printf("AT+XPINCNT=%d", lock_type);
1866         req = tcore_at_request_new(cmd_str, "+XPINCNT:", TCORE_AT_SINGLELINE);
1867         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
1868
1869         tcore_pending_set_request_data(pending, 0, req);
1870         tcore_pending_set_response_callback(pending, _on_response_get_retry_count, hal);
1871         tcore_pending_link_user_request(pending, ur);
1872         tcore_hal_send_request(hal, pending);
1873
1874         free(cmd_str);
1875         dbg(" Function exit");
1876         return TCORE_RETURN_SUCCESS;
1877 }
1878
1879
1880 static gboolean on_event_facility_lock_status(CoreObject *o, const void *event_info, void *user_data)
1881 {
1882         struct s_sim_property *sp = NULL;
1883         char *line = NULL;
1884         GSList *tokens = NULL;
1885         GSList *lines = NULL;
1886
1887         dbg("Function entry");
1888         return TRUE;
1889
1890         sp = tcore_sim_ref_userdata(o);
1891         lines = (GSList *) event_info;
1892         if (1 != g_slist_length(lines)) {
1893                 dbg("unsolicited msg but multiple line");
1894                 goto OUT;
1895         }
1896         line = (char *) (lines->data);
1897         tokens = tcore_at_tok_new(line);
1898         if (g_slist_length(tokens) != 1) {
1899                 msg("invalid message");
1900                 tcore_at_tok_free(tokens);
1901                 return TRUE;
1902         }
1903
1904 OUT:
1905         dbg(" Function exit");
1906         if (NULL != tokens)
1907                 tcore_at_tok_free(tokens);
1908         return TRUE;
1909 }
1910
1911
1912 static gboolean on_event_pin_status(CoreObject *o, const void *event_info, void *user_data)
1913 {
1914
1915         struct s_sim_property *sp = NULL;
1916         enum tel_sim_status sim_status = SIM_STATUS_INITIALIZING;
1917         GSList *tokens = NULL;
1918         GSList *lines = NULL;
1919         const char *line = NULL;
1920         int sim_state = 0;
1921         int sms_state = 0;
1922
1923         dbg(" Function entry ");
1924
1925         sp = tcore_sim_ref_userdata(o);
1926
1927         lines = (GSList *) event_info;
1928         if (1 != g_slist_length(lines)) {
1929                 dbg("unsolicited msg but multiple line");
1930                 goto OUT;
1931         }
1932         line = (char *) (lines->data);
1933
1934         tokens = tcore_at_tok_new(line);
1935
1936         if (g_slist_length(tokens) == 4) {
1937                 sim_state = atoi(g_slist_nth_data(tokens, 1));
1938                 sms_state = atoi(g_slist_nth_data(tokens, 3));
1939         } else if (g_slist_length(tokens) == 1)
1940                 sim_state = atoi(g_slist_nth_data(tokens, 0));
1941         else {
1942                 msg("invalid message");
1943                 tcore_at_tok_free(tokens);
1944                 return TRUE;
1945         }
1946
1947         switch (sim_state) {
1948         case 0:                                                         // sim state = SIM not present
1949                 sim_status = SIM_STATUS_CARD_NOT_PRESENT;
1950                 dbg("NO SIM");
1951                 break;
1952
1953         case 1:                                                         // sim state = PIN verification needed
1954                 sim_status = SIM_STATUS_PIN_REQUIRED;
1955                 dbg(" PIN required");
1956                 break;
1957
1958         case 2:                                                         // sim state = PIN verification not needed \96 Ready
1959         case 3:                                                         // sim state = PIN verified \96 Ready
1960                 sim_status = SIM_STATUS_INITIALIZING;
1961                 dbg(" Inside PIN disabled at BOOT UP");
1962                 break;
1963
1964         case 4:                                                         // sim state = PUK verification needed
1965                 sim_status = SIM_STATUS_PUK_REQUIRED;
1966                 dbg(" PUK required");
1967                 break;
1968
1969         case 5:                                                         // sim state = SIM permanently blocked
1970                 sim_status = SIM_STATUS_CARD_BLOCKED;
1971                 dbg(" Card permanently blocked");
1972                 break;
1973
1974         case 6:                                                         // sim state = SIM error
1975                 sim_status = SIM_STATUS_CARD_ERROR;
1976                 dbg("SIM card error ");
1977                 break;
1978
1979         case 7:                                                         // sim state = ready for attach (+COPS)
1980                 sim_status = SIM_STATUS_INIT_COMPLETED;
1981                 dbg("Modem init completed");
1982                 break;
1983
1984         case 8:                                                         // sim state = SIM Technical Problem
1985                 sim_status = SIM_STATUS_CARD_ERROR;
1986                 dbg("SIM unavailable");
1987                 break;
1988
1989         case 9:                                                         // sim state = SIM removed
1990                 sim_status = SIM_STATUS_CARD_REMOVED;
1991                 dbg("SIM removed");
1992                 break;
1993
1994         case 99:                                                            // sim state = SIM State Unknown
1995                 sim_status = SIM_STATUS_UNKNOWN;
1996                 dbg("SIM State Unknown");
1997                 break;
1998
1999         case 12:
2000                 dbg("SIM Status : %d", sim_status);
2001                 goto OUT;
2002
2003         default:
2004                 dbg(" not handled SEC lock type ");
2005                 break;
2006         }
2007
2008         switch (sim_status) {
2009         case SIM_STATUS_INIT_COMPLETED:
2010                 if (tcore_sim_get_type(o) == SIM_TYPE_UNKNOWN)
2011                         _get_sim_type(o);
2012                 else
2013                         _sim_status_update(o, sim_status);
2014
2015                 if (sms_state) {
2016                         struct tnoti_sms_ready_status readyStatusInfo = {0, };
2017                         CoreObject *sms;
2018                         TcorePlugin *plugin;
2019
2020                         dbg("SMS Ready");
2021                         readyStatusInfo.status = TRUE;
2022                         plugin = tcore_object_ref_plugin(o);
2023                         sms = tcore_plugin_ref_core_object(plugin, CORE_OBJECT_TYPE_SMS);
2024                         tcore_sms_set_ready_status(sms, readyStatusInfo.status);
2025
2026                         tcore_server_send_notification(tcore_plugin_ref_server(plugin), sms, TNOTI_SMS_DEVICE_READY, sizeof(struct tnoti_sms_ready_status), &readyStatusInfo);
2027                 }
2028                 break;
2029
2030         case SIM_STATUS_INITIALIZING:
2031         case SIM_STATUS_PIN_REQUIRED:
2032         case SIM_STATUS_PUK_REQUIRED:
2033         case SIM_STATUS_CARD_BLOCKED:
2034         case SIM_STATUS_NCK_REQUIRED:
2035         case SIM_STATUS_NSCK_REQUIRED:
2036         case SIM_STATUS_SPCK_REQUIRED:
2037         case SIM_STATUS_CCK_REQUIRED:
2038         case SIM_STATUS_LOCK_REQUIRED:
2039                 _sim_status_update(o, sim_status);
2040                 break;
2041
2042         case SIM_STATUS_CARD_REMOVED:
2043         case SIM_STATUS_CARD_NOT_PRESENT:
2044         case SIM_STATUS_CARD_ERROR:
2045                 if (sim_status == SIM_STATUS_CARD_NOT_PRESENT && tcore_sim_get_status(o) != SIM_STATUS_UNKNOWN) {
2046                         dbg("[SIM]SIM CARD REMOVED!!");
2047                         sim_status = SIM_STATUS_CARD_REMOVED;
2048                 }
2049
2050                 tcore_sim_set_type(o, SIM_TYPE_UNKNOWN);
2051                 _sim_status_update(o, sim_status);
2052                 break;
2053
2054         default:
2055                 dbg("not handled status[%d]", sim_status);
2056
2057                 break;
2058         }
2059 OUT:
2060         dbg(" Function exit");
2061         if (NULL != tokens)
2062                 tcore_at_tok_free(tokens);
2063         return TRUE;
2064 }
2065
2066 static void on_response_get_sim_status(TcorePending *p, int data_len, const void *data, void *user_data)
2067 {
2068         const TcoreATResponse *resp = data;
2069         CoreObject *co_sim = NULL;
2070
2071         dbg(" Function entry ");
2072
2073         co_sim = tcore_pending_ref_core_object(p);
2074
2075         if (resp->success > 0) {
2076                 dbg("RESPONSE OK");
2077                 if (resp->lines)
2078                         on_event_pin_status(co_sim, resp->lines, NULL);
2079         } else {
2080                 dbg("RESPONSE NOK");
2081         }
2082
2083         dbg(" Function exit");
2084 }
2085
2086 static enum tcore_hook_return on_hook_modem_power(Server *s, CoreObject *source, enum tcore_notification_command command,
2087                                                                                            unsigned int data_len, void *data, void *user_data)
2088 {
2089         TcorePlugin *plugin = tcore_object_ref_plugin(source);
2090         CoreObject *co_sim = tcore_plugin_ref_core_object(plugin, CORE_OBJECT_TYPE_SIM);
2091
2092         if (co_sim == NULL)
2093                 return TCORE_HOOK_RETURN_CONTINUE;
2094
2095         dbg("Get SIM status");
2096
2097         sim_prepare_and_send_pending_request(co_sim, "AT+XSIMSTATE?", "+XSIMSTATE:", TCORE_AT_SINGLELINE, on_response_get_sim_status);
2098
2099         return TCORE_HOOK_RETURN_CONTINUE;
2100 }
2101
2102 static void on_response_verify_pins(TcorePending *p, int data_len, const void *data, void *user_data)
2103 {
2104         const TcoreATResponse *resp = data;
2105         UserRequest *ur = NULL;
2106         CoreObject *co_sim = NULL;
2107         struct s_sim_property *sp = NULL;
2108         GSList *tokens = NULL;
2109         struct tresp_sim_verify_pins res;
2110         GQueue *queue = NULL;
2111         const char *line;
2112         int err;
2113
2114         dbg(" Function entry ");
2115
2116         co_sim = tcore_pending_ref_core_object(p);
2117         sp = tcore_sim_ref_userdata(co_sim);
2118         ur = tcore_pending_ref_user_request(p);
2119
2120         memset(&res, 0, sizeof(struct tresp_sim_verify_pins));
2121
2122         if (resp->success > 0) {
2123                 dbg("RESPONSE OK");
2124                 res.result = SIM_PIN_OPERATION_SUCCESS;
2125                 res.pin_type = _sim_get_current_pin_facility(sp->current_sec_op);
2126                 if (res.pin_type == SIM_PTYPE_PIN1 || res.pin_type == SIM_PTYPE_SIM) {
2127                         if (tcore_sim_get_status(co_sim) != SIM_STATUS_INIT_COMPLETED)
2128                                 _sim_status_update(co_sim, SIM_STATUS_INITIALIZING);
2129                 }
2130                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_verify_pins), &res);
2131         } else {
2132                 dbg("RESPONSE NOK");
2133                 line = (const char *) resp->final_response;
2134                 tokens = tcore_at_tok_new(line);
2135                 if (g_slist_length(tokens) < 1) {
2136                         dbg("err cause not specified or string corrupted");
2137                         res.result = TCORE_RETURN_3GPP_ERROR;
2138                         tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_verify_pins), &res);
2139                 } else {
2140                         err = atoi(g_slist_nth_data(tokens, 0));
2141                         dbg("on_response_verify_pins: err = %d", err);
2142                         queue = tcore_object_ref_user_data(co_sim);
2143                         ur = tcore_user_request_ref(ur);
2144                         _get_retry_count(co_sim, ur);
2145                 }
2146                 tcore_at_tok_free(tokens);
2147         }
2148         dbg(" Function exit");
2149 }
2150
2151 static void on_response_verify_puks(TcorePending *p, int data_len, const void *data, void *user_data)
2152 {
2153         const TcoreATResponse *resp = data;
2154         UserRequest *ur = NULL;
2155         CoreObject *co_sim = NULL;
2156         struct s_sim_property *sp = NULL;
2157         GSList *tokens = NULL;
2158         struct tresp_sim_verify_puks res;
2159         GQueue *queue = NULL;
2160         const char *line;
2161         int err;
2162
2163         dbg(" Function entry ");
2164
2165         co_sim = tcore_pending_ref_core_object(p);
2166         sp = tcore_sim_ref_userdata(co_sim);
2167         ur = tcore_pending_ref_user_request(p);
2168
2169         memset(&res, 0, sizeof(struct tresp_sim_verify_pins));
2170
2171         if (resp->success > 0) {
2172                 dbg("RESPONSE OK");
2173                 res.result = SIM_PIN_OPERATION_SUCCESS;
2174                 res.pin_type = _sim_get_current_pin_facility(sp->current_sec_op);
2175                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_verify_pins), &res);
2176         } else {
2177                 dbg("RESPONSE NOK");
2178                 line = (const char *) resp->final_response;
2179                 tokens = tcore_at_tok_new(line);
2180
2181                 if (g_slist_length(tokens) < 1) {
2182                         dbg("err cause not specified or string corrupted");
2183                         res.result = TCORE_RETURN_3GPP_ERROR;
2184                         tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_verify_pins), &res);
2185                 } else {
2186                         err = atoi(g_slist_nth_data(tokens, 0));
2187                         queue = tcore_object_ref_user_data(co_sim);
2188                         ur = tcore_user_request_ref(ur);
2189                         _get_retry_count(co_sim, ur);
2190                 }
2191                 tcore_at_tok_free(tokens);
2192         }
2193         dbg(" Function exit");
2194 }
2195
2196 static void on_response_change_pins(TcorePending *p, int data_len, const void *data, void *user_data)
2197 {
2198         const TcoreATResponse *resp = data;
2199         UserRequest *ur = NULL;
2200         CoreObject *co_sim = NULL;
2201         struct s_sim_property *sp = NULL;
2202         GSList *tokens = NULL;
2203         struct tresp_sim_change_pins res;
2204         GQueue *queue;
2205         const char *line;
2206         int err;
2207
2208         dbg(" Function entry ");
2209
2210         co_sim = tcore_pending_ref_core_object(p);
2211         sp = tcore_sim_ref_userdata(co_sim);
2212         ur = tcore_pending_ref_user_request(p);
2213
2214         memset(&res, 0, sizeof(struct tresp_sim_change_pins));
2215
2216         if (resp->success > 0) {
2217                 dbg("RESPONSE OK");
2218                 res.result = SIM_PIN_OPERATION_SUCCESS;
2219                 res.pin_type = _sim_get_current_pin_facility(sp->current_sec_op);
2220                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_change_pins), &res);
2221         } else {
2222                 dbg("RESPONSE NOK");
2223                 line = (const char *) resp->final_response;
2224                 tokens = tcore_at_tok_new(line);
2225
2226                 if (g_slist_length(tokens) < 1) {
2227                         dbg("err cause not specified or string corrupted");
2228                         res.result = TCORE_RETURN_3GPP_ERROR;
2229                         tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_change_pins), &res);
2230                 } else {
2231                         err = atoi(g_slist_nth_data(tokens, 0));
2232                         queue = tcore_object_ref_user_data(co_sim);
2233                         ur = tcore_user_request_ref(ur);
2234                         _get_retry_count(co_sim, ur);
2235                 }
2236                 tcore_at_tok_free(tokens);
2237         }
2238         dbg(" Function exit");
2239 }
2240
2241 static void on_response_get_facility_status(TcorePending *p, int data_len, const void *data, void *user_data)
2242 {
2243         const TcoreATResponse *resp = data;
2244         UserRequest *ur = NULL;
2245         GSList *tokens = NULL;
2246         struct tresp_sim_get_facility_status *res = user_data;
2247         const char *line;
2248
2249         dbg(" Function entry ");
2250
2251         ur = tcore_pending_ref_user_request(p);
2252
2253         res->result = SIM_PIN_OPERATION_SUCCESS;
2254
2255         if (resp->success > 0) {
2256                 dbg("RESPONSE OK");
2257                 if (resp->lines) {
2258                         line = (const char *) resp->lines->data;
2259                         tokens = tcore_at_tok_new(line);
2260                         if (g_slist_length(tokens) != 1) {
2261                                 msg("invalid message");
2262                                 tcore_at_tok_free(tokens);
2263                                 return;
2264                         }
2265                 }
2266                 res->b_enable = atoi(g_slist_nth_data(tokens, 0));
2267         } else {
2268                 dbg("RESPONSE NOK");
2269                 res->result = SIM_INCOMPATIBLE_PIN_OPERATION;
2270         }
2271
2272         if (ur) {
2273                 tcore_user_request_send_response(ur, _find_resp_command(ur),
2274                                                                                  sizeof(struct tresp_sim_get_facility_status), res);
2275         }
2276         tcore_at_tok_free(tokens);
2277         g_free(res);
2278         dbg(" Function exit");
2279 }
2280
2281 static void on_response_enable_facility(TcorePending *p, int data_len, const void *data, void *user_data)
2282 {
2283         const TcoreATResponse *resp = data;
2284         UserRequest *ur = NULL;
2285         CoreObject *co_sim = NULL;
2286         struct s_sim_property *sp = NULL;
2287         GSList *tokens = NULL;
2288         struct tresp_sim_enable_facility res;
2289         GQueue *queue;
2290         const char *line;
2291
2292         dbg(" Function entry ");
2293
2294         co_sim = tcore_pending_ref_core_object(p);
2295         sp = tcore_sim_ref_userdata(co_sim);
2296         ur = tcore_pending_ref_user_request(p);
2297
2298         memset(&res, 0, sizeof(struct tresp_sim_enable_facility));
2299
2300         res.result = SIM_CARD_ERROR;
2301         res.type = _sim_get_current_pin_facility(sp->current_sec_op);
2302
2303         if (resp->success > 0) {
2304                 dbg("RESPONSE OK");
2305                 if (resp->lines) {
2306                         line = (const char *) resp->lines->data;
2307                         tokens = tcore_at_tok_new(line);
2308                         if (g_slist_length(tokens) != 1) {
2309                                 msg("invalid message");
2310                                 tcore_user_request_send_response(ur, _find_resp_command(ur),
2311                                                                                          sizeof(struct tresp_sim_enable_facility), &res);
2312                                 tcore_at_tok_free(tokens);
2313                                 return;
2314                         }
2315                 }
2316                 res.result = SIM_PIN_OPERATION_SUCCESS;
2317                 if (ur) {
2318                         tcore_user_request_send_response(ur, _find_resp_command(ur),
2319                                                                                          sizeof(struct tresp_sim_enable_facility), &res);
2320                 }
2321                 tcore_at_tok_free(tokens);
2322         } else {
2323                 dbg("RESPONSE NOK");
2324                 queue = tcore_object_ref_user_data(co_sim);
2325                 ur = tcore_user_request_ref(ur);
2326                 _get_retry_count(co_sim, ur);
2327         }
2328         dbg(" Function exit");
2329 }
2330
2331 static void on_response_disable_facility(TcorePending *p, int data_len, const void *data, void *user_data)
2332 {
2333         const TcoreATResponse *resp = data;
2334         UserRequest *ur = NULL;
2335         CoreObject *co_sim = NULL;
2336         struct s_sim_property *sp = NULL;
2337         GSList *tokens = NULL;
2338         struct tresp_sim_disable_facility res;
2339         GQueue *queue;
2340         const char *line;
2341
2342         dbg(" Function entry ");
2343
2344         co_sim = tcore_pending_ref_core_object(p);
2345         sp = tcore_sim_ref_userdata(co_sim);
2346         ur = tcore_pending_ref_user_request(p);
2347
2348         memset(&res, 0, sizeof(struct tresp_sim_disable_facility));
2349
2350         res.result = SIM_CARD_ERROR;
2351         res.type = _sim_get_current_pin_facility(sp->current_sec_op);
2352
2353         if (resp->success > 0) {
2354                 dbg("RESPONSE OK");
2355                 if (resp->lines) {
2356                         line = (const char *) resp->lines->data;
2357                         tokens = tcore_at_tok_new(line);
2358                         if (g_slist_length(tokens) != 1) {
2359                                 msg("invalid message");
2360                                 tcore_user_request_send_response(ur, _find_resp_command(ur),
2361                                                                                          sizeof(struct tresp_sim_disable_facility), &res);
2362                                 tcore_at_tok_free(tokens);
2363                                 return;
2364                         }
2365                 }
2366                 res.result = SIM_PIN_OPERATION_SUCCESS;
2367                 if (ur) {
2368                         tcore_user_request_send_response(ur, _find_resp_command(ur),
2369                                                                                          sizeof(struct tresp_sim_disable_facility), &res);
2370                 }
2371                 tcore_at_tok_free(tokens);
2372         } else {
2373                 dbg("RESPONSE NOK");
2374                 queue = tcore_object_ref_user_data(co_sim);
2375                 ur = tcore_user_request_ref(ur);
2376                 _get_retry_count(co_sim, ur);
2377         }
2378         dbg(" Function exit");
2379 }
2380
2381 static void on_response_get_lock_info(TcorePending *p, int data_len, const void *data, void *user_data)
2382 {
2383         const TcoreATResponse *resp = data;
2384         UserRequest *ur = NULL;
2385         CoreObject *co_sim = NULL;
2386         struct s_sim_property *sp = NULL;
2387         GSList *tokens = NULL;
2388         const char *line;
2389         struct tresp_sim_verify_pins v_pin = {0, };
2390         struct tresp_sim_verify_puks v_puk = {0, };
2391         struct tresp_sim_change_pins change_pin = {0, };
2392         struct tresp_sim_disable_facility dis_facility = {0, };
2393         struct tresp_sim_enable_facility en_facility = {0, };
2394         int lock_type;
2395         int attempts_left = 0;
2396         int time_penalty = 0;
2397
2398         dbg(" Function entry ");
2399
2400         co_sim = tcore_pending_ref_core_object(p);
2401         sp = tcore_sim_ref_userdata(co_sim);
2402         ur = tcore_pending_ref_user_request(p);
2403
2404         if (resp->success > 0) {
2405                 dbg("RESPONSE OK");
2406                 if (resp->lines) {
2407                         line = (const char *) resp->lines->data;
2408                         tokens = tcore_at_tok_new(line);
2409                         if (g_slist_length(tokens) != 3) {
2410                                 msg("invalid message");
2411                                 tcore_at_tok_free(tokens);
2412                                 return;
2413                         }
2414                 }
2415                 lock_type = atoi(g_slist_nth_data(tokens, 0));
2416                 attempts_left = atoi(g_slist_nth_data(tokens, 1));
2417                 time_penalty = atoi(g_slist_nth_data(tokens, 2));
2418
2419                 switch (sp->current_sec_op) {
2420                 case SEC_PIN1_VERIFY:
2421                 case SEC_PIN2_VERIFY:
2422                 case SEC_SIM_VERIFY:
2423                 case SEC_ADM_VERIFY:
2424                         v_pin.result = SIM_INCORRECT_PASSWORD;
2425                         v_pin.pin_type = _sim_get_current_pin_facility(sp->current_sec_op);
2426                         v_pin.retry_count = attempts_left;
2427                         tcore_user_request_send_response(ur, _find_resp_command(ur),
2428                                                                                          sizeof(struct tresp_sim_verify_pins), &v_pin);
2429                         break;
2430
2431                 case SEC_PUK1_VERIFY:
2432                 case SEC_PUK2_VERIFY:
2433                         v_puk.result = SIM_INCORRECT_PASSWORD;
2434                         v_puk.pin_type = _sim_get_current_pin_facility(sp->current_sec_op);
2435                         v_puk.retry_count = attempts_left;
2436                         tcore_user_request_send_response(ur, _find_resp_command(ur),
2437                                                                                          sizeof(struct tresp_sim_verify_puks), &v_puk);
2438                         break;
2439
2440                 case SEC_PIN1_CHANGE:
2441                 case SEC_PIN2_CHANGE:
2442                         change_pin.result = SIM_INCORRECT_PASSWORD;
2443                         change_pin.pin_type = _sim_get_current_pin_facility(sp->current_sec_op);
2444                         change_pin.retry_count = attempts_left;
2445                         tcore_user_request_send_response(ur, _find_resp_command(ur),
2446                                                                                          sizeof(struct tresp_sim_change_pins), &change_pin);
2447                         break;
2448
2449                 case SEC_PIN1_DISABLE:
2450                 case SEC_PIN2_DISABLE:
2451                 case SEC_FDN_DISABLE:
2452                 case SEC_SIM_DISABLE:
2453                 case SEC_NET_DISABLE:
2454                 case SEC_NS_DISABLE:
2455                 case SEC_SP_DISABLE:
2456                 case SEC_CP_DISABLE:
2457                         dis_facility.result = SIM_INCORRECT_PASSWORD;
2458                         dis_facility.type = _sim_get_current_pin_facility(sp->current_sec_op);
2459                         dis_facility.retry_count = attempts_left;
2460                         tcore_user_request_send_response(ur, _find_resp_command(ur),
2461                                                                                          sizeof(struct tresp_sim_disable_facility), &dis_facility);
2462                         break;
2463
2464                 case SEC_PIN1_ENABLE:
2465                 case SEC_PIN2_ENABLE:
2466                 case SEC_FDN_ENABLE:
2467                 case SEC_SIM_ENABLE:
2468                 case SEC_NET_ENABLE:
2469                 case SEC_NS_ENABLE:
2470                 case SEC_SP_ENABLE:
2471                 case SEC_CP_ENABLE:
2472                         en_facility.result = SIM_INCORRECT_PASSWORD;
2473                         en_facility.type = _sim_get_current_pin_facility(sp->current_sec_op);
2474                         en_facility.retry_count = attempts_left;
2475                         tcore_user_request_send_response(ur, _find_resp_command(ur),
2476                                                                                          sizeof(struct tresp_sim_enable_facility), &en_facility);
2477                         break;
2478
2479                 default:
2480                         dbg("not handled sec op[%d]", sp->current_sec_op);
2481                         break;
2482                 }
2483                 tcore_at_tok_free(tokens);
2484         }
2485         dbg(" Function exit");
2486 }
2487
2488 static void on_response_update_file(TcorePending *p, int data_len, const void *data, void *user_data)
2489 {
2490         const TcoreATResponse *resp = data;
2491         UserRequest *ur = NULL;
2492         CoreObject *co_sim = NULL;
2493         struct tresp_sim_set_data resp_cf = {0, };
2494         struct tresp_sim_set_data resp_language = {0, };
2495         struct s_sim_property *sp = NULL;
2496         GSList *tokens = NULL;
2497         enum tel_sim_access_result result = SIM_CARD_ERROR;
2498         const char *line;
2499         int sw1 = 0;
2500         int sw2 = 0;
2501
2502         dbg(" Function entry ");
2503
2504         co_sim = tcore_pending_ref_core_object(p);
2505         ur = tcore_pending_ref_user_request(p);
2506         sp = (struct s_sim_property *) tcore_user_request_ref_metainfo(ur, NULL);
2507
2508         if (resp->success > 0) {
2509                 dbg("RESPONSE OK");
2510                 if (resp->lines) {
2511                         line = (const char *) resp->lines->data;
2512                         tokens = tcore_at_tok_new(line);
2513                         if (g_slist_length(tokens) != 2) {
2514                                 msg("invalid message");
2515                                 goto OUT;
2516                         }
2517                 }
2518                 sw1 = atoi(g_slist_nth_data(tokens, 0));
2519                 sw2 = atoi(g_slist_nth_data(tokens, 1));
2520
2521                 if ((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
2522                         result = SIM_ACCESS_SUCCESS;
2523                 } else {
2524                         result = _decode_status_word(sw1, sw2);
2525                 }
2526         } else {
2527                 dbg("RESPONSE NOK");
2528                 result = SIM_ACCESS_FAILED;
2529         }
2530 OUT:
2531         switch (sp->file_id) {
2532         case SIM_EF_CPHS_CALL_FORWARD_FLAGS:
2533         case SIM_EF_USIM_CFIS:
2534                 resp_cf.result = result;
2535                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_set_data), &resp_cf);
2536                 break;
2537
2538         case SIM_EF_ELP:
2539         case SIM_EF_LP:
2540         case SIM_EF_USIM_LI:
2541         case SIM_EF_USIM_PL:
2542                 resp_language.result = result;
2543                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_set_data), &resp_language);
2544                 break;
2545
2546         default:
2547                 dbg("Invalid File ID - %d", sp->file_id)
2548                 break;
2549         }
2550         tcore_at_tok_free(tokens);
2551         dbg(" Function exit");
2552 }
2553
2554 static void on_response_transmit_apdu(TcorePending *p, int data_len, const void *data, void *user_data)
2555 {
2556         const TcoreATResponse *resp = data;
2557         UserRequest *ur = NULL;
2558         CoreObject *co_sim = NULL;
2559         GSList *tokens = NULL;
2560         struct tresp_sim_transmit_apdu res;
2561         const char *line;
2562
2563         dbg(" Function entry ");
2564
2565         co_sim = tcore_pending_ref_core_object(p);
2566         ur = tcore_pending_ref_user_request(p);
2567
2568         memset(&res, 0, sizeof(struct tresp_sim_transmit_apdu));
2569         res.result = SIM_ACCESS_FAILED;
2570
2571         if (resp->success > 0) {
2572                 dbg("RESPONSE OK");
2573                 if (resp->lines) {
2574                         char *tmp = NULL;
2575                         char *decoded_data = NULL;
2576                         line = (const char *) resp->lines->data;
2577                         tokens = tcore_at_tok_new(line);
2578                         if (g_slist_length(tokens) != 2) {
2579                                 msg("invalid message");
2580                                 goto OUT;
2581                         }
2582                         res.apdu_resp_length = atoi(g_slist_nth_data(tokens, 0)) / 2;
2583
2584                         tmp = util_removeQuotes(g_slist_nth_data(tokens, 1));
2585                         decoded_data = util_hexStringToBytes(tmp);
2586
2587                         memcpy((char *) res.apdu_resp, decoded_data, res.apdu_resp_length);
2588                         free(tmp);
2589                         free(decoded_data);
2590                         res.result = SIM_ACCESS_SUCCESS;
2591                 }
2592         } else {
2593                 dbg("RESPONSE NOK");
2594         }
2595 OUT:
2596         if (ur) {
2597                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_transmit_apdu), &res);
2598         }
2599         tcore_at_tok_free(tokens);
2600         dbg(" Function exit");
2601 }
2602
2603 static void on_response_get_atr(TcorePending *p, int data_len, const void *data, void *user_data)
2604 {
2605         const TcoreATResponse *resp = data;
2606         UserRequest *ur = NULL;
2607         GSList *tokens = NULL;
2608         struct tresp_sim_get_atr res;
2609         const char *line;
2610
2611         dbg(" Function entry ");
2612
2613         memset(&res, 0, sizeof(struct tresp_sim_get_atr));
2614         ur = tcore_pending_ref_user_request(p);
2615
2616         res.result = SIM_ACCESS_FAILED;
2617         if (resp->success > 0) {
2618                 dbg("RESPONSE OK");
2619                 if (resp->lines) {
2620                         char *tmp = NULL;
2621                         char *decoded_data = NULL;
2622                         line = (const char *) resp->lines->data;
2623                         tokens = tcore_at_tok_new(line);
2624                         if (g_slist_length(tokens) < 1) {
2625                                 msg("invalid message");
2626                                 goto OUT;
2627                         }
2628
2629                         tmp = util_removeQuotes(g_slist_nth_data(tokens, 0));
2630                         decoded_data = util_hexStringToBytes(tmp);
2631
2632                         res.atr_length = strlen(decoded_data);
2633                         memcpy((char *) res.atr, decoded_data, res.atr_length);
2634                         free(tmp);
2635                         free(decoded_data);
2636                         res.result = SIM_ACCESS_SUCCESS;
2637                 }
2638         } else {
2639                 dbg("RESPONSE NOK");
2640         }
2641
2642 OUT:
2643         if (ur) {
2644                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_get_atr), &res);
2645         }
2646         tcore_at_tok_free(tokens);
2647         dbg(" Function exit");
2648 }
2649
2650 static TReturn s_verify_pins(CoreObject *o, UserRequest *ur)
2651 {
2652         TcoreHal *hal = NULL;
2653         TcoreATRequest *req = NULL;
2654         TcorePending *pending = NULL;
2655         char *cmd_str = NULL;
2656         const struct treq_sim_verify_pins *req_data = NULL;
2657         struct s_sim_property *sp = NULL;
2658         TReturn ret = TCORE_RETURN_FAILURE;
2659
2660         dbg(" Function entry ");
2661
2662         if (!o || !ur)
2663                 return TCORE_RETURN_EINVAL;
2664
2665         hal = tcore_object_get_hal(o);
2666         if(FALSE == tcore_hal_get_power_state(hal)){
2667                 dbg("cp not ready/n");
2668                 return TCORE_RETURN_ENOSYS;
2669         }
2670
2671         sp = tcore_sim_ref_userdata(o);
2672         pending = tcore_pending_new(o, 0);
2673         req_data = tcore_user_request_ref_data(ur, NULL);
2674
2675         if (req_data->pin_type == SIM_PTYPE_PIN1) {
2676                 sp->current_sec_op = SEC_PIN1_VERIFY;
2677                 cmd_str = g_strdup_printf("AT+CPIN=\"%s\"", req_data->pin);
2678         } else if (req_data->pin_type == SIM_PTYPE_PIN2) {
2679                 sp->current_sec_op = SEC_PIN2_VERIFY;
2680                 cmd_str = g_strdup_printf("AT+CPIN2=\"%s\"", req_data->pin);
2681         } else if (req_data->pin_type == SIM_PTYPE_SIM) {
2682                 sp->current_sec_op = SEC_SIM_VERIFY;
2683                 cmd_str = g_strdup_printf("AT+CPIN=\"%s\"", req_data->pin);
2684         } else if (req_data->pin_type == SIM_PTYPE_ADM) {
2685                 sp->current_sec_op = SEC_ADM_VERIFY;
2686                 cmd_str = g_strdup_printf("AT+CPIN=\"%s\"", req_data->pin);
2687         } else {
2688                 return TCORE_RETURN_EINVAL;
2689         }
2690
2691         req = tcore_at_request_new(cmd_str, NULL, TCORE_AT_NO_RESULT);
2692
2693         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
2694
2695         tcore_pending_set_request_data(pending, 0, req);
2696         tcore_pending_set_response_callback(pending, on_response_verify_pins, hal);
2697         tcore_pending_link_user_request(pending, ur);
2698         ret = tcore_hal_send_request(hal, pending);
2699
2700         free(cmd_str);
2701         dbg(" Function exit");
2702         return ret;
2703 }
2704
2705 static TReturn s_verify_puks(CoreObject *o, UserRequest *ur)
2706 {
2707         TcoreHal *hal = NULL;
2708         TcoreATRequest *req = NULL;
2709         TcorePending *pending = NULL;
2710         char *cmd_str = NULL;
2711         const struct treq_sim_verify_puks *req_data;
2712         struct s_sim_property *sp = NULL;
2713         TReturn ret = TCORE_RETURN_FAILURE;
2714
2715         dbg(" Function entry ");
2716
2717         if (!o || !ur)
2718                 return TCORE_RETURN_EINVAL;
2719
2720         hal = tcore_object_get_hal(o);
2721         if(FALSE == tcore_hal_get_power_state(hal)){
2722                 dbg("cp not ready/n");
2723                 return TCORE_RETURN_ENOSYS;
2724         }
2725
2726         sp = tcore_sim_ref_userdata(o);
2727         pending = tcore_pending_new(o, 0);
2728         req_data = tcore_user_request_ref_data(ur, NULL);
2729
2730         if (req_data->puk_type == SIM_PTYPE_PUK1) {
2731                 sp->current_sec_op = SEC_PUK1_VERIFY;
2732                 cmd_str = g_strdup_printf("AT+CPIN=\"%s\", \"%s\"", req_data->puk, req_data->pin);
2733         } else if (req_data->puk_type == SIM_PTYPE_PUK2) {
2734                 sp->current_sec_op = SEC_PUK2_VERIFY;
2735                 cmd_str = g_strdup_printf("AT+CPIN2=\"%s\", \"%s\"", req_data->puk, req_data->pin);
2736         } else {
2737                 return TCORE_RETURN_EINVAL;
2738         }
2739         req = tcore_at_request_new(cmd_str, NULL, TCORE_AT_NO_RESULT);
2740
2741         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
2742
2743         tcore_pending_set_request_data(pending, 0, req);
2744         tcore_pending_set_response_callback(pending, on_response_verify_puks, hal);
2745         tcore_pending_link_user_request(pending, ur);
2746         ret = tcore_hal_send_request(hal, pending);
2747
2748         free(cmd_str);
2749         dbg(" Function exit");
2750         return ret;
2751 }
2752
2753 static TReturn s_change_pins(CoreObject *o, UserRequest *ur)
2754 {
2755         TcoreHal *hal = NULL;
2756         TcoreATRequest *req = NULL;
2757         TcorePending *pending = NULL;
2758         char *cmd_str = NULL;
2759         const struct treq_sim_change_pins *req_data;
2760         struct s_sim_property *sp = NULL;
2761         char *pin1 = "SC";
2762         char *pin2 = "P2";
2763         TReturn ret = TCORE_RETURN_FAILURE;
2764
2765         dbg(" Function entry ");
2766
2767         if (!o || !ur)
2768                 return TCORE_RETURN_EINVAL;
2769
2770         hal = tcore_object_get_hal(o);
2771         if(FALSE == tcore_hal_get_power_state(hal)){
2772                 dbg("cp not ready/n");
2773                 return TCORE_RETURN_ENOSYS;
2774         }
2775
2776         sp = tcore_sim_ref_userdata(o);
2777         pending = tcore_pending_new(o, 0);
2778         req_data = tcore_user_request_ref_data(ur, NULL);
2779
2780         if (req_data->type == SIM_PTYPE_PIN1) {
2781                 sp->current_sec_op = SEC_PIN1_CHANGE;
2782                 cmd_str = g_strdup_printf("AT+CPWD=\"%s\",\"%s\",\"%s\"", pin1, req_data->old_pin, req_data->new_pin);
2783         } else if (req_data->type == SIM_PTYPE_PIN2) {
2784                 sp->current_sec_op = SEC_PIN2_CHANGE;
2785                 cmd_str = g_strdup_printf("AT+CPWD=\"%s\",\"%s\",\"%s\"", pin2, req_data->old_pin, req_data->new_pin);
2786         } else {
2787                 return TCORE_RETURN_EINVAL;
2788         }
2789         req = tcore_at_request_new(cmd_str, NULL, TCORE_AT_NO_RESULT);
2790
2791         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
2792
2793         tcore_pending_set_request_data(pending, 0, req);
2794         tcore_pending_set_response_callback(pending, on_response_change_pins, hal);
2795         tcore_pending_link_user_request(pending, ur);
2796         ret = tcore_hal_send_request(hal, pending);
2797
2798         free(cmd_str);
2799         dbg(" Function exit");
2800         return ret;
2801 }
2802
2803 static TReturn s_get_facility_status(CoreObject *o, UserRequest *ur)
2804 {
2805         TcoreHal *hal = NULL;
2806         TcoreATRequest *req = NULL;
2807         TcorePending *pending = NULL;
2808         char *cmd_str = NULL;
2809         const struct treq_sim_get_facility_status *req_data;
2810         struct tresp_sim_get_facility_status *res;
2811         char *fac = "SC";
2812         int mode = 2;       /* 0:unlock, 1:lock, 2:query*/
2813         TReturn ret = TCORE_RETURN_FAILURE;
2814
2815         dbg(" Function entry ");
2816
2817         if (!o || !ur)
2818                 return TCORE_RETURN_EINVAL;
2819
2820         hal = tcore_object_get_hal(o);
2821         if(FALSE == tcore_hal_get_power_state(hal)){
2822                 dbg("cp not ready/n");
2823                 return TCORE_RETURN_ENOSYS;
2824         }
2825
2826         pending = tcore_pending_new(o, 0);
2827         req_data = tcore_user_request_ref_data(ur, NULL);
2828
2829         res = g_try_new0(struct tresp_sim_get_facility_status, 1);
2830         if (!res)
2831                 return TCORE_RETURN_ENOMEM;
2832
2833         res->type = req_data->type;
2834
2835         if (req_data->type == SIM_FACILITY_PS) {
2836                 fac = "PS";                             /*PH-SIM, Lock PHone to SIM/UICC card*/
2837         } else if (req_data->type == SIM_FACILITY_SC) {
2838                 fac = "SC";                             /*Lock SIM/UICC card, simply PIN1*/
2839         } else if (req_data->type == SIM_FACILITY_FD) {
2840                 fac = "FD";                             /*Fixed Dialing Number feature, need PIN2*/
2841         } else if (req_data->type == SIM_FACILITY_PN) {
2842                 fac = "PN";                             /*Network Personalization*/
2843         } else if (req_data->type == SIM_FACILITY_PU) {
2844                 fac = "PU";                             /*network sUbset Personalization*/
2845         } else if (req_data->type == SIM_FACILITY_PP) {
2846                 fac = "PP";                             /*service Provider Personalization*/
2847         } else if (req_data->type == SIM_FACILITY_PC) {
2848                 fac = "PC";                             /*Corporate Personalization*/
2849         } else {
2850                 return TCORE_RETURN_EINVAL;
2851         }
2852         cmd_str = g_strdup_printf("AT+CLCK=\"%s\", %d", fac, mode);
2853         req = tcore_at_request_new(cmd_str, "+CLCK:", TCORE_AT_SINGLELINE);
2854
2855         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
2856
2857         tcore_pending_set_request_data(pending, 0, req);
2858         tcore_pending_set_response_callback(pending, on_response_get_facility_status, res);
2859         tcore_pending_link_user_request(pending, ur);
2860         ret = tcore_hal_send_request(hal, pending);
2861
2862         free(cmd_str);
2863         dbg(" Function exit");
2864         return ret;
2865 }
2866
2867 static TReturn s_enable_facility(CoreObject *o, UserRequest *ur)
2868 {
2869         TcoreHal *hal = NULL;
2870         TcoreATRequest *req = NULL;
2871         TcorePending *pending = NULL;
2872         char *cmd_str = NULL;
2873         const struct treq_sim_enable_facility *req_data;
2874         struct s_sim_property *sp = NULL;
2875         char *fac = "SC";
2876         int mode = 1;       /* 0:unlock, 1:lock, 2:query*/
2877         TReturn ret = TCORE_RETURN_FAILURE;
2878
2879         dbg(" Function entry ");
2880
2881         if (!o || !ur)
2882                 return TCORE_RETURN_EINVAL;
2883
2884         hal = tcore_object_get_hal(o);
2885         if(FALSE == tcore_hal_get_power_state(hal)){
2886                 dbg("cp not ready/n");
2887                 return TCORE_RETURN_ENOSYS;
2888         }
2889
2890         sp = tcore_sim_ref_userdata(o);
2891         pending = tcore_pending_new(o, 0);
2892         req_data = tcore_user_request_ref_data(ur, NULL);
2893
2894         if (req_data->type == SIM_FACILITY_PS) {
2895                 fac = "PS";                             /*PH-SIM, Lock PHone to SIM/UICC card*/
2896                 sp->current_sec_op = SEC_SIM_ENABLE;
2897         } else if (req_data->type == SIM_FACILITY_SC) {
2898                 fac = "SC";                             /*Lock SIM/UICC card, simply PIN1*/
2899                 sp->current_sec_op = SEC_PIN1_ENABLE;
2900         } else if (req_data->type == SIM_FACILITY_FD) {
2901                 fac = "FD";                             /*Fixed Dialing Number feature, need PIN2*/
2902                 sp->current_sec_op = SEC_FDN_ENABLE;
2903         } else if (req_data->type == SIM_FACILITY_PN) {
2904                 fac = "PN";                             /*Network Personalization*/
2905                 sp->current_sec_op = SEC_NET_ENABLE;
2906         } else if (req_data->type == SIM_FACILITY_PU) {
2907                 fac = "PU";                             /*network sUbset Personalization*/
2908                 sp->current_sec_op = SEC_NS_ENABLE;
2909         } else if (req_data->type == SIM_FACILITY_PP) {
2910                 fac = "PP";                             /*service Provider Personalization*/
2911                 sp->current_sec_op = SEC_SP_ENABLE;
2912         } else if (req_data->type == SIM_FACILITY_PC) {
2913                 fac = "PC";                             /*Corporate Personalization*/
2914                 sp->current_sec_op = SEC_CP_ENABLE;
2915         } else {
2916                 return TCORE_RETURN_EINVAL;
2917         }
2918         cmd_str = g_strdup_printf("AT+CLCK=\"%s\", %d, \"%s\"", fac, mode, req_data->password);
2919         req = tcore_at_request_new(cmd_str, "+CLCK:", TCORE_AT_SINGLELINE);
2920
2921         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
2922
2923         tcore_pending_set_request_data(pending, 0, req);
2924         tcore_pending_set_response_callback(pending, on_response_enable_facility, hal);
2925         tcore_pending_link_user_request(pending, ur);
2926         ret = tcore_hal_send_request(hal, pending);
2927
2928         free(cmd_str);
2929         dbg(" Function exit");
2930         return ret;
2931 }
2932
2933 static TReturn s_disable_facility(CoreObject *o, UserRequest *ur)
2934 {
2935         TcoreHal *hal;
2936         TcoreATRequest *req;
2937         TcorePending *pending = NULL;
2938         char *cmd_str = NULL;
2939         const struct treq_sim_enable_facility *req_data;
2940         struct s_sim_property *sp = NULL;
2941         char *fac = "SC";
2942         int mode = 0;       /* 0:unlock, 1:lock, 2:query*/
2943         TReturn ret = TCORE_RETURN_FAILURE;
2944
2945         dbg(" Function entry ");
2946
2947         if (!o || !ur)
2948                 return TCORE_RETURN_EINVAL;
2949
2950         hal = tcore_object_get_hal(o);
2951         if(FALSE == tcore_hal_get_power_state(hal)){
2952                 dbg("cp not ready/n");
2953                 return TCORE_RETURN_ENOSYS;
2954         }
2955
2956         sp = tcore_sim_ref_userdata(o);
2957         pending = tcore_pending_new(o, 0);
2958         req_data = tcore_user_request_ref_data(ur, NULL);
2959
2960         if (req_data->type == SIM_FACILITY_PS) {
2961                 fac = "PS";                             /*PH-SIM, Lock PHone to SIM/UICC card*/
2962                 sp->current_sec_op = SEC_SIM_DISABLE;
2963         } else if (req_data->type == SIM_FACILITY_SC) {
2964                 fac = "SC";                             /*Lock SIM/UICC card, simply PIN1*/
2965                 sp->current_sec_op = SEC_PIN1_DISABLE;
2966         } else if (req_data->type == SIM_FACILITY_FD) {
2967                 fac = "FD";                             /*Fixed Dialing Number feature, need PIN2*/
2968                 sp->current_sec_op = SEC_FDN_DISABLE;
2969         } else if (req_data->type == SIM_FACILITY_PN) {
2970                 fac = "PN";                             /*Network Personalization*/
2971                 sp->current_sec_op = SEC_NET_DISABLE;
2972         } else if (req_data->type == SIM_FACILITY_PU) {
2973                 fac = "PU";                             /*network sUbset Personalization*/
2974                 sp->current_sec_op = SEC_NS_DISABLE;
2975         } else if (req_data->type == SIM_FACILITY_PP) {
2976                 fac = "PP";                             /*service Provider Personalization*/
2977                 sp->current_sec_op = SEC_SP_DISABLE;
2978         } else if (req_data->type == SIM_FACILITY_PC) {
2979                 fac = "PC";                             /*Corporate Personalization*/
2980                 sp->current_sec_op = SEC_CP_DISABLE;
2981         } else {
2982                 return TCORE_RETURN_EINVAL;
2983         }
2984         cmd_str = g_strdup_printf("AT+CLCK=\"%s\", %d, \"%s\"", fac, mode, req_data->password);
2985         req = tcore_at_request_new(cmd_str, "+CLCK:", TCORE_AT_SINGLELINE);
2986
2987         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
2988
2989         tcore_pending_set_request_data(pending, 0, req);
2990         tcore_pending_set_response_callback(pending, on_response_disable_facility, hal);
2991         tcore_pending_link_user_request(pending, ur);
2992         ret = tcore_hal_send_request(hal, pending);
2993
2994         free(cmd_str);
2995         dbg(" Function exit");
2996         return ret;
2997 }
2998
2999 static TReturn s_get_lock_info_n(CoreObject *o, UserRequest *ur)
3000 {
3001         TcoreHal *hal = NULL;
3002         TcoreATRequest *req = NULL;
3003         TcorePending *pending = NULL;
3004         char *cmd_str = NULL;
3005         int lock_type = 0;
3006         const struct treq_sim_get_lock_info *req_data;
3007         struct s_sim_property *sp = NULL;
3008
3009         dbg(" Function entry ");
3010
3011         hal = tcore_object_get_hal(o);
3012
3013         sp = tcore_sim_ref_userdata(o);
3014         pending = tcore_pending_new(o, 0);
3015         req_data = tcore_user_request_ref_data(ur, NULL);
3016
3017         if (!o || !ur)
3018                 return TCORE_RETURN_EINVAL;
3019
3020         switch (req_data->type) {
3021         case SIM_FACILITY_PS:
3022                 lock_type = 9; // IMSI lock
3023                 break;
3024
3025         case SIM_FACILITY_SC:
3026                 lock_type = 1;
3027                 break;
3028
3029         case SIM_FACILITY_FD:
3030                 lock_type = 2;
3031                 break;
3032
3033         case SIM_FACILITY_PN:
3034                 lock_type = 5;
3035                 break;
3036
3037         case SIM_FACILITY_PU:
3038                 lock_type = 6;
3039                 break;
3040
3041         case SIM_FACILITY_PP:
3042                 lock_type = 7;
3043                 break;
3044
3045         case SIM_FACILITY_PC:
3046                 lock_type = 8;
3047                 break;
3048
3049         default:
3050                 break;
3051         }
3052         cmd_str = g_strdup_printf("AT+XPINCNT =%d", lock_type);
3053         req = tcore_at_request_new(cmd_str, "+XPINCNT:", TCORE_AT_SINGLELINE);
3054
3055         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
3056
3057         tcore_pending_set_request_data(pending, 0, req);
3058         tcore_pending_set_response_callback(pending, on_response_get_lock_info, hal);
3059         tcore_pending_link_user_request(pending, ur);
3060         tcore_hal_send_request(hal, pending);
3061
3062         free(cmd_str);
3063         dbg(" Function exit");
3064         return TCORE_RETURN_SUCCESS;
3065 }
3066
3067 static TReturn s_get_lock_info_str(CoreObject *o, UserRequest *ur)
3068 {
3069         TcoreHal *hal = NULL;
3070         TcoreATRequest *req = NULL;
3071         TcorePending *pending = NULL;
3072         char *cmd_str = NULL;
3073         char *lock_type = NULL;
3074         const struct treq_sim_get_lock_info *req_data;
3075         TReturn ret = TCORE_RETURN_FAILURE;
3076
3077         dbg(" Function entry ");
3078
3079         if (!o || !ur)
3080                 return TCORE_RETURN_EINVAL;
3081
3082         hal = tcore_object_get_hal(o);
3083
3084         pending = tcore_pending_new(o, 0);
3085         req_data = tcore_user_request_ref_data(ur, NULL);
3086
3087         switch (req_data->type) {
3088         case SIM_FACILITY_PS:
3089                 lock_type = "PS";
3090                 break;
3091
3092         case SIM_FACILITY_SC:
3093                 lock_type = "SC";
3094                 break;
3095
3096         case SIM_FACILITY_FD:
3097                 lock_type = "FD";
3098                 break;
3099
3100         case SIM_FACILITY_PN:
3101                 lock_type = "PN";
3102                 break;
3103
3104         case SIM_FACILITY_PU:
3105                 lock_type = "PU";
3106                 break;
3107
3108         case SIM_FACILITY_PP:
3109                 lock_type = "PP";
3110                 break;
3111
3112         case SIM_FACILITY_PC:
3113                 lock_type = "PC";
3114                 break;
3115
3116         default:
3117                 break;
3118         }
3119         cmd_str = g_strdup_printf("AT+XPINCNT =\"%s\"", lock_type);
3120         req = tcore_at_request_new(cmd_str, "+XPINCNT:", TCORE_AT_SINGLELINE);
3121
3122         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
3123
3124         tcore_pending_set_request_data(pending, 0, req);
3125         tcore_pending_set_response_callback(pending, on_response_get_lock_info, hal);
3126         tcore_pending_link_user_request(pending, ur);
3127         ret = tcore_hal_send_request(hal, pending);
3128
3129         free(cmd_str);
3130         dbg(" Function exit");
3131         return ret;
3132 }
3133
3134 static TReturn s_get_lock_info(CoreObject *o, UserRequest *ur)
3135 {
3136         TcorePlugin *plugin = tcore_object_ref_plugin(o);
3137         const char *cpname = tcore_server_get_cp_name_by_plugin(plugin);
3138
3139         if (g_str_has_prefix(cpname, "mfld_blackbay") == TRUE)
3140                 return s_get_lock_info_n(o, ur);
3141         else
3142                 return s_get_lock_info_str(o, ur);
3143 }
3144
3145 static TReturn s_read_file(CoreObject *o, UserRequest *ur)
3146 {
3147         TReturn api_ret = TCORE_RETURN_SUCCESS;
3148         enum tcore_request_command command;
3149
3150         dbg(" Function entry ");
3151
3152         if (!o || !ur)
3153                 return TCORE_RETURN_EINVAL;
3154
3155         command = tcore_user_request_get_command(ur);
3156         if(FALSE == tcore_hal_get_power_state(tcore_object_get_hal(o))){
3157                 dbg("cp not ready/n");
3158                 return TCORE_RETURN_ENOSYS;
3159         }
3160
3161         switch (command) {
3162         case TREQ_SIM_GET_ECC:
3163                 api_ret = _get_file_info(o, ur, SIM_EF_ECC);
3164                 break;
3165
3166         case TREQ_SIM_GET_LANGUAGE:
3167                 if (tcore_sim_get_type(o) == SIM_TYPE_GSM)
3168                         api_ret = _get_file_info(o, ur, SIM_EF_ELP);
3169                 else if (tcore_sim_get_type(o) == SIM_TYPE_USIM)
3170                         api_ret = _get_file_info(o, ur, SIM_EF_LP);
3171                 else
3172                         api_ret = TCORE_RETURN_ENOSYS;
3173                 break;
3174
3175         case TREQ_SIM_GET_ICCID:
3176                 api_ret = _get_file_info(o, ur, SIM_EF_ICCID);
3177                 break;
3178
3179         case TREQ_SIM_GET_MAILBOX:
3180                 if (tcore_sim_get_cphs_status(o))
3181                         api_ret = _get_file_info(o, ur, SIM_EF_CPHS_MAILBOX_NUMBERS);
3182                 else
3183                         api_ret = _get_file_info(o, ur, SIM_EF_MBDN);
3184                 break;
3185
3186         case TREQ_SIM_GET_CALLFORWARDING:
3187                 if (tcore_sim_get_cphs_status(o))
3188                         api_ret = _get_file_info(o, ur, SIM_EF_CPHS_CALL_FORWARD_FLAGS);
3189                 else
3190                         api_ret = _get_file_info(o, ur, SIM_EF_USIM_CFIS);
3191                 break;
3192
3193         case TREQ_SIM_GET_MESSAGEWAITING:
3194                 if (tcore_sim_get_cphs_status(o))
3195                         api_ret = _get_file_info(o, ur, SIM_EF_CPHS_VOICE_MSG_WAITING);
3196                 else
3197                         api_ret = _get_file_info(o, ur, SIM_EF_USIM_MWIS);
3198                 break;
3199
3200         case TREQ_SIM_GET_CPHS_INFO:
3201                 api_ret = _get_file_info(o, ur, SIM_EF_CPHS_CPHS_INFO);
3202                 break;
3203
3204         case TREQ_SIM_GET_MSISDN:
3205                 api_ret = _get_file_info(o, ur, SIM_EF_MSISDN);
3206                 break;
3207
3208         case TREQ_SIM_GET_SPN:
3209                 dbg("enter case SPN");
3210                 api_ret = _get_file_info(o, ur, SIM_EF_SPN);
3211                 break;
3212
3213         case TREQ_SIM_GET_SPDI:
3214                 api_ret = _get_file_info(o, ur, SIM_EF_SPDI);
3215                 break;
3216
3217         case TREQ_SIM_GET_OPL:
3218                 api_ret = _get_file_info(o, ur, SIM_EF_OPL);
3219                 break;
3220
3221         case TREQ_SIM_GET_PNN:
3222                 api_ret = _get_file_info(o, ur, SIM_EF_PNN);
3223                 break;
3224
3225         case TREQ_SIM_GET_CPHS_NETNAME:
3226                 api_ret = _get_file_info(o, ur, SIM_EF_CPHS_OPERATOR_NAME_STRING);
3227                 break;
3228
3229         case TREQ_SIM_GET_OPLMNWACT:
3230                 api_ret = _get_file_info(o, ur, SIM_EF_OPLMN_ACT);
3231                 break;
3232
3233         default:
3234                 dbg("error - not handled read treq command[%d]", command);
3235                 api_ret = TCORE_RETURN_EINVAL;
3236                 break;
3237         }
3238         dbg(" Function exit");
3239         return api_ret;
3240 }
3241
3242 static TReturn s_update_file(CoreObject *o, UserRequest *ur)
3243 {
3244         TcoreHal *hal;
3245         TcoreATRequest *req;
3246         TcorePending *pending = NULL;
3247         char *cmd_str = NULL;
3248         TReturn ret = TCORE_RETURN_SUCCESS;
3249         char *encoded_data = NULL;
3250         int encoded_len = 0;
3251         enum tcore_request_command command;
3252         enum tel_sim_file_id ef = SIM_EF_INVALID;
3253         const struct treq_sim_set_callforwarding *cf;
3254         const struct treq_sim_set_language *cl;
3255         struct s_sim_property file_meta = {0, };
3256         int p1 = 0;
3257         int p2 = 0;
3258         int p3 = 0;
3259         int cmd = 0;
3260         int out_length = 0;
3261         int trt = 0;
3262         struct tel_sim_language sim_language;
3263         char *tmp = NULL;
3264         gboolean result;
3265
3266         command = tcore_user_request_get_command(ur);
3267
3268         dbg(" Function entry ");
3269
3270         if (!o || !ur) {
3271                 return TCORE_RETURN_EINVAL;
3272         }
3273
3274         hal = tcore_object_get_hal(o);
3275         if(FALSE == tcore_hal_get_power_state(hal)){
3276                 dbg("cp not ready/n");
3277                 return TCORE_RETURN_ENOSYS;
3278         }
3279
3280         pending = tcore_pending_new(o, 0);
3281
3282         switch (command) {
3283         case TREQ_SIM_SET_LANGUAGE:
3284                 cl = tcore_user_request_ref_data(ur, NULL);
3285                 memset(&sim_language, 0x00, sizeof(struct tel_sim_language));
3286                 cmd = 214;
3287
3288                 sim_language.language_count = 1;
3289                 sim_language.language[0] = cl->language;
3290                 dbg("language %d", cl->language);
3291
3292                 if (tcore_sim_get_type(o) == SIM_TYPE_GSM) {
3293                         dbg("2G");
3294                         ef = SIM_EF_LP;
3295                         tmp = tcore_sim_encode_lp(&out_length, &sim_language);
3296
3297                         encoded_data = (char *) malloc(2 * (sim_language.language_count) + 1);
3298                         memset(encoded_data, 0x00, (2 * sim_language.language_count) + 1);
3299                         result = util_byte_to_hex(tmp, encoded_data, out_length);
3300
3301                         p1 = 0;
3302                         p2 = 0;
3303                         p3 = out_length;
3304                         dbg("encoded_data - %s ---", encoded_data);
3305                         dbg("out_length - %d ---", out_length);
3306                 } else if (tcore_sim_get_type(o) == SIM_TYPE_USIM) {
3307                         dbg("3G");
3308                         ef = SIM_EF_LP;
3309                         tmp = tcore_sim_encode_li(&out_length, &sim_language);
3310
3311                         encoded_data = (char *) malloc(2 * (out_length) + 1);
3312                         memset(encoded_data, 0x00, (2 * out_length) + 1);
3313                         result = util_byte_to_hex(tmp, encoded_data, out_length);
3314
3315                         p1 = 0;
3316                         p2 = 0;
3317                         p3 = out_length;
3318                         dbg("encoded_data - %s ---", encoded_data);
3319                         dbg("out_length - %d ---", out_length);
3320                 } else {
3321                         ret = TCORE_RETURN_ENOSYS;
3322                 }
3323                 break;
3324
3325         case TREQ_SIM_SET_CALLFORWARDING:
3326                 cf = tcore_user_request_ref_data(ur, NULL);
3327                 if (tcore_sim_get_cphs_status(o)) {
3328                         tmp =  tcore_sim_encode_cff((const struct tel_sim_cphs_cf*)&cf->cphs_cf);
3329                         ef = SIM_EF_CPHS_CALL_FORWARD_FLAGS;
3330                         p1 = 0;
3331                         p2 = 0;
3332                         p3 = strlen(tmp);
3333                         encoded_data = (char *) malloc(2 * (p3) + 1);
3334                         memset(encoded_data, 0x00, (2 *p3) + 1);
3335                         result = util_byte_to_hex(tmp, encoded_data, p3);
3336                         cmd = 214;                  /*command - 214 : UPDATE BINARY*/
3337                 } else {
3338                         tmp =  tcore_sim_encode_cfis(&encoded_len, (const struct tel_sim_cfis*)&cf->cf);
3339                         ef = SIM_EF_USIM_CFIS;
3340                         p1 = 1;
3341                         p2 = 0x04;
3342                         p3 = encoded_len;
3343                         encoded_data = (char *) malloc(2 * (encoded_len) + 1);
3344                         memset(encoded_data, 0x00, (2 * encoded_len) + 1);
3345                         result = util_byte_to_hex(tmp, encoded_data, encoded_len);
3346                         cmd = 220;                  /*command - 220 : UPDATE RECORD*/
3347                 }
3348                 break;
3349
3350         default:
3351                 dbg("error - not handled update treq command[%d]", command);
3352                 ret = TCORE_RETURN_EINVAL;
3353                 break;
3354         }
3355         file_meta.file_id = ef;
3356         dbg("file_meta.file_id: %d", file_meta.file_id);
3357
3358         trt = tcore_user_request_set_metainfo(ur, sizeof(struct s_sim_property), &file_meta);
3359         dbg("trt[%d]", trt);
3360
3361         cmd_str = g_strdup_printf("AT+CRSM=%d,%d,%d,%d,%d,\"%s\"", cmd, ef, p1, p2, p3, encoded_data);
3362         req = tcore_at_request_new(cmd_str, "+CRSM:", TCORE_AT_SINGLELINE);
3363
3364         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
3365
3366         tcore_pending_set_request_data(pending, 0, req);
3367         tcore_pending_set_response_callback(pending, on_response_update_file, hal);
3368         tcore_pending_link_user_request(pending, ur);
3369         ret = tcore_hal_send_request(hal, pending);
3370
3371         if (NULL != encoded_data) {
3372                 g_free(encoded_data);
3373         }
3374         free(cmd_str);
3375
3376         if (tmp) {
3377                 free(tmp);
3378         }
3379
3380         dbg(" Function exit");
3381         return ret;
3382 }
3383
3384 static TReturn s_transmit_apdu(CoreObject *o, UserRequest *ur)
3385 {
3386         TcoreHal *hal = NULL;
3387         TcoreATRequest *req = NULL;
3388         TcorePending *pending = NULL;
3389         char *cmd_str = NULL;
3390         char *apdu = NULL;
3391         int apdu_len = 0;
3392         int result = 0;
3393         const struct treq_sim_transmit_apdu *req_data;
3394         TReturn ret = TCORE_RETURN_FAILURE;
3395
3396         dbg(" Function entry ");
3397
3398         if (!o || !ur)
3399                 return TCORE_RETURN_EINVAL;
3400
3401         hal = tcore_object_get_hal(o);
3402         if(FALSE == tcore_hal_get_power_state(hal)){
3403                 dbg("cp not ready/n");
3404                 return TCORE_RETURN_ENOSYS;
3405         }
3406
3407         pending = tcore_pending_new(o, 0);
3408         req_data = tcore_user_request_ref_data(ur, NULL);
3409
3410         apdu = (char *) malloc((2 * req_data->apdu_length) + 1);
3411         memset(apdu, 0x00, (2 * req_data->apdu_length) + 1);
3412         result = util_byte_to_hex((const char *) req_data->apdu, apdu, req_data->apdu_length);
3413         apdu_len = strlen(apdu);
3414         cmd_str = g_strdup_printf("AT+CSIM=%d,\"%s\"", apdu_len, apdu);
3415         req = tcore_at_request_new(cmd_str, "+CSIM:", TCORE_AT_SINGLELINE);
3416         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
3417
3418         tcore_pending_set_request_data(pending, 0, req);
3419         tcore_pending_set_response_callback(pending, on_response_transmit_apdu, hal);
3420         tcore_pending_link_user_request(pending, ur);
3421         ret = tcore_hal_send_request(hal, pending);
3422
3423         free(cmd_str);
3424         free(apdu);
3425         dbg(" Function exit");
3426         return ret;
3427 }
3428
3429 static TReturn s_get_atr(CoreObject *o, UserRequest *ur)
3430 {
3431         TcoreHal *hal = NULL;
3432         TcoreATRequest *req = NULL;
3433         TcorePending *pending = NULL;
3434         char *cmd_str = NULL;
3435         TReturn ret = TCORE_RETURN_FAILURE;
3436
3437         dbg(" Function entry ");
3438
3439         if (!o || !ur)
3440                 return TCORE_RETURN_EINVAL;
3441
3442         hal = tcore_object_get_hal(o);
3443         if(FALSE == tcore_hal_get_power_state(hal)) {
3444                 dbg("cp not ready/n");
3445                 return TCORE_RETURN_ENOSYS;
3446         }
3447         pending = tcore_pending_new(o, 0);
3448
3449         cmd_str = g_strdup_printf("AT+XGATR");
3450         req = tcore_at_request_new(cmd_str, "+XGATR:", TCORE_AT_SINGLELINE);
3451         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
3452
3453         tcore_pending_set_request_data(pending, 0, req);
3454         tcore_pending_set_response_callback(pending, on_response_get_atr, hal);
3455         tcore_pending_link_user_request(pending, ur);
3456         ret = tcore_hal_send_request(hal, pending);
3457
3458         free(cmd_str);
3459         dbg(" Function exit");
3460         return ret;
3461 }
3462
3463 static struct tcore_sim_operations sim_ops = {
3464         .verify_pins = s_verify_pins,
3465         .verify_puks = s_verify_puks,
3466         .change_pins = s_change_pins,
3467         .get_facility_status = s_get_facility_status,
3468         .enable_facility = s_enable_facility,
3469         .disable_facility = s_disable_facility,
3470         .get_lock_info = s_get_lock_info,
3471         .read_file = s_read_file,
3472         .update_file = s_update_file,
3473         .transmit_apdu = s_transmit_apdu,
3474         .get_atr = s_get_atr,
3475         .req_authentication = NULL,
3476 };
3477
3478 gboolean s_sim_init(TcorePlugin *cp, CoreObject *co_sim)
3479 {
3480         struct s_sim_property *file_meta;
3481
3482         dbg("Entry");
3483
3484         tcore_sim_override_ops(co_sim, &sim_ops);
3485
3486         file_meta = g_try_new0(struct s_sim_property, 1);
3487         if (!file_meta)
3488                 return FALSE;
3489
3490         tcore_sim_link_userdata(co_sim, file_meta);
3491
3492         tcore_object_override_callback(co_sim, "+XLOCK", on_event_facility_lock_status, NULL);
3493         tcore_object_override_callback(co_sim, "+XSIM", on_event_pin_status, NULL);
3494
3495         tcore_server_add_notification_hook(tcore_plugin_ref_server(cp), TNOTI_MODEM_POWER, on_hook_modem_power, co_sim);
3496
3497         dbg("Exit");
3498
3499         return TRUE;
3500 }
3501
3502 void s_sim_exit(TcorePlugin *cp, CoreObject *co_sim)
3503 {
3504         struct s_sim_property *file_meta;
3505
3506         file_meta = tcore_sim_ref_userdata(co_sim);
3507         g_free(file_meta);
3508
3509         dbg("Exit");
3510 }