-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathartnet.cpp
More file actions
402 lines (350 loc) · 12.2 KB
/
Copy pathartnet.cpp
File metadata and controls
402 lines (350 loc) · 12.2 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
/*
Copyright 2019 Tinic Uro
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdint.h>
#include <string.h>
#include <algorithm>
extern "C" {
#include "lwip/udp.h"
}; //extern "C" {
#include "./artnet.h"
#include "./model.h"
#include "./control.h"
#include "./systick.h"
#include "./netconf.h"
#include "./perf.h"
#include "version.h"
namespace lightkraken {
static ArtSyncWatchDog syncWatchDog;
void ArtSyncWatchDog::feed() {
fedtime = Systick::instance().systemTime();
}
bool ArtSyncWatchDog::starved() {
uint32_t now = Systick::instance().systemTime();
if (fedtime == 0 || ((now - fedtime) > ArtSyncTimeout )) {
fedtime = 0;
return true;
}
return false;
}
class OutputPacket : public ArtNetPacket {
public:
OutputPacket() { };
size_t len() const;
uint8_t sequence() const;
uint8_t physical() const;
uint16_t universe() const;
const uint8_t *data() const { return &packet[18]; }
private:
virtual bool verify() const override;
};
class OutputNzsPacket : public ArtNetPacket {
public:
OutputNzsPacket() { };
size_t len() const;
uint8_t sequence() const;
uint8_t startCode() const;
uint16_t universe() const;
const uint8_t *data() const { return &packet[18]; }
private:
virtual bool verify() const override;
};
int ArtNetPacket::version() const {
return static_cast<int>((packet[10] << 8) | (packet[11]));
}
ArtNetPacket::Opcode ArtNetPacket::opcode() const {
return static_cast<Opcode>((packet[8]) | (packet[9] << 8));
}
ArtNetPacket::Opcode ArtNetPacket::maybeValid(const uint8_t *buf, size_t len) {
bool bufValid = buf ? true : false;
bool sizeValid = len <= sizeof(packet);
bool validSignature = memcmp(buf, "Art-Net", 8) == 0;
bool opcodeValid = false;
Opcode opcode = static_cast<Opcode>((buf[8]) | (buf[9] << 8));
switch (opcode) {
case OpPoll:
case OpPollReply:
case OpDiagData:
case OpCommand:
case OpOutput:
case OpNzs:
case OpSync:
case OpAddress:
case OpInput:
case OpTodRequest:
case OpTodData:
case OpTodControl:
case OpRdm:
case OpRdmSub:
case OpVideoSetup:
case OpVideoPalette:
case OpVideoData:
case OpMacMaster:
case OpMacSlave:
case OpFirmwareMaster:
case OpFirmwareReply:
case OpFileTnMaster:
case OpFileFnMaster:
case OpFileFnReply:
case OpIpProg:
case OpIpProgReply:
case OpMedia:
case OpMediaPatch:
case OpMediaControl:
case OpMediaContrlReply:
case OpTimeCode:
case OpTimeSync:
case OpTrigger:
case OpDirectory:
case OpDirectoryReply:
opcodeValid = true;
break;
default:
opcodeValid = false;
break;
}
bool versionValid = static_cast<int>((buf[10] << 8) | (buf[11])) >= currentVersion;
return (bufValid &&
sizeValid &&
validSignature &&
opcodeValid &&
versionValid) ? opcode : OpInvalid;
}
__attribute__ ((hot, flatten, optimize("O3"), optimize("unroll-loops")))
static void memcpy_fast_aligned(uint8_t * dst, const uint8_t *src, size_t len) {
uint8_t *d = (uint8_t *)__builtin_assume_aligned (dst, 4);
const uint8_t *s = (const uint8_t *)__builtin_assume_aligned (src, 4);
for (size_t c = 0; c < len; c++) {
d[c] = s[c];
}
}
bool ArtNetPacket::verify(ArtNetPacket &packet, const uint8_t *buf, size_t len) {
Opcode opcode = maybeValid(buf, len);
if (opcode == OpInvalid) {
return false;
}
memcpy_fast_aligned(packet.packet, buf, std::min(len, sizeof(packet.packet)));
switch (opcode) {
case OpPoll:
case OpSync:
case OpNzs:
case OpOutput: {
return packet.verify();
} break;
default: {
return false;
} break;
}
return false;
}
static constexpr uint32_t syncTimeout = 4;
void ArtNetPacket::sendArtPollReply(const ip_addr_t *from, uint16_t universe) {
struct ArtPollReply {
uint8_t artNet[8];
uint16_t opCode;
uint8_t ipAddress[4];
uint16_t portNumber;
uint16_t versionInfo;
uint8_t netSwitch;
uint8_t subSwitch;
uint16_t oem;
uint8_t uebaVersion;
uint8_t status1;
uint16_t estaManufactor;
uint8_t shortName[18];
uint8_t longName[64];
uint8_t nodeReport[64];
uint16_t numPorts;
uint8_t portTypes[4];
uint8_t goodInput[4];
uint8_t goodOutput[4];
uint8_t swIn[4];
uint8_t swOut[4];
uint8_t swVideo;
uint8_t swMacro;
uint8_t swRemote;
uint8_t spare1;
uint8_t spare2;
uint8_t spare3;
uint8_t style;
uint8_t macAddress[6];
uint8_t bindIp[4];
uint8_t bindIndex;
uint8_t status2;
uint8_t filler[26];
} __attribute__((packed)) reply;
memset(&reply, 0, sizeof(reply));
reply.opCode = OpPollReply;
memcpy(reply.artNet, "Art-Net", 8);
reply.ipAddress[0] = ip4_addr1(&NetConf::instance().netInterface()->ip_addr);
reply.ipAddress[1] = ip4_addr2(&NetConf::instance().netInterface()->ip_addr);
reply.ipAddress[2] = ip4_addr3(&NetConf::instance().netInterface()->ip_addr);
reply.ipAddress[3] = ip4_addr4(&NetConf::instance().netInterface()->ip_addr);
reply.portNumber = 6454;
reply.versionInfo = GIT_REV_COUNT;
reply.netSwitch = (universe >> 8) & 0xFF;
reply.subSwitch = (universe >> 0) & 0xFF;
reply.oem = 0x1ed5;
reply.estaManufactor = 0x1ed5;
const char short_hostname_base[] = "lk-";
const char hex_table[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f',};
char short_hostname[sizeof(short_hostname_base)+8];
memset(short_hostname, 0, sizeof(short_hostname));
strcpy(short_hostname, short_hostname_base);
uint32_t short_mac = (NetConf::instance().netInterface()->hwaddr[2] << 24) |
(NetConf::instance().netInterface()->hwaddr[3] << 16) |
(NetConf::instance().netInterface()->hwaddr[4] << 8) |
(NetConf::instance().netInterface()->hwaddr[5] << 0);
for (size_t c=0; c<8; c++) {
short_hostname[c + sizeof(short_hostname_base) - 1] = hex_table[(short_mac>>(32-((c+1)*4)))&0xF];
}
strncpy((char *)reply.shortName, short_hostname, 17);
if (strlen(Model::instance().tag())) {
snprintf((char *)reply.longName, 63, "%.16s - %.16s",
NetConf::instance().netInterface()->hostname,
Model::instance().tag());
} else {
snprintf((char *)reply.longName, 63, "%.16s",
NetConf::instance().netInterface()->hostname);
}
memcpy(reply.macAddress, NetConf::instance().netInterface()->hwaddr, 6);
reply.bindIp[0] = ip4_addr1(&NetConf::instance().netInterface()->ip_addr);
reply.bindIp[1] = ip4_addr2(&NetConf::instance().netInterface()->ip_addr);
reply.bindIp[2] = ip4_addr3(&NetConf::instance().netInterface()->ip_addr);
reply.bindIp[3] = ip4_addr4(&NetConf::instance().netInterface()->ip_addr);
reply.status2 = 0x01 | // support web browser config
0x02 | // supports dhcp
(Model::instance().dhcpEnabled() ? 0x04 : 0x00) |
0x08; // ArtNet3
NetConf::instance().sendArtNetUdpPacket(from, 6454, (const uint8_t *)&reply, sizeof(reply));
}
bool ArtNetPacket::dispatch(const ip_addr_t *from, const uint8_t *buf, size_t len, bool isBroadcast) {
PerfMeasure perf(PerfMeasure::SLOT_ARNET_DISPATCH);
Opcode opcode = ArtNetPacket::maybeValid(buf, len);
if (opcode == OpInvalid) {
return false;
}
switch(opcode) {
case OpPoll: {
Control::instance().interateAllActiveArtnetUniverses([from](uint16_t universe) {
Systick::instance().schedulePollReply(from, universe);
});
return true;
} break;
case OpSync: {
if (!Model::instance().broadcastEnabled() && isBroadcast) {
return false;
}
Control::instance().setEnableSyncMode(true);
Control::instance().sync();
syncWatchDog.feed();
return true;
} break;
case OpNzs: {
if (!Model::instance().broadcastEnabled() && isBroadcast) {
return false;
}
OutputNzsPacket outputPacket;
if (ArtNetPacket::verify(outputPacket, buf, len)) {
lightkraken::Control::instance().setArtnetUniverseOutputData(outputPacket.universe(), outputPacket.data(), outputPacket.len());
if(Control::instance().syncModeEnabled() && syncWatchDog.starved()) {
Control::instance().sync();
Control::instance().setEnableSyncMode(false);
}
return true;
}
} break;
case OpOutput: {
if (!Model::instance().broadcastEnabled() && isBroadcast) {
return false;
}
OutputPacket outputPacket;
if (ArtNetPacket::verify(outputPacket, buf, len)) {
lightkraken::Control::instance().setArtnetUniverseOutputData(outputPacket.universe(), outputPacket.data(), outputPacket.len());
if(Control::instance().syncModeEnabled() && syncWatchDog.starved()) {
Control::instance().sync();
Control::instance().setEnableSyncMode(false);
}
return true;
}
} break;
default: {
return false;
} break;
}
return false;
}
size_t OutputPacket::len() const {
return (packet[16] << 8) | packet[17];
}
uint16_t OutputPacket::universe() const {
return (packet[14]) | (packet[15]<<8);
}
uint8_t OutputPacket::sequence() const {
return packet[12];
}
uint8_t OutputPacket::physical() const {
return packet[13];
}
bool OutputPacket::verify() const {
if (len() < 2) {
return false;
}
if (len() > 512) {
return false;
}
if ((len() & 1) == 1) {
return false;
}
if (universe() >= 32768) {
return false;
}
return true;
}
size_t OutputNzsPacket::len() const {
return (packet[16] << 8) | packet[17];
}
uint16_t OutputNzsPacket::universe() const {
return (packet[14]) | (packet[15]<<8);
}
uint8_t OutputNzsPacket::sequence() const {
return packet[12];
}
uint8_t OutputNzsPacket::startCode() const {
return packet[13];
}
bool OutputNzsPacket::verify() const {
if (len() < 2) {
return false;
}
if (len() > 512) {
return false;
}
if ((len() & 1) == 1) {
return false;
}
if (universe() >= 32768) {
return false;
}
if (startCode() != 0) {
return false;
}
return true;
}
};