Fix breakage caused by libgdbus API change
[connman] / src / manager.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 <gdbus.h>
27
28 #include "connman.h"
29
30 static DBusMessage *list_interfaces(DBusConnection *conn,
31                                         DBusMessage *msg, void *data)
32 {
33         DBusMessage *reply;
34         DBusMessageIter array, iter;
35
36         DBG("conn %p", conn);
37
38         reply = dbus_message_new_method_return(msg);
39         if (reply == NULL)
40                 return NULL;
41
42         dbus_message_iter_init_append(reply, &array);
43
44         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
45                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
46
47         __connman_iface_list(&iter);
48
49         dbus_message_iter_close_container(&array, &iter);
50
51         return reply;
52 }
53
54 static DBusMessage *get_state(DBusConnection *conn,
55                                         DBusMessage *msg, void *data)
56 {
57         DBusMessage *reply;
58         const char *state;
59
60         DBG("conn %p", conn);
61
62         reply = dbus_message_new_method_return(msg);
63         if (reply == NULL)
64                 return NULL;
65
66         if (__connman_iface_is_connected() == TRUE)
67                 state = "online";
68         else
69                 state = "offline";
70
71         dbus_message_append_args(reply, DBUS_TYPE_STRING, &state,
72                                                         DBUS_TYPE_INVALID);
73
74         return reply;
75 }
76
77 static DBusMessage *register_agent(DBusConnection *conn,
78                                         DBusMessage *msg, void *data)
79 {
80         DBusMessage *reply;
81         const char *sender, *path;
82
83         DBG("conn %p", conn);
84
85         sender = dbus_message_get_sender(msg);
86
87         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
88                                                         DBUS_TYPE_INVALID);
89
90         reply = dbus_message_new_method_return(msg);
91         if (reply == NULL)
92                 return NULL;
93
94         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
95
96         __connman_agent_register(sender, path);
97
98         return reply;
99 }
100
101 static DBusMessage *unregister_agent(DBusConnection *conn,
102                                         DBusMessage *msg, void *data)
103 {
104         DBusMessage *reply;
105         const char *sender, *path;
106
107         DBG("conn %p", conn);
108
109         sender = dbus_message_get_sender(msg);
110
111         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
112                                                         DBUS_TYPE_INVALID);
113
114         reply = dbus_message_new_method_return(msg);
115         if (reply == NULL)
116                 return NULL;
117
118         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
119
120         __connman_agent_unregister(sender, path);
121
122         return reply;
123 }
124
125 static GDBusMethodTable manager_methods[] = {
126         { "ListInterfaces",  "",  "ao", list_interfaces  },
127         { "GetState",        "",  "s",  get_state        },
128         { "RegisterAgent",   "o", "",   register_agent   },
129         { "UnregisterAgent", "o", "",   unregister_agent },
130         { },
131 };
132
133 static GDBusSignalTable manager_signals[] = {
134         { "InterfaceAdded",   "o" },
135         { "InterfaceRemoved", "o" },
136         { "StateChanged",     "s" },
137         { },
138 };
139
140 static DBusMessage *nm_sleep(DBusConnection *conn,
141                                         DBusMessage *msg, void *data)
142 {
143         DBusMessage *reply;
144
145         DBG("conn %p", conn);
146
147         reply = dbus_message_new_method_return(msg);
148         if (reply == NULL)
149                 return NULL;
150
151         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
152
153         return reply;
154 }
155
156 static DBusMessage *nm_wake(DBusConnection *conn,
157                                         DBusMessage *msg, void *data)
158 {
159         DBusMessage *reply;
160
161         DBG("conn %p", conn);
162
163         reply = dbus_message_new_method_return(msg);
164         if (reply == NULL)
165                 return NULL;
166
167         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
168
169         return reply;
170 }
171
172 enum {
173         NM_STATE_UNKNOWN = 0,
174         NM_STATE_ASLEEP,
175         NM_STATE_CONNECTING,
176         NM_STATE_CONNECTED,
177         NM_STATE_DISCONNECTED
178 };
179
180 static DBusMessage *nm_state(DBusConnection *conn,
181                                         DBusMessage *msg, void *data)
182 {
183         DBusMessage *reply;
184         dbus_uint32_t state;
185
186         DBG("conn %p", conn);
187
188         reply = dbus_message_new_method_return(msg);
189         if (reply == NULL)
190                 return NULL;
191
192         if (__connman_iface_is_connected() == TRUE)
193                 state = NM_STATE_CONNECTED;
194         else
195                 state = NM_STATE_DISCONNECTED;
196
197         dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
198                                                         DBUS_TYPE_INVALID);
199
200         return reply;
201 }
202
203 static GDBusMethodTable nm_methods[] = {
204         { "sleep", "",  "",   nm_sleep        },
205         { "wake",  "",  "",   nm_wake         },
206         { "state", "",  "u",  nm_state        },
207         { },
208 };
209
210 static DBusConnection *connection = NULL;
211 static gboolean nm_compat = FALSE;
212
213 int __connman_manager_init(DBusConnection *conn, gboolean compat)
214 {
215         DBG("conn %p", conn);
216
217         connection = dbus_connection_ref(conn);
218         if (connection == NULL)
219                 return -1;
220
221         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
222                                         CONNMAN_MANAGER_INTERFACE,
223                                         manager_methods,
224                                         manager_signals, NULL, NULL, NULL);
225
226         if (compat == TRUE) {
227                 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
228                                         nm_methods, NULL, NULL, NULL, NULL);
229
230                 nm_compat = TRUE;
231         }
232
233         return 0;
234 }
235
236 void __connman_manager_cleanup(void)
237 {
238         DBG("conn %p", connection);
239
240         if (nm_compat == TRUE) {
241                 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
242         }
243
244         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
245                                                 CONNMAN_MANAGER_INTERFACE);
246
247         dbus_connection_unref(connection);
248 }