summaryrefslogtreecommitdiffstats
path: root/meta-agl-devel/ATTIC/meta-audio-4a-framework/recipes-connectivity/bluez-alsa/bluez-alsa/0003-dbus-request-a-name-on-startup.patch
blob: 1ef59296934a7a1ef8272c10066ae82eec200fd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
From 4c14fffbd20a6dd9e6ceca1eb1726ceaafab7410 Mon Sep 17 00:00:00 2001
From: Thierry Bultel <thierry.bultel@iot.bzh>
Date: Fri, 18 Jan 2019 11:45:29 +0100
Subject: [PATCH 3/3] dbus: request a name on startup

When the service thread is ready, ask a name to dbus
The advantage of doing that is that client applications
can check dbus for bluez-alsa presence, without having
to implement any kinf of polling logic. Also, this
way, they can be notified on the service exit, to
perform any needed cleanup.
Since there must be an instance of bluealsa daemon
per hci device, the dbus name will be
'org.bluez-alsa.hci0', 'org.bluez-alsa.hci1' ...
and so on.

Signed-off-by: Thierry Bultel <thierry.bultel@iot.bzh>
---
 configure.ac        | 10 ++++++++++
 src/Makefile.am     |  3 +++
 src/bluez-alsa.conf | 14 ++++++++++++++
 src/main.c          | 32 ++++++++++++++++++++++++++++++++
 4 files changed, 59 insertions(+)
 create mode 100644 src/bluez-alsa.conf

diff --git a/configure.ac b/configure.ac
index 6323d60..d4ec7d4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -130,6 +130,16 @@ AC_ARG_WITH([alsaconfdir],
 		[alsaconfdir="$sysconfdir/alsa/conf.d"],
 		[alsaconfdir="$datadir/alsa/alsa.conf.d"])])
 
+AC_ARG_WITH(dbusconfdir, AC_HELP_STRING([--with-dbusconfdir=PATH],
+	[path to D-Bus config directory]), [path_dbusconf=${withval}],
+		[path_dbusconf="`$PKG_CONFIG --variable=sysconfdir dbus-1`"])
+if (test -z "${path_dbusconf}"); then
+	DBUS_CONFDIR="${sysconfdir}/dbus-1/system.d"
+else
+	DBUS_CONFDIR="${path_dbusconf}/dbus-1/system.d"
+fi
+AC_SUBST(DBUS_CONFDIR)
+
 test "x$prefix" = xNONE && prefix=$ac_default_prefix
 test "x$exec_prefix" = xNONE && exec_prefix=$prefix
 
diff --git a/src/Makefile.am b/src/Makefile.am
index 5626ae5..c501f2e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -23,6 +23,9 @@ bluealsa_SOURCES += \
 	ofono-iface.c
 endif
 
+dbusconfdir = @DBUS_CONFDIR@
+dist_dbusconf_DATA = bluez-alsa.conf
+
 AM_CFLAGS = \
 	@BLUEZ_CFLAGS@ \
 	@GLIB2_CFLAGS@ \
diff --git a/src/bluez-alsa.conf b/src/bluez-alsa.conf
new file mode 100644
index 0000000..6b999d6
--- /dev/null
+++ b/src/bluez-alsa.conf
@@ -0,0 +1,14 @@
+<!-- This configuration file specifies the required security policies
+     for bluez-alsa core daemon to work. -->
+
+<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
+ "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
+<busconfig>
+
+  <!-- ../system.conf have denied everything, so we just punch some holes -->
+
+  <policy user="root">
+    <allow own_prefix="org.bluez-alsa"/>
+  </policy>
+
+</busconfig>
diff --git a/src/main.c b/src/main.c
index 9cef402..9183978 100644
--- a/src/main.c
+++ b/src/main.c
@@ -12,6 +12,7 @@
 # include "config.h"
 #endif
 
+#define _GNU_SOURCE
 #include <errno.h>
 #include <getopt.h>
 #include <signal.h>
@@ -58,6 +59,20 @@ static char *get_a2dp_codecs(
 	return g_strjoinv(", ", (char **)tmp);
 }
 
+static void name_acquired_handler(
+		GDBusConnection *connection,
+		const gchar *name,
+		gpointer user_data) {
+	debug("Acquired name: %s", name);
+}
+
+static void name_lost_handler(
+		GDBusConnection *connection,
+		const gchar *name,
+		gpointer user_data) {
+	debug("Lost name: %s", name);
+}
+
 static GMainLoop *loop = NULL;
 static void main_loop_stop(int sig) {
 	/* Call to this handler restores the default action, so on the
@@ -318,6 +333,7 @@ int main(int argc, char **argv) {
 	GError *err;
 
 	err = NULL;
+
 	address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
 	if ((config.dbus = g_dbus_connection_new_for_address_sync(address,
 					G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
@@ -336,6 +352,19 @@ int main(int argc, char **argv) {
 	ofono_register();
 #endif
 
+	char * name_on_bus = NULL;
+	asprintf(&name_on_bus, "org.bluez-alsa.%s", config.hci_dev.name);
+
+	guint g_dbusid;
+	g_dbusid = g_bus_own_name_on_connection(config.dbus,
+								name_on_bus,
+								G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
+								G_BUS_NAME_OWNER_FLAGS_REPLACE,
+								name_acquired_handler,
+								name_lost_handler,
+								NULL,
+								NULL);
+
 	/* In order to receive EPIPE while writing to the pipe whose reading end
 	 * is closed, the SIGPIPE signal has to be handled. For more information
 	 * see the io_thread_write_pcm() function. */
@@ -359,5 +388,8 @@ int main(int argc, char **argv) {
 	bluealsa_ctl_free();
 	bluealsa_config_free();
 
+	g_bus_unown_name (g_dbusid);
+	free(name_on_bus);
+
 	return EXIT_SUCCESS;
 }
-- 
2.16.4