Iterate over the Bluetooth Devices property
[connman] / plugins / bluetooth.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  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/plugin.h>
31 #include <connman/driver.h>
32 #include <connman/dbus.h>
33 #include <connman/log.h>
34
35 #define BLUEZ_SERVICE                   "org.bluez"
36 #define BLUEZ_MANAGER_INTERFACE         BLUEZ_SERVICE ".Manager"
37 #define BLUEZ_ADAPTER_INTERFACE         BLUEZ_SERVICE ".Adapter"
38
39 #define LIST_ADAPTERS                   "ListAdapters"
40 #define ADAPTER_ADDED                   "AdapterAdded"
41 #define ADAPTER_REMOVED                 "AdapterRemoved"
42
43 #define PROPERTY_CHANGED                "PropertyChanged"
44 #define GET_PROPERTIES                  "GetProperties"
45 #define SET_PROPERTY                    "SetProperty"
46
47 #define TIMEOUT 5000
48
49 struct adapter_data {
50         DBusConnection *connection;
51 };
52
53 static int bluetooth_probe(struct connman_element *adapter)
54 {
55         struct adapter_data *data;
56
57         DBG("adapter %p name %s", adapter, adapter->name);
58
59         data = g_try_new0(struct adapter_data, 1);
60         if (data == NULL)
61                 return -ENOMEM;
62
63         data->connection = connman_dbus_get_connection();
64         if (data->connection == NULL) {
65                 g_free(data);
66                 return -EIO;
67         }
68
69         connman_element_set_data(adapter, data);
70
71         return 0;
72 }
73
74 static void bluetooth_remove(struct connman_element *adapter)
75 {
76         struct adapter_data *data = connman_element_get_data(adapter);
77
78         DBG("adapter %p name %s", adapter, adapter->name);
79
80         connman_element_set_data(adapter, NULL);
81
82         dbus_connection_unref(data->connection);
83
84         g_free(data);
85 }
86
87 static void powered_reply(DBusPendingCall *call, void *user_data)
88 {
89         DBusMessage *reply;
90
91         DBG("");
92
93         reply = dbus_pending_call_steal_reply(call);
94
95         dbus_message_unref(reply);
96 }
97
98 static int change_powered(DBusConnection *connection, const char *path,
99                                                         dbus_bool_t powered)
100 {
101         DBusMessage *message;
102         DBusMessageIter iter;
103         DBusPendingCall *call;
104
105         DBG("");
106
107         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
108                                         BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY);
109         if (message == NULL)
110                 return -ENOMEM;
111
112         dbus_message_iter_init_append(message, &iter);
113         connman_dbus_property_append_variant(&iter, "Powered",
114                                                 DBUS_TYPE_BOOLEAN, &powered);
115
116         if (dbus_connection_send_with_reply(connection, message,
117                                                 &call, TIMEOUT) == FALSE) {
118                 connman_error("Failed to change Powered property");
119                 dbus_message_unref(message);
120                 return -EINVAL;
121         }
122
123         dbus_pending_call_set_notify(call, powered_reply, NULL, NULL);
124
125         dbus_message_unref(message);
126
127         return -EINPROGRESS;
128 }
129
130 static int bluetooth_enable(struct connman_element *adapter)
131 {
132         struct adapter_data *data = connman_element_get_data(adapter);
133
134         DBG("adapter %p name %s", adapter, adapter->name);
135
136         return change_powered(data->connection, adapter->devpath, TRUE);
137 }
138
139 static int bluetooth_disable(struct connman_element *adapter)
140 {
141         struct adapter_data *data = connman_element_get_data(adapter);
142
143         DBG("adapter %p name %s", adapter, adapter->name);
144
145         return change_powered(data->connection, adapter->devpath, FALSE);
146 }
147
148 static struct connman_driver bluetooth_driver = {
149         .name           = "bluetooth",
150         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
151         .subtype        = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH,
152         .probe          = bluetooth_probe,
153         .remove         = bluetooth_remove,
154         .enable         = bluetooth_enable,
155         .disable        = bluetooth_disable,
156 };
157
158 static GSList *device_list = NULL;
159
160 static struct connman_element *find_adapter(const char *path)
161 {
162         GSList *list;
163
164         DBG("path %s", path);
165
166         for (list = device_list; list; list = list->next) {
167                 struct connman_element *device = list->data;
168
169                 if (g_str_equal(device->devpath, path) == TRUE)
170                         return device;
171         }
172
173         return NULL;
174 }
175
176 static void check_devices(struct connman_element *adapter,
177                                                 DBusMessageIter *array)
178 {
179         DBusMessageIter value;
180
181         DBG("adapter %p name %s", adapter, adapter->name);
182
183         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
184                 return;
185
186         dbus_message_iter_recurse(array, &value);
187
188         while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
189                 const char *path;
190
191                 dbus_message_iter_get_basic(&value, &path);
192
193                 DBG("device %s", path);
194
195                 dbus_message_iter_next(&value);
196         }
197 }
198
199 static void property_changed(DBusConnection *connection, DBusMessage *message)
200 {
201         const char *path = dbus_message_get_path(message);
202         struct connman_element *device;
203         DBusMessageIter iter, value;
204         const char *key;
205
206         DBG("path %s", path);
207
208         device = find_adapter(path);
209         if (device == NULL)
210                 return;
211
212         if (dbus_message_iter_init(message, &iter) == FALSE)
213                 return;
214
215         dbus_message_iter_get_basic(&iter, &key);
216
217         dbus_message_iter_next(&iter);
218         dbus_message_iter_recurse(&iter, &value);
219
220         if (g_str_equal(key, "Powered") == TRUE) {
221                 gboolean val;
222
223                 dbus_message_iter_get_basic(&value, &val);
224                 connman_element_set_enabled(device, val);
225         } else if (g_str_equal(key, "Discovering") == TRUE) {
226                 gboolean val;
227
228                 dbus_message_iter_get_basic(&value, &val);
229                 connman_element_set_scanning(device, val);
230         }
231 }
232
233 static void properties_reply(DBusPendingCall *call, void *user_data)
234 {
235         DBusMessage *message = user_data;
236         const char *path = dbus_message_get_path(message);
237         struct connman_element *device;
238         DBusMessageIter array, dict;
239         DBusMessage *reply;
240
241         DBG("path %s", path);
242
243         device = find_adapter(path);
244
245         dbus_message_unref(message);
246
247         reply = dbus_pending_call_steal_reply(call);
248
249         if (device == NULL)
250                 goto done;
251
252         if (dbus_message_iter_init(reply, &array) == FALSE)
253                 goto done;
254
255         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
256                 goto done;
257
258         dbus_message_iter_recurse(&array, &dict);
259         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
260                 DBusMessageIter entry, value;
261                 const char *key;
262
263                 dbus_message_iter_recurse(&dict, &entry);
264                 dbus_message_iter_get_basic(&entry, &key);
265
266                 dbus_message_iter_next(&entry);
267                 dbus_message_iter_recurse(&entry, &value);
268
269                 if (g_str_equal(key, "Powered") == TRUE) {
270                         gboolean val;
271
272                         dbus_message_iter_get_basic(&value, &val);
273                         connman_element_set_enabled(device, val);
274                 } else if (g_str_equal(key, "Discovering") == TRUE) {
275                         gboolean val;
276
277                         dbus_message_iter_get_basic(&value, &val);
278                         connman_element_set_scanning(device, val);
279                 } else if (g_str_equal(key, "Devices") == TRUE) {
280                         check_devices(device, &value);
281                 }
282
283                 dbus_message_iter_next(&dict);
284         }
285
286 done:
287         dbus_message_unref(reply);
288 }
289
290 static void add_adapter(DBusConnection *connection, const char *path)
291 {
292         struct connman_element *device;
293         DBusMessage *message;
294         DBusPendingCall *call;
295
296         DBG("path %s", path);
297
298         device = find_adapter(path);
299         if (device != NULL)
300                 return;
301
302         device = connman_element_create(NULL);
303         device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
304         device->subtype = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH;
305         device->policy = CONNMAN_ELEMENT_POLICY_IGNORE;
306
307         device->name = g_path_get_basename(path);
308         device->devpath = g_strdup(path);
309
310         if (connman_element_register(device, NULL) < 0) {
311                 connman_element_unref(device);
312                 return;
313         }
314
315         device_list = g_slist_append(device_list, device);
316
317         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
318                                 BLUEZ_ADAPTER_INTERFACE, GET_PROPERTIES);
319         if (message == NULL)
320                 return;
321
322         if (dbus_connection_send_with_reply(connection, message,
323                                                 &call, TIMEOUT) == FALSE) {
324                 connman_error("Failed to get adapter properties");
325                 dbus_message_unref(message);
326                 return;
327         }
328
329         dbus_pending_call_set_notify(call, properties_reply, message, NULL);
330 }
331
332 static void remove_adapter(DBusConnection *connection, const char *path)
333 {
334         struct connman_element *device;
335
336         DBG("path %s", path);
337
338         device = find_adapter(path);
339         if (device == NULL)
340                 return;
341
342         device_list = g_slist_remove(device_list, device);
343
344         connman_element_unregister(device);
345         connman_element_unref(device);
346 }
347
348 static void adapters_reply(DBusPendingCall *call, void *user_data)
349 {
350         DBusConnection *connection = user_data;
351         DBusMessage *reply;
352         DBusError error;
353         char **adapters;
354         int i, num_adapters;
355
356         DBG("");
357
358         reply = dbus_pending_call_steal_reply(call);
359
360         dbus_error_init(&error);
361
362         if (dbus_message_get_args(reply, &error,
363                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
364                                                 &adapters, &num_adapters,
365                                                 DBUS_TYPE_INVALID) == FALSE) {
366                 if (dbus_error_is_set(&error) == TRUE) {
367                         connman_error("%s", error.message);
368                         dbus_error_free(&error);
369                 } else
370                         connman_error("Wrong arguments for adapter list");
371                 goto done;
372         }
373
374         for (i = 0; i < num_adapters; i++)
375                 add_adapter(connection, adapters[i]);
376
377         g_strfreev(adapters);
378
379 done:
380         dbus_message_unref(reply);
381 }
382
383 static void bluetooth_connect(DBusConnection *connection, void *user_data)
384 {
385         DBusMessage *message;
386         DBusPendingCall *call;
387
388         DBG("connection %p", connection);
389
390         message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
391                                 BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
392         if (message == NULL)
393                 return;
394
395         if (dbus_connection_send_with_reply(connection, message,
396                                                 &call, TIMEOUT) == FALSE) {
397                 connman_error("Failed to get Bluetooth adapters");
398                 dbus_message_unref(message);
399                 return;
400         }
401
402         dbus_pending_call_set_notify(call, adapters_reply, connection, NULL);
403
404         dbus_message_unref(message);
405 }
406
407 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
408 {
409         GSList *list;
410
411         DBG("connection %p", connection);
412
413         for (list = device_list; list; list = list->next) {
414                 struct connman_element *device = list->data;
415
416                 connman_element_unregister(device);
417                 connman_element_unref(device);
418         }
419
420         g_slist_free(device_list);
421         device_list = NULL;
422 }
423
424 static DBusHandlerResult bluetooth_signal(DBusConnection *connection,
425                                         DBusMessage *message, void *user_data)
426 {
427         DBG("connection %p", connection);
428
429         if (dbus_message_is_signal(message, BLUEZ_ADAPTER_INTERFACE,
430                                                 PROPERTY_CHANGED) == TRUE) {
431                 property_changed(connection, message);
432         } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
433                                                 ADAPTER_ADDED) == TRUE) {
434                 const char *path;
435                 dbus_message_get_args(message, NULL,
436                                         DBUS_TYPE_OBJECT_PATH, &path,
437                                                         DBUS_TYPE_INVALID);
438                 add_adapter(connection, path);
439         } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
440                                                 ADAPTER_REMOVED) == TRUE) {
441                 const char *path;
442                 dbus_message_get_args(message, NULL,
443                                         DBUS_TYPE_OBJECT_PATH, &path,
444                                                         DBUS_TYPE_INVALID);
445                 remove_adapter(connection, path);
446         }
447
448         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
449 }
450
451 static DBusConnection *connection;
452 static guint watch;
453
454 static const char *added_rule = "type=signal,member=" ADAPTER_ADDED
455                                         ",interface=" BLUEZ_MANAGER_INTERFACE;
456 static const char *removed_rule = "type=signal,member=" ADAPTER_REMOVED
457                                         ",interface=" BLUEZ_MANAGER_INTERFACE;
458
459 static const char *adapter_rule = "type=signal,member=" PROPERTY_CHANGED
460                                         ",interface=" BLUEZ_ADAPTER_INTERFACE;
461
462 static int bluetooth_init(void)
463 {
464         int err = -EIO;
465
466         connection = connman_dbus_get_connection();
467         if (connection == NULL)
468                 return -EIO;
469
470         if (dbus_connection_add_filter(connection, bluetooth_signal,
471                                                         NULL, NULL) == FALSE)
472                 goto unref;
473
474         err = connman_driver_register(&bluetooth_driver);
475         if (err < 0)
476                 goto remove;
477
478         watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
479                         bluetooth_connect, bluetooth_disconnect, NULL, NULL);
480         if (watch == 0) {
481                 connman_driver_unregister(&bluetooth_driver);
482                 err = -EIO;
483                 goto remove;
484         }
485
486         if (g_dbus_check_service(connection, BLUEZ_SERVICE) == TRUE)
487                 bluetooth_connect(connection, NULL);
488
489         dbus_bus_add_match(connection, added_rule, NULL);
490         dbus_bus_add_match(connection, removed_rule, NULL);
491         dbus_bus_add_match(connection, adapter_rule, NULL);
492         dbus_connection_flush(connection);
493
494         return 0;
495
496 remove:
497         dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
498
499 unref:
500         dbus_connection_unref(connection);
501
502         return err;
503 }
504
505 static void bluetooth_exit(void)
506 {
507         dbus_bus_remove_match(connection, adapter_rule, NULL);
508         dbus_bus_remove_match(connection, removed_rule, NULL);
509         dbus_bus_remove_match(connection, added_rule, NULL);
510         dbus_connection_flush(connection);
511
512         g_dbus_remove_watch(connection, watch);
513
514         bluetooth_disconnect(connection, NULL);
515
516         connman_driver_unregister(&bluetooth_driver);
517
518         dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
519
520         dbus_connection_unref(connection);
521 }
522
523 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
524                                                 bluetooth_init, bluetooth_exit)