labcomm_private.h 10.9 KB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
1
2
3
#ifndef _LABCOMM_PRIVATE_H_
#define _LABCOMM_PRIVATE_H_

4
5
#ifdef LABCOMM_COMPAT
  #include LABCOMM_COMPAT
Oscar Olsson's avatar
Oscar Olsson committed
6
7
8
9
10
#else
  #include <endian.h>
  #include <stdio.h>
#endif

11
#include <stdint.h>
Anders Nilsson's avatar
Anders Nilsson committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdlib.h>
#include <string.h>
#include "labcomm.h"

/*
 * Predeclared aggregate type indices
 */
#define LABCOMM_TYPEDEF  0x01
#define LABCOMM_SAMPLE   0x02
#define LABCOMM_ARRAY    0x10
#define LABCOMM_STRUCT   0x11

/*
 * Predeclared primitive type indices
 */
#define LABCOMM_BOOLEAN  0x20 
#define LABCOMM_BYTE     0x21
#define LABCOMM_SHORT    0x22
#define LABCOMM_INT      0x23
#define LABCOMM_LONG     0x24
#define LABCOMM_FLOAT    0x25
#define LABCOMM_DOUBLE   0x26
#define LABCOMM_STRING   0x27

/*
 * Start index for user defined types
 */
39
#define LABCOMM_USER     0x40
Anders Nilsson's avatar
Anders Nilsson committed
40

41
42
43
44
45
46
/*
 *
 */
#define LABCOMM_DECLARE_SIGNATURE(name) \
  labcomm_signature_t name __attribute__((section("labcomm")))

Anders Nilsson's avatar
Anders Nilsson committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Semi private decoder declarations
 */
typedef void (*labcomm_handler_typecast_t)(void *, void *);

typedef void (*labcomm_decoder_typecast_t)(
  struct labcomm_decoder *,
  labcomm_handler_typecast_t,
  void *);

typedef struct labcomm_decoder {
  void *context;
  labcomm_reader_t reader;
  void (*do_register)(struct labcomm_decoder *, 
		      labcomm_signature_t *, 
		      labcomm_decoder_typecast_t,
		      labcomm_handler_typecast_t,
		      void *context);
  int (*do_decode_one)(struct labcomm_decoder *decoder);
Oscar Olsson's avatar
Oscar Olsson committed
66
67
  labcomm_error_handler_callback on_error;
  labcomm_handle_new_datatype_callback on_new_datatype;
Anders Nilsson's avatar
Anders Nilsson committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
} labcomm_decoder_t;

/*
 * Non typesafe registration function to be called from
 * generated labcomm_decoder_register_* functions.
 */
void labcomm_internal_decoder_register(
  labcomm_decoder_t *, 
  labcomm_signature_t *, 
  labcomm_decoder_typecast_t,
  labcomm_handler_typecast_t,
  void *context);

#if __BYTE_ORDER == __LITTLE_ENDIAN

#define LABCOMM_DECODE(name, type)					\
  static inline type labcomm_read_##name(labcomm_reader_t *r) {		\
    type result; int i;							\
    for (i = sizeof(type) - 1 ; i >= 0 ; i--) {				\
      if (r->pos >= r->count) {						\
	r->read(r, labcomm_reader_continue);				\
      }									\
      ((unsigned char*)(&result))[i] = r->data[r->pos];			\
      r->pos++;								\
    }									\
    return result;							\
  }									\
  static inline type labcomm_decode_##name(labcomm_decoder_t *d) {	\
    return labcomm_read_##name(&d->reader);				\
  }

#else

#define LABCOMM_DECODE(name, type)					\
  static inline type labcomm_read_##name(labcomm_reader_t *r) {		\
    type result; int i;							\
    for (i = 0 ; i < sizeof(type) ; i++) {				\
      if (r->pos >= r->count) {						\
	r->read(r, labcomm_reader_continue);				\
      }									\
      ((unsigned char*)(&result))[i] = r->data[r->pos];			\
      r->pos++;								\
    }									\
    return result;							\
  }									\
  static inline type labcomm_decode_##name(labcomm_decoder_t *d) {	\
    return labcomm_read_##name(&d->reader);				\
  }

#endif

