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>
24 #include <TapiUtility.h>
26 #include <tzplatform_config.h>
28 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
29 #include <wifi-direct.h>
37 #include "wifi-power.h"
38 #include "wifi-state.h"
39 #include "wifi-tel-intf.h"
40 #include "netsupplicant.h"
41 #include "network-state.h"
42 #include "network-dpm.h"
43 #include "wifi-firmware.h"
44 #include "wifi-background-scan.h"
47 #define WLAN_SUPPLICANT_SCRIPT "/usr/sbin/wpa_supp.sh"
48 #define P2P_SUPPLICANT_SCRIPT "/usr/sbin/p2p_supp.sh"
50 #define VCONF_WIFI_OFF_STATE_BY_AIRPLANE "file/private/wifi/wifi_off_by_airplane"
51 #define VCONF_WIFI_OFF_STATE_BY_RESTRICTED "file/private/wifi/wifi_off_by_restricted"
52 #define VCONF_WIFI_OFF_STATE_BY_EMERGENCY "file/private/wifi/wifi_off_by_emergency"
53 #if defined TIZEN_WEARABLE
54 #define VCONF_WIFI_WEARABLE_WIFI_USE "db/private/wifi/wearable_wifi_use"
56 #if !defined TIZEN_WEARABLE
57 #define VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT "db/setting/network_with_lcd_off_limit"
60 #define WLAN_MAC_INFO tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
61 #define WLAN_MAC_ADDRESS_FILEPATH "/sys/class/net/wlan0/address"
62 #define WLAN_MAC_ADDR_MAX 20
63 #define VCONF_WIFI_BSSID_ADDRESS "db/wifi/bssid_address"
65 #define ETH_MAC_ADDR_SIZE 6
66 #define VCONF_ETH_MAC_ADDRESS "db/dnet/mac_address"
67 #define NET_EXEC_PATH "/sbin/ifconfig"
68 #define OS_RANDOM_FILE "/dev/urandom"
70 static gboolean connman_wifi_technology_state = FALSE;
71 static gboolean wifi_firmware_recovery_mode = FALSE;
72 static int airplane_mode = 0;
74 #if defined TIZEN_WEARABLE
75 static int psmode_wifi_use = 1;
78 static gboolean __is_wifi_restricted(void)
80 #if defined TIZEN_WEARABLE
83 int restricted_mode = 0;
85 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted_mode);
86 if (restricted_mode != 0) {
87 DBG("network restricted mode[%d]", restricted_mode);
94 static void __technology_reply(GObject *source_object, GAsyncResult *res, gpointer user_data)
97 GDBusConnection *conn = NULL;
100 conn = G_DBUS_CONNECTION(source_object);
101 reply = g_dbus_connection_call_finish(conn, res, &error);
105 if (g_strstr_len(error->message, strlen(error->message),
106 CONNMAN_ERROR_INTERFACE ".AlreadyEnabled") != NULL) {
107 wifi_state_update_power_state(TRUE);
108 } else if (g_strstr_len(error->message, strlen(error->message),
109 CONNMAN_ERROR_INTERFACE ".AlreadyDisabled") != NULL) {
110 wifi_state_update_power_state(FALSE);
112 ERR("Fail to request status [%d: %s]", error->code, error->message);
113 wifi_state_update_power_state(FALSE);
117 ERR("Fail to request status");
118 wifi_state_update_power_state(FALSE);
121 DBG("Successfully requested");
124 g_variant_unref(reply);
125 netconfig_gdbus_pending_call_unref();
128 static int __execute_supplicant(gboolean enable)
131 const char *path = WLAN_SUPPLICANT_SCRIPT;
132 char *const args_enable[] = { "/usr/sbin/wpa_supp.sh", "start", NULL };
133 char *const args_disable[] = { "/usr/sbin/wpa_supp.sh", "stop", NULL };
134 char *const envs[] = { NULL };
135 static gboolean enabled = FALSE;
137 if (enabled == enable)
141 rv = netconfig_execute_file(path, args_enable, envs);
143 rv = netconfig_execute_file(path, args_disable, envs);
147 DBG("wpa_supplicant %s", enable == TRUE ? "started" : "stopped");
154 #if defined TIZEN_P2P_ENABLE && defined WLAN_CONCURRENT_MODE
155 static int __netconfig_p2p_supplicant(gboolean enable)
158 const char *path = P2P_SUPPLICANT_SCRIPT;
159 char *const args_enable[] = { P2P_SUPPLICANT_SCRIPT, "start", NULL };
160 char *const args_disable[] = { P2P_SUPPLICANT_SCRIPT, "stop", NULL };
161 char *const envs[] = { NULL };
164 rv = netconfig_execute_file(path, args_enable, envs);
166 rv = netconfig_execute_file(path, args_disable, envs);
170 DBG("p2p_supplicant %s", enable == TRUE ? "started" : "stopped");
176 void netconfig_wifi_recover_firmware(void)
178 wifi_firmware_recovery_mode = TRUE;
180 netconfig_wifi_bgscan_stop();
185 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
186 static void __netconfig_wifi_direct_state_cb(int error_code, wifi_direct_device_state_e device_state, void *user_data)
190 wifi_direct_unset_device_state_changed_cb();
191 wifi_direct_deinitialize();
193 if (device_state == WIFI_DIRECT_DEVICE_STATE_DEACTIVATED) {
194 err = wifi_power_on();
196 if (err == -EALREADY)
197 wifi_state_update_power_state(TRUE);
199 wifi_state_emit_power_failed();
204 static gboolean __netconfig_wifi_direct_power_off(void)
206 DBG("Wi-Fi direct is turning off");
208 if (wifi_direct_initialize() < 0)
211 if (wifi_direct_set_device_state_changed_cb(__netconfig_wifi_direct_state_cb, NULL) < 0)
214 if (wifi_direct_deactivate() < 0)
221 static int _load_driver_and_supplicant(void)
224 wifi_tech_state_e tech_state;
226 tech_state = wifi_state_get_technology_state();
227 if (tech_state > NETCONFIG_WIFI_TECH_OFF)
230 err = __execute_supplicant(TRUE);
231 if (err < 0 && err != -EALREADY)
234 err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, TRUE);
235 if (err < 0 && err != -EALREADY) {
236 __execute_supplicant(FALSE);
240 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
245 static int _remove_driver_and_supplicant(void)
249 if (wifi_firmware_recovery_mode != TRUE &&
250 netconfig_wifi_is_wps_enabled() == TRUE) {
251 DBG("Wi-Fi WPS mode");
255 err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, FALSE);
256 if (err < 0 && err != -EALREADY)
259 err = __execute_supplicant(FALSE);
260 if (err < 0 && err != -EALREADY)
263 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_OFF);
265 if (wifi_firmware_recovery_mode == TRUE) {
266 if (wifi_power_on() < 0)
267 ERR("Failed to recover Wi-Fi firmware");
269 wifi_firmware_recovery_mode = FALSE;
275 static int _set_connman_technology_power(gboolean enable)
277 gboolean reply = FALSE;
278 GVariant *param0 = NULL;
279 GVariant *params = NULL;
280 char key[] = "Powered";
281 gboolean value_enable = TRUE;
282 gboolean value_disable = FALSE;
284 if (connman_wifi_technology_state == enable)
288 param0 = g_variant_new_boolean(value_enable);
290 param0 = g_variant_new_boolean(value_disable);
292 params = g_variant_new("(sv)", key, param0);
294 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
295 CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
296 "SetProperty", params, __technology_reply);
299 ERR("Fail to set technology %s", enable == TRUE ? "enable" : "disable");
303 /* If Wi-Fi powered off,
304 * Do not remove Wi-Fi driver until ConnMan technology state updated
307 connman_wifi_technology_state = enable;
309 /* To be keep safe, early disable Wi-Fi tech state */
311 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
316 static void __netconfig_set_wifi_bssid(void)
319 char bssid[WLAN_MAC_ADDR_MAX];
321 FILE *fp = fopen(WLAN_MAC_INFO, "r");
324 ERR("Fail to open %s", WLAN_MAC_INFO);
325 fp = fopen(WLAN_MAC_ADDRESS_FILEPATH, "r");
327 ERR("Fail to open %s", WLAN_MAC_ADDRESS_FILEPATH);
332 fseek(fp, 0L, SEEK_SET);
333 rv = fscanf(fp, "%17s", bssid);
336 ERR("Fail to read bssid");
338 netconfig_set_vconf_str(VCONF_WIFI_BSSID_ADDRESS, bssid);
343 int netconfig_wifi_driver_and_supplicant(gboolean enable)
345 /* There are 3 thumb rules for Wi-Fi power management
346 * 1. Do not make exposed API to control wpa_supplicant and driver directly.
347 * It probably breaks ConnMan technology operation.
349 * 2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
350 * It breaks ConnMan technology operation.
352 * 3. Final the best rule: make it as simple as possible.
353 * Simple code enables easy maintenance and reduces logical errors.
356 return _load_driver_and_supplicant();
358 if (connman_wifi_technology_state == TRUE)
361 return _load_driver_and_supplicant();
365 void netconfig_wifi_disable_technology_state_by_only_connman_signal(void)
367 /* Important: it's only done by ConnMan technology signal update */
368 connman_wifi_technology_state = FALSE;
371 #if defined TIZEN_WEARABLE
372 int netconfig_wifi_on_wearable(gboolean device_picker_test)
378 if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
379 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
384 if (netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_mode) < 0) {
385 ERR("Fail to get VCONFKEY_SETAPPL_PSMODE");
389 if (ps_mode > SETTING_PSMODE_NORMAL) {
390 WARN("ps mode is on(%d), Not turn on Wi-Fi", ps_mode);
394 WARN("Not permitted Wi-Fi on");
398 err = wifi_power_driver_and_supplicant(TRUE);
399 if (err < 0 && err != -EALREADY)
402 err = _set_connman_technology_power(TRUE);
404 if (device_picker_test == TRUE)
405 netconfig_wifi_enable_device_picker_test();
410 static void __wearable_wifi_use_changed_cb(keynode_t* node, void* user_data)
415 if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
416 ERR("Fail to get VCONFKEY_WIFI_STATE");
421 wifi_use = vconf_keynode_get_int(node);
423 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
427 if (wifi_state > VCONFKEY_WIFI_OFF) {
428 WARN("Wi-Fi is already turned on");
431 wifi_power_on_wearable(TRUE);
433 ERR("## wifi use [OFF]");
434 if (wifi_state == VCONFKEY_WIFI_OFF) {
435 WARN("Wi-Fi is already turned off");
443 static void __netconfig_wifi_restrict_mode(keynode_t *node, void *user_data)
445 int wifi_state = 0, restricted = 0;
446 int wifi_off_by_restricted = 0;
448 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
450 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
453 restricted = vconf_keynode_get_bool(node);
455 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted);
457 DBG("network restricted mode %s", restricted > 0 ? "ON" : "OFF");
458 DBG("Wi-Fi state %d, Wi-Fi was off by restricted mode %s", wifi_state,
459 wifi_off_by_restricted ? "Yes" : "No");
461 if (restricted > 0) {
462 /* network restricted on */
463 if (wifi_state == VCONFKEY_WIFI_OFF)
468 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 1);
470 /* network restricted off */
471 if (!wifi_off_by_restricted)
474 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 0);
476 if (wifi_state > VCONFKEY_WIFI_OFF)
484 static void __netconfig_wifi_airplane_mode(keynode_t *node, void *user_data)
486 int wifi_state = 0, airplane_state = 0;
487 int wifi_off_by_airplane = 0;
489 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
491 #if defined TIZEN_WEARABLE
492 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_state)
494 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
498 airplane_state = vconf_keynode_get_bool(node);
500 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
502 DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
503 DBG("Wi-Fi state(or use) %d, Wi-Fi was off by flight mode %s", wifi_state,
504 wifi_off_by_airplane ? "Yes" : "No");
506 if (airplane_mode == airplane_state)
509 airplane_mode = airplane_state;
511 if (airplane_state > 0) {
512 /* airplane mode on */
513 if (wifi_state == VCONFKEY_WIFI_OFF)
518 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
519 #if defined TIZEN_WEARABLE
520 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
523 /* airplane mode off */
524 if (!wifi_off_by_airplane)
527 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
528 #if defined TIZEN_WEARABLE
529 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 1);
531 if (wifi_state > VCONFKEY_WIFI_OFF)
539 static void __emergency_mode_changed_cb(keynode_t *node, void *user_data)
541 int wifi_state = 0, emergency = 0;
542 int wifi_off_by_emergency = 0;
543 #if !defined TIZEN_WEARABLE
544 int emergency_by_fmm = 0;
546 #if defined TIZEN_WEARABLE
550 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
551 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
553 #if !defined TIZEN_WEARABLE
554 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT, &emergency_by_fmm);
555 DBG("emergency mode by Find My Mobile (%d)", emergency_by_fmm);
556 if (emergency_by_fmm == 1)
561 emergency = vconf_keynode_get_int(node);
563 netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &emergency);
565 DBG("emergency mode %s", emergency > SETTING_PSMODE_POWERFUL ? "ON" : "OFF");
566 DBG("Wi-Fi state %d, Wi-Fi was off by emergency mode %s", wifi_state, wifi_off_by_emergency ? "Yes" : "No");
568 #if defined TIZEN_WEARABLE
569 if (emergency == SETTING_PSMODE_WEARABLE) {
570 /* basic power saving mode on */
571 } else if (emergency == SETTING_PSMODE_WEARABLE_ENHANCED) {
572 /* enhanced power saving mode on */
573 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
574 psmode_wifi_use = wifi_use;
576 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
578 if (wifi_state == VCONFKEY_WIFI_OFF)
582 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
584 /* power saving mode off */
585 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, psmode_wifi_use);
586 if (!wifi_off_by_emergency)
589 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
590 if (wifi_state > VCONFKEY_WIFI_OFF)
593 wifi_power_on_wearable(TRUE);
596 if (emergency > SETTING_PSMODE_POWERFUL) {
597 /* emergency mode on */
598 if (wifi_state == VCONFKEY_WIFI_OFF)
603 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
605 /* emergency mode off */
606 if (!wifi_off_by_emergency)
609 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
611 if (wifi_state > VCONFKEY_WIFI_OFF)
620 static void __pm_state_changed_cb(keynode_t* node, void* user_data)
624 static int prev_state = VCONFKEY_PM_STATE_NORMAL;
626 if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
627 ERR("Fail to get VCONFKEY_WIFI_STATE");
632 * VCONFKEY_PM_STATE_NORMAL = 1,
633 * VCONFKEY_PM_STATE_LCDDIM,
634 * VCONFKEY_PM_STATE_LCDOFF,
635 * VCONFKEY_PM_STATE_SLEEP
638 new_state = vconf_keynode_get_int(node);
640 netconfig_vconf_get_int(VCONFKEY_PM_STATE, &new_state);
642 DBG("wifi state: %d (0 off / 1 on / 2 connected)", wifi_state);
643 DBG("Old PM state: %d, current: %d", prev_state, new_state);
645 if ((new_state == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
646 netconfig_wifi_bgscan_stop();
647 netconfig_wifi_bgscan_start(TRUE);
650 prev_state = new_state;
653 static void _tapi_noti_sim_status_cb(TapiHandle *handle, const char *noti_id,
654 void *data, void *user_data)
656 TelSimCardStatus_t *status = data;
658 if (*status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
659 DBG("Turn Wi-Fi on automatically");
660 #if defined TIZEN_WEARABLE
661 wifi_power_on_wearable(TRUE);
665 netconfig_tel_deinit();
669 static gboolean netconfig_tapi_check_sim_state(void)
671 int ret, card_changed;
672 TelSimCardStatus_t status = TAPI_SIM_STATUS_UNKNOWN;
673 TapiHandle *tapi_handle = NULL;
675 tapi_handle = (TapiHandle *)netconfig_tel_init();
676 if (tapi_handle == NULL) {
677 ERR("Failed to tapi init");
681 ret = tel_get_sim_init_info(tapi_handle, &status, &card_changed);
682 if (ret != TAPI_API_SUCCESS) {
683 ERR("tel_get_sim_init_info() Failed : [%d]", ret);
684 netconfig_tel_deinit();
689 case TAPI_SIM_STATUS_UNKNOWN:
690 case TAPI_SIM_STATUS_CARD_ERROR:
691 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
692 case TAPI_SIM_STATUS_CARD_BLOCKED:
693 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
695 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
696 case TAPI_SIM_STATUS_SIM_INITIALIZING:
697 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
698 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
699 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
700 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
701 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
702 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
703 tel_register_noti_event(tapi_handle, TAPI_NOTI_SIM_STATUS,
704 _tapi_noti_sim_status_cb, NULL);
707 ERR("not defined status(%d)", status);
711 netconfig_tel_deinit();
716 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
718 int telephony_ready = 0;
721 telephony_ready = vconf_keynode_get_bool(node);
723 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
725 if (telephony_ready != 0) {
726 if (netconfig_tapi_check_sim_state() == FALSE) {
727 DBG("Sim is not initialized yet.");
734 DBG("Turn Wi-Fi on automatically");
736 #if defined TIZEN_WEARABLE
737 wifi_power_on_wearable(TRUE);
743 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
746 int wifi_power_driver_and_supplicant(gboolean enable)
748 /* There are 3 thumb rules for Wi-Fi power management
749 * 1. Do not make exposed API to control wpa_supplicant and driver directly.
750 * It probably breaks ConnMan technology operation.
752 * 2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
753 * It breaks ConnMan technology operation.
755 * 3. Final the best rule: make it as simple as possible.
756 * Simple code enables easy maintenance and reduces logical errors.
758 if (enable == TRUE) {
759 return _load_driver_and_supplicant();
761 if (connman_wifi_technology_state == TRUE)
764 return _remove_driver_and_supplicant();
768 void wifi_power_disable_technology_state_by_only_connman_signal(void)
770 /* Important: it's only done by ConnMan technology signal update */
771 connman_wifi_technology_state = FALSE;
774 void wifi_power_recover_firmware(void)
776 wifi_firmware_recovery_mode = TRUE;
778 netconfig_wifi_bgscan_stop();
783 int wifi_power_on(void)
786 wifi_tech_state_e tech_state;
788 tech_state = wifi_state_get_technology_state();
789 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
792 if (__is_wifi_restricted() == TRUE)
795 if (netconfig_is_wifi_tethering_on() == TRUE) {
796 /* TODO: Wi-Fi tethering turns off here */
798 ERR("Failed to turn tethering off");
802 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
803 if (netconfig_is_wifi_direct_on() == TRUE) {
804 if (__netconfig_wifi_direct_power_off() == TRUE)
807 ERR("Failed to turn Wi-Fi direct off");
813 err = wifi_power_driver_and_supplicant(TRUE);
814 if (err < 0 && err != -EALREADY)
817 err = _set_connman_technology_power(TRUE);
822 int wifi_power_off(void)
826 err = _set_connman_technology_power(FALSE);
827 if (err == -EALREADY)
828 wifi_state_update_power_state(FALSE);
833 #if defined TIZEN_WEARABLE
834 int wifi_power_on_wearable(gboolean device_picker_test)
838 wifi_tech_state_e tech_state;
840 tech_state = wifi_state_get_technology_state();
841 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
844 if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
845 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
850 WARN("VCONF_WIFI_WEARABLE_WIFI_USE is OFF");
854 err = wifi_power_driver_and_supplicant(TRUE);
855 if (err < 0 && err != -EALREADY)
858 err = _set_connman_technology_power(TRUE);
860 if (device_picker_test == TRUE)
861 netconfig_wifi_enable_device_picker_test();
867 void wifi_power_initialize(void)
869 int wifi_last_power_state = 0;
871 /* Initialize Airplane mode */
872 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
873 DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
875 /* Update the last Wi-Fi power state */
876 netconfig_vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
877 if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
878 if (TIZEN_TELEPHONY_ENABLE) {
879 int telephony_ready = 0;
880 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
881 if (telephony_ready == 0) {
882 DBG("Telephony API is not initialized yet");
883 vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
884 __netconfig_telephony_ready_changed_cb, NULL);
886 if (netconfig_tapi_check_sim_state() == FALSE)
887 DBG("SIM is not initialized yet");
890 DBG("Turn Wi-Fi on automatically");
891 #if defined TIZEN_WEARABLE
892 wifi_power_on_wearable(TRUE);
898 #if defined TIZEN_WEARABLE
899 vconf_notify_key_changed(VCONF_WIFI_WEARABLE_WIFI_USE, __wearable_wifi_use_changed_cb, NULL);
901 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
902 __netconfig_wifi_airplane_mode, NULL);
904 vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
905 __netconfig_wifi_restrict_mode, NULL);
906 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
907 __netconfig_wifi_airplane_mode, NULL);
910 vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
911 vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
914 void wifi_power_deinitialize(void)
918 gboolean handle_load_driver(Wifi *wifi,
919 GDBusMethodInvocation *context, gboolean device_picker_test)
923 DBG("Wi-Fi power on requested");
925 g_return_val_if_fail(wifi != NULL, FALSE);
927 if (!netconfig_dpm_update_from_wifi()) {
928 DBG("DPM policy restricts Wi-Fi");
929 netconfig_error_permission_denied(context);
933 if (TIZEN_WLAN_BOARD_SPRD)
934 wifi_firmware_download();
936 #if defined TIZEN_WEARABLE
937 err = wifi_power_on_wearable(device_picker_test);
939 err = wifi_power_on();
941 if (device_picker_test == TRUE)
942 netconfig_wifi_enable_device_picker_test();
945 if (err == -EINPROGRESS)
946 netconfig_error_inprogress(context);
947 else if (err == -EALREADY)
948 netconfig_error_already_exists(context);
949 else if (err == -EPERM)
950 netconfig_error_permission_denied(context);
952 netconfig_error_wifi_driver_failed(context);
958 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
959 __netconfig_set_wifi_bssid();
961 wifi_complete_load_driver(wifi, context);
965 gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
969 DBG("Wi-Fi power off requested");
971 g_return_val_if_fail(wifi != NULL, FALSE);
973 err = wifi_power_off();
975 if (err == -EINPROGRESS)
976 netconfig_error_inprogress(context);
977 else if (err == -EALREADY)
978 netconfig_error_already_exists(context);
979 else if (err == -EPERM)
980 netconfig_error_permission_denied(context);
982 netconfig_error_wifi_driver_failed(context);
986 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
988 wifi_complete_remove_driver(wifi, context);
992 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
996 wifi_complete_load_p2p_driver(wifi, context);
1000 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
1004 wifi_complete_remove_p2p_driver(wifi, context);
1008 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
1010 DBG("Generate Random Mac address of ethernet");
1014 fp = fopen(OS_RANDOM_FILE, "rb");
1017 ERR("Could not open /dev/urandom");
1020 rc = fread(mac_buf, 1, mac_len, fp);
1024 return rc != mac_len ? -1 : 0;
1027 void __netconfig_set_ether_macaddr()
1029 DBG("Set wired Mac address ");
1030 char *mac_addr = NULL;
1031 char rand_addr[WLAN_MAC_ADDR_MAX];
1034 mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
1035 if (mac_addr == NULL) {
1036 DBG("vconf_get_str Failed\n");
1039 /* Checking Invalid MAC Address */
1040 if ((strlen(mac_addr) == 0)) {
1041 ERR("Failed to get valid MAC Address from vconf");
1042 /*Generate the Random Mac address*/
1043 unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
1045 if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE) == -1) {
1047 ERR("Could not generate the Random Mac address");
1052 rand_mac_add[0] &= 0xFE; /*Clear multicase bit*/
1053 rand_mac_add[0] |= 0x02; /*set local assignment bit*/
1055 /*Set the Mac address in Vconf*/
1056 snprintf(rand_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
1057 rand_mac_add[0], rand_mac_add[1],
1058 rand_mac_add[2], rand_mac_add[3],
1059 rand_mac_add[4], rand_mac_add[5]);
1061 netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, rand_addr);
1062 } else { /* Valid MAC address */
1063 strncpy(rand_addr, mac_addr, strlen(mac_addr));
1064 rand_addr[strlen(mac_addr)] = '\0';
1067 DBG("MAC Address of eth0 [%s]", rand_addr);
1068 const char *path = NET_EXEC_PATH;
1069 char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
1070 "ether", rand_addr, "up", NULL};
1071 char *const envs[] = { NULL };
1072 rv = netconfig_execute_file(path, args, envs);
1075 ERR("Unable to execute system command");