ESPHome 2025.5.0
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
7
8#include <lwip/igmp.h>
9#include <lwip/init.h>
10#include <lwip/ip4_addr.h>
11#include <lwip/ip_addr.h>
13namespace esphome {
14namespace e131 {
16static const char *const TAG = "e131";
17
18static const uint8_t ACN_ID[12] = {0x41, 0x53, 0x43, 0x2d, 0x45, 0x31, 0x2e, 0x31, 0x37, 0x00, 0x00, 0x00};
19static const uint32_t VECTOR_ROOT = 4;
20static const uint32_t VECTOR_FRAME = 2;
21static const uint8_t VECTOR_DMP = 2;
23// E1.31 Packet Structure
24union E131RawPacket {
25 struct {
26 // Root Layer
27 uint16_t preamble_size;
28 uint16_t postamble_size;
29 uint8_t acn_id[12];
30 uint16_t root_flength;
31 uint32_t root_vector;
32 uint8_t cid[16];
33
34 // Frame Layer
35 uint16_t frame_flength;
36 uint32_t frame_vector;
37 uint8_t source_name[64];
38 uint8_t priority;
39 uint16_t reserved;
40 uint8_t sequence_number;
41 uint8_t options;
42 uint16_t universe;
43
44 // DMP Layer
45 uint16_t dmp_flength;
46 uint8_t dmp_vector;
47 uint8_t type;
48 uint16_t first_address;
49 uint16_t address_increment;
50 uint16_t property_value_count;
51 uint8_t property_values[E131_MAX_PROPERTY_VALUES_COUNT];
52 } __attribute__((packed));
53
54 uint8_t raw[638];
55};
56
57// We need to have at least one `1` value
58// Get the offset of `property_values[1]`
59const size_t E131_MIN_PACKET_SIZE = reinterpret_cast<size_t>(&((E131RawPacket *) nullptr)->property_values[1]);
60
63 return false;
64 if (this->socket_ == nullptr)
65 return false;
66
67 for (auto universe : universe_consumers_) {
68 if (!universe.second)
69 continue;
70
71 ip4_addr_t multicast_addr =
72 network::IPAddress(239, 255, ((universe.first >> 8) & 0xff), ((universe.first >> 0) & 0xff));
73
74 auto err = igmp_joingroup(IP4_ADDR_ANY4, &multicast_addr);
75
76 if (err) {
77 ESP_LOGW(TAG, "IGMP join for %d universe of E1.31 failed. Multicast might not work.", universe.first);
78 }
79 }
80
81 return true;
82}
83
85 // store only latest received packet for the given universe
86 auto consumers = ++universe_consumers_[universe];
87
88 if (consumers > 1) {
89 return; // we already joined before
90 }
91
92 if (join_igmp_groups_()) {
93 ESP_LOGD(TAG, "Joined %d universe for E1.31.", universe);
94 }
95}
96
98 auto consumers = --universe_consumers_[universe];
99
100 if (consumers > 0) {
101 return; // we have other consumers of the given universe
102 }
103
105 ip4_addr_t multicast_addr = network::IPAddress(239, 255, ((universe >> 8) & 0xff), ((universe >> 0) & 0xff));
106
107 igmp_leavegroup(IP4_ADDR_ANY4, &multicast_addr);
108 }
109
110 ESP_LOGD(TAG, "Left %d universe for E1.31.", universe);
111}
112
113bool E131Component::packet_(const std::vector<uint8_t> &data, int &universe, E131Packet &packet) {
114 if (data.size() < E131_MIN_PACKET_SIZE)
115 return false;
116
117 auto *sbuff = reinterpret_cast<const E131RawPacket *>(&data[0]);
118
119 if (memcmp(sbuff->acn_id, ACN_ID, sizeof(sbuff->acn_id)) != 0)
120 return false;
121 if (htonl(sbuff->root_vector) != VECTOR_ROOT)
122 return false;
123 if (htonl(sbuff->frame_vector) != VECTOR_FRAME)
124 return false;
125 if (sbuff->dmp_vector != VECTOR_DMP)
126 return false;
127 if (sbuff->property_values[0] != 0)
128 return false;
129
130 universe = htons(sbuff->universe);
131 packet.count = htons(sbuff->property_value_count);
133 return false;
134
135 memcpy(packet.values, sbuff->property_values, packet.count);
136 return true;
137}
138
139} // namespace e131
140} // namespace esphome
141#endif
void leave_(int universe)
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