console_easycomm.c 10.4 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
/*
	console_easycomm.c
	
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007
	Copyright Stichting C.A. Muller Radioastronomiestation, 2007

	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 <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <math.h>
#include <sys/select.h>
#include <time.h>
#include <errno.h>
#include <signal.h>

Jeroen Vreeken's avatar
Jeroen Vreeken committed
35
36
37
#include <tcp_connect.h>
#include <tcp_listen.h>
#include <config.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475

#include "setpoint.h"

typedef union {
	float f;
	uint32_t u;
} f32_t;

int easycomm_local = 0;
int easycomm_port = 13000;

char *controller_trace_host = "localhost";
int controller_trace_port = 10000;
char *controller_trace_azimuth = "Azimuth_Position";
char *controller_trace_elevation = "Elevation_Position";

char *controller_command_host = "localhost";
int controller_command_port = 11000;
char *controller_command_azimuth = "Azimuth_Setpoint";
char *controller_command_elevation = "Elevation_Setpoint";

char *controller_trace_unit;

struct traceval {
	time_t seconds;
	int sample;
	float value;
	int wasnan;
	int new;
	float newval;
};

struct traceval traceval_az = { 0, -1, 0.0, 0, 0, 0.0 };
struct traceval traceval_el = { 0, -1, 0.0, 0, 0, 0.0 };

int controller_command_fd = -1;

#define VERSION "CAMRAS-console_easycomm-0.1"

#define RX_BUFSIZE 4096
#define TX_BUFSIZE 4096

struct easycomm_conn {
	struct easycomm_conn *next;
	
	int fd;
	char rxbuffer[RX_BUFSIZE];
	char txbuffer[TX_BUFSIZE];
	size_t rxsize;
	size_t txsize;
};

struct easycomm_conn *easycomm_connections = NULL;

struct setpoint_command *sp_command_az = NULL;
struct setpoint_command *sp_command_el = NULL;

int init_trace(int fd, char *trace_name)
{
	char name[100];
	char prevname[100];
	int len = 0;
	int unit = 0;
	int ret;
	
	do {
		ret = read(fd, name + len, 1);
		if (ret == 1) {
			if (name[len] == 0) {
				if (len == 0)
					break;
				if (!unit) {
					strcpy(prevname, name);
				} else {
					if (!strcmp(prevname, trace_name)) {
						controller_trace_unit =
						     malloc(strlen(name) + 1);
						if (controller_trace_unit) {
							strcpy(
							    controller_trace_unit,
							    name);
						} else {
							controller_trace_unit = "?";
						}
					}
				}
				len = -1;
				unit = !unit;
			}
			len++;
			if (len >= 100) {
				len = 0;
			}
		}
	} while (ret == 1);
	
	write(fd, trace_name, strlen(trace_name) + 1);

	return 0;
}

int handle_trace(int fd, struct traceval *trc)
{
	int ret;
	uint32_t nin;
	float fin;
	f32_t f;
	
	ret = read(fd, &nin, sizeof(uint32_t));
	if (ret != sizeof(uint32_t)) {
		close(fd);
		return -1;
	}
	nin = ntohl(nin);
	f.u = nin;
	fin = f.f;
	if (trc->wasnan) {
		trc->seconds = nin;
		trc->sample = 0;
		trc->wasnan = 0;
	} else if (isnan(fin)) {
		trc->wasnan = 1;
	} else {
		if (trc->seconds != 0)
			trc->value = fin;
		if (trc->sample == 0) {
			trc->new = 1;
			trc->newval = fin;
		}
		trc->sample++;
	}
	return 0;
}


int easycomm_output(void)
{
	struct tm trace_time;
	char ec_line[100];
	struct easycomm_conn *conn;
	
	if (!traceval_az.new || !traceval_el.new)
		return 0;
	if (traceval_az.seconds != traceval_el.seconds)
		return 0;
	gmtime_r(&traceval_az.seconds, &trace_time);

	sprintf(ec_line, "ST%04d:%02d:%02d:%02d:%02d:%02d AZ%f EL%f\n",
	    trace_time.tm_year+1900, trace_time.tm_mon+1, trace_time.tm_mday,
	    trace_time.tm_hour, trace_time.tm_min, trace_time.tm_sec,
	    traceval_az.newval * 180.0 / M_PI,
	    traceval_el.newval * 180.0 / M_PI);

	for (conn = easycomm_connections; conn; conn = conn->next)
	{
		if (conn->txsize + strlen(ec_line) <= TX_BUFSIZE) {
			memcpy(conn->txbuffer + conn->txsize, ec_line,
			    strlen(ec_line));
			conn->txsize += strlen(ec_line);
		}
	}
	
	traceval_az.new = 0;
	traceval_el.new = 0;

	return 0;
}

int handle_ec_new(int fd_listen)
{
	int fd;
	struct easycomm_conn *conn;

	fd = tcp_accept(fd_listen);
	if (fd < 0)
		return fd;

	printf("Accepted new client connection.\n");
	
	conn = malloc(sizeof(struct easycomm_conn));
	if (!conn) {
		return -1;
		close(fd);
	}

	conn->fd = fd;
	conn->rxsize = 0;
	sprintf(conn->txbuffer, "VE%s\n", VERSION);
	conn->txsize = strlen(conn->txbuffer);
	
	conn->next = easycomm_connections;
	easycomm_connections = conn;
	
	return 0;
}

int handle_ec_send(void)
{
	struct easycomm_conn **connp, **next;
	
	for (connp = &easycomm_connections; *connp; connp = next) {
		struct easycomm_conn *conn = *connp;
		int ret;
		
		next = &conn->next;
		
		if (conn->txsize == 0)
			continue;
		
		ret = write(conn->fd, conn->txbuffer, conn->txsize);
		if (ret <= 0 && errno != EAGAIN) {
			printf("Closing client socket\n");
			close(conn->fd);
			*connp = conn->next;
			next = connp;
			free(conn);
		} else {
			if (ret == conn->txsize) {
				conn->txsize = 0;
			} else if (ret > 0) {
				memmove(conn->txbuffer,
				    conn->txbuffer + ret,
				    conn->txsize - ret);
				conn->txsize -= ret;
			}
		}
	}

	return 0;
}

void handle_ec_command(struct easycomm_conn *conn)
{
	conn->rxbuffer[conn->rxsize] = 0;
	
	if (!strncmp(conn->rxbuffer, "AZ", 2)) {
		float value_deg;
		float value;
		
		value_deg = atof(conn->rxbuffer + 2);
		
		value = value_deg / 180.0 * M_PI;
		printf("New setpoint: %e (%e)\n", value, value_deg);
		
		setpoint_command_setpoint(sp_command_az, value);
	}
	if (!strncmp(conn->rxbuffer, "EL", 2)) {
		float value_deg;
		float value;
		
		value_deg = atof(conn->rxbuffer + 2);
		
		value = value_deg / 180.0 * M_PI;
		printf("New setpoint: %e (%e)\n", value, value_deg);
		
		setpoint_command_setpoint(sp_command_el, value);
	}
}

int handle_ec_recv(void)
{
	struct easycomm_conn **connp, **next;
	
	for (connp = &easycomm_connections; *connp; connp = next) {
		struct easycomm_conn *conn = *connp;
		int ret = 1;
		
		next = &conn->next;
		
		if (conn->rxsize < RX_BUFSIZE) do {
			ret = read(conn->fd, conn->rxbuffer + conn->rxsize, 1);
			if (ret == 1) {
				conn->rxsize += 1;
				switch(conn->rxbuffer[conn->rxsize -1]) {
					case ' ':
					case '\t':
					case '\n':
					case '\r':
						handle_ec_command(conn);
						conn->rxsize = 0;
				}
			}
		} while (ret > 0 && conn->rxsize < RX_BUFSIZE);
		if (ret <= 0 && errno != EAGAIN) {
			close(conn->fd);
			*connp = conn->next;
			next = connp;
			free(conn);
		}
		if (conn->rxsize == RX_BUFSIZE)
			conn->rxsize = 0;
	}
	
	return 0;
}

int main(int argc, char **argv)
{
	int fd_trace_az, fd_trace_el;
	int fd_ec_listen;
	int ret;
	fd_set fdset_rx;
	int high;
	
	printf("Controller console interface using Easycomm starting...\n");
	
	signal(SIGPIPE, SIG_IGN);

	config_add("controller_trace_host", config_string, 
	    &controller_trace_host);
	config_add("controller_trace_port", config_int, 
	    &controller_trace_port);
	config_add("controller_trace_azimuth", config_string,
	    &controller_trace_azimuth);
	config_add("controller_command_host", config_string,
	    &controller_command_host);
	config_add("controller_command_port", config_int,
	    &controller_command_port);
	config_add("controller_command_azimuth", config_string,
	    &controller_command_azimuth);
	config_add("easycomm_port", config_int,
	    &easycomm_port);
	config_add("easycomm_local", config_int,
	    &easycomm_local);

	if (argc >= 2) {
		printf("Reading config file '%s'\n", argv[1]);
		config_read(argv[1]);
	}
	
	
	fd_trace_az = tcp_connect(controller_trace_host, controller_trace_port);
	if (fd_trace_az >= 0) {
		init_trace(fd_trace_az, controller_trace_azimuth);
		printf("Controller trace is in '%s'.\n", controller_trace_unit);
	}
	fd_trace_el = tcp_connect(controller_trace_host, controller_trace_port);
	if (fd_trace_el >= 0) {
		init_trace(fd_trace_el, controller_trace_elevation);
		printf("Controller trace is in '%s'.\n", controller_trace_unit);
	}

	sp_command_az = setpoint_command_init(controller_command_host,
	    controller_command_port, controller_command_azimuth,
	    "console/easycomm");
	sp_command_el = setpoint_command_init(controller_command_host,
	    controller_command_port, controller_command_elevation,
	    "console/easycomm");

	printf("Going to listen for Easycomm clients on port %d.\n",
	    easycomm_port);
	printf("Connections from %s will be accepted.\n",
	   easycomm_local ? "local clients only" : "any clients");
	fd_ec_listen = tcp_listen(easycomm_port, easycomm_local, 10);
	if (fd_ec_listen < 0) {
		return 0;
	}
	
	printf("Starting main loop.\n");

	do {
		high = 0;
		FD_ZERO(&fdset_rx);
		
		if (fd_trace_el >= 0 && fd_trace_az >= 0) {
			FD_SET(fd_trace_az, &fdset_rx);
			if (fd_trace_az > high)
				high = fd_trace_az;
			FD_SET(fd_trace_el, &fdset_rx);
			if (fd_trace_el > high)
				high = fd_trace_el;
		
			FD_SET(fd_ec_listen, &fdset_rx);
			if (fd_ec_listen > high)
				high = fd_ec_listen;
		
			ret = select(high + 1, &fdset_rx, NULL, NULL, NULL);

			if (FD_ISSET(fd_trace_az, &fdset_rx)) {
				ret = handle_trace(fd_trace_az, &traceval_az);
				if (ret < 0) {
					fd_trace_az = -1;
					printf("Trace connection lost.\n");
				}
				
			}
			if (FD_ISSET(fd_trace_el, &fdset_rx)) {
				ret = handle_trace(fd_trace_el, &traceval_el);
				if (ret < 0) {
					fd_trace_el = -1;
					printf("Trace connection lost.\n");
				}
				
			}
			easycomm_output();
		
			if (FD_ISSET(fd_ec_listen, &fdset_rx)) {
				handle_ec_new(fd_ec_listen);
			}
			
			handle_ec_recv();
		} else {
			if (fd_trace_az < 0) {
				do {
					sleep(1);
					fd_trace_az = tcp_connect(
					    controller_trace_host,
					    controller_trace_port);
				} while (fd_trace_az < 0);

				init_trace(fd_trace_az,
				    controller_trace_azimuth);
				printf("Controller trace is in '%s'.\n", 
				    controller_trace_unit);
			}
			if (fd_trace_el < 0) {
				do {
					sleep(1);
					fd_trace_el = tcp_connect(
					    controller_trace_host,
					    controller_trace_port);
				} while (fd_trace_el < 0);

				init_trace(fd_trace_el,
				    controller_trace_elevation);
				printf("Controller trace is in '%s'.\n", 
				    controller_trace_unit);
			}
		}
		
		handle_ec_send();
	} while (1);

	close(fd_trace_az);
	close(fd_trace_el);

	return 0;
}