Support selected listing and counting of elements
[platform/upstream/connman.git] / 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 void append_profiles(DBusMessageIter *dict)
31 {
32         DBusMessageIter entry, value, iter;
33         const char *key = "Profiles";
34
35         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
36                                                                 NULL, &entry);
37
38         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
39
40         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
41                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
42                                                                 &value);
43
44         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
45                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
46
47         __connman_profile_list(&iter);
48
49         dbus_message_iter_close_container(&value, &iter);
50
51         dbus_message_iter_close_container(&entry, &value);
52
53         dbus_message_iter_close_container(dict, &entry);
54 }
55
56 static void append_devices(DBusMessageIter *dict)
57 {
58         DBusMessageIter entry, value, iter;
59         const char *key = "Devices";
60
61         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
62                                                                 NULL, &entry);
63
64         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
65
66         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
67                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
68                                                                 &value);
69
70         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
71                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
72
73         __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
74
75         dbus_message_iter_close_container(&value, &iter);
76
77         dbus_message_iter_close_container(&entry, &value);
78
79         dbus_message_iter_close_container(dict, &entry);
80 }
81
82 static void append_connections(DBusMessageIter *dict)
83 {
84         DBusMessageIter entry, value, iter;
85         const char *key = "Connections";
86
87         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
88                                                                 NULL, &entry);
89
90         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
91
92         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
93                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
94                                                                 &value);
95
96         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
97                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
98
99         __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION, &iter);
100
101         dbus_message_iter_close_container(&value, &iter);
102
103         dbus_message_iter_close_container(&entry, &value);
104
105         dbus_message_iter_close_container(dict, &entry);
106 }
107
108 static void append_state(DBusMessageIter *dict, const char *state)
109 {
110         DBusMessageIter entry, value;
111         const char *key = "State";
112
113         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
114                                                                 NULL, &entry);
115
116         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
117
118         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
119                                         DBUS_TYPE_STRING_AS_STRING, &value);
120
121         dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &state);
122
123         dbus_message_iter_close_container(&entry, &value);
124
125         dbus_message_iter_close_container(dict, &entry);
126 }
127
128 static DBusMessage *get_properties(DBusConnection *conn,
129                                         DBusMessage *msg, void *data)
130 {
131         DBusMessage *reply;
132         DBusMessageIter array, dict;
133
134         DBG("conn %p", conn);
135
136         reply = dbus_message_new_method_return(msg);
137         if (reply == NULL)
138                 return NULL;
139
140         dbus_message_iter_init_append(reply, &array);
141
142         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
143                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
144                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
145                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
146
147         append_profiles(&dict);
148
149         append_devices(&dict);
150         append_connections(&dict);
151
152         if (__connman_element_count(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION) > 0)
153                 append_state(&dict, "online");
154         else
155                 append_state(&dict, "offline");
156
157         dbus_message_iter_close_container(&array, &dict);
158
159         return reply;
160 }
161
162 static DBusMessage *register_agent(DBusConnection *conn,
163                                         DBusMessage *msg, void *data)
164 {
165         DBusMessage *reply;
166         const char *sender, *path;
167
168         DBG("conn %p", conn);
169
170         sender = dbus_message_get_sender(msg);
171
172         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
173                                                         DBUS_TYPE_INVALID);
174
175         reply = dbus_message_new_method_return(msg);
176         if (reply == NULL)
177                 return NULL;
178
179         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
180
181         __connman_agent_register(sender, path);
182
183         return reply;
184 }
185
186 static DBusMessage *unregister_agent(DBusConnection *conn,
187                                         DBusMessage *msg, void *data)
188 {
189         DBusMessage *reply;
190         const char *sender, *path;
191
192         DBG("conn %p", conn);
193
194         sender = dbus_message_get_sender(msg);
195
196         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
197                                                         DBUS_TYPE_INVALID);
198
199         reply = dbus_message_new_method_return(msg);
200         if (reply == NULL)
201                 return NULL;
202
203         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
204
205         __connman_agent_unregister(sender, path);
206
207         return reply;
208 }
209
210 #if 0
211 static DBusMessage *list_elements(DBusConnection *conn,
212                                         DBusMessage *msg, void *data)
213 {
214         DBusMessage *reply;
215         DBusMessageIter array, iter;
216
217         DBG("conn %p", conn);
218
219         reply = dbus_message_new_method_return(msg);
220         if (reply == NULL)
221                 return NULL;
222
223         dbus_message_iter_init_append(reply, &array);
224
225         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
226                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
227
228         __connman_element_list(CONNMAN_ELEMENT_TYPE_UNKNOWN, &iter);
229
230         dbus_message_iter_close_container(&array, &iter);
231
232         return reply;
233 }
234 #endif
235
236 static GDBusMethodTable manager_methods[] = {
237         { "GetProperties",   "",  "a{sv}", get_properties   },
238         { "RegisterAgent",   "o", "",      register_agent   },
239         { "UnregisterAgent", "o", "",      unregister_agent },
240 #if 0
241         { "ListElements",    "",  "ao",    list_elements    },
242 #endif
243         { },
244 };
245
246 static GDBusSignalTable manager_signals[] = {
247         { "PropertyChanged", "sv" },
248 #if 0
249         { "ElementAdded",    "o"  },
250         { "ElementUpdated",  "o"  },
251         { "ElementRemoved",  "o"  },
252 #endif
253         { },
254 };
255
256 static DBusMessage *nm_sleep(DBusConnection *conn,
257                                         DBusMessage *msg, void *data)
258 {
259         DBusMessage *reply;
260
261         DBG("conn %p", conn);
262
263         reply = dbus_message_new_method_return(msg);
264         if (reply == NULL)
265                 return NULL;
266
267         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
268
269         return reply;
270 }
271
272 static DBusMessage *nm_wake(DBusConnection *conn,
273                                         DBusMessage *msg, void *data)
274 {
275         DBusMessage *reply;
276
277         DBG("conn %p", conn);
278
279         reply = dbus_message_new_method_return(msg);
280         if (reply == NULL)
281                 return NULL;
282
283         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
284
285         return reply;
286 }
287
288 enum {
289         NM_STATE_UNKNOWN = 0,
290         NM_STATE_ASLEEP,
291         NM_STATE_CONNECTING,
292         NM_STATE_CONNECTED,
293         NM_STATE_DISCONNECTED
294 };
295
296 static DBusMessage *nm_state(DBusConnection *conn,
297                                         DBusMessage *msg, void *data)
298 {
299         DBusMessage *reply;
300         dbus_uint32_t state;
301
302         DBG("conn %p", conn);
303
304         reply = dbus_message_new_method_return(msg);
305         if (reply == NULL)
306                 return NULL;
307
308         state = NM_STATE_DISCONNECTED;
309
310         dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
311                                                         DBUS_TYPE_INVALID);
312
313         return reply;
314 }
315
316 static GDBusMethodTable nm_methods[] = {
317         { "sleep", "",  "",   nm_sleep        },
318         { "wake",  "",  "",   nm_wake         },
319         { "state", "",  "u",  nm_state        },
320         { },
321 };
322
323 static DBusConnection *connection = NULL;
324 static gboolean nm_compat = FALSE;
325
326 int __connman_manager_init(DBusConnection *conn, gboolean compat)
327 {
328         DBG("conn %p", conn);
329
330         connection = dbus_connection_ref(conn);
331         if (connection == NULL)
332                 return -1;
333
334         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
335                                         CONNMAN_MANAGER_INTERFACE,
336                                         manager_methods,
337                                         manager_signals, NULL, NULL, NULL);
338
339         if (compat == TRUE) {
340                 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
341                                         nm_methods, NULL, NULL, NULL, NULL);
342
343                 nm_compat = TRUE;
344         }
345
346         return 0;
347 }
348
349 void __connman_manager_cleanup(void)
350 {
351         DBG("conn %p", connection);
352
353         if (nm_compat == TRUE) {
354                 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
355         }
356
357         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
358                                                 CONNMAN_MANAGER_INTERFACE);
359
360         dbus_connection_unref(connection);
361 }