[PATCH v3 15/18] tagstruct: Add pa_tagstruct_put/get_direction()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This will be used for dealing with node directions. Someone might ask
what's the point of adding functions for serializing just one byte;
the point is the validation in pa_tagstruct_get_direction(). It's nice
to be able to assume that the function will return a valid direction,
instead of an arbitrary 8-bit value.
---
 src/pulsecore/tagstruct.c | 33 +++++++++++++++++++++++++++++++++
 src/pulsecore/tagstruct.h |  4 ++++
 2 files changed, 37 insertions(+)

diff --git a/src/pulsecore/tagstruct.c b/src/pulsecore/tagstruct.c
index 762947a..ded4157 100644
--- a/src/pulsecore/tagstruct.c
+++ b/src/pulsecore/tagstruct.c
@@ -304,6 +304,15 @@ void pa_tagstruct_put_format_info(pa_tagstruct *t, pa_format_info *f) {
     pa_tagstruct_put_proplist(t, f->plist);
 }
 
+void pa_tagstruct_put_direction(pa_tagstruct *t, pa_direction_t direction) {
+    pa_assert(t);
+
+    extend(t, 2);
+
+    t->data[t->length++] = PA_TAG_DIRECTION;
+    t->data[t->length++] = (uint8_t) direction;
+}
+
 int pa_tagstruct_gets(pa_tagstruct*t, const char **s) {
     int error = 0;
     size_t n;
@@ -677,6 +686,30 @@ fail:
     return -1;
 }
 
+int pa_tagstruct_get_direction(pa_tagstruct *t, pa_direction_t *direction) {
+    uint8_t u;
+    uint8_t mask = PA_DIRECTION_OUTPUT | PA_DIRECTION_INPUT;
+
+    pa_assert(t);
+    pa_assert(direction);
+
+    if (t->rindex + 2 > t->length)
+        return -1;
+
+    if (t->data[t->rindex] != PA_TAG_DIRECTION)
+        return -1;
+
+    u = t->data[t->rindex + 1];
+
+    if (!(u & mask) || (u & ~mask))
+        return -1;
+
+    *direction = (pa_direction_t) u;
+    t->rindex += 2;
+
+    return 0;
+}
+
 void pa_tagstruct_put(pa_tagstruct *t, ...) {
     va_list va;
     pa_assert(t);
diff --git a/src/pulsecore/tagstruct.h b/src/pulsecore/tagstruct.h
index 5f729bc..6cc9d42 100644
--- a/src/pulsecore/tagstruct.h
+++ b/src/pulsecore/tagstruct.h
@@ -26,6 +26,7 @@
 #include <sys/types.h>
 #include <sys/time.h>
 
+#include <pulse/def.h>
 #include <pulse/sample.h>
 #include <pulse/format.h>
 #include <pulse/channelmap.h>
@@ -60,6 +61,7 @@ enum {
     PA_TAG_PROPLIST = 'P',
     PA_TAG_VOLUME = 'V',
     PA_TAG_FORMAT_INFO = 'f',
+    PA_TAG_DIRECTION = 'd',
 };
 
 pa_tagstruct *pa_tagstruct_new(const uint8_t* data, size_t length);
@@ -86,6 +88,7 @@ void pa_tagstruct_put_cvolume(pa_tagstruct *t, const pa_cvolume *cvolume);
 void pa_tagstruct_put_proplist(pa_tagstruct *t, pa_proplist *p);
 void pa_tagstruct_put_volume(pa_tagstruct *t, pa_volume_t volume);
 void pa_tagstruct_put_format_info(pa_tagstruct *t, pa_format_info *f);
+void pa_tagstruct_put_direction(pa_tagstruct *t, pa_direction_t direction);
 
 int pa_tagstruct_get(pa_tagstruct *t, ...);
 
@@ -104,5 +107,6 @@ int pa_tagstruct_get_cvolume(pa_tagstruct *t, pa_cvolume *v);
 int pa_tagstruct_get_proplist(pa_tagstruct *t, pa_proplist *p);
 int pa_tagstruct_get_volume(pa_tagstruct *t, pa_volume_t *v);
 int pa_tagstruct_get_format_info(pa_tagstruct *t, pa_format_info *f);
+int pa_tagstruct_get_direction(pa_tagstruct *t, pa_direction_t *direction);
 
 #endif
-- 
1.8.1.2



[Index of Archives]     [Linux Audio Users]     [AMD Graphics]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux