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