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
31 #include <connman/plugin.h>
32 #include <connman/device.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);
134 const char *path = connman_device_get_path(adapter);
136 DBG("adapter %p", adapter);
138 return change_powered(data->connection, path, TRUE);
141 static int bluetooth_disable(struct connman_device *adapter)
143 struct adapter_data *data = connman_device_get_data(adapter);
144 const char *path = connman_device_get_path(adapter);
146 DBG("adapter %p", adapter);
148 return change_powered(data->connection, path, 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 *adapter_list = NULL;
170 static void free_adapters(void)
176 for (list = adapter_list; list; list = list->next) {
177 struct connman_device *adapter = list->data;
179 connman_device_unregister(adapter);
180 connman_device_unref(adapter);
183 g_slist_free(adapter_list);
187 static struct connman_device *find_adapter(const char *path)
191 DBG("path %s", path);
193 for (list = adapter_list; list; list = list->next) {
194 struct connman_device *adapter = list->data;
195 const char *adapter_path = connman_device_get_path(adapter);
197 if (adapter_path == NULL)
200 if (g_str_equal(adapter_path, path) == TRUE)
207 static void check_devices(struct connman_device *adapter,
208 DBusMessageIter *array)
210 DBusMessageIter value;
212 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
215 dbus_message_iter_recurse(array, &value);
217 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
220 dbus_message_iter_get_basic(&value, &path);
222 DBG("device %s", path);
224 dbus_message_iter_next(&value);
228 static void property_changed(DBusConnection *connection, DBusMessage *message)
230 const char *path = dbus_message_get_path(message);
231 struct connman_device *adapter;
232 DBusMessageIter iter, value;
235 DBG("path %s", path);
237 adapter = find_adapter(path);
241 if (dbus_message_iter_init(message, &iter) == FALSE)
244 dbus_message_iter_get_basic(&iter, &key);
246 dbus_message_iter_next(&iter);
247 dbus_message_iter_recurse(&iter, &value);
249 if (g_str_equal(key, "Powered") == TRUE) {
252 dbus_message_iter_get_basic(&value, &val);
253 connman_device_set_powered(adapter, val);
254 } else if (g_str_equal(key, "Discovering") == TRUE) {
257 dbus_message_iter_get_basic(&value, &val);
258 connman_device_set_scanning(adapter, val);
262 static void properties_reply(DBusPendingCall *call, void *user_data)
264 DBusMessage *message = user_data;
265 const char *path = dbus_message_get_path(message);
266 struct connman_device *adapter;
267 DBusMessageIter array, dict;
270 DBG("path %s", path);
272 adapter = find_adapter(path);
274 dbus_message_unref(message);
276 reply = dbus_pending_call_steal_reply(call);
281 if (dbus_message_iter_init(reply, &array) == FALSE)
284 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
287 dbus_message_iter_recurse(&array, &dict);
288 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
289 DBusMessageIter entry, value;
292 dbus_message_iter_recurse(&dict, &entry);
293 dbus_message_iter_get_basic(&entry, &key);
295 dbus_message_iter_next(&entry);
296 dbus_message_iter_recurse(&entry, &value);
298 if (g_str_equal(key, "Powered") == TRUE) {
301 dbus_message_iter_get_basic(&value, &val);
302 connman_device_set_powered(adapter, val);
303 } else if (g_str_equal(key, "Discovering") == TRUE) {
306 dbus_message_iter_get_basic(&value, &val);
307 connman_device_set_scanning(adapter, val);
308 } else if (g_str_equal(key, "Devices") == TRUE) {
309 check_devices(adapter, &value);
312 dbus_message_iter_next(&dict);
316 dbus_message_unref(reply);
319 static void add_adapter(DBusConnection *connection, const char *path)
321 const char *node = g_basename(path);
322 struct connman_device *adapter;
323 DBusMessage *message;
324 DBusPendingCall *call;
326 DBG("path %s", path);
328 adapter = find_adapter(path);
332 adapter = connman_device_create(node, CONNMAN_DEVICE_TYPE_BLUETOOTH);
334 connman_device_set_path(adapter, path);
336 if (g_str_has_prefix(node, "hci") == TRUE) {
339 index = atoi(node + 3);
341 connman_device_set_index(adapter, index);
344 connman_device_set_interface(adapter, node);
346 connman_device_set_mode(adapter, CONNMAN_DEVICE_MODE_MULTIPLE_NETWORKS);
348 if (connman_device_register(adapter) < 0) {
349 connman_device_unref(adapter);
353 adapter_list = g_slist_append(adapter_list, adapter);
355 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
356 BLUEZ_ADAPTER_INTERFACE, GET_PROPERTIES);
360 if (dbus_connection_send_with_reply(connection, message,
361 &call, TIMEOUT) == FALSE) {
362 connman_error("Failed to get adapter properties");
363 dbus_message_unref(message);
367 dbus_pending_call_set_notify(call, properties_reply, message, NULL);
370 static void remove_adapter(DBusConnection *connection, const char *path)
372 struct connman_device *adapter;
374 DBG("path %s", path);
376 adapter = find_adapter(path);
380 adapter_list = g_slist_remove(adapter_list, adapter);
382 connman_device_unregister(adapter);
383 connman_device_unref(adapter);
386 static void adapters_reply(DBusPendingCall *call, void *user_data)
388 DBusConnection *connection = user_data;
396 reply = dbus_pending_call_steal_reply(call);
398 dbus_error_init(&error);
400 if (dbus_message_get_args(reply, &error,
401 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
402 &adapters, &num_adapters,
403 DBUS_TYPE_INVALID) == FALSE) {
404 if (dbus_error_is_set(&error) == TRUE) {
405 connman_error("%s", error.message);
406 dbus_error_free(&error);
408 connman_error("Wrong arguments for adapter list");
412 for (i = 0; i < num_adapters; i++)
413 add_adapter(connection, adapters[i]);
415 g_strfreev(adapters);
418 dbus_message_unref(reply);
421 static void bluetooth_connect(DBusConnection *connection, void *user_data)
423 DBusMessage *message;
424 DBusPendingCall *call;
426 DBG("connection %p", connection);
428 message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
429 BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
433 if (dbus_connection_send_with_reply(connection, message,
434 &call, TIMEOUT) == FALSE) {
435 connman_error("Failed to get Bluetooth adapters");
436 dbus_message_unref(message);
440 dbus_pending_call_set_notify(call, adapters_reply, connection, NULL);
442 dbus_message_unref(message);
445 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
447 DBG("connection %p", connection);
452 static DBusHandlerResult bluetooth_signal(DBusConnection *connection,
453 DBusMessage *message, void *user_data)
455 if (dbus_message_has_interface(message,
456 BLUEZ_MANAGER_INTERFACE) == FALSE &&
457 dbus_message_has_interface(message,
458 BLUEZ_ADAPTER_INTERFACE) == FALSE)
459 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
461 DBG("connection %p", connection);
463 if (dbus_message_is_signal(message, BLUEZ_ADAPTER_INTERFACE,
464 PROPERTY_CHANGED) == TRUE) {
465 property_changed(connection, message);
466 } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
467 ADAPTER_ADDED) == TRUE) {
469 dbus_message_get_args(message, NULL,
470 DBUS_TYPE_OBJECT_PATH, &path,
472 add_adapter(connection, path);
473 } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
474 ADAPTER_REMOVED) == TRUE) {
476 dbus_message_get_args(message, NULL,
477 DBUS_TYPE_OBJECT_PATH, &path,
479 remove_adapter(connection, path);
482 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
485 static DBusConnection *connection;
488 static const char *added_rule = "type=signal,member=" ADAPTER_ADDED
489 ",interface=" BLUEZ_MANAGER_INTERFACE;
490 static const char *removed_rule = "type=signal,member=" ADAPTER_REMOVED
491 ",interface=" BLUEZ_MANAGER_INTERFACE;
493 static const char *adapter_rule = "type=signal,member=" PROPERTY_CHANGED
494 ",interface=" BLUEZ_ADAPTER_INTERFACE;
496 static int bluetooth_init(void)
500 connection = connman_dbus_get_connection();
501 if (connection == NULL)
504 if (dbus_connection_add_filter(connection, bluetooth_signal,
505 NULL, NULL) == FALSE)
508 err = connman_device_driver_register(&bluetooth_driver);
512 watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
513 bluetooth_connect, bluetooth_disconnect, NULL, NULL);
515 connman_device_driver_unregister(&bluetooth_driver);
520 if (g_dbus_check_service(connection, BLUEZ_SERVICE) == TRUE)
521 bluetooth_connect(connection, NULL);
523 dbus_bus_add_match(connection, added_rule, NULL);
524 dbus_bus_add_match(connection, removed_rule, NULL);
525 dbus_bus_add_match(connection, adapter_rule, NULL);
526 dbus_connection_flush(connection);
531 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
534 dbus_connection_unref(connection);
539 static void bluetooth_exit(void)
541 dbus_bus_remove_match(connection, adapter_rule, NULL);
542 dbus_bus_remove_match(connection, removed_rule, NULL);
543 dbus_bus_remove_match(connection, added_rule, NULL);
544 dbus_connection_flush(connection);
546 g_dbus_remove_watch(connection, watch);
550 connman_device_driver_unregister(&bluetooth_driver);
552 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
554 dbus_connection_unref(connection);
557 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
558 bluetooth_init, bluetooth_exit)