ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
e131_packet.cpp
Go to the documentation of this file.
1#include <cstring>
2#include "e131.h"
3#ifdef USE_NETWORK
8
9#include <lwip/igmp.h>
10#include <lwip/init.h>
11#include <lwip/ip4_addr.h>
12#include <lwip/ip_addr.h>
14namespace esphome {
15namespace e131 {
17static const char *const TAG = "e131";
18
19static const uint8_t ACN_ID[12] = {0x41, 0x53, 0x43, 0x2d, 0x45, 0x31, 0x2e, 0x31, 0x37, 0x00, 0x00, 0x00};
20static const uint32_t VECTOR_ROOT = 4;
21static const uint32_t VECTOR_FRAME = 2;
22static const uint8_t VECTOR_DMP = 2;
24// E1.31 Packet Structure
25union E131RawPacket {
26 struct {
27 // Root Layer
28 uint16_t preamble_size;
29 uint16_t postamble_size;
30 uint8_t acn_id[12];
31 uint16_t root_flength;
32 uint32_t root_vector;
33 uint8_t cid[16];
34
35 // Frame Layer
36 uint16_t frame_flength;
37 uint32_t frame_vector;
38 uint8_t source_name[64];
39 uint8_t priority;
40 uint16_t reserved;
41 uint8_t sequence_number;
42 uint8_t options;
43 uint16_t universe;
44
45 // DMP Layer
46 uint16_t dmp_flength;
47 uint8_t dmp_vector;
48 uint8_t type;
49 uint16_t first_address;
50 uint16_t address_increment;
51 uint16_t property_value_count;
52 uint8_t property_values[E131_MAX_PROPERTY_VALUES_COUNT];
53 } __attribute__((packed));
54
55 uint8_t raw[638];
56};
57
58// We need to have at least one `1` value
59// Get the offset of `property_values[1]`
60const size_t E131_MIN_PACKET_SIZE = reinterpret_cast<size_t>(&((E131RawPacket *) nullptr)->property_values[1]);
61
64 return false;
65 if (this->socket_ == nullptr)
66 return false;
67
68 for (auto universe : universe_consumers_) {
69 if (!universe.second)
70 continue;
71
72 ip4_addr_t multicast_addr =
73 network::IPAddress(239, 255, ((universe.first >> 8) & 0xff), ((universe.first >> 0) & 0xff));
74
75 err_t err;
76 {
77 LwIPLock lock;
78 err = igmp_joingroup(IP4_ADDR_ANY4, &multicast_addr);
79 }
80
81 if (err) {
82 ESP_LOGW(TAG, "IGMP join for %d universe of E1.31 failed. Multicast might not work.", universe.first);
83 }
84 }
85
86 return true;
87}
88
90 // store only latest received packet for the given universe
91 auto consumers = ++universe_consumers_[universe];
92
93 if (consumers > 1) {
94 return; // we already joined before
95 }
96
97 if (join_igmp_groups_()) {
98 ESP_LOGD(TAG, "Joined %d universe for E1.31.", universe);
99 }
100}
101
103 auto consumers = --universe_consumers_[universe];
104
105 if (consumers > 0) {
106 return; // we have other consumers of the given universe
107 }
108
110 ip4_addr_t multicast_addr = network::IPAddress(239, 255, ((universe >> 8) & 0xff), ((universe >> 0) & 0xff));
111
112 LwIPLock lock;
113 igmp_leavegroup(IP4_ADDR_ANY4, &multicast_addr);
114 }
115
116 ESP_LOGD(TAG, "Left %d universe for E1.31.", universe);
117}
118
119bool E131Component::packet_(const std::vector<uint8_t> &data, int &universe, E131Packet &packet) {
120 if (data.size() < E131_MIN_PACKET_SIZE)
121 return false;
122
123 auto *sbuff = reinterpret_cast<const E131RawPacket *>(&data[0]);
124
125 if (memcmp(sbuff->acn_id, ACN_ID, sizeof(sbuff->acn_id)) != 0)
126 return false;
127 if (htonl(sbuff->root_vector) != VECTOR_ROOT)
128 return false;
129 if (htonl(sbuff->frame_vector) != VECTOR_FRAME)
130 return false;
131 if (sbuff->dmp_vector != VECTOR_DMP)
132 return false;
133 if (sbuff->property_values[0] != 0)
134 return false;
135
136 universe = htons(sbuff->universe);
137 packet.count = htons(sbuff->property_value_count);
139 return false;
140
141 memcpy(packet.values, sbuff->property_values, packet.count);
142 return true;
143}
144
145} // namespace e131
146} // namespace esphome
147#endif
Helper class to lock the lwIP TCPIP core when making lwIP API calls from non-TCPIP threads.
Definition helpers.h:750
E131ListenMethod listen_method_
Definition e131.h:48
std::unique_ptr< socket::Socket > socket_
Definition e131.h:49
std::map< int, int > universe_consumers_
Definition e131.h:51
bool packet_(const std::vector< uint8_t > &data, int &universe, E131Packet &packet)
struct @67::@68 __attribute__
uint16_t universe
uint8_t property_values[E131_MAX_PROPERTY_VALUES_COUNT]
in_addr ip4_addr_t
Definition ip_address.h:23
const int E131_MAX_PROPERTY_VALUES_COUNT
Definition e131.h:20
const size_t E131_MIN_PACKET_SIZE
@ E131_MULTICAST
Definition e131.h:18
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint8_t values[E131_MAX_PROPERTY_VALUES_COUNT]
Definition e131.h:24