ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
uart_tcp.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_SENSOR
6#endif
8#ifdef USE_SOCKET_TCP_LISTENER
10#endif
13
14#include <cstdint>
15#include <memory>
16
17namespace esphome::uart_tcp {
18
20class UartTcp final : public Component, public uart::UARTDevice {
21 public:
22 void set_host(const char *host) { this->link_.set_host(host); }
23 void set_port(uint16_t port) { this->link_.set_port(port); }
26#ifdef USE_SENSOR
28#endif
29#ifdef USE_SOCKET_TCP_LISTENER
30 void set_server(bool server) { this->server_ = server; }
31#ifdef USE_SOCKET_IPV4_ALLOW
32 void set_allow(const socket::Ipv4AllowEntry *entries, size_t count) { this->listener_.set_allow(entries, count); }
33#endif
34#endif
35
36 void setup() override;
37 void loop() override;
38 void dump_config() override;
39 void on_shutdown() override;
40 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
41
42 protected:
43 void sync_link_();
44 void read_socket_();
45 void read_uart_();
46 void discard_uart_();
47
48 static constexpr size_t READ_CHUNK = 128;
49 // Scratch size for dropping stale UART bytes on connect.
50 static constexpr size_t DISCARD_CHUNK = 32;
51
53#ifdef USE_SOCKET_TCP_LISTENER
55#endif
57 // Loop start time of the last socket-to-UART write; sizes the next paced write.
59#ifdef USE_SENSOR
62#endif
63 bool server_{false};
64 // The link state loop() saw last; edges clear the buffer and publish the sensor.
65 bool link_was_up_{false};
66 // A read stopped before EAGAIN. ready() stays false until new data arrives.
67 bool rx_pending_{false};
68};
69
70} // namespace esphome::uart_tcp
Base class for all binary_sensor-type classes.
Base-class for all sensors.
Definition sensor.h:47
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:20
static constexpr size_t DISCARD_CHUNK
Definition uart_tcp.h:50
socket::TcpClientLink link_
Definition uart_tcp.h:52
void set_reconnect_interval(uint32_t ms)
Definition uart_tcp.h:24
void set_disconnects_sensor(sensor::Sensor *sensor)
Definition uart_tcp.h:27
sensor::Sensor * disconnects_sensor_
Definition uart_tcp.h:60
void on_shutdown() override
Definition uart_tcp.cpp:46
static constexpr size_t READ_CHUNK
Definition uart_tcp.h:48
socket::TcpListener listener_
Definition uart_tcp.h:54
void set_host(const char *host)
Definition uart_tcp.h:22
binary_sensor::BinarySensor * connected_sensor_
Definition uart_tcp.h:56
void set_server(bool server)
Definition uart_tcp.h:30
void set_allow(const socket::Ipv4AllowEntry *entries, size_t count)
Definition uart_tcp.h:32
void set_connected_sensor(binary_sensor::BinarySensor *sensor)
Definition uart_tcp.h:25
void dump_config() override
Definition uart_tcp.cpp:29
void set_port(uint16_t port)
Definition uart_tcp.h:23
float get_setup_priority() const override
Definition uart_tcp.h:40
void setup() override
Definition uart_tcp.cpp:14
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