Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • anders_blomdell/labcomm
  • klaren/labcomm
  • tommyo/labcomm
  • erikj/labcomm
  • sven/labcomm
5 results
Show changes
Showing
with 749 additions and 838 deletions
#include <stdlib.h>
#include <stdio.h>
#include "labcomm2014_private.h"
#include "test/gen/another_encoding.h"
#include "test/gen/generated_encoding.h"
static void info(char *name, char *full_name,
const struct labcomm2014_signature *signature) {
printf("%s %s %p -> %d\n", name, full_name, signature,
labcomm2014_get_local_index(signature));
if (labcomm2014_get_local_index(signature) < 0x40) {
exit(1);
}
};
int main(int argc, char *argv[])
{
#define FUNC(name, full_name) \
info( #name, #full_name, labcomm2014_signature_##full_name)
LABCOMM_FORALL_SAMPLES_generated_encoding(FUNC, ;);
LABCOMM_FORALL_SAMPLES_another_encoding(FUNC, ;);
return 0;
}
VERSIONS=2014
all:
%:
for v in $(VERSIONS) ; do $(MAKE) -C $${v} $@ || exit 1 ; done
.PHONY: clean
clean:
for v in $(VERSIONS) ; do $(MAKE) -C $${v} $@ || exit 1 ; done
.PHONY: distclean
distclean:
for v in $(VERSIONS) ; do $(MAKE) -C $${v} $@ || exit 1 ; done
rm -f *.o *.so *.so.1 *.a
Throttlenet library
The library consists of a generic part and readers/writers for the low-level transport.
The stable low-level is the _fd_ variety, on top of unix file descriptors.
Currently, experimental varieties include datagrams on top of UDP and raw ethernet
#include <errno.h>
#include <string.h>
#include <stdio.h>
#ifndef __VXWORKS__
#include <strings.h>
#endif
#include <stdlib.h>
#ifdef __VXWORKS__
#if (CPU == PPC603)
#undef _LITTLE_ENDIAN
#endif
#if (CPU == PENTIUM4)
#undef _BIG_ENDIAN
#endif
#endif
#include "labcomm.h"
#include "labcomm_private.h"
typedef struct labcomm_sample_entry {
struct labcomm_sample_entry *next;
int index;
labcomm_signature_t *signature;
labcomm_decoder_typecast_t decoder;
labcomm_handler_typecast_t handler;
labcomm_encode_typecast_t encode;
void *context;
} labcomm_sample_entry_t;
typedef struct labcomm_encoder_context {
labcomm_sample_entry_t *sample;
int index;
} labcomm_encoder_context_t;
typedef struct labcomm_decoder_context {
labcomm_sample_entry_t *sample;
} labcomm_decoder_context_t;
static labcomm_sample_entry_t *get_sample_by_signature_address(
labcomm_sample_entry_t *head,
labcomm_signature_t *signature)
{
labcomm_sample_entry_t *p;
for (p = head ; p && p->signature != signature ; p = p->next) {
}
return p;
}
static labcomm_sample_entry_t *get_sample_by_signature_value(
labcomm_sample_entry_t *head,
labcomm_signature_t *signature)
{
labcomm_sample_entry_t *p;
for (p = head ; p ; p = p->next) {
if (p->signature->type == signature->type &&
p->signature->size == signature->size &&
strcmp(p->signature->name, signature->name) == 0 &&
bcmp((void*)p->signature->signature, (void*)signature->signature,
signature->size) == 0) {
break;
}
}
return p;
}
static labcomm_sample_entry_t *get_sample_by_index(
labcomm_sample_entry_t *head,
int index)
{
labcomm_sample_entry_t *p;
for (p = head ; p && p->index != index ; p = p->next) {
}
return p;
}
static int get_encoder_index(
labcomm_encoder_t *e,
labcomm_signature_t *s)
{
int result = 0;
labcomm_encoder_context_t *context = e->context;
labcomm_sample_entry_t *sample = context->sample;
while (sample) {
if (sample->signature == s) { break; }
sample = sample->next;
}
if (sample) {
result = sample->index;
}
return result;
}
static void do_encoder_register(struct labcomm_encoder *e,
labcomm_signature_t *signature,
labcomm_encode_typecast_t encode)
{
if (signature->type == LABCOMM_SAMPLE) {
if (get_encoder_index(e, signature) == 0) {
int i;
labcomm_encoder_context_t *context = e->context;
labcomm_sample_entry_t *sample =
(labcomm_sample_entry_t*)malloc(sizeof(labcomm_sample_entry_t));
sample->next = context->sample;
sample->index = context->index;
sample->signature = signature;
sample->encode = encode;
context->index++;
context->sample = sample;
e->writer.write(&e->writer, labcomm_writer_start);
labcomm_encode_int(e, signature->type);
labcomm_encode_type_index(e, signature);
labcomm_encode_string(e, signature->name);
for (i = 0 ; i < signature->size ; i++) {
if (e->writer.pos >= e->writer.count) {
e->writer.write(&e->writer, labcomm_writer_continue);
}
e->writer.data[e->writer.pos] = signature->signature[i];
e->writer.pos++;
}
e->writer.write(&e->writer, labcomm_writer_end);
}
}
}
static void do_encode(
labcomm_encoder_t *encoder,
labcomm_signature_t *signature,
void *value)
{
labcomm_encoder_context_t *context = encoder->context;
labcomm_sample_entry_t *sample;
sample = get_sample_by_signature_address(context->sample,
signature);
if (sample && sample->encode) {
sample->encode(encoder, value);
} else {
printf("Encoder has no registration for %s\n", signature->name);
}
}
labcomm_encoder_t *labcomm_encoder_new(
int (*writer)(labcomm_writer_t *, labcomm_writer_action_t),
void *writer_context)
{
labcomm_encoder_t *result = malloc(sizeof(labcomm_encoder_t));
if (result) {
labcomm_encoder_context_t *context;
context = malloc(sizeof(labcomm_encoder_context_t));
context->sample = 0;
context->index = LABCOMM_USER;
result->context = context;
result->writer.context = writer_context;
result->writer.data = 0;
result->writer.data_size = 0;
result->writer.count = 0;
result->writer.pos = 0;
result->writer.write = writer;
result->writer.write(&result->writer, labcomm_writer_alloc);
result->do_register = do_encoder_register;
result->do_encode = do_encode;
}
return result;
}
void labcomm_internal_encoder_register(
labcomm_encoder_t *e,
labcomm_signature_t *signature,
labcomm_encode_typecast_t encode)
{
if (e && e->do_register) {
e->do_register(e, signature, encode);
} else {
printf("Encoder is missing do_register\n");
}
}
void labcomm_internal_encode(
labcomm_encoder_t *e,
labcomm_signature_t *signature,
void *value)
{
if (e && e->do_encode) {
e->do_encode(e, signature, value);
} else {
printf("Encoder is missing do_encode\n");
}
}
void labcomm_internal_encoder_user_action(labcomm_encoder_t *e,
int action)
{
e->writer.write(&e->writer, action);
}
void labcomm_encoder_free(labcomm_encoder_t* e)
{
e->writer.write(&e->writer, labcomm_writer_free);
free(e);
}
void labcomm_encode_type_index(labcomm_encoder_t *e, labcomm_signature_t *s)
{
int index = get_encoder_index(e, s);
labcomm_encode_int(e, index);
}
static int signature_writer(
labcomm_writer_t *w,
labcomm_writer_action_t action)
{
switch (action) {
case labcomm_writer_alloc: {
w->data_size = 1000;
w->count = w->data_size;
w->data = malloc(w->data_size);
w->pos = 0;
} break;
case labcomm_writer_start: {
w->data_size = 1000;
w->count = w->data_size;
w->data = realloc(w->data, w->data_size);
w->pos = 0;
} break;
case labcomm_writer_continue: {
w->data_size += 1000;
w->count = w->data_size;
w->data = realloc(w->data, w->data_size);
} break;
case labcomm_writer_end: {
} break;
case labcomm_writer_free: {
free(w->data);
w->data = 0;
w->data_size = 0;
w->count = 0;
w->pos = 0;
} break;
case labcomm_writer_available: {
} break;
}
return 0;
}
static void collect_flat_signature(
labcomm_decoder_t *decoder,
labcomm_encoder_t *signature_writer)
{
int type = labcomm_decode_int(decoder);
if (type >= LABCOMM_USER) {
printf("Implement %s ...\n", __FUNCTION__);
} else {
labcomm_encode_int(signature_writer, type);
switch (type) {
case LABCOMM_ARRAY: {
int dimensions, i;
dimensions = labcomm_decode_int(decoder);
labcomm_encode_int(signature_writer, dimensions);
for (i = 0 ; i < dimensions ; i++) {
int n = labcomm_decode_int(decoder);
labcomm_encode_int(signature_writer, n);
}
collect_flat_signature(decoder, signature_writer);
} break;
case LABCOMM_STRUCT: {
int fields, i;
fields = labcomm_decode_int(decoder);
labcomm_encode_int(signature_writer, fields);
for (i = 0 ; i < fields ; i++) {
char *name = labcomm_decode_string(decoder);
labcomm_encode_string(signature_writer, name);
free(name);
collect_flat_signature(decoder, signature_writer);
}
} break;
case LABCOMM_BOOLEAN:
case LABCOMM_BYTE:
case LABCOMM_SHORT:
case LABCOMM_INT:
case LABCOMM_LONG:
case LABCOMM_FLOAT:
case LABCOMM_DOUBLE:
case LABCOMM_STRING: {
} break;
default: {
printf("Implement %s ...\n", __FUNCTION__);
} break;
}
}
}
static void do_decoder_register(
labcomm_decoder_t *decoder,
labcomm_signature_t *signature,
labcomm_decoder_typecast_t type_decoder,
labcomm_handler_typecast_t handler,
void *handler_context)
{
labcomm_decoder_context_t *context = decoder->context;
labcomm_sample_entry_t *sample;
sample = get_sample_by_signature_address(context->sample,
signature);
if (!sample) {
sample = (labcomm_sample_entry_t*)malloc(sizeof(labcomm_sample_entry_t));
sample->next = context->sample;
context->sample = sample;
sample->index = 0;
sample->signature = signature;
}
sample->decoder = type_decoder;
sample->handler = handler;
sample->context = handler_context;
}
static int do_decode_one(labcomm_decoder_t *d)
{
int result;
do {
result = d->reader.read(&d->reader, labcomm_reader_start);
if (result > 0) {
labcomm_decoder_context_t *context = d->context;
result = labcomm_decode_int(d);
if (result == LABCOMM_TYPEDEF || result == LABCOMM_SAMPLE) {
labcomm_encoder_t *e = labcomm_encoder_new(signature_writer, 0);
labcomm_signature_t signature;
labcomm_sample_entry_t *entry;
int index;
e->writer.write(&e->writer, labcomm_writer_start);
signature.type = result;
index = labcomm_decode_int(d);
signature.name = labcomm_decode_string(d);
collect_flat_signature(d, e);
signature.size = e->writer.pos;
signature.signature = e->writer.data;
entry = get_sample_by_signature_value(context->sample, &signature);
if (! entry) {
// Unknown datatype, bail out
fprintf(stderr, "%s: unknown datatype '%s' (id=0x%x)\n",
__FUNCTION__, signature.name, index);
} else if (entry->index && entry->index != index) {
fprintf(stderr, "%s: index mismatch '%s' (id=0x%x != 0x%x)\n",
__FUNCTION__, signature.name, entry->index, index);
} else {
entry->index = index;
}
free(signature.name);
e->writer.write(&e->writer, labcomm_writer_end);
if (!entry) {
// No handler for found type, bail out (after cleanup)
result = -ENOENT;
}
labcomm_encoder_free(e);
} else {
labcomm_sample_entry_t *entry;
entry = get_sample_by_index(context->sample, result);
if (!entry) {
fprintf(stderr, "%s: type not found (id=0x%x)\n",
__FUNCTION__, result);
result = -ENOENT;
} else {
entry->decoder(d, entry->handler, entry->context);
}
}
}
d->reader.read(&d->reader, labcomm_reader_end);
} while (result > 0 && result < LABCOMM_USER);
return result;
}
labcomm_decoder_t *labcomm_decoder_new(
int (*reader)(labcomm_reader_t *, labcomm_reader_action_t),
void *reader_context)
{
labcomm_decoder_t *result = malloc(sizeof(labcomm_decoder_t));
if (result) {
labcomm_decoder_context_t *context =
(labcomm_decoder_context_t*)malloc(sizeof(labcomm_decoder_context_t));
context->sample = 0;
result->context = context;
result->reader.context = reader_context;
result->reader.data = 0;
result->reader.data_size = 0;
result->reader.count = 0;
result->reader.pos = 0;
result->reader.read = reader;
result->reader.read(&result->reader, labcomm_reader_alloc);
result->do_register = do_decoder_register;
result->do_decode_one = do_decode_one;
}
return result;
}
void labcomm_internal_decoder_register(
labcomm_decoder_t *d,
labcomm_signature_t *signature,
labcomm_decoder_typecast_t type_decoder,
labcomm_handler_typecast_t handler,
void *handler_context)
{
if (d && d->do_register) {
d->do_register(d, signature, type_decoder, handler, handler_context);
} else {
printf("Decoder is missing do_register\n");
}
}
int labcomm_decoder_decode_one(labcomm_decoder_t *d)
{
int result = -1;
if (d && d->do_decode_one) {
result = d->do_decode_one(d);
} else {
printf("Decoder is missing do_decode_one\n");
}
return result;
}
void labcomm_decoder_run(labcomm_decoder_t *d)
{
while (labcomm_decoder_decode_one(d) > 0) {
}
}
void labcomm_decoder_free(labcomm_decoder_t* d)
{
d->reader.read(&d->reader, labcomm_reader_free);
free(d);
}
#ifndef _LABCOMM_H_
#define _LABCOMM_H_
#include <endian.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Signature entry
*/
typedef struct {
int type;
char *name;
int (*encoded_size)(void *);
int size;
unsigned char *signature;
} labcomm_signature_t;
/*
* Decoder
*/
struct labcomm_decoder;
typedef enum {
labcomm_reader_alloc,
labcomm_reader_free,
labcomm_reader_start,
labcomm_reader_continue,
labcomm_reader_end
} labcomm_reader_action_t;
typedef struct labcomm_reader {
void *context;
unsigned char *data;
int data_size;
int count;
int pos;
int (*read)(struct labcomm_reader *, labcomm_reader_action_t);
} labcomm_reader_t;
struct labcomm_decoder *labcomm_decoder_new(
int (*reader)(labcomm_reader_t *, labcomm_reader_action_t),
void *reader_context);
int labcomm_decoder_decode_one(
struct labcomm_decoder *decoder);
void labcomm_decoder_run(
struct labcomm_decoder *decoder);
void labcomm_decoder_free(
struct labcomm_decoder *decoder);
/*
* Encoder
*/
struct labcomm_encoder;
typedef enum {
labcomm_writer_alloc,
labcomm_writer_free,
labcomm_writer_start,
labcomm_writer_continue,
labcomm_writer_end,
labcomm_writer_available,
} labcomm_writer_action_t;
typedef struct labcomm_writer {
void *context;
unsigned char *data;
int data_size;
int count;
int pos;
int (*write)(struct labcomm_writer *, labcomm_writer_action_t);
} labcomm_writer_t;
struct labcomm_encoder;
struct labcomm_encoder *labcomm_encoder_new(
int (*writer)(labcomm_writer_t *, labcomm_writer_action_t),
void *writer_context);
void labcomm_encoder_free(
struct labcomm_encoder *encoder);
#endif
#ifndef _LABCOMM_PRIVATE_H_
#define _LABCOMM_PRIVATE_H_
#include <endian.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "labcomm.h"
/*
* Predeclared aggregate type indices
*/
#define LABCOMM_TYPEDEF 0x01
#define LABCOMM_SAMPLE 0x02
#define LABCOMM_ARRAY 0x10
#define LABCOMM_STRUCT 0x11
/*
* Predeclared primitive type indices
*/
#define LABCOMM_BOOLEAN 0x20
#define LABCOMM_BYTE 0x21
#define LABCOMM_SHORT 0x22
#define LABCOMM_INT 0x23
#define LABCOMM_LONG 0x24
#define LABCOMM_FLOAT 0x25
#define LABCOMM_DOUBLE 0x26
#define LABCOMM_STRING 0x27
/*
* Start index for user defined types
*/
#define LABCOMM_USER 0x80
/*
* Semi private decoder declarations
*/
typedef void (*labcomm_handler_typecast_t)(void *, void *);
typedef void (*labcomm_decoder_typecast_t)(
struct labcomm_decoder *,
labcomm_handler_typecast_t,
void *);
typedef struct labcomm_decoder {
void *context;
labcomm_reader_t reader;
void (*do_register)(struct labcomm_decoder *,
labcomm_signature_t *,
labcomm_decoder_typecast_t,
labcomm_handler_typecast_t,
void *context);
int (*do_decode_one)(struct labcomm_decoder *decoder);
} labcomm_decoder_t;
/*
* Non typesafe registration function to be called from
* generated labcomm_decoder_register_* functions.
*/
void labcomm_internal_decoder_register(
labcomm_decoder_t *,
labcomm_signature_t *,
labcomm_decoder_typecast_t,
labcomm_handler_typecast_t,
void *context);
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define LABCOMM_DECODE(name, type) \
static inline type labcomm_read_##name(labcomm_reader_t *r) { \
type result; int i; \
for (i = sizeof(type) - 1 ; i >= 0 ; i--) { \
if (r->pos >= r->count) { \
r->read(r, labcomm_reader_continue); \
} \
((unsigned char*)(&result))[i] = r->data[r->pos]; \
r->pos++; \
} \
return result; \
} \
static inline type labcomm_decode_##name(labcomm_decoder_t *d) { \
return labcomm_read_##name(&d->reader); \
}
#else
#define LABCOMM_DECODE(name, type) \
static inline type labcomm_read_##name(labcomm_reader_t *r) { \
type result; int i; \
for (i = 0 ; i < sizeof(type) ; i++) { \
if (r->pos >= r->count) { \
r->read(r, labcomm_reader_continue); \
} \
((unsigned char*)(&result))[i] = r->data[r->pos]; \
r->pos++; \
} \
return result; \
} \
static inline type labcomm_decode_##name(labcomm_decoder_t *d) { \
return labcomm_read_##name(&d->reader); \
}
#endif
LABCOMM_DECODE(boolean, unsigned char)
LABCOMM_DECODE(byte, unsigned char)
LABCOMM_DECODE(short, short)
LABCOMM_DECODE(int, int)
LABCOMM_DECODE(long, long long)
LABCOMM_DECODE(float, float)
LABCOMM_DECODE(double, double)
static inline char *labcomm_read_string(labcomm_reader_t *r)
{
char *result;
int length, i;
length = labcomm_read_int(r);
result = malloc(length + 1);
for (i = 0 ; i < length ; i++) {
if (r->pos >= r->count) {
r->read(r, labcomm_reader_continue);
}
result[i] = r->data[r->pos];
r->pos++;
}
result[length] = 0;
return result;
}
static inline char *labcomm_decode_string(labcomm_decoder_t *d)
{
return labcomm_read_string(&d->reader);
}
static inline int labcomm_buffer_read(struct labcomm_reader *r,
labcomm_reader_action_t action)
{
// If this gets called, it is an error,
// so note error and let producer proceed
r->context = r;
r->pos = 0;
return 0;
}
static inline int labcomm_buffer_reader_error(struct labcomm_reader *r)
{
return r->context != NULL;
}
static inline void labcomm_buffer_reader_setup(
labcomm_reader_t *r,
void *data,
int length)
{
r->context = NULL; // Used as errer flag
r->data = data;
r->data_size = length;
r->count = length;
r->pos = 0;
r->read = labcomm_buffer_read;
}
/*
* Semi private encoder declarations
*/
typedef void (*labcomm_encode_typecast_t)(
struct labcomm_encoder *,
void *value);
typedef struct labcomm_encoder {
void *context;
labcomm_writer_t writer;
void (*do_register)(struct labcomm_encoder *encoder,
labcomm_signature_t *signature,
labcomm_encode_typecast_t);
void (*do_encode)(struct labcomm_encoder *encoder,
labcomm_signature_t *signature,
void *value);
} labcomm_encoder_t;
void labcomm_internal_encoder_register(
labcomm_encoder_t *encoder,
labcomm_signature_t *signature,
labcomm_encode_typecast_t encode);
void labcomm_internal_encode(
labcomm_encoder_t *encoder,
labcomm_signature_t *signature,
void *value);
#define LABCOMM_USER_ACTION(i) (i + 100)
void labcomm_internal_encoder_user_action(struct labcomm_encoder *encoder,
int action);
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define LABCOMM_ENCODE(name, type) \
static inline void labcomm_write_##name(labcomm_writer_t *w, type data) { \
int i; \
for (i = sizeof(type) - 1 ; i >= 0 ; i--) { \
if (w->pos >= w->count) { \
w->write(w, labcomm_writer_continue); \
} \
w->data[w->pos] = ((unsigned char*)(&data))[i]; \
w->pos++; \
} \
} \
static inline void labcomm_encode_##name(labcomm_encoder_t *e, type data) { \
labcomm_write_##name(&e->writer, data); \
}
#else
#define LABCOMM_ENCODE(name, type) \
static inline void labcomm_write_##name(labcomm_writer_t *w, type data) { \
int i; \
for (i = 0 ; i < sizeof(type) ; i++) { \
if (w->pos >= w->count) { \
w->write(w, labcomm_writer_continue); \
} \
w->data[w->pos] = ((unsigned char*)(&data))[i]; \
w->pos++; \
} \
} \
static inline void labcomm_encode_##name(labcomm_encoder_t *e, type data) { \
labcomm_write_##name(&e->writer, data); \
}
#endif
LABCOMM_ENCODE(boolean, unsigned char)
LABCOMM_ENCODE(byte, unsigned char)
LABCOMM_ENCODE(short, short)
LABCOMM_ENCODE(int, int)
LABCOMM_ENCODE(long, long long)
LABCOMM_ENCODE(float, float)
LABCOMM_ENCODE(double, double)
static inline void labcomm_write_string(labcomm_writer_t *w, char *s)
{
int length, i;
length = strlen((char*)s);
labcomm_write_int(w, length);
for (i = 0 ; i < length ; i++) {
if (w->pos >= w->count) {
w->write(w, labcomm_writer_continue);
}
w->data[w->pos] = s[i];
w->pos++;
}
}
static inline void labcomm_encode_string(labcomm_encoder_t *e,
char *s)
{
labcomm_write_string(&e->writer, s);
}
void labcomm_encode_type_index(labcomm_encoder_t *e, labcomm_signature_t *s);
static inline int labcomm_buffer_write(struct labcomm_writer *w,
labcomm_writer_action_t action)
{
// If this gets called, it is an error,
// so note error and let producer proceed
w->context = w;
w->pos = 0;
return 0;
}
static inline int labcomm_buffer_writer_error(struct labcomm_writer *w)
{
return w->context != NULL;
}
static inline void labcomm_buffer_writer_setup(struct labcomm_writer *w,
void *data,
int length)
{
w->context = NULL; // Used as error flag
w->data = data;
w->data_size = length;
w->count = length;
w->pos = 0;
w->write = labcomm_buffer_write;
}
#endif
## Macros
UNAME_S=$(shell uname -s)
VERSION=2014
LIBVERSION=2014
ifeq ($(UNAME_S),Linux)
CC=$(CROSS_COMPILE)gcc
LD=$(CROSS_COMPILE)gcc
CFLAGS=-std=c99 -g -Wall -Werror -O3 -I.
CFLAGS_TEST=$(CFLAGS) -Itest
LDFLAGS=-L..
LDLIBS=-llabcomm$(LIBVERSION) -lrt
LD_LIBRARY_PATH_NAME=LD_LIBRARY_PATH
MAKESHARED=gcc -o $1 -shared -Wl,-soname,$2 $3 -lc -lrt
else ifeq ($(UNAME_S),Darwin)
#CC=$(CROSS_COMPILE)clang
#LD=$(CROSS_COMPILE)ld
CC=$(CROSS_COMPILE)gcc
LD=$(CROSS_COMPILE)gcc
CFLAGS=-g -Wall -Werror -O3 -I. -Itest \
-DLABCOMM_COMPAT=\"labcomm$(VERSION)_compat_osx.h\" \
-DLABCOMM_OS_DARWIN=1 \
-Wno-unused-function
# -Wno-tautological-compare
CFLAGS+=-std=c99
LDFLAGS=-L..
LDLIBS=-llabcomm$(LIBVERSION)
LD_LIBRARY_PATH_NAME=DYLD_LIBRARY_PATH
MAKESHARED=clang -o $1 -shared -Wl,-install_name,$2 $3 -lc
else ifneq ($(findstring CYGWIN,$(UNAME_S)),)
CC=$(CROSS_COMPILE)gcc
LD=$(CROSS_COMPILE)ld
CFLAGS=-std=c99 -g -Wall -Werror -O3 -I.
LDFLAGS=-L..
LDLIBS=-llabcomm$(LIBVERSION) -lrt
ALL_DEPS:=$(filter-out %.so.1, $(ALL_DEPS)) # No -fPIC supported in windows?
else
$(error Unknown system $(UNAME_S))
endif
CFLAGS_TEST=$(CFLAGS) -Itest -DVERSION=$(VERSION)
ifeq ($(CROSS_COMPILE),i586-wrs-vxworks-)
ALL_DEPS:=$(filter-out %.so.1, $(ALL_DEPS)) # PIC is only supported for RTPs
CFLAGS:=$(CFLAGS) -DLABCOMM_COMPAT=\"labcomm_compat_vxworks.h\"
endif
#!/usr/bin/python
import os
import sys
import difflib
import re
class File:
def __init__(self, path, match, replacement):
def replace(s):
r = re.sub('[ \t]+', ' ', s).replace(match, replacement)
r = r.strip() + '\n'
return r
self.name = path.replace(match, replacement)
self.path = path
with open(path) as f:
self.lines = map(replace, f.readlines())
def __cmp__(self, other):
if other == None:
return cmp(self.name, other)
return cmp(self.name, other.name)
def readfiles(root, match, replacement):
result = []
for n in os.listdir(root):
path = os.path.join(root, n)
if os.path.islink(path):
pass
elif os.path.isdir(path):
for f in filter(None, readfiles(path, match, replacement)):
result.append(f)
else:
result.append(File(path, match, replacement))
for f in sorted(result):
yield f
yield None
if __name__ == '__main__':
A = readfiles(*sys.argv[1:4])
B = readfiles(*sys.argv[4:7])
a = A.next()
b = B.next()
while a != None or b != None:
if b == None or a.name < b.name:
print "Only in %s:" %sys.argv[1], a.path
a = A.next()
elif a == None or a.name > b.name:
print "Only in %s:" %sys.argv[4], b.path
b = B.next()
else:
equal = True
for l in difflib.unified_diff(a.lines, b.lines, a.path, b.path):
print l,
equal = False
if equal:
print "Identical", a.path, b.path
a = A.next()
b = B.next()
labcomm2014.dll
MODULES=Constant\
Decoder \
DecoderChannel \
DecoderRegistry \
Encoder \
EncoderChannel \
EncoderRegistry \
RenamingDecoder \
RenamingEncoder \
RenamingRegistry \
Sample \
SampleDispatcher \
SampleHandler \
SampleType \
WrappingDecoder \
WrappingEncoder
.PHONY: all
all: labcomm2014.dll
labcomm2014.dll: $(MODULES:%=se/lth/control/labcomm2014/%.cs) Makefile
mcs -out:$@ -target:library $(filter %.cs, $^)
.PHONY: test
test:
.PHONY: clean
clean:
.PHONY: distclean
distclean:
rm -f labcomm2014.dll
namespace se.lth.control.labcomm {
public interface LabCommHandler {
}
}
\ No newline at end of file
public interface LabCommSample {
}
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
public class LabComm {
public class Constant {
public const string CURRENT_VERSION = "LabComm2014";
/*
* Allowed packet tags
*/
public const int VERSION = 0x01;
public const int SAMPLE_DEF = 0x02;
public const int SAMPLE_REF = 0x03;
public const int TYPE_DEF = 0x04;
public const int TYPE_BINDING = 0x05;
public const int PRAGMA = 0x3f;
public const int FIRST_USER_INDEX = 0x40; /* ..0xffffffff */
/*
* Predeclared aggregate type indices
* Predefined aggregate type indices
*/
public const int TYPEDEF = 0x01;
public const int SAMPLE = 0x02;
public const int ARRAY = 0x10;
public const int STRUCT = 0x11;
......@@ -21,12 +32,8 @@ namespace se.lth.control.labcomm {
public const int FLOAT = 0x25;
public const int DOUBLE = 0x26;
public const int STRING = 0x27;
public const int SAMPLE = 0x28;
/*
* start of user defined types
*/
public const int FIRST_USER_INDEX = 0x80;
}
}
using System;
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
public interface LabCommDecoder {
public interface Decoder {
void runOne();
void run();
void register(SampleDispatcher dispatcher,
SampleHandler handler);
void registerSampleRef(SampleDispatcher dispatcher);
void register(LabCommDispatcher dispatcher,
LabCommHandler handler);
bool decodeBoolean();
byte decodeByte();
short decodeShort();
......@@ -14,6 +18,8 @@ namespace se.lth.control.labcomm {
float decodeFloat();
double decodeDouble();
String decodeString();
int decodePacked32();
SampleDispatcher decodeSampleRef();
}
......
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
public class LabCommDecoderChannel : LabCommDecoder {
public class DecoderChannel : Decoder {
private Stream stream;
private LabCommDecoderRegistry registry = new LabCommDecoderRegistry();
private DecoderRegistry def_registry = new DecoderRegistry();
private DecoderRegistry ref_registry = new DecoderRegistry();
byte[] buf = new byte[8];
public LabCommDecoderChannel(Stream stream) {
public DecoderChannel(Stream stream) {
this.stream = stream;
}
public void runOne() {
bool done = false;
while (!done) {
int tag = decodeInt();
int tag = decodePacked32();
int length = decodePacked32();
switch (tag) {
case LabComm.TYPEDEF:
case LabComm.SAMPLE: {
int index = decodeInt();
String name = decodeString();
MemoryStream signature = new MemoryStream();
collectFlatSignature(new LabCommEncoderChannel(signature));
registry.add(index, name, signature.ToArray());
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_DEF: {
int index = decodePacked32();
String name = decodeIntentions();
int signature_length = decodePacked32();
byte[] signature = new byte[signature_length];
ReadBytes(signature, signature_length);
def_registry.add(index, name, signature);
} break;
case Constant.SAMPLE_REF: {
int index = decodePacked32();
String name = decodeIntentions();
int signature_length = decodePacked32();
byte[] signature = new byte[signature_length];
ReadBytes(signature, signature_length);
ref_registry.add(index, name, signature);
} break;
case Constant.TYPE_DEF:
case Constant.TYPE_BINDING: {
for(int i=0; i<length;i++){
decodeByte();
}
} break;
default: {
LabCommDecoderRegistry.Entry e = registry.get(tag);
DecoderRegistry.Entry e = def_registry.get(tag);
if (e == null) {
throw new IOException("Unhandled tag " + tag);
}
LabCommDispatcher d = e.getDispatcher();
SampleDispatcher d = e.getSampleDispatcher();
if (d == null) {
throw new IOException("No dispatcher for '" + e.getName() + "'" + e.getSignature());
}
LabCommHandler h = e.getHandler();
SampleHandler h = e.getHandler();
if (h == null) {
throw new IOException("No handler for '" + e.getName() +"'");
}
......@@ -54,45 +77,13 @@ namespace se.lth.control.labcomm {
}
}
private void collectFlatSignature(LabCommEncoder e) {
int type = decodeInt();
e.encodeInt(type);
switch (type) {
case LabComm.ARRAY: {
int dimensions = decodeInt();
e.encodeInt(dimensions);
for (int i = 0 ; i < dimensions ; i++) {
e.encodeInt(decodeInt());
}
collectFlatSignature(e);
} break;
case LabComm.STRUCT: {
int fields = decodeInt();
e.encodeInt(fields);
for (int i = 0 ; i < fields ; i++) {
e.encodeString(decodeString());
collectFlatSignature(e);
}
} break;
case LabComm.BOOLEAN:
case LabComm.BYTE:
case LabComm.SHORT:
case LabComm.INT:
case LabComm.LONG:
case LabComm.FLOAT:
case LabComm.DOUBLE:
case LabComm.STRING: {
} break;
default: {
throw new IOException("Unimplemented type=" + type);
}
}
e.end(null);
public void register(SampleDispatcher dispatcher,
SampleHandler handler) {
def_registry.add(dispatcher, handler);
}
public void register(LabCommDispatcher dispatcher,
LabCommHandler handler) {
registry.add(dispatcher, handler);
public void registerSampleRef(SampleDispatcher dispatcher) {
ref_registry.add(dispatcher, null);
}
private void ReadBytes(byte[] result, int length) {
......@@ -101,17 +92,18 @@ namespace se.lth.control.labcomm {
int count = stream.Read(result, offset, length - offset);
if (count <= 0)
throw new EndOfStreamException(
String.Format("End of stream reached with {0} bytes left to read",
String.Format("End of stream reached with {0} bytes left to read",
length - offset));
offset += count;
}
}
private Int64 ReadInt(int length) {
int result = 0;
Int64 result = 0;
ReadBytes(buf, length);
for (int i = 0 ; i < length ; i++) {
result = (result << 8) + buf[i];
}
return result;
}
......@@ -123,7 +115,7 @@ namespace se.lth.control.labcomm {
public byte decodeByte() {
return (byte)ReadInt(1);
}
public short decodeShort() {
return (short)ReadInt(2);
}
......@@ -131,11 +123,11 @@ namespace se.lth.control.labcomm {
public int decodeInt() {
return (int)ReadInt(4);
}
public long decodeLong() {
return (long)ReadInt(8);
}
[StructLayout(LayoutKind.Explicit)]
private struct Int32SingleUnion {
......@@ -149,18 +141,65 @@ namespace se.lth.control.labcomm {
u.AsInt = (int)ReadInt(4);
return u.AsFloat;
}
public double decodeDouble() {
return BitConverter.Int64BitsToDouble(ReadInt(8));
}
public String decodeString() {
int length = (int)ReadInt(4);
int length = decodePacked32();
byte[] buf = new byte[length];
ReadBytes(buf, length);
return Encoding.UTF8.GetString(buf);
}
public int decodePacked32() {
Int64 res = 0;
bool cont = true;
do {
Int64 c = decodeByte();
res = (res << 7) | (c & 0x7f);
cont = (c & 0x80) != 0;
} while(cont);
return (int) (res & 0xffffffff);
}
private byte[] decodeBytes() {
int len = decodePacked32();
byte[] result = new byte[len];
for(int i=0; i<len; i++) {
result[i] = decodeByte();
}
return result;
}
private String decodeIntentions() {
int numIntentions = decodePacked32();
string name = "";
for(int i = 0; i<numIntentions; i++) {
byte[] key = decodeBytes();
byte[] val = decodeBytes();
if(key.Length == 0) {
name = Encoding.UTF8.GetString(val, 0, val.Length);
}
}
return name;
}
public SampleDispatcher decodeSampleRef() {
int index = (int)ReadInt(4);
try {
DecoderRegistry.Entry e = ref_registry.get(index);
return e.getSampleDispatcher();
} catch (NullReferenceException) {
return null;
}
}
}
}
}
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
using System;
using System.Collections.Generic;
public class LabCommDecoderRegistry {
public class DecoderRegistry {
public class Entry {
private LabCommDispatcher dispatcher;
private LabCommHandler handler;
private SampleDispatcher dispatcher;
private SampleHandler handler;
private int index;
private String name;
private byte[] signature;
public Entry(LabCommDispatcher dispatcher,
LabCommHandler handler) {
public Entry(SampleDispatcher dispatcher,
SampleHandler handler) {
this.dispatcher = dispatcher;
this.name = dispatcher.getName();
this.signature = dispatcher.getSignature();
......@@ -27,19 +27,19 @@ namespace se.lth.control.labcomm {
this.signature = signature;
}
public LabCommDispatcher getDispatcher() {
public SampleDispatcher getSampleDispatcher() {
return dispatcher;
}
public void setDispatcher(LabCommDispatcher dispatcher) {
public void setSampleDispatcher(SampleDispatcher dispatcher) {
this.dispatcher = dispatcher;
}
public LabCommHandler getHandler() {
public SampleHandler getHandler() {
return handler;
}
public void setHandler(LabCommHandler handler) {
public void setHandler(SampleHandler handler) {
this.handler = handler;
}
......@@ -90,26 +90,26 @@ namespace se.lth.control.labcomm {
}
}
private Dictionary<Type, Entry> byClass;
private Dictionary<SampleDispatcher, Entry> byDispatcher;
private Dictionary<int, Entry> byIndex;
public LabCommDecoderRegistry() {
byClass = new Dictionary<Type, Entry>();
public DecoderRegistry() {
byDispatcher = new Dictionary<SampleDispatcher, Entry>();
byIndex = new Dictionary<int, Entry>();
}
public void add(LabCommDispatcher dispatcher,
LabCommHandler handler) {
public void add(SampleDispatcher dispatcher,
SampleHandler handler) {
lock(this) {
Entry e;
byClass.TryGetValue(dispatcher.getSampleClass(), out e);
byDispatcher.TryGetValue(dispatcher, out e);
if (e != null) {
e.check(dispatcher.getName(), dispatcher.getSignature());
e.setHandler(handler);
} else {
foreach (Entry e2 in byIndex.Values) {
if (e2.match(dispatcher.getName(), dispatcher.getSignature())) {
e2.setDispatcher(dispatcher);
e2.setSampleDispatcher(dispatcher);
e2.setHandler(handler);
e = e2;
break;
......@@ -117,7 +117,7 @@ namespace se.lth.control.labcomm {
}
if (e == null) {
e = new Entry(dispatcher, handler);
byClass.Add(dispatcher.getSampleClass(), e);
byDispatcher.Add(dispatcher, e);
}
}
}
......@@ -132,7 +132,7 @@ namespace se.lth.control.labcomm {
if (e != null) {
e.check(name, signature);
} else {
foreach (Entry e2 in byClass.Values) {
foreach (Entry e2 in byDispatcher.Values) {
if (e2.match(name, signature)) {
e2.setIndex(index);
e = e2;
......
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
using System;
public interface LabCommEncoder {
public interface Encoder {
void register(LabCommDispatcher dispatcher);
void begin(Type c);
void end(Type c);
void register(SampleDispatcher dispatcher);
void registerSampleRef(SampleDispatcher dispatcher);
void begin(SampleDispatcher dispatcher);
void end(SampleDispatcher dispatcher);
void encodeBoolean(bool value);
void encodeByte(byte value);
void encodeShort(short value);
......@@ -15,6 +17,8 @@ namespace se.lth.control.labcomm {
void encodeFloat(float value);
void encodeDouble(double value);
void encodeString(String value);
void encodePacked32(Int64 value);
void encodeSampleRef(SampleDispatcher value);
}
......
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
public class LabCommEncoderChannel : LabCommEncoder {
public class EncoderChannel : Encoder {
private Stream writer;
private MemoryStream bytes = new MemoryStream();
private LabCommEncoderRegistry registry = new LabCommEncoderRegistry();
private EncoderRegistry def_registry = new EncoderRegistry();
private EncoderRegistry ref_registry = new EncoderRegistry();
byte[] buf = new byte[8];
private int current_tag;
public LabCommEncoderChannel(Stream writer) {
public EncoderChannel(Stream writer) {
this.writer = writer;
begin(Constant.VERSION);
encodeString(Constant.CURRENT_VERSION);
end(null);
}
public void register(LabCommDispatcher dispatcher) {
int index = registry.add(dispatcher);
encodeInt(LabComm.SAMPLE);
encodeInt(index);
encodeString(dispatcher.getName());
public void register(SampleDispatcher dispatcher) {
int index = def_registry.add(dispatcher);
begin(Constant.SAMPLE_DEF);
encodePacked32(index);
encodeIntentions(dispatcher.getName());
byte[] signature = dispatcher.getSignature();
encodePacked32(signature.Length);
for (int i = 0 ; i < signature.Length ; i++) {
encodeByte(signature[i]);
}
end(null);
}
public void begin(Type c) {
encodeInt(registry.getTag(c));
public void registerSampleRef(SampleDispatcher dispatcher) {
int index = ref_registry.add(dispatcher);
begin(Constant.SAMPLE_REF);
encodePacked32(index);
encodeIntentions(dispatcher.getName());
byte[] signature = dispatcher.getSignature();
encodePacked32(signature.Length);
for (int i = 0 ; i < signature.Length ; i++) {
encodeByte(signature[i]);
}
end(null);
}
private void begin(int tag) {
current_tag = tag;
bytes.SetLength(0);
}
public void begin(SampleDispatcher identity) {
begin(def_registry.getTag(identity));
}
public void end(Type c) {
public void end(SampleDispatcher identity) {
WritePacked32(writer, current_tag);
WritePacked32(writer, bytes.Length);
bytes.WriteTo(writer);
bytes.SetLength(0);
writer.Flush();
}
private void WritePacked32(Stream s, Int64 value) {
Int64 v = value & 0xffffffff;
int i;
for (i = 0 ; i == 0 || v != 0 ; i++, v = (v >> 7)) {
buf[i] = (byte)(v & 0x7f | (i!=0?0x80:0x00));
}
for (i = i - 1 ; i >= 0 ; i--) {
s.WriteByte(buf[i]);
}
}
private void WriteInt(Int64 value, int length) {
......@@ -46,7 +86,7 @@ namespace se.lth.control.labcomm {
}
public void encodeBoolean(bool value) {
WriteInt(value ? 0 : 1, 1);
WriteInt(value ? 1 : 0, 1);
}
public void encodeByte(byte value) {
......@@ -84,9 +124,28 @@ namespace se.lth.control.labcomm {
public void encodeString(String value) {
byte[] buf = Encoding.UTF8.GetBytes(value);
WriteInt(buf.Length, 4);
encodePacked32(buf.Length);
bytes.Write(buf, 0, buf.Length);
}
public void encodePacked32(Int64 value) {
WritePacked32(bytes, value);
}
private void encodeIntentions(String name) {
encodePacked32(1); // one intention field
encodePacked32(0); // empty key: name
encodeString(name);
}
public void encodeSampleRef(SampleDispatcher value) {
int index = 0;
try {
index = ref_registry.getTag(value);
} catch (NullReferenceException) {
}
WriteInt(index, 4);
}
}
}
\ No newline at end of file
}
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
using System;
using System.Collections.Generic;
public class LabCommEncoderRegistry {
public class EncoderRegistry {
public class Entry {
private LabCommDispatcher dispatcher;
private SampleDispatcher dispatcher;
private int index;
public Entry(LabCommDispatcher dispatcher, int index) {
public Entry(SampleDispatcher dispatcher, int index) {
this.dispatcher = dispatcher;
this.index = index;
}
public LabCommDispatcher getDispatcher() {
public SampleDispatcher getSampleDispatcher() {
return dispatcher;
}
......@@ -25,30 +25,30 @@ namespace se.lth.control.labcomm {
}
private int userIndex = LabComm.FIRST_USER_INDEX;
private Dictionary<Type, Entry> byClass;
private int userIndex = Constant.FIRST_USER_INDEX;
private Dictionary<SampleDispatcher, Entry> byDispatcher;
public LabCommEncoderRegistry() {
byClass = new Dictionary<Type, Entry>();
public EncoderRegistry() {
byDispatcher = new Dictionary<SampleDispatcher, Entry>();
}
public int add(LabCommDispatcher dispatcher) {
public int add(SampleDispatcher dispatcher) {
lock(this) {
Entry e;
byClass.TryGetValue(dispatcher.getSampleClass(), out e);
byDispatcher.TryGetValue(dispatcher, out e);
if (e == null) {
e = new Entry(dispatcher, userIndex);
byClass.Add(dispatcher.getSampleClass(), e);
byDispatcher.Add(dispatcher, e);
userIndex++;
}
return e.getIndex();
}
}
public int getTag(Type sample) {
public int getTag(SampleDispatcher sample) {
lock(this) {
Entry e;
byClass.TryGetValue(sample, out e);
byDispatcher.TryGetValue(sample, out e);
if (e == null) {
throw new Exception("'" +
sample.ToString() +
......@@ -60,4 +60,4 @@ namespace se.lth.control.labcomm {
}
}
\ No newline at end of file
}
namespace se.lth.control.labcomm2014 {
using System;
public class RenamingDecoder : WrappingDecoder {
private Decoder decoder;
private RenamingRegistry registry;
private Func<String,String> rename;
public RenamingDecoder(Decoder decoder,
RenamingRegistry registry,
Func<String,String> rename)
: base(decoder) {
this.decoder = decoder;
this.registry = registry;
this.rename = rename;
}
public override void register(SampleDispatcher dispatcher,
SampleHandler handler) {
decoder.register(registry.add(
dispatcher, rename(dispatcher.getName())), handler);
}
public override void registerSampleRef(SampleDispatcher dispatcher) {
decoder.registerSampleRef(registry.add(
dispatcher, rename(dispatcher.getName())));
}
}
}