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