Commit 5b7e2b76 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Changed generated C code to use reader/writer interface and moved

labcomm_encoder/labcomm_decoder into labcomm.c
parent 88bfe766
...@@ -443,7 +443,7 @@ aspect C_Decoder { ...@@ -443,7 +443,7 @@ aspect C_Decoder {
env = env.nestStruct("v"); env = env.nestStruct("v");
env.println("static void decode_" + getName() + "("); env.println("static void decode_" + getName() + "(");
env.indent(); env.indent();
env.println("struct labcomm_decoder *d,"); env.println("struct labcomm_reader *r,");
env.println("void (*handle)("); env.println("void (*handle)(");
env.indent(); env.indent();
env.println(env.prefix + getName() + " *v,"); env.println(env.prefix + getName() + " *v,");
...@@ -479,7 +479,7 @@ aspect C_Decoder { ...@@ -479,7 +479,7 @@ aspect C_Decoder {
} }
public void PrimType.C_emitDecoder(C_env env) { public void PrimType.C_emitDecoder(C_env env) {
env.println(env.qualid + " = labcomm_decode_" + getName() + "(d);"); env.println(env.qualid + " = labcomm_read_" + getName() + "(r);");
} }
public void UserType.C_emitDecoder(C_env env) { public void UserType.C_emitDecoder(C_env env) {
...@@ -525,8 +525,7 @@ aspect C_Decoder { ...@@ -525,8 +525,7 @@ aspect C_Decoder {
} }
public void VariableSize.C_emitDecoderDecodeLimit(C_env env, int i) { public void VariableSize.C_emitDecoderDecodeLimit(C_env env, int i) {
//env.println(env.qualid + ".n_" + i + " = labcomm_decode_int(d);"); env.println(env.qualid + ".n_" + i + " = labcomm_read_packed32(r);");
env.println(env.qualid + ".n_" + i + " = labcomm_decode_packed32(d);");
} }
public void ArrayType.C_emitDecoderDecodeLimit(C_env env) { public void ArrayType.C_emitDecoderDecodeLimit(C_env env) {
...@@ -700,7 +699,7 @@ aspect C_Encoder { ...@@ -700,7 +699,7 @@ aspect C_Encoder {
env = env.nestStruct("(*v)"); env = env.nestStruct("(*v)");
env.println("static int encode_" + getName() + "("); env.println("static int encode_" + getName() + "(");
env.indent(); env.indent();
env.println("struct labcomm_encoder *e,"); env.println("struct labcomm_writer *w,");
env.println(env.prefix + getName() + " *v"); env.println(env.prefix + getName() + " *v");
env.unindent(); env.unindent();
env.println(")"); env.println(")");
...@@ -739,8 +738,8 @@ aspect C_Encoder { ...@@ -739,8 +738,8 @@ aspect C_Encoder {
} }
public void PrimType.C_emitEncoder(C_env env) { public void PrimType.C_emitEncoder(C_env env) {
env.println("result = labcomm_encode_" + getName() + env.println("result = labcomm_write_" + getName() +
"(e, " + env.qualid + ");"); "(w, " + env.qualid + ");");
env.println("if (result != 0) { return result; }"); env.println("if (result != 0) { return result; }");
} }
...@@ -786,8 +785,7 @@ aspect C_Encoder { ...@@ -786,8 +785,7 @@ aspect C_Encoder {
} }
public void VariableSize.C_emitEncoderEncodeLimit(C_env env, int i) { public void VariableSize.C_emitEncoderEncodeLimit(C_env env, int i) {
//env.println("labcomm_encode_int(e, " + env.qualid + ".n_" + i + ");"); env.println("labcomm_write_packed32(w, " + env.qualid + ".n_" + i + ");");
env.println("labcomm_encode_packed32(e, " + env.qualid + ".n_" + i + ");");
} }
public void ArrayType.C_emitEncoderEncodeLimit(C_env env) { public void ArrayType.C_emitEncoderEncodeLimit(C_env env) {
......
...@@ -36,6 +36,27 @@ ...@@ -36,6 +36,27 @@
#define LABCOMM_VERSION "LabComm2013" #define LABCOMM_VERSION "LabComm2013"
struct labcomm_decoder {
void *context;
struct labcomm_reader reader;
struct {
void *context;
const struct labcomm_lock_action *action;
} lock;
labcomm_error_handler_callback on_error;
labcomm_handle_new_datatype_callback on_new_datatype;
};
struct labcomm_encoder {
void *context;
struct labcomm_writer writer;
struct {
void *context;
const struct labcomm_lock_action *action;
} lock;
labcomm_error_handler_callback on_error;
};
struct labcomm_sample_entry { struct labcomm_sample_entry {
struct labcomm_sample_entry *next; struct labcomm_sample_entry *next;
int index; int index;
...@@ -408,7 +429,7 @@ int labcomm_internal_encode( ...@@ -408,7 +429,7 @@ int labcomm_internal_encode(
if (result != 0) { goto out; } if (result != 0) { goto out; }
result = labcomm_write_packed32(&e->writer, index); result = labcomm_write_packed32(&e->writer, index);
if (result != 0) { goto out; } if (result != 0) { goto out; }
result = encode(e, value); result = encode(&e->writer, value);
out: out:
e->writer.action.end(&e->writer); e->writer.action.end(&e->writer);
no_end: no_end:
...@@ -633,7 +654,7 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d) ...@@ -633,7 +654,7 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
d->on_error(LABCOMM_ERROR_DEC_TYPE_NOT_FOUND, 3, "%s(): type not found (id=0x%x)\n", __FUNCTION__, result); d->on_error(LABCOMM_ERROR_DEC_TYPE_NOT_FOUND, 3, "%s(): type not found (id=0x%x)\n", __FUNCTION__, result);
result = -ENOENT; result = -ENOENT;
} else { } else {
entry->decoder(d, entry->handler, entry->context); entry->decoder(&d->reader, entry->handler, entry->context);
} }
} }
} }
......
...@@ -50,21 +50,10 @@ ...@@ -50,21 +50,10 @@
typedef void (*labcomm_handler_function)(void *value, void *context); typedef void (*labcomm_handler_function)(void *value, void *context);
typedef void (*labcomm_decoder_function)( typedef void (*labcomm_decoder_function)(
struct labcomm_decoder *d, struct labcomm_reader *r,
labcomm_handler_function handler, labcomm_handler_function handler,
void *context); void *context);
struct labcomm_decoder {
void *context;
struct labcomm_reader reader;
struct {
void *context;
const struct labcomm_lock_action *action;
} lock;
labcomm_error_handler_callback on_error;
labcomm_handle_new_datatype_callback on_new_datatype;
};
/* /*
* Non typesafe registration function to be called from * Non typesafe registration function to be called from
* generated labcomm_decoder_register_* functions. * generated labcomm_decoder_register_* functions.
...@@ -94,9 +83,6 @@ int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder, ...@@ -94,9 +83,6 @@ int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder,
r->pos++; \ r->pos++; \
} \ } \
return result; \ return result; \
} \
static inline type labcomm_decode_##name(struct labcomm_decoder *d) { \
return labcomm_read_##name(&d->reader); \
} }
#else #else
...@@ -112,9 +98,6 @@ int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder, ...@@ -112,9 +98,6 @@ int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder,
r->pos++; \ r->pos++; \
} \ } \
return result; \ return result; \
} \
static inline type labcomm_decode_##name(struct labcomm_decoder *d) { \
return labcomm_read_##name(&d->reader); \
} }
#endif #endif
...@@ -147,11 +130,6 @@ static inline unsigned int labcomm_read_packed32(struct labcomm_reader *r) ...@@ -147,11 +130,6 @@ static inline unsigned int labcomm_read_packed32(struct labcomm_reader *r)
return result; return result;
} }
static inline unsigned int labcomm_decode_packed32(struct labcomm_decoder *d)
{
return labcomm_read_packed32(&d->reader);
}
static inline char *labcomm_read_string(struct labcomm_reader *r) static inline char *labcomm_read_string(struct labcomm_reader *r)
{ {
char *result; char *result;
...@@ -170,28 +148,13 @@ static inline char *labcomm_read_string(struct labcomm_reader *r) ...@@ -170,28 +148,13 @@ static inline char *labcomm_read_string(struct labcomm_reader *r)
return result; return result;
} }
static inline char *labcomm_decode_string(struct labcomm_decoder *d)
{
return labcomm_read_string(&d->reader);
}
/* /*
* Semi private encoder declarations * Semi private encoder declarations
*/ */
typedef int (*labcomm_encoder_function)( typedef int (*labcomm_encoder_function)(
struct labcomm_encoder *, struct labcomm_writer *,
void *value); void *value);
struct labcomm_encoder {
void *context;
struct labcomm_writer writer;
struct {
void *context;
const struct labcomm_lock_action *action;
} lock;
labcomm_error_handler_callback on_error;
};
void labcomm_internal_encoder_register( void labcomm_internal_encoder_register(
struct labcomm_encoder *encoder, struct labcomm_encoder *encoder,
struct labcomm_signature *signature, struct labcomm_signature *signature,
...@@ -223,9 +186,6 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder, ...@@ -223,9 +186,6 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
w->pos++; \ w->pos++; \
} \ } \
return 0; \ return 0; \
} \
static inline int labcomm_encode_##name(struct labcomm_encoder *e, type data) { \
return labcomm_write_##name(&e->writer, data); \
} }
#else #else
...@@ -243,9 +203,6 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder, ...@@ -243,9 +203,6 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
w->pos++; \ w->pos++; \
} \ } \
return 0; \ return 0; \
} \
static inline int labcomm_encode_##name(struct labcomm_encoder *e, type data) { \
return labcomm_write_##name(&e->writer, data); \
} }
#endif #endif
...@@ -278,14 +235,6 @@ static inline int labcomm_write_packed32(struct labcomm_writer *w, ...@@ -278,14 +235,6 @@ static inline int labcomm_write_packed32(struct labcomm_writer *w,
return 0; return 0;
} }
static inline int labcomm_encode_packed32(struct labcomm_encoder *e,
unsigned int data)
{
return labcomm_write_packed32(&e->writer, data);
}
static inline int labcomm_write_string(struct labcomm_writer *w, char *s) static inline int labcomm_write_string(struct labcomm_writer *w, char *s)
{ {
int length, i, err; int length, i, err;
...@@ -305,10 +254,4 @@ static inline int labcomm_write_string(struct labcomm_writer *w, char *s) ...@@ -305,10 +254,4 @@ static inline int labcomm_write_string(struct labcomm_writer *w, char *s)
return 0; return 0;
} }
static inline int labcomm_encode_string(struct labcomm_encoder *e,
char *s)
{
return labcomm_write_string(&e->writer, s);
}
#endif #endif
...@@ -6,34 +6,25 @@ ...@@ -6,34 +6,25 @@
static int line; static int line;
static unsigned char buffer[128]; static unsigned char buffer[128];
static struct labcomm_encoder encoder = {
static struct labcomm_writer writer = {
.context = NULL, .context = NULL,
.writer = { .data = buffer,
.context = NULL, .data_size = sizeof(buffer),
.data = buffer, .count = sizeof(buffer),
.data_size = sizeof(buffer), .pos = 0,
.count = sizeof(buffer), .error = 0,
.pos = 0, .action = { NULL, NULL, NULL, NULL, NULL, NULL },
.error = 0,
.action = { NULL, NULL, NULL, NULL, NULL, NULL },
.on_error = NULL,
},
.lock = { NULL, NULL },
.on_error = NULL, .on_error = NULL,
}; };
static struct labcomm_decoder decoder = { static struct labcomm_reader reader = {
.context = NULL, .context = NULL,
.reader = { .data = buffer,
.context = NULL, .data_size = sizeof(buffer),
.data = buffer, .count = 0,
.data_size = sizeof(buffer), .pos = 0,
.count = 0, .action = { NULL, NULL, NULL, NULL, NULL, NULL },
.pos = 0,
.action = { NULL, NULL, NULL, NULL, NULL, NULL },
.on_error = NULL,
},
.lock = { NULL, NULL },
.on_error = NULL, .on_error = NULL,
}; };
...@@ -43,15 +34,15 @@ typedef uint32_t packed32; ...@@ -43,15 +34,15 @@ typedef uint32_t packed32;
{ \ { \
type decoded; \ type decoded; \
line = __LINE__; \ line = __LINE__; \
encoder.writer.pos = 0; \ writer.pos = 0; \
labcomm_encode_##ltype(&encoder, value); \ labcomm_write_##ltype(&writer, value); \
writer_assert(#ltype, expect_count, (uint8_t*)expect_bytes); \ writer_assert(#ltype, expect_count, (uint8_t*)expect_bytes); \
decoder.reader.count = encoder.writer.pos; \ reader.count = writer.pos; \
decoder.reader.pos = 0; \ reader.pos = 0; \
decoded = labcomm_decode_##ltype(&decoder); \ decoded = labcomm_read_##ltype(&reader); \
if (decoded != value) { \ if (decoded != value) { \
fprintf(stderr, "Decode error" format " != " format " @%s:%d \n", value, decoded, \ fprintf(stderr, "Decode error" format " != " format " @%s:%d \n", \
__FILE__, __LINE__); \ value, decoded, __FILE__, __LINE__); \
exit(1); \ exit(1); \
} \ } \
} }
...@@ -60,18 +51,18 @@ static void writer_assert(char *type, ...@@ -60,18 +51,18 @@ static void writer_assert(char *type,
int count, int count,
uint8_t *bytes) uint8_t *bytes)
{ {
if (encoder.writer.pos != count) { if (writer.pos != count) {
fprintf(stderr, fprintf(stderr,
"Wrong number of bytes written for '%s' (%d != %d) @%s:%d\n", "Wrong number of bytes written for '%s' (%d != %d) @%s:%d\n",
type, encoder.writer.pos, count, __FILE__, line); type, writer.pos, count, __FILE__, line);
exit(1); exit(1);
} }
if (memcmp(encoder.writer.data, bytes, count) != 0) { if (memcmp(writer.data, bytes, count) != 0) {
int i; int i;
fprintf(stderr, "Wrong bytes written for '%s' ( ", type); fprintf(stderr, "Wrong bytes written for '%s' ( ", type);
for (i = 0 ; i < count ; i++) { for (i = 0 ; i < count ; i++) {
fprintf(stderr, "%2.2x ", encoder.writer.data[i]); fprintf(stderr, "%2.2x ", writer.data[i]);
} }
fprintf(stderr, "!= "); fprintf(stderr, "!= ");
for (i = 0 ; i < count ; i++) { for (i = 0 ; i < count ; i++) {
......
...@@ -9,9 +9,11 @@ ...@@ -9,9 +9,11 @@
#define IOCTL_WRITER_RESET 4097 #define IOCTL_WRITER_RESET 4097
static unsigned char buffer[128]; static unsigned char buffer[128];
struct labcomm_writer *writer;
static int buf_writer_alloc(struct labcomm_writer *w, char *labcomm_version) static int buf_writer_alloc(struct labcomm_writer *w, char *labcomm_version)
{ {
writer = w; /* Hack */
w->data_size = sizeof(buffer); w->data_size = sizeof(buffer);
w->count = w->data_size; w->count = w->data_size;
w->data = buffer; w->data = buffer;
...@@ -112,8 +114,8 @@ void dump_encoder(struct labcomm_encoder *encoder) ...@@ -112,8 +114,8 @@ void dump_encoder(struct labcomm_encoder *encoder)
{ {
int i; int i;
for (i = 0 ; i < encoder->writer.pos ; i++) { for (i = 0 ; i < writer->pos ; i++) {
printf("%2.2x ", encoder->writer.data[i]); printf("%2.2x ", writer->data[i]);
} }
printf("\n"); printf("\n");
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment