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 *param0 = NULL;
225 GVariant *params = NULL;
226 char key[] = "Powered";
228 if (wifi_state_is_technology_available() == FALSE) {
234 param0 = g_variant_new_boolean(value_enable);
235 params = g_variant_new("(sv)", key, param0);
237 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
238 CONNMAN_WIFI_TECHNOLOGY_PREFIX,
239 CONNMAN_TECHNOLOGY_INTERFACE,
240 "SetProperty", params, __technology_reply);
243 ERR("Fail to set technology enable");
244 wifi_state_update_power_state(FALSE);
246 retry_count = NETCONFIG_TECH_WAITING_COUNT;
250 retry_count = NETCONFIG_TECH_WAITING_COUNT;
254 static int _set_connman_technology_power(gboolean enable)
256 gboolean reply = FALSE;
257 GVariant *param0 = NULL;
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;
274 param0 = g_variant_new_boolean(value_enable);
276 param0 = g_variant_new_boolean(value_disable);
278 params = g_variant_new("(sv)", key, param0);
280 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
281 CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
282 "SetProperty", params, __technology_reply);
285 ERR("Fail to set technology %s", enable == TRUE ? "enable" : "disable");
289 /* If Wi-Fi powered off,
290 * Do not remove Wi-Fi driver until ConnMan technology state updated
293 connman_wifi_technology_state = enable;
295 /* To be keep safe, early disable Wi-Fi tech state */
297 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
302 static void __netconfig_set_wifi_bssid(void)
305 char bssid[WLAN_MAC_ADDR_MAX];
307 FILE *fp = fopen(WLAN_MAC_ADDRESS_FILEPATH, "r");
310 ERR("Fail to open %s", WLAN_MAC_ADDRESS_FILEPATH);
314 fseek(fp, 0L, SEEK_SET);
315 rv = fscanf(fp, "%17s", bssid);
318 ERR("Fail to read bssid");
320 netconfig_set_vconf_str(VCONF_WIFI_BSSID_ADDRESS, bssid);
325 void netconfig_wifi_disable_technology_state_by_only_connman_signal(void)
327 /* Important: it's only done by ConnMan technology signal update */
328 connman_wifi_technology_state = FALSE;
331 #if defined TIZEN_WEARABLE
332 int netconfig_wifi_on_wearable(gboolean device_picker_test)
338 if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
339 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
344 if (netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_mode) < 0) {
345 ERR("Fail to get VCONFKEY_SETAPPL_PSMODE");
349 if (ps_mode > SETTING_PSMODE_NORMAL) {
350 WARN("ps mode is on(%d), Not turn on Wi-Fi", ps_mode);
354 WARN("Not permitted Wi-Fi on");
358 err = wifi_power_driver_and_supplicant(TRUE);
359 if (err < 0 && err != -EALREADY)
362 err = _set_connman_technology_power(TRUE);
364 if (device_picker_test == TRUE)
365 netconfig_wifi_enable_device_picker_test();
370 static void __wearable_wifi_use_changed_cb(keynode_t* node, void* user_data)
375 if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
376 ERR("Fail to get VCONFKEY_WIFI_STATE");
381 wifi_use = vconf_keynode_get_int(node);
383 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
387 if (wifi_state > VCONFKEY_WIFI_OFF) {
388 WARN("Wi-Fi is already turned on");
391 wifi_power_on_wearable(TRUE);
393 ERR("## wifi use [OFF]");
394 if (wifi_state == VCONFKEY_WIFI_OFF) {
395 WARN("Wi-Fi is already turned off");
403 static void __netconfig_wifi_restrict_mode(keynode_t *node, void *user_data)
405 int wifi_state = 0, restricted = 0;
406 int wifi_off_by_restricted = 0;
408 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
410 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
413 restricted = vconf_keynode_get_bool(node);
415 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted);
417 DBG("network restricted mode %s", restricted > 0 ? "ON" : "OFF");
418 DBG("Wi-Fi state %d, Wi-Fi was off by restricted mode %s", wifi_state,
419 wifi_off_by_restricted ? "Yes" : "No");
421 if (restricted > 0) {
422 /* network restricted on */
423 if (wifi_state == VCONFKEY_WIFI_OFF)
428 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 1);
430 /* network restricted off */
431 if (!wifi_off_by_restricted)
434 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 0);
436 if (wifi_state > VCONFKEY_WIFI_OFF)
444 static void __netconfig_wifi_airplane_mode(keynode_t *node, void *user_data)
446 int wifi_state = 0, airplane_state = 0;
447 int wifi_off_by_airplane = 0;
449 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
451 #if defined TIZEN_WEARABLE
452 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_state);
454 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
458 airplane_state = vconf_keynode_get_bool(node);
460 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
462 DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
463 DBG("Wi-Fi state(or use) %d, Wi-Fi was off by flight mode %s", wifi_state,
464 wifi_off_by_airplane ? "Yes" : "No");
466 if (airplane_mode == airplane_state)
469 airplane_mode = airplane_state;
471 if (airplane_state > 0) {
472 /* airplane mode on */
473 if (wifi_state == VCONFKEY_WIFI_OFF)
478 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
479 #if defined TIZEN_WEARABLE
480 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
483 /* airplane mode off */
484 if (!wifi_off_by_airplane)
487 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
488 #if defined TIZEN_WEARABLE
489 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 1);
491 if (wifi_state > VCONFKEY_WIFI_OFF)
499 static void __emergency_mode_changed_cb(keynode_t *node, void *user_data)
501 int wifi_state = 0, emergency = 0;
502 int wifi_off_by_emergency = 0;
503 #if !defined TIZEN_WEARABLE
504 int emergency_by_fmm = 0;
506 #if defined TIZEN_WEARABLE
510 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
511 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
513 #if !defined TIZEN_WEARABLE
514 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT, &emergency_by_fmm);
515 DBG("emergency mode by Find My Mobile (%d)", emergency_by_fmm);
516 if (emergency_by_fmm == 1)
521 emergency = vconf_keynode_get_int(node);
523 netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &emergency);
525 DBG("emergency mode %s", emergency > SETTING_PSMODE_POWERFUL ? "ON" : "OFF");
526 DBG("Wi-Fi state %d, Wi-Fi was off by emergency mode %s", wifi_state, wifi_off_by_emergency ? "Yes" : "No");
528 #if defined TIZEN_WEARABLE
529 if (emergency == SETTING_PSMODE_WEARABLE) {
530 /* basic power saving mode on */
531 } else if (emergency == SETTING_PSMODE_WEARABLE_ENHANCED) {
532 /* enhanced power saving mode on */
533 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
534 psmode_wifi_use = wifi_use;
536 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
538 if (wifi_state == VCONFKEY_WIFI_OFF)
542 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
544 /* power saving mode off */
545 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, psmode_wifi_use);
546 if (!wifi_off_by_emergency)
549 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
550 if (wifi_state > VCONFKEY_WIFI_OFF)
553 wifi_power_on_wearable(TRUE);
556 if (emergency > SETTING_PSMODE_POWERFUL) {
557 /* emergency mode on */
558 if (wifi_state == VCONFKEY_WIFI_OFF)
563 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
565 /* emergency mode off */
566 if (!wifi_off_by_emergency)
569 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
571 if (wifi_state > VCONFKEY_WIFI_OFF)
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 netconfig_wifi_bgscan_stop();
607 netconfig_wifi_bgscan_set_interval(SCAN_EXPONENTIAL_MIN);
608 netconfig_wifi_bgscan_start(TRUE);
611 prev_state = new_state;
614 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
616 int telephony_ready = 0;
619 telephony_ready = vconf_keynode_get_bool(node);
621 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
623 if (telephony_ready != 0) {
624 if (netconfig_tapi_check_sim_state() == FALSE) {
625 DBG("Sim is not initialized yet.");
632 DBG("Turn Wi-Fi on automatically");
634 #if defined TIZEN_WEARABLE
635 wifi_power_on_wearable(TRUE);
641 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
644 int wifi_power_driver_and_supplicant(gboolean enable)
646 /* There are 3 thumb rules for Wi-Fi power management
647 * 1. Do not make exposed API to control wpa_supplicant and driver directly.
648 * It probably breaks ConnMan technology operation.
650 * 2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
651 * It breaks ConnMan technology operation.
653 * 3. Final the best rule: make it as simple as possible.
654 * Simple code enables easy maintenance and reduces logical errors.
656 if (enable == TRUE) {
657 return _load_driver_and_supplicant();
659 if (connman_wifi_technology_state == TRUE)
662 return _remove_driver_and_supplicant();
666 void wifi_power_disable_technology_state_by_only_connman_signal(void)
668 /* Important: it's only done by ConnMan technology signal update */
669 connman_wifi_technology_state = FALSE;
672 void wifi_power_recover_firmware(void)
674 wifi_firmware_recovery_mode = TRUE;
676 netconfig_wifi_bgscan_stop();
681 int wifi_power_on(void)
684 wifi_tech_state_e tech_state;
686 tech_state = wifi_state_get_technology_state();
687 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED) {
688 /* There can be a scenario where wifi is automatically *
689 * activated by connman if wifi was powered in last boot. *
690 * So we should update connman_wifi_technology_state variable *
691 * if it is found that wifi_tech_state variable is *
692 * NETCONFIG_WIFI_TECH_POWERED and connman_wifi_technology_state *
693 * variable is FALSE. Earlier connman_wifi_technology_state *
694 * variable was only updated when wifi was Powered on from *
695 * net-config resulting in variable not getting updated. *
696 * This caused wifi to not get deactivated after reboot if *
697 * last power state was activated */
698 ERR("Net-Config WiFi connman technology state %d",
699 connman_wifi_technology_state);
700 if (connman_wifi_technology_state == FALSE)
701 connman_wifi_technology_state = TRUE;
705 if (__is_wifi_restricted() == TRUE)
708 if (netconfig_is_wifi_tethering_on() == TRUE) {
709 /* TODO: Wi-Fi tethering turns off here */
711 ERR("Failed to turn tethering off");
715 err = wifi_power_driver_and_supplicant(TRUE);
716 if (err < 0 && err != -EALREADY)
719 err = _set_connman_technology_power(TRUE);
724 int wifi_power_off(void)
728 err = _set_connman_technology_power(FALSE);
729 if (err == -EALREADY)
730 wifi_state_update_power_state(FALSE);
735 #if defined TIZEN_WEARABLE
736 int wifi_power_on_wearable(gboolean device_picker_test)
740 wifi_tech_state_e tech_state;
742 tech_state = wifi_state_get_technology_state();
743 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
746 if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
747 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
752 WARN("VCONF_WIFI_WEARABLE_WIFI_USE is OFF");
756 err = wifi_power_driver_and_supplicant(TRUE);
757 if (err < 0 && err != -EALREADY)
760 err = _set_connman_technology_power(TRUE);
762 if (device_picker_test == TRUE)
763 netconfig_wifi_enable_device_picker_test();
769 void wifi_power_initialize(void)
771 int wifi_last_power_state = 0;
773 /* Initialize Airplane mode */
774 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
775 DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
777 /* Update the last Wi-Fi power state */
778 netconfig_vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
779 if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
780 if (TIZEN_TELEPHONY_ENABLE) {
781 int telephony_ready = 0;
782 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
783 if (telephony_ready == 0) {
784 DBG("Telephony API is not initialized yet");
785 vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
786 __netconfig_telephony_ready_changed_cb, NULL);
788 if (netconfig_tapi_check_sim_state() == FALSE)
789 DBG("SIM is not initialized yet");
792 DBG("Turn Wi-Fi on automatically");
793 #if defined TIZEN_WEARABLE
794 wifi_power_on_wearable(TRUE);
800 #if defined TIZEN_WEARABLE
801 vconf_notify_key_changed(VCONF_WIFI_WEARABLE_WIFI_USE, __wearable_wifi_use_changed_cb, NULL);
803 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
804 __netconfig_wifi_airplane_mode, NULL);
806 vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
807 __netconfig_wifi_restrict_mode, NULL);
808 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
809 __netconfig_wifi_airplane_mode, NULL);
812 vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
813 vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
816 void wifi_power_deinitialize(void)
820 gboolean handle_load_driver(Wifi *wifi,
821 GDBusMethodInvocation *context, gboolean device_picker_test)
825 DBG("Wi-Fi power on requested");
827 g_return_val_if_fail(wifi != NULL, FALSE);
829 if (!netconfig_dpm_update_from_wifi()) {
830 DBG("DPM policy restricts Wi-Fi");
831 netconfig_error_permission_denied(context);
835 if (TIZEN_WLAN_BOARD_SPRD)
836 wifi_firmware_download();
838 #if defined TIZEN_WEARABLE
839 err = wifi_power_on_wearable(device_picker_test);
841 err = wifi_power_on();
843 if (device_picker_test == TRUE)
844 netconfig_wifi_enable_device_picker_test();
847 if (err == -EALREADY)
848 netconfig_error_already_exists(context);
849 else if (err == -EPERM)
850 netconfig_error_permission_denied(context);
852 netconfig_error_wifi_driver_failed(context);
858 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
859 __netconfig_set_wifi_bssid();
861 wifi_complete_load_driver(wifi, context);
865 gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
869 DBG("Wi-Fi power off requested");
871 g_return_val_if_fail(wifi != NULL, FALSE);
873 err = wifi_power_off();
875 if (err == -EINPROGRESS)
876 netconfig_error_inprogress(context);
877 else if (err == -EALREADY)
878 netconfig_error_already_exists(context);
879 else if (err == -EPERM)
880 netconfig_error_permission_denied(context);
882 netconfig_error_wifi_driver_failed(context);
886 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
888 wifi_complete_remove_driver(wifi, context);
892 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
896 wifi_complete_load_p2p_driver(wifi, context);
900 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
904 wifi_complete_remove_p2p_driver(wifi, context);
908 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
910 DBG("Generate Random Mac address of ethernet");
914 fp = fopen(OS_RANDOM_FILE, "rb");
917 ERR("Could not open /dev/urandom");
920 rc = fread(mac_buf, 1, mac_len, fp);
924 return rc != mac_len ? -1 : 0;
927 void __netconfig_set_ether_macaddr()
929 DBG("Set wired Mac address ");
930 char *mac_addr = NULL;
931 char rand_addr[WLAN_MAC_ADDR_MAX];
934 mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
935 if (mac_addr == NULL) {
936 DBG("vconf_get_str Failed\n");
939 /* Checking Invalid MAC Address */
940 if ((strlen(mac_addr) == 0)) {
941 ERR("Failed to get valid MAC Address from vconf");
942 /*Generate the Random Mac address*/
943 unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
945 if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE) == -1) {
947 ERR("Could not generate the Random Mac address");
952 rand_mac_add[0] &= 0xFE; /*Clear multicase bit*/
953 rand_mac_add[0] |= 0x02; /*set local assignment bit*/
955 /*Set the Mac address in Vconf*/
956 snprintf(rand_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
957 rand_mac_add[0], rand_mac_add[1],
958 rand_mac_add[2], rand_mac_add[3],
959 rand_mac_add[4], rand_mac_add[5]);
961 netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, rand_addr);
962 } else { /* Valid MAC address */
963 g_strlcpy(rand_addr, mac_addr, WLAN_MAC_ADDR_MAX);
966 DBG("MAC Address of eth0 [%s]", rand_addr);
967 const char *path = NET_EXEC_PATH;
968 char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
969 "ether", rand_addr, "up", NULL};
970 char *const envs[] = { NULL };
971 rv = netconfig_execute_file(path, args, envs);
974 ERR("Unable to execute system command");