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 #if defined TIZEN_WEARABLE
52 static weconn_h weconn_handle = NULL;
55 #define VCONF_WIFI_OFF_STATE_BY_AIRPLANE "file/private/wifi/wifi_off_by_airplane"
56 #define VCONF_WIFI_OFF_STATE_BY_RESTRICTED "file/private/wifi/wifi_off_by_restricted"
57 #define VCONF_WIFI_OFF_STATE_BY_EMERGENCY "file/private/wifi/wifi_off_by_emergency"
58 #if defined TIZEN_WEARABLE
59 #define VCONF_WIFI_WEARABLE_WIFI_USE "db/private/wifi/wearable_wifi_use"
61 #if !defined TIZEN_WEARABLE
62 #define VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT "db/setting/network_with_lcd_off_limit"
65 #define WLAN_MAC_INFO tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
66 #define WLAN_MAC_ADDR_MAX 20
67 #define VCONF_WIFI_BSSID_ADDRESS "db/wifi/bssid_address"
70 #define ETH_MAC_ADDR_SIZE 6
71 #define VCONF_ETH_MAC_ADDRESS "db/dnet/mac_address"
72 #define NET_EXEC_PATH "/sbin/ifconfig"
73 #define OS_RANDOM_FILE "/dev/urandom"
76 static gboolean connman_wifi_technology_state = FALSE;
77 static gboolean wifi_firmware_recovery_mode = FALSE;
78 static int airplane_mode = 0;
80 #if defined TIZEN_WEARABLE
81 static int psmode_wifi_use = 1;
84 static gboolean __is_wifi_restricted(void)
86 #if defined TIZEN_WEARABLE
89 int restricted_mode = 0;
91 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted_mode);
92 if (restricted_mode != 0) {
93 DBG("network restricted mode[%d]", restricted_mode);
100 static void __technology_reply(GObject *source_object, GAsyncResult *res, gpointer user_data)
103 GDBusConnection *conn = NULL;
104 GError *error = NULL;
106 conn = G_DBUS_CONNECTION(source_object);
107 reply = g_dbus_connection_call_finish(conn, res, &error);
111 if (g_strcmp0(error->message, CONNMAN_ERROR_INTERFACE ".AlreadyEnabled") == 0)
112 wifi_state_update_power_state(TRUE);
113 else if (g_strcmp0(error->message, CONNMAN_ERROR_INTERFACE ".AlreadyDisabled") == 0)
114 wifi_state_update_power_state(FALSE);
116 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 (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
445 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
450 if (netconfig_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 (netconfig_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 netconfig_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 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE,
562 &wifi_use_off_by_airplane);
564 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
567 airplane_state = vconf_keynode_get_bool(node);
569 netconfig_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 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
607 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
610 airplane_state = vconf_keynode_get_bool(node);
612 netconfig_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 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
653 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
656 restricted = vconf_keynode_get_bool(node);
658 netconfig_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 netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
699 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
701 #if !defined TIZEN_WEARABLE
702 netconfig_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 netconfig_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 netconfig_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);
726 if (wifi_state == VCONFKEY_WIFI_OFF)
730 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
732 /* power saving mode off */
733 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, psmode_wifi_use);
734 if (!wifi_off_by_emergency)
737 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
738 if (wifi_state > VCONFKEY_WIFI_OFF)
741 wifi_power_on_wearable(TRUE);
744 if (emergency > SETTING_PSMODE_POWERFUL) {
745 /* emergency mode on */
746 if (wifi_state == VCONFKEY_WIFI_OFF)
751 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
753 /* emergency mode off */
754 if (!wifi_off_by_emergency)
757 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
759 if (wifi_state > VCONFKEY_WIFI_OFF)
768 static void __pm_state_changed_cb(keynode_t* node, void* user_data)
772 static int prev_state = VCONFKEY_PM_STATE_NORMAL;
774 if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
775 ERR("Fail to get VCONFKEY_WIFI_STATE");
780 * VCONFKEY_PM_STATE_NORMAL = 1,
781 * VCONFKEY_PM_STATE_LCDDIM,
782 * VCONFKEY_PM_STATE_LCDOFF,
783 * VCONFKEY_PM_STATE_SLEEP
786 new_state = vconf_keynode_get_int(node);
788 netconfig_vconf_get_int(VCONFKEY_PM_STATE, &new_state);
790 DBG("wifi state: %d (0 off / 1 on / 2 connected)", wifi_state);
791 DBG("Old PM state: %d, current: %d", prev_state, new_state);
793 if ((new_state == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
794 netconfig_wifi_bgscan_stop();
795 netconfig_wifi_bgscan_start(TRUE);
798 prev_state = new_state;
801 #if defined TIZEN_TELEPHONY_ENABLE
802 static void _tapi_noti_sim_status_cb(TapiHandle *handle, const char *noti_id,
803 void *data, void *user_data)
805 TelSimCardStatus_t *status = data;
807 if (*status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
808 DBG("Turn Wi-Fi on automatically");
809 #if defined TIZEN_WEARABLE
810 wifi_power_on_wearable(TRUE);
814 netconfig_tel_deinit();
818 static gboolean netconfig_tapi_check_sim_state(void)
820 int ret, card_changed;
821 TelSimCardStatus_t status = TAPI_SIM_STATUS_UNKNOWN;
822 TapiHandle *tapi_handle = NULL;
824 tapi_handle = (TapiHandle *)netconfig_tel_init();
825 if (tapi_handle == NULL) {
826 ERR("Failed to tapi init");
830 ret = tel_get_sim_init_info(tapi_handle, &status, &card_changed);
831 if (ret != TAPI_API_SUCCESS) {
832 ERR("tel_get_sim_init_info() Failed : [%d]", ret);
833 netconfig_tel_deinit();
838 case TAPI_SIM_STATUS_UNKNOWN:
839 case TAPI_SIM_STATUS_CARD_ERROR:
840 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
841 case TAPI_SIM_STATUS_CARD_BLOCKED:
842 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
844 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
845 case TAPI_SIM_STATUS_SIM_INITIALIZING:
846 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
847 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
848 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
849 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
850 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
851 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
852 tel_register_noti_event(tapi_handle, TAPI_NOTI_SIM_STATUS,
853 _tapi_noti_sim_status_cb, NULL);
856 ERR("not defined status(%d)", status);
860 netconfig_tel_deinit();
865 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
867 int telephony_ready = 0;
870 telephony_ready = vconf_keynode_get_bool(node);
872 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
874 if (telephony_ready != 0) {
875 if (netconfig_tapi_check_sim_state() == FALSE) {
876 DBG("Sim is not initialized yet.");
883 DBG("Turn Wi-Fi on automatically");
885 #if defined TIZEN_WEARABLE
886 wifi_power_on_wearable(TRUE);
892 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
896 int wifi_power_driver_and_supplicant(gboolean enable)
898 /* There are 3 thumb rules for Wi-Fi power management
899 * 1. Do not make exposed API to control wpa_supplicant and driver directly.
900 * It probably breaks ConnMan technology operation.
902 * 2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
903 * It breaks ConnMan technology operation.
905 * 3. Final the best rule: make it as simple as possible.
906 * Simple code enables easy maintenance and reduces logical errors.
908 if (enable == TRUE) {
909 return _load_driver_and_supplicant();
911 if (connman_wifi_technology_state == TRUE)
914 return _remove_driver_and_supplicant();
918 void wifi_power_disable_technology_state_by_only_connman_signal(void)
920 /* Important: it's only done by ConnMan technology signal update */
921 connman_wifi_technology_state = FALSE;
924 void wifi_power_recover_firmware(void)
926 wifi_firmware_recovery_mode = TRUE;
928 netconfig_wifi_bgscan_stop();
933 int wifi_power_on(void)
936 wifi_tech_state_e tech_state;
938 tech_state = wifi_state_get_technology_state();
939 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
942 if (__is_wifi_restricted() == TRUE)
945 if (netconfig_is_wifi_tethering_on() == TRUE) {
946 /* TODO: Wi-Fi tethering turns off here */
948 ERR("Failed to turn tethering off");
952 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
953 if (netconfig_is_wifi_direct_on() == TRUE) {
954 if (__netconfig_wifi_direct_power_off() == TRUE)
957 ERR("Failed to turn Wi-Fi direct off");
963 err = wifi_power_driver_and_supplicant(TRUE);
964 if (err < 0 && err != -EALREADY)
967 err = _set_connman_technology_power(TRUE);
972 int wifi_power_off(void)
976 err = _set_connman_technology_power(FALSE);
977 if (err == -EALREADY)
978 wifi_state_update_power_state(FALSE);
983 #if defined TIZEN_WEARABLE
984 int wifi_power_on_wearable(gboolean device_picker_test)
988 wifi_tech_state_e tech_state;
989 weconn_service_state_e weconn_state;
991 tech_state = wifi_state_get_technology_state();
992 if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
995 err = weconn_get_service_state(weconn_handle, W_SERVICE_TYPE_BT, &weconn_state);
996 if (err == 0 && weconn_state == W_SERVICE_STATE_CONNECTED) {
997 WARN("Not permitted Wi-Fi on");
1001 if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
1002 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
1006 if (wifi_use == 0) {
1007 WARN("VCONF_WIFI_WEARABLE_WIFI_USE is OFF");
1011 err = wifi_power_driver_and_supplicant(TRUE);
1012 if (err < 0 && err != -EALREADY)
1015 err = _set_connman_technology_power(TRUE);
1017 if (device_picker_test == TRUE)
1018 netconfig_wifi_enable_device_picker_test();
1024 void wifi_power_initialize(void)
1026 int wifi_last_power_state = 0;
1028 /* Initialize Airplane mode */
1029 #if defined TIZEN_TELEPHONY_ENABLE
1030 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
1032 DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
1034 /* Update the last Wi-Fi power state */
1035 netconfig_vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
1036 if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
1037 #if defined TIZEN_TELEPHONY_ENABLE
1038 int telephony_ready = 0;
1039 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
1040 if (telephony_ready == 0) {
1041 DBG("Telephony API is not initialized yet");
1042 vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
1043 __netconfig_telephony_ready_changed_cb, NULL);
1045 if (netconfig_tapi_check_sim_state() == FALSE)
1046 DBG("SIM is not initialized yet");
1049 DBG("Turn Wi-Fi on automatically");
1050 #if defined TIZEN_WEARABLE
1051 wifi_power_on_wearable(TRUE);
1057 #if defined TIZEN_WEARABLE
1058 _weconn_set_state_changed_cb(W_SERVICE_TYPE_BT, NULL);
1059 vconf_notify_key_changed(VCONF_WIFI_WEARABLE_WIFI_USE, __wearable_wifi_use_changed_cb, NULL);
1061 #if defined TIZEN_TELEPHONY_ENABLE
1062 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
1063 __netconfig_wifi_wearable_airplane_mode, NULL);
1066 vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
1067 __netconfig_wifi_restrict_mode, NULL);
1068 #if defined TIZEN_TELEPHONY_ENABLE
1069 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
1070 __netconfig_wifi_airplane_mode, NULL);
1074 vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
1075 vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
1078 void wifi_power_deinitialize(void)
1082 gboolean handle_load_driver(Wifi *wifi,
1083 GDBusMethodInvocation *context, gboolean device_picker_test)
1087 DBG("Wi-Fi power on requested");
1089 g_return_val_if_fail(wifi != NULL, FALSE);
1091 if (!netconfig_dpm_update_from_wifi()) {
1092 DBG("DPM policy restricts Wi-Fi");
1093 netconfig_error_permission_denied(context);
1097 #if defined TIZEN_WEARABLE
1098 err = wifi_power_on_wearable(device_picker_test);
1100 err = wifi_power_on();
1102 if (device_picker_test == TRUE)
1103 netconfig_wifi_enable_device_picker_test();
1106 if (err == -EINPROGRESS)
1107 netconfig_error_inprogress(context);
1108 else if (err == -EALREADY)
1109 netconfig_error_already_exists(context);
1110 else if (err == -EPERM)
1111 netconfig_error_permission_denied(context);
1113 netconfig_error_wifi_driver_failed(context);
1119 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
1120 __netconfig_set_wifi_bssid();
1122 wifi_complete_load_driver(wifi, context);
1126 gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
1130 DBG("Wi-Fi power off requested");
1132 g_return_val_if_fail(wifi != NULL, FALSE);
1134 err = wifi_power_off();
1136 if (err == -EINPROGRESS)
1137 netconfig_error_inprogress(context);
1138 else if (err == -EALREADY)
1139 netconfig_error_already_exists(context);
1140 else if (err == -EPERM)
1141 netconfig_error_permission_denied(context);
1143 netconfig_error_wifi_driver_failed(context);
1147 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
1149 wifi_complete_remove_driver(wifi, context);
1153 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
1157 wifi_complete_load_p2p_driver(wifi, context);
1161 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
1165 wifi_complete_remove_p2p_driver(wifi, context);
1169 #if defined TIZEN_TV
1170 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
1172 DBG("Generate Random Mac address of ethernet");
1176 fp = fopen(OS_RANDOM_FILE, "rb");
1179 ERR("Could not open /dev/urandom");
1182 rc = fread(mac_buf, 1, mac_len, fp);
1186 return rc != mac_len ? -1 : 0;
1189 void __netconfig_set_ether_macaddr()
1192 DBG("Set wired Mac address ");
1193 char *mac_addr = NULL;
1196 mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
1197 if (mac_addr == NULL) {
1198 DBG("vconf_get_str Failed\n");
1201 /* Checking Invalid MAC Address */
1202 if ((strlen(mac_addr) == 0)) {
1203 ERR("Failed to get valid MAC Address from vconf");
1204 /*Generate the Random Mac address*/
1205 unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
1207 if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE == -1)) {
1209 ERR("Could not generate the Random Mac address");
1214 rand_mac_add[0] &= 0xFE; /*Clear multicase bit*/
1215 rand_mac_add[0] |= 0x02; /*set local assignment bit*/
1217 /*Set the Mac address in Vconf*/
1218 snprintf(mac_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
1219 rand_mac_add[0], rand_mac_add[1],
1220 rand_mac_add[2], rand_mac_add[3],
1221 rand_mac_add[4], rand_mac_add[5]);
1223 netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, mac_addr);
1226 DBG("MAC Address of eth0 [%s]", mac_addr);
1227 const char *path = NET_EXEC_PATH;
1228 char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
1229 "ether", mac_addr, "up", NULL};
1230 char *const envs[] = { NULL };
1231 rv = netconfig_execute_file(path, args, envs);
1234 ERR("Unable to execute system command");