Debian Codegraph @

interfaces/artnet/artnet.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 * artnet.c
17 * Implementes the external functions for libartnet
18 * Copyright (C) 2004-2007 Simon Newton
19 */
20 #include "private.h"
21
22 // various constants used everywhere
23 int ARTNET_ADDRESS_NO_CHANGE = 0x7f;
24 int ARTNET_PORT = 6454;
25 int ARTNET_STRING_SIZE = 8;
26 char ARTNET_STRING[] = "Art-Net";
27 uint8_t ARTNET_VERSION = 14;
28 uint8_t OEM_HI = 0x04;
29 uint8_t OEM_LO = 0x30;
30 char ESTA_HI = 'z';
31 char ESTA_LO = 'p';
32 uint8_t TTM_BEHAVIOUR_MASK = 0x02;
33 uint8_t TTM_REPLY_MASK = 0x01;
34 uint8_t PROGRAM_NO_CHANGE = 0x7f;
35 uint8_t PROGRAM_DEFAULTS = 0x00;
36 uint8_t PROGRAM_CHANGE_MASK = 0x80;
37 uint8_t HIGH_NIBBLE = 0xF0;
38 uint8_t LOW_NIBBLE = 0x0F;
39 uint8_t STATUS_PROG_AUTH_MASK = 0x30;
40 uint8_t PORT_STATUS_LPT_MODE = 0x02;
41 uint8_t PORT_STATUS_SHORT = 0x04;
42 uint8_t PORT_STATUS_ERROR = 0x04;
43 uint8_t PORT_STATUS_DISABLED_MASK = 0x08;
44 uint8_t PORT_STATUS_MERGE = 0x08;
45 uint8_t PORT_STATUS_DMX_TEXT = 0x10;
46 uint8_t PORT_STATUS_DMX_SIP = 0x20;
47 uint8_t PORT_STATUS_DMX_TEST = 0x40;
48 uint8_t PORT_STATUS_ACT_MASK = 0x80;
49 uint8_t PORT_DISABLE_MASK = 0x01;
50 uint8_t TOD_RESPONSE_FULL = 0x00;
51 uint8_t TOD_RESPONSE_NAK = 0x00;
52 uint8_t MIN_PACKET_SIZE = 10;
53 uint8_t MERGE_TIMEOUT_SECONDS = 10;
54 uint8_t FIRMWARE_TIMEOUT_SECONDS = 20;
55 uint8_t RECV_NO_DATA = 1;
56 uint8_t MAX_NODE_BCAST_LIMIT = 30; // always bcast after this point
57
58 #ifndef TRUE
59 int TRUE = 1;
60 int FALSE = 0;
61 #endif
62
63 uint16_t LOW_BYTE = 0x00FF;
64 uint16_t HIGH_BYTE = 0xFF00;
65
66 void copy_apr_to_node_entry(artnet_node_entry e, artnet_reply_t *reply);
67 int find_nodes_from_uni(node_list_t *nl, uint8_t uni, SI *ips, int size);
68
69 /*
70 * Creates a new ArtNet node.
71 * Takes a string containing the ip address to bind to, if the string is NULL
72 * it uses the first non loopback address
73 *
74 * @param ip the IP address to bind to
75 * @param debug level of logging provided 0: none
76 * @return an artnet_node, or NULL on failure
77 */
78 artnet_node artnet_new(const char *ip, int verbose) {
79 node n;
80 int i;
81
82 n = malloc(sizeof(artnet_node_t));
83
84 if (!n) {
85 artnet_error("malloc failure");
86 return NULL;
87 }
88
89 memset(n, 0x0, sizeof(artnet_node_t));
90
91 // init node listing
92 n->node_list.first = NULL;
93 n->node_list.current = NULL;
94 n->node_list.last = NULL;
95 n->node_list.length = 0;
96 n->state.verbose = verbose;
97 n->state.oem_hi = OEM_HI;
98 n->state.oem_lo = OEM_LO;
99 n->state.esta_hi = ESTA_HI;
100 n->state.esta_lo = ESTA_LO;
101 n->state.bcast_limit = 0;
102
103 n->peering.peer = NULL;
104 n->peering.master = TRUE;
105
106 n->sd = INVALID_SOCKET;
107
108 if (artnet_net_init(n, ip)) {
109 free(n);
110 return NULL;
111 }
112
113 // now setup the default parameters
114 n->state.send_apr_on_change = FALSE;
115 n->state.ar_count = 0;
116 n->state.report_code = ARTNET_RCPOWEROK;
117 n->state.reply_addr.s_addr = 0;
118 n->state.mode = ARTNET_STANDBY;
119
120 // set all ports to MERGE HTP mode and disable
121 for (i=0; i < ARTNET_MAX_PORTS; i++) {
122 n->ports.out[i].merge_mode = ARTNET_MERGE_HTP;
123 n->ports.out[i].port_enabled = FALSE;
124 n->ports.in[i].port_enabled = FALSE;
125
126 // reset tods
127 reset_tod(&n->ports.in[i].port_tod);
128 reset_tod(&n->ports.out[i].port_tod);
129 }
130 return n;
131 }
132
133
134 /*
135 * Starts the ArtNet node.
136 * Binds the network socket and sends an ArtPoll
137 * @param vn the artnet_node
138 * @return 0 on success, non 0 on failure
139 *
140 */
141 int artnet_start(artnet_node vn) {
142 node n = (node) vn;
143 int ret;
144 check_nullnode(vn);
145
146 if (n->state.mode != ARTNET_STANDBY)
147 return ARTNET_ESTATE;
148
149 if ((ret = artnet_net_start(n)))
150 return ret;
151
152 n->state.mode = ARTNET_ON;
153
154 if (n->state.reply_addr.s_addr == 0) {
155 n->state.reply_addr = n->state.bcast_addr;
156 }
157
158 // build the initial reply
159 if ((ret = artnet_tx_build_art_poll_reply(n)))
160 return ret;
161
162 if (n->state.node_type == ARTNET_SRV) {
163 // poll the network
164 if ((ret = artnet_tx_poll(n,NULL, ARTNET_TTM_AUTO)))
165 return ret;
166
167 if ((ret = artnet_tx_tod_request(n)))
168 return ret;
169 } else {
170 // send a reply on startup
171 if ((ret = artnet_tx_poll_reply(n, FALSE)))
172 return ret;
173 }
174 return ret;
175 }
176
177
178 /*
179 * Stops the ArtNet node. This closes the network sockets held by the node
180 * @param vn the artnet_node
181 * @return 0 on success, non-0 on failure
182 */
183 int artnet_stop(artnet_node vn) {
184 node n = (node) vn;
185 check_nullnode(vn);
186
187 if (n->state.mode != ARTNET_ON)
188 return ARTNET_EACTION;
189
190 artnet_net_close(n->sd);
191 n->state.mode = ARTNET_STANDBY;
192 return ARTNET_EOK;
193 }
194
195
196 /*
197 * Free the memory associated with this node
198 */
199 int artnet_destroy(artnet_node vn) {
200 node n = (node) vn;
201 node_entry_private_t *ent, *tmp;
202 int i;
203
204 check_nullnode(vn);
205
206 // free any memory associated with firmware transfers
207 for (ent = n->node_list.first; ent != NULL; ent = tmp) {
208 if (ent->firmware.data != NULL)
209 free(ent->firmware.data);
210 tmp = ent->next;
211 free(ent);
212 }
213
214 for (i =0; i < ARTNET_MAX_PORTS; i++) {
215 flush_tod(&n->ports.in[i].port_tod);
216 flush_tod(&n->ports.out[i].port_tod);
217 }
218
219 free(vn);
220 return ARTNET_EOK;
221 }
222
223
224 /*
225 * Set the OEM code
226 * This can only be done in the standby state
227 */
228 int artnet_setoem(artnet_node vn, uint8_t hi, uint8_t lo) {
229 node n = (node) vn;
230 check_nullnode(vn);
231
232 if (n->state.mode != ARTNET_STANDBY)
233 return ARTNET_ESTATE;
234
235 n->state.oem_hi = hi;
236 n->state.oem_lo = lo;
237 return ARTNET_EOK;
238 }
239
240
241 /*
242 * Set the ESTA code
243 * This can only be done in the standby state
244 */
245 int artnet_setesta(artnet_node vn, char hi, char lo) {
246 node n = (node) vn;
247 check_nullnode(vn);
248
249 if (n->state.mode != ARTNET_STANDBY)
250 return ARTNET_ESTATE;
251
252 n->state.esta_hi = hi;
253 n->state.esta_lo = lo;
254 return ARTNET_EOK;
255 }
256
257
258 /*
259 * Set the number of nodes above which we start to bcast data
260 * @param vn the artnet_node
261 * @param limit 0 to always broadcast
262 */
263 int artnet_set_bcast_limit(artnet_node vn, int limit) {
264 node n = (node) vn;
265 check_nullnode(vn);
266
267 if (limit > MAX_NODE_BCAST_LIMIT) {
268 artnet_error("attempt to set bcast limit > %d", MAX_NODE_BCAST_LIMIT);
269 return ARTNET_EARG;
270 }
271
272 n->state.bcast_limit = limit;
273 return ARTNET_EOK;
274 }
275
276 /*
277 * Handle any received packets.
278 * This function is the workhorse of libartnet. You have a couple of options:
279 * - use artnet_get_sd() to retrieve the socket descriptors and select to
280 * detect network activity. Then call artnet_read(node,0)
281 * when activity is detected.
282 * - call artnet_read repeatedly from within a loop with an appropriate
283 * timeout
284 *
285 * @param vn the artnet_node
286 * @param timeout the number of seconds to block for if nothing is pending
287 * @return 0 on success, -1 on failure
288 */
289 int artnet_read(artnet_node vn, int timeout) {
290 node n = (node) vn;
291 node tmp;
292 artnet_packet_t p;
293 int ret;
294
295 check_nullnode(vn);
296
297 if (n->state.mode != ARTNET_ON)
298 return ARTNET_EACTION;
299
300 while (1) {
301 memset(&p.data, 0x0, sizeof(p.data));
302
303 // check timeouts now, else this packet may update the timestamps
304 check_timeouts(n);
305
306 if ((ret = artnet_net_recv(n, &p, timeout)) < 0)
307 return ret;
308
309 // nothing to read
310 if (ret == RECV_NO_DATA)
311 break;
312
313 // skip this packet (filtered)
314 if (p.length == 0)
315 continue;
316
317 for (tmp = n->peering.peer; tmp != NULL && tmp != n; tmp = tmp->peering.peer)
318 check_timeouts(tmp);
319
320 if (p.length > MIN_PACKET_SIZE && get_type(&p)) {
321 handle(n, &p);
322 for (tmp = n->peering.peer; tmp != NULL && tmp != n; tmp = tmp->peering.peer) {
323 handle(tmp, &p);
324 }
325 }
326 }
327 return ARTNET_EOK;
328 }
329
330
331 /*
332 * To get around the 4 universes per node limitation , we can start more than
333 * one node on different ip addresses - you'll need to add aliases to your
334 * network interface something like:
335 *
336 * $ ifconfig eth0:1 10.0.0.10 netmask 255.255.255.0
337 *
338 * Then the nodes must be joined so that they can share the socket
339 * bound to the broadcast address.
340 * TODO: use IP_PKTINFO so that packets are sent from the correct source ip
341 *
342 * @param vn1 The artnet node
343 * @param vn2 The second artnet node
344 *
345 * @return 0 on sucess, non 0 on failure
346 */
347 int artnet_join(artnet_node vn1, artnet_node vn2) {
348
349 check_nullnode(vn1);
350 check_nullnode(vn2);
351
352 node n1 = (node) vn1;
353 node n2 = (node) vn2;
354 node tmp, n;
355
356 if (n1->state.mode == ARTNET_ON || n2->state.mode == ARTNET_ON) {
357 artnet_error("%s called after artnet_start", __FUNCTION__);
358 return ARTNET_EACTION;
359 }
360
361 tmp = n1->peering.peer == NULL ? n1 : n1->peering.peer;
362 n1->peering.peer = n2;
363 for (n = n2; n->peering.peer != NULL && n->peering.peer != n2; n = n->peering.peer) ;
364 n->peering.peer = tmp;
365
366 // make sure there is only 1 master
367 for (n = n1->peering.peer; n != n1; n = n->peering.peer)
368 n->peering.master = FALSE;
369
370 n1->peering.master = TRUE;
371
372 return ARTNET_ESTATE;
373 }
374
375
376 /*
377 * This is used to set handlers for sent/received artnet packets.
378 * If you're using a stock standard node you more than likely don't want
379 * to set these. See the artnet_set_dmx_callback and artnet_set_firmware_callback.
380 * If you want to get down and dirty with artnet packets, you can set this
381 * read / manipulate packets as they arrive (or get sent)
382 *
383 * @param vn The artnet_node
384 * @param handler The handler to set
385 * @param fh A pointer to a function, set to NULL to turn off
386 * The function should return 0,
387 * @param data Data to be passed to the handler when its called
388 * @return 0 on sucess, non 0 on failure
389 */
390 int artnet_set_handler(artnet_node vn,
391 artnet_handler_name_t handler,
392 int (*fh)(artnet_node n, void *pp, void * d),
393 void *data) {
394 node n = (node) vn;
395 callback_t *callback;
396 check_nullnode(vn);
397
398 switch(handler) {
399 case ARTNET_RECV_HANDLER:
400 callback = &n->callbacks.recv;
401 break;
402 case ARTNET_SEND_HANDLER:
403 callback = &n->callbacks.send;
404 break;
405 case ARTNET_POLL_HANDLER:
406 callback = &n->callbacks.poll;
407 break;
408 case ARTNET_REPLY_HANDLER:
409 callback = &n->callbacks.reply;
410 break;
411 case ARTNET_ADDRESS_HANDLER:
412 callback = &n->callbacks.address;
413 break;
414 case ARTNET_INPUT_HANDLER:
415 callback = &n->callbacks.input;
416 break;
417 case ARTNET_DMX_HANDLER:
418 callback = &n->callbacks.dmx;
419 break;
420 case ARTNET_TOD_REQUEST_HANDLER:
421 callback = &n->callbacks.todrequest;
422 break;
423 case ARTNET_TOD_DATA_HANDLER:
424 callback = &n->callbacks.toddata;
425 break;
426 case ARTNET_TOD_CONTROL_HANDLER:
427 callback = &n->callbacks.todcontrol;
428 break;
429 case ARTNET_RDM_HANDLER:
430 callback = &n->callbacks.rdm;
431 break;
432 default:
433 artnet_error("%s : Invalid handler defined", __FUNCTION__);
434 return ARTNET_EARG;
435 }
436 callback->fh = fh;
437 callback->data = data;
438 return ARTNET_EOK;
439 }
440
441
442 /*
443 * This is a special callback which is invoked when dmx data is received.
444 *
445 * @param vn The artnet_node
446 * @param fh The callback to invoke (parameters passwd are the artnet_node, the port_id
447 * that received the dmx, and some user data
448 * @param data Data to be passed to the handler when its called
449 */
450 int artnet_set_dmx_handler(artnet_node vn,
451 int (*fh)(artnet_node n, int port, void *d),
452 void *data) {
453 node n = (node) vn;
454 check_nullnode(vn);
455
456 n->callbacks.dmx_c.fh = fh;
457 n->callbacks.dmx_c.data = data;
458 return ARTNET_EOK;
459 }
460
461
462 /*
463 * This is a special callback which is invoked when a firmware upload is received.
464 *
465 * @param vn The artnet_node
466 * @param fh The callback to invoke (parameters passwd are the artnet_node, a value which
467 * is true if this was a ubea upload, and some user data
468 * @param data Data to be passed to the handler when its called
469 */
470 int artnet_set_firmware_handler(
471 artnet_node vn,
472 int (*fh)(artnet_node n, int ubea, uint16_t *data, int length, void *d),
473 void *data) {
474 node n = (node) vn;
475 check_nullnode(vn);
476 n->callbacks.firmware_c.fh = fh;
477 n->callbacks.firmware_c.data = data;
478 return ARTNET_EOK;
479 }
480
481
482 /*
483 * @param vn The artnet_node
484 * @param fh The callback to invoke (parameters passwd are the artnet_node, a value which
485 * is true if this was a ubea upload, and some user data
486 * @param data Data to be passed to the handler when its called
487 */
488 int artnet_set_program_handler(artnet_node vn,
489 int (*fh)(artnet_node n, void *d),
490 void *data) {
491 node n = (node) vn;
492 check_nullnode(vn);
493 n->callbacks.program_c.fh = fh;
494 n->callbacks.program_c.data = data;
495 return ARTNET_EOK;
496 }
497
498
499 /*
500 *
501 * @param vn The artnet_node
502 * @param fh The callback to invoke (parameters passed are the artnet_node, pointer to the
503 * rdm data, the length of the data and the user data
504 * @param data Data to be passed to the handler when its called
505 *
506 */
507 int artnet_set_rdm_handler(
508 artnet_node vn,
509 int (*fh)(artnet_node n, int address, uint8_t *rdm, int length, void *d),
510 void *data) {
511 node n = (node) vn;
512 check_nullnode(vn);
513 n->callbacks.rdm_c.fh = fh;
514 n->callbacks.rdm_c.data = data;
515 return ARTNET_EOK;
516 }
517
518
519 int artnet_set_rdm_initiate_handler(
520 artnet_node vn,
521 int (*fh)(artnet_node n, int port, void *d),
522 void *data) {
523 node n = (node) vn;
524 check_nullnode(vn);
525
526 n->callbacks.rdm_init_c.fh = fh;
527 n->callbacks.rdm_init_c.data = data;
528 return ARTNET_EOK;
529 }
530
531
532 int artnet_set_rdm_tod_handler(
533 artnet_node vn,
534 int (*fh)(artnet_node n, int port, void *d),
535 void *data) {
536 node n = (node) vn;
537 check_nullnode(vn);
538
539 n->callbacks.rdm_tod_c.fh = fh;
540 n->callbacks.rdm_tod_c.data = data;
541 return ARTNET_EOK;
542 }
543
544
545 // sends a poll to the specified ip, or if null, will broadcast
546 // talk_to_me - modify remote nodes behaviour, see spec
547 // TODO - this should clear the node list - but this will cause issues if the caller holds references
548 // to certain nodes
549
550 /**
551 *
552 * @param vn the artnet_node
553 * @param ip the ip address to send to, NULL will broadcast the ArtPoll
554 * @param talk_to_me the value for the talk to me
555 */
556 int artnet_send_poll(artnet_node vn,
557 const char *ip,
558 artnet_ttm_value_t talk_to_me) {
559 node n = (node) vn;
560 check_nullnode(vn);
561
562 if (n->state.mode != ARTNET_ON)
563 return ARTNET_EACTION;
564
565 if (n->state.node_type == ARTNET_SRV || n->state.node_type == ARTNET_RAW) {
566 return artnet_tx_poll(n, ip, talk_to_me);
567 }
568
569 artnet_error("%s : Not sending poll, not a server or raw device", __FUNCTION__);
570 return ARTNET_ESTATE;
571 }
572
573
574 /*
575 * Sends an artpoll reply
576 *
577 * @param vn the artnet_node
578 */
579 int artnet_send_poll_reply(artnet_node vn) {
580 node n = (node) vn;
581 check_nullnode(vn);
582
583 if (n->state.mode != ARTNET_ON)
584 return ARTNET_EACTION;
585
586 return artnet_tx_poll_reply(n, FALSE);
587 }
588
589
590 /*
591 * Sends some dmx data
592 *
593 * @param vn the artnet_node
594 */
595 int artnet_send_dmx(artnet_node vn,
596 int port_id,
597 int16_t length,
598 const uint8_t *data) {
599 node n = (node) vn;
600 artnet_packet_t p;
601 int ret;
602 input_port_t *port;
603
604 check_nullnode(vn);
605
606 if (n->state.mode != ARTNET_ON)
607 return ARTNET_EACTION;
608
609 if (port_id < 0 || port_id >= ARTNET_MAX_PORTS) {
610 artnet_error("%s : port index out of bounds (%i < 0 || %i > ARTNET_MAX_PORTS)", __FUNCTION__, port_id);
611 return ARTNET_EARG;
612 }
613 port = &n->ports.in[port_id];
614
615 if (length < 1 || length > ARTNET_DMX_LENGTH) {
616 artnet_error("%s : Length of dmx data out of bounds (%i < 1 || %i > ARTNET_MAX_DMX)", __FUNCTION__, length);
617 return ARTNET_EARG;
618 }
619
620 if (port->port_status & PORT_STATUS_DISABLED_MASK) {
621 artnet_error("%s : attempt to send on a disabled port (id:%i)", __FUNCTION__, port_id);
622 return ARTNET_EARG;
623 }
624
625 // ok we're going to send now, make sure we turn the activity bit on
626 port->port_status = port->port_status | PORT_STATUS_ACT_MASK;
627
628 p.length = sizeof(artnet_dmx_t) - (ARTNET_DMX_LENGTH - length);
629
630 // now build packet
631 memcpy(&p.data.admx.id, ARTNET_STRING, ARTNET_STRING_SIZE);
632 p.data.admx.opCode = htols(ARTNET_DMX);
633 p.data.admx.verH = 0;
634 p.data.admx.ver = ARTNET_VERSION;
635 p.data.admx.sequence = port->seq;
636 p.data.admx.physical = port_id;
637 p.data.admx.universe = htols(port->port_addr);
638
639 // set length
640 p.data.admx.lengthHi = short_get_high_byte(length);
641 p.data.admx.length = short_get_low_byte(length);
642 memcpy(&p.data.admx.data, data, length);
643
644 // default to bcast
645 p.to.s_addr = n->state.bcast_addr.s_addr;
646
647 if (n->state.bcast_limit == 0) {
648 if ((ret = artnet_net_send(n, &p)))
649 return ret;
650 } else {
651 int nodes;
652 // find the number of ports for this uni
653 SI *ips = malloc(sizeof(SI) * n->state.bcast_limit);
654
655 if (!ips) {
656 // Fallback to broadcast mode
657 if ((ret = artnet_net_send(n, &p)))
658 return ret;
659 }
660
661 nodes = find_nodes_from_uni(&n->node_list,
662 port->port_addr,
663 ips,
664 n->state.bcast_limit);
665
666 if (nodes > n->state.bcast_limit) {
667 // fall back to broadcast
668 free(ips);
669 if ((ret = artnet_net_send(n, &p))) {
670 return ret;
671 }
672 } else {
673 // unicast to the specified nodes
674 int i;
675 for (i =0; i < nodes; i++) {
676 p.to = ips[i];
677 artnet_net_send(n, &p);
678 }
679 free(ips);
680 }
681 }
682 port->seq++;
683 return ARTNET_EOK;
684 }
685
686
687 /*
688 * Use for performance testing.
689 * This allows data to be sent on any universe, not just the ones that have
690 * ports configured.
691 */
692 int artnet_raw_send_dmx(artnet_node vn,
693 uint8_t uni,
694 int16_t length,
695 const uint8_t *data) {
696 node n = (node) vn;
697 artnet_packet_t p;
698
699 check_nullnode(vn);
700
701 if (n->state.mode != ARTNET_ON)
702 return ARTNET_EACTION;
703
704 if (n->state.node_type != ARTNET_RAW)
705 return ARTNET_ESTATE;
706
707 if ( length < 1 || length > ARTNET_DMX_LENGTH) {
708 artnet_error("%s : Length of dmx data out of bounds (%i < 1 || %i > ARTNET_MAX_DMX)", __FUNCTION__, length);
709 return ARTNET_EARG;
710 }
711
712 // set dst addr and length
713 p.to.s_addr = n->state.bcast_addr.s_addr;
714
715 p.length = sizeof(artnet_dmx_t) - (ARTNET_DMX_LENGTH - length);
716
717 // now build packet
718 memcpy( &p.data.admx.id, ARTNET_STRING, ARTNET_STRING_SIZE);
719 p.data.admx.opCode = htols(ARTNET_DMX);
720 p.data.admx.verH = 0;
721 p.data.admx.ver = ARTNET_VERSION;
722 p.data.admx.sequence = 0;
723 p.data.admx.physical = 0;
724 p.data.admx.universe = uni;
725
726 // set length
727 p.data.admx.lengthHi = short_get_high_byte(length);
728 p.data.admx.length = short_get_low_byte(length);
729 memcpy(&p.data.admx.data, data, length);
730
731 return artnet_net_send(n, &p);
732 }
733
734
735
736 int artnet_send_address(artnet_node vn,
737 artnet_node_entry e,
738 const char *shortName,
739 const char *longName,
740 uint8_t inAddr[ARTNET_MAX_PORTS],
741 uint8_t outAddr[ARTNET_MAX_PORTS],
742 uint8_t subAddr, artnet_port_command_t cmd) {
743 node n = (node) vn;
744 artnet_packet_t p;
745 node_entry_private_t *ent = find_private_entry(n,e);
746
747 check_nullnode(vn);
748
749 if (e == NULL || ent == NULL)
750 return ARTNET_EARG;
751
752 if (n->state.mode != ARTNET_ON)
753 return ARTNET_EACTION;
754
755 if (n->state.node_type == ARTNET_SRV || n->state.node_type == ARTNET_RAW) {
756 p.to.s_addr = ent->ip.s_addr;
757
758 p.length = sizeof(artnet_address_t);
759 p.type = ARTNET_ADDRESS;
760
761 // now build packet, copy the number of ports from the reply recieved from this node
762 memcpy( &p.data.addr.id, ARTNET_STRING, ARTNET_STRING_SIZE);
763 p.data.addr.opCode = htols(ARTNET_ADDRESS);
764 p.data.addr.verH = 0;
765 p.data.addr.ver = ARTNET_VERSION;
766 p.data.addr.filler1 = 0;
767 p.data.addr.filler2 = 0;
768 strncpy((char*) &p.data.addr.shortname, shortName, ARTNET_SHORT_NAME_LENGTH);
769 strncpy((char*) &p.data.addr.longname, longName, ARTNET_LONG_NAME_LENGTH);
770
771 memcpy(&p.data.addr.swin, inAddr, ARTNET_MAX_PORTS);
772 memcpy(&p.data.addr.swout, outAddr, ARTNET_MAX_PORTS);
773
774 p.data.addr.subnet = subAddr;
775 p.data.addr.swvideo = 0x00;
776 p.data.addr.command = cmd;
777
778 return artnet_net_send(n, &p);
779 }
780 return ARTNET_ESTATE;
781 }
782
783
784 /*
785 * Sends an ArtInput packet to the specified node, this packet is used to
786 * enable/disable the input ports on the remote node.
787 *
788 * 0x01 disable port
789 * 0x00 enable port
790 *
791 * NOTE: should have enums here instead of uint8_t for settings
792 *
793 */
794 int artnet_send_input(artnet_node vn,
795 artnet_node_entry e,
796 uint8_t settings[ARTNET_MAX_PORTS]) {
797 node n = (node) vn;
798 artnet_packet_t p;
799 node_entry_private_t *ent = find_private_entry(n,e);
800
801 check_nullnode(vn);
802
803 if (e == NULL)
804 return ARTNET_EARG;
805
806 if (n->state.mode != ARTNET_ON)
807 return ARTNET_EACTION;
808
809 if (n->state.node_type == ARTNET_SRV || n->state.node_type == ARTNET_RAW) {
810 // set dst, type and length
811 p.to.s_addr = ent->ip.s_addr;
812
813 p.length = sizeof(artnet_input_t);
814 p.type = ARTNET_INPUT;
815
816 // now build packet, copy the number of ports from the reply recieved from this node
817 memcpy( &p.data.ainput.id, ARTNET_STRING, ARTNET_STRING_SIZE);
818 p.data.ainput.opCode = htols(ARTNET_INPUT);
819 p.data.ainput.verH = 0;
820 p.data.ainput.ver = ARTNET_VERSION;
821 p.data.ainput.filler1 = 0;
822 p.data.ainput.filler2 = 0;
823 p.data.ainput.numbportsH = short_get_high_byte(e->numbports);
824 p.data.ainput.numbports = short_get_low_byte(e->numbports);
825 memcpy(&p.data.ainput.input, &settings, ARTNET_MAX_PORTS);
826
827 return artnet_net_send(n, &p);
828 }
829 return ARTNET_ESTATE;
830 }
831
832
833 /*
834 *
835 * Sends a series of ArtFirmwareMaster packets to the specified node, these are used to
836 * upload firmware to the remote node.
837 *
838 * We send the first packet now, and another one every time we get a ArtFirmwareReply from
839 * the node
840 *
841 * @param vn the artnet_node
842 * @param e the node entry to send firmware to
843 * @param ubea set to a true value if this is a ubea upload
844 * @param data pointer to the firmware
845 * @param length the number of 16bit words of the firmware (length * 2 = size in bytes)
846 * @param fh the callback that is invoked when the transfer is complete
847 * @param user_data data to be passed to the callback
848 */
849 int artnet_send_firmware(
850 artnet_node vn,
851 artnet_node_entry e,
852 int ubea,
853 uint16_t *data,
854 int length,
855 int (*fh)(artnet_node n, artnet_firmware_status_code code, void *d),
856 void *user_data) {
857 node n = (node) vn;
858 node_entry_private_t *ent = find_private_entry(n,e);
859 int blen;
860
861 check_nullnode(vn);
862 if (e == NULL || ent == NULL)
863 return ARTNET_EARG;
864
865 if (n->state.mode != ARTNET_ON)
866 return ARTNET_EACTION;
867
868 if (n->state.node_type == ARTNET_SRV || n->state.node_type == ARTNET_RAW) {
869
870 // length in bytes
871 blen = length * sizeof(uint16_t);
872
873 // store the parameters for this transfer
874 ent->firmware.data = malloc(blen);
875
876 if ( ent->firmware.data == NULL) {
877 artnet_error_malloc();
878 return ARTNET_EMEM;
879 }
880
881 ent->firmware.bytes_current = 0;
882 ent->firmware.bytes_total = blen;
883 ent->firmware.peer = ent->ip;
884 ent->firmware.ubea = ubea;
885 // entry->firmware.last_time set upon sending a packet
886 // id of the current block
887 ent->firmware.expected_block = 0;
888 ent->firmware.callback = fh;
889 ent->firmware.user_data = user_data;
890
891 memcpy(ent->firmware.data, data, blen);
892
893 return artnet_tx_firmware_packet(n, &ent->firmware);
894 }
895
896 return ARTNET_ESTATE;
897 }
898
899
900 /**
901 *
902 * Sends an ArtFirmwareReply packet to the specified node,
903 * this packet is used to acknowledge firmware master packets.
904 *
905 * Note, you should never call this function directly, it is provided for
906 * completness and will only work if the node type is ARTNET_RAW
907 *
908 * @param vn the artnet_node
909 * @param e the node entry to send firmware to
910 * @param code the status code to send
911 */
912 int artnet_send_firmware_reply(artnet_node vn,
913 artnet_node_entry e,
914 artnet_firmware_status_code code) {
915 node n = (node) vn;
916 node_entry_private_t *ent = find_private_entry(n,e);
917
918 check_nullnode(vn);
919
920 if (e == NULL || ent == NULL)
921 return ARTNET_EARG;
922
923 if (n->state.mode != ARTNET_ON)
924 return ARTNET_EACTION;
925
926 return artnet_tx_firmware_reply(n, ent->ip.s_addr, code);
927 }
928
929
930 /*
931 * Sends a tod request
932 *
933 * @param vn the artnet node
934 *
935 */
936 int artnet_send_tod_request(artnet_node vn) {
937 node n = (node) vn;
938 check_nullnode(vn);
939
940 return artnet_tx_tod_request(n);
941 }
942
943
944 /*
945 * Sends a tod control datagram
946 *
947 * @param vn the artnet node
948 * @param address the universe address to control
949 * @param action the action to take
950 *
951 */
952 int artnet_send_tod_control(artnet_node vn,
953 uint8_t address,
954 artnet_tod_command_code action) {
955 node n = (node) vn;
956 check_nullnode(vn);
957 return artnet_tx_tod_control(n, address, action);
958 }
959
960
961 /*
962 * Send a tod data datagram
963 * Note you should not use this, a TodData message should only be sent in response
964 * to a request, or when a RDM device is added. Use artnet_add_rdm_device() or artnet_add_rdm_devices() instead
965 *
966 * @param
967 */
968 int artnet_send_tod_data(artnet_node vn, int port) {
969 node n = (node) vn;
970 check_nullnode(vn);
971
972 // should update the check for enabled port here
973 if (port < 0 || port >= ARTNET_MAX_PORTS) {
974 artnet_error("%s : port index out of bounds (%i < 0 || %i > ARTNET_MAX_PORTS)", __FUNCTION__, port);
975 return ARTNET_EARG;
976 }
977
978 return artnet_tx_tod_data(n, port);
979 }
980
981
982 /*
983 * Send a rdm datagram
984 * @param address the universe address to send to
985 * @param data the rdm data to send
986 * @param length the length of the rdm data
987 */
988 int artnet_send_rdm(artnet_node vn,
989 uint8_t address,
990 uint8_t *data,
991 int length) {
992 node n = (node) vn;
993 check_nullnode(vn);
994
995 //we check that we are using this address
996 return artnet_tx_rdm(n, address, data, length);
997 }
998
999
1000 /*
1001 * Add a rdm device to our tod.
1002 * @param port the port the device is connected to
1003 * @param the uid of the device
1004 */
1005 int artnet_add_rdm_device(artnet_node vn,
1006 int port,
1007 uint8_t uid[ARTNET_RDM_UID_WIDTH]) {
1008 node n = (node) vn;
1009 check_nullnode(vn);
1010
1011 if (port < 0 || port >= ARTNET_MAX_PORTS) {
1012 artnet_error("%s : port index out of bounds (%i < 0 || %i > ARTNET_MAX_PORTS)", __FUNCTION__, port);
1013 return ARTNET_EARG;
1014 }
1015
1016 // add uid to tod for this port
1017 add_tod_uid(&n->ports.out[port].port_tod, uid);
1018
1019 // notify everyone our tod changed
1020 return artnet_tx_tod_data(n, port);
1021 }
1022
1023
1024 /*
1025 * add a list of rdm devices to our tod, use this for full discovery
1026 *
1027 * @param port the port the device is connected to
1028 * @param uid pointer to the uids
1029 * @param count number of uids
1030 */
1031 int artnet_add_rdm_devices(artnet_node vn, int port, uint8_t *uid, int count) {
1032 node n = (node) vn;
1033 int i;
1034
1035 check_nullnode(vn);
1036
1037 if (port < 0 || port >= ARTNET_MAX_PORTS) {
1038 artnet_error("%s : port index out of bounds (%i < 0 || %i > ARTNET_MAX_PORTS)", __FUNCTION__, port);
1039 return ARTNET_EARG;
1040 }
1041
1042 if (count < 0)
1043 return ARTNET_EARG;
1044
1045 for (i = 0; i < count; i++) {
1046 // add uid to tod for this port
1047 add_tod_uid(&n->ports.out[port].port_tod, uid);
1048 uid += ARTNET_RDM_UID_WIDTH;
1049 }
1050 // notify everyone our tod changed
1051 return artnet_tx_tod_data(n, port);
1052
1053 }
1054
1055
1056 /*
1057 * remove a rdm device to our tod.
1058 *
1059 * @param port the port the device was connected to
1060 * @param the uid of the device
1061 */
1062 int artnet_remove_rdm_device(artnet_node vn,
1063 int port,
1064 uint8_t uid[ARTNET_RDM_UID_WIDTH]) {
1065 node n = (node) vn;
1066 check_nullnode(vn);
1067
1068 if (port < 0 || port >= ARTNET_MAX_PORTS) {
1069 artnet_error("%s : port index out of bounds (%i < 0 || %i > ARTNET_MAX_PORTS)", __FUNCTION__, port);
1070 return ARTNET_EARG;
1071 }
1072
1073 // remove uid to tod for this port
1074 remove_tod_uid(&n->ports.out[port].port_tod, uid);
1075
1076 // notify everyone our tod changed
1077 return artnet_tx_tod_data(n, port);
1078 }
1079
1080
1081 /*
1082 * Reads the latest dmx data
1083 * @param vn the artnet node
1084 * @param port_id the port to read data from
1085 * @param length
1086 * @return a pointer to the dmx data, NULL on error
1087 */
1088
1089 uint8_t *artnet_read_dmx(artnet_node vn, int port_id, int *length) {
1090 node n = (node) vn;
1091
1092 if (n == NULL)
1093 return NULL;
1094
1095 if (port_id < 0 || port_id >= ARTNET_MAX_PORTS) {
1096 artnet_error("%s : port index out of bounds (%i < 0 || %i > ARTNET_MAX_PORTS)", __FUNCTION__, port_id);
1097 return NULL;
1098 }
1099
1100 *length = n->ports.out[port_id].length;
1101 return &n->ports.out[port_id].data[0];
1102 }
1103
1104
1105 //--------------------------------------
1106 // Functions to change the node state (setters)
1107
1108 // type : server, node, mserver, raw
1109 int artnet_set_node_type(artnet_node vn, artnet_node_type type) {
1110 node n = (node) vn;
1111 check_nullnode(vn);
1112
1113 n->state.node_type = type;
1114 return ARTNET_EOK;
1115 }
1116
1117
1118 /**
1119 * Sets the artnet subnet address for this node.
1120 * The subnet address has nothing to do with IP addresses). An ArtNet subnet is a grouping of 16 DMX universes
1121 * (ie. ports)
1122 *
1123 * The subnet address is between 0 and 15. If the supplied address is larger than 15, the
1124 * lower 4 bits will be used in setting the address.
1125 *
1126 * It will have no effect if the node is under network control.
1127 *
1128 * Note that changing the subnet address will cause the universe addresses of all ports to change.
1129 *
1130 * @param vn the artnet_node
1131 * @param subnet new subnet address
1132 */
1133 int artnet_set_subnet_addr(artnet_node vn, uint8_t subnet) {
1134 node n = (node) vn;
1135 int i, ret;
1136
1137 check_nullnode(vn);
1138
1139 n->state.default_subnet = subnet;
1140
1141 // if not under network control, and the subnet is different from the current one
1142 if (!n->state.subnet_net_ctl && subnet != n->state.subnet) {
1143 n->state.subnet = subnet;
1144
1145 // redo the addresses for each port
1146 for (i =0; i < ARTNET_MAX_PORTS; i++) {
1147 n->ports.in[i].port_addr = ((n->state.subnet & LOW_NIBBLE) << 4) | (n->ports.in[i].port_addr & LOW_NIBBLE);
1148 // reset dmx sequence number
1149 n->ports.in[i].seq = 0;
1150
1151 n->ports.out[i].port_addr = ((n->state.subnet & LOW_NIBBLE) << 4) | (n->ports.out[i].port_addr & LOW_NIBBLE);
1152 }
1153
1154 if (n->state.mode == ARTNET_ON) {
1155
1156 if ((ret = artnet_tx_build_art_poll_reply(n)))
1157 return ret;
1158
1159 return artnet_tx_poll_reply(n,FALSE);
1160 }
1161 } else if (n->state.subnet_net_ctl ) {
1162 // trying to change subnet addr while under network control
1163 n->state.report_code = ARTNET_RCUSERFAIL;
1164 }
1165
1166 return ARTNET_EOK;
1167 }
1168
1169
1170 /**
1171 * Sets the short name of the node.
1172 * The string should be null terminated and a maxmium of 18 Characters will be used
1173 *
1174 * @param vn the artnet_node
1175 * @param name the short name of the node.
1176 */
1177 int artnet_set_short_name(artnet_node vn, const char *name) {
1178 node n = (node) vn;
1179 check_nullnode(vn);
1180
1181 strncpy((char *) &n->state.short_name, name, ARTNET_SHORT_NAME_LENGTH);
1182 n->state.short_name[ARTNET_SHORT_NAME_LENGTH-1] = 0x00;
1183 return artnet_tx_build_art_poll_reply(n);
1184 }
1185
1186
1187 /*
1188 * Sets the long name of the node.
1189 * The string should be null terminated and a maximium of 64 characters will be used
1190 *
1191 * @param vn the artnet_node
1192 * @param name the node's long name
1193 */
1194 int artnet_set_long_name(artnet_node vn, const char *name) {
1195 node n = (node) vn;
1196 check_nullnode(vn);
1197
1198 strncpy((char *) &n->state.long_name, name, ARTNET_LONG_NAME_LENGTH);
1199 n->state.long_name[ARTNET_LONG_NAME_LENGTH-1] = 0x00;
1200 return artnet_tx_build_art_poll_reply(n);
1201 }
1202
1203
1204 /*
1205 * Sets the direction and type of port
1206 * @param vn the artnet_node
1207 * @param id
1208 * @param direction
1209 * @param data
1210 */
1211 int artnet_set_port_type(artnet_node vn,
1212 int port_id,
1213 artnet_port_settings_t settings,
1214 artnet_port_data_code data) {
1215 node n = (node) vn;
1216 check_nullnode(vn);
1217
1218 if (port_id < 0 || port_id >= ARTNET_MAX_PORTS) {
1219 artnet_error("%s : port index out of bounds (%i < 0 || %i > ARTNET_MAX_PORTS)", __FUNCTION__, port_id);
1220 return ARTNET_EARG;
1221 }
1222
1223 n->ports.types[port_id] = settings | data;
1224 return ARTNET_EOK;
1225 }
1226
1227
1228 /*
1229 * Sets the port address of the port.
1230 *
1231 * Just to set some terms straight:
1232 * - subnet address, is 4 bits, set on a per-node basis
1233 * - port address, 4 bits, set on a per-port basis
1234 * - universe address, 8 bits derrived from the subnet and port addresses, specific (but may
1235 * not be unique) to a port.
1236 *
1237 * The upper four bits of the universe address are from the subnet address, while the lower
1238 * four are from the port address.
1239 *
1240 * So for example, if the subnet address of the node is 0x03, and the port address is
1241 * 0x02, the universe address for the port will be 0x32.
1242 *
1243 * As the port address is between 0 and 15, only the lower 4 bits of the addr argument
1244 * will be used.
1245 *
1246 * The operation may have no affect if the port is under network control.
1247 *
1248 * @param vn the artnet_node
1249 * @param id the phyiscal port number (from 0 to ARTNET_MAX_PORTS-1 )
1250 * @param dir either ARTNET_INPUT_PORT or ARTNET_OUTPUT_PORT
1251 * @param addr the new port address
1252 */
1253 int artnet_set_port_addr(artnet_node vn,
1254 int id,
1255 artnet_port_dir_t dir,
1256 uint8_t addr) {
1257 node n = (node) vn;
1258 int ret;
1259 int changed = 0;
1260
1261 g_port_t *port;
1262
1263 check_nullnode(vn);
1264
1265 if (id < 0 || id >= ARTNET_MAX_PORTS) {
1266 artnet_error("%s : port index out of bounds (%i < 0 || %i > ARTNET_MAX_PORTS)", __FUNCTION__, id);
1267 return ARTNET_EARG;
1268 }
1269
1270 if (addr > 16) {
1271 artnet_error("%s : Attempt to set port %i to invalid address %#hhx\n", __FUNCTION__, id, addr);
1272 return ARTNET_EARG;
1273 }
1274
1275 if (dir == ARTNET_INPUT_PORT) {
1276 port = &n->ports.in[id].port;
1277 changed = n->ports.in[id].port_enabled?0:1;
1278 n->ports.in[id].port_enabled = TRUE;
1279 } else if (dir == ARTNET_OUTPUT_PORT) {
1280 port = &n->ports.out[id].port;
1281 changed = n->ports.out[id].port_enabled?0:1;
1282 n->ports.out[id].port_enabled = TRUE;
1283 } else {
1284 artnet_error("%s : Invalid port direction\n", __FUNCTION__);
1285 return ARTNET_EARG;
1286 }
1287
1288 port->default_addr = addr;
1289
1290 // if not under network control and address is changing
1291 if (!port->net_ctl &&
1292 (changed || (addr & LOW_NIBBLE) != (port->addr & LOW_NIBBLE))) {
1293 port->addr = ((n->state.subnet & LOW_NIBBLE) << 4) | (addr & LOW_NIBBLE);
1294
1295 // reset seq if input port
1296 if (dir == ARTNET_INPUT_PORT)
1297 n->ports.in[id].seq = 0;
1298
1299 if (n->state.mode == ARTNET_ON) {
1300 if ((ret = artnet_tx_build_art_poll_reply(n)))
1301 return ret;
1302
1303 return artnet_tx_poll_reply(n,FALSE);
1304 }
1305 } else if (port->net_ctl) {
1306 // trying to change port addr while under network control
1307 n->state.report_code = ARTNET_RCUSERFAIL;
1308 }
1309 return ARTNET_EOK;
1310 }
1311
1312
1313 /*
1314 * Returns the universe address of this port
1315 *
1316 * @param vn the artnet_node
1317 * @param id the phyiscal port number (from 0 to ARTNET_MAX_PORTS-1 )
1318 * @param dir either ARTNET_INPUT_PORT or ARTNET_OUTPUT_PO
1319 *
1320 * @return the universe address, or < 0 on error
1321 */
1322 int artnet_get_universe_addr(artnet_node vn, int id, artnet_port_dir_t dir) {
1323 node n = (node) vn;
1324 check_nullnode(vn);
1325
1326 if (id < 0 || id >= ARTNET_MAX_PORTS) {
1327 artnet_error("%s : port index out of bounds (%i < 0 || %i > ARTNET_MAX_PORTS)", __FUNCTION__, id);
1328 return ARTNET_EARG;
1329 }
1330
1331 if (dir == ARTNET_INPUT_PORT)
1332 return n->ports.in[id].port.addr;
1333 else if (dir == ARTNET_OUTPUT_PORT)
1334 return n->ports.out[id].port.addr;
1335 else {
1336 artnet_error("%s : Invalid port direction\n", __FUNCTION__);
1337 return ARTNET_EARG;
1338 }
1339 }
1340
1341 int artnet_get_config(artnet_node vn, artnet_node_config_t *config) {
1342 int i;
1343 node n = (node) vn;
1344 check_nullnode(vn);
1345
1346 strncpy(config->short_name, n->state.short_name, ARTNET_SHORT_NAME_LENGTH);
1347 strncpy(config->long_name, n->state.long_name, ARTNET_LONG_NAME_LENGTH);
1348 config->subnet = n->state.subnet;
1349
1350 for (i = 0; i < ARTNET_MAX_PORTS; i++) {
1351 config->in_ports[i] = n->ports.in[i].port.addr & LOW_NIBBLE;
1352 config->out_ports[i] = n->ports.out[i].port.addr & LOW_NIBBLE;
1353 }
1354
1355 return ARTNET_EOK;
1356 }
1357
1358
1359 /*
1360 * Dumps the node config to stdout.
1361 *
1362 * @param vn the artnet_node
1363 */
1364 int artnet_dump_config(artnet_node vn) {
1365 node n = (node) vn;
1366 check_nullnode(vn);
1367
1368 printf("#### NODE CONFIG ####\n");
1369 printf("Node Type: %i\n", n->state.node_type);
1370 printf("Short Name: %s\n", n->state.short_name);
1371 printf("Long Name: %s\n", n->state.long_name);
1372 printf("Subnet: %#02x\n", n->state.subnet);
1373 printf("Default Subnet: %#02x\n", n->state.default_subnet);
1374 printf("Net Ctl: %i\n", n->state.subnet_net_ctl);
1375 printf("#####################\n");
1376
1377 return ARTNET_EOK;
1378 }
1379
1380
1381 /*
1382 * Returns the socket descriptor associated with this artnet_node.
1383 * libartnet currently uses two descriptors per node, one bound
1384 * to the network address and one bound to the subnet broadcast address
1385 *
1386 * @param vn the artnet_node
1387 * @param socket the index of the socket descriptor to fetch (0 or 1)
1388 * @return the socket descriptor
1389 */
1390 int artnet_get_sd(artnet_node vn) {
1391 node n = (node) vn;
1392 check_nullnode(vn);
1393
1394 if (n->state.mode != ARTNET_ON)
1395 return ARTNET_EACTION;
1396
1397 return n->sd;
1398 }
1399
1400
1401 /**
1402 * Sets the file descriptors in the fdset that we are interested in.
1403 *
1404 * @param vn the artnet_node
1405 * @param fdset pointer to the fdset to change
1406 * @return the maxfd+1
1407 */
1408 int artnet_set_fdset(artnet_node vn, fd_set *fdset) {
1409 node n = (node) vn;
1410 check_nullnode(vn);
1411
1412 if (!fdset)
1413 return ARTNET_EARG;
1414
1415 if (n->state.mode != ARTNET_ON)
1416 return ARTNET_EACTION;
1417
1418 return artnet_net_set_fdset(n, fdset);
1419 }
1420
1421
1422 /**
1423 * Returns the artnet_node_list.
1424 * The artnet_node_list holds artnet_node_entry(s) that represent the discovered
1425 * remote nodes on the network
1426 * NOTE: this function is not THREAD SAFE
1427 *
1428 * @param vn the artnet_node
1429 * @return the artnet_node_list
1430 */
1431 artnet_node_list artnet_get_nl(artnet_node vn) {
1432 node n = (node) vn;
1433
1434 if (!vn)
1435 return NULL;
1436
1437 return &n->node_list;
1438 }
1439
1440
1441 /**
1442 * Repositions the pointer to the first entry in the artnet_node_list
1443 * NOTE: this function is not THREAD SAFE
1444 *
1445 * @param vnl the artnet_node_list
1446 * @return the first artnet_node_entry in the list, or NULL if the list is empty
1447 */
1448 artnet_node_entry artnet_nl_first(artnet_node_list vnl) {
1449 node_list_t *nl = (node_list_t*) vnl;
1450
1451 if (!nl)
1452 return NULL;
1453
1454 nl->current = nl->first;
1455 return &nl->current->pub;
1456 }
1457
1458
1459 /**
1460 * Moves the pointer to the next element in the artnet_node_list
1461 * NOTE: this function is not THREAD SAFE
1462 *
1463 * @param vnl the artnet_node_list
1464 * @return the next artnet_node_entry, or NULL if the end of the list is reached
1465 */
1466 artnet_node_entry artnet_nl_next(artnet_node_list vnl) {
1467 node_list_t *nl = (node_list_t*) vnl;
1468
1469 if (!nl)
1470 return NULL;
1471
1472 nl->current = nl->current->next;
1473 return &nl->current->pub;
1474 }
1475
1476
1477 /*
1478 * Returns the length of the artnet_node_list
1479 * NOTE: this function is not THREAD SAFE
1480 *
1481 * @param vnl the artnet_node_list
1482 * @return the length of the list
1483 */
1484 int artnet_nl_get_length(artnet_node_list vnl) {
1485 node_list_t *nl = (node_list_t*) vnl;
1486
1487 if (!nl)
1488 return 0;
1489
1490 return nl->length;
1491 }
1492
1493
1494 /*
1495 * Return a pointer to the staticly allocated error string
1496 */
1497 char *artnet_strerror() {
1498 return artnet_errstr;
1499 }
1500
1501
1502 //-----------------------------------------------------------------------------
1503 // Private functions follow
1504 //-----------------------------------------------------------------------------
1505
1506 int artnet_nl_update(node_list_t *nl, artnet_packet reply) {
1507 node_entry_private_t *entry;
1508
1509 entry = find_entry_from_ip(nl, reply->from);
1510
1511 if (!entry) {
1512 // add to list
1513 entry = (node_entry_private_t*) malloc(sizeof(node_entry_private_t));
1514
1515 if (!entry) {
1516 artnet_error_malloc();
1517 return ARTNET_EMEM;
1518 }
1519
1520 memset(entry, 0x00, sizeof(node_entry_private_t));
1521
1522 copy_apr_to_node_entry(&entry->pub, &reply->data.ar);
1523 entry->ip = reply->from;
1524 entry->next = NULL;
1525
1526 if (!nl->first) {
1527 nl->first = entry;
1528 nl->last = entry;
1529 } else {
1530 nl->last->next = entry;
1531 nl->last = entry;
1532 }
1533 nl->length++;
1534 } else {
1535 // update entry
1536 copy_apr_to_node_entry(&entry->pub, &reply->data.ar);
1537 }
1538 return ARTNET_EOK;
1539 }
1540
1541
1542 /*
1543 * check if this packet is in list
1544 */
1545 node_entry_private_t *find_entry_from_ip(node_list_t *nl, SI ip) {
1546 node_entry_private_t *tmp;
1547
1548 for (tmp = nl->first; tmp; tmp = tmp->next) {
1549 if (ip.s_addr == tmp->ip.s_addr)
1550 break;
1551 }
1552 return tmp;
1553 }
1554
1555
1556 /*
1557 * Find all nodes with a port bound to a particular universe
1558 * @param nl the node list
1559 * @param uni the universe to search for
1560 * @param ips store matching node ips here
1561 * @param size size of ips
1562 * @return number of nodes matched
1563 */
1564 int find_nodes_from_uni(node_list_t *nl, uint8_t uni, SI *ips, int size) {
1565 node_entry_private_t *tmp;
1566 int count = 0;
1567 int i,j = 0;
1568
1569 for (tmp = nl->first; tmp; tmp = tmp->next) {
1570 int added = FALSE;
1571 for (i =0; i < tmp->pub.numbports; i++) {
1572 if (tmp->pub.swout[i] == uni && ips) {
1573 if (j < size && !added) {
1574 ips[j++] = tmp->ip;
1575 added = TRUE;
1576 }
1577 count++;
1578 }
1579 }
1580 }
1581 return count;
1582 }
1583
1584
1585 /*
1586 * Add a node to the node list from an ArtPollReply msg
1587 */
1588 void copy_apr_to_node_entry(artnet_node_entry e, artnet_reply_t *reply) {
1589
1590 // the ip is network byte ordered
1591 memcpy(&e->ip, &reply->ip, 4);
1592 e->ver = bytes_to_short(reply->verH, reply->ver);
1593 e->sub = bytes_to_short(reply->subH, reply->sub);
1594 e->oem = bytes_to_short(reply->oemH, reply->oem);
1595 e->ubea = reply->ubea;
1596 memcpy(&e->etsaman, &reply->etsaman, 2);
1597 memcpy(&e->shortname, &reply->shortname, sizeof(e->shortname));
1598 memcpy(&e->longname, &reply->longname, sizeof(e->longname));
1599 memcpy(&e->nodereport, &reply->nodereport, sizeof(e->nodereport));
1600 e->numbports = bytes_to_short(reply->numbportsH, reply->numbports);
1601 memcpy(&e->porttypes, &reply->porttypes, ARTNET_MAX_PORTS);
1602 memcpy(&e->goodinput, &reply->goodinput, ARTNET_MAX_PORTS);
1603 memcpy(&e->goodinput, &reply->goodinput, ARTNET_MAX_PORTS);
1604 memcpy(&e->goodoutput, &reply->goodoutput, ARTNET_MAX_PORTS);
1605 memcpy(&e->swin, &reply->swin, ARTNET_MAX_PORTS);
1606 memcpy(&e->swout, &reply->swout, ARTNET_MAX_PORTS);
1607 e->swvideo = reply->swvideo;
1608 e->swmacro = reply->swmacro;
1609 e->swremote = reply->swremote;
1610 e->style = reply->style;
1611 memcpy(&e->mac, &reply->mac, ARTNET_MAC_SIZE);
1612 }
1613
1614 /*
1615 * find a node_entry in the node list
1616 */
1617 node_entry_private_t *find_private_entry(node n, artnet_node_entry e) {
1618 node_entry_private_t *tmp;
1619 if (!e)
1620 return NULL;
1621
1622 // check if this packet is in list
1623 for (tmp = n->node_list.first; tmp; tmp = tmp->next) {
1624 if (!memcmp(&e->ip, &tmp->pub.ip, 4))
1625 break;
1626 }
1627 return tmp;
1628 }
1629
1630
1631 void check_timeouts(node n) {
1632 time_t now = time(NULL);
1633
1634 if (n->firmware.peer.s_addr != 0
1635 && (now - n->firmware.last_time >= FIRMWARE_TIMEOUT_SECONDS)) {
1636
1637 printf("firmware timeout\n");
1638 reset_firmware_upload(n);
1639
1640 n->state.report_code = ARTNET_RCFIRMWAREFAIL;
1641 // spec says to set ArtPollReply->Status here, but don't know to what value
1642 }
1643 }