Remove address information
[platform/core/api/multi-device-group.git] / test / mdg-manager.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/time.h>
22 #include <unistd.h>
23 #include <glib.h>
24 #include <gio/gio.h>
25
26 #include "menu.h"
27 #include "common.h"
28
29 mdg_h handle = NULL;
30
31 GList *found_group_list;
32 GList *found_device_list;
33 GList *found_invited_device_list;
34 GList *found_channel_list;
35
36 static char groupid[MENU_DATA_SIZE + 1] = "mygroup";
37 static char request_groupid[MENU_DATA_SIZE + 1] = "subgroup";
38 static char timeout[MENU_DATA_SIZE + 1] = "2";
39 static char group_idx[MENU_DATA_SIZE + 1] = "1";
40 static char device_idx[MENU_DATA_SIZE + 1] = "1";
41 static char channel_idx[MENU_DATA_SIZE + 1] = "1";
42 static char pin[MENU_DATA_SIZE + 1] = "12341234";
43 static char message[MENU_DATA_SIZE + 1] = "Hello World!!";
44 static char channel_id[MENU_DATA_SIZE + 1] = "Channel1";
45 static char file_path[MENU_DATA_SIZE + 1] = "/path/to/file";
46
47 static int run_group_find(MManager *mm, struct menu_data *menu);
48 static int run_devices_find(MManager *mm, struct menu_data *menu);
49
50 static const char* __device_type_to_string(mdg_device_type_e type)
51 {
52         switch (type) {
53         /* CHECK: List all enum values here */
54         case MDG_DEVICE_TYPE_LOCAL:
55                 return "Local";
56         case MDG_DEVICE_TYPE_REMOTE:
57                 return "Remote";
58         default:
59                 return "Unknown";
60         }
61 }
62
63 static void _destroy_mdg_device_info(gpointer data)
64 {
65         mdg_device_info_destroy((mdg_device_h)data);
66 }
67
68 void receive_request_result(char *cmd, char *device_id, unsigned char *arg,
69                                                         int len, int ret, void *user_data)
70 {
71         msg("_request_result_cb is called command %s Requester id %s", cmd,
72                                 device_id);
73
74         if (cmd == NULL) {
75                 msgp("cmd is null [%s(0x%X)]", mdg_error_to_string(ret), ret);
76                 return;
77         }
78
79         if (strncmp("COMP_REQ_CREATE_GROUP", cmd, strlen(cmd)) == 0) {
80                 msgp("[CMD] Request Create Group [%s(0x%X)]", mdg_error_to_string(ret),
81                          ret);
82                 if (ret == MDG_ERROR_NONE && arg != NULL) {
83                         msgb("Created Group name is %s", arg);
84                         run_group_find(NULL, NULL);
85                         run_devices_find(NULL, NULL);
86                 }
87         } else if (strncmp("COMP_REQ_INVITE_DEVICE", cmd, strlen(cmd)) == 0) {
88                 msgp("[CMD] Request Invite [%s(0x%X)]", mdg_error_to_string(ret), ret);
89                 if (ret == MDG_ERROR_NONE && arg != NULL)
90                         msgb("Invited Device ID is %s", arg);
91         } else if (strncmp("COMP_REQ_EJECT_DEVICE", cmd, strlen(cmd)) == 0) {
92                 msgp("[CMD] Request Eject [%s(0x%X)]", mdg_error_to_string(ret), ret);
93                 if (ret == MDG_ERROR_NONE && arg != NULL)
94                         msgb("Ejected Device ID is %s", arg);
95         } else if (strncmp("COMP_REQ_DELETE_GROUP", cmd, strlen(cmd)) == 0) {
96                 msgp("[CMD] Request Delete Group [%s(0x%X)]", mdg_error_to_string(ret),
97                          ret);
98                 if (ret == MDG_ERROR_NONE && arg != NULL)
99                         msgb("Deleted Group name is %s", arg);
100         } else if (strncmp("COMP_REQ_SEND_DATA", cmd, strlen(cmd)) == 0) {
101                 msgp("Received data [%d] %s", strlen((char *)arg), arg);
102         } else {
103                 msgp("[Recv][%d] %s", strlen((char *) arg), arg);
104         }
105 }
106
107 void _device_eject_result_cb(int result, void *user_data)
108 {
109         msgb("\rEject Device Complete [%s]", mdg_error_to_string(result));
110 }
111
112 static int run_device_eject(MManager *mm, struct menu_data *menu)
113 {
114         int ret;
115         int idx;
116         mdg_group_h group = NULL;
117         mdg_device_h device = NULL;
118
119         msg("Eject Device");
120
121         if (strlen(group_idx)) {
122                 idx = (unsigned short)strtol(group_idx, NULL, 10);
123                 if (0 >= idx) {
124                         msgp("Invalid index. set to 1");
125                         idx = 1;
126                 }
127         }
128
129         if (found_group_list) {
130                 group = g_list_nth_data(found_group_list, idx - 1);
131                 if (NULL == group) {
132                         msgr("Failed to g_hash_table_find");
133                         return RET_FAILURE;
134                 }
135         }
136
137         if (strlen(device_idx)) {
138                 idx = (unsigned short)strtol(device_idx, NULL, 10);
139                 if (0 >= idx) {
140                         msgp("Invalid index. set to 1");
141                         idx = 1;
142                 }
143         }
144
145         if (NULL == found_invited_device_list) {
146                 msgr("Find device first");
147                 return RET_FAILURE;
148         }
149
150         device = g_list_nth_data(found_invited_device_list, idx - 1);
151         if (NULL == device) {
152                 msgr("Failed to g_hash_table_find");
153                 return RET_FAILURE;
154         }
155
156         ret = mdg_group_eject_device(handle, group, device, _device_eject_result_cb, NULL);
157         if (MDG_ERROR_NONE != ret) {
158                 msgr("Failed to Delete Group: [%s(0x%X)]", mdg_error_to_string(ret), ret);
159                 return RET_FAILURE;
160         }
161         msg(" - mdg_group_eject_device() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
162
163         return RET_SUCCESS;
164 }
165
166 bool __group_foreach_cb(mdg_device_h device, void *user_data)
167 {
168         char *device_id;
169
170         mdg_device_info_get_device_id(device, &device_id);
171         msgb("\rDevice is [%s]", device_id);
172
173         found_invited_device_list = g_list_append(found_invited_device_list, device);
174
175         return true;
176 }
177
178 static int run_group_foreach_device(MManager *mm, struct menu_data *menu)
179 {
180         int ret;
181         int idx;
182         mdg_group_h group = NULL;
183
184         msg("Foreach Device");
185
186         if (strlen(group_idx)) {
187                 idx = (unsigned short)strtol(group_idx, NULL, 10);
188                 if (0 >= idx) {
189                         msgp("Invalid index. set to 1");
190                         idx = 1;
191                 }
192         }
193
194         if (found_group_list) {
195                 group = g_list_nth_data(found_group_list, idx - 1);
196                 if (NULL == group) {
197                         msgr("Failed to g_hash_table_find");
198                         return RET_FAILURE;
199                 }
200         }
201
202         if (found_invited_device_list) {
203                 g_list_free_full(found_device_list, _destroy_mdg_device_info);
204                 found_invited_device_list = NULL;
205         }
206
207         ret = mdg_group_foreach_device(handle, group, __group_foreach_cb, NULL);
208         if (MDG_ERROR_NONE != ret) {
209                 msgr("Failed to foreach device: [%s(0x%X)]", mdg_error_to_string(ret), ret);
210                 return RET_FAILURE;
211         }
212         msg(" - mdg_group_foreach_device() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
213
214         return RET_SUCCESS;
215 }
216
217 void _device_invite_result_cb(int result, mdg_device_h device, void *user_data)
218 {
219         bool is_invited;
220         mdg_device_info_is_invited(device, &is_invited);
221         msgb("\rInvite Device Finished [%s]", mdg_error_to_string(result));
222         msgb("\rThe device invited is : %d", is_invited);
223 }
224
225 static int run_device_invite(MManager *mm, struct menu_data *menu)
226 {
227         int ret;
228         int idx;
229         mdg_group_h group = NULL;
230         mdg_device_h device = NULL;
231
232         msg("Invite Device");
233
234         if (strlen(group_idx)) {
235                 idx = (unsigned short)strtol(group_idx, NULL, 10);
236                 if (0 >= idx) {
237                         msgp("Invalid index. set to 1");
238                         idx = 1;
239                 }
240         }
241
242         if (found_group_list) {
243                 group = g_list_nth_data(found_group_list, idx - 1);
244                 if (NULL == group) {
245                         msgr("Failed to g_hash_table_find");
246                         return RET_FAILURE;
247                 }
248         }
249
250         if (strlen(device_idx)) {
251                 idx = (unsigned short)strtol(device_idx, NULL, 10);
252                 if (0 >= idx) {
253                         msgp("Invalid index. set to 1");
254                         idx = 1;
255                 }
256         }
257
258         if (NULL == found_device_list) {
259                 msgr("Find device first");
260                 return RET_FAILURE;
261         }
262
263         device = g_list_nth_data(found_device_list, idx - 1);
264         if (NULL == device) {
265                 msgr("Failed to g_hash_table_find");
266                 return RET_FAILURE;
267         }
268
269         ret = mdg_group_invite_device(handle, group, device, pin, _device_invite_result_cb, NULL);
270         if (MDG_ERROR_NONE != ret) {
271                 msgr("Failed to device invite: [%s(0x%X)]", mdg_error_to_string(ret), ret);
272                 return RET_FAILURE;
273         }
274         msg(" - mdg_group_invite_device() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
275
276         return RET_SUCCESS;
277 }
278
279
280 static int run_devices_show(MManager *mm, struct menu_data *menu)
281 {
282         mdg_device_h device;
283
284         int i;
285         GList *iter = NULL;
286
287         /* Get a first item */
288         i = 0;
289         iter = g_list_first(found_device_list);
290         while (NULL != iter) {
291                 device = iter->data;
292                 if (!device) {
293                         msgr("device is null");
294                         break;
295                 }
296
297                 char *model_name;
298                 char *device_id;
299                 mdg_device_info_get_model_name(device, &model_name);
300                 mdg_device_info_get_device_id(device, &device_id);
301
302                 msgp("[%d] Model : %s, Device ID : %s", i+1, model_name, device_id);
303
304                 mdg_device_h cloned_device;
305                 char *cloned_model_name;
306                 char *cloned_device_id;
307
308                 mdg_device_info_clone(&cloned_device, device);
309                 mdg_device_info_get_model_name(cloned_device, &cloned_model_name);
310                 mdg_device_info_get_device_id(cloned_device, &cloned_device_id);
311
312                 msgp("*Cloned* [%d] Model : %s, Device ID : %s", i+1, cloned_model_name, cloned_device_id);
313
314                 if (model_name)
315                         free(model_name);
316
317                 if (device_id)
318                         free(device_id);
319
320                 if (cloned_model_name)
321                         free(cloned_model_name);
322
323                 if (cloned_device_id)
324                         free(cloned_device_id);
325
326                 mdg_device_info_destroy(cloned_device);
327
328                 /* Next item */
329                 iter = g_list_next(iter);
330                 i++;
331         }
332
333         return RET_SUCCESS;
334 }
335
336 static int run_invited_devices_show(MManager *mm, struct menu_data *menu)
337 {
338         mdg_device_h device;
339
340         int i;
341         GList *iter = NULL;
342
343         /* Get a first item */
344         i = 0;
345         iter = g_list_first(found_invited_device_list);
346         while (NULL != iter) {
347                 device = iter->data;
348                 if (!device) {
349                         msgr("device is null");
350                         break;
351                 }
352
353                 char *model_name;
354                 char *device_id;
355
356                 mdg_device_info_get_device_id(device, &device_id);
357                 mdg_device_info_get_model_name(device, &model_name);
358
359                 msgp("[%d] Device : %s, Model : %s", i+1, device_id, model_name);
360
361                 if (model_name)
362                         free(model_name);
363
364                 if (device_id)
365                         free(device_id);
366
367                 /* Next item */
368                 iter = g_list_next(iter);
369                 i++;
370         }
371
372         return RET_SUCCESS;
373 }
374
375 typedef struct _channel_t {
376         char *device_id; /**< User callback to be called */
377         char *channel_id; /**< User data pointer */
378 } channel_t;
379
380 static int run_channels_show(MManager *mm, struct menu_data *menu)
381 {
382         channel_t *channel;
383
384         int i;
385         GList *iter = NULL;
386
387         /* Get a first item */
388         i = 0;
389         iter = g_list_first(found_channel_list);
390         while (NULL != iter) {
391                 channel = iter->data;
392                 if (!channel) {
393                         msgr("channel is null");
394                         break;
395                 }
396
397                 msgp("[%d] device id : %s, channel id : %s,", i+1, channel->device_id, channel->channel_id);
398
399                 /* Next item */
400                 iter = g_list_next(iter);
401                 i++;
402         }
403
404         return RET_SUCCESS;
405 }
406
407 #if 0
408 static int run_group_get_members(MManager *mm, struct menu_data *menu)
409 {
410         int ret;
411         int idx;
412         int count;
413         mdg_group_h group = NULL;
414         mdg_device_h **devices = NULL;
415
416         msg("Get Group Members");
417
418         if (strlen(group_idx)) {
419                 idx = (unsigned short)strtol(group_idx, NULL, 10);
420                 if (0 >= idx) {
421                         msgp("Invalid index. set to 1");
422                         idx = 1;
423                 }
424         }
425
426         if (found_group_list) {
427                 group = g_list_nth_data(found_group_list, idx - 1);
428                 if (NULL == group) {
429                         msgr("Failed to g_hash_table_find");
430                         return RET_FAILURE;
431                 }
432         }
433
434         ret = mdg_group_get_member_devices(group, devices, &count);
435         if (MDG_ERROR_NONE != ret) {
436                 msgr("Failed to Delete Group: [%s(0x%X)]", mdg_error_to_string(ret), ret);
437                 return RET_FAILURE;
438         }
439         msg(" - mdg_group_get_member_devices() ret: [0x%X] [%s]", ret,
440                 mdg_error_to_string(ret));
441
442         for (int i = 0; i < count; i++) {
443
444                 char *device_id = NULL;
445                 char *ip = NULL;
446                 mdg_device_type_e device_type;
447
448                 mdg_device_info_get_device_id(devices[i], &device_id);
449                 mdg_device_info_get_ip(devices[i], &ip);
450                 mdg_device_info_get_device_type(devices[i], &device_type);
451
452                 msgp("device_id : %s, name : %s, type : %s", device_id, ip,
453                         __device_type_to_string(device_type));
454
455                 if (device_id)
456                         free(device_id);
457                 if (ip)
458                         free(ip);
459         }
460
461         return RET_SUCCESS;
462 }
463 #endif
464
465 static int run_group_delete(MManager *mm, struct menu_data *menu)
466 {
467         int ret;
468         int idx;
469         mdg_group_h group;
470
471         msg("Delete Group");
472
473         if (strlen(group_idx)) {
474                 idx = (unsigned short)strtol(group_idx, NULL, 10);
475                 if (0 >= idx) {
476                         msgp("Invalid index. set to 1");
477                         idx = 1;
478                 }
479         }
480
481         if (found_group_list) {
482                 group = g_list_nth_data(found_group_list, idx - 1);
483                 if (NULL == group) {
484                         msgr("Failed to g_hash_table_find");
485                         return RET_FAILURE;
486                 }
487         }
488
489         ret = mdg_group_delete(handle, group);
490         if (MDG_ERROR_NONE != ret) {
491                 msgr("Failed to Delete Group: [%s(0x%X)]", mdg_error_to_string(ret), ret);
492                 return RET_FAILURE;
493         }
494         msg(" - mdg_group_delete() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
495
496         return RET_SUCCESS;
497 }
498
499 void _device_finish_cb(int result, void *user_data)
500 {
501         msgb("\rFind Device Finished ret: [0x%X] [%s]", result, mdg_error_to_string(result));
502 }
503
504 bool _device_found_cb(mdg_device_h device, void *user_data)
505 {
506         char *addr = NULL;
507         char *device_id = NULL;
508         char *model_name = NULL;
509         mdg_device_type_e device_type;
510         GList *iter = NULL;
511         gboolean is_exist = FALSE;
512
513         mdg_device_info_get_device_id(device, &device_id);
514         mdg_device_info_get_model_name(device, &model_name);
515         mdg_device_info_get_type(device, &device_type);
516
517         iter = found_device_list;
518         while (iter != NULL) {
519                 mdg_device_h temp = (mdg_device_h)iter->data;
520                 char *temp_device_id;
521
522                 mdg_device_info_get_device_id(temp, &temp_device_id);
523
524                 if (g_strcmp0(device_id, temp_device_id) == 0) {
525                         is_exist = TRUE;
526                         free(temp_device_id);
527                         mdg_device_info_destroy(device);
528                         break;
529                 }
530
531                 free(temp_device_id);
532                 iter = g_list_next(iter);
533         }
534
535         if (is_exist == FALSE) {
536                 found_device_list = g_list_append(found_device_list, device);
537                 msgp("\r[ID] %s [Type] %s [Name] %s", device_id,
538                         __device_type_to_string(device_type), model_name);
539         }
540
541         if (device_id)
542                 free(device_id);
543         if (addr)
544                 free(addr);
545
546         return TRUE;
547 }
548
549 static int run_devices_find(MManager *mm, struct menu_data *menu)
550 {
551         int ret;
552         int duration;
553         msg("Find Devices");
554
555         if (strlen(timeout))
556                 duration = (unsigned short)strtol(timeout, NULL, 10);
557
558         ret = mdg_device_find(handle, duration, false, _device_found_cb, _device_finish_cb, NULL);
559         if (MDG_ERROR_NONE != ret) {
560                 msgr("Failed to Find Devices: [%s(0x%X)]", mdg_error_to_string(ret), ret);
561                 return RET_FAILURE;
562         }
563         msg(" - mdg_device_find() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
564
565         if (found_device_list) {
566                 g_list_free_full(found_device_list, _destroy_mdg_device_info);
567                 found_device_list = NULL;
568         }
569
570         return RET_SUCCESS;
571 }
572
573 void __receive_file_cb(int result, char *device_id, const char *file_path, void *user_data)
574 {
575         msgb("The %s is stored by %s", file_path, device_id);
576 }
577
578 int run_device_show_local_device(MManager *mm, struct menu_data *menu)
579 {
580         int ret = 0;
581         mdg_device_h device = NULL;
582
583         msg("Get Local Device");
584
585         ret = mdg_device_get_local_device(handle, &device);
586         if (MDG_ERROR_NONE != ret) {
587                 msgr("Failed to Get My Device: [%s(0x%X)]", mdg_error_to_string(ret), ret);
588                 return RET_FAILURE;
589         }
590         msg(" - mdg_device_info_get_local_device() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
591
592         if (ret == 0) {
593                 char *device_id = NULL;
594                 char *model_name;
595                 char *device_name;
596                 char *platform_ver;
597                 char *vendor_id;
598                 char *profile;
599                 bool is_invited;
600                 mdg_device_type_e type;
601
602                 mdg_device_info_get_device_id(device, &device_id);
603                 mdg_device_info_get_model_name(device, &model_name);
604                 mdg_device_info_get_device_name(device, &device_name);
605                 mdg_device_info_get_platform_ver(device, &platform_ver);
606                 mdg_device_info_get_vendor_id(device, &vendor_id);
607                 mdg_device_info_get_profile(device, &profile);
608                 mdg_device_info_is_invited(device, &is_invited);
609                 mdg_device_info_get_type(device, &type);
610
611                 msgb("\n[ID] %s", device_id);
612                 msgb("[Model Name] %s", model_name);
613                 msgb("[Device Name] %s", device_name);
614                 msgb("[Platform Ver.] %s", platform_ver);
615                 msgb("[Vendor ID] %s", vendor_id);
616                 msgb("[Profile] %s", profile);
617                 msgb("[Is Invited] %d", is_invited);
618                 msgb("[Type] %s\n", __device_type_to_string(type));
619
620                 if (device_id)
621                         free(device_id);
622                 if (device_name)
623                         free(device_name);
624                 if (platform_ver)
625                         free(platform_ver);
626                 if (vendor_id)
627                         free(vendor_id);
628                 if (profile)
629                         free(profile);
630
631                 mdg_device_info_destroy(device);
632                 device = NULL;
633         }
634
635
636         mdg_set_receive_file_cb(handle, __receive_file_cb, NULL);
637
638         return RET_SUCCESS;
639 }
640
641 static int run_group_show(MManager *mm, struct menu_data *menu)
642 {
643         mdg_group_h group;
644
645         int i;
646         GList *iter = NULL;
647
648         /* Get a first item */
649         i = 0;
650         iter = g_list_first(found_group_list);
651         while (NULL != iter) {
652                 group = iter->data;
653                 if (!group) {
654                         msgr("groups is null");
655                         break;
656                 }
657
658                 char *device_id;
659                 char *name;
660                 mdg_group_type_e type;
661
662                 mdg_group_info_get_device_id(group, &device_id);
663                 mdg_group_info_get_name(group, &name);
664                 mdg_group_info_get_type(group, &type);
665
666                 msgp("[%d] type: %s, name: %s", i+1, mdg_group_type_to_string(type), name);
667                 msgp("     device_id: %s\n", device_id);
668
669                 mdg_group_h cloned_group;
670                 char *cloned_device_id;
671                 char *cloned_name;
672                 mdg_group_type_e cloned_type;
673
674                 mdg_group_info_clone(&cloned_group, group);
675                 mdg_group_info_get_device_id(cloned_group, &cloned_device_id);
676                 mdg_group_info_get_name(cloned_group, &cloned_name);
677                 mdg_group_info_get_type(cloned_group, &cloned_type);
678
679                 msgp("*Cloned* [%d] type: %s, name: %s", i+1, mdg_group_type_to_string(cloned_type), cloned_name);
680                 msgp("     device_id: %s\n", cloned_device_id);
681
682                 if (device_id)
683                         free(device_id);
684                 if (name)
685                         free(name);
686
687                 if (cloned_device_id)
688                         free(cloned_device_id);
689                 if (cloned_name)
690                         free(cloned_name);
691
692                 mdg_group_info_destroy(cloned_group);
693
694                 /* Next item */
695                 iter = g_list_next(iter);
696                 i++;
697         }
698
699         return RET_SUCCESS;
700 }
701
702 static void _destroy_mdg_group_info(gpointer data)
703 {
704         mdg_group_info_destroy((mdg_group_h)data);
705 }
706
707 bool _group_found_cb(mdg_group_type_e type, mdg_group_h group, void *user_data)
708 {
709         char *group_name;
710
711         mdg_group_info_get_name(group, &group_name);
712
713         if (type == MDG_GROUP_TYPE_LOCAL)
714                 msgp("\rfound group type : MINE, name : %s", group_name);
715         else
716                 msgp("\rfound group type : REMOTE, name : %s", group_name);
717
718         found_group_list = g_list_append(found_group_list, group);
719
720         return TRUE;
721 }
722
723 void _group_finish_cb(int result, void *user_data)
724 {
725         msgb("\rFind Group Finished ret: [0x%X] [%s]", result, mdg_error_to_string(result));
726 }
727
728 static int run_group_find(MManager *mm, struct menu_data *menu)
729 {
730         int ret;
731         int duration;
732         msg("Find Group");
733
734         if (strlen(timeout))
735                 duration = (unsigned short)strtol(timeout, NULL, 10);
736
737         if (found_group_list) {
738                 g_list_free_full(found_group_list, _destroy_mdg_group_info);
739                 found_group_list = NULL;
740         }
741
742         ret = mdg_group_find(handle, duration, _group_found_cb, _group_finish_cb, NULL);
743         if (MDG_ERROR_NONE != ret) {
744                 msgr("Failed to Find Group: [%s(0x%X)]", mdg_error_to_string(ret), ret);
745                 return RET_FAILURE;
746         }
747         msg(" - mdg_group_find() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
748
749         return RET_SUCCESS;
750 }
751
752 static int run_group_create(MManager *mm, struct menu_data *menu)
753 {
754         int ret;
755         msg("Create Group");
756
757         ret = mdg_group_create(handle, groupid);
758         if (MDG_ERROR_NONE != ret) {
759                 msgr("Failed to Create Group: [%s(0x%X)]", mdg_error_to_string(ret), ret);
760                 return RET_FAILURE;
761         }
762         msg(" - mdg_group_create() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
763         msgb("Create Group Successfully");
764
765         return RET_SUCCESS;
766 }
767
768 void _send_data_finish_cb(int result, void *user_data)
769 {
770         msgb("\rFind Send Data Finished = %d", result);
771 }
772
773 static int __send_data(int devidx, int chaidx)
774 {
775         int ret = 0;
776         char *deviceid = NULL;
777         char *address = NULL;
778         mdg_device_h device = NULL;
779         channel_t *channel = NULL;
780         if (found_invited_device_list) {
781                 device = g_list_nth_data(found_invited_device_list, devidx - 1);
782                 if (NULL == device) {
783                         msgr("Find local device first");
784                         return RET_FAILURE;
785                 }
786         }
787
788         if (found_channel_list) {
789                 channel = g_list_nth_data(found_channel_list, chaidx - 1);
790                 if (NULL == channel) {
791                         msgr("Find local device first");
792                         return RET_FAILURE;
793                 }
794         }
795
796         mdg_device_info_get_device_id(device, &deviceid);
797         msgp("Sent to [ID] %s [CHANNEL ID] %s", deviceid, channel->channel_id);
798         if (deviceid) {
799                 free(deviceid);
800                 deviceid = NULL;
801         }
802         if (address) {
803                 free(address);
804                 address = NULL;
805         }
806
807         ret = mdg_device_send_data(handle, device, channel->channel_id, (unsigned char *)message,
808                                                  strlen(message), _send_data_finish_cb, NULL);
809         if (MDG_ERROR_NONE != ret)
810                 msgr("Failed to Send Data: [ID] %s [IP] %s", deviceid, address);
811
812         return ret;
813 }
814
815 static int run_send_data(MManager *mm, struct menu_data *menu)
816 {
817         int devidx = 0;
818         int chaidx = 0;
819         int count = g_list_length(found_invited_device_list);
820
821         if (0 >= count) {
822                 msgr("No Device");
823                 return RET_SUCCESS;
824         }
825
826         if (strlen(device_idx)) {
827                 devidx = (unsigned short)strtol(device_idx, NULL, 10);
828                 if (0 >= devidx) {
829                         msgp("Invalid index. set to 1");
830                         devidx = 1;
831                 }
832         }
833         if (strlen(channel_idx)) {
834                 chaidx = (unsigned short)strtol(channel_idx, NULL, 10);
835                 if (0 >= chaidx) {
836                         msgp("Invalid index. set to 1");
837                         chaidx = 1;
838                 }
839         }
840         return __send_data(devidx, chaidx);
841 }
842
843 void __send_file_progress_cb(const char *file_path, long send_size,
844         long total_size, int percent, void *user_data)
845 {
846         msgp("%s  %d%% [%ld/%ld]", file_path, percent, send_size, total_size);
847 }
848 void __send_file_finish_cb(int result, mdg_device_h device, void *user_data)
849 {
850         char *device_id;
851
852         mdg_device_info_get_device_id(device, &device_id);
853
854         msgp("Send file to %s Finished [%s]", device_id, mdg_error_to_string(result));
855 }
856
857 static int __send_file(int devidx)
858 {
859         int ret = 0;
860         char *deviceid = NULL;
861         char *address = NULL;
862         mdg_device_h device = NULL;
863
864         if (found_invited_device_list) {
865                 device = g_list_nth_data(found_invited_device_list, devidx - 1);
866                 if (NULL == device) {
867                         msgr("Find local device first");
868                         return RET_FAILURE;
869                 }
870         }
871
872         mdg_device_info_get_device_id(device, &deviceid);
873         msgp("Sent to [ID] %s", deviceid);
874         if (deviceid) {
875                 free(deviceid);
876                 deviceid = NULL;
877         }
878         if (address) {
879                 free(address);
880                 address = NULL;
881         }
882
883         ret = mdg_device_send_file(handle, device, file_path,
884                 __send_file_progress_cb, __send_file_finish_cb, NULL);
885         if (MDG_ERROR_NONE != ret)
886                 msgr("Failed to Send Data: [ID] %s [IP] %s", deviceid, address);
887
888         return ret;
889 }
890
891 static int run_send_file(MManager *mm, struct menu_data *menu)
892 {
893         int devidx = 0;
894         int count = g_list_length(found_invited_device_list);
895
896         if (0 >= count) {
897                 msgr("No Device");
898                 return RET_SUCCESS;
899         }
900
901         if (strlen(device_idx)) {
902                 devidx = (unsigned short)strtol(device_idx, NULL, 10);
903                 if (0 >= devidx) {
904                         msgp("Invalid index. set to 1");
905                         devidx = 1;
906                 }
907         }
908
909         return __send_file(devidx);
910 }
911
912 void _invited_device_finish_cb(int result, void *user_data)
913 {
914         msgb("\rFind My Owned Devices Finished ret: [0x%X] [%s]", result,
915                 mdg_error_to_string(result));
916 }
917
918 bool _invited_device_found_cb(mdg_device_h device, void *user_data)
919 {
920         char *device_id = NULL;
921         mdg_device_type_e device_type;
922
923         mdg_device_info_get_device_id(device, &device_id);
924         mdg_device_info_get_type(device, &device_type);
925
926         msgp("\r[ID] %s [Type] %s", device_id, __device_type_to_string(device_type));
927
928         if (device_id)
929                 free(device_id);
930
931         found_invited_device_list = g_list_append(found_invited_device_list, device);
932
933         return TRUE;
934 }
935
936 static int run_devices_find_invited_device(MManager *mm, struct menu_data *menu)
937 {
938         int ret;
939         int duration;
940         msg("Find My Owned Devices");
941
942         if (strlen(timeout))
943                 duration = (unsigned short)strtol(timeout, NULL, 10);
944
945         ret = mdg_device_find(handle, duration, true, _invited_device_found_cb,
946                 _invited_device_finish_cb, NULL);
947         if (MDG_ERROR_NONE != ret) {
948                 msgr("Failed to Find My Owned Devices: [%s(0x%X)]",
949                         mdg_error_to_string(ret), ret);
950                 return RET_FAILURE;
951         }
952
953         msg(" - mdg_device_find_invited_device() ret: [0x%X] [%s]",
954                 ret, mdg_error_to_string(ret));
955
956         if (found_invited_device_list) {
957                 g_list_free_full(found_invited_device_list, _destroy_mdg_device_info);
958                 found_invited_device_list = NULL;
959         }
960
961         return RET_SUCCESS;
962 }
963
964 static int run_request_create_group(MManager *mm, struct menu_data *menu)
965 {
966         int ret = 0;
967         int dev_count = 0;
968         mdg_device_h device = NULL;
969
970         msg("Request create group");
971
972         dev_count = g_list_length(found_invited_device_list);
973
974         if (0 >= dev_count) {
975                 msgr("No Device");
976                 return RET_SUCCESS;
977         }
978
979         int dev_idx;
980         if (device_idx != NULL && strlen(device_idx)) {
981                 dev_idx = (unsigned short)strtol(device_idx, NULL, 10);
982                 if (0 >= dev_idx) {
983                         msgp("Invalid index. set to 1");
984                         dev_idx = 1;
985                 }
986         }
987
988         if (found_invited_device_list) {
989                 device = g_list_nth_data(found_invited_device_list, dev_idx - 1);
990                 if (NULL == device) {
991                         msgr("Failed to g_hash_table_find");
992                         return RET_FAILURE;
993                 }
994         }
995
996         ret = mdg_request_create_group(handle, device, request_groupid,
997                 receive_request_result, NULL);
998         if (MDG_ERROR_NONE != ret) {
999                 msgr("Failed to Request Create Group: [%s(0x%X)]",
1000                         mdg_error_to_string(ret), ret);
1001                 return RET_FAILURE;
1002         }
1003
1004         msg(" - mdg_request_create_group() ret: [0x%X] [%s]",
1005                 ret, mdg_error_to_string(ret));
1006
1007         return RET_SUCCESS;
1008 }
1009
1010 static int run_request_invite(MManager *mm, struct menu_data *menu)
1011 {
1012         int ret = 0;
1013         int grp_count = 0;
1014         int dev_count = 0;
1015         mdg_device_h device = NULL;
1016         mdg_group_h group = NULL;
1017
1018         dev_count = g_list_length(found_invited_device_list);
1019         grp_count = g_list_length(found_group_list);
1020
1021         if (0 >= dev_count || 0 >= grp_count) {
1022                 msgr("No Device");
1023                 return RET_SUCCESS;
1024         }
1025
1026         int grp_idx;
1027         if (group_idx != NULL && strlen(group_idx)) {
1028                 grp_idx = (unsigned short)strtol(group_idx, NULL, 10);
1029                 if (0 >= grp_idx) {
1030                         msgp("Invalid index. set to 1");
1031                         grp_idx = 1;
1032                 }
1033         }
1034
1035         if (found_group_list) {
1036                 group = g_list_nth_data(found_group_list, grp_idx - 1);
1037                 if (NULL == group) {
1038                         msgr("Failed to g_hash_table_find");
1039                         return RET_FAILURE;
1040                 }
1041         }
1042
1043         int dev_idx;
1044         if (device_idx != NULL && strlen(device_idx)) {
1045                 dev_idx = (unsigned short)strtol(device_idx, NULL, 10);
1046                 if (0 >= dev_idx) {
1047                         msgp("Invalid index. set to 1");
1048                         dev_idx = 1;
1049                 }
1050         }
1051
1052         if (found_invited_device_list) {
1053                 device = g_list_nth_data(found_invited_device_list, dev_idx - 1);
1054                 if (NULL == device) {
1055                         msgr("Failed to g_hash_table_find");
1056                         return RET_FAILURE;
1057                 }
1058         }
1059
1060         ret = mdg_request_invite_device(handle, group, device, pin,
1061                 receive_request_result, NULL);
1062         if (MDG_ERROR_NONE != ret) {
1063                 msgr("Failed to Request Invite: [%s(0x%X)]", mdg_error_to_string(ret), ret);
1064                 return RET_FAILURE;
1065         }
1066
1067         msg(" - mdg_request_invite_device() ret: [0x%X] [%s]",
1068                 ret, mdg_error_to_string(ret));
1069
1070         return RET_SUCCESS;
1071 }
1072
1073 static int run_request_eject(MManager *mm, struct menu_data *menu)
1074 {
1075         int ret = 0;
1076         int grp_count = 0;
1077         int dev_count = 0;
1078         mdg_device_h device = NULL;
1079         mdg_group_h group = NULL;
1080
1081         dev_count = g_list_length(found_invited_device_list);
1082         grp_count = g_list_length(found_group_list);
1083
1084         if (0 >= dev_count || 0 >= grp_count) {
1085                 msgr("No Device");
1086                 return RET_SUCCESS;
1087         }
1088
1089         int grp_idx;
1090         if (group_idx != NULL && strlen(group_idx)) {
1091                 grp_idx = (unsigned short)strtol(group_idx, NULL, 10);
1092                 if (0 >= grp_idx) {
1093                         msgp("Invalid index. set to 1");
1094                         grp_idx = 1;
1095                 }
1096         }
1097
1098         if (found_group_list) {
1099                 group = g_list_nth_data(found_group_list, grp_idx - 1);
1100                 if (NULL == group) {
1101                         msgr("Failed to g_hash_table_find");
1102                         return RET_FAILURE;
1103                 }
1104         }
1105
1106         int dev_idx;
1107         if (device_idx != NULL && strlen(device_idx)) {
1108                 dev_idx = (unsigned short)strtol(device_idx, NULL, 10);
1109                 if (0 >= dev_idx) {
1110                         msgp("Invalid index. set to 1");
1111                         dev_idx = 1;
1112                 }
1113         }
1114
1115         if (found_invited_device_list) {
1116                 device = g_list_nth_data(found_invited_device_list, dev_idx - 1);
1117                 if (NULL == device) {
1118                         msgr("Failed to g_hash_table_find");
1119                         return RET_FAILURE;
1120                 }
1121         }
1122
1123         ret = mdg_request_eject_device(handle, group, device, receive_request_result,
1124                 NULL);
1125         if (MDG_ERROR_NONE != ret) {
1126                 msgr("Failed to Request Eject: [%s(0x%X)]", mdg_error_to_string(ret), ret);
1127                 return RET_FAILURE;
1128         }
1129
1130         msg(" - mdg_request_eject_device() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
1131
1132         return RET_SUCCESS;
1133 }
1134
1135 void __req_channel_list_finish_cb(char *device_id, char *channel_id, void *user_data)
1136 {
1137         msgp("\rDevice ID : [%s], Channel ID : [%s]", device_id, channel_id);
1138
1139         channel_t *channel = g_new0(channel_t, 1);
1140         channel->device_id = g_strdup(device_id);
1141         channel->channel_id = g_strdup(channel_id);
1142
1143         found_channel_list = g_list_append(found_channel_list, channel);
1144
1145         return;
1146 }
1147
1148 static int run_request_channel_list(MManager *mm, struct menu_data *menu)
1149 {
1150         int ret = 0;
1151         int dev_count = 0;
1152         mdg_device_h device = NULL;
1153
1154         dev_count = g_list_length(found_invited_device_list);
1155
1156         if (0 >= dev_count) {
1157                 msgr("No Device");
1158                 return RET_SUCCESS;
1159         }
1160
1161         int dev_idx;
1162         if (device_idx != NULL && strlen(device_idx)) {
1163                 dev_idx = (unsigned short)strtol(device_idx, NULL, 10);
1164                 if (0 >= dev_idx) {
1165                         msgp("Invalid index. set to 1");
1166                         dev_idx = 1;
1167                 }
1168         }
1169
1170         if (found_invited_device_list) {
1171                 device = g_list_nth_data(found_invited_device_list, dev_idx - 1);
1172                 if (NULL == device) {
1173                         msgr("Failed to g_hash_table_find");
1174                         return RET_FAILURE;
1175                 }
1176         }
1177
1178         ret = mdg_request_channel_list(handle, device, __req_channel_list_finish_cb, NULL);
1179         if (MDG_ERROR_NONE != ret) {
1180                 msgr("Failed to Request Eject: [%s(0x%X)]", mdg_error_to_string(ret), ret);
1181                 return RET_FAILURE;
1182         }
1183
1184         msg(" - mdg_request_eject_device() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
1185
1186         return RET_SUCCESS;
1187 }
1188
1189 void __channel_cb(int result, char *device_id, char *channel_id,
1190         unsigned char *arg, int len, void *user_data)
1191 {
1192         msg("[%s] channel callback is called by [%s]'s send message", channel_id, device_id);
1193         msg("arg : %s, len : %d", arg, len);
1194         msg("result is %d", result);
1195 }
1196
1197 static int run_regist_channel(MManager *mm, struct menu_data *menu)
1198 {
1199         int ret = 0;
1200
1201         ret = mdg_device_regist_channel(handle, channel_id, __channel_cb, NULL);
1202
1203         msg(" - mdg_device_regist_channel() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
1204
1205         return RET_SUCCESS;
1206 }
1207
1208 static int run_unregist_channel(MManager *mm, struct menu_data *menu)
1209 {
1210         int ret = 0;
1211
1212         ret = mdg_device_unregist_channel(handle, channel_id);
1213
1214         msg(" - mdg_unregist_channel() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
1215
1216         return RET_SUCCESS;
1217 }
1218
1219 static struct menu_data menu_group_create[] = {
1220         { "0", "Group Name", NULL, NULL, groupid },
1221         { "1", "Run", NULL, run_group_create, NULL },
1222         { NULL, NULL, },
1223 };
1224
1225 static struct menu_data menu_group_find[] = {
1226         { "0", "Timeout", NULL, NULL, timeout },
1227         { "1", "Run", NULL, run_group_find, NULL },
1228         { NULL, NULL, },
1229 };
1230
1231 static struct menu_data menu_group_delete[] = {
1232         { "0", "Show Found Group(s)", NULL, run_group_show, NULL },
1233         { "1", "Index", NULL, NULL, group_idx },
1234         { "2", "Run", NULL, run_group_delete, NULL },
1235         { NULL, NULL, },
1236 };
1237
1238 static struct menu_data menu_devices_find[] = {
1239         { "0", "Timeout", NULL, NULL, timeout },
1240         { "1", "Run", NULL, run_devices_find, NULL },
1241         { NULL, NULL, },
1242 };
1243
1244 static struct menu_data menu_group_invite_device[] = {
1245         { "0", "Show Found Group(s)", NULL, run_group_show, NULL },
1246         { "1", "Show Found Device(s)", NULL, run_devices_show, NULL },
1247         { "2", "Group Index", NULL, NULL, group_idx },
1248         { "3", "Device Index", NULL, NULL, device_idx },
1249         { "4", "PIN", NULL, NULL, pin },
1250         { "5", "Run", NULL, run_device_invite, NULL },
1251         { NULL, NULL, },
1252 };
1253
1254 static struct menu_data menu_group_eject_device[] = {
1255         { "0", "Show Found Group(s)", NULL, run_group_show, NULL },
1256         { "1", "Show Found Invited Device(s)", NULL, run_invited_devices_show, NULL },
1257         { "2", "Group Index", NULL, NULL, group_idx },
1258         { "3", "Device Index", NULL, NULL, device_idx },
1259         { "4", "Run", NULL, run_device_eject, NULL },
1260         { NULL, NULL, },
1261 };
1262
1263 static struct menu_data menu_group_foreach_device[] = {
1264         { "0", "Show Found Group(s)", NULL, run_group_show, NULL },
1265         { "1", "Group Index", NULL, NULL, group_idx },
1266         { "2", "Run", NULL, run_group_foreach_device, NULL },
1267         { NULL, NULL, },
1268 };
1269
1270 static struct menu_data menu_send_data[] = {
1271         { "0", "Show Found Invited Device(s)", NULL, run_invited_devices_show, NULL },
1272         { "1", "Show Channel List", NULL, run_channels_show, NULL },
1273         { "2", "Message", NULL, NULL, message },
1274         { "3", "Device Index", NULL, NULL, device_idx },
1275         { "4", "Channel Index", NULL, NULL, channel_idx },
1276         { "5", "Send", NULL, run_send_data, NULL },
1277         { NULL, NULL, },
1278 };
1279
1280 static struct menu_data menu_send_file[] = {
1281         { "0", "Show Found Invited Device(s)", NULL, run_invited_devices_show, NULL },
1282         { "1", "File Path", NULL, NULL, file_path },
1283         { "2", "Device Index", NULL, NULL, device_idx },
1284         { "3", "Send", NULL, run_send_file, NULL },
1285         { NULL, NULL, },
1286 };
1287
1288 static struct menu_data menu_devices_find_invited_device[] = {
1289         { "0", "Timeout", NULL, NULL, timeout },
1290         { "1", "Run", NULL, run_devices_find_invited_device, NULL },
1291         { NULL, NULL, },
1292 };
1293
1294 static struct menu_data menu_request_create_group[] = {
1295         { "0", "Show Found Invited Device(s)", NULL, run_invited_devices_show, NULL },
1296         { "1", "Device Index", NULL, NULL, device_idx },
1297         { "2", "Request Group Name", NULL, NULL, request_groupid },
1298         { "3", "Run", NULL, run_request_create_group, NULL },
1299         { NULL, NULL, },
1300 };
1301
1302 static struct menu_data menu_request_invite[] = {
1303         { "0", "Show Found Group(s)", NULL, run_group_show, NULL },
1304         { "1", "Show Found Invited Device(s)", NULL, run_invited_devices_show, NULL },
1305         { "2", "Group Index", NULL, NULL, group_idx },
1306         { "3", "Device Index", NULL, NULL, device_idx },
1307         { "4", "PIN", NULL, NULL, pin },
1308         { "5", "Run", NULL, run_request_invite, NULL },
1309         { NULL, NULL, },
1310 };
1311
1312 static struct menu_data menu_request_eject[] = {
1313         { "0", "Show Found Group(s)", NULL, run_group_show, NULL },
1314         { "1", "Show Found Invited Device(s)", NULL, run_invited_devices_show, NULL },
1315         { "2", "Group Index", NULL, NULL, group_idx },
1316         { "3", "Device Index", NULL, NULL, device_idx },
1317         { "4", "Run", NULL, run_request_eject, NULL },
1318         { NULL, NULL, },
1319 };
1320
1321 static struct menu_data menu_request_channel_list[] = {
1322         { "0", "Show Found Invited Device(s)", NULL, run_invited_devices_show, NULL },
1323         { "1", "Device Index", NULL, NULL, device_idx },
1324         { "2", "Run", NULL, run_request_channel_list, NULL },
1325         { NULL, NULL, },
1326 };
1327
1328 static struct menu_data menu_regist_channel[] = {
1329         { "0", "Channel ID", NULL, NULL, channel_id },
1330         { "1", "Run", NULL, run_regist_channel, NULL },
1331         { NULL, NULL, },
1332 };
1333
1334 static struct menu_data menu_unregist_channel[] = {
1335         { "0", "Channel ID", NULL, NULL, channel_id },
1336         { "1", "Run", NULL, run_unregist_channel, NULL },
1337         { NULL, NULL, },
1338 };
1339
1340 struct menu_data menu_mdg_manager[] = {
1341         { "1", "Show Local Device", NULL, run_device_show_local_device, NULL }, // O
1342         { "2", "Create Group", menu_group_create, NULL, NULL }, // 0
1343         { "3", "Find Group(s)", menu_group_find, NULL, NULL }, // 0
1344         { "4", "Show Group(s)", NULL, run_group_show, NULL }, // 0
1345         { "5", "Delete Group", menu_group_delete, NULL, NULL }, // 0
1346         { "6", "Find Device(s)", menu_devices_find, NULL, NULL }, // 0
1347         { "7", "Show Device(s)", NULL, run_devices_show, NULL }, // 0
1348         { "8", "Invite Device", menu_group_invite_device, NULL, NULL }, // 0
1349         { "9", "Eject Device", menu_group_eject_device, NULL, NULL }, // 0
1350         { "10", "Foreach Device", menu_group_foreach_device, NULL, NULL },
1351         { "11", "Find Invited Device(s)", menu_devices_find_invited_device, NULL, NULL }, // 0
1352         { "12", "Show Invited Device(s)", NULL, run_invited_devices_show, NULL }, // 0
1353         { "13", "Request Create Group", menu_request_create_group, NULL, NULL }, // 0
1354         { "14", "Request Invite", menu_request_invite, NULL, NULL }, // 0
1355         { "15", "Request Eject", menu_request_eject, NULL, NULL }, // 0
1356         { "16", "Request Channel List", menu_request_channel_list, NULL, NULL }, // 0
1357         { "17", "Regist Channel", menu_regist_channel, NULL, NULL },
1358         { "18", "Unregist Channel", menu_unregist_channel, NULL, NULL },
1359         { "19", "Send Message", menu_send_data, NULL, NULL }, // 0
1360         { "20", "Send File", menu_send_file, NULL, NULL }, // 0
1361 };