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 && 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;
126 snprintf(*icc_id, strlen(iccid) + 1, "%s", iccid);
132 error_code = _convert_access_rt_to_sim_error(result);
136 LOGE("g_dbus_conn failed. error (%s)", gerr->message);
138 error_code = SIM_ERROR_OPERATION_FAILED;
146 int sim_get_mcc(char** mcc)
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;
154 SIM_CHECK_INPUT_PARAMETER(mcc);
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;
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;
166 *mcc = (char*) malloc(sizeof(char) * (3 + 1));
168 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
169 error_code = SIM_ERROR_OUT_OF_MEMORY;
171 snprintf(*mcc, strlen(sim_imsi_info.szMcc) + 1, "%s", sim_imsi_info.szMcc);
179 int sim_get_mnc(char** mnc)
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;
187 SIM_CHECK_INPUT_PARAMETER(mnc);
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;
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;
199 *mnc = (char*) malloc(sizeof(char) * (3 + 1));
201 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
202 error_code = SIM_ERROR_OUT_OF_MEMORY;
204 snprintf(*mnc, strlen(sim_imsi_info.szMnc) + 1, "%s", sim_imsi_info.szMnc);
210 return SIM_ERROR_NONE;
213 int sim_get_msin(char** msin)
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;
221 SIM_CHECK_INPUT_PARAMETER(msin);
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;
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;
233 *msin = (char*) malloc(sizeof(char) * (10 + 1));
235 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
236 error_code = SIM_ERROR_OUT_OF_MEMORY;
238 snprintf(*msin, strlen(sim_imsi_info.szMsin) + 1, "%s", sim_imsi_info.szMsin);
246 int sim_get_spn(char** spn)
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;
253 GVariant *sync_gv = NULL;
254 TelSimAccessResult_t result = TAPI_SIM_ACCESS_SUCCESS;
255 gchar *spn_str = NULL;
258 SIM_CHECK_INPUT_PARAMETER(spn);
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;
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,
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);
276 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
277 error_code = SIM_ERROR_OUT_OF_MEMORY;
279 snprintf(*spn, strlen(spn_str) + 1, "%s", spn_str);
285 error_code = _convert_access_rt_to_sim_error(result);
289 LOGE("g_dbus_conn failed. error (%s)", gerr->message);
291 error_code = SIM_ERROR_OPERATION_FAILED;
299 int sim_get_cphs_operator_name(char** full_name, char** short_name)
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;
306 GVariant *sync_gv = NULL;
307 TelSimAccessResult_t result = TAPI_SIM_ACCESS_SUCCESS;
308 gchar *full_str = NULL;
309 gchar *short_str = NULL;
311 SIM_CHECK_INPUT_PARAMETER(full_name);
312 SIM_CHECK_INPUT_PARAMETER(short_name);
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;
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,
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;
334 snprintf(*full_name, strlen(full_str) + 1, "%s", full_str);
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;
346 snprintf(*short_name, strlen(short_str) + 1, "%s", short_str);
352 error_code = _convert_access_rt_to_sim_error(result);
357 LOGE("g_dbus_conn failed. error (%s)", gerr->message);
359 error_code = SIM_ERROR_OPERATION_FAILED;
368 int sim_get_state(sim_state_e* sim_state)
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;
375 SIM_CHECK_INPUT_PARAMETER(sim_state);
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;
382 switch (sim_card_state) {
383 case TAPI_SIM_STATUS_CARD_ERROR:
384 *sim_state = SIM_STATE_UNAVAILABLE;
386 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
387 *sim_state = SIM_STATE_UNAVAILABLE;
389 case TAPI_SIM_STATUS_SIM_INITIALIZING:
390 *sim_state = SIM_STATE_UNKNOWN;
392 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
393 *sim_state = SIM_STATE_AVAILABLE;
395 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
396 *sim_state = SIM_STATE_LOCKED;
398 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
399 *sim_state = SIM_STATE_LOCKED;
401 case TAPI_SIM_STATUS_CARD_BLOCKED:
402 *sim_state = SIM_STATE_UNAVAILABLE;
404 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
405 *sim_state = SIM_STATE_LOCKED;
407 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
408 *sim_state = SIM_STATE_LOCKED;
410 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
411 *sim_state = SIM_STATE_LOCKED;
413 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
414 *sim_state = SIM_STATE_LOCKED;
416 case TAPI_SIM_STATUS_CARD_REMOVED:
417 *sim_state = SIM_STATE_UNAVAILABLE;
419 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
420 *sim_state = SIM_STATE_LOCKED;
423 *sim_state = SIM_STATE_UNAVAILABLE;
432 int sim_get_subscriber_number(char** subscriber_number)
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;
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;
449 SIM_CHECK_INPUT_PARAMETER(subscriber_number);
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;
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,
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);
466 if (result == TAPI_SIM_ACCESS_SUCCESS) {
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);
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);
480 g_variant_iter_free(iter_row);
482 g_variant_iter_free(iter);
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;
490 snprintf(*subscriber_number, strlen(list.list[0].num) + 1, "%s",
494 *subscriber_number = NULL;
498 error_code = _convert_access_rt_to_sim_error(result);
499 *subscriber_number = NULL;
502 LOGE("g_dbus_conn failed. error (%s)", gerr->message);
504 error_code = SIM_ERROR_OPERATION_FAILED;
505 *subscriber_number = NULL;
512 static void on_noti_sim_status(struct tapi_handle *handle, const char *noti_id, void *data,
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);
522 case TAPI_SIM_STATUS_CARD_ERROR:
523 state = SIM_STATE_UNAVAILABLE;
525 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
526 state = SIM_STATE_UNAVAILABLE;
528 case TAPI_SIM_STATUS_SIM_INITIALIZING:
529 state = SIM_STATE_UNKNOWN;
531 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
532 state = SIM_STATE_AVAILABLE;
534 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
535 state = SIM_STATE_LOCKED;
537 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
538 state = SIM_STATE_LOCKED;
540 case TAPI_SIM_STATUS_CARD_BLOCKED:
541 state = SIM_STATE_UNAVAILABLE;
543 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
544 state = SIM_STATE_LOCKED;
546 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
547 state = SIM_STATE_LOCKED;
549 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
550 state = SIM_STATE_LOCKED;
552 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
553 state = SIM_STATE_LOCKED;
555 case TAPI_SIM_STATUS_CARD_REMOVED:
556 state = SIM_STATE_UNAVAILABLE;
558 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
559 state = SIM_STATE_LOCKED;
562 state = SIM_STATE_UNAVAILABLE;
566 LOGE("[%s] callback is null", __FUNCTION__);
570 cb(state, ccb->user_data);
573 int sim_set_state_changed_cb(sim_state_changed_cb sim_cb, void* user_data)
575 int error_code = SIM_ERROR_NONE;
577 sim_cb_data *ccb = NULL;
579 SIM_CHECK_INPUT_PARAMETER(sim_cb);
581 ghandle = tel_init(NULL);
583 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
584 return SIM_ERROR_OPERATION_FAILED;
587 ccb = (sim_cb_data*) calloc(sizeof(sim_cb_data), 1);
589 ccb->cb = (void*) sim_cb;
590 ccb->user_data = user_data;
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;
597 int sim_unset_state_changed_cb()
599 int error_code = SIM_ERROR_NONE;
602 if (ghandle == NULL) {
603 ghandle = tel_init(NULL);
605 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
606 return SIM_ERROR_OPERATION_FAILED;
609 ret = tel_deregister_noti_event(ghandle, TAPI_NOTI_SIM_STATUS);
610 if (ret != TAPI_API_SUCCESS) error_code = SIM_ERROR_OPERATION_FAILED;