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_SHARE, "/.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);
119 ERR("Fail torequest status");
122 DBG("Successfully requested");
125 g_variant_unref(reply);
126 netconfig_gdbus_pending_call_unref();
129 static int __execute_supplicant(gboolean enable)
132 const char *path = WLAN_SUPPLICANT_SCRIPT;
133 char *const args_enable[] = { "/usr/sbin/wpa_supp.sh", "start", NULL };
134 char *const args_disable[] = { "/usr/sbin/wpa_supp.sh", "stop", NULL };
135 char *const envs[] = { NULL };
136 static gboolean enabled = FALSE;
138 if (enabled == enable)
142 rv = netconfig_execute_file(path, args_enable, envs);
144 rv = netconfig_execute_file(path, args_disable, envs);
148 DBG("wpa_supplicant %s", enable == TRUE ? "started" : "stopped");
155 #if defined TIZEN_P2P_ENABLE && defined WLAN_CONCURRENT_MODE
156 static int __netconfig_p2p_supplicant(gboolean enable)
159 const char *path = P2P_SUPPLICANT_SCRIPT;
160 char *const args_enable[] = { P2P_SUPPLICANT_SCRIPT, "start", NULL };
161 char *const args_disable[] = { P2P_SUPPLICANT_SCRIPT, "stop", NULL };
162 char *const envs[] = { NULL };
165 rv = netconfig_execute_file(path, args_enable, envs);
167 rv = netconfig_execute_file(path, args_disable, envs);
171 DBG("p2p_supplicant %s", enable == TRUE ? "started" : "stopped");
177 void netconfig_wifi_recover_firmware(void)
179 wifi_firmware_recovery_mode = TRUE;
181 netconfig_wifi_bgscan_stop();
186 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
187 static void __netconfig_wifi_direct_state_cb(int error_code, wifi_direct_device_state_e device_state, void *user_data)
191 wifi_direct_unset_device_state_changed_cb();
192 wifi_direct_deinitialize();
194 if (device_state == WIFI_DIRECT_DEVICE_STATE_DEACTIVATED) {
195 err = wifi_power_on();
197 if (err == -EALREADY)
198 wifi_state_update_power_state(TRUE);
200 wifi_state_emit_power_failed();
205 static gboolean __netconfig_wifi_direct_power_off(void)
207 DBG("Wi-Fi direct is turning off");
209 if (wifi_direct_initialize() < 0)
212 if (wifi_direct_set_device_state_changed_cb(__netconfig_wifi_direct_state_cb, NULL) < 0)
215 if (wifi_direct_deactivate() < 0)
222 static int _load_driver_and_supplicant(void)
225 wifi_tech_state_e tech_state;
227 tech_state = wifi_state_get_technology_state();
228 if (tech_state > NETCONFIG_WIFI_TECH_OFF)
231 err = __execute_supplicant(TRUE);
232 if (err < 0 && err != -EALREADY)
235 err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, TRUE);
236 if (err < 0 && err != -EALREADY) {
237 __execute_supplicant(FALSE);
241 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
246 static int _remove_driver_and_supplicant(void)
250 if (wifi_firmware_recovery_mode != TRUE &&
251 netconfig_wifi_is_wps_enabled() == TRUE) {
252 DBG("Wi-Fi WPS mode");
256 err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, FALSE);
257 if (err < 0 && err != -EALREADY)
260 err = __execute_supplicant(FALSE);
261 if (err < 0 && err != -EALREADY)
264 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_OFF);
266 if (wifi_firmware_recovery_mode == TRUE) {
267 if (wifi_power_on() < 0)
268 ERR("Failed to recover Wi-Fi firmware");
270 wifi_firmware_recovery_mode = FALSE;
276 static int _set_connman_technology_power(gboolean enable)
278 gboolean reply = FALSE;
279 GVariant *param0 = NULL;
280 GVariant *params = NULL;
281 char key[] = "Powered";
282 gboolean value_enable = TRUE;
283 gboolean value_disable = FALSE;
285 if (connman_wifi_technology_state == enable)
289 param0 = g_variant_new_boolean(value_enable);
291 param0 = g_variant_new_boolean(value_disable);
293 params = g_variant_new("(sv)",key, param0);
295 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
296 CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
297 "SetProperty", params, __technology_reply);
300 ERR("Fail to set technology %s", enable == TRUE ? "enable" : "disable");
304 /* If Wi-Fi powered off,
305 * Do not remove Wi-Fi driver until ConnMan technology state updated
308 connman_wifi_technology_state = enable;
310 /* To be keep safe, early disable Wi-Fi tech state */
312 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
317 static void __netconfig_set_wifi_bssid(void)
320 char bssid[WLAN_MAC_ADDR_MAX];
322 FILE *fp = fopen(WLAN_MAC_INFO, "r");
325 ERR("Fail to open file");
329 fseek(fp, 0L, SEEK_SET);
330 rv = fscanf(fp, "%17s", bssid);
333 ERR("Fail to read bssid");
335 netconfig_set_vconf_str(VCONF_WIFI_BSSID_ADDRESS, bssid);
340 int netconfig_wifi_driver_and_supplicant(gboolean enable)
342 /* There are 3 thumb rules for Wi-Fi power management
343 * 1. Do not make exposed API to control wpa_supplicant and driver directly.
344 * It probably breaks ConnMan technology operation.
346 * 2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
347 * It breaks ConnMan technology operation.
349 * 3. Final the best rule: make it as simple as possible.
350 * Simple code enables easy maintenance and reduces logical errors.
353 return _load_driver_and_supplicant();
355 if (connman_wifi_technology_state == TRUE)
358 return _load_driver_and_supplicant();
362 void netconfig_wifi_disable_technology_state_by_only_connman_signal(void)
364 /* Important: it's only done by ConnMan technology signal update */
365 connman_wifi_technology_state = FALSE;
368 int netconfig_wifi_on(void)
371 wifi_tech_state_e wifi_tech_state;
373 wifi_tech_state = wifi_state_get_technology_state();
374 if (wifi_tech_state >= NETCONFIG_WIFI_TECH_POWERED)
377 if (__is_wifi_restricted() == TRUE)
380 if (netconfig_is_wifi_tethering_on() == TRUE) {
381 /* TODO: Wi-Fi tethering turns off here */
383 ERR("Failed to turn tethering off");
387 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
388 if (netconfig_is_wifi_direct_on() == TRUE) {
389 if (__netconfig_wifi_direct_power_off() == TRUE)
392 ERR("Failed to turn Wi-Fi direct off");
398 err = wifi_power_driver_and_supplicant(TRUE);
399 if (err < 0 && err != -EALREADY)
402 err = _set_connman_technology_power(TRUE);
404 __netconfig_set_wifi_bssid();
409 int netconfig_wifi_off(void)
413 #if defined TIZEN_P2P_ENABLE && defined WLAN_CONCURRENT_MODE
414 __netconfig_p2p_supplicant(FALSE);
417 err = _set_connman_technology_power(FALSE);
418 if (err == -EALREADY)
419 wifi_state_update_power_state(FALSE);
424 #if defined TIZEN_WEARABLE
425 int netconfig_wifi_on_wearable(gboolean device_picker_test)
430 enum netconfig_wifi_tech_state wifi_tech_state;
431 weconn_service_state_e weconn_state;
433 wifi_tech_state = wifi_state_get_technology_state();
434 if (wifi_tech_state >= NETCONFIG_WIFI_TECH_POWERED)
437 err = weconn_get_service_state(weconn_handle, W_SERVICE_TYPE_BT,
439 if (err == 0 && weconn_state == W_SERVICE_STATE_CONNECTED) {
440 WARN("Not permitted Wi-Fi on");
444 if (vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
445 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
450 if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_mode) < 0) {
451 ERR("Fail to get VCONFKEY_SETAPPL_PSMODE");
455 if (ps_mode > SETTING_PSMODE_NORMAL) {
456 WARN("ps mode is on(%d), Not turn on Wi-Fi", ps_mode);
460 WARN("Not permitted Wi-Fi on");
464 err = wifi_power_driver_and_supplicant(TRUE);
465 if (err < 0 && err != -EALREADY)
468 err = _set_connman_technology_power(TRUE);
470 if (device_picker_test == TRUE)
471 netconfig_wifi_enable_device_picker_test();
476 static void __weconn_service_state_changed_cb(weconn_service_state_e state, void *user_data)
478 if (state == W_SERVICE_STATE_CONNECTED) {
479 DBG("SAP is connected");
480 if (wifi_state > VCONFKEY_WIFI_OFF)
482 } else if (state == W_SERVICE_STATE_DISCONNECTED) {
483 DBG("SAP is disconnected");
484 wifi_power_on_wearable(FALSE);
488 static int _weconn_set_state_changed_cb(int service_type, void *user_data)
493 weconn_destroy(weconn_handle);
494 weconn_handle = NULL;
497 ret = weconn_create(&weconn_handle);
499 ERR("Failed weconn_create(%d)", ret);
503 ret = weconn_set_service_state_change_cb(weconn_handle, __weconn_service_state_changed_cb, service_type, user_data);
505 ERR("Failed weconn_set_service_state_change_cb(%d)", ret);
512 static void __wearable_wifi_use_changed_cb(keynode_t* node, void* user_data)
516 gboolean wifi_restrict = FALSE;
518 if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
519 ERR("Fail to get VCONFKEY_WIFI_STATE");
524 wifi_use = vconf_keynode_get_int(node);
526 vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
530 if (wifi_state > VCONFKEY_WIFI_OFF) {
531 WARN("Wi-Fi is already turned on");
535 wifi_restrict = netconfig_is_wifi_allowed();
536 if (wifi_restrict == FALSE) {
537 DBG("launch wifi restrict popup");
538 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
539 wc_launch_syspopup(WC_POPUP_TYPE_WIFI_RESTRICT);
541 wifi_power_on_wearable(TRUE);
544 ERR("## wifi use [OFF]");
545 if (wifi_state == VCONFKEY_WIFI_OFF) {
546 WARN("Wi-Fi is already turned off");
554 #if defined TIZEN_TELEPHONY_ENABLE
555 static void __netconfig_wifi_wearable_airplane_mode(keynode_t *node,
558 int wifi_use = 0, airplane_state = 0;
559 int wifi_use_off_by_airplane = 0;
561 vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE,
562 &wifi_use_off_by_airplane);
564 vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
567 airplane_state = vconf_keynode_get_bool(node);
569 vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
571 DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
572 DBG("Wi-Fi use %d, Wi-Fi was off by flight mode %s", wifi_use,
573 wifi_use_off_by_airplane ? "Yes" : "No");
575 if (airplane_mode == airplane_state)
578 airplane_mode = airplane_state;
580 if (airplane_state > 0) {
581 /* airplane mode on */
585 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
586 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
589 /* airplane mode off */
590 if (!wifi_use_off_by_airplane)
593 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
594 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 1);
599 #if defined TIZEN_TELEPHONY_ENABLE
600 static void __netconfig_wifi_airplane_mode(keynode_t *node, void *user_data)
602 int wifi_state = 0, airplane_state = 0;
603 int wifi_off_by_airplane = 0;
605 vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
607 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
610 airplane_state = vconf_keynode_get_bool(node);
612 vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
614 DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
615 DBG("Wi-Fi state %d, Wi-Fi was off by flight mode %s", wifi_state,
616 wifi_off_by_airplane ? "Yes" : "No");
618 if (airplane_mode == airplane_state)
621 airplane_mode = airplane_state;
623 if (airplane_state > 0) {
624 /* airplane mode on */
625 if (wifi_state == VCONFKEY_WIFI_OFF)
630 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
632 /* airplane mode off */
633 if (!wifi_off_by_airplane)
636 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
638 if (wifi_state > VCONFKEY_WIFI_OFF)
646 static void __netconfig_wifi_restrict_mode(keynode_t *node, void *user_data)
648 int wifi_state = 0, restricted = 0;
649 int wifi_off_by_restricted = 0;
651 vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
653 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
656 restricted = vconf_keynode_get_bool(node);
658 vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted);
660 DBG("network restricted mode %s", restricted > 0 ? "ON" : "OFF");
661 DBG("Wi-Fi state %d, Wi-Fi was off by restricted mode %s", wifi_state,
662 wifi_off_by_restricted ? "Yes" : "No");
664 if (restricted > 0) {
665 /* network restricted on */
666 if (wifi_state == VCONFKEY_WIFI_OFF)
671 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 1);
673 /* network restricted off */
674 if (!wifi_off_by_restricted)
677 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 0);
679 if (wifi_state > VCONFKEY_WIFI_OFF)
687 static void __emergency_mode_changed_cb(keynode_t *node, void *user_data)
689 int wifi_state = 0, emergency = 0;
690 int wifi_off_by_emergency = 0;
691 #if !defined TIZEN_WEARABLE
692 int emergency_by_fmm = 0;
694 #if defined TIZEN_WEARABLE
698 vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
699 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
701 #if !defined TIZEN_WEARABLE
702 vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT, &emergency_by_fmm);
703 DBG("emergency mode by Find My Mobile (%d)", emergency_by_fmm);
704 if (emergency_by_fmm == 1)
709 emergency = vconf_keynode_get_int(node);
711 vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &emergency);
713 DBG("emergency mode %s", emergency > SETTING_PSMODE_POWERFUL ? "ON" : "OFF");
714 DBG("Wi-Fi state %d, Wi-Fi was off by emergency mode %s", wifi_state, wifi_off_by_emergency ? "Yes" : "No");
716 #if defined TIZEN_WEARABLE
717 if (emergency == SETTING_PSMODE_WEARABLE) {
718 /* basic power saving mode on */
719 } else if (emergency == SETTING_PSMODE_WEARABLE_ENHANCED) {
720 /* enhanced power saving mode on */
721 vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
722 psmode_wifi_use = wifi_use;
724 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
727 if (wifi_state == VCONFKEY_WIFI_OFF)
731 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
733 /* power saving mode off */
734 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, psmode_wifi_use);
735 if (!wifi_off_by_emergency)
738 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
739 if (wifi_state > VCONFKEY_WIFI_OFF)
742 wifi_power_on_wearable(TRUE);
745 if (emergency > SETTING_PSMODE_POWERFUL) {
746 /* emergency mode on */
747 if (wifi_state == VCONFKEY_WIFI_OFF)
752 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
754 /* emergency mode off */
755 if (!wifi_off_by_emergency)
758 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
760 if (wifi_state > VCONFKEY_WIFI_OFF)
769 static void __pm_state_changed_cb(keynode_t* node, void* user_data)
773 static int prev_state = VCONFKEY_PM_STATE_NORMAL;
775 if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
776 ERR("Fail to get VCONFKEY_WIFI_STATE");
781 * VCONFKEY_PM_STATE_NORMAL = 1,
782 * VCONFKEY_PM_STATE_LCDDIM,
783 * VCONFKEY_PM_STATE_LCDOFF,
784 * VCONFKEY_PM_STATE_SLEEP
787 new_state = vconf_keynode_get_int(node);
789 vconf_get_int(VCONFKEY_PM_STATE, &new_state);
791 DBG("wifi state: %d (0 off / 1 on / 2 connected)", wifi_state);
792 DBG("Old PM state: %d, current: %d", prev_state, new_state);
794 if ((new_state == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
795 netconfig_wifi_bgscan_stop();
796 netconfig_wifi_bgscan_start(TRUE);
799 prev_state = new_state;
802 #if defined TIZEN_TELEPHONY_ENABLE
803 static void _tapi_noti_sim_status_cb(TapiHandle *handle, const char *noti_id,
804 void *data, void *user_data)
806 TelSimCardStatus_t *status = data;
808 if (*status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
809 DBG("Turn Wi-Fi on automatically");
810 #if defined TIZEN_WEARABLE
811 wifi_power_on_wearable(TRUE);
815 netconfig_tel_deinit();
819 static gboolean netconfig_tapi_check_sim_state(void)
821 int ret, card_changed;
822 TelSimCardStatus_t status = TAPI_SIM_STATUS_UNKNOWN;
823 TapiHandle *tapi_handle = NULL;
825 tapi_handle = (TapiHandle *)netconfig_tel_init();
826 if (tapi_handle == NULL) {
827 ERR("Failed to tapi init");
831 ret = tel_get_sim_init_info(tapi_handle, &status, &card_changed);
832 if (ret != TAPI_API_SUCCESS) {
833 ERR("tel_get_sim_init_info() Failed : [%d]", ret);
834 netconfig_tel_deinit();
839 case TAPI_SIM_STATUS_UNKNOWN:
840 case TAPI_SIM_STATUS_CARD_ERROR:
841 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
842 case TAPI_SIM_STATUS_CARD_BLOCKED:
843 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
845 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
846 case TAPI_SIM_STATUS_SIM_INITIALIZING:
847 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
848 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
849 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
850 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
851 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
852 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
853 tel_register_noti_event(tapi_handle, TAPI_NOTI_SIM_STATUS,
854 _tapi_noti_sim_status_cb, NULL);
857 ERR("not defined status(%d)", status);
861 netconfig_tel_deinit();
866 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
868 int telephony_ready = 0;
871 telephony_ready = vconf_keynode_get_bool(node);
873 vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
875 if (telephony_ready != 0) {
876 if (netconfig_tapi_check_sim_state() == FALSE) {
877 DBG("Sim is not initialized yet.");
884 DBG("Turn Wi-Fi on automatically");
886 #if defined TIZEN_WEARABLE
887 wifi_power_on_wearable(TRUE);
893 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
897 int wifi_power_driver_and_supplicant(gboolean enable)
899 /* There are 3 thumb rules for Wi-Fi power management
900 * 1. Do not make exposed API to control wpa_supplicant and driver directly.
901 * It probably breaks ConnMan technology operation.
903 * 2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
904 * It breaks ConnMan technology operation.
906 * 3. Final the best rule: make it as simple as possible.
907 * Simple code enables easy maintenance and reduces logical errors.
909 if (enable == TRUE) {
910 return _load_driver_and_supplicant();
912 if (connman_wifi_technology_state == TRUE)
915 return _remove_driver_and_supplicant();
919 void wifi_power_disable_technology_state_by_only_connman_signal(void)
921 /* Important: it's only done by ConnMan technology signal update */
922 connman_wifi_technology_state = FALSE;
925 void wifi_power_recover_firmware(void)
927 wifi_firmware_recovery_mode = TRUE;
929 netconfig_wifi_bgscan_stop();
934 int wifi_power_on(void)
937 wifi_tech_state_e tech_state;
939 tech_state = wifi_state_get_technology_state();
940 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
943 if (__is_wifi_restricted() == TRUE)
946 if (netconfig_is_wifi_tethering_on() == TRUE) {
947 /* TODO: Wi-Fi tethering turns off here */
949 ERR("Failed to turn tethering off");
953 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
954 if (netconfig_is_wifi_direct_on() == TRUE) {
955 if (__netconfig_wifi_direct_power_off() == TRUE)
958 ERR("Failed to turn Wi-Fi direct off");
964 err = wifi_power_driver_and_supplicant(TRUE);
965 if (err < 0 && err != -EALREADY)
968 err = _set_connman_technology_power(TRUE);
973 int wifi_power_off(void)
977 err = _set_connman_technology_power(FALSE);
978 if (err == -EALREADY)
979 wifi_state_update_power_state(FALSE);
984 #if defined TIZEN_WEARABLE
985 int wifi_power_on_wearable(gboolean device_picker_test)
989 wifi_tech_state_e tech_state;
990 weconn_service_state_e weconn_state;
992 tech_state = wifi_state_get_technology_state();
993 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
996 err = weconn_get_service_state(weconn_handle, W_SERVICE_TYPE_BT, &weconn_state);
997 if (err == 0 && weconn_state == W_SERVICE_STATE_CONNECTED) {
998 WARN("Not permitted Wi-Fi on");
1002 if (vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
1003 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
1007 if (wifi_use == 0) {
1008 WARN("VCONF_WIFI_WEARABLE_WIFI_USE is OFF");
1012 err = wifi_power_driver_and_supplicant(TRUE);
1013 if (err < 0 && err != -EALREADY)
1016 err = _set_connman_technology_power(TRUE);
1018 if (device_picker_test == TRUE)
1019 netconfig_wifi_enable_device_picker_test();
1025 void wifi_power_initialize(void)
1027 int wifi_last_power_state = 0;
1029 /* Initialize Airplane mode */
1030 #if defined TIZEN_TELEPHONY_ENABLE
1031 vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
1033 DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
1035 /* Update the last Wi-Fi power state */
1036 vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
1037 if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
1038 #if defined TIZEN_TELEPHONY_ENABLE
1039 int telephony_ready = 0;
1040 vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
1041 if (telephony_ready == 0) {
1042 DBG("Telephony API is not initialized yet");
1043 vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
1044 __netconfig_telephony_ready_changed_cb, NULL);
1046 if (netconfig_tapi_check_sim_state() == FALSE)
1047 DBG("SIM is not initialized yet");
1050 DBG("Turn Wi-Fi on automatically");
1051 #if defined TIZEN_WEARABLE
1052 wifi_power_on_wearable(TRUE);
1058 #if defined TIZEN_WEARABLE
1059 _weconn_set_state_changed_cb(W_SERVICE_TYPE_BT, NULL);
1060 vconf_notify_key_changed(VCONF_WIFI_WEARABLE_WIFI_USE, __wearable_wifi_use_changed_cb, NULL);
1062 #if defined TIZEN_TELEPHONY_ENABLE
1063 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
1064 __netconfig_wifi_wearable_airplane_mode, NULL);
1067 vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
1068 __netconfig_wifi_restrict_mode, NULL);
1069 #if defined TIZEN_TELEPHONY_ENABLE
1070 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
1071 __netconfig_wifi_airplane_mode, NULL);
1075 vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
1076 vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
1079 void wifi_power_deinitialize(void)
1083 gboolean handle_load_driver(Wifi *wifi,
1084 GDBusMethodInvocation *context, gboolean device_picker_test)
1088 DBG("Wi-Fi power on requested");
1090 g_return_val_if_fail(wifi != NULL, FALSE);
1092 #if defined TIZEN_WEARABLE
1093 err = wifi_power_on_wearable(device_picker_test);
1095 err = wifi_power_on();
1097 if (device_picker_test == TRUE)
1098 netconfig_wifi_enable_device_picker_test();
1101 if (err == -EINPROGRESS)
1102 netconfig_error_inprogress(context);
1103 else if (err == -EALREADY)
1104 netconfig_error_already_exists(context);
1105 else if (err == -EPERM)
1106 netconfig_error_permission_denied(context);
1108 netconfig_error_wifi_driver_failed(context);
1114 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
1115 __netconfig_set_wifi_bssid();
1117 wifi_complete_load_driver(wifi, context);
1121 gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
1125 DBG("Wi-Fi power off requested");
1127 g_return_val_if_fail(wifi != NULL, FALSE);
1129 err = wifi_power_off();
1131 if (err == -EINPROGRESS)
1132 netconfig_error_inprogress(context);
1133 else if (err == -EALREADY)
1134 netconfig_error_already_exists(context);
1135 else if (err == -EPERM)
1136 netconfig_error_permission_denied(context);
1138 netconfig_error_wifi_driver_failed(context);
1142 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
1144 wifi_complete_remove_driver(wifi, context);
1148 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
1152 wifi_complete_load_p2p_driver(wifi, context);
1156 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
1160 wifi_complete_remove_p2p_driver(wifi, context);
1164 #if defined TIZEN_TV
1165 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
1167 DBG("Generate Random Mac address of ethernet");
1171 fp = fopen(OS_RANDOM_FILE, "rb");
1174 ERR("Could not open /dev/urandom");
1177 rc = fread(mac_buf, 1, mac_len, fp);
1181 return rc != mac_len ? -1 : 0;
1184 void __netconfig_set_ether_macaddr()
1187 DBG("Set wired Mac address ");
1188 char *mac_addr = NULL;
1191 mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
1192 if (mac_addr == NULL) {
1193 DBG("vconf_get_str Failed\n");
1196 /* Checking Invalid MAC Address */
1197 if ((strlen(mac_addr) == 0)) {
1198 ERR("Failed to get valid MAC Address from vconf");
1199 /*Generate the Random Mac address*/
1200 unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
1202 if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE == -1)) {
1204 ERR("Could not generate the Random Mac address");
1209 rand_mac_add[0] &= 0xFE; /*Clear multicase bit*/
1210 rand_mac_add[0] |= 0x02; /*set local assignment bit*/
1212 /*Set the Mac address in Vconf*/
1213 snprintf(mac_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
1214 rand_mac_add[0], rand_mac_add[1],
1215 rand_mac_add[2], rand_mac_add[3],
1216 rand_mac_add[4], rand_mac_add[5]);
1218 netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, mac_addr);
1221 DBG("MAC Address of eth0 [%s]",mac_addr);
1222 const char *path = NET_EXEC_PATH;
1223 char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
1224 "ether",mac_addr, "up", NULL};
1225 char *const envs[] = { NULL };
1226 rv = netconfig_execute_file(path, args, envs);
1229 ERR("Unable to execute system command");