[pulseaudio-discuss] [PATCH 3/4] bluetooth: use native and ofono backends in parallel with headset=auto

Georg Chini georg at chini.tk
Tue Feb 14 13:33:02 UTC 2017


This patch changes the behavior of the headset=auto switch for module-bluez5-discover.
With headset=auto now both backends will be active at the same time for the HF role and
the switching between the backends is only done for the AG role.
headset=ofono and headset=native remain unchanged.

---
 src/modules/bluetooth/backend-native.c | 24 +++++++++++++++++++++---
 src/modules/bluetooth/bluez5-util.c    | 10 +++-------
 src/modules/bluetooth/bluez5-util.h    |  4 ++--
 3 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/src/modules/bluetooth/backend-native.c b/src/modules/bluetooth/backend-native.c
index f4e6760..66d545d 100644
--- a/src/modules/bluetooth/backend-native.c
+++ b/src/modules/bluetooth/backend-native.c
@@ -40,6 +40,7 @@ struct pa_bluetooth_backend {
   pa_core *core;
   pa_dbus_connection *connection;
   pa_bluetooth_discovery *discovery;
+  bool enable_ag;
 
   PA_LLIST_HEAD(pa_dbus_pending, pending);
 };
@@ -626,10 +627,24 @@ static void profile_done(pa_bluetooth_backend *b, pa_bluetooth_profile_t profile
     }
 }
 
-pa_bluetooth_backend *pa_bluetooth_native_backend_new(pa_core *c, pa_bluetooth_discovery *y) {
+pa_bluetooth_backend *pa_bluetooth_native_backend_new(pa_core *c, pa_bluetooth_discovery *y, pa_bluetooth_backend *native_backend, bool enable_ag) {
     pa_bluetooth_backend *backend;
     DBusError err;
 
+    /* If the backend already exists just switch the AG role on or off */
+    if (native_backend) {
+       if (enable_ag == native_backend->enable_ag)
+          return native_backend;
+
+       if (enable_ag)
+          profile_init(native_backend, PA_BLUETOOTH_PROFILE_HEADSET_AUDIO_GATEWAY);
+       else
+          profile_done(native_backend, PA_BLUETOOTH_PROFILE_HEADSET_AUDIO_GATEWAY);
+
+       native_backend->enable_ag = enable_ag;
+       return native_backend;
+    }
+
     pa_log_debug("Bluetooth Headset Backend API support using the native backend");
 
     backend = pa_xnew0(pa_bluetooth_backend, 1);
@@ -644,8 +659,10 @@ pa_bluetooth_backend *pa_bluetooth_native_backend_new(pa_core *c, pa_bluetooth_d
     }
 
     backend->discovery = y;
+    backend->enable_ag = enable_ag;
 
-    profile_init(backend, PA_BLUETOOTH_PROFILE_HEADSET_AUDIO_GATEWAY);
+    if (enable_ag)
+       profile_init(backend, PA_BLUETOOTH_PROFILE_HEADSET_AUDIO_GATEWAY);
     profile_init(backend, PA_BLUETOOTH_PROFILE_HEADSET_HEAD_UNIT);
 
     return backend;
@@ -656,7 +673,8 @@ void pa_bluetooth_native_backend_free(pa_bluetooth_backend *backend) {
 
     pa_dbus_free_pending_list(&backend->pending);
 
-    profile_done(backend, PA_BLUETOOTH_PROFILE_HEADSET_AUDIO_GATEWAY);
+    if (backend->enable_ag)
+       profile_done(backend, PA_BLUETOOTH_PROFILE_HEADSET_AUDIO_GATEWAY);
     profile_done(backend, PA_BLUETOOTH_PROFILE_HEADSET_HEAD_UNIT);
 
     pa_dbus_connection_unref(backend->connection);
diff --git a/src/modules/bluetooth/bluez5-util.c b/src/modules/bluetooth/bluez5-util.c
index 7d63f35..46ba22e 100644
--- a/src/modules/bluetooth/bluez5-util.c
+++ b/src/modules/bluetooth/bluez5-util.c
@@ -982,12 +982,8 @@ void pa_bluetooth_discovery_set_ofono_running(pa_bluetooth_discovery *y, bool is
     if (y->headset_backend != HEADSET_BACKEND_AUTO)
         return;
 
-    if (is_running && y->native_backend) {
-        pa_bluetooth_native_backend_free(y->native_backend);
-        y->native_backend = NULL;
-    }
-    else if (!is_running && !y->native_backend)
-        y->native_backend = pa_bluetooth_native_backend_new(y->core, y);
+    y->native_backend = pa_bluetooth_native_backend_new(y->core, y, y->native_backend, !is_running);
+
 }
 
 static void get_managed_objects_reply(DBusPendingCall *pending, void *userdata) {
@@ -1031,7 +1027,7 @@ static void get_managed_objects_reply(DBusPendingCall *pending, void *userdata)
     if (!y->ofono_backend && y->headset_backend != HEADSET_BACKEND_NATIVE)
         y->ofono_backend = pa_bluetooth_ofono_backend_new(y->core, y);
     if (!y->ofono_backend && !y->native_backend && y->headset_backend != HEADSET_BACKEND_OFONO)
-        y->native_backend = pa_bluetooth_native_backend_new(y->core, y);
+        y->native_backend = pa_bluetooth_native_backend_new(y->core, y, y->native_backend, true);
 
 finish:
     dbus_message_unref(r);
diff --git a/src/modules/bluetooth/bluez5-util.h b/src/modules/bluetooth/bluez5-util.h
index ef0c731..084bebf 100644
--- a/src/modules/bluetooth/bluez5-util.h
+++ b/src/modules/bluetooth/bluez5-util.h
@@ -129,10 +129,10 @@ static inline void pa_bluetooth_ofono_backend_free(pa_bluetooth_backend *b) {}
 #endif
 
 #ifdef HAVE_BLUEZ_5_NATIVE_HEADSET
-pa_bluetooth_backend *pa_bluetooth_native_backend_new(pa_core *c, pa_bluetooth_discovery *y);
+pa_bluetooth_backend *pa_bluetooth_native_backend_new(pa_core *c, pa_bluetooth_discovery *y, pa_bluetooth_backend *native_backend, bool enable_ag);
 void pa_bluetooth_native_backend_free(pa_bluetooth_backend *b);
 #else
-static inline pa_bluetooth_backend *pa_bluetooth_native_backend_new(pa_core *c, pa_bluetooth_discovery *y) {
+static inline pa_bluetooth_backend *pa_bluetooth_native_backend_new(pa_core *c, pa_bluetooth_discovery *y, pa_bluetooth_backend *native_backend, bool enable_ag) {
     return NULL;
 }
 static inline void pa_bluetooth_native_backend_free(pa_bluetooth_backend *b) {}
-- 
2.10.1



More information about the pulseaudio-discuss mailing list