labcomm_private.h 15.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
  labcomm_private.h -- semi private declarations for handling encoding and 
                       decoding of labcomm samples.

  Copyright 2006-2013 Anders Blomdell <anders.blomdell@control.lth.se>

  This file is part of LabComm.

  LabComm is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  LabComm is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

Anders Nilsson's avatar
Anders Nilsson committed
23
24
25
#ifndef _LABCOMM_PRIVATE_H_
#define _LABCOMM_PRIVATE_H_

26
27
#ifdef LABCOMM_COMPAT
  #include LABCOMM_COMPAT
Oscar Olsson's avatar
Oscar Olsson committed
28
29
30
31
32
#else
  #include <endian.h>
  #include <stdio.h>
#endif

33
#include <stdint.h>
34
//#include <stdlib.h>
Anders Nilsson's avatar
Anders Nilsson committed
35
#include <string.h>
36
#include <unistd.h>
Anders Nilsson's avatar
Anders Nilsson committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#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
 */
62
#define LABCOMM_USER     0x40
Anders Nilsson's avatar
Anders Nilsson committed
63

64
65
66
67
/*
 *
 */
#define LABCOMM_DECLARE_SIGNATURE(name) \
68
  struct labcomm_signature __attribute__((section("labcomm"),aligned(1))) name 
69

70
71
72
/*
 * Semi private lock declarations
 */
73
74
struct labcomm_lock;

75
struct labcomm_lock_action {
76
77
78
79
80
  int (*free)(struct labcomm_lock *lock);
  int (*acquire)(struct labcomm_lock *lock);
  int (*release)(struct labcomm_lock *lock);
  int (*wait)(struct labcomm_lock *lock, useconds_t usec);
  int (*notify)(struct labcomm_lock *lock);
81
82
};

83
struct labcomm_lock {
84
  const struct labcomm_lock_action *action;
85
86
87
88
89
90
91
92
93
94
95
96
97
  struct labcomm_memory *memory;
  void *context;
};

/*
 * Semi private dynamic memory declarations
 */

struct labcomm_memory {
  void *(*alloc)(struct labcomm_memory *m, int lifetime, size_t size);
  void *(*realloc)(struct labcomm_memory *m, int lifetime, 
		   void *ptr, size_t size);
  void (*free)(struct labcomm_memory *m, int lifetime, void *ptr);
98
  void *context;
99
100
};

Anders Nilsson's avatar
Anders Nilsson committed
101
102
103
/*
 * Semi private decoder declarations
 */
104
typedef void (*labcomm_handler_function)(void *value, void *context);
Anders Nilsson's avatar
Anders Nilsson committed
105

106
typedef void (*labcomm_decoder_function)(
107
  struct labcomm_reader *r,
108
109
  labcomm_handler_function handler,
  void *context);
Anders Nilsson's avatar
Anders Nilsson committed
110

111
112
struct labcomm_reader_action_context;

113
struct labcomm_reader_action {
114
115
116
117
118
119
120
121
122
  /* 'alloc' is called at the first invocation of 'labcomm_decoder_decode_one' 
     on the decoder containing the reader. If 'labcomm_version' != NULL
     and non-empty the transport layer may use it to ensure that
     compatible versions are used.

     Returned value:
       >  0    Number of bytes allocated for buffering
       <= 0    Error
  */
123
124
  int (*alloc)(struct labcomm_reader *r, 
	       struct labcomm_reader_action_context *action_context, 
125
	       struct labcomm_decoder *decoder, char *labcomm_version);
126
127
128
129
130
131
132
  /* 'free' returns the resources claimed by 'alloc' and might have other
     reader specific side-effects as well.

     Returned value:
       == 0    Success
       != 0    Error
  */
133
134
  int (*free)(struct labcomm_reader *r, 
	      struct labcomm_reader_action_context *action_context);
135
136
137
  /* 'start' is called right after a sample has arrived. In the case of 
     a sample or typedef, 'value' == NULL.
   */
138
  int (*start)(struct labcomm_reader *r, 
139
140
141
	       struct labcomm_reader_action_context *action_context,
	       int index, struct labcomm_signature *signature,
	       void *value);
142
143
144
145
146
147
  int (*end)(struct labcomm_reader *r, 
	     struct labcomm_reader_action_context *action_context);
  int (*fill)(struct labcomm_reader *r, 
	      struct labcomm_reader_action_context *action_context);
  int (*ioctl)(struct labcomm_reader *r, 
	       struct labcomm_reader_action_context *action_context,
148
	       int index, struct labcomm_signature *signature, 
149
	       uint32_t ioctl_action, va_list args);
150
151
};

