controller_block.c 18.8 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007, 2008, 2009
	Copyright Stichting C.A. Muller Radioastronomiestation, 2007, 2008, 2009

	This program 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.

	This program 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/>.

 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <pthread.h>
#include <dlfcn.h>

#include "controller_block.h"
#include "controller_sample.h"
29
#include "log.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

int nr_blocks = 0;
static struct controller_block **blocks = NULL;

struct calculate_func {
	void (*func)(struct controller_block *);
	struct controller_block *block;
};

static int nr_calculates = 0;
static struct calculate_func *calculates = NULL;

static int nr_traces = 0;
static struct controller_trace **traces;
static int max_traces = 0;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
46
47
static char *controller_block_context = "unknown";

Jeroen Vreeken's avatar
Jeroen Vreeken committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#define BLOCK_PREFIX "block_"
#define BLOCK_POSTFIX "_create"

int controller_block_create(char *type, char *name, va_list ap)
{
	void *handle;
	struct controller_block *(*create_func)(char *name, va_list ap);
	char *prefix = BLOCK_PREFIX;
	char *postfix = BLOCK_POSTFIX;
	char *symbol;
	int ret = 0;
	
	handle = dlopen(NULL, RTLD_NOW);
	if (!handle) {
62
63
		log_send(LOG_T_ERROR, 
		    "Error getting handle from dlopen(): %s", dlerror());
Jeroen Vreeken's avatar
Jeroen Vreeken committed
64
65
66
67
68
69
70
71
72
73
		return -1;
	}
	
	symbol = malloc(strlen(prefix)+strlen(type)+strlen(postfix)+1);
	sprintf(symbol, "%s%s%s", prefix, type, postfix);
	
	create_func = dlsym(handle, symbol);
	
	if (create_func) {
		if(!create_func(name, ap)) {
74
			log_send(LOG_T_ERROR, "Error: %s() failed", symbol);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
75
76
77
			ret = -1;
		}
	} else {
78
79
		log_send(LOG_T_ERROR, 
		    "Error finding function %s for block %s", symbol, type);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
		ret = -1;
	}
	
	free(symbol);
	
	dlclose(handle);
	
	return ret;
}

static char * controller_block_term_str(enum controller_block_term_type et)
{
	char *type;

	switch (et) {
		case CONTROLLER_BLOCK_TERM_FLOAT:
			type = "float";
			break;
		case CONTROLLER_BLOCK_TERM_BOOL:
			type = "bool";
			break;
		case CONTROLLER_BLOCK_TERM_UINT32:
			type = "uint32";
			break;
		default:
			type = "unknown";
			break;
	};
	return type;
}

void controller_block_add(struct controller_block *newblock)
{
	void *tmp;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
115
116
	newblock->context = strdup(controller_block_context);

117
	log_send(LOG_T_DEBUG, "Adding a new block: %s (%s) in context %s",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
118
	    newblock->name, newblock->type, newblock->context);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
119
120
121
122
123
124
125
126
	
	tmp = realloc(blocks, sizeof(struct controller_block *) * (nr_blocks+1));
	if (tmp) {
		int i;
	
		blocks = tmp;
		blocks[nr_blocks] = newblock;
		
127
		log_send(LOG_T_DEBUG, "\tInputs:");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
128
129
130
131
		for (i = 0; i < newblock->inputs; i++) {
			char *type;
			
			type = controller_block_term_str(newblock->input[i].type);
132
133
			log_send(LOG_T_DEBUG,
			    "\t\t%d\t%s (%s)", i, newblock->input[i].name,
Jeroen Vreeken's avatar
Jeroen Vreeken committed
134
135
136
137
138
			    type);
			*newblock->input[i].value.v = NULL;
			newblock->input[i].otherside = NULL;
			newblock->input[i].block = newblock;
		}
139
		log_send(LOG_T_DEBUG, "\tOutputs:");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
140
141
142
143
		for (i = 0; i < newblock->outputs; i++) {
			char *type;
			
			type = controller_block_term_str(newblock->output[i].type);
144
145
			log_send(LOG_T_DEBUG, "\t\t%d\t%s (%s)",
			    i, newblock->output[i].name, type);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
146
		}
147
		log_send(LOG_T_DEBUG, "\tParams:");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
148
		for (i = 0; i < newblock->params; i++)
149
			log_send(LOG_T_DEBUG, "\t\t%d\t%s%s", i, 
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
			    newblock->param[i].name,
			    newblock->param[i].sample ? " (SAMPLE)" : "");
		
		nr_blocks++;
	}
}

