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 1907 additions and 651 deletions
#ifndef LABCOMM_MEM_WRITER_H
#define LABCOMM_MEM_WRITER_H
#include "labcomm.h"
/* Wrapper structure for the memory buffer including a writer position. */
typedef struct labcomm_mem_writer_context_t labcomm_mem_writer_context_t;
struct labcomm_mem_writer_context_t {
size_t write_pos; // Position where next write should be.
size_t length; // Length of the buffer.
unsigned char *buf; // Allocated destination buffer.
};
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);
/* Allocate new labcomm_mem_writer_context_t. */
labcomm_mem_writer_context_t *labcomm_mem_writer_context_t_new(size_t init_pos, size_t length, unsigned char *buf);
/* Deallocate mcontext. */
void labcomm_mem_writer_context_t_free(labcomm_mem_writer_context_t **mcontext);
#endif
sample string S;
sample int A[8];
sample struct {
string s1;
string s2;
} NS;
sample string AS[_];
/*
test_labcomm2014.c -- Various labcomm2014 tests
Copyright 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/>.
*/
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "labcomm2014_private.h"
#include "labcomm2014_default_error_handler.h"
#include "labcomm2014_default_memory.h"
#include "labcomm2014_default_scheduler.h"
#include "test/gen/test_sample.h"
static struct labcomm2014_writer writer;
static struct expect {
char *description;
int result;
int (*trampoline)(struct labcomm2014_decoder *context);
struct labcomm2014_decoder *context;
} *expect;
static void check_expect()
{
struct expect *p = expect;
{
int i;
for (i = 0 ; i < writer.pos ; i++) {
fprintf(stderr, "%02x ", writer.data[i]);
}
fprintf(stderr, "\n");
}
if (p && p->trampoline) {
int err;
expect = p + 1;
fprintf(stderr, "Checking '%s' expected=%d ", p->description, p->result);
err = p->trampoline(p->context);
fprintf(stderr, "actual=%d\n", err);
if (p->result >= 0 && p->result != err) {
fprintf(stderr, "FAILED\n");
exit(1);
} else if (err == 0) {
fprintf(stderr, "FAILED (unexpected 0)\n");
exit(1);
}
writer.pos = 0;
}
}
static unsigned char buffer[512];
static int writer_alloc(struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context)
{
w->data = buffer;
w->data_size = sizeof(buffer);
w->count = sizeof(buffer);
return 0;
}
static int writer_start(struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context,
int index, const struct labcomm2014_signature *signature,
void *value)
{
return 0;
}
static int buf_writer_end(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context)
{
check_expect();
return 0;
}
const struct labcomm2014_writer_action writer_action = {
.alloc = writer_alloc,
.start = writer_start,
.end = buf_writer_end,
};
static struct labcomm2014_writer_action_context writer_action_context = {
.next = NULL,
.action = &writer_action,
.context = NULL
};
static struct labcomm2014_writer writer = {
.action_context = &writer_action_context,
.data = buffer,
.data_size = sizeof(buffer),
.count = sizeof(buffer),
.pos = 0,
.error = 0,
};
static int reader_alloc(struct labcomm2014_reader *r,
struct labcomm2014_reader_action_context *action_context)
{
r->data = buffer;
r->data_size = sizeof(buffer);
r->count = 0;
r->memory = labcomm2014_default_memory;
return 0;
}
static int reader_fill(struct labcomm2014_reader *r,
struct labcomm2014_reader_action_context *action_context)
{
r->error = -ENOMEM;
return r->error;
}
const struct labcomm2014_reader_action reader_action = {
.alloc = reader_alloc,
.fill = reader_fill,
};
static struct labcomm2014_reader_action_context reader_action_context = {
.next = NULL,
.action = &reader_action,
.context = NULL
};
static struct labcomm2014_reader reader = {
.action_context = &reader_action_context,
.data = buffer,
.data_size = sizeof(buffer),
.count = 0,
.pos = 0,
.error = 0,
};
static int32_t encoder_data[256];
static test_sample_test_var encoder_var = {
.n_0 = 1,
.n_2 = 1,
.a = encoder_data,
};
static int32_t decoder_data[256];
static test_sample_test_var decoder_var = {
.n_0 = 1,
.n_2 = 1,
.a = decoder_data,
};;
void handle_test_var(test_sample_test_var *v, void *ctx)
{
if (v->n_0 * v->n_2 > 0) {
decoder_var.a[0] = v->a[0];
}
}
int test_decode_one(struct labcomm2014_decoder *decoder)
{
int result;
for (reader.count = 0 ; reader.count < writer.pos ; reader.count++) {
reader.error = 0;
reader.pos = 0;
result = labcomm2014_decoder_decode_one(decoder);
if (result >= 0 ) {
fprintf(stderr,
"Got result from buffer with bogus length (%d, %d != %d)\n",
result, reader.count, writer.pos);
exit(1);
}
}
reader.error = 0;
reader.pos = 0;
reader.count = writer.pos;
result = labcomm2014_decoder_decode_one(decoder);
if (result < 0) {
fprintf(stderr, "Got no result from buffer with correct length (%d)\n",
result);
exit(1);
}
return result;
}
static void test_encode_decode(struct labcomm2014_encoder *encoder,
struct labcomm2014_decoder *decoder,
int expected, uint32_t n_0, uint32_t n_2)
{
int err;
writer.pos = 0;
encoder_var.n_0 = n_0;
encoder_var.n_2 = n_2;
encoder_var.a[0] = 314;
labcomm2014_encode_test_sample_test_var(encoder, &encoder_var);
err = test_decode_one(decoder);
fprintf(stderr, "decode of sample %u * 2 * %u -> size=%d err=%d\n",
n_0, n_2, writer.pos, err);
if (writer.pos != labcomm2014_sizeof_test_sample_test_var(NULL, &encoder_var)) {
fprintf(stderr, "Incorrect sizeof %u * 2 * %u (%d != %d)\n",
n_0, n_2,
writer.pos, labcomm2014_sizeof_test_sample_test_var(NULL, &encoder_var));
exit(1);
}
if (writer.pos != expected) {
fprintf(stderr, "Unexpected size %u * 2 * %u (%d != %d)\n",
n_0, n_2,
writer.pos, expected);
exit(1);
}
}
static int do_test(int argc, char *argv[])
{
int i;
struct labcomm2014_decoder *decoder = labcomm2014_decoder_new(
&reader,
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
struct expect expect_version[] = {
{ "Version", 1, test_decode_one, decoder },
{ 0, 0, 0 }
};
expect = expect_version;
struct labcomm2014_encoder *encoder = labcomm2014_encoder_new(
&writer,
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
labcomm2014_decoder_register_test_sample_test_var(decoder,
handle_test_var,
NULL);
struct expect expect_registration[] = {
{ "Sampledef", -1, test_decode_one, decoder },
#ifdef SHOULD_THIS_BE_REMOVED
{ "Typedef", -1, test_decode_one, decoder },
#endif
{ "Binding", -1, test_decode_one, decoder },
{ 0, 0, 0 }
};
expect = expect_registration;
labcomm2014_encoder_register_test_sample_test_var(encoder);
test_encode_decode(encoder, decoder, 12, 1, 1);
if (decoder_var.a[0] != encoder_var.a[0]) {
fprintf(stderr, "Failed to decode correct value %d != %d\n",
encoder_var.a[0], decoder_var.a[0]);
exit(1);
}
test_encode_decode(encoder, decoder, 36, 2, 2);
test_encode_decode(encoder, decoder, 4, 0, 0);
for (i = 1 ; i <= 4 ; i++) {
test_encode_decode(encoder, decoder, 3+i, 0, (1<<(7*i))-1);
test_encode_decode(encoder, decoder, 4+i, 0, (1<<(7*i)));
}
test_encode_decode(encoder, decoder, 8, 0, 4294967295);
labcomm2014_encoder_free(encoder);
labcomm2014_decoder_free(decoder);
return 0;
}
int main(int argc, char *argv[])
{
return do_test(argc, argv);
}
/*
test_labcomm2014_basic_type_encoding.c -- LabComm tests of basic encoding
Copyright 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/>.
*/
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include "labcomm2014_private.h"
static int line;
static unsigned char buffer[128];
static struct labcomm2014_writer writer = {
.action_context = NULL,
.data = buffer,
.data_size = sizeof(buffer),
.count = sizeof(buffer),
.pos = 0,
.error = 0,
};
static struct labcomm2014_reader reader = {
.action_context = NULL,
.data = buffer,
.data_size = sizeof(buffer),
.count = 0,
.pos = 0,
};
typedef uint32_t packed32;
#define TEST_WRITE_READ(type, ltype, format, value, expect_count, expect_bytes) \
{ \
type decoded; \
line = __LINE__; \
writer.pos = 0; \
labcomm2014_write_##ltype(&writer, value); \
writer_assert(#ltype, expect_count, (uint8_t*)expect_bytes); \
reader.count = writer.pos; \
reader.pos = 0; \
decoded = labcomm2014_read_##ltype(&reader); \
if (decoded != value) { \
fprintf(stderr, "Decode error" format " != " format " @%s:%d \n", \
value, decoded, __FILE__, __LINE__); \
exit(1); \
} \
}
static void writer_assert(char *type,
int count,
uint8_t *bytes)
{
if (writer.pos != count) {
fprintf(stderr,
"Wrong number of bytes written for '%s' (%d != %d) @%s:%d\n",
type, writer.pos, count, __FILE__, line);
exit(1);
}
if (memcmp(writer.data, bytes, count) != 0) {
int i;
fprintf(stderr, "Wrong bytes written for '%s' ( ", type);
for (i = 0 ; i < count ; i++) {
fprintf(stderr, "%2.2x ", writer.data[i]);
}
fprintf(stderr, "!= ");
for (i = 0 ; i < count ; i++) {
fprintf(stderr, "%2.2x ", bytes[i]);
}
fprintf(stderr, ") @%s:%d\n", __FILE__, line);
exit(1);
}
}
static int do_test(int argc, char *argv[])
{
TEST_WRITE_READ(packed32, packed32, "%d", 0x0, 1, "\x00");
TEST_WRITE_READ(packed32, packed32, "%d", 0x7f, 1, "\x7f");
TEST_WRITE_READ(packed32, packed32, "%d", 0x80, 2, "\x81\x00");
TEST_WRITE_READ(packed32, packed32, "%d", 0x3fff, 2, "\xff\x7f");
TEST_WRITE_READ(packed32, packed32, "%d", 0x4000, 3, "\x81\x80\x00");
TEST_WRITE_READ(packed32, packed32, "%d", 0x1fffff, 3, "\xff\xff\x7f");
TEST_WRITE_READ(packed32, packed32, "%d", 0x200000, 4, "\x81\x80\x80\x00");
TEST_WRITE_READ(packed32, packed32, "%d", 0xfffffff, 4, "\xff\xff\xff\x7f");
TEST_WRITE_READ(packed32, packed32, "%d", 0x10000000, 5, "\x81\x80\x80\x80\x00");
TEST_WRITE_READ(packed32, packed32, "%d", 0xffffffff, 5, "\x8f\xff\xff\xff\x7f");
TEST_WRITE_READ(uint8_t, boolean, "%d", 0, 1, "\x00");
TEST_WRITE_READ(uint8_t, boolean, "%d", 1, 1, "\x01");
TEST_WRITE_READ(uint8_t, byte, "%d", 0, 1, "\x00");
TEST_WRITE_READ(uint8_t, byte, "%d", 1, 1, "\x01");
TEST_WRITE_READ(uint8_t, byte, "%d", 0xff, 1, "\xff");
TEST_WRITE_READ(int16_t, short, "%d", 0, 2, "\x00\x00");
TEST_WRITE_READ(int16_t, short, "%d", 0x7fff, 2, "\x7f\xff");
TEST_WRITE_READ(int16_t, short, "%d", -1, 2, "\xff\xff");
TEST_WRITE_READ(int32_t, int, "%d", 0, 4, "\x00\x00\x00\x00");
TEST_WRITE_READ(int32_t, int, "%d", 0x7fffffff, 4, "\x7f\xff\xff\xff");
TEST_WRITE_READ(int32_t, int, "%d", -1, 4, "\xff\xff\xff\xff");
TEST_WRITE_READ(int64_t, long, "%" PRId64, INT64_C(0), 8, "\x00\x00\x00\x00\x00\x00\x00\x00");
TEST_WRITE_READ(int64_t, long, "%" PRId64, INT64_C(0x7fffffffffffffff), 8, "\x7f\xff\xff\xff\xff\xff\xff\xff");
TEST_WRITE_READ(int64_t, long, "%" PRId64, INT64_C(-1), 8, "\xff\xff\xff\xff\xff\xff\xff\xff");
TEST_WRITE_READ(float, float, "%f", 0.0, 4, "\x00\x00\x00\x00");
TEST_WRITE_READ(float, float, "%f", 1.0, 4, "\x3f\x80\x00\x00");
TEST_WRITE_READ(float, float, "%f", 2.0, 4, "\x40\x00\x00\x00");
TEST_WRITE_READ(float, float, "%f", 0.5, 4, "\x3f\x00\x00\x00");
TEST_WRITE_READ(float, float, "%f", 0.25, 4, "\x3e\x80\x00\x00");
TEST_WRITE_READ(float, float, "%f", -0.0, 4, "\x80\x00\x00\x00");
TEST_WRITE_READ(float, float, "%f", -1.0, 4, "\xbf\x80\x00\x00");
TEST_WRITE_READ(float, float, "%f", -2.0, 4, "\xc0\x00\x00\x00");
TEST_WRITE_READ(float, float, "%f", -0.5, 4, "\xbf\x00\x00\x00");
TEST_WRITE_READ(float, float, "%f", -0.25, 4, "\xbe\x80\x00\x00");
TEST_WRITE_READ(double, double, "%f", 0.0, 8, "\x00\x00\x00\x00\x00\x00\x00\x00");
TEST_WRITE_READ(double, double, "%f", 1.0, 8, "\x3f\xf0\x00\x00\x00\x00\x00\x00");
TEST_WRITE_READ(double, double, "%f", 2.0, 8, "\x40\x00\x00\x00\x00\x00\x00\x00");
TEST_WRITE_READ(double, double, "%f", 0.5, 8, "\x3f\xe0\x00\x00\x00\x00\x00\x00");
TEST_WRITE_READ(double, double, "%f", 0.25, 8, "\x3f\xd0\x00\x00\x00\x00\x00\x00");
TEST_WRITE_READ(double, double, "%f", -0.0, 8, "\x80\x00\x00\x00\x00\x00\x00\x00");
TEST_WRITE_READ(double, double, "%f", -1.0, 8, "\xbf\xf0\x00\x00\x00\x00\x00\x00");
TEST_WRITE_READ(double, double, "%f", -2.0, 8, "\xc0\x00\x00\x00\x00\x00\x00\x00");
TEST_WRITE_READ(double, double, "%f", -0.5, 8, "\xbf\xe0\x00\x00\x00\x00\x00\x00");
TEST_WRITE_READ(double, double, "%f", -0.25, 8, "\xbf\xd0\x00\x00\x00\x00\x00\x00");
fprintf(stderr, "%s succeded\n", __FILE__);
return 0;
}
int main(int argc, char *argv[])
{
return do_test(argc, argv);
}
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <err.h>
#include "labcomm2014.h"
#include "labcomm2014_private.h"
#include "labcomm2014_default_error_handler.h"
#include "labcomm2014_default_memory.h"
#include "labcomm2014_default_scheduler.h"
#include "labcomm2014_fd_writer.h"
#include "labcomm2014_fd_reader.h"
#include "test/gen/generated_encoding.h"
#include "test/gen/test_sample.h"
#include "test/gen/more_types.h"
#define DATA_FILE "copy_test.dat"
static void handle_s1(generated_encoding_S1 *v, void *context)
{
labcomm2014_copy_generated_encoding_S1(labcomm2014_default_memory, context, v);
}
static void handle_b(generated_encoding_B *v, void *context)
{
labcomm2014_copy_generated_encoding_B(labcomm2014_default_memory, context, v);
}
static void handle_i(generated_encoding_I *v, void *context)
{
labcomm2014_copy_generated_encoding_I(labcomm2014_default_memory, context, v);
}
static void handle_p(generated_encoding_P *v, void *context)
{
labcomm2014_copy_generated_encoding_P(labcomm2014_default_memory, context, v);
}
static void handle_r(generated_encoding_R *v, void *context)
{
labcomm2014_copy_generated_encoding_R(labcomm2014_default_memory, context, v);
}
static void handle_test_var(test_sample_test_var *v, void *context)
{
labcomm2014_copy_test_sample_test_var(labcomm2014_default_memory, context, v);
}
static void handle_a(more_types_A *v, void *context)
{
labcomm2014_copy_more_types_A(labcomm2014_default_memory, context, v);
}
static void handle_s(more_types_S *v, void *context)
{
labcomm2014_copy_more_types_S(labcomm2014_default_memory, context, v);
}
static void handle_ns(more_types_NS *v, void *context)
{
labcomm2014_copy_more_types_NS(labcomm2014_default_memory, context, v);
}
static void handle_as(more_types_AS *v, void *context)
{
labcomm2014_copy_more_types_AS(labcomm2014_default_memory, context, v);
}
int do_test(int argc, char **argv)
{
struct labcomm2014_encoder *encoder;
struct labcomm2014_decoder *decoder;
int fd;
generated_encoding_S1 s1;
generated_encoding_S1 cache_s1;
generated_encoding_B b;
generated_encoding_B cache_b;
generated_encoding_I I;
generated_encoding_I cache_I;
generated_encoding_P p;
generated_encoding_P cache_p;
test_sample_test_var test_var;
test_sample_test_var cache_test_var;
more_types_A a;
more_types_A cache_a;
more_types_S s;
more_types_S cache_s = NULL;
more_types_NS ns;
more_types_NS cache_ns;
more_types_AS as;
more_types_AS cache_as;
generated_encoding_R r;
generated_encoding_R cache_r;
fd = open(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd == -1)
err(1, "open()");
encoder =
labcomm2014_encoder_new(labcomm2014_fd_writer_new(labcomm2014_default_memory, fd, 0),
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
labcomm2014_encoder_register_generated_encoding_S1(encoder);
s1.i = 1;
labcomm2014_encode_generated_encoding_S1(encoder, &s1);
labcomm2014_encoder_register_generated_encoding_B(encoder);
b = 2;
labcomm2014_encode_generated_encoding_B(encoder, &b);
labcomm2014_encoder_register_generated_encoding_I(encoder);
I.n_0 = 3;
I.a = calloc(I.n_0, sizeof(I.a[0]));
I.a[0] = 4;
I.a[1] = 5;
I.a[2] = 6;
labcomm2014_encode_generated_encoding_I(encoder, &I);
labcomm2014_encoder_register_generated_encoding_P(encoder);
p.n_0 = 7;
p.a = calloc(p.n_0, sizeof(p.a[0]));
for (int i = 0; i < p.n_0; i++)
p.a[i].i = 8 + i;
labcomm2014_encode_generated_encoding_P(encoder, &p);
labcomm2014_encoder_register_test_sample_test_var(encoder);
test_var.n_0 = 2;
test_var.n_2 = 7;
test_var.a = calloc(test_var.n_0 * 2 * test_var.n_2, sizeof(*test_var.a));
for (int i = 0; i < test_var.n_0; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < test_var.n_2; k++) {
test_var.a[(((i) * 2 + j) * test_var.n_2) + k] = 100 * i + 10 * j + k;
}
labcomm2014_encode_test_sample_test_var(encoder, &test_var);
labcomm2014_encoder_register_more_types_A(encoder);
for (int i = 0; i < sizeof(a.a) / sizeof(a.a[0]); i++)
a.a[i] = i;
labcomm2014_encode_more_types_A(encoder, &a);
labcomm2014_encoder_register_more_types_S(encoder);
s = "this is a string";
labcomm2014_encode_more_types_S(encoder, &s);
labcomm2014_encoder_register_more_types_NS(encoder);
ns.s1 = "this is a string";
ns.s2 = "this is a another string";
labcomm2014_encode_more_types_NS(encoder, &ns);
labcomm2014_encoder_register_more_types_AS(encoder);
as.n_0 = 3;
as.a = calloc(as.n_0, sizeof(as.a[0]));
as.a[0] = "string 0";
as.a[1] = "string 1";
as.a[2] = "string 2";
labcomm2014_encode_more_types_AS(encoder, &as);
labcomm2014_encoder_register_generated_encoding_R(encoder);
labcomm2014_encoder_sample_ref_register(encoder, labcomm2014_signature_generated_encoding_V);
labcomm2014_encoder_sample_ref_register(encoder,
labcomm2014_signature_generated_encoding_UnusedD);
labcomm2014_encoder_sample_ref_register(encoder, labcomm2014_signature_generated_encoding_R);
r.a[0] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_V);
r.a[1] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_UnusedE);
r.a[2] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_UnusedD);
r.a[3] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_encode_generated_encoding_R(encoder, &r);
labcomm2014_encoder_free(encoder);
encoder = NULL;
lseek(fd, 0, SEEK_SET);
decoder =
labcomm2014_decoder_new(labcomm2014_fd_reader_new(labcomm2014_default_memory, fd, 0),
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
labcomm2014_decoder_register_generated_encoding_S1(decoder, handle_s1, &cache_s1);
labcomm2014_decoder_register_generated_encoding_B(decoder, handle_b, &cache_b);
labcomm2014_decoder_register_generated_encoding_I(decoder, handle_i, &cache_I);
labcomm2014_decoder_register_generated_encoding_P(decoder, handle_p, &cache_p);
labcomm2014_decoder_register_test_sample_test_var(decoder, handle_test_var,
&cache_test_var);
labcomm2014_decoder_register_more_types_A(decoder, handle_a, &cache_a);
labcomm2014_decoder_register_more_types_S(decoder, handle_s, &cache_s);
labcomm2014_decoder_register_more_types_NS(decoder, handle_ns, &cache_ns);
labcomm2014_decoder_register_more_types_AS(decoder, handle_as, &cache_as);
labcomm2014_decoder_register_generated_encoding_R(decoder, handle_r, &cache_r);
labcomm2014_decoder_sample_ref_register(decoder, labcomm2014_signature_generated_encoding_V);
labcomm2014_decoder_sample_ref_register(decoder,
labcomm2014_signature_generated_encoding_UnusedE);
labcomm2014_decoder_sample_ref_register(decoder, labcomm2014_signature_generated_encoding_R);
while (labcomm2014_decoder_decode_one(decoder) > 0) ;
printf("cache_s1.i = %d, s1.i = %d\n", cache_s1.i, s1.i);
assert(cache_s1.i == s1.i);
puts("S1 copied ok");
assert(cache_b == b);
puts("B copied ok");
assert(cache_I.n_0 == I.n_0);
assert(cache_I.a[0] == I.a[0]);
assert(cache_I.a[1] == I.a[1]);
assert(cache_I.a[2] == I.a[2]);
free(I.a);
puts("I copied ok");
assert(cache_p.n_0 == p.n_0);
for (int i = 0; i < p.n_0; i++)
assert(cache_p.a[i].i == p.a[i].i);
free(p.a);
puts("P copied ok");
assert(cache_test_var.n_0 == test_var.n_0);
assert(cache_test_var.n_2 == test_var.n_2);
for (int i = 0; i < test_var.n_0; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < test_var.n_2; k++) {
assert(cache_test_var.a[(((i) * 2 + j) * test_var.n_2) + k] ==
test_var.a[(((i) * 2 + j) * test_var.n_2) + k]);
assert(cache_test_var.a[(((i) * 2 + j) * test_var.n_2) + k] ==
100 * i + 10 * j + k);
}
free(test_var.a);
puts("test_var copied ok");
for (int i = 0; i < sizeof(a.a) / sizeof(a.a[0]); i++)
assert(cache_a.a[i] == a.a[i]);
puts("A copied ok");
assert(!strcmp(cache_s, s));
puts("S copied ok");
assert(!strcmp(cache_ns.s1, ns.s1));
assert(!strcmp(cache_ns.s2, ns.s2));
puts("NS copied ok");
for (int i = 0; i < as.n_0; i++)
assert(!strcmp(cache_as.a[i], as.a[i]));
free(as.a);
puts("AS copied ok");
fprintf(stderr, "%p %p\n", r.a[0], cache_r.a[0]);
fprintf(stderr, "%p %p\n", r.a[1], cache_r.a[1]);
fprintf(stderr, "%p %p\n", r.a[2], cache_r.a[2]);
fprintf(stderr, "%p %p\n", r.a[3], cache_r.a[3]);
assert(cache_r.a[0] == r.a[0]);
assert(cache_r.a[1] == NULL); /* UnusedE */
assert(cache_r.a[2] == NULL); /* UnusedD */
assert(cache_r.a[3] == r.a[3]);
puts("R copied ok");
labcomm2014_decoder_free(decoder);
close(fd);
unlink(DATA_FILE);
labcomm2014_copy_free_generated_encoding_S1(labcomm2014_default_memory, &cache_s1);
puts("S1 deallocated ok");
labcomm2014_copy_free_generated_encoding_B(labcomm2014_default_memory, &cache_b);
puts("B deallocated ok");
labcomm2014_copy_free_generated_encoding_I(labcomm2014_default_memory, &cache_I);
puts("I deallocated ok");
labcomm2014_copy_free_generated_encoding_P(labcomm2014_default_memory, &cache_p);
puts("P deallocated ok");
labcomm2014_copy_free_test_sample_test_var(labcomm2014_default_memory, &cache_test_var);
puts("test_var deallocated ok");
labcomm2014_copy_free_more_types_A(labcomm2014_default_memory, &cache_a);
puts("A deallocated ok");
labcomm2014_copy_free_more_types_S(labcomm2014_default_memory, &cache_s);
puts("S deallocated ok");
labcomm2014_copy_free_more_types_NS(labcomm2014_default_memory, &cache_ns);
puts("NS deallocated ok");
labcomm2014_copy_free_more_types_AS(labcomm2014_default_memory, &cache_as);
puts("AS deallocated ok");
labcomm2014_copy_free_generated_encoding_R(labcomm2014_default_memory,
&cache_r);
puts("R deallocated ok");
return 0;
}
int main(int argc, char **argv)
{
return do_test(argc, argv);
}
#include "test_labcomm_errors.h"
#include <stdlib.h>
#include <labcomm.h>
#include <labcomm_private.h>
#include <labcomm_mem_writer.h>
#include <labcomm_mem_reader.h>
static enum labcomm_error callback_error_id;
int assert_callback(enum labcomm_error expected, const char *name, const char *err_msg)
{
int success;
printf("----> %s()\n", name);
if (callback_error_id == expected) {
printf("Succeeded.\n");
success = 1;
} else {
printf("Failed! %s\n", err_msg);
success = 0;
}
return success;
}
/* Our callback that just logs which error_id that the library reported. */
void test_callback(enum labcomm_error error_id, size_t nbr_va_args, ...)
{
va_list arg_pointer;
va_start(arg_pointer, nbr_va_args);
va_end(arg_pointer);
callback_error_id = error_id;
}
void reset_callback_erro_id()
{
callback_error_id = -128;
}
int encoded_size_mock(struct labcomm_signature *signature, void *voidp)
{
return 0;
}
int test_enc_not_reg_encoder_sign()
{
reset_callback_erro_id();
unsigned char *buf = (unsigned char *) "a";
labcomm_mem_writer_context_t *mcontext = labcomm_mem_writer_context_t_new(0, 1, buf);
labcomm_encoder_t *encoder = labcomm_encoder_new(labcomm_mem_writer, mcontext);
labcomm_register_error_handler_encoder(encoder, test_callback);
labcomm_signature_t signature = {
.type = 0,
.name = "test_signature",
.encoded_size = encoded_size_mock,
.size = 0,
.signature = (unsigned char *) "0"};
encoder->do_encode(encoder, &signature, NULL);
return assert_callback(LABCOMM_ERROR_ENC_NO_REG_SIGNATURE, __FUNCTION__, "");
}
int test_enc_missing_do_reg()
{
reset_callback_erro_id();
unsigned char *buf = (unsigned char *) "a";
labcomm_mem_writer_context_t *mcontext = labcomm_mem_writer_context_t_new(0, 1, buf);
labcomm_encoder_t *encoder = labcomm_encoder_new(labcomm_mem_writer, mcontext);
labcomm_register_error_handler_encoder(encoder, test_callback);
encoder->do_register = NULL;
labcomm_internal_encoder_register(encoder, NULL, NULL);
return assert_callback(LABCOMM_ERROR_ENC_MISSING_DO_REG, __FUNCTION__, "");
}
int test_enc_missing_do_encode()
{
reset_callback_erro_id();
unsigned char *buf = (unsigned char *) "a";
labcomm_mem_writer_context_t *mcontext = labcomm_mem_writer_context_t_new(0, 1, buf);
labcomm_encoder_t *encoder = labcomm_encoder_new(labcomm_mem_writer, mcontext);
labcomm_register_error_handler_encoder(encoder, test_callback);
encoder->do_encode = NULL;
labcomm_internal_encode(encoder, NULL, NULL);
return assert_callback(LABCOMM_ERROR_ENC_MISSING_DO_ENCODE, __FUNCTION__, "");
}
int test_enc_buf_full()
{
reset_callback_erro_id();
unsigned char *buf = (unsigned char *) "a";
labcomm_mem_writer_context_t *mcontext = labcomm_mem_writer_context_t_new(0, 1, buf);
labcomm_encoder_t *encoder = labcomm_encoder_new(labcomm_mem_writer, mcontext);
labcomm_register_error_handler_encoder(encoder, test_callback);
unsigned char *mbuf = mcontext->buf;
labcomm_writer_t writer = encoder->writer;
writer.data = malloc(1);
writer.pos = 1;
mcontext->write_pos = 1;
test_copy_data(&writer, mcontext, mbuf);
return assert_callback(LABCOMM_ERROR_ENC_BUF_FULL, __FUNCTION__, "");
}
void labcomm_decoder_typecast_t_mock(struct labcomm_decoder *decoder, labcomm_handler_typecast_t handler, void *voidp)
{
;
}
void labcomm_handler_typecast_t_mock(void *arg1, void *arg2)
{
;
}
int test_dec_missing_do_reg()
{
reset_callback_erro_id();
unsigned char *buf = (unsigned char *) "a";
labcomm_mem_reader_context_t *mcontext = (labcomm_mem_reader_context_t *) malloc(sizeof(labcomm_mem_reader_context_t));
labcomm_decoder_t *decoder = labcomm_decoder_new(labcomm_mem_reader, mcontext);
labcomm_register_error_handler_decoder(decoder, test_callback);
decoder->do_register = NULL;
labcomm_internal_decoder_register(decoder, NULL, labcomm_decoder_typecast_t_mock, labcomm_handler_typecast_t_mock, buf);
return assert_callback(LABCOMM_ERROR_DEC_MISSING_DO_REG, __FUNCTION__, "");
}
int test_dec_missing_do_decode_one()
{
reset_callback_erro_id();
labcomm_mem_reader_context_t *mcontext = (labcomm_mem_reader_context_t *) malloc(sizeof(labcomm_mem_reader_context_t));
labcomm_decoder_t *decoder = labcomm_decoder_new(labcomm_mem_reader, mcontext);
labcomm_register_error_handler_decoder(decoder, test_callback);
decoder->do_decode_one = NULL;
labcomm_decoder_decode_one(decoder);
return assert_callback(LABCOMM_ERROR_DEC_MISSING_DO_DECODE_ONE, __FUNCTION__, "");
}
int main()
{
printf("####> Begin tests.\n");
unsigned int nbr_succeed = 0;
unsigned int nbr_tests = 6; // Increment this when new tests are written.
nbr_succeed += test_enc_not_reg_encoder_sign();
nbr_succeed += test_enc_missing_do_reg();
nbr_succeed += test_enc_missing_do_encode();
nbr_succeed += test_enc_buf_full();
nbr_succeed += test_dec_missing_do_reg();
nbr_succeed += test_dec_missing_do_decode_one();
// Too tedius to test really...
//nbr_succeed += test_dec_unknown_datatype();
//nbr_succeed += test_dec_index_mismatch();
//nbr_succeed += test_dec_type_not_found();
//nbr_succeed += test_unimplemented_func(); // This test will be obsolete in the future ;-)
//nbr_succeed += test_user_def(); // There are no user defined errors in the library of course.
printf("####> End tests.\nSummary: %u/%u tests succeed.\n", nbr_succeed, nbr_tests);
if (nbr_succeed == nbr_tests) {
return EXIT_SUCCESS;
} else {
return EXIT_FAILURE;
}
}
#ifndef TEST_LABCOMM_ERRORS_H
#define TEST_LABCOMM_ERRORS_H
void test_not_reg_encoder_sign();
#endif
/*
test_labcomm2014_generated_encoding.c -- LabComm tests of generated encoding
Copyright 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/>.
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "labcomm2014_private.h"
#include "labcomm2014_default_error_handler.h"
#include "labcomm2014_default_memory.h"
#include "labcomm2014_pthread_scheduler.h"
#include "test/gen/generated_encoding.h"
#define IOCTL_WRITER_ASSERT_BYTES 4096
#define IOCTL_WRITER_RESET 4097
#define EXPECT(...) \
{ \
int expected[] = __VA_ARGS__; \
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_ASSERT_BYTES, \
__LINE__, \
sizeof(expected)/sizeof(expected[0]), \
expected); \
}
#define VARIABLE(i) -(i + 1)
#define IS_VARIABLE(i) (i < 0)
static unsigned char buffer[128];
struct labcomm2014_writer *writer;
static int seen_variable[1024];
static int buf_writer_alloc(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context)
{
writer = w; /* Hack */
w->data_size = sizeof(buffer);
w->count = w->data_size;
w->data = buffer;
w->pos = 0;
return 0;
}
static int buf_writer_free(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context)
{
return 0;
}
static int buf_writer_start(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context,
int index,
const struct labcomm2014_signature *signature,
void *value)
{
return 0;
}
static int buf_writer_end(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context)
{
return 0;
}
static int buf_writer_flush(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context)
{
fprintf(stderr, "Should not come here %s:%d\n", __FILE__, __LINE__);
exit(1);
return 0;
}
static int buf_writer_ioctl(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context,
int signature_index, const struct labcomm2014_signature *signature,
uint32_t action, va_list arg)
{
int result = -ENOTSUP;
switch (action) {
case IOCTL_WRITER_ASSERT_BYTES: {
int line = va_arg(arg, int);
int count = va_arg(arg, int);
int *expected = va_arg(arg, int *);
int i, mismatch;
mismatch = 0;
if (w->pos != count) {
fprintf(stderr, "Invalid length detected %d != %d (%s:%d)\n",
w->pos, count, __FILE__, line);
mismatch = 1;
}
for (i = 0 ; i < count ; i++) {
if (IS_VARIABLE(expected[i])) {
if (seen_variable[VARIABLE(expected[i])] == -1) {
seen_variable[VARIABLE(expected[i])] = buffer[i];
}
if (seen_variable[VARIABLE(expected[i])] != buffer[i]) {
fprintf(stderr, "Unexpected variable (%d: != %d)\n",
seen_variable[VARIABLE(expected[i])], buffer[i]);
mismatch = 1;
}
} else if (expected[i] != buffer[i]) {
mismatch = 1;
}
}
if (mismatch) {
fprintf(stderr, "Encoder mismatch (%s:%d)\n",
__FILE__, line);
for (i = 0 ; i < w->pos ; i++) {
printf("%2.2x ", w->data[i]);
}
printf("\n");
for (i = 0 ; i < count ; i++) {
if (expected[i] < 0) {
printf("v%d ", VARIABLE(expected[i]));
} else {
printf("%2.2x ", expected[i] );
}
}
printf("\n");
exit(1);
}
result = 0;
} break;
case IOCTL_WRITER_RESET: {
w->pos = 0;
result = 0;
}
}
return result;
}
const struct labcomm2014_writer_action writer_action = {
.alloc = buf_writer_alloc,
.free = buf_writer_free,
.start = buf_writer_start,
.end = buf_writer_end,
.flush = buf_writer_flush,
.ioctl = buf_writer_ioctl
};
static struct labcomm2014_writer_action_context action_context = {
.next = NULL,
.action = &writer_action,
.context = NULL
};
static struct labcomm2014_writer buffer_writer = {
.action_context = &action_context,
.data = buffer,
.data_size = sizeof(buffer),
.count = sizeof(buffer),
.pos = 0,
.error = 0,
};
void dump_encoder(struct labcomm2014_encoder *encoder)
{
int i;
for (i = 0 ; i < writer->pos ; i++) {
printf("%2.2x ", writer->data[i]);
}
printf("\n");
}
int main(void)
{
generated_encoding_B B = 1;
generated_encoding_R R;
struct labcomm2014_encoder *encoder;
int i;
for (i = 0 ; i < sizeof(seen_variable)/sizeof(seen_variable[0]) ; i++) {
seen_variable[i] = -1;
}
encoder = labcomm2014_encoder_new(
&buffer_writer,
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_pthread_scheduler_new(labcomm2014_default_memory));
EXPECT({ 0x01, 0x0c, 0x0b,
'L', 'a', 'b', 'C', 'o', 'm', 'm', '2','0', '1', '4' });
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_register_generated_encoding_V(encoder);
labcomm2014_encoder_register_generated_encoding_V(encoder);
EXPECT({ 0x02, 0x08, VARIABLE(0), 0x01, 0x00, 0x01, 'V', 0x02, 0x11, 0x00,
0x04, 0x0a, VARIABLE(1), 0x01, 0x00, 0x03, 'v', '_', 't', 0x02, 0x11, 0x00,
0x04, 0x07, VARIABLE(2), 0x01, 0x00, 0x01, 'V', 0x01, VARIABLE(1),
0x05, 0x02, VARIABLE(0), VARIABLE(2) });
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_register_generated_encoding_B(encoder);
labcomm2014_encoder_register_generated_encoding_B(encoder);
EXPECT({ 0x02, 0x07, VARIABLE(3),0x01, 0x00, 0x01, 'B', 0x01, 0x21,
0x05, 0x02, VARIABLE(3), LABCOMM_BIND_SELF });
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_register_generated_encoding_R(encoder);
labcomm2014_encoder_register_generated_encoding_R(encoder);
EXPECT({ 0x02, 0x0a, VARIABLE(4), 0x01, 0x00, 0x01, 'R', 0x04, 0x10, 0x01, 0x04, 0x28,
0x05, 0x02, VARIABLE(4), LABCOMM_BIND_SELF });
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_sample_ref_register(encoder,
labcomm2014_signature_generated_encoding_V);
labcomm2014_encoder_sample_ref_register(encoder,
labcomm2014_signature_generated_encoding_V);
EXPECT({0x03, 0x08, VARIABLE(5), 0x01, 0x00, 0x01, 'V', 0x02, 0x11, 0x00});
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_sample_ref_register(encoder,
labcomm2014_signature_generated_encoding_B);
labcomm2014_encoder_sample_ref_register(encoder,
labcomm2014_signature_generated_encoding_B);
EXPECT({0x03, 0x07, VARIABLE(6), 0x01, 0x00, 0x01, 'B', 0x01, 0x21});
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_sample_ref_register(encoder,
labcomm2014_signature_generated_encoding_R);
labcomm2014_encoder_sample_ref_register(encoder,
labcomm2014_signature_generated_encoding_R);
EXPECT({0x03, 0x0a, VARIABLE(7), 0x01, 0x00, 0x01, 'R', 0x04, 0x10, 0x01, 0x04, 0x28});
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
// was: labcomm2014_encode_generated_encoding_V(encoder, &V);
labcomm2014_encode_generated_encoding_V(encoder);
EXPECT({VARIABLE(10), 0x00 });
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
labcomm2014_encode_generated_encoding_B(encoder, &B);
EXPECT({VARIABLE(20), 0x01, 1});
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
R.a[0] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_V);
R.a[1] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_B);
R.a[2] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_UnusedE);
R.a[3] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_encode_generated_encoding_R(encoder, &R);
EXPECT({VARIABLE(4), 0x10, 0x00, 0x00, 0x00, VARIABLE(5),
0x00, 0x00, 0x00, VARIABLE(6),
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, VARIABLE(7)});
return 0;
}
/*
test_labcomm2014_pthread_scheduler.c -- test labcomm2014 pthread based task
coordination
Copyright 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/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include "labcomm2014_default_memory.h"
#include "labcomm2014_scheduler.h"
#include "labcomm2014_pthread_scheduler.h"
#define TICK 100000
struct func_arg {
struct labcomm2014_scheduler *scheduler;
int i;
};
static void func(void *arg)
{
struct func_arg *func_arg = arg;
printf("%p %d\n", arg, func_arg->i);
if (func_arg->i == 999) {
labcomm2014_scheduler_wakeup(func_arg->scheduler);
}
free(func_arg);
}
void enqueue(struct labcomm2014_scheduler *scheduler,
int first, int last)
{
int i;
for (i = first ; i <= last ; i++) {
struct func_arg *tmp = malloc(sizeof(*tmp));
tmp->scheduler = scheduler;
tmp->i = i;
labcomm2014_scheduler_enqueue(scheduler, i*TICK, func, tmp);
}
}
static int do_test(int argc, char *argv[])
{
struct labcomm2014_scheduler *scheduler;
struct labcomm2014_time *time;
scheduler = labcomm2014_pthread_scheduler_new(labcomm2014_default_memory);
enqueue(scheduler, 0, 5);
enqueue(scheduler, 0, 1);
enqueue(scheduler, 1, 3);
enqueue(scheduler, 7, 10);
{
struct func_arg *tmp = malloc(sizeof(*tmp));
tmp->scheduler = scheduler;
tmp->i = 999;
labcomm2014_scheduler_enqueue(scheduler, 6*TICK, func, tmp);
}
time = labcomm2014_scheduler_now(scheduler);
labcomm2014_time_add_usec(time, 12*TICK);
labcomm2014_scheduler_sleep(scheduler, NULL);
labcomm2014_scheduler_sleep(scheduler, time);
labcomm2014_time_free(time);
labcomm2014_scheduler_free(scheduler);
return 0;
}
int main(int argc, char *argv[])
{
return do_test(argc, argv);
}
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <err.h>
#include "labcomm2014.h"
#include "labcomm2014_private.h"
#include "labcomm2014_default_error_handler.h"
#include "labcomm2014_default_memory.h"
#include "labcomm2014_default_scheduler.h"
#include "labcomm2014_fd_writer.h"
#include "labcomm2014_fd_reader.h"
#include "labcomm2014_renaming.h"
#include "labcomm2014_renaming_encoder.h"
#include "labcomm2014_renaming_decoder.h"
#include "test/gen/generated_encoding.h"
#define DATA_FILE "renaming_test.dat"
static void handle_r(generated_encoding_R *v, void *context)
{
labcomm2014_copy_generated_encoding_R(labcomm2014_default_memory, context, v);
}
int main(int argc, char **argv)
{
struct labcomm2014_renaming_registry *registry;
struct labcomm2014_encoder *encoder, *prefix_encoder, *suffix_encoder;
struct labcomm2014_decoder *decoder, *prefix_decoder, *suffix_decoder;
int fd;
generated_encoding_R r;
generated_encoding_R cache_r, prefix_cache_r, suffix_cache_r;
fd = open(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
err(1, "open()");
}
registry = labcomm2014_renaming_registry_new(
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
encoder = labcomm2014_encoder_new(
labcomm2014_fd_writer_new(labcomm2014_default_memory, fd, 0),
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
prefix_encoder = labcomm2014_renaming_encoder_new(
encoder, registry, labcomm2014_renaming_prefix, "p.");
suffix_encoder = labcomm2014_renaming_encoder_new(
prefix_encoder, registry, labcomm2014_renaming_suffix, ".s");
labcomm2014_encoder_register_generated_encoding_R(encoder);
labcomm2014_encoder_register_generated_encoding_R(prefix_encoder);
labcomm2014_encoder_register_generated_encoding_R(suffix_encoder);
labcomm2014_encoder_sample_ref_register(
encoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_encoder_sample_ref_register(
prefix_encoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_encoder_sample_ref_register(
suffix_encoder, labcomm2014_signature_generated_encoding_R);
r.a[0] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_R);
r.a[1] = labcomm2014_encoder_get_sample_ref(
prefix_encoder, labcomm2014_signature_generated_encoding_R);
r.a[2] = labcomm2014_encoder_get_sample_ref(
suffix_encoder, labcomm2014_signature_generated_encoding_R);
r.a[3] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_encode_generated_encoding_R(encoder, &r);
r.a[3] = labcomm2014_encoder_get_sample_ref(
prefix_encoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_encode_generated_encoding_R(prefix_encoder, &r);
r.a[3] = labcomm2014_encoder_get_sample_ref(
suffix_encoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_encode_generated_encoding_R(suffix_encoder, &r);
labcomm2014_encoder_free(suffix_encoder);
labcomm2014_encoder_free(prefix_encoder);
labcomm2014_encoder_free(encoder);
encoder = NULL;
lseek(fd, 0, SEEK_SET);
decoder =
labcomm2014_decoder_new(
labcomm2014_fd_reader_new(labcomm2014_default_memory, fd, 0),
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
prefix_decoder = labcomm2014_renaming_decoder_new(
decoder, registry, labcomm2014_renaming_prefix, "p.");
suffix_decoder = labcomm2014_renaming_decoder_new(
prefix_decoder, registry, labcomm2014_renaming_suffix, ".s");
labcomm2014_decoder_register_generated_encoding_R(
decoder, handle_r, &cache_r);
labcomm2014_decoder_register_generated_encoding_R(
prefix_decoder, handle_r, &prefix_cache_r);
labcomm2014_decoder_register_generated_encoding_R(
suffix_decoder, handle_r, &suffix_cache_r);
labcomm2014_decoder_sample_ref_register(
decoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_decoder_sample_ref_register(
prefix_decoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_decoder_sample_ref_register(
suffix_decoder, labcomm2014_signature_generated_encoding_R);
while (labcomm2014_decoder_decode_one(decoder) > 0) ;
fprintf(stderr, "cache_r:\n");
fprintf(stderr, "%p %p %p %p\n",
cache_r.a[0],
cache_r.a[1],
cache_r.a[2],
cache_r.a[3]);
fprintf(stderr, "prefix_cache_r:\n");
fprintf(stderr, "%p %p %p %p\n",
prefix_cache_r.a[0],
prefix_cache_r.a[1],
prefix_cache_r.a[2],
prefix_cache_r.a[3]);
fprintf(stderr, "suffix_cache_r:\n");
fprintf(stderr, "%p %p %p %p\n",
suffix_cache_r.a[0],
suffix_cache_r.a[1],
suffix_cache_r.a[2],
suffix_cache_r.a[3]);
assert(cache_r.a[0] ==
labcomm2014_decoder_get_sample_ref(
decoder, labcomm2014_signature_generated_encoding_R));
assert(cache_r.a[1] ==
labcomm2014_decoder_get_sample_ref(
prefix_decoder, labcomm2014_signature_generated_encoding_R));
assert(cache_r.a[2] ==
labcomm2014_decoder_get_sample_ref(
suffix_decoder, labcomm2014_signature_generated_encoding_R));
assert(cache_r.a[3] ==
labcomm2014_decoder_get_sample_ref(
decoder, labcomm2014_signature_generated_encoding_R));
assert(prefix_cache_r.a[0] ==
labcomm2014_decoder_get_sample_ref(
decoder, labcomm2014_signature_generated_encoding_R));
assert(prefix_cache_r.a[1] ==
labcomm2014_decoder_get_sample_ref(
prefix_decoder, labcomm2014_signature_generated_encoding_R));
assert(prefix_cache_r.a[2] ==
labcomm2014_decoder_get_sample_ref(
suffix_decoder, labcomm2014_signature_generated_encoding_R));
assert(prefix_cache_r.a[3] ==
labcomm2014_decoder_get_sample_ref(
prefix_decoder, labcomm2014_signature_generated_encoding_R));
assert(suffix_cache_r.a[0] ==
labcomm2014_decoder_get_sample_ref(
decoder, labcomm2014_signature_generated_encoding_R));
assert(suffix_cache_r.a[1] ==
labcomm2014_decoder_get_sample_ref(
prefix_decoder, labcomm2014_signature_generated_encoding_R));
assert(suffix_cache_r.a[2] ==
labcomm2014_decoder_get_sample_ref(
suffix_decoder, labcomm2014_signature_generated_encoding_R));
assert(suffix_cache_r.a[3] ==
labcomm2014_decoder_get_sample_ref(
suffix_decoder, labcomm2014_signature_generated_encoding_R));
labcomm2014_decoder_free(suffix_decoder);
labcomm2014_decoder_free(prefix_decoder);
labcomm2014_decoder_free(decoder);
labcomm2014_renaming_registry_free(registry);
close(fd);
unlink(DATA_FILE);
}
/*
test_labcomm2014_renaming.c -- LabComm tests of renaming
Copyright 2015 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/>.
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "labcomm2014_private.h"
#include "labcomm2014_default_error_handler.h"
#include "labcomm2014_default_memory.h"
#include "labcomm2014_default_scheduler.h"
#include "labcomm2014_renaming.h"
#include "labcomm2014_renaming_encoder.h"
#include "test/gen/generated_encoding.h"
#define IOCTL_WRITER_ASSERT_BYTES 4096
#define IOCTL_WRITER_RESET 4097
#define EXPECT(...) \
{ \
int expected[] = __VA_ARGS__; \
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_ASSERT_BYTES, \
__LINE__, \
sizeof(expected)/sizeof(expected[0]), \
expected); \
}
#define VARIABLE(i) -(i + 1)
#define IS_VARIABLE(i) (i < 0)
static unsigned char buffer[128];
struct labcomm2014_writer *writer;
static int seen_variable[1024];
static int buf_writer_alloc(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context)
{
writer = w; /* Hack */
w->data_size = sizeof(buffer);
w->count = w->data_size;
w->data = buffer;
w->pos = 0;
return 0;
}
static int buf_writer_free(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context)
{
return 0;
}
static int buf_writer_start(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context,
int index,
const struct labcomm2014_signature *signature,
void *value)
{
return 0;
}
static int buf_writer_end(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context)
{
return 0;
}
static int buf_writer_flush(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context)
{
fprintf(stderr, "Should not come here %s:%d\n", __FILE__, __LINE__);
exit(1);
return 0;
}
static int buf_writer_ioctl(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context,
int signature_index, const struct labcomm2014_signature *signature,
uint32_t action, va_list arg)
{
int result = -ENOTSUP;
switch (action) {
case IOCTL_WRITER_ASSERT_BYTES: {
int line = va_arg(arg, int);
int count = va_arg(arg, int);
int *expected = va_arg(arg, int *);
int i, mismatch;
mismatch = 0;
if (w->pos != count) {
fprintf(stderr, "Invalid length detected %d != %d (%s:%d)\n",
w->pos, count, __FILE__, line);
mismatch = 1;
}
for (i = 0 ; i < count ; i++) {
if (IS_VARIABLE(expected[i])) {
if (seen_variable[VARIABLE(expected[i])] == -1) {
seen_variable[VARIABLE(expected[i])] = buffer[i];
}
if (seen_variable[VARIABLE(expected[i])] != buffer[i]) {
fprintf(stderr, "Unexpected variable v%d (%02x: != %02x)\n",
VARIABLE(expected[i]),
seen_variable[VARIABLE(expected[i])], buffer[i]);
mismatch = 1;
}
} else if (expected[i] != buffer[i]) {
mismatch = 1;
}
}
if (mismatch) {
fprintf(stderr, "Encoder mismatch (%s:%d)\n",
__FILE__, line);
for (i = 0 ; i < w->pos ; i++) {
if (32 <= w->data[i] && w->data[i] < 127) {
printf("%2c ", w->data[i]);
} else {
printf("%2.2x ", w->data[i]);
}
}
printf("\n");
for (i = 0 ; i < w->pos ; i++) {
printf("%2.2x ", w->data[i]);
}
printf("\n");
for (i = 0 ; i < count ; i++) {
if (expected[i] < 0) {
printf("v%d ", VARIABLE(expected[i]));
} else {
printf("%2.2x ", expected[i] );
}
}
printf("\n");
if (mismatch) { exit(1); }
}
result = 0;
} break;
case IOCTL_WRITER_RESET: {
w->pos = 0;
result = 0;
}
}
return result;
}
const struct labcomm2014_writer_action writer_action = {
.alloc = buf_writer_alloc,
.free = buf_writer_free,
.start = buf_writer_start,
.end = buf_writer_end,
.flush = buf_writer_flush,
.ioctl = buf_writer_ioctl
};
static struct labcomm2014_writer_action_context action_context = {
.next = NULL,
.action = &writer_action,
.context = NULL
};
static struct labcomm2014_writer buffer_writer = {
.action_context = &action_context,
.data = buffer,
.data_size = sizeof(buffer),
.count = sizeof(buffer),
.pos = 0,
.error = 0,
};
void dump_encoder(struct labcomm2014_encoder *encoder)
{
int i;
for (i = 0 ; i < writer->pos ; i++) {
printf("%2.2x ", writer->data[i]);
}
printf("\n");
}
static int do_test(int argc, char *argv[])
{
struct labcomm2014_renaming_registry *registry;
struct labcomm2014_encoder *encoder, *prefix, *suffix;
int i;
for (i = 0 ; i < sizeof(seen_variable)/sizeof(seen_variable[0]) ; i++) {
seen_variable[i] = -1;
}
registry = labcomm2014_renaming_registry_new(
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
encoder = labcomm2014_encoder_new(
&buffer_writer,
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
prefix = labcomm2014_renaming_encoder_new(encoder,
registry,
labcomm2014_renaming_prefix,
"p.");
suffix = labcomm2014_renaming_encoder_new(prefix,
registry,
labcomm2014_renaming_suffix,
".s");
EXPECT({ 0x01, 0x0c, 0x0b,
'L', 'a', 'b', 'C', 'o', 'm', 'm', '2','0', '1', '4' });
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_register_generated_encoding_V(encoder);
labcomm2014_encoder_register_generated_encoding_V(encoder);
EXPECT({ 0x02, 0x08, VARIABLE(0), 0x01, 0x00, 0x01, 'V', 0x02, 0x11, 0x00,
0x04, 0x0a, VARIABLE(1), 0x01, 0x00, 0x03, 'v', '_', 't', 0x02, 0x11, 0x00,
0x04, 0x07, VARIABLE(2), 0x01, 0x00, 0x01, 'V', 0x01, VARIABLE(1),
0x05, 0x02, VARIABLE(0), VARIABLE(2) });
labcomm2014_encoder_ioctl(prefix, IOCTL_WRITER_RESET);
labcomm2014_encoder_register_generated_encoding_V(prefix);
labcomm2014_encoder_register_generated_encoding_V(prefix);
EXPECT({ 0x02, 0x0a, VARIABLE(3), 0x01, 0x00, 0x03, 'p', '.', 'V', 0x02, 0x11, 0x00,
0x04, 0x09, VARIABLE(4), 0x01, 0x00, 0x03, 'p', '.', 'V', 0x01, VARIABLE(2),
0x05, 0x02, VARIABLE(3), VARIABLE(4) });
labcomm2014_encoder_ioctl(suffix, IOCTL_WRITER_RESET);
labcomm2014_encoder_register_generated_encoding_V(suffix);
labcomm2014_encoder_register_generated_encoding_V(suffix);
// XXX HERE BE DRAGONS! What does "BOGUS" man in the below renaming test?
// previous version (from merge conflict): VARIABLE(6) vs VARIABLE(2)
// <<<<<<< ours
// EXPECT({ 0x02, 0x0c, VARIABLE(5), 0x01, 0x00, 0x05, 'p', '.', 'V', '.', 's', 0x02, 0x11, 0x00,
// 0x04, 0x09, VARIABLE(6), 0x01, 0x00, 0x03, 'V', '.', 's', 0x01, VARIABLE(2),
// 0x04, 0x0b, VARIABLE(7), 0x01, 0x00, 0x05, 'p', '.', 'V', '.', 's', 0x01, VARIABLE(6),
// =======
EXPECT({ 0x02, 0x0c, VARIABLE(5), 0x01, 0x00, 0x05, 'p', '.', 'V', '.', 's', 0x02, 0x11, 0x00,
0x04, 0x09, VARIABLE(6), 0x01, 0x00, 0x03, 'V', '.', 's', 0x01, VARIABLE(2), /* BOGUS */
0x04, 0x0b, VARIABLE(7), 0x01, 0x00, 0x05, 'p', '.', 'V', '.', 's', 0x01, VARIABLE(2),
0x05, 0x02, VARIABLE(5), VARIABLE(7) });
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_sample_ref_register(encoder,
labcomm2014_signature_generated_encoding_V);
labcomm2014_encoder_sample_ref_register(encoder,
labcomm2014_signature_generated_encoding_V);
EXPECT({0x03, 0x08, VARIABLE(8), 0x01, 0x00, 0x01, 'V', 0x02, 0x11, 0x00});
labcomm2014_encoder_ioctl(prefix, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_sample_ref_register(prefix,
labcomm2014_signature_generated_encoding_V);
labcomm2014_encoder_sample_ref_register(prefix,
labcomm2014_signature_generated_encoding_V);
EXPECT({0x03, 0x0a, VARIABLE(9), 0x01, 0x00, 0x03, 'p', '.', 'V', 0x02, 0x11, 0x00});
labcomm2014_encoder_ioctl(suffix, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_sample_ref_register(suffix,
labcomm2014_signature_generated_encoding_V);
labcomm2014_encoder_sample_ref_register(suffix,
labcomm2014_signature_generated_encoding_V);
EXPECT({0x03, 0x0c, VARIABLE(10), 0x01, 0x00, 0x05, 'p', '.', 'V', '.', 's', 0x02, 0x11, 0x00});
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
labcomm2014_encode_generated_encoding_V(encoder);
EXPECT({VARIABLE(0), 0x00 });
labcomm2014_encoder_ioctl(prefix, IOCTL_WRITER_RESET);
labcomm2014_encode_generated_encoding_V(prefix);
EXPECT({VARIABLE(3), 0x00 });
labcomm2014_encoder_ioctl(suffix, IOCTL_WRITER_RESET);
labcomm2014_encode_generated_encoding_V(suffix);
EXPECT({VARIABLE(5), 0x00 });
labcomm2014_encoder_free(suffix);
labcomm2014_encoder_free(prefix);
labcomm2014_encoder_free(encoder);
labcomm2014_renaming_registry_free(registry);
return 0;
}
int main(int argc, char *argv[])
{
return do_test(argc, argv);
}
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <err.h>
#include "labcomm2014.h"
#include "labcomm2014_private.h"
#include "labcomm2014_default_error_handler.h"
#include "labcomm2014_default_memory.h"
#include "labcomm2014_default_scheduler.h"
#include "labcomm2014_renaming.h"
#include "labcomm2014_renaming_private.h"
#include "test/gen/generated_encoding.h"
static int do_test(int argc, char **argv)
{
struct labcomm2014_renaming_registry *registry;
struct labcomm2014_renaming_rename *r1, *r2, *r3, *r4;
registry = labcomm2014_renaming_registry_new(
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
r1 = labcomm2014_renaming_rename_new(
registry,
labcomm2014_signature_generated_encoding_R,
labcomm2014_renaming_prefix, "p:");
r2 = labcomm2014_renaming_rename_new(
registry,
labcomm2014_renaming_rename_signature(r1),
labcomm2014_renaming_suffix, ":s");
r3 = labcomm2014_renaming_rename_new(
registry,
labcomm2014_signature_generated_encoding_R,
labcomm2014_renaming_suffix, ":s");
r4 = labcomm2014_renaming_rename_new(
registry,
labcomm2014_renaming_rename_signature(r3),
labcomm2014_renaming_prefix, "p:");
assert(r2 == r4);
labcomm2014_renaming_rename_free(registry, r1);
labcomm2014_renaming_rename_free(registry, r2);
labcomm2014_renaming_rename_free(registry, r3);
labcomm2014_renaming_rename_free(registry, r4);
labcomm2014_renaming_registry_free(registry);
return 0;
}
int main(int argc, char **argv)
{
return do_test(argc, argv);
}
sample int test_var[_,2,_];
#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;
}
CC = gcc
CFLAGS = -g
VERSIONS=2014
liblabcomm.a : labcomm.o labcomm_fd_reader_writer.o labcomm_udp_reader_writer.o labcomm_thr_reader_writer.o ethaddr.o ThrottleDrv/ethernet_drv.o ThrottleDrv/throttle_drv.o
# ar -r liblabcomm.a labcomm.o labcomm_fd_reader_writer.o ethaddr.o
ar -r liblabcomm.a $^
all:
labcomm.o : labcomm.c labcomm.h labcomm_private.h
%:
for v in $(VERSIONS) ; do $(MAKE) -C $${v} $@ || exit 1 ; done
labcomm_fd_reader_writer.o : labcomm_fd_reader_writer.c labcomm_fd_reader_writer.h labcomm.h labcomm_private.h
ethaddr.o: ethaddr.c
.PHONY: clean
clean:
for v in $(VERSIONS) ; do $(MAKE) -C $${v} $@ || exit 1 ; done
%o: %c %h
.PHONY: distclean
distclean:
for v in $(VERSIONS) ; do $(MAKE) -C $${v} $@ || exit 1 ; done
rm -f *.o *.so *.so.1 *.a
clean:
rm *.o
distclean: clean
rm liblabcomm.a
......@@ -2,6 +2,6 @@ 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 sockets.
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
#include <errno.h>
#include <unistd.h>
#include "labcomm.h"
#define BUFFER_SIZE 2048
int labcomm_fd_reader(
labcomm_reader_t *r,
labcomm_reader_action_t action)
{
int result = -EINVAL;
int *fd = r->context;
switch (action) {
case labcomm_reader_alloc: {
r->data = malloc(BUFFER_SIZE);
if (r->data) {
r->data_size = BUFFER_SIZE;
result = r->data_size;
} else {
r->data_size = 0;
result = -ENOMEM;
}
r->count = 0;
r->pos = 0;
} break;
case labcomm_reader_start:
case labcomm_reader_continue: {
if (r->pos < r->count) {
result = r->count - r->pos;
} else {
int err;
r->pos = 0;
err = read(*fd, r->data, r->data_size);
if (err <= 0) {
r->count = 0;
result = -1;
} else {
r->count = err;
result = r->count - r->pos;
}
}
} break;
case labcomm_reader_end: {
result = 0;
} break;
case labcomm_reader_free: {
free(r->data);
r->data = 0;
r->data_size = 0;
r->count = 0;
r->pos = 0;
result = 0;
} break;
}
return result;
}
int labcomm_fd_writer(
labcomm_writer_t *w,
labcomm_writer_action_t action)
{
int result = 0;
int *fd = w->context;
switch (action) {
case labcomm_writer_alloc: {
w->data = malloc(BUFFER_SIZE);
if (! w->data) {
result = -ENOMEM;
w->data_size = 0;
w->count = 0;
w->pos = 0;
} else {
w->data_size = BUFFER_SIZE;
w->count = BUFFER_SIZE;
w->pos = 0;
}
} 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_start: {
w->pos = 0;
} break;
case labcomm_writer_continue: {
result = write(*fd, w->data, w->pos);
w->pos = 0;
} break;
case labcomm_writer_end: {
result = write(*fd, w->data, w->pos);
w->pos = 0;
} break;
case labcomm_writer_available: {
result = w->count - w->pos;
} break;
}
return result;
}
#include "labcomm.h"
extern int labcomm_fd_reader(
labcomm_reader_t *r,
labcomm_reader_action_t action);
extern int labcomm_fd_writer(
labcomm_writer_t *r,
labcomm_writer_action_t action);