e181820097c784cd32719108bf36d77a6a7f2471
[apps/livebox/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 #include <termios.h>
25
26 #include <glib.h>
27 #include <glib-object.h>
28
29 #include <packet.h>
30 #include <com-core_packet.h>
31 #include <com-core.h>
32
33 #include <livebox-service.h>
34
35 #include <Ecore.h>
36
37 #include "liveinfo.h"
38 #include "node.h"
39
40 #define PROMPT "liveinfo "
41
42 struct package {
43         int primary;
44         char *pkgid;
45
46         int pid;
47         char *slavename;
48         char *abi;
49         int refcnt;
50         int fault_count;
51         int inst_count;
52 };
53
54 struct instance {
55         char *cluster;
56         char *category;
57         double period;
58         char *state;
59         int width;
60         int height;
61 };
62
63 struct slave {
64         int pid;
65         char *pkgname;
66         char *abi;
67         int secured;
68         int refcnt;
69         int fault_count;
70         char *state;
71         int loaded_inst;
72         int loaded_pkg;
73         double ttl;
74 };
75
76 enum command {
77         NOP,
78         PKG_LIST,
79         INST_LIST,
80         TOGGLE_DEBUG,
81         SLAVE_LIST,     
82 };
83
84 static struct info {
85         int fifo_handle;
86         int fd;
87         Ecore_Fd_Handler *fd_handler;
88         Ecore_Fd_Handler *in_handler;
89         int input_fd;
90
91         struct node *rootdir;
92         struct node *curdir;
93
94         enum command cmd;
95 } s_info = {
96         .fifo_handle = -EINVAL,
97         .fd = -EINVAL,
98         .fd_handler = NULL,
99         .in_handler = NULL,
100         .input_fd = -1,
101         .rootdir = NULL,
102         .curdir = NULL,
103         .cmd = NOP,
104 };
105
106 static inline void ls(void)
107 {
108         struct node *node;
109         int cnt = 0;
110         int is_package;
111         int is_provider;
112         int is_instance;
113         char *path;
114
115         is_package = node_name(s_info.curdir) && !strcmp(node_name(s_info.curdir), "package");
116         is_provider = !is_package && node_name(s_info.curdir) && !strcmp(node_name(s_info.curdir), "provider");
117         is_instance = !is_package && !is_provider && node_parent(s_info.curdir) && node_name(node_parent(s_info.curdir)) && !strcmp(node_name(node_parent(s_info.curdir)), "package");
118
119         node = node_child(s_info.curdir);
120         while (node) {
121                 if (is_package) {
122                         struct package *info;
123                         info = node_data(node);
124                         printf(" %3d %20s %5s ", info->inst_count, info->slavename ? info->slavename : "-", info->abi ? info->abi : "-");
125                 } else if (is_provider) {
126                         struct slave *info;
127                         info = node_data(node);
128                         printf(" %3d %5s %5.2f ", info->loaded_inst, info->abi ? info->abi : "-", info->ttl);
129                 } else if (is_instance) {
130                         struct instance *info;
131                         struct stat stat;
132                         char buf[4096];
133                         info = node_data(node);
134
135                         printf(" %5.2f %6s %10s %10s %4dx%-4d ", info->period, info->state, info->cluster, info->category, info->width, info->height);
136                         snprintf(buf, sizeof(buf), "/opt/usr/share/live_magazine/reader/%s", node_name(node));
137                         if (lstat(buf, &stat) < 0)
138                                 printf("%3d ERR ", errno);
139                         else
140                                 printf("%2.2lf MB ", (double)stat.st_size / 1024.0f / 1024.0f);
141                 }
142
143                 if (node_type(node) == NODE_DIR)
144                         printf("%s/", node_name(node));
145                 else if (node_type(node) == NODE_FILE)
146                         printf("%s", node_name(node));
147
148                 printf("\n");
149                 node = node_next_sibling(node);
150                 cnt++;
151         }
152         printf("Total: %d\n", cnt);
153         path = node_to_abspath(s_info.curdir);
154         printf(PROMPT"%s # ", path);
155         free(path);
156 }
157
158 static void send_slave_list(void)
159 {
160         struct packet *packet;
161
162         if (s_info.cmd != NOP) {
163                 printf("Previous command is not finished\n");
164                 return;
165         }
166
167         packet = packet_create_noack("slave_list", "d", 0.0f);
168         if (!packet) {
169                 printf("Failed to create a packet\n");
170                 return;
171         }
172
173         com_core_packet_send_only(s_info.fd, packet);
174         packet_destroy(packet);
175         s_info.cmd = SLAVE_LIST;
176 }
177
178 static void send_pkg_list(void)
179 {
180         struct packet *packet;
181
182         if (s_info.cmd != NOP) {
183                 printf("Previous command is not finished\n");
184                 return;
185         }
186
187         packet = packet_create_noack("pkg_list", "d", 0.0f);
188         if (!packet) {
189                 printf("Failed to create a packet\n");
190                 return;
191         }
192
193         com_core_packet_send_only(s_info.fd, packet);
194         packet_destroy(packet);
195         s_info.cmd = PKG_LIST;
196 }
197
198 static void send_toggle_debug(void)
199 {
200         struct packet *packet;
201
202         if (s_info.cmd != NOP) {
203                 printf("Previous command is not finished\n");
204                 return;
205         }
206
207         packet = packet_create_noack("toggle_debug", "d", 0.0f);
208         if (!packet) {
209                 printf("Failed to create a packet\n");
210                 return;
211         }
212
213         com_core_packet_send_only(s_info.fd, packet);
214         packet_destroy(packet);
215         s_info.cmd = TOGGLE_DEBUG;
216 }
217
218 static void send_inst_list(const char *pkgname)
219 {
220         struct packet *packet;
221
222         if (s_info.cmd != NOP) {
223                 printf("Previous command is not finished\n");
224                 return;
225         }
226
227         packet = packet_create_noack("inst_list", "s", pkgname);
228         if (!packet) {
229                 printf("Failed to create a packet\n");
230                 return;
231         }
232
233         com_core_packet_send_only(s_info.fd, packet);
234         packet_destroy(packet);
235         s_info.cmd = INST_LIST;
236 }
237
238 static inline void help(void)
239 {
240         printf("liveinfo - Livebox utility\n");
241         printf("------------------------------ [Command list] ------------------------------\n");
242         printf("\e[32cd - Change directory\e[0m\n");
243         printf("\e[32ls - List up content as a file\e[0m\n");
244         printf("\e[32cat - Open a file to get some detail information\e[0m\n");
245         printf("\e[32mexit - \e[0m\n");
246         printf("\e[32mquit - \e[0m\n");
247         printf("----------------------------------------------------------------------------\n");
248 }
249
250 static int pkglist_cb(const char *appid, const char *lbid, int is_prime, void *data)
251 {
252         struct node *parent = data;
253         struct node *node;
254         struct package *info;
255
256         info = calloc(1, sizeof(*info));
257         if (!info) {
258                 printf("Error: %s\n", strerror(errno));
259                 return -ENOMEM;
260         }
261
262         info->pkgid = strdup(appid);
263         if (!info->pkgid) {
264                 printf("Error: %s\n", strerror(errno));
265                 free(info);
266                 return -ENOMEM;
267         }
268
269         info->primary = is_prime;
270
271         node = node_create(parent, lbid, NODE_DIR);
272         if (!node) {
273                 free(info->pkgid);
274                 free(info);
275                 return -ENOMEM;
276         }
277
278         node_set_mode(node, NODE_READ | NODE_EXEC);
279         node_set_data(node, info);
280         return 0;
281 }
282
283 static inline void init_directory(void)
284 {
285         struct node *node;
286         s_info.rootdir = node_create(NULL, NULL, NODE_DIR);
287         if (!s_info.rootdir)
288                 return;
289
290         node = node_create(s_info.rootdir, "provider", NODE_DIR);
291         if (!node) {
292                 node_destroy(s_info.rootdir);
293                 s_info.rootdir = NULL;
294                 return;
295         }
296         node_set_mode(node, NODE_READ | NODE_EXEC);
297
298         node = node_create(s_info.rootdir, "package", NODE_DIR);
299         if (!node) {
300                 node_destroy(node_child(s_info.rootdir));
301                 node_destroy(s_info.rootdir);
302                 s_info.rootdir = NULL;
303                 return;
304         }
305         node_set_mode(node, NODE_READ | NODE_EXEC);
306
307         s_info.curdir = s_info.rootdir;
308         livebox_service_get_pkglist(pkglist_cb, node);
309         return;
310 }
311
312 static inline void fini_directory(void)
313 {
314 }
315
316 static inline void do_command(const char *cmd)
317 {
318         char command[256];
319         char argument[4096] = { '\0', };
320         char *path;
321
322         if (!strlen(cmd)) {
323                 char *path;
324                 path = node_to_abspath(s_info.curdir);
325                 printf(PROMPT"%s # ", path);
326                 free(path);
327                 return;
328         }
329
330         if (sscanf(cmd, "%255[^ ] %4095s", command, argument) == 2)
331                 cmd = command;
332
333         if (!strcasecmp(cmd, "exit") || !strcasecmp(cmd, "quit")) {
334                 ecore_main_loop_quit();
335         } else if (!strcasecmp(cmd, "toggle_debug")) {
336                 if (s_info.cmd != NOP) {
337                         printf("Waiting the server response\n");
338                         return;
339                 }
340
341                 send_toggle_debug();
342         } else if (!strcasecmp(cmd, "ls")) {
343                 if (node_name(s_info.curdir)) {
344                         if (!strcmp(node_name(s_info.curdir), "package")) {
345                                 if (s_info.cmd != NOP) {
346                                         printf("Waiting the server response\n");
347                                         return;
348                                 }
349                                 send_pkg_list();
350                                 return;
351                         } else if (!strcmp(node_name(s_info.curdir), "provider")) {
352                                 if (s_info.cmd != NOP) {
353                                         printf("Waiting the server response\n");
354                                         return;
355                                 }
356                                 send_slave_list();
357                                 return;
358                         }
359                 }
360
361                 if (node_parent(s_info.curdir) && node_name(node_parent(s_info.curdir))) {
362                         if (!strcmp(node_name(node_parent(s_info.curdir)), "package")) {
363                                 if (s_info.cmd != NOP) {
364                                         printf("Waiting the server response\n");
365                                         return;
366                                 }
367                                 send_inst_list(node_name(s_info.curdir));
368                                 return;
369                         }
370                 }
371
372                 ls();
373         } else if (!strcasecmp(cmd, "cd")) {
374                 struct node *node;
375
376                 if (!strcmp(argument, "."))
377                         return;
378
379                 if (s_info.cmd != NOP) {
380                         printf("Waiting the server response\n");
381                         return;
382                 }
383
384                 if (!strcmp(argument, "..")) {
385                         if (node_parent(s_info.curdir) == NULL)
386                                 return;
387
388                         s_info.curdir = node_parent(s_info.curdir);
389                 }
390
391                 node = node_child(s_info.curdir);
392                 while (node) {
393                         if (!strcmp(node_name(node), argument)) {
394                                 if (node_type(node) != NODE_DIR)
395                                         printf("Unable to go into the %s\n", node_name(node));
396                                 else
397                                         s_info.curdir = node;
398
399                                 break;
400                         }
401
402                         node = node_next_sibling(node);
403                 }
404                 if (!node)
405                         printf("Not found: %s\n", argument);
406
407                 path = node_to_abspath(s_info.curdir);
408                 printf(PROMPT"%s # ", path);
409                 free(path);
410         } else if (!strcasecmp(cmd, "help")) {
411                 goto errout;
412         } else {
413                 printf("Unknown command - \"help\"\n");
414                 path = node_to_abspath(s_info.curdir);
415                 printf(PROMPT"%s # ", path);
416                 free(path);
417         }
418
419         return;
420
421 errout:
422         help();
423         path = node_to_abspath(s_info.curdir);
424         printf(PROMPT"%s # ", path);
425         free(path);
426 }
427
428 static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler)
429 {
430         static int idx = 0;
431         static char cmd_buffer[256];
432         char *path;
433         char ch;
434         int fd;
435
436         fd = ecore_main_fd_handler_fd_get(fd_handler);
437         if (fd < 0) {
438                 printf("FD is not valid: %d\n", fd);
439                 return ECORE_CALLBACK_CANCEL;
440         }
441
442         /*!
443          * \note
444          * Using this routine, we can implement the command recommend algorithm.
445          * When a few more characters are matched with history of command, we can show it to user
446          * Then the user will choose one or write new command
447          */
448
449         /* Silly.. Silly */
450         if (read(fd, &ch, sizeof(ch)) != sizeof(ch)) {
451                 printf("Failed to get a byte: %s\n", strerror(errno));
452                 return ECORE_CALLBACK_CANCEL;
453         }
454
455         switch (ch) {
456         case 0x08: /* BKSP */
457                 cmd_buffer[idx] = '\0';
458                 if (idx > 0)
459                         idx--;
460                 cmd_buffer[idx] = ' ';
461                 path = node_to_abspath(s_info.curdir);
462                 printf("\r"PROMPT"%s # %s", path, cmd_buffer);
463                 cmd_buffer[idx] = '\0';
464                 printf("\r"PROMPT"%s # %s", path, cmd_buffer);
465                 free(path);
466                 break;
467         case '\n':
468         case '\r':
469                 cmd_buffer[idx] = '\0';
470                 idx = 0;
471                 putc((int)'\n', stdout);
472                 do_command(cmd_buffer);
473                 memset(cmd_buffer, 0, sizeof(cmd_buffer));
474                 break;
475         default:
476                 cmd_buffer[idx++] = ch;
477                 putc((int)ch, stdout);
478                 if (idx == sizeof(cmd_buffer) - 1) {
479                         cmd_buffer[idx] = '\0';
480                         printf("\nCommand buffer is overflow: %s\n", cmd_buffer);
481                         idx = 0;
482                 }
483                 break;
484         }
485
486         return ECORE_CALLBACK_RENEW;
487 }
488
489 static void processing_line_buffer(const char *buffer)
490 {
491         int pid;
492         char slavename[256];
493         char pkgname[256];
494         char abi[256];
495         char inst_id[4096];
496         char cluster[256];
497         char category[256];
498         char state[10];
499         int refcnt;
500         int fault_count;
501         int list_count;
502         int loaded_inst;
503         int loaded_pkg;
504         double ttl;
505         int secured;
506         double period;
507         int width;
508         int height;
509         int debug;
510         struct node *node;
511         struct package *pkginfo;
512         struct instance *instinfo;
513         struct slave *slaveinfo;
514         int i;
515
516         switch (s_info.cmd) {
517         case PKG_LIST:
518                 if (sscanf(buffer, "%d %[^ ] %[^ ] %[^ ] %d %d %d", &pid, slavename, pkgname, abi, &refcnt, &fault_count, &list_count) != 7) {
519                         printf("Invalid format : [%s]\n", buffer);
520                         return;
521                 }
522
523                 node = node_find(s_info.curdir, pkgname);
524                 if (!node) {
525                         pkginfo = malloc(sizeof(*pkginfo));
526                         if (!pkginfo) {
527                                 printf("Error: %s\n", strerror(errno));
528                                 return;
529                         }
530
531                         pkginfo->pkgid = strdup("conf.file");
532                         if (!pkginfo->pkgid)
533                                 printf("Error: %s\n", strerror(errno));
534
535                         pkginfo->primary = 1;
536
537                         node = node_create(s_info.curdir, pkgname, NODE_DIR);
538                         if (!node) {
539                                 free(pkginfo->pkgid);
540                                 free(pkginfo);
541                                 printf("Failed to create a new node (%s)\n", pkgname);
542                                 return;
543                         }
544
545                         node_set_data(node, pkginfo);
546                 } else {
547                         pkginfo = node_data(node);
548
549                         free(pkginfo->slavename);
550                         free(pkginfo->abi);
551
552                         pkginfo->slavename = NULL;
553                         pkginfo->abi = NULL;
554                 }
555
556                 pkginfo->slavename = strdup(slavename);
557                 if (!pkginfo->slavename)
558                         printf("Error: %s\n", strerror(errno));
559
560                 pkginfo->abi = strdup(abi);
561                 if (!pkginfo->abi)
562                         printf("Error: %s\n", strerror(errno));
563
564                 pkginfo->pid = pid;
565                 pkginfo->refcnt = refcnt;
566                 pkginfo->fault_count = fault_count;
567                 pkginfo->inst_count = list_count;
568                 break;
569         case SLAVE_LIST:
570                 if (sscanf(buffer, "%d %[^ ] %[^ ] %[^ ] %d %d %d %[^ ] %d %d %lf", &pid, slavename, pkgname, abi, &secured, &refcnt, &fault_count, state, &loaded_inst, &loaded_pkg, &ttl) != 11) {
571                         printf("Invalid format : [%s]\n", buffer);
572                         return;
573                 }
574                 node = node_find(s_info.curdir, slavename);
575                 if (!node) {
576                         slaveinfo = calloc(1, sizeof(*slaveinfo));
577                         if (!slaveinfo) {
578                                 printf("Error: %s\n", strerror(errno));
579                                 return;
580                         }
581
582                         node = node_create(s_info.curdir, slavename, NODE_DIR);
583                         if (!node) {
584                                 free(slaveinfo);
585                                 return;
586                         }
587
588                         node_set_data(node, slaveinfo);
589                 } else {
590                         slaveinfo = node_data(node);
591                 }
592                 free(slaveinfo->pkgname);
593                 free(slaveinfo->abi);
594                 free(slaveinfo->state);
595
596                 slaveinfo->pkgname = strdup(pkgname);
597                 slaveinfo->abi = strdup(abi);
598                 slaveinfo->state = strdup(state);
599
600                 slaveinfo->pid = pid;
601                 slaveinfo->secured = secured;
602                 slaveinfo->refcnt = refcnt;
603                 slaveinfo->fault_count = fault_count;
604                 slaveinfo->loaded_inst = loaded_inst;
605                 slaveinfo->loaded_pkg = loaded_pkg;
606                 slaveinfo->ttl = ttl;
607                 break;
608         case INST_LIST:
609                 if (sscanf(buffer, "%[^ ] %[^ ] %[^ ] %lf %[^ ] %d %d", inst_id, cluster, category, &period, state, &width, &height) != 7) {
610                         printf("Invalid format : [%s]\n", buffer);
611                         return;
612                 }
613
614                 for (i = strlen(inst_id); i > 0 && inst_id[i] != '/'; i--);
615                 i += (inst_id[i] == '/');
616
617                 node = node_find(s_info.curdir, inst_id + i);
618                 if (!node) {
619                         instinfo = calloc(1, sizeof(*instinfo));
620                         if (!instinfo) {
621                                 printf("Error: %s\n", strerror(errno));
622                                 return;
623                         }
624
625                         node = node_create(s_info.curdir, inst_id + i, NODE_FILE);
626                         if (!node) {
627                                 free(instinfo);
628                                 return;
629                         }
630
631                         node_set_data(node, instinfo);
632                 } else {
633                         instinfo = node_data(node);
634                 }
635
636                 free(instinfo->cluster);
637                 free(instinfo->category);
638                 free(instinfo->state);
639
640                 instinfo->cluster = strdup(cluster);
641                 if (!instinfo->cluster)
642                         printf("Error: %s\n", strerror(errno));
643
644                 instinfo->category = strdup(category);
645                 if (!instinfo->category)
646                         printf("Error: %s\n", strerror(errno));
647
648                 instinfo->state = strdup(state);
649                 instinfo->period = period;
650                 instinfo->width = width;
651                 instinfo->height = height;
652                 break;
653         case TOGGLE_DEBUG:
654                 sscanf(buffer, "%d", &debug);
655                 break;
656         default:
657                 break;
658         }
659 }
660
661 static inline void do_line_command(void)
662 {
663         switch (s_info.cmd) {
664         case PKG_LIST:
665                 ls();
666                 break;
667         case INST_LIST:
668                 ls();
669                 break;
670         case SLAVE_LIST:
671                 ls();
672                 break;
673         case TOGGLE_DEBUG:
674                 break;
675         default:
676                 break;
677         }
678 }
679
680 static Eina_Bool read_cb(void *data, Ecore_Fd_Handler *fd_handler)
681 {
682         int fd;
683         static char *line_buffer = NULL;
684         static int line_index = 0;
685         static int bufsz = 256;
686         char ch;
687
688         fd = ecore_main_fd_handler_fd_get(fd_handler);
689         if (fd < 0) {
690                 printf("FD is not valid: %d\n", fd);
691                 return ECORE_CALLBACK_CANCEL;
692         }
693
694         if (read(fd, &ch, sizeof(ch)) != sizeof(ch)) {
695                 printf("Error: %s\n", strerror(errno));
696                 return ECORE_CALLBACK_CANCEL;
697         }
698
699         if (!line_buffer) {
700                 line_index = 0;
701                 line_buffer = malloc(bufsz);
702                 if (!line_buffer) {
703                         printf("Error: %s\n", strerror(errno));
704                         return ECORE_CALLBACK_CANCEL;
705                 }
706         }       
707
708         if (ch == '\n') { /* End of a line */
709                 line_buffer[line_index] = '\0';
710
711                 if (!strcmp(line_buffer, "EOD")) {
712                         do_line_command();
713                         s_info.cmd = NOP;
714                 } else {
715                         processing_line_buffer(line_buffer);
716                 }
717
718                 free(line_buffer);
719                 line_buffer = NULL;
720                 line_index = 0;
721                 bufsz = 256;
722         } else {
723                 char *new_buf;
724
725                 line_buffer[line_index++] = ch;
726                 if (line_index == bufsz - 1) {
727                         bufsz += 256;
728                         new_buf = realloc(line_buffer, bufsz);
729                         if (!new_buf) {
730                                 printf("Error: %s\n", strerror(errno));
731                                 free(line_buffer);
732                                 line_buffer = 0;
733                                 line_index = 0;
734                                 bufsz = 256;
735                                 return ECORE_CALLBACK_CANCEL;
736                         }
737
738                         line_buffer = new_buf;
739                 }
740         }
741
742         return ECORE_CALLBACK_RENEW;
743 }
744
745 static int ret_cb(pid_t pid, int handle, const struct packet *packet, void *data)
746 {
747         const char *fifo_name;
748         int ret;
749         char *path;
750
751         if (packet_get(packet, "si", &fifo_name, &ret) != 2) {
752                 printf("Invalid packet\n");
753                 return -EFAULT;
754         }
755
756         if (ret != 0) {
757                 printf("Returns %d\n", ret);
758                 return ret;
759         }
760
761         printf("FIFO: %s\n", fifo_name);
762
763         s_info.fifo_handle = open(fifo_name, O_RDONLY | O_NONBLOCK);
764         if (s_info.fifo_handle < 0) {
765                 printf("Error: %s\n", strerror(errno));
766                 s_info.fifo_handle = -EINVAL;
767                 ecore_main_loop_quit();
768                 return -EINVAL;
769         }
770
771         s_info.fd_handler = ecore_main_fd_handler_add(s_info.fifo_handle, ECORE_FD_READ, read_cb, NULL, NULL, NULL);
772         if (!s_info.fd_handler) {
773                 printf("Failed to add a fd handler\n");
774                 close(s_info.fifo_handle);
775                 s_info.fifo_handle = -EINVAL;
776                 ecore_main_loop_quit();
777                 return -EFAULT;
778         }
779
780         path = node_to_abspath(s_info.curdir);
781         printf(PROMPT"%s # ", path);
782         free(path);
783
784         if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) < 0)
785                 printf("Error: %s\n", strerror(errno));
786
787         s_info.in_handler = ecore_main_fd_handler_add(STDIN_FILENO, ECORE_FD_READ, input_cb, NULL, NULL, NULL);
788         if (!s_info.in_handler) {
789                 printf("Failed to add a input handler\n");
790                 ecore_main_loop_quit();
791                 return -EFAULT;
792         }
793
794         return 0;
795 }
796
797 static int disconnected_cb(int handle, void *data)
798 {
799         printf("Disconnected\n");
800         ecore_main_loop_quit();
801         return 0;
802 }
803
804 static int connected_cb(int handle, void *data)
805 {
806         struct packet *packet;
807
808         printf("Connected\n");
809
810         packet = packet_create("liveinfo_hello", "d", 0.0f);
811         if (!packet) {
812                 printf("Failed to build a packet for hello\n");
813                 com_core_packet_client_fini(s_info.fd);
814                 s_info.fd = -EINVAL;
815                 return -EFAULT;
816         }
817
818         s_info.fd = handle;
819
820         if (com_core_packet_async_send(s_info.fd, packet, 0.0f, ret_cb, NULL) < 0) {
821                 printf("Failed to send a packet hello\n");
822                 packet_destroy(packet);
823                 com_core_packet_client_fini(s_info.fd);
824                 s_info.fd = -EINVAL;
825                 return -EFAULT;
826         }
827
828         packet_destroy(packet);
829         return 0;
830 }
831
832 int main(int argc, char *argv[])
833 {
834         struct termios ttystate;
835         static struct method s_table[] = {
836                 {
837                         .cmd = NULL,
838                         .handler = NULL,
839                 },
840         };
841
842         ecore_init();
843         g_type_init();
844
845         com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
846         com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
847         livebox_service_init();
848
849         s_info.fd = com_core_packet_client_init(SOCKET_FILE, 0, s_table);
850         if (s_info.fd < 0) {
851                 printf("Failed to make a connection\n");
852                 return -EIO;
853         }
854
855         printf("Type your command on below empty line\n");
856
857         if (tcgetattr(STDIN_FILENO, &ttystate) < 0) {
858                 printf("Error: %s\n", strerror(errno));
859         } else {
860                 ttystate.c_lflag &= ~(ICANON | ECHO);
861                 ttystate.c_cc[VMIN] = 1;
862
863                 if (tcsetattr(STDIN_FILENO, TCSANOW, &ttystate) < 0)
864                         printf("Error: %s\n", strerror(errno));
865         }
866
867         if (setvbuf(stdout, (char *)NULL, _IONBF, 0) != 0)
868                 printf("Error: %s\n", strerror(errno));
869
870         init_directory();
871
872         ecore_main_loop_begin();
873
874         fini_directory();
875         livebox_service_fini();
876
877         ttystate.c_lflag |= ICANON | ECHO;
878         if (tcsetattr(STDIN_FILENO, TCSANOW, &ttystate) < 0)
879                 printf("Error: %s\n", strerror(errno));
880
881         if (s_info.fd > 0) {
882                 com_core_packet_client_fini(s_info.fd);
883                 s_info.fd = -EINVAL;
884         }
885
886         if (s_info.fd_handler) {
887                 ecore_main_fd_handler_del(s_info.fd_handler);
888                 s_info.fd_handler = NULL;
889         }
890
891         if (s_info.fifo_handle > 0) {
892                 close(s_info.fifo_handle);
893                 s_info.fifo_handle = -EINVAL;
894         }
895
896         if (s_info.in_handler) {
897                 ecore_main_fd_handler_del(s_info.in_handler);
898                 s_info.in_handler = NULL;
899         }
900
901         ecore_shutdown();
902         return 0;
903 }
904
905 /* End of a file */