Change open data path call to sync
authorCheoleun Moon <chleun.moon@samsung.com>
Tue, 10 Mar 2020 11:48:58 +0000 (20:48 +0900)
committerCheoleun Moon <chleun.moon@samsung.com>
Tue, 10 Mar 2020 11:48:58 +0000 (20:48 +0900)
src/include/wifi-aware-gdbus.h
src/wifi-aware-data-path.c
src/wifi-aware-gdbus.c

index 811824a..f0fa06b 100644 (file)
@@ -49,6 +49,7 @@ extern "C" {
 typedef enum {
        SERVICE_DISCOVERED_SIGNAL = 0,
        MESSAGE_RECEIVED_SIGNAL,
+       DATA_PATH_CONFIRMED_SIGNAL,
 } wifi_aware_signal_e;
 
 int wifi_aware_gdbus_init();
@@ -78,17 +79,15 @@ int wifi_aware_gdbus_followup(wifi_aware_session_h session,
 
 int wifi_aware_gdbus_open_ndp(wifi_aware_data_path_h data_path,
                int client_id, uint16_t pub_sub_id, unsigned int peer_id,
-               wifi_aware_data_path_role_e role, int port,
-               GAsyncReadyCallback cb);
+               wifi_aware_data_path_role_e role, int port);
 int wifi_aware_gdbus_open_ndp_psk(wifi_aware_data_path_h data_path,
                int client_id, uint16_t pub_sub_id, unsigned int peer_id,
                wifi_aware_data_path_role_e role, int port,
-               const char *psk, GAsyncReadyCallback cb);
+               const char *psk);
 int wifi_aware_gdbus_open_ndp_pmk(wifi_aware_data_path_h data_path,
                int client_id, uint16_t pub_sub_id, unsigned int peer_id,
                wifi_aware_data_path_role_e role, int port,
-               const unsigned char *pmk,
-               GAsyncReadyCallback cb);
+               const unsigned char *pmk);
 int wifi_aware_gdbus_ndp_close(wifi_aware_data_path_h data_path,
                int client_id, unsigned int ndp_id, uint16_t pub_sub_id, unsigned int peer_id);
 #ifdef __cplusplus
index 88998d8..8ea3fe2 100644 (file)
@@ -57,6 +57,8 @@ typedef struct {
        wifi_aware_data_path_terminated_cb terminated_cb;
        void *terminated_cb_user_data;
 
+       guint ndp_confirmed_signal_id;
+
        bool is_open;
 } wifi_aware_data_path_s;
 
@@ -228,51 +230,60 @@ static void __data_path_invoke_callback(wifi_aware_data_path_h data_path, wifi_a
                ndp->open_cb(data_path, error, ndp->open_cb_user_data);
 }
 
