Fixed wifi activation failure
[platform/core/connectivity/net-config.git] / src / wifi-power.c
index 5705b69..81e0eac 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
-
 #include "log.h"
 #include "util.h"
 #include "netdbus.h"
 #include "neterror.h"
 #include "wifi-wps.h"
+#include "wifi-bssid-scan.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"
@@ -44,8 +40,8 @@
 #include "wifi-background-scan.h"
 
 
-#define WLAN_SUPPLICANT_SCRIPT         "/usr/sbin/wpa_supp.sh"
-#define P2P_SUPPLICANT_SCRIPT          "/usr/sbin/p2p_supp.sh"
+#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 VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT     "db/setting/network_with_lcd_off_limit"
 #endif
 
-#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"
 
-#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
+
+#define NETCONFIG_TECH_WAITING_INTERVAL 500
+#define NETCONFIG_TECH_WAITING_COUNT 6
 
 static gboolean connman_wifi_technology_state = FALSE;
 static gboolean wifi_firmware_recovery_mode = FALSE;
@@ -98,7 +95,7 @@ static void __technology_reply(GObject *source_object, GAsyncResult *res, gpoint
        GDBusConnection *conn = NULL;
        GError *error = NULL;
 
-       conn = G_DBUS_CONNECTION (source_object);
+       conn = G_DBUS_CONNECTION(source_object);
        reply = g_dbus_connection_call_finish(conn, res, &error);
 
        if (reply == NULL) {
@@ -130,8 +127,8 @@ 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;
 
@@ -152,28 +149,6 @@ static int __execute_supplicant(gboolean enable)
        return 0;
 }
 
-#if defined TIZEN_P2P_ENABLE && defined WLAN_CONCURRENT_MODE
-static int __netconfig_p2p_supplicant(gboolean enable)
-{
-       int rv = 0;
-       const char *path = P2P_SUPPLICANT_SCRIPT;
-       char *const args_enable[] = { P2P_SUPPLICANT_SCRIPT, "start", NULL };
-       char *const args_disable[] = { P2P_SUPPLICANT_SCRIPT, "stop", NULL };
-       char *const envs[] = { NULL };
-
-       if (enable == TRUE)
-               rv = netconfig_execute_file(path, args_enable, envs);
-       else
-               rv = netconfig_execute_file(path, args_disable, envs);
-       if (rv < 0)
-               return -EIO;
-
-       DBG("p2p_supplicant %s", enable == TRUE ? "started" : "stopped");
-
-       return 0;
-}
-#endif
-
 void netconfig_wifi_recover_firmware(void)
 {
        wifi_firmware_recovery_mode = TRUE;
@@ -183,42 +158,6 @@ void netconfig_wifi_recover_firmware(void)
        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)
-{
-       int err;
-
-       wifi_direct_unset_device_state_changed_cb();
-       wifi_direct_deinitialize();
-
-       if (device_state == WIFI_DIRECT_DEVICE_STATE_DEACTIVATED) {
-               err = wifi_power_on();
-               if (err < 0) {
-                       if (err == -EALREADY)
-                               wifi_state_update_power_state(TRUE);
-                       else
-                               wifi_state_emit_power_failed();
-               }
-       }
-}
-
-static gboolean __netconfig_wifi_direct_power_off(void)
-{
-       DBG("Wi-Fi direct is turning off");
-
-       if (wifi_direct_initialize() < 0)
-               return FALSE;
-
-       if (wifi_direct_set_device_state_changed_cb(__netconfig_wifi_direct_state_cb, NULL) < 0)
-               return FALSE;
-
-       if (wifi_direct_deactivate() < 0)
-               return FALSE;
-
-       return TRUE;
-}
-#endif
-
 static int _load_driver_and_supplicant(void)
 {
        int err = 0;
@@ -247,8 +186,9 @@ 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) {
+                                       netconfig_wifi_is_bssid_scan_started() == TRUE) {
                DBG("Wi-Fi WPS mode");
                return 0;
        }
@@ -263,6 +203,9 @@ static int _remove_driver_and_supplicant(void)
 
        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 (wifi_power_on() < 0)
                        ERR("Failed to recover Wi-Fi firmware");
@@ -273,6 +216,41 @@ static int _remove_driver_and_supplicant(void)
        return 0;
 }
 
