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 VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT "db/setting/network_with_lcd_off_limit"
53 #define WLAN_MAC_ADDRESS_FILEPATH "/sys/class/net/wlan0/address"
54 #define WLAN_MAC_ADDR_MAX 20
55 #define VCONF_WIFI_BSSID_ADDRESS "db/wifi/bssid_address"
57 #define ETH_MAC_ADDR_SIZE 6
58 #define VCONF_ETH_MAC_ADDRESS "db/dnet/mac_address"
59 #define NET_EXEC_PATH "/sbin/ifconfig"
60 #define OS_RANDOM_FILE "/dev/urandom"
62 #define NETCONFIG_TECH_WAITING_INTERVAL 500
63 #define NETCONFIG_TECH_WAITING_COUNT 6
65 static gboolean connman_wifi_technology_state = FALSE;
66 static gboolean wifi_firmware_recovery_mode = FALSE;
67 static int airplane_mode = 0;
69 static gboolean __is_wifi_restricted(void)
71 #if defined TIZEN_WEARABLE
74 int restricted_mode = 0;
76 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted_mode);
77 if (restricted_mode != 0) {
78 DBG("network restricted mode[%d]", restricted_mode);
85 static void __technology_reply(GObject *source_object, GAsyncResult *res, gpointer user_data)
88 GDBusConnection *conn = NULL;
91 conn = G_DBUS_CONNECTION(source_object);
92 reply = g_dbus_connection_call_finish(conn, res, &error);
96 if (g_strstr_len(error->message, strlen(error->message),
97 CONNMAN_ERROR_INTERFACE ".AlreadyEnabled") != NULL) {
98 wifi_state_update_power_state(TRUE);
99 } else if (g_strstr_len(error->message, strlen(error->message),
100 CONNMAN_ERROR_INTERFACE ".AlreadyDisabled") != NULL) {
101 wifi_state_update_power_state(FALSE);
103 ERR("Fail to request status [%d: %s]", error->code, error->message);
104 wifi_state_update_power_state(FALSE);
108 ERR("Fail to request status");
109 wifi_state_update_power_state(FALSE);
112 DBG("Successfully requested");
113 g_variant_unref(reply);
116 netconfig_gdbus_pending_call_unref();
119 static int __execute_supplicant(gboolean enable)
122 const char *path = WLAN_SUPPLICANT_SCRIPT;
123 char *const args_enable[] = { "/usr/bin/wpa_supp.sh", "start", NULL };
124 char *const args_disable[] = { "/usr/bin/wpa_supp.sh", "stop", NULL };
125 char *const envs[] = { NULL };
126 static gboolean enabled = FALSE;
128 if (enabled == enable)
132 rv = netconfig_execute_file(path, args_enable, envs);
134 rv = netconfig_execute_file(path, args_disable, envs);
138 DBG("wpa_supplicant %s", enable == TRUE ? "started" : "stopped");
145 void netconfig_wifi_recover_firmware(void)
147 wifi_firmware_recovery_mode = TRUE;
149 netconfig_wifi_bgscan_stop();
154 static int _load_driver_and_supplicant(void)
157 wifi_tech_state_e tech_state;
159 tech_state = wifi_state_get_technology_state();
160 if (tech_state > NETCONFIG_WIFI_TECH_OFF)
163 err = __execute_supplicant(TRUE);
164 if (err < 0 && err != -EALREADY)
167 err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, TRUE);
168 if (err < 0 && err != -EALREADY) {
169 __execute_supplicant(FALSE);
173 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
178 static int _remove_driver_and_supplicant(void)
182 DBG("remove driver and supplicant");
183 if (wifi_firmware_recovery_mode != TRUE &&
184 netconfig_wifi_is_bssid_scan_started() == TRUE) {
185 DBG("Wi-Fi WPS mode");
189 err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, FALSE);
190 if (err < 0 && err != -EALREADY)
193 err = __execute_supplicant(FALSE);
194 if (err < 0 && err != -EALREADY)
197 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_OFF);
199 // reset service state
200 wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
202 if (wifi_firmware_recovery_mode == TRUE) {
203 if (wifi_power_on() < 0)
204 ERR("Failed to recover Wi-Fi firmware");
206 wifi_firmware_recovery_mode = FALSE;
212 static gboolean __check_and_set_technology_enable(gpointer data)
214 static int retry_count = NETCONFIG_TECH_WAITING_COUNT;
215 gboolean value_enable = TRUE;
216 gboolean reply = FALSE;
217 GVariant *params = NULL;
218 char key[] = "Powered";
220 if (wifi_state_is_technology_available() == FALSE) {
226 params = g_variant_new("(sv)", key, g_variant_new_boolean(value_enable));
228 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
229 CONNMAN_WIFI_TECHNOLOGY_PREFIX,
230 CONNMAN_TECHNOLOGY_INTERFACE,
231 "SetProperty", params, __technology_reply);
234 ERR("Fail to set technology enable");
235 wifi_state_update_power_state(FALSE);
237 retry_count = NETCONFIG_TECH_WAITING_COUNT;
241 retry_count = NETCONFIG_TECH_WAITING_COUNT;
245 static int _set_connman_technology_power(gboolean enable)
247 gboolean reply = FALSE;
248 GVariant *params = NULL;
249 char key[] = "Powered";
250 gboolean value_enable = TRUE;
251 gboolean value_disable = FALSE;
253 if (connman_wifi_technology_state == enable)
256 if (enable && wifi_state_is_technology_available() == FALSE) {
257 netconfig_start_timer(NETCONFIG_TECH_WAITING_INTERVAL,
258 __check_and_set_technology_enable, NULL, NULL);
259 connman_wifi_technology_state = enable;
263 params = g_variant_new("(sv)", key, (enable == TRUE) ?
264 g_variant_new_boolean(value_enable) : g_variant_new_boolean(value_disable));
266 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
267 CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
268 "SetProperty", params, __technology_reply);
271 ERR("Fail to set technology %s", enable == TRUE ? "enable" : "disable");
275 /* If Wi-Fi powered off,
276 * Do not remove Wi-Fi driver until ConnMan technology state updated
279 connman_wifi_technology_state = enable;
281 /* To be keep safe, early disable Wi-Fi tech state */
283 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
288 static void __netconfig_set_wifi_bssid(void)
291 char bssid[WLAN_MAC_ADDR_MAX];
293 FILE *fp = fopen(WLAN_MAC_ADDRESS_FILEPATH, "r");
296 ERR("Fail to open %s", WLAN_MAC_ADDRESS_FILEPATH);
300 fseek(fp, 0L, SEEK_SET);
301 rv = fscanf(fp, "%17s", bssid);
304 ERR("Fail to read bssid");
306 netconfig_set_vconf_str(VCONF_WIFI_BSSID_ADDRESS, bssid);
311 void netconfig_wifi_disable_technology_state_by_only_connman_signal(void)
313 /* Important: it's only done by ConnMan technology signal update */
314 connman_wifi_technology_state = FALSE;
317 #if defined TIZEN_WEARABLE
318 int netconfig_wifi_on_wearable(gboolean device_picker_test)
323 if (netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_mode) < 0) {
324 ERR("Fail to get VCONFKEY_SETAPPL_PSMODE");
328 if (ps_mode > SETTING_PSMODE_NORMAL) {
329 WARN("ps mode is on(%d), Not turn on Wi-Fi", ps_mode);
333 err = wifi_power_driver_and_supplicant(TRUE);
334 if (err < 0 && err != -EALREADY)
337 err = _set_connman_technology_power(TRUE);
339 if (device_picker_test == TRUE)
340 netconfig_wifi_enable_device_picker_test();
345 static void __netconfig_wifi_restrict_mode(keynode_t *node, void *user_data)
347 int wifi_state = 0, restricted = 0;
348 int wifi_off_by_restricted = 0;
350 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
352 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
355 restricted = vconf_keynode_get_bool(node);
357 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted);
359 DBG("network restricted mode %s", restricted > 0 ? "ON" : "OFF");
360 DBG("Wi-Fi state %d, Wi-Fi was off by restricted mode %s", wifi_state,
361 wifi_off_by_restricted ? "Yes" : "No");
363 if (restricted > 0) {
364 /* network restricted on */
365 if (wifi_state == VCONFKEY_WIFI_OFF)
370 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 1);
372 /* network restricted off */
373 if (!wifi_off_by_restricted)
376 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 0);
378 if (wifi_state > VCONFKEY_WIFI_OFF)
386 static void __netconfig_wifi_airplane_mode(keynode_t *node, void *user_data)
388 int wifi_state = 0, airplane_state = 0;
389 int wifi_off_by_airplane = 0;
391 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
392 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
395 airplane_state = vconf_keynode_get_bool(node);
397 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
399 DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
400 DBG("Wi-Fi state(or use) %d, Wi-Fi was off by flight mode %s", wifi_state,
401 wifi_off_by_airplane ? "Yes" : "No");
403 if (airplane_mode == airplane_state)
406 airplane_mode = airplane_state;
408 if (airplane_state > 0) {
409 /* airplane mode on */
410 if (wifi_state == VCONFKEY_WIFI_OFF)
415 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
417 /* airplane mode off */
418 if (!wifi_off_by_airplane)
421 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
422 if (wifi_state > VCONFKEY_WIFI_OFF)
429 static void __emergency_mode_changed_cb(keynode_t *node, void *user_data)
431 int wifi_state = 0, emergency = 0;
432 int wifi_off_by_emergency = 0;
433 #if !defined TIZEN_WEARABLE
434 int emergency_by_fmm = 0;
436 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
437 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
439 #if !defined TIZEN_WEARABLE
440 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT, &emergency_by_fmm);
441 DBG("emergency mode by Find My Mobile (%d)", emergency_by_fmm);
442 if (emergency_by_fmm == 1)
447 emergency = vconf_keynode_get_int(node);
449 netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &emergency);
451 DBG("emergency mode %s", emergency > SETTING_PSMODE_POWERFUL ? "ON" : "OFF");
452 DBG("Wi-Fi state %d, Wi-Fi was off by emergency mode %s", wifi_state, wifi_off_by_emergency ? "Yes" : "No");
454 #if defined TIZEN_WEARABLE
455 if (emergency == SETTING_PSMODE_WEARABLE) {
456 /* basic power saving mode on */
457 } else if (emergency == SETTING_PSMODE_WEARABLE_ENHANCED) {
458 /* enhanced power saving mode on */
459 if (wifi_state == VCONFKEY_WIFI_OFF)
463 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
465 /* power saving mode off */
466 if (!wifi_off_by_emergency)
469 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
470 if (wifi_state > VCONFKEY_WIFI_OFF)
473 wifi_power_on_wearable(TRUE);
476 if (emergency > SETTING_PSMODE_POWERFUL) {
477 /* emergency mode on */
478 if (wifi_state == VCONFKEY_WIFI_OFF)
483 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
485 /* emergency mode off */
486 if (!wifi_off_by_emergency)
489 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
491 if (wifi_state > VCONFKEY_WIFI_OFF)
500 static void __pm_state_changed_cb(keynode_t* node, void* user_data)
504 static int prev_state = VCONFKEY_PM_STATE_NORMAL;
506 if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
507 ERR("Fail to get VCONFKEY_WIFI_STATE");
512 * VCONFKEY_PM_STATE_NORMAL = 1,
513 * VCONFKEY_PM_STATE_LCDDIM,
514 * VCONFKEY_PM_STATE_LCDOFF,
515 * VCONFKEY_PM_STATE_SLEEP
518 new_state = vconf_keynode_get_int(node);
520 netconfig_vconf_get_int(VCONFKEY_PM_STATE, &new_state);
522 DBG("wifi state: %d (0 off / 1 on / 2 connected)", wifi_state);
523 DBG("Old PM state: %d, current: %d (1 normal / 2 lcddim / 3 lcdoff / 4 sleep)", prev_state, new_state);
525 if ((new_state == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
526 netconfig_wifi_bgscan_stop();
527 netconfig_wifi_bgscan_set_interval(SCAN_EXPONENTIAL_MIN);
528 netconfig_wifi_bgscan_start(TRUE);
531 prev_state = new_state;
534 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
536 int telephony_ready = 0;
539 telephony_ready = vconf_keynode_get_bool(node);
541 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
543 if (telephony_ready != 0) {
544 if (netconfig_tapi_check_sim_state() == FALSE) {
545 DBG("Sim is not initialized yet.");
552 DBG("Turn Wi-Fi on automatically");
554 #if defined TIZEN_WEARABLE
555 wifi_power_on_wearable(TRUE);
561 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
564 int wifi_power_driver_and_supplicant(gboolean enable)
566 /* There are 3 thumb rules for Wi-Fi power management
567 * 1. Do not make exposed API to control wpa_supplicant and driver directly.
568 * It probably breaks ConnMan technology operation.
570 * 2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
571 * It breaks ConnMan technology operation.
573 * 3. Final the best rule: make it as simple as possible.
574 * Simple code enables easy maintenance and reduces logical errors.
576 if (enable == TRUE) {
577 return _load_driver_and_supplicant();
579 if (connman_wifi_technology_state == TRUE)
582 return _remove_driver_and_supplicant();
586 void wifi_power_disable_technology_state_by_only_connman_signal(void)
588 /* Important: it's only done by ConnMan technology signal update */
589 connman_wifi_technology_state = FALSE;
592 void wifi_power_recover_firmware(void)
594 wifi_firmware_recovery_mode = TRUE;
596 netconfig_wifi_bgscan_stop();
601 int wifi_power_on(void)
604 wifi_tech_state_e tech_state;
606 tech_state = wifi_state_get_technology_state();
607 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED) {
608 /* There can be a scenario where wifi is automatically *
609 * activated by connman if wifi was powered in last boot. *
610 * So we should update connman_wifi_technology_state variable *
611 * if it is found that wifi_tech_state variable is *
612 * NETCONFIG_WIFI_TECH_POWERED and connman_wifi_technology_state *
613 * variable is FALSE. Earlier connman_wifi_technology_state *
614 * variable was only updated when wifi was Powered on from *
615 * net-config resulting in variable not getting updated. *
616 * This caused wifi to not get deactivated after reboot if *
617 * last power state was activated */
618 ERR("Net-Config WiFi connman technology state %d",
619 connman_wifi_technology_state);
620 if (connman_wifi_technology_state == FALSE)
621 connman_wifi_technology_state = TRUE;
625 if (__is_wifi_restricted() == TRUE)
628 if (netconfig_is_wifi_tethering_on() == TRUE) {
629 /* TODO: Wi-Fi tethering turns off here */
631 ERR("Failed to turn tethering off");
635 err = wifi_power_driver_and_supplicant(TRUE);
636 if (err < 0 && err != -EALREADY)
639 err = _set_connman_technology_power(TRUE);
644 int wifi_power_off(void)
648 err = _set_connman_technology_power(FALSE);
649 if (err == -EALREADY)
650 wifi_state_update_power_state(FALSE);
655 #if defined TIZEN_WEARABLE
656 int wifi_power_on_wearable(gboolean device_picker_test)
659 wifi_tech_state_e tech_state;
661 tech_state = wifi_state_get_technology_state();
662 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
665 err = wifi_power_driver_and_supplicant(TRUE);
666 if (err < 0 && err != -EALREADY)
669 err = _set_connman_technology_power(TRUE);
671 if (device_picker_test == TRUE)
672 netconfig_wifi_enable_device_picker_test();
678 void wifi_power_initialize(void)
680 int wifi_last_power_state = 0;
682 /* Initialize Airplane mode */
683 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
684 DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
686 /* Update the last Wi-Fi power state */
687 netconfig_vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
688 if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
689 if (TIZEN_TELEPHONY_ENABLE) {
690 int telephony_ready = 0;
691 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
692 if (telephony_ready == 0) {
693 DBG("Telephony API is not initialized yet");
694 vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
695 __netconfig_telephony_ready_changed_cb, NULL);
697 if (netconfig_tapi_check_sim_state() == FALSE)
698 DBG("SIM is not initialized yet");
701 DBG("Turn Wi-Fi on automatically");
702 #if defined TIZEN_WEARABLE
703 wifi_power_on_wearable(TRUE);
709 #if defined TIZEN_WEARABLE
710 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
711 __netconfig_wifi_airplane_mode, NULL);
713 vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
714 __netconfig_wifi_restrict_mode, NULL);
715 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
716 __netconfig_wifi_airplane_mode, NULL);
719 vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
720 vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
723 void wifi_power_deinitialize(void)
727 gboolean handle_load_driver(Wifi *wifi,
728 GDBusMethodInvocation *context, gboolean device_picker_test)
732 DBG("Wi-Fi power on requested");
734 g_return_val_if_fail(wifi != NULL, TRUE);
736 if (!netconfig_dpm_update_from_wifi()) {
737 DBG("DPM policy restricts Wi-Fi");
738 netconfig_error_permission_denied(context);
742 if (TIZEN_WLAN_BOARD_SPRD)
743 wifi_firmware_download();
745 #if defined TIZEN_WEARABLE
746 err = wifi_power_on_wearable(device_picker_test);
748 err = wifi_power_on();
750 if (device_picker_test == TRUE)
751 netconfig_wifi_enable_device_picker_test();
754 if (err == -EALREADY)
755 netconfig_error_already_exists(context);
756 else if (err == -EPERM)
757 netconfig_error_permission_denied(context);
759 netconfig_error_wifi_driver_failed(context);
765 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
766 __netconfig_set_wifi_bssid();
768 wifi_complete_load_driver(wifi, context);
772 gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
776 DBG("Wi-Fi power off requested");
778 g_return_val_if_fail(wifi != NULL, TRUE);
780 err = wifi_power_off();
782 if (err == -EINPROGRESS)
783 netconfig_error_inprogress(context);
784 else if (err == -EALREADY)
785 netconfig_error_already_exists(context);
786 else if (err == -EPERM)
787 netconfig_error_permission_denied(context);
789 netconfig_error_wifi_driver_failed(context);
793 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
795 wifi_complete_remove_driver(wifi, context);
799 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
803 wifi_complete_load_p2p_driver(wifi, context);
807 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
811 wifi_complete_remove_p2p_driver(wifi, context);
815 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
817 DBG("Generate Random Mac address of ethernet");
821 fp = fopen(OS_RANDOM_FILE, "rb");
824 ERR("Could not open /dev/urandom");
827 rc = fread(mac_buf, 1, mac_len, fp);
831 return rc != mac_len ? -1 : 0;
834 void __netconfig_set_ether_macaddr()
836 DBG("Set wired Mac address ");
837 char *mac_addr = NULL;
838 char rand_addr[WLAN_MAC_ADDR_MAX];
841 mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
842 if (mac_addr == NULL) {
843 DBG("vconf_get_str Failed\n");
846 /* Checking Invalid MAC Address */
847 if ((strlen(mac_addr) == 0)) {
848 ERR("Failed to get valid MAC Address from vconf");
849 /*Generate the Random Mac address*/
850 unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
852 if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE) == -1) {
854 ERR("Could not generate the Random Mac address");
859 rand_mac_add[0] &= 0xFE; /*Clear multicase bit*/
860 rand_mac_add[0] |= 0x02; /*set local assignment bit*/
862 /*Set the Mac address in Vconf*/
863 snprintf(rand_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
864 rand_mac_add[0], rand_mac_add[1],
865 rand_mac_add[2], rand_mac_add[3],
866 rand_mac_add[4], rand_mac_add[5]);
868 netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, rand_addr);
869 } else { /* Valid MAC address */
870 g_strlcpy(rand_addr, mac_addr, WLAN_MAC_ADDR_MAX);
873 DBG("MAC Address of eth0 [%s]", rand_addr);
874 const char *path = NET_EXEC_PATH;
875 char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
876 "ether", rand_addr, "up", NULL};
877 char *const envs[] = { NULL };
878 rv = netconfig_execute_file(path, args, envs);
881 ERR("Unable to execute system command");