Add support for Technologies and EnabledTechnologies properties
[platform/upstream/connman.git] / src / manager.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 <gdbus.h>
27
28 #include "connman.h"
29
30 static connman_bool_t global_offlinemode = FALSE;
31
32 static void append_profiles(DBusMessageIter *dict)
33 {
34         DBusMessageIter entry, value, iter;
35         const char *key = "Profiles";
36
37         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
38                                                                 NULL, &entry);
39
40         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
41
42         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
43                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
44                                                                 &value);
45
46         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
47                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
48         __connman_profile_list(&iter);
49         dbus_message_iter_close_container(&value, &iter);
50
51         dbus_message_iter_close_container(&entry, &value);
52
53         dbus_message_iter_close_container(dict, &entry);
54 }
55
56 static void append_services(DBusMessageIter *dict)
57 {
58         DBusMessageIter entry, value, iter;
59         const char *key = "Services";
60
61         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
62                                                                 NULL, &entry);
63
64         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
65
66         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
67                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
68                                                                 &value);
69
70         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
71                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
72         __connman_service_list(&iter);
73         dbus_message_iter_close_container(&value, &iter);
74
75         dbus_message_iter_close_container(&entry, &value);
76
77         dbus_message_iter_close_container(dict, &entry);
78 }
79
80 static void append_devices(DBusMessageIter *dict)
81 {
82         DBusMessageIter entry, value, iter;
83         const char *key = "Devices";
84
85         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
86                                                                 NULL, &entry);
87
88         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
89
90         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
91                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
92                                                                 &value);
93
94         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
95                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
96         __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
97         dbus_message_iter_close_container(&value, &iter);
98
99         dbus_message_iter_close_container(&entry, &value);
100
101         dbus_message_iter_close_container(dict, &entry);
102 }
103
104 static void append_connections(DBusMessageIter *dict)
105 {
106         DBusMessageIter entry, value, iter;
107         const char *key = "Connections";
108
109         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
110                                                                 NULL, &entry);
111
112         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
113
114         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
115                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
116                                                                 &value);
117
118         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
119                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
120         __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION, &iter);
121         dbus_message_iter_close_container(&value, &iter);
122
123         dbus_message_iter_close_container(&entry, &value);
124
125         dbus_message_iter_close_container(dict, &entry);
126 }
127
128 static void append_technologies(gboolean powered, DBusMessageIter *dict)
129 {
130         DBusMessageIter entry, value, iter;
131         const char *key;
132
133         if (powered)
134                 key = "EnabledTechnologies";
135         else
136                 key = "Technologies";
137
138         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
139                                                                 NULL, &entry);
140
141         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
142
143         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
144                         DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
145                                                                 &value);
146
147         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
148                                         DBUS_TYPE_STRING_AS_STRING, &iter);
149         __connman_notifier_device_type_list(powered, &iter);
150         dbus_message_iter_close_container(&value, &iter);
151
152         dbus_message_iter_close_container(&entry, &value);
153
154         dbus_message_iter_close_container(dict, &entry);
155 }
156
157 static DBusMessage *get_properties(DBusConnection *conn,
158                                         DBusMessage *msg, void *data)
159 {
160         DBusMessage *reply;
161         DBusMessageIter array, dict;
162         const char *str;
163
164         DBG("conn %p", conn);
165
166         if (__connman_security_check_privilege(msg,
167                                         CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
168                 return __connman_error_permission_denied(msg);
169
170         reply = dbus_message_new_method_return(msg);
171         if (reply == NULL)
172                 return NULL;
173
174         dbus_message_iter_init_append(reply, &array);
175
176         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
177                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
178                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
179                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
180
181         str = __connman_profile_active_path();
182         if (str != NULL)
183                 connman_dbus_dict_append_variant(&dict, "ActiveProfile",
184                                                 DBUS_TYPE_OBJECT_PATH, &str);
185
186         append_profiles(&dict);
187         append_services(&dict);
188
189         append_devices(&dict);
190         append_connections(&dict);
191
192         if (__connman_element_count(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION) > 0)
193                 str = "online";
194         else
195                 str = "offline";
196
197         connman_dbus_dict_append_variant(&dict, "State",
198                                                 DBUS_TYPE_STRING, &str);
199
200         connman_dbus_dict_append_variant(&dict, "OfflineMode",
201                                 DBUS_TYPE_BOOLEAN, &global_offlinemode);
202
203         append_technologies(FALSE, &dict);
204         append_technologies(TRUE, &dict);
205
206         dbus_message_iter_close_container(&array, &dict);
207
208         return reply;
209 }
210
211 static DBusMessage *set_property(DBusConnection *conn,
212                                         DBusMessage *msg, void *data)
213 {
214         DBusMessageIter iter, value;
215         const char *name;
216
217         DBG("conn %p", conn);
218
219         if (dbus_message_iter_init(msg, &iter) == FALSE)
220                 return __connman_error_invalid_arguments(msg);
221
222         dbus_message_iter_get_basic(&iter, &name);
223         dbus_message_iter_next(&iter);
224         dbus_message_iter_recurse(&iter, &value);
225
226         if (__connman_security_check_privilege(msg,
227                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
228                 return __connman_error_permission_denied(msg);
229
230         if (g_str_equal(name, "OfflineMode") == TRUE) {
231                 connman_bool_t offlinemode;
232
233                 dbus_message_iter_get_basic(&value, &offlinemode);
234
235                 if (global_offlinemode == offlinemode)
236                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
237
238                 global_offlinemode = offlinemode;
239
240                 __connman_device_set_offlinemode(offlinemode);
241         } else if (g_str_equal(name, "ActiveProfile") == TRUE) {
242                 const char *str;
243
244                 dbus_message_iter_get_basic(&value, &str);
245
246                 return __connman_error_not_supported(msg);
247         } else
248                 return __connman_error_invalid_property(msg);
249
250         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
251 }
252
253 static DBusMessage *add_profile(DBusConnection *conn,
254                                         DBusMessage *msg, void *data)
255 {
256         const char *name;
257
258         DBG("conn %p", conn);
259
260         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
261                                                         DBUS_TYPE_INVALID);
262
263         return __connman_error_not_supported(msg);
264 }
265
266 static DBusMessage *remove_profile(DBusConnection *conn,
267                                         DBusMessage *msg, void *data)
268 {
269         const char *path;
270
271         DBG("conn %p", conn);
272
273         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
274                                                         DBUS_TYPE_INVALID);
275
276         return __connman_error_not_supported(msg);
277 }
278
279 static DBusMessage *request_scan(DBusConnection *conn,
280                                         DBusMessage *msg, void *data)
281 {
282         enum connman_device_type type;
283         const char *str;
284         int err;
285
286         DBG("conn %p", conn);
287
288         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
289                                                         DBUS_TYPE_INVALID);
290
291         if (g_strcmp0(str, "") == 0)
292                 type = CONNMAN_DEVICE_TYPE_UNKNOWN;
293         else if (g_strcmp0(str, "wifi") == 0)
294                 type = CONNMAN_DEVICE_TYPE_WIFI;
295         else if (g_strcmp0(str, "wimax") == 0)
296                 type = CONNMAN_DEVICE_TYPE_WIMAX;
297         else
298                 return __connman_error_invalid_arguments(msg);
299
300         err = __connman_element_request_scan(type);
301         if (err < 0) {
302                 if (err == -EINPROGRESS) {
303                         connman_error("Invalid return code from scan");
304                         err = -EINVAL;
305                 }
306
307                 return __connman_error_failed(msg, -err);
308         }
309
310         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
311 }
312
313 static DBusMessage *connect_service(DBusConnection *conn,
314                                         DBusMessage *msg, void *data)
315 {
316         int err;
317
318         DBG("conn %p", conn);
319
320         if (__connman_security_check_privilege(msg,
321                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
322                 return __connman_error_permission_denied(msg);
323
324         err = __connman_service_create_and_connect(msg);
325         if (err < 0) {
326                 if (err == -EINPROGRESS) {
327                         connman_error("Invalid return code from connect");
328                         err = -EINVAL;
329                 }
330
331                 return __connman_error_failed(msg, -err);
332         }
333
334         return NULL;
335 }
336
337 static DBusMessage *register_agent(DBusConnection *conn,
338                                         DBusMessage *msg, void *data)
339 {
340         DBusMessage *reply;
341         const char *sender, *path;
342
343         DBG("conn %p", conn);
344
345         sender = dbus_message_get_sender(msg);
346
347         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
348                                                         DBUS_TYPE_INVALID);
349
350         reply = dbus_message_new_method_return(msg);
351         if (reply == NULL)
352                 return NULL;
353
354         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
355
356         __connman_agent_register(sender, path);
357
358         return reply;
359 }
360
361 static DBusMessage *unregister_agent(DBusConnection *conn,
362                                         DBusMessage *msg, void *data)
363 {
364         DBusMessage *reply;
365         const char *sender, *path;
366
367         DBG("conn %p", conn);
368
369         sender = dbus_message_get_sender(msg);
370
371         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
372                                                         DBUS_TYPE_INVALID);
373
374         reply = dbus_message_new_method_return(msg);
375         if (reply == NULL)
376                 return NULL;
377
378         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
379
380         __connman_agent_unregister(sender, path);
381
382         return reply;
383 }
384
385 static GDBusMethodTable manager_methods[] = {
386         { "GetProperties",   "",      "a{sv}", get_properties   },
387         { "SetProperty",     "sv",    "",      set_property     },
388         { "AddProfile",      "s",     "o",     add_profile      },
389         { "RemoveProfile",   "o",     "",      remove_profile   },
390         { "RequestScan",     "s",     "",      request_scan     },
391         { "ConnectService",  "a{sv}", "o",     connect_service,
392                                                 G_DBUS_METHOD_FLAG_ASYNC },
393         { "RegisterAgent",   "o",     "",      register_agent   },
394         { "UnregisterAgent", "o",     "",      unregister_agent },
395         { },
396 };
397
398 static GDBusSignalTable manager_signals[] = {
399         { "PropertyChanged", "sv" },
400         { "StateChanged",    "s"  },
401         { },
402 };
403
404 static DBusMessage *nm_sleep(DBusConnection *conn,
405                                         DBusMessage *msg, void *data)
406 {
407         DBusMessage *reply;
408
409         DBG("conn %p", conn);
410
411         reply = dbus_message_new_method_return(msg);
412         if (reply == NULL)
413                 return NULL;
414
415         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
416
417         return reply;
418 }
419
420 static DBusMessage *nm_wake(DBusConnection *conn,
421                                         DBusMessage *msg, void *data)
422 {
423         DBusMessage *reply;
424
425         DBG("conn %p", conn);
426
427         reply = dbus_message_new_method_return(msg);
428         if (reply == NULL)
429                 return NULL;
430
431         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
432
433         return reply;
434 }
435
436 enum {
437         NM_STATE_UNKNOWN = 0,
438         NM_STATE_ASLEEP,
439         NM_STATE_CONNECTING,
440         NM_STATE_CONNECTED,
441         NM_STATE_DISCONNECTED
442 };
443
444 static DBusMessage *nm_state(DBusConnection *conn,
445                                         DBusMessage *msg, void *data)
446 {
447         DBusMessage *reply;
448         dbus_uint32_t state;
449
450         DBG("conn %p", conn);
451
452         reply = dbus_message_new_method_return(msg);
453         if (reply == NULL)
454                 return NULL;
455
456         state = NM_STATE_DISCONNECTED;
457
458         dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
459                                                         DBUS_TYPE_INVALID);
460
461         return reply;
462 }
463
464 static GDBusMethodTable nm_methods[] = {
465         { "sleep", "",  "",   nm_sleep        },
466         { "wake",  "",  "",   nm_wake         },
467         { "state", "",  "u",  nm_state        },
468         { },
469 };
470
471 static DBusConnection *connection = NULL;
472 static gboolean nm_compat = FALSE;
473
474 int __connman_manager_init(DBusConnection *conn, gboolean compat)
475 {
476         DBG("conn %p", conn);
477
478         connection = dbus_connection_ref(conn);
479         if (connection == NULL)
480                 return -1;
481
482         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
483                                         CONNMAN_MANAGER_INTERFACE,
484                                         manager_methods,
485                                         manager_signals, NULL, NULL, NULL);
486
487         if (compat == TRUE) {
488                 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
489                                         nm_methods, NULL, NULL, NULL, NULL);
490
491                 nm_compat = TRUE;
492         }
493
494         return 0;
495 }
496
497 void __connman_manager_cleanup(void)
498 {
499         DBG("conn %p", connection);
500
501         if (nm_compat == TRUE) {
502                 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
503         }
504
505         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
506                                                 CONNMAN_MANAGER_INTERFACE);
507
508         dbus_connection_unref(connection);
509 }