struct controller_block *controller_block_find(char *name)
{
	int i;
	
	for (i = 0; i < nr_blocks; i++) {
		if (!strcmp(blocks[i]->name, name))
			return blocks[i];
	}
	return NULL;
}

int controller_block_nr(void)
{
	return nr_blocks;
}

struct controller_block *controller_block_get(int nr)
{
	return blocks[nr];
}

struct controller_block_outterm *controller_block_find_outterm(
    struct controller_block *block, char *output)
{
	int i;
	
	for (i = 0; i < block->outputs; i++) {
		if (!strcmp(block->output[i].name, output))
			return &block->output[i];
	}
	
	return NULL;
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

char *controller_block_context_get(void)
{
	return controller_block_context;
}

void controller_block_context_set(char *new_context)
{
	controller_block_context = new_context;
}

/*
	Sample processing
 */

Jeroen Vreeken's avatar
Jeroen Vreeken committed
206
207
static int sample_running = 0;

208
int controller_block_sample_init(void)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
209
210
211
{
	int i, j, k;
	void *tmp;
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
	bool check = true;

	/* Sanity check */
	for (i = 0; i < nr_blocks; i++) {
		int j;
		
		for (j = 0; j < blocks[i]->inputs; j++) {
			if (*blocks[i]->input[j].value.v == NULL) {
				log_send(LOG_T_ERROR,
				    "%s.%s is not connected!",
				    blocks[i]->name,
				    blocks[i]->input[j].name);
				check = false;
			}
		}
	}
	if (!check) {
		log_send(LOG_T_ERROR, 
		     "Errors found while checking block connections");
		return -1;
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256

	while (1) {
		k = -1;
		for (i = 0; i < nr_blocks; i++) {
			for (j = 0; j < blocks[i]->inputs; j++) {
				if (blocks[i]->input[j].otherside) {
					for (k = 0; k < nr_blocks; k++) {
						if (blocks[i]->input[j].otherside ==
						    blocks[k])
							break;
					}
					if (i < k) {
						break;
					}
				}
				if (i < k)
					break;
			}
			if (i < k)
				break;
		}
		if (i < k) {
			struct controller_block *dep = blocks[k];
			
257
			log_send(LOG_T_DEBUG, "Moving %s before %s in chain",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
258
259
260
261
262
263
264
			    blocks[k]->name,
			    blocks[i]->name);
			memmove(&blocks[i + 1], &blocks[i],
			    sizeof(struct controller_block *) * (k - i));
			blocks[i] = dep;
		} else {
			if (i == nr_blocks) {
265
				log_send(LOG_T_DEBUG, "Sorting done");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
266
267
268
269
270
271
272
273
274
275
276
				break;
			}
		}
	}
	

	for (i = 0; i < nr_blocks; i++) {
		int first = 1;
		for (j = 0; j < blocks[i]->inputs; j++) {
			if (blocks[i]->input[j].otherside) {
				if (first)
277
					log_send(LOG_T_DEBUG, "%s chained to:",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
278
279
					    blocks[i]->name);
				first = 0;
280
				log_send(LOG_T_DEBUG, "\t%s",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
281
282
283
284
285
286
287
				    blocks[i]->input[j].otherside->name);
			}
		}
	}

	for (i = 0; i < nr_blocks; i++) {
		if (blocks[i]->calculate) {
288
289
			log_send(LOG_T_DEBUG,
			    "Block %s has a calculate function",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
290
291
292
293
294
295
296
297
298
			    blocks[i]->name);
			tmp = realloc(calculates,
			     sizeof(struct calculate_func) * (nr_calculates+1));
			if (tmp) {
				calculates = tmp;
				calculates[nr_calculates].func =
				    blocks[i]->calculate;
				calculates[nr_calculates].block = blocks[i];
				nr_calculates++;
299
300
301
			} else {
				log_send(LOG_T_ERROR,
				    "Out of memory allocating memory");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
302
303
			}
		} else
304
305
			log_send(LOG_T_DEBUG, 
			    "Block %s has no calculate function",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
306
307
			    blocks[i]->name);
	}
308
	log_send(LOG_T_DEBUG, "Found %d calculate functions in %d blocks",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
309
310
	    nr_calculates, nr_blocks);
	sample_running = 1;
311
312

	return 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
}

static pthread_mutex_t param_lock;
static sem_t param_sync_sem;
static void (*param_set_func)(struct controller_block *, int, va_list) = NULL;
static void (*param_get_func)(struct controller_block *, int, void *) = NULL;
static int param_nr;
static void *param_value;
static va_list param_set_value;
static struct controller_block *param_block;

void controller_block_param_init(void)
{
	pthread_mutex_init(&param_lock, NULL);
	sem_init(&param_sync_sem, 0, 0);
}

int controller_block_param_set(char *block, char *param, va_list value)
{
	int i, j;
	
	for (i = 0; i < nr_blocks; i++) {
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
		if (strcmp(blocks[i]->name, block))
			continue;
			
		for (j = 0; j < blocks[i]->params; j++) {
			if (strcmp(blocks[i]->param[j].name, param))
				continue;
			
			if (!sample_running ||
			    controller_sample_context() ||
			    !blocks[i]->param[j].sample) {
				blocks[i]->param_set(
				    blocks[i], j, value);
				return 0;
			}
				
			pthread_mutex_lock(&param_lock);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
351

352
353
354
355
356
357
			param_nr = j;
			va_copy(param_set_value, value);
			param_block = blocks[i];
			param_set_func = blocks[i]->param_set;
				
			sem_wait(&param_sync_sem);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
358

359
360
			pthread_mutex_unlock(&param_lock);
			return 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
361
362
363
364
365
366
367
368
369
370
		}
	}
	return -1;
}

int controller_block_param_get(char *block, char *param, void *value)
{
	int i, j;
	
	for (i = 0; i < nr_blocks; i++) {
371
372
		if (strcmp(blocks[i]->name, block))
			continue;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
373

374
375
376
		for (j = 0; j < blocks[i]->params; j++) {
			if (strcmp(blocks[i]->param[j].name, param))
				continue;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
377
					
378
379
380
381
382
383
384
385
386
			if (!sample_running ||
			    controller_sample_context() ||
			    !blocks[i]->param[j].sample) {
				blocks[i]->param_get(
				    blocks[i], j, value);
				return 0;
			}

			pthread_mutex_lock(&param_lock);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
387
					
388
389
390
391
392
393
			param_nr = j;
			param_value = value;
			param_block = blocks[i];
			param_get_func = blocks[i]->param_get;
				
			sem_wait(&param_sync_sem);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
394
					
395
396
			pthread_mutex_unlock(&param_lock);
			return 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
397
398
399
400
401
402
403
404
405
406
		}
	}
	return -1;
}

int controller_block_output_get_float(char *block, char *param, float *value)
{
	int i, j, ret = -1;
	
	for (i = 0; i < nr_blocks; i++) {
407
408
409
410
411
412
413
414
415
		if (strcmp(blocks[i]->name, block))
			continue;
		
		for (j = 0; j < blocks[i]->outputs; j++) {
			if (!strcmp(blocks[i]->output[j].name, param) &&
			    blocks[i]->output[j].type == CONTROLLER_BLOCK_TERM_FLOAT) {
				*value = *blocks[i]->output[j].value.f;
				ret = 0;
				break;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
			}
		}
	}
	return ret;
}


static struct controller_block_link *links = NULL;
static int link_nr = 0;

struct controller_block_link *controller_block_link_get(int nr)
{
	return &links[nr];
}

int controller_block_link_nr(void)
{
	return link_nr;
}

436
int controller_block_connect(char *outblock, char *outterm, char *inblock, char *interm, int chain)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
{
	struct controller_block_link *tmp;

	link_nr++;
	tmp = realloc(
	    links, 
	    sizeof(struct controller_block_link)*link_nr);
	if (tmp) {
		links = tmp;
		links[link_nr-1].outblock = strdup(outblock);
		links[link_nr-1].outterm = strdup(outterm);
		links[link_nr-1].inblock = strdup(inblock);
		links[link_nr-1].interm = strdup(interm);
		links[link_nr-1].chain = chain;
451
452
453
454
455
		links[link_nr-1].type = NULL;
		
		return 0;
	} else {
		return -1;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
456
457
458
	}
}

459
static int controller_block_resolve_link(struct controller_block_link *link)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
460
461
462
463
464
465
466
467
468
469
{
	struct controller_block *out = NULL;
	struct controller_block *in = NULL;
	float *outptr = NULL;
	struct controller_block *outsource = NULL;
	struct controller_block_interm *realin = NULL;
	int i;
	enum controller_block_term_type outtype = CONTROLLER_BLOCK_TERM_VOID;
	
	for (i = 0; i < nr_blocks; i++) {
470
		if (!strcmp(link->outblock, blocks[i]->name))
Jeroen Vreeken's avatar
Jeroen Vreeken committed
471
			out = blocks[i];
472
		if (!strcmp(link->inblock, blocks[i]->name))
Jeroen Vreeken's avatar
Jeroen Vreeken committed
473
474
475
476
477
			in = blocks[i];
	}
	
	if (in && out) {
		for (i = 0; i < out->outputs; i++) {
478
			if (!strcmp(out->output[i].name, link->outterm)) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
479
480
481
482
483
484
485
				outptr = out->output[i].value.v;
				outsource = out->output[i].source;
				outtype = out->output[i].type;
			}
		}
		if (outptr) {
			for (i = 0; i < in->inputs; i++) {
486
487
488
				if (strcmp(in->input[i].name, link->interm) ||
				    in->input[i].type != outtype)
					continue;
489

490
			    	link->type = controller_block_term_str(in->input[i].type);
491
				
492
493
494
495
496
497
498
499
500
501
502
503
504
505
				log_send(LOG_T_DEBUG,
				    "Linking %s.%s to %s.%s%s",
				    link->outblock, link->outterm, 
				    link->inblock, link->interm,
				    link->chain ? " (chained)" : "");

				*in->input[i].value.v = outptr;
				if (link->chain)
					in->input[i].otherside = outsource;
				else
					in->input[i].otherside = NULL;
				for (realin = &in->input[i];
				    realin->ghostof;
				    realin = realin->ghostof) {
506
					if (link->chain)
507
						realin->ghostof->otherside = outsource;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
508
					else
509
						realin->ghostof->otherside = NULL;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
510
				}
511
				return 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
512
			}
513
514
			log_send(LOG_T_ERROR, 
			    "Input terminal %s.%s not found",
515
			    link->inblock, link->interm);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
516
		} else {
517
518
			log_send(LOG_T_ERROR,
			    "Output terminal %s.%s not found",
519
			    link->outblock, link->outterm);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
520
521
522
		}
	} else {
		if (!in) {
523
524
			log_send(LOG_T_DEBUG,
			    "Input block %s does not (yet) exist, will try to link later",
525
			    link->inblock);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
526
527
		}
		if (!out) {
528
529
			log_send(LOG_T_DEBUG,
			    "Output block %s does not (yet) exist, will try to link later",
530
			    link->outblock);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
531
532
533
534
535
536
		}
	}
	
	return -1;
}

537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558

int controller_block_link(void)
{
	int i, r;
	int ret = 0;
	
	for (i = 0; i < controller_block_link_nr(); i++) {
		struct controller_block_link *link;
		
		link = controller_block_link_get(i);
		if (!link->type) {
			r = controller_block_resolve_link(link);
			if (r) {
				ret = r;
			}
		}
	}
	
	return ret;
}


Jeroen Vreeken's avatar
Jeroen Vreeken committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
static pthread_mutex_t trace_lock;
static sem_t trace_sync_sem;
static sem_t trace_waitsem;
static struct controller_trace *trace_add_ptr = NULL;
static struct controller_trace *trace_del_ptr = NULL;

void controller_block_trace_init(int max)
{
	int i;
	
	traces = malloc(sizeof(struct controller_trace *) * max);
	max_traces = max;

	/* touch entire trace list */
	for (i = 0; i < max; i++) {
		traces[i] = NULL;
	}

	pthread_mutex_init(&trace_lock, NULL);
	sem_init(&trace_sync_sem, 0, 0);
	sem_init(&trace_waitsem, 0, 0);
}

int controller_block_trace_add(char *block, char *outterm, 
    struct controller_trace *trace)
{
	void *ptr = NULL;
	enum controller_block_term_type type = 0;
	int i, j;
	int ret = 0;
	
