ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
uart_tcp.cpp
Go to the documentation of this file.
1#include "uart_tcp.h"
2
4#include "esphome/core/log.h"
5
6#include <algorithm>
7#include <cerrno>
8#include <cinttypes>
9
11
12ESPHOME_LOG_TAG(TAG, "uart_tcp");
13
14void UartTcp::setup() {
15 this->link_.begin(TAG);
16#ifdef USE_SOCKET_TCP_LISTENER
17 this->listener_.begin(TAG);
18#endif
19#ifdef USE_BINARY_SENSOR
20 PUBLISH_STATE(this->connected_binary_sensor_, false);
21#endif
22#ifdef USE_SENSOR
23 PUBLISH_STATE(this->disconnects_sensor_, 0);
24#endif
25}
26
27void UartTcp::dump_config() {
28 ESP_LOGCONFIG(TAG,
29 "UART TCP:\n"
30 " %s: %s:%u\n"
31 " Reconnect Interval: %" PRIu32 "ms\n"
32 " Timeout: %" PRIu32 "ms",
33 this->server_ ? LOG_STR_LITERAL("Listen") : LOG_STR_LITERAL("Host"),
34 this->server_ ? LOG_STR_LITERAL("*") : this->link_.host(), this->link_.port(),
35 this->link_.reconnect_interval(), this->link_.idle_timeout());
36#ifdef USE_SOCKET_TCP_LISTENER
37 this->listener_.dump_config();
38#endif
39#ifdef USE_BINARY_SENSOR
40 LOG_BINARY_SENSOR(" ", "Connected", this->connected_binary_sensor_);
41#endif
42#ifdef USE_SENSOR
43 LOG_SENSOR(" ", "Disconnects", this->disconnects_sensor_);
44#endif
45}
46
47void UartTcp::on_shutdown() {
48 this->link_.close();
49#ifdef USE_SOCKET_TCP_LISTENER
50 this->listener_.close();
51#endif
52}
53
55 bool up = this->link_.connected();
56 this->link_was_up_ = up;
57 if (up) {
58 // The driver kept whatever arrived while the link was down.
59 this->discard_uart_();
60 }
61#ifdef USE_BINARY_SENSOR
62 PUBLISH_STATE(this->connected_binary_sensor_, up);
63#endif
64#ifdef USE_SENSOR
65 // Only edges get here, so down is the falling edge.
66 if (!up && this->disconnects_sensor_ != nullptr) {
67 this->disconnects_++;
68 this->disconnects_sensor_->publish_state(this->disconnects_);
69 }
70#endif
71}
72
74 // A hardware write blocks until the driver takes every byte. Leave what does
75 // not fit in the socket, so TCP flow control throttles the peer.
76 size_t room = this->parent_->paced_write_room(this->last_write_ms_);
77 if (room == 0) {
78 // The UART cannot take more. The peer is not idle.
79 this->rx_pending_ = true;
80 this->link_.note_io();
81 return;
82 }
83 uint8_t tmp[READ_CHUNK];
84 size_t want = std::min(room, sizeof(tmp));
85 ssize_t count = this->link_.read(tmp, want);
86 if (count <= 0) {
87 // A dropped link (-1) is cleaned up by sync_link_() on the next loop.
88 if (count == 0) {
89 this->rx_pending_ = false;
90 }
91 return;
92 }
93 this->rx_pending_ = static_cast<size_t>(count) == want;
94 this->write_array(tmp, static_cast<size_t>(count));
96}
97
99 // Drain exactly what was buffered while the link was down; later bytes are live.
100 uint8_t dump[DISCARD_CHUNK];
101 size_t left = this->available();
102 while (left != 0) {
103 size_t n = std::min(left, sizeof(dump));
104 if (!this->read_array(dump, n)) {
105 return;
106 }
107 left -= n;
108 }
109}
110
112 size_t want = std::min<size_t>(this->available(), this->link_.tx_free());
113 if (want != 0 && this->read_array(this->link_.tx_tail(), want)) {
114 this->link_.tx_commit(want);
115 }
116}
117
118void UartTcp::loop() {
119#ifdef USE_SOCKET_TCP_LISTENER
120 if (this->server_) {
121 // link_was_up_ holds the accept until the previous drop's edge has run,
122 // so the sensor and the stale UART discard always see the disconnect.
123 this->listener_.poll(this->link_, !this->link_was_up_);
124 } else {
125 this->link_.poll();
126 }
127#else
128 this->link_.poll();
129#endif
130 if (this->link_.connected() != this->link_was_up_) {
131 this->sync_link_();
132 }
133 if (!this->link_was_up_) {
134 return;
135 }
136 if (this->rx_pending_ || this->link_.ready()) {
137 this->read_socket_();
138 }
139 // UART bytes picked up here go out in the same pass.
140 this->read_uart_();
141 this->link_.flush_tx();
142 this->link_.check_idle();
143}
144
145} // namespace esphome::uart_tcp
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void begin(const char *tag)
Call from setup(); tag names the log lines.
void dump_config() const
One config line per allowed network.
void poll(TcpClientLinkBase &link, bool may_accept)
Server state machine; call every loop.
size_t paced_write_room(uint32_t last_write_ms)
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:43
UARTComponent * parent_
Definition uart.h:147
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
static constexpr size_t DISCARD_CHUNK
Definition uart_tcp.h:57
socket::TcpClientLink link_
Definition uart_tcp.h:59
static constexpr size_t READ_CHUNK
Definition uart_tcp.h:55
socket::TcpListener listener_
Definition uart_tcp.h:61
unsigned count
Definition gif.cpp:51
__int64 ssize_t
Definition httplib.h:178
ESPHOME_LOG_TAG(TAG, "uart_tcp")
Application App
Global storage of Application pointer - only one Application can exist.