labcomm2014_encoder.c 14.9 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
23
24
25
26
27
28
29
30
31
/*
  labcomm2014_encoder.c -- handling encoding of labcomm2014 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/>.
*/
#define CURRENT_VERSION "LabComm2014"

#include <errno.h>
#include "labcomm2014.h"
#include "labcomm2014_private.h"
#include "labcomm2014_ioctl.h"
#include "labcomm2014_dynamic_buffer_writer.h"

//define the following to disable encoding of typedefs
#undef LABCOMM_WITHOUT_TYPE_DEFS

32
33
struct encoder {
  struct labcomm2014_encoder encoder;
34
35
36
37
38
  LABCOMM_SIGNATURE_ARRAY_DEF(registered, int);
  LABCOMM_SIGNATURE_ARRAY_DEF(sample_ref, int);
  LABCOMM_SIGNATURE_ARRAY_DEF(typedefs, int);
};

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* XXX: TEMPORARY PLACEHOLDERS FOR INTENTIONS */

static int TODO_sizeof_intentions(const struct labcomm2014_signature *signature) {
    return labcomm2014_size_string(signature->name) + 2;
}

static int TODO_encode_intentions(
  struct labcomm2014_encoder *e,
  const struct labcomm2014_signature *signature)
{
  int result = -EINVAL;

  labcomm2014_write_packed32(e->writer, 1); // one intention: the name
  labcomm2014_write_packed32(e->writer, 0); // key: the empty string
  labcomm2014_write_string(e->writer, signature->name);

  result = e->writer->error;
  return result;
}
58
static int do_sample_register(
59
60
61
62
63
  struct labcomm2014_encoder *e,
  const struct labcomm2014_signature *signature,
  labcomm2014_encoder_function encode)
{
  int result = -EINVAL;
64
  struct encoder *ie = e->context;
65
66
67
68
69
  int index, *done, err, i, length;

  index = labcomm2014_get_local_index(signature);
  labcomm2014_scheduler_writer_lock(e->scheduler);
  if (index <= 0) { goto out; }
70
  done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, ie->registered, int, index);
71
72
73
74
75
76
77
78
79
  if (*done) {
      goto out; }
  *done = 1;
  err = labcomm2014_writer_start(e->writer, e->writer->action_context,
			     index, signature, NULL);
  if (err == -EALREADY) { result = 0; goto out; }
  if (err != 0) { result = err; goto out; }
  labcomm2014_write_packed32(e->writer, LABCOMM_SAMPLE_DEF);
  length = (labcomm2014_size_packed32(index) +
80
            TODO_sizeof_intentions(signature) +
81
82
83
84
            labcomm2014_size_packed32(signature->size) +
            signature->size);
  labcomm2014_write_packed32(e->writer, length);
  labcomm2014_write_packed32(e->writer, index);
85
  TODO_encode_intentions(e, signature);
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  labcomm2014_write_packed32(e->writer, signature->size);
  for (i = 0 ; i < signature->size ; i++) {
    if (e->writer->pos >= e->writer->count) {
      labcomm2014_writer_flush(e->writer, e->writer->action_context);
    }
    e->writer->data[e->writer->pos] = signature->signature[i];
    e->writer->pos++;
  }
  labcomm2014_writer_end(e->writer, e->writer->action_context);
  result = e->writer->error;
out:
  labcomm2014_scheduler_writer_unlock(e->scheduler);
  return result;
}

101
static int do_encode(
102
103
104
105
106
107
  struct labcomm2014_encoder *e,
  const struct labcomm2014_signature *signature,
  labcomm2014_encoder_function encode,
  void *value)
{
  int result, index, length;
108
109
  struct encoder *ie = e->context;
  
110
111
112
  index = labcomm2014_get_local_index(signature);
  length = (signature->encoded_size(value));
  labcomm2014_scheduler_writer_lock(e->scheduler);
113
  if (! LABCOMM_SIGNATURE_ARRAY_GET(ie->registered, int, index, 0)) {
114
115
116
117
    result = -EINVAL;
    goto no_end;
  }
  result = labcomm2014_writer_start(e->writer, e->writer->action_context,
118
                                    index, signature, value);
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  if (result == -EALREADY) { result = 0; goto no_end; }
  if (result != 0) { goto out; }
  result = labcomm2014_write_packed32(e->writer, index);
  result = labcomm2014_write_packed32(e->writer, length);
  if (result != 0) { goto out; }
  result = encode(e->writer, value);
out:
  labcomm2014_writer_end(e->writer, e->writer->action_context);
no_end:
  labcomm2014_scheduler_writer_unlock(e->scheduler);
  return result;
}

