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/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_device *device)
46 DBG("device %p", device);
51 static void bluetooth_remove(struct connman_device *device)
53 DBG("device %p", device);
56 static struct connman_device_driver bluetooth_driver = {
58 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
59 .probe = bluetooth_probe,
60 .remove = bluetooth_remove,
63 static GSList *device_list = NULL;
65 static void property_changed(DBusMessage *msg)
70 static struct connman_element *find_adapter(const char *path)
72 const char *devname = g_basename(path);
77 for (list = device_list; list; list = list->next) {
78 struct connman_element *device = list->data;
80 if (g_str_equal(device->devname, devname) == TRUE)
87 static void add_adapter(const char *path)
89 struct connman_element *device;
93 device = find_adapter(path);
97 device = connman_element_create(NULL);
98 device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
99 device->subtype = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH;
101 device->name = g_path_get_basename(path);
103 connman_element_register(device, NULL);
104 device_list = g_slist_append(device_list, device);
107 static void remove_adapter(const char *path)
109 struct connman_element *device;
111 DBG("path %s", path);
113 device = find_adapter(path);
117 device_list = g_slist_remove(device_list, device);
119 connman_element_unregister(device);
120 connman_element_unref(device);
123 static void adapters_reply(DBusPendingCall *call, void *user_data)
132 reply = dbus_pending_call_steal_reply(call);
134 dbus_error_init(&error);
136 if (dbus_message_get_args(reply, &error,
137 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
138 &adapters, &num_adapters,
139 DBUS_TYPE_INVALID) == FALSE) {
140 if (dbus_error_is_set(&error) == TRUE) {
141 connman_error("%s", error.message);
142 dbus_error_free(&error);
144 connman_error("Wrong arguments for adapter list");
148 for (i = 0; i < num_adapters; i++)
149 add_adapter(adapters[i]);
151 g_strfreev(adapters);
154 dbus_message_unref(reply);
157 static void bluetooth_connect(DBusConnection *connection, void *user_data)
159 DBusMessage *message;
160 DBusPendingCall *call;
162 DBG("connection %p", connection);
164 message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
165 BLUEZ_MANAGER_INTERFACE, "ListAdapters");
169 if (dbus_connection_send_with_reply(connection, message,
170 &call, TIMEOUT) == FALSE) {
171 connman_error("Failed to get Bluetooth adapters");
172 dbus_message_unref(message);
176 dbus_pending_call_set_notify(call, adapters_reply, NULL, NULL);
178 dbus_message_unref(message);
181 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
185 DBG("connection %p", connection);
187 for (list = device_list; list; list = list->next) {
188 struct connman_element *device = list->data;
190 connman_element_unregister(device);
191 connman_element_unref(device);
194 g_slist_free(device_list);
198 static DBusHandlerResult bluetooth_signal(DBusConnection *conn,
199 DBusMessage *msg, void *data)
201 DBG("connection %p", conn);
203 if (dbus_message_is_signal(msg, BLUEZ_ADAPTER_INTERFACE,
204 PROPERTY_CHANGED) == TRUE) {
205 property_changed(msg);
206 } else if (dbus_message_is_signal(msg, BLUEZ_MANAGER_INTERFACE,
207 ADAPTER_ADDED) == TRUE) {
209 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
212 } else if (dbus_message_is_signal(msg, BLUEZ_MANAGER_INTERFACE,
213 ADAPTER_REMOVED) == TRUE) {
215 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
217 remove_adapter(path);
220 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
223 static DBusConnection *connection;
226 static const char *added_rule = "type=signal,member=" ADAPTER_ADDED
227 ",interface=" BLUEZ_MANAGER_INTERFACE;
228 static const char *removed_rule = "type=signal,member=" ADAPTER_REMOVED
229 ",interface=" BLUEZ_MANAGER_INTERFACE;
231 static const char *adapter_rule = "type=signal,member=" PROPERTY_CHANGED
232 ",interface=" BLUEZ_ADAPTER_INTERFACE;
234 static int bluetooth_init(void)
238 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
239 if (connection == NULL)
242 if (dbus_connection_add_filter(connection, bluetooth_signal,
243 NULL, NULL) == FALSE)
246 err = connman_device_driver_register(&bluetooth_driver);
250 watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
251 bluetooth_connect, bluetooth_disconnect, NULL, NULL);
253 connman_device_driver_unregister(&bluetooth_driver);
258 if (g_dbus_check_service(connection, BLUEZ_SERVICE) == TRUE)
259 bluetooth_connect(connection, NULL);
261 dbus_bus_add_match(connection, added_rule, NULL);
262 dbus_bus_add_match(connection, removed_rule, NULL);
263 dbus_bus_add_match(connection, adapter_rule, NULL);
264 dbus_connection_flush(connection);
269 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
272 dbus_connection_unref(connection);
277 static void bluetooth_exit(void)
279 dbus_bus_remove_match(connection, adapter_rule, NULL);
280 dbus_bus_remove_match(connection, removed_rule, NULL);
281 dbus_bus_remove_match(connection, added_rule, NULL);
282 dbus_connection_flush(connection);
284 g_dbus_remove_watch(connection, watch);
286 bluetooth_disconnect(connection, NULL);
288 connman_device_driver_unregister(&bluetooth_driver);
290 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
292 dbus_connection_unref(connection);
295 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
296 bluetooth_init, bluetooth_exit)