Revert manifest to default one
[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_FILE_NOT_FOUND; // 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_FILE_NOT_FOUND; // 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         file_meta = (struct s_sim_property *)tcore_user_request_ref_metainfo(ur,&info_len);
868         metainfo = &(file_meta->metainfo);
869
870         dbg("file_meta->type[%d]", file_meta->metainfo.type);
871         dbg("metainfo->type[%d]", metainfo->type);
872
873         if((metainfo->type == SINGLELINE)||
874                 (metainfo->type == MULTILINE))
875         {
876                 //cp rsp prefix
877                 s_responsePrefix = strdup(metainfo->responsePrefix);
878                 dbg("duplicating responsePrefix : %s\n", s_responsePrefix);
879         }
880         else
881         {
882                 s_responsePrefix = NULL;
883         }
884
885         //set atcmd type into s_type
886         s_type = metainfo->type;
887
888         if (result == FALSE) {
889                 /* Fail */
890                 dbg("SEND FAIL");
891         }
892         else {
893                 dbg("SEND OK");
894         }
895 }
896
897 static void _response_get_sim_type(TcorePending *p, int data_len, const void *data, void *user_data)
898 {
899         struct s_sim_property *sp = NULL;
900         CoreObject *co_sim = NULL;
901         enum tel_sim_type sim_type = SIM_TYPE_UNKNOWN;
902         char* line=NULL;
903         int ret = 0;
904
905         if(sp_response->success > 0)
906         {
907                 line = sp_response->p_intermediates->line;
908
909                 ret = at_tok_start(&line);
910                 if (ret < 0)
911                         AT_TOK_ERROR(line);
912
913                 ret = at_tok_nextint(&line,(int *)&sim_type);
914                 if (ret < 0)
915                         AT_TOK_ERROR(line);
916         }
917         else
918         {
919                 sim_type = SIM_TYPE_UNKNOWN;
920         }
921
922         dbg("resp sim type[%d]", sim_type);
923
924         ReleaseResponse();
925
926         co_sim = tcore_pending_ref_core_object(p);
927         tcore_sim_set_type(co_sim, sim_type);
928         sp = tcore_sim_ref_userdata(co_sim);
929         _sim_status_update(co_sim, sp->first_recv_status);
930 }
931
932 static void _response_get_file_info(TcorePending *p, int data_len, const void *data, void *user_data)
933 {
934         CoreObject *co_sim = NULL;
935         UserRequest *ur = NULL;
936         struct s_sim_property *file_meta = NULL;
937         enum tel_sim_access_result rt;
938
939         char *line = NULL;
940         int ret = 0;
941         int sw1 = 0;
942         int sw2 = 0;
943         //char *hexData;
944         //char *recordData;
945
946         co_sim = tcore_pending_ref_core_object(p);
947         if(!co_sim){
948                 dbg("error -  core object is null");
949                 return;
950         }
951         ur = tcore_pending_ref_user_request(p);
952         file_meta = (struct s_sim_property*)tcore_user_request_ref_metainfo(ur, NULL);
953
954         if(sp_response->success > 0)
955         {
956                 line = sp_response->p_intermediates->line;
957                 ret = at_tok_start(&line);
958                 if (ret < 0)
959                         AT_TOK_ERROR(line);
960
961                 ret = at_tok_nextint(&line,&sw1);
962                 if (ret < 0)
963                         AT_TOK_ERROR(line);
964                 ret = at_tok_nextint(&line,&sw2);
965                 if (ret < 0)
966                         AT_TOK_ERROR(line);
967
968                 /*1. SIM access success case*/
969                 if ((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
970                         unsigned char tag_len = 0; /*   1 or 2 bytes ??? */
971                         unsigned short record_len = 0;
972                         char num_of_records = 0;
973                         unsigned char file_id_len = 0;
974                         unsigned short file_id = 0;
975                         unsigned short file_size = 0;
976                         unsigned short file_type = 0;
977                         unsigned short arr_file_id = 0;
978                         int arr_file_id_rec_num = 0;
979
980                         /*      handling only last 3 bits */
981                         unsigned char file_type_tag = 0x07;
982                         unsigned char *ptr_data;
983
984                         char *hexData;
985                         char *recordData;
986                         ret = at_tok_nextstr(&line,&hexData);
987                         if (ret < 0)
988                                 AT_TOK_ERROR(line);
989
990                         recordData = util_hexStringToBytes(hexData);
991                         util_hex_dump("    ", strlen(hexData)/2, recordData);
992
993                         ptr_data = (unsigned char *)recordData;
994                         if (tcore_sim_get_type(co_sim) == SIM_TYPE_USIM) {
995                                 /*
996                                  ETSI TS 102 221 v7.9.0
997                                  - Response Data
998                                  '62'   FCP template tag
999                                  - Response for an EF
1000                                  '82'   M       File Descriptor
1001                                  '83'   M       File Identifier
1002                                  'A5'   O       Proprietary information
1003                                  '8A'   M       Life Cycle Status Integer
1004                                  '8B', '8C' or 'AB'     C1      Security attributes
1005                                  '80'   M       File size
1006                                  '81'   O       Total file size
1007                                  '88'   O       Short File Identifier (SFI)
1008                                  */
1009
1010                                 /* rsim.res_len  has complete data length received  */
1011
1012                                 /* FCP template tag - File Control Parameters tag*/
1013                                 if (*ptr_data == 0x62) {
1014                                         /* parse complete FCP tag*/
1015                                         /* increment to next byte */
1016                                         ptr_data++;
1017                                         tag_len = *ptr_data++;
1018                                         /* FCP file descriptor - file type, accessibility, DF, ADF etc*/
1019                                         if (*ptr_data == 0x82) {
1020                                                 /* increment to next byte */
1021                                                 ptr_data++;
1022                                                 /*2 or 5 value*/
1023                                                 ptr_data++;
1024                                                 /*      unsigned char file_desc_len = *ptr_data++;*/
1025                                                 /*      dbg("file descriptor length: [%d]", file_desc_len);*/
1026                                                 /* TBD:  currently capture only file type : ignore sharable, non sharable, working, internal etc*/
1027                                                 /* consider only last 3 bits*/
1028                                                 file_type_tag = file_type_tag & (*ptr_data);
1029
1030                                                 switch (file_type_tag) {
1031                                                         /* increment to next byte */
1032                                                         ptr_data++;
1033                                                         case 0x1:
1034                                                                 dbg("Getting FileType: [Transparent file type]");
1035                                                                 /* increment to next byte */
1036                                                                 ptr_data++;
1037                                                                 file_type = SIM_FTYPE_TRANSPARENT;
1038                                                                 /*      data coding byte - value 21 */
1039                                                                 ptr_data++;
1040                                                                 break;
1041
1042                                                         case 0x2:
1043                                                                 dbg("Getting FileType: [Linear fixed file type]");
1044                                                                 /* increment to next byte */
1045                                                                 ptr_data++;
1046                                                                 /*      data coding byte - value 21 */
1047                                                                 ptr_data++;
1048                                                                 /*      2bytes */
1049                                                                 memcpy(&record_len, ptr_data, 2);
1050                                                                 /* swap bytes */
1051                                                                 SWAPBYTES16(record_len);
1052                                                                 ptr_data = ptr_data + 2;
1053                                                                 num_of_records = *ptr_data++;
1054                                                                 /* Data lossy conversation from enum (int) to unsigned char */
1055                                                                 file_type = SIM_FTYPE_LINEAR_FIXED;
1056                                                                 break;
1057
1058                                                         case 0x6:
1059                                                                 dbg(" Cyclic fixed file type");
1060                                                                 /* increment to next byte */
1061                                                                 ptr_data++;
1062                                                                 /*      data coding byte - value 21 */
1063                                                                 ptr_data++;
1064                                                                 /*      2bytes */
1065                                                                 memcpy(&record_len, ptr_data, 2);
1066                                                                 /* swap bytes  */
1067                                                                 SWAPBYTES16(record_len);
1068                                                                 ptr_data = ptr_data + 2;
1069                                                                 num_of_records = *ptr_data++;
1070                                                                 file_type = SIM_FTYPE_CYCLIC;
1071                                                                 break;
1072
1073                                                 default:
1074                                                         dbg("not handled file type [0x%x]", *ptr_data);
1075                                                         break;
1076                                                 }
1077                                         } else {
1078                                                 dbg("INVALID FCP received - DEbug!");
1079                                                 return;
1080                                         }
1081
1082                                         /*File identifier - file id?? */ // 0x84,0x85,0x86 etc are currently ignored and not handled
1083                                         if (*ptr_data == 0x83) {
1084                                                 /* increment to next byte */
1085                                                 ptr_data++;
1086                                                 file_id_len = *ptr_data++;
1087                                                 memcpy(&file_id, ptr_data, file_id_len);
1088                                                 /* swap bytes    */
1089                                                 SWAPBYTES16(file_id);
1090                                                 ptr_data = ptr_data + 2;
1091                                                 dbg("Getting FileID=[0x%x]", file_id);
1092                                         } else {
1093                                                 dbg("INVALID FCP received - DEbug!");
1094                                                 free(recordData);
1095                                                 ReleaseResponse();
1096                                                 return;
1097                                         }
1098
1099                                         /*      proprietary information  */
1100                                         if (*ptr_data == 0xA5) {
1101                                                 unsigned short prop_len;
1102                                                 /* increment to next byte */
1103                                                 ptr_data++;
1104                                                 /* length */
1105                                                 prop_len = *ptr_data;
1106                                                 /* skip data */
1107                                                 ptr_data = ptr_data + prop_len + 1;
1108                                         } else {
1109                                                 dbg("INVALID FCP received - DEbug!");
1110                                         }
1111
1112                                         /* life cycle status integer [8A][length:0x01][status]*/
1113                                         /*
1114                                          status info b8~b1
1115                                          00000000 : No information given
1116                                          00000001 : creation state
1117                                          00000011 : initialization state
1118                                          000001-1 : operation state -activated
1119                                          000001-0 : operation state -deactivated
1120                                          000011-- : Termination state
1121                                          b8~b5 !=0, b4~b1=X : Proprietary
1122                                          Any other value : RFU
1123                                          */
1124                                         if (*ptr_data == 0x8A) {
1125                                                 /* increment to next byte */
1126                                                 ptr_data++;
1127                                                 /* length - value 1 */
1128                                                 ptr_data++;
1129
1130                                                 switch (*ptr_data) {
1131                                                         case 0x04:
1132                                                         case 0x06:
1133                                                                 dbg("<IPC_RX> operation state -deactivated");
1134                                                                 ptr_data++;
1135                                                                 break;
1136                                                         case 0x05:
1137                                                         case 0x07:
1138                                                                 dbg("<IPC_RX> operation state -activated");
1139                                                                 ptr_data++;
1140                                                                 break;
1141                                                         default:
1142                                                                 dbg("<IPC_RX> DEBUG! LIFE CYCLE STATUS =[0x%x]",*ptr_data);
1143                                                                 ptr_data++;
1144                                                                 break;
1145                                                 }
1146                                         }
1147
1148                                         /* related to security attributes : currently not handled*/
1149                                         if (*ptr_data == 0x86 || *ptr_data == 0x8B || *ptr_data == 0x8C || *ptr_data == 0xAB) {
1150                                                 /* increment to next byte */
1151                                                 ptr_data++;
1152                                                 /* if tag length is 3 */
1153                                                 if (*ptr_data == 0x03) {
1154                                                         /* increment to next byte */
1155                                                         ptr_data++;
1156                                                         /* EFARR file id */
1157                                                         memcpy(&arr_file_id, ptr_data, 2);
1158                                                         /* swap byes */
1159                                                         SWAPBYTES16(arr_file_id);
1160                                                         ptr_data = ptr_data + 2;
1161                                                         arr_file_id_rec_num = *ptr_data++;
1162                                                 } else {
1163                                                         /* if tag length is not 3 */
1164                                                         /* ignoring bytes       */
1165                                                         //      ptr_data = ptr_data + 4;
1166                                                         dbg("Useless security attributes, so jump to next tag");
1167                                                         ptr_data = ptr_data + (*ptr_data + 1);
1168                                                 }
1169                                         } else {
1170                                                 dbg("INVALID FCP received[0x%x] - DEbug!", *ptr_data);
1171                                                 free(recordData);
1172                                                 ReleaseResponse();
1173                                                 return;
1174                                         }
1175
1176                                         dbg("Current ptr_data value is [%x]", *ptr_data);
1177
1178                                         /* file size excluding structural info*/
1179                                         if (*ptr_data == 0x80) {
1180                                                 /* for EF file size is body of file and for Linear or cyclic it is
1181                                                  * number of recXsizeof(one record)
1182                                                  */
1183                                                 /* increment to next byte */
1184                                                 ptr_data++;
1185                                                 /* length is 1 byte - value is 2 bytes or more */
1186                                                 ptr_data++;
1187                                                 memcpy(&file_size, ptr_data, 2);
1188                                                 /* swap bytes */
1189                                                 SWAPBYTES16(file_size);
1190                                                 ptr_data = ptr_data + 2;
1191                                         } else {
1192                                                 dbg("INVALID FCP received - DEbug!");
1193                                                 free(recordData);
1194                                                 ReleaseResponse();
1195                                                 return;
1196                                         }
1197
1198                                         /* total file size including structural info*/
1199                                         if (*ptr_data == 0x81) {
1200                                                 int len;
1201                                                 /* increment to next byte */
1202                                                 ptr_data++;
1203                                                 /* length */
1204                                                 len = *ptr_data;
1205                                                 /* ignored bytes */
1206                                                 ptr_data = ptr_data + 3;
1207                                         } else {
1208                                                 dbg("INVALID FCP received - DEbug!");
1209                                                 /* 0x81 is optional tag?? check out! so do not return -1 from here! */
1210                                                 /* return -1; */
1211                                         }
1212                                         /*short file identifier ignored*/
1213                                         if (*ptr_data == 0x88) {
1214                                                 dbg("0x88: Do Nothing");
1215                                                 /*DO NOTHING*/
1216                                         }
1217                                 } else {
1218                                         dbg("INVALID FCP received - DEbug!");
1219                                         free(recordData);
1220                                         ReleaseResponse();
1221                                         return;
1222                                 }
1223                         } else if (tcore_sim_get_type(co_sim) == SIM_TYPE_GSM) {
1224                                 unsigned char gsm_specific_file_data_len = 0;
1225                                 /*      ignore RFU byte1 and byte2 */
1226                                 ptr_data++;
1227                                 ptr_data++;
1228                                 /*      file size */
1229                                 //file_size = p_info->response_len;
1230                                 memcpy(&file_size, ptr_data, 2);
1231                                 /* swap bytes */
1232                                 SWAPBYTES16(file_size);
1233                                 /*      parsed file size */
1234                                 ptr_data = ptr_data + 2;
1235                                 /*  file id  */
1236                                 memcpy(&file_id, ptr_data, 2);
1237                                 SWAPBYTES16(file_id);
1238                                 dbg(" FILE id --> [%x]", file_id);
1239                                 ptr_data = ptr_data + 2;
1240                                 /* save file type - transparent, linear fixed or cyclic */
1241                                 file_type_tag = (*(ptr_data + 7));
1242
1243                                 switch (*ptr_data) {
1244                                         case 0x0:
1245                                                 /* RFU file type */
1246                                                 dbg(" RFU file type- not handled - Debug!");
1247                                                 break;
1248                                         case 0x1:
1249                                                 /* MF file type */
1250                                                 dbg(" MF file type - not handled - Debug!");
1251                                                 break;
1252                                         case 0x2:
1253                                                 /* DF file type */
1254                                                 dbg(" DF file type - not handled - Debug!");
1255                                                 break;
1256                                         case 0x4:
1257                                                 /* EF file type */
1258                                                 dbg(" EF file type [%d] ", file_type_tag);
1259                                                 /*      increment to next byte */
1260                                                 ptr_data++;
1261
1262                                                 if (file_type_tag == 0x00 || file_type_tag == 0x01) {
1263                                                         /* increament to next byte as this byte is RFU */
1264                                                         ptr_data++;
1265                                                         file_type =
1266                                                                         (file_type_tag == 0x00) ? SIM_FTYPE_TRANSPARENT : SIM_FTYPE_LINEAR_FIXED;
1267                                                 } else {
1268                                                         /* increment to next byte */
1269                                                         ptr_data++;
1270                                                         /*      For a cyclic EF all bits except bit 7 are RFU; b7=1 indicates that */
1271                                                         /* the INCREASE command is allowed on the selected cyclic file. */
1272                                                         file_type = SIM_FTYPE_CYCLIC;
1273                                                 }
1274                                                 /* bytes 9 to 11 give SIM file access conditions */
1275                                                 ptr_data++;
1276                                                 /* byte 10 has one nibble that is RF U and another for INCREASE which is not used currently */
1277                                                 ptr_data++;
1278                                                 /* byte 11 is invalidate and rehabilate nibbles */
1279                                                 ptr_data++;
1280                                                 /* byte 12 - file status */
1281                                                 ptr_data++;
1282                                                 /* byte 13 - GSM specific data */
1283                                                 gsm_specific_file_data_len = *ptr_data;
1284                                                 ptr_data++;
1285                                                 /*      byte 14 - structure of EF - transparent or linear or cyclic , already saved above */
1286                                                 ptr_data++;
1287                                                 /* byte 15 - length of record for linear and cyclic , for transparent it is set to 0x00. */
1288                                                 record_len = *ptr_data;
1289                                                 dbg("record length[%d], file size[%d]", record_len, file_size);
1290
1291                                                 if (record_len != 0)
1292                                                         num_of_records = (file_size / record_len);
1293
1294                                                 dbg("Number of records [%d]", num_of_records);
1295                                                 break;
1296
1297                                         default:
1298                                                 dbg(" not handled file type");
1299                                                 break;
1300                                 }
1301                         }
1302                         else
1303                         {
1304                                 dbg(" Card Type - UNKNOWN  [%d]", tcore_sim_get_type(co_sim));
1305                         }
1306
1307                         dbg("req ef[0x%x] resp ef[0x%x] size[%ld] Type[0x%x] NumOfRecords[%ld] RecordLen[%ld]",
1308                                         file_meta->file_id, file_id, file_size, file_type, num_of_records, record_len);
1309
1310                         file_meta->file_type = file_type;
1311                         file_meta->data_size = file_size;
1312                         file_meta->rec_length = record_len;
1313                         file_meta->rec_count = num_of_records;
1314                         file_meta->current_index = 0; //reset for new record type EF
1315                         rt = SIM_ACCESS_SUCCESS;
1316                         free(recordData);
1317                 }
1318                 else
1319                 {
1320                         /*2. SIM access fail case*/
1321                         dbg("error to get ef[0x%x]", file_meta->file_id);
1322                         rt = _decode_status_word(sw1, sw2);
1323                 }
1324
1325                 ReleaseResponse();
1326
1327                 ur = tcore_user_request_ref(ur);
1328                 _next_from_get_file_info(co_sim, ur, file_meta->file_id, rt);
1329         }
1330         else
1331         {
1332                 ReleaseResponse();
1333                 dbg("error to get ef[0x%x]", file_meta->file_id);
1334                 rt = SIM_ACCESS_FAILED;;
1335
1336                 ur = tcore_user_request_ref(ur);
1337                 _next_from_get_file_info(co_sim, ur, file_meta->file_id, rt);
1338         }
1339 }
1340
1341 static void _response_get_file_data(TcorePending *p, int data_len, const void *data, void *user_data)
1342 {
1343         UserRequest *ur = NULL;
1344         CoreObject *co_sim = NULL;
1345         enum tel_sim_access_result rt;
1346         struct tel_sim_imsi imsi;
1347         struct s_sim_property *file_meta = NULL;
1348         gboolean dr = FALSE;
1349
1350         char *line = NULL;
1351         int ret = 0;
1352         int sw1 = 0;
1353         int sw2 = 0;
1354
1355         dbg("[SIM_READ_BINARY] or [SIM_READ_RECORD]");
1356
1357         dbg("sizeof struct tresp_sim_read = [%d]", sizeof(struct tresp_sim_read));
1358
1359         co_sim = tcore_pending_ref_core_object(p);
1360         if(!co_sim){
1361                 dbg("error -  core object is null");
1362                 return;
1363         }
1364         ur = tcore_pending_ref_user_request(p);
1365         file_meta = (struct s_sim_property*)tcore_user_request_ref_metainfo(ur, NULL);
1366
1367         if(sp_response->success > 0)
1368         {
1369                 line = sp_response->p_intermediates->line;
1370
1371                 ret = at_tok_start(&line);
1372                 if (ret < 0)
1373                         AT_TOK_ERROR(line);
1374
1375                 ret = at_tok_nextint(&line,&sw1);
1376                 if (ret < 0)
1377                         AT_TOK_ERROR(line);
1378                 ret = at_tok_nextint(&line,&sw2);
1379                 if (ret < 0)
1380                         AT_TOK_ERROR(line);
1381
1382                 if((sw1 == 0x90 && sw2 == 0x00) || sw1 == 0x91) {
1383                         char *hexStr;
1384                         char *fileData;
1385
1386                         ret = at_tok_nextstr(&line,&hexStr);
1387                         if (ret < 0)
1388                                 AT_TOK_ERROR(line);
1389
1390                         fileData = util_hexStringToBytes(hexStr);
1391                         util_hex_dump("    ", strlen(hexStr)/2, fileData);
1392
1393                         rt = SIM_ACCESS_SUCCESS;
1394                         file_meta->files.result = rt;
1395
1396                         switch (file_meta->file_id)
1397                         {
1398                                         case SIM_EF_IMSI:
1399                                                 dr = tcore_sim_decode_imsi(&imsi, (unsigned char *)fileData, strlen(fileData));
1400                                                 if (dr == FALSE) {
1401                                                         dbg("imsi decoding failed");
1402                                                 } else {
1403                                                         _sim_check_identity(co_sim,&imsi);
1404                                                         tcore_sim_set_imsi(co_sim,&imsi);
1405                                                 }
1406                                                 break;
1407
1408                                         case SIM_EF_ICCID:
1409                                                 dr = tcore_sim_decode_iccid(&file_meta->files.data.iccid, (unsigned char *)fileData, strlen(fileData));
1410                                                 break;
1411
1412                                         case SIM_EF_ELP:/*  2G EF -  2 bytes decoding*/
1413                                         case SIM_EF_USIM_LI: /* 3G EF - 2 bytes decoding*/
1414                                         case SIM_EF_USIM_PL:/*  3G EF - same as EFELP, so 2  byte decoding*/
1415                                         case SIM_EF_LP:/*  1 byte encoding*/
1416                                                 if (tcore_sim_get_type(co_sim) == SIM_TYPE_GSM && file_meta->file_id == SIM_EF_LP) {
1417                                                          /*2G LP(0x6F05) has 1 byte for each language*/
1418                                                         dr = tcore_sim_decode_lp(&file_meta->files.data.language, (unsigned char *)fileData, strlen(fileData));
1419                                                 } else {
1420                                                          /*3G LI(0x6F05)/PL(0x2F05), 2G ELP(0x2F05) has 2 bytes for each language*/
1421                                                         dr = tcore_sim_decode_li(file_meta->file_id, &file_meta->files.data.language, (unsigned char *)fileData, strlen(fileData));
1422                                                 }
1423                                                 break;
1424
1425                                         case SIM_EF_SPN:
1426                                                 dr = tcore_sim_decode_spn(&file_meta->files.data.spn, (unsigned char *)fileData, strlen(fileData));
1427                                                 break;
1428
1429                                         case SIM_EF_SPDI:
1430                                                 dr = tcore_sim_decode_spdi(&file_meta->files.data.spdi, (unsigned char *)fileData, strlen(fileData));
1431                                                 break;
1432
1433                                         case SIM_EF_SST:
1434 /*                                      if(tcore_sim_get_type(o) == SIM_TYPE_GSM)
1435                                                         dr = tcore_sim_decode_sst(&file_meta->files.data , fileData, response_len);
1436                                                 else if(tcore_sim_get_type(o) == SIM_TYPE_USIM)
1437                                                         dr = tcore_sim_decode_ust(&po->st_u.ust, fileData, response_len);
1438                                                 else {
1439                                                         dbg("err not handled tcore_sim_get_type(o)[%d] in here",tcore_sim_get_type(o));
1440                                                 }*/
1441                                                 break;
1442
1443                                         case SIM_EF_MBDN:
1444 /*                                      dr = tcore_sim_decode_xdn(&file_meta->files.data.mailbox, fileData, strlen(fileData));*/
1445                                                 break;
1446
1447                                         case SIM_EF_ECC:
1448                                                 if(tcore_sim_get_type(co_sim) == SIM_TYPE_GSM) {
1449                                                         dr = tcore_sim_decode_ecc(&file_meta->files.data.ecc, (unsigned char *)fileData, strlen(fileData));
1450                                                 } else if(tcore_sim_get_type(co_sim) == SIM_TYPE_USIM){
1451                                                         dr = tcore_sim_decode_uecc(&file_meta->files.data.ecc.ecc[file_meta->current_index-1], (unsigned char *)fileData, strlen(fileData));
1452                                                 } else {
1453                                                         dbg("err not handled tcore_sim_get_type(o)[%d] in here",tcore_sim_get_type(co_sim));
1454                                                 }
1455                                                 break;
1456
1457                                         case SIM_EF_USIM_MBI:
1458 /*                                      dr = tcore_sim_decode_mbi(&po->mb_u.mailbox.mbi, fileData, strlen(fileData));*/
1459                                                 break;
1460
1461                                         case SIM_EF_OPL:
1462                                                 dr = tcore_sim_decode_opl(&file_meta->files.data.opl.opl[file_meta->current_index-1], (unsigned char *)fileData, strlen(fileData));
1463                                                 break;
1464
1465                                         case SIM_EF_PNN:
1466                                                 dr = tcore_sim_decode_pnn(&file_meta->files.data.pnn.pnn[file_meta->current_index-1], (unsigned char *)fileData, strlen(fileData));
1467                                                 break;
1468
1469                                         case SIM_EF_OPLMN_ACT:
1470                                                 dr = tcore_sim_decode_oplmnwact(&file_meta->files.data.opwa, (unsigned char *)fileData, strlen(fileData));
1471                                                 break;
1472
1473                                         case SIM_EF_CPHS_CALL_FORWARD_FLAGS:
1474                                                 dr = tcore_sim_decode_cff(&file_meta->files.data.cf, (unsigned char *)fileData, strlen(fileData));
1475                                                 break;
1476
1477                                         case SIM_EF_CPHS_CUSTOMER_SERVICE_PROFILE:
1478 /*                                      dr = tcore_sim_decode_csp(&po->p_cphs->csp, fileData, strlen(fileData));*/
1479                                                 break;
1480
1481                                         case SIM_EF_CPHS_VOICE_MSG_WAITING:
1482                                                 dr = tcore_sim_decode_vmwf(&file_meta->files.data.mw.mw_data_u.cphs_mw, (unsigned char *)fileData, strlen(fileData));
1483                                                 break;
1484
1485                                         case SIM_EF_CPHS_MAILBOX_NUMBERS:
1486 /*                                      dbg("[CPHS]decoding SIM_EF_CPHS_MAILBOX_NUMBERS");
1487                                                 if (file_meta->current_index == 1)
1488                                                         dr = tcore_sim_decode_xdn(&po->mb_u.mbn.voice_line1, fileData, strlen(fileData));
1489                                                 else if (file_meta->current_index == 2)
1490                                                         dr = tcore_sim_decode_xdn(&po->mb_u.mbn.voice_line2, fileData, strlen(fileData));
1491                                                 else if (file_meta->current_index == 3)
1492                                                         dr = tcore_sim_decode_xdn(&po->mb_u.mbn.video, fileData, strlen(fileData));
1493                                                 else if (file_meta->current_index == 4)
1494                                                         dr = tcore_sim_decode_xdn(&po->mb_u.mbn.fax, fileData, strlen(fileData));
1495                                                 else {
1496                                                         dbg("[CPHS]wrong EF record index[%d] in here", file_meta->current_index);
1497                                                 }*/
1498                                                 break;
1499
1500                                         case SIM_EF_USIM_MWIS:
1501                                                 dr = tcore_sim_decode_mwis(&file_meta->files.data.mw.mw_data_u.mw, (unsigned char *)fileData, strlen(fileData));
1502                                                 break;
1503
1504                                         case SIM_EF_USIM_CFIS:
1505                                                 dr = tcore_sim_decode_cfis(&file_meta->files.data.cf, (unsigned char *)fileData, strlen(fileData));
1506                                                 break;
1507
1508                                         case SIM_EF_CPHS_SERVICE_STRING_TABLE:
1509                                                 dbg(" not handled -SIM_EF_CPHS_SERVICE_STRING_TABLE ");
1510                                                 break;
1511
1512                                         case SIM_EF_CPHS_OPERATOR_NAME_STRING:
1513                                                 dr = tcore_sim_decode_ons((unsigned char*)&file_meta->files.data.cphs_net.full_name, (unsigned char *)fileData, strlen(fileData));
1514                                                 break;
1515
1516                                         case SIM_EF_CPHS_DYNAMICFLAGS:
1517 /*                                      dr = tcore_sim_decode_dynamic_flag(&po->p_cphs->dflagsinfo, fileData, strlen(fileData));*/
1518                                                 break;
1519
1520                                         case SIM_EF_CPHS_DYNAMIC2FLAG:
1521 /*                                      dr = tcore_sim_decode_dynamic2_flag(&po->p_cphs->d2flagsinfo, fileData, strlen(fileData));*/
1522                                                 break;
1523
1524                                         case SIM_EF_CPHS_CPHS_INFO:
1525                                                 dr = tcore_sim_decode_cphs_info(&file_meta->files.data.cphs, (unsigned char *)fileData, strlen(fileData));
1526                                                 break;
1527
1528                                         case SIM_EF_CPHS_OPERATOR_NAME_SHORT_FORM_STRING:
1529                                                 dr = tcore_sim_decode_short_ons((unsigned char*)&file_meta->files.data.cphs_net.short_name, (unsigned char *)fileData,  strlen(fileData));
1530                                                 break;
1531
1532                                         case SIM_EF_CPHS_INFORMATION_NUMBERS:
1533 /*                                      dr = tcore_sim_decode_information_number(&po->p_cphs->infn, fileData, strlen(fileData));*/
1534                                                 break;
1535
1536                                         default:
1537                                                 dbg("File Decoding Failed - not handled File[0x%x]", file_meta->file_id);
1538                                                 dr = 0;
1539                                                 break;
1540                                 }
1541
1542                         free(fileData);
1543                 }
1544                 else
1545                 {
1546                         rt =  _decode_status_word(sw1, sw2);
1547                         file_meta->files.result = rt;
1548                 }
1549
1550                 ReleaseResponse();
1551         }
1552         else
1553         {
1554                 ReleaseResponse();
1555                 rt = SIM_ACCESS_FAILED;;
1556                 file_meta->files.result = rt;
1557         }
1558
1559         ur = tcore_user_request_ref(ur);
1560         _next_from_get_file_data(tcore_pending_ref_core_object(p), ur, rt, dr);
1561 }
1562
1563 static gboolean _get_sim_type(CoreObject *o)
1564 {
1565         TcorePlugin *p = NULL;
1566         TcoreHal *h = NULL;
1567         TcorePending *pending = NULL;
1568
1569         char *cmd_str = NULL;
1570         struct s_sim_property file_meta={0,};
1571         TReturn trt = 0;
1572         UserRequest *ur = NULL;
1573
1574         if (!o)
1575                 return FALSE;
1576
1577         p = tcore_object_ref_plugin(o);
1578         h = tcore_object_get_hal(o);
1579
1580         file_meta.metainfo.type = SINGLELINE;
1581         memcpy(file_meta.metainfo.responsePrefix,"%SCCT:",strlen("%SCCT:"));
1582
1583         ur = tcore_user_request_new(NULL, NULL);
1584
1585         trt = tcore_user_request_set_metainfo(ur, sizeof(struct s_sim_property), &file_meta);
1586         dbg("trt[%d]",trt);
1587
1588
1589         // AT+CPIN=<pin>[,<newpin>]
1590         cmd_str = g_strdup("AT%SCCT\r");
1591
1592         pending = tcore_pending_new(o, ID_RESERVED_AT);
1593         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
1594         tcore_pending_set_timeout(pending, 0);
1595         tcore_pending_set_response_callback(pending, _response_get_sim_type, NULL);
1596         tcore_pending_link_user_request(pending, ur);
1597
1598         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
1599
1600         tcore_hal_send_request(h, pending);
1601
1602         free(cmd_str);
1603
1604         return TRUE;
1605 }
1606
1607 static TReturn _get_file_info(CoreObject *o, UserRequest *ur, const enum tel_sim_file_id ef)
1608 {
1609         TcorePlugin *p = NULL;
1610         TcoreHal *h = NULL;
1611         TcorePending *pending = NULL;
1612         //size_t size = 0;
1613         struct s_sim_property file_meta={0,};
1614         TReturn trt = 0;
1615
1616         char *cmd_str = NULL;
1617
1618         if (!o)
1619                 return TCORE_RETURN_EINVAL;
1620
1621         p = tcore_object_ref_plugin(o);
1622         h = tcore_object_get_hal(o);
1623
1624         file_meta.file_id = ef;
1625         file_meta.metainfo.type = SINGLELINE;
1626         memcpy(file_meta.metainfo.responsePrefix,"+CRSM:",strlen("+CRSM:"));
1627
1628         trt = tcore_user_request_set_metainfo(ur, sizeof(struct s_sim_property), &file_meta);
1629         dbg("trt[%d]",trt);
1630
1631         // AT+CRSM=<command>,<fildid>,<p1>,<p2+C29,<p3>
1632         cmd_str = g_strdup_printf("AT+CRSM=192, %d%s", ef, "\r");
1633
1634         dbg("new pending(IPC_SEC_RSIM_ACCESS GET - SELECT EF[0x%x])",ef);
1635         pending = tcore_pending_new(o, ID_RESERVED_AT);
1636         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
1637         tcore_pending_set_timeout(pending, 0);
1638         tcore_pending_set_response_callback(pending, _response_get_file_info, NULL);
1639         tcore_pending_link_user_request(pending, ur);
1640
1641         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
1642
1643         tcore_hal_send_request(h, pending);
1644         free(cmd_str);
1645
1646         return TCORE_RETURN_SUCCESS;
1647 }
1648
1649 static gboolean _get_file_data(CoreObject *o, UserRequest *ur, const enum tel_sim_file_id ef, const int offset, const int length)
1650 {
1651         TcorePlugin *p = NULL;
1652         TcoreHal *h = NULL;
1653         TcorePending *pending = NULL;
1654         //size_t size = 0;
1655
1656         char *cmd_str = NULL;
1657         struct ATReqMetaInfo metainfo;
1658         int info_len =0;
1659         int p1;
1660         int p2;
1661         int p3;
1662
1663         if (!o)
1664                 return FALSE;
1665
1666         dbg("new pending(IPC_SEC_RSIM_ACCESS GET - READ BIN)");
1667
1668         p = tcore_object_ref_plugin(o);
1669         h = tcore_object_get_hal(o);
1670
1671 // offset for reading the TRANSPARENT data
1672         p1 = (unsigned char)(offset & 0xFF00) >> 8;
1673         p2 = (unsigned char)offset & 0x00FF; //offset low
1674         p3 = (unsigned char)length;
1675         dbg("EF[0x%x]", ef);
1676
1677         memset(&metainfo, 0, sizeof(struct ATReqMetaInfo));
1678         metainfo.type = SINGLELINE;
1679         memcpy(metainfo.responsePrefix,"+CRSM:",strlen("+CRSM:"));
1680         info_len = sizeof(struct ATReqMetaInfo);
1681
1682         // AT+CRSM=<command>,<fildid>,<p1>,<p2+C29,<p3>
1683         cmd_str = g_strdup_printf("AT+CRSM=176, %d%s", ef, "\r");
1684
1685         pending = tcore_pending_new(o, ID_RESERVED_AT);
1686         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
1687         tcore_pending_set_timeout(pending, 0);
1688         tcore_pending_set_response_callback(pending, _response_get_file_data, NULL);
1689         tcore_pending_link_user_request(pending, ur);
1690
1691         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
1692
1693         tcore_hal_send_request(h, pending);
1694         free(cmd_str);
1695         return TRUE;
1696 }
1697
1698 static gboolean _get_file_record(CoreObject *o, UserRequest *ur, const enum tel_sim_file_id ef, const int index, const int length)
1699 {
1700         dbg("need to be implemented to use ATCMD");
1701
1702         return TRUE;
1703 }
1704
1705 static gboolean on_event_pin_status(CoreObject *o, const void *event_info, void *user_data)
1706 {
1707         UserRequest *ur;
1708         char *line = (char *) event_info;
1709         //struct tnoti_sim_status noti_data;
1710         struct s_sim_property *sp = NULL;
1711         enum tel_sim_status sim_status = SIM_STATUS_INITIALIZING;
1712         enum s_sim_sec_locktype_e locktype = SEC_LOCK_TYPE_NONE;
1713         enum s_sim_sec_lockkey_e lockkey = SEC_LOCK_KEY_NONE;
1714
1715         dbg("PIN_STATUS NOTI : %s", line);
1716
1717         _convert_SCPIN_noti(line,&locktype, &lockkey);
1718
1719         sp = tcore_sim_ref_userdata(o);
1720
1721
1722         switch (locktype) {
1723                 case SEC_LOCK_TYPE_READY:
1724                         if (lockkey == SEC_LOCK_KEY_UNLOCKED) {
1725                                 sim_status = SIM_STATUS_INITIALIZING;
1726                                 dbg(" Inside PIN disabled at BOOT UP");
1727                         }
1728                         else {
1729                                 dbg(" not handled case p_status->lock_key[%d]", lockkey);
1730                         }
1731                         break;
1732
1733                 case SEC_LOCK_TYPE_PS:
1734                         sim_status = SIM_STATUS_LOCK_REQUIRED;
1735                         dbg( " SIM LOCK required");
1736                         break;
1737
1738                 case SEC_LOCK_TYPE_PF:
1739                         sim_status = SIM_STATUS_CARD_ERROR;
1740                         dbg( "PF required ");
1741                         break;
1742
1743                 case SEC_LOCK_TYPE_SC:
1744                         switch (lockkey) {
1745                                 case SEC_LOCK_KEY_UNLOCKED:
1746                                         break;
1747                                 case SEC_LOCK_KEY_PIN:
1748                                         sim_status = SIM_STATUS_PIN_REQUIRED;
1749                                         dbg( " PIN1 required");
1750                                         break;
1751                                 case SEC_LOCK_KEY_PUK:
1752                                         sim_status = SIM_STATUS_PUK_REQUIRED;
1753                                         dbg( " PUK required");
1754                                         break;
1755                                 case SEC_LOCK_KEY_PERM_BLOCKED:
1756                                         sim_status = SIM_STATUS_CARD_BLOCKED;
1757                                         dbg( " Card permanently blocked");
1758                                         break;
1759                                 default:
1760                                         dbg(" SEC_SIM_LOCK_SC -not handled SEC Lock key ");
1761                                         break;
1762                         }
1763                         break;
1764
1765                 case SEC_LOCK_TYPE_FD:
1766                         dbg(" SEC_LOCK_TYPE_FD -not handled Notification");
1767                         break;
1768
1769                 case SEC_LOCK_TYPE_PN:
1770                         switch (lockkey) {
1771                                 case SEC_LOCK_KEY_PIN:
1772                                         dbg(" ADMIN-NCK required");
1773                                         sim_status = SIM_STATUS_NCK_REQUIRED;
1774                                         break;
1775                                 default:
1776                                         dbg(" SIM_LOCK_PN/PU/PP/PC -not handled SEC Lock key =[%d]",
1777                                                         lockkey);
1778                                         break;
1779                         }
1780                         break;
1781
1782                 case SEC_LOCK_TYPE_PU:
1783                         dbg("Lock Personalization p_status->lock_key =[%d]", lockkey);
1784                         switch (lockkey) {
1785                                 case SEC_LOCK_KEY_PIN:
1786                                         dbg(" ADMIN-NSCK required");
1787                                         sim_status = SIM_STATUS_NSCK_REQUIRED;
1788                                         break;
1789                                 default:
1790                                         dbg(" SIM_LOCK_PN/PU/PP/PC -not handled SEC Lock key =[%d]",
1791                                                         lockkey);
1792                                         break;
1793                         }
1794                         break;
1795
1796                 case SEC_LOCK_TYPE_PP:
1797                         switch (lockkey) {
1798                                 dbg("Lock Personalization p_status->lock_key =[%d]", lockkey);
1799                         case SEC_LOCK_KEY_PIN:
1800                                 dbg(" ADMIN-SPCK required");
1801                                 sim_status = SIM_STATUS_SPCK_REQUIRED;
1802                                 break;
1803                         default:
1804                                 dbg(" SIM_LOCK_PN/PU/PP/PC -not handled SEC Lock key =[%d]",
1805                                                 lockkey);
1806                                 break;
1807                         }
1808                         break;
1809
1810                 case SEC_LOCK_TYPE_PC:
1811                         switch (lockkey) {
1812                                 dbg("Lock Personalization p_status->lock_key =[%d]", lockkey);
1813                         case SEC_LOCK_KEY_PIN:
1814                                 dbg(" ADMIN-CCK required");
1815                                 sim_status = SIM_STATUS_CCK_REQUIRED;
1816                                 break;
1817                         default:
1818                                 dbg(" SIM_LOCK_PN/PU/PP/PC -not handled SEC Lock key =[%d]",
1819                                                 lockkey);
1820                                 break;
1821                         }
1822                         break;
1823
1824                 case SEC_LOCK_TYPE_SC2:
1825                         dbg("SEC_LOCK_TYPE_SC2: NOT Handled - Debug");
1826                         break;
1827
1828                 case SEC_LOCL_TYPE_PUK2:
1829                         dbg("SEC_LOCL_TYPE_PUK2: NOT Handled - Debug");
1830                         break;
1831
1832                 case SEC_LOCK_TYPE_NO_SIM:
1833                         sim_status = SIM_STATUS_CARD_NOT_PRESENT;
1834                         dbg( "NO SIM");
1835                         break;
1836
1837                 case SEC_LOCK_TYPE_UNAVAIL:
1838                 case SEC_SIM_INIT_CRASH: //SMC Lab requirement
1839                         sim_status = SIM_STATUS_CARD_ERROR;
1840                         dbg( "SIM unavailable");
1841                         break;
1842
1843                 case SEC_SIM_INIT_COMPLETED:
1844                         dbg( "[SIM DATA] MODEM SIM INIT COMPLETED");
1845                         sim_status = SIM_STATUS_INIT_COMPLETED;
1846                         break;
1847
1848                 case SEC_PB_INIT_COMPLETED:
1849                         dbg("[SIM DATA] MODEM SIM PB INIT COMPLETED. not handled here! s_phonebook should handle!");
1850                         return TRUE;
1851                         break;
1852
1853                 default:
1854                         dbg(" not handled SEC lock type ");
1855                         break;
1856         }
1857
1858         dbg("[SIM]Current co->sim_status[%d] and from modem[0x%x]",tcore_sim_get_status(o), sim_status);
1859
1860         switch (sim_status) {
1861                 case SIM_STATUS_INIT_COMPLETED:
1862                         ur = tcore_user_request_new(NULL, NULL); //this is for using ur metainfo set/ref functionality.
1863                         _get_file_info(o, ur, SIM_EF_IMSI);
1864                         break;
1865
1866                 case SIM_STATUS_INITIALIZING:
1867                 case SIM_STATUS_PIN_REQUIRED:
1868                 case SIM_STATUS_PUK_REQUIRED:
1869                 case SIM_STATUS_CARD_BLOCKED:
1870                 case SIM_STATUS_NCK_REQUIRED:
1871                 case SIM_STATUS_NSCK_REQUIRED:
1872                 case SIM_STATUS_SPCK_REQUIRED:
1873                 case SIM_STATUS_CCK_REQUIRED:
1874                 case SIM_STATUS_LOCK_REQUIRED:
1875                         if( sp->first_recv_status == SIM_STATUS_UNKNOWN ) {
1876                                 dbg("first received sim status[%d]",sim_status);
1877                                 sp->first_recv_status = sim_status;
1878                                 _get_sim_type(o);
1879                         }
1880                         break;
1881
1882                 case SIM_STATUS_CARD_REMOVED:
1883                 case SIM_STATUS_CARD_NOT_PRESENT:
1884                 case SIM_STATUS_CARD_ERROR:
1885                         if (sim_status == SIM_STATUS_CARD_NOT_PRESENT && tcore_sim_get_status(o) != SIM_STATUS_UNKNOWN) {
1886                                 dbg("[SIM]SIM CARD REMOVED!!");
1887                                 sim_status = SIM_STATUS_CARD_REMOVED;
1888                         }
1889                         _sim_status_update(o,sim_status);
1890                         break;
1891
1892                 default:
1893                         dbg("not handled status[%d]", sim_status);
1894                         break;
1895         }
1896
1897         return TRUE;
1898 }
1899
1900 static void on_response_verify_pins(TcorePending *p, int data_len, const void *data, void *user_data)
1901 {
1902         UserRequest *ur = NULL;
1903         CoreObject *co_sim = NULL;
1904         struct s_sim_property *sp = NULL;
1905         struct tresp_sim_verify_pins resp = {0,};
1906
1907         char* line=NULL;
1908         int ret;
1909         int error;
1910
1911         co_sim = tcore_pending_ref_core_object(p);
1912         sp = tcore_sim_ref_userdata(co_sim);
1913
1914         ur = tcore_pending_ref_user_request(p);
1915         if(!ur)
1916                 dbg("error - current ur is NULL");
1917
1918         printResponse();
1919
1920         if(sp_response->success > 0)
1921         {
1922                 ReleaseResponse();
1923
1924                 resp.result = SIM_PIN_OPERATION_SUCCESS;
1925                 resp.pin_type = _sim_get_current_pin_facility(sp);
1926                 tcore_user_request_send_response(ur, TRESP_SIM_VERIFY_PINS,     sizeof(struct tresp_sim_verify_pins), &resp);
1927         }
1928         else
1929         {
1930                 //failure case - consider this later
1931                 line = sp_response->finalResponse;
1932
1933                 ret = at_tok_start(&line);
1934                 if (ret < 0)
1935                         AT_TOK_ERROR(line);
1936
1937                 ret = at_tok_nextint(&line,&error);
1938                 if (ret < 0)
1939                         AT_TOK_ERROR(line);
1940
1941                 ReleaseResponse();
1942
1943                 // ur = user_request_dup(ur);
1944                 // _get_retry_count(co_sim, ur);
1945
1946                 resp.result = SIM_INCORRECT_PASSWORD;
1947                 resp.pin_type = _sim_get_current_pin_facility(sp);
1948                 resp.retry_count = 3;
1949                 tcore_user_request_send_response(ur, TRESP_SIM_VERIFY_PINS,     sizeof(struct tresp_sim_verify_pins), &resp);
1950
1951         }
1952 }
1953
1954 static void on_response_verify_puks(TcorePending *p, int data_len, const void *data, void *user_data)
1955 {
1956         UserRequest *ur = NULL;
1957         CoreObject *co_sim = NULL;
1958         struct s_sim_property *sp = NULL;
1959         struct tresp_sim_verify_puks resp = {0,};
1960
1961         char* line=NULL;
1962         int ret;
1963         int error;
1964
1965         co_sim = tcore_pending_ref_core_object(p);
1966         sp = tcore_sim_ref_userdata(co_sim);
1967
1968         ur = tcore_pending_ref_user_request(p);
1969         if(!ur)
1970                 dbg("error - current ur is NULL");
1971
1972         printResponse();
1973
1974         if(sp_response->success > 0)
1975         {
1976                 ReleaseResponse();
1977
1978                 resp.result = SIM_PIN_OPERATION_SUCCESS;
1979                 resp.pin_type = _sim_get_current_pin_facility(sp);
1980                 tcore_user_request_send_response(ur, TRESP_SIM_VERIFY_PINS,     sizeof(struct tresp_sim_verify_pins), &resp);
1981         }
1982         else
1983         {
1984                 //failure case - consider this later
1985                 line = sp_response->finalResponse;
1986
1987                 ret = at_tok_start(&line);
1988                 if (ret < 0)
1989                         AT_TOK_ERROR(line);
1990
1991                 ret = at_tok_nextint(&line,&error);
1992                 if (ret < 0)
1993                         AT_TOK_ERROR(line);
1994
1995                 ReleaseResponse();
1996
1997                 // ur = user_request_dup(ur);
1998                 // _get_retry_count(co_sim, ur);
1999
2000                 resp.result = SIM_INCORRECT_PASSWORD;
2001                 resp.pin_type = _sim_get_current_pin_facility(sp);
2002                 tcore_user_request_send_response(ur, TRESP_SIM_VERIFY_PUKS,     sizeof(struct tresp_sim_verify_puks), &resp);
2003
2004         }
2005 }
2006
2007 static void on_response_change_pins(TcorePending *p, int data_len, const void *data, void *user_data)
2008 {
2009         UserRequest *ur = NULL;
2010         CoreObject *co_sim = NULL;
2011         struct s_sim_property *sp = NULL;
2012         struct tresp_sim_change_pins resp = {0,};
2013
2014         char* line=NULL;
2015         int ret;
2016         int error;
2017
2018         co_sim = tcore_pending_ref_core_object(p);
2019         sp = tcore_sim_ref_userdata(co_sim);
2020
2021         ur = tcore_pending_ref_user_request(p);
2022         if(!ur)
2023                 dbg("error - current ur is NULL");
2024
2025         printResponse();
2026
2027         if(sp_response->success > 0)
2028         {
2029                 ReleaseResponse();
2030
2031                 resp.result = SIM_PIN_OPERATION_SUCCESS;
2032                 resp.pin_type = _sim_get_current_pin_facility(sp);
2033                 tcore_user_request_send_response(ur, TRESP_SIM_VERIFY_PINS,     sizeof(struct tresp_sim_verify_pins), &resp);
2034         }
2035         else
2036         {
2037                 //failure case - consider this later
2038                 line = sp_response->finalResponse;
2039
2040                 ret = at_tok_start(&line);
2041                 if (ret < 0)
2042                         AT_TOK_ERROR(line);
2043
2044                 ret = at_tok_nextint(&line,&error);
2045                 if (ret < 0)
2046                         AT_TOK_ERROR(line);
2047
2048                 ReleaseResponse();
2049
2050                 // ur = user_request_dup(ur);
2051                 // _get_retry_count(co_sim, ur);
2052
2053                 resp.result = SIM_INCORRECT_PASSWORD;
2054                 resp.pin_type = _sim_get_current_pin_facility(sp);
2055                 tcore_user_request_send_response(ur, TRESP_SIM_CHANGE_PINS,     sizeof(struct tresp_sim_change_pins), &resp);
2056         }
2057 }
2058
2059 static void on_response_get_facility_status(TcorePending *p, int data_len, const void *data, void *user_data)
2060 {
2061         UserRequest *ur = NULL;
2062         CoreObject *co_sim = NULL;
2063         struct s_sim_property *sec_meta = NULL;
2064         struct tresp_sim_get_facility_status resp = {0,};
2065
2066         char *line = NULL;
2067         int ret;
2068
2069         co_sim = tcore_pending_ref_core_object(p);
2070         ur = tcore_pending_ref_user_request(p);
2071         sec_meta = (struct s_sim_property*)tcore_user_request_ref_metainfo(ur, NULL);
2072
2073         resp.result = SIM_PIN_OPERATION_SUCCESS;
2074         resp.type = _sim_get_current_pin_facility(sec_meta);
2075
2076         printResponse();
2077
2078         if(sp_response->success > 0)
2079         {
2080                 line = sp_response->p_intermediates->line;
2081
2082                 ret = at_tok_start(&line);
2083                 if (ret < 0)
2084                         AT_TOK_ERROR(line);
2085
2086                 ret = at_tok_nextint(&line,(int *)&resp.b_enable);
2087                 if (ret < 0)
2088                         AT_TOK_ERROR(line);
2089         }
2090         else
2091         {
2092                 resp.result = SIM_INCOMPATIBLE_PIN_OPERATION;
2093         }
2094
2095         ReleaseResponse();
2096
2097         if (ur) {
2098                 tcore_user_request_send_response(ur, TRESP_SIM_GET_FACILITY_STATUS,
2099                                 sizeof(struct tresp_sim_get_facility_status), &resp);
2100         }
2101 }
2102
2103 static void on_response_enable_facility(TcorePending *p, int data_len, const void *data, void *user_data)
2104 {
2105         UserRequest *ur = NULL;
2106         CoreObject *co_sim = NULL;
2107         struct s_sim_property *sec_meta = NULL;
2108         struct tresp_sim_enable_facility resp = {0,};
2109         struct s_sim_property *sp = NULL;
2110
2111         char *line = NULL;
2112         int ret;
2113
2114         co_sim = tcore_pending_ref_core_object(p);
2115         ur = tcore_pending_ref_user_request(p);
2116         sp = tcore_sim_ref_userdata(co_sim);
2117         sec_meta = (struct s_sim_property*)tcore_user_request_ref_metainfo(ur, NULL);
2118
2119         resp.result = SIM_PIN_OPERATION_SUCCESS;
2120         resp.type = _sim_get_current_pin_facility(sec_meta);
2121
2122         printResponse();
2123
2124         if(sp_response->success > 0)
2125         {
2126                 line = sp_response->p_intermediates->line;
2127
2128                 ret = at_tok_start(&line);
2129                 if (ret < 0)
2130                         AT_TOK_ERROR(line);
2131
2132                 ret = at_tok_nextint(&line,(int *)&resp.result);
2133                 if (ret < 0)
2134                         AT_TOK_ERROR(line);
2135         }
2136         else
2137         {
2138                 resp.result = SIM_INCOMPATIBLE_PIN_OPERATION;
2139         }
2140
2141         ReleaseResponse();
2142
2143         if (ur) {
2144                 resp.type = _sim_get_current_pin_facility(sp);
2145                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_enable_facility), &resp);
2146         }
2147 }
2148
2149 static void on_response_disable_facility(TcorePending *p, int data_len, const void *data, void *user_data)
2150 {
2151         UserRequest *ur = NULL;
2152         CoreObject *co_sim = NULL;
2153         struct s_sim_property *sec_meta = NULL;
2154         struct tresp_sim_disable_facility resp = {0,};
2155         struct s_sim_property *sp = NULL;
2156
2157         char *line = NULL;
2158         int ret;
2159
2160         co_sim = tcore_pending_ref_core_object(p);
2161         ur = tcore_pending_ref_user_request(p);
2162         sp = tcore_sim_ref_userdata(co_sim);
2163         sec_meta = (struct s_sim_property*)tcore_user_request_ref_metainfo(ur, NULL);
2164
2165         resp.result = SIM_PIN_OPERATION_SUCCESS;
2166         resp.type = _sim_get_current_pin_facility(sec_meta);
2167
2168         printResponse();
2169
2170         if(sp_response->success > 0)
2171         {
2172                 line = sp_response->p_intermediates->line;
2173
2174                 ret = at_tok_start(&line);
2175                 if (ret < 0)
2176                         AT_TOK_ERROR(line);
2177
2178                 ret = at_tok_nextint(&line,(int *)&resp.result);
2179                 if (ret < 0)
2180                         AT_TOK_ERROR(line);
2181         }
2182         else
2183         {
2184                 resp.result = SIM_INCOMPATIBLE_PIN_OPERATION;
2185         }
2186
2187         ReleaseResponse();
2188
2189         if (ur) {
2190                 resp.type = _sim_get_current_pin_facility(sp);
2191                 tcore_user_request_send_response(ur, _find_resp_command(ur), sizeof(struct tresp_sim_disable_facility), &resp);
2192         }
2193 }
2194
2195 static TReturn s_verify_pins(CoreObject *o, UserRequest *ur)
2196 {
2197         TcorePlugin *p = NULL;
2198         TcoreHal *h = NULL;
2199         TcorePending *pending = NULL;
2200
2201         struct s_sim_property *sp = NULL;
2202         const struct treq_sim_verify_pins *req_data;
2203         TReturn trt = 0;
2204
2205         char *cmd_str = NULL;
2206
2207         if (!o || !ur)
2208                 return TCORE_RETURN_EINVAL;
2209
2210         p = tcore_object_ref_plugin(o);
2211         h = tcore_object_get_hal(o);
2212         req_data = tcore_user_request_ref_data(ur, NULL);
2213         sp = tcore_sim_ref_userdata(o);
2214
2215         if (req_data->pin_type == SIM_PTYPE_PIN1) {
2216                 sp->current_sec_op = SEC_PIN1_VERIFY;
2217         }
2218         else if (req_data->pin_type == SIM_PTYPE_PIN2) {
2219                 sp->current_sec_op = SEC_PIN2_VERIFY;
2220         }
2221         else if (req_data->pin_type == SIM_PTYPE_SIM) {
2222                 sp->current_sec_op = SEC_SIM_VERIFY;
2223         }
2224         else if (req_data->pin_type == SIM_PTYPE_ADM) {
2225                 sp->current_sec_op = SEC_ADM_VERIFY;
2226         }
2227         else {
2228                 return TCORE_RETURN_EINVAL;
2229         }
2230
2231         sp->metainfo.type = NO_RESULT;
2232         sp->metainfo.responsePrefix[0] = '\0';
2233
2234         trt = tcore_user_request_set_metainfo(ur, sizeof(struct s_sim_property), sp);
2235         dbg("trt[%d]",trt);
2236
2237
2238         // AT+CPIN=<pin>[,<newpin>]
2239         cmd_str = g_strdup_printf("AT+CPIN=\"%s\"%s", req_data->pin, "\r");
2240         dbg("new pending(verify - pins), %s", cmd_str);
2241
2242         pending = tcore_pending_new(o,ID_RESERVED_AT);
2243         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
2244         tcore_pending_set_timeout(pending, 0);
2245         tcore_pending_set_priority(pending, TCORE_PENDING_PRIORITY_DEFAULT);
2246         tcore_pending_set_response_callback(pending, on_response_verify_pins, NULL);
2247         tcore_pending_link_user_request(pending, ur);
2248
2249         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
2250
2251         tcore_hal_send_request(h, pending);
2252
2253         free(cmd_str);
2254
2255         return TCORE_RETURN_SUCCESS;
2256 }
2257
2258 static TReturn s_verify_puks(CoreObject *o, UserRequest *ur)
2259 {
2260         TcorePlugin *p = NULL;
2261         TcoreHal *h = NULL;
2262         TcorePending *pending = NULL;
2263
2264         const struct treq_sim_verify_puks *req_data;
2265         struct s_sim_property *sp = NULL;
2266         TReturn trt = 0;
2267
2268         char *cmd_str = NULL;
2269
2270         if (!o || !ur)
2271                 return TCORE_RETURN_EINVAL;
2272
2273         p = tcore_object_ref_plugin(o);
2274         h = tcore_object_get_hal(o);
2275         req_data = tcore_user_request_ref_data(ur, NULL);
2276         sp = tcore_sim_ref_userdata(o);
2277
2278
2279         if(req_data->puk_type == SIM_PTYPE_PUK1){
2280                 sp->current_sec_op = SEC_PUK1_VERIFY;
2281         }
2282         else if(req_data->puk_type == SIM_PTYPE_PUK2){
2283                 sp->current_sec_op = SEC_PUK2_VERIFY;
2284         }
2285         else {
2286                 return TCORE_RETURN_EINVAL;
2287         }
2288
2289         sp->metainfo.type = NO_RESULT;
2290         sp->metainfo.responsePrefix[0] = '\0';
2291
2292         trt = tcore_user_request_set_metainfo(ur, sizeof(struct s_sim_property), sp);
2293         dbg("trt[%d]",trt);
2294
2295         // AT+CPIN=<pin>[,<newpin>]
2296         cmd_str = g_strdup_printf("AT+CPIN=\"%s\", \"%s\"%s", req_data->puk, req_data->pin, "\r");
2297
2298         dbg("new pending(IPC_SEC_PIN_STATUS SET - verify puks)");
2299         pending = tcore_pending_new(o, ID_RESERVED_AT);
2300         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
2301         tcore_pending_set_timeout(pending, 0);
2302         tcore_pending_set_priority(pending, TCORE_PENDING_PRIORITY_DEFAULT);
2303         tcore_pending_set_response_callback(pending, on_response_verify_puks, NULL);
2304         tcore_pending_link_user_request(pending, ur);
2305
2306         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
2307
2308         tcore_hal_send_request(h, pending);
2309
2310         free(cmd_str);
2311
2312         return TCORE_RETURN_SUCCESS;
2313 }
2314
2315 static TReturn s_change_pins(CoreObject *o, UserRequest *ur)
2316 {
2317         TcorePlugin *p = NULL;
2318         TcoreHal *h = NULL;
2319         TcorePending *pending = NULL;
2320
2321         const struct treq_sim_change_pins *req_data;
2322         struct s_sim_property *sp = NULL;
2323         TReturn trt = 0;
2324
2325         char *cmd_str = NULL;
2326
2327         if (!o || !ur)
2328                 return TCORE_RETURN_EINVAL;
2329
2330         p = tcore_object_ref_plugin(o);
2331         h = tcore_object_get_hal(o);
2332         req_data = tcore_user_request_ref_data(ur, NULL);
2333         sp = tcore_sim_ref_userdata(o);
2334
2335         if(req_data->type == SIM_PTYPE_PIN1) {
2336                 sp->current_sec_op = SEC_PIN1_CHANGE;
2337         }
2338         else if(req_data->type == SIM_PTYPE_PIN2) {
2339                 sp->current_sec_op = SEC_PIN2_CHANGE;
2340         }
2341         else {
2342                 return TCORE_RETURN_EINVAL;
2343         }
2344
2345         sp->metainfo.type = NO_RESULT;
2346         sp->metainfo.responsePrefix[0] = '\0';
2347
2348         trt = tcore_user_request_set_metainfo(ur, sizeof(struct s_sim_property), sp);
2349         dbg("trt[%d]",trt);
2350
2351         // AT+CPIN=<pin>[,<newpin>]
2352         cmd_str = g_strdup_printf("AT+CPIN=\"%s\", \"%s\"%s", req_data->old_pin, req_data->new_pin, "\r");
2353
2354         dbg("new pending(IPC_SEC_CHANGE_LOCKING_PW SET)");
2355         pending = tcore_pending_new(o, ID_RESERVED_AT);
2356         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
2357         tcore_pending_set_timeout(pending, 0);
2358         tcore_pending_set_priority(pending, TCORE_PENDING_PRIORITY_DEFAULT);
2359         tcore_pending_set_response_callback(pending, on_response_change_pins, NULL);
2360         tcore_pending_link_user_request(pending, ur);
2361
2362         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
2363
2364         tcore_hal_send_request(h, pending);
2365
2366         free(cmd_str);
2367
2368         return TCORE_RETURN_SUCCESS;
2369 }
2370
2371 static TReturn s_get_facility_status(CoreObject *o, UserRequest *ur)
2372 {
2373         TcorePlugin *p = NULL;
2374         TcoreHal *h = NULL;
2375         TcorePending *pending = NULL;
2376         struct s_sim_property sec_meta = {0,};
2377         TReturn trt = 0;
2378
2379         const struct treq_sim_get_facility_status *req_data;
2380         char *fac = "SC";
2381         int mode = 2;           // 2:query, 0: unlock, 1:lock
2382
2383         char *cmd_str = NULL;
2384 //      struct ATReqMetaInfo metainfo;
2385
2386         if (!o || !ur)
2387                 return TCORE_RETURN_EINVAL;
2388
2389         p = tcore_object_ref_plugin(o);
2390         h = tcore_object_get_hal(o);
2391         req_data = tcore_user_request_ref_data(ur, NULL);
2392
2393         if(req_data->type == SIM_FACILITY_PS)
2394                 fac = "PS";
2395         else if(req_data->type == SIM_FACILITY_SC)
2396                 fac = "SC";
2397         else if(req_data->type == SIM_FACILITY_FD)
2398                 fac = "FD";
2399         else if(req_data->type == SIM_FACILITY_PN)
2400                 fac = "PN";
2401         else if(req_data->type == SIM_FACILITY_PU)
2402                 fac = "PU";
2403         else if(req_data->type == SIM_FACILITY_PP)
2404                 fac = "PP";
2405         else if(req_data->type == SIM_FACILITY_PC)
2406                 fac = "PC";
2407         else
2408                 return TCORE_RETURN_EINVAL;
2409
2410         sec_meta.current_sec_op = req_data->type;
2411         sec_meta.metainfo.type = SINGLELINE;
2412         memcpy(sec_meta.metainfo.responsePrefix,"+CLCK:",strlen("+CLCK:"));
2413
2414         trt = tcore_user_request_set_metainfo(ur, sizeof(struct s_sim_property), &sec_meta);
2415         dbg("trt[%d]",trt);
2416
2417         // AT+CLCK=<fac>,<mode>,<password>
2418         cmd_str = g_strdup_printf("AT+CLCK=\"%s\", %d%s", fac, mode, "\r");
2419
2420         dbg("new pending(IPC_SEC_PHONE_LOCK GET)");
2421         pending = tcore_pending_new(o, ID_RESERVED_AT);
2422         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
2423         tcore_pending_set_timeout(pending, 0);
2424         tcore_pending_set_priority(pending, TCORE_PENDING_PRIORITY_DEFAULT);
2425         tcore_pending_set_response_callback(pending, on_response_get_facility_status, NULL);
2426         tcore_pending_link_user_request(pending, ur);
2427
2428         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
2429
2430         tcore_hal_send_request(h, pending);
2431
2432         free(cmd_str);
2433
2434         return TCORE_RETURN_SUCCESS;
2435 }
2436
2437 static TReturn s_enable_facility(CoreObject *o, UserRequest *ur)
2438 {
2439         TcorePlugin *p = NULL;
2440         TcoreHal *h = NULL;
2441         TcorePending *pending = NULL;
2442         struct s_sim_property sec_meta = {0,};
2443         const struct treq_sim_enable_facility *req_data;
2444         struct s_sim_property *sp = NULL;
2445         char *fac = "SC";
2446         int mode = 1;           // 2:query, 0: unlock, 1:lock
2447
2448         char *cmd_str = NULL;
2449 //      struct ATReqMetaInfo metainfo;
2450
2451         if (!o || !ur)
2452                 return TCORE_RETURN_EINVAL;
2453
2454         p = tcore_object_ref_plugin(o);
2455         h = tcore_object_get_hal(o);
2456         req_data = tcore_user_request_ref_data(ur, NULL);
2457         sp = tcore_sim_ref_userdata(o);
2458
2459         if (req_data->type == SIM_FACILITY_PS)
2460                 fac = "PS";
2461         else if (req_data->type == SIM_FACILITY_SC)
2462                 fac = "SC";
2463         else if (req_data->type == SIM_FACILITY_FD)
2464                 fac = "FD";
2465         else if (req_data->type == SIM_FACILITY_PN)
2466                 fac = "PN";
2467         else if (req_data->type == SIM_FACILITY_PU)
2468                 fac = "PU";
2469         else if (req_data->type == SIM_FACILITY_PP)
2470                 fac = "PP";
2471         else if (req_data->type == SIM_FACILITY_PC)
2472                 fac = "PC";
2473         else
2474                 return TCORE_RETURN_EINVAL;
2475
2476         sp->current_sec_op = SEC_SIM_ENABLE;
2477
2478         sec_meta.current_sec_op = req_data->type;
2479         sec_meta.metainfo.type = SINGLELINE;
2480         memcpy(sec_meta.metainfo.responsePrefix,"+CLCK:",strlen("+CLCK:"));
2481
2482         tcore_user_request_set_metainfo(ur, sizeof(struct s_sim_property), &sec_meta);
2483
2484         // AT+CLCK=<fac>,<mode>,<password>
2485         cmd_str = g_strdup_printf("AT+CLCK=\"%s\", %d, %s%s", fac, mode, req_data->password,"\r");
2486         dbg("new pending(enable_facility), %s", cmd_str);
2487
2488         pending = tcore_pending_new(o, ID_RESERVED_AT);
2489         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
2490         tcore_pending_set_timeout(pending, 0);
2491         tcore_pending_set_priority(pending, TCORE_PENDING_PRIORITY_DEFAULT);
2492         tcore_pending_set_response_callback(pending, on_response_enable_facility, NULL);
2493         tcore_pending_link_user_request(pending, ur);
2494
2495         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
2496
2497         tcore_hal_send_request(h, pending);
2498
2499         free(cmd_str);
2500
2501         return TCORE_RETURN_SUCCESS;
2502 }
2503
2504 static TReturn s_disable_facility(CoreObject *o, UserRequest *ur)
2505 {
2506         TcorePlugin *p = NULL;
2507         TcoreHal *h = NULL;
2508         TcorePending *pending = NULL;
2509         struct s_sim_property sec_meta = {0,};
2510         const struct treq_sim_enable_facility *req_data;
2511         struct s_sim_property *sp = NULL;
2512         char *fac = "SC";
2513         int mode = 0;           // 2:query, 0: unlock, 1:lock
2514
2515         char *cmd_str = NULL;
2516         //      struct ATReqMetaInfo metainfo;
2517
2518         if (!o || !ur)
2519                 return TCORE_RETURN_EINVAL;
2520
2521         p = tcore_object_ref_plugin(o);
2522         h = tcore_object_get_hal(o);
2523         req_data = tcore_user_request_ref_data(ur, NULL);
2524         sp = tcore_sim_ref_userdata(o);
2525
2526         if (req_data->type == SIM_FACILITY_PS)
2527                 fac = "PS";
2528         else if (req_data->type == SIM_FACILITY_SC)
2529                 fac = "SC";
2530         else if (req_data->type == SIM_FACILITY_FD)
2531                 fac = "FD";
2532         else if (req_data->type == SIM_FACILITY_PN)
2533                 fac = "PN";
2534         else if (req_data->type == SIM_FACILITY_PU)
2535                 fac = "PU";
2536         else if (req_data->type == SIM_FACILITY_PP)
2537                 fac = "PP";
2538         else if (req_data->type == SIM_FACILITY_PC)
2539                 fac = "PC";
2540         else
2541                 return TCORE_RETURN_EINVAL;
2542
2543         sp->current_sec_op = SEC_SIM_ENABLE;
2544
2545         sec_meta.current_sec_op = req_data->type;
2546         sec_meta.metainfo.type = SINGLELINE;
2547         memcpy(sec_meta.metainfo.responsePrefix,"+CLCK:",strlen("+CLCK:"));
2548
2549         tcore_user_request_set_metainfo(ur, sizeof(struct s_sim_property), &sec_meta);
2550
2551         // AT+CLCK=<fac>,<mode>,<password>
2552         cmd_str = g_strdup_printf("AT+CLCK=\"%s\", %d, %s%s", fac, mode, req_data->password,"\r");
2553         dbg("new pending(enable_facility), %s", cmd_str);
2554
2555         pending = tcore_pending_new(o, ID_RESERVED_AT);
2556         tcore_pending_set_request_data(pending, strlen(cmd_str), cmd_str);
2557         tcore_pending_set_timeout(pending, 0);
2558         tcore_pending_set_priority(pending, TCORE_PENDING_PRIORITY_DEFAULT);
2559         tcore_pending_set_response_callback(pending, on_response_disable_facility, NULL);
2560         tcore_pending_link_user_request(pending, ur);
2561
2562         tcore_pending_set_send_callback(pending, on_confirmation_sim_message_send, NULL);
2563
2564         tcore_hal_send_request(h, pending);
2565
2566         free(cmd_str);
2567
2568         return TCORE_RETURN_SUCCESS;
2569 }
2570
2571 static TReturn s_read_file(CoreObject *o, UserRequest *ur)
2572 {
2573         TReturn api_ret = TCORE_RETURN_SUCCESS;
2574         enum tcore_request_command command;
2575         command = tcore_user_request_get_command(ur);
2576
2577         dbg("enter");
2578
2579         switch (command) {
2580                 case TREQ_SIM_GET_ECC:
2581                         api_ret = _get_file_info(o, ur, SIM_EF_ECC);
2582                         break;
2583
2584                 case TREQ_SIM_GET_LANGUAGE:
2585                         if (tcore_sim_get_type(o) == SIM_TYPE_GSM)
2586                                 api_ret = _get_file_info(o, ur, SIM_EF_ELP);
2587                         else if (tcore_sim_get_type(o) == SIM_TYPE_USIM)
2588                                 api_ret = _get_file_info(o, ur, SIM_EF_LP);
2589                         else
2590                                 api_ret = TCORE_RETURN_ENOSYS;
2591                         break;
2592
2593                 case TREQ_SIM_GET_ICCID:
2594                         api_ret = _get_file_info(o, ur, SIM_EF_ICCID);
2595                         break;
2596
2597                 case TREQ_SIM_GET_MAILBOX:
2598                         if(tcore_sim_get_cphs_status(o))
2599                                 api_ret = _get_file_info(o, ur, SIM_EF_CPHS_MAILBOX_NUMBERS);
2600                         else
2601                                 api_ret = _get_file_info(o, ur, SIM_EF_MBDN);
2602                         break;
2603
2604                 case TREQ_SIM_GET_CALLFORWARDING:
2605                         if(tcore_sim_get_cphs_status(o))
2606                                 api_ret = _get_file_info(o, ur, SIM_EF_CPHS_CALL_FORWARD_FLAGS);
2607                         else
2608                                 api_ret = _get_file_info(o, ur, SIM_EF_USIM_CFIS);
2609                         break;
2610
2611                 case TREQ_SIM_GET_MESSAGEWAITING:
2612                         if(tcore_sim_get_cphs_status(o))
2613                                 api_ret = _get_file_info(o, ur, SIM_EF_CPHS_VOICE_MSG_WAITING);
2614                         else
2615                                 api_ret = _get_file_info(o, ur, SIM_EF_USIM_MWIS);
2616                         break;
2617
2618                 case TREQ_SIM_GET_CPHS_INFO:
2619                         if(tcore_sim_get_cphs_status(o))
2620                                 api_ret = _get_file_info(o, ur, SIM_EF_CPHS_CPHS_INFO);
2621                         else
2622                                 api_ret = TCORE_RETURN_ENOSYS;
2623                         break;
2624
2625                 case TREQ_SIM_GET_MSISDN:
2626                         api_ret = _get_file_info(o, ur, SIM_EF_MSISDN);
2627                         break;
2628
2629                 case TREQ_SIM_GET_SPN:
2630                         dbg("enter case SPN");
2631                         api_ret = _get_file_info(o, ur, SIM_EF_SPN);
2632                         break;
2633
2634                 case TREQ_SIM_GET_SPDI:
2635                         api_ret = _get_file_info(o, ur, SIM_EF_SPDI);
2636                         break;
2637
2638                 case TREQ_SIM_GET_OPL:
2639                         api_ret = _get_file_info(o, ur, SIM_EF_OPL);
2640                         break;
2641
2642                 case TREQ_SIM_GET_PNN:
2643                         api_ret = _get_file_info(o, ur, SIM_EF_PNN);
2644                         break;
2645
2646                 case TREQ_SIM_GET_CPHS_NETNAME:
2647                         api_ret = _get_file_info(o, ur, SIM_EF_CPHS_OPERATOR_NAME_STRING);
2648                         break;
2649
2650                 case TREQ_SIM_GET_OPLMNWACT:
2651                         api_ret = _get_file_info(o, ur, SIM_EF_OPLMN_ACT);
2652                         break;
2653
2654                 default:
2655                         dbg("error - not handled read treq command[%d]", command);
2656                         api_ret = TCORE_RETURN_EINVAL;
2657                         break;
2658         }
2659         return api_ret;
2660 }
2661
2662 static TReturn s_update_file(CoreObject *o, UserRequest *ur)
2663 {
2664         TReturn api_ret = TCORE_RETURN_ENOSYS;
2665         return api_ret;
2666 }
2667
2668 static TReturn s_transmit_apdu(CoreObject *o, UserRequest *ur)
2669 {
2670         dbg("need to be implemented to use ATCMD");
2671
2672         return TCORE_RETURN_SUCCESS;
2673 }
2674
2675 static TReturn s_get_atr(CoreObject *o, UserRequest *ur)
2676 {
2677         dbg("need to be implemented to use ATCMD");
2678
2679         return TCORE_RETURN_SUCCESS;
2680 }
2681
2682 static TReturn s_req_authentication(CoreObject *o, UserRequest *ur)
2683 {
2684         return TCORE_RETURN_SUCCESS;
2685 }
2686
2687 static struct tcore_sim_operations sim_ops =
2688 {
2689                 .verify_pins = s_verify_pins,
2690                 .verify_puks = s_verify_puks,
2691                 .change_pins = s_change_pins,
2692                 .get_facility_status = s_get_facility_status,
2693                 .enable_facility = s_enable_facility,
2694                 .disable_facility = s_disable_facility,
2695                 .read_file = s_read_file,
2696                 .update_file = s_update_file,
2697                 .transmit_apdu = s_transmit_apdu,
2698                 .get_atr = s_get_atr,
2699                 .req_authentication = s_req_authentication
2700 };
2701
2702 gboolean s_sim_init(TcorePlugin *p, TcoreHal *h)
2703 {
2704         CoreObject *o;
2705         struct s_sim_property *sp = NULL;
2706
2707         o = tcore_sim_new(p, "sim", &sim_ops, h);
2708         if (!o)
2709                 return FALSE;
2710
2711         sp = calloc(sizeof(struct s_sim_property),1);
2712         if (!sp)
2713                 return FALSE;
2714
2715         sp->first_recv_status = SIM_STATUS_UNKNOWN;
2716         tcore_sim_link_userdata(o, sp);
2717
2718         tcore_object_add_callback(o, EVENT_SIM_PIN_STATUS, on_event_pin_status, NULL);
2719         return TRUE;
2720 }
2721
2722
2723 void s_sim_exit(TcorePlugin *p)
2724 {
2725         CoreObject *o;
2726
2727         o = tcore_plugin_ref_core_object(p, "sim");
2728         if (!o)
2729                 return;
2730         tcore_sim_free(o);
2731 }