int labcomm2014_encoder_sample_ref_register(
  struct labcomm2014_encoder *e,
  const struct labcomm2014_signature *signature)
135
136
137
138
139
140
141
{
  return e->ref_register(e, signature);
}

static int do_ref_register(
  struct labcomm2014_encoder *e,
  const struct labcomm2014_signature *signature)
142
143
{
  int result = -EINVAL;
144
  struct encoder *ie = e->context;
145
146
147
148
149
150
  int index, *done, err, i, length;

  index = labcomm2014_get_local_index(signature);
  labcomm2014_scheduler_writer_lock(e->scheduler);
  if (index <= 0) { goto out; }

151
  done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, ie->sample_ref, int, index);
152
153
154
155
156
157
158
159
  if (*done) { goto out; }
  *done = 1;
  err = labcomm2014_writer_start(e->writer, e->writer->action_context,
			     index, signature, NULL);
  if (err == -EALREADY) { result = 0; goto out; }
  if (err != 0) { result = err; goto out; }
  labcomm2014_write_packed32(e->writer, LABCOMM_SAMPLE_REF);
  length = (labcomm2014_size_packed32(index) +
160
            TODO_sizeof_intentions(signature) +
161
162
163
164
            labcomm2014_size_packed32(signature->size) +
            signature->size);
  labcomm2014_write_packed32(e->writer, length);
  labcomm2014_write_packed32(e->writer, index);
165
  TODO_encode_intentions(e, signature);
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  labcomm2014_write_packed32(e->writer, signature->size);
  for (i = 0 ; i < signature->size ; i++) {
    if (e->writer->pos >= e->writer->count) {
      labcomm2014_writer_flush(e->writer, e->writer->action_context);
    }
    e->writer->data[e->writer->pos] = signature->signature[i];
    e->writer->pos++;
  }
  labcomm2014_writer_end(e->writer, e->writer->action_context);
  result = e->writer->error;
out:
  labcomm2014_scheduler_writer_unlock(e->scheduler);
  return result;
}

int labcomm2014_encoder_ioctl(struct labcomm2014_encoder *encoder,
182
183
                              uint32_t action,
                              ...)
184
185
186
187
188
189
190
191
192
193
{
  int result;
  va_list va;

  if (LABCOMM_IOC_SIG(action) != LABCOMM_IOC_NOSIG) {
    result = -EINVAL;
    goto out;
  }

  va_start(va, action);
194
  result = encoder->ioctl(encoder, NULL, action, va);
195
196
197
198
199
200
  va_end(va);

out:
  return result;
}

201
202
203
204
static int do_ioctl(
  struct labcomm2014_encoder *encoder,
  const struct labcomm2014_signature *signature,
  uint32_t action, va_list va)
205
206
207
208
209
210
211
212
213
214
215
{
  int result = -ENOTSUP;
  int index;

  index = labcomm2014_get_local_index(signature);
  result = labcomm2014_writer_ioctl(encoder->writer,
				encoder->writer->action_context,
				index, signature, action, va);
  return result;
}

216
217
218
static int do_sample_ref_to_index(
  struct labcomm2014_encoder *e,
  const struct labcomm2014_sample_ref *sample_ref)
219
220
{
  /* writer_lock should be held at this point */
221
  struct encoder *ie = e->context;
222
  int index = 0;
223
224
225
  if (sample_ref != NULL) {
    index = labcomm2014_get_local_index(
      (struct labcomm2014_signature *)sample_ref);
226
    if (! LABCOMM_SIGNATURE_ARRAY_GET(ie->sample_ref, int, index, 0)) {
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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
279
      index = 0;
    }
  }
  return index;
}


/**********************************************************
 * Start of code related to sending (hierarchical)
 * typedefs. Define LABCOMM_WITHOUT_TYPEDEFS to disable
 **********************************************************/
#ifndef LABCOMM_WITHOUT_TYPE_DEFS

static void write_sig_tree_byte(char b, const struct labcomm2014_signature *signature,
                           void *context)
{
  struct labcomm2014_encoder *e = context;
  if(signature) {
    labcomm2014_write_packed32(e->writer, labcomm2014_get_local_index(signature));
  }else {
    if (e->writer->pos >= e->writer->count) {
     labcomm2014_writer_flush(e->writer, e->writer->action_context);
    }
    e->writer->data[e->writer->pos] = b;
    e->writer->pos++;
  }
}

