2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://www.tizenopensource.org/license
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.
21 #include <sys/types.h>
26 #include <glib-object.h>
29 #include <com-core_packet.h>
37 Ecore_Fd_Handler *fd_handler;
38 Ecore_Fd_Handler *in_handler;
40 .fifo_handle = -EINVAL,
46 static void send_slave_list(void)
48 struct packet *packet;
50 printf("Send request SLAVE LIST\n");
51 packet = packet_create_noack("slave_list", "d", 0.0f);
53 fprintf(stderr, "Failed to create a packet\n");
57 com_core_packet_send_only(s_info.fd, packet);
58 packet_destroy(packet);
61 static void send_pkg_list(void)
63 struct packet *packet;
65 printf("Send request PACKAGE LIST\n");
66 packet = packet_create_noack("pkg_list", "d", 0.0f);
68 fprintf(stderr, "Failed to create a packet\n");
72 com_core_packet_send_only(s_info.fd, packet);
73 packet_destroy(packet);
76 static void send_slave_load(pid_t pid)
78 struct packet *packet;
80 printf("Send request Loaded package list\n");
81 packet = packet_create_noack("slave_load", "i", pid);
83 fprintf(stderr, "Failed to create a packet\n");
87 com_core_packet_send_only(s_info.fd, packet);
88 packet_destroy(packet);
91 static void send_inst_list(const char *pkgname)
93 struct packet *packet;
95 printf("Send request instance list\n");
96 packet = packet_create_noack("inst_list", "s", pkgname);
98 fprintf(stderr, "Failed to create a packet\n");
102 com_core_packet_send_only(s_info.fd, packet);
103 packet_destroy(packet);
106 static inline void help(void)
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");
119 static inline void do_command(const char *cmd)
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")) {
129 if (sscanf(argument, "%d", &pid) == 1)
130 send_slave_load(pid);
135 if (!strcasecmp(cmd, "pkg_list"))
137 else if (!strcasecmp(cmd, "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"))
148 static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler)
151 static char cmd_buffer[256];
156 fd = ecore_main_fd_handler_fd_get(fd_handler);
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
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;
173 case 0x08: /* BKSP */
180 cmd_buffer[idx] = '\0';
183 do_command(cmd_buffer);
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);
195 return ECORE_CALLBACK_RENEW;
198 static Eina_Bool read_cb(void *data, Ecore_Fd_Handler *fd_handler)
204 fd = ecore_main_fd_handler_fd_get(fd_handler);
206 fprintf(stderr, "FD is not valid: %d\n", fd);
207 return ECORE_CALLBACK_RENEW;
210 while ((len = read(fd, buffer, sizeof(buffer) - 1)) > 0) {
212 fputs(buffer, stdout);
217 return ECORE_CALLBACK_RENEW;
220 static int ret_cb(pid_t pid, int handle, const struct packet *packet, void *data)
222 const char *fifo_name;
225 if (packet_get(packet, "si", &fifo_name, &ret) != 2) {
226 fprintf(stderr, "Invalid packet\n");
231 fprintf(stderr, "Returns %d\n", ret);
235 printf("FIFO: %s\n", fifo_name);
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();
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();
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();
264 static int disconnected_cb(int handle, void *data)
266 printf("Disconnected\n");
267 ecore_main_loop_quit();
271 static int connected_cb(int handle, void *data)
273 struct packet *packet;
275 printf("Connected\n");
277 packet = packet_create("liveinfo_hello", "d", 0.0f);
279 fprintf(stderr, "Failed to build a packet for hello\n");
280 com_core_packet_client_fini(s_info.fd);
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);
295 packet_destroy(packet);
299 int main(int argc, char *argv[])
301 static struct method s_table[] = {
311 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
312 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
314 s_info.fd = com_core_packet_client_init(SOCKET_FILE, 0, s_table);
316 fprintf(stderr, "Failed to make a connection\n");
320 printf("Type your command on below empty line\n");
322 ecore_main_loop_begin();
325 com_core_packet_client_fini(s_info.fd);
329 if (s_info.fd_handler) {
330 ecore_main_fd_handler_del(s_info.fd_handler);
331 s_info.fd_handler = NULL;
334 if (s_info.fifo_handle > 0) {
335 close(s_info.fifo_handle);
336 s_info.fifo_handle = -EINVAL;
339 if (s_info.in_handler) {
340 ecore_main_fd_handler_del(s_info.in_handler);
341 s_info.in_handler = NULL;