-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathartnet.c
More file actions
1650 lines (1355 loc) · 43.4 KB
/
artnet.c
File metadata and controls
1650 lines (1355 loc) · 43.4 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
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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
*
* artnet.c
* Implementes the external functions for libartnet
* Copyright (C) 2004-2007 Simon Newton
*/
#include "private.h"
// various constants used everywhere
int ARTNET_ADDRESS_NO_CHANGE = 0x7f;
int ARTNET_PORT = 6454;
int ARTNET_STRING_SIZE = 8;
char ARTNET_STRING[] = "Art-Net";
uint8_t ARTNET_VERSION = 14;
uint8_t OEM_HI = 0x04;
uint8_t OEM_LO = 0x30;
char ESTA_HI = 'z';
char ESTA_LO = 'p';
uint8_t TTM_BEHAVIOUR_MASK = 0x02;
uint8_t TTM_REPLY_MASK = 0x01;
uint8_t PROGRAM_NO_CHANGE = 0x7f;
uint8_t PROGRAM_DEFAULTS = 0x00;
uint8_t PROGRAM_CHANGE_MASK = 0x80;
uint8_t HIGH_NIBBLE = 0xF0;
uint8_t LOW_NIBBLE = 0x0F;
uint8_t STATUS_PROG_AUTH_MASK = 0x30;
uint8_t PORT_STATUS_LPT_MODE = 0x02;
uint8_t PORT_STATUS_SHORT = 0x04;
uint8_t PORT_STATUS_ERROR = 0x04;
uint8_t PORT_STATUS_DISABLED_MASK = 0x08;
uint8_t PORT_STATUS_MERGE = 0x08;
uint8_t PORT_STATUS_DMX_TEXT = 0x10;
uint8_t PORT_STATUS_DMX_SIP = 0x20;
uint8_t PORT_STATUS_DMX_TEST = 0x40;
uint8_t PORT_STATUS_ACT_MASK = 0x80;
uint8_t PORT_DISABLE_MASK = 0x01;
uint8_t TOD_RESPONSE_FULL = 0x00;
uint8_t TOD_RESPONSE_NAK = 0x00;
uint8_t MIN_PACKET_SIZE = 10;
uint8_t MERGE_TIMEOUT_SECONDS = 10;
uint8_t FIRMWARE_TIMEOUT_SECONDS = 20;
uint8_t RECV_NO_DATA = 1;
uint8_t MAX_NODE_BCAST_LIMIT = 30; // always bcast after this point
#ifndef TRUE
int TRUE = 1;
int FALSE = 0;
#endif
uint16_t LOW_BYTE = 0x00FF;
uint16_t HIGH_BYTE = 0xFF00;
void copy_apr_to_node_entry(artnet_node_entry e, artnet_reply_t *reply);
int find_nodes_from_uni(node_list_t *nl, uint16_t uni, SI *ips, int size);
/*
* Creates a new ArtNet node.
* Takes a string containing the ip address to bind to, if the string is NULL
* it uses the first non loopback address
*
* @param ip the IP address to bind to
* @param debug level of logging provided 0: none
* @return an artnet_node, or NULL on failure
*/
artnet_node artnet_new(const char *ip, int verbose) {
node n;
int i;
n = malloc(sizeof(artnet_node_t));
if (!n) {
artnet_error("malloc failure");
return NULL;
}
memset(n, 0x0, sizeof(artnet_node_t));
// init node listing
n->node_list.first = NULL;
n->node_list.current = NULL;
n->node_list.last = NULL;
n->node_list.length = 0;
n->state.verbose = verbose;
n->state.oem_hi = OEM_HI;
n->state.oem_lo = OEM_LO;
n->state.esta_hi = ESTA_HI;
n->state.esta_lo = ESTA_LO;
n->state.bcast_limit = 0;
n->peering.peer = NULL;
n->peering.master = TRUE;
n->sd = INVALID_SOCKET;
if (artnet_net_init(n, ip)) {
free(n);
return NULL;
}
// now setup the default parameters
n->state.send_apr_on_change = FALSE;
n->state.ar_count = 0;
n->state.report_code = ARTNET_RCPOWEROK;
n->state.reply_addr.s_addr = 0;
n->state.mode = ARTNET_STANDBY;
// set all ports to MERGE HTP mode and disable
for (i=0; i < ARTNET_MAX_PORTS; i++) {
n->ports.out[i].merge_mode = ARTNET_MERGE_HTP;
n->ports.out[i].port_enabled = FALSE;
n->ports.in[i].port_enabled = FALSE;
// reset tods
reset_tod(&n->ports.in[i].port_tod);
reset_tod(&n->ports.out[i].port_tod);
}
return n;
}
/*
* Starts the ArtNet node.
* Binds the network socket and sends an ArtPoll
* @param vn the artnet_node
* @return 0 on success, non 0 on failure
*
*/
int artnet_start(artnet_node vn) {
node n = (node) vn;
int ret;
check_nullnode(vn);
if (n->state.mode != ARTNET_STANDBY)
return ARTNET_ESTATE;
if ((ret = artnet_net_start(n)))
return ret;
n->state.mode = ARTNET_ON;
if (n->state.reply_addr.s_addr == 0) {
n->state.reply_addr = n->state.bcast_addr;
}
// build the initial reply
if ((ret = artnet_tx_build_art_poll_reply(n)))
return ret;
if (n->state.node_type == ARTNET_SRV) {
// poll the network
if ((ret = artnet_tx_poll(n,NULL, ARTNET_TTM_AUTO)))
return ret;
if ((ret = artnet_tx_tod_request(n)))
return ret;
} else {
// send a reply on startup
if ((ret = artnet_tx_poll_reply(n, FALSE)))
return ret;
}
return ret;
}
/*
* Stops the ArtNet node. This closes the network sockets held by the node
* @param vn the artnet_node
* @return 0 on success, non-0 on failure
*/
int artnet_stop(artnet_node vn) {
node n = (node) vn;
check_nullnode(vn);
if (n->state.mode != ARTNET_ON)
return ARTNET_EACTION;
artnet_net_close(n->sd);
n->state.mode = ARTNET_STANDBY;
return ARTNET_EOK;
}
/*
* Free the memory associated with this node
*/
int artnet_destroy(artnet_node vn) {
node n = (node) vn;
node_entry_private_t *ent, *tmp;
int i;
check_nullnode(vn);
// free any memory associated with firmware transfers
for (ent = n->node_list.first; ent != NULL; ent = tmp) {
if (ent->firmware.data != NULL)
free(ent->firmware.data);
tmp = ent->next;
free(ent);
}
for (i =0; i < ARTNET_MAX_PORTS; i++) {
flush_tod(&n->ports.in[i].port_tod);
flush_tod(&n->ports.out[i].port_tod);
}
free(vn);
return ARTNET_EOK;
}
/*
* Set the OEM code
* This can only be done in the standby state
*/
int artnet_setoem(artnet_node vn, uint8_t hi, uint8_t lo) {
node n = (node) vn;
check_nullnode(vn);
if (n->state.mode != ARTNET_STANDBY)
return ARTNET_ESTATE;
n->state.oem_hi = hi;
n->state.oem_lo = lo;
return ARTNET_EOK;
}
/*
* Set the ESTA code
* This can only be done in the standby state
*/
int artnet_setesta(artnet_node vn, char hi, char lo) {
node n = (node) vn;
check_nullnode(vn);
if (n->state.mode != ARTNET_STANDBY)
return ARTNET_ESTATE;
n->state.esta_hi = hi;
n->state.esta_lo = lo;
return ARTNET_EOK;
}
/*
* Set the number of nodes above which we start to bcast data
* @param vn the artnet_node
* @param limit 0 to always broadcast
*/
int artnet_set_bcast_limit(artnet_node vn, int limit) {
node n = (node) vn;
check_nullnode(vn);
if (limit > MAX_NODE_BCAST_LIMIT) {
artnet_error("attempt to set bcast limit > %d", MAX_NODE_BCAST_LIMIT);
return ARTNET_EARG;
}
n->state.bcast_limit = limit;
return ARTNET_EOK;
}
/*
* Handle any received packets.
* This function is the workhorse of libartnet. You have a couple of options:
* - use artnet_get_sd() to retrieve the socket descriptors and select to
* detect network activity. Then call artnet_read(node,0)
* when activity is detected.
* - call artnet_read repeatedly from within a loop with an appropriate
* timeout
*
* @param vn the artnet_node
* @param timeout the number of seconds to block for if nothing is pending
* @return 0 on success, -1 on failure
*/
int artnet_read(artnet_node vn, int timeout) {
node n = (node) vn;
node tmp;
artnet_packet_t p;
int ret;
check_nullnode(vn);
if (n->state.mode != ARTNET_ON)
return ARTNET_EACTION;
while (1) {
memset(&p.data, 0x0, sizeof(p.data));
// check timeouts now, else this packet may update the timestamps
check_timeouts(n);
if ((ret = artnet_net_recv(n, &p, timeout)) < 0)
return ret;
// nothing to read
if (ret == RECV_NO_DATA)
break;
// skip this packet (filtered)
if (p.length == 0)
continue;
for (tmp = n->peering.peer; tmp != NULL && tmp != n; tmp = tmp->peering.peer)
check_timeouts(tmp);
if (p.length > MIN_PACKET_SIZE && get_type(&p)) {
handle(n, &p);
for (tmp = n->peering.peer; tmp != NULL && tmp != n; tmp = tmp->peering.peer) {
handle(tmp, &p);
}
}
}
return ARTNET_EOK;
}
/*
* To get around the 4 universes per node limitation , we can start more than
* one node on different ip addresses - you'll need to add aliases to your
* network interface something like:
*
* $ ifconfig eth0:1 10.0.0.10 netmask 255.255.255.0
*
* Then the nodes must be joined so that they can share the socket
* bound to the broadcast address.
* TODO: use IP_PKTINFO so that packets are sent from the correct source ip
*
* @param vn1 The artnet node
* @param vn2 The second artnet node
*
* @return 0 on sucess, non 0 on failure
*/
int artnet_join(artnet_node vn1, artnet_node vn2) {
check_nullnode(vn1);
check_nullnode(vn2);
node n1 = (node) vn1;
node n2 = (node) vn2;
node tmp, n;
if (n1->state.mode == ARTNET_ON || n2->state.mode == ARTNET_ON) {
artnet_error("%s called after artnet_start", __FUNCTION__);
return ARTNET_EACTION;
}
tmp = n1->peering.peer == NULL ? n1 : n1->peering.peer;
n1->peering.peer = n2;
for (n = n2; n->peering.peer != NULL && n->peering.peer != n2; n = n->peering.peer) ;
n->peering.peer = tmp;
// make sure there is only 1 master
for (n = n1->peering.peer; n != n1; n = n->peering.peer)
n->peering.master = FALSE;
n1->peering.master = TRUE;
return ARTNET_ESTATE;
}
/*
* This is used to set handlers for sent/received artnet packets.
* If you're using a stock standard node you more than likely don't want
* to set these. See the artnet_set_dmx_callback and artnet_set_firmware_callback.
* If you want to get down and dirty with artnet packets, you can set this
* read / manipulate packets as they arrive (or get sent)
*
* @param vn The artnet_node
* @param handler The handler to set
* @param fh A pointer to a function, set to NULL to turn off
* The function should return 0,
* @param data Data to be passed to the handler when its called
* @return 0 on sucess, non 0 on failure
*/
int artnet_set_handler(artnet_node vn,
artnet_handler_name_t handler,
int (*fh)(artnet_node n, void *pp, void * d),
void *data) {
node n = (node) vn;
callback_t *callback;
check_nullnode(vn);
switch(handler) {
case ARTNET_RECV_HANDLER:
callback = &n->callbacks.recv;
break;
case ARTNET_SEND_HANDLER:
callback = &n->callbacks.send;
break;
case ARTNET_POLL_HANDLER:
callback = &n->callbacks.poll;
break;
case ARTNET_REPLY_HANDLER:
callback = &n->callbacks.reply;
break;
case ARTNET_ADDRESS_HANDLER:
callback = &n->callbacks.address;
break;
case ARTNET_INPUT_HANDLER:
callback = &n->callbacks.input;
break;
case ARTNET_DMX_HANDLER:
callback = &n->callbacks.dmx;
break;
case ARTNET_TOD_REQUEST_HANDLER:
callback = &n->callbacks.todrequest;
break;
case ARTNET_TOD_DATA_HANDLER:
callback = &n->callbacks.toddata;
break;
case ARTNET_TOD_CONTROL_HANDLER:
callback = &n->callbacks.todcontrol;
break;
case ARTNET_RDM_HANDLER:
callback = &n->callbacks.rdm;
break;
default:
artnet_error("%s : Invalid handler defined", __FUNCTION__);
return ARTNET_EARG;
}
callback->fh = fh;
callback->data = data;
return ARTNET_EOK;
}
/*
* This is a special callback which is invoked when dmx data is received.
*
* @param vn The artnet_node
* @param fh The callback to invoke (parameters passwd are the artnet_node, the port_id
* that received the dmx, and some user data
* @param data Data to be passed to the handler when its called
*/
int artnet_set_dmx_handler(artnet_node vn,
int (*fh)(artnet_node n, int port, void *d),
void *data) {
node n = (node) vn;
check_nullnode(vn);
n->callbacks.dmx_c.fh = fh;
n->callbacks.dmx_c.data = data;
return ARTNET_EOK;
}
/*
* This is a special callback which is invoked when a firmware upload is received.
*
* @param vn The artnet_node
* @param fh The callback to invoke (parameters passwd are the artnet_node, a value which
* is true if this was a ubea upload, and some user data
* @param data Data to be passed to the handler when its called
*/
int artnet_set_firmware_handler(
artnet_node vn,
int (*fh)(artnet_node n, int ubea, uint16_t *data, int length, void *d),
void *data) {
node n = (node) vn;
check_nullnode(vn);
n->callbacks.firmware_c.fh = fh;
n->callbacks.firmware_c.data = data;
return ARTNET_EOK;
}
/*
* @param vn The artnet_node
* @param fh The callback to invoke (parameters passwd are the artnet_node, a value which
* is true if this was a ubea upload, and some user data
* @param data Data to be passed to the handler when its called
*/
int artnet_set_program_handler(artnet_node vn,
int (*fh)(artnet_node n, void *d),
void *data) {
node n = (node) vn;
check_nullnode(vn);
n->callbacks.program_c.fh = fh;
n->callbacks.program_c.data = data;
return ARTNET_EOK;
}
/*
*
* @param vn The artnet_node
* @param fh The callback to invoke (parameters passed are the artnet_node, pointer to the
* rdm data, the length of the data and the user data
* @param data Data to be passed to the handler when its called
*
*/
int artnet_set_rdm_handler(
artnet_node vn,
int (*fh)(artnet_node n, int address, uint8_t *rdm, int length, void *d),
void *data) {
node n = (node) vn;
check_nullnode(vn);
n->callbacks.rdm_c.fh = fh;
n->callbacks.rdm_c.data = data;
return ARTNET_EOK;
}
int artnet_set_rdm_initiate_handler(
artnet_node vn,
int (*fh)(artnet_node n, int port, void *d),
void *data) {
node n = (node) vn;
check_nullnode(vn);
n->callbacks.rdm_init_c.fh = fh;
n->callbacks.rdm_init_c.data = data;
return ARTNET_EOK;
}
int artnet_set_rdm_tod_handler(
artnet_node vn,
int (*fh)(artnet_node n, int port, void *d),
void *data) {
node n = (node) vn;
check_nullnode(vn);
n->callbacks.rdm_tod_c.fh = fh;
n->callbacks.rdm_tod_c.data = data;
return ARTNET_EOK;
}
// sends a poll to the specified ip, or if null, will broadcast
// talk_to_me - modify remote nodes behaviour, see spec
// TODO - this should clear the node list - but this will cause issues if the caller holds references
// to certain nodes
/**
*
* @param vn the artnet_node
* @param ip the ip address to send to, NULL will broadcast the ArtPoll
* @param talk_to_me the value for the talk to me
*/
int artnet_send_poll(artnet_node vn,
const char *ip,
artnet_ttm_value_t talk_to_me) {
node n = (node) vn;
check_nullnode(vn);
if (n->state.mode != ARTNET_ON)
return ARTNET_EACTION;
if (n->state.node_type == ARTNET_SRV || n->state.node_type == ARTNET_RAW) {
return artnet_tx_poll(n, ip, talk_to_me);
}
artnet_error("%s : Not sending poll, not a server or raw device", __FUNCTION__);
return ARTNET_ESTATE;
}
/*
* Sends an artpoll reply
*
* @param vn the artnet_node
*/
int artnet_send_poll_reply(artnet_node vn) {
node n = (node) vn;
check_nullnode(vn);
if (n->state.mode != ARTNET_ON)
return ARTNET_EACTION;
return artnet_tx_poll_reply(n, FALSE);
}
/*
* Sends some dmx data
*
* @param vn the artnet_node
*/
int artnet_send_dmx(artnet_node vn,
int port_id,
int16_t length,
const uint8_t *data) {
node n = (node) vn;
artnet_packet_t p;
int ret;
input_port_t *port;
check_nullnode(vn);
if (n->state.mode != ARTNET_ON)
return ARTNET_EACTION;
if (port_id < 0 || port_id >= ARTNET_MAX_PORTS) {
artnet_error("%s : port index out of bounds (%i < 0 || %i > ARTNET_MAX_PORTS)", __FUNCTION__, port_id);
return ARTNET_EARG;
}
port = &n->ports.in[port_id];
if (length < 1 || length > ARTNET_DMX_LENGTH) {
artnet_error("%s : Length of dmx data out of bounds (%i < 1 || %i > ARTNET_MAX_DMX)", __FUNCTION__, length);
return ARTNET_EARG;
}
if (port->port_status & PORT_STATUS_DISABLED_MASK) {
artnet_error("%s : attempt to send on a disabled port (id:%i)", __FUNCTION__, port_id);
return ARTNET_EARG;
}
// ok we're going to send now, make sure we turn the activity bit on
port->port_status = port->port_status | PORT_STATUS_ACT_MASK;
p.length = sizeof(artnet_dmx_t) - (ARTNET_DMX_LENGTH - length);
// now build packet
memcpy(&p.data.admx.id, ARTNET_STRING, ARTNET_STRING_SIZE);
p.data.admx.opCode = htols(ARTNET_DMX);
p.data.admx.verH = 0;
p.data.admx.ver = ARTNET_VERSION;
p.data.admx.sequence = port->seq;
p.data.admx.physical = port_id;
p.data.admx.universe = htols(port->port_addr);
// set length
p.data.admx.lengthHi = short_get_high_byte(length);
p.data.admx.length = short_get_low_byte(length);
memcpy(&p.data.admx.data, data, length);
// default to bcast
p.to.s_addr = n->state.bcast_addr.s_addr;
if (n->state.bcast_limit == 0) {
if ((ret = artnet_net_send(n, &p)))
return ret;
} else {
int nodes;
// find the number of ports for this uni
SI *ips = malloc(sizeof(SI) * n->state.bcast_limit);
if (!ips) {
// Fallback to broadcast mode
if ((ret = artnet_net_send(n, &p)))
return ret;
}
nodes = find_nodes_from_uni(&n->node_list,
port->port_addr,
ips,
n->state.bcast_limit);
if (nodes > n->state.bcast_limit) {
// fall back to broadcast
free(ips);
if ((ret = artnet_net_send(n, &p))) {
return ret;
}
} else {
// unicast to the specified nodes
int i;
for (i =0; i < nodes; i++) {
p.to = ips[i];
artnet_net_send(n, &p);
}
free(ips);
}
}
port->seq++;
return ARTNET_EOK;
}
/*
* Use for performance testing.
* This allows data to be sent on any universe, not just the ones that have
* ports configured.
*/
int artnet_raw_send_dmx(artnet_node vn,
uint8_t uni,
int16_t length,
const uint8_t *data) {
node n = (node) vn;
artnet_packet_t p;
check_nullnode(vn);
if (n->state.mode != ARTNET_ON)
return ARTNET_EACTION;
if (n->state.node_type != ARTNET_RAW)
return ARTNET_ESTATE;
if ( length < 1 || length > ARTNET_DMX_LENGTH) {
artnet_error("%s : Length of dmx data out of bounds (%i < 1 || %i > ARTNET_MAX_DMX)", __FUNCTION__, length);
return ARTNET_EARG;
}
// set dst addr and length
p.to.s_addr = n->state.bcast_addr.s_addr;
p.length = sizeof(artnet_dmx_t) - (ARTNET_DMX_LENGTH - length);
// now build packet
memcpy( &p.data.admx.id, ARTNET_STRING, ARTNET_STRING_SIZE);
p.data.admx.opCode = htols(ARTNET_DMX);
p.data.admx.verH = 0;
p.data.admx.ver = ARTNET_VERSION;
p.data.admx.sequence = 0;
p.data.admx.physical = 0;
p.data.admx.universe = uni;
// set length
p.data.admx.lengthHi = short_get_high_byte(length);
p.data.admx.length = short_get_low_byte(length);
memcpy(&p.data.admx.data, data, length);
return artnet_net_send(n, &p);
}
int artnet_send_address(artnet_node vn,
artnet_node_entry e,
const char *shortName,
const char *longName,
uint8_t inAddr[ARTNET_MAX_PORTS],
uint8_t outAddr[ARTNET_MAX_PORTS],
uint8_t net, uint8_t subAddr, artnet_port_command_t cmd) {
node n = (node) vn;
artnet_packet_t p;
node_entry_private_t *ent = find_private_entry(n,e);
check_nullnode(vn);
if (e == NULL || ent == NULL)
return ARTNET_EARG;
if (n->state.mode != ARTNET_ON)
return ARTNET_EACTION;
if (n->state.node_type == ARTNET_SRV || n->state.node_type == ARTNET_RAW) {
p.to.s_addr = ent->ip.s_addr;
p.length = sizeof(artnet_address_t);
p.type = ARTNET_ADDRESS;
// now build packet, copy the number of ports from the reply recieved from this node
memcpy( &p.data.addr.id, ARTNET_STRING, ARTNET_STRING_SIZE);
p.data.addr.opCode = htols(ARTNET_ADDRESS);
p.data.addr.verH = 0;
p.data.addr.ver = ARTNET_VERSION;
p.data.addr.filler2 = 0;
strncpy((char*) &p.data.addr.shortname, shortName, ARTNET_SHORT_NAME_LENGTH);
strncpy((char*) &p.data.addr.longname, longName, ARTNET_LONG_NAME_LENGTH);
memcpy(&p.data.addr.swin, inAddr, ARTNET_MAX_PORTS);
memcpy(&p.data.addr.swout, outAddr, ARTNET_MAX_PORTS);
p.data.addr.net = net;
p.data.addr.subnet = subAddr;
p.data.addr.swvideo = 0x00;
p.data.addr.command = cmd;
return artnet_net_send(n, &p);
}
return ARTNET_ESTATE;
}
/*
* Sends an ArtInput packet to the specified node, this packet is used to
* enable/disable the input ports on the remote node.
*
* 0x01 disable port
* 0x00 enable port
*
* NOTE: should have enums here instead of uint8_t for settings
*
*/
int artnet_send_input(artnet_node vn,
artnet_node_entry e,
uint8_t settings[ARTNET_MAX_PORTS]) {
node n = (node) vn;
artnet_packet_t p;
node_entry_private_t *ent = find_private_entry(n,e);
check_nullnode(vn);
if (e == NULL)
return ARTNET_EARG;
if (n->state.mode != ARTNET_ON)
return ARTNET_EACTION;
if (n->state.node_type == ARTNET_SRV || n->state.node_type == ARTNET_RAW) {
// set dst, type and length
p.to.s_addr = ent->ip.s_addr;
p.length = sizeof(artnet_input_t);
p.type = ARTNET_INPUT;
// now build packet, copy the number of ports from the reply recieved from this node
memcpy( &p.data.ainput.id, ARTNET_STRING, ARTNET_STRING_SIZE);
p.data.ainput.opCode = htols(ARTNET_INPUT);
p.data.ainput.verH = 0;
p.data.ainput.ver = ARTNET_VERSION;
p.data.ainput.filler1 = 0;
p.data.ainput.filler2 = 0;
p.data.ainput.numbportsH = short_get_high_byte(e->numbports);
p.data.ainput.numbports = short_get_low_byte(e->numbports);
memcpy(&p.data.ainput.input, &settings, ARTNET_MAX_PORTS);
return artnet_net_send(n, &p);
}
return ARTNET_ESTATE;
}
/*
*
* Sends a series of ArtFirmwareMaster packets to the specified node, these are used to
* upload firmware to the remote node.
*
* We send the first packet now, and another one every time we get a ArtFirmwareReply from
* the node
*
* @param vn the artnet_node
* @param e the node entry to send firmware to
* @param ubea set to a true value if this is a ubea upload
* @param data pointer to the firmware
* @param length the number of 16bit words of the firmware (length * 2 = size in bytes)
* @param fh the callback that is invoked when the transfer is complete
* @param user_data data to be passed to the callback
*/
int artnet_send_firmware(
artnet_node vn,
artnet_node_entry e,
int ubea,
uint16_t *data,
int length,
int (*fh)(artnet_node n, artnet_firmware_status_code code, void *d),
void *user_data) {
node n = (node) vn;
node_entry_private_t *ent = find_private_entry(n,e);
int blen;
check_nullnode(vn);
if (e == NULL || ent == NULL)
return ARTNET_EARG;
if (n->state.mode != ARTNET_ON)
return ARTNET_EACTION;
if (n->state.node_type == ARTNET_SRV || n->state.node_type == ARTNET_RAW) {
// length in bytes
blen = length * sizeof(uint16_t);
// store the parameters for this transfer
ent->firmware.data = malloc(blen);
if ( ent->firmware.data == NULL) {
artnet_error_malloc();
return ARTNET_EMEM;
}
ent->firmware.bytes_current = 0;
ent->firmware.bytes_total = blen;
ent->firmware.peer = ent->ip;
ent->firmware.ubea = ubea;
// entry->firmware.last_time set upon sending a packet
// id of the current block
ent->firmware.expected_block = 0;
ent->firmware.callback = fh;
ent->firmware.user_data = user_data;
memcpy(ent->firmware.data, data, blen);
return artnet_tx_firmware_packet(n, &ent->firmware);
}
return ARTNET_ESTATE;
}
/**
*
* Sends an ArtFirmwareReply packet to the specified node,
* this packet is used to acknowledge firmware master packets.
*
* Note, you should never call this function directly, it is provided for
* completness and will only work if the node type is ARTNET_RAW
*
* @param vn the artnet_node
* @param e the node entry to send firmware to
* @param code the status code to send
*/
int artnet_send_firmware_reply(artnet_node vn,
artnet_node_entry e,
artnet_firmware_status_code code) {
node n = (node) vn;
node_entry_private_t *ent = find_private_entry(n,e);
check_nullnode(vn);
if (e == NULL || ent == NULL)
return ARTNET_EARG;
if (n->state.mode != ARTNET_ON)
return ARTNET_EACTION;
return artnet_tx_firmware_reply(n, ent->ip.s_addr, code);
}
/*
* Sends a tod request
*
* @param vn the artnet node
*
*/
int artnet_send_tod_request(artnet_node vn) {
node n = (node) vn;
check_nullnode(vn);
return artnet_tx_tod_request(n);
}
/*
* Sends a tod control datagram
*
* @param vn the artnet node
* @param address the universe address to control
* @param action the action to take
*
*/
int artnet_send_tod_control(artnet_node vn,
uint8_t address,
artnet_tod_command_code action) {
node n = (node) vn;
check_nullnode(vn);
return artnet_tx_tod_control(n, address, action);
}
/*
* Send a tod data datagram
* Note you should not use this, a TodData message should only be sent in response
* to a request, or when a RDM device is added. Use artnet_add_rdm_device() or artnet_add_rdm_devices() instead
*
* @param
*/
int artnet_send_tod_data(artnet_node vn, int port) {
node n = (node) vn;
check_nullnode(vn);
// should update the check for enabled port here
if (port < 0 || port >= ARTNET_MAX_PORTS) {
artnet_error("%s : port index out of bounds (%i < 0 || %i > ARTNET_MAX_PORTS)", __FUNCTION__, port);
return ARTNET_EARG;
}
return artnet_tx_tod_data(n, port);
}
/*
* Send a rdm datagram
* @param address the universe address to send to
* @param data the rdm data to send
* @param length the length of the rdm data
*/
int artnet_send_rdm(artnet_node vn,
uint8_t address,
uint8_t *data,
int length) {
node n = (node) vn;
check_nullnode(vn);
//we check that we are using this address
return artnet_tx_rdm(n, address, data, length);
}
/*