From: Cheoleun Moon Date: Wed, 4 Mar 2020 11:12:22 +0000 (+0900) Subject: Add followup test X-Git-Tag: submit/tizen/20200403.050222~2^2~47^2~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=05c8cb4de51fdf91302cc421cecaa03160cbdae2;p=platform%2Fcore%2Fapi%2Fwifi-aware.git Add followup test --- diff --git a/src/wifi-aware-gdbus.c b/src/wifi-aware-gdbus.c index 97637df..dcb20db 100644 --- a/src/wifi-aware-gdbus.c +++ b/src/wifi-aware-gdbus.c @@ -361,7 +361,7 @@ static GVariant *__create_followup_request_param(int client_id, uint16_t pub_sub g_variant_builder_add(builder, "{sv}", "message_len", g_variant_new("q", len)); g_variant_builder_add(builder, "{sv}", "message", __pack_byte_array(message, len)); - params = g_variant_new("(iuua{sv})", client_id, pub_sub_id, peer_id, builder); + params = g_variant_new("(iqua{sv})", client_id, pub_sub_id, peer_id, builder); g_variant_builder_unref(builder); return params; } diff --git a/src/wifi-aware-private.c b/src/wifi-aware-private.c index bd15897..a3744bd 100644 --- a/src/wifi-aware-private.c +++ b/src/wifi-aware-private.c @@ -855,7 +855,7 @@ static void __parsing_message_received_event(GVariant *parameters, unsigned int g_free(value); } -static int __get_peer_handle(unsigned int peer_id, wifi_aware_peer_h *peer) +static int __get_peer_by_handle(unsigned int peer_id, wifi_aware_peer_h *peer) { __WIFI_AWARE_FUNC_ENTER__; int ret = WIFI_AWARE_ERROR_NONE; @@ -903,7 +903,7 @@ static void __service_discovered_cb(GDBusConnection *connection, } WIFI_AWARE_LOGD("subscribe ID: %u, Peer ID: %u", sub_id, peer_id); - if (__get_peer_handle(peer_id, &peer) != WIFI_AWARE_ERROR_NONE) { + if (__get_peer_by_handle(peer_id, &peer) != WIFI_AWARE_ERROR_NONE) { WIFI_AWARE_LOGE("Invalid peer %u", peer_id); return; } @@ -932,7 +932,7 @@ static void __message_received_cb(GDBusConnection *connection, message, &message_len); WIFI_AWARE_LOGD("Peer ID: %u", peer_id); - if (__get_peer_handle(peer_id, &peer) != WIFI_AWARE_ERROR_NONE) { + if (__get_peer_by_handle(peer_id, &peer) != WIFI_AWARE_ERROR_NONE) { WIFI_AWARE_LOGE("Invalid peer %u", peer_id); return; } diff --git a/test/wifi-aware-test.c b/test/wifi-aware-test.c index c9419d4..053a3d1 100644 --- a/test/wifi-aware-test.c +++ b/test/wifi-aware-test.c @@ -94,6 +94,14 @@ #define MAX_SPECIFIC_INFO_LEN 1024 #define MAX_MATCH_FILTER_LEN 255 +typedef struct { + int peer_id; + wifi_aware_peer_h peer; +} peer_info; + +static GSList *g_peer_list = NULL; +static int g_peer_id = 0; + enum { CMD_QUIT, CMD_FULL_MENU, @@ -111,6 +119,9 @@ enum { CMD_CANCEL_SUBSCRIBE, CMD_TEST_SPECIFIC_INFO, + CMD_MESSAGE_RECV_CB, + CMD_SEND_MESSAGE, + CMD_INVALID, }; @@ -146,6 +157,10 @@ static char *g_menu_str[] = { [CMD_TEST_SPECIFIC_INFO] = "TEST SPECIFIC INFO", + [CMD_MESSAGE_RECV_CB] + = "SET MESSAGE_RECV CB", + [CMD_SEND_MESSAGE] + = "SEND MESSAGE", [CMD_INVALID] = NULL, }; @@ -527,15 +542,73 @@ static void __subscribed_cb(wifi_aware_session_h session, printf("Subscirbe Request fails. Error: %s\n", __print_error(error)); } +static int __equal_peer_handle(gconstpointer a, gconstpointer b) +{ + peer_info *info = (peer_info *)a; + wifi_aware_peer_h peer = (wifi_aware_peer_h) b; + return info->peer - peer; +} + +static void __add_new_peer(wifi_aware_peer_h peer) +{ + GSList *list = (GSList *)g_slist_find_custom(g_peer_list, + (gconstpointer)peer, (GCompareFunc)__equal_peer_handle); + + if (list) { + printf("Peer %p already exists\n", peer); + return; + } + + printf("New Peer %p is dicovered", peer); + peer_info *info = (peer_info *)g_try_malloc0(sizeof(peer_info)); + info->peer_id = g_peer_id++; + info->peer = peer; + g_peer_list = g_slist_append(g_peer_list, info); +} + +static void __print_peer_info(gpointer data, gpointer user_data) +{ + peer_info *info = (peer_info *)data; + printf("%02d %p\n", info->peer_id, info->peer); +} + +static void __display_peer_list() +{ + printf("------- Peer List -------\n"); + g_slist_foreach(g_peer_list, (GFunc)__print_peer_info, NULL); +} + +static int __equal_peer_id(gconstpointer a, gconstpointer b) +{ + peer_info *info = (peer_info *)a; + int peer_id = GPOINTER_TO_INT(b); + printf("a: %d, b: %d\n", info->peer_id, peer_id); + return (info->peer_id - peer_id); +} + +static wifi_aware_peer_h __get_peer_by_id(int peer_id) +{ + GSList *list = (GSList *)g_slist_find_custom(g_peer_list, + GINT_TO_POINTER(peer_id), (GCompareFunc)__equal_peer_id); + if (!list) { + printf("No Peer %d ", peer_id); + return NULL; + } + peer_info *info = list->data; + return info->peer; +} + static void __service_discovered_cb(wifi_aware_session_h session, wifi_aware_peer_h peer, const unsigned char *service_specific_info, size_t service_specific_info_len, void *user_data) { - printf("Find A Wi-Fi Aware Peer\n"); + printf("Find A Wi-Fi Aware Peer %p\n", peer); // printf("Subscribe ID: %u\n", subscribe_id); - printf("Service Specific Info\n"); + printf("Service Specific Info: "); __print_byte_array(service_specific_info, service_specific_info_len); + printf("\n"); + __add_new_peer(peer); } static void __set_subscribe_config(wifi_aware_subscribe_h subscribe) @@ -675,6 +748,53 @@ void test_specific_info() } } +void __message_result_cb(wifi_aware_session_h session, wifi_aware_error_e error, void *user_data) +{ + wifi_aware_peer_h peer = (wifi_aware_peer_h) user_data; + if (error == WIFI_AWARE_ERROR_NONE) + printf("Success to send message to %p", peer); + else + printf("Fail to send message to %p: %d", peer, error); +} + +static void __message_received_cb(wifi_aware_session_h session, wifi_aware_peer_h peer, + const unsigned char *message, size_t len, void *user_data) +{ + printf("Receive message from a Wi-Fi Aware Peer %p\n", peer); + printf("Message: "); + __print_byte_array(message, len); + printf("\n"); + __add_new_peer(peer); +} + + +void test_message_recv_cb() +{ + int ret = wifi_aware_set_message_received_cb(g_wifi_aware_session, __message_received_cb, NULL); + __print_result(ret, "wifi_aware_set_message_received_cb"); +} + +void test_send_message() +{ + int peer_id; + unsigned char message[4] = {'T', 'E', 'S', 'T'}; + size_t len = 4; + + RET_IF_LOOP_IS_NULL(); + + __display_peer_list(); + printf("Select a peer to send message: "); + if (scanf("%d", &peer_id) < 0) + return; + + wifi_aware_peer_h peer = __get_peer_by_id(peer_id); + printf("Selected peer: %p", peer); + + int ret = wifi_aware_send_message(g_wifi_aware_session, peer, message, len, __message_result_cb, peer); + __print_result(ret, "wifi_aware_send_message"); + printf("Send a message to %p", peer); +} + typedef void (*test_func)(void); test_func g_menu_func[] = { [CMD_QUIT] = test_quit, @@ -689,6 +809,8 @@ test_func g_menu_func[] = { [CMD_SUBSCRIBE] = test_subscribe, [CMD_CANCEL_SUBSCRIBE] = test_cancel_subscribe, [CMD_TEST_SPECIFIC_INFO] = test_specific_info, + [CMD_MESSAGE_RECV_CB] = test_message_recv_cb, + [CMD_SEND_MESSAGE] = test_send_message, [CMD_INVALID] = NULL, };