ADD_SUBDIRECTORY(include)
ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(pkgconfig)
-ADD_SUBDIRECTORY(test)
+ADD_SUBDIRECTORY(tests/nan-test)
+ADD_SUBDIRECTORY(tests/unittest)
ADD_SUBDIRECTORY(cts-verifier)
+++ /dev/null
-<manifest>
- <request>
- <domain name="_" />
- </request>
-</manifest>
\ No newline at end of file
* @return 0 on success, otherwise a negative error value
* @retval #WIFI_AWARE_ERROR_NONE
* @retval #WIFI_AWARE_ERROR_ALREADY_INITIALIZED
+ * @retval #WIFI_AWARE_ERROR_OPERATION_FAILED
* @see wifi_aware_deinitialize()
*/
int wifi_aware_initialize();
--- /dev/null
+<manifest>
+ <request>
+ <domain name="_" />
+ </request>
+</manifest>
\ No newline at end of file
Group: Network & Connectivity/API
License: Apache-2.0
Source0: %{name}-%{version}.tar.gz
+Source1: %{name}.manifest
BuildRequires: cmake
BuildRequires: pkgconfig(gobject-2.0)
BuildRequires: pkgconfig(capi-base-common)
BuildRequires: pkgconfig(capi-system-info)
BuildRequires: pkgconfig(cynara-client)
+BuildRequires: pkgconfig(gmock)
+%if 0%{?gcov:1}
+BuildRequires: lcov
+%endif
Requires(post): /sbin/ldconfig
Requires(postun): /sbin/ldconfig
%description cts-verifier
cts-verifier command app
+%if 0%{?gcov:1}
+%package gcov
+Summary: Coverage Data of %{name}
+
+%description gcov
+The %{name}-gcov package contains gcov objects
+%endif
%prep
%setup -q
chmod 644 %{SOURCE0}
+cp %{SOURCE1} ./%{name}.manifest
%ifarch %{arm}
export ARCH=arm
%endif
%build
+%if 0%{?gcov:1}
+export CFLAGS+=" -fprofile-arcs -ftest-coverage"
+export CXXFLAGS+=" -fprofile-arcs -ftest-coverage"
+export FFLAGS+=" -fprofile-arcs -ftest-coverage"
+export LDFLAGS+=" -lgcov"
+%endif
+
%if 0%{?sec_build_binary_debug_enable}
export CFLAGS="$CFLAGS -DTIZEN_DEBUG_ENABLE"
export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE"
-DMAJORVER=${MAJORVER}
make %{?jobs:-j%jobs}
+%if 0%{?gcov:1}
+mkdir -p gcov-obj
+find . -name '*.gcno' -exec cp '{}' gcov-obj ';'
+%endif
+
%install
rm -rf %{buildroot}
%make_install
+%if 0%{?gcov:1}
+mkdir -p %{buildroot}%{_datadir}/gcov/obj
+install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj
+%endif
+
+%check
+export LD_LIBRARY_PATH=./src/
+LD_PRELOAD=./tests/unittest/libwifi-aware-mock.so ./tests/unittest/wifi-aware-unittest
+#LD_LIBRARY_PATH=
+
+%if 0%{?gcov:1}
+lcov -c --ignore-errors graph --no-external -b . -d . -o %{name}.info
+genhtml %{name}.info -o out --legend --show-details
+%endif
+
%post -p /sbin/ldconfig
%postun -p /sbin/ldconfig
%files
-%manifest capi-network-wifi-aware.manifest
+%manifest %{name}.manifest
%license LICENSE
%defattr(-,root,root,-)
%{_libdir}/*.so.*
%{_libdir}/*.so
%files test
-%manifest wifi-aware-test.manifest
+%manifest %{name}.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
%files cts-verifier
#%manifest wifi-aware-test.manifest
%attr(755,root,root) %{_bindir}/cts-verifier
+
+%if 0%{?gcov:1}
+%files gcov
+%{_datadir}/gcov/obj/*
+%endif
--- /dev/null
+<manifest>
+ <request>
+ <domain name="_" />
+ </request>
+</manifest>
\ No newline at end of file
&& security_type <= WIFI_AWARE_SECURITY_TYPE_PMK;
}
-//LCOV_EXCL_START
static uint32_t __data_path_get_ndp_id(wifi_aware_data_path_h data_path)
{
wifi_aware_data_path_s *ndp = (wifi_aware_data_path_s *)data_path;
wifi_aware_gdbus_deregister_signal(DATA_PATH_TERMINATED_SIGNAL);
ndp->terminated_signal_id = 0;
-
+
__WIFI_AWARE_FUNC_ENTER__;
return ret;
}
-//LCOV_EXCL_STOP
if (gdbus_data.connection != NULL) {
WIFI_AWARE_LOGI("There's already gdbus connection"); //LCOV_EXCL_LINE
__WIFI_AWARE_FUNC_EXIT__; //LCOV_EXCL_LINE
- return 0;
+ return WIFI_AWARE_ERROR_NONE;
}
gdbus_data.connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
if (gdbus_data.connection == NULL) {
WIFI_AWARE_LOGI("Failed to get connection, Error[%s]\n", error->message); //LCOV_EXCL_LINE
g_error_free(error); //LCOV_EXCL_LINE
- return -1;
+ return WIFI_AWARE_ERROR_OPERATION_FAILED;
}
gdbus_data.cancellable = g_cancellable_new();
return ret;
}
-//LCOV_EXCL_START
int wifi_aware_gdbus_followup(wifi_aware_session_h session,
int client_id, uint16_t pub_sub_id, unsigned int peer_id,
const unsigned char *message, size_t len,
__WIFI_AWARE_FUNC_EXIT__;
return ret;
}
-//LCOV_EXCL_STOP
+
unsigned char mac[WIFI_AWARE_MAC_ADDRESS_LEN];
} wifi_aware_peer_s;
-//LCOV_EXCL_START
int _wifi_aware_peer_create(wifi_aware_peer_h *peer, unsigned int peer_id)
{
wifi_aware_peer_s *peer_handle = (wifi_aware_peer_s *)g_try_malloc0(sizeof(wifi_aware_peer_s));
wifi_aware_peer_s *handle = (wifi_aware_peer_s *)peer;
memcpy(handle->mac, mac, WIFI_AWARE_MAC_ADDRESS_LEN);
}
-//LCOV_EXCL_STOP
}
}
-//LCOV_EXCL_START
static void __wifi_aware_discovered_cb(wifi_aware_session_h session, wifi_aware_peer_h peer,
const unsigned char *specific_info, size_t specific_info_len, int distance)
{
if (handle->terminated_cb)
handle->terminated_cb(session, reason, handle->terminated_cb_data);
}
-//LCOV_EXCL_STOP
static wifi_aware_error_e __get_dbus_result(GObject *src, GAsyncResult *res, uint16_t *id)
{
g_variant_get(parameters, "(qi)", pub_sub_id, reason);
}
-//LCOV_EXCL_START
static void __session_terminated_cb(GDBusConnection *connection,
const gchar *sender, const gchar *object_path,
const gchar *interface, const gchar *signal,
__session_set_pub_sub_id(session, 0);
__wifi_aware_session_terminated_cb(session, (wifi_aware_termination_reason_e)reason);
}
-//LCOV_EXCL_STOP
void _wifi_aware_session_set_terminated_cb(wifi_aware_session_h session,
wifi_aware_session_terminated_cb callback, void *user_data)
return true;
}
-//LCOV_EXCL_START
static bool __parsing_service_discovered_event(GVariant *parameters,
uint16_t *sub_id, unsigned int *peer_id, unsigned char *peer_mac,
unsigned char *specific_info, size_t *specific_info_len, int *distance)
_wifi_aware_peer_set_mac(peer, peer_mac);
__wifi_aware_received_cb(session, peer, message, message_len);
}
-//LCOV_EXCL_STOP
int _wifi_aware_set_service_discovered_cb(wifi_aware_session_h session,
wifi_aware_service_discovered_cb callback, void *user_data)
g_free(message);
}
-//LCOV_EXCL_START
static void __wifi_aware_followup_invoke_callback(wifi_aware_message_s *message, wifi_aware_error_e error)
{
if (message && message->callback) {
__send_message_reply,
message_data);
}
-//LCOV_EXCL_STOP
+++ /dev/null
-# Copyright (c) 2020 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.
-#
-# @file CMakeLists.txt
-#
-
-CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
-
-SET(WIFI_AWARE_TEST_INCLUDE_DIR ${WIFI_AWARE_TEST_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../include)
-INCLUDE_DIRECTORIES(${WIFI_AWARE_TEST_INCLUDE_DIR})
-
-INCLUDE(FindPkgConfig)
-pkg_check_modules(TARGET_WIFI_AWARE_TEST REQUIRED capi-base-common glib-2.0)
-FOREACH(flag ${TARGET_WIFI_AWAREs_TEST_CFLAGS})
- SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
- MESSAGE(${flag})
-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}")
- ADD_EXECUTABLE(${src_name} ${src})
- TARGET_LINK_LIBRARIES(${src_name} ${TARGET_WIFI_AWARE} ${TARGET_WIFI_AWARE_TEST_LDFLAGS})
-ENDFOREACH()
-
-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})
-INSTALL(TARGETS wifi-aware-publish-matching-filter-test RUNTIME DESTINATION ${BIN_DIR})
-INSTALL(TARGETS wifi-aware-subscribe-matching-filter-test RUNTIME DESTINATION ${BIN_DIR})
+++ /dev/null
-/*
- * Wi-Fi Aware Test Program
- *
- * Copyright (c) 2020 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 __WIFI_AWARE_MATCHING_FILTER_TEST_H__
-#define __WIFI_AWARE_MATCHING_FILTER_TEST_H__
-
-#include <stdio.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-typedef enum {
- DUMMY_TEST_NUM = 0,
-
- // Sol / Active
- POSITIVE_TEST_01,
- POSITIVE_TEST_02,
- POSITIVE_TEST_03,
- POSITIVE_TEST_04,
-
- POSITIVE_TEST_05,
- POSITIVE_TEST_06,
- POSITIVE_TEST_07,
-
- POSITIVE_TEST_08,
- POSITIVE_TEST_09,
- POSITIVE_TEST_10,
-
- // Unsol / Passive
- POSITIVE_TEST_11,
- POSITIVE_TEST_12,
- POSITIVE_TEST_13,
- POSITIVE_TEST_14,
-
- POSITIVE_TEST_15,
- POSITIVE_TEST_16,
- POSITIVE_TEST_17,
-
- POSITIVE_TEST_18,
- POSITIVE_TEST_19,
- POSITIVE_TEST_20,
-
- ///// custom ///
- POSITIVE_TEST_21,
- POSITIVE_TEST_22,
- POSITIVE_TEST_23,
- POSITIVE_TEST_24,
-
- POSITIVE_TEST_25,
- POSITIVE_TEST_26,
- POSITIVE_TEST_27,
-
- POSITIVE_TEST_28,
- POSITIVE_TEST_29,
- POSITIVE_TEST_30,
-
- // Sol / Active
- NEGATIVE_TEST_01,
- NEGATIVE_TEST_02,
- NEGATIVE_TEST_03,
-
- // Unsol / Passive
- NEGATIVE_TEST_04,
- NEGATIVE_TEST_05,
- NEGATIVE_TEST_06,
-
- //// custom
- NEGATIVE_TEST_07,
- NEGATIVE_TEST_08,
- NEGATIVE_TEST_09,
-
- INVALID,
-} test_case_e;
-
-#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 PRINT_START(x) printf(MAKE_GREEN "Running test: %dth..." RESET_COLOR "\n", x)
-
-#define MAX_INIT_NUM 1024
-
-#define SERVICE_NAME "matching filter test"
-#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_MATCH_FILTER_LEN 255
-
-typedef struct {
- size_t len;
- unsigned char values[MAX_MATCH_FILTER_LEN];
-} matching_filter_format_t;
-
-#define MATCHING_FILTER_FORMAT_NUM 8
-
-matching_filter_format_t test_matching_filter[MATCHING_FILTER_FORMAT_NUM]
- = { {5, {0, 0, 0, 0, 0}}, // <0><0><0><0><0> -> wildcard
- {10, {1, 1, 1, 2, 1, 3, 1, 4, 1, 5}}, // <1,1,><1,2><1,3><1,4><1,5> -> value filter 1
- {10, {1, 1, 1, 2, 1, 1, 1, 4, 1, 5}}, // <1,1,><1,2><1,1><1,4><1,5> -> value filter 2
- {6, {0, 1, 2, 0, 1, 4}}, // <0><1,2><0><1,4> -> value filter 3
- {8, {0, 1, 2, 1, 3, 0, 1, 5}}, // <0><1,2><1,3><0><1,5> -> value filter 4
- {6, {1, 1, 0, 1, 3, 0}}, // <1,1><0><1,3><0> -> value filter 5
- {8, {1, 1, 0, 1, 3, 0, 1, 5}}, // <1,1><0><1,3><0><1,5> -> value filter 6
- {0, {0, }}, // No filter
-};
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __WIFI_AWARE_MATCHING_FILTER_TEST_H__ */
+++ /dev/null
-/*
- * Wi-Fi Aware
- *
- * Copyright (c) 2020 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.
- *
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <glib.h>
-
-#include "wifi-aware.h"
-#include "wifi-aware-matching-filter-test.h"
-
-#define RET_IF_LOOP_IS_NULL()\
- do {\
- if (!g_main_loop_p) {\
- printf("Loop was not initialized\n");\
- return;\
- } \
- } while (0)
-
-static GMainLoop *g_main_loop_p;
-
-static test_case_e g_current_test = DUMMY_TEST_NUM;
-static bool g_initialized = false;
-static bool g_enabled = false;
-
-static bool g_test_result = false;
-
-static wifi_aware_session_h g_wifi_aware_session = NULL;
-static wifi_aware_publish_h g_publish_handle = NULL;
-
-void test_init();
-void test_deinit();
-
-static void __print_test_result(int num, bool result)
-{
- char test_name[20] = {0, };
-
- if (num < NEGATIVE_TEST_01)
- snprintf(test_name, 19, "%s %d", "Positivie Test", num);
- else
- snprintf(test_name, 19, "%s %d", "Negative Test", num - NEGATIVE_TEST_01 + 1);
-
- if (result)
- printf(MAKE_GREEN"[%s] is pass."RESET_COLOR"\n", test_name);
- else
- printf(MAKE_RED"[%s] is fail."RESET_COLOR"\n", test_name);
-
- printf("Please enter to run the next test..\n");
-}
-
-void test_quit()
-{
- RET_IF_LOOP_IS_NULL();
-
- 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)
-{
- printf("published..!\n");
- if (error == WIFI_AWARE_ERROR_NONE)
- g_test_result = true;
- else
- g_test_result = false;
-
- __print_test_result(g_current_test, g_test_result);
-}
-
-static bool __set_default_configures(void)
-{
- unsigned char specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
-
- int ret = wifi_aware_session_create(WIFI_AWARE_SESSION_PUBLISH, &g_wifi_aware_session);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return false;
-
- ret = wifi_aware_publish_create(&g_publish_handle);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return false;
-
- ret = wifi_aware_publish_set_service_name(g_publish_handle, SERVICE_NAME);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return false;
-
- size_t len = strlen(PUB_SSI);
- memcpy(specific_info, PUB_SSI, len);
- ret = wifi_aware_publish_set_service_specific_info(g_publish_handle, specific_info, len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return false;
-
- ret = wifi_aware_publish_enable_ranging(g_publish_handle, false);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return false;
-
- return true;
-}
-
-static void __enabled_cb(wifi_aware_error_e error, void *user_data)
-{
- if (error == WIFI_AWARE_ERROR_NONE)
- g_enabled = true;
- else
- g_enabled = false;
-
- g_main_loop_quit(g_main_loop_p);
-}
-
-void clear_resources()
-{
- if (g_publish_handle)
- wifi_aware_publish_destroy(g_publish_handle);
- g_publish_handle = NULL;
-
- if (g_wifi_aware_session) {
- wifi_aware_session_stop(g_wifi_aware_session);
- wifi_aware_session_unset_message_received_cb(g_wifi_aware_session);
- wifi_aware_session_unset_terminated_cb(g_wifi_aware_session);
- wifi_aware_session_destroy(g_wifi_aware_session);
- }
- g_wifi_aware_session = NULL;
-
- g_test_result = false;
-}
-
-void test_init()
-{
- int ret = 0;
-
- RET_IF_LOOP_IS_NULL();
-
- if (g_initialized == false) {
- ret = wifi_aware_initialize();
- if (ret != WIFI_AWARE_ERROR_NONE) {
- g_main_loop_quit(g_main_loop_p);
- return;
- }
- g_initialized = true;
- }
-
- if (g_enabled == false) {
- ret = wifi_aware_enable(__enabled_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- g_main_loop_quit(g_main_loop_p);
- return;
- }
- }
-}
-
-void test_deinit()
-{
- wifi_aware_disable();
- wifi_aware_deinitialize();
-}
-
-void test_sol_no_filter()
-{
- PRINT_START(g_current_test);
-
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_SOLICITED);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // no filter
-
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_sol_wildcard_filter()
-{
- PRINT_START(g_current_test);
-
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_SOLICITED);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <0><0><0><0><0>
- ret = wifi_aware_publish_set_match_filter(g_publish_handle,
- test_matching_filter[0].values, test_matching_filter[0].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_sol_value_filter_1()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_SOLICITED);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <1,1><1,2><1,3><1,4><1,5>
- ret = wifi_aware_publish_set_match_filter(g_publish_handle,
- test_matching_filter[1].values, test_matching_filter[1].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_sol_value_filter_2()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_SOLICITED);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <1,1><1,2><1,1><1,4><1,5>
- ret = wifi_aware_publish_set_match_filter(g_publish_handle,
- test_matching_filter[2].values, test_matching_filter[2].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_sol_value_filter_4()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_SOLICITED);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <0><1,2><1,3><0><1,5>
- ret = wifi_aware_publish_set_match_filter(g_publish_handle,
- test_matching_filter[4].values, test_matching_filter[4].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_sol_value_filter_5()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_SOLICITED);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <1,1><0><1,3><0>
- ret = wifi_aware_publish_set_match_filter(g_publish_handle,
- test_matching_filter[5].values, test_matching_filter[5].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_unsol_no_filter()
-{
- PRINT_START(g_current_test);
-
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // no filter
-
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_unsol_wildcard_filter()
-{
- PRINT_START(g_current_test);
-
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <0><0><0><0><0>
- ret = wifi_aware_publish_set_match_filter(g_publish_handle,
- test_matching_filter[0].values, test_matching_filter[0].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_unsol_value_filter_1(void)
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_publish_set_match_filter(g_publish_handle,
- test_matching_filter[1].values, test_matching_filter[1].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_unsol_value_filter_2()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <1,1><1,2><1,1><1,4><1,5>
- ret = wifi_aware_publish_set_match_filter(g_publish_handle,
- test_matching_filter[2].values, test_matching_filter[2].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_unsol_value_filter_3()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <0><1,2><0><1,4>
- ret = wifi_aware_publish_set_match_filter(g_publish_handle,
- test_matching_filter[3].values, test_matching_filter[3].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_unsol_value_filter_4()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <0><1,2><1,3><0><1,5>
- ret = wifi_aware_publish_set_match_filter(g_publish_handle,
- test_matching_filter[4].values, test_matching_filter[4].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_unsol_value_filter_5()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <1,1><0><1,3><0>
- ret = wifi_aware_publish_set_match_filter(g_publish_handle,
- test_matching_filter[5].values, test_matching_filter[5].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_unsol_value_filter_6()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <1,1><0><1,3><0><1,5>
- ret = wifi_aware_publish_set_match_filter(g_publish_handle,
- test_matching_filter[6].values, test_matching_filter[6].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-typedef void (*test_func)(void);
-test_func g_menu_func[] = {
- [0] = NULL,
-
- [POSITIVE_TEST_01] = test_sol_no_filter,
- [POSITIVE_TEST_02] = test_sol_wildcard_filter,
- [POSITIVE_TEST_03] = test_sol_no_filter,
- [POSITIVE_TEST_04] = test_sol_value_filter_1,
-
- [POSITIVE_TEST_05] = test_sol_value_filter_1,
- [POSITIVE_TEST_06] = test_sol_wildcard_filter,
- [POSITIVE_TEST_07] = test_sol_value_filter_1,
-
- [POSITIVE_TEST_08] = test_sol_value_filter_1,
- [POSITIVE_TEST_09] = test_sol_value_filter_4,
- [POSITIVE_TEST_10] = test_sol_value_filter_1,
-
- [POSITIVE_TEST_11] = test_unsol_no_filter,
- [POSITIVE_TEST_12] = test_unsol_no_filter,
- [POSITIVE_TEST_13] = test_unsol_wildcard_filter,
- [POSITIVE_TEST_14] = test_unsol_value_filter_1,
-
- [POSITIVE_TEST_15] = test_unsol_wildcard_filter,
- [POSITIVE_TEST_16] = test_unsol_value_filter_1,
- [POSITIVE_TEST_17] = test_unsol_value_filter_1,
-
- [POSITIVE_TEST_18] = test_unsol_value_filter_6,
- [POSITIVE_TEST_19] = test_unsol_value_filter_1,
- [POSITIVE_TEST_20] = test_unsol_value_filter_1,
-
- // custom: unsol + active
- [POSITIVE_TEST_21] = test_unsol_no_filter,
- [POSITIVE_TEST_22] = test_unsol_wildcard_filter,
- [POSITIVE_TEST_23] = test_unsol_no_filter,
- [POSITIVE_TEST_24] = test_unsol_value_filter_1,
-
- [POSITIVE_TEST_25] = test_unsol_value_filter_1,
- [POSITIVE_TEST_26] = test_unsol_wildcard_filter,
- [POSITIVE_TEST_27] = test_unsol_value_filter_1,
-
- [POSITIVE_TEST_28] = test_unsol_value_filter_1,
- [POSITIVE_TEST_29] = test_unsol_value_filter_4,
- [POSITIVE_TEST_30] = test_unsol_value_filter_1,
-
- [NEGATIVE_TEST_01] = test_sol_no_filter,
- [NEGATIVE_TEST_02] = test_sol_value_filter_2,
- [NEGATIVE_TEST_03] = test_sol_value_filter_5,
-
- [NEGATIVE_TEST_04] = test_unsol_no_filter,
- [NEGATIVE_TEST_05] = test_unsol_value_filter_1,
- [NEGATIVE_TEST_06] = test_unsol_value_filter_3,
-
- // custom test: unsol + active
- [NEGATIVE_TEST_07] = test_unsol_no_filter,
- [NEGATIVE_TEST_08] = test_unsol_value_filter_2,
- [NEGATIVE_TEST_09] = test_unsol_value_filter_5,
-
-
- [INVALID] = NULL,
-};
-
-static gboolean __test_terminal_read_std_input(GIOChannel * source,
- GIOCondition condition, gpointer user_data)
-{
- int fd = 0;
- static char buf[1024];
- int 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 {
- ++g_current_test;
- if (g_current_test == INVALID) {
- printf("Test is finished..\n");
- g_main_loop_quit(g_main_loop_p);
- return TRUE;
- }
- clear_resources();
- g_menu_func[g_current_test]();
- }
- 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);
-
- printf(MAKE_GREEN"\nThis is Matching filter test on Publisher side"RESET_COLOR"\n");
-
- test_init();
- g_main_loop_run(g_main_loop_p);
- if (!g_enabled) {
- printf(MAKE_RED"Can not start test.."RESET_COLOR"\n");
- g_main_loop_unref(g_main_loop_p);
- return 0;
- }
-
- 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);
-
- printf("Please enter to start test..\n");
-
- g_main_loop_run(g_main_loop_p);
-
- g_main_loop_unref(g_main_loop_p);
- test_deinit();
-
- return 0;
-}
+++ /dev/null
-/*
- * Wi-Fi Aware
- *
- * Copyright (c) 2020 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 PRINT_SUCCESS(format, args...) printf(MAKE_GREEN format RESET_COLOR "\n", ##args)
-#define PRINT_FAILURE(format, args...) printf(MAKE_RED format RESET_COLOR "\n", ##args)
-#define PRINT_FAILURE_ERROR(format, error) printf(MAKE_RED format " Failure(%d:%s)" RESET_COLOR "\n", error, __print_error(error))
-
-#define SERVICE_NAME "Wi-Fi Aware Test"
-#define OTHER_SERVICE_NAME "Other Service Name"
-#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 PMK_LEN 32
-#define PMK "12345678901234567890123456789012"
-#define IPV6_ADDRESS_LEN 39
-
-enum {
- CMD_QUIT,
-
- CMD_OPEN_UNSOLICITED,
- CMD_OPEN_SOLICITED,
- CMD_PSK_UNSOLICITED,
- CMD_PSK_SOLICITED,
- CMD_PMK_UNSOLICITED,
- CMD_PMK_SOLICITED,
-
- CMD_OPEN_UNSOLICITED_OTHER_SERVICE,
- CMD_OPEN_SOLICITED_OTHER_SERVICE,
-
- CMD_OPEN_UNSOLICITED_RANGING,
- CMD_OPEN_SOLICITED_RANGING,
-
- CMD_INVALID,
-};
-
-static char *g_menu_str[] = {
- [CMD_QUIT]
- = "Quit",
- [CMD_OPEN_UNSOLICITED]
- = "Open + Unsolicited",
- [CMD_OPEN_SOLICITED]
- = "Open + Solicited",
- [CMD_PSK_UNSOLICITED]
- = "PSK + Unsolicited",
- [CMD_PSK_SOLICITED]
- = "PSK + Solicited",
- [CMD_PMK_UNSOLICITED]
- = "PMK + Unsolicited",
- [CMD_PMK_SOLICITED]
- = "PMK + Solicited",
- [CMD_OPEN_UNSOLICITED_OTHER_SERVICE]
- = "Open + Unsolicited + Other Service",
- [CMD_OPEN_SOLICITED_OTHER_SERVICE]
- = "Open + Solicited + Other Service",
- [CMD_OPEN_UNSOLICITED_RANGING]
- = "Open + Unsolicited + Ranging",
- [CMD_OPEN_SOLICITED_RANGING]
- = "Open + Solicited + Ranging",
- [CMD_INVALID]
- = NULL,
-};
-
-static GMainLoop *g_main_loop_p;
-static bool g_ranging = false;
-
-static bool g_initialized = false;
-static bool g_enabled = 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 char g_service_name[MAX_SERVICE_NAME_LEN + 1] = {0, };
-static unsigned char g_pmk[PMK_LEN];
-
-void test_init();
-void test_deinit();
-
-static void __test_finish(int is_validated)
-{
- if (is_validated)
- printf(MAKE_GREEN"[TEST RESULT] Wi-Fi Aware validated!"RESET_COLOR"\n");
- else
- printf(MAKE_RED"[TEST RESULT] Wi-Fi Aware invalidated!"RESET_COLOR"\n");
- test_deinit();
-}
-
-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_NONE:
- return "NO ERROR";
- 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";
- case WIFI_AWARE_ERROR_ALREADY_ENABLED:
- return "ALREADY_ENABLED";
- case WIFI_AWARE_ERROR_REJECTED_BY_PEER:
- return "REJECTED_BY_PEER";
- case WIFI_AWARE_ERROR_INTERFACE_DOWN:
- return "INTERFACE_IS_DOWN";
- default:
- return "UNKNOWN";
- }
-}
-
-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();
-
- 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) {
- PRINT_SUCCESS("[CB][Publish] Requested");
- } else {
- PRINT_FAILURE_ERROR("[CB][Publish]", error);
- __test_finish(0);
- }
-}
-
-static void __data_path_terminated_cb(wifi_aware_data_path_h data_path,
- wifi_aware_termination_reason_e reason, void *user_data)
-{
- printf("[EVENT][DataPath Terminated] %p", 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;
- PRINT_SUCCESS("[CB][OpenDataPath] Connected %p", data_path);
- ret = wifi_aware_data_path_get_interface(data_path, &interface);
- if (ret == WIFI_AWARE_ERROR_NONE)
- printf("\tInterface: %s\n", interface);
- else
- PRINT_FAILURE_ERROR("\tInterface:", ret);
-
- 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("\tPeer's IPv6 Address: %s\n", ipv6_address);
- else
- PRINT_FAILURE_ERROR("\tPeer's IPv6 Address:", ret);
- ret = wifi_aware_data_path_get_peer_port(data_path, &port);
- if (ret == WIFI_AWARE_ERROR_NONE)
- printf("\tPeer's Port: %d\n", port);
- else
- PRINT_FAILURE_ERROR("\tPeer's Port:", ret);
- __test_finish(1);
- }
- else {
- PRINT_FAILURE("[CB][OpenDataPath] Failure(%d:%s) %p", error, __print_error(error), data_path);
- __test_finish(0);
- }
-}
-
-static void __open_data_path(wifi_aware_session_h session)
-{
- printf("[OpenDataPath] Request\n");
- if (g_peer == NULL) {
- printf("Peer is NULL\n");
- __test_finish(0);
- return;
- }
-
- int ret = wifi_aware_data_path_create(session, g_peer, &g_ndp);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("\tHandle:", ret);
- __test_finish(0);
- return;
- }
- printf("\tHandle: %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) {
- PRINT_FAILURE_ERROR("\tSet TerminatedCallback:", ret);
- __test_finish(0);
- return;
- }
-
- ret = wifi_aware_data_path_set_security(g_ndp, g_security_type);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("\tSecurity Type:", ret);
- __test_finish(0);
- return;
- }
- printf("\tSecurity Type: ");
- switch (g_security_type) {
- case WIFI_AWARE_SECURITY_TYPE_OPEN:
- printf("Open\n");
- break;
- case WIFI_AWARE_SECURITY_TYPE_PSK:
- printf("PSK\n");
- wifi_aware_data_path_set_psk(g_ndp, PSK);
- break;
- case WIFI_AWARE_SECURITY_TYPE_PMK:
- wifi_aware_data_path_set_pmk(g_ndp, g_pmk);
- printf("PMK\n");
- break;
- default:
- PRINT_FAILURE("Unknown security type");
- __test_finish(0);
- return;
- }
-
- ret = wifi_aware_data_path_set_port(g_ndp, PORT);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("\tPort:", ret);
- __test_finish(0);
- } else {
- printf("\tPort: %d\n", PORT);
- }
-
- ret = wifi_aware_data_path_open(g_ndp, __open_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("[OpenDataPath]", ret);
- __test_finish(0);
- }
-}
-
-static void __message_result_cb(wifi_aware_session_h session, wifi_aware_error_e error, void *user_data)
-{
- if (error == WIFI_AWARE_ERROR_NONE) {
- PRINT_SUCCESS("[CB][Send Message] Requested");
- __open_data_path(session);
- } else {
- PRINT_FAILURE_ERROR("[CB][Send Message]", error);
- __test_finish(0);
- }
-}
-
-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;
-
- PRINT_SUCCESS("[Event][Receive Message]");
-
- ret = wifi_aware_peer_get_mac(peer, &mac);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- printf("\tPeer's NMI: Failure(%d:%s)\n", ret, __print_error(ret));
- if (mac)
- g_free(mac);
- __test_finish(0);
- return;
- } else {
- printf("\tPeer's NMI: ");
- if (mac != NULL) {
- __print_mac(mac);
- g_free(mac);
- }
- printf("\n");
- }
-
- memcpy(buf, message, 1024);
- buf[len] = '\0';
- printf("\tMessage: %s\n", buf);
-
- printf("[Send Message] Request\n");
- g_peer = peer;
- 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_session_send_message(session, peer, send_message, message_len, __message_result_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("[Send Message]", ret);
- __test_finish(0);
- }
-}
-
-static void __session_terminated_cb(wifi_aware_session_h session,
- wifi_aware_termination_reason_e reason, void *user_data)
-{
- PRINT_FAILURE("[Event][Session Terminated] Session: %p Reason: %d", session, reason);
-
- if (session != NULL) {
- wifi_aware_session_unset_message_received_cb(session);
- wifi_aware_session_destroy(session);
- }
- __test_finish(0);
-}
-
-static bool __set_publish_config(wifi_aware_publish_h publish)
-{
- int ret = 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("\tPublish Type: %s\n", g_pub_type == WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED ?
- "Unsolicited" : "Solicited");
- }
- else {
- PRINT_FAILURE_ERROR("\tPublish Type:", ret);
- return false;
- }
-
- ret = wifi_aware_publish_set_service_name(publish, g_service_name);
- if (ret == WIFI_AWARE_ERROR_NONE) {
- printf("\tService Name: %s\n", g_service_name);
- }
- else {
- PRINT_FAILURE_ERROR("\tService Name:", ret);
- 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("\tService Specific Info: ");
- __print_byte_array(specific_info, len);
- }
- else {
- PRINT_FAILURE_ERROR("\tService Specific Info:", ret);
- return false;
- }
-
- ret = wifi_aware_publish_enable_ranging(publish, g_ranging);
- if (ret == WIFI_AWARE_ERROR_NONE) {
- printf("\tRanging: %s\n", g_ranging ? "Enable" : "Disable");
- }
- else {
- PRINT_FAILURE_ERROR("\tRanging:", ret);
- return false;
- }
- return true;
-}
-
-static void start_publish()
-{
- printf("[Session][Publish] Create\n");
- int ret = wifi_aware_session_create(WIFI_AWARE_SESSION_PUBLISH, &g_wifi_aware_session);
- if (ret == WIFI_AWARE_ERROR_NONE) {
- printf("\tSession: %p\n", g_wifi_aware_session);
- } else {
- PRINT_FAILURE_ERROR("\tSession:", ret);
- __test_finish(0);
- }
-
- ret = wifi_aware_publish_create(&g_publish_handle);
- if (ret != WIFI_AWARE_ERROR_NONE)
- PRINT_FAILURE_ERROR("\tPublish Config:", ret);
-
- if (__set_publish_config(g_publish_handle) == false) {
- PRINT_FAILURE("\tPublish Config: Failure");
- __test_finish(0);
- return;
- }
-
- ret = wifi_aware_session_set_message_received_cb(g_wifi_aware_session, __message_received_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("\tMessage Received CB:", ret);
- __test_finish(0);
- }
-
- ret = wifi_aware_session_set_terminated_cb(g_wifi_aware_session, __session_terminated_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("\tSession Terminated CB:", ret);
- __test_finish(0);
- }
-
- printf("[Publish] Request\n");
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("[Publish]", ret);
- __test_finish(0);
- }
-}
-
-static void __enabled_cb(wifi_aware_error_e error, void *user_data)
-{
- if (error == WIFI_AWARE_ERROR_NONE) {
- PRINT_SUCCESS("[CB][Enable] Enabled");
- }
- else {
- PRINT_FAILURE_ERROR("[CB][Enable]", error);
- return;
- }
-
- start_publish();
-}
-
-void run_common()
-{
- int ret = 0;
- RET_IF_LOOP_IS_NULL();
-
- test_init();
- if (g_enabled == false) {
- ret = wifi_aware_enable(__enabled_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("[Enable]", ret);
- __test_finish(0);
- return;
- }
- g_enabled = true;
- } else {
- start_publish();
- }
-}
-
-void clear_resources()
-{
- if (g_ndp) {
- wifi_aware_data_path_unset_terminated_cb(g_ndp);
- wifi_aware_data_path_destroy(g_ndp);
- }
- g_ndp = NULL;
-
- if (g_publish_handle)
- wifi_aware_publish_destroy(g_publish_handle);
- g_publish_handle = NULL;
-
- if (g_wifi_aware_session) {
- wifi_aware_session_stop(g_wifi_aware_session);
- wifi_aware_session_unset_message_received_cb(g_wifi_aware_session);
- wifi_aware_session_unset_terminated_cb(g_wifi_aware_session);
- wifi_aware_session_destroy(g_wifi_aware_session);
- }
- g_wifi_aware_session = 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;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- g_ranging = false;
- run_common();
-}
-
-void test_open_solicited()
-{
- clear_resources();
- g_pub_type = WIFI_AWARE_PUBLISH_TYPE_SOLICITED;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- g_ranging = false;
- run_common();
-}
-
-void test_psk_unsolicited()
-{
- clear_resources();
- g_pub_type = WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- g_ranging = false;
- run_common();
-}
-
-void test_psk_solicited()
-{
- clear_resources();
- g_pub_type = WIFI_AWARE_PUBLISH_TYPE_SOLICITED;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- g_ranging = false;
- run_common();
-}
-
-void test_pmk_unsolicited()
-{
- clear_resources();
- g_pub_type = WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_PMK;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- g_ranging = false;
- memcpy(g_pmk, PMK, PMK_LEN);
- run_common();
-}
-
-void test_pmk_solicited()
-{
- clear_resources();
- g_pub_type = WIFI_AWARE_PUBLISH_TYPE_SOLICITED;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_PMK;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- g_ranging = false;
- memcpy(g_pmk, PMK, PMK_LEN);
- run_common();
-}
-
-void test_open_unsolicited_other_service()
-{
- clear_resources();
- g_pub_type = WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
- strncpy(g_service_name, OTHER_SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- g_ranging = false;
- run_common();
-}
-
-void test_open_solicited_other_service()
-{
- clear_resources();
- g_pub_type = WIFI_AWARE_PUBLISH_TYPE_SOLICITED;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
- strncpy(g_service_name, OTHER_SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- g_ranging = false;
- run_common();
-}
-
-void test_open_unsolicited_ranging()
-{
- clear_resources();
- g_pub_type = WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- g_ranging = true;
- run_common();
-}
-
-void test_open_solicited_ranging()
-{
- clear_resources();
- g_pub_type = WIFI_AWARE_PUBLISH_TYPE_SOLICITED;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- g_ranging = true;
- run_common();
-}
-
-void test_init()
-{
- int ret = 0;
-
- RET_IF_LOOP_IS_NULL();
-
- if (g_initialized == false) {
- ret = wifi_aware_initialize();
- if (ret == WIFI_AWARE_ERROR_NONE)
- PRINT_SUCCESS("[Initialize] Success");
- else
- PRINT_FAILURE_ERROR("[Initialize]", ret);
- g_initialized = true;
- }
-}
-
-void test_deinit()
-{
-}
-
-typedef void (*test_func)(void);
-test_func g_menu_func[] = {
- [CMD_QUIT] = test_quit,
- [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_PMK_UNSOLICITED] = test_pmk_unsolicited,
- [CMD_PMK_SOLICITED] = test_pmk_solicited,
- [CMD_OPEN_UNSOLICITED_OTHER_SERVICE] = test_open_unsolicited_other_service,
- [CMD_OPEN_SOLICITED_OTHER_SERVICE] = test_open_solicited_other_service,
- [CMD_OPEN_UNSOLICITED_RANGING] = test_open_unsolicited_ranging,
- [CMD_OPEN_SOLICITED_RANGING] = test_open_solicited_ranging,
-
- [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;
-
- 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 if (buf[0] == '0') {
- test_deinit();
- printf(MAKE_GREEN"Test is finished.."RESET_COLOR"\n");
- exit(1);
- } else if (buf[0] == '\n' || buf[0] == '\r') {
- __usage_full();
- } else if (n - 1 > 0 && n < 1024) {
- buf[n - 1] = '\0'; /* remove new line... */
- printf("\n\n");
- __process_input(buf, user_data);
- } else {
- printf("invalid input\n");
- }
-
- 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;
-}
+++ /dev/null
-/*
- * Wi-Fi Aware
- *
- * Copyright (c) 2020 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.
- *
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <glib.h>
-
-#include "wifi-aware.h"
-#include "wifi-aware-matching-filter-test.h"
-
-#define RET_IF_LOOP_IS_NULL()\
- do {\
- if (!g_main_loop_p) {\
- printf("Loop was not initialized\n");\
- return;\
- } \
- } while (0)
-
-#define SUBSCRIBE_TIMEOUT 7000
-
-static GMainLoop *g_main_loop_p;
-static guint g_timeout_id = 0;
-
-static test_case_e g_current_test = DUMMY_TEST_NUM;
-static bool g_initialized = false;
-static bool g_enabled = false;
-
-static bool g_test_result = false;
-
-static wifi_aware_session_h g_wifi_aware_session = NULL;
-static wifi_aware_subscribe_h g_subscribe_handle = NULL;
-
-void test_init();
-void test_deinit();
-
-static void __print_test_result(int num, bool result)
-{
- char test_name[20] = {0, };
-
- if (num < NEGATIVE_TEST_01)
- snprintf(test_name, 19, "%s %d", "Positivie Test", num);
- else
- snprintf(test_name, 19, "%s %d", "Negative Test", num - NEGATIVE_TEST_01 + 1);
-
- if (result)
- printf(MAKE_GREEN"[%s] is pass."RESET_COLOR"\n", test_name);
- else
- printf(MAKE_RED"[%s] is fail."RESET_COLOR"\n", test_name);
-
- printf("Prease enter to run the next test...\n");
-}
-
-void test_quit()
-{
- RET_IF_LOOP_IS_NULL();
-
- g_main_loop_quit(g_main_loop_p);
-
- return;
-}
-
-static void clear_resources()
-{
- if (g_subscribe_handle)
- wifi_aware_subscribe_destroy(g_subscribe_handle);
- g_subscribe_handle = NULL;
-
- if (g_wifi_aware_session) {
- wifi_aware_session_stop(g_wifi_aware_session);
- wifi_aware_session_unset_message_received_cb(g_wifi_aware_session);
- wifi_aware_session_unset_service_discovered_cb(g_wifi_aware_session);
- wifi_aware_session_unset_terminated_cb(g_wifi_aware_session);
- wifi_aware_session_destroy(g_wifi_aware_session);
- }
- g_wifi_aware_session = NULL;
-
- g_test_result = false;
-}
-
-static gboolean __subscribed_timeout_handler(gpointer user_data)
-{
- printf("Not discovered! reached timeout!\n");
-
- if (g_current_test >= NEGATIVE_TEST_01)
- g_test_result = true;
- else
- g_test_result = false;
-
- __print_test_result(g_current_test, g_test_result);
- clear_resources();
- return FALSE;
-}
-
-static void __subscribed_cb(wifi_aware_session_h session,
- wifi_aware_error_e error, void *user_data)
-{
- if (error == WIFI_AWARE_ERROR_NONE)
- g_timeout_id = g_timeout_add(SUBSCRIBE_TIMEOUT, __subscribed_timeout_handler, session);
- else
- g_main_loop_quit(g_main_loop_p);
-}
-
-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,
- int distance, void *user_data)
-{
- printf("discovered!!\n");
- if (g_current_test >= NEGATIVE_TEST_01)
- g_test_result = false;
- else
- g_test_result = true;
-
- if (g_timeout_id > 0) {
- g_source_remove(g_timeout_id);
- g_timeout_id = 0;
- }
- __print_test_result(g_current_test, g_test_result);
- wifi_aware_session_unset_service_discovered_cb(g_wifi_aware_session);
-}
-
-static bool __set_default_configures(void)
-{
- unsigned char specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
-
- int ret = wifi_aware_session_create(WIFI_AWARE_SESSION_SUBSCRIBE, &g_wifi_aware_session);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return false;
-
- ret = wifi_aware_subscribe_create(&g_subscribe_handle);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return false;
-
- ret = wifi_aware_subscribe_set_service_name(g_subscribe_handle, SERVICE_NAME);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return false;
-
- ret = wifi_aware_session_set_service_discovered_cb(g_wifi_aware_session,
- __service_discovered_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return false;
-
- size_t len = strlen(PUB_SSI);
- memcpy(specific_info, PUB_SSI, len);
- ret = wifi_aware_subscribe_set_service_specific_info(g_subscribe_handle, specific_info, len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return false;
-
- return true;
-}
-
-static void __enabled_cb(wifi_aware_error_e error, void *user_data)
-{
- if (error == WIFI_AWARE_ERROR_NONE)
- g_enabled = true;
- else
- g_enabled = false;
-
- g_main_loop_quit(g_main_loop_p);
-}
-
-void test_init()
-{
- int ret = 0;
-
- RET_IF_LOOP_IS_NULL();
-
- if (g_initialized == false) {
- ret = wifi_aware_initialize();
- if (ret != WIFI_AWARE_ERROR_NONE) {
- g_main_loop_quit(g_main_loop_p);
- return;
- }
- g_initialized = true;
- }
-
- if (g_enabled == false) {
- ret = wifi_aware_enable(__enabled_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- g_main_loop_quit(g_main_loop_p);
- return;
- }
- }
-}
-
-void test_deinit()
-{
- wifi_aware_disable();
- wifi_aware_deinitialize();
-}
-
-void test_active_no_filter()
-{
- PRINT_START(g_current_test);
-
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // No filter
- ret = wifi_aware_session_subscribe(g_wifi_aware_session,
- g_subscribe_handle, __subscribed_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_active_wildcard_filter()
-{
- PRINT_START(g_current_test);
-
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <0><0><0><0><0>
- ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
- test_matching_filter[0].values, test_matching_filter[0].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_subscribe(g_wifi_aware_session,
- g_subscribe_handle, __subscribed_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_active_value_filter_1()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <1,1><1,2><1,3><1,4><1,5>
- ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
- test_matching_filter[1].values, test_matching_filter[1].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_subscribe(g_wifi_aware_session,
- g_subscribe_handle, __subscribed_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_active_value_filter_3()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <0><1,2><0><1,4>
- ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
- test_matching_filter[3].values, test_matching_filter[3].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_subscribe(g_wifi_aware_session,
- g_subscribe_handle, __subscribed_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_active_value_filter_6()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <1,1><0><1,3><0><1,5>
- ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
- test_matching_filter[6].values, test_matching_filter[6].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_subscribe(g_wifi_aware_session,
- g_subscribe_handle, __subscribed_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_passive_no_filter()
-{
- PRINT_START(g_current_test);
-
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // No filter
- ret = wifi_aware_session_subscribe(g_wifi_aware_session,
- g_subscribe_handle, __subscribed_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_passive_wildcard_filter()
-{
- PRINT_START(g_current_test);
-
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <0><0><0><0><0>
- ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
- test_matching_filter[0].values, test_matching_filter[0].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_subscribe(g_wifi_aware_session,
- g_subscribe_handle, __subscribed_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_passive_value_filter_1()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <1,1><1,2><1,3><1,4><1,5>
- ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
- test_matching_filter[1].values, test_matching_filter[1].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_subscribe(g_wifi_aware_session,
- g_subscribe_handle, __subscribed_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_passive_value_filter_2()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <1,1><1,2><1,1><1,4><1,5>
- ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
- test_matching_filter[2].values, test_matching_filter[2].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_subscribe(g_wifi_aware_session,
- g_subscribe_handle, __subscribed_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_passive_value_filter_4()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <0><1,2><1,3><0><1,5>
- ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
- test_matching_filter[4].values, test_matching_filter[4].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_subscribe(g_wifi_aware_session,
- g_subscribe_handle, __subscribed_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-void test_passive_value_filter_5()
-{
- PRINT_START(g_current_test);
- if (__set_default_configures() == false)
- goto ERR;
-
- int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- // <1,1><0><1,3><0>
- ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
- test_matching_filter[5].values, test_matching_filter[5].len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
-
- ret = wifi_aware_session_subscribe(g_wifi_aware_session,
- g_subscribe_handle, __subscribed_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE)
- goto ERR;
- return;
-
-ERR:
- g_test_result = false;
- __print_test_result(g_current_test, g_test_result);
-}
-
-typedef void (*test_func)(void);
-test_func g_menu_func[] = {
- [0] = NULL,
- // active
- [POSITIVE_TEST_01] = test_active_no_filter,
- [POSITIVE_TEST_02] = test_active_no_filter,
- [POSITIVE_TEST_03] = test_active_wildcard_filter,
- [POSITIVE_TEST_04] = test_active_no_filter,
-
- [POSITIVE_TEST_05] = test_active_wildcard_filter,
- [POSITIVE_TEST_06] = test_active_value_filter_1,
- [POSITIVE_TEST_07] = test_active_value_filter_1,
-
- [POSITIVE_TEST_08] = test_active_value_filter_6,
- [POSITIVE_TEST_09] = test_active_value_filter_1,
- [POSITIVE_TEST_10] = test_active_value_filter_3,
-
- // passive
- [POSITIVE_TEST_11] = test_passive_no_filter,
- [POSITIVE_TEST_12] = test_passive_wildcard_filter,
- [POSITIVE_TEST_13] = test_passive_no_filter,
- [POSITIVE_TEST_14] = test_passive_no_filter,
-
- [POSITIVE_TEST_15] = test_passive_value_filter_1,
- [POSITIVE_TEST_16] = test_passive_wildcard_filter,
- [POSITIVE_TEST_17] = test_passive_value_filter_1,
-
- [POSITIVE_TEST_18] = test_passive_value_filter_1,
- [POSITIVE_TEST_19] = test_passive_value_filter_4,
- [POSITIVE_TEST_20] = test_passive_value_filter_5,
-
- // custom: unsol + active
- [POSITIVE_TEST_21] = test_active_no_filter,
- [POSITIVE_TEST_22] = test_active_no_filter,
- [POSITIVE_TEST_23] = test_active_wildcard_filter,
- [POSITIVE_TEST_24] = test_active_no_filter,
-
- [POSITIVE_TEST_25] = test_active_wildcard_filter,
- [POSITIVE_TEST_26] = test_active_value_filter_1,
- [POSITIVE_TEST_27] = test_active_value_filter_1,
-
- [POSITIVE_TEST_28] = test_active_value_filter_6,
- [POSITIVE_TEST_29] = test_active_value_filter_1,
- [POSITIVE_TEST_30] = test_active_value_filter_3,
-
- // active
- [NEGATIVE_TEST_01] = test_active_value_filter_1,
- [NEGATIVE_TEST_02] = test_active_value_filter_1,
- [NEGATIVE_TEST_03] = test_active_value_filter_1,
-
- // passive
- [NEGATIVE_TEST_04] = test_passive_value_filter_1,
- [NEGATIVE_TEST_05] = test_passive_value_filter_2,
- [NEGATIVE_TEST_06] = test_passive_value_filter_1,
-
- // custom: unsol + active
- [NEGATIVE_TEST_07] = test_active_value_filter_1,
- [NEGATIVE_TEST_08] = test_active_value_filter_1,
- [NEGATIVE_TEST_09] = test_active_value_filter_1,
-
- [INVALID] = NULL,
-};
-
-static gboolean __test_terminal_read_std_input(GIOChannel * source,
- GIOCondition condition, gpointer user_data)
-{
- int fd = 0;
- static char buf[1024];
- int 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 {
- ++g_current_test;
- if (g_current_test == INVALID) {
- printf("Test is finished..\n");
- g_main_loop_quit(g_main_loop_p);
- return TRUE;
- }
- clear_resources();
- g_menu_func[g_current_test]();
- }
- 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);
-
- printf(MAKE_GREEN"\nThis is Matching filter test on Subscriber side"RESET_COLOR"\n");
-
- test_init();
- g_main_loop_run(g_main_loop_p);
- if (!g_enabled) {
- printf(MAKE_RED"Can not start test.."RESET_COLOR"\n");
- g_main_loop_unref(g_main_loop_p);
- return 0;
- }
-
- 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);
-
- printf("Please enter to start test..\n");
-
- g_main_loop_run(g_main_loop_p);
-
- g_main_loop_unref(g_main_loop_p);
- test_deinit();
-
- return 0;
-}
+++ /dev/null
-/*
- * Wi-Fi Aware
- *
- * Copyright (c) 2020 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 PRINT_SUCCESS(format, args...) printf(MAKE_GREEN format RESET_COLOR "\n", ##args)
-#define PRINT_FAILURE(format, args...) printf(MAKE_RED format RESET_COLOR "\n", ##args)
-#define PRINT_FAILURE_ERROR(format, error) printf(MAKE_RED format " Failure(%d:%s)" RESET_COLOR "\n", error, __print_error(error))
-
-#define SERVICE_NAME "Wi-Fi Aware Test"
-#define OTHER_SERVICE_NAME "Other Service Name"
-#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 WRONG_PSK "11223344"
-#define PMK_LEN 32
-#define PMK "12345678901234567890123456789012"
-#define WRONG_PMK "11223344556677889900123456789012"
-#define IPV6_ADDRESS_LEN 39
-
-#define SUBSCRIBE_TIMEOUT 5000
-
-enum {
- CMD_QUIT,
-
- CMD_OPEN_PASSIVE,
- CMD_OPEN_ACTIVE,
- CMD_PSK_PASSIVE,
- CMD_PSK_ACTIVE,
- CMD_PMK_PASSIVE,
- CMD_PMK_ACTIVE,
- CMD_WRONG_PSK_PASSIVE,
- CMD_WRONG_PSK_ACTIVE,
- CMD_WRONG_PMK_PASSIVE,
- CMD_WRONG_PMK_ACTIVE,
-
- CMD_OPEN_PASSIVE_OTHER_SERVICE,
- CMD_OPEN_ACTIVE_OTHER_SERVICE,
-
- CMD_OPEN_PASSIVE_RANGING,
- CMD_OPEN_ACTIVE_RANGING,
-
- CMD_INVALID,
-};
-
-static char *g_menu_str[] = {
- [CMD_QUIT]
- = "Quit",
- [CMD_OPEN_PASSIVE]
- = "Open + Passive",
- [CMD_OPEN_ACTIVE]
- = "Open + Active",
- [CMD_PSK_PASSIVE]
- = "PSK + Passive",
- [CMD_PSK_ACTIVE]
- = "PSK + Active",
- [CMD_PMK_PASSIVE]
- = "PMK + Passive",
- [CMD_PMK_ACTIVE]
- = "PMK + Active",
- [CMD_WRONG_PSK_PASSIVE]
- = "PSK + Passive (Wrong password)",
- [CMD_WRONG_PSK_ACTIVE]
- = "PSK + Active (Wrong password)",
- [CMD_WRONG_PMK_PASSIVE]
- = "PMK + Passive (Wrong password)",
- [CMD_WRONG_PMK_ACTIVE]
- = "PMK + Active (Wrong password)",
- [CMD_OPEN_PASSIVE_OTHER_SERVICE]
- = "Open + Passive + Other Service",
- [CMD_OPEN_ACTIVE_OTHER_SERVICE]
- = "Open + Active + Other Service",
- [CMD_OPEN_PASSIVE_RANGING]
- = "Open + Passive + Ranging",
- [CMD_OPEN_ACTIVE_RANGING]
- = "Open + Active + Ranging",
- [CMD_INVALID]
- = NULL,
-};
-
-static GMainLoop *g_main_loop_p;
-static int g_min_distance = -1;
-static int g_max_distance = -1;
-
-static bool g_initialized = false;
-static bool g_enabled = 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 char g_service_name[MAX_SERVICE_NAME_LEN + 1] = {0, };
-static char g_psk[MAX_PSK_LEN + 1];
-static unsigned char g_pmk[PMK_LEN];
-
-static bool g_service_discovered = false;
-static guint g_timeout_id = 0;
-
-void test_init();
-void test_deinit();
-
-static void __test_finish(int is_validated)
-{
- if (is_validated)
- printf(MAKE_GREEN"[TEST RESULT] Wi-Fi Aware validated!"RESET_COLOR"\n");
- else
- printf(MAKE_RED"[TEST RESULT] Wi-Fi Aware invalidated!"RESET_COLOR"\n");
- test_deinit();
-}
-
-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;
-}
-
-static const char *__print_error(wifi_aware_error_e err)
-{
- switch (err) {
- case WIFI_AWARE_ERROR_NONE:
- return "NO ERROR";
- 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";
- case WIFI_AWARE_ERROR_ALREADY_ENABLED:
- return "ALREADY_ENABLED";
- case WIFI_AWARE_ERROR_REJECTED_BY_PEER:
- return "REJECTED_BY_PEER";
- case WIFI_AWARE_ERROR_INTERFACE_DOWN:
- return "INTERFACE_IS_DOWN";
- default:
- return "UNKNOWN";
- }
-}
-
-static void __print_mac(unsigned char mac[6])
-{
- printf(MACSTR, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
-}
-
-static 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");
-}
-
-static void test_quit()
-{
- RET_IF_LOOP_IS_NULL();
-
- printf("Bye\n");
- g_main_loop_quit(g_main_loop_p);
-
- return;
-}
-
-static gboolean __subscribed_timeout_handler(gpointer user_data)
-{
- wifi_aware_session_h session = (wifi_aware_session_h) user_data;
- PRINT_FAILURE("[TIMEOUT][Subscribe] Session: %p", session);
- __test_finish(0);
- return FALSE;
-}
-
-static void __subscribed_cb(wifi_aware_session_h session,
- wifi_aware_error_e error, void *user_data)
-{
- if (error == WIFI_AWARE_ERROR_NONE) {
- PRINT_SUCCESS("[CB][Subscribe] Requested");
- g_timeout_id = g_timeout_add(SUBSCRIBE_TIMEOUT, __subscribed_timeout_handler, session);
- } else {
- PRINT_FAILURE_ERROR("[CB][Subscribe]", error);
- __test_finish(0);
- }
-}
-
-static void __data_path_terminated_cb(wifi_aware_data_path_h data_path,
- wifi_aware_termination_reason_e reason, void *user_data)
-{
- printf("[EVENT][DataPath Terminated] %p", 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;
- PRINT_SUCCESS("[CB][OpenDataPath] Connected %p", data_path);
- ret = wifi_aware_data_path_get_interface(data_path, &interface);
- if (ret == WIFI_AWARE_ERROR_NONE)
- printf("\tInterface: %s\n", interface);
- else
- PRINT_FAILURE_ERROR("\tInterface:", ret);
-
- 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("\tPeer's IPv6 Address: %s\n", ipv6_address);
- else
- PRINT_FAILURE_ERROR("\tPeer's IPv6 Address:", ret);
- ret = wifi_aware_data_path_get_peer_port(data_path, &port);
- if (ret == WIFI_AWARE_ERROR_NONE)
- printf("\tPeer's Port: %d\n", port);
- else
- PRINT_FAILURE_ERROR("\tPeer's Port:", ret);
- __test_finish(1);
- }
- else {
- PRINT_FAILURE("[CB][OpenDataPath] Failure(%d:%s) %p", error, __print_error(error), data_path);
- __test_finish(0);
- }
-}
-
-static void __open_data_path(wifi_aware_session_h session)
-{
- printf("[OpenDataPath] Request\n");
- if (g_peer == NULL) {
- printf("Peer is NULL\n");
- __test_finish(0);
- return;
- }
-
- int ret = wifi_aware_data_path_create(session, g_peer, &g_ndp);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("\tHandle:", ret);
- __test_finish(0);
- return;
- }
- printf("\tHandle: %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) {
- PRINT_FAILURE_ERROR("\tSet TerminatedCallback:", ret);
- __test_finish(0);
- return;
- }
-
- ret = wifi_aware_data_path_set_security(g_ndp, g_security_type);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("\tSecurity Type:", ret);
- __test_finish(0);
- return;
- }
- printf("\tSecurity Type: ");
- switch (g_security_type) {
- case WIFI_AWARE_SECURITY_TYPE_OPEN:
- printf("Open\n");
- break;
- case WIFI_AWARE_SECURITY_TYPE_PSK:
- printf("PSK\n");
- wifi_aware_data_path_set_psk(g_ndp, g_psk);
- break;
- case WIFI_AWARE_SECURITY_TYPE_PMK:
- wifi_aware_data_path_set_pmk(g_ndp, g_pmk);
- printf("PMK\n");
- break;
- default:
- PRINT_FAILURE("Unknown security type");
- __test_finish(0);
- return;
- }
-
- ret = wifi_aware_data_path_open(g_ndp, __open_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("[OpenDataPath]", ret);
- __test_finish(0);
- }
-}
-
-static void __message_result_cb(wifi_aware_session_h session, wifi_aware_error_e error, void *user_data)
-{
- if (error == WIFI_AWARE_ERROR_NONE) {
- PRINT_SUCCESS("[CB][Send Message] Requested");
- } else {
- PRINT_FAILURE_ERROR("[CB][Send Message]", error);
- __test_finish(0);
- }
-}
-
-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("[Event][Receive Message]\n");
-
- if (wifi_aware_peer_get_mac(peer, &mac) != WIFI_AWARE_ERROR_NONE) {
- printf("\tPeer's NMI: Failure(%d:%s)\n", ret, __print_error(ret));
- } else {
- printf("\tPeer's NMI: ");
- if (mac != NULL) {
- __print_mac(mac);
- }
- printf("\n");
- }
-
- if (mac)
- g_free(mac);
-
- memcpy(buf, message, 1024);
- buf[len] = '\0';
- printf("\tMessage: %s\n", buf);
-
- 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,
- int distance, void *user_data)
-{
- unsigned char *mac = NULL;
- int ret = 0;
-
- PRINT_SUCCESS("[Event][Serivce Discovered]");
- if (g_timeout_id > 0) {
- g_source_remove(g_timeout_id);
- g_timeout_id = 0;
- }
-
- ret = wifi_aware_peer_get_mac(peer, &mac);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- printf("\tPeer's NMI: Failure(%d:%s)\n", ret, __print_error(ret));
- if (mac)
- g_free(mac);
- __test_finish(0);
- return;
- } else {
- printf("\tPeer's NMI: ");
- if (mac != NULL) {
- __print_mac(mac);
- g_free(mac);
- }
- printf("\n");
- }
- printf("\tService Specific Info(%d): ", service_specific_info_len);
- __print_byte_array(service_specific_info, service_specific_info_len);
- printf("\n");
- printf("\tDistance: %d\n", distance);
-
- if (g_service_discovered == false) {
- g_service_discovered = true;
- printf("[Send Message] Request\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_session_send_message(session, peer, send_message, message_len, __message_result_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("[Send Message]", ret);
- __test_finish(0);
- }
- }
-}
-
-static void __session_terminated_cb(wifi_aware_session_h session,
- wifi_aware_termination_reason_e reason, void *user_data)
-{
- PRINT_FAILURE("[Event][Session Terminated] Session: %p Reason: %d", session, reason);
-
- if (session != NULL) {
- wifi_aware_session_unset_message_received_cb(session);
- wifi_aware_session_destroy(session);
- }
- __test_finish(0);
-}
-
-static bool __set_subscribe_config(wifi_aware_subscribe_h subscribe)
-{
- int ret = 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("\tSubscribe Type: %s\n", g_sub_type == WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE ?
- "Passive" : "Active");
- }
- else {
- PRINT_FAILURE_ERROR("\tSubscribe Type:", ret);
- return false;
- }
-
- ret = wifi_aware_subscribe_set_service_name(subscribe,
- g_service_name);
- if (ret == WIFI_AWARE_ERROR_NONE) {
- printf("\tService Name: %s\n", g_service_name);
- }
- else {
- PRINT_FAILURE_ERROR("\tService Name:", ret);
- 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("\tService Specific Info: ");
- __print_byte_array(specific_info, len);
- }
- else {
- PRINT_FAILURE_ERROR("\tService Specific Info:", ret);
- return false;
- }
-
- if (g_min_distance >= 0) {
- ret = wifi_aware_subscribe_set_min_distance(subscribe, g_min_distance);
- if (ret == WIFI_AWARE_ERROR_NONE) {
- printf("\tMin Distance: %d\n", g_min_distance);
- }
- else {
- PRINT_FAILURE_ERROR("\tMin Distance:", ret);
- return false;
- }
- }
- if (g_max_distance > 0) {
- ret = wifi_aware_subscribe_set_max_distance(subscribe, g_max_distance);
- if (ret == WIFI_AWARE_ERROR_NONE) {
- printf("\tMax Distance: %d\n", g_min_distance);
- }
- else {
- PRINT_FAILURE_ERROR("\tMax Distance:", ret);
- return false;
- }
- }
- return true;
-}
-
-static void start_subscribe()
-{
- printf("[Session][Subscribe] Create\n");
- int ret = wifi_aware_session_create(WIFI_AWARE_SESSION_SUBSCRIBE, &g_wifi_aware_session);
- if (ret == WIFI_AWARE_ERROR_NONE) {
- printf("\tSession: %p\n", g_wifi_aware_session);
- } else {
- PRINT_FAILURE_ERROR("\tSession:", ret);
- __test_finish(0);
- }
-
- ret = wifi_aware_subscribe_create(&g_subscribe_handle);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("\tSubscribe Config:", ret);
- __test_finish(0);
- }
-
- if (__set_subscribe_config(g_subscribe_handle) == false) {
- PRINT_FAILURE("\tSubscribeConfig: Failure");
- __test_finish(0);
- return;
- }
-
- g_service_discovered = false;
- ret = wifi_aware_session_set_service_discovered_cb(g_wifi_aware_session, __service_discovered_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("\tService Discovered CB:", ret);
- __test_finish(0);
- }
-
- ret = wifi_aware_session_set_message_received_cb(g_wifi_aware_session, __message_received_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("\tMessage Received CB:", ret);
- __test_finish(0);
- }
-
- ret = wifi_aware_session_set_terminated_cb(g_wifi_aware_session, __session_terminated_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("\tSession Terminated CB:", ret);
- __test_finish(0);
- }
-
- printf("[Subscribe] Request\n");
- ret = wifi_aware_session_subscribe(g_wifi_aware_session, g_subscribe_handle, __subscribed_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("[Subscribe]", ret);
- __test_finish(0);
- }
-}
-
-static void __enabled_cb(wifi_aware_error_e error, void *user_data)
-{
- if (error == WIFI_AWARE_ERROR_NONE) {
- PRINT_SUCCESS("[CB][Enable] Enabled");
- }
- else {
- PRINT_FAILURE_ERROR("[CB][Enable]", error);
- return;
- }
-
- start_subscribe();
-}
-
-void run_common()
-{
- int ret = 0;
- RET_IF_LOOP_IS_NULL();
-
- test_init();
- if (g_enabled == false) {
- ret = wifi_aware_enable(__enabled_cb, NULL);
- if (ret != WIFI_AWARE_ERROR_NONE) {
- PRINT_FAILURE_ERROR("[Enable]", ret);
- __test_finish(0);
- return;
- }
- g_enabled = true;
- } else {
- start_subscribe();
- }
-}
-
-void clear_resources()
-{
- if (g_ndp) {
- wifi_aware_data_path_unset_terminated_cb(g_ndp);
- wifi_aware_data_path_destroy(g_ndp);
- }
- g_ndp = NULL;
-
- if (g_subscribe_handle)
- wifi_aware_subscribe_destroy(g_subscribe_handle);
- g_subscribe_handle = NULL;
-
- if (g_wifi_aware_session) {
- wifi_aware_session_stop(g_wifi_aware_session);
- wifi_aware_session_unset_message_received_cb(g_wifi_aware_session);
- wifi_aware_session_unset_service_discovered_cb(g_wifi_aware_session);
- wifi_aware_session_unset_terminated_cb(g_wifi_aware_session);
- wifi_aware_session_destroy(g_wifi_aware_session);
- }
- g_wifi_aware_session = 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;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- run_common();
-}
-
-void test_open_active()
-{
- clear_resources();
- g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- run_common();
-}
-
-void test_psk_passive()
-{
- clear_resources();
- g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- strncpy(g_psk, PSK, MAX_PSK_LEN);
- g_psk[MAX_PSK_LEN] = '\0';
- run_common();
-}
-
-void test_psk_active()
-{
- clear_resources();
- g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- strncpy(g_psk, PSK, MAX_PSK_LEN);
- g_psk[MAX_PSK_LEN] = '\0';
- run_common();
-}
-
-void test_pmk_passive()
-{
- clear_resources();
- g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_PMK;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- memcpy(g_pmk, PMK, PMK_LEN);
- run_common();
-}
-
-void test_pmk_active()
-{
- clear_resources();
- g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_PMK;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- memcpy(g_pmk, PMK, PMK_LEN);
- run_common();
-}
-
-void test_wrong_psk_passive()
-{
- clear_resources();
- g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- strncpy(g_psk, WRONG_PSK, MAX_PSK_LEN);
- g_psk[MAX_PSK_LEN] = '\0';
- run_common();
-}
-
-void test_wrong_psk_active()
-{
- clear_resources();
- g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- strncpy(g_psk, WRONG_PSK, MAX_PSK_LEN);
- g_psk[MAX_PSK_LEN] = '\0';
- run_common();
-}
-
-void test_wrong_pmk_passive()
-{
- clear_resources();
- g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_PMK;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- memcpy(g_pmk, WRONG_PMK, PMK_LEN);
- run_common();
-}
-
-void test_wrong_pmk_active()
-{
- clear_resources();
- g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_PMK;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- memcpy(g_pmk, WRONG_PMK, PMK_LEN);
- run_common();
-}
-
-void test_open_passive_other_service()
-{
- clear_resources();
- g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
- strncpy(g_service_name, OTHER_SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- run_common();
-}
-
-void test_open_active_other_service()
-{
- clear_resources();
- g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
- strncpy(g_service_name, OTHER_SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- run_common();
-}
-
-void test_open_passive_ranging()
-{
- clear_resources();
- g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- g_min_distance = 0;
- g_max_distance = 100000;
- run_common();
-}
-
-void test_open_active_ranging()
-{
- clear_resources();
- g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
- g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
- strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
- g_min_distance = 0;
- g_max_distance = 100000;
- run_common();
-}
-
-void test_init()
-{
- int ret = 0;
-
- RET_IF_LOOP_IS_NULL();
-
- if (g_initialized == false) {
- ret = wifi_aware_initialize();
- if (ret == WIFI_AWARE_ERROR_NONE)
- PRINT_SUCCESS("[Initialize] Success");
- else
- PRINT_FAILURE_ERROR("[Initialize]", ret);
- g_initialized = true;
- }
-}
-
-void test_deinit()
-{
-}
-
-typedef void (*test_func)(void);
-test_func g_menu_func[] = {
- [CMD_QUIT] = test_quit,
- [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_PMK_PASSIVE] = test_pmk_passive,
- [CMD_PMK_ACTIVE] = test_pmk_active,
- [CMD_WRONG_PSK_PASSIVE] = test_wrong_psk_passive,
- [CMD_WRONG_PSK_ACTIVE] = test_wrong_psk_active,
- [CMD_WRONG_PMK_PASSIVE] = test_wrong_pmk_passive,
- [CMD_WRONG_PMK_ACTIVE] = test_wrong_pmk_active,
- [CMD_OPEN_PASSIVE_OTHER_SERVICE] = test_open_passive_other_service,
- [CMD_OPEN_ACTIVE_OTHER_SERVICE] = test_open_active_other_service,
- [CMD_OPEN_PASSIVE_RANGING] = test_open_passive_ranging,
- [CMD_OPEN_ACTIVE_RANGING] = test_open_active_ranging,
-
- [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;
-
- 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 if (buf[0] == '0') {
- test_deinit();
- printf(MAKE_GREEN"Test is finished.."RESET_COLOR"\n");
- exit(1);
- } else if (buf[0] == '\n' || buf[0] == '\r') {
- __usage_full();
- } else if (n - 1 > 0 && n < 1024) {
- buf[n - 1] = '\0'; /* remove new line... */
- printf("\n\n");
- __process_input(buf, user_data);
- } else {
- printf("invalid input\n");
- }
-
- 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;
-}
+++ /dev/null
-/*
- * Wi-Fi Aware
- *
- * Copyright (c) 2020 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 RET_IF_SESSION_IS_NULL()\
- do {\
- if (!g_wifi_aware_session) {\
- printf("wifi-aware-session handle is not created yet\n");\
- return;\
- }\
- } while (0)
-
-#define RET_IF_NDP_IS_NULL()\
- do {\
- if (!g_ndp) {\
- printf("wifi_aware_data_path handle is not created yet\n");\
- return;\
- }\
- } while (0)
-
-#define MATCH_FILTER "bytes used for matching"
-#define PUB_SSI "Extra bytes in the publisher discovery"
-#define UPDATE_PUB_SSI "Updated Specific Information"
-#define SUB_SSI "Arbitrary bytes for the subscribe discovery"
-#define UPDATE_SUB_SSI "Updated Arbitrary bytes for the subscribe discovery"
-#define MSG_SUB_TO_PUB "Let's talk"
-#define MSG_PUB_TO_SUB "Ready"
-#define PASSPHRASE "Some super secret password"
-
-#define MAX_SERVICE_NAME_LEN 255
-#define MAX_SPECIFIC_INFO_LEN 1024
-#define MAX_MATCH_FILTER_LEN 255
-
-#define STR_(X) #X
-#define STR(X) STR_(X)
-
-#define MAX_PSK_LEN 63
-#define PMK_LEN 32
-#define IPV6_ADDRESS_LEN 39
-
-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,
-
- CMD_INITIALIZE,
- CMD_DEINITIALIZE,
-
- CMD_ENABLE,
- CMD_DISABLE,
-
- CMD_PUBLISH,
- CMD_CANCEL_PUBLISH,
- CMD_UPDATE_PUBLISH,
- CMD_SUBSCRIBE,
- CMD_UPDATE_SUBSCRIBE,
- CMD_CANCEL_SUBSCRIBE,
-
- CMD_MESSAGE_RECV_CB,
- CMD_SEND_MESSAGE,
-
- CMD_OPEN_DATA_PATH,
-
- CMD_TEST_SPECIFIC_INFO,
- CMD_INVALID,
-};
-
-/*****************************************************************************
- * Global Variables
- *****************************************************************************/
-
-static char *g_menu_str[] = {
- [CMD_QUIT]
- = "QUIT",
- [CMD_FULL_MENU]
- = "FULL_MENU",
- [CMD_INITIALIZE]
- = "INITIALIZE",
- [CMD_DEINITIALIZE]
- = "DEINITIALIZE",
-
- [CMD_ENABLE]
- = "ENABLE",
- [CMD_DISABLE]
- = "DISABLE",
-
- [CMD_PUBLISH]
- = "PUBLISH",
- [CMD_CANCEL_PUBLISH]
- = "CANCEL PUBLISH",
- [CMD_UPDATE_PUBLISH]
- = "UPDATE PUBLISH",
- [CMD_SUBSCRIBE]
- = "SUBSCRIBE",
- [CMD_UPDATE_SUBSCRIBE]
- = "UPDATE_SUBSCRIBE",
- [CMD_CANCEL_SUBSCRIBE]
- = "CANCEL SUBSCRIBE",
-
- [CMD_MESSAGE_RECV_CB]
- = "SET MESSAGE_RECV CB",
- [CMD_SEND_MESSAGE]
- = "SEND MESSAGE",
- [CMD_OPEN_DATA_PATH]
- = "OPEN DATA PATH",
-
- [CMD_TEST_SPECIFIC_INFO]
- = "TEST SPECIFIC INFO",
- [CMD_INVALID]
- = NULL,
-};
-
-static GMainLoop *g_main_loop_p;
-static wifi_aware_session_h g_wifi_aware_session = NULL;
-static wifi_aware_publish_h g_publish_handle = NULL;
-static wifi_aware_subscribe_h g_subscribe_handle = NULL;
-static wifi_aware_data_path_h g_ndp = NULL;
-static int g_session_type = -1; // 0: publish, 1: subscribe
-static unsigned char g_msg[4];
-
-/*****************************************************************************
- * Local Functions Definition
- *****************************************************************************/
-
-static inline char* __cmd_transform(char*str)
-{
- int i, j;
- int len;
- static char static_buffer[255];
-
- if (str == NULL)
- return "";
-
- len = strlen(str);
- if (len == 0)
- return "";
-
- /* lower char */
- /* replance "_" to space */
- for (i = 0, j = 0; i < len; i++, j++) {
-
- if (str[j] >= 'A' && str[j] <= 'Z')
- static_buffer[i] = str[j] + 'a' - 'A';
- else if (str[j] == '_')
- static_buffer[i] = ' ';
- else
- static_buffer[i] = str[j];
- }
- static_buffer[j] = '\0';
-
- return static_buffer;
-}
-
-static inline void __usage_full()
-{
- int i;
- printf("Call Test Program\n");
-
- for (i = CMD_QUIT; i < CMD_INVALID; i++) {
- if (i%3 == 0)
- printf("\n");
- printf(" %02d: %-32s ", i,
- __cmd_transform(g_menu_str[i]));
- }
- printf("\n");
-}
-
-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";
- case WIFI_AWARE_ERROR_ALREADY_ENABLED:
- return "ALREADY_ENABLED";
- case WIFI_AWARE_ERROR_REJECTED_BY_PEER:
- return "REJECTED_BY_PEER";
- case WIFI_AWARE_ERROR_INTERFACE_DOWN:
- return "INTERFACE_IS_DOWN";
- default:
- return "UNKNOWN";
- }
-}
-
-static void __print_result(int ret, gchar *function_name)
-{
- if (ret == WIFI_AWARE_ERROR_NONE) {
- printf(MAKE_GREEN"%s"RESET_COLOR"\n", function_name);
- } else {
- printf(MAKE_RED"%s: %s ", function_name, __print_error(ret));
- printf(RESET_COLOR"\n");
- }
-
- printf("%s() result=[%d]\n", function_name, ret);
-}
-
-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 (strchr(" &;`'\"|*?~<>^()[]{}$", *ptr))
- printf("\\");
- 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;
-}
-
-void test_init()
-{
- int ret = 0;
-
- RET_IF_LOOP_IS_NULL();
-
- ret = wifi_aware_initialize();
- __print_result(ret, "wifi_aware_initialize");
-}
-
-void test_deinit()
-{
- int ret = 0;
-
- RET_IF_LOOP_IS_NULL();
-
- ret = wifi_aware_deinitialize();
- __print_result(ret, "wifi_aware_deinitialize");
-}
-
-
-static void __enabled_cb(wifi_aware_error_e error, void *user_data)
-{
- if (error == WIFI_AWARE_ERROR_NONE)
- printf("Wi-Fi Aware is enabled\n");
- else
- printf("Wi-Fi Aware is not enabled. Error: %s\n", __print_error(error));
-}
-
-void test_enable()
-{
- int ret = 0;
-
- RET_IF_LOOP_IS_NULL();
-
- ret = wifi_aware_enable(__enabled_cb, NULL);
- __print_result(ret, "wifi_aware_enable");
-}
-
-void test_disable()
-{
- int ret = 0;
-
- RET_IF_LOOP_IS_NULL();
-
- ret = wifi_aware_disable();
- __print_result(ret, "wifi_aware_disable");
-}
-
-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. Publish ID: [*]\n");
- else
- printf("Publish Request fails. Error: %s\n", __print_error(error));
-}
-
-static void __print_match_filter(unsigned char filter[MAX_MATCH_FILTER_LEN + 1], int filter_len)
-{
- int len = 0;
- int i = 0;
- while (i < filter_len) {
- len = filter[i];
- printf("%d: ", len);
- for (int j = 1; j <= len; ++j) {
- printf("%d, ", filter[i + j]);
- }
- printf("\n");
- i += len + 1;
- }
-}
-
-static int __set_match_filter(unsigned char match_filter[MAX_MATCH_FILTER_LEN + 1])
-{
- int total_num;
- int total_len = 0;
- int len;
- char filter[MAX_MATCH_FILTER_LEN + 1];
- printf("Total number of match filter items: ");
- if (scanf("%d", &total_num) < 0)
- return -1;
-
- if (total_num < 0 || total_num > MAX_MATCH_FILTER_LEN) {
- printf("Too many match filters");
- return -1;
- }
- for (int i = 0; i < total_num; ++i) {
- if (total_len > MAX_MATCH_FILTER_LEN) {
- printf("The length of filter value is longer than max length(%d)", MAX_MATCH_FILTER_LEN);
- return -1;
- }
- printf("%d match filter len: ", i);
- if (scanf("%d", &len) < 0)
- return -1;
- if (len <= 0 || len > MAX_MATCH_FILTER_LEN) {
- printf("Invalid match filter lentgh");
- return -1;
- }
- if (total_len + len > MAX_MATCH_FILTER_LEN) {
- printf("The length of filter value is longer than max length(%d)", MAX_MATCH_FILTER_LEN);
- return -1;
- }
- printf("%d match filter value: ", i);
- if (scanf("%" STR(MAX_MATCH_FILTER_LEN) "s", filter) < 0)
- return -1;
- if (len != strlen(filter)) {
- printf("The length of filter value is not %d\n", len);
- return -1;
- }
- match_filter[total_len++] = len;
- memcpy(&match_filter[total_len], filter, len);
- total_len += len;
- }
- return total_len;
-}
-
-static void __set_publish_config(wifi_aware_publish_h publish)
-{
- int ret = 0;
- int publish_type = 0;
- char service_name[MAX_SERVICE_NAME_LEN + 1] = {0, };
- char str_specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
- unsigned char specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
- unsigned char match_filter[MAX_MATCH_FILTER_LEN + 1] = {0, };
- size_t len = 0;
- int set_info = 0;
- int set_mf = 0;
-
- printf("Service Name: ");
- if (scanf(" %" STR(MAX_SERVICE_NAME_LEN) "s", service_name) < 0)
- return;
-
- ret = wifi_aware_publish_set_service_name(publish,
- service_name);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return;
-
- printf("Choose publish type(0-Solicited, 1-Unsolicited): ");
- if (scanf("%d", &publish_type) < 0)
- return;
-
- ret = wifi_aware_publish_set_type(publish,
- publish_type ? WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED
- : WIFI_AWARE_PUBLISH_TYPE_SOLICITED);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return;
-
- printf("Do you want to set Service Specific Information? (0-No, 1-Yes) ");
- if (scanf("%d", &set_info) < 0)
- return;
-
- if (set_info == 1) {
- if (scanf(" %" STR(MAX_SPECIFIC_INFO_LEN) "s", str_specific_info) < 0)
- return;
-
- len = strlen(str_specific_info);
- memcpy(specific_info, str_specific_info, len);
- ret = wifi_aware_publish_set_service_specific_info(publish,
- specific_info, len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return;
- }
-
- printf("Do you want to set Match Filter? (0-No, 1-Yes) ");
- if (scanf("%d", &set_mf) < 0)
- return;
-
- if (set_mf == 1) {
- int n = __set_match_filter(match_filter);
- if (n < 0 || n >= MAX_MATCH_FILTER_LEN) {
- printf("Not set match filter\n");
- return;
- }
- __print_match_filter(match_filter, n);
- ret = wifi_aware_publish_set_match_filter(publish, match_filter, n);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return;
- }
-}
-
-void test_publish()
-{
- int ret = 0;
-
- RET_IF_LOOP_IS_NULL();
-
- ret = wifi_aware_session_create(WIFI_AWARE_SESSION_PUBLISH, &g_wifi_aware_session);
- RET_IF_SESSION_IS_NULL();
-
- ret = wifi_aware_publish_create(&g_publish_handle);
- __print_result(ret, "wifi_aware_publish_create");
-
- __set_publish_config(g_publish_handle);
-
- ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
- __print_result(ret, "wifi_aware_publish");
-
- memcpy(g_msg, "TEST", 4);
- g_session_type = 0;
- //ret = wifi_aware_publish_destroy(publish);
- //__print_result(ret, "wifi_aware_publish_destroy");
-}
-
-void test_cancel_publish()
-{
- int ret = 0;
-
- RET_IF_LOOP_IS_NULL();
- RET_IF_SESSION_IS_NULL();
-
- if (g_publish_handle == NULL) {
- printf("publish should be done\n");
- return;
- }
-
- ret = wifi_aware_session_stop(g_wifi_aware_session);
- __print_result(ret, "wifi_aware_cancel_publish");
-}
-
-static void __updated_publish_cb(wifi_aware_session_h session,
- wifi_aware_error_e error, void *user_data)
-{
- if (error == WIFI_AWARE_ERROR_NONE)
- printf("Update Publish Request is success. Publish ID: [*]\n");
- else
- printf("Update Publish Request is fail.\n");
-}
-
-void test_update_publish()
-{
- int ret = 0;
- unsigned char specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
- size_t len = 0;
-
- RET_IF_LOOP_IS_NULL();
- RET_IF_SESSION_IS_NULL();
-
- if (g_publish_handle == NULL) {
- printf("publish should be done\n");
- return;
- }
-
- len = strlen(UPDATE_PUB_SSI);
- memcpy(specific_info, UPDATE_PUB_SSI, len);
- ret = wifi_aware_publish_set_service_specific_info(g_publish_handle,
- specific_info, len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return;
-
- ret = wifi_aware_session_update_publish(g_wifi_aware_session, g_publish_handle, __updated_publish_cb, NULL);
- __print_result(ret, "wifi_aware_update_publish");
-}
-
-static void __subscribed_cb(wifi_aware_session_h session,
- wifi_aware_error_e error, void *user_data)
-{
- if (error == WIFI_AWARE_ERROR_NONE)
- printf("Subscribe Request is success. Subscirbe ID: [*]\n");
- else
- 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));
- if (info == NULL) {
- printf("Out of memory\n");
- return;
- }
- info->peer_id = g_peer_id++;
- info->peer = peer;
- g_peer_list = g_slist_append(g_peer_list, info);
-}
-
-static void __print_mac(unsigned char mac[6])
-{
- printf(MACSTR, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
-}
-
-static void __print_peer_info(gpointer data, gpointer user_data)
-{
- peer_info *info = (peer_info *)data;
- unsigned char *mac = NULL;
- wifi_aware_peer_get_mac(info->peer, &mac);
- printf("%02d %p ", info->peer_id, info->peer);
- __print_mac(mac);
- free(mac);
- printf("\n");
-}
-
-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,
- int distance, void *user_data)
-{
- printf("Find A Wi-Fi Aware Peer %p\n", peer);
-// printf("Subscribe ID: %u\n", subscribe_id);
- printf("Service Specific Info: ");
- __print_byte_array(service_specific_info, service_specific_info_len);
- printf("\n");
- printf("Distance: %d\n", distance);
- __add_new_peer(peer);
-}
-
-static void __set_subscribe_config(wifi_aware_subscribe_h subscribe)
-{
- int ret = 0;
- int subscribe_type = 0;
- char service_name[MAX_SERVICE_NAME_LEN + 1] = {0, };
- char str_specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
- unsigned char specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
- unsigned char match_filter[MAX_MATCH_FILTER_LEN + 1] = {0, };
- size_t len = 0;
- int set_info = 0;
- int set_mf = 0;
-
- printf("Service Name: ");
- if (scanf("%" STR(MAX_SERVICE_NAME_LEN) "s", service_name) < 0)
- return;
-
- ret = wifi_aware_subscribe_set_service_name(subscribe,
- service_name);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return;
-
- printf("Choose subscribe type(0-Active, 1-Passive): ");
- if (scanf("%d", &subscribe_type) < 0)
- return;
-
- ret = wifi_aware_subscribe_set_type(subscribe,
- subscribe_type ? WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE
- : WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return;
-
- printf("Do you want to set Service Specific Information? (0-No, 1-Yes) ");
- if (scanf("%d", &set_info) < 0)
- return;
-
- if (set_info == 1) {
- if (scanf(" %" STR(MAX_SPECIFIC_INFO_LEN) "s", str_specific_info) < 0)
- return;
-
- len = strlen(str_specific_info);
- memcpy(specific_info, str_specific_info, len);
- ret = wifi_aware_subscribe_set_service_specific_info(subscribe,
- specific_info, len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return;
- }
-
- printf("Do you set Match Filter(0-No, 1-Yes): ");
- if (scanf("%d", &set_mf) < 0)
- return;
- if (set_mf == 1) {
- int n = __set_match_filter(match_filter);
- if (n < 0 || n >= MAX_MATCH_FILTER_LEN) {
- printf("Not set match filter\n");
- return;
- }
- ret = wifi_aware_subscribe_set_match_filter(subscribe, match_filter, n);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return;
- }
-}
-
-void test_subscribe()
-{
- int ret = 0;
-
- RET_IF_LOOP_IS_NULL();
-
- ret = wifi_aware_session_create(WIFI_AWARE_SESSION_SUBSCRIBE, &g_wifi_aware_session);
- RET_IF_SESSION_IS_NULL();
-
- ret = wifi_aware_subscribe_create(&g_subscribe_handle);
- __print_result(ret, "wifi_aware_subscribe_create");
-
- ret = wifi_aware_session_set_service_discovered_cb(g_wifi_aware_session, __service_discovered_cb, NULL);
- __print_result(ret, "wifi_aware_session_set_service_discovered_cb");
-
- __set_subscribe_config(g_subscribe_handle);
-
- ret = wifi_aware_session_subscribe(g_wifi_aware_session, g_subscribe_handle, __subscribed_cb, NULL);
- __print_result(ret, "wifi_aware_subscribe");
-
- memcpy(g_msg, "ABCD", 4);
- g_session_type = 1;
-}
-
-static void __updated_subscribe_cb(wifi_aware_session_h session,
- wifi_aware_error_e error, void *user_data)
-{
- if (error == WIFI_AWARE_ERROR_NONE)
- printf("Update Subscribe Request is success. Subscribe ID: [*]\n");
- else
- printf("Update Subscribe Request is fail.\n");
-}
-
-void test_update_subscribe()
-{
- int ret = 0;
- unsigned char specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
- size_t len = 0;
-
- RET_IF_LOOP_IS_NULL();
- RET_IF_SESSION_IS_NULL();
-
- if (g_subscribe_handle == NULL) {
- printf("subscribe should be done\n");
- return;
- }
-
- len = strlen(UPDATE_SUB_SSI);
- memcpy(specific_info, UPDATE_SUB_SSI, len);
- ret = wifi_aware_subscribe_set_service_specific_info(g_subscribe_handle,
- specific_info, len);
- if (ret != WIFI_AWARE_ERROR_NONE)
- return;
-
- ret = wifi_aware_session_update_subscribe(g_wifi_aware_session, g_subscribe_handle, __updated_subscribe_cb, NULL);
- __print_result(ret, "wifi_aware_update_subscribe");
-}
-
-void test_cancel_subscribe()
-{
- int ret = 0;
-
- RET_IF_LOOP_IS_NULL();
- RET_IF_SESSION_IS_NULL();
-
- if (g_subscribe_handle == NULL) {
- printf("subscribe should be done\n");
- return;
- }
-
- ret = wifi_aware_session_stop(g_wifi_aware_session);
- __print_result(ret, "wifi_aware_cancel_subscribe");
-}
-
-static struct {
- wifi_aware_session_h session;
- wifi_aware_publish_h handle;
- char service_name[MAX_SERVICE_NAME_LEN + 1];
- unsigned char info[MAX_SPECIFIC_INFO_LEN];
- size_t len;
-} info_test_data[] = {
- { NULL, NULL, "SPECIFIC_INFO_TEST_1", {1, 2, 3, 4, 5}, 5 },
- { NULL, NULL, "SPECIFIC_INFO_TEST_2", {'A', 'B', 'C', '\0'}, 4 },
- { NULL, NULL, "SPECIFIC_INFO_TEST_3", {0, 0, 0, 0, 0, 0, 0}, 7 },
- { NULL, NULL, "SPECIFIC_INFO_TEST_4", {1, 2, 3, 4, 5}, 3 },
- { NULL, NULL, "SPECIFIC_INFO_TEST_5", {1, 2, 3, 4, 5}, 7 },
-};
-
-void test_specific_info()
-{
- int ret = 0;
-
- RET_IF_LOOP_IS_NULL();
-
- for (int i = 0; i < sizeof(info_test_data) / sizeof(info_test_data[0]); ++i) {
- ret = wifi_aware_session_create(WIFI_AWARE_SESSION_PUBLISH, &info_test_data[i].session);
- __print_result(ret, "wifi_aware_session_create");
-
- ret = wifi_aware_publish_create(&info_test_data[i].handle);
- __print_result(ret, "wifi_aware_publish_create");
-
- ret = wifi_aware_publish_set_type(info_test_data[i].handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
- __print_result(ret, "wifi_aware_publish_set_type");
- if (ret != WIFI_AWARE_ERROR_NONE)
- return;
-
- ret = wifi_aware_publish_set_service_name(info_test_data[i].handle,
- info_test_data[i].service_name);
- __print_result(ret, "wifi_aware_publish_set_service_name");
- if (ret != WIFI_AWARE_ERROR_NONE)
- return;
-
- ret = wifi_aware_publish_set_service_specific_info(info_test_data[i].handle,
- info_test_data[i].info, info_test_data[i].len);
- __print_result(ret, "wifi_aware_publish_set_service_specific_info");
- if (ret != WIFI_AWARE_ERROR_NONE)
- return;
-
- ret = wifi_aware_session_publish(info_test_data[i].session, info_test_data[i].handle, __published_cb, NULL);
- __print_result(ret, "wifi_aware_session_publish");
- }
-}
-
-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_session_set_message_received_cb(g_wifi_aware_session, __message_received_cb, NULL);
- __print_result(ret, "wifi_aware_session_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_session_send_message(g_wifi_aware_session, peer, g_msg, len, __message_result_cb, peer);
- __print_result(ret, "wifi_aware_send_message");
- printf("Send a message to %p", peer);
-}
-
-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("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");
-
- if (g_session_type == 1) {
- 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. error: %s\n", data_path, __print_error(error));
- }
-
-}
-
-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.\n", data_path);
- wifi_aware_data_path_unset_terminated_cb(data_path);
-}
-
-void test_open_data_path()
-{
- int ret = WIFI_AWARE_ERROR_NONE;
- int peer_id;
- int security_type;
-
- RET_IF_LOOP_IS_NULL();
-
- __display_peer_list();
- printf("Select a peer to connect: ");
- if (scanf("%d", &peer_id) < 0)
- return;
-
- wifi_aware_peer_h peer = __get_peer_by_id(peer_id);
- ret = wifi_aware_data_path_create(g_wifi_aware_session, peer, &g_ndp);
- __print_result(ret, "wifi_aware_data_path_create");
- printf("NDP %p", g_ndp);
-
- printf("Choose security type(0-Open, 1-PSK, 2-PMK): ");
- ret = scanf("%d", &security_type);
- if (ret < 0)
- return;
-
- ret = wifi_aware_data_path_set_terminated_cb(g_ndp, _data_path_terminated_cb, NULL);
- __print_result(ret, "wifi_aware_data_path_set_terminated_cb");
-
- ret = wifi_aware_data_path_set_security(g_ndp, security_type);
- __print_result(ret, "wifi_aware_data_path_set_security");
-
- if (security_type == 1) {
- char key[MAX_PSK_LEN + 1] = {0, };
- printf("Passphrase: ");
- ret = scanf("%63s", key);
- if (ret < 0)
- return;
- ret = wifi_aware_data_path_set_psk(g_ndp, key);
- __print_result(ret, "wifi_aware_data_path_set_psk");
- }
-
- if (security_type == 2) {
- char key[PMK_LEN + 1] = {0, };
- unsigned char pmk[PMK_LEN] = {0, };
- printf("PMK: ");
- ret = scanf("%32s", key);
- if (ret < 0)
- return;
- memcpy(pmk, key, PMK_LEN);
- ret = wifi_aware_data_path_set_pmk(g_ndp, pmk);
- __print_result(ret, "wifi_aware_data_path_set_pmk");
- }
-
- if (g_session_type == 0) {
- int port = -1;
- printf("Port: ");
- ret = scanf("%d", &port);
- if (ret < 0)
- return;
- ret = wifi_aware_data_path_set_port(g_ndp, port);
- __print_result(ret, "wifi_aware_data_set_port");
- }
-
- ret = wifi_aware_data_path_open(g_ndp, __open_cb, NULL);
- __print_result(ret, "wifi_aware_data_path_open");
-}
-
-void test_close_data_path()
-{
- int ret = WIFI_AWARE_ERROR_NONE;
-
- RET_IF_LOOP_IS_NULL();
- RET_IF_NDP_IS_NULL();
-
- ret = wifi_aware_data_path_close(g_ndp);
- __print_result(ret, "wifi_aware_data_path_close");
-
- ret = wifi_aware_data_path_destroy(g_ndp);
- __print_result(ret, "wifi_aware_data_path_destroy");
-}
-
-typedef void (*test_func)(void);
-test_func g_menu_func[] = {
- [CMD_QUIT] = test_quit,
- [CMD_FULL_MENU] = test_full_menu,
- [CMD_INITIALIZE] = test_init,
- [CMD_DEINITIALIZE] = test_deinit,
- [CMD_ENABLE] = test_enable,
- [CMD_DISABLE] = test_disable,
- [CMD_PUBLISH] = test_publish,
- [CMD_CANCEL_PUBLISH] = test_cancel_publish,
- [CMD_UPDATE_PUBLISH] = test_update_publish,
- [CMD_SUBSCRIBE] = test_subscribe,
- [CMD_UPDATE_SUBSCRIBE] = test_update_subscribe,
- [CMD_CANCEL_SUBSCRIBE] = test_cancel_subscribe,
- [CMD_MESSAGE_RECV_CB] = test_message_recv_cb,
- [CMD_SEND_MESSAGE] = test_send_message,
- [CMD_OPEN_DATA_PATH] = test_open_data_path,
-
- [CMD_TEST_SPECIFIC_INFO] = test_specific_info,
-
- [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;
-
- 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 if (n - 1 > 0 && n < 1024) {
- buf[n - 1] = '\0'; /* remove new line... */
- printf("\n\n");
- __process_input(buf, user_data);
- } else {
- printf("invalid input\n");
- }
-
- 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;
-}
+++ /dev/null
-/*
- * Wi-Fi Aware Test Program
- *
- * Copyright (c) 2020 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 declares functions for testing Wi-Fi aware CAPIs.
- *
- * @file wifi-aware-test.h
- * @author Jiung Yu (jiung.yu@samsung.com)
- */
-#ifndef __WIFI_AWARE_TEST_H__
-#define __WIFI_AWARE_TEST_H__
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __WIFI_AWARE_TEST_H__ */
--- /dev/null
+# Copyright (c) 2020 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.
+#
+# @file CMakeLists.txt
+#
+
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+
+SET(WIFI_AWARE_TEST_INCLUDE_DIR ${WIFI_AWARE_TEST_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/include)
+INCLUDE_DIRECTORIES(${WIFI_AWARE_TEST_INCLUDE_DIR})
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(TARGET_WIFI_AWARE_TEST REQUIRED capi-base-common glib-2.0)
+FOREACH(flag ${TARGET_WIFI_AWAREs_TEST_CFLAGS})
+ SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ MESSAGE(${flag})
+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}")
+ ADD_EXECUTABLE(${src_name} ${src})
+ TARGET_LINK_LIBRARIES(${src_name} ${TARGET_WIFI_AWARE} ${TARGET_WIFI_AWARE_TEST_LDFLAGS})
+ENDFOREACH()
+
+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})
+INSTALL(TARGETS wifi-aware-publish-matching-filter-test RUNTIME DESTINATION ${BIN_DIR})
+INSTALL(TARGETS wifi-aware-subscribe-matching-filter-test RUNTIME DESTINATION ${BIN_DIR})
--- /dev/null
+/*
+ * Wi-Fi Aware Test Program
+ *
+ * Copyright (c) 2020 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 __WIFI_AWARE_MATCHING_FILTER_TEST_H__
+#define __WIFI_AWARE_MATCHING_FILTER_TEST_H__
+
+#include <stdio.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum {
+ DUMMY_TEST_NUM = 0,
+
+ // Sol / Active
+ POSITIVE_TEST_01,
+ POSITIVE_TEST_02,
+ POSITIVE_TEST_03,
+ POSITIVE_TEST_04,
+
+ POSITIVE_TEST_05,
+ POSITIVE_TEST_06,
+ POSITIVE_TEST_07,
+
+ POSITIVE_TEST_08,
+ POSITIVE_TEST_09,
+ POSITIVE_TEST_10,
+
+ // Unsol / Passive
+ POSITIVE_TEST_11,
+ POSITIVE_TEST_12,
+ POSITIVE_TEST_13,
+ POSITIVE_TEST_14,
+
+ POSITIVE_TEST_15,
+ POSITIVE_TEST_16,
+ POSITIVE_TEST_17,
+
+ POSITIVE_TEST_18,
+ POSITIVE_TEST_19,
+ POSITIVE_TEST_20,
+
+ ///// custom ///
+ POSITIVE_TEST_21,
+ POSITIVE_TEST_22,
+ POSITIVE_TEST_23,
+ POSITIVE_TEST_24,
+
+ POSITIVE_TEST_25,
+ POSITIVE_TEST_26,
+ POSITIVE_TEST_27,
+
+ POSITIVE_TEST_28,
+ POSITIVE_TEST_29,
+ POSITIVE_TEST_30,
+
+ // Sol / Active
+ NEGATIVE_TEST_01,
+ NEGATIVE_TEST_02,
+ NEGATIVE_TEST_03,
+
+ // Unsol / Passive
+ NEGATIVE_TEST_04,
+ NEGATIVE_TEST_05,
+ NEGATIVE_TEST_06,
+
+ //// custom
+ NEGATIVE_TEST_07,
+ NEGATIVE_TEST_08,
+ NEGATIVE_TEST_09,
+
+ INVALID,
+} test_case_e;
+
+#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 PRINT_START(x) printf(MAKE_GREEN "Running test: %dth..." RESET_COLOR "\n", x)
+
+#define MAX_INIT_NUM 1024
+
+#define SERVICE_NAME "matching filter test"
+#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_MATCH_FILTER_LEN 255
+
+typedef struct {
+ size_t len;
+ unsigned char values[MAX_MATCH_FILTER_LEN];
+} matching_filter_format_t;
+
+#define MATCHING_FILTER_FORMAT_NUM 8
+
+matching_filter_format_t test_matching_filter[MATCHING_FILTER_FORMAT_NUM]
+ = { {5, {0, 0, 0, 0, 0}}, // <0><0><0><0><0> -> wildcard
+ {10, {1, 1, 1, 2, 1, 3, 1, 4, 1, 5}}, // <1,1,><1,2><1,3><1,4><1,5> -> value filter 1
+ {10, {1, 1, 1, 2, 1, 1, 1, 4, 1, 5}}, // <1,1,><1,2><1,1><1,4><1,5> -> value filter 2
+ {6, {0, 1, 2, 0, 1, 4}}, // <0><1,2><0><1,4> -> value filter 3
+ {8, {0, 1, 2, 1, 3, 0, 1, 5}}, // <0><1,2><1,3><0><1,5> -> value filter 4
+ {6, {1, 1, 0, 1, 3, 0}}, // <1,1><0><1,3><0> -> value filter 5
+ {8, {1, 1, 0, 1, 3, 0, 1, 5}}, // <1,1><0><1,3><0><1,5> -> value filter 6
+ {0, {0, }}, // No filter
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __WIFI_AWARE_MATCHING_FILTER_TEST_H__ */
--- /dev/null
+/*
+ * Wi-Fi Aware
+ *
+ * Copyright (c) 2020 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.
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <glib.h>
+
+#include "wifi-aware.h"
+#include "wifi-aware-matching-filter-test.h"
+
+#define RET_IF_LOOP_IS_NULL()\
+ do {\
+ if (!g_main_loop_p) {\
+ printf("Loop was not initialized\n");\
+ return;\
+ } \
+ } while (0)
+
+static GMainLoop *g_main_loop_p;
+
+static test_case_e g_current_test = DUMMY_TEST_NUM;
+static bool g_initialized = false;
+static bool g_enabled = false;
+
+static bool g_test_result = false;
+
+static wifi_aware_session_h g_wifi_aware_session = NULL;
+static wifi_aware_publish_h g_publish_handle = NULL;
+
+void test_init();
+void test_deinit();
+
+static void __print_test_result(int num, bool result)
+{
+ char test_name[30] = {0, };
+
+ if (num < NEGATIVE_TEST_01)
+ snprintf(test_name, 29, "%s %d", "Positive Test", num);
+ else
+ snprintf(test_name, 29, "%s %d", "Negative Test", num - NEGATIVE_TEST_01 + 1);
+
+ if (result)
+ printf(MAKE_GREEN"[%s] is pass."RESET_COLOR"\n", test_name);
+ else
+ printf(MAKE_RED"[%s] is fail."RESET_COLOR"\n", test_name);
+
+ printf("Please enter to run the next test..\n");
+}
+
+void test_quit()
+{
+ RET_IF_LOOP_IS_NULL();
+
+ 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)
+{
+ printf("published..!\n");
+ if (error == WIFI_AWARE_ERROR_NONE)
+ g_test_result = true;
+ else
+ g_test_result = false;
+
+ __print_test_result(g_current_test, g_test_result);
+}
+
+static bool __set_default_configures(void)
+{
+ unsigned char specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
+
+ int ret = wifi_aware_session_create(WIFI_AWARE_SESSION_PUBLISH, &g_wifi_aware_session);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return false;
+
+ ret = wifi_aware_publish_create(&g_publish_handle);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return false;
+
+ ret = wifi_aware_publish_set_service_name(g_publish_handle, SERVICE_NAME);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return false;
+
+ size_t len = strlen(PUB_SSI);
+ memcpy(specific_info, PUB_SSI, len);
+ ret = wifi_aware_publish_set_service_specific_info(g_publish_handle, specific_info, len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return false;
+
+ ret = wifi_aware_publish_enable_ranging(g_publish_handle, false);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return false;
+
+ return true;
+}
+
+static void __enabled_cb(wifi_aware_error_e error, void *user_data)
+{
+ if (error == WIFI_AWARE_ERROR_NONE)
+ g_enabled = true;
+ else
+ g_enabled = false;
+
+ g_main_loop_quit(g_main_loop_p);
+}
+
+void clear_resources()
+{
+ if (g_publish_handle)
+ wifi_aware_publish_destroy(g_publish_handle);
+ g_publish_handle = NULL;
+
+ if (g_wifi_aware_session) {
+ wifi_aware_session_stop(g_wifi_aware_session);
+ wifi_aware_session_unset_message_received_cb(g_wifi_aware_session);
+ wifi_aware_session_unset_terminated_cb(g_wifi_aware_session);
+ wifi_aware_session_destroy(g_wifi_aware_session);
+ }
+ g_wifi_aware_session = NULL;
+
+ g_test_result = false;
+}
+
+void test_init()
+{
+ int ret = 0;
+
+ RET_IF_LOOP_IS_NULL();
+
+ if (g_initialized == false) {
+ ret = wifi_aware_initialize();
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ g_main_loop_quit(g_main_loop_p);
+ return;
+ }
+ g_initialized = true;
+ }
+
+ if (g_enabled == false) {
+ ret = wifi_aware_enable(__enabled_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ g_main_loop_quit(g_main_loop_p);
+ return;
+ }
+ }
+}
+
+void test_deinit()
+{
+ wifi_aware_disable();
+ wifi_aware_deinitialize();
+}
+
+void test_sol_no_filter()
+{
+ PRINT_START(g_current_test);
+
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_SOLICITED);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // no filter
+
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_sol_wildcard_filter()
+{
+ PRINT_START(g_current_test);
+
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_SOLICITED);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <0><0><0><0><0>
+ ret = wifi_aware_publish_set_match_filter(g_publish_handle,
+ test_matching_filter[0].values, test_matching_filter[0].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_sol_value_filter_1()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_SOLICITED);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <1,1><1,2><1,3><1,4><1,5>
+ ret = wifi_aware_publish_set_match_filter(g_publish_handle,
+ test_matching_filter[1].values, test_matching_filter[1].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_sol_value_filter_2()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_SOLICITED);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <1,1><1,2><1,1><1,4><1,5>
+ ret = wifi_aware_publish_set_match_filter(g_publish_handle,
+ test_matching_filter[2].values, test_matching_filter[2].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_sol_value_filter_4()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_SOLICITED);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <0><1,2><1,3><0><1,5>
+ ret = wifi_aware_publish_set_match_filter(g_publish_handle,
+ test_matching_filter[4].values, test_matching_filter[4].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_sol_value_filter_5()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_SOLICITED);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <1,1><0><1,3><0>
+ ret = wifi_aware_publish_set_match_filter(g_publish_handle,
+ test_matching_filter[5].values, test_matching_filter[5].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_unsol_no_filter()
+{
+ PRINT_START(g_current_test);
+
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // no filter
+
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_unsol_wildcard_filter()
+{
+ PRINT_START(g_current_test);
+
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <0><0><0><0><0>
+ ret = wifi_aware_publish_set_match_filter(g_publish_handle,
+ test_matching_filter[0].values, test_matching_filter[0].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_unsol_value_filter_1(void)
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_publish_set_match_filter(g_publish_handle,
+ test_matching_filter[1].values, test_matching_filter[1].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_unsol_value_filter_2()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <1,1><1,2><1,1><1,4><1,5>
+ ret = wifi_aware_publish_set_match_filter(g_publish_handle,
+ test_matching_filter[2].values, test_matching_filter[2].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_unsol_value_filter_3()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <0><1,2><0><1,4>
+ ret = wifi_aware_publish_set_match_filter(g_publish_handle,
+ test_matching_filter[3].values, test_matching_filter[3].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_unsol_value_filter_4()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <0><1,2><1,3><0><1,5>
+ ret = wifi_aware_publish_set_match_filter(g_publish_handle,
+ test_matching_filter[4].values, test_matching_filter[4].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_unsol_value_filter_5()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <1,1><0><1,3><0>
+ ret = wifi_aware_publish_set_match_filter(g_publish_handle,
+ test_matching_filter[5].values, test_matching_filter[5].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_unsol_value_filter_6()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_publish_set_type(g_publish_handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <1,1><0><1,3><0><1,5>
+ ret = wifi_aware_publish_set_match_filter(g_publish_handle,
+ test_matching_filter[6].values, test_matching_filter[6].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+typedef void (*test_func)(void);
+test_func g_menu_func[] = {
+ [0] = NULL,
+
+ [POSITIVE_TEST_01] = test_sol_no_filter,
+ [POSITIVE_TEST_02] = test_sol_wildcard_filter,
+ [POSITIVE_TEST_03] = test_sol_no_filter,
+ [POSITIVE_TEST_04] = test_sol_value_filter_1,
+
+ [POSITIVE_TEST_05] = test_sol_value_filter_1,
+ [POSITIVE_TEST_06] = test_sol_wildcard_filter,
+ [POSITIVE_TEST_07] = test_sol_value_filter_1,
+
+ [POSITIVE_TEST_08] = test_sol_value_filter_1,
+ [POSITIVE_TEST_09] = test_sol_value_filter_4,
+ [POSITIVE_TEST_10] = test_sol_value_filter_1,
+
+ [POSITIVE_TEST_11] = test_unsol_no_filter,
+ [POSITIVE_TEST_12] = test_unsol_no_filter,
+ [POSITIVE_TEST_13] = test_unsol_wildcard_filter,
+ [POSITIVE_TEST_14] = test_unsol_value_filter_1,
+
+ [POSITIVE_TEST_15] = test_unsol_wildcard_filter,
+ [POSITIVE_TEST_16] = test_unsol_value_filter_1,
+ [POSITIVE_TEST_17] = test_unsol_value_filter_1,
+
+ [POSITIVE_TEST_18] = test_unsol_value_filter_6,
+ [POSITIVE_TEST_19] = test_unsol_value_filter_1,
+ [POSITIVE_TEST_20] = test_unsol_value_filter_1,
+
+ // custom: unsol + active
+ [POSITIVE_TEST_21] = test_unsol_no_filter,
+ [POSITIVE_TEST_22] = test_unsol_wildcard_filter,
+ [POSITIVE_TEST_23] = test_unsol_no_filter,
+ [POSITIVE_TEST_24] = test_unsol_value_filter_1,
+
+ [POSITIVE_TEST_25] = test_unsol_value_filter_1,
+ [POSITIVE_TEST_26] = test_unsol_wildcard_filter,
+ [POSITIVE_TEST_27] = test_unsol_value_filter_1,
+
+ [POSITIVE_TEST_28] = test_unsol_value_filter_1,
+ [POSITIVE_TEST_29] = test_unsol_value_filter_4,
+ [POSITIVE_TEST_30] = test_unsol_value_filter_1,
+
+ [NEGATIVE_TEST_01] = test_sol_no_filter,
+ [NEGATIVE_TEST_02] = test_sol_value_filter_2,
+ [NEGATIVE_TEST_03] = test_sol_value_filter_5,
+
+ [NEGATIVE_TEST_04] = test_unsol_no_filter,
+ [NEGATIVE_TEST_05] = test_unsol_value_filter_1,
+ [NEGATIVE_TEST_06] = test_unsol_value_filter_3,
+
+ // custom test: unsol + active
+ [NEGATIVE_TEST_07] = test_unsol_no_filter,
+ [NEGATIVE_TEST_08] = test_unsol_value_filter_2,
+ [NEGATIVE_TEST_09] = test_unsol_value_filter_5,
+
+
+ [INVALID] = NULL,
+};
+
+static gboolean __test_terminal_read_std_input(GIOChannel * source,
+ GIOCondition condition, gpointer user_data)
+{
+ int fd = 0;
+ static char buf[1024];
+ int 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 {
+ ++g_current_test;
+ if (g_current_test == INVALID) {
+ printf("Test is finished..\n");
+ g_main_loop_quit(g_main_loop_p);
+ return TRUE;
+ }
+ clear_resources();
+ g_menu_func[g_current_test]();
+ }
+ 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);
+
+ printf(MAKE_GREEN"\nThis is Matching filter test on Publisher side"RESET_COLOR"\n");
+
+ test_init();
+ g_main_loop_run(g_main_loop_p);
+ if (!g_enabled) {
+ printf(MAKE_RED"Can not start test.."RESET_COLOR"\n");
+ g_main_loop_unref(g_main_loop_p);
+ return 0;
+ }
+
+ 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);
+
+ printf("Please enter to start test..\n");
+
+ g_main_loop_run(g_main_loop_p);
+
+ g_main_loop_unref(g_main_loop_p);
+ test_deinit();
+
+ return 0;
+}
--- /dev/null
+/*
+ * Wi-Fi Aware
+ *
+ * Copyright (c) 2020 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 PRINT_SUCCESS(format, args...) printf(MAKE_GREEN format RESET_COLOR "\n", ##args)
+#define PRINT_FAILURE(format, args...) printf(MAKE_RED format RESET_COLOR "\n", ##args)
+#define PRINT_FAILURE_ERROR(format, error) printf(MAKE_RED format " Failure(%d:%s)" RESET_COLOR "\n", error, __print_error(error))
+
+#define SERVICE_NAME "Wi-Fi Aware Test"
+#define OTHER_SERVICE_NAME "Other Service Name"
+#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 PMK_LEN 32
+#define PMK "12345678901234567890123456789012"
+#define IPV6_ADDRESS_LEN 39
+
+enum {
+ CMD_QUIT,
+
+ CMD_OPEN_UNSOLICITED,
+ CMD_OPEN_SOLICITED,
+ CMD_PSK_UNSOLICITED,
+ CMD_PSK_SOLICITED,
+ CMD_PMK_UNSOLICITED,
+ CMD_PMK_SOLICITED,
+
+ CMD_OPEN_UNSOLICITED_OTHER_SERVICE,
+ CMD_OPEN_SOLICITED_OTHER_SERVICE,
+
+ CMD_OPEN_UNSOLICITED_RANGING,
+ CMD_OPEN_SOLICITED_RANGING,
+
+ CMD_INVALID,
+};
+
+static char *g_menu_str[] = {
+ [CMD_QUIT]
+ = "Quit",
+ [CMD_OPEN_UNSOLICITED]
+ = "Open + Unsolicited",
+ [CMD_OPEN_SOLICITED]
+ = "Open + Solicited",
+ [CMD_PSK_UNSOLICITED]
+ = "PSK + Unsolicited",
+ [CMD_PSK_SOLICITED]
+ = "PSK + Solicited",
+ [CMD_PMK_UNSOLICITED]
+ = "PMK + Unsolicited",
+ [CMD_PMK_SOLICITED]
+ = "PMK + Solicited",
+ [CMD_OPEN_UNSOLICITED_OTHER_SERVICE]
+ = "Open + Unsolicited + Other Service",
+ [CMD_OPEN_SOLICITED_OTHER_SERVICE]
+ = "Open + Solicited + Other Service",
+ [CMD_OPEN_UNSOLICITED_RANGING]
+ = "Open + Unsolicited + Ranging",
+ [CMD_OPEN_SOLICITED_RANGING]
+ = "Open + Solicited + Ranging",
+ [CMD_INVALID]
+ = NULL,
+};
+
+static GMainLoop *g_main_loop_p;
+static bool g_ranging = false;
+
+static bool g_initialized = false;
+static bool g_enabled = 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 char g_service_name[MAX_SERVICE_NAME_LEN + 1] = {0, };
+static unsigned char g_pmk[PMK_LEN];
+
+void test_init();
+void test_deinit();
+
+static void __test_finish(int is_validated)
+{
+ if (is_validated)
+ printf(MAKE_GREEN"[TEST RESULT] Wi-Fi Aware validated!"RESET_COLOR"\n");
+ else
+ printf(MAKE_RED"[TEST RESULT] Wi-Fi Aware invalidated!"RESET_COLOR"\n");
+ test_deinit();
+}
+
+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_NONE:
+ return "NO ERROR";
+ 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";
+ case WIFI_AWARE_ERROR_ALREADY_ENABLED:
+ return "ALREADY_ENABLED";
+ case WIFI_AWARE_ERROR_REJECTED_BY_PEER:
+ return "REJECTED_BY_PEER";
+ case WIFI_AWARE_ERROR_INTERFACE_DOWN:
+ return "INTERFACE_IS_DOWN";
+ default:
+ return "UNKNOWN";
+ }
+}
+
+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();
+
+ 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) {
+ PRINT_SUCCESS("[CB][Publish] Requested");
+ } else {
+ PRINT_FAILURE_ERROR("[CB][Publish]", error);
+ __test_finish(0);
+ }
+}
+
+static void __data_path_terminated_cb(wifi_aware_data_path_h data_path,
+ wifi_aware_termination_reason_e reason, void *user_data)
+{
+ printf("[EVENT][DataPath Terminated] %p", 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;
+ PRINT_SUCCESS("[CB][OpenDataPath] Connected %p", data_path);
+ ret = wifi_aware_data_path_get_interface(data_path, &interface);
+ if (ret == WIFI_AWARE_ERROR_NONE)
+ printf("\tInterface: %s\n", interface);
+ else
+ PRINT_FAILURE_ERROR("\tInterface:", ret);
+
+ 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("\tPeer's IPv6 Address: %s\n", ipv6_address);
+ else
+ PRINT_FAILURE_ERROR("\tPeer's IPv6 Address:", ret);
+ ret = wifi_aware_data_path_get_peer_port(data_path, &port);
+ if (ret == WIFI_AWARE_ERROR_NONE)
+ printf("\tPeer's Port: %d\n", port);
+ else
+ PRINT_FAILURE_ERROR("\tPeer's Port:", ret);
+ __test_finish(1);
+ }
+ else {
+ PRINT_FAILURE("[CB][OpenDataPath] Failure(%d:%s) %p", error, __print_error(error), data_path);
+ __test_finish(0);
+ }
+}
+
+static void __open_data_path(wifi_aware_session_h session)
+{
+ printf("[OpenDataPath] Request\n");
+ if (g_peer == NULL) {
+ printf("Peer is NULL\n");
+ __test_finish(0);
+ return;
+ }
+
+ int ret = wifi_aware_data_path_create(session, g_peer, &g_ndp);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("\tHandle:", ret);
+ __test_finish(0);
+ return;
+ }
+ printf("\tHandle: %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) {
+ PRINT_FAILURE_ERROR("\tSet TerminatedCallback:", ret);
+ __test_finish(0);
+ return;
+ }
+
+ ret = wifi_aware_data_path_set_security(g_ndp, g_security_type);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("\tSecurity Type:", ret);
+ __test_finish(0);
+ return;
+ }
+ printf("\tSecurity Type: ");
+ switch (g_security_type) {
+ case WIFI_AWARE_SECURITY_TYPE_OPEN:
+ printf("Open\n");
+ break;
+ case WIFI_AWARE_SECURITY_TYPE_PSK:
+ printf("PSK\n");
+ wifi_aware_data_path_set_psk(g_ndp, PSK);
+ break;
+ case WIFI_AWARE_SECURITY_TYPE_PMK:
+ wifi_aware_data_path_set_pmk(g_ndp, g_pmk);
+ printf("PMK\n");
+ break;
+ default:
+ PRINT_FAILURE("Unknown security type");
+ __test_finish(0);
+ return;
+ }
+
+ ret = wifi_aware_data_path_set_port(g_ndp, PORT);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("\tPort:", ret);
+ __test_finish(0);
+ } else {
+ printf("\tPort: %d\n", PORT);
+ }
+
+ ret = wifi_aware_data_path_open(g_ndp, __open_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("[OpenDataPath]", ret);
+ __test_finish(0);
+ }
+}
+
+static void __message_result_cb(wifi_aware_session_h session, wifi_aware_error_e error, void *user_data)
+{
+ if (error == WIFI_AWARE_ERROR_NONE) {
+ PRINT_SUCCESS("[CB][Send Message] Requested");
+ __open_data_path(session);
+ } else {
+ PRINT_FAILURE_ERROR("[CB][Send Message]", error);
+ __test_finish(0);
+ }
+}
+
+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;
+
+ PRINT_SUCCESS("[Event][Receive Message]");
+
+ ret = wifi_aware_peer_get_mac(peer, &mac);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ printf("\tPeer's NMI: Failure(%d:%s)\n", ret, __print_error(ret));
+ if (mac)
+ g_free(mac);
+ __test_finish(0);
+ return;
+ } else {
+ printf("\tPeer's NMI: ");
+ if (mac != NULL) {
+ __print_mac(mac);
+ g_free(mac);
+ }
+ printf("\n");
+ }
+
+ memcpy(buf, message, 1024);
+ buf[len] = '\0';
+ printf("\tMessage: %s\n", buf);
+
+ printf("[Send Message] Request\n");
+ g_peer = peer;
+ 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_session_send_message(session, peer, send_message, message_len, __message_result_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("[Send Message]", ret);
+ __test_finish(0);
+ }
+}
+
+static void __session_terminated_cb(wifi_aware_session_h session,
+ wifi_aware_termination_reason_e reason, void *user_data)
+{
+ PRINT_FAILURE("[Event][Session Terminated] Session: %p Reason: %d", session, reason);
+
+ if (session != NULL) {
+ wifi_aware_session_unset_message_received_cb(session);
+ wifi_aware_session_destroy(session);
+ }
+ __test_finish(0);
+}
+
+static bool __set_publish_config(wifi_aware_publish_h publish)
+{
+ int ret = 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("\tPublish Type: %s\n", g_pub_type == WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED ?
+ "Unsolicited" : "Solicited");
+ }
+ else {
+ PRINT_FAILURE_ERROR("\tPublish Type:", ret);
+ return false;
+ }
+
+ ret = wifi_aware_publish_set_service_name(publish, g_service_name);
+ if (ret == WIFI_AWARE_ERROR_NONE) {
+ printf("\tService Name: %s\n", g_service_name);
+ }
+ else {
+ PRINT_FAILURE_ERROR("\tService Name:", ret);
+ 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("\tService Specific Info: ");
+ __print_byte_array(specific_info, len);
+ }
+ else {
+ PRINT_FAILURE_ERROR("\tService Specific Info:", ret);
+ return false;
+ }
+
+ ret = wifi_aware_publish_enable_ranging(publish, g_ranging);
+ if (ret == WIFI_AWARE_ERROR_NONE) {
+ printf("\tRanging: %s\n", g_ranging ? "Enable" : "Disable");
+ }
+ else {
+ PRINT_FAILURE_ERROR("\tRanging:", ret);
+ return false;
+ }
+ return true;
+}
+
+static void start_publish()
+{
+ printf("[Session][Publish] Create\n");
+ int ret = wifi_aware_session_create(WIFI_AWARE_SESSION_PUBLISH, &g_wifi_aware_session);
+ if (ret == WIFI_AWARE_ERROR_NONE) {
+ printf("\tSession: %p\n", g_wifi_aware_session);
+ } else {
+ PRINT_FAILURE_ERROR("\tSession:", ret);
+ __test_finish(0);
+ }
+
+ ret = wifi_aware_publish_create(&g_publish_handle);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ PRINT_FAILURE_ERROR("\tPublish Config:", ret);
+
+ if (__set_publish_config(g_publish_handle) == false) {
+ PRINT_FAILURE("\tPublish Config: Failure");
+ __test_finish(0);
+ return;
+ }
+
+ ret = wifi_aware_session_set_message_received_cb(g_wifi_aware_session, __message_received_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("\tMessage Received CB:", ret);
+ __test_finish(0);
+ }
+
+ ret = wifi_aware_session_set_terminated_cb(g_wifi_aware_session, __session_terminated_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("\tSession Terminated CB:", ret);
+ __test_finish(0);
+ }
+
+ printf("[Publish] Request\n");
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("[Publish]", ret);
+ __test_finish(0);
+ }
+}
+
+static void __enabled_cb(wifi_aware_error_e error, void *user_data)
+{
+ if (error == WIFI_AWARE_ERROR_NONE) {
+ PRINT_SUCCESS("[CB][Enable] Enabled");
+ }
+ else {
+ PRINT_FAILURE_ERROR("[CB][Enable]", error);
+ return;
+ }
+
+ start_publish();
+}
+
+void run_common()
+{
+ int ret = 0;
+ RET_IF_LOOP_IS_NULL();
+
+ test_init();
+ if (g_enabled == false) {
+ ret = wifi_aware_enable(__enabled_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("[Enable]", ret);
+ __test_finish(0);
+ return;
+ }
+ g_enabled = true;
+ } else {
+ start_publish();
+ }
+}
+
+void clear_resources()
+{
+ if (g_ndp) {
+ wifi_aware_data_path_unset_terminated_cb(g_ndp);
+ wifi_aware_data_path_destroy(g_ndp);
+ }
+ g_ndp = NULL;
+
+ if (g_publish_handle)
+ wifi_aware_publish_destroy(g_publish_handle);
+ g_publish_handle = NULL;
+
+ if (g_wifi_aware_session) {
+ wifi_aware_session_stop(g_wifi_aware_session);
+ wifi_aware_session_unset_message_received_cb(g_wifi_aware_session);
+ wifi_aware_session_unset_terminated_cb(g_wifi_aware_session);
+ wifi_aware_session_destroy(g_wifi_aware_session);
+ }
+ g_wifi_aware_session = 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;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ g_ranging = false;
+ run_common();
+}
+
+void test_open_solicited()
+{
+ clear_resources();
+ g_pub_type = WIFI_AWARE_PUBLISH_TYPE_SOLICITED;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ g_ranging = false;
+ run_common();
+}
+
+void test_psk_unsolicited()
+{
+ clear_resources();
+ g_pub_type = WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ g_ranging = false;
+ run_common();
+}
+
+void test_psk_solicited()
+{
+ clear_resources();
+ g_pub_type = WIFI_AWARE_PUBLISH_TYPE_SOLICITED;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ g_ranging = false;
+ run_common();
+}
+
+void test_pmk_unsolicited()
+{
+ clear_resources();
+ g_pub_type = WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_PMK;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ g_ranging = false;
+ memcpy(g_pmk, PMK, PMK_LEN);
+ run_common();
+}
+
+void test_pmk_solicited()
+{
+ clear_resources();
+ g_pub_type = WIFI_AWARE_PUBLISH_TYPE_SOLICITED;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_PMK;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ g_ranging = false;
+ memcpy(g_pmk, PMK, PMK_LEN);
+ run_common();
+}
+
+void test_open_unsolicited_other_service()
+{
+ clear_resources();
+ g_pub_type = WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
+ strncpy(g_service_name, OTHER_SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ g_ranging = false;
+ run_common();
+}
+
+void test_open_solicited_other_service()
+{
+ clear_resources();
+ g_pub_type = WIFI_AWARE_PUBLISH_TYPE_SOLICITED;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
+ strncpy(g_service_name, OTHER_SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ g_ranging = false;
+ run_common();
+}
+
+void test_open_unsolicited_ranging()
+{
+ clear_resources();
+ g_pub_type = WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ g_ranging = true;
+ run_common();
+}
+
+void test_open_solicited_ranging()
+{
+ clear_resources();
+ g_pub_type = WIFI_AWARE_PUBLISH_TYPE_SOLICITED;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ g_ranging = true;
+ run_common();
+}
+
+void test_init()
+{
+ int ret = 0;
+
+ RET_IF_LOOP_IS_NULL();
+
+ if (g_initialized == false) {
+ ret = wifi_aware_initialize();
+ if (ret == WIFI_AWARE_ERROR_NONE)
+ PRINT_SUCCESS("[Initialize] Success");
+ else
+ PRINT_FAILURE_ERROR("[Initialize]", ret);
+ g_initialized = true;
+ }
+}
+
+void test_deinit()
+{
+}
+
+typedef void (*test_func)(void);
+test_func g_menu_func[] = {
+ [CMD_QUIT] = test_quit,
+ [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_PMK_UNSOLICITED] = test_pmk_unsolicited,
+ [CMD_PMK_SOLICITED] = test_pmk_solicited,
+ [CMD_OPEN_UNSOLICITED_OTHER_SERVICE] = test_open_unsolicited_other_service,
+ [CMD_OPEN_SOLICITED_OTHER_SERVICE] = test_open_solicited_other_service,
+ [CMD_OPEN_UNSOLICITED_RANGING] = test_open_unsolicited_ranging,
+ [CMD_OPEN_SOLICITED_RANGING] = test_open_solicited_ranging,
+
+ [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;
+
+ 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 if (buf[0] == '0') {
+ test_deinit();
+ printf(MAKE_GREEN"Test is finished.."RESET_COLOR"\n");
+ exit(1);
+ } else if (buf[0] == '\n' || buf[0] == '\r') {
+ __usage_full();
+ } else if (n - 1 > 0 && n < 1024) {
+ buf[n - 1] = '\0'; /* remove new line... */
+ printf("\n\n");
+ __process_input(buf, user_data);
+ } else {
+ printf("invalid input\n");
+ }
+
+ 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;
+}
--- /dev/null
+/*
+ * Wi-Fi Aware
+ *
+ * Copyright (c) 2020 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.
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <glib.h>
+
+#include "wifi-aware.h"
+#include "wifi-aware-matching-filter-test.h"
+
+#define RET_IF_LOOP_IS_NULL()\
+ do {\
+ if (!g_main_loop_p) {\
+ printf("Loop was not initialized\n");\
+ return;\
+ } \
+ } while (0)
+
+#define SUBSCRIBE_TIMEOUT 7000
+
+static GMainLoop *g_main_loop_p;
+static guint g_timeout_id = 0;
+
+static test_case_e g_current_test = DUMMY_TEST_NUM;
+static bool g_initialized = false;
+static bool g_enabled = false;
+
+static bool g_test_result = false;
+
+static wifi_aware_session_h g_wifi_aware_session = NULL;
+static wifi_aware_subscribe_h g_subscribe_handle = NULL;
+
+void test_init();
+void test_deinit();
+
+static void __print_test_result(int num, bool result)
+{
+ char test_name[30] = {0, };
+
+ if (num < NEGATIVE_TEST_01)
+ snprintf(test_name, 29, "%s %d", "Positivie Test", num);
+ else
+ snprintf(test_name, 29, "%s %d", "Negative Test", num - NEGATIVE_TEST_01 + 1);
+
+ if (result)
+ printf(MAKE_GREEN"[%s] is pass."RESET_COLOR"\n", test_name);
+ else
+ printf(MAKE_RED"[%s] is fail."RESET_COLOR"\n", test_name);
+
+ printf("Prease enter to run the next test...\n");
+}
+
+void test_quit()
+{
+ RET_IF_LOOP_IS_NULL();
+
+ g_main_loop_quit(g_main_loop_p);
+
+ return;
+}
+
+static void clear_resources()
+{
+ if (g_subscribe_handle)
+ wifi_aware_subscribe_destroy(g_subscribe_handle);
+ g_subscribe_handle = NULL;
+
+ if (g_wifi_aware_session) {
+ wifi_aware_session_stop(g_wifi_aware_session);
+ wifi_aware_session_unset_message_received_cb(g_wifi_aware_session);
+ wifi_aware_session_unset_service_discovered_cb(g_wifi_aware_session);
+ wifi_aware_session_unset_terminated_cb(g_wifi_aware_session);
+ wifi_aware_session_destroy(g_wifi_aware_session);
+ }
+ g_wifi_aware_session = NULL;
+
+ g_test_result = false;
+}
+
+static gboolean __subscribed_timeout_handler(gpointer user_data)
+{
+ printf("Not discovered! reached timeout!\n");
+
+ if (g_current_test >= NEGATIVE_TEST_01)
+ g_test_result = true;
+ else
+ g_test_result = false;
+
+ __print_test_result(g_current_test, g_test_result);
+ clear_resources();
+ return FALSE;
+}
+
+static void __subscribed_cb(wifi_aware_session_h session,
+ wifi_aware_error_e error, void *user_data)
+{
+ if (error == WIFI_AWARE_ERROR_NONE)
+ g_timeout_id = g_timeout_add(SUBSCRIBE_TIMEOUT, __subscribed_timeout_handler, session);
+ else
+ g_main_loop_quit(g_main_loop_p);
+}
+
+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,
+ int distance, void *user_data)
+{
+ printf("discovered!!\n");
+ if (g_current_test >= NEGATIVE_TEST_01)
+ g_test_result = false;
+ else
+ g_test_result = true;
+
+ if (g_timeout_id > 0) {
+ g_source_remove(g_timeout_id);
+ g_timeout_id = 0;
+ }
+ __print_test_result(g_current_test, g_test_result);
+ wifi_aware_session_unset_service_discovered_cb(g_wifi_aware_session);
+}
+
+static bool __set_default_configures(void)
+{
+ unsigned char specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
+
+ int ret = wifi_aware_session_create(WIFI_AWARE_SESSION_SUBSCRIBE, &g_wifi_aware_session);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return false;
+
+ ret = wifi_aware_subscribe_create(&g_subscribe_handle);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return false;
+
+ ret = wifi_aware_subscribe_set_service_name(g_subscribe_handle, SERVICE_NAME);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return false;
+
+ ret = wifi_aware_session_set_service_discovered_cb(g_wifi_aware_session,
+ __service_discovered_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return false;
+
+ size_t len = strlen(PUB_SSI);
+ memcpy(specific_info, PUB_SSI, len);
+ ret = wifi_aware_subscribe_set_service_specific_info(g_subscribe_handle, specific_info, len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return false;
+
+ return true;
+}
+
+static void __enabled_cb(wifi_aware_error_e error, void *user_data)
+{
+ if (error == WIFI_AWARE_ERROR_NONE)
+ g_enabled = true;
+ else
+ g_enabled = false;
+
+ g_main_loop_quit(g_main_loop_p);
+}
+
+void test_init()
+{
+ int ret = 0;
+
+ RET_IF_LOOP_IS_NULL();
+
+ if (g_initialized == false) {
+ ret = wifi_aware_initialize();
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ g_main_loop_quit(g_main_loop_p);
+ return;
+ }
+ g_initialized = true;
+ }
+
+ if (g_enabled == false) {
+ ret = wifi_aware_enable(__enabled_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ g_main_loop_quit(g_main_loop_p);
+ return;
+ }
+ }
+}
+
+void test_deinit()
+{
+ wifi_aware_disable();
+ wifi_aware_deinitialize();
+}
+
+void test_active_no_filter()
+{
+ PRINT_START(g_current_test);
+
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // No filter
+ ret = wifi_aware_session_subscribe(g_wifi_aware_session,
+ g_subscribe_handle, __subscribed_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_active_wildcard_filter()
+{
+ PRINT_START(g_current_test);
+
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <0><0><0><0><0>
+ ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
+ test_matching_filter[0].values, test_matching_filter[0].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_subscribe(g_wifi_aware_session,
+ g_subscribe_handle, __subscribed_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_active_value_filter_1()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <1,1><1,2><1,3><1,4><1,5>
+ ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
+ test_matching_filter[1].values, test_matching_filter[1].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_subscribe(g_wifi_aware_session,
+ g_subscribe_handle, __subscribed_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_active_value_filter_3()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <0><1,2><0><1,4>
+ ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
+ test_matching_filter[3].values, test_matching_filter[3].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_subscribe(g_wifi_aware_session,
+ g_subscribe_handle, __subscribed_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_active_value_filter_6()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <1,1><0><1,3><0><1,5>
+ ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
+ test_matching_filter[6].values, test_matching_filter[6].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_subscribe(g_wifi_aware_session,
+ g_subscribe_handle, __subscribed_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_passive_no_filter()
+{
+ PRINT_START(g_current_test);
+
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // No filter
+ ret = wifi_aware_session_subscribe(g_wifi_aware_session,
+ g_subscribe_handle, __subscribed_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_passive_wildcard_filter()
+{
+ PRINT_START(g_current_test);
+
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <0><0><0><0><0>
+ ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
+ test_matching_filter[0].values, test_matching_filter[0].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_subscribe(g_wifi_aware_session,
+ g_subscribe_handle, __subscribed_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_passive_value_filter_1()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <1,1><1,2><1,3><1,4><1,5>
+ ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
+ test_matching_filter[1].values, test_matching_filter[1].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_subscribe(g_wifi_aware_session,
+ g_subscribe_handle, __subscribed_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_passive_value_filter_2()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <1,1><1,2><1,1><1,4><1,5>
+ ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
+ test_matching_filter[2].values, test_matching_filter[2].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_subscribe(g_wifi_aware_session,
+ g_subscribe_handle, __subscribed_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_passive_value_filter_4()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <0><1,2><1,3><0><1,5>
+ ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
+ test_matching_filter[4].values, test_matching_filter[4].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_subscribe(g_wifi_aware_session,
+ g_subscribe_handle, __subscribed_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+void test_passive_value_filter_5()
+{
+ PRINT_START(g_current_test);
+ if (__set_default_configures() == false)
+ goto ERR;
+
+ int ret = wifi_aware_subscribe_set_type(g_subscribe_handle, WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ // <1,1><0><1,3><0>
+ ret = wifi_aware_subscribe_set_match_filter(g_subscribe_handle,
+ test_matching_filter[5].values, test_matching_filter[5].len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+
+ ret = wifi_aware_session_subscribe(g_wifi_aware_session,
+ g_subscribe_handle, __subscribed_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ goto ERR;
+ return;
+
+ERR:
+ g_test_result = false;
+ __print_test_result(g_current_test, g_test_result);
+}
+
+typedef void (*test_func)(void);
+test_func g_menu_func[] = {
+ [0] = NULL,
+ // active
+ [POSITIVE_TEST_01] = test_active_no_filter,
+ [POSITIVE_TEST_02] = test_active_no_filter,
+ [POSITIVE_TEST_03] = test_active_wildcard_filter,
+ [POSITIVE_TEST_04] = test_active_no_filter,
+
+ [POSITIVE_TEST_05] = test_active_wildcard_filter,
+ [POSITIVE_TEST_06] = test_active_value_filter_1,
+ [POSITIVE_TEST_07] = test_active_value_filter_1,
+
+ [POSITIVE_TEST_08] = test_active_value_filter_6,
+ [POSITIVE_TEST_09] = test_active_value_filter_1,
+ [POSITIVE_TEST_10] = test_active_value_filter_3,
+
+ // passive
+ [POSITIVE_TEST_11] = test_passive_no_filter,
+ [POSITIVE_TEST_12] = test_passive_wildcard_filter,
+ [POSITIVE_TEST_13] = test_passive_no_filter,
+ [POSITIVE_TEST_14] = test_passive_no_filter,
+
+ [POSITIVE_TEST_15] = test_passive_value_filter_1,
+ [POSITIVE_TEST_16] = test_passive_wildcard_filter,
+ [POSITIVE_TEST_17] = test_passive_value_filter_1,
+
+ [POSITIVE_TEST_18] = test_passive_value_filter_1,
+ [POSITIVE_TEST_19] = test_passive_value_filter_4,
+ [POSITIVE_TEST_20] = test_passive_value_filter_5,
+
+ // custom: unsol + active
+ [POSITIVE_TEST_21] = test_active_no_filter,
+ [POSITIVE_TEST_22] = test_active_no_filter,
+ [POSITIVE_TEST_23] = test_active_wildcard_filter,
+ [POSITIVE_TEST_24] = test_active_no_filter,
+
+ [POSITIVE_TEST_25] = test_active_wildcard_filter,
+ [POSITIVE_TEST_26] = test_active_value_filter_1,
+ [POSITIVE_TEST_27] = test_active_value_filter_1,
+
+ [POSITIVE_TEST_28] = test_active_value_filter_6,
+ [POSITIVE_TEST_29] = test_active_value_filter_1,
+ [POSITIVE_TEST_30] = test_active_value_filter_3,
+
+ // active
+ [NEGATIVE_TEST_01] = test_active_value_filter_1,
+ [NEGATIVE_TEST_02] = test_active_value_filter_1,
+ [NEGATIVE_TEST_03] = test_active_value_filter_1,
+
+ // passive
+ [NEGATIVE_TEST_04] = test_passive_value_filter_1,
+ [NEGATIVE_TEST_05] = test_passive_value_filter_2,
+ [NEGATIVE_TEST_06] = test_passive_value_filter_1,
+
+ // custom: unsol + active
+ [NEGATIVE_TEST_07] = test_active_value_filter_1,
+ [NEGATIVE_TEST_08] = test_active_value_filter_1,
+ [NEGATIVE_TEST_09] = test_active_value_filter_1,
+
+ [INVALID] = NULL,
+};
+
+static gboolean __test_terminal_read_std_input(GIOChannel * source,
+ GIOCondition condition, gpointer user_data)
+{
+ int fd = 0;
+ static char buf[1024];
+ int 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 {
+ ++g_current_test;
+ if (g_current_test == INVALID) {
+ printf("Test is finished..\n");
+ g_main_loop_quit(g_main_loop_p);
+ return TRUE;
+ }
+ clear_resources();
+ g_menu_func[g_current_test]();
+ }
+ 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);
+
+ printf(MAKE_GREEN"\nThis is Matching filter test on Subscriber side"RESET_COLOR"\n");
+
+ test_init();
+ g_main_loop_run(g_main_loop_p);
+ if (!g_enabled) {
+ printf(MAKE_RED"Can not start test.."RESET_COLOR"\n");
+ g_main_loop_unref(g_main_loop_p);
+ return 0;
+ }
+
+ 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);
+
+ printf("Please enter to start test..\n");
+
+ g_main_loop_run(g_main_loop_p);
+
+ g_main_loop_unref(g_main_loop_p);
+ test_deinit();
+
+ return 0;
+}
--- /dev/null
+/*
+ * Wi-Fi Aware
+ *
+ * Copyright (c) 2020 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 PRINT_SUCCESS(format, args...) printf(MAKE_GREEN format RESET_COLOR "\n", ##args)
+#define PRINT_FAILURE(format, args...) printf(MAKE_RED format RESET_COLOR "\n", ##args)
+#define PRINT_FAILURE_ERROR(format, error) printf(MAKE_RED format " Failure(%d:%s)" RESET_COLOR "\n", error, __print_error(error))
+
+#define SERVICE_NAME "Wi-Fi Aware Test"
+#define OTHER_SERVICE_NAME "Other Service Name"
+#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 WRONG_PSK "11223344"
+#define PMK_LEN 32
+#define PMK "12345678901234567890123456789012"
+#define WRONG_PMK "11223344556677889900123456789012"
+#define IPV6_ADDRESS_LEN 39
+
+#define SUBSCRIBE_TIMEOUT 5000
+
+enum {
+ CMD_QUIT,
+
+ CMD_OPEN_PASSIVE,
+ CMD_OPEN_ACTIVE,
+ CMD_PSK_PASSIVE,
+ CMD_PSK_ACTIVE,
+ CMD_PMK_PASSIVE,
+ CMD_PMK_ACTIVE,
+ CMD_WRONG_PSK_PASSIVE,
+ CMD_WRONG_PSK_ACTIVE,
+ CMD_WRONG_PMK_PASSIVE,
+ CMD_WRONG_PMK_ACTIVE,
+
+ CMD_OPEN_PASSIVE_OTHER_SERVICE,
+ CMD_OPEN_ACTIVE_OTHER_SERVICE,
+
+ CMD_OPEN_PASSIVE_RANGING,
+ CMD_OPEN_ACTIVE_RANGING,
+
+ CMD_INVALID,
+};
+
+static char *g_menu_str[] = {
+ [CMD_QUIT]
+ = "Quit",
+ [CMD_OPEN_PASSIVE]
+ = "Open + Passive",
+ [CMD_OPEN_ACTIVE]
+ = "Open + Active",
+ [CMD_PSK_PASSIVE]
+ = "PSK + Passive",
+ [CMD_PSK_ACTIVE]
+ = "PSK + Active",
+ [CMD_PMK_PASSIVE]
+ = "PMK + Passive",
+ [CMD_PMK_ACTIVE]
+ = "PMK + Active",
+ [CMD_WRONG_PSK_PASSIVE]
+ = "PSK + Passive (Wrong password)",
+ [CMD_WRONG_PSK_ACTIVE]
+ = "PSK + Active (Wrong password)",
+ [CMD_WRONG_PMK_PASSIVE]
+ = "PMK + Passive (Wrong password)",
+ [CMD_WRONG_PMK_ACTIVE]
+ = "PMK + Active (Wrong password)",
+ [CMD_OPEN_PASSIVE_OTHER_SERVICE]
+ = "Open + Passive + Other Service",
+ [CMD_OPEN_ACTIVE_OTHER_SERVICE]
+ = "Open + Active + Other Service",
+ [CMD_OPEN_PASSIVE_RANGING]
+ = "Open + Passive + Ranging",
+ [CMD_OPEN_ACTIVE_RANGING]
+ = "Open + Active + Ranging",
+ [CMD_INVALID]
+ = NULL,
+};
+
+static GMainLoop *g_main_loop_p;
+static int g_min_distance = -1;
+static int g_max_distance = -1;
+
+static bool g_initialized = false;
+static bool g_enabled = 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 char g_service_name[MAX_SERVICE_NAME_LEN + 1] = {0, };
+static char g_psk[MAX_PSK_LEN + 1];
+static unsigned char g_pmk[PMK_LEN];
+
+static bool g_service_discovered = false;
+static guint g_timeout_id = 0;
+
+void test_init();
+void test_deinit();
+
+static void __test_finish(int is_validated)
+{
+ if (is_validated)
+ printf(MAKE_GREEN"[TEST RESULT] Wi-Fi Aware validated!"RESET_COLOR"\n");
+ else
+ printf(MAKE_RED"[TEST RESULT] Wi-Fi Aware invalidated!"RESET_COLOR"\n");
+ test_deinit();
+}
+
+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;
+}
+
+static const char *__print_error(wifi_aware_error_e err)
+{
+ switch (err) {
+ case WIFI_AWARE_ERROR_NONE:
+ return "NO ERROR";
+ 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";
+ case WIFI_AWARE_ERROR_ALREADY_ENABLED:
+ return "ALREADY_ENABLED";
+ case WIFI_AWARE_ERROR_REJECTED_BY_PEER:
+ return "REJECTED_BY_PEER";
+ case WIFI_AWARE_ERROR_INTERFACE_DOWN:
+ return "INTERFACE_IS_DOWN";
+ default:
+ return "UNKNOWN";
+ }
+}
+
+static void __print_mac(unsigned char mac[6])
+{
+ printf(MACSTR, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+}
+
+static 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");
+}
+
+static void test_quit()
+{
+ RET_IF_LOOP_IS_NULL();
+
+ printf("Bye\n");
+ g_main_loop_quit(g_main_loop_p);
+
+ return;
+}
+
+static gboolean __subscribed_timeout_handler(gpointer user_data)
+{
+ wifi_aware_session_h session = (wifi_aware_session_h) user_data;
+ PRINT_FAILURE("[TIMEOUT][Subscribe] Session: %p", session);
+ __test_finish(0);
+ return FALSE;
+}
+
+static void __subscribed_cb(wifi_aware_session_h session,
+ wifi_aware_error_e error, void *user_data)
+{
+ if (error == WIFI_AWARE_ERROR_NONE) {
+ PRINT_SUCCESS("[CB][Subscribe] Requested");
+ g_timeout_id = g_timeout_add(SUBSCRIBE_TIMEOUT, __subscribed_timeout_handler, session);
+ } else {
+ PRINT_FAILURE_ERROR("[CB][Subscribe]", error);
+ __test_finish(0);
+ }
+}
+
+static void __data_path_terminated_cb(wifi_aware_data_path_h data_path,
+ wifi_aware_termination_reason_e reason, void *user_data)
+{
+ printf("[EVENT][DataPath Terminated] %p", 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;
+ PRINT_SUCCESS("[CB][OpenDataPath] Connected %p", data_path);
+ ret = wifi_aware_data_path_get_interface(data_path, &interface);
+ if (ret == WIFI_AWARE_ERROR_NONE)
+ printf("\tInterface: %s\n", interface);
+ else
+ PRINT_FAILURE_ERROR("\tInterface:", ret);
+
+ 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("\tPeer's IPv6 Address: %s\n", ipv6_address);
+ else
+ PRINT_FAILURE_ERROR("\tPeer's IPv6 Address:", ret);
+ ret = wifi_aware_data_path_get_peer_port(data_path, &port);
+ if (ret == WIFI_AWARE_ERROR_NONE)
+ printf("\tPeer's Port: %d\n", port);
+ else
+ PRINT_FAILURE_ERROR("\tPeer's Port:", ret);
+ __test_finish(1);
+ }
+ else {
+ PRINT_FAILURE("[CB][OpenDataPath] Failure(%d:%s) %p", error, __print_error(error), data_path);
+ __test_finish(0);
+ }
+}
+
+static void __open_data_path(wifi_aware_session_h session)
+{
+ printf("[OpenDataPath] Request\n");
+ if (g_peer == NULL) {
+ printf("Peer is NULL\n");
+ __test_finish(0);
+ return;
+ }
+
+ int ret = wifi_aware_data_path_create(session, g_peer, &g_ndp);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("\tHandle:", ret);
+ __test_finish(0);
+ return;
+ }
+ printf("\tHandle: %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) {
+ PRINT_FAILURE_ERROR("\tSet TerminatedCallback:", ret);
+ __test_finish(0);
+ return;
+ }
+
+ ret = wifi_aware_data_path_set_security(g_ndp, g_security_type);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("\tSecurity Type:", ret);
+ __test_finish(0);
+ return;
+ }
+ printf("\tSecurity Type: ");
+ switch (g_security_type) {
+ case WIFI_AWARE_SECURITY_TYPE_OPEN:
+ printf("Open\n");
+ break;
+ case WIFI_AWARE_SECURITY_TYPE_PSK:
+ printf("PSK\n");
+ wifi_aware_data_path_set_psk(g_ndp, g_psk);
+ break;
+ case WIFI_AWARE_SECURITY_TYPE_PMK:
+ wifi_aware_data_path_set_pmk(g_ndp, g_pmk);
+ printf("PMK\n");
+ break;
+ default:
+ PRINT_FAILURE("Unknown security type");
+ __test_finish(0);
+ return;
+ }
+
+ ret = wifi_aware_data_path_open(g_ndp, __open_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("[OpenDataPath]", ret);
+ __test_finish(0);
+ }
+}
+
+static void __message_result_cb(wifi_aware_session_h session, wifi_aware_error_e error, void *user_data)
+{
+ if (error == WIFI_AWARE_ERROR_NONE) {
+ PRINT_SUCCESS("[CB][Send Message] Requested");
+ } else {
+ PRINT_FAILURE_ERROR("[CB][Send Message]", error);
+ __test_finish(0);
+ }
+}
+
+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("[Event][Receive Message]\n");
+
+ if (wifi_aware_peer_get_mac(peer, &mac) != WIFI_AWARE_ERROR_NONE) {
+ printf("\tPeer's NMI: Failure(%d:%s)\n", ret, __print_error(ret));
+ } else {
+ printf("\tPeer's NMI: ");
+ if (mac != NULL) {
+ __print_mac(mac);
+ }
+ printf("\n");
+ }
+
+ if (mac)
+ g_free(mac);
+
+ memcpy(buf, message, 1024);
+ buf[len] = '\0';
+ printf("\tMessage: %s\n", buf);
+
+ 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,
+ int distance, void *user_data)
+{
+ unsigned char *mac = NULL;
+ int ret = 0;
+
+ PRINT_SUCCESS("[Event][Serivce Discovered]");
+ if (g_timeout_id > 0) {
+ g_source_remove(g_timeout_id);
+ g_timeout_id = 0;
+ }
+
+ ret = wifi_aware_peer_get_mac(peer, &mac);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ printf("\tPeer's NMI: Failure(%d:%s)\n", ret, __print_error(ret));
+ if (mac)
+ g_free(mac);
+ __test_finish(0);
+ return;
+ } else {
+ printf("\tPeer's NMI: ");
+ if (mac != NULL) {
+ __print_mac(mac);
+ g_free(mac);
+ }
+ printf("\n");
+ }
+ printf("\tService Specific Info(%d): ", service_specific_info_len);
+ __print_byte_array(service_specific_info, service_specific_info_len);
+ printf("\n");
+ printf("\tDistance: %d\n", distance);
+
+ if (g_service_discovered == false) {
+ g_service_discovered = true;
+ printf("[Send Message] Request\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_session_send_message(session, peer, send_message, message_len, __message_result_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("[Send Message]", ret);
+ __test_finish(0);
+ }
+ }
+}
+
+static void __session_terminated_cb(wifi_aware_session_h session,
+ wifi_aware_termination_reason_e reason, void *user_data)
+{
+ PRINT_FAILURE("[Event][Session Terminated] Session: %p Reason: %d", session, reason);
+
+ if (session != NULL) {
+ wifi_aware_session_unset_message_received_cb(session);
+ wifi_aware_session_destroy(session);
+ }
+ __test_finish(0);
+}
+
+static bool __set_subscribe_config(wifi_aware_subscribe_h subscribe)
+{
+ int ret = 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("\tSubscribe Type: %s\n", g_sub_type == WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE ?
+ "Passive" : "Active");
+ }
+ else {
+ PRINT_FAILURE_ERROR("\tSubscribe Type:", ret);
+ return false;
+ }
+
+ ret = wifi_aware_subscribe_set_service_name(subscribe,
+ g_service_name);
+ if (ret == WIFI_AWARE_ERROR_NONE) {
+ printf("\tService Name: %s\n", g_service_name);
+ }
+ else {
+ PRINT_FAILURE_ERROR("\tService Name:", ret);
+ 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("\tService Specific Info: ");
+ __print_byte_array(specific_info, len);
+ }
+ else {
+ PRINT_FAILURE_ERROR("\tService Specific Info:", ret);
+ return false;
+ }
+
+ if (g_min_distance >= 0) {
+ ret = wifi_aware_subscribe_set_min_distance(subscribe, g_min_distance);
+ if (ret == WIFI_AWARE_ERROR_NONE) {
+ printf("\tMin Distance: %d\n", g_min_distance);
+ }
+ else {
+ PRINT_FAILURE_ERROR("\tMin Distance:", ret);
+ return false;
+ }
+ }
+ if (g_max_distance > 0) {
+ ret = wifi_aware_subscribe_set_max_distance(subscribe, g_max_distance);
+ if (ret == WIFI_AWARE_ERROR_NONE) {
+ printf("\tMax Distance: %d\n", g_min_distance);
+ }
+ else {
+ PRINT_FAILURE_ERROR("\tMax Distance:", ret);
+ return false;
+ }
+ }
+ return true;
+}
+
+static void start_subscribe()
+{
+ printf("[Session][Subscribe] Create\n");
+ int ret = wifi_aware_session_create(WIFI_AWARE_SESSION_SUBSCRIBE, &g_wifi_aware_session);
+ if (ret == WIFI_AWARE_ERROR_NONE) {
+ printf("\tSession: %p\n", g_wifi_aware_session);
+ } else {
+ PRINT_FAILURE_ERROR("\tSession:", ret);
+ __test_finish(0);
+ }
+
+ ret = wifi_aware_subscribe_create(&g_subscribe_handle);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("\tSubscribe Config:", ret);
+ __test_finish(0);
+ }
+
+ if (__set_subscribe_config(g_subscribe_handle) == false) {
+ PRINT_FAILURE("\tSubscribeConfig: Failure");
+ __test_finish(0);
+ return;
+ }
+
+ g_service_discovered = false;
+ ret = wifi_aware_session_set_service_discovered_cb(g_wifi_aware_session, __service_discovered_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("\tService Discovered CB:", ret);
+ __test_finish(0);
+ }
+
+ ret = wifi_aware_session_set_message_received_cb(g_wifi_aware_session, __message_received_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("\tMessage Received CB:", ret);
+ __test_finish(0);
+ }
+
+ ret = wifi_aware_session_set_terminated_cb(g_wifi_aware_session, __session_terminated_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("\tSession Terminated CB:", ret);
+ __test_finish(0);
+ }
+
+ printf("[Subscribe] Request\n");
+ ret = wifi_aware_session_subscribe(g_wifi_aware_session, g_subscribe_handle, __subscribed_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("[Subscribe]", ret);
+ __test_finish(0);
+ }
+}
+
+static void __enabled_cb(wifi_aware_error_e error, void *user_data)
+{
+ if (error == WIFI_AWARE_ERROR_NONE) {
+ PRINT_SUCCESS("[CB][Enable] Enabled");
+ }
+ else {
+ PRINT_FAILURE_ERROR("[CB][Enable]", error);
+ return;
+ }
+
+ start_subscribe();
+}
+
+void run_common()
+{
+ int ret = 0;
+ RET_IF_LOOP_IS_NULL();
+
+ test_init();
+ if (g_enabled == false) {
+ ret = wifi_aware_enable(__enabled_cb, NULL);
+ if (ret != WIFI_AWARE_ERROR_NONE) {
+ PRINT_FAILURE_ERROR("[Enable]", ret);
+ __test_finish(0);
+ return;
+ }
+ g_enabled = true;
+ } else {
+ start_subscribe();
+ }
+}
+
+void clear_resources()
+{
+ if (g_ndp) {
+ wifi_aware_data_path_unset_terminated_cb(g_ndp);
+ wifi_aware_data_path_destroy(g_ndp);
+ }
+ g_ndp = NULL;
+
+ if (g_subscribe_handle)
+ wifi_aware_subscribe_destroy(g_subscribe_handle);
+ g_subscribe_handle = NULL;
+
+ if (g_wifi_aware_session) {
+ wifi_aware_session_stop(g_wifi_aware_session);
+ wifi_aware_session_unset_message_received_cb(g_wifi_aware_session);
+ wifi_aware_session_unset_service_discovered_cb(g_wifi_aware_session);
+ wifi_aware_session_unset_terminated_cb(g_wifi_aware_session);
+ wifi_aware_session_destroy(g_wifi_aware_session);
+ }
+ g_wifi_aware_session = 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;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ run_common();
+}
+
+void test_open_active()
+{
+ clear_resources();
+ g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ run_common();
+}
+
+void test_psk_passive()
+{
+ clear_resources();
+ g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ strncpy(g_psk, PSK, MAX_PSK_LEN);
+ g_psk[MAX_PSK_LEN] = '\0';
+ run_common();
+}
+
+void test_psk_active()
+{
+ clear_resources();
+ g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ strncpy(g_psk, PSK, MAX_PSK_LEN);
+ g_psk[MAX_PSK_LEN] = '\0';
+ run_common();
+}
+
+void test_pmk_passive()
+{
+ clear_resources();
+ g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_PMK;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ memcpy(g_pmk, PMK, PMK_LEN);
+ run_common();
+}
+
+void test_pmk_active()
+{
+ clear_resources();
+ g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_PMK;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ memcpy(g_pmk, PMK, PMK_LEN);
+ run_common();
+}
+
+void test_wrong_psk_passive()
+{
+ clear_resources();
+ g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ strncpy(g_psk, WRONG_PSK, MAX_PSK_LEN);
+ g_psk[MAX_PSK_LEN] = '\0';
+ run_common();
+}
+
+void test_wrong_psk_active()
+{
+ clear_resources();
+ g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_PSK;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ strncpy(g_psk, WRONG_PSK, MAX_PSK_LEN);
+ g_psk[MAX_PSK_LEN] = '\0';
+ run_common();
+}
+
+void test_wrong_pmk_passive()
+{
+ clear_resources();
+ g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_PMK;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ memcpy(g_pmk, WRONG_PMK, PMK_LEN);
+ run_common();
+}
+
+void test_wrong_pmk_active()
+{
+ clear_resources();
+ g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_PMK;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ memcpy(g_pmk, WRONG_PMK, PMK_LEN);
+ run_common();
+}
+
+void test_open_passive_other_service()
+{
+ clear_resources();
+ g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
+ strncpy(g_service_name, OTHER_SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ run_common();
+}
+
+void test_open_active_other_service()
+{
+ clear_resources();
+ g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
+ strncpy(g_service_name, OTHER_SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ run_common();
+}
+
+void test_open_passive_ranging()
+{
+ clear_resources();
+ g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ g_min_distance = 0;
+ g_max_distance = 100000;
+ run_common();
+}
+
+void test_open_active_ranging()
+{
+ clear_resources();
+ g_sub_type = WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE;
+ g_security_type = WIFI_AWARE_SECURITY_TYPE_OPEN;
+ strncpy(g_service_name, SERVICE_NAME, MAX_SERVICE_NAME_LEN);
+ g_min_distance = 0;
+ g_max_distance = 100000;
+ run_common();
+}
+
+void test_init()
+{
+ int ret = 0;
+
+ RET_IF_LOOP_IS_NULL();
+
+ if (g_initialized == false) {
+ ret = wifi_aware_initialize();
+ if (ret == WIFI_AWARE_ERROR_NONE)
+ PRINT_SUCCESS("[Initialize] Success");
+ else
+ PRINT_FAILURE_ERROR("[Initialize]", ret);
+ g_initialized = true;
+ }
+}
+
+void test_deinit()
+{
+}
+
+typedef void (*test_func)(void);
+test_func g_menu_func[] = {
+ [CMD_QUIT] = test_quit,
+ [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_PMK_PASSIVE] = test_pmk_passive,
+ [CMD_PMK_ACTIVE] = test_pmk_active,
+ [CMD_WRONG_PSK_PASSIVE] = test_wrong_psk_passive,
+ [CMD_WRONG_PSK_ACTIVE] = test_wrong_psk_active,
+ [CMD_WRONG_PMK_PASSIVE] = test_wrong_pmk_passive,
+ [CMD_WRONG_PMK_ACTIVE] = test_wrong_pmk_active,
+ [CMD_OPEN_PASSIVE_OTHER_SERVICE] = test_open_passive_other_service,
+ [CMD_OPEN_ACTIVE_OTHER_SERVICE] = test_open_active_other_service,
+ [CMD_OPEN_PASSIVE_RANGING] = test_open_passive_ranging,
+ [CMD_OPEN_ACTIVE_RANGING] = test_open_active_ranging,
+
+ [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;
+
+ 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 if (buf[0] == '0') {
+ test_deinit();
+ printf(MAKE_GREEN"Test is finished.."RESET_COLOR"\n");
+ exit(1);
+ } else if (buf[0] == '\n' || buf[0] == '\r') {
+ __usage_full();
+ } else if (n - 1 > 0 && n < 1024) {
+ buf[n - 1] = '\0'; /* remove new line... */
+ printf("\n\n");
+ __process_input(buf, user_data);
+ } else {
+ printf("invalid input\n");
+ }
+
+ 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;
+}
--- /dev/null
+/*
+ * Wi-Fi Aware
+ *
+ * Copyright (c) 2020 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 RET_IF_SESSION_IS_NULL()\
+ do {\
+ if (!g_wifi_aware_session) {\
+ printf("wifi-aware-session handle is not created yet\n");\
+ return;\
+ }\
+ } while (0)
+
+#define RET_IF_NDP_IS_NULL()\
+ do {\
+ if (!g_ndp) {\
+ printf("wifi_aware_data_path handle is not created yet\n");\
+ return;\
+ }\
+ } while (0)
+
+#define MATCH_FILTER "bytes used for matching"
+#define PUB_SSI "Extra bytes in the publisher discovery"
+#define UPDATE_PUB_SSI "Updated Specific Information"
+#define SUB_SSI "Arbitrary bytes for the subscribe discovery"
+#define UPDATE_SUB_SSI "Updated Arbitrary bytes for the subscribe discovery"
+#define MSG_SUB_TO_PUB "Let's talk"
+#define MSG_PUB_TO_SUB "Ready"
+#define PASSPHRASE "Some super secret password"
+
+#define MAX_SERVICE_NAME_LEN 255
+#define MAX_SPECIFIC_INFO_LEN 1024
+#define MAX_MATCH_FILTER_LEN 255
+
+#define STR_(X) #X
+#define STR(X) STR_(X)
+
+#define MAX_PSK_LEN 63
+#define PMK_LEN 32
+#define IPV6_ADDRESS_LEN 39
+
+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,
+
+ CMD_INITIALIZE,
+ CMD_DEINITIALIZE,
+
+ CMD_ENABLE,
+ CMD_DISABLE,
+
+ CMD_PUBLISH,
+ CMD_CANCEL_PUBLISH,
+ CMD_UPDATE_PUBLISH,
+ CMD_SUBSCRIBE,
+ CMD_UPDATE_SUBSCRIBE,
+ CMD_CANCEL_SUBSCRIBE,
+
+ CMD_MESSAGE_RECV_CB,
+ CMD_SEND_MESSAGE,
+
+ CMD_OPEN_DATA_PATH,
+
+ CMD_TEST_SPECIFIC_INFO,
+ CMD_INVALID,
+};
+
+/*****************************************************************************
+ * Global Variables
+ *****************************************************************************/
+
+static char *g_menu_str[] = {
+ [CMD_QUIT]
+ = "QUIT",
+ [CMD_FULL_MENU]
+ = "FULL_MENU",
+ [CMD_INITIALIZE]
+ = "INITIALIZE",
+ [CMD_DEINITIALIZE]
+ = "DEINITIALIZE",
+
+ [CMD_ENABLE]
+ = "ENABLE",
+ [CMD_DISABLE]
+ = "DISABLE",
+
+ [CMD_PUBLISH]
+ = "PUBLISH",
+ [CMD_CANCEL_PUBLISH]
+ = "CANCEL PUBLISH",
+ [CMD_UPDATE_PUBLISH]
+ = "UPDATE PUBLISH",
+ [CMD_SUBSCRIBE]
+ = "SUBSCRIBE",
+ [CMD_UPDATE_SUBSCRIBE]
+ = "UPDATE_SUBSCRIBE",
+ [CMD_CANCEL_SUBSCRIBE]
+ = "CANCEL SUBSCRIBE",
+
+ [CMD_MESSAGE_RECV_CB]
+ = "SET MESSAGE_RECV CB",
+ [CMD_SEND_MESSAGE]
+ = "SEND MESSAGE",
+ [CMD_OPEN_DATA_PATH]
+ = "OPEN DATA PATH",
+
+ [CMD_TEST_SPECIFIC_INFO]
+ = "TEST SPECIFIC INFO",
+ [CMD_INVALID]
+ = NULL,
+};
+
+static GMainLoop *g_main_loop_p;
+static wifi_aware_session_h g_wifi_aware_session = NULL;
+static wifi_aware_publish_h g_publish_handle = NULL;
+static wifi_aware_subscribe_h g_subscribe_handle = NULL;
+static wifi_aware_data_path_h g_ndp = NULL;
+static int g_session_type = -1; // 0: publish, 1: subscribe
+static unsigned char g_msg[4];
+
+/*****************************************************************************
+ * Local Functions Definition
+ *****************************************************************************/
+
+static inline char* __cmd_transform(char*str)
+{
+ int i, j;
+ int len;
+ static char static_buffer[255];
+
+ if (str == NULL)
+ return "";
+
+ len = strlen(str);
+ if (len == 0)
+ return "";
+
+ /* lower char */
+ /* replance "_" to space */
+ for (i = 0, j = 0; i < len; i++, j++) {
+
+ if (str[j] >= 'A' && str[j] <= 'Z')
+ static_buffer[i] = str[j] + 'a' - 'A';
+ else if (str[j] == '_')
+ static_buffer[i] = ' ';
+ else
+ static_buffer[i] = str[j];
+ }
+ static_buffer[j] = '\0';
+
+ return static_buffer;
+}
+
+static inline void __usage_full()
+{
+ int i;
+ printf("Call Test Program\n");
+
+ for (i = CMD_QUIT; i < CMD_INVALID; i++) {
+ if (i%3 == 0)
+ printf("\n");
+ printf(" %02d: %-32s ", i,
+ __cmd_transform(g_menu_str[i]));
+ }
+ printf("\n");
+}
+
+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";
+ case WIFI_AWARE_ERROR_ALREADY_ENABLED:
+ return "ALREADY_ENABLED";
+ case WIFI_AWARE_ERROR_REJECTED_BY_PEER:
+ return "REJECTED_BY_PEER";
+ case WIFI_AWARE_ERROR_INTERFACE_DOWN:
+ return "INTERFACE_IS_DOWN";
+ default:
+ return "UNKNOWN";
+ }
+}
+
+static void __print_result(int ret, gchar *function_name)
+{
+ if (ret == WIFI_AWARE_ERROR_NONE) {
+ printf(MAKE_GREEN"%s"RESET_COLOR"\n", function_name);
+ } else {
+ printf(MAKE_RED"%s: %s ", function_name, __print_error(ret));
+ printf(RESET_COLOR"\n");
+ }
+
+ printf("%s() result=[%d]\n", function_name, ret);
+}
+
+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 (strchr(" &;`'\"|*?~<>^()[]{}$", *ptr))
+ printf("\\");
+ 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;
+}
+
+void test_init()
+{
+ int ret = 0;
+
+ RET_IF_LOOP_IS_NULL();
+
+ ret = wifi_aware_initialize();
+ __print_result(ret, "wifi_aware_initialize");
+}
+
+void test_deinit()
+{
+ int ret = 0;
+
+ RET_IF_LOOP_IS_NULL();
+
+ ret = wifi_aware_deinitialize();
+ __print_result(ret, "wifi_aware_deinitialize");
+}
+
+
+static void __enabled_cb(wifi_aware_error_e error, void *user_data)
+{
+ if (error == WIFI_AWARE_ERROR_NONE)
+ printf("Wi-Fi Aware is enabled\n");
+ else
+ printf("Wi-Fi Aware is not enabled. Error: %s\n", __print_error(error));
+}
+
+void test_enable()
+{
+ int ret = 0;
+
+ RET_IF_LOOP_IS_NULL();
+
+ ret = wifi_aware_enable(__enabled_cb, NULL);
+ __print_result(ret, "wifi_aware_enable");
+}
+
+void test_disable()
+{
+ int ret = 0;
+
+ RET_IF_LOOP_IS_NULL();
+
+ ret = wifi_aware_disable();
+ __print_result(ret, "wifi_aware_disable");
+}
+
+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. Publish ID: [*]\n");
+ else
+ printf("Publish Request fails. Error: %s\n", __print_error(error));
+}
+
+static void __print_match_filter(unsigned char filter[MAX_MATCH_FILTER_LEN + 1], int filter_len)
+{
+ int len = 0;
+ int i = 0;
+ while (i < filter_len) {
+ len = filter[i];
+ printf("%d: ", len);
+ for (int j = 1; j <= len; ++j) {
+ printf("%d, ", filter[i + j]);
+ }
+ printf("\n");
+ i += len + 1;
+ }
+}
+
+static int __set_match_filter(unsigned char match_filter[MAX_MATCH_FILTER_LEN + 1])
+{
+ int total_num;
+ int total_len = 0;
+ int len;
+ char filter[MAX_MATCH_FILTER_LEN + 1];
+ printf("Total number of match filter items: ");
+ if (scanf("%d", &total_num) < 0)
+ return -1;
+
+ if (total_num < 0 || total_num > MAX_MATCH_FILTER_LEN) {
+ printf("Too many match filters");
+ return -1;
+ }
+ for (int i = 0; i < total_num; ++i) {
+ if (total_len > MAX_MATCH_FILTER_LEN) {
+ printf("The length of filter value is longer than max length(%d)", MAX_MATCH_FILTER_LEN);
+ return -1;
+ }
+ printf("%d match filter len: ", i);
+ if (scanf("%d", &len) < 0)
+ return -1;
+ if (len <= 0 || len > MAX_MATCH_FILTER_LEN) {
+ printf("Invalid match filter lentgh");
+ return -1;
+ }
+ if (total_len + len > MAX_MATCH_FILTER_LEN) {
+ printf("The length of filter value is longer than max length(%d)", MAX_MATCH_FILTER_LEN);
+ return -1;
+ }
+ printf("%d match filter value: ", i);
+ if (scanf("%" STR(MAX_MATCH_FILTER_LEN) "s", filter) < 0)
+ return -1;
+ if (len != strlen(filter)) {
+ printf("The length of filter value is not %d\n", len);
+ return -1;
+ }
+ match_filter[total_len++] = len;
+ memcpy(&match_filter[total_len], filter, len);
+ total_len += len;
+ }
+ return total_len;
+}
+
+static void __set_publish_config(wifi_aware_publish_h publish)
+{
+ int ret = 0;
+ int publish_type = 0;
+ char service_name[MAX_SERVICE_NAME_LEN + 1] = {0, };
+ char str_specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
+ unsigned char specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
+ unsigned char match_filter[MAX_MATCH_FILTER_LEN + 1] = {0, };
+ size_t len = 0;
+ int set_info = 0;
+ int set_mf = 0;
+
+ printf("Service Name: ");
+ if (scanf(" %" STR(MAX_SERVICE_NAME_LEN) "s", service_name) < 0)
+ return;
+
+ ret = wifi_aware_publish_set_service_name(publish,
+ service_name);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return;
+
+ printf("Choose publish type(0-Solicited, 1-Unsolicited): ");
+ if (scanf("%d", &publish_type) < 0)
+ return;
+
+ ret = wifi_aware_publish_set_type(publish,
+ publish_type ? WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED
+ : WIFI_AWARE_PUBLISH_TYPE_SOLICITED);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return;
+
+ printf("Do you want to set Service Specific Information? (0-No, 1-Yes) ");
+ if (scanf("%d", &set_info) < 0)
+ return;
+
+ if (set_info == 1) {
+ if (scanf(" %" STR(MAX_SPECIFIC_INFO_LEN) "s", str_specific_info) < 0)
+ return;
+
+ len = strlen(str_specific_info);
+ memcpy(specific_info, str_specific_info, len);
+ ret = wifi_aware_publish_set_service_specific_info(publish,
+ specific_info, len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return;
+ }
+
+ printf("Do you want to set Match Filter? (0-No, 1-Yes) ");
+ if (scanf("%d", &set_mf) < 0)
+ return;
+
+ if (set_mf == 1) {
+ int n = __set_match_filter(match_filter);
+ if (n < 0 || n >= MAX_MATCH_FILTER_LEN) {
+ printf("Not set match filter\n");
+ return;
+ }
+ __print_match_filter(match_filter, n);
+ ret = wifi_aware_publish_set_match_filter(publish, match_filter, n);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return;
+ }
+}
+
+void test_publish()
+{
+ int ret = 0;
+
+ RET_IF_LOOP_IS_NULL();
+
+ ret = wifi_aware_session_create(WIFI_AWARE_SESSION_PUBLISH, &g_wifi_aware_session);
+ RET_IF_SESSION_IS_NULL();
+
+ ret = wifi_aware_publish_create(&g_publish_handle);
+ __print_result(ret, "wifi_aware_publish_create");
+
+ __set_publish_config(g_publish_handle);
+
+ ret = wifi_aware_session_publish(g_wifi_aware_session, g_publish_handle, __published_cb, NULL);
+ __print_result(ret, "wifi_aware_publish");
+
+ memcpy(g_msg, "TEST", 4);
+ g_session_type = 0;
+ //ret = wifi_aware_publish_destroy(publish);
+ //__print_result(ret, "wifi_aware_publish_destroy");
+}
+
+void test_cancel_publish()
+{
+ int ret = 0;
+
+ RET_IF_LOOP_IS_NULL();
+ RET_IF_SESSION_IS_NULL();
+
+ if (g_publish_handle == NULL) {
+ printf("publish should be done\n");
+ return;
+ }
+
+ ret = wifi_aware_session_stop(g_wifi_aware_session);
+ __print_result(ret, "wifi_aware_cancel_publish");
+}
+
+static void __updated_publish_cb(wifi_aware_session_h session,
+ wifi_aware_error_e error, void *user_data)
+{
+ if (error == WIFI_AWARE_ERROR_NONE)
+ printf("Update Publish Request is success. Publish ID: [*]\n");
+ else
+ printf("Update Publish Request is fail.\n");
+}
+
+void test_update_publish()
+{
+ int ret = 0;
+ unsigned char specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
+ size_t len = 0;
+
+ RET_IF_LOOP_IS_NULL();
+ RET_IF_SESSION_IS_NULL();
+
+ if (g_publish_handle == NULL) {
+ printf("publish should be done\n");
+ return;
+ }
+
+ len = strlen(UPDATE_PUB_SSI);
+ memcpy(specific_info, UPDATE_PUB_SSI, len);
+ ret = wifi_aware_publish_set_service_specific_info(g_publish_handle,
+ specific_info, len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return;
+
+ ret = wifi_aware_session_update_publish(g_wifi_aware_session, g_publish_handle, __updated_publish_cb, NULL);
+ __print_result(ret, "wifi_aware_update_publish");
+}
+
+static void __subscribed_cb(wifi_aware_session_h session,
+ wifi_aware_error_e error, void *user_data)
+{
+ if (error == WIFI_AWARE_ERROR_NONE)
+ printf("Subscribe Request is success. Subscirbe ID: [*]\n");
+ else
+ 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));
+ if (info == NULL) {
+ printf("Out of memory\n");
+ return;
+ }
+ info->peer_id = g_peer_id++;
+ info->peer = peer;
+ g_peer_list = g_slist_append(g_peer_list, info);
+}
+
+static void __print_mac(unsigned char mac[6])
+{
+ printf(MACSTR, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+}
+
+static void __print_peer_info(gpointer data, gpointer user_data)
+{
+ peer_info *info = (peer_info *)data;
+ unsigned char *mac = NULL;
+ wifi_aware_peer_get_mac(info->peer, &mac);
+ printf("%02d %p ", info->peer_id, info->peer);
+ __print_mac(mac);
+ free(mac);
+ printf("\n");
+}
+
+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,
+ int distance, void *user_data)
+{
+ printf("Find A Wi-Fi Aware Peer %p\n", peer);
+// printf("Subscribe ID: %u\n", subscribe_id);
+ printf("Service Specific Info: ");
+ __print_byte_array(service_specific_info, service_specific_info_len);
+ printf("\n");
+ printf("Distance: %d\n", distance);
+ __add_new_peer(peer);
+}
+
+static void __set_subscribe_config(wifi_aware_subscribe_h subscribe)
+{
+ int ret = 0;
+ int subscribe_type = 0;
+ char service_name[MAX_SERVICE_NAME_LEN + 1] = {0, };
+ char str_specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
+ unsigned char specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
+ unsigned char match_filter[MAX_MATCH_FILTER_LEN + 1] = {0, };
+ size_t len = 0;
+ int set_info = 0;
+ int set_mf = 0;
+
+ printf("Service Name: ");
+ if (scanf("%" STR(MAX_SERVICE_NAME_LEN) "s", service_name) < 0)
+ return;
+
+ ret = wifi_aware_subscribe_set_service_name(subscribe,
+ service_name);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return;
+
+ printf("Choose subscribe type(0-Active, 1-Passive): ");
+ if (scanf("%d", &subscribe_type) < 0)
+ return;
+
+ ret = wifi_aware_subscribe_set_type(subscribe,
+ subscribe_type ? WIFI_AWARE_SUBSCRIBE_TYPE_PASSIVE
+ : WIFI_AWARE_SUBSCRIBE_TYPE_ACTIVE);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return;
+
+ printf("Do you want to set Service Specific Information? (0-No, 1-Yes) ");
+ if (scanf("%d", &set_info) < 0)
+ return;
+
+ if (set_info == 1) {
+ if (scanf(" %" STR(MAX_SPECIFIC_INFO_LEN) "s", str_specific_info) < 0)
+ return;
+
+ len = strlen(str_specific_info);
+ memcpy(specific_info, str_specific_info, len);
+ ret = wifi_aware_subscribe_set_service_specific_info(subscribe,
+ specific_info, len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return;
+ }
+
+ printf("Do you set Match Filter(0-No, 1-Yes): ");
+ if (scanf("%d", &set_mf) < 0)
+ return;
+ if (set_mf == 1) {
+ int n = __set_match_filter(match_filter);
+ if (n < 0 || n >= MAX_MATCH_FILTER_LEN) {
+ printf("Not set match filter\n");
+ return;
+ }
+ ret = wifi_aware_subscribe_set_match_filter(subscribe, match_filter, n);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return;
+ }
+}
+
+void test_subscribe()
+{
+ int ret = 0;
+
+ RET_IF_LOOP_IS_NULL();
+
+ ret = wifi_aware_session_create(WIFI_AWARE_SESSION_SUBSCRIBE, &g_wifi_aware_session);
+ RET_IF_SESSION_IS_NULL();
+
+ ret = wifi_aware_subscribe_create(&g_subscribe_handle);
+ __print_result(ret, "wifi_aware_subscribe_create");
+
+ ret = wifi_aware_session_set_service_discovered_cb(g_wifi_aware_session, __service_discovered_cb, NULL);
+ __print_result(ret, "wifi_aware_session_set_service_discovered_cb");
+
+ __set_subscribe_config(g_subscribe_handle);
+
+ ret = wifi_aware_session_subscribe(g_wifi_aware_session, g_subscribe_handle, __subscribed_cb, NULL);
+ __print_result(ret, "wifi_aware_subscribe");
+
+ memcpy(g_msg, "ABCD", 4);
+ g_session_type = 1;
+}
+
+static void __updated_subscribe_cb(wifi_aware_session_h session,
+ wifi_aware_error_e error, void *user_data)
+{
+ if (error == WIFI_AWARE_ERROR_NONE)
+ printf("Update Subscribe Request is success. Subscribe ID: [*]\n");
+ else
+ printf("Update Subscribe Request is fail.\n");
+}
+
+void test_update_subscribe()
+{
+ int ret = 0;
+ unsigned char specific_info[MAX_SPECIFIC_INFO_LEN + 1] = {0, };
+ size_t len = 0;
+
+ RET_IF_LOOP_IS_NULL();
+ RET_IF_SESSION_IS_NULL();
+
+ if (g_subscribe_handle == NULL) {
+ printf("subscribe should be done\n");
+ return;
+ }
+
+ len = strlen(UPDATE_SUB_SSI);
+ memcpy(specific_info, UPDATE_SUB_SSI, len);
+ ret = wifi_aware_subscribe_set_service_specific_info(g_subscribe_handle,
+ specific_info, len);
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return;
+
+ ret = wifi_aware_session_update_subscribe(g_wifi_aware_session, g_subscribe_handle, __updated_subscribe_cb, NULL);
+ __print_result(ret, "wifi_aware_update_subscribe");
+}
+
+void test_cancel_subscribe()
+{
+ int ret = 0;
+
+ RET_IF_LOOP_IS_NULL();
+ RET_IF_SESSION_IS_NULL();
+
+ if (g_subscribe_handle == NULL) {
+ printf("subscribe should be done\n");
+ return;
+ }
+
+ ret = wifi_aware_session_stop(g_wifi_aware_session);
+ __print_result(ret, "wifi_aware_cancel_subscribe");
+}
+
+static struct {
+ wifi_aware_session_h session;
+ wifi_aware_publish_h handle;
+ char service_name[MAX_SERVICE_NAME_LEN + 1];
+ unsigned char info[MAX_SPECIFIC_INFO_LEN];
+ size_t len;
+} info_test_data[] = {
+ { NULL, NULL, "SPECIFIC_INFO_TEST_1", {1, 2, 3, 4, 5}, 5 },
+ { NULL, NULL, "SPECIFIC_INFO_TEST_2", {'A', 'B', 'C', '\0'}, 4 },
+ { NULL, NULL, "SPECIFIC_INFO_TEST_3", {0, 0, 0, 0, 0, 0, 0}, 7 },
+ { NULL, NULL, "SPECIFIC_INFO_TEST_4", {1, 2, 3, 4, 5}, 3 },
+ { NULL, NULL, "SPECIFIC_INFO_TEST_5", {1, 2, 3, 4, 5}, 7 },
+};
+
+void test_specific_info()
+{
+ int ret = 0;
+
+ RET_IF_LOOP_IS_NULL();
+
+ for (int i = 0; i < sizeof(info_test_data) / sizeof(info_test_data[0]); ++i) {
+ ret = wifi_aware_session_create(WIFI_AWARE_SESSION_PUBLISH, &info_test_data[i].session);
+ __print_result(ret, "wifi_aware_session_create");
+
+ ret = wifi_aware_publish_create(&info_test_data[i].handle);
+ __print_result(ret, "wifi_aware_publish_create");
+
+ ret = wifi_aware_publish_set_type(info_test_data[i].handle, WIFI_AWARE_PUBLISH_TYPE_UNSOLICITED);
+ __print_result(ret, "wifi_aware_publish_set_type");
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return;
+
+ ret = wifi_aware_publish_set_service_name(info_test_data[i].handle,
+ info_test_data[i].service_name);
+ __print_result(ret, "wifi_aware_publish_set_service_name");
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return;
+
+ ret = wifi_aware_publish_set_service_specific_info(info_test_data[i].handle,
+ info_test_data[i].info, info_test_data[i].len);
+ __print_result(ret, "wifi_aware_publish_set_service_specific_info");
+ if (ret != WIFI_AWARE_ERROR_NONE)
+ return;
+
+ ret = wifi_aware_session_publish(info_test_data[i].session, info_test_data[i].handle, __published_cb, NULL);
+ __print_result(ret, "wifi_aware_session_publish");
+ }
+}
+
+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_session_set_message_received_cb(g_wifi_aware_session, __message_received_cb, NULL);
+ __print_result(ret, "wifi_aware_session_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_session_send_message(g_wifi_aware_session, peer, g_msg, len, __message_result_cb, peer);
+ __print_result(ret, "wifi_aware_send_message");
+ printf("Send a message to %p", peer);
+}
+
+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("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");
+
+ if (g_session_type == 1) {
+ 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. error: %s\n", data_path, __print_error(error));
+ }
+
+}
+
+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.\n", data_path);
+ wifi_aware_data_path_unset_terminated_cb(data_path);
+}
+
+void test_open_data_path()
+{
+ int ret = WIFI_AWARE_ERROR_NONE;
+ int peer_id;
+ int security_type;
+
+ RET_IF_LOOP_IS_NULL();
+
+ __display_peer_list();
+ printf("Select a peer to connect: ");
+ if (scanf("%d", &peer_id) < 0)
+ return;
+
+ wifi_aware_peer_h peer = __get_peer_by_id(peer_id);
+ ret = wifi_aware_data_path_create(g_wifi_aware_session, peer, &g_ndp);
+ __print_result(ret, "wifi_aware_data_path_create");
+ printf("NDP %p", g_ndp);
+
+ printf("Choose security type(0-Open, 1-PSK, 2-PMK): ");
+ ret = scanf("%d", &security_type);
+ if (ret < 0)
+ return;
+
+ ret = wifi_aware_data_path_set_terminated_cb(g_ndp, _data_path_terminated_cb, NULL);
+ __print_result(ret, "wifi_aware_data_path_set_terminated_cb");
+
+ ret = wifi_aware_data_path_set_security(g_ndp, security_type);
+ __print_result(ret, "wifi_aware_data_path_set_security");
+
+ if (security_type == 1) {
+ char key[MAX_PSK_LEN + 1] = {0, };
+ printf("Passphrase: ");
+ ret = scanf("%63s", key);
+ if (ret < 0)
+ return;
+ ret = wifi_aware_data_path_set_psk(g_ndp, key);
+ __print_result(ret, "wifi_aware_data_path_set_psk");
+ }
+
+ if (security_type == 2) {
+ char key[PMK_LEN + 1] = {0, };
+ unsigned char pmk[PMK_LEN] = {0, };
+ printf("PMK: ");
+ ret = scanf("%32s", key);
+ if (ret < 0)
+ return;
+ memcpy(pmk, key, PMK_LEN);
+ ret = wifi_aware_data_path_set_pmk(g_ndp, pmk);
+ __print_result(ret, "wifi_aware_data_path_set_pmk");
+ }
+
+ if (g_session_type == 0) {
+ int port = -1;
+ printf("Port: ");
+ ret = scanf("%d", &port);
+ if (ret < 0)
+ return;
+ ret = wifi_aware_data_path_set_port(g_ndp, port);
+ __print_result(ret, "wifi_aware_data_set_port");
+ }
+
+ ret = wifi_aware_data_path_open(g_ndp, __open_cb, NULL);
+ __print_result(ret, "wifi_aware_data_path_open");
+}
+
+void test_close_data_path()
+{
+ int ret = WIFI_AWARE_ERROR_NONE;
+
+ RET_IF_LOOP_IS_NULL();
+ RET_IF_NDP_IS_NULL();
+
+ ret = wifi_aware_data_path_close(g_ndp);
+ __print_result(ret, "wifi_aware_data_path_close");
+
+ ret = wifi_aware_data_path_destroy(g_ndp);
+ __print_result(ret, "wifi_aware_data_path_destroy");
+}
+
+typedef void (*test_func)(void);
+test_func g_menu_func[] = {
+ [CMD_QUIT] = test_quit,
+ [CMD_FULL_MENU] = test_full_menu,
+ [CMD_INITIALIZE] = test_init,
+ [CMD_DEINITIALIZE] = test_deinit,
+ [CMD_ENABLE] = test_enable,
+ [CMD_DISABLE] = test_disable,
+ [CMD_PUBLISH] = test_publish,
+ [CMD_CANCEL_PUBLISH] = test_cancel_publish,
+ [CMD_UPDATE_PUBLISH] = test_update_publish,
+ [CMD_SUBSCRIBE] = test_subscribe,
+ [CMD_UPDATE_SUBSCRIBE] = test_update_subscribe,
+ [CMD_CANCEL_SUBSCRIBE] = test_cancel_subscribe,
+ [CMD_MESSAGE_RECV_CB] = test_message_recv_cb,
+ [CMD_SEND_MESSAGE] = test_send_message,
+ [CMD_OPEN_DATA_PATH] = test_open_data_path,
+
+ [CMD_TEST_SPECIFIC_INFO] = test_specific_info,
+
+ [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;
+
+ 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 if (n - 1 > 0 && n < 1024) {
+ buf[n - 1] = '\0'; /* remove new line... */
+ printf("\n\n");
+ __process_input(buf, user_data);
+ } else {
+ printf("invalid input\n");
+ }
+
+ 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;
+}
--- /dev/null
+/*
+ * Wi-Fi Aware Test Program
+ *
+ * Copyright (c) 2020 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 declares functions for testing Wi-Fi aware CAPIs.
+ *
+ * @file wifi-aware-test.h
+ * @author Jiung Yu (jiung.yu@samsung.com)
+ */
+#ifndef __WIFI_AWARE_TEST_H__
+#define __WIFI_AWARE_TEST_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __WIFI_AWARE_TEST_H__ */
--- /dev/null
+# Copyright (c) 2020 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.
+#
+# @file CMakeLists.txt
+#
+
+SET(WIFI_AWARE_MOCK "wifi-aware-mock")
+SET(WIFI_AWARE_MOCK_SRCS
+ mocks/wifi-aware-mock.c
+ mocks/wifi-aware-mock-gdbus-internal.c)
+
+INCLUDE_DIRECTORIES(
+ ${CMAKE_SOURCE_DIR}/include
+ ${CMAKE_SOURCE_DIR}/src/include
+ ${CMAKE_CURRENT_SOURCE_DIR}/mocks
+ )
+ADD_DEFINITIONS("-fvisibility=default")
+ADD_LIBRARY(${WIFI_AWARE_MOCK} SHARED ${WIFI_AWARE_MOCK_SRCS})
+
+
+SET(WIFI_AWARE_UNITTEST "wifi-aware-unittest")
+
+pkg_check_modules(TARGET_WIFI_AWARE_UNITTEST REQUIRED
+ capi-system-info
+ glib-2.0
+ gmock
+)
+INCLUDE_DIRECTORIES(
+ ${TARGET_WIFI_AWARE_UNITTEST_INCLUDE_DIRS}
+)
+LINK_DIRECTORIES(${TARGET_WIFI_AWARE_UNITTEST_LIBRARY_DIRS})
+
+FILE(GLOB UNITTEST_SRCS *.cpp)
+
+ADD_EXECUTABLE(${WIFI_AWARE_UNITTEST} ${UNITTEST_SRCS})
+TARGET_LINK_LIBRARIES(${WIFI_AWARE_UNITTEST}
+ ${TARGET_WIFI_AWARE}
+ ${TARGET_WIFI_AWARE_UNITTEST_LIBRARIES}
+)
--- /dev/null
+/*
+ * Copyright (c) 2020 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.
+ */
+
+#include <stdio.h>
+#include <glib.h>
+#include "wifi-aware-mock-gdbus-internal.h"
+
+typedef struct {
+ GAsyncReadyCallback callback;
+ GVariant *result;
+ gpointer user_data;
+} gdbus_result_data_s;
+
+static gdbus_result_data_s _gdbus_async_result;
+
+static gboolean __reply_async_method(gpointer data)
+{
+ gdbus_result_data_s *gdbus_result = (gdbus_result_data_s *)data;
+ gdbus_result->callback((GObject *)_get_dummy_gdbus_connection(),
+ NULL,
+ gdbus_result->user_data);
+ return FALSE;
+}
+
+static void __enable_handler(GVariant *parameters, GAsyncReadyCallback callback, gpointer user_data)
+{
+ // Always Success
+ _gdbus_async_result.callback = callback;
+ _gdbus_async_result.result = g_variant_new("(ii)", DUMMY_CLIENT_ID, WIFI_AWARE_ERROR_NONE);
+ _gdbus_async_result.user_data = user_data;
+ g_timeout_add(ASYNC_RESULT_DELAY, __reply_async_method, &_gdbus_async_result);
+}
+
+static GVariant *__disable_handler(GVariant *parameters)
+{
+ GVariant *reply = g_variant_new("(i)", WIFI_AWARE_ERROR_NONE);
+ return reply;
+}
+
+struct {
+ const gchar *interface_name;
+ const gchar *method_name;
+ // parameters is omitted
+ void(*async_handler)(GVariant *parameters, GAsyncReadyCallback callback, gpointer user_data);
+ GVariant *(*sync_handler)(GVariant *parameters);
+} wifi_aware_g_dbus_method_list[] = {
+ {
+ "net.nan.discovery",
+ "Enable",
+ __enable_handler,
+ NULL,
+ },
+ {
+ "net.nan.discovery",
+ "Disable",
+ NULL,
+ __disable_handler,
+ },
+ /*
+ {
+ "net.nan.discovery",
+ "Publish",
+ },
+ {
+ "net.nan.discovery",
+ "UpdatePublish",
+ },
+ {
+ "net.nan.discovery",
+ "CancelPublish",
+ },
+ */
+ {
+ NULL,
+ NULL,
+ NULL,
+ },
+};
+
+GDBusConnection *_get_dummy_gdbus_connection()
+{
+ return (GDBusConnection *)g_object_new(G_TYPE_DBUS_CONNECTION, NULL);
+}
+
+bool _check_g_dbus_bus_name(const gchar *bus_name)
+{
+ return g_strcmp0(bus_name, WIFI_AWARE_INTERFACE) == 0;
+}
+
+bool _check_g_dbus_object_path(const gchar *object_path)
+{
+ return g_strcmp0(object_path, WIFI_AWARE_DISCOVERY_PATH) == 0;
+}
+
+bool _check_g_dbus_interface_name(const gchar *interface_name)
+{
+ return g_strcmp0(interface_name, WIFI_AWARE_DISCOVERY_INTERFACE) == 0;
+}
+
+static bool __is_same_method(int i, const char *interface_name, const char *method_name)
+{
+ return (g_strcmp0(wifi_aware_g_dbus_method_list[i].interface_name, interface_name) == 0
+ && g_strcmp0(wifi_aware_g_dbus_method_list[i].method_name, method_name) == 0);
+}
+
+void _handle_async_method(const gchar *interface_name,
+ const char *method_name, GVariant *parameters,
+ GAsyncReadyCallback callback, gpointer user_data)
+{
+ for (int i = 0; ; ++i) {
+ if (wifi_aware_g_dbus_method_list[i].interface_name == NULL)
+ return;
+ if (__is_same_method(i, interface_name, method_name))
+ wifi_aware_g_dbus_method_list[i].async_handler(parameters, callback, user_data);
+ }
+}
+
+GVariant *_get_gdbus_async_result(GError **error)
+{
+ *error = NULL;
+ return _gdbus_async_result.result;
+}
+
+void _set_gdbus_error(gint code, const char *message, GError **error)
+{
+ *error = g_error_new_literal(G_DBUS_ERROR, code, message);
+}
+
+GVariant *_handle_sync_method(const gchar *interface_name,
+ const char *method_name, GVariant *parameters, GError **error)
+{
+ for (int i = 0; ; ++i) {
+ if (wifi_aware_g_dbus_method_list[i].interface_name == NULL) {
+ _set_gdbus_error(G_DBUS_ERROR_UNKNOWN_METHOD, "UnknownMethod", error);
+ return NULL;
+ }
+ if (__is_same_method(i, interface_name, method_name))
+ return wifi_aware_g_dbus_method_list[i].sync_handler(parameters);
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2020 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.
+ */
+
+#pragma once
+
+#include "wifi-aware-gdbus.h"
+
+#define ASYNC_RESULT_DELAY 500
+#define DUMMY_CLIENT_ID 1
+
+GDBusConnection *_get_dummy_gdbus_connection();
+bool _check_g_dbus_bus_name(const gchar *bus_name);
+bool _check_g_dbus_object_path(const gchar *object_path);
+bool _check_g_dbus_interface_name(const gchar *interface_name);
+void _handle_async_method(const gchar *interface_name,
+ const char *method_name, GVariant *parameters,
+ GAsyncReadyCallback callback, gpointer user_data);
+GVariant *_get_gdbus_async_result(GError **error);
+void _set_gdbus_error(gint code, const char *message, GError **error);
+GVariant *_handle_sync_method(const gchar *interface_name,
+ const char *method_name, GVariant *parameters, GError **error);
--- /dev/null
+/*
+ * Copyright (c) 2020 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.
+ */
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <system_info.h>
+
+#include "wifi-aware-mock-gdbus-internal.h"
+
+int system_info_get_platform_bool(const char *key, bool *value)
+{
+ *value = true;
+ return SYSTEM_INFO_ERROR_NONE;
+}
+
+bool _wifi_aware_check_permission()
+{
+ return true;
+}
+
+GDBusConnection *g_bus_get_sync(GBusType bus_type, GCancellable *cancellable, GError **error)
+{
+ return _get_dummy_gdbus_connection();
+}
+
+GCancellable *g_cancellable_new()
+{
+ return (GCancellable *)g_object_new(G_TYPE_CANCELLABLE, NULL);
+}
+
+void
+g_dbus_connection_call(GDBusConnection *connection,
+ const gchar *bus_name,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *method_name,
+ GVariant *parameters,
+ const GVariantType *reply_type,
+ GDBusCallFlags flags,
+ gint timeout_msec,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ if (!_check_g_dbus_bus_name(bus_name))
+ return;
+ if (!_check_g_dbus_object_path(object_path))
+ return;
+ if (!_check_g_dbus_interface_name(interface_name))
+ return;
+ _handle_async_method(interface_name, method_name, parameters, callback, user_data);
+}
+
+GVariant *g_dbus_connection_call_finish(GDBusConnection *connection,
+ GAsyncResult *res,
+ GError **error)
+{
+ return _get_gdbus_async_result(error);
+}
+
+GVariant *g_dbus_connection_call_sync(GDBusConnection *connection,
+ const gchar *bus_name,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *method_name,
+ GVariant *parameters,
+ const GVariantType *reply_type,
+ GDBusCallFlags flags,
+ gint timeout_msec,
+ GCancellable *cancellable,
+ GError **error)
+{
+ if (!_check_g_dbus_bus_name(bus_name)) {
+ _set_gdbus_error(G_DBUS_ERROR_UNKNOWN_METHOD, "UnknownMethod", error);
+ return NULL;
+ }
+ if (!_check_g_dbus_object_path(object_path)) {
+ _set_gdbus_error(G_DBUS_ERROR_UNKNOWN_METHOD, "UnknownMethod", error);
+ return NULL;
+ }
+ if (!_check_g_dbus_interface_name(interface_name)) {
+ _set_gdbus_error(G_DBUS_ERROR_UNKNOWN_METHOD, "UnknownMethod", error);
+ return NULL;
+ }
+ return _handle_sync_method(interface_name, method_name, parameters, error);
+}
--- /dev/null
+/*
+ * Copyright (c) 2020 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.
+ */
+
+#pragma once
+
+#include <gtest/gtest.h>
+#include <system_info.h>
+
+#include "wifi-aware.h"
+
+#define WIFI_AWARE_FEATURE "http://tizen.org/feature/network.wifi.aware"
+
+bool g_enabled;
+bool g_wifi_aware_supported;
+
+class WifiAware : public ::testing::Test
+{
+protected:
+ virtual void SetUp() {
+ g_enabled = false;
+ g_wifi_aware_supported = false;
+ system_info_get_platform_bool(WIFI_AWARE_FEATURE, &g_wifi_aware_supported);
+ wifi_aware_initialize();
+ }
+
+ virtual void TearDown() {
+ wifi_aware_deinitialize();
+ }
+
+};
--- /dev/null
+/*
+ * Copyright (c) 2019-2020 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.
+ */
+
+#include <gtest/gtest.h>
+
+int main(int argc, char **argv)
+{
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}
--- /dev/null
+/*
+ * Copyright (c) 2019-2020 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.
+ */
+
+#include "wifi-aware-unittest-util.h"
+
+GMainLoop *g_main_loop;
+guint g_timeout_id;
+
+gboolean _wifi_aware_callback_timeout(gpointer data)
+{
+ GMainLoop *main_loop = NULL;
+ main_loop = (GMainLoop *)data;
+ if (main_loop != NULL)
+ {
+ g_main_loop_quit(main_loop);
+ }
+ return false;
+}
+
+const char *_convert_error_to_string(wifi_aware_error_e error)
+{
+ switch (error) {
+ case WIFI_AWARE_ERROR_NONE:
+ return "NO ERROR";
+ 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";
+ case WIFI_AWARE_ERROR_ALREADY_ENABLED:
+ return "ALREADY_ENABLED";
+ case WIFI_AWARE_ERROR_REJECTED_BY_PEER:
+ return "REJECTED_BY_PEER";
+ case WIFI_AWARE_ERROR_INTERFACE_DOWN:
+ return "INTERFACE_IS_DOWN";
+ default:
+ return "UNKNOWN";
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2019-2020 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.
+ */
+
+#pragma once
+
+#include <glib.h>
+#include "wifi-aware.h"
+
+#define CHECK_WIFI_AWARE_SUPPORTED(ret) do { \
+ if (!g_wifi_aware_supported) { \
+ ASSERT_EQ(ret, WIFI_AWARE_ERROR_NOT_SUPPORTED) \
+ << "FAIL NOT_SUPPORTED"; \
+ } \
+ } while (0)
+
+#define CHECK_RESULT(ret, expected, message) do { \
+ CHECK_WIFI_AWARE_SUPPORTED(ret); \
+ ASSERT_EQ(ret, expected) << "FAIL " << message; \
+ } while (0)
+
+#define CALLBACK_TIMEOUT 20000
+#define RUN_GMAIN_LOOP do { \
+ g_main_loop = g_main_loop_new(NULL, false); \
+ g_timeout_id = g_timeout_add(CALLBACK_TIMEOUT, _wifi_aware_callback_timeout, g_main_loop); \
+ g_main_loop_run(g_main_loop); \
+ g_source_remove(g_timeout_id); \
+ g_main_loop = NULL; \
+ } while (0)
+
+#define QUIT_GMAIN_LOOP do { \
+ if (g_main_loop) { \
+ g_main_loop_quit(g_main_loop); \
+ g_main_loop = NULL; \
+ } \
+ } while (0)
+
+gboolean _wifi_aware_callback_timeout(gpointer data);
+const char *_convert_error_to_string(wifi_aware_error_e error);
--- /dev/null
+/*
+ * Copyright (c) 2019-2020 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.
+ */
+
+#include <iostream>
+#include <gtest/gtest.h>
+
+#include "wifi-aware-unittest-fixture.h"
+#include "wifi-aware-unittest-util.h"
+#include "wifi-aware.h"
+
+extern GMainLoop *g_main_loop;
+extern guint g_timeout_id;
+
+void __enabled_cb(wifi_aware_error_e error, void *user_data)
+{
+ if (error == WIFI_AWARE_ERROR_NONE) {
+ // LOGI("Successfully Enabled");
+ g_enabled = true;
+ }
+ else {
+ // LOGE("Fail to enable %s(%d)", _wifi_aware_error_to_string(error), error);
+ }
+ QUIT_GMAIN_LOOP;
+}
+
+TEST_F(WifiAware, wifi_aware_initialize_p)
+{
+ int ret = 0;
+
+ wifi_aware_deinitialize();
+
+ ret = wifi_aware_initialize();
+ CHECK_RESULT(ret, WIFI_AWARE_ERROR_NONE, "wifi_aware_initialize");
+}
+
+TEST_F(WifiAware, wifi_aware_initialize_n)
+{
+ int ret = 0;
+ ret = wifi_aware_initialize();
+ CHECK_RESULT(ret, WIFI_AWARE_ERROR_ALREADY_INITIALIZED, "wifi_aware_initialize");
+}
+
+TEST_F(WifiAware, wifi_aware_deinitialize_p)
+{
+ int ret = 0;
+ ret = wifi_aware_deinitialize();
+ CHECK_RESULT(ret, WIFI_AWARE_ERROR_NONE, "wifi_aware_deinitialize");
+}
+
+TEST_F(WifiAware, wifi_aware_deinitialize_n)
+{
+ int ret = 0;
+ wifi_aware_deinitialize();
+
+ ret = wifi_aware_deinitialize();
+ CHECK_RESULT(ret, WIFI_AWARE_ERROR_NOT_INITIALIZED, "wifi_aware_deinitialize");
+}
+
+TEST_F(WifiAware, wifi_aware_enable_p)
+{
+ int ret = 0;
+
+ ret = wifi_aware_enable(__enabled_cb, NULL);
+ CHECK_RESULT(ret, WIFI_AWARE_ERROR_NONE, "wifi_aware_enable");
+
+ RUN_GMAIN_LOOP;
+
+ ASSERT_TRUE(g_enabled) << "FAIL Enabled";
+
+ wifi_aware_disable();
+}
+
+TEST_F(WifiAware, wifi_aware_enable_n)
+{
+ int ret = 0;
+
+ wifi_aware_enable(__enabled_cb, NULL);
+ RUN_GMAIN_LOOP;
+
+ ret = wifi_aware_enable(__enabled_cb, NULL);
+ CHECK_RESULT(ret, WIFI_AWARE_ERROR_ALREADY_ENABLED, "wifi_aware_enable");
+
+ wifi_aware_disable();
+}
+
+TEST_F(WifiAware, wifi_aware_disable_p)
+{
+ int ret = 0;
+
+ wifi_aware_enable(__enabled_cb, NULL);
+ RUN_GMAIN_LOOP;
+
+ ret = wifi_aware_disable();
+ CHECK_RESULT(ret, WIFI_AWARE_ERROR_NONE, "wifi_aware_disable");
+
+ wifi_aware_disable();
+}
+
+TEST_F(WifiAware, wifi_aware_disable_n)
+{
+ int ret = 0;
+
+ ret = wifi_aware_disable();
+ CHECK_RESULT(ret, WIFI_AWARE_ERROR_INVALID_OPERATION, "wifi_aware_disable");
+}
+++ /dev/null
-<manifest>
- <request>
- <domain name="_" />
- </request>
-</manifest>
\ No newline at end of file