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