	for (i = 0; i < nr_blocks; i++) {
591
592
593
594
595
596
597
		if (strcmp(block, blocks[i]->name))
			continue;
			
		for (j = 0; j < blocks[i]->outputs; j++) {
			if (!strcmp(outterm, blocks[i]->output[j].name)){
				ptr = blocks[i]->output[j].value.v;
				type = blocks[i]->output[j].type;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
			}
		}
	}

	trace->rd_pos = 0;
	trace->wr_pos = 0;
	trace->type = type;
	trace->ptr = ptr;
	
	/* touch the entire buffer atleast once */
	for (i = 0; i < trace->len; i++) {
		trace->buffer[i] = (float)i;
	}

	pthread_mutex_lock(&trace_lock);

	if (nr_traces < max_traces) {
		trace_add_ptr = trace;

617
618
		log_send(LOG_T_DEBUG, "Addded %s.%s to trace list",
		    block, outterm);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
	} else {
		ret = 1;
	}
	sem_wait(&trace_sync_sem);

	pthread_mutex_unlock(&trace_lock);

	return ret;
}

void controller_block_trace_del(struct controller_trace *trace)
{
	pthread_mutex_lock(&trace_lock);

	trace_del_ptr = trace;
	sem_wait(&trace_sync_sem);

	pthread_mutex_unlock(&trace_lock);

	return;
}

void controller_block_trace_wait(void)
{
	sem_wait(&trace_waitsem);
}

uint64_t controller_time_nseconds = 0;
uint32_t controller_time_seconds = 0;
uint32_t controller_time_samplenr = 0;
uint32_t controller_samplenr = 0;

void controller_block_calculate(void)
{
	int i;
	
	for (i = 0; i < nr_calculates; i++)
		calculates[i].func(calculates[i].block);
}


void controller_block_param_handle(void)
{
	if (param_set_func) {
		param_set_func(param_block, param_nr, param_set_value);
		param_set_func = NULL;
		sem_post(&param_sync_sem);
	}
	if (param_get_func) {
		param_get_func(param_block, param_nr, param_value);
		param_get_func = NULL;
		sem_post(&param_sync_sem);
	}
}    


void controller_block_trace(void)
{
	int i;
	int semval;
	
	if (trace_add_ptr) {
		traces[nr_traces] = trace_add_ptr;
		nr_traces++;

		trace_add_ptr = NULL;
		sem_post(&trace_sync_sem);
	}
	if (trace_del_ptr) {
		for (i = 0; i < nr_traces; i++) {
			if (traces[i] == trace_del_ptr) {
				if (i + 1 < nr_traces) {
					memmove(&traces[i + 0],
					    &traces[i + 1],
					    sizeof(struct controller_trace *) *
					    (nr_traces - i - 1));
				}
				break;
			}
		}
		nr_traces--;
		trace_del_ptr = NULL;
		sem_post(&trace_sync_sem);
	}
	
	for (i = 0; i < nr_traces; i++) {
		if (controller_time_samplenr == 0)
		{
			traces[i]->wr_pos++;
			if (traces[i]->wr_pos == traces[i]->len)
				traces[i]->wr_pos = 0;
			*((uint32_t *)&traces[i]->buffer[traces[i]->wr_pos]) =
			    0xffffffff; /* NaN */
			traces[i]->wr_pos++;
			if (traces[i]->wr_pos == traces[i]->len)
				traces[i]->wr_pos = 0;
			*((uint32_t *)&traces[i]->buffer[traces[i]->wr_pos]) =
			    controller_time_seconds;
		}
		traces[i]->wr_pos++;
		if (traces[i]->wr_pos == traces[i]->len)
			traces[i]->wr_pos = 0;
		switch (traces[i]->type) {
			case CONTROLLER_BLOCK_TERM_BOOL:
				traces[i]->buffer[traces[i]->wr_pos] = 
				    *(bool*)traces[i]->ptr;
				break;
			case CONTROLLER_BLOCK_TERM_FLOAT:
				traces[i]->buffer[traces[i]->wr_pos] = 
				    *(float*)traces[i]->ptr;
				break;
			case CONTROLLER_BLOCK_TERM_UINT32:
				traces[i]->buffer[traces[i]->wr_pos] = 
				    *(uint32_t*)traces[i]->ptr;
				break;
			default:
				traces[i]->buffer[traces[i]->wr_pos] = -1;
		}
		
	}

	sem_getvalue(&trace_waitsem, &semval);
	if (!semval) {
		sem_post(&trace_waitsem);
	}
}

/* helper function to initialize param lists */
int controller_block_param_list_init(struct controller_block *block,
    struct controller_block_param_list *list)
{
	int i;
	
