Remove Telephony Dependency for IoT_Core/Headless devices
[platform/core/connectivity/net-config.git] / src / wifi-power.c
index 1d2309d..72b8673 100755 (executable)
 #include <errno.h>
 #include <vconf.h>
 #include <vconf-keys.h>
-#include <ITapiSim.h>
-#include <TapiUtility.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <glib.h>
+#include <tzplatform_config.h>
 
 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
 #include <wifi-direct.h>
 #endif
 
-#if defined TIZEN_WEARABLE
-#include <weconn.h>
-#endif
-
 #include "log.h"
 #include "util.h"
 #include "netdbus.h"
 #include "wifi-wps.h"
 #include "wifi-power.h"
 #include "wifi-state.h"
-#include "wifi-tel-intf.h"
 #include "netsupplicant.h"
 #include "network-state.h"
+#include "network-dpm.h"
 #include "wifi-firmware.h"
 #include "wifi-background-scan.h"
 
-#define WLAN_SUPPLICANT_SCRIPT         "/usr/sbin/wpa_supp.sh"
-#define P2P_SUPPLICANT_SCRIPT          "/usr/sbin/p2p_supp.sh"
-#define VCONF_WIFI_OFF_STATE_BY_AIRPLANE \
-                       "file/private/wifi/wifi_off_by_airplane"
-#define VCONF_WIFI_OFF_STATE_BY_RESTRICTED \
-                       "file/private/wifi/wifi_off_by_restricted"
-#define VCONF_WIFI_OFF_STATE_BY_EMERGENCY \
-                       "file/private/wifi/wifi_off_by_emergency"
 
+#define WLAN_SUPPLICANT_SCRIPT         "/usr/bin/wpa_supp.sh"
+#define P2P_SUPPLICANT_SCRIPT          "/usr/bin/p2p_supp.sh"
+
+#define VCONF_WIFI_OFF_STATE_BY_AIRPLANE       "file/private/wifi/wifi_off_by_airplane"
+#define VCONF_WIFI_OFF_STATE_BY_RESTRICTED     "file/private/wifi/wifi_off_by_restricted"
+#define VCONF_WIFI_OFF_STATE_BY_EMERGENCY      "file/private/wifi/wifi_off_by_emergency"
 #if defined TIZEN_WEARABLE
-static weconn_h weconn_handle = NULL;
-#define VCONF_WIFI_WEARABLE_WIFI_USE   "db/private/wifi/wearable_wifi_use"
+#define VCONF_WIFI_WEARABLE_WIFI_USE                   "db/private/wifi/wearable_wifi_use"
+#endif
+#if !defined TIZEN_WEARABLE
+#define VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT     "db/setting/network_with_lcd_off_limit"
 #endif
 
-#define WLAN_MAC_INFO              "/opt/etc/.mac.info"
+#define WLAN_MAC_INFO              tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
+#define WLAN_MAC_ADDRESS_FILEPATH   "/sys/class/net/wlan0/address"
 #define WLAN_MAC_ADDR_MAX          20
-#define VCONF_WIFI_BSSID_ADDRESS   "db/wifi/bssid_address"
+#define VCONF_WIFI_BSSID_ADDRESS       "db/wifi/bssid_address"
 
-#if defined TIZEN_TV
 #define ETH_MAC_ADDR_SIZE 6
 #define VCONF_ETH_MAC_ADDRESS  "db/dnet/mac_address"
 #define NET_EXEC_PATH "/sbin/ifconfig"
 #define OS_RANDOM_FILE "/dev/urandom"
-#endif
 
 static gboolean connman_wifi_technology_state = FALSE;
 static gboolean wifi_firmware_recovery_mode = FALSE;
 static int airplane_mode = 0;
 
-static void __netconfig_wifi_technology_reply(GObject *source_object,
-               GAsyncResult *res, gpointer user_data)
-{
-       GVariant *reply;
-       GDBusConnection *conn = NULL;
-       GError *error = NULL;
-
-       conn = G_DBUS_CONNECTION (source_object);
-       reply = g_dbus_connection_call_finish(conn, res, &error);
-
-       if (reply == NULL) {
-               if (error != NULL) {
-                       if (g_strcmp0(error->message,
-                                       CONNMAN_ERROR_INTERFACE ".AlreadyEnabled") == 0) {
-                               netconfig_wifi_update_power_state(TRUE);
-                       } else if (g_strcmp0(error->message,
-                                       CONNMAN_ERROR_INTERFACE ".AlreadyDisabled") == 0) {
-                               netconfig_wifi_update_power_state(FALSE);
-                       } else {
-                               ERR("Fail to request status [%d: %s]",
-                                                               error->code, error->message);
-                       }
-                       g_error_free(error);
-               } else {
-                       ERR("Fail torequest status");
-               }
-       } else {
-               DBG("Successfully requested");
-       }
-
-       g_variant_unref(reply);
-       netconfig_gdbus_pending_call_unref();
-}
+#if defined TIZEN_WEARABLE
+static int psmode_wifi_use = 1;
+#endif
 
-static gboolean __netconfig_is_wifi_restricted(void)
+static gboolean __is_wifi_restricted(void)
 {
 #if defined TIZEN_WEARABLE
        return FALSE;
 #endif
        int restricted_mode = 0;
 
-       vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted_mode);
+       netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted_mode);
        if (restricted_mode != 0) {
                DBG("network restricted mode[%d]", restricted_mode);
                return TRUE;
@@ -124,53 +90,46 @@ static gboolean __netconfig_is_wifi_restricted(void)
        return FALSE;
 }
 
-static int __netconfig_wifi_connman_technology(gboolean enable)
+static void __technology_reply(GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
-       gboolean reply = FALSE;
-       GVariant *param0 = NULL;
-       GVariant *params = NULL;
-       char key[] = "Powered";
-       gboolean value_enable = TRUE;
-       gboolean value_disable = FALSE;
-
-       if (connman_wifi_technology_state == enable)
-               return -EALREADY;
-
-       if (enable == TRUE)
-               param0 = g_variant_new_boolean(value_enable);
-       else
-               param0 = g_variant_new_boolean(value_disable);
-
-       params = g_variant_new("(sv)",key, param0);
+       GVariant *reply;
+       GDBusConnection *conn = NULL;
+       GError *error = NULL;
 
-       reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
-                       CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
-                       "SetProperty", params, __netconfig_wifi_technology_reply);
+       conn = G_DBUS_CONNECTION(source_object);
+       reply = g_dbus_connection_call_finish(conn, res, &error);
 
