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-bssid-scan.h"
34 #include "wifi-power.h"
35 #include "wifi-state.h"
36 #include "netsupplicant.h"
37 #include "network-state.h"
38 #include "network-dpm.h"
39 #include "wifi-firmware.h"
40 #include "wifi-background-scan.h"
43 #define WLAN_SUPPLICANT_SCRIPT "/usr/bin/wpa_supp.sh"
44 #define P2P_SUPPLICANT_SCRIPT "/usr/bin/p2p_supp.sh"
46 #define VCONF_WIFI_OFF_STATE_BY_AIRPLANE "file/private/wifi/wifi_off_by_airplane"
47 #define VCONF_WIFI_OFF_STATE_BY_RESTRICTED "file/private/wifi/wifi_off_by_restricted"
48 #define VCONF_WIFI_OFF_STATE_BY_EMERGENCY "file/private/wifi/wifi_off_by_emergency"
49 #if defined TIZEN_WEARABLE
50 #define VCONF_WIFI_WEARABLE_WIFI_USE "db/private/wifi/wearable_wifi_use"
52 #if !defined TIZEN_WEARABLE
53 #define VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT "db/setting/network_with_lcd_off_limit"
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_bssid_scan_started() == 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_ADDRESS_FILEPATH, "r");
265 ERR("Fail to open %s", WLAN_MAC_ADDRESS_FILEPATH);
269 fseek(fp, 0L, SEEK_SET);
270 rv = fscanf(fp, "%17s", bssid);
273 ERR("Fail to read bssid");
275 netconfig_set_vconf_str(VCONF_WIFI_BSSID_ADDRESS, bssid);
280 void netconfig_wifi_disable_technology_state_by_only_connman_signal(void)
282 /* Important: it's only done by ConnMan technology signal update */
283 connman_wifi_technology_state = FALSE;
286 #if defined TIZEN_WEARABLE
287 int netconfig_wifi_on_wearable(gboolean device_picker_test)
293 if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
294 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
299 if (netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_mode) < 0) {
300 ERR("Fail to get VCONFKEY_SETAPPL_PSMODE");
304 if (ps_mode > SETTING_PSMODE_NORMAL) {
305 WARN("ps mode is on(%d), Not turn on Wi-Fi", ps_mode);
309 WARN("Not permitted Wi-Fi on");
313 err = wifi_power_driver_and_supplicant(TRUE);
314 if (err < 0 && err != -EALREADY)
317 err = _set_connman_technology_power(TRUE);
319 if (device_picker_test == TRUE)
320 netconfig_wifi_enable_device_picker_test();
325 static void __wearable_wifi_use_changed_cb(keynode_t* node, void* user_data)
330 if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
331 ERR("Fail to get VCONFKEY_WIFI_STATE");
336 wifi_use = vconf_keynode_get_int(node);
338 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
342 if (wifi_state > VCONFKEY_WIFI_OFF) {
343 WARN("Wi-Fi is already turned on");
346 wifi_power_on_wearable(TRUE);
348 ERR("## wifi use [OFF]");
349 if (wifi_state == VCONFKEY_WIFI_OFF) {
350 WARN("Wi-Fi is already turned off");
358 static void __netconfig_wifi_restrict_mode(keynode_t *node, void *user_data)
360 int wifi_state = 0, restricted = 0;
361 int wifi_off_by_restricted = 0;
363 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
365 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
368 restricted = vconf_keynode_get_bool(node);
370 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted);
372 DBG("network restricted mode %s", restricted > 0 ? "ON" : "OFF");
373 DBG("Wi-Fi state %d, Wi-Fi was off by restricted mode %s", wifi_state,
374 wifi_off_by_restricted ? "Yes" : "No");
376 if (restricted > 0) {
377 /* network restricted on */
378 if (wifi_state == VCONFKEY_WIFI_OFF)
383 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 1);
385 /* network restricted off */
386 if (!wifi_off_by_restricted)
389 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 0);
391 if (wifi_state > VCONFKEY_WIFI_OFF)
399 static void __netconfig_wifi_airplane_mode(keynode_t *node, void *user_data)
401 int wifi_state = 0, airplane_state = 0;
402 int wifi_off_by_airplane = 0;
404 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
406 #if defined TIZEN_WEARABLE
407 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_state)
409 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);
434 #if defined TIZEN_WEARABLE
435 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
438 /* airplane mode off */
439 if (!wifi_off_by_airplane)
442 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
443 #if defined TIZEN_WEARABLE
444 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 1);
446 if (wifi_state > VCONFKEY_WIFI_OFF)
454 static void __emergency_mode_changed_cb(keynode_t *node, void *user_data)
456 int wifi_state = 0, emergency = 0;
457 int wifi_off_by_emergency = 0;
458 #if !defined TIZEN_WEARABLE
459 int emergency_by_fmm = 0;
461 #if defined TIZEN_WEARABLE
465 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
466 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
468 #if !defined TIZEN_WEARABLE
469 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT, &emergency_by_fmm);
470 DBG("emergency mode by Find My Mobile (%d)", emergency_by_fmm);
471 if (emergency_by_fmm == 1)
476 emergency = vconf_keynode_get_int(node);
478 netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &emergency);
480 DBG("emergency mode %s", emergency > SETTING_PSMODE_POWERFUL ? "ON" : "OFF");
481 DBG("Wi-Fi state %d, Wi-Fi was off by emergency mode %s", wifi_state, wifi_off_by_emergency ? "Yes" : "No");
483 #if defined TIZEN_WEARABLE
484 if (emergency == SETTING_PSMODE_WEARABLE) {
485 /* basic power saving mode on */
486 } else if (emergency == SETTING_PSMODE_WEARABLE_ENHANCED) {
487 /* enhanced power saving mode on */
488 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
489 psmode_wifi_use = wifi_use;
491 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
493 if (wifi_state == VCONFKEY_WIFI_OFF)
497 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
499 /* power saving mode off */
500 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, psmode_wifi_use);
501 if (!wifi_off_by_emergency)
504 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
505 if (wifi_state > VCONFKEY_WIFI_OFF)
508 wifi_power_on_wearable(TRUE);
511 if (emergency > SETTING_PSMODE_POWERFUL) {
512 /* emergency mode on */
513 if (wifi_state == VCONFKEY_WIFI_OFF)
518 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
520 /* emergency mode off */
521 if (!wifi_off_by_emergency)
524 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
526 if (wifi_state > VCONFKEY_WIFI_OFF)
535 static void __pm_state_changed_cb(keynode_t* node, void* user_data)
539 static int prev_state = VCONFKEY_PM_STATE_NORMAL;
541 if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
542 ERR("Fail to get VCONFKEY_WIFI_STATE");
547 * VCONFKEY_PM_STATE_NORMAL = 1,
548 * VCONFKEY_PM_STATE_LCDDIM,
549 * VCONFKEY_PM_STATE_LCDOFF,
550 * VCONFKEY_PM_STATE_SLEEP
553 new_state = vconf_keynode_get_int(node);
555 netconfig_vconf_get_int(VCONFKEY_PM_STATE, &new_state);
557 DBG("wifi state: %d (0 off / 1 on / 2 connected)", wifi_state);
558 DBG("Old PM state: %d, current: %d", prev_state, new_state);
560 if ((new_state == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
561 netconfig_wifi_bgscan_stop();
562 netconfig_wifi_bgscan_start(TRUE);
565 prev_state = new_state;
568 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
570 int telephony_ready = 0;
573 telephony_ready = vconf_keynode_get_bool(node);
575 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
577 if (telephony_ready != 0) {
578 if (netconfig_tapi_check_sim_state() == FALSE) {
579 DBG("Sim is not initialized yet.");
586 DBG("Turn Wi-Fi on automatically");
588 #if defined TIZEN_WEARABLE
589 wifi_power_on_wearable(TRUE);
595 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
598 int wifi_power_driver_and_supplicant(gboolean enable)
600 /* There are 3 thumb rules for Wi-Fi power management
601 * 1. Do not make exposed API to control wpa_supplicant and driver directly.
602 * It probably breaks ConnMan technology operation.
604 * 2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
605 * It breaks ConnMan technology operation.
607 * 3. Final the best rule: make it as simple as possible.
608 * Simple code enables easy maintenance and reduces logical errors.
610 if (enable == TRUE) {
611 return _load_driver_and_supplicant();
613 if (connman_wifi_technology_state == TRUE)
616 return _remove_driver_and_supplicant();
620 void wifi_power_disable_technology_state_by_only_connman_signal(void)
622 /* Important: it's only done by ConnMan technology signal update */
623 connman_wifi_technology_state = FALSE;
626 void wifi_power_recover_firmware(void)
628 wifi_firmware_recovery_mode = TRUE;
630 netconfig_wifi_bgscan_stop();
635 int wifi_power_on(void)
638 wifi_tech_state_e tech_state;
640 tech_state = wifi_state_get_technology_state();
641 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED) {
642 /* There can be a scenario where wifi is automatically *
643 * activated by connman if wifi was powered in last boot. *
644 * So we should update connman_wifi_technology_state variable *
645 * if it is found that wifi_tech_state variable is *
646 * NETCONFIG_WIFI_TECH_POWERED and connman_wifi_technology_state *
647 * variable is FALSE. Earlier connman_wifi_technology_state *
648 * variable was only updated when wifi was Powered on from *
649 * net-config resulting in variable not getting updated. *
650 * This caused wifi to not get deactivated after reboot if *
651 * last power state was activated */
652 ERR("Net-Config WiFi connman technology state %d",
653 connman_wifi_technology_state);
654 if (connman_wifi_technology_state == FALSE)
655 connman_wifi_technology_state = TRUE;
659 if (__is_wifi_restricted() == TRUE)
662 if (netconfig_is_wifi_tethering_on() == TRUE) {
663 /* TODO: Wi-Fi tethering turns off here */
665 ERR("Failed to turn tethering off");
669 err = wifi_power_driver_and_supplicant(TRUE);
670 if (err < 0 && err != -EALREADY)
673 err = _set_connman_technology_power(TRUE);
678 int wifi_power_off(void)
682 err = _set_connman_technology_power(FALSE);
683 if (err == -EALREADY)
684 wifi_state_update_power_state(FALSE);
689 #if defined TIZEN_WEARABLE
690 int wifi_power_on_wearable(gboolean device_picker_test)
694 wifi_tech_state_e tech_state;
696 tech_state = wifi_state_get_technology_state();
697 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
700 if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
701 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
706 WARN("VCONF_WIFI_WEARABLE_WIFI_USE is OFF");
710 err = wifi_power_driver_and_supplicant(TRUE);
711 if (err < 0 && err != -EALREADY)
714 err = _set_connman_technology_power(TRUE);
716 if (device_picker_test == TRUE)
717 netconfig_wifi_enable_device_picker_test();
723 void wifi_power_initialize(void)
725 int wifi_last_power_state = 0;
727 /* Initialize Airplane mode */
728 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
729 DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
731 /* Update the last Wi-Fi power state */
732 netconfig_vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
733 if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
734 if (TIZEN_TELEPHONY_ENABLE) {
735 int telephony_ready = 0;
736 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
737 if (telephony_ready == 0) {
738 DBG("Telephony API is not initialized yet");
739 vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
740 __netconfig_telephony_ready_changed_cb, NULL);
742 if (netconfig_tapi_check_sim_state() == FALSE)
743 DBG("SIM is not initialized yet");
746 DBG("Turn Wi-Fi on automatically");
747 #if defined TIZEN_WEARABLE
748 wifi_power_on_wearable(TRUE);
754 #if defined TIZEN_WEARABLE
755 vconf_notify_key_changed(VCONF_WIFI_WEARABLE_WIFI_USE, __wearable_wifi_use_changed_cb, NULL);
757 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
758 __netconfig_wifi_airplane_mode, NULL);
760 vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
761 __netconfig_wifi_restrict_mode, NULL);
762 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
763 __netconfig_wifi_airplane_mode, NULL);
766 vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
767 vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
770 void wifi_power_deinitialize(void)
774 gboolean handle_load_driver(Wifi *wifi,
775 GDBusMethodInvocation *context, gboolean device_picker_test)
779 DBG("Wi-Fi power on requested");
781 g_return_val_if_fail(wifi != NULL, FALSE);
783 if (!netconfig_dpm_update_from_wifi()) {
784 DBG("DPM policy restricts Wi-Fi");
785 netconfig_error_permission_denied(context);
789 if (TIZEN_WLAN_BOARD_SPRD)
790 wifi_firmware_download();
792 #if defined TIZEN_WEARABLE
793 err = wifi_power_on_wearable(device_picker_test);
795 err = wifi_power_on();
797 if (device_picker_test == TRUE)
798 netconfig_wifi_enable_device_picker_test();
801 if (err == -EINPROGRESS)
802 netconfig_error_inprogress(context);
803 else if (err == -EALREADY)
804 netconfig_error_already_exists(context);
805 else if (err == -EPERM)
806 netconfig_error_permission_denied(context);
808 netconfig_error_wifi_driver_failed(context);
814 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
815 __netconfig_set_wifi_bssid();
817 wifi_complete_load_driver(wifi, context);
821 gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
825 DBG("Wi-Fi power off requested");
827 g_return_val_if_fail(wifi != NULL, FALSE);
829 err = wifi_power_off();
831 if (err == -EINPROGRESS)
832 netconfig_error_inprogress(context);
833 else if (err == -EALREADY)
834 netconfig_error_already_exists(context);
835 else if (err == -EPERM)
836 netconfig_error_permission_denied(context);
838 netconfig_error_wifi_driver_failed(context);
842 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
844 wifi_complete_remove_driver(wifi, context);
848 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
852 wifi_complete_load_p2p_driver(wifi, context);
856 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
860 wifi_complete_remove_p2p_driver(wifi, context);
864 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
866 DBG("Generate Random Mac address of ethernet");
870 fp = fopen(OS_RANDOM_FILE, "rb");
873 ERR("Could not open /dev/urandom");
876 rc = fread(mac_buf, 1, mac_len, fp);
880 return rc != mac_len ? -1 : 0;
883 void __netconfig_set_ether_macaddr()
885 DBG("Set wired Mac address ");
886 char *mac_addr = NULL;
887 char rand_addr[WLAN_MAC_ADDR_MAX];
890 mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
891 if (mac_addr == NULL) {
892 DBG("vconf_get_str Failed\n");
895 /* Checking Invalid MAC Address */
896 if ((strlen(mac_addr) == 0)) {
897 ERR("Failed to get valid MAC Address from vconf");
898 /*Generate the Random Mac address*/
899 unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
901 if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE) == -1) {
903 ERR("Could not generate the Random Mac address");
908 rand_mac_add[0] &= 0xFE; /*Clear multicase bit*/
909 rand_mac_add[0] |= 0x02; /*set local assignment bit*/
911 /*Set the Mac address in Vconf*/
912 snprintf(rand_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
913 rand_mac_add[0], rand_mac_add[1],
914 rand_mac_add[2], rand_mac_add[3],
915 rand_mac_add[4], rand_mac_add[5]);
917 netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, rand_addr);
918 } else { /* Valid MAC address */
919 g_strlcpy(rand_addr, mac_addr, WLAN_MAC_ADDR_MAX);
922 DBG("MAC Address of eth0 [%s]", rand_addr);
923 const char *path = NET_EXEC_PATH;
924 char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
925 "ether", rand_addr, "up", NULL};
926 char *const envs[] = { NULL };
927 rv = netconfig_execute_file(path, args, envs);
930 ERR("Unable to execute system command");