Initialize the project.
[platform/framework/web/data-provider-master.git] / util_liveinfo / src / liveinfo.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 #include <stdio.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <libgen.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24
25 #include <glib.h>
26 #include <glib-object.h>
27
28 #include <packet.h>
29 #include <com-core_packet.h>
30 #include <com-core.h>
31
32 #include <Ecore.h>
33
34 static struct info {
35         int fifo_handle;
36         int fd;
37         Ecore_Fd_Handler *fd_handler;
38         Ecore_Fd_Handler *in_handler;
39 } s_info = {
40         .fifo_handle = -EINVAL,
41         .fd = -EINVAL,
42         .fd_handler = NULL,
43         .in_handler = NULL,
44 };
45
46 static void send_slave_list(void)
47 {
48         struct packet *packet;
49
50         printf("Send request SLAVE LIST\n");
51         packet = packet_create_noack("slave_list", "d", 0.0f);
52         if (!packet) {
53                 fprintf(stderr, "Failed to create a packet\n");
54                 return;
55         }
56
57         com_core_packet_send_only(s_info.fd, packet);
58         packet_destroy(packet);
59 }
60
61 static void send_pkg_list(void)
62 {
63         struct packet *packet;
64
65         printf("Send request PACKAGE LIST\n");
66         packet = packet_create_noack("pkg_list", "d", 0.0f);
67         if (!packet) {
68                 fprintf(stderr, "Failed to create a packet\n");
69                 return;
70         }
71
72         com_core_packet_send_only(s_info.fd, packet);
73         packet_destroy(packet);
74 }
75
76 static void send_slave_load(pid_t pid)
77 {
78         struct packet *packet;
79
80         printf("Send request Loaded package list\n");
81         packet = packet_create_noack("slave_load", "i", pid);
82         if (!packet) {
83                 fprintf(stderr, "Failed to create a packet\n");
84                 return;
85         }
86
87         com_core_packet_send_only(s_info.fd, packet);
88         packet_destroy(packet);
89 }
90
91 static void send_inst_list(const char *pkgname)
92 {
93         struct packet *packet;
94
95         printf("Send request instance list\n");
96         packet = packet_create_noack("inst_list", "s", pkgname);
97         if (!packet) {
98                 fprintf(stderr, "Failed to create a packet\n");
99                 return;
100         }
101
102         com_core_packet_send_only(s_info.fd, packet);
103         packet_destroy(packet);
104 }
105
106 static inline void help(void)
107 {
108         printf("liveinfo - Livebox utility\n");
109         printf("------------------------------ [Command list] ------------------------------\n");
110         printf("\e[33mpkg_list\e[0m - Display the installed package list\n");
111         printf("\e[33mslave_list\e[0m - Display the slave list\n");
112         printf("\e[33minst_list\e[0m \e[37mLIVEBOX_PKGNAME\e[0m - Display the instance list of this LIVEBOX_PKGNAME\n");
113         printf("\e[33mslave_load\e[0m \e[37mSLAVE_PID\e[0m - Display the loaded livebox instance list on the given slave\n");
114         printf("\e[32mexit - \e[0m\n");
115         printf("\e[32mquit - \e[0m\n");
116         printf("----------------------------------------------------------------------------\n");
117 }
118
119 static inline void do_command(const char *cmd)
120 {
121         char command[256];
122         char argument[256];
123
124         if (sscanf(cmd, "%255[^ ] %255s", command, argument) == 2) {
125                 if (!strcasecmp(command, "inst_list")) {
126                         send_inst_list(argument);
127                 } else if (!strcasecmp(command, "slave_load")) {
128                         pid_t pid;
129                         if (sscanf(argument, "%d", &pid) == 1)
130                                 send_slave_load(pid);
131                 } else {
132                         help();
133                 }
134         } else {
135                 if (!strcasecmp(cmd, "pkg_list"))
136                         send_pkg_list();
137                 else if (!strcasecmp(cmd, "slave_list"))
138                         send_slave_list();
139                 else if (!strcasecmp(cmd, "exit"))
140                         ecore_main_loop_quit();
141                 else if (!strcasecmp(cmd, "quit"))
142                         ecore_main_loop_quit();
143                 else if (!strcasecmp(cmd, "help"))
144                         help();
145         }
146 }
147
148 static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler)
149 {
150         static int idx = 0;
151         static char cmd_buffer[256];
152         char ch;
153         int ret;
154         int fd;
155
156         fd = ecore_main_fd_handler_fd_get(fd_handler);
157
158         /*!
159          * \note
160          * Using this routine, we can implement the command recommend algorithm.
161          * When a few more characters are matched with history of command, we can show it to user
162          * Then the user will choose one or write new command
163          */
164
165         /* Silly.. Silly */
166         ret = read(fd, &ch, 1);
167         if (ret != 1 || ret < 0) {
168                 fprintf(stderr, "Failed to get a byte: %s\n", strerror(errno));
169                 return ECORE_CALLBACK_RENEW;
170         }
171
172         switch (ch) {
173         case 0x08: /* BKSP */
174                 printf("\033[3D");
175                 if (idx > 0)
176                         idx--;
177                 break;
178         case '\n':
179         case '\r':
180                 cmd_buffer[idx] = '\0';
181                 idx = 0;
182                 printf("\n");
183                 do_command(cmd_buffer);
184                 break;
185         default:
186                 cmd_buffer[idx++] = ch;
187                 if (idx == sizeof(cmd_buffer) - 1) {
188                         cmd_buffer[idx] = '\0';
189                         printf("\nCommand buffer is overflow: %s\n", cmd_buffer);
190                         idx = 0;
191                 }
192                 break;
193         }
194
195         return ECORE_CALLBACK_RENEW;
196 }
197
198 static Eina_Bool read_cb(void *data, Ecore_Fd_Handler *fd_handler)
199 {
200         int fd;
201         char buffer[1024];
202         int len;
203
204         fd = ecore_main_fd_handler_fd_get(fd_handler);
205         if (fd < 0) {
206                 fprintf(stderr, "FD is not valid: %d\n", fd);
207                 return ECORE_CALLBACK_RENEW;
208         }
209
210         while ((len = read(fd, buffer, sizeof(buffer) - 1)) > 0) {
211                 buffer[len] = '\0';
212                 fputs(buffer, stdout);
213         }
214
215         fflush(stdout);
216
217         return ECORE_CALLBACK_RENEW;
218 }
219
220 static int ret_cb(pid_t pid, int handle, const struct packet *packet, void *data)
221 {
222         const char *fifo_name;
223         int ret;
224
225         if (packet_get(packet, "si", &fifo_name, &ret) != 2) {
226                 fprintf(stderr, "Invalid packet\n");
227                 return -EFAULT;
228         }
229
230         if (ret != 0) {
231                 fprintf(stderr, "Returns %d\n", ret);
232                 return ret;
233         }
234
235         printf("FIFO: %s\n", fifo_name);
236
237         s_info.fifo_handle = open(fifo_name, O_RDONLY | O_NONBLOCK);
238         if (s_info.fifo_handle < 0) {
239                 fprintf(stderr, "Error: %s\n", strerror(errno));
240                 s_info.fifo_handle = -EINVAL;
241                 ecore_main_loop_quit();
242                 return -EINVAL;
243         }
244
245         s_info.fd_handler = ecore_main_fd_handler_add(s_info.fifo_handle, ECORE_FD_READ, read_cb, NULL, NULL, NULL);
246         if (!s_info.fd_handler) {
247                 fprintf(stderr, "Failed to add a fd handler\n");
248                 close(s_info.fifo_handle);
249                 s_info.fifo_handle = -EINVAL;
250                 ecore_main_loop_quit();
251                 return -EFAULT;
252         }
253
254         s_info.in_handler = ecore_main_fd_handler_add(STDIN_FILENO, ECORE_FD_READ, input_cb, NULL, NULL, NULL);
255         if (!s_info.in_handler) {
256                 fprintf(stderr, "Failed to add a input handler\n");
257                 ecore_main_loop_quit();
258                 return -EFAULT;
259         }
260
261         return 0;
262 }
263
264 static int disconnected_cb(int handle, void *data)
265 {
266         printf("Disconnected\n");
267         ecore_main_loop_quit();
268         return 0;
269 }
270
271 static int connected_cb(int handle, void *data)
272 {
273         struct packet *packet;
274
275         printf("Connected\n");
276
277         packet = packet_create("liveinfo_hello", "d", 0.0f);
278         if (!packet) {
279                 fprintf(stderr, "Failed to build a packet for hello\n");
280                 com_core_packet_client_fini(s_info.fd);
281                 s_info.fd = -EINVAL;
282                 return -EFAULT;
283         }
284
285         s_info.fd = handle;
286
287         if (com_core_packet_async_send(s_info.fd, packet, 0.0f, ret_cb, NULL) < 0) {
288                 fprintf(stderr, "Failed to send a packet hello\n");
289                 packet_destroy(packet);
290                 com_core_packet_client_fini(s_info.fd);
291                 s_info.fd = -EINVAL;
292                 return -EFAULT;
293         }
294
295         packet_destroy(packet);
296         return 0;
297 }
298
299 int main(int argc, char *argv[])
300 {
301         static struct method s_table[] = {
302                 {
303                         .cmd = NULL,
304                         .handler = NULL,
305                 },
306         };
307
308         ecore_init();
309         g_type_init();
310
311         com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
312         com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
313
314         s_info.fd = com_core_packet_client_init(SOCKET_FILE, 0, s_table);
315         if (s_info.fd < 0) {
316                 fprintf(stderr, "Failed to make a connection\n");
317                 return -EIO;
318         }
319
320         printf("Type your command on below empty line\n");
321
322         ecore_main_loop_begin();
323
324         if (s_info.fd > 0) {
325                 com_core_packet_client_fini(s_info.fd);
326                 s_info.fd = -EINVAL;
327         }
328
329         if (s_info.fd_handler) {
330                 ecore_main_fd_handler_del(s_info.fd_handler);
331                 s_info.fd_handler = NULL;
332         }
333
334         if (s_info.fifo_handle > 0) {
335                 close(s_info.fifo_handle);
336                 s_info.fifo_handle = -EINVAL;
337         }
338
339         if (s_info.in_handler) {
340                 ecore_main_fd_handler_del(s_info.in_handler);
341                 s_info.in_handler = NULL;
342         }
343
344         ecore_shutdown();
345         return 0;
346 }
347
348 /* End of a file */