2 * Network Configuration Module
4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <vconf-keys.h>
24 #include <wifi-direct.h>
32 #include "netconfig.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"
43 #define WLAN_DRIVER_SCRIPT "/usr/bin/wlan.sh"
45 static gboolean power_in_progress = FALSE;
46 static gboolean fm_waiting = FALSE;
48 static void __netconfig_wifi_technology_reply(DBusPendingCall *call, void *data)
52 message = dbus_pending_call_steal_reply(call);
54 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
55 ERR("%s", dbus_message_get_error_name(message));
57 if (dbus_message_is_error(message,
58 CONNMAN_ERROR_INTERFACE ".AlreadyEnabled") == TRUE) {
59 netconfig_wifi_update_power_state(TRUE);
60 power_in_progress = FALSE;
61 } else if (dbus_message_is_error(message,
62 CONNMAN_ERROR_INTERFACE ".AlreadyDisabled") == TRUE) {
63 netconfig_wifi_update_power_state(FALSE);
64 power_in_progress = FALSE;
67 DBG("Successfully requested");
70 dbus_message_unref(message);
71 dbus_pending_call_unref(call);
74 static gboolean __netconfig_wifi_enable_technology(void)
76 gboolean reply = FALSE;
77 char param0[] = "string:Powered";
78 char param1[] = "variant:boolean:true";
79 char *param_array[] = { NULL, NULL, NULL };
81 param_array[0] = param0;
82 param_array[1] = param1;
84 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
85 CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
86 "SetProperty", param_array, __netconfig_wifi_technology_reply);
89 ERR("Fail to enable Wi-Fi");
94 static gboolean __netconfig_wifi_disable_technology(void)
96 gboolean reply = FALSE;
97 char param0[] = "string:Powered";
98 char param1[] = "variant:boolean:false";
99 char *param_array[] = { NULL, NULL, NULL };
101 param_array[0] = param0;
102 param_array[1] = param1;
104 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
105 CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
106 "SetProperty", param_array, __netconfig_wifi_technology_reply);
109 ERR("Fail to disable Wi-Fi");
114 static gboolean __netconfig_wifi_load_driver(void)
117 const char *path = WLAN_DRIVER_SCRIPT;
118 char *const args[] = { "wlan.sh", "start", NULL };
119 char *const envs[] = { NULL };
121 if (netconfig_emulator_is_emulated() == TRUE)
124 #if defined EMBEDDED_TARGET
125 rv = netconfig_execute_file(path, args, envs);
127 DBG("Failed to load wireless device driver");
132 DBG("Successfully loaded wireless device driver");
136 gboolean netconfig_wifi_remove_driver(void)
139 const char *path = WLAN_DRIVER_SCRIPT;
140 char *const args[] = { "wlan.sh", "stop", NULL };
141 char *const env[] = { NULL };
143 if (netconfig_emulator_is_emulated() == TRUE)
146 #if defined EMBEDDED_TARGET
147 rv = netconfig_execute_file(path, args, env);
149 DBG("Failed to remove wireless device driver");
154 DBG("Successfully removed wireless device driver");
158 static int __netconfig_wifi_try_to_load_driver(void);
159 static gboolean __netconfig_wifi_try_to_remove_driver(void);
161 void netconfig_wifi_notify_power_completed(gboolean power_on)
164 DBusConnection *connection;
169 sig_name = "PowerOnCompleted";
171 sig_name = "PowerOffCompleted";
173 dbus_error_init(&error);
175 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
176 if (connection == NULL) {
177 ERR("Error!!! Failed to get system DBus, error [%s]", error.message);
178 dbus_error_free(&error);
182 signal = dbus_message_new_signal(NETCONFIG_WIFI_PATH,
183 NETCONFIG_WIFI_INTERFACE, sig_name);
187 dbus_connection_send(connection, signal, NULL);
189 dbus_message_unref(signal);
190 dbus_connection_unref(connection);
192 INFO("(%s)", sig_name);
195 static void __netconfig_wifi_direct_state_cb(int error_code,
196 wifi_direct_device_state_e device_state, void *user_data)
198 wifi_direct_unset_device_state_changed_cb();
199 wifi_direct_deinitialize();
201 if (device_state == WIFI_DIRECT_DEVICE_STATE_DEACTIVATED) {
202 if (__netconfig_wifi_try_to_load_driver() < 0) {
203 power_in_progress = FALSE;
205 /* TODO: error report */
212 static gboolean __netconfig_wifi_direct_power_off(void)
214 DBG("Wi-Fi direct is turning off");
216 if (wifi_direct_initialize() < 0)
219 if (wifi_direct_set_device_state_changed_cb(
220 __netconfig_wifi_direct_state_cb, NULL) < 0)
223 if (wifi_direct_deactivate() < 0)
229 static int __netconfig_wifi_try_to_load_driver(void)
231 if (netconfig_is_wifi_allowed() != TRUE)
234 if (netconfig_is_wifi_tethering_on() == TRUE) {
235 /* TODO: Wi-Fi tethering turns off here */
240 if (netconfig_is_wifi_direct_on() == TRUE) {
241 if (__netconfig_wifi_direct_power_off() == TRUE) {
242 power_in_progress = TRUE;
248 if (__netconfig_wifi_load_driver() != TRUE) {
249 netconfig_wifi_remove_driver();
254 if (__netconfig_wifi_enable_technology() != TRUE) {
255 netconfig_wifi_remove_driver();
259 power_in_progress = TRUE;
264 static gboolean __netconfig_wifi_try_to_remove_driver(void)
266 netconfig_wifi_device_picker_service_stop();
268 netconfig_wifi_statistics_update_powered_off();
270 if (__netconfig_wifi_disable_technology() != TRUE)
273 power_in_progress = TRUE;
278 static void __netconfig_wifi_airplane_mode(keynode_t* node,
283 static gboolean powered_off_by_flightmode = FALSE;
285 if (power_in_progress) {
292 vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &value);
293 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
295 DBG("flight mode %s", value > 0 ? "ON" : "OFF");
296 DBG("Wi-Fi state %d, Wi-Fi was off by flight mode %s",
297 wifi_state, powered_off_by_flightmode == TRUE ? "Yes" : "No");
300 /* flight mode enabled */
301 if (wifi_state == VCONFKEY_WIFI_OFF)
304 DBG("Turning Wi-Fi off");
306 __netconfig_wifi_try_to_remove_driver();
308 powered_off_by_flightmode = TRUE;
309 } else if (value == 0) {
310 /* flight mode disabled */
311 if (wifi_state > VCONFKEY_WIFI_OFF)
314 if (powered_off_by_flightmode != TRUE)
317 __netconfig_wifi_try_to_load_driver();
319 powered_off_by_flightmode = FALSE;
321 DBG("Invalid value (%d)", value);
324 static void __netconfig_wifi_pm_state_mode(keynode_t* node,
329 static int prev_state = VCONFKEY_PM_STATE_NORMAL;
331 /*** vconf-keys.h ***
332 * VCONFKEY_PM_STATE_NORMAL = 1,
333 * VCONFKEY_PM_STATE_LCDDIM,
334 * VCONFKEY_PM_STATE_LCDOFF,
335 * VCONFKEY_PM_STATE_SLEEP
338 if(vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) == 0) {
339 DBG("wifi state : %d (0 off / 1 on / 2 connected)", wifi_state);
340 if(wifi_state <= VCONFKEY_WIFI_OFF)
344 if(vconf_get_int(VCONFKEY_PM_STATE, &value) < 0) {
345 ERR("VCONFKEY_PM_STATE get failed");
349 DBG("Old state: %d, current: %d", prev_state, value);
351 if((value == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
352 DBG("PM state : Wake UP!");
354 netconfig_wifi_bgscan_stop();
355 netconfig_wifi_bgscan_start();
361 void netconfig_set_power_in_progress(gboolean in_progress)
363 power_in_progress = in_progress;
366 void netconfig_check_fm_waiting(void)
369 __netconfig_wifi_airplane_mode(NULL, NULL);
372 void netconfig_wifi_power_configuration(void)
374 int wifi_last_power_state = 0;
376 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
377 __netconfig_wifi_airplane_mode, NULL);
379 vconf_notify_key_changed(VCONFKEY_PM_STATE,
380 __netconfig_wifi_pm_state_mode, NULL);
382 vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
384 if (wifi_last_power_state == WIFI_POWER_ON) {
385 DBG("Turn Wi-Fi on automatically");
387 __netconfig_wifi_try_to_load_driver();
391 gboolean netconfig_iface_wifi_load_driver(NetconfigWifi *wifi, GError **error)
393 DBG("Wi-Fi power on requested");
395 g_return_val_if_fail(wifi != NULL, FALSE);
399 if (netconfig_is_wifi_allowed() != TRUE) {
400 netconfig_error_security_restricted(error);
405 if (power_in_progress) {
406 netconfig_error_wifi_driver_failed(error);
410 err = __netconfig_wifi_try_to_load_driver();
412 if (err == -EINPROGRESS)
413 netconfig_error_wifi_load_inprogress(error);
415 netconfig_error_wifi_driver_failed(error);
423 gboolean netconfig_iface_wifi_remove_driver(NetconfigWifi *wifi, GError **error)
425 DBG("Wi-Fi power off requested");
427 g_return_val_if_fail(wifi != NULL, FALSE);
429 if (power_in_progress) {
430 netconfig_error_wifi_driver_failed(error);
434 if (__netconfig_wifi_try_to_remove_driver() != TRUE) {
435 netconfig_error_wifi_driver_failed(error);