+static gboolean __check_and_set_technology_enable(gpointer data)
+{
+       static int retry_count = NETCONFIG_TECH_WAITING_COUNT;
+       gboolean value_enable = TRUE;
+       gboolean reply = FALSE;
+       GVariant *param0 = NULL;
+       GVariant *params = NULL;
+       char key[] = "Powered";
+
+       if (wifi_state_is_technology_available() == FALSE) {
+               retry_count--;
+               if (retry_count > 0)
+                       return TRUE;
+       }
+
+       param0 = g_variant_new_boolean(value_enable);
+       params = g_variant_new("(sv)", key, param0);
+
+       reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
+                                       CONNMAN_WIFI_TECHNOLOGY_PREFIX,
+                                       CONNMAN_TECHNOLOGY_INTERFACE,
+                                       "SetProperty", params, __technology_reply);
+
+       if (reply != TRUE) {
+               ERR("Fail to set technology enable");
+               wifi_state_update_power_state(FALSE);
+
+               retry_count = NETCONFIG_TECH_WAITING_COUNT;
+               return FALSE;
+       }
+
+       retry_count = NETCONFIG_TECH_WAITING_COUNT;
+       return FALSE;
+}
+
 static int _set_connman_technology_power(gboolean enable)
 {
        gboolean reply = FALSE;
@@ -285,6 +263,13 @@ static int _set_connman_technology_power(gboolean enable)
        if (connman_wifi_technology_state == enable)
                return -EALREADY;
 
+       if (enable && wifi_state_is_technology_available() == FALSE) {
+               netconfig_start_timer(NETCONFIG_TECH_WAITING_INTERVAL,
+                               __check_and_set_technology_enable, NULL, NULL);
+               connman_wifi_technology_state = enable;
+               return 0;
+       }
+
        if (enable == TRUE)
                param0 = g_variant_new_boolean(value_enable);
        else
@@ -319,10 +304,10 @@ static void __netconfig_set_wifi_bssid(void)
        int rv = 0;
        char bssid[WLAN_MAC_ADDR_MAX];
 
-       FILE *fp = fopen(WLAN_MAC_INFO, "r");
+       FILE *fp = fopen(WLAN_MAC_ADDRESS_FILEPATH, "r");
 
        if (fp == NULL) {
-               ERR("Fail to open file");
+               ERR("Fail to open %s", WLAN_MAC_ADDRESS_FILEPATH);
                return;
        }
 
@@ -337,90 +322,12 @@ static void __netconfig_set_wifi_bssid(void)
        fclose(fp);
 }
 
-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 (enable == TRUE)
-               return _load_driver_and_supplicant();
-       else {
-               if (connman_wifi_technology_state == TRUE)
-                       return -ENOSYS;
-
-               return _load_driver_and_supplicant();
-       }
-}
-
 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;
 }
 
