Git init
[framework/api/sim.git] / test / sim_state_changed_test.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 <stdio.h>
19 #include <string.h>
20 #include <sim.h>
21 #include <dlog.h>
22 #include <glib.h>
23
24 #ifdef LOG_TAG
25 #undef LOG_TAG
26 #endif
27 #define LOG_TAG "TIZEN_N_SIM_TEST"
28
29 static GMainLoop *event_loop;
30
31 char* convert_state_to_string(sim_state_e sim_state)
32 {
33         switch(sim_state)
34         {
35                 case SIM_STATE_AVAILABLE:
36                         return "available";
37                 case SIM_STATE_LOCKED:
38                         return "locked";
39                 case SIM_STATE_UNKNOWN:
40                         return "unknown";
41                 case SIM_STATE_UNAVAILABLE:
42                         return "unavailable";
43                 default:
44                         return "unexpected";
45         }       
46 }
47
48 void sim_state_changed(sim_state_e state, void* user_data)
49 {
50         LOGI("[%s] Start sim_state_changed_cb", __FUNCTION__);
51
52         LOGI("[%s] Status of sim: %s", __FUNCTION__, convert_state_to_string(state));
53         LOGI("[%s] user data: %s", __FUNCTION__, user_data);
54
55         LOGI("[%s] End sim_state_changed_cb", __FUNCTION__);    
56         g_main_loop_quit(event_loop);
57 }
58
59 int main()
60 {
61         if( sim_set_state_changed_cb(sim_state_changed, "sim_state_changed_test") == SIM_ERROR_NONE )
62         {
63                 LOGI("[%s] Succeeded to add callback function", __FUNCTION__);
64         }
65         else
66         {
67                 LOGE("[%s] Failed to add callback function", __FUNCTION__);
68                 return -1;
69         }
70
71         LOGI("[%s] If you change the state of SIM card, then callback function will be called", __FUNCTION__);
72         event_loop = g_main_loop_new(NULL, FALSE);
73         g_main_loop_run(event_loop);
74
75         if( sim_unset_state_changed_cb() == SIM_ERROR_NONE )
76         {
77                 LOGI("[%s] Succeeded to remove callback function", __FUNCTION__);
78         }
79         else
80         {
81                 LOGE("[%s] Failed to remove callback function", __FUNCTION__);
82                 return -1;
83         }
84
85         return 0;
86 }