[pulseaudio-discuss] [RFC v0 1/3] bluetooth: Refactor property parsing code

Mikel Astiz mikel.astiz.oss at gmail.com
Wed Jul 25 07:29:52 PDT 2012


From: Mikel Astiz <mikel.astiz at bmw-carit.de>

Use a separate method to perform the common checks when a property needs
to be parsed.
---
 src/modules/bluetooth/bluetooth-util.c |   64 ++++++++++++--------------------
 1 files changed, 24 insertions(+), 40 deletions(-)

diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c
index cc3b8c6..65a6fe8 100644
--- a/src/modules/bluetooth/bluetooth-util.c
+++ b/src/modules/bluetooth/bluetooth-util.c
@@ -184,31 +184,42 @@ static pa_bool_t device_is_audio(pa_bluetooth_device *d) {
           d->headset_state != PA_BT_AUDIO_STATE_INVALID)));
 }
 
-static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device *d, DBusMessageIter *i) {
+static const char *check_variant_property(DBusMessageIter *i) {
     const char *key;
-    DBusMessageIter variant_i;
 
-    pa_assert(y);
-    pa_assert(d);
     pa_assert(i);
 
     if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) {
         pa_log("Property name not a string.");
-        return -1;
+        return NULL;
     }
 
     dbus_message_iter_get_basic(i, &key);
 
     if (!dbus_message_iter_next(i)) {
         pa_log("Property value missing");
-        return -1;
+        return NULL;
     }
 
     if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_VARIANT) {
         pa_log("Property value not a variant.");
-        return -1;
+        return NULL;
     }
 
+    return key;
+}
+
+static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device *d, DBusMessageIter *i) {
+    const char *key;
+    DBusMessageIter variant_i;
+
+    pa_assert(y);
+    pa_assert(d);
+
+    key = check_variant_property(i);
+    if (key == NULL)
+	return -1;
+
     dbus_message_iter_recurse(i, &variant_i);
 
 /*     pa_log_debug("Parsing property org.bluez.Device.%s", key); */
@@ -327,24 +338,10 @@ static int parse_audio_property(pa_bluetooth_discovery *u, int *state, DBusMessa
 
     pa_assert(u);
     pa_assert(state);
-    pa_assert(i);
-
-    if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) {
-        pa_log("Property name not a string.");
-        return -1;
-    }
 
-    dbus_message_iter_get_basic(i, &key);
-
-    if (!dbus_message_iter_next(i)) {
-        pa_log("Property value missing");
-        return -1;
-    }
-
-    if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_VARIANT) {
-        pa_log("Property value not a variant.");
-        return -1;
-    }
+    key = check_variant_property(i);
+    if (key == NULL)
+	return -1;
 
     dbus_message_iter_recurse(i, &variant_i);
 
@@ -735,22 +732,9 @@ int pa_bluetooth_transport_parse_property(pa_bluetooth_transport *t, DBusMessage
     const char *key;
     DBusMessageIter variant_i;
 
-    if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) {
-        pa_log("Property name not a string.");
-        return -1;
-    }
-
-    dbus_message_iter_get_basic(i, &key);
-
-    if (!dbus_message_iter_next(i))  {
-        pa_log("Property value missing");
-        return -1;
-    }
-
-    if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_VARIANT) {
-        pa_log("Property value not a variant.");
-        return -1;
-    }
+    key = check_variant_property(i);
+    if (key == NULL)
+	return -1;
 
     dbus_message_iter_recurse(i, &variant_i);
 
-- 
1.7.7.6



More information about the pulseaudio-discuss mailing list