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 error = SIM_ERROR_NONE;
81 case TAPI_SIM_ACCESS_FILE_NOT_FOUND:
82 case TAPI_SIM_ACCESS_ACCESS_CONDITION_NOT_SATISFIED:
83 error = SIM_ERROR_NOT_AVAILABLE;
85 case TAPI_SIM_ACCESS_CARD_ERROR:
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;
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,
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;
125 snprintf(*icc_id, strlen(iccid) + 1, "%s", iccid);
131 error_code = _convert_access_rt_to_sim_error(result);
134 LOGE("g_dbus_conn failed. error (%s)", gerr->message);
136 error_code = SIM_ERROR_OPERATION_FAILED;
143 int sim_get_mcc(char** mcc)
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;
151 SIM_CHECK_INPUT_PARAMETER(mcc);
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;
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 = (char*) malloc(sizeof(char) * (3 + 1));
164 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
165 error_code = SIM_ERROR_OUT_OF_MEMORY;
167 snprintf(*mcc, strlen(sim_imsi_info.szMcc) + 1, "%s", sim_imsi_info.szMcc);
175 int sim_get_mnc(char** mnc)
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;
183 SIM_CHECK_INPUT_PARAMETER(mnc);
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;
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;
195 *mnc = (char*) malloc(sizeof(char) * (3 + 1));
197 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
198 error_code = SIM_ERROR_OUT_OF_MEMORY;
200 snprintf(*mnc, strlen(sim_imsi_info.szMnc) + 1, "%s", sim_imsi_info.szMnc);
206 return SIM_ERROR_NONE;
209 int sim_get_msin(char** msin)
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;
217 SIM_CHECK_INPUT_PARAMETER(msin);
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;
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;
228 *msin = (char*) malloc(sizeof(char) * (10 + 1));
230 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
231 error_code = SIM_ERROR_OUT_OF_MEMORY;
233 snprintf(*msin, strlen(sim_imsi_info.szMsin) + 1, "%s", sim_imsi_info.szMsin);
241 int sim_get_spn(char** spn)
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;
248 GVariant *sync_gv = NULL;
249 TelSimAccessResult_t result = TAPI_SIM_ACCESS_SUCCESS;
250 gchar *spn_str = NULL;
253 SIM_CHECK_INPUT_PARAMETER(spn);
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;
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,
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);
271 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
272 error_code = SIM_ERROR_OUT_OF_MEMORY;
274 snprintf(*spn, strlen(spn_str) + 1, "%s", spn_str);
280 error_code = _convert_access_rt_to_sim_error(result);
283 LOGE("g_dbus_conn failed. error (%s)", gerr->message);
285 error_code = SIM_ERROR_OPERATION_FAILED;
292 int sim_get_cphs_operator_name(char** full_name, char** short_name)
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;
299 GVariant *sync_gv = NULL;
300 TelSimAccessResult_t result = TAPI_SIM_ACCESS_SUCCESS;
301 gchar *full_str = NULL;
302 gchar *short_str = NULL;
304 SIM_CHECK_INPUT_PARAMETER(full_name);
305 SIM_CHECK_INPUT_PARAMETER(short_name);
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;
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,
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;
326 snprintf(*full_name, strlen(full_str) + 1, "%s", full_str);
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;
338 snprintf(*short_name, strlen(short_str) + 1, "%s", short_str);
344 error_code = _convert_access_rt_to_sim_error(result);
347 LOGE("g_dbus_conn failed. error (%s)", gerr->message);
349 error_code = SIM_ERROR_OPERATION_FAILED;
356 int sim_get_state(sim_state_e* sim_state)
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;
363 SIM_CHECK_INPUT_PARAMETER(sim_state);
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;
370 switch (sim_card_state) {
371 case TAPI_SIM_STATUS_CARD_ERROR:
372 *sim_state = SIM_STATE_UNAVAILABLE;
374 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
375 *sim_state = SIM_STATE_UNAVAILABLE;
377 case TAPI_SIM_STATUS_SIM_INITIALIZING:
378 *sim_state = SIM_STATE_UNKNOWN;
380 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
381 *sim_state = SIM_STATE_AVAILABLE;
383 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
384 *sim_state = SIM_STATE_LOCKED;
386 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
387 *sim_state = SIM_STATE_LOCKED;
389 case TAPI_SIM_STATUS_CARD_BLOCKED:
390 *sim_state = SIM_STATE_UNAVAILABLE;
392 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
393 *sim_state = SIM_STATE_LOCKED;
395 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
396 *sim_state = SIM_STATE_LOCKED;
398 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
399 *sim_state = SIM_STATE_LOCKED;
401 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
402 *sim_state = SIM_STATE_LOCKED;
404 case TAPI_SIM_STATUS_CARD_REMOVED:
405 *sim_state = SIM_STATE_UNAVAILABLE;
407 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
408 *sim_state = SIM_STATE_LOCKED;
411 *sim_state = SIM_STATE_UNAVAILABLE;
420 int sim_get_subscriber_number(char** subscriber_number)
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;
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;
437 SIM_CHECK_INPUT_PARAMETER(subscriber_number);
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;
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,
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);
453 if (result == TAPI_SIM_ACCESS_SUCCESS) {
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);
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);
469 g_variant_iter_free(iter_row);
475 g_variant_iter_free(iter);
479 if (list.list[0].num != NULL && strlen(list.list[0].num) != 0) {
480 *subscriber_number = (char*) malloc(strlen(list.list[0].num) + 1);
481 if (*subscriber_number == NULL) {
482 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
483 error_code = SIM_ERROR_OUT_OF_MEMORY;
485 snprintf(*subscriber_number, strlen(list.list[0].num) + 1, "%s",
489 *subscriber_number = NULL;
493 error_code = _convert_access_rt_to_sim_error(result);
496 LOGE("g_dbus_conn failed. error (%s)", gerr->message);
498 error_code = SIM_ERROR_OPERATION_FAILED;
505 static void on_noti_sim_status(struct tapi_handle *handle, const char *noti_id, void *data,
508 TelSimCardStatus_t *status = data;
509 sim_cb_data *ccb = user_data;
510 sim_state_e state = SIM_STATE_UNKNOWN;
511 sim_state_changed_cb cb;
512 LOGE("event(%s) receive with status[%d]", TAPI_NOTI_SIM_STATUS, *status);
515 case TAPI_SIM_STATUS_CARD_ERROR:
516 state = SIM_STATE_UNAVAILABLE;
518 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
519 state = SIM_STATE_UNAVAILABLE;
521 case TAPI_SIM_STATUS_SIM_INITIALIZING:
522 state = SIM_STATE_UNKNOWN;
524 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
525 state = SIM_STATE_AVAILABLE;
527 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
528 state = SIM_STATE_LOCKED;
530 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
531 state = SIM_STATE_LOCKED;
533 case TAPI_SIM_STATUS_CARD_BLOCKED:
534 state = SIM_STATE_UNAVAILABLE;
536 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
537 state = SIM_STATE_LOCKED;
539 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
540 state = SIM_STATE_LOCKED;
542 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
543 state = SIM_STATE_LOCKED;
545 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
546 state = SIM_STATE_LOCKED;
548 case TAPI_SIM_STATUS_CARD_REMOVED:
549 state = SIM_STATE_UNAVAILABLE;
551 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
552 state = SIM_STATE_LOCKED;
555 state = SIM_STATE_UNAVAILABLE;
559 LOGE("[%s] callback is null", __FUNCTION__);
563 cb(state, ccb->user_data);
566 int sim_set_state_changed_cb(sim_state_changed_cb sim_cb, void* user_data)
568 int error_code = SIM_ERROR_NONE;
570 sim_cb_data *ccb = NULL;
572 SIM_CHECK_INPUT_PARAMETER(sim_cb);
574 ghandle = tel_init(NULL);
576 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
577 return SIM_ERROR_OPERATION_FAILED;
580 ccb = (sim_cb_data*) calloc(sizeof(sim_cb_data), 1);
582 ccb->cb = (void*) sim_cb;
583 ccb->user_data = user_data;
585 ret = tel_register_noti_event(ghandle, TAPI_NOTI_SIM_STATUS, on_noti_sim_status, ccb);
586 if (ret != TAPI_API_SUCCESS) error_code = SIM_ERROR_OPERATION_FAILED;
590 int sim_unset_state_changed_cb()
592 int error_code = SIM_ERROR_NONE;
595 if (ghandle == NULL) {
596 ghandle = tel_init(NULL);
598 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
599 return SIM_ERROR_OPERATION_FAILED;
602 ret = tel_deregister_noti_event(ghandle, TAPI_NOTI_SIM_STATUS);
603 if (ret != TAPI_API_SUCCESS) error_code = SIM_ERROR_OPERATION_FAILED;