controller_module.c 4.11 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
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
/*
	Copyright Jeroen Vreeken (jeroen@vreeken.net), 2015

	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 <controller/controller_module.h>
#include <controller/controller_block.h>
#include <log/log.h>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>


struct ghost_term {
	char *name;
	char *realblock;
	char *realname;
};

static struct ghost_term *in_terms = NULL;
static struct ghost_term *out_terms = NULL;
static int in_term_nr = 0;
static int out_term_nr = 0;

int controller_module_input_add(char *name, char *blockname, char *inputname)
{
	in_term_nr++;
	in_terms = realloc(in_terms, sizeof(struct ghost_term) * in_term_nr);
	in_terms[in_term_nr-1].name = strdup(name);
	in_terms[in_term_nr-1].realblock = strdup(blockname);
	in_terms[in_term_nr-1].realname = strdup(inputname);
	
	return 0;
}

int controller_module_output_add(char *name, char *blockname, char *outputname)
{
	out_term_nr++;
	out_terms = realloc(out_terms, sizeof(struct ghost_term) * out_term_nr);
	out_terms[out_term_nr-1].name = strdup(name);
	out_terms[out_term_nr-1].realblock = strdup(blockname);
	out_terms[out_term_nr-1].realname = strdup(outputname);
	
	return 0;
}

int controller_module_create(char *name)
{
	struct controller_block *module;
	int i;
	
	if (!(module = controller_block_alloc("module", name, 0)))
		goto err_alloc;

	module->inputs = in_term_nr;
	module->input = calloc(in_term_nr, sizeof(struct controller_block_interm));
	if (!module->input)
		goto err_input;
	for (i = 0; i < in_term_nr; i++) {
		struct controller_block *subblock;
		int j;
		
		module->input[i].name = in_terms[i].name;
		subblock = controller_block_find(in_terms[i].realblock);
		if (!subblock) {
			log_send(LOG_T_ERROR, "block %s not found",
			    in_terms[i].realblock);
			goto err_output;
		}
		for (j = 0; j < subblock->inputs; j++) {
			if (!strcmp(subblock->input[j].name,
			    in_terms[i].realname)) {
				module->input[i].value =
				    subblock->input[j].value;
				module->input[i].type =
				    subblock->input[j].type;
				module->input[i].ghostof =
				    &subblock->input[j];
				break;
			}
		}
		if (j == subblock->inputs) {
			log_send(LOG_T_ERROR, 
			    "Block %s does not have an input %s",
			    in_terms[i].realblock, in_terms[i].realname);
			goto err_input;
		}
		free(in_terms[i].realblock);
	}

	module->outputs = out_term_nr;
	module->output = calloc(out_term_nr, sizeof(struct controller_block_outterm));
	if (!module->output)
		goto err_output;

	for (i = 0; i < out_term_nr; i++) {
		struct controller_block *subblock;
		int j;
		
		module->output[i].name = out_terms[i].name;
		subblock = controller_block_find(out_terms[i].realblock);
		if (!subblock) {
			log_send(LOG_T_ERROR, "block %s not found",
			    out_terms[i].realblock);
			goto err_output;
		}
		for (j = 0; j < subblock->outputs; j++) {
			if (!strcmp(subblock->output[j].name,
			    out_terms[i].realname)) {
				module->output[i].value =
				    subblock->output[j].value;
				module->output[i].type =
				    subblock->output[j].type;
				module->output[i].source = subblock->output[j].source;
				module->output[i].sourceterm = &subblock->output[j];
				break;
			}
		}
		if (j == subblock->outputs) {
			log_send(LOG_T_ERROR, 
			    "Block %s does not have an output %s",
			    out_terms[i].realblock, out_terms[i].realname);
			goto err_output;
		}
		free(out_terms[i].realblock);
	}

	free(in_terms);
	in_terms = NULL;
	free(out_terms);
	out_terms = NULL;
	in_term_nr = 0;
	out_term_nr = 0;

	controller_block_add(module);

	return 0;

err_input:
err_output:
	controller_block_free(module);
err_alloc:
	return -1;
}