2#ifdef USE_PACKET_INTERFACE_BASIC_SESSION
11static constexpr uint8_t PACKET_TYPE_DATA = 0x00;
12static constexpr uint8_t PACKET_TYPE_KEEP_ALIVE = 0x01;
14static constexpr size_t KEEP_ALIVE_SIZE = 5;
30 this->
mark_failed(LOG_STR(
"max_packet_size too small for the configured layers"));
34#ifdef USE_BINARY_SENSOR
47 "Basic Session on '%s':\n"
48 " Max Packet Size: %zu\n"
49 " Keep Alive: %" PRIu32
"ms\n"
50 " Timeout: %" PRIu32
"ms",
52#ifdef USE_BINARY_SENSOR
60 uint8_t header = PACKET_TYPE_DATA;
66 uint8_t packet[KEEP_ALIVE_SIZE] = {
67 PACKET_TYPE_KEEP_ALIVE,
static_cast<uint8_t
>(this->
boot_id_),
static_cast<uint8_t
>(this->
boot_id_ >> 8),
68 static_cast<uint8_t
>(this->
boot_id_ >> 16),
static_cast<uint8_t
>(this->
boot_id_ >> 24)};
70 ESP_LOGV(TAG,
"%s: Lower layer did not accept keep-alive", this->
name_);
76 ESP_LOGW(TAG,
"%s: Peer not heard for %" PRIu32
"ms", this->
name_, this->
timeout_ms_);
83 ESP_LOGD(TAG,
"%s: Session %s", this->
name_, connected ? LOG_STR_LITERAL(
"connected") : LOG_STR_LITERAL(
"lost"));
84#ifdef USE_BINARY_SENSOR
93 if (!data.is_contiguous()) {
94 ESP_LOGW(TAG,
"%s: Packet split across buffers is not supported", this->
name_);
97 auto packet = data.as_span();
102 uint8_t
type = packet[0];
103 if (
type == PACKET_TYPE_KEEP_ALIVE) {
104 if (packet.size() != KEEP_ALIVE_SIZE) {
105 ESP_LOGV(TAG,
"%s: Keep-alive of %zu bytes dropped", this->
name_, packet.size());
113 ESP_LOGD(TAG,
"%s: Peer has restarted", this->
name_);
121 }
else if (type != PACKET_TYPE_DATA) {
122 ESP_LOGV(TAG,
"%s: Unknown packet type 0x%02X dropped", this->
name_, type);
130 if (type == PACKET_TYPE_DATA)
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 mark_failed()
Mark this component as failed.
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
void publish_state(bool new_state)
Publish a new state to the front-end.
void publish_initial_state(bool new_state)
Publish the initial state, this will not make the callback manager send callbacks and is meant only f...
binary_sensor::BinarySensor * connected_sensor_
BasicSession(PacketInterface *lower)
bool send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data) override
void set_connected_(bool connected)
void dump_config() override
uint32_t last_receive_ms_
static constexpr size_t HEADER_SIZE
The packet type byte.
void receive_(const PacketBuffer &data, const PacketMetaData &meta_data)
A buffer class that efficiently represents a composite buffer with three sections:
A class that will transport data to and from an abstract channel.
bool has_capability(Capability capability) const
void add_on_session_state_callback(F &&callback)
With CAPABILITY_SESSION, the callback is called with true when the peer can be reached,...
size_t get_max_packet_size() const
The largest packet this layer accepts; fixed when the layer is constructed.
void note_received_(bool understood)
Called by a layer for each received packet it understood, or could not make sense of.
void stack_on_(PacketInterface *lower, Capability capability)
Called by a layer's constructor to record itself as the layer above lower, adding its own capability.
virtual bool send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data)=0
Method to be implemented by child classes to send data to the interface.
void add_packet_interface_listener(F &&callback)
Add a listener that will be called when data is received.
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...
constexpr size_t remaining_packet_size(size_t size, size_t used)
The room left for a packet of size bytes after used bytes, or 0 if it is too small.
ESPHOME_LOG_TAG(TAG, "packet_interface.session")
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Application App
Global storage of Application pointer - only one Application can exist.