Fix Side effect from "Fix Coverity issues"
[platform/core/api/multi-device-group.git] / test / main.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 <sys/socket.h>
23 #include <unistd.h>
24 #include <errno.h>
25
26 #include <glib.h>
27 #include <gio/gio.h>
28
29 #include "menu.h"
30 #include "common.h"
31
32 extern struct menu_data menu_mdg_manager[];
33
34 extern GList *found_group_list;
35 extern GList *found_device_list;
36 extern GList *found_invited_device_list;
37 extern mdg_h handle;
38
39 #define CASE_TO_STR(x) case x: return #x;
40
41 void receive_request_result(char *cmd, char *device_id, unsigned char *arg,
42                                                         int len, int ret, void *user_data);
43
44 const char* mdg_error_to_string(mdg_error_e err)
45 {
46         switch (err) {
47         /* CHECK: List all enum values here */
48         CASE_TO_STR(MDG_ERROR_NONE)
49         CASE_TO_STR(MDG_ERROR_IO_ERROR)
50         CASE_TO_STR(MDG_ERROR_INVALID_PARAMETER)
51         CASE_TO_STR(MDG_ERROR_OUT_OF_MEMORY)
52         CASE_TO_STR(MDG_ERROR_PERMISSION_DENIED)
53         CASE_TO_STR(MDG_ERROR_NOT_SUPPORTED)
54         CASE_TO_STR(MDG_ERROR_OPERATION_FAILED)
55         CASE_TO_STR(MDG_ERROR_NO_DATA)
56         CASE_TO_STR(MDG_ERROR_ALREADY_REGISTERED)
57         CASE_TO_STR(MDG_ERROR_IN_PROGRESS)
58         CASE_TO_STR(MDG_ERROR_COMM_ERROR)
59         CASE_TO_STR(MDG_ERROR_NOT_STARTED)
60         CASE_TO_STR(MDG_ERROR_DB)
61         CASE_TO_STR(MDG_ERROR_NOT_PROPER_GROUP)
62         CASE_TO_STR(MDG_ERROR_NOT_PROPER_DEVICE)
63
64         default :
65                 return "MDG_ERROR_UNKNOWN";
66         }
67 }
68
69 const char *mdg_group_type_to_string(mdg_group_type_e e)
70 {
71         switch (e) {
72         CASE_TO_STR(MDG_GROUP_TYPE_LOCAL)
73         CASE_TO_STR(MDG_GROUP_TYPE_REMOTE)
74         default :
75                 return "Unknown station type";
76         }
77 }
78
79
80 static int __init_func(MManager *mm, struct menu_data *menu)
81 {
82         int ret = -1;
83
84         ret = mdg_initialize(&handle);
85         if (ret != 0) {
86                 msg("Failed to initialize mdgd: [%s(0x%X)]",
87                                 mdg_error_to_string(ret), ret);
88                 return RET_FAILURE;
89         }
90 /*
91         ret = mdg_request_result_callback(handle, receive_request_result,
92                                         NULL);
93         if (ret != 0) {
94                 msg("Failed to request result callback: [%s(0x%X)]",
95                                 mdg_error_to_string(ret), ret);
96                 return RET_FAILURE;
97         }
98 */
99         return RET_SUCCESS;
100 }
101
102 static struct menu_data menu_main[] = {
103         { "1", "Mdg-Manager", menu_mdg_manager, NULL, NULL },
104         { NULL, NULL, },
105 };
106
107 static gboolean __create_init_menu(struct menu_data init_menu[1])
108 {
109         init_menu[0].key = "1";
110         init_menu[0].title = "Init";
111         init_menu[0].sub_menu = menu_main;
112         init_menu[0].callback = __init_func;
113         init_menu[0].data = NULL;
114
115         return TRUE;
116 }
117
118 void _free_group(gpointer data)
119 {
120         mdg_group_info_destroy(data);
121 }
122
123 void _free_device(gpointer data)
124 {
125         mdg_device_info_destroy(data);
126 }
127
128 int main(int arg, char **argv)
129 {
130         GMainLoop *mainloop = NULL;
131         GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO);
132         MManager *manager;
133         struct menu_data init_menu[1+1] = { {NULL, NULL, } };
134
135 #if !GLIB_CHECK_VERSION(2, 35, 0)
136         g_type_init();
137 #endif
138         mainloop = g_main_loop_new(NULL, FALSE);
139
140         msg("");
141         msg("* Mdg-Manager Test application ");
142         msg("* Build On: %s  %s", __DATE__, __TIME__);
143
144         if (__create_init_menu(init_menu) == FALSE)
145                 goto OUT;
146
147         manager = menu_manager_new(init_menu, mainloop);
148         if (!manager)
149                 goto OUT;
150
151         menu_manager_run(manager);
152
153         g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
154                                 on_menu_manager_keyboard, manager);
155         g_main_loop_run(mainloop);
156
157 OUT:
158         if (found_group_list)
159                 g_list_free_full(found_group_list, _free_group);
160         if (found_device_list)
161                 g_list_free_full(found_device_list, _free_device);
162         if (found_invited_device_list)
163                 g_list_free_full(found_invited_device_list, _free_device);
164
165         mdg_deinitialize(handle);
166
167         g_main_loop_unref(mainloop);
168         msg("******* Bye bye *******");
169
170         return 0;
171 }