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 if (g_str_equal(key, "Powered") == TRUE) {
120 dbus_message_iter_next(&iter);
121 dbus_message_iter_recurse(&iter, &value);
123 dbus_message_iter_get_basic(&value, &val);
124 connman_element_set_enabled(device, val);
128 static void properties_reply(DBusPendingCall *call, void *user_data)
130 DBusMessage *message = user_data;
131 const char *path = dbus_message_get_path(message);
132 struct connman_element *device;
133 DBusMessageIter array, dict;
136 DBG("path %s", path);
138 device = find_adapter(path);
140 dbus_message_unref(message);
142 reply = dbus_pending_call_steal_reply(call);
147 if (dbus_message_iter_init(reply, &array) == FALSE)
150 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
153 dbus_message_iter_recurse(&array, &dict);
154 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
155 DBusMessageIter entry, value;
158 dbus_message_iter_recurse(&dict, &entry);
159 dbus_message_iter_get_basic(&entry, &key);
161 if (g_str_equal(key, "Powered") == TRUE) {
164 dbus_message_iter_next(&entry);
165 dbus_message_iter_recurse(&entry, &value);
167 dbus_message_iter_get_basic(&value, &val);
168 connman_element_set_enabled(device, val);
171 dbus_message_iter_next(&dict);
175 dbus_message_unref(reply);
178 static void devices_reply(DBusPendingCall *call, void *user_data)
180 DBusMessage *message = user_data;
181 const char *path = dbus_message_get_path(message);
187 DBG("path %s", path);
189 dbus_message_unref(message);
191 reply = dbus_pending_call_steal_reply(call);
193 dbus_error_init(&error);
195 if (dbus_message_get_args(reply, &error,
196 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
197 &devices, &num_devices,
198 DBUS_TYPE_INVALID) == FALSE) {
199 if (dbus_error_is_set(&error) == TRUE) {
200 connman_error("%s", error.message);
201 dbus_error_free(&error);
203 connman_error("Wrong arguments for device list");
207 for (i = 0; i < num_devices; i++) {
208 DBG("device %s", devices[i]);
214 dbus_message_unref(reply);
217 static void add_adapter(DBusConnection *connection, const char *path)
219 struct connman_element *device;
220 DBusMessage *message;
221 DBusPendingCall *call;
223 DBG("path %s", path);
225 device = find_adapter(path);
229 device = connman_element_create(NULL);
230 device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
231 device->subtype = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH;
232 device->policy = CONNMAN_ELEMENT_POLICY_IGNORE;
234 device->name = g_path_get_basename(path);
236 connman_element_register(device, NULL);
237 device_list = g_slist_append(device_list, device);
239 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
240 BLUEZ_ADAPTER_INTERFACE, "GetProperties");
244 if (dbus_connection_send_with_reply(connection, message,
245 &call, TIMEOUT) == FALSE) {
246 connman_error("Failed to get adapter properties");
247 dbus_message_unref(message);
251 dbus_pending_call_set_notify(call, properties_reply, message, NULL);
253 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
254 BLUEZ_ADAPTER_INTERFACE, "ListDevices");
258 if (dbus_connection_send_with_reply(connection, message,
259 &call, TIMEOUT) == FALSE) {
260 connman_error("Failed to get Bluetooth devices");
261 dbus_message_unref(message);
265 dbus_pending_call_set_notify(call, devices_reply, message, NULL);
268 static void remove_adapter(DBusConnection *connection, const char *path)
270 struct connman_element *device;
272 DBG("path %s", path);
274 device = find_adapter(path);
278 device_list = g_slist_remove(device_list, device);
280 connman_element_unregister(device);
281 connman_element_unref(device);
284 static void adapters_reply(DBusPendingCall *call, void *user_data)
286 DBusConnection *connection = user_data;
294 reply = dbus_pending_call_steal_reply(call);
296 dbus_error_init(&error);
298 if (dbus_message_get_args(reply, &error,
299 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
300 &adapters, &num_adapters,
301 DBUS_TYPE_INVALID) == FALSE) {
302 if (dbus_error_is_set(&error) == TRUE) {
303 connman_error("%s", error.message);
304 dbus_error_free(&error);
306 connman_error("Wrong arguments for adapter list");
310 for (i = 0; i < num_adapters; i++)
311 add_adapter(connection, adapters[i]);
313 g_strfreev(adapters);
316 dbus_message_unref(reply);
319 static void bluetooth_connect(DBusConnection *connection, void *user_data)
321 DBusMessage *message;
322 DBusPendingCall *call;
324 DBG("connection %p", connection);
326 message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
327 BLUEZ_MANAGER_INTERFACE, "ListAdapters");
331 if (dbus_connection_send_with_reply(connection, message,
332 &call, TIMEOUT) == FALSE) {
333 connman_error("Failed to get Bluetooth adapters");
334 dbus_message_unref(message);
338 dbus_pending_call_set_notify(call, adapters_reply, connection, NULL);
340 dbus_message_unref(message);
343 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
347 DBG("connection %p", connection);
349 for (list = device_list; list; list = list->next) {
350 struct connman_element *device = list->data;
352 connman_element_unregister(device);
353 connman_element_unref(device);
356 g_slist_free(device_list);
360 static DBusHandlerResult bluetooth_signal(DBusConnection *connection,
361 DBusMessage *message, void *user_data)
363 DBG("connection %p", connection);
365 if (dbus_message_is_signal(message, BLUEZ_ADAPTER_INTERFACE,
366 PROPERTY_CHANGED) == TRUE) {
367 property_changed(connection, message);
368 } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
369 ADAPTER_ADDED) == TRUE) {
371 dbus_message_get_args(message, NULL,
372 DBUS_TYPE_OBJECT_PATH, &path,
374 add_adapter(connection, path);
375 } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
376 ADAPTER_REMOVED) == TRUE) {
378 dbus_message_get_args(message, NULL,
379 DBUS_TYPE_OBJECT_PATH, &path,
381 remove_adapter(connection, path);
384 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
387 static DBusConnection *connection;
390 static const char *added_rule = "type=signal,member=" ADAPTER_ADDED
391 ",interface=" BLUEZ_MANAGER_INTERFACE;
392 static const char *removed_rule = "type=signal,member=" ADAPTER_REMOVED
393 ",interface=" BLUEZ_MANAGER_INTERFACE;
395 static const char *adapter_rule = "type=signal,member=" PROPERTY_CHANGED
396 ",interface=" BLUEZ_ADAPTER_INTERFACE;
398 static int bluetooth_init(void)
402 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
403 if (connection == NULL)
406 if (dbus_connection_add_filter(connection, bluetooth_signal,
407 NULL, NULL) == FALSE)
410 err = connman_driver_register(&bluetooth_driver);
414 watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
415 bluetooth_connect, bluetooth_disconnect, NULL, NULL);
417 connman_driver_unregister(&bluetooth_driver);
422 if (g_dbus_check_service(connection, BLUEZ_SERVICE) == TRUE)
423 bluetooth_connect(connection, NULL);
425 dbus_bus_add_match(connection, added_rule, NULL);
426 dbus_bus_add_match(connection, removed_rule, NULL);
427 dbus_bus_add_match(connection, adapter_rule, NULL);
428 dbus_connection_flush(connection);
433 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
436 dbus_connection_unref(connection);
441 static void bluetooth_exit(void)
443 dbus_bus_remove_match(connection, adapter_rule, NULL);
444 dbus_bus_remove_match(connection, removed_rule, NULL);
445 dbus_bus_remove_match(connection, added_rule, NULL);
446 dbus_connection_flush(connection);
448 g_dbus_remove_watch(connection, watch);
450 bluetooth_disconnect(connection, NULL);
452 connman_driver_unregister(&bluetooth_driver);
454 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
456 dbus_connection_unref(connection);
459 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
460 bluetooth_init, bluetooth_exit)