controller_block.c 12.5 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

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;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
42
43
static char *controller_block_context = "unknown";

Jeroen Vreeken's avatar
Jeroen Vreeken committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#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) {
58
		log_send(LOG_T_ERROR, 
59
		    "Could not get handle from dlopen(): %s", dlerror());
Jeroen Vreeken's avatar
Jeroen Vreeken committed
60
61
62
63
64
65
66
67
68
69
		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)) {
70
			log_send(LOG_T_ERROR, "%s() failed", symbol);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
71
72
73
			ret = -1;
		}
	} else {
74
		log_send(LOG_T_ERROR, 
75
		    "Could not find function %s for block %s", symbol, type);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
		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
111
112
	newblock->context = strdup(controller_block_context);

113
	log_send(LOG_T_DEBUG, "Adding a new block: %s (%s) in context %s",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
114
	    newblock->name, newblock->type, newblock->context);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
115
116
117
118
119
120
121
122
	
	tmp = realloc(blocks, sizeof(struct controller_block *) * (nr_blocks+1));
	if (tmp) {
		int i;
	
		blocks = tmp;
		blocks[nr_blocks] = newblock;
		
123
		log_send(LOG_T_DEBUG, "\tInputs:");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
124
125
126
127
		for (i = 0; i < newblock->inputs; i++) {
			char *type;
			
			type = controller_block_term_str(newblock->input[i].type);
128
129
			log_send(LOG_T_DEBUG,
			    "\t\t%d\t%s (%s)", i, newblock->input[i].name,
Jeroen Vreeken's avatar
Jeroen Vreeken committed
130
131
132
133
134
			    type);
			*newblock->input[i].value.v = NULL;
			newblock->input[i].otherside = NULL;
			newblock->input[i].block = newblock;
		}
135
		log_send(LOG_T_DEBUG, "\tOutputs:");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
136
137
138
139
		for (i = 0; i < newblock->outputs; i++) {
			char *type;
			
			type = controller_block_term_str(newblock->output[i].type);
140
141
			log_send(LOG_T_DEBUG, "\t\t%d\t%s (%s)",
			    i, newblock->output[i].name, type);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
142
		}
143
		log_send(LOG_T_DEBUG, "\tParams:");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
144
		for (i = 0; i < newblock->params; i++)
145
			log_send(LOG_T_DEBUG, "\t\t%d\t%s%s", i, 
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
			    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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

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
202

203
int controller_block_sample_init(void)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
204
205
206
{
	int i, j, k;
	void *tmp;
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
	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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251

	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];
			
252
			log_send(LOG_T_DEBUG, "Moving %s before %s in chain",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
253
254
255
256
257
258
259
			    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) {
260
				log_send(LOG_T_DEBUG, "Sorting done");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
261
262
263
264
265
266
267
268
269
270
271
				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)
272
					log_send(LOG_T_DEBUG, "%s chained to:",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
273
274
					    blocks[i]->name);
				first = 0;
275
				log_send(LOG_T_DEBUG, "\t%s",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
276
277
278
279
280
281
282
				    blocks[i]->input[j].otherside->name);
			}
		}
	}

	for (i = 0; i < nr_blocks; i++) {
		if (blocks[i]->calculate) {
283
284
			log_send(LOG_T_DEBUG,
			    "Block %s has a calculate function",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
285
286
287
288
289
290
291
292
293
			    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++;
294
295
296
			} else {
				log_send(LOG_T_ERROR,
				    "Out of memory allocating memory");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
297
298
			}
		} else
299
300
			log_send(LOG_T_DEBUG, 
			    "Block %s has no calculate function",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
301
302
			    blocks[i]->name);
	}
303
	log_send(LOG_T_DEBUG, "Found %d calculate functions in %d blocks",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
304
	    nr_calculates, nr_blocks);
305
306

	return 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
307
308
309
310
311
312
313
}

int controller_block_output_get_float(char *block, char *param, float *value)
{
	int i, j, ret = -1;
	
	for (i = 0; i < nr_blocks; i++) {
314
315
316
317
318
319
320
321
322
		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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
			}
		}
	}
	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;
}

343
int controller_block_connect(char *outblock, char *outterm, char *inblock, char *interm, int chain)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
{
	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;
358
359
360
361
362
		links[link_nr-1].type = NULL;
		
		return 0;
	} else {
		return -1;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
363
364
365
	}
}

366
static int controller_block_resolve_link(struct controller_block_link *link)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
367
368
369
370
371
372
373
374
375
376
{
	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++) {
377
		if (!strcmp(link->outblock, blocks[i]->name))
Jeroen Vreeken's avatar
Jeroen Vreeken committed
378
			out = blocks[i];
379
		if (!strcmp(link->inblock, blocks[i]->name))
Jeroen Vreeken's avatar
Jeroen Vreeken committed
380
381
382
383
384
			in = blocks[i];
	}
	
	if (in && out) {
		for (i = 0; i < out->outputs; i++) {
385
			if (!strcmp(out->output[i].name, link->outterm)) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
386
387
388
389
390
391
392
				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++) {
393
394
395
				if (strcmp(in->input[i].name, link->interm) ||
				    in->input[i].type != outtype)
					continue;
396

397
			    	link->type = controller_block_term_str(in->input[i].type);
398
				
399
400
401
402
403
404
405
406
407
408
409
410
411
412
				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) {
413
					if (link->chain)
414
						realin->ghostof->otherside = outsource;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
415
					else
416
						realin->ghostof->otherside = NULL;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
417
				}
418
				return 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
419
			}
420
421
			log_send(LOG_T_ERROR, 
			    "Input terminal %s.%s not found",
422
			    link->inblock, link->interm);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
423
		} else {
424
425
			log_send(LOG_T_ERROR,
			    "Output terminal %s.%s not found",
426
			    link->outblock, link->outterm);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
427
428
429
		}
	} else {
		if (!in) {
430
431
			log_send(LOG_T_DEBUG,
			    "Input block %s does not (yet) exist, will try to link later",
432
			    link->inblock);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
433
434
		}
		if (!out) {
435
436
			log_send(LOG_T_DEBUG,
			    "Output block %s does not (yet) exist, will try to link later",
437
			    link->outblock);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
438
439
440
441
442
443
		}
	}
	
	return -1;
}

444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465

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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481

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



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

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;
}

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

/* 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);
}