controller_trace.c 8.37 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
/*
Jeroen Vreeken's avatar
Jeroen Vreeken committed
2
3
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007, 2013
	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
19
20
21
22
23
24
25
26
27
28
29
30
31
32

	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 <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#include <errno.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
33
#include <limits.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
34
35
36
37

#include <controller/controller_block.h>
#include <controller/controller_trace.h>
#include <controller/controller_sample.h>
38
#include "log.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

static int nr_traces = 0;
static struct controller_trace_name *trace_list = NULL;

struct controller_trace_name *controller_trace_get(int nr)
{
	return &trace_list[nr];
}

int controller_trace_nr(void)
{
	return nr_traces;
}

void controller_trace_list_add(struct controller_trace_name *trace)
{
	struct controller_trace_name *tmp;

	nr_traces++;
	tmp = realloc(trace_list, sizeof(struct controller_trace) * nr_traces);
	if (tmp) {
		trace_list = tmp;
		memcpy(&trace_list[nr_traces-1], trace,
		    sizeof(struct controller_trace));
63
		log_send(LOG_T_DEBUG, "Added trace %s [%s] using %s.%s",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
		    trace->name, trace->unit, 
		    trace->blockname, trace->termname);
	} else
		nr_traces--;
}

#define TRACE_LEN	1024

enum {
	TRC_ST_NEW,
	TRC_ST_GETNAME,
	TRC_ST_TRACING,
};

struct trace_hdl {
	int free;

	int state;

	int sock;
	int tracename_len;
	char tracename[100];
	struct controller_trace trace;
	float buffer[TRACE_LEN];
};

static int nr_trace_hdl;
static struct trace_hdl *trace_hdl;

typedef union float32 {
	float f;
	uint32_t u;
} float32_t;

static int controller_trace_server_port;

static void *controller_trace_handle(void *arg)
{
	int i;

	while (1) {
		for (i = 0; i < nr_trace_hdl; i++) {
			if (trace_hdl[i].free)
				continue;
			switch(trace_hdl[i].state) {
				case TRC_ST_NEW: {
					int sock = trace_hdl[i].sock;
					int j;
					int ret = 1;
					float32_t period;
					char c;

					/* Output list of available traces
					   (extra 0 as terminator) */
					for (j = 0; j < nr_traces; j++) {
						ret = write(sock, 
						    trace_list[j].name,
						    strlen(trace_list[j].name)+1);
						if (ret <= 0)
							break;
						ret = write(sock, 
						    trace_list[j].unit,
						    strlen(trace_list[j].unit)+1);
						if (ret <= 0)
							break;
						period.f = controller_sample_period();
						period.u = htonl(period.u);
						ret = write(sock,
						    &period.u, sizeof(period.u));
						if (ret <= 0)
							break;
						c = 0;
						ret = write(sock, &c, 1);
						if (ret <= 0)
							break;
					}
					if (ret > 0) {
						c = 0;
						ret = write(sock, &c, 1);
					}
					if (ret <= 0) {
						close(sock);
						trace_hdl[i].free = 1;
					} else {
						int val = 1;

						trace_hdl[i].state =
						    TRC_ST_GETNAME;
						ioctl(sock, FIONBIO, &val);
					}

					break;
				}
				case TRC_ST_GETNAME: {
					if (trace_hdl[i].tracename_len < 100 &&
					    (trace_hdl[i].tracename_len == 0 ||
					     trace_hdl[i].tracename[trace_hdl[i].tracename_len-1])) {
						int ret;
						
						ret = read(trace_hdl[i].sock,
						    trace_hdl[i].tracename +
						    trace_hdl[i].tracename_len,
						    1);
						if (ret <= 0) {
							if (errno != EAGAIN) {
								close(trace_hdl[i].sock);
								trace_hdl[i].free = 1;
							}
							break;
						}
						if (trace_hdl[i].tracename[trace_hdl[i].tracename_len] == '\n')
							trace_hdl[i].tracename[trace_hdl[i].tracename_len] = 0;
						if (trace_hdl[i].tracename[trace_hdl[i].tracename_len] == '\r')
							trace_hdl[i].tracename[trace_hdl[i].tracename_len] = 0;
						trace_hdl[i].tracename_len++;
					} else {
						int j;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
181
182
183
						char *blockname;
						char *termname;
						bool found = false;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
184
185
186
						
						trace_hdl[i].tracename[99] = 0;
						
Jeroen Vreeken's avatar
Jeroen Vreeken committed
187
						/* Check the list of defined traces */
Jeroen Vreeken's avatar
Jeroen Vreeken committed
188
189
190
						for (j = 0; j < nr_traces; j++) {
							if (!strcmp(trace_hdl[i].tracename,
							    trace_list[j].name)) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
191
192
193
							    	blockname = trace_list[j].blockname;
								termname = trace_list[j].termname;
								found = true;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
194
195
196
								break;
							}
						}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
197
198
199
200
201
202
203
204
205
206
						/* If not found, check if it is a "block.term" format */
						if (!found) {
							blockname = trace_hdl[i].tracename;
							termname = trace_hdl[i].tracename;
							while (termname[0] && termname[0] != '.')
								termname++;
							if (termname[0] == '.') {
								termname[0] = 0;
								termname++;
							}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
207
208
209
210
211
212
						}
	
						trace_hdl[i].trace.len = TRACE_LEN;
						trace_hdl[i].trace.buffer = trace_hdl[i].buffer;
	
						if (controller_block_trace_add(
Jeroen Vreeken's avatar
Jeroen Vreeken committed
213
214
						    blockname,
						    termname,
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
						    &trace_hdl[i].trace)) {
							close(trace_hdl[i].sock);
							trace_hdl[i].free = 1;
							break;
						}
						trace_hdl[i].state = TRC_ST_TRACING;
					}
				
					break;
				}
				case TRC_ST_TRACING: {
					size_t wr_pos, rd_pos;
					size_t len;
					int ret, j;
		
					wr_pos = trace_hdl[i].trace.wr_pos;
					rd_pos = trace_hdl[i].trace.rd_pos;
		
					if (wr_pos != rd_pos) {
						if (rd_pos >= wr_pos)
							len = trace_hdl[i].trace.len - rd_pos;
						else
							len = wr_pos - rd_pos;
						for (j = rd_pos; j < rd_pos + len; j++)
							*(uint32_t *)&trace_hdl[i].trace.buffer[j] = 
							    htonl(*(uint32_t *)&trace_hdl[i].trace.buffer[j]);
						ret = write(trace_hdl[i].sock,
						    &trace_hdl[i].trace.buffer[rd_pos], 
						    len * sizeof(trace_hdl[i].trace.buffer[rd_pos]));
						if (ret <= 0 && errno != EAGAIN) {
							controller_block_trace_del(&trace_hdl[i].trace);
							close(trace_hdl[i].sock);
							trace_hdl[i].free = 1;
							break;
						}
						trace_hdl[i].trace.rd_pos += len;
						if (trace_hdl[i].trace.rd_pos >= trace_hdl[i].trace.len)
							trace_hdl[i].trace.rd_pos = 0;
					}

					break;
				}
			}
		}
		controller_block_trace_wait();
	}

	return NULL;
}

