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>
27 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
28 #include <wifi-direct.h>
36 #include "wifi-power.h"
37 #include "wifi-state.h"
38 #include "wifi-tel-intf.h"
39 #include "netsupplicant.h"
40 #include "network-state.h"
41 #include "wifi-firmware.h"
42 #include "wifi-background-scan.h"
45 #define WLAN_SUPPLICANT_SCRIPT "/usr/sbin/wpa_supp.sh"
46 #define P2P_SUPPLICANT_SCRIPT "/usr/sbin/p2p_supp.sh"
48 #if defined TIZEN_WEARABLE
50 static weconn_h weconn_handle = NULL;
53 #define VCONF_WIFI_OFF_STATE_BY_AIRPLANE "file/private/wifi/wifi_off_by_airplane"
54 #define VCONF_WIFI_OFF_STATE_BY_RESTRICTED "file/private/wifi/wifi_off_by_restricted"
55 #define VCONF_WIFI_OFF_STATE_BY_EMERGENCY "file/private/wifi/wifi_off_by_emergency"
56 #if defined TIZEN_WEARABLE
57 #define VCONF_WIFI_WEARABLE_WIFI_USE "db/private/wifi/wearable_wifi_use"
59 #if !defined TIZEN_WEARABLE
60 #define VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT "db/setting/network_with_lcd_off_limit"
63 #define WLAN_MAC_INFO "/opt/etc/.mac.info"
64 #define WLAN_MAC_ADDR_MAX 20
65 #define VCONF_WIFI_BSSID_ADDRESS "db/wifi/bssid_address"
68 #define ETH_MAC_ADDR_SIZE 6
69 #define VCONF_ETH_MAC_ADDRESS "db/dnet/mac_address"
70 #define NET_EXEC_PATH "/sbin/ifconfig"
71 #define OS_RANDOM_FILE "/dev/urandom"
74 static gboolean connman_wifi_technology_state = FALSE;
75 static gboolean wifi_firmware_recovery_mode = FALSE;
76 static int airplane_mode = 0;
78 #if defined TIZEN_WEARABLE
79 static int psmode_wifi_use = 1;
82 static gboolean __is_wifi_restricted(void)
84 #if defined TIZEN_WEARABLE
87 int restricted_mode = 0;
89 vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted_mode);
90 if (restricted_mode != 0) {
91 DBG("network restricted mode[%d]", restricted_mode);
98 static void __technology_reply(GObject *source_object, GAsyncResult *res, gpointer user_data)
101 GDBusConnection *conn = NULL;
102 GError *error = NULL;
104 conn = G_DBUS_CONNECTION (source_object);
105 reply = g_dbus_connection_call_finish(conn, res, &error);
109 if (g_strcmp0(error->message, CONNMAN_ERROR_INTERFACE ".AlreadyEnabled") == 0) {
110 wifi_state_update_power_state(TRUE);
111 } else if (g_strcmp0(error->message, CONNMAN_ERROR_INTERFACE ".AlreadyDisabled") == 0) {
112 wifi_state_update_power_state(FALSE);
114 ERR("Fail to request status [%d: %s]", error->code, error->message);
118 ERR("Fail torequest status");
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 static int _start_supplicant(void)
156 GVariant *reply = NULL;
157 GVariant *params = NULL;
159 params = g_variant_new("(ss)","wpasupplicant.service", "replace");
161 reply = netconfig_invoke_dbus_method("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnit", params);
163 ERR("Fail to _start_supplicant");
166 g_variant_unref(reply);
172 static int _stop_supplicant(void)
174 GVariant *reply = NULL;
176 reply = netconfig_invoke_dbus_method("fi.w1.wpa_supplicant1", "/fi/w1/wpa_supplicant1", "fi.w1.wpa_supplicant1", "Terminate", NULL);
178 ERR("Fail to _stop_supplicant");
181 g_variant_unref(reply);
187 #if defined TIZEN_P2P_ENABLE && defined WLAN_CONCURRENT_MODE
188 static int __netconfig_p2p_supplicant(gboolean enable)
191 const char *path = P2P_SUPPLICANT_SCRIPT;
192 char *const args_enable[] = { P2P_SUPPLICANT_SCRIPT, "start", NULL };
193 char *const args_disable[] = { P2P_SUPPLICANT_SCRIPT, "stop", NULL };
194 char *const envs[] = { NULL };
197 rv = netconfig_execute_file(path, args_enable, envs);
199 rv = netconfig_execute_file(path, args_disable, envs);
203 DBG("p2p_supplicant %s", enable == TRUE ? "started" : "stopped");
209 void netconfig_wifi_recover_firmware(void)
211 wifi_firmware_recovery_mode = TRUE;
213 netconfig_wifi_bgscan_stop();
218 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
219 static void __netconfig_wifi_direct_state_cb(int error_code, wifi_direct_device_state_e device_state, void *user_data)
223 wifi_direct_unset_device_state_changed_cb();
224 wifi_direct_deinitialize();
226 if (device_state == WIFI_DIRECT_DEVICE_STATE_DEACTIVATED) {
227 err = wifi_power_on();
229 if (err == -EALREADY)
230 wifi_state_update_power_state(TRUE);
232 wifi_state_emit_power_failed();
237 static gboolean __netconfig_wifi_direct_power_off(void)
239 DBG("Wi-Fi direct is turning off");
241 if (wifi_direct_initialize() < 0)
244 if (wifi_direct_set_device_state_changed_cb(__netconfig_wifi_direct_state_cb, NULL) < 0)
247 if (wifi_direct_deactivate() < 0)
254 static int _load_driver_and_supplicant(void)
257 wifi_tech_state_e tech_state;
259 tech_state = wifi_state_get_technology_state();
260 if (tech_state > NETCONFIG_WIFI_TECH_OFF)
263 err = __execute_supplicant(TRUE);
264 if (err < 0 && err != -EALREADY)
267 err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, TRUE);
268 if (err < 0 && err != -EALREADY) {
269 __execute_supplicant(FALSE);
273 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
278 static int _remove_driver_and_supplicant(void)
282 if (wifi_firmware_recovery_mode != TRUE &&
283 netconfig_wifi_is_wps_enabled() == TRUE) {
284 DBG("Wi-Fi WPS mode");
288 err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, FALSE);
289 if (err < 0 && err != -EALREADY)
292 err = __execute_supplicant(FALSE);
293 if (err < 0 && err != -EALREADY)
296 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_OFF);
298 if (wifi_firmware_recovery_mode == TRUE) {
299 if (wifi_power_on() < 0)
300 ERR("Failed to recover Wi-Fi firmware");
302 wifi_firmware_recovery_mode = FALSE;
308 static int _set_connman_technology_power(gboolean enable)
310 gboolean reply = FALSE;
311 GVariant *param0 = NULL;
312 GVariant *params = NULL;
313 char key[] = "Powered";
314 gboolean value_enable = TRUE;
315 gboolean value_disable = FALSE;
317 if (connman_wifi_technology_state == enable)
321 param0 = g_variant_new_boolean(value_enable);
323 param0 = g_variant_new_boolean(value_disable);
325 params = g_variant_new("(sv)",key, param0);
327 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
328 CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
329 "SetProperty", params, __technology_reply);
332 ERR("Fail to set technology %s", enable == TRUE ? "enable" : "disable");
336 /* If Wi-Fi powered off,
337 * Do not remove Wi-Fi driver until ConnMan technology state updated
340 connman_wifi_technology_state = enable;
342 /* To be keep safe, early disable Wi-Fi tech state */
344 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
349 static void __netconfig_set_wifi_bssid(void)
352 char bssid[WLAN_MAC_ADDR_MAX];
354 FILE *fp = fopen(WLAN_MAC_INFO, "r");
357 ERR("Fail to open file");
361 fseek(fp, 0L, SEEK_SET);
362 rv = fscanf(fp, "%s", bssid);
365 ERR("Fail to read bssid");
367 netconfig_set_vconf_str(VCONF_WIFI_BSSID_ADDRESS, bssid);
372 int netconfig_wifi_driver_and_supplicant(gboolean enable)
374 /* There are 3 thumb rules for Wi-Fi power management
375 * 1. Do not make exposed API to control wpa_supplicant and driver directly.
376 * It probably breaks ConnMan technology operation.
378 * 2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
379 * It breaks ConnMan technology operation.
381 * 3. Final the best rule: make it as simple as possible.
382 * Simple code enables easy maintenance and reduces logical errors.
385 return _load_driver_and_supplicant();
387 if (connman_wifi_technology_state == TRUE)
390 return _load_driver_and_supplicant();
394 void netconfig_wifi_disable_technology_state_by_only_connman_signal(void)
396 /* Important: it's only done by ConnMan technology signal update */
397 connman_wifi_technology_state = FALSE;
400 int netconfig_wifi_on(void)
403 wifi_tech_state_e wifi_tech_state;
405 wifi_tech_state = wifi_state_get_technology_state();
406 if (wifi_tech_state >= NETCONFIG_WIFI_TECH_POWERED)
409 if (__is_wifi_restricted() == TRUE)
412 if (netconfig_is_wifi_tethering_on() == TRUE) {
413 /* TODO: Wi-Fi tethering turns off here */
415 ERR("Failed to turn tethering off");
419 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
420 if (netconfig_is_wifi_direct_on() == TRUE) {
421 if (__netconfig_wifi_direct_power_off() == TRUE)
424 ERR("Failed to turn Wi-Fi direct off");
430 err = wifi_power_driver_and_supplicant(TRUE);
431 if (err < 0 && err != -EALREADY)
434 err = _set_connman_technology_power(TRUE);
436 __netconfig_set_wifi_bssid();
441 int netconfig_wifi_off(void)
445 #if defined TIZEN_P2P_ENABLE && defined WLAN_CONCURRENT_MODE
446 __netconfig_p2p_supplicant(FALSE);
449 err = _set_connman_technology_power(FALSE);
450 if (err == -EALREADY)
451 wifi_state_update_power_state(FALSE);
456 #if defined TIZEN_WEARABLE
457 int netconfig_wifi_on_wearable(gboolean device_picker_test)
462 enum netconfig_wifi_tech_state wifi_tech_state;
463 weconn_service_state_e weconn_state;
465 wifi_tech_state = wifi_state_get_technology_state();
466 if (wifi_tech_state >= NETCONFIG_WIFI_TECH_POWERED)
469 err = weconn_get_service_state(weconn_handle, W_SERVICE_TYPE_BT,
471 if (err == 0 && weconn_state == W_SERVICE_STATE_CONNECTED) {
472 WARN("Not permitted Wi-Fi on");
476 if (vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
477 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
482 if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_mode) < 0) {
483 ERR("Fail to get VCONFKEY_SETAPPL_PSMODE");
487 if (ps_mode > SETTING_PSMODE_NORMAL) {
488 WARN("ps mode is on(%d), Not turn on Wi-Fi", ps_mode);
492 WARN("Not permitted Wi-Fi on");
496 err = wifi_power_driver_and_supplicant(TRUE);
497 if (err < 0 && err != -EALREADY)
500 err = _set_connman_technology_power(TRUE);
502 if (device_picker_test == TRUE)
503 netconfig_wifi_enable_device_picker_test();
508 static void __weconn_service_state_changed_cb(weconn_service_state_e state, void *user_data)
510 if (state == W_SERVICE_STATE_CONNECTED) {
511 DBG("SAP is connected");
512 if (wifi_state > VCONFKEY_WIFI_OFF)
514 } else if (state == W_SERVICE_STATE_DISCONNECTED) {
515 DBG("SAP is disconnected");
516 wifi_power_on_wearable(FALSE);
520 static int _weconn_set_state_changed_cb(int service_type, void *user_data)
525 weconn_destroy(weconn_handle);
526 weconn_handle = NULL;
529 ret = weconn_create(&weconn_handle);
531 ERR("Failed weconn_create(%d)", ret);
535 ret = weconn_set_service_state_change_cb(weconn_handle, __weconn_service_state_changed_cb, service_type, user_data);
537 ERR("Failed weconn_set_service_state_change_cb(%d)", ret);
544 static void __wearable_wifi_use_changed_cb(keynode_t* node, void* user_data)
548 gboolean wifi_restrict = FALSE;
550 if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
551 ERR("Fail to get VCONFKEY_WIFI_STATE");
556 wifi_use = vconf_keynode_get_int(node);
558 vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
562 if (wifi_state > VCONFKEY_WIFI_OFF) {
563 WARN("Wi-Fi is already turned on");
567 wifi_restrict = netconfig_is_wifi_allowed();
568 if (wifi_restrict == FALSE) {
569 DBG("launch wifi restrict popup");
570 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
571 wc_launch_syspopup(WC_POPUP_TYPE_WIFI_RESTRICT);
573 wifi_power_on_wearable(TRUE);
576 ERR("## wifi use [OFF]");
577 if (wifi_state == VCONFKEY_WIFI_OFF) {
578 WARN("Wi-Fi is already turned off");
586 #if defined TIZEN_TELEPHONY_ENABLE
587 static void __netconfig_wifi_wearable_airplane_mode(keynode_t *node,
590 int wifi_use = 0, airplane_state = 0;
591 int wifi_use_off_by_airplane = 0;
593 vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE,
594 &wifi_use_off_by_airplane);
596 vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
599 airplane_state = vconf_keynode_get_bool(node);
601 vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
603 DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
604 DBG("Wi-Fi use %d, Wi-Fi was off by flight mode %s", wifi_use,
605 wifi_use_off_by_airplane ? "Yes" : "No");
607 if (airplane_mode == airplane_state)
610 airplane_mode = airplane_state;
612 if (airplane_state > 0) {
613 /* airplane mode on */
617 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
618 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
621 /* airplane mode off */
622 if (!wifi_use_off_by_airplane)
625 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
626 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 1);
631 #if defined TIZEN_TELEPHONY_ENABLE
632 static void __netconfig_wifi_airplane_mode(keynode_t *node, void *user_data)
634 int wifi_state = 0, airplane_state = 0;
635 int wifi_off_by_airplane = 0;
637 vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
639 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
642 airplane_state = vconf_keynode_get_bool(node);
644 vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
646 DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
647 DBG("Wi-Fi state %d, Wi-Fi was off by flight mode %s", wifi_state,
648 wifi_off_by_airplane ? "Yes" : "No");
650 if (airplane_mode == airplane_state)
653 airplane_mode = airplane_state;
655 if (airplane_state > 0) {
656 /* airplane mode on */
657 if (wifi_state == VCONFKEY_WIFI_OFF)
662 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
664 /* airplane mode off */
665 if (!wifi_off_by_airplane)
668 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
670 if (wifi_state > VCONFKEY_WIFI_OFF)
678 static void __netconfig_wifi_restrict_mode(keynode_t *node, void *user_data)
680 int wifi_state = 0, restricted = 0;
681 int wifi_off_by_restricted = 0;
683 vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
685 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
688 restricted = vconf_keynode_get_bool(node);
690 vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted);
692 DBG("network restricted mode %s", restricted > 0 ? "ON" : "OFF");
693 DBG("Wi-Fi state %d, Wi-Fi was off by restricted mode %s", wifi_state,
694 wifi_off_by_restricted ? "Yes" : "No");
696 if (restricted > 0) {
697 /* network restricted on */
698 if (wifi_state == VCONFKEY_WIFI_OFF)
703 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 1);
705 /* network restricted off */
706 if (!wifi_off_by_restricted)
709 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 0);
711 if (wifi_state > VCONFKEY_WIFI_OFF)
719 static void __emergency_mode_changed_cb(keynode_t *node, void *user_data)
721 int wifi_state = 0, emergency = 0;
722 int wifi_off_by_emergency = 0;
723 #if !defined TIZEN_WEARABLE
724 int emergency_by_fmm = 0;
726 #if defined TIZEN_WEARABLE
730 vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
731 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
733 #if !defined TIZEN_WEARABLE
734 vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT, &emergency_by_fmm);
735 DBG("emergency mode by Find My Mobile (%d)", emergency_by_fmm);
736 if (emergency_by_fmm == 1)
741 emergency = vconf_keynode_get_int(node);
743 vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &emergency);
745 DBG("emergency mode %s", emergency > SETTING_PSMODE_POWERFUL ? "ON" : "OFF");
746 DBG("Wi-Fi state %d, Wi-Fi was off by emergency mode %s", wifi_state, wifi_off_by_emergency ? "Yes" : "No");
748 #if defined TIZEN_WEARABLE
749 if (emergency == SETTING_PSMODE_WEARABLE) {
750 /* basic power saving mode on */
751 } else if (emergency == SETTING_PSMODE_WEARABLE_ENHANCED) {
752 /* enhanced power saving mode on */
753 vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
754 psmode_wifi_use = wifi_use;
756 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
759 if (wifi_state == VCONFKEY_WIFI_OFF)
763 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
765 /* power saving mode off */
766 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, psmode_wifi_use);
767 if (!wifi_off_by_emergency)
770 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
771 if (wifi_state > VCONFKEY_WIFI_OFF)
774 wifi_power_on_wearable(TRUE);
777 if (emergency > SETTING_PSMODE_POWERFUL) {
778 /* emergency mode on */
779 if (wifi_state == VCONFKEY_WIFI_OFF)
784 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
786 /* emergency mode off */
787 if (!wifi_off_by_emergency)
790 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
792 if (wifi_state > VCONFKEY_WIFI_OFF)
801 static void __pm_state_changed_cb(keynode_t* node, void* user_data)
805 static int prev_state = VCONFKEY_PM_STATE_NORMAL;
807 if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
808 ERR("Fail to get VCONFKEY_WIFI_STATE");
813 * VCONFKEY_PM_STATE_NORMAL = 1,
814 * VCONFKEY_PM_STATE_LCDDIM,
815 * VCONFKEY_PM_STATE_LCDOFF,
816 * VCONFKEY_PM_STATE_SLEEP
819 new_state = vconf_keynode_get_int(node);
821 vconf_get_int(VCONFKEY_PM_STATE, &new_state);
823 DBG("wifi state: %d (0 off / 1 on / 2 connected)", wifi_state);
824 DBG("Old PM state: %d, current: %d", prev_state, new_state);
826 if ((new_state == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
827 netconfig_wifi_bgscan_stop();
828 netconfig_wifi_bgscan_start(TRUE);
831 prev_state = new_state;
834 #if defined TIZEN_TELEPHONY_ENABLE
835 static void _tapi_noti_sim_status_cb(TapiHandle *handle, const char *noti_id,
836 void *data, void *user_data)
838 TelSimCardStatus_t *status = data;
840 if (*status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
841 DBG("Turn Wi-Fi on automatically");
842 #if defined TIZEN_WEARABLE
843 wifi_power_on_wearable(TRUE);
847 netconfig_tel_deinit();
851 static gboolean netconfig_tapi_check_sim_state(void)
853 int ret, card_changed;
854 TelSimCardStatus_t status = TAPI_SIM_STATUS_UNKNOWN;
855 TapiHandle *tapi_handle = NULL;
857 tapi_handle = (TapiHandle *)netconfig_tel_init();
858 if (tapi_handle == NULL) {
859 ERR("Failed to tapi init");
863 ret = tel_get_sim_init_info(tapi_handle, &status, &card_changed);
864 if (ret != TAPI_API_SUCCESS) {
865 ERR("tel_get_sim_init_info() Failed : [%d]", ret);
866 netconfig_tel_deinit();
871 case TAPI_SIM_STATUS_UNKNOWN:
872 case TAPI_SIM_STATUS_CARD_ERROR:
873 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
874 case TAPI_SIM_STATUS_CARD_BLOCKED:
875 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
877 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
878 case TAPI_SIM_STATUS_SIM_INITIALIZING:
879 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
880 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
881 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
882 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
883 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
884 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
885 tel_register_noti_event(tapi_handle, TAPI_NOTI_SIM_STATUS,
886 _tapi_noti_sim_status_cb, NULL);
889 ERR("not defined status(%d)", status);
893 netconfig_tel_deinit();
898 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
900 int telephony_ready = 0;
903 telephony_ready = vconf_keynode_get_bool(node);
905 vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
907 if (telephony_ready != 0) {
908 if (netconfig_tapi_check_sim_state() == FALSE) {
909 DBG("Sim is not initialized yet.");
916 DBG("Turn Wi-Fi on automatically");
918 #if defined TIZEN_WEARABLE
919 wifi_power_on_wearable(TRUE);
925 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
929 int wifi_power_driver_and_supplicant(gboolean enable)
931 /* There are 3 thumb rules for Wi-Fi power management
932 * 1. Do not make exposed API to control wpa_supplicant and driver directly.
933 * It probably breaks ConnMan technology operation.
935 * 2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
936 * It breaks ConnMan technology operation.
938 * 3. Final the best rule: make it as simple as possible.
939 * Simple code enables easy maintenance and reduces logical errors.
941 if (enable == TRUE) {
942 return _load_driver_and_supplicant();
944 if (connman_wifi_technology_state == TRUE)
947 return _remove_driver_and_supplicant();
951 void wifi_power_disable_technology_state_by_only_connman_signal(void)
953 /* Important: it's only done by ConnMan technology signal update */
954 connman_wifi_technology_state = FALSE;
957 void wifi_power_recover_firmware(void)
959 wifi_firmware_recovery_mode = TRUE;
961 netconfig_wifi_bgscan_stop();
966 int wifi_power_on(void)
969 wifi_tech_state_e tech_state;
971 tech_state = wifi_state_get_technology_state();
972 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
975 if (__is_wifi_restricted() == TRUE)
978 if (netconfig_is_wifi_tethering_on() == TRUE) {
979 /* TODO: Wi-Fi tethering turns off here */
981 ERR("Failed to turn tethering off");
985 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
986 if (netconfig_is_wifi_direct_on() == TRUE) {
987 if (__netconfig_wifi_direct_power_off() == TRUE)
990 ERR("Failed to turn Wi-Fi direct off");
996 err = wifi_power_driver_and_supplicant(TRUE);
997 if (err < 0 && err != -EALREADY)
1000 err = _set_connman_technology_power(TRUE);
1005 int wifi_power_off(void)
1009 err = _set_connman_technology_power(FALSE);
1010 if (err == -EALREADY)
1011 wifi_state_update_power_state(FALSE);
1016 #if defined TIZEN_WEARABLE
1017 int wifi_power_on_wearable(gboolean device_picker_test)
1021 wifi_tech_state_e tech_state;
1022 weconn_service_state_e weconn_state;
1024 tech_state = wifi_state_get_technology_state();
1025 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
1028 err = weconn_get_service_state(weconn_handle, W_SERVICE_TYPE_BT, &weconn_state);
1029 if (err == 0 && weconn_state == W_SERVICE_STATE_CONNECTED) {
1030 WARN("Not permitted Wi-Fi on");
1034 if (vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
1035 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
1039 if (wifi_use == 0) {
1040 WARN("VCONF_WIFI_WEARABLE_WIFI_USE is OFF");
1044 err = wifi_power_driver_and_supplicant(TRUE);
1045 if (err < 0 && err != -EALREADY)
1048 err = _set_connman_technology_power(TRUE);
1050 if (device_picker_test == TRUE)
1051 netconfig_wifi_enable_device_picker_test();
1057 void wifi_power_initialize(void)
1059 int wifi_last_power_state = 0;
1061 /* Initialize Airplane mode */
1062 #if defined TIZEN_TELEPHONY_ENABLE
1063 vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
1065 DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
1067 /* Update the last Wi-Fi power state */
1068 vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
1069 if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
1070 #if defined TIZEN_TELEPHONY_ENABLE
1071 int telephony_ready = 0;
1072 vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
1073 if (telephony_ready == 0) {
1074 DBG("Telephony API is not initialized yet");
1075 vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
1076 __netconfig_telephony_ready_changed_cb, NULL);
1078 if (netconfig_tapi_check_sim_state() == FALSE)
1079 DBG("SIM is not initialized yet");
1082 DBG("Turn Wi-Fi on automatically");
1083 #if defined TIZEN_WEARABLE
1084 wifi_power_on_wearable(TRUE);
1090 #if defined TIZEN_WEARABLE
1091 _weconn_set_state_changed_cb(W_SERVICE_TYPE_BT, NULL);
1092 vconf_notify_key_changed(VCONF_WIFI_WEARABLE_WIFI_USE, __wearable_wifi_use_changed_cb, NULL);
1094 #if defined TIZEN_TELEPHONY_ENABLE
1095 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
1096 __netconfig_wifi_wearable_airplane_mode, NULL);
1099 vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
1100 __netconfig_wifi_restrict_mode, NULL);
1101 #if defined TIZEN_TELEPHONY_ENABLE
1102 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
1103 __netconfig_wifi_airplane_mode, NULL);
1107 vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
1108 vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
1111 void wifi_power_deinitialize(void)
1115 gboolean handle_load_driver(Wifi *wifi,
1116 GDBusMethodInvocation *context, gboolean device_picker_test)
1120 DBG("Wi-Fi power on requested");
1122 g_return_val_if_fail(wifi != NULL, FALSE);
1124 #if defined TIZEN_WEARABLE
1125 err = wifi_power_on_wearable(device_picker_test);
1127 err = wifi_power_on();
1129 if (device_picker_test == TRUE)
1130 netconfig_wifi_enable_device_picker_test();
1133 if (err == -EINPROGRESS)
1134 netconfig_error_inprogress(context);
1135 else if (err == -EALREADY)
1136 netconfig_error_already_exists(context);
1137 else if (err == -EPERM)
1138 netconfig_error_permission_denied(context);
1140 netconfig_error_wifi_driver_failed(context);
1146 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
1147 __netconfig_set_wifi_bssid();
1149 wifi_complete_load_driver(wifi, context);
1153 gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
1157 DBG("Wi-Fi power off requested");
1159 g_return_val_if_fail(wifi != NULL, FALSE);
1161 err = wifi_power_off();
1163 if (err == -EINPROGRESS)
1164 netconfig_error_inprogress(context);
1165 else if (err == -EALREADY)
1166 netconfig_error_already_exists(context);
1167 else if (err == -EPERM)
1168 netconfig_error_permission_denied(context);
1170 netconfig_error_wifi_driver_failed(context);
1174 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
1176 wifi_complete_remove_driver(wifi, context);
1180 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
1184 wifi_complete_load_p2p_driver(wifi, context);
1188 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
1192 wifi_complete_remove_p2p_driver(wifi, context);
1196 #if defined TIZEN_TV
1197 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
1199 DBG("Generate Random Mac address of ethernet");
1203 fp = fopen(OS_RANDOM_FILE, "rb");
1206 ERR("Could not open /dev/urandom");
1209 rc = fread(mac_buf, 1, mac_len, fp);
1213 return rc != mac_len ? -1 : 0;
1216 void __netconfig_set_ether_macaddr()
1219 DBG("Set wired Mac address ");
1220 char *mac_addr = NULL;
1223 mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
1224 if (mac_addr == NULL) {
1225 DBG("vconf_get_str Failed\n");
1228 /* Checking Invalid MAC Address */
1229 if ((strlen(mac_addr) == 0)) {
1230 ERR("Failed to get valid MAC Address from vconf");
1231 /*Generate the Random Mac address*/
1232 unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
1234 if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE == -1)) {
1236 ERR("Could not generate the Random Mac address");
1241 rand_mac_add[0] &= 0xFE; /*Clear multicase bit*/
1242 rand_mac_add[0] |= 0x02; /*set local assignment bit*/
1244 /*Set the Mac address in Vconf*/
1245 sprintf_s(mac_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
1246 rand_mac_add[0], rand_mac_add[1],
1247 rand_mac_add[2], rand_mac_add[3],
1248 rand_mac_add[4], rand_mac_add[5]);
1250 netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, mac_addr);
1253 DBG("MAC Address of eth0 [%s]",mac_addr);
1254 const char *path = NET_EXEC_PATH;
1255 char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
1256 "ether",mac_addr, "up", NULL};
1257 char *const envs[] = { NULL };
1258 rv = netconfig_execute_file(path, args, envs);
1261 ERR("Unable to execute system command");