	for (i = 0; list[i].name; i++);
	block->params = i;
	block->param = malloc(sizeof(struct controller_block_param_list) * i);
	if (!block->param)
		return 1;
	
	for (i = 0; list[i].name; i++){
		block->param[i].name = malloc(strlen(list[i].name) + 1);
		if (!block->param[i].name)
			goto err_undo;
		strcpy(block->param[i].name, list[i].name);
		block->param[i].sample = list[i].sample;
	}
	
	return 0;

err_undo:
	for (; i > 0; i--) {
		free(block->param[i - 1].name);
	}
	free(block->param);
	
	return 1;
}
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822

int controller_block_interm_list_init(struct controller_block *block,
    struct controller_block_interm_list *list)
{
	int inputs, i;
	
	for (inputs = 0; list[inputs].name; inputs++);
	
	block->inputs = inputs;
	block->input = calloc(sizeof(struct controller_block_interm), inputs);
	if (!block->input)
		return -1;
		
	for (i = 0; i < inputs; i++) {
		block->input[i].name = list[i].name;
		block->input[i].type = list[i].type;
		block->input[i].value.v = (void **)(
		    (char *)block->private + list[i].priv_offset);
		block->input[i].ghostof = NULL;
	}

	return 0;
}

int controller_block_outterm_list_init(struct controller_block *block,
    struct controller_block_outterm_list *list)
{
	int outputs, i;
	