152
153
struct labcomm_reader_action_context {
  struct labcomm_reader_action_context *next;
154
  const struct labcomm_reader_action *action;
155
156
157
158
159
  void *context;  
};

struct labcomm_reader {
  struct labcomm_reader_action_context *action_context;
160
161
162
  struct labcomm_memory *memory;
  /* The following fields are initialized by labcomm_decoder_new */
  struct labcomm_decoder *decoder;
163
164
165
166
167
168
169
  unsigned char *data;
  int data_size;
  int count;
  int pos;
  int error;
};

170
171
172
173
174
175
176
int labcomm_reader_alloc(struct labcomm_reader *r, 
			 struct labcomm_reader_action_context *action_context, 
			 struct labcomm_decoder *decoder, 
			 char *labcomm_version);
int labcomm_reader_free(struct labcomm_reader *r, 
			struct labcomm_reader_action_context *action_context);
int labcomm_reader_start(struct labcomm_reader *r, 
177
178
179
			 struct labcomm_reader_action_context *action_context,
			 int index, struct labcomm_signature *signature,
			 void *value);
180
181
182
183
184
185
int labcomm_reader_end(struct labcomm_reader *r, 
		       struct labcomm_reader_action_context *action_context);
int labcomm_reader_fill(struct labcomm_reader *r, 
			struct labcomm_reader_action_context *action_context);
int labcomm_reader_ioctl(struct labcomm_reader *r, 
			 struct labcomm_reader_action_context *action_context,
186
			 int index, struct labcomm_signature *signature, 
187
			 uint32_t ioctl_action, va_list args);
188

Anders Nilsson's avatar
Anders Nilsson committed
189
190
191
192
/*
 * Non typesafe registration function to be called from
 * generated labcomm_decoder_register_* functions.
 */
193
int labcomm_internal_decoder_register(
194
195
196
197
  struct labcomm_decoder *d, 
  struct labcomm_signature *s, 
  labcomm_decoder_function decoder,
  labcomm_handler_function handler,
Anders Nilsson's avatar
Anders Nilsson committed
198
199
  void *context);

200
int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder, 
201
				   struct labcomm_signature *signature,
202
				   uint32_t ioctl_action, va_list args);
203

Anders Nilsson's avatar
Anders Nilsson committed
204
205
206
#if __BYTE_ORDER == __LITTLE_ENDIAN

#define LABCOMM_DECODE(name, type)					\
207
  static inline type labcomm_read_##name(struct labcomm_reader *r) {	\
Anders Nilsson's avatar
Anders Nilsson committed
208
209
210
    type result; int i;							\
    for (i = sizeof(type) - 1 ; i >= 0 ; i--) {				\
      if (r->pos >= r->count) {						\
211
	labcomm_reader_fill(r, r->action_context);			\
Anders Nilsson's avatar
Anders Nilsson committed
212
213
214
215
216
217
218
219
220
221
      }									\
      ((unsigned char*)(&result))[i] = r->data[r->pos];			\
      r->pos++;								\
    }									\
    return result;							\
  }

#else

#define LABCOMM_DECODE(name, type)					\
222
  static inline type labcomm_read_##name(struct labcomm_reader *r) {	\
