-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparameter.c
578 lines (515 loc) · 16.4 KB
/
parameter.c
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
177
178
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
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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
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
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
414
415
416
417
418
419
420
421
422
423
424
425
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include <string.h>
#include "parameter.h"
#include "parameter_port.h"
/*
* Synchronization is implemented using the parameter_port_lock() and
* parameter_port_unlock() functions provided in the parameter_port.h.
* Synchronization is required in the following places:
* - linked-list head pointer of the parameter list in a namespace
* - linked-list head pointer of the sub-namespace list in a namespace
* - the changed count of a namespace
* - the changed flag of a parameter
* - the parameter value
* All other values are read-only once the parameter/namespace has been linked
* into the parameter tree and therefore require no synchronization.
* Access to the above values can be synchronized independently except for the
* update of the changed flag, which has to be checked inside the critical
* section to know whether the changed count has to be updated for the
* containing namespaces. (The thread that succeeds in modifying the flag is
* responsible for updating the counters. Note that the counters could
* temporarily become negative in case the increment is interrupted by a
* get_parameter() which decreases the counter. This poses no problem since the
* check for the changed count correctly handles the signed integer counter)
*/
/* find the length of the next element in hierarchical id
* returns number of characters until the first '/' or the entire
* length if no '/' is found)
*/
static int id_split(const char *id, int id_len)
{
int i = 0;
for (i = 0; i < id_len; i++) {
if (id[i] == '/') {
return i;
}
}
return id_len;
}
/*
* get a sub-namespace of a namespace by id. search depth is only one level
*/
static parameter_namespace_t *get_subnamespace(parameter_namespace_t *ns,
const char *ns_id,
size_t ns_id_len)
{
if (ns_id_len == 0) {
return ns; // this allows to start with a '/' or have '//' instead of '/'
}
parameter_port_lock();
parameter_namespace_t *i = ns->subspaces;
parameter_port_unlock();
while (i != NULL) {
if (strncmp(ns_id, i->id, ns_id_len) == 0 && i->id[ns_id_len] == '\0') {
// if the first ns_id_len bytes of ns_id match with i->id and
// i->id is only ns_id_len bytes long, we've found the namespace
break;
}
i = i->next;
}
return i;
}
/*
* get a parameter of a namespace by id. search depth is only one level
*/
static parameter_t *get_parameter(parameter_namespace_t *ns, const char *id,
size_t param_id_len)
{
if (param_id_len == 0) {
return NULL;
}
parameter_port_lock();
parameter_t *i = ns->parameter_list;
parameter_port_unlock();
while (i != NULL) {
if (strncmp(id, i->id, param_id_len) == 0 && i->id[param_id_len] == '\0') {
// if the first param_id_len bytes of id match with i->id and
// i->id is only param_id_len bytes long, we've found the parameter
break;
}
i = i->next;
}
return i;
}
void parameter_namespace_declare(parameter_namespace_t *ns,
parameter_namespace_t *parent,
const char *id)
{
ns->id = id;
ns->changed_cnt = 0;
ns->parent = parent;
ns->subspaces = NULL;
ns->parameter_list = NULL;
if (parent != NULL) {
parameter_port_lock();
// link into parent namespace
ns->next = ns->parent->subspaces;
ns->parent->subspaces = ns;
parameter_port_unlock();
} else {
ns->next = NULL;
}
}
parameter_namespace_t *_parameter_namespace_find_w_id_len(parameter_namespace_t *ns,
const char *id, size_t id_len)
{
parameter_namespace_t *nret = ns;
uint32_t i = 0;
while (nret != NULL && i < id_len) {
int id_elem_len = id_split(&id[i], id_len - i);
nret = get_subnamespace(nret, &id[i], id_elem_len);
i += id_elem_len + 1;
}
return nret;
}
parameter_namespace_t *parameter_namespace_find(parameter_namespace_t *ns,
const char *id)
{
return _parameter_namespace_find_w_id_len(ns, id, strlen(id));
}
parameter_t *_parameter_find_w_id_len(parameter_namespace_t *ns,
const char *id, size_t id_len)
{
parameter_namespace_t *pns = ns;
uint32_t i = 0;
while (pns != NULL) {
int id_elem_len = id_split(&id[i], id_len - i);
if (id_elem_len + i < id_len) {
pns = get_subnamespace(pns, &id[i], id_elem_len);
} else {
return get_parameter(pns, &id[i], id_elem_len);
}
i += id_elem_len + 1;
}
return NULL;
}
parameter_t *parameter_find(parameter_namespace_t *ns, const char *id)
{
return _parameter_find_w_id_len(ns, id, strlen(id));
}
void _parameter_declare(parameter_t *p, parameter_namespace_t *ns,
const char *id)
{
p->id = id;
p->ns = ns;
p->changed = false;
p->defined = false;
parameter_port_lock();
// link into namespace
p->next = p->ns->parameter_list;
p->ns->parameter_list = p;
parameter_port_unlock();
}
bool parameter_namespace_contains_changed(const parameter_namespace_t *ns)
{
parameter_port_lock();
uint32_t changed_cnt = ns->changed_cnt;
parameter_port_unlock();
return changed_cnt > 0;
}
bool parameter_changed(const parameter_t *p)
{
parameter_port_lock();
bool changed = p->changed;
parameter_port_unlock();
return changed;
}
bool parameter_defined(const parameter_t *p)
{
if (p == NULL) {
return false;
}
parameter_port_lock();
bool defined = p->defined;
parameter_port_unlock();
return defined;
}
void _parameter_changed_set(parameter_t *p)
{
parameter_port_lock();
bool changed_was_set = p->changed;
p->changed = true;
parameter_port_unlock();
if (changed_was_set) {
return;
}
// if the above "compare and set" passes, the changed count can safely
// be incremented for the namespaces
parameter_namespace_t *ns = p->ns;
while (ns != NULL) {
parameter_port_lock();
ns->changed_cnt++;
parameter_port_unlock();
ns = ns->parent;
}
p->defined = true;
}
void _parameter_changed_clear(parameter_t *p)
{
parameter_port_lock();
bool changed_was_set = p->changed;
p->changed = false;
parameter_port_unlock();
if (!changed_was_set) {
return;
}
// if the above "compare and set" passes, the changed count can safely
// be decremented for the namespaces
parameter_namespace_t *ns = p->ns;
while (ns != NULL) {
parameter_port_lock();
ns->changed_cnt--; // here change counts can temporarily become negative
parameter_port_unlock();
ns = ns->parent;
}
}
/*
* Scalar type parameter
*/
void parameter_scalar_declare(parameter_t *p, parameter_namespace_t *ns,
const char *id)
{
p->type = _PARAM_TYPE_SCALAR;
_parameter_declare(p, ns, id);
}
void parameter_scalar_declare_with_default(parameter_t *p,
parameter_namespace_t *ns,
const char *id,
float default_val)
{
p->value.s = default_val;
p->type = _PARAM_TYPE_SCALAR;
_parameter_declare(p, ns, id);
_parameter_changed_set(p);
}
float parameter_scalar_get(parameter_t *p)
{
_parameter_changed_clear(p);
return parameter_scalar_read(p);
}
float parameter_scalar_read(parameter_t *p)
{
parameter_port_assert(p->type == _PARAM_TYPE_SCALAR);
parameter_port_lock();
parameter_port_assert(p->defined == true);
float ret = p->value.s;
parameter_port_unlock();
return ret;
}
void parameter_scalar_set(parameter_t *p, float value)
{
parameter_port_assert(p->type == _PARAM_TYPE_SCALAR);
parameter_port_lock();
p->value.s = value;
parameter_port_unlock();
_parameter_changed_set(p);
}
/*
* Integer type parameter
*/
void parameter_integer_declare(parameter_t *p, parameter_namespace_t *ns,
const char *id)
{
p->type = _PARAM_TYPE_INTEGER;
_parameter_declare(p, ns, id);
}
void parameter_integer_declare_with_default(parameter_t *p,
parameter_namespace_t *ns,
const char *id,
int32_t default_val)
{
p->value.i = default_val;
p->type = _PARAM_TYPE_INTEGER;
_parameter_declare(p, ns, id);
_parameter_changed_set(p);
}
int32_t parameter_integer_get(parameter_t *p)
{
_parameter_changed_clear(p);
return parameter_integer_read(p);
}
int32_t parameter_integer_read(parameter_t *p)
{
parameter_port_assert(p->type == _PARAM_TYPE_INTEGER);
parameter_port_lock();
parameter_port_assert(p->defined == true);
int32_t ret = p->value.i;
parameter_port_unlock();
return ret;
}
void parameter_integer_set(parameter_t *p, int32_t value)
{
parameter_port_assert(p->type == _PARAM_TYPE_INTEGER);
parameter_port_lock();
p->value.i = value;
parameter_port_unlock();
_parameter_changed_set(p);
}
/* Boolean type parameter. */
void parameter_boolean_declare(parameter_t *p, parameter_namespace_t *ns, const char *id)
{
p->type = _PARAM_TYPE_BOOLEAN;
_parameter_declare(p, ns, id);
}
void parameter_boolean_declare_with_default(parameter_t *p,
parameter_namespace_t *ns,
const char *id,
bool default_val)
{
p->value.b = default_val;
p->type = _PARAM_TYPE_BOOLEAN;
_parameter_declare(p, ns, id);
_parameter_changed_set(p);
}
void parameter_boolean_set(parameter_t *p, bool value)
{
parameter_port_assert(p->type == _PARAM_TYPE_BOOLEAN);
parameter_port_lock();
p->value.b = value;
parameter_port_unlock();
_parameter_changed_set(p);
}
bool parameter_boolean_get(parameter_t *p)
{
_parameter_changed_clear(p);
return parameter_boolean_read(p);
}
bool parameter_boolean_read(parameter_t *p)
{
parameter_port_assert(p->type == _PARAM_TYPE_BOOLEAN);
parameter_port_lock();
parameter_port_assert(p->defined == true);
bool ret = p->value.b;
parameter_port_unlock();
return ret;
}
/*
* Vector type parameter
*/
void parameter_vector_declare(parameter_t *p, parameter_namespace_t *ns,
const char *id, float *buf, uint16_t dim)
{
p->type = _PARAM_TYPE_VECTOR;
p->value.vect.buf = buf;
p->value.vect.dim = dim;
_parameter_declare(p, ns, id);
}
void parameter_vector_declare_with_default(parameter_t *p,
parameter_namespace_t *ns,
const char *id,
float *buf,
uint16_t dim)
{
parameter_vector_declare(p, ns, id, buf, dim);
_parameter_changed_set(p);
}
uint16_t parameter_vector_dim(parameter_t *p)
{
parameter_port_assert(p->type == _PARAM_TYPE_VECTOR);
return p->value.vect.dim;
}
void parameter_vector_get(parameter_t *p, float *out)
{
_parameter_changed_clear(p);
parameter_vector_read(p, out);
}
void parameter_vector_read(parameter_t *p, float *out)
{
parameter_port_assert(p->type == _PARAM_TYPE_VECTOR);
parameter_port_lock();
parameter_port_assert(p->defined == true);
int i;
for (i = 0; i < p->value.vect.dim; i++) {
out[i] = p->value.vect.buf[i];
}
parameter_port_unlock();
}
void parameter_vector_set(parameter_t *p, const float *v)
{
parameter_port_assert(p->type == _PARAM_TYPE_VECTOR);
parameter_port_lock();
int i;
for (i = 0; i < p->value.vect.dim; i++) {
p->value.vect.buf[i] = v[i];
}
parameter_port_unlock();
_parameter_changed_set(p);
}
/*
* Variable size vector type parameter
*/
void parameter_variable_vector_declare(parameter_t *p,
parameter_namespace_t *ns,
const char *id,
float *buf,
uint16_t buf_size)
{
p->type = _PARAM_TYPE_VAR_VECTOR;
p->value.vect.buf = buf;
p->value.vect.buf_dim = buf_size;
_parameter_declare(p, ns, id);
}
void parameter_variable_vector_declare_with_default(parameter_t *p,
parameter_namespace_t *ns,
const char *id,
float *buf,
uint16_t buf_size,
uint16_t init_size)
{
parameter_variable_vector_declare(p, ns, id, buf, buf_size);
parameter_port_assert(init_size <= buf_size);
p->value.vect.dim = init_size;
_parameter_changed_set(p);
}
uint16_t parameter_variable_vector_max_dim(parameter_t *p)
{
parameter_port_assert(p->type == _PARAM_TYPE_VAR_VECTOR);
return p->value.vect.buf_dim;
}
uint16_t parameter_variable_vector_get(parameter_t *p, float *out)
{
_parameter_changed_clear(p);
return parameter_variable_vector_read(p, out);
}
uint16_t parameter_variable_vector_read(parameter_t *p, float *out)
{
parameter_port_assert(p->type == _PARAM_TYPE_VAR_VECTOR);
parameter_port_lock();
parameter_port_assert(p->defined == true);
int i;
for (i = 0; i < p->value.vect.dim; i++) {
out[i] = p->value.vect.buf[i];
}
uint16_t ret = p->value.vect.dim;
parameter_port_unlock();
return ret;
}
void parameter_variable_vector_set(parameter_t *p, const float *v, uint16_t dim)
{
parameter_port_assert(p->type == _PARAM_TYPE_VAR_VECTOR);
parameter_port_lock();
parameter_port_assert(dim <= p->value.vect.buf_dim);
int i;
for (i = 0; i < dim; i++) {
p->value.vect.buf[i] = v[i];
}
p->value.vect.dim = dim;
parameter_port_unlock();
_parameter_changed_set(p);
}
/*
* String type parameter
*/
void parameter_string_declare(parameter_t *p,
parameter_namespace_t *ns,
const char *id,
char *buf,
uint16_t buf_size)
{
p->type = _PARAM_TYPE_STRING;
p->value.str.buf = buf;
p->value.str.buf_len = buf_size;
_parameter_declare(p, ns, id);
}
void parameter_string_declare_with_default(parameter_t *p,
parameter_namespace_t *ns,
const char *id,
char *buf,
uint16_t buf_size,
const char *default_str)
{
parameter_string_declare(p, ns, id, buf, buf_size);
p->value.str.len = strlen(default_str);
parameter_port_assert(p->value.str.len <= buf_size);
strncpy(p->value.str.buf, default_str, buf_size);
_parameter_changed_set(p);
}
uint16_t parameter_string_max_len(parameter_t *p)
{
parameter_port_assert(p->type == _PARAM_TYPE_STRING);
return p->value.str.buf_len + 1; // +1 for '\0' terminator
}
uint16_t parameter_string_get(parameter_t *p, char *out, uint16_t out_size)
{
_parameter_changed_clear(p);
return parameter_string_read(p, out, out_size);
}
uint16_t parameter_string_read(parameter_t *p, char *out, uint16_t out_size)
{
parameter_port_assert(p->type == _PARAM_TYPE_STRING);
parameter_port_lock();
parameter_port_assert(p->defined == true);
uint16_t len = p->value.str.len;
if (out_size > len) {
memcpy(out, p->value.str.buf, len);
out[len] = '\0';
} else {
memcpy(out, p->value.str.buf, out_size - 1);
out[out_size - 1] = '\0';
}
parameter_port_unlock();
return len;
}
void parameter_string_set(parameter_t *p, const char *str)
{
parameter_string_set_w_len(p, str, strlen(str));
}
void parameter_string_set_w_len(parameter_t *p, const char *str, uint16_t len)
{
parameter_port_assert(p->type == _PARAM_TYPE_STRING);
parameter_port_lock();
parameter_port_assert(len <= p->value.str.buf_len);
memcpy(p->value.str.buf, str, len);
p->value.str.len = len;
parameter_port_unlock();
_parameter_changed_set(p);
}