[pulseaudio-commits] 11 commits - src/modules

Tanu Kaskinen tanuk at kemper.freedesktop.org
Thu Sep 11 04:02:05 PDT 2014


 src/modules/bluetooth/backend-ofono.c |  503 +++++++++++++++++++++++++++++++++-
 1 file changed, 497 insertions(+), 6 deletions(-)

New commits:
commit f7c7cd1825a0bf0ad643d4dfd4a38f589930e886
Author: João Paulo Rechi Vita <jprvita at openbossa.org>
Date:   Wed Sep 10 11:48:34 2014 +0200

    bluetooth: Implement org.ofono.HandsfreeAudioAgent.Release()

diff --git a/src/modules/bluetooth/backend-ofono.c b/src/modules/bluetooth/backend-ofono.c
index 3f40941..cab5319 100644
--- a/src/modules/bluetooth/backend-ofono.c
+++ b/src/modules/bluetooth/backend-ofono.c
@@ -491,7 +491,15 @@ static DBusMessage *hf_audio_agent_release(DBusConnection *c, DBusMessage *m, vo
         return r;
     }
 
-    r = dbus_message_new_error(m, "org.ofono.Error.NotImplemented", "Operation is not implemented");
+    pa_log_debug("HF audio agent has been unregistered by oFono (%s)", backend->ofono_bus_id);
+
+    pa_hashmap_remove_all(backend->cards);
+
+    pa_xfree(backend->ofono_bus_id);
+    backend->ofono_bus_id = NULL;
+
+    pa_assert_se(r = dbus_message_new_method_return(m));
+
     return r;
 }
 

commit b1a09ebf7ea3ba9471d2376b2b42fc252897278d
Author: João Paulo Rechi Vita <jprvita at openbossa.org>
Date:   Wed Sep 10 11:48:33 2014 +0200

    bluetooth: Handle CardRemoved signal

diff --git a/src/modules/bluetooth/backend-ofono.c b/src/modules/bluetooth/backend-ofono.c
index fe4e2ca..3f40941 100644
--- a/src/modules/bluetooth/backend-ofono.c
+++ b/src/modules/bluetooth/backend-ofono.c
@@ -269,6 +269,24 @@ fail:
     hf_audio_card_free(card);
 }
 
+static void hf_audio_agent_card_removed(pa_bluetooth_backend *backend, const char *path) {
+    struct hf_audio_card *card;
+
+    pa_assert(backend);
+    pa_assert(path);
+
+    pa_log_debug("HF card removed: %s", path);
+
+    card = pa_hashmap_remove(backend->cards, path);
+    if (!card)
+        return;
+
+    if (card->transport)
+        pa_bluetooth_transport_unlink(card->transport);
+
+    hf_audio_card_free(card);
+}
+
 static void hf_audio_agent_get_cards_reply(DBusPendingCall *pending, void *userdata) {
     DBusMessage *r;
     pa_dbus_pending *p;
@@ -444,6 +462,15 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *da
         dbus_message_iter_recurse(&arg_i, &props_i);
 
         hf_audio_agent_card_found(backend, p, &props_i);
+    } else if (dbus_message_is_signal(m, "org.ofono.HandsfreeAudioManager", "CardRemoved")) {
+        const char *p;
+
+        if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &p, DBUS_TYPE_INVALID)) {
+            pa_log_error("Failed to parse org.ofono.HandsfreeAudioManager.CardRemoved: %s", err.message);
+            goto fail;
+        }
+
+        hf_audio_agent_card_removed(backend, p);
     }
 
 fail:

commit 8f838b1f31b45db68303d45918e6fd4fc7018dcb
Author: João Paulo Rechi Vita <jprvita at openbossa.org>
Date:   Wed Sep 10 11:48:32 2014 +0200

    bluetooth: Handle CardAdded signal

diff --git a/src/modules/bluetooth/backend-ofono.c b/src/modules/bluetooth/backend-ofono.c
index ef8b2ce..fe4e2ca 100644
--- a/src/modules/bluetooth/backend-ofono.c
+++ b/src/modules/bluetooth/backend-ofono.c
@@ -249,18 +249,19 @@ static void hf_audio_agent_card_found(pa_bluetooth_backend *backend, const char
         dbus_message_iter_next(props_i);
     }
 
-    pa_hashmap_put(backend->cards, card->path, card);
-
     d = pa_bluetooth_discovery_get_device_by_address(backend->discovery, card->remote_address, card->local_address);