Anders Nilsson's avatar
Anders Nilsson committed
223
224
225
    type result; int i;							\
    for (i = 0 ; i < sizeof(type) ; i++) {				\
      if (r->pos >= r->count) {						\
226
	labcomm_reader_fille(r, r->action_context);			\
Anders Nilsson's avatar
Anders Nilsson committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
      }									\
      ((unsigned char*)(&result))[i] = r->data[r->pos];			\
      r->pos++;								\
    }									\
    return result;							\
  }

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

244
static inline unsigned int labcomm_read_packed32(struct labcomm_reader *r)
245
246
247
248
249
250
251
{
  unsigned int result = 0;
  
  while (1) {
    unsigned char tmp;

    if (r->pos >= r->count) {	
252
      labcomm_reader_fill(r, r->action_context);
Anders Blomdell's avatar
Anders Blomdell committed
253
254
255
      if (r->error != 0) {
	goto out;
      }
256
257
258
259
260
261
262
263
    }
    tmp = r->data[r->pos];
    r->pos++;
    result = (result << 7) | (tmp & 0x7f);
    if ((tmp & 0x80) == 0) { 
      break; 
    }
  }
Anders Blomdell's avatar
Anders Blomdell committed
264
out:
265
266
267
  return result;
}
 
268
static inline char *labcomm_read_string(struct labcomm_reader *r)
Anders Nilsson's avatar
Anders Nilsson committed
269
270
271
272
{
  char *result;
  int length, i; 
  
273
  length = labcomm_read_packed32(r);
274
  result = labcomm_memory_alloc(r->memory, 1, length + 1);
Anders Nilsson's avatar
Anders Nilsson committed
275
276
  for (i = 0 ; i < length ; i++) {
    if (r->pos >= r->count) {	
277
      labcomm_reader_fill(r, r->action_context);
Anders Nilsson's avatar
Anders Nilsson committed
278
279
280
281
282
283
284
285
286
287
288
    }
    result[i] = r->data[r->pos];
    r->pos++;
  }
  result[length] = 0;
  return result;
}

/*
 * Semi private encoder declarations
 */
289
typedef int (*labcomm_encoder_function)(
290
  struct labcomm_writer *,
Anders Nilsson's avatar
Anders Nilsson committed
291
  void *value);
292
293
294
295
296
typedef int (*labcomm_encoder_enqueue)(
  struct labcomm_encoder *encoder, 
  void (*action)(struct labcomm_encoder *encoder, 
		 void *context),
  void *context);
297
struct labcomm_writer_action_context;
298
299

struct labcomm_writer_action {
300
301
  int (*alloc)(struct labcomm_writer *w, 
	       struct labcomm_writer_action_context *action_context, 
302
303
	       struct labcomm_encoder *encoder, char *labcomm_version,
	       labcomm_encoder_enqueue enqueue);
304
305
  int (*free)(struct labcomm_writer *w, 
	      struct labcomm_writer_action_context *action_context);
306
307
308
309
310
311
312
313
314
  /* 'start' is called right before a sample is to be sent. In the 
     case of a sample or typedef, 'value' == NULL.

     Returned value:
       == 0          Success -> continue sending the sample
       == -EALREADY  Success -> silently skip sending the sample,
                                'end' will not be called
       < 0           Error
   */
315
316
  int (*start)(struct labcomm_writer *w, 
	       struct labcomm_writer_action_context *action_context,
317
318
	       int index, struct labcomm_signature *signature,
	       void *value);
319
320
321
322
323
324
  int (*end)(struct labcomm_writer *w, 
	     struct labcomm_writer_action_context *action_context);
  int (*flush)(struct labcomm_writer *w, 
	       struct labcomm_writer_action_context *action_context); 
  int (*ioctl)(struct labcomm_writer *w, 
	       struct labcomm_writer_action_context *action_context, 
325
	       int index, struct labcomm_signature *signature, 
326
	       uint32_t ioctl_action, va_list args);
327
328
};

