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;
22 if (!connected && this->connected_)
23 this->set_connected_(
false);
28void BasicSession::setup() {
29 if (this->lower_->get_max_packet_size() < KEEP_ALIVE_SIZE) {
30 this->mark_failed(LOG_STR(
"max_packet_size too small for the configured layers"));
34#ifdef USE_BINARY_SENSOR
35 if (this->connected_binary_sensor_ !=
nullptr)
36 this->connected_binary_sensor_->publish_initial_state(
false);
38 this->lower_->add_packet_interface_listener(
39 [
this](
const PacketBuffer &data,
const PacketMetaData &meta_data) { this->receive_(data, meta_data); });
41 this->send_keep_alive_();
42 this->set_interval(this->keep_alive_ms_, [
this] { this->on_interval_(); });
45void BasicSession::dump_config() {
47 "Basic Session on '%s':\n"
48 " Max Packet Size: %zu\n"
49 " Keep Alive: %" PRIu32
"ms\n"
50 " Timeout: %" PRIu32
"ms",
51 this->name_, this->max_packet_size_, this->keep_alive_ms_, this->timeout_ms_);
52#ifdef USE_BINARY_SENSOR
53 LOG_BINARY_SENSOR(
" ",
"Connected", this->connected_binary_sensor_);
57bool BasicSession::send_to_interface(
const PacketBuffer &data,
const PacketMetaData &meta_data) {
58 if (this->is_failed())
60 uint8_t header = PACKET_TYPE_DATA;
61 PacketBuffer packet(&header, HEADER_SIZE, &data,
nullptr, 0);
62 return this->lower_->send_to_interface(packet, meta_data);
65void BasicSession::send_keep_alive_() {
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)};
69 if (!this->lower_->send_to_interface(
PacketBuffer(packet,
sizeof(packet)), {}))
70 ESP_LOGV(TAG,
"%s: Lower layer did not accept keep-alive", this->name_);
73void BasicSession::on_interval_() {
74 this->send_keep_alive_();
76 ESP_LOGW(TAG,
"%s: Peer not heard for %" PRIu32
"ms", this->name_, this->timeout_ms_);
77 this->set_connected_(
false);
81void BasicSession::set_connected_(
bool connected) {
82 this->connected_ = connected;
83 ESP_LOGD(TAG,
"%s: Session %s", this->name_, connected ? LOG_STR_LITERAL(
"connected") : LOG_STR_LITERAL(
"lost"));
84#ifdef USE_BINARY_SENSOR
85 PUBLISH_STATE(this->connected_binary_sensor_, connected);
87 this->session_callback_.call(connected);
92 if (!data.is_contiguous()) {
93 ESP_LOGW(TAG,
"%s: Packet split across buffers is not supported", this->name_);
96 auto packet = data.as_span();
98 this->note_received_(
false);
101 uint8_t
type = packet[0];
102 if (
type == PACKET_TYPE_KEEP_ALIVE) {
103 if (packet.size() != KEEP_ALIVE_SIZE) {
104 ESP_LOGV(TAG,
"%s: Keep-alive of %zu bytes dropped", this->name_, packet.size());
105 this->note_received_(
false);
109 if (!this->peer_known_ || boot_id != this->peer_boot_id_) {
111 if (this->connected_ && this->peer_known_) {
112 ESP_LOGD(TAG,
"%s: Peer has restarted", this->name_);
113 this->set_connected_(
false);
115 this->peer_boot_id_ = boot_id;
116 this->peer_known_ =
true;
118 this->send_keep_alive_();
120 }
else if (
type != PACKET_TYPE_DATA) {
121 ESP_LOGV(TAG,
"%s: Unknown packet type 0x%02X dropped", this->name_,
type);
122 this->note_received_(
false);
125 this->note_received_(
true);
127 if (!this->connected_)
128 this->set_connected_(
true);
129 if (
type == PACKET_TYPE_DATA)
130 this->on_receive_from_interface_(
PacketBuffer(packet.data() + HEADER_SIZE, packet.size() - HEADER_SIZE), meta_data);
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.