forked from mazzoo/gigargoyle
/
packets.c
424 lines (376 loc) · 9.04 KB
/
packets.c
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
/*
* gigargoyle
*
* a nighttime composition in opensource
*
* this is part of the 2010 binkenlights installation
* in Giesing, Munich, Germany which is called
*
* a.c.a.b. - all colors are beautiful
*
* the installation is run by the Chaos Computer Club Munich
* as part of the puerto giesing
*
*
* license:
* GPL v2, see the file LICENSE
* authors:
* Matthias Wenzel - aka - mazzoo
*
*/
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "packets.h"
#include "fifo.h"
#include "gigargoyle.h"
#include "command_line_arguments.h"
/* Contains parsed command line arguments */
extern struct arguments arguments;
int in_packet(pkt_t * p, uint32_t plen)
{
if (plen < 8)
{
LOG("PKTS: WARNING: got very short packet (len %d)\n", plen);
return -1;
}
if ((p->hdr & PKT_MASK_VERSION) != VERSION << VERSION_SHIFT)
{
LOG("PKTS: WARNING: dropping pkt with invalid version, hdr %x\n", p->hdr);
return -2; /* drop wrong version packets */
}
if (plen < p->pkt_len )
{
#if 0 /* this is fine -1 is the marker for incomplete packets */
LOG("PKTS: WARNING: got short packet (%d < %d)\n",
plen, p->pkt_len
);
#endif
return -1;
}
if (p->pkt_len > FIFO_WIDTH)
{
LOG("PKTS: WARNING: got long packet (len %d)\n", plen);
return -3;
}
switch(p->hdr & PKT_MASK_TYPE)
{
case PKT_TYPE_SET_SCREEN_BLK:
case PKT_TYPE_SET_SCREEN_WHT:
case PKT_TYPE_SET_SCREEN_RND_BW:
case PKT_TYPE_SET_SCREEN_RND_COL:
case PKT_TYPE_SET_FRAME_RATE:
case PKT_TYPE_SET_FADE_RATE:
case PKT_TYPE_SET_DURATION:
case PKT_TYPE_SET_PIXEL:
case PKT_TYPE_SET_SCREEN:
case PKT_TYPE_FLIP_DBL_BUF:
case PKT_TYPE_TEXT:
case PKT_TYPE_SET_FONT:
wr_fifo(p);
break;
/* out-of-band immediate commands follow */
case PKT_TYPE_FLUSH_FIFO:
flush_fifo();
break;
case PKT_TYPE_SHUTDOWN:
gigargoyle_shutdown(); /* FIXME only from QM, not from IS */
break;
default:
return 0; /* drop unsupported packages */
}
return 0;
}
void set_pixel_xy_rgb8(
uint32_t hdr,
uint8_t r,
uint8_t g,
uint8_t b,
uint16_t x,
uint16_t y
){
static uint64_t last_timestamp[4] =
{0, 0, 0, 0}; /* FIXME flexible row/bus mapping */
uint64_t timestamp;
shadow_screen[y][x][0] = r;
shadow_screen[y][x][1] = g;
shadow_screen[y][x][2] = b;
/* Mapping hack */
if(y % 2 == 1)
x = ACAB_X - 1 - x;
uint8_t bus_buf[9];
bus_buf[0] = 0x5c;
bus_buf[1] = 0x30;
bus_buf[2] = x + 0x10;
bus_buf[3] = 'C';
bus_buf[4] = r;
bus_buf[5] = g;
bus_buf[6] = b;
bus_buf[7] = 0x5c;
bus_buf[8] = 0x31;
int ret;
if (hdr & PKT_MASK_DBL_BUF)
bus_buf[3] = 'P';
timestamp = gettimeofday64();
if (last_timestamp[y] + MIN_GAP_BUS_TRANSFERS > timestamp)
{
usleep(last_timestamp[y] + MIN_GAP_BUS_TRANSFERS - timestamp);
timestamp = gettimeofday64();
}
ret = write(ggg->uart[y], bus_buf, 9);
if (ret != 9)
LOG("PKTS: WARNING: write(bus %d) = %d != 9\n", y, ret);
last_timestamp[y] = timestamp;
}
void set_pixel_xy_rgb16(
uint32_t hdr,
uint16_t r,
uint16_t g,
uint16_t b,
uint16_t x,
uint16_t y
){
/* FIXME */
set_pixel_xy_rgb8(hdr, r, g, b, x, y);
}
void set_screen_blk(void)
{
int ix, iy;
for (iy=0; iy < ACAB_Y; iy++)
{
for (ix=0; ix < ACAB_X; ix++)
{
set_pixel_xy_rgb8(0, 0, 0, 0, ix, iy); /* FIXME 0 */
}
}
}
void set_screen_rgb8(uint32_t hdr, uint8_t s[ACAB_Y][ACAB_X][3])
{
int ix, iy;
for (iy=0; iy < ACAB_Y; iy++)
{
for (ix=0; ix < ACAB_X; ix++)
{
/* LOG("%x %x %x, ", s[ix][iy][0], s[ix][iy][1], s[ix][iy][2]); */
set_pixel_xy_rgb8(
hdr,
s[iy][ix][0],
s[iy][ix][1],
s[iy][ix][2],
ix, iy
);
}
}
if (hdr & PKT_MASK_REQ_ACK)
if (ggg->source == SOURCE_QM)
if (ggg->qm->state == NET_CONNECTED)
{
//LOG("PKTS: ACK\n");
write(ggg->qm->sock, ACK_AB_KLINGON, strlen(ACK_AB_KLINGON));
}
}
void set_screen_rgb16(uint32_t hdr, uint16_t s[ACAB_Y][ACAB_X][3])
{
int ix, iy;
for (iy=0; iy < ACAB_Y; iy++)
{
for (ix=0; ix < ACAB_X; ix++)
{
set_pixel_xy_rgb16(
hdr,
s[iy][ix][0],
s[iy][ix][1],
s[iy][ix][2],
ix, iy
);
}
}
if (hdr & PKT_MASK_REQ_ACK)
if (ggg->source == SOURCE_QM)
if (ggg->qm->state == NET_CONNECTED)
write(ggg->qm->sock, ACK_AB_KLINGON, strlen(ACK_AB_KLINGON));
}
void set_screen_rnd_bw(void)
{
int ix, iy;
for (iy=0; iy < ACAB_Y; iy++)
{
for (ix=0; ix < ACAB_X; ix++)
{
if (random()&1)
{
tmp_screen8[iy][ix][0] = 0xff;
tmp_screen8[iy][ix][1] = 0xff;
tmp_screen8[iy][ix][2] = 0xff;
}else{
tmp_screen8[iy][ix][0] = 0x00;
tmp_screen8[iy][ix][1] = 0x00;
tmp_screen8[iy][ix][2] = 0x00;
}
}
}
set_screen_rgb8(0, tmp_screen8); /* FIXME 0 */
}
void set_screen_rnd_col(void)
{
int ix, iy;
for (iy=0; iy < ACAB_Y; iy++)
{
for (ix=0; ix < ACAB_X; ix++)
{
tmp_screen8[iy][ix][0] = random();
tmp_screen8[iy][ix][1] = random();
tmp_screen8[iy][ix][2] = random();
}
}
set_screen_rgb8(0, tmp_screen8); /* FIXME 0 */
}
void flip_double_buffer_on_bus(int b)
{
return;
uint8_t bus_buf[9]; //FIXME
bus_buf[0] = 0x5c;
bus_buf[1] = 0x30;
bus_buf[2] = 0x00;
bus_buf[3] = 'U';
bus_buf[7] = 0x5c;
bus_buf[8] = 0x31;
int ret;
ret = write(ggg->uart[b], bus_buf, 9);
if (ret != 9)
LOG("PKTS: WARNING: flip_double_buffer_on_bus() write(bus %d) = %d != 9\n", b, ret);
}
void flip_double_buffer(void)
{
int i;
for (i=0; i<4; i++)
flip_double_buffer_on_bus(i);
}
void next_frame(void)
{
pkt_t * p;
/*
* yeah, we use goto here! deal with it!
* problem: we don't want to wait after processing a control packet,
* like setting framerate or duration
*/
again:
p = rd_fifo();
if (p == NULL)
{
LOG("PKTS: next_frame() ran into an empty fifo: network timeout: %d/%d\n", MISSING_PKTS_TO_LOCAL - ggg->fifo->running_empty_on_network, MISSING_PKTS_TO_LOCAL);
return;
}
switch(p->hdr & PKT_MASK_TYPE)
{
case PKT_TYPE_SET_FADE_RATE:
case PKT_TYPE_SET_PIXEL:
case PKT_TYPE_FLIP_DBL_BUF:
case PKT_TYPE_TEXT:
case PKT_TYPE_SET_FONT:
case PKT_TYPE_SET_SCREEN_BLK:
set_screen_blk();
break;
case PKT_TYPE_SET_SCREEN_WHT:
case PKT_TYPE_SET_SCREEN_RND_BW:
set_screen_rnd_bw();
break;
case PKT_TYPE_SET_SCREEN_RND_COL:
set_screen_rnd_col();
break;
case PKT_TYPE_SET_SCREEN:
if (p->hdr & PKT_MASK_RGB16)
{
if (p->pkt_len != 8 + 2 * 3 * ACAB_X * ACAB_Y)
{
LOG("PKTS: WARNING: dropping SET_SCREEN RGB16 pkt, invalid length");
LOG(" %d != %d\n", p->pkt_len, 8 + 2 * 3 * ACAB_X * ACAB_Y);
return;
}
set_screen_rgb16(p->hdr, (uint16_t (*)[ACAB_X][3])p->data);
}
if (p->hdr & PKT_MASK_RGB8)
{
if (p->pkt_len != 8 + 3 * ACAB_X * ACAB_Y)
{
LOG("PKTS: WARNING: dropping SET_SCREEN RGB8 pkt, invalid length");
LOG(" %d != %d\n", p->pkt_len, 8 + 3 * ACAB_X * ACAB_Y);
return;
}
set_screen_rgb8(p->hdr, (uint8_t (*)[ACAB_X][3])p->data);
}
break;
case PKT_TYPE_SET_FRAME_RATE:
if (p->pkt_len != 12)
{
LOG("PKTS: WARNING: dropping short SET_FRAME_RATE pkt\n");
return;
}
frame_duration = 1000000 / (*((uint32_t *)p->data));
goto again;
case PKT_TYPE_SET_DURATION:
if (p->pkt_len != 12)
{
LOG("PKTS: WARNING: dropping short SET_DURATION pkt\n");
return;
}
frame_duration = ntohl(*((uint32_t *)p->data));
goto again;
/* out-of-band immediate commands follow */
/* but were handled already async when entering gigargoyle, see in_packet() */
case PKT_TYPE_FLUSH_FIFO:
case PKT_TYPE_SHUTDOWN:
default:
return; /* drop unsupported packages */
}
if (p->hdr & PKT_MASK_DBL_BUF)
flip_double_buffer();
serve_web_clients();
}
void serve_web_clients(void)
{
int ret;
int i;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
fd_set rfd;
fd_set wfd;
fd_set efd;
for (i=0; i<MAX_WEB_CLIENTS; i++)
{
if (ggg->web->sock[i] != -1)
{
FD_ZERO(&rfd);
FD_ZERO(&wfd);
FD_ZERO(&efd);
FD_SET(ggg->web->sock[i], &wfd);
FD_SET(ggg->web->sock[i], &efd);
select(ggg->web->sock[i]+1, &rfd, &wfd, &efd, &tv);
if (FD_ISSET(ggg->web->sock[i], &efd))
{
close(ggg->web->sock[i]);
ggg->web->sock[i] = -1;
LOG("WEB: wizard%d disagrees no more\n", i);
}
if (FD_ISSET(ggg->web->sock[i], &wfd))
{
ret = write(ggg->web->sock[i], shadow_screen, ACAB_X*ACAB_Y*3);
if (ret != ACAB_X*ACAB_Y*3)
{
LOG("PKTS: gigargoyle told wizard%d to move aside. she refused and is now dead.\n", i);
close(ggg->web->sock[i]);
ggg->web->sock[i] = -1;
}
}else{
close(ggg->web->sock[i]);
ggg->web->sock[i] = -1;
LOG("WEB: wizard%d got some special treatment\n", i);
}
}
}
}