Commit 3b18c8bf authored by Sven Robertz's avatar Sven Robertz
Browse files

applied patch from Anders Blomdell plus minor fixes

parent 33fabe9f
......@@ -2,11 +2,11 @@
# Use LLVM clang if it's found.
CC = $(shell hash clang 2>/dev/null && echo clang || echo gcc)
CFLAGS = -g -Wall -I .
CFLAGS = -g -Wall -Werror -I. -Itest
LDFLAGS = -L .
LDLIBS_TEST = -lcunit -llabcomm
OBJS= labcomm.o labcomm_fd_reader_writer.o labcomm_mem_reader.o labcomm_mem_writer.o
OBJS= labcomm.o labcomm_dynamic_buffer_reader_writer.o labcomm_fd_reader_writer.o labcomm_mem_reader.o labcomm_mem_writer.o
LABCOMMC_PATH=../../compiler
LABCOMMC_JAR=$(LABCOMMC_PATH)/labComm.jar
......@@ -14,9 +14,11 @@ TEST_DIR=test
TESTDATA_DIR=$(TEST_DIR)/testdata
TEST_GEN_DIR=$(TESTDATA_DIR)/gen
VPATH=$(TEST_DIR)
CREATED_DIRS=$(TEST_DIR) $(TESTDATA_DIR) $(TEST_GEN_DIR)
# Enable experimental objects by invoking make like `make -e LABCOMM_EXPERIMENTAL=true`
# Enable experimental objects by invoking make like `make LABCOMM_EXPERIMENTAL=true`
ifeq ($(LABCOMM_EXPERIMENTAL),true)
OBJS += experimental/udp_hack.o experimental/ethaddr.o \
experimental/labcomm_thr_reader_writer.o \
......@@ -39,9 +41,9 @@ labcomm.o : labcomm.c labcomm.h labcomm_private.h
labcomm_fd_reader_writer.o : labcomm_fd_reader_writer.c labcomm_fd_reader_writer.h labcomm.h labcomm_private.h
labcomm_mem_reader.o: labcomm_fd_reader_writer.c labcomm_fd_reader_writer.h
$(TEST_DIR)/labcomm_mem_reader.o: labcomm_fd_reader_writer.c labcomm_fd_reader_writer.h
labcomm_mem_writer.o: labcomm_mem_writer.c labcomm_mem_writer.h cppmacros.h
$(TEST_DIR)/labcomm_mem_writer.o: labcomm_mem_writer.c labcomm_mem_writer.h cppmacros.h
ethaddr.o: ethaddr.c
......
......@@ -29,6 +29,8 @@
#include "labcomm.h"
#include "labcomm_private.h"
#include "labcomm_ioctl.h"
#include "labcomm_dynamic_buffer_reader_writer.h"
typedef struct labcomm_sample_entry {
struct labcomm_sample_entry *next;
......@@ -185,7 +187,7 @@ void labcomm_encode_signature(struct labcomm_encoder *e,
labcomm_signature_t *signature)
{
int i;
e->writer.write(&e->writer, labcomm_writer_start);
e->writer.write(&e->writer, labcomm_writer_start_signature);
labcomm_encode_packed32(e, signature->type);
labcomm_encode_type_index(e, signature);
labcomm_encode_string(e, signature->name);
......@@ -196,7 +198,7 @@ void labcomm_encode_signature(struct labcomm_encoder *e,
e->writer.data[e->writer.pos] = signature->signature[i];
e->writer.pos++;
}
e->writer.write(&e->writer, labcomm_writer_end);
e->writer.write(&e->writer, labcomm_writer_end_signature);
}
static void do_encoder_register(struct labcomm_encoder *e,
......@@ -215,11 +217,16 @@ static void do_encoder_register(struct labcomm_encoder *e,
context->index++;
context->sample = sample;
#ifdef OLD_STUFF
labcomm_encode_signature(e, signature);
#else
e->writer.write(&e->writer, labcomm_writer_send_signature, signature, e);
#endif
struct labcomm_ioctl_register_signature ioctl_data;
int err;
ioctl_data.index = sample->index;
ioctl_data.signature = signature;
err = labcomm_encoder_ioctl(e, LABCOMM_IOCTL_REGISTER_SIGNATURE,
&ioctl_data);
if (err != 0) {
labcomm_encode_signature(e, signature);
}
}
}
}
......@@ -241,7 +248,7 @@ static void do_encode(
}
labcomm_encoder_t *labcomm_encoder_new(
int (*writer)(labcomm_writer_t *, labcomm_writer_action_t, ...),
int (*writer)(labcomm_writer_t *, labcomm_writer_action_t),
void *writer_context)
{
labcomm_encoder_t *result = malloc(sizeof(labcomm_encoder_t));
......@@ -257,12 +264,14 @@ labcomm_encoder_t *labcomm_encoder_new(
result->writer.data_size = 0;
result->writer.count = 0;
result->writer.pos = 0;
result->writer.error = 0;
result->writer.write = writer;
result->writer.write(&result->writer, labcomm_writer_alloc);
result->writer.ioctl = NULL;
result->writer.on_error = on_error_fprintf;
result->do_register = do_encoder_register;
result->do_encode = do_encode;
result->on_error = on_error_fprintf;
result->writer.write(&result->writer, labcomm_writer_alloc);
}
return result;
}
......@@ -316,51 +325,27 @@ void labcomm_encoder_free(labcomm_encoder_t* e)
free(e);
}
void labcomm_encode_type_index(labcomm_encoder_t *e, labcomm_signature_t *s)
int labcomm_encoder_ioctl(struct labcomm_encoder *encoder,
int action,
...)
{
int index = get_encoder_index(e, s);
labcomm_encode_packed32(e, index);
int result = -ENOTSUP;
if (encoder->writer.ioctl != NULL) {
va_list va;
va_start(va, action);
result = encoder->writer.ioctl(&encoder->writer, action, va);
va_end(va);
}
return result;
}
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;
case labcomm_writer_send_signature:
case labcomm_writer_user_action:
return -ENOTSUP; //HERE BE DRAGONS: should probably be cleaned up
}
return 0;
void labcomm_encode_type_index(labcomm_encoder_t *e, labcomm_signature_t *s)
{
int index = get_encoder_index(e, s);
labcomm_encode_packed32(e, index);
}
static void collect_flat_signature(
......@@ -455,7 +440,8 @@ static int do_decode_one(labcomm_decoder_t *d)
result = labcomm_decode_packed32(d);
// printf("do_decode_one: result(2) = %x\n", result);
if (result == LABCOMM_TYPEDEF || result == LABCOMM_SAMPLE) {
labcomm_encoder_t *e = labcomm_encoder_new(signature_writer, 0);
labcomm_encoder_t *e = labcomm_encoder_new(
labcomm_dynamic_buffer_writer, 0);
labcomm_signature_t signature;
labcomm_sample_entry_t *entry;
int index;
......@@ -521,12 +507,13 @@ labcomm_decoder_t *labcomm_decoder_new(
result->reader.count = 0;
result->reader.pos = 0;
result->reader.read = reader;
result->reader.read(&result->reader, labcomm_reader_alloc);
result->reader.ioctl = NULL;
result->reader.on_error = on_error_fprintf;
result->do_register = do_decoder_register;
result->do_decode_one = do_decode_one;
result->on_error = on_error_fprintf;
result->on_new_datatype = on_new_datatype;
result->on_new_datatype = on_new_datatype;
result->reader.read(&result->reader, labcomm_reader_alloc);
}
return result;
}
......
......@@ -59,12 +59,15 @@ enum labcomm_error {
/* Error strings. _must_ be the same order as in enum labcomm_error */
extern const char *labcomm_error_strings[];
/* The callback prototype for error handling.\
/* The callback prototype for error handling.
* First parameter is the error ID.
* The second paramters is the number of va_args that comes after this one. If noneit should be 0.
* Optionaly other paramters can be supplied depending on what is needed for this error ID.
* The second paramters is the number of va_args that comes after this
* one. If none it should be 0.
* Optionaly other paramters can be supplied depending on what is needed
* for this error ID.
*/
typedef void (* labcomm_error_handler_callback)(enum labcomm_error error_id, size_t nbr_va_args, ...);
typedef void (*labcomm_error_handler_callback)(enum labcomm_error error_id,
size_t nbr_va_args, ...);
/* Default error handler, prints message to stderr.
* Extra info about the error can be supplied as char* as VA-args. Especially user defined errors should supply a describing string. if nbr_va_args > 1 the first variable argument must be a printf format string and the possibly following arguments are passed as va_args to vprintf.
......@@ -80,8 +83,9 @@ void labcomm_register_error_handler_decoder(struct labcomm_decoder *decoder, lab
/* Get a string describing the supplied standrad labcomm error. */
const char *labcomm_error_get_str(enum labcomm_error error_id);
typedef int (* labcomm_handle_new_datatype_callback)(struct labcomm_decoder *decoder,
labcomm_signature_t *sig);
typedef int (*labcomm_handle_new_datatype_callback)(
struct labcomm_decoder *decoder,
labcomm_signature_t *sig);
void labcomm_decoder_register_new_datatype_handler(struct labcomm_decoder *d,
labcomm_handle_new_datatype_callback on_new_datatype);
......@@ -96,7 +100,8 @@ typedef enum {
labcomm_reader_free,
labcomm_reader_start,
labcomm_reader_continue,
labcomm_reader_end
labcomm_reader_end,
labcomm_reader_ioctl
} labcomm_reader_action_t;
typedef struct labcomm_reader {
......@@ -106,6 +111,7 @@ typedef struct labcomm_reader {
int count;
int pos;
int (*read)(struct labcomm_reader *, labcomm_reader_action_t);
int (*ioctl)(struct labcomm_reader *, int, va_list);
labcomm_error_handler_callback on_error;
} labcomm_reader_t;
......@@ -124,14 +130,14 @@ void labcomm_decoder_free(
*/
typedef enum {
labcomm_writer_alloc, // no args
labcomm_writer_free, // no args
labcomm_writer_start, // no args
labcomm_writer_continue, // no args
labcomm_writer_end, // no args
labcomm_writer_available, // no args
labcomm_writer_send_signature, // args = (labcomm_signature_t*, struct labcomm_encoder*)
labcomm_writer_user_action // args = (int user_action, ... defined by the writer)
labcomm_writer_alloc, /* Allocate all neccessary data */
labcomm_writer_free, /* Free all allocated data */
labcomm_writer_start, /* Start writing an ordinary sample */
labcomm_writer_continue, /* Buffer full during ordinary sample */
labcomm_writer_end, /* End writing ordinary sample */
labcomm_writer_start_signature, /* Start writing signature */
labcomm_writer_continue_signature, /* Buffer full during signature */
labcomm_writer_end_signature, /* End writing signature */
} labcomm_writer_action_t;
typedef struct labcomm_writer {
......@@ -140,14 +146,20 @@ typedef struct labcomm_writer {
int data_size;
int count;
int pos;
int (*write)(struct labcomm_writer *, labcomm_writer_action_t, ...);
int error;
int (*write)(struct labcomm_writer *, labcomm_writer_action_t);
int (*ioctl)(struct labcomm_writer *, int, va_list);
labcomm_error_handler_callback on_error;
} labcomm_writer_t;
struct labcomm_encoder *labcomm_encoder_new(
int (*writer)(labcomm_writer_t *, labcomm_writer_action_t, ...),
int (*writer)(labcomm_writer_t *, labcomm_writer_action_t),
void *writer_context);
void labcomm_encoder_free(
struct labcomm_encoder *encoder);
/* See labcomm_ioctl.h for predefined ioctl_action values */
int labcomm_encoder_ioctl(struct labcomm_encoder *encoder,
int ioctl_action,
...);
#endif
#include "labcomm_dynamic_buffer_reader_writer.h"
int labcomm_dynamic_buffer_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:
case labcomm_writer_start_signature: {
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:
case labcomm_writer_continue_signature: {
w->data_size += 1000;
w->count = w->data_size;
w->data = realloc(w->data, w->data_size);
} break;
case labcomm_writer_end:
case labcomm_writer_end_signature: {
} break;
case labcomm_writer_free: {
free(w->data);
w->data = 0;
w->data_size = 0;
w->count = 0;
w->pos = 0;
} break;
}
return 0;
}
#ifndef _LABCOMM_DYNAMIC_BUFFER_READER_WRITER_H_
#define _LABCOMM_DYNAMIC_BUFFER_READER_WRITER_H_
#include "labcomm.h"
extern int labcomm_dynamic_buffer_reader(
labcomm_reader_t *reader,
labcomm_reader_action_t action);
extern int labcomm_dynamic_buffer_writer(
labcomm_writer_t *writer,
labcomm_writer_action_t action);
#endif
......@@ -53,15 +53,17 @@ int labcomm_fd_reader(
r->pos = 0;
result = 0;
} break;
case labcomm_reader_ioctl: {
result = -ENOTSUP;
}
}
return result;
}
int labcomm_fd_writer(
labcomm_writer_t *w,
labcomm_writer_action_t action, ...)
labcomm_writer_action_t action)
{
va_list argp;
int result = 0;
int *fd = w->context;
......@@ -86,33 +88,20 @@ int labcomm_fd_writer(
w->count = 0;
w->pos = 0;
} break;
case labcomm_writer_start: {
case labcomm_writer_start:
case labcomm_writer_start_signature: {
w->pos = 0;
} break;
case labcomm_writer_continue: {
case labcomm_writer_continue:
case labcomm_writer_continue_signature: {
result = write(*fd, w->data, w->pos);
w->pos = 0;
} break;
case labcomm_writer_end: {
case labcomm_writer_end:
case labcomm_writer_end_signature: {
result = write(*fd, w->data, w->pos);
w->pos = 0;
} break;
case labcomm_writer_available: {
result = w->count - w->pos;
} break;
case labcomm_writer_send_signature: {
//TODO: move to generic auxilliary writer functions file in lib?
va_start(argp, action);
labcomm_signature_t *signature = va_arg(argp, labcomm_signature_t*);
struct labcomm_encoder *e = va_arg(argp, struct labcomm_encoder*);
va_end(argp);
labcomm_encode_signature(e, signature);
} break;
case labcomm_writer_user_action: {
result = -ENOTSUP;
} break;
}
return result;
}
#ifndef _LABCOMM_FD_READER_WRITER_H_
#define _LABCOMM_FD_READER_WRITER_H_
#include "labcomm.h"
extern int labcomm_fd_reader(
labcomm_reader_t *r,
labcomm_reader_t *reader,
labcomm_reader_action_t action);
extern int labcomm_fd_writer(
labcomm_writer_t *r,
labcomm_writer_action_t action,
...);
labcomm_writer_t *writer,
labcomm_writer_action_t action);
#endif
#include "labcomm.h"
struct labcomm_ioctl_register_signature {
int index;
labcomm_signature_t *signature;
};
#define LABCOMM_IOCTL_REGISTER_SIGNATURE 0x0001
#define LABCOMM_IOCTL_WRITER_GET_BYTES_WRITTEN 0x0002
#define LABCOMM_IOCTL_WRITER_GET_BYTE_POINTER 0x0003
#define LABCOMM_IOCTL_WRITER_COPY_BYTES 0x0004
......@@ -39,7 +39,7 @@
/*
* Start index for user defined types
*/
#define LABCOMM_USER 0x60
#define LABCOMM_USER 0x40
/*
* Semi private decoder declarations
......@@ -196,7 +196,7 @@ static inline void labcomm_buffer_reader_setup(
void *data,
int length)
{
r->context = NULL; // Used as errer flag
r->context = NULL; // Used as error flag
r->data = data;
r->data_size = length;
r->count = length;
......@@ -233,10 +233,6 @@ void labcomm_internal_encode(
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) \
......@@ -335,7 +331,7 @@ static inline void labcomm_encode_string(labcomm_encoder_t *e,
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, ...)
labcomm_writer_action_t action)
{
// If this gets called, it is an error,
// so note error and let producer proceed
......
......@@ -67,6 +67,9 @@ int labcomm_mem_reader(labcomm_reader_t *r, labcomm_reader_action_t action)
r->pos = 0;
result = 0;
} break;
case labcomm_reader_ioctl: {
result = -ENOTSUP;
}
}
return result;
}
......
......@@ -27,9 +27,8 @@ static void copy_data(labcomm_writer_t *w, labcomm_mem_writer_context_t *mcontex
* Write encoded messages to memory. w->context is assumed to be a pointer to a
* labcomm_mem_writer_context_t structure.
*/
int labcomm_mem_writer(labcomm_writer_t *w, labcomm_writer_action_t action, ...)
int labcomm_mem_writer(labcomm_writer_t *w, labcomm_writer_action_t action)
{
va_list argp;
int result = 0;
// Unwrap pointers for easy access.
labcomm_mem_writer_context_t *mcontext = (labcomm_mem_writer_context_t *) w->context;
......@@ -66,14 +65,17 @@ int labcomm_mem_writer(labcomm_writer_t *w, labcomm_writer_action_t action, ...)
w->count = 0;
w->pos = 0;
} break;
case labcomm_writer_start:{
case labcomm_writer_start:
case labcomm_writer_start_signature: {
#if (ENCODED_BUFFER == 1)
w->pos = 0;
#elif (ENCODED_BUFFER == 2)
w->pos = mcontext->write_pos;
#endif
} break;
case labcomm_writer_continue:{ // Encode-buffer(w->data) is full; empty/handle it. (w->pos == w->count) most likely.
case labcomm_writer_continue:
case labcomm_writer_continue_signature: {
// Encode-buffer(w->data) is full; empty/handle it. (w->pos == w->count) most likely.
#if (ENCODED_BUFFER == 1)
copy_data(w, mcontext, mbuf);
result = w->pos; // Assume result here should be number of bytes written.
......@@ -82,8 +84,9 @@ int labcomm_mem_writer(labcomm_writer_t *w, labcomm_writer_action_t action, ...)
mcontext->write_pos = w->pos;
#endif
result = 0;
} break;
case labcomm_writer_end:{ // Nothing more to encode, handle encode-buffer(w->data).
} break;
case labcomm_writer_end:
case labcomm_writer_end_signature:{ // Nothing more to encode, handle encode-buffer(w->data).
#if (ENCODED_BUFFER == 1)
copy_data(w, mcontext, mbuf);
result = w->pos;
......@@ -93,19 +96,6 @@ int labcomm_mem_writer(labcomm_writer_t *w, labcomm_writer_action_t action, ...)
#endif
result = 0;
} break;
case labcomm_writer_available:{
result = w->count - w->pos;
} break;
case labcomm_writer_send_signature: {
va_start(argp, action);
labcomm_signature_t *signature = va_arg(argp, labcomm_signature_t*);
struct labcomm_encoder *e = va_arg(argp, struct labcomm_encoder*);
va_end(argp);
labcomm_encode_signature(e, signature);
} break;
case labcomm_writer_user_action:{
result = -ENOTSUP;
} break;
}
return result;
}
......
......@@ -11,7 +11,7 @@ struct labcomm_mem_writer_context_t {
unsigned char *buf; // Allocated destination buffer.
};
int labcomm_mem_writer(labcomm_writer_t *w, labcomm_writer_action_t action, ...);
int labcomm_mem_writer(labcomm_writer_t *w, labcomm_writer_action_t action);
/* Wrapper the internal static function copy_data. This is needed so that the exceptions can be unit tested. */
void test_copy_data(labcomm_writer_t *w, labcomm_mem_writer_context_t *mcontext, unsigned char *mbuf);
......
......@@ -25,7 +25,7 @@ namespace se.lth.control.labcomm {
/*
* start of user defined types
*/
public const int FIRST_USER_INDEX = 0x60;
public const int FIRST_USER_INDEX = 0x40;
}
......
......@@ -25,6 +25,6 @@ public class LabComm {
/*
* Start of
*/
public static final int FIRST_USER_INDEX = 0x60;
public static final int FIRST_USER_INDEX = 0x40;
}
......@@ -113,7 +113,7 @@ i_FLOAT = 0x25
i_DOUBLE = 0x26
i_STRING = 0x27
i_USER = 0x60
i_USER = 0x40
def indent(i, s):
return ("\n%s" % (" " * i)).join(s.split("\n"))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment