Git init
[framework/api/sim.git] / src / sim.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include <sim.h>
19 #include <TapiCommon.h>
20 #include <ITapiSim.h>
21 #include <vconf.h>
22 #include <vconf-keys.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <dlog.h>
27 #include <glib.h>
28
29 #ifdef LOG_TAG
30 #undef LOG_TAG
31 #endif
32 #define LOG_TAG "TIZEN_N_SIM"
33
34 typedef struct sim_cb_data
35 {
36         sim_state_e previous_state;
37         const void* cb;
38         void* user_data;
39 } sim_cb_data;
40
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;
45
46 // Callback function data
47 static sim_cb_data sim_state_cb = {SIM_STATE_UNKNOWN, NULL, NULL};
48
49 // Callback function adapter
50 void _sim_state_changed_cb_adapter(keynode_t *node, void* user_data);
51
52 // Internal Macros
53 #define SIM_CHECK_INPUT_PARAMETER(arg) \
54         if( arg == NULL ) \
55         { \
56                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, SIM_ERROR_INVALID_PARAMETER); \
57                 return SIM_ERROR_INVALID_PARAMETER; \
58         }
59
60 #define SIM_INIT() \
61         if( tel_init() != TAPI_API_SUCCESS ) \
62         { \
63                 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED); \
64                 return SIM_ERROR_OPERATION_FAILED; \
65         }
66
67
68 int sim_get_icc_id(char** icc_id)
69 {
70         TelSimIccIdInfo_t icc_data;
71         int error_code = SIM_ERROR_NONE;
72         int card_changed = 0;
73         TelSimCardStatus_t sim_card_state = 0x00;
74
75         SIM_CHECK_INPUT_PARAMETER(icc_id);
76         SIM_INIT();
77         
78         if( tel_get_sim_init_info(&sim_card_state, &card_changed) != 0 
79                 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED )
80         {
81                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);              
82                 error_code = SIM_ERROR_NOT_AVAILABLE;   
83         }
84         
85         else
86         {
87                 if( tel_get_sim_iccid(&icc_data) != 0 )
88                 {
89                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);                
90                         error_code = SIM_ERROR_OPERATION_FAILED;        
91                 }
92                 else
93                 {
94                         *icc_id = strndup(icc_data.icc_num, icc_data.icc_length);
95                         if( *icc_id == NULL )
96                         {
97                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
98                                 error_code = SIM_ERROR_OUT_OF_MEMORY; 
99                         }                       
100                 }
101         }
102
103         tel_deinit();
104         return error_code;
105 }
106
107
108 int sim_get_mcc(char** mcc)
109 {
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;
114
115         SIM_CHECK_INPUT_PARAMETER(mcc);
116         SIM_INIT();
117         
118         if( tel_get_sim_init_info(&sim_card_state, &card_changed) != 0 
119                 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED )
120         {
121                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);              
122                 error_code = SIM_ERROR_NOT_AVAILABLE;   
123         }
124         else
125         {
126                 if( tel_get_sim_imsi(&sim_imsi_info) != 0 )
127                 {
128                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);                
129                         error_code = SIM_ERROR_OPERATION_FAILED;        
130                 }
131                 else
132                 {
133                         *mcc = (char*)malloc(sizeof(char) * (TAPI_SIM_MCC_CODE_LEN+1));
134                         if( *mcc == NULL )
135                         {
136                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
137                                 error_code = SIM_ERROR_OUT_OF_MEMORY;                                                        
138                         }
139                         else
140                         {
141                                 strncpy(*mcc, sim_imsi_info.szMcc, TAPI_SIM_MCC_CODE_LEN+1);                            
142                         }
143                 }
144         }
145
146         tel_deinit();
147         return error_code;
148 }
149
150
151 int sim_get_mnc(char** mnc)
152 {
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;       
157
158         SIM_CHECK_INPUT_PARAMETER(mnc);
159         SIM_INIT();
160
161         if( tel_get_sim_init_info(&sim_card_state, &card_changed) != 0 
162                 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED )
163         {
164                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);              
165                 error_code = SIM_ERROR_NOT_AVAILABLE;   
166         }
167         else
168         {
169                 if( tel_get_sim_imsi(&sim_imsi_info) != 0 )
170                 {
171                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);                
172                         error_code = SIM_ERROR_OPERATION_FAILED;        
173                 }
174                 else
175                 {
176                         *mnc = (char*)malloc(sizeof(char) * (TAPI_SIM_MNC_CODE_LEN+1));
177                         if( *mnc == NULL )
178                         {
179                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
180                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
181                         }
182                         else
183                         {
184                                 strncpy(*mnc, sim_imsi_info.szMnc, TAPI_SIM_MNC_CODE_LEN+1);
185                         }
186                 }
187         }
188
189         tel_deinit();
190         return SIM_ERROR_NONE;
191 }
192
193
194 int sim_get_spn(char** spn)
195 {
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;
200
201         SIM_CHECK_INPUT_PARAMETER(spn);
202         SIM_INIT();
203
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 )
206         {
207                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);              
208                 error_code = SIM_ERROR_NOT_AVAILABLE;   
209         }
210         else
211         {
212                 service_provider_name = vconf_get_str(VCONFKEY_TELEPHONY_SPN_NAME);
213                 if( service_provider_name == NULL )
214                 {
215                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
216                         error_code = SIM_ERROR_OPERATION_FAILED;
217                 }
218                 else if( strlen(service_provider_name) == 0 )
219                 {
220                         LOGI("[%s] spn has no value", __FUNCTION__);
221                         free(service_provider_name);
222                         *spn = NULL;
223                 }
224                 else
225                 {
226                         *spn = service_provider_name;
227                 }
228         }
229
230         tel_deinit();
231         return error_code;
232 }
233
234
235 int sim_get_state(sim_state_e* sim_state)
236 {
237         int card_changed = 0;
238         TelSimCardStatus_t sim_card_state = 0x00;
239         int error_code = SIM_ERROR_NONE;
240
241         SIM_CHECK_INPUT_PARAMETER(sim_state);
242         SIM_INIT();
243
244         if( tel_get_sim_init_info(&sim_card_state, &card_changed) != 0 )
245         {
246                 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);                
247                 error_code = SIM_ERROR_OPERATION_FAILED;
248         }
249         else
250         {
251                 switch(sim_card_state)
252                 {
253                         case TAPI_SIM_STATUS_CARD_ERROR:
254                                 *sim_state = SIM_STATE_UNAVAILABLE;
255                                 break;
256                         case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
257                                 *sim_state = SIM_STATE_UNAVAILABLE;
258                                 break;
259                         case TAPI_SIM_STATUS_SIM_INITIALIZING:
260                                 *sim_state = SIM_STATE_UNKNOWN;
261                                 break;
262                         case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
263                                 *sim_state = SIM_STATE_AVAILABLE;
264                                 break;
265                         case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
266                                 *sim_state = SIM_STATE_LOCKED;
267                                 break;
268                         case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
269                                 *sim_state = SIM_STATE_LOCKED;
270                                 break;                          
271                         case TAPI_SIM_STATUS_CARD_BLOCKED:
272                                 *sim_state = SIM_STATE_UNAVAILABLE;
273                                 break;                          
274                         case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
275                                 *sim_state = SIM_STATE_LOCKED;
276                                 break;                          
277                         case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
278                                 *sim_state = SIM_STATE_LOCKED;
279                                 break;                          
280                         case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
281                                 *sim_state = SIM_STATE_LOCKED;
282                                 break;                          
283                         case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
284                                 *sim_state = SIM_STATE_LOCKED;
285                                 break;                          
286                         case TAPI_SIM_STATUS_CARD_REMOVED:
287                                 *sim_state = SIM_STATE_UNAVAILABLE;
288                                 break;                  
289                         case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
290                                 *sim_state = SIM_STATE_LOCKED;
291                                 break;
292                         default:
293                                 *sim_state = SIM_STATE_UNAVAILABLE;
294                                 break;                  
295                 }
296         }
297
298         tel_deinit();
299         return error_code;
300 }
301
302
303 int sim_get_subscriber_number(char** subscriber_number)
304 {
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;
309
310         SIM_CHECK_INPUT_PARAMETER(subscriber_number);
311         SIM_INIT();
312
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 )
315         {
316                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);              
317                 error_code = SIM_ERROR_NOT_AVAILABLE;   
318         }
319         else
320         {
321                 subscriber_number_p = vconf_get_str(VCONFKEY_TELEPHONY_SUBSCRIBER_NUMBER);
322                 if( subscriber_number_p == NULL )
323                 {
324                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
325                         error_code = SIM_ERROR_OPERATION_FAILED;
326                 }
327                 else if( strlen(subscriber_number_p) == 0 )     
328                 {
329                         LOGI("[%s] subscriber number has no value", __FUNCTION__);
330                         free(subscriber_number_p);
331                         *subscriber_number = NULL;
332                 }
333                 else
334                 {
335                         *subscriber_number = subscriber_number_p;
336                 }
337         }
338
339         tel_deinit();
340         return error_code;
341 }
342
343 int sim_set_state_changed_cb(sim_state_changed_cb sim_cb, void* user_data)
344 {
345         sim_state_e state = SIM_STATE_UNKNOWN;
346
347         SIM_CHECK_INPUT_PARAMETER(sim_cb);
348
349         if( sim_get_state(&state) != SIM_ERROR_NONE )
350         {
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;
353         }
354
355         if( init_is_registered == false) 
356         {               
357                 if( vconf_notify_key_changed(VCONFKEY_TELEPHONY_SIM_INIT, (vconf_callback_fn)_sim_state_changed_cb_adapter, NULL) != 0 )
358                 {
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;
362                 }
363                 init_is_registered = true;
364         }
365
366         if( chv_is_registered == false )
367         {
368                 if( vconf_notify_key_changed(VCONFKEY_TELEPHONY_SIM_CHV, (vconf_callback_fn)_sim_state_changed_cb_adapter, NULL) != 0)
369                 {
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;
373                 }
374                 chv_is_registered = true;               
375         }
376
377         if( slot_is_registered == false )
378         {
379                 if(vconf_notify_key_changed(VCONFKEY_TELEPHONY_SIM_SLOT, (vconf_callback_fn)_sim_state_changed_cb_adapter, NULL) != 0)
380                 {
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;
384                 }
385                 slot_is_registered = true;
386         }
387
388         sim_state_cb.previous_state = state;
389         sim_state_cb.cb = sim_cb;
390         sim_state_cb.user_data = user_data;
391
392         return SIM_ERROR_NONE;
393 }
394
395 int sim_unset_state_changed_cb()
396 {
397         if( init_is_registered == true) 
398         {               
399                 if( vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SIM_INIT, (vconf_callback_fn)_sim_state_changed_cb_adapter) != 0 )
400                 {
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;
404                 }
405                 init_is_registered = false;
406         }
407
408         if( chv_is_registered == true )
409         {
410                 if( vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SIM_CHV, (vconf_callback_fn)_sim_state_changed_cb_adapter) != 0)
411                 {
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;
415                 }
416                 chv_is_registered = false;              
417         }
418
419         if( slot_is_registered == true )
420         {
421                 if( vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SIM_SLOT, (vconf_callback_fn)_sim_state_changed_cb_adapter) != 0)
422                 {
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;
426                 }
427                 slot_is_registered = false;
428         }
429
430         sim_state_cb.previous_state = SIM_STATE_UNKNOWN;
431         sim_state_cb.cb = NULL;
432         sim_state_cb.user_data = NULL;
433
434         return SIM_ERROR_NONE;
435 }
436
437 void _sim_state_changed_cb_adapter(keynode_t *node, void* user_data) 
438 {
439         sim_state_e sim_state = SIM_STATE_UNKNOWN;
440
441         if( sim_state_cb.cb == NULL )
442         {
443                 return;
444         }
445
446         if( sim_get_state(&sim_state) == SIM_ERROR_NONE )
447         {
448                 if( sim_state != sim_state_cb.previous_state )
449                 {
450                         ((sim_state_changed_cb)(sim_state_cb.cb))(sim_state, sim_state_cb.user_data);
451                         sim_state_cb.previous_state = sim_state;
452                 }
453         }
454 }
455
456
457
458
459