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