From: hyunuk.tak Date: Fri, 18 Mar 2022 06:20:50 +0000 (+0900) Subject: Move test files to thread-test folder X-Git-Tag: accepted/tizen/unified/20220914.164046~112 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c0d1ef35d7543fbbc15af8cb1a7c2485fa9d0e3f;p=platform%2Fcore%2Fapi%2Fthread.git Move test files to thread-test folder Change-Id: Iaccf336e0d02b79c718f961e089c51215758206a Signed-off-by: hyunuk.tak --- diff --git a/CMakeLists.txt b/CMakeLists.txt index a9da0bc..59a94cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,10 +74,7 @@ CONFIGURE_FILE( ) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) -#ADD_SUBDIRECTORY(tests/test) -#ADD_SUBDIRECTORY(tests/unittest) - -ADD_SUBDIRECTORY(tests) +ADD_SUBDIRECTORY(tests/thread-test) IF(UNIX) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt deleted file mode 100755 index ad99a19..0000000 --- a/tests/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -SET(test thread-test) - -INCLUDE(FindPkgConfig) -pkg_check_modules(${test} REQUIRED - glib-2.0 -) - -FOREACH(flag ${${test}_CFLAGS}) - SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") -ENDFOREACH(flag) - -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -Werror") - -SET(TEST_SRCS - thread-init.c - thread-menu.c - thread-main.c - thread-core.c - thread-util.c - thread-joiner.c - thread-commissioner.c - thread-network.c - thread-br.c - thread-srp.c - thread-scenarios.c -) - -SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") -ADD_EXECUTABLE(${test} ${TEST_SRCS}) -TARGET_LINK_LIBRARIES(${test} ${PROJECT_NAME} ${test_LDFLAGS}) -INSTALL(TARGETS ${test} DESTINATION bin) - diff --git a/tests/thread-br.c b/tests/thread-br.c deleted file mode 100644 index 8c40d19..0000000 --- a/tests/thread-br.c +++ /dev/null @@ -1,520 +0,0 @@ -/* - * Copyright (c) 2021 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 -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "thread.h" -#include "thread-menu.h" -#include "thread-type.h" -#include "thread-log.h" -#include "thread-util.h" - -/* External route related variables */ -char g_str_ext_ipv6_prefix[MENU_DATA_SIZE + 1]; -char g_str_ext_ipv6_prefix_len[MENU_DATA_SIZE + 1]; -char g_str_ext_rloc16[MENU_DATA_SIZE + 1]; -char g_str_ext_preference[MENU_DATA_SIZE + 1] = {"1"}; -char g_str_ext_is_stable[MENU_DATA_SIZE + 1] = {"1"}; - -uint8_t g_ext_ipv6_prefix[THREAD_IPV6_ADDRESS_SIZE]; -uint8_t g_ext_ipv6_prefix_len; -uint16_t g_ext_rloc16; -int8_t g_ext_preference; -bool g_ext_is_stable; -bool g_ext_is_device_next_hop = THREAD_IS_DEVICE_NEXT_HOP_DEFAULT; - -/* Onmesh prefix related variables */ -char g_str_onmesh_ipv6_prefix[MENU_DATA_SIZE + 1]; -char g_str_onmesh_ipv6_prefix_len[MENU_DATA_SIZE + 1]; -char g_str_onmesh_preference[MENU_DATA_SIZE + 1] = {"1"}; -char g_str_slaac[MENU_DATA_SIZE + 1] = {"1"}; -char g_str_dhcp[MENU_DATA_SIZE + 1] = {"1"}; -char g_str_configure[MENU_DATA_SIZE + 1] = {"1"}; -char g_str_default_route[MENU_DATA_SIZE + 1] = {"1"}; -char g_str_on_mesh[MENU_DATA_SIZE + 1] = {"1"}; -char g_str_stable[MENU_DATA_SIZE + 1] = {"1"}; -char g_str_preferred[MENU_DATA_SIZE + 1] = {"1"}; - -int g_slaac; -int g_dhcp; -int g_configure; -int g_default_route; -int g_on_mesh; -int g_stable; -uint8_t g_onmesh_ipv6_prefix[THREAD_IPV6_ADDRESS_SIZE]; -uint8_t g_onmesh_ipv6_prefix_len; -int8_t g_onmesh_preference; -int g_preferred; - -GSList *route_list; -GSList *onmesh_prefix_list; - -void free_list(GSList *list) -{ - for (GSList *l = list; l; l = g_slist_next(l)) - if (l->data) - g_free(l->data); - - return; -} - -thread_route_info_a* _create_new_route(void) -{ - FUNC_ENTRY; - - thread_route_info_a *new_route = NULL; - new_route = g_malloc0(sizeof(thread_route_info_a)); - if (!new_route) { - THREAD_ERR("g_malloc0 failed while allocating new route"); - return NULL; - } - - FUNC_EXIT; - return new_route; -} - -thread_onmesh_prefix_info_a* _create_new_onmesh_prefix(void) -{ - FUNC_ENTRY; - - thread_onmesh_prefix_info_a *new_onmesh_prefix = NULL; - new_onmesh_prefix = g_malloc0(sizeof(thread_onmesh_prefix_info_a)); - if (!new_onmesh_prefix) { - THREAD_ERR("g_malloc0 failed while allocating new_onmesh_prefix"); - return NULL; - } - - FUNC_EXIT; - return new_onmesh_prefix; -} - -static bool __thread_br_get_external_routes_callback(int total, thread_route_info_h route_info, void *user_data) -{ - msg("__thread_br_get_external_routes_callback, result: %d", total); - - msg("Found the external route..."); - - int ret = thread_br_get_route_info(route_info, - g_ext_ipv6_prefix, &g_ext_ipv6_prefix_len, &g_ext_rloc16, - &g_ext_preference, &g_ext_is_stable, &g_ext_is_device_next_hop); - - if (ret == THREAD_ERROR_NONE) { - msg("thread_br_get_route_info success"); - uint8_t *prefix_address = g_ext_ipv6_prefix; - - msg("prefix_address: %02x%02x:%02x%02x:%02x%02x:%02x%02x", - prefix_address[0], prefix_address[1], prefix_address[2], - prefix_address[3], prefix_address[4], prefix_address[5], - prefix_address[6], prefix_address[7]); - msg("prefix_len: %u", g_ext_ipv6_prefix_len); - msg("rloc16: %u", g_ext_rloc16); - msg("preference: %u", g_ext_preference); - msg("stable: %s", g_ext_is_stable ? "TRUE" : "FALSE"); - msg("next_hop_is_self: %s", g_ext_is_device_next_hop ? - "TRUE" : "FALSE"); - - /* Create a new route and add to route_list */ - thread_route_info_a *new_route = _create_new_route(); - if (!new_route) - goto OUT; - - new_route->handle = route_info; - memcpy(new_route->prefix_address, prefix_address, - THREAD_IPV6_PREFIX_SIZE); - new_route->prefix_length = g_ext_ipv6_prefix_len; - - route_list = g_slist_append(route_list, new_route); - - } else - msg("thread_br_get_route_info failed"); -OUT: - - msg("Callback exit"); - - return TRUE; -} - -static int run_thread_br_enable(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_br_enable(g_instance); - if (ret == THREAD_ERROR_NONE) { - msg("thread_br_enable success"); - free_list(route_list); - route_list = NULL; - free_list(onmesh_prefix_list); - onmesh_prefix_list = NULL; - - /* Initialize the route_list */ - ret = thread_br_get_external_routes(g_instance, - __thread_br_get_external_routes_callback, NULL); - retv_if(ret != THREAD_ERROR_NONE, ret); - } else - msg("thread_br_enable failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_br_disable(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_br_disable(g_instance); - - if (ret == THREAD_ERROR_NONE) { - msg("thread_br_disable success"); - free_list(route_list); - route_list = NULL; - free_list(onmesh_prefix_list); - onmesh_prefix_list = NULL; - - } else - msg("thread_br_disable failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_br_get_external_routes(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - free_list(route_list); - route_list = NULL; - int ret = thread_br_get_external_routes(g_instance, - __thread_br_get_external_routes_callback, NULL); - if (ret == THREAD_ERROR_NONE) - msg("thread_br_get_external_routes success"); - else - msg("thread_br_get_external_routes failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_br_add_external_route(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int is_stable; - int ret = _preprocess_ipv6_prefix(g_str_ext_ipv6_prefix, - g_ext_ipv6_prefix); - retv_if(ret != THREAD_ERROR_NONE, ret); - - sscanf(g_str_ext_ipv6_prefix_len, "%hhu", &g_ext_ipv6_prefix_len); - sscanf(g_str_ext_rloc16, "%hu", &g_ext_rloc16); - sscanf(g_str_ext_preference, "%hhd", &g_ext_preference); - sscanf(g_str_ext_is_stable, "%d", &is_stable); - g_ext_is_stable = (bool)is_stable; - - /* Printing parameters value */ - msg("Parameter value for add external route"); - - uint8_t *prefix_address = g_ext_ipv6_prefix; - msg("prefix_address: %02x%02x:%02x%02x:%02x%02x:%02x%02x::", - prefix_address[0], prefix_address[1], prefix_address[2], - prefix_address[3], prefix_address[4], prefix_address[5], - prefix_address[6], prefix_address[7]); - - msg("external__prefix_len: %hhu", g_ext_ipv6_prefix_len); - msg("external_preference: %hhd", g_ext_preference); - msg("external_rloc16: %hhd", g_ext_rloc16); - msg("external_is_stable: %s", g_ext_is_stable ? "TRUE" : "FALSE"); - - thread_route_info_a *new_route = _create_new_route(); - if (!new_route) - goto OUT; - - ret = thread_br_add_external_route(g_instance, g_ext_ipv6_prefix, - g_ext_ipv6_prefix_len, g_ext_rloc16, g_ext_preference, - g_ext_is_stable, g_ext_is_device_next_hop, - &(new_route->handle)); - - if (ret == THREAD_ERROR_NONE) { - msg("thread_br_add_external_route success"); - memcpy(new_route->prefix_address, prefix_address, - THREAD_IPV6_PREFIX_SIZE); - new_route->prefix_length = g_ext_ipv6_prefix_len; - route_list = g_slist_append(route_list, new_route); - } else { - msg("thread_br_add_external_route failed"); - g_free(new_route); - } -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_br_remove_external_route(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - - if (g_instance == NULL) - goto OUT; - - int ret = _preprocess_ipv6_prefix(g_str_ext_ipv6_prefix, - g_ext_ipv6_prefix); - retv_if(ret != THREAD_ERROR_NONE, ret); - sscanf(g_str_ext_ipv6_prefix_len, "%hhu", &g_ext_ipv6_prefix_len); - - thread_route_info_a *req_route = NULL; - - for (GSList *l = route_list; l; l = g_slist_next(l)) { - thread_route_info_a *route_info = l->data; - if (memcmp(route_info->prefix_address, g_ext_ipv6_prefix, - THREAD_IPV6_PREFIX_SIZE*(sizeof(uint8_t))) == 0) { - - if (route_info->prefix_length == g_ext_ipv6_prefix_len) { - req_route = route_info; - break; - } - } - } - - if (!req_route) { - msg("No Route found"); - goto OUT; - } - - ret = thread_br_remove_external_route(g_instance, req_route->handle); - if (ret == THREAD_ERROR_NONE) { - msg("thread_br_remove_external_route success"); - route_list = g_slist_remove(route_list, req_route); - } else - msg("thread_br_remove_external_route failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_br_add_onmesh_prefix(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = _preprocess_ipv6_prefix(g_str_onmesh_ipv6_prefix, - g_onmesh_ipv6_prefix); - retv_if(ret != THREAD_ERROR_NONE, ret); - - sscanf(g_str_onmesh_ipv6_prefix_len, "%hhu", &g_onmesh_ipv6_prefix_len); - sscanf(g_str_onmesh_preference, "%hhd", &g_onmesh_preference); - sscanf(g_str_slaac, "%d", &g_slaac); - sscanf(g_str_dhcp, "%d", &g_dhcp); - sscanf(g_str_configure, "%d", &g_configure); - sscanf(g_str_default_route, "%d", &g_default_route); - sscanf(g_str_on_mesh, "%d", &g_on_mesh); - sscanf(g_str_stable, "%d", &g_stable); - sscanf(g_str_preferred, "%d", &g_preferred); - - /* Printing parameters value */ - msg("Parameter value for add onmesh_prefix"); - - uint8_t *prefix_address = g_onmesh_ipv6_prefix; - msg("prefix_address: %02x%02x:%02x%02x:%02x%02x:%02x%02x", - prefix_address[0], prefix_address[1], prefix_address[2], - prefix_address[3], prefix_address[4], prefix_address[5], - prefix_address[6], prefix_address[7]); - - msg("g_onmesh_prefix_len: %hhu", g_onmesh_ipv6_prefix_len); - msg("g_onmesh_preference: %hhd", g_onmesh_preference); - msg("g_slaac: %s", g_slaac ? "TRUE" : "FALSE"); - msg("g_dhcp: %s", g_dhcp ? "TRUE" : "FALSE"); - msg("g_configure: %s", g_configure ? "TRUE" : "FALSE"); - msg("g_default_route: %s", g_default_route ? "TRUE" : "FALSE"); - msg("g_on_mesh: %s", g_on_mesh ? "TRUE" : "FALSE"); - msg("g_stable: %s", g_stable ? "TRUE" : "FALSE"); - msg("g_preferred: %s", g_preferred ? "TRUE" : "FALSE"); - - thread_onmesh_prefix_info_a *new_onmesh_prefix = _create_new_onmesh_prefix(); - if (!new_onmesh_prefix) - goto OUT; - - ret = thread_br_add_onmesh_prefix(g_instance, g_onmesh_ipv6_prefix, - g_onmesh_ipv6_prefix_len, g_onmesh_preference, - g_preferred, g_slaac, g_dhcp, g_configure, - g_default_route, g_on_mesh, g_stable, - &(new_onmesh_prefix->handle)); - - if (ret == THREAD_ERROR_NONE) { - msg("thread_br_add_onmesh_prefix success"); - memcpy(new_onmesh_prefix->prefix_address, prefix_address, - THREAD_IPV6_PREFIX_SIZE); - new_onmesh_prefix->prefix_length = g_onmesh_ipv6_prefix_len; - onmesh_prefix_list = g_slist_append(onmesh_prefix_list, - new_onmesh_prefix); - } else { - msg("thread_br_add_onmesh_prefix failed"); - g_free(new_onmesh_prefix); - } -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_br_remove_onmesh_prefix(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = _preprocess_ipv6_prefix(g_str_onmesh_ipv6_prefix, - g_onmesh_ipv6_prefix); - retv_if(ret != THREAD_ERROR_NONE, ret); - sscanf(g_str_onmesh_ipv6_prefix_len, "%hhu", &g_onmesh_ipv6_prefix_len); - - thread_onmesh_prefix_info_a *req_onmesh_prefix = NULL; - - for (GSList *l = route_list; l; l = g_slist_next(l)) { - thread_onmesh_prefix_info_a *onmesh_prefix_info = l->data; - if (memcmp(onmesh_prefix_info->prefix_address, - g_onmesh_ipv6_prefix, - THREAD_IPV6_PREFIX_SIZE*(sizeof(uint8_t))) == 0) { - - if (onmesh_prefix_info->prefix_length == - g_onmesh_ipv6_prefix_len) { - req_onmesh_prefix = onmesh_prefix_info; - break; - } - } - } - - if (!req_onmesh_prefix) { - msg("No Onmesh Prefix found"); - goto OUT; - } - - ret = thread_br_remove_onmesh_prefix(g_instance, - req_onmesh_prefix->handle); - if (ret == THREAD_ERROR_NONE) { - msg("thread_br_remove_onmesh_prefix success"); - onmesh_prefix_list = g_slist_remove(onmesh_prefix_list, - req_onmesh_prefix); - } else - msg("thread_br_remove_onmesh_prefix failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static struct menu_data menu_thread_br_add_external_route[] = { - { "1", "ext_ipv6_prefix", - NULL, NULL, g_str_ext_ipv6_prefix }, - { "2", "ext_ipv6_prefix_len", - NULL, NULL, g_str_ext_ipv6_prefix_len }, - { "3", "ext_rloc16", - NULL, NULL, g_str_ext_rloc16 }, - { "4", "ext_preference", - NULL, NULL, g_str_ext_preference }, - { "5", "is_stable[ Type 0:false, 1:true]", - NULL, NULL, g_str_ext_is_stable }, - { "6", "run", NULL, - run_thread_br_add_external_route, NULL }, - { NULL, NULL, }, -}; - -static struct menu_data menu_thread_br_remove_external_route[] = { - { "1", "ext_ipv6_prefix", - NULL, NULL, g_str_ext_ipv6_prefix }, - { "2", "ext_ipv6_prefix_len", - NULL, NULL, g_str_ext_ipv6_prefix_len }, - { "3", "run", NULL, - run_thread_br_remove_external_route, NULL }, - { NULL, NULL, }, -}; - - -static struct menu_data menu_thread_br_add_onmesh_prefix[] = { - { "1", "onmesh_ipv6_prefix", - NULL, NULL, g_str_onmesh_ipv6_prefix }, - { "2", "onmesh_ipv6_prefix_len", - NULL, NULL, g_str_onmesh_ipv6_prefix_len }, - { "3", "onmesh_prefernce [Type 0:Low, 1:Med, 2:High]", - NULL, NULL, g_str_onmesh_preference }, - { "4", "slaac [Type 0:False, 1:True]", - NULL, NULL, g_str_slaac }, - { "5", "dhcp [Type 0:False, 1:True]", - NULL, NULL, g_str_dhcp }, - { "6", "configure [Type 0:False, 1:True]", - NULL, NULL, g_str_configure }, - { "7", "default_route [Type 0:False, 1:True]", - NULL, NULL, g_str_default_route }, - { "8", "on_mesh [Type 0:False, 1:True]", - NULL, NULL, g_str_on_mesh }, - { "9", "stable [Type 0:False, 1:True]", - NULL, NULL, g_str_stable }, - { "10", "preferred [Type 0:False, 1:True]", - NULL, NULL, g_str_preferred }, - { "11", "run", NULL, - run_thread_br_add_onmesh_prefix, NULL }, - { NULL, NULL, }, -}; - -static struct menu_data menu_thread_br_remove_onmesh_prefix[] = { - { "1", "onmesh_ipv6_prefix", - NULL, NULL, g_str_onmesh_ipv6_prefix }, - { "2", "onmesh_prefix_len", - NULL, NULL, g_str_onmesh_ipv6_prefix_len }, - { "3", "run", NULL, - run_thread_br_remove_onmesh_prefix, NULL }, - { NULL, NULL, }, -}; - -struct menu_data menu_thread_br[] = { - { "1", "thread_br_enable", - NULL, run_thread_br_enable, NULL}, - { "2", "thread_br_disable", - NULL, run_thread_br_disable, NULL }, - { "3", "thread_br_get_external_routes", - NULL, run_thread_br_get_external_routes, NULL }, - { "4", "thread_br_add_external_route", - menu_thread_br_add_external_route, NULL, NULL }, - { "5", "thread_br_remove_external_route", - menu_thread_br_remove_external_route, NULL, NULL }, - { "6", "thread_br_add_onmesh_prefix", - menu_thread_br_add_onmesh_prefix, NULL, NULL }, - { "7", "thread_br_remove_onmesh_prefix", - menu_thread_br_remove_onmesh_prefix, NULL, NULL }, - { NULL, NULL, }, -}; diff --git a/tests/thread-commissioner.c b/tests/thread-commissioner.c deleted file mode 100644 index e2baf2b..0000000 --- a/tests/thread-commissioner.c +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (c) 2021 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 -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "thread.h" -#include "thread-menu.h" -#include "thread-type.h" -#include "thread-log.h" -#include "thread-util.h" - -static char g_joiner_uuid[MENU_DATA_SIZE + 1]; -static char g_joiner_passphrase[MENU_DATA_SIZE + 1]; - -static int run_thread_commissioner_start(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_commissioner_start(g_instance); - if (ret == THREAD_ERROR_NONE) - msg("thread_commissioner_start success"); - else - msg("thread_commissioner_start failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_commissioner_set_commisioning_data(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_commissioner_set_commisioning_data(g_instance, g_joiner_uuid, - g_joiner_passphrase); - if (ret == THREAD_ERROR_NONE) - msg("thread_commissioner_set_commisioning_data success"); - else - msg("thread_commissioner_set_commisioning_data failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static struct menu_data menu_thread_commissioner_set_commisioning_data[] = { - { "1", "Joiner UUID", NULL, NULL, g_joiner_uuid}, - { "2", "Joiner Passphrase", NULL, NULL, g_joiner_passphrase}, - { "3", "run", NULL, run_thread_commissioner_set_commisioning_data, NULL}, - { NULL, NULL, }, -}; - -struct menu_data menu_thread_commissioner[] = { - { "1", "thread_commissioner_start", - NULL, run_thread_commissioner_start, NULL}, - { "2", "thread_commissioner_set_commisioning_data", - menu_thread_commissioner_set_commisioning_data, NULL, NULL }, - { NULL, NULL, }, -}; diff --git a/tests/thread-core.c b/tests/thread-core.c deleted file mode 100755 index 7d2d987..0000000 --- a/tests/thread-core.c +++ /dev/null @@ -1,394 +0,0 @@ -/* - * Copyright (c) 2021 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 -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "thread.h" -#include "thread-menu.h" -#include "thread-type.h" -#include "thread-log.h" -#include "thread-util.h" - -thread_device_role_e g_device_role; -thread_device_type_e g_device_type; -char g_str_device_type[MENU_DATA_SIZE + 1]; - -char g_str_scan_duration[MENU_DATA_SIZE + 1]; -uint16_t g_scan_duration; - -uint64_t g_extended_address; -thread_scan_param_h g_param_handle; -const char* g_uuid; - -static void __thread_scan_callback(int result, thread_network_scanning_state_e state, - uint64_t ext_address, const char *network_name, uint64_t ext_panidi, - const uint8_t *steering_data, int length, uint16_t panid, uint16_t joiner_udp_port, uint16_t channel, - uint16_t rssi, uint8_t lqi, uint8_t version, bool is_native, bool is_joinable, void *user_data) { - - msg("Thread scan callback, result: %s", _thread_get_error_message(result)); - - if (state == THREAD_SCANNING_STARTED) - msg("The scanning state is: STARTED"); - else if (state == THREAD_SCANNING_FINISHED) - msg("The scanning state is: STOPPED"); - else { - msg("thread_network_scanning_state: [%d]", state); - msg("network_name: [%s]", network_name); - msg("ext_address: [%zu]", (size_t)ext_address); - msg("ext_panidi: [%zu]", (size_t)ext_panidi); - msg("steering_data: [%s]", steering_data); - msg("length: [%d]", length); - msg("panid: [%hu]", panid); - msg("joiner_udp_port: [%hu]", joiner_udp_port); - msg("channel: [%hu]", channel); - msg("rssi: [%hu]", rssi); - msg("lqi: [%hu]", lqi); - msg("version: [%hu]", version); - msg("is_native: [%d]", is_native); - msg("is_joinable: [%d]", is_joinable); - } - - msg("Callback exit"); - - return; -} - -void thread_device_role_callback(thread_device_role_e device_role, void *user_data) -{ - FUNC_ENTRY; - THREAD_DBG("Device Role is [%d]", device_role); - FUNC_EXIT; -} - - -static int run_thread_enable(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h *g_instance = &(mm->t_instance); - - int ret = thread_enable(g_instance); - if (ret == THREAD_ERROR_NONE) - msg("successfully enabled"); - else - msg("enabled failed"); - thread_set_device_role_changed_cb(g_instance, thread_device_role_callback, NULL); - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_disable(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_disable(g_instance); - if (ret == THREAD_ERROR_NONE) - msg("successfully disabled"); - else - msg("disabled failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_start(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_start(g_instance); - if (ret == THREAD_ERROR_NONE) - msg("successfully started"); - else - msg("start failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_stop(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_stop(g_instance); - if (ret == THREAD_ERROR_NONE) - msg("successfully stopped"); - else - msg("stop failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_reset(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_reset(g_instance); - if (ret == THREAD_ERROR_NONE) - msg("successfully reset"); - else - msg("reset failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_factory_reset(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_factoryreset(g_instance); - if (ret == THREAD_ERROR_NONE) - msg("factoryreset success"); - else - msg("factoryreset failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_get_device_role(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_get_device_role(g_instance, &g_device_role); - if (ret == THREAD_ERROR_NONE) - msg("thread_get_device_role success"); - else - msg("thread_get_device_role failed"); - - msg("- thread_get_device_role() ret: Device role->[%s]", _thread_device_role_to_string(g_device_role)); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_get_device_type(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_get_device_type(g_instance, &g_device_type); - if (ret == THREAD_ERROR_NONE) - msg("thread_get_device_type success"); - else - msg("thread_get_device_type failed"); - - msg("-thread_get_device_type() ret: Device type->[%s]", _thread_device_type_to_string(g_device_type)); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_set_device_type(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - g_device_type = (thread_device_type_e)atoi(g_str_device_type); - - int ret = thread_set_device_type(g_instance, g_device_type); - if (ret == THREAD_ERROR_NONE) - msg("thread_get_device_type success"); - else - msg("thread_get_device_type failed"); - - msg("- thread_set_device_type() ret: Device type->[%s]", _thread_device_type_to_string(g_device_type)); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_scan_param_create(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - g_scan_duration = (uint16_t)atoi(g_str_scan_duration); - - int ret = thread_scan_param_create(g_instance, g_scan_duration, &g_param_handle); - if (ret == THREAD_ERROR_NONE) - msg("thread_scan_param_create success"); - else - msg("thread_scan_param_create failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_scan_param_destroy(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_scan_param_destroy(g_instance, g_param_handle); - if (ret == THREAD_ERROR_NONE) - msg("thread_scan_param_distroy success"); - else - msg("thread_scan_param_distroy failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_scan(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = THREAD_ERROR_NONE; - - if (!g_param_handle) { - ret = thread_scan_param_create(g_instance, THREAD_DEFAULT_SCAN_TIME, &g_param_handle); - if (ret == THREAD_ERROR_NONE) - msg("thread_scan_param_create success"); - else { - msg("thread_scan_param_create failed"); - goto OUT; - } - } - - ret = thread_scan(g_instance, g_param_handle, __thread_scan_callback, NULL); - if (ret == THREAD_ERROR_NONE) - msg("thread_scan success"); - else - msg("thread_scan failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_get_extended_address(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_get_extended_address(g_instance, &g_extended_address); - if (ret == THREAD_ERROR_NONE) { - msg("thread_get_extended_address success"); - msg("- thread_get_exenteded_address() ret: Device extended address->[%zu]", - (size_t)g_extended_address); - } else - msg("thread_get_extended_address failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_get_extended_uuid(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_get_extended_uuid(g_instance, &g_uuid); - if (ret == THREAD_ERROR_NONE) { - msg("thread_get_extended_uuid success"); - msg("- thread_get_exenteded_uuid() ret: Device extended uuid->[%s]", g_uuid); - } else - msg("thread_get_extended_uuid failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static struct menu_data menu_thread_set_device_type[] = { - { "1", "type(1: Router 2: FED(full end device) 3: MED(minimal end device) 4: SED(sleepy end device)", - NULL, NULL, g_str_device_type }, - { "2", "run", NULL, - run_thread_set_device_type, NULL }, - { NULL, NULL, }, -}; - -static struct menu_data menu_thread_scan_param_create[] = { - { "1", "duration", - NULL, NULL, g_str_scan_duration }, - { "2", "run", NULL, - run_thread_scan_param_create, NULL }, - { NULL, NULL, }, -}; - -struct menu_data menu_thread_core[] = { - { "1", "thread_enable", - NULL, run_thread_enable, NULL}, - { "2", "thread_disable", - NULL, run_thread_disable, NULL }, - { "3", "thread_start", - NULL, run_thread_start, NULL }, - { "4", "thread_stop", - NULL, run_thread_stop, NULL }, - { "5", "thread_reset", - NULL, run_thread_reset, NULL }, - { "6", "thread_factoryreset", - NULL, run_factory_reset, NULL }, - { "7", "thread_get_device_role", - NULL, run_thread_get_device_role, NULL }, - { "8", "thread_get_device_type", - NULL, run_thread_get_device_type, NULL }, - { "9", "thread_set_device_type", - menu_thread_set_device_type, NULL, NULL }, - { "10", "thread_scan_param_create", - menu_thread_scan_param_create, NULL, NULL }, - { "11", "thread_scan_param_destroy", - NULL, run_thread_scan_param_destroy, NULL }, - { "12", "thread_scan", - NULL, run_thread_scan, NULL }, - { "13", "thread_get_extended_address", - NULL, run_thread_get_extended_address, NULL }, - { "14", "thread_get_extended_uuid", - NULL, run_thread_get_extended_uuid, NULL }, - { NULL, NULL, }, -}; diff --git a/tests/thread-init.c b/tests/thread-init.c deleted file mode 100755 index a8b2733..0000000 --- a/tests/thread-init.c +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2021 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 -#include -#include - -#include -#include - -#include "thread.h" -#include "thread-menu.h" - -char g_selected_user_idx[MENU_DATA_SIZE + 1] = {0,}; - - -static int run_thread_init(MManager *mm, struct menu_data *menu) -{ - msg("thread_initialize started..."); - - int ret = thread_initialize(); - - if (ret == THREAD_ERROR_NOT_SUPPORTED) - msg("thread_initialize_completed"); - else - msg("thread_initalize_failed"); - - return RET_SUCCESS; -} - -struct menu_data menu_thread_init[] = { - { "1", "Run Init", NULL, run_thread_init, NULL}, - { NULL, NULL, }, -}; diff --git a/tests/thread-joiner.c b/tests/thread-joiner.c deleted file mode 100644 index c71e0c3..0000000 --- a/tests/thread-joiner.c +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (c) 2021 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 -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "thread.h" -#include "thread-menu.h" -#include "thread-type.h" -#include "thread-log.h" -#include "thread-util.h" - -static char g_network_key[MENU_DATA_SIZE + 1]; -static char g_pskd[MENU_DATA_SIZE + 1]; -static char g_prov_url[MENU_DATA_SIZE + 1]; -static char g_vendor_name[MENU_DATA_SIZE + 1]; -static char g_vendor_model[MENU_DATA_SIZE + 1]; -static char g_vendor_sw_version[MENU_DATA_SIZE + 1]; -static char g_vendor_data[MENU_DATA_SIZE + 1]; -static char g_panid[MENU_DATA_SIZE + 1]; - -static void __thread_joiner_start_callback(int result, void* user_data) -{ - msg("Thread joiner start callback, result: %s", _thread_get_error_message(result)); - - return; -} -static int run_thread_joiner_join_by_network_key(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_joiner_join_by_network_key(g_instance, g_network_key, g_panid); - if (ret == THREAD_ERROR_NONE) - msg("thread_joiner_join_by_networkkey success"); - else - msg("thread_joiner_join_by_networkkey failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_joiner_stop(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_joiner_stop(g_instance); - if (ret == THREAD_ERROR_NONE) - msg("thread_joiner_stop success"); - else - msg("thread_joiner_stop failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_joiner_start(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_joiner_start(g_instance, g_pskd, g_prov_url, - g_vendor_name, g_vendor_model, g_vendor_sw_version, g_vendor_data, - __thread_joiner_start_callback, NULL); - - if (ret == THREAD_ERROR_NONE) - msg("thread_joiner_start success"); - else - msg("thread_joiner_start failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static struct menu_data menu_thread_joiner_join_by_network_key[] = { - { "1", "Type network_key", NULL, NULL, g_network_key}, - { "2", "Type panid", NULL, NULL, g_panid}, - { "3", "run", NULL, run_thread_joiner_join_by_network_key, NULL}, - { NULL, NULL, }, -}; - -static struct menu_data menu_thread_joiner_start[] = { - { "1", "Type Pass key", NULL, NULL, g_pskd}, - { "2", "Type Prov url", NULL, NULL, g_prov_url}, - { "3", "Type Vendor Name", NULL, NULL, g_vendor_name}, - { "4", "Type Vendor Model", NULL, NULL, g_vendor_model}, - { "5", "Type Vendor sw_version", NULL, NULL, g_vendor_sw_version}, - { "6", "Type Vendor data", NULL, NULL, g_vendor_data}, - { "7", "run", NULL, run_thread_joiner_start, NULL}, - { NULL, NULL, }, -}; -struct menu_data menu_thread_joiner[] = { - { "1", "thread_joiner_join_by_networkkey", - menu_thread_joiner_join_by_network_key, NULL, NULL}, - { "2", "thread_joiner_start", - menu_thread_joiner_start, NULL, NULL }, - { "3", "thread_joiner_stop", - NULL, run_thread_joiner_stop, NULL }, - { NULL, NULL, }, -}; diff --git a/tests/thread-main.c b/tests/thread-main.c deleted file mode 100755 index 7c4fced..0000000 --- a/tests/thread-main.c +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright (c) 2021 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 -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "thread.h" -#include "thread-menu.h" - - -extern struct menu_data menu_thread_init[]; -extern struct menu_data menu_thread_core[]; -extern struct menu_data menu_thread_joiner[]; -extern struct menu_data menu_thread_commissioner[]; -extern struct menu_data menu_thread_network[]; -extern struct menu_data menu_thread_br[]; -extern struct menu_data menu_thread_srp[]; -extern struct menu_data menu_thread_scenarios[]; - -static struct menu_data menu_main[] = { - { "1", "Core", menu_thread_core, NULL, NULL}, - { "2", "Joiner", menu_thread_joiner, NULL, NULL}, - { "3", "Commissioner", menu_thread_commissioner, NULL, NULL}, - { "4", "Network", menu_thread_network, NULL, NULL}, - { "5", "Border Router", menu_thread_br, NULL, NULL}, - { "6", "SRP", menu_thread_srp, NULL, NULL}, - { "7", "Scenarios", menu_thread_scenarios, NULL, NULL}, - { NULL, NULL, }, -}; - -static int __init_func(MManager *mm, struct menu_data *menu) -{ - msg("thread_initialize started..."); - - int ret = thread_initialize(); - - if (ret == THREAD_ERROR_NONE) - msg("thread_initialize_completed"); - else - msg("thread_initalize_failed"); - - return RET_SUCCESS; -} - -static gboolean __create_init_menu(struct menu_data init_menu[1]) -{ - init_menu[0].key = "1"; - init_menu[0].title = "Init"; - init_menu[0].sub_menu = menu_main; - init_menu[0].callback = __init_func; - init_menu[0].data = NULL; - - return TRUE; -} - -static void __deinit_func(void) -{ - msg("thread_deinit started..."); - - int ret = thread_deinitialize(); - - if (ret == THREAD_ERROR_NONE) - msg("thread_deinit_completed"); - else - msg("thread_deinit_failed"); - - return; -} - -int main(int arg, char **argv) -{ - GMainLoop *mainloop = NULL; - GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO); - MManager *manager; - struct menu_data init_menu[1+1] = { {NULL, NULL, } }; - -#if !GLIB_CHECK_VERSION(2, 35, 0) - g_type_init(); -#endif - mainloop = g_main_loop_new(NULL, FALSE); - - msg(""); - msg("* Thread test application "); - msg("* Build On: %s %s", __DATE__, __TIME__); - - if (__create_init_menu(init_menu) == FALSE) - goto OUT; - - manager = menu_manager_new(init_menu, mainloop); - if (!manager) - goto OUT; - - menu_manager_run(manager); - - g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL), - on_menu_manager_keyboard, manager); - g_main_loop_run(mainloop); - thread_disable(manager->t_instance); - -OUT: - __deinit_func(); - menu_manager_free(manager); - - g_main_loop_unref(mainloop); - msg("******* Bye bye *******"); - - return 0; -} diff --git a/tests/thread-menu.c b/tests/thread-menu.c deleted file mode 100755 index 7675fba..0000000 --- a/tests/thread-menu.c +++ /dev/null @@ -1,333 +0,0 @@ -/* - * Copyright (c) 2021 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. - * - */ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include "thread-menu.h" - -#define DEFAULT_MENU_MENU "m" -#define DEFAULT_MENU_NONE "t" -#define DEFAULT_MENU_PREV "p" -#define DEFAULT_MENU_QUIT "q" - -#define TAB_SPACE " " - -char key_buffer[MENU_DATA_SIZE]; -int flag_pid_display = 1; - -char g_thread_interface_str[MENU_DATA_SIZE + 1] = "thread.default.interface"; - -pid_t get_tid() -{ - return syscall(__NR_gettid); -} - -void hide_pid() -{ - flag_pid_display = 0; -} - -void show_pid() -{ - flag_pid_display = 1; -} - - -static void _show_prompt(void) -{ - msgn("(%5d) >> ", get_tid()); -} - -static void _show_reserved_menu(void) -{ - msg(ANSI_COLOR_DARKGRAY HR_SINGLE2 ANSI_COLOR_NORMAL); - - msg(ANSI_COLOR_DARKGRAY " [ " ANSI_COLOR_NORMAL "%s" ANSI_COLOR_DARKGRAY - " ] " ANSI_COLOR_NORMAL "Thread default interface [" ANSI_COLOR_YELLOW "%s" ANSI_COLOR_NORMAL - " ] ", DEFAULT_MENU_NONE, g_thread_interface_str); - - msg(ANSI_COLOR_DARKGRAY HR_SINGLE2 ANSI_COLOR_NORMAL); - - - msg(ANSI_COLOR_DARKGRAY HR_SINGLE2 ANSI_COLOR_NORMAL); - msg(ANSI_COLOR_DARKGRAY " [ " ANSI_COLOR_NORMAL "%s" ANSI_COLOR_DARKGRAY - " ] " ANSI_COLOR_NORMAL "Previous menu " , DEFAULT_MENU_PREV); - msg(ANSI_COLOR_DARKGRAY " [ " ANSI_COLOR_NORMAL "%s" ANSI_COLOR_DARKGRAY - " ] " ANSI_COLOR_NORMAL "Show Menu " , DEFAULT_MENU_MENU); - msg(ANSI_COLOR_DARKGRAY " [ " ANSI_COLOR_NORMAL "%s" ANSI_COLOR_DARKGRAY - " ] " ANSI_COLOR_NORMAL "Quit " , DEFAULT_MENU_QUIT); -} - -static void _show_input_ok(void) -{ - msg("OK."); -} - -static void _show_menu(MManager *m, struct menu_data menu[]) -{ - int i = 0; - int len = 0; - struct menu_data *item; - char title_buf[256] = { 0, }; - - if (!menu) - return; - - msg(""); - msg(HR_DOUBLE); - - len = g_queue_get_length(m->title_stack); - msgn(ANSI_COLOR_YELLOW " Main"); - if (len > 0) { - for (i = 0; i < len; i++) { - msgn(ANSI_COLOR_NORMAL " >> " ANSI_COLOR_YELLOW "%s", - (char *)g_queue_peek_nth(m->title_stack, i)); - } - } - msg(ANSI_COLOR_NORMAL); - msg(HR_SINGLE); - - hide_pid(); - i = 0; - - while (1) { - item = menu + i; - if (item->key == NULL) - break; - - if (!g_strcmp0(item->key, "-")) { - msgn(" "); - } else if (!g_strcmp0(item->key, "_")) { - msg(ANSI_COLOR_DARKGRAY HR_SINGLE2 ANSI_COLOR_NORMAL); - - if (item->callback) - item->callback(m, item); - - i++; - - continue; - } else if (!g_strcmp0(item->key, "*")) { - msg(" %s", item->title); - if (item->callback) - item->callback(m, item); - } else { - msgn(ANSI_COLOR_DARKGRAY " [" ANSI_COLOR_NORMAL "%3s" - ANSI_COLOR_DARKGRAY "] " ANSI_COLOR_NORMAL, item->key); - } - - memset(title_buf, 0, 256); - if (item->title) { - snprintf(title_buf, MENU_DATA_SIZE, "%s", item->title); - - if (strlen(item->title) >= MAX_TITLE) { - title_buf[MAX_TITLE - 2] = '.'; - title_buf[MAX_TITLE - 1] = '.'; - } - } - - if (item->data) { - msg("%s " ANSI_COLOR_LIGHTBLUE "(%s)" ANSI_COLOR_NORMAL, - title_buf, item->data); - } else if (!g_strcmp0(item->key, "*")) { - /* none */ - } else { - msg("%s", title_buf); - } - - if (item->sub_menu) - msg("\e[1A\e[%dC >", (int)POS_MORE); - - i++; - } - - show_pid(); - - _show_reserved_menu(); - - msg(HR_DOUBLE); - - _show_prompt(); -} - -static void _show_item_data_input_msg(struct menu_data *item) -{ - msg(""); - msg(HR_DOUBLE); - msg(" Input [%s] data ", item->title); - msg(HR_SINGLE); - msg(" current = [%s]", item->data); - msgn(" new >> "); -} - -static void _move_menu(MManager *mm, struct menu_data menu[], char *key) -{ - struct menu_data *item; - int i = 0; - - if (!mm->menu) - return; - - if (!g_strcmp0(DEFAULT_MENU_PREV, key)) { - if (g_queue_get_length(mm->stack) > 0) { - mm->menu = g_queue_pop_tail(mm->stack); - g_queue_pop_tail(mm->title_stack); - } - - _show_menu(mm, mm->menu); - mm->buf = key_buffer; - - return; - } else if (!g_strcmp0(DEFAULT_MENU_MENU, key)) { - _show_menu(mm, mm->menu); - return; - } else if (!g_strcmp0(DEFAULT_MENU_QUIT, key)) { - g_main_loop_quit(mm->mainloop); - return; - } - - while (1) { - int ret = RET_SUCCESS; - item = menu + i; - if (item->key == NULL) - break; - - if (!g_strcmp0(item->key, key)) { - if (item->callback) { - ret = item->callback(mm, item); - _show_prompt(); - } - if (RET_SUCCESS != ret) - return; - - if (item->sub_menu) { - g_queue_push_tail(mm->stack, mm->menu); - g_queue_push_tail(mm->title_stack, (gpointer *)item->title); - - mm->menu = item->sub_menu; - _show_menu(mm, mm->menu); - mm->buf = key_buffer; - } - - if (NULL == item->sub_menu && item->data) { - _show_item_data_input_msg(item); - mm->buf = item->data; - } - - return; - } - - i++; - } - - _show_prompt(); -} - -MManager *menu_manager_new(struct menu_data items[], GMainLoop *mainloop) -{ - MManager *mm; - - mm = calloc(sizeof(struct menu_manager), 1); - if (!mm) - return NULL; - - mm->stack = g_queue_new(); - mm->title_stack = g_queue_new(); - mm->menu = items; - mm->mainloop = mainloop; - mm->t_instance = NULL; - return mm; -} - -int menu_manager_free(MManager *mm) -{ - if (mm) { - if (mm->stack) - g_queue_free(mm->stack); - if (mm->title_stack) - g_queue_free(mm->title_stack); - free(mm); - } - return 0; -} - -int menu_manager_run(MManager *mm) -{ - _show_menu(mm, mm->menu); - - mm->buf = key_buffer; - - return 0; -} - -int menu_manager_set_user_data(MManager *mm, void *user_data) -{ - if (!mm) - return -1; - - mm->user_data = user_data; - - return 0; -} - -void *menu_manager_ref_user_data(MManager *mm) -{ - if (!mm) - return NULL; - - return mm->user_data; -} - -gboolean on_menu_manager_keyboard(GIOChannel *src, GIOCondition con, - gpointer data) -{ - MManager *mm = data; - char local_buf[MENU_DATA_SIZE + 1] = { 0, }; - - if (fgets(local_buf, MENU_DATA_SIZE, stdin) == NULL) - return TRUE; - - if (strlen(local_buf) > 0) { - if (local_buf[strlen(local_buf) - 1] == '\n') - local_buf[strlen(local_buf) - 1] = '\0'; - } - - if (mm->buf == key_buffer) { - if (strlen(local_buf) < 1) { - _show_prompt(); - return TRUE; - } - - _move_menu(mm, mm->menu, local_buf); - } else { - if (mm->buf) { - memset(mm->buf, 0, MENU_DATA_SIZE); - memcpy(mm->buf, local_buf, MENU_DATA_SIZE); - _show_input_ok(); - } - mm->buf = key_buffer; - _move_menu(mm, mm->menu, (char *)DEFAULT_MENU_MENU); - } - - return TRUE; -} diff --git a/tests/thread-menu.h b/tests/thread-menu.h deleted file mode 100755 index c871fc3..0000000 --- a/tests/thread-menu.h +++ /dev/null @@ -1,177 +0,0 @@ -/* - * Copyright (c) 2021 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 __MENU_H__ -#define __MENU_H__ - -__BEGIN_DECLS - -#define ANSI_COLOR_NORMAL "\e[0m" - -#define ANSI_COLOR_BLACK "\e[0;30m" -#define ANSI_COLOR_RED "\e[0;31m" -#define ANSI_COLOR_GREEN "\e[0;32m" -#define ANSI_COLOR_BROWN "\e[0;33m" -#define ANSI_COLOR_BLUE "\e[0;34m" -#define ANSI_COLOR_MAGENTA "\e[0;35m" -#define ANSI_COLOR_CYAN "\e[0;36m" -#define ANSI_COLOR_LIGHTGRAY "\e[0;37m" - -#define ANSI_COLOR_DARKGRAY "\e[1;30m" -#define ANSI_COLOR_LIGHTRED "\e[1;31m" -#define ANSI_COLOR_LIGHTGREEN "\e[1;32m" -#define ANSI_COLOR_YELLOW "\e[1;33m" -#define ANSI_COLOR_LIGHTBLUE "\e[1;34m" -#define ANSI_COLOR_LIGHTMAGENTA "\e[1;35m" -#define ANSI_COLOR_LIGHTCYAN "\e[1;36m" -#define ANSI_COLOR_WHITE "\e[1;37m" - - -#define msg(fmt, args...) do { fprintf(stdout, fmt "\n", ##args); \ - fflush(stdout); } while (0) -#define msgn(fmt, args...) do { fprintf(stdout, fmt, ##args); \ - fflush(stdout); } while (0) - -/* Bold (red) */ -#define msgr(fmt, args...) do { fprintf(stdout, ANSI_COLOR_RED fmt \ - ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) - -/* Bold (green) */ -#define msgb(fmt, args...) do { fprintf(stdout, ANSI_COLOR_LIGHTGREEN fmt \ - ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) - -/* Property message */ -#define msgp(fmt, args...) do { fprintf(stdout, ANSI_COLOR_LIGHTMAGENTA fmt \ - ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) - -/* Bold (Cyan) */ -#define msgc(fmt, args...) do { fprintf(stdout, ANSI_COLOR_CYAN fmt \ - ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) - -/* Bold (Yellow) */ -#define msgy(fmt, args...) do { fprintf(stdout, ANSI_COLOR_YELLOW fmt \ - ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) - -/* Bold (Light Red) */ -#define msglr(fmt, args...) do { fprintf(stdout, ANSI_COLOR_LIGHTRED fmt \ - ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) - -/* Bold (Blue) */ -#define msgbr(fmt, args...) do { fprintf(stdout, ANSI_COLOR_BLUE fmt \ - ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) - - -#define msgt(n, fmt, args...) do { fprintf(stdout, "\e[%dC" fmt "\n", \ - 3 + ((n) * 2), ##args); fflush(stdout); } while (0) - -#define pmsg(fmt, args...) do { \ - if (is_pid_show()) fprintf(stdout, "(%5d) ", get_tid()); \ - fprintf(stdout, fmt "\n", ##args); fflush(stdout); } while (0) - -#define pmsgb(fmt, args...) do { \ - if (is_pid_show()) fprintf(stdout, "(%5d) ", get_tid()); \ - fprintf(stdout, ANSI_COLOR_LIGHTGREEN fmt \ - ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) - -#define pmsgt(n, fmt, args...) do { \ - if (is_pid_show()) fprintf(stdout, "(%5d) ", get_tid()); \ - fprintf(stdout, "\e[%dC" fmt "\n", \ - 3 + ((n) * 2), ##args); fflush(stdout); } while (0) - -#define ret_if(expr) \ -do { \ - if (expr) { \ - msgr("(%s) return", #expr); \ - return; \ - } \ -} while (0) - -#define retv_if(expr, val) \ -do { \ - if (expr) { \ - msgr("(%s) return", #expr); \ - return (val); \ - } \ -} while (0) - -/* Null check */ -#define check_if(expr) \ -do { \ - if (expr) { \ - msgr("(%s) ", #expr); \ - } \ -} while (0) - -#define MENU_DATA_SIZE 255 -#define NETWORK_DATA_SIZE 256 -/* - * Horizontal Line - width: 65 - * .12345678901234567890123456789012345678901234567890. - */ -#define HR_SINGLE "----------------------------------------" \ - "-------------------------" -#define HR_DOUBLE "========================================" \ - "=========================" -#define HR_SINGLE2 " ---------------------------------------" \ - "------------------------ " - -#define MAX_WIDTH strlen(HR_SINGLE) -#define MAX_TITLE ((MAX_WIDTH) - 10) -#define POS_MORE ((MAX_WIDTH) - 3) -#define RET_SUCCESS 0 -#define RET_FAILURE -1 - -#include "thread-log.h" -#include "thread-util.h" - -struct menu_manager { - GQueue *stack; - GQueue *title_stack; - struct menu_data *menu; - char *buf; - void *user_data; - GMainLoop *mainloop; - thread_instance_h t_instance; -}; -typedef struct menu_manager MManager; - -struct menu_data { - const char *key; - const char *title; - struct menu_data *sub_menu; - int (*callback)(MManager *mm, struct menu_data *menu); - char *data; -}; - -MManager* menu_manager_new(struct menu_data items[], GMainLoop *mainloop); -int menu_manager_run(MManager *mm); -int menu_manager_set_user_data(MManager *mm, void *user_data); -void* menu_manager_ref_user_data(MManager *mm); -int menu_manager_free(MManager *mm); - -gboolean on_menu_manager_keyboard(GIOChannel *src, GIOCondition con, - gpointer data); - -pid_t get_tid(); -void hide_pid(); -void show_pid(); -int is_pid_show(); -void menu_print_dump(int data_len, void *data); - -__END_DECLS - -#endif diff --git a/tests/thread-network.c b/tests/thread-network.c deleted file mode 100644 index eb3a3ba..0000000 --- a/tests/thread-network.c +++ /dev/null @@ -1,339 +0,0 @@ -/* - * Copyright (c) 2021 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 -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "thread.h" -#include "thread-menu.h" -#include "thread-type.h" -#include "thread-log.h" -#include "thread-util.h" - -static char g_name[NETWORK_DATA_SIZE + 1] = {"Thread-test"}; -static char g_key[NETWORK_DATA_SIZE + 1]; -static char g_pskc[NETWORK_DATA_SIZE + 1]; -static char g_str_channel[NETWORK_DATA_SIZE + 1] = {"07fff800"}; -static char g_str_extended_panid[NETWORK_DATA_SIZE + 1] = {"18446744073709551615"}; /* UINT64_MAX */ -static char g_str_panid[NETWORK_DATA_SIZE + 1] = {"65535"}; /* UINT16_MAX */ -static char g_str_buf_length[NETWORK_DATA_SIZE + 1]; -static char g_str_tlvs_buffer[2*NETWORK_DATA_SIZE + 1]; -static thread_network_h g_network; - -uint32_t g_channel; -uint64_t g_extended_panid; -uint16_t g_panid; - -static char g_str_ipaddr_type[NETWORK_DATA_SIZE + 1] = "0"; -static char g_str_ipaddr[NETWORK_DATA_SIZE + 1]; - -uint8_t g_ipv6_address[THREAD_IPV6_ADDRESS_SIZE]; - -static int run_thread_network_destroy_operational_network(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_network_destroy_operational_network(g_instance, g_network); - if (ret == THREAD_ERROR_NONE) - msg("thread_network_destroy_operational_network success"); - else - msg("thread_network_destroy_operational_network failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_network_create_operational_network(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - sscanf(g_str_channel, "%x", &g_channel); - sscanf(g_str_extended_panid, "%zu", (size_t *)&g_extended_panid); - sscanf(g_str_panid, "%hu", &g_panid); - - int ret = thread_network_create_operational_network(g_instance, g_name, g_key, - g_pskc, g_channel, g_extended_panid, g_panid, &g_network); - if (ret == THREAD_ERROR_NONE) - msg("thread_network_create_operational_network success"); - else - msg("thread_network_create_operational_network failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_network_set_active_dataset_tlvs(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int buf_length; - uint8_t tlvs_buffer[1024]; - - sscanf(g_str_buf_length, "%d", &buf_length); - msg("Set active dataset tlvs size: %d :: %s", - buf_length, g_str_tlvs_buffer); - - if (!buf_length || buf_length > (MENU_DATA_SIZE + 1)) { - msg("Invalid Input"); - goto OUT; - } - for (int i = 0; i < buf_length; i++) { - char subbuff[3]; - memcpy(subbuff, &g_str_tlvs_buffer[2*i], 2); - subbuff[2] = '\0'; - tlvs_buffer[i] = (uint8_t)strtol(subbuff, NULL, 16); - } - - int ret = thread_network_set_active_dataset_tlvs( - g_instance, tlvs_buffer, buf_length); - if (ret == THREAD_ERROR_NONE) - msg("thread_network_set_active_dataset_tlvs success"); - else - msg("thread_network_set_active_dataset_tlvs failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_network_get_active_dataset_tlvs(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - - uint8_t *tlvs_buffer = NULL; - char buf[1024]; - int buf_length; - - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_network_get_active_dataset_tlvs(g_instance, &tlvs_buffer, &buf_length); - if (ret == THREAD_ERROR_NONE) { - msg("thread_network_get_active_dataset_tlvs success"); - - for (int i = 0; i < buf_length; i++) - snprintf(buf + i*2, 3, "%2.2x", tlvs_buffer[i]); - - msg("Active dataset tlvs size: %d :: %s", buf_length, buf); - } else { - msg("thread_network_get_active_dataset_tlvs failed"); - } -OUT: - if (tlvs_buffer) - free(tlvs_buffer); - - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_network_attach(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_network_attach(g_instance); - if (ret == THREAD_ERROR_NONE) - msg("thread_network_attach success"); - else - msg("thread_network_attach failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_network_detach(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_network_detach(g_instance); - if (ret == THREAD_ERROR_NONE) - msg("thread_network_detach success"); - else - msg("thread_network_detach failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static void __get_ipaddr_callback(int index, char* ipaddr, - thread_ipaddr_type_e ipaddr_type, void *user_data) { - FUNC_ENTRY; - - if (index == 1) - msg("IP address type: %s", _thread_ipaddr_type_to_string(ipaddr_type)); - msg("IP address count: %d, ipv6 address: %s", index, ipaddr); - - FUNC_EXIT; -} - -static int run_thread_get_ipaddr(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int type = atoi(g_str_ipaddr_type); - thread_ipaddr_type_e ipaddr_type; - switch (type) { - case 0: - ipaddr_type = THREAD_IPADDR_TYPE_ALL; - break; - case 1: - ipaddr_type = THREAD_IPADDR_TYPE_LINK_LOCAL; - break; - case 2: - ipaddr_type = THREAD_IPADDR_TYPE_RLOC; - break; - case 3: - ipaddr_type = THREAD_IPADDR_TYPE_MLEID; - break; - default: - goto OUT; - } - - int ret = thread_get_ipaddr(g_instance, __get_ipaddr_callback, ipaddr_type, NULL); - if (ret == THREAD_ERROR_NONE) - msg("thread_get_ipaddr success"); - else - msg("thread_get_ipaddr failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_add_ipaddr(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = _preprocess_ipv6_prefix(g_str_ipaddr, g_ipv6_address); - retv_if(ret != THREAD_ERROR_NONE, ret); - - ret = thread_add_ipaddr(g_instance, g_ipv6_address); - if (ret == THREAD_ERROR_NONE) - msg("thread_add_ipaddr success"); - else - msg("thread_add_ipaddr failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int run_thread_remove_ipaddr(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = _preprocess_ipv6_prefix(g_str_ipaddr, g_ipv6_address); - retv_if(ret != THREAD_ERROR_NONE, ret); - - ret = thread_remove_ipaddr(g_instance, g_ipv6_address); - if (ret == THREAD_ERROR_NONE) - msg("thread_remove_ipaddr success"); - else - msg("thread_remove_ipaddr failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static struct menu_data menu_thread_network_set_active_dataset_tlvs[] = { - { "1", "Tlvs_buffer len", NULL, NULL, g_str_buf_length}, - { "2", "Tlvs_buffer", NULL, NULL, g_str_tlvs_buffer}, - { "3", "run", NULL, run_thread_network_set_active_dataset_tlvs, NULL}, - { NULL, NULL, }, -}; - -static struct menu_data menu_thread_network_create_operational_network[] = { - { "1", "Network Name", NULL, NULL, g_name}, - { "2", "Network Key", NULL, NULL, g_key}, - { "3", "Network pskc", NULL, NULL, g_pskc}, - { "4", "Network Channel", NULL, NULL, g_str_channel}, - { "5", "Network Extended Panid", NULL, NULL, g_str_extended_panid}, - { "6", "Network panid", NULL, NULL, g_str_panid}, - { "7", "run", NULL, run_thread_network_create_operational_network, NULL}, - { NULL, NULL, }, -}; - -static struct menu_data menu_thread_get_ipaddr[] = { - { "1", "Node Addr Type: ALL > 0, LINK LOCAL > 1, RLOC > 2, MLEID > 3", - NULL, NULL, g_str_ipaddr_type}, - { "2", "run", NULL, run_thread_get_ipaddr, NULL}, - { NULL, NULL, }, -}; - -static struct menu_data menu_thread_add_ipaddr[] = { - { "1", "Type IPv6 address", NULL, NULL, g_str_ipaddr}, - { "2", "run", NULL, run_thread_add_ipaddr, NULL}, - { NULL, NULL, }, -}; - -static struct menu_data menu_thread_remove_ipaddr[] = { - { "1", "Type IPv6 address", NULL, NULL, g_str_ipaddr}, - { "2", "run", NULL, run_thread_remove_ipaddr, NULL}, - { NULL, NULL, }, -}; - -struct menu_data menu_thread_network[] = { - { "1", "thread_network_create_operational_network", - menu_thread_network_create_operational_network, NULL, NULL}, - { "2", "thread_network_destroy_operational_network", - NULL, run_thread_network_destroy_operational_network, NULL }, - { "3", "thread_network_set_active_dataset_tlvs", - menu_thread_network_set_active_dataset_tlvs, NULL, NULL}, - { "4", "thread_network_get_active_dataset_tlvs", - NULL, run_thread_network_get_active_dataset_tlvs, NULL }, - { "5", "thread_network_attach", - NULL, run_thread_network_attach, NULL}, - { "6", "thread_network_detach", - NULL, run_thread_network_detach, NULL}, - { "7", "thread_get_ipaddr", - menu_thread_get_ipaddr, NULL, NULL}, - { "8", "thread_add_ipaddr", - menu_thread_add_ipaddr, NULL, NULL}, - { "9", "thread_remove_ipaddr", - menu_thread_remove_ipaddr, NULL, NULL}, - { NULL, NULL, }, -}; - diff --git a/tests/thread-srp.c b/tests/thread-srp.c deleted file mode 100644 index 8b50389..0000000 --- a/tests/thread-srp.c +++ /dev/null @@ -1,246 +0,0 @@ -/* - * Copyright (c) 2021 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 -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "thread.h" -#include "thread-menu.h" -#include "thread-type.h" -#include "thread-log.h" -#include "thread-util.h" - -const char *service_name; -const char *address; -uint64_t port; -bool is_deleted; -uint64_t g_port; - -static char g_host_address[MENU_DATA_SIZE + 1]; -static char g_service_name[MENU_DATA_SIZE + 1]; -static char g_service_type[MENU_DATA_SIZE + 1]; -static char g_str_port[MENU_DATA_SIZE + 1]; -static char g_host_name[MENU_DATA_SIZE + 1]; - -static int __run_thread_srp_client_start(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_srp_client_start(g_instance); - if (ret == THREAD_ERROR_NONE) - msg("thread_srp_client_start success"); - else - msg("thread_srp_client_start failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int __run_thread_srp_client_stop(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_srp_client_stop(g_instance); - if (ret == THREAD_ERROR_NONE) - msg("thread_srp_client_stop success"); - else - msg("thread_srp_client_stop failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int __run_thread_srp_client_remove_host(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_srp_client_remove_host(g_instance); - if (ret == THREAD_ERROR_NONE) - msg("thread_srp_client_remove_host success"); - else - msg("thread_srp_client_remove_host failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int __run_thread_srp_client_set_host_name(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_srp_client_set_host_name(g_instance, g_host_name); - if (ret == THREAD_ERROR_NONE) - msg("thread_srp_client_set_host_name success"); - else - msg("thread_srp_client_set_host_name failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int __run_thread_srp_client_set_host_address(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_srp_client_set_host_address(g_instance, g_host_address); - if (ret == THREAD_ERROR_NONE) - msg("thread_srp_client_set_host_address success"); - else - msg("thread_srp_client_set_host_address failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int __run_thread_srp_client_register_service(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - g_port = (uint64_t)strtoll(g_str_port, (char **)NULL, 10); - - int ret = thread_srp_client_register_service(g_instance, - g_service_name, g_service_type, g_port); - if (ret == THREAD_ERROR_NONE) - msg("thread_srp_client_register_service success"); - else - msg("thread_srp_client_register_service failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int __run_thread_srp_server_start(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_srp_server_start(g_instance); - if (ret == THREAD_ERROR_NONE) - msg("thread_srp_server_start success"); - else - msg("thread_srp_server_start failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int __run_thread_srp_server_stop(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - - int ret = thread_srp_server_stop(g_instance); - if (ret == THREAD_ERROR_NONE) - msg("thread_srp_server_stop success"); - else - msg("thread_srp_server_stop failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static int __run_thread_srp_server_get_registered_service(MManager *mm, struct menu_data *menu) -{ - FUNC_ENTRY; - thread_instance_h g_instance = mm->t_instance; - if (g_instance == NULL) - goto OUT; - int ret = thread_srp_server_get_registered_service(g_instance, &service_name, &address, &port, &is_deleted); - - if (ret == THREAD_ERROR_NONE) { - msg("thread_srp_get_registered_service success"); - msg("service: [%s], [%s], [%zu], [%s]", service_name, address, (size_t)port, is_deleted ? "TRUE" : "FALSE"); - } else - msg("thread_srp_get_registered_service failed"); -OUT: - FUNC_EXIT; - return RET_SUCCESS; -} - -static struct menu_data menu_thread_srp_client_set_host_name[] = { - { "1", "Type host_name", NULL, NULL, g_host_name}, - { "2", "run", NULL, __run_thread_srp_client_set_host_name, NULL}, - { NULL, NULL, }, -}; - -static struct menu_data menu_thread_srp_client_set_host_address[] = { - { "1", "Type host_address", NULL, NULL, g_host_address}, - { "2", "run", NULL, __run_thread_srp_client_set_host_address, NULL}, - { NULL, NULL, }, -}; - -static struct menu_data menu_thread_srp_client_register_service[] = { - { "1", "Type service_name", NULL, NULL, g_service_name}, - { "2", "Type service_type", NULL, NULL, g_service_type}, - { "3", "Type port", NULL, NULL, g_str_port}, - { "4", "run", NULL, __run_thread_srp_client_register_service, NULL}, - { NULL, NULL, }, -}; - -struct menu_data menu_thread_srp[] = { - - { "1", "thread_srp_client_start", - NULL, __run_thread_srp_client_start, NULL}, - { "2", "thread_srp_client_stop", - NULL, __run_thread_srp_client_stop, NULL }, - { "3", "thread_srp_client_set_host_name", - menu_thread_srp_client_set_host_name, NULL, NULL}, - { "4", "thread_srp_client_remove_host", - NULL, __run_thread_srp_client_remove_host, NULL }, - { "5", "thread_srp_client_set_host_address", - menu_thread_srp_client_set_host_address, NULL, NULL }, - { "6", "thread_srp_client_register_service", - menu_thread_srp_client_register_service, NULL, NULL }, - { "7", "thread_srp_server_start", - NULL, __run_thread_srp_server_start, NULL }, - { "8", "thread_srp_server_stop", - NULL, __run_thread_srp_server_stop, NULL }, - { "9", "thread_srp_server_get_registered_service", - NULL, __run_thread_srp_server_get_registered_service, NULL }, - { NULL, NULL, }, -}; - diff --git a/tests/thread-test/CMakeLists.txt b/tests/thread-test/CMakeLists.txt new file mode 100755 index 0000000..ad99a19 --- /dev/null +++ b/tests/thread-test/CMakeLists.txt @@ -0,0 +1,32 @@ +SET(test thread-test) + +INCLUDE(FindPkgConfig) +pkg_check_modules(${test} REQUIRED + glib-2.0 +) + +FOREACH(flag ${${test}_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -Werror") + +SET(TEST_SRCS + thread-init.c + thread-menu.c + thread-main.c + thread-core.c + thread-util.c + thread-joiner.c + thread-commissioner.c + thread-network.c + thread-br.c + thread-srp.c + thread-scenarios.c +) + +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") +ADD_EXECUTABLE(${test} ${TEST_SRCS}) +TARGET_LINK_LIBRARIES(${test} ${PROJECT_NAME} ${test_LDFLAGS}) +INSTALL(TARGETS ${test} DESTINATION bin) + diff --git a/tests/thread-test/thread-br.c b/tests/thread-test/thread-br.c new file mode 100644 index 0000000..8c40d19 --- /dev/null +++ b/tests/thread-test/thread-br.c @@ -0,0 +1,520 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "thread.h" +#include "thread-menu.h" +#include "thread-type.h" +#include "thread-log.h" +#include "thread-util.h" + +/* External route related variables */ +char g_str_ext_ipv6_prefix[MENU_DATA_SIZE + 1]; +char g_str_ext_ipv6_prefix_len[MENU_DATA_SIZE + 1]; +char g_str_ext_rloc16[MENU_DATA_SIZE + 1]; +char g_str_ext_preference[MENU_DATA_SIZE + 1] = {"1"}; +char g_str_ext_is_stable[MENU_DATA_SIZE + 1] = {"1"}; + +uint8_t g_ext_ipv6_prefix[THREAD_IPV6_ADDRESS_SIZE]; +uint8_t g_ext_ipv6_prefix_len; +uint16_t g_ext_rloc16; +int8_t g_ext_preference; +bool g_ext_is_stable; +bool g_ext_is_device_next_hop = THREAD_IS_DEVICE_NEXT_HOP_DEFAULT; + +/* Onmesh prefix related variables */ +char g_str_onmesh_ipv6_prefix[MENU_DATA_SIZE + 1]; +char g_str_onmesh_ipv6_prefix_len[MENU_DATA_SIZE + 1]; +char g_str_onmesh_preference[MENU_DATA_SIZE + 1] = {"1"}; +char g_str_slaac[MENU_DATA_SIZE + 1] = {"1"}; +char g_str_dhcp[MENU_DATA_SIZE + 1] = {"1"}; +char g_str_configure[MENU_DATA_SIZE + 1] = {"1"}; +char g_str_default_route[MENU_DATA_SIZE + 1] = {"1"}; +char g_str_on_mesh[MENU_DATA_SIZE + 1] = {"1"}; +char g_str_stable[MENU_DATA_SIZE + 1] = {"1"}; +char g_str_preferred[MENU_DATA_SIZE + 1] = {"1"}; + +int g_slaac; +int g_dhcp; +int g_configure; +int g_default_route; +int g_on_mesh; +int g_stable; +uint8_t g_onmesh_ipv6_prefix[THREAD_IPV6_ADDRESS_SIZE]; +uint8_t g_onmesh_ipv6_prefix_len; +int8_t g_onmesh_preference; +int g_preferred; + +GSList *route_list; +GSList *onmesh_prefix_list; + +void free_list(GSList *list) +{ + for (GSList *l = list; l; l = g_slist_next(l)) + if (l->data) + g_free(l->data); + + return; +} + +thread_route_info_a* _create_new_route(void) +{ + FUNC_ENTRY; + + thread_route_info_a *new_route = NULL; + new_route = g_malloc0(sizeof(thread_route_info_a)); + if (!new_route) { + THREAD_ERR("g_malloc0 failed while allocating new route"); + return NULL; + } + + FUNC_EXIT; + return new_route; +} + +thread_onmesh_prefix_info_a* _create_new_onmesh_prefix(void) +{ + FUNC_ENTRY; + + thread_onmesh_prefix_info_a *new_onmesh_prefix = NULL; + new_onmesh_prefix = g_malloc0(sizeof(thread_onmesh_prefix_info_a)); + if (!new_onmesh_prefix) { + THREAD_ERR("g_malloc0 failed while allocating new_onmesh_prefix"); + return NULL; + } + + FUNC_EXIT; + return new_onmesh_prefix; +} + +static bool __thread_br_get_external_routes_callback(int total, thread_route_info_h route_info, void *user_data) +{ + msg("__thread_br_get_external_routes_callback, result: %d", total); + + msg("Found the external route..."); + + int ret = thread_br_get_route_info(route_info, + g_ext_ipv6_prefix, &g_ext_ipv6_prefix_len, &g_ext_rloc16, + &g_ext_preference, &g_ext_is_stable, &g_ext_is_device_next_hop); + + if (ret == THREAD_ERROR_NONE) { + msg("thread_br_get_route_info success"); + uint8_t *prefix_address = g_ext_ipv6_prefix; + + msg("prefix_address: %02x%02x:%02x%02x:%02x%02x:%02x%02x", + prefix_address[0], prefix_address[1], prefix_address[2], + prefix_address[3], prefix_address[4], prefix_address[5], + prefix_address[6], prefix_address[7]); + msg("prefix_len: %u", g_ext_ipv6_prefix_len); + msg("rloc16: %u", g_ext_rloc16); + msg("preference: %u", g_ext_preference); + msg("stable: %s", g_ext_is_stable ? "TRUE" : "FALSE"); + msg("next_hop_is_self: %s", g_ext_is_device_next_hop ? + "TRUE" : "FALSE"); + + /* Create a new route and add to route_list */ + thread_route_info_a *new_route = _create_new_route(); + if (!new_route) + goto OUT; + + new_route->handle = route_info; + memcpy(new_route->prefix_address, prefix_address, + THREAD_IPV6_PREFIX_SIZE); + new_route->prefix_length = g_ext_ipv6_prefix_len; + + route_list = g_slist_append(route_list, new_route); + + } else + msg("thread_br_get_route_info failed"); +OUT: + + msg("Callback exit"); + + return TRUE; +} + +static int run_thread_br_enable(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_br_enable(g_instance); + if (ret == THREAD_ERROR_NONE) { + msg("thread_br_enable success"); + free_list(route_list); + route_list = NULL; + free_list(onmesh_prefix_list); + onmesh_prefix_list = NULL; + + /* Initialize the route_list */ + ret = thread_br_get_external_routes(g_instance, + __thread_br_get_external_routes_callback, NULL); + retv_if(ret != THREAD_ERROR_NONE, ret); + } else + msg("thread_br_enable failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_br_disable(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_br_disable(g_instance); + + if (ret == THREAD_ERROR_NONE) { + msg("thread_br_disable success"); + free_list(route_list); + route_list = NULL; + free_list(onmesh_prefix_list); + onmesh_prefix_list = NULL; + + } else + msg("thread_br_disable failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_br_get_external_routes(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + free_list(route_list); + route_list = NULL; + int ret = thread_br_get_external_routes(g_instance, + __thread_br_get_external_routes_callback, NULL); + if (ret == THREAD_ERROR_NONE) + msg("thread_br_get_external_routes success"); + else + msg("thread_br_get_external_routes failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_br_add_external_route(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int is_stable; + int ret = _preprocess_ipv6_prefix(g_str_ext_ipv6_prefix, + g_ext_ipv6_prefix); + retv_if(ret != THREAD_ERROR_NONE, ret); + + sscanf(g_str_ext_ipv6_prefix_len, "%hhu", &g_ext_ipv6_prefix_len); + sscanf(g_str_ext_rloc16, "%hu", &g_ext_rloc16); + sscanf(g_str_ext_preference, "%hhd", &g_ext_preference); + sscanf(g_str_ext_is_stable, "%d", &is_stable); + g_ext_is_stable = (bool)is_stable; + + /* Printing parameters value */ + msg("Parameter value for add external route"); + + uint8_t *prefix_address = g_ext_ipv6_prefix; + msg("prefix_address: %02x%02x:%02x%02x:%02x%02x:%02x%02x::", + prefix_address[0], prefix_address[1], prefix_address[2], + prefix_address[3], prefix_address[4], prefix_address[5], + prefix_address[6], prefix_address[7]); + + msg("external__prefix_len: %hhu", g_ext_ipv6_prefix_len); + msg("external_preference: %hhd", g_ext_preference); + msg("external_rloc16: %hhd", g_ext_rloc16); + msg("external_is_stable: %s", g_ext_is_stable ? "TRUE" : "FALSE"); + + thread_route_info_a *new_route = _create_new_route(); + if (!new_route) + goto OUT; + + ret = thread_br_add_external_route(g_instance, g_ext_ipv6_prefix, + g_ext_ipv6_prefix_len, g_ext_rloc16, g_ext_preference, + g_ext_is_stable, g_ext_is_device_next_hop, + &(new_route->handle)); + + if (ret == THREAD_ERROR_NONE) { + msg("thread_br_add_external_route success"); + memcpy(new_route->prefix_address, prefix_address, + THREAD_IPV6_PREFIX_SIZE); + new_route->prefix_length = g_ext_ipv6_prefix_len; + route_list = g_slist_append(route_list, new_route); + } else { + msg("thread_br_add_external_route failed"); + g_free(new_route); + } +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_br_remove_external_route(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + + if (g_instance == NULL) + goto OUT; + + int ret = _preprocess_ipv6_prefix(g_str_ext_ipv6_prefix, + g_ext_ipv6_prefix); + retv_if(ret != THREAD_ERROR_NONE, ret); + sscanf(g_str_ext_ipv6_prefix_len, "%hhu", &g_ext_ipv6_prefix_len); + + thread_route_info_a *req_route = NULL; + + for (GSList *l = route_list; l; l = g_slist_next(l)) { + thread_route_info_a *route_info = l->data; + if (memcmp(route_info->prefix_address, g_ext_ipv6_prefix, + THREAD_IPV6_PREFIX_SIZE*(sizeof(uint8_t))) == 0) { + + if (route_info->prefix_length == g_ext_ipv6_prefix_len) { + req_route = route_info; + break; + } + } + } + + if (!req_route) { + msg("No Route found"); + goto OUT; + } + + ret = thread_br_remove_external_route(g_instance, req_route->handle); + if (ret == THREAD_ERROR_NONE) { + msg("thread_br_remove_external_route success"); + route_list = g_slist_remove(route_list, req_route); + } else + msg("thread_br_remove_external_route failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_br_add_onmesh_prefix(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = _preprocess_ipv6_prefix(g_str_onmesh_ipv6_prefix, + g_onmesh_ipv6_prefix); + retv_if(ret != THREAD_ERROR_NONE, ret); + + sscanf(g_str_onmesh_ipv6_prefix_len, "%hhu", &g_onmesh_ipv6_prefix_len); + sscanf(g_str_onmesh_preference, "%hhd", &g_onmesh_preference); + sscanf(g_str_slaac, "%d", &g_slaac); + sscanf(g_str_dhcp, "%d", &g_dhcp); + sscanf(g_str_configure, "%d", &g_configure); + sscanf(g_str_default_route, "%d", &g_default_route); + sscanf(g_str_on_mesh, "%d", &g_on_mesh); + sscanf(g_str_stable, "%d", &g_stable); + sscanf(g_str_preferred, "%d", &g_preferred); + + /* Printing parameters value */ + msg("Parameter value for add onmesh_prefix"); + + uint8_t *prefix_address = g_onmesh_ipv6_prefix; + msg("prefix_address: %02x%02x:%02x%02x:%02x%02x:%02x%02x", + prefix_address[0], prefix_address[1], prefix_address[2], + prefix_address[3], prefix_address[4], prefix_address[5], + prefix_address[6], prefix_address[7]); + + msg("g_onmesh_prefix_len: %hhu", g_onmesh_ipv6_prefix_len); + msg("g_onmesh_preference: %hhd", g_onmesh_preference); + msg("g_slaac: %s", g_slaac ? "TRUE" : "FALSE"); + msg("g_dhcp: %s", g_dhcp ? "TRUE" : "FALSE"); + msg("g_configure: %s", g_configure ? "TRUE" : "FALSE"); + msg("g_default_route: %s", g_default_route ? "TRUE" : "FALSE"); + msg("g_on_mesh: %s", g_on_mesh ? "TRUE" : "FALSE"); + msg("g_stable: %s", g_stable ? "TRUE" : "FALSE"); + msg("g_preferred: %s", g_preferred ? "TRUE" : "FALSE"); + + thread_onmesh_prefix_info_a *new_onmesh_prefix = _create_new_onmesh_prefix(); + if (!new_onmesh_prefix) + goto OUT; + + ret = thread_br_add_onmesh_prefix(g_instance, g_onmesh_ipv6_prefix, + g_onmesh_ipv6_prefix_len, g_onmesh_preference, + g_preferred, g_slaac, g_dhcp, g_configure, + g_default_route, g_on_mesh, g_stable, + &(new_onmesh_prefix->handle)); + + if (ret == THREAD_ERROR_NONE) { + msg("thread_br_add_onmesh_prefix success"); + memcpy(new_onmesh_prefix->prefix_address, prefix_address, + THREAD_IPV6_PREFIX_SIZE); + new_onmesh_prefix->prefix_length = g_onmesh_ipv6_prefix_len; + onmesh_prefix_list = g_slist_append(onmesh_prefix_list, + new_onmesh_prefix); + } else { + msg("thread_br_add_onmesh_prefix failed"); + g_free(new_onmesh_prefix); + } +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_br_remove_onmesh_prefix(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = _preprocess_ipv6_prefix(g_str_onmesh_ipv6_prefix, + g_onmesh_ipv6_prefix); + retv_if(ret != THREAD_ERROR_NONE, ret); + sscanf(g_str_onmesh_ipv6_prefix_len, "%hhu", &g_onmesh_ipv6_prefix_len); + + thread_onmesh_prefix_info_a *req_onmesh_prefix = NULL; + + for (GSList *l = route_list; l; l = g_slist_next(l)) { + thread_onmesh_prefix_info_a *onmesh_prefix_info = l->data; + if (memcmp(onmesh_prefix_info->prefix_address, + g_onmesh_ipv6_prefix, + THREAD_IPV6_PREFIX_SIZE*(sizeof(uint8_t))) == 0) { + + if (onmesh_prefix_info->prefix_length == + g_onmesh_ipv6_prefix_len) { + req_onmesh_prefix = onmesh_prefix_info; + break; + } + } + } + + if (!req_onmesh_prefix) { + msg("No Onmesh Prefix found"); + goto OUT; + } + + ret = thread_br_remove_onmesh_prefix(g_instance, + req_onmesh_prefix->handle); + if (ret == THREAD_ERROR_NONE) { + msg("thread_br_remove_onmesh_prefix success"); + onmesh_prefix_list = g_slist_remove(onmesh_prefix_list, + req_onmesh_prefix); + } else + msg("thread_br_remove_onmesh_prefix failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static struct menu_data menu_thread_br_add_external_route[] = { + { "1", "ext_ipv6_prefix", + NULL, NULL, g_str_ext_ipv6_prefix }, + { "2", "ext_ipv6_prefix_len", + NULL, NULL, g_str_ext_ipv6_prefix_len }, + { "3", "ext_rloc16", + NULL, NULL, g_str_ext_rloc16 }, + { "4", "ext_preference", + NULL, NULL, g_str_ext_preference }, + { "5", "is_stable[ Type 0:false, 1:true]", + NULL, NULL, g_str_ext_is_stable }, + { "6", "run", NULL, + run_thread_br_add_external_route, NULL }, + { NULL, NULL, }, +}; + +static struct menu_data menu_thread_br_remove_external_route[] = { + { "1", "ext_ipv6_prefix", + NULL, NULL, g_str_ext_ipv6_prefix }, + { "2", "ext_ipv6_prefix_len", + NULL, NULL, g_str_ext_ipv6_prefix_len }, + { "3", "run", NULL, + run_thread_br_remove_external_route, NULL }, + { NULL, NULL, }, +}; + + +static struct menu_data menu_thread_br_add_onmesh_prefix[] = { + { "1", "onmesh_ipv6_prefix", + NULL, NULL, g_str_onmesh_ipv6_prefix }, + { "2", "onmesh_ipv6_prefix_len", + NULL, NULL, g_str_onmesh_ipv6_prefix_len }, + { "3", "onmesh_prefernce [Type 0:Low, 1:Med, 2:High]", + NULL, NULL, g_str_onmesh_preference }, + { "4", "slaac [Type 0:False, 1:True]", + NULL, NULL, g_str_slaac }, + { "5", "dhcp [Type 0:False, 1:True]", + NULL, NULL, g_str_dhcp }, + { "6", "configure [Type 0:False, 1:True]", + NULL, NULL, g_str_configure }, + { "7", "default_route [Type 0:False, 1:True]", + NULL, NULL, g_str_default_route }, + { "8", "on_mesh [Type 0:False, 1:True]", + NULL, NULL, g_str_on_mesh }, + { "9", "stable [Type 0:False, 1:True]", + NULL, NULL, g_str_stable }, + { "10", "preferred [Type 0:False, 1:True]", + NULL, NULL, g_str_preferred }, + { "11", "run", NULL, + run_thread_br_add_onmesh_prefix, NULL }, + { NULL, NULL, }, +}; + +static struct menu_data menu_thread_br_remove_onmesh_prefix[] = { + { "1", "onmesh_ipv6_prefix", + NULL, NULL, g_str_onmesh_ipv6_prefix }, + { "2", "onmesh_prefix_len", + NULL, NULL, g_str_onmesh_ipv6_prefix_len }, + { "3", "run", NULL, + run_thread_br_remove_onmesh_prefix, NULL }, + { NULL, NULL, }, +}; + +struct menu_data menu_thread_br[] = { + { "1", "thread_br_enable", + NULL, run_thread_br_enable, NULL}, + { "2", "thread_br_disable", + NULL, run_thread_br_disable, NULL }, + { "3", "thread_br_get_external_routes", + NULL, run_thread_br_get_external_routes, NULL }, + { "4", "thread_br_add_external_route", + menu_thread_br_add_external_route, NULL, NULL }, + { "5", "thread_br_remove_external_route", + menu_thread_br_remove_external_route, NULL, NULL }, + { "6", "thread_br_add_onmesh_prefix", + menu_thread_br_add_onmesh_prefix, NULL, NULL }, + { "7", "thread_br_remove_onmesh_prefix", + menu_thread_br_remove_onmesh_prefix, NULL, NULL }, + { NULL, NULL, }, +}; diff --git a/tests/thread-test/thread-commissioner.c b/tests/thread-test/thread-commissioner.c new file mode 100644 index 0000000..e2baf2b --- /dev/null +++ b/tests/thread-test/thread-commissioner.c @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "thread.h" +#include "thread-menu.h" +#include "thread-type.h" +#include "thread-log.h" +#include "thread-util.h" + +static char g_joiner_uuid[MENU_DATA_SIZE + 1]; +static char g_joiner_passphrase[MENU_DATA_SIZE + 1]; + +static int run_thread_commissioner_start(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_commissioner_start(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("thread_commissioner_start success"); + else + msg("thread_commissioner_start failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_commissioner_set_commisioning_data(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_commissioner_set_commisioning_data(g_instance, g_joiner_uuid, + g_joiner_passphrase); + if (ret == THREAD_ERROR_NONE) + msg("thread_commissioner_set_commisioning_data success"); + else + msg("thread_commissioner_set_commisioning_data failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static struct menu_data menu_thread_commissioner_set_commisioning_data[] = { + { "1", "Joiner UUID", NULL, NULL, g_joiner_uuid}, + { "2", "Joiner Passphrase", NULL, NULL, g_joiner_passphrase}, + { "3", "run", NULL, run_thread_commissioner_set_commisioning_data, NULL}, + { NULL, NULL, }, +}; + +struct menu_data menu_thread_commissioner[] = { + { "1", "thread_commissioner_start", + NULL, run_thread_commissioner_start, NULL}, + { "2", "thread_commissioner_set_commisioning_data", + menu_thread_commissioner_set_commisioning_data, NULL, NULL }, + { NULL, NULL, }, +}; diff --git a/tests/thread-test/thread-core.c b/tests/thread-test/thread-core.c new file mode 100755 index 0000000..7d2d987 --- /dev/null +++ b/tests/thread-test/thread-core.c @@ -0,0 +1,394 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "thread.h" +#include "thread-menu.h" +#include "thread-type.h" +#include "thread-log.h" +#include "thread-util.h" + +thread_device_role_e g_device_role; +thread_device_type_e g_device_type; +char g_str_device_type[MENU_DATA_SIZE + 1]; + +char g_str_scan_duration[MENU_DATA_SIZE + 1]; +uint16_t g_scan_duration; + +uint64_t g_extended_address; +thread_scan_param_h g_param_handle; +const char* g_uuid; + +static void __thread_scan_callback(int result, thread_network_scanning_state_e state, + uint64_t ext_address, const char *network_name, uint64_t ext_panidi, + const uint8_t *steering_data, int length, uint16_t panid, uint16_t joiner_udp_port, uint16_t channel, + uint16_t rssi, uint8_t lqi, uint8_t version, bool is_native, bool is_joinable, void *user_data) { + + msg("Thread scan callback, result: %s", _thread_get_error_message(result)); + + if (state == THREAD_SCANNING_STARTED) + msg("The scanning state is: STARTED"); + else if (state == THREAD_SCANNING_FINISHED) + msg("The scanning state is: STOPPED"); + else { + msg("thread_network_scanning_state: [%d]", state); + msg("network_name: [%s]", network_name); + msg("ext_address: [%zu]", (size_t)ext_address); + msg("ext_panidi: [%zu]", (size_t)ext_panidi); + msg("steering_data: [%s]", steering_data); + msg("length: [%d]", length); + msg("panid: [%hu]", panid); + msg("joiner_udp_port: [%hu]", joiner_udp_port); + msg("channel: [%hu]", channel); + msg("rssi: [%hu]", rssi); + msg("lqi: [%hu]", lqi); + msg("version: [%hu]", version); + msg("is_native: [%d]", is_native); + msg("is_joinable: [%d]", is_joinable); + } + + msg("Callback exit"); + + return; +} + +void thread_device_role_callback(thread_device_role_e device_role, void *user_data) +{ + FUNC_ENTRY; + THREAD_DBG("Device Role is [%d]", device_role); + FUNC_EXIT; +} + + +static int run_thread_enable(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h *g_instance = &(mm->t_instance); + + int ret = thread_enable(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("successfully enabled"); + else + msg("enabled failed"); + thread_set_device_role_changed_cb(g_instance, thread_device_role_callback, NULL); + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_disable(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_disable(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("successfully disabled"); + else + msg("disabled failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_start(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_start(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("successfully started"); + else + msg("start failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_stop(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_stop(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("successfully stopped"); + else + msg("stop failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_reset(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_reset(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("successfully reset"); + else + msg("reset failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_factory_reset(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_factoryreset(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("factoryreset success"); + else + msg("factoryreset failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_get_device_role(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_get_device_role(g_instance, &g_device_role); + if (ret == THREAD_ERROR_NONE) + msg("thread_get_device_role success"); + else + msg("thread_get_device_role failed"); + + msg("- thread_get_device_role() ret: Device role->[%s]", _thread_device_role_to_string(g_device_role)); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_get_device_type(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_get_device_type(g_instance, &g_device_type); + if (ret == THREAD_ERROR_NONE) + msg("thread_get_device_type success"); + else + msg("thread_get_device_type failed"); + + msg("-thread_get_device_type() ret: Device type->[%s]", _thread_device_type_to_string(g_device_type)); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_set_device_type(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + g_device_type = (thread_device_type_e)atoi(g_str_device_type); + + int ret = thread_set_device_type(g_instance, g_device_type); + if (ret == THREAD_ERROR_NONE) + msg("thread_get_device_type success"); + else + msg("thread_get_device_type failed"); + + msg("- thread_set_device_type() ret: Device type->[%s]", _thread_device_type_to_string(g_device_type)); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_scan_param_create(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + g_scan_duration = (uint16_t)atoi(g_str_scan_duration); + + int ret = thread_scan_param_create(g_instance, g_scan_duration, &g_param_handle); + if (ret == THREAD_ERROR_NONE) + msg("thread_scan_param_create success"); + else + msg("thread_scan_param_create failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_scan_param_destroy(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_scan_param_destroy(g_instance, g_param_handle); + if (ret == THREAD_ERROR_NONE) + msg("thread_scan_param_distroy success"); + else + msg("thread_scan_param_distroy failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_scan(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = THREAD_ERROR_NONE; + + if (!g_param_handle) { + ret = thread_scan_param_create(g_instance, THREAD_DEFAULT_SCAN_TIME, &g_param_handle); + if (ret == THREAD_ERROR_NONE) + msg("thread_scan_param_create success"); + else { + msg("thread_scan_param_create failed"); + goto OUT; + } + } + + ret = thread_scan(g_instance, g_param_handle, __thread_scan_callback, NULL); + if (ret == THREAD_ERROR_NONE) + msg("thread_scan success"); + else + msg("thread_scan failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_get_extended_address(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_get_extended_address(g_instance, &g_extended_address); + if (ret == THREAD_ERROR_NONE) { + msg("thread_get_extended_address success"); + msg("- thread_get_exenteded_address() ret: Device extended address->[%zu]", + (size_t)g_extended_address); + } else + msg("thread_get_extended_address failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_get_extended_uuid(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_get_extended_uuid(g_instance, &g_uuid); + if (ret == THREAD_ERROR_NONE) { + msg("thread_get_extended_uuid success"); + msg("- thread_get_exenteded_uuid() ret: Device extended uuid->[%s]", g_uuid); + } else + msg("thread_get_extended_uuid failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static struct menu_data menu_thread_set_device_type[] = { + { "1", "type(1: Router 2: FED(full end device) 3: MED(minimal end device) 4: SED(sleepy end device)", + NULL, NULL, g_str_device_type }, + { "2", "run", NULL, + run_thread_set_device_type, NULL }, + { NULL, NULL, }, +}; + +static struct menu_data menu_thread_scan_param_create[] = { + { "1", "duration", + NULL, NULL, g_str_scan_duration }, + { "2", "run", NULL, + run_thread_scan_param_create, NULL }, + { NULL, NULL, }, +}; + +struct menu_data menu_thread_core[] = { + { "1", "thread_enable", + NULL, run_thread_enable, NULL}, + { "2", "thread_disable", + NULL, run_thread_disable, NULL }, + { "3", "thread_start", + NULL, run_thread_start, NULL }, + { "4", "thread_stop", + NULL, run_thread_stop, NULL }, + { "5", "thread_reset", + NULL, run_thread_reset, NULL }, + { "6", "thread_factoryreset", + NULL, run_factory_reset, NULL }, + { "7", "thread_get_device_role", + NULL, run_thread_get_device_role, NULL }, + { "8", "thread_get_device_type", + NULL, run_thread_get_device_type, NULL }, + { "9", "thread_set_device_type", + menu_thread_set_device_type, NULL, NULL }, + { "10", "thread_scan_param_create", + menu_thread_scan_param_create, NULL, NULL }, + { "11", "thread_scan_param_destroy", + NULL, run_thread_scan_param_destroy, NULL }, + { "12", "thread_scan", + NULL, run_thread_scan, NULL }, + { "13", "thread_get_extended_address", + NULL, run_thread_get_extended_address, NULL }, + { "14", "thread_get_extended_uuid", + NULL, run_thread_get_extended_uuid, NULL }, + { NULL, NULL, }, +}; diff --git a/tests/thread-test/thread-init.c b/tests/thread-test/thread-init.c new file mode 100755 index 0000000..a8b2733 --- /dev/null +++ b/tests/thread-test/thread-init.c @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 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 +#include +#include + +#include +#include + +#include "thread.h" +#include "thread-menu.h" + +char g_selected_user_idx[MENU_DATA_SIZE + 1] = {0,}; + + +static int run_thread_init(MManager *mm, struct menu_data *menu) +{ + msg("thread_initialize started..."); + + int ret = thread_initialize(); + + if (ret == THREAD_ERROR_NOT_SUPPORTED) + msg("thread_initialize_completed"); + else + msg("thread_initalize_failed"); + + return RET_SUCCESS; +} + +struct menu_data menu_thread_init[] = { + { "1", "Run Init", NULL, run_thread_init, NULL}, + { NULL, NULL, }, +}; diff --git a/tests/thread-test/thread-joiner.c b/tests/thread-test/thread-joiner.c new file mode 100644 index 0000000..c71e0c3 --- /dev/null +++ b/tests/thread-test/thread-joiner.c @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "thread.h" +#include "thread-menu.h" +#include "thread-type.h" +#include "thread-log.h" +#include "thread-util.h" + +static char g_network_key[MENU_DATA_SIZE + 1]; +static char g_pskd[MENU_DATA_SIZE + 1]; +static char g_prov_url[MENU_DATA_SIZE + 1]; +static char g_vendor_name[MENU_DATA_SIZE + 1]; +static char g_vendor_model[MENU_DATA_SIZE + 1]; +static char g_vendor_sw_version[MENU_DATA_SIZE + 1]; +static char g_vendor_data[MENU_DATA_SIZE + 1]; +static char g_panid[MENU_DATA_SIZE + 1]; + +static void __thread_joiner_start_callback(int result, void* user_data) +{ + msg("Thread joiner start callback, result: %s", _thread_get_error_message(result)); + + return; +} +static int run_thread_joiner_join_by_network_key(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_joiner_join_by_network_key(g_instance, g_network_key, g_panid); + if (ret == THREAD_ERROR_NONE) + msg("thread_joiner_join_by_networkkey success"); + else + msg("thread_joiner_join_by_networkkey failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_joiner_stop(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_joiner_stop(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("thread_joiner_stop success"); + else + msg("thread_joiner_stop failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_joiner_start(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_joiner_start(g_instance, g_pskd, g_prov_url, + g_vendor_name, g_vendor_model, g_vendor_sw_version, g_vendor_data, + __thread_joiner_start_callback, NULL); + + if (ret == THREAD_ERROR_NONE) + msg("thread_joiner_start success"); + else + msg("thread_joiner_start failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static struct menu_data menu_thread_joiner_join_by_network_key[] = { + { "1", "Type network_key", NULL, NULL, g_network_key}, + { "2", "Type panid", NULL, NULL, g_panid}, + { "3", "run", NULL, run_thread_joiner_join_by_network_key, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_thread_joiner_start[] = { + { "1", "Type Pass key", NULL, NULL, g_pskd}, + { "2", "Type Prov url", NULL, NULL, g_prov_url}, + { "3", "Type Vendor Name", NULL, NULL, g_vendor_name}, + { "4", "Type Vendor Model", NULL, NULL, g_vendor_model}, + { "5", "Type Vendor sw_version", NULL, NULL, g_vendor_sw_version}, + { "6", "Type Vendor data", NULL, NULL, g_vendor_data}, + { "7", "run", NULL, run_thread_joiner_start, NULL}, + { NULL, NULL, }, +}; +struct menu_data menu_thread_joiner[] = { + { "1", "thread_joiner_join_by_networkkey", + menu_thread_joiner_join_by_network_key, NULL, NULL}, + { "2", "thread_joiner_start", + menu_thread_joiner_start, NULL, NULL }, + { "3", "thread_joiner_stop", + NULL, run_thread_joiner_stop, NULL }, + { NULL, NULL, }, +}; diff --git a/tests/thread-test/thread-main.c b/tests/thread-test/thread-main.c new file mode 100755 index 0000000..7c4fced --- /dev/null +++ b/tests/thread-test/thread-main.c @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "thread.h" +#include "thread-menu.h" + + +extern struct menu_data menu_thread_init[]; +extern struct menu_data menu_thread_core[]; +extern struct menu_data menu_thread_joiner[]; +extern struct menu_data menu_thread_commissioner[]; +extern struct menu_data menu_thread_network[]; +extern struct menu_data menu_thread_br[]; +extern struct menu_data menu_thread_srp[]; +extern struct menu_data menu_thread_scenarios[]; + +static struct menu_data menu_main[] = { + { "1", "Core", menu_thread_core, NULL, NULL}, + { "2", "Joiner", menu_thread_joiner, NULL, NULL}, + { "3", "Commissioner", menu_thread_commissioner, NULL, NULL}, + { "4", "Network", menu_thread_network, NULL, NULL}, + { "5", "Border Router", menu_thread_br, NULL, NULL}, + { "6", "SRP", menu_thread_srp, NULL, NULL}, + { "7", "Scenarios", menu_thread_scenarios, NULL, NULL}, + { NULL, NULL, }, +}; + +static int __init_func(MManager *mm, struct menu_data *menu) +{ + msg("thread_initialize started..."); + + int ret = thread_initialize(); + + if (ret == THREAD_ERROR_NONE) + msg("thread_initialize_completed"); + else + msg("thread_initalize_failed"); + + return RET_SUCCESS; +} + +static gboolean __create_init_menu(struct menu_data init_menu[1]) +{ + init_menu[0].key = "1"; + init_menu[0].title = "Init"; + init_menu[0].sub_menu = menu_main; + init_menu[0].callback = __init_func; + init_menu[0].data = NULL; + + return TRUE; +} + +static void __deinit_func(void) +{ + msg("thread_deinit started..."); + + int ret = thread_deinitialize(); + + if (ret == THREAD_ERROR_NONE) + msg("thread_deinit_completed"); + else + msg("thread_deinit_failed"); + + return; +} + +int main(int arg, char **argv) +{ + GMainLoop *mainloop = NULL; + GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO); + MManager *manager; + struct menu_data init_menu[1+1] = { {NULL, NULL, } }; + +#if !GLIB_CHECK_VERSION(2, 35, 0) + g_type_init(); +#endif + mainloop = g_main_loop_new(NULL, FALSE); + + msg(""); + msg("* Thread test application "); + msg("* Build On: %s %s", __DATE__, __TIME__); + + if (__create_init_menu(init_menu) == FALSE) + goto OUT; + + manager = menu_manager_new(init_menu, mainloop); + if (!manager) + goto OUT; + + menu_manager_run(manager); + + g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL), + on_menu_manager_keyboard, manager); + g_main_loop_run(mainloop); + thread_disable(manager->t_instance); + +OUT: + __deinit_func(); + menu_manager_free(manager); + + g_main_loop_unref(mainloop); + msg("******* Bye bye *******"); + + return 0; +} diff --git a/tests/thread-test/thread-menu.c b/tests/thread-test/thread-menu.c new file mode 100755 index 0000000..7675fba --- /dev/null +++ b/tests/thread-test/thread-menu.c @@ -0,0 +1,333 @@ +/* + * Copyright (c) 2021 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. + * + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include "thread-menu.h" + +#define DEFAULT_MENU_MENU "m" +#define DEFAULT_MENU_NONE "t" +#define DEFAULT_MENU_PREV "p" +#define DEFAULT_MENU_QUIT "q" + +#define TAB_SPACE " " + +char key_buffer[MENU_DATA_SIZE]; +int flag_pid_display = 1; + +char g_thread_interface_str[MENU_DATA_SIZE + 1] = "thread.default.interface"; + +pid_t get_tid() +{ + return syscall(__NR_gettid); +} + +void hide_pid() +{ + flag_pid_display = 0; +} + +void show_pid() +{ + flag_pid_display = 1; +} + + +static void _show_prompt(void) +{ + msgn("(%5d) >> ", get_tid()); +} + +static void _show_reserved_menu(void) +{ + msg(ANSI_COLOR_DARKGRAY HR_SINGLE2 ANSI_COLOR_NORMAL); + + msg(ANSI_COLOR_DARKGRAY " [ " ANSI_COLOR_NORMAL "%s" ANSI_COLOR_DARKGRAY + " ] " ANSI_COLOR_NORMAL "Thread default interface [" ANSI_COLOR_YELLOW "%s" ANSI_COLOR_NORMAL + " ] ", DEFAULT_MENU_NONE, g_thread_interface_str); + + msg(ANSI_COLOR_DARKGRAY HR_SINGLE2 ANSI_COLOR_NORMAL); + + + msg(ANSI_COLOR_DARKGRAY HR_SINGLE2 ANSI_COLOR_NORMAL); + msg(ANSI_COLOR_DARKGRAY " [ " ANSI_COLOR_NORMAL "%s" ANSI_COLOR_DARKGRAY + " ] " ANSI_COLOR_NORMAL "Previous menu " , DEFAULT_MENU_PREV); + msg(ANSI_COLOR_DARKGRAY " [ " ANSI_COLOR_NORMAL "%s" ANSI_COLOR_DARKGRAY + " ] " ANSI_COLOR_NORMAL "Show Menu " , DEFAULT_MENU_MENU); + msg(ANSI_COLOR_DARKGRAY " [ " ANSI_COLOR_NORMAL "%s" ANSI_COLOR_DARKGRAY + " ] " ANSI_COLOR_NORMAL "Quit " , DEFAULT_MENU_QUIT); +} + +static void _show_input_ok(void) +{ + msg("OK."); +} + +static void _show_menu(MManager *m, struct menu_data menu[]) +{ + int i = 0; + int len = 0; + struct menu_data *item; + char title_buf[256] = { 0, }; + + if (!menu) + return; + + msg(""); + msg(HR_DOUBLE); + + len = g_queue_get_length(m->title_stack); + msgn(ANSI_COLOR_YELLOW " Main"); + if (len > 0) { + for (i = 0; i < len; i++) { + msgn(ANSI_COLOR_NORMAL " >> " ANSI_COLOR_YELLOW "%s", + (char *)g_queue_peek_nth(m->title_stack, i)); + } + } + msg(ANSI_COLOR_NORMAL); + msg(HR_SINGLE); + + hide_pid(); + i = 0; + + while (1) { + item = menu + i; + if (item->key == NULL) + break; + + if (!g_strcmp0(item->key, "-")) { + msgn(" "); + } else if (!g_strcmp0(item->key, "_")) { + msg(ANSI_COLOR_DARKGRAY HR_SINGLE2 ANSI_COLOR_NORMAL); + + if (item->callback) + item->callback(m, item); + + i++; + + continue; + } else if (!g_strcmp0(item->key, "*")) { + msg(" %s", item->title); + if (item->callback) + item->callback(m, item); + } else { + msgn(ANSI_COLOR_DARKGRAY " [" ANSI_COLOR_NORMAL "%3s" + ANSI_COLOR_DARKGRAY "] " ANSI_COLOR_NORMAL, item->key); + } + + memset(title_buf, 0, 256); + if (item->title) { + snprintf(title_buf, MENU_DATA_SIZE, "%s", item->title); + + if (strlen(item->title) >= MAX_TITLE) { + title_buf[MAX_TITLE - 2] = '.'; + title_buf[MAX_TITLE - 1] = '.'; + } + } + + if (item->data) { + msg("%s " ANSI_COLOR_LIGHTBLUE "(%s)" ANSI_COLOR_NORMAL, + title_buf, item->data); + } else if (!g_strcmp0(item->key, "*")) { + /* none */ + } else { + msg("%s", title_buf); + } + + if (item->sub_menu) + msg("\e[1A\e[%dC >", (int)POS_MORE); + + i++; + } + + show_pid(); + + _show_reserved_menu(); + + msg(HR_DOUBLE); + + _show_prompt(); +} + +static void _show_item_data_input_msg(struct menu_data *item) +{ + msg(""); + msg(HR_DOUBLE); + msg(" Input [%s] data ", item->title); + msg(HR_SINGLE); + msg(" current = [%s]", item->data); + msgn(" new >> "); +} + +static void _move_menu(MManager *mm, struct menu_data menu[], char *key) +{ + struct menu_data *item; + int i = 0; + + if (!mm->menu) + return; + + if (!g_strcmp0(DEFAULT_MENU_PREV, key)) { + if (g_queue_get_length(mm->stack) > 0) { + mm->menu = g_queue_pop_tail(mm->stack); + g_queue_pop_tail(mm->title_stack); + } + + _show_menu(mm, mm->menu); + mm->buf = key_buffer; + + return; + } else if (!g_strcmp0(DEFAULT_MENU_MENU, key)) { + _show_menu(mm, mm->menu); + return; + } else if (!g_strcmp0(DEFAULT_MENU_QUIT, key)) { + g_main_loop_quit(mm->mainloop); + return; + } + + while (1) { + int ret = RET_SUCCESS; + item = menu + i; + if (item->key == NULL) + break; + + if (!g_strcmp0(item->key, key)) { + if (item->callback) { + ret = item->callback(mm, item); + _show_prompt(); + } + if (RET_SUCCESS != ret) + return; + + if (item->sub_menu) { + g_queue_push_tail(mm->stack, mm->menu); + g_queue_push_tail(mm->title_stack, (gpointer *)item->title); + + mm->menu = item->sub_menu; + _show_menu(mm, mm->menu); + mm->buf = key_buffer; + } + + if (NULL == item->sub_menu && item->data) { + _show_item_data_input_msg(item); + mm->buf = item->data; + } + + return; + } + + i++; + } + + _show_prompt(); +} + +MManager *menu_manager_new(struct menu_data items[], GMainLoop *mainloop) +{ + MManager *mm; + + mm = calloc(sizeof(struct menu_manager), 1); + if (!mm) + return NULL; + + mm->stack = g_queue_new(); + mm->title_stack = g_queue_new(); + mm->menu = items; + mm->mainloop = mainloop; + mm->t_instance = NULL; + return mm; +} + +int menu_manager_free(MManager *mm) +{ + if (mm) { + if (mm->stack) + g_queue_free(mm->stack); + if (mm->title_stack) + g_queue_free(mm->title_stack); + free(mm); + } + return 0; +} + +int menu_manager_run(MManager *mm) +{ + _show_menu(mm, mm->menu); + + mm->buf = key_buffer; + + return 0; +} + +int menu_manager_set_user_data(MManager *mm, void *user_data) +{ + if (!mm) + return -1; + + mm->user_data = user_data; + + return 0; +} + +void *menu_manager_ref_user_data(MManager *mm) +{ + if (!mm) + return NULL; + + return mm->user_data; +} + +gboolean on_menu_manager_keyboard(GIOChannel *src, GIOCondition con, + gpointer data) +{ + MManager *mm = data; + char local_buf[MENU_DATA_SIZE + 1] = { 0, }; + + if (fgets(local_buf, MENU_DATA_SIZE, stdin) == NULL) + return TRUE; + + if (strlen(local_buf) > 0) { + if (local_buf[strlen(local_buf) - 1] == '\n') + local_buf[strlen(local_buf) - 1] = '\0'; + } + + if (mm->buf == key_buffer) { + if (strlen(local_buf) < 1) { + _show_prompt(); + return TRUE; + } + + _move_menu(mm, mm->menu, local_buf); + } else { + if (mm->buf) { + memset(mm->buf, 0, MENU_DATA_SIZE); + memcpy(mm->buf, local_buf, MENU_DATA_SIZE); + _show_input_ok(); + } + mm->buf = key_buffer; + _move_menu(mm, mm->menu, (char *)DEFAULT_MENU_MENU); + } + + return TRUE; +} diff --git a/tests/thread-test/thread-menu.h b/tests/thread-test/thread-menu.h new file mode 100755 index 0000000..c871fc3 --- /dev/null +++ b/tests/thread-test/thread-menu.h @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2021 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 __MENU_H__ +#define __MENU_H__ + +__BEGIN_DECLS + +#define ANSI_COLOR_NORMAL "\e[0m" + +#define ANSI_COLOR_BLACK "\e[0;30m" +#define ANSI_COLOR_RED "\e[0;31m" +#define ANSI_COLOR_GREEN "\e[0;32m" +#define ANSI_COLOR_BROWN "\e[0;33m" +#define ANSI_COLOR_BLUE "\e[0;34m" +#define ANSI_COLOR_MAGENTA "\e[0;35m" +#define ANSI_COLOR_CYAN "\e[0;36m" +#define ANSI_COLOR_LIGHTGRAY "\e[0;37m" + +#define ANSI_COLOR_DARKGRAY "\e[1;30m" +#define ANSI_COLOR_LIGHTRED "\e[1;31m" +#define ANSI_COLOR_LIGHTGREEN "\e[1;32m" +#define ANSI_COLOR_YELLOW "\e[1;33m" +#define ANSI_COLOR_LIGHTBLUE "\e[1;34m" +#define ANSI_COLOR_LIGHTMAGENTA "\e[1;35m" +#define ANSI_COLOR_LIGHTCYAN "\e[1;36m" +#define ANSI_COLOR_WHITE "\e[1;37m" + + +#define msg(fmt, args...) do { fprintf(stdout, fmt "\n", ##args); \ + fflush(stdout); } while (0) +#define msgn(fmt, args...) do { fprintf(stdout, fmt, ##args); \ + fflush(stdout); } while (0) + +/* Bold (red) */ +#define msgr(fmt, args...) do { fprintf(stdout, ANSI_COLOR_RED fmt \ + ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) + +/* Bold (green) */ +#define msgb(fmt, args...) do { fprintf(stdout, ANSI_COLOR_LIGHTGREEN fmt \ + ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) + +/* Property message */ +#define msgp(fmt, args...) do { fprintf(stdout, ANSI_COLOR_LIGHTMAGENTA fmt \ + ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) + +/* Bold (Cyan) */ +#define msgc(fmt, args...) do { fprintf(stdout, ANSI_COLOR_CYAN fmt \ + ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) + +/* Bold (Yellow) */ +#define msgy(fmt, args...) do { fprintf(stdout, ANSI_COLOR_YELLOW fmt \ + ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) + +/* Bold (Light Red) */ +#define msglr(fmt, args...) do { fprintf(stdout, ANSI_COLOR_LIGHTRED fmt \ + ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) + +/* Bold (Blue) */ +#define msgbr(fmt, args...) do { fprintf(stdout, ANSI_COLOR_BLUE fmt \ + ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) + + +#define msgt(n, fmt, args...) do { fprintf(stdout, "\e[%dC" fmt "\n", \ + 3 + ((n) * 2), ##args); fflush(stdout); } while (0) + +#define pmsg(fmt, args...) do { \ + if (is_pid_show()) fprintf(stdout, "(%5d) ", get_tid()); \ + fprintf(stdout, fmt "\n", ##args); fflush(stdout); } while (0) + +#define pmsgb(fmt, args...) do { \ + if (is_pid_show()) fprintf(stdout, "(%5d) ", get_tid()); \ + fprintf(stdout, ANSI_COLOR_LIGHTGREEN fmt \ + ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) + +#define pmsgt(n, fmt, args...) do { \ + if (is_pid_show()) fprintf(stdout, "(%5d) ", get_tid()); \ + fprintf(stdout, "\e[%dC" fmt "\n", \ + 3 + ((n) * 2), ##args); fflush(stdout); } while (0) + +#define ret_if(expr) \ +do { \ + if (expr) { \ + msgr("(%s) return", #expr); \ + return; \ + } \ +} while (0) + +#define retv_if(expr, val) \ +do { \ + if (expr) { \ + msgr("(%s) return", #expr); \ + return (val); \ + } \ +} while (0) + +/* Null check */ +#define check_if(expr) \ +do { \ + if (expr) { \ + msgr("(%s) ", #expr); \ + } \ +} while (0) + +#define MENU_DATA_SIZE 255 +#define NETWORK_DATA_SIZE 256 +/* + * Horizontal Line - width: 65 + * .12345678901234567890123456789012345678901234567890. + */ +#define HR_SINGLE "----------------------------------------" \ + "-------------------------" +#define HR_DOUBLE "========================================" \ + "=========================" +#define HR_SINGLE2 " ---------------------------------------" \ + "------------------------ " + +#define MAX_WIDTH strlen(HR_SINGLE) +#define MAX_TITLE ((MAX_WIDTH) - 10) +#define POS_MORE ((MAX_WIDTH) - 3) +#define RET_SUCCESS 0 +#define RET_FAILURE -1 + +#include "thread-log.h" +#include "thread-util.h" + +struct menu_manager { + GQueue *stack; + GQueue *title_stack; + struct menu_data *menu; + char *buf; + void *user_data; + GMainLoop *mainloop; + thread_instance_h t_instance; +}; +typedef struct menu_manager MManager; + +struct menu_data { + const char *key; + const char *title; + struct menu_data *sub_menu; + int (*callback)(MManager *mm, struct menu_data *menu); + char *data; +}; + +MManager* menu_manager_new(struct menu_data items[], GMainLoop *mainloop); +int menu_manager_run(MManager *mm); +int menu_manager_set_user_data(MManager *mm, void *user_data); +void* menu_manager_ref_user_data(MManager *mm); +int menu_manager_free(MManager *mm); + +gboolean on_menu_manager_keyboard(GIOChannel *src, GIOCondition con, + gpointer data); + +pid_t get_tid(); +void hide_pid(); +void show_pid(); +int is_pid_show(); +void menu_print_dump(int data_len, void *data); + +__END_DECLS + +#endif diff --git a/tests/thread-test/thread-network.c b/tests/thread-test/thread-network.c new file mode 100644 index 0000000..eb3a3ba --- /dev/null +++ b/tests/thread-test/thread-network.c @@ -0,0 +1,339 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "thread.h" +#include "thread-menu.h" +#include "thread-type.h" +#include "thread-log.h" +#include "thread-util.h" + +static char g_name[NETWORK_DATA_SIZE + 1] = {"Thread-test"}; +static char g_key[NETWORK_DATA_SIZE + 1]; +static char g_pskc[NETWORK_DATA_SIZE + 1]; +static char g_str_channel[NETWORK_DATA_SIZE + 1] = {"07fff800"}; +static char g_str_extended_panid[NETWORK_DATA_SIZE + 1] = {"18446744073709551615"}; /* UINT64_MAX */ +static char g_str_panid[NETWORK_DATA_SIZE + 1] = {"65535"}; /* UINT16_MAX */ +static char g_str_buf_length[NETWORK_DATA_SIZE + 1]; +static char g_str_tlvs_buffer[2*NETWORK_DATA_SIZE + 1]; +static thread_network_h g_network; + +uint32_t g_channel; +uint64_t g_extended_panid; +uint16_t g_panid; + +static char g_str_ipaddr_type[NETWORK_DATA_SIZE + 1] = "0"; +static char g_str_ipaddr[NETWORK_DATA_SIZE + 1]; + +uint8_t g_ipv6_address[THREAD_IPV6_ADDRESS_SIZE]; + +static int run_thread_network_destroy_operational_network(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_network_destroy_operational_network(g_instance, g_network); + if (ret == THREAD_ERROR_NONE) + msg("thread_network_destroy_operational_network success"); + else + msg("thread_network_destroy_operational_network failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_network_create_operational_network(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + sscanf(g_str_channel, "%x", &g_channel); + sscanf(g_str_extended_panid, "%zu", (size_t *)&g_extended_panid); + sscanf(g_str_panid, "%hu", &g_panid); + + int ret = thread_network_create_operational_network(g_instance, g_name, g_key, + g_pskc, g_channel, g_extended_panid, g_panid, &g_network); + if (ret == THREAD_ERROR_NONE) + msg("thread_network_create_operational_network success"); + else + msg("thread_network_create_operational_network failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_network_set_active_dataset_tlvs(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int buf_length; + uint8_t tlvs_buffer[1024]; + + sscanf(g_str_buf_length, "%d", &buf_length); + msg("Set active dataset tlvs size: %d :: %s", + buf_length, g_str_tlvs_buffer); + + if (!buf_length || buf_length > (MENU_DATA_SIZE + 1)) { + msg("Invalid Input"); + goto OUT; + } + for (int i = 0; i < buf_length; i++) { + char subbuff[3]; + memcpy(subbuff, &g_str_tlvs_buffer[2*i], 2); + subbuff[2] = '\0'; + tlvs_buffer[i] = (uint8_t)strtol(subbuff, NULL, 16); + } + + int ret = thread_network_set_active_dataset_tlvs( + g_instance, tlvs_buffer, buf_length); + if (ret == THREAD_ERROR_NONE) + msg("thread_network_set_active_dataset_tlvs success"); + else + msg("thread_network_set_active_dataset_tlvs failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_network_get_active_dataset_tlvs(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + + uint8_t *tlvs_buffer = NULL; + char buf[1024]; + int buf_length; + + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_network_get_active_dataset_tlvs(g_instance, &tlvs_buffer, &buf_length); + if (ret == THREAD_ERROR_NONE) { + msg("thread_network_get_active_dataset_tlvs success"); + + for (int i = 0; i < buf_length; i++) + snprintf(buf + i*2, 3, "%2.2x", tlvs_buffer[i]); + + msg("Active dataset tlvs size: %d :: %s", buf_length, buf); + } else { + msg("thread_network_get_active_dataset_tlvs failed"); + } +OUT: + if (tlvs_buffer) + free(tlvs_buffer); + + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_network_attach(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_network_attach(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("thread_network_attach success"); + else + msg("thread_network_attach failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_network_detach(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_network_detach(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("thread_network_detach success"); + else + msg("thread_network_detach failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static void __get_ipaddr_callback(int index, char* ipaddr, + thread_ipaddr_type_e ipaddr_type, void *user_data) { + FUNC_ENTRY; + + if (index == 1) + msg("IP address type: %s", _thread_ipaddr_type_to_string(ipaddr_type)); + msg("IP address count: %d, ipv6 address: %s", index, ipaddr); + + FUNC_EXIT; +} + +static int run_thread_get_ipaddr(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int type = atoi(g_str_ipaddr_type); + thread_ipaddr_type_e ipaddr_type; + switch (type) { + case 0: + ipaddr_type = THREAD_IPADDR_TYPE_ALL; + break; + case 1: + ipaddr_type = THREAD_IPADDR_TYPE_LINK_LOCAL; + break; + case 2: + ipaddr_type = THREAD_IPADDR_TYPE_RLOC; + break; + case 3: + ipaddr_type = THREAD_IPADDR_TYPE_MLEID; + break; + default: + goto OUT; + } + + int ret = thread_get_ipaddr(g_instance, __get_ipaddr_callback, ipaddr_type, NULL); + if (ret == THREAD_ERROR_NONE) + msg("thread_get_ipaddr success"); + else + msg("thread_get_ipaddr failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_add_ipaddr(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = _preprocess_ipv6_prefix(g_str_ipaddr, g_ipv6_address); + retv_if(ret != THREAD_ERROR_NONE, ret); + + ret = thread_add_ipaddr(g_instance, g_ipv6_address); + if (ret == THREAD_ERROR_NONE) + msg("thread_add_ipaddr success"); + else + msg("thread_add_ipaddr failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_remove_ipaddr(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = _preprocess_ipv6_prefix(g_str_ipaddr, g_ipv6_address); + retv_if(ret != THREAD_ERROR_NONE, ret); + + ret = thread_remove_ipaddr(g_instance, g_ipv6_address); + if (ret == THREAD_ERROR_NONE) + msg("thread_remove_ipaddr success"); + else + msg("thread_remove_ipaddr failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static struct menu_data menu_thread_network_set_active_dataset_tlvs[] = { + { "1", "Tlvs_buffer len", NULL, NULL, g_str_buf_length}, + { "2", "Tlvs_buffer", NULL, NULL, g_str_tlvs_buffer}, + { "3", "run", NULL, run_thread_network_set_active_dataset_tlvs, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_thread_network_create_operational_network[] = { + { "1", "Network Name", NULL, NULL, g_name}, + { "2", "Network Key", NULL, NULL, g_key}, + { "3", "Network pskc", NULL, NULL, g_pskc}, + { "4", "Network Channel", NULL, NULL, g_str_channel}, + { "5", "Network Extended Panid", NULL, NULL, g_str_extended_panid}, + { "6", "Network panid", NULL, NULL, g_str_panid}, + { "7", "run", NULL, run_thread_network_create_operational_network, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_thread_get_ipaddr[] = { + { "1", "Node Addr Type: ALL > 0, LINK LOCAL > 1, RLOC > 2, MLEID > 3", + NULL, NULL, g_str_ipaddr_type}, + { "2", "run", NULL, run_thread_get_ipaddr, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_thread_add_ipaddr[] = { + { "1", "Type IPv6 address", NULL, NULL, g_str_ipaddr}, + { "2", "run", NULL, run_thread_add_ipaddr, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_thread_remove_ipaddr[] = { + { "1", "Type IPv6 address", NULL, NULL, g_str_ipaddr}, + { "2", "run", NULL, run_thread_remove_ipaddr, NULL}, + { NULL, NULL, }, +}; + +struct menu_data menu_thread_network[] = { + { "1", "thread_network_create_operational_network", + menu_thread_network_create_operational_network, NULL, NULL}, + { "2", "thread_network_destroy_operational_network", + NULL, run_thread_network_destroy_operational_network, NULL }, + { "3", "thread_network_set_active_dataset_tlvs", + menu_thread_network_set_active_dataset_tlvs, NULL, NULL}, + { "4", "thread_network_get_active_dataset_tlvs", + NULL, run_thread_network_get_active_dataset_tlvs, NULL }, + { "5", "thread_network_attach", + NULL, run_thread_network_attach, NULL}, + { "6", "thread_network_detach", + NULL, run_thread_network_detach, NULL}, + { "7", "thread_get_ipaddr", + menu_thread_get_ipaddr, NULL, NULL}, + { "8", "thread_add_ipaddr", + menu_thread_add_ipaddr, NULL, NULL}, + { "9", "thread_remove_ipaddr", + menu_thread_remove_ipaddr, NULL, NULL}, + { NULL, NULL, }, +}; + diff --git a/tests/thread-test/thread-srp.c b/tests/thread-test/thread-srp.c new file mode 100644 index 0000000..8b50389 --- /dev/null +++ b/tests/thread-test/thread-srp.c @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "thread.h" +#include "thread-menu.h" +#include "thread-type.h" +#include "thread-log.h" +#include "thread-util.h" + +const char *service_name; +const char *address; +uint64_t port; +bool is_deleted; +uint64_t g_port; + +static char g_host_address[MENU_DATA_SIZE + 1]; +static char g_service_name[MENU_DATA_SIZE + 1]; +static char g_service_type[MENU_DATA_SIZE + 1]; +static char g_str_port[MENU_DATA_SIZE + 1]; +static char g_host_name[MENU_DATA_SIZE + 1]; + +static int __run_thread_srp_client_start(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_srp_client_start(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("thread_srp_client_start success"); + else + msg("thread_srp_client_start failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int __run_thread_srp_client_stop(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_srp_client_stop(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("thread_srp_client_stop success"); + else + msg("thread_srp_client_stop failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int __run_thread_srp_client_remove_host(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_srp_client_remove_host(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("thread_srp_client_remove_host success"); + else + msg("thread_srp_client_remove_host failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int __run_thread_srp_client_set_host_name(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_srp_client_set_host_name(g_instance, g_host_name); + if (ret == THREAD_ERROR_NONE) + msg("thread_srp_client_set_host_name success"); + else + msg("thread_srp_client_set_host_name failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int __run_thread_srp_client_set_host_address(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_srp_client_set_host_address(g_instance, g_host_address); + if (ret == THREAD_ERROR_NONE) + msg("thread_srp_client_set_host_address success"); + else + msg("thread_srp_client_set_host_address failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int __run_thread_srp_client_register_service(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + g_port = (uint64_t)strtoll(g_str_port, (char **)NULL, 10); + + int ret = thread_srp_client_register_service(g_instance, + g_service_name, g_service_type, g_port); + if (ret == THREAD_ERROR_NONE) + msg("thread_srp_client_register_service success"); + else + msg("thread_srp_client_register_service failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int __run_thread_srp_server_start(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_srp_server_start(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("thread_srp_server_start success"); + else + msg("thread_srp_server_start failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int __run_thread_srp_server_stop(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_srp_server_stop(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("thread_srp_server_stop success"); + else + msg("thread_srp_server_stop failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int __run_thread_srp_server_get_registered_service(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + int ret = thread_srp_server_get_registered_service(g_instance, &service_name, &address, &port, &is_deleted); + + if (ret == THREAD_ERROR_NONE) { + msg("thread_srp_get_registered_service success"); + msg("service: [%s], [%s], [%zu], [%s]", service_name, address, (size_t)port, is_deleted ? "TRUE" : "FALSE"); + } else + msg("thread_srp_get_registered_service failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static struct menu_data menu_thread_srp_client_set_host_name[] = { + { "1", "Type host_name", NULL, NULL, g_host_name}, + { "2", "run", NULL, __run_thread_srp_client_set_host_name, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_thread_srp_client_set_host_address[] = { + { "1", "Type host_address", NULL, NULL, g_host_address}, + { "2", "run", NULL, __run_thread_srp_client_set_host_address, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_thread_srp_client_register_service[] = { + { "1", "Type service_name", NULL, NULL, g_service_name}, + { "2", "Type service_type", NULL, NULL, g_service_type}, + { "3", "Type port", NULL, NULL, g_str_port}, + { "4", "run", NULL, __run_thread_srp_client_register_service, NULL}, + { NULL, NULL, }, +}; + +struct menu_data menu_thread_srp[] = { + + { "1", "thread_srp_client_start", + NULL, __run_thread_srp_client_start, NULL}, + { "2", "thread_srp_client_stop", + NULL, __run_thread_srp_client_stop, NULL }, + { "3", "thread_srp_client_set_host_name", + menu_thread_srp_client_set_host_name, NULL, NULL}, + { "4", "thread_srp_client_remove_host", + NULL, __run_thread_srp_client_remove_host, NULL }, + { "5", "thread_srp_client_set_host_address", + menu_thread_srp_client_set_host_address, NULL, NULL }, + { "6", "thread_srp_client_register_service", + menu_thread_srp_client_register_service, NULL, NULL }, + { "7", "thread_srp_server_start", + NULL, __run_thread_srp_server_start, NULL }, + { "8", "thread_srp_server_stop", + NULL, __run_thread_srp_server_stop, NULL }, + { "9", "thread_srp_server_get_registered_service", + NULL, __run_thread_srp_server_get_registered_service, NULL }, + { NULL, NULL, }, +}; + diff --git a/tests/thread-test/thread-util.c b/tests/thread-test/thread-util.c new file mode 100644 index 0000000..65812cb --- /dev/null +++ b/tests/thread-test/thread-util.c @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include +#include +#include + +#include +#include +#include "thread-log.h" +#include "thread-util.h" + +static thread_instance_h g_instance = NULL; + +bool _thread_check_instance() +{ + if (g_instance == NULL) { + msg("g_instance == NULL"); + return false; + } + return true; +} +thread_instance_h _thread_get_instance(void) +{ + return g_instance; +} + +const char* _thread_device_role_to_string(thread_device_role_e role) +{ + switch (role) { + CASE_TO_STR(THREAD_DEVICE_ROLE_DISABLED); + CASE_TO_STR(THREAD_DEVICE_ROLE_DETACHED); + CASE_TO_STR(THREAD_DEVICE_ROLE_CHILD); + CASE_TO_STR(THREAD_DEVICE_ROLE_ROUTER); + CASE_TO_STR(THREAD_DEVICE_ROLE_LEADER); + default: + return "NOT DEFINED"; + } +} + +const char* _thread_device_type_to_string(thread_device_type_e type) +{ + switch (type) { + CASE_TO_STR(THREAD_DEVICE_TYPE_NOT_SUPPORTED); + CASE_TO_STR(THREAD_DEVICE_TYPE_ROUTER); + CASE_TO_STR(THREAD_DEVICE_TYPE_FULL_END_DEVICE); + CASE_TO_STR(THREAD_DEVICE_TYPE_MINIMAL_END_DEVICE); + CASE_TO_STR(THREAD_DEVICE_TYPE_SLEEPY_END_DEVICE); + default: + return "NOT DEFINED"; + } +} +const char* _thread_ipaddr_type_to_string(thread_ipaddr_type_e type) +{ + switch (type) { + CASE_TO_STR(THREAD_IPADDR_TYPE_ALL); + CASE_TO_STR(THREAD_IPADDR_TYPE_LINK_LOCAL); + CASE_TO_STR(THREAD_IPADDR_TYPE_RLOC); + CASE_TO_STR(THREAD_IPADDR_TYPE_MLEID); + default: + return "NOT DEFINED"; + } +} + +const char* _thread_get_error_message(thread_error_e err) +{ + switch (err) { + CASE_TO_STR(THREAD_ERROR_NONE); + CASE_TO_STR(THREAD_ERROR_NOT_PERMITTED); + CASE_TO_STR(THREAD_ERROR_INVALID_PARAMETER); + CASE_TO_STR(THREAD_ERROR_OUT_OF_MEMORY); + CASE_TO_STR(THREAD_ERROR_RESOURCE_BUSY); + CASE_TO_STR(THREAD_ERROR_TIMED_OUT); + CASE_TO_STR(THREAD_ERROR_CANCELED); + CASE_TO_STR(THREAD_ERROR_NOW_IN_PROGRESS); + CASE_TO_STR(THREAD_ERROR_NOT_SUPPORTED); + CASE_TO_STR(THREAD_ERROR_NO_DATA); + CASE_TO_STR(THREAD_ERROR_NOT_INITIALIZED); + CASE_TO_STR(THREAD_ERROR_NOT_IN_PROGRESS); + CASE_TO_STR(THREAD_ERROR_ALREADY_DONE); + CASE_TO_STR(THREAD_ERROR_OPERATION_FAILED); + CASE_TO_STR(THREAD_ERROR_NOT_READY); + CASE_TO_STR(THREAD_ERROR_NOT_ENABLED); + CASE_TO_STR(THREAD_ERROR_NOT_FOUND); + CASE_TO_STR(THREAD_ERROR_ALREADY_REGISTERED); + CASE_TO_STR(THREAD_ERROR_DB_FAILED); + CASE_TO_STR(THREAD_ERROR_NOT_REGISTERED); + default: + return "NOT DEFINED"; + } +} + +int _preprocess_ipv6_prefix(char *prefix, uint8_t *ipv6_prefix) +{ + FUNC_ENTRY; + struct in6_addr result; + retv_if(inet_pton(AF_INET6, prefix, &result) == 0, + THREAD_ERROR_INVALID_PARAMETER); + + msg("ipv6 string parse successful"); + memcpy(ipv6_prefix, result.s6_addr, + THREAD_IPV6_PREFIX_SIZE*sizeof(uint8_t)); + + FUNC_EXIT; + return THREAD_ERROR_NONE; +} + diff --git a/tests/thread-test/thread-util.h b/tests/thread-test/thread-util.h new file mode 100644 index 0000000..1b79cc1 --- /dev/null +++ b/tests/thread-test/thread-util.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2021 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 __UTIL_H__ +#define __UTIL_H__ + +__BEGIN_DECLS + +#include +#include +#include +#include +#include + +#include +#include + +#include "thread.h" +#include "thread-menu.h" +#include "thread-type.h" +#include "thread-log.h" + +#define CASE_TO_STR(x) case x: return #x; + +#define THREAD_DEFAULT_SCAN_TIME 80 +#define THREAD_IS_DEVICE_NEXT_HOP_DEFAULT TRUE +#define THREAD_IPV6_ADDRESS_SIZE 16 +#define THREAD_IPV6_PREFIX_SIZE 8 + +typedef struct { + thread_route_info_h handle; + uint8_t prefix_address[THREAD_IPV6_ADDRESS_SIZE]; + uint8_t prefix_length; +} thread_route_info_a; + +typedef struct { + thread_onmesh_prefix_info_h handle; + uint8_t prefix_address[THREAD_IPV6_ADDRESS_SIZE]; + uint8_t prefix_length; +} thread_onmesh_prefix_info_a; + +thread_instance_h _thread_get_instance(); +bool _thread_check_instance(); +const char* _thread_device_role_to_string(thread_device_role_e role); +const char* _thread_device_type_to_string(thread_device_type_e type); +const char* _thread_get_error_message(thread_error_e err); +int _preprocess_ipv6_prefix(char *prefix, uint8_t *ipv6_prefix); +const char* _thread_ipaddr_type_to_string(thread_ipaddr_type_e type); +#endif diff --git a/tests/thread-util.c b/tests/thread-util.c deleted file mode 100644 index 65812cb..0000000 --- a/tests/thread-util.c +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (c) 2021 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 -#include -#include -#include -#include -#include - -#include -#include -#include "thread-log.h" -#include "thread-util.h" - -static thread_instance_h g_instance = NULL; - -bool _thread_check_instance() -{ - if (g_instance == NULL) { - msg("g_instance == NULL"); - return false; - } - return true; -} -thread_instance_h _thread_get_instance(void) -{ - return g_instance; -} - -const char* _thread_device_role_to_string(thread_device_role_e role) -{ - switch (role) { - CASE_TO_STR(THREAD_DEVICE_ROLE_DISABLED); - CASE_TO_STR(THREAD_DEVICE_ROLE_DETACHED); - CASE_TO_STR(THREAD_DEVICE_ROLE_CHILD); - CASE_TO_STR(THREAD_DEVICE_ROLE_ROUTER); - CASE_TO_STR(THREAD_DEVICE_ROLE_LEADER); - default: - return "NOT DEFINED"; - } -} - -const char* _thread_device_type_to_string(thread_device_type_e type) -{ - switch (type) { - CASE_TO_STR(THREAD_DEVICE_TYPE_NOT_SUPPORTED); - CASE_TO_STR(THREAD_DEVICE_TYPE_ROUTER); - CASE_TO_STR(THREAD_DEVICE_TYPE_FULL_END_DEVICE); - CASE_TO_STR(THREAD_DEVICE_TYPE_MINIMAL_END_DEVICE); - CASE_TO_STR(THREAD_DEVICE_TYPE_SLEEPY_END_DEVICE); - default: - return "NOT DEFINED"; - } -} -const char* _thread_ipaddr_type_to_string(thread_ipaddr_type_e type) -{ - switch (type) { - CASE_TO_STR(THREAD_IPADDR_TYPE_ALL); - CASE_TO_STR(THREAD_IPADDR_TYPE_LINK_LOCAL); - CASE_TO_STR(THREAD_IPADDR_TYPE_RLOC); - CASE_TO_STR(THREAD_IPADDR_TYPE_MLEID); - default: - return "NOT DEFINED"; - } -} - -const char* _thread_get_error_message(thread_error_e err) -{ - switch (err) { - CASE_TO_STR(THREAD_ERROR_NONE); - CASE_TO_STR(THREAD_ERROR_NOT_PERMITTED); - CASE_TO_STR(THREAD_ERROR_INVALID_PARAMETER); - CASE_TO_STR(THREAD_ERROR_OUT_OF_MEMORY); - CASE_TO_STR(THREAD_ERROR_RESOURCE_BUSY); - CASE_TO_STR(THREAD_ERROR_TIMED_OUT); - CASE_TO_STR(THREAD_ERROR_CANCELED); - CASE_TO_STR(THREAD_ERROR_NOW_IN_PROGRESS); - CASE_TO_STR(THREAD_ERROR_NOT_SUPPORTED); - CASE_TO_STR(THREAD_ERROR_NO_DATA); - CASE_TO_STR(THREAD_ERROR_NOT_INITIALIZED); - CASE_TO_STR(THREAD_ERROR_NOT_IN_PROGRESS); - CASE_TO_STR(THREAD_ERROR_ALREADY_DONE); - CASE_TO_STR(THREAD_ERROR_OPERATION_FAILED); - CASE_TO_STR(THREAD_ERROR_NOT_READY); - CASE_TO_STR(THREAD_ERROR_NOT_ENABLED); - CASE_TO_STR(THREAD_ERROR_NOT_FOUND); - CASE_TO_STR(THREAD_ERROR_ALREADY_REGISTERED); - CASE_TO_STR(THREAD_ERROR_DB_FAILED); - CASE_TO_STR(THREAD_ERROR_NOT_REGISTERED); - default: - return "NOT DEFINED"; - } -} - -int _preprocess_ipv6_prefix(char *prefix, uint8_t *ipv6_prefix) -{ - FUNC_ENTRY; - struct in6_addr result; - retv_if(inet_pton(AF_INET6, prefix, &result) == 0, - THREAD_ERROR_INVALID_PARAMETER); - - msg("ipv6 string parse successful"); - memcpy(ipv6_prefix, result.s6_addr, - THREAD_IPV6_PREFIX_SIZE*sizeof(uint8_t)); - - FUNC_EXIT; - return THREAD_ERROR_NONE; -} - diff --git a/tests/thread-util.h b/tests/thread-util.h deleted file mode 100644 index 1b79cc1..0000000 --- a/tests/thread-util.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2021 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 __UTIL_H__ -#define __UTIL_H__ - -__BEGIN_DECLS - -#include -#include -#include -#include -#include - -#include -#include - -#include "thread.h" -#include "thread-menu.h" -#include "thread-type.h" -#include "thread-log.h" - -#define CASE_TO_STR(x) case x: return #x; - -#define THREAD_DEFAULT_SCAN_TIME 80 -#define THREAD_IS_DEVICE_NEXT_HOP_DEFAULT TRUE -#define THREAD_IPV6_ADDRESS_SIZE 16 -#define THREAD_IPV6_PREFIX_SIZE 8 - -typedef struct { - thread_route_info_h handle; - uint8_t prefix_address[THREAD_IPV6_ADDRESS_SIZE]; - uint8_t prefix_length; -} thread_route_info_a; - -typedef struct { - thread_onmesh_prefix_info_h handle; - uint8_t prefix_address[THREAD_IPV6_ADDRESS_SIZE]; - uint8_t prefix_length; -} thread_onmesh_prefix_info_a; - -thread_instance_h _thread_get_instance(); -bool _thread_check_instance(); -const char* _thread_device_role_to_string(thread_device_role_e role); -const char* _thread_device_type_to_string(thread_device_type_e type); -const char* _thread_get_error_message(thread_error_e err); -int _preprocess_ipv6_prefix(char *prefix, uint8_t *ipv6_prefix); -const char* _thread_ipaddr_type_to_string(thread_ipaddr_type_e type); -#endif