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

Oscar Olsson's avatar
Oscar Olsson committed
4
5
6
7
8
9
10
11
12
13
14
#ifdef ARM_CORTEXM3_CODESOURCERY
  #include <machine/endian.h>
#else
  #include <endian.h>
#endif

// Some projects can not use stdio.h.
#ifndef LABCOMM_NO_STDIO
  #include <stdio.h>
#endif

Anders Nilsson's avatar
Anders Nilsson committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#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
 */
42
#define LABCOMM_USER     0x40
Anders Nilsson's avatar
Anders Nilsson committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

/*
 * 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
63
64
  labcomm_error_handler_callback on_error;
  labcomm_handle_new_datatype_callback on_new_datatype;
Anders Nilsson's avatar
Anders Nilsson committed
65
66
67
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
} 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)
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

static inline unsigned int labcomm_unpack32(labcomm_reader_t *r)
{
  unsigned int res=0;
  unsigned char i=0;
  unsigned char cont=1;
  do {
    if (r->pos >= r->count) {	
      r->read(r, labcomm_reader_continue);
    }
#ifdef IDIOTDEBUG
    {
	int k;
	for(k=0; k<=r->pos; k++) printf("%2x\n", r->data[k]);
    }
#endif
    unsigned char c = r->data[r->pos];
    res |= (c & 0x7f) << 7*i;
    cont = c & 0x80;
#ifdef IDIOTDEBUG
    printf("unpack32: %x (%x, %d, %d)\n", res, c, i, cont);
#endif
    i++;
    r->pos++;
  } while(cont);
  return res;
}

static inline unsigned int labcomm_decode_packed32(labcomm_decoder_t *d) 
{
    return labcomm_unpack32(&d->reader);
}

Anders Nilsson's avatar
Anders Nilsson committed
156
157
158
159
160
static inline char *labcomm_read_string(labcomm_reader_t *r)
{
  char *result;
  int length, i; 
  
161
  length = labcomm_unpack32(r);
Anders Nilsson's avatar
Anders Nilsson committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  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);
}

static inline int labcomm_buffer_read(struct labcomm_reader *r, 
				      labcomm_reader_action_t action)
{
  // If this gets called, it is an error, 
  // so note error and let producer proceed
  r->context = r;
  r->pos = 0;
  return 0;
}

static inline int labcomm_buffer_reader_error(struct labcomm_reader *r) 
{
  return r->context != NULL;
} 

static inline void labcomm_buffer_reader_setup(
  labcomm_reader_t *r,
  void *data,
  int length)
{
199
  r->context = NULL; // Used as error flag
Anders Nilsson's avatar
Anders Nilsson committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
  r->data = data;  
  r->data_size = length;
  r->count = length;
  r->pos = 0;
  r->read = labcomm_buffer_read;
}

/*
 * Semi private encoder declarations
 */
typedef void (*labcomm_encode_typecast_t)(
  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,
		      labcomm_encode_typecast_t);
  void (*do_encode)(struct labcomm_encoder *encoder, 
		    labcomm_signature_t *signature, 
		    void *value);
Oscar Olsson's avatar
Oscar Olsson committed
223
  labcomm_error_handler_callback on_error;
Anders Nilsson's avatar
Anders Nilsson committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
} labcomm_encoder_t;

void labcomm_internal_encoder_register(
  labcomm_encoder_t *encoder, 
  labcomm_signature_t *signature, 
  labcomm_encode_typecast_t encode);

void labcomm_internal_encode(
  labcomm_encoder_t *encoder, 
  labcomm_signature_t *signature, 
  void *value);

#if __BYTE_ORDER == __LITTLE_ENDIAN

#define LABCOMM_ENCODE(name, type)					\
  static inline void labcomm_write_##name(labcomm_writer_t *w, type data) { \
    int i;								\
    for (i = sizeof(type) - 1 ; i >= 0 ; i--) {				\
Oscar Olsson's avatar
Oscar Olsson committed
242
      if (w->pos >= w->count) { /*buffer is full*/			\
Anders Nilsson's avatar
Anders Nilsson committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
	w->write(w, labcomm_writer_continue);				\
      }									\
      w->data[w->pos] = ((unsigned char*)(&data))[i];			\
      w->pos++;								\
    }									\
  }									\
  static inline void labcomm_encode_##name(labcomm_encoder_t *e, type data) { \
    labcomm_write_##name(&e->writer, data);				\
  }

#else

#define LABCOMM_ENCODE(name, type)					\
  static inline void labcomm_write_##name(labcomm_writer_t *w, type data) { \
    int i;								\
    for (i = 0 ; i < sizeof(type) ; i++) {				\
      if (w->pos >= w->count) {						\
	w->write(w, labcomm_writer_continue);			\
      }									\
      w->data[w->pos] = ((unsigned char*)(&data))[i];			\
      w->pos++;								\
    }									\
  }									\
  static inline void labcomm_encode_##name(labcomm_encoder_t *e, type data) { \
    labcomm_write_##name(&e->writer, data);				\
  }

#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)
279

280
281
282
283
284
285
286
/* 
 * Pack the 32 bit number data as a sequence of 7 bit chunks, represented in bytes 
 * with the high bit meaning that more data is to come.
 *
 * The chunks are sent "little endian": each 7 bit chunk is more significant than
 * the previous.
 */ 
287
288
static inline void labcomm_pack32(labcomm_writer_t *w, unsigned int data)
{
Sven Robertz's avatar
Sven Robertz committed
289
  unsigned int tmp; 
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

  tmp = data;

  while (tmp >= 0x80) {
    if (w->pos >= w->count) {	
      w->write(w, labcomm_writer_continue);
    }
    w->data[w->pos] = (tmp & 0x7f) | 0x80;
    w->pos++;
    tmp >>= 7;
  } 
  w->data[w->pos] = tmp; 
  w->pos++;
}

static inline void labcomm_encode_packed32(labcomm_encoder_t *e, unsigned int data) 
{ 
    labcomm_pack32(&e->writer, data);				
}

Anders Nilsson's avatar
Anders Nilsson committed
310
311
312
313
314
static inline void labcomm_write_string(labcomm_writer_t *w, char *s)
{
  int length, i; 

  length = strlen((char*)s);
315
  labcomm_pack32(w, length);
Anders Nilsson's avatar
Anders Nilsson committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  for (i = 0 ; i < length ; i++) {
    if (w->pos >= w->count) {	
      w->write(w, labcomm_writer_continue);
    }
    w->data[w->pos] = s[i];
    w->pos++;
  }
}

static inline void labcomm_encode_string(labcomm_encoder_t *e, 
					 char *s)
{
  labcomm_write_string(&e->writer, s);
}

void labcomm_encode_type_index(labcomm_encoder_t *e, labcomm_signature_t *s);

static inline int labcomm_buffer_write(struct labcomm_writer *w, 
Sven Robertz's avatar
Sven Robertz committed
334
                                       labcomm_writer_action_t action, ...)
Anders Nilsson's avatar
Anders Nilsson committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
{
  // If this gets called, it is an error, 
  // so note error and let producer proceed
  w->context = w;
  w->pos = 0;
  return 0;
}


static inline int labcomm_buffer_writer_error(struct labcomm_writer *w) 
{
  return w->context != NULL;
} 


static inline void labcomm_buffer_writer_setup(struct labcomm_writer *w,
                                               void *data,
                                               int length)
{
  w->context = NULL; // Used as error flag
  w->data = data;
  w->data_size = length;
  w->count = length;
  w->pos = 0;
  w->write = labcomm_buffer_write;
}

#endif