dt_ctrl.c 5.33 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
/*
Jeroen Vreeken's avatar
Jeroen Vreeken committed
2
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007 - 2018
Jeroen Vreeken's avatar
Jeroen Vreeken committed
3
	Copyright Stichting C.A. Muller Radioastronomiestation, 2007 - 2013
Jeroen Vreeken's avatar
Jeroen Vreeken committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

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

 */
Jeroen Vreeken's avatar
Jeroen Vreeken committed
19
/*
20
	dt_ctrl.c
Jeroen Vreeken's avatar
Jeroen Vreeken committed
21
22
23
24
25
26
27
	
	Main loop for the DT controller.
	Here the controller network is initialized.
	(By loading a .ctrl file supplied on the command line).
	After initialization the sample thread is started and this function's
	work is done.
 */
Jeroen Vreeken's avatar
Jeroen Vreeken committed
28
29
30
31
32
33

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
root's avatar
root committed
34
#include <signal.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
35

36
#include "dynarg.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
37

38
#include <controller/controller_bus.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
39
40
41
42
43
#include <controller/controller_block.h>
#include <controller/controller_trace.h>
#include <controller/controller_sample.h>
#include <controller/controller_dumpdot.h>
#include <controller/controller_load.h>
Daan Vreeken's avatar
Daan Vreeken committed
44
#include <controller/controller_time.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
45
#include <controller/controller_mem.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
46
47
48
49
#include <shell/shell.h>
#include <log/log.h>

#include <dt_port_numbers.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
50

Jeroen Vreeken's avatar
Jeroen Vreeken committed
51
52
53
54
#ifdef HAVE_EMBEDDED
extern char _binary_controller_ctrl_embedded_ctrl_start;
extern char _binary_controller_ctrl_embedded_ctrl_end;
#endif
Jeroen Vreeken's avatar
Jeroen Vreeken committed
55
56
57
58
#ifdef HAVE_SYSTICK
#include <samx70.h>
#include <wdt.h>
#endif
Jeroen Vreeken's avatar
Jeroen Vreeken committed
59
60
61