329
330
struct labcomm_writer_action_context {
  struct labcomm_writer_action_context *next;
331
  const struct labcomm_writer_action *action;
332
333
334
335
336
  void *context;  
};

struct labcomm_writer {
  struct labcomm_writer_action_context *action_context;
337
338
339
  struct labcomm_memory *memory;
  /* The following fields are initialized by labcomm_encoder_new */
  struct labcomm_encoder *encoder;
340
341
342
343
344
345
346
  unsigned char *data;
  int data_size;
  int count;
  int pos;
  int error;
};

347
348
349
int labcomm_writer_alloc(struct labcomm_writer *w, 
			 struct labcomm_writer_action_context *action_context, 
			 struct labcomm_encoder *encoder, 
350
351
			 char *labcomm_version,
			 labcomm_encoder_enqueue enqueue);
352
353
354
355
356
357
358
359
360
361
362
363
int labcomm_writer_free(struct labcomm_writer *w, 
			struct labcomm_writer_action_context *action_context);
int labcomm_writer_start(struct labcomm_writer *w, 
			 struct labcomm_writer_action_context *action_context,
			 int index, struct labcomm_signature *signature,
			 void *value);
int labcomm_writer_end(struct labcomm_writer *w, 
		       struct labcomm_writer_action_context *action_context);
int labcomm_writer_flush(struct labcomm_writer *w, 
			 struct labcomm_writer_action_context *action_context); 
int labcomm_writer_ioctl(struct labcomm_writer *w, 
			 struct labcomm_writer_action_context *action_context, 
364
			 int index, struct labcomm_signature *signature, 
365
366
			 uint32_t ioctl_action, va_list args);

367
int labcomm_internal_encoder_register(
368
369
  struct labcomm_encoder *encoder, 
  struct labcomm_signature *signature, 
370
  labcomm_encoder_function encode);
Anders Nilsson's avatar
Anders Nilsson committed
371

372
int labcomm_internal_encode(
373
374
  struct labcomm_encoder *encoder, 
  struct labcomm_signature *signature, 
375
  labcomm_encoder_function encode,
Anders Nilsson's avatar
Anders Nilsson committed
376
377
  void *value);

378
int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder, 
379
				   struct labcomm_signature *signature,
380
				   uint32_t ioctl_action, va_list args);
381

Anders Nilsson's avatar
Anders Nilsson committed
382
383
384
#if __BYTE_ORDER == __LITTLE_ENDIAN

#define LABCOMM_ENCODE(name, type)					\
385
  static inline int labcomm_write_##name(struct labcomm_writer *w, type data) { \
Anders Nilsson's avatar
Anders Nilsson committed
386
387
    int i;								\
    for (i = sizeof(type) - 1 ; i >= 0 ; i--) {				\
Oscar Olsson's avatar
Oscar Olsson committed
388
      if (w->pos >= w->count) { /*buffer is full*/			\
389
        int err;							\
390
	err = labcomm_writer_flush(w, w->action_context);		\
391
	if (err != 0) { return err; }					\
Anders Nilsson's avatar
Anders Nilsson committed
392
393
394
395
      }									\
      w->data[w->pos] = ((unsigned char*)(&data))[i];			\
      w->pos++;								\
    }									\
396
    return 0;								\
Anders Nilsson's avatar
Anders Nilsson committed
397
398
399
400
401
  }

#else

#define LABCOMM_ENCODE(name, type)					\
402
  static inline int labcomm_write_##name(struct labcomm_writer *w, type data) { \
Anders Nilsson's avatar
Anders Nilsson committed
403
404
405
    int i;								\
    for (i = 0 ; i < sizeof(type) ; i++) {				\
      if (w->pos >= w->count) {						\
406
        int err;							\
407
	err = labcomm_writer_flush(w, w->action_context);		\
408
	if (err != 0) { return err; }					\
Anders Nilsson's avatar
Anders Nilsson committed
409
410
411
412
      }									\
      w->data[w->pos] = ((unsigned char*)(&data))[i];			\
      w->pos++;								\
    }									\
413
    return 0;								\
Anders Nilsson's avatar
Anders Nilsson committed
414
415
416
417
418
419
420
421
422
423
424
  }

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

426
static inline int labcomm_write_packed32(struct labcomm_writer *w, 
427
428
429
430
431
432
433
434
435
436
437
					 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;
438
      err = labcomm_writer_flush(w, w->action_context);	
439
440
441
442
443
444
445
      if (err != 0) { return err; }
    }
    w->data[w->pos++] = tmp[i] | (i?0x80:0x00);
  }
  return 0;
}

446
static inline int labcomm_write_string(struct labcomm_writer *w, char *s)
Anders Nilsson's avatar
Anders Nilsson committed
447
{
448
  int length, i, err; 
Anders Nilsson's avatar
Anders Nilsson committed
449
450

  length = strlen((char*)s);
451
452
  err = labcomm_write_packed32(w, length);
  if (err != 0) { return err; }
Anders Nilsson's avatar
Anders Nilsson committed
453
454
  for (i = 0 ; i < length ; i++) {
    if (w->pos >= w->count) {	
455
      int err;
456
      err = labcomm_writer_flush(w, w->action_context);	
457
      if (err != 0) { return err; }
Anders Nilsson's avatar
Anders Nilsson committed
458
459
460
461
    }
    w->data[w->pos] = s[i];
    w->pos++;
  }
462
  return 0;
Anders Nilsson's avatar
Anders Nilsson committed
463
464
}

465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 * Macros for handling arrays indexed by signature index
 */

#define LABCOMM_SIGNATURE_ARRAY_DEF(name, kind)	\
  struct {					\
    int first;					\
    int last;					\
    kind *data;					\
  } name

#define LABCOMM_SIGNATURE_ARRAY_DEF_INIT(name, kind)		\
  LABCOMM_SIGNATURE_ARRAY_DEF(name, kind) = { 0, 0, NULL }

#define LABCOMM_SIGNATURE_ARRAY_INIT(name, kind)		\
  name.first = 0; name.last = 0; name.data = NULL;		\
  name.data = (kind *)name.data; /* typechecking no-op */

483
484
#define LABCOMM_SIGNATURE_ARRAY_FREE(memory, name, kind)	\
  if (name.data) { labcomm_memory_free(memory, 0, name.data); }	\
485
486
  name.data = (kind *)NULL; /* typechecking */

487
488
489
490
491
492
493
494
495
496
497
void *labcomm_signature_array_ref(struct labcomm_memory * memory,
				  int *first, int *last, void **data,
				  int size, int index);
/*
 * NB: the pointer returned by LABCOMM_SIGNATURE_ARRAY_REF might be
 *     rendered invalid by a subsequent call to LABCOMM_SIGNATURE_ARRAY_REF
 *     on the same SIGNATURE_ARRAY, so make sure not to use the result if 
 *     any other code might have made a call to LABCOMM_SIGNATURE_ARRAY_REF
 *     on the same SIGNATURE_ARRAY.
 */
#define LABCOMM_SIGNATURE_ARRAY_REF(memory, name, kind, index)		\
498
  (name.data = (kind *)name.data, /* typechecking no-op */		\
499
500
   (kind *)(labcomm_signature_array_ref(memory,				\
					&name.first, &name.last,	\
501
502
503
					(void **)&name.data,		\
					sizeof(kind), index)))

504
505
506
#define LABCOMM_SIGNATURE_ARRAY_FOREACH(name, kind, var)		\
  for (name.data = (kind *)name.data, /* typechecking no-op */		\
       var = name.first ; var < name.last ; var++)
507

Anders Nilsson's avatar
Anders Nilsson committed
508
#endif