Use new signal callback function from libgdbus
[connman] / 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 <gdbus.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 gboolean bluetooth_signal(DBusConnection *conn,
62                                         DBusMessage *msg, void *data)
63 {
64         const char *sender, *interface, *member;
65
66         DBG("conn %p msg %p", conn, msg);
67
68         sender = dbus_message_get_sender(msg);
69         interface = dbus_message_get_interface(msg);
70         member = dbus_message_get_member(msg);
71
72         DBG("sender %s name %s.%s", sender, interface, member);
73
74         return TRUE;
75 }
76
77 static void list_adapters(DBusConnection *conn)
78 {
79         DBusMessage *msg, *reply;
80         char **paths = NULL;
81         int i, num = 0;
82
83         DBG("conn %p");
84
85         msg = dbus_message_new_method_call(BLUEZ_SERVICE, MANAGER_PATH,
86                                         MANAGER_INTERFACE, "ListAdapters");
87         if (!msg) {
88                 connman_error("ListAdpaters message alloction failed");
89                 return;
90         }
91
92         reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, NULL);
93
94         dbus_message_unref(msg);
95
96         if (!reply) {
97                 connman_error("ListAdapters method call failed");
98                 return;
99         }
100
101         dbus_message_get_args(reply, NULL, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
102                                                 &paths, &num, DBUS_TYPE_INVALID);
103
104         for (i = 0; i < num; i++)
105                 create_element(conn, paths[i]);
106
107         g_strfreev(paths);
108
109         dbus_message_unref(reply);
110 }
111
112 static int bluetooth_probe(struct connman_element *element)
113 {
114         DBG("element %p name %s", element, element->name);
115
116         return 0;
117 }
118
119 static void bluetooth_remove(struct connman_element *element)
120 {
121         DBG("element %p name %s", element, element->name);
122 }
123
124 static struct connman_driver bluetooth_driver = {
125         .name           = "bluetooth",
126         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
127         .subtype        = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH,
128         .probe          = bluetooth_probe,
129         .remove         = bluetooth_remove,
130 };
131
132 static DBusConnection *connection;
133 static guint signal;
134
135 static int bluetooth_init(void)
136 {
137         int err;
138
139         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
140         if (connection == NULL)
141                 return -EIO;
142
143         signal = g_dbus_add_signal_watch(connection, "sender=org.bluez",
144                                                 bluetooth_signal, NULL, NULL);
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         g_dbus_remove_watch(connection, signal);
162
163         dbus_connection_unref(connection);
164 }
165
166 CONNMAN_PLUGIN_DEFINE("bluetooth", "Bluetooth technology plugin", VERSION,
167                                                 bluetooth_init, bluetooth_exit)