14static constexpr size_t LENGTH_SIZE = 2;
16static constexpr uint8_t MAX_FRAMES_PER_LOOP = 8;
19 : packet_interface::PacketInterface(rx_buffer.
size()), rx_buffer_(rx_buffer) {
26#ifdef USE_SOCKET_TCP_LISTENER
29#ifdef USE_BINARY_SENSOR
30 if (this->connected_binary_sensor_ !=
nullptr)
31 this->connected_binary_sensor_->publish_initial_state(
false);
37#ifdef USE_SOCKET_TCP_LISTENER
41 "TCP Packet Interface '%s':\n"
44 " Reconnect Interval: %" PRIu32
"ms\n"
45 " Keep Alive: %" PRIu32
"ms\n"
46 " Timeout: %" PRIu32
"ms\n"
47 " Max Packet Size: %zu",
48 this->
name_, server ? LOG_STR_LITERAL(
"server") : LOG_STR_LITERAL(
"client"),
50 this->link_.reconnect_interval(), this->keep_alive_ms_, this->timeout_ms_, this->max_packet_size_);
51#ifdef USE_SOCKET_TCP_LISTENER
54#ifdef USE_BINARY_SENSOR
55 LOG_BINARY_SENSOR(
" ",
"Connected", this->connected_binary_sensor_);
61#ifdef USE_SOCKET_TCP_LISTENER
67#ifdef USE_SOCKET_TCP_LISTENER
86 ESP_LOGW(TAG,
"%s: Nothing received for %" PRIu32
"ms, closing the connection", this->
name_, this->
timeout_ms_);
106 ESP_LOGD(TAG,
"%s: Connection %s", this->
name_, connected ? LOG_STR_LITERAL(
"up") : LOG_STR_LITERAL(
"down"));
111#ifdef USE_BINARY_SENSOR
112 PUBLISH_STATE(this->connected_binary_sensor_, connected);
118 for (uint8_t frames = 0; frames != MAX_FRAMES_PER_LOOP;) {
124 this->rx_length_ + LENGTH_SIZE - this->rx_have_);
133 if (this->
rx_have_ == LENGTH_SIZE) {
137 ESP_LOGW(TAG,
"%s: Packet of %u bytes is larger than %zu, closing the connection", this->
name_,
161 data->copy_to(tail + LENGTH_SIZE,
size);
168 size_t size = data.size();
170 ESP_LOGW(TAG,
"%s: Packet of %zu bytes is larger than %zu", this->
name_, size, this->
max_packet_size_);
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.
A buffer class that efficiently represents a composite buffer with three sections:
LazyCallbackManager< void(bool)> session_callback_
const size_t max_packet_size_
void on_receive_from_interface_(const PacketBuffer &data, const PacketMetaData &meta_data)
This function should be called by implementing classes with received data to be passed to any listene...
void close()
Close without scheduling a reconnect (shutdown).
void note_attempt()
Shared retry clock, also usable for a listen socket.
void tx_commit(size_t len)
void poll()
Connect state machine; call every loop while acting as a client.
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
bool flush_tx()
Send the front of the buffer; true once it is empty.
uint8_t * tx_tail()
Direct access to the buffer's free tail.
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.
socket::TcpListener listener_
bool queue_frame_(const packet_interface::PacketBuffer *data, size_t size)
void on_connection_(bool connected)
void on_shutdown() override
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
const std::span< uint8_t > rx_buffer_
void dump_config() override
TcpPacketInterface(std::span< uint8_t > rx_buffer)
socket::TcpClientLink link_
ESPHOME_LOG_TAG(TAG, "tcp.packet_interface")
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Application App
Global storage of Application pointer - only one Application can exist.
constexpr uint8_t * put_le16(uint8_t *dst, uint16_t value)