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 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_spn(char** spn)
196 int error_code = SIM_ERROR_NONE;
197 int card_changed = 0;
198 TelSimCardStatus_t sim_card_state = 0x00;
199 char* service_provider_name = NULL;
201 SIM_CHECK_INPUT_PARAMETER(spn);
204 error_code = tel_get_sim_init_info(&sim_card_state, &card_changed);
205 if( error_code != 0 || 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 service_provider_name = vconf_get_str(VCONFKEY_TELEPHONY_SPN_NAME);
213 if( service_provider_name == NULL )
215 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
216 error_code = SIM_ERROR_OPERATION_FAILED;
218 else if( strlen(service_provider_name) == 0 )
220 LOGI("[%s] spn has no value", __FUNCTION__);
221 free(service_provider_name);
226 *spn = service_provider_name;
235 int sim_get_state(sim_state_e* sim_state)
237 int card_changed = 0;
238 TelSimCardStatus_t sim_card_state = 0x00;
239 int error_code = SIM_ERROR_NONE;
241 SIM_CHECK_INPUT_PARAMETER(sim_state);
244 if( tel_get_sim_init_info(&sim_card_state, &card_changed) != 0 )
246 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
247 error_code = SIM_ERROR_OPERATION_FAILED;
251 switch(sim_card_state)
253 case TAPI_SIM_STATUS_CARD_ERROR:
254 *sim_state = SIM_STATE_UNAVAILABLE;
256 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
257 *sim_state = SIM_STATE_UNAVAILABLE;
259 case TAPI_SIM_STATUS_SIM_INITIALIZING:
260 *sim_state = SIM_STATE_UNKNOWN;
262 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
263 *sim_state = SIM_STATE_AVAILABLE;
265 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
266 *sim_state = SIM_STATE_LOCKED;
268 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
269 *sim_state = SIM_STATE_LOCKED;
271 case TAPI_SIM_STATUS_CARD_BLOCKED:
272 *sim_state = SIM_STATE_UNAVAILABLE;
274 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
275 *sim_state = SIM_STATE_LOCKED;
277 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
278 *sim_state = SIM_STATE_LOCKED;
280 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
281 *sim_state = SIM_STATE_LOCKED;
283 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
284 *sim_state = SIM_STATE_LOCKED;
286 case TAPI_SIM_STATUS_CARD_REMOVED:
287 *sim_state = SIM_STATE_UNAVAILABLE;
289 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
290 *sim_state = SIM_STATE_LOCKED;
293 *sim_state = SIM_STATE_UNAVAILABLE;
303 int sim_get_subscriber_number(char** subscriber_number)
305 int error_code = SIM_ERROR_NONE;
306 int card_changed = 0;
307 TelSimCardStatus_t sim_card_state = 0x00;
308 char* subscriber_number_p = NULL;
310 SIM_CHECK_INPUT_PARAMETER(subscriber_number);
313 error_code = tel_get_sim_init_info(&sim_card_state, &card_changed);
314 if( error_code != 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 subscriber_number_p = vconf_get_str(VCONFKEY_TELEPHONY_SUBSCRIBER_NUMBER);
322 if( subscriber_number_p == NULL )
324 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
325 error_code = SIM_ERROR_OPERATION_FAILED;
327 else if( strlen(subscriber_number_p) == 0 )
329 LOGI("[%s] subscriber number has no value", __FUNCTION__);
330 free(subscriber_number_p);
331 *subscriber_number = NULL;
335 *subscriber_number = subscriber_number_p;
343 int sim_set_state_changed_cb(sim_state_changed_cb sim_cb, void* user_data)
345 sim_state_e state = SIM_STATE_UNKNOWN;
347 SIM_CHECK_INPUT_PARAMETER(sim_cb);
349 if( sim_get_state(&state) != SIM_ERROR_NONE )
351 LOGE("[%s] OPERATION_FAILED(0x%08x) : fail to get current state of SIM", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
352 return SIM_ERROR_OPERATION_FAILED;
355 if( init_is_registered == false)
357 if( vconf_notify_key_changed(VCONFKEY_TELEPHONY_SIM_INIT, (vconf_callback_fn)_sim_state_changed_cb_adapter, NULL) != 0 )
359 LOGE("[%s] OPERATION_FAILED(0x%08x) : fail to register callback of sim initialization",
360 __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
361 return SIM_ERROR_OPERATION_FAILED;
363 init_is_registered = true;
366 if( chv_is_registered == false )
368 if( vconf_notify_key_changed(VCONFKEY_TELEPHONY_SIM_CHV, (vconf_callback_fn)_sim_state_changed_cb_adapter, NULL) != 0)
370 LOGE("[%s] OPERATION_FAILED(0x%08x) : fail to register callback of several lock verification",
371 __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
372 return SIM_ERROR_OPERATION_FAILED;
374 chv_is_registered = true;
377 if( slot_is_registered == false )
379 if(vconf_notify_key_changed(VCONFKEY_TELEPHONY_SIM_SLOT, (vconf_callback_fn)_sim_state_changed_cb_adapter, NULL) != 0)
381 LOGE("[%s] OPERATION_FAILED(0x%08x) : fail to register callback of sim slot",
382 __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
383 return SIM_ERROR_OPERATION_FAILED;
385 slot_is_registered = true;
388 sim_state_cb.previous_state = state;
389 sim_state_cb.cb = sim_cb;
390 sim_state_cb.user_data = user_data;
392 return SIM_ERROR_NONE;
395 int sim_unset_state_changed_cb()
397 if( init_is_registered == true)
399 if( vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SIM_INIT, (vconf_callback_fn)_sim_state_changed_cb_adapter) != 0 )
401 LOGE("[%s] OPERATION_FAILED(0x%08x) : fail to unregister callback of sim initialization",
402 __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
403 return SIM_ERROR_OPERATION_FAILED;
405 init_is_registered = false;
408 if( chv_is_registered == true )
410 if( vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SIM_CHV, (vconf_callback_fn)_sim_state_changed_cb_adapter) != 0)
412 LOGE("[%s] OPERATION_FAILED(0x%08x) : fail to unregister callback of several lock verification",
413 __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
414 return SIM_ERROR_OPERATION_FAILED;
416 chv_is_registered = false;
419 if( slot_is_registered == true )
421 if( vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SIM_SLOT, (vconf_callback_fn)_sim_state_changed_cb_adapter) != 0)
423 LOGE("[%s] OPERATION_FAILED(0x%08x) : fail to unregister callback of sim slot",
424 __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
425 return SIM_ERROR_OPERATION_FAILED;
427 slot_is_registered = false;
430 sim_state_cb.previous_state = SIM_STATE_UNKNOWN;
431 sim_state_cb.cb = NULL;
432 sim_state_cb.user_data = NULL;
434 return SIM_ERROR_NONE;
437 void _sim_state_changed_cb_adapter(keynode_t *node, void* user_data)
439 sim_state_e sim_state = SIM_STATE_UNKNOWN;
441 if( sim_state_cb.cb == NULL )
446 if( sim_get_state(&sim_state) == SIM_ERROR_NONE )
448 if( sim_state != sim_state_cb.previous_state )
450 ((sim_state_changed_cb)(sim_state_cb.cb))(sim_state, sim_state_cb.user_data);
451 sim_state_cb.previous_state = sim_state;