-    if (d) {
-        card->transport = pa_bluetooth_transport_new(d, backend->ofono_bus_id, path, PA_BLUETOOTH_PROFILE_HEADSET_AUDIO_GATEWAY, NULL, 0);
-        card->transport->acquire = hf_audio_agent_transport_acquire;
-        card->transport->release = hf_audio_agent_transport_release;
-        card->transport->userdata = card;
-
-        pa_bluetooth_transport_put(card->transport);
-    } else
+    if (!d) {
         pa_log_error("Device doesnt exist for %s", path);
+        goto fail;
+    }
+
+    card->transport = pa_bluetooth_transport_new(d, backend->ofono_bus_id, path, PA_BLUETOOTH_PROFILE_HEADSET_AUDIO_GATEWAY, NULL, 0);
+    card->transport->acquire = hf_audio_agent_transport_acquire;
+    card->transport->release = hf_audio_agent_transport_release;
+    card->transport->userdata = card;
+
+    pa_bluetooth_transport_put(card->transport);
+    pa_hashmap_put(backend->cards, card->path, card);
 
     return;
 
@@ -426,6 +427,23 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *da
             }
         }
 
+    } else if (dbus_message_is_signal(m, "org.ofono.HandsfreeAudioManager", "CardAdded")) {
+        const char *p;
+        DBusMessageIter arg_i, props_i;
+
+        if (!dbus_message_iter_init(m, &arg_i) || !pa_streq(dbus_message_get_signature(m), "oa{sv}")) {
+            pa_log_error("Failed to parse org.ofono.HandsfreeAudioManager.CardAdded");
+            goto fail;
+        }
+
+        dbus_message_iter_get_basic(&arg_i, &p);
+
+        pa_assert_se(dbus_message_iter_next(&arg_i));
+        pa_assert(dbus_message_iter_get_arg_type(&arg_i) == DBUS_TYPE_ARRAY);
+
+        dbus_message_iter_recurse(&arg_i, &props_i);
+
+        hf_audio_agent_card_found(backend, p, &props_i);
     }
 
 fail:

commit 0e7f303256ff9c156a00b0530c3a90d9a26c07ca
Author: João Paulo Rechi Vita <jprvita at openbossa.org>
Date:   Wed Sep 10 11:48:31 2014 +0200

    bluetooth: Track oFono service

