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
Select Git revision

Target

Select target project
  • anders_blomdell/labcomm
  • klaren/labcomm
  • tommyo/labcomm
  • erikj/labcomm
  • sven/labcomm
5 results
Select Git revision
Show changes
Showing
with 199 additions and 885 deletions
/* Hack to get a link error with (hopefully) useful information if not linked
with -Tlabcomm.linkscript */
"You need to link with '-Tlabcomm.linkscript'"=0;
SECTIONS {
labcomm : {
labcomm_first_signature = ABSOLUTE(.) ;
*(labcomm)
labcomm_last_signature = ABSOLUTE(.) ;
}
}
INSERT AFTER .data;
#ifndef _LABCOMM_DYNAMIC_BUFFER_READER_WRITER_H_
#define _LABCOMM_DYNAMIC_BUFFER_READER_WRITER_H_
#include "labcomm.h"
extern const struct labcomm_writer_action *labcomm_dynamic_buffer_writer_action;
#endif
#ifndef __LABCOMM_ERROR_H__
#define __LABCOMM_ERROR_H__
enum labcomm_error {
#define LABCOMM_ERROR(name, description) name ,
#include "labcomm_error.h"
#undef LABCOMM_ERROR
};
#endif
#ifdef LABCOMM_ERROR
LABCOMM_ERROR(LABCOMM_ERROR_ENC_NO_REG_SIGNATURE,
"Encoder has no registration for this signature")
LABCOMM_ERROR(LABCOMM_ERROR_ENC_BUF_FULL,
"The labcomm buffer is full")
LABCOMM_ERROR(LABCOMM_ERROR_DEC_UNKNOWN_DATATYPE,
"Decoder: Unknown datatype")
LABCOMM_ERROR(LABCOMM_ERROR_DEC_INDEX_MISMATCH,
"Decoder: index mismatch")
LABCOMM_ERROR(LABCOMM_ERROR_DEC_TYPE_NOT_FOUND,
"Decoder: type not found")
LABCOMM_ERROR(LABCOMM_ERROR_UNIMPLEMENTED_FUNC,
"This function is not yet implemented")
LABCOMM_ERROR(LABCOMM_ERROR_MEMORY,
"Could not allocate memory")
LABCOMM_ERROR(LABCOMM_ERROR_USER_DEF,
"User defined error")
#endif
#ifndef _LABCOMM_FD_READER_H_
#define _LABCOMM_FD_READER_H_
#include "labcomm.h"
struct labcomm_reader *labcomm_fd_reader_new(int fd, int close_fd_on_free);
#endif
#ifndef _LABCOMM_FD_WRITER_H_
#define _LABCOMM_FD_WRITER_H_
#include "labcomm.h"
struct labcomm_writer *labcomm_fd_writer_new(int fd, int close_on_free);
#endif
/*
labcomm_private.h -- semi private declarations for handling encoding and
decoding of labcomm samples.
Copyright 2006-2013 Anders Blomdell <anders.blomdell@control.lth.se>
This file is part of LabComm.
LabComm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LabComm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _LABCOMM_PRIVATE_H_
#define _LABCOMM_PRIVATE_H_
#ifdef LABCOMM_COMPAT
#include LABCOMM_COMPAT
#else
#include <endian.h>
#include <stdio.h>
#endif
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.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 0x40
/*
*
*/
#define LABCOMM_DECLARE_SIGNATURE(name) \
struct labcomm_signature __attribute__((section("labcomm"),aligned(1))) name
/*
* Semi private lock declarations
*/
struct labcomm_lock;
struct labcomm_lock_action {
int (*free)(struct labcomm_lock *lock);
int (*acquire)(struct labcomm_lock *lock);
int (*release)(struct labcomm_lock *lock);
int (*wait)(struct labcomm_lock *lock, useconds_t usec);
int (*notify)(struct labcomm_lock *lock);
};
struct labcomm_lock {
const struct labcomm_lock_action *action;
void *context;
};
/*
* Semi private decoder declarations
*/
typedef void (*labcomm_handler_function)(void *value, void *context);
typedef void (*labcomm_decoder_function)(
struct labcomm_reader *r,
labcomm_handler_function handler,
void *context);
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.
Returned value:
> 0 Number of bytes allocated for buffering
<= 0 Error
*/
int (*alloc)(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context,
struct labcomm_decoder *decoder, char *labcomm_version);
/* 'free' returns the resources claimed by 'alloc' and might have other
reader specific side-effects as well.
Returned value:
== 0 Success
!= 0 Error
*/
int (*free)(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context);
/* 'start' is called right after a sample has arrived. In the case of
a sample or typedef, 'value' == NULL.
*/
int (*start)(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context,
int index, struct labcomm_signature *signature,
void *value);
int (*end)(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context);
int (*fill)(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context);
int (*ioctl)(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context,
int index, struct labcomm_signature *signature,
uint32_t ioctl_action, va_list args);
};
struct labcomm_reader_action_context {
struct labcomm_reader_action_context *next;
const struct labcomm_reader_action *action;
void *context;
};
struct labcomm_reader {
struct labcomm_reader_action_context *action_context;
unsigned char *data;
int data_size;
int count;
int pos;
int error;
};
int labcomm_reader_alloc(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context,
struct labcomm_decoder *decoder,
char *labcomm_version);
int labcomm_reader_free(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context);
int labcomm_reader_start(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context,
int index, struct labcomm_signature *signature,
void *value);
int labcomm_reader_end(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context);
int labcomm_reader_fill(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context);
int labcomm_reader_ioctl(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context,
int index, struct labcomm_signature *signature,
uint32_t ioctl_action, va_list args);
/*
* Non typesafe registration function to be called from
* generated labcomm_decoder_register_* functions.
*/
int labcomm_internal_decoder_register(
struct labcomm_decoder *d,
struct labcomm_signature *s,
labcomm_decoder_function decoder,
labcomm_handler_function handler,
void *context);
int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder,
struct labcomm_signature *signature,
uint32_t ioctl_action, va_list args);
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define LABCOMM_DECODE(name, type) \
static inline type labcomm_read_##name(struct labcomm_reader *r) { \
type result; int i; \
for (i = sizeof(type) - 1 ; i >= 0 ; i--) { \
if (r->pos >= r->count) { \
labcomm_reader_fill(r, r->action_context); \
} \
((unsigned char*)(&result))[i] = r->data[r->pos]; \
r->pos++; \
} \
return result; \
}
#else
#define LABCOMM_DECODE(name, type) \
static inline type labcomm_read_##name(struct labcomm_reader *r) { \
type result; int i; \
for (i = 0 ; i < sizeof(type) ; i++) { \
if (r->pos >= r->count) { \
labcomm_reader_fille(r, r->action_context); \
} \
((unsigned char*)(&result))[i] = r->data[r->pos]; \
r->pos++; \
} \
return result; \
}
#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 unsigned int labcomm_read_packed32(struct labcomm_reader *r)
{
unsigned int result = 0;
while (1) {
unsigned char tmp;
if (r->pos >= r->count) {
labcomm_reader_fill(r, r->action_context);
if (r->error != 0) {
goto out;
}
}
tmp = r->data[r->pos];
r->pos++;
result = (result << 7) | (tmp & 0x7f);
if ((tmp & 0x80) == 0) {
break;
}
}
out:
return result;
}
static inline char *labcomm_read_string(struct labcomm_reader *r)
{
char *result;
int length, i;
length = labcomm_read_packed32(r);
result = malloc(length + 1);
for (i = 0 ; i < length ; i++) {
if (r->pos >= r->count) {
labcomm_reader_fill(r, r->action_context);
}
result[i] = r->data[r->pos];
r->pos++;
}
result[length] = 0;
return result;
}
/*
* Semi private encoder declarations
*/
typedef int (*labcomm_encoder_function)(
struct labcomm_writer *,
void *value);
typedef int (*labcomm_encoder_enqueue)(
struct labcomm_encoder *encoder,
void (*action)(struct labcomm_encoder *encoder,
void *context),
void *context);
struct labcomm_writer_action_context;
struct labcomm_writer_action {
int (*alloc)(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context,
struct labcomm_encoder *encoder, char *labcomm_version,
labcomm_encoder_enqueue enqueue);
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
case of a sample or typedef, 'value' == NULL.
Returned value:
== 0 Success -> continue sending the sample
== -EALREADY Success -> silently skip sending the sample,
'end' will not be called
< 0 Error
*/
int (*start)(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context,
int index, struct labcomm_signature *signature,
void *value);
int (*end)(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context);
int (*flush)(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context);
int (*ioctl)(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context,
int index, struct labcomm_signature *signature,
uint32_t ioctl_action, va_list args);
};
struct labcomm_writer_action_context {
struct labcomm_writer_action_context *next;
const struct labcomm_writer_action *action;
void *context;
};
struct labcomm_writer {
struct labcomm_writer_action_context *action_context;
unsigned char *data;
int data_size;
int count;
int pos;
int error;
};
int labcomm_writer_alloc(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context,
struct labcomm_encoder *encoder,
char *labcomm_version,
labcomm_encoder_enqueue enqueue);
int labcomm_writer_free(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context);
int labcomm_writer_start(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context,
int index, struct labcomm_signature *signature,
void *value);
int labcomm_writer_end(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context);
int labcomm_writer_flush(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context);
int labcomm_writer_ioctl(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context,
int index, struct labcomm_signature *signature,
uint32_t ioctl_action, va_list args);
int labcomm_internal_encoder_register(
struct labcomm_encoder *encoder,
struct labcomm_signature *signature,
labcomm_encoder_function encode);
int labcomm_internal_encode(
struct labcomm_encoder *encoder,
struct labcomm_signature *signature,
labcomm_encoder_function encode,
void *value);
int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
struct labcomm_signature *signature,
uint32_t ioctl_action, va_list args);
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define LABCOMM_ENCODE(name, type) \
static inline int labcomm_write_##name(struct labcomm_writer *w, type data) { \
int i; \
for (i = sizeof(type) - 1 ; i >= 0 ; i--) { \
if (w->pos >= w->count) { /*buffer is full*/ \
int err; \
err = labcomm_writer_flush(w, w->action_context); \
if (err != 0) { return err; } \
} \
w->data[w->pos] = ((unsigned char*)(&data))[i]; \
w->pos++; \
} \
return 0; \
}
#else
#define LABCOMM_ENCODE(name, type) \
static inline int labcomm_write_##name(struct labcomm_writer *w, type data) { \
int i; \
for (i = 0 ; i < sizeof(type) ; i++) { \
if (w->pos >= w->count) { \
int err; \
err = labcomm_writer_flush(w, w->action_context); \
if (err != 0) { return err; } \
} \
w->data[w->pos] = ((unsigned char*)(&data))[i]; \
w->pos++; \
} \
return 0; \
}
#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 int labcomm_write_packed32(struct labcomm_writer *w,
unsigned int data)
{
unsigned char tmp[5];
int i;
for (i = 0 ; i == 0 || data ; i++, data = (data >> 7)) {
tmp[i] = data & 0x7f;
}
for (i = i - 1 ; i >= 0 ; i--) {
if (w->pos >= w->count) {
int err;
err = labcomm_writer_flush(w, w->action_context);
if (err != 0) { return err; }
}
w->data[w->pos++] = tmp[i] | (i?0x80:0x00);
}
return 0;
}
static inline int labcomm_write_string(struct labcomm_writer *w, char *s)
{
int length, i, err;
length = strlen((char*)s);
err = labcomm_write_packed32(w, length);
if (err != 0) { return err; }
for (i = 0 ; i < length ; i++) {
if (w->pos >= w->count) {
int err;
err = labcomm_writer_flush(w, w->action_context);
if (err != 0) { return err; }
}
w->data[w->pos] = s[i];
w->pos++;
}
return 0;
}
/*
* Macros for handling arrays indexed by signature index
*/
#define LABCOMM_SIGNATURE_ARRAY_DEF(name, kind) \
struct { \
int first; \
int last; \
kind *data; \
} name
#define LABCOMM_SIGNATURE_ARRAY_DEF_INIT(name, kind) \
LABCOMM_SIGNATURE_ARRAY_DEF(name, kind) = { 0, 0, NULL }
#define LABCOMM_SIGNATURE_ARRAY_INIT(name, kind) \
name.first = 0; name.last = 0; name.data = NULL; \
name.data = (kind *)name.data; /* typechecking no-op */
#define LABCOMM_SIGNATURE_ARRAY_FREE(name, kind) \
if (name.data) { free(name.data); } \
name.data = (kind *)NULL; /* typechecking */
#define LABCOMM_SIGNATURE_ARRAY_REF(name, kind, index) \
(name.data = (kind *)name.data, /* typechecking no-op */ \
(kind *)(labcomm_signature_array_ref(&name.first, &name.last, \
(void **)&name.data, \
sizeof(kind), index)))
void *labcomm_signature_array_ref(int *first, int *last, void **data,
int size, int index);
#endif
#include <errno.h>
#include <pthread.h>
#include "labcomm.h"
#include "labcomm_private.h"
struct labcomm_pthread_mutex_lock {
struct labcomm_lock lock;
pthread_mutex_t mutex;
pthread_cond_t cond;
};
int do_free(struct labcomm_lock *l)
{
struct labcomm_pthread_mutex_lock *lock = l->context;
pthread_cond_destroy(&lock->cond);
pthread_mutex_destroy(&lock->mutex);
free(lock);
return 0;
}
int do_acquire(struct labcomm_lock *l)
{
struct labcomm_pthread_mutex_lock *lock = l->context;
if (pthread_mutex_lock(&lock->mutex) != 0) {
return -errno;
}
return 0;
}
int do_release(struct labcomm_lock *l)
{
struct labcomm_pthread_mutex_lock *lock = l->context;
if (pthread_mutex_unlock(&lock->mutex) != 0) {
return -errno;
}
return 0;
}
int do_wait(struct labcomm_lock *l, useconds_t usec)
{
struct labcomm_pthread_mutex_lock *lock = l->context;
if (usec <= 0) {
if (pthread_cond_wait(&lock->cond, &lock->mutex) != 0) {
return -errno;
}
} else {
struct timespec abstime;
time_t sec = usec / 1000000;
long nsec = (usec % 1000000) * 1000;
clock_gettime(CLOCK_REALTIME, &abstime);
abstime.tv_nsec += nsec;
abstime.tv_sec += sec + abstime.tv_nsec / 1000000000;
abstime.tv_nsec %= 1000000000;
if (pthread_cond_timedwait(&lock->cond, &lock->mutex, &abstime) != 0) {
return -errno;
}
}
return 0;
}
int do_notify(struct labcomm_lock *l)
{
struct labcomm_pthread_mutex_lock *lock = l->context;
if (pthread_cond_broadcast(&lock->cond) != 0) {
return -errno;
}
return 0;
}
static struct labcomm_lock_action action = {
.free = do_free,
.acquire = do_acquire,
.release = do_release,
.wait = do_wait,
.notify = do_notify
};
struct labcomm_lock *labcomm_pthread_mutex_lock_new()
{
struct labcomm_lock *result = NULL;
struct labcomm_pthread_mutex_lock *lock;
lock = malloc(sizeof(*lock));
if (lock == NULL) {
goto out;
}
if (pthread_mutex_init(&lock->mutex, NULL) != 0) {
goto free_lock;
}
if (pthread_cond_init(&lock->cond, NULL) != 0) {
goto destroy_mutex;
}
lock->lock.action = &action;
lock->lock.context = lock;
result = &lock->lock;
goto out;
destroy_mutex:
pthread_mutex_destroy(&lock->mutex);
free_lock:
free(lock);
out:
return result;
}
#ifndef _LABCOMM_PTHREAD_MUTEX_LOCK_H_
#define _LABCOMM_PTHREAD_MUTEX_LOCK_H_
#include "labcomm.h"
struct labcomm_lock *labcomm_pthread_mutex_lock_new();
#endif
INPUT(liblabcomm.so.1)
/* Hack to get a link error with (hopefully) useful information if not linked
with -Tlabcomm.linkscript */
"You need to link with '-Tlabcomm.linkscript'"+=1;
/*INPUT(liblabcomm.a)*/
## 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
#include "CUnit/Basic.h"
#include "CUnit/Console.h"
#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();
}
sample int test_var;
#!/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=LabCommDispatcher \
LabCommDecoderRegistry \
LabComm \
LabCommSample \
LabCommHandler \
LabCommEncoderRegistry \
LabCommDecoder \
LabCommType \
LabCommEncoderChannel \
LabCommEncoder \
LabCommDecoderChannel \
MODULES=Constant\
Decoder \
DecoderChannel \
DecoderRegistry \
Encoder \
EncoderChannel \
EncoderRegistry \
RenamingDecoder \
RenamingEncoder \
RenamingRegistry \
Sample \
SampleDispatcher \
SampleHandler \
SampleType \
WrappingDecoder \
WrappingEncoder
all: labcomm.dll
.PHONY: all
all: labcomm2014.dll
labcomm.dll: $(MODULES:%=se/lth/control/labcomm/%.cs) Makefile
labcomm2014.dll: $(MODULES:%=se/lth/control/labcomm2014/%.cs) Makefile
mcs -out:$@ -target:library $(filter %.cs, $^)
.PHONY: test
test:
.PHONY: clean
clean:
rm -f labcomm.dll
.PHONY: distclean
distclean:
rm -f labcomm2014.dll
namespace se.lth.control.labcomm {
public interface LabCommHandler {
}
}
\ No newline at end of file
public interface LabCommSample {
}
public interface LabCommType {
}
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
public class LabComm {
public class Constant {
public const string VERSION = "LabComm2013";
public const string CURRENT_VERSION = "LabComm2014";
/*
* Predeclared aggregate type indices
/*
* 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 */
/*
* Predefined aggregate type indices
*/
public const int TYPEDEF = 0x01;
public const int SAMPLE = 0x02;
public const int ARRAY = 0x10;
public const int STRUCT = 0x11;
......@@ -23,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 = 0x40;
}
}
using System;
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
public interface LabCommDecoder {
public interface Decoder {
void register(LabCommDispatcher dispatcher,
LabCommHandler handler);
void runOne();
void run();
void register(SampleDispatcher dispatcher,
SampleHandler handler);
void registerSampleRef(SampleDispatcher dispatcher);
bool decodeBoolean();
byte decodeByte();
......@@ -16,6 +19,7 @@ namespace se.lth.control.labcomm {
double decodeDouble();
String decodeString();
int decodePacked32();
SampleDispatcher decodeSampleRef();
}
......