labcomm2014_encoder.c 13.6 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
static int do_sample_register(
40
41
42
43
44
  struct labcomm2014_encoder *e,
  const struct labcomm2014_signature *signature,
  labcomm2014_encoder_function encode)
{
  int result = -EINVAL;
45
  struct encoder *ie = e->context;
46
47
48
49
50
  int index, *done, err, i, length;

  index = labcomm2014_get_local_index(signature);
  labcomm2014_scheduler_writer_lock(e->scheduler);
  if (index <= 0) { goto out; }
51
  done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, ie->registered, int, index);
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  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) +
            labcomm2014_size_string(signature->name) +
            labcomm2014_size_packed32(signature->size) +
            signature->size);
  labcomm2014_write_packed32(e->writer, length);
  labcomm2014_write_packed32(e->writer, index);
  labcomm2014_write_string(e->writer, signature->name);
  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;
}

82
static int do_encode(
83
84
85
86
87
88
  struct labcomm2014_encoder *e,
  const struct labcomm2014_signature *signature,
  labcomm2014_encoder_function encode,
  void *value)
{
  int result, index, length;
89
90
  struct encoder *ie = e->context;
  
91
92
93
  index = labcomm2014_get_local_index(signature);
  length = (signature->encoded_size(value));
  labcomm2014_scheduler_writer_lock(e->scheduler);
94
  if (! LABCOMM_SIGNATURE_ARRAY_GET(ie->registered, int, index, 0)) {
95
96
97
98
    result = -EINVAL;
    goto no_end;
  }
  result = labcomm2014_writer_start(e->writer, e->writer->action_context,
99
                                    index, signature, value);
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  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)
{
  int result = -EINVAL;
118
  struct encoder *ie = e->context;
119
120
121
122
123
124
  int index, *done, err, i, length;

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

125
  done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, ie->sample_ref, int, index);
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  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) +
            labcomm2014_size_string(signature->name) +
            labcomm2014_size_packed32(signature->size) +
            signature->size);
  labcomm2014_write_packed32(e->writer, length);
  labcomm2014_write_packed32(e->writer, index);
  labcomm2014_write_string(e->writer, signature->name);
  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,
			  uint32_t action,
			  ...)
{
  int result;
  va_list va;

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

  va_start(va, action);
  result = labcomm2014_writer_ioctl(encoder->writer,
			       encoder->writer->action_context,
			       0, NULL, action, va);
  va_end(va);

out:
  return result;
}

177
178
179
180
static int do_ioctl(
  struct labcomm2014_encoder *encoder,
  const struct labcomm2014_signature *signature,
  uint32_t action, va_list va)
181
182
183
184
185
186
187
188
189
190
191
{
  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;
}

192
static int do_signature_to_index(
193
194
195
  struct labcomm2014_encoder *e, const struct labcomm2014_signature *signature)
{
  /* writer_lock should be held at this point */
196
  struct encoder *ie = e->context;
197
198
199
  int index = 0;
  if (signature != NULL) {
    index = labcomm2014_get_local_index(signature);
200
    if (! LABCOMM_SIGNATURE_ARRAY_GET(ie->sample_ref, int, index, 0)) {
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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
      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;
254
  map_signature(sig_size, &result, sig, LABCOMM2014_FALSE);
255
256
257
258
259
260
261
262
263
264
  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;
265
266
  struct encoder *ie = e->context;
  
267
268
269
  index = labcomm2014_get_local_index(signature);
  labcomm2014_scheduler_writer_lock(e->scheduler);
  if (index <= 0) { goto out; }
270
  done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, ie->typedefs, int, index);
271
272
273
  if (*done) { goto out; }
  *done = 1;
  err = labcomm2014_writer_start(e->writer, e->writer->action_context,
274
                                 index, signature, NULL);
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  if (err == -EALREADY) { result = 0; goto out; }
  if (err != 0) { result = err; goto out; }

  int sig_size = calc_sig_encoded_size(e, signature);
  int len =  labcomm2014_size_packed32(index) +
             labcomm2014_size_string(signature->name) +
             labcomm2014_size_packed32(sig_size) +
              sig_size;

  labcomm2014_write_packed32(e->writer, LABCOMM_TYPE_DEF);
  labcomm2014_write_packed32(e->writer, len);
  labcomm2014_write_packed32(e->writer, index);
  labcomm2014_write_string(e->writer, signature->name);
  labcomm2014_write_packed32(e->writer, sig_size);
289
  do_write_signature(e, signature, LABCOMM2014_FALSE);
290
291
292
293
294
295
296
297
298

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

299
static int do_type_register(
300
301
302
303
  struct labcomm2014_encoder *e,
  const struct labcomm2014_signature *signature)
{
#ifndef LABCOMM_WITHOUT_TYPE_DEFS
304
  return internal_reg_type(e, signature, LABCOMM2014_FALSE);
305
306
307
308
#else
  return 0;
#endif
}
309
310

static int do_type_bind(
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
  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
}
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413

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;
    result->encoder.type_register = do_type_register;
    result->encoder.type_bind = do_type_bind;
    result->encoder.sample_register = do_sample_register;
    result->encoder.encode = do_encode;
    result->encoder.ioctl = do_ioctl;
    result->encoder.signature_to_index = do_signature_to_index;
    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);
}

void labcomm2014_encoder_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);
}