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"
46 static gboolean __netconfig_wifi_enable_technology(void)
48 DBusMessage *reply = NULL;
49 char param1[] = "string:Powered";
50 char param2[] = "variant:boolean:true";
51 char *param_array[] = {NULL, NULL, NULL};
53 param_array[0] = param1;
54 param_array[1] = param2;
56 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE, CONNMAN_WIFI_TECHNOLOGY_PREFIX,
57 CONNMAN_TECHNOLOGY_INTERFACE, "SetProperty", param_array);
60 ERR("Error! Request failed");
64 dbus_message_unref(reply);
69 static gboolean __netconfig_wifi_disable_technology(void)
71 DBusMessage *reply = NULL;
72 char param1[] = "string:Powered";
73 char param2[] = "variant:boolean:false";
74 char *param_array[] = {NULL, NULL, NULL};
76 param_array[0] = param1;
77 param_array[1] = param2;
79 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE, CONNMAN_WIFI_TECHNOLOGY_PREFIX,
80 CONNMAN_TECHNOLOGY_INTERFACE, "SetProperty", param_array);
83 ERR("Error! Request failed");
87 dbus_message_unref(reply);
92 static gboolean __netconfig_wifi_load_driver(void)
95 const char *path = WLAN_DRIVER_SCRIPT;
96 char *const args[] = { "wlan.sh", "start", NULL };
97 char *const envs[] = { NULL };
99 if (netconfig_emulator_is_emulated() == TRUE)
102 rv = netconfig_execute_file(path, args, envs);
104 DBG("Failed to load wireless device driver");
108 DBG("Successfully loaded wireless device driver");
112 gboolean netconfig_wifi_remove_driver(void)
115 const char *path = WLAN_DRIVER_SCRIPT;
116 char *const args[] = { "wlan.sh", "stop", NULL };
117 char *const env[] = { NULL };
119 if (netconfig_emulator_is_emulated() == TRUE)
122 rv = netconfig_execute_file(path, args, env);
124 DBG("Failed to remove wireless device driver");
128 DBG("Successfully removed wireless device driver");
132 static int __netconfig_wifi_try_to_load_driver(void);
133 static gboolean __netconfig_wifi_try_to_remove_driver(void);
135 void netconfig_wifi_notify_power_completed(gboolean power_on)
138 DBusConnection *connection;
143 sig_name = "PowerOnCompleted";
145 sig_name = "PowerOffCompleted";
147 dbus_error_init(&error);
149 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
150 if (connection == NULL) {
151 ERR("Error!!! Failed to get system DBus, error [%s]", error.message);
152 dbus_error_free(&error);
156 signal = dbus_message_new_signal(NETCONFIG_WIFI_PATH,
157 NETCONFIG_WIFI_INTERFACE, sig_name);
161 dbus_connection_send(connection, signal, NULL);
163 dbus_message_unref(signal);
164 dbus_connection_unref(connection);
166 INFO("(%s)", sig_name);
169 static void __netconfig_wifi_direct_state_cb(int error_code,
170 wifi_direct_device_state_e device_state, void *user_data)
172 wifi_direct_unset_device_state_changed_cb();
173 wifi_direct_deinitialize();
175 if (device_state == WIFI_DIRECT_DEVICE_STATE_DEACTIVATED) {
176 if (__netconfig_wifi_try_to_load_driver() < 0) {
178 /* TODO: error report */
183 netconfig_wifi_notify_power_completed(TRUE);
187 static gboolean __netconfig_wifi_direct_power_off(void)
189 DBG("Wi-Fi direct is turning off");
191 if (wifi_direct_initialize() < 0)
194 if (wifi_direct_set_device_state_changed_cb(
195 __netconfig_wifi_direct_state_cb, NULL) < 0)
198 if (wifi_direct_deactivate() < 0)
204 static int __netconfig_wifi_try_to_load_driver(void)
206 if (netconfig_is_wifi_allowed() != TRUE)
209 if (netconfig_is_wifi_tethering_on() == TRUE) {
210 /* TODO: Wi-Fi tethering turns off here */
215 if (netconfig_is_wifi_direct_on() == TRUE) {
216 if (__netconfig_wifi_direct_power_off() == TRUE)
222 if (__netconfig_wifi_load_driver() != TRUE) {
223 netconfig_wifi_remove_driver();
228 __netconfig_wifi_enable_technology();
233 static gboolean __netconfig_wifi_try_to_remove_driver(void)
235 netconfig_wifi_device_picker_service_stop();
237 netconfig_wifi_statistics_update_powered_off();
239 __netconfig_wifi_disable_technology();
244 static void __netconfig_wifi_airplane_mode(keynode_t* node,
249 static gboolean powered_off_by_flightmode = FALSE;
251 vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &value);
252 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
254 DBG("flight mode %s", value > 0 ? "ON" : "OFF");
255 DBG("Wi-Fi state %d, Wi-Fi was off by flight mode %s",
256 wifi_state, powered_off_by_flightmode == TRUE ? "Yes" : "No");
259 /* flight mode enabled */
260 if (wifi_state == VCONFKEY_WIFI_OFF)
263 DBG("Turning Wi-Fi off");
265 __netconfig_wifi_try_to_remove_driver();
267 powered_off_by_flightmode = TRUE;
268 } else if (value == 0) {
269 /* flight mode disabled */
270 if (wifi_state > VCONFKEY_WIFI_OFF)
273 if (powered_off_by_flightmode != TRUE)
276 __netconfig_wifi_try_to_load_driver();
278 powered_off_by_flightmode = FALSE;
280 DBG("Invalid value (%d)", value);
283 static void __netconfig_wifi_pm_state_mode(keynode_t* node,
288 static int prev_state = VCONFKEY_PM_STATE_NORMAL;
290 /*** vconf-keys.h ***
291 * VCONFKEY_PM_STATE_NORMAL = 1,
292 * VCONFKEY_PM_STATE_LCDDIM,
293 * VCONFKEY_PM_STATE_LCDOFF,
294 * VCONFKEY_PM_STATE_SLEEP
297 if(vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) == 0) {
298 DBG("wifi state : %d (0 off / 1 on / 2 connected)", wifi_state);
299 if(wifi_state <= VCONFKEY_WIFI_OFF)
303 if(vconf_get_int(VCONFKEY_PM_STATE, &value) < 0) {
304 ERR("VCONFKEY_PM_STATE get failed");
308 DBG("Old state: %d, current: %d", prev_state, value);
310 if((value == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
311 DBG("PM state : Wake UP!");
313 netconfig_wifi_bgscan_stop();
314 netconfig_wifi_bgscan_start();
320 void netconfig_wifi_power_configuration(void)
322 int wifi_last_power_state = 0;
324 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
325 __netconfig_wifi_airplane_mode, NULL);
327 vconf_notify_key_changed(VCONFKEY_PM_STATE,
328 __netconfig_wifi_pm_state_mode, NULL);
330 vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
332 if (wifi_last_power_state == WIFI_POWER_ON) {
333 DBG("Turn Wi-Fi on automatically");
335 __netconfig_wifi_try_to_load_driver();
339 gboolean netconfig_iface_wifi_load_driver(NetconfigWifi *wifi, GError **error)
341 DBG("Wi-Fi power on requested");
343 g_return_val_if_fail(wifi != NULL, FALSE);
347 if (netconfig_is_wifi_allowed() != TRUE) {
348 netconfig_error_security_restricted(error);
353 err = __netconfig_wifi_try_to_load_driver();
355 if (err == -EINPROGRESS)
356 netconfig_error_wifi_load_inprogress(error);
358 netconfig_error_wifi_driver_failed(error);
366 gboolean netconfig_iface_wifi_remove_driver(NetconfigWifi *wifi, GError **error)
368 DBG("Wi-Fi power off requested");
370 g_return_val_if_fail(wifi != NULL, FALSE);
372 if (__netconfig_wifi_try_to_remove_driver() != TRUE) {
373 netconfig_error_wifi_driver_failed(error);