static void do_write_signature(struct labcomm2014_encoder * e, 
                               const struct labcomm2014_signature *signature, 
                               unsigned char flatten)
{
  map_signature(write_sig_tree_byte, e, signature, flatten);
}

static void sig_size(char b, const struct labcomm2014_signature *signature,
                     void *context)
{
  int *result = context;
  int diff;
  if(signature) {
    int idx = labcomm2014_get_local_index(signature);
    diff = labcomm2014_size_packed32(idx);
  }else {
    diff = 1;
  }
    (*result)+=diff;
}

static int calc_sig_encoded_size(struct labcomm2014_encoder *e,
                                 const struct labcomm2014_signature *sig)
{
  int result=0;
280
  map_signature(sig_size, &result, sig, LABCOMM2014_FALSE);
281
282
283
284
285
286
287
288
289
290
  return result;
}

static int internal_reg_type(
  struct labcomm2014_encoder *e,
  const struct labcomm2014_signature *signature,
  labcomm2014_bool flatten)
{
  int result = -EINVAL;
  int index, *done, err;
291
292
  struct encoder *ie = e->context;
  
293
294
295
  index = labcomm2014_get_local_index(signature);
  labcomm2014_scheduler_writer_lock(e->scheduler);
  if (index <= 0) { goto out; }
296
  done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, ie->typedefs, int, index);
297
298
299
  if (*done) { goto out; }
  *done = 1;
  err = labcomm2014_writer_start(e->writer, e->writer->action_context,
300
                                 index, signature, NULL);
301
302
303
304
  if (err == -EALREADY) { result = 0; goto out; }
  if (err != 0) { result = err; goto out; }

  int sig_size = calc_sig_encoded_size(e, signature);
305
306
307
308
  int len = (labcomm2014_size_packed32(index) +
            TODO_sizeof_intentions(signature) +
            labcomm2014_size_packed32(signature->size) +
            sig_size);
309
310
311
312

  labcomm2014_write_packed32(e->writer, LABCOMM_TYPE_DEF);
  labcomm2014_write_packed32(e->writer, len);
  labcomm2014_write_packed32(e->writer, index);
313
  TODO_encode_intentions(e, signature);
314
  labcomm2014_write_packed32(e->writer, sig_size);
315
  do_write_signature(e, signature, LABCOMM2014_FALSE);
316
317
318
319
320
321
322
323
324

  labcomm2014_writer_end(e->writer, e->writer->action_context);
  result = e->writer->error;
out:
  labcomm2014_scheduler_writer_unlock(e->scheduler);
  return result;
}
#endif

325
static int do_type_register(
326
327
328
329
  struct labcomm2014_encoder *e,
  const struct labcomm2014_signature *signature)
{
#ifndef LABCOMM_WITHOUT_TYPE_DEFS
330
  return internal_reg_type(e, signature, LABCOMM2014_FALSE);
331
332
333
334
#else
  return 0;
#endif
}
335
336

static int do_type_bind(
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
363
364
365
366
367
  struct labcomm2014_encoder *e,
  const struct labcomm2014_signature *signature,
  char has_deps)
{
#ifndef LABCOMM_WITHOUT_TYPE_DEFS
  int result = -EINVAL;
  int err;
  int sindex = labcomm2014_get_local_index(signature);
  int tindex = has_deps ? labcomm2014_get_local_type_index(signature) : LABCOMM_BIND_SELF;
  labcomm2014_scheduler_writer_lock(e->scheduler);
  if(sindex <= 0 || (has_deps && tindex <= 0)) {goto out;}
  err = labcomm2014_writer_start(e->writer, e->writer->action_context,
			     LABCOMM_TYPE_BINDING, signature, NULL);
  if (err == -EALREADY) { result = 0; goto out; }
  if (err != 0) { result = err; goto out; }
  int length = (labcomm2014_size_packed32(sindex) +
                labcomm2014_size_packed32(tindex));
  labcomm2014_write_packed32(e->writer, LABCOMM_TYPE_BINDING);
  labcomm2014_write_packed32(e->writer, length);
  labcomm2014_write_packed32(e->writer, sindex);
  labcomm2014_write_packed32(e->writer, tindex);
  labcomm2014_writer_end(e->writer, e->writer->action_context);
  result = e->writer->error;

out:
  labcomm2014_scheduler_writer_unlock(e->scheduler);
  return result;
#else
  return 0;
#endif
}
368