-       if (reply != TRUE) {
-               ERR("Fail to set technology %s", enable == TRUE ? "enable" : "disable");
-               return -ESRCH;
+       if (reply == NULL) {
+               if (error != NULL) {
+                       if (g_strstr_len(error->message, strlen(error->message),
+                                       CONNMAN_ERROR_INTERFACE ".AlreadyEnabled") != NULL) {
+                               wifi_state_update_power_state(TRUE);
+                       } else if (g_strstr_len(error->message, strlen(error->message),
+                                       CONNMAN_ERROR_INTERFACE ".AlreadyDisabled") != NULL) {
+                               wifi_state_update_power_state(FALSE);
+                       } else {
+                               ERR("Fail to request status [%d: %s]", error->code, error->message);
+                               wifi_state_update_power_state(FALSE);
+                       }
+                       g_error_free(error);
+               } else {
+                       ERR("Fail to request status");
+                       wifi_state_update_power_state(FALSE);
+               }
+       } else {
+               DBG("Successfully requested");
        }
 
-       /* If Wi-Fi powered off,
-        * Do not remove Wi-Fi driver until ConnMan technology state updated
-        */
-       if (enable == TRUE)
-               connman_wifi_technology_state = enable;
-
-       /* To be keep safe, early disable Wi-Fi tech state */
-       if (enable != TRUE)
-               netconfig_wifi_state_set_technology_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
-
-       return 0;
+       g_variant_unref(reply);
+       netconfig_gdbus_pending_call_unref();
 }
 
-static int __netconfig_wifi_supplicant(gboolean enable)
+static int __execute_supplicant(gboolean enable)
 {
        int rv = 0;
        const char *path = WLAN_SUPPLICANT_SCRIPT;
-       char *const args_enable[] = { "/usr/sbin/wpa_supp.sh", "start", NULL };
-       char *const args_disable[] = { "/usr/sbin/wpa_supp.sh", "stop", NULL };
+       char *const args_enable[] = { "/usr/bin/wpa_supp.sh", "start", NULL };
+       char *const args_disable[] = { "/usr/bin/wpa_supp.sh", "stop", NULL };
        char *const envs[] = { NULL };
        static gboolean enabled = FALSE;
 
@@ -219,12 +178,11 @@ void netconfig_wifi_recover_firmware(void)
 
        netconfig_wifi_bgscan_stop();
 
-       netconfig_wifi_off();
+       wifi_power_off();
 }
 
 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
-static void __netconfig_wifi_direct_state_cb(int error_code,
-               wifi_direct_device_state_e device_state, void *user_data)
+static void __netconfig_wifi_direct_state_cb(int error_code, wifi_direct_device_state_e device_state, void *user_data)
 {
        int err;
 
@@ -232,12 +190,12 @@ static void __netconfig_wifi_direct_state_cb(int error_code,
        wifi_direct_deinitialize();
 
        if (device_state == WIFI_DIRECT_DEVICE_STATE_DEACTIVATED) {
-               err = netconfig_wifi_on();
+               err = wifi_power_on();
                if (err < 0) {
                        if (err == -EALREADY)
-                               netconfig_wifi_update_power_state(TRUE);
+                               wifi_state_update_power_state(TRUE);
                        else
-                               netconfig_wifi_notify_power_failed();
+                               wifi_state_emit_power_failed();
                }
        }
 }
@@ -249,8 +207,7 @@ static gboolean __netconfig_wifi_direct_power_off(void)
        if (wifi_direct_initialize() < 0)
                return FALSE;
 
-       if (wifi_direct_set_device_state_changed_cb(
-                       __netconfig_wifi_direct_state_cb, NULL) < 0)
+       if (wifi_direct_set_device_state_changed_cb(__netconfig_wifi_direct_state_cb, NULL) < 0)
                return FALSE;
 
        if (wifi_direct_deactivate() < 0)
@@ -260,34 +217,35 @@ static gboolean __netconfig_wifi_direct_power_off(void)
 }
 #endif
 
-static int __netconfig_wifi_try_to_load_driver_and_supplicant(void)
+static int _load_driver_and_supplicant(void)
 {
        int err = 0;
-       enum netconfig_wifi_tech_state wifi_tech_state;
+       wifi_tech_state_e tech_state;
 
-       wifi_tech_state = netconfig_wifi_state_get_technology_state();
-       if (wifi_tech_state > NETCONFIG_WIFI_TECH_OFF)
+       tech_state = wifi_state_get_technology_state();
+       if (tech_state > NETCONFIG_WIFI_TECH_OFF)
                return -EALREADY;
 
-       err = __netconfig_wifi_supplicant(TRUE);
+       err = __execute_supplicant(TRUE);
        if (err < 0 && err != -EALREADY)
                return err;
 
        err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, TRUE);
        if (err < 0 && err != -EALREADY) {
-               __netconfig_wifi_supplicant(FALSE);
+               __execute_supplicant(FALSE);
                return err;
        }
 
-       netconfig_wifi_state_set_technology_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
+       wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
 
        return 0;
 }
 