LABCOMM_DECODE(boolean, unsigned char)
LABCOMM_DECODE(byte, unsigned char)
LABCOMM_DECODE(short, short)
LABCOMM_DECODE(int, int)
LABCOMM_DECODE(long, long long)
LABCOMM_DECODE(float, float)
LABCOMM_DECODE(double, double)
126

127
#if 0
128
129
130
131
132
133
134
135
136
137
138
139
/* 
 * Unpack a 32 bit unsigned number from a sequence bytes, where the 
 * first byte is prefixed with a variable length bit pattern that
 * indicates the number of bytes used for encoding. The encoding
 * is inspired by the UTF-8 encoding.
 *
 * 0b0     - 1 byte  (0x00000000 - 0x0000007f)
 * 0b10    - 2 bytes (0x00000080 - 0x00003fff)
 * 0b110   - 3 bytes (0x00004000 - 0x001fffff)
 * 0b1110  - 4 bytes (0x00200000 - 0x0fffffff)
 * 0b11110 - 5 bytes (0x10000000 - 0xffffffff) [4 bits unused]
 */
140
static inline unsigned int labcomm_read_unpacked32(labcomm_reader_t *r)
141
{
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  unsigned int result = 0;
  int n, i;
  unsigned char tag;

  if (r->pos >= r->count) {	
    r->read(r, labcomm_reader_continue);
  }
  tag = r->data[r->pos];
  r->pos++;
  if (tag < 0x80) {
    n = 1;
    result = tag;
  } else if (tag < 0xc0) {
    n = 2;
    result = tag & 0x3f;
  } else if (tag < 0xe0) {
    n = 3;
    result = tag & 0x1f;
  } else if (tag < 0xf0) {
    n = 4;
    result = tag & 0x0f;
  } else {
    n = 5;
  }
  for (i = 1 ; i < n ; i++) {
167
168
169
    if (r->pos >= r->count) {	
      r->read(r, labcomm_reader_continue);
    }
170
    result = (result << 8) | r->data[r->pos];
171
    r->pos++;
172
173
  }
  return result;
174
}
175
#endif
176

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
static inline unsigned int labcomm_read_unpacked32(labcomm_reader_t *r)
{
  unsigned int result = 0;
  
  while (1) {
    unsigned char tmp;

    if (r->pos >= r->count) {	
      r->read(r, labcomm_reader_continue);
    }
    tmp = r->data[r->pos];
    r->pos++;
    result = (result << 7) | (tmp & 0x7f);
    if ((tmp & 0x80) == 0) { 
      break; 
    }
  }
  return result;
}
 
197
198
static inline unsigned int labcomm_decode_packed32(labcomm_decoder_t *d) 
{
199
  return labcomm_read_unpacked32(&d->reader);
200
201
}

Anders Nilsson's avatar
Anders Nilsson committed
202
203
204
205
206
static inline char *labcomm_read_string(labcomm_reader_t *r)
{
  char *result;
  int length, i; 
  
207
  length = labcomm_read_unpacked32(r);
Anders Nilsson's avatar
Anders Nilsson committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  result = malloc(length + 1);
  for (i = 0 ; i < length ; i++) {
    if (r->pos >= r->count) {	
      r->read(r, labcomm_reader_continue);
    }
    result[i] = r->data[r->pos];
    r->pos++;
  }
  result[length] = 0;
  return result;
}

static inline char *labcomm_decode_string(labcomm_decoder_t *d)
{
  return labcomm_read_string(&d->reader);
}

/*
 * Semi private encoder declarations
 */
228
typedef int (*labcomm_encoder_function)(
Anders Nilsson's avatar
Anders Nilsson committed
229
230
231
232
233
234
235
236
  struct labcomm_encoder *,
  void *value);

typedef struct labcomm_encoder {
  void *context;
  labcomm_writer_t writer;
  void (*do_register)(struct labcomm_encoder *encoder, 
		      labcomm_signature_t *signature,
237
238
		      labcomm_encoder_function encode);
  int (*do_encode)(struct labcomm_encoder *encoder, 
Anders Nilsson's avatar
Anders Nilsson committed
239
		    labcomm_signature_t *signature, 
240
		    labcomm_encoder_function encode,
Anders Nilsson's avatar
Anders Nilsson committed
241
		    void *value);
Oscar Olsson's avatar
Oscar Olsson committed
242
  labcomm_error_handler_callback on_error;
Anders Nilsson's avatar
Anders Nilsson committed
243
244
245
246
247
} labcomm_encoder_t;

