Handle failure of device registration
[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         dbus_message_iter_next(&iter);
118         dbus_message_iter_recurse(&iter, &value);
119
120         if (g_str_equal(key, "Powered") == TRUE) {
121                 gboolean val;
122
123                 dbus_message_iter_get_basic(&value, &val);
124                 connman_element_set_enabled(device, val);
125         } else if (g_str_equal(key, "Discovering") == TRUE) {
126                 gboolean val;
127
128                 dbus_message_iter_get_basic(&value, &val);
129                 connman_element_set_scanning(device, val);
130         }
131 }
132
133 static void properties_reply(DBusPendingCall *call, void *user_data)
134 {
135         DBusMessage *message = user_data;
136         const char *path = dbus_message_get_path(message);
137         struct connman_element *device;
138         DBusMessageIter array, dict;
139         DBusMessage *reply;
140
141         DBG("path %s", path);
142
143         device = find_adapter(path);
144
145         dbus_message_unref(message);
146
147         reply = dbus_pending_call_steal_reply(call);
148
149         if (device == NULL)
150                 goto done;
151
152         if (dbus_message_iter_init(reply, &array) == FALSE)
153                 goto done;
154
155         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
156                 goto done;
157
158         dbus_message_iter_recurse(&array, &dict);
159         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
160                 DBusMessageIter entry, value;
161                 const char *key;
162
163                 dbus_message_iter_recurse(&dict, &entry);
164                 dbus_message_iter_get_basic(&entry, &key);
165
166                 dbus_message_iter_next(&entry);
167                 dbus_message_iter_recurse(&entry, &value);
168
169                 if (g_str_equal(key, "Powered") == TRUE) {
170                         gboolean val;
171
172                         dbus_message_iter_get_basic(&value, &val);
173                         connman_element_set_enabled(device, val);
174                 } else if (g_str_equal(key, "Discovering") == TRUE) {
175                         gboolean val;
176
177                         dbus_message_iter_get_basic(&value, &val);
178                         connman_element_set_scanning(device, val);
179                 }
180
181                 dbus_message_iter_next(&dict);
182         }
183
184 done:
185         dbus_message_unref(reply);
186 }
187
188 static void devices_reply(DBusPendingCall *call, void *user_data)
189 {
190         DBusMessage *message = user_data;
191         const char *path = dbus_message_get_path(message);
192         DBusMessage *reply;
193         DBusError error;
194         char **devices;
195         int i, num_devices;
196
197         DBG("path %s", path);
198
199         dbus_message_unref(message);
200
201         reply = dbus_pending_call_steal_reply(call);
202
203         dbus_error_init(&error);
204
205         if (dbus_message_get_args(reply, &error,
206                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
207                                                 &devices, &num_devices,
208                                                 DBUS_TYPE_INVALID) == FALSE) {
209                 if (dbus_error_is_set(&error) == TRUE) {
210                         connman_error("%s", error.message);
211                         dbus_error_free(&error);
212                 } else
213                         connman_error("Wrong arguments for device list");
214                 goto done;
215         }
216
217         for (i = 0; i < num_devices; i++) {
218                 DBG("device %s", devices[i]);
219         }
220
221         g_strfreev(devices);
222
223 done:
224         dbus_message_unref(reply);
225 }
226
227 static void add_adapter(DBusConnection *connection, const char *path)
228 {
229         struct connman_element *device;
230         DBusMessage *message;
231         DBusPendingCall *call;
232
233         DBG("path %s", path);
234
235         device = find_adapter(path);
236         if (device != NULL)
237                 return;
238
239         device = connman_element_create(NULL);
240         device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
241         device->subtype = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH;
242         device->policy = CONNMAN_ELEMENT_POLICY_IGNORE;
243
244         device->name = g_path_get_basename(path);
245
246         if (connman_element_register(device, NULL) < 0) {
247                 connman_element_unref(device);
248                 return;
249         }
250
251         device_list = g_slist_append(device_list, device);
252
253         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
254                                 BLUEZ_ADAPTER_INTERFACE, "GetProperties");
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 adapter properties");
261                 dbus_message_unref(message);
262                 return;
263         }
264
265         dbus_pending_call_set_notify(call, properties_reply, message, NULL);
266
267         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
268                                 BLUEZ_ADAPTER_INTERFACE, "ListDevices");
269         if (message == NULL)
270                 return;
271
272         if (dbus_connection_send_with_reply(connection, message,
273                                                 &call, TIMEOUT) == FALSE) {
274                 connman_error("Failed to get Bluetooth devices");
275                 dbus_message_unref(message);
276                 return;
277         }
278
279         dbus_pending_call_set_notify(call, devices_reply, message, NULL);
280 }
281
282 static void remove_adapter(DBusConnection *connection, const char *path)
283 {
284         struct connman_element *device;
285
286         DBG("path %s", path);
287
288         device = find_adapter(path);
289         if (device == NULL)
290                 return;
291
292         device_list = g_slist_remove(device_list, device);
293
294         connman_element_unregister(device);
295         connman_element_unref(device);
296 }
297
298 static void adapters_reply(DBusPendingCall *call, void *user_data)
299 {
300         DBusConnection *connection = user_data;
301         DBusMessage *reply;
302         DBusError error;
303         char **adapters;
304         int i, num_adapters;
305
306         DBG("");
307
308         reply = dbus_pending_call_steal_reply(call);
309
310         dbus_error_init(&error);
311
312         if (dbus_message_get_args(reply, &error,
313                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
314                                                 &adapters, &num_adapters,
315                                                 DBUS_TYPE_INVALID) == FALSE) {
316                 if (dbus_error_is_set(&error) == TRUE) {
317                         connman_error("%s", error.message);
318                         dbus_error_free(&error);
319                 } else
320                         connman_error("Wrong arguments for adapter list");
321                 goto done;
322         }
323
324         for (i = 0; i < num_adapters; i++)
325                 add_adapter(connection, adapters[i]);
326
327         g_strfreev(adapters);
328
329 done:
330         dbus_message_unref(reply);
331 }
332
333 static void bluetooth_connect(DBusConnection *connection, void *user_data)
334 {
335         DBusMessage *message;
336         DBusPendingCall *call;
337
338         DBG("connection %p", connection);
339
340         message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
341                                 BLUEZ_MANAGER_INTERFACE, "ListAdapters");
342         if (message == NULL)
343                 return;
344
345         if (dbus_connection_send_with_reply(connection, message,
346                                                 &call, TIMEOUT) == FALSE) {
347                 connman_error("Failed to get Bluetooth adapters");
348                 dbus_message_unref(message);
349                 return;
350         }
351
352         dbus_pending_call_set_notify(call, adapters_reply, connection, NULL);
353
354         dbus_message_unref(message);
355 }
356
357 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
358 {
359         GSList *list;
360
361         DBG("connection %p", connection);
362
363         for (list = device_list; list; list = list->next) {
364                 struct connman_element *device = list->data;
365
366                 connman_element_unregister(device);
367                 connman_element_unref(device);
368         }
369
370         g_slist_free(device_list);
371         device_list = NULL;
372 }
373
374 static DBusHandlerResult bluetooth_signal(DBusConnection *connection,
375                                         DBusMessage *message, void *user_data)
376 {
377         DBG("connection %p", connection);
378
379         if (dbus_message_is_signal(message, BLUEZ_ADAPTER_INTERFACE,
380                                                 PROPERTY_CHANGED) == TRUE) {
381                 property_changed(connection, message);
382         } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
383                                                 ADAPTER_ADDED) == TRUE) {
384                 const char *path;
385                 dbus_message_get_args(message, NULL,
386                                         DBUS_TYPE_OBJECT_PATH, &path,
387                                                         DBUS_TYPE_INVALID);
388                 add_adapter(connection, path);
389         } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
390                                                 ADAPTER_REMOVED) == TRUE) {
391                 const char *path;
392                 dbus_message_get_args(message, NULL,
393                                         DBUS_TYPE_OBJECT_PATH, &path,
394                                                         DBUS_TYPE_INVALID);
395                 remove_adapter(connection, path);
396         }
397
398         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
399 }
400
401 static DBusConnection *connection;
402 static guint watch;
403
404 static const char *added_rule = "type=signal,member=" ADAPTER_ADDED
405                                         ",interface=" BLUEZ_MANAGER_INTERFACE;
406 static const char *removed_rule = "type=signal,member=" ADAPTER_REMOVED
407                                         ",interface=" BLUEZ_MANAGER_INTERFACE;
408
409 static const char *adapter_rule = "type=signal,member=" PROPERTY_CHANGED
410                                         ",interface=" BLUEZ_ADAPTER_INTERFACE;
411
412 static int bluetooth_init(void)
413 {
414         int err = -EIO;
415
416         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
417         if (connection == NULL)
418                 return -EIO;
419
420         if (dbus_connection_add_filter(connection, bluetooth_signal,
421                                                         NULL, NULL) == FALSE)
422                 goto unref;
423
424         err = connman_driver_register(&bluetooth_driver);
425         if (err < 0)
426                 goto remove;
427
428         watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
429                         bluetooth_connect, bluetooth_disconnect, NULL, NULL);
430         if (watch == 0) {
431                 connman_driver_unregister(&bluetooth_driver);
432                 err = -EIO;
433                 goto remove;
434         }
435
436         if (g_dbus_check_service(connection, BLUEZ_SERVICE) == TRUE)
437                 bluetooth_connect(connection, NULL);
438
439         dbus_bus_add_match(connection, added_rule, NULL);
440         dbus_bus_add_match(connection, removed_rule, NULL);
441         dbus_bus_add_match(connection, adapter_rule, NULL);
442         dbus_connection_flush(connection);
443
444         return 0;
445
446 remove:
447         dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
448
449 unref:
450         dbus_connection_unref(connection);
451
452         return err;
453 }
454
455 static void bluetooth_exit(void)
456 {
457         dbus_bus_remove_match(connection, adapter_rule, NULL);
458         dbus_bus_remove_match(connection, removed_rule, NULL);
459         dbus_bus_remove_match(connection, added_rule, NULL);
460         dbus_connection_flush(connection);
461
462         g_dbus_remove_watch(connection, watch);
463
464         bluetooth_disconnect(connection, NULL);
465
466         connman_driver_unregister(&bluetooth_driver);
467
468         dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
469
470         dbus_connection_unref(connection);
471 }
472
473 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
474                                                 bluetooth_init, bluetooth_exit)