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.
19 #include <TapiCommon.h>
22 #include <vconf-keys.h>
32 #define LOG_TAG "TIZEN_N_SIM"
34 typedef struct sim_cb_data
36 sim_state_e previous_state;
41 // Whether vconf_notifiy_key_chnaged is registered or not
42 static bool init_is_registered = false;
43 static bool chv_is_registered = false;
44 static bool slot_is_registered = false;
46 // Callback function data
47 static sim_cb_data sim_state_cb = {SIM_STATE_UNKNOWN, NULL, NULL};
49 // Callback function adapter
50 static void __sim_state_changed_cb_adapter(keynode_t *node, void* user_data);
53 #define SIM_CHECK_INPUT_PARAMETER(arg) \
56 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, SIM_ERROR_INVALID_PARAMETER); \
57 return SIM_ERROR_INVALID_PARAMETER; \
61 if( tel_init() != TAPI_API_SUCCESS ) \
63 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED); \
64 return SIM_ERROR_OPERATION_FAILED; \
68 int sim_get_icc_id(char** icc_id)
70 TelSimIccIdInfo_t icc_data;
71 int error_code = SIM_ERROR_NONE;
73 TelSimCardStatus_t sim_card_state = 0x00;
75 SIM_CHECK_INPUT_PARAMETER(icc_id);
78 if( tel_get_sim_init_info(&sim_card_state, &card_changed) != 0
79 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED )
81 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
82 error_code = SIM_ERROR_NOT_AVAILABLE;
87 if( tel_get_sim_iccid(&icc_data) != 0 )
89 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
90 error_code = SIM_ERROR_OPERATION_FAILED;
94 *icc_id = strndup(icc_data.icc_num, icc_data.icc_length);
97 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
98 error_code = SIM_ERROR_OUT_OF_MEMORY;
108 int sim_get_mcc(char** mcc)
110 TelSimImsiInfo_t sim_imsi_info;
111 int error_code = SIM_ERROR_NONE;
112 int card_changed = 0;
113 TelSimCardStatus_t sim_card_state = 0x00;
115 SIM_CHECK_INPUT_PARAMETER(mcc);
118 if( tel_get_sim_init_info(&sim_card_state, &card_changed) != 0
119 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED )
121 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
122 error_code = SIM_ERROR_NOT_AVAILABLE;
126 if( tel_get_sim_imsi(&sim_imsi_info) != 0 )
128 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
129 error_code = SIM_ERROR_OPERATION_FAILED;
133 *mcc = (char*)malloc(sizeof(char) * (TAPI_SIM_MCC_CODE_LEN+1));
136 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
137 error_code = SIM_ERROR_OUT_OF_MEMORY;
141 strncpy(*mcc, sim_imsi_info.szMcc, TAPI_SIM_MCC_CODE_LEN+1);
151 int sim_get_mnc(char** mnc)
153 TelSimImsiInfo_t sim_imsi_info;
154 int error_code = SIM_ERROR_NONE;
155 int card_changed = 0;
156 TelSimCardStatus_t sim_card_state = 0x00;
158 SIM_CHECK_INPUT_PARAMETER(mnc);
161 if( tel_get_sim_init_info(&sim_card_state, &card_changed) != 0
162 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED )
164 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
165 error_code = SIM_ERROR_NOT_AVAILABLE;
169 if( tel_get_sim_imsi(&sim_imsi_info) != 0 )
171 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
172 error_code = SIM_ERROR_OPERATION_FAILED;
176 *mnc = (char*)malloc(sizeof(char) * (TAPI_SIM_MNC_CODE_LEN+1));
179 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
180 error_code = SIM_ERROR_OUT_OF_MEMORY;
184 strncpy(*mnc, sim_imsi_info.szMnc, TAPI_SIM_MNC_CODE_LEN+1);
190 return SIM_ERROR_NONE;
194 int sim_get_msin(char** msin)
196 TelSimImsiInfo_t sim_imsi_info;
197 int error_code = SIM_ERROR_NONE;
198 int card_changed = 0;
199 TelSimCardStatus_t sim_card_state = 0x00;
201 SIM_CHECK_INPUT_PARAMETER(msin);
204 if( tel_get_sim_init_info(&sim_card_state, &card_changed) != 0
205 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED )
207 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
208 error_code = SIM_ERROR_NOT_AVAILABLE;
212 if( tel_get_sim_imsi(&sim_imsi_info) != 0 )
214 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
215 error_code = SIM_ERROR_OPERATION_FAILED;
219 *msin = (char*)malloc(sizeof(char) * (TAPI_SIM_MSIN_CODE_LEN+1));
222 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
223 error_code = SIM_ERROR_OUT_OF_MEMORY;
227 strncpy(*msin, sim_imsi_info.szMsin, TAPI_SIM_MSIN_CODE_LEN+1);
237 int sim_get_spn(char** spn)
239 int error_code = SIM_ERROR_NONE;
240 int card_changed = 0;
241 TelSimCardStatus_t sim_card_state = 0x00;
242 char* service_provider_name = NULL;
244 SIM_CHECK_INPUT_PARAMETER(spn);
247 error_code = tel_get_sim_init_info(&sim_card_state, &card_changed);
248 if( error_code != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED )
250 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
251 error_code = SIM_ERROR_NOT_AVAILABLE;
255 service_provider_name = vconf_get_str(VCONFKEY_TELEPHONY_SPN_NAME);
256 if( service_provider_name == NULL )
258 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
259 error_code = SIM_ERROR_OPERATION_FAILED;
261 else if( strlen(service_provider_name) == 0 )
263 LOGI("[%s] spn has no value", __FUNCTION__);
264 free(service_provider_name);
269 *spn = service_provider_name;
278 int sim_get_cphs_operator_name(char** full_name, char** short_name)
280 TelSimCphsLocalInfo_t cphs_info;
281 int error_code = SIM_ERROR_NONE;
282 int card_changed = 0;
283 TelSimCardStatus_t sim_card_state = 0x00;
285 SIM_CHECK_INPUT_PARAMETER(full_name);
286 SIM_CHECK_INPUT_PARAMETER(short_name);
289 if( tel_get_sim_init_info(&sim_card_state, &card_changed) != 0
290 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED )
292 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
293 error_code = SIM_ERROR_NOT_AVAILABLE;
297 if( tel_get_sim_cphs_info(&cphs_info) != 0 )
299 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
300 error_code = SIM_ERROR_OPERATION_FAILED;
304 if(cphs_info.opname.NameLength)
306 *full_name = strndup((const char*)cphs_info.opname.OperatorName, cphs_info.opname.NameLength);
307 if(*full_name == NULL)
309 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
310 error_code = SIM_ERROR_OUT_OF_MEMORY;
318 if(cphs_info.opshortform.ShortNameLength)
320 *short_name = strndup((const char*)cphs_info.opshortform.OperatorShortName, cphs_info.opshortform.ShortNameLength);
321 if(*short_name == NULL)
323 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
324 error_code = SIM_ERROR_OUT_OF_MEMORY;
338 int sim_get_state(sim_state_e* sim_state)
340 int card_changed = 0;
341 TelSimCardStatus_t sim_card_state = 0x00;
342 int error_code = SIM_ERROR_NONE;
344 SIM_CHECK_INPUT_PARAMETER(sim_state);
347 if( tel_get_sim_init_info(&sim_card_state, &card_changed) != 0 )
349 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
350 error_code = SIM_ERROR_OPERATION_FAILED;
354 switch(sim_card_state)
356 case TAPI_SIM_STATUS_CARD_ERROR:
357 *sim_state = SIM_STATE_UNAVAILABLE;
359 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
360 *sim_state = SIM_STATE_UNAVAILABLE;
362 case TAPI_SIM_STATUS_SIM_INITIALIZING:
363 *sim_state = SIM_STATE_UNKNOWN;
365 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
366 *sim_state = SIM_STATE_AVAILABLE;
368 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
369 *sim_state = SIM_STATE_LOCKED;
371 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
372 *sim_state = SIM_STATE_LOCKED;
374 case TAPI_SIM_STATUS_CARD_BLOCKED:
375 *sim_state = SIM_STATE_UNAVAILABLE;
377 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
378 *sim_state = SIM_STATE_LOCKED;
380 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
381 *sim_state = SIM_STATE_LOCKED;
383 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
384 *sim_state = SIM_STATE_LOCKED;
386 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
387 *sim_state = SIM_STATE_LOCKED;
389 case TAPI_SIM_STATUS_CARD_REMOVED:
390 *sim_state = SIM_STATE_UNAVAILABLE;
392 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
393 *sim_state = SIM_STATE_LOCKED;
396 *sim_state = SIM_STATE_UNAVAILABLE;
406 int sim_get_subscriber_number(char** subscriber_number)
408 int error_code = SIM_ERROR_NONE;
409 int card_changed = 0;
410 TelSimCardStatus_t sim_card_state = 0x00;
411 char* subscriber_number_p = NULL;
413 SIM_CHECK_INPUT_PARAMETER(subscriber_number);
416 error_code = tel_get_sim_init_info(&sim_card_state, &card_changed);
417 if( error_code != 0 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED )
419 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
420 error_code = SIM_ERROR_NOT_AVAILABLE;
424 subscriber_number_p = vconf_get_str(VCONFKEY_TELEPHONY_SUBSCRIBER_NUMBER);
425 if( subscriber_number_p == NULL )
427 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
428 error_code = SIM_ERROR_OPERATION_FAILED;
430 else if( strlen(subscriber_number_p) == 0 )
432 LOGI("[%s] subscriber number has no value", __FUNCTION__);
433 free(subscriber_number_p);
434 *subscriber_number = NULL;
438 *subscriber_number = subscriber_number_p;
446 int sim_set_state_changed_cb(sim_state_changed_cb sim_cb, void* user_data)
448 sim_state_e state = SIM_STATE_UNKNOWN;
450 SIM_CHECK_INPUT_PARAMETER(sim_cb);
452 if( sim_get_state(&state) != SIM_ERROR_NONE )
454 LOGE("[%s] OPERATION_FAILED(0x%08x) : fail to get current state of SIM", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
455 return SIM_ERROR_OPERATION_FAILED;
458 if( init_is_registered == false)
460 if( vconf_notify_key_changed(VCONFKEY_TELEPHONY_SIM_INIT, (vconf_callback_fn)__sim_state_changed_cb_adapter, NULL) != 0 )
462 LOGE("[%s] OPERATION_FAILED(0x%08x) : fail to register callback of sim initialization",
463 __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
464 return SIM_ERROR_OPERATION_FAILED;
466 init_is_registered = true;
469 if( chv_is_registered == false )
471 if( vconf_notify_key_changed(VCONFKEY_TELEPHONY_SIM_CHV, (vconf_callback_fn)__sim_state_changed_cb_adapter, NULL) != 0)
473 LOGE("[%s] OPERATION_FAILED(0x%08x) : fail to register callback of several lock verification",
474 __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
475 return SIM_ERROR_OPERATION_FAILED;
477 chv_is_registered = true;
480 if( slot_is_registered == false )
482 if(vconf_notify_key_changed(VCONFKEY_TELEPHONY_SIM_SLOT, (vconf_callback_fn)__sim_state_changed_cb_adapter, NULL) != 0)
484 LOGE("[%s] OPERATION_FAILED(0x%08x) : fail to register callback of sim slot",
485 __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
486 return SIM_ERROR_OPERATION_FAILED;
488 slot_is_registered = true;
491 sim_state_cb.previous_state = state;
492 sim_state_cb.cb = sim_cb;
493 sim_state_cb.user_data = user_data;
495 return SIM_ERROR_NONE;
498 int sim_unset_state_changed_cb()
500 if( init_is_registered == true)
502 if( vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SIM_INIT, (vconf_callback_fn)__sim_state_changed_cb_adapter) != 0 )
504 LOGE("[%s] OPERATION_FAILED(0x%08x) : fail to unregister callback of sim initialization",
505 __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
506 return SIM_ERROR_OPERATION_FAILED;
508 init_is_registered = false;
511 if( chv_is_registered == true )
513 if( vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SIM_CHV, (vconf_callback_fn)__sim_state_changed_cb_adapter) != 0)
515 LOGE("[%s] OPERATION_FAILED(0x%08x) : fail to unregister callback of several lock verification",
516 __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
517 return SIM_ERROR_OPERATION_FAILED;
519 chv_is_registered = false;
522 if( slot_is_registered == true )
524 if( vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SIM_SLOT, (vconf_callback_fn)__sim_state_changed_cb_adapter) != 0)
526 LOGE("[%s] OPERATION_FAILED(0x%08x) : fail to unregister callback of sim slot",
527 __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
528 return SIM_ERROR_OPERATION_FAILED;
530 slot_is_registered = false;
533 sim_state_cb.previous_state = SIM_STATE_UNKNOWN;
534 sim_state_cb.cb = NULL;
535 sim_state_cb.user_data = NULL;
537 return SIM_ERROR_NONE;
540 static void __sim_state_changed_cb_adapter(keynode_t *node, void* user_data)
542 sim_state_e sim_state = SIM_STATE_UNKNOWN;
544 if( sim_state_cb.cb == NULL )
549 if( sim_get_state(&sim_state) == SIM_ERROR_NONE )
551 if( sim_state != sim_state_cb.previous_state )
553 ((sim_state_changed_cb)(sim_state_cb.cb))(sim_state, sim_state_cb.user_data);
554 sim_state_cb.previous_state = sim_state;