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"
38 static int bluetooth_probe(struct connman_device *device)
40 DBG("device %p", device);
45 static void bluetooth_remove(struct connman_device *device)
47 DBG("device %p", device);
50 static struct connman_device_driver bluetooth_driver = {
52 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
53 .probe = bluetooth_probe,
54 .remove = bluetooth_remove,
57 static GSList *device_list = NULL;
59 static void adapters_reply(DBusPendingCall *call, void *user_data)
68 reply = dbus_pending_call_steal_reply(call);
70 dbus_error_init(&error);
72 if (dbus_message_get_args(reply, &error,
73 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
74 &adapters, &num_adapters,
75 DBUS_TYPE_INVALID) == FALSE) {
76 if (dbus_error_is_set(&error) == TRUE) {
77 connman_error("%s", error.message);
78 dbus_error_free(&error);
80 connman_error("Wrong arguments for adapter list");
84 for (i = 0; i < num_adapters; i++) {
85 struct connman_element *device;
87 device = connman_element_create(NULL);
88 device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
89 device->subtype = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH;
91 device->name = g_path_get_basename(adapters[i]);
93 connman_element_register(device, NULL);
94 device_list = g_slist_append(device_list, device);
100 dbus_message_unref(reply);
103 static void bluetooth_connect(DBusConnection *connection, void *user_data)
105 DBusMessage *message;
106 DBusPendingCall *call;
108 DBG("connection %p", connection);
110 message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
111 "org.bluez.Manager", "ListAdapters");
115 if (dbus_connection_send_with_reply(connection, message,
116 &call, TIMEOUT) == FALSE) {
117 connman_error("Failed to get Bluetooth adapters");
118 dbus_message_unref(message);
122 dbus_pending_call_set_notify(call, adapters_reply, NULL, NULL);
124 dbus_message_unref(message);
127 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
131 DBG("connection %p", connection);
133 for (list = device_list; list; list = list->next) {
134 struct connman_element *device = list->data;
136 connman_element_unregister(device);
137 connman_element_unref(device);
140 g_slist_free(device_list);
144 static DBusConnection *connection;
147 static int bluetooth_init(void)
151 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
152 if (connection == NULL)
155 err = connman_device_driver_register(&bluetooth_driver);
157 dbus_connection_unref(connection);
161 watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
162 bluetooth_connect, bluetooth_disconnect, NULL, NULL);
164 connman_device_driver_unregister(&bluetooth_driver);
165 dbus_connection_unref(connection);
169 if (g_dbus_check_service(connection, BLUEZ_SERVICE) == TRUE)
170 bluetooth_connect(connection, NULL);
175 static void bluetooth_exit(void)
177 g_dbus_remove_watch(connection, watch);
179 connman_device_driver_unregister(&bluetooth_driver);
181 dbus_connection_unref(connection);
184 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
185 bluetooth_init, bluetooth_exit)