Check already powered during Wi-Fi enable/disable
[framework/connectivity/net-config.git] / src / wifi-power.c
index 7017793..15ac07c 100644 (file)
 
 #define WLAN_DRIVER_SCRIPT "/usr/bin/wlan.sh"
 
+static gboolean power_in_progress = FALSE;
+static gboolean fm_waiting = FALSE;
 
-static gboolean __netconfig_wifi_enable_technology(void)
+static void __netconfig_wifi_technology_reply(DBusPendingCall *call, void *data)
 {
-       DBusMessage *reply = NULL;
-       char param1[] = "string:Powered";
-       char param2[] = "variant:boolean:true";
-       char *param_array[] = {NULL, NULL, NULL};
+       DBusMessage *message;
 
-       param_array[0] = param1;
-       param_array[1] = param2;
+       message = dbus_pending_call_steal_reply(call);
 
-       reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE, CONNMAN_WIFI_TECHNOLOGY_PREFIX,
-                       CONNMAN_TECHNOLOGY_INTERFACE, "SetProperty", param_array);
+       if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
+               ERR("%s", dbus_message_get_error_name(message));
 
-       if (reply == NULL) {
-               ERR("Error! Request failed");
-               return FALSE;
+               if (dbus_message_is_error(message,
+                               CONNMAN_ERROR_INTERFACE ".AlreadyEnabled") == TRUE) {
+                       netconfig_wifi_update_power_state(TRUE);
+                       power_in_progress = FALSE;
+               } else if (dbus_message_is_error(message,
+                               CONNMAN_ERROR_INTERFACE ".AlreadyDisabled") == TRUE) {
+                       netconfig_wifi_update_power_state(FALSE);
+                       power_in_progress = FALSE;
+               }
+       } else {
+               DBG("Successfully requested");
        }
 
-       dbus_message_unref(reply);
+       dbus_message_unref(message);
+       dbus_pending_call_unref(call);
+}
 
-       return TRUE;
+static gboolean __netconfig_wifi_enable_technology(void)
+{
+       gboolean reply = FALSE;
+       char param0[] = "string:Powered";
+       char param1[] = "variant:boolean:true";
+       char *param_array[] = { NULL, NULL, NULL };
+
+       param_array[0] = param0;
+       param_array[1] = param1;
+
+       reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
+                       CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
+                       "SetProperty", param_array, __netconfig_wifi_technology_reply);
+
+       if (reply != TRUE)
+               ERR("Fail to enable Wi-Fi");
+
+       return reply;
 }
 
 static gboolean __netconfig_wifi_disable_technology(void)
 {
-       DBusMessage *reply = NULL;
-       char param1[] = "string:Powered";
-       char param2[] = "variant:boolean:false";
-       char *param_array[] = {NULL, NULL, NULL};
+       gboolean reply = FALSE;
+       char param0[] = "string:Powered";
+       char param1[] = "variant:boolean:false";
+       char *param_array[] = { NULL, NULL, NULL };
 
-       param_array[0] = param1;
-       param_array[1] = param2;
+       param_array[0] = param0;
+       param_array[1] = param1;
 
-       reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE, CONNMAN_WIFI_TECHNOLOGY_PREFIX,
-                       CONNMAN_TECHNOLOGY_INTERFACE, "SetProperty", param_array);
+       reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
+                       CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
+                       "SetProperty", param_array, __netconfig_wifi_technology_reply);
 
-       if (reply == NULL) {
-               ERR("Error! Request failed");
-               return FALSE;
-       }
-
-       dbus_message_unref(reply);
+       if (reply != TRUE)
+               ERR("Fail to disable Wi-Fi");
 
-       return TRUE;
+       return reply;
 }
 
 static gboolean __netconfig_wifi_load_driver(void)
@@ -174,13 +196,12 @@ static void __netconfig_wifi_direct_state_cb(int error_code,
 
        if (device_state == WIFI_DIRECT_DEVICE_STATE_DEACTIVATED) {
                if (__netconfig_wifi_try_to_load_driver() < 0) {
+                       power_in_progress = FALSE;
 
                        /* TODO: error report */
 
                        return;
                }
-
-               netconfig_wifi_notify_power_completed(TRUE);
        }
 }
 
@@ -213,9 +234,10 @@ static int __netconfig_wifi_try_to_load_driver(void)
        }
 
        if (netconfig_is_wifi_direct_on() == TRUE) {
-               if (__netconfig_wifi_direct_power_off() == TRUE)
+               if (__netconfig_wifi_direct_power_off() == TRUE) {
+                       power_in_progress = TRUE;
                        return -EINPROGRESS;
-               else
+               else
                        return -EBUSY;
        }
 
@@ -225,7 +247,12 @@ static int __netconfig_wifi_try_to_load_driver(void)
                return -EIO;
        }
 
-       __netconfig_wifi_enable_technology();
+       if (__netconfig_wifi_enable_technology() != TRUE) {
+               netconfig_wifi_remove_driver();
+               return -EIO;
+       }
+
+       power_in_progress = TRUE;
 
        return 0;
 }
@@ -236,7 +263,10 @@ static gboolean __netconfig_wifi_try_to_remove_driver(void)
 
        netconfig_wifi_statistics_update_powered_off();
 
-       __netconfig_wifi_disable_technology();
+       if (__netconfig_wifi_disable_technology() != TRUE)
+               return FALSE;
+
+       power_in_progress = TRUE;
 
        return TRUE;
 }
@@ -248,6 +278,13 @@ static void __netconfig_wifi_airplane_mode(keynode_t* node,
        int wifi_state = 0;
        static gboolean powered_off_by_flightmode = FALSE;
 
+       if (power_in_progress) {
+               fm_waiting = TRUE;
+               return;
+       }
+
+       fm_waiting = FALSE;
+
        vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &value);
        vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
 
@@ -317,6 +354,17 @@ static void __netconfig_wifi_pm_state_mode(keynode_t* node,
        prev_state = value;
 }
 
+void netconfig_set_power_in_progress(gboolean in_progress)
+{
+       power_in_progress = in_progress;
+}
+
+void netconfig_check_fm_waiting(void)
+{
+       if (fm_waiting)
+               __netconfig_wifi_airplane_mode(NULL, NULL);
+}
+
 void netconfig_wifi_power_configuration(void)
 {
        int wifi_last_power_state = 0;
@@ -350,6 +398,11 @@ gboolean netconfig_iface_wifi_load_driver(NetconfigWifi *wifi, GError **error)
                return FALSE;
        }
 
+       if (power_in_progress) {
+               netconfig_error_wifi_driver_failed(error);
+               return FALSE;
+       }
+
        err = __netconfig_wifi_try_to_load_driver();
        if (err < 0) {
                if (err == -EINPROGRESS)
@@ -369,6 +422,11 @@ gboolean netconfig_iface_wifi_remove_driver(NetconfigWifi *wifi, GError **error)
 
        g_return_val_if_fail(wifi != NULL, FALSE);
 
+       if (power_in_progress) {
+               netconfig_error_wifi_driver_failed(error);
+               return FALSE;
+       }
+
        if (__netconfig_wifi_try_to_remove_driver() != TRUE) {
                netconfig_error_wifi_driver_failed(error);