Use ignore policy as default for Bluetooth
[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 add_adapter(DBusConnection *connection, const char *path)
179 {
180         struct connman_element *device;
181         DBusMessage *message;
182         DBusPendingCall *call;
183
184         DBG("path %s", path);
185
186         device = find_adapter(path);
187         if (device != NULL)
188                 return;
189
190         device = connman_element_create(NULL);
191         device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
192         device->subtype = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH;
193         device->policy = CONNMAN_ELEMENT_POLICY_IGNORE;
194
195         device->name = g_path_get_basename(path);
196
197         connman_element_register(device, NULL);
198         device_list = g_slist_append(device_list, device);
199
200         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
201                                 BLUEZ_ADAPTER_INTERFACE, "GetProperties");
202         if (message == NULL)
203                 return;
204
205         if (dbus_connection_send_with_reply(connection, message,
206                                                 &call, TIMEOUT) == FALSE) {
207                 connman_error("Failed to get adapter properties");
208                 dbus_message_unref(message);
209                 return;
210         }
211
212         dbus_pending_call_set_notify(call, properties_reply, message, NULL);
213 }
214
215 static void remove_adapter(DBusConnection *connection, const char *path)
216 {
217         struct connman_element *device;
218
219         DBG("path %s", path);
220
221         device = find_adapter(path);
222         if (device == NULL)
223                 return;
224
225         device_list = g_slist_remove(device_list, device);
226
227         connman_element_unregister(device);
228         connman_element_unref(device);
229 }
230
231 static void adapters_reply(DBusPendingCall *call, void *user_data)
232 {
233         DBusConnection *connection = user_data;
234         DBusMessage *reply;
235         DBusError error;
236         char **adapters;
237         int i, num_adapters;
238
239         DBG("");
240
241         reply = dbus_pending_call_steal_reply(call);
242
243         dbus_error_init(&error);
244
245         if (dbus_message_get_args(reply, &error,
246                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
247                                                 &adapters, &num_adapters,
248                                                 DBUS_TYPE_INVALID) == FALSE) {
249                 if (dbus_error_is_set(&error) == TRUE) {
250                         connman_error("%s", error.message);
251                         dbus_error_free(&error);
252                 } else
253                         connman_error("Wrong arguments for adapter list");
254                 goto done;
255         }
256
257         for (i = 0; i < num_adapters; i++)
258                 add_adapter(connection, adapters[i]);
259
260         g_strfreev(adapters);
261
262 done:
263         dbus_message_unref(reply);
264 }
265
266 static void bluetooth_connect(DBusConnection *connection, void *user_data)
267 {
268         DBusMessage *message;
269         DBusPendingCall *call;
270
271         DBG("connection %p", connection);
272
273         message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
274                                 BLUEZ_MANAGER_INTERFACE, "ListAdapters");
275         if (message == NULL)
276                 return;
277
278         if (dbus_connection_send_with_reply(connection, message,
279                                                 &call, TIMEOUT) == FALSE) {
280                 connman_error("Failed to get Bluetooth adapters");
281                 dbus_message_unref(message);
282                 return;
283         }
284
285         dbus_pending_call_set_notify(call, adapters_reply, connection, NULL);
286
287         dbus_message_unref(message);
288 }
289
290 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
291 {
292         GSList *list;
293
294         DBG("connection %p", connection);
295
296         for (list = device_list; list; list = list->next) {
297                 struct connman_element *device = list->data;
298
299                 connman_element_unregister(device);
300                 connman_element_unref(device);
301         }
302
303         g_slist_free(device_list);
304         device_list = NULL;
305 }
306
307 static DBusHandlerResult bluetooth_signal(DBusConnection *connection,
308                                         DBusMessage *message, void *user_data)
309 {
310         DBG("connection %p", connection);
311
312         if (dbus_message_is_signal(message, BLUEZ_ADAPTER_INTERFACE,
313                                                 PROPERTY_CHANGED) == TRUE) {
314                 property_changed(connection, message);
315         } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
316                                                 ADAPTER_ADDED) == TRUE) {
317                 const char *path;
318                 dbus_message_get_args(message, NULL,
319                                         DBUS_TYPE_OBJECT_PATH, &path,
320                                                         DBUS_TYPE_INVALID);
321                 add_adapter(connection, path);
322         } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
323                                                 ADAPTER_REMOVED) == TRUE) {
324                 const char *path;
325                 dbus_message_get_args(message, NULL,
326                                         DBUS_TYPE_OBJECT_PATH, &path,
327                                                         DBUS_TYPE_INVALID);
328                 remove_adapter(connection, path);
329         }
330
331         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
332 }
333
334 static DBusConnection *connection;
335 static guint watch;
336
337 static const char *added_rule = "type=signal,member=" ADAPTER_ADDED
338                                         ",interface=" BLUEZ_MANAGER_INTERFACE;
339 static const char *removed_rule = "type=signal,member=" ADAPTER_REMOVED
340                                         ",interface=" BLUEZ_MANAGER_INTERFACE;
341
342 static const char *adapter_rule = "type=signal,member=" PROPERTY_CHANGED
343                                         ",interface=" BLUEZ_ADAPTER_INTERFACE;
344
345 static int bluetooth_init(void)
346 {
347         int err = -EIO;
348
349         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
350         if (connection == NULL)
351                 return -EIO;
352
353         if (dbus_connection_add_filter(connection, bluetooth_signal,
354                                                         NULL, NULL) == FALSE)
355                 goto unref;
356
357         err = connman_driver_register(&bluetooth_driver);
358         if (err < 0)
359                 goto remove;
360
361         watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
362                         bluetooth_connect, bluetooth_disconnect, NULL, NULL);
363         if (watch == 0) {
364                 connman_driver_unregister(&bluetooth_driver);
365                 err = -EIO;
366                 goto remove;
367         }
368
369         if (g_dbus_check_service(connection, BLUEZ_SERVICE) == TRUE)
370                 bluetooth_connect(connection, NULL);
371
372         dbus_bus_add_match(connection, added_rule, NULL);
373         dbus_bus_add_match(connection, removed_rule, NULL);
374         dbus_bus_add_match(connection, adapter_rule, NULL);
375         dbus_connection_flush(connection);
376
377         return 0;
378
379 remove:
380         dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
381
382 unref:
383         dbus_connection_unref(connection);
384
385         return err;
386 }
387
388 static void bluetooth_exit(void)
389 {
390         dbus_bus_remove_match(connection, adapter_rule, NULL);
391         dbus_bus_remove_match(connection, removed_rule, NULL);
392         dbus_bus_remove_match(connection, added_rule, NULL);
393         dbus_connection_flush(connection);
394
395         g_dbus_remove_watch(connection, watch);
396
397         bluetooth_disconnect(connection, NULL);
398
399         connman_driver_unregister(&bluetooth_driver);
400
401         dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
402
403         dbus_connection_unref(connection);
404 }
405
406 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
407                                                 bluetooth_init, bluetooth_exit)