gdbus: Add support for org.freedesktop.DBus.ObjectManager interface
[platform/upstream/connman.git] / src / manager.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 <errno.h>
27
28 #include <gdbus.h>
29
30 #include <connman/agent.h>
31
32 #include "connman.h"
33
34 static connman_bool_t connman_state_idle;
35 static DBusMessage *session_mode_pending = NULL;
36
37 static DBusMessage *get_properties(DBusConnection *conn,
38                                         DBusMessage *msg, void *data)
39 {
40         DBusMessage *reply;
41         DBusMessageIter array, dict;
42         connman_bool_t offlinemode, sessionmode;
43         const char *str;
44
45         DBG("conn %p", conn);
46
47         reply = dbus_message_new_method_return(msg);
48         if (reply == NULL)
49                 return NULL;
50
51         dbus_message_iter_init_append(reply, &array);
52
53         connman_dbus_dict_open(&array, &dict);
54
55         str = __connman_notifier_get_state();
56         connman_dbus_dict_append_basic(&dict, "State",
57                                                 DBUS_TYPE_STRING, &str);
58
59         offlinemode = __connman_technology_get_offlinemode();
60         connman_dbus_dict_append_basic(&dict, "OfflineMode",
61                                         DBUS_TYPE_BOOLEAN, &offlinemode);
62
63         sessionmode = __connman_session_mode();
64         connman_dbus_dict_append_basic(&dict, "SessionMode",
65                                         DBUS_TYPE_BOOLEAN,
66                                         &sessionmode);
67
68         connman_dbus_dict_close(&array, &dict);
69
70         return reply;
71 }
72
73 static DBusMessage *set_property(DBusConnection *conn,
74                                         DBusMessage *msg, void *data)
75 {
76         DBusMessageIter iter, value;
77         const char *name;
78         int type;
79
80         DBG("conn %p", conn);
81
82         if (dbus_message_iter_init(msg, &iter) == FALSE)
83                 return __connman_error_invalid_arguments(msg);
84
85         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
86                 return __connman_error_invalid_arguments(msg);
87
88         dbus_message_iter_get_basic(&iter, &name);
89         dbus_message_iter_next(&iter);
90
91         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
92                 return __connman_error_invalid_arguments(msg);
93
94         dbus_message_iter_recurse(&iter, &value);
95
96         type = dbus_message_iter_get_arg_type(&value);
97
98         if (g_str_equal(name, "OfflineMode") == TRUE) {
99                 connman_bool_t offlinemode;
100
101                 if (type != DBUS_TYPE_BOOLEAN)
102                         return __connman_error_invalid_arguments(msg);
103
104                 dbus_message_iter_get_basic(&value, &offlinemode);
105
106                 __connman_technology_set_offlinemode(offlinemode);
107         } else if (g_str_equal(name, "SessionMode") == TRUE) {
108                 connman_bool_t sessionmode;
109
110                 if (type != DBUS_TYPE_BOOLEAN)
111                         return __connman_error_invalid_arguments(msg);
112
113                 dbus_message_iter_get_basic(&value, &sessionmode);
114
115                 if (session_mode_pending != NULL)
116                         return __connman_error_in_progress(msg);
117
118                 __connman_session_set_mode(sessionmode);
119
120                 if (sessionmode == TRUE && connman_state_idle == FALSE) {
121                         session_mode_pending = dbus_message_ref(msg);
122                         return NULL;
123                 }
124
125         } else
126                 return __connman_error_invalid_property(msg);
127
128         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
129 }
130
131 static void append_technology_structs(DBusMessageIter *iter, void *user_data)
132 {
133         __connman_technology_list_struct(iter);
134 }
135
136 static DBusMessage *get_technologies(DBusConnection *conn,
137                 DBusMessage *msg, void *data)
138 {
139         DBusMessage *reply;
140
141         DBG("");
142
143         reply = dbus_message_new_method_return(msg);
144         if (reply == NULL)
145                 return NULL;
146
147         __connman_dbus_append_objpath_dict_array(reply,
148                         append_technology_structs, NULL);
149
150         return reply;
151 }
152
153 static DBusMessage *remove_provider(DBusConnection *conn,
154                                     DBusMessage *msg, void *data)
155 {
156         const char *path;
157         int err;
158
159         DBG("conn %p", conn);
160
161         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
162                                                         DBUS_TYPE_INVALID);
163
164         err = __connman_provider_remove(path);
165         if (err < 0)
166                 return __connman_error_failed(msg, -err);
167
168         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
169 }
170
171 static DBusConnection *connection = NULL;
172
173 static void session_mode_notify(void)
174 {
175         DBusMessage *reply;
176
177         reply = g_dbus_create_reply(session_mode_pending, DBUS_TYPE_INVALID);
178         g_dbus_send_message(connection, reply);
179
180         dbus_message_unref(session_mode_pending);
181         session_mode_pending = NULL;
182 }
183
184 static void idle_state(connman_bool_t idle)
185 {
186
187         DBG("idle %d", idle);
188
189         connman_state_idle = idle;
190
191         if (connman_state_idle == FALSE || session_mode_pending == NULL)
192                 return;
193
194         session_mode_notify();
195 }
196
197 static struct connman_notifier technology_notifier = {
198         .name           = "manager",
199         .priority       = CONNMAN_NOTIFIER_PRIORITY_HIGH,
200         .idle_state     = idle_state,
201 };
202
203 static void append_service_structs(DBusMessageIter *iter, void *user_data)
204 {
205         __connman_service_list_struct(iter);
206 }
207
208 static DBusMessage *get_services(DBusConnection *conn,
209                                         DBusMessage *msg, void *data)
210 {
211         DBusMessage *reply;
212
213         reply = dbus_message_new_method_return(msg);
214         if (reply == NULL)
215                 return NULL;
216
217         __connman_dbus_append_objpath_dict_array(reply,
218                         append_service_structs, NULL);
219
220         return reply;
221 }
222
223 static DBusMessage *connect_provider(DBusConnection *conn,
224                                         DBusMessage *msg, void *data)
225 {
226         int err;
227
228         DBG("conn %p", conn);
229
230         if (__connman_session_mode() == TRUE) {
231                 connman_info("Session mode enabled: "
232                                 "direct provider connect disabled");
233
234                 return __connman_error_failed(msg, EINVAL);
235         }
236
237         err = __connman_provider_create_and_connect(msg);
238         if (err < 0) {
239                 if (err == -EINPROGRESS) {
240                         connman_error("Invalid return code from connect");
241                         err = -EINVAL;
242                 }
243
244                 return __connman_error_failed(msg, -err);
245         }
246
247         return NULL;
248 }
249
250 static DBusMessage *register_agent(DBusConnection *conn,
251                                         DBusMessage *msg, void *data)
252 {
253         const char *sender, *path;
254         int err;
255
256         DBG("conn %p", conn);
257
258         sender = dbus_message_get_sender(msg);
259
260         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
261                                                         DBUS_TYPE_INVALID);
262
263         err = connman_agent_register(sender, path);
264         if (err < 0)
265                 return __connman_error_failed(msg, -err);
266
267         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
268 }
269
270 static DBusMessage *unregister_agent(DBusConnection *conn,
271                                         DBusMessage *msg, void *data)
272 {
273         const char *sender, *path;
274         int err;
275
276         DBG("conn %p", conn);
277
278         sender = dbus_message_get_sender(msg);
279
280         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
281                                                         DBUS_TYPE_INVALID);
282
283         err = connman_agent_unregister(sender, path);
284         if (err < 0)
285                 return __connman_error_failed(msg, -err);
286
287         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
288 }
289
290 static DBusMessage *register_counter(DBusConnection *conn,
291                                         DBusMessage *msg, void *data)
292 {
293         const char *sender, *path;
294         unsigned int accuracy, period;
295         int err;
296
297         DBG("conn %p", conn);
298
299         sender = dbus_message_get_sender(msg);
300
301         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
302                                                 DBUS_TYPE_UINT32, &accuracy,
303                                                 DBUS_TYPE_UINT32, &period,
304                                                         DBUS_TYPE_INVALID);
305
306         /* FIXME: add handling of accuracy parameter */
307
308         err = __connman_counter_register(sender, path, period);
309         if (err < 0)
310                 return __connman_error_failed(msg, -err);
311
312         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
313 }
314
315 static DBusMessage *unregister_counter(DBusConnection *conn,
316                                         DBusMessage *msg, void *data)
317 {
318         const char *sender, *path;
319         int err;
320
321         DBG("conn %p", conn);
322
323         sender = dbus_message_get_sender(msg);
324
325         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
326                                                         DBUS_TYPE_INVALID);
327
328         err = __connman_counter_unregister(sender, path);
329         if (err < 0)
330                 return __connman_error_failed(msg, -err);
331
332         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
333 }
334
335 static DBusMessage *create_session(DBusConnection *conn,
336                                         DBusMessage *msg, void *data)
337 {
338         int err;
339
340         DBG("conn %p", conn);
341
342         err = __connman_session_create(msg);
343         if (err < 0) {
344                 if (err == -EINPROGRESS)
345                         return NULL;
346
347                 return __connman_error_failed(msg, -err);
348         }
349
350         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
351 }
352
353 static DBusMessage *destroy_session(DBusConnection *conn,
354                                         DBusMessage *msg, void *data)
355 {
356         int err;
357
358         DBG("conn %p", conn);
359
360         err = __connman_session_destroy(msg);
361         if (err < 0)
362                 return __connman_error_failed(msg, -err);
363
364         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
365 }
366
367 static DBusMessage *request_private_network(DBusConnection *conn,
368                                         DBusMessage *msg, void *data)
369 {
370         const char *sender;
371         int  err;
372
373         DBG("conn %p", conn);
374
375         sender = dbus_message_get_sender(msg);
376
377         err = __connman_private_network_request(msg, sender);
378         if (err < 0)
379                 return __connman_error_failed(msg, -err);
380
381         return NULL;
382 }
383
384 static DBusMessage *release_private_network(DBusConnection *conn,
385                                         DBusMessage *msg, void *data)
386 {
387         const char *path;
388         int err;
389
390         DBG("conn %p", conn);
391
392         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
393                                                         DBUS_TYPE_INVALID);
394
395         err = __connman_private_network_release(path);
396         if (err < 0)
397                 return __connman_error_failed(msg, -err);
398
399         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
400 }
401
402 static const GDBusMethodTable manager_methods[] = {
403         { GDBUS_METHOD("GetProperties",
404                         NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
405                         get_properties) },
406         { GDBUS_ASYNC_METHOD("SetProperty",
407                         GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
408                         NULL, set_property) },
409         { GDBUS_METHOD("GetTechnologies",
410                         NULL, GDBUS_ARGS({ "technologies", "a(oa{sv})" }),
411                         get_technologies) },
412         { GDBUS_DEPRECATED_METHOD("RemoveProvider",
413                         GDBUS_ARGS({ "provider", "o" }), NULL,
414                         remove_provider) },
415         { GDBUS_METHOD("GetServices",
416                         NULL, GDBUS_ARGS({ "services", "a(oa{sv})" }),
417                         get_services) },
418         { GDBUS_DEPRECATED_ASYNC_METHOD("ConnectProvider",
419                               GDBUS_ARGS({ "provider", "a{sv}" }),
420                               GDBUS_ARGS({ "path", "o" }),
421                               connect_provider) },
422         { GDBUS_METHOD("RegisterAgent",
423                         GDBUS_ARGS({ "path", "o" }), NULL,
424                         register_agent) },
425         { GDBUS_METHOD("UnregisterAgent",
426                         GDBUS_ARGS({ "path", "o" }), NULL,
427                         unregister_agent) },
428         { GDBUS_METHOD("RegisterCounter",
429                         GDBUS_ARGS({ "path", "o" }, { "accuracy", "u" },
430                                         { "period", "u" }),
431                         NULL, register_counter) },
432         { GDBUS_METHOD("UnregisterCounter",
433                         GDBUS_ARGS({ "path", "o" }), NULL,
434                         unregister_counter) },
435         { GDBUS_ASYNC_METHOD("CreateSession",
436                         GDBUS_ARGS({ "settings", "a{sv}" },
437                                                 { "notifier", "o" }),
438                         GDBUS_ARGS({ "session", "o" }),
439                         create_session) },
440         { GDBUS_METHOD("DestroySession",
441                         GDBUS_ARGS({ "session", "o" }), NULL,
442                         destroy_session) },
443         { GDBUS_ASYNC_METHOD("RequestPrivateNetwork",
444                               NULL, GDBUS_ARGS({ "path", "o" },
445                                                { "settings", "a{sv}" },
446                                                { "socket", "h" }),
447                               request_private_network) },
448         { GDBUS_METHOD("ReleasePrivateNetwork",
449                         GDBUS_ARGS({ "path", "o" }), NULL,
450                         release_private_network) },
451         { },
452 };
453
454 static const GDBusSignalTable manager_signals[] = {
455         { GDBUS_SIGNAL("PropertyChanged",
456                         GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
457         { GDBUS_SIGNAL("TechnologyAdded",
458                         GDBUS_ARGS({ "path", "o" },
459                                    { "properties", "a{sv}" })) },
460         { GDBUS_SIGNAL("TechnologyRemoved",
461                         GDBUS_ARGS({ "path", "o" })) },
462         { GDBUS_SIGNAL("ServicesChanged",
463                         GDBUS_ARGS({ "changed", "a(oa{sv})" },
464                                         { "removed", "ao" })) },
465         { },
466 };
467
468 int __connman_manager_init(void)
469 {
470         DBG("");
471
472         connection = connman_dbus_get_connection();
473         if (connection == NULL)
474                 return -1;
475
476         if (connman_notifier_register(&technology_notifier) < 0)
477                 connman_error("Failed to register technology notifier");
478
479         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
480                                         CONNMAN_MANAGER_INTERFACE,
481                                         manager_methods,
482                                         manager_signals, NULL, NULL, NULL);
483
484         connman_state_idle = TRUE;
485
486         return 0;
487 }
488
489 void __connman_manager_cleanup(void)
490 {
491         DBG("");
492
493         if (connection == NULL)
494                 return;
495
496         if (session_mode_pending != NULL)
497                 dbus_message_unref(session_mode_pending);
498
499         connman_notifier_unregister(&technology_notifier);
500
501         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
502                                                 CONNMAN_MANAGER_INTERFACE);
503
504         dbus_connection_unref(connection);
505 }