ESPHome 2026.2.3
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 defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
66 if (this->socket_ == nullptr)
67 return false;
68#endif
69
70 for (auto universe : universe_consumers_) {
71 if (!universe.second)
72 continue;
73
74 ip4_addr_t multicast_addr =
75 network::IPAddress(239, 255, ((universe.first >> 8) & 0xff), ((universe.first >> 0) & 0xff));
76
77 err_t err;
78 {
79 LwIPLock lock;
80 err = igmp_joingroup(IP4_ADDR_ANY4, &multicast_addr);
81 }
82
83 if (err) {
84 ESP_LOGW(TAG, "IGMP join for %d universe of E1.31 failed. Multicast might not work.", universe.first);
85 }
86 }
87
88 return true;
89}
90
92 // store only latest received packet for the given universe
93 auto consumers = ++universe_consumers_[universe];
94
95 if (consumers > 1) {
96 return; // we already joined before
97 }
98
99 if (join_igmp_groups_()) {
100 ESP_LOGD(TAG, "Joined %d universe for E1.31.", universe);
101 }
102}
103
105 auto consumers = --universe_consumers_[universe];
106
107 if (consumers > 0) {
108 return; // we have other consumers of the given universe
109 }
110
112 ip4_addr_t multicast_addr = network::IPAddress(239, 255, ((universe >> 8) & 0xff), ((universe >> 0) & 0xff));
113
114 LwIPLock lock;
115 igmp_leavegroup(IP4_ADDR_ANY4, &multicast_addr);
116 }
117
118 ESP_LOGD(TAG, "Left %d universe for E1.31.", universe);
119}
120
121bool E131Component::packet_(const uint8_t *data, size_t len, int &universe, E131Packet &packet) {
123 return false;
124
125 auto *sbuff = reinterpret_cast<const E131RawPacket *>(data);
126
127 if (memcmp(sbuff->acn_id, ACN_ID, sizeof(sbuff->acn_id)) != 0)
128 return false;
129 if (htonl(sbuff->root_vector) != VECTOR_ROOT)
130 return false;
131 if (htonl(sbuff->frame_vector) != VECTOR_FRAME)
132 return false;
133 if (sbuff->dmp_vector != VECTOR_DMP)
134 return false;
135 if (sbuff->property_values[0] != 0)
136 return false;
137
138 universe = htons(sbuff->universe);
139 packet.count = htons(sbuff->property_value_count);
141 return false;
142
143 memcpy(packet.values, sbuff->property_values, packet.count);
144 return true;
145}
146
147} // namespace e131
148} // namespace esphome
149#endif
Helper class to lock the lwIP TCPIP core when making lwIP API calls from non-TCPIP threads.
Definition helpers.h:1565
E131ListenMethod listen_method_
Definition e131.h:51
bool packet_(const uint8_t *data, size_t len, int &universe, E131Packet &packet)
std::unique_ptr< socket::Socket > socket_
Definition e131.h:53
std::map< int, int > universe_consumers_
Definition e131.h:58
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:23
const size_t E131_MIN_PACKET_SIZE
@ E131_MULTICAST
Definition e131.h:21
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:692
uint8_t values[E131_MAX_PROPERTY_VALUES_COUNT]
Definition e131.h:27