Remove Telephony Dependency for IoT_Core/Headless devices
[platform/core/connectivity/net-config.git] / src / wifi-power.c
index 9cb9af8..72b8673 100755 (executable)
@@ -20,9 +20,9 @@
 #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
@@ -36,7 +36,6 @@
 #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"
@@ -44,8 +43,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"
@@ -129,8 +128,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;
 
@@ -246,6 +245,7 @@ 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");
@@ -262,6 +262,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");
@@ -340,28 +343,6 @@ 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 */
@@ -650,69 +631,6 @@ static void __pm_state_changed_cb(keynode_t* node, void* user_data)
        prev_state = new_state;
 }
 
-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;
@@ -786,8 +704,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;
@@ -1045,7 +978,7 @@ void __netconfig_set_ether_macaddr()
                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;
                }
 
@@ -1060,8 +993,7 @@ void __netconfig_set_ether_macaddr()
 
                netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, rand_addr);
        } else { /* Valid MAC address */
-               strncpy(rand_addr, mac_addr, strlen(mac_addr));
-               rand_addr[strlen(mac_addr)] = '\0';
+               g_strlcpy(rand_addr, mac_addr, WLAN_MAC_ADDR_MAX);
        }
 
        DBG("MAC Address of eth0 [%s]", rand_addr);
@@ -1073,6 +1005,6 @@ void __netconfig_set_ether_macaddr()
 
        if (rv < 0)
                ERR("Unable to execute system command");
-       g_free(mac_addr);
+       free(mac_addr);
 
 }