labcomm_private.h 7.43 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
/*
 * 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;
60
61
62
63
  struct {
    void *context;
    const struct labcomm_lock_action *action;
  } lock;
Oscar Olsson's avatar
Oscar Olsson committed
64
65
  labcomm_error_handler_callback on_error;
  labcomm_handle_new_datatype_callback on_new_datatype;
Anders Nilsson's avatar
Anders Nilsson committed
66
67
68
69
70
71
72
73
74
75
76
77
78
} 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);

79
80
81
82
83
int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder, 
				   int ioctl_action,
				   labcomm_signature_t *signature,
				   va_list args);

Anders Nilsson's avatar
Anders Nilsson committed
84
85
86
87
88
89
90
#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) {						\
91
	r->action.fill(r);						\
Anders Nilsson's avatar
Anders Nilsson committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
      }									\
      ((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) {						\
109
	r->action.fill(r);						\
Anders Nilsson's avatar
Anders Nilsson committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
      }									\
      ((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)
129

130
static inline unsigned int labcomm_read_packed32(labcomm_reader_t *r)
131
132
133
134
135
136
137
{
  unsigned int result = 0;
  
  while (1) {
    unsigned char tmp;

    if (r->pos >= r->count) {	
138
      r->action.fill(r);
139
140
141
142
143
144
145
146
147
148
149
    }
    tmp = r->data[r->pos];
    r->pos++;
    result = (result << 7) | (tmp & 0x7f);
    if ((tmp & 0x80) == 0) { 
      break; 
    }
  }
  return result;
}
 
150
151
static inline unsigned int labcomm_decode_packed32(labcomm_decoder_t *d) 
{
152
  return labcomm_read_packed32(&d->reader);
153
154
}

Anders Nilsson's avatar
Anders Nilsson committed
155
156
157
158
159
static inline char *labcomm_read_string(labcomm_reader_t *r)
{
  char *result;
  int length, i; 
  
160
  length = labcomm_read_packed32(r);
Anders Nilsson's avatar
Anders Nilsson committed
161
162
163
  result = malloc(length + 1);
  for (i = 0 ; i < length ; i++) {
    if (r->pos >= r->count) {	
164
      r->action.fill(r);
Anders Nilsson's avatar
Anders Nilsson committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    }
    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
 */
181
typedef int (*labcomm_encoder_function)(
Anders Nilsson's avatar
Anders Nilsson committed
182
183
184
185
186
187
  struct labcomm_encoder *,
  void *value);

typedef struct labcomm_encoder {
  void *context;
  labcomm_writer_t writer;
188
189
190
191
  struct {
    void *context;
    const struct labcomm_lock_action *action;
  } lock;
Oscar Olsson's avatar
Oscar Olsson committed
192
  labcomm_error_handler_callback on_error;
Anders Nilsson's avatar
Anders Nilsson committed
193
194
195
196
197
} labcomm_encoder_t;

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

200
int labcomm_internal_encode(
Anders Nilsson's avatar
Anders Nilsson committed
201
202
  labcomm_encoder_t *encoder, 
  labcomm_signature_t *signature, 
203
  labcomm_encoder_function encode,
Anders Nilsson's avatar
Anders Nilsson committed
204
205
  void *value);

206
207
208
209
210
int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder, 
				   int ioctl_action,
				   labcomm_signature_t *signature,
				   va_list args);

Anders Nilsson's avatar
Anders Nilsson committed
211
212
213
#if __BYTE_ORDER == __LITTLE_ENDIAN

#define LABCOMM_ENCODE(name, type)					\
214
  static inline int labcomm_write_##name(labcomm_writer_t *w, type data) { \
Anders Nilsson's avatar
Anders Nilsson committed
215
216
    int i;								\
    for (i = sizeof(type) - 1 ; i >= 0 ; i--) {				\
Oscar Olsson's avatar
Oscar Olsson committed
217
      if (w->pos >= w->count) { /*buffer is full*/			\
218
        int err;							\
219
	err = w->action.flush(w);					\
220
	if (err != 0) { return err; }					\
Anders Nilsson's avatar
Anders Nilsson committed
221
222
223
224
      }									\
      w->data[w->pos] = ((unsigned char*)(&data))[i];			\
      w->pos++;								\
    }									\
225
    return 0;								\
Anders Nilsson's avatar
Anders Nilsson committed
226
  }									\
227
  static inline int labcomm_encode_##name(labcomm_encoder_t *e, type data) { \
228
    return labcomm_write_##name(&e->writer, data);			\
Anders Nilsson's avatar
Anders Nilsson committed
229
230
231
232
233
  }

#else

#define LABCOMM_ENCODE(name, type)					\
234
  static inline int labcomm_write_##name(labcomm_writer_t *w, type data) { \
Anders Nilsson's avatar
Anders Nilsson committed
235
236
237
    int i;								\
    for (i = 0 ; i < sizeof(type) ; i++) {				\
      if (w->pos >= w->count) {						\
238
        int err;							\
239
	err = w->action.flush(w);					\
240
	if (err != 0) { return err; }					\
Anders Nilsson's avatar
Anders Nilsson committed
241
242
243
244
      }									\
      w->data[w->pos] = ((unsigned char*)(&data))[i];			\
      w->pos++;								\
    }									\
245
    return 0;								\
Anders Nilsson's avatar
Anders Nilsson committed
246
  }									\
247
  static inline int labcomm_encode_##name(labcomm_encoder_t *e, type data) { \
248
    return labcomm_write_##name(&e->writer, data);			\
Anders Nilsson's avatar
Anders Nilsson committed
249
250
251
252
253
254
255
256
257
258
259
  }

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

261
262
263
264
265
266
267
268
269
270
271
272
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;
273
      err = w->action.flush(w);
274
275
276
277
278
279
280
      if (err != 0) { return err; }
    }
    w->data[w->pos++] = tmp[i] | (i?0x80:0x00);
  }
  return 0;
}

281

282
283
static inline int labcomm_encode_packed32(labcomm_encoder_t *e, 
					   unsigned int data) 
284
{ 
285
  return labcomm_write_packed32(&e->writer, data);				
286
287
}

288

289
static inline int labcomm_write_string(labcomm_writer_t *w, char *s)
Anders Nilsson's avatar
Anders Nilsson committed
290
{
291
  int length, i, err; 
Anders Nilsson's avatar
Anders Nilsson committed
292
293

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

308
static inline int labcomm_encode_string(labcomm_encoder_t *e, 
Anders Nilsson's avatar
Anders Nilsson committed
309
310
					 char *s)
{
311
  return labcomm_write_string(&e->writer, s);
Anders Nilsson's avatar
Anders Nilsson committed
312
313
314
}

#endif