369
static const struct labcomm2014_sample_ref *do_ref_get(
Anders Blomdell's avatar
Anders Blomdell committed
370
371
372
  struct labcomm2014_encoder *e,
  const struct labcomm2014_signature *signature)
{
373
  return (const struct labcomm2014_sample_ref *) signature;
Anders Blomdell's avatar
Anders Blomdell committed
374
375
}

376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
void labcomm2014_encoder_free(struct labcomm2014_encoder* e)
{
  e->free(e);
}

static void do_free(struct labcomm2014_encoder* e)
{
  struct encoder *ie = e->context;
  struct labcomm2014_memory *memory = e->memory;

  labcomm2014_writer_free(e->writer, e->writer->action_context);
  LABCOMM_SIGNATURE_ARRAY_FREE(e->memory, ie->registered, int);
  LABCOMM_SIGNATURE_ARRAY_FREE(e->memory, ie->sample_ref, int);
  LABCOMM_SIGNATURE_ARRAY_FREE(e->memory, ie->typedefs, int);
  labcomm2014_memory_free(memory, 0, ie);
}

393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
static struct labcomm2014_encoder *internal_encoder_new(
  struct labcomm2014_writer *writer,
  struct labcomm2014_error_handler *error,
  struct labcomm2014_memory *memory,
  struct labcomm2014_scheduler *scheduler,
  labcomm2014_bool outputVer)
{
  struct encoder *result;

  result = labcomm2014_memory_alloc(memory, 0, sizeof(*result));
  if (result) {
    int length;

    result->encoder.context = result;
    result->encoder.writer = writer;
    result->encoder.writer->encoder = &result->encoder;
    result->encoder.writer->data = NULL;
    result->encoder.writer->data_size = 0;
    result->encoder.writer->count = 0;
    result->encoder.writer->pos = 0;
    result->encoder.writer->error = 0;
    result->encoder.error = error;
    result->encoder.memory = memory;
    result->encoder.scheduler = scheduler;
417
    result->encoder.free = do_free;
418
419
420
    result->encoder.type_register = do_type_register;
    result->encoder.type_bind = do_type_bind;
    result->encoder.sample_register = do_sample_register;
421
    result->encoder.ref_register = do_ref_register;
422
423
    result->encoder.encode = do_encode;
    result->encoder.ioctl = do_ioctl;
424
    result->encoder.sample_ref_to_index = do_sample_ref_to_index;
Anders Blomdell's avatar
Anders Blomdell committed
425
    result->encoder.ref_get = do_ref_get;
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
    LABCOMM_SIGNATURE_ARRAY_INIT(result->registered, int);
    LABCOMM_SIGNATURE_ARRAY_INIT(result->sample_ref, int);
    LABCOMM_SIGNATURE_ARRAY_INIT(result->typedefs, int);
    labcomm2014_writer_alloc(result->encoder.writer,
                             result->encoder.writer->action_context);
    if(outputVer) {
        labcomm2014_writer_start(result->encoder.writer,
                                 result->encoder.writer->action_context,
                                 LABCOMM_VERSION, NULL, CURRENT_VERSION);
        labcomm2014_write_packed32(result->encoder.writer, LABCOMM_VERSION);
        length = labcomm2014_size_string(CURRENT_VERSION);
        labcomm2014_write_packed32(result->encoder.writer, length);
        labcomm2014_write_string(result->encoder.writer, CURRENT_VERSION);
        labcomm2014_writer_end(result->encoder.writer,
                               result->encoder.writer->action_context);
    }
  }
  return &(result->encoder);
}

struct labcomm2014_encoder *labcomm2014_encoder_new(
  struct labcomm2014_writer *writer,
  struct labcomm2014_error_handler *error,
  struct labcomm2014_memory *memory,
  struct labcomm2014_scheduler *scheduler)
{
    return internal_encoder_new(writer,error,memory,scheduler,LABCOMM2014_TRUE);
}


456
const struct labcomm2014_sample_ref *labcomm2014_encoder_get_sample_ref(
Anders Blomdell's avatar
Anders Blomdell committed
457
458
459
460
461
462
  struct labcomm2014_encoder *encoder,
  const struct labcomm2014_signature *signature)
{
  return encoder->ref_get(encoder, signature);
}