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 "wifi-firmware.h"
43 #include "wifi-background-scan.h"
46 #define WLAN_SUPPLICANT_SCRIPT "/usr/sbin/wpa_supp.sh"
47 #define P2P_SUPPLICANT_SCRIPT "/usr/sbin/p2p_supp.sh"
49 #if defined TIZEN_WEARABLE
51 static weconn_h weconn_handle = NULL;
54 #define VCONF_WIFI_OFF_STATE_BY_AIRPLANE "file/private/wifi/wifi_off_by_airplane"
55 #define VCONF_WIFI_OFF_STATE_BY_RESTRICTED "file/private/wifi/wifi_off_by_restricted"
56 #define VCONF_WIFI_OFF_STATE_BY_EMERGENCY "file/private/wifi/wifi_off_by_emergency"
57 #if defined TIZEN_WEARABLE
58 #define VCONF_WIFI_WEARABLE_WIFI_USE "db/private/wifi/wearable_wifi_use"
60 #if !defined TIZEN_WEARABLE
61 #define VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT "db/setting/network_with_lcd_off_limit"
64 #define WLAN_MAC_INFO tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
65 #define WLAN_MAC_ADDR_MAX 20
66 #define VCONF_WIFI_BSSID_ADDRESS "db/wifi/bssid_address"
69 #define ETH_MAC_ADDR_SIZE 6
70 #define VCONF_ETH_MAC_ADDRESS "db/dnet/mac_address"
71 #define NET_EXEC_PATH "/sbin/ifconfig"
72 #define OS_RANDOM_FILE "/dev/urandom"
75 static gboolean connman_wifi_technology_state = FALSE;
76 static gboolean wifi_firmware_recovery_mode = FALSE;
77 static int airplane_mode = 0;
79 #if defined TIZEN_WEARABLE
80 static int psmode_wifi_use = 1;
83 static gboolean __is_wifi_restricted(void)
85 #if defined TIZEN_WEARABLE
88 int restricted_mode = 0;
90 vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted_mode);
91 if (restricted_mode != 0) {
92 DBG("network restricted mode[%d]", restricted_mode);
99 static void __technology_reply(GObject *source_object, GAsyncResult *res, gpointer user_data)
102 GDBusConnection *conn = NULL;
103 GError *error = NULL;
105 conn = G_DBUS_CONNECTION(source_object);
106 reply = g_dbus_connection_call_finish(conn, res, &error);
110 if (g_strcmp0(error->message, CONNMAN_ERROR_INTERFACE ".AlreadyEnabled") == 0)
111 wifi_state_update_power_state(TRUE);
112 else if (g_strcmp0(error->message, CONNMAN_ERROR_INTERFACE ".AlreadyDisabled") == 0)
113 wifi_state_update_power_state(FALSE);
115 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 #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 file");
328 fseek(fp, 0L, SEEK_SET);
329 rv = fscanf(fp, "%17s", bssid);
332 ERR("Fail to read bssid");
334 netconfig_set_vconf_str(VCONF_WIFI_BSSID_ADDRESS, bssid);
339 int netconfig_wifi_driver_and_supplicant(gboolean enable)
341 /* There are 3 thumb rules for Wi-Fi power management
342 * 1. Do not make exposed API to control wpa_supplicant and driver directly.
343 * It probably breaks ConnMan technology operation.
345 * 2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
346 * It breaks ConnMan technology operation.
348 * 3. Final the best rule: make it as simple as possible.
349 * Simple code enables easy maintenance and reduces logical errors.
352 return _load_driver_and_supplicant();
354 if (connman_wifi_technology_state == TRUE)
357 return _load_driver_and_supplicant();
361 void netconfig_wifi_disable_technology_state_by_only_connman_signal(void)
363 /* Important: it's only done by ConnMan technology signal update */
364 connman_wifi_technology_state = FALSE;
367 int netconfig_wifi_on(void)
370 wifi_tech_state_e wifi_tech_state;
372 wifi_tech_state = wifi_state_get_technology_state();
373 if (wifi_tech_state >= NETCONFIG_WIFI_TECH_POWERED)
376 if (__is_wifi_restricted() == TRUE)
379 if (netconfig_is_wifi_tethering_on() == TRUE) {
380 /* TODO: Wi-Fi tethering turns off here */
382 ERR("Failed to turn tethering off");
386 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
387 if (netconfig_is_wifi_direct_on() == TRUE) {
388 if (__netconfig_wifi_direct_power_off() == TRUE)
391 ERR("Failed to turn Wi-Fi direct off");
397 err = wifi_power_driver_and_supplicant(TRUE);
398 if (err < 0 && err != -EALREADY)
401 err = _set_connman_technology_power(TRUE);
403 __netconfig_set_wifi_bssid();
408 int netconfig_wifi_off(void)
412 #if defined TIZEN_P2P_ENABLE && defined WLAN_CONCURRENT_MODE
413 __netconfig_p2p_supplicant(FALSE);
416 err = _set_connman_technology_power(FALSE);
417 if (err == -EALREADY)
418 wifi_state_update_power_state(FALSE);
423 #if defined TIZEN_WEARABLE
424 int netconfig_wifi_on_wearable(gboolean device_picker_test)
429 enum netconfig_wifi_tech_state wifi_tech_state;
430 weconn_service_state_e weconn_state;
432 wifi_tech_state = wifi_state_get_technology_state();
433 if (wifi_tech_state >= NETCONFIG_WIFI_TECH_POWERED)
436 err = weconn_get_service_state(weconn_handle, W_SERVICE_TYPE_BT,
438 if (err == 0 && weconn_state == W_SERVICE_STATE_CONNECTED) {
439 WARN("Not permitted Wi-Fi on");
443 if (vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
444 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
449 if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_mode) < 0) {
450 ERR("Fail to get VCONFKEY_SETAPPL_PSMODE");
454 if (ps_mode > SETTING_PSMODE_NORMAL) {
455 WARN("ps mode is on(%d), Not turn on Wi-Fi", ps_mode);
459 WARN("Not permitted Wi-Fi on");
463 err = wifi_power_driver_and_supplicant(TRUE);
464 if (err < 0 && err != -EALREADY)
467 err = _set_connman_technology_power(TRUE);
469 if (device_picker_test == TRUE)
470 netconfig_wifi_enable_device_picker_test();
475 static void __weconn_service_state_changed_cb(weconn_service_state_e state, void *user_data)
477 if (state == W_SERVICE_STATE_CONNECTED) {
478 DBG("SAP is connected");
479 if (wifi_state > VCONFKEY_WIFI_OFF)
481 } else if (state == W_SERVICE_STATE_DISCONNECTED) {
482 DBG("SAP is disconnected");
483 wifi_power_on_wearable(FALSE);
487 static int _weconn_set_state_changed_cb(int service_type, void *user_data)
492 weconn_destroy(weconn_handle);
493 weconn_handle = NULL;
496 ret = weconn_create(&weconn_handle);
498 ERR("Failed weconn_create(%d)", ret);
502 ret = weconn_set_service_state_change_cb(weconn_handle, __weconn_service_state_changed_cb, service_type, user_data);
504 ERR("Failed weconn_set_service_state_change_cb(%d)", ret);
511 static void __wearable_wifi_use_changed_cb(keynode_t* node, void* user_data)
515 gboolean wifi_restrict = FALSE;
517 if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
518 ERR("Fail to get VCONFKEY_WIFI_STATE");
523 wifi_use = vconf_keynode_get_int(node);
525 vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
529 if (wifi_state > VCONFKEY_WIFI_OFF) {
530 WARN("Wi-Fi is already turned on");
534 wifi_restrict = netconfig_is_wifi_allowed();
535 if (wifi_restrict == FALSE) {
536 DBG("launch wifi restrict popup");
537 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
538 wc_launch_syspopup(WC_POPUP_TYPE_WIFI_RESTRICT);
540 wifi_power_on_wearable(TRUE);
543 ERR("## wifi use [OFF]");
544 if (wifi_state == VCONFKEY_WIFI_OFF) {
545 WARN("Wi-Fi is already turned off");
553 #if defined TIZEN_TELEPHONY_ENABLE
554 static void __netconfig_wifi_wearable_airplane_mode(keynode_t *node,
557 int wifi_use = 0, airplane_state = 0;
558 int wifi_use_off_by_airplane = 0;
560 vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE,
561 &wifi_use_off_by_airplane);
563 vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
566 airplane_state = vconf_keynode_get_bool(node);
568 vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
570 DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
571 DBG("Wi-Fi use %d, Wi-Fi was off by flight mode %s", wifi_use,
572 wifi_use_off_by_airplane ? "Yes" : "No");
574 if (airplane_mode == airplane_state)
577 airplane_mode = airplane_state;
579 if (airplane_state > 0) {
580 /* airplane mode on */
584 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
585 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
588 /* airplane mode off */
589 if (!wifi_use_off_by_airplane)
592 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
593 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 1);
598 #if defined TIZEN_TELEPHONY_ENABLE
599 static void __netconfig_wifi_airplane_mode(keynode_t *node, void *user_data)
601 int wifi_state = 0, airplane_state = 0;
602 int wifi_off_by_airplane = 0;
604 vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
606 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
609 airplane_state = vconf_keynode_get_bool(node);
611 vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
613 DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
614 DBG("Wi-Fi state %d, Wi-Fi was off by flight mode %s", wifi_state,
615 wifi_off_by_airplane ? "Yes" : "No");
617 if (airplane_mode == airplane_state)
620 airplane_mode = airplane_state;
622 if (airplane_state > 0) {
623 /* airplane mode on */
624 if (wifi_state == VCONFKEY_WIFI_OFF)
629 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
631 /* airplane mode off */
632 if (!wifi_off_by_airplane)
635 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
637 if (wifi_state > VCONFKEY_WIFI_OFF)
645 static void __netconfig_wifi_restrict_mode(keynode_t *node, void *user_data)
647 int wifi_state = 0, restricted = 0;
648 int wifi_off_by_restricted = 0;
650 vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
652 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
655 restricted = vconf_keynode_get_bool(node);
657 vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted);
659 DBG("network restricted mode %s", restricted > 0 ? "ON" : "OFF");
660 DBG("Wi-Fi state %d, Wi-Fi was off by restricted mode %s", wifi_state,
661 wifi_off_by_restricted ? "Yes" : "No");
663 if (restricted > 0) {
664 /* network restricted on */
665 if (wifi_state == VCONFKEY_WIFI_OFF)
670 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 1);
672 /* network restricted off */
673 if (!wifi_off_by_restricted)
676 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 0);
678 if (wifi_state > VCONFKEY_WIFI_OFF)
686 static void __emergency_mode_changed_cb(keynode_t *node, void *user_data)
688 int wifi_state = 0, emergency = 0;
689 int wifi_off_by_emergency = 0;
690 #if !defined TIZEN_WEARABLE
691 int emergency_by_fmm = 0;
693 #if defined TIZEN_WEARABLE
697 vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
698 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
700 #if !defined TIZEN_WEARABLE
701 vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT, &emergency_by_fmm);
702 DBG("emergency mode by Find My Mobile (%d)", emergency_by_fmm);
703 if (emergency_by_fmm == 1)
708 emergency = vconf_keynode_get_int(node);
710 vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &emergency);
712 DBG("emergency mode %s", emergency > SETTING_PSMODE_POWERFUL ? "ON" : "OFF");
713 DBG("Wi-Fi state %d, Wi-Fi was off by emergency mode %s", wifi_state, wifi_off_by_emergency ? "Yes" : "No");
715 #if defined TIZEN_WEARABLE
716 if (emergency == SETTING_PSMODE_WEARABLE) {
717 /* basic power saving mode on */
718 } else if (emergency == SETTING_PSMODE_WEARABLE_ENHANCED) {
719 /* enhanced power saving mode on */
720 vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
721 psmode_wifi_use = wifi_use;
723 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
725 if (wifi_state == VCONFKEY_WIFI_OFF)
729 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
731 /* power saving mode off */
732 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, psmode_wifi_use);
733 if (!wifi_off_by_emergency)
736 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
737 if (wifi_state > VCONFKEY_WIFI_OFF)
740 wifi_power_on_wearable(TRUE);
743 if (emergency > SETTING_PSMODE_POWERFUL) {
744 /* emergency mode on */
745 if (wifi_state == VCONFKEY_WIFI_OFF)
750 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
752 /* emergency mode off */
753 if (!wifi_off_by_emergency)
756 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
758 if (wifi_state > VCONFKEY_WIFI_OFF)
767 static void __pm_state_changed_cb(keynode_t* node, void* user_data)
771 static int prev_state = VCONFKEY_PM_STATE_NORMAL;
773 if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
774 ERR("Fail to get VCONFKEY_WIFI_STATE");
779 * VCONFKEY_PM_STATE_NORMAL = 1,
780 * VCONFKEY_PM_STATE_LCDDIM,
781 * VCONFKEY_PM_STATE_LCDOFF,
782 * VCONFKEY_PM_STATE_SLEEP
785 new_state = vconf_keynode_get_int(node);
787 vconf_get_int(VCONFKEY_PM_STATE, &new_state);
789 DBG("wifi state: %d (0 off / 1 on / 2 connected)", wifi_state);
790 DBG("Old PM state: %d, current: %d", prev_state, new_state);
792 if ((new_state == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
793 netconfig_wifi_bgscan_stop();
794 netconfig_wifi_bgscan_start(TRUE);
797 prev_state = new_state;
800 #if defined TIZEN_TELEPHONY_ENABLE
801 static void _tapi_noti_sim_status_cb(TapiHandle *handle, const char *noti_id,
802 void *data, void *user_data)
804 TelSimCardStatus_t *status = data;
806 if (*status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
807 DBG("Turn Wi-Fi on automatically");
808 #if defined TIZEN_WEARABLE
809 wifi_power_on_wearable(TRUE);
813 netconfig_tel_deinit();
817 static gboolean netconfig_tapi_check_sim_state(void)
819 int ret, card_changed;
820 TelSimCardStatus_t status = TAPI_SIM_STATUS_UNKNOWN;
821 TapiHandle *tapi_handle = NULL;
823 tapi_handle = (TapiHandle *)netconfig_tel_init();
824 if (tapi_handle == NULL) {
825 ERR("Failed to tapi init");
829 ret = tel_get_sim_init_info(tapi_handle, &status, &card_changed);
830 if (ret != TAPI_API_SUCCESS) {
831 ERR("tel_get_sim_init_info() Failed : [%d]", ret);
832 netconfig_tel_deinit();
837 case TAPI_SIM_STATUS_UNKNOWN:
838 case TAPI_SIM_STATUS_CARD_ERROR:
839 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
840 case TAPI_SIM_STATUS_CARD_BLOCKED:
841 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
843 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
844 case TAPI_SIM_STATUS_SIM_INITIALIZING:
845 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
846 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
847 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
848 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
849 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
850 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
851 tel_register_noti_event(tapi_handle, TAPI_NOTI_SIM_STATUS,
852 _tapi_noti_sim_status_cb, NULL);
855 ERR("not defined status(%d)", status);
859 netconfig_tel_deinit();
864 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
866 int telephony_ready = 0;
869 telephony_ready = vconf_keynode_get_bool(node);
871 vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
873 if (telephony_ready != 0) {
874 if (netconfig_tapi_check_sim_state() == FALSE) {
875 DBG("Sim is not initialized yet.");
882 DBG("Turn Wi-Fi on automatically");
884 #if defined TIZEN_WEARABLE
885 wifi_power_on_wearable(TRUE);
891 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
895 int wifi_power_driver_and_supplicant(gboolean enable)
897 /* There are 3 thumb rules for Wi-Fi power management
898 * 1. Do not make exposed API to control wpa_supplicant and driver directly.
899 * It probably breaks ConnMan technology operation.
901 * 2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
902 * It breaks ConnMan technology operation.
904 * 3. Final the best rule: make it as simple as possible.
905 * Simple code enables easy maintenance and reduces logical errors.
907 if (enable == TRUE) {
908 return _load_driver_and_supplicant();
910 if (connman_wifi_technology_state == TRUE)
913 return _remove_driver_and_supplicant();
917 void wifi_power_disable_technology_state_by_only_connman_signal(void)
919 /* Important: it's only done by ConnMan technology signal update */
920 connman_wifi_technology_state = FALSE;
923 void wifi_power_recover_firmware(void)
925 wifi_firmware_recovery_mode = TRUE;
927 netconfig_wifi_bgscan_stop();
932 int wifi_power_on(void)
935 wifi_tech_state_e tech_state;
937 tech_state = wifi_state_get_technology_state();
938 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
941 if (__is_wifi_restricted() == TRUE)
944 if (netconfig_is_wifi_tethering_on() == TRUE) {
945 /* TODO: Wi-Fi tethering turns off here */
947 ERR("Failed to turn tethering off");
951 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
952 if (netconfig_is_wifi_direct_on() == TRUE) {
953 if (__netconfig_wifi_direct_power_off() == TRUE)
956 ERR("Failed to turn Wi-Fi direct off");
962 err = wifi_power_driver_and_supplicant(TRUE);
963 if (err < 0 && err != -EALREADY)
966 err = _set_connman_technology_power(TRUE);
971 int wifi_power_off(void)
975 err = _set_connman_technology_power(FALSE);
976 if (err == -EALREADY)
977 wifi_state_update_power_state(FALSE);
982 #if defined TIZEN_WEARABLE
983 int wifi_power_on_wearable(gboolean device_picker_test)
987 wifi_tech_state_e tech_state;
988 weconn_service_state_e weconn_state;
990 tech_state = wifi_state_get_technology_state();
991 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
994 err = weconn_get_service_state(weconn_handle, W_SERVICE_TYPE_BT, &weconn_state);
995 if (err == 0 && weconn_state == W_SERVICE_STATE_CONNECTED) {
996 WARN("Not permitted Wi-Fi on");
1000 if (vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
1001 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
1005 if (wifi_use == 0) {
1006 WARN("VCONF_WIFI_WEARABLE_WIFI_USE is OFF");
1010 err = wifi_power_driver_and_supplicant(TRUE);
1011 if (err < 0 && err != -EALREADY)
1014 err = _set_connman_technology_power(TRUE);
1016 if (device_picker_test == TRUE)
1017 netconfig_wifi_enable_device_picker_test();
1023 void wifi_power_initialize(void)
1025 int wifi_last_power_state = 0;
1027 /* Initialize Airplane mode */
1028 #if defined TIZEN_TELEPHONY_ENABLE
1029 vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
1031 DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
1033 /* Update the last Wi-Fi power state */
1034 vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
1035 if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
1036 #if defined TIZEN_TELEPHONY_ENABLE
1037 int telephony_ready = 0;
1038 vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
1039 if (telephony_ready == 0) {
1040 DBG("Telephony API is not initialized yet");
1041 vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
1042 __netconfig_telephony_ready_changed_cb, NULL);
1044 if (netconfig_tapi_check_sim_state() == FALSE)
1045 DBG("SIM is not initialized yet");
1048 DBG("Turn Wi-Fi on automatically");
1049 #if defined TIZEN_WEARABLE
1050 wifi_power_on_wearable(TRUE);
1056 #if defined TIZEN_WEARABLE
1057 _weconn_set_state_changed_cb(W_SERVICE_TYPE_BT, NULL);
1058 vconf_notify_key_changed(VCONF_WIFI_WEARABLE_WIFI_USE, __wearable_wifi_use_changed_cb, NULL);
1060 #if defined TIZEN_TELEPHONY_ENABLE
1061 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
1062 __netconfig_wifi_wearable_airplane_mode, NULL);
1065 vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
1066 __netconfig_wifi_restrict_mode, NULL);
1067 #if defined TIZEN_TELEPHONY_ENABLE
1068 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
1069 __netconfig_wifi_airplane_mode, NULL);
1073 vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
1074 vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
1077 void wifi_power_deinitialize(void)
1081 gboolean handle_load_driver(Wifi *wifi,
1082 GDBusMethodInvocation *context, gboolean device_picker_test)
1086 DBG("Wi-Fi power on requested");
1088 g_return_val_if_fail(wifi != NULL, FALSE);
1090 #if defined TIZEN_WEARABLE
1091 err = wifi_power_on_wearable(device_picker_test);
1093 err = wifi_power_on();
1095 if (device_picker_test == TRUE)
1096 netconfig_wifi_enable_device_picker_test();
1099 if (err == -EINPROGRESS)
1100 netconfig_error_inprogress(context);
1101 else if (err == -EALREADY)
1102 netconfig_error_already_exists(context);
1103 else if (err == -EPERM)
1104 netconfig_error_permission_denied(context);
1106 netconfig_error_wifi_driver_failed(context);
1112 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
1113 __netconfig_set_wifi_bssid();
1115 wifi_complete_load_driver(wifi, context);
1119 gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
1123 DBG("Wi-Fi power off requested");
1125 g_return_val_if_fail(wifi != NULL, FALSE);
1127 err = wifi_power_off();
1129 if (err == -EINPROGRESS)
1130 netconfig_error_inprogress(context);
1131 else if (err == -EALREADY)
1132 netconfig_error_already_exists(context);
1133 else if (err == -EPERM)
1134 netconfig_error_permission_denied(context);
1136 netconfig_error_wifi_driver_failed(context);
1140 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
1142 wifi_complete_remove_driver(wifi, context);
1146 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
1150 wifi_complete_load_p2p_driver(wifi, context);
1154 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
1158 wifi_complete_remove_p2p_driver(wifi, context);
1162 #if defined TIZEN_TV
1163 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
1165 DBG("Generate Random Mac address of ethernet");
1169 fp = fopen(OS_RANDOM_FILE, "rb");
1172 ERR("Could not open /dev/urandom");
1175 rc = fread(mac_buf, 1, mac_len, fp);
1179 return rc != mac_len ? -1 : 0;
1182 void __netconfig_set_ether_macaddr()
1185 DBG("Set wired Mac address ");
1186 char *mac_addr = NULL;
1189 mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
1190 if (mac_addr == NULL) {
1191 DBG("vconf_get_str Failed\n");
1194 /* Checking Invalid MAC Address */
1195 if ((strlen(mac_addr) == 0)) {
1196 ERR("Failed to get valid MAC Address from vconf");
1197 /*Generate the Random Mac address*/
1198 unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
1200 if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE == -1)) {
1202 ERR("Could not generate the Random Mac address");
1207 rand_mac_add[0] &= 0xFE; /*Clear multicase bit*/
1208 rand_mac_add[0] |= 0x02; /*set local assignment bit*/
1210 /*Set the Mac address in Vconf*/
1211 snprintf(mac_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
1212 rand_mac_add[0], rand_mac_add[1],
1213 rand_mac_add[2], rand_mac_add[3],
1214 rand_mac_add[4], rand_mac_add[5]);
1216 netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, mac_addr);
1219 DBG("MAC Address of eth0 [%s]", mac_addr);
1220 const char *path = NET_EXEC_PATH;
1221 char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
1222 "ether", mac_addr, "up", NULL};
1223 char *const envs[] = { NULL };
1224 rv = netconfig_execute_file(path, args, envs);
1227 ERR("Unable to execute system command");