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>
33 #include "wifi-power.h"
34 #include "wifi-state.h"
35 #include "netsupplicant.h"
36 #include "network-state.h"
37 #include "network-dpm.h"
38 #include "wifi-firmware.h"
39 #include "wifi-background-scan.h"
42 #define WLAN_SUPPLICANT_SCRIPT "/usr/bin/wpa_supp.sh"
43 #define P2P_SUPPLICANT_SCRIPT "/usr/bin/p2p_supp.sh"
45 #define VCONF_WIFI_OFF_STATE_BY_AIRPLANE "file/private/wifi/wifi_off_by_airplane"
46 #define VCONF_WIFI_OFF_STATE_BY_RESTRICTED "file/private/wifi/wifi_off_by_restricted"
47 #define VCONF_WIFI_OFF_STATE_BY_EMERGENCY "file/private/wifi/wifi_off_by_emergency"
48 #if defined TIZEN_WEARABLE
49 #define VCONF_WIFI_WEARABLE_WIFI_USE "db/private/wifi/wearable_wifi_use"
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_INFO tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
56 #define WLAN_MAC_ADDRESS_FILEPATH "/sys/class/net/wlan0/address"
57 #define WLAN_MAC_ADDR_MAX 20
58 #define VCONF_WIFI_BSSID_ADDRESS "db/wifi/bssid_address"
60 #define ETH_MAC_ADDR_SIZE 6
61 #define VCONF_ETH_MAC_ADDRESS "db/dnet/mac_address"
62 #define NET_EXEC_PATH "/sbin/ifconfig"
63 #define OS_RANDOM_FILE "/dev/urandom"
65 static gboolean connman_wifi_technology_state = FALSE;
66 static gboolean wifi_firmware_recovery_mode = FALSE;
67 static int airplane_mode = 0;
69 #if defined TIZEN_WEARABLE
70 static int psmode_wifi_use = 1;
73 static gboolean __is_wifi_restricted(void)
75 #if defined TIZEN_WEARABLE
78 int restricted_mode = 0;
80 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted_mode);
81 if (restricted_mode != 0) {
82 DBG("network restricted mode[%d]", restricted_mode);
89 static void __technology_reply(GObject *source_object, GAsyncResult *res, gpointer user_data)
92 GDBusConnection *conn = NULL;
95 conn = G_DBUS_CONNECTION(source_object);
96 reply = g_dbus_connection_call_finish(conn, res, &error);
100 if (g_strstr_len(error->message, strlen(error->message),
101 CONNMAN_ERROR_INTERFACE ".AlreadyEnabled") != NULL) {
102 wifi_state_update_power_state(TRUE);
103 } else if (g_strstr_len(error->message, strlen(error->message),
104 CONNMAN_ERROR_INTERFACE ".AlreadyDisabled") != NULL) {
105 wifi_state_update_power_state(FALSE);
107 ERR("Fail to request status [%d: %s]", error->code, error->message);
108 wifi_state_update_power_state(FALSE);
112 ERR("Fail to request status");
113 wifi_state_update_power_state(FALSE);
116 DBG("Successfully requested");
119 g_variant_unref(reply);
120 netconfig_gdbus_pending_call_unref();
123 static int __execute_supplicant(gboolean enable)
126 const char *path = WLAN_SUPPLICANT_SCRIPT;
127 char *const args_enable[] = { "/usr/bin/wpa_supp.sh", "start", NULL };
128 char *const args_disable[] = { "/usr/bin/wpa_supp.sh", "stop", NULL };
129 char *const envs[] = { NULL };
130 static gboolean enabled = FALSE;
132 if (enabled == enable)
136 rv = netconfig_execute_file(path, args_enable, envs);
138 rv = netconfig_execute_file(path, args_disable, envs);
142 DBG("wpa_supplicant %s", enable == TRUE ? "started" : "stopped");
149 void netconfig_wifi_recover_firmware(void)
151 wifi_firmware_recovery_mode = TRUE;
153 netconfig_wifi_bgscan_stop();
158 static int _load_driver_and_supplicant(void)
161 wifi_tech_state_e tech_state;
163 tech_state = wifi_state_get_technology_state();
164 if (tech_state > NETCONFIG_WIFI_TECH_OFF)
167 err = __execute_supplicant(TRUE);
168 if (err < 0 && err != -EALREADY)
171 err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, TRUE);
172 if (err < 0 && err != -EALREADY) {
173 __execute_supplicant(FALSE);
177 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
182 static int _remove_driver_and_supplicant(void)
186 INFO("remove driver and supplicant");
187 if (wifi_firmware_recovery_mode != TRUE &&
188 netconfig_wifi_is_wps_enabled() == TRUE) {
189 DBG("Wi-Fi WPS mode");
193 err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, FALSE);
194 if (err < 0 && err != -EALREADY)
197 err = __execute_supplicant(FALSE);
198 if (err < 0 && err != -EALREADY)
201 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_OFF);
203 // reset service state
204 wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
206 if (wifi_firmware_recovery_mode == TRUE) {
207 if (wifi_power_on() < 0)
208 ERR("Failed to recover Wi-Fi firmware");
210 wifi_firmware_recovery_mode = FALSE;
216 static int _set_connman_technology_power(gboolean enable)
218 gboolean reply = FALSE;
219 GVariant *param0 = NULL;
220 GVariant *params = NULL;
221 char key[] = "Powered";
222 gboolean value_enable = TRUE;
223 gboolean value_disable = FALSE;
225 if (connman_wifi_technology_state == enable)
229 param0 = g_variant_new_boolean(value_enable);
231 param0 = g_variant_new_boolean(value_disable);
233 params = g_variant_new("(sv)", key, param0);
235 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
236 CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
237 "SetProperty", params, __technology_reply);
240 ERR("Fail to set technology %s", enable == TRUE ? "enable" : "disable");
244 /* If Wi-Fi powered off,
245 * Do not remove Wi-Fi driver until ConnMan technology state updated
248 connman_wifi_technology_state = enable;
250 /* To be keep safe, early disable Wi-Fi tech state */
252 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
257 static void __netconfig_set_wifi_bssid(void)
260 char bssid[WLAN_MAC_ADDR_MAX];
262 FILE *fp = fopen(WLAN_MAC_INFO, "r");
265 ERR("Fail to open %s", WLAN_MAC_INFO);
266 fp = fopen(WLAN_MAC_ADDRESS_FILEPATH, "r");
268 ERR("Fail to open %s", WLAN_MAC_ADDRESS_FILEPATH);
273 fseek(fp, 0L, SEEK_SET);
274 rv = fscanf(fp, "%17s", bssid);
277 ERR("Fail to read bssid");
279 netconfig_set_vconf_str(VCONF_WIFI_BSSID_ADDRESS, bssid);
284 void netconfig_wifi_disable_technology_state_by_only_connman_signal(void)
286 /* Important: it's only done by ConnMan technology signal update */
287 connman_wifi_technology_state = FALSE;
290 #if defined TIZEN_WEARABLE
291 int netconfig_wifi_on_wearable(gboolean device_picker_test)
297 if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
298 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
303 if (netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_mode) < 0) {
304 ERR("Fail to get VCONFKEY_SETAPPL_PSMODE");
308 if (ps_mode > SETTING_PSMODE_NORMAL) {
309 WARN("ps mode is on(%d), Not turn on Wi-Fi", ps_mode);
313 WARN("Not permitted Wi-Fi on");
317 err = wifi_power_driver_and_supplicant(TRUE);
318 if (err < 0 && err != -EALREADY)
321 err = _set_connman_technology_power(TRUE);
323 if (device_picker_test == TRUE)
324 netconfig_wifi_enable_device_picker_test();
329 static void __wearable_wifi_use_changed_cb(keynode_t* node, void* user_data)
334 if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
335 ERR("Fail to get VCONFKEY_WIFI_STATE");
340 wifi_use = vconf_keynode_get_int(node);
342 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
346 if (wifi_state > VCONFKEY_WIFI_OFF) {
347 WARN("Wi-Fi is already turned on");
350 wifi_power_on_wearable(TRUE);
352 ERR("## wifi use [OFF]");
353 if (wifi_state == VCONFKEY_WIFI_OFF) {
354 WARN("Wi-Fi is already turned off");
362 static void __netconfig_wifi_restrict_mode(keynode_t *node, void *user_data)
364 int wifi_state = 0, restricted = 0;
365 int wifi_off_by_restricted = 0;
367 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
369 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
372 restricted = vconf_keynode_get_bool(node);
374 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted);
376 DBG("network restricted mode %s", restricted > 0 ? "ON" : "OFF");
377 DBG("Wi-Fi state %d, Wi-Fi was off by restricted mode %s", wifi_state,
378 wifi_off_by_restricted ? "Yes" : "No");
380 if (restricted > 0) {
381 /* network restricted on */
382 if (wifi_state == VCONFKEY_WIFI_OFF)
387 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 1);
389 /* network restricted off */
390 if (!wifi_off_by_restricted)
393 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 0);
395 if (wifi_state > VCONFKEY_WIFI_OFF)
403 static void __netconfig_wifi_airplane_mode(keynode_t *node, void *user_data)
405 int wifi_state = 0, airplane_state = 0;
406 int wifi_off_by_airplane = 0;
408 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
410 #if defined TIZEN_WEARABLE
411 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_state)
413 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
417 airplane_state = vconf_keynode_get_bool(node);
419 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
421 DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
422 DBG("Wi-Fi state(or use) %d, Wi-Fi was off by flight mode %s", wifi_state,
423 wifi_off_by_airplane ? "Yes" : "No");
425 if (airplane_mode == airplane_state)
428 airplane_mode = airplane_state;
430 if (airplane_state > 0) {
431 /* airplane mode on */
432 if (wifi_state == VCONFKEY_WIFI_OFF)
437 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
438 #if defined TIZEN_WEARABLE
439 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
442 /* airplane mode off */
443 if (!wifi_off_by_airplane)
446 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
447 #if defined TIZEN_WEARABLE
448 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 1);
450 if (wifi_state > VCONFKEY_WIFI_OFF)
458 static void __emergency_mode_changed_cb(keynode_t *node, void *user_data)
460 int wifi_state = 0, emergency = 0;
461 int wifi_off_by_emergency = 0;
462 #if !defined TIZEN_WEARABLE
463 int emergency_by_fmm = 0;
465 #if defined TIZEN_WEARABLE
469 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
470 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
472 #if !defined TIZEN_WEARABLE
473 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT, &emergency_by_fmm);
474 DBG("emergency mode by Find My Mobile (%d)", emergency_by_fmm);
475 if (emergency_by_fmm == 1)
480 emergency = vconf_keynode_get_int(node);
482 netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &emergency);
484 DBG("emergency mode %s", emergency > SETTING_PSMODE_POWERFUL ? "ON" : "OFF");
485 DBG("Wi-Fi state %d, Wi-Fi was off by emergency mode %s", wifi_state, wifi_off_by_emergency ? "Yes" : "No");
487 #if defined TIZEN_WEARABLE
488 if (emergency == SETTING_PSMODE_WEARABLE) {
489 /* basic power saving mode on */
490 } else if (emergency == SETTING_PSMODE_WEARABLE_ENHANCED) {
491 /* enhanced power saving mode on */
492 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
493 psmode_wifi_use = wifi_use;
495 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
497 if (wifi_state == VCONFKEY_WIFI_OFF)
501 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
503 /* power saving mode off */
504 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, psmode_wifi_use);
505 if (!wifi_off_by_emergency)
508 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
509 if (wifi_state > VCONFKEY_WIFI_OFF)
512 wifi_power_on_wearable(TRUE);
515 if (emergency > SETTING_PSMODE_POWERFUL) {
516 /* emergency mode on */
517 if (wifi_state == VCONFKEY_WIFI_OFF)
522 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
524 /* emergency mode off */
525 if (!wifi_off_by_emergency)
528 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
530 if (wifi_state > VCONFKEY_WIFI_OFF)
539 static void __pm_state_changed_cb(keynode_t* node, void* user_data)
543 static int prev_state = VCONFKEY_PM_STATE_NORMAL;
545 if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
546 ERR("Fail to get VCONFKEY_WIFI_STATE");
551 * VCONFKEY_PM_STATE_NORMAL = 1,
552 * VCONFKEY_PM_STATE_LCDDIM,
553 * VCONFKEY_PM_STATE_LCDOFF,
554 * VCONFKEY_PM_STATE_SLEEP
557 new_state = vconf_keynode_get_int(node);
559 netconfig_vconf_get_int(VCONFKEY_PM_STATE, &new_state);
561 DBG("wifi state: %d (0 off / 1 on / 2 connected)", wifi_state);
562 DBG("Old PM state: %d, current: %d", prev_state, new_state);
564 if ((new_state == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
565 netconfig_wifi_bgscan_stop();
566 netconfig_wifi_bgscan_start(TRUE);
569 prev_state = new_state;
572 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
574 int telephony_ready = 0;
577 telephony_ready = vconf_keynode_get_bool(node);
579 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
581 if (telephony_ready != 0) {
582 if (netconfig_tapi_check_sim_state() == FALSE) {
583 DBG("Sim is not initialized yet.");
590 DBG("Turn Wi-Fi on automatically");
592 #if defined TIZEN_WEARABLE
593 wifi_power_on_wearable(TRUE);
599 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
602 int wifi_power_driver_and_supplicant(gboolean enable)
604 /* There are 3 thumb rules for Wi-Fi power management
605 * 1. Do not make exposed API to control wpa_supplicant and driver directly.
606 * It probably breaks ConnMan technology operation.
608 * 2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
609 * It breaks ConnMan technology operation.
611 * 3. Final the best rule: make it as simple as possible.
612 * Simple code enables easy maintenance and reduces logical errors.
614 if (enable == TRUE) {
615 return _load_driver_and_supplicant();
617 if (connman_wifi_technology_state == TRUE)
620 return _remove_driver_and_supplicant();
624 void wifi_power_disable_technology_state_by_only_connman_signal(void)
626 /* Important: it's only done by ConnMan technology signal update */
627 connman_wifi_technology_state = FALSE;
630 void wifi_power_recover_firmware(void)
632 wifi_firmware_recovery_mode = TRUE;
634 netconfig_wifi_bgscan_stop();
639 int wifi_power_on(void)
642 wifi_tech_state_e tech_state;
644 tech_state = wifi_state_get_technology_state();
645 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED) {
646 /* There can be a scenario where wifi is automatically *
647 * activated by connman if wifi was powered in last boot. *
648 * So we should update connman_wifi_technology_state variable *
649 * if it is found that wifi_tech_state variable is *
650 * NETCONFIG_WIFI_TECH_POWERED and connman_wifi_technology_state *
651 * variable is FALSE. Earlier connman_wifi_technology_state *
652 * variable was only updated when wifi was Powered on from *
653 * net-config resulting in variable not getting updated. *
654 * This caused wifi to not get deactivated after reboot if *
655 * last power state was activated */
656 ERR("Net-Config WiFi connman technology state %d",
657 connman_wifi_technology_state);
658 if (connman_wifi_technology_state == FALSE)
659 connman_wifi_technology_state = TRUE;
663 if (__is_wifi_restricted() == TRUE)
666 if (netconfig_is_wifi_tethering_on() == TRUE) {
667 /* TODO: Wi-Fi tethering turns off here */
669 ERR("Failed to turn tethering off");
673 err = wifi_power_driver_and_supplicant(TRUE);
674 if (err < 0 && err != -EALREADY)
677 err = _set_connman_technology_power(TRUE);
682 int wifi_power_off(void)
686 err = _set_connman_technology_power(FALSE);
687 if (err == -EALREADY)
688 wifi_state_update_power_state(FALSE);
693 #if defined TIZEN_WEARABLE
694 int wifi_power_on_wearable(gboolean device_picker_test)
698 wifi_tech_state_e tech_state;
700 tech_state = wifi_state_get_technology_state();
701 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
704 if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
705 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
710 WARN("VCONF_WIFI_WEARABLE_WIFI_USE is OFF");
714 err = wifi_power_driver_and_supplicant(TRUE);
715 if (err < 0 && err != -EALREADY)
718 err = _set_connman_technology_power(TRUE);
720 if (device_picker_test == TRUE)
721 netconfig_wifi_enable_device_picker_test();
727 void wifi_power_initialize(void)
729 int wifi_last_power_state = 0;
731 /* Initialize Airplane mode */
732 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
733 DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
735 /* Update the last Wi-Fi power state */
736 netconfig_vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
737 if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
738 if (TIZEN_TELEPHONY_ENABLE) {
739 int telephony_ready = 0;
740 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
741 if (telephony_ready == 0) {
742 DBG("Telephony API is not initialized yet");
743 vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
744 __netconfig_telephony_ready_changed_cb, NULL);
746 if (netconfig_tapi_check_sim_state() == FALSE)
747 DBG("SIM is not initialized yet");
750 DBG("Turn Wi-Fi on automatically");
751 #if defined TIZEN_WEARABLE
752 wifi_power_on_wearable(TRUE);
758 #if defined TIZEN_WEARABLE
759 vconf_notify_key_changed(VCONF_WIFI_WEARABLE_WIFI_USE, __wearable_wifi_use_changed_cb, NULL);
761 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
762 __netconfig_wifi_airplane_mode, NULL);
764 vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
765 __netconfig_wifi_restrict_mode, NULL);
766 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
767 __netconfig_wifi_airplane_mode, NULL);
770 vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
771 vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
774 void wifi_power_deinitialize(void)
778 gboolean handle_load_driver(Wifi *wifi,
779 GDBusMethodInvocation *context, gboolean device_picker_test)
783 DBG("Wi-Fi power on requested");
785 g_return_val_if_fail(wifi != NULL, FALSE);
787 if (!netconfig_dpm_update_from_wifi()) {
788 DBG("DPM policy restricts Wi-Fi");
789 netconfig_error_permission_denied(context);
793 if (TIZEN_WLAN_BOARD_SPRD)
794 wifi_firmware_download();
796 #if defined TIZEN_WEARABLE
797 err = wifi_power_on_wearable(device_picker_test);
799 err = wifi_power_on();
801 if (device_picker_test == TRUE)
802 netconfig_wifi_enable_device_picker_test();
805 if (err == -EINPROGRESS)
806 netconfig_error_inprogress(context);
807 else if (err == -EALREADY)
808 netconfig_error_already_exists(context);
809 else if (err == -EPERM)
810 netconfig_error_permission_denied(context);
812 netconfig_error_wifi_driver_failed(context);
818 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
819 __netconfig_set_wifi_bssid();
821 wifi_complete_load_driver(wifi, context);
825 gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
829 DBG("Wi-Fi power off requested");
831 g_return_val_if_fail(wifi != NULL, FALSE);
833 err = wifi_power_off();
835 if (err == -EINPROGRESS)
836 netconfig_error_inprogress(context);
837 else if (err == -EALREADY)
838 netconfig_error_already_exists(context);
839 else if (err == -EPERM)
840 netconfig_error_permission_denied(context);
842 netconfig_error_wifi_driver_failed(context);
846 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
848 wifi_complete_remove_driver(wifi, context);
852 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
856 wifi_complete_load_p2p_driver(wifi, context);
860 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
864 wifi_complete_remove_p2p_driver(wifi, context);
868 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
870 DBG("Generate Random Mac address of ethernet");
874 fp = fopen(OS_RANDOM_FILE, "rb");
877 ERR("Could not open /dev/urandom");
880 rc = fread(mac_buf, 1, mac_len, fp);
884 return rc != mac_len ? -1 : 0;
887 void __netconfig_set_ether_macaddr()
889 DBG("Set wired Mac address ");
890 char *mac_addr = NULL;
891 char rand_addr[WLAN_MAC_ADDR_MAX];
894 mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
895 if (mac_addr == NULL) {
896 DBG("vconf_get_str Failed\n");
899 /* Checking Invalid MAC Address */
900 if ((strlen(mac_addr) == 0)) {
901 ERR("Failed to get valid MAC Address from vconf");
902 /*Generate the Random Mac address*/
903 unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
905 if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE) == -1) {
907 ERR("Could not generate the Random Mac address");
912 rand_mac_add[0] &= 0xFE; /*Clear multicase bit*/
913 rand_mac_add[0] |= 0x02; /*set local assignment bit*/
915 /*Set the Mac address in Vconf*/
916 snprintf(rand_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
917 rand_mac_add[0], rand_mac_add[1],
918 rand_mac_add[2], rand_mac_add[3],
919 rand_mac_add[4], rand_mac_add[5]);
921 netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, rand_addr);
922 } else { /* Valid MAC address */
923 g_strlcpy(rand_addr, mac_addr, WLAN_MAC_ADDR_MAX);
926 DBG("MAC Address of eth0 [%s]", rand_addr);
927 const char *path = NET_EXEC_PATH;
928 char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
929 "ether", rand_addr, "up", NULL};
930 char *const envs[] = { NULL };
931 rv = netconfig_execute_file(path, args, envs);
934 ERR("Unable to execute system command");