void labcomm_internal_encoder_register(
  labcomm_encoder_t *encoder, 
  labcomm_signature_t *signature, 
248
  labcomm_encoder_function encode);
Anders Nilsson's avatar
Anders Nilsson committed
249

250
int labcomm_internal_encode(
Anders Nilsson's avatar
Anders Nilsson committed
251
252
  labcomm_encoder_t *encoder, 
  labcomm_signature_t *signature, 
253
  labcomm_encoder_function encode,
Anders Nilsson's avatar
Anders Nilsson committed
254
255
256
257
258
  void *value);

#if __BYTE_ORDER == __LITTLE_ENDIAN

#define LABCOMM_ENCODE(name, type)					\
259
  static inline int labcomm_write_##name(labcomm_writer_t *w, type data) { \
Anders Nilsson's avatar
Anders Nilsson committed
260
261
    int i;								\
    for (i = sizeof(type) - 1 ; i >= 0 ; i--) {				\
Oscar Olsson's avatar
Oscar Olsson committed
262
      if (w->pos >= w->count) { /*buffer is full*/			\
263
264
265
        int err;							\
	err = w->write(w, labcomm_writer_continue);			\
	if (err != 0) { return err; }					\
Anders Nilsson's avatar
Anders Nilsson committed
266
267
268
269
      }									\
      w->data[w->pos] = ((unsigned char*)(&data))[i];			\
      w->pos++;								\
    }									\
270
    return 0;								\
Anders Nilsson's avatar
Anders Nilsson committed
271
  }									\
272
  static inline int labcomm_encode_##name(labcomm_encoder_t *e, type data) { \
273
    return labcomm_write_##name(&e->writer, data);			\
Anders Nilsson's avatar
Anders Nilsson committed
274
275
276
277
278
  }

#else

#define LABCOMM_ENCODE(name, type)					\
279
  static inline int labcomm_write_##name(labcomm_writer_t *w, type data) { \
Anders Nilsson's avatar
Anders Nilsson committed
280
281
282
    int i;								\
    for (i = 0 ; i < sizeof(type) ; i++) {				\
      if (w->pos >= w->count) {						\
283
284
285
        int err;							\
	err = w->write(w, labcomm_writer_continue);			\
	if (err != 0) { return err; }					\
Anders Nilsson's avatar
Anders Nilsson committed
286
287
288
289
      }									\
      w->data[w->pos] = ((unsigned char*)(&data))[i];			\
      w->pos++;								\
    }									\
290
    return 0;								\
Anders Nilsson's avatar
Anders Nilsson committed
291
  }									\
292
  static inline int labcomm_encode_##name(labcomm_encoder_t *e, type data) { \
293
    return labcomm_write_##name(&e->writer, data);			\
Anders Nilsson's avatar
Anders Nilsson committed
294
295
296
297
298
299
300
301
302
303
304
  }

#endif

LABCOMM_ENCODE(boolean, unsigned char)
LABCOMM_ENCODE(byte, unsigned char)
LABCOMM_ENCODE(short, short)
LABCOMM_ENCODE(int, int)
LABCOMM_ENCODE(long, long long)
LABCOMM_ENCODE(float, float)
LABCOMM_ENCODE(double, double)
305

306
#if 0
307
/* 
308
309
310
311
 * Pack the 32 bit unsigned number data as a sequence bytes, where the 
 * first byte is prefixed with a variable length bit pattern that
 * indicates the number of bytes used for encoding. The encoding
 * is inspired by the UTF-8 encoding.
312
 *
313
314
315
316
317
318
 * 0b0     - 1 byte  (0x00000000 - 0x0000007f)
 * 0b10    - 2 bytes (0x00000080 - 0x00003fff)
 * 0b110   - 3 bytes (0x00004000 - 0x001fffff)
 * 0b1110  - 4 bytes (0x00200000 - 0x0fffffff)
 * 0b11110 - 5 bytes (0x10000000 - 0xffffffff) [4 bits unused]
 */
319
static inline int labcomm_write_packed32(labcomm_writer_t *w, 
320
					 unsigned int data)
