Skip to content

Commit

Permalink
Migrate e131 to use socket instead of WiFiUDP arduino library (#4832)
Browse files Browse the repository at this point in the history
  • Loading branch information
jesserockz committed May 17, 2023
1 parent 18f4e41 commit 9e3ecc8
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 67 deletions.
16 changes: 6 additions & 10 deletions esphome/components/e131/__init__.py
Expand Up @@ -4,6 +4,7 @@
from esphome.components.light.effects import register_addressable_effect
from esphome.const import CONF_ID, CONF_NAME, CONF_METHOD, CONF_CHANNELS

AUTO_LOAD = ["socket"]
DEPENDENCIES = ["network"]

e131_ns = cg.esphome_ns.namespace("e131")
Expand All @@ -23,16 +24,11 @@
CONF_UNIVERSE = "universe"
CONF_E131_ID = "e131_id"

CONFIG_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(E131Component),
cv.Optional(CONF_METHOD, default="MULTICAST"): cv.one_of(
*METHODS, upper=True
),
}
),
cv.only_with_arduino,
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(E131Component),
cv.Optional(CONF_METHOD, default="MULTICAST"): cv.one_of(*METHODS, upper=True),
}
)


Expand Down
75 changes: 43 additions & 32 deletions esphome/components/e131/e131.cpp
@@ -1,18 +1,7 @@
#ifdef USE_ARDUINO

#include "e131.h"
#include "e131_addressable_light_effect.h"
#include "esphome/core/log.h"

#ifdef USE_ESP32
#include <WiFi.h>
#endif

#ifdef USE_ESP8266
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#endif

