5 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
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.
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.
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
30 #include <connman/plugin.h>
31 #include <connman/driver.h>
32 #include <connman/log.h>
34 #define BLUEZ_SERVICE "org.bluez"
35 #define BLUEZ_MANAGER_INTERFACE BLUEZ_SERVICE ".Manager"
36 #define BLUEZ_ADAPTER_INTERFACE BLUEZ_SERVICE ".Adapter"
38 #define ADAPTER_ADDED "AdapterAdded"
39 #define ADAPTER_REMOVED "AdapterRemoved"
40 #define PROPERTY_CHANGED "PropertyChanged"
44 static int bluetooth_probe(struct connman_element *device)
46 DBG("device %p name %s", device, device->name);
51 static void bluetooth_remove(struct connman_element *device)
53 DBG("device %p name %s", device, device->name);
56 static int bluetooth_enable(struct connman_element *device)
58 DBG("device %p name %s", device, device->name);
63 static int bluetooth_disable(struct connman_element *device)
65 DBG("device %p name %s", device, device->name);
70 static struct connman_driver bluetooth_driver = {
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,
80 static GSList *device_list = NULL;
82 static struct connman_element *find_adapter(const char *path)
84 const char *devname = g_basename(path);
89 for (list = device_list; list; list = list->next) {
90 struct connman_element *device = list->data;
92 if (g_str_equal(device->devname, devname) == TRUE)
99 static void property_changed(DBusConnection *connection, DBusMessage *message)
101 const char *path = dbus_message_get_path(message);
102 struct connman_element *device;
103 DBusMessageIter iter, value;
106 DBG("path %s", path);
108 device = find_adapter(path);
112 if (dbus_message_iter_init(message, &iter) == FALSE)
115 dbus_message_iter_get_basic(&iter, &key);
117 dbus_message_iter_next(&iter);
118 dbus_message_iter_recurse(&iter, &value);
120 if (g_str_equal(key, "Powered") == TRUE) {
123 dbus_message_iter_get_basic(&value, &val);
124 connman_element_set_enabled(device, val);
125 } else if (g_str_equal(key, "Discovering") == TRUE) {
128 dbus_message_iter_get_basic(&value, &val);
129 connman_element_set_scanning(device, val);
133 static void properties_reply(DBusPendingCall *call, void *user_data)
135 DBusMessage *message = user_data;
136 const char *path = dbus_message_get_path(message);
137 struct connman_element *device;
138 DBusMessageIter array, dict;
141 DBG("path %s", path);
143 device = find_adapter(path);
145 dbus_message_unref(message);
147 reply = dbus_pending_call_steal_reply(call);
152 if (dbus_message_iter_init(reply, &array) == FALSE)
155 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
158 dbus_message_iter_recurse(&array, &dict);
159 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
160 DBusMessageIter entry, value;
163 dbus_message_iter_recurse(&dict, &entry);
164 dbus_message_iter_get_basic(&entry, &key);
166 dbus_message_iter_next(&entry);
167 dbus_message_iter_recurse(&entry, &value);
169 if (g_str_equal(key, "Powered") == TRUE) {
172 dbus_message_iter_get_basic(&value, &val);
173 connman_element_set_enabled(device, val);
174 } else if (g_str_equal(key, "Discovering") == TRUE) {
177 dbus_message_iter_get_basic(&value, &val);
178 connman_element_set_scanning(device, val);
181 dbus_message_iter_next(&dict);
185 dbus_message_unref(reply);
188 static void devices_reply(DBusPendingCall *call, void *user_data)
190 DBusMessage *message = user_data;
191 const char *path = dbus_message_get_path(message);
197 DBG("path %s", path);
199 dbus_message_unref(message);
201 reply = dbus_pending_call_steal_reply(call);
203 dbus_error_init(&error);
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);
213 connman_error("Wrong arguments for device list");
217 for (i = 0; i < num_devices; i++) {
218 DBG("device %s", devices[i]);
224 dbus_message_unref(reply);
227 static void add_adapter(DBusConnection *connection, const char *path)
229 struct connman_element *device;
230 DBusMessage *message;
231 DBusPendingCall *call;
233 DBG("path %s", path);
235 device = find_adapter(path);
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;
244 device->name = g_path_get_basename(path);
246 connman_element_register(device, NULL);
247 device_list = g_slist_append(device_list, device);
249 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
250 BLUEZ_ADAPTER_INTERFACE, "GetProperties");
254 if (dbus_connection_send_with_reply(connection, message,
255 &call, TIMEOUT) == FALSE) {
256 connman_error("Failed to get adapter properties");
257 dbus_message_unref(message);
261 dbus_pending_call_set_notify(call, properties_reply, message, NULL);
263 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
264 BLUEZ_ADAPTER_INTERFACE, "ListDevices");
268 if (dbus_connection_send_with_reply(connection, message,
269 &call, TIMEOUT) == FALSE) {
270 connman_error("Failed to get Bluetooth devices");
271 dbus_message_unref(message);
275 dbus_pending_call_set_notify(call, devices_reply, message, NULL);
278 static void remove_adapter(DBusConnection *connection, const char *path)
280 struct connman_element *device;
282 DBG("path %s", path);
284 device = find_adapter(path);
288 device_list = g_slist_remove(device_list, device);
290 connman_element_unregister(device);
291 connman_element_unref(device);
294 static void adapters_reply(DBusPendingCall *call, void *user_data)
296 DBusConnection *connection = user_data;
304 reply = dbus_pending_call_steal_reply(call);
306 dbus_error_init(&error);
308 if (dbus_message_get_args(reply, &error,
309 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
310 &adapters, &num_adapters,
311 DBUS_TYPE_INVALID) == FALSE) {
312 if (dbus_error_is_set(&error) == TRUE) {
313 connman_error("%s", error.message);
314 dbus_error_free(&error);
316 connman_error("Wrong arguments for adapter list");
320 for (i = 0; i < num_adapters; i++)
321 add_adapter(connection, adapters[i]);
323 g_strfreev(adapters);
326 dbus_message_unref(reply);
329 static void bluetooth_connect(DBusConnection *connection, void *user_data)
331 DBusMessage *message;
332 DBusPendingCall *call;
334 DBG("connection %p", connection);
336 message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
337 BLUEZ_MANAGER_INTERFACE, "ListAdapters");
341 if (dbus_connection_send_with_reply(connection, message,
342 &call, TIMEOUT) == FALSE) {
343 connman_error("Failed to get Bluetooth adapters");
344 dbus_message_unref(message);
348 dbus_pending_call_set_notify(call, adapters_reply, connection, NULL);
350 dbus_message_unref(message);
353 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
357 DBG("connection %p", connection);
359 for (list = device_list; list; list = list->next) {
360 struct connman_element *device = list->data;
362 connman_element_unregister(device);
363 connman_element_unref(device);
366 g_slist_free(device_list);
370 static DBusHandlerResult bluetooth_signal(DBusConnection *connection,
371 DBusMessage *message, void *user_data)
373 DBG("connection %p", connection);
375 if (dbus_message_is_signal(message, BLUEZ_ADAPTER_INTERFACE,
376 PROPERTY_CHANGED) == TRUE) {
377 property_changed(connection, message);
378 } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
379 ADAPTER_ADDED) == TRUE) {
381 dbus_message_get_args(message, NULL,
382 DBUS_TYPE_OBJECT_PATH, &path,
384 add_adapter(connection, path);
385 } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
386 ADAPTER_REMOVED) == TRUE) {
388 dbus_message_get_args(message, NULL,
389 DBUS_TYPE_OBJECT_PATH, &path,
391 remove_adapter(connection, path);
394 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
397 static DBusConnection *connection;
400 static const char *added_rule = "type=signal,member=" ADAPTER_ADDED
401 ",interface=" BLUEZ_MANAGER_INTERFACE;
402 static const char *removed_rule = "type=signal,member=" ADAPTER_REMOVED
403 ",interface=" BLUEZ_MANAGER_INTERFACE;
405 static const char *adapter_rule = "type=signal,member=" PROPERTY_CHANGED
406 ",interface=" BLUEZ_ADAPTER_INTERFACE;
408 static int bluetooth_init(void)
412 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
413 if (connection == NULL)
416 if (dbus_connection_add_filter(connection, bluetooth_signal,
417 NULL, NULL) == FALSE)
420 err = connman_driver_register(&bluetooth_driver);
424 watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
425 bluetooth_connect, bluetooth_disconnect, NULL, NULL);
427 connman_driver_unregister(&bluetooth_driver);
432 if (g_dbus_check_service(connection, BLUEZ_SERVICE) == TRUE)
433 bluetooth_connect(connection, NULL);
435 dbus_bus_add_match(connection, added_rule, NULL);
436 dbus_bus_add_match(connection, removed_rule, NULL);
437 dbus_bus_add_match(connection, adapter_rule, NULL);
438 dbus_connection_flush(connection);
443 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
446 dbus_connection_unref(connection);
451 static void bluetooth_exit(void)
453 dbus_bus_remove_match(connection, adapter_rule, NULL);
454 dbus_bus_remove_match(connection, removed_rule, NULL);
455 dbus_bus_remove_match(connection, added_rule, NULL);
456 dbus_connection_flush(connection);
458 g_dbus_remove_watch(connection, watch);
460 bluetooth_disconnect(connection, NULL);
462 connman_driver_unregister(&bluetooth_driver);
464 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
466 dbus_connection_unref(connection);
469 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
470 bluetooth_init, bluetooth_exit)