321
{
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  int n;
  unsigned char tag;
  unsigned char tmp[4] = { (data >> 24) & 0xff, 
			   (data >> 16) & 0xff, 
			   (data >>  8) & 0xff, 
			   (data      ) & 0xff }; 
  if (data < 0x80) {
    n = 1;
    tag = 0x00;
  } else if (data < 0x4000) { 
    n = 2;
    tag = 0x80;
  } else if (data < 0x200000) { 
    n = 3;
    tag = 0xc0;
  } else if (data < 0x10000000) { 
    n = 4;
    tag = 0xe0;
  } else  {
    n = 5;
    tag = 0xf0;
  }
344
345
  /* TODO: maybe?
    if (w->pos + n - 1 >= w->count) {	
346
    w->write(w, labcomm_writer_continue, n);
347
348
    }
  */
349
  switch (n) {
350
    case 5: { 
351
352
353
354
      if (w->pos >= w->count) {	
	int err;
	err = w->write(w, labcomm_writer_continue);
	if (err != 0) { return err; }
355
356
357
358
359
      }
      w->data[w->pos++] = tag; tag = 0;
    }
    case 4: { 
      if (w->pos >= w->count) {					
360
361
362
	int err;
	err = w->write(w, labcomm_writer_continue);
	if (err != 0) { return err; }
363
364
365
366
367
      }
      w->data[w->pos++] = tmp[0] | tag; tag = 0;
    }
    case 3: { 
      if (w->pos >= w->count) {					
368
369
370
	int err;
	err = w->write(w, labcomm_writer_continue);
	if (err != 0) { return err; }
371
372
373
374
375
      }
      w->data[w->pos++] = tmp[1] | tag; tag = 0;
    }
    case 2: { 
      if (w->pos >= w->count) {					
376
377
378
	int err;
	err = w->write(w, labcomm_writer_continue);
	if (err != 0) { return err; }
379
380
381
382
383
      }
      w->data[w->pos++] = tmp[2] | tag; tag = 0;
    }
    case 1: { 
      if (w->pos >= w->count) {					
384
385
386
	int err;
	err = w->write(w, labcomm_writer_continue);
	if (err != 0) { return err; }
387
388
389
      }
      w->data[w->pos++] = tmp[3] | tag;
    }
390
  }
391
  return 0;
392
}
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#endif

static inline int labcomm_write_packed32(labcomm_writer_t *w, 
					 unsigned int data)
{
  unsigned char tmp[5];
  int i;
  
  for (i = 0 ; i == 0 || data ; i++, data = (data >> 7)) {
    tmp[i] = data & 0x7f;
  }
  for (i = i - 1 ; i >= 0 ; i--) {
    if (w->pos >= w->count) {					
      int err;
      err = w->write(w, labcomm_writer_continue);
      if (err != 0) { return err; }
    }
    w->data[w->pos++] = tmp[i] | (i?0x80:0x00);
  }
  return 0;
}

415

416
417
static inline int labcomm_encode_packed32(labcomm_encoder_t *e, 
					   unsigned int data) 
418
{ 
419
  return labcomm_write_packed32(&e->writer, data);				
420
421
}

422

423
static inline int labcomm_write_string(labcomm_writer_t *w, char *s)
Anders Nilsson's avatar
Anders Nilsson committed
424
{
425
  int length, i, err; 
Anders Nilsson's avatar
Anders Nilsson committed
426
427

  length = strlen((char*)s);
428
429
  err = labcomm_write_packed32(w, length);
  if (err != 0) { return err; }
Anders Nilsson's avatar
Anders Nilsson committed
430
431
  for (i = 0 ; i < length ; i++) {
    if (w->pos >= w->count) {	
432
433
434
      int err;
      err = w->write(w, labcomm_writer_continue);
      if (err != 0) { return err; }
Anders Nilsson's avatar
Anders Nilsson committed
435
436
437
438
    }
    w->data[w->pos] = s[i];
    w->pos++;
  }
439
  return 0;
Anders Nilsson's avatar
Anders Nilsson committed
440
441
}

442
static inline int labcomm_encode_string(labcomm_encoder_t *e, 
Anders Nilsson's avatar
Anders Nilsson committed
443
444
					 char *s)
{
445
  return labcomm_write_string(&e->writer, s);
Anders Nilsson's avatar
Anders Nilsson committed
446
447
448
}

#endif