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
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 gboolean bluetooth_signal(DBusConnection *conn,
62 DBusMessage *msg, void *data)
64 const char *sender, *interface, *member;
66 DBG("conn %p msg %p", conn, msg);
68 sender = dbus_message_get_sender(msg);
69 interface = dbus_message_get_interface(msg);
70 member = dbus_message_get_member(msg);
72 DBG("sender %s name %s.%s", sender, interface, member);
77 static void list_adapters(DBusConnection *conn)
79 DBusMessage *msg, *reply;
85 msg = dbus_message_new_method_call(BLUEZ_SERVICE, MANAGER_PATH,
86 MANAGER_INTERFACE, "ListAdapters");
88 connman_error("ListAdpaters message alloction failed");
92 reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, NULL);
94 dbus_message_unref(msg);
97 connman_error("ListAdapters method call failed");
101 dbus_message_get_args(reply, NULL, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
102 &paths, &num, DBUS_TYPE_INVALID);
104 for (i = 0; i < num; i++)
105 create_element(conn, paths[i]);
109 dbus_message_unref(reply);
112 static int bluetooth_probe(struct connman_element *element)
114 DBG("element %p name %s", element, element->name);
119 static void bluetooth_remove(struct connman_element *element)
121 DBG("element %p name %s", element, element->name);
124 static struct connman_driver bluetooth_driver = {
126 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
127 .subtype = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH,
128 .probe = bluetooth_probe,
129 .remove = bluetooth_remove,
132 static DBusConnection *connection;
135 static int bluetooth_init(void)
139 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
140 if (connection == NULL)
143 signal = g_dbus_add_signal_watch(connection, "sender=org.bluez",
144 bluetooth_signal, NULL, 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 g_dbus_remove_watch(connection, signal);
163 dbus_connection_unref(connection);
166 CONNMAN_PLUGIN_DEFINE("bluetooth", "Bluetooth technology plugin", VERSION,
167 bluetooth_init, bluetooth_exit)