release tizen_2.0
[framework/api/sim.git] / src / sim.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <sim.h>
18 #include <tapi_common.h>
19 #include <TapiUtility.h>
20 #include <ITapiSim.h>
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <dlog.h>
26
27 #include <glib.h>
28 #include <glib-object.h>
29 #include <gio/gio.h>
30
31 #ifdef LOG_TAG
32 #undef LOG_TAG
33 #endif
34 #define LOG_TAG "TIZEN_N_SIM"
35
36 struct tapi_handle {
37         gpointer dbus_connection;
38         char *path;
39         char *cp_name;
40         GHashTable *evt_list;
41         char cookie[20];
42 };
43
44 typedef struct sim_cb_data {
45         sim_state_e previous_state;
46         struct tapi_handle *th;
47         void* cb;
48         void* user_data;
49 } sim_cb_data;
50
51 static struct tapi_handle *ghandle = NULL;
52
53 // Internal Macros
54 #define SIM_CHECK_INPUT_PARAMETER(arg) \
55         if( arg == NULL ) \
56         { \
57                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, SIM_ERROR_INVALID_PARAMETER); \
58                 return SIM_ERROR_INVALID_PARAMETER; \
59         }
60
61 #define SIM_INIT(th) \
62         th = tel_init(NULL); \
63         if (!th) { \
64                 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED); \
65                 return SIM_ERROR_OPERATION_FAILED; \
66         }
67
68 #define SIM_MAKE_CB(ccb,th,callback,user_data)  \
69         ccb = (sim_cb_data*) calloc(sizeof(sim_cb_data), 1);\
70         ccb->th = th; \
71         ccb->cb = (void*) callback;\
72         ccb->user_data = user_data
73
74 static sim_error_e _convert_access_rt_to_sim_error(TelSimAccessResult_t access_rt)
75 {
76         sim_error_e error = SIM_ERROR_NONE;
77         switch (access_rt) {
78                 case TAPI_SIM_ACCESS_SUCCESS:
79                         error = SIM_ERROR_NONE;
80                         break;
81                 case TAPI_SIM_ACCESS_FILE_NOT_FOUND:
82                 case TAPI_SIM_ACCESS_ACCESS_CONDITION_NOT_SATISFIED:
83                         error = SIM_ERROR_NOT_AVAILABLE;
84                         break;
85                 case TAPI_SIM_ACCESS_CARD_ERROR:
86                 case TAPI_SIM_ACCESS_FAILED:
87                 default:
88                         error = SIM_ERROR_OPERATION_FAILED;
89                         break;
90         }
91         return error;
92 }
93
94 int sim_get_icc_id(char** icc_id)
95 {
96         int error_code = SIM_ERROR_NONE;
97         int card_changed = 0;
98         TelSimCardStatus_t sim_card_state = 0x00;
99         struct tapi_handle *th = NULL;
100         GError *gerr = NULL;
101         GVariant *sync_gv = NULL;
102         gchar *iccid = NULL;
103         TelSimAccessResult_t result = TAPI_SIM_ACCESS_SUCCESS;
104
105         SIM_CHECK_INPUT_PARAMETER(icc_id);
106         SIM_INIT(th);
107
108         if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
109                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
110                 error_code = SIM_ERROR_NOT_AVAILABLE;
111         } else {
112                 sync_gv = g_dbus_connection_call_sync(th->dbus_connection, DBUS_TELEPHONY_SERVICE, th->path,
113                                 DBUS_TELEPHONY_SIM_INTERFACE, "GetICCID", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, -1,
114                                 NULL, &gerr);
115
116                 if (sync_gv) {
117                         g_variant_get(sync_gv, "(is)", &result, &iccid);
118                         if (result == TAPI_SIM_ACCESS_SUCCESS) {
119                                 if (iccid != NULL && strlen(iccid) != 0) {
120                                         *icc_id = (char*) malloc(strlen(iccid) + 1);
121                                         if (*icc_id == NULL) {
122                                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
123                                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
124                                         } else {
125                                                 snprintf(*icc_id, strlen(iccid) + 1, "%s", iccid);
126                                         }
127                                 } else {
128                                         *icc_id = NULL;
129                                 }
130                         } else {
131                                 error_code = _convert_access_rt_to_sim_error(result);
132                         }
133                 } else {
134                         LOGE("g_dbus_conn failed. error (%s)", gerr->message);
135                         g_error_free(gerr);
136                         error_code = SIM_ERROR_OPERATION_FAILED;
137                 }
138         }
139         tel_deinit(th);
140         return error_code;
141 }
142
143 int sim_get_mcc(char** mcc)
144 {
145         TelSimImsiInfo_t sim_imsi_info;
146         int error_code = SIM_ERROR_NONE;
147         int card_changed = 0;
148         TelSimCardStatus_t sim_card_state = 0x00;
149         struct tapi_handle *th = NULL;
150
151         SIM_CHECK_INPUT_PARAMETER(mcc);
152         SIM_INIT(th);
153
154         if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
155                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
156                 error_code = SIM_ERROR_NOT_AVAILABLE;
157         } else {
158                 if (tel_get_sim_imsi(th, &sim_imsi_info) != 0) {
159                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
160                         error_code = SIM_ERROR_OPERATION_FAILED;
161                 } else {
162                         *mcc = (char*) malloc(sizeof(char) * (3 + 1));
163                         if (*mcc == NULL) {
164                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
165                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
166                         } else {
167                                 snprintf(*mcc, strlen(sim_imsi_info.szMcc) + 1, "%s", sim_imsi_info.szMcc);
168                         }
169                 }
170         }
171         tel_deinit(th);
172         return error_code;
173 }
174
175 int sim_get_mnc(char** mnc)
176 {
177         TelSimImsiInfo_t sim_imsi_info;
178         int error_code = SIM_ERROR_NONE;
179         int card_changed = 0;
180         TelSimCardStatus_t sim_card_state = 0x00;
181         struct tapi_handle *th = NULL;
182
183         SIM_CHECK_INPUT_PARAMETER(mnc);
184         SIM_INIT(th);
185
186         if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0
187                         || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
188                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
189                 error_code = SIM_ERROR_NOT_AVAILABLE;
190         } else {
191                 if (tel_get_sim_imsi(th, &sim_imsi_info) != 0) {
192                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
193                         error_code = SIM_ERROR_OPERATION_FAILED;
194                 } else {
195                         *mnc = (char*) malloc(sizeof(char) * (3 + 1));
196                         if (*mnc == NULL) {
197                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
198                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
199                         } else {
200                                 snprintf(*mnc, strlen(sim_imsi_info.szMnc) + 1, "%s", sim_imsi_info.szMnc);
201                         }
202                 }
203         }
204
205         tel_deinit(th);
206         return SIM_ERROR_NONE;
207 }
208
209 int sim_get_msin(char** msin)
210 {
211         TelSimImsiInfo_t sim_imsi_info;
212         int error_code = SIM_ERROR_NONE;
213         int card_changed = 0;
214         TelSimCardStatus_t sim_card_state = 0x00;
215         struct tapi_handle *th = NULL;
216
217         SIM_CHECK_INPUT_PARAMETER(msin);
218         SIM_INIT(th);
219
220         if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
221                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
222                 error_code = SIM_ERROR_NOT_AVAILABLE;
223         } else {
224                 if (tel_get_sim_imsi(th, &sim_imsi_info) != 0) {
225                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
226                         error_code = SIM_ERROR_OPERATION_FAILED;
227                 } else {
228                         *msin = (char*) malloc(sizeof(char) * (10 + 1));
229                         if (*msin == NULL) {
230                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
231                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
232                         } else {
233                                 snprintf(*msin, strlen(sim_imsi_info.szMsin) + 1, "%s", sim_imsi_info.szMsin);
234                         }
235                 }
236         }
237         tel_deinit(th);
238         return error_code;
239 }
240
241 int sim_get_spn(char** spn)
242 {
243         int error_code = SIM_ERROR_NONE;
244         int card_changed = 0;
245         TelSimCardStatus_t sim_card_state = 0x00;
246         struct tapi_handle *th = NULL;
247         GError *gerr = NULL;
248         GVariant *sync_gv = NULL;
249         TelSimAccessResult_t result = TAPI_SIM_ACCESS_SUCCESS;
250         gchar *spn_str = NULL;
251         guchar dc = 0;
252
253         SIM_CHECK_INPUT_PARAMETER(spn);
254         SIM_INIT(th);
255
256         if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0
257                         || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
258                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
259                 error_code = SIM_ERROR_NOT_AVAILABLE;
260         } else {
261                 sync_gv = g_dbus_connection_call_sync(th->dbus_connection, DBUS_TELEPHONY_SERVICE, th->path,
262                                 DBUS_TELEPHONY_SIM_INTERFACE, "GetSpn", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, -1,
263                                 NULL, &gerr);
264
265                 if (sync_gv) {
266                         g_variant_get(sync_gv, "(iys)", &result, &dc, &spn_str);
267                         if (result == TAPI_SIM_ACCESS_SUCCESS) {
268                                 if (spn_str != NULL && strlen(spn_str) != 0) {
269                                         *spn = (char*) malloc(strlen(spn_str) + 1);
270                                         if (*spn == NULL) {
271                                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
272                                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
273                                         } else {
274                                                 snprintf(*spn, strlen(spn_str) + 1, "%s", spn_str);
275                                         }
276                                 } else {
277                                         *spn = NULL;
278                                 }
279                         } else {
280                                 error_code = _convert_access_rt_to_sim_error(result);
281                         }
282                 } else {
283                         LOGE("g_dbus_conn failed. error (%s)", gerr->message);
284                         g_error_free(gerr);
285                         error_code = SIM_ERROR_OPERATION_FAILED;
286                 }
287         }
288         tel_deinit(th);
289         return error_code;
290 }
291
292 int sim_get_cphs_operator_name(char** full_name, char** short_name)
293 {
294         int error_code = SIM_ERROR_NONE;
295         int card_changed = 0;
296         TelSimCardStatus_t sim_card_state = 0x00;
297         struct tapi_handle *th = NULL;
298         GError *gerr = NULL;
299         GVariant *sync_gv = NULL;
300         TelSimAccessResult_t result = TAPI_SIM_ACCESS_SUCCESS;
301         gchar *full_str = NULL;
302         gchar *short_str = NULL;
303
304         SIM_CHECK_INPUT_PARAMETER(full_name);
305         SIM_CHECK_INPUT_PARAMETER(short_name);
306         SIM_INIT(th);
307
308         if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0
309                         || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
310                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
311                 error_code = SIM_ERROR_NOT_AVAILABLE;
312         } else {
313                 sync_gv = g_dbus_connection_call_sync(th->dbus_connection, DBUS_TELEPHONY_SERVICE, th->path,
314                                 DBUS_TELEPHONY_SIM_INTERFACE, "GetCphsNetName", NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
315                                 -1, NULL, &gerr);
316
317                 if (sync_gv) {
318                         g_variant_get(sync_gv, "(iss)", &result, &full_str, &short_str);
319                         if (result == TAPI_SIM_ACCESS_SUCCESS) {
320                                 if (full_str != NULL && strlen(full_str) != 0) {
321                                         *full_name = (char*) malloc(strlen(full_str) + 1);
322                                         if (*full_name == NULL) {
323                                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
324                                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
325                                         } else {
326                                                 snprintf(*full_name, strlen(full_str) + 1, "%s", full_str);
327                                         }
328                                 } else {
329                                         *full_name = NULL;
330                                 }
331
332                                 if (short_str != NULL && strlen(short_str) != 0) {
333                                         *short_name = (char*) malloc(strlen(short_str) + 1);
334                                         if (*short_name == NULL) {
335                                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
336                                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
337                                         } else {
338                                                 snprintf(*short_name, strlen(short_str) + 1, "%s", short_str);
339                                         }
340                                 } else {
341                                         *short_name = NULL;
342                                 }
343                         } else {
344                                 error_code = _convert_access_rt_to_sim_error(result);
345                         }
346                 } else {
347                         LOGE("g_dbus_conn failed. error (%s)", gerr->message);
348                         g_error_free(gerr);
349                         error_code = SIM_ERROR_OPERATION_FAILED;
350                 }
351         }
352         tel_deinit(th);
353         return error_code;
354 }
355
356 int sim_get_state(sim_state_e* sim_state)
357 {
358         int card_changed = 0;
359         TelSimCardStatus_t sim_card_state = 0x00;
360         int error_code = SIM_ERROR_NONE;
361         struct tapi_handle *th = NULL;
362
363         SIM_CHECK_INPUT_PARAMETER(sim_state);
364         SIM_INIT(th);
365
366         if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0) {
367                 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
368                 error_code = SIM_ERROR_OPERATION_FAILED;
369         } else {
370                 switch (sim_card_state) {
371                         case TAPI_SIM_STATUS_CARD_ERROR:
372                                 *sim_state = SIM_STATE_UNAVAILABLE;
373                                 break;
374                         case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
375                                 *sim_state = SIM_STATE_UNAVAILABLE;
376                                 break;
377                         case TAPI_SIM_STATUS_SIM_INITIALIZING:
378                                 *sim_state = SIM_STATE_UNKNOWN;
379                                 break;
380                         case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
381                                 *sim_state = SIM_STATE_AVAILABLE;
382                                 break;
383                         case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
384                                 *sim_state = SIM_STATE_LOCKED;
385                                 break;
386                         case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
387                                 *sim_state = SIM_STATE_LOCKED;
388                                 break;
389                         case TAPI_SIM_STATUS_CARD_BLOCKED:
390                                 *sim_state = SIM_STATE_UNAVAILABLE;
391                                 break;
392                         case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
393                                 *sim_state = SIM_STATE_LOCKED;
394                                 break;
395                         case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
396                                 *sim_state = SIM_STATE_LOCKED;
397                                 break;
398                         case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
399                                 *sim_state = SIM_STATE_LOCKED;
400                                 break;
401                         case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
402                                 *sim_state = SIM_STATE_LOCKED;
403                                 break;
404                         case TAPI_SIM_STATUS_CARD_REMOVED:
405                                 *sim_state = SIM_STATE_UNAVAILABLE;
406                                 break;
407                         case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
408                                 *sim_state = SIM_STATE_LOCKED;
409                                 break;
410                         default:
411                                 *sim_state = SIM_STATE_UNAVAILABLE;
412                                 break;
413                 }
414         }
415
416         tel_deinit(th);
417         return error_code;
418 }
419
420 int sim_get_subscriber_number(char** subscriber_number)
421 {
422         int error_code = SIM_ERROR_NONE;
423         int card_changed = 0;
424         TelSimCardStatus_t sim_card_state = 0x00;
425         struct tapi_handle *th = NULL;
426         GError *gerr = NULL;
427         GVariant *sync_gv = NULL;
428         GVariant *value = NULL;
429         GVariantIter *iter = NULL;
430         GVariantIter *iter_row = NULL;
431         const gchar *key = NULL;
432         const gchar *str_value = NULL;
433         TelSimAccessResult_t result = TAPI_SIM_ACCESS_SUCCESS;
434         TelSimMsisdnList_t list;
435         int i = 0;
436
437         SIM_CHECK_INPUT_PARAMETER(subscriber_number);
438         SIM_INIT(th);
439
440         if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
441                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
442                 error_code = SIM_ERROR_NOT_AVAILABLE;
443         } else {
444                 sync_gv = g_dbus_connection_call_sync(th->dbus_connection, DBUS_TELEPHONY_SERVICE, th->path,
445                                 DBUS_TELEPHONY_SIM_INTERFACE, "GetMSISDN", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, -1,
446                                 NULL, &gerr);
447
448                 if (sync_gv) {
449                         memset(&list, 0, sizeof(TelSimMsisdnList_t));
450                         g_variant_get(sync_gv, "(iaa{sv})", &result, &iter);
451                         list.count = g_variant_iter_n_children(iter);
452
453                         if (result == TAPI_SIM_ACCESS_SUCCESS) {
454                                 i = 0;
455                                 while (g_variant_iter_next(iter, "a{sv}", &iter_row)) {
456                                         while (g_variant_iter_loop(iter_row, "{sv}", &key, &value)) {
457                                                 if (!g_strcmp0(key, "name")) {
458                                                         str_value = g_variant_get_string(value, NULL);
459                                                         snprintf(list.list[i].name, strlen(str_value) + 1, "%s", str_value);
460                                                 }
461                                                 if (!g_strcmp0(key, "number")) {
462                                                         str_value = g_variant_get_string(value, NULL);
463                                                         snprintf(list.list[i].num, strlen(str_value) + 1, "%s", str_value);
464                                                 }
465                                         }
466                                         i++;
467                                         g_variant_iter_free(iter_row);
468                                 }
469                                 g_variant_iter_free(iter);
470
471                                 if (list.list[0].num != NULL && strlen(list.list[0].num) != 0) {
472                                         *subscriber_number = (char*) malloc(strlen(list.list[0].num) + 1);
473                                         if (*subscriber_number == NULL) {
474                                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
475                                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
476                                         } else {
477                                                 snprintf(*subscriber_number, strlen(list.list[0].num) + 1, "%s",
478                                                                 list.list[0].num);
479                                         }
480                                 } else {
481                                         *subscriber_number = NULL;
482                                 }
483
484                         } else {
485                                 error_code = _convert_access_rt_to_sim_error(result);
486                         }
487                 } else {
488                         LOGE("g_dbus_conn failed. error (%s)", gerr->message);
489                         g_error_free(gerr);
490                         error_code = SIM_ERROR_OPERATION_FAILED;
491                 }
492         }
493         tel_deinit(th);
494         return error_code;
495 }
496
497 static void on_noti_sim_status(struct tapi_handle *handle, const char *noti_id, void *data,
498                 void *user_data)
499 {
500         TelSimCardStatus_t *status = data;
501         sim_cb_data *ccb = user_data;
502         sim_state_e state = SIM_STATE_UNKNOWN;
503         sim_state_changed_cb cb;
504         LOGE("event(%s) receive with status[%d]", TAPI_NOTI_SIM_STATUS, *status);
505
506         switch (*status) {
507                 case TAPI_SIM_STATUS_CARD_ERROR:
508                         state = SIM_STATE_UNAVAILABLE;
509                         break;
510                 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
511                         state = SIM_STATE_UNAVAILABLE;
512                         break;
513                 case TAPI_SIM_STATUS_SIM_INITIALIZING:
514                         state = SIM_STATE_UNKNOWN;
515                         break;
516                 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
517                         state = SIM_STATE_AVAILABLE;
518                         break;
519                 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
520                         state = SIM_STATE_LOCKED;
521                         break;
522                 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
523                         state = SIM_STATE_LOCKED;
524                         break;
525                 case TAPI_SIM_STATUS_CARD_BLOCKED:
526                         state = SIM_STATE_UNAVAILABLE;
527                         break;
528                 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
529                         state = SIM_STATE_LOCKED;
530                         break;
531                 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
532                         state = SIM_STATE_LOCKED;
533                         break;
534                 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
535                         state = SIM_STATE_LOCKED;
536                         break;
537                 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
538                         state = SIM_STATE_LOCKED;
539                         break;
540                 case TAPI_SIM_STATUS_CARD_REMOVED:
541                         state = SIM_STATE_UNAVAILABLE;
542                         break;
543                 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
544                         state = SIM_STATE_LOCKED;
545                         break;
546                 default:
547                         state = SIM_STATE_UNAVAILABLE;
548                         break;
549         }
550         if (!ccb->cb) {
551                 LOGE("[%s] callback is null", __FUNCTION__);
552                 return;
553         }
554         cb = ccb->cb;
555         cb(state, ccb->user_data);
556 }
557
558 int sim_set_state_changed_cb(sim_state_changed_cb sim_cb, void* user_data)
559 {
560         int error_code = SIM_ERROR_NONE;
561         int ret;
562         sim_cb_data *ccb = NULL;
563
564         SIM_CHECK_INPUT_PARAMETER(sim_cb);
565
566         ghandle = tel_init(NULL);
567         if (!ghandle) {
568                 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
569                 return SIM_ERROR_OPERATION_FAILED;
570         }
571
572         ccb = (sim_cb_data*) calloc(sizeof(sim_cb_data), 1);
573         ccb->th = ghandle;
574         ccb->cb = (void*) sim_cb;
575         ccb->user_data = user_data;
576
577         ret = tel_register_noti_event(ghandle, TAPI_NOTI_SIM_STATUS, on_noti_sim_status, ccb);
578         if (ret != TAPI_API_SUCCESS) error_code = SIM_ERROR_OPERATION_FAILED;
579         return error_code;
580 }
581
582 int sim_unset_state_changed_cb()
583 {
584         int error_code = SIM_ERROR_NONE;
585         int ret;
586
587         if (ghandle == NULL) {
588                 ghandle = tel_init(NULL);
589                 if (!ghandle) {
590                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
591                         return SIM_ERROR_OPERATION_FAILED;
592                 }
593         }
594         ret = tel_deregister_noti_event(ghandle, TAPI_NOTI_SIM_STATUS);
595         if (ret != TAPI_API_SUCCESS) error_code = SIM_ERROR_OPERATION_FAILED;
596         return error_code;
597 }