Skip to content

Commit

Permalink
Add a small framework to decode fields generically
Browse files Browse the repository at this point in the history
v2: Fix rebase fail that removed a necessary hunk

Signed-off-by: Damien Lespiau <damien.lespiau@intel.com>
Reviewed-by: Adam Jackson <ajax@redhat.com>
  • Loading branch information
Damien Lespiau committed Aug 19, 2013
1 parent d1fc2b7 commit 70f8ab8
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions edid-decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,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];
Expand Down

0 comments on commit 70f8ab8

Please sign in to comment.