console_dt_model.c 6.08 KB
Newer Older
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
/*
	Console interface to the DT model

	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2013
	Copyright Stichting C.A. Muller Radioastronomiestation, 2013

	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 <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <signal.h>
#include <ctype.h>
#include <inttypes.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <stdbool.h>
#include <glib.h>

#include "tcp_connect.h"
#include "tcp_listen.h"
#include "dt_model.h"

#include "dt_port_numbers.h"

47
48
#include "command_server.h"

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
int stat_port = CONSOLE_DT_MODEL_STAT_PORT;
int cmd_port = CONSOLE_DT_MODEL_CMD_PORT;


#define OUTPUT_RATE 3


/************************************/

static int load_ini(void)
{
	GKeyFile *keyfile;
	gboolean loaded;
	gchar *keyfilename = "dt_model.ini";
	int ret = 0;
	int i;
	char *active_name = "none";
	struct dt_model_params params = {};
	
	keyfile = g_key_file_new();
	
	loaded = g_key_file_load_from_file(
	    keyfile,
	    keyfilename,
	    G_KEY_FILE_KEEP_COMMENTS,
	    NULL);
	
	if (loaded) {
		gsize groups_nr;
		gchar **groups;
		
		groups = g_key_file_get_groups(keyfile, &groups_nr);

		printf("Number of groups in ini file: %d\n", (int)groups_nr);
		
		for (i = 0; i < groups_nr; i++) {
			if (!strcmp(groups[i], "global")) {
				if (g_key_file_has_key(keyfile, groups[i], "active", NULL)) {
					active_name =
					    g_key_file_get_string(keyfile, groups[i], "active", NULL);
					printf("Active params: '%s'\n", active_name);
				}
			}
		}
		for (i = 0; i < groups_nr; i++) {
			if (strcmp(groups[i], "global")) {
				if (g_key_file_has_key(keyfile, groups[i], "a0", NULL)) {
					params.a0 =
					    g_key_file_get_double(keyfile, groups[i], "a0", NULL);
				}
				if (g_key_file_has_key(keyfile, groups[i], "c1", NULL)) {
					params.c1 =
					    g_key_file_get_double(keyfile, groups[i], "c1", NULL);
				}
				if (g_key_file_has_key(keyfile, groups[i], "c2", NULL)) {
					params.c2 =
					    g_key_file_get_double(keyfile, groups[i], "c2", NULL);
				}
				if (g_key_file_has_key(keyfile, groups[i], "e0", NULL)) {
					params.e0 =
					    g_key_file_get_double(keyfile, groups[i], "e0", NULL);
				}
				if (g_key_file_has_key(keyfile, groups[i], "b", NULL)) {
					params.b =
					    g_key_file_get_double(keyfile, groups[i], "b", NULL);
				}
				if (g_key_file_has_key(keyfile, groups[i], "za", NULL)) {
					params.za =
					    g_key_file_get_double(keyfile, groups[i], "za", NULL);
				}
				if (g_key_file_has_key(keyfile, groups[i], "aa", NULL)) {
					params.aa =
					    g_key_file_get_double(keyfile, groups[i], "aa", NULL);
				}
				
				strncpy(params.name, groups[i], DT_MODEL_NAME_SIZE);
				params.name[DT_MODEL_NAME_SIZE-1] = 0;
				
				if (!strcmp(groups[i], active_name)) {
					printf("Set params: %d\n", 
					    dt_model_params_set(&params));
				}
			}
		}
	} else {
		printf("ini file load failed\n");
		ret = -1;
	}

	g_key_file_free(keyfile);
	
	return ret;
}



/************************************/

struct stat_client {
	struct stat_client *next;
	int fd;
};

struct stat_client *stat_clients = NULL;




void new_stat_client(int fd_stat)
{
	struct stat_client *new_stat;
	int fd;
	
	fd = tcp_accept(fd_stat);
	if (fd < 0)
		return;
	ioctl(fd, FIONBIO, &(int){ 1 });
	
	new_stat = malloc(sizeof(struct stat_client));
	if (!new_stat) {
		close(fd);
		return;
	}
	
	new_stat->fd = fd;
	new_stat->next = stat_clients;
	stat_clients = new_stat;
}


179
static int handle_cmd(char *name, char *val)
180
{
181
182
183
	printf("name %s value %s\n", name, val);
	if (!strcmp(name, "reload"))
		load_ini();
184

185
	return 0;
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
}


void output(void)
{
	static time_t last = 0;
	time_t now;
	struct stat_client **stat_clientp, **nextp;
	char statline[800];
	char *statlinepos;
	struct dt_model_params params;
	
	now = time(NULL);
	if (now <= last)
		return;
	
	last = now;
	
	statlinepos = statline;

	dt_model_params_get(&params);

	sprintf(statlinepos, 
	    "name=%s,a0=%f,c1=%f,c2=%f,e0=%f,b=%f,za=%f,aa=%f\n",
	    params.name, params.a0, params.c1, params.c2, params.e0,
	    params.b, params.za, params.aa);

	for (stat_clientp = &stat_clients; *stat_clientp; stat_clientp = nextp){
		int ret;
		
		ret = write((*stat_clientp)->fd, statline, strlen(statline));
		if (ret <= 0) {
			struct stat_client *tmp;
			
			tmp = *stat_clientp;
			close((*stat_clientp)->fd);
			*stat_clientp = (*stat_clientp)->next;
			free(tmp);
			nextp = stat_clientp;
		} else {
			nextp = &(*stat_clientp)->next;
		}
	}
}


int main(int argc, char **argv)
{
234
235
	int fd_stat;
	struct command_server *cmd_srv;
236
237
238
239
240
241
242
	
	dt_model_init_server();
	
	load_ini();

	signal(SIGPIPE, SIG_IGN);

243
244
	cmd_srv = command_server_create(cmd_port, 0, 100);
	if (!cmd_srv) {
245
246
		printf("Could not open listen port for commands\n");
	} else {
247
		command_server_handler_set(cmd_srv, handle_cmd);
248
	}
249

250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
	fd_stat = tcp_listen(stat_port, 0, 100);
	if (fd_stat < 0) {
		printf("Could not open listen port for status\n");
	} else {
		ioctl(fd_stat, FIONBIO, &(int){ 1 });
	}

	while (1)
	{
		int high = 0;
		fd_set fdset_rx;
		struct timeval tv;


		FD_ZERO(&fdset_rx);
	
		if (fd_stat >= 0) {
			FD_SET(fd_stat, &fdset_rx);
			if (fd_stat >= high)
				high = fd_stat + 1;
		}
271
		command_server_fdset_add(cmd_srv, &fdset_rx, &high);
272
273
274
275
276
277
278
279
280
		
		tv.tv_sec = OUTPUT_RATE;
		tv.tv_usec = 0;

		select(high, &fdset_rx, NULL, NULL, &tv);

		if (FD_ISSET(fd_stat, &fdset_rx)) {
			new_stat_client(fd_stat);
		}
281
282

		command_server_fdset_handle(cmd_srv, &fdset_rx);
283
284
285
286
		
		output();
	}
}