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