From a257bc182a20446e8235d80d31b2dda7d339d602 Mon Sep 17 00:00:00 2001 From: Yu Date: Thu, 27 Aug 2020 16:35:16 +0900 Subject: [PATCH] Add test program Change-Id: Id1dfafbdb5fa9bbb406f67463bf26060e39c3a8b Signed-off-by: Yu jiung --- include/uwb.h | 16 +- src/uwb-util.c | 4 +- src/uwb-util.h | 6 +- src/uwb.c | 77 ++++-- tests/CMakeLists.txt | 27 ++ tests/capi-network-uwb-test.c | 597 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 697 insertions(+), 30 deletions(-) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/capi-network-uwb-test.c diff --git a/include/uwb.h b/include/uwb.h index c3724e7..e0edd16 100755 --- a/include/uwb.h +++ b/include/uwb.h @@ -48,19 +48,19 @@ typedef void (*uwb_message_received_cb)(uint64_t node_id, const unsigned char *m int message_length, void *user_data); typedef void (*uwb_position_changed_cb)(uint64_t node_id, int x, int y, int z, void *user_data); -typedef void (*uwb_network_get_finished_cb)(int result, uwb_network_h *network, void *user_data); +typedef void (*uwb_get_network_finished_cb)(int result, uwb_network_h network, void *user_data); typedef bool (*uwb_network_foreach_remote_node_cb)(uwb_node_h remote_node, void *user_data); -int uwb_initialize(); -int uwb_deinitialize(); -int uwb_reset(); -int uwb_factory_reset(); +int uwb_initialize(void); +int uwb_deinitialize(void); +int uwb_reset(void); +int uwb_factory_reset(void); int uwb_set_message_received_cb(uwb_message_received_cb message_received_cb, void *user_data); int uwb_set_position_changed_cb(uwb_position_changed_cb position_changed_cb, void *user_data); -int uwb_unset_message_received_cb(); -int uwb_unset_position_changed_cb(); +int uwb_unset_message_received_cb(void); +int uwb_unset_position_changed_cb(void); int uwb_get_own_node(uwb_node_h *own_node); -int uwb_network_get(uwb_network_get_finished_cb finished_cb, void *user_data); +int uwb_get_network(uwb_get_network_finished_cb finished_cb, void *user_data); int uwb_network_clone(uwb_network_h source, uwb_network_h *target); int uwb_network_destroy(uwb_network_h network); int uwb_network_get_pan_id(uwb_network_h network, uint64_t *pan_id); diff --git a/src/uwb-util.c b/src/uwb-util.c index 5a4a563..96f52d1 100755 --- a/src/uwb-util.c +++ b/src/uwb-util.c @@ -28,7 +28,7 @@ void _node_free_func(gpointer data) node = NULL; } -void uwb_network_clean(uwb_network_s *network) +void uwb_util_destroy_network(uwb_network_s *network) { if (network == NULL) return; @@ -39,7 +39,7 @@ void uwb_network_clean(uwb_network_s *network) network = NULL; } -uwb_node_s *uwb_get_node_from_variant(GVariant *va) +uwb_node_s *uwb_util_get_node_from_variant(GVariant *va) { GVariantIter *iter = NULL; const gchar *key; diff --git a/src/uwb-util.h b/src/uwb-util.h index c30859c..cb07e55 100755 --- a/src/uwb-util.h +++ b/src/uwb-util.h @@ -18,12 +18,14 @@ #ifndef __UWB_UTIL_H__ #define __UWB_UTIL_H__ +#include + #ifdef __cplusplus extern "C" { #endif -uwb_node_s *uwb_get_node_from_variant(GVariant *va); -void uwb_network_clean(uwb_network_s *network); +uwb_node_s *uwb_util_get_node_from_variant(GVariant *va); +void uwb_util_destroy_network(uwb_network_s *network); #ifdef __cplusplus } diff --git a/src/uwb.c b/src/uwb.c index adf2530..1be4838 100755 --- a/src/uwb.c +++ b/src/uwb.c @@ -37,7 +37,7 @@ static struct _uwb_ctx { void *message_received_user_data; uwb_position_changed_cb position_changed_cb; void *position_changed_user_data; - uwb_network_get_finished_cb network_get_finished_cb; + uwb_get_network_finished_cb get_network_finished_cb; uwb_network_foreach_remote_node_cb foreach_remote_node_cb; } uwb_ctx = {NULL,}; @@ -107,7 +107,7 @@ static void __position_changed(GObject *source_object, uwb_ctx.position_changed_cb(node_id, x, y, z, uwb_ctx.position_changed_user_data); } -static int manager_proxy_init() +static int manager_proxy_init(void) { GError *error = NULL; @@ -137,13 +137,13 @@ static int manager_proxy_init() return UWB_ERROR_NONE; } -static void manager_proxy_deinit() +static void manager_proxy_deinit(void) { g_object_unref(uwb_ctx.manager_proxy); uwb_ctx.manager_proxy = NULL; } -EXPORT_API int uwb_initialize() +EXPORT_API int uwb_initialize(void) { int ret = UWB_ERROR_NONE; @@ -159,7 +159,7 @@ EXPORT_API int uwb_initialize() return ret; } -EXPORT_API int uwb_deinitialize() +EXPORT_API int uwb_deinitialize(void) { int ret = UWB_ERROR_NONE; @@ -175,7 +175,7 @@ EXPORT_API int uwb_deinitialize() return ret; } -EXPORT_API int uwb_reset() +EXPORT_API int uwb_reset(void) { int ret = UWB_ERROR_NONE; GError *error = NULL; @@ -195,7 +195,7 @@ EXPORT_API int uwb_reset() return ret; } -EXPORT_API int uwb_factory_reset() +EXPORT_API int uwb_factory_reset(void) { int ret = UWB_ERROR_NONE; GError *error = NULL; @@ -247,7 +247,7 @@ EXPORT_API int uwb_set_position_changed_cb(uwb_position_changed_cb position_chan return ret; } -EXPORT_API int uwb_unset_message_received_cb() +EXPORT_API int uwb_unset_message_received_cb(void) { int ret = UWB_ERROR_NONE; @@ -263,7 +263,7 @@ EXPORT_API int uwb_unset_message_received_cb() return ret; } -EXPORT_API int uwb_unset_position_changed_cb() +EXPORT_API int uwb_unset_position_changed_cb(void) { int ret = UWB_ERROR_NONE; @@ -299,7 +299,7 @@ EXPORT_API int uwb_get_own_node(uwb_node_h *own_node) } if (ret == UWB_ERROR_NONE && own_node_va != NULL) - *_own_node = uwb_get_node_from_variant(own_node_va); + *_own_node = uwb_util_get_node_from_variant(own_node_va); else *_own_node = NULL; @@ -325,8 +325,8 @@ static void __network_get_cb(GObject *source_object, GAsyncResult *res, gpointer _ERR("manager_call_get_network_info_finish failed : %s", error->message); __handle_error(error, &ret); - if (uwb_ctx.network_get_finished_cb != NULL) { - uwb_ctx.network_get_finished_cb(ret, NULL, user_data); + if (uwb_ctx.get_network_finished_cb != NULL) { + uwb_ctx.get_network_finished_cb(ret, NULL, user_data); } return; @@ -348,7 +348,7 @@ static void __network_get_cb(GObject *source_object, GAsyncResult *res, gpointer while ((nodes_va = g_variant_iter_next_value(iter)) != NULL) { uwb_node_s *node; - node = uwb_get_node_from_variant(nodes_va); + node = uwb_util_get_node_from_variant(nodes_va); node->is_remote = true; _DBG("(%d, %d, %d)", node->x, node->y, node->z); @@ -361,12 +361,12 @@ static void __network_get_cb(GObject *source_object, GAsyncResult *res, gpointer g_variant_unref(remote_node_list_va); } - if (uwb_ctx.network_get_finished_cb != NULL) { - uwb_ctx.network_get_finished_cb(ret, (uwb_network_h)network_s, user_data); + if (uwb_ctx.get_network_finished_cb != NULL) { + uwb_ctx.get_network_finished_cb(ret, (uwb_network_h)network_s, user_data); } } -EXPORT_API int uwb_network_get(uwb_network_get_finished_cb finished_cb, void *user_data) +EXPORT_API int uwb_network_get(uwb_get_network_finished_cb finished_cb, void *user_data) { int ret = UWB_ERROR_NONE; @@ -377,7 +377,7 @@ EXPORT_API int uwb_network_get(uwb_network_get_finished_cb finished_cb, void *us uwb_check_null_ret_error("finished_cb", finished_cb, UWB_ERROR_INVALID_PARAMETER); - uwb_ctx.network_get_finished_cb = finished_cb; + uwb_ctx.get_network_finished_cb = finished_cb; manager_call_get_network_info(uwb_ctx.manager_proxy, NULL, __network_get_cb, user_data); @@ -386,16 +386,57 @@ EXPORT_API int uwb_network_get(uwb_network_get_finished_cb finished_cb, void *us return ret; } + +static gpointer __copy_node(gconstpointer src, gpointer data) +{ + if (!src) + return NULL; + + uwb_node_s *src_ptr = (uwb_node_s *)src; + uwb_node_s *dst_ptr = (uwb_node_s *)malloc(sizeof(uwb_node_s)); + if (!dst_ptr) + return NULL; + + dst_ptr->node_id = src_ptr->node_id; + dst_ptr->pan_id = src_ptr->pan_id; + dst_ptr->is_remote = src_ptr->is_remote; + dst_ptr->distance = src_ptr->distance; + dst_ptr->x = src_ptr->x; + dst_ptr->y = src_ptr->y; + dst_ptr->z = src_ptr->z; + + return (gpointer)dst_ptr; +} + EXPORT_API int uwb_network_clone(uwb_network_h source, uwb_network_h *target) { + uwb_network_s *result_network = NULL; + uwb_network_s *src_network = (uwb_network_s *)source; + GSList *remote_node_list = NULL; int ret = UWB_ERROR_NONE; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); _BEGIN(); + uwb_check_null_ret_error("source", source, UWB_ERROR_INVALID_PARAMETER); + + result_network = (uwb_network_s *)malloc(sizeof(uwb_network_s)); + if (result_network == NULL) { + _ERR("malloc failed"); + return UWB_ERROR_OPERATION_FAILED; + } + + result_network->pan_id = src_network->pan_id; + result_network->remote_node_count = src_network->remote_node_count; + remote_node_list = src_network->remote_node_list; + result_network->remote_node_list = g_slist_copy_deep(remote_node_list, + __copy_node, + NULL); + _END(); + *target = result_network; return ret; } @@ -409,7 +450,7 @@ EXPORT_API int uwb_network_destroy(uwb_network_h network) uwb_check_null_ret_error("network", network, UWB_ERROR_INVALID_PARAMETER); - uwb_network_clean(network); + uwb_util_destroy_network(network); _END(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..da32239 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,27 @@ +########################## search for packages ################################ + +PKG_CHECK_MODULES(TARGET_UWB_TEST_REQ_PKGS REQUIRED glib-2.0) + +############################# compiler flags ################################## + +SET(EXTRA_FLAGS "-Wall -Werror") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} ${CFLAGS} -fPIE") +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") + +######################## directory configuration ############################ + +INCLUDE_DIRECTORIES(${TARGET_UWB_TEST_REQ_PKGS_INCLUDE_DIRS}) +LINK_DIRECTORIES(${TARGET_UWB_TEST_REQ_PKGS_LIBRARY_DIRS}) + +INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include) + +# Build +ADD_EXECUTABLE(${TARGET_UWB_TEST} + ${CMAKE_CURRENT_SOURCE_DIR}/capi-network-uwb-test.c +) + +TARGET_LINK_LIBRARIES(${TARGET_UWB_TEST} + ${TARGET_UWB_TEST_REQ_PKGS_LIBRARIES} ${PROJECT_NAME}) + +#Install +INSTALL(TARGETS ${TARGET_UWB_TEST} DESTINATION ${BIN_DIR}) \ No newline at end of file diff --git a/tests/capi-network-uwb-test.c b/tests/capi-network-uwb-test.c new file mode 100644 index 0000000..fad84f8 --- /dev/null +++ b/tests/capi-network-uwb-test.c @@ -0,0 +1,597 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#define RESET_COLOR "\e[m" +#define MAKE_RED "\e[31m" +#define MAKE_GREEN "\e[32m" + +#define __FUNC_ENTER__ printf("%s() entering...\n", __func__) +#define __FUNC_EXIT__ printf("%s() leaving...\n", __func__) + +#define RET_IF_LOOP_IS_NULL()\ + do {\ + if (!g_main_loop_p) {\ + printf("Loop was not initialized\n");\ + return;\ + } \ + } while (0) + + +enum { + CMD_QUIT, + CMD_FULL_MENU, + CMD_INITIALIZE, + CMD_DEINITIALIZE, + CMD_RESET, + CMD_FACTORY_RESET, + CMD_GET_OWN_NODE, + CMD_GET_NETWORK, + CMD_SET_POSITION, + CMD_SEND_MESSAGE, + CMD_SEND_MESSAGE_TO, + CMD_GET_CONFIGURATION, + CMD_SET_CONFIGURATION, + + CMD_INVALID, +}; + +char *g_menu_str[] = { + [CMD_QUIT] + = "QUIT", + [CMD_FULL_MENU] + = "FULL_MENU", + [CMD_INITIALIZE] + = "INITIALIZE", + [CMD_DEINITIALIZE] + = "DEINITIALIZE", + [CMD_RESET] + = "RESET", + [CMD_FACTORY_RESET] + = "FACTORY_RESET", + [CMD_GET_OWN_NODE] + = "GET_OWN_NODE", + [CMD_GET_NETWORK] + = "GET_NETWORK", + + [CMD_SET_POSITION] + = "SET_POSITION", + [CMD_SEND_MESSAGE] + = "SEND_MESSAGE", + [CMD_SEND_MESSAGE_TO] + = "SEND_MESSAGE_TO", + [CMD_GET_CONFIGURATION] + = "GET_CONFIGURATION", + [CMD_SET_CONFIGURATION] + = "SET_CONFIGURATION", + + [CMD_INVALID] + = NULL, }; + +static GMainLoop* g_main_loop_p; + +static const char *__print_error(uwb_error_e err_type) +{ + switch (err_type) { + case UWB_ERROR_NONE: + return "NONE"; + case UWB_ERROR_IO_ERROR: + return "IO_ERROR"; + case UWB_ERROR_OUT_OF_MEMORY: + return "OUT_OF_MEMORY"; + case UWB_ERROR_INVALID_PARAMETER: + return "INVALID_PARAMETER"; + case UWB_ERROR_PERMISSION_DENIED: + return "PERMISSION_DENIED"; + case UWB_ERROR_NOT_SUPPORTED: + return "NOT_SUPPORTED"; + case UWB_ERROR_OPERATION_FAILED: + return "OPERATION_FAILED"; + case UWB_ERROR_ALREADY_INITIALIZED: + return "ALREADY_INITIALIZED"; + case UWB_ERROR_NOT_INITIALIZED: + return "NOT_INITIALIZED"; + default: + break; + } + return "UNKNOWN"; +} + +static inline void __print_result(int ret, gchar *function_name) +{ + if (ret == UWB_ERROR_NONE) { + printf(MAKE_GREEN"%s"RESET_COLOR"\n", function_name); + } else { + printf(MAKE_RED"%s : %s ", function_name, __print_error(ret)); + printf(RESET_COLOR"\n"); + } + + printf("%s() result=[%d]\n", function_name, ret); +} + +static char *__cmd_transform(char *str) +{ + int i, j; + int len; + static char static_buffer[255]; + + if (str == NULL) + return ""; + + len = strlen(str); + if (len == 0) + return ""; + + /* lower char */ + /* replance "_" to space */ + for (i = 0, j = 0; i < len; i++, j++) { + + if (str[j] >= 'A' && str[j] <= 'Z') + static_buffer[i] = str[j] + 'a' - 'A'; + else if (str[j] == '_') + static_buffer[i] = ' '; + else + static_buffer[i] = str[j]; + } + static_buffer[j] = '\0'; + + return static_buffer; +} + +static inline void __usage_full() +{ + int i; + printf("Call Test Program\n"); + + for (i = CMD_QUIT; i < CMD_INVALID; i++) { + if (i%3 == 0) + printf("\n"); + printf(" %02d: %-32s ", i, + __cmd_transform(g_menu_str[i])); + } + printf("\n"); +} + +static int __is_digit(const char* str) +{ + int len; + int i; + + if (str == NULL) + return -1; + + if (strlen(str) == 0) + return -1; + + len = strlen(str); + for (i = 0; i < len; i++) { + if (str[i] < '0' || str[i] > '9') + return -2; + } + + return 0; +} + +void test_full_menu(void) +{ + __usage_full(); + + return; +} + +void test_quit(void) +{ + RET_IF_LOOP_IS_NULL(); + + printf("Bye\n"); + g_main_loop_quit(g_main_loop_p); + + return; +} + +void test_init(void) +{ + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + ret = uwb_initialize(); + __print_result(ret, "uwb_initialize"); + + return; +} + +void test_deinit(void) +{ + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + ret = uwb_deinitialize(); + __print_result(ret, "uwb_deinitialize"); + + return; +} + +void test_reset(void) +{ + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + ret = uwb_reset(); + __print_result(ret, "uwb_reset"); + + return; +} + +void test_factory_reset(void) +{ + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + ret = uwb_factory_reset(); + __print_result(ret, "uwb_factory_reset"); + + return; +} + +bool __print_node_info(uwb_node_h node, void *user_data) +{ + uint64_t distance = 0; + uint64_t node_id = 0; + uint64_t pan_id = 0; + bool is_remote = 0; + int x = 0, y = 0, z = 0; + int ret = 0; + + if (node == NULL) + return true; + + ret = uwb_node_get_distance(node, &distance); + __print_result(ret, "uwb_node_get_distance"); + ret = uwb_node_get_node_id(node, &node_id); + __print_result(ret, "uwb_node_get_node_id"); + ret = uwb_node_get_pan_id(node, &pan_id); + __print_result(ret, "uwb_node_get_pan_id"); + ret = uwb_node_get_is_remote(node, &is_remote); + __print_result(ret, "uwb_node_get_is_remote"); + ret = uwb_node_get_position(node, &x, &y, &z); + __print_result(ret, "uwb_node_get_position"); + + printf("Distance :%llu Node ID: %llu Pan ID: %llu\n", distance, node_id, pan_id); + printf("Position X: %d Y: %d Z: %d\n", x, y, z); + return true; +} + +void test_get_own_node(void) +{ + uwb_node_h own_node = NULL; + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + ret = uwb_get_own_node(&own_node); + __print_result(ret, "uwb_get_own_node"); + + if (own_node == NULL) + return; + + uwb_node_h cloned_node = NULL; + ret = uwb_node_clone(own_node, &cloned_node); + __print_result(ret, "uwb_node_clone"); + + ret = uwb_node_destroy(own_node); + __print_result(ret, "uwb_node_destroy"); + + if (cloned_node == NULL) + return; + + __print_node_info(cloned_node, NULL); + ret = uwb_node_destroy(cloned_node); + __print_result(ret, "cloned_node"); + + return; +} + +void __print_network_info(uwb_network_h uwb_network) +{ + uint64_t pan_id = 0; + int remote_node_count = 0; + int ret = 0; + + ret = uwb_network_get_pan_id(uwb_network, &pan_id); + __print_result(ret, "uwb_network_get_pan_id"); + ret = uwb_network_get_remote_node_count(uwb_network, &remote_node_count); + __print_result(ret, "uwb_network_get_remote_node_count"); + + printf("Pan ID: %llu remote node count: %d\n", pan_id, remote_node_count); + uwb_network_foreach_remote_node(uwb_network, __print_node_info, NULL); +} + +void __network_finished_cb(int result, uwb_network_h uwb_network, void *user_data) +{ + int ret = 0; + if (result != UWB_ERROR_NONE) + return; + + if (uwb_network == NULL) + return; + + uwb_network_h cloned_network = NULL; + ret = uwb_network_clone(uwb_network, &cloned_network); + __print_result(ret, "uwb_network_clone"); + + ret = uwb_network_destroy(uwb_network); + __print_result(ret, "uwb_network_destroy"); + + if (cloned_network == NULL) + return; + + __print_network_info(cloned_network); + ret = uwb_network_destroy(cloned_network); + __print_result(ret, "uwb_network_destroy"); + +} + +void test_get_network(void) +{ + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + ret = uwb_get_network(__network_finished_cb, NULL); + __print_result(ret, "uwb_get_network"); + + return; +} + +void test_set_position(void) +{ + uwb_node_h own_node = NULL; + int ret = 0; + int test_x = 10; + int test_y = 10; + int test_z = 10; + + RET_IF_LOOP_IS_NULL(); + ret = uwb_get_own_node(&own_node); + __print_result(ret, "uwb_get_own_node"); + + + ret = uwb_node_set_position(own_node, test_x, test_y, test_z); + __print_result(ret, "uwb_node_set_position"); +} + +void test_send_message(void) +{ + uwb_node_h own_node = NULL; + int ret = 0; + const char test_string[] = {"test string"}; + + ret = uwb_get_own_node(&own_node); + __print_result(ret, "uwb_get_own_node"); + + ret = uwb_node_send_message((const unsigned char *)test_string, strlen(test_string)); + __print_result(ret, "uwb_node_send_message"); +} + +void test_send_message_to(void) +{ + uwb_node_h own_node = NULL; + int ret = 0; + const char test_string[] = {"test string"}; + + ret = uwb_get_own_node(&own_node); + __print_result(ret, "uwb_get_own_node"); + + ret = uwb_node_send_message_to(own_node, (const unsigned char *)test_string, strlen(test_string)); + __print_result(ret, "uwb_node_send_message"); +} + +void test_get_configuration(void) +{ + uwb_node_h own_node = NULL; + int data_type = 0; + char *key; + int32_t int32_data; + int64_t int64_data; + char *string_data = NULL; + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + printf("Select Data type :\n"); + printf("0. int32_t\n"); + printf("1. int64_t\n"); + printf("2. const char *\n"); + if (scanf("%d", &data_type) < 1) + return; + + printf("Input key :\n"); + if (scanf(" %255ms", &key) < 1) + return; + + ret = uwb_get_own_node(&own_node); + __print_result(ret, "uwb_get_own_node"); + if (!own_node) + goto out; + + if (data_type == 0) { + ret = uwb_node_get_configuration_int32(own_node, key, &int32_data); + __print_result(ret, "uwb_node_get_configuration_int32"); + } else if (data_type == 1) { + ret = uwb_node_get_configuration_int64(own_node, key, &int64_data); + __print_result(ret, "uwb_node_get_configuration_int64"); + } else if (data_type == 2) { + ret = uwb_node_get_configuration_string(own_node, key, &string_data); + __print_result(ret, "uwb_node_get_configuration_string"); + } + +out: + if (key) + free(key); + uwb_node_destroy(own_node); + if (string_data) + free(string_data); +} + +void test_set_configuration(void) +{ + uwb_node_h own_node = NULL; + int data_type = 0; + char *key; + int32_t int32_data; + int64_t int64_data; + char *string_data = NULL; + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + printf("Select Data type :\n"); + printf("0. int32_t\n"); + printf("1. int64_t\n"); + printf("2. const char *\n"); + if (scanf("%d", &data_type) < 1) + return; + + printf("Input key :\n"); + if (scanf(" %255ms", &key) < 1) + return; + + ret = uwb_get_own_node(&own_node); + __print_result(ret, "uwb_get_own_node"); + if (!own_node) + goto out; + + if (data_type == 0) { + ret = uwb_node_set_configuration_int32(own_node, key, int32_data); + __print_result(ret, "uwb_node_set_configuration_int32"); + } else if (data_type == 1) { + ret = uwb_node_set_configuration_int64(own_node, key, int64_data); + __print_result(ret, "uwb_node_set_configuration_int64"); + } else if (data_type == 2) { + printf("Input string :\n"); + if (scanf(" %255ms", &string_data) < 1) + return; + ret = uwb_node_set_configuration_string(own_node, key, string_data); + __print_result(ret, "uwb_node_set_configuration_string"); + } + +out: + if (key) + free(key); + uwb_node_destroy(own_node); + if (string_data) + free(string_data); +} + + +typedef void (*test_func)(void); +test_func g_menu_func[] = { + [CMD_QUIT] + = test_quit, + [CMD_FULL_MENU] + = test_full_menu, + + [CMD_INITIALIZE] + = test_init, + [CMD_DEINITIALIZE] + = test_deinit, + + [CMD_RESET] + = test_reset, + [CMD_FACTORY_RESET] + = test_factory_reset, + + [CMD_GET_OWN_NODE] + = test_get_own_node, + [CMD_GET_NETWORK] + = test_get_network, + + [CMD_SET_POSITION] + = test_set_position, + [CMD_SEND_MESSAGE] + = test_send_message, + [CMD_SEND_MESSAGE_TO] + = test_send_message_to, + [CMD_GET_CONFIGURATION] + = test_get_configuration, + [CMD_SET_CONFIGURATION] + = test_set_configuration, + [CMD_INVALID] + = NULL, }; + +static void __process_input(const char *input, gpointer user_data) +{ + int cmd = strtol(input, NULL, 0); + + if (__is_digit(input) < 0 || strlen(input) == 0 || errno == ERANGE || errno + == EINVAL) + cmd = CMD_INVALID; + + printf("cmd=[%d]\n", cmd); + if (cmd >= CMD_INVALID || cmd < CMD_QUIT) { + printf("Invalid CMD\n"); + return; + } + g_menu_func[cmd](); +} + +static gboolean __test_terminal_read_std_input(GIOChannel * source, + GIOCondition condition, gpointer user_data) +{ + int fd = 0; + + static char buf[1024]; + int n; + + errno = 0; + n = read(fd, buf, 1024); + if (n == 0) { + printf("Error: read() from stdin returns 0.\n"); + } else if (n < 0) { + char error_buf[100] = {0, }; + strerror_r(errno, error_buf, sizeof(error_buf)); + printf("input: read, err=%s\n", error_buf); + } else { + buf[n - 1] = '\0'; /* remove new line... */ + printf("\n\n"); + /* printf("Read [%d]bytes data: [%s]\n", n, buf); */ + /* printf("Processing it ---------------------\n", n, buf); */ + __process_input(buf, user_data); + } + + return TRUE; +} + +int main(int argc, char **argv) +{ + +#if !GLIB_CHECK_VERSION(2, 36, 0) + g_type_init(); +#endif + + g_main_loop_p = g_main_loop_new(NULL, FALSE); + + int std_input_fd = 0; + GIOChannel *gio2 = g_io_channel_unix_new(std_input_fd); + g_io_add_watch(gio2, G_IO_IN, (GIOFunc) __test_terminal_read_std_input, NULL); + g_io_channel_unref(gio2); + + __usage_full(); + + g_main_loop_run(g_main_loop_p); + + return 0; +} -- 2.7.4