-static int __netconfig_wifi_try_to_remove_driver_and_supplicant(void)
+static int _remove_driver_and_supplicant(void)
 {
        int err = 0;
 
+       INFO("remove driver and supplicant");
        if (wifi_firmware_recovery_mode != TRUE &&
                                        netconfig_wifi_is_wps_enabled() == TRUE) {
                DBG("Wi-Fi WPS mode");
@@ -298,14 +256,17 @@ static int __netconfig_wifi_try_to_remove_driver_and_supplicant(void)
        if (err < 0 && err != -EALREADY)
                return err;
 
-       err = __netconfig_wifi_supplicant(FALSE);
+       err = __execute_supplicant(FALSE);
        if (err < 0 && err != -EALREADY)
                return err;
 
-       netconfig_wifi_state_set_technology_state(NETCONFIG_WIFI_TECH_OFF);
+       wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_OFF);
+
+       // reset service state
+       wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
 
        if (wifi_firmware_recovery_mode == TRUE) {
-               if (netconfig_wifi_on() < 0)
+               if (wifi_power_on() < 0)
                        ERR("Failed to recover Wi-Fi firmware");
 
                wifi_firmware_recovery_mode = FALSE;
@@ -314,111 +275,78 @@ static int __netconfig_wifi_try_to_remove_driver_and_supplicant(void)
        return 0;
 }
 
-static void __netconfig_set_wifi_bssid(void)
+static int _set_connman_technology_power(gboolean enable)
 {
-       int rv = 0;
-       char bssid[WLAN_MAC_ADDR_MAX];
-
-       FILE *fp = fopen(WLAN_MAC_INFO, "r");
+       gboolean reply = FALSE;
+       GVariant *param0 = NULL;
+       GVariant *params = NULL;
+       char key[] = "Powered";
+       gboolean value_enable = TRUE;
+       gboolean value_disable = FALSE;
 
-       if (fp == NULL) {
-               ERR("Fail to open file");
-               return;
-       }
+       if (connman_wifi_technology_state == enable)
+               return -EALREADY;
 
-       fseek(fp, 0L, SEEK_SET);
-       rv = fscanf(fp, "%s", bssid);
+       if (enable == TRUE)
+               param0 = g_variant_new_boolean(value_enable);
+       else
+               param0 = g_variant_new_boolean(value_disable);
 
-       if (rv < 0)
-               ERR("Fail to read bssid");
+       params = g_variant_new("(sv)", key, param0);
 
-       netconfig_set_vconf_str(VCONF_WIFI_BSSID_ADDRESS, bssid);
+       reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
+                                       CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
+                                       "SetProperty", params, __technology_reply);
 
-       fclose(fp);
-}
+       if (reply != TRUE) {
+               ERR("Fail to set technology %s", enable == TRUE ? "enable" : "disable");
+               return -ESRCH;
+       }
 
-int netconfig_wifi_driver_and_supplicant(gboolean enable)
-{
-       /* There are 3 thumb rules for Wi-Fi power management
-        *   1. Do not make exposed API to control wpa_supplicant and driver directly.
-        *      It probably breaks ConnMan technology operation.
-        *
-        *   2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
-        *      It breaks ConnMan technology operation.
-        *
-        *   3. Final the best rule: make it as simple as possible.
-        *      Simple code enables easy maintenance and reduces logical errors.
+       /* If Wi-Fi powered off,
+        * Do not remove Wi-Fi driver until ConnMan technology state updated
         */
        if (enable == TRUE)
-               return __netconfig_wifi_try_to_load_driver_and_supplicant();
-       else {
-               if (connman_wifi_technology_state == TRUE)
-                       return -ENOSYS;
+               connman_wifi_technology_state = enable;
 
-               return __netconfig_wifi_try_to_remove_driver_and_supplicant();
-       }
-}
+       /* To be keep safe, early disable Wi-Fi tech state */
+       if (enable != TRUE)
+               wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
 
-void netconfig_wifi_disable_technology_state_by_only_connman_signal(void)
-{
-       /* Important: it's only done by ConnMan technology signal update */
-       connman_wifi_technology_state = FALSE;
+       return 0;
 }
 
-int netconfig_wifi_on(void)
+static void __netconfig_set_wifi_bssid(void)
 {
-       int err = 0;
-       enum netconfig_wifi_tech_state wifi_tech_state;
-
-       wifi_tech_state = netconfig_wifi_state_get_technology_state();
-       if (wifi_tech_state >= NETCONFIG_WIFI_TECH_POWERED)
-               return -EALREADY;
-
-       if (__netconfig_is_wifi_restricted() == TRUE)
-               return -EPERM;
+       int rv = 0;
+       char bssid[WLAN_MAC_ADDR_MAX];
 
-       if (netconfig_is_wifi_tethering_on() == TRUE) {
-               /* TODO: Wi-Fi tethering turns off here */
-               /* return TRUE; */
-               ERR("Failed to turn tethering off");
-               return -EBUSY;
-       }
+       FILE *fp = fopen(WLAN_MAC_INFO, "r");
 
-#if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
-       if (netconfig_is_wifi_direct_on() == TRUE) {
-               if (__netconfig_wifi_direct_power_off() == TRUE)
-                       return -EINPROGRESS;
-               else {
-                       ERR("Failed to turn Wi-Fi direct off");
-                       return -EBUSY;
+       if (fp == NULL) {
+               ERR("Fail to open %s", WLAN_MAC_INFO);
+               fp = fopen(WLAN_MAC_ADDRESS_FILEPATH, "r");
+               if (fp == NULL) {
+                       ERR("Fail to open %s", WLAN_MAC_ADDRESS_FILEPATH);
+                       return;
                }
        }
-#endif
 
-       err = netconfig_wifi_driver_and_supplicant(TRUE);
-       if (err < 0 && err != -EALREADY)
-               return err;
+       fseek(fp, 0L, SEEK_SET);
+       rv = fscanf(fp, "%17s", bssid);
 
-       err = __netconfig_wifi_connman_technology(TRUE);
+       if (rv < 0)
+               ERR("Fail to read bssid");
 
-       __netconfig_set_wifi_bssid();
+       netconfig_set_vconf_str(VCONF_WIFI_BSSID_ADDRESS, bssid);
 
-       return err;
+       fclose(fp);
 }
 
-int netconfig_wifi_off(void)
+void netconfig_wifi_disable_technology_state_by_only_connman_signal(void)
 {
-       int err;
-
-#if defined TIZEN_P2P_ENABLE && defined WLAN_CONCURRENT_MODE
-       __netconfig_p2p_supplicant(FALSE);
-#endif
-
-       err = __netconfig_wifi_connman_technology(FALSE);
-       if (err == -EALREADY)
-               netconfig_wifi_update_power_state(FALSE);
-
-       return 0;
+       /* Important: it's only done by ConnMan technology signal update */
+       connman_wifi_technology_state = FALSE;
 }
 
 #if defined TIZEN_WEARABLE
@@ -427,27 +355,14 @@ int netconfig_wifi_on_wearable(gboolean device_picker_test)
        int err = 0;
        int wifi_use;
        int ps_mode;
-       enum netconfig_wifi_tech_state wifi_tech_state;
-       weconn_service_state_e weconn_state;
-
-       wifi_tech_state = netconfig_wifi_state_get_technology_state();
-       if (wifi_tech_state >= NETCONFIG_WIFI_TECH_POWERED)
-               return -EALREADY;
-
-       err = weconn_get_service_state(weconn_handle, W_SERVICE_TYPE_BT,
-                       &weconn_state);
-       if (err == 0 && weconn_state == W_SERVICE_STATE_CONNECTED) {
-               WARN("Not permitted Wi-Fi on");
-               return -EPERM;
-       }
 
-       if (vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
+       if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
                ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
                return -EIO;
        }
 
        if (wifi_use > 0) {
-               if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_mode) < 0) {
+               if (netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_mode) < 0) {
                        ERR("Fail to get VCONFKEY_SETAPPL_PSMODE");
                        return -EIO;
                }
@@ -461,11 +376,11 @@ int netconfig_wifi_on_wearable(gboolean device_picker_test)
                return -EPERM;
        }
 
