labcomm_private.h 8.97 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
/*
 *
 */
#define LABCOMM_DECLARE_SIGNATURE(name) \
45
  struct labcomm_signature __attribute__((section("labcomm"),aligned(1))) name 
46

47
48
49
/*
 * Semi private lock declarations
 */
50
51
52
53
54
55
56
57
58
struct labcomm_lock_action {
  int (*alloc)(void *context);
  int (*free)(void *context);
  int (*read_lock)(void *context);
  int (*read_unlock)(void *context);
  int (*write_lock)(void *context);
  int (*write_unlock)(void *context);
};

59
struct labcomm_lock {
60
  const struct labcomm_lock_action action;
61
62
};

Anders Nilsson's avatar
Anders Nilsson committed
63
64
65
/*
 * Semi private decoder declarations
 */
66
typedef void (*labcomm_handler_function)(void *value, void *context);
Anders Nilsson's avatar
Anders Nilsson committed
67

68
typedef void (*labcomm_decoder_function)(
69
  struct labcomm_reader *r,
70
71
  labcomm_handler_function handler,
  void *context);
Anders Nilsson's avatar
Anders Nilsson committed
72

73
struct labcomm_reader_action {
74
75
  int (*alloc)(struct labcomm_reader *r, void *context, 
	       struct labcomm_decoder *decoder, char *labcomm_version);
76
77
78
79
80
  int (*free)(struct labcomm_reader *r, void *context);
  int (*start)(struct labcomm_reader *r, void *context);
  int (*end)(struct labcomm_reader *r, void *context);
  int (*fill)(struct labcomm_reader *r, void *context); 
  int (*ioctl)(struct labcomm_reader *r, void *context,
81
82
	       int signature_index, struct labcomm_signature *signature, 
	       int ioctl_action, va_list args);
83
84
85
86
87
88
89
90
91
92
93
94
95
96
};

struct labcomm_reader {
  const struct labcomm_reader_action *action;
  void *context;
  unsigned char *data;
  int data_size;
  int count;
  int pos;
  int error;
};



Anders Nilsson's avatar
Anders Nilsson committed
97
98
99
100
101
/*
 * Non typesafe registration function to be called from
 * generated labcomm_decoder_register_* functions.
 */
void labcomm_internal_decoder_register(
102
103
104
105
  struct labcomm_decoder *d, 
  struct labcomm_signature *s, 
  labcomm_decoder_function decoder,
  labcomm_handler_function handler,
Anders Nilsson's avatar
Anders Nilsson committed
106
107
  void *context);

108
int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder, 
109
				   struct labcomm_signature *signature,
110
				   int ioctl_action, va_list args);
111

Anders Nilsson's avatar
Anders Nilsson committed
112
113
114
#if __BYTE_ORDER == __LITTLE_ENDIAN

#define LABCOMM_DECODE(name, type)					\
115
  static inline type labcomm_read_##name(struct labcomm_reader *r) {	\
Anders Nilsson's avatar
Anders Nilsson committed
116
117
118
    type result; int i;							\
    for (i = sizeof(type) - 1 ; i >= 0 ; i--) {				\
      if (r->pos >= r->count) {						\
119
	r->action->fill(r, r->context);					\
Anders Nilsson's avatar
Anders Nilsson committed
120
121
122
123
124
125
126
127
128
129
      }									\
      ((unsigned char*)(&result))[i] = r->data[r->pos];			\
      r->pos++;								\
    }									\
    return result;							\
  }

#else

#define LABCOMM_DECODE(name, type)					\
130
  static inline type labcomm_read_##name(struct labcomm_reader *r) {	\
Anders Nilsson's avatar
Anders Nilsson committed
131
132
133
    type result; int i;							\
    for (i = 0 ; i < sizeof(type) ; i++) {				\
      if (r->pos >= r->count) {						\
134
	r->action->fill(r, r->context);					\
Anders Nilsson's avatar
Anders Nilsson committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
      }									\
      ((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)
151

152
static inline unsigned int labcomm_read_packed32(struct labcomm_reader *r)
153
154
155
156
157
158
159
{
  unsigned int result = 0;
  
  while (1) {
    unsigned char tmp;

    if (r->pos >= r->count) {	
160
      r->action->fill(r, r->context);
Anders Blomdell's avatar
Anders Blomdell committed
161
162
163
      if (r->error != 0) {
	goto out;
      }
164
165
166
167
168
169
170
171
    }
    tmp = r->data[r->pos];
    r->pos++;
    result = (result << 7) | (tmp & 0x7f);
    if ((tmp & 0x80) == 0) { 
      break; 
    }
  }
Anders Blomdell's avatar
Anders Blomdell committed
172
out:
173
174
175
  return result;
}
 
176
static inline char *labcomm_read_string(struct labcomm_reader *r)
Anders Nilsson's avatar
Anders Nilsson committed
177
178
179
180
{
  char *result;
  int length, i; 
  
181
  length = labcomm_read_packed32(r);
Anders Nilsson's avatar
Anders Nilsson committed
182
183
184
  result = malloc(length + 1);
  for (i = 0 ; i < length ; i++) {
    if (r->pos >= r->count) {	
185
      r->action->fill(r, r->context);
Anders Nilsson's avatar
Anders Nilsson committed
186
187
188
189
190
191
192
193
194
195
196
    }
    result[i] = r->data[r->pos];
    r->pos++;
  }
  result[length] = 0;
  return result;
}

/*
 * Semi private encoder declarations
 */
197
typedef int (*labcomm_encoder_function)(
198
  struct labcomm_writer *,
Anders Nilsson's avatar
Anders Nilsson committed
199
200
  void *value);

201
202
203
struct labcomm_writer;

struct labcomm_writer_action {
204
205
  int (*alloc)(struct labcomm_writer *w, void *context, 
	       struct labcomm_encoder *encoder, char *labcomm_version);
206
207
  int (*free)(struct labcomm_writer *w, void *context);
  int (*start)(struct labcomm_writer *w, void *context,
208
209
210
	       struct labcomm_encoder *encoder,
	       int index, struct labcomm_signature *signature,
	       void *value);
211
212
213
  int (*end)(struct labcomm_writer *w, void *context);
  int (*flush)(struct labcomm_writer *w, void *context); 
  int (*ioctl)(struct labcomm_writer *w, void *context, 
214
215
	       int signature_index, struct labcomm_signature *signature, 
	       int ioctl_action, va_list args);
216
217
218
219
220
221
222
223
224
225
226
227
};

struct labcomm_writer {
  const struct labcomm_writer_action *action;
  void *context;
  unsigned char *data;
  int data_size;
  int count;
  int pos;
  int error;
};

Anders Nilsson's avatar
Anders Nilsson committed
228
void labcomm_internal_encoder_register(
229
230
  struct labcomm_encoder *encoder, 
  struct labcomm_signature *signature, 
231
  labcomm_encoder_function encode);
Anders Nilsson's avatar
Anders Nilsson committed
232

233
int labcomm_internal_encode(
234
235
  struct labcomm_encoder *encoder, 
  struct labcomm_signature *signature, 
236
  labcomm_encoder_function encode,
Anders Nilsson's avatar
Anders Nilsson committed
237
238
  void *value);

239

240
int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder, 
241
				   struct labcomm_signature *signature,
242
				   int ioctl_action, va_list args);
243

Anders Nilsson's avatar
Anders Nilsson committed
244
245
246
#if __BYTE_ORDER == __LITTLE_ENDIAN

#define LABCOMM_ENCODE(name, type)					\
247
  static inline int labcomm_write_##name(struct labcomm_writer *w, type data) { \
Anders Nilsson's avatar
Anders Nilsson committed
248
249
    int i;								\
    for (i = sizeof(type) - 1 ; i >= 0 ; i--) {				\
Oscar Olsson's avatar
Oscar Olsson committed
250
      if (w->pos >= w->count) { /*buffer is full*/			\
251
        int err;							\
252
	err = w->action->flush(w, w->context);				\
253
	if (err != 0) { return err; }					\
Anders Nilsson's avatar
Anders Nilsson committed
254
255
256
257
      }									\
      w->data[w->pos] = ((unsigned char*)(&data))[i];			\
      w->pos++;								\
    }									\
258
    return 0;								\
Anders Nilsson's avatar
Anders Nilsson committed
259
260
261
262
263
  }

#else

#define LABCOMM_ENCODE(name, type)					\
264
  static inline int labcomm_write_##name(struct labcomm_writer *w, type data) { \
Anders Nilsson's avatar
Anders Nilsson committed
265
266
267
    int i;								\
    for (i = 0 ; i < sizeof(type) ; i++) {				\
      if (w->pos >= w->count) {						\
268
        int err;							\
269
	err = w->action->flush(w, w->context);					\
270
	if (err != 0) { return err; }					\
Anders Nilsson's avatar
Anders Nilsson committed
271
272
273
274
      }									\
      w->data[w->pos] = ((unsigned char*)(&data))[i];			\
      w->pos++;								\
    }									\
275
    return 0;								\
Anders Nilsson's avatar
Anders Nilsson committed
276
277
278
279
280
281
282
283
284
285
286
  }

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

288
static inline int labcomm_write_packed32(struct labcomm_writer *w, 
289
290
291
292
293
294
295
296
297
298
299
					 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;
300
      err = w->action->flush(w, w->context);
301
302
303
304
305
306
307
      if (err != 0) { return err; }
    }
    w->data[w->pos++] = tmp[i] | (i?0x80:0x00);
  }
  return 0;
}

308
static inline int labcomm_write_string(struct labcomm_writer *w, char *s)
Anders Nilsson's avatar
Anders Nilsson committed
309
{
310
  int length, i, err; 
Anders Nilsson's avatar
Anders Nilsson committed
311
312

  length = strlen((char*)s);
313
314
  err = labcomm_write_packed32(w, length);
  if (err != 0) { return err; }
Anders Nilsson's avatar
Anders Nilsson committed
315
316
  for (i = 0 ; i < length ; i++) {
    if (w->pos >= w->count) {	
317
      int err;
318
      err = w->action->flush(w, w->context);
319
      if (err != 0) { return err; }
Anders Nilsson's avatar
Anders Nilsson committed
320
321
322
323
    }
    w->data[w->pos] = s[i];
    w->pos++;
  }
324
  return 0;
Anders Nilsson's avatar
Anders Nilsson committed
325
326
}

327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * 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 */

#define LABCOMM_SIGNATURE_ARRAY_FREE(name, kind)		\
  if (name.data) { free(name.data); }				\
  name.data = (kind *)NULL; /* typechecking */

#define LABCOMM_SIGNATURE_ARRAY_REF(name, kind, index)			\
  (name.data = (kind *)name.data, /* typechecking no-op */		\
   (kind *)(labcomm_signature_array_ref(&name.first, &name.last,	\
					(void **)&name.data,		\
					sizeof(kind), index)))

void *labcomm_signature_array_ref(int *first, int *last, void **data,
				  int size, int index);


Anders Nilsson's avatar
Anders Nilsson committed
359
#endif