client: Delete the now obsolete monitor.[hc] files
[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
43 static GMainLoop *main_loop;
44 DBusConnection *connection;
45
46 static gboolean timeout_wait(gpointer data)
47 {
48         static int i;
49         i++;
50         /* Set to whatever number of retries is wanted/needed */
51         if (i == 1) {
52                 g_main_loop_quit(data);
53                 return FALSE;
54         }
55         return TRUE;
56 }
57
58 static void rl_handler(char *input)
59 {
60
61         if (input == NULL)
62                 exit(EXIT_FAILURE);
63         else
64                 printf("Use ctrl-d to exit\n");
65 }
66
67 static gboolean readmonitor(GIOChannel *channel, GIOCondition condition,
68                                                 gpointer user_data){
69         rl_callback_read_char();
70         return TRUE;
71 }
72
73 int main(int argc, char *argv[])
74 {
75         DBusError err;
76         int events, error;
77         GIOChannel *gchan;
78         main_loop = g_main_loop_new(NULL, FALSE);
79
80         dbus_error_init(&err);
81
82         connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
83
84         if (dbus_error_is_set(&err)) {
85                 fprintf(stderr, "Connection Error: %s\n", err.message);
86                 dbus_error_free(&err);
87         }
88
89         if (connection == NULL) {
90                 fprintf(stderr, "Could not connect to system bus...exiting\n");
91                 exit(EXIT_FAILURE);
92         }
93
94         if (argc < 2)
95                 show_interactive(connection, main_loop);
96
97         error = commands(connection, argv + 1, argc -1);
98
99         if (error == -1) {
100                 char *help = "help";
101
102                 printf("Usage: connmanctl [[command] [args]]\n");
103                 commands(connection, &help, 1);
104                 printf("\nNote: arguments and output are considered "
105                                 "EXPERIMENTAL for now.\n\n");
106                 return -EINVAL;
107         }
108
109         if (error < 0)
110                 return error;
111
112         gchan = g_io_channel_unix_new(fileno(stdin));
113         events = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
114         g_io_add_watch(gchan, events, readmonitor, NULL);
115         rl_callback_handler_install("", rl_handler);
116
117         if (strcmp(argv[1], "monitor") != 0)
118                 g_timeout_add_full(G_PRIORITY_DEFAULT, 100, timeout_wait,
119                                                                main_loop, NULL);
120         g_main_loop_run(main_loop);
121         rl_callback_handler_remove();
122         g_io_channel_unref(gchan);
123         if (main_loop != NULL)
124                 g_main_loop_unref(main_loop);
125         return 0;
126 }