used boolean defines from stdbool.h
[apps/core/preloaded/indicator-win.git] / src / modules / connection / wifi.c
1 /*
2  *  Indicator
3  *
4  * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <vconf.h>
25 #include <wifi.h>
26
27 #include "common.h"
28 #include "indicator.h"
29 #include "icon.h"
30 #include "modules.h"
31 #include "main.h"
32 #include "util.h"
33 #include "log.h"
34
35 #define ICON_PRIORITY   INDICATOR_PRIORITY_FIXED6
36 #define MODULE_NAME             "wifi"
37 #define TIMER_INTERVAL  0.3
38
39 static int register_wifi_module(void *data);
40 static int unregister_wifi_module(void);
41 static int wake_up_cb(void *data);
42
43 icon_s wifi = {
44         .type = INDICATOR_IMG_ICON,
45         .name = MODULE_NAME,
46         .priority = ICON_PRIORITY,
47         .always_top = EINA_FALSE,
48         .exist_in_view = EINA_FALSE,
49         .img_obj = {0,},
50         .obj_exist = EINA_FALSE,
51         .area = INDICATOR_ICON_AREA_FIXED,
52         .init = register_wifi_module,
53         .fini = unregister_wifi_module,
54         .wake_up = wake_up_cb
55 };
56
57 enum {
58         LEVEL_WIFI_01 = 0,
59         LEVEL_WIFI_02,
60         LEVEL_WIFI_03,
61         LEVEL_WIFI_04,
62         LEVEL_WIFI_MAX
63 };
64
65 static const char *icon_path[LEVEL_WIFI_MAX] = {
66         [LEVEL_WIFI_01] = "Connection/B03_data_downloading_Wifi_01.png",
67         [LEVEL_WIFI_02] = "Connection/B03_data_downloading_Wifi_02.png",
68         [LEVEL_WIFI_03] = "Connection/B03_data_downloading_Wifi_03.png",
69         [LEVEL_WIFI_04] = "Connection/B03_data_downloading_Wifi_04.png",
70 };
71
72 static int transfer_state = -1;
73 static Eina_Bool wifi_transferring = EINA_FALSE;
74 static int updated_while_lcd_off = 0;
75 static int prevIndex = -1;
76
77
78
79 static void set_app_state(void* data)
80 {
81         wifi.ad = data;
82 }
83
84 static void show_image_icon(void *data, int index)
85 {
86         if(prevIndex == index)
87         {
88                 return;
89         }
90
91         wifi.img_obj.data = icon_path[index];
92         icon_show(&wifi);
93
94         prevIndex = index;
95         util_signal_emit(wifi.ad, "indicator.wifi.show", "indicator.prog");
96 }
97
98 static void hide_image_icon(void)
99 {
100         transfer_state = -1;
101
102         icon_hide(&wifi);
103
104         prevIndex = -1;
105         util_signal_emit(wifi.ad, "indicator.wifi.hide", "indicator.prog");
106         util_signal_emit(wifi.ad, "indicator.wifi.updown.hide", "indicator.prog");
107 }
108
109 static void show_wifi_transfer_icon(void* data)
110 {
111         int type = -1;
112         int status = 0;
113
114         if (vconf_get_int(VCONFKEY_WIFI_TRANSFER_STATE, &status) < 0)
115         {
116                 ERR("Error getting VCONFKEY_WIFI_TRANSFER_STATE value");
117                 return;
118         }
119
120         switch(status)
121         {
122         case VCONFKEY_WIFI_TRANSFER_STATE_TXRX://TX/RX BOTH
123                 type = TRANSFER_UPDOWN;
124                 break;
125         case VCONFKEY_WIFI_TRANSFER_STATE_TX:
126                 type = TRANSFER_UP;
127                 break;
128         case VCONFKEY_WIFI_TRANSFER_STATE_RX:
129                 type = TRANSFER_DOWN;
130                 break;
131         case VCONFKEY_WIFI_TRANSFER_STATE_NONE:
132                 type = TRANSFER_NONE;
133                 break;
134         default:
135                 break;
136         }
137
138         if(transfer_state==type)
139         {
140                 DBG("same transfer state");
141                 return;
142         }
143
144         transfer_state = type;
145         switch (type)
146         {
147                 case TRANSFER_NONE:
148                         util_signal_emit(wifi.ad, "indicator.wifi.updown.none", "indicator.prog");
149                         break;
150                 case TRANSFER_DOWN:
151                         util_signal_emit(wifi.ad, "indicator.wifi.updown.download", "indicator.prog");
152                         break;
153                 case TRANSFER_UP:
154                         util_signal_emit(wifi.ad, "indicator.wifi.updown.upload", "indicator.prog");
155                         break;
156                 case TRANSFER_UPDOWN:
157                         util_signal_emit(wifi.ad, "indicator.wifi.updown.updownload", "indicator.prog");
158                         break;
159                 default:
160                         break;
161
162         }
163 }
164
165 static void _wifi_changed_cb(keynode_t *node, void *data)
166 {
167         bool wifi_state = false;
168         int status, strength;
169         int ret;
170
171         ret_if(!data);
172
173         if (icon_get_update_flag()==0) {
174                 updated_while_lcd_off = 1;
175                 return;
176         }
177         updated_while_lcd_off = 0;
178
179         ret = vconf_get_int(VCONFKEY_WIFI_STRENGTH, &strength);
180         if (ret == OK) {
181                 if (strength < VCONFKEY_WIFI_STRENGTH_MIN) {
182                         strength = VCONFKEY_WIFI_STRENGTH_MIN;
183                 }
184                 else if (strength > VCONFKEY_WIFI_STRENGTH_MAX) {
185                         strength = VCONFKEY_WIFI_STRENGTH_MAX;
186                 }
187         } else {
188                 strength = VCONFKEY_WIFI_STRENGTH_MAX;
189         }
190
191         if (strength <= 0) strength = 1;
192
193         /* Second, check wifi status */
194         ret = wifi_is_activated(&wifi_state);
195         _D("wifi_state : %d", wifi_state);
196         if(ret != WIFI_ERROR_NONE) {
197                 _E("wifi_is_activated error. ret is [%d]", ret);
198         }
199
200         ret = wifi_get_connection_state(&status);
201         if (ret == WIFI_ERROR_NONE) {
202                 DBG("CONNECTION WiFi Status: %d", status);
203                 switch(status) {
204                 case WIFI_CONNECTION_STATE_CONNECTED:
205                         show_wifi_transfer_icon(data);
206                         show_image_icon(data, strength-1);
207                         break;
208                 default: //WIFI_CONNECTION_STATE_DISCONNECTED
209                         hide_image_icon();
210                         break;
211                 }
212         }
213
214         return;
215 }
216
217 static void _wifi_device_state_changed_cb(wifi_device_state_e state, void *user_data)
218 {
219         bool wifi_state = false;
220         int ret, strength;
221
222         ret_if(!user_data);
223
224         if (icon_get_update_flag()==0) {
225                 updated_while_lcd_off = 1;
226                 return;
227         }
228         updated_while_lcd_off = 0;
229
230         ret = vconf_get_int(VCONFKEY_WIFI_STRENGTH, &strength);
231         if (ret == OK) {
232                 if (strength < VCONFKEY_WIFI_STRENGTH_MIN) {
233                         strength = VCONFKEY_WIFI_STRENGTH_MIN;
234                 }
235                 else if (strength > VCONFKEY_WIFI_STRENGTH_MAX) {
236                         strength = VCONFKEY_WIFI_STRENGTH_MAX;
237                 }
238         } else {
239                 strength = VCONFKEY_WIFI_STRENGTH_MAX;
240         }
241
242         if (strength <= 0) strength = 1;
243
244         ret = wifi_is_activated(&wifi_state);
245         _D("wifi_state : %d", wifi_state);
246         if(ret != WIFI_ERROR_NONE) {
247                 _E("wifi_is_activated error. ret is [%d]", ret);
248         }
249
250         if (wifi_state) {
251                 show_wifi_transfer_icon(user_data);
252                 show_image_icon(user_data, strength-1);
253         } else {
254                 hide_image_icon();
255         }
256
257         return;
258 }
259
260 static void _wifi_connection_state_changed_cb(wifi_connection_state_e state, wifi_ap_h ap, void *user_data)
261 {
262         ret_if(!user_data);
263
264         _wifi_changed_cb(NULL, user_data);
265
266         return;
267 }
268
269 static int wake_up_cb(void *data)
270 {
271         if(updated_while_lcd_off==0)
272         {
273                 return OK;
274         }
275
276         _wifi_changed_cb(NULL, data);
277         return OK;
278 }
279
280 static bool _wifi_init(void)
281 {
282         int ret = -1;
283         ret = wifi_initialize();
284         if (ret != WIFI_ERROR_NONE) {
285                 _E("wifi_initialize is fail : %d", ret);
286                 return false;
287         }
288
289         return true;
290 }
291
292 static void _wifi_fini(void)
293 {
294         int ret = -1;
295         ret = wifi_deinitialize();
296         if (ret != WIFI_ERROR_NONE) {
297                 _E("wifi_deinitialize is fail : %d", ret);
298         }
299 }
300
301 static int register_wifi_module(void *data)
302 {
303         int r = 0, ret = -1;
304
305         retv_if(!data, 0);
306
307         set_app_state(data);
308         _wifi_init();
309
310         ret = wifi_set_device_state_changed_cb(_wifi_device_state_changed_cb, data);
311         if (ret != WIFI_ERROR_NONE) r = ret;
312
313         ret = wifi_set_connection_state_changed_cb(_wifi_connection_state_changed_cb, data);
314         if (ret != WIFI_ERROR_NONE) r = ret;
315
316         ret = vconf_notify_key_changed(VCONFKEY_WIFI_STRENGTH, _wifi_changed_cb, data);
317         if (ret != OK) r = r | ret;
318
319         ret = vconf_notify_key_changed(VCONFKEY_WIFI_TRANSFER_STATE, _wifi_changed_cb, data);
320         if (ret != OK) r = r | ret;
321
322         _wifi_changed_cb(NULL, data);
323
324         return r;
325 }
326
327 static int unregister_wifi_module(void)
328 {
329         int ret;
330
331         ret = wifi_unset_device_state_changed_cb();
332         ret = wifi_unset_connection_state_changed_cb();
333         ret = ret | vconf_ignore_key_changed(VCONFKEY_WIFI_STRENGTH, _wifi_changed_cb);
334         ret = ret | vconf_ignore_key_changed(VCONFKEY_WIFI_TRANSFER_STATE, _wifi_changed_cb);
335
336         if (wifi_transferring == EINA_TRUE) {
337                 wifi_transferring  = EINA_FALSE;
338         }
339
340         _wifi_fini();
341
342         return ret;
343 }
344 /* End of file */