ESPHome
2026.11.0b6
Loading...
Searching...
No Matches
esphome
components
tcp_uart
tcp_uart.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
esphome/core/defines.h
"
4
#ifdef USE_BINARY_SENSOR
5
#include "
esphome/components/binary_sensor/binary_sensor.h
"
6
#endif
7
#ifdef USE_SENSOR
8
#include "
esphome/components/sensor/sensor.h
"
9
#endif
10
#include "
esphome/components/socket/tcp_client_link.h
"
11
#ifdef USE_SOCKET_TCP_LISTENER
12
#include "
esphome/components/socket/tcp_listener.h
"
13
#endif
14
#include "
esphome/components/uart/uart_component.h
"
15
#include "
esphome/core/component.h
"
16
17
#include <cstdint>
18
19
namespace
esphome::tcp_uart
{
20
22
class
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
:
31
TcpUart
() { this->
rx_buffer_size_
=
RX_BUFFER_SIZE
; }
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
69
socket::TcpClientLink
link_
;
70
#ifdef USE_SOCKET_TCP_LISTENER
71
socket::TcpListener
listener_
;
72
#endif
73
#ifdef USE_SENSOR
74
uint32_t
disconnects_
{0};
75
#endif
76
uint32_t
last_drop_log_ms_
{0};
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
binary_sensor.h
esphome::Component
Definition
component.h:147
esphome::socket::TcpClientLink
A reconnecting TCP stream driven from loop().
Definition
tcp_client_link.h:22
esphome::socket::TcpClientLink::connected
bool connected() const
Definition
tcp_client_link.h:77
esphome::socket::TcpClientLink::set_reconnect_interval
void set_reconnect_interval(uint32_t ms)
Definition
tcp_client_link.h:26
esphome::socket::TcpClientLink::set_port
void set_port(uint16_t port)
Definition
tcp_client_link.h:25
esphome::socket::TcpClientLink::tx_free
size_t tx_free() const
Definition
tcp_client_link.h:55
esphome::socket::TcpClientLink::set_host
void set_host(const char *host)
Definition
tcp_client_link.h:24
esphome::socket::TcpListener
The server side of a bridged TCP link: owns the listen socket and the allow list, accepts one peer at...
Definition
tcp_listener.h:21
esphome::socket::TcpListener::set_allow
void set_allow(const Ipv4AllowEntry *entries, size_t count)
Definition
tcp_listener.h:24
esphome::tcp_uart::TcpUart
TCP client or server presented as a UART. Bytes are copied unchanged.
Definition
tcp_uart.h:22
esphome::tcp_uart::TcpUart::link_
socket::TcpClientLink link_
Definition
tcp_uart.h:69
esphome::tcp_uart::TcpUart::check_logger_conflict
void check_logger_conflict() override
Definition
tcp_uart.h:63
esphome::tcp_uart::TcpUart::rx_start_
uint16_t rx_start_
Definition
tcp_uart.h:78
esphome::tcp_uart::TcpUart::server_
bool server_
Definition
tcp_uart.h:80
esphome::tcp_uart::TcpUart::RX_BUFFER_SIZE
static constexpr size_t RX_BUFFER_SIZE
Definition
tcp_uart.h:67
esphome::tcp_uart::TcpUart::last_drop_log_ms_
uint32_t last_drop_log_ms_
Definition
tcp_uart.h:76
esphome::tcp_uart::TcpUart::rx_pending_
bool rx_pending_
Definition
tcp_uart.h:84
esphome::tcp_uart::TcpUart::read_socket_
void read_socket_()
Definition
tcp_uart.cpp:73
esphome::tcp_uart::TcpUart::rx_
uint8_t rx_[RX_BUFFER_SIZE]
Definition
tcp_uart.h:85
esphome::tcp_uart::TcpUart::link_was_up_
bool link_was_up_
Definition
tcp_uart.h:82
esphome::tcp_uart::TcpUart::rx_end_
uint16_t rx_end_
Definition
tcp_uart.h:79
esphome::tcp_uart::TcpUart::listener_
socket::TcpListener listener_
Definition
tcp_uart.h:71
esphome::tcp_uart::TcpUart::sync_link_
void sync_link_()
Definition
tcp_uart.cpp:54
esphome::tcp_uart::TcpUart::disconnects_
uint32_t disconnects_
Definition
tcp_uart.h:74
esphome::uart::UARTComponent
Definition
uart_component.h:52
esphome::uart::UARTComponent::load_settings
void load_settings()
Load the UART settings.
Definition
uart_component.h:229
esphome::uart::UARTComponent::rx_buffer_size_
size_t rx_buffer_size_
Definition
uart_component.h:243
component.h
defines.h
count
unsigned count
Definition
gif.cpp:51
esphome::setup_priority::AFTER_WIFI
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition
component.h:55
esphome::tcp_uart
Definition
tcp_uart.cpp:9
esphome::uart::UARTFlushResult
UARTFlushResult
Result of a flush() call.
Definition
uart_component.h:45
esphome::len
const void size_t len
Definition
hal.h:68
uint32_t
static void uint32_t
Definition
crash_handler.cpp:153
sensor.h
esphome::socket::Ipv4AllowEntry
One allowed IPv4 network, network byte order, host bits cleared.
Definition
ipv4_allow.h:14
tcp_client_link.h
tcp_listener.h
uart_component.h
Generated by
1.12.0