Debian Codegraph @

interfaces/artnet/private.h

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 * private.h
17 * Private definitions, data structures, macros and functions for libartnet
18 * Copyright (C) 2004-2007 Simon Newton
19 */
20
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #ifndef WIN32
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #endif
30
31 #include <string.h>
32 #include <stdlib.h>
33 #include <time.h>
34 #include <stdio.h>
35
36 #include "artnet.h"
37 #include "artnet/packets.h"
38 #include "misc.h"
39 #include "tod.h"
40
41 #ifndef ARTNET_PRIVATE_H
42 #define ARTNET_PRIVATE_H
43
44 // these are defined in artnet.c
45 extern int ARTNET_PORT;
46 extern char ARTNET_STRING[];
47 extern int ARTNET_STRING_SIZE;
48 extern uint8_t ARTNET_VERSION;
49 extern uint8_t OEM_HI;
50 extern uint8_t OEM_LO;
51 extern char ESTA_HI;
52 extern char ESTA_LO;
53 extern uint8_t TTM_BEHAVIOUR_MASK;
54 extern uint8_t TTM_REPLY_MASK;
55 extern uint8_t PROGRAM_NO_CHANGE;
56 extern uint8_t PROGRAM_DEFAULTS;
57 extern uint8_t PROGRAM_CHANGE_MASK;
58 extern uint8_t HIGH_NIBBLE;
59 extern uint8_t LOW_NIBBLE;
60 extern uint8_t STATUS_PROG_AUTH_MASK;
61 extern uint8_t PORT_STATUS_LPT_MODE;
62 extern uint8_t PORT_STATUS_SHORT;
63 extern uint8_t PORT_STATUS_ERROR;
64 extern uint8_t PORT_STATUS_DISABLED_MASK;
65 extern uint8_t ORT_STATUS_MERGE;
66 extern uint8_t PORT_STATUS_DMX_TEXT;
67 extern uint8_t PORT_STATUS_DMX_SIP;
68 extern uint8_t PORT_STATUS_DMX_TEST;
69 extern uint8_t PORT_STATUS_ACT_MASK;
70 extern uint8_t PORT_DISABLE_MASK;
71 extern uint8_t TOD_RESPONSE_FULL;
72 extern uint8_t TOD_RESPONSE_NAK;
73 extern uint8_t MIN_PACKET_SIZE;
74 extern uint8_t MERGE_TIMEOUT_SECONDS;
75 extern uint8_t FIRMWARE_TIMEOUT_SECONDS;
76 extern uint8_t RECV_NO_DATA;
77
78 #ifndef TRUE
79 extern int TRUE;
80 extern int FALSE;
81 #endif
82
83 extern uint16_t LOW_BYTE;
84 extern uint16_t HIGH_BYTE;
85
86 // non artnet specific
87 #define SA struct sockaddr
88 #define SI struct in_addr
89
90 #ifndef min
91 #define min(a, b) ((a) < (b) ? (a) : (b))
92 #endif
93
94 #ifndef max
95 #define max(a, b) ((a) > (b) ? (a) : (b))
96 #endif
97
98 #ifndef INVALID_SOCKET
99 #define INVALID_SOCKET -1
100 #endif
101
102 // byte ordering macros
103 #ifndef bswap_16
104 #define bswap_16(x) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
105 #endif
106
107 // htols : convert short from host to little endian order
108 #ifdef WIN32
109 # define htols(x) (x)
110 #else
111 # ifdef HAVE_ENDIAN_H
112 # if BYTE_ORDER == __BIG_ENDIAN
113 # define htols(x) bswap_16 (x)
114 # else
115 # define htols(x) (x)
116 # endif
117 # else
118 # if BYTE_ORDER == BIG_ENDIAN
119 # define htols(x) bswap_16 (x)
120 # else
121 # define htols(x) (x)
122 # endif
123 # endif
124 #endif
125
126 // convert from shorts to bytes and back again
127 #define short_get_high_byte(x) ((HIGH_BYTE & x) >> 8)
128 #define short_get_low_byte(x) (LOW_BYTE & x)
129
130 #define bytes_to_short(h,l) ( ((h << 8) & 0xff00) | (l & 0x00FF) );
131
132 /*
133 * These are enums for fields in packets
134 * Ordered by packets they appear in. Some of these appear in the public header file
135 * artnet.h if they have to be used by the user.
136 *
137 * They are all in the form artnet_xxx_code in case they have to be made public some day
138 *
139 */
140
141 // ArtPollReply
142 //-----------------------------------------------------------------------------
143
144 // the node report codes
145 typedef enum {
146 ARTNET_RCDEBUG,
147 ARTNET_RCPOWEROK,
148 ARTNET_RCPOWERFAIL,
149 ARTNET_RCSOCKETWR1,
150 ARTNET_RCPARSEFAIL,
151 ARTNET_RCUDPFAIL,
152 ARTNET_RCSHNAMEOK,
153 ARTNET_RCLONAMEOK,
154 ARTNET_RCDMXERROR,
155 ARTNET_RCDMXUDPFULL,
156 ARTNET_RCDMXRXFULL,
157 ARTNET_RCSWITCHERR,
158 ARTNET_RCCONFIGERR,
159 ARTNET_RCDMXSHORT,
160 ARTNET_RCFIRMWAREFAIL,
161 ARTNET_RCUSERFAIL
162 } artnet_node_report_code;
163
164 // these define the types of node that can exist
165 // note it's different from artnet_node_type
166 typedef enum {
167 STNODE = 0x00,
168 STSERVER = 0x01,
169 STMEDIA = 0x02,
170 STROUTE = 0x03,
171 STBACKUP = 0x04,
172 STCONFIG = 0x05
173 } artnet_node_style_code;
174
175 // artnet_port_data in artnet.h
176
177 // ArtAddress
178 //-----------------------------------------------------------------------------
179
180 // artnet_port_command_code in artnet.h
181
182 // ArtFirmwareMaster
183 //-----------------------------------------------------------------------------
184
185 // defines contents of the firmware packet
186 typedef enum {
187 ARTNET_FIRMWARE_FIRMFIRST = 0x00,
188 ARTNET_FIRMWARE_FIRMCONT = 0x01,
189 ARTNET_FIRMWARE_FIRMLAST = 0x02,
190 ARTNET_FIRMWARE_UBEAFIRST = 0x03,
191 ARTNET_FIRMWARE_UBEACONT = 0x04,
192 ARTNET_FIRMWARE_UBEALAST = 0x05,
193 } artnet_firmware_type_code;
194
195 // ArtFirmwareReply
196 //-----------------------------------------------------------------------------
197
198 // artnet_firmware_status_code in artnet.h
199
200
201
202 // End packet enums
203 //-----------------------------------------------------------------------------
204
205
206 /*
207 * Data structures to manage callbacks
208 */
209
210 // packet callbacks have a function and some user data
211 typedef struct {
212 int (*fh)(artnet_node n, void *p, void *data);
213 void *data;
214 } callback_t;
215
216 /*
217 * the dmx callback is triggered when a dmx packet arrives
218 */
219 typedef struct {
220 int (*fh)(artnet_node n, int portid, void *data);
221 void *data;
222 } dmx_callback_t;
223
224 /*
225 * firmware callback is triggered when a firmware recieve has been completed sucessfully
226 */
227 typedef struct {
228 int (*fh)(artnet_node n, int ubea, uint16_t *data, int length, void *d);
229 void *data;
230 } firmware_callback_t;
231
232 /*
233 * called when a node is remote programmed
234 */
235 typedef struct {
236 int (*fh)(artnet_node n, void *d);
237 void *data;
238 } program_callback_t;
239
240 /*
241 * called when a node receives rdm data
242 */
243 typedef struct {
244 int (*fh)(artnet_node n, int address, uint8_t *rdm, int length, void *d);
245 void *data;
246 } rdm_callback_t;
247
248
249 // struct to store callbacks
250 typedef struct {
251 callback_t recv;
252 callback_t send;
253 callback_t poll;
254 callback_t reply;
255 callback_t dmx;
256 callback_t address;
257 callback_t input;
258 callback_t todrequest;
259 callback_t toddata;
260 callback_t todcontrol;
261 callback_t rdm;
262 callback_t ipprog;
263 callback_t firmware;
264 callback_t firmware_reply;
265 dmx_callback_t dmx_c;
266 firmware_callback_t firmware_c;
267 program_callback_t program_c;
268 rdm_callback_t rdm_c;
269 dmx_callback_t rdm_init_c;
270 dmx_callback_t rdm_tod_c;
271 } node_callbacks_t;
272
273
274 // End callback structures
275 //-----------------------------------------------------------------------------
276
277 /*
278 * Begin port structures
279 */
280
281
282
283 // first a generic port
284 typedef struct {
285 uint8_t addr; // the port address
286 uint8_t default_addr; // the address set by the hardware
287 uint8_t net_ctl; // if the port address is under network control
288 uint8_t status; // status of the port
289 uint8_t enabled; // true if the port has had it's address set, this is internal only,
290 // it's not used by the ArtNet protocol, otherwise the node keeps
291 // picking up packets for the 0x00 port
292 tod_t tod;
293 } g_port_t;
294
295 /**
296 * struct to represent an input port
297 * input ports need to keep track of sequence numbers
298 * (this doesn't seem to be used though)
299 */
300 typedef struct {
301 g_port_t port;
302 uint8_t seq;
303 } input_port_t;
304
305
306 /**
307 * For output ports we need to track if they merge in HTP or LTP modes
308 */
309 typedef enum {
310 ARTNET_MERGE_HTP,
311 ARTNET_MERGE_LTP
312 } merge_t;
313
314 /**
315 * struct to represent an output port
316 *
317 * output ports can merge data from two sources in either HTP
318 * (highest takes precedence) or LPT (lowest takes precedence) mode
319 *
320 * we need to store:
321 * o The data from each source
322 * o The ip of the source
323 * o The time the data was recv'ed
324 * o the tod table, length and max length
325 */
326 typedef struct {
327 g_port_t port;
328 int length; // the length of the data THAT HAS CHANGED since the last dmx packet
329 uint8_t enabled; // true if the port has had it's address set, this is internal only,
330 // it's not used by the ArtNet protocol, otherwise the node keeps
331 // picking up packets for the 0x00 port
332 uint8_t data[ARTNET_DMX_LENGTH]; // output data
333 merge_t merge_mode; // for merging
334 uint8_t dataA[ARTNET_DMX_LENGTH];
335 uint8_t dataB[ARTNET_DMX_LENGTH];
336 time_t timeA;
337 time_t timeB;
338 SI ipA;
339 SI ipB;
340 } output_port_t;
341
342 // use defines to hide the inner structures
343 #define port_addr port.addr
344 #define port_default_addr port.default_addr
345 #define port_net_ctl port.net_ctl
346 #define port_status port.status
347 #define port_enabled port.enabled
348 #define port_tod port.tod
349
350 // End port structures
351 //-----------------------------------------------------------------------------
352
353
354 /*
355 * We use a linked list to keep track of nodes on the network
356 * Here be the structures
357 */
358
359 /* firstly we have the potential to do a firmware transfer to any node,
360 * this struct keeps the information such as how much data has been transfered
361 * and the address of the peer. It's also used for receiving firmware
362 */
363 typedef struct {
364 uint16_t *data;
365 int bytes_current;
366 int bytes_total;
367 struct in_addr peer;
368 int ubea;
369 time_t last_time;
370 int expected_block;
371 int (*callback)(artnet_node n, artnet_firmware_status_code code, void *d);
372 void *user_data;
373 } firmware_transfer_t;
374
375 /*
376 * The node entry in the LL. It contains the public entry, as well as some stuff
377 * we don't want public like firmware
378 */
379 typedef struct node_entry_private_s {
380 artnet_node_entry_t pub;
381 struct node_entry_private_s *next;
382 firmware_transfer_t firmware;
383 SI ip; // don't rely on the ip address that the node
384 // sends, they could be faking it. This is the ip that
385 // the pollreply was sent from
386 } node_entry_private_t;
387
388 /**
389 * The node list stores a pointer to the first, last and current
390 * entries.
391 */
392 typedef struct {
393 node_entry_private_t *first;
394 node_entry_private_t *current;
395 node_entry_private_t *last;
396 int length;
397 } node_list_t;
398
399
400 // End node list structures
401 //-----------------------------------------------------------------------------
402
403
404 // the status of the node
405 typedef enum {
406 ARTNET_OFF,
407 ARTNET_STANDBY,
408 ARTNET_ON
409 } node_status_t;
410
411
412 // struct to hold the state of the node
413 typedef struct {
414 artnet_node_type node_type;
415 node_status_t mode;
416 SI reply_addr;
417 SI ip_addr;
418 SI bcast_addr;
419 uint8_t hw_addr[ARTNET_MAC_SIZE];
420 uint8_t default_subnet;
421 uint8_t subnet_net_ctl;
422 int send_apr_on_change;
423 int ar_count;
424 int verbose;
425 char short_name[ARTNET_SHORT_NAME_LENGTH];
426 char long_name[ARTNET_LONG_NAME_LENGTH];
427 char report[ARTNET_REPORT_LENGTH];
428 uint8_t subnet;
429 uint8_t oem_hi;
430 uint8_t oem_lo;
431 uint8_t esta_hi;
432 uint8_t esta_lo;
433 int bcast_limit; // the number of nodes after which we change to bcast
434 artnet_node_report_code report_code;
435 } node_state_t;
436
437
438 typedef struct {
439 struct artnet_node_s *peer; // peer if we've joined a group
440 int master;
441 } node_peering_t;
442
443
444 /**
445 * The main node structure
446 */
447 typedef struct artnet_node_s{
448 int sd; // the two sockets
449 node_state_t state; // the state struct
450 node_callbacks_t callbacks; // the callbacks struct
451 struct ports_s {
452 uint8_t types[ARTNET_MAX_PORTS]; // type of port
453 input_port_t in[ARTNET_MAX_PORTS]; // input ports
454 output_port_t out[ARTNET_MAX_PORTS]; // output ports
455 } ports;
456 artnet_reply_t ar_temp; // buffered artpoll reply packet
457 node_list_t node_list; // node list
458 firmware_transfer_t firmware; // firmware details
459 node_peering_t peering; // peer if we've joined a group
460 } artnet_node_t;
461
462
463 typedef artnet_node_t *node;
464
465 /*
466 * Function definitions follow
467 */
468
469 // exported from artnet.c
470 node_entry_private_t *find_private_entry( node n, artnet_node_entry e);
471 void check_timeouts(node n);
472 node_entry_private_t *find_entry_from_ip(node_list_t *nl, SI ip);
473 int artnet_nl_update(node_list_t *nl, artnet_packet reply);
474
475
476 // exported from receive.c
477 int handle(node n, artnet_packet p);
478 int16_t get_type(artnet_packet p);
479 void reset_firmware_upload(node n);
480
481
482 // exported from transmit.c
483 int artnet_tx_poll(node n, const char *ip, artnet_ttm_value_t ttm);
484 int artnet_tx_poll_reply(node n, int reply);
485 int artnet_tx_tod_data(node n, int id);
486 int artnet_tx_firmware_reply(node n, in_addr_t ip, artnet_firmware_status_code code);
487 int artnet_tx_firmware_packet(node n, firmware_transfer_t *firm );
488 int artnet_tx_tod_request(node n);
489 int artnet_tx_tod_control(node n, uint8_t address, artnet_tod_command_code action);
490 int artnet_tx_rdm(node n, uint8_t address, uint8_t *data, int length);
491 int artnet_tx_build_art_poll_reply(node n);
492
493
494 // exported from network.c
495 int artnet_net_recv(node n, artnet_packet p, int block);
496 int artnet_net_send(node n, artnet_packet p);
497 int artnet_net_set_non_block(node n);
498 int artnet_net_init(node n, const char *ip);
499 int artnet_net_start(node n);
500 int artnet_net_close(int sock);
501 int artnet_net_join(node n1, node n2);
502 int artnet_net_set_fdset(node n, fd_set *fdset);
503 int artnet_net_inet_aton(const char *ip_address, struct in_addr *address);
504 const char *artnet_net_last_error();
505
506 #endif