Update Bluetooth plugin with hardware detection support
[framework/connectivity/connman.git] / plugins / bluetooth.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <dbus/dbus.h>
27
28 #include <connman/plugin.h>
29 #include <connman/driver.h>
30 #include <connman/log.h>
31
32 #define BLUEZ_SERVICE "org.bluez"
33
34 #define MANAGER_INTERFACE "org.bluez.Manager"
35 #define MANAGER_PATH "/"
36
37 static GStaticMutex element_mutex = G_STATIC_MUTEX_INIT;
38 static GSList *element_list = NULL;
39
40 static void create_element(DBusConnection *conn, const char *path)
41 {
42         struct connman_element *element;
43
44         DBG("conn %p path %s", conn, path);
45
46         element = connman_element_create();
47
48         element->name = g_path_get_basename(path);
49         element->type = CONNMAN_ELEMENT_TYPE_DEVICE;
50         element->subtype = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH;
51
52         g_static_mutex_lock(&element_mutex);
53
54         connman_element_register(element, NULL);
55
56         element_list = g_slist_append(element_list, element);
57
58         g_static_mutex_unlock(&element_mutex);
59 }
60
61 static DBusHandlerResult bluetooth_filter(DBusConnection *conn,
62                                                 DBusMessage *msg, void *data)
63 {
64         DBG("conn %p msg %p", conn, msg);
65
66         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
67 }
68
69 static void list_adapters(DBusConnection *conn)
70 {
71         DBusMessage *msg, *reply;
72         char **paths = NULL;
73         int i, num = 0;
74
75         DBG("conn %p");
76
77         msg = dbus_message_new_method_call(BLUEZ_SERVICE, MANAGER_PATH,
78                                         MANAGER_INTERFACE, "ListAdapters");
79         if (!msg) {
80                 connman_error("ListAdpaters message alloction failed");
81                 return;
82         }
83
84         reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, NULL);
85
86         dbus_message_unref(msg);
87
88         if (!reply) {
89                 connman_error("ListAdapters method call failed");
90                 return;
91         }
92
93         dbus_message_get_args(reply, NULL, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
94                                                 &paths, &num, DBUS_TYPE_INVALID);
95
96         for (i = 0; i < num; i++)
97                 create_element(conn, paths[i]);
98
99         g_strfreev(paths);
100
101         dbus_message_unref(reply);
102 }
103
104 static int bluetooth_probe(struct connman_element *element)
105 {
106         DBG("element %p name %s", element, element->name);
107
108         return 0;
109 }
110
111 static void bluetooth_remove(struct connman_element *element)
112 {
113         DBG("element %p name %s", element, element->name);
114 }
115
116 static struct connman_driver bluetooth_driver = {
117         .name           = "bluetooth",
118         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
119         .subtype        = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH,
120         .probe          = bluetooth_probe,
121         .remove         = bluetooth_remove,
122 };
123
124 static DBusConnection *connection;
125
126 static int bluetooth_init(void)
127 {
128         gchar *match;
129         int err;
130
131         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
132         if (connection == NULL)
133                 return -EIO;
134
135         if (dbus_connection_add_filter(connection, bluetooth_filter,
136                                                 NULL, NULL) == FALSE)
137                 connman_error("Can't add D-Bus filter for Bluetooth");
138
139         match = g_strdup_printf("sender=%s,interface=%s", "org.bluez",
140                                                         "org.bluez.Manager");
141
142         dbus_bus_add_match(connection, match, NULL);
143
144         g_free(match);
145
146         err = connman_driver_register(&bluetooth_driver);
147         if (err < 0) {
148                 dbus_connection_unref(connection);
149                 return err;
150         }
151
152         list_adapters(connection);
153
154         return 0;
155 }
156
157 static void bluetooth_exit(void)
158 {
159         connman_driver_unregister(&bluetooth_driver);
160
161         dbus_connection_unref(connection);
162 }
163
164 CONNMAN_PLUGIN_DEFINE("bluetooth", "Bluetooth technology plugin", VERSION,
165                                                 bluetooth_init, bluetooth_exit)