From: Cheoleun Moon Date: Mon, 17 Feb 2020 10:34:15 +0000 (+0900) Subject: Handle new peer X-Git-Tag: submit/tizen/20200403.050222~2^2~64^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=95c6840d48a58a17e538261cebbf082393b5500f;p=platform%2Fcore%2Fapi%2Fwifi-aware.git Handle new peer --- diff --git a/include/wifi-aware.h b/include/wifi-aware.h index ccdcbef..4bd6cdd 100644 --- a/include/wifi-aware.h +++ b/include/wifi-aware.h @@ -539,6 +539,7 @@ int wifi_aware_set_received_message_cb(wifi_aware_h wifi_aware, // 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-config.h b/src/include/wifi-aware-config.h index fa3a016..8ec749d 100644 --- a/src/include/wifi-aware-config.h +++ b/src/include/wifi-aware-config.h @@ -14,8 +14,8 @@ * limitations under the License. * */ -#ifndef __TIZEN_NET_WIFI_AWARE_CONN_H__ -#define __TIZEN_NET_WIFI_AWARE_CONN_H__ +#ifndef __TIZEN_NET_WIFI_AWARE_CONFIG_H__ +#define __TIZEN_NET_WIFI_AWARE_CONFIG_H__ #include @@ -65,4 +65,4 @@ typedef struct { #ifdef __cplusplus } #endif -#endif /* __TIZEN_NET_WIFI_AWARE_CONN_H__ */ +#endif /* __TIZEN_NET_WIFI_AWARE_CONFIG_H__ */ diff --git a/src/include/wifi-aware-gdbus.h b/src/include/wifi-aware-gdbus.h index 5524c10..dddd0c1 100644 --- a/src/include/wifi-aware-gdbus.h +++ b/src/include/wifi-aware-gdbus.h @@ -21,6 +21,7 @@ #include #include +#include "wifi-aware.h" #include "wifi-aware-config.h" #ifdef __cplusplus diff --git a/src/include/wifi-aware-handle.h b/src/include/wifi-aware-handle.h deleted file mode 100644 index 1a68921..0000000 --- a/src/include/wifi-aware-handle.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * -*/ -#ifndef __TIZEN_NET_WIFI_AWARE_HANDLE_H__ -#define __TIZEN_NET_WIFI_AWARE_HANDLE_H__ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef __cplusplus -} -#endif - -#endif /* __TIZEN_NET_WIFI_AWARE_HANDLE_H__ */ diff --git a/src/include/wifi-aware-peer.h b/src/include/wifi-aware-peer.h index c3ca648..8c35435 100644 --- a/src/include/wifi-aware-peer.h +++ b/src/include/wifi-aware-peer.h @@ -19,11 +19,16 @@ #include +#include "wifi-aware.h" + #ifdef __cplusplus extern "C" { #endif int _wifi_aware_peer_create(wifi_aware_peer_h *peer, unsigned int peer_id); +int _wifi_aware_peer_add(wifi_aware_h wifi_aware, wifi_aware_peer_h peer); +int _wifi_aware_peer_remove(wifi_aware_h wifi_aware, wifi_aware_peer_h peer); +wifi_aware_peer_h _wifi_aware_peer_get_handle(wifi_aware_h wifi_aware, unsigned int peer_id); #ifdef __cplusplus } diff --git a/src/include/wifi-aware-private.h b/src/include/wifi-aware-private.h index 21aa696..0e8e8ee 100644 --- a/src/include/wifi-aware-private.h +++ b/src/include/wifi-aware-private.h @@ -22,7 +22,6 @@ #include "wifi-aware.h" #include "wifi-aware-config.h" -#include "wifi-aware-log.h" #ifdef __cplusplus extern "C" { @@ -32,6 +31,13 @@ extern "C" { #define WIFI_AWARE_HANDLE_LOCK #define WIFI_AWARE_HANDLE_UNLOCK +typedef struct { + int client_id; + wifi_aware_enabled_cb enabled_cb; + void *enabled_cb_data; + GHashTable *peer_map; +} wifi_aware_s; + const char *_wifi_aware_convert_error_type_to_string(wifi_aware_error_e err); int _wifi_aware_handle_create(wifi_aware_h *wifi_aware); diff --git a/src/include/wifi-aware-utils.h b/src/include/wifi-aware-utils.h index 4c25271..a970d22 100644 --- a/src/include/wifi-aware-utils.h +++ b/src/include/wifi-aware-utils.h @@ -21,7 +21,6 @@ #include #include "wifi-aware-log.h" -#include "wifi-aware-private.h" #ifdef __cplusplus extern "C" { diff --git a/src/wifi-aware-gdbus.c b/src/wifi-aware-gdbus.c index 48dfe6d..788da45 100644 --- a/src/wifi-aware-gdbus.c +++ b/src/wifi-aware-gdbus.c @@ -26,7 +26,6 @@ #include #include "wifi-aware-log.h" -#include "wifi-aware.h" #include "wifi-aware-private.h" #include "wifi-aware-utils.h" #include "wifi-aware-gdbus.h" diff --git a/src/wifi-aware-peer.c b/src/wifi-aware-peer.c index c144bf5..0b94164 100644 --- a/src/wifi-aware-peer.c +++ b/src/wifi-aware-peer.c @@ -18,12 +18,73 @@ #include -#include "wifi-aware.h" #include "wifi-aware-log.h" +#include "wifi-aware-private.h" #include "wifi-aware-peer.h" +#include "wifi-aware-utils.h" + +typedef struct { + unsigned int id; +} wifi_aware_peer_s; int _wifi_aware_peer_create(wifi_aware_peer_h *peer, unsigned int peer_id) { + *peer = (wifi_aware_peer_s *)g_try_malloc0(sizeof(wifi_aware_peer_s)); + RET_VAL_IF(*peer == NULL, WIFI_AWARE_ERROR_OUT_OF_MEMORY, "g_try_malloc0 failed"); + return WIFI_AWARE_ERROR_NONE; +} + +int _wifi_aware_peer_add(wifi_aware_h wifi_aware, wifi_aware_peer_h peer) +{ + wifi_aware_s *aware_handle = (wifi_aware_s *)wifi_aware; + GHashTable *peer_map = aware_handle->peer_map; + wifi_aware_peer_s *peer_handle = (wifi_aware_peer_s *)peer; + RET_VAL_IF(peer_handle == NULL, WIFI_AWARE_ERROR_INVALID_PARAMETER, "peer is NULL"); + + WIFI_AWARE_HANDLE_LOCK; + if (g_hash_table_contains(peer_map, GINT_TO_POINTER(peer_handle->id))) { + WIFI_AWARE_LOGE("peer_id %d alread exists", peer_handle->id); + WIFI_AWARE_HANDLE_UNLOCK; + return WIFI_AWARE_ERROR_INVALID_PARAMETER; + } + WIFI_AWARE_HANDLE_UNLOCK; + + WIFI_AWARE_HANDLE_LOCK; + g_hash_table_insert(peer_map, GINT_TO_POINTER(peer_handle->id), peer); + WIFI_AWARE_HANDLE_UNLOCK; // Check peer_id validation return WIFI_AWARE_ERROR_NONE; } + +int _wifi_aware_peer_remove(wifi_aware_h wifi_aware, wifi_aware_peer_h peer) +{ + wifi_aware_s *aware_handle = (wifi_aware_s *)wifi_aware; + GHashTable *peer_map = aware_handle->peer_map; + wifi_aware_peer_s *peer_handle = (wifi_aware_peer_s *)peer; + RET_VAL_IF(peer_handle == NULL, WIFI_AWARE_ERROR_INVALID_PARAMETER, "peer is NULL"); + + if (peer_handle == NULL) { + WIFI_AWARE_LOGE("peer is NULL"); + return WIFI_AWARE_ERROR_INVALID_PARAMETER; + } + + WIFI_AWARE_HANDLE_LOCK; + if (!g_hash_table_remove(peer_map, GINT_TO_POINTER(peer_handle->id))) { + WIFI_AWARE_LOGE("peer %d doesn't exist", peer_handle->id); + } + WIFI_AWARE_HANDLE_UNLOCK; + + return WIFI_AWARE_ERROR_NONE; +} + +wifi_aware_peer_h _wifi_aware_peer_get_handle(wifi_aware_h wifi_aware, unsigned int peer_id) +{ + wifi_aware_s *aware_handle = (wifi_aware_s *)wifi_aware; + GHashTable *peer_map = aware_handle->peer_map; + wifi_aware_peer_s *peer = NULL; + + WIFI_AWARE_HANDLE_LOCK; + peer = g_hash_table_lookup(peer_map, GINT_TO_POINTER(peer_id)); + WIFI_AWARE_HANDLE_UNLOCK; + return peer; +} diff --git a/src/wifi-aware-private.c b/src/wifi-aware-private.c index 2f855a1..9589ba4 100644 --- a/src/wifi-aware-private.c +++ b/src/wifi-aware-private.c @@ -26,8 +26,6 @@ #include -#include "wifi-aware.h" - #include "wifi-aware-gdbus.h" #include "wifi-aware-log.h" #include "wifi-aware-peer.h" @@ -55,12 +53,6 @@ enum { } nan_manager_error_e; typedef struct { - int client_id; - wifi_aware_enabled_cb enabled_cb; - void *enabled_cb_data; -} wifi_aware_s; - -typedef struct { wifi_aware_s *aware_handle; uint32_t publish_id; wifi_aware_publish_request_s req; @@ -331,16 +323,19 @@ static void __subscribe_request_reply(GObject *src, GAsyncResult *res, gpointer int _wifi_aware_handle_create(wifi_aware_h *wifi_aware) { - *wifi_aware = g_try_malloc0(sizeof(wifi_aware_s)); - if (*wifi_aware == NULL) { + wifi_aware_s *aware = (wifi_aware_s *)g_try_malloc0(sizeof(wifi_aware_s)); + if (aware == NULL) { WIFI_AWARE_LOGE("g_try_malloc0 failed"); return WIFI_AWARE_ERROR_OUT_OF_MEMORY; } + aware->peer_map = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free); WIFI_AWARE_HANDLE_LOCK; - __wifi_aware_handle_list = g_list_append(__wifi_aware_handle_list, wifi_aware); + __wifi_aware_handle_list = g_list_append(__wifi_aware_handle_list, aware); WIFI_AWARE_HANDLE_UNLOCK; + *wifi_aware = aware; + WIFI_AWARE_LOGI("Crate new handle [%p]", *wifi_aware); return WIFI_AWARE_ERROR_NONE; } @@ -632,6 +627,23 @@ 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) +{ + int ret = WIFI_AWARE_ERROR_NONE; + if (_wifi_aware_peer_get_handle(aware_handle, peer_id) != NULL) { + WIFI_AWARE_LOGI("Peer %u already exists", peer_id); + return WIFI_AWARE_ERROR_NONE; + } + + ret = _wifi_aware_peer_create(peer, peer_id); + RET_VAL_IF(ret != WIFI_AWARE_ERROR_NONE, ret, "Fail to create peer"); + + ret = _wifi_aware_peer_add(aware_handle, *peer); + RET_VAL_IF(ret != WIFI_AWARE_ERROR_NONE, ret, "Fail to add peer"); + + return ret; +} + static void __service_discovered_cb(GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface, const gchar *signal, @@ -661,13 +673,11 @@ static void __service_discovered_cb(GDBusConnection *connection, } WIFI_AWARE_LOGD("subscribe ID: %u, Peer ID: %u", sub_id, peer_id); - if (_wifi_aware_peer_create(&peer, peer_id) != WIFI_AWARE_ERROR_NONE) { - WIFI_AWARE_LOGE("Fail to create peer"); + if (__handle_new_peer(handle->aware_handle, peer_id, &peer) != WIFI_AWARE_ERROR_NONE) { + WIFI_AWARE_LOGE("Invalid peer %u", peer_id); return; } - // TODO: Add peer list to wifi_aware_s - if (handle->discovered_cb) handle->discovered_cb(handle, peer, specific_info, specific_info_len, diff --git a/src/wifi-aware.c b/src/wifi-aware.c index 63f1a38..7715f7a 100644 --- a/src/wifi-aware.c +++ b/src/wifi-aware.c @@ -48,7 +48,7 @@ API int wifi_aware_initialize(wifi_aware_h *wifi_aware) RET_VAL_IF(ret != WIFI_AWARE_ERROR_NONE, ret, "_wifi_aware_handle_create failed [%s]", _wifi_aware_convert_error_type_to_string(ret)); - ret = _wifi_aware_init(wifi_aware); + ret = _wifi_aware_init(); RET_VAL_IF(ret != WIFI_AWARE_ERROR_NONE, ret, "_wifi_aware_init failed [%s]", _wifi_aware_convert_error_type_to_string(ret)); @@ -93,8 +93,9 @@ API int wifi_aware_publish_create(wifi_aware_h wifi_aware, wifi_aware_publish_h ret = _wifi_aware_publish_handle_create(wifi_aware, publish); RET_VAL_IF(ret != WIFI_AWARE_ERROR_NONE, ret, "Fail to create publish handle"); + WIFI_AWARE_LOGD("publish handle: %p", *publish); - _wifi_aware_publish_request_set_default_values(publish); + _wifi_aware_publish_request_set_default_values(*publish); __WIFI_AWARE_FUNC_EXIT__; return WIFI_AWARE_ERROR_NONE; @@ -279,8 +280,9 @@ API int wifi_aware_subscribe_create(wifi_aware_h wifi_aware, ret = _wifi_aware_subscribe_handle_create(wifi_aware, subscribe); RET_VAL_IF(ret != WIFI_AWARE_ERROR_NONE, ret, "Fail to create subscribe handle"); + WIFI_AWARE_LOGD("subscribe handle: %p", *subscribe); - _wifi_aware_subscribe_set_default_values(subscribe); + _wifi_aware_subscribe_set_default_values(*subscribe); __WIFI_AWARE_FUNC_EXIT__; return WIFI_AWARE_ERROR_NONE;