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