diff --git a/src/modules/bluetooth/backend-ofono.c b/src/modules/bluetooth/backend-ofono.c
index bc92f4a..ef8b2ce 100644
--- a/src/modules/bluetooth/backend-ofono.c
+++ b/src/modules/bluetooth/backend-ofono.c
@@ -384,6 +384,7 @@ static void hf_audio_agent_unregister(pa_bluetooth_backend *backend) {
 
 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *data) {
     const char *sender;
+    DBusError err;
     pa_bluetooth_backend *backend = data;
 
     pa_assert(bus);
@@ -394,6 +395,41 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *da
     if (!pa_safe_streq(backend->ofono_bus_id, sender) && !pa_streq("org.freedesktop.DBus", sender))
         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
+    dbus_error_init(&err);
+
+    if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged")) {
+        const char *name, *old_owner, *new_owner;
+
+        if (!dbus_message_get_args(m, &err,
+                                   DBUS_TYPE_STRING, &name,
+                                   DBUS_TYPE_STRING, &old_owner,
+                                   DBUS_TYPE_STRING, &new_owner,
+                                   DBUS_TYPE_INVALID)) {
+            pa_log_error("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err.message);
+            goto fail;
+        }
+
+        if (pa_streq(name, OFONO_SERVICE)) {
+
+            if (old_owner && *old_owner) {
+                pa_log_debug("oFono disappeared");
+
+                pa_hashmap_remove_all(backend->cards);
+
+                pa_xfree(backend->ofono_bus_id);
+                backend->ofono_bus_id = NULL;
+            }
+
+            if (new_owner && *new_owner) {
+                pa_log_debug("oFono appeared");
+                hf_audio_agent_register(backend);
+            }
+        }
+
+    }
+
+fail:
+    dbus_error_free(&err);
     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 

commit c4c4de532d3837fcc42a50536130a14ed48f5153
Author: João Paulo Rechi Vita <jprvita at openbossa.org>
Date:   Wed Sep 10 11:48:30 2014 +0200

    bluetooth: Implement transport release for hf_audio_agent transports

diff --git a/src/modules/bluetooth/backend-ofono.c b/src/modules/bluetooth/backend-ofono.c
index 518e775..bc92f4a 100644
--- a/src/modules/bluetooth/backend-ofono.c
+++ b/src/modules/bluetooth/backend-ofono.c
@@ -188,6 +188,22 @@ static int hf_audio_agent_transport_acquire(pa_bluetooth_transport *t, bool opti
 }
 
 static void hf_audio_agent_transport_release(pa_bluetooth_transport *t) {
+    struct hf_audio_card *card = t->userdata;
+
+    pa_assert(card);
+
+    if (t->state <= PA_BLUETOOTH_TRANSPORT_STATE_IDLE) {
+        pa_log_info("Transport %s already released", t->path);
+        return;
+    }
+
+    if (card->fd < 0)
+        return;
+
+    /* shutdown to make sure connection is dropped immediately */
+    shutdown(card->fd, SHUT_RDWR);
+    close(card->fd);
+    card->fd = -1;
 }
 
 static void hf_audio_agent_card_found(pa_bluetooth_backend *backend, const char *path, DBusMessageIter *props_i) {

commit 8dd4aa1f006b31ccee0bf17081e961ae86bb48c6
Author: João Paulo Rechi Vita <jprvita at openbossa.org>
Date:   Wed Sep 10 11:48:29 2014 +0200

    bluetooth: Implement transport acquire for hf_audio_agent transports

diff --git a/src/modules/bluetooth/backend-ofono.c b/src/modules/bluetooth/backend-ofono.c
index 02c2b65..518e775 100644
--- a/src/modules/bluetooth/backend-ofono.c
+++ b/src/modules/bluetooth/backend-ofono.c
@@ -23,9 +23,13 @@
 #include <config.h>
 #endif
 
+#include <errno.h>
+#include <poll.h>
+
 #include <pulsecore/core-util.h>
 #include <pulsecore/dbus-shared.h>
 #include <pulsecore/shared.h>
+#include <pulsecore/core-error.h>
 
 #include "bluez5-util.h"
 
@@ -118,8 +122,69 @@ static void hf_audio_card_free(struct hf_audio_card *card) {
     pa_xfree(card);
 }
 
+static int socket_accept(int sock)
+{
+    char c;
+    struct pollfd pfd;
+
+    if (sock < 0)
+        return -ENOTCONN;
+
+    memset(&pfd, 0, sizeof(pfd));
+    pfd.fd = sock;
+    pfd.events = POLLOUT;
+
+    if (poll(&pfd, 1, 0) < 0)
+        return -errno;
+
+    /*
+     * If socket already writable then it is not in defer setup state,
+     * otherwise it needs to be read to authorize the connection.
+     */
+    if ((pfd.revents & POLLOUT))
+        return 0;
+
+    /* Enable socket by reading 1 byte */
+    if (read(sock, &c, 1) < 0)
+        return -errno;
+
+    return 0;
+}
+
 static int hf_audio_agent_transport_acquire(pa_bluetooth_transport *t, bool optional, size_t *imtu, size_t *omtu) {
-    return -1;
+    struct hf_audio_card *card = t->userdata;
+    int err;
+
+    pa_assert(card);
+
+    if (!optional) {
+        DBusMessage *m;
+
+        pa_assert_se(m = dbus_message_new_method_call(t->owner, t->path, "org.ofono.HandsfreeAudioCard", "Connect"));
+        pa_assert_se(dbus_connection_send(pa_dbus_connection_get(card->backend->connection), m, NULL));
+
+        return -1;
+    }
+
+    /* The correct block size should take into account the SCO MTU from
+     * the Bluetooth adapter and (for adapters in the USB bus) the MxPS
+     * value from the Isoc USB endpoint in use by btusb and should be
+     * made available to userspace by the Bluetooth kernel subsystem.
+     * Meanwhile the empiric value 48 will be used. */
+    if (imtu)
+        *imtu = 48;
+    if (omtu)
+        *omtu = 48;
+
+    t->codec = card->codec;
+
+    err = socket_accept(card->fd);
+    if (err < 0) {
+        pa_log_error("Deferred setup failed on fd %d: %s", card->fd, pa_cstrerror(-err));
+        return -1;
+    }
+
+    return card->fd;
 }
 
 static void hf_audio_agent_transport_release(pa_bluetooth_transport *t) {

commit c09866582063ff06f60b4c88e0de71fae989981f
Author: João Paulo Rechi Vita <jprvita at openbossa.org>
Date:   Wed Sep 10 11:48:28 2014 +0200

    bluetooth: Parse HandsfreeAudioCard properties

diff --git a/src/modules/bluetooth/backend-ofono.c b/src/modules/bluetooth/backend-ofono.c
index 0da7c18..02c2b65 100644
--- a/src/modules/bluetooth/backend-ofono.c
+++ b/src/modules/bluetooth/backend-ofono.c
@@ -25,6 +25,7 @@
 
 #include <pulsecore/core-util.h>
 #include <pulsecore/dbus-shared.h>
+#include <pulsecore/shared.h>
 
 #include "bluez5-util.h"
 
@@ -56,6 +57,18 @@
     "  </interface>"                                                \
     "</node>"
 
+struct hf_audio_card {
+    pa_bluetooth_backend *backend;
+    char *path;
+    char *remote_address;
+    char *local_address;
+
+    int fd;
+    uint8_t codec;
+
+    pa_bluetooth_transport *transport;
+};
+
 struct pa_bluetooth_backend {
     pa_core *core;
     pa_bluetooth_discovery *discovery;
@@ -83,6 +96,97 @@ static pa_dbus_pending* hf_dbus_send_and_add_to_pending(pa_bluetooth_backend *ba
     return p;
 }
 
+static struct hf_audio_card *hf_audio_card_new(pa_bluetooth_backend *backend, const char *path) {
+    struct hf_audio_card *card = pa_xnew0(struct hf_audio_card, 1);
+
+    card->path = pa_xstrdup(path);
+    card->backend = backend;
+    card->fd = -1;
+
+    return card;
+}
+
+static void hf_audio_card_free(struct hf_audio_card *card) {
+    pa_assert(card);
+
+    if (card->transport)
+        pa_bluetooth_transport_free(card->transport);
+
+    pa_xfree(card->path);
+    pa_xfree(card->remote_address);
+    pa_xfree(card->local_address);
+    pa_xfree(card);
+}
+
+static int hf_audio_agent_transport_acquire(pa_bluetooth_transport *t, bool optional, size_t *imtu, size_t *omtu) {
+    return -1;
+}
+
+static void hf_audio_agent_transport_release(pa_bluetooth_transport *t) {
+}
+
+static void hf_audio_agent_card_found(pa_bluetooth_backend *backend, const char *path, DBusMessageIter *props_i) {
+    DBusMessageIter i, value_i;
+    const char *key, *value;
+    struct hf_audio_card *card;
+    pa_bluetooth_device *d;
+
+    pa_assert(backend);
+    pa_assert(path);
+    pa_assert(props_i);
+
+    pa_log_debug("New HF card found: %s", path);
+
+    card = hf_audio_card_new(backend, path);
+
+    while (dbus_message_iter_get_arg_type(props_i) != DBUS_TYPE_INVALID) {
+        char c;
+
+        dbus_message_iter_recurse(props_i, &i);
+
+        dbus_message_iter_get_basic(&i, &key);
+        dbus_message_iter_next(&i);
+        dbus_message_iter_recurse(&i, &value_i);
+
+        if ((c = dbus_message_iter_get_arg_type(&value_i)) != DBUS_TYPE_STRING) {
+            pa_log_error("Invalid properties for %s: expected 's', received '%c'", path, c);
+            goto fail;
+        }
+
+        dbus_message_iter_get_basic(&value_i, &value);
+
+        if (pa_streq(key, "RemoteAddress")) {
+            pa_xfree(card->remote_address);
+            card->remote_address = pa_xstrdup(value);
+        } else if (pa_streq(key, "LocalAddress")) {
+            pa_xfree(card->local_address);
+            card->local_address = pa_xstrdup(value);
+        }
+
+        pa_log_debug("%s: %s", key, value);
+
+        dbus_message_iter_next(props_i);
+    }
+
+    pa_hashmap_put(backend->cards, card->path, card);
+
+    d = pa_bluetooth_discovery_get_device_by_address(backend->discovery, card->remote_address, card->local_address);
+    if (d) {
+        card->transport = pa_bluetooth_transport_new(d, backend->ofono_bus_id, path, PA_BLUETOOTH_PROFILE_HEADSET_AUDIO_GATEWAY, NULL, 0);
+        card->transport->acquire = hf_audio_agent_transport_acquire;
+        card->transport->release = hf_audio_agent_transport_release;
+        card->transport->userdata = card;
+
+        pa_bluetooth_transport_put(card->transport);
+    } else
+        pa_log_error("Device doesnt exist for %s", path);
+
+    return;
+
+fail:
+    hf_audio_card_free(card);
+}
+
 static void hf_audio_agent_get_cards_reply(DBusPendingCall *pending, void *userdata) {
     DBusMessage *r;
     pa_dbus_pending *p;
@@ -114,7 +218,7 @@ static void hf_audio_agent_get_cards_reply(DBusPendingCall *pending, void *userd
 
         dbus_message_iter_recurse(&struct_i, &props_i);
 
-        /* TODO: Parse HandsfreeAudioCard properties */
+        hf_audio_agent_card_found(backend, path, &props_i);
 
         dbus_message_iter_next(&array_i);
     }
@@ -295,7 +399,8 @@ pa_bluetooth_backend *pa_bluetooth_backend_new(pa_core *c, pa_bluetooth_discover
     backend = pa_xnew0(pa_bluetooth_backend, 1);
     backend->core = c;
     backend->discovery = y;
-    backend->cards = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+    backend->cards = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL,
+                                         (pa_free_cb_t) hf_audio_card_free);
 
     dbus_error_init(&err);
 

commit a5a0506c4e51a3254b4ac3267bd275c65a01ec34
Author: João Paulo Rechi Vita <jprvita at openbossa.org>
Date:   Wed Sep 10 11:48:27 2014 +0200

    bluetooth: List HandsfreeAudioCard objects from oFono

diff --git a/src/modules/bluetooth/backend-ofono.c b/src/modules/bluetooth/backend-ofono.c
index fb66fc7..0da7c18 100644
--- a/src/modules/bluetooth/backend-ofono.c
+++ b/src/modules/bluetooth/backend-ofono.c
@@ -83,6 +83,58 @@ static pa_dbus_pending* hf_dbus_send_and_add_to_pending(pa_bluetooth_backend *ba
     return p;
 }
 
+static void hf_audio_agent_get_cards_reply(DBusPendingCall *pending, void *userdata) {
+    DBusMessage *r;
+    pa_dbus_pending *p;
+    pa_bluetooth_backend *backend;
+    DBusMessageIter i, array_i, struct_i, props_i;
+
+    pa_assert_se(p = userdata);
+    pa_assert_se(backend = p->context_data);
+    pa_assert_se(r = dbus_pending_call_steal_reply(pending));
+
+    if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
+        pa_log_error("Failed to get a list of handsfree audio cards from ofono: %s: %s",
+                     dbus_message_get_error_name(r), pa_dbus_get_error_message(r));
+        goto finish;
+    }
+
+    if (!dbus_message_iter_init(r, &i) || !pa_streq(dbus_message_get_signature(r), "a(oa{sv})")) {
+        pa_log_error("Invalid arguments in GetCards() reply");
+        goto finish;
+    }
+
+    dbus_message_iter_recurse(&i, &array_i);
+    while (dbus_message_iter_get_arg_type(&array_i) != DBUS_TYPE_INVALID) {
+        const char *path;
+
+        dbus_message_iter_recurse(&array_i, &struct_i);
+        dbus_message_iter_get_basic(&struct_i, &path);
+        dbus_message_iter_next(&struct_i);
+
+        dbus_message_iter_recurse(&struct_i, &props_i);
+
+        /* TODO: Parse HandsfreeAudioCard properties */
+
+        dbus_message_iter_next(&array_i);
+    }
+
+finish:
+    dbus_message_unref(r);
+
+    PA_LLIST_REMOVE(pa_dbus_pending, backend->pending, p);
+    pa_dbus_pending_free(p);
+}
+
+static void hf_audio_agent_get_cards(pa_bluetooth_backend *hf) {
+    DBusMessage *m;
+
+    pa_assert(hf);
+
+    pa_assert_se(m = dbus_message_new_method_call(OFONO_SERVICE, "/", HF_AUDIO_MANAGER_INTERFACE, "GetCards"));
+    hf_dbus_send_and_add_to_pending(hf, m, hf_audio_agent_get_cards_reply, NULL);
+}
+
 static void hf_audio_agent_register_reply(DBusPendingCall *pending, void *userdata) {
     DBusMessage *r;
     pa_dbus_pending *p;
@@ -100,7 +152,7 @@ static void hf_audio_agent_register_reply(DBusPendingCall *pending, void *userda
 
     backend->ofono_bus_id = pa_xstrdup(dbus_message_get_sender(r));
 
-    /* TODO: List all HandsfreeAudioCard objects */
+    hf_audio_agent_get_cards(backend);
 
 finish:
     dbus_message_unref(r);

commit 374c28a40eea2a608d7bc066803588b5d843df08
Author: João Paulo Rechi Vita <jprvita at openbossa.org>
Date:   Wed Sep 10 11:48:26 2014 +0200

    bluetooth: Register/Unregister Handsfree Audio Agent with oFono
    
    Register as a HandsfreeAudioAgent with oFono during backend
    initialization and unregiter during backend finalization. This commit
    also adds a check when receiving method calls or signals to make sure
    the sender matches with the D-Bus service we're registered with.

diff --git a/src/modules/bluetooth/backend-ofono.c b/src/modules/bluetooth/backend-ofono.c
index 1a4416e..fb66fc7 100644
--- a/src/modules/bluetooth/backend-ofono.c
+++ b/src/modules/bluetooth/backend-ofono.c
@@ -28,6 +28,9 @@
 
 #include "bluez5-util.h"
 
+#define HFP_AUDIO_CODEC_CVSD    0x01
+#define HFP_AUDIO_CODEC_MSBC    0x02
+
 #define OFONO_SERVICE "org.ofono"
 #define HF_AUDIO_AGENT_INTERFACE OFONO_SERVICE ".HandsfreeAudioAgent"
 #define HF_AUDIO_MANAGER_INTERFACE OFONO_SERVICE ".HandsfreeAudioManager"
@@ -58,6 +61,7 @@ struct pa_bluetooth_backend {
     pa_bluetooth_discovery *discovery;
     pa_dbus_connection *connection;
     pa_hashmap *cards;
+    char *ofono_bus_id;
 
     PA_LLIST_HEAD(pa_dbus_pending, pending);
 };
@@ -79,20 +83,114 @@ static pa_dbus_pending* hf_dbus_send_and_add_to_pending(pa_bluetooth_backend *ba
     return p;
 }
 
+static void hf_audio_agent_register_reply(DBusPendingCall *pending, void *userdata) {
+    DBusMessage *r;
+    pa_dbus_pending *p;
+    pa_bluetooth_backend *backend;
+
+    pa_assert_se(p = userdata);
+    pa_assert_se(backend = p->context_data);
+    pa_assert_se(r = dbus_pending_call_steal_reply(pending));
+
+    if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
+        pa_log_error("Failed to register as a handsfree audio agent with ofono: %s: %s",
+                     dbus_message_get_error_name(r), pa_dbus_get_error_message(r));
+        goto finish;
+    }
+
+    backend->ofono_bus_id = pa_xstrdup(dbus_message_get_sender(r));
+
+    /* TODO: List all HandsfreeAudioCard objects */
+
+finish:
+    dbus_message_unref(r);
+
+    PA_LLIST_REMOVE(pa_dbus_pending, backend->pending, p);
+    pa_dbus_pending_free(p);
+}
+
+static void hf_audio_agent_register(pa_bluetooth_backend *hf) {
+    DBusMessage *m;
+    uint8_t codecs[2];
+    const uint8_t *pcodecs = codecs;
+    int ncodecs = 0;
+    const char *path = HF_AUDIO_AGENT_PATH;
+
+    pa_assert(hf);
+
+    pa_assert_se(m = dbus_message_new_method_call(OFONO_SERVICE, "/", HF_AUDIO_MANAGER_INTERFACE, "Register"));
+
+    codecs[ncodecs++] = HFP_AUDIO_CODEC_CVSD;
+
+    pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &pcodecs, ncodecs,
+                                          DBUS_TYPE_INVALID));
+
+    hf_dbus_send_and_add_to_pending(hf, m, hf_audio_agent_register_reply, NULL);
+}
+
+static void hf_audio_agent_unregister(pa_bluetooth_backend *backend) {
+    DBusMessage *m;
+    const char *path = HF_AUDIO_AGENT_PATH;
+
+    pa_assert(backend);
+    pa_assert(backend->connection);
+
+    if (backend->ofono_bus_id) {
+        pa_assert_se(m = dbus_message_new_method_call(backend->ofono_bus_id, "/", HF_AUDIO_MANAGER_INTERFACE, "Unregister"));
+        pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID));
+        pa_assert_se(dbus_connection_send(pa_dbus_connection_get(backend->connection), m, NULL));
+
+        pa_xfree(backend->ofono_bus_id);
+        backend->ofono_bus_id = NULL;
+    }
+}
+
 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *data) {
+    const char *sender;
+    pa_bluetooth_backend *backend = data;
+
     pa_assert(bus);
     pa_assert(m);
+    pa_assert(backend);
+
+    sender = dbus_message_get_sender(m);
+    if (!pa_safe_streq(backend->ofono_bus_id, sender) && !pa_streq("org.freedesktop.DBus", sender))
+        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 
 static DBusMessage *hf_audio_agent_release(DBusConnection *c, DBusMessage *m, void *data) {
-    DBusMessage *r = dbus_message_new_error(m, "org.ofono.Error.NotImplemented", "Operation is not implemented");
+    DBusMessage *r;
+    const char *sender;
+    pa_bluetooth_backend *backend = data;
+
+    pa_assert(backend);
+
+    sender = dbus_message_get_sender(m);
+    if (!pa_safe_streq(backend->ofono_bus_id, sender)) {
+        pa_assert_se(r = dbus_message_new_error(m, "org.ofono.Error.NotAllowed", "Operation is not allowed by this sender"));
+        return r;
+    }
+
+    r = dbus_message_new_error(m, "org.ofono.Error.NotImplemented", "Operation is not implemented");
     return r;
 }
 
 static DBusMessage *hf_audio_agent_new_connection(DBusConnection *c, DBusMessage *m, void *data) {
-    DBusMessage *r = dbus_message_new_error(m, "org.ofono.Error.NotImplemented", "Operation is not implemented");
+    DBusMessage *r;
+    const char *sender;
+    pa_bluetooth_backend *backend = data;
+
+    pa_assert(backend);
+
+    sender = dbus_message_get_sender(m);
+    if (!pa_safe_streq(backend->ofono_bus_id, sender)) {
+        pa_assert_se(r = dbus_message_new_error(m, "org.ofono.Error.NotAllowed", "Operation is not allowed by this sender"));
+        return r;
+    }
+
+    r = dbus_message_new_error(m, "org.ofono.Error.NotImplemented", "Operation is not implemented");
     return r;
 }
 
@@ -180,6 +278,8 @@ pa_bluetooth_backend *pa_bluetooth_backend_new(pa_core *c, pa_bluetooth_discover
     pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(backend->connection), HF_AUDIO_AGENT_PATH,
                                                       &vtable_hf_audio_agent, backend));
 
