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