-       err = netconfig_wifi_driver_and_supplicant(TRUE);
+       err = wifi_power_driver_and_supplicant(TRUE);
        if (err < 0 && err != -EALREADY)
                return err;
 
-       err = __netconfig_wifi_connman_technology(TRUE);
+       err = _set_connman_technology_power(TRUE);
 
        if (device_picker_test == TRUE)
                netconfig_wifi_enable_device_picker_test();
@@ -473,61 +388,12 @@ int netconfig_wifi_on_wearable(gboolean device_picker_test)
        return err;
 }
 
-static void __netconfig_wifi_wearable_weconn_service_state_changed_cb(
-               weconn_service_state_e state, void *user_data)
+static void __wearable_wifi_use_changed_cb(keynode_t* node, void* user_data)
 {
        int wifi_state;
+       int wifi_use = 1;
 
-       if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
-               ERR("Fail to get VCONFKEY_WIFI_STATE");
-               return;
-       }
-
-       if (state == W_SERVICE_STATE_CONNECTED) {
-               DBG("SAP is connected");
-               if (wifi_state > VCONFKEY_WIFI_OFF)
-                       netconfig_wifi_off();
-       } else if (state == W_SERVICE_STATE_DISCONNECTED) {
-               DBG("SAP is disconnected");
-               netconfig_wifi_on_wearable(TRUE);
-       }
-}
-
-static int __netconfig_wifi_wearable_set_state_changed_cb(int service_type,
-               void *user_data)
-{
-       int ret;
-
-       if (weconn_handle) {
-               weconn_destroy(weconn_handle);
-               weconn_handle = NULL;
-       }
-
-       ret = weconn_create(&weconn_handle);
-       if (ret < 0) {
-               ERR("Failed weconn_create(%d)", ret);
-               return -1;
-       }
-
-       ret = weconn_set_service_state_change_cb(weconn_handle,
-                       __netconfig_wifi_wearable_weconn_service_state_changed_cb,
-                       service_type, user_data);
-       if (ret < 0) {
-               ERR("Failed weconn_set_service_state_change_cb(%d)", ret);
-               return -1;
-       }
-
-       return 0;
-}
-
-static void __netconfig_wifi_wearable_wifi_use_mode(keynode_t* node,
-               void* user_data)
-{
-       int wifi_state;
-       int wifi_use;
-       gboolean wifi_restrict = FALSE;
-
-       if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
+       if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
                ERR("Fail to get VCONFKEY_WIFI_STATE");
                return;
        }
@@ -535,7 +401,7 @@ static void __netconfig_wifi_wearable_wifi_use_mode(keynode_t* node,
        if (node != NULL)
                wifi_use = vconf_keynode_get_int(node);
        else
-               vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
+               netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
 
        if (wifi_use > 0) {
                DBG("wifi use on");
@@ -543,88 +409,79 @@ static void __netconfig_wifi_wearable_wifi_use_mode(keynode_t* node,
                        WARN("Wi-Fi is already turned on");
                        return;
                }
-
-               wifi_restrict = netconfig_is_wifi_allowed();
-               if (wifi_restrict == FALSE) {
-                       DBG("launch wifi restrict popup");
-                       netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
-                       wc_launch_syspopup(WC_POPUP_TYPE_WIFI_RESTRICT);
-               } else {
-                       netconfig_wifi_on_wearable(TRUE);
-               }
+               wifi_power_on_wearable(TRUE);
        } else {
-               DBG("wifi use off");
+               ERR("## wifi use [OFF]");
                if (wifi_state == VCONFKEY_WIFI_OFF) {
                        WARN("Wi-Fi is already turned off");
                        return;
                }
 
-               netconfig_wifi_off();
+               wifi_power_off();
        }
 }
-
-#if defined TIZEN_TELEPHONY_ENABLE
-static void __netconfig_wifi_wearable_airplane_mode(keynode_t *node,
-               void *user_data)
+#else
+static void __netconfig_wifi_restrict_mode(keynode_t *node, void *user_data)
 {
-       int wifi_use = 0, airplane_state = 0;
-       int wifi_use_off_by_airplane = 0;
+       int wifi_state = 0, restricted = 0;
+       int wifi_off_by_restricted = 0;
 
-       vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE,
-                       &wifi_use_off_by_airplane);
+       netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
 
-       vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
+       netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
 
        if (node != NULL)
-               airplane_state = vconf_keynode_get_bool(node);
+               restricted = vconf_keynode_get_bool(node);
        else
-               vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
+               netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted);
 
