[edid-decode 6/7] Add a small framework to decode fields generically

Damien Lespiau damien.lespiau at intel.com
Wed Aug 7 06:58:22 PDT 2013


Signed-off-by: Damien Lespiau <damien.lespiau at intel.com>
---
 edid-decode.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 67 insertions(+), 2 deletions(-)

diff --git a/edid-decode.c b/edid-decode.c
index 5061228..7aed3c6 100644
--- a/edid-decode.c
+++ b/edid-decode.c
@@ -32,8 +32,6 @@
 #include <time.h>
 #include <ctype.h>
 
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
-
 static int claims_one_point_oh = 0;
 static int claims_one_point_two = 0;
 static int claims_one_point_three = 0;
@@ -65,6 +63,73 @@ static int warning_zero_preferred_refresh = 0;
 
 static int conformant = 1;
 
+struct value {
+    int value;
+    const char *description;
+};
+
+struct field {
+    const char *name;
+    int start, end;
+    struct value *values;
+    int n_values;
+};
+
+#define DEFINE_FIELD(n, var, s, e, ...)				\
+    static struct value var##_values[] =  {			\
+        __VA_ARGS__						\
+    };								\
+    static struct field var = {					\
+        .name = n,						\
+        .start = s,		        			\
+        .end = e,						\
+        .values = var##_values,	        			\
+        .n_values = ARRAY_SIZE(var##_values),			\
+    }
+
+static void
+decode_value(struct field *field, int val, const char *prefix)
+{
+    struct value *v;
+    int i;
+
+    for (i = 0; i < field->n_values; i++) {
+        v = &field->values[i];
+
+        if (v->value == val)
+           break;
+    }
+
+    if (i == field->n_values) {
+       printf("%s%s: %d\n", prefix, field->name, val);
+       return;
+    }
+
+    printf("%s%s: %s (%d)\n", prefix, field->name, v->description, val);
+}
+
+static void
+_decode(struct field **fields, int n_fields, int data, const char *prefix)
+{
+    int i;
+
+    for (i = 0; i < n_fields; i++) {
+	struct field *f = fields[i];
+	int field_length = f->end - f->start + 1;
+	int val;
+
+        if (field_length == 32)
+            val = data;
+        else
+            val = (data >> f->start) & ((1 << field_length) - 1);
+
+        decode_value(f, val, prefix);
+    }
+}
+
+#define decode(fields, data, prefix)    \
+    _decode(fields, ARRAY_SIZE(fields), data, prefix)
+
 static char *manufacturer_name(unsigned char *x)
 {
     static char name[4];
-- 
1.8.3.1



More information about the dri-devel mailing list