1ca7d4929e37f6d30380e43f2dfc684ce552e5d5
[profile/ivi/tel-plugin-atmodem.git] / src / s_sim.c
1 /*
2  * tel-plugin-samsung-atmodem
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hayoon Ko <hayoon.ko@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
25 #include <glib.h>
26
27
28 #include <tcore.h>
29 #include <hal.h>
30 #include <core_object.h>
31 #include <plugin.h>
32 #include <server.h>
33 #include <queue.h>
34 #include <co_sim.h>
35 #include <storage.h>
36 #include <user_request.h>
37
38 #include "s_common.h"
39 #include "s_sim.h"
40
41 #include "atchannel.h"
42 #include "at_tok.h"
43
44 extern struct ATResponse *sp_response;
45 extern char *s_responsePrefix;
46 extern enum ATCommandType s_type;
47
48 #define SWAPBYTES16(x) \
49 { \
50     unsigned short int data = *(unsigned short int*)&(x); \
51     data = ((data & 0xff00) >> 8) |    \
52            ((data & 0x00ff) << 8);     \
53     *(unsigned short int*)&(x) = data ;      \
54 }
55
56 enum s_sim_file_type_e {
57         SIM_FTYPE_DEDICATED = 0x00, /**< Dedicated */
58         SIM_FTYPE_TRANSPARENT = 0x01, /**< Transparent -binary type*/
59         SIM_FTYPE_LINEAR_FIXED = 0x02, /**< Linear fixed - record type*/
60         SIM_FTYPE_CYCLIC = 0x04, /**< Cyclic - record type*/
61         SIM_FTYPE_INVALID_TYPE = 0xFF /**< Invalid type */
62 };
63
64 enum s_sim_sec_op_e {
65         SEC_PIN1_VERIFY,
66         SEC_PIN2_VERIFY,
67         SEC_PUK1_VERIFY,
68         SEC_PUK2_VERIFY,
69         SEC_SIM_VERIFY,
70         SEC_ADM_VERIFY,
71         SEC_PIN1_CHANGE,
72         SEC_PIN2_CHANGE,
73         SEC_PIN1_ENABLE,
74         SEC_PIN1_DISABLE,
75         SEC_PIN2_ENABLE,
76         SEC_PIN2_DISABLE, //10
77         SEC_SIM_ENABLE,
78         SEC_SIM_DISABLE,
79         SEC_NET_ENABLE,
80         SEC_NET_DISABLE,
81         SEC_NS_ENABLE,
82         SEC_NS_DISABLE,
83         SEC_SP_ENABLE,
84         SEC_SP_DISABLE,
85         SEC_CP_ENABLE,
86         SEC_CP_DISABLE, //20
87         SEC_FDN_ENABLE,
88         SEC_FDN_DISABLE,
89         SEC_PIN1_STATUS,
90         SEC_PIN2_STATUS,
91         SEC_FDN_STATUS,
92         SEC_NET_STATUS,
93         SEC_NS_STATUS,
94         SEC_SP_STATUS,
95         SEC_CP_STATUS,
96         SEC_SIM_STATUS
97 };
98
99 struct s_sim_property {
100         gboolean b_valid; /**< Valid or not */
101         enum tel_sim_file_id file_id; /**< File identifier */
102         enum s_sim_file_type_e file_type; /**< File type and structure */
103         int rec_length; /**< Length of one record in file */
104         int rec_count; /**< Number of records in file */
105         int data_size; /**< File size */
106         int current_index; /**< current index to read */
107         enum tel_sim_status first_recv_status;
108         enum s_sim_sec_op_e current_sec_op; /**< current index to read */
109         struct tresp_sim_read files;
110         struct ATReqMetaInfo metainfo;
111 };
112
113 enum s_sim_sec_locktype_e{
114         SEC_LOCK_TYPE_NONE =0,
115         SEC_LOCK_TYPE_READY,    /*  ME is not locked */
116         SEC_LOCK_TYPE_PS,               /* PH-SIM, Lock Phone to SIM/UICC card(MT asks password when other than current SIM/UICC card inserted; MT may remember certain amount of
117                                                          previously used cards thus not requiring password when they are inserted ) */
118         SEC_LOCK_TYPE_PF,       /*  PH-FSIM, Lock Phone to the very First  inserted SIM/UICC card ( MT asks password when other than the first SIM/UICC card is inserted ) */
119         SEC_LOCK_TYPE_SC,       /*Lock SIM/UICC card ( SIM asks password in ME power-up and when this command is issued ) */
120         SEC_LOCK_TYPE_FD,       /* SIM card or active application in the UICC(GSM or USIM) fixed dialing memory feature */
121         SEC_LOCK_TYPE_PN,               /*  Network Personalization */
122         SEC_LOCK_TYPE_PU,       /*  Network subset Personalization */
123         SEC_LOCK_TYPE_PP,       /*  Service Provider Personalization */
124         SEC_LOCK_TYPE_PC,       /*  Corporate Personalization */
125         SEC_LOCK_TYPE_SC2,      /*  Lock PIN2 ( ... ) */
126         SEC_LOCL_TYPE_PUK2,     /*  Lock PUK2 (... ) */
127         SEC_LOCK_TYPE_ACL,      /* ACL */
128
129         SEC_LOCK_TYPE_NO_SIM,           /* SIM is not inserted */
130         SEC_LOCK_TYPE_UNAVAIL,  /*  SIM is inserted but can not communicate with SIM ( SIM interface error ) */
131         SEC_SIM_INIT_COMPLETED, /*  SIM Initialize Completed */
132         SEC_PB_INIT_COMPLETED,  /*  Phonebook Initialize Completed*/
133         SEC_SIM_INIT_CRASH,             /*  SIM Crash request from SMC lab*/
134
135         SEC_LOCK_TYPE_MAX
136 };
137
138 enum s_sim_sec_lockkey_e{
139         SEC_LOCK_KEY_NONE,
140         SEC_LOCK_KEY_UNLOCKED,          /* Not necessary */
141         SEC_LOCK_KEY_PIN,               /* PIN required as a password */
142         SEC_LOCK_KEY_PUK,               /* 0PUK required as a password */
143         SEC_LOCK_KEY_PIN2,              /* PIN2 required as a password */
144         SEC_LOCK_KEY_PUK2,              /*  PUK2 required as a password */
145         SEC_LOCK_KEY_PERM_BLOCKED,    /* PIN Permanent Blocked */
146         SEC_LOCK_KEY_PIN2_DISABLE,     /* PIN2 Lock Disabled*/
147         SEC_LOCK_KEY_MAX
148 };
149
150
151
152 static void _next_from_get_file_info(CoreObject *o, UserRequest *ur, enum tel_sim_file_id ef, enum tel_sim_access_result rt);
153 static void _next_from_get_file_data(CoreObject *o, UserRequest *ur, enum tel_sim_access_result rt, int decode_ret);
154 static gboolean _get_sim_type(CoreObject *o);
155 static TReturn _get_file_info(CoreObject *o, UserRequest *ur, const enum tel_sim_file_id ef);
156 static gboolean _get_file_data(CoreObject *o, UserRequest *ur, const enum tel_sim_file_id ef, const int offset, const int length);
157 static gboolean _get_file_record(CoreObject *o, UserRequest *ur, const enum tel_sim_file_id ef, const int index, const int length);
158 static void _sim_status_update(CoreObject *o, enum tel_sim_status sim_status);
159
160 static gboolean _convert_SCPIN_noti(char* line, enum s_sim_sec_locktype_e* lock_type, enum s_sim_sec_lockkey_e* lock_key);
161
162 static gboolean _convert_SCPIN_noti(char* line, enum s_sim_sec_locktype_e* lock_type, enum s_sim_sec_lockkey_e* lock_key)
163 {
164         char *type =NULL, *key = NULL;
165         int err;
166         if(line == NULL)
167                 return FALSE;
168
169         dbg("incoming string : %s\n", line);
170
171         //rip off %SCPIN:
172         at_tok_start(&line);
173
174         // 1. find type string
175         err = at_tok_nextstr(&line, &type);
176         if(err<0){
177                 // no type string found.
178                 type = NULL;
179         }
180         if(type !=NULL){
181                 // 2. find key string
182                 err = at_tok_nextstr(&line, &key);
183         }
184         if(err<0){
185                 // no key found
186                 key = NULL;
187         }
188
189         dbg("type : %s, key : %s\n", type, key);
190
191 // 3. convert string into enum
192         if(type!=NULL)
193         {
194                 if(strStartsWith (type, "NO_SIM"))
195                         *lock_type = SEC_LOCK_TYPE_NO_SIM;
196                 else if(strStartsWith (type, "UNAVAIL"))
197                         *lock_type = SEC_LOCK_TYPE_UNAVAIL;
198                 else if(strStartsWith (type, "NO_LOCK"))
199                         *lock_type =  SEC_LOCK_TYPE_READY;
200                 else if(strStartsWith (type, "LOCK_PS"))
201                         *lock_type =  SEC_LOCK_TYPE_PS;
202                 else if(strStartsWith (type, "LOCK_PF"))
203                         *lock_type = SEC_LOCK_TYPE_PF ;
204                 else if(strStartsWith (type, "LOCK_SC"))
205                         *lock_type =  SEC_LOCK_TYPE_SC;
206                 else if(strStartsWith (type, "LOCK_FD"))
207                         *lock_type =  SEC_LOCK_TYPE_FD;
208                 else if(strStartsWith (type, "LOCK_PN"))
209                         *lock_type = SEC_LOCK_TYPE_PN ;
210                 else if(strStartsWith (type, "LOCK_PU"))
211                         *lock_type = SEC_LOCK_TYPE_PU ;
212                 else if(strStartsWith (type, "LOCK_PP"))
213                         *lock_type =  SEC_LOCK_TYPE_PP;
214                 else if(strStartsWith (type, "LOCK_PC"))
215                         *lock_type =  SEC_LOCK_TYPE_PC;
216                 else if(strStartsWith (type, "LOCK_SC2"))
217                         *lock_type = SEC_LOCK_TYPE_SC2 ;
218                 else if(strStartsWith (type, "LOCK_ACL"))
219                         *lock_type = SEC_LOCK_TYPE_ACL;
220                 else if(strStartsWith (type, "LOCK_PUK2"))
221                         *lock_type = SEC_LOCL_TYPE_PUK2;
222                 else if(strStartsWith (type, "INIT_COMP"))
223                         *lock_type = SEC_SIM_INIT_COMPLETED;
224                 else if(strStartsWith (type, "INIT_ERROR"))
225                         *lock_type = SEC_SIM_INIT_CRASH;
226                 else
227                         *lock_type = SEC_LOCK_TYPE_NONE;
228         }
229         else
230                 type = SEC_LOCK_TYPE_NONE;
231
232         if(key!=NULL)
233         {
234                 if(strStartsWith (type, "PIN"))
235                         *lock_key = SEC_LOCK_KEY_PIN;
236                 else if(strStartsWith (type, "PUK"))
237                         *lock_key = SEC_LOCK_KEY_PUK;
238                 else if(strStartsWith (type, "PIN2"))
239                         *lock_key =  SEC_LOCK_KEY_PIN2;
240                 else if(strStartsWith (type, "PUK2"))
241                         *lock_key =  SEC_LOCK_KEY_PUK2;
242                 else if(strStartsWith (type, "BLOCKED"))
243                         *lock_key = SEC_LOCK_KEY_PERM_BLOCKED ;
244                 else if(strStartsWith (type, "UNLOCKED"))
245                         *lock_key = SEC_LOCK_KEY_UNLOCKED ;
246                 else if(strStartsWith (type, "PIN2_DISABLE"))
247                         *lock_key =  SEC_LOCK_KEY_PIN2_DISABLE;
248                 else
249                         *lock_key = SEC_LOCK_KEY_NONE;
250         }
251         else
252                 *lock_key = SEC_LOCK_KEY_NONE;
253
254
255         // 4. apply exceptional case.
256         //if type is READY, key has no meanig
257         if(*lock_type ==  SEC_LOCK_TYPE_READY)
258                 *lock_key = SEC_LOCK_KEY_UNLOCKED;
259
260         // no sim, unvail, init_comp, init_error have no key info
261         if((*lock_type == SEC_LOCK_TYPE_NO_SIM)||(*lock_type == SEC_LOCK_TYPE_UNAVAIL)||
262                         (*lock_type == SEC_SIM_INIT_COMPLETED)||(*lock_type == SEC_SIM_INIT_CRASH))
263                 *lock_key = SEC_LOCK_KEY_NONE;
264
265         dbg("type : %d, key : %d\n", *lock_type, *lock_key);
266
267         return TRUE;
268 }
269
270 static enum tcore_response_command _find_resp_command(UserRequest *ur)
271 {
272         enum tcore_request_command command;
273         command = tcore_user_request_get_command(ur);
274         switch(command){
275                 case TREQ_SIM_VERIFY_PINS:
276                         return TRESP_SIM_VERIFY_PINS;
277                         break;
278                 case TREQ_SIM_VERIFY_PUKS:
279                         return TRESP_SIM_VERIFY_PUKS;
280                         break;
281                 case TREQ_SIM_CHANGE_PINS:
282                         return TRESP_SIM_CHANGE_PINS;
283                         break;
284                 case TREQ_SIM_GET_FACILITY_STATUS:
285                         return TRESP_SIM_GET_FACILITY_STATUS;
286                         break;
287                 case TREQ_SIM_DISABLE_FACILITY:
288                         return TRESP_SIM_DISABLE_FACILITY;
289                         break;
290                 case TREQ_SIM_ENABLE_FACILITY:
291                         return TRESP_SIM_ENABLE_FACILITY;
292                         break;
293                 case TREQ_SIM_TRANSMIT_APDU:
294                         return TRESP_SIM_TRANSMIT_APDU;
295                         break;
296                 case TREQ_SIM_GET_ATR:
297                         return TRESP_SIM_GET_ATR;
298                         break;
299                 case TREQ_SIM_GET_ECC:
300                         return TRESP_SIM_GET_ECC;
301                         break;
302                 case TREQ_SIM_GET_LANGUAGE:
303                         return TRESP_SIM_GET_LANGUAGE;
304                         break;
305                 case TREQ_SIM_SET_LANGUAGE:
306                         return TRESP_SIM_SET_LANGUAGE;
307                         break;
308                 case TREQ_SIM_GET_ICCID:
309                         return TRESP_SIM_GET_ICCID;
310                         break;
311                 case TREQ_SIM_GET_MAILBOX:
312                         return TRESP_SIM_GET_MAILBOX;
313                         break;
314                 case TREQ_SIM_GET_CALLFORWARDING:
315                         return TRESP_SIM_GET_CALLFORWARDING;
316                         break;
317                 case TREQ_SIM_GET_MESSAGEWAITING:
318                         return TRESP_SIM_GET_MESSAGEWAITING;
319                         break;
320                 case TREQ_SIM_GET_CPHS_INFO:
321                         return TRESP_SIM_GET_CPHS_INFO;
322                         break;
323                 case TREQ_SIM_GET_MSISDN:
324                         return TRESP_SIM_GET_MSISDN;
325                         break;
326                 case TREQ_SIM_GET_SPN:
327                         return TRESP_SIM_GET_SPN;
328                         break;
329                 case TREQ_SIM_GET_SPDI:
330                         return TRESP_SIM_GET_SPDI;
331                         break;
332                 case TREQ_SIM_GET_OPL:
333                         return TRESP_SIM_GET_OPL;
334                         break;
335                 case TREQ_SIM_GET_PNN:
336                         return TRESP_SIM_GET_PNN;
337                         break;
338                 case TREQ_SIM_GET_CPHS_NETNAME:
339                         return TRESP_SIM_GET_CPHS_NETNAME;
340                         break;
341                 case TREQ_SIM_GET_OPLMNWACT:
342                         return TRESP_SIM_GET_OPLMNWACT;
343                         break;
344                 case TREQ_SIM_REQ_AUTHENTICATION:
345                         return TRESP_SIM_REQ_AUTHENTICATION;
346                         break;
347                 default:
348                         break;
349         }
350         return TRESP_UNKNOWN;
351 }
352
353 static int _sim_get_current_pin_facility(struct s_sim_property *sp)
354 {
355         int ret_type = 0;
356         dbg("current sp->current_sec_op[%d]", sp->current_sec_op);
357         switch(sp->current_sec_op){
358                 case SEC_PIN1_VERIFY :
359                 case SEC_PIN1_CHANGE :
360                         ret_type = SIM_PTYPE_PIN1;
361                         break;
362                 case SEC_PIN2_VERIFY :
363                 case SEC_PIN2_CHANGE :
364                         ret_type = SIM_PTYPE_PIN2;
365                         break;
366                 case SEC_PUK1_VERIFY :
367                         ret_type = SIM_PTYPE_PUK1;
368                         break;
369                 case SEC_PUK2_VERIFY :
370                         ret_type = SIM_PTYPE_PUK2;
371                         break;
372                 case SEC_SIM_VERIFY :
373                         ret_type = SIM_PTYPE_SIM;
374                         break;
375                 case SEC_ADM_VERIFY :
376                         ret_type = SIM_PTYPE_ADM;
377                         break;
378
379                 case SEC_PIN1_ENABLE :
380                 case SEC_PIN1_DISABLE :
381                 case SEC_PIN1_STATUS :
382                         ret_type = SIM_FACILITY_SC;
383                         break;
384                 case SEC_SIM_ENABLE :
385                 case SEC_SIM_DISABLE :
386                 case SEC_SIM_STATUS :
387                         ret_type = SIM_FACILITY_PS;
388                         break;
389                 case SEC_NET_ENABLE :
390                 case SEC_NET_DISABLE :
391                 case SEC_NET_STATUS :
392                         ret_type = SIM_FACILITY_PN;
393                         break;
394                 case SEC_NS_ENABLE :
395                 case SEC_NS_DISABLE :
396                 case SEC_NS_STATUS :
397                         ret_type = SIM_FACILITY_PU;
398                         break;
399                 case SEC_SP_ENABLE :
400                 case SEC_SP_DISABLE :
401                 case SEC_SP_STATUS :
402                         ret_type = SIM_FACILITY_PP;
403                         break;
404                 case SEC_CP_ENABLE :
405                 case SEC_CP_DISABLE :
406                 case SEC_CP_STATUS :
407                         ret_type = SIM_FACILITY_PC;
408                         break;
409                 case SEC_FDN_ENABLE :
410                 case SEC_FDN_DISABLE :
411                 case SEC_FDN_STATUS :
412                         ret_type = SIM_FACILITY_FD;
413                         break;
414
415                 default:
416                         dbg("not handled current op[%d]",sp->current_sec_op )
417                         break;
418         }
419         return ret_type;
420 }
421
422 static enum tel_sim_access_result _decode_status_word(unsigned short status_word1, unsigned short status_word2)
423 {
424         enum tel_sim_access_result rst = SIM_ACCESS_FAILED;
425
426         if (status_word1 == 0x93 && status_word2 == 0x00) {
427                 rst = SIM_ACCESS_FAILED;
428                 /*Failed SIM request command*/
429                 dbg(" error - SIM application toolkit busy [%x][%x]", status_word1, status_word2);
430         }
431         else if (status_word1 == 0x94 && status_word2 == 0x00) {
432                 rst = SIM_ACCESS_FAILED;
433                 /*Failed SIM request command*/
434                 dbg(" error - No EF Selected [%x][%x]", status_word1, status_word2);
435         }
436         else if (status_word1 == 0x94 && status_word2 == 0x02) {
437                 rst = SIM_ACCESS_FAILED;
438                 /*Failed SIM request command*/
439                 dbg("error - Out of Range - Invalid address or record number[%x][%x]",
440                                 status_word1, status_word2);
441         }
442         else if (status_word1 == 0x94 && status_word2 == 0x04) {
443                 rst = SIM_ACCESS_FILE_NOT_FOUND;
444                 /*Failed SIM request command*/
445                 dbg(" error - File ID not found [%x][%x]", status_word1, status_word2);
446         }
447         else if (status_word1 == 0x94 && status_word2 == 0x08) {
448                 rst = SIM_ACCESS_FAILED; /* MOdem not support */
449                 /*Failed SIM request command*/
450                 dbg(" error - File is inconsistent with command - Modem not support or USE IPC [%x][%x]",
451                                 status_word1, status_word2);
452         }
453         else if (status_word1 == 0x98 && status_word2 == 0x02) {
454                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
455                 /*Failed SIM request command*/
456                 dbg(" error - CHV not initialized [%x][%x]", status_word1, status_word2);
457         }
458         else if (status_word1 == 0x98 && status_word2 == 0x04) {
459                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
460                 /*Failed SIM request command*/
461                 dbg(" error - Access condition not fullfilled [%x][%x]", status_word1, status_word2);
462                 dbg(" error -Unsuccessful CHV verification - at least one attempt left [%x][%x]",
463                                 status_word1, status_word2);
464                 dbg(" error - Unsuccessful Unblock CHV - at least one attempt left [%x][%x]",
465                                 status_word1, status_word2);
466                 dbg(" error - Authentication failure [%x][%x]", status_word1, status_word2);
467         }
468         else if (status_word1 == 0x98 && status_word2 == 0x08) {
469                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
470                 /*Failed SIM request command*/
471                 dbg(" error - Contradiction with CHV status [%x][%x]", status_word1, status_word2);
472         }
473         else if (status_word1 == 0x98 && status_word2 == 0x10) {
474                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
475                 /*Failed SIM request command*/
476                 dbg(" error - Contradiction with invalidation  status [%x][%x]",
477                                 status_word1, status_word2);
478         }
479         else if (status_word1 == 0x98 && status_word2 == 0x40) {
480                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
481                 /*Failed SIM request command*/
482                 dbg(" error -Unsuccessful CHV verification - no attempt left [%x][%x]",
483                                 status_word1, status_word2);
484                 dbg(" error - Unsuccessful Unblock CHV - no attempt left [%x][%x]",
485                                 status_word1, status_word2);
486                 dbg(" error - CHV blocked [%x][%x]", status_word1, status_word2);
487         }
488         else if (status_word1 == 0x67 && status_word2 == 0x00) {
489                 rst = SIM_ACCESS_FAILED;
490                 dbg(" error -Incorrect Parameter 3 [%x][%x]", status_word1, status_word2);
491         }
492         else if (status_word1 == 0x6B && status_word2 == 0x00) {
493                 rst = SIM_ACCESS_FAILED;
494                 dbg(" error -Incorrect Parameter 1 or 2 [%x][%x]", status_word1, status_word2);
495         }
496         else if (status_word1 == 0x6D && status_word2 == 0x00) {
497                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
498                 dbg(" error -Unknown instruction given as command [%x][%x]", status_word1, status_word2);
499         }
500         else if (status_word1 == 0x6E && status_word2 == 0x00) {
501                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
502                 dbg(" error -Unknown instruction given as command [%x][%x]", status_word1, status_word2);
503         }
504         else if (status_word1 == 0x69 && status_word2 == 0x82) {
505                 rst = SIM_ACCESS_CONDITION_NOT_SATISFIED;
506                 dbg(" error -Access denied [%x][%x]", status_word1, status_word2);
507         }
508         else if (status_word1 == 0x6A && status_word2 == 0x87) {
509                 rst = SIM_ACCESS_FAILED;
510                 dbg(" error -Incorrect parameters [%x][%x]", status_word1, status_word2);
511         }
512         else if (status_word1 == 0x6A && status_word2 == 0x82) {
513                 rst = SIM_ACCESS_FAILED; // not sure of the SW1 and SW2 meaning here
514                 dbg(" error -File Not found [%x][%x]", status_word1, status_word2);
515         }
516         else if (status_word1 == 0x6A && status_word2 == 0x83) {
517                 rst = SIM_ACCESS_FAILED; // not sure of the SW1 and SW2 meaning here
518                 dbg(" error -Record Not found [%x][%x]", status_word1, status_word2);
519         }
520         else {
521                 rst = SIM_ACCESS_CARD_ERROR;
522                 dbg(" error -Unknown state [%x][%x]", status_word1, status_word2);
523         }
524         return rst;
525 }
526
527 static gboolean _sim_check_identity(CoreObject *o, struct tel_sim_imsi *imsi)
528 {
529         Server *s = NULL;
530         Storage *strg = NULL;
531         char* old_imsi = NULL;
532         char new_imsi[15+1] = {0,};
533
534         s = tcore_plugin_ref_server(tcore_object_ref_plugin(o));
535         if(!s){
536                 dbg("there is no valid server at this point");
537                 return FALSE;
538         }
539
540         strg = (Storage*)tcore_server_find_storage(s, "vconf");
541         if(!strg){
542                 dbg("there is no valid storage plugin");
543                 return FALSE;
544         }
545
546         memcpy(&new_imsi, imsi->plmn, strlen(imsi->plmn));
547         memcpy(&new_imsi[strlen(imsi->plmn)], imsi->msin, strlen(imsi->msin));
548         new_imsi[strlen(imsi->plmn)+strlen(imsi->msin)] = '\0';
549
550         old_imsi = tcore_storage_get_string(strg, STORAGE_KEY_TELEPHONY_IMSI);
551         dbg("old_imsi[%s],newImsi[%s]", old_imsi, new_imsi);
552
553         if (old_imsi != NULL) {
554                 if (strncmp(old_imsi, new_imsi, 15) != 0) {
555                         dbg("NEW SIM");
556                         if (tcore_storage_set_string(strg, STORAGE_KEY_TELEPHONY_IMSI, (const char*) &new_imsi) == FALSE )
557                                 dbg("[FAIL] UPDATE STORAGE_KEY_TELEPHONY_IMSI");
558                         tcore_sim_set_identification(o, TRUE);
559                 }
560                 else {
561                         dbg("SAME SIM");
562                         tcore_sim_set_identification(o, FALSE);
563                 }
564         }
565         else {
566                 dbg("OLD SIM VALUE IS NULL. NEW SIM");
567                 if (tcore_storage_set_string(strg, STORAGE_KEY_TELEPHONY_IMSI, (const char*) &new_imsi) == FALSE)
568                         dbg("[FAIL] UPDATE STORAGE_KEY_TELEPHONY_IMSI");
569                 tcore_sim_set_identification(o, TRUE);
570         }
571         return 1;
572 }
573
574 static void  _next_from_get_file_info(CoreObject *o,  UserRequest *ur, enum tel_sim_file_id ef, enum tel_sim_access_result rt )
575 {
576         struct tresp_sim_read resp = {0,};
577         struct s_sim_property *file_meta = NULL;
578
579         dbg("EF[0x%x] access Result[%d]", ef, rt);
580
581         resp.result = rt;
582         memset(&resp.data, 0x00, sizeof(resp.data));
583
584         if ((ef != SIM_EF_ELP || ef != SIM_EF_LP || ef != SIM_EF_USIM_PL)
585                         && (rt != SIM_ACCESS_SUCCESS)) {
586                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_read),
587                                 &resp);
588                 return;
589         }
590
591         file_meta = (struct s_sim_property*)tcore_user_request_ref_metainfo(ur, NULL);
592
593         switch (ef) {
594                 case SIM_EF_ELP:
595                         if (rt == SIM_ACCESS_SUCCESS) {
596                                 dbg("[SIM DATA] exist EFELP/PL(0x2F05)");
597                                 /*                              if (po->language_file == 0x00)
598                                  po->language_file = SIM_EF_ELP;*/
599                                 _get_file_data(o, ur, ef, 0, file_meta->data_size);
600                         }
601                         else {
602                                 if (tcore_sim_get_type(o) == SIM_TYPE_GSM) {
603                                         dbg(" [SIM DATA]SIM_EF_ELP(2F05) access fail. Request SIM_EF_LP(0x6F05) info");
604                                         /* The ME requests the Language Preference (EFLP) if EFELP is not available  */
605                                         _get_file_info(o, ur, SIM_EF_LP);
606                                 }
607                                 else if (tcore_sim_get_type(o) == SIM_TYPE_USIM) {
608                                         dbg(
609                                                         " [SIM DATA]fail to get Language information in USIM(EF-LI(6F05),EF-PL(2F05)). Request SIM_EF_ECC(0x6FB7) info");
610                                         /* EFELPand EFLI not present at this point. */
611                                         /*                                      po->language.lang_cnt = 0;*/
612                                         tcore_user_request_send_response(ur, _find_resp_command(ur),
613                                                         sizeof(struct tresp_sim_read), &resp);
614                                         return;
615                                 }
616                         }
617                         break;
618
619                 case SIM_EF_LP: //same with SIM_EF_USIM_LI
620                         if (rt == SIM_ACCESS_SUCCESS) {
621                                 dbg("[SIM DATA] exist EFLP/LI(0x6F05)");
622                                 /*                              if (po->language_file == 0x00)
623                                  po->language_file = SIM_EF_LP;*/
624                                 _get_file_data(o, ur, ef, 0, file_meta->data_size);
625                         }
626                         else {
627                                 dbg("[SIM DATA]SIM_EF_LP/LI(6F05) access fail. Current CardType[%d]",
628                                                 tcore_sim_get_type(o));
629                                 if (tcore_sim_get_type(o) == SIM_TYPE_GSM) {
630                                         /* EFELPand EFLP not present at this point.*/
631                                         /*                                      po->language.lang_cnt = 0;*/
632                                         tcore_user_request_send_response(ur, _find_resp_command(ur),
633                                                         sizeof(struct tresp_sim_read), &resp);
634                                         return;
635                                 }
636                                 /*  if EFLI is not present, then the language selection shall be as defined in EFPL at the MF level     */
637                                 else if (tcore_sim_get_type(o) == SIM_TYPE_USIM) {
638                                         dbg("[SIM DATA] try USIM EFPL(0x2F05)");
639                                         _get_file_info(o, ur, SIM_EF_ELP);
640                                 }
641                         }
642                         break;
643
644                 case SIM_EF_USIM_PL:
645                         if (rt == SIM_ACCESS_SUCCESS) {
646                                 dbg("[SIM DATA] exist EFELP/PL(0x2F05)");
647                                 /*                              if (po->language_file == 0x00)
648                                  po->language_file = SIM_EF_ELP;*/
649                                 _get_file_data(o, ur, SIM_EF_ELP, 0, file_meta->data_size);
650                         }
651                         else {
652                                 /* EFELIand EFPL not present, so set language count as zero and select ECC */
653                                 dbg(
654                                                 " [SIM DATA]SIM_EF_USIM_PL(2A05) access fail. Request SIM_EF_ECC(0x6FB7) info");
655                                 /*                              po->language.lang_cnt = 0;*/
656                                 tcore_user_request_send_response(ur, _find_resp_command(ur),
657                                                 sizeof(struct tresp_sim_read), &resp);
658                                 return;
659                         }
660                         break;
661
662                 case SIM_EF_ECC:
663                         if (tcore_sim_get_type(o) == SIM_TYPE_GSM) {
664                                 _get_file_data(o, ur, ef, 0, file_meta->data_size);
665                         }
666                         else if (tcore_sim_get_type(o) == SIM_TYPE_USIM) {
667                                 if (file_meta->rec_count > SIM_ECC_RECORD_CNT_MAX)
668                                         file_meta->rec_count = SIM_ECC_RECORD_CNT_MAX;
669
670                                 file_meta->current_index++;
671                                 _get_file_record(o, ur, ef, file_meta->current_index, file_meta->rec_length);
672                         }
673                         break;
674
675                 case SIM_EF_ICCID:
676                 case SIM_EF_IMSI:
677                 case SIM_EF_SST:
678                 case SIM_EF_SPN:
679                 case SIM_EF_SPDI:
680                 case SIM_EF_CPHS_CPHS_INFO:
681                 case SIM_EF_CPHS_CALL_FORWARD_FLAGS:
682                 case SIM_EF_CPHS_VOICE_MSG_WAITING:
683                 case SIM_EF_CPHS_OPERATOR_NAME_STRING:
684                 case SIM_EF_CPHS_OPERATOR_NAME_SHORT_FORM_STRING:
685                 case SIM_EF_CPHS_DYNAMICFLAGS:
686                 case SIM_EF_CPHS_DYNAMIC2FLAG:
687                 case SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE:
688                 case SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE_LINE2:
689                         _get_file_data(o, ur, ef, 0, file_meta->data_size);
690                         break;
691
692                 case SIM_EF_USIM_CFIS:
693                         if (file_meta->rec_count > SIM_CF_RECORD_CNT_MAX)
694                                 file_meta->rec_count = SIM_CF_RECORD_CNT_MAX;
695                         file_meta->current_index++;
696                         _get_file_record(o, ur, ef, file_meta->current_index, file_meta->rec_length);
697                         break;
698
699                 case SIM_EF_OPL:
700                 case SIM_EF_PNN:
701                 case SIM_EF_USIM_MWIS:
702                 case SIM_EF_USIM_MBI:
703                 case SIM_EF_MBDN:
704                 case SIM_EF_CPHS_MAILBOX_NUMBERS:
705                 case SIM_EF_CPHS_INFORMATION_NUMBERS:
706                         file_meta->current_index++;
707                         _get_file_record(o, ur, ef, file_meta->current_index, file_meta->rec_length);
708                         break;
709
710                 default:
711                         dbg( "error - File id for get file info [0x%x]", ef);
712                         break;
713         }
714         return;
715 }
716
717 static void _next_from_get_file_data(CoreObject *o, UserRequest *ur, enum tel_sim_access_result rt, int decode_ret)
718 {
719         struct s_sim_property *file_meta = NULL;
720         file_meta = (struct s_sim_property*)tcore_user_request_ref_metainfo(ur, NULL);
721
722         dbg("[SIM]EF[0x%x] read rt[%d] Decode rt[%d]", file_meta->file_id, rt, decode_ret);
723
724         switch (file_meta->file_id) {
725                 case SIM_EF_ELP:
726                 case SIM_EF_USIM_PL:
727                 case SIM_EF_LP:
728                 case SIM_EF_USIM_LI:
729                         if (decode_ret == TRUE) {
730                                 if (file_meta->file_id == SIM_EF_LP || file_meta->file_id == SIM_EF_USIM_LI) {
731 /*                                      po->language_file = SIM_EF_LP;*/
732                                 } else if (file_meta->file_id == SIM_EF_ELP || file_meta->file_id == SIM_EF_USIM_PL) {
733 /*                                      po->language_file = SIM_EF_ELP;*/
734                                 }
735                                 tcore_user_request_send_response(ur, _find_resp_command(ur),    sizeof(struct tresp_sim_read), &file_meta->files);
736                         } else {
737                                 /* 2G */
738                                 /*  The ME requests the Extended Language Preference. The ME only requests the Language Preference (EFLP) if at least one of the following conditions holds:
739                                  -      EFELP is not available;
740                                  -      EFELP does not contain an entry corresponding to a language specified in ISO 639[30];
741                                  -      the ME does not support any of the languages in EFELP.
742                                  */
743                                 /* 3G */
744                                 /*  The ME only requests the Language Preference (EFPL) if at least one of the following conditions holds:
745                                  -      if the EFLI has the value 'FFFF' in its highest priority position
746                                  -      if the ME does not support any of the language codes indicated in EFLI , or if EFLI is not present
747                                  */
748                                 if (tcore_sim_get_type(o) == SIM_TYPE_GSM) {
749                                         if (file_meta->file_id == SIM_EF_LP)
750                                                 tcore_user_request_send_response(ur, _find_resp_command(ur),    sizeof(struct tresp_sim_read), &file_meta->files);
751                                         else
752                                                 _get_file_info(o, ur, SIM_EF_LP);
753                                 } else if (tcore_sim_get_type(o) == SIM_TYPE_USIM) {
754                                         if (file_meta->file_id == SIM_EF_LP || file_meta->file_id == SIM_EF_USIM_LI)
755                                                 _get_file_info(o, ur, SIM_EF_ELP);
756                                         else
757                                                 tcore_user_request_send_response(ur, _find_resp_command(ur),    sizeof(struct tresp_sim_read), &file_meta->files);
758                                 }
759                         }
760                         break;
761
762                 case SIM_EF_ECC:
763                         if (tcore_sim_get_type(o) == SIM_TYPE_USIM) {
764                                 file_meta->files.data.ecc.ecc_count++;
765                                 if (file_meta->current_index == file_meta->rec_count) {
766                                         tcore_user_request_send_response(ur, _find_resp_command(ur),    sizeof(struct tresp_sim_read), &file_meta->files);
767                                 } else {
768                                         file_meta->current_index++;
769                                         _get_file_record(o, ur, file_meta->file_id, file_meta->current_index, file_meta->rec_length );
770                                 }
771                         } else if (tcore_sim_get_type(o) == SIM_TYPE_GSM) {
772                                 tcore_user_request_send_response(ur, _find_resp_command(ur),    sizeof(struct tresp_sim_read), &file_meta->files);
773                         } else {
774                                 dbg("[SIM DATA]Invalid CardType[%d] Unable to handle", tcore_sim_get_type(o));
775                         }
776                         break;
777
778                 case SIM_EF_IMSI:
779                         _sim_status_update(o, SIM_STATUS_INIT_COMPLETED);
780                         break;
781
782                 case SIM_EF_OPL:
783                         file_meta->files.data.opl.opl_count++;
784                         dbg("file_meta->files.data.opl.opl_count[%d], current index[%d], rec_cnt[%d]",
785                                         file_meta->files.data.opl.opl_count, file_meta->current_index,file_meta->rec_count);
786                         if (file_meta->current_index == file_meta->rec_count) {
787                                 tcore_user_request_send_response(ur, _find_resp_command(ur),    sizeof(struct tresp_sim_read), &file_meta->files);
788                         } else {
789                                 file_meta->current_index++;
790                                 _get_file_record(o, ur, file_meta->file_id, file_meta->current_index, file_meta->rec_length );
791                         }
792                         break;
793                 case SIM_EF_PNN:
794                         file_meta->files.data.pnn.pnn_count++;
795                         if (file_meta->current_index == file_meta->rec_count) {
796                                 tcore_user_request_send_response(ur, _find_resp_command(ur),    sizeof(struct tresp_sim_read), &file_meta->files);
797                         } else {
798                                 file_meta->current_index++;
799                                 _get_file_record(o, ur, file_meta->file_id, file_meta->current_index, file_meta->rec_length );
800                         }
801                         break;
802                 case SIM_EF_USIM_CFIS:
803                 case SIM_EF_USIM_MWIS:
804                 case SIM_EF_USIM_MBI:
805                 case SIM_EF_MBDN:
806                 case SIM_EF_CPHS_MAILBOX_NUMBERS:
807                 case SIM_EF_CPHS_INFORMATION_NUMBERS:
808                         if (file_meta->current_index == file_meta->rec_count) {
809                                 tcore_user_request_send_response(ur, _find_resp_command(ur),    sizeof(struct tresp_sim_read), &file_meta->files);
810                         } else {
811                                 file_meta->current_index++;
812                                 _get_file_record(o, ur, file_meta->file_id, file_meta->current_index, file_meta->rec_length );
813                         }
814                         break;
815
816                 case SIM_EF_ICCID:
817                 case SIM_EF_SST:
818                 case SIM_EF_SPN:
819                 case SIM_EF_SPDI:
820                 case SIM_EF_CPHS_CPHS_INFO:
821                 case SIM_EF_CPHS_CALL_FORWARD_FLAGS:
822                 case SIM_EF_CPHS_VOICE_MSG_WAITING:
823                 case SIM_EF_CPHS_OPERATOR_NAME_STRING:
824                 case SIM_EF_CPHS_OPERATOR_NAME_SHORT_FORM_STRING:
825                 case SIM_EF_CPHS_DYNAMICFLAGS:
826                 case SIM_EF_CPHS_DYNAMIC2FLAG:
827                 case SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE:
828                 case SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE_LINE2:
829                         tcore_user_request_send_response(ur, _find_resp_command(ur),    sizeof(struct tresp_sim_read), &file_meta->files);
830                         break;
831
832                 default:
833                         dbg("File id not handled [0x%x]", file_meta->file_id);
834                         break;
835         }
836 }
837
838 static void _sim_status_update(CoreObject *o, enum tel_sim_status sim_status)
839 {
840         struct tnoti_sim_status noti_data = {0,};
841
842         dbg("tcore_sim_set_status and send noti w/ [%d]", sim_status);
843         tcore_sim_set_status(o, sim_status);
844         noti_data.sim_status = sim_status;
845         tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_SIM_STATUS,
846                         sizeof(struct tnoti_sim_status), &noti_data);
847 }
848
849 static void on_confirmation_sim_message_send( TcorePending *p, gboolean result, void *user_data )
850 {
851         UserRequest* ur = NULL;
852         struct ATReqMetaInfo* metainfo = NULL;
853         unsigned int info_len =0;
854         struct s_sim_property *file_meta = NULL;
855         dbg("on_confirmation_sim_message_send - msg out from queue. alloc ATRsp buffer & write rspPrefix if needed\n");
856
857 //alloc new sp_response
858         ReleaseResponse(); //release leftover
859 //alloc new sp_response
860
861         sp_response = at_response_new();
862
863         ur = tcore_pending_ref_user_request(p);
864
865         dbg("********************************tcore_user_request_get_command[0x%x]", tcore_user_request_get_command(ur));
866
867         if(tcore_user_request_get_command(ur) == TREQ_CUSTOM)
868         {
869                 file_meta = (struct s_sim_property *)tcore_user_request_ref_metainfo(ur,&info_len);
870                 metainfo = &(file_meta->metainfo);
871
872                 dbg("file_meta->type[%d]", file_meta->metainfo.type);
873                 dbg("metainfo->type[%d]", metainfo->type);
874         }
875         else
876         {
877                 metainfo = (struct ATReqMetaInfo*)tcore_user_request_ref_metainfo(ur,&info_len);
878         }
879
880         if((metainfo->type == SINGLELINE)||
881                 (metainfo->type == MULTILINE))
882         {
883                 //cp rsp prefix
884                 s_responsePrefix = strdup(metainfo->responsePrefix);
885                 dbg("duplicating responsePrefix : %s\n", s_responsePrefix);
886         }
887         else
888         {
889                 s_responsePrefix = NULL;
890         }
891
892         //set atcmd type into s_type
893         s_type = metainfo->type;
894
895         if (result == FALSE) {
896                 /* Fail */
897                 dbg("SEND FAIL");
898         }
899         else {
900                 dbg("SEND OK");
901         }
902 }
903
904 static void _response_get_sim_type(TcorePending *p, int data_len, const void *data, void *user_data)
905 {
906         struct s_sim_property *sp = NULL;
907         CoreObject *co_sim = NULL;
908         enum tel_sim_type sim_type = SIM_TYPE_UNKNOWN;
909         char* line=NULL;
910         int ret = 0;
911
912         if(sp_response->success > 0)
913         {
914                 line = sp_response->p_intermediates->line;
915
916                 ret = at_tok_start(&line);
917                 if (ret < 0)
918                         AT_TOK_ERROR(line);
919
920                 ret = at_tok_nextint(&line,(int *)&sim_type);
921                 if (ret < 0)
922                         AT_TOK_ERROR(line);
923         }
924         else
925         {
926                 sim_type = SIM_TYPE_UNKNOWN;
927         }
928
929         dbg("resp sim type[%d]", sim_type);
930
931         ReleaseResponse();
932
933         co_sim = tcore_pending_ref_core_object(p);
934         tcore_sim_set_type(co_sim, sim_type);
935         sp = tcore_sim_ref_userdata(co_sim);
936         _sim_status_update(co_sim, sp->first_recv_status);
937 }
938
939 static void _response_get_file_info(TcorePending *p, int data_len, const void *data, void *user_data)
940 {
941         CoreObject *co_sim = NULL;
942         UserRequest *ur = NULL;
943         struct s_sim_property *file_meta = NULL;
944         enum tel_sim_access_result rt;
945
946         char *line = NULL;
947         int ret = 0;
948         int sw1 = 0;
949         int sw2 = 0;
950         //char *hexData;
951         //char *recordData;
952
953         co_sim = tcore_pending_ref_core_object(p);
954         if(!co_sim){
955                 dbg("error -  core object is null");
956                 return;
957         }
958         ur = tcore_pending_ref_user_request(p);
959         file_meta = (struct s_sim_property*)tcore_user_request_ref_metainfo(ur, NULL);
960
961         if(sp_response->success > 0)
962         {
963                 line = sp_response->p_intermediates->line;
964                 ret = at_tok_start(&line);
965                 if (ret < 0)
966                         AT_TOK_ERROR(line);
967
968                 ret = at_tok_nextint(&line,&sw1);
969                 if (ret < 0)
970                         AT_TOK_ERROR(line);
971                 ret = at_tok_nextint(&line,&sw2);
972                 if (ret < 0)
973                         AT_TOK_ERROR(line);
974
975                 /*1. SIM access success case*/
976                 if ((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
977                         unsigned char tag_len = 0; /*   1 or 2 bytes ??? */
978                         unsigned short record_len = 0;
979                         char num_of_records = 0;
980                         unsigned char file_id_len = 0;
981                         unsigned short file_id = 0;
982                         unsigned short file_size = 0;
983                         unsigned short file_type = 0;
984                         unsigned short arr_file_id = 0;
985                         int arr_file_id_rec_num = 0;
986
987                         /*      handling only last 3 bits */
988                         unsigned char file_type_tag = 0x07;
989                         unsigned char *ptr_data;
990
991                         char *hexData;
992                         char *recordData;
993                         ret = at_tok_nextstr(&line,&hexData);
994                         if (ret < 0)
995                                 AT_TOK_ERROR(line);
996
997                         recordData = util_hexStringToBytes(hexData);
998                         util_hex_dump("    ", strlen(hexData)/2, recordData);
999
1000                         ptr_data = (unsigned char *)recordData;
1001                         if (tcore_sim_get_type(co_sim) == SIM_TYPE_USIM) {
1002                                 /*
1003                                  ETSI TS 102 221 v7.9.0
1004                                  - Response Data
1005                                  '62'   FCP template tag
1006                                  - Response for an EF
1007                                  '82'   M       File Descriptor
1008                                  '83'   M       File Identifier
1009                                  'A5'   O       Proprietary information
1010                                  '8A'   M       Life Cycle Status Integer
1011                                  '8B', '8C' or 'AB'     C1      Security attributes
1012                                  '80'   M       File size
1013                                  '81'   O       Total file size
1014                                  '88'   O       Short File Identifier (SFI)
1015                                  */
1016
1017                                 /* rsim.res_len  has complete data length received  */
1018
1019                                 /* FCP template tag - File Control Parameters tag*/
1020                                 if (*ptr_data == 0x62) {
1021                                         /* parse complete FCP tag*/
1022                                         /* increment to next byte */
1023                                         ptr_data++;
1024                                         tag_len = *ptr_data++;
1025                                         /* FCP file descriptor - file type, accessibility, DF, ADF etc*/
1026                                         if (*ptr_data == 0x82) {
1027                                                 /* increment to next byte */
1028                                                 ptr_data++;
1029                                                 /*2 or 5 value*/
1030                                                 ptr_data++;
1031                                                 /*      unsigned char file_desc_len = *ptr_data++;*/
1032                                                 /*      dbg("file descriptor length: [%d]", file_desc_len);*/
1033                                                 /* TBD:  currently capture only file type : ignore sharable, non sharable, working, internal etc*/
1034                                                 /* consider only last 3 bits*/
1035                                                 file_type_tag = file_type_tag & (*ptr_data);
1036
1037                                                 switch (file_type_tag) {
1038                                                         /* increment to next byte */
1039                                                         ptr_data++;
1040                                                         case 0x1:
1041                                                                 dbg("Getting FileType: [Transparent file type]");
1042                                                                 /* increment to next byte */
1043                                                                 ptr_data++;
1044                                                                 file_type = SIM_FTYPE_TRANSPARENT;
1045                                                                 /*      data coding byte - value 21 */
1046                                                                 ptr_data++;
1047                                                                 break;
1048
1049                                                         case 0x2:
1050                                                                 dbg("Getting FileType: [Linear fixed file type]");
1051                                                                 /* increment to next byte */
1052                                                                 ptr_data++;
1053                                                                 /*      data coding byte - value 21 */
1054                                                                 ptr_data++;
1055                                                                 /*      2bytes */
1056                                                                 memcpy(&record_len, ptr_data, 2);
1057                                                                 /* swap bytes */
1058                                                                 SWAPBYTES16(record_len);
1059                                                                 ptr_data = ptr_data + 2;
1060                                                                 num_of_records = *ptr_data++;
1061                                                                 /* Data lossy conversation from enum (int) to unsigned char */
1062                                                                 file_type = SIM_FTYPE_LINEAR_FIXED;
1063                                                                 break;
1064
1065                                                         case 0x6:
1066                                                                 dbg(" Cyclic fixed file type");
1067                                                                 /* increment to next byte */
1068                                                                 ptr_data++;
1069                                                                 /*      data coding byte - value 21 */
1070                                                                 ptr_data++;
1071                                                                 /*      2bytes */
1072                                                                 memcpy(&record_len, ptr_data, 2);
1073                                                                 /* swap bytes  */
1074                                                                 SWAPBYTES16(record_len);
1075                                                                 ptr_data = ptr_data + 2;
1076                                                                 num_of_records = *ptr_data++;
1077                                                                 file_type = SIM_FTYPE_CYCLIC;
1078                                                                 break;
1079
1080                                                 default:
1081                                                         dbg("not handled file type [0x%x]", *ptr_data);
1082                                                         break;
1083                                                 }
1084                                         } else {
1085                                                 dbg("INVALID FCP received - DEbug!");
1086                                                 return;
1087                                         }
1088
1089                                         /*File identifier - file id?? */ // 0x84,0x85,0x86 etc are currently ignored and not handled
1090                                         if (*ptr_data == 0x83) {
1091                                                 /* increment to next byte */
1092                                                 ptr_data++;
1093                                                 file_id_len = *ptr_data++;
1094                                                 memcpy(&file_id, ptr_data, file_id_len);
1095                                                 /* swap bytes    */
1096                                                 SWAPBYTES16(file_id);
1097                                                 ptr_data = ptr_data + 2;
1098                                                 dbg("Getting FileID=[0x%x]", file_id);
1099                                         } else {
1100                                                 dbg("INVALID FCP received - DEbug!");
1101                                                 free(recordData);
1102                                                 ReleaseResponse();
1103                                                 return;
1104                                         }
1105
1106                                         /*      proprietary information  */
1107                                         if (*ptr_data == 0xA5) {
1108                                                 unsigned short prop_len;
1109                                                 /* increment to next byte */
1110                                                 ptr_data++;
1111                                                 /* length */
1112                                                 prop_len = *ptr_data;
1113                                                 /* skip data */
1114                                                 ptr_data = ptr_data + prop_len + 1;
1115                                         } else {
1116                                                 dbg("INVALID FCP received - DEbug!");
1117                                         }
1118
1119                                         /* life cycle status integer [8A][length:0x01][status]*/
1120                                         /*
1121                                          status info b8~b1
1122                                          00000000 : No information given
1123                                          00000001 : creation state
1124                                          00000011 : initialization state
1125                                          000001-1 : operation state -activated
1126                                          000001-0 : operation state -deactivated
1127                                          000011-- : Termination state
1128                                          b8~b5 !=0, b4~b1=X : Proprietary
1129                                          Any other value : RFU
1130                                          */
1131                                         if (*ptr_data == 0x8A) {
1132                                                 /* increment to next byte */
1133                                                 ptr_data++;
1134                                                 /* length - value 1 */
1135                                                 ptr_data++;
1136
1137                                                 switch (*ptr_data) {
1138                                                         case 0x04:
1139                                                         case 0x06:
1140                                                                 dbg("<IPC_RX> operation state -deactivated");
1141                                                                 ptr_data++;
1142                                                                 break;
1143                                                         case 0x05:
1144                                                         case 0x07:
1145                                                                 dbg("<IPC_RX> operation state -activated");
1146                                                                 ptr_data++;
1147                                                                 break;
1148                                                         default:
1149                                                                 dbg("<IPC_RX> DEBUG! LIFE CYCLE STATUS =[0x%x]",*ptr_data);
1150                                                                 ptr_data++;
1151                                                                 break;
1152                                                 }
1153                                         }
1154
1155                                         /* related to security attributes : currently not handled*/
1156                                         if (*ptr_data == 0x86 || *ptr_data == 0x8B || *ptr_data == 0x8C || *ptr_data == 0xAB) {
1157                                                 /* increment to next byte */
1158                                                 ptr_data++;
1159                                                 /* if tag length is 3 */
1160                                                 if (*ptr_data == 0x03) {
1161                                                         /* increment to next byte */
1162                                                         ptr_data++;
1163                                                         /* EFARR file id */
1164                                                         memcpy(&arr_file_id, ptr_data, 2);
1165                                                         /* swap byes */
1166                                                         SWAPBYTES16(arr_file_id);
1167                                                         ptr_data = ptr_data + 2;
1168                                                         arr_file_id_rec_num = *ptr_data++;
1169                                                 } else {
1170                                                         /* if tag length is not 3 */
1171                                                         /* ignoring bytes       */
1172                                                         //      ptr_data = ptr_data + 4;
1173                                                         dbg("Useless security attributes, so jump to next tag");
1174                                                         ptr_data = ptr_data + (*ptr_data + 1);
1175                                                 }
1176                                         } else {
1177                                                 dbg("INVALID FCP received[0x%x] - DEbug!", *ptr_data);
1178                                                 free(recordData);
1179                                                 ReleaseResponse();
1180                                                 return;
1181                                         }
1182
1183                                         dbg("Current ptr_data value is [%x]", *ptr_data);
1184
1185                                         /* file size excluding structural info*/
1186                                         if (*ptr_data == 0x80) {
1187                                                 /* for EF file size is body of file and for Linear or cyclic it is
1188                                                  * number of recXsizeof(one record)
1189                                                  */
1190                                                 /* increment to next byte */
1191                                                 ptr_data++;
1192                                                 /* length is 1 byte - value is 2 bytes or more */
1193                                                 ptr_data++;
1194                                                 memcpy(&file_size, ptr_data, 2);
1195                                                 /* swap bytes */
1196                                                 SWAPBYTES16(file_size);
1197                                                 ptr_data = ptr_data + 2;
1198                                         } else {
1199                                                 dbg("INVALID FCP received - DEbug!");
1200                                                 free(recordData);
1201                                                 ReleaseResponse();
1202                                                 return;
1203                                         }
1204
1205                                         /* total file size including structural info*/
1206                                         if (*ptr_data == 0x81) {
1207                                                 int len;
1208                                                 /* increment to next byte */
1209                                                 ptr_data++;
1210                                                 /* length */
1211                                                 len = *ptr_data;
1212                                                 /* ignored bytes */
1213                                                 ptr_data = ptr_data + 3;
1214                                         } else {
1215                                                 dbg("INVALID FCP received - DEbug!");
1216                                                 /* 0x81 is optional tag?? check out! so do not return -1 from here! */
1217                                                 /* return -1; */
1218                                         }
1219                                         /*short file identifier ignored*/
1220                                         if (*ptr_data == 0x88) {
1221                                                 dbg("0x88: Do Nothing");
1222                                                 /*DO NOTHING*/
1223                                         }
1224                                 } else {
1225                                         dbg("INVALID FCP received - DEbug!");
1226                                         free(recordData);
1227                                         ReleaseResponse();
1228                                         return;
1229                                 }
1230                         } else if (tcore_sim_get_type(co_sim) == SIM_TYPE_GSM) {
1231                                 unsigned char gsm_specific_file_data_len = 0;
1232                                 /*      ignore RFU byte1 and byte2 */
1233                                 ptr_data++;
1234                                 ptr_data++;
1235                                 /*      file size */
1236                                 //file_size = p_info->response_len;
1237                                 memcpy(&file_size, ptr_data, 2);
1238                                 /* swap bytes */
1239                                 SWAPBYTES16(file_size);
1240                                 /*      parsed file size */
1241                                 ptr_data = ptr_data + 2;
1242                                 /*  file id  */
1243                                 memcpy(&file_id, ptr_data, 2);
1244                                 SWAPBYTES16(file_id);
1245                                 dbg(" FILE id --> [%x]", file_id);
1246                                 ptr_data = ptr_data + 2;
1247                                 /* save file type - transparent, linear fixed or cyclic */
1248                                 file_type_tag = (*(ptr_data + 7));
1249
1250                                 switch (*ptr_data) {
1251                                         case 0x0:
1252                                                 /* RFU file type */
1253                                                 dbg(" RFU file type- not handled - Debug!");
1254                                                 break;
1255                                         case 0x1:
1256                                                 /* MF file type */
1257                                                 dbg(" MF file type - not handled - Debug!");
1258                                                 break;
1259                                         case 0x2:
1260                                                 /* DF file type */
1261                                                 dbg(" DF file type - not handled - Debug!");
1262                                                 break;
1263                                         case 0x4:
1264                                                 /* EF file type */
1265                                                 dbg(" EF file type [%d] ", file_type_tag);
1266                                                 /*      increment to next byte */
1267                                                 ptr_data++;
1268
1269                                                 if (file_type_tag == 0x00 || file_type_tag == 0x01) {
1270                                                         /* increament to next byte as this byte is RFU */
1271                                                         ptr_data++;
1272                                                         file_type =
1273                                                                         (file_type_tag == 0x00) ? SIM_FTYPE_TRANSPARENT : SIM_FTYPE_LINEAR_FIXED;
1274                                                 } else {
1275                                                         /* increment to next byte */
1276                                                         ptr_data++;
1277                                                         /*      For a cyclic EF all bits except bit 7 are RFU; b7=1 indicates that */
1278                                                         /* the INCREASE command is allowed on the selected cyclic file. */
1279                                                         file_type = SIM_FTYPE_CYCLIC;
1280                                                 }
1281                                                 /* bytes 9 to 11 give SIM file access conditions */
1282                                                 ptr_data++;
1283                                                 /* byte 10 has one nibble that is RF U and another for INCREASE which is not used currently */
1284                                                 ptr_data++;
1285                                                 /* byte 11 is invalidate and rehabilate nibbles */
1286                                                 ptr_data++;
1287                                                 /* byte 12 - file status */
1288                                                 ptr_data++;
1289                                                 /* byte 13 - GSM specific data */
1290                                                 gsm_specific_file_data_len = *ptr_data;
1291                                                 ptr_data++;
1292                                                 /*      byte 14 - structure of EF - transparent or linear or cyclic , already saved above */
1293                                                 ptr_data++;
1294                                                 /* byte 15 - length of record for linear and cyclic , for transparent it is set to 0x00. */
1295                                                 record_len = *ptr_data;
1296                                                 dbg("record length[%d], file size[%d]", record_len, file_size);
1297
1298                                                 if (record_len != 0)
1299                                                         num_of_records = (file_size / record_len);
1300
1301                                                 dbg("Number of records [%d]", num_of_records);
1302                                                 break;
1303
1304                                         default:
1305                                                 dbg(" not handled file type");
1306                                                 break;
1307                                 }
1308                         }
1309                         else
1310                         {
1311                                 dbg(" Card Type - UNKNOWN  [%d]", tcore_sim_get_type(co_sim));
1312                         }
1313
1314                         dbg("req ef[0x%x] resp ef[0x%x] size[%ld] Type[0x%x] NumOfRecords[%ld] RecordLen[%ld]",
1315                                         file_meta->file_id, file_id, file_size, file_type, num_of_records, record_len);
1316
1317                         file_meta->file_type = file_type;
1318                         file_meta->data_size = file_size;
1319                         file_meta->rec_length = record_len;
1320                         file_meta->rec_count = num_of_records;
1321                         file_meta->current_index = 0; //reset for new record type EF
1322                         rt = SIM_ACCESS_SUCCESS;
1323                         free(recordData);
1324                 }
1325                 else
1326                 {
1327                         /*2. SIM access fail case*/
1328                         dbg("error to get ef[0x%x]", file_meta->file_id);
1329                         rt = _decode_status_word(sw1, sw2);
1330                 }
1331
1332                 ReleaseResponse();
1333
1334                 ur = tcore_user_request_ref(ur);
1335                 _next_from_get_file_info(co_sim, ur, file_meta->file_id, rt);
1336         }
1337         else
1338         {
1339                 ReleaseResponse();
1340                 dbg("error to get ef[0x%x]", file_meta->file_id);
1341                 rt = SIM_ACCESS_FAILED;;
1342
1343                 ur = tcore_user_request_ref(ur);
1344                 _next_from_get_file_info(co_sim, ur, file_meta->file_id, rt);
1345         }
1346 }
1347
1348 static void _response_get_file_data(TcorePending *p, int data_len, const void *data, void *user_data)
1349 {
1350         UserRequest *ur = NULL;
1351         CoreObject *co_sim = NULL;
1352         enum tel_sim_access_result rt;
1353         struct tel_sim_imsi imsi;
1354         struct s_sim_property *file_meta = NULL;
1355         gboolean dr = FALSE;
1356
1357         char *line = NULL;
1358         int ret = 0;
1359         int sw1 = 0;
1360         int sw2 = 0;
1361
1362         dbg("[SIM_READ_BINARY] or [SIM_READ_RECORD]");
1363
1364         dbg("sizeof struct tresp_sim_read = [%d]", sizeof(struct tresp_sim_read));
1365
1366         co_sim = tcore_pending_ref_core_object(p);
1367         if(!co_sim){
1368                 dbg("error -  core object is null");
1369                 return;
1370         }
1371         ur = tcore_pending_ref_user_request(p);
1372         file_meta = (struct s_sim_property*)tcore_user_request_ref_metainfo(ur, NULL);
1373
1374         if(sp_response->success > 0)
1375         {
1376                 line = sp_response->p_intermediates->line;
1377
1378                 ret = at_tok_start(&line);
1379                 if (ret < 0)
1380                         AT_TOK_ERROR(line);
1381
1382                 ret = at_tok_nextint(&line,&sw1);
1383                 if (ret < 0)
1384                         AT_TOK_ERROR(line);
1385                 ret = at_tok_nextint(&line,&sw2);
1386                 if (ret < 0)
1387                         AT_TOK_ERROR(line);
1388
1389                 if((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
1390                         char *hexStr;
1391                         char *fileData;
1392
1393                         ret = at_tok_nextstr(&line,&hexStr);
1394                         if (ret < 0)
1395                                 AT_TOK_ERROR(line);
1396
1397                         fileData = util_hexStringToBytes(hexStr);
1398                         util_hex_dump("    ", strlen(hexStr)/2, fileData);
1399
1400                         rt = SIM_ACCESS_SUCCESS;
1401                         file_meta->files.result = rt;
1402
1403                         switch (file_meta->file_id)
1404                         {
1405                                         case SIM_EF_IMSI:
1406                                                 dr = tcore_sim_decode_imsi(&imsi, (unsigned char *)fileData, strlen(fileData));
1407                                                 if (dr == FALSE) {
1408                                                         dbg("imsi decoding failed");
1409                                                 } else {
1410                                                         _sim_check_identity(co_sim,&imsi);
1411                                                         tcore_sim_set_imsi(co_sim,&imsi);
1412                                                 }
1413                                                 break;
1414
1415                                         case SIM_EF_ICCID:
1416                                                 dr = tcore_sim_decode_iccid(&file_meta->files.data.iccid, (unsigned char *)fileData, strlen(fileData));
1417                                                 break;
1418
1419                                         case SIM_EF_ELP:/*  2G EF -  2 bytes decoding*/
1420                                         case SIM_EF_USIM_LI: /* 3G EF - 2 bytes decoding*/
1421                                         case SIM_EF_USIM_PL:/*  3G EF - same as EFELP, so 2  byte decoding*/
1422                                         case SIM_EF_LP:/*  1 byte encoding*/
1423                                                 if (tcore_sim_get_type(co_sim) == SIM_TYPE_GSM && file_meta->file_id == SIM_EF_LP) {
1424                                                          /*2G LP(0x6F05) has 1 byte for each language*/
1425                                                         dr = tcore_sim_decode_lp(&file_meta->files.data.language, (unsigned char *)fileData, strlen(fileData));
1426                                                 } else {
1427                                                          /*3G LI(0x6F05)/PL(0x2F05), 2G ELP(0x2F05) has 2 bytes for each language*/
1428                                                         dr = tcore_sim_decode_li(file_meta->file_id, &file_meta->files.data.language, (unsigned char *)fileData, strlen(fileData));
1429                                                 }
1430                                                 break;
1431
1432                                         case SIM_EF_SPN:
1433                                                 dr = tcore_sim_decode_spn(&file_meta->files.data.spn, (unsigned char *)fileData, strlen(fileData));
1434                                                 break;
1435
1436                                         case SIM_EF_SPDI:
1437                                                 dr = tcore_sim_decode_spdi(&file_meta->files.data.spdi, (unsigned char *)fileData, strlen(fileData));
1438                                                 break;
1439
1440                                         case SIM_EF_SST:
1441 /*                                      if(tcore_sim_get_type(o) == SIM_TYPE_GSM)
1442                                                         dr = tcore_sim_decode_sst(&file_meta->files.data , fileData, response_len);
1443                                                 else if(tcore_sim_get_type(o) == SIM_TYPE_USIM)
1444                                                         dr = tcore_sim_decode_ust(&po->st_u.ust, fileData, response_len);
1445                                                 else {
1446                                                         dbg("err not handled tcore_sim_get_type(o)[%d] in here",tcore_sim_get_type(o));
1447                                                 }*/
1448                                                 break;
1449
1450                                         case SIM_EF_MBDN:
1451 /*                                      dr = tcore_sim_decode_xdn(&file_meta->files.data.mailbox, fileData, strlen(fileData));*/
1452                                                 break;
1453
1454                                         case SIM_EF_ECC:
1455                                                 if(tcore_sim_get_type(co_sim) == SIM_TYPE_GSM) {
1456                                                         dr = tcore_sim_decode_ecc(&file_meta->files.data.ecc, (unsigned char *)fileData, strlen(fileData));
1457                                                 } else if(tcore_sim_get_type(co_sim) == SIM_TYPE_USIM){
1458                                                         dr = tcore_sim_decode_uecc(&file_meta->files.data.ecc.ecc[file_meta->current_index-1], (unsigned char *)fileData, strlen(fileData));
1459                                                 } else {
1460                                                         dbg("err not handled tcore_sim_get_type(o)[%d] in here",tcore_sim_get_type(co_sim));
1461                                                 }
1462                                                 break;
1463
1464                                         case SIM_EF_USIM_MBI:
1465 /*                                      dr = tcore_sim_decode_mbi(&po->mb_u.mailbox.mbi, fileData, strlen(fileData));*/
1466                                                 break;
1467
1468                                         case SIM_EF_OPL:
1469                                                 dr = tcore_sim_decode_opl(&file_meta->files.data.opl.opl[file_meta->current_index-1], (unsigned char *)fileData, strlen(fileData));
1470                                                 break;
1471
1472                                         case SIM_EF_PNN:
1473                                                 dr = tcore_sim_decode_pnn(&file_meta->files.data.pnn.pnn[file_meta->current_index-1], (unsigned char *)fileData, strlen(fileData));
1474                                                 break;
1475
1476                                         case SIM_EF_OPLMN_ACT:
1477                                                 dr = tcore_sim_decode_oplmnwact(&file_meta->files.data.opwa, (unsigned char *)fileData, strlen(fileData));
1478                                                 break;
1479
1480                                         case SIM_EF_CPHS_CALL_FORWARD_FLAGS:
1481                                                 dr = tcore_sim_decode_cff(&file_meta->files.data.cf, (unsigned char *)fileData, strlen(fileData));
1482                                                 break;
1483
1484                                         case SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE:
1485 /*                                      dr = tcore_sim_decode_csp(&po->p_cphs->csp, fileData, strlen(fileData));*/
1486                                                 break;
1487
1488                                         case SIM_EF_CPHS_VOICE_MSG_WAITING:
1489                                                 dr = tcore_sim_decode_vmwf(&file_meta->files.data.mw.mw_data_u.cphs_mw, (unsigned char *)fileData, strlen(fileData));
1490                                                 break;
1491
1492                                         case SIM_EF_CPHS_MAILBOX_NUMBERS:
1493 /*                                      dbg("[CPHS]decoding SIM_EF_CPHS_MAILBOX_NUMBERS");
1494                                                 if (file_meta->current_index == 1)
1495                                                         dr = tcore_sim_decode_xdn(&po->mb_u.mbn.voice_line1, fileData, strlen(fileData));
1496                                                 else if (file_meta->current_index == 2)
1497                                                         dr = tcore_sim_decode_xdn(&po->mb_u.mbn.voice_line2, fileData, strlen(fileData));
1498                                                 else if (file_meta->current_index == 3)
1499                                                         dr = tcore_sim_decode_xdn(&po->mb_u.mbn.video, fileData, strlen(fileData));
1500                                                 else if (file_meta->current_index == 4)
1501                                                         dr = tcore_sim_decode_xdn(&po->mb_u.mbn.fax, fileData, strlen(fileData));
1502                                                 else {
1503                                                         dbg("[CPHS]wrong EF record index[%d] in here", file_meta->current_index);
1504                                                 }*/
1505                                                 break;
1506
1507                                         case SIM_EF_USIM_MWIS:
1508                                                 dr = tcore_sim_decode_mwis(&file_meta->files.data.mw.mw_data_u.mw, (unsigned char *)fileData, strlen(fileData));
1509                                                 break;
1510
1511                                         case SIM_EF_USIM_CFIS:
1512                                                 dr = tcore_sim_decode_cfis(&file_meta->files.data.cf, (unsigned char *)fileData, strlen(fileData));
1513                                                 break;
1514
1515                                         case SIM_EF_CPHS_SERVICE_STRING_TABLE:
1516                                                 dbg(" not handled -SIM_EF_CPHS_SERVICE_STRING_TABLE ");
1517                                                 break;
1518
1519                                         case SIM_EF_CPHS_OPERATOR_NAME_STRING:
1520                                                 dr = tcore_sim_decode_ons((unsigned char*)&file_meta->files.data.cphs_net.full_name, (unsigned char *)fileData, strlen(fileData));
1521                                                 break;
1522
1523                                         case SIM_EF_CPHS_DYNAMICFLAGS:
1524 /*                                      dr = tcore_sim_decode_dynamic_flag(&po->p_cphs->dflagsinfo, fileData, strlen(fileData));*/
1525                                                 break;
1526
1527                                         case SIM_EF_CPHS_DYNAMIC2FLAG:
1528 /*                                      dr = tcore_sim_decode_dynamic2_flag(&po->p_cphs->d2flagsinfo, fileData, strlen(fileData));*/
1529                                                 break;
1530
1531                                         case SIM_EF_CPHS_CPHS_INFO:
1532                                                 dr = tcore_sim_decode_cphs_info(&file_meta->files.data.cphs, (unsigned char *)fileData, strlen(fileData));
1533                                                 break;
1534
1535                                         case SIM_EF_CPHS_OPERATOR_NAME_SHORT_FORM_STRING:
1536                                                 dr = tcore_sim_decode_short_ons((unsigned char*)&file_meta->files.data.cphs_net.short_name, (unsigned char *)fileData,  strlen(fileData));
1537                                                 break;
1538
1539                                         case SIM_EF_CPHS_INFORMATION_NUMBERS:
1540 /*                                      dr = tcore_sim_decode_information_number(&po->p_cphs->infn, fileData, strlen(fileData));*/
1541                                                 break;
1542
1543                                         default:
1544                                                 dbg("File Decoding Failed - not handled File[0x%x]", file_meta->file_id);
1545                                                 dr = 0;
1546                                                 break;
1547                                 }
1548
1549                         free(fileData);
1550                 }
1551                 else
1552                 {
1553                         rt =  _decode_status_word(sw1, sw2);
1554                         file_meta->files.result = rt;
1555                 }
1556
1557                 ReleaseResponse();
1558         }
1559         else
1560         {
1561                 ReleaseResponse();
1562                 rt = SIM_ACCESS_FAILED;;
1563                 file_meta->files.result = rt;
1564         }
1565
1566         ur = tcore_user_request_ref(ur);
1567         _next_from_get_file_data(tcore_pending_ref_core_object(p), ur, rt, dr);
1568 }
1569
1570 static gboolean _get_sim_type(CoreObject *o)
1571 {
1572         TcorePlugin *p = NULL;
1573         TcoreHal *h = NULL;
1574         TcorePending *pending = NULL;
1575
1576         char *cmd_str = NULL;
1577         struct ATReqMetaInfo metainfo;
1578         int info_len =0;
1579         UserRequest *ur = NULL;
1580
1581         if (!o)
1582                 return FALSE;
1583
1584         p = tcore_object_ref_plugin(o);
1585         h = tcore_object_get_hal(o);
1586
1587         memset(&metainfo, 0, sizeof(struct ATReqMetaInfo));
1588         metainfo.type = SINGLELINE;
1589         memcpy(metainfo.responsePrefix,"%SCCT:",strlen("%SCCT:"));
1590         info_len = sizeof(struct ATReqMetaInfo);
1591
1592         ur = tcore_user_request_new(NULL, NULL);
1593
1594         tcore_user_request_set_metainfo(ur, info_len, &metainfo);
1595
1596         // AT+CPIN=<pin>[,<newpin>]
1597         cmd_str = g_strdup("AT%SCCT\r");
1598
1599         pending = tcore_pending_new(o, ID_RESERVED_AT);
1600         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
1601         tcore_pending_set_timeout(pending, 0);
1602         tcore_pending_set_response_callback(pending, _response_get_sim_type, NULL);
1603         tcore_pending_link_user_request(pending, ur);
1604
1605         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
1606
1607         tcore_hal_send_request(h, pending);
1608
1609         free(cmd_str);
1610
1611         return TRUE;
1612 }
1613
1614 static TReturn _get_file_info(CoreObject *o, UserRequest *ur, const enum tel_sim_file_id ef)
1615 {
1616         TcorePlugin *p = NULL;
1617         TcoreHal *h = NULL;
1618         TcorePending *pending = NULL;
1619         //size_t size = 0;
1620         struct s_sim_property file_meta={0,};
1621         TReturn trt = 0;
1622
1623         char *cmd_str = NULL;
1624         int info_len =0;
1625
1626         if (!o)
1627                 return TCORE_RETURN_EINVAL;
1628
1629         p = tcore_object_ref_plugin(o);
1630         h = tcore_object_get_hal(o);
1631
1632         file_meta.file_id = ef;
1633         file_meta.metainfo.type = SINGLELINE;
1634         memcpy(file_meta.metainfo.responsePrefix,"+CRSM:",strlen("+CRSM:"));
1635         info_len = sizeof(struct s_sim_property);
1636
1637         tcore_user_request_set_command(ur, TREQ_CUSTOM);
1638
1639         trt = tcore_user_request_set_metainfo(ur, sizeof(struct s_sim_property), &file_meta);
1640         dbg("trt[%d]",trt);
1641
1642         // AT+CRSM=<command>,<fildid>,<p1>,<p2+C29,<p3>
1643         cmd_str = g_strdup_printf("AT+CRSM=192, %d%s", ef, "\r");
1644
1645         dbg("new pending(IPC_SEC_RSIM_ACCESS GET - SELECT EF[0x%x])",ef);
1646         pending = tcore_pending_new(o, ID_RESERVED_AT);
1647         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
1648         tcore_pending_set_timeout(pending, 0);
1649         tcore_pending_set_response_callback(pending, _response_get_file_info, NULL);
1650         tcore_pending_link_user_request(pending, ur);
1651
1652         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
1653
1654         tcore_hal_send_request(h, pending);
1655         free(cmd_str);
1656
1657         return TCORE_RETURN_SUCCESS;
1658 }
1659
1660 static gboolean _get_file_data(CoreObject *o, UserRequest *ur, const enum tel_sim_file_id ef, const int offset, const int length)
1661 {
1662         TcorePlugin *p = NULL;
1663         TcoreHal *h = NULL;
1664         TcorePending *pending = NULL;
1665         //size_t size = 0;
1666
1667         char *cmd_str = NULL;
1668         struct ATReqMetaInfo metainfo;
1669         int info_len =0;
1670         int p1;
1671         int p2;
1672         int p3;
1673
1674         if (!o)
1675                 return FALSE;
1676
1677         dbg("new pending(IPC_SEC_RSIM_ACCESS GET - READ BIN)");
1678
1679         p = tcore_object_ref_plugin(o);
1680         h = tcore_object_get_hal(o);
1681
1682 // offset for reading the TRANSPARENT data
1683         p1 = (unsigned char)(offset & 0xFF00) >> 8;
1684         p2 = (unsigned char)offset & 0x00FF; //offset low
1685         p3 = (unsigned char)length;
1686         dbg("EF[0x%x]", ef);
1687
1688         memset(&metainfo, 0, sizeof(struct ATReqMetaInfo));
1689         metainfo.type = SINGLELINE;
1690         memcpy(metainfo.responsePrefix,"+CRSM:",strlen("+CRSM:"));
1691         info_len = sizeof(struct ATReqMetaInfo);
1692
1693         // AT+CRSM=<command>,<fildid>,<p1>,<p2+C29,<p3>
1694         cmd_str = g_strdup_printf("AT+CRSM=176, %d%s", ef, "\r");
1695
1696         pending = tcore_pending_new(o, ID_RESERVED_AT);
1697         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
1698         tcore_pending_set_timeout(pending, 0);
1699         tcore_pending_set_response_callback(pending, _response_get_file_data, NULL);
1700         tcore_pending_link_user_request(pending, ur);
1701
1702         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
1703
1704         tcore_hal_send_request(h, pending);
1705         free(cmd_str);
1706         return TRUE;
1707 }
1708
1709 static gboolean _get_file_record(CoreObject *o, UserRequest *ur, const enum tel_sim_file_id ef, const int index, const int length)
1710 {
1711         dbg("need to be implemented to use ATCMD");
1712
1713         return TRUE;
1714 }
1715
1716 static gboolean on_event_pin_status(CoreObject *o, const void *event_info, void *user_data)
1717 {
1718         UserRequest *ur;
1719         char *line = (char *) event_info;
1720         //struct tnoti_sim_status noti_data;
1721         struct s_sim_property *sp = NULL;
1722         enum tel_sim_status sim_status = SIM_STATUS_INITIALIZING;
1723         enum s_sim_sec_locktype_e locktype = SEC_LOCK_TYPE_NONE;
1724         enum s_sim_sec_lockkey_e lockkey = SEC_LOCK_KEY_NONE;
1725
1726         dbg("PIN_STATUS NOTI : %s", line);
1727
1728         _convert_SCPIN_noti(line,&locktype, &lockkey);
1729
1730         sp = tcore_sim_ref_userdata(o);
1731
1732
1733         switch (locktype) {
1734                 case SEC_LOCK_TYPE_READY:
1735                         if (lockkey == SEC_LOCK_KEY_UNLOCKED) {
1736                                 sim_status = SIM_STATUS_INITIALIZING;
1737                                 dbg(" Inside PIN disabled at BOOT UP");
1738                         }
1739                         else {
1740                                 dbg(" not handled case p_status->lock_key[%d]", lockkey);
1741                         }
1742                         break;
1743
1744                 case SEC_LOCK_TYPE_PS:
1745                         sim_status = SIM_STATUS_LOCK_REQUIRED;
1746                         dbg( " SIM LOCK required");
1747                         break;
1748
1749                 case SEC_LOCK_TYPE_PF:
1750                         sim_status = SIM_STATUS_CARD_ERROR;
1751                         dbg( "PF required ");
1752                         break;
1753
1754                 case SEC_LOCK_TYPE_SC:
1755                         switch (lockkey) {
1756                                 case SEC_LOCK_KEY_UNLOCKED:
1757                                         break;
1758                                 case SEC_LOCK_KEY_PIN:
1759                                         sim_status = SIM_STATUS_PIN_REQUIRED;
1760                                         dbg( " PIN1 required");
1761                                         break;
1762                                 case SEC_LOCK_KEY_PUK:
1763                                         sim_status = SIM_STATUS_PUK_REQUIRED;
1764                                         dbg( " PUK required");
1765                                         break;
1766                                 case SEC_LOCK_KEY_PERM_BLOCKED:
1767                                         sim_status = SIM_STATUS_CARD_BLOCKED;
1768                                         dbg( " Card permanently blocked");
1769                                         break;
1770                                 default:
1771                                         dbg(" SEC_SIM_LOCK_SC -not handled SEC Lock key ");
1772                                         break;
1773                         }
1774                         break;
1775
1776                 case SEC_LOCK_TYPE_FD:
1777                         dbg(" SEC_LOCK_TYPE_FD -not handled Notification");
1778                         break;
1779
1780                 case SEC_LOCK_TYPE_PN:
1781                         switch (lockkey) {
1782                                 case SEC_LOCK_KEY_PIN:
1783                                         dbg(" ADMIN-NCK required");
1784                                         sim_status = SIM_STATUS_NCK_REQUIRED;
1785                                         break;
1786                                 default:
1787                                         dbg(" SIM_LOCK_PN/PU/PP/PC -not handled SEC Lock key =[%d]",
1788                                                         lockkey);
1789                                         break;
1790                         }
1791                         break;
1792
1793                 case SEC_LOCK_TYPE_PU:
1794                         dbg("Lock Personalization p_status->lock_key =[%d]", lockkey);
1795                         switch (lockkey) {
1796                                 case SEC_LOCK_KEY_PIN:
1797                                         dbg(" ADMIN-NSCK required");
1798                                         sim_status = SIM_STATUS_NSCK_REQUIRED;
1799                                         break;
1800                                 default:
1801                                         dbg(" SIM_LOCK_PN/PU/PP/PC -not handled SEC Lock key =[%d]",
1802                                                         lockkey);
1803                                         break;
1804                         }
1805                         break;
1806
1807                 case SEC_LOCK_TYPE_PP:
1808                         switch (lockkey) {
1809                                 dbg("Lock Personalization p_status->lock_key =[%d]", lockkey);
1810                         case SEC_LOCK_KEY_PIN:
1811                                 dbg(" ADMIN-SPCK required");
1812                                 sim_status = SIM_STATUS_SPCK_REQUIRED;
1813                                 break;
1814                         default:
1815                                 dbg(" SIM_LOCK_PN/PU/PP/PC -not handled SEC Lock key =[%d]",
1816                                                 lockkey);
1817                                 break;
1818                         }
1819                         break;
1820
1821                 case SEC_LOCK_TYPE_PC:
1822                         switch (lockkey) {
1823                                 dbg("Lock Personalization p_status->lock_key =[%d]", lockkey);
1824                         case SEC_LOCK_KEY_PIN:
1825                                 dbg(" ADMIN-CCK required");
1826                                 sim_status = SIM_STATUS_CCK_REQUIRED;
1827                                 break;
1828                         default:
1829                                 dbg(" SIM_LOCK_PN/PU/PP/PC -not handled SEC Lock key =[%d]",
1830                                                 lockkey);
1831                                 break;
1832                         }
1833                         break;
1834
1835                 case SEC_LOCK_TYPE_SC2:
1836                         dbg("SEC_LOCK_TYPE_SC2: NOT Handled - Debug");
1837                         break;
1838
1839                 case SEC_LOCL_TYPE_PUK2:
1840                         dbg("SEC_LOCL_TYPE_PUK2: NOT Handled - Debug");
1841                         break;
1842
1843                 case SEC_LOCK_TYPE_NO_SIM:
1844                         sim_status = SIM_STATUS_CARD_NOT_PRESENT;
1845                         dbg( "NO SIM");
1846                         break;
1847
1848                 case SEC_LOCK_TYPE_UNAVAIL:
1849                 case SEC_SIM_INIT_CRASH: //SMC Lab requirement
1850                         sim_status = SIM_STATUS_CARD_ERROR;
1851                         dbg( "SIM unavailable");
1852                         break;
1853
1854                 case SEC_SIM_INIT_COMPLETED:
1855                         dbg( "[SIM DATA] MODEM SIM INIT COMPLETED");
1856                         sim_status = SIM_STATUS_INIT_COMPLETED;
1857                         break;
1858
1859                 case SEC_PB_INIT_COMPLETED:
1860                         dbg("[SIM DATA] MODEM SIM PB INIT COMPLETED. not handled here! s_phonebook should handle!");
1861                         return TRUE;
1862                         break;
1863
1864                 default:
1865                         dbg(" not handled SEC lock type ");
1866                         break;
1867         }
1868
1869         dbg("[SIM]Current co->sim_status[%d] and from modem[0x%x]",tcore_sim_get_status(o), sim_status);
1870
1871         switch (sim_status) {
1872                 case SIM_STATUS_INIT_COMPLETED:
1873                         ur = tcore_user_request_new(NULL, NULL); //this is for using ur metainfo set/ref functionality.
1874                         _get_file_info(o, ur, SIM_EF_IMSI);
1875                         break;
1876
1877                 case SIM_STATUS_INITIALIZING:
1878                 case SIM_STATUS_PIN_REQUIRED:
1879                 case SIM_STATUS_PUK_REQUIRED:
1880                 case SIM_STATUS_CARD_BLOCKED:
1881                 case SIM_STATUS_NCK_REQUIRED:
1882                 case SIM_STATUS_NSCK_REQUIRED:
1883                 case SIM_STATUS_SPCK_REQUIRED:
1884                 case SIM_STATUS_CCK_REQUIRED:
1885                 case SIM_STATUS_LOCK_REQUIRED:
1886                         if( sp->first_recv_status == SIM_STATUS_UNKNOWN ) {
1887                                 dbg("first received sim status[%d]",sim_status);
1888                                 sp->first_recv_status = sim_status;
1889                                 _get_sim_type(o);
1890                         }
1891                         break;
1892
1893                 case SIM_STATUS_CARD_REMOVED:
1894                 case SIM_STATUS_CARD_NOT_PRESENT:
1895                 case SIM_STATUS_CARD_ERROR:
1896                         if (sim_status == SIM_STATUS_CARD_NOT_PRESENT && tcore_sim_get_status(o) != SIM_STATUS_UNKNOWN) {
1897                                 dbg("[SIM]SIM CARD REMOVED!!");
1898                                 sim_status = SIM_STATUS_CARD_REMOVED;
1899                         }
1900                         _sim_status_update(o,sim_status);
1901                         break;
1902
1903                 default:
1904                         dbg("not handled status[%d]", sim_status);
1905                         break;
1906         }
1907
1908         return TRUE;
1909 }
1910
1911 static void on_response_verify_pins(TcorePending *p, int data_len, const void *data, void *user_data)
1912 {
1913         UserRequest *ur = NULL;
1914         CoreObject *co_sim = NULL;
1915         struct s_sim_property *sp = NULL;
1916         struct tresp_sim_verify_pins resp = {0,};
1917
1918         char* line=NULL;
1919         int ret;
1920         int error;
1921
1922         co_sim = tcore_pending_ref_core_object(p);
1923         sp = tcore_sim_ref_userdata(co_sim);
1924
1925         ur = tcore_pending_ref_user_request(p);
1926         if(!ur)
1927                 dbg("error - current ur is NULL");
1928
1929         printResponse();
1930
1931         if(sp_response->success > 0)
1932         {
1933                 ReleaseResponse();
1934
1935                 resp.result = SIM_PIN_OPERATION_SUCCESS;
1936                 resp.pin_type = _sim_get_current_pin_facility(sp);
1937                 tcore_user_request_send_response(ur, TRESP_SIM_VERIFY_PINS,     sizeof(struct tresp_sim_verify_pins), &resp);
1938         }
1939         else
1940         {
1941                 //failure case - consider this later
1942                 line = sp_response->finalResponse;
1943
1944                 ret = at_tok_start(&line);
1945                 if (ret < 0)
1946                         AT_TOK_ERROR(line);
1947
1948                 ret = at_tok_nextint(&line,&error);
1949                 if (ret < 0)
1950                         AT_TOK_ERROR(line);
1951
1952                 ReleaseResponse();
1953
1954                 // ur = user_request_dup(ur);
1955                 // _get_retry_count(co_sim, ur);
1956
1957                 resp.result = SIM_INCORRECT_PASSWORD;
1958                 resp.pin_type = _sim_get_current_pin_facility(sp);
1959                 resp.retry_count = 3;
1960                 tcore_user_request_send_response(ur, TRESP_SIM_VERIFY_PINS,     sizeof(struct tresp_sim_verify_pins), &resp);
1961
1962         }
1963 }
1964
1965 static void on_response_verify_puks(TcorePending *p, int data_len, const void *data, void *user_data)
1966 {
1967         UserRequest *ur = NULL;
1968         CoreObject *co_sim = NULL;
1969         struct s_sim_property *sp = NULL;
1970         struct tresp_sim_verify_puks resp = {0,};
1971
1972         char* line=NULL;
1973         int ret;
1974         int error;
1975
1976         co_sim = tcore_pending_ref_core_object(p);
1977         sp = tcore_sim_ref_userdata(co_sim);
1978
1979         ur = tcore_pending_ref_user_request(p);
1980         if(!ur)
1981                 dbg("error - current ur is NULL");
1982
1983         printResponse();
1984
1985         if(sp_response->success > 0)
1986         {
1987                 ReleaseResponse();
1988
1989                 resp.result = SIM_PIN_OPERATION_SUCCESS;
1990                 resp.pin_type = _sim_get_current_pin_facility(sp);
1991                 tcore_user_request_send_response(ur, TRESP_SIM_VERIFY_PINS,     sizeof(struct tresp_sim_verify_pins), &resp);
1992         }
1993         else
1994         {
1995                 //failure case - consider this later
1996                 line = sp_response->finalResponse;
1997
1998                 ret = at_tok_start(&line);
1999                 if (ret < 0)
2000                         AT_TOK_ERROR(line);
2001
2002                 ret = at_tok_nextint(&line,&error);
2003                 if (ret < 0)
2004                         AT_TOK_ERROR(line);
2005
2006                 ReleaseResponse();
2007
2008                 // ur = user_request_dup(ur);
2009                 // _get_retry_count(co_sim, ur);
2010
2011                 resp.result = SIM_INCORRECT_PASSWORD;
2012                 resp.pin_type = _sim_get_current_pin_facility(sp);
2013                 tcore_user_request_send_response(ur, TRESP_SIM_VERIFY_PUKS,     sizeof(struct tresp_sim_verify_puks), &resp);
2014
2015         }
2016 }
2017
2018 static void on_response_change_pins(TcorePending *p, int data_len, const void *data, void *user_data)
2019 {
2020         UserRequest *ur = NULL;
2021         CoreObject *co_sim = NULL;
2022         struct s_sim_property *sp = NULL;
2023         struct tresp_sim_change_pins resp = {0,};
2024
2025         char* line=NULL;
2026         int ret;
2027         int error;
2028
2029         co_sim = tcore_pending_ref_core_object(p);
2030         sp = tcore_sim_ref_userdata(co_sim);
2031
2032         ur = tcore_pending_ref_user_request(p);
2033         if(!ur)
2034                 dbg("error - current ur is NULL");
2035
2036         printResponse();
2037
2038         if(sp_response->success > 0)
2039         {
2040                 ReleaseResponse();
2041
2042                 resp.result = SIM_PIN_OPERATION_SUCCESS;
2043                 resp.pin_type = _sim_get_current_pin_facility(sp);
2044                 tcore_user_request_send_response(ur, TRESP_SIM_VERIFY_PINS,     sizeof(struct tresp_sim_verify_pins), &resp);
2045         }
2046         else
2047         {
2048                 //failure case - consider this later
2049                 line = sp_response->finalResponse;
2050
2051                 ret = at_tok_start(&line);
2052                 if (ret < 0)
2053                         AT_TOK_ERROR(line);
2054
2055                 ret = at_tok_nextint(&line,&error);
2056                 if (ret < 0)
2057                         AT_TOK_ERROR(line);
2058
2059                 ReleaseResponse();
2060
2061                 // ur = user_request_dup(ur);
2062                 // _get_retry_count(co_sim, ur);
2063
2064                 resp.result = SIM_INCORRECT_PASSWORD;
2065                 resp.pin_type = _sim_get_current_pin_facility(sp);
2066                 tcore_user_request_send_response(ur, TRESP_SIM_CHANGE_PINS,     sizeof(struct tresp_sim_change_pins), &resp);
2067         }
2068 }
2069
2070 static void on_response_get_facility_status(TcorePending *p, int data_len, const void *data, void *user_data)
2071 {
2072         UserRequest *ur = NULL;
2073         CoreObject *co_sim = NULL;
2074         struct s_sim_property *sec_meta = NULL;
2075         struct tresp_sim_get_facility_status resp = {0,};
2076
2077         char *line = NULL;
2078         int ret;
2079
2080         co_sim = tcore_pending_ref_core_object(p);
2081         ur = tcore_pending_ref_user_request(p);
2082         sec_meta = (struct s_sim_property*)tcore_user_request_ref_metainfo(ur, NULL);
2083
2084         resp.result = SIM_PIN_OPERATION_SUCCESS;
2085         resp.type = _sim_get_current_pin_facility(sec_meta);
2086
2087         printResponse();
2088
2089         if(sp_response->success > 0)
2090         {
2091                 line = sp_response->p_intermediates->line;
2092
2093                 ret = at_tok_start(&line);
2094                 if (ret < 0)
2095                         AT_TOK_ERROR(line);
2096
2097                 ret = at_tok_nextint(&line,(int *)&resp.b_enable);
2098                 if (ret < 0)
2099                         AT_TOK_ERROR(line);
2100         }
2101         else
2102         {
2103                 resp.result = SIM_INCOMPATIBLE_PIN_OPERATION;
2104         }
2105
2106         ReleaseResponse();
2107
2108         if (ur) {
2109                 tcore_user_request_send_response(ur, TRESP_SIM_GET_FACILITY_STATUS,
2110                                 sizeof(struct tresp_sim_get_facility_status), &resp);
2111         }
2112 }
2113
2114 static void on_response_enable_facility(TcorePending *p, int data_len, const void *data, void *user_data)
2115 {
2116         UserRequest *ur = NULL;
2117         CoreObject *co_sim = NULL;
2118         struct s_sim_property *sec_meta = NULL;
2119         struct tresp_sim_enable_facility resp = {0,};
2120         struct s_sim_property *sp = NULL;
2121
2122         char *line = NULL;
2123         int ret;
2124
2125         co_sim = tcore_pending_ref_core_object(p);
2126         ur = tcore_pending_ref_user_request(p);
2127         sp = tcore_sim_ref_userdata(co_sim);
2128         sec_meta = (struct s_sim_property*)tcore_user_request_ref_metainfo(ur, NULL);
2129
2130         resp.result = SIM_PIN_OPERATION_SUCCESS;
2131         resp.type = _sim_get_current_pin_facility(sec_meta);
2132
2133         printResponse();
2134
2135         if(sp_response->success > 0)
2136         {
2137                 line = sp_response->p_intermediates->line;
2138
2139                 ret = at_tok_start(&line);
2140                 if (ret < 0)
2141                         AT_TOK_ERROR(line);
2142
2143                 ret = at_tok_nextint(&line,(int *)&resp.result);
2144                 if (ret < 0)
2145                         AT_TOK_ERROR(line);
2146         }
2147         else
2148         {
2149                 resp.result = SIM_INCOMPATIBLE_PIN_OPERATION;
2150         }
2151
2152         ReleaseResponse();
2153
2154         if (ur) {
2155                 resp.type = _sim_get_current_pin_facility(sp);
2156                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_enable_facility), &resp);
2157         }
2158 }
2159
2160 static void on_response_disable_facility(TcorePending *p, int data_len, const void *data, void *user_data)
2161 {
2162         UserRequest *ur = NULL;
2163         CoreObject *co_sim = NULL;
2164         struct s_sim_property *sec_meta = NULL;
2165         struct tresp_sim_disable_facility resp = {0,};
2166         struct s_sim_property *sp = NULL;
2167
2168         char *line = NULL;
2169         int ret;
2170
2171         co_sim = tcore_pending_ref_core_object(p);
2172         ur = tcore_pending_ref_user_request(p);
2173         sp = tcore_sim_ref_userdata(co_sim);
2174         sec_meta = (struct s_sim_property*)tcore_user_request_ref_metainfo(ur, NULL);
2175
2176         resp.result = SIM_PIN_OPERATION_SUCCESS;
2177         resp.type = _sim_get_current_pin_facility(sec_meta);
2178
2179         printResponse();
2180
2181         if(sp_response->success > 0)
2182         {
2183                 line = sp_response->p_intermediates->line;
2184
2185                 ret = at_tok_start(&line);
2186                 if (ret < 0)
2187                         AT_TOK_ERROR(line);
2188
2189                 ret = at_tok_nextint(&line,(int *)&resp.result);
2190                 if (ret < 0)
2191                         AT_TOK_ERROR(line);
2192         }
2193         else
2194         {
2195                 resp.result = SIM_INCOMPATIBLE_PIN_OPERATION;
2196         }
2197
2198         ReleaseResponse();
2199
2200         if (ur) {
2201                 resp.type = _sim_get_current_pin_facility(sp);
2202                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_disable_facility), &resp);
2203         }
2204 }
2205
2206 static TReturn s_verify_pins(CoreObject *o, UserRequest *ur)
2207 {
2208         TcorePlugin *p = NULL;
2209         TcoreHal *h = NULL;
2210         TcorePending *pending = NULL;
2211
2212         struct s_sim_property *sp = NULL;
2213         const struct treq_sim_verify_pins *req_data;
2214
2215         char *cmd_str = NULL;
2216         struct ATReqMetaInfo metainfo;
2217         int info_len =0;
2218
2219         if (!o || !ur)
2220                 return TCORE_RETURN_EINVAL;
2221
2222         p = tcore_object_ref_plugin(o);
2223         h = tcore_object_get_hal(o);
2224         req_data = tcore_user_request_ref_data(ur, NULL);
2225         sp = tcore_sim_ref_userdata(o);
2226
2227         if (req_data->pin_type == SIM_PTYPE_PIN1) {
2228                 sp->current_sec_op = SEC_PIN1_VERIFY;
2229         }
2230         else if (req_data->pin_type == SIM_PTYPE_PIN2) {
2231                 sp->current_sec_op = SEC_PIN2_VERIFY;
2232         }
2233         else if (req_data->pin_type == SIM_PTYPE_SIM) {
2234                 sp->current_sec_op = SEC_SIM_VERIFY;
2235         }
2236         else if (req_data->pin_type == SIM_PTYPE_ADM) {
2237                 sp->current_sec_op = SEC_ADM_VERIFY;
2238         }
2239         else {
2240                 return TCORE_RETURN_EINVAL;
2241         }
2242
2243         memset(&metainfo, 0, sizeof(struct ATReqMetaInfo));
2244         metainfo.type = NO_RESULT;
2245         metainfo.responsePrefix[0] ='\0';
2246         info_len = sizeof(struct ATReqMetaInfo);
2247
2248         tcore_user_request_set_metainfo(ur, info_len, &metainfo);
2249
2250         // AT+CPIN=<pin>[,<newpin>]
2251         cmd_str = g_strdup_printf("AT+CPIN=\"%s\"%s", req_data->pin, "\r");
2252         dbg("new pending(verify - pins), %s", cmd_str);
2253
2254         pending = tcore_pending_new(o,ID_RESERVED_AT);
2255         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
2256         tcore_pending_set_timeout(pending, 0);
2257         tcore_pending_set_priority(pending, TCORE_PENDING_PRIORITY_DEFAULT);
2258         tcore_pending_set_response_callback(pending, on_response_verify_pins, NULL);
2259         tcore_pending_link_user_request(pending, ur);
2260
2261         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
2262
2263         tcore_hal_send_request(h, pending);
2264
2265         free(cmd_str);
2266
2267         return TCORE_RETURN_SUCCESS;
2268 }
2269
2270 static TReturn s_verify_puks(CoreObject *o, UserRequest *ur)
2271 {
2272         TcorePlugin *p = NULL;
2273         TcoreHal *h = NULL;
2274         TcorePending *pending = NULL;
2275
2276         const struct treq_sim_verify_puks *req_data;
2277         struct s_sim_property *sp = NULL;
2278
2279         char *cmd_str = NULL;
2280         struct ATReqMetaInfo metainfo;
2281         int info_len =0;
2282
2283         if (!o || !ur)
2284                 return TCORE_RETURN_EINVAL;
2285
2286         p = tcore_object_ref_plugin(o);
2287         h = tcore_object_get_hal(o);
2288         req_data = tcore_user_request_ref_data(ur, NULL);
2289         sp = tcore_sim_ref_userdata(o);
2290
2291
2292         if(req_data->puk_type == SIM_PTYPE_PUK1){
2293                 sp->current_sec_op = SEC_PUK1_VERIFY;
2294         }
2295         else if(req_data->puk_type == SIM_PTYPE_PUK2){
2296                 sp->current_sec_op = SEC_PUK2_VERIFY;
2297         }
2298         else {
2299                 return TCORE_RETURN_EINVAL;
2300         }
2301
2302         memset(&metainfo, 0, sizeof(struct ATReqMetaInfo));
2303         metainfo.type = NO_RESULT;
2304         metainfo.responsePrefix[0] ='\0';
2305         info_len = sizeof(struct ATReqMetaInfo);
2306
2307         tcore_user_request_set_metainfo(ur, info_len, &metainfo);
2308
2309         // AT+CPIN=<pin>[,<newpin>]
2310         cmd_str = g_strdup_printf("AT+CPIN=\"%s\", \"%s\"%s", req_data->puk, req_data->pin, "\r");
2311
2312         dbg("new pending(IPC_SEC_PIN_STATUS SET - verify puks)");
2313         pending = tcore_pending_new(o, ID_RESERVED_AT);
2314         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
2315         tcore_pending_set_timeout(pending, 0);
2316         tcore_pending_set_priority(pending, TCORE_PENDING_PRIORITY_DEFAULT);
2317         tcore_pending_set_response_callback(pending, on_response_verify_puks, NULL);
2318         tcore_pending_link_user_request(pending, ur);
2319
2320         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
2321
2322         tcore_hal_send_request(h, pending);
2323
2324         free(cmd_str);
2325
2326         return TCORE_RETURN_SUCCESS;
2327 }
2328
2329 static TReturn s_change_pins(CoreObject *o, UserRequest *ur)
2330 {
2331         TcorePlugin *p = NULL;
2332         TcoreHal *h = NULL;
2333         TcorePending *pending = NULL;
2334
2335         const struct treq_sim_change_pins *req_data;
2336         struct s_sim_property *sp = NULL;
2337
2338         char *cmd_str = NULL;
2339         struct ATReqMetaInfo metainfo;
2340         int info_len =0;
2341
2342         if (!o || !ur)
2343                 return TCORE_RETURN_EINVAL;
2344
2345         p = tcore_object_ref_plugin(o);
2346         h = tcore_object_get_hal(o);
2347         req_data = tcore_user_request_ref_data(ur, NULL);
2348         sp = tcore_sim_ref_userdata(o);
2349
2350         if(req_data->type == SIM_PTYPE_PIN1) {
2351                 sp->current_sec_op = SEC_PIN1_CHANGE;
2352         }
2353         else if(req_data->type == SIM_PTYPE_PIN2) {
2354                 sp->current_sec_op = SEC_PIN2_CHANGE;
2355         }
2356         else {
2357                 return TCORE_RETURN_EINVAL;
2358         }
2359
2360         memset(&metainfo, 0, sizeof(struct ATReqMetaInfo));
2361         metainfo.type = NO_RESULT;
2362         metainfo.responsePrefix[0] ='\0';
2363         info_len = sizeof(struct ATReqMetaInfo);
2364
2365         tcore_user_request_set_metainfo(ur, info_len, &metainfo);
2366
2367         // AT+CPIN=<pin>[,<newpin>]
2368         cmd_str = g_strdup_printf("AT+CPIN=\"%s\", \"%s\"%s", req_data->old_pin, req_data->new_pin, "\r");
2369
2370         dbg("new pending(IPC_SEC_CHANGE_LOCKING_PW SET)");
2371         pending = tcore_pending_new(o, ID_RESERVED_AT);
2372         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
2373         tcore_pending_set_timeout(pending, 0);
2374         tcore_pending_set_priority(pending, TCORE_PENDING_PRIORITY_DEFAULT);
2375         tcore_pending_set_response_callback(pending, on_response_change_pins, NULL);
2376         tcore_pending_link_user_request(pending, ur);
2377
2378         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
2379
2380         tcore_hal_send_request(h, pending);
2381
2382         free(cmd_str);
2383
2384         return TCORE_RETURN_SUCCESS;
2385 }
2386
2387 static TReturn s_get_facility_status(CoreObject *o, UserRequest *ur)
2388 {
2389         TcorePlugin *p = NULL;
2390         TcoreHal *h = NULL;
2391         TcorePending *pending = NULL;
2392         struct s_sim_property sec_meta = {0,};
2393         TReturn trt = 0;
2394
2395         const struct treq_sim_get_facility_status *req_data;
2396         char *fac = "SC";
2397         int mode = 2;           // 2:query, 0: unlock, 1:lock
2398
2399         char *cmd_str = NULL;
2400 //      struct ATReqMetaInfo metainfo;
2401         int info_len =0;
2402
2403         if (!o || !ur)
2404                 return TCORE_RETURN_EINVAL;
2405
2406         p = tcore_object_ref_plugin(o);
2407         h = tcore_object_get_hal(o);
2408         req_data = tcore_user_request_ref_data(ur, NULL);
2409
2410         if(req_data->type == SIM_FACILITY_PS)
2411                 fac = "PS";
2412         else if(req_data->type == SIM_FACILITY_SC)
2413                 fac = "SC";
2414         else if(req_data->type == SIM_FACILITY_FD)
2415                 fac = "FD";
2416         else if(req_data->type == SIM_FACILITY_PN)
2417                 fac = "PN";
2418         else if(req_data->type == SIM_FACILITY_PU)
2419                 fac = "PU";
2420         else if(req_data->type == SIM_FACILITY_PP)
2421                 fac = "PP";
2422         else if(req_data->type == SIM_FACILITY_PC)
2423                 fac = "PC";
2424         else
2425                 return TCORE_RETURN_EINVAL;
2426
2427         sec_meta.current_sec_op = req_data->type;
2428         sec_meta.metainfo.type = SINGLELINE;
2429         memcpy(sec_meta.metainfo.responsePrefix,"+CLCK:",strlen("+CLCK:"));
2430         info_len = sizeof(struct s_sim_property);
2431         tcore_user_request_set_command(ur, TREQ_CUSTOM);
2432         trt = tcore_user_request_set_metainfo(ur, sizeof(struct s_sim_property), &sec_meta);
2433         dbg("trt[%d]",trt);
2434
2435         // AT+CLCK=<fac>,<mode>,<password>
2436         cmd_str = g_strdup_printf("AT+CLCK=\"%s\", %d%s", fac, mode, "\r");
2437
2438         dbg("new pending(IPC_SEC_PHONE_LOCK GET)");
2439         pending = tcore_pending_new(o, ID_RESERVED_AT);
2440         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
2441         tcore_pending_set_timeout(pending, 0);
2442         tcore_pending_set_priority(pending, TCORE_PENDING_PRIORITY_DEFAULT);
2443         tcore_pending_set_response_callback(pending, on_response_get_facility_status, NULL);
2444         tcore_pending_link_user_request(pending, ur);
2445
2446         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
2447
2448         tcore_hal_send_request(h, pending);
2449
2450         free(cmd_str);
2451
2452         return TCORE_RETURN_SUCCESS;
2453 }
2454
2455 static TReturn s_enable_facility(CoreObject *o, UserRequest *ur)
2456 {
2457         TcorePlugin *p = NULL;
2458         TcoreHal *h = NULL;
2459         TcorePending *pending = NULL;
2460         struct s_sim_property sec_meta = {0,};
2461         const struct treq_sim_enable_facility *req_data;
2462         struct s_sim_property *sp = NULL;
2463         char *fac = "SC";
2464         int mode = 1;           // 2:query, 0: unlock, 1:lock
2465
2466         char *cmd_str = NULL;
2467 //      struct ATReqMetaInfo metainfo;
2468         int info_len =0;
2469
2470         if (!o || !ur)
2471                 return TCORE_RETURN_EINVAL;
2472
2473         p = tcore_object_ref_plugin(o);
2474         h = tcore_object_get_hal(o);
2475         req_data = tcore_user_request_ref_data(ur, NULL);
2476         sp = tcore_sim_ref_userdata(o);
2477
2478         if (req_data->type == SIM_FACILITY_PS)
2479                 fac = "PS";
2480         else if (req_data->type == SIM_FACILITY_SC)
2481                 fac = "SC";
2482         else if (req_data->type == SIM_FACILITY_FD)
2483                 fac = "FD";
2484         else if (req_data->type == SIM_FACILITY_PN)
2485                 fac = "PN";
2486         else if (req_data->type == SIM_FACILITY_PU)
2487                 fac = "PU";
2488         else if (req_data->type == SIM_FACILITY_PP)
2489                 fac = "PP";
2490         else if (req_data->type == SIM_FACILITY_PC)
2491                 fac = "PC";
2492         else
2493                 return TCORE_RETURN_EINVAL;
2494
2495         sp->current_sec_op = SEC_SIM_ENABLE;
2496
2497         sec_meta.current_sec_op = req_data->type;
2498         sec_meta.metainfo.type = SINGLELINE;
2499         memcpy(sec_meta.metainfo.responsePrefix,"+CLCK:",strlen("+CLCK:"));
2500         info_len = sizeof(struct s_sim_property);
2501         tcore_user_request_set_command(ur, TREQ_CUSTOM);
2502         tcore_user_request_set_metainfo(ur, sizeof(struct s_sim_property), &sec_meta);
2503
2504         // AT+CLCK=<fac>,<mode>,<password>
2505         cmd_str = g_strdup_printf("AT+CLCK=\"%s\", %d, %s%s", fac, mode, req_data->password,"\r");
2506         dbg("new pending(enable_facility), %s", cmd_str);
2507
2508         pending = tcore_pending_new(o, ID_RESERVED_AT);
2509         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
2510         tcore_pending_set_timeout(pending, 0);
2511         tcore_pending_set_priority(pending, TCORE_PENDING_PRIORITY_DEFAULT);
2512         tcore_pending_set_response_callback(pending, on_response_enable_facility, NULL);
2513         tcore_pending_link_user_request(pending, ur);
2514
2515         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
2516
2517         tcore_hal_send_request(h, pending);
2518
2519         free(cmd_str);
2520
2521         return TCORE_RETURN_SUCCESS;
2522 }
2523
2524 static TReturn s_disable_facility(CoreObject *o, UserRequest *ur)
2525 {
2526         TcorePlugin *p = NULL;
2527         TcoreHal *h = NULL;
2528         TcorePending *pending = NULL;
2529         struct s_sim_property sec_meta = {0,};
2530         const struct treq_sim_enable_facility *req_data;
2531         struct s_sim_property *sp = NULL;
2532         char *fac = "SC";
2533         int mode = 0;           // 2:query, 0: unlock, 1:lock
2534
2535         char *cmd_str = NULL;
2536         //      struct ATReqMetaInfo metainfo;
2537         int info_len =0;
2538
2539         if (!o || !ur)
2540                 return TCORE_RETURN_EINVAL;
2541
2542         p = tcore_object_ref_plugin(o);
2543         h = tcore_object_get_hal(o);
2544         req_data = tcore_user_request_ref_data(ur, NULL);
2545         sp = tcore_sim_ref_userdata(o);
2546
2547         if (req_data->type == SIM_FACILITY_PS)
2548                 fac = "PS";
2549         else if (req_data->type == SIM_FACILITY_SC)
2550                 fac = "SC";
2551         else if (req_data->type == SIM_FACILITY_FD)
2552                 fac = "FD";
2553         else if (req_data->type == SIM_FACILITY_PN)
2554                 fac = "PN";
2555         else if (req_data->type == SIM_FACILITY_PU)
2556                 fac = "PU";
2557         else if (req_data->type == SIM_FACILITY_PP)
2558                 fac = "PP";
2559         else if (req_data->type == SIM_FACILITY_PC)
2560                 fac = "PC";
2561         else
2562                 return TCORE_RETURN_EINVAL;
2563
2564         sp->current_sec_op = SEC_SIM_ENABLE;
2565
2566         sec_meta.current_sec_op = req_data->type;
2567         sec_meta.metainfo.type = SINGLELINE;
2568         memcpy(sec_meta.metainfo.responsePrefix,"+CLCK:",strlen("+CLCK:"));
2569         info_len = sizeof(struct s_sim_property);
2570         tcore_user_request_set_command(ur, TREQ_CUSTOM);
2571         tcore_user_request_set_metainfo(ur, sizeof(struct s_sim_property), &sec_meta);
2572
2573         // AT+CLCK=<fac>,<mode>,<password>
2574         cmd_str = g_strdup_printf("AT+CLCK=\"%s\", %d, %s%s", fac, mode, req_data->password,"\r");
2575         dbg("new pending(enable_facility), %s", cmd_str);
2576
2577         pending = tcore_pending_new(o, ID_RESERVED_AT);
2578         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
2579         tcore_pending_set_timeout(pending, 0);
2580         tcore_pending_set_priority(pending, TCORE_PENDING_PRIORITY_DEFAULT);
2581         tcore_pending_set_response_callback(pending, on_response_disable_facility, NULL);
2582         tcore_pending_link_user_request(pending, ur);
2583
2584         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
2585
2586         tcore_hal_send_request(h, pending);
2587
2588         free(cmd_str);
2589
2590         return TCORE_RETURN_SUCCESS;
2591 }
2592
2593 static TReturn s_read_file(CoreObject *o, UserRequest *ur)
2594 {
2595         TReturn api_ret = TCORE_RETURN_SUCCESS;
2596         enum tcore_request_command command;
2597         command = tcore_user_request_get_command(ur);
2598
2599         dbg("enter");
2600
2601         switch (command) {
2602                 case TREQ_SIM_GET_ECC:
2603                         api_ret = _get_file_info(o, ur, SIM_EF_ECC);
2604                         break;
2605
2606                 case TREQ_SIM_GET_LANGUAGE:
2607                         if (tcore_sim_get_type(o) == SIM_TYPE_GSM)
2608                                 api_ret = _get_file_info(o, ur, SIM_EF_ELP);
2609                         else if (tcore_sim_get_type(o) == SIM_TYPE_USIM)
2610                                 api_ret = _get_file_info(o, ur, SIM_EF_LP);
2611                         else
2612                                 api_ret = TCORE_RETURN_ENOSYS;
2613                         break;
2614
2615                 case TREQ_SIM_GET_ICCID:
2616                         api_ret = _get_file_info(o, ur, SIM_EF_ICCID);
2617                         break;
2618
2619                 case TREQ_SIM_GET_MAILBOX:
2620                         if(tcore_sim_get_cphs_status(o))
2621                                 api_ret = _get_file_info(o, ur, SIM_EF_CPHS_MAILBOX_NUMBERS);
2622                         else
2623                                 api_ret = _get_file_info(o, ur, SIM_EF_MBDN);
2624                         break;
2625
2626                 case TREQ_SIM_GET_CALLFORWARDING:
2627                         if(tcore_sim_get_cphs_status(o))
2628                                 api_ret = _get_file_info(o, ur, SIM_EF_CPHS_CALL_FORWARD_FLAGS);
2629                         else
2630                                 api_ret = _get_file_info(o, ur, SIM_EF_USIM_CFIS);
2631                         break;
2632
2633                 case TREQ_SIM_GET_MESSAGEWAITING:
2634                         if(tcore_sim_get_cphs_status(o))
2635                                 api_ret = _get_file_info(o, ur, SIM_EF_CPHS_VOICE_MSG_WAITING);
2636                         else
2637                                 api_ret = _get_file_info(o, ur, SIM_EF_USIM_MWIS);
2638                         break;
2639
2640                 case TREQ_SIM_GET_CPHS_INFO:
2641                         if(tcore_sim_get_cphs_status(o))
2642                                 api_ret = _get_file_info(o, ur, SIM_EF_CPHS_CPHS_INFO);
2643                         else
2644                                 api_ret = TCORE_RETURN_ENOSYS;
2645                         break;
2646
2647                 case TREQ_SIM_GET_MSISDN:
2648                         api_ret = _get_file_info(o, ur, SIM_EF_MSISDN);
2649                         break;
2650
2651                 case TREQ_SIM_GET_SPN:
2652                         dbg("enter case SPN");
2653                         api_ret = _get_file_info(o, ur, SIM_EF_SPN);
2654                         break;
2655
2656                 case TREQ_SIM_GET_SPDI:
2657                         api_ret = _get_file_info(o, ur, SIM_EF_SPDI);
2658                         break;
2659
2660                 case TREQ_SIM_GET_OPL:
2661                         api_ret = _get_file_info(o, ur, SIM_EF_OPL);
2662                         break;
2663
2664                 case TREQ_SIM_GET_PNN:
2665                         api_ret = _get_file_info(o, ur, SIM_EF_PNN);
2666                         break;
2667
2668                 case TREQ_SIM_GET_CPHS_NETNAME:
2669                         api_ret = TCORE_RETURN_ENOSYS;
2670                         break;
2671
2672                 case TREQ_SIM_GET_OPLMNWACT:
2673                         api_ret = _get_file_info(o, ur, SIM_EF_OPLMN_ACT);
2674                         break;
2675
2676                 default:
2677                         dbg("error - not handled read treq command[%d]", command);
2678                         api_ret = TCORE_RETURN_EINVAL;
2679                         break;
2680         }
2681         return api_ret;
2682 }
2683
2684 static TReturn s_update_file(CoreObject *o, UserRequest *ur)
2685 {
2686         TReturn api_ret = TCORE_RETURN_ENOSYS;
2687         return api_ret;
2688 }
2689
2690 static TReturn s_transmit_apdu(CoreObject *o, UserRequest *ur)
2691 {
2692         dbg("need to be implemented to use ATCMD");
2693
2694         return TCORE_RETURN_SUCCESS;
2695 }
2696
2697 static TReturn s_get_atr(CoreObject *o, UserRequest *ur)
2698 {
2699         dbg("need to be implemented to use ATCMD");
2700
2701         return TCORE_RETURN_SUCCESS;
2702 }
2703
2704 static TReturn s_req_authentication(CoreObject *o, UserRequest *ur)
2705 {
2706         return TCORE_RETURN_SUCCESS;
2707 }
2708
2709 static struct tcore_sim_operations sim_ops =
2710 {
2711                 .verify_pins = s_verify_pins,
2712                 .verify_puks = s_verify_puks,
2713                 .change_pins = s_change_pins,
2714                 .get_facility_status = s_get_facility_status,
2715                 .enable_facility = s_enable_facility,
2716                 .disable_facility = s_disable_facility,
2717                 .read_file = s_read_file,
2718                 .update_file = s_update_file,
2719                 .transmit_apdu = s_transmit_apdu,
2720                 .get_atr = s_get_atr,
2721                 .req_authentication = s_req_authentication
2722 };
2723
2724 gboolean s_sim_init(TcorePlugin *p, TcoreHal *h)
2725 {
2726         CoreObject *o;
2727         struct s_sim_property *sp = NULL;
2728
2729         o = tcore_sim_new(p, "sim", &sim_ops, h);
2730         if (!o)
2731                 return FALSE;
2732
2733         sp = calloc(sizeof(struct s_sim_property),1);
2734         if (!sp)
2735                 return FALSE;
2736
2737         sp->first_recv_status = SIM_STATUS_UNKNOWN;
2738         tcore_sim_link_userdata(o, sp);
2739
2740         tcore_object_add_callback(o, EVENT_SIM_PIN_STATUS, on_event_pin_status, NULL);
2741         return TRUE;
2742 }
2743
2744
2745 void s_sim_exit(TcorePlugin *p)
2746 {
2747         CoreObject *o;
2748
2749         o = tcore_plugin_ref_core_object(p, "sim");
2750         if (!o)
2751                 return;
2752         tcore_sim_free(o);
2753 }