aboutsummaryrefslogtreecommitdiff
path: root/aepd/websocket-protocol.c
blob: 666827d96635def36279b498dcc4e4477a1cb950 (plain)
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
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
/*
 * Author: Andy Green <andy.green@linaro.org> 
 * Copyright (C) 2012 Linaro, LTD
 * Libwebsocket demo code (C) 2010-2012 Andy Green <andy@warmcat.com>
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 */

#include "aepd.h"

/*
 * We take a strict whitelist approach to stop ../ attacks
 */

struct serveable {
	const char *urlpath;
	const char *mimetype;
}; 

static const struct serveable whitelist[] = {
	{ "/favicon.ico", "image/x-icon" },
	{ "/linaro-logo-32.png", "image/png" },
	{ "/caliper.png", "image/png" },
	{ "/trigger.png", "image/png" },
	{ "/indicator-bg.png", "image/png" },
	{ "/indicator-triggered.png", "image/png" },

	/* last one is the default served if no match */
	{ "/aepscope.html", "text/html" },
};

/* this protocol server (always the first one) just knows how to do HTTP */

static int callback_http(struct libwebsocket_context *context,
		struct libwebsocket *wsi,
		enum libwebsocket_callback_reasons reason, void *user,
							   void *in, size_t len)
{
	int n;
	char buf[512];

	switch (reason) {
	case LWS_CALLBACK_HTTP:

		for (n = 0; n < (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++)
			if (in && strcmp(in, whitelist[n].urlpath) == 0)
				break;

		sprintf(buf, LOCAL_RESOURCE_PATH"%s", whitelist[n].urlpath);

		if (libwebsockets_serve_http_file(context, wsi, buf,
				whitelist[n].mimetype, NULL, 0))
			fprintf(stderr, "Failed to send HTTP file\n");
		break;

	default:
		break;
	}

	return 0;
}



/* linaro_aepd_protocol */

struct per_session_data__linaro_aepd {
	struct libwebsocket *wsi;
	long ringbuffer_tail;
	double sam[MAX_PROBES * CHANNELS_PER_PROBE * 3];
	int sam_valid;
	int stride;
	int channels_sent_flag;
	int issue_timestamp;
	int viewport_budget;
	double viewport_offset_time;
	long caliper_offset[2];
	double caliper_time[2];
	unsigned long ms10_last_caliper;
	unsigned long ms10_last_triglevel;
	int seen_rate;
};

extern struct aep_context aep_context;

static int
callback_linaro_aepd(struct libwebsocket_context *context,
			struct libwebsocket *wsi,
			enum libwebsocket_callback_reasons reason,
					       void *user, void *in, size_t len)
{
	int n, m;
	struct per_session_data__linaro_aepd *pss = user;
	double sam[MAX_PROBES * CHANNELS_PER_PROBE * 3];
	double sam2[MAX_PROBES * CHANNELS_PER_PROBE * 3];
	double d[10];
	char buf[LWS_SEND_BUFFER_PRE_PADDING + 16384 + LWS_SEND_BUFFER_POST_PADDING];
	char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
	int budget = 10;
	int no_valid_sam_flag = 0;
	long extent;
	long l;
	struct timeval tv;
	unsigned long ms10;

	switch (reason) {

	case LWS_CALLBACK_ESTABLISHED:
		pss->ringbuffer_tail = aepd_shared->fifo_pos;
		pss->sam_valid = 0;
		pss->wsi = wsi;
		pss->stride = 100;
		pss->channels_sent_flag = 0;
		pss->issue_timestamp = 1;
		pss->viewport_offset_time = 0;
		pss->viewport_budget = 0;
		pss->caliper_offset[0] = -1;
		pss->caliper_offset[1] = -1;
		pss->seen_rate = 0;
		break;

	case LWS_CALLBACK_SERVER_WRITEABLE:

		if (aepd_shared->chans < 0)
			break;

		gettimeofday(&tv, NULL);
		ms10 = (tv.tv_sec * 10) + (tv.tv_usec / 100000);

		/*
		 * is it time to do a caliper update?
		 */

		if (ms10 != pss->ms10_last_caliper && aepd_shared->chans) {

			pss->ms10_last_caliper = ms10;

			if (pss->caliper_time[0] > aepd_shared->fifo_head_time ||
				  pss->caliper_time[1] > aepd_shared->fifo_head_time)
				goto bad_caliper;

			if (pss->caliper_time[0] < 0 ||
				  pss->caliper_time[1] < 0)
				goto bad_caliper;

			l = pss->ringbuffer_tail - pss->caliper_offset[0];
			if (l < 0)
				l += aepd_shared->modulo_integer_chan_size;
			if (l < 0)
				goto bad_caliper;
			if (lseek(aepd_shared->fd_fifo_read, l, SEEK_SET) < 0)
				fprintf(stderr, "seek %ld failed\n", l);
			if (sizeof(sam) < (aepd_shared->chans * sizeof(double) * 3))
				fprintf(stderr, "reading too much %d\n", aepd_shared->chans);
			if (read(aepd_shared->fd_fifo_read, &sam[0],
					aepd_shared->chans * sizeof(double) * 3) < 0)
				fprintf(stderr, "fifo read %ld failed\n", l);

			l = pss->ringbuffer_tail - pss->caliper_offset[1];
			if (l < 0)
				l += aepd_shared->modulo_integer_chan_size;
			if (l < 0)
				goto bad_caliper;
			if (lseek(aepd_shared->fd_fifo_read, l, SEEK_SET) < 0)
				fprintf(stderr, "seek %ld failed\n", l);
			if (sizeof(sam2) < (aepd_shared->chans * sizeof(double) * 3))
				fprintf(stderr, "reading too much %d\n", aepd_shared->chans);
			if (read(aepd_shared->fd_fifo_read, &sam2[0],
					aepd_shared->chans * sizeof(double) * 3) < 0)
				fprintf(stderr, "fifo read %ld fail\n", l);

			extent = (pss->caliper_offset[1] - pss->caliper_offset[0]) /
						(aepd_shared->chans * sizeof(double) * 3);

			*p++ = 'c';
			p += sprintf(p, "%f,", aepd_shared->fifo_head_time);
			m = 0;
			for (n = 0; n < aepd_shared->chans; n++) {

				p += sprintf(p, "%f %f %f",
					(sam[m] - sam2[m]) / (double)extent,
					(sam[m + 1] - sam2[m + 1]) / (double)extent,
					(sam[m + 2] - sam2[m + 2]) / (double)extent
				);

				if (n + 1 != aepd_shared->chans) {
					*p++ = ',';
					*p = '\0';
				}
				m += 3;
			}

			*p = '\0';
			goto send;
bad_caliper:
			*p++ = 'C';
			*p = '\0';
			goto send;
		}

		if (ms10 != pss->ms10_last_triglevel && aepd_shared->chans) {
			pss->ms10_last_caliper = ms10;
		}

		/*
		 * do we need to issue a sync timestamp?
		 */

		if (pss->issue_timestamp && aepd_shared->chans) {
			pss->issue_timestamp = 0;

			/*
			 * we want to set the sample time context for what we are
			 * about to send
			 */

			pss->sam_valid = 0;

			l = pss->ringbuffer_tail;

			if (l <= (long)aepd_shared->fifo_pos)
				extent = aepd_shared->fifo_pos - l;
			else
				extent = (aepd_shared->modulo_integer_chan_size - l) + aepd_shared->fifo_pos;

			extent /= aepd_shared->chans * sizeof(double) * 3;


			p += sprintf(p, "t%f %f %f %d %d", aepd_shared->fifo_head_time - ((double)extent * 0.0001), aepd_shared->fifo_tail_time, aepd_shared->fifo_head_time, aepd_shared->stop_flag ^ 1, pss->viewport_budget);
			goto send;
		}

		/*
		 * do we need to dump channel info?
		 */

		if (!pss->channels_sent_flag && aepd_shared->chans) {

			/* signal it's a message with channel names */
			*p++ = '=';

			for (n = 0; n < aep_context.aepd_interface->chans + aep_context.aepd_interface->vchans; n++)
				p += sprintf(p, "%s,%s,%s,%s,;",
					aep_context.aepd_interface->channel_name[n],
					aep_context.aepd_interface->supply[n],
					aep_context.aepd_interface->colour[n],
					aep_context.aepd_interface->class[n]);

			pss->channels_sent_flag = 1;

			goto send;
		}

		/*
		 * if we're not following the samples head, we don't need to
		 * spam the viewport any more than one viewport's worth of samples.
		 *
		 * We're still collecting samples, he can get them by moving his
		 * viewport offset
		 */

		if (pss->viewport_offset_time < -0.00009)
			if (!pss->viewport_budget)
				break;

		/*
		 * if we didn't have to do any of the other tasks,
		 * aggregate up to 'budget' pending results in one websocket message
		 */

		if (!pss->seen_rate || !aepd_shared->chans)
			return 0;

		while (budget-- && (p - &buf[LWS_SEND_BUFFER_PRE_PADDING]) < (sizeof(buf) - 4096)) {

			if (pss->ringbuffer_tail <= (long)aepd_shared->fifo_pos)
				extent = aepd_shared->fifo_pos - pss->ringbuffer_tail;
			else
				extent = (aepd_shared->modulo_integer_chan_size - pss->ringbuffer_tail) + aepd_shared->fifo_pos;

			if (pss->ringbuffer_tail >= 0 && extent < (pss->stride * aepd_shared->chans * sizeof(double) * 3)) {
				budget = 0;
				continue;
			}

			if (pss->ringbuffer_tail < 0) {
				no_valid_sam_flag = 1;
				pss->sam_valid = 0;
				/* force a 0 sample */
				for (n = 0; n < aepd_shared->chans * 3; n++)
					sam[n] = pss->sam[n];
			} else {
				no_valid_sam_flag = 0;
				lseek(aepd_shared->fd_fifo_read, pss->ringbuffer_tail, SEEK_SET);
				if (read(aepd_shared->fd_fifo_read, &sam[0], aepd_shared->chans * sizeof(double) * 3) < 0)
					fprintf(stderr, "fifo read fail\n");
			}

			if (pss->viewport_budget)
				pss->viewport_budget--;
			else
				if (pss->viewport_offset_time < -0.00009)
					goto send;

			pss->ringbuffer_tail += (pss->stride * aepd_shared->chans * sizeof(double) * 3 );
			if (pss->ringbuffer_tail > (long)aepd_shared->modulo_integer_chan_size)
				pss->ringbuffer_tail -=  aepd_shared->modulo_integer_chan_size;

			if (pss->sam_valid || no_valid_sam_flag) {

				/*
				 * Javascript can't cope with binary, so we must ascii-fy it
				 */

				m = 0;
				for (n = 0; n < aepd_shared->chans; n++) {

					if ((sam[m] - pss->sam[m]) < 0)
						fprintf(stderr, "sam[m] %lf < pss->sam[m] %lf\n", sam[m], pss->sam[m]);

					p += sprintf(p, "%f %f %f",
						(sam[m] - pss->sam[m]) / (double)pss->stride,
						(sam[m + 1] - pss->sam[m + 1]) / (double)pss->stride,
						(sam[m + 2] - pss->sam[m + 2]) / (double)pss->stride
					);

					if (n + 1 != aepd_shared->chans) {
						*p++ = ',';
						*p = '\0';
					}
					m += 3;
				}

				*p++ = ';';
				*p = '\0';
			}
			memcpy(&pss->sam[0], &sam[0], aepd_shared->chans * sizeof(double) * 3);
			if (!no_valid_sam_flag)
				pss->sam_valid = 1;


			if ((p - &buf[LWS_SEND_BUFFER_PRE_PADDING]) > (sizeof(buf) - 2048)) {
				fprintf(stderr, "insane buffer usage %d! pss->ringbuffer_tail = %ld, budget=%d aepd_shared->chans=%d\n",
					(int)(p - &buf[LWS_SEND_BUFFER_PRE_PADDING]), pss->ringbuffer_tail, budget, aepd_shared->chans);
				budget = 0;
			}
		}
send:
		/*
		 * if we generated something, send it.  We are guaranteed not to block
		 * because we got here by POLLOUT seen on the socket
		 */

		if (p != &buf[LWS_SEND_BUFFER_PRE_PADDING]) {
			n = libwebsocket_write(wsi, (unsigned char *)
				   &buf[LWS_SEND_BUFFER_PRE_PADDING],
				   p - &buf[LWS_SEND_BUFFER_PRE_PADDING], LWS_WRITE_TEXT);
			if (n < 0) {
				fprintf(stderr, "ERROR writing to socket");
				return 1;
			}
			libwebsocket_callback_on_writable(context, wsi);
		}

		break;

	case LWS_CALLBACK_RECEIVE:

		switch (*(char *)in) {
		case 'Z': /* zero */
			fprintf(stderr, "zero\n");
			aepd_shared->zero = 1;
			break;
		case 't': /* set trigger level */
			if (sscanf(((char *)in) + 1, "%lf\n", &d[0]) == 1) {
				aepd_shared->trigger_level = d[0];
				/* set it */
			}
			break;

		case 'c': /* calipers changed */
			if (sscanf(((char *)in) + 1, "%lf %lf\n", &d[0], &d[1]) == 2) {

				if (d[0] != d[0] || d[1] != d[1]) { /* NaN */
					pss->caliper_offset[0] = -1;
					pss->caliper_time[0] = -1;
					pss->caliper_offset[1] = -1;
					pss->caliper_time[1] = -1;
					break;
				}

				if (d[0] > d[1]) {
					d[2] = d[1];
					d[1] = d[0];
					d[0] = d[2];
				}

				/*
				 * caliper positions in seconds behind rhs -->
				 * byte offset behind rhs in ringbuffer
				 */
				for (n = 0; n < 2; n++) {
					pss->caliper_offset[n] = (d[n] * 10000 * aepd_shared->chans * sizeof(double) * 3);
					pss->caliper_time[n] = d[n];
				}
			} else
				fprintf(stderr, "caliper sscanf failed\n");
			break;

		case 'r': /* rate or other change */
			if (sscanf(((char *)in) + 1, "%lf %lf %lf %lf\n", &d[0], &d[1], &d[2], &d[3]) == 4) {
				pss->stride = (int)d[0];
				pss->viewport_offset_time = d[3];

				l = aepd_shared->fifo_pos - ((int)d[1] * pss->stride * aepd_shared->chans * sizeof(double) * 3);
				l += (int)((pss->viewport_offset_time / 0.0001)) * aepd_shared->chans * sizeof(double) * 3;
				if (pss->viewport_offset_time)
					pss->viewport_budget = d[1];
				pss->ringbuffer_tail = l;
				aepd_shared->stop_flag = ((int)d[2]) ^ 1;
				pss->issue_timestamp = 1;
				pss->seen_rate = 1;
				pss->sam_valid = 0;
				libwebsocket_callback_on_writable(context, wsi);
			} else
				fprintf(stderr, "sscanf failed\n");
			break;
		}
		break;

	default:
		break;
	}

	return 0;
}

/* list of supported protocols and callbacks */

struct libwebsocket_protocols protocols[] = {
	/* first protocol must always be HTTP handler */

	{
		"http-only",		/* name */
		callback_http,		/* callback */
		0			/* per_session_data_size */
	},
	{
		"linaro.aepd",
		callback_linaro_aepd,
		sizeof(struct per_session_data__linaro_aepd),
	},
	{
		NULL, NULL, 0		/* End of list */
	}
};