-       DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
-       DBG("Wi-Fi use %d, Wi-Fi was off by flight mode %s", wifi_use,
-                       wifi_use_off_by_airplane ? "Yes" : "No");
+       DBG("network restricted mode %s", restricted > 0 ? "ON" : "OFF");
+       DBG("Wi-Fi state %d, Wi-Fi was off by restricted mode %s", wifi_state,
+                       wifi_off_by_restricted ? "Yes" : "No");
 
-       if (airplane_mode == airplane_state)
-               return ;
+       if (restricted > 0) {
+               /* network restricted on */
+               if (wifi_state == VCONFKEY_WIFI_OFF)
+                       return;
 
-       airplane_mode = airplane_state;
+               wifi_power_off();
 
-       if (airplane_state > 0) {
-               /* airplane mode on */
-               if (wifi_use == 0)
+               netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 1);
+       } else {
+               /* network restricted off */
+               if (!wifi_off_by_restricted)
                        return;
 
-               netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
-               netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
+               netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 0);
 
-       } else {
-               /* airplane mode off */
-               if (!wifi_use_off_by_airplane)
+               if (wifi_state > VCONFKEY_WIFI_OFF)
                        return;
 
-               netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
-               netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 1);
+               wifi_power_on();
        }
 }
 #endif
-#else
-#if defined TIZEN_TELEPHONY_ENABLE
+
 static void __netconfig_wifi_airplane_mode(keynode_t *node, void *user_data)
 {
        int wifi_state = 0, airplane_state = 0;
        int wifi_off_by_airplane = 0;
 
-       vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
+       netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
 
-       vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
+#if defined TIZEN_WEARABLE
+       netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_state)
+#else
+       netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
+#endif
 
        if (node != NULL)
                airplane_state = vconf_keynode_get_bool(node);
        else
-               vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
+               netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
 
        DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
-       DBG("Wi-Fi state %d, Wi-Fi was off by flight mode %s", wifi_state,
+       DBG("Wi-Fi state(or use) %d, Wi-Fi was off by flight mode %s", wifi_state,
                        wifi_off_by_airplane ? "Yes" : "No");
 
        if (airplane_mode == airplane_state)
@@ -637,81 +494,45 @@ static void __netconfig_wifi_airplane_mode(keynode_t *node, void *user_data)
                if (wifi_state == VCONFKEY_WIFI_OFF)
                        return;
 
-               netconfig_wifi_off();
+               wifi_power_off();
 
                netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
+#if defined TIZEN_WEARABLE
+               netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
+#endif
        } else {
                /* airplane mode off */
                if (!wifi_off_by_airplane)
                        return;
 
                netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
-
+#if defined TIZEN_WEARABLE
+               netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 1);
+#else
                if (wifi_state > VCONFKEY_WIFI_OFF)
                        return;
 
-               netconfig_wifi_on();
-       }
-}
+               wifi_power_on();
 #endif
-#endif
-
-#if !defined TIZEN_WEARABLE
-static void __netconfig_wifi_restrict_mode(keynode_t *node, void *user_data)
-{
-       int wifi_state = 0, restricted = 0;
-       int wifi_off_by_restricted = 0;
-
-       vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
-
-       vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
-
-       if (node != NULL)
-               restricted = vconf_keynode_get_bool(node);
-       else
-               vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted);
-
-       DBG("network restricted mode %s", restricted > 0 ? "ON" : "OFF");
-       DBG("Wi-Fi state %d, Wi-Fi was off by restricted mode %s", wifi_state,
-                       wifi_off_by_restricted ? "Yes" : "No");
-
-       if (restricted > 0) {
-               /* network restricted on */
-               if (wifi_state == VCONFKEY_WIFI_OFF)
-                       return;
-
-               netconfig_wifi_off();
-
-               netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 1);
-       } else {
-               /* network restricted off */
-               if (!wifi_off_by_restricted)
-                       return;
-
-               netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 0);
-
-               if (wifi_state > VCONFKEY_WIFI_OFF)
-                       return;
-
-               netconfig_wifi_on();
        }
 }
-#endif
 
-static void __netconfig_wifi_emergency_mode(keynode_t *node, void *user_data)
+static void __emergency_mode_changed_cb(keynode_t *node, void *user_data)
 {
        int wifi_state = 0, emergency = 0;
        int wifi_off_by_emergency = 0;
 #if !defined TIZEN_WEARABLE
        int emergency_by_fmm = 0;
 #endif
+#if defined TIZEN_WEARABLE
+       int wifi_use = 1;
+#endif
 
-       vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
-
-       vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
+       netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
+       netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
 
 #if !defined TIZEN_WEARABLE
-       vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT, &emergency_by_fmm);
+       netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT, &emergency_by_fmm);
        DBG("emergency mode by Find My Mobile (%d)", emergency_by_fmm);
        if (emergency_by_fmm == 1)
                return;
@@ -720,34 +541,37 @@ static void __netconfig_wifi_emergency_mode(keynode_t *node, void *user_data)
        if (node != NULL)
                emergency = vconf_keynode_get_int(node);
        else
-               vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &emergency);
+               netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &emergency);
 
        DBG("emergency mode %s", emergency > SETTING_PSMODE_POWERFUL ? "ON" : "OFF");
-       DBG("Wi-Fi state %d, Wi-Fi was off by emergency mode %s", wifi_state,
-                       wifi_off_by_emergency ? "Yes" : "No");
+       DBG("Wi-Fi state %d, Wi-Fi was off by emergency mode %s", wifi_state, wifi_off_by_emergency ? "Yes" : "No");
 
 #if defined TIZEN_WEARABLE
        if (emergency == SETTING_PSMODE_WEARABLE) {
                /* basic power saving mode on */
        } else if (emergency == SETTING_PSMODE_WEARABLE_ENHANCED) {
                /* enhanced power saving mode on */
+               netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
+               psmode_wifi_use = wifi_use;
+               if (wifi_use != 0)
+                       netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
+
                if (wifi_state == VCONFKEY_WIFI_OFF)
                        return;
 
-               netconfig_wifi_off();
-
+               wifi_power_off();
                netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
        } else {
                /* power saving mode off */
+               netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, psmode_wifi_use);
                if (!wifi_off_by_emergency)
                        return;
 
                netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
