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
26 #include <dbus/dbus.h>
28 #include <connman/plugin.h>
29 #include <connman/driver.h>
30 #include <connman/log.h>
32 #define BLUEZ_SERVICE "org.bluez"
34 #define MANAGER_INTERFACE "org.bluez.Manager"
35 #define MANAGER_PATH "/"
37 static GStaticMutex element_mutex = G_STATIC_MUTEX_INIT;
38 static GSList *element_list = NULL;
40 static void create_element(DBusConnection *conn, const char *path)
42 struct connman_element *element;
44 DBG("conn %p path %s", conn, path);
46 element = connman_element_create();
48 element->name = g_path_get_basename(path);
49 element->type = CONNMAN_ELEMENT_TYPE_DEVICE;
50 element->subtype = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH;
52 g_static_mutex_lock(&element_mutex);
54 connman_element_register(element, NULL);
56 element_list = g_slist_append(element_list, element);
58 g_static_mutex_unlock(&element_mutex);
61 static DBusHandlerResult bluetooth_filter(DBusConnection *conn,
62 DBusMessage *msg, void *data)
64 DBG("conn %p msg %p", conn, msg);
66 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
69 static void list_adapters(DBusConnection *conn)
71 DBusMessage *msg, *reply;
77 msg = dbus_message_new_method_call(BLUEZ_SERVICE, MANAGER_PATH,
78 MANAGER_INTERFACE, "ListAdapters");
80 connman_error("ListAdpaters message alloction failed");
84 reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, NULL);
86 dbus_message_unref(msg);
89 connman_error("ListAdapters method call failed");
93 dbus_message_get_args(reply, NULL, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
94 &paths, &num, DBUS_TYPE_INVALID);
96 for (i = 0; i < num; i++)
97 create_element(conn, paths[i]);
101 dbus_message_unref(reply);
104 static int bluetooth_probe(struct connman_element *element)
106 DBG("element %p name %s", element, element->name);
111 static void bluetooth_remove(struct connman_element *element)
113 DBG("element %p name %s", element, element->name);
116 static struct connman_driver bluetooth_driver = {
118 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
119 .subtype = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH,
120 .probe = bluetooth_probe,
121 .remove = bluetooth_remove,
124 static DBusConnection *connection;
126 static int bluetooth_init(void)
131 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
132 if (connection == NULL)
135 if (dbus_connection_add_filter(connection, bluetooth_filter,
136 NULL, NULL) == FALSE)
137 connman_error("Can't add D-Bus filter for Bluetooth");
139 match = g_strdup_printf("sender=%s,interface=%s", "org.bluez",
140 "org.bluez.Manager");
142 dbus_bus_add_match(connection, match, NULL);
146 err = connman_driver_register(&bluetooth_driver);
148 dbus_connection_unref(connection);
152 list_adapters(connection);
157 static void bluetooth_exit(void)
159 connman_driver_unregister(&bluetooth_driver);
161 dbus_connection_unref(connection);
164 CONNMAN_PLUGIN_DEFINE("bluetooth", "Bluetooth technology plugin", VERSION,
165 bluetooth_init, bluetooth_exit)