ESPHome  2024.10.2
e131_packet.cpp
Go to the documentation of this file.
1 #include <cstring>
2 #include "e131.h"
3 #ifdef USE_NETWORK
5 #include "esphome/core/log.h"
6 #include "esphome/core/util.h"
7 
8 #include <lwip/igmp.h>
9 #include <lwip/init.h>
10 #include <lwip/ip4_addr.h>
11 #include <lwip/ip_addr.h>
12 
13 namespace esphome {
14 namespace e131 {
15 
16 static const char *const TAG = "e131";
17 
18 static const uint8_t ACN_ID[12] = {0x41, 0x53, 0x43, 0x2d, 0x45, 0x31, 0x2e, 0x31, 0x37, 0x00, 0x00, 0x00};
19 static const uint32_t VECTOR_ROOT = 4;
20 static const uint32_t VECTOR_FRAME = 2;
21 static const uint8_t VECTOR_DMP = 2;
22 
23 // E1.31 Packet Structure
24 union 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;
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]`
59 const size_t E131_MIN_PACKET_SIZE = reinterpret_cast<size_t>(&((E131RawPacket *) nullptr)->property_values[1]);
60 
62  if (listen_method_ != E131_MULTICAST)
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 
104  if (listen_method_ == E131_MULTICAST) {
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 
113 bool E131Component::packet_(const std::vector<uint8_t> &data, int &universe, E131Packet &packet) {
114  if (data.size() < E131_MIN_PACKET_SIZE)
115  return false;
117  auto *sbuff = reinterpret_cast<const E131RawPacket *>(&data[0]);
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;
130  universe = htons(sbuff->universe);
131  packet.count = htons(sbuff->property_value_count);
133  return false;
135  memcpy(packet.values, sbuff->property_values, packet.count);
136  return true;
137 }
139 } // namespace e131
140 } // namespace esphome
141 #endif
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:24
uint16_t universe
void join_(int universe)
Definition: e131_packet.cpp:84
uint8_t source_name[64]
in_addr ip4_addr_t
Definition: ip_address.h:23
uint8_t acn_id[12]
uint16_t address_increment
uint8_t type
const int E131_MAX_PROPERTY_VALUES_COUNT
Definition: e131.h:20
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:97
uint8_t priority
uint16_t dmp_flength
uint32_t frame_vector
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const size_t E131_MIN_PACKET_SIZE
Definition: e131_packet.cpp:59
uint16_t root_flength
uint8_t dmp_vector
uint16_t postamble_size
uint16_t reserved
uint8_t cid[16]