int main(int argc, char **argv)
{
62
63
	char *ctrl_filename;
	char *dot_filename;
root's avatar
root committed
64
	sigset_t sigset;
65
66
67
	int blocks;
	int outputs;
	int i;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
68
	size_t usage_0, usage_r, usage_w, usage_rw;
root's avatar
root committed
69

Jeroen Vreeken's avatar
Jeroen Vreeken committed
70
#ifdef HAVE_FILEIO
root's avatar
root committed
71
72
73
74
	sigemptyset (&sigset);
	sigaddset(&sigset, SIGALRM);
	sigprocmask(SIG_BLOCK, &sigset, NULL);

75
76
77
78
	if (argc < 2) {
		printf("Usage: %s <controllerfile>\n", argv[0]);
		printf("\n");
		printf("E.g.: %s dt_ctrl.ctrl\n", argv[0]);
79
		goto err_init;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
80
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
81
82
83
#endif

	log_server_start(CTRL_LOG_PORT, LOG_T_DEBUG, LOG_T_INFO);
84
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
85
#ifndef HAVE_EMBEDDED
86
87
	ctrl_filename = argv[1];
	
88
89
	controller_load_variable_int_set("trace_server", 1);
	
90
	/* Create and link blocks */
91
92
	log_send(LOG_T_DEBUG, "Going to load controller file '%s'", 
	    ctrl_filename);
93
	if (controller_load(ctrl_filename)) {
94
		log_send(LOG_T_ERROR, "Could not load controller file");
95
		goto err_init;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
96
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
97
#else
Jeroen Vreeken's avatar
Jeroen Vreeken committed
98
	log_send(LOG_T_DEBUG, "Going to load embedded controller file");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
99
	if (controller_load_mem("ctrl_embedded",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
100
	    &_binary_controller_ctrl_embedded_ctrl_start,
Jeroen Vreeken's avatar
Jeroen Vreeken committed
101
102
	    &_binary_controller_ctrl_embedded_ctrl_end - &_binary_controller_ctrl_embedded_ctrl_start))
		goto err_init;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
103

Jeroen Vreeken's avatar
Jeroen Vreeken committed
104
105
	if (controller_block_link())
		goto err_init;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
106
#endif
Jeroen Vreeken's avatar
Jeroen Vreeken committed
107

Daan Vreeken's avatar
Daan Vreeken committed
108
	if (controller_time_process() != 0) {
109
110
111
		goto err_init;
	}

112
113
114
	blocks = controller_block_nr();
	outputs = 0;
	for (i = 0; i < blocks; i++) {
115
		outputs += controller_block_get(i)->outputs;
116
	}
117
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
118
#ifdef HAVE_TCP
119
120
	if (controller_load_variable_int_get("trace_server"))
		controller_trace_server_start(CTRL_TRACE_PORT, outputs);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
121

Jeroen Vreeken's avatar
Jeroen Vreeken committed
122
123
124
125
	controller_load_variable_int_set("shell", 0);
	controller_load_variable_string_set("shell_intro", shell_intro);
	controller_load_variable_string_set("shell_prompt", shell_prompt);
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
126
	/* Start command shell */
127
128
129
	if (controller_load_variable_int_get("shell")) {
		controller_sample_shell_add();
		controller_load_shell_add();
Jeroen Vreeken's avatar
Jeroen Vreeken committed
130

131
132
		shell_intro = controller_load_variable_string_get("shell_intro");
		shell_prompt = controller_load_variable_string_get("shell_prompt");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
133
	
134
135
		shell_server_start(CTRL_SHELL_PORT, 10);
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
136
#endif
Jeroen Vreeken's avatar
Jeroen Vreeken committed
137

Jeroen Vreeken's avatar
Jeroen Vreeken committed
138
#ifdef HAVE_FILEIO
139
140
141
	asprintf(&dot_filename, "%s.dot", ctrl_filename);
	controller_dumpdot(dot_filename);
	free(dot_filename);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
142
#endif
Jeroen Vreeken's avatar
Jeroen Vreeken committed
143
144
145

	/* Start 'sample' */

146
	if (controller_block_sample_init()) {
147
		goto err_init;
148
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
149
150
151

	controller_sample_start();

Jeroen Vreeken's avatar
Jeroen Vreeken committed
152
153
154
155
156
157
158
159
160
161
	controller_mem_usage(0, &usage_0);
	controller_mem_usage(CONTROLLER_MEM_PERIODIC_WRITE, &usage_w);
	controller_mem_usage(CONTROLLER_MEM_PERIODIC_READ, &usage_r);
	controller_mem_usage(CONTROLLER_MEM_PERIODIC_WRITE | CONTROLLER_MEM_PERIODIC_READ, &usage_rw);
	
	log_send(LOG_T_DEBUG, "Memory usage: %zd bytes unused", usage_0);
	log_send(LOG_T_DEBUG, "Memory usage: %zd bytes periodic read", usage_r);
	log_send(LOG_T_DEBUG, "Memory usage: %zd bytes periodic write", usage_w);
	log_send(LOG_T_DEBUG, "Memory usage: %zd bytes periodic read/write", usage_rw);

162
	log_send(LOG_T_DEBUG, "Entering state polling loop");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
163
	int oks_p = 0, errors_p = 0, recoverables_p = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
164
	while (1) {
165
		int oks, errors, recoverables;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
166
		
167
		controller_bus_poll_states(&oks, &errors, &recoverables);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
		if (oks != oks_p) {
			log_send(LOG_T_WARNING, 
			    "Number of OK blocks changed: %d -> %d",
			    oks_p, oks);
			oks_p = oks;
		}
		if (errors != errors_p) {
			log_send(LOG_T_WARNING, 
			    "Number of ERROR blocks changed: %d -> %d",
			    errors_p, errors);
			errors_p = errors;
		}
		if (recoverables != recoverables_p) {
			log_send(LOG_T_WARNING, 
			    "Number of RECOVERABLE blocks changed: %d -> %d",
			    recoverables_p, recoverables);
			recoverables_p = recoverables;
		}
		
187
188
189
190
		if (recoverables) {
			controller_bus_recover();
		}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
191
192
193
#ifdef HAVE_SYSTICK
		wdt_restart();
#endif
194
		sleep(1);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
195
196
197
	}

	return 0;
198
199
200
201

err_init:
	log_server_flush();
	return 1;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
202
}