Modified way of update rssi value
[platform/core/connectivity/net-config.git] / src / wifi-power.c
index be2dacd..c0236c7 100755 (executable)
@@ -23,6 +23,7 @@
 #include <ITapiSim.h>
 #include <TapiUtility.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <tzplatform_config.h>
 
 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
 #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
 
 static gboolean connman_wifi_technology_state = FALSE;
 static gboolean wifi_firmware_recovery_mode = FALSE;
@@ -248,6 +247,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");
@@ -264,6 +264,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");
@@ -342,90 +345,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)
 {
@@ -708,7 +633,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)
 {
@@ -801,7 +725,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)
 {
@@ -846,8 +769,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;
@@ -935,18 +873,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);
@@ -990,9 +928,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);
@@ -1066,7 +1003,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");
@@ -1107,7 +1043,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;
                }
 
@@ -1135,7 +1071,6 @@ void __netconfig_set_ether_macaddr()
 
        if (rv < 0)
                ERR("Unable to execute system command");
-       g_free(mac_addr);
+       free(mac_addr);
 
 }
-#endif