ESPHome 2026.3.2
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/util.h"
10#include <lwip/igmp.h>
11#include <lwip/init.h>
12#include <lwip/ip4_addr.h>
13#include <lwip/ip_addr.h>
15namespace esphome {
16namespace e131 {
17
18static const char *const 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)
65 return false;
66#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
67 if (this->socket_ == nullptr)
68 return false;
69#endif
70
71 for (auto &entry : this->universe_consumers_) {
72 if (!entry.consumers)
73 continue;
74
75 ip4_addr_t multicast_addr =
76 network::IPAddress(239, 255, ((entry.universe >> 8) & 0xff), ((entry.universe >> 0) & 0xff));
77
78 err_t err;
79 {
80 LwIPLock lock;
81 err = igmp_joingroup(IP4_ADDR_ANY4, &multicast_addr);
82 }
83
84 if (err) {
85 ESP_LOGW(TAG, "IGMP join for %d universe of E1.31 failed. Multicast might not work.", entry.universe);
86 }
87 }
88
89 return true;
90}
91
93 for (auto &entry : this->universe_consumers_) {
94 if (entry.universe == universe)
95 return &entry;
96 }
97 return nullptr;
98}
99
101 // store only latest received packet for the given universe
102 auto *consumer = this->find_universe_(universe);
103 if (consumer != nullptr) {
104 if (consumer->consumers++ > 0) {
105 return; // we already joined before
106 }
107 } else {
108 this->universe_consumers_.push_back({static_cast<uint16_t>(universe), 1});
109 }
110
111 if (this->join_igmp_groups_()) {
112 ESP_LOGD(TAG, "Joined %d universe for E1.31.", universe);
113 }
114}
115
117 auto *consumer = this->find_universe_(universe);
118 if (consumer == nullptr)
119 return;
120
121 if (--consumer->consumers > 0) {
122 return; // we have other consumers of the given universe
123 }
124
125 if (this->listen_method_ == E131_MULTICAST) {
126 ip4_addr_t multicast_addr = network::IPAddress(239, 255, ((universe >> 8) & 0xff), ((universe >> 0) & 0xff));
127
128 LwIPLock lock;
129 igmp_leavegroup(IP4_ADDR_ANY4, &multicast_addr);
130 }
131
132 ESP_LOGD(TAG, "Left %d universe for E1.31.", universe);
133}
134
135bool E131Component::packet_(const uint8_t *data, size_t len, int &universe, E131Packet &packet) {
137 return false;
138
139 auto *sbuff = reinterpret_cast<const E131RawPacket *>(data);
140
141 if (memcmp(sbuff->acn_id, ACN_ID, sizeof(sbuff->acn_id)) != 0)
142 return false;
143 if (htonl(sbuff->root_vector) != VECTOR_ROOT)
144 return false;
145 if (htonl(sbuff->frame_vector) != VECTOR_FRAME)
146 return false;
147 if (sbuff->dmp_vector != VECTOR_DMP)
148 return false;
149 if (sbuff->property_values[0] != 0)
150 return false;
151
152 universe = htons(sbuff->universe);
153 packet.count = htons(sbuff->property_value_count);
155 return false;
156
157 memcpy(packet.values, sbuff->property_values, packet.count);
158 return true;
159}
160
161} // namespace e131
162} // namespace esphome
163#endif
Helper class to lock the lwIP TCPIP core when making lwIP API calls from non-TCPIP threads.
Definition helpers.h:1810
E131ListenMethod listen_method_
Definition e131.h:65
bool packet_(const uint8_t *data, size_t len, int &universe, E131Packet &packet)
std::unique_ptr< socket::Socket > socket_
Definition e131.h:67
UniverseConsumer * find_universe_(int universe)
std::vector< UniverseConsumer > universe_consumers_
Definition e131.h:72
struct @65::@66 __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:22
const size_t E131_MIN_PACKET_SIZE
@ E131_MULTICAST
Definition e131.h:20
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:892
static void uint32_t
uint8_t values[E131_MAX_PROPERTY_VALUES_COUNT]
Definition e131.h:26