static void *controller_trace_server(void *arg)
{
	int listen_sock;
	struct sockaddr_in sockaddr;
	int val;
	
	signal(SIGPIPE, SIG_IGN);
	
	listen_sock = socket(AF_INET, SOCK_STREAM, 0);
	if (listen_sock < 0) {
		perror("socket() failed");
		return NULL;
	}
	
	sockaddr.sin_family = AF_INET;
	sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	sockaddr.sin_port = htons(controller_trace_server_port);
	
	val = 1;
	setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int));

	if (bind(listen_sock,
	    (struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0) {
		perror("bind() failed");
		close(listen_sock);
		return NULL;
	}
	
	if (listen(listen_sock, 8) < 0) {
		perror("listen() failed");
		close(listen_sock);
		return NULL;
	}
	
	while (1) {
		int client;
		struct sockaddr_in sockaddr_client;
		socklen_t len;
		
		len = sizeof(sockaddr_client);
		client = accept(listen_sock,
		    (struct sockaddr *)&sockaddr_client, &len);
		
		if (client >= 0) {
			int i;
			
			for (i = 0; i < nr_trace_hdl; i++) {
				if (!trace_hdl[i].free)
					continue;
				
				trace_hdl[i].sock = client;
				trace_hdl[i].tracename_len = 0;
				trace_hdl[i].state = TRC_ST_NEW;

				trace_hdl[i].free = 0;
				break;
			}
			if (i == nr_trace_hdl)
				close(client);
		} else {
325
326
			log_send(LOG_T_ERROR, "accept() failed: %s",
			    strerror(errno));
Jeroen Vreeken's avatar
Jeroen Vreeken committed
327
328
329
330
331
332
333
334
335
336
337
338
		}
	}

	return NULL;
}

void controller_trace_server_start(int portnr, int max)
{
	pthread_t thread_id;
	pthread_attr_t attr;
	int i;
	
339
	log_send(LOG_T_DEBUG, "Starting trace server on port %d", portnr);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

	trace_hdl = malloc(sizeof(struct trace_hdl) * max);
	if (!trace_hdl)
		return;
	memset(trace_hdl, 0, sizeof(struct trace_hdl) * max);
	nr_trace_hdl = max;
	for (i = 0; i < max; i++)
		trace_hdl[i].free = 1;
	
	controller_block_trace_init(max);

	controller_trace_server_port = portnr;

	pthread_attr_init(&attr);
	pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN * 2);
	pthread_create(&thread_id, &attr,
	    controller_trace_server, NULL);
	pthread_create(&thread_id, &attr,
	    controller_trace_handle, NULL);
}