bluetooth: Monitor D-Bus signals
authorJoão Paulo Rechi Vita <jprvita@openbossa.org>
Wed, 10 Jul 2013 15:18:07 +0000 (12:18 -0300)
committerJaska Uimonen <jaska.uimonen@intel.com>
Fri, 7 Mar 2014 10:43:58 +0000 (12:43 +0200)
src/modules/bluetooth/hfaudioagent-ofono.c

index af78d4d..0e3ffc4 100644 (file)
@@ -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"
 
 struct hf_audio_agent_data {
     pa_core *core;
     pa_dbus_connection *connection;
+
+    bool filter_added;
+    pa_hashmap *hf_audio_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;
@@ -115,6 +126,7 @@ hf_audio_agent_data *hf_audio_agent_init(pa_core *c) {
 
     hfdata = pa_xnew0(hf_audio_agent_data, 1);
     hfdata->core = c;
+    hfdata->hf_audio_cards = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
 
     dbus_error_init(&err);
 
@@ -124,6 +136,25 @@ hf_audio_agent_data *hf_audio_agent_init(pa_core *c) {
         return NULL;
     }
 
+    /* dynamic detection of handsfree audio cards */
+    if (!dbus_connection_add_filter(pa_dbus_connection_get(hfdata->connection), filter_cb, hfdata, NULL)) {
+        pa_log_error("Failed to add filter function");
+        hf_audio_agent_done(hfdata);
+        return NULL;
+    }
+    hfdata->filter_added = true;
+
+    if (pa_dbus_add_matches(pa_dbus_connection_get(hfdata->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);
+        hf_audio_agent_done(hfdata);
+        return NULL;
+    }
+
     pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(hfdata->connection), HF_AUDIO_AGENT_PATH,
                                                       &vtable_hf_audio_agent, hfdata));
 
@@ -135,7 +166,24 @@ void hf_audio_agent_done(hf_audio_agent_data *data) {
 
     pa_assert(hfdata);
 
+    if (hfdata->hf_audio_cards) {
+        pa_hashmap_free(hfdata->hf_audio_cards, NULL);
+        hfdata->hf_audio_cards = NULL;
+    }
+
     if (hfdata->connection) {
+
+        pa_dbus_remove_matches(
+            pa_dbus_connection_get(hfdata->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);
+
+        if (hfdata->filter_added)
+            dbus_connection_remove_filter(pa_dbus_connection_get(hfdata->connection), filter_cb, hfdata);
+
         dbus_connection_unregister_object_path(pa_dbus_connection_get(hfdata->connection), HF_AUDIO_AGENT_PATH);
 
         pa_dbus_connection_unref(hfdata->connection);