-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathreceive.c
More file actions
914 lines (756 loc) · 27.3 KB
/
receive.c
File metadata and controls
914 lines (756 loc) · 27.3 KB
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* receive.c
* Handles the receiving of datagrams
* Copyright (C) 2004-2007 Simon Newton
*/
#include "private.h"
uint16_t _make_addr(uint8_t net, uint8_t subnet, uint8_t addr);
void check_merge_timeouts(node n, int port);
void merge(node n, int port, int length, uint8_t *latest);
/*
* Checks if the callback is defined, if so call it passing the packet and
* the user supplied data.
* If the callbacks return a non-zero result, further processing is canceled.
*/
int check_callback(node n, artnet_packet p, callback_t callback) {
if (callback.fh != NULL)
return callback.fh(n, p, callback.data);
return 0;
}
/*
* Handle an artpoll packet
*/
int handle_poll(node n, artnet_packet p) {
// run callback if defined
if (check_callback(n, p, n->callbacks.poll))
return ARTNET_EOK;
if (n->state.node_type != ARTNET_RAW) {
//if we're told to unicast further replies
if (p->data.ap.ttm & TTM_REPLY_MASK) {
n->state.reply_addr = p->from;
} else {
n->state.reply_addr.s_addr = n->state.bcast_addr.s_addr;
}
// if we are told to send updates when node conditions change
if (p->data.ap.ttm & TTM_BEHAVIOUR_MASK) {
n->state.send_apr_on_change = TRUE;
} else {
n->state.send_apr_on_change = FALSE;
}
return artnet_tx_poll_reply(n, TRUE);
}
return ARTNET_EOK;
}
/*
* handle an art poll reply
*/
void handle_reply(node n, artnet_packet p) {
// update the node list
artnet_nl_update(&n->node_list, p);
// run callback if defined
if (check_callback(n, p, n->callbacks.reply))
return;
}
/*
* handle a art dmx packet
*/
void handle_dmx(node n, artnet_packet p) {
int i, data_length;
output_port_t *port;
in_addr_t ipA, ipB;
// run callback if defined
if (check_callback(n, p, n->callbacks.dmx))
return;
data_length = (int) bytes_to_short(p->data.admx.lengthHi,
p->data.admx.length);
data_length = min(data_length, ARTNET_DMX_LENGTH);
// find matching output ports
for (i = 0; i < ARTNET_MAX_PORTS; i++) {
// if the addr matches and this port is enabled
if (p->data.admx.universe == n->ports.out[i].port_addr &&
n->ports.out[i].port_enabled) {
port = &n->ports.out[i];
ipA = port->ipA.s_addr;
ipB = port->ipB.s_addr;
// ok packet matches this port
n->ports.out[i].port_status = n->ports.out[i].port_status | PORT_STATUS_ACT_MASK;
/**
* 9 cases for merging depending on what the stored ips are.
* here's the truth table
*
*
* \ ipA # # # #
* ------ # empty # # #
* ipB \ # ( 0 ) # p.from # ! p.from #
* ##################################################
* # new node # continued # start #
* empty # first # trans- # merge #
* (0) # packet # mission # #
* ##################################################
* #continued # # cont #
* p.from # trans- # invalid! # merge #
* # mission # # #
* ##################################################
* # start # cont # #
* ! p.from # merge # merge # discard #
* # # # #
* ##################################################
*
* The merge exits when:
* o ACCancel command is received in an ArtAddress packet
* (this is done in handle_address )
* o no data is recv'ed from one source in 10 seconds
*
*/
check_merge_timeouts(n,i);
if (ipA == 0 && ipB == 0) {
// first packet recv on this port
port->ipA.s_addr = p->from.s_addr;
port->timeA = time(NULL);
memcpy(&port->dataA, &p->data.admx.data, data_length);
port->length = data_length;
memcpy(&port->data, &p->data.admx.data, data_length);
}
else if (ipA == p->from.s_addr && ipB == 0) {
//continued transmission from the same ip (source A)
port->timeA = time(NULL);
memcpy(&port->dataA, &p->data.admx.data, data_length);
port->length = data_length;
memcpy(&port->data, &p->data.admx.data, data_length);
}
else if (ipA == 0 && ipB == p->from.s_addr) {
//continued transmission from the same ip (source B)
port->timeB = time(NULL);
memcpy(&port->dataB, &p->data.admx.data, data_length);
port->length = data_length;
memcpy(&port->data, &p->data.admx.data, data_length);
}
else if (ipA != p->from.s_addr && ipB == 0) {
// new source, start the merge
port->ipB.s_addr = p->from.s_addr;
port->timeB = time(NULL);
memcpy(&port->dataB, &p->data.admx.data,data_length);
port->length = data_length;
// merge, newest data is port B
merge(n,i,data_length, port->dataB);
// send reply if needed
}
else if (ipA == 0 && ipB == p->from.s_addr) {
// new source, start the merge
port->ipA.s_addr = p->from.s_addr;
port->timeB = time(NULL);
memcpy(&port->dataB, &p->data.admx.data,data_length);
port->length = data_length;
// merge, newest data is portA
merge(n,i,data_length, port->dataA);
// send reply if needed
}
else if (ipA == p->from.s_addr && ipB != p->from.s_addr) {
// continue merge
port->timeA = time(NULL);
memcpy(&port->dataA, &p->data.admx.data,data_length);
port->length = data_length;
// merge, newest data is portA
merge(n,i,data_length, port->dataA);
}
else if (ipA != p->from.s_addr && ipB == p->from.s_addr) {
// continue merge
port->timeB = time(NULL);
memcpy(&port->dataB, &p->data.admx.data,data_length);
port->length = data_length;
// merge newest data is portB
merge(n,i,data_length, port->dataB);
}
else if (ipA == p->from.s_addr && ipB == p->from.s_addr) {
// err_warn("In handle_dmx, source matches both buffers, this shouldn't be happening!\n");
}
else if (ipA != p->from.s_addr && ipB != p->from.s_addr) {
// err_warn("In handle_dmx, more than two sources, discarding data\n");
}
else {
// err_warn("In handle_dmx, no cases matched, this shouldn't happen!\n");
}
// do the dmx callback here
if (n->callbacks.dmx_c.fh != NULL)
n->callbacks.dmx_c.fh(n,i, n->callbacks.dmx_c.data);
}
}
return;
}
/**
* handle art address packet.
* This can reprogram certain nodes settings such as short/long name, port
* addresses, subnet address etc.
*
*/
int handle_address(node n, artnet_packet p) {
int i, old_subnet, old_net;
int addr[ARTNET_MAX_PORTS];
int ret;
if (check_callback(n, p, n->callbacks.address))
return ARTNET_EOK;
// servers (and raw nodes) don't respond to address packets
if (n->state.node_type == ARTNET_SRV || n->state.node_type == ARTNET_RAW)
return ARTNET_EOK;
// reprogram shortname if required
if (p->data.addr.shortname[0] != PROGRAM_DEFAULTS &&
p->data.addr.shortname[0] != PROGRAM_NO_CHANGE) {
memcpy(&n->state.short_name, &p->data.addr.shortname, ARTNET_SHORT_NAME_LENGTH);
n->state.report_code = ARTNET_RCSHNAMEOK;
}
// reprogram long name if required
if (p->data.addr.longname[0] != PROGRAM_DEFAULTS &&
p->data.addr.longname[0] != PROGRAM_NO_CHANGE) {
memcpy(&n->state.long_name, &p->data.addr.longname, ARTNET_LONG_NAME_LENGTH);
n->state.report_code = ARTNET_RCLONAMEOK;
}
// first of all store existing port addresses
// then we can work out if they change
for (i=0; i< ARTNET_MAX_PORTS; i++) {
addr[i] = n->ports.in[i].port_addr;
}
// program subnet
old_net = p->data.addr.net;
old_subnet = p->data.addr.subnet;
if (p->data.addr.subnet == PROGRAM_DEFAULTS) {
// reset to defaults
n->state.net = n->state.default_net;
n->state.subnet = n->state.default_subnet;
n->state.subnet_net_ctl = FALSE;
} else if (p->data.addr.subnet & PROGRAM_CHANGE_MASK) {
n->state.subnet = p->data.addr.subnet & ~PROGRAM_CHANGE_MASK;
n->state.subnet_net_ctl = TRUE;
}
// check if subnet has actually changed
if (old_net != n->state.net || old_subnet != n->state.subnet) {
// if it does we need to change all port addresses
for(i=0; i< ARTNET_MAX_PORTS; i++) {
n->ports.in[i].port_addr = _make_addr(n->state.net, n->state.subnet, (uint8_t)n->ports.in[i].port_addr);
n->ports.out[i].port_addr = _make_addr(n->state.net, n->state.subnet, (uint8_t)n->ports.out[i].port_addr);
}
}
// program swins
for (i =0; i < ARTNET_MAX_PORTS; i++) {
if (p->data.addr.swin[i] == PROGRAM_NO_CHANGE) {
continue;
} else if (p->data.addr.swin[i] == PROGRAM_DEFAULTS) {
// reset to defaults
n->ports.in[i].port_addr = _make_addr(n->state.net, n->state.subnet, n->ports.in[i].port_default_addr);
n->ports.in[i].port_net_ctl = FALSE;
} else if ( p->data.addr.swin[i] & PROGRAM_CHANGE_MASK) {
n->ports.in[i].port_addr = _make_addr(n->state.net, n->state.subnet, p->data.addr.swin[i]);
n->ports.in[i].port_net_ctl = TRUE;
}
}
// program swouts
for (i =0; i < ARTNET_MAX_PORTS; i++) {
if (p->data.addr.swout[i] == PROGRAM_NO_CHANGE) {
continue;
} else if (p->data.addr.swout[i] == PROGRAM_DEFAULTS) {
// reset to defaults
n->ports.out[i].port_addr = _make_addr(n->state.net, n->state.subnet, n->ports.out[i].port_default_addr);
n->ports.out[i].port_net_ctl = FALSE;
n->ports.out[i].port_enabled = TRUE;
} else if ( p->data.addr.swout[i] & PROGRAM_CHANGE_MASK) {
n->ports.out[i].port_addr = _make_addr(n->state.net, n->state.subnet, p->data.addr.swout[i]);
n->ports.in[i].port_net_ctl = TRUE;
n->ports.out[i].port_enabled = TRUE;
}
}
// reset sequence numbers if the addresses change
for (i=0; i< ARTNET_MAX_PORTS; i++) {
if (addr[i] != n->ports.in[i].port_addr)
n->ports.in[i].seq = 0;
}
// check command
switch (p->data.addr.command) {
case ARTNET_PC_CANCEL:
// fix me
break;
case ARTNET_PC_RESET:
n->ports.out[0].port_status = n->ports.out[0].port_status & ~PORT_STATUS_DMX_SIP & ~PORT_STATUS_DMX_TEST & ~PORT_STATUS_DMX_TEXT;
// need to force a rerun of short tests here
break;
case ARTNET_PC_MERGE_LTP_O:
n->ports.out[0].merge_mode = ARTNET_MERGE_LTP;
n->ports.out[0].port_status = n->ports.out[0].port_status | PORT_STATUS_LPT_MODE;
break;
case ARTNET_PC_MERGE_LTP_1:
n->ports.out[1].merge_mode = ARTNET_MERGE_LTP;
n->ports.out[1].port_status = n->ports.out[1].port_status | PORT_STATUS_LPT_MODE;
break;
case ARTNET_PC_MERGE_LTP_2:
n->ports.out[2].merge_mode = ARTNET_MERGE_LTP;
n->ports.out[2].port_status = n->ports.out[2].port_status | PORT_STATUS_LPT_MODE;
break;
case ARTNET_PC_MERGE_LTP_3:
n->ports.out[3].merge_mode = ARTNET_MERGE_LTP;
n->ports.out[3].port_status = n->ports.out[3].port_status | PORT_STATUS_LPT_MODE;
break;
case ARTNET_PC_MERGE_HTP_0:
n->ports.out[0].merge_mode = ARTNET_MERGE_HTP;
n->ports.out[0].port_status = n->ports.out[0].port_status | PORT_STATUS_LPT_MODE;
break;
case ARTNET_PC_MERGE_HTP_1:
n->ports.out[1].merge_mode = ARTNET_MERGE_HTP;
n->ports.out[1].port_status = n->ports.out[1].port_status | PORT_STATUS_LPT_MODE;
break;
case ARTNET_PC_MERGE_HTP_2:
n->ports.out[2].merge_mode = ARTNET_MERGE_HTP;
n->ports.out[2].port_status = n->ports.out[2].port_status | PORT_STATUS_LPT_MODE;
break;
case ARTNET_PC_MERGE_HTP_3:
n->ports.out[3].merge_mode = ARTNET_MERGE_HTP;
n->ports.out[3].port_status = n->ports.out[3].port_status | PORT_STATUS_LPT_MODE;
break;
}
if (n->callbacks.program_c.fh != NULL)
n->callbacks.program_c.fh(n , n->callbacks.program_c.data);
if ((ret = artnet_tx_build_art_poll_reply(n)))
return ret;
return artnet_tx_poll_reply(n, TRUE);
}
/*
* handle art input.
* ArtInput packets can disable input ports.
*/
int _artnet_handle_input(node n, artnet_packet p) {
int i, ports, ret;
if (check_callback(n, p, n->callbacks.input))
return ARTNET_EOK;
// servers (and raw nodes) don't respond to input packets
if (n->state.node_type != ARTNET_NODE && n->state.node_type != ARTNET_MSRV)
return ARTNET_EOK;
ports = min( p->data.ainput.numbports, ARTNET_MAX_PORTS);
for (i =0; i < ports; i++) {
if (p->data.ainput.input[i] & PORT_DISABLE_MASK) {
// disable
n->ports.in[i].port_status = n->ports.in[i].port_status | PORT_STATUS_DISABLED_MASK;
} else {
// enable
n->ports.in[i].port_status = n->ports.in[i].port_status & ~PORT_STATUS_DISABLED_MASK;
}
}
if ((ret = artnet_tx_build_art_poll_reply(n)))
return ret;
return artnet_tx_poll_reply(n, TRUE);
}
/***
* handle tod request packet
*/
int handle_tod_request(node n, artnet_packet p) {
int i, j, limit;
int ret = ARTNET_EOK;
if (check_callback(n, p, n->callbacks.todrequest))
return ARTNET_EOK;
if (n->state.node_type != ARTNET_NODE)
return ARTNET_EOK;
// limit to 32
limit = min(ARTNET_MAX_RDM_ADCOUNT, p->data.todreq.adCount);
// this should always be true
if (p->data.todreq.command == 0x00) {
for (i=0; i < limit; i++) {
for (j=0; j < ARTNET_MAX_PORTS; j++) {
if (n->ports.out[j].port_addr == p->data.todreq.address[i] &&
n->ports.out[j].port_enabled) {
// reply with tod
ret = ret || artnet_tx_tod_data(n, j);
}
}
}
}
// err_warn("tod request received but command is 0x%02hhx rather than 0x00\n", p->data.todreq.command);
return ret;
}
/**
* handle tod data packet
*
* we don't maintain a tod of whats out on the network,
* the calling app can deal with this.
*/
void handle_tod_data(node n, artnet_packet p) {
if (check_callback(n, p, n->callbacks.toddata))
return;
// pass data to app
// if (n->callbacks.rdm_tod_c.fh != NULL)
// n->callbacks.rdm_tod_c.fh(n, i, n->callbacks.rdm_tod_c.data);
return;
}
int handle_tod_control(node n, artnet_packet p) {
int i;
int ret = ARTNET_EOK;
if (check_callback(n, p, n->callbacks.todcontrol))
return ARTNET_EOK;
for (i=0; i < ARTNET_MAX_PORTS; i++) {
if (n->ports.out[i].port_addr == p->data.todcontrol.address &&
n->ports.out[i].port_enabled) {
if (p->data.todcontrol.cmd == ARTNET_TOD_FLUSH) {
// flush tod for this port
flush_tod(&n->ports.out[i].port_tod);
//initiate full rdm discovery
// do callback here
if (n->callbacks.rdm_init_c.fh != NULL)
n->callbacks.rdm_init_c.fh(n, i, n->callbacks.rdm_init_c.data);
// not really sure what to do here, the calling app should do a rdm
// init and call artnet_add_rdm_devices() which will send a tod data
// but do we really trust the caller ?
// Instead we'll send an empty tod data and then another one a bit later
// when our tod is populated
}
// reply with tod
ret = ret || artnet_tx_tod_data(n, i);
}
}
return ret;
}
/**
* handle rdm packet
*
*/
void handle_rdm(node n, artnet_packet p) {
if (check_callback(n, p, n->callbacks.rdm))
return;
printf("rdm data\n");
// hell dodgy
if (n->callbacks.rdm_c.fh != NULL)
n->callbacks.rdm_c.fh(n, p->data.rdm.address, p->data.rdm.data, ARTNET_MAX_RDM_DATA, n->callbacks.rdm_c.data);
return;
}
/**
* handle a firmware master
*/
// THIS NEEDS TO BE CHECKED FOR BUFFER OVERFLOWS
// IMPORTANT!!!!
int handle_firmware(node n, artnet_packet p) {
int length, offset, block_length, total_blocks, block_id;
artnet_firmware_status_code response_code = ARTNET_FIRMWARE_FAIL;
// run callback if defined
if (check_callback(n, p, n->callbacks.firmware))
return ARTNET_EOK;
/*
* What happens if an upload is less than 512 bytes ?????
*/
if ( p->data.firmware.type == ARTNET_FIRMWARE_FIRMFIRST ||
p->data.firmware.type == ARTNET_FIRMWARE_UBEAFIRST) {
// a new transfer is initiated
if (n->firmware.peer.s_addr == 0) {
//new transfer
// these are 2 byte words, so we get a total of 1k of data per packet
length = artnet_misc_nbytes_to_32( p->data.firmware.length ) *
sizeof(p->data.firmware.data[0]);
// set parameters
n->firmware.peer.s_addr = p->from.s_addr;
n->firmware.data = malloc(length);
if (n->firmware.data == NULL) {
artnet_error_malloc();
return ARTNET_EMEM;
}
n->firmware.bytes_total = length;
n->firmware.last_time = time(NULL);
n->firmware.expected_block = 1;
// check if this is a ubea upload or not
if (p->data.firmware.type == ARTNET_FIRMWARE_FIRMFIRST)
n->firmware.ubea = 0;
else
n->firmware.ubea = 1;
// take the minimum of the total length and the max packet size
block_length = min((unsigned int) length, ARTNET_FIRMWARE_SIZE *
sizeof(p->data.firmware.data[0]));
memcpy(n->firmware.data, p->data.firmware.data, block_length);
n->firmware.bytes_current = block_length;
if (block_length == length) {
// this is the first and last packet
// upload was less than 1k bytes
// this behaviour isn't in the spec, presumably no firmware will be less that 1k
response_code = ARTNET_FIRMWARE_ALLGOOD;
// do the callback here
if (n->callbacks.firmware_c.fh != NULL)
n->callbacks.firmware_c.fh(n,
n->firmware.ubea,
n->firmware.data,
n->firmware.bytes_total,
n->callbacks.firmware_c.data);
} else {
response_code = ARTNET_FIRMWARE_BLOCKGOOD;
}
} else {
// already in a transfer
printf("First, but already for a packet\n");
// send a failure
response_code = ARTNET_FIRMWARE_FAIL;
}
} else if (p->data.firmware.type == ARTNET_FIRMWARE_FIRMCONT ||
p->data.firmware.type == ARTNET_FIRMWARE_UBEACONT) {
// continued transfer
length = artnet_misc_nbytes_to_32(p->data.firmware.length) *
sizeof(p->data.firmware.data[0]);
total_blocks = length / ARTNET_FIRMWARE_SIZE / 2 + 1;
block_length = ARTNET_FIRMWARE_SIZE * sizeof(uint16_t);
block_id = p->data.firmware.blockId;
// ok the blockid field is only 1 byte, so it wraps back to 0x00 we
// need to watch for this
if (n->firmware.expected_block > UINT8_MAX &&
(n->firmware.expected_block % (UINT8_MAX+1)) == p->data.firmware.blockId) {
block_id = n->firmware.expected_block;
}
offset = block_id * ARTNET_FIRMWARE_SIZE;
if (n->firmware.peer.s_addr == p->from.s_addr &&
length == n->firmware.bytes_total &&
block_id < total_blocks-1) {
memcpy(n->firmware.data + offset, p->data.firmware.data, block_length);
n->firmware.bytes_current += block_length;
n->firmware.expected_block++;
response_code = ARTNET_FIRMWARE_BLOCKGOOD;
} else {
printf("cont, ips don't match or length has changed or out of range block num\n" );
// in a transfer not from this ip
response_code = ARTNET_FIRMWARE_FAIL;
}
} else if (p->data.firmware.type == ARTNET_FIRMWARE_FIRMLAST ||
p->data.firmware.type == ARTNET_FIRMWARE_UBEALAST) {
length = artnet_misc_nbytes_to_32( p->data.firmware.length) *
sizeof(p->data.firmware.data[0]);
total_blocks = length / ARTNET_FIRMWARE_SIZE / 2 + 1;
// length should be the remaining data
block_length = n->firmware.bytes_total % (ARTNET_FIRMWARE_SIZE * sizeof(uint16_t));
block_id = p->data.firmware.blockId;
// ok the blockid field is only 1 byte, so it wraps back to 0x00 we
// need to watch for this
if (n->firmware.expected_block > UINT8_MAX &&
(n->firmware.expected_block % (UINT8_MAX+1)) == p->data.firmware.blockId) {
block_id = n->firmware.expected_block;
}
offset = block_id * ARTNET_FIRMWARE_SIZE;
if (n->firmware.peer.s_addr == p->from.s_addr &&
length == n->firmware.bytes_total &&
block_id == total_blocks-1) {
// all the checks work out
memcpy(n->firmware.data + offset, p->data.firmware.data, block_length);
n->firmware.bytes_current += block_length;
// do the callback here
if (n->callbacks.firmware_c.fh != NULL)
n->callbacks.firmware_c.fh(n, n->firmware.ubea,
n->firmware.data,
n->firmware.bytes_total / sizeof(p->data.firmware.data[0]),
n->callbacks.firmware_c.data);
// reset values and free
reset_firmware_upload(n);
response_code = ARTNET_FIRMWARE_ALLGOOD;
printf("Firmware upload complete\n");
} else if (n->firmware.peer.s_addr != p->from.s_addr) {
// in a transfer not from this ip
printf("last, ips don't match\n" );
response_code = ARTNET_FIRMWARE_FAIL;
} else if (length != n->firmware.bytes_total) {
// they changed the length mid way thru a transfer
printf("last, lengths have changed %d %d\n", length, n->firmware.bytes_total);
response_code = ARTNET_FIRMWARE_FAIL;
} else if (block_id != total_blocks -1) {
// the blocks don't match up
printf("This is the last block, but not according to the lengths %d %d\n", block_id, total_blocks -1);
response_code = ARTNET_FIRMWARE_FAIL;
}
}
return artnet_tx_firmware_reply(n, p->from.s_addr, response_code);
}
/**
* handle an firmware reply
*/
int handle_firmware_reply(node n, artnet_packet p) {
node_entry_private_t *ent;
// run callback if defined
if (check_callback(n, p, n->callbacks.firmware_reply))
return ARTNET_EOK;
ent = find_entry_from_ip(&n->node_list, p->from);
// node doesn't exist in our list, or we're not doing a transfer to this node
if (ent== NULL || ent->firmware.bytes_total == 0)
return ARTNET_EOK;
// three types of response, ALLGOOD, BLOCKGOOD and FIRMFAIL
if (p->data.firmwarer.type == ARTNET_FIRMWARE_ALLGOOD) {
if (ent->firmware.bytes_total == ent->firmware.bytes_current) {
// transfer complete
// do the callback
if (ent->firmware.callback != NULL)
ent->firmware.callback(n, ARTNET_FIRMWARE_ALLGOOD, ent->firmware.user_data);
memset(&ent->firmware, 0x0, sizeof(firmware_transfer_t));
} else {
// random ALLGOOD received, don't let this abort the transfer
printf("FIRMWARE_ALLGOOD received before transfer completed\n");
}
} else if (p->data.firmwarer.type == ARTNET_FIRMWARE_FAIL) {
// do the callback
if (ent->firmware.callback != NULL)
ent->firmware.callback(n, ARTNET_FIRMWARE_FAIL, ent->firmware.user_data);
// cancel transfer
memset(&ent->firmware, 0x0, sizeof(firmware_transfer_t));
} else if (p->data.firmwarer.type == ARTNET_FIRMWARE_BLOCKGOOD) {
// send the next block (only if we're not done yet)
if (ent->firmware.bytes_total != ent->firmware.bytes_current) {
return artnet_tx_firmware_packet(n, &ent->firmware);
}
}
return ARTNET_EOK;
}
/*
* have to sort this one out.
*/
void handle_ipprog(node n, artnet_packet p) {
if (check_callback(n, p, n->callbacks.ipprog))
return;
printf("in ipprog\n");
}
/*
* The main handler for an artnet packet. calls
* the appropriate handler function
*/
int handle(node n, artnet_packet p) {
if (check_callback(n, p, n->callbacks.recv))
return 0;
switch (p->type) {
case ARTNET_POLL:
handle_poll(n, p);
break;
case ARTNET_REPLY:
handle_reply(n,p);
break;
case ARTNET_DMX:
handle_dmx(n, p);
break;
case ARTNET_ADDRESS:
handle_address(n, p);
break;
case ARTNET_INPUT:
_artnet_handle_input(n, p);
break;
case ARTNET_TODREQUEST:
handle_tod_request(n, p);
break;
case ARTNET_TODDATA:
handle_tod_data(n, p);
break;
case ARTNET_TODCONTROL:
handle_tod_control(n, p);
break;
case ARTNET_RDM:
handle_rdm(n, p);
break;
case ARTNET_VIDEOSTEUP:
printf("vid setup\n");
break;
case ARTNET_VIDEOPALETTE:
printf("video palette\n");
break;
case ARTNET_VIDEODATA:
printf("video data\n");
break;
case ARTNET_MACMASTER:
printf("mac master\n");
break;
case ARTNET_MACSLAVE:
printf("mac slave\n");
break;
case ARTNET_FIRMWAREMASTER:
handle_firmware(n, p);
break;
case ARTNET_FIRMWAREREPLY:
handle_firmware_reply(n, p);
break;
case ARTNET_IPPROG :
handle_ipprog(n, p);
break;
case ARTNET_IPREPLY:
printf("ip reply\n");
break;
case ARTNET_MEDIA:
printf("media \n");
break;
case ARTNET_MEDIAPATCH:
printf("media patch\n");
break;
case ARTNET_MEDIACONTROLREPLY:
printf("media control reply\n");
break;
default:
n->state.report_code = ARTNET_RCPARSEFAIL;
printf("artnet but not yet implemented!, op was %x\n", (int) p->type);
}
return 0;
}
/**
* this gets the opcode from a packet
*/
int16_t get_type(artnet_packet p) {
uint8_t *data;
if (p->length < 10)
return 0;
if (!memcmp(&p->data, "Art-Net\0", 8)) {
// not the best here, this needs to be tested on different arch
data = (uint8_t *) &p->data;
p->type = (data[9] << 8) + data[8];
return p->type;
} else {
return 0;
}
}
/*
* takes a net, subnet and an address and creates the universe address
*/
uint16_t _make_addr(uint8_t net, uint8_t subnet, uint8_t addr) {
return (net << 8 ) | ((subnet & LOW_NIBBLE) << 4) | (addr & LOW_NIBBLE);
}
/*
*
*/
void check_merge_timeouts(node n, int port_id) {
output_port_t *port;
time_t now;
time_t timeoutA, timeoutB;
port = &n->ports.out[port_id];
time(&now);
timeoutA = now - port->timeA;
timeoutB = now - port->timeB;
if (timeoutA > MERGE_TIMEOUT_SECONDS) {
// A is old, stop the merge
port->ipA.s_addr = 0;
}
if (timeoutB > MERGE_TIMEOUT_SECONDS) {
// B is old, stop the merge
port->ipB.s_addr = 0;
}
}
/*
* merge the data from two sources
*/
void merge(node n, int port_id, int length, uint8_t *latest) {
int i;
output_port_t *port;
port = &n->ports.out[port_id];
if (port->merge_mode == ARTNET_MERGE_HTP) {
for (i=0; i< length; i++)
port->data[i] = max(port->dataA[i], port->dataB[i]);
} else {
memcpy(port->data, latest, length);
}
}
void reset_firmware_upload(node n) {
n->firmware.bytes_current = 0;
n->firmware.bytes_total = 0;
n->firmware.peer.s_addr = 0;
n->firmware.ubea = 0;
n->firmware.last_time = 0;
free(n->firmware.data);
}