+    hf_audio_agent_register(backend);
+
     return backend;
 }
 
@@ -188,6 +288,8 @@ void pa_bluetooth_backend_free(pa_bluetooth_backend *backend) {
 
     pa_dbus_free_pending_list(&backend->pending);
 
+    hf_audio_agent_unregister(backend);
+
     dbus_connection_unregister_object_path(pa_dbus_connection_get(backend->connection), HF_AUDIO_AGENT_PATH);
 
     pa_dbus_remove_matches(pa_dbus_connection_get(backend->connection),

commit d7a8ccce891454250a45e8ac7094cd4382aac3fa
Author: João Paulo Rechi Vita <jprvita at openbossa.org>
Date:   Wed Sep 10 11:48:25 2014 +0200

    bluetooth: Create hf_dbus_send_and_add_to_pending() for oFono backend

diff --git a/src/modules/bluetooth/backend-ofono.c b/src/modules/bluetooth/backend-ofono.c
index 99da783..1a4416e 100644
--- a/src/modules/bluetooth/backend-ofono.c
+++ b/src/modules/bluetooth/backend-ofono.c
@@ -58,8 +58,27 @@ struct pa_bluetooth_backend {
     pa_bluetooth_discovery *discovery;
     pa_dbus_connection *connection;
     pa_hashmap *cards;
+
+    PA_LLIST_HEAD(pa_dbus_pending, pending);
 };
 
+static pa_dbus_pending* hf_dbus_send_and_add_to_pending(pa_bluetooth_backend *backend, DBusMessage *m,
+                                                    DBusPendingCallNotifyFunction func, void *call_data) {
+    pa_dbus_pending *p;
+    DBusPendingCall *call;
+
+    pa_assert(backend);
+    pa_assert(m);
+
+    pa_assert_se(dbus_connection_send_with_reply(pa_dbus_connection_get(backend->connection), m, &call, -1));
+
+    p = pa_dbus_pending_new(pa_dbus_connection_get(backend->connection), m, call, backend, call_data);
+    PA_LLIST_PREPEND(pa_dbus_pending, backend->pending, p);
+    dbus_pending_call_set_notify(call, func, p, NULL);
+
+    return p;
+}
+
 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *data) {
     pa_assert(bus);
     pa_assert(m);
@@ -167,6 +186,8 @@ pa_bluetooth_backend *pa_bluetooth_backend_new(pa_core *c, pa_bluetooth_discover
 void pa_bluetooth_backend_free(pa_bluetooth_backend *backend) {
     pa_assert(backend);
 
+    pa_dbus_free_pending_list(&backend->pending);
+
     dbus_connection_unregister_object_path(pa_dbus_connection_get(backend->connection), HF_AUDIO_AGENT_PATH);
 
     pa_dbus_remove_matches(pa_dbus_connection_get(backend->connection),

commit 98d3d857058d08bd23f7f344312fcae7751f1b60
Author: João Paulo Rechi Vita <jprvita at openbossa.org>
Date:   Wed Sep 10 11:48:24 2014 +0200

    bluetooth: Monitor D-Bus signals

diff --git a/src/modules/bluetooth/backend-ofono.c b/src/modules/bluetooth/backend-ofono.c
index 99ff09d..99da783 100644
--- a/src/modules/bluetooth/backend-ofono.c
+++ b/src/modules/bluetooth/backend-ofono.c
@@ -30,6 +30,7 @@
 
 #define OFONO_SERVICE "org.ofono"
 #define HF_AUDIO_AGENT_INTERFACE OFONO_SERVICE ".HandsfreeAudioAgent"
+#define HF_AUDIO_MANAGER_INTERFACE OFONO_SERVICE ".HandsfreeAudioManager"
 
 #define HF_AUDIO_AGENT_PATH "/HandsfreeAudioAgent"
 
@@ -56,8 +57,16 @@ struct pa_bluetooth_backend {
     pa_core *core;
     pa_bluetooth_discovery *discovery;
     pa_dbus_connection *connection;
+    pa_hashmap *cards;
 };
 
+static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *data) {
+    pa_assert(bus);
+    pa_assert(m);
+
+    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
 static DBusMessage *hf_audio_agent_release(DBusConnection *c, DBusMessage *m, void *data) {
     DBusMessage *r = dbus_message_new_error(m, "org.ofono.Error.NotImplemented", "Operation is not implemented");
     return r;
@@ -117,12 +126,35 @@ pa_bluetooth_backend *pa_bluetooth_backend_new(pa_core *c, pa_bluetooth_discover
     backend = pa_xnew0(pa_bluetooth_backend, 1);
     backend->core = c;
     backend->discovery = y;
+    backend->cards = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
 
     dbus_error_init(&err);
 
     if (!(backend->connection = pa_dbus_bus_get(c, DBUS_BUS_SYSTEM, &err))) {
         pa_log("Failed to get D-Bus connection: %s", err.message);
         dbus_error_free(&err);
+        pa_xfree(backend);
+        return NULL;
+    }
+
+    /* dynamic detection of handsfree audio cards */
+    if (!dbus_connection_add_filter(pa_dbus_connection_get(backend->connection), filter_cb, backend, NULL)) {
+        pa_log_error("Failed to add filter function");
+        pa_dbus_connection_unref(backend->connection);
+        pa_xfree(backend);
+        return NULL;
+    }
+
+    if (pa_dbus_add_matches(pa_dbus_connection_get(backend->connection), &err,
+            "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',"
+            "arg0='" OFONO_SERVICE "'",
+            "type='signal',sender='" OFONO_SERVICE "',interface='" HF_AUDIO_MANAGER_INTERFACE "',member='CardAdded'",
+            "type='signal',sender='" OFONO_SERVICE "',interface='" HF_AUDIO_MANAGER_INTERFACE "',member='CardRemoved'",
+            NULL) < 0) {
+        pa_log("Failed to add oFono D-Bus matches: %s", err.message);
+        dbus_connection_remove_filter(pa_dbus_connection_get(backend->connection), filter_cb, backend);
+        pa_dbus_connection_unref(backend->connection);
+        pa_xfree(backend);
         return NULL;
     }
 
@@ -135,11 +167,20 @@ pa_bluetooth_backend *pa_bluetooth_backend_new(pa_core *c, pa_bluetooth_discover
 void pa_bluetooth_backend_free(pa_bluetooth_backend *backend) {
     pa_assert(backend);
 
-    if (backend->connection) {
-        dbus_connection_unregister_object_path(pa_dbus_connection_get(backend->connection), HF_AUDIO_AGENT_PATH);
+    dbus_connection_unregister_object_path(pa_dbus_connection_get(backend->connection), HF_AUDIO_AGENT_PATH);
 
-        pa_dbus_connection_unref(backend->connection);
-    }
+    pa_dbus_remove_matches(pa_dbus_connection_get(backend->connection),
+            "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',"
+            "arg0='" OFONO_SERVICE "'",
+            "type='signal',sender='" OFONO_SERVICE "',interface='" HF_AUDIO_MANAGER_INTERFACE "',member='CardAdded'",
+            "type='signal',sender='" OFONO_SERVICE "',interface='" HF_AUDIO_MANAGER_INTERFACE "',member='CardRemoved'",
+            NULL);
+
+    dbus_connection_remove_filter(pa_dbus_connection_get(backend->connection), filter_cb, backend);
+
+    pa_dbus_connection_unref(backend->connection);
+
+    pa_hashmap_free(backend->cards);
 
     pa_xfree(backend);
 }



More information about the pulseaudio-commits mailing list