forked from forkineye/E131
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathE131.cpp
133 lines (115 loc) · 3.5 KB
/
E131.cpp
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
/*
* Project: E131 - E.131 (sACN) library for Particle Devices
* Copyright (c) 2017 AdmiralTriggerHappy
* Dervived from work by:
* Copyright (c) 2015 Shelby Merrick
* http://www.forkineye.com
*
* This program is provided free for you to use in any way that you wish,
* subject to the laws and regulations where you are using it. Due diligence
* is strongly suggested before using this code. Please give credit where due.
*
* The Author makes no warranty of any kind, express or implied, with regard
* to this program or the documentation contained in this document. The
* Author shall not be liable in any event for incidental or consequential
* damages in connection with, or arising out of, the furnishing, performance
* or use of these programs.
*
*/
#include "E131.h"
uint8_t sequence; /* Sequence tracker */
UDP udp;
/**
* Constructor.
*/
E131::E131()
{
// be sure not to call anything that requires hardware be initialized here, put those in begin()
}
/**
* Example method.
*/
void E131::begin()
{
// initialize hardware
IPAddress multicast(239,255,0,1);
udp.begin(E131_DEFAULT_PORT);
udp.joinMulticast(multicast);
packet = &packetBuffer;
Serial.println("called begin");
}
void E131::begin(uint16_t _universe)
{
// initialize hardware
IPAddress multicast(239,255,((_universe >> 8) & 0xff),((_universe >> 0) & 0xff));
udp.begin(E131_DEFAULT_PORT);
udp.joinMulticast(multicast);
packet = &packetBuffer;
Serial.println("called begin");
}
/* Packet validater */
e131_error_t E131::validateE131Packet()
{
if (memcmp(packet->acn_id, ACN_ID, sizeof(packet->acn_id)) !=0)
return ERROR_ACN_ID;
if (htonl(packet->root_vector) != VECTOR_ROOT)
return ERROR_VECTOR_ROOT;
if (htonl(packet->frame_vector) != VECTOR_FRAME)
return ERROR_VECTOR_FRAME;
if (packet->dmp_vector != VECTOR_DMP)
return ERROR_VECTOR_DMP;
return ERROR_NONE;
}
void E131::dumpError(e131_error_t error)
{
switch (error) {
case ERROR_ACN_ID:
Serial.print(F("INVALID PACKET ID: "));
for (uint8_t i = 0; i < sizeof(ACN_ID); i++)
Serial.print(packet->acn_id[i], HEX);
Serial.println("");
break;
case ERROR_PACKET_SIZE:
Serial.println(F("INVALID PACKET SIZE: "));
break;
case ERROR_VECTOR_ROOT:
Serial.print(F("INVALID ROOT VECTOR: 0x"));
Serial.println(htonl(packet->root_vector), HEX);
break;
case ERROR_VECTOR_FRAME:
Serial.print(F("INVALID FRAME VECTOR: 0x"));
Serial.println(htonl(packet->frame_vector), HEX);
break;
case ERROR_VECTOR_DMP:
Serial.print(F("INVALID DMP VECTOR: 0x"));
Serial.println(packet->dmp_vector, HEX);
}
}
uint16_t E131::parsePacket()
{
e131_error_t error;
uint16_t retval = 0;
int size = udp.receivePacket(packet->raw, E131_PACKET_SIZE);
if (size > 0)
{
error = validateE131Packet();
if (!error)
{
universe = htons(packet->universe);
data = packet->property_values;
retval = htons(packet->property_value_count) - 1;
if (packet->sequence_number != sequence++)
{
stats.sequence_errors++;
sequence = packet->sequence_number + 1;
}
stats.num_packets++;
}
else
{
dumpError(error);
stats.packet_errors++;
}
}
return retval;
}