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