Debian Codegraph @

interfaces/artnet/receive.c

raw symbols SCIP
1 /*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 * receive.c
17 * Handles the receiving of datagrams
18 * Copyright (C) 2004-2007 Simon Newton
19 */
20
21 #include "private.h"
22
23 uint8_t _make_addr(uint8_t subnet, uint8_t addr);
24 void check_merge_timeouts(node n, int port);
25 void merge(node n, int port, int length, uint8_t *latest);
26
27 /*
28 * Checks if the callback is defined, if so call it passing the packet and
29 * the user supplied data.
30 * If the callbacks return a non-zero result, further processing is canceled.
31 */
32 int check_callback(node n, artnet_packet p, callback_t callback) {
33 if (callback.fh != NULL)
34 return callback.fh(n, p, callback.data);
35
36 return 0;
37 }
38
39
40 /*
41 * Handle an artpoll packet
42 */
43 int handle_poll(node n, artnet_packet p) {
44 // run callback if defined
45 if (check_callback(n, p, n->callbacks.poll))
46 return ARTNET_EOK;
47
48 if (n->state.node_type != ARTNET_RAW) {
49 //if we're told to unicast further replies
50 if (p->data.ap.ttm & TTM_REPLY_MASK) {
51 n->state.reply_addr = p->from;
52 } else {
53 n->state.reply_addr.s_addr = n->state.bcast_addr.s_addr;
54 }
55
56 // if we are told to send updates when node conditions change
57 if (p->data.ap.ttm & TTM_BEHAVIOUR_MASK) {
58 n->state.send_apr_on_change = TRUE;
59 } else {
60 n->state.send_apr_on_change = FALSE;
61 }
62
63 return artnet_tx_poll_reply(n, TRUE);
64
65 }
66 return ARTNET_EOK;
67 }
68
69 /*
70 * handle an art poll reply
71 */
72 void handle_reply(node n, artnet_packet p) {
73 // update the node list
74 artnet_nl_update(&n->node_list, p);
75
76 // run callback if defined
77 if (check_callback(n, p, n->callbacks.reply))
78 return;
79 }
80
81
82 /*
83 * handle a art dmx packet
84 */
85 void handle_dmx(node n, artnet_packet p) {
86 int i, data_length;
87 output_port_t *port;
88 in_addr_t ipA, ipB;
89
90 // run callback if defined
91 if (check_callback(n, p, n->callbacks.dmx))
92 return;
93
94 data_length = (int) bytes_to_short(p->data.admx.lengthHi,
95 p->data.admx.length);
96 data_length = min(data_length, ARTNET_DMX_LENGTH);
97
98 // find matching output ports
99 for (i = 0; i < ARTNET_MAX_PORTS; i++) {
100 // if the addr matches and this port is enabled
101 if (p->data.admx.universe == n->ports.out[i].port_addr &&
102 n->ports.out[i].port_enabled) {
103
104 port = &n->ports.out[i];
105 ipA = port->ipA.s_addr;
106 ipB = port->ipB.s_addr;
107
108 // ok packet matches this port
109 n->ports.out[i].port_status = n->ports.out[i].port_status | PORT_STATUS_ACT_MASK;
110
111 /**
112 * 9 cases for merging depending on what the stored ips are.
113 * here's the truth table
114 *
115 *
116 * \ ipA # # # #
117 * ------ # empty # # #
118 * ipB \ # ( 0 ) # p.from # ! p.from #
119 * ##################################################
120 * # new node # continued # start #
121 * empty # first # trans- # merge #
122 * (0) # packet # mission # #
123 * ##################################################
124 * #continued # # cont #
125 * p.from # trans- # invalid! # merge #
126 * # mission # # #
127 * ##################################################
128 * # start # cont # #
129 * ! p.from # merge # merge # discard #
130 * # # # #
131 * ##################################################
132 *
133 * The merge exits when:
134 * o ACCancel command is received in an ArtAddress packet
135 * (this is done in handle_address )
136 * o no data is recv'ed from one source in 10 seconds
137 *
138 */
139
140 check_merge_timeouts(n,i);
141
142 if (ipA == 0 && ipB == 0) {
143 // first packet recv on this port
144 port->ipA.s_addr = p->from.s_addr;
145 port->timeA = time(NULL);
146
147 memcpy(&port->dataA, &p->data.admx.data, data_length);
148 port->length = data_length;
149 memcpy(&port->data, &p->data.admx.data, data_length);
150 }
151 else if (ipA == p->from.s_addr && ipB == 0) {
152 //continued transmission from the same ip (source A)
153
154 port->timeA = time(NULL);
155 memcpy(&port->dataA, &p->data.admx.data, data_length);
156 port->length = data_length;
157 memcpy(&port->data, &p->data.admx.data, data_length);
158 }
159 else if (ipA == 0 && ipB == p->from.s_addr) {
160 //continued transmission from the same ip (source B)
161
162 port->timeB = time(NULL);
163 memcpy(&port->dataB, &p->data.admx.data, data_length);
164 port->length = data_length;
165 memcpy(&port->data, &p->data.admx.data, data_length);
166 }
167 else if (ipA != p->from.s_addr && ipB == 0) {
168 // new source, start the merge
169 port->ipB.s_addr = p->from.s_addr;
170 port->timeB = time(NULL);
171 memcpy(&port->dataB, &p->data.admx.data,data_length);
172 port->length = data_length;
173
174 // merge, newest data is port B
175 merge(n,i,data_length, port->dataB);
176
177 // send reply if needed
178
179 }
180 else if (ipA == 0 && ipB == p->from.s_addr) {
181 // new source, start the merge
182 port->ipA.s_addr = p->from.s_addr;
183 port->timeB = time(NULL);
184 memcpy(&port->dataB, &p->data.admx.data,data_length);
185 port->length = data_length;
186
187 // merge, newest data is portA
188 merge(n,i,data_length, port->dataA);
189
190 // send reply if needed
191 }
192 else if (ipA == p->from.s_addr && ipB != p->from.s_addr) {
193 // continue merge
194 port->timeA = time(NULL);
195 memcpy(&port->dataA, &p->data.admx.data,data_length);
196 port->length = data_length;
197
198 // merge, newest data is portA
199 merge(n,i,data_length, port->dataA);
200
201 }
202 else if (ipA != p->from.s_addr && ipB == p->from.s_addr) {
203 // continue merge
204 port->timeB = time(NULL);
205 memcpy(&port->dataB, &p->data.admx.data,data_length);
206 port->length = data_length;
207
208 // merge newest data is portB
209 merge(n,i,data_length, port->dataB);
210
211 }
212 else if (ipA == p->from.s_addr && ipB == p->from.s_addr) {
213 // err_warn("In handle_dmx, source matches both buffers, this shouldn't be happening!\n");
214
215 }
216 else if (ipA != p->from.s_addr && ipB != p->from.s_addr) {
217 // err_warn("In handle_dmx, more than two sources, discarding data\n");
218
219 }
220 else {
221 // err_warn("In handle_dmx, no cases matched, this shouldn't happen!\n");
222
223 }
224
225 // do the dmx callback here
226 if (n->callbacks.dmx_c.fh != NULL)
227 n->callbacks.dmx_c.fh(n,i, n->callbacks.dmx_c.data);
228 }
229 }
230 return;
231 }
232
233
234 /**
235 * handle art address packet.
236 * This can reprogram certain nodes settings such as short/long name, port
237 * addresses, subnet address etc.
238 *
239 */
240 int handle_address(node n, artnet_packet p) {
241 int i, old_subnet;
242 int addr[ARTNET_MAX_PORTS];
243 int ret;
244
245 if (check_callback(n, p, n->callbacks.address))
246 return ARTNET_EOK;
247
248 // servers (and raw nodes) don't respond to address packets
249 if (n->state.node_type == ARTNET_SRV || n->state.node_type == ARTNET_RAW)
250 return ARTNET_EOK;
251
252 // reprogram shortname if required
253 if (p->data.addr.shortname[0] != PROGRAM_DEFAULTS &&
254 p->data.addr.shortname[0] != PROGRAM_NO_CHANGE) {
255 memcpy(&n->state.short_name, &p->data.addr.shortname, ARTNET_SHORT_NAME_LENGTH);
256 n->state.report_code = ARTNET_RCSHNAMEOK;
257 }
258 // reprogram long name if required
259 if (p->data.addr.longname[0] != PROGRAM_DEFAULTS &&
260 p->data.addr.longname[0] != PROGRAM_NO_CHANGE) {
261 memcpy(&n->state.long_name, &p->data.addr.longname, ARTNET_LONG_NAME_LENGTH);
262 n->state.report_code = ARTNET_RCLONAMEOK;
263 }
264
265 // first of all store existing port addresses
266 // then we can work out if they change
267 for (i=0; i< ARTNET_MAX_PORTS; i++) {
268 addr[i] = n->ports.in[i].port_addr;
269 }
270
271 // program subnet
272 old_subnet = p->data.addr.subnet;
273 if (p->data.addr.subnet == PROGRAM_DEFAULTS) {
274 // reset to defaults
275 n->state.subnet = n->state.default_subnet;
276 n->state.subnet_net_ctl = FALSE;
277
278 } else if (p->data.addr.subnet & PROGRAM_CHANGE_MASK) {
279 n->state.subnet = p->data.addr.subnet & ~PROGRAM_CHANGE_MASK;
280 n->state.subnet_net_ctl = TRUE;
281 }
282
283 // check if subnet has actually changed
284 if (old_subnet != n->state.subnet) {
285 // if it does we need to change all port addresses
286 for(i=0; i< ARTNET_MAX_PORTS; i++) {
287 n->ports.in[i].port_addr = _make_addr(n->state.subnet, n->ports.in[i].port_addr);
288 n->ports.out[i].port_addr = _make_addr(n->state.subnet, n->ports.out[i].port_addr);
289 }
290 }
291
292 // program swins
293 for (i =0; i < ARTNET_MAX_PORTS; i++) {
294 if (p->data.addr.swin[i] == PROGRAM_NO_CHANGE) {
295 continue;
296 } else if (p->data.addr.swin[i] == PROGRAM_DEFAULTS) {
297 // reset to defaults
298 n->ports.in[i].port_addr = _make_addr(n->state.subnet, n->ports.in[i].port_default_addr);
299 n->ports.in[i].port_net_ctl = FALSE;
300
301 } else if ( p->data.addr.swin[i] & PROGRAM_CHANGE_MASK) {
302 n->ports.in[i].port_addr = _make_addr(n->state.subnet, p->data.addr.swin[i]);
303 n->ports.in[i].port_net_ctl = TRUE;
304 }
305 }
306
307 // program swouts
308 for (i =0; i < ARTNET_MAX_PORTS; i++) {
309 if (p->data.addr.swout[i] == PROGRAM_NO_CHANGE) {
310 continue;
311 } else if (p->data.addr.swout[i] == PROGRAM_DEFAULTS) {
312 // reset to defaults
313 n->ports.out[i].port_addr = _make_addr(n->state.subnet, n->ports.out[i].port_default_addr);
314 n->ports.out[i].port_net_ctl = FALSE;
315 n->ports.out[i].port_enabled = TRUE;
316 } else if ( p->data.addr.swout[i] & PROGRAM_CHANGE_MASK) {
317 n->ports.out[i].port_addr = _make_addr(n->state.subnet, p->data.addr.swout[i]);
318 n->ports.in[i].port_net_ctl = TRUE;
319 n->ports.out[i].port_enabled = TRUE;
320 }
321 }
322
323 // reset sequence numbers if the addresses change
324 for (i=0; i< ARTNET_MAX_PORTS; i++) {
325 if (addr[i] != n->ports.in[i].port_addr)
326 n->ports.in[i].seq = 0;
327 }
328
329 // check command
330 switch (p->data.addr.command) {
331 case ARTNET_PC_CANCEL:
332 // fix me
333 break;
334 case ARTNET_PC_RESET:
335 n->ports.out[0].port_status = n->ports.out[0].port_status & ~PORT_STATUS_DMX_SIP & ~PORT_STATUS_DMX_TEST & ~PORT_STATUS_DMX_TEXT;
336 // need to force a rerun of short tests here
337 break;
338 case ARTNET_PC_MERGE_LTP_O:
339 n->ports.out[0].merge_mode = ARTNET_MERGE_LTP;
340 n->ports.out[0].port_status = n->ports.out[0].port_status | PORT_STATUS_LPT_MODE;
341 break;
342 case ARTNET_PC_MERGE_LTP_1:
343 n->ports.out[1].merge_mode = ARTNET_MERGE_LTP;
344 n->ports.out[1].port_status = n->ports.out[1].port_status | PORT_STATUS_LPT_MODE;
345 break;
346 case ARTNET_PC_MERGE_LTP_2:
347 n->ports.out[2].merge_mode = ARTNET_MERGE_LTP;
348 n->ports.out[2].port_status = n->ports.out[2].port_status | PORT_STATUS_LPT_MODE;
349 break;
350 case ARTNET_PC_MERGE_LTP_3:
351 n->ports.out[3].merge_mode = ARTNET_MERGE_LTP;
352 n->ports.out[3].port_status = n->ports.out[3].port_status | PORT_STATUS_LPT_MODE;
353 break;
354 case ARTNET_PC_MERGE_HTP_0:
355 n->ports.out[0].merge_mode = ARTNET_MERGE_HTP;
356 n->ports.out[0].port_status = n->ports.out[0].port_status | PORT_STATUS_LPT_MODE;
357 break;
358 case ARTNET_PC_MERGE_HTP_1:
359 n->ports.out[1].merge_mode = ARTNET_MERGE_HTP;
360 n->ports.out[1].port_status = n->ports.out[1].port_status | PORT_STATUS_LPT_MODE;
361 break;
362 case ARTNET_PC_MERGE_HTP_2:
363 n->ports.out[2].merge_mode = ARTNET_MERGE_HTP;
364 n->ports.out[2].port_status = n->ports.out[2].port_status | PORT_STATUS_LPT_MODE;
365 break;
366 case ARTNET_PC_MERGE_HTP_3:
367 n->ports.out[3].merge_mode = ARTNET_MERGE_HTP;
368 n->ports.out[3].port_status = n->ports.out[3].port_status | PORT_STATUS_LPT_MODE;
369 break;
370
371 }
372
373 if (n->callbacks.program_c.fh != NULL)
374 n->callbacks.program_c.fh(n , n->callbacks.program_c.data);
375
376 if ((ret = artnet_tx_build_art_poll_reply(n)))
377 return ret;
378
379 return artnet_tx_poll_reply(n, TRUE);
380 }
381
382
383 /*
384 * handle art input.
385 * ArtInput packets can disable input ports.
386 */
387 int _artnet_handle_input(node n, artnet_packet p) {
388 int i, ports, ret;
389
390 if (check_callback(n, p, n->callbacks.input))
391 return ARTNET_EOK;
392
393 // servers (and raw nodes) don't respond to input packets
394 if (n->state.node_type != ARTNET_NODE && n->state.node_type != ARTNET_MSRV)
395 return ARTNET_EOK;
396
397 ports = min( p->data.ainput.numbports, ARTNET_MAX_PORTS);
398 for (i =0; i < ports; i++) {
399 if (p->data.ainput.input[i] & PORT_DISABLE_MASK) {
400 // disable
401 n->ports.in[i].port_status = n->ports.in[i].port_status | PORT_STATUS_DISABLED_MASK;
402 } else {
403 // enable
404 n->ports.in[i].port_status = n->ports.in[i].port_status & ~PORT_STATUS_DISABLED_MASK;
405 }
406 }
407
408 if ((ret = artnet_tx_build_art_poll_reply(n)))
409 return ret;
410
411 return artnet_tx_poll_reply(n, TRUE);
412 }
413
414
415 /***
416 * handle tod request packet
417 */
418 int handle_tod_request(node n, artnet_packet p) {
419 int i, j, limit;
420 int ret = ARTNET_EOK;
421
422 if (check_callback(n, p, n->callbacks.todrequest))
423 return ARTNET_EOK;
424
425 if (n->state.node_type != ARTNET_NODE)
426 return ARTNET_EOK;
427
428 // limit to 32
429 limit = min(ARTNET_MAX_RDM_ADCOUNT, p->data.todreq.adCount);
430
431 // this should always be true
432 if (p->data.todreq.command == 0x00) {
433 for (i=0; i < limit; i++) {
434 for (j=0; j < ARTNET_MAX_PORTS; j++) {
435 if (n->ports.out[j].port_addr == p->data.todreq.address[i] &&
436 n->ports.out[j].port_enabled) {
437 // reply with tod
438 ret = ret || artnet_tx_tod_data(n, j);
439 }
440 }
441 }
442 }
443
444 // err_warn("tod request received but command is 0x%02hhx rather than 0x00\n", p->data.todreq.command);
445 return ret;
446 }
447
448 /**
449 * handle tod data packet
450 *
451 * we don't maintain a tod of whats out on the network,
452 * the calling app can deal with this.
453 */
454 void handle_tod_data(node n, artnet_packet p) {
455
456 if (check_callback(n, p, n->callbacks.toddata))
457 return;
458
459 // pass data to app
460
461 // if (n->callbacks.rdm_tod_c.fh != NULL)
462 // n->callbacks.rdm_tod_c.fh(n, i, n->callbacks.rdm_tod_c.data);
463
464 return;
465 }
466
467
468
469 int handle_tod_control(node n, artnet_packet p) {
470 int i;
471 int ret = ARTNET_EOK;
472
473 if (check_callback(n, p, n->callbacks.todcontrol))
474 return ARTNET_EOK;
475
476 for (i=0; i < ARTNET_MAX_PORTS; i++) {
477 if (n->ports.out[i].port_addr == p->data.todcontrol.address &&
478 n->ports.out[i].port_enabled) {
479
480 if (p->data.todcontrol.cmd == ARTNET_TOD_FLUSH) {
481 // flush tod for this port
482 flush_tod(&n->ports.out[i].port_tod);
483
484 //initiate full rdm discovery
485 // do callback here
486 if (n->callbacks.rdm_init_c.fh != NULL)
487 n->callbacks.rdm_init_c.fh(n, i, n->callbacks.rdm_init_c.data);
488
489 // not really sure what to do here, the calling app should do a rdm
490 // init and call artnet_add_rdm_devices() which will send a tod data
491 // but do we really trust the caller ?
492 // Instead we'll send an empty tod data and then another one a bit later
493 // when our tod is populated
494 }
495 // reply with tod
496 ret = ret || artnet_tx_tod_data(n, i);
497 }
498 }
499 return ret;
500 }
501
502 /**
503 * handle rdm packet
504 *
505 */
506 void handle_rdm(node n, artnet_packet p) {
507
508
509 if (check_callback(n, p, n->callbacks.rdm))
510 return;
511
512 printf("rdm data\n");
513
514 // hell dodgy
515 if (n->callbacks.rdm_c.fh != NULL)
516 n->callbacks.rdm_c.fh(n, p->data.rdm.address, p->data.rdm.data, ARTNET_MAX_RDM_DATA, n->callbacks.rdm_c.data);
517
518 return;
519 }
520
521 /**
522 * handle a firmware master
523 */
524
525 // THIS NEEDS TO BE CHECKED FOR BUFFER OVERFLOWS
526 // IMPORTANT!!!!
527 int handle_firmware(node n, artnet_packet p) {
528 int length, offset, block_length, total_blocks, block_id;
529 artnet_firmware_status_code response_code = ARTNET_FIRMWARE_FAIL;
530
531 // run callback if defined
532 if (check_callback(n, p, n->callbacks.firmware))
533 return ARTNET_EOK;
534
535 /*
536 * What happens if an upload is less than 512 bytes ?????
537 */
538
539 if ( p->data.firmware.type == ARTNET_FIRMWARE_FIRMFIRST ||
540 p->data.firmware.type == ARTNET_FIRMWARE_UBEAFIRST) {
541 // a new transfer is initiated
542
543 if (n->firmware.peer.s_addr == 0) {
544 //new transfer
545 // these are 2 byte words, so we get a total of 1k of data per packet
546 length = artnet_misc_nbytes_to_32( p->data.firmware.length ) *
547 sizeof(p->data.firmware.data[0]);
548
549 // set parameters
550 n->firmware.peer.s_addr = p->from.s_addr;
551 n->firmware.data = malloc(length);
552
553 if (n->firmware.data == NULL) {
554 artnet_error_malloc();
555 return ARTNET_EMEM;
556 }
557 n->firmware.bytes_total = length;
558 n->firmware.last_time = time(NULL);
559 n->firmware.expected_block = 1;
560
561 // check if this is a ubea upload or not
562 if (p->data.firmware.type == ARTNET_FIRMWARE_FIRMFIRST)
563 n->firmware.ubea = 0;
564 else
565 n->firmware.ubea = 1;
566
567 // take the minimum of the total length and the max packet size
568 block_length = min((unsigned int) length, ARTNET_FIRMWARE_SIZE *
569 sizeof(p->data.firmware.data[0]));
570
571 memcpy(n->firmware.data, p->data.firmware.data, block_length);
572 n->firmware.bytes_current = block_length;
573
574 if (block_length == length) {
575 // this is the first and last packet
576 // upload was less than 1k bytes
577 // this behaviour isn't in the spec, presumably no firmware will be less that 1k
578 response_code = ARTNET_FIRMWARE_ALLGOOD;
579
580 // do the callback here
581 if (n->callbacks.firmware_c.fh != NULL)
582 n->callbacks.firmware_c.fh(n,
583 n->firmware.ubea,
584 n->firmware.data,
585 n->firmware.bytes_total,
586 n->callbacks.firmware_c.data);
587
588 } else {
589 response_code = ARTNET_FIRMWARE_BLOCKGOOD;
590 }
591
592 } else {
593 // already in a transfer
594 printf("First, but already for a packet\n");
595
596 // send a failure
597 response_code = ARTNET_FIRMWARE_FAIL;
598 }
599
600 } else if (p->data.firmware.type == ARTNET_FIRMWARE_FIRMCONT ||
601 p->data.firmware.type == ARTNET_FIRMWARE_UBEACONT) {
602 // continued transfer
603 length = artnet_misc_nbytes_to_32(p->data.firmware.length) *
604 sizeof(p->data.firmware.data[0]);
605 total_blocks = length / ARTNET_FIRMWARE_SIZE / 2 + 1;
606 block_length = ARTNET_FIRMWARE_SIZE * sizeof(uint16_t);
607 block_id = p->data.firmware.blockId;
608
609 // ok the blockid field is only 1 byte, so it wraps back to 0x00 we
610 // need to watch for this
611 if (n->firmware.expected_block > UINT8_MAX &&
612 (n->firmware.expected_block % (UINT8_MAX+1)) == p->data.firmware.blockId) {
613
614 block_id = n->firmware.expected_block;
615 }
616 offset = block_id * ARTNET_FIRMWARE_SIZE;
617
618 if (n->firmware.peer.s_addr == p->from.s_addr &&
619 length == n->firmware.bytes_total &&
620 block_id < total_blocks-1) {
621
622 memcpy(n->firmware.data + offset, p->data.firmware.data, block_length);
623 n->firmware.bytes_current += block_length;
624 n->firmware.expected_block++;
625
626 response_code = ARTNET_FIRMWARE_BLOCKGOOD;
627 } else {
628 printf("cont, ips don't match or length has changed or out of range block num\n" );
629
630 // in a transfer not from this ip
631 response_code = ARTNET_FIRMWARE_FAIL;
632 }
633
634 } else if (p->data.firmware.type == ARTNET_FIRMWARE_FIRMLAST ||
635 p->data.firmware.type == ARTNET_FIRMWARE_UBEALAST) {
636 length = artnet_misc_nbytes_to_32( p->data.firmware.length) *
637 sizeof(p->data.firmware.data[0]);
638 total_blocks = length / ARTNET_FIRMWARE_SIZE / 2 + 1;
639
640 // length should be the remaining data
641 block_length = n->firmware.bytes_total % (ARTNET_FIRMWARE_SIZE * sizeof(uint16_t));
642 block_id = p->data.firmware.blockId;
643
644 // ok the blockid field is only 1 byte, so it wraps back to 0x00 we
645 // need to watch for this
646 if (n->firmware.expected_block > UINT8_MAX &&
647 (n->firmware.expected_block % (UINT8_MAX+1)) == p->data.firmware.blockId) {
648
649 block_id = n->firmware.expected_block;
650 }
651 offset = block_id * ARTNET_FIRMWARE_SIZE;
652
653 if (n->firmware.peer.s_addr == p->from.s_addr &&
654 length == n->firmware.bytes_total &&
655 block_id == total_blocks-1) {
656
657 // all the checks work out
658 memcpy(n->firmware.data + offset, p->data.firmware.data, block_length);
659 n->firmware.bytes_current += block_length;
660
661 // do the callback here
662 if (n->callbacks.firmware_c.fh != NULL)
663 n->callbacks.firmware_c.fh(n, n->firmware.ubea,
664 n->firmware.data,
665 n->firmware.bytes_total / sizeof(p->data.firmware.data[0]),
666 n->callbacks.firmware_c.data);
667
668 // reset values and free
669 reset_firmware_upload(n);
670
671 response_code = ARTNET_FIRMWARE_ALLGOOD;
672 printf("Firmware upload complete\n");
673
674 } else if (n->firmware.peer.s_addr != p->from.s_addr) {
675 // in a transfer not from this ip
676 printf("last, ips don't match\n" );
677 response_code = ARTNET_FIRMWARE_FAIL;
678 } else if (length != n->firmware.bytes_total) {
679 // they changed the length mid way thru a transfer
680 printf("last, lengths have changed %d %d\n", length, n->firmware.bytes_total);
681 response_code = ARTNET_FIRMWARE_FAIL;
682 } else if (block_id != total_blocks -1) {
683 // the blocks don't match up
684 printf("This is the last block, but not according to the lengths %d %d\n", block_id, total_blocks -1);
685 response_code = ARTNET_FIRMWARE_FAIL;
686 }
687 }
688
689 return artnet_tx_firmware_reply(n, p->from.s_addr, response_code);
690 }
691
692 /**
693 * handle an firmware reply
694 */
695 int handle_firmware_reply(node n, artnet_packet p) {
696 node_entry_private_t *ent;
697
698 // run callback if defined
699 if (check_callback(n, p, n->callbacks.firmware_reply))
700 return ARTNET_EOK;
701
702 ent = find_entry_from_ip(&n->node_list, p->from);
703
704 // node doesn't exist in our list, or we're not doing a transfer to this node
705 if (ent== NULL || ent->firmware.bytes_total == 0)
706 return ARTNET_EOK;
707
708 // three types of response, ALLGOOD, BLOCKGOOD and FIRMFAIL
709 if (p->data.firmwarer.type == ARTNET_FIRMWARE_ALLGOOD) {
710
711 if (ent->firmware.bytes_total == ent->firmware.bytes_current) {
712 // transfer complete
713
714 // do the callback
715 if (ent->firmware.callback != NULL)
716 ent->firmware.callback(n, ARTNET_FIRMWARE_ALLGOOD, ent->firmware.user_data);
717
718 memset(&ent->firmware, 0x0, sizeof(firmware_transfer_t));
719
720 } else {
721 // random ALLGOOD received, don't let this abort the transfer
722 printf("FIRMWARE_ALLGOOD received before transfer completed\n");
723 }
724
725 } else if (p->data.firmwarer.type == ARTNET_FIRMWARE_FAIL) {
726
727 // do the callback
728 if (ent->firmware.callback != NULL)
729 ent->firmware.callback(n, ARTNET_FIRMWARE_FAIL, ent->firmware.user_data);
730
731 // cancel transfer
732 memset(&ent->firmware, 0x0, sizeof(firmware_transfer_t));
733
734 } else if (p->data.firmwarer.type == ARTNET_FIRMWARE_BLOCKGOOD) {
735 // send the next block (only if we're not done yet)
736 if (ent->firmware.bytes_total != ent->firmware.bytes_current) {
737 return artnet_tx_firmware_packet(n, &ent->firmware);
738 }
739 }
740 return ARTNET_EOK;
741 }
742
743
744 /*
745 * have to sort this one out.
746 */
747 void handle_ipprog(node n, artnet_packet p) {
748
749 if (check_callback(n, p, n->callbacks.ipprog))
750 return;
751
752 printf("in ipprog\n");
753 }
754
755
756 /*
757 * The main handler for an artnet packet. calls
758 * the appropriate handler function
759 */
760 int handle(node n, artnet_packet p) {
761
762 if (check_callback(n, p, n->callbacks.recv))
763 return 0;
764
765 switch (p->type) {
766 case ARTNET_POLL:
767 handle_poll(n, p);
768 break;
769 case ARTNET_REPLY:
770 handle_reply(n,p);
771 break;
772 case ARTNET_DMX:
773 handle_dmx(n, p);
774 break;
775 case ARTNET_ADDRESS:
776 handle_address(n, p);
777 break;
778 case ARTNET_INPUT:
779 _artnet_handle_input(n, p);
780 break;
781 case ARTNET_TODREQUEST:
782 handle_tod_request(n, p);
783 break;
784 case ARTNET_TODDATA:
785 handle_tod_data(n, p);
786 break;
787 case ARTNET_TODCONTROL:
788 handle_tod_control(n, p);
789 break;
790 case ARTNET_RDM:
791 handle_rdm(n, p);
792 break;
793 case ARTNET_VIDEOSTEUP:
794 printf("vid setup\n");
795 break;
796 case ARTNET_VIDEOPALETTE:
797 printf("video palette\n");
798 break;
799 case ARTNET_VIDEODATA:
800 printf("video data\n");
801 break;
802 case ARTNET_MACMASTER:
803 printf("mac master\n");
804 break;
805 case ARTNET_MACSLAVE:
806 printf("mac slave\n");
807 break;
808 case ARTNET_FIRMWAREMASTER:
809 handle_firmware(n, p);
810 break;
811 case ARTNET_FIRMWAREREPLY:
812 handle_firmware_reply(n, p);
813 break;
814 case ARTNET_IPPROG :
815 handle_ipprog(n, p);
816 break;
817 case ARTNET_IPREPLY:
818 printf("ip reply\n");
819 break;
820 case ARTNET_MEDIA:
821 printf("media \n");
822 break;
823 case ARTNET_MEDIAPATCH:
824 printf("media patch\n");
825 break;
826 case ARTNET_MEDIACONTROLREPLY:
827 printf("media control reply\n");
828 break;
829 default:
830 n->state.report_code = ARTNET_RCPARSEFAIL;
831 printf("artnet but not yet implemented!, op was %x\n", (int) p->type);
832 }
833 return 0;
834 }
835
836 /**
837 * this gets the opcode from a packet
838 */
839 int16_t get_type(artnet_packet p) {
840 uint8_t *data;
841
842 if (p->length < 10)
843 return 0;
844 if (!memcmp(&p->data, "Art-Net\0", 8)) {
845 // not the best here, this needs to be tested on different arch
846 data = (uint8_t *) &p->data;
847
848 p->type = (data[9] << 8) + data[8];
849 return p->type;
850 } else {
851 return 0;
852 }
853 }
854
855
856 /*
857 * takes a subnet and an address and creates the universe address
858 */
859 uint8_t _make_addr(uint8_t subnet, uint8_t addr) {
860 return ((subnet & LOW_NIBBLE) << 4) | (addr & LOW_NIBBLE);
861 }
862
863
864 /*
865 *
866 */
867 void check_merge_timeouts(node n, int port_id) {
868 output_port_t *port;
869 time_t now;
870 time_t timeoutA, timeoutB;
871 port = &n->ports.out[port_id];
872 time(&now);
873 timeoutA = now - port->timeA;
874 timeoutB = now - port->timeB;
875
876 if (timeoutA > MERGE_TIMEOUT_SECONDS) {
877 // A is old, stop the merge
878 port->ipA.s_addr = 0;
879 }
880
881 if (timeoutB > MERGE_TIMEOUT_SECONDS) {
882 // B is old, stop the merge
883 port->ipB.s_addr = 0;
884 }
885 }
886
887
888 /*
889 * merge the data from two sources
890 */
891 void merge(node n, int port_id, int length, uint8_t *latest) {
892 int i;
893 output_port_t *port;
894 port = &n->ports.out[port_id];
895
896 if (port->merge_mode == ARTNET_MERGE_HTP) {
897 for (i=0; i< length; i++)
898 port->data[i] = max(port->dataA[i], port->dataB[i]);
899 } else {
900 memcpy(port->data, latest, length);
901 }
902 }
903
904
905 void reset_firmware_upload(node n) {
906 n->firmware.bytes_current = 0;
907 n->firmware.bytes_total = 0;
908 n->firmware.peer.s_addr = 0;
909 n->firmware.ubea = 0;
910 n->firmware.last_time = 0;
911 free(n->firmware.data);
912 }