2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <tapi_common.h>
19 #include <TapiUtility.h>
28 #include <glib-object.h>
34 #define LOG_TAG "TIZEN_N_SIM"
37 gpointer dbus_connection;
44 typedef struct sim_cb_data {
45 sim_state_e previous_state;
46 struct tapi_handle *th;
51 static struct tapi_handle *ghandle = NULL;
54 #define SIM_CHECK_INPUT_PARAMETER(arg) \
57 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, SIM_ERROR_INVALID_PARAMETER); \
58 return SIM_ERROR_INVALID_PARAMETER; \
61 #define SIM_INIT(th) \
62 th = tel_init(NULL); \
64 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED); \
65 return SIM_ERROR_OPERATION_FAILED; \
68 #define SIM_MAKE_CB(ccb,th,callback,user_data) \
69 ccb = (sim_cb_data*) calloc(sizeof(sim_cb_data), 1);\
71 ccb->cb = (void*) callback;\
72 ccb->user_data = user_data
74 static sim_error_e _convert_access_rt_to_sim_error(TelSimAccessResult_t access_rt)
76 sim_error_e error = SIM_ERROR_NONE;
78 case TAPI_SIM_ACCESS_SUCCESS:
79 case TAPI_SIM_ACCESS_FILE_NOT_FOUND:
80 error = SIM_ERROR_NONE;
82 case TAPI_SIM_ACCESS_ACCESS_CONDITION_NOT_SATISFIED:
83 case TAPI_SIM_ACCESS_CARD_ERROR:
84 error = SIM_ERROR_NOT_AVAILABLE;
86 case TAPI_SIM_ACCESS_FAILED:
88 error = SIM_ERROR_OPERATION_FAILED;
94 int sim_get_icc_id(char** icc_id)
96 int error_code = SIM_ERROR_NONE;
98 TelSimCardStatus_t sim_card_state = 0x00;
99 struct tapi_handle *th = NULL;
101 GVariant *sync_gv = NULL;
103 TelSimAccessResult_t result = TAPI_SIM_ACCESS_SUCCESS;
105 SIM_CHECK_INPUT_PARAMETER(icc_id);
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;
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,
118 g_variant_get(sync_gv, "(is)", &result, &iccid);
119 if (result == TAPI_SIM_ACCESS_SUCCESS) {
120 if (iccid != NULL && iccid[0] != '\0') {
121 *icc_id = g_strdup(iccid);
126 error_code = _convert_access_rt_to_sim_error(result);
131 LOGE("g_dbus_conn failed. error (%s)", gerr->message);
133 error_code = SIM_ERROR_OPERATION_FAILED;
136 g_variant_unref(sync_gv);
142 int sim_get_mcc(char** mcc)
144 TelSimImsiInfo_t sim_imsi_info;
145 int error_code = SIM_ERROR_NONE;
146 int card_changed = 0;
147 TelSimCardStatus_t sim_card_state = 0x00;
148 struct tapi_handle *th = NULL;
150 SIM_CHECK_INPUT_PARAMETER(mcc);
153 if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
154 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
155 error_code = SIM_ERROR_NOT_AVAILABLE;
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;
162 *mcc = g_strdup(sim_imsi_info.szMcc);
169 int sim_get_mnc(char** mnc)
171 TelSimImsiInfo_t sim_imsi_info;
172 int error_code = SIM_ERROR_NONE;
173 int card_changed = 0;
174 TelSimCardStatus_t sim_card_state = 0x00;
175 struct tapi_handle *th = NULL;
177 SIM_CHECK_INPUT_PARAMETER(mnc);
180 if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
181 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
182 error_code = SIM_ERROR_NOT_AVAILABLE;
185 if (tel_get_sim_imsi(th, &sim_imsi_info) != 0) {
186 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
187 error_code = SIM_ERROR_OPERATION_FAILED;
189 *mnc = g_strdup(sim_imsi_info.szMnc);
193 return SIM_ERROR_NONE;
196 int sim_get_msin(char** msin)
198 TelSimImsiInfo_t sim_imsi_info;
199 int error_code = SIM_ERROR_NONE;
200 int card_changed = 0;
201 TelSimCardStatus_t sim_card_state = 0x00;
202 struct tapi_handle *th = NULL;
204 SIM_CHECK_INPUT_PARAMETER(msin);
207 if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
208 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
209 error_code = SIM_ERROR_NOT_AVAILABLE;
212 if (tel_get_sim_imsi(th, &sim_imsi_info) != 0) {
213 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
214 error_code = SIM_ERROR_OPERATION_FAILED;
216 *msin = g_strdup(sim_imsi_info.szMsin);
223 int sim_get_spn(char** spn)
225 int error_code = SIM_ERROR_NONE;
226 int card_changed = 0;
227 TelSimCardStatus_t sim_card_state = 0x00;
228 struct tapi_handle *th = NULL;
230 GVariant *sync_gv = NULL;
231 TelSimAccessResult_t result = TAPI_SIM_ACCESS_SUCCESS;
232 gchar *spn_str = NULL;
235 SIM_CHECK_INPUT_PARAMETER(spn);
238 if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
239 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
240 error_code = SIM_ERROR_NOT_AVAILABLE;
243 sync_gv = g_dbus_connection_call_sync(th->dbus_connection, DBUS_TELEPHONY_SERVICE, th->path,
244 DBUS_TELEPHONY_SIM_INTERFACE, "GetSpn", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, -1,
248 g_variant_get(sync_gv, "(iys)", &result, &dc, &spn_str);
249 if (result == TAPI_SIM_ACCESS_SUCCESS) {
250 if (spn_str != NULL && spn_str[0] != '\0') {
251 *spn = g_strdup(spn_str);
256 error_code = _convert_access_rt_to_sim_error(result);
261 LOGE("g_dbus_conn failed. error (%s)", gerr->message);
263 error_code = SIM_ERROR_OPERATION_FAILED;
266 g_variant_unref(sync_gv);
272 int sim_get_cphs_operator_name(char** full_name, char** short_name)
274 int error_code = SIM_ERROR_NONE;
275 int card_changed = 0;
276 TelSimCardStatus_t sim_card_state = 0x00;
277 struct tapi_handle *th = NULL;
279 GVariant *sync_gv = NULL;
280 TelSimAccessResult_t result = TAPI_SIM_ACCESS_SUCCESS;
281 gchar *full_str = NULL;
282 gchar *short_str = NULL;
284 SIM_CHECK_INPUT_PARAMETER(full_name);
285 SIM_CHECK_INPUT_PARAMETER(short_name);
288 if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
289 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
290 error_code = SIM_ERROR_NOT_AVAILABLE;
294 sync_gv = g_dbus_connection_call_sync(th->dbus_connection, DBUS_TELEPHONY_SERVICE, th->path,
295 DBUS_TELEPHONY_SIM_INTERFACE, "GetCphsNetName", NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
299 g_variant_get(sync_gv, "(iss)", &result, &full_str, &short_str);
300 if (result == TAPI_SIM_ACCESS_SUCCESS) {
301 if (full_str != NULL && full_str[0] != '\0') {
302 *full_name = g_strdup(full_str);
307 if (short_str != NULL && short_str[0] != '\0') {
308 *full_name = g_strdup(short_str);
313 error_code = _convert_access_rt_to_sim_error(result);
320 LOGE("g_dbus_conn failed. error (%s)", gerr->message);
322 error_code = SIM_ERROR_OPERATION_FAILED;
326 g_variant_unref(sync_gv);
332 int sim_get_state(sim_state_e* sim_state)
334 int card_changed = 0;
335 TelSimCardStatus_t sim_card_state = 0x00;
336 int error_code = SIM_ERROR_NONE;
337 struct tapi_handle *th = NULL;
339 SIM_CHECK_INPUT_PARAMETER(sim_state);
342 if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0) {
343 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
344 error_code = SIM_ERROR_OPERATION_FAILED;
346 switch (sim_card_state) {
347 case TAPI_SIM_STATUS_CARD_ERROR:
348 *sim_state = SIM_STATE_UNAVAILABLE;
350 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
351 *sim_state = SIM_STATE_UNAVAILABLE;
353 case TAPI_SIM_STATUS_SIM_INITIALIZING:
354 *sim_state = SIM_STATE_UNKNOWN;
356 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
357 *sim_state = SIM_STATE_AVAILABLE;
359 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
360 *sim_state = SIM_STATE_LOCKED;
362 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
363 *sim_state = SIM_STATE_LOCKED;
365 case TAPI_SIM_STATUS_CARD_BLOCKED:
366 *sim_state = SIM_STATE_UNAVAILABLE;
368 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
369 *sim_state = SIM_STATE_LOCKED;
371 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
372 *sim_state = SIM_STATE_LOCKED;
374 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
375 *sim_state = SIM_STATE_LOCKED;
377 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
378 *sim_state = SIM_STATE_LOCKED;
380 case TAPI_SIM_STATUS_CARD_REMOVED:
381 *sim_state = SIM_STATE_UNAVAILABLE;
383 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
384 *sim_state = SIM_STATE_LOCKED;
387 *sim_state = SIM_STATE_UNAVAILABLE;
396 int sim_get_subscriber_number(char** subscriber_number)
398 int error_code = SIM_ERROR_NONE;
399 int card_changed = 0;
400 TelSimCardStatus_t sim_card_state = 0x00;
401 struct tapi_handle *th = NULL;
403 GVariant *sync_gv = NULL;
404 GVariant *value = NULL;
405 GVariantIter *iter = NULL;
406 GVariantIter *iter_row = NULL;
407 const gchar *key = NULL;
408 const gchar *str_value = NULL;
409 TelSimAccessResult_t result = TAPI_SIM_ACCESS_SUCCESS;
410 TelSimMsisdnList_t list;
413 SIM_CHECK_INPUT_PARAMETER(subscriber_number);
416 if (tel_get_sim_init_info(th, &sim_card_state, &card_changed) != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
417 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
418 error_code = SIM_ERROR_NOT_AVAILABLE;
419 *subscriber_number = NULL;
421 sync_gv = g_dbus_connection_call_sync(th->dbus_connection, DBUS_TELEPHONY_SERVICE, th->path,
422 DBUS_TELEPHONY_SIM_INTERFACE, "GetMSISDN", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, -1,
426 memset(&list, 0, sizeof(TelSimMsisdnList_t));
427 g_variant_get(sync_gv, "(iaa{sv})", &result, &iter);
428 list.count = g_variant_iter_n_children(iter);
430 if (result == TAPI_SIM_ACCESS_SUCCESS) {
432 while (g_variant_iter_next(iter, "a{sv}", &iter_row)) {
433 while (g_variant_iter_loop(iter_row, "{sv}", &key, &value)) {
434 if (!g_strcmp0(key, "name")) {
435 str_value = g_variant_get_string(value, NULL);
436 snprintf(list.list[i].name, strlen(str_value) + 1, "%s", str_value);
438 if (!g_strcmp0(key, "number")) {
439 str_value = g_variant_get_string(value, NULL);
440 snprintf(list.list[i].num, strlen(str_value) + 1, "%s", str_value);
444 g_variant_iter_free(iter_row);
446 g_variant_iter_free(iter);
448 if (strlen(list.list[0].num) != 0) {
449 *subscriber_number = (char*) malloc(strlen(list.list[0].num) + 1);
450 if (*subscriber_number == NULL) {
451 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
452 error_code = SIM_ERROR_OUT_OF_MEMORY;
454 snprintf(*subscriber_number, strlen(list.list[0].num) + 1, "%s",
458 *subscriber_number = NULL;
462 error_code = _convert_access_rt_to_sim_error(result);
463 *subscriber_number = NULL;
466 LOGE("g_dbus_conn failed. error (%s)", gerr->message);
468 error_code = SIM_ERROR_OPERATION_FAILED;
469 *subscriber_number = NULL;
471 g_variant_unref(sync_gv);
477 static void on_noti_sim_status(struct tapi_handle *handle, const char *noti_id, void *data,
480 TelSimCardStatus_t *status = data;
481 sim_cb_data *ccb = user_data;
482 sim_state_e state = SIM_STATE_UNKNOWN;
483 sim_state_changed_cb cb;
484 LOGE("event(%s) receive with status[%d]", TAPI_NOTI_SIM_STATUS, *status);
487 case TAPI_SIM_STATUS_CARD_ERROR:
488 state = SIM_STATE_UNAVAILABLE;
490 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
491 state = SIM_STATE_UNAVAILABLE;
493 case TAPI_SIM_STATUS_SIM_INITIALIZING:
494 state = SIM_STATE_UNKNOWN;
496 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
497 state = SIM_STATE_AVAILABLE;
499 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
500 state = SIM_STATE_LOCKED;
502 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
503 state = SIM_STATE_LOCKED;
505 case TAPI_SIM_STATUS_CARD_BLOCKED:
506 state = SIM_STATE_UNAVAILABLE;
508 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
509 state = SIM_STATE_LOCKED;
511 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
512 state = SIM_STATE_LOCKED;
514 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
515 state = SIM_STATE_LOCKED;
517 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
518 state = SIM_STATE_LOCKED;
520 case TAPI_SIM_STATUS_CARD_REMOVED:
521 state = SIM_STATE_UNAVAILABLE;
523 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
524 state = SIM_STATE_LOCKED;
527 state = SIM_STATE_UNAVAILABLE;
531 LOGE("[%s] callback is null", __FUNCTION__);
535 cb(state, ccb->user_data);
538 int sim_set_state_changed_cb(sim_state_changed_cb sim_cb, void* user_data)
540 int error_code = SIM_ERROR_NONE;
542 sim_cb_data *ccb = NULL;
544 SIM_CHECK_INPUT_PARAMETER(sim_cb);
546 ghandle = tel_init(NULL);
548 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
549 return SIM_ERROR_OPERATION_FAILED;
552 ccb = (sim_cb_data*) calloc(sizeof(sim_cb_data), 1);
554 ccb->cb = (void*) sim_cb;
555 ccb->user_data = user_data;
557 ret = tel_register_noti_event(ghandle, TAPI_NOTI_SIM_STATUS, on_noti_sim_status, ccb);
558 if (ret != TAPI_API_SUCCESS) error_code = SIM_ERROR_OPERATION_FAILED;
562 int sim_unset_state_changed_cb()
564 int error_code = SIM_ERROR_NONE;
567 if (ghandle == NULL) {
568 ghandle = tel_init(NULL);
570 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
571 return SIM_ERROR_OPERATION_FAILED;
574 ret = tel_deregister_noti_event(ghandle, TAPI_NOTI_SIM_STATUS);
575 if (ret != TAPI_API_SUCCESS) error_code = SIM_ERROR_OPERATION_FAILED;