From d3e25d39d764a0804d289187c819b8c74ea4ba49 Mon Sep 17 00:00:00 2001 From: Anders Blomdell <anders.blomdell@control.lth.se> Date: Wed, 15 Oct 2014 11:29:41 +0200 Subject: [PATCH] Version string encapsulated with tag and length, hence version string not passed to labcomm_writer_alloc any more. --- examples/twoway/decimating.c | 36 ++-- examples/twoway/introspecting.c | 12 +- lib/c/2006/labcomm2006.c | 10 +- lib/c/2006/labcomm2006_decoder.c | 5 +- .../2006/labcomm2006_dynamic_buffer_writer.c | 3 +- lib/c/2006/labcomm2006_encoder.c | 2 +- lib/c/2006/labcomm2006_fd_reader.c | 12 +- lib/c/2006/labcomm2006_fd_writer.c | 15 +- lib/c/2006/labcomm2006_private.h | 16 +- lib/c/labcomm.c | 17 +- lib/c/labcomm_decoder.c | 37 +++- lib/c/labcomm_dynamic_buffer_writer.c | 3 +- lib/c/labcomm_encoder.c | 15 +- lib/c/labcomm_fd_reader.c | 14 +- lib/c/labcomm_fd_writer.c | 7 +- lib/c/labcomm_private.h | 33 ++- lib/c/test/test_labcomm.c | 201 +----------------- lib/c/test/test_labcomm_generated_encoding.c | 3 +- lib/csharp/se/lth/control/labcomm/Constant.cs | 19 +- .../se/lth/control/labcomm/DecoderChannel.cs | 12 +- .../se/lth/control/labcomm/EncoderChannel.cs | 7 +- lib/java/se/lth/control/labcomm/Constant.java | 18 +- .../lth/control/labcomm/DecoderChannel.java | 13 +- .../lth/control/labcomm/EncoderChannel.java | 7 +- lib/python/labcomm/LabComm.py | 78 +++---- lib/python/labcomm/StreamReader.py | 10 - lib/python/labcomm/StreamWriter.py | 7 - 27 files changed, 196 insertions(+), 416 deletions(-) diff --git a/examples/twoway/decimating.c b/examples/twoway/decimating.c index dbcc80c..32d1ab0 100644 --- a/examples/twoway/decimating.c +++ b/examples/twoway/decimating.c @@ -62,14 +62,13 @@ static void set_decimation( static int wrap_reader_alloc( struct labcomm_reader *r, - struct labcomm_reader_action_context *action_context, - char *labcomm_version) + struct labcomm_reader_action_context *action_context) { struct decimating_private *decimating = action_context->context; labcomm_decoder_register_decimating_messages_set_decimation( r->decoder, set_decimation, decimating); - return labcomm_reader_alloc(r, action_context->next, labcomm_version); + return labcomm_reader_alloc(r, action_context->next); } struct send_set_decimation { @@ -186,14 +185,13 @@ static void register_signatures(void *context) static int wrap_writer_alloc( struct labcomm_writer *w, - struct labcomm_writer_action_context *action_context, - char *labcomm_version) + struct labcomm_writer_action_context *action_context) { struct decimating_private *decimating = action_context->context; labcomm_scheduler_enqueue(decimating->scheduler, 0, register_signatures, decimating); - return labcomm_writer_alloc(w, action_context->next, labcomm_version); + return labcomm_writer_alloc(w, action_context->next); } static int wrap_writer_start( @@ -206,20 +204,22 @@ static int wrap_writer_start( struct decimation *decimation; int result; - labcomm_scheduler_data_lock(decimating->scheduler); - - decimation = LABCOMM_SIGNATURE_ARRAY_REF(decimating->memory, - decimating->writer_decimation, - struct decimation, index); - decimation->current++; - if (decimation->current < decimation->n) { - result = -EALREADY; - } else { - decimation->current = 0; + if (index < LABCOMM_USER) { result = 0; + } else { + labcomm_scheduler_data_lock(decimating->scheduler); + decimation = LABCOMM_SIGNATURE_ARRAY_REF(decimating->memory, + decimating->writer_decimation, + struct decimation, index); + decimation->current++; + if (decimation->current < decimation->n) { + result = -EALREADY; + } else { + decimation->current = 0; + result = 0; + } + labcomm_scheduler_data_unlock(decimating->scheduler); } - labcomm_scheduler_data_unlock(decimating->scheduler); - if (result == 0) { result = labcomm_writer_start(w, action_context->next, index, signature, value); diff --git a/examples/twoway/introspecting.c b/examples/twoway/introspecting.c index a1e1028..0f90b2b 100644 --- a/examples/twoway/introspecting.c +++ b/examples/twoway/introspecting.c @@ -124,15 +124,14 @@ static void handles_signature( static int wrap_reader_alloc( struct labcomm_reader *r, - struct labcomm_reader_action_context *action_context, - char *labcomm_version) + struct labcomm_reader_action_context *action_context) { struct introspecting_private *introspecting = action_context->context; labcomm_decoder_register_introspecting_messages_handles_signature( introspecting->introspecting.reader->decoder, handles_signature, introspecting); - return labcomm_reader_alloc(r, action_context->next, labcomm_version); + return labcomm_reader_alloc(r, action_context->next); } struct handles_signature { @@ -213,14 +212,13 @@ static void register_encoder_signatures(void *context) static int wrap_writer_alloc( struct labcomm_writer *w, - struct labcomm_writer_action_context *action_context, - char *labcomm_version) + struct labcomm_writer_action_context *action_context) { struct introspecting_private *introspecting = action_context->context; labcomm_scheduler_enqueue(introspecting->scheduler, 0, register_encoder_signatures, introspecting); - return labcomm_writer_alloc(w, action_context->next, labcomm_version); + return labcomm_writer_alloc(w, action_context->next); } static int wrap_writer_start( @@ -231,7 +229,7 @@ static int wrap_writer_start( { struct introspecting_private *introspecting = action_context->context; - if (value == NULL) { + if (index >= LABCOMM_USER && value == NULL) { struct local *local; labcomm_scheduler_data_lock(introspecting->scheduler); diff --git a/lib/c/2006/labcomm2006.c b/lib/c/2006/labcomm2006.c index cbb0ea8..111ec2f 100644 --- a/lib/c/2006/labcomm2006.c +++ b/lib/c/2006/labcomm2006.c @@ -48,10 +48,9 @@ } int labcomm2006_reader_alloc(struct labcomm2006_reader *r, - struct labcomm2006_reader_action_context *action_context, - char *labcomm2006_version) + struct labcomm2006_reader_action_context *action_context) { - UNWRAP(alloc, r, action_context, labcomm2006_version); + UNWRAP(alloc, r, action_context); } int labcomm2006_reader_free(struct labcomm2006_reader *r, @@ -92,10 +91,9 @@ int labcomm2006_reader_ioctl(struct labcomm2006_reader *r, } int labcomm2006_writer_alloc(struct labcomm2006_writer *w, - struct labcomm2006_writer_action_context *action_context, - char *labcomm2006_version) + struct labcomm2006_writer_action_context *action_context) { - UNWRAP(alloc, w, action_context, labcomm2006_version); + UNWRAP(alloc, w, action_context); } int labcomm2006_writer_free(struct labcomm2006_writer *w, diff --git a/lib/c/2006/labcomm2006_decoder.c b/lib/c/2006/labcomm2006_decoder.c index 36a075a..d472cdf 100644 --- a/lib/c/2006/labcomm2006_decoder.c +++ b/lib/c/2006/labcomm2006_decoder.c @@ -186,7 +186,7 @@ static int decode_typedef_or_sample(struct labcomm2006_decoder *d, int kind) local_signature = NULL; local_index = 0; - labcomm2006_writer_alloc(&writer, writer.action_context, ""); + labcomm2006_writer_alloc(&writer, writer.action_context); labcomm2006_writer_start(&writer, writer.action_context, 0, NULL, NULL); remote_index = labcomm2006_read_packed32(d->reader); signature.name = labcomm2006_read_string(d->reader); @@ -286,8 +286,7 @@ static void reader_alloc(struct labcomm2006_decoder *d) { if (!d->reader_allocated) { d->reader_allocated = 1; - labcomm2006_reader_alloc(d->reader, d->reader->action_context, - LABCOMM_VERSION); + labcomm2006_reader_alloc(d->reader, d->reader->action_context); } } diff --git a/lib/c/2006/labcomm2006_dynamic_buffer_writer.c b/lib/c/2006/labcomm2006_dynamic_buffer_writer.c index 02acb62..c931ec0 100644 --- a/lib/c/2006/labcomm2006_dynamic_buffer_writer.c +++ b/lib/c/2006/labcomm2006_dynamic_buffer_writer.c @@ -28,8 +28,7 @@ #include "labcomm2006_dynamic_buffer_writer.h" static int dyn_alloc(struct labcomm2006_writer *w, - struct labcomm2006_writer_action_context *action_context, - char *labcomm2006_version) + struct labcomm2006_writer_action_context *action_context) { w->data_size = 1000; w->count = w->data_size; diff --git a/lib/c/2006/labcomm2006_encoder.c b/lib/c/2006/labcomm2006_encoder.c index 28674e1..9e17a60 100644 --- a/lib/c/2006/labcomm2006_encoder.c +++ b/lib/c/2006/labcomm2006_encoder.c @@ -53,7 +53,7 @@ struct labcomm2006_encoder *labcomm2006_encoder_new( result->scheduler = scheduler; LABCOMM_SIGNATURE_ARRAY_INIT(result->registered, int); labcomm2006_writer_alloc(result->writer, - result->writer->action_context, LABCOMM_VERSION); + result->writer->action_context); } return result; } diff --git a/lib/c/2006/labcomm2006_fd_reader.c b/lib/c/2006/labcomm2006_fd_reader.c index 485be6b..15f1bfb 100644 --- a/lib/c/2006/labcomm2006_fd_reader.c +++ b/lib/c/2006/labcomm2006_fd_reader.c @@ -36,19 +36,9 @@ struct labcomm2006_fd_reader { }; static int fd_alloc(struct labcomm2006_reader *r, - struct labcomm2006_reader_action_context *action_context, - char *version) + struct labcomm2006_reader_action_context *action_context) { int result = 0; -// in-band version is not included in version 2006. -// This may be a place for version checking and/or printing -// a warning message - if (version && version[0]) { - if (strcmp(LABCOMM_VERSION, version) != 0) { - fprintf(stderr, "ERROR: version mismatch: %s != %s\n", version, LABCOMM_VERSION); - return -EINVAL; - } - } r->count = 0; r->pos = 0; diff --git a/lib/c/2006/labcomm2006_fd_writer.c b/lib/c/2006/labcomm2006_fd_writer.c index af058b9..69f30bb 100644 --- a/lib/c/2006/labcomm2006_fd_writer.c +++ b/lib/c/2006/labcomm2006_fd_writer.c @@ -40,20 +40,9 @@ static int fd_flush(struct labcomm2006_writer *w, struct labcomm2006_writer_action_context *action_context); static int fd_alloc(struct labcomm2006_writer *w, - struct labcomm2006_writer_action_context *action_context, - char *version) + struct labcomm2006_writer_action_context *action_context) { -// in-band version is not included in version 2006. -// This may be a place for version checking and/or printing -// a warning message - if (version && version[0]) { - if (strcmp(LABCOMM_VERSION, version) != 0) { - fprintf(stderr, "ERROR: version mismatch: %s != %s\n", version, LABCOMM_VERSION); - return -EINVAL; - } - } - - w->data = labcomm2006_memory_alloc(w->memory, 0, BUFFER_SIZE); + w->data = labcomm2006_memory_alloc(w->memory, 0, BUFFER_SIZE); if (! w->data) { w->error = -ENOMEM; w->data_size = 0; diff --git a/lib/c/2006/labcomm2006_private.h b/lib/c/2006/labcomm2006_private.h index fa2ec14..b8f7b78 100644 --- a/lib/c/2006/labcomm2006_private.h +++ b/lib/c/2006/labcomm2006_private.h @@ -96,17 +96,14 @@ struct labcomm2006_reader_action_context; struct labcomm2006_reader_action { /* 'alloc' is called at the first invocation of 'labcomm2006_decoder_decode_one' - on the decoder containing the reader. If 'labcomm2006_version' != NULL - and non-empty the transport layer may use it to ensure that - compatible versions are used. + on the decoder containing the reader. Returned value: > 0 Number of bytes allocated for buffering <= 0 Error */ int (*alloc)(struct labcomm2006_reader *r, - struct labcomm2006_reader_action_context *action_context, - char *labcomm2006_version); + struct labcomm2006_reader_action_context *action_context); /* 'free' returns the resources claimed by 'alloc' and might have other reader specific side-effects as well. @@ -159,8 +156,7 @@ struct labcomm2006_reader { }; int labcomm2006_reader_alloc(struct labcomm2006_reader *r, - struct labcomm2006_reader_action_context *action_context, - char *labcomm2006_version); + struct labcomm2006_reader_action_context *action_context); int labcomm2006_reader_free(struct labcomm2006_reader *r, struct labcomm2006_reader_action_context *action_context); int labcomm2006_reader_start(struct labcomm2006_reader *r, @@ -299,8 +295,7 @@ struct labcomm2006_writer_action_context; struct labcomm2006_writer_action { int (*alloc)(struct labcomm2006_writer *w, - struct labcomm2006_writer_action_context *action_context, - char *labcomm2006_version); + struct labcomm2006_writer_action_context *action_context); int (*free)(struct labcomm2006_writer *w, struct labcomm2006_writer_action_context *action_context); /* 'start' is called right before a sample is to be sent. In the @@ -345,8 +340,7 @@ struct labcomm2006_writer { }; int labcomm2006_writer_alloc(struct labcomm2006_writer *w, - struct labcomm2006_writer_action_context *action_context, - char *labcomm_version); + struct labcomm2006_writer_action_context *action_context); int labcomm2006_writer_free(struct labcomm2006_writer *w, struct labcomm2006_writer_action_context *action_context); int labcomm2006_writer_start(struct labcomm2006_writer *w, diff --git a/lib/c/labcomm.c b/lib/c/labcomm.c index fb51d38..8b9ade2 100644 --- a/lib/c/labcomm.c +++ b/lib/c/labcomm.c @@ -37,7 +37,7 @@ #include "labcomm_ioctl.h" #include "labcomm_dynamic_buffer_writer.h" -#define LABCOMM_VERSION "LabComm20141009" +#define CURRENT_VERSION "LabComm20141009" /* Unwrapping reader/writer functions */ #define UNWRAP_ac(rw, ac, ...) ac @@ -50,10 +50,9 @@ } int labcomm_reader_alloc(struct labcomm_reader *r, - struct labcomm_reader_action_context *action_context, - char *labcomm_version) + struct labcomm_reader_action_context *action_context) { - UNWRAP(alloc, r, action_context, labcomm_version); + UNWRAP(alloc, r, action_context); } int labcomm_reader_free(struct labcomm_reader *r, @@ -94,10 +93,9 @@ int labcomm_reader_ioctl(struct labcomm_reader *r, } int labcomm_writer_alloc(struct labcomm_writer *w, - struct labcomm_writer_action_context *action_context, - char *labcomm_version) + struct labcomm_writer_action_context *action_context) { - UNWRAP(alloc, w, action_context, labcomm_version); + UNWRAP(alloc, w, action_context); } int labcomm_writer_free(struct labcomm_writer *w, @@ -265,11 +263,6 @@ int labcomm_internal_sizeof(struct labcomm_signature *signature, void *v) { int length = signature->encoded_size(v); - fprintf(stderr, "SIZEOF(%s) = %d %d %d\n", - signature->name, - labcomm_size_packed32(signature->index), - labcomm_size_packed32(length), - length); return (labcomm_size_packed32(signature->index) + labcomm_size_packed32(length) + length); diff --git a/lib/c/labcomm_decoder.c b/lib/c/labcomm_decoder.c index db0d348..706ab20 100644 --- a/lib/c/labcomm_decoder.c +++ b/lib/c/labcomm_decoder.c @@ -18,7 +18,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#define LABCOMM_VERSION "LabComm20141009" +#define CURRENT_VERSION "LabComm20141009" #include <errno.h> #include "labcomm.h" @@ -37,6 +37,7 @@ struct sample_entry { struct labcomm_decoder { struct labcomm_reader *reader; int reader_allocated; + int version_ok; struct labcomm_error_handler *error; struct labcomm_memory *memory; struct labcomm_scheduler *scheduler; @@ -64,6 +65,7 @@ struct labcomm_decoder *labcomm_decoder_new( result->reader->pos = 0; result->reader->error = 0; result->reader_allocated = 0; + result->version_ok = 0; result->error = error; result->memory = memory; result->scheduler = scheduler; @@ -105,7 +107,7 @@ static int decode_sample(struct labcomm_decoder *d, int kind) signature.size = labcomm_read_packed32(d->reader); if (d->reader->error < 0) { result = d->reader->error; - goto out; + goto free_signature_name; } signature.signature = labcomm_memory_alloc(d->memory, 1, signature.size); if (d->reader->error < 0) { @@ -184,14 +186,13 @@ static void reader_alloc(struct labcomm_decoder *d) { if (!d->reader_allocated) { d->reader_allocated = 1; - labcomm_reader_alloc(d->reader, d->reader->action_context, - LABCOMM_VERSION); + labcomm_reader_alloc(d->reader, d->reader->action_context); } } int labcomm_decoder_decode_one(struct labcomm_decoder *d) { - int result, remote_index, length __attribute__((__unused__)); + int result, remote_index, length; reader_alloc(d); remote_index = labcomm_read_packed32(d->reader); @@ -204,8 +205,30 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d) result = d->reader->error; goto out; } - if (remote_index == LABCOMM_SAMPLE) { + if (remote_index == LABCOMM_VERSION) { + char *version = labcomm_read_string(d->reader); + if (d->reader->error < 0) { + result = d->reader->error; + goto out; + } + if (strcmp(version, CURRENT_VERSION) == 0) { + result = LABCOMM_VERSION; + d->version_ok = 1; + } else { + result = -ECONNRESET; + } + fprintf(stderr, "VERSION %s %d\n", version, result); + labcomm_memory_free(d->memory, 1, version); + } else if (! d->version_ok) { + fprintf(stderr, "No VERSION %d %d\n", remote_index, length); + result = -ECONNRESET; + } else if (remote_index == LABCOMM_SAMPLE) { result = decode_sample(d, remote_index); + } else if (remote_index == LABCOMM_PRAGMA && 0 /* d->pragma_handler*/) { + /* d->prama_handler(...); */ + } else if (remote_index < LABCOMM_USER) { + fprintf(stderr, "SKIP %d %d\n", remote_index, length); + result = remote_index; } else { int *local_index; struct call_handler_context wrap = { @@ -244,7 +267,7 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d) result = -ENOENT; } } -out: +out: return result; } diff --git a/lib/c/labcomm_dynamic_buffer_writer.c b/lib/c/labcomm_dynamic_buffer_writer.c index 3a7425d..8a9b6d1 100644 --- a/lib/c/labcomm_dynamic_buffer_writer.c +++ b/lib/c/labcomm_dynamic_buffer_writer.c @@ -28,8 +28,7 @@ #include "labcomm_dynamic_buffer_writer.h" static int dyn_alloc(struct labcomm_writer *w, - struct labcomm_writer_action_context *action_context, - char *labcomm_version) + struct labcomm_writer_action_context *action_context) { w->data_size = 1000; w->count = w->data_size; diff --git a/lib/c/labcomm_encoder.c b/lib/c/labcomm_encoder.c index 6daa1ae..e138610 100644 --- a/lib/c/labcomm_encoder.c +++ b/lib/c/labcomm_encoder.c @@ -18,7 +18,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#define LABCOMM_VERSION "LabComm20141009" +#define CURRENT_VERSION "LabComm20141009" #include <errno.h> #include "labcomm.h" @@ -43,6 +43,8 @@ struct labcomm_encoder *labcomm_encoder_new( result = labcomm_memory_alloc(memory, 0, sizeof(*result)); if (result) { + int length; + result->writer = writer; result->writer->encoder = result; result->writer->data = NULL; @@ -55,7 +57,16 @@ struct labcomm_encoder *labcomm_encoder_new( result->scheduler = scheduler; LABCOMM_SIGNATURE_ARRAY_INIT(result->registered, int); labcomm_writer_alloc(result->writer, - result->writer->action_context, LABCOMM_VERSION); + result->writer->action_context); + labcomm_writer_start(result->writer, + result->writer->action_context, + LABCOMM_VERSION, NULL, CURRENT_VERSION); + labcomm_write_packed32(result->writer, LABCOMM_VERSION); + length = (labcomm_size_packed32(LABCOMM_VERSION) + + labcomm_size_string(CURRENT_VERSION)); + labcomm_write_packed32(result->writer, length); + labcomm_write_string(result->writer, CURRENT_VERSION); + labcomm_writer_end(result->writer, result->writer->action_context); } return result; } diff --git a/lib/c/labcomm_fd_reader.c b/lib/c/labcomm_fd_reader.c index 65a9fbb..eaa59f9 100644 --- a/lib/c/labcomm_fd_reader.c +++ b/lib/c/labcomm_fd_reader.c @@ -36,8 +36,7 @@ struct labcomm_fd_reader { }; static int fd_alloc(struct labcomm_reader *r, - struct labcomm_reader_action_context *action_context, - char *version) + struct labcomm_reader_action_context *action_context) { int result = 0; @@ -51,17 +50,6 @@ static int fd_alloc(struct labcomm_reader *r, r->data_size = BUFFER_SIZE; result = r->data_size; - if (version && version[0]) { - char *tmp; - - tmp = labcomm_read_string(r); - if (strcmp(tmp, version) != 0) { - result = -EINVAL; - } else { - result = r->data_size; - } - labcomm_memory_free(r->memory, 1, tmp); - } } return result; } diff --git a/lib/c/labcomm_fd_writer.c b/lib/c/labcomm_fd_writer.c index 9ea707f..a395c54 100644 --- a/lib/c/labcomm_fd_writer.c +++ b/lib/c/labcomm_fd_writer.c @@ -40,8 +40,7 @@ static int fd_flush(struct labcomm_writer *w, struct labcomm_writer_action_context *action_context); static int fd_alloc(struct labcomm_writer *w, - struct labcomm_writer_action_context *action_context, - char *version) + struct labcomm_writer_action_context *action_context) { w->data = labcomm_memory_alloc(w->memory, 0, BUFFER_SIZE); if (! w->data) { @@ -53,10 +52,6 @@ static int fd_alloc(struct labcomm_writer *w, w->data_size = BUFFER_SIZE; w->count = BUFFER_SIZE; w->pos = 0; - if (version && version[0]) { - labcomm_write_string(w, version); - fd_flush(w, action_context); - } } return w->error; diff --git a/lib/c/labcomm_private.h b/lib/c/labcomm_private.h index c9d3c03..1ba35c7 100644 --- a/lib/c/labcomm_private.h +++ b/lib/c/labcomm_private.h @@ -37,15 +37,22 @@ #include "labcomm.h" /* - * Predeclared aggregate type indices + * Allowed packet tags */ -#define LABCOMM_TYPEDEF 0x01 +#define LABCOMM_VERSION 0x01 #define LABCOMM_SAMPLE 0x02 +#define LABCOMM_PRAGMA 0x3f +#define LABCOMM_USER 0x40 /* ..0xffffffff */ + + +/* + * Predefined aggregate type indices + */ #define LABCOMM_ARRAY 0x10 #define LABCOMM_STRUCT 0x11 /* - * Predeclared primitive type indices + * Predefined primitive type indices */ #define LABCOMM_BOOLEAN 0x20 #define LABCOMM_BYTE 0x21 @@ -56,10 +63,6 @@ #define LABCOMM_DOUBLE 0x26 #define LABCOMM_STRING 0x27 -/* - * Start index for user defined types - */ -#define LABCOMM_USER 0x40 /* * Macro to automagically call constructors in modules compiled @@ -96,17 +99,14 @@ struct labcomm_reader_action_context; struct labcomm_reader_action { /* 'alloc' is called at the first invocation of 'labcomm_decoder_decode_one' - on the decoder containing the reader. If 'labcomm_version' != NULL - and non-empty the transport layer may use it to ensure that - compatible versions are used. + on the decoder containing the reader. Returned value: > 0 Number of bytes allocated for buffering <= 0 Error */ int (*alloc)(struct labcomm_reader *r, - struct labcomm_reader_action_context *action_context, - char *labcomm_version); + struct labcomm_reader_action_context *action_context); /* 'free' returns the resources claimed by 'alloc' and might have other reader specific side-effects as well. @@ -159,8 +159,7 @@ struct labcomm_reader { }; int labcomm_reader_alloc(struct labcomm_reader *r, - struct labcomm_reader_action_context *action_context, - char *labcomm_version); + struct labcomm_reader_action_context *action_context); int labcomm_reader_free(struct labcomm_reader *r, struct labcomm_reader_action_context *action_context); int labcomm_reader_start(struct labcomm_reader *r, @@ -299,8 +298,7 @@ struct labcomm_writer_action_context; struct labcomm_writer_action { int (*alloc)(struct labcomm_writer *w, - struct labcomm_writer_action_context *action_context, - char *labcomm_version); + struct labcomm_writer_action_context *action_context); int (*free)(struct labcomm_writer *w, struct labcomm_writer_action_context *action_context); /* 'start' is called right before a sample is to be sent. In the @@ -345,8 +343,7 @@ struct labcomm_writer { }; int labcomm_writer_alloc(struct labcomm_writer *w, - struct labcomm_writer_action_context *action_context, - char *labcomm_version); + struct labcomm_writer_action_context *action_context); int labcomm_writer_free(struct labcomm_writer *w, struct labcomm_writer_action_context *action_context); int labcomm_writer_start(struct labcomm_writer *w, diff --git a/lib/c/test/test_labcomm.c b/lib/c/test/test_labcomm.c index a199c10..4d158a6 100644 --- a/lib/c/test/test_labcomm.c +++ b/lib/c/test/test_labcomm.c @@ -33,8 +33,7 @@ static unsigned char buffer[512]; static int writer_alloc(struct labcomm_writer *w, - struct labcomm_writer_action_context *action_context, - char *labcomm_version) + struct labcomm_writer_action_context *action_context) { w->data = buffer; w->data_size = sizeof(buffer); @@ -68,8 +67,7 @@ static struct labcomm_writer writer = { }; static int reader_alloc(struct labcomm_reader *r, - struct labcomm_reader_action_context *action_context, - char *labcomm_version) + struct labcomm_reader_action_context *action_context) { r->data = buffer; r->data_size = sizeof(buffer); @@ -127,8 +125,8 @@ int test_decode_one(struct labcomm_decoder *decoder) for (reader.count = 0 ; reader.count < writer.pos ; reader.count++) { reader.error = 0; reader.pos = 0; - result = labcomm_decoder_decode_one(decoder); - if (result >= 0) { + result = labcomm_decoder_decode_one(decoder); + if (result >= 0 ) { fprintf(stderr, "Got result from buffer with bogus length (%d)\n", result); exit(1); @@ -187,6 +185,9 @@ int main(void) labcomm_default_error_handler, labcomm_default_memory, labcomm_default_scheduler); + err = test_decode_one(decoder); + fprintf(stderr, "decode of version -> index %d\n", err); + writer.pos = 0; labcomm_decoder_register_test_sample_test_var(decoder, handle_test_var, NULL); @@ -208,191 +209,3 @@ int main(void) test_encode_decode(encoder, decoder, 8, 0, 4294967295); return 0; } - -#if 0 -#include <stdbool.h> -#include <stdlib.h> - -#include <labcomm.h> -#include <labcomm_mem_writer.h> -#include <labcomm_mem_reader.h> -#include "test/testdata/gen/test_sample.h" - -#define TEST_BUFFER_SIZE (50) - -void test_error_handler(enum labcomm_error error_id, size_t nbr_va_args, ...); - -int init_suit_labcomm() -{ - return 0; -} - -int clean_suit_labcomm() -{ - return 0; -} - -void setup_connected_encoder_decoder(struct labcomm_encoder **enc, - labcomm_mem_writer_context_t *enc_ctx, - struct labcomm_decoder **dec, - labcomm_mem_reader_context_t *dec_ctx) -{ - enc_ctx->write_pos = 0; - enc_ctx->buf = malloc(TEST_BUFFER_SIZE); - enc_ctx->length = TEST_BUFFER_SIZE; - - *enc = labcomm_encoder_new(labcomm_mem_writer, enc_ctx); - - dec_ctx->size = 0; - dec_ctx->enc_data = enc_ctx->buf; - *dec = labcomm_decoder_new(labcomm_mem_reader, dec_ctx); - - labcomm_register_error_handler_decoder(*dec, test_error_handler); - labcomm_register_error_handler_encoder(*enc, test_error_handler); -} - -static bool in_error = false; -static enum labcomm_error in_error_id = LABCOMM_ERROR_ENUM_BEGIN_GUARD; -void test_error_handler(enum labcomm_error error_id, size_t nbr_va_args, ...) -{ - in_error = true; - in_error_id = error_id; -} - -static bool got_sample = false; -void test_decoder_handle_test_sample_test_var(test_sample_test_var *v, void *ctx) -{ - got_sample = true; -} - -void test_decoder_decode_sig() -{ - labcomm_mem_writer_context_t enc_ctx; - struct labcomm_encoder *encoder; - labcomm_mem_reader_context_t dec_ctx; - struct labcomm_decoder *decoder; - setup_connected_encoder_decoder(&encoder, &enc_ctx, &decoder, &dec_ctx); - - labcomm_encoder_register_test_sample_test_var(encoder); - dec_ctx.size = enc_ctx.write_pos; - - labcomm_decoder_register_test_sample_test_var(decoder, - test_decoder_handle_test_sample_test_var, NULL); - labcomm_decoder_decode_one(decoder); - - CU_ASSERT_FALSE(in_error); - enc_ctx.write_pos = 0; - test_sample_test_var var = 1; - labcomm_encode_test_sample_test_var(encoder, &var); - dec_ctx.size = enc_ctx.write_pos; - labcomm_decoder_decode_one(decoder); - - CU_ASSERT_FALSE(in_error); - CU_ASSERT_FALSE(got_sample); - - labcomm_decoder_free(decoder); - labcomm_encoder_free(encoder); - free(enc_ctx.buf); - - in_error = false; - in_error_id = LABCOMM_ERROR_ENUM_BEGIN_GUARD; - got_sample = false; -} - -static bool got_new_datatype = false; -static labcomm_signature_t new_sig; -int test_new_datatype(struct labcomm_decoder *decoder, - labcomm_signature_t *sig) -{ - got_new_datatype = true; - memcpy(&new_sig, sig, sizeof(labcomm_signature_t)); - return 0; -} - -void test_decode_unreg_signature_handle() -{ - labcomm_mem_writer_context_t enc_ctx; - struct labcomm_encoder *encoder; - labcomm_mem_reader_context_t dec_ctx; - struct labcomm_decoder *decoder; - setup_connected_encoder_decoder(&encoder, &enc_ctx, &decoder, &dec_ctx); - - labcomm_encoder_register_test_sample_test_var(encoder); - dec_ctx.size = enc_ctx.write_pos; - labcomm_decoder_register_new_datatype_handler(decoder, test_new_datatype); - labcomm_decoder_decode_one(decoder); - - CU_ASSERT_TRUE(got_new_datatype); - CU_ASSERT_EQUAL( - memcmp(new_sig.signature, dec_ctx.enc_data, dec_ctx.size), 0); - - got_new_datatype = false; - labcomm_decoder_free(decoder); - labcomm_encoder_free(encoder); - free(enc_ctx.buf); -} - -void test_decode_unreg_signature_error() -{ - labcomm_mem_writer_context_t enc_ctx; - struct labcomm_encoder *encoder; - labcomm_mem_reader_context_t dec_ctx; - struct labcomm_decoder *decoder; - setup_connected_encoder_decoder(&encoder, &enc_ctx, &decoder, &dec_ctx); - - labcomm_encoder_register_test_sample_test_var(encoder); - dec_ctx.size = enc_ctx.write_pos; - - labcomm_decoder_decode_one(decoder); - - CU_ASSERT_TRUE(in_error); - CU_ASSERT_EQUAL(in_error_id, LABCOMM_ERROR_DEC_UNKNOWN_DATATYPE); - got_new_datatype = false; - labcomm_decoder_free(decoder); - labcomm_encoder_free(encoder); - free(enc_ctx.buf); -} -int main() -{ - CU_pSuite suite_decoder = NULL; - - // Initialize CUnit test registry. - if (CUE_SUCCESS != CU_initialize_registry()) { - return CU_get_error(); - } - - // Add our test suites. - suite_decoder = CU_add_suite("transport_enc_dec", - init_suit_labcomm, clean_suit_labcomm); - if (suite_decoder == NULL) { - CU_cleanup_registry(); - return CU_get_error(); - } - - if ( - (CU_add_test(suite_decoder, "test_decoder_decode_sig", - test_decoder_decode_sig) == NULL) - || - (CU_add_test(suite_decoder, "test_decode_unreg_signature_handle", - test_decode_unreg_signature_handle) == NULL) - || - (CU_add_test(suite_decoder, "test_decode_unreg_signature_error", - test_decode_unreg_signature_error) == NULL) - ) { - CU_cleanup_registry(); - return CU_get_error(); - } - - // Set verbosity. - CU_basic_set_mode(CU_BRM_VERBOSE); - /*CU_console_run_tests();*/ - - // Run all test suites. - CU_basic_run_tests(); - - // Clean up. - CU_cleanup_registry(); - - return CU_get_error(); -} -#endif diff --git a/lib/c/test/test_labcomm_generated_encoding.c b/lib/c/test/test_labcomm_generated_encoding.c index 6b9e8a3..8a85ada 100644 --- a/lib/c/test/test_labcomm_generated_encoding.c +++ b/lib/c/test/test_labcomm_generated_encoding.c @@ -37,8 +37,7 @@ struct labcomm_writer *writer; static int buf_writer_alloc( struct labcomm_writer *w, - struct labcomm_writer_action_context *action_context, - char *labcomm_version) + struct labcomm_writer_action_context *action_context) { writer = w; /* Hack */ w->data_size = sizeof(buffer); diff --git a/lib/csharp/se/lth/control/labcomm/Constant.cs b/lib/csharp/se/lth/control/labcomm/Constant.cs index e2f39a4..1b5a44c 100644 --- a/lib/csharp/se/lth/control/labcomm/Constant.cs +++ b/lib/csharp/se/lth/control/labcomm/Constant.cs @@ -2,13 +2,19 @@ namespace se.lth.control.labcomm { public class Constant { - public const string VERSION = "LabComm20141009"; + public const string CURRENT_VERSION = "LabComm20141009"; - /* - * Predeclared aggregate type indices + /* + * Allowed packet tags */ - public const int TYPEDEF = 0x01; + public const int VERSION = 0x01; public const int SAMPLE = 0x02; + public const int PRAGMA = 0x3f; + public const int FIRST_USER_INDEX = 0x40; /* ..0xffffffff */ + + /* + * Predefined aggregate type indices + */ public const int ARRAY = 0x10; public const int STRUCT = 0x11; @@ -24,11 +30,6 @@ namespace se.lth.control.labcomm { public const int DOUBLE = 0x26; public const int STRING = 0x27; - /* - * start of user defined types - */ - public const int FIRST_USER_INDEX = 0x40; - } } diff --git a/lib/csharp/se/lth/control/labcomm/DecoderChannel.cs b/lib/csharp/se/lth/control/labcomm/DecoderChannel.cs index 493f10d..19e4e0d 100644 --- a/lib/csharp/se/lth/control/labcomm/DecoderChannel.cs +++ b/lib/csharp/se/lth/control/labcomm/DecoderChannel.cs @@ -13,11 +13,6 @@ namespace se.lth.control.labcomm { public DecoderChannel(Stream stream) { this.stream = stream; - String version = decodeString(); - if (version != Constant.VERSION) { - throw new IOException("LabComm version mismatch " + - version + " != " + Constant.VERSION); - } } public void runOne() { @@ -26,6 +21,13 @@ namespace se.lth.control.labcomm { int tag = decodePacked32(); int length = decodePacked32(); switch (tag) { + case Constant.VERSION: { + String version = decodeString(); + if (version != Constant.CURRENT_VERSION) { + throw new IOException("LabComm version mismatch " + + version + " != " + Constant.CURRENT_VERSION); + } + } break; case Constant.SAMPLE: { int index = decodePacked32(); String name = decodeString(); diff --git a/lib/csharp/se/lth/control/labcomm/EncoderChannel.cs b/lib/csharp/se/lth/control/labcomm/EncoderChannel.cs index 8284ff9..ca717d5 100644 --- a/lib/csharp/se/lth/control/labcomm/EncoderChannel.cs +++ b/lib/csharp/se/lth/control/labcomm/EncoderChannel.cs @@ -16,10 +16,9 @@ namespace se.lth.control.labcomm { public EncoderChannel(Stream writer) { this.writer = writer; - encodeString(Constant.VERSION); - bytes.WriteTo(writer); - bytes.SetLength(0); - writer.Flush(); + begin(Constant.VERSION); + encodeString(Constant.CURRENT_VERSION); + end(null); } public void register(SampleDispatcher dispatcher) { diff --git a/lib/java/se/lth/control/labcomm/Constant.java b/lib/java/se/lth/control/labcomm/Constant.java index 7c5da70..b67fb05 100644 --- a/lib/java/se/lth/control/labcomm/Constant.java +++ b/lib/java/se/lth/control/labcomm/Constant.java @@ -2,13 +2,22 @@ package se.lth.control.labcomm; public class Constant { - public static final String VERSION = "LabComm20141009"; + public static final String CURRENT_VERSION = "LabComm20141009"; + /* + * Allowed packet tags + */ /* * Predeclared aggregate type indices */ - public static final int TYPEDEF = 0x01; + public static final int VERSION = 0x01; public static final int SAMPLE = 0x02; + public static final int PRAGMA = 0x3f; + public static final int FIRST_USER_INDEX = 0x40; /* ..0xffffffff */ + + /* + * Predefined aggregate type indices + */ public static final int ARRAY = 0x10; public static final int STRUCT = 0x11; @@ -24,9 +33,4 @@ public class Constant { public static final int DOUBLE = 0x26; public static final int STRING = 0x27; - /* - * Start of user declared types - */ - public static final int FIRST_USER_INDEX = 0x40; - } diff --git a/lib/java/se/lth/control/labcomm/DecoderChannel.java b/lib/java/se/lth/control/labcomm/DecoderChannel.java index 6c5a1d2..6f7760f 100644 --- a/lib/java/se/lth/control/labcomm/DecoderChannel.java +++ b/lib/java/se/lth/control/labcomm/DecoderChannel.java @@ -14,12 +14,6 @@ public class DecoderChannel implements Decoder { public DecoderChannel(InputStream in) throws IOException { this.in = new DataInputStream(in); registry = new DecoderRegistry(); - String version = decodeString(); - if (! version.equals(Constant.VERSION)) { - throw new IOException("LabComm version mismatch " + - version + " != " + Constant.VERSION); - } - System.err.println(Constant.VERSION); } public void runOne() throws Exception { @@ -28,6 +22,13 @@ public class DecoderChannel implements Decoder { int tag = decodePacked32(); int length = decodePacked32(); switch (tag) { + case Constant.VERSION: { + String version = decodeString(); + if (! version.equals(Constant.CURRENT_VERSION)) { + throw new IOException("LabComm version mismatch " + + version + " != " + Constant.CURRENT_VERSION); + } + } break; case Constant.SAMPLE: { int index = decodePacked32(); String name = decodeString(); diff --git a/lib/java/se/lth/control/labcomm/EncoderChannel.java b/lib/java/se/lth/control/labcomm/EncoderChannel.java index 16755b8..e7c3e80 100644 --- a/lib/java/se/lth/control/labcomm/EncoderChannel.java +++ b/lib/java/se/lth/control/labcomm/EncoderChannel.java @@ -19,10 +19,9 @@ public class EncoderChannel implements Encoder { data = new DataOutputStream(bytes); registry = new EncoderRegistry(); - encodeString(Constant.VERSION); - data.flush(); - writer.write(bytes.toByteArray()); - bytes.reset(); + begin(Constant.VERSION); + encodeString(Constant.CURRENT_VERSION); + end(null); } public EncoderChannel(OutputStream writer) throws IOException { diff --git a/lib/python/labcomm/LabComm.py b/lib/python/labcomm/LabComm.py index 20e306b..da370ea 100644 --- a/lib/python/labcomm/LabComm.py +++ b/lib/python/labcomm/LabComm.py @@ -122,52 +122,47 @@ import struct as packer DEFAULT_VERSION = "LabComm20141009" -# Version testing -def sendVersionString(version): - return version in [ "LabComm2013", "LabComm20141009" ] +# Allowed packet tags +i_VERSION = 0x01 +i_SAMPLE = 0x02 +i_PRAGMA = 0x3f +i_USER = 0x40 # ..0xffffffff -def usePacked32(version): - return version in [ "LabComm2013", "LabComm20141009" ] +# Predefined types +i_ARRAY = 0x10 +i_STRUCT = 0x11 +i_BOOLEAN = 0x20 +i_BYTE = 0x21 +i_SHORT = 0x22 +i_INTEGER = 0x23 +i_LONG = 0x24 +i_FLOAT = 0x25 +i_DOUBLE = 0x26 +i_STRING = 0x27 + + +# Version testing def usePacketLength(version): - return version in [ "LabComm20141009" ] + return version in [ None, "LabComm20141009" ] class length_encoder: def __init__(self, encoder): self.encoder = encoder + self.version = encoder.version self.data = "" - def start(self, encoder, version): - self.version = version - def write(self, data): self.data += data def __enter__(self): - return Encoder(self) + return Encoder(self, None) def __exit__(self, type, value, traceback): if usePacketLength(self.version): self.encoder.encode_packed32(len(self.data)) self.encoder.pack("%ds" % len(self.data), self.data) -i_TYPEDEF = 0x01 -i_SAMPLE = 0x02 - -i_ARRAY = 0x10 -i_STRUCT = 0x11 - -i_BOOLEAN = 0x20 -i_BYTE = 0x21 -i_SHORT = 0x22 -i_INTEGER = 0x23 -i_LONG = 0x24 -i_FLOAT = 0x25 -i_DOUBLE = 0x26 -i_STRING = 0x27 - -i_USER = 0x40 - def indent(i, s): return ("\n%s" % (" " * i)).join(s.split("\n")) @@ -315,8 +310,6 @@ class sample(object): self.decl = decl def encode_decl(self, encoder): -# encoder.encode_type(i_SAMPLE) -# self.encode_decl_tail(encoder) encoder.encode_type(i_SAMPLE) with length_encoder(encoder) as e1: e1.encode_type(encoder.decl_to_index[self]) @@ -593,7 +586,14 @@ class Encoder(Codec): super(Encoder, self).__init__() self.writer = writer self.version = version - self.writer.start(self, self.version) + if self.version in [ "LabComm20141009" ]: + self.encode_type(i_VERSION) + with length_encoder(self) as e: + e.encode_string(version) + elif self.version in [ None, "LabComm2006" ]: + pass + else: + raise Exception("Unsupported labcomm version %s" % self.version) def pack(self, format, *args): self.writer.write(packer.pack(format, *args)) @@ -621,8 +621,7 @@ class Encoder(Codec): decl.encode_decl(self) def encode_packed32(self, v): - #if usePacked32(self.version) : - if self.version in [ "LabComm2013", "LabComm20141009" ]: + if self.version in [ None, "LabComm20141009" ]: v = v & 0xffffffff tmp = [ v & 0x7f ] v = v >> 7 @@ -676,7 +675,6 @@ class Decoder(Codec): super(Decoder, self).__init__() self.reader = reader self.version = version - self.reader.start(self, version) def unpack(self, format): size = packer.calcsize(format) @@ -696,9 +694,17 @@ class Decoder(Codec): return result def decode(self): - index = self.decode_type_number() - if usePacketLength(self.version): - length = self.decode_packed32() + while True: + index = self.decode_type_number() + if usePacketLength(self.version): + length = self.decode_packed32() + if index != i_VERSION: + break + else: + other_version = self.decode_string() + if self.version != other_version: + raise Exception("LabComm version mismatch %s != %s" % + (version, other_version)) if index == i_SAMPLE: decl = self.index_to_decl[index].decode_decl(self) value = None diff --git a/lib/python/labcomm/StreamReader.py b/lib/python/labcomm/StreamReader.py index b20983a..87e9a59 100644 --- a/lib/python/labcomm/StreamReader.py +++ b/lib/python/labcomm/StreamReader.py @@ -6,16 +6,6 @@ class StreamReader: self.stream = stream pass - def start(self, decoder, version): - self.version = version; - if labcomm.LabComm.sendVersionString(version): - other_version = decoder.decode_string() - if version != other_version: - raise Exception("LabComm version mismatch %s != %s" % - (version, other_version)) - pass - pass - def read(self, count): result = self.stream.read(count) if len(result) == 0: diff --git a/lib/python/labcomm/StreamWriter.py b/lib/python/labcomm/StreamWriter.py index 759f56e..a85c43d 100644 --- a/lib/python/labcomm/StreamWriter.py +++ b/lib/python/labcomm/StreamWriter.py @@ -6,13 +6,6 @@ class StreamWriter: self.stream = stream pass - def start(self, encoder, version): - self.version = version; - - if labcomm.LabComm.sendVersionString(version): - encoder.encode_string(version) - pass - def write(self, data): self.stream.write(data) pass -- GitLab