]> code.delx.au - pulseaudio/blobdiff - src/modules/bluetooth/bluez5-util.c
bluetooth: Remove redundant assignments
[pulseaudio] / src / modules / bluetooth / bluez5-util.c
index 225e5c828f1156d27d0270bf5988624198304c00..89244a0e2ead0b2f89eb239650a4d46331240c5e 100644 (file)
@@ -395,8 +395,8 @@ pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_address(pa_bluetooth_d
     pa_assert(local);
 
     while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
-        if (pa_streq(d->address, remote) && pa_streq(d->adapter->address, local))
-            return d->device_info_valid == 1 ? d : NULL;
+        if (d->device_info_valid == 1 && pa_streq(d->address, remote) && pa_streq(d->adapter->address, local))
+            return d;
 
     return NULL;
 }
@@ -419,11 +419,10 @@ static void device_free(pa_bluetooth_device *d) {
     if (d->uuids)
         pa_hashmap_free(d->uuids);
 
-    d->discovery = NULL;
-    d->adapter = NULL;
     pa_xfree(d->path);
     pa_xfree(d->alias);
     pa_xfree(d->address);
+    pa_xfree(d->adapter_path);
     pa_xfree(d);
 }
 
@@ -438,16 +437,20 @@ static void device_remove(pa_bluetooth_discovery *y, const char *path) {
     }
 }
 
