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/device.h>
32 #include <connman/driver.h>
33 #include <connman/dbus.h>
34 #include <connman/log.h>
36 #define BLUEZ_SERVICE "org.bluez"
37 #define BLUEZ_MANAGER_INTERFACE BLUEZ_SERVICE ".Manager"
38 #define BLUEZ_ADAPTER_INTERFACE BLUEZ_SERVICE ".Adapter"
40 #define LIST_ADAPTERS "ListAdapters"
41 #define ADAPTER_ADDED "AdapterAdded"
42 #define ADAPTER_REMOVED "AdapterRemoved"
44 #define PROPERTY_CHANGED "PropertyChanged"
45 #define GET_PROPERTIES "GetProperties"
46 #define SET_PROPERTY "SetProperty"
51 DBusConnection *connection;
54 static int bluetooth_probe(struct connman_device *adapter)
56 struct adapter_data *data;
58 DBG("adapter %p", adapter);
60 data = g_try_new0(struct adapter_data, 1);
64 data->connection = connman_dbus_get_connection();
65 if (data->connection == NULL) {
70 connman_device_set_data(adapter, data);
75 static void bluetooth_remove(struct connman_device *adapter)
77 struct adapter_data *data = connman_device_get_data(adapter);
79 DBG("adapter %p", adapter);
81 connman_device_set_data(adapter, NULL);
83 dbus_connection_unref(data->connection);
88 static void powered_reply(DBusPendingCall *call, void *user_data)
94 reply = dbus_pending_call_steal_reply(call);
96 dbus_message_unref(reply);
99 static int change_powered(DBusConnection *connection, const char *path,
102 DBusMessage *message;
103 DBusMessageIter iter;
104 DBusPendingCall *call;
108 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
109 BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY);
113 dbus_message_iter_init_append(message, &iter);
114 connman_dbus_property_append_variant(&iter, "Powered",
115 DBUS_TYPE_BOOLEAN, &powered);
117 if (dbus_connection_send_with_reply(connection, message,
118 &call, TIMEOUT) == FALSE) {
119 connman_error("Failed to change Powered property");
120 dbus_message_unref(message);
124 dbus_pending_call_set_notify(call, powered_reply, NULL, NULL);
126 dbus_message_unref(message);
131 static int bluetooth_enable(struct connman_device *adapter)
133 struct adapter_data *data = connman_device_get_data(adapter);
135 DBG("adapter %p", adapter);
137 return change_powered(data->connection,
138 adapter->element->devpath, TRUE);
141 static int bluetooth_disable(struct connman_device *adapter)
143 struct adapter_data *data = connman_device_get_data(adapter);
145 DBG("adapter %p", adapter);
147 return change_powered(data->connection,
148 adapter->element->devpath, FALSE);
151 static int bluetooth_scan(struct connman_device *adapter)
153 DBG("adapter %p", adapter);
158 static struct connman_device_driver bluetooth_driver = {
160 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
161 .probe = bluetooth_probe,
162 .remove = bluetooth_remove,
163 .enable = bluetooth_enable,
164 .disable = bluetooth_disable,
165 .scan = bluetooth_scan,
168 static GSList *device_list = NULL;
170 static struct connman_element *find_adapter(const char *path)
174 DBG("path %s", path);
176 for (list = device_list; list; list = list->next) {
177 struct connman_element *device = list->data;
179 if (g_str_equal(device->devpath, path) == TRUE)
186 static void check_devices(struct connman_element *adapter,
187 DBusMessageIter *array)
189 DBusMessageIter value;
191 DBG("adapter %p name %s", adapter, adapter->name);
193 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
196 dbus_message_iter_recurse(array, &value);
198 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
201 dbus_message_iter_get_basic(&value, &path);
203 DBG("device %s", path);
205 dbus_message_iter_next(&value);
209 static void property_changed(DBusConnection *connection, DBusMessage *message)
211 const char *path = dbus_message_get_path(message);
212 struct connman_element *device;
213 DBusMessageIter iter, value;
216 DBG("path %s", path);
218 device = find_adapter(path);
222 if (dbus_message_iter_init(message, &iter) == FALSE)
225 dbus_message_iter_get_basic(&iter, &key);
227 dbus_message_iter_next(&iter);
228 dbus_message_iter_recurse(&iter, &value);
230 if (g_str_equal(key, "Powered") == TRUE) {
231 struct connman_device *dev = connman_element_get_data(device);
234 dbus_message_iter_get_basic(&value, &val);
236 connman_device_set_powered(dev, val);
237 } else if (g_str_equal(key, "Discovering") == TRUE) {
238 struct connman_device *dev = connman_element_get_data(device);
241 dbus_message_iter_get_basic(&value, &val);
243 connman_device_set_scanning(dev, val);
247 static void properties_reply(DBusPendingCall *call, void *user_data)
249 DBusMessage *message = user_data;
250 const char *path = dbus_message_get_path(message);
251 struct connman_element *device;
252 DBusMessageIter array, dict;
255 DBG("path %s", path);
257 device = find_adapter(path);
259 dbus_message_unref(message);
261 reply = dbus_pending_call_steal_reply(call);
266 if (dbus_message_iter_init(reply, &array) == FALSE)
269 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
272 dbus_message_iter_recurse(&array, &dict);
273 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
274 struct connman_device *dev = connman_element_get_data(device);
275 DBusMessageIter entry, value;
278 dbus_message_iter_recurse(&dict, &entry);
279 dbus_message_iter_get_basic(&entry, &key);
281 dbus_message_iter_next(&entry);
282 dbus_message_iter_recurse(&entry, &value);
284 if (g_str_equal(key, "Powered") == TRUE) {
287 dbus_message_iter_get_basic(&value, &val);
289 connman_device_set_powered(dev, val);
290 } else if (g_str_equal(key, "Discovering") == TRUE) {
293 dbus_message_iter_get_basic(&value, &val);
295 connman_device_set_scanning(dev, val);
296 } else if (g_str_equal(key, "Devices") == TRUE) {
297 check_devices(device, &value);
300 dbus_message_iter_next(&dict);
304 dbus_message_unref(reply);
307 static void add_adapter(DBusConnection *connection, const char *path)
309 struct connman_element *device;
310 DBusMessage *message;
311 DBusPendingCall *call;
313 DBG("path %s", path);
315 device = find_adapter(path);
319 device = connman_element_create(NULL);
320 device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
321 device->subtype = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH;
322 device->policy = CONNMAN_ELEMENT_POLICY_IGNORE;
324 device->name = g_path_get_basename(path);
325 device->devpath = g_strdup(path);
327 if (connman_element_register(device, NULL) < 0) {
328 connman_element_unref(device);
332 device_list = g_slist_append(device_list, device);
334 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
335 BLUEZ_ADAPTER_INTERFACE, GET_PROPERTIES);
339 if (dbus_connection_send_with_reply(connection, message,
340 &call, TIMEOUT) == FALSE) {
341 connman_error("Failed to get adapter properties");
342 dbus_message_unref(message);
346 dbus_pending_call_set_notify(call, properties_reply, message, NULL);
349 static void remove_adapter(DBusConnection *connection, const char *path)
351 struct connman_element *device;
353 DBG("path %s", path);
355 device = find_adapter(path);
359 device_list = g_slist_remove(device_list, device);
361 connman_element_unregister(device);
362 connman_element_unref(device);
365 static void adapters_reply(DBusPendingCall *call, void *user_data)
367 DBusConnection *connection = user_data;
375 reply = dbus_pending_call_steal_reply(call);
377 dbus_error_init(&error);
379 if (dbus_message_get_args(reply, &error,
380 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
381 &adapters, &num_adapters,
382 DBUS_TYPE_INVALID) == FALSE) {
383 if (dbus_error_is_set(&error) == TRUE) {
384 connman_error("%s", error.message);
385 dbus_error_free(&error);
387 connman_error("Wrong arguments for adapter list");
391 for (i = 0; i < num_adapters; i++)
392 add_adapter(connection, adapters[i]);
394 g_strfreev(adapters);
397 dbus_message_unref(reply);
400 static void bluetooth_connect(DBusConnection *connection, void *user_data)
402 DBusMessage *message;
403 DBusPendingCall *call;
405 DBG("connection %p", connection);
407 message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
408 BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
412 if (dbus_connection_send_with_reply(connection, message,
413 &call, TIMEOUT) == FALSE) {
414 connman_error("Failed to get Bluetooth adapters");
415 dbus_message_unref(message);
419 dbus_pending_call_set_notify(call, adapters_reply, connection, NULL);
421 dbus_message_unref(message);
424 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
428 DBG("connection %p", connection);
430 for (list = device_list; list; list = list->next) {
431 struct connman_element *device = list->data;
433 connman_element_unregister(device);
434 connman_element_unref(device);
437 g_slist_free(device_list);
441 static DBusHandlerResult bluetooth_signal(DBusConnection *connection,
442 DBusMessage *message, void *user_data)
444 DBG("connection %p", connection);
446 if (dbus_message_is_signal(message, BLUEZ_ADAPTER_INTERFACE,
447 PROPERTY_CHANGED) == TRUE) {
448 property_changed(connection, message);
449 } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
450 ADAPTER_ADDED) == TRUE) {
452 dbus_message_get_args(message, NULL,
453 DBUS_TYPE_OBJECT_PATH, &path,
455 add_adapter(connection, path);
456 } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
457 ADAPTER_REMOVED) == TRUE) {
459 dbus_message_get_args(message, NULL,
460 DBUS_TYPE_OBJECT_PATH, &path,
462 remove_adapter(connection, path);
465 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
468 static DBusConnection *connection;
471 static const char *added_rule = "type=signal,member=" ADAPTER_ADDED
472 ",interface=" BLUEZ_MANAGER_INTERFACE;
473 static const char *removed_rule = "type=signal,member=" ADAPTER_REMOVED
474 ",interface=" BLUEZ_MANAGER_INTERFACE;
476 static const char *adapter_rule = "type=signal,member=" PROPERTY_CHANGED
477 ",interface=" BLUEZ_ADAPTER_INTERFACE;
479 static int bluetooth_init(void)
483 connection = connman_dbus_get_connection();
484 if (connection == NULL)
487 if (dbus_connection_add_filter(connection, bluetooth_signal,
488 NULL, NULL) == FALSE)
491 err = connman_device_driver_register(&bluetooth_driver);
495 watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
496 bluetooth_connect, bluetooth_disconnect, NULL, NULL);
498 connman_device_driver_unregister(&bluetooth_driver);
503 if (g_dbus_check_service(connection, BLUEZ_SERVICE) == TRUE)
504 bluetooth_connect(connection, NULL);
506 dbus_bus_add_match(connection, added_rule, NULL);
507 dbus_bus_add_match(connection, removed_rule, NULL);
508 dbus_bus_add_match(connection, adapter_rule, NULL);
509 dbus_connection_flush(connection);
514 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
517 dbus_connection_unref(connection);
522 static void bluetooth_exit(void)
524 dbus_bus_remove_match(connection, adapter_rule, NULL);
525 dbus_bus_remove_match(connection, removed_rule, NULL);
526 dbus_bus_remove_match(connection, added_rule, NULL);
527 dbus_connection_flush(connection);
529 g_dbus_remove_watch(connection, watch);
531 bluetooth_disconnect(connection, NULL);
533 connman_device_driver_unregister(&bluetooth_driver);
535 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
537 dbus_connection_unref(connection);
540 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
541 bluetooth_init, bluetooth_exit)