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
  • anders.blomdell
  • compiler-refactoring
  • labcomm2006
  • labcomm2013
  • labcomm2014
  • master
  • pragma
  • python_sig_hash
  • typedefs
  • typeref
  • v2006.0
  • v2013.0
  • v2014.0
  • v2014.1
  • v2014.2
  • v2014.3
  • v2014.4
  • v2014.5
  • v2014.6
  • v2015.0
20 results

Target

Select target project
  • anders_blomdell/labcomm
  • klaren/labcomm
  • tommyo/labcomm
  • erikj/labcomm
  • sven/labcomm
5 results
Select Git revision
  • anders.blomdell
  • compiler-refactoring
  • labcomm2006
  • labcomm2013
  • master
  • pragma
  • typedefs
  • typeref
  • v2006.0
  • v2013.0
  • v2014.0
  • v2014.1
  • v2014.2
  • v2014.3
  • v2014.4
15 results
Show changes
Showing
with 939 additions and 449 deletions
......@@ -6,6 +6,7 @@ import java.io.IOException;
import se.lth.control.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.TypeDef;
import se.lth.control.labcomm2014.TypeDefParser;
import se.lth.control.labcomm2014.SigTypeDef;
//import se.lth.control.labcomm2014.TypeBinding;
public class Decoder
......@@ -65,10 +66,14 @@ public class Decoder
// System.out.println("Got TypeBinding: "+d.getSampleIndex()+" --> "+d.getTypeIndex()+"");
// }
public void onTypeDef(TypeDefParser.ParsedTypeDef d) {
public void onTypeDef(SigTypeDef d) {
System.out.println("ontype_def: ");
if(d != null) {
System.out.print((d.isSampleDef()?"sample ":"typedef ")+d);
System.out.println(" "+d.getName()+";");
} else {
System.out.println(" null???");
}
//for(byte b: d.getSignature()) {
// System.out.print(Integer.toHexString(b)+" ");
//}
......
......@@ -4,6 +4,7 @@ LCLJAR=${LCDIR}/lib/java/labcomm2014.jar# the LabComm library
EXECUTABLES=example_encoder example_decoder \
Encoder.class Decoder.class TDDecoder.class \
TestDataType.class \
ExampleEncoder.exe ExampleDecoder.exe
include ${LCDIR}/lib/c/os_compat.mk
......@@ -51,6 +52,7 @@ build :
javac -cp ${LCDIR}/lib/java/labcomm2014.jar:. ${GENDIR}/*.java Encoder.java Decoder.java
javac -cp ${LCDIR}/lib/java/labcomm2014.jar:${LCCJAR}:${GENDIR}:. TDDecoder.java
javac -cp ${LCDIR}/lib/java/labcomm2014.jar:${LCCJAR}:${GENDIR}:. TestDataType.java
${CC} ${CFLAGS} ${LDFLAGS} -Wall -Werror -Wno-unused-function \
-I. -I${LCDIR}/lib/c/2014 -L${LCDIR}/lib/c \
......@@ -76,23 +78,29 @@ run:
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Decoder encoded_data_j
@echo "************ running C decoder: *****************"
@LD_LIBRARY_PATH=${LCDIR}/lib/c/ ./example_decoder encoded_data_j
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_decoder encoded_data_j
@echo "************ running python decoder (from wiki_example):"
@PYTHONPATH=${LCDIR}/lib/python ../wiki_example/example_decoder.py encoded_data_j LabComm2014
@echo "************ running python decoder (with handlers):"
@PYTHONPATH=${LCDIR}/lib/python:gen:. ./example_handlers.py encoded_data_j LabComm2014
@echo "************ running C encoder: *****************"
@LD_LIBRARY_PATH=${LCDIR}/lib/c/ ./example_encoder encoded_data_c
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_encoder encoded_data_c
@echo "************ running Java decoder: *****************"
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Decoder encoded_data_c
@echo "************ running C decoder: *****************"
@LD_LIBRARY_PATH=${LCDIR}/lib/c/ ./example_decoder encoded_data_c
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_decoder encoded_data_c
@echo "************ running python decoder (from wiki_example):"
@PYTHONPATH=${LCDIR}/lib/python ../wiki_example/example_decoder.py encoded_data_c LabComm2014
@echo "************ running python decoder (with handlers):"
@PYTHONPATH=${LCDIR}/lib/python:gen:. ./example_handlers.py encoded_data_j LabComm2014
@echo "************ running python encoder: *****************"
@PYTHONPATH=${LCDIR}/lib/python:${GENDIR} ./example_encoder.py encoded_data_p LabComm2014
......@@ -100,11 +108,15 @@ run:
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Decoder encoded_data_p
@echo "************ running C decoder: *****************"
@LD_LIBRARY_PATH=${LCDIR}/lib/c/ ./example_decoder encoded_data_p
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_decoder encoded_data_p
@echo "************ running python decoder (from wiki_example):"
PYTHONPATH=${LCDIR}/lib/python ../wiki_example/example_decoder.py encoded_data_p LabComm2014
@echo "************ running python decoder (with handlers):"
@PYTHONPATH=${LCDIR}/lib/python:gen:. ./example_handlers.py encoded_data_j LabComm2014
runwcs: build ExampleEncoder.exe ExampleDecoder.exe
@echo
@echo "********************************************"
......@@ -118,22 +130,22 @@ runwcs: build ExampleEncoder.exe ExampleDecoder.exe
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Decoder encoded_data_j
@echo "************ running C decoder: *****************"
@LD_LIBRARY_PATH=${LCDIR}/lib/c/ ./example_decoder encoded_data_j
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_decoder encoded_data_j
@echo "************ running python decoder (from wiki_example):"
@PYTHONPATH=${LCDIR}/lib/python ../wiki_example/example_decoder.py encoded_data_j LabComm2014
@echo "************ running C# decoder: *****************"
@LD_LIBRARY_PATH=${LCDIR}/lib/c/ mono ./ExampleDecoder.exe encoded_data_j
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ mono ./ExampleDecoder.exe encoded_data_j
@echo "************ running C encoder: *****************"
@LD_LIBRARY_PATH=${LCDIR}/lib/c/ ./example_encoder encoded_data_c
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_encoder encoded_data_c
@echo "************ running Java decoder: *****************"
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Decoder encoded_data_c
@echo "************ running C decoder: *****************"
@LD_LIBRARY_PATH=${LCDIR}/lib/c/ ./example_decoder encoded_data_c
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_decoder encoded_data_c
@echo "************ running python decoder (from wiki_example):"
@PYTHONPATH=${LCDIR}/lib/python ../wiki_example/example_decoder.py encoded_data_c LabComm2014
......@@ -148,14 +160,14 @@ runwcs: build ExampleEncoder.exe ExampleDecoder.exe
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Decoder encoded_data_p
@echo "************ running C decoder: *****************"
@LD_LIBRARY_PATH=${LCDIR}/lib/c/ ./example_decoder encoded_data_p
@echo "************ running python decoder (from wiki_example):"
PYTHONPATH=${LCDIR}/lib/python ../wiki_example/example_decoder.py encoded_data_p LabComm2014
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_decoder encoded_data_p
@echo "************ running C# decoder: *****************"
@mono ./ExampleDecoder.exe encoded_data_p
@echo "************ running python decoder (from wiki_example):"
PYTHONPATH=${LCDIR}/lib/python ../wiki_example/example_decoder.py encoded_data_p LabComm2014
@echo "************ running C# encoder: *****************"
@mono ./ExampleEncoder.exe encoded_data_cs
......@@ -183,6 +195,15 @@ runjastadd: cleanbuild
@echo "************ running Java TypeDefdecoder: *****************"
java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${LCCJAR}:${GENDIR} TDDecoder encoded_data_j
rundatatype: cleanbuild
@echo
@echo "********************************************"
@echo "*************** running datatype example ***"
@echo "********************************************"
@echo
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} TestDataType
clean:
rm -rf ${GENDIR}
......
......@@ -6,10 +6,13 @@ import java.io.IOException;
import se.lth.control.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.TypeDef;
import se.lth.control.labcomm2014.TypeDefParser;
import se.lth.control.labcomm2014.SigTypeDef;
import se.lth.control.labcomm2014.ParsedSampleDef;
import se.lth.control.labcomm2014.ASTbuilder;
//import se.lth.control.labcomm2014.TypeBinding;
import se.lth.control.labcomm2014.DataType;
import se.lth.control.labcomm2014.compiler.Program;
import se.lth.control.labcomm2014.compiler.Specification;
import java.io.FileOutputStream;
import java.io.PrintStream;
......@@ -75,18 +78,29 @@ public class TDDecoder
// System.out.println("Got TypeBinding: "+d.getSampleIndex()+" --> "+d.getTypeIndex()+"");
// }
public void onTypeDef(TypeDefParser.ParsedTypeDef d) {
if(d.isSampleDef()){
public void onTypeDef(SigTypeDef d) {
if(d != null && d.isSampleDef()){
System.out.println("onTypeDef (sample): ");
//------------
try {
System.out.println("==================== DataType ======");
DataType.printDataType(System.out, d);
System.out.println();
System.out.println("==================== end ======");
} catch (IOException ioe) {
ioe.printStackTrace();
}
ASTbuilder v = new ASTbuilder();
Program p = v.makeProgram((TypeDefParser.ParsedSampleDef) d);
Specification p = v.makeSpecification((ParsedSampleDef) d);
try {
//FileOutputStream f = new FileOutputStream("/tmp/foopp"+d.getName()+".txt");
//PrintStream out = new PrintStream(f);
FileOutputStream f = new FileOutputStream("/tmp/foopp"+d.getName()+".txt");
PrintStream out = new PrintStream(f);
p.pp(System.out);
//p.C_genC(System.out, new Vector(), "lcname", "prefix", 2014);
//p.J_gen(out, "testpackage", 2014);
//out.close();
p.J_gen(out, "testpackage", 2014);
out.close();
} catch (Throwable e) {
System.err.println("Exception: " + e);
e.printStackTrace();
......
import java.io.PrintStream;
import se.lth.control.labcomm2014.*;
/**
* Test data type tree
*/
public class TestDataType
{
private PrintStream out;
public TestDataType(PrintStream out)
throws Exception
{
this.out = out;
}
public void doTest() throws java.io.IOException {
twoLines x = new twoLines();
DataType.printDataType(out, x.getDispatcher());
twoStructsAndInt y = new twoStructsAndInt();
DataType.printDataType(out, y.getDispatcher());
theFirstInt z = new theFirstInt();
DataType.printDataType(out, z.getDispatcher());
doavoid a = new doavoid();
DataType.printDataType(out, a.getDispatcher());
}
public static void main(String[] arg) throws Exception {
TestDataType example = new TestDataType(System.out);
example.doTest();
}
}
......@@ -13,7 +13,7 @@ static void handle_test_doavoid(test_doavoid *v,void *context) {
}
static void handle_test_intAndRef(test_intAndRef *v,void *context) {
printf("Got intAndRef. (%d : %s) \n", v->x, v->reference->name);
printf("Got intAndRef. (%d : %p) \n", v->x, v->reference);
}
static void handle_test_twoInts(test_twoInts *v,void *context) {
......
......@@ -8,31 +8,31 @@ if __name__ == '__main__':
version = sys.argv[2] if len(sys.argv) == 3 else "LabComm2014"
encoder = labcomm2014.Encoder(labcomm2014.StreamWriter(open(sys.argv[1], 'w')), version)
encoder.add_decl(test.twoLines.signature)
tl = dict(
l1=dict(
start=dict(
x=dict(val=1),
y=dict(val=11)
),
end=dict(
x=dict(val=2),
y=dict(val=22)
)
),
l2=dict(
start=dict(
x=dict(val=3),
y=dict(val=33)
),
end=dict(
x=dict(val=4),
y=dict(val=44)
)
),
f=dict(
a=10,
b=20,
c=False
)
)
tl = {
(('','l1'),):{
(('','start'),):{
(('','x'),):{(('','val'),):1},
(('','y'),):{(('','val'),):11}
},
(('','end'),):{
(('','x'),):{(('','val'),):2},
(('','y'),):{(('','val'),):22}
}
},
(('','l2'),):{
(('','start'),):{
(('','x'),):{(('','val'),):3},
(('','y'),):{(('','val'),):33}
},
(('','end'),):{
(('','x'),):{(('','val'),):4},
(('','y'),):{(('','val'),):44}
}
},
(('','f'),):{
(('','a'),):10,
(('','b'),):20,
(('','c'),):False
}
}
encoder.encode(tl, test.twoLines.signature)
#!/usr/bin/python
import labcomm2014
import sys,traceback
import test
def handle_twoInts(val):
print "got twoInts: %s" % val
def handle_twoLines(val):
print "got twoLines: %s" % val
if __name__ == "__main__":
version = sys.argv[2] if len(sys.argv) == 3 else "LabComm2014"
d = labcomm2014.Decoder(labcomm2014.StreamReader(open(sys.argv[1])), version)
d.register_handler(test.twoInts.signature, handle_twoInts)
d.register_handler(test.twoLines.signature, handle_twoLines)
while True:
try:
d.runOne()
except EOFError:
print "got EOF"
break
except Exception, e:
print "got Exception"
print e
traceback.print_exc()
break
typedef struct {
int val;
(foo:bar) int val;
} coord;
typedef int anInt;
typedef void avoid;
sample avoid doavoid;
sample (function:"a trigger")(foo:bar) avoid doavoid;
sample struct {
int x;
sample (a:b) "A struct: an int and a ref." struct {
(e:f)(c:d) int x;
sample reference;
} intAndRef;
......@@ -29,9 +29,9 @@ typedef struct {
} foo;
sample struct {
line l1;
line l2;
foo f;
(name:l1)line l1;
(name:l2)line l2;
(b:"kalle anka")(c:hejdu)(a:"kalle anka")foo f;
} twoLines;
sample struct {
......@@ -41,3 +41,19 @@ sample struct {
sample anInt theFirstInt;
sample anInt theSecondInt;
sample struct {
struct {
int x;
int y;
} s1;
struct {
int a;
int b;
} s2;
int i;
double double_array[2,3,_][3][_];
} twoStructsAndInt;
......@@ -22,7 +22,11 @@ OBJS=labcomm$(VERSION).o \
labcomm$(VERSION)_fd_reader.o \
labcomm$(VERSION)_type_signature.o \
labcomm$(VERSION)_fd_writer.o \
labcomm$(VERSION)_pthread_scheduler.o
labcomm$(VERSION)_pthread_scheduler.o \
labcomm$(VERSION)_renaming.o \
labcomm$(VERSION)_renaming_registry.o \
labcomm$(VERSION)_renaming_encoder.o \
labcomm$(VERSION)_renaming_decoder.o
# Enable experimental objects by `make LABCOMM_EXPERIMENTAL=true`
ifeq ($(LABCOMM_EXPERIMENTAL),true)
......@@ -35,7 +39,7 @@ endif
# Enable experimental objects by `make LABCOMM_SIG_PARSER=true`
ifeq ($(LABCOMM_SIG_PARSER),true)
OBJS += experimental/labcomm_sig_parser.o
OBJS += experimental/labcomm2014_sig_parser.o
endif
LABCOMM_JAR=../../../compiler/labcomm$(LIBVERSION)_compiler.jar
......@@ -46,7 +50,10 @@ TESTS=test_labcomm_basic_type_encoding \
test_signature_numbers \
test_labcomm \
test_labcomm_pthread_scheduler \
test_labcomm_copy
test_labcomm_copy \
test_labcomm_renaming_registry \
test_labcomm_renaming_encoder \
test_labcomm_renaming_decoder
#FIXME: test_labcomm_errors
TEST_DIR=test
......@@ -88,9 +95,9 @@ distclean: clean
../liblabcomm$(LIBVERSION).a: $(OBJS)
ar -r $@ $^
# Enable sig parser objects by `make LABCOMM_SIG_PARSER=true`
# Enable sig parser objects by `make labcomm2014_sig_PARSER=true`
ifeq ($(LABCOMM_SIG_PARSER),true)
experimental/test_sig_parser : experimental/labcomm_sig_parser.o experimental/test_sig_parser.c
experimental/test_sig_parser : experimental/labcomm2014_sig_parser.o experimental/test_sig_parser.c
endif
# compilation rules
......@@ -103,7 +110,7 @@ endif
# rules invoked by 'test'
.PHONY: run-test-%
run-test-%: $(TEST_DIR)/gen/% | $(TEST_DIR)/gen
$<
$(VALGRIND) $<
.PRECIOUS: $(TEST_DIR)/gen/%
......@@ -169,3 +176,12 @@ $(TEST_DIR)/gen/test_labcomm_copy: \
$(TEST_DIR)/gen/generated_encoding.o \
$(TEST_DIR)/gen/test_sample.o \
$(TEST_DIR)/gen/more_types.o
$(TEST_DIR)/gen/test_labcomm_renaming_registry: \
$(TEST_DIR)/gen/generated_encoding.o
$(TEST_DIR)/gen/test_labcomm_renaming_encoder: \
$(TEST_DIR)/gen/generated_encoding.o
$(TEST_DIR)/gen/test_labcomm_renaming_decoder: \
$(TEST_DIR)/gen/generated_encoding.o
/* labcomm_sig_parser.c:
/* labcomm2014_sig_parser.c:
* an example parser for labcomm signatures, illustrating how to skip samples
* based on their signature. Intended as an embryo for introducing this
* functionality into the lib to allow a channel to survive types with no
......@@ -27,7 +27,7 @@
#include <stdio.h>
#include <string.h>
#include "labcomm_sig_parser.h"
#include "labcomm2014_sig_parser.h"
static void error(char *s) {
fprintf(stderr, "ERROR: %s", s);
......@@ -45,7 +45,7 @@ static int unpack32(unsigned char *c, int idx) {
return b0 | b1 | b2 | b3;
}
static inline int get32(labcomm_sig_parser_t *b) {
static inline int get32(labcomm2014_sig_parser_t *b) {
int res = unpack32(b->c, b->idx);
b->idx+=4;
return res;
......@@ -61,7 +61,7 @@ static int unpack_varint(unsigned char *buf,
{
int res = 0;
unsigned int i=0;
unsigned char cont = TRUE;
unsigned char cont = LABCOMM2014_TRUE;
do {
unsigned char c = buf[idx+i];
......@@ -75,7 +75,7 @@ static int unpack_varint(unsigned char *buf,
return res;
}
void dumpValStack(labcomm_sig_parser_t *b) {
void dumpValStack(labcomm2014_sig_parser_t *b) {
printf("=== value stack: ");
#ifdef DEBUG_STACK_VERBOSE
int i;
......@@ -85,7 +85,7 @@ void dumpValStack(labcomm_sig_parser_t *b) {
#endif
printf(" top==%d\n", b->val_top);
}
void dumpPtrStack(labcomm_sig_parser_t *b) {
void dumpPtrStack(labcomm2014_sig_parser_t *b) {
printf("=== pointer stack: ");
#ifdef DEBUG_STACK_VERBOSE
int i;
......@@ -96,7 +96,7 @@ void dumpPtrStack(labcomm_sig_parser_t *b) {
printf(" top==%d\n", b->ptr_top);
}
void push_val(labcomm_sig_parser_t *b, unsigned int e) {
void push_val(labcomm2014_sig_parser_t *b, unsigned int e) {
b->val_stack[b->val_top]=e;
b->val_top=b->val_top-1;
if(b->val_top<0) error("value stack overrun");
......@@ -104,7 +104,7 @@ void push_val(labcomm_sig_parser_t *b, unsigned int e) {
dumpValStack(b);
#endif
}
unsigned int pop_val(labcomm_sig_parser_t *b) {
unsigned int pop_val(labcomm2014_sig_parser_t *b) {
b->val_top=b->val_top+1;
if(b->val_top>b->stacksize) error("value stack underrun");
#if defined DEBUG && !defined QUIET_STACK
......@@ -112,7 +112,7 @@ unsigned int pop_val(labcomm_sig_parser_t *b) {
#endif
return b->val_stack[b->val_top];
}
void push_ptr(labcomm_sig_parser_t *b, void* e) {
void push_ptr(labcomm2014_sig_parser_t *b, void* e) {
b->ptr_stack[b->ptr_top]=e;
b->ptr_top=b->ptr_top-1;
if(b->ptr_top<0) error("pointer stack overrun");
......@@ -120,7 +120,7 @@ void push_ptr(labcomm_sig_parser_t *b, void* e) {
dumpPtrStack(b);
#endif
}
void* pop_ptr(labcomm_sig_parser_t *b) {
void* pop_ptr(labcomm2014_sig_parser_t *b) {
b->ptr_top=b->ptr_top+1;
if(b->ptr_top>b->stacksize) error("pointer stack underrun");
#ifdef DEBUG
......@@ -129,7 +129,7 @@ void* pop_ptr(labcomm_sig_parser_t *b) {
return b->ptr_stack[b->ptr_top];
}
int labcomm_sig_parser_init(labcomm_sig_parser_t *b, size_t buffer_size,
int labcomm2014_sig_parser_init(labcomm2014_sig_parser_t *b, size_t buffer_size,
size_t stacksize, size_t num_signatures,
size_t max_name_len, size_t max_sig_len)
{
......@@ -151,9 +151,9 @@ int labcomm_sig_parser_init(labcomm_sig_parser_t *b, size_t buffer_size,
b->max_sig_len = max_sig_len;
#ifdef STATIC_ALLOCATION
printf("warning: labcomm_sig_parser_t_init: size params ignored, using defaults from .h file \n");
printf("warning: labcomm2014_sig_parser_t_init: size params ignored, using defaults from .h file \n");
#else
b->sig_ts=calloc(num_signatures, sizeof(struct labcomm_signature));
b->sig_ts=calloc(num_signatures, sizeof(struct labcomm2014_signature));
b->signatures_length=calloc(num_signatures, sizeof(int));
b->signatures_name_length=calloc(num_signatures, sizeof(int));
b->signatures_name=calloc(num_signatures, sizeof(void *)); //HERE BE DRAGONS: add range checks
......@@ -167,51 +167,72 @@ int labcomm_sig_parser_init(labcomm_sig_parser_t *b, size_t buffer_size,
return b->c == NULL || b->val_stack == NULL || b->ptr_stack == NULL;
}
int labcomm_sig_parser_read_file(labcomm_sig_parser_t *b, FILE *f) {
/* free the objects allocated by labcomm_sig_parser_init(b)
* NB! does not free b itself */
void labcomm2014_sig_parser_free(labcomm2014_sig_parser_t *b)
{
int i;
free(b->c);
free(b->val_stack);
free(b->ptr_stack);
#ifndef STATIC_ALLOCATION
for(i = 0; i<b->max_signatures; i++) {
free(b->signatures[i]);
free(b->signatures_name[i]);
}
free(b->signatures);
free(b->signatures_name);
free(b->signatures_name_length);
free(b->signatures_length);
free(b->sig_ts);
#endif
}
int labcomm2014_sig_parser_read_file(labcomm2014_sig_parser_t *b, FILE *f) {
int s = fread(b->c, sizeof(char), b->capacity, f);
b->size = s;
b->idx=0;
return s;
}
int more(labcomm_sig_parser_t *b)
int more(labcomm2014_sig_parser_t *b)
{
return b->idx < b->size;
}
unsigned char get(labcomm_sig_parser_t *b) {
unsigned char get(labcomm2014_sig_parser_t *b) {
return b->c[b->idx++];
}
unsigned char peek(labcomm_sig_parser_t *b) {
unsigned char peek(labcomm2014_sig_parser_t *b) {
return b->c[b->idx];
}
void advance(labcomm_sig_parser_t *b) {
void advance(labcomm2014_sig_parser_t *b) {
b->idx++;
}
void advancen(labcomm_sig_parser_t *b, size_t n) {
void advancen(labcomm2014_sig_parser_t *b, size_t n) {
b->idx+=n;
}
int peek_varint(labcomm_sig_parser_t *b, size_t *size) {
int peek_varint(labcomm2014_sig_parser_t *b, size_t *size) {
return unpack_varint(b->c, b->idx, size);
}
int get_varint(labcomm_sig_parser_t *b) {
int get_varint(labcomm2014_sig_parser_t *b) {
size_t size;
int res = peek_varint(b, &size);
advancen(b, size);
return res;
}
int get_varint_size(labcomm_sig_parser_t *b, size_t *size) {
int get_varint_size(labcomm2014_sig_parser_t *b, size_t *size) {
unsigned int res = peek_varint(b, size);
advancen(b, *size);
return res;
}
void getStr(labcomm_sig_parser_t *b, unsigned char *dest, size_t size) {
void getStr(labcomm2014_sig_parser_t *b, unsigned char *dest, size_t size) {
int rem = b->size - b->idx;
if( size > rem )
size = rem;
......@@ -220,31 +241,31 @@ void getStr(labcomm_sig_parser_t *b, unsigned char *dest, size_t size) {
b->idx += size;
}
struct labcomm_signature *get_sig_t(labcomm_sig_parser_t *p, unsigned int uid)
struct labcomm2014_signature *get_sig_t(labcomm2014_sig_parser_t *p, unsigned int uid)
{
return &(p->sig_ts[uid-LABCOMM_USER]);
}
unsigned int get_signature_len(labcomm_sig_parser_t *p, unsigned int uid){
unsigned int get_signature_len(labcomm2014_sig_parser_t *p, unsigned int uid){
//return signatures_length[uid-LABCOMM_USER];
return p->sig_ts[uid-LABCOMM_USER].size;
}
unsigned char* get_signature(labcomm_sig_parser_t *p, unsigned int uid){
unsigned char* get_signature(labcomm2014_sig_parser_t *p, unsigned int uid){
//return signatures[uid-LABCOMM_USER];
return p->sig_ts[uid-LABCOMM_USER].signature;
}
//is this needed?
//unsigned int get_signature_name_len(labcomm_sig_parser_t *p, unsigned int uid){
//unsigned int get_signature_name_len(labcomm2014_sig_parser_t *p, unsigned int uid){
// return signatures_name_length[uid-LABCOMM_USER];
//}
char* get_signature_name(labcomm_sig_parser_t *p, unsigned int uid){
char* get_signature_name(labcomm2014_sig_parser_t *p, unsigned int uid){
//return signatures_name[uid-LABCOMM_USER];
return p->sig_ts[uid-LABCOMM_USER].name;
}
void dump_signature(labcomm_sig_parser_t *p, unsigned int uid){
void dump_signature(labcomm2014_sig_parser_t *p, unsigned int uid){
int i;
unsigned int len = get_signature_len(p, uid);
printf("signature for uid %x : %s (len=%d):\n", uid, get_signature_name(p, uid), len);
......@@ -256,11 +277,11 @@ void dump_signature(labcomm_sig_parser_t *p, unsigned int uid){
printf("\n");
}
static inline void labcomm_sig_parser_t_set_varsize(labcomm_sig_parser_t *b)
static inline void labcomm2014_sig_parser_t_set_varsize(labcomm2014_sig_parser_t *b)
{
b->current_decl_is_varsize = TRUE;
b->current_decl_is_varsize = LABCOMM2014_TRUE;
}
static size_t labcomm_sizeof_primitive(unsigned int type)
static size_t labcomm2014_sizeof_primitive(unsigned int type)
{
switch(type) {
case TYPE_BOOLEAN :
......@@ -276,34 +297,35 @@ static size_t labcomm_sizeof_primitive(unsigned int type)
case TYPE_DOUBLE :
return 8;
default:
printf("labcomm_sizeof_primitive(%x)\n", type);
error("labcomm_sizeof_primitive should only be called for primitive types");
printf("labcomm2014_sizeof_primitive(%x)\n", type);
error("labcomm2014_sizeof_primitive should only be called for primitive types");
return 0;
}
}
//these are inlined in do_accept_packet
//static int accept_type_decl(labcomm_sig_parser_t *d);
//static int accept_sample_decl(labcomm_sig_parser_t *d);
static int accept_user_id(labcomm_sig_parser_t *d);
static int accept_string(labcomm_sig_parser_t *d);
static int accept_type(labcomm_sig_parser_t *d);
static int accept_array_decl(labcomm_sig_parser_t *d);
//static int accept_type_decl(labcomm2014_sig_parser_t *d);
//static int accept_sample_decl(labcomm2014_sig_parser_t *d);
static int accept_user_id(labcomm2014_sig_parser_t *d);
static int accept_string(labcomm2014_sig_parser_t *d);
static int accept_type(labcomm2014_sig_parser_t *d);
static int accept_array_decl(labcomm2014_sig_parser_t *d);
static int accept_intentions(labcomm2014_sig_parser_t *d);
#if 0
// UNUSED declarations
static int accept_string_length(labcomm_sig_parser_t *d);
static int accept_char(labcomm_sig_parser_t *d);
static int accept_number_of_indices(labcomm_sig_parser_t *d);
static int accept_indices(labcomm_sig_parser_t *d);
static int accept_variable_index(labcomm_sig_parser_t *d);
static int accept_fixed_index(labcomm_sig_parser_t *d);
static int accept_number_of_fields(labcomm_sig_parser_t *d);
static int accept_string_length(labcomm2014_sig_parser_t *d);
static int accept_char(labcomm2014_sig_parser_t *d);
static int accept_number_of_indices(labcomm2014_sig_parser_t *d);
static int accept_indices(labcomm2014_sig_parser_t *d);
static int accept_variable_index(labcomm2014_sig_parser_t *d);
static int accept_fixed_index(labcomm2014_sig_parser_t *d);
static int accept_number_of_fields(labcomm2014_sig_parser_t *d);
#endif
static int accept_struct_decl(labcomm_sig_parser_t *d);
static int accept_field(labcomm_sig_parser_t *d);
static int accept_sample_data(labcomm_sig_parser_t *d);
static int accept_struct_decl(labcomm2014_sig_parser_t *d);
static int accept_field(labcomm2014_sig_parser_t *d);
static int accept_sample_data(labcomm2014_sig_parser_t *d);
static unsigned char labcomm_varint_sizeof(unsigned int i)
static unsigned char labcomm2014_varint_sizeof(unsigned int i)
{
if(i < 128) {
return 1;
......@@ -314,7 +336,7 @@ static unsigned char labcomm_varint_sizeof(unsigned int i)
return res;
}
}
int encoded_size_static(struct labcomm_signature *sig, void *unused)
int encoded_size_static(struct labcomm2014_signature *sig, void *unused)
{
#ifdef LABCOMM_EXPERIMENTAL_CACHED_ENCODED_SIZE
if(sig->cached_encoded_size == -1) {
......@@ -334,14 +356,14 @@ int encoded_size_static(struct labcomm_signature *sig, void *unused)
on the receiver side, is to skip unhandled samples.
*/
int encoded_size_parse_sig(struct labcomm_signature *sig, void *sample)
int encoded_size_parse_sig(struct labcomm2014_signature *sig, void *sample)
{
printf("Warning: encoded_size_parse_sig not implemented\n");
return -1;
}
static int accept_signature(labcomm_sig_parser_t *d,
labcomm_type type,
static int accept_signature(labcomm2014_sig_parser_t *d,
labcomm2014_type type,
unsigned int start,
unsigned int uid, char *name)
{
......@@ -360,19 +382,19 @@ static int accept_signature(labcomm_sig_parser_t *d,
if(type != PKG_SAMPLE_DECL) {
if(type == PKG_SAMPLE_REF) {
INFO_PRINTF("accept_signature: ignoring sample ref\n");
return TRUE;
return LABCOMM2014_TRUE;
} else if (type == PKG_TYPE_DECL) {
INFO_PRINTF("accept_signature: ignoring typedef\n");
return TRUE;
return LABCOMM2014_TRUE;
} else {
error("decl is neither sample, ref, or typedef???");
return FALSE;
return LABCOMM2014_FALSE;
}
}
unsigned int end = d->idx;
unsigned int len = end-start;
struct labcomm_signature *newsig = get_sig_t(d, uid);
struct labcomm2014_signature *newsig = get_sig_t(d, uid);
// newsig->type = type;
if(len <= d->max_sig_len) {
d->signatures_length[uid-LABCOMM_USER] = len;
......@@ -396,22 +418,22 @@ static int accept_signature(labcomm_sig_parser_t *d,
INFO_PRINTF(".... is variable size\n");
}
#endif
return TRUE;
return LABCOMM2014_TRUE;
}
static int accept_decl(labcomm_sig_parser_t *d, labcomm_type type)
static int accept_decl(labcomm2014_sig_parser_t *d, labcomm2014_type type)
{
if(accept_user_id(d)) {
unsigned int uid = pop_val(d);
unsigned int nstart = d->idx;
VERBOSE_PRINTF(", name = ");
accept_string(d);
VERBOSE_PRINTF(", intentions = ");
accept_intentions(d);
unsigned int start = d->idx;
unsigned int nstart = pop_val(d);
unsigned int nlen = pop_val(d);
#ifdef RETURN_STRINGS
char *str = (char *) pop_ptr(d);
free(str);
#endif
unsigned char lenlen = labcomm_varint_sizeof(nlen);
unsigned char lenlen = labcomm2014_varint_sizeof(nlen);
if(type != PKG_SAMPLE_DECL) {
// don't record typedefs and samplerefs (for now)
......@@ -428,16 +450,16 @@ static int accept_decl(labcomm_sig_parser_t *d, labcomm_type type)
return accept_signature(d, type, start, uid, name);
} else {
error("sig name longer than max length (this ought to be dynamic...");
return FALSE;
return LABCOMM2014_FALSE;
}
} else {
error("sample_decl with uid < LABCOMM_USER");
return FALSE;
return LABCOMM2014_FALSE;
}
}
// HERE BE DRAGONS! what does the return value mean?
int accept_packet(labcomm_sig_parser_t *d) {
int accept_packet(labcomm2014_sig_parser_t *d) {
size_t nbytes;
unsigned int type = peek_varint(d, &nbytes) ;
if(type == PKG_VERSION ) {
......@@ -451,19 +473,19 @@ int accept_packet(labcomm_sig_parser_t *d) {
free(str);
#endif
} else if (type == PKG_SAMPLE_DECL) {
d->current_decl_is_varsize = FALSE; // <-- a conveniance flag in labcomm_sig_parser_t
d->current_decl_is_varsize = LABCOMM2014_FALSE; // <-- a conveniance flag in labcomm2014_sig_parser_t
advancen(d, nbytes);
VERBOSE_PRINTF("sample_decl ");
get_varint(d); //ignore length field
accept_decl(d, type);
} else if (type == PKG_SAMPLE_REF) {
d->current_decl_is_varsize = FALSE; // <-- a conveniance flag in labcomm_sig_parser_t
d->current_decl_is_varsize = LABCOMM2014_FALSE; // <-- a conveniance flag in labcomm2014_sig_parser_t
advancen(d, nbytes);
VERBOSE_PRINTF("sample_ref ");
get_varint(d); //ignore length field
accept_decl(d, type);
}else if(type == PKG_TYPE_DECL ) {
d->current_decl_is_varsize = FALSE; // <-- a conveniance flag in labcomm_sig_parser_t
d->current_decl_is_varsize = LABCOMM2014_FALSE; // <-- a conveniance flag in labcomm2014_sig_parser_t
advancen(d, nbytes);//consume type field
VERBOSE_PRINTF("type_decl ");
get_varint(d); //ignore length field
......@@ -498,24 +520,76 @@ int accept_packet(labcomm_sig_parser_t *d) {
advancen(d, len);
#endif
}
return TRUE;
return LABCOMM2014_TRUE;
}
static int accept_user_id(labcomm_sig_parser_t *d){
static int accept_user_id(labcomm2014_sig_parser_t *d){
size_t nbytes;
int uid = peek_varint(d, &nbytes);
if(uid >= LABCOMM_USER) {
advancen(d, nbytes);
VERBOSE_PRINTF("uid = 0x%x ", uid);
push_val(d, uid);
return TRUE;
return LABCOMM2014_TRUE;
} else {
error("uid < LABCOMM_USER");
return FALSE;
return LABCOMM2014_FALSE;
}
}
static int accept_string(labcomm_sig_parser_t *d){
/** pushes (index in stream of name) and (length of name) */
static int accept_intentions(labcomm2014_sig_parser_t *d){
unsigned int num = get_varint(d);
int i;
int npos = 0;
int nlen = 0;
for(i=0; i<num; i++) {
int klen, vlen;
printf("( ");
#ifdef RETURN_STRINGS
char *key;
char *val;
#endif
accept_string(d);
klen = pop_val(d);
if(klen==0) {
npos = d->idx;
}
#ifdef RETURN_STRINGS
key = (char *) pop_ptr(d);
if(klen!=0) {
printf("%s] : ",key);
}
free(key);
#else
if(klen!=0) {
printf(": ");
} else {
printf("name: ");
}
#endif
accept_string(d);
printf(" ");
vlen = pop_val(d);
if(klen==0) {
nlen = vlen;
}
#ifdef RETURN_STRINGS
val = (char *) pop_ptr(d);
printf("%s %s",(klen?",val : ":""), val);
free(val);
#endif
printf(") ");
}
push_val(d, nlen);
push_val(d, npos);
return LABCOMM2014_TRUE;
}
static int accept_string(labcomm2014_sig_parser_t *d){
unsigned int len = get_varint(d);
unsigned char *str=malloc(len+1); // len is without terminating null
getStr(d, str, len);
......@@ -526,10 +600,10 @@ static int accept_string(labcomm_sig_parser_t *d){
free(str);
#endif
push_val(d, len);
return TRUE;
return LABCOMM2014_TRUE;
}
/* pushes size and type id */
static int accept_type(labcomm_sig_parser_t *d){
static int accept_type(labcomm2014_sig_parser_t *d){
size_t nbytes;
unsigned int type = peek_varint(d, &nbytes) ;
switch(type) {
......@@ -571,7 +645,7 @@ static int accept_type(labcomm_sig_parser_t *d){
case TYPE_STRING :
VERBOSE_PRINTF("string\n");
advancen(d, nbytes);
labcomm_sig_parser_t_set_varsize(d);
labcomm2014_sig_parser_t_set_varsize(d);
push_val(d, 0);
break;
case TYPE_SAMPLE_REF :
......@@ -595,14 +669,14 @@ static int accept_type(labcomm_sig_parser_t *d){
advancen(d, nbytes);
push_val(d, 0);
push_val(d, type);
return FALSE;
return LABCOMM2014_FALSE;
}
push_val(d, type);
return TRUE;
return LABCOMM2014_TRUE;
}
/* pushes size and element type */
static int accept_array_decl(labcomm_sig_parser_t *d){
static int accept_array_decl(labcomm2014_sig_parser_t *d){
size_t nbytes;
int tid = peek_varint(d, &nbytes) ;
if(tid == ARRAY_DECL) {
......@@ -617,7 +691,7 @@ static int accept_array_decl(labcomm_sig_parser_t *d){
if(idx == 0) {
numVar++;
VERBOSE_PRINTF("[_] ");
labcomm_sig_parser_t_set_varsize(d);
labcomm2014_sig_parser_t_set_varsize(d);
} else {
VERBOSE_PRINTF("[%d] ", idx);
size*=idx;
......@@ -640,17 +714,17 @@ static int accept_array_decl(labcomm_sig_parser_t *d){
push_val(d, 0);
}
push_val(d, et);
return TRUE;
return LABCOMM2014_TRUE;
} else {
printf("accept_array_decl: type=%x, should not happen\n",tid);
push_val(d, 0);
push_val(d, tid);
return FALSE;
return LABCOMM2014_FALSE;
}
}
/* pushes size */
static int accept_struct_decl(labcomm_sig_parser_t *d){
static int accept_struct_decl(labcomm2014_sig_parser_t *d){
size_t nbytes;
int tid = peek_varint(d, &nbytes) ;
if(tid == STRUCT_DECL) {
......@@ -672,19 +746,20 @@ static int accept_struct_decl(labcomm_sig_parser_t *d){
fieldsizes += pop_val(d);
}
push_val(d, fieldsizes);
return TRUE;
return LABCOMM2014_TRUE;
} else {
printf("accept_struct_decl: type=%x, should not happen\n",tid);
push_val(d, 0);
return FALSE;
return LABCOMM2014_FALSE;
}
}
/* pushes field size */
static int accept_field(labcomm_sig_parser_t *d){
VERBOSE_PRINTF("\tfield name: ");
accept_string(d);
pop_val(d); // ignore length, for now
static int accept_field(labcomm2014_sig_parser_t *d){
VERBOSE_PRINTF("\tfield: ");
accept_intentions(d);
pop_val(d); // ignore name pos, for now
pop_val(d); // ignore name length, for now
#ifdef RETURN_STRINGS
char *str = (char *) pop_ptr(d);
free(str);
......@@ -694,9 +769,9 @@ static int accept_field(labcomm_sig_parser_t *d){
pop_val(d); // ignore type, for now
// push(pop() is really a NOP , leave size on the stack when debugging done
VERBOSE_PRINTF("\n");
return TRUE;
return LABCOMM2014_TRUE;
}
static int accept_sample_data(labcomm_sig_parser_t *d){
static int accept_sample_data(labcomm2014_sig_parser_t *d){
accept_user_id(d);
unsigned int uid = pop_val(d);
printf("sample data... uid=0x%x\n", uid);
......@@ -705,20 +780,20 @@ static int accept_sample_data(labcomm_sig_parser_t *d){
dump_signature(d, uid);
#endif
#ifdef SKIP_BY_PARSING
struct labcomm_signature *sigt = get_sig_t(d, uid);
struct labcomm2014_signature *sigt = get_sig_t(d, uid);
int encoded_size = sigt->encoded_size(NULL);
INFO_PRINTF("encoded_size from sig: %d\n", encoded_size);
struct labcomm_signature *sig = get_sig_t(d, uid);
struct labcomm2014_signature *sig = get_sig_t(d, uid);
skip_packed_sample_data(d, sig);
#else
advancen(d, len);
#endif
return TRUE;
return LABCOMM2014_TRUE;
}
static int skip_type(unsigned int,labcomm_sig_parser_t*,unsigned char*,unsigned int,int*) ;
static int skip_type(unsigned int,labcomm2014_sig_parser_t*,unsigned char*,unsigned int,int*) ;
static int skip_array(labcomm_sig_parser_t *d, unsigned char *sig, int len, int *pos) {
static int skip_array(labcomm2014_sig_parser_t *d, unsigned char *sig, int len, int *pos) {
unsigned int skip = 0;
unsigned int tot_nbr_elem_tmp = 1;
size_t nbytes;
......@@ -753,7 +828,7 @@ static int skip_array(labcomm_sig_parser_t *d, unsigned char *sig, int len, int
int type = unpack_varint(sig, *pos, &nbytes);
*pos+=nbytes;
unsigned int elemSize = labcomm_sizeof_primitive(type);
unsigned int elemSize = labcomm2014_sizeof_primitive(type);
skip = elemSize * tot_nbr_elem_tmp;
......@@ -765,7 +840,7 @@ static int skip_array(labcomm_sig_parser_t *d, unsigned char *sig, int len, int
return skip + varSize;
}
int skip_struct(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int len, int *pos) {
int skip_struct(labcomm2014_sig_parser_t *d, unsigned char *sig, unsigned int len, int *pos) {
size_t nbytes;
int nFields = unpack_varint(sig,*pos, &nbytes);
*pos += nbytes;
......@@ -795,7 +870,7 @@ int skip_struct(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int len, i
}
#ifndef QUIET
/* print and skip */
int skip_type(unsigned int type, labcomm_sig_parser_t *d,
int skip_type(unsigned int type, labcomm2014_sig_parser_t *d,
unsigned char *sig, unsigned int len, int *pos)
{
int skipped=0;
......@@ -855,7 +930,7 @@ int skip_type(unsigned int type, labcomm_sig_parser_t *d,
return skipped;
}
#else
int skip_type(unsigned int type, labcomm_sig_parser_t *d,
int skip_type(unsigned int type, labcomm2014_sig_parser_t *d,
const char *sig, unsigned int len, int *pos)
{
int skipped=0;
......@@ -901,9 +976,9 @@ int skip_type(unsigned int type, labcomm_sig_parser_t *d,
}
#endif
/* parse signature and skip the corresponding bytes in the labcomm_sig_parser_t
/* parse signature and skip the corresponding bytes in the labcomm2014_sig_parser_t
*/
int skip_packed_sample_data(labcomm_sig_parser_t *d, struct labcomm_signature *sig) {
int skip_packed_sample_data(labcomm2014_sig_parser_t *d, struct labcomm2014_signature *sig) {
int pos = 0; //current position in signature
unsigned int skipped = 0; //skipped byte counter
while(pos < sig->size) {
......@@ -913,5 +988,5 @@ int skip_packed_sample_data(labcomm_sig_parser_t *d, struct labcomm_signature *s
skipped += skip_type(type, d, sig->signature, sig->size, &pos);
}
printf("skipped %d bytes\n", skipped);
return TRUE;
return LABCOMM2014_TRUE;
}
/* labcomm_sig_parser.h:
/* labcomm2014_sig_parser.h:
* an example parser for labcomm signatures, illustrating how to skip samples
* based on their signature. Intended as an embryo for introducing this
* functionality into the lib to allow a channel to survive types with no
......@@ -8,10 +8,10 @@
#ifndef LABCOMM_SIG_PARSER_H
#define LABCOMM_SIG_PARSER_H
#include "../labcomm.h"
#include "../labcomm_private.h"
#include "labcomm2014.h"
#include "labcomm2014_private.h"
#define DEBUG
#undef DEBUG
#define QUIET_STACK // don't print anything for push/pop
#undef DEBUG_STACK_VERBOSE // dump stack, otherwise just print value of top
......@@ -25,7 +25,7 @@
#ifdef STATIC_ALLOCATION
#define MAX_SIGNATURES 16
#define MAX_SIGNATURES 100
#define MAX_NAME_LEN 32
#define MAX_SIG_LEN 128
#define TYPEDEF_BASE MAX_SIGNATURES
......@@ -52,14 +52,14 @@ typedef struct {
// signatures start at index 0
// typedefs start at index MAX_SIGNATURES
#ifdef STATIC_ALLOCATION
struct labcomm_signature sig_ts[2*MAX_SIGNATURES];
struct labcomm2014_signature sig_ts[2*MAX_SIGNATURES];
unsigned int signatures_length[2*MAX_SIGNATURES];
unsigned int signatures_name_length[2*MAX_SIGNATURES];
unsigned char signatures_name[2*MAX_SIGNATURES][MAX_NAME_LEN];
unsigned char signatures[2*MAX_SIGNATURES][MAX_SIG_LEN];
#else
struct labcomm_signature *sig_ts; // [2*MAX_SIGNATURES]
struct labcomm2014_signature *sig_ts; // [2*MAX_SIGNATURES]
unsigned int *signatures_length; // [2*MAX_SIGNATURES]
unsigned char **signatures; // [2*MAX_SIGNATURES][MAX_SIG_LEN];
......@@ -68,30 +68,31 @@ typedef struct {
char **signatures_name; // [2*MAX_SIGNATURES][MAX_NAME_LEN];
#endif
} labcomm_sig_parser_t;
} labcomm2014_sig_parser_t;
int labcomm_sig_parser_init(labcomm_sig_parser_t *p, size_t size,
int labcomm2014_sig_parser_init(labcomm2014_sig_parser_t *p, size_t size,
size_t stacksize, size_t max_num_signatures,
size_t max_name_len, size_t max_sig_len);
int labcomm_sig_parser_read_file(labcomm_sig_parser_t *p, FILE *f);
void labcomm2014_sig_parser_free(labcomm2014_sig_parser_t *b);
int labcomm2014_sig_parser_read_file(labcomm2014_sig_parser_t *p, FILE *f);
int accept_packet(labcomm_sig_parser_t *p);
int accept_packet(labcomm2014_sig_parser_t *p);
struct labcomm_signature *get_sig_t(labcomm_sig_parser_t *p,unsigned int uid);
struct labcomm2014_signature *get_sig_t(labcomm2014_sig_parser_t *p,unsigned int uid);
unsigned int get_signature_len(labcomm_sig_parser_t *p,unsigned int uid);
char* get_signature_name(labcomm_sig_parser_t *p,unsigned int uid);
unsigned char* get_signature(labcomm_sig_parser_t *p,unsigned int uid);
void dump_signature(labcomm_sig_parser_t *p,unsigned int uid);
unsigned int get_signature_len(labcomm2014_sig_parser_t *p,unsigned int uid);
char* get_signature_name(labcomm2014_sig_parser_t *p,unsigned int uid);
unsigned char* get_signature(labcomm2014_sig_parser_t *p,unsigned int uid);
void dump_signature(labcomm2014_sig_parser_t *p,unsigned int uid);
int more(labcomm_sig_parser_t *b);
int more(labcomm2014_sig_parser_t *b);
/* parse signature and skip the corresponding bytes in the labcomm_sig_parser
/* parse signature and skip the corresponding bytes in the labcomm2014_sig_parser
*/
int skip_packed_sample_data(labcomm_sig_parser_t *p, struct labcomm_signature *sig);
int skip_packed_sample_data(labcomm2014_sig_parser_t *p, struct labcomm2014_signature *sig);
#ifdef QUIET
#define INFO_PRINTF(format, args...)
......@@ -131,5 +132,5 @@ typedef enum{
TYPE_DOUBLE = LABCOMM_DOUBLE,
TYPE_STRING = LABCOMM_STRING,
TYPE_SAMPLE_REF = LABCOMM_REF
} labcomm_type ;
} labcomm2014_type ;
#endif
/* labcomm_sig_parser.c:
/* labcomm2014_sig_parser.c:
* a main program for the example labcomm signatures parser
*/
#include <stdio.h>
#include <stdlib.h>
#include "labcomm_sig_parser.h"
#include "experimental/labcomm2014_sig_parser.h"
#define DEBUG_READ
#define BUF_SIZE 1024
#define STACK_SIZE 16
#define MAX_NUM_SIGNATURES 10
#define MAX_SIGNATURES 16
#define MAX_NAME_LEN 32
#define MAX_SIG_LEN 128
#define MAX_SIGNATURES 100
#define MAX_NAME_LEN 64
#define MAX_SIG_LEN 512
void test_read(labcomm_sig_parser_t *p) {
int r = labcomm_sig_parser_read_file(p, stdin);
void test_read(labcomm2014_sig_parser_t *p) {
int r = labcomm2014_sig_parser_read_file(p, stdin);
#ifdef DEBUG_READ
printf("read %d bytes:\n\n", r);
int i;
......@@ -29,9 +29,9 @@ void test_read(labcomm_sig_parser_t *p) {
#endif
}
int main() {
labcomm_sig_parser_t p;
labcomm2014_sig_parser_t p;
if(labcomm_sig_parser_init(&p, BUF_SIZE, STACK_SIZE,
if(labcomm2014_sig_parser_init(&p, BUF_SIZE, STACK_SIZE,
MAX_NUM_SIGNATURES, MAX_NAME_LEN, MAX_SIG_LEN) ) {
printf("failed to init buffer\n");
exit(1);
......@@ -41,5 +41,6 @@ int main() {
printf("--------------------------------------------- new packet: \n");
} while(more(&p) && accept_packet(&p));
printf("EOF\n");
labcomm2014_sig_parser_free(&p);
}
......@@ -250,11 +250,18 @@ void labcomm2014_set_local_index(struct labcomm2014_signature *signature)
int labcomm2014_get_local_index(const struct labcomm2014_signature *signature)
{
int result;
if (! signature) {
result = 0;
} else {
if (signature->index == 0) {
labcomm2014_error_fatal_global(LABCOMM2014_ERROR_SIGNATURE_NOT_SET,
"Signature not set: %s\n", signature->name);
"Signature not set: %s\n",
signature->name);
}
result = signature->index;
}
return signature->index;
return result;
}
int labcomm2014_get_local_type_index(const struct labcomm2014_signature *signature)
......@@ -265,7 +272,7 @@ int labcomm2014_get_local_type_index(const struct labcomm2014_signature *signatu
int labcomm2014_internal_sizeof(const struct labcomm2014_signature *signature,
void *v)
{
int length = signature->encoded_size(v);
int length = signature->encoded_size(signature, v);
return (labcomm2014_size_packed32(signature->index) +
labcomm2014_size_packed32(length) +
length);
......
......@@ -20,8 +20,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __LABCOMM_H__
#define __LABCOMM_H__
#ifndef __LABCOMM2014_H__
#define __LABCOMM2014_H__
#include <stdarg.h>
......@@ -112,6 +112,10 @@ int labcomm2014_decoder_ioctl(struct labcomm2014_decoder *decoder,
uint32_t ioctl_action,
...);
const struct labcomm2014_sample_ref *labcomm2014_decoder_get_sample_ref(
struct labcomm2014_decoder *decoder,
const struct labcomm2014_signature *signature);
/*
* Encoder
*/
......@@ -133,6 +137,8 @@ int labcomm2014_encoder_ioctl(struct labcomm2014_encoder *encoder,
uint32_t ioctl_action,
...);
#define LABCOMM_VOID ((void*)1)
const struct labcomm2014_sample_ref *labcomm2014_encoder_get_sample_ref(
struct labcomm2014_encoder *encoder,
const struct labcomm2014_signature *signature);
#endif
......@@ -46,82 +46,31 @@ struct sample_entry {
void *context;
};
struct labcomm2014_decoder {
struct labcomm2014_reader *reader;
int reader_allocated;
int version_ok;
struct labcomm2014_error_handler *error;
struct labcomm2014_memory *memory;
struct labcomm2014_scheduler *scheduler;
labcomm2014_error_handler_callback on_error;
labcomm2014_handle_new_datatype_callback on_new_datatype;
struct decoder {
struct labcomm2014_decoder decoder;
LABCOMM_SIGNATURE_ARRAY_DEF(local, struct sample_entry);
LABCOMM_SIGNATURE_ARRAY_DEF(remote_to_local, int);
LABCOMM_SIGNATURE_ARRAY_DEF(local_ref, const struct labcomm2014_signature *);
LABCOMM_SIGNATURE_ARRAY_DEF(remote_to_local_ref, int);
};
struct labcomm2014_decoder *labcomm2014_decoder_new(
struct labcomm2014_reader *reader,
struct labcomm2014_error_handler *error,
struct labcomm2014_memory *memory,
struct labcomm2014_scheduler *scheduler)
{
struct labcomm2014_decoder *result;
result = labcomm2014_memory_alloc(memory, 0, sizeof(*result));
if (result) {
result->reader = reader;
result->reader->decoder = result;
result->reader->data = 0;
result->reader->data_size = 0;
result->reader->count = 0;
result->reader->pos = 0;
result->reader->error = 0;
result->reader_allocated = 0;
result->version_ok = 0;
result->error = error;
result->memory = memory;
result->scheduler = scheduler;
result->on_error = labcomm20142014_on_error_fprintf;
LABCOMM_SIGNATURE_ARRAY_INIT(result->local, struct sample_entry);
LABCOMM_SIGNATURE_ARRAY_INIT(result->remote_to_local, int);
LABCOMM_SIGNATURE_ARRAY_INIT(result->local_ref,
const struct labcomm2014_signature*);
LABCOMM_SIGNATURE_ARRAY_INIT(result->remote_to_local_ref, int);
}
return result;
}
void labcomm2014_decoder_free(struct labcomm2014_decoder* d)
{
struct labcomm2014_memory *memory = d->memory;
labcomm2014_reader_free(d->reader, d->reader->action_context);
LABCOMM_SIGNATURE_ARRAY_FREE(memory, d->local, struct sample_entry);
LABCOMM_SIGNATURE_ARRAY_FREE(memory, d->remote_to_local, int);
LABCOMM_SIGNATURE_ARRAY_FREE(memory, d->local_ref,
const struct labcomm2014_signature*);
LABCOMM_SIGNATURE_ARRAY_FREE(memory, d->remote_to_local_ref, int);
labcomm2014_memory_free(memory, 0, d);
}
static int handle_sample_def(struct labcomm2014_decoder *d, int remote_index,
const struct labcomm2014_signature *signature)
{
struct decoder *id = d->context;
int result;
int i;
const struct labcomm2014_signature *local_signature = NULL;
int local_index = 0;
labcomm2014_scheduler_data_lock(d->scheduler);
LABCOMM_SIGNATURE_ARRAY_FOREACH(d->local, struct sample_entry, i) {
LABCOMM_SIGNATURE_ARRAY_FOREACH(id->local, struct sample_entry, i) {
struct sample_entry *s;
int *remote_to_local;
result = -ENOENT;
s = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
d->local, struct sample_entry, i);
id->local, struct sample_entry, i);
if (s->signature &&
s->signature->size == signature->size &&
strcmp(s->signature->name, signature->name) == 0 &&
......@@ -131,7 +80,7 @@ static int handle_sample_def(struct labcomm2014_decoder *d, int remote_index,
local_signature = s->signature;
local_index = i;
remote_to_local = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
d->remote_to_local, int,
id->remote_to_local, int,
remote_index);
*remote_to_local = i;
result = remote_index;
......@@ -151,23 +100,26 @@ static int handle_sample_def(struct labcomm2014_decoder *d, int remote_index,
static int handle_sample_ref(struct labcomm2014_decoder *d, int remote_index,
const struct labcomm2014_signature *signature)
{
struct decoder *id = d->context;
int result;
int i;
labcomm2014_scheduler_data_lock(d->scheduler);
LABCOMM_SIGNATURE_ARRAY_FOREACH(d->local_ref, const struct labcomm2014_signature *, i) {
LABCOMM_SIGNATURE_ARRAY_FOREACH(id->local_ref,
const struct labcomm2014_signature *, i) {
const struct labcomm2014_signature *s;
int *remote_to_local_ref;
result = -ENOENT;
s = LABCOMM_SIGNATURE_ARRAY_GET(d->local_ref, const struct labcomm2014_signature *, i, 0);
s = LABCOMM_SIGNATURE_ARRAY_GET(id->local_ref, const struct labcomm2014_signature *, i, 0);
if (s &&
s->signature &&
s->size == signature->size &&
strcmp(s->name, signature->name) == 0 &&
memcmp((void*)s->signature, (void*)signature->signature, signature->size) == 0) {
remote_to_local_ref = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
d->remote_to_local_ref, int,
id->remote_to_local_ref,
int,
remote_index);
*remote_to_local_ref = i;
result = remote_index;
......@@ -178,22 +130,47 @@ static int handle_sample_ref(struct labcomm2014_decoder *d, int remote_index,
return result;
}
static int decoder_skip(struct labcomm2014_decoder *d, int len, int tag)
static int reader_skip(struct labcomm2014_reader *r, int len, int tag)
{
int i;
DECODER_DEBUG_FPRINTF(stdout, "got tag 0x%x, skipping %d bytes\n", tag, len);
for(i = 0; i <len; i++){
DECODER_DEBUG_FPRINTF(stderr, ".");
labcomm2014_read_byte(d->reader);
if (d->reader->error < 0) {
DECODER_DEBUG_FPRINTF(stderr, "\nerror while skipping: %d\n", d->reader->error);
return d->reader->error;
labcomm2014_read_byte(r);
if (r->error < 0) {
DECODER_DEBUG_FPRINTF(stderr, "\nerror while skipping: %d\n", r->error);
return r->error;
}
}
DECODER_DEBUG_FPRINTF(stderr, "\n");
return tag;
}
static char* TODO_read_intentions(struct labcomm2014_reader *r)
{
int numInts = labcomm2014_read_byte(r);
int i;
char *name=NULL;
printf("TODO_read_intentions: numInts=%d\n", numInts);
for(i=0; i <numInts; i++){
int klen = labcomm2014_read_packed32(r);
printf("TODO_read_intentions: klen=%d\n", klen);
if(klen == 0) {
name = labcomm2014_read_string(r);
printf("TODO_read_intentions: name=%s\n", name);
}else{
int vlen;
reader_skip(r, klen, 1);
vlen = labcomm2014_read_packed32(r);
reader_skip(r, vlen, 1);
printf("TODO_read_intentions: skipping value, %d bytes\n", vlen);
}
}
return name;
}
static int decode_sample_def_or_ref(struct labcomm2014_decoder *d, int kind)
{
int result;
......@@ -205,10 +182,11 @@ static int decode_sample_def_or_ref(struct labcomm2014_decoder *d, int kind)
result = d->reader->error;
goto out;
}
signature.name = labcomm2014_read_string(d->reader);
signature.name = TODO_read_intentions(d->reader);
if (d->reader->error < 0) {
result = d->reader->error;
goto out;
goto free_signature_name;
}
signature.size = labcomm2014_read_packed32(d->reader);
if (d->reader->error < 0) {
......@@ -246,7 +224,15 @@ static int decode_sample_def_or_ref(struct labcomm2014_decoder *d, int kind)
free_signature_signature:
labcomm2014_memory_free(d->memory, 1, signature.signature);
free_signature_name:
if (signature.name) {
if (result == -ENOENT) {
labcomm2014_error_warning(d->error,
LABCOMM2014_ERROR_DEC_NO_REG_SIGNATURE,
"Signature not found: %s\n",
signature.name);
}
labcomm2014_memory_free(d->memory, 0, signature.name);
}
out:
return result;
}
......@@ -299,7 +285,7 @@ static int decode_pragma(struct labcomm2014_decoder *d,
}
int bytes = labcomm2014_size_string(pragma_type);
int psize = len-bytes;
result = decoder_skip(d, psize, LABCOMM_PRAGMA);
result = reader_skip(d->reader, psize, LABCOMM_PRAGMA);
out:
return result;
}
......@@ -307,20 +293,24 @@ out:
static labcomm2014_decoder_function lookup_h(struct labcomm2014_decoder *d,
struct call_handler_context *wrap,
int remote_index,
int **local_index)
int *r_local_index)
{
struct decoder *id = d->context;
labcomm2014_decoder_function do_decode = NULL;
int *local_index;
labcomm2014_scheduler_data_lock(d->scheduler);
*local_index = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
d->remote_to_local, int,
local_index = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
id->remote_to_local, int,
remote_index);
if (**local_index != 0) {
*r_local_index = *local_index;
if (*local_index != 0) {
struct sample_entry *entry;
entry = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
d->local, struct sample_entry,
**local_index);
wrap->local_index = **local_index;
id->local, struct sample_entry,
*local_index);
wrap->local_index = *local_index;
wrap->signature = entry->signature;
wrap->handler = entry->handler;
wrap->context = entry->context;
......@@ -339,7 +329,7 @@ static int decode_and_handle(struct labcomm2014_decoder *d,
int remote_index)
{
int result;
int *local_index;
int local_index;
struct call_handler_context wrap = {
.reader = d->reader,
.remote_index = remote_index,
......@@ -348,7 +338,7 @@ static int decode_and_handle(struct labcomm2014_decoder *d,
.context = NULL,
};
labcomm2014_decoder_function do_decode = lookup_h(registry, &wrap, remote_index, &local_index);
result = *local_index;
result = local_index;
if (do_decode) {
do_decode(d->reader, call_handler, &wrap);
if (d->reader->error < 0) {
......@@ -359,7 +349,13 @@ static int decode_and_handle(struct labcomm2014_decoder *d,
}
return result;
}
int labcomm2014_decoder_decode_one(struct labcomm2014_decoder *d)
{
return d->decode_one(d);
}
static int do_decode_one(struct labcomm2014_decoder *d)
{
int result, remote_index, length;
......@@ -378,15 +374,16 @@ int labcomm2014_decoder_decode_one(struct labcomm2014_decoder *d)
char *version = labcomm2014_read_string(d->reader);
if (d->reader->error < 0) {
result = d->reader->error;
goto out;
}
if (strcmp(version, CURRENT_VERSION) == 0) {
} else if (strcmp(version, CURRENT_VERSION) == 0) {
result = LABCOMM_VERSION;
d->version_ok = 1;
} else {
result = -ECONNRESET;
}
labcomm2014_memory_free(d->memory, 1, version);
if (result < 0) {
goto out;
}
} else if (! d->version_ok) {
DEBUG_FPRINTF(stderr, "No VERSION %d %d\n", remote_index, length);
result = -ECONNRESET;
......@@ -398,19 +395,19 @@ int labcomm2014_decoder_decode_one(struct labcomm2014_decoder *d)
result = decode_and_handle(d, d, remote_index);
if(result == -ENOENT) {
//No handler for type_defs, skip
result = decoder_skip(d, length, remote_index);
result = reader_skip(d->reader, length, remote_index);
}
} else if (remote_index == LABCOMM_TYPE_BINDING) {
result = decode_and_handle(d, d, remote_index);
if(result == -ENOENT) {
//No handler for type_bindings, skip
result = decoder_skip(d, length, remote_index);
result = reader_skip(d->reader, length, remote_index);
}
} else if (remote_index == LABCOMM_PRAGMA) {
result = decode_pragma(d, d, length);
} else if (remote_index < LABCOMM_USER) {
DECODER_DEBUG_FPRINTF(stderr, "SKIP %d %d\n", remote_index, length);
result = decoder_skip(d, length, remote_index);
result = reader_skip(d->reader, length, remote_index);
} else {
result = decode_and_handle(d, d, remote_index);
}
......@@ -432,9 +429,7 @@ int labcomm2014_decoder_ioctl(struct labcomm2014_decoder *d,
va_list va;
va_start(va, action);
result = labcomm2014_reader_ioctl(d->reader,
d->reader->action_context,
0, 0, NULL, action, va);
result = d->ioctl(d, NULL, action, va);
va_end(va);
return result;
}
......@@ -443,6 +438,14 @@ int labcomm2014_decoder_sample_ref_register(
struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature)
{
return d->ref_register(d, signature);
}
static int do_ref_register(
struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature)
{
struct decoder *id = d->context;
int local_index, *remote_to_local_ref;
const struct labcomm2014_signature **s;
......@@ -450,13 +453,13 @@ int labcomm2014_decoder_sample_ref_register(
if (local_index <= 0) { goto out; }
labcomm2014_scheduler_data_lock(d->scheduler);
s = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
d->local_ref,
id->local_ref,
const struct labcomm2014_signature*, local_index);
if (s == NULL) { local_index = -ENOMEM; goto unlock; };
if (*s) { goto unlock; }
*s = signature;
remote_to_local_ref = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
d->remote_to_local_ref,
id->remote_to_local_ref,
int, local_index);
*remote_to_local_ref = 0;
unlock:
......@@ -465,20 +468,25 @@ out:
return local_index;
}
int labcomm2014_internal_decoder_ioctl(struct labcomm2014_decoder *d,
static int do_ioctl(struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature,
uint32_t action, va_list va)
{
struct decoder *id = d->context;
int result;
int local_index, remote_index;
local_index = labcomm2014_get_local_index(signature);
if (local_index == 0) {
remote_index = 0;
} else {
labcomm2014_scheduler_data_lock(d->scheduler);
remote_index = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
d->local,
id->local,
struct sample_entry,
local_index)->remote_index;
labcomm2014_scheduler_data_unlock(d->scheduler);
}
result = labcomm2014_reader_ioctl(d->reader, d->reader->action_context,
local_index, remote_index,
signature, action, va);
......@@ -501,7 +509,7 @@ static void decode_raw_type_def(
struct labcomm2014_raw_type_def v;
v.index = labcomm2014_read_packed32(r);
if (r->error < 0) { goto out; }
v.name = labcomm2014_read_string(r);
v.name = TODO_read_intentions(r);
if (r->error < 0) { goto free_name; }
v.length = labcomm2014_read_packed32(r);
if (r->error < 0) { goto free_name; }
......@@ -530,13 +538,14 @@ int labcomm2014_decoder_register_labcomm2014_type_def(
void *context
)
{
struct decoder *id = d->context;
int tag = LABCOMM_TYPE_DEF;
struct sample_entry *entry;
int *remote_to_local;
labcomm2014_scheduler_data_lock(d->scheduler);
entry = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
d->local, struct sample_entry,
id->local, struct sample_entry,
tag);
if (entry == NULL) { tag = -ENOMEM; goto unlock; }
entry->remote_index = tag;
......@@ -546,7 +555,7 @@ int labcomm2014_decoder_register_labcomm2014_type_def(
entry->context = context;
remote_to_local = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
d->remote_to_local, int,
id->remote_to_local, int,
tag);
*remote_to_local = tag;
unlock:
......@@ -584,13 +593,14 @@ int labcomm2014_decoder_register_labcomm2014_type_binding(
void *context
)
{
struct decoder *id = d->context;
int tag = LABCOMM_TYPE_BINDING;
struct sample_entry *entry;
int *remote_to_local;
labcomm2014_scheduler_data_lock(d->scheduler);
entry = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
d->local, struct sample_entry,
id->local, struct sample_entry,
tag);
if (entry == NULL) { tag = -ENOMEM; goto unlock; }
entry->remote_index = tag;
......@@ -600,7 +610,7 @@ int labcomm2014_decoder_register_labcomm2014_type_binding(
entry->context = context;
remote_to_local = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
d->remote_to_local, int,
id->remote_to_local, int,
tag);
*remote_to_local = tag;
unlock:
......@@ -612,13 +622,14 @@ unlock:
//// End type_def handling
#endif
int labcomm2014_internal_decoder_register(
static int do_register_sample(
struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature,
labcomm2014_decoder_function decode,
labcomm2014_handler_function handler,
void *context)
{
struct decoder *id = d->context;
int local_index;
struct sample_entry *entry;
......@@ -632,7 +643,7 @@ int labcomm2014_internal_decoder_register(
labcomm2014_scheduler_data_lock(d->scheduler);
entry = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
d->local, struct sample_entry,
id->local, struct sample_entry,
local_index);
if (entry == NULL) { local_index = -ENOMEM; goto unlock; }
entry->remote_index = 0;
......@@ -646,20 +657,96 @@ out:
return local_index;
}
const struct labcomm2014_signature *labcomm2014_internal_decoder_index_to_signature(
static const struct labcomm2014_sample_ref *do_index_to_sample_ref(
struct labcomm2014_decoder *d, int index)
{
const struct labcomm2014_signature *result = 0;
struct decoder *id = d->context;
int local_index;
labcomm2014_scheduler_data_lock(d->scheduler);
local_index = LABCOMM_SIGNATURE_ARRAY_GET(d->remote_to_local_ref,
local_index = LABCOMM_SIGNATURE_ARRAY_GET(id->remote_to_local_ref,
int, index, 0);
if (local_index) {
result = LABCOMM_SIGNATURE_ARRAY_GET(d->local_ref,
result = LABCOMM_SIGNATURE_ARRAY_GET(id->local_ref,
const struct labcomm2014_signature*,
local_index, 0);
}
labcomm2014_scheduler_data_unlock(d->scheduler);
return result;
return labcomm2014_signature_to_sample_ref(result);
}
static const struct labcomm2014_sample_ref *do_ref_get(
struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature)
{
return (const struct labcomm2014_sample_ref *) signature;
}
static void do_free(struct labcomm2014_decoder* d)
{
struct decoder *id = d->context;
struct labcomm2014_memory *memory = d->memory;
labcomm2014_reader_free(d->reader, d->reader->action_context);
LABCOMM_SIGNATURE_ARRAY_FREE(memory, id->local, struct sample_entry);
LABCOMM_SIGNATURE_ARRAY_FREE(memory, id->remote_to_local, int);
LABCOMM_SIGNATURE_ARRAY_FREE(memory, id->local_ref,
const struct labcomm2014_signature*);
LABCOMM_SIGNATURE_ARRAY_FREE(memory, id->remote_to_local_ref, int);
labcomm2014_memory_free(memory, 0, id);
}
void labcomm2014_decoder_free(struct labcomm2014_decoder* d)
{
d->free(d);
}
struct labcomm2014_decoder *labcomm2014_decoder_new(
struct labcomm2014_reader *reader,
struct labcomm2014_error_handler *error,
struct labcomm2014_memory *memory,
struct labcomm2014_scheduler *scheduler)
{
struct decoder *result;
result = labcomm2014_memory_alloc(memory, 0, sizeof(*result));
if (result) {
result->decoder.context = result;
result->decoder.reader = reader;
result->decoder.reader->decoder = &result->decoder;
result->decoder.reader->data = 0;
result->decoder.reader->data_size = 0;
result->decoder.reader->count = 0;
result->decoder.reader->pos = 0;
result->decoder.reader->error = 0;
result->decoder.reader_allocated = 0;
result->decoder.version_ok = 0;
result->decoder.error = error;
result->decoder.memory = memory;
result->decoder.scheduler = scheduler;
result->decoder.on_error = labcomm20142014_on_error_fprintf;
result->decoder.free = do_free;
result->decoder.decode_one = do_decode_one;
result->decoder.ref_register = do_ref_register;
result->decoder.sample_register = do_register_sample;
result->decoder.ioctl = do_ioctl;
result->decoder.index_to_sample_ref = do_index_to_sample_ref;
result->decoder.ref_get = do_ref_get;
LABCOMM_SIGNATURE_ARRAY_INIT(result->local, struct sample_entry);
LABCOMM_SIGNATURE_ARRAY_INIT(result->remote_to_local, int);
LABCOMM_SIGNATURE_ARRAY_INIT(result->local_ref,
const struct labcomm2014_signature*);
LABCOMM_SIGNATURE_ARRAY_INIT(result->remote_to_local_ref, int);
}
return &(result->decoder);
}
const struct labcomm2014_sample_ref *labcomm2014_decoder_get_sample_ref(
struct labcomm2014_decoder *decoder,
const struct labcomm2014_signature *signature)
{
return decoder->ref_get(decoder, signature);
}
......@@ -29,89 +29,84 @@
//define the following to disable encoding of typedefs
#undef LABCOMM_WITHOUT_TYPE_DEFS
struct labcomm2014_encoder {
struct labcomm2014_writer *writer;
struct labcomm2014_error_handler *error;
struct labcomm2014_memory *memory;
struct labcomm2014_scheduler *scheduler;
#undef LABCOMM2014_WITH_SANITY_CHECKS
struct encoder {
struct labcomm2014_encoder encoder;
LABCOMM_SIGNATURE_ARRAY_DEF(registered, int);
LABCOMM_SIGNATURE_ARRAY_DEF(sample_ref, int);
LABCOMM_SIGNATURE_ARRAY_DEF(typedefs, int);
};
static struct labcomm2014_encoder *internal_encoder_new(
struct labcomm2014_writer *writer,
struct labcomm2014_error_handler *error,
struct labcomm2014_memory *memory,
struct labcomm2014_scheduler *scheduler,
labcomm2014_bool outputVer)
#ifdef LABCOMM2014_WITH_SANITY_CHECKS
static int expectedByteCount;
static void encoder_check_write_start(struct labcomm2014_encoder *e, int numBytes)
{
struct labcomm2014_encoder *result;
result = labcomm2014_memory_alloc(memory, 0, sizeof(*result));
if (result) {
int length;
int previouslyWritten = 0;
int err = labcomm2014_encoder_ioctl(e, LABCOMM_IOCTL_WRITER_GET_BYTES_WRITTEN, &previouslyWritten);
if(err) {
printf("ERROR: get_bytes_written returned %d (%s)\n", err, strerror(err));
}
expectedByteCount = numBytes + previouslyWritten;
#ifdef LABCOMM2014_ENCODER_DEBUG
printf("previously written: %d bytes, length = %d bytes, expected = %d bytes\n",
previouslyWritten, numBytes, expectedByteCount);
#endif
}
result->writer = writer;
result->writer->encoder = result;
result->writer->data = NULL;
result->writer->data_size = 0;
result->writer->count = 0;
result->writer->pos = 0;
result->writer->error = 0;
result->error = error;
result->memory = memory;
result->scheduler = scheduler;
LABCOMM_SIGNATURE_ARRAY_INIT(result->registered, int);
LABCOMM_SIGNATURE_ARRAY_INIT(result->sample_ref, int);
LABCOMM_SIGNATURE_ARRAY_INIT(result->typedefs, int);
labcomm2014_writer_alloc(result->writer,
result->writer->action_context);
if(outputVer) {
labcomm2014_writer_start(result->writer,
result->writer->action_context,
LABCOMM_VERSION, NULL, CURRENT_VERSION);
labcomm2014_write_packed32(result->writer, LABCOMM_VERSION);
length = labcomm2014_size_string(CURRENT_VERSION);
labcomm2014_write_packed32(result->writer, length);
labcomm2014_write_string(result->writer, CURRENT_VERSION);
labcomm2014_writer_end(result->writer, result->writer->action_context);
static int encoder_check_write_end(struct labcomm2014_encoder *e)
{
int written = 0;
int err = labcomm2014_encoder_ioctl(e, LABCOMM_IOCTL_WRITER_GET_BYTES_WRITTEN, &written);
if(err) {
printf("ERROR: get_bytes_written returned %d (%s)\n", err, strerror(err));
}
int result = 0;
#ifdef LABCOMM2014_ENCODER_DEBUG
printf("DEBUG: encoder_check_write_end: expected: %d, was: %d\n",
expectedByteCount, written);
#endif
if(written != expectedByteCount) {
printf("WARNING! encoder_check_write_end: expected: %d, was: %d\n",
expectedByteCount, written);
result = -EINVAL;
}
return result;
}
#endif
/* XXX: TEMPORARY PLACEHOLDERS FOR INTENTIONS */
struct labcomm2014_encoder *labcomm2014_encoder_new(
struct labcomm2014_writer *writer,
struct labcomm2014_error_handler *error,
struct labcomm2014_memory *memory,
struct labcomm2014_scheduler *scheduler)
{
return internal_encoder_new(writer,error,memory,scheduler,LABCOMM2014_TRUE);
static int TODO_sizeof_intentions(const struct labcomm2014_signature *signature) {
int res = labcomm2014_size_string(signature->name) + 2;
return res;
}
void labcomm2014_encoder_free(struct labcomm2014_encoder* e)
static int TODO_encode_intentions(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
struct labcomm2014_memory *memory = e->memory;
int result = -EINVAL;
labcomm2014_writer_free(e->writer, e->writer->action_context);
LABCOMM_SIGNATURE_ARRAY_FREE(e->memory, e->registered, int);
LABCOMM_SIGNATURE_ARRAY_FREE(e->memory, e->sample_ref, int);
LABCOMM_SIGNATURE_ARRAY_FREE(e->memory, e->typedefs, int);
labcomm2014_memory_free(memory, 0, e);
}
labcomm2014_write_packed32(e->writer, 1); // one intention: the name
labcomm2014_write_packed32(e->writer, 0); // key: the empty string
labcomm2014_write_string(e->writer, signature->name);
int labcomm2014_internal_encoder_register(
result = e->writer->error;
return result;
}
static int do_sample_register(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
labcomm2014_encoder_function encode)
{
int result = -EINVAL;
struct encoder *ie = e->context;
int index, *done, err, i, length;
index = labcomm2014_get_local_index(signature);
labcomm2014_scheduler_writer_lock(e->scheduler);
if (index <= 0) { goto out; }
done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, e->registered, int, index);
done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, ie->registered, int, index);
if (*done) {
goto out; }
*done = 1;
......@@ -121,12 +116,12 @@ int labcomm2014_internal_encoder_register(
if (err != 0) { result = err; goto out; }
labcomm2014_write_packed32(e->writer, LABCOMM_SAMPLE_DEF);
length = (labcomm2014_size_packed32(index) +
labcomm2014_size_string(signature->name) +
TODO_sizeof_intentions(signature) +
labcomm2014_size_packed32(signature->size) +
signature->size);
labcomm2014_write_packed32(e->writer, length);
labcomm2014_write_packed32(e->writer, index);
labcomm2014_write_string(e->writer, signature->name);
TODO_encode_intentions(e, signature);
labcomm2014_write_packed32(e->writer, signature->size);
for (i = 0 ; i < signature->size ; i++) {
if (e->writer->pos >= e->writer->count) {
......@@ -142,18 +137,19 @@ out:
return result;
}
int labcomm2014_internal_encode(
static int do_encode(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
labcomm2014_encoder_function encode,
void *value)
{
int result, index, length;
struct encoder *ie = e->context;
index = labcomm2014_get_local_index(signature);
length = (signature->encoded_size(value));
length = (signature->encoded_size(signature, value));
labcomm2014_scheduler_writer_lock(e->scheduler);
if (! LABCOMM_SIGNATURE_ARRAY_GET(e->registered, int, index, 0)) {
if (! LABCOMM_SIGNATURE_ARRAY_GET(ie->registered, int, index, 0)) {
result = -EINVAL;
goto no_end;
}
......@@ -175,15 +171,23 @@ no_end:
int labcomm2014_encoder_sample_ref_register(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
return e->ref_register(e, signature);
}
static int do_ref_register(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
int result = -EINVAL;
struct encoder *ie = e->context;
int index, *done, err, i, length;
index = labcomm2014_get_local_index(signature);
labcomm2014_scheduler_writer_lock(e->scheduler);
if (index <= 0) { goto out; }
done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, e->sample_ref, int, index);
done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, ie->sample_ref, int, index);
if (*done) { goto out; }
*done = 1;
err = labcomm2014_writer_start(e->writer, e->writer->action_context,
......@@ -192,12 +196,12 @@ int labcomm2014_encoder_sample_ref_register(
if (err != 0) { result = err; goto out; }
labcomm2014_write_packed32(e->writer, LABCOMM_SAMPLE_REF);
length = (labcomm2014_size_packed32(index) +
labcomm2014_size_string(signature->name) +
TODO_sizeof_intentions(signature) +
labcomm2014_size_packed32(signature->size) +
signature->size);
labcomm2014_write_packed32(e->writer, length);
labcomm2014_write_packed32(e->writer, index);
labcomm2014_write_string(e->writer, signature->name);
TODO_encode_intentions(e, signature);
labcomm2014_write_packed32(e->writer, signature->size);
for (i = 0 ; i < signature->size ; i++) {
if (e->writer->pos >= e->writer->count) {
......@@ -226,16 +230,15 @@ int labcomm2014_encoder_ioctl(struct labcomm2014_encoder *encoder,
}
va_start(va, action);
result = labcomm2014_writer_ioctl(encoder->writer,
encoder->writer->action_context,
0, NULL, action, va);
result = encoder->ioctl(encoder, NULL, action, va);
va_end(va);
out:
return result;
}
int labcomm2014_internal_encoder_ioctl(struct labcomm2014_encoder *encoder,
static int do_ioctl(
struct labcomm2014_encoder *encoder,
const struct labcomm2014_signature *signature,
uint32_t action, va_list va)
{
......@@ -249,14 +252,17 @@ int labcomm2014_internal_encoder_ioctl(struct labcomm2014_encoder *encoder,
return result;
}
int labcomm2014_internal_encoder_signature_to_index(
struct labcomm2014_encoder *e, const struct labcomm2014_signature *signature)
static int do_sample_ref_to_index(
struct labcomm2014_encoder *e,
const struct labcomm2014_sample_ref *sample_ref)
{
/* writer_lock should be held at this point */
struct encoder *ie = e->context;
int index = 0;
if (signature != NULL) {
index = labcomm2014_get_local_index(signature);
if (! LABCOMM_SIGNATURE_ARRAY_GET(e->sample_ref, int, index, 0)) {
if (sample_ref != NULL) {
index = labcomm2014_get_local_index(
(struct labcomm2014_signature *)sample_ref);
if (! LABCOMM_SIGNATURE_ARRAY_GET(ie->sample_ref, int, index, 0)) {
index = 0;
}
}
......@@ -321,11 +327,12 @@ static int internal_reg_type(
{
int result = -EINVAL;
int index, *done, err;
struct encoder *ie = e->context;
index = labcomm2014_get_local_index(signature);
labcomm2014_scheduler_writer_lock(e->scheduler);
if (index <= 0) { goto out; }
done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, e->typedefs, int, index);
done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, ie->typedefs, int, index);
if (*done) { goto out; }
*done = 1;
err = labcomm2014_writer_start(e->writer, e->writer->action_context,
......@@ -333,19 +340,27 @@ static int internal_reg_type(
if (err == -EALREADY) { result = 0; goto out; }
if (err != 0) { result = err; goto out; }
printf("internal_reg_type: %s\n", signature->name);
int sig_size = calc_sig_encoded_size(e, signature);
int len = labcomm2014_size_packed32(index) +
labcomm2014_size_string(signature->name) +
labcomm2014_size_packed32(sig_size) +
sig_size;
int len_idx= labcomm2014_size_packed32(index);
int len_ints =TODO_sizeof_intentions(signature);
int len_sigsize =labcomm2014_size_packed32(sig_size);
int len_sig =sig_size;
int len = len_idx + len_ints + len_sigsize + len_sig;
labcomm2014_write_packed32(e->writer, LABCOMM_TYPE_DEF);
labcomm2014_write_packed32(e->writer, len);
#ifdef LABCOMM2014_WITH_SANITY_CHECKS
encoder_check_write_start(e, len);
#endif
labcomm2014_write_packed32(e->writer, index);
labcomm2014_write_string(e->writer, signature->name);
TODO_encode_intentions(e, signature);
labcomm2014_write_packed32(e->writer, sig_size);
do_write_signature(e, signature, LABCOMM2014_FALSE);
#ifdef LABCOMM2014_WITH_SANITY_CHECKS
encoder_check_write_end(e);
#endif
labcomm2014_writer_end(e->writer, e->writer->action_context);
result = e->writer->error;
out:
......@@ -354,7 +369,7 @@ out:
}
#endif
int labcomm2014_internal_encoder_type_register(
static int do_type_register(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
......@@ -364,7 +379,8 @@ int labcomm2014_internal_encoder_type_register(
return 0;
#endif
}
int labcomm2014_internal_encoder_type_bind(
static int do_type_bind(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
char has_deps)
......@@ -396,3 +412,98 @@ out:
return 0;
#endif
}
static const struct labcomm2014_sample_ref *do_ref_get(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
return (const struct labcomm2014_sample_ref *) signature;
}
void labcomm2014_encoder_free(struct labcomm2014_encoder* e)
{
e->free(e);
}
static void do_free(struct labcomm2014_encoder* e)
{
struct encoder *ie = e->context;
struct labcomm2014_memory *memory = e->memory;
labcomm2014_writer_free(e->writer, e->writer->action_context);
LABCOMM_SIGNATURE_ARRAY_FREE(e->memory, ie->registered, int);
LABCOMM_SIGNATURE_ARRAY_FREE(e->memory, ie->sample_ref, int);
LABCOMM_SIGNATURE_ARRAY_FREE(e->memory, ie->typedefs, int);
labcomm2014_memory_free(memory, 0, ie);
}
static struct labcomm2014_encoder *internal_encoder_new(
struct labcomm2014_writer *writer,
struct labcomm2014_error_handler *error,
struct labcomm2014_memory *memory,
struct labcomm2014_scheduler *scheduler,
labcomm2014_bool outputVer)
{
struct encoder *result;
result = labcomm2014_memory_alloc(memory, 0, sizeof(*result));
if (result) {
int length;
result->encoder.context = result;
result->encoder.writer = writer;
result->encoder.writer->encoder = &result->encoder;
result->encoder.writer->data = NULL;
result->encoder.writer->data_size = 0;
result->encoder.writer->count = 0;
result->encoder.writer->pos = 0;
result->encoder.writer->error = 0;
result->encoder.error = error;
result->encoder.memory = memory;
result->encoder.scheduler = scheduler;
result->encoder.free = do_free;
result->encoder.type_register = do_type_register;
result->encoder.type_bind = do_type_bind;
result->encoder.sample_register = do_sample_register;
result->encoder.ref_register = do_ref_register;
result->encoder.encode = do_encode;
result->encoder.ioctl = do_ioctl;
result->encoder.sample_ref_to_index = do_sample_ref_to_index;
result->encoder.ref_get = do_ref_get;
LABCOMM_SIGNATURE_ARRAY_INIT(result->registered, int);
LABCOMM_SIGNATURE_ARRAY_INIT(result->sample_ref, int);
LABCOMM_SIGNATURE_ARRAY_INIT(result->typedefs, int);
labcomm2014_writer_alloc(result->encoder.writer,
result->encoder.writer->action_context);
if(outputVer) {
labcomm2014_writer_start(result->encoder.writer,
result->encoder.writer->action_context,
LABCOMM_VERSION, NULL, CURRENT_VERSION);
labcomm2014_write_packed32(result->encoder.writer, LABCOMM_VERSION);
length = labcomm2014_size_string(CURRENT_VERSION);
labcomm2014_write_packed32(result->encoder.writer, length);
labcomm2014_write_string(result->encoder.writer, CURRENT_VERSION);
labcomm2014_writer_end(result->encoder.writer,
result->encoder.writer->action_context);
}
}
return &(result->encoder);
}
struct labcomm2014_encoder *labcomm2014_encoder_new(
struct labcomm2014_writer *writer,
struct labcomm2014_error_handler *error,
struct labcomm2014_memory *memory,
struct labcomm2014_scheduler *scheduler)
{
return internal_encoder_new(writer,error,memory,scheduler,LABCOMM2014_TRUE);
}
const struct labcomm2014_sample_ref *labcomm2014_encoder_get_sample_ref(
struct labcomm2014_encoder *encoder,
const struct labcomm2014_signature *signature)
{
return encoder->ref_get(encoder, signature);
}
......@@ -24,16 +24,38 @@
#include <stdarg.h>
#include "labcomm2014_error.h"
static char *description[] = {
#define LABCOMM2014_ERROR(name, description) description ,
#include "labcomm2014_error.h"
#undef LABCOMM2014_ERROR
};
void labcomm2014_error_fatal_global(enum labcomm2014_error error,
char *format,
...)
{
va_list args;
fprintf(stderr, "Fatal error %d\n", error);
fprintf(stderr, "Fatal error %d (%s)\n", error, description[error]);
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
exit(1);
}
void labcomm2014_error_warning(struct labcomm2014_error_handler *e,
enum labcomm2014_error error,
char *format,
...)
{
va_list args;
fprintf(stderr, "Fatal warning %d (%s)\n", error, description[error]);
va_start(args, format);
vprintf(format, args);
vfprintf(stderr, format, args);
va_end(args);
exit(1);
}
......@@ -52,6 +52,9 @@ LABCOMM2014_ERROR(LABCOMM2014_ERROR_ENC_NO_REG_SIGNATURE,
"Encoder has no registration for this signature")
LABCOMM2014_ERROR(LABCOMM2014_ERROR_ENC_BUF_FULL,
"The labcomm2014 buffer is full")
LABCOMM2014_ERROR(LABCOMM2014_ERROR_DEC_NO_REG_SIGNATURE,
"Decoder has no registration for this signature")
LABCOMM2014_ERROR(LABCOMM2014_ERROR_DEC_UNKNOWN_DATATYPE,
"Decoder: Unknown datatype")
LABCOMM2014_ERROR(LABCOMM2014_ERROR_DEC_INDEX_MISMATCH,
......
......@@ -26,9 +26,14 @@
#include <stdarg.h>
#include "labcomm2014_private.h"
#include "labcomm2014_fd_writer.h"
#include "labcomm2014_ioctl.h"
#define BUFFER_SIZE 2048
#ifdef LABCOMM_WITH_SANITY_CHECKS
static int bytecount_carry = 0;
#endif
struct labcomm2014_fd_writer {
struct labcomm2014_writer writer;
struct labcomm2014_writer_action_context action_context;
......@@ -82,6 +87,9 @@ static int fd_start(struct labcomm2014_writer *w,
const struct labcomm2014_signature *signature,
void *value)
{
#ifdef LABCOMM_WITH_SANITY_CHECKS
bytecount_carry = w->pos;
#endif
w->pos = 0;
return w->error;
......@@ -107,18 +115,45 @@ static int fd_flush(struct labcomm2014_writer *w,
} else if (err == 0) {
w->error = -EINVAL;
}
#ifdef LABCOMM_WITH_SANITY_CHECKS
bytecount_carry = w->pos;
#endif
w->pos = 0;
return w->error;
}
#ifdef LABCOMM_WITH_SANITY_CHECKS
static int fd_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 LABCOMM_IOCTL_WRITER_GET_BYTES_WRITTEN: {
int *value = va_arg(arg, int*);
*value = w->pos + bytecount_carry;
result = 0;
} break;
}
return result;
}
#endif
static const struct labcomm2014_writer_action action = {
.alloc = fd_alloc,
.free = fd_free,
.start = fd_start,
.end = fd_flush,
.flush = fd_flush,
#ifdef LABCOMM_WITH_SANITY_CHECKS
.ioctl = fd_ioctl
#else
.ioctl = NULL
#endif
};
struct labcomm2014_writer *labcomm2014_fd_writer_new(struct labcomm2014_memory *memory,
......