	for (outputs = 0; list[outputs].name; outputs++);
	
	block->outputs = outputs;
	block->output = calloc(sizeof(struct controller_block_outterm), outputs);
	if (!block->output)
		return -1;
		
	for (i = 0; i < outputs; i++) {
		block->output[i].name = list[i].name;
		block->output[i].type = list[i].type;
		block->output[i].value.v = 
		    (char *)block->private + list[i].priv_offset;
		block->output[i].source = block;
	}

	return 0;
}

823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868

/* Helper function to create a new block */
struct controller_block *controller_block_alloc(char *type, char *name,
    size_t private_size)
{
	struct controller_block	*blk;
	
	blk = calloc(1, sizeof(struct controller_block));
	if (!blk)
		goto err_calloc;
	
	blk->type = type;
	blk->name = strdup(name);
	if (!blk->name)
		goto err_name;
	
	if (private_size > 0) {
		blk->private = calloc(1, private_size);
		if (!blk->private)
			goto err_private;
	}

	return blk;

err_private:
	free(blk->name);
err_name:
	free(blk);
err_calloc:
	return NULL;
}

void controller_block_free(struct controller_block *blk)
{
	if (blk->name)
		free(blk->name);
	if (blk->private)
		free(blk->private);
	
	if (blk->input)
		free(blk->input);
	if (blk->output)
		free(blk->output);
	
	free(blk);
}