ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
uart_tcp.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_BINARY_SENSOR
6#endif
7#ifdef USE_SENSOR
9#endif
11#ifdef USE_SOCKET_TCP_LISTENER
13#endif
16
17#include <cstdint>
18#include <memory>
19
20namespace esphome::uart_tcp {
21
23class UartTcp final : public Component, public uart::UARTDevice {
24#ifdef USE_BINARY_SENSOR
25 SUB_BINARY_SENSOR(connected)
26#endif
27#ifdef USE_SENSOR
28 SUB_SENSOR(disconnects)
29#endif
30
31 public:
32 void set_host(const char *host) { this->link_.set_host(host); }
33 void set_port(uint16_t port) { this->link_.set_port(port); }
34 void set_reconnect_interval(uint32_t ms) { this->link_.set_reconnect_interval(ms); }
35#ifdef USE_SOCKET_TCP_LISTENER
36 void set_server(bool server) { this->server_ = server; }
37#ifdef USE_SOCKET_IPV4_ALLOW
38 void set_allow(const socket::Ipv4AllowEntry *entries, size_t count) { this->listener_.set_allow(entries, count); }
39#endif
40#endif
41
42 void setup() override;
43 void loop() override;
44 void dump_config() override;
45 void on_shutdown() override;
46 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
47
48 protected:
49 void sync_link_();
50 void read_socket_();
51 void read_uart_();
52 void discard_uart_();
53
54 static constexpr size_t READ_CHUNK = 128;
55 // Scratch size for dropping stale UART bytes on connect.
56 static constexpr size_t DISCARD_CHUNK = 32;
57
59#ifdef USE_SOCKET_TCP_LISTENER
61#endif
62 // Loop start time of the last socket-to-UART write; sizes the next paced write.
64#ifdef USE_SENSOR
66#endif
67 bool server_{false};
68 // The link state loop() saw last; edges clear the buffer and publish the sensor.
69 bool link_was_up_{false};
70 // A read stopped before EAGAIN. ready() stays false until new data arrives.
71 bool rx_pending_{false};
72};
73
74} // namespace esphome::uart_tcp
The server side of a bridged TCP link: owns the listen socket and the allow list, accepts one peer at...
void set_allow(const Ipv4AllowEntry *entries, size_t count)
Copies raw bytes between one hardware UART and one TCP socket.
Definition uart_tcp.h:23
static constexpr size_t DISCARD_CHUNK
Definition uart_tcp.h:56
socket::TcpClientLink link_
Definition uart_tcp.h:58
static constexpr size_t READ_CHUNK
Definition uart_tcp.h:54
socket::TcpListener listener_
Definition uart_tcp.h:60
unsigned count
Definition gif.cpp:51
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.h:55
static void uint32_t
One allowed IPv4 network, network byte order, host bits cleared.
Definition ipv4_allow.h:14