namespace esphome {
namespace e131 {

Expand All @@ -22,17 +11,41 @@ static const int PORT = 5568;
E131Component::E131Component() {}

E131Component::~E131Component() {
if (udp_) {
udp_->stop();
if (this->socket_) {
this->socket_->close();
}
}

void E131Component::setup() {
udp_ = make_unique<WiFiUDP>();
this->socket_ = socket::socket_ip(SOCK_DGRAM, IPPROTO_IP);

if (!udp_->begin(PORT)) {
ESP_LOGE(TAG, "Cannot bind E131 to %d.", PORT);
mark_failed();
int enable = 1;
int err = this->socket_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
if (err != 0) {
ESP_LOGW(TAG, "Socket unable to set reuseaddr: errno %d", err);
// we can still continue
}
err = this->socket_->setblocking(false);
if (err != 0) {
ESP_LOGW(TAG, "Socket unable to set nonblocking mode: errno %d", err);
this->mark_failed();
return;
}

struct sockaddr_storage server;

socklen_t sl = socket::set_sockaddr_any((struct sockaddr *) &server, sizeof(server), PORT);
if (sl == 0) {
ESP_LOGW(TAG, "Socket unable to set sockaddr: errno %d", errno);
this->mark_failed();
return;
}
server.ss_family = AF_INET;

err = this->socket_->bind((struct sockaddr *) &server, sizeof(server));
if (err != 0) {
ESP_LOGW(TAG, "Socket unable to bind: errno %d", errno);
this->mark_failed();
return;
}

Expand All @@ -43,22 +56,22 @@ void E131Component::loop() {
std::vector<uint8_t> payload;
E131Packet packet;
int universe = 0;
uint8_t buf[1460];

while (uint16_t packet_size = udp_->parsePacket()) {
payload.resize(packet_size);

if (!udp_->read(&payload[0], payload.size())) {
continue;
}
ssize_t len = this->socket_->read(buf, sizeof(buf));
if (len == -1) {
return;
}
payload.resize(len);
memmove(&payload[0], buf, len);

if (!packet_(payload, universe, packet)) {
ESP_LOGV(TAG, "Invalid packet received of size %zu.", payload.size());
continue;
}
if (!this->packet_(payload, universe, packet)) {
ESP_LOGV(TAG, "Invalid packet received of size %zu.", payload.size());
return;
}

if (!process_(universe, packet)) {
ESP_LOGV(TAG, "Ignored packet for %d universe of size %d.", universe, packet.count);
}
if (!this->process_(universe, packet)) {
ESP_LOGV(TAG, "Ignored packet for %d universe of size %d.", universe, packet.count);
}
}

Expand Down Expand Up @@ -106,5 +119,3 @@ bool E131Component::process_(int universe, const E131Packet &packet) {

} // namespace e131
} // namespace esphome

#endif // USE_ARDUINO
9 changes: 2 additions & 7 deletions esphome/components/e131/e131.h
@@ -1,16 +1,13 @@
#pragma once

#ifdef USE_ARDUINO

#include "esphome/components/socket/socket.h"
#include "esphome/core/component.h"

#include <map>
#include <memory>
#include <set>
#include <vector>

class UDP;

namespace esphome {
namespace e131 {

Expand Down Expand Up @@ -47,13 +44,11 @@ class E131Component : public esphome::Component {
void leave_(int universe);

E131ListenMethod listen_method_{E131_MULTICAST};
std::unique_ptr<UDP> udp_;
std::unique_ptr<socket::Socket> socket_;
std::set<E131AddressableLightEffect *> light_effects_;
std::map<int, int> universe_consumers_;
std::map<int, E131Packet> universe_packets_;
};

} // namespace e131
} // namespace esphome

#endif // USE_ARDUINO
6 changes: 1 addition & 5 deletions esphome/components/e131/e131_addressable_light_effect.cpp
@@ -1,7 +1,5 @@
#ifdef USE_ARDUINO

#include "e131.h"
#include "e131_addressable_light_effect.h"
#include "e131.h"
#include "esphome/core/log.h"

namespace esphome {
Expand Down Expand Up @@ -92,5 +90,3 @@ bool E131AddressableLightEffect::process_(int universe, const E131Packet &packet

} // namespace e131
} // namespace esphome

#endif // USE_ARDUINO
4 changes: 0 additions & 4 deletions esphome/components/e131/e131_addressable_light_effect.h
@@ -1,7 +1,5 @@
#pragma once

#ifdef USE_ARDUINO

#include "esphome/core/component.h"
#include "esphome/components/light/addressable_light_effect.h"

Expand Down Expand Up @@ -44,5 +42,3 @@ class E131AddressableLightEffect : public light::AddressableLightEffect {

} // namespace e131
} // namespace esphome

#endif // USE_ARDUINO
14 changes: 5 additions & 9 deletions esphome/components/e131/e131_packet.cpp
@@ -1,15 +1,13 @@
#ifdef USE_ARDUINO

#include <cstring>
#include "e131.h"
#include "esphome/components/network/ip_address.h"
#include "esphome/core/log.h"
#include "esphome/core/util.h"
#include "esphome/components/network/ip_address.h"
#include <cstring>

#include <lwip/igmp.h>
#include <lwip/init.h>
#include <lwip/ip_addr.h>
#include <lwip/ip4_addr.h>
#include <lwip/igmp.h>
#include <lwip/ip_addr.h>

namespace esphome {
namespace e131 {
Expand Down Expand Up @@ -62,7 +60,7 @@ const size_t E131_MIN_PACKET_SIZE = reinterpret_cast<size_t>(&((E131RawPacket *)
bool E131Component::join_igmp_groups_() {
if (listen_method_ != E131_MULTICAST)
return false;
if (!udp_)
if (this->socket_ == nullptr)
return false;

for (auto universe : universe_consumers_) {
Expand Down Expand Up @@ -140,5 +138,3 @@ bool E131Component::packet_(const std::vector<uint8_t> &data, int &universe, E13

} // namespace e131
} // namespace esphome

#endif // USE_ARDUINO

0 comments on commit 9e3ecc8

Please sign in to comment.