Merge "Fix set/get configuration API" into tizen
[platform/core/api/uwb.git] / tests / capi-network-uwb-test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <time.h>
7 #include <assert.h>
8
9 #include <glib.h>
10 #include <glib-object.h>
11
12 #include <uwb.h>
13
14 #define RESET_COLOR "\e[m"
15 #define MAKE_RED "\e[31m"
16 #define MAKE_GREEN "\e[32m"
17
18 #define __FUNC_ENTER__ printf("%s() entering...\n", __func__)
19 #define __FUNC_EXIT__ printf("%s() leaving...\n", __func__)
20
21 #define RET_IF_LOOP_IS_NULL()\
22         do {\
23                 if (!g_main_loop_p) {\
24                         printf("Loop was not initialized\n");\
25                         return;\
26                 } \
27         } while (0)
28
29
30 enum {
31         CMD_QUIT,
32         CMD_FULL_MENU,
33         CMD_INITIALIZE,
34         CMD_DEINITIALIZE,
35         CMD_RESET,
36         CMD_FACTORY_RESET,
37         CMD_GET_OWN_NODE,
38         CMD_GET_NETWORK,
39         CMD_SET_POSITION,
40         CMD_SEND_MESSAGE,
41         CMD_SEND_MESSAGE_TO,
42         CMD_GET_CONFIGURATION,
43         CMD_SET_CONFIGURATION,
44
45         CMD_INVALID,
46 };
47
48 char *g_menu_str[] = {
49                 [CMD_QUIT]
50                  = "QUIT",
51                 [CMD_FULL_MENU]
52                  = "FULL_MENU",
53                 [CMD_INITIALIZE]
54                  = "INITIALIZE",
55                 [CMD_DEINITIALIZE]
56                  = "DEINITIALIZE",
57                 [CMD_RESET]
58                  = "RESET",
59                 [CMD_FACTORY_RESET]
60                  = "FACTORY_RESET",
61                 [CMD_GET_OWN_NODE]
62                  = "GET_OWN_NODE",
63                 [CMD_GET_NETWORK]
64                  = "GET_NETWORK",
65
66                 [CMD_SET_POSITION]
67                  = "SET_POSITION",
68                 [CMD_SEND_MESSAGE]
69                  = "SEND_MESSAGE",
70                 [CMD_SEND_MESSAGE_TO]
71                  = "SEND_MESSAGE_TO",
72                 [CMD_GET_CONFIGURATION]
73                  = "GET_CONFIGURATION",
74                 [CMD_SET_CONFIGURATION]
75                  = "SET_CONFIGURATION",
76
77                 [CMD_INVALID]
78                  = NULL, };
79
80 static GMainLoop* g_main_loop_p;
81
82 static const char *__print_error(uwb_error_e err_type)
83 {
84         switch (err_type) {
85         case UWB_ERROR_NONE:
86                 return "NONE";
87         case UWB_ERROR_IO_ERROR:
88                 return "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";
103         default:
104                 break;
105         }
106         return "UNKNOWN";
107 }
108
109 static inline void __print_result(int ret, gchar *function_name)
110 {
111         if (ret == UWB_ERROR_NONE) {
112                 printf(MAKE_GREEN"%s"RESET_COLOR"\n", function_name);
113         } else {
114                 printf(MAKE_RED"%s : %s ", function_name, __print_error(ret));
115                 printf(RESET_COLOR"\n");
116         }
117
118         printf("%s() result=[%d]\n", function_name, ret);
119 }
120
121 static char *__cmd_transform(char *str)
122 {
123         int i, j;
124         int len;
125         static char static_buffer[255];
126
127         if (str == NULL)
128                 return "";
129
130         len = strlen(str);
131         if (len == 0)
132                 return "";
133
134         /* lower char */
135         /* replance "_" to space */
136         for (i = 0, j = 0; i < len; i++, j++) {
137
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] = ' ';
142                 else
143                         static_buffer[i] = str[j];
144         }
145         static_buffer[j] = '\0';
146
147         return static_buffer;
148 }
149
150 static inline void __usage_full()
151 {
152         int i;
153         printf("Call Test Program\n");
154
155         for (i = CMD_QUIT; i < CMD_INVALID; i++) {
156                 if (i%3 == 0)
157                         printf("\n");
158                 printf(" %02d: %-32s ", i,
159                                 __cmd_transform(g_menu_str[i]));
160         }
161         printf("\n");
162 }
163
164 static int __is_digit(const char* str)
165 {
166         int len;
167         int i;
168
169         if (str == NULL)
170                 return -1;
171
172         if (strlen(str) == 0)
173                 return -1;
174
175         len = strlen(str);
176         for (i = 0; i < len; i++) {
177                 if (str[i] < '0' || str[i] > '9')
178                         return -2;
179         }
180
181         return 0;
182 }
183
184 void test_full_menu(void)
185 {
186         __usage_full();
187
188         return;
189 }
190
191 void test_quit(void)
192 {
193         RET_IF_LOOP_IS_NULL();
194
195         printf("Bye\n");
196         g_main_loop_quit(g_main_loop_p);
197
198         return;
199 }
200
201 void test_init(void)
202 {
203         int ret = 0;
204
205         RET_IF_LOOP_IS_NULL();
206
207         ret = uwb_initialize();
208         __print_result(ret, "uwb_initialize");
209
210         return;
211 }
212
213 void test_deinit(void)
214 {
215         int ret = 0;
216
217         RET_IF_LOOP_IS_NULL();
218
219         ret = uwb_deinitialize();
220         __print_result(ret, "uwb_deinitialize");
221
222         return;
223 }
224
225 void test_reset(void)
226 {
227         int ret = 0;
228
229         RET_IF_LOOP_IS_NULL();
230
231         ret = uwb_reset();
232         __print_result(ret, "uwb_reset");
233
234         return;
235 }
236
237 void test_factory_reset(void)
238 {
239         int ret = 0;
240
241         RET_IF_LOOP_IS_NULL();
242
243         ret = uwb_factory_reset();
244         __print_result(ret, "uwb_factory_reset");
245
246         return;
247 }
248
249 bool __print_node_info(uwb_node_h node, void *user_data)
250 {
251         uint64_t distance = 0;
252         uint64_t node_id = 0;
253         uint64_t pan_id = 0;
254         bool is_remote = 0;
255         int x = 0, y = 0, z = 0;
256         int ret = 0;
257
258         if (node == NULL)
259                 return true;
260
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");
271
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);
274         return true;
275 }
276
277 void test_get_own_node(void)
278 {
279         uwb_node_h own_node = NULL;
280         int ret = 0;
281
282         RET_IF_LOOP_IS_NULL();
283
284         ret = uwb_get_own_node(&own_node);
285         __print_result(ret, "uwb_get_own_node");
286
287         if (own_node == NULL)
288                 return;
289
290         uwb_node_h cloned_node = NULL;
291         ret = uwb_node_clone(own_node, &cloned_node);
292         __print_result(ret, "uwb_node_clone");
293
294         ret = uwb_node_destroy(own_node);
295         __print_result(ret, "uwb_node_destroy");
296
297         if (cloned_node == NULL)
298                 return;
299
300         __print_node_info(cloned_node, NULL);
301         ret = uwb_node_destroy(cloned_node);
302         __print_result(ret, "cloned_node");
303
304         return;
305 }
306
307 void __print_network_info(uwb_network_h uwb_network)
308 {
309         uint64_t pan_id = 0;
310         int remote_node_count = 0;
311         int ret = 0;
312
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");
317
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);
320 }
321
322 void __network_finished_cb(int result, uwb_network_h uwb_network, void *user_data)
323 {
324         int ret = 0;
325         if (result != UWB_ERROR_NONE)
326                 return;
327
328         if (uwb_network == NULL)
329                 return;
330
331         uwb_network_h cloned_network = NULL;
332         ret = uwb_network_clone(uwb_network, &cloned_network);
333         __print_result(ret, "uwb_network_clone");
334
335         ret = uwb_network_destroy(uwb_network);
336         __print_result(ret, "uwb_network_destroy");
337
338         if (cloned_network == NULL)
339                 return;
340
341         __print_network_info(cloned_network);
342         ret = uwb_network_destroy(cloned_network);
343         __print_result(ret, "uwb_network_destroy");
344
345 }
346
347 void test_get_network(void)
348 {
349         int ret = 0;
350
351         RET_IF_LOOP_IS_NULL();
352
353         ret = uwb_get_network(__network_finished_cb, NULL);
354         __print_result(ret, "uwb_get_network");
355
356         return;
357 }
358
359 void test_set_position(void)
360 {
361         uwb_node_h own_node = NULL;
362         int ret = 0;
363         int test_x = 10;
364         int test_y = 10;
365         int test_z = 10;
366
367         RET_IF_LOOP_IS_NULL();
368         ret = uwb_get_own_node(&own_node);
369         __print_result(ret, "uwb_get_own_node");
370
371
372         ret = uwb_node_set_position(own_node, test_x, test_y, test_z);
373         __print_result(ret, "uwb_node_set_position");
374 }
375
376 void test_send_message(void)
377 {
378         uwb_node_h own_node = NULL;
379         int ret = 0;
380         const char test_string[] = {"test string"};
381
382         ret = uwb_get_own_node(&own_node);
383         __print_result(ret, "uwb_get_own_node");
384
385         ret = uwb_node_send_message((const unsigned char *)test_string, strlen(test_string));
386         __print_result(ret, "uwb_node_send_message");
387 }
388
389 void test_send_message_to(void)
390 {
391         uwb_node_h own_node = NULL;
392         int ret = 0;
393         const char test_string[] = {"test string"};
394
395         ret = uwb_get_own_node(&own_node);
396         __print_result(ret, "uwb_get_own_node");
397
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");
400 }
401
402 void test_get_configuration(void)
403 {
404         uwb_node_h own_node = NULL;
405         int data_type = 0;
406         char *key;
407         int32_t int32_data;
408         int64_t int64_data;
409         char *string_data = NULL;
410         int ret = 0;
411
412         RET_IF_LOOP_IS_NULL();
413
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)
419                 return;
420
421         printf("Input key :\n");
422         if (scanf(" %255ms", &key) < 1)
423                 return;
424
425         ret = uwb_get_own_node(&own_node);
426         __print_result(ret, "uwb_get_own_node");
427         if (!own_node)
428                 goto out;
429
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");
441                 if (string_data)
442                         printf("Value [%s]\n", string_data);
443         }
444
445 out:
446         if (key)
447                 free(key);
448         uwb_node_destroy(own_node);
449         if (string_data)
450                 free(string_data);
451 }
452
453 void test_set_configuration(void)
454 {
455         uwb_node_h own_node = NULL;
456         int data_type = 0;
457         char *key;
458         int32_t int32_data = 2020;
459         int64_t int64_data = 2020;
460         char *string_data = NULL;
461         int ret = 0;
462
463         RET_IF_LOOP_IS_NULL();
464
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)
470                 return;
471
472         printf("Input key :\n");
473         if (scanf(" %255ms", &key) < 1)
474                 return;
475
476         ret = uwb_get_own_node(&own_node);
477         __print_result(ret, "uwb_get_own_node");
478         if (!own_node)
479                 goto out;
480
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)
490                         return;
491                 ret = uwb_node_set_configuration_string(own_node, key, string_data);
492                 __print_result(ret, "uwb_node_set_configuration_string");
493         }
494
495 out:
496         if (key)
497                 free(key);
498         uwb_node_destroy(own_node);
499         if (string_data)
500                 free(string_data);
501 }
502
503
504 typedef void (*test_func)(void);
505 test_func g_menu_func[] = {
506                 [CMD_QUIT]
507                  = test_quit,
508                 [CMD_FULL_MENU]
509                  = test_full_menu,
510
511                 [CMD_INITIALIZE]
512                  = test_init,
513                 [CMD_DEINITIALIZE]
514                  = test_deinit,
515
516                 [CMD_RESET]
517                  = test_reset,
518                 [CMD_FACTORY_RESET]
519                  = test_factory_reset,
520
521                 [CMD_GET_OWN_NODE]
522                  = test_get_own_node,
523                 [CMD_GET_NETWORK]
524                  = test_get_network,
525
526                 [CMD_SET_POSITION]
527                  = test_set_position,
528                 [CMD_SEND_MESSAGE]
529                  = test_send_message,
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,
536                 [CMD_INVALID]
537                  = NULL, };
538
539 static void __process_input(const char *input, gpointer user_data)
540 {
541         int cmd = strtol(input, NULL, 0);
542
543         if (__is_digit(input) < 0 || strlen(input) == 0 || errno == ERANGE || errno
544                         == EINVAL)
545                 cmd = CMD_INVALID;
546
547         printf("cmd=[%d]\n", cmd);
548         if (cmd >= CMD_INVALID || cmd < CMD_QUIT) {
549                 printf("Invalid CMD\n");
550                 return;
551         }
552         g_menu_func[cmd]();
553 }
554
555 static gboolean __test_terminal_read_std_input(GIOChannel * source,
556                 GIOCondition condition, gpointer user_data)
557 {
558         int fd = 0;
559
560         static char buf[1024];
561         int n;
562
563         errno = 0;
564         n = read(fd, buf, 1024);
565         if (n == 0) {
566                 printf("Error: read() from stdin returns 0.\n");
567         } else if (n < 0) {
568                 char error_buf[100] = {0, };
569                 strerror_r(errno, error_buf, sizeof(error_buf));
570                 printf("input: read, err=%s\n", error_buf);
571         } else {
572                 buf[n - 1] = '\0'; /* remove new line... */
573                 printf("\n\n");
574                 /* printf("Read [%d]bytes data: [%s]\n", n, buf); */
575                 /* printf("Processing it ---------------------\n", n, buf); */
576                 __process_input(buf, user_data);
577         }
578
579         return TRUE;
580 }
581
582 static void __glib_log(
583                 const gchar *log_domain,
584                 GLogLevelFlags log_level,
585                 const gchar *msg,
586                 gpointer user_data)
587 {
588         printf("[GLib Err] %s-0x%2.2X: %s\n",
589                         log_domain, log_level, msg);
590 }
591
592 int main(int argc, char **argv)
593 {
594
595 #if !GLIB_CHECK_VERSION(2, 36, 0)
596         g_type_init();
597 #endif
598
599         g_log_set_default_handler(__glib_log, NULL);
600         g_main_loop_p = g_main_loop_new(NULL, FALSE);
601
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);
606
607         __usage_full();
608
609         g_main_loop_run(g_main_loop_p);
610
611         return 0;
612 }