-
                if (wifi_state > VCONFKEY_WIFI_OFF)
                        return;
 
-               netconfig_wifi_on_wearable(TRUE);
+               wifi_power_on_wearable(TRUE);
        }
 #else
        if (emergency > SETTING_PSMODE_POWERFUL) {
@@ -755,7 +579,7 @@ static void __netconfig_wifi_emergency_mode(keynode_t *node, void *user_data)
                if (wifi_state == VCONFKEY_WIFI_OFF)
                        return;
 
-               netconfig_wifi_off();
+               wifi_power_off();
 
                netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
        } else {
@@ -768,19 +592,19 @@ static void __netconfig_wifi_emergency_mode(keynode_t *node, void *user_data)
                if (wifi_state > VCONFKEY_WIFI_OFF)
                        return;
 
-               netconfig_wifi_on();
+               wifi_power_on();
        }
 #endif
 
 }
 
-static void __netconfig_wifi_pm_state_mode(keynode_t* node, void* user_data)
+static void __pm_state_changed_cb(keynode_t* node, void* user_data)
 {
        int new_state = -1;
        int wifi_state = 0;
        static int prev_state = VCONFKEY_PM_STATE_NORMAL;
 
-       if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
+       if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
                ERR("Fail to get VCONFKEY_WIFI_STATE");
                return;
        }
@@ -794,13 +618,12 @@ static void __netconfig_wifi_pm_state_mode(keynode_t* node, void* user_data)
        if (node != NULL)
                new_state = vconf_keynode_get_int(node);
        else
-               vconf_get_int(VCONFKEY_PM_STATE, &new_state);
+               netconfig_vconf_get_int(VCONFKEY_PM_STATE, &new_state);
 
        DBG("wifi state: %d (0 off / 1 on / 2 connected)", wifi_state);
        DBG("Old PM state: %d, current: %d", prev_state, new_state);
 
