Remove trivial unnecessary build dependency
[apps/core/preloaded/lockscreen.git] / src / sim.c
1 /*
2  * Copyright (c) 2016 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 #include "log.h"
18 #include "util.h"
19 #include "sim.h"
20
21 #include <telephony/telephony.h>
22 #include <Ecore.h>
23
24 static telephony_handle_list_s handle_list;
25 static char *sim_plmn[LOCKSCREEN_SIM_MAX];
26 static int init_count;
27 int LOCKSCREEN_EVENT_SIM_STATUS_CHANGED;
28
29 static const telephony_noti_e notis[] = {
30         TELEPHONY_NOTI_SIM_STATUS,
31         TELEPHONY_NOTI_NETWORK_NETWORK_NAME,
32         TELEPHONY_NOTI_NETWORK_SERVICE_STATE,
33 };
34
35 static char *_sim_plmn_get(telephony_h handle)
36 {
37         char *network_name;
38
39         int ret = telephony_network_get_network_name(handle, &network_name);
40         if (ret != TELEPHONY_ERROR_NONE) {
41                 ERR("telephony_network_get_network_name failed: %s", get_error_message(ret));
42                 return NULL;
43         }
44
45         return network_name;
46 }
47
48 static char *_sim_spn_get(telephony_h handle)
49 {
50         char *spn_name;
51
52         int ret = telephony_sim_get_spn(handle, &spn_name);
53         if (ret != TELEPHONY_ERROR_NONE) {
54                 ERR("telephony_sim_get_spn failed: %s", get_error_message(ret));
55                 return NULL;
56         }
57         return spn_name;
58 }
59
60 static char *_sim_state_text_for_sim_get(telephony_h handle)
61 {
62         int ret;
63         telephony_network_service_state_e service_state;
64         telephony_network_name_option_e name_option;
65
66         char *plmn = NULL;
67         char *spn = NULL;
68         char buf[1024] = { 0, };
69
70         /* get service state */
71         ret = telephony_network_get_service_state(handle, &service_state);
72         if (ret != TELEPHONY_ERROR_NONE) {
73                 ERR("telephony_network_get_service_state failed: %s", get_error_message(ret));
74                 return NULL;
75         }
76
77         switch (service_state) {
78         case TELEPHONY_NETWORK_SERVICE_STATE_IN_SERVICE:
79                 /* get network name option */
80                 ret = telephony_network_get_network_name_option(handle, &name_option);
81                 if (ret != TELEPHONY_ERROR_NONE) {
82                         ERR("telephony_network_get_network_name_option failed: %s", get_error_message(ret));
83                         return NULL;
84                 }
85
86                 switch (name_option) {
87                 case TELEPHONY_NETWORK_NAME_OPTION_SPN:
88                         spn = _sim_spn_get(handle);
89                         if (spn != NULL && spn[0] != 0) {
90                                 INF("PLMN/SPN - Sim %p using SPN: %s", handle, spn);
91                                 snprintf(buf, sizeof(buf), "%s", spn);
92                         }
93                         break;
94                 case TELEPHONY_NETWORK_NAME_OPTION_NETWORK:
95                         plmn = _sim_plmn_get(handle);
96                         if (plmn != NULL && plmn[0] != 0) {
97                                 INF("PLMN/SPN - Sim %p using PLMN: %s", handle, plmn);
98                                 snprintf(buf, sizeof(buf), "%s", plmn);
99                         }
100                         break;
101                 case TELEPHONY_NETWORK_NAME_OPTION_ANY:
102                         spn = _sim_spn_get(handle);
103                         plmn = _sim_plmn_get(handle);
104                         if (spn != NULL && spn[0] != 0 && plmn != NULL && plmn[0] != 0) {
105                                 INF("PLMN/SPN - Sim %p using SPN: %s, PLMN: %s", handle, spn, plmn);
106                                 snprintf(buf, sizeof(buf), "%s - %s", plmn, spn);
107                         } else if (spn != NULL && spn[0] != 0) {
108                                 INF("PLMN/SPN - Sim %p using SPN: %s", handle, spn);
109                                 snprintf(buf, sizeof(buf), "%s", spn);
110                         } else if (plmn != NULL && plmn[0] != 0) {
111                                 INF("PLMN/SPN - Sim %p using PLMN: %s", handle, plmn);
112                                 snprintf(buf, sizeof(buf), "%s", plmn);
113                         }
114                         break;
115                 default:
116                         ERR("Invalid name option[%d]", name_option);
117                         plmn = _sim_plmn_get(handle);
118                         if (plmn != NULL && plmn[0] != 0) {
119                                 INF("PLMN/SPN - Sim %p using PLMN: %s", handle, plmn);
120                                 snprintf(buf, sizeof(buf), "%s", plmn);
121                         }
122                         break;
123                 }
124                 break;
125         case TELEPHONY_NETWORK_SERVICE_STATE_OUT_OF_SERVICE:
126                 snprintf(buf, sizeof(buf), "%s", _("IDS_COM_BODY_NO_SERVICE"));
127                 break;
128         case TELEPHONY_NETWORK_SERVICE_STATE_EMERGENCY_ONLY:
129                 snprintf(buf, sizeof(buf), "%s", _("IDS_IDLE_MBODY_EMERGENCY_CALLS_ONLY"));
130                 break;
131         default:
132                 snprintf(buf, sizeof(buf), "%s", _("IDS_COM_BODY_NO_SERVICE"));
133                 break;
134         }
135
136         return strdup(buf);
137 }
138
139 static void _update_sim_info(bool emit)
140 {
141         int i;
142         telephony_sim_state_e state;
143
144         for (i = 0; (i < handle_list.count) && (i < LOCKSCREEN_SIM_MAX); i++)
145         {
146                 char *out = NULL;
147
148                 int ret = telephony_sim_get_state(handle_list.handle[i], &state);
149                 if (ret != TELEPHONY_ERROR_NONE)
150                         continue;
151
152                 if (state == TELEPHONY_SIM_STATE_UNAVAILABLE)
153                         out = NULL;
154                 else {
155                         out = _sim_state_text_for_sim_get(handle_list.handle[i]);
156                 }
157
158                 free(sim_plmn[i]);
159                 sim_plmn[i] = out;
160         }
161
162         if (emit) ecore_event_add(LOCKSCREEN_EVENT_SIM_STATUS_CHANGED, NULL, NULL, NULL);
163 }
164
165 static void _on_telephony_state_changed_cb(telephony_state_e state, void *user_data)
166 {
167         _update_sim_info(true);
168 }
169
170 static void _on_sim_info_changed_cb(telephony_h handle, telephony_noti_e noti_id, void *data, void *user_data)
171 {
172         _update_sim_info(true);
173 }
174
175 int lockscreen_sim_init(void)
176 {
177         int i;
178         if (!init_count) {
179                 LOCKSCREEN_EVENT_SIM_STATUS_CHANGED = ecore_event_type_new();
180                 int ret = telephony_init(&handle_list);
181                 if (ret != TELEPHONY_ERROR_NONE)
182                 {
183                         ERR("telephony_init failed: %s", get_error_message(ret));
184                         return -1;
185                 }
186
187                 ret = telephony_set_state_changed_cb(_on_telephony_state_changed_cb, NULL);
188                 if (ret != TELEPHONY_ERROR_NONE) {
189                         telephony_deinit(&handle_list);
190                         ERR("telephony_set_state_changed_cb failed: %s", get_error_message(ret));
191                         return -1;
192                 }
193
194                 for (i = 0; (i < handle_list.count) && (i < LOCKSCREEN_SIM_MAX); i++)
195                 {
196                         int j;
197                         for (j = 0; j < SIZE(notis); j++)
198                         {
199                                 ret = telephony_set_noti_cb(handle_list.handle[i], notis[j], _on_sim_info_changed_cb, NULL);
200                                 if (ret != TELEPHONY_ERROR_NONE) {
201                                         ERR("telephony_set_noti_cb failed: %s", get_error_message(ret));
202                                 }
203                         }
204                 }
205
206                 _update_sim_info(false);
207         }
208         init_count++;
209         return 0;
210 }
211
212 void lockscreen_sim_shutdown(void)
213 {
214         if (init_count) {
215                 init_count--;
216                 if (!init_count) {
217                         int i;
218                         for (i = 0; i < LOCKSCREEN_SIM_MAX; i++) {
219                                 free(sim_plmn[i]);
220                                 sim_plmn[i] = NULL;
221                         }
222                         telephony_deinit(&handle_list);
223                         telephony_unset_state_changed_cb(_on_telephony_state_changed_cb);
224                 }
225         }
226 }
227
228 const char *lockscreen_sim_get_plmn(lockscreen_sim_num_e num)
229 {
230         return sim_plmn[num];
231 }