client: Add pretty-printing for ServicesChanged signal
[platform/upstream/connman.git] / client / main.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  Intel Corporation. All rights reserved.
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <getopt.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <readline/readline.h>
34
35 #include <glib.h>
36 #include <gdbus.h>
37
38 #include "data_manager.h"
39 #include "services.h"
40 #include "technology.h"
41 #include "interactive.h"
42 #include "monitor.h"
43
44 static GMainLoop *main_loop;
45 DBusConnection *connection;
46
47 static gboolean timeout_wait(gpointer data)
48 {
49         static int i;
50         i++;
51         /* Set to whatever number of retries is wanted/needed */
52         if (i == 1) {
53                 g_main_loop_quit(data);
54                 return FALSE;
55         }
56         return TRUE;
57 }
58
59 static void rl_handler(char *input)
60 {
61
62         if (input == NULL)
63                 exit(EXIT_FAILURE);
64         else
65                 printf("Use ctrl-d to exit\n");
66 }
67
68 static gboolean readmonitor(GIOChannel *channel, GIOCondition condition,
69                                                 gpointer user_data){
70         rl_callback_read_char();
71         return TRUE;
72 }
73
74 int main(int argc, char *argv[])
75 {
76         DBusError err;
77         int events, error;
78         GIOChannel *gchan;
79         main_loop = g_main_loop_new(NULL, FALSE);
80
81         dbus_error_init(&err);
82
83         connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
84
85         if (dbus_error_is_set(&err)) {
86                 fprintf(stderr, "Connection Error: %s\n", err.message);
87                 dbus_error_free(&err);
88         }
89
90         if (connection == NULL) {
91                 fprintf(stderr, "Could not connect to system bus...exiting\n");
92                 exit(EXIT_FAILURE);
93         }
94
95         if (argc < 2)
96                 show_interactive(connection, main_loop);
97
98         error = commands(connection, argv + 1, argc -1);
99
100         if (error == -1) {
101                 char *help = "help";
102
103                 printf("Usage: connmanctl [[command] [args]]\n");
104                 commands(connection, &help, 1);
105                 printf("\nNote: arguments and output are considered "
106                                 "EXPERIMENTAL for now.\n\n");
107                 return -EINVAL;
108         }
109
110         if (error < 0)
111                 return error;
112
113         gchan = g_io_channel_unix_new(fileno(stdin));
114         events = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
115         g_io_add_watch(gchan, events, readmonitor, NULL);
116         rl_callback_handler_install("", rl_handler);
117
118         if (strcmp(argv[1], "monitor") != 0)
119                 g_timeout_add_full(G_PRIORITY_DEFAULT, 100, timeout_wait,
120                                                                main_loop, NULL);
121         g_main_loop_run(main_loop);
122         rl_callback_handler_remove();
123         g_io_channel_unref(gchan);
124         if (main_loop != NULL)
125                 g_main_loop_unref(main_loop);
126         return 0;
127 }