From ad5e35e82992c5a24fb162cacd8df83b187e82dc Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Tue, 25 Feb 2020 19:24:28 +0900 Subject: [PATCH] Add message_received_cb --- include/wifi-aware.h | 17 +++++-- src/include/wifi-aware-gdbus.h | 1 + src/include/wifi-aware-private.h | 3 ++ src/wifi-aware-gdbus.c | 12 +++-- src/wifi-aware-private.c | 103 ++++++++++++++++++++++++++++++++++++--- src/wifi-aware.c | 18 ++++++- 6 files changed, 137 insertions(+), 17 deletions(-) diff --git a/include/wifi-aware.h b/include/wifi-aware.h index fe34204..86799e1 100644 --- a/include/wifi-aware.h +++ b/include/wifi-aware.h @@ -568,22 +568,31 @@ int wifi_aware_send_message( * @return 0 on success, otherwise a negative error value * @retval #WIFI_AWARE_ERROR_NONE */ -typedef void(*wifi_aware_received_message_cb)( +typedef void(*wifi_aware_message_received_cb)( wifi_aware_session_h session, wifi_aware_peer_h peer, unsigned char *message, size_t len, void *user_data); /** - * @brief TODO: This callback is registered to wifi_aware handle, not publish/subscribe sessions + * @brief * @since_tizen 6.0 * @param[in] session * @param[in] callback + * @param[in] user_data * @return 0 on success, otherwise a negative error value * @retval #WIFI_AWARE_ERROR_NONE */ -int wifi_aware_set_received_message_cb(wifi_aware_session_h session, - wifi_aware_received_message_cb callback, void *user_data); +int wifi_aware_set_message_received_cb(wifi_aware_session_h session, + wifi_aware_message_received_cb callback, void *user_data); +/** + * @brief + * @since_tizen 6.0 + * @param[in] session + * @return 0 on success, otherwise a negative error value + * @retval #WIFI_AWARE_ERROR_NONE + */ +int wifi_aware_unset_message_received_cb(wifi_aware_session_h session); // Can APIs to get peer information using wifi_aware_peer_h? // wifi_aware_peer_get_mac_address(): Which mac address should this API show, NMI or NDI? diff --git a/src/include/wifi-aware-gdbus.h b/src/include/wifi-aware-gdbus.h index e556c2d..101fd0f 100644 --- a/src/include/wifi-aware-gdbus.h +++ b/src/include/wifi-aware-gdbus.h @@ -47,6 +47,7 @@ extern "C" { typedef enum { SERVICE_DISCOVERED_SIGNAL = 0, + MESSAGE_RECEIVED_SIGNAL, } wifi_aware_signal_e; int wifi_aware_gdbus_init(); diff --git a/src/include/wifi-aware-private.h b/src/include/wifi-aware-private.h index 4406a87..738dc4a 100644 --- a/src/include/wifi-aware-private.h +++ b/src/include/wifi-aware-private.h @@ -93,6 +93,9 @@ int _wifi_aware_session_cancel(wifi_aware_session_h session); void _wifi_aware_set_service_discovered_cb(wifi_aware_session_h session, wifi_aware_service_discovered_cb callback, void *user_data); void _wifi_aware_unset_service_discovered_cb(wifi_aware_session_h session); +void _wifi_aware_set_message_received_cb(wifi_aware_session_h session, + wifi_aware_message_received_cb callback, void *user_data); +void _wifi_aware_unset_message_received_cb(wifi_aware_session_h session); int _wifi_aware_send_message(wifi_aware_session_h session, wifi_aware_peer_h peer, unsigned char *message, size_t len, diff --git a/src/wifi-aware-gdbus.c b/src/wifi-aware-gdbus.c index 3adae10..9851f66 100644 --- a/src/wifi-aware-gdbus.c +++ b/src/wifi-aware-gdbus.c @@ -48,10 +48,14 @@ static struct { _mac_address_changed }, */ - [SERVICE_DISCOVERED_SIGNAL] = { - WIFI_AWARE_DISCOVERY_INTERFACE, - "ServiceDiscovered", - }, + [SERVICE_DISCOVERED_SIGNAL] = { + WIFI_AWARE_DISCOVERY_INTERFACE, + "ServiceDiscovered", + }, + [MESSAGE_RECEIVED_SIGNAL] = { + WIFI_AWARE_DISCOVERY_INTERFACE, + "MessageReceived", + }, }; guint wifi_aware_gdbus_register_signal(wifi_aware_signal_e signal, diff --git a/src/wifi-aware-private.c b/src/wifi-aware-private.c index fd6a36b..f33dfac 100644 --- a/src/wifi-aware-private.c +++ b/src/wifi-aware-private.c @@ -83,7 +83,7 @@ typedef struct { wifi_aware_send_message_result_cb send_cb; void *send_cb_data; - wifi_aware_received_message_cb received_cb; + wifi_aware_message_received_cb received_cb; void *received_cb_data; guint received_signal_id; } wifi_aware_session_s; @@ -231,6 +231,7 @@ static wifi_aware_subscribe_s *__session_get_subscribe_config(wifi_aware_session static void __session_deregister_signals(wifi_aware_session_h session) { _wifi_aware_unset_service_discovered_cb(session); + _wifi_aware_unset_message_received_cb(session); } static void __enable_request_reply(GObject *src, GAsyncResult *res, gpointer user_data) @@ -305,6 +306,18 @@ static void __wifi_aware_discovered_cb(wifi_aware_session_h session, wifi_aware_ handle->discovered_cb_data); } +static void __wifi_aware_received_cb(wifi_aware_session_h session, wifi_aware_peer_h peer, + unsigned char *specific_info, size_t specific_info_len) +{ + RET_IF(session == NULL, "Session is NULL"); + wifi_aware_session_s *handle = (wifi_aware_session_s *)session; + + if (handle->received_cb) + handle->received_cb(session, peer, + specific_info, specific_info_len, + handle->received_cb_data); +} + static wifi_aware_error_e __get_dbus_result(GObject *src, GAsyncResult *res, unsigned int *id) { GDBusConnection *conn = G_DBUS_CONNECTION(src); @@ -739,6 +752,7 @@ int _wifi_aware_subscribe_request(wifi_aware_session_h session, wifi_aware_subscribe_h subscribe, wifi_aware_subscribed_cb callback, void *user_data) { + __WIFI_AWARE_FUNC_ENTER__; RET_VAL_IF(session == NULL, WIFI_AWARE_ERROR_INVALID_PARAMETER, "wifi_aware_session_h is NULL"); RET_VAL_IF(subscribe == NULL, WIFI_AWARE_ERROR_INVALID_PARAMETER, "wifi_aware_subscribe_h is NULL"); RET_VAL_IF(!__is_subscribe_session(session), WIFI_AWARE_ERROR_INVALID_OPERATION, @@ -768,6 +782,7 @@ int _wifi_aware_update_subscribe_request(wifi_aware_session_h session, wifi_aware_subscribe_h subscribe, wifi_aware_subscribed_cb callback, void *user_data) { + __WIFI_AWARE_FUNC_ENTER__; RET_VAL_IF(session == NULL, WIFI_AWARE_ERROR_INVALID_PARAMETER, "wifi_aware_session_h is NULL"); RET_VAL_IF(subscribe == NULL, WIFI_AWARE_ERROR_INVALID_PARAMETER, "wifi_aware_subscribe_h is NULL"); RET_VAL_IF(!__is_subscribe_session(session), WIFI_AWARE_ERROR_INVALID_OPERATION, @@ -797,6 +812,7 @@ int _wifi_aware_update_subscribe_request(wifi_aware_session_h session, static int __wifi_aware_cancel_publish_request(wifi_aware_session_h session) { + __WIFI_AWARE_FUNC_ENTER__; RET_VAL_IF(session == NULL, WIFI_AWARE_ERROR_INVALID_PARAMETER, "wifi_aware_session_h is NULL"); RET_VAL_IF(!__is_publish_session(session), WIFI_AWARE_ERROR_INVALID_OPERATION, "This session is not for Publish"); @@ -809,6 +825,7 @@ static int __wifi_aware_cancel_publish_request(wifi_aware_session_h session) static int __wifi_aware_cancel_subscribe_request(wifi_aware_session_h session) { + __WIFI_AWARE_FUNC_ENTER__; RET_VAL_IF(session == NULL, WIFI_AWARE_ERROR_INVALID_PARAMETER, "wifi_aware_session_h is NULL"); RET_VAL_IF(!__is_subscribe_session(session), WIFI_AWARE_ERROR_INVALID_OPERATION, "This session is not for Subscribe"); @@ -822,6 +839,7 @@ static int __wifi_aware_cancel_subscribe_request(wifi_aware_session_h session) int _wifi_aware_session_cancel(wifi_aware_session_h session) { + __WIFI_AWARE_FUNC_ENTER__; RET_VAL_IF(session == NULL, WIFI_AWARE_ERROR_INVALID_PARAMETER, "wifi_aware_session_h is NULL"); if (__is_publish_session(session)) { WIFI_AWARE_LOGD("Stop publish session"); @@ -848,10 +866,24 @@ static void __parsing_service_discovered_event(GVariant *parameters, g_free(info_value); } -static int __handle_new_peer(wifi_aware_s *aware_handle, unsigned int peer_id, wifi_aware_peer_h *peer) +static void __parsing_message_received_event(GVariant *parameters, unsigned int *peer_id, + unsigned char *specific_info, size_t *specific_info_len) +{ + gchar *info_value = NULL; + + g_variant_get(parameters, "(usq)", peer_id, + &info_value, specific_info_len); + + memcpy(specific_info, info_value, *specific_info_len); + g_free(info_value); +} + +static int __get_peer_handle(wifi_aware_s *aware_handle, unsigned int peer_id, wifi_aware_peer_h *peer) { + __WIFI_AWARE_FUNC_ENTER__; int ret = WIFI_AWARE_ERROR_NONE; - if (_wifi_aware_get_peer(aware_handle, peer_id) != NULL) { + *peer = _wifi_aware_get_peer(aware_handle, peer_id); + if (*peer != NULL) { WIFI_AWARE_LOGI("Peer %u already exists", peer_id); return WIFI_AWARE_ERROR_NONE; } @@ -871,7 +903,6 @@ static void __service_discovered_cb(GDBusConnection *connection, GVariant *parameters, gpointer user_data) { __WIFI_AWARE_FUNC_ENTER__; - WIFI_AWARE_LOGD("NAN Service is discovered"); unsigned int sub_id = 0; @@ -896,7 +927,7 @@ static void __service_discovered_cb(GDBusConnection *connection, } WIFI_AWARE_LOGD("subscribe ID: %u, Peer ID: %u", sub_id, peer_id); - if (__handle_new_peer(__session_get_wifi_aware(session), + if (__get_peer_handle(__session_get_wifi_aware(session), peer_id, &peer) != WIFI_AWARE_ERROR_NONE) { WIFI_AWARE_LOGE("Invalid peer %u", peer_id); return; @@ -905,23 +936,54 @@ static void __service_discovered_cb(GDBusConnection *connection, __wifi_aware_discovered_cb(session, peer, specific_info, specific_info_len); } +static void __message_received_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 Service is discovered"); + + unsigned int peer_id = 0; + unsigned char specific_info[WIFI_AWARE_MAX_SERVICE_SPECIFIC_INFO_LEN]; + size_t specific_info_len = 0; + + wifi_aware_session_h session = (wifi_aware_session_h)user_data; + wifi_aware_peer_h peer = NULL; + + RET_IF(!__is_subscribe_session(session), "This session is not for Subscribe"); + + __parsing_message_received_event(parameters, &peer_id, + specific_info, &specific_info_len); + + WIFI_AWARE_LOGD("Peer ID: %u", peer_id); + if (__get_peer_handle(__session_get_wifi_aware(session), + peer_id, &peer) != WIFI_AWARE_ERROR_NONE) { + WIFI_AWARE_LOGE("Invalid peer %u", peer_id); + return; + } + + __wifi_aware_received_cb(session, peer, specific_info, specific_info_len); +} + void _wifi_aware_set_service_discovered_cb(wifi_aware_session_h session, wifi_aware_service_discovered_cb callback, void *user_data) { __WIFI_AWARE_FUNC_ENTER__; + RET_IF(session == NULL, "wifi_aware_session_h is NULL"); RET_IF(!__is_subscribe_session(session), "This session is not for Subscribe"); wifi_aware_session_s *handle = (wifi_aware_session_s *)session; handle->discovered_cb = callback; handle->discovered_cb_data = user_data; handle->discovered_signal_id = - wifi_aware_gdbus_register_signal(SERVICE_DISCOVERED_SIGNAL, - __service_discovered_cb, session); + wifi_aware_gdbus_register_signal(SERVICE_DISCOVERED_SIGNAL, __service_discovered_cb, session); } void _wifi_aware_unset_service_discovered_cb(wifi_aware_session_h session) { __WIFI_AWARE_FUNC_ENTER__; + RET_IF(session == NULL, "wifi_aware_session_h is NULL"); RET_IF(!__is_subscribe_session(session), "This session is not for Subscribe"); wifi_aware_session_s *handle = (wifi_aware_session_s *)session; @@ -932,6 +994,32 @@ void _wifi_aware_unset_service_discovered_cb(wifi_aware_session_h session) handle->discovered_signal_id = 0; } +void _wifi_aware_set_message_received_cb(wifi_aware_session_h session, + wifi_aware_message_received_cb callback, void *user_data) +{ + __WIFI_AWARE_FUNC_ENTER__; + RET_IF(session == NULL, "wifi_aware_session_h is NULL"); + + wifi_aware_session_s *handle = (wifi_aware_session_s *)session; + handle->received_cb = callback; + handle->received_cb_data = user_data; + handle->received_signal_id = + wifi_aware_gdbus_register_signal(MESSAGE_RECEIVED_SIGNAL, __message_received_cb, session); +} + +void _wifi_aware_unset_message_received_cb(wifi_aware_session_h session) +{ + __WIFI_AWARE_FUNC_ENTER__; + RET_IF(session == NULL, "wifi_aware_session_h is NULL"); + + wifi_aware_session_s *handle = (wifi_aware_session_s *)session; + handle->received_cb = NULL; + handle->received_cb_data = NULL; + if (handle->received_signal_id > 0) + wifi_aware_gdbus_deregister_signal(handle->received_signal_id); + handle->received_signal_id = 0; +} + wifi_aware_message_s *__create_message_data(wifi_aware_session_h session, wifi_aware_send_message_result_cb cb, void *user_data) { @@ -953,6 +1041,7 @@ static void __wifi_aware_followup_invoke_callback(wifi_aware_message_s *message, static void __send_message_reply(GObject *src, GAsyncResult *res, gpointer user_data) { + __WIFI_AWARE_FUNC_ENTER__; GDBusConnection *conn = NULL; GError *dbus_error = NULL; GVariant *reply = NULL; diff --git a/src/wifi-aware.c b/src/wifi-aware.c index 4d73b97..ff725c6 100644 --- a/src/wifi-aware.c +++ b/src/wifi-aware.c @@ -529,13 +529,27 @@ API int wifi_aware_send_message(wifi_aware_session_h session, return _wifi_aware_send_message(session, peer, message, len, callback, user_data); } -API int wifi_aware_set_received_message_cb(wifi_aware_session_h session, - wifi_aware_received_message_cb callback, void *user_data) +API int wifi_aware_set_message_received_cb(wifi_aware_session_h session, + wifi_aware_message_received_cb callback, void *user_data) { __WIFI_AWARE_FUNC_ENTER__; CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_WIFI_AWARE); + RET_VAL_IF(session == NULL, WIFI_AWARE_ERROR_INVALID_PARAMETER, "wifi_aware_session_h is NULL"); + + _wifi_aware_set_message_received_cb(session, callback, user_data); __WIFI_AWARE_FUNC_EXIT__; return WIFI_AWARE_ERROR_NONE; } +API int wifi_aware_unset_message_received_cb(wifi_aware_session_h session) +{ + __WIFI_AWARE_FUNC_ENTER__; + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_WIFI_AWARE); + RET_VAL_IF(session == NULL, WIFI_AWARE_ERROR_INVALID_PARAMETER, "wifi_aware_session_h is NULL"); + + _wifi_aware_unset_message_received_cb(session); + + __WIFI_AWARE_FUNC_EXIT__; + return WIFI_AWARE_ERROR_NONE; +} -- 2.7.4