-int netconfig_wifi_on(void)
-{
-       int err = 0;
-       wifi_tech_state_e wifi_tech_state;
-
-       wifi_tech_state = wifi_state_get_technology_state();
-       if (wifi_tech_state >= NETCONFIG_WIFI_TECH_POWERED)
-               return -EALREADY;
-
-       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;
-       }
-
-#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
-
-       err = wifi_power_driver_and_supplicant(TRUE);
-       if (err < 0 && err != -EALREADY)
-               return err;
-
-       err = _set_connman_technology_power(TRUE);
-
-       __netconfig_set_wifi_bssid();
-
-       return err;
-}
-
-int netconfig_wifi_off(void)
-{
-       int err;
-
-#if defined TIZEN_P2P_ENABLE && defined WLAN_CONCURRENT_MODE
-       __netconfig_p2p_supplicant(FALSE);
-#endif
-
-       err = _set_connman_technology_power(FALSE);
-       if (err == -EALREADY)
-               wifi_state_update_power_state(FALSE);
-
-       return 0;
-}
-
 #if defined TIZEN_WEARABLE
 int netconfig_wifi_on_wearable(gboolean device_picker_test)
 {
@@ -703,70 +610,6 @@ static void __pm_state_changed_cb(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)
-{
-       TelSimCardStatus_t *status = data;
-
-       if (*status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
-               DBG("Turn Wi-Fi on automatically");
-#if defined TIZEN_WEARABLE
-               wifi_power_on_wearable(TRUE);
-#else
-               wifi_power_on();
-#endif
-               netconfig_tel_deinit();
-       }
-}
-
-static gboolean netconfig_tapi_check_sim_state(void)
-{
-       int ret, card_changed;
-       TelSimCardStatus_t status = TAPI_SIM_STATUS_UNKNOWN;
-       TapiHandle *tapi_handle = NULL;
-
-       tapi_handle = (TapiHandle *)netconfig_tel_init();
-       if (tapi_handle == NULL) {
-               ERR("Failed to tapi init");
-               return FALSE;
-       }
-
-       ret = tel_get_sim_init_info(tapi_handle, &status, &card_changed);
-       if (ret != TAPI_API_SUCCESS) {
-               ERR("tel_get_sim_init_info() Failed : [%d]", ret);
-               netconfig_tel_deinit();
-               return FALSE;
-       }
-
-       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;
-       }
-
-       netconfig_tel_deinit();
-
-       return TRUE;
-}
-
 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
 {
        int telephony_ready = 0;
@@ -796,7 +639,6 @@ static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
 done:
        vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
 }
-#endif
 
 int wifi_power_driver_and_supplicant(gboolean enable)
 {
@@ -841,8 +683,23 @@ int wifi_power_on(void)
        wifi_tech_state_e tech_state;
 
        tech_state = wifi_state_get_technology_state();
-       if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
+       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;
+       }
 
        if (__is_wifi_restricted() == TRUE)
                return -EPERM;
@@ -854,17 +711,6 @@ int wifi_power_on(void)
                return -EBUSY;
        }
 
-#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
-
        err = wifi_power_driver_and_supplicant(TRUE);
        if (err < 0 && err != -EALREADY)
                return err;
@@ -930,18 +776,18 @@ void wifi_power_initialize(void)
        /* Update the last Wi-Fi power state */
        netconfig_vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
        if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
-#if defined 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_telephony_ready_changed_cb, NULL);
-               } else {
-                       if (netconfig_tapi_check_sim_state() == FALSE)
-                               DBG("SIM is not initialized yet");
+               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_telephony_ready_changed_cb, NULL);
+                       } else {
+                               if (netconfig_tapi_check_sim_state() == FALSE)
+                                       DBG("SIM is not initialized yet");
+                       }
                }
-#endif
                DBG("Turn Wi-Fi on automatically");
 #if defined TIZEN_WEARABLE
                wifi_power_on_wearable(TRUE);
@@ -985,9 +831,8 @@ gboolean handle_load_driver(Wifi *wifi,
                return TRUE;
        }
 
-#if defined TIZEN_WLAN_BOARD_SPRD
-       wifi_firmware_download();
-#endif
+       if (TIZEN_WLAN_BOARD_SPRD)
+               wifi_firmware_download();
 
 #if defined TIZEN_WEARABLE
        err = wifi_power_on_wearable(device_picker_test);
@@ -1061,7 +906,6 @@ gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *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");
@@ -1083,9 +927,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);
@@ -1099,10 +943,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;
                }
 
@@ -1110,24 +954,25 @@ void __netconfig_set_ether_macaddr()
                rand_mac_add[0] |= 0x02; /*set local assignment bit*/
 
                /*Set the Mac address in Vconf*/
-               snprintf(mac_addr, WLAN_MAC_ADDR_MAX, "%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)
                ERR("Unable to execute system command");
-       g_free(mac_addr);
+       free(mac_addr);
 
 }
-#endif