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 #define NETCONFIG_TECH_WAITING_INTERVAL 500
66 #define NETCONFIG_TECH_WAITING_COUNT 6
68 static gboolean connman_wifi_technology_state = FALSE;
69 static gboolean wifi_firmware_recovery_mode = FALSE;
70 static int airplane_mode = 0;
72 #if defined TIZEN_WEARABLE
73 static int psmode_wifi_use = 1;
76 static gboolean __is_wifi_restricted(void)
78 #if defined TIZEN_WEARABLE
81 int restricted_mode = 0;
83 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted_mode);
84 if (restricted_mode != 0) {
85 DBG("network restricted mode[%d]", restricted_mode);
92 static void __technology_reply(GObject *source_object, GAsyncResult *res, gpointer user_data)
95 GDBusConnection *conn = NULL;
98 conn = G_DBUS_CONNECTION(source_object);
99 reply = g_dbus_connection_call_finish(conn, res, &error);
103 if (g_strstr_len(error->message, strlen(error->message),
104 CONNMAN_ERROR_INTERFACE ".AlreadyEnabled") != NULL) {
105 wifi_state_update_power_state(TRUE);
106 } else if (g_strstr_len(error->message, strlen(error->message),
107 CONNMAN_ERROR_INTERFACE ".AlreadyDisabled") != NULL) {
108 wifi_state_update_power_state(FALSE);
110 ERR("Fail to request status [%d: %s]", error->code, error->message);
111 wifi_state_update_power_state(FALSE);
115 ERR("Fail to request status");
116 wifi_state_update_power_state(FALSE);
119 DBG("Successfully requested");
120 g_variant_unref(reply);
123 netconfig_gdbus_pending_call_unref();
126 static int __execute_supplicant(gboolean enable)
129 const char *path = WLAN_SUPPLICANT_SCRIPT;
130 char *const args_enable[] = { "/usr/bin/wpa_supp.sh", "start", NULL };
131 char *const args_disable[] = { "/usr/bin/wpa_supp.sh", "stop", NULL };
132 char *const envs[] = { NULL };
133 static gboolean enabled = FALSE;
135 if (enabled == enable)
139 rv = netconfig_execute_file(path, args_enable, envs);
141 rv = netconfig_execute_file(path, args_disable, envs);
145 DBG("wpa_supplicant %s", enable == TRUE ? "started" : "stopped");
152 void netconfig_wifi_recover_firmware(void)
154 wifi_firmware_recovery_mode = TRUE;
156 netconfig_wifi_bgscan_stop();
161 static int _load_driver_and_supplicant(void)
164 wifi_tech_state_e tech_state;
166 tech_state = wifi_state_get_technology_state();
167 if (tech_state > NETCONFIG_WIFI_TECH_OFF)
170 err = __execute_supplicant(TRUE);
171 if (err < 0 && err != -EALREADY)
174 err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, TRUE);
175 if (err < 0 && err != -EALREADY) {
176 __execute_supplicant(FALSE);
180 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
185 static int _remove_driver_and_supplicant(void)
189 DBG("remove driver and supplicant");
190 if (wifi_firmware_recovery_mode != TRUE &&
191 netconfig_wifi_is_bssid_scan_started() == TRUE) {
192 DBG("Wi-Fi WPS mode");
196 err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, FALSE);
197 if (err < 0 && err != -EALREADY)
200 err = __execute_supplicant(FALSE);
201 if (err < 0 && err != -EALREADY)
204 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_OFF);
206 // reset service state
207 wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
209 if (wifi_firmware_recovery_mode == TRUE) {
210 if (wifi_power_on() < 0)
211 ERR("Failed to recover Wi-Fi firmware");
213 wifi_firmware_recovery_mode = FALSE;
219 static gboolean __check_and_set_technology_enable(gpointer data)
221 static int retry_count = NETCONFIG_TECH_WAITING_COUNT;
222 gboolean value_enable = TRUE;
223 gboolean reply = FALSE;
224 GVariant *params = NULL;
225 char key[] = "Powered";
227 if (wifi_state_is_technology_available() == FALSE) {
233 params = g_variant_new("(sv)", key, g_variant_new_boolean(value_enable));
235 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
236 CONNMAN_WIFI_TECHNOLOGY_PREFIX,
237 CONNMAN_TECHNOLOGY_INTERFACE,
238 "SetProperty", params, __technology_reply);
241 ERR("Fail to set technology enable");
242 wifi_state_update_power_state(FALSE);
244 retry_count = NETCONFIG_TECH_WAITING_COUNT;
248 retry_count = NETCONFIG_TECH_WAITING_COUNT;
252 static int _set_connman_technology_power(gboolean enable)
254 gboolean reply = FALSE;
255 GVariant *params = NULL;
256 char key[] = "Powered";
257 gboolean value_enable = TRUE;
258 gboolean value_disable = FALSE;
260 if (connman_wifi_technology_state == enable)
263 if (enable && wifi_state_is_technology_available() == FALSE) {
264 netconfig_start_timer(NETCONFIG_TECH_WAITING_INTERVAL,
265 __check_and_set_technology_enable, NULL, NULL);
266 connman_wifi_technology_state = enable;
270 params = g_variant_new("(sv)", key, (enable == TRUE) ?
271 g_variant_new_boolean(value_enable) : g_variant_new_boolean(value_disable));
273 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
274 CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
275 "SetProperty", params, __technology_reply);
278 ERR("Fail to set technology %s", enable == TRUE ? "enable" : "disable");
282 /* If Wi-Fi powered off,
283 * Do not remove Wi-Fi driver until ConnMan technology state updated
286 connman_wifi_technology_state = enable;
288 /* To be keep safe, early disable Wi-Fi tech state */
290 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
295 static void __netconfig_set_wifi_bssid(void)
298 char bssid[WLAN_MAC_ADDR_MAX];
300 FILE *fp = fopen(WLAN_MAC_ADDRESS_FILEPATH, "r");
303 ERR("Fail to open %s", WLAN_MAC_ADDRESS_FILEPATH);
307 fseek(fp, 0L, SEEK_SET);
308 rv = fscanf(fp, "%17s", bssid);
311 ERR("Fail to read bssid");
313 netconfig_set_vconf_str(VCONF_WIFI_BSSID_ADDRESS, bssid);
318 void netconfig_wifi_disable_technology_state_by_only_connman_signal(void)
320 /* Important: it's only done by ConnMan technology signal update */
321 connman_wifi_technology_state = FALSE;
324 #if defined TIZEN_WEARABLE
325 int netconfig_wifi_on_wearable(gboolean device_picker_test)
331 if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
332 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
337 if (netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_mode) < 0) {
338 ERR("Fail to get VCONFKEY_SETAPPL_PSMODE");
342 if (ps_mode > SETTING_PSMODE_NORMAL) {
343 WARN("ps mode is on(%d), Not turn on Wi-Fi", ps_mode);
347 WARN("Not permitted Wi-Fi on");
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 __wearable_wifi_use_changed_cb(keynode_t* node, void* user_data)
368 if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
369 ERR("Fail to get VCONFKEY_WIFI_STATE");
374 wifi_use = vconf_keynode_get_int(node);
376 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
380 if (wifi_state > VCONFKEY_WIFI_OFF) {
381 WARN("Wi-Fi is already turned on");
384 wifi_power_on_wearable(TRUE);
386 ERR("## wifi use [OFF]");
387 if (wifi_state == VCONFKEY_WIFI_OFF) {
388 WARN("Wi-Fi is already turned off");
396 static void __netconfig_wifi_restrict_mode(keynode_t *node, void *user_data)
398 int wifi_state = 0, restricted = 0;
399 int wifi_off_by_restricted = 0;
401 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
403 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
406 restricted = vconf_keynode_get_bool(node);
408 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted);
410 DBG("network restricted mode %s", restricted > 0 ? "ON" : "OFF");
411 DBG("Wi-Fi state %d, Wi-Fi was off by restricted mode %s", wifi_state,
412 wifi_off_by_restricted ? "Yes" : "No");
414 if (restricted > 0) {
415 /* network restricted on */
416 if (wifi_state == VCONFKEY_WIFI_OFF)
421 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 1);
423 /* network restricted off */
424 if (!wifi_off_by_restricted)
427 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 0);
429 if (wifi_state > VCONFKEY_WIFI_OFF)
437 static void __netconfig_wifi_airplane_mode(keynode_t *node, void *user_data)
439 int wifi_state = 0, airplane_state = 0;
440 int wifi_off_by_airplane = 0;
442 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
444 #if defined TIZEN_WEARABLE
445 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_state);
447 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
451 airplane_state = vconf_keynode_get_bool(node);
453 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
455 DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
456 DBG("Wi-Fi state(or use) %d, Wi-Fi was off by flight mode %s", wifi_state,
457 wifi_off_by_airplane ? "Yes" : "No");
459 if (airplane_mode == airplane_state)
462 airplane_mode = airplane_state;
464 if (airplane_state > 0) {
465 /* airplane mode on */
466 if (wifi_state == VCONFKEY_WIFI_OFF)
471 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
472 #if defined TIZEN_WEARABLE
473 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
476 /* airplane mode off */
477 if (!wifi_off_by_airplane)
480 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
481 #if defined TIZEN_WEARABLE
482 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 1);
484 if (wifi_state > VCONFKEY_WIFI_OFF)
492 static void __emergency_mode_changed_cb(keynode_t *node, void *user_data)
494 int wifi_state = 0, emergency = 0;
495 int wifi_off_by_emergency = 0;
496 #if !defined TIZEN_WEARABLE
497 int emergency_by_fmm = 0;
499 #if defined TIZEN_WEARABLE
503 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
504 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
506 #if !defined TIZEN_WEARABLE
507 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT, &emergency_by_fmm);
508 DBG("emergency mode by Find My Mobile (%d)", emergency_by_fmm);
509 if (emergency_by_fmm == 1)
514 emergency = vconf_keynode_get_int(node);
516 netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &emergency);
518 DBG("emergency mode %s", emergency > SETTING_PSMODE_POWERFUL ? "ON" : "OFF");
519 DBG("Wi-Fi state %d, Wi-Fi was off by emergency mode %s", wifi_state, wifi_off_by_emergency ? "Yes" : "No");
521 #if defined TIZEN_WEARABLE
522 if (emergency == SETTING_PSMODE_WEARABLE) {
523 /* basic power saving mode on */
524 } else if (emergency == SETTING_PSMODE_WEARABLE_ENHANCED) {
525 /* enhanced power saving mode on */
526 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
527 psmode_wifi_use = wifi_use;
529 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
531 if (wifi_state == VCONFKEY_WIFI_OFF)
535 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
537 /* power saving mode off */
538 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, psmode_wifi_use);
539 if (!wifi_off_by_emergency)
542 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
543 if (wifi_state > VCONFKEY_WIFI_OFF)
546 wifi_power_on_wearable(TRUE);
549 if (emergency > SETTING_PSMODE_POWERFUL) {
550 /* emergency mode on */
551 if (wifi_state == VCONFKEY_WIFI_OFF)
556 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
558 /* emergency mode off */
559 if (!wifi_off_by_emergency)
562 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
564 if (wifi_state > VCONFKEY_WIFI_OFF)
573 static void __pm_state_changed_cb(keynode_t* node, void* user_data)
577 static int prev_state = VCONFKEY_PM_STATE_NORMAL;
579 if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
580 ERR("Fail to get VCONFKEY_WIFI_STATE");
585 * VCONFKEY_PM_STATE_NORMAL = 1,
586 * VCONFKEY_PM_STATE_LCDDIM,
587 * VCONFKEY_PM_STATE_LCDOFF,
588 * VCONFKEY_PM_STATE_SLEEP
591 new_state = vconf_keynode_get_int(node);
593 netconfig_vconf_get_int(VCONFKEY_PM_STATE, &new_state);
595 DBG("wifi state: %d (0 off / 1 on / 2 connected)", wifi_state);
596 DBG("Old PM state: %d, current: %d (1 normal / 2 lcddim / 3 lcdoff / 4 sleep)", prev_state, new_state);
598 if ((new_state == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
599 netconfig_wifi_bgscan_stop();
600 netconfig_wifi_bgscan_set_interval(SCAN_EXPONENTIAL_MIN);
601 netconfig_wifi_bgscan_start(TRUE);
604 prev_state = new_state;
607 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
609 int telephony_ready = 0;
612 telephony_ready = vconf_keynode_get_bool(node);
614 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
616 if (telephony_ready != 0) {
617 if (netconfig_tapi_check_sim_state() == FALSE) {
618 DBG("Sim is not initialized yet.");
625 DBG("Turn Wi-Fi on automatically");
627 #if defined TIZEN_WEARABLE
628 wifi_power_on_wearable(TRUE);
634 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
637 int wifi_power_driver_and_supplicant(gboolean enable)
639 /* There are 3 thumb rules for Wi-Fi power management
640 * 1. Do not make exposed API to control wpa_supplicant and driver directly.
641 * It probably breaks ConnMan technology operation.
643 * 2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
644 * It breaks ConnMan technology operation.
646 * 3. Final the best rule: make it as simple as possible.
647 * Simple code enables easy maintenance and reduces logical errors.
649 if (enable == TRUE) {
650 return _load_driver_and_supplicant();
652 if (connman_wifi_technology_state == TRUE)
655 return _remove_driver_and_supplicant();
659 void wifi_power_disable_technology_state_by_only_connman_signal(void)
661 /* Important: it's only done by ConnMan technology signal update */
662 connman_wifi_technology_state = FALSE;
665 void wifi_power_recover_firmware(void)
667 wifi_firmware_recovery_mode = TRUE;
669 netconfig_wifi_bgscan_stop();
674 int wifi_power_on(void)
677 wifi_tech_state_e tech_state;
679 tech_state = wifi_state_get_technology_state();
680 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED) {
681 /* There can be a scenario where wifi is automatically *
682 * activated by connman if wifi was powered in last boot. *
683 * So we should update connman_wifi_technology_state variable *
684 * if it is found that wifi_tech_state variable is *
685 * NETCONFIG_WIFI_TECH_POWERED and connman_wifi_technology_state *
686 * variable is FALSE. Earlier connman_wifi_technology_state *
687 * variable was only updated when wifi was Powered on from *
688 * net-config resulting in variable not getting updated. *
689 * This caused wifi to not get deactivated after reboot if *
690 * last power state was activated */
691 ERR("Net-Config WiFi connman technology state %d",
692 connman_wifi_technology_state);
693 if (connman_wifi_technology_state == FALSE)
694 connman_wifi_technology_state = TRUE;
698 if (__is_wifi_restricted() == TRUE)
701 if (netconfig_is_wifi_tethering_on() == TRUE) {
702 /* TODO: Wi-Fi tethering turns off here */
704 ERR("Failed to turn tethering off");
708 err = wifi_power_driver_and_supplicant(TRUE);
709 if (err < 0 && err != -EALREADY)
712 err = _set_connman_technology_power(TRUE);
717 int wifi_power_off(void)
721 err = _set_connman_technology_power(FALSE);
722 if (err == -EALREADY)
723 wifi_state_update_power_state(FALSE);
728 #if defined TIZEN_WEARABLE
729 int wifi_power_on_wearable(gboolean device_picker_test)
733 wifi_tech_state_e tech_state;
735 tech_state = wifi_state_get_technology_state();
736 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
739 if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
740 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
745 WARN("VCONF_WIFI_WEARABLE_WIFI_USE is OFF");
749 err = wifi_power_driver_and_supplicant(TRUE);
750 if (err < 0 && err != -EALREADY)
753 err = _set_connman_technology_power(TRUE);
755 if (device_picker_test == TRUE)
756 netconfig_wifi_enable_device_picker_test();
762 void wifi_power_initialize(void)
764 int wifi_last_power_state = 0;
766 /* Initialize Airplane mode */
767 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
768 DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
770 /* Update the last Wi-Fi power state */
771 netconfig_vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
772 if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
773 if (TIZEN_TELEPHONY_ENABLE) {
774 int telephony_ready = 0;
775 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
776 if (telephony_ready == 0) {
777 DBG("Telephony API is not initialized yet");
778 vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
779 __netconfig_telephony_ready_changed_cb, NULL);
781 if (netconfig_tapi_check_sim_state() == FALSE)
782 DBG("SIM is not initialized yet");
785 DBG("Turn Wi-Fi on automatically");
786 #if defined TIZEN_WEARABLE
787 wifi_power_on_wearable(TRUE);
793 #if defined TIZEN_WEARABLE
794 vconf_notify_key_changed(VCONF_WIFI_WEARABLE_WIFI_USE, __wearable_wifi_use_changed_cb, NULL);
796 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
797 __netconfig_wifi_airplane_mode, NULL);
799 vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
800 __netconfig_wifi_restrict_mode, NULL);
801 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
802 __netconfig_wifi_airplane_mode, NULL);
805 vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
806 vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
809 void wifi_power_deinitialize(void)
813 gboolean handle_load_driver(Wifi *wifi,
814 GDBusMethodInvocation *context, gboolean device_picker_test)
818 DBG("Wi-Fi power on requested");
820 g_return_val_if_fail(wifi != NULL, TRUE);
822 if (!netconfig_dpm_update_from_wifi()) {
823 DBG("DPM policy restricts Wi-Fi");
824 netconfig_error_permission_denied(context);
828 if (TIZEN_WLAN_BOARD_SPRD)
829 wifi_firmware_download();
831 #if defined TIZEN_WEARABLE
832 err = wifi_power_on_wearable(device_picker_test);
834 err = wifi_power_on();
836 if (device_picker_test == TRUE)
837 netconfig_wifi_enable_device_picker_test();
840 if (err == -EALREADY)
841 netconfig_error_already_exists(context);
842 else if (err == -EPERM)
843 netconfig_error_permission_denied(context);
845 netconfig_error_wifi_driver_failed(context);
851 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
852 __netconfig_set_wifi_bssid();
854 wifi_complete_load_driver(wifi, context);
858 gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
862 DBG("Wi-Fi power off requested");
864 g_return_val_if_fail(wifi != NULL, TRUE);
866 err = wifi_power_off();
868 if (err == -EINPROGRESS)
869 netconfig_error_inprogress(context);
870 else if (err == -EALREADY)
871 netconfig_error_already_exists(context);
872 else if (err == -EPERM)
873 netconfig_error_permission_denied(context);
875 netconfig_error_wifi_driver_failed(context);
879 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
881 wifi_complete_remove_driver(wifi, context);
885 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
889 wifi_complete_load_p2p_driver(wifi, context);
893 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
897 wifi_complete_remove_p2p_driver(wifi, context);
901 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
903 DBG("Generate Random Mac address of ethernet");
907 fp = fopen(OS_RANDOM_FILE, "rb");
910 ERR("Could not open /dev/urandom");
913 rc = fread(mac_buf, 1, mac_len, fp);
917 return rc != mac_len ? -1 : 0;
920 void __netconfig_set_ether_macaddr()
922 DBG("Set wired Mac address ");
923 char *mac_addr = NULL;
924 char rand_addr[WLAN_MAC_ADDR_MAX];
927 mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
928 if (mac_addr == NULL) {
929 DBG("vconf_get_str Failed\n");
932 /* Checking Invalid MAC Address */
933 if ((strlen(mac_addr) == 0)) {
934 ERR("Failed to get valid MAC Address from vconf");
935 /*Generate the Random Mac address*/
936 unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
938 if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE) == -1) {
940 ERR("Could not generate the Random Mac address");
945 rand_mac_add[0] &= 0xFE; /*Clear multicase bit*/
946 rand_mac_add[0] |= 0x02; /*set local assignment bit*/
948 /*Set the Mac address in Vconf*/
949 snprintf(rand_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
950 rand_mac_add[0], rand_mac_add[1],
951 rand_mac_add[2], rand_mac_add[3],
952 rand_mac_add[4], rand_mac_add[5]);
954 netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, rand_addr);
955 } else { /* Valid MAC address */
956 g_strlcpy(rand_addr, mac_addr, WLAN_MAC_ADDR_MAX);
959 DBG("MAC Address of eth0 [%s]", rand_addr);
960 const char *path = NET_EXEC_PATH;
961 char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
962 "ether", rand_addr, "up", NULL};
963 char *const envs[] = { NULL };
964 rv = netconfig_execute_file(path, args, envs);
967 ERR("Unable to execute system command");