-static void device_remove_all(pa_bluetooth_discovery *y) {
-    pa_bluetooth_device *d;
+static void set_device_info_valid(pa_bluetooth_device *device, int valid) {
+    bool old_any_connected;
 
-    pa_assert(y);
+    pa_assert(device);
+    pa_assert(valid == -1 || valid == 0 || valid == 1);
 
-    while ((d = pa_hashmap_steal_first(y->devices))) {
-        d->device_info_valid = -1;
-        pa_hook_fire(&y->hooks[PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED], d);
-        device_free(d);
-   }
+    if (valid == device->device_info_valid)
+        return;
+
+    old_any_connected = pa_bluetooth_device_any_transport_connected(device);
+    device->device_info_valid = valid;
+
+    if (pa_bluetooth_device_any_transport_connected(device) != old_any_connected)
+        pa_hook_fire(&device->discovery->hooks[PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED], device);
 }
 
 static pa_bluetooth_adapter* adapter_create(pa_bluetooth_discovery *y, const char *path) {
@@ -473,8 +476,10 @@ static void adapter_free(pa_bluetooth_adapter *a) {
     pa_assert(a->discovery);
 
     PA_HASHMAP_FOREACH(d, a->discovery->devices, state)
-        if (d->adapter == a)
+        if (d->adapter == a) {
+            set_device_info_valid(d, -1);
             d->adapter = NULL;
+        }
 
     pa_xfree(a->path);
     pa_xfree(a->address);
@@ -492,17 +497,6 @@ static void adapter_remove(pa_bluetooth_discovery *y, const char *path) {
     }
 }
 
-static void adapter_remove_all(pa_bluetooth_discovery *y) {
-    pa_bluetooth_adapter *a;
-
-    pa_assert(y);
-
-    /* When this function is called all devices have already been freed */
-
-    while ((a = pa_hashmap_steal_first(y->adapters)))
-        adapter_free(a);
-}
-
 static void parse_device_property(pa_bluetooth_device *d, DBusMessageIter *i, bool is_property_change) {
     const char *key;
     DBusMessageIter variant_i;
@@ -618,7 +612,6 @@ static void parse_device_property(pa_bluetooth_device *d, DBusMessageIter *i, bo
 
 static int parse_device_properties(pa_bluetooth_device *d, DBusMessageIter *i, bool is_property_change) {
     DBusMessageIter element_i;
-    int ret = 0;
 
     dbus_message_iter_recurse(i, &element_i);
 
@@ -632,12 +625,17 @@ static int parse_device_properties(pa_bluetooth_device *d, DBusMessageIter *i, b
 
     if (!d->address || !d->adapter_path || !d->alias) {
         pa_log_error("Non-optional information missing for device %s", d->path);
-        d->device_info_valid = -1;
+        set_device_info_valid(d, -1);
         return -1;
     }
 
-    d->device_info_valid = 1;
-    return ret;
+    if (!is_property_change && d->adapter)
+        set_device_info_valid(d, 1);
+
+    /* If d->adapter is NULL, device_info_valid will be left as 0, and updated
+     * after all interfaces have been parsed. */
+
+    return 0;
 }
 
 static void parse_adapter_properties(pa_bluetooth_adapter *a, DBusMessageIter *i, bool is_property_change) {
@@ -662,7 +660,7 @@ static void parse_adapter_properties(pa_bluetooth_adapter *a, DBusMessageIter *i
         dbus_message_iter_recurse(&dict_i, &variant_i);
 
         if (dbus_message_iter_get_arg_type(&variant_i) == DBUS_TYPE_STRING && pa_streq(key, "Address")) {
-            char *value;
+            const char *value;
 
             if (is_property_change) {
                 pa_log_warn("Adapter property 'Address' expected to be constant but changed for %s, ignoring", a->path);
@@ -815,14 +813,19 @@ static void parse_interfaces_and_properties(pa_bluetooth_discovery *y, DBusMessa
         dbus_message_iter_next(&element_i);
     }
 
-    PA_HASHMAP_FOREACH(d, y->devices, state)
+    PA_HASHMAP_FOREACH(d, y->devices, state) {
+        if (d->device_info_valid != 0)
+            continue;
+
         if (!d->adapter && d->adapter_path) {
             d->adapter = pa_hashmap_get(d->discovery->adapters, d->adapter_path);
-            if (!d->adapter) {
-                pa_log_error("Device %s is child of nonexistent adapter %s", d->path, d->adapter_path);
-                d->device_info_valid = -1;
-            }
+            if (!d->adapter || !d->adapter->address) {
+                pa_log_error("Device %s is child of nonexistent or corrupted adapter %s", d->path, d->adapter_path);
+                set_device_info_valid(d, -1);
+            } else
+                set_device_info_valid(d, 1);
         }
+    }
 
     return;
 }
@@ -914,8 +917,8 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *us
         if (pa_streq(name, BLUEZ_SERVICE)) {
             if (old_owner && *old_owner) {
                 pa_log_debug("Bluetooth daemon disappeared");
-                device_remove_all(y);
-                adapter_remove_all(y);
+                pa_hashmap_remove_all(y->devices);
+                pa_hashmap_remove_all(y->adapters);
                 y->objects_listed = false;
             }
 
@@ -1228,7 +1231,7 @@ static DBusMessage *endpoint_set_configuration(DBusConnection *conn, DBusMessage
             goto fail2;
         }
     } else {
-        /* InterfacesAdded signal is probably on it's way, device_info_valid is kept as 0. */
+        /* InterfacesAdded signal is probably on its way, device_info_valid is kept as 0. */
         pa_log_warn("SetConfiguration() received for unknown device %s", dev_path);
         d = device_create(y, dev_path);
     }
@@ -1517,8 +1520,10 @@ pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
     y = pa_xnew0(pa_bluetooth_discovery, 1);
     PA_REFCNT_INIT(y);
     y->core = c;
-    y->adapters = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
-    y->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+    y->adapters = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL,
+                                      (pa_free_cb_t) adapter_free);
+    y->devices = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL,
+                                     (pa_free_cb_t) device_free);
     y->transports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
     PA_LLIST_HEAD_INIT(pa_dbus_pending, y->pending);
 
@@ -1593,15 +1598,11 @@ void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) {
 
     pa_dbus_free_pending_list(&y->pending);
 
-    if (y->devices) {
-        device_remove_all(y);
+    if (y->devices)
         pa_hashmap_free(y->devices);
-    }
 
-    if (y->adapters) {
-        adapter_remove_all(y);
+    if (y->adapters)
         pa_hashmap_free(y->adapters);
-    }
 
     if (y->transports) {
         pa_assert(pa_hashmap_isempty(y->transports));