Add really simple client application
[platform/upstream/connman.git] / client / main.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <dbus/dbus.h>
32
33 #define CONNMAN_SERVICE                 "org.moblin.connman"
34
35 #define CONNMAN_MANAGER_INTERFACE       CONNMAN_SERVICE ".Manager"
36 #define CONNMAN_MANAGER_PATH            "/"
37
38 static const char *extract_state(DBusMessage *message)
39 {
40         DBusMessageIter array, dict;
41
42         dbus_message_iter_init(message, &array);
43         dbus_message_iter_recurse(&array, &dict);
44
45         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
46                 DBusMessageIter entry, value;
47                 const char *key;
48
49                 dbus_message_iter_recurse(&dict, &entry);
50                 dbus_message_iter_get_basic(&entry, &key);
51
52                 dbus_message_iter_next(&entry);
53
54                 dbus_message_iter_recurse(&entry, &value);
55
56                 //type = dbus_message_iter_get_arg_type(&value);
57                 //dbus_message_iter_get_basic(&value, &val);
58
59                 if (strcmp(key, "State") == 0) {
60                         const char *val;
61                         dbus_message_iter_get_basic(&value, &val);
62                         return val;
63                 }
64
65                 dbus_message_iter_next(&dict);
66         }
67
68         return NULL;
69 }
70
71 static int cmd_status(DBusConnection *connection)
72 {
73         DBusMessage *message, *reply;
74         DBusError error;
75         const char *state;
76
77         message = dbus_message_new_method_call(CONNMAN_SERVICE,
78                                                 CONNMAN_MANAGER_PATH,
79                                                 CONNMAN_MANAGER_INTERFACE,
80                                                         "GetProperties");
81         if (message == NULL)
82                 return -ENOMEM;
83
84         dbus_error_init(&error);
85
86         reply = dbus_connection_send_with_reply_and_block(connection,
87                                                         message, -1, &error);
88         if (reply == NULL) {
89                 if (dbus_error_is_set(&error) == TRUE) {
90                         fprintf(stderr, "%s\n", error.message);
91                         dbus_error_free(&error);
92                 } else
93                         fprintf(stderr, "Failed to get properties\n");
94                 dbus_message_unref(message);
95                 return -EIO;
96         }
97
98         dbus_message_unref(message);
99
100         dbus_error_init(&error);
101
102         state = extract_state(reply);
103
104         dbus_message_unref(reply);
105
106         if (state == NULL)
107                 return -EINVAL;
108
109         printf("System is %s\n", state);
110
111         return 0;
112 }
113
114 int main(int argc, char *argv[])
115 {
116         DBusConnection *conn;
117
118         conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
119         if (!conn) {
120                 fprintf(stderr, "Can't get on system bus\n");
121                 exit(1);
122         }
123
124         cmd_status(conn);
125
126         dbus_connection_unref(conn);
127
128         return 0;
129 }