]> code.delx.au - pulseaudio/blob - src/modules/bluetooth/bluez5-util.c
bluetooth: Create infrastrucure for bluetooth hooks
[pulseaudio] / src / modules / bluetooth / bluez5-util.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008-2013 João Paulo Rechi Vita
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <pulse/xmalloc.h>
27
28 #include <pulsecore/core.h>
29 #include <pulsecore/core-util.h>
30 #include <pulsecore/dbus-shared.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/refcnt.h>
34 #include <pulsecore/shared.h>
35
36 #include "bluez5-util.h"
37
38 #define BLUEZ_SERVICE "org.bluez"
39
40 struct pa_bluetooth_discovery {
41 PA_REFCNT_DECLARE;
42
43 pa_core *core;
44 pa_dbus_connection *connection;
45 bool filter_added;
46 bool matches_added;
47 pa_hook hooks[PA_BLUETOOTH_HOOK_MAX];
48 };
49
50 pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y, pa_bluetooth_hook_t hook) {
51 pa_assert(y);
52 pa_assert(PA_REFCNT_VALUE(y) > 0);
53
54 return &y->hooks[hook];
55 }
56
57 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *userdata) {
58 pa_bluetooth_discovery *y;
59 DBusError err;
60
61 pa_assert(bus);
62 pa_assert(m);
63 pa_assert_se(y = userdata);
64
65 dbus_error_init(&err);
66
67 if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged")) {
68 const char *name, *old_owner, *new_owner;
69
70 if (!dbus_message_get_args(m, &err,
71 DBUS_TYPE_STRING, &name,
72 DBUS_TYPE_STRING, &old_owner,
73 DBUS_TYPE_STRING, &new_owner,
74 DBUS_TYPE_INVALID)) {
75 pa_log_error("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err.message);
76 goto fail;
77 }
78
79 if (pa_streq(name, BLUEZ_SERVICE)) {
80 if (old_owner && *old_owner) {
81 pa_log_debug("Bluetooth daemon disappeared");
82 /* TODO: remove all devices */
83 }
84
85 if (new_owner && *new_owner) {
86 pa_log_debug("Bluetooth daemon appeared");
87 /* TODO: get managed objects */
88 }
89 }
90
91 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
92 }
93
94 fail:
95 dbus_error_free(&err);
96
97 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
98 }
99
100 pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
101 pa_bluetooth_discovery *y;
102 DBusError err;
103 DBusConnection *conn;
104 unsigned i;
105
106 if ((y = pa_shared_get(c, "bluetooth-discovery")))
107 return pa_bluetooth_discovery_ref(y);
108
109 y = pa_xnew0(pa_bluetooth_discovery, 1);
110 PA_REFCNT_INIT(y);
111 y->core = c;
112
113 for (i = 0; i < PA_BLUETOOTH_HOOK_MAX; i++)
114 pa_hook_init(&y->hooks[i], y);
115
116 pa_shared_set(c, "bluetooth-discovery", y);
117
118 dbus_error_init(&err);
119
120 if (!(y->connection = pa_dbus_bus_get(y->core, DBUS_BUS_SYSTEM, &err))) {
121 pa_log_error("Failed to get D-Bus connection: %s", err.message);
122 goto fail;
123 }
124
125 conn = pa_dbus_connection_get(y->connection);
126
127 /* dynamic detection of bluetooth audio devices */
128 if (!dbus_connection_add_filter(conn, filter_cb, y, NULL)) {
129 pa_log_error("Failed to add filter function");
130 goto fail;
131 }
132 y->filter_added = true;
133
134 if (pa_dbus_add_matches(conn, &err,
135 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'"
136 ",arg0='" BLUEZ_SERVICE "'",
137 NULL) < 0) {
138 pa_log_error("Failed to add D-Bus matches: %s", err.message);
139 goto fail;
140 }
141 y->matches_added = true;
142
143 return y;
144
145 fail:
146 pa_bluetooth_discovery_unref(y);
147 dbus_error_free(&err);
148
149 return NULL;
150 }
151
152 pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y) {
153 pa_assert(y);
154 pa_assert(PA_REFCNT_VALUE(y) > 0);
155
156 PA_REFCNT_INC(y);
157
158 return y;
159 }
160
161 void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) {
162 pa_assert(y);
163 pa_assert(PA_REFCNT_VALUE(y) > 0);
164
165 if (PA_REFCNT_DEC(y) > 0)
166 return;
167
168 if (y->connection) {
169
170 if (y->matches_added)
171 pa_dbus_remove_matches(pa_dbus_connection_get(y->connection),
172 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',"
173 "arg0='" BLUEZ_SERVICE "'",
174 NULL);
175
176 if (y->filter_added)
177 dbus_connection_remove_filter(pa_dbus_connection_get(y->connection), filter_cb, y);
178
179 pa_dbus_connection_unref(y->connection);
180 }
181
182 pa_shared_remove(y->core, "bluetooth-discovery");
183 pa_xfree(y);
184 }