Add test programs for publish/subscribe
authorCheoleun Moon <chleun.moon@samsung.com>
Wed, 18 Mar 2020 11:53:20 +0000 (20:53 +0900)
committerCheoleun Moon <chleun.moon@samsung.com>
Wed, 18 Mar 2020 11:53:20 +0000 (20:53 +0900)
include/wifi-aware.h
packaging/capi-network-wifi-aware.spec
test/CMakeLists.txt
test/wifi-aware-publish-test.c [new file with mode: 0644]
test/wifi-aware-subscribe-test.c [new file with mode: 0644]
test/wifi-aware-test.c

index 9b2b2d1..81ee76c 100644 (file)
@@ -413,7 +413,7 @@ int wifi_aware_session_destroy(wifi_aware_session_h session);
  * @param[in] reason
  * @param[in] user_data
  */
-typedef(*wifi_aware_session_terminated_cb)(wifi_aware_session_h session,
+typedef void(*wifi_aware_session_terminated_cb)(wifi_aware_session_h session,
                wifi_aware_termination_reason_e reason, void *user_data);
 
 /**
index 6332793..8f31329 100644 (file)
@@ -89,3 +89,5 @@ rm -rf %{buildroot}
 %files test
 %manifest wifi-aware-test.manifest
 %attr(755,root,root) %{_bindir}/wifi-aware-test
+%attr(755,root,root) %{_bindir}/wifi-aware-publish-test
+%attr(755,root,root) %{_bindir}/wifi-aware-subscribe-test
index 1840ca8..47b0949 100644 (file)
@@ -16,7 +16,6 @@
 #
 
 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
-PROJECT(wifi-aware-test C)
 
 SET(WIFI_AWARE_TEST_INCLUDE_DIR ${WIFI_AWARE_TEST_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../include)
 INCLUDE_DIRECTORIES(${WIFI_AWARE_TEST_INCLUDE_DIR})
@@ -29,15 +28,16 @@ FOREACH(flag ${TARGET_WIFI_AWAREs_TEST_CFLAGS})
 ENDFOREACH()
 
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -fPIE")
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
 
 aux_source_directory(. sources)
 FOREACH(src ${sources})
        GET_FILENAME_COMPONENT(src_name ${src} NAME_WE)
        MESSAGE("${src_name}")
-       SET(WIFI_AWARE_TEST_SRCS ${WIFI_AWARE_TEST_SRCS} ${src})
+       ADD_EXECUTABLE(${src_name} ${src})
+       TARGET_LINK_LIBRARIES(${src_name} ${TARGET_WIFI_AWARE} ${TARGET_WIFI_AWARE_TEST_LDFLAGS})
 ENDFOREACH()
 
-ADD_EXECUTABLE(${PROJECT_NAME} ${WIFI_AWARE_TEST_SRCS})
-SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
-TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${TARGET_WIFI_AWARE} ${TARGET_WIFI_AWARE_TEST_LDFLAGS})
-INSTALL(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${BIN_DIR})
+INSTALL(TARGETS wifi-aware-test RUNTIME DESTINATION ${BIN_DIR})
+INSTALL(TARGETS wifi-aware-publish-test RUNTIME DESTINATION ${BIN_DIR})
+INSTALL(TARGETS wifi-aware-subscribe-test RUNTIME DESTINATION ${BIN_DIR})
diff --git a/test/wifi-aware-publish-test.c b/test/wifi-aware-publish-test.c
new file mode 100644 (file)
index 0000000..152b108
--- /dev/null
@@ -0,0 +1,603 @@
+/*
+ * Wi-Fi Aware
+ *
+ * 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.
+ *
+ */
+
+/**
+ * This file implements Wi-Fi Aware test executable.
+ *
+ * @file        wifi-aware-test.c
+ * @author      Jiung Yu (jiung.yu@samsung.com)
+ */
+
+
+/*****************************************************************************
+ *  Standard headers
+ *****************************************************************************/
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <glib.h>
+
+/*****************************************************************************
+ *  System headers
+ *****************************************************************************/
+#include <system_info.h>
+
+/*****************************************************************************
+ *  Wi-Fi Aware library headers
+ *****************************************************************************/
+#include "wifi-aware.h"
+
+/*****************************************************************************
+ *  Intelligent Network Monitoring(WIFI_AWARE) test local headers
+ *****************************************************************************/
+#include "wifi-aware-test.h"
+
+/*****************************************************************************
+ *  Macros and Typedefs
+ *****************************************************************************/
+
+#define RESET_COLOR "\e[m"
+#define MAKE_RED "\e[31m"
+#define MAKE_GREEN "\e[32m"
+
+#define __FUNC_ENTER__ printf("%s() entering...\n", __func__)
+#define __FUNC_EXIT__ printf("%s() leaving...\n", __func__)
+
+#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
+#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
+
+#define MAX_INIT_NUM 1024
+
+#define RET_IF_LOOP_IS_NULL()\
+       do {\
+               if (!g_main_loop_p) {\
+                       printf("Loop was not initialized\n");\
+                       return;\
+               } \
+       } while (0)
+
+#define SERVICE_NAME "CtsVerifierTestService"
+#define PUB_SSI "Extra bytes in the publisher discovery"
+#define MSG_PUB_TO_SUB "Ready"
+#define PORT 1234
+
+#define MAX_SERVICE_NAME_LEN 255
+#define MAX_SPECIFIC_INFO_LEN 1024
+
+#define MAX_PSK_LEN 63
+#define PSK "AABBCCDDEE"
+#define IPV6_ADDRESS_LEN 39
+
+enum {
+       CMD_QUIT,
+       CMD_FULL_MENU,
+
+       CMD_OPEN_UNSOLICITED,
+       CMD_OPEN_SOLICITED,
+       CMD_PSK_UNSOLICITED,
+       CMD_PSK_SOLICITED,
+
+       CMD_INVALID,
+};
+
+static char *g_menu_str[] = {
+       [CMD_QUIT]
+               = "Quit",
+       [CMD_FULL_MENU]
+               = "Full Menu",
+       [CMD_OPEN_UNSOLICITED]
+               = "Open + Unsolicited",
+       [CMD_OPEN_SOLICITED]
+               = "Open + Solicited",
+       [CMD_PSK_UNSOLICITED]
+               = "PSK + Unsolicited",
+       [CMD_PSK_SOLICITED]
+               = "PSK + Solicited",
+       [CMD_INVALID]
+               = NULL,
+};
+
+static GMainLoop *g_main_loop_p;
+static bool g_initialized = false;
+
+static wifi_aware_session_h g_wifi_aware_session = NULL;
+static wifi_aware_publish_h g_publish_handle = NULL;
+static wifi_aware_data_path_h g_ndp = NULL;
+static wifi_aware_peer_h g_peer = NULL;
+
+static wifi_aware_publish_type_e g_pub_type;
+static wifi_aware_security_type_e g_security_type;
+
+static inline void __usage_full()
+{
+       int i;
+       printf("\nPublish Test\n");
+       for (i = CMD_QUIT; i < CMD_INVALID; i++)
+               printf(" %02d: %s\n", i, g_menu_str[i]);
+}
+
+static inline int __is_digit(const char* str)
+{
+       int len;
+       int i;
+
+       if (str == NULL)
+               return -1;
+
+       if (strlen(str) == 0)
+               return -1;
+
+       len = strlen(str);
+       for (i = 0; i < len; i++)       {
+               if (str[i] < '0' || str[i] > '9')
+                       return -2;
+       }
+
+       return 0;
+}
+
+const char *__print_error(wifi_aware_error_e err)
+{
+       switch (err) {
+       case WIFI_AWARE_ERROR_NOT_PERMITTED:
+               return "NOT_PERMITTED";
+       case WIFI_AWARE_ERROR_OUT_OF_MEMORY:
+               return "OUT_OF_MEMORY";
+       case WIFI_AWARE_ERROR_PERMISSION_DENIED:
+               return "PERMISSION_DENIED";
+       case WIFI_AWARE_ERROR_RESOURCE_BUSY:
+               return "RESOURCE_BUSY";
+       case WIFI_AWARE_ERROR_INVALID_PARAMETER:
+               return "INVALID_PARAMETER";
+       case WIFI_AWARE_ERROR_NOW_IN_PROGRESS:
+               return "NOW_IN_PROGRESS";
+       case WIFI_AWARE_ERROR_NOT_SUPPORTED:
+               return "NOT_SUPPORTED";
+       case WIFI_AWARE_ERROR_OPERATION_FAILED:
+               return "OPERATION_FAILED";
+       case WIFI_AWARE_ERROR_NOT_INITIALIZED:
+               return "NOT_INITIALIZED";
+       case WIFI_AWARE_ERROR_ALREADY_INITIALIZED:
+               return "ALREADY_INITIALIZED";
+       default:
+               return "UNKNOWN";
+       }
+}
+
+static void __print_result(int ret, gchar *msg)
+{
+    if (ret == WIFI_AWARE_ERROR_NONE) {
+        printf(MAKE_GREEN"success to %s"RESET_COLOR"\n", msg);
+    } else {
+        printf(MAKE_RED"fail to %s: %s ", msg, __print_error(ret));
+        printf(RESET_COLOR"\n");
+    }
+}
+
+static void __print_mac(unsigned char mac[6])
+{
+       printf(MACSTR, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+}
+
+void __print_byte_array(const unsigned char *data, size_t len)
+{
+       const unsigned char *ptr = data;
+       const unsigned char *max = data + len;
+       while (ptr < max) {
+               if (*ptr == '\\')
+                       printf("\\\\\\\\");
+               else if (*ptr >= ' ')
+                       printf("%c", *ptr);
+               else
+                       printf("\\\\x%02X", *ptr);
+               ptr++;
+       }   
+    printf("\n");
+}
+
+void test_full_menu()
+{
+       __usage_full();
+
+       return;
+}
+
+void test_quit()
+{
+       RET_IF_LOOP_IS_NULL();
+
+       printf("Bye\n");
+       g_main_loop_quit(g_main_loop_p);
+
+       return;
+}
+
+static void __published_cb(wifi_aware_session_h session,
+               wifi_aware_error_e error, void *user_data)
+{
+       if (error == WIFI_AWARE_ERROR_NONE) {
+               printf("Publish Request is success.\n");
+       }
+       else {
+               printf("Publish Request fails. Error: %s\n", __print_error(error));
+               return;
+       }
+}
+
+static void __data_path_terminated_cb(wifi_aware_data_path_h data_path,
+               wifi_aware_termination_reason_e reason, void *user_data)
+{
+       printf("\n>>Data path(%p) is terminated.", data_path);
+       if (data_path != NULL) {
+               wifi_aware_data_path_unset_terminated_cb(data_path);
+               wifi_aware_data_path_destroy(data_path);
+       }
+}
+
+static void __open_cb(wifi_aware_data_path_h data_path, wifi_aware_error_e error, void *user_data)
+{
+       if (error == WIFI_AWARE_ERROR_NONE) {
+               int ret = WIFI_AWARE_ERROR_NONE;
+               char *interface;
+               printf("\n>>Data path is established. NDP: %p\n", data_path);
+               ret = wifi_aware_data_path_get_interface(data_path, &interface);
+               if (ret == WIFI_AWARE_ERROR_NONE)
+                       printf("Interface: %s\n", interface);
+               else
+                       printf("Fail to get interface\n");
+       }
+       else {
+               printf("\n>>Fail to open Data path. data path: %p\n", data_path);
+       }
+
+}
+
+static void __open_data_path(wifi_aware_session_h session)
+{
+       printf("\n>>Open Data Path\n");
+       if (g_peer == NULL) {
+               printf("Peer is NULL\n");
+               return;
+       }
+
+       int ret = wifi_aware_data_path_create(session, g_peer, &g_ndp);
+       if (ret != WIFI_AWARE_ERROR_NONE) {
+               printf("Fail to create data path: %s\n", __print_error(ret));
+               return;
+       }
+       printf("data path: %p\n", g_ndp);
+
+       ret = wifi_aware_data_path_set_terminated_cb(g_ndp, __data_path_terminated_cb, NULL);
+       if (ret != WIFI_AWARE_ERROR_NONE) {
+               printf("Fail to create data path: %s\n", __print_error(ret));
+               return;
+       }
+
+       ret = wifi_aware_data_path_set_security(g_ndp, g_security_type);
+       if (ret != WIFI_AWARE_ERROR_NONE) {
+               printf("Fail to set security type for data path: %s\n", __print_error(ret));
+               return;
+       }
+       printf("security type: ");
+       switch (g_security_type) {
+       case WIFI_AWARE_SECURITY_TYPE_OPEN:
+               printf("Open\n");
+               break;
+       case WIFI_AWARE_SECURITY_TYPE_PSK:
+               printf("PSK\n");
+               ret = wifi_aware_data_path_set_psk(g_ndp, PSK);
+               break;
+       case WIFI_AWARE_SECURITY_TYPE_PMK:
+               printf("PMK\n");
+               break;
+       default:
+               printf("Unknown security type\n");
+               return;
+       }
+
+       ret = wifi_aware_data_path_set_port(g_ndp, PORT);
+       if (ret != WIFI_AWARE_ERROR_NONE)
+               printf("Fail to set port for data path: %s\n", __print_error(ret));
+       else
+               printf("port: %d\n", PORT);
+
+       ret = wifi_aware_data_path_open(g_ndp, __open_cb, NULL);
+       if (ret != WIFI_AWARE_ERROR_NONE) {
+               printf("Fail to open data path: %s\n", __print_error(ret));
+       }
+}
+
+static void __message_result_cb(wifi_aware_session_h session, wifi_aware_error_e error, void *user_data)
+{
+       printf("\n>>Result of sending message: %s\n", __print_error(error));
+       __open_data_path(session);
+}
+
+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)
+{
+       int ret = 0;
+       char buf[1024] = {0, };
+       unsigned char *mac = NULL;
+
+       printf("\n>>Receive message from a Wi-Fi Aware Peer\n");
+
+       memcpy(buf, message, 1024);
+       if (wifi_aware_peer_get_mac(peer, &mac) != WIFI_AWARE_ERROR_NONE)
+               printf("Fail to get Peer's NMI: %s\n", __print_error(ret));
+
+       printf("Message: %s\n", buf);
+       printf("Peer's NMI: ");
+       if (mac != NULL) {
+               __print_mac(mac);
+               g_free(mac);
+       }
+       printf("\n");
+
+       g_peer = peer;
+       printf("\n>>Send message\n");
+       unsigned char send_message[1024] = {0, };
+       size_t message_len = strlen(MSG_PUB_TO_SUB);
+       memcpy(send_message, MSG_PUB_TO_SUB, message_len);
+       ret = wifi_aware_send_message(session, peer, send_message, message_len, __message_result_cb, NULL);
+       if (ret != WIFI_AWARE_ERROR_NONE)
+               printf("Fail to send message: %s\n", __print_error(ret));
+}
+
+static void __session_terminated_cb(wifi_aware_session_h session,
+               wifi_aware_termination_reason_e reason, void *user_data)
+{
+       printf("\n>>Session is terminated\n");
+       printf("session: %p\n", session);
+       printf("reason: %d\n", reason);
+
+       if (session != NULL) {
+               wifi_aware_session_destroy(session);
+               wifi_aware_unset_message_received_cb(session);
+       }
+}
+
+static bool __set_publish_config(wifi_aware_publish_h publish)
+{
+       int ret = 0;
+       char service_name[MAX_SERVICE_NAME_LEN + 1] = {0, };
+       unsigned char specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
+
+       ret = wifi_aware_publish_set_type(publish, g_pub_type);
+       if (ret == WIFI_AWARE_ERROR_NONE) {
+               printf("publish type: %s\n", g_pub_type == WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED ? 
+                               "Unsolicited" : "Solicited");
+       }
+       else {
+               printf("Fail to set publish type\n");
+               return false;
+       }
+
+       strncpy(service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+       ret = wifi_aware_publish_set_service_name(publish,
+                       service_name);
+       if (ret == WIFI_AWARE_ERROR_NONE) {
+               printf("service name: %s\n", SERVICE_NAME);
+       }
+       else {
+               printf("Fail to set service name\n");
+               return false;
+       }
+
+       size_t len = strlen(PUB_SSI);
+       memcpy(specific_info, PUB_SSI, len);
+       ret = wifi_aware_publish_set_service_specific_info(publish,
+                       specific_info, len);
+       if (ret == WIFI_AWARE_ERROR_NONE) {
+               printf("service service specific info: ");
+               __print_byte_array(specific_info, len);
+       }
+       else {
+               printf("Fail to set service specific info\n");
+               return false;
+       }
+
+       return true;
+}
+
+static void __enabled_cb(wifi_aware_error_e error, void *user_data)
+{
+       if (error == WIFI_AWARE_ERROR_NONE) {
+               printf("\n>>Wi-Fi Aware is enabled\n");
+       }
+       else {
+               printf("\n>>Wi-Fi Aware is not enabled. Error: %s\n", __print_error(error));
+               return;
+       }
+
+       int ret = wifi_aware_session_create(WIFI_AWARE_SESSION_PUBLISH, &g_wifi_aware_session);
+       __print_result(ret, "create publish session");
+       printf("session: %p\n", g_wifi_aware_session);
+
+       ret = wifi_aware_publish_create(&g_publish_handle);
+       __print_result(ret, "create publish configurations");
+
+       if (__set_publish_config(g_publish_handle) == false) {
+               printf(MAKE_RED"Fail to set publish configurations"RESET_COLOR"\n");
+               return;
+       }
+
+       ret = wifi_aware_set_message_received_cb(g_wifi_aware_session, __message_received_cb, NULL);
+       __print_result(ret, "set message_received_cb");
+
+       ret = wifi_aware_session_set_terminated_cb(g_wifi_aware_session, __session_terminated_cb, NULL);
+       __print_result(ret, "set session terminated callback");
+
+       printf("\n>>Publish a service\n");
+       ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+       __print_result(ret, "publish service");
+}
+
+void run_common()
+{
+       int ret = 0;
+       RET_IF_LOOP_IS_NULL();
+
+       ret = wifi_aware_enable(__enabled_cb, NULL);
+       __print_result(ret, "enable Wi-Fi Aware");
+}
+
+void clear_resources()
+{
+       if (g_ndp) {
+               wifi_aware_data_path_destroy(g_ndp);
+               wifi_aware_data_path_unset_terminated_cb(g_ndp);
+       }
+       g_ndp = NULL;
+
+       if (g_wifi_aware_session) {
+               wifi_aware_session_destroy(g_wifi_aware_session);
+               wifi_aware_unset_message_received_cb(g_wifi_aware_session);
+               wifi_aware_session_unset_terminated_cb(g_wifi_aware_session);
+       }
+       g_wifi_aware_session = NULL;
+
+       if (g_publish_handle)
+               wifi_aware_publish_destroy(g_publish_handle);
+       g_publish_handle = NULL;
+
+       g_peer = NULL;
+}
+
+void test_open_unsolicited()
+{
+       clear_resources();
+       g_pub_type = WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED;
+       g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
+       run_common();
+}
+
+void test_open_solicited()
+{
+       clear_resources();
+       g_pub_type = WIFI_AWARE_PUBLISH_TYPE_SOLICITED;
+       g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
+       run_common();
+}
+
+void test_psk_unsolicited()
+{
+       clear_resources();
+       g_pub_type = WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED;
+       g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
+       run_common();
+}
+
+void test_psk_solicited()
+{
+       clear_resources();
+       g_pub_type = WIFI_AWARE_PUBLISH_TYPE_SOLICITED;
+       g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
+       run_common();
+}
+
+void test_init()
+{
+       int ret = 0;
+
+       RET_IF_LOOP_IS_NULL();
+
+       ret = wifi_aware_initialize();
+       __print_result(ret, "wifi_aware_initialize");
+}
+
+typedef void (*test_func)(void);
+test_func g_menu_func[] = {
+       [CMD_QUIT]                              = test_quit,
+       [CMD_FULL_MENU]                 = test_full_menu,
+       [CMD_OPEN_UNSOLICITED]  = test_open_unsolicited,
+       [CMD_OPEN_SOLICITED]    = test_open_solicited,
+       [CMD_PSK_UNSOLICITED]   = test_psk_unsolicited,
+       [CMD_PSK_SOLICITED]             = test_psk_solicited,
+
+       [CMD_INVALID]                   = NULL,
+};
+
+static inline void __process_input(const char *input, gpointer user_data)
+{
+       int cmd = -1;
+
+       cmd = strtol(input, NULL, 0);
+       if (__is_digit(input) < 0 || strlen(input) == 0 || errno == ERANGE || errno
+                       == EINVAL)
+               cmd = CMD_INVALID;
+
+       printf("cmd=[%d]\n", cmd);
+       if (cmd >= CMD_INVALID || cmd < CMD_QUIT) {
+               printf("Invalid CMD\n");
+               return;
+       }
+       g_menu_func[cmd]();
+}
+
+static gboolean __test_terminal_read_std_input(GIOChannel * source,
+               GIOCondition condition, gpointer user_data)
+{
+       int fd = 0;
+
+       static char buf[1024];
+       int n;
+
+       if (g_initialized == false) {
+               test_init();
+               g_initialized = true;
+       }
+
+       errno = 0;
+       n = read(fd, buf, 1024);
+       if (n == 0) {
+               printf("Error: read() from stdin returns 0.\n");
+       } else if (n < 0) {
+               printf("input: read, err\n");
+       } else {
+               buf[n - 1] = '\0'; /* remove new line... */
+               printf("\n\n");
+               __process_input(buf, user_data);
+       }
+
+       return TRUE;
+}
+
+int main(int argc, char **argv)
+{
+
+#if !GLIB_CHECK_VERSION(2, 36, 0)
+       g_type_init();
+#endif
+       g_main_loop_p = g_main_loop_new(NULL, FALSE);
+
+       int std_input_fd = 0;
+       GIOChannel *gio2 = g_io_channel_unix_new(std_input_fd);
+       g_io_add_watch(gio2, G_IO_IN, (GIOFunc) __test_terminal_read_std_input, NULL);
+       g_io_channel_unref(gio2);
+
+       __usage_full();
+
+       g_main_loop_run(g_main_loop_p);
+
+       g_main_loop_unref(g_main_loop_p);
+
+       return 0;
+}
diff --git a/test/wifi-aware-subscribe-test.c b/test/wifi-aware-subscribe-test.c
new file mode 100644 (file)
index 0000000..977a42c
--- /dev/null
@@ -0,0 +1,624 @@
+/*
+ * Wi-Fi Aware
+ *
+ * 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.
+ *
+ */
+
+/**
+ * This file implements Wi-Fi Aware test executable.
+ *
+ * @file        wifi-aware-test.c
+ * @author      Jiung Yu (jiung.yu@samsung.com)
+ */
+
+
+/*****************************************************************************
+ *  Standard headers
+ *****************************************************************************/
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <glib.h>
+
+/*****************************************************************************
+ *  System headers
+ *****************************************************************************/
+#include <system_info.h>
+
+/*****************************************************************************
+ *  Wi-Fi Aware library headers
+ *****************************************************************************/
+#include "wifi-aware.h"
+
+/*****************************************************************************
+ *  Intelligent Network Monitoring(WIFI_AWARE) test local headers
+ *****************************************************************************/
+#include "wifi-aware-test.h"
+
+/*****************************************************************************
+ *  Macros and Typedefs
+ *****************************************************************************/
+
+#define RESET_COLOR "\e[m"
+#define MAKE_RED "\e[31m"
+#define MAKE_GREEN "\e[32m"
+
+#define __FUNC_ENTER__ printf("%s() entering...\n", __func__)
+#define __FUNC_EXIT__ printf("%s() leaving...\n", __func__)
+
+#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
+#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
+
+#define MAX_INIT_NUM 1024
+
+#define RET_IF_LOOP_IS_NULL()\
+       do {\
+               if (!g_main_loop_p) {\
+                       printf("Loop was not initialized\n");\
+                       return;\
+               } \
+       } while (0)
+
+#define SERVICE_NAME "CtsVerifierTestService"
+#define PUB_SSI "Extra bytes in the subscriber discovery"
+#define MSG_SUB_TO_PUB "Let's talk"
+
+#define MAX_SERVICE_NAME_LEN 255
+#define MAX_SPECIFIC_INFO_LEN 1024
+
+#define MAX_PSK_LEN 63
+#define PSK "AABBCCDDEE"
+#define IPV6_ADDRESS_LEN 39
+
+enum {
+       CMD_QUIT,
+       CMD_FULL_MENU,
+
+       CMD_OPEN_PASSIVE,
+       CMD_OPEN_ACTIVE,
+       CMD_PSK_PASSIVE,
+       CMD_PSK_ACTIVE,
+
+       CMD_INVALID,
+};
+
+static char *g_menu_str[] = {
+       [CMD_QUIT]
+               = "Quit",
+       [CMD_FULL_MENU]
+               = "Full Menu",
+       [CMD_OPEN_PASSIVE]
+               = "Open + Passive",
+       [CMD_OPEN_ACTIVE]
+               = "Open + Active",
+       [CMD_PSK_PASSIVE]
+               = "PSK + Passive",
+       [CMD_PSK_ACTIVE]
+               = "PSK + Active",
+       [CMD_INVALID]
+               = NULL,
+};
+
+static GMainLoop *g_main_loop_p;
+static bool g_initialized = false;
+
+static wifi_aware_session_h g_wifi_aware_session = NULL;
+static wifi_aware_subscribe_h g_subscribe_handle = NULL;
+static wifi_aware_data_path_h g_ndp = NULL;
+static wifi_aware_peer_h g_peer = NULL;
+
+static wifi_aware_subscribe_type_e g_sub_type;
+static wifi_aware_security_type_e g_security_type;
+
+static inline void __usage_full()
+{
+       int i;
+       printf("\nSubscribe Test\n");
+       for (i = CMD_QUIT; i < CMD_INVALID; i++)
+               printf(" %02d: %s\n", i, g_menu_str[i]);
+}
+
+static inline int __is_digit(const char* str)
+{
+       int len;
+       int i;
+
+       if (str == NULL)
+               return -1;
+
+       if (strlen(str) == 0)
+               return -1;
+
+       len = strlen(str);
+       for (i = 0; i < len; i++)       {
+               if (str[i] < '0' || str[i] > '9')
+                       return -2;
+       }
+
+       return 0;
+}
+
+const char *__print_error(wifi_aware_error_e err)
+{
+       switch (err) {
+       case WIFI_AWARE_ERROR_NOT_PERMITTED:
+               return "NOT_PERMITTED";
+       case WIFI_AWARE_ERROR_OUT_OF_MEMORY:
+               return "OUT_OF_MEMORY";
+       case WIFI_AWARE_ERROR_PERMISSION_DENIED:
+               return "PERMISSION_DENIED";
+       case WIFI_AWARE_ERROR_RESOURCE_BUSY:
+               return "RESOURCE_BUSY";
+       case WIFI_AWARE_ERROR_INVALID_PARAMETER:
+               return "INVALID_PARAMETER";
+       case WIFI_AWARE_ERROR_NOW_IN_PROGRESS:
+               return "NOW_IN_PROGRESS";
+       case WIFI_AWARE_ERROR_NOT_SUPPORTED:
+               return "NOT_SUPPORTED";
+       case WIFI_AWARE_ERROR_OPERATION_FAILED:
+               return "OPERATION_FAILED";
+       case WIFI_AWARE_ERROR_NOT_INITIALIZED:
+               return "NOT_INITIALIZED";
+       case WIFI_AWARE_ERROR_ALREADY_INITIALIZED:
+               return "ALREADY_INITIALIZED";
+       default:
+               return "UNKNOWN";
+       }
+}
+
+static void __print_result(int ret, gchar *msg)
+{
+    if (ret == WIFI_AWARE_ERROR_NONE) {
+        printf(MAKE_GREEN"success to %s"RESET_COLOR"\n", msg);
+    } else {
+        printf(MAKE_RED"fail to %s: %s ", msg, __print_error(ret));
+        printf(RESET_COLOR"\n");
+    }
+}
+
+static void __print_mac(unsigned char mac[6])
+{
+       printf(MACSTR, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+}
+
+void __print_byte_array(const unsigned char *data, size_t len)
+{
+       const unsigned char *ptr = data;
+       const unsigned char *max = data + len;
+       while (ptr < max) {
+               if (*ptr == '\\')
+                       printf("\\\\\\\\");
+               else if (*ptr >= ' ')
+                       printf("%c", *ptr);
+               else
+                       printf("\\\\x%02X", *ptr);
+               ptr++;
+       }   
+    printf("\n");
+}
+
+void test_full_menu()
+{
+       __usage_full();
+
+       return;
+}
+
+void test_quit()
+{
+       RET_IF_LOOP_IS_NULL();
+
+       printf("Bye\n");
+       g_main_loop_quit(g_main_loop_p);
+
+       return;
+}
+
+static void __subscribed_cb(wifi_aware_session_h session,
+               wifi_aware_error_e error, void *user_data)
+{
+       if (error == WIFI_AWARE_ERROR_NONE) {
+               printf("Publish Request is success.\n");
+       }
+       else {
+               printf("Publish Request fails. Error: %s\n", __print_error(error));
+               return;
+       }
+}
+
+static void __data_path_terminated_cb(wifi_aware_data_path_h data_path,
+               wifi_aware_termination_reason_e reason, void *user_data)
+{
+       printf("\n>>Data path(%p) is terminated.", data_path);
+       if (data_path != NULL) {
+               wifi_aware_data_path_unset_terminated_cb(data_path);
+               wifi_aware_data_path_destroy(data_path);
+       }
+}
+
+static void __open_cb(wifi_aware_data_path_h data_path, wifi_aware_error_e error, void *user_data)
+{
+       if (error == WIFI_AWARE_ERROR_NONE) {
+               int ret = WIFI_AWARE_ERROR_NONE;
+               char *interface;
+               printf("\n>>Data path is created. NDP: %p\n", data_path);
+               ret = wifi_aware_data_path_get_interface(data_path, &interface);
+               if (ret == WIFI_AWARE_ERROR_NONE)
+                       printf("Interface: %s\n", interface);
+               else
+                       printf("Fail to get interface\n");
+
+               char *ipv6_address;
+               int port;
+               ret = wifi_aware_data_path_get_peer_ipv6_address(data_path, &ipv6_address);
+               if (ret == WIFI_AWARE_ERROR_NONE)
+                       printf("IPv6 Address of Peer: %s\n", ipv6_address);
+               else
+                       printf("Fail to get IPV6 Address\n");
+               ret = wifi_aware_data_path_get_peer_port(data_path, &port);
+               if (ret == WIFI_AWARE_ERROR_NONE)
+                       printf("Port number of Peer: %d\n", port);
+               else
+                       printf("Fail to get Port number\n");
+       }
+       else {
+               printf("\n>>Fail to open Data path. data path: %p\n", data_path);
+       }
+
+}
+
+static void __open_data_path(wifi_aware_session_h session)
+{
+       printf("\n>>Open Data Path\n");
+       if (g_peer == NULL) {
+               printf("Peer is NULL\n");
+               return;
+       }
+
+       int ret = wifi_aware_data_path_create(session, g_peer, &g_ndp);
+       if (ret != WIFI_AWARE_ERROR_NONE) {
+               printf("Fail to create data path: %s\n", __print_error(ret));
+               return;
+       }
+       printf("data path: %p\n", g_ndp);
+
+       ret = wifi_aware_data_path_set_terminated_cb(g_ndp, __data_path_terminated_cb, NULL);
+       if (ret != WIFI_AWARE_ERROR_NONE) {
+               printf("Fail to create data path: %s\n", __print_error(ret));
+               return;
+       }
+
+       ret = wifi_aware_data_path_set_security(g_ndp, g_security_type);
+       if (ret != WIFI_AWARE_ERROR_NONE) {
+               printf("Fail to set security type for data path: %s\n", __print_error(ret));
+               return;
+       }
+       printf("security type: ");
+       switch (g_security_type) {
+       case WIFI_AWARE_SECURITY_TYPE_OPEN:
+               printf("Open\n");
+               break;
+       case WIFI_AWARE_SECURITY_TYPE_PSK:
+               printf("PSK\n");
+               ret = wifi_aware_data_path_set_psk(g_ndp, PSK);
+               break;
+       case WIFI_AWARE_SECURITY_TYPE_PMK:
+               printf("PMK\n");
+               break;
+       default:
+               printf("Unknown security type\n");
+               return;
+       }
+
+       ret = wifi_aware_data_path_open(g_ndp, __open_cb, NULL);
+       if (ret != WIFI_AWARE_ERROR_NONE) {
+               printf("Fail to open data path: %s\n", __print_error(ret));
+       }
+}
+
+static void __message_result_cb(wifi_aware_session_h session, wifi_aware_error_e error, void *user_data)
+{
+       printf("\n>>Result of sending message: %s\n", __print_error(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)
+{
+       int ret = 0;
+       char buf[1024] = {0, };
+       unsigned char *mac = NULL;
+
+       printf("\n>>Receive message from a Wi-Fi Aware Peer\n");
+
+       memcpy(buf, message, 1024);
+       if (wifi_aware_peer_get_mac(peer, &mac) != WIFI_AWARE_ERROR_NONE)
+               printf("Fail to get Peer's NMI: %s\n", __print_error(ret));
+
+       printf("Message: %s\n", buf);
+       printf("Peer's NMI: ");
+       if (mac != NULL) {
+               __print_mac(mac);
+               g_free(mac);
+       }
+       printf("\n");
+
+       g_peer = peer;
+       __open_data_path(session);
+}
+
+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("\n>>Find A Wi-Fi Aware Peer %p\n", peer);
+       printf("Service Specific Info: ");
+       __print_byte_array(service_specific_info, service_specific_info_len);
+       printf("\n");
+
+       printf("\n>>Send message\n");
+       unsigned char send_message[1024] = {0, };
+       size_t message_len = strlen(MSG_SUB_TO_PUB);
+       memcpy(send_message, MSG_SUB_TO_PUB, message_len);
+       int ret = wifi_aware_send_message(session, peer, send_message, message_len, __message_result_cb, NULL);
+       if (ret != WIFI_AWARE_ERROR_NONE)
+               printf("Fail to send message: %s\n", __print_error(ret));
+}
+
+static void __session_terminated_cb(wifi_aware_session_h session,
+               wifi_aware_termination_reason_e reason, void *user_data)
+{
+       printf("\n>>Session is terminated\n");
+       printf("session: %p\n", session);
+       printf("reason: %d\n", reason);
+
+       if (session != NULL) {
+               wifi_aware_session_destroy(session);
+               wifi_aware_unset_message_received_cb(session);
+       }
+}
+
+static bool __set_subscribe_config(wifi_aware_subscribe_h subscribe)
+{
+       int ret = 0;
+       char service_name[MAX_SERVICE_NAME_LEN + 1] = {0, };
+       unsigned char specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
+
+       ret = wifi_aware_subscribe_set_type(subscribe, g_sub_type);
+       if (ret == WIFI_AWARE_ERROR_NONE) {
+               printf("subscribe type: %s\n", g_sub_type == WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE ? 
+                               "Unactive" : "Solicited");
+       }
+       else {
+               printf("Fail to set subscribe type\n");
+               return false;
+       }
+
+       strncpy(service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+       ret = wifi_aware_subscribe_set_service_name(subscribe,
+                       service_name);
+       if (ret == WIFI_AWARE_ERROR_NONE) {
+               printf("service name: %s\n", SERVICE_NAME);
+       }
+       else {
+               printf("Fail to set service name\n");
+               return false;
+       }
+
+       size_t len = strlen(PUB_SSI);
+       memcpy(specific_info, PUB_SSI, len);
+       ret = wifi_aware_subscribe_set_service_specific_info(subscribe,
+                       specific_info, len);
+       if (ret == WIFI_AWARE_ERROR_NONE) {
+               printf("service service specific info: ");
+               __print_byte_array(specific_info, len);
+       }
+       else {
+               printf("Fail to set service specific info\n");
+               return false;
+       }
+
+       return true;
+}
+
+static void __enabled_cb(wifi_aware_error_e error, void *user_data)
+{
+       if (error == WIFI_AWARE_ERROR_NONE) {
+               printf("\n>>Wi-Fi Aware is enabled\n");
+       }
+       else {
+               printf("\n>>Wi-Fi Aware is not enabled. Error: %s\n", __print_error(error));
+               return;
+       }
+
+       int ret = wifi_aware_session_create(WIFI_AWARE_SESSION_SUBSCRIBE, &g_wifi_aware_session);
+       __print_result(ret, "create subscribe session");
+       printf("session: %p\n", g_wifi_aware_session);
+
+       ret = wifi_aware_subscribe_create(&g_subscribe_handle);
+       __print_result(ret, "create subscribe configurations");
+
+       if (__set_subscribe_config(g_subscribe_handle) == false) {
+               printf(MAKE_RED"Fail to set subscribe configurations"RESET_COLOR"\n");
+               return;
+       }
+
+       ret = wifi_aware_set_service_discovered_cb(g_wifi_aware_session, __service_discovered_cb, NULL);
+       __print_result(ret, "set service_discovered_cb");
+
+       ret = wifi_aware_set_message_received_cb(g_wifi_aware_session, __message_received_cb, NULL);
+       __print_result(ret, "set message_received_cb");
+
+       ret = wifi_aware_session_set_terminated_cb(g_wifi_aware_session, __session_terminated_cb, NULL);
+       __print_result(ret, "set session terminated callback");
+
+       printf("\n>>Publish a service\n");
+       ret = wifi_aware_session_subscribe(g_wifi_aware_session, g_subscribe_handle, __subscribed_cb, NULL);
+       __print_result(ret, "subscribe service");
+}
+
+void run_common()
+{
+       int ret = 0;
+       RET_IF_LOOP_IS_NULL();
+
+       ret = wifi_aware_enable(__enabled_cb, NULL);
+       __print_result(ret, "enable Wi-Fi Aware");
+}
+
+void clear_resources()
+{
+       if (g_ndp) {
+               wifi_aware_data_path_destroy(g_ndp);
+               wifi_aware_data_path_unset_terminated_cb(g_ndp);
+       }
+       g_ndp = NULL;
+
+       if (g_wifi_aware_session) {
+               wifi_aware_session_destroy(g_wifi_aware_session);
+               wifi_aware_unset_message_received_cb(g_wifi_aware_session);
+               wifi_aware_session_unset_terminated_cb(g_wifi_aware_session);
+       }
+       g_wifi_aware_session = NULL;
+
+       if (g_subscribe_handle)
+               wifi_aware_subscribe_destroy(g_subscribe_handle);
+       g_subscribe_handle = NULL;
+
+       g_peer = NULL;
+}
+
+void test_open_passive()
+{
+       clear_resources();
+       g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE;
+       g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
+       run_common();
+}
+
+void test_open_active()
+{
+       clear_resources();
+       g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
+       g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
+       run_common();
+}
+
+void test_psk_passive()
+{
+       clear_resources();
+       g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE;
+       g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
+       run_common();
+}
+
+void test_psk_active()
+{
+       clear_resources();
+       g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
+       g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
+       run_common();
+}
+
+void test_init()
+{
+       int ret = 0;
+
+       RET_IF_LOOP_IS_NULL();
+
+       ret = wifi_aware_initialize();
+       __print_result(ret, "wifi_aware_initialize");
+}
+
+typedef void (*test_func)(void);
+test_func g_menu_func[] = {
+       [CMD_QUIT]                              = test_quit,
+       [CMD_FULL_MENU]                 = test_full_menu,
+       [CMD_OPEN_PASSIVE]              = test_open_passive,
+       [CMD_OPEN_ACTIVE]               = test_open_active,
+       [CMD_PSK_PASSIVE]               = test_psk_passive,
+       [CMD_PSK_ACTIVE]                = test_psk_active,
+
+       [CMD_INVALID]                   = NULL,
+};
+
+static inline void __process_input(const char *input, gpointer user_data)
+{
+       int cmd = -1;
+
+       cmd = strtol(input, NULL, 0);
+       if (__is_digit(input) < 0 || strlen(input) == 0 || errno == ERANGE || errno
+                       == EINVAL)
+               cmd = CMD_INVALID;
+
+       printf("cmd=[%d]\n", cmd);
+       if (cmd >= CMD_INVALID || cmd < CMD_QUIT) {
+               printf("Invalid CMD\n");
+               return;
+       }
+       g_menu_func[cmd]();
+}
+
+static gboolean __test_terminal_read_std_input(GIOChannel * source,
+               GIOCondition condition, gpointer user_data)
+{
+       int fd = 0;
+
+       static char buf[1024];
+       int n;
+
+       if (g_initialized == false) {
+               test_init();
+               g_initialized = true;
+       }
+
+       errno = 0;
+       n = read(fd, buf, 1024);
+       if (n == 0) {
+               printf("Error: read() from stdin returns 0.\n");
+       } else if (n < 0) {
+               printf("input: read, err\n");
+       } else {
+               buf[n - 1] = '\0'; /* remove new line... */
+               printf("\n\n");
+               __process_input(buf, user_data);
+       }
+
+       return TRUE;
+}
+
+int main(int argc, char **argv)
+{
+
+#if !GLIB_CHECK_VERSION(2, 36, 0)
+       g_type_init();
+#endif
+       g_main_loop_p = g_main_loop_new(NULL, FALSE);
+
+       int std_input_fd = 0;
+       GIOChannel *gio2 = g_io_channel_unix_new(std_input_fd);
+       g_io_add_watch(gio2, G_IO_IN, (GIOFunc) __test_terminal_read_std_input, NULL);
+       g_io_channel_unref(gio2);
+
+       __usage_full();
+
+       g_main_loop_run(g_main_loop_p);
+
+       g_main_loop_unref(g_main_loop_p);
+
+       return 0;
+}
index 035aa31..548ac67 100644 (file)
@@ -832,7 +832,7 @@ static void __open_cb(wifi_aware_data_path_h data_path, wifi_aware_error_e error
        if (error == WIFI_AWARE_ERROR_NONE) {
                int ret = WIFI_AWARE_ERROR_NONE;
                char *interface;
-               printf("Data path is created. NDP: %p\n", data_path);
+               printf("Data path is established. NDP: %p\n", data_path);
                ret = wifi_aware_data_path_get_interface(data_path, &interface);
                if (ret == WIFI_AWARE_ERROR_NONE)
                        printf("Interface: %s\n", interface);
@@ -860,7 +860,8 @@ static void __open_cb(wifi_aware_data_path_h data_path, wifi_aware_error_e error
 
 }
 
-static void _data_path_terminated_cb(wifi_aware_data_path_h data_path, int reason, void *user_data)
+static void _data_path_terminated_cb(wifi_aware_data_path_h data_path,
+               wifi_aware_termination_reason_e reason, void *user_data)
 {
        printf("%p data path is terminated.", data_path);
        wifi_aware_data_path_unset_terminated_cb(data_path);