sync with private git ver.0.1.4
[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                 case TAPI_SIM_ACCESS_FILE_NOT_FOUND:
80                         error = SIM_ERROR_NONE;
81                         break;
82                 case TAPI_SIM_ACCESS_ACCESS_CONDITION_NOT_SATISFIED:
83                 case TAPI_SIM_ACCESS_CARD_ERROR:
84                         error = SIM_ERROR_NOT_AVAILABLE;
85                         break;
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                 *icc_id = NULL;
112         } else {
113                 sync_gv = g_dbus_connection_call_sync(th->dbus_connection, DBUS_TELEPHONY_SERVICE, th->path,
114                                 DBUS_TELEPHONY_SIM_INTERFACE, "GetICCID", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, -1,
115                                 NULL, &gerr);
116
117                 if (sync_gv) {
118                         g_variant_get(sync_gv, "(is)", &result, &iccid);
119                         if (result == TAPI_SIM_ACCESS_SUCCESS) {
120                                 if (iccid != NULL && strlen(iccid) != 0) {
121                                         *icc_id = (char*) malloc(strlen(iccid) + 1);
122                                         if (*icc_id == NULL) {
123                                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
124                                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
125                                         } else {
126                                                 snprintf(*icc_id, strlen(iccid) + 1, "%s", iccid);
127                                         }
128                                 } else {
129                                         *icc_id = NULL;
130                                 }
131                         } else {
132                                 error_code = _convert_access_rt_to_sim_error(result);
133                                 *icc_id = NULL;
134                         }
135                 } else {
136                         LOGE("g_dbus_conn failed. error (%s)", gerr->message);
137                         g_error_free(gerr);
138                         error_code = SIM_ERROR_OPERATION_FAILED;
139                         *icc_id = NULL;
140                 }
141         }
142         tel_deinit(th);
143         return error_code;
144 }
145
146 int sim_get_mcc(char** mcc)
147 {
148         TelSimImsiInfo_t sim_imsi_info;
149         int error_code = SIM_ERROR_NONE;
150         int card_changed = 0;
151         TelSimCardStatus_t sim_card_state = 0x00;
152         struct tapi_handle *th = NULL;
153
154         SIM_CHECK_INPUT_PARAMETER(mcc);
155         SIM_INIT(th);
156
157         if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
158                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
159                 error_code = SIM_ERROR_NOT_AVAILABLE;
160                 *mcc = NULL;
161         } else {
162                 if (tel_get_sim_imsi(th, &sim_imsi_info) != 0) {
163                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
164                         error_code = SIM_ERROR_OPERATION_FAILED;
165                 } else {
166                         *mcc = (char*) malloc(sizeof(char) * (3 + 1));
167                         if (*mcc == NULL) {
168                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
169                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
170                         } else {
171                                 snprintf(*mcc, strlen(sim_imsi_info.szMcc) + 1, "%s", sim_imsi_info.szMcc);
172                         }
173                 }
174         }
175         tel_deinit(th);
176         return error_code;
177 }
178
179 int sim_get_mnc(char** mnc)
180 {
181         TelSimImsiInfo_t sim_imsi_info;
182         int error_code = SIM_ERROR_NONE;
183         int card_changed = 0;
184         TelSimCardStatus_t sim_card_state = 0x00;
185         struct tapi_handle *th = NULL;
186
187         SIM_CHECK_INPUT_PARAMETER(mnc);
188         SIM_INIT(th);
189
190         if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
191                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
192                 error_code = SIM_ERROR_NOT_AVAILABLE;
193                 *mnc = NULL;
194         } else {
195                 if (tel_get_sim_imsi(th, &sim_imsi_info) != 0) {
196                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
197                         error_code = SIM_ERROR_OPERATION_FAILED;
198                 } else {
199                         *mnc = (char*) malloc(sizeof(char) * (3 + 1));
200                         if (*mnc == NULL) {
201                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
202                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
203                         } else {
204                                 snprintf(*mnc, strlen(sim_imsi_info.szMnc) + 1, "%s", sim_imsi_info.szMnc);
205                         }
206                 }
207         }
208
209         tel_deinit(th);
210         return SIM_ERROR_NONE;
211 }
212
213 int sim_get_msin(char** msin)
214 {
215         TelSimImsiInfo_t sim_imsi_info;
216         int error_code = SIM_ERROR_NONE;
217         int card_changed = 0;
218         TelSimCardStatus_t sim_card_state = 0x00;
219         struct tapi_handle *th = NULL;
220
221         SIM_CHECK_INPUT_PARAMETER(msin);
222         SIM_INIT(th);
223
224         if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
225                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
226                 error_code = SIM_ERROR_NOT_AVAILABLE;
227                 *msin = NULL;
228         } else {
229                 if (tel_get_sim_imsi(th, &sim_imsi_info) != 0) {
230                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
231                         error_code = SIM_ERROR_OPERATION_FAILED;
232                 } else {
233                         *msin = (char*) malloc(sizeof(char) * (10 + 1));
234                         if (*msin == NULL) {
235                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
236                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
237                         } else {
238                                 snprintf(*msin, strlen(sim_imsi_info.szMsin) + 1, "%s", sim_imsi_info.szMsin);
239                         }
240                 }
241         }
242         tel_deinit(th);
243         return error_code;
244 }
245
246 int sim_get_spn(char** spn)
247 {
248         int error_code = SIM_ERROR_NONE;
249         int card_changed = 0;
250         TelSimCardStatus_t sim_card_state = 0x00;
251         struct tapi_handle *th = NULL;
252         GError *gerr = NULL;
253         GVariant *sync_gv = NULL;
254         TelSimAccessResult_t result = TAPI_SIM_ACCESS_SUCCESS;
255         gchar *spn_str = NULL;
256         guchar dc = 0;
257
258         SIM_CHECK_INPUT_PARAMETER(spn);
259         SIM_INIT(th);
260
261         if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
262                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
263                 error_code = SIM_ERROR_NOT_AVAILABLE;
264                 *spn = NULL;
265         } else {
266                 sync_gv = g_dbus_connection_call_sync(th->dbus_connection, DBUS_TELEPHONY_SERVICE, th->path,
267                                 DBUS_TELEPHONY_SIM_INTERFACE, "GetSpn", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, -1,
268                                 NULL, &gerr);
269
270                 if (sync_gv) {
271                         g_variant_get(sync_gv, "(iys)", &result, &dc, &spn_str);
272                         if (result == TAPI_SIM_ACCESS_SUCCESS) {
273                                 if (spn_str != NULL && strlen(spn_str) != 0) {
274                                         *spn = (char*) malloc(strlen(spn_str) + 1);
275                                         if (*spn == NULL) {
276                                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
277                                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
278                                         } else {
279                                                 snprintf(*spn, strlen(spn_str) + 1, "%s", spn_str);
280                                         }
281                                 } else {
282                                         *spn = NULL;
283                                 }
284                         } else {
285                                 error_code = _convert_access_rt_to_sim_error(result);
286                                 *spn = NULL;
287                         }
288                 } else {
289                         LOGE("g_dbus_conn failed. error (%s)", gerr->message);
290                         g_error_free(gerr);
291                         error_code = SIM_ERROR_OPERATION_FAILED;
292                         *spn = NULL;
293                 }
294         }
295         tel_deinit(th);
296         return error_code;
297 }
298
299 int sim_get_cphs_operator_name(char** full_name, char** short_name)
300 {
301         int error_code = SIM_ERROR_NONE;
302         int card_changed = 0;
303         TelSimCardStatus_t sim_card_state = 0x00;
304         struct tapi_handle *th = NULL;
305         GError *gerr = NULL;
306         GVariant *sync_gv = NULL;
307         TelSimAccessResult_t result = TAPI_SIM_ACCESS_SUCCESS;
308         gchar *full_str = NULL;
309         gchar *short_str = NULL;
310
311         SIM_CHECK_INPUT_PARAMETER(full_name);
312         SIM_CHECK_INPUT_PARAMETER(short_name);
313         SIM_INIT(th);
314
315         if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
316                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
317                 error_code = SIM_ERROR_NOT_AVAILABLE;
318                 *full_name = NULL;
319                 *short_name = NULL;
320         } else {
321                 sync_gv = g_dbus_connection_call_sync(th->dbus_connection, DBUS_TELEPHONY_SERVICE, th->path,
322                                 DBUS_TELEPHONY_SIM_INTERFACE, "GetCphsNetName", NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
323                                 -1, NULL, &gerr);
324
325                 if (sync_gv) {
326                         g_variant_get(sync_gv, "(iss)", &result, &full_str, &short_str);
327                         if (result == TAPI_SIM_ACCESS_SUCCESS) {
328                                 if (full_str != NULL && strlen(full_str) != 0) {
329                                         *full_name = (char*) malloc(strlen(full_str) + 1);
330                                         if (*full_name == NULL) {
331                                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
332                                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
333                                         } else {
334                                                 snprintf(*full_name, strlen(full_str) + 1, "%s", full_str);
335                                         }
336                                 } else {
337                                         *full_name = NULL;
338                                 }
339
340                                 if (short_str != NULL && strlen(short_str) != 0) {
341                                         *short_name = (char*) malloc(strlen(short_str) + 1);
342                                         if (*short_name == NULL) {
343                                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
344                                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
345                                         } else {
346                                                 snprintf(*short_name, strlen(short_str) + 1, "%s", short_str);
347                                         }
348                                 } else {
349                                         *short_name = NULL;
350                                 }
351                         } else {
352                                 error_code = _convert_access_rt_to_sim_error(result);
353                                 *full_name = NULL;
354                                 *short_name = NULL;
355                         }
356                 } else {
357                         LOGE("g_dbus_conn failed. error (%s)", gerr->message);
358                         g_error_free(gerr);
359                         error_code = SIM_ERROR_OPERATION_FAILED;
360                         *full_name = NULL;
361                         *short_name = NULL;
362                 }
363         }
364         tel_deinit(th);
365         return error_code;
366 }
367
368 int sim_get_state(sim_state_e* sim_state)
369 {
370         int card_changed = 0;
371         TelSimCardStatus_t sim_card_state = 0x00;
372         int error_code = SIM_ERROR_NONE;
373         struct tapi_handle *th = NULL;
374
375         SIM_CHECK_INPUT_PARAMETER(sim_state);
376         SIM_INIT(th);
377
378         if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0) {
379                 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
380                 error_code = SIM_ERROR_OPERATION_FAILED;
381         } else {
382                 switch (sim_card_state) {
383                         case TAPI_SIM_STATUS_CARD_ERROR:
384                                 *sim_state = SIM_STATE_UNAVAILABLE;
385                                 break;
386                         case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
387                                 *sim_state = SIM_STATE_UNAVAILABLE;
388                                 break;
389                         case TAPI_SIM_STATUS_SIM_INITIALIZING:
390                                 *sim_state = SIM_STATE_UNKNOWN;
391                                 break;
392                         case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
393                                 *sim_state = SIM_STATE_AVAILABLE;
394                                 break;
395                         case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
396                                 *sim_state = SIM_STATE_LOCKED;
397                                 break;
398                         case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
399                                 *sim_state = SIM_STATE_LOCKED;
400                                 break;
401                         case TAPI_SIM_STATUS_CARD_BLOCKED:
402                                 *sim_state = SIM_STATE_UNAVAILABLE;
403                                 break;
404                         case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
405                                 *sim_state = SIM_STATE_LOCKED;
406                                 break;
407                         case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
408                                 *sim_state = SIM_STATE_LOCKED;
409                                 break;
410                         case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
411                                 *sim_state = SIM_STATE_LOCKED;
412                                 break;
413                         case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
414                                 *sim_state = SIM_STATE_LOCKED;
415                                 break;
416                         case TAPI_SIM_STATUS_CARD_REMOVED:
417                                 *sim_state = SIM_STATE_UNAVAILABLE;
418                                 break;
419                         case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
420                                 *sim_state = SIM_STATE_LOCKED;
421                                 break;
422                         default:
423                                 *sim_state = SIM_STATE_UNAVAILABLE;
424                                 break;
425                 }
426         }
427
428         tel_deinit(th);
429         return error_code;
430 }
431
432 int sim_get_subscriber_number(char** subscriber_number)
433 {
434         int error_code = SIM_ERROR_NONE;
435         int card_changed = 0;
436         TelSimCardStatus_t sim_card_state = 0x00;
437         struct tapi_handle *th = NULL;
438         GError *gerr = NULL;
439         GVariant *sync_gv = NULL;
440         GVariant *value = NULL;
441         GVariantIter *iter = NULL;
442         GVariantIter *iter_row = NULL;
443         const gchar *key = NULL;
444         const gchar *str_value = NULL;
445         TelSimAccessResult_t result = TAPI_SIM_ACCESS_SUCCESS;
446         TelSimMsisdnList_t list;
447         int i = 0;
448
449         SIM_CHECK_INPUT_PARAMETER(subscriber_number);
450         SIM_INIT(th);
451
452         if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
453                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
454                 error_code = SIM_ERROR_NOT_AVAILABLE;
455                 *subscriber_number = NULL;
456         } else {
457                 sync_gv = g_dbus_connection_call_sync(th->dbus_connection, DBUS_TELEPHONY_SERVICE, th->path,
458                                 DBUS_TELEPHONY_SIM_INTERFACE, "GetMSISDN", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, -1,
459                                 NULL, &gerr);
460
461                 if (sync_gv) {
462                         memset(&list, 0, sizeof(TelSimMsisdnList_t));
463                         g_variant_get(sync_gv, "(iaa{sv})", &result, &iter);
464                         list.count = g_variant_iter_n_children(iter);
465
466                         if (result == TAPI_SIM_ACCESS_SUCCESS) {
467                                 i = 0;
468                                 while (g_variant_iter_next(iter, "a{sv}", &iter_row)) {
469                                         while (g_variant_iter_loop(iter_row, "{sv}", &key, &value)) {
470                                                 if (!g_strcmp0(key, "name")) {
471                                                         str_value = g_variant_get_string(value, NULL);
472                                                         snprintf(list.list[i].name, strlen(str_value) + 1, "%s", str_value);
473                                                 }
474                                                 if (!g_strcmp0(key, "number")) {
475                                                         str_value = g_variant_get_string(value, NULL);
476                                                         snprintf(list.list[i].num, strlen(str_value) + 1, "%s", str_value);
477                                                 }
478                                         }
479                                         i++;
480                                         g_variant_iter_free(iter_row);
481                                 }
482                                 g_variant_iter_free(iter);
483
484                                 if (list.list[0].num != NULL && strlen(list.list[0].num) != 0) {
485                                         *subscriber_number = (char*) malloc(strlen(list.list[0].num) + 1);
486                                         if (*subscriber_number == NULL) {
487                                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
488                                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
489                                         } else {
490                                                 snprintf(*subscriber_number, strlen(list.list[0].num) + 1, "%s",
491                                                                 list.list[0].num);
492                                         }
493                                 } else {
494                                         *subscriber_number = NULL;
495                                 }
496
497                         } else {
498                                 error_code = _convert_access_rt_to_sim_error(result);
499                                 *subscriber_number = NULL;
500                         }
501                 } else {
502                         LOGE("g_dbus_conn failed. error (%s)", gerr->message);
503                         g_error_free(gerr);
504                         error_code = SIM_ERROR_OPERATION_FAILED;
505                         *subscriber_number = NULL;
506                 }
507         }
508         tel_deinit(th);
509         return error_code;
510 }
511
512 static void on_noti_sim_status(struct tapi_handle *handle, const char *noti_id, void *data,
513                 void *user_data)
514 {
515         TelSimCardStatus_t *status = data;
516         sim_cb_data *ccb = user_data;
517         sim_state_e state = SIM_STATE_UNKNOWN;
518         sim_state_changed_cb cb;
519         LOGE("event(%s) receive with status[%d]", TAPI_NOTI_SIM_STATUS, *status);
520
521         switch (*status) {
522                 case TAPI_SIM_STATUS_CARD_ERROR:
523                         state = SIM_STATE_UNAVAILABLE;
524                         break;
525                 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
526                         state = SIM_STATE_UNAVAILABLE;
527                         break;
528                 case TAPI_SIM_STATUS_SIM_INITIALIZING:
529                         state = SIM_STATE_UNKNOWN;
530                         break;
531                 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
532                         state = SIM_STATE_AVAILABLE;
533                         break;
534                 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
535                         state = SIM_STATE_LOCKED;
536                         break;
537                 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
538                         state = SIM_STATE_LOCKED;
539                         break;
540                 case TAPI_SIM_STATUS_CARD_BLOCKED:
541                         state = SIM_STATE_UNAVAILABLE;
542                         break;
543                 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
544                         state = SIM_STATE_LOCKED;
545                         break;
546                 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
547                         state = SIM_STATE_LOCKED;
548                         break;
549                 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
550                         state = SIM_STATE_LOCKED;
551                         break;
552                 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
553                         state = SIM_STATE_LOCKED;
554                         break;
555                 case TAPI_SIM_STATUS_CARD_REMOVED:
556                         state = SIM_STATE_UNAVAILABLE;
557                         break;
558                 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
559                         state = SIM_STATE_LOCKED;
560                         break;
561                 default:
562                         state = SIM_STATE_UNAVAILABLE;
563                         break;
564         }
565         if (!ccb->cb) {
566                 LOGE("[%s] callback is null", __FUNCTION__);
567                 return;
568         }
569         cb = ccb->cb;
570         cb(state, ccb->user_data);
571 }
572
573 int sim_set_state_changed_cb(sim_state_changed_cb sim_cb, void* user_data)
574 {
575         int error_code = SIM_ERROR_NONE;
576         int ret;
577         sim_cb_data *ccb = NULL;
578
579         SIM_CHECK_INPUT_PARAMETER(sim_cb);
580
581         ghandle = tel_init(NULL);
582         if (!ghandle) {
583                 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
584                 return SIM_ERROR_OPERATION_FAILED;
585         }
586
587         ccb = (sim_cb_data*) calloc(sizeof(sim_cb_data), 1);
588         ccb->th = ghandle;
589         ccb->cb = (void*) sim_cb;
590         ccb->user_data = user_data;
591
592         ret = tel_register_noti_event(ghandle, TAPI_NOTI_SIM_STATUS, on_noti_sim_status, ccb);
593         if (ret != TAPI_API_SUCCESS) error_code = SIM_ERROR_OPERATION_FAILED;
594         return error_code;
595 }
596
597 int sim_unset_state_changed_cb()
598 {
599         int error_code = SIM_ERROR_NONE;
600         int ret;
601
602         if (ghandle == NULL) {
603                 ghandle = tel_init(NULL);
604                 if (!ghandle) {
605                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
606                         return SIM_ERROR_OPERATION_FAILED;
607                 }
608         }
609         ret = tel_deregister_noti_event(ghandle, TAPI_NOTI_SIM_STATUS);
610         if (ret != TAPI_API_SUCCESS) error_code = SIM_ERROR_OPERATION_FAILED;
611         return error_code;
612 }