2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 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.
22 #include <vconf-keys.h>
26 #include <tzplatform_config.h>
28 #include <sys/ioctl.h>
35 #include "wifi-bssid-scan.h"
36 #include "wifi-power.h"
37 #include "wifi-state.h"
38 #include "netsupplicant.h"
39 #include "network-state.h"
40 #include "network-dpm.h"
41 #include "wifi-firmware.h"
42 #include "wifi-background-scan.h"
45 #define WLAN_SUPPLICANT_SCRIPT "/usr/bin/wpa_supp.sh"
46 #define P2P_SUPPLICANT_SCRIPT "/usr/bin/p2p_supp.sh"
48 #define VCONF_WIFI_OFF_STATE_BY_AIRPLANE "file/private/wifi/wifi_off_by_airplane"
49 #define VCONF_WIFI_OFF_STATE_BY_RESTRICTED "file/private/wifi/wifi_off_by_restricted"
50 #define VCONF_WIFI_OFF_STATE_BY_EMERGENCY "file/private/wifi/wifi_off_by_emergency"
51 #if !defined TIZEN_WEARABLE
52 #define VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT "db/setting/network_with_lcd_off_limit"
55 #define WLAN_MAC_ADDRESS_FILEPATH "/sys/class/net/wlan0/address"
56 #define WLAN_MAC_ADDR_MAX 20
57 #define VCONF_WIFI_BSSID_ADDRESS "db/wifi/bssid_address"
59 #define ETH_MAC_ADDR_SIZE 6
60 #define VCONF_ETH_MAC_ADDRESS "db/dnet/mac_address"
61 #define NET_EXEC_PATH "/sbin/ifconfig"
62 #define OS_RANDOM_FILE "/dev/urandom"
64 #define NETCONFIG_TECH_WAITING_INTERVAL 500
65 #define NETCONFIG_TECH_WAITING_COUNT 6
66 #define MAX_DRV_CMD_SIZE 248
67 #define WLAN_IOCTL_SUSPEND (SIOCDEVPRIVATE + 1)
69 static gboolean connman_wifi_technology_state = FALSE;
70 static gboolean wifi_firmware_recovery_mode = FALSE;
71 static int airplane_mode = 0;
77 } netconfig_wifi_priv_cmd;
79 static gboolean __is_wifi_restricted(void)
81 #if defined TIZEN_WEARABLE
84 int restricted_mode = 0;
86 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted_mode);
87 if (restricted_mode != 0) {
88 DBG("network restricted mode[%d]", restricted_mode);
95 static void __technology_reply(GObject *source_object, GAsyncResult *res, gpointer user_data)
98 GDBusConnection *conn = NULL;
101 conn = G_DBUS_CONNECTION(source_object);
102 reply = g_dbus_connection_call_finish(conn, res, &error);
106 if (g_strstr_len(error->message, strlen(error->message),
107 CONNMAN_ERROR_INTERFACE ".AlreadyEnabled") != NULL) {
108 wifi_state_update_power_state(TRUE);
109 } else if (g_strstr_len(error->message, strlen(error->message),
110 CONNMAN_ERROR_INTERFACE ".AlreadyDisabled") != NULL) {
111 wifi_state_update_power_state(FALSE);
113 ERR("Fail to request status [%d: %s]", error->code, error->message);
114 wifi_state_update_power_state(FALSE);
118 ERR("Fail to request status");
119 wifi_state_update_power_state(FALSE);
122 DBG("Successfully requested");
123 g_variant_unref(reply);
126 netconfig_gdbus_pending_call_unref();
129 static int __execute_supplicant(gboolean enable)
132 const char *path = WLAN_SUPPLICANT_SCRIPT;
133 char *const args_enable[] = { "/usr/bin/wpa_supp.sh", "start", NULL };
134 char *const args_disable[] = { "/usr/bin/wpa_supp.sh", "stop", NULL };
135 char *const envs[] = { NULL };
136 static gboolean enabled = FALSE;
138 if (enabled == enable)
142 rv = netconfig_execute_file(path, args_enable, envs);
144 rv = netconfig_execute_file(path, args_disable, envs);
148 DBG("wpa_supplicant %s", enable == TRUE ? "started" : "stopped");
155 void netconfig_wifi_recover_firmware(void)
157 wifi_firmware_recovery_mode = TRUE;
159 netconfig_wifi_bgscan_stop();
164 static int _load_driver_and_supplicant(void)
167 wifi_tech_state_e tech_state;
169 tech_state = wifi_state_get_technology_state();
170 if (tech_state > NETCONFIG_WIFI_TECH_OFF)
173 err = __execute_supplicant(TRUE);
174 if (err < 0 && err != -EALREADY)
177 err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, TRUE);
178 if (err < 0 && err != -EALREADY) {
179 __execute_supplicant(FALSE);
183 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
188 static int _remove_driver_and_supplicant(void)
192 DBG("remove driver and supplicant");
193 if (wifi_firmware_recovery_mode != TRUE &&
194 netconfig_wifi_is_bssid_scan_started() == TRUE) {
195 DBG("Wi-Fi WPS mode");
199 err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, FALSE);
200 if (err < 0 && err != -EALREADY)
203 err = __execute_supplicant(FALSE);
204 if (err < 0 && err != -EALREADY)
207 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_OFF);
209 // reset service state
210 wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
212 if (wifi_firmware_recovery_mode == TRUE) {
213 if (wifi_power_on() < 0)
214 ERR("Failed to recover Wi-Fi firmware");
216 wifi_firmware_recovery_mode = FALSE;
222 static gboolean __check_and_set_technology_enable(gpointer data)
224 static int retry_count = NETCONFIG_TECH_WAITING_COUNT;
225 gboolean value_enable = TRUE;
226 gboolean reply = FALSE;
227 GVariant *params = NULL;
228 char key[] = "Powered";
230 if (wifi_state_is_technology_available() == FALSE) {
236 params = g_variant_new("(sv)", key, g_variant_new_boolean(value_enable));
238 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
239 CONNMAN_WIFI_TECHNOLOGY_PREFIX,
240 CONNMAN_TECHNOLOGY_INTERFACE,
241 "SetProperty", params, __technology_reply);
244 ERR("Fail to set technology enable");
245 wifi_state_update_power_state(FALSE);
247 retry_count = NETCONFIG_TECH_WAITING_COUNT;
251 retry_count = NETCONFIG_TECH_WAITING_COUNT;
255 static int _set_connman_technology_power(gboolean enable)
257 gboolean reply = FALSE;
258 GVariant *params = NULL;
259 char key[] = "Powered";
260 gboolean value_enable = TRUE;
261 gboolean value_disable = FALSE;
263 if (connman_wifi_technology_state == enable)
266 if (enable && wifi_state_is_technology_available() == FALSE) {
267 netconfig_start_timer(NETCONFIG_TECH_WAITING_INTERVAL,
268 __check_and_set_technology_enable, NULL, NULL);
269 connman_wifi_technology_state = enable;
273 params = g_variant_new("(sv)", key, (enable == TRUE) ?
274 g_variant_new_boolean(value_enable) : g_variant_new_boolean(value_disable));
276 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
277 CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
278 "SetProperty", params, __technology_reply);
281 ERR("Fail to set technology %s", enable == TRUE ? "enable" : "disable");
285 /* If Wi-Fi powered off,
286 * Do not remove Wi-Fi driver until ConnMan technology state updated
289 connman_wifi_technology_state = enable;
291 /* To be keep safe, early disable Wi-Fi tech state */
293 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
298 static void __netconfig_set_wifi_bssid(void)
301 char bssid[WLAN_MAC_ADDR_MAX];
303 FILE *fp = fopen(WLAN_MAC_ADDRESS_FILEPATH, "r");
306 ERR("Fail to open %s", WLAN_MAC_ADDRESS_FILEPATH);
310 rv = fseek(fp, 0L, SEEK_SET);
312 ERR("Fail to seek file");
317 rv = fscanf(fp, "%17s", bssid);
319 ERR("Fail to read bssid");
324 netconfig_set_vconf_str(VCONF_WIFI_BSSID_ADDRESS, bssid);
329 void netconfig_wifi_disable_technology_state_by_only_connman_signal(void)
331 /* Important: it's only done by ConnMan technology signal update */
332 connman_wifi_technology_state = FALSE;
335 #if defined TIZEN_WEARABLE
336 int netconfig_wifi_on_wearable(gboolean device_picker_test)
341 if (netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_mode) < 0) {
342 ERR("Fail to get VCONFKEY_SETAPPL_PSMODE");
346 if (ps_mode > SETTING_PSMODE_NORMAL) {
347 WARN("ps mode is on(%d), Not turn on Wi-Fi", ps_mode);
351 err = wifi_power_driver_and_supplicant(TRUE);
352 if (err < 0 && err != -EALREADY)
355 err = _set_connman_technology_power(TRUE);
357 if (device_picker_test == TRUE)
358 netconfig_wifi_enable_device_picker_test();
363 static void __netconfig_wifi_restrict_mode(keynode_t *node, void *user_data)
365 int wifi_state = 0, restricted = 0;
366 int wifi_off_by_restricted = 0;
368 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
370 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
373 restricted = vconf_keynode_get_bool(node);
375 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted);
377 DBG("network restricted mode %s", restricted > 0 ? "ON" : "OFF");
378 DBG("Wi-Fi state %d, Wi-Fi was off by restricted mode %s", wifi_state,
379 wifi_off_by_restricted ? "Yes" : "No");
381 if (restricted > 0) {
382 /* network restricted on */
383 if (wifi_state == VCONFKEY_WIFI_OFF)
388 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 1);
390 /* network restricted off */
391 if (!wifi_off_by_restricted)
394 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 0);
396 if (wifi_state > VCONFKEY_WIFI_OFF)
404 static void __netconfig_wifi_airplane_mode(keynode_t *node, void *user_data)
406 int wifi_state = 0, airplane_state = 0;
407 int wifi_off_by_airplane = 0;
409 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
410 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
413 airplane_state = vconf_keynode_get_bool(node);
415 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
417 DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
418 DBG("Wi-Fi state(or use) %d, Wi-Fi was off by flight mode %s", wifi_state,
419 wifi_off_by_airplane ? "Yes" : "No");
421 if (airplane_mode == airplane_state)
424 airplane_mode = airplane_state;
426 if (airplane_state > 0) {
427 /* airplane mode on */
428 if (wifi_state == VCONFKEY_WIFI_OFF)
433 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
435 /* airplane mode off */
436 if (!wifi_off_by_airplane)
439 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
440 if (wifi_state > VCONFKEY_WIFI_OFF)
447 static void __emergency_mode_changed_cb(keynode_t *node, void *user_data)
449 int wifi_state = 0, emergency = 0;
450 int wifi_off_by_emergency = 0;
451 #if !defined TIZEN_WEARABLE
452 int emergency_by_fmm = 0;
454 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
455 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
457 #if !defined TIZEN_WEARABLE
458 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT, &emergency_by_fmm);
459 DBG("emergency mode by Find My Mobile (%d)", emergency_by_fmm);
460 if (emergency_by_fmm == 1)
465 emergency = vconf_keynode_get_int(node);
467 netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &emergency);
469 DBG("emergency mode %s", emergency > SETTING_PSMODE_POWERFUL ? "ON" : "OFF");
470 DBG("Wi-Fi state %d, Wi-Fi was off by emergency mode %s", wifi_state, wifi_off_by_emergency ? "Yes" : "No");
472 #if defined TIZEN_WEARABLE
473 if (emergency == SETTING_PSMODE_WEARABLE) {
474 /* basic power saving mode on */
475 } else if (emergency == SETTING_PSMODE_WEARABLE_ENHANCED) {
476 /* enhanced power saving mode on */
477 if (wifi_state == VCONFKEY_WIFI_OFF)
481 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
483 /* power saving mode off */
484 if (!wifi_off_by_emergency)
487 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
488 if (wifi_state > VCONFKEY_WIFI_OFF)
491 wifi_power_on_wearable(TRUE);
494 if (emergency > SETTING_PSMODE_POWERFUL) {
495 /* emergency mode on */
496 if (wifi_state == VCONFKEY_WIFI_OFF)
501 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
503 /* emergency mode off */
504 if (!wifi_off_by_emergency)
507 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
509 if (wifi_state > VCONFKEY_WIFI_OFF)
518 void wifi_set_early_suspend(gboolean value)
520 static gboolean old_state = FALSE;
522 char buf[MAX_DRV_CMD_SIZE];
523 netconfig_wifi_priv_cmd priv_cmd;
527 wifi_service_state_e wifi_state;
528 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
530 if (old_state == value) {
531 DBG("Old and new states are same");
535 if (netconfig_vconf_get_int(VCONFKEY_PM_STATE, &pm_state) < 0)
536 ERR("Fail to get VCONFKEY_PM_STATE");
538 wifi_state = wifi_state_get_service_state();
541 (pm_state < VCONFKEY_PM_STATE_LCDOFF ||
542 wifi_state == NETCONFIG_WIFI_ASSOCIATION ||
543 wifi_state == NETCONFIG_WIFI_CONFIGURATION)){
548 memset(buf, 0, sizeof(buf));
549 snprintf(buf, sizeof(buf), "SETSUSPENDMODE %d", value);
551 memset(&ifr, 0, sizeof(struct ifreq));
552 g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
554 DBG("Early suspend command: [%s]", buf);
556 memset(&priv_cmd, 0, sizeof(priv_cmd));
558 priv_cmd.used_len = sizeof(buf);
559 priv_cmd.total_len = sizeof(buf);
560 ifr.ifr_data = (char *)&priv_cmd;
562 ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
563 if (ioctl_sock < 0) {
564 DBG("socket(PF_INET,SOCK_DGRAM) failed: %s",
565 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
569 ret = ioctl(ioctl_sock, WLAN_IOCTL_SUSPEND, &ifr);
571 ERR("Fail to issue private commands: %d. %s", ret,
572 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
580 static void __pm_state_changed_cb(keynode_t* node, void* user_data)
584 static int prev_state = VCONFKEY_PM_STATE_NORMAL;
586 if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
587 ERR("Fail to get VCONFKEY_WIFI_STATE");
592 * VCONFKEY_PM_STATE_NORMAL = 1,
593 * VCONFKEY_PM_STATE_LCDDIM,
594 * VCONFKEY_PM_STATE_LCDOFF,
595 * VCONFKEY_PM_STATE_SLEEP
598 new_state = vconf_keynode_get_int(node);
600 netconfig_vconf_get_int(VCONFKEY_PM_STATE, &new_state);
602 DBG("wifi state: %d (0 off / 1 on / 2 connected)", wifi_state);
603 DBG("Old PM state: %d, current: %d (1 normal / 2 lcddim / 3 lcdoff / 4 sleep)", prev_state, new_state);
605 if ((new_state == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
606 /* Send early suspend mode based on LCD state and disallow early suspend count */
607 if (wifi_state != VCONFKEY_WIFI_OFF) {
608 wifi_set_early_suspend(FALSE);
609 DBG("Unset early suspend");
612 netconfig_wifi_bgscan_stop();
613 netconfig_wifi_bgscan_set_interval(SCAN_EXPONENTIAL_MIN);
614 netconfig_wifi_bgscan_start(TRUE);
615 } else if ((new_state == VCONFKEY_PM_STATE_LCDOFF) && (prev_state < VCONFKEY_PM_STATE_LCDOFF) && (wifi_state != VCONFKEY_WIFI_OFF)) {
616 wifi_set_early_suspend(TRUE);
617 DBG("Set early suspend");
620 prev_state = new_state;
623 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
625 int telephony_ready = 0;
628 telephony_ready = vconf_keynode_get_bool(node);
630 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
632 if (telephony_ready != 0) {
633 if (netconfig_tapi_check_sim_state() == FALSE) {
634 DBG("Sim is not initialized yet.");
641 DBG("Turn Wi-Fi on automatically");
643 #if defined TIZEN_WEARABLE
644 wifi_power_on_wearable(TRUE);
650 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
653 int wifi_power_driver_and_supplicant(gboolean enable)
655 /* There are 3 thumb rules for Wi-Fi power management
656 * 1. Do not make exposed API to control wpa_supplicant and driver directly.
657 * It probably breaks ConnMan technology operation.
659 * 2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
660 * It breaks ConnMan technology operation.
662 * 3. Final the best rule: make it as simple as possible.
663 * Simple code enables easy maintenance and reduces logical errors.
665 if (enable == TRUE) {
666 return _load_driver_and_supplicant();
668 if (connman_wifi_technology_state == TRUE)
671 return _remove_driver_and_supplicant();
675 void wifi_power_disable_technology_state_by_only_connman_signal(void)
677 /* Important: it's only done by ConnMan technology signal update */
678 connman_wifi_technology_state = FALSE;
681 void wifi_power_recover_firmware(void)
683 wifi_firmware_recovery_mode = TRUE;
685 netconfig_wifi_bgscan_stop();
690 int wifi_power_on(void)
693 wifi_tech_state_e tech_state;
695 tech_state = wifi_state_get_technology_state();
696 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED) {
697 /* There can be a scenario where wifi is automatically *
698 * activated by connman if wifi was powered in last boot. *
699 * So we should update connman_wifi_technology_state variable *
700 * if it is found that wifi_tech_state variable is *
701 * NETCONFIG_WIFI_TECH_POWERED and connman_wifi_technology_state *
702 * variable is FALSE. Earlier connman_wifi_technology_state *
703 * variable was only updated when wifi was Powered on from *
704 * net-config resulting in variable not getting updated. *
705 * This caused wifi to not get deactivated after reboot if *
706 * last power state was activated */
707 ERR("Net-Config WiFi connman technology state %d",
708 connman_wifi_technology_state);
709 if (connman_wifi_technology_state == FALSE)
710 connman_wifi_technology_state = TRUE;
714 if (__is_wifi_restricted() == TRUE)
717 if (netconfig_is_wifi_tethering_on() == TRUE) {
718 /* TODO: Wi-Fi tethering turns off here */
720 ERR("Failed to turn tethering off");
724 err = wifi_power_driver_and_supplicant(TRUE);
725 if (err < 0 && err != -EALREADY)
728 err = _set_connman_technology_power(TRUE);
733 int wifi_power_off(void)
737 err = _set_connman_technology_power(FALSE);
738 if (err == -EALREADY)
739 wifi_state_update_power_state(FALSE);
744 #if defined TIZEN_WEARABLE
745 int wifi_power_on_wearable(gboolean device_picker_test)
748 wifi_tech_state_e tech_state;
750 tech_state = wifi_state_get_technology_state();
751 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
754 err = wifi_power_driver_and_supplicant(TRUE);
755 if (err < 0 && err != -EALREADY)
758 err = _set_connman_technology_power(TRUE);
760 if (device_picker_test == TRUE)
761 netconfig_wifi_enable_device_picker_test();
767 void wifi_power_initialize(void)
769 int wifi_last_power_state = 0;
771 /* Initialize Airplane mode */
772 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
773 DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
775 /* Update the last Wi-Fi power state */
776 netconfig_vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
777 if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
778 if (TIZEN_TELEPHONY_ENABLE) {
779 int telephony_ready = 0;
780 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
781 if (telephony_ready == 0) {
782 DBG("Telephony API is not initialized yet");
783 vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
784 __netconfig_telephony_ready_changed_cb, NULL);
786 if (netconfig_tapi_check_sim_state() == FALSE)
787 DBG("SIM is not initialized yet");
790 DBG("Turn Wi-Fi on automatically");
791 #if defined TIZEN_WEARABLE
792 wifi_power_on_wearable(TRUE);
798 #if defined TIZEN_WEARABLE
799 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
800 __netconfig_wifi_airplane_mode, NULL);
802 vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
803 __netconfig_wifi_restrict_mode, NULL);
804 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
805 __netconfig_wifi_airplane_mode, NULL);
808 vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
809 vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
812 void wifi_power_deinitialize(void)
816 gboolean handle_load_driver(Wifi *wifi,
817 GDBusMethodInvocation *context, gboolean device_picker_test)
821 DBG("Wi-Fi power on requested");
823 g_return_val_if_fail(wifi != NULL, TRUE);
825 if (!netconfig_dpm_update_from_wifi()) {
826 DBG("DPM policy restricts Wi-Fi");
827 netconfig_error_permission_denied(context);
831 if (TIZEN_WLAN_BOARD_SPRD)
832 wifi_firmware_download();
834 #if defined TIZEN_WEARABLE
835 err = wifi_power_on_wearable(device_picker_test);
837 err = wifi_power_on();
839 if (device_picker_test == TRUE)
840 netconfig_wifi_enable_device_picker_test();
843 if (err == -EALREADY)
844 netconfig_error_already_exists(context);
845 else if (err == -EPERM)
846 netconfig_error_permission_denied(context);
848 netconfig_error_wifi_driver_failed(context);
854 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
855 __netconfig_set_wifi_bssid();
857 wifi_complete_load_driver(wifi, context);
861 gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
865 DBG("Wi-Fi power off requested");
867 g_return_val_if_fail(wifi != NULL, TRUE);
869 err = wifi_power_off();
871 if (err == -EINPROGRESS)
872 netconfig_error_inprogress(context);
873 else if (err == -EALREADY)
874 netconfig_error_already_exists(context);
875 else if (err == -EPERM)
876 netconfig_error_permission_denied(context);
878 netconfig_error_wifi_driver_failed(context);
882 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
884 wifi_complete_remove_driver(wifi, context);
888 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
892 wifi_complete_load_p2p_driver(wifi, context);
896 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
900 wifi_complete_remove_p2p_driver(wifi, context);
904 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
906 DBG("Generate Random Mac address of ethernet");
910 fp = fopen(OS_RANDOM_FILE, "rb");
913 ERR("Could not open /dev/urandom");
916 rc = fread(mac_buf, 1, mac_len, fp);
920 return rc != mac_len ? -1 : 0;
923 void __netconfig_set_ether_macaddr()
925 DBG("Set wired Mac address ");
926 char *mac_addr = NULL;
927 char rand_addr[WLAN_MAC_ADDR_MAX];
930 mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
931 if (mac_addr == NULL) {
932 DBG("vconf_get_str Failed\n");
935 /* Checking Invalid MAC Address */
936 if ((strlen(mac_addr) == 0)) {
937 ERR("Failed to get valid MAC Address from vconf");
938 /*Generate the Random Mac address*/
939 unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
941 if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE) == -1) {
943 ERR("Could not generate the Random Mac address");
948 rand_mac_add[0] &= 0xFE; /*Clear multicase bit*/
949 rand_mac_add[0] |= 0x02; /*set local assignment bit*/
951 /*Set the Mac address in Vconf*/
952 snprintf(rand_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
953 rand_mac_add[0], rand_mac_add[1],
954 rand_mac_add[2], rand_mac_add[3],
955 rand_mac_add[4], rand_mac_add[5]);
957 netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, rand_addr);
958 } else { /* Valid MAC address */
959 g_strlcpy(rand_addr, mac_addr, WLAN_MAC_ADDR_MAX);
962 DBG("MAC Address of eth0 [%s]", rand_addr);
963 const char *path = NET_EXEC_PATH;
964 char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
965 "ether", rand_addr, "up", NULL};
966 char *const envs[] = { NULL };
967 rv = netconfig_execute_file(path, args, envs);
970 ERR("Unable to execute system command");