ESPHome 2026.12.0-dev
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#ifdef USE_TCP_UART_MAX_CONNECTIONS
18#endif
19
20#include <cstdint>
21
22namespace esphome::tcp_uart {
23
25class TcpUart : public uart::UARTComponent, public Component {
26#ifdef USE_BINARY_SENSOR
27 SUB_BINARY_SENSOR(connected)
28#endif
29#ifdef USE_SENSOR
30 SUB_SENSOR(disconnects)
31#endif
32
33 public:
35
36 void set_host(const char *host) { this->link_.set_host(host); }
37 void set_port(uint16_t port) { this->link_.set_port(port); }
38 void set_reconnect_interval(uint32_t ms) { this->link_.set_reconnect_interval(ms); }
39 void set_idle_timeout(uint32_t ms) { this->link_.set_idle_timeout(ms); }
40#ifdef USE_SOCKET_TCP_LISTENER
41 void set_server(bool server) { this->server_ = server; }
42#ifdef USE_SOCKET_IPV4_ALLOW
43 void set_allow(const socket::Ipv4AllowEntry *entries, size_t count) { this->listener_.set_allow(entries, count); }
44#endif
45#endif
46#ifdef USE_TCP_UART_MAX_CONNECTIONS
48 void set_max_connections(uint8_t count);
49 void set_turn_timeout(uint16_t ms) { this->turn_timeout_ms_ = ms; }
50#endif
51
54 uint16_t turn() const { return this->turn_; }
57 void hold_turn() {
58 this->held_ = true;
59#ifdef USE_TCP_UART_MAX_CONNECTIONS
60 this->rest_ = 0;
61#endif
62 }
66 void release_turn(uint16_t rest = 0) {
67 this->held_ = false;
68#ifdef USE_TCP_UART_MAX_CONNECTIONS
69 this->release_ = true;
70 this->rest_ = rest;
71#endif
72 }
73
74 void setup() override;
75 void loop() override;
76 void dump_config() override;
77 void on_shutdown() override;
78 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
79
80 void write_array(const uint8_t *data, size_t len) override;
81 bool peek_byte(uint8_t *data) override;
82 bool read_array(uint8_t *data, size_t len) override;
83 size_t available() override { return static_cast<size_t>(this->rx_end_ - this->rx_start_); }
84 // Same room write_array() grants, so consumers can apply backpressure.
85 size_t available_for_write() override {
86 Link *link = this->tx_link_();
87 return link != nullptr ? link->tx_free() : 0;
88 }
89 uart::UARTFlushResult flush() override;
90 bool is_connected() override;
91#if defined(USE_ESP8266) || defined(USE_ESP32) || defined(USE_ZEPHYR)
92 void load_settings(bool dump_config) override {}
93 using UARTComponent::load_settings; // also bring in the no-arg overload for convenience
94#endif
95
96 protected:
97#ifdef USE_TCP_UART_MAX_CONNECTIONS
98 // All connections share tx_; only the one that writes go to holds bytes in it.
100#else
102#endif
103
104 void check_logger_conflict() override {}
105 void sync_link_();
107 size_t read_socket_(Link &link, size_t limit = RX_BUFFER_SIZE);
108 void publish_connected_(bool up);
109 void count_disconnect_();
110#ifdef USE_TCP_UART_MAX_CONNECTIONS
111 size_t queue_link_(Link &link, const uint8_t *data, size_t len) { return link.queue(this->tx_, data, len); }
112 bool flush_link_(Link &link) { return link.flush_tx(this->tx_); }
113#else
114 size_t queue_link_(Link &link, const uint8_t *data, size_t len) { return link.queue(data, len); }
115 bool flush_link_(Link &link) { return link.flush_tx(); }
116#endif
119#ifdef USE_TCP_UART_MAX_CONNECTIONS
120 if (!this->extra_.empty()) {
121 return this->tx_slot_ == NO_SLOT ? nullptr : &this->slot_(this->tx_slot_);
122 }
123#endif
124 return &this->link_;
125 }
126#ifdef USE_TCP_UART_MAX_CONNECTIONS
127 static constexpr uint8_t NO_SLOT = 0xFF;
128
129 Link &slot_(uint8_t i) { return i == 0 ? this->link_ : this->extra_[i - 1]; }
130 uint8_t slot_count_() const { return static_cast<uint8_t>(this->extra_.size() + 1); }
131 void loop_pool_(bool waiting);
132 void slot_edge_(uint8_t i);
133 void accept_slot_(uint32_t now);
134 void pick_holder_(uint32_t now);
137 bool tx_drained_(uint32_t now);
138 void read_holder_(uint32_t now);
139 void end_turn_(uint32_t now);
140#endif
141
142 static constexpr size_t RX_BUFFER_SIZE = 1024;
143
145#ifdef USE_SOCKET_TCP_LISTENER
147#endif
148#ifdef USE_SENSOR
150#endif
152#ifdef USE_TCP_UART_MAX_CONNECTIONS
153 // Slots 1 and up; slot 0 is link_, which also holds the port and the listener's retry clock.
155 // Last byte in either direction on the holder's connection.
157 // When the last holder's reply last left the TX buffer, and the free room seen then.
159 uint16_t tx_free_seen_{0};
160 uint8_t holder_{NO_SLOT};
161 // Writes go here: the holder, else the last holder until it closes.
163 // The search for the next holder starts after this slot.
164 uint8_t last_holder_{0};
165 // Slots loop() saw connected.
166 uint8_t up_mask_{0};
167 // Slots whose last read stopped before EAGAIN; ready() stays false for them.
168 uint8_t pending_mask_{0};
169 bool release_{false};
170 // Bytes the released turn may still read: the rest of a request started in the bytes already read.
171 uint16_t rest_{0};
172 uint16_t turn_timeout_ms_{100};
173#endif
174 // rx_[rx_start_, rx_end_) holds unread bytes; read_socket_() compacts to the front.
175 uint16_t rx_start_{0};
176 uint16_t rx_end_{0};
177 uint16_t turn_{0};
178 bool server_{false};
179 // A consumer holds the turn until its reply.
180 bool held_{false};
181 // The link state loop() saw last; edges publish the sensor, the up edge clears rx_.
182 bool link_was_up_{false};
183 // A read stopped before EAGAIN. ready() stays false until new data arrives.
184 bool rx_pending_{false};
185 uint8_t rx_[RX_BUFFER_SIZE]{};
186#ifdef USE_TCP_UART_MAX_CONNECTIONS
188#endif
189};
190
191} // namespace esphome::tcp_uart
Fixed-capacity vector - sized once through init() or try_init(); push_back never reallocates This avo...
Definition helpers.h:769
The server side of a bridged TCP link: owns the listen socket and the allow list.
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:25
size_t queue_link_(Link &link, const uint8_t *data, size_t len)
Definition tcp_uart.h:111
socket::TcpClientLinkSharedTx Link
Definition tcp_uart.h:99
bool tx_drained_(uint32_t now)
True once the last holder's reply has left the shared TX buffer.
Definition tcp_uart.cpp:341
void loop_pool_(bool waiting)
Definition tcp_uart.cpp:195
void check_logger_conflict() override
Definition tcp_uart.h:104
void end_turn_(uint32_t now)
Definition tcp_uart.cpp:383
size_t read_socket_(Link &link, size_t limit=RX_BUFFER_SIZE)
Reads at most limit bytes and returns how many arrived.
Definition tcp_uart.cpp:132
void accept_slot_(uint32_t now)
Definition tcp_uart.cpp:261
void publish_connected_(bool up)
Definition tcp_uart.cpp:99
Link & slot_(uint8_t i)
Definition tcp_uart.h:129
Link * tx_link_()
The connection that writes go to, or nullptr.
Definition tcp_uart.h:118
static constexpr size_t RX_BUFFER_SIZE
Definition tcp_uart.h:142
bool flush_link_(Link &link)
Definition tcp_uart.h:112
static constexpr uint8_t NO_SLOT
Definition tcp_uart.h:127
uint8_t rx_[RX_BUFFER_SIZE]
Definition tcp_uart.h:185
void read_holder_(uint32_t now)
Definition tcp_uart.cpp:372
uint8_t slot_count_() const
Definition tcp_uart.h:130
void pick_holder_(uint32_t now)
Definition tcp_uart.cpp:309
void slot_edge_(uint8_t i)
Definition tcp_uart.cpp:234
FixedVector< Link > extra_
Definition tcp_uart.h:154
uint8_t tx_[Link::TX_BUFFER_SIZE]
Definition tcp_uart.h:187
socket::TcpListener listener_
Definition tcp_uart.h:146
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:56
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
httpd_handle_t server