mirror of
https://github.com/esphome/esphome.git
synced 2026-05-27 11:56:11 +08:00
[e131] Replace std::map with std::vector for universe tracking (#14087)
This commit is contained in:
@@ -9,7 +9,6 @@
|
|||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
|
|
||||||
#include <cinttypes>
|
#include <cinttypes>
|
||||||
#include <map>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -27,6 +26,11 @@ struct E131Packet {
|
|||||||
uint8_t values[E131_MAX_PROPERTY_VALUES_COUNT];
|
uint8_t values[E131_MAX_PROPERTY_VALUES_COUNT];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct UniverseConsumer {
|
||||||
|
uint16_t universe;
|
||||||
|
uint16_t consumers;
|
||||||
|
};
|
||||||
|
|
||||||
class E131Component : public esphome::Component {
|
class E131Component : public esphome::Component {
|
||||||
public:
|
public:
|
||||||
E131Component();
|
E131Component();
|
||||||
@@ -45,6 +49,7 @@ class E131Component : public esphome::Component {
|
|||||||
bool packet_(const uint8_t *data, size_t len, int &universe, E131Packet &packet);
|
bool packet_(const uint8_t *data, size_t len, int &universe, E131Packet &packet);
|
||||||
bool process_(int universe, const E131Packet &packet);
|
bool process_(int universe, const E131Packet &packet);
|
||||||
bool join_igmp_groups_();
|
bool join_igmp_groups_();
|
||||||
|
UniverseConsumer *find_universe_(int universe);
|
||||||
void join_(int universe);
|
void join_(int universe);
|
||||||
void leave_(int universe);
|
void leave_(int universe);
|
||||||
|
|
||||||
@@ -55,7 +60,7 @@ class E131Component : public esphome::Component {
|
|||||||
WiFiUDP udp_;
|
WiFiUDP udp_;
|
||||||
#endif
|
#endif
|
||||||
std::vector<E131AddressableLightEffect *> light_effects_;
|
std::vector<E131AddressableLightEffect *> light_effects_;
|
||||||
std::map<int, int> universe_consumers_;
|
std::vector<UniverseConsumer> universe_consumers_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace e131
|
} // namespace e131
|
||||||
|
|||||||
@@ -60,19 +60,19 @@ union E131RawPacket {
|
|||||||
const size_t E131_MIN_PACKET_SIZE = reinterpret_cast<size_t>(&((E131RawPacket *) nullptr)->property_values[1]);
|
const size_t E131_MIN_PACKET_SIZE = reinterpret_cast<size_t>(&((E131RawPacket *) nullptr)->property_values[1]);
|
||||||
|
|
||||||
bool E131Component::join_igmp_groups_() {
|
bool E131Component::join_igmp_groups_() {
|
||||||
if (listen_method_ != E131_MULTICAST)
|
if (this->listen_method_ != E131_MULTICAST)
|
||||||
return false;
|
return false;
|
||||||
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
|
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
|
||||||
if (this->socket_ == nullptr)
|
if (this->socket_ == nullptr)
|
||||||
return false;
|
return false;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for (auto universe : universe_consumers_) {
|
for (auto &entry : this->universe_consumers_) {
|
||||||
if (!universe.second)
|
if (!entry.consumers)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
ip4_addr_t multicast_addr =
|
ip4_addr_t multicast_addr =
|
||||||
network::IPAddress(239, 255, ((universe.first >> 8) & 0xff), ((universe.first >> 0) & 0xff));
|
network::IPAddress(239, 255, ((entry.universe >> 8) & 0xff), ((entry.universe >> 0) & 0xff));
|
||||||
|
|
||||||
err_t err;
|
err_t err;
|
||||||
{
|
{
|
||||||
@@ -81,34 +81,47 @@ bool E131Component::join_igmp_groups_() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
ESP_LOGW(TAG, "IGMP join for %d universe of E1.31 failed. Multicast might not work.", universe.first);
|
ESP_LOGW(TAG, "IGMP join for %d universe of E1.31 failed. Multicast might not work.", entry.universe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UniverseConsumer *E131Component::find_universe_(int universe) {
|
||||||
|
for (auto &entry : this->universe_consumers_) {
|
||||||
|
if (entry.universe == universe)
|
||||||
|
return &entry;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
void E131Component::join_(int universe) {
|
void E131Component::join_(int universe) {
|
||||||
// store only latest received packet for the given universe
|
// store only latest received packet for the given universe
|
||||||
auto consumers = ++universe_consumers_[universe];
|
auto *consumer = this->find_universe_(universe);
|
||||||
|
if (consumer != nullptr) {
|
||||||
if (consumers > 1) {
|
if (consumer->consumers++ > 0) {
|
||||||
return; // we already joined before
|
return; // we already joined before
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this->universe_consumers_.push_back({static_cast<uint16_t>(universe), 1});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (join_igmp_groups_()) {
|
if (this->join_igmp_groups_()) {
|
||||||
ESP_LOGD(TAG, "Joined %d universe for E1.31.", universe);
|
ESP_LOGD(TAG, "Joined %d universe for E1.31.", universe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void E131Component::leave_(int universe) {
|
void E131Component::leave_(int universe) {
|
||||||
auto consumers = --universe_consumers_[universe];
|
auto *consumer = this->find_universe_(universe);
|
||||||
|
if (consumer == nullptr)
|
||||||
|
return;
|
||||||
|
|
||||||
if (consumers > 0) {
|
if (--consumer->consumers > 0) {
|
||||||
return; // we have other consumers of the given universe
|
return; // we have other consumers of the given universe
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listen_method_ == E131_MULTICAST) {
|
if (this->listen_method_ == E131_MULTICAST) {
|
||||||
ip4_addr_t multicast_addr = network::IPAddress(239, 255, ((universe >> 8) & 0xff), ((universe >> 0) & 0xff));
|
ip4_addr_t multicast_addr = network::IPAddress(239, 255, ((universe >> 8) & 0xff), ((universe >> 0) & 0xff));
|
||||||
|
|
||||||
LwIPLock lock;
|
LwIPLock lock;
|
||||||
|
|||||||
Reference in New Issue
Block a user