-       if ((new_state == VCONFKEY_PM_STATE_NORMAL) &&
-                       (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
+       if ((new_state == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
                netconfig_wifi_bgscan_stop();
                netconfig_wifi_bgscan_start(TRUE);
        }
@@ -808,167 +631,220 @@ static void __netconfig_wifi_pm_state_mode(keynode_t* node, void* user_data)
        prev_state = new_state;
 }
 
-#if defined TIZEN_TELEPHONY_ENABLE
-static void _tapi_noti_sim_status_cb(TapiHandle *handle, const char *noti_id,
-                                                                               void *data, void *user_data)
+static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
 {
-       TelSimCardStatus_t *status = data;
+       int telephony_ready = 0;
+
+       if (node != NULL)
+               telephony_ready = vconf_keynode_get_bool(node);
+       else
+               netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
+
+       if (telephony_ready != 0) {
+               if (netconfig_tapi_check_sim_state() == FALSE) {
+                       DBG("Sim is not initialized yet.");
+
+                       goto done;
+               }
+       } else
+               return;
+
+       DBG("Turn Wi-Fi on automatically");
 
-       if (*status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
-               DBG("Turn Wi-Fi on automatically");
 #if defined TIZEN_WEARABLE
-               netconfig_wifi_on_wearable(TRUE);
+       wifi_power_on_wearable(TRUE);
 #else
-               netconfig_wifi_on();
+       wifi_power_on();
 #endif
-               netconfig_tel_deinit();
+
+done:
+       vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
+}
+
+int wifi_power_driver_and_supplicant(gboolean enable)
+{
+       /* There are 3 thumb rules for Wi-Fi power management
+        *   1. Do not make exposed API to control wpa_supplicant and driver directly.
+        *      It probably breaks ConnMan technology operation.
+        *
+        *   2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
+        *      It breaks ConnMan technology operation.
+        *
+        *   3. Final the best rule: make it as simple as possible.
+        *      Simple code enables easy maintenance and reduces logical errors.
+        */
+       if (enable == TRUE) {
+               return _load_driver_and_supplicant();
+       } else {
+               if (connman_wifi_technology_state == TRUE)
+                       return -ENOSYS;
+
+               return _remove_driver_and_supplicant();
        }
 }
 
-static gboolean netconfig_tapi_check_sim_state(void)
+void wifi_power_disable_technology_state_by_only_connman_signal(void)
 {
-       int ret, card_changed;
-       TelSimCardStatus_t status;
-       TapiHandle *tapi_handle = NULL;
+       /* Important: it's only done by ConnMan technology signal update */
+       connman_wifi_technology_state = FALSE;
+}
 
-       tapi_handle = (TapiHandle *)netconfig_tel_init();
+void wifi_power_recover_firmware(void)
+{
+       wifi_firmware_recovery_mode = TRUE;
 
-       ret = tel_get_sim_init_info(tapi_handle, &status, &card_changed);
-       if (ret != TAPI_API_SUCCESS) {
-               ERR("tel_get_sim_init_info() Failed");
-               netconfig_tel_deinit();
-               return FALSE;
+       netconfig_wifi_bgscan_stop();
+
+       wifi_power_off();
+}
+
+int wifi_power_on(void)
+{
+       int err = 0;
+       wifi_tech_state_e tech_state;
+
+       tech_state = wifi_state_get_technology_state();
+       if (tech_state >= NETCONFIG_WIFI_TECH_POWERED) {
+               /* There can be a scenario where wifi is automatically *
+                * activated by connman if wifi was powered in last boot. *
+                * So we should update connman_wifi_technology_state variable *
+                * if it is found that wifi_tech_state variable is *
+                * NETCONFIG_WIFI_TECH_POWERED and connman_wifi_technology_state *
+                * variable is FALSE. Earlier connman_wifi_technology_state *
+                * variable was only updated when wifi was Powered on from *
+                * net-config resulting in variable not getting updated. *
+                * This caused wifi to not get deactivated after reboot if *
+                * last power state was activated */
+               ERR("Net-Config WiFi connman technology state %d",
+                               connman_wifi_technology_state);
+               if (connman_wifi_technology_state == FALSE)
+                       connman_wifi_technology_state = TRUE;
+               return -EALREADY;
        }
 
-       switch (status) {
-       case TAPI_SIM_STATUS_UNKNOWN:
-       case TAPI_SIM_STATUS_CARD_ERROR:
-       case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
-       case TAPI_SIM_STATUS_CARD_BLOCKED:
-       case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
-               break;
-       case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
-       case TAPI_SIM_STATUS_SIM_INITIALIZING:
-       case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
-       case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
-       case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
-       case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
-       case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
-       case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
-               tel_register_noti_event(tapi_handle, TAPI_NOTI_SIM_STATUS,
-                               _tapi_noti_sim_status_cb, NULL);
-               return FALSE;
-       default:
-               ERR("not defined status(%d)", status);
-               break;
+       if (__is_wifi_restricted() == TRUE)
+               return -EPERM;
+
+       if (netconfig_is_wifi_tethering_on() == TRUE) {
+               /* TODO: Wi-Fi tethering turns off here */
+               /* return TRUE; */
+               ERR("Failed to turn tethering off");
+               return -EBUSY;
        }
 
-       netconfig_tel_deinit();
+#if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
+       if (netconfig_is_wifi_direct_on() == TRUE) {
+               if (__netconfig_wifi_direct_power_off() == TRUE)
+                       return -EINPROGRESS;
+               else {
+                       ERR("Failed to turn Wi-Fi direct off");
+                       return -EBUSY;
+               }
+       }
+#endif
 
-       return TRUE;
+       err = wifi_power_driver_and_supplicant(TRUE);
+       if (err < 0 && err != -EALREADY)
+               return err;
+
+       err = _set_connman_technology_power(TRUE);
+
+       return err;
 }
 
-static void __netconfig_tapi_state_changed_cb(keynode_t * node, void *data)
+int wifi_power_off(void)
 {
-       int tapi_state = 0;
+       int err;
 
-       if (node != NULL)
-               tapi_state = vconf_keynode_get_int(node);
-       else
-               vconf_get_bool(VCONFKEY_TELEPHONY_READY, &tapi_state);
+       err = _set_connman_technology_power(FALSE);
+       if (err == -EALREADY)
+               wifi_state_update_power_state(FALSE);
 
-       if (tapi_state != FALSE) {
-               if (netconfig_tapi_check_sim_state() == FALSE) {
-                       DBG("Sim is not initialized yet.");
+       return 0;
+}
 
-                       goto done;
-               }
-       } else
-               return;
+#if defined TIZEN_WEARABLE
+int wifi_power_on_wearable(gboolean device_picker_test)
+{
+       int err = 0;
+       int wifi_use = 1;
+       wifi_tech_state_e tech_state;
 
-       DBG("Turn Wi-Fi on automatically");
+       tech_state = wifi_state_get_technology_state();
+       if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
+               return -EALREADY;
 
-#if defined TIZEN_WEARABLE
-       netconfig_wifi_on_wearable(TRUE);
-#else
-       netconfig_wifi_on();
-#endif
+       if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
+               ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
+               return -EIO;
+       }
 
-done:
-       vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY,
-                       __netconfig_tapi_state_changed_cb);
+       if (wifi_use == 0) {
+               WARN("VCONF_WIFI_WEARABLE_WIFI_USE is OFF");
+               return -EPERM;
+       }
+
+       err = wifi_power_driver_and_supplicant(TRUE);
+       if (err < 0 && err != -EALREADY)
+               return err;
+
+       err = _set_connman_technology_power(TRUE);
+
+       if (device_picker_test == TRUE)
+               netconfig_wifi_enable_device_picker_test();
+
+       return err;
 }
 #endif
 
-void netconfig_wifi_power_initialize(void)
+void wifi_power_initialize(void)
 {
        int wifi_last_power_state = 0;
 
        /* Initialize Airplane mode */
-#if defined TIZEN_TELEPHONY_ENABLE
-       vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
-#endif
+       netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
        DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
 
        /* Update the last Wi-Fi power state */
-       vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
-       if (wifi_last_power_state > VCONFKEY_WIFI_OFF && airplane_mode == 0) {
-#if defined TIZEN_TELEPHONY_ENABLE
-               int tapi_state = 0;
-                       vconf_get_bool(VCONFKEY_TELEPHONY_READY, &tapi_state);
-                       if (tapi_state == FALSE) {
+       netconfig_vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
+       if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
+               if (TIZEN_TELEPHONY_ENABLE) {
+                       int telephony_ready = 0;
+                       netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
+                       if (telephony_ready == 0) {
                                DBG("Telephony API is not initialized yet");
                                vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
-                                               __netconfig_tapi_state_changed_cb, NULL);
-
-                               goto done;
+                                               __netconfig_telephony_ready_changed_cb, NULL);
                        } else {
-                               if (netconfig_tapi_check_sim_state() == FALSE) {
+                               if (netconfig_tapi_check_sim_state() == FALSE)
                                        DBG("SIM is not initialized yet");
-
-                                       goto done;
-                               }
                        }
-#endif
+               }
                DBG("Turn Wi-Fi on automatically");
 #if defined TIZEN_WEARABLE
-               netconfig_wifi_on_wearable(TRUE);
+               wifi_power_on_wearable(TRUE);
 #else
-               netconfig_wifi_on();
+               wifi_power_on();
 #endif
        }
 
-#if defined TIZEN_TELEPHONY_ENABLE
-done:
-#endif
-
 #if defined TIZEN_WEARABLE
-       __netconfig_wifi_wearable_set_state_changed_cb(W_SERVICE_TYPE_BT, NULL);
-
-       vconf_notify_key_changed(VCONF_WIFI_WEARABLE_WIFI_USE,
-                       __netconfig_wifi_wearable_wifi_use_mode, NULL);
+       vconf_notify_key_changed(VCONF_WIFI_WEARABLE_WIFI_USE, __wearable_wifi_use_changed_cb, NULL);
 
-#if defined TIZEN_TELEPHONY_ENABLE
        vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
-                       __netconfig_wifi_wearable_airplane_mode, NULL);
-#endif
+                       __netconfig_wifi_airplane_mode, NULL);
 #else
        vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
                        __netconfig_wifi_restrict_mode, NULL);
-#if defined TIZEN_TELEPHONY_ENABLE
        vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
                        __netconfig_wifi_airplane_mode, NULL);
 #endif
-#endif
-
-       vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
-                       __netconfig_wifi_emergency_mode, NULL);
 
-       vconf_notify_key_changed(VCONFKEY_PM_STATE,
-                       __netconfig_wifi_pm_state_mode, NULL);
+       vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
+       vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
 }
 
-void netconfig_wifi_power_deinitialize(void)
+void wifi_power_deinitialize(void)
 {
 }
 
@@ -981,10 +857,19 @@ gboolean handle_load_driver(Wifi *wifi,
 
        g_return_val_if_fail(wifi != NULL, FALSE);
 
+       if (!netconfig_dpm_update_from_wifi()) {
+               DBG("DPM policy restricts Wi-Fi");
+               netconfig_error_permission_denied(context);
+               return TRUE;
+       }
+
+       if (TIZEN_WLAN_BOARD_SPRD)
+               wifi_firmware_download();
+
 #if defined TIZEN_WEARABLE
-       err = netconfig_wifi_on_wearable(device_picker_test);
+       err = wifi_power_on_wearable(device_picker_test);
 #else
-       err = netconfig_wifi_on();
+       err = wifi_power_on();
 
        if (device_picker_test == TRUE)
                netconfig_wifi_enable_device_picker_test();
@@ -999,11 +884,12 @@ gboolean handle_load_driver(Wifi *wifi,
                else
                        netconfig_error_wifi_driver_failed(context);
 
-               return FALSE;
+               return TRUE;
        }
 
 
        netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
+       __netconfig_set_wifi_bssid();
 
        wifi_complete_load_driver(wifi, context);
        return TRUE;
@@ -1017,7 +903,7 @@ gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
 
        g_return_val_if_fail(wifi != NULL, FALSE);
 
-       err = netconfig_wifi_off();
+       err = wifi_power_off();
        if (err < 0) {
                if (err == -EINPROGRESS)
                        netconfig_error_inprogress(context);
@@ -1027,7 +913,7 @@ gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
                        netconfig_error_permission_denied(context);
                else
                        netconfig_error_wifi_driver_failed(context);
-               return FALSE;
+               return TRUE;
        }
 
        netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
@@ -1038,32 +924,7 @@ gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
 
 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
 {
-       int err = 0;
-
-       DBG("P2P power on requested");
-
-       g_return_val_if_fail(wifi != NULL, FALSE);
-
-#if defined TIZEN_WEARABLE
-       err = netconfig_wifi_on_wearable(FALSE);
-#else
-       err = netconfig_wifi_on();
-#endif
-       if (err < 0)
-               if (err != -EINPROGRESS && err != -EALREADY) {
-                       netconfig_error_inprogress(context);
-                       return FALSE;
-               }
-
-       netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
-
-#if defined TIZEN_P2P_ENABLE && defined WLAN_CONCURRENT_MODE
-       err = __netconfig_p2p_supplicant(TRUE);
-       if (err < 0) {
-               netconfig_error_wifi_direct_failed(context);
-               return FALSE;
-       }
-#endif
+       ERR("Deprecated");
 
        wifi_complete_load_p2p_driver(wifi, context);
        return TRUE;
@@ -1071,27 +932,12 @@ gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
 
 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
 {
-#if defined TIZEN_P2P_ENABLE && defined WLAN_CONCURRENT_MODE
-       int err = 0;
-#endif
-
-       DBG("P2P power off requested");
-
-       g_return_val_if_fail(wifi != NULL, FALSE);
-
-#if defined TIZEN_P2P_ENABLE && defined WLAN_CONCURRENT_MODE
-       err = __netconfig_p2p_supplicant(FALSE);
-       if (err < 0) {
-               netconfig_error_wifi_direct_failed(context);;
-               return FALSE;
-       }
-#endif
+       ERR("Deprecated");
 
        wifi_complete_remove_p2p_driver(wifi, context);
        return TRUE;
 }
 
-#if defined TIZEN_TV
 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
 {
        DBG("Generate Random Mac address of ethernet");
@@ -1113,9 +959,9 @@ static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
 
 void __netconfig_set_ether_macaddr()
 {
-
        DBG("Set wired Mac address ");
        char *mac_addr = NULL;
+       char rand_addr[WLAN_MAC_ADDR_MAX];
        int rv = -1;
 
        mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
@@ -1129,10 +975,10 @@ void __netconfig_set_ether_macaddr()
                /*Generate the Random Mac address*/
                unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
 
-               if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE == -1)) {
+               if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE) == -1) {
 
                        ERR("Could not generate the Random Mac address");
-                       g_free(mac_addr);
+                       free(mac_addr);
                        return;
                }
 
@@ -1140,25 +986,25 @@ void __netconfig_set_ether_macaddr()
                rand_mac_add[0] |= 0x02; /*set local assignment bit*/
 
                /*Set the Mac address in Vconf*/
-               sprintf(mac_addr, "%x:%x:%x:%x:%x:%x",
+               snprintf(rand_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
                                rand_mac_add[0], rand_mac_add[1],
                                rand_mac_add[2], rand_mac_add[3],
                                rand_mac_add[4], rand_mac_add[5]);
 
-               netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, mac_addr);
+               netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, rand_addr);
+       } else { /* Valid MAC address */
+               g_strlcpy(rand_addr, mac_addr, WLAN_MAC_ADDR_MAX);
        }
 
-       DBG("MAC Address of eth0 [%s]",mac_addr);
+       DBG("MAC Address of eth0 [%s]", rand_addr);
        const char *path = NET_EXEC_PATH;
        char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
-               "ether",mac_addr, "up", NULL};
+               "ether", rand_addr, "up", NULL};
        char *const envs[] = { NULL };
        rv = netconfig_execute_file(path, args, envs);
 
-       if (rv < 0) {
+       if (rv < 0)
                ERR("Unable to execute system command");
-       }
-       g_free(mac_addr);
+       free(mac_addr);
 
 }
-#endif