-static wifi_aware_error_e __get_data_path_open_result(GObject *src,
-               GAsyncResult *res, wifi_aware_data_path_h data_path)
+static wifi_aware_error_e __parsing_data_path_confirmed_event(GVariant *parameters,
+               wifi_aware_data_path_h data_path)
 {
-       GDBusConnection *conn = G_DBUS_CONNECTION(src);
-       GError *dbus_error = NULL;
-       GVariant *reply = g_dbus_connection_call_finish(conn, res, &dbus_error);
        int nan_error = 0;
        wifi_aware_error_e error = WIFI_AWARE_ERROR_NONE;
        gchar *iface = NULL;
        gchar *ip = NULL;
 
-       RET_VAL_IF(reply == NULL, WIFI_AWARE_ERROR_INVALID_OPERATION, "reply is NULL");
-
        wifi_aware_data_path_s *ndp = (wifi_aware_data_path_s *)data_path;
-       if (dbus_error != NULL) {
-               WIFI_AWARE_LOGE("Fail to open Data Path: %s", dbus_error->message);
-               error = _wifi_aware_convert_error_string_to_enum(dbus_error->message);
-               g_error_free(dbus_error);
-       }
-       else {
-               g_variant_get(reply, "ussii", &ndp->ndp_id, &iface, &ip, &ndp->peer_port, nan_error);
-               error = _wifi_aware_convert_nan_manager_error(nan_error);
-               if (error == WIFI_AWARE_ERROR_NONE) {
-                       strncpy(ndp->iface_name, iface, IFNAMSIZ);
-                       strncpy(ndp->peer_ip, ip, WIFI_AWARE_STR_IPV6_ADDRESS_LEN);
-                       WIFI_AWARE_LOGD("New Data Path. ndp_id: %u", ndp->ndp_id);
-               }
+       g_variant_get(parameters, "ussii", &ndp->ndp_id, &iface, &ip, &ndp->peer_port, nan_error);
+       error = _wifi_aware_convert_nan_manager_error(nan_error);
+       if (error == WIFI_AWARE_ERROR_NONE) {
+               strncpy(ndp->iface_name, iface, IFNAMSIZ);
+               strncpy(ndp->peer_ip, ip, WIFI_AWARE_STR_IPV6_ADDRESS_LEN);
+               WIFI_AWARE_LOGD("New Data Path. ndp_id: %u", ndp->ndp_id);
        }
        return error;
 }
 
-static void __data_path_open_reply(GObject *src, GAsyncResult *res, gpointer user_data)
+static void __data_path_unset_data_path_confirmed_cb(wifi_aware_data_path_h data_path)
 {
-       wifi_aware_error_e error = WIFI_AWARE_ERROR_NONE;
-       wifi_aware_data_path_h data_path = (wifi_aware_data_path_h)user_data;
+       wifi_aware_data_path_s *ndp = (wifi_aware_data_path_s *)data_path;
+       if (ndp->ndp_confirmed_signal_id == 0) {
+               WIFI_AWARE_LOGE("Invalid signal id");
+               return;
+       }
+       wifi_aware_gdbus_deregister_signal(ndp->ndp_confirmed_signal_id);
+       ndp->ndp_confirmed_signal_id = 0;
+}
 
-       WIFI_AWARE_LOGI("Reply for Data Path Open request");
-       RET_IF(data_path == NULL, "Data path is NULL");
+static void __ndp_confirmed_cb(GDBusConnection *connection,
+                    const gchar *sender, const gchar *object_path,
+                    const gchar *interface, const gchar *signal,
+                    GVariant *parameters, gpointer user_data)
+{
+       __WIFI_AWARE_FUNC_ENTER__;
+       WIFI_AWARE_LOGD("NAN Data Path Confirmed");
 
-       error = __get_data_path_open_result(src, res, data_path);
-       WIFI_AWARE_LOGD("Error: %d", error);
+       wifi_aware_error_e error = WIFI_AWARE_ERROR_NONE;
+       wifi_aware_data_path_h data_path = (wifi_aware_data_path_h)user_data;
 
+       error = __parsing_data_path_confirmed_event(parameters, data_path);
        if (error == WIFI_AWARE_ERROR_NONE)
                __data_path_open(data_path);
        __data_path_invoke_callback(data_path, error, NULL, NULL, 0);
+       __data_path_unset_data_path_confirmed_cb(data_path);
+}
+
+static void __data_path_set_data_path_confirmed_cb(wifi_aware_data_path_h data_path)
+{
+       wifi_aware_data_path_s *ndp = (wifi_aware_data_path_s *)data_path;
+       ndp->ndp_confirmed_signal_id =
+               wifi_aware_gdbus_register_signal(DATA_PATH_CONFIRMED_SIGNAL,
+                               __ndp_confirmed_cb, data_path);
 }
 
 int _wifi_aware_data_path_open(wifi_aware_data_path_h data_path,
@@ -282,40 +293,47 @@ int _wifi_aware_data_path_open(wifi_aware_data_path_h data_path,
 
        RET_VAL_IF(data_path == NULL, WIFI_AWARE_ERROR_INVALID_PARAMETER, "data_path is NULL");
        wifi_aware_data_path_s *ndp = (wifi_aware_data_path_s *)data_path;
+       int ret = WIFI_AWARE_ERROR_NONE;
 
        __data_path_set_open_cb(data_path, callback, user_data);
+       __data_path_set_data_path_confirmed_cb(data_path);
        switch (ndp->security_type) {
        case WIFI_AWARE_SECURITY_TYPE_OPEN:
-               return wifi_aware_gdbus_open_ndp(data_path,
+               ret = wifi_aware_gdbus_open_ndp(data_path,
                                _wifi_aware_get_client_id(),
                                _wifi_aware_session_get_pub_sub_id(ndp->session),
                                _wifi_aware_peer_get_id(ndp->peer),
                                ndp->role,
-                               ndp->port,
-                               __data_path_open_reply);
+                               ndp->port);
+               break;
        case WIFI_AWARE_SECURITY_TYPE_PSK:
-               return wifi_aware_gdbus_open_ndp_psk(data_path,
+               ret = wifi_aware_gdbus_open_ndp_psk(data_path,
                                _wifi_aware_get_client_id(),
                                _wifi_aware_session_get_pub_sub_id(ndp->session),
                                _wifi_aware_peer_get_id(ndp->peer),
                                ndp->role,
                                ndp->port,
-                               ndp->psk,
-                               __data_path_open_reply);
+                               ndp->psk);
+               break;
        case WIFI_AWARE_SECURITY_TYPE_PMK:
-               return wifi_aware_gdbus_open_ndp_pmk(data_path,
+               ret = wifi_aware_gdbus_open_ndp_pmk(data_path,
                                _wifi_aware_get_client_id(),
                                _wifi_aware_session_get_pub_sub_id(ndp->session),
                                _wifi_aware_peer_get_id(ndp->peer),
                                ndp->role,
                                ndp->port,
-                               ndp->pmk,
-                               __data_path_open_reply);
+                               ndp->pmk);
+               break;
+       default:
+               ret = WIFI_AWARE_ERROR_INVALID_PARAMETER;
        }
 
-       WIFI_AWARE_LOGE("Invalid security type");
-       __data_path_set_open_cb(data_path, NULL, NULL);
-       return WIFI_AWARE_ERROR_INVALID_PARAMETER;
+       if (ret != WIFI_AWARE_ERROR_NONE) {
+               WIFI_AWARE_LOGE("Invalid security type");
+               __data_path_set_open_cb(data_path, NULL, NULL);
+               __data_path_unset_data_path_confirmed_cb(data_path);
+       }
+       return ret;
 }
 
 int _wifi_aware_data_path_close(wifi_aware_data_path_h data_path)
index 470c82a..35496fd 100644 (file)
@@ -56,6 +56,10 @@ static struct {
                WIFI_AWARE_DISCOVERY_INTERFACE,
                "MessageReceived",
        },
+       [DATA_PATH_CONFIRMED_SIGNAL] = {
+               WIFI_AWARE_DISCOVERY_INTERFACE,
+               "DataPathConfirmed",
+       },
 };
 
 guint wifi_aware_gdbus_register_signal(wifi_aware_signal_e signal,
@@ -429,7 +433,7 @@ static int __handle_ndp_close_reply(GVariant *reply)
 {
        int error;
        g_variant_get(reply, "(i)", &error);
-       WIFI_AWARE_LOGI("Reply for Disable %d", error);
+       WIFI_AWARE_LOGI("Reply for Close Data Path %d", error);
        return error;
 }
 
@@ -657,23 +661,36 @@ int wifi_aware_gdbus_followup(wifi_aware_session_h session,
        return ret;
 }
 
+static int __handle_ndp_open_reply(GVariant *reply)
+{
+       int error;
+       g_variant_get(reply, "(i)", &error);
+       WIFI_AWARE_LOGI("Reply for Open Data Path %d", error);
+       return error;
+}
+
 int wifi_aware_gdbus_open_ndp(wifi_aware_data_path_h data_path,
                int client_id, uint16_t pub_sub_id, unsigned int peer_id,
-               wifi_aware_data_path_role_e role, int port,
-               GAsyncReadyCallback cb)
+               wifi_aware_data_path_role_e role, int port)
 {
        __WIFI_AWARE_FUNC_ENTER__;
        GVariant *params = NULL;
+       GVariant *reply = NULL;
        int ret = WIFI_AWARE_ERROR_NONE;
 
        params = __create_ndp_open_request_param(client_id, pub_sub_id, peer_id, role, port);
        WIFI_AWARE_LOGD("created parameters");
        DBUS_DEBUG_VARIANT(params);
 
-       ret = __wifi_aware_dbus_method_call_async(
+       ret = __wifi_aware_dbus_method_call_sync(
                WIFI_AWARE_DISCOVERY_INTERFACE,
                WIFI_AWARE_DISCOVERY_PATH,
-               "OpenDataPath", params, cb, data_path);
+               "OpenDataPath", params, &reply);
+
+       RET_VAL_IF(ret != WIFI_AWARE_ERROR_NONE, ret, "Failed to open data path");
+
+       ret = __handle_ndp_open_reply(reply);
+       g_variant_unref(reply);
 
        __WIFI_AWARE_FUNC_EXIT__;
        return ret;
@@ -681,12 +698,12 @@ int wifi_aware_gdbus_open_ndp(wifi_aware_data_path_h data_path,
 
 int wifi_aware_gdbus_open_ndp_psk(wifi_aware_data_path_h data_path,
                int client_id, uint16_t pub_sub_id, unsigned int peer_id,
-               wifi_aware_data_path_role_e role, int port, const char *psk,
-               GAsyncReadyCallback cb)
+               wifi_aware_data_path_role_e role, int port, const char *psk)
 {
        __WIFI_AWARE_FUNC_ENTER__;
        GVariant *params = NULL;
        GVariant *params_psk = NULL;
+       GVariant *reply = NULL;
        int ret = WIFI_AWARE_ERROR_NONE;
 
        params = __create_ndp_open_request_param(client_id, pub_sub_id, peer_id, role, port);
@@ -695,10 +712,15 @@ int wifi_aware_gdbus_open_ndp_psk(wifi_aware_data_path_h data_path,
        WIFI_AWARE_LOGD("created parameters");
        DBUS_DEBUG_VARIANT(params_psk);
 
-       ret = __wifi_aware_dbus_method_call_async(
+       ret = __wifi_aware_dbus_method_call_sync(
                WIFI_AWARE_DISCOVERY_INTERFACE,
                WIFI_AWARE_DISCOVERY_PATH,
-               "OpenDataPathPSK", params_psk, cb, data_path);
+               "OpenDataPathPsk", params_psk, &reply);
+
+       RET_VAL_IF(ret != WIFI_AWARE_ERROR_NONE, ret, "Failed to open data path");
+
+       ret = __handle_ndp_open_reply(reply);
+       g_variant_unref(reply);
 
        __WIFI_AWARE_FUNC_EXIT__;
        return ret;
@@ -706,12 +728,12 @@ int wifi_aware_gdbus_open_ndp_psk(wifi_aware_data_path_h data_path,
 
 int wifi_aware_gdbus_open_ndp_pmk(wifi_aware_data_path_h data_path,
                int client_id, uint16_t pub_sub_id, unsigned int peer_id,
-               wifi_aware_data_path_role_e role, int port, const unsigned char *pmk,
-               GAsyncReadyCallback cb)
+               wifi_aware_data_path_role_e role, int port, const unsigned char *pmk)
 {
        __WIFI_AWARE_FUNC_ENTER__;
        GVariant *params = NULL;
        GVariant *params_pmk = NULL;
+       GVariant *reply = NULL;
        int ret = WIFI_AWARE_ERROR_NONE;
 
        params = __create_ndp_open_request_param(client_id, pub_sub_id, peer_id, role, port);
@@ -720,10 +742,15 @@ int wifi_aware_gdbus_open_ndp_pmk(wifi_aware_data_path_h data_path,
        WIFI_AWARE_LOGD("created parameters");
        DBUS_DEBUG_VARIANT(params_pmk);
 
-       ret = __wifi_aware_dbus_method_call_async(
+       ret = __wifi_aware_dbus_method_call_sync(
                WIFI_AWARE_DISCOVERY_INTERFACE,
                WIFI_AWARE_DISCOVERY_PATH,
-               "OpenDataPathPMK", params_pmk, cb, data_path);
+               "OpenDataPathPmk", params_pmk, &reply);
+
+       RET_VAL_IF(ret != WIFI_AWARE_ERROR_NONE, ret, "Failed to open data path");
+
+       ret = __handle_ndp_open_reply(reply);
+       g_variant_unref(reply);
 
        __WIFI_AWARE_FUNC_EXIT__;
        return ret;