e56f2e13434d55f6c9686efb79d55a0da1ea53f5
[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_by_path(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                 return __connman_error_failed(msg, -err);
240
241         return NULL;
242 }
243
244 static DBusMessage *register_agent(DBusConnection *conn,
245                                         DBusMessage *msg, void *data)
246 {
247         const char *sender, *path;
248         int err;
249
250         DBG("conn %p", conn);
251
252         sender = dbus_message_get_sender(msg);
253
254         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
255                                                         DBUS_TYPE_INVALID);
256
257         err = connman_agent_register(sender, path);
258         if (err < 0)
259                 return __connman_error_failed(msg, -err);
260
261         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
262 }
263
264 static DBusMessage *unregister_agent(DBusConnection *conn,
265                                         DBusMessage *msg, void *data)
266 {
267         const char *sender, *path;
268         int err;
269
270         DBG("conn %p", conn);
271
272         sender = dbus_message_get_sender(msg);
273
274         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
275                                                         DBUS_TYPE_INVALID);
276
277         err = connman_agent_unregister(sender, path);
278         if (err < 0)
279                 return __connman_error_failed(msg, -err);
280
281         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
282 }
283
284 static DBusMessage *register_counter(DBusConnection *conn,
285                                         DBusMessage *msg, void *data)
286 {
287         const char *sender, *path;
288         unsigned int accuracy, period;
289         int err;
290
291         DBG("conn %p", conn);
292
293         sender = dbus_message_get_sender(msg);
294
295         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
296                                                 DBUS_TYPE_UINT32, &accuracy,
297                                                 DBUS_TYPE_UINT32, &period,
298                                                         DBUS_TYPE_INVALID);
299
300         /* FIXME: add handling of accuracy parameter */
301
302         err = __connman_counter_register(sender, path, period);
303         if (err < 0)
304                 return __connman_error_failed(msg, -err);
305
306         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
307 }
308
309 static DBusMessage *unregister_counter(DBusConnection *conn,
310                                         DBusMessage *msg, void *data)
311 {
312         const char *sender, *path;
313         int err;
314
315         DBG("conn %p", conn);
316
317         sender = dbus_message_get_sender(msg);
318
319         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
320                                                         DBUS_TYPE_INVALID);
321
322         err = __connman_counter_unregister(sender, path);
323         if (err < 0)
324                 return __connman_error_failed(msg, -err);
325
326         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
327 }
328
329 static DBusMessage *create_session(DBusConnection *conn,
330                                         DBusMessage *msg, void *data)
331 {
332         int err;
333
334         DBG("conn %p", conn);
335
336         err = __connman_session_create(msg);
337         if (err < 0) {
338                 if (err == -EINPROGRESS)
339                         return NULL;
340
341                 return __connman_error_failed(msg, -err);
342         }
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",
398                         NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
399                         get_properties) },
400         { GDBUS_ASYNC_METHOD("SetProperty",
401                         GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
402                         NULL, set_property) },
403         { GDBUS_METHOD("GetTechnologies",
404                         NULL, GDBUS_ARGS({ "technologies", "a(oa{sv})" }),
405                         get_technologies) },
406         { GDBUS_DEPRECATED_METHOD("RemoveProvider",
407                         GDBUS_ARGS({ "provider", "o" }), NULL,
408                         remove_provider) },
409         { GDBUS_METHOD("GetServices",
410                         NULL, GDBUS_ARGS({ "services", "a(oa{sv})" }),
411                         get_services) },
412         { GDBUS_DEPRECATED_ASYNC_METHOD("ConnectProvider",
413                               GDBUS_ARGS({ "provider", "a{sv}" }),
414                               GDBUS_ARGS({ "path", "o" }),
415                               connect_provider) },
416         { GDBUS_METHOD("RegisterAgent",
417                         GDBUS_ARGS({ "path", "o" }), NULL,
418                         register_agent) },
419         { GDBUS_METHOD("UnregisterAgent",
420                         GDBUS_ARGS({ "path", "o" }), NULL,
421                         unregister_agent) },
422         { GDBUS_METHOD("RegisterCounter",
423                         GDBUS_ARGS({ "path", "o" }, { "accuracy", "u" },
424                                         { "period", "u" }),
425                         NULL, register_counter) },
426         { GDBUS_METHOD("UnregisterCounter",
427                         GDBUS_ARGS({ "path", "o" }), NULL,
428                         unregister_counter) },
429         { GDBUS_ASYNC_METHOD("CreateSession",
430                         GDBUS_ARGS({ "settings", "a{sv}" },
431                                                 { "notifier", "o" }),
432                         GDBUS_ARGS({ "session", "o" }),
433                         create_session) },
434         { GDBUS_METHOD("DestroySession",
435                         GDBUS_ARGS({ "session", "o" }), NULL,
436                         destroy_session) },
437         { GDBUS_ASYNC_METHOD("RequestPrivateNetwork",
438                               NULL, GDBUS_ARGS({ "path", "o" },
439                                                { "settings", "a{sv}" },
440                                                { "socket", "h" }),
441                               request_private_network) },
442         { GDBUS_METHOD("ReleasePrivateNetwork",
443                         GDBUS_ARGS({ "path", "o" }), NULL,
444                         release_private_network) },
445         { },
446 };
447
448 static const GDBusSignalTable manager_signals[] = {
449         { GDBUS_SIGNAL("PropertyChanged",
450                         GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
451         { GDBUS_SIGNAL("TechnologyAdded",
452                         GDBUS_ARGS({ "path", "o" },
453                                    { "properties", "a{sv}" })) },
454         { GDBUS_SIGNAL("TechnologyRemoved",
455                         GDBUS_ARGS({ "path", "o" })) },
456         { GDBUS_SIGNAL("ServicesChanged",
457                         GDBUS_ARGS({ "changed", "a(oa{sv})" },
458                                         { "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         if (session_mode_pending != NULL)
491                 dbus_message_unref(session_mode_pending);
492
493         connman_notifier_unregister(&technology_notifier);
494
495         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
496                                                 CONNMAN_MANAGER_INTERFACE);
497
498         dbus_connection_unref(connection);
499 }