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