ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
tcp_uart.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
19namespace esphome::tcp_uart {
20
22class TcpUart : public uart::UARTComponent, public Component {
23#ifdef USE_BINARY_SENSOR
24 SUB_BINARY_SENSOR(connected)
25#endif
26#ifdef USE_SENSOR
27 SUB_SENSOR(disconnects)
28#endif
29
30 public:
32
33 void set_host(const char *host) { this->link_.set_host(host); }
34 void set_port(uint16_t port) { this->link_.set_port(port); }
35 void set_reconnect_interval(uint32_t ms) { this->link_.set_reconnect_interval(ms); }
36#ifdef USE_SOCKET_TCP_LISTENER
37 void set_server(bool server) { this->server_ = server; }
38#ifdef USE_SOCKET_IPV4_ALLOW
39 void set_allow(const socket::Ipv4AllowEntry *entries, size_t count) { this->listener_.set_allow(entries, count); }
40#endif
41#endif
42
43 void setup() override;
44 void loop() override;
45 void dump_config() override;
46 void on_shutdown() override;
47 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
48
49 void write_array(const uint8_t *data, size_t len) override;
50 bool peek_byte(uint8_t *data) override;
51 bool read_array(uint8_t *data, size_t len) override;
52 size_t available() override { return static_cast<size_t>(this->rx_end_ - this->rx_start_); }
53 // Same room write_array() grants, so consumers can apply backpressure.
54 size_t available_for_write() override { return this->link_.tx_free(); }
55 uart::UARTFlushResult flush() override;
56 bool is_connected() override { return this->link_.connected(); }
57#if defined(USE_ESP8266) || defined(USE_ESP32)
58 void load_settings(bool dump_config) override {}
59 using UARTComponent::load_settings; // also bring in the no-arg overload for convenience
60#endif
61
62 protected:
63 void check_logger_conflict() override {}
64 void sync_link_();
65 void read_socket_();
66
67 static constexpr size_t RX_BUFFER_SIZE = 1024;
68
70#ifdef USE_SOCKET_TCP_LISTENER
72#endif
73#ifdef USE_SENSOR
75#endif
77 // rx_[rx_start_, rx_end_) holds unread bytes; read_socket_() compacts to the front.
78 uint16_t rx_start_{0};
79 uint16_t rx_end_{0};
80 bool server_{false};
81 // The link state loop() saw last; edges publish the sensor, the up edge clears rx_.
82 bool link_was_up_{false};
83 // A read stopped before EAGAIN. ready() stays false until new data arrives.
84 bool rx_pending_{false};
85 uint8_t rx_[RX_BUFFER_SIZE]{};
86};
87
88} // namespace esphome::tcp_uart
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)
TCP client or server presented as a UART. Bytes are copied unchanged.
Definition tcp_uart.h:22
socket::TcpClientLink link_
Definition tcp_uart.h:69
void check_logger_conflict() override
Definition tcp_uart.h:63
static constexpr size_t RX_BUFFER_SIZE
Definition tcp_uart.h:67
uint8_t rx_[RX_BUFFER_SIZE]
Definition tcp_uart.h:85
socket::TcpListener listener_
Definition tcp_uart.h:71
void load_settings()
Load the UART settings.
unsigned count
Definition gif.cpp:51
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.h:55
UARTFlushResult
Result of a flush() call.
const void size_t len
Definition hal.h:68
static void uint32_t
One allowed IPv4 network, network byte order, host bits cleared.
Definition ipv4_allow.h:14