ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
e131_packet.cpp
Go to the documentation of this file.
1#include <cstddef>
2#include <cstring>
3#include "e131.h"
4#ifdef USE_NETWORK
7#include "esphome/core/log.h"
8#include "esphome/core/util.h"
11#include <lwip/igmp.h>
12#include <lwip/init.h>
13#include <lwip/ip4_addr.h>
14#include <lwip/ip_addr.h>
16namespace esphome::e131 {
17
18ESPHOME_LOG_TAG(TAG, "e131");
20static const uint8_t ACN_ID[12] = {0x41, 0x53, 0x43, 0x2d, 0x45, 0x31, 0x2e, 0x31, 0x37, 0x00, 0x00, 0x00};
21static const uint32_t VECTOR_ROOT = 4;
22static const uint32_t VECTOR_FRAME = 2;
23static const uint8_t VECTOR_DMP = 2;
25// E1.31 Packet Structure
26union E131RawPacket {
27 struct {
28 // Root Layer
29 uint16_t preamble_size;
30 uint16_t postamble_size;
31 uint8_t acn_id[12];
32 uint16_t root_flength;
33 uint32_t root_vector;
34 uint8_t cid[16];
35
36 // Frame Layer
37 uint16_t frame_flength;
38 uint32_t frame_vector;
39 uint8_t source_name[64];
40 uint8_t priority;
41 uint16_t reserved;
42 uint8_t sequence_number;
43 uint8_t options;
44 uint16_t universe;
45
46 // DMP Layer
47 uint16_t dmp_flength;
48 uint8_t dmp_vector;
49 uint8_t type;
50 uint16_t first_address;
51 uint16_t address_increment;
52 uint16_t property_value_count;
53 uint8_t property_values[E131_MAX_PROPERTY_VALUES_COUNT];
54 } __attribute__((packed));
55
56 uint8_t raw[638];
57};
58
59// We need to have at least one `1` value
60// Get the offset of `property_values[1]`
61const size_t E131_MIN_PACKET_SIZE = offsetof(E131RawPacket, property_values) + sizeof(uint8_t);
62
64 if (this->listen_method_ != E131_MULTICAST || this->socket_ == nullptr)
65 return;
66 // Joined from loop() once the network is up; RP2040 has no interface to join before then
67 if (!network::is_connected()) {
68 this->igmp_join_pending_ = true;
69 return;
70 }
71 this->igmp_join_pending_ = false;
72 for (auto &entry : this->universe_consumers_) {
73 if (entry.consumers)
74 this->join_universe_(entry.universe);
75 }
76}
77
79 ip4_addr_t multicast_addr = network::IPAddress(239, 255, ((universe >> 8) & 0xff), ((universe >> 0) & 0xff));
80
81 err_t err;
82 {
84 err = igmp_joingroup(IP4_ADDR_ANY4, &multicast_addr);
85 }
86
87 if (err) {
88 ESP_LOGW(TAG, "IGMP join for %d universe of E1.31 failed. Multicast might not work.", universe);
89 } else {
90 ESP_LOGD(TAG, "Joined %d universe for E1.31.", universe);
91 }
92}
93
95 for (auto &entry : this->universe_consumers_) {
96 if (entry.universe == universe)
97 return &entry;
98 }
99 return nullptr;
100}
101
103 // store only latest received packet for the given universe
104 auto *consumer = this->find_universe_(universe);
105 if (consumer != nullptr) {
106 if (consumer->consumers++ > 0) {
107 return; // we already joined before
108 }
109 } else {
110 this->universe_consumers_.push_back({static_cast<uint16_t>(universe), 1});
111 }
112
113 if (this->listen_method_ != E131_MULTICAST || this->socket_ == nullptr)
114 return;
115 // A pending batch join covers this universe too; joining it here as well would break leave_()
117 ESP_LOGD(TAG, "IGMP join for %d universe of E1.31 deferred until the network is up.", universe);
118 this->igmp_join_pending_ = true;
119 return;
120 }
121 this->join_universe_(universe);
122}
123
125 auto *consumer = this->find_universe_(universe);
126 if (consumer == nullptr)
127 return;
128
129 if (--consumer->consumers > 0) {
130 return; // we have other consumers of the given universe
131 }
132
133 if (this->listen_method_ == E131_MULTICAST) {
134 ip4_addr_t multicast_addr = network::IPAddress(239, 255, ((universe >> 8) & 0xff), ((universe >> 0) & 0xff));
135
137 igmp_leavegroup(IP4_ADDR_ANY4, &multicast_addr);
138 }
139
140 ESP_LOGD(TAG, "Left %d universe for E1.31.", universe);
141}
142
143bool E131Component::packet_(const uint8_t *data, size_t len, int &universe, E131Packet &packet) {
145 return false;
146
147 auto *sbuff = reinterpret_cast<const E131RawPacket *>(data);
148
149 if (memcmp(sbuff->acn_id, ACN_ID, sizeof(sbuff->acn_id)) != 0)
150 return false;
151 if (htonl(sbuff->root_vector) != VECTOR_ROOT)
152 return false;
153 if (htonl(sbuff->frame_vector) != VECTOR_FRAME)
154 return false;
155 if (sbuff->dmp_vector != VECTOR_DMP)
156 return false;
157 if (sbuff->property_values[0] != 0)
158 return false;
159
160 universe = htons(sbuff->universe);
161 packet.count = htons(sbuff->property_value_count);
163 return false;
164
165 memcpy(packet.values, sbuff->property_values, packet.count);
166 return true;
167}
168
169} // namespace esphome::e131
170
171#endif
Helper class to lock the lwIP TCPIP core when making lwIP API calls from non-TCPIP threads.
Definition helpers.h:2183
void join_universe_(uint16_t universe)
E131ListenMethod listen_method_
Definition e131.h:52
bool packet_(const uint8_t *data, size_t len, int &universe, E131Packet &packet)
std::unique_ptr< socket::UDPSocket > socket_
Definition e131.h:54
UniverseConsumer * find_universe_(int universe)
std::vector< UniverseConsumer > universe_consumers_
Definition e131.h:56
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:45
uint16_t universe
uint8_t property_values[E131_MAX_PROPERTY_VALUES_COUNT]
struct in_addr ip4_addr_t
Definition ip_address.h:23
const int E131_MAX_PROPERTY_VALUES_COUNT
Definition e131.h:17
const size_t E131_MIN_PACKET_SIZE
@ E131_MULTICAST
Definition e131.h:15
ESPHOME_LOG_TAG(TAG, "e131")
ESPHOME_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition util.h:36
const void size_t len
Definition hal.h:68
static void uint32_t
uint8_t values[E131_MAX_PROPERTY_VALUES_COUNT]
Definition e131.h:21
SemaphoreHandle_t lock