upload tizen1.0 source
[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 static 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_msin(char** msin)
195 {
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;
200
201         SIM_CHECK_INPUT_PARAMETER(msin);
202         SIM_INIT();
203
204         if( tel_get_sim_init_info(&sim_card_state, &card_changed) != 0
205                 || 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                 if( tel_get_sim_imsi(&sim_imsi_info) != 0 )
213                 {
214                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
215                         error_code = SIM_ERROR_OPERATION_FAILED;
216                 }
217                 else
218                 {
219                         *msin = (char*)malloc(sizeof(char) * (TAPI_SIM_MSIN_CODE_LEN+1));
220                         if( *msin == NULL )
221                         {
222                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
223                                 error_code = SIM_ERROR_OUT_OF_MEMORY;
224                         }
225                         else
226                         {
227                                 strncpy(*msin, sim_imsi_info.szMsin, TAPI_SIM_MSIN_CODE_LEN+1);
228                         }
229                 }
230         }
231
232         tel_deinit();
233         return error_code;
234 }
235
236
237 int sim_get_spn(char** spn)
238 {
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;
243
244         SIM_CHECK_INPUT_PARAMETER(spn);
245         SIM_INIT();
246
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 )
249         {
250                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);              
251                 error_code = SIM_ERROR_NOT_AVAILABLE;   
252         }
253         else
254         {
255                 service_provider_name = vconf_get_str(VCONFKEY_TELEPHONY_SPN_NAME);
256                 if( service_provider_name == NULL )
257                 {
258                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
259                         error_code = SIM_ERROR_OPERATION_FAILED;
260                 }
261                 else if( strlen(service_provider_name) == 0 )
262                 {
263                         LOGI("[%s] spn has no value", __FUNCTION__);
264                         free(service_provider_name);
265                         *spn = NULL;
266                 }
267                 else
268                 {
269                         *spn = service_provider_name;
270                 }
271         }
272
273         tel_deinit();
274         return error_code;
275 }
276
277
278 int sim_get_cphs_operator_name(char** full_name, char** short_name)
279 {
280         TelSimCphsLocalInfo_t cphs_info;
281         int error_code = SIM_ERROR_NONE;
282         int card_changed = 0;
283         TelSimCardStatus_t sim_card_state = 0x00;
284
285         SIM_CHECK_INPUT_PARAMETER(full_name);
286         SIM_CHECK_INPUT_PARAMETER(short_name);
287         SIM_INIT();
288
289         if( tel_get_sim_init_info(&sim_card_state, &card_changed) != 0
290                 || sim_card_state != TAPI_SIM_STATUS_SIM_INIT_COMPLETED )
291         {
292                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);
293                 error_code = SIM_ERROR_NOT_AVAILABLE;
294         }
295         else
296         {
297                 if( tel_get_sim_cphs_info(&cphs_info) != 0 )
298                 {
299                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
300                         error_code = SIM_ERROR_OPERATION_FAILED;
301                 }
302                 else
303                 {
304                         if(cphs_info.opname.NameLength)
305                         {
306                                 *full_name = strndup((const char*)cphs_info.opname.OperatorName, cphs_info.opname.NameLength);
307                                 if(*full_name == NULL)
308                                 {
309                                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
310                                         error_code = SIM_ERROR_OUT_OF_MEMORY;
311                                 }
312                         }
313                         else
314                         {
315                                 *full_name = NULL;
316                         }
317
318                         if(cphs_info.opshortform.ShortNameLength)
319                         {
320                                 *short_name = strndup((const char*)cphs_info.opshortform.OperatorShortName, cphs_info.opshortform.ShortNameLength);
321                                 if(*short_name == NULL)
322                                 {
323                                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, SIM_ERROR_OUT_OF_MEMORY);
324                                         error_code = SIM_ERROR_OUT_OF_MEMORY;
325                                 }
326                         }
327                         else
328                         {
329                                 *short_name = NULL;
330                         }
331                 }
332         }
333
334         tel_deinit();
335         return error_code;
336 }
337
338 int sim_get_state(sim_state_e* sim_state)
339 {
340         int card_changed = 0;
341         TelSimCardStatus_t sim_card_state = 0x00;
342         int error_code = SIM_ERROR_NONE;
343
344         SIM_CHECK_INPUT_PARAMETER(sim_state);
345         SIM_INIT();
346
347         if( tel_get_sim_init_info(&sim_card_state, &card_changed) != 0 )
348         {
349                 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);                
350                 error_code = SIM_ERROR_OPERATION_FAILED;
351         }
352         else
353         {
354                 switch(sim_card_state)
355                 {
356                         case TAPI_SIM_STATUS_CARD_ERROR:
357                                 *sim_state = SIM_STATE_UNAVAILABLE;
358                                 break;
359                         case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
360                                 *sim_state = SIM_STATE_UNAVAILABLE;
361                                 break;
362                         case TAPI_SIM_STATUS_SIM_INITIALIZING:
363                                 *sim_state = SIM_STATE_UNKNOWN;
364                                 break;
365                         case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
366                                 *sim_state = SIM_STATE_AVAILABLE;
367                                 break;
368                         case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
369                                 *sim_state = SIM_STATE_LOCKED;
370                                 break;
371                         case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
372                                 *sim_state = SIM_STATE_LOCKED;
373                                 break;                          
374                         case TAPI_SIM_STATUS_CARD_BLOCKED:
375                                 *sim_state = SIM_STATE_UNAVAILABLE;
376                                 break;                          
377                         case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
378                                 *sim_state = SIM_STATE_LOCKED;
379                                 break;                          
380                         case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
381                                 *sim_state = SIM_STATE_LOCKED;
382                                 break;                          
383                         case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
384                                 *sim_state = SIM_STATE_LOCKED;
385                                 break;                          
386                         case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
387                                 *sim_state = SIM_STATE_LOCKED;
388                                 break;                          
389                         case TAPI_SIM_STATUS_CARD_REMOVED:
390                                 *sim_state = SIM_STATE_UNAVAILABLE;
391                                 break;                  
392                         case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
393                                 *sim_state = SIM_STATE_LOCKED;
394                                 break;
395                         default:
396                                 *sim_state = SIM_STATE_UNAVAILABLE;
397                                 break;                  
398                 }
399         }
400
401         tel_deinit();
402         return error_code;
403 }
404
405
406 int sim_get_subscriber_number(char** subscriber_number)
407 {
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;
412
413         SIM_CHECK_INPUT_PARAMETER(subscriber_number);
414         SIM_INIT();
415
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 )
418         {
419                 LOGE("[%s] NOT_AVAILABLE(0x%08x)", __FUNCTION__, SIM_ERROR_NOT_AVAILABLE);              
420                 error_code = SIM_ERROR_NOT_AVAILABLE;   
421         }
422         else
423         {
424                 subscriber_number_p = vconf_get_str(VCONFKEY_TELEPHONY_SUBSCRIBER_NUMBER);
425                 if( subscriber_number_p == NULL )
426                 {
427                         LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, SIM_ERROR_OPERATION_FAILED);
428                         error_code = SIM_ERROR_OPERATION_FAILED;
429                 }
430                 else if( strlen(subscriber_number_p) == 0 )     
431                 {
432                         LOGI("[%s] subscriber number has no value", __FUNCTION__);
433                         free(subscriber_number_p);
434                         *subscriber_number = NULL;
435                 }
436                 else
437                 {
438                         *subscriber_number = subscriber_number_p;
439                 }
440         }
441
442         tel_deinit();
443         return error_code;
444 }
445
446 int sim_set_state_changed_cb(sim_state_changed_cb sim_cb, void* user_data)
447 {
448         sim_state_e state = SIM_STATE_UNKNOWN;
449
450         SIM_CHECK_INPUT_PARAMETER(sim_cb);
451
452         if( sim_get_state(&state) != SIM_ERROR_NONE )
453         {
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;
456         }
457
458         if( init_is_registered == false) 
459         {               
460                 if( vconf_notify_key_changed(VCONFKEY_TELEPHONY_SIM_INIT, (vconf_callback_fn)__sim_state_changed_cb_adapter, NULL) != 0 )
461                 {
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;
465                 }
466                 init_is_registered = true;
467         }
468
469         if( chv_is_registered == false )
470         {
471                 if( vconf_notify_key_changed(VCONFKEY_TELEPHONY_SIM_CHV, (vconf_callback_fn)__sim_state_changed_cb_adapter, NULL) != 0)
472                 {
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;
476                 }
477                 chv_is_registered = true;               
478         }
479
480         if( slot_is_registered == false )
481         {
482                 if(vconf_notify_key_changed(VCONFKEY_TELEPHONY_SIM_SLOT, (vconf_callback_fn)__sim_state_changed_cb_adapter, NULL) != 0)
483                 {
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;
487                 }
488                 slot_is_registered = true;
489         }
490
491         sim_state_cb.previous_state = state;
492         sim_state_cb.cb = sim_cb;
493         sim_state_cb.user_data = user_data;
494
495         return SIM_ERROR_NONE;
496 }
497
498 int sim_unset_state_changed_cb()
499 {
500         if( init_is_registered == true) 
501         {               
502                 if( vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SIM_INIT, (vconf_callback_fn)__sim_state_changed_cb_adapter) != 0 )
503                 {
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;
507                 }
508                 init_is_registered = false;
509         }
510
511         if( chv_is_registered == true )
512         {
513                 if( vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SIM_CHV, (vconf_callback_fn)__sim_state_changed_cb_adapter) != 0)
514                 {
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;
518                 }
519                 chv_is_registered = false;              
520         }
521
522         if( slot_is_registered == true )
523         {
524                 if( vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SIM_SLOT, (vconf_callback_fn)__sim_state_changed_cb_adapter) != 0)
525                 {
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;
529                 }
530                 slot_is_registered = false;
531         }
532
533         sim_state_cb.previous_state = SIM_STATE_UNKNOWN;
534         sim_state_cb.cb = NULL;
535         sim_state_cb.user_data = NULL;
536
537         return SIM_ERROR_NONE;
538 }
539
540 static void __sim_state_changed_cb_adapter(keynode_t *node, void* user_data) 
541 {
542         sim_state_e sim_state = SIM_STATE_UNKNOWN;
543
544         if( sim_state_cb.cb == NULL )
545         {
546                 return;
547         }
548
549         if( sim_get_state(&sim_state) == SIM_ERROR_NONE )
550         {
551                 if( sim_state != sim_state_cb.previous_state )
552                 {
553                         ((sim_state_changed_cb)(sim_state_cb.cb))(sim_state, sim_state_cb.user_data);
554                         sim_state_cb.previous_state = sim_state;
555                 }
556         }
557 }
558
559
560
561
562