10 #include <glib-object.h>
14 #define RESET_COLOR "\e[m"
15 #define MAKE_RED "\e[31m"
16 #define MAKE_GREEN "\e[32m"
18 #define __FUNC_ENTER__ printf("%s() entering...\n", __func__)
19 #define __FUNC_EXIT__ printf("%s() leaving...\n", __func__)
21 #define RET_IF_LOOP_IS_NULL()\
23 if (!g_main_loop_p) {\
24 printf("Loop was not initialized\n");\
42 CMD_GET_CONFIGURATION,
43 CMD_SET_CONFIGURATION,
48 char *g_menu_str[] = {
72 [CMD_GET_CONFIGURATION]
73 = "GET_CONFIGURATION",
74 [CMD_SET_CONFIGURATION]
75 = "SET_CONFIGURATION",
80 static GMainLoop* g_main_loop_p;
82 static const char *__print_error(uwb_error_e err_type)
87 case UWB_ERROR_IO_ERROR:
89 case UWB_ERROR_OUT_OF_MEMORY:
90 return "OUT_OF_MEMORY";
91 case UWB_ERROR_INVALID_PARAMETER:
92 return "INVALID_PARAMETER";
93 case UWB_ERROR_PERMISSION_DENIED:
94 return "PERMISSION_DENIED";
95 case UWB_ERROR_NOT_SUPPORTED:
96 return "NOT_SUPPORTED";
97 case UWB_ERROR_OPERATION_FAILED:
98 return "OPERATION_FAILED";
99 case UWB_ERROR_ALREADY_INITIALIZED:
100 return "ALREADY_INITIALIZED";
101 case UWB_ERROR_NOT_INITIALIZED:
102 return "NOT_INITIALIZED";
109 static inline void __print_result(int ret, gchar *function_name)
111 if (ret == UWB_ERROR_NONE) {
112 printf(MAKE_GREEN"%s"RESET_COLOR"\n", function_name);
114 printf(MAKE_RED"%s : %s ", function_name, __print_error(ret));
115 printf(RESET_COLOR"\n");
118 printf("%s() result=[%d]\n", function_name, ret);
121 static char *__cmd_transform(char *str)
125 static char static_buffer[255];
135 /* replance "_" to space */
136 for (i = 0, j = 0; i < len; i++, j++) {
138 if (str[j] >= 'A' && str[j] <= 'Z')
139 static_buffer[i] = str[j] + 'a' - 'A';
140 else if (str[j] == '_')
141 static_buffer[i] = ' ';
143 static_buffer[i] = str[j];
145 static_buffer[j] = '\0';
147 return static_buffer;
150 static inline void __usage_full()
153 printf("Call Test Program\n");
155 for (i = CMD_QUIT; i < CMD_INVALID; i++) {
158 printf(" %02d: %-32s ", i,
159 __cmd_transform(g_menu_str[i]));
164 static int __is_digit(const char* str)
172 if (strlen(str) == 0)
176 for (i = 0; i < len; i++) {
177 if (str[i] < '0' || str[i] > '9')
184 void test_full_menu(void)
193 RET_IF_LOOP_IS_NULL();
196 g_main_loop_quit(g_main_loop_p);
205 RET_IF_LOOP_IS_NULL();
207 ret = uwb_initialize();
208 __print_result(ret, "uwb_initialize");
213 void test_deinit(void)
217 RET_IF_LOOP_IS_NULL();
219 ret = uwb_deinitialize();
220 __print_result(ret, "uwb_deinitialize");
225 void test_reset(void)
229 RET_IF_LOOP_IS_NULL();
232 __print_result(ret, "uwb_reset");
237 void test_factory_reset(void)
241 RET_IF_LOOP_IS_NULL();
243 ret = uwb_factory_reset();
244 __print_result(ret, "uwb_factory_reset");
249 bool __print_node_info(uwb_node_h node, void *user_data)
251 uint64_t distance = 0;
252 uint64_t node_id = 0;
255 int x = 0, y = 0, z = 0;
261 ret = uwb_node_get_distance(node, &distance);
262 __print_result(ret, "uwb_node_get_distance");
263 ret = uwb_node_get_node_id(node, &node_id);
264 __print_result(ret, "uwb_node_get_node_id");
265 ret = uwb_node_get_pan_id(node, &pan_id);
266 __print_result(ret, "uwb_node_get_pan_id");
267 ret = uwb_node_get_is_remote(node, &is_remote);
268 __print_result(ret, "uwb_node_get_is_remote");
269 ret = uwb_node_get_position(node, &x, &y, &z);
270 __print_result(ret, "uwb_node_get_position");
272 printf("Distance :%llu Node ID: %llu Pan ID: %llu\n", distance, node_id, pan_id);
273 printf("Position X: %d Y: %d Z: %d\n", x, y, z);
277 void test_get_own_node(void)
279 uwb_node_h own_node = NULL;
282 RET_IF_LOOP_IS_NULL();
284 ret = uwb_get_own_node(&own_node);
285 __print_result(ret, "uwb_get_own_node");
287 if (own_node == NULL)
290 uwb_node_h cloned_node = NULL;
291 ret = uwb_node_clone(own_node, &cloned_node);
292 __print_result(ret, "uwb_node_clone");
294 ret = uwb_node_destroy(own_node);
295 __print_result(ret, "uwb_node_destroy");
297 if (cloned_node == NULL)
300 __print_node_info(cloned_node, NULL);
301 ret = uwb_node_destroy(cloned_node);
302 __print_result(ret, "cloned_node");
307 void __print_network_info(uwb_network_h uwb_network)
310 int remote_node_count = 0;
313 ret = uwb_network_get_pan_id(uwb_network, &pan_id);
314 __print_result(ret, "uwb_network_get_pan_id");
315 ret = uwb_network_get_remote_node_count(uwb_network, &remote_node_count);
316 __print_result(ret, "uwb_network_get_remote_node_count");
318 printf("Pan ID: %llu remote node count: %d\n", pan_id, remote_node_count);
319 uwb_network_foreach_remote_node(uwb_network, __print_node_info, NULL);
322 void __network_finished_cb(int result, uwb_network_h uwb_network, void *user_data)
325 if (result != UWB_ERROR_NONE)
328 if (uwb_network == NULL)
331 uwb_network_h cloned_network = NULL;
332 ret = uwb_network_clone(uwb_network, &cloned_network);
333 __print_result(ret, "uwb_network_clone");
335 ret = uwb_network_destroy(uwb_network);
336 __print_result(ret, "uwb_network_destroy");
338 if (cloned_network == NULL)
341 __print_network_info(cloned_network);
342 ret = uwb_network_destroy(cloned_network);
343 __print_result(ret, "uwb_network_destroy");
347 void test_get_network(void)
351 RET_IF_LOOP_IS_NULL();
353 ret = uwb_get_network(__network_finished_cb, NULL);
354 __print_result(ret, "uwb_get_network");
359 void test_set_position(void)
361 uwb_node_h own_node = NULL;
367 RET_IF_LOOP_IS_NULL();
368 ret = uwb_get_own_node(&own_node);
369 __print_result(ret, "uwb_get_own_node");
372 ret = uwb_node_set_position(own_node, test_x, test_y, test_z);
373 __print_result(ret, "uwb_node_set_position");
376 void test_send_message(void)
378 uwb_node_h own_node = NULL;
380 const char test_string[] = {"test string"};
382 ret = uwb_get_own_node(&own_node);
383 __print_result(ret, "uwb_get_own_node");
385 ret = uwb_node_send_message((const unsigned char *)test_string, strlen(test_string));
386 __print_result(ret, "uwb_node_send_message");
389 void test_send_message_to(void)
391 uwb_node_h own_node = NULL;
393 const char test_string[] = {"test string"};
395 ret = uwb_get_own_node(&own_node);
396 __print_result(ret, "uwb_get_own_node");
398 ret = uwb_node_send_message_to(own_node, (const unsigned char *)test_string, strlen(test_string));
399 __print_result(ret, "uwb_node_send_message");
402 void test_get_configuration(void)
404 uwb_node_h own_node = NULL;
409 char *string_data = NULL;
412 RET_IF_LOOP_IS_NULL();
414 printf("Select Data type :\n");
415 printf("0. int32_t\n");
416 printf("1. int64_t\n");
417 printf("2. const char *\n");
418 if (scanf("%d", &data_type) < 1)
421 printf("Input key :\n");
422 if (scanf(" %255ms", &key) < 1)
425 ret = uwb_get_own_node(&own_node);
426 __print_result(ret, "uwb_get_own_node");
430 if (data_type == 0) {
431 ret = uwb_node_get_configuration_int32(own_node, key, &int32_data);
432 __print_result(ret, "uwb_node_get_configuration_int32");
433 printf("Value [%d]\n", int32_data);
434 } else if (data_type == 1) {
435 ret = uwb_node_get_configuration_int64(own_node, key, &int64_data);
436 __print_result(ret, "uwb_node_get_configuration_int64");
437 printf("Value [%lld]\n", int64_data);
438 } else if (data_type == 2) {
439 ret = uwb_node_get_configuration_string(own_node, key, &string_data);
440 __print_result(ret, "uwb_node_get_configuration_string");
442 printf("Value [%s]\n", string_data);
448 uwb_node_destroy(own_node);
453 void test_set_configuration(void)
455 uwb_node_h own_node = NULL;
458 int32_t int32_data = 2020;
459 int64_t int64_data = 2020;
460 char *string_data = NULL;
463 RET_IF_LOOP_IS_NULL();
465 printf("Select Data type :\n");
466 printf("0. int32_t\n");
467 printf("1. int64_t\n");
468 printf("2. const char *\n");
469 if (scanf("%d", &data_type) < 1)
472 printf("Input key :\n");
473 if (scanf(" %255ms", &key) < 1)
476 ret = uwb_get_own_node(&own_node);
477 __print_result(ret, "uwb_get_own_node");
481 if (data_type == 0) {
482 ret = uwb_node_set_configuration_int32(own_node, key, int32_data);
483 __print_result(ret, "uwb_node_set_configuration_int32");
484 } else if (data_type == 1) {
485 ret = uwb_node_set_configuration_int64(own_node, key, int64_data);
486 __print_result(ret, "uwb_node_set_configuration_int64");
487 } else if (data_type == 2) {
488 printf("Input string :\n");
489 if (scanf(" %255ms", &string_data) < 1)
491 ret = uwb_node_set_configuration_string(own_node, key, string_data);
492 __print_result(ret, "uwb_node_set_configuration_string");
498 uwb_node_destroy(own_node);
504 typedef void (*test_func)(void);
505 test_func g_menu_func[] = {
519 = test_factory_reset,
530 [CMD_SEND_MESSAGE_TO]
531 = test_send_message_to,
532 [CMD_GET_CONFIGURATION]
533 = test_get_configuration,
534 [CMD_SET_CONFIGURATION]
535 = test_set_configuration,
539 static void __process_input(const char *input, gpointer user_data)
541 int cmd = strtol(input, NULL, 0);
543 if (__is_digit(input) < 0 || strlen(input) == 0 || errno == ERANGE || errno
547 printf("cmd=[%d]\n", cmd);
548 if (cmd >= CMD_INVALID || cmd < CMD_QUIT) {
549 printf("Invalid CMD\n");
555 static gboolean __test_terminal_read_std_input(GIOChannel * source,
556 GIOCondition condition, gpointer user_data)
560 static char buf[1024];
564 n = read(fd, buf, 1024);
566 printf("Error: read() from stdin returns 0.\n");
568 char error_buf[100] = {0, };
569 strerror_r(errno, error_buf, sizeof(error_buf));
570 printf("input: read, err=%s\n", error_buf);
572 buf[n - 1] = '\0'; /* remove new line... */
574 /* printf("Read [%d]bytes data: [%s]\n", n, buf); */
575 /* printf("Processing it ---------------------\n", n, buf); */
576 __process_input(buf, user_data);
582 static void __glib_log(
583 const gchar *log_domain,
584 GLogLevelFlags log_level,
588 printf("[GLib Err] %s-0x%2.2X: %s\n",
589 log_domain, log_level, msg);
592 int main(int argc, char **argv)
595 #if !GLIB_CHECK_VERSION(2, 36, 0)
599 g_log_set_default_handler(__glib_log, NULL);
600 g_main_loop_p = g_main_loop_new(NULL, FALSE);
602 int std_input_fd = 0;
603 GIOChannel *gio2 = g_io_channel_unix_new(std_input_fd);
604 g_io_add_watch(gio2, G_IO_IN, (GIOFunc) __test_terminal_read_std_input, NULL);
605 g_io_channel_unref(gio2);
609 g_main_loop_run(g_main_loop_p);