ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
tcp_packet_interface.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <span>
5
8#ifdef USE_SOCKET_TCP_LISTENER
10#endif
11#ifdef USE_BINARY_SENSOR
13#endif
14
15namespace esphome::tcp {
16
33 public:
35 explicit TcpPacketInterface(std::span<uint8_t> rx_buffer);
36
37 void setup() override;
38 void loop() override;
39 void dump_config() override;
40 void on_shutdown() override;
42 const packet_interface::PacketMetaData &meta_data) override;
47 size_t get_send_capacity() const override;
48
49 void set_host(const char *host) { this->link_.set_host(host); }
50 void set_port(uint16_t port) { this->link_.set_port(port); }
51 void set_reconnect_interval(uint32_t interval_ms) { this->link_.set_reconnect_interval(interval_ms); }
52 void set_keep_alive(uint32_t keep_alive_ms) { this->keep_alive_ms_ = keep_alive_ms; }
54 void set_receive_timeout(uint32_t timeout_ms) { this->timeout_ms_ = timeout_ms; }
55#ifdef USE_SOCKET_TCP_LISTENER
56 void set_server(bool server) { this->server_ = server; }
57#ifdef USE_SOCKET_IPV4_ALLOW
58 void set_allow(const socket::Ipv4AllowEntry *entries, size_t count) { this->listener_.set_allow(entries, count); }
59#endif
60#endif
61#ifdef USE_BINARY_SENSOR
63#endif
64
65 bool is_connected() const { return this->connected_; }
66
67 protected:
68 void on_connection_(bool connected);
69 void read_();
70 bool queue_frame_(const packet_interface::PacketBuffer *data, size_t size);
71 void flush_();
72
74#ifdef USE_SOCKET_TCP_LISTENER
76#endif
77 const std::span<uint8_t> rx_buffer_;
82 uint16_t rx_length_{0};
83 // Bytes of the current frame received so far, counting the 2 byte length
84 uint16_t rx_have_{0};
85 uint8_t rx_header_[2]{};
86 bool connected_ : 1 {false};
87 bool tx_waiting_ : 1 {false}; // frames are queued that the socket has not taken yet
88#ifdef USE_SOCKET_TCP_LISTENER
89 bool server_ : 1 {false};
90#endif
91};
92
93} // namespace esphome::tcp
A buffer class that efficiently represents a composite buffer with three sections:
A class that will transport data to and from an abstract channel.
A struct to hold metadata about a packet.
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)
A packet interface link over one TCP connection, as a client that connects to a host,...
bool queue_frame_(const packet_interface::PacketBuffer *data, size_t size)
void set_receive_timeout(uint32_t timeout_ms)
The connection is closed when nothing has been received for this long; the peer's keep_alive sets the...
SUB_BINARY_SENSOR(connected) bool is_connected() const
size_t get_send_capacity() const override
Unlimited while the socket takes what is sent.
bool send_to_interface(const packet_interface::PacketBuffer &data, const packet_interface::PacketMetaData &meta_data) override
void set_allow(const socket::Ipv4AllowEntry *entries, size_t count)
const std::span< uint8_t > rx_buffer_
TcpPacketInterface(std::span< uint8_t > rx_buffer)
void set_keep_alive(uint32_t keep_alive_ms)
void set_reconnect_interval(uint32_t interval_ms)
unsigned count
Definition gif.cpp:51
uint16_t size
Definition helpers.cpp:44
static void uint32_t
One allowed IPv4 network, network byte order, host bits cleared.
Definition ipv4_allow.h:14
httpd_handle_t server