2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
31 GList *found_group_list;
32 GList *found_device_list;
33 GList *found_invited_device_list;
34 GList *found_channel_list;
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";
46 static int run_group_find(MManager *mm, struct menu_data *menu);
47 static int run_devices_find(MManager *mm, struct menu_data *menu);
49 static const char* __device_type_to_string(mdg_device_type_e type)
52 /* CHECK: List all enum values here */
53 case MDG_DEVICE_TYPE_LOCAL:
55 case MDG_DEVICE_TYPE_REMOTE:
62 static void _destroy_mdg_device_info(gpointer data)
64 mdg_device_info_destroy((mdg_device_h)data);
67 void receive_request_result(char *cmd, char *device_id, unsigned char *arg,
68 int len, int ret, void *user_data)
70 msg("_request_result_cb is called command %s Requester id %s", cmd,
74 msgp("cmd is null [%s(0x%X)]", mdg_error_to_string(ret), ret);
78 if (strncmp("COMP_REQ_CREATE_GROUP", cmd, strlen(cmd)) == 0) {
79 msgp("[CMD] Request Create Group [%s(0x%X)]", mdg_error_to_string(ret), ret);
80 if (ret == MDG_ERROR_NONE && arg != NULL) {
81 msgb("Created Group name is %s", arg);
82 run_group_find(NULL, NULL);
83 run_devices_find(NULL, NULL);
85 } else if (strncmp("COMP_REQ_INVITE_DEVICE", cmd, strlen(cmd)) == 0) {
86 msgp("[CMD] Request Invite [%s(0x%X)]", mdg_error_to_string(ret), ret);
87 if (ret == MDG_ERROR_NONE && arg != NULL)
88 msgb("Invited Device ID is %s", arg);
89 } else if (strncmp("COMP_REQ_EJECT_DEVICE", cmd, strlen(cmd)) == 0) {
90 msgp("[CMD] Request Eject [%s(0x%X)]", mdg_error_to_string(ret), ret);
91 if (ret == MDG_ERROR_NONE && arg != NULL)
92 msgb("Ejected Device ID is %s", arg);
93 } else if (strncmp("COMP_REQ_DELETE_GROUP", cmd, strlen(cmd)) == 0) {
94 msgp("[CMD] Request Delete Group [%s(0x%X)]", mdg_error_to_string(ret), ret);
95 if (ret == MDG_ERROR_NONE && arg != NULL)
96 msgb("Deleted Group name is %s", arg);
97 } else if (strncmp("COMP_REQ_SEND_DATA", cmd, strlen(cmd)) == 0) {
98 msgp("Received data [%zd] %s", strlen((char *)arg), arg);
100 msgp("[Recv][%zd] %s", strlen((char *) arg), arg);
104 void _device_eject_result_cb(int result, void *user_data)
106 msgb("\rEject Device Complete [%s]", mdg_error_to_string(result));
109 static int run_device_eject(MManager *mm, struct menu_data *menu)
113 mdg_group_h group = NULL;
114 mdg_device_h device = NULL;
118 if (strlen(group_idx)) {
119 idx = (unsigned short)strtol(group_idx, NULL, 10);
121 msgp("Invalid index. set to 1");
126 if (found_group_list) {
127 group = g_list_nth_data(found_group_list, idx - 1);
129 msgr("Failed to g_hash_table_find");
134 if (strlen(device_idx)) {
135 idx = (unsigned short)strtol(device_idx, NULL, 10);
137 msgp("Invalid index. set to 1");
142 if (NULL == found_invited_device_list) {
143 msgr("Find device first");
147 device = g_list_nth_data(found_invited_device_list, idx - 1);
148 if (NULL == device) {
149 msgr("Failed to g_hash_table_find");
153 ret = mdg_group_eject_device(handle, group, device, _device_eject_result_cb, NULL);
154 if (MDG_ERROR_NONE != ret) {
155 msgr("Failed to Delete Group: [%s(0x%X)]", mdg_error_to_string(ret), ret);
158 msg(" - mdg_group_eject_device() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
163 bool __group_foreach_cb(mdg_device_h device, void *user_data)
167 mdg_device_info_get_device_id(device, &device_id);
168 msgb("\rDevice is [%s]", device_id);
170 found_invited_device_list = g_list_append(found_invited_device_list, device);
177 static int run_group_foreach_device(MManager *mm, struct menu_data *menu)
181 mdg_group_h group = NULL;
183 msg("Foreach Device");
185 if (strlen(group_idx)) {
186 idx = (unsigned short)strtol(group_idx, NULL, 10);
188 msgp("Invalid index. set to 1");
193 if (found_group_list) {
194 group = g_list_nth_data(found_group_list, idx - 1);
196 msgr("Failed to g_hash_table_find");
201 if (found_invited_device_list) {
202 g_list_free_full(found_device_list, _destroy_mdg_device_info);
203 found_invited_device_list = NULL;
206 ret = mdg_group_foreach_device(handle, group, __group_foreach_cb, NULL);
207 if (MDG_ERROR_NONE != ret) {
208 msgr("Failed to foreach device: [%s(0x%X)]", mdg_error_to_string(ret), ret);
211 msg(" - mdg_group_foreach_device() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
216 void _device_invite_result_cb(int result, mdg_device_h device, void *user_data)
219 mdg_device_info_is_invited(device, &is_invited);
220 msgb("\rInvite Device Finished [%s]", mdg_error_to_string(result));
221 msgb("\rThe device invited is : %d", is_invited);
224 static int run_device_invite(MManager *mm, struct menu_data *menu)
228 mdg_group_h group = NULL;
229 mdg_device_h device = NULL;
231 msg("Invite Device");
233 if (strlen(group_idx)) {
234 idx = (unsigned short)strtol(group_idx, NULL, 10);
236 msgp("Invalid index. set to 1");
241 if (found_group_list) {
242 group = g_list_nth_data(found_group_list, idx - 1);
244 msgr("Failed to g_hash_table_find");
249 if (strlen(device_idx)) {
250 idx = (unsigned short)strtol(device_idx, NULL, 10);
252 msgp("Invalid index. set to 1");
257 if (NULL == found_device_list) {
258 msgr("Find device first");
262 device = g_list_nth_data(found_device_list, idx - 1);
263 if (NULL == device) {
264 msgr("Failed to g_hash_table_find");
268 ret = mdg_group_invite_device(handle, group, device, pin, _device_invite_result_cb, NULL);
269 if (MDG_ERROR_NONE != ret) {
270 msgr("Failed to device invite: [%s(0x%X)]", mdg_error_to_string(ret), ret);
273 msg(" - mdg_group_invite_device() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
279 static int run_devices_show(MManager *mm, struct menu_data *menu)
286 /* Get a first item */
288 iter = g_list_first(found_device_list);
289 while (NULL != iter) {
292 msgr("device is null");
298 mdg_device_info_get_model_name(device, &model_name);
299 mdg_device_info_get_device_id(device, &device_id);
301 msgp("[%d] Model : %s, Device ID : %s", i+1, model_name, device_id);
303 mdg_device_h cloned_device;
304 char *cloned_model_name;
305 char *cloned_device_id;
307 mdg_device_info_clone(&cloned_device, device);
308 mdg_device_info_get_model_name(cloned_device, &cloned_model_name);
309 mdg_device_info_get_device_id(cloned_device, &cloned_device_id);
311 msgp("*Cloned* [%d] Model : %s, Device ID : %s", i+1, cloned_model_name, cloned_device_id);
319 if (cloned_model_name)
320 free(cloned_model_name);
322 if (cloned_device_id)
323 free(cloned_device_id);
325 mdg_device_info_destroy(cloned_device);
328 iter = g_list_next(iter);
335 static int run_invited_devices_show(MManager *mm, struct menu_data *menu)
342 /* Get a first item */
344 iter = g_list_first(found_invited_device_list);
345 while (NULL != iter) {
348 msgr("device is null");
355 mdg_device_info_get_device_id(device, &device_id);
356 mdg_device_info_get_model_name(device, &model_name);
358 msgp("[%d] Device : %s, Model : %s", i+1, device_id, model_name);
367 iter = g_list_next(iter);
374 typedef struct _channel_t {
375 char *device_id; /**< User callback to be called */
376 char *channel_id; /**< User data pointer */
379 static int run_channels_show(MManager *mm, struct menu_data *menu)
386 /* Get a first item */
388 iter = g_list_first(found_channel_list);
389 while (NULL != iter) {
390 channel = iter->data;
392 msgr("channel is null");
396 msgp("[%d] device id : %s, channel id : %s,", i+1, channel->device_id, channel->channel_id);
399 iter = g_list_next(iter);
407 static int run_group_get_members(MManager *mm, struct menu_data *menu)
412 mdg_group_h group = NULL;
413 mdg_device_h **devices = NULL;
415 msg("Get Group Members");
417 if (strlen(group_idx)) {
418 idx = (unsigned short)strtol(group_idx, NULL, 10);
420 msgp("Invalid index. set to 1");
425 if (found_group_list) {
426 group = g_list_nth_data(found_group_list, idx - 1);
428 msgr("Failed to g_hash_table_find");
433 ret = mdg_group_get_member_devices(group, devices, &count);
434 if (MDG_ERROR_NONE != ret) {
435 msgr("Failed to Delete Group: [%s(0x%X)]", mdg_error_to_string(ret), ret);
438 msg(" - mdg_group_get_member_devices() ret: [0x%X] [%s]", ret,
439 mdg_error_to_string(ret));
441 for (int i = 0; i < count; i++) {
443 char *device_id = NULL;
445 mdg_device_type_e device_type;
447 mdg_device_info_get_device_id(devices[i], &device_id);
448 mdg_device_info_get_ip(devices[i], &ip);
449 mdg_device_info_get_device_type(devices[i], &device_type);
451 msgp("device_id : %s, name : %s, type : %s", device_id, ip,
452 __device_type_to_string(device_type));
464 static int run_group_delete(MManager *mm, struct menu_data *menu)
468 mdg_group_h group = NULL;
472 if (strlen(group_idx)) {
473 idx = (unsigned short)strtol(group_idx, NULL, 10);
475 msgp("Invalid index. set to 1");
480 if (found_group_list) {
481 group = g_list_nth_data(found_group_list, idx - 1);
483 msgr("Failed to g_hash_table_find");
488 ret = mdg_group_delete(handle, group);
489 if (MDG_ERROR_NONE != ret) {
490 msgr("Failed to Delete Group: [%s(0x%X)]", mdg_error_to_string(ret), ret);
493 msg(" - mdg_group_delete() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
498 void _device_finish_cb(int result, void *user_data)
500 msgb("\rFind Device Finished ret: [0x%X] [%s]", result, mdg_error_to_string(result));
503 bool _device_found_cb(mdg_device_h device, void *user_data)
505 char *device_id = NULL;
506 char *model_name = NULL;
507 mdg_device_type_e device_type;
509 gboolean is_exist = FALSE;
511 mdg_device_info_get_device_id(device, &device_id);
512 mdg_device_info_get_model_name(device, &model_name);
513 mdg_device_info_get_type(device, &device_type);
515 iter = found_device_list;
516 while (iter != NULL) {
517 mdg_device_h temp = (mdg_device_h)iter->data;
518 char *temp_device_id;
520 mdg_device_info_get_device_id(temp, &temp_device_id);
522 if (g_strcmp0(device_id, temp_device_id) == 0) {
524 free(temp_device_id);
525 mdg_device_info_destroy(device);
529 free(temp_device_id);
530 iter = g_list_next(iter);
533 if (is_exist == FALSE) {
534 mdg_device_h cloned_device;
535 mdg_device_info_clone(&cloned_device, device);
536 found_device_list = g_list_append(found_device_list, cloned_device);
537 msgp("\r[ID] %s [Type] %s [Name] %s", device_id,
538 __device_type_to_string(device_type), model_name);
549 static int run_devices_find(MManager *mm, struct menu_data *menu)
556 duration = (unsigned short)strtol(timeout, NULL, 10);
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);
563 msg(" - mdg_device_find() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
565 if (found_device_list) {
566 g_list_free_full(found_device_list, _destroy_mdg_device_info);
567 found_device_list = NULL;
573 void __invited_event_cb(mdg_group_h group, void *user_data)
578 mdg_group_info_get_device_id(group, &device_id);
579 mdg_group_info_get_name(group, &name);
581 msgb("device is invited by [%s], Group Name [%s]", device_id, name);
587 void __ejected_event_cb(mdg_group_h group, void *user_data)
592 mdg_group_info_get_device_id(group, &device_id);
593 mdg_group_info_get_name(group, &name);
595 msgb("device is ejected by [%s], Group Name [%s]", device_id, name);
601 int run_device_show_local_device(MManager *mm, struct menu_data *menu)
604 mdg_device_h device = NULL;
606 msg("Get Local Device");
608 ret = mdg_device_get_local_device(handle, &device);
609 if (MDG_ERROR_NONE != ret) {
610 msgr("Failed to Get My Device: [%s(0x%X)]", mdg_error_to_string(ret), ret);
613 msg(" - mdg_device_info_get_local_device() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
616 char *device_id = NULL;
623 mdg_device_type_e type;
625 mdg_device_info_get_device_id(device, &device_id);
626 mdg_device_info_get_model_name(device, &model_name);
627 mdg_device_info_get_device_name(device, &device_name);
628 mdg_device_info_get_platform_ver(device, &platform_ver);
629 mdg_device_info_get_vendor_id(device, &vendor_id);
630 mdg_device_info_get_profile(device, &profile);
631 mdg_device_info_is_invited(device, &is_invited);
632 mdg_device_info_get_type(device, &type);
634 msgb("\n[ID] %s", device_id);
635 msgb("[Model Name] %s", model_name);
636 msgb("[Device Name] %s", device_name);
637 msgb("[Platform Ver.] %s", platform_ver);
638 msgb("[Vendor ID] %s", vendor_id);
639 msgb("[Profile] %s", profile);
640 msgb("[Is Invited] %d", is_invited);
641 msgb("[Type] %s\n", __device_type_to_string(type));
656 mdg_device_info_destroy(device);
660 mdg_set_invited_event_cb(handle, __invited_event_cb, NULL);
661 mdg_set_ejected_event_cb(handle, __ejected_event_cb, NULL);
666 static int run_group_show(MManager *mm, struct menu_data *menu)
673 /* Get a first item */
675 iter = g_list_first(found_group_list);
676 while (NULL != iter) {
679 msgr("groups is null");
685 mdg_group_type_e type;
687 mdg_group_info_get_device_id(group, &device_id);
688 mdg_group_info_get_name(group, &name);
689 mdg_group_info_get_type(group, &type);
691 msgp("[%d] type: %s, name: %s", i+1, mdg_group_type_to_string(type), name);
692 msgp(" device_id: %s\n", device_id);
694 mdg_group_h cloned_group;
695 char *cloned_device_id;
697 mdg_group_type_e cloned_type;
699 mdg_group_info_clone(&cloned_group, group);
700 mdg_group_info_get_device_id(cloned_group, &cloned_device_id);
701 mdg_group_info_get_name(cloned_group, &cloned_name);
702 mdg_group_info_get_type(cloned_group, &cloned_type);
704 msgp("*Cloned* [%d] type: %s, name: %s", i+1, mdg_group_type_to_string(cloned_type), cloned_name);
705 msgp(" device_id: %s\n", cloned_device_id);
712 if (cloned_device_id)
713 free(cloned_device_id);
717 mdg_group_info_destroy(cloned_group);
720 iter = g_list_next(iter);
727 static void _destroy_mdg_group_info(gpointer data)
729 mdg_group_info_destroy((mdg_group_h)data);
732 bool _group_found_cb(mdg_group_type_e type, mdg_group_h group, void *user_data)
735 mdg_group_h cloned_group;
737 mdg_group_info_get_name(group, &group_name);
739 if (type == MDG_GROUP_TYPE_LOCAL)
740 msgp("\rfound group type : MINE, name : %s", group_name);
742 msgp("\rfound group type : REMOTE, name : %s", group_name);
744 mdg_group_info_clone(&cloned_group, group);
746 found_group_list = g_list_append(found_group_list, cloned_group);
748 if (group_name != NULL)
754 void _group_finish_cb(int result, void *user_data)
756 msgb("\rFind Group Finished ret: [0x%X] [%s]", result, mdg_error_to_string(result));
759 static int run_group_find(MManager *mm, struct menu_data *menu)
766 duration = (unsigned short)strtol(timeout, NULL, 10);
768 if (found_group_list) {
769 g_list_free_full(found_group_list, _destroy_mdg_group_info);
770 found_group_list = NULL;
773 ret = mdg_group_find(handle, duration, _group_found_cb, _group_finish_cb, NULL);
774 if (MDG_ERROR_NONE != ret) {
775 msgr("Failed to Find Group: [%s(0x%X)]", mdg_error_to_string(ret), ret);
778 msg(" - mdg_group_find() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
783 static int run_group_create(MManager *mm, struct menu_data *menu)
788 ret = mdg_group_create(handle, groupid);
789 if (MDG_ERROR_NONE != ret) {
790 msgr("Failed to Create Group: [%s(0x%X)]", mdg_error_to_string(ret), ret);
793 msg(" - mdg_group_create() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
794 msgb("Create Group Successfully");
799 void _send_data_finish2_cb(int result, mdg_device_h device, char *channel_id, int msg_id,
800 unsigned char *data, int len, void *user_data)
802 msgb("\rmsg id = %d, data = %s, len = %d", msg_id, data, len);
803 msgb("\rSend Data once more Finished = %d", result);
806 void _send_data_finish_cb(int result, mdg_device_h device, char *channel_id, int msg_id,
807 unsigned char *data, int len, void *user_data)
811 msgb("\rmsg id = %d, channel ID data = %s, len = %d", msg_id, data, len);
812 msgb("\rSend Data Finished = %d", result);
814 msgb("\rSend Data Once more");
815 mdg_device_send_data(handle, device, channel_id, (unsigned char *)"Welcome",
816 7, _send_data_finish2_cb, NULL, &msg_id2);
818 msgp("The message ID is %d", msg_id2);
821 static int __send_data(int devidx, int chaidx)
824 char *deviceid = NULL;
825 mdg_device_h device = NULL;
826 channel_t *channel = NULL;
828 if (!found_invited_device_list || !found_channel_list) {
829 msgr("Find local device first");
833 if (found_invited_device_list) {
834 device = g_list_nth_data(found_invited_device_list, devidx - 1);
835 if (NULL == device) {
836 msgr("Find local device first");
841 if (found_channel_list) {
842 channel = g_list_nth_data(found_channel_list, chaidx - 1);
843 if (NULL == channel) {
844 msgr("Find local device first");
849 if (NULL == device) {
850 msgr("device is NULL");
854 if (NULL == channel) {
855 msgr("channel is NULL");
859 mdg_device_info_get_device_id(device, &deviceid);
860 msgp("Sent to [ID] %s [CHANNEL ID] %s", deviceid, channel->channel_id);
867 ret = mdg_device_send_data(handle, device, channel->channel_id, (unsigned char *)message,
868 strlen(message), _send_data_finish_cb, NULL, &msg_id);
869 if (MDG_ERROR_NONE != ret)
870 msgr("Failed to Send Data: [ID] %s", deviceid);
872 msgp("The message ID is %d", msg_id);
877 static int run_send_data(MManager *mm, struct menu_data *menu)
881 int count = g_list_length(found_invited_device_list);
888 if (strlen(device_idx)) {
889 devidx = (unsigned short)strtol(device_idx, NULL, 10);
891 msgp("Invalid index. set to 1");
895 if (strlen(channel_idx)) {
896 chaidx = (unsigned short)strtol(channel_idx, NULL, 10);
898 msgp("Invalid index. set to 1");
902 return __send_data(devidx, chaidx);
905 void _invited_device_finish_cb(int result, void *user_data)
907 msgb("\rFind My Owned Devices Finished ret: [0x%X] [%s]", result,
908 mdg_error_to_string(result));
911 bool _invited_device_found_cb(mdg_device_h device, void *user_data)
913 char *device_id = NULL;
914 mdg_device_type_e device_type;
915 mdg_device_h cloned_device;
917 mdg_device_info_get_device_id(device, &device_id);
918 mdg_device_info_get_type(device, &device_type);
920 msgp("\r[ID] %s [Type] %s", device_id, __device_type_to_string(device_type));
925 mdg_device_info_clone(&cloned_device, device);
927 found_invited_device_list = g_list_append(found_invited_device_list, cloned_device);
932 static int run_devices_find_invited_device(MManager *mm, struct menu_data *menu)
936 msg("Find My Owned Devices");
939 duration = (unsigned short)strtol(timeout, NULL, 10);
941 ret = mdg_device_find(handle, duration, true, _invited_device_found_cb,
942 _invited_device_finish_cb, NULL);
943 if (MDG_ERROR_NONE != ret) {
944 msgr("Failed to Find My Owned Devices: [%s(0x%X)]",
945 mdg_error_to_string(ret), ret);
949 msg(" - mdg_device_find_invited_device() ret: [0x%X] [%s]",
950 ret, mdg_error_to_string(ret));
952 if (found_invited_device_list) {
953 g_list_free_full(found_invited_device_list, _destroy_mdg_device_info);
954 found_invited_device_list = NULL;
960 static int run_request_create_group(MManager *mm, struct menu_data *menu)
964 mdg_device_h device = NULL;
966 msg("Request create group");
968 dev_count = g_list_length(found_invited_device_list);
970 if (0 >= dev_count) {
976 if (strlen(device_idx)) {
977 dev_idx = (unsigned short)strtol(device_idx, NULL, 10);
979 msgp("Invalid index. set to 1");
984 if (found_invited_device_list) {
985 device = g_list_nth_data(found_invited_device_list, dev_idx - 1);
986 if (NULL == device) {
987 msgr("Failed to g_hash_table_find");
992 ret = mdg_request_create_group(handle, device, request_groupid,
993 receive_request_result, NULL);
994 if (MDG_ERROR_NONE != ret) {
995 msgr("Failed to Request Create Group: [%s(0x%X)]",
996 mdg_error_to_string(ret), ret);
1000 msg(" - mdg_request_create_group() ret: [0x%X] [%s]",
1001 ret, mdg_error_to_string(ret));
1006 static int run_request_invite(MManager *mm, struct menu_data *menu)
1011 mdg_device_h device = NULL;
1012 mdg_group_h group = NULL;
1014 dev_count = g_list_length(found_invited_device_list);
1015 grp_count = g_list_length(found_group_list);
1017 if (0 >= dev_count || 0 >= grp_count) {
1023 if (strlen(group_idx)) {
1024 grp_idx = (unsigned short)strtol(group_idx, NULL, 10);
1026 msgp("Invalid index. set to 1");
1031 if (found_group_list) {
1032 group = g_list_nth_data(found_group_list, grp_idx - 1);
1033 if (NULL == group) {
1034 msgr("Failed to g_hash_table_find");
1040 if (strlen(device_idx)) {
1041 dev_idx = (unsigned short)strtol(device_idx, NULL, 10);
1043 msgp("Invalid index. set to 1");
1048 if (found_invited_device_list) {
1049 device = g_list_nth_data(found_invited_device_list, dev_idx - 1);
1050 if (NULL == device) {
1051 msgr("Failed to g_hash_table_find");
1056 ret = mdg_request_invite_device(handle, group, device, pin,
1057 receive_request_result, NULL);
1058 if (MDG_ERROR_NONE != ret) {
1059 msgr("Failed to Request Invite: [%s(0x%X)]", mdg_error_to_string(ret), ret);
1063 msg(" - mdg_request_invite_device() ret: [0x%X] [%s]",
1064 ret, mdg_error_to_string(ret));
1069 static int run_request_eject(MManager *mm, struct menu_data *menu)
1074 mdg_device_h device = NULL;
1075 mdg_group_h group = NULL;
1077 dev_count = g_list_length(found_invited_device_list);
1078 grp_count = g_list_length(found_group_list);
1080 if (0 >= dev_count || 0 >= grp_count) {
1086 if (group_idx != NULL && strlen(group_idx)) {
1087 grp_idx = (unsigned short)strtol(group_idx, NULL, 10);
1089 msgp("Invalid index. set to 1");
1094 if (found_group_list) {
1095 group = g_list_nth_data(found_group_list, grp_idx - 1);
1096 if (NULL == group) {
1097 msgr("Failed to g_hash_table_find");
1103 if (device_idx != NULL && strlen(device_idx)) {
1104 dev_idx = (unsigned short)strtol(device_idx, NULL, 10);
1106 msgp("Invalid index. set to 1");
1111 if (found_invited_device_list) {
1112 device = g_list_nth_data(found_invited_device_list, dev_idx - 1);
1113 if (NULL == device) {
1114 msgr("Failed to g_hash_table_find");
1119 ret = mdg_request_eject_device(handle, group, device, receive_request_result,
1121 if (MDG_ERROR_NONE != ret) {
1122 msgr("Failed to Request Eject: [%s(0x%X)]", mdg_error_to_string(ret), ret);
1126 msg(" - mdg_request_eject_device() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
1131 void __req_channel_list_finish_cb(char *device_id, char *channel_id, void *user_data)
1133 msgp("\rDevice ID : [%s], Channel ID : [%s]", device_id, channel_id);
1135 channel_t *channel = g_new0(channel_t, 1);
1136 channel->device_id = g_strdup(device_id);
1137 channel->channel_id = g_strdup(channel_id);
1139 found_channel_list = g_list_append(found_channel_list, channel);
1144 static int run_request_channel_list(MManager *mm, struct menu_data *menu)
1148 mdg_device_h device = NULL;
1150 dev_count = g_list_length(found_invited_device_list);
1152 if (0 >= dev_count) {
1158 if (strlen(device_idx)) {
1159 dev_idx = (unsigned short)strtol(device_idx, NULL, 10);
1161 msgp("Invalid index. set to 1");
1166 if (found_invited_device_list) {
1167 device = g_list_nth_data(found_invited_device_list, dev_idx - 1);
1168 if (NULL == device) {
1169 msgr("Failed to g_hash_table_find");
1174 ret = mdg_request_channel_list(handle, device, __req_channel_list_finish_cb, NULL);
1175 if (MDG_ERROR_NONE != ret) {
1176 msgr("Failed to Request Eject: [%s(0x%X)]", mdg_error_to_string(ret), ret);
1180 msg(" - mdg_request_eject_device() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
1185 void __channel_cb(int result, char *device_id, char *channel_id, int msg_id,
1186 unsigned char *arg, int len, void *user_data)
1188 msgb("[%s] channel callback is called by [%s]'s send message", channel_id, device_id);
1189 msgb("The msg_id is %d, arg : %s, len : %d", msg_id, arg, len);
1190 msgb("result is %d", result);
1192 mdg_device_send_response(handle, device_id, channel_id, msg_id, (unsigned char *)"Ya, Hi", 6);
1196 void __channel_opened_cb(int result, mdg_channel_h channel, void *user_data)
1198 msgb("result is %d", result);
1199 if (result == MDG_ERROR_NONE)
1200 msgb("channel opened callback is called. server socket is created");
1202 msgb("channel opened callback is called. but server socket is not created");
1204 unsigned char buf[256] = "SENDSEND";
1205 mdg_channel_write(channel, buf, 256);
1206 //write and recv using mdg_channel_write / mdg_channel_recv
1209 static int run_regist_channel(MManager *mm, struct menu_data *menu)
1213 ret = mdg_device_regist_channel(handle, channel_id, __channel_cb, NULL);
1214 msg(" - mdg_device_regist_channel() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
1216 ret = mdg_set_channel_opened_callback(handle, channel_id, __channel_opened_cb, NULL);
1217 msg(" - mdg_set_channel_opened_callback() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
1222 static int run_unregist_channel(MManager *mm, struct menu_data *menu)
1226 ret = mdg_device_unregist_channel(handle, channel_id);
1228 msg(" - mdg_unregist_channel() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
1233 static int run_set_preconfigured_pin(MManager *mm, struct menu_data *menu)
1237 ret = mdg_set_preconfigured_pin(handle, pin);
1239 msg(" - mdg_set_preconfigured_pin() ret: [0x%X] [%s]", ret, mdg_error_to_string(ret));
1244 void _mdg_channel_client_opened_cb(int result, mdg_channel_h channel, void *user_data)
1246 msgp("Client Opened result : %d", result);
1249 buf = (unsigned char *)malloc(1024);
1251 msgp("memory allocation error");
1255 mdg_channel_read(channel, &buf, &len);
1257 msgp("Get Data : %s, %d", buf, len);
1262 static int __request_open_channel(int devidx, int chaidx)
1265 char *deviceid = NULL;
1266 mdg_device_h device = NULL;
1267 channel_t *channel = NULL;
1269 if (!found_invited_device_list || !found_channel_list) {
1270 msgr("Find local device first");
1274 if (found_invited_device_list) {
1275 device = g_list_nth_data(found_invited_device_list, devidx - 1);
1276 if (NULL == device) {
1277 msgr("Find local device first");
1282 if (found_channel_list) {
1283 channel = g_list_nth_data(found_channel_list, chaidx - 1);
1284 if (NULL == channel) {
1285 msgr("Find local device first");
1290 if (NULL == device) {
1291 msgr("device is NULL");
1295 if (NULL == channel) {
1296 msgr("channel is NULL");
1300 mdg_device_info_get_device_id(device, &deviceid);
1301 msgp("Open channel between [ID] %s [CHANNEL ID] %s", deviceid, channel->channel_id);
1307 ret = mdg_request_channel_open(handle, device, channel->channel_id,
1308 _mdg_channel_client_opened_cb, NULL);
1309 if (MDG_ERROR_NONE != ret)
1310 msgr("Failed to Send Data: [ID] %s", deviceid);
1315 static int run_request_open_channel(MManager *mm, struct menu_data *menu)
1319 int count = g_list_length(found_invited_device_list);
1326 if (strlen(device_idx)) {
1327 devidx = (unsigned short)strtol(device_idx, NULL, 10);
1329 msgp("Invalid index. set to 1");
1333 if (strlen(channel_idx)) {
1334 chaidx = (unsigned short)strtol(channel_idx, NULL, 10);
1336 msgp("Invalid index. set to 1");
1340 return __request_open_channel(devidx, chaidx);
1343 static struct menu_data menu_group_create[] = {
1344 { "0", "Group Name", NULL, NULL, groupid },
1345 { "1", "Run", NULL, run_group_create, NULL },
1349 static struct menu_data menu_group_find[] = {
1350 { "0", "Timeout", NULL, NULL, timeout },
1351 { "1", "Run", NULL, run_group_find, NULL },
1355 static struct menu_data menu_group_delete[] = {
1356 { "0", "Show Found Group(s)", NULL, run_group_show, NULL },
1357 { "1", "Index", NULL, NULL, group_idx },
1358 { "2", "Run", NULL, run_group_delete, NULL },
1362 static struct menu_data menu_devices_find[] = {
1363 { "0", "Timeout", NULL, NULL, timeout },
1364 { "1", "Run", NULL, run_devices_find, NULL },
1368 static struct menu_data menu_group_invite_device[] = {
1369 { "0", "Show Found Group(s)", NULL, run_group_show, NULL },
1370 { "1", "Show Found Device(s)", NULL, run_devices_show, NULL },
1371 { "2", "Group Index", NULL, NULL, group_idx },
1372 { "3", "Device Index", NULL, NULL, device_idx },
1373 { "4", "PIN", NULL, NULL, pin },
1374 { "5", "Run", NULL, run_device_invite, NULL },
1378 static struct menu_data menu_group_eject_device[] = {
1379 { "0", "Show Found Group(s)", NULL, run_group_show, NULL },
1380 { "1", "Show Found Invited Device(s)", NULL, run_invited_devices_show, NULL },
1381 { "2", "Group Index", NULL, NULL, group_idx },
1382 { "3", "Device Index", NULL, NULL, device_idx },
1383 { "4", "Run", NULL, run_device_eject, NULL },
1387 static struct menu_data menu_group_foreach_device[] = {
1388 { "0", "Show Found Group(s)", NULL, run_group_show, NULL },
1389 { "1", "Group Index", NULL, NULL, group_idx },
1390 { "2", "Run", NULL, run_group_foreach_device, NULL },
1394 static struct menu_data menu_send_data[] = {
1395 { "0", "Show Found Invited Device(s)", NULL, run_invited_devices_show, NULL },
1396 { "1", "Show Channel List", NULL, run_channels_show, NULL },
1397 { "2", "Message", NULL, NULL, message },
1398 { "3", "Device Index", NULL, NULL, device_idx },
1399 { "4", "Channel Index", NULL, NULL, channel_idx },
1400 { "5", "Send", NULL, run_send_data, NULL },
1404 static struct menu_data menu_devices_find_invited_device[] = {
1405 { "0", "Timeout", NULL, NULL, timeout },
1406 { "1", "Run", NULL, run_devices_find_invited_device, NULL },
1410 static struct menu_data menu_request_create_group[] = {
1411 { "0", "Show Found Invited Device(s)", NULL, run_invited_devices_show, NULL },
1412 { "1", "Device Index", NULL, NULL, device_idx },
1413 { "2", "Request Group Name", NULL, NULL, request_groupid },
1414 { "3", "Run", NULL, run_request_create_group, NULL },
1418 static struct menu_data menu_request_invite[] = {
1419 { "0", "Show Found Group(s)", NULL, run_group_show, NULL },
1420 { "1", "Show Found Invited Device(s)", NULL, run_invited_devices_show, NULL },
1421 { "2", "Group Index", NULL, NULL, group_idx },
1422 { "3", "Device Index", NULL, NULL, device_idx },
1423 { "4", "PIN", NULL, NULL, pin },
1424 { "5", "Run", NULL, run_request_invite, NULL },
1428 static struct menu_data menu_request_eject[] = {
1429 { "0", "Show Found Group(s)", NULL, run_group_show, NULL },
1430 { "1", "Show Found Invited Device(s)", NULL, run_invited_devices_show, NULL },
1431 { "2", "Group Index", NULL, NULL, group_idx },
1432 { "3", "Device Index", NULL, NULL, device_idx },
1433 { "4", "Run", NULL, run_request_eject, NULL },
1437 static struct menu_data menu_request_channel_list[] = {
1438 { "0", "Show Found Invited Device(s)", NULL, run_invited_devices_show, NULL },
1439 { "1", "Device Index", NULL, NULL, device_idx },
1440 { "2", "Run", NULL, run_request_channel_list, NULL },
1444 static struct menu_data menu_regist_channel[] = {
1445 { "0", "Channel ID", NULL, NULL, channel_id },
1446 { "1", "Run", NULL, run_regist_channel, NULL },
1450 static struct menu_data menu_unregist_channel[] = {
1451 { "0", "Channel ID", NULL, NULL, channel_id },
1452 { "1", "Run", NULL, run_unregist_channel, NULL },
1456 static struct menu_data menu_set_preconfigured_pin[] = {
1457 { "0", "PIN", NULL, NULL, pin },
1458 { "1", "Run", NULL, run_set_preconfigured_pin, NULL },
1462 static struct menu_data menu_request_open_channel[] = {
1463 { "0", "Show Found Invited Device(s)", NULL, run_invited_devices_show, NULL },
1464 { "1", "Show Channel List", NULL, run_channels_show, NULL },
1465 { "2", "Device Index", NULL, NULL, device_idx },
1466 { "3", "Channel Index", NULL, NULL, channel_idx },
1467 { "4", "Run", NULL, run_request_open_channel, NULL },
1471 struct menu_data menu_mdg_manager[] = {
1472 { "1", "Show Local Device", NULL, run_device_show_local_device, NULL }, // O
1473 { "2", "Create Group", menu_group_create, NULL, NULL }, // 0
1474 { "3", "Find Group(s)", menu_group_find, NULL, NULL }, // 0
1475 { "4", "Show Group(s)", NULL, run_group_show, NULL }, // 0
1476 { "5", "Delete Group", menu_group_delete, NULL, NULL }, // 0
1477 { "6", "Find Device(s)", menu_devices_find, NULL, NULL }, // 0
1478 { "7", "Show Device(s)", NULL, run_devices_show, NULL }, // 0
1479 { "8", "Invite Device", menu_group_invite_device, NULL, NULL }, // 0
1480 { "9", "Eject Device", menu_group_eject_device, NULL, NULL }, // 0
1481 { "10", "Foreach Device", menu_group_foreach_device, NULL, NULL },
1482 { "11", "Find Invited Device(s)", menu_devices_find_invited_device, NULL, NULL }, // 0
1483 { "12", "Show Invited Device(s)", NULL, run_invited_devices_show, NULL }, // 0
1484 { "13", "Request Create Group", menu_request_create_group, NULL, NULL }, // 0
1485 { "14", "Request Invite", menu_request_invite, NULL, NULL }, // 0
1486 { "15", "Request Eject", menu_request_eject, NULL, NULL }, // 0
1487 { "16", "Request Channel List", menu_request_channel_list, NULL, NULL }, // 0
1488 { "17", "Regist Channel", menu_regist_channel, NULL, NULL },
1489 { "18", "Unregist Channel", menu_unregist_channel, NULL, NULL },
1490 { "19", "Send Message", menu_send_data, NULL, NULL }, // 0
1491 { "20", "Set Preconfigured PIN", menu_set_preconfigured_pin, NULL, NULL }, // 0
1492 { "21", "Request Open Channel", menu_request_open_channel, NULL, NULL }, // 0