Debian Codegraph @

interfaces/artnet/transmit.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 * transmit.c
17 * Functions to handle sending datagrams
18 * Copyright (C) 2004-2005 Simon Newton
19 */
20
21 #include "private.h"
22
23 /*
24 * Send an art poll
25 *
26 * @param ip the ip address to send to
27 * @param ttm the talk to me value, either ARTNET_TTM_DEFAULT,
28 * ARTNET_TTM_PRIVATE or ARTNET_TTM_AUTO
29 */
30 int artnet_tx_poll(node n, const char *ip, artnet_ttm_value_t ttm) {
31 artnet_packet_t p;
32 int ret;
33
34 if (n->state.mode != ARTNET_ON)
35 return ARTNET_EACTION;
36
37 if (n->state.node_type == ARTNET_SRV || n->state.node_type == ARTNET_RAW) {
38 if (ip) {
39 ret = artnet_net_inet_aton(ip, &p.to);
40 if (ret)
41 return ret;
42 } else {
43 p.to.s_addr = n->state.bcast_addr.s_addr;
44 }
45
46 memcpy(&p.data.ap.id, ARTNET_STRING, ARTNET_STRING_SIZE);
47 p.data.ap.opCode = htols(ARTNET_POLL);
48 p.data.ap.verH = 0;
49 p.data.ap.ver = ARTNET_VERSION;
50 p.data.ap.ttm = ~ttm;
51 p.data.ap.pad = 0;
52
53 p.length = sizeof(artnet_poll_t);
54 return artnet_net_send(n, &p);
55
56 } else {
57 artnet_error("Not sending poll, not a server or raw device");
58 return ARTNET_EACTION;
59 }
60 }
61
62 /*
63 * Send an ArtPollReply
64 * @param n the node
65 * @param response true if this reply is in response to a network packet
66 * false if this reply is due to the node changing it's conditions
67 */
68 int artnet_tx_poll_reply(node n, int response) {
69 artnet_packet_t reply;
70 int i;
71
72 if (!response && n->state.mode == ARTNET_ON) {
73 n->state.ar_count++;
74 }
75
76 reply.to = n->state.reply_addr;
77 reply.type = ARTNET_REPLY;
78 reply.length = sizeof(artnet_reply_t);
79
80 // copy from a poll reply template
81 memcpy(&reply.data, &n->ar_temp, sizeof(artnet_reply_t));
82
83 for (i=0; i< ARTNET_MAX_PORTS; i++) {
84 reply.data.ar.goodinput[i] = n->ports.in[i].port_status;
85 reply.data.ar.goodoutput[i] = n->ports.out[i].port_status;
86 }
87
88 snprintf((char *) &reply.data.ar.nodereport,
89 sizeof(reply.data.ar.nodereport),
90 "%04x [%04i] libartnet",
91 n->state.report_code,
92 n->state.ar_count);
93
94 return artnet_net_send(n, &reply);
95 }
96
97
98 /*
99 * Send a tod request
100 */
101 int artnet_tx_tod_request(node n) {
102 int i;
103 artnet_packet_t todreq;
104
105 todreq.to = n->state.bcast_addr;
106 todreq.type = ARTNET_TODREQUEST;
107 todreq.length = sizeof(artnet_todrequest_t);
108 memset(&todreq.data,0x00, todreq.length);
109
110 // set up the data
111 memcpy(&todreq.data.todreq.id, ARTNET_STRING, ARTNET_STRING_SIZE);
112 todreq.data.todreq.opCode = htols(ARTNET_TODREQUEST);
113 todreq.data.todreq.verH = 0;
114 todreq.data.todreq.ver = ARTNET_VERSION;
115 todreq.data.todreq.command = ARTNET_TOD_FULL; // todfull
116 todreq.data.todreq.adCount = 0;
117
118 // include all enabled ports
119 for (i=0; i < ARTNET_MAX_PORTS; i++) {
120 if (n->ports.out[i].port_enabled) {
121 todreq.data.todreq.address[todreq.data.todreq.adCount++] = n->ports.out[i].port_addr;
122 }
123 }
124
125 return artnet_net_send(n, &todreq);
126 }
127
128
129 /*
130 * Send a tod data for port number id
131 * @param id the number of the port to send data for
132 */
133 int artnet_tx_tod_data(node n, int id) {
134 artnet_packet_t tod;
135 int lim, remaining, bloc, offset;
136 int ret = ARTNET_EOK;
137
138 // ok we need to check how many uid's we have,
139 // may need to send more than one datagram
140
141 tod.to = n->state.bcast_addr;
142 tod.type = ARTNET_TODDATA;
143 tod.length = sizeof(artnet_toddata_t);
144
145 memset(&tod.data,0x00, tod.length);
146
147 // set up the data
148 memcpy(&tod.data.toddata.id, ARTNET_STRING, ARTNET_STRING_SIZE);
149 tod.data.toddata.opCode = htols(ARTNET_TODDATA);
150 tod.data.toddata.verH = 0;
151 tod.data.toddata.ver = ARTNET_VERSION;
152 tod.data.toddata.port = id;
153
154 // this is interesting, the spec mentions TOD_ADD and TOD_SUBTRACT, but the
155 // codes aren't given. The windows drivers don't have these either....
156 tod.data.toddata.cmdRes = ARTNET_TOD_FULL;
157
158 tod.data.toddata.address = n->ports.out[id].port_addr;
159 tod.data.toddata.uidTotalHi = short_get_high_byte(n->ports.out[id].port_tod.length);
160 tod.data.toddata.uidTotal = short_get_low_byte(n->ports.out[id].port_tod.length);
161
162 remaining = n->ports.out[id].port_tod.length;
163 bloc = 0;
164
165 while (remaining > 0) {
166 memset(&tod.data.toddata.tod,0x00, ARTNET_MAX_UID_COUNT);
167 lim = min(ARTNET_MAX_UID_COUNT, remaining);
168 tod.data.toddata.blockCount = bloc++;
169 tod.data.toddata.uidCount = lim;
170
171 offset = (n->ports.out[id].port_tod.length - remaining) * ARTNET_RDM_UID_WIDTH;
172 if (n->ports.out[id].port_tod.data != NULL)
173 memcpy(tod.data.toddata.tod,
174 n->ports.out[id].port_tod.data + offset,
175 lim * ARTNET_RDM_UID_WIDTH);
176
177 ret = ret || artnet_net_send(n, &tod);
178 remaining = remaining - lim;
179 }
180 return ret;
181 }
182
183
184 /*
185 * Send a tod data for port number id
186 * @param id the number of the port to send data for
187 */
188 int artnet_tx_tod_control(node n,
189 uint8_t address,
190 artnet_tod_command_code action) {
191 artnet_packet_t tod;
192
193 tod.to = n->state.bcast_addr;
194 tod.type = ARTNET_TODCONTROL;
195 tod.length = sizeof(artnet_todcontrol_t);
196
197 memset(&tod.data,0x00, tod.length);
198
199 // set up the data
200 memcpy(&tod.data.todcontrol.id, ARTNET_STRING, ARTNET_STRING_SIZE);
201 tod.data.todcontrol.opCode = htols(ARTNET_TODCONTROL);
202 tod.data.todcontrol.verH = 0;
203 tod.data.todcontrol.ver = ARTNET_VERSION;
204 tod.data.todcontrol.cmd = action;
205 tod.data.todcontrol.address = address;
206
207 return artnet_net_send(n, &tod);
208 }
209
210
211 /*
212 * Send a RDM message
213 * @param address the universe to address this datagram to
214 * @param action the action to perform. Either ARTNET_TOD_FULL or
215 * ARTNET_TOD_FLUSH
216 */
217 int artnet_tx_rdm(node n, uint8_t address, uint8_t *data, int length) {
218 artnet_packet_t rdm;
219 int len;
220
221 rdm.to = n->state.bcast_addr;
222 rdm.type = ARTNET_RDM;
223 rdm.length = sizeof(artnet_rdm_t);
224
225 memset(&rdm.data,0x00, rdm.length);
226
227 // set up the data
228 memcpy(&rdm.data.todcontrol.id, ARTNET_STRING, ARTNET_STRING_SIZE);
229 rdm.data.rdm.opCode = htols(ARTNET_RDM);
230 rdm.data.rdm.verH = 0;
231 rdm.data.rdm.ver = ARTNET_VERSION;
232 rdm.data.rdm.cmd = 0x00;
233 rdm.data.rdm.address = address;
234
235 len = min(length, ARTNET_MAX_RDM_DATA);
236 memcpy(&rdm.data.rdm.data, data, len);
237 return artnet_net_send(n, &rdm);
238
239 }
240
241
242 /*
243 * Send a firmware reply
244 * @param ip the ip address to send to
245 * @param code the response code
246 */
247 int artnet_tx_firmware_reply(node n, in_addr_t ip,
248 artnet_firmware_status_code code) {
249 artnet_packet_t p;
250 memset(&p, 0x0, sizeof(p));
251
252 p.to.s_addr = ip;
253 p.length = sizeof(artnet_firmware_t);
254 p.type = ARTNET_FIRMWAREREPLY;
255
256 // now build packet
257 memcpy(&p.data.firmware.id, ARTNET_STRING, ARTNET_STRING_SIZE);
258 p.data.firmware.opCode = htols(ARTNET_FIRMWAREREPLY);
259 p.data.firmware.verH = 0;
260 p.data.firmware.ver = ARTNET_VERSION;
261 p.data.firmware.type = code;
262
263 return artnet_net_send(n, &p);
264 }
265
266
267 /*
268 * Send an firmware data datagram
269 *
270 * @param firm a pointer to the firmware structure for this transfer
271 */
272 int artnet_tx_firmware_packet(node n, firmware_transfer_t *firm) {
273 artnet_packet_t p;
274 uint8_t type = 0;
275 int data_len, max_len, ret;
276
277 memset(&p, 0x0, sizeof(p));
278
279 // max value of data_len is 1024;
280 max_len = ARTNET_FIRMWARE_SIZE * sizeof(p.data.firmware.data[0]);
281
282 // calculate length
283 data_len = firm->bytes_total - firm->bytes_current;
284 data_len = min(data_len, max_len);
285
286 // work out type - 6 cases
287 if(firm->ubea) {
288 // ubea upload
289 if (firm->bytes_current == 0) {
290 // first
291 type = ARTNET_FIRMWARE_UBEAFIRST;
292 } else if (data_len == max_len) {
293 // cont
294 type = ARTNET_FIRMWARE_UBEACONT;
295 } else if (data_len < max_len) {
296 // last
297 type = ARTNET_FIRMWARE_UBEALAST;
298 } else {
299 // this should never happen, something has gone wrong
300 artnet_error("Attempting to send %d when the max is %d, very very bad...\n", data_len, max_len);
301 }
302 } else {
303 // firmware upload
304 if (firm->bytes_current == 0) {
305 // first
306 type = ARTNET_FIRMWARE_FIRMFIRST;
307 } else if (data_len == max_len) {
308 // cont
309 type = ARTNET_FIRMWARE_FIRMCONT;
310 } else if (data_len < max_len) {
311 // last
312 type = ARTNET_FIRMWARE_FIRMLAST;
313 } else {
314 // this should never happen, something has gone wrong
315 artnet_error("Attempting to send %d when the max is %d, very very bad...\n", data_len, max_len);
316 }
317 }
318
319 // set packet properties
320 p.to.s_addr = firm->peer.s_addr;
321 p.length = sizeof(artnet_firmware_t);
322 p.type = ARTNET_FIRMWAREMASTER;
323
324 // now build packet
325 memcpy(&p.data.firmware.id, ARTNET_STRING, ARTNET_STRING_SIZE);
326 p.data.firmware.opCode = htols(ARTNET_FIRMWAREMASTER);
327 p.data.firmware.verH = 0;
328 p.data.firmware.ver = ARTNET_VERSION;
329 p.data.firmware.type = type;
330 p.data.firmware.blockId = firm->expected_block;
331
332 artnet_misc_int_to_bytes(firm->bytes_total / sizeof(uint16_t),
333 p.data.firmware.length);
334
335 memcpy(&p.data.firmware.data,
336 firm->data + (firm->bytes_current / sizeof(uint16_t)),
337 data_len);
338
339 if ((ret = artnet_net_send(n, &p))) {
340 // send failed
341 return ret;
342 } else {
343 // update stats
344 firm->bytes_current = firm->bytes_current + data_len;
345 firm->last_time = time(NULL);
346 firm->expected_block++;
347 // limit between 0 and 255 (only 8 bits wide)
348 // we dont' actually need this cause it will be shorted when assigned above
349 firm->expected_block %= UINT8_MAX;
350 }
351 return ARTNET_EOK;
352 }
353
354
355 // this is called when the node's state changes to rebuild the
356 // artpollreply packet
357 int artnet_tx_build_art_poll_reply(node n) {
358 int i;
359
360 // shorten the amount we have to type
361 artnet_reply_t *ar = &n->ar_temp;
362
363 memset(ar, 0x00, sizeof(artnet_reply_t));
364
365 memcpy(&ar->id, ARTNET_STRING, ARTNET_STRING_SIZE);
366 ar->opCode = htols(ARTNET_REPLY);
367 memcpy(&ar->ip, &n->state.ip_addr.s_addr, 4);
368 ar->port = htols(ARTNET_PORT);
369 ar->verH = 0;
370 ar->ver = 0;
371 ar->subH = 0;
372 ar->sub = n->state.subnet;
373 ar->oemH = n->state.oem_hi;
374 ar->oem = n->state.oem_lo;
375 ar->ubea = 0;
376 // ar->status
377
378 // if(n->state
379
380 // status need to be recalc everytime
381 //ar->status
382
383 // ESTA Manufacturer ID
384 // Assigned 18/4/2006
385 ar->etsaman[0] = n->state.esta_hi;
386 ar->etsaman[1] = n->state.esta_lo;
387
388 memcpy(&ar->shortname, &n->state.short_name, sizeof(n->state.short_name));
389 memcpy(&ar->longname, &n->state.long_name, sizeof(n->state.long_name));
390
391 // the report is generated on every send
392
393 // port stuff here
394 ar->numbportsH = 0;
395
396 for (i = ARTNET_MAX_PORTS; i > 0; i--) {
397 if (n->ports.out[i-1].port_enabled || n->ports.in[i-1].port_enabled)
398 break;
399 }
400
401 ar->numbports = i;
402
403 for (i=0; i< ARTNET_MAX_PORTS; i++) {
404 ar->porttypes[i] = n->ports.types[i];
405 ar->goodinput[i] = n->ports.in[i].port_status;
406 ar->goodoutput[i] = n->ports.out[i].port_status;
407 ar->swin[i] = n->ports.in[i].port_addr;
408 ar->swout[i] = n->ports.out[i].port_addr;
409 }
410
411 ar->swvideo = 0;
412 ar->swmacro = 0;
413 ar->swremote = 0;
414
415 // spares
416 ar->sp1 = 0;
417 ar->sp2 = 0;
418 ar->sp3 = 0;
419
420 // hw address
421 memcpy(&ar->mac, &n->state.hw_addr, ARTNET_MAC_SIZE);
422
423 // set style
424 switch (n->state.node_type) {
425 case ARTNET_SRV:
426 ar->style = STSERVER;
427 break;
428 case ARTNET_NODE:
429 ar->style = STNODE;
430 break;
431 case ARTNET_MSRV:
432 ar->style = STMEDIA;
433 break;
434 // we should fix this, it'll do for now
435 case ARTNET_RAW:
436 ar->style = STNODE;
437 break;
438 default:
439 artnet_error("Node type not recognised!");
440 ar->style = STNODE;
441 return ARTNET_ESTATE;
442 }
443
444 return ARTNET_EOK;
445 }