14static constexpr uint32_t DROP_LOG_INTERVAL_MS = 5000;
15#ifdef USE_TCP_UART_MAX_CONNECTIONS
17static constexpr uint32_t TX_STALL_TIMEOUT_MS = 5000;
20#ifdef USE_TCP_UART_MAX_CONNECTIONS
21void TcpUart::set_max_connections(uint8_t
count) {
22 this->
extra_.init(count - 1);
23 for (uint8_t i = 1; i <
count; i++) {
24 this->
extra_.emplace_back();
29void TcpUart::setup() {
31#ifdef USE_TCP_UART_MAX_CONNECTIONS
32 for (
auto &link : this->
extra_) {
37#ifdef USE_SOCKET_TCP_LISTENER
40#ifdef USE_BINARY_SENSOR
41 PUBLISH_STATE(this->connected_binary_sensor_,
false);
44 PUBLISH_STATE(this->disconnects_sensor_, 0);
48void TcpUart::dump_config() {
52 " Reconnect Interval: %" PRIu32
"ms\n"
53 " Timeout: %" PRIu32
"ms",
54 this->
server_ ? LOG_STR_LITERAL(
"Listen") : LOG_STR_LITERAL(
"Host"),
55 this->
server_ ? LOG_STR_LITERAL(
"*") : this->
link_.
host(), this->link_.port(),
56 this->link_.reconnect_interval(), this->link_.idle_timeout());
57#ifdef USE_TCP_UART_MAX_CONNECTIONS
58 if (!this->extra_.empty()) {
60 " Max Connections: %u\n"
61 " Turn Timeout: %ums",
65#ifdef USE_SOCKET_TCP_LISTENER
68#ifdef USE_BINARY_SENSOR
69 LOG_BINARY_SENSOR(
" ",
"Connected", this->connected_binary_sensor_);
72 LOG_SENSOR(
" ",
"Disconnects", this->disconnects_sensor_);
76void TcpUart::on_shutdown() {
78#ifdef USE_TCP_UART_MAX_CONNECTIONS
79 for (
auto &link : this->extra_) {
83#ifdef USE_SOCKET_TCP_LISTENER
88bool TcpUart::is_connected() {
89#ifdef USE_TCP_UART_MAX_CONNECTIONS
90 for (
auto &link : this->extra_) {
91 if (link.connected()) {
100#ifdef USE_BINARY_SENSOR
101 PUBLISH_STATE(this->connected_binary_sensor_, up);
107 if (this->disconnects_sensor_ !=
nullptr) {
109 this->disconnects_sensor_->publish_state(this->
disconnects_);
152 return static_cast<size_t>(
count);
155void TcpUart::loop() {
156#ifdef USE_TCP_UART_MAX_CONNECTIONS
157 if (!this->extra_.empty()) {
160 if (waiting || this->
up_mask_ != 0) {
166#ifdef USE_SOCKET_TCP_LISTENER
194#ifdef USE_TCP_UART_MAX_CONNECTIONS
198 for (uint8_t i = 0; i <
count; i++) {
199 if (this->
slot_(i).connected() != ((this->
up_mask_ & (1u << i)) != 0)) {
208 const bool rest = this->
rest_ != 0 && !this->
held_ && this->available() == 0;
212 if (this->available() == 0 && !this->
held_ &&
220 for (uint8_t i = 0; i <
count; i++) {
227 if (busy || link.
ready()) {
235 const uint8_t bit = 1u << i;
236 const bool was_up = this->
up_mask_ != 0;
237 if (this->
slot_(i).connected()) {
256 if ((this->
up_mask_ != 0) != was_up) {
263 if (sock ==
nullptr) {
269 bool quietest_waits =
false;
271 for (uint8_t i = 0; i <
count; i++) {
287 if (quietest ==
NO_SLOT || (quietest_waits && !waiting) || (waiting == quietest_waits && idle > quietest_idle)) {
289 quietest_waits = waiting;
290 quietest_idle = idle;
300 ESP_LOGI(TAG,
"All %u connections in use, closing the quietest",
count);
314 for (uint8_t step = 1; step <=
count; step++) {
317 const uint8_t bit = 1u << i;
318 if (!link.
connected() || ((this->pending_mask_ & bit) == 0 && !link.
ready())) {
330 if (this->available() == 0 && !this->
rx_pending_) {
348 const uint16_t room =
static_cast<uint16_t
>(link.
tx_free());
358 for (uint8_t i = 0; i <
count; i++) {
360 if (i == this->
tx_slot_ || !other.
connected() || ((this->pending_mask_ & (1u << i)) == 0 && !other.
ready())) {
364 ESP_LOGW(TAG,
"Connection took no bytes for %" PRIu32
"ms while another waits, closing", now - this->
tx_moved_ms_);
399void TcpUart::write_array(
const uint8_t *data,
size_t len) {
401 size_t queued = link !=
nullptr ? this->
queue_link_(*link, data,
len) : 0;
402#ifdef USE_TCP_UART_MAX_CONNECTIONS
411 ESP_LOGW(TAG,
"%s, dropped %u bytes",
412 link !=
nullptr && link->connected() ? LOG_STR_LITERAL(
"TX buffer full")
413 : this->is_connected() ? LOG_STR_LITERAL(
"No connection holds the turn")
414 : LOG_STR_LITERAL(
"Not connected"),
415 static_cast<unsigned>(
len - queued));
421bool TcpUart::peek_byte(uint8_t *data) {
429bool TcpUart::read_array(uint8_t *data,
size_t len) {
430 if (this->available() <
len) {
440 if (link ==
nullptr) {
444 if (!link->connected()) {
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.
uint32_t last_io() const
When a byte last moved, or note_io() last ran.
void close()
Close without scheduling a reconnect (shutdown).
void adopt(std::unique_ptr< Socket > sock)
Take over an accepted socket (the server side of a bridge).
uint32_t idle_timeout() const
void poll()
Connect state machine; call every loop while acting as a client.
void check_idle()
Close when no byte has moved for idle_timeout(). Inline: the close runs only once that time has passe...
static constexpr size_t TX_BUFFER_SIZE
void note_io()
Restart the idle clock.
void begin(const char *tag)
Call from setup(). tag names this link's log lines.
ssize_t read(uint8_t *buf, size_t len)
Returns bytes moved, 0 when nothing can move now, -1 when the link dropped.
const char * host() const
A link whose outgoing buffer belongs to its owner, so several links can share one.
std::unique_ptr< Socket > accept(TcpClientLinkBase &link)
Accepts a waiting connection that the allow list admits, or returns nullptr.
void begin(const char *tag)
Call from setup(); tag names the log lines.
void dump_config() const
One config line per allowed network.
bool poll_listen(TcpClientLinkBase &link, uint8_t backlog=1)
Opens the listen socket on link's port after its backoff; true when a connection waits.
void poll(TcpClientLinkBase &link, bool may_accept)
Server state machine; call every loop.
size_t queue_link_(Link &link, const uint8_t *data, size_t len)
socket::TcpClientLinkSharedTx Link
bool tx_drained_(uint32_t now)
True once the last holder's reply has left the shared TX buffer.
uint16_t turn_timeout_ms_
void loop_pool_(bool waiting)
void end_turn_(uint32_t now)
uint32_t last_traffic_ms_
size_t read_socket_(Link &link, size_t limit=RX_BUFFER_SIZE)
Reads at most limit bytes and returns how many arrived.
void accept_slot_(uint32_t now)
void publish_connected_(bool up)
Link * tx_link_()
The connection that writes go to, or nullptr.
static constexpr size_t RX_BUFFER_SIZE
uint32_t last_drop_log_ms_
bool flush_link_(Link &link)
static constexpr uint8_t NO_SLOT
uint8_t rx_[RX_BUFFER_SIZE]
void read_holder_(uint32_t now)
uint8_t slot_count_() const
void pick_holder_(uint32_t now)
void slot_edge_(uint8_t i)
FixedVector< Link > extra_
socket::TcpListener listener_
ESPHOME_LOG_TAG(TAG, "tcp_uart")
UARTFlushResult
Result of a flush() call.
@ UART_FLUSH_RESULT_SUCCESS
Confirmed: all bytes left the TX FIFO.
@ UART_FLUSH_RESULT_FAILED
Confirmed: driver or hardware error.
@ UART_FLUSH_RESULT_TIMEOUT
Confirmed: timed out before TX completed.
Application App
Global storage of Application pointer - only one Application can exist.