labcomm_private.h 7.18 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 name __attribute__((section("labcomm")))
46

Anders Nilsson's avatar
Anders Nilsson committed
47
48
49
/*
 * Semi private decoder declarations
 */
50
typedef void (*labcomm_handler_function)(void *value, void *context);
Anders Nilsson's avatar
Anders Nilsson committed
51

52
typedef void (*labcomm_decoder_function)(
53
  struct labcomm_reader *r,
54
55
  labcomm_handler_function handler,
  void *context);
Anders Nilsson's avatar
Anders Nilsson committed
56

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
struct labcomm_reader_action {
  int (*alloc)(struct labcomm_reader *r, char *labcomm_version);
  int (*free)(struct labcomm_reader *r);
  int (*start)(struct labcomm_reader *r);
  int (*end)(struct labcomm_reader *r);
  int (*fill)(struct labcomm_reader *r); 
  int (*ioctl)(struct labcomm_reader *r, int, struct labcomm_signature *, va_list);
};

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
78
79
80
81
82
/*
 * Non typesafe registration function to be called from
 * generated labcomm_decoder_register_* functions.
 */
void labcomm_internal_decoder_register(
83
84
85
86
  struct labcomm_decoder *d, 
  struct labcomm_signature *s, 
  labcomm_decoder_function decoder,
  labcomm_handler_function handler,
Anders Nilsson's avatar
Anders Nilsson committed
87
88
  void *context);

89
90
int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder, 
				   int ioctl_action,
91
				   struct labcomm_signature *signature,
92
93
				   va_list args);

Anders Nilsson's avatar
Anders Nilsson committed
94
95
96
#if __BYTE_ORDER == __LITTLE_ENDIAN

#define LABCOMM_DECODE(name, type)					\
97
  static inline type labcomm_read_##name(struct labcomm_reader *r) {	\
Anders Nilsson's avatar
Anders Nilsson committed
98
99
100
    type result; int i;							\
    for (i = sizeof(type) - 1 ; i >= 0 ; i--) {				\
      if (r->pos >= r->count) {						\
101
	r->action->fill(r);						\
Anders Nilsson's avatar
Anders Nilsson committed
102
103
104
105
106
107
108
109
110
111
      }									\
      ((unsigned char*)(&result))[i] = r->data[r->pos];			\
      r->pos++;								\
    }									\
    return result;							\
  }

#else

#define LABCOMM_DECODE(name, type)					\
112
  static inline type labcomm_read_##name(struct labcomm_reader *r) {	\
Anders Nilsson's avatar
Anders Nilsson committed
113
114
115
    type result; int i;							\
    for (i = 0 ; i < sizeof(type) ; i++) {				\
      if (r->pos >= r->count) {						\
116
	r->action->fill(r);						\
Anders Nilsson's avatar
Anders Nilsson committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
      }									\
      ((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)
133

134
static inline unsigned int labcomm_read_packed32(struct labcomm_reader *r)
135
136
137
138
139
140
141
{
  unsigned int result = 0;
  
  while (1) {
    unsigned char tmp;

    if (r->pos >= r->count) {	
142
      r->action->fill(r);
143
144
145
146
147
148
149
150
151
152
153
    }
    tmp = r->data[r->pos];
    r->pos++;
    result = (result << 7) | (tmp & 0x7f);
    if ((tmp & 0x80) == 0) { 
      break; 
    }
  }
  return result;
}
 
154
static inline char *labcomm_read_string(struct labcomm_reader *r)
Anders Nilsson's avatar
Anders Nilsson committed
155
156
157
158
{
  char *result;
  int length, i; 
  
159
  length = labcomm_read_packed32(r);
Anders Nilsson's avatar
Anders Nilsson committed
160
161
162
  result = malloc(length + 1);
  for (i = 0 ; i < length ; i++) {
    if (r->pos >= r->count) {	
163
      r->action->fill(r);
Anders Nilsson's avatar
Anders Nilsson committed
164
165
166
167
168
169
170
171
172
173
174
    }
    result[i] = r->data[r->pos];
    r->pos++;
  }
  result[length] = 0;
  return result;
}

/*
 * Semi private encoder declarations
 */
175
typedef int (*labcomm_encoder_function)(
176
  struct labcomm_writer *,
Anders Nilsson's avatar
Anders Nilsson committed
177
178
  void *value);

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
struct labcomm_writer;

struct labcomm_writer_action {
  int (*alloc)(struct labcomm_writer *w, char *labcomm_version);
  int (*free)(struct labcomm_writer *w);
  int (*start)(struct labcomm_writer *w,
	       struct labcomm_encoder *encoder,
	       int index, struct labcomm_signature *signature,
	       void *value);
  int (*end)(struct labcomm_writer *w);
  int (*flush)(struct labcomm_writer *w); 
  int (*ioctl)(struct labcomm_writer *w, 
	       int index, struct labcomm_signature *, 
	       va_list);
};

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
205
void labcomm_internal_encoder_register(
206
207
  struct labcomm_encoder *encoder, 
  struct labcomm_signature *signature, 
208
  labcomm_encoder_function encode);
Anders Nilsson's avatar
Anders Nilsson committed
209

210
int labcomm_internal_encode(
211
212
  struct labcomm_encoder *encoder, 
  struct labcomm_signature *signature, 
213
  labcomm_encoder_function encode,
Anders Nilsson's avatar
Anders Nilsson committed
214
215
  void *value);

216

217
218
int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder, 
				   int ioctl_action,
219
				   struct labcomm_signature *signature,
220
221
				   va_list args);

Anders Nilsson's avatar
Anders Nilsson committed
222
223
224
#if __BYTE_ORDER == __LITTLE_ENDIAN

#define LABCOMM_ENCODE(name, type)					\
225
  static inline int labcomm_write_##name(struct labcomm_writer *w, type data) { \
Anders Nilsson's avatar
Anders Nilsson committed
226
227
    int i;								\
    for (i = sizeof(type) - 1 ; i >= 0 ; i--) {				\
Oscar Olsson's avatar
Oscar Olsson committed
228
      if (w->pos >= w->count) { /*buffer is full*/			\
229
        int err;							\
230
	err = w->action->flush(w);					\
231
	if (err != 0) { return err; }					\
Anders Nilsson's avatar
Anders Nilsson committed
232
233
234
235
      }									\
      w->data[w->pos] = ((unsigned char*)(&data))[i];			\
      w->pos++;								\
    }									\
236
    return 0;								\
Anders Nilsson's avatar
Anders Nilsson committed
237
238
239
240
241
  }

#else

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

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

266
static inline int labcomm_write_packed32(struct labcomm_writer *w, 
267
268
269
270
271
272
273
274
275
276
277
					 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;
278
      err = w->action->flush(w);
279
280
281
282
283
284
285
      if (err != 0) { return err; }
    }
    w->data[w->pos++] = tmp[i] | (i?0x80:0x00);
  }
  return 0;
}

286
static inline int labcomm_write_string(struct labcomm_writer *w, char *s)
Anders Nilsson's avatar
Anders Nilsson committed
287
{
288
  int length, i, err; 
Anders Nilsson's avatar
Anders Nilsson committed
289
290

  length = strlen((char*)s);
291
292
  err = labcomm_write_packed32(w, length);
  if (err != 0) { return err; }
Anders Nilsson's avatar
Anders Nilsson committed
293
294
  for (i = 0 ; i < length ; i++) {
    if (w->pos >= w->count) {	
295
      int err;
296
      err = w->action->flush(w);
297
      if (err != 0) { return err; }
Anders Nilsson's avatar
Anders Nilsson committed
298
299
300
301
    }
    w->data[w->pos] = s[i];
    w->pos++;
  }
302
  return 0;
Anders Nilsson's avatar
Anders Nilsson committed
303
304
305
}

#endif