6eb0fea09f2fa118dfb0e08fefdecb80f5279215
[platform/core/connectivity/net-config.git] / src / wifi-power.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2012-2013 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 #include <stdio.h>
21 #include <unistd.h>
22 #include <vconf.h>
23 #include <vconf-keys.h>
24 #include <wifi-direct.h>
25
26 #include "wifi.h"
27 #include "log.h"
28 #include "wifi.h"
29 #include "util.h"
30 #include "netdbus.h"
31 #include "neterror.h"
32 #include "netconfig.h"
33 #include "emulator.h"
34 #include "network-statistics.h"
35 #include "wifi-background-scan.h"
36 #include "wifi-power.h"
37 #include "wifi-state.h"
38 #include "mdm-private.h"
39 #include "wifi-agent.h"
40 #include "wifi-eap-config.h"
41
42
43 #define WLAN_DRIVER_SCRIPT "/usr/bin/wlan.sh"
44
45 static gboolean power_in_progress = FALSE;
46 static gboolean fm_waiting = FALSE;
47
48 static gboolean __netconfig_wifi_enable_technology(void)
49 {
50         DBusMessage *reply = NULL;
51         char param1[] = "string:Powered";
52         char param2[] = "variant:boolean:true";
53         char *param_array[] = {NULL, NULL, NULL};
54
55         param_array[0] = param1;
56         param_array[1] = param2;
57
58         reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE, CONNMAN_WIFI_TECHNOLOGY_PREFIX,
59                         CONNMAN_TECHNOLOGY_INTERFACE, "SetProperty", param_array);
60
61         if (reply == NULL) {
62                 ERR("Error! Request failed");
63                 return FALSE;
64         }
65
66         dbus_message_unref(reply);
67
68         return TRUE;
69 }
70
71 static gboolean __netconfig_wifi_disable_technology(void)
72 {
73         DBusMessage *reply = NULL;
74         char param1[] = "string:Powered";
75         char param2[] = "variant:boolean:false";
76         char *param_array[] = {NULL, NULL, NULL};
77
78         param_array[0] = param1;
79         param_array[1] = param2;
80
81         reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE, CONNMAN_WIFI_TECHNOLOGY_PREFIX,
82                         CONNMAN_TECHNOLOGY_INTERFACE, "SetProperty", param_array);
83
84         if (reply == NULL) {
85                 ERR("Error! Request failed");
86                 return FALSE;
87         }
88
89         dbus_message_unref(reply);
90
91         return TRUE;
92 }
93
94 static gboolean __netconfig_wifi_load_driver(void)
95 {
96         gboolean rv = FALSE;
97         const char *path = WLAN_DRIVER_SCRIPT;
98         char *const args[] = { "wlan.sh", "start", NULL };
99         char *const envs[] = { NULL };
100
101         if (netconfig_emulator_is_emulated() == TRUE)
102                 return rv;
103
104         rv = netconfig_execute_file(path, args, envs);
105         if (rv != TRUE) {
106                 DBG("Failed to load wireless device driver");
107                 return FALSE;
108         }
109
110         DBG("Successfully loaded wireless device driver");
111         return TRUE;
112 }
113
114 gboolean netconfig_wifi_remove_driver(void)
115 {
116         gboolean rv = FALSE;
117         const char *path = WLAN_DRIVER_SCRIPT;
118         char *const args[] = { "wlan.sh", "stop", NULL };
119         char *const env[] = { NULL };
120
121         if (netconfig_emulator_is_emulated() == TRUE)
122                 return rv;
123
124         rv = netconfig_execute_file(path, args, env);
125         if (rv != TRUE) {
126                 DBG("Failed to remove wireless device driver");
127                 return FALSE;
128         }
129
130         DBG("Successfully removed wireless device driver");
131         return TRUE;
132 }
133
134 static int __netconfig_wifi_try_to_load_driver(void);
135 static gboolean __netconfig_wifi_try_to_remove_driver(void);
136
137 void netconfig_wifi_notify_power_completed(gboolean power_on)
138 {
139         DBusMessage *signal;
140         DBusConnection *connection;
141         DBusError error;
142         char *sig_name;
143
144         if (power_on)
145                 sig_name = "PowerOnCompleted";
146         else
147                 sig_name = "PowerOffCompleted";
148
149         dbus_error_init(&error);
150
151         connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
152         if (connection == NULL) {
153                 ERR("Error!!! Failed to get system DBus, error [%s]", error.message);
154                 dbus_error_free(&error);
155                 return;
156         }
157
158         signal = dbus_message_new_signal(NETCONFIG_WIFI_PATH,
159                                         NETCONFIG_WIFI_INTERFACE, sig_name);
160         if (signal == NULL)
161                 return;
162
163         dbus_connection_send(connection, signal, NULL);
164
165         dbus_message_unref(signal);
166         dbus_connection_unref(connection);
167
168         INFO("(%s)", sig_name);
169 }
170
171 static void __netconfig_wifi_direct_state_cb(int error_code,
172                 wifi_direct_device_state_e device_state, void *user_data)
173 {
174         wifi_direct_unset_device_state_changed_cb();
175         wifi_direct_deinitialize();
176
177         if (device_state == WIFI_DIRECT_DEVICE_STATE_DEACTIVATED) {
178                 if (__netconfig_wifi_try_to_load_driver() < 0) {
179                         power_in_progress = FALSE;
180
181                         /* TODO: error report */
182
183                         return;
184                 }
185         }
186 }
187
188 static gboolean __netconfig_wifi_direct_power_off(void)
189 {
190         DBG("Wi-Fi direct is turning off");
191
192         if (wifi_direct_initialize() < 0)
193                 return FALSE;
194
195         if (wifi_direct_set_device_state_changed_cb(
196                         __netconfig_wifi_direct_state_cb, NULL) < 0)
197                 return FALSE;
198
199         if (wifi_direct_deactivate() < 0)
200                 return FALSE;
201
202         return TRUE;
203 }
204
205 static int __netconfig_wifi_try_to_load_driver(void)
206 {
207         if (netconfig_is_wifi_allowed() != TRUE)
208                 return -EPERM;
209
210         if (netconfig_is_wifi_tethering_on() == TRUE) {
211                 /* TODO: Wi-Fi tethering turns off here */
212                 /* return TRUE; */
213                 return -EBUSY;
214         }
215
216         if (netconfig_is_wifi_direct_on() == TRUE) {
217                 if (__netconfig_wifi_direct_power_off() == TRUE) {
218                         power_in_progress = TRUE;
219                         return -EINPROGRESS;
220                 } else
221                         return -EBUSY;
222         }
223
224         if (__netconfig_wifi_load_driver() != TRUE) {
225                 netconfig_wifi_remove_driver();
226
227                 return -EIO;
228         }
229
230         if (__netconfig_wifi_enable_technology() != TRUE) {
231                 netconfig_wifi_remove_driver();
232                 return -EIO;
233         }
234
235         power_in_progress = TRUE;
236
237         return 0;
238 }
239
240 static gboolean __netconfig_wifi_try_to_remove_driver(void)
241 {
242         netconfig_wifi_device_picker_service_stop();
243
244         netconfig_wifi_statistics_update_powered_off();
245
246         if (__netconfig_wifi_disable_technology() != TRUE)
247                 return FALSE;
248
249         power_in_progress = TRUE;
250
251         return TRUE;
252 }
253
254 static void __netconfig_wifi_airplane_mode(keynode_t* node,
255                 void* user_data)
256 {
257         int value = 0;
258         int wifi_state = 0;
259         static gboolean powered_off_by_flightmode = FALSE;
260
261         if (power_in_progress) {
262                 fm_waiting = TRUE;
263                 return;
264         }
265
266         fm_waiting = FALSE;
267
268         vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &value);
269         vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
270
271         DBG("flight mode %s", value > 0 ? "ON" : "OFF");
272         DBG("Wi-Fi state %d, Wi-Fi was off by flight mode %s",
273                         wifi_state, powered_off_by_flightmode == TRUE ? "Yes" : "No");
274
275         if (value > 0) {
276                 /* flight mode enabled */
277                 if (wifi_state == VCONFKEY_WIFI_OFF)
278                         return;
279
280                 DBG("Turning Wi-Fi off");
281
282                 __netconfig_wifi_try_to_remove_driver();
283
284                 powered_off_by_flightmode = TRUE;
285         } else if (value == 0) {
286                 /* flight mode disabled */
287                 if (wifi_state > VCONFKEY_WIFI_OFF)
288                         return;
289
290                 if (powered_off_by_flightmode != TRUE)
291                         return;
292
293                 __netconfig_wifi_try_to_load_driver();
294
295                 powered_off_by_flightmode = FALSE;
296         } else
297                 DBG("Invalid value (%d)", value);
298 }
299
300 static void __netconfig_wifi_pm_state_mode(keynode_t* node,
301                 void* user_data)
302 {
303         int value = -1;
304         int wifi_state = 0;
305         static int prev_state = VCONFKEY_PM_STATE_NORMAL;
306
307         /*** vconf-keys.h ***
308          *              VCONFKEY_PM_STATE_NORMAL = 1,
309          *              VCONFKEY_PM_STATE_LCDDIM,
310          *              VCONFKEY_PM_STATE_LCDOFF,
311          *              VCONFKEY_PM_STATE_SLEEP
312          */
313
314         if(vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) == 0) {
315                 DBG("wifi state : %d (0 off / 1 on / 2 connected)", wifi_state);
316                 if(wifi_state <= VCONFKEY_WIFI_OFF)
317                         return;
318         }
319
320         if(vconf_get_int(VCONFKEY_PM_STATE, &value) < 0) {
321                 ERR("VCONFKEY_PM_STATE get failed");
322                 return;
323         }
324
325         DBG("Old state: %d, current: %d", prev_state, value);
326
327         if((value == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
328                 DBG("PM state : Wake UP!");
329
330                 netconfig_wifi_bgscan_stop();
331                 netconfig_wifi_bgscan_start();
332         }
333
334         prev_state = value;
335 }
336
337 void netconfig_set_power_in_progress(gboolean in_progress)
338 {
339         power_in_progress = in_progress;
340 }
341
342 void netconfig_check_fm_waiting(void)
343 {
344         if (fm_waiting)
345                 __netconfig_wifi_airplane_mode(NULL, NULL);
346 }
347
348 void netconfig_wifi_power_configuration(void)
349 {
350         int wifi_last_power_state = 0;
351
352         vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
353                         __netconfig_wifi_airplane_mode, NULL);
354
355         vconf_notify_key_changed(VCONFKEY_PM_STATE,
356                         __netconfig_wifi_pm_state_mode, NULL);
357
358         vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
359
360         if (wifi_last_power_state == WIFI_POWER_ON) {
361                 DBG("Turn Wi-Fi on automatically");
362
363                 __netconfig_wifi_try_to_load_driver();
364         }
365 }
366
367 gboolean netconfig_iface_wifi_load_driver(NetconfigWifi *wifi, GError **error)
368 {
369         DBG("Wi-Fi power on requested");
370
371         g_return_val_if_fail(wifi != NULL, FALSE);
372
373         int err;
374
375         if (netconfig_is_wifi_allowed() != TRUE) {
376                 netconfig_error_security_restricted(error);
377
378                 return FALSE;
379         }
380
381         if (power_in_progress) {
382                 netconfig_error_wifi_driver_failed(error);
383                 return FALSE;
384         }
385
386         err = __netconfig_wifi_try_to_load_driver();
387         if (err < 0) {
388                 if (err == -EINPROGRESS)
389                         netconfig_error_wifi_load_inprogress(error);
390                 else
391                         netconfig_error_wifi_driver_failed(error);
392
393                 return FALSE;
394         }
395
396         return TRUE;
397 }
398
399 gboolean netconfig_iface_wifi_remove_driver(NetconfigWifi *wifi, GError **error)
400 {
401         DBG("Wi-Fi power off requested");
402
403         g_return_val_if_fail(wifi != NULL, FALSE);
404
405         if (power_in_progress) {
406                 netconfig_error_wifi_driver_failed(error);
407                 return FALSE;
408         }
409
410         if (__netconfig_wifi_try_to_remove_driver() != TRUE) {
411                 netconfig_error_wifi_driver_failed(error);
412
413                 return FALSE;
414         }
415
416         return TRUE;
417 }