diff --git a/CODEOWNERS b/CODEOWNERS index 540f33853d..ca3849eb0d 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -87,6 +87,7 @@ esphome/components/bp1658cj/* @Cossid esphome/components/bp5758d/* @Cossid esphome/components/button/* @esphome/core esphome/components/bytebuffer/* @clydebarrow +esphome/components/camera/* @DT-art1 @bdraco esphome/components/canbus/* @danielschramm @mvturnho esphome/components/cap1188/* @mreditor97 esphome/components/captive_portal/* @OttoWinter @@ -441,6 +442,7 @@ esphome/components/sun/* @OttoWinter esphome/components/sun_gtil2/* @Mat931 esphome/components/switch/* @esphome/core esphome/components/switch/binary_sensor/* @ssieb +esphome/components/sx126x/* @swoboda1337 esphome/components/sx127x/* @swoboda1337 esphome/components/syslog/* @clydebarrow esphome/components/t6615/* @tylermenezes diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index a9aa0b4bff..c3795bb796 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -836,7 +836,7 @@ message ListEntitiesCameraResponse { option (id) = 43; option (base_class) = "InfoResponseProtoMessage"; option (source) = SOURCE_SERVER; - option (ifdef) = "USE_ESP32_CAMERA"; + option (ifdef) = "USE_CAMERA"; string object_id = 1; fixed32 key = 2; @@ -851,7 +851,7 @@ message ListEntitiesCameraResponse { message CameraImageResponse { option (id) = 44; option (source) = SOURCE_SERVER; - option (ifdef) = "USE_ESP32_CAMERA"; + option (ifdef) = "USE_CAMERA"; fixed32 key = 1; bytes data = 2; @@ -860,7 +860,7 @@ message CameraImageResponse { message CameraImageRequest { option (id) = 45; option (source) = SOURCE_CLIENT; - option (ifdef) = "USE_ESP32_CAMERA"; + option (ifdef) = "USE_CAMERA"; option (no_delay) = true; bool single = 1; diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 4d99bdbbd6..51a5769f99 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -38,8 +38,8 @@ static constexpr uint16_t PING_RETRY_INTERVAL = 1000; static constexpr uint32_t KEEPALIVE_DISCONNECT_TIMEOUT = (KEEPALIVE_TIMEOUT_MS * 5) / 2; static const char *const TAG = "api.connection"; -#ifdef USE_ESP32_CAMERA -static const int ESP32_CAMERA_STOP_STREAM = 5000; +#ifdef USE_CAMERA +static const int CAMERA_STOP_STREAM = 5000; #endif APIConnection::APIConnection(std::unique_ptr sock, APIServer *parent) @@ -58,6 +58,11 @@ APIConnection::APIConnection(std::unique_ptr sock, APIServer *pa #else #error "No frame helper defined" #endif +#ifdef USE_CAMERA + if (camera::Camera::instance() != nullptr) { + this->image_reader_ = std::unique_ptr{camera::Camera::instance()->create_image_reader()}; + } +#endif } uint32_t APIConnection::get_batch_delay_ms_() const { return this->parent_->get_batch_delay(); } @@ -180,10 +185,10 @@ void APIConnection::loop() { } } -#ifdef USE_ESP32_CAMERA - if (this->image_reader_.available() && this->helper_->can_write_without_blocking()) { - uint32_t to_send = std::min((size_t) MAX_PACKET_SIZE, this->image_reader_.available()); - bool done = this->image_reader_.available() == to_send; +#ifdef USE_CAMERA + if (this->image_reader_ && this->image_reader_->available() && this->helper_->can_write_without_blocking()) { + uint32_t to_send = std::min((size_t) MAX_PACKET_SIZE, this->image_reader_->available()); + bool done = this->image_reader_->available() == to_send; uint32_t msg_size = 0; ProtoSize::add_fixed_field<4>(msg_size, 1, true); // partial message size calculated manually since its a special case @@ -193,18 +198,18 @@ void APIConnection::loop() { auto buffer = this->create_buffer(msg_size); // fixed32 key = 1; - buffer.encode_fixed32(1, esp32_camera::global_esp32_camera->get_object_id_hash()); + buffer.encode_fixed32(1, camera::Camera::instance()->get_object_id_hash()); // bytes data = 2; - buffer.encode_bytes(2, this->image_reader_.peek_data_buffer(), to_send); + buffer.encode_bytes(2, this->image_reader_->peek_data_buffer(), to_send); // bool done = 3; buffer.encode_bool(3, done); bool success = this->send_buffer(buffer, CameraImageResponse::MESSAGE_TYPE); if (success) { - this->image_reader_.consume_data(to_send); + this->image_reader_->consume_data(to_send); if (done) { - this->image_reader_.return_image(); + this->image_reader_->return_image(); } } } @@ -1112,36 +1117,36 @@ void APIConnection::media_player_command(const MediaPlayerCommandRequest &msg) { } #endif -#ifdef USE_ESP32_CAMERA -void APIConnection::set_camera_state(std::shared_ptr image) { +#ifdef USE_CAMERA +void APIConnection::set_camera_state(std::shared_ptr image) { if (!this->flags_.state_subscription) return; - if (this->image_reader_.available()) + if (!this->image_reader_) return; - if (image->was_requested_by(esphome::esp32_camera::API_REQUESTER) || - image->was_requested_by(esphome::esp32_camera::IDLE)) - this->image_reader_.set_image(std::move(image)); + if (this->image_reader_->available()) + return; + if (image->was_requested_by(esphome::camera::API_REQUESTER) || image->was_requested_by(esphome::camera::IDLE)) + this->image_reader_->set_image(std::move(image)); } uint16_t APIConnection::try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single) { - auto *camera = static_cast(entity); + auto *camera = static_cast(entity); ListEntitiesCameraResponse msg; msg.unique_id = get_default_unique_id("camera", camera); fill_entity_info_base(camera, msg); return encode_message_to_buffer(msg, ListEntitiesCameraResponse::MESSAGE_TYPE, conn, remaining_size, is_single); } void APIConnection::camera_image(const CameraImageRequest &msg) { - if (esp32_camera::global_esp32_camera == nullptr) + if (camera::Camera::instance() == nullptr) return; if (msg.single) - esp32_camera::global_esp32_camera->request_image(esphome::esp32_camera::API_REQUESTER); + camera::Camera::instance()->request_image(esphome::camera::API_REQUESTER); if (msg.stream) { - esp32_camera::global_esp32_camera->start_stream(esphome::esp32_camera::API_REQUESTER); + camera::Camera::instance()->start_stream(esphome::camera::API_REQUESTER); - App.scheduler.set_timeout(this->parent_, "api_esp32_camera_stop_stream", ESP32_CAMERA_STOP_STREAM, []() { - esp32_camera::global_esp32_camera->stop_stream(esphome::esp32_camera::API_REQUESTER); - }); + App.scheduler.set_timeout(this->parent_, "api_camera_stop_stream", CAMERA_STOP_STREAM, + []() { camera::Camera::instance()->stop_stream(esphome::camera::API_REQUESTER); }); } } #endif diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index dc4b84a535..166dbc3656 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -60,8 +60,8 @@ class APIConnection : public APIServerConnection { #ifdef USE_TEXT_SENSOR bool send_text_sensor_state(text_sensor::TextSensor *text_sensor); #endif -#ifdef USE_ESP32_CAMERA - void set_camera_state(std::shared_ptr image); +#ifdef USE_CAMERA + void set_camera_state(std::shared_ptr image); void camera_image(const CameraImageRequest &msg) override; #endif #ifdef USE_CLIMATE @@ -425,7 +425,7 @@ class APIConnection : public APIServerConnection { static uint16_t try_send_update_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single); #endif -#ifdef USE_ESP32_CAMERA +#ifdef USE_CAMERA static uint16_t try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single); #endif @@ -455,8 +455,8 @@ class APIConnection : public APIServerConnection { // These contain vectors/pointers internally, so putting them early ensures good alignment InitialStateIterator initial_state_iterator_; ListEntitiesIterator list_entities_iterator_; -#ifdef USE_ESP32_CAMERA - esp32_camera::CameraImageReader image_reader_; +#ifdef USE_CAMERA + std::unique_ptr image_reader_; #endif // Group 3: Strings (12 bytes each on 32-bit, 4-byte aligned) diff --git a/esphome/components/api/api_frame_helper.cpp b/esphome/components/api/api_frame_helper.cpp index af6dd0220d..6ed9c95354 100644 --- a/esphome/components/api/api_frame_helper.cpp +++ b/esphome/components/api/api_frame_helper.cpp @@ -614,20 +614,14 @@ APIError APINoiseFrameHelper::read_packet(ReadPacketBuffer *buffer) { return APIError::OK; } APIError APINoiseFrameHelper::write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) { - std::vector *raw_buffer = buffer.get_buffer(); - uint16_t payload_len = static_cast(raw_buffer->size() - frame_header_padding_); - // Resize to include MAC space (required for Noise encryption) - raw_buffer->resize(raw_buffer->size() + frame_footer_size_); - - // Use write_protobuf_packets with a single packet - std::vector packets; - packets.emplace_back(type, 0, payload_len); - - return write_protobuf_packets(buffer, packets); + buffer.get_buffer()->resize(buffer.get_buffer()->size() + frame_footer_size_); + PacketInfo packet{type, 0, + static_cast(buffer.get_buffer()->size() - frame_header_padding_ - frame_footer_size_)}; + return write_protobuf_packets(buffer, std::span(&packet, 1)); } -APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, const std::vector &packets) { +APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, std::span packets) { APIError aerr = state_action_(); if (aerr != APIError::OK) { return aerr; @@ -642,18 +636,15 @@ APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, co } std::vector *raw_buffer = buffer.get_buffer(); + uint8_t *buffer_data = raw_buffer->data(); // Cache buffer pointer + this->reusable_iovs_.clear(); this->reusable_iovs_.reserve(packets.size()); // We need to encrypt each packet in place for (const auto &packet : packets) { - uint16_t type = packet.message_type; - uint16_t offset = packet.offset; - uint16_t payload_len = packet.payload_size; - uint16_t msg_len = 4 + payload_len; // type(2) + data_len(2) + payload - // The buffer already has padding at offset - uint8_t *buf_start = raw_buffer->data() + offset; + uint8_t *buf_start = buffer_data + packet.offset; // Write noise header buf_start[0] = 0x01; // indicator @@ -661,10 +652,10 @@ APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, co // Write message header (to be encrypted) const uint8_t msg_offset = 3; - buf_start[msg_offset + 0] = (uint8_t) (type >> 8); // type high byte - buf_start[msg_offset + 1] = (uint8_t) type; // type low byte - buf_start[msg_offset + 2] = (uint8_t) (payload_len >> 8); // data_len high byte - buf_start[msg_offset + 3] = (uint8_t) payload_len; // data_len low byte + buf_start[msg_offset] = static_cast(packet.message_type >> 8); // type high byte + buf_start[msg_offset + 1] = static_cast(packet.message_type); // type low byte + buf_start[msg_offset + 2] = static_cast(packet.payload_size >> 8); // data_len high byte + buf_start[msg_offset + 3] = static_cast(packet.payload_size); // data_len low byte // payload data is already in the buffer starting at offset + 7 // Make sure we have space for MAC @@ -673,7 +664,8 @@ APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, co // Encrypt the message in place NoiseBuffer mbuf; noise_buffer_init(mbuf); - noise_buffer_set_inout(mbuf, buf_start + msg_offset, msg_len, msg_len + frame_footer_size_); + noise_buffer_set_inout(mbuf, buf_start + msg_offset, 4 + packet.payload_size, + 4 + packet.payload_size + frame_footer_size_); int err = noise_cipherstate_encrypt(send_cipher_, &mbuf); if (err != 0) { @@ -683,14 +675,12 @@ APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, co } // Fill in the encrypted size - buf_start[1] = (uint8_t) (mbuf.size >> 8); - buf_start[2] = (uint8_t) mbuf.size; + buf_start[1] = static_cast(mbuf.size >> 8); + buf_start[2] = static_cast(mbuf.size); // Add iovec for this encrypted packet - struct iovec iov; - iov.iov_base = buf_start; - iov.iov_len = 3 + mbuf.size; // indicator + size + encrypted data - this->reusable_iovs_.push_back(iov); + this->reusable_iovs_.push_back( + {buf_start, static_cast(3 + mbuf.size)}); // indicator + size + encrypted data } // Send all encrypted packets in one writev call @@ -1029,18 +1019,11 @@ APIError APIPlaintextFrameHelper::read_packet(ReadPacketBuffer *buffer) { return APIError::OK; } APIError APIPlaintextFrameHelper::write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) { - std::vector *raw_buffer = buffer.get_buffer(); - uint16_t payload_len = static_cast(raw_buffer->size() - frame_header_padding_); - - // Use write_protobuf_packets with a single packet - std::vector packets; - packets.emplace_back(type, 0, payload_len); - - return write_protobuf_packets(buffer, packets); + PacketInfo packet{type, 0, static_cast(buffer.get_buffer()->size() - frame_header_padding_)}; + return write_protobuf_packets(buffer, std::span(&packet, 1)); } -APIError APIPlaintextFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, - const std::vector &packets) { +APIError APIPlaintextFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, std::span packets) { if (state_ != State::DATA) { return APIError::BAD_STATE; } @@ -1050,17 +1033,15 @@ APIError APIPlaintextFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer } std::vector *raw_buffer = buffer.get_buffer(); + uint8_t *buffer_data = raw_buffer->data(); // Cache buffer pointer + this->reusable_iovs_.clear(); this->reusable_iovs_.reserve(packets.size()); for (const auto &packet : packets) { - uint16_t type = packet.message_type; - uint16_t offset = packet.offset; - uint16_t payload_len = packet.payload_size; - // Calculate varint sizes for header layout - uint8_t size_varint_len = api::ProtoSize::varint(static_cast(payload_len)); - uint8_t type_varint_len = api::ProtoSize::varint(static_cast(type)); + uint8_t size_varint_len = api::ProtoSize::varint(static_cast(packet.payload_size)); + uint8_t type_varint_len = api::ProtoSize::varint(static_cast(packet.message_type)); uint8_t total_header_len = 1 + size_varint_len + type_varint_len; // Calculate where to start writing the header @@ -1088,23 +1069,20 @@ APIError APIPlaintextFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer // // The message starts at offset + frame_header_padding_ // So we write the header starting at offset + frame_header_padding_ - total_header_len - uint8_t *buf_start = raw_buffer->data() + offset; + uint8_t *buf_start = buffer_data + packet.offset; uint32_t header_offset = frame_header_padding_ - total_header_len; // Write the plaintext header buf_start[header_offset] = 0x00; // indicator - // Encode size varint directly into buffer - ProtoVarInt(payload_len).encode_to_buffer_unchecked(buf_start + header_offset + 1, size_varint_len); - - // Encode type varint directly into buffer - ProtoVarInt(type).encode_to_buffer_unchecked(buf_start + header_offset + 1 + size_varint_len, type_varint_len); + // Encode varints directly into buffer + ProtoVarInt(packet.payload_size).encode_to_buffer_unchecked(buf_start + header_offset + 1, size_varint_len); + ProtoVarInt(packet.message_type) + .encode_to_buffer_unchecked(buf_start + header_offset + 1 + size_varint_len, type_varint_len); // Add iovec for this packet (header + payload) - struct iovec iov; - iov.iov_base = buf_start + header_offset; - iov.iov_len = total_header_len + payload_len; - this->reusable_iovs_.push_back(iov); + this->reusable_iovs_.push_back( + {buf_start + header_offset, static_cast(total_header_len + packet.payload_size)}); } // Send all packets in one writev call diff --git a/esphome/components/api/api_frame_helper.h b/esphome/components/api/api_frame_helper.h index 1e157278a1..1bb6bc7ed3 100644 --- a/esphome/components/api/api_frame_helper.h +++ b/esphome/components/api/api_frame_helper.h @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -101,7 +102,7 @@ class APIFrameHelper { // Write multiple protobuf packets in a single operation // packets contains (message_type, offset, length) for each message in the buffer // The buffer contains all messages with appropriate padding before each - virtual APIError write_protobuf_packets(ProtoWriteBuffer buffer, const std::vector &packets) = 0; + virtual APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span packets) = 0; // Get the frame header padding required by this protocol virtual uint8_t frame_header_padding() = 0; // Get the frame footer size required by this protocol @@ -194,7 +195,7 @@ class APINoiseFrameHelper : public APIFrameHelper { APIError loop() override; APIError read_packet(ReadPacketBuffer *buffer) override; APIError write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) override; - APIError write_protobuf_packets(ProtoWriteBuffer buffer, const std::vector &packets) override; + APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span packets) override; // Get the frame header padding required by this protocol uint8_t frame_header_padding() override { return frame_header_padding_; } // Get the frame footer size required by this protocol @@ -248,7 +249,7 @@ class APIPlaintextFrameHelper : public APIFrameHelper { APIError loop() override; APIError read_packet(ReadPacketBuffer *buffer) override; APIError write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) override; - APIError write_protobuf_packets(ProtoWriteBuffer buffer, const std::vector &packets) override; + APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span packets) override; uint8_t frame_header_padding() override { return frame_header_padding_; } // Get the frame footer size required by this protocol uint8_t frame_footer_size() override { return frame_footer_size_; } diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 5c2b22d22a..3505ec758d 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -2216,7 +2216,7 @@ void ExecuteServiceRequest::calculate_size(uint32_t &total_size) const { ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false); ProtoSize::add_repeated_message(total_size, 1, this->args); } -#ifdef USE_ESP32_CAMERA +#ifdef USE_CAMERA bool ListEntitiesCameraResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 5: { diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index c0079bd29c..3bfc5f1cf4 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -1273,7 +1273,7 @@ class ExecuteServiceRequest : public ProtoMessage { bool decode_32bit(uint32_t field_id, Proto32Bit value) override; bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override; }; -#ifdef USE_ESP32_CAMERA +#ifdef USE_CAMERA class ListEntitiesCameraResponse : public InfoResponseProtoMessage { public: static constexpr uint16_t MESSAGE_TYPE = 43; diff --git a/esphome/components/api/api_pb2_dump.cpp b/esphome/components/api/api_pb2_dump.cpp index db330a17fb..84e765e40f 100644 --- a/esphome/components/api/api_pb2_dump.cpp +++ b/esphome/components/api/api_pb2_dump.cpp @@ -1890,7 +1890,7 @@ void ExecuteServiceRequest::dump_to(std::string &out) const { } out.append("}"); } -#ifdef USE_ESP32_CAMERA +#ifdef USE_CAMERA void ListEntitiesCameraResponse::dump_to(std::string &out) const { __attribute__((unused)) char buffer[64]; out.append("ListEntitiesCameraResponse {\n"); diff --git a/esphome/components/api/api_pb2_service.cpp b/esphome/components/api/api_pb2_service.cpp index de8e6574b2..92dd90053b 100644 --- a/esphome/components/api/api_pb2_service.cpp +++ b/esphome/components/api/api_pb2_service.cpp @@ -204,7 +204,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type, this->on_execute_service_request(msg); break; } -#ifdef USE_ESP32_CAMERA +#ifdef USE_CAMERA case 45: { CameraImageRequest msg; msg.decode(msg_data, msg_size); @@ -682,7 +682,7 @@ void APIServerConnection::on_button_command_request(const ButtonCommandRequest & } } #endif -#ifdef USE_ESP32_CAMERA +#ifdef USE_CAMERA void APIServerConnection::on_camera_image_request(const CameraImageRequest &msg) { if (this->check_authenticated_()) { this->camera_image(msg); diff --git a/esphome/components/api/api_pb2_service.h b/esphome/components/api/api_pb2_service.h index 8c870e5e1c..458f8ec81b 100644 --- a/esphome/components/api/api_pb2_service.h +++ b/esphome/components/api/api_pb2_service.h @@ -71,7 +71,7 @@ class APIServerConnectionBase : public ProtoService { virtual void on_execute_service_request(const ExecuteServiceRequest &value){}; -#ifdef USE_ESP32_CAMERA +#ifdef USE_CAMERA virtual void on_camera_image_request(const CameraImageRequest &value){}; #endif @@ -223,7 +223,7 @@ class APIServerConnection : public APIServerConnectionBase { #ifdef USE_BUTTON virtual void button_command(const ButtonCommandRequest &msg) = 0; #endif -#ifdef USE_ESP32_CAMERA +#ifdef USE_CAMERA virtual void camera_image(const CameraImageRequest &msg) = 0; #endif #ifdef USE_CLIMATE @@ -340,7 +340,7 @@ class APIServerConnection : public APIServerConnectionBase { #ifdef USE_BUTTON void on_button_command_request(const ButtonCommandRequest &msg) override; #endif -#ifdef USE_ESP32_CAMERA +#ifdef USE_CAMERA void on_camera_image_request(const CameraImageRequest &msg) override; #endif #ifdef USE_CLIMATE diff --git a/esphome/components/api/api_server.cpp b/esphome/components/api/api_server.cpp index 0fd9c1a228..575229cf04 100644 --- a/esphome/components/api/api_server.cpp +++ b/esphome/components/api/api_server.cpp @@ -24,6 +24,14 @@ static const char *const TAG = "api"; // APIServer APIServer *global_api_server = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) +#ifndef USE_API_YAML_SERVICES +// Global empty vector to avoid guard variables (saves 8 bytes) +// This is initialized at program startup before any threads +static const std::vector empty_user_services{}; + +const std::vector &get_empty_user_services_instance() { return empty_user_services; } +#endif + APIServer::APIServer() { global_api_server = this; // Pre-allocate shared write buffer @@ -111,15 +119,14 @@ void APIServer::setup() { } #endif -#ifdef USE_ESP32_CAMERA - if (esp32_camera::global_esp32_camera != nullptr && !esp32_camera::global_esp32_camera->is_internal()) { - esp32_camera::global_esp32_camera->add_image_callback( - [this](const std::shared_ptr &image) { - for (auto &c : this->clients_) { - if (!c->flags_.remove) - c->set_camera_state(image); - } - }); +#ifdef USE_CAMERA + if (camera::Camera::instance() != nullptr && !camera::Camera::instance()->is_internal()) { + camera::Camera::instance()->add_image_callback([this](const std::shared_ptr &image) { + for (auto &c : this->clients_) { + if (!c->flags_.remove) + c->set_camera_state(image); + } + }); } #endif } diff --git a/esphome/components/api/api_server.h b/esphome/components/api/api_server.h index 9dc2b4b7d6..f34fd55974 100644 --- a/esphome/components/api/api_server.h +++ b/esphome/components/api/api_server.h @@ -25,6 +25,11 @@ struct SavedNoisePsk { } PACKED; // NOLINT #endif +#ifndef USE_API_YAML_SERVICES +// Forward declaration of helper function +const std::vector &get_empty_user_services_instance(); +#endif + class APIServer : public Component, public Controller { public: APIServer(); @@ -151,8 +156,11 @@ class APIServer : public Component, public Controller { #ifdef USE_API_YAML_SERVICES return this->user_services_; #else - static const std::vector EMPTY; - return this->user_services_ ? *this->user_services_ : EMPTY; + if (this->user_services_) { + return *this->user_services_; + } + // Return reference to global empty instance (no guard needed) + return get_empty_user_services_instance(); #endif } diff --git a/esphome/components/api/list_entities.cpp b/esphome/components/api/list_entities.cpp index 3f84ef306e..60814e359d 100644 --- a/esphome/components/api/list_entities.cpp +++ b/esphome/components/api/list_entities.cpp @@ -40,8 +40,8 @@ LIST_ENTITIES_HANDLER(lock, lock::Lock, ListEntitiesLockResponse) #ifdef USE_VALVE LIST_ENTITIES_HANDLER(valve, valve::Valve, ListEntitiesValveResponse) #endif -#ifdef USE_ESP32_CAMERA -LIST_ENTITIES_HANDLER(camera, esp32_camera::ESP32Camera, ListEntitiesCameraResponse) +#ifdef USE_CAMERA +LIST_ENTITIES_HANDLER(camera, camera::Camera, ListEntitiesCameraResponse) #endif #ifdef USE_CLIMATE LIST_ENTITIES_HANDLER(climate, climate::Climate, ListEntitiesClimateResponse) diff --git a/esphome/components/api/list_entities.h b/esphome/components/api/list_entities.h index b9506073d2..4c83ca0935 100644 --- a/esphome/components/api/list_entities.h +++ b/esphome/components/api/list_entities.h @@ -45,8 +45,8 @@ class ListEntitiesIterator : public ComponentIterator { bool on_text_sensor(text_sensor::TextSensor *entity) override; #endif bool on_service(UserServiceDescriptor *service) override; -#ifdef USE_ESP32_CAMERA - bool on_camera(esp32_camera::ESP32Camera *entity) override; +#ifdef USE_CAMERA + bool on_camera(camera::Camera *entity) override; #endif #ifdef USE_CLIMATE bool on_climate(climate::Climate *entity) override; diff --git a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp index 98050b552f..e0370328f2 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp @@ -52,7 +52,12 @@ bool BluetoothProxy::parse_device(const esp32_ble_tracker::ESPBTDevice &device) return true; } -static constexpr size_t FLUSH_BATCH_SIZE = 8; +// Batch size for BLE advertisements to maximize WiFi efficiency +// Each advertisement is up to 80 bytes when packaged (including protocol overhead) +// Most advertisements are 20-30 bytes, allowing even more to fit per packet +// 16 advertisements × 80 bytes (worst case) = 1280 bytes out of ~1320 bytes usable payload +// This achieves ~97% WiFi MTU utilization while staying under the limit +static constexpr size_t FLUSH_BATCH_SIZE = 16; // Global batch buffer to avoid guard variable (saves 8 bytes) // This is initialized at program startup before any threads @@ -173,7 +178,7 @@ int BluetoothProxy::get_bluetooth_connections_free() { void BluetoothProxy::loop() { if (!api::global_api_server->is_connected() || this->api_connection_ == nullptr) { for (auto *connection : this->connections_) { - if (connection->get_address() != 0) { + if (connection->get_address() != 0 && !connection->disconnect_pending()) { connection->disconnect(); } } diff --git a/esphome/components/camera/__init__.py b/esphome/components/camera/__init__.py new file mode 100644 index 0000000000..a19f7707af --- /dev/null +++ b/esphome/components/camera/__init__.py @@ -0,0 +1 @@ +CODEOWNERS = ["@DT-art1", "@bdraco"] diff --git a/esphome/components/camera/camera.cpp b/esphome/components/camera/camera.cpp new file mode 100644 index 0000000000..3bd632af5c --- /dev/null +++ b/esphome/components/camera/camera.cpp @@ -0,0 +1,22 @@ +#include "camera.h" + +namespace esphome { +namespace camera { + +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) +Camera *Camera::global_camera = nullptr; + +Camera::Camera() { + if (global_camera != nullptr) { + this->status_set_error("Multiple cameras are configured, but only one is supported."); + this->mark_failed(); + return; + } + + global_camera = this; +} + +Camera *Camera::instance() { return global_camera; } + +} // namespace camera +} // namespace esphome diff --git a/esphome/components/camera/camera.h b/esphome/components/camera/camera.h new file mode 100644 index 0000000000..fb9da58cc1 --- /dev/null +++ b/esphome/components/camera/camera.h @@ -0,0 +1,80 @@ +#pragma once + +#include "esphome/core/automation.h" +#include "esphome/core/component.h" +#include "esphome/core/entity_base.h" +#include "esphome/core/helpers.h" + +namespace esphome { +namespace camera { + +/** Different sources for filtering. + * IDLE: Camera requests to send an image to the API. + * API_REQUESTER: API requests a new image. + * WEB_REQUESTER: ESP32 web server request an image. Ignored by API. + */ +enum CameraRequester : uint8_t { IDLE, API_REQUESTER, WEB_REQUESTER }; + +/** Abstract camera image base class. + * Encapsulates the JPEG encoded data and it is shared among + * all connected clients. + */ +class CameraImage { + public: + virtual uint8_t *get_data_buffer() = 0; + virtual size_t get_data_length() = 0; + virtual bool was_requested_by(CameraRequester requester) const = 0; + virtual ~CameraImage() {} +}; + +/** Abstract image reader base class. + * Keeps track of the data offset of the camera image and + * how many bytes are remaining to read. When the image + * is returned, the shared_ptr is reset and the camera can + * reuse the memory of the camera image. + */ +class CameraImageReader { + public: + virtual void set_image(std::shared_ptr image) = 0; + virtual size_t available() const = 0; + virtual uint8_t *peek_data_buffer() = 0; + virtual void consume_data(size_t consumed) = 0; + virtual void return_image() = 0; + virtual ~CameraImageReader() {} +}; + +/** Abstract camera base class. Collaborates with API. + * 1) API server starts and installs callback (add_image_callback) + * which is called by the camera when a new image is available. + * 2) New API client connects and creates a new image reader (create_image_reader). + * 3) API connection receives protobuf CameraImageRequest and calls request_image. + * 3.a) API connection receives protobuf CameraImageRequest and calls start_stream. + * 4) Camera implementation provides JPEG data in the CameraImage and calls callback. + * 5) API connection sets the image in the image reader. + * 6) API connection consumes data from the image reader and returns the image when finished. + * 7.a) Camera captures a new image and continues with 4) until start_stream is called. + */ +class Camera : public EntityBase, public Component { + public: + Camera(); + // Camera implementation invokes callback to publish a new image. + virtual void add_image_callback(std::function)> &&callback) = 0; + /// Returns a new camera image reader that keeps track of the JPEG data in the camera image. + virtual CameraImageReader *create_image_reader() = 0; + // Connection, camera or web server requests one new JPEG image. + virtual void request_image(CameraRequester requester) = 0; + // Connection, camera or web server requests a stream of images. + virtual void start_stream(CameraRequester requester) = 0; + // Connection or web server stops the previously started stream. + virtual void stop_stream(CameraRequester requester) = 0; + virtual ~Camera() {} + /// The singleton instance of the camera implementation. + static Camera *instance(); + + protected: + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) + static Camera *global_camera; +}; + +} // namespace camera +} // namespace esphome diff --git a/esphome/components/esp32/helpers.cpp b/esphome/components/esp32/helpers.cpp new file mode 100644 index 0000000000..310e7bd94a --- /dev/null +++ b/esphome/components/esp32/helpers.cpp @@ -0,0 +1,69 @@ +#include "esphome/core/helpers.h" + +#ifdef USE_ESP32 + +#include "esp_efuse.h" +#include "esp_efuse_table.h" +#include "esp_mac.h" + +#include +#include +#include "esp_random.h" +#include "esp_system.h" + +namespace esphome { + +uint32_t random_uint32() { return esp_random(); } +bool random_bytes(uint8_t *data, size_t len) { + esp_fill_random(data, len); + return true; +} + +Mutex::Mutex() { handle_ = xSemaphoreCreateMutex(); } +Mutex::~Mutex() {} +void Mutex::lock() { xSemaphoreTake(this->handle_, portMAX_DELAY); } +bool Mutex::try_lock() { return xSemaphoreTake(this->handle_, 0) == pdTRUE; } +void Mutex::unlock() { xSemaphoreGive(this->handle_); } + +// only affects the executing core +// so should not be used as a mutex lock, only to get accurate timing +IRAM_ATTR InterruptLock::InterruptLock() { portDISABLE_INTERRUPTS(); } +IRAM_ATTR InterruptLock::~InterruptLock() { portENABLE_INTERRUPTS(); } + +void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter) +#if defined(CONFIG_SOC_IEEE802154_SUPPORTED) + // When CONFIG_SOC_IEEE802154_SUPPORTED is defined, esp_efuse_mac_get_default + // returns the 802.15.4 EUI-64 address, so we read directly from eFuse instead. + if (has_custom_mac_address()) { + esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48); + } else { + esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, 48); + } +#else + if (has_custom_mac_address()) { + esp_efuse_mac_get_custom(mac); + } else { + esp_efuse_mac_get_default(mac); + } +#endif +} + +void set_mac_address(uint8_t *mac) { esp_base_mac_addr_set(mac); } + +bool has_custom_mac_address() { +#if !defined(USE_ESP32_IGNORE_EFUSE_CUSTOM_MAC) + uint8_t mac[6]; + // do not use 'esp_efuse_mac_get_custom(mac)' because it drops an error in the logs whenever it fails +#ifndef USE_ESP32_VARIANT_ESP32 + return (esp_efuse_read_field_blob(ESP_EFUSE_USER_DATA_MAC_CUSTOM, mac, 48) == ESP_OK) && mac_address_is_valid(mac); +#else + return (esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48) == ESP_OK) && mac_address_is_valid(mac); +#endif +#else + return false; +#endif +} + +} // namespace esphome + +#endif // USE_ESP32 diff --git a/esphome/components/esp32_ble/ble.h b/esphome/components/esp32_ble/ble.h index ce452d65c4..2c5697df82 100644 --- a/esphome/components/esp32_ble/ble.h +++ b/esphome/components/esp32_ble/ble.h @@ -25,10 +25,15 @@ namespace esphome { namespace esp32_ble { // Maximum number of BLE scan results to buffer +// Sized to handle bursts of advertisements while allowing for processing delays +// With 16 advertisements per batch and some safety margin: +// - Without PSRAM: 24 entries (1.5× batch size) +// - With PSRAM: 36 entries (2.25× batch size) +// The reduced structure size (~80 bytes vs ~400 bytes) allows for larger buffers #ifdef USE_PSRAM -static constexpr uint8_t SCAN_RESULT_BUFFER_SIZE = 32; +static constexpr uint8_t SCAN_RESULT_BUFFER_SIZE = 36; #else -static constexpr uint8_t SCAN_RESULT_BUFFER_SIZE = 20; +static constexpr uint8_t SCAN_RESULT_BUFFER_SIZE = 24; #endif // Maximum size of the BLE event queue - must be power of 2 for lock-free queue @@ -51,7 +56,7 @@ enum IoCapability { IO_CAP_KBDISP = ESP_IO_CAP_KBDISP, }; -enum BLEComponentState { +enum BLEComponentState : uint8_t { /** Nothing has been initialized yet. */ BLE_COMPONENT_STATE_OFF = 0, /** BLE should be disabled on next loop. */ @@ -141,21 +146,31 @@ class ESP32BLE : public Component { private: template friend void enqueue_ble_event(Args... args); + // Vectors (12 bytes each on 32-bit, naturally aligned to 4 bytes) std::vector gap_event_handlers_; std::vector gap_scan_event_handlers_; std::vector gattc_event_handlers_; std::vector gatts_event_handlers_; std::vector ble_status_event_handlers_; - BLEComponentState state_{BLE_COMPONENT_STATE_OFF}; + // Large objects (size depends on template parameters, but typically aligned to 4 bytes) esphome::LockFreeQueue ble_events_; esphome::EventPool ble_event_pool_; - BLEAdvertising *advertising_{}; - esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE}; - uint32_t advertising_cycle_time_{}; - bool enable_on_boot_{}; + + // optional (typically 16+ bytes on 32-bit, aligned to 4 bytes) optional name_; - uint16_t appearance_{0}; + + // 4-byte aligned members + BLEAdvertising *advertising_{}; // 4 bytes (pointer) + esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE}; // 4 bytes (enum) + uint32_t advertising_cycle_time_{}; // 4 bytes + + // 2-byte aligned members + uint16_t appearance_{0}; // 2 bytes + + // 1-byte aligned members (grouped together to minimize padding) + BLEComponentState state_{BLE_COMPONENT_STATE_OFF}; // 1 byte (uint8_t enum) + bool enable_on_boot_{}; // 1 byte }; // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) diff --git a/esphome/components/esp32_camera/__init__.py b/esphome/components/esp32_camera/__init__.py index 8dc2ede372..138f318a5d 100644 --- a/esphome/components/esp32_camera/__init__.py +++ b/esphome/components/esp32_camera/__init__.py @@ -23,7 +23,7 @@ from esphome.core.entity_helpers import setup_entity DEPENDENCIES = ["esp32"] -AUTO_LOAD = ["psram"] +AUTO_LOAD = ["camera", "psram"] esp32_camera_ns = cg.esphome_ns.namespace("esp32_camera") ESP32Camera = esp32_camera_ns.class_("ESP32Camera", cg.PollingComponent, cg.EntityBase) @@ -283,6 +283,7 @@ SETTERS = { async def to_code(config): + cg.add_define("USE_CAMERA") var = cg.new_Pvariable(config[CONF_ID]) await setup_entity(var, config, "camera") await cg.register_component(var, config) diff --git a/esphome/components/esp32_camera/esp32_camera.cpp b/esphome/components/esp32_camera/esp32_camera.cpp index 243d3d3e47..eadb8a4408 100644 --- a/esphome/components/esp32_camera/esp32_camera.cpp +++ b/esphome/components/esp32_camera/esp32_camera.cpp @@ -14,8 +14,6 @@ static const char *const TAG = "esp32_camera"; /* ---------------- public API (derivated) ---------------- */ void ESP32Camera::setup() { - global_esp32_camera = this; - #ifdef USE_I2C if (this->i2c_bus_ != nullptr) { this->config_.sccb_i2c_port = this->i2c_bus_->get_port(); @@ -43,7 +41,7 @@ void ESP32Camera::setup() { xTaskCreatePinnedToCore(&ESP32Camera::framebuffer_task, "framebuffer_task", // name 1024, // stack size - nullptr, // task pv params + this, // task pv params 1, // priority nullptr, // handle 1 // core @@ -176,7 +174,7 @@ void ESP32Camera::loop() { const uint32_t now = App.get_loop_component_start_time(); if (this->idle_update_interval_ != 0 && now - this->last_idle_request_ > this->idle_update_interval_) { this->last_idle_request_ = now; - this->request_image(IDLE); + this->request_image(camera::IDLE); } // Check if we should fetch a new image @@ -202,7 +200,7 @@ void ESP32Camera::loop() { xQueueSend(this->framebuffer_return_queue_, &fb, portMAX_DELAY); return; } - this->current_image_ = std::make_shared(fb, this->single_requesters_ | this->stream_requesters_); + this->current_image_ = std::make_shared(fb, this->single_requesters_ | this->stream_requesters_); ESP_LOGD(TAG, "Got Image: len=%u", fb->len); this->new_image_callback_.call(this->current_image_); @@ -225,8 +223,6 @@ ESP32Camera::ESP32Camera() { this->config_.fb_count = 1; this->config_.grab_mode = CAMERA_GRAB_WHEN_EMPTY; this->config_.fb_location = CAMERA_FB_IN_PSRAM; - - global_esp32_camera = this; } /* ---------------- setters ---------------- */ @@ -356,7 +352,7 @@ void ESP32Camera::set_frame_buffer_count(uint8_t fb_count) { } /* ---------------- public API (specific) ---------------- */ -void ESP32Camera::add_image_callback(std::function)> &&callback) { +void ESP32Camera::add_image_callback(std::function)> &&callback) { this->new_image_callback_.add(std::move(callback)); } void ESP32Camera::add_stream_start_callback(std::function &&callback) { @@ -365,15 +361,16 @@ void ESP32Camera::add_stream_start_callback(std::function &&callback) { void ESP32Camera::add_stream_stop_callback(std::function &&callback) { this->stream_stop_callback_.add(std::move(callback)); } -void ESP32Camera::start_stream(CameraRequester requester) { +void ESP32Camera::start_stream(camera::CameraRequester requester) { this->stream_start_callback_.call(); this->stream_requesters_ |= (1U << requester); } -void ESP32Camera::stop_stream(CameraRequester requester) { +void ESP32Camera::stop_stream(camera::CameraRequester requester) { this->stream_stop_callback_.call(); this->stream_requesters_ &= ~(1U << requester); } -void ESP32Camera::request_image(CameraRequester requester) { this->single_requesters_ |= (1U << requester); } +void ESP32Camera::request_image(camera::CameraRequester requester) { this->single_requesters_ |= (1U << requester); } +camera::CameraImageReader *ESP32Camera::create_image_reader() { return new ESP32CameraImageReader; } void ESP32Camera::update_camera_parameters() { sensor_t *s = esp_camera_sensor_get(); /* update image */ @@ -402,39 +399,39 @@ void ESP32Camera::update_camera_parameters() { bool ESP32Camera::has_requested_image_() const { return this->single_requesters_ || this->stream_requesters_; } bool ESP32Camera::can_return_image_() const { return this->current_image_.use_count() == 1; } void ESP32Camera::framebuffer_task(void *pv) { + ESP32Camera *that = (ESP32Camera *) pv; while (true) { camera_fb_t *framebuffer = esp_camera_fb_get(); - xQueueSend(global_esp32_camera->framebuffer_get_queue_, &framebuffer, portMAX_DELAY); + xQueueSend(that->framebuffer_get_queue_, &framebuffer, portMAX_DELAY); // return is no-op for config with 1 fb - xQueueReceive(global_esp32_camera->framebuffer_return_queue_, &framebuffer, portMAX_DELAY); + xQueueReceive(that->framebuffer_return_queue_, &framebuffer, portMAX_DELAY); esp_camera_fb_return(framebuffer); } } -ESP32Camera *global_esp32_camera; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -/* ---------------- CameraImageReader class ---------------- */ -void CameraImageReader::set_image(std::shared_ptr image) { - this->image_ = std::move(image); +/* ---------------- ESP32CameraImageReader class ----------- */ +void ESP32CameraImageReader::set_image(std::shared_ptr image) { + this->image_ = std::static_pointer_cast(image); this->offset_ = 0; } -size_t CameraImageReader::available() const { +size_t ESP32CameraImageReader::available() const { if (!this->image_) return 0; return this->image_->get_data_length() - this->offset_; } -void CameraImageReader::return_image() { this->image_.reset(); } -void CameraImageReader::consume_data(size_t consumed) { this->offset_ += consumed; } -uint8_t *CameraImageReader::peek_data_buffer() { return this->image_->get_data_buffer() + this->offset_; } +void ESP32CameraImageReader::return_image() { this->image_.reset(); } +void ESP32CameraImageReader::consume_data(size_t consumed) { this->offset_ += consumed; } +uint8_t *ESP32CameraImageReader::peek_data_buffer() { return this->image_->get_data_buffer() + this->offset_; } -/* ---------------- CameraImage class ---------------- */ -CameraImage::CameraImage(camera_fb_t *buffer, uint8_t requesters) : buffer_(buffer), requesters_(requesters) {} +/* ---------------- ESP32CameraImage class ----------- */ +ESP32CameraImage::ESP32CameraImage(camera_fb_t *buffer, uint8_t requesters) + : buffer_(buffer), requesters_(requesters) {} -camera_fb_t *CameraImage::get_raw_buffer() { return this->buffer_; } -uint8_t *CameraImage::get_data_buffer() { return this->buffer_->buf; } -size_t CameraImage::get_data_length() { return this->buffer_->len; } -bool CameraImage::was_requested_by(CameraRequester requester) const { +camera_fb_t *ESP32CameraImage::get_raw_buffer() { return this->buffer_; } +uint8_t *ESP32CameraImage::get_data_buffer() { return this->buffer_->buf; } +size_t ESP32CameraImage::get_data_length() { return this->buffer_->len; } +bool ESP32CameraImage::was_requested_by(camera::CameraRequester requester) const { return (this->requesters_ & (1 << requester)) != 0; } diff --git a/esphome/components/esp32_camera/esp32_camera.h b/esphome/components/esp32_camera/esp32_camera.h index 75139ba400..8ce3faf039 100644 --- a/esphome/components/esp32_camera/esp32_camera.h +++ b/esphome/components/esp32_camera/esp32_camera.h @@ -7,7 +7,7 @@ #include #include "esphome/core/automation.h" #include "esphome/core/component.h" -#include "esphome/core/entity_base.h" +#include "esphome/components/camera/camera.h" #include "esphome/core/helpers.h" #ifdef USE_I2C @@ -19,9 +19,6 @@ namespace esp32_camera { class ESP32Camera; -/* ---------------- enum classes ---------------- */ -enum CameraRequester { IDLE, API_REQUESTER, WEB_REQUESTER }; - enum ESP32CameraFrameSize { ESP32_CAMERA_SIZE_160X120, // QQVGA ESP32_CAMERA_SIZE_176X144, // QCIF @@ -77,13 +74,13 @@ enum ESP32SpecialEffect { }; /* ---------------- CameraImage class ---------------- */ -class CameraImage { +class ESP32CameraImage : public camera::CameraImage { public: - CameraImage(camera_fb_t *buffer, uint8_t requester); + ESP32CameraImage(camera_fb_t *buffer, uint8_t requester); camera_fb_t *get_raw_buffer(); - uint8_t *get_data_buffer(); - size_t get_data_length(); - bool was_requested_by(CameraRequester requester) const; + uint8_t *get_data_buffer() override; + size_t get_data_length() override; + bool was_requested_by(camera::CameraRequester requester) const override; protected: camera_fb_t *buffer_; @@ -96,21 +93,21 @@ struct CameraImageData { }; /* ---------------- CameraImageReader class ---------------- */ -class CameraImageReader { +class ESP32CameraImageReader : public camera::CameraImageReader { public: - void set_image(std::shared_ptr image); - size_t available() const; - uint8_t *peek_data_buffer(); - void consume_data(size_t consumed); - void return_image(); + void set_image(std::shared_ptr image) override; + size_t available() const override; + uint8_t *peek_data_buffer() override; + void consume_data(size_t consumed) override; + void return_image() override; protected: - std::shared_ptr image_; + std::shared_ptr image_; size_t offset_{0}; }; /* ---------------- ESP32Camera class ---------------- */ -class ESP32Camera : public EntityBase, public Component { +class ESP32Camera : public camera::Camera { public: ESP32Camera(); @@ -162,14 +159,15 @@ class ESP32Camera : public EntityBase, public Component { void dump_config() override; float get_setup_priority() const override; /* public API (specific) */ - void start_stream(CameraRequester requester); - void stop_stream(CameraRequester requester); - void request_image(CameraRequester requester); + void start_stream(camera::CameraRequester requester) override; + void stop_stream(camera::CameraRequester requester) override; + void request_image(camera::CameraRequester requester) override; void update_camera_parameters(); - void add_image_callback(std::function)> &&callback); + void add_image_callback(std::function)> &&callback) override; void add_stream_start_callback(std::function &&callback); void add_stream_stop_callback(std::function &&callback); + camera::CameraImageReader *create_image_reader() override; protected: /* internal methods */ @@ -206,12 +204,12 @@ class ESP32Camera : public EntityBase, public Component { uint32_t idle_update_interval_{15000}; esp_err_t init_error_{ESP_OK}; - std::shared_ptr current_image_; + std::shared_ptr current_image_; uint8_t single_requesters_{0}; uint8_t stream_requesters_{0}; QueueHandle_t framebuffer_get_queue_; QueueHandle_t framebuffer_return_queue_; - CallbackManager)> new_image_callback_{}; + CallbackManager)> new_image_callback_{}; CallbackManager stream_start_callback_{}; CallbackManager stream_stop_callback_{}; @@ -222,13 +220,10 @@ class ESP32Camera : public EntityBase, public Component { #endif // USE_I2C }; -// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) -extern ESP32Camera *global_esp32_camera; - class ESP32CameraImageTrigger : public Trigger { public: explicit ESP32CameraImageTrigger(ESP32Camera *parent) { - parent->add_image_callback([this](const std::shared_ptr &image) { + parent->add_image_callback([this](const std::shared_ptr &image) { CameraImageData camera_image_data{}; camera_image_data.length = image->get_data_length(); camera_image_data.data = image->get_data_buffer(); diff --git a/esphome/components/esp32_camera_web_server/__init__.py b/esphome/components/esp32_camera_web_server/__init__.py index df137c8ff2..a6a7ac3630 100644 --- a/esphome/components/esp32_camera_web_server/__init__.py +++ b/esphome/components/esp32_camera_web_server/__init__.py @@ -3,7 +3,8 @@ import esphome.config_validation as cv from esphome.const import CONF_ID, CONF_MODE, CONF_PORT CODEOWNERS = ["@ayufan"] -DEPENDENCIES = ["esp32_camera", "network"] +AUTO_LOAD = ["camera"] +DEPENDENCIES = ["network"] MULTI_CONF = True esp32_camera_web_server_ns = cg.esphome_ns.namespace("esp32_camera_web_server") diff --git a/esphome/components/esp32_camera_web_server/camera_web_server.cpp b/esphome/components/esp32_camera_web_server/camera_web_server.cpp index 0a83128908..1b81989296 100644 --- a/esphome/components/esp32_camera_web_server/camera_web_server.cpp +++ b/esphome/components/esp32_camera_web_server/camera_web_server.cpp @@ -40,7 +40,7 @@ CameraWebServer::CameraWebServer() {} CameraWebServer::~CameraWebServer() {} void CameraWebServer::setup() { - if (!esp32_camera::global_esp32_camera || esp32_camera::global_esp32_camera->is_failed()) { + if (!camera::Camera::instance() || camera::Camera::instance()->is_failed()) { this->mark_failed(); return; } @@ -67,8 +67,8 @@ void CameraWebServer::setup() { httpd_register_uri_handler(this->httpd_, &uri); - esp32_camera::global_esp32_camera->add_image_callback([this](std::shared_ptr image) { - if (this->running_ && image->was_requested_by(esp32_camera::WEB_REQUESTER)) { + camera::Camera::instance()->add_image_callback([this](std::shared_ptr image) { + if (this->running_ && image->was_requested_by(camera::WEB_REQUESTER)) { this->image_ = std::move(image); xSemaphoreGive(this->semaphore_); } @@ -108,8 +108,8 @@ void CameraWebServer::loop() { } } -std::shared_ptr CameraWebServer::wait_for_image_() { - std::shared_ptr image; +std::shared_ptr CameraWebServer::wait_for_image_() { + std::shared_ptr image; image.swap(this->image_); if (!image) { @@ -172,7 +172,7 @@ esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) { uint32_t last_frame = millis(); uint32_t frames = 0; - esp32_camera::global_esp32_camera->start_stream(esphome::esp32_camera::WEB_REQUESTER); + camera::Camera::instance()->start_stream(esphome::camera::WEB_REQUESTER); while (res == ESP_OK && this->running_) { auto image = this->wait_for_image_(); @@ -205,7 +205,7 @@ esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) { res = httpd_send_all(req, STREAM_ERROR, strlen(STREAM_ERROR)); } - esp32_camera::global_esp32_camera->stop_stream(esphome::esp32_camera::WEB_REQUESTER); + camera::Camera::instance()->stop_stream(esphome::camera::WEB_REQUESTER); ESP_LOGI(TAG, "STREAM: closed. Frames: %" PRIu32, frames); @@ -215,7 +215,7 @@ esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) { esp_err_t CameraWebServer::snapshot_handler_(struct httpd_req *req) { esp_err_t res = ESP_OK; - esp32_camera::global_esp32_camera->request_image(esphome::esp32_camera::WEB_REQUESTER); + camera::Camera::instance()->request_image(esphome::camera::WEB_REQUESTER); auto image = this->wait_for_image_(); diff --git a/esphome/components/esp32_camera_web_server/camera_web_server.h b/esphome/components/esp32_camera_web_server/camera_web_server.h index 3ba8f31dd7..e70246745c 100644 --- a/esphome/components/esp32_camera_web_server/camera_web_server.h +++ b/esphome/components/esp32_camera_web_server/camera_web_server.h @@ -6,7 +6,7 @@ #include #include -#include "esphome/components/esp32_camera/esp32_camera.h" +#include "esphome/components/camera/camera.h" #include "esphome/core/component.h" #include "esphome/core/helpers.h" #include "esphome/core/preferences.h" @@ -32,7 +32,7 @@ class CameraWebServer : public Component { void loop() override; protected: - std::shared_ptr wait_for_image_(); + std::shared_ptr wait_for_image_(); esp_err_t handler_(struct httpd_req *req); esp_err_t streaming_handler_(struct httpd_req *req); esp_err_t snapshot_handler_(struct httpd_req *req); @@ -40,7 +40,7 @@ class CameraWebServer : public Component { uint16_t port_{0}; void *httpd_{nullptr}; SemaphoreHandle_t semaphore_; - std::shared_ptr image_; + std::shared_ptr image_; bool running_{false}; Mode mode_{STREAM}; }; diff --git a/esphome/components/esp8266/helpers.cpp b/esphome/components/esp8266/helpers.cpp new file mode 100644 index 0000000000..993de710c6 --- /dev/null +++ b/esphome/components/esp8266/helpers.cpp @@ -0,0 +1,31 @@ +#include "esphome/core/helpers.h" + +#ifdef USE_ESP8266 + +#include +#include +// for xt_rsil()/xt_wsr_ps() +#include + +namespace esphome { + +uint32_t random_uint32() { return os_random(); } +bool random_bytes(uint8_t *data, size_t len) { return os_get_random(data, len) == 0; } + +// ESP8266 doesn't have mutexes, but that shouldn't be an issue as it's single-core and non-preemptive OS. +Mutex::Mutex() {} +Mutex::~Mutex() {} +void Mutex::lock() {} +bool Mutex::try_lock() { return true; } +void Mutex::unlock() {} + +IRAM_ATTR InterruptLock::InterruptLock() { state_ = xt_rsil(15); } +IRAM_ATTR InterruptLock::~InterruptLock() { xt_wsr_ps(state_); } + +void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter) + wifi_get_macaddr(STATION_IF, mac); +} + +} // namespace esphome + +#endif // USE_ESP8266 diff --git a/esphome/components/host/helpers.cpp b/esphome/components/host/helpers.cpp new file mode 100644 index 0000000000..fdad4f5cb6 --- /dev/null +++ b/esphome/components/host/helpers.cpp @@ -0,0 +1,57 @@ +#include "esphome/core/helpers.h" + +#ifdef USE_HOST + +#ifndef _WIN32 +#include +#include +#include +#endif +#include +#include +#include + +#include "esphome/core/defines.h" +#include "esphome/core/log.h" + +namespace esphome { + +static const char *const TAG = "helpers.host"; + +uint32_t random_uint32() { + std::random_device dev; + std::mt19937 rng(dev()); + std::uniform_int_distribution dist(0, std::numeric_limits::max()); + return dist(rng); +} + +bool random_bytes(uint8_t *data, size_t len) { + FILE *fp = fopen("/dev/urandom", "r"); + if (fp == nullptr) { + ESP_LOGW(TAG, "Could not open /dev/urandom, errno=%d", errno); + exit(1); + } + size_t read = fread(data, 1, len, fp); + if (read != len) { + ESP_LOGW(TAG, "Not enough data from /dev/urandom"); + exit(1); + } + fclose(fp); + return true; +} + +// Host platform uses std::mutex for proper thread synchronization +Mutex::Mutex() { handle_ = new std::mutex(); } +Mutex::~Mutex() { delete static_cast(handle_); } +void Mutex::lock() { static_cast(handle_)->lock(); } +bool Mutex::try_lock() { return static_cast(handle_)->try_lock(); } +void Mutex::unlock() { static_cast(handle_)->unlock(); } + +void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter) + static const uint8_t esphome_host_mac_address[6] = USE_ESPHOME_HOST_MAC_ADDRESS; + memcpy(mac, esphome_host_mac_address, sizeof(esphome_host_mac_address)); +} + +} // namespace esphome + +#endif // USE_HOST diff --git a/esphome/components/http_request/update/http_request_update.cpp b/esphome/components/http_request/update/http_request_update.cpp index 6bc88ae49a..202c7b88b2 100644 --- a/esphome/components/http_request/update/http_request_update.cpp +++ b/esphome/components/http_request/update/http_request_update.cpp @@ -50,7 +50,8 @@ void HttpRequestUpdate::update_task(void *params) { if (container == nullptr || container->status_code != HTTP_STATUS_OK) { std::string msg = str_sprintf("Failed to fetch manifest from %s", this_update->source_url_.c_str()); - this_update->status_set_error(msg.c_str()); + // Defer to main loop to avoid race condition on component_state_ read-modify-write + this_update->defer([this_update, msg]() { this_update->status_set_error(msg.c_str()); }); UPDATE_RETURN; } @@ -58,7 +59,8 @@ void HttpRequestUpdate::update_task(void *params) { uint8_t *data = allocator.allocate(container->content_length); if (data == nullptr) { std::string msg = str_sprintf("Failed to allocate %zu bytes for manifest", container->content_length); - this_update->status_set_error(msg.c_str()); + // Defer to main loop to avoid race condition on component_state_ read-modify-write + this_update->defer([this_update, msg]() { this_update->status_set_error(msg.c_str()); }); container->end(); UPDATE_RETURN; } @@ -120,7 +122,8 @@ void HttpRequestUpdate::update_task(void *params) { if (!valid) { std::string msg = str_sprintf("Failed to parse JSON from %s", this_update->source_url_.c_str()); - this_update->status_set_error(msg.c_str()); + // Defer to main loop to avoid race condition on component_state_ read-modify-write + this_update->defer([this_update, msg]() { this_update->status_set_error(msg.c_str()); }); UPDATE_RETURN; } @@ -147,18 +150,34 @@ void HttpRequestUpdate::update_task(void *params) { this_update->update_info_.current_version = current_version; } + bool trigger_update_available = false; + if (this_update->update_info_.latest_version.empty() || this_update->update_info_.latest_version == this_update->update_info_.current_version) { this_update->state_ = update::UPDATE_STATE_NO_UPDATE; } else { + if (this_update->state_ != update::UPDATE_STATE_AVAILABLE) { + trigger_update_available = true; + } this_update->state_ = update::UPDATE_STATE_AVAILABLE; } - this_update->update_info_.has_progress = false; - this_update->update_info_.progress = 0.0f; + // Defer to main loop to ensure thread-safe execution of: + // - status_clear_error() performs non-atomic read-modify-write on component_state_ + // - publish_state() triggers API callbacks that write to the shared protobuf buffer + // which can be corrupted if accessed concurrently from task and main loop threads + // - update_available trigger to ensure consistent state when the trigger fires + this_update->defer([this_update, trigger_update_available]() { + this_update->update_info_.has_progress = false; + this_update->update_info_.progress = 0.0f; - this_update->status_clear_error(); - this_update->publish_state(); + this_update->status_clear_error(); + this_update->publish_state(); + + if (trigger_update_available) { + this_update->get_update_available_trigger()->trigger(this_update->update_info_); + } + }); UPDATE_RETURN; } diff --git a/esphome/components/inkplate6/display.py b/esphome/components/inkplate6/display.py index a7d31c0131..063fc8b0aa 100644 --- a/esphome/components/inkplate6/display.py +++ b/esphome/components/inkplate6/display.py @@ -1,6 +1,7 @@ from esphome import pins import esphome.codegen as cg from esphome.components import display, i2c +from esphome.components.esp32 import CONF_CPU_FREQUENCY import esphome.config_validation as cv from esphome.const import ( CONF_FULL_UPDATE_EVERY, @@ -13,7 +14,9 @@ from esphome.const import ( CONF_PAGES, CONF_TRANSFORM, CONF_WAKEUP_PIN, + PLATFORM_ESP32, ) +import esphome.final_validate as fv DEPENDENCIES = ["i2c", "esp32"] AUTO_LOAD = ["psram"] @@ -120,6 +123,18 @@ CONFIG_SCHEMA = cv.All( ) +def _validate_cpu_frequency(config): + esp32_config = fv.full_config.get()[PLATFORM_ESP32] + if esp32_config[CONF_CPU_FREQUENCY] != "240MHZ": + raise cv.Invalid( + "Inkplate requires 240MHz CPU frequency (set in esp32 component)" + ) + return config + + +FINAL_VALIDATE_SCHEMA = _validate_cpu_frequency + + async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) diff --git a/esphome/components/ld2410/button/__init__.py b/esphome/components/ld2410/button/__init__.py index 4cb50d707b..1cd56082c3 100644 --- a/esphome/components/ld2410/button/__init__.py +++ b/esphome/components/ld2410/button/__init__.py @@ -14,8 +14,8 @@ from esphome.const import ( from .. import CONF_LD2410_ID, LD2410Component, ld2410_ns +FactoryResetButton = ld2410_ns.class_("FactoryResetButton", button.Button) QueryButton = ld2410_ns.class_("QueryButton", button.Button) -ResetButton = ld2410_ns.class_("ResetButton", button.Button) RestartButton = ld2410_ns.class_("RestartButton", button.Button) CONF_QUERY_PARAMS = "query_params" @@ -23,7 +23,7 @@ CONF_QUERY_PARAMS = "query_params" CONFIG_SCHEMA = { cv.GenerateID(CONF_LD2410_ID): cv.use_id(LD2410Component), cv.Optional(CONF_FACTORY_RESET): button.button_schema( - ResetButton, + FactoryResetButton, device_class=DEVICE_CLASS_RESTART, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_RESTART_ALERT, @@ -47,7 +47,7 @@ async def to_code(config): if factory_reset_config := config.get(CONF_FACTORY_RESET): b = await button.new_button(factory_reset_config) await cg.register_parented(b, config[CONF_LD2410_ID]) - cg.add(ld2410_component.set_reset_button(b)) + cg.add(ld2410_component.set_factory_reset_button(b)) if restart_config := config.get(CONF_RESTART): b = await button.new_button(restart_config) await cg.register_parented(b, config[CONF_LD2410_ID]) diff --git a/esphome/components/ld2410/button/factory_reset_button.cpp b/esphome/components/ld2410/button/factory_reset_button.cpp new file mode 100644 index 0000000000..a848b02a9d --- /dev/null +++ b/esphome/components/ld2410/button/factory_reset_button.cpp @@ -0,0 +1,9 @@ +#include "factory_reset_button.h" + +namespace esphome { +namespace ld2410 { + +void FactoryResetButton::press_action() { this->parent_->factory_reset(); } + +} // namespace ld2410 +} // namespace esphome diff --git a/esphome/components/ld2410/button/reset_button.h b/esphome/components/ld2410/button/factory_reset_button.h similarity index 65% rename from esphome/components/ld2410/button/reset_button.h rename to esphome/components/ld2410/button/factory_reset_button.h index 78dd92c9f5..45bf979033 100644 --- a/esphome/components/ld2410/button/reset_button.h +++ b/esphome/components/ld2410/button/factory_reset_button.h @@ -6,9 +6,9 @@ namespace esphome { namespace ld2410 { -class ResetButton : public button::Button, public Parented { +class FactoryResetButton : public button::Button, public Parented { public: - ResetButton() = default; + FactoryResetButton() = default; protected: void press_action() override; diff --git a/esphome/components/ld2410/button/reset_button.cpp b/esphome/components/ld2410/button/reset_button.cpp deleted file mode 100644 index f16c5faa79..0000000000 --- a/esphome/components/ld2410/button/reset_button.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "reset_button.h" - -namespace esphome { -namespace ld2410 { - -void ResetButton::press_action() { this->parent_->factory_reset(); } - -} // namespace ld2410 -} // namespace esphome diff --git a/esphome/components/ld2410/ld2410.cpp b/esphome/components/ld2410/ld2410.cpp index a34f99ee33..375d1088e8 100644 --- a/esphome/components/ld2410/ld2410.cpp +++ b/esphome/components/ld2410/ld2410.cpp @@ -18,11 +18,10 @@ namespace esphome { namespace ld2410 { static const char *const TAG = "ld2410"; -static const char *const NO_MAC = "08:05:04:03:02:01"; static const char *const UNKNOWN_MAC = "unknown"; static const char *const VERSION_FMT = "%u.%02X.%02X%02X%02X%02X"; -enum BaudRateStructure : uint8_t { +enum BaudRate : uint8_t { BAUD_RATE_9600 = 1, BAUD_RATE_19200 = 2, BAUD_RATE_38400 = 3, @@ -33,23 +32,23 @@ enum BaudRateStructure : uint8_t { BAUD_RATE_460800 = 8, }; -enum DistanceResolutionStructure : uint8_t { +enum DistanceResolution : uint8_t { DISTANCE_RESOLUTION_0_2 = 0x01, DISTANCE_RESOLUTION_0_75 = 0x00, }; -enum LightFunctionStructure : uint8_t { +enum LightFunction : uint8_t { LIGHT_FUNCTION_OFF = 0x00, LIGHT_FUNCTION_BELOW = 0x01, LIGHT_FUNCTION_ABOVE = 0x02, }; -enum OutPinLevelStructure : uint8_t { +enum OutPinLevel : uint8_t { OUT_PIN_LEVEL_LOW = 0x00, OUT_PIN_LEVEL_HIGH = 0x01, }; -enum PeriodicDataStructure : uint8_t { +enum PeriodicData : uint8_t { DATA_TYPES = 6, TARGET_STATES = 8, MOVING_TARGET_LOW = 9, @@ -67,12 +66,12 @@ enum PeriodicDataStructure : uint8_t { }; enum PeriodicDataValue : uint8_t { - HEAD = 0xAA, - END = 0x55, + HEADER = 0xAA, + FOOTER = 0x55, CHECK = 0x00, }; -enum AckDataStructure : uint8_t { +enum AckData : uint8_t { COMMAND = 6, COMMAND_STATUS = 7, }; @@ -80,11 +79,11 @@ enum AckDataStructure : uint8_t { // Memory-efficient lookup tables struct StringToUint8 { const char *str; - uint8_t value; + const uint8_t value; }; struct Uint8ToString { - uint8_t value; + const uint8_t value; const char *str; }; @@ -144,96 +143,119 @@ template const char *find_str(const Uint8ToString (&arr)[N], uint8_t v } // Commands -static const uint8_t CMD_ENABLE_CONF = 0xFF; -static const uint8_t CMD_DISABLE_CONF = 0xFE; -static const uint8_t CMD_ENABLE_ENG = 0x62; -static const uint8_t CMD_DISABLE_ENG = 0x63; -static const uint8_t CMD_MAXDIST_DURATION = 0x60; -static const uint8_t CMD_QUERY = 0x61; -static const uint8_t CMD_GATE_SENS = 0x64; -static const uint8_t CMD_VERSION = 0xA0; -static const uint8_t CMD_QUERY_DISTANCE_RESOLUTION = 0xAB; -static const uint8_t CMD_SET_DISTANCE_RESOLUTION = 0xAA; -static const uint8_t CMD_QUERY_LIGHT_CONTROL = 0xAE; -static const uint8_t CMD_SET_LIGHT_CONTROL = 0xAD; -static const uint8_t CMD_SET_BAUD_RATE = 0xA1; -static const uint8_t CMD_BT_PASSWORD = 0xA9; -static const uint8_t CMD_MAC = 0xA5; -static const uint8_t CMD_RESET = 0xA2; -static const uint8_t CMD_RESTART = 0xA3; -static const uint8_t CMD_BLUETOOTH = 0xA4; +static constexpr uint8_t CMD_ENABLE_CONF = 0xFF; +static constexpr uint8_t CMD_DISABLE_CONF = 0xFE; +static constexpr uint8_t CMD_ENABLE_ENG = 0x62; +static constexpr uint8_t CMD_DISABLE_ENG = 0x63; +static constexpr uint8_t CMD_MAXDIST_DURATION = 0x60; +static constexpr uint8_t CMD_QUERY = 0x61; +static constexpr uint8_t CMD_GATE_SENS = 0x64; +static constexpr uint8_t CMD_QUERY_VERSION = 0xA0; +static constexpr uint8_t CMD_QUERY_DISTANCE_RESOLUTION = 0xAB; +static constexpr uint8_t CMD_SET_DISTANCE_RESOLUTION = 0xAA; +static constexpr uint8_t CMD_QUERY_LIGHT_CONTROL = 0xAE; +static constexpr uint8_t CMD_SET_LIGHT_CONTROL = 0xAD; +static constexpr uint8_t CMD_SET_BAUD_RATE = 0xA1; +static constexpr uint8_t CMD_BT_PASSWORD = 0xA9; +static constexpr uint8_t CMD_QUERY_MAC_ADDRESS = 0xA5; +static constexpr uint8_t CMD_RESET = 0xA2; +static constexpr uint8_t CMD_RESTART = 0xA3; +static constexpr uint8_t CMD_BLUETOOTH = 0xA4; // Commands values -static const uint8_t CMD_MAX_MOVE_VALUE = 0x00; -static const uint8_t CMD_MAX_STILL_VALUE = 0x01; -static const uint8_t CMD_DURATION_VALUE = 0x02; +static constexpr uint8_t CMD_MAX_MOVE_VALUE = 0x00; +static constexpr uint8_t CMD_MAX_STILL_VALUE = 0x01; +static constexpr uint8_t CMD_DURATION_VALUE = 0x02; +// Header & Footer size +static constexpr uint8_t HEADER_FOOTER_SIZE = 4; // Command Header & Footer -static const uint8_t CMD_FRAME_HEADER[4] = {0xFD, 0xFC, 0xFB, 0xFA}; -static const uint8_t CMD_FRAME_END[4] = {0x04, 0x03, 0x02, 0x01}; +static constexpr uint8_t CMD_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xFD, 0xFC, 0xFB, 0xFA}; +static constexpr uint8_t CMD_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0x04, 0x03, 0x02, 0x01}; // Data Header & Footer -static const uint8_t DATA_FRAME_HEADER[4] = {0xF4, 0xF3, 0xF2, 0xF1}; -static const uint8_t DATA_FRAME_END[4] = {0xF8, 0xF7, 0xF6, 0xF5}; +static constexpr uint8_t DATA_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xF4, 0xF3, 0xF2, 0xF1}; +static constexpr uint8_t DATA_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0xF8, 0xF7, 0xF6, 0xF5}; +// MAC address the module uses when Bluetooth is disabled +static constexpr uint8_t NO_MAC[] = {0x08, 0x05, 0x04, 0x03, 0x02, 0x01}; static inline int two_byte_to_int(char firstbyte, char secondbyte) { return (int16_t) (secondbyte << 8) + firstbyte; } +static bool validate_header_footer(const uint8_t *header_footer, const uint8_t *buffer) { + for (uint8_t i = 0; i < HEADER_FOOTER_SIZE; i++) { + if (header_footer[i] != buffer[i]) { + return false; // Mismatch in header/footer + } + } + return true; // Valid header/footer +} + void LD2410Component::dump_config() { - ESP_LOGCONFIG(TAG, "LD2410:"); + std::string mac_str = + mac_address_is_valid(this->mac_address_) ? format_mac_address_pretty(this->mac_address_) : UNKNOWN_MAC; + std::string version = str_sprintf(VERSION_FMT, this->version_[1], this->version_[0], this->version_[5], + this->version_[4], this->version_[3], this->version_[2]); + ESP_LOGCONFIG(TAG, + "LD2410:\n" + " Firmware version: %s\n" + " MAC address: %s\n" + " Throttle: %u ms", + version.c_str(), mac_str.c_str(), this->throttle_); #ifdef USE_BINARY_SENSOR - LOG_BINARY_SENSOR(" ", "TargetBinarySensor", this->target_binary_sensor_); - LOG_BINARY_SENSOR(" ", "MovingTargetBinarySensor", this->moving_target_binary_sensor_); - LOG_BINARY_SENSOR(" ", "StillTargetBinarySensor", this->still_target_binary_sensor_); - LOG_BINARY_SENSOR(" ", "OutPinPresenceStatusBinarySensor", this->out_pin_presence_status_binary_sensor_); -#endif -#ifdef USE_SWITCH - LOG_SWITCH(" ", "EngineeringModeSwitch", this->engineering_mode_switch_); - LOG_SWITCH(" ", "BluetoothSwitch", this->bluetooth_switch_); -#endif -#ifdef USE_BUTTON - LOG_BUTTON(" ", "ResetButton", this->reset_button_); - LOG_BUTTON(" ", "RestartButton", this->restart_button_); - LOG_BUTTON(" ", "QueryButton", this->query_button_); + ESP_LOGCONFIG(TAG, "Binary Sensors:"); + LOG_BINARY_SENSOR(" ", "Target", this->target_binary_sensor_); + LOG_BINARY_SENSOR(" ", "MovingTarget", this->moving_target_binary_sensor_); + LOG_BINARY_SENSOR(" ", "StillTarget", this->still_target_binary_sensor_); + LOG_BINARY_SENSOR(" ", "OutPinPresenceStatus", this->out_pin_presence_status_binary_sensor_); #endif #ifdef USE_SENSOR - LOG_SENSOR(" ", "LightSensor", this->light_sensor_); - LOG_SENSOR(" ", "MovingTargetDistanceSensor", this->moving_target_distance_sensor_); - LOG_SENSOR(" ", "StillTargetDistanceSensor", this->still_target_distance_sensor_); - LOG_SENSOR(" ", "MovingTargetEnergySensor", this->moving_target_energy_sensor_); - LOG_SENSOR(" ", "StillTargetEnergySensor", this->still_target_energy_sensor_); - LOG_SENSOR(" ", "DetectionDistanceSensor", this->detection_distance_sensor_); - for (sensor::Sensor *s : this->gate_still_sensors_) { - LOG_SENSOR(" ", "NthGateStillSesnsor", s); - } + ESP_LOGCONFIG(TAG, "Sensors:"); + LOG_SENSOR(" ", "Light", this->light_sensor_); + LOG_SENSOR(" ", "DetectionDistance", this->detection_distance_sensor_); + LOG_SENSOR(" ", "MovingTargetDistance", this->moving_target_distance_sensor_); + LOG_SENSOR(" ", "MovingTargetEnergy", this->moving_target_energy_sensor_); + LOG_SENSOR(" ", "StillTargetDistance", this->still_target_distance_sensor_); + LOG_SENSOR(" ", "StillTargetEnergy", this->still_target_energy_sensor_); for (sensor::Sensor *s : this->gate_move_sensors_) { - LOG_SENSOR(" ", "NthGateMoveSesnsor", s); + LOG_SENSOR(" ", "GateMove", s); + } + for (sensor::Sensor *s : this->gate_still_sensors_) { + LOG_SENSOR(" ", "GateStill", s); } #endif #ifdef USE_TEXT_SENSOR - LOG_TEXT_SENSOR(" ", "VersionTextSensor", this->version_text_sensor_); - LOG_TEXT_SENSOR(" ", "MacTextSensor", this->mac_text_sensor_); -#endif -#ifdef USE_SELECT - LOG_SELECT(" ", "LightFunctionSelect", this->light_function_select_); - LOG_SELECT(" ", "OutPinLevelSelect", this->out_pin_level_select_); - LOG_SELECT(" ", "DistanceResolutionSelect", this->distance_resolution_select_); - LOG_SELECT(" ", "BaudRateSelect", this->baud_rate_select_); + ESP_LOGCONFIG(TAG, "Text Sensors:"); + LOG_TEXT_SENSOR(" ", "Mac", this->mac_text_sensor_); + LOG_TEXT_SENSOR(" ", "Version", this->version_text_sensor_); #endif #ifdef USE_NUMBER - LOG_NUMBER(" ", "LightThresholdNumber", this->light_threshold_number_); - LOG_NUMBER(" ", "MaxStillDistanceGateNumber", this->max_still_distance_gate_number_); - LOG_NUMBER(" ", "MaxMoveDistanceGateNumber", this->max_move_distance_gate_number_); - LOG_NUMBER(" ", "TimeoutNumber", this->timeout_number_); - for (number::Number *n : this->gate_still_threshold_numbers_) { - LOG_NUMBER(" ", "Still Thresholds Number", n); - } + ESP_LOGCONFIG(TAG, "Numbers:"); + LOG_NUMBER(" ", "LightThreshold", this->light_threshold_number_); + LOG_NUMBER(" ", "MaxMoveDistanceGate", this->max_move_distance_gate_number_); + LOG_NUMBER(" ", "MaxStillDistanceGate", this->max_still_distance_gate_number_); + LOG_NUMBER(" ", "Timeout", this->timeout_number_); for (number::Number *n : this->gate_move_threshold_numbers_) { - LOG_NUMBER(" ", "Move Thresholds Number", n); + LOG_NUMBER(" ", "MoveThreshold", n); + } + for (number::Number *n : this->gate_still_threshold_numbers_) { + LOG_NUMBER(" ", "StillThreshold", n); } #endif - this->read_all_info(); - ESP_LOGCONFIG(TAG, - " Throttle: %ums\n" - " MAC address: %s\n" - " Firmware version: %s", - this->throttle_, this->mac_ == NO_MAC ? UNKNOWN_MAC : this->mac_.c_str(), this->version_.c_str()); +#ifdef USE_SELECT + ESP_LOGCONFIG(TAG, "Selects:"); + LOG_SELECT(" ", "BaudRate", this->baud_rate_select_); + LOG_SELECT(" ", "DistanceResolution", this->distance_resolution_select_); + LOG_SELECT(" ", "LightFunction", this->light_function_select_); + LOG_SELECT(" ", "OutPinLevel", this->out_pin_level_select_); +#endif +#ifdef USE_SWITCH + ESP_LOGCONFIG(TAG, "Switches:"); + LOG_SWITCH(" ", "Bluetooth", this->bluetooth_switch_); + LOG_SWITCH(" ", "EngineeringMode", this->engineering_mode_switch_); +#endif +#ifdef USE_BUTTON + ESP_LOGCONFIG(TAG, "Buttons:"); + LOG_BUTTON(" ", "FactoryReset", this->factory_reset_button_); + LOG_BUTTON(" ", "Query", this->query_button_); + LOG_BUTTON(" ", "Restart", this->restart_button_); +#endif } void LD2410Component::setup() { @@ -246,12 +268,12 @@ void LD2410Component::read_all_info() { this->get_version_(); this->get_mac_(); this->get_distance_resolution_(); - this->get_light_control_(); + this->query_light_control_(); this->query_parameters_(); this->set_config_mode_(false); #ifdef USE_SELECT const auto baud_rate = std::to_string(this->parent_->get_baud_rate()); - if (this->baud_rate_select_ != nullptr && this->baud_rate_select_->state != baud_rate) { + if (this->baud_rate_select_ != nullptr) { this->baud_rate_select_->publish_state(baud_rate); } #endif @@ -264,66 +286,59 @@ void LD2410Component::restart_and_read_all_info() { } void LD2410Component::loop() { - const int max_line_length = 80; - static uint8_t buffer[max_line_length]; - - while (available()) { - this->readline_(read(), buffer, max_line_length); + while (this->available()) { + this->readline_(this->read()); } } -void LD2410Component::send_command_(uint8_t command, const uint8_t *command_value, int command_value_len) { +void LD2410Component::send_command_(uint8_t command, const uint8_t *command_value, uint8_t command_value_len) { ESP_LOGV(TAG, "Sending COMMAND %02X", command); - // frame start bytes - this->write_array(CMD_FRAME_HEADER, 4); + // frame header bytes + this->write_array(CMD_FRAME_HEADER, sizeof(CMD_FRAME_HEADER)); // length bytes - int len = 2; - if (command_value != nullptr) + uint8_t len = 2; + if (command_value != nullptr) { len += command_value_len; - this->write_byte(lowbyte(len)); - this->write_byte(highbyte(len)); - - // command - this->write_byte(lowbyte(command)); - this->write_byte(highbyte(command)); + } + uint8_t len_cmd[] = {lowbyte(len), highbyte(len), command, 0x00}; + this->write_array(len_cmd, sizeof(len_cmd)); // command value bytes if (command_value != nullptr) { - for (int i = 0; i < command_value_len; i++) { + for (uint8_t i = 0; i < command_value_len; i++) { this->write_byte(command_value[i]); } } - // frame end bytes - this->write_array(CMD_FRAME_END, 4); + // frame footer bytes + this->write_array(CMD_FRAME_FOOTER, sizeof(CMD_FRAME_FOOTER)); // FIXME to remove delay(50); // NOLINT } -void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) { - if (len < 12) - return; // 4 frame start bytes + 2 length bytes + 1 data end byte + 1 crc byte + 4 frame end bytes - if (buffer[0] != 0xF4 || buffer[1] != 0xF3 || buffer[2] != 0xF2 || buffer[3] != 0xF1) // check 4 frame start bytes +void LD2410Component::handle_periodic_data_() { + // Reduce data update rate to reduce home assistant database growth + // Check this first to prevent unnecessary processing done in later checks/parsing + if (App.get_loop_component_start_time() - this->last_periodic_millis_ < this->throttle_) { return; - if (buffer[7] != HEAD || buffer[len - 6] != END || buffer[len - 5] != CHECK) // Check constant values - return; // data head=0xAA, data end=0x55, crc=0x00 - - /* - Reduce data update rate to prevent home assistant database size grow fast - */ - int32_t current_millis = App.get_loop_component_start_time(); - if (current_millis - last_periodic_millis_ < this->throttle_) + } + // 4 frame header bytes + 2 length bytes + 1 data end byte + 1 crc byte + 4 frame footer bytes + // data header=0xAA, data footer=0x55, crc=0x00 + if (this->buffer_pos_ < 12 || !ld2410::validate_header_footer(DATA_FRAME_HEADER, this->buffer_data_) || + this->buffer_data_[7] != HEADER || this->buffer_data_[this->buffer_pos_ - 6] != FOOTER || + this->buffer_data_[this->buffer_pos_ - 5] != CHECK) { return; - last_periodic_millis_ = current_millis; + } + // Save the timestamp after validating the frame so, if invalid, we'll take the next frame immediately + this->last_periodic_millis_ = App.get_loop_component_start_time(); /* Data Type: 7th 0x01: Engineering mode 0x02: Normal mode */ - bool engineering_mode = buffer[DATA_TYPES] == 0x01; + bool engineering_mode = this->buffer_data_[DATA_TYPES] == 0x01; #ifdef USE_SWITCH - if (this->engineering_mode_switch_ != nullptr && - current_millis - last_engineering_mode_change_millis_ > this->throttle_) { + if (this->engineering_mode_switch_ != nullptr) { this->engineering_mode_switch_->publish_state(engineering_mode); } #endif @@ -335,7 +350,7 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) { 0x02 = Still targets 0x03 = Moving+Still targets */ - char target_state = buffer[TARGET_STATES]; + char target_state = this->buffer_data_[TARGET_STATES]; if (this->target_binary_sensor_ != nullptr) { this->target_binary_sensor_->publish_state(target_state != 0x00); } @@ -355,27 +370,30 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) { */ #ifdef USE_SENSOR if (this->moving_target_distance_sensor_ != nullptr) { - int new_moving_target_distance = ld2410::two_byte_to_int(buffer[MOVING_TARGET_LOW], buffer[MOVING_TARGET_HIGH]); + int new_moving_target_distance = + ld2410::two_byte_to_int(this->buffer_data_[MOVING_TARGET_LOW], this->buffer_data_[MOVING_TARGET_HIGH]); if (this->moving_target_distance_sensor_->get_state() != new_moving_target_distance) this->moving_target_distance_sensor_->publish_state(new_moving_target_distance); } if (this->moving_target_energy_sensor_ != nullptr) { - int new_moving_target_energy = buffer[MOVING_ENERGY]; + int new_moving_target_energy = this->buffer_data_[MOVING_ENERGY]; if (this->moving_target_energy_sensor_->get_state() != new_moving_target_energy) this->moving_target_energy_sensor_->publish_state(new_moving_target_energy); } if (this->still_target_distance_sensor_ != nullptr) { - int new_still_target_distance = ld2410::two_byte_to_int(buffer[STILL_TARGET_LOW], buffer[STILL_TARGET_HIGH]); + int new_still_target_distance = + ld2410::two_byte_to_int(this->buffer_data_[STILL_TARGET_LOW], this->buffer_data_[STILL_TARGET_HIGH]); if (this->still_target_distance_sensor_->get_state() != new_still_target_distance) this->still_target_distance_sensor_->publish_state(new_still_target_distance); } if (this->still_target_energy_sensor_ != nullptr) { - int new_still_target_energy = buffer[STILL_ENERGY]; + int new_still_target_energy = this->buffer_data_[STILL_ENERGY]; if (this->still_target_energy_sensor_->get_state() != new_still_target_energy) this->still_target_energy_sensor_->publish_state(new_still_target_energy); } if (this->detection_distance_sensor_ != nullptr) { - int new_detect_distance = ld2410::two_byte_to_int(buffer[DETECT_DISTANCE_LOW], buffer[DETECT_DISTANCE_HIGH]); + int new_detect_distance = + ld2410::two_byte_to_int(this->buffer_data_[DETECT_DISTANCE_LOW], this->buffer_data_[DETECT_DISTANCE_HIGH]); if (this->detection_distance_sensor_->get_state() != new_detect_distance) this->detection_distance_sensor_->publish_state(new_detect_distance); } @@ -388,7 +406,7 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) { for (std::vector::size_type i = 0; i != this->gate_move_sensors_.size(); i++) { sensor::Sensor *s = this->gate_move_sensors_[i]; if (s != nullptr) { - s->publish_state(buffer[MOVING_SENSOR_START + i]); + s->publish_state(this->buffer_data_[MOVING_SENSOR_START + i]); } } /* @@ -397,16 +415,17 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) { for (std::vector::size_type i = 0; i != this->gate_still_sensors_.size(); i++) { sensor::Sensor *s = this->gate_still_sensors_[i]; if (s != nullptr) { - s->publish_state(buffer[STILL_SENSOR_START + i]); + s->publish_state(this->buffer_data_[STILL_SENSOR_START + i]); } } /* Light sensor: 38th bytes */ if (this->light_sensor_ != nullptr) { - int new_light_sensor = buffer[LIGHT_SENSOR]; - if (this->light_sensor_->get_state() != new_light_sensor) + int new_light_sensor = this->buffer_data_[LIGHT_SENSOR]; + if (this->light_sensor_->get_state() != new_light_sensor) { this->light_sensor_->publish_state(new_light_sensor); + } } } else { for (auto *s : this->gate_move_sensors_) { @@ -427,7 +446,7 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) { #ifdef USE_BINARY_SENSOR if (engineering_mode) { if (this->out_pin_presence_status_binary_sensor_ != nullptr) { - this->out_pin_presence_status_binary_sensor_->publish_state(buffer[OUT_PIN_SENSOR] == 0x01); + this->out_pin_presence_status_binary_sensor_->publish_state(this->buffer_data_[OUT_PIN_SENSOR] == 0x01); } } else { if (this->out_pin_presence_status_binary_sensor_ != nullptr) { @@ -439,127 +458,149 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) { #ifdef USE_NUMBER std::function set_number_value(number::Number *n, float value) { - float normalized_value = value * 1.0; - if (n != nullptr && (!n->has_state() || n->state != normalized_value)) { - n->state = normalized_value; - return [n, normalized_value]() { n->publish_state(normalized_value); }; + if (n != nullptr && (!n->has_state() || n->state != value)) { + n->state = value; + return [n, value]() { n->publish_state(value); }; } return []() {}; } #endif -bool LD2410Component::handle_ack_data_(uint8_t *buffer, int len) { - ESP_LOGV(TAG, "Handling ACK DATA for COMMAND %02X", buffer[COMMAND]); - if (len < 10) { +bool LD2410Component::handle_ack_data_() { + ESP_LOGV(TAG, "Handling ACK DATA for COMMAND %02X", this->buffer_data_[COMMAND]); + if (this->buffer_pos_ < 10) { ESP_LOGE(TAG, "Invalid length"); return true; } - if (buffer[0] != 0xFD || buffer[1] != 0xFC || buffer[2] != 0xFB || buffer[3] != 0xFA) { // check 4 frame start bytes - ESP_LOGE(TAG, "Invalid header"); + if (!ld2410::validate_header_footer(CMD_FRAME_HEADER, this->buffer_data_)) { + ESP_LOGW(TAG, "Invalid header: %s", format_hex_pretty(this->buffer_data_, HEADER_FOOTER_SIZE).c_str()); return true; } - if (buffer[COMMAND_STATUS] != 0x01) { + if (this->buffer_data_[COMMAND_STATUS] != 0x01) { ESP_LOGE(TAG, "Invalid status"); return true; } - if (ld2410::two_byte_to_int(buffer[8], buffer[9]) != 0x00) { - ESP_LOGE(TAG, "Invalid command: %u, %u", buffer[8], buffer[9]); + if (ld2410::two_byte_to_int(this->buffer_data_[8], this->buffer_data_[9]) != 0x00) { + ESP_LOGW(TAG, "Invalid command: %02X, %02X", this->buffer_data_[8], this->buffer_data_[9]); return true; } - switch (buffer[COMMAND]) { - case lowbyte(CMD_ENABLE_CONF): + switch (this->buffer_data_[COMMAND]) { + case CMD_ENABLE_CONF: ESP_LOGV(TAG, "Enable conf"); break; - case lowbyte(CMD_DISABLE_CONF): + + case CMD_DISABLE_CONF: ESP_LOGV(TAG, "Disabled conf"); break; - case lowbyte(CMD_SET_BAUD_RATE): + + case CMD_SET_BAUD_RATE: ESP_LOGV(TAG, "Baud rate change"); #ifdef USE_SELECT if (this->baud_rate_select_ != nullptr) { - ESP_LOGE(TAG, "Configure baud rate to %s and reinstall", this->baud_rate_select_->state.c_str()); + ESP_LOGE(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->state.c_str()); } #endif break; - case lowbyte(CMD_VERSION): - this->version_ = str_sprintf(VERSION_FMT, buffer[13], buffer[12], buffer[17], buffer[16], buffer[15], buffer[14]); - ESP_LOGV(TAG, "Firmware version: %s", this->version_.c_str()); + + case CMD_QUERY_VERSION: { + std::memcpy(this->version_, &this->buffer_data_[12], sizeof(this->version_)); + std::string version = str_sprintf(VERSION_FMT, this->version_[1], this->version_[0], this->version_[5], + this->version_[4], this->version_[3], this->version_[2]); + ESP_LOGV(TAG, "Firmware version: %s", version.c_str()); #ifdef USE_TEXT_SENSOR if (this->version_text_sensor_ != nullptr) { - this->version_text_sensor_->publish_state(this->version_); + this->version_text_sensor_->publish_state(version); } #endif break; - case lowbyte(CMD_QUERY_DISTANCE_RESOLUTION): { - std::string distance_resolution = - find_str(DISTANCE_RESOLUTIONS_BY_UINT, ld2410::two_byte_to_int(buffer[10], buffer[11])); - ESP_LOGV(TAG, "Distance resolution: %s", distance_resolution.c_str()); + } + + case CMD_QUERY_DISTANCE_RESOLUTION: { + const auto *distance_resolution = find_str(DISTANCE_RESOLUTIONS_BY_UINT, this->buffer_data_[10]); + ESP_LOGV(TAG, "Distance resolution: %s", distance_resolution); #ifdef USE_SELECT - if (this->distance_resolution_select_ != nullptr && - this->distance_resolution_select_->state != distance_resolution) { + if (this->distance_resolution_select_ != nullptr) { this->distance_resolution_select_->publish_state(distance_resolution); } #endif - } break; - case lowbyte(CMD_QUERY_LIGHT_CONTROL): { - this->light_function_ = find_str(LIGHT_FUNCTIONS_BY_UINT, buffer[10]); - this->light_threshold_ = buffer[11] * 1.0; - this->out_pin_level_ = find_str(OUT_PIN_LEVELS_BY_UINT, buffer[12]); - ESP_LOGV(TAG, "Light function: %s", const_cast(this->light_function_.c_str())); - ESP_LOGV(TAG, "Light threshold: %f", this->light_threshold_); - ESP_LOGV(TAG, "Out pin level: %s", const_cast(this->out_pin_level_.c_str())); + break; + } + + case CMD_QUERY_LIGHT_CONTROL: { + this->light_function_ = this->buffer_data_[10]; + this->light_threshold_ = this->buffer_data_[11]; + this->out_pin_level_ = this->buffer_data_[12]; + const auto *light_function_str = find_str(LIGHT_FUNCTIONS_BY_UINT, this->light_function_); + const auto *out_pin_level_str = find_str(OUT_PIN_LEVELS_BY_UINT, this->out_pin_level_); + ESP_LOGV(TAG, + "Light function is: %s\n" + "Light threshold is: %u\n" + "Out pin level: %s", + light_function_str, this->light_threshold_, out_pin_level_str); #ifdef USE_SELECT - if (this->light_function_select_ != nullptr && this->light_function_select_->state != this->light_function_) { - this->light_function_select_->publish_state(this->light_function_); + if (this->light_function_select_ != nullptr) { + this->light_function_select_->publish_state(light_function_str); } - if (this->out_pin_level_select_ != nullptr && this->out_pin_level_select_->state != this->out_pin_level_) { - this->out_pin_level_select_->publish_state(this->out_pin_level_); + if (this->out_pin_level_select_ != nullptr) { + this->out_pin_level_select_->publish_state(out_pin_level_str); } #endif #ifdef USE_NUMBER - if (this->light_threshold_number_ != nullptr && - (!this->light_threshold_number_->has_state() || - this->light_threshold_number_->state != this->light_threshold_)) { - this->light_threshold_number_->publish_state(this->light_threshold_); + if (this->light_threshold_number_ != nullptr) { + this->light_threshold_number_->publish_state(static_cast(this->light_threshold_)); } #endif - } break; - case lowbyte(CMD_MAC): - if (len < 20) { + break; + } + case CMD_QUERY_MAC_ADDRESS: { + if (this->buffer_pos_ < 20) { return false; } - this->mac_ = format_mac_address_pretty(&buffer[10]); - ESP_LOGV(TAG, "MAC address: %s", this->mac_.c_str()); + + this->bluetooth_on_ = std::memcmp(&this->buffer_data_[10], NO_MAC, sizeof(NO_MAC)) != 0; + if (this->bluetooth_on_) { + std::memcpy(this->mac_address_, &this->buffer_data_[10], sizeof(this->mac_address_)); + } + + std::string mac_str = + mac_address_is_valid(this->mac_address_) ? format_mac_address_pretty(this->mac_address_) : UNKNOWN_MAC; + ESP_LOGV(TAG, "MAC address: %s", mac_str.c_str()); #ifdef USE_TEXT_SENSOR if (this->mac_text_sensor_ != nullptr) { - this->mac_text_sensor_->publish_state(this->mac_ == NO_MAC ? UNKNOWN_MAC : this->mac_); + this->mac_text_sensor_->publish_state(mac_str); } #endif #ifdef USE_SWITCH if (this->bluetooth_switch_ != nullptr) { - this->bluetooth_switch_->publish_state(this->mac_ != NO_MAC); + this->bluetooth_switch_->publish_state(this->bluetooth_on_); } #endif break; - case lowbyte(CMD_GATE_SENS): + } + + case CMD_GATE_SENS: ESP_LOGV(TAG, "Sensitivity"); break; - case lowbyte(CMD_BLUETOOTH): + + case CMD_BLUETOOTH: ESP_LOGV(TAG, "Bluetooth"); break; - case lowbyte(CMD_SET_DISTANCE_RESOLUTION): + + case CMD_SET_DISTANCE_RESOLUTION: ESP_LOGV(TAG, "Set distance resolution"); break; - case lowbyte(CMD_SET_LIGHT_CONTROL): + + case CMD_SET_LIGHT_CONTROL: ESP_LOGV(TAG, "Set light control"); break; - case lowbyte(CMD_BT_PASSWORD): + + case CMD_BT_PASSWORD: ESP_LOGV(TAG, "Set bluetooth password"); break; - case lowbyte(CMD_QUERY): // Query parameters response - { - if (buffer[10] != 0xAA) + + case CMD_QUERY: { // Query parameters response + if (this->buffer_data_[10] != 0xAA) return true; // value head=0xAA #ifdef USE_NUMBER /* @@ -567,29 +608,31 @@ bool LD2410Component::handle_ack_data_(uint8_t *buffer, int len) { Still distance range: 14th byte */ std::vector> updates; - updates.push_back(set_number_value(this->max_move_distance_gate_number_, buffer[12])); - updates.push_back(set_number_value(this->max_still_distance_gate_number_, buffer[13])); + updates.push_back(set_number_value(this->max_move_distance_gate_number_, this->buffer_data_[12])); + updates.push_back(set_number_value(this->max_still_distance_gate_number_, this->buffer_data_[13])); /* Moving Sensitivities: 15~23th bytes */ for (std::vector::size_type i = 0; i != this->gate_move_threshold_numbers_.size(); i++) { - updates.push_back(set_number_value(this->gate_move_threshold_numbers_[i], buffer[14 + i])); + updates.push_back(set_number_value(this->gate_move_threshold_numbers_[i], this->buffer_data_[14 + i])); } /* Still Sensitivities: 24~32th bytes */ for (std::vector::size_type i = 0; i != this->gate_still_threshold_numbers_.size(); i++) { - updates.push_back(set_number_value(this->gate_still_threshold_numbers_[i], buffer[23 + i])); + updates.push_back(set_number_value(this->gate_still_threshold_numbers_[i], this->buffer_data_[23 + i])); } /* None Duration: 33~34th bytes */ - updates.push_back(set_number_value(this->timeout_number_, ld2410::two_byte_to_int(buffer[32], buffer[33]))); + updates.push_back(set_number_value(this->timeout_number_, + ld2410::two_byte_to_int(this->buffer_data_[32], this->buffer_data_[33]))); for (auto &update : updates) { update(); } #endif - } break; + break; + } default: break; } @@ -597,59 +640,66 @@ bool LD2410Component::handle_ack_data_(uint8_t *buffer, int len) { return true; } -void LD2410Component::readline_(int readch, uint8_t *buffer, int len) { - static int pos = 0; +void LD2410Component::readline_(int readch) { + if (readch < 0) { + return; // No data available + } - if (readch >= 0) { - if (pos < len - 1) { - buffer[pos++] = readch; - buffer[pos] = 0; + if (this->buffer_pos_ < MAX_LINE_LENGTH - 1) { + this->buffer_data_[this->buffer_pos_++] = readch; + this->buffer_data_[this->buffer_pos_] = 0; + } else { + // We should never get here, but just in case... + ESP_LOGW(TAG, "Max command length exceeded; ignoring"); + this->buffer_pos_ = 0; + } + if (this->buffer_pos_ < 4) { + return; // Not enough data to process yet + } + if (this->buffer_data_[this->buffer_pos_ - 4] == DATA_FRAME_FOOTER[0] && + this->buffer_data_[this->buffer_pos_ - 3] == DATA_FRAME_FOOTER[1] && + this->buffer_data_[this->buffer_pos_ - 2] == DATA_FRAME_FOOTER[2] && + this->buffer_data_[this->buffer_pos_ - 1] == DATA_FRAME_FOOTER[3]) { + ESP_LOGV(TAG, "Handling Periodic Data: %s", format_hex_pretty(this->buffer_data_, this->buffer_pos_).c_str()); + this->handle_periodic_data_(); + this->buffer_pos_ = 0; // Reset position index for next message + } else if (this->buffer_data_[this->buffer_pos_ - 4] == CMD_FRAME_FOOTER[0] && + this->buffer_data_[this->buffer_pos_ - 3] == CMD_FRAME_FOOTER[1] && + this->buffer_data_[this->buffer_pos_ - 2] == CMD_FRAME_FOOTER[2] && + this->buffer_data_[this->buffer_pos_ - 1] == CMD_FRAME_FOOTER[3]) { + ESP_LOGV(TAG, "Handling Ack Data: %s", format_hex_pretty(this->buffer_data_, this->buffer_pos_).c_str()); + if (this->handle_ack_data_()) { + this->buffer_pos_ = 0; // Reset position index for next message } else { - pos = 0; - } - if (pos >= 4) { - if (buffer[pos - 4] == 0xF8 && buffer[pos - 3] == 0xF7 && buffer[pos - 2] == 0xF6 && buffer[pos - 1] == 0xF5) { - ESP_LOGV(TAG, "Will handle Periodic Data"); - this->handle_periodic_data_(buffer, pos); - pos = 0; // Reset position index ready for next time - } else if (buffer[pos - 4] == 0x04 && buffer[pos - 3] == 0x03 && buffer[pos - 2] == 0x02 && - buffer[pos - 1] == 0x01) { - ESP_LOGV(TAG, "Will handle ACK Data"); - if (this->handle_ack_data_(buffer, pos)) { - pos = 0; // Reset position index ready for next time - } else { - ESP_LOGV(TAG, "ACK Data incomplete"); - } - } + ESP_LOGV(TAG, "Ack Data incomplete"); } } } void LD2410Component::set_config_mode_(bool enable) { - uint8_t cmd = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF; - uint8_t cmd_value[2] = {0x01, 0x00}; - this->send_command_(cmd, enable ? cmd_value : nullptr, 2); + const uint8_t cmd = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF; + const uint8_t cmd_value[2] = {0x01, 0x00}; + this->send_command_(cmd, enable ? cmd_value : nullptr, sizeof(cmd_value)); } void LD2410Component::set_bluetooth(bool enable) { this->set_config_mode_(true); - uint8_t enable_cmd_value[2] = {0x01, 0x00}; - uint8_t disable_cmd_value[2] = {0x00, 0x00}; - this->send_command_(CMD_BLUETOOTH, enable ? enable_cmd_value : disable_cmd_value, 2); + const uint8_t cmd_value[2] = {enable ? (uint8_t) 0x01 : (uint8_t) 0x00, 0x00}; + this->send_command_(CMD_BLUETOOTH, cmd_value, sizeof(cmd_value)); this->set_timeout(200, [this]() { this->restart_and_read_all_info(); }); } void LD2410Component::set_distance_resolution(const std::string &state) { this->set_config_mode_(true); - uint8_t cmd_value[2] = {find_uint8(DISTANCE_RESOLUTIONS_BY_STR, state), 0x00}; - this->send_command_(CMD_SET_DISTANCE_RESOLUTION, cmd_value, 2); + const uint8_t cmd_value[2] = {find_uint8(DISTANCE_RESOLUTIONS_BY_STR, state), 0x00}; + this->send_command_(CMD_SET_DISTANCE_RESOLUTION, cmd_value, sizeof(cmd_value)); this->set_timeout(200, [this]() { this->restart_and_read_all_info(); }); } void LD2410Component::set_baud_rate(const std::string &state) { this->set_config_mode_(true); - uint8_t cmd_value[2] = {find_uint8(BAUD_RATES_BY_STR, state), 0x00}; - this->send_command_(CMD_SET_BAUD_RATE, cmd_value, 2); + const uint8_t cmd_value[2] = {find_uint8(BAUD_RATES_BY_STR, state), 0x00}; + this->send_command_(CMD_SET_BAUD_RATE, cmd_value, sizeof(cmd_value)); this->set_timeout(200, [this]() { this->restart_(); }); } @@ -661,14 +711,13 @@ void LD2410Component::set_bluetooth_password(const std::string &password) { this->set_config_mode_(true); uint8_t cmd_value[6]; std::copy(password.begin(), password.end(), std::begin(cmd_value)); - this->send_command_(CMD_BT_PASSWORD, cmd_value, 6); + this->send_command_(CMD_BT_PASSWORD, cmd_value, sizeof(cmd_value)); this->set_config_mode_(false); } void LD2410Component::set_engineering_mode(bool enable) { + const uint8_t cmd = enable ? CMD_ENABLE_ENG : CMD_DISABLE_ENG; this->set_config_mode_(true); - last_engineering_mode_change_millis_ = App.get_loop_component_start_time(); - uint8_t cmd = enable ? CMD_ENABLE_ENG : CMD_DISABLE_ENG; this->send_command_(cmd, nullptr, 0); this->set_config_mode_(false); } @@ -682,14 +731,17 @@ void LD2410Component::factory_reset() { void LD2410Component::restart_() { this->send_command_(CMD_RESTART, nullptr, 0); } void LD2410Component::query_parameters_() { this->send_command_(CMD_QUERY, nullptr, 0); } -void LD2410Component::get_version_() { this->send_command_(CMD_VERSION, nullptr, 0); } + +void LD2410Component::get_version_() { this->send_command_(CMD_QUERY_VERSION, nullptr, 0); } + void LD2410Component::get_mac_() { - uint8_t cmd_value[2] = {0x01, 0x00}; - this->send_command_(CMD_MAC, cmd_value, 2); + const uint8_t cmd_value[2] = {0x01, 0x00}; + this->send_command_(CMD_QUERY_MAC_ADDRESS, cmd_value, sizeof(cmd_value)); } + void LD2410Component::get_distance_resolution_() { this->send_command_(CMD_QUERY_DISTANCE_RESOLUTION, nullptr, 0); } -void LD2410Component::get_light_control_() { this->send_command_(CMD_QUERY_LIGHT_CONTROL, nullptr, 0); } +void LD2410Component::query_light_control_() { this->send_command_(CMD_QUERY_LIGHT_CONTROL, nullptr, 0); } #ifdef USE_NUMBER void LD2410Component::set_max_distances_timeout() { @@ -719,7 +771,7 @@ void LD2410Component::set_max_distances_timeout() { 0x00, 0x00}; this->set_config_mode_(true); - this->send_command_(CMD_MAXDIST_DURATION, value, 18); + this->send_command_(CMD_MAXDIST_DURATION, value, sizeof(value)); delay(50); // NOLINT this->query_parameters_(); this->set_timeout(200, [this]() { this->restart_and_read_all_info(); }); @@ -749,17 +801,17 @@ void LD2410Component::set_gate_threshold(uint8_t gate) { uint8_t value[18] = {0x00, 0x00, lowbyte(gate), highbyte(gate), 0x00, 0x00, 0x01, 0x00, lowbyte(motion), highbyte(motion), 0x00, 0x00, 0x02, 0x00, lowbyte(still), highbyte(still), 0x00, 0x00}; - this->send_command_(CMD_GATE_SENS, value, 18); + this->send_command_(CMD_GATE_SENS, value, sizeof(value)); delay(50); // NOLINT this->query_parameters_(); this->set_config_mode_(false); } -void LD2410Component::set_gate_still_threshold_number(int gate, number::Number *n) { +void LD2410Component::set_gate_still_threshold_number(uint8_t gate, number::Number *n) { this->gate_still_threshold_numbers_[gate] = n; } -void LD2410Component::set_gate_move_threshold_number(int gate, number::Number *n) { +void LD2410Component::set_gate_move_threshold_number(uint8_t gate, number::Number *n) { this->gate_move_threshold_numbers_[gate] = n; } #endif @@ -767,35 +819,29 @@ void LD2410Component::set_gate_move_threshold_number(int gate, number::Number *n void LD2410Component::set_light_out_control() { #ifdef USE_NUMBER if (this->light_threshold_number_ != nullptr && this->light_threshold_number_->has_state()) { - this->light_threshold_ = this->light_threshold_number_->state; + this->light_threshold_ = static_cast(this->light_threshold_number_->state); } #endif #ifdef USE_SELECT if (this->light_function_select_ != nullptr && this->light_function_select_->has_state()) { - this->light_function_ = this->light_function_select_->state; + this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->state); } if (this->out_pin_level_select_ != nullptr && this->out_pin_level_select_->has_state()) { - this->out_pin_level_ = this->out_pin_level_select_->state; + this->out_pin_level_ = find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->state); } #endif - if (this->light_function_.empty() || this->out_pin_level_.empty() || this->light_threshold_ < 0) { - return; - } this->set_config_mode_(true); - uint8_t light_function = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_); - uint8_t light_threshold = static_cast(this->light_threshold_); - uint8_t out_pin_level = find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_); - uint8_t value[4] = {light_function, light_threshold, out_pin_level, 0x00}; - this->send_command_(CMD_SET_LIGHT_CONTROL, value, 4); + uint8_t value[4] = {this->light_function_, this->light_threshold_, this->out_pin_level_, 0x00}; + this->send_command_(CMD_SET_LIGHT_CONTROL, value, sizeof(value)); delay(50); // NOLINT - this->get_light_control_(); + this->query_light_control_(); this->set_timeout(200, [this]() { this->restart_and_read_all_info(); }); this->set_config_mode_(false); } #ifdef USE_SENSOR -void LD2410Component::set_gate_move_sensor(int gate, sensor::Sensor *s) { this->gate_move_sensors_[gate] = s; } -void LD2410Component::set_gate_still_sensor(int gate, sensor::Sensor *s) { this->gate_still_sensors_[gate] = s; } +void LD2410Component::set_gate_move_sensor(uint8_t gate, sensor::Sensor *s) { this->gate_move_sensors_[gate] = s; } +void LD2410Component::set_gate_still_sensor(uint8_t gate, sensor::Sensor *s) { this->gate_still_sensors_[gate] = s; } #endif } // namespace ld2410 diff --git a/esphome/components/ld2410/ld2410.h b/esphome/components/ld2410/ld2410.h index 1b5f6e3057..8bd1dbcb5a 100644 --- a/esphome/components/ld2410/ld2410.h +++ b/esphome/components/ld2410/ld2410.h @@ -29,45 +29,48 @@ namespace esphome { namespace ld2410 { +static const uint8_t MAX_LINE_LENGTH = 46; // Max characters for serial buffer +static const uint8_t TOTAL_GATES = 9; // Total number of gates supported by the LD2410 + class LD2410Component : public Component, public uart::UARTDevice { -#ifdef USE_SENSOR - SUB_SENSOR(moving_target_distance) - SUB_SENSOR(still_target_distance) - SUB_SENSOR(moving_target_energy) - SUB_SENSOR(still_target_energy) - SUB_SENSOR(light) - SUB_SENSOR(detection_distance) -#endif #ifdef USE_BINARY_SENSOR - SUB_BINARY_SENSOR(target) + SUB_BINARY_SENSOR(out_pin_presence_status) SUB_BINARY_SENSOR(moving_target) SUB_BINARY_SENSOR(still_target) - SUB_BINARY_SENSOR(out_pin_presence_status) + SUB_BINARY_SENSOR(target) +#endif +#ifdef USE_SENSOR + SUB_SENSOR(light) + SUB_SENSOR(detection_distance) + SUB_SENSOR(moving_target_distance) + SUB_SENSOR(moving_target_energy) + SUB_SENSOR(still_target_distance) + SUB_SENSOR(still_target_energy) #endif #ifdef USE_TEXT_SENSOR SUB_TEXT_SENSOR(version) SUB_TEXT_SENSOR(mac) #endif +#ifdef USE_NUMBER + SUB_NUMBER(light_threshold) + SUB_NUMBER(max_move_distance_gate) + SUB_NUMBER(max_still_distance_gate) + SUB_NUMBER(timeout) +#endif #ifdef USE_SELECT - SUB_SELECT(distance_resolution) SUB_SELECT(baud_rate) + SUB_SELECT(distance_resolution) SUB_SELECT(light_function) SUB_SELECT(out_pin_level) #endif #ifdef USE_SWITCH - SUB_SWITCH(engineering_mode) SUB_SWITCH(bluetooth) + SUB_SWITCH(engineering_mode) #endif #ifdef USE_BUTTON - SUB_BUTTON(reset) - SUB_BUTTON(restart) + SUB_BUTTON(factory_reset) SUB_BUTTON(query) -#endif -#ifdef USE_NUMBER - SUB_NUMBER(max_still_distance_gate) - SUB_NUMBER(max_move_distance_gate) - SUB_NUMBER(timeout) - SUB_NUMBER(light_threshold) + SUB_BUTTON(restart) #endif public: @@ -76,14 +79,14 @@ class LD2410Component : public Component, public uart::UARTDevice { void loop() override; void set_light_out_control(); #ifdef USE_NUMBER - void set_gate_still_threshold_number(int gate, number::Number *n); - void set_gate_move_threshold_number(int gate, number::Number *n); + void set_gate_still_threshold_number(uint8_t gate, number::Number *n); + void set_gate_move_threshold_number(uint8_t gate, number::Number *n); void set_max_distances_timeout(); void set_gate_threshold(uint8_t gate); #endif #ifdef USE_SENSOR - void set_gate_move_sensor(int gate, sensor::Sensor *s); - void set_gate_still_sensor(int gate, sensor::Sensor *s); + void set_gate_move_sensor(uint8_t gate, sensor::Sensor *s); + void set_gate_still_sensor(uint8_t gate, sensor::Sensor *s); #endif void set_throttle(uint16_t value) { this->throttle_ = value; }; void set_bluetooth_password(const std::string &password); @@ -96,33 +99,35 @@ class LD2410Component : public Component, public uart::UARTDevice { void factory_reset(); protected: - void send_command_(uint8_t command_str, const uint8_t *command_value, int command_value_len); + void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len); void set_config_mode_(bool enable); - void handle_periodic_data_(uint8_t *buffer, int len); - bool handle_ack_data_(uint8_t *buffer, int len); - void readline_(int readch, uint8_t *buffer, int len); + void handle_periodic_data_(); + bool handle_ack_data_(); + void readline_(int readch); void query_parameters_(); void get_version_(); void get_mac_(); void get_distance_resolution_(); - void get_light_control_(); + void query_light_control_(); void restart_(); - int32_t last_periodic_millis_ = 0; - int32_t last_engineering_mode_change_millis_ = 0; - uint16_t throttle_; - float light_threshold_ = -1; - std::string version_; - std::string mac_; - std::string out_pin_level_; - std::string light_function_; + uint32_t last_periodic_millis_ = 0; + uint16_t throttle_ = 0; + uint8_t light_function_ = 0; + uint8_t light_threshold_ = 0; + uint8_t out_pin_level_ = 0; + uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer + uint8_t buffer_data_[MAX_LINE_LENGTH]; + uint8_t mac_address_[6] = {0, 0, 0, 0, 0, 0}; + uint8_t version_[6] = {0, 0, 0, 0, 0, 0}; + bool bluetooth_on_{false}; #ifdef USE_NUMBER - std::vector gate_still_threshold_numbers_ = std::vector(9); - std::vector gate_move_threshold_numbers_ = std::vector(9); + std::vector gate_move_threshold_numbers_ = std::vector(TOTAL_GATES); + std::vector gate_still_threshold_numbers_ = std::vector(TOTAL_GATES); #endif #ifdef USE_SENSOR - std::vector gate_still_sensors_ = std::vector(9); - std::vector gate_move_sensors_ = std::vector(9); + std::vector gate_move_sensors_ = std::vector(TOTAL_GATES); + std::vector gate_still_sensors_ = std::vector(TOTAL_GATES); #endif }; diff --git a/esphome/components/ld2450/button/__init__.py b/esphome/components/ld2450/button/__init__.py index 39671d3a3b..429aa59389 100644 --- a/esphome/components/ld2450/button/__init__.py +++ b/esphome/components/ld2450/button/__init__.py @@ -13,13 +13,13 @@ from esphome.const import ( from .. import CONF_LD2450_ID, LD2450Component, ld2450_ns -ResetButton = ld2450_ns.class_("ResetButton", button.Button) +FactoryResetButton = ld2450_ns.class_("FactoryResetButton", button.Button) RestartButton = ld2450_ns.class_("RestartButton", button.Button) CONFIG_SCHEMA = { cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), cv.Optional(CONF_FACTORY_RESET): button.button_schema( - ResetButton, + FactoryResetButton, device_class=DEVICE_CLASS_RESTART, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_RESTART_ALERT, @@ -38,7 +38,7 @@ async def to_code(config): if factory_reset_config := config.get(CONF_FACTORY_RESET): b = await button.new_button(factory_reset_config) await cg.register_parented(b, config[CONF_LD2450_ID]) - cg.add(ld2450_component.set_reset_button(b)) + cg.add(ld2450_component.set_factory_reset_button(b)) if restart_config := config.get(CONF_RESTART): b = await button.new_button(restart_config) await cg.register_parented(b, config[CONF_LD2450_ID]) diff --git a/esphome/components/ld2450/button/factory_reset_button.cpp b/esphome/components/ld2450/button/factory_reset_button.cpp new file mode 100644 index 0000000000..bcac7ada2f --- /dev/null +++ b/esphome/components/ld2450/button/factory_reset_button.cpp @@ -0,0 +1,9 @@ +#include "factory_reset_button.h" + +namespace esphome { +namespace ld2450 { + +void FactoryResetButton::press_action() { this->parent_->factory_reset(); } + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/button/reset_button.h b/esphome/components/ld2450/button/factory_reset_button.h similarity index 65% rename from esphome/components/ld2450/button/reset_button.h rename to esphome/components/ld2450/button/factory_reset_button.h index 73804fa6d6..8e80347119 100644 --- a/esphome/components/ld2450/button/reset_button.h +++ b/esphome/components/ld2450/button/factory_reset_button.h @@ -6,9 +6,9 @@ namespace esphome { namespace ld2450 { -class ResetButton : public button::Button, public Parented { +class FactoryResetButton : public button::Button, public Parented { public: - ResetButton() = default; + FactoryResetButton() = default; protected: void press_action() override; diff --git a/esphome/components/ld2450/button/reset_button.cpp b/esphome/components/ld2450/button/reset_button.cpp deleted file mode 100644 index e96ec99cc5..0000000000 --- a/esphome/components/ld2450/button/reset_button.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "reset_button.h" - -namespace esphome { -namespace ld2450 { - -void ResetButton::press_action() { this->parent_->factory_reset(); } - -} // namespace ld2450 -} // namespace esphome diff --git a/esphome/components/ld2450/ld2450.cpp b/esphome/components/ld2450/ld2450.cpp index 0e1123db1a..8f3b3a3f21 100644 --- a/esphome/components/ld2450/ld2450.cpp +++ b/esphome/components/ld2450/ld2450.cpp @@ -1,5 +1,6 @@ #include "ld2450.h" #include +#include #ifdef USE_NUMBER #include "esphome/components/number/number.h" #endif @@ -17,11 +18,10 @@ namespace esphome { namespace ld2450 { static const char *const TAG = "ld2450"; -static const char *const NO_MAC = "08:05:04:03:02:01"; static const char *const UNKNOWN_MAC = "unknown"; static const char *const VERSION_FMT = "%u.%02X.%02X%02X%02X%02X"; -enum BaudRateStructure : uint8_t { +enum BaudRate : uint8_t { BAUD_RATE_9600 = 1, BAUD_RATE_19200 = 2, BAUD_RATE_38400 = 3, @@ -32,14 +32,13 @@ enum BaudRateStructure : uint8_t { BAUD_RATE_460800 = 8 }; -// Zone type struct -enum ZoneTypeStructure : uint8_t { +enum ZoneType : uint8_t { ZONE_DISABLED = 0, ZONE_DETECTION = 1, ZONE_FILTER = 2, }; -enum PeriodicDataStructure : uint8_t { +enum PeriodicData : uint8_t { TARGET_X = 4, TARGET_Y = 6, TARGET_SPEED = 8, @@ -47,12 +46,12 @@ enum PeriodicDataStructure : uint8_t { }; enum PeriodicDataValue : uint8_t { - HEAD = 0xAA, - END = 0x55, + HEADER = 0xAA, + FOOTER = 0x55, CHECK = 0x00, }; -enum AckDataStructure : uint8_t { +enum AckData : uint8_t { COMMAND = 6, COMMAND_STATUS = 7, }; @@ -60,11 +59,11 @@ enum AckDataStructure : uint8_t { // Memory-efficient lookup tables struct StringToUint8 { const char *str; - uint8_t value; + const uint8_t value; }; struct Uint8ToString { - uint8_t value; + const uint8_t value; const char *str; }; @@ -74,6 +73,13 @@ constexpr StringToUint8 BAUD_RATES_BY_STR[] = { {"256000", BAUD_RATE_256000}, {"460800", BAUD_RATE_460800}, }; +constexpr Uint8ToString DIRECTION_BY_UINT[] = { + {DIRECTION_APPROACHING, "Approaching"}, + {DIRECTION_MOVING_AWAY, "Moving away"}, + {DIRECTION_STATIONARY, "Stationary"}, + {DIRECTION_NA, "NA"}, +}; + constexpr Uint8ToString ZONE_TYPE_BY_UINT[] = { {ZONE_DISABLED, "Disabled"}, {ZONE_DETECTION, "Detection"}, @@ -103,36 +109,38 @@ template const char *find_str(const Uint8ToString (&arr)[N], uint8_t v return ""; // Not found } -// LD2450 serial command header & footer -static const uint8_t CMD_FRAME_HEADER[4] = {0xFD, 0xFC, 0xFB, 0xFA}; -static const uint8_t CMD_FRAME_END[4] = {0x04, 0x03, 0x02, 0x01}; // LD2450 UART Serial Commands -static const uint8_t CMD_ENABLE_CONF = 0xFF; -static const uint8_t CMD_DISABLE_CONF = 0xFE; -static const uint8_t CMD_VERSION = 0xA0; -static const uint8_t CMD_MAC = 0xA5; -static const uint8_t CMD_RESET = 0xA2; -static const uint8_t CMD_RESTART = 0xA3; -static const uint8_t CMD_BLUETOOTH = 0xA4; -static const uint8_t CMD_SINGLE_TARGET_MODE = 0x80; -static const uint8_t CMD_MULTI_TARGET_MODE = 0x90; -static const uint8_t CMD_QUERY_TARGET_MODE = 0x91; -static const uint8_t CMD_SET_BAUD_RATE = 0xA1; -static const uint8_t CMD_QUERY_ZONE = 0xC1; -static const uint8_t CMD_SET_ZONE = 0xC2; +static constexpr uint8_t CMD_ENABLE_CONF = 0xFF; +static constexpr uint8_t CMD_DISABLE_CONF = 0xFE; +static constexpr uint8_t CMD_QUERY_VERSION = 0xA0; +static constexpr uint8_t CMD_QUERY_MAC_ADDRESS = 0xA5; +static constexpr uint8_t CMD_RESET = 0xA2; +static constexpr uint8_t CMD_RESTART = 0xA3; +static constexpr uint8_t CMD_BLUETOOTH = 0xA4; +static constexpr uint8_t CMD_SINGLE_TARGET_MODE = 0x80; +static constexpr uint8_t CMD_MULTI_TARGET_MODE = 0x90; +static constexpr uint8_t CMD_QUERY_TARGET_MODE = 0x91; +static constexpr uint8_t CMD_SET_BAUD_RATE = 0xA1; +static constexpr uint8_t CMD_QUERY_ZONE = 0xC1; +static constexpr uint8_t CMD_SET_ZONE = 0xC2; +// Header & Footer size +static constexpr uint8_t HEADER_FOOTER_SIZE = 4; +// Command Header & Footer +static constexpr uint8_t CMD_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xFD, 0xFC, 0xFB, 0xFA}; +static constexpr uint8_t CMD_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0x04, 0x03, 0x02, 0x01}; +// Data Header & Footer +static constexpr uint8_t DATA_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xAA, 0xFF, 0x03, 0x00}; +static constexpr uint8_t DATA_FRAME_FOOTER[2] = {0x55, 0xCC}; +// MAC address the module uses when Bluetooth is disabled +static constexpr uint8_t NO_MAC[] = {0x08, 0x05, 0x04, 0x03, 0x02, 0x01}; static inline uint16_t convert_seconds_to_ms(uint16_t value) { return value * 1000; }; -static inline std::string convert_signed_int_to_hex(int value) { - auto value_as_str = str_snprintf("%04x", 4, value & 0xFFFF); - return value_as_str; -} - static inline void convert_int_values_to_hex(const int *values, uint8_t *bytes) { - for (int i = 0; i < 4; i++) { - std::string temp_hex = convert_signed_int_to_hex(values[i]); - bytes[i * 2] = std::stoi(temp_hex.substr(2, 2), nullptr, 16); // Store high byte - bytes[i * 2 + 1] = std::stoi(temp_hex.substr(0, 2), nullptr, 16); // Store low byte + for (uint8_t i = 0; i < 4; i++) { + uint16_t val = values[i] & 0xFFFF; + bytes[i * 2] = val & 0xFF; // Store low byte first (little-endian) + bytes[i * 2 + 1] = (val >> 8) & 0xFF; // Store high byte second } } @@ -170,18 +178,13 @@ static inline float calculate_angle(float base, float hypotenuse) { return angle_degrees; } -static inline std::string get_direction(int16_t speed) { - static const char *const APPROACHING = "Approaching"; - static const char *const MOVING_AWAY = "Moving away"; - static const char *const STATIONARY = "Stationary"; - - if (speed > 0) { - return MOVING_AWAY; +static bool validate_header_footer(const uint8_t *header_footer, const uint8_t *buffer) { + for (uint8_t i = 0; i < HEADER_FOOTER_SIZE; i++) { + if (header_footer[i] != buffer[i]) { + return false; // Mismatch in header/footer + } } - if (speed < 0) { - return APPROACHING; - } - return STATIONARY; + return true; // Valid header/footer } void LD2450Component::setup() { @@ -196,84 +199,93 @@ void LD2450Component::setup() { } void LD2450Component::dump_config() { - ESP_LOGCONFIG(TAG, "LD2450:"); + std::string mac_str = + mac_address_is_valid(this->mac_address_) ? format_mac_address_pretty(this->mac_address_) : UNKNOWN_MAC; + std::string version = str_sprintf(VERSION_FMT, this->version_[1], this->version_[0], this->version_[5], + this->version_[4], this->version_[3], this->version_[2]); + ESP_LOGCONFIG(TAG, + "LD2450:\n" + " Firmware version: %s\n" + " MAC address: %s\n" + " Throttle: %u ms", + version.c_str(), mac_str.c_str(), this->throttle_); #ifdef USE_BINARY_SENSOR - LOG_BINARY_SENSOR(" ", "TargetBinarySensor", this->target_binary_sensor_); - LOG_BINARY_SENSOR(" ", "MovingTargetBinarySensor", this->moving_target_binary_sensor_); - LOG_BINARY_SENSOR(" ", "StillTargetBinarySensor", this->still_target_binary_sensor_); -#endif -#ifdef USE_SWITCH - LOG_SWITCH(" ", "BluetoothSwitch", this->bluetooth_switch_); - LOG_SWITCH(" ", "MultiTargetSwitch", this->multi_target_switch_); -#endif -#ifdef USE_BUTTON - LOG_BUTTON(" ", "ResetButton", this->reset_button_); - LOG_BUTTON(" ", "RestartButton", this->restart_button_); + ESP_LOGCONFIG(TAG, "Binary Sensors:"); + LOG_BINARY_SENSOR(" ", "MovingTarget", this->moving_target_binary_sensor_); + LOG_BINARY_SENSOR(" ", "StillTarget", this->still_target_binary_sensor_); + LOG_BINARY_SENSOR(" ", "Target", this->target_binary_sensor_); #endif #ifdef USE_SENSOR - LOG_SENSOR(" ", "TargetCountSensor", this->target_count_sensor_); - LOG_SENSOR(" ", "StillTargetCountSensor", this->still_target_count_sensor_); - LOG_SENSOR(" ", "MovingTargetCountSensor", this->moving_target_count_sensor_); + ESP_LOGCONFIG(TAG, "Sensors:"); + LOG_SENSOR(" ", "MovingTargetCount", this->moving_target_count_sensor_); + LOG_SENSOR(" ", "StillTargetCount", this->still_target_count_sensor_); + LOG_SENSOR(" ", "TargetCount", this->target_count_sensor_); for (sensor::Sensor *s : this->move_x_sensors_) { - LOG_SENSOR(" ", "NthTargetXSensor", s); + LOG_SENSOR(" ", "TargetX", s); } for (sensor::Sensor *s : this->move_y_sensors_) { - LOG_SENSOR(" ", "NthTargetYSensor", s); - } - for (sensor::Sensor *s : this->move_speed_sensors_) { - LOG_SENSOR(" ", "NthTargetSpeedSensor", s); + LOG_SENSOR(" ", "TargetY", s); } for (sensor::Sensor *s : this->move_angle_sensors_) { - LOG_SENSOR(" ", "NthTargetAngleSensor", s); + LOG_SENSOR(" ", "TargetAngle", s); } for (sensor::Sensor *s : this->move_distance_sensors_) { - LOG_SENSOR(" ", "NthTargetDistanceSensor", s); + LOG_SENSOR(" ", "TargetDistance", s); } for (sensor::Sensor *s : this->move_resolution_sensors_) { - LOG_SENSOR(" ", "NthTargetResolutionSensor", s); + LOG_SENSOR(" ", "TargetResolution", s); + } + for (sensor::Sensor *s : this->move_speed_sensors_) { + LOG_SENSOR(" ", "TargetSpeed", s); } for (sensor::Sensor *s : this->zone_target_count_sensors_) { - LOG_SENSOR(" ", "NthZoneTargetCountSensor", s); - } - for (sensor::Sensor *s : this->zone_still_target_count_sensors_) { - LOG_SENSOR(" ", "NthZoneStillTargetCountSensor", s); + LOG_SENSOR(" ", "ZoneTargetCount", s); } for (sensor::Sensor *s : this->zone_moving_target_count_sensors_) { - LOG_SENSOR(" ", "NthZoneMovingTargetCountSensor", s); + LOG_SENSOR(" ", "ZoneMovingTargetCount", s); + } + for (sensor::Sensor *s : this->zone_still_target_count_sensors_) { + LOG_SENSOR(" ", "ZoneStillTargetCount", s); } #endif #ifdef USE_TEXT_SENSOR - LOG_TEXT_SENSOR(" ", "VersionTextSensor", this->version_text_sensor_); - LOG_TEXT_SENSOR(" ", "MacTextSensor", this->mac_text_sensor_); + ESP_LOGCONFIG(TAG, "Text Sensors:"); + LOG_TEXT_SENSOR(" ", "Version", this->version_text_sensor_); + LOG_TEXT_SENSOR(" ", "Mac", this->mac_text_sensor_); for (text_sensor::TextSensor *s : this->direction_text_sensors_) { - LOG_TEXT_SENSOR(" ", "NthDirectionTextSensor", s); + LOG_TEXT_SENSOR(" ", "Direction", s); } #endif #ifdef USE_NUMBER + ESP_LOGCONFIG(TAG, "Numbers:"); + LOG_NUMBER(" ", "PresenceTimeout", this->presence_timeout_number_); for (auto n : this->zone_numbers_) { - LOG_NUMBER(" ", "ZoneX1Number", n.x1); - LOG_NUMBER(" ", "ZoneY1Number", n.y1); - LOG_NUMBER(" ", "ZoneX2Number", n.x2); - LOG_NUMBER(" ", "ZoneY2Number", n.y2); + LOG_NUMBER(" ", "ZoneX1", n.x1); + LOG_NUMBER(" ", "ZoneY1", n.y1); + LOG_NUMBER(" ", "ZoneX2", n.x2); + LOG_NUMBER(" ", "ZoneY2", n.y2); } #endif #ifdef USE_SELECT - LOG_SELECT(" ", "BaudRateSelect", this->baud_rate_select_); - LOG_SELECT(" ", "ZoneTypeSelect", this->zone_type_select_); + ESP_LOGCONFIG(TAG, "Selects:"); + LOG_SELECT(" ", "BaudRate", this->baud_rate_select_); + LOG_SELECT(" ", "ZoneType", this->zone_type_select_); #endif -#ifdef USE_NUMBER - LOG_NUMBER(" ", "PresenceTimeoutNumber", this->presence_timeout_number_); +#ifdef USE_SWITCH + ESP_LOGCONFIG(TAG, "Switches:"); + LOG_SWITCH(" ", "Bluetooth", this->bluetooth_switch_); + LOG_SWITCH(" ", "MultiTarget", this->multi_target_switch_); +#endif +#ifdef USE_BUTTON + ESP_LOGCONFIG(TAG, "Buttons:"); + LOG_BUTTON(" ", "FactoryReset", this->factory_reset_button_); + LOG_BUTTON(" ", "Restart", this->restart_button_); #endif - ESP_LOGCONFIG(TAG, - " Throttle: %ums\n" - " MAC Address: %s\n" - " Firmware version: %s", - this->throttle_, this->mac_ == NO_MAC ? UNKNOWN_MAC : this->mac_.c_str(), this->version_.c_str()); } void LD2450Component::loop() { while (this->available()) { - this->readline_(read(), this->buffer_data_, MAX_LINE_LENGTH); + this->readline_(this->read()); } } @@ -308,7 +320,7 @@ void LD2450Component::set_radar_zone(int32_t zone_type, int32_t zone1_x1, int32_ this->zone_type_ = zone_type; int zone_parameters[12] = {zone1_x1, zone1_y1, zone1_x2, zone1_y2, zone2_x1, zone2_y1, zone2_x2, zone2_y2, zone3_x1, zone3_y1, zone3_x2, zone3_y2}; - for (int i = 0; i < MAX_ZONES; i++) { + for (uint8_t i = 0; i < MAX_ZONES; i++) { this->zone_config_[i].x1 = zone_parameters[i * 4]; this->zone_config_[i].y1 = zone_parameters[i * 4 + 1]; this->zone_config_[i].x2 = zone_parameters[i * 4 + 2]; @@ -322,15 +334,15 @@ void LD2450Component::send_set_zone_command_() { uint8_t cmd_value[26] = {}; uint8_t zone_type_bytes[2] = {static_cast(this->zone_type_), 0x00}; uint8_t area_config[24] = {}; - for (int i = 0; i < MAX_ZONES; i++) { + for (uint8_t i = 0; i < MAX_ZONES; i++) { int values[4] = {this->zone_config_[i].x1, this->zone_config_[i].y1, this->zone_config_[i].x2, this->zone_config_[i].y2}; ld2450::convert_int_values_to_hex(values, area_config + (i * 8)); } - std::memcpy(cmd_value, zone_type_bytes, 2); - std::memcpy(cmd_value + 2, area_config, 24); + std::memcpy(cmd_value, zone_type_bytes, sizeof(zone_type_bytes)); + std::memcpy(cmd_value + 2, area_config, sizeof(area_config)); this->set_config_mode_(true); - this->send_command_(CMD_SET_ZONE, cmd_value, 26); + this->send_command_(CMD_SET_ZONE, cmd_value, sizeof(cmd_value)); this->set_config_mode_(false); } @@ -346,14 +358,14 @@ bool LD2450Component::get_timeout_status_(uint32_t check_millis) { } // Extract, store and publish zone details LD2450 buffer -void LD2450Component::process_zone_(uint8_t *buffer) { +void LD2450Component::process_zone_() { uint8_t index, start; for (index = 0; index < MAX_ZONES; index++) { start = 12 + index * 8; - this->zone_config_[index].x1 = ld2450::hex_to_signed_int(buffer, start); - this->zone_config_[index].y1 = ld2450::hex_to_signed_int(buffer, start + 2); - this->zone_config_[index].x2 = ld2450::hex_to_signed_int(buffer, start + 4); - this->zone_config_[index].y2 = ld2450::hex_to_signed_int(buffer, start + 6); + this->zone_config_[index].x1 = ld2450::hex_to_signed_int(this->buffer_data_, start); + this->zone_config_[index].y1 = ld2450::hex_to_signed_int(this->buffer_data_, start + 2); + this->zone_config_[index].x2 = ld2450::hex_to_signed_int(this->buffer_data_, start + 4); + this->zone_config_[index].y2 = ld2450::hex_to_signed_int(this->buffer_data_, start + 6); #ifdef USE_NUMBER // only one null check as all coordinates are required for a single zone if (this->zone_numbers_[index].x1 != nullptr) { @@ -399,27 +411,25 @@ void LD2450Component::restart_and_read_all_info() { // Send command with values to LD2450 void LD2450Component::send_command_(uint8_t command, const uint8_t *command_value, uint8_t command_value_len) { - ESP_LOGV(TAG, "Sending command %02X", command); - // frame header - this->write_array(CMD_FRAME_HEADER, 4); + ESP_LOGV(TAG, "Sending COMMAND %02X", command); + // frame header bytes + this->write_array(CMD_FRAME_HEADER, sizeof(CMD_FRAME_HEADER)); // length bytes - int len = 2; + uint8_t len = 2; if (command_value != nullptr) { len += command_value_len; } - this->write_byte(lowbyte(len)); - this->write_byte(highbyte(len)); - // command - this->write_byte(lowbyte(command)); - this->write_byte(highbyte(command)); + uint8_t len_cmd[] = {lowbyte(len), highbyte(len), command, 0x00}; + this->write_array(len_cmd, sizeof(len_cmd)); + // command value bytes if (command_value != nullptr) { - for (int i = 0; i < command_value_len; i++) { + for (uint8_t i = 0; i < command_value_len; i++) { this->write_byte(command_value[i]); } } - // footer - this->write_array(CMD_FRAME_END, 4); + // frame footer bytes + this->write_array(CMD_FRAME_FOOTER, sizeof(CMD_FRAME_FOOTER)); // FIXME to remove delay(50); // NOLINT } @@ -427,25 +437,23 @@ void LD2450Component::send_command_(uint8_t command, const uint8_t *command_valu // LD2450 Radar data message: // [AA FF 03 00] [0E 03 B1 86 10 00 40 01] [00 00 00 00 00 00 00 00] [00 00 00 00 00 00 00 00] [55 CC] // Header Target 1 Target 2 Target 3 End -void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) { - if (len < 29) { // header (4 bytes) + 8 x 3 target data + footer (2 bytes) - ESP_LOGE(TAG, "Invalid message length"); - return; - } - if (buffer[0] != 0xAA || buffer[1] != 0xFF || buffer[2] != 0x03 || buffer[3] != 0x00) { // header - ESP_LOGE(TAG, "Invalid message header"); - return; - } - if (buffer[len - 2] != 0x55 || buffer[len - 1] != 0xCC) { // footer - ESP_LOGE(TAG, "Invalid message footer"); - return; - } - +void LD2450Component::handle_periodic_data_() { + // Early throttle check - moved before any processing to save CPU cycles if (App.get_loop_component_start_time() - this->last_periodic_millis_ < this->throttle_) { - ESP_LOGV(TAG, "Throttling: %d", this->throttle_); return; } + if (this->buffer_pos_ < 29) { // header (4 bytes) + 8 x 3 target data + footer (2 bytes) + ESP_LOGE(TAG, "Invalid length"); + return; + } + if (!ld2450::validate_header_footer(DATA_FRAME_HEADER, this->buffer_data_) || + this->buffer_data_[this->buffer_pos_ - 2] != DATA_FRAME_FOOTER[0] || + this->buffer_data_[this->buffer_pos_ - 1] != DATA_FRAME_FOOTER[1]) { + ESP_LOGE(TAG, "Invalid header/footer"); + return; + } + // Save the timestamp after validating the frame so, if invalid, we'll take the next frame immediately this->last_periodic_millis_ = App.get_loop_component_start_time(); int16_t target_count = 0; @@ -453,13 +461,13 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) { int16_t moving_target_count = 0; int16_t start = 0; int16_t val = 0; - uint8_t index = 0; int16_t tx = 0; int16_t ty = 0; int16_t td = 0; int16_t ts = 0; int16_t angle = 0; - std::string direction{}; + uint8_t index = 0; + Direction direction{DIRECTION_UNDEFINED}; bool is_moving = false; #if defined(USE_BINARY_SENSOR) || defined(USE_SENSOR) || defined(USE_TEXT_SENSOR) @@ -471,29 +479,38 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) { is_moving = false; sensor::Sensor *sx = this->move_x_sensors_[index]; if (sx != nullptr) { - val = ld2450::decode_coordinate(buffer[start], buffer[start + 1]); + val = ld2450::decode_coordinate(this->buffer_data_[start], this->buffer_data_[start + 1]); tx = val; - sx->publish_state(val); + if (this->cached_target_data_[index].x != val) { + sx->publish_state(val); + this->cached_target_data_[index].x = val; + } } // Y start = TARGET_Y + index * 8; sensor::Sensor *sy = this->move_y_sensors_[index]; if (sy != nullptr) { - val = ld2450::decode_coordinate(buffer[start], buffer[start + 1]); + val = ld2450::decode_coordinate(this->buffer_data_[start], this->buffer_data_[start + 1]); ty = val; - sy->publish_state(val); + if (this->cached_target_data_[index].y != val) { + sy->publish_state(val); + this->cached_target_data_[index].y = val; + } } // RESOLUTION start = TARGET_RESOLUTION + index * 8; sensor::Sensor *sr = this->move_resolution_sensors_[index]; if (sr != nullptr) { - val = (buffer[start + 1] << 8) | buffer[start]; - sr->publish_state(val); + val = (this->buffer_data_[start + 1] << 8) | this->buffer_data_[start]; + if (this->cached_target_data_[index].resolution != val) { + sr->publish_state(val); + this->cached_target_data_[index].resolution = val; + } } #endif // SPEED start = TARGET_SPEED + index * 8; - val = ld2450::decode_speed(buffer[start], buffer[start + 1]); + val = ld2450::decode_speed(this->buffer_data_[start], this->buffer_data_[start + 1]); ts = val; if (val) { is_moving = true; @@ -502,13 +519,17 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) { #ifdef USE_SENSOR sensor::Sensor *ss = this->move_speed_sensors_[index]; if (ss != nullptr) { - ss->publish_state(val); + if (this->cached_target_data_[index].speed != val) { + ss->publish_state(val); + this->cached_target_data_[index].speed = val; + } } #endif // DISTANCE - val = (uint16_t) sqrt( - pow(ld2450::decode_coordinate(buffer[TARGET_X + index * 8], buffer[(TARGET_X + index * 8) + 1]), 2) + - pow(ld2450::decode_coordinate(buffer[TARGET_Y + index * 8], buffer[(TARGET_Y + index * 8) + 1]), 2)); + // Optimized: use already decoded tx and ty values, replace pow() with multiplication + int32_t x_squared = (int32_t) tx * tx; + int32_t y_squared = (int32_t) ty * ty; + val = (uint16_t) sqrt(x_squared + y_squared); td = val; if (val > 0) { target_count++; @@ -516,27 +537,42 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) { #ifdef USE_SENSOR sensor::Sensor *sd = this->move_distance_sensors_[index]; if (sd != nullptr) { - sd->publish_state(val); + if (this->cached_target_data_[index].distance != val) { + sd->publish_state(val); + this->cached_target_data_[index].distance = val; + } } // ANGLE - angle = calculate_angle(static_cast(ty), static_cast(td)); + angle = ld2450::calculate_angle(static_cast(ty), static_cast(td)); if (tx > 0) { angle = angle * -1; } sensor::Sensor *sa = this->move_angle_sensors_[index]; if (sa != nullptr) { - sa->publish_state(angle); + if (std::isnan(this->cached_target_data_[index].angle) || + std::abs(this->cached_target_data_[index].angle - angle) > 0.1f) { + sa->publish_state(angle); + this->cached_target_data_[index].angle = angle; + } } #endif #ifdef USE_TEXT_SENSOR // DIRECTION - direction = get_direction(ts); if (td == 0) { - direction = "NA"; + direction = DIRECTION_NA; + } else if (ts > 0) { + direction = DIRECTION_MOVING_AWAY; + } else if (ts < 0) { + direction = DIRECTION_APPROACHING; + } else { + direction = DIRECTION_STATIONARY; } text_sensor::TextSensor *tsd = this->direction_text_sensors_[index]; if (tsd != nullptr) { - tsd->publish_state(direction); + if (this->cached_target_data_[index].direction != direction) { + tsd->publish_state(find_str(ld2450::DIRECTION_BY_UINT, direction)); + this->cached_target_data_[index].direction = direction; + } } #endif @@ -563,32 +599,50 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) { // Publish Still Target Count in Zones sensor::Sensor *szstc = this->zone_still_target_count_sensors_[index]; if (szstc != nullptr) { - szstc->publish_state(zone_still_targets); + if (this->cached_zone_data_[index].still_count != zone_still_targets) { + szstc->publish_state(zone_still_targets); + this->cached_zone_data_[index].still_count = zone_still_targets; + } } // Publish Moving Target Count in Zones sensor::Sensor *szmtc = this->zone_moving_target_count_sensors_[index]; if (szmtc != nullptr) { - szmtc->publish_state(zone_moving_targets); + if (this->cached_zone_data_[index].moving_count != zone_moving_targets) { + szmtc->publish_state(zone_moving_targets); + this->cached_zone_data_[index].moving_count = zone_moving_targets; + } } // Publish All Target Count in Zones sensor::Sensor *sztc = this->zone_target_count_sensors_[index]; if (sztc != nullptr) { - sztc->publish_state(zone_all_targets); + if (this->cached_zone_data_[index].total_count != zone_all_targets) { + sztc->publish_state(zone_all_targets); + this->cached_zone_data_[index].total_count = zone_all_targets; + } } } // End loop thru zones // Target Count if (this->target_count_sensor_ != nullptr) { - this->target_count_sensor_->publish_state(target_count); + if (this->cached_global_data_.target_count != target_count) { + this->target_count_sensor_->publish_state(target_count); + this->cached_global_data_.target_count = target_count; + } } // Still Target Count if (this->still_target_count_sensor_ != nullptr) { - this->still_target_count_sensor_->publish_state(still_target_count); + if (this->cached_global_data_.still_count != still_target_count) { + this->still_target_count_sensor_->publish_state(still_target_count); + this->cached_global_data_.still_count = still_target_count; + } } // Moving Target Count if (this->moving_target_count_sensor_ != nullptr) { - this->moving_target_count_sensor_->publish_state(moving_target_count); + if (this->cached_global_data_.moving_count != moving_target_count) { + this->moving_target_count_sensor_->publish_state(moving_target_count); + this->cached_global_data_.moving_count = moving_target_count; + } } #endif @@ -640,117 +694,139 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) { #endif } -bool LD2450Component::handle_ack_data_(uint8_t *buffer, uint8_t len) { - ESP_LOGV(TAG, "Handling ack data for command %02X", buffer[COMMAND]); - if (len < 10) { - ESP_LOGE(TAG, "Invalid ack length"); +bool LD2450Component::handle_ack_data_() { + ESP_LOGV(TAG, "Handling ACK DATA for COMMAND %02X", this->buffer_data_[COMMAND]); + if (this->buffer_pos_ < 10) { + ESP_LOGE(TAG, "Invalid length"); return true; } - if (buffer[0] != 0xFD || buffer[1] != 0xFC || buffer[2] != 0xFB || buffer[3] != 0xFA) { // frame header - ESP_LOGE(TAG, "Invalid ack header (command %02X)", buffer[COMMAND]); + if (!ld2450::validate_header_footer(CMD_FRAME_HEADER, this->buffer_data_)) { + ESP_LOGW(TAG, "Invalid header: %s", format_hex_pretty(this->buffer_data_, HEADER_FOOTER_SIZE).c_str()); return true; } - if (buffer[COMMAND_STATUS] != 0x01) { - ESP_LOGE(TAG, "Invalid ack status"); + if (this->buffer_data_[COMMAND_STATUS] != 0x01) { + ESP_LOGE(TAG, "Invalid status"); return true; } - if (buffer[8] || buffer[9]) { - ESP_LOGE(TAG, "Last buffer was %u, %u", buffer[8], buffer[9]); + if (this->buffer_data_[8] || this->buffer_data_[9]) { + ESP_LOGW(TAG, "Invalid command: %02X, %02X", this->buffer_data_[8], this->buffer_data_[9]); return true; } - switch (buffer[COMMAND]) { - case lowbyte(CMD_ENABLE_CONF): - ESP_LOGV(TAG, "Enable conf command"); + switch (this->buffer_data_[COMMAND]) { + case CMD_ENABLE_CONF: + ESP_LOGV(TAG, "Enable conf"); break; - case lowbyte(CMD_DISABLE_CONF): - ESP_LOGV(TAG, "Disable conf command"); + + case CMD_DISABLE_CONF: + ESP_LOGV(TAG, "Disabled conf"); break; - case lowbyte(CMD_SET_BAUD_RATE): - ESP_LOGV(TAG, "Baud rate change command"); + + case CMD_SET_BAUD_RATE: + ESP_LOGV(TAG, "Baud rate change"); #ifdef USE_SELECT if (this->baud_rate_select_ != nullptr) { - ESP_LOGV(TAG, "Change baud rate to %s", this->baud_rate_select_->state.c_str()); + ESP_LOGE(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->state.c_str()); } #endif break; - case lowbyte(CMD_VERSION): - this->version_ = str_sprintf(VERSION_FMT, buffer[13], buffer[12], buffer[17], buffer[16], buffer[15], buffer[14]); - ESP_LOGV(TAG, "Firmware version: %s", this->version_.c_str()); + + case CMD_QUERY_VERSION: { + std::memcpy(this->version_, &this->buffer_data_[12], sizeof(this->version_)); + std::string version = str_sprintf(VERSION_FMT, this->version_[1], this->version_[0], this->version_[5], + this->version_[4], this->version_[3], this->version_[2]); + ESP_LOGV(TAG, "Firmware version: %s", version.c_str()); #ifdef USE_TEXT_SENSOR if (this->version_text_sensor_ != nullptr) { - this->version_text_sensor_->publish_state(this->version_); + this->version_text_sensor_->publish_state(version); } #endif break; - case lowbyte(CMD_MAC): - if (len < 20) { + } + + case CMD_QUERY_MAC_ADDRESS: { + if (this->buffer_pos_ < 20) { return false; } - this->mac_ = format_mac_address_pretty(&buffer[10]); - ESP_LOGV(TAG, "MAC address: %s", this->mac_.c_str()); + + this->bluetooth_on_ = std::memcmp(&this->buffer_data_[10], NO_MAC, sizeof(NO_MAC)) != 0; + if (this->bluetooth_on_) { + std::memcpy(this->mac_address_, &this->buffer_data_[10], sizeof(this->mac_address_)); + } + + std::string mac_str = + mac_address_is_valid(this->mac_address_) ? format_mac_address_pretty(this->mac_address_) : UNKNOWN_MAC; + ESP_LOGV(TAG, "MAC address: %s", mac_str.c_str()); #ifdef USE_TEXT_SENSOR if (this->mac_text_sensor_ != nullptr) { - this->mac_text_sensor_->publish_state(this->mac_ == NO_MAC ? UNKNOWN_MAC : this->mac_); + this->mac_text_sensor_->publish_state(mac_str); } #endif #ifdef USE_SWITCH if (this->bluetooth_switch_ != nullptr) { - this->bluetooth_switch_->publish_state(this->mac_ != NO_MAC); + this->bluetooth_switch_->publish_state(this->bluetooth_on_); } #endif break; - case lowbyte(CMD_BLUETOOTH): - ESP_LOGV(TAG, "Bluetooth command"); + } + + case CMD_BLUETOOTH: + ESP_LOGV(TAG, "Bluetooth"); break; - case lowbyte(CMD_SINGLE_TARGET_MODE): - ESP_LOGV(TAG, "Single target conf command"); + + case CMD_SINGLE_TARGET_MODE: + ESP_LOGV(TAG, "Single target conf"); #ifdef USE_SWITCH if (this->multi_target_switch_ != nullptr) { this->multi_target_switch_->publish_state(false); } #endif break; - case lowbyte(CMD_MULTI_TARGET_MODE): - ESP_LOGV(TAG, "Multi target conf command"); + + case CMD_MULTI_TARGET_MODE: + ESP_LOGV(TAG, "Multi target conf"); #ifdef USE_SWITCH if (this->multi_target_switch_ != nullptr) { this->multi_target_switch_->publish_state(true); } #endif break; - case lowbyte(CMD_QUERY_TARGET_MODE): - ESP_LOGV(TAG, "Query target tracking mode command"); + + case CMD_QUERY_TARGET_MODE: + ESP_LOGV(TAG, "Query target tracking mode"); #ifdef USE_SWITCH if (this->multi_target_switch_ != nullptr) { - this->multi_target_switch_->publish_state(buffer[10] == 0x02); + this->multi_target_switch_->publish_state(this->buffer_data_[10] == 0x02); } #endif break; - case lowbyte(CMD_QUERY_ZONE): - ESP_LOGV(TAG, "Query zone conf command"); - this->zone_type_ = std::stoi(std::to_string(buffer[10]), nullptr, 16); + + case CMD_QUERY_ZONE: + ESP_LOGV(TAG, "Query zone conf"); + this->zone_type_ = std::stoi(std::to_string(this->buffer_data_[10]), nullptr, 16); this->publish_zone_type(); #ifdef USE_SELECT if (this->zone_type_select_ != nullptr) { ESP_LOGV(TAG, "Change zone type to: %s", this->zone_type_select_->state.c_str()); } #endif - if (buffer[10] == 0x00) { + if (this->buffer_data_[10] == 0x00) { ESP_LOGV(TAG, "Zone: Disabled"); } - if (buffer[10] == 0x01) { + if (this->buffer_data_[10] == 0x01) { ESP_LOGV(TAG, "Zone: Area detection"); } - if (buffer[10] == 0x02) { + if (this->buffer_data_[10] == 0x02) { ESP_LOGV(TAG, "Zone: Area filter"); } - this->process_zone_(buffer); + this->process_zone_(); break; - case lowbyte(CMD_SET_ZONE): - ESP_LOGV(TAG, "Set zone conf command"); + + case CMD_SET_ZONE: + ESP_LOGV(TAG, "Set zone conf"); this->query_zone_info(); break; + default: break; } @@ -758,55 +834,57 @@ bool LD2450Component::handle_ack_data_(uint8_t *buffer, uint8_t len) { } // Read LD2450 buffer data -void LD2450Component::readline_(int readch, uint8_t *buffer, uint8_t len) { +void LD2450Component::readline_(int readch) { if (readch < 0) { - return; + return; // No data available } - if (this->buffer_pos_ < len - 1) { - buffer[this->buffer_pos_++] = readch; - buffer[this->buffer_pos_] = 0; + + if (this->buffer_pos_ < MAX_LINE_LENGTH - 1) { + this->buffer_data_[this->buffer_pos_++] = readch; + this->buffer_data_[this->buffer_pos_] = 0; } else { + // We should never get here, but just in case... + ESP_LOGW(TAG, "Max command length exceeded; ignoring"); this->buffer_pos_ = 0; } if (this->buffer_pos_ < 4) { - return; + return; // Not enough data to process yet } - if (buffer[this->buffer_pos_ - 2] == 0x55 && buffer[this->buffer_pos_ - 1] == 0xCC) { - ESP_LOGV(TAG, "Handle periodic radar data"); - this->handle_periodic_data_(buffer, this->buffer_pos_); + if (this->buffer_data_[this->buffer_pos_ - 2] == DATA_FRAME_FOOTER[0] && + this->buffer_data_[this->buffer_pos_ - 1] == DATA_FRAME_FOOTER[1]) { + ESP_LOGV(TAG, "Handling Periodic Data: %s", format_hex_pretty(this->buffer_data_, this->buffer_pos_).c_str()); + this->handle_periodic_data_(); this->buffer_pos_ = 0; // Reset position index for next frame - } else if (buffer[this->buffer_pos_ - 4] == 0x04 && buffer[this->buffer_pos_ - 3] == 0x03 && - buffer[this->buffer_pos_ - 2] == 0x02 && buffer[this->buffer_pos_ - 1] == 0x01) { - ESP_LOGV(TAG, "Handle command ack data"); - if (this->handle_ack_data_(buffer, this->buffer_pos_)) { - this->buffer_pos_ = 0; // Reset position index for next frame + } else if (ld2450::validate_header_footer(CMD_FRAME_FOOTER, &this->buffer_data_[this->buffer_pos_ - 4])) { + ESP_LOGV(TAG, "Handling Ack Data: %s", format_hex_pretty(this->buffer_data_, this->buffer_pos_).c_str()); + if (this->handle_ack_data_()) { + this->buffer_pos_ = 0; // Reset position index for next message } else { - ESP_LOGV(TAG, "Command ack data invalid"); + ESP_LOGV(TAG, "Ack Data incomplete"); } } } // Set Config Mode - Pre-requisite sending commands void LD2450Component::set_config_mode_(bool enable) { - uint8_t cmd = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF; - uint8_t cmd_value[2] = {0x01, 0x00}; - this->send_command_(cmd, enable ? cmd_value : nullptr, 2); + const uint8_t cmd = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF; + const uint8_t cmd_value[2] = {0x01, 0x00}; + this->send_command_(cmd, enable ? cmd_value : nullptr, sizeof(cmd_value)); } // Set Bluetooth Enable/Disable void LD2450Component::set_bluetooth(bool enable) { this->set_config_mode_(true); - uint8_t enable_cmd_value[2] = {0x01, 0x00}; - uint8_t disable_cmd_value[2] = {0x00, 0x00}; - this->send_command_(CMD_BLUETOOTH, enable ? enable_cmd_value : disable_cmd_value, 2); + const uint8_t cmd_value[2] = {enable ? (uint8_t) 0x01 : (uint8_t) 0x00, 0x00}; + this->send_command_(CMD_BLUETOOTH, cmd_value, sizeof(cmd_value)); this->set_timeout(200, [this]() { this->restart_and_read_all_info(); }); } // Set Baud rate void LD2450Component::set_baud_rate(const std::string &state) { this->set_config_mode_(true); - uint8_t cmd_value[2] = {find_uint8(BAUD_RATES_BY_STR, state), 0x00}; - this->send_command_(CMD_SET_BAUD_RATE, cmd_value, 2); + const uint8_t cmd_value[2] = {find_uint8(BAUD_RATES_BY_STR, state), 0x00}; + this->send_command_(CMD_SET_BAUD_RATE, cmd_value, sizeof(cmd_value)); this->set_timeout(200, [this]() { this->restart_(); }); } @@ -847,12 +925,12 @@ void LD2450Component::factory_reset() { void LD2450Component::restart_() { this->send_command_(CMD_RESTART, nullptr, 0); } // Get LD2450 firmware version -void LD2450Component::get_version_() { this->send_command_(CMD_VERSION, nullptr, 0); } +void LD2450Component::get_version_() { this->send_command_(CMD_QUERY_VERSION, nullptr, 0); } // Get LD2450 mac address void LD2450Component::get_mac_() { uint8_t cmd_value[2] = {0x01, 0x00}; - this->send_command_(CMD_MAC, cmd_value, 2); + this->send_command_(CMD_QUERY_MAC_ADDRESS, cmd_value, 2); } // Query for target tracking mode diff --git a/esphome/components/ld2450/ld2450.h b/esphome/components/ld2450/ld2450.h index b0c19dc96c..ae72a0d8cb 100644 --- a/esphome/components/ld2450/ld2450.h +++ b/esphome/components/ld2450/ld2450.h @@ -5,6 +5,8 @@ #include "esphome/core/defines.h" #include "esphome/core/helpers.h" #include "esphome/core/preferences.h" +#include +#include #ifdef USE_SENSOR #include "esphome/components/sensor/sensor.h" #endif @@ -36,10 +38,18 @@ namespace ld2450 { // Constants static const uint8_t DEFAULT_PRESENCE_TIMEOUT = 5; // Timeout to reset presense status 5 sec. -static const uint8_t MAX_LINE_LENGTH = 60; // Max characters for serial buffer +static const uint8_t MAX_LINE_LENGTH = 41; // Max characters for serial buffer static const uint8_t MAX_TARGETS = 3; // Max 3 Targets in LD2450 static const uint8_t MAX_ZONES = 3; // Max 3 Zones in LD2450 +enum Direction : uint8_t { + DIRECTION_APPROACHING = 0, + DIRECTION_MOVING_AWAY = 1, + DIRECTION_STATIONARY = 2, + DIRECTION_NA = 3, + DIRECTION_UNDEFINED = 4, +}; + // Target coordinate struct struct Target { int16_t x; @@ -65,19 +75,22 @@ struct ZoneOfNumbers { #endif class LD2450Component : public Component, public uart::UARTDevice { -#ifdef USE_SENSOR - SUB_SENSOR(target_count) - SUB_SENSOR(still_target_count) - SUB_SENSOR(moving_target_count) -#endif #ifdef USE_BINARY_SENSOR - SUB_BINARY_SENSOR(target) SUB_BINARY_SENSOR(moving_target) SUB_BINARY_SENSOR(still_target) + SUB_BINARY_SENSOR(target) +#endif +#ifdef USE_SENSOR + SUB_SENSOR(moving_target_count) + SUB_SENSOR(still_target_count) + SUB_SENSOR(target_count) #endif #ifdef USE_TEXT_SENSOR - SUB_TEXT_SENSOR(version) SUB_TEXT_SENSOR(mac) + SUB_TEXT_SENSOR(version) +#endif +#ifdef USE_NUMBER + SUB_NUMBER(presence_timeout) #endif #ifdef USE_SELECT SUB_SELECT(baud_rate) @@ -88,19 +101,16 @@ class LD2450Component : public Component, public uart::UARTDevice { SUB_SWITCH(multi_target) #endif #ifdef USE_BUTTON - SUB_BUTTON(reset) + SUB_BUTTON(factory_reset) SUB_BUTTON(restart) #endif -#ifdef USE_NUMBER - SUB_NUMBER(presence_timeout) -#endif public: void setup() override; void dump_config() override; void loop() override; void set_presence_timeout(); - void set_throttle(uint16_t value) { this->throttle_ = value; }; + void set_throttle(uint16_t value) { this->throttle_ = value; } void read_all_info(); void query_zone_info(); void restart_and_read_all_info(); @@ -136,10 +146,10 @@ class LD2450Component : public Component, public uart::UARTDevice { protected: void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len); void set_config_mode_(bool enable); - void handle_periodic_data_(uint8_t *buffer, uint8_t len); - bool handle_ack_data_(uint8_t *buffer, uint8_t len); - void process_zone_(uint8_t *buffer); - void readline_(int readch, uint8_t *buffer, uint8_t len); + void handle_periodic_data_(); + bool handle_ack_data_(); + void process_zone_(); + void readline_(int readch); void get_version_(); void get_mac_(); void query_target_tracking_mode_(); @@ -157,13 +167,40 @@ class LD2450Component : public Component, public uart::UARTDevice { uint32_t moving_presence_millis_ = 0; uint16_t throttle_ = 0; uint16_t timeout_ = 5; - uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer uint8_t buffer_data_[MAX_LINE_LENGTH]; + uint8_t mac_address_[6] = {0, 0, 0, 0, 0, 0}; + uint8_t version_[6] = {0, 0, 0, 0, 0, 0}; + uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer uint8_t zone_type_ = 0; + bool bluetooth_on_{false}; Target target_info_[MAX_TARGETS]; Zone zone_config_[MAX_ZONES]; - std::string version_{}; - std::string mac_{}; + + // Change detection - cache previous values to avoid redundant publishes + // All values are initialized to sentinel values that are outside the valid sensor ranges + // to ensure the first real measurement is always published + struct CachedTargetData { + int16_t x = std::numeric_limits::min(); // -32768, outside range of -4860 to 4860 + int16_t y = std::numeric_limits::min(); // -32768, outside range of 0 to 7560 + int16_t speed = std::numeric_limits::min(); // -32768, outside practical sensor range + uint16_t resolution = std::numeric_limits::max(); // 65535, unlikely resolution value + uint16_t distance = std::numeric_limits::max(); // 65535, outside range of 0 to ~8990 + Direction direction = DIRECTION_UNDEFINED; // Undefined, will differ from any real direction + float angle = NAN; // NAN, safe sentinel for floats + } cached_target_data_[MAX_TARGETS]; + + struct CachedZoneData { + uint8_t still_count = std::numeric_limits::max(); // 255, unlikely zone count + uint8_t moving_count = std::numeric_limits::max(); // 255, unlikely zone count + uint8_t total_count = std::numeric_limits::max(); // 255, unlikely zone count + } cached_zone_data_[MAX_ZONES]; + + struct CachedGlobalData { + uint8_t target_count = std::numeric_limits::max(); // 255, max 3 targets possible + uint8_t still_count = std::numeric_limits::max(); // 255, max 3 targets possible + uint8_t moving_count = std::numeric_limits::max(); // 255, max 3 targets possible + } cached_global_data_; + #ifdef USE_NUMBER ESPPreferenceObject pref_; // only used when numbers are in use ZoneOfNumbers zone_numbers_[MAX_ZONES]; diff --git a/esphome/components/libretiny/helpers.cpp b/esphome/components/libretiny/helpers.cpp new file mode 100644 index 0000000000..b6451860d5 --- /dev/null +++ b/esphome/components/libretiny/helpers.cpp @@ -0,0 +1,35 @@ +#include "esphome/core/helpers.h" + +#ifdef USE_LIBRETINY + +#include "esphome/core/hal.h" + +#include // for macAddress() + +namespace esphome { + +uint32_t random_uint32() { return rand(); } + +bool random_bytes(uint8_t *data, size_t len) { + lt_rand_bytes(data, len); + return true; +} + +Mutex::Mutex() { handle_ = xSemaphoreCreateMutex(); } +Mutex::~Mutex() {} +void Mutex::lock() { xSemaphoreTake(this->handle_, portMAX_DELAY); } +bool Mutex::try_lock() { return xSemaphoreTake(this->handle_, 0) == pdTRUE; } +void Mutex::unlock() { xSemaphoreGive(this->handle_); } + +// only affects the executing core +// so should not be used as a mutex lock, only to get accurate timing +IRAM_ATTR InterruptLock::InterruptLock() { portDISABLE_INTERRUPTS(); } +IRAM_ATTR InterruptLock::~InterruptLock() { portENABLE_INTERRUPTS(); } + +void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter) + WiFi.macAddress(mac); +} + +} // namespace esphome + +#endif // USE_LIBRETINY diff --git a/esphome/components/mqtt/mqtt_backend_esp32.h b/esphome/components/mqtt/mqtt_backend_esp32.h index 3611caf554..a24e75eaf9 100644 --- a/esphome/components/mqtt/mqtt_backend_esp32.h +++ b/esphome/components/mqtt/mqtt_backend_esp32.h @@ -252,7 +252,7 @@ class MQTTBackendESP32 final : public MQTTBackend { #if defined(USE_MQTT_IDF_ENQUEUE) static void esphome_mqtt_task(void *params); EventPool mqtt_event_pool_; - LockFreeQueue mqtt_queue_; + NotifyingLockFreeQueue mqtt_queue_; TaskHandle_t task_handle_{nullptr}; bool enqueue_(MqttQueueTypeT type, const char *topic, int qos = 0, bool retain = false, const char *payload = NULL, size_t len = 0); diff --git a/esphome/components/nextion/base_component.py b/esphome/components/nextion/base_component.py index 98dea4b513..392481e39a 100644 --- a/esphome/components/nextion/base_component.py +++ b/esphome/components/nextion/base_component.py @@ -11,6 +11,7 @@ CONF_AUTO_WAKE_ON_TOUCH = "auto_wake_on_touch" CONF_BACKGROUND_PRESSED_COLOR = "background_pressed_color" CONF_COMMAND_SPACING = "command_spacing" CONF_COMPONENT_NAME = "component_name" +CONF_DUMP_DEVICE_INFO = "dump_device_info" CONF_EXIT_REPARSE_ON_START = "exit_reparse_on_start" CONF_FONT_ID = "font_id" CONF_FOREGROUND_PRESSED_COLOR = "foreground_pressed_color" diff --git a/esphome/components/nextion/display.py b/esphome/components/nextion/display.py index 0aa5efeba7..4254ae45fe 100644 --- a/esphome/components/nextion/display.py +++ b/esphome/components/nextion/display.py @@ -15,6 +15,7 @@ from . import Nextion, nextion_ns, nextion_ref from .base_component import ( CONF_AUTO_WAKE_ON_TOUCH, CONF_COMMAND_SPACING, + CONF_DUMP_DEVICE_INFO, CONF_EXIT_REPARSE_ON_START, CONF_MAX_COMMANDS_PER_LOOP, CONF_MAX_QUEUE_SIZE, @@ -57,6 +58,7 @@ CONFIG_SCHEMA = ( cv.positive_time_period_milliseconds, cv.Range(max=TimePeriod(milliseconds=255)), ), + cv.Optional(CONF_DUMP_DEVICE_INFO, default=False): cv.boolean, cv.Optional(CONF_EXIT_REPARSE_ON_START, default=False): cv.boolean, cv.Optional(CONF_MAX_COMMANDS_PER_LOOP): cv.uint16_t, cv.Optional(CONF_MAX_QUEUE_SIZE): cv.positive_int, @@ -95,7 +97,9 @@ CONFIG_SCHEMA = ( cv.Optional(CONF_SKIP_CONNECTION_HANDSHAKE, default=False): cv.boolean, cv.Optional(CONF_START_UP_PAGE): cv.uint8_t, cv.Optional(CONF_TFT_URL): cv.url, - cv.Optional(CONF_TOUCH_SLEEP_TIMEOUT): cv.int_range(min=3, max=65535), + cv.Optional(CONF_TOUCH_SLEEP_TIMEOUT): cv.Any( + 0, cv.int_range(min=3, max=65535) + ), cv.Optional(CONF_WAKE_UP_PAGE): cv.uint8_t, } ) @@ -167,13 +171,19 @@ async def to_code(config): cg.add(var.set_wake_up_page(config[CONF_WAKE_UP_PAGE])) if CONF_START_UP_PAGE in config: + cg.add_define("USE_NEXTION_CONF_START_UP_PAGE") cg.add(var.set_start_up_page(config[CONF_START_UP_PAGE])) cg.add(var.set_auto_wake_on_touch(config[CONF_AUTO_WAKE_ON_TOUCH])) - cg.add(var.set_exit_reparse_on_start(config[CONF_EXIT_REPARSE_ON_START])) + if config[CONF_DUMP_DEVICE_INFO]: + cg.add_define("USE_NEXTION_CONFIG_DUMP_DEVICE_INFO") - cg.add(var.set_skip_connection_handshake(config[CONF_SKIP_CONNECTION_HANDSHAKE])) + if config[CONF_EXIT_REPARSE_ON_START]: + cg.add_define("USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START") + + if config[CONF_SKIP_CONNECTION_HANDSHAKE]: + cg.add_define("USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE") if max_commands_per_loop := config.get(CONF_MAX_COMMANDS_PER_LOOP): cg.add_define("USE_NEXTION_MAX_COMMANDS_PER_LOOP") diff --git a/esphome/components/nextion/nextion.cpp b/esphome/components/nextion/nextion.cpp index bb75385d8c..66e2d26061 100644 --- a/esphome/components/nextion/nextion.cpp +++ b/esphome/components/nextion/nextion.cpp @@ -11,28 +11,25 @@ static const char *const TAG = "nextion"; void Nextion::setup() { this->is_setup_ = false; - this->ignore_is_setup_ = true; + this->connection_state_.ignore_is_setup_ = true; - // Wake up the nextion - this->send_command_("bkcmd=0"); - this->send_command_("sleep=0"); + // Wake up the nextion and ensure clean communication state + this->send_command_("sleep=0"); // Exit sleep mode if sleeping + this->send_command_("bkcmd=0"); // Disable return data during init sequence - this->send_command_("bkcmd=0"); - this->send_command_("sleep=0"); - - // Reboot it + // Reset device for clean state - critical for reliable communication this->send_command_("rest"); - this->ignore_is_setup_ = false; + this->connection_state_.ignore_is_setup_ = false; } bool Nextion::send_command_(const std::string &command) { - if (!this->ignore_is_setup_ && !this->is_setup()) { + if (!this->connection_state_.ignore_is_setup_ && !this->is_setup()) { return false; } #ifdef USE_NEXTION_COMMAND_SPACING - if (!this->ignore_is_setup_ && !this->command_pacer_.can_send()) { + if (!this->connection_state_.ignore_is_setup_ && !this->command_pacer_.can_send()) { ESP_LOGN(TAG, "Command spacing: delaying command '%s'", command.c_str()); return false; } @@ -48,31 +45,26 @@ bool Nextion::send_command_(const std::string &command) { } bool Nextion::check_connect_() { - if (this->is_connected_) + if (this->connection_state_.is_connected_) return true; - // Check if the handshake should be skipped for the Nextion connection - if (this->skip_connection_handshake_) { - // Log the connection status without handshake - ESP_LOGW(TAG, "Connected (no handshake)"); - // Set the connection status to true - this->is_connected_ = true; - // Return true indicating the connection is set - return true; - } - +#ifdef USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE + ESP_LOGW(TAG, "Connected (no handshake)"); // Log the connection status without handshake + this->is_connected_ = true; // Set the connection status to true + return true; // Return true indicating the connection is set +#else // USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE if (this->comok_sent_ == 0) { this->reset_(false); - this->ignore_is_setup_ = true; + this->connection_state_.ignore_is_setup_ = true; this->send_command_("boguscommand=0"); // bogus command. needed sometimes after updating - if (this->exit_reparse_on_start_) { - this->send_command_("DRAKJHSUYDGBNCJHGJKSHBDN"); - } +#ifdef USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START + this->send_command_("DRAKJHSUYDGBNCJHGJKSHBDN"); +#endif // USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START this->send_command_("connect"); this->comok_sent_ = App.get_loop_component_start_time(); - this->ignore_is_setup_ = false; + this->connection_state_.ignore_is_setup_ = false; return false; } @@ -94,16 +86,16 @@ bool Nextion::check_connect_() { for (size_t i = 0; i < response.length(); i++) { ESP_LOGN(TAG, "resp: %s %d %d %c", response.c_str(), i, response[i], response[i]); } -#endif +#endif // NEXTION_PROTOCOL_LOG ESP_LOGW(TAG, "Not connected"); comok_sent_ = 0; return false; } - this->ignore_is_setup_ = true; + this->connection_state_.ignore_is_setup_ = true; ESP_LOGI(TAG, "Connected"); - this->is_connected_ = true; + this->connection_state_.is_connected_ = true; ESP_LOGN(TAG, "connect: %s", response.c_str()); @@ -118,18 +110,27 @@ bool Nextion::check_connect_() { this->is_detected_ = (connect_info.size() == 7); if (this->is_detected_) { ESP_LOGN(TAG, "Connect info: %zu", connect_info.size()); - +#ifdef USE_NEXTION_CONFIG_DUMP_DEVICE_INFO this->device_model_ = connect_info[2]; this->firmware_version_ = connect_info[3]; this->serial_number_ = connect_info[5]; this->flash_size_ = connect_info[6]; +#else // USE_NEXTION_CONFIG_DUMP_DEVICE_INFO + ESP_LOGI(TAG, + " Device Model: %s\n" + " FW Version: %s\n" + " Serial Number: %s\n" + " Flash Size: %s\n", + connect_info[2].c_str(), connect_info[3].c_str(), connect_info[5].c_str(), connect_info[6].c_str()); +#endif // USE_NEXTION_CONFIG_DUMP_DEVICE_INFO } else { ESP_LOGE(TAG, "Bad connect value: '%s'", response.c_str()); } - this->ignore_is_setup_ = false; + this->connection_state_.ignore_is_setup_ = false; this->dump_config(); return true; +#endif // USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE } void Nextion::reset_(bool reset_nextion) { @@ -144,36 +145,42 @@ void Nextion::reset_(bool reset_nextion) { void Nextion::dump_config() { ESP_LOGCONFIG(TAG, "Nextion:"); - if (this->skip_connection_handshake_) { - ESP_LOGCONFIG(TAG, " Skip handshake: %s", YESNO(this->skip_connection_handshake_)); - } else { - ESP_LOGCONFIG(TAG, - " Device Model: %s\n" - " FW Version: %s\n" - " Serial Number: %s\n" - " Flash Size: %s", - this->device_model_.c_str(), this->firmware_version_.c_str(), this->serial_number_.c_str(), - this->flash_size_.c_str()); - } + +#ifdef USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE + ESP_LOGCONFIG(TAG, " Skip handshake: YES"); +#else // USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE ESP_LOGCONFIG(TAG, +#ifdef USE_NEXTION_CONFIG_DUMP_DEVICE_INFO + " Device Model: %s\n" + " FW Version: %s\n" + " Serial Number: %s\n" + " Flash Size: %s\n" +#endif // USE_NEXTION_CONFIG_DUMP_DEVICE_INFO +#ifdef USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START + " Exit reparse: YES\n" +#endif // USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START " Wake On Touch: %s\n" - " Exit reparse: %s", - YESNO(this->auto_wake_on_touch_), YESNO(this->exit_reparse_on_start_)); + " Touch Timeout: %" PRIu16, +#ifdef USE_NEXTION_CONFIG_DUMP_DEVICE_INFO + this->device_model_.c_str(), this->firmware_version_.c_str(), this->serial_number_.c_str(), + this->flash_size_.c_str(), +#endif // USE_NEXTION_CONFIG_DUMP_DEVICE_INFO + YESNO(this->connection_state_.auto_wake_on_touch_), this->touch_sleep_timeout_); +#endif // USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE + #ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP ESP_LOGCONFIG(TAG, " Max commands per loop: %u", this->max_commands_per_loop_); #endif // USE_NEXTION_MAX_COMMANDS_PER_LOOP - if (this->touch_sleep_timeout_ != 0) { - ESP_LOGCONFIG(TAG, " Touch Timeout: %" PRIu16, this->touch_sleep_timeout_); + if (this->wake_up_page_ != 255) { + ESP_LOGCONFIG(TAG, " Wake Up Page: %u", this->wake_up_page_); } - if (this->wake_up_page_ != -1) { - ESP_LOGCONFIG(TAG, " Wake Up Page: %d", this->wake_up_page_); - } - - if (this->start_up_page_ != -1) { - ESP_LOGCONFIG(TAG, " Start Up Page: %d", this->start_up_page_); +#ifdef USE_NEXTION_CONF_START_UP_PAGE + if (this->start_up_page_ != 255) { + ESP_LOGCONFIG(TAG, " Start Up Page: %u", this->start_up_page_); } +#endif // USE_NEXTION_CONF_START_UP_PAGE #ifdef USE_NEXTION_COMMAND_SPACING ESP_LOGCONFIG(TAG, " Cmd spacing: %u ms", this->command_pacer_.get_spacing()); @@ -219,7 +226,7 @@ void Nextion::add_buffer_overflow_event_callback(std::function &&callbac } void Nextion::update_all_components() { - if ((!this->is_setup() && !this->ignore_is_setup_) || this->is_sleeping()) + if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || this->is_sleeping()) return; for (auto *binarysensortype : this->binarysensortype_) { @@ -237,7 +244,7 @@ void Nextion::update_all_components() { } bool Nextion::send_command(const char *command) { - if ((!this->is_setup() && !this->ignore_is_setup_) || this->is_sleeping()) + if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || this->is_sleeping()) return false; if (this->send_command_(command)) { @@ -248,7 +255,7 @@ bool Nextion::send_command(const char *command) { } bool Nextion::send_command_printf(const char *format, ...) { - if ((!this->is_setup() && !this->ignore_is_setup_) || this->is_sleeping()) + if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || this->is_sleeping()) return false; char buffer[256]; @@ -289,40 +296,46 @@ void Nextion::print_queue_members_() { #endif void Nextion::loop() { - if (!this->check_connect_() || this->is_updating_) + if (!this->check_connect_() || this->connection_state_.is_updating_) return; - if (this->nextion_reports_is_setup_ && !this->sent_setup_commands_) { - this->ignore_is_setup_ = true; - this->sent_setup_commands_ = true; + if (this->connection_state_.nextion_reports_is_setup_ && !this->connection_state_.sent_setup_commands_) { + this->connection_state_.ignore_is_setup_ = true; + this->connection_state_.sent_setup_commands_ = true; this->send_command_("bkcmd=3"); // Always, returns 0x00 to 0x23 result of serial command. if (this->brightness_.has_value()) { this->set_backlight_brightness(this->brightness_.value()); } +#ifdef USE_NEXTION_CONF_START_UP_PAGE // Check if a startup page has been set and send the command - if (this->start_up_page_ >= 0) { + if (this->start_up_page_ != 255) { this->goto_page(this->start_up_page_); } +#endif // USE_NEXTION_CONF_START_UP_PAGE - if (this->wake_up_page_ >= 0) { + if (this->wake_up_page_ != 255) { this->set_wake_up_page(this->wake_up_page_); } - this->ignore_is_setup_ = false; + if (this->touch_sleep_timeout_ != 0) { + this->set_touch_sleep_timeout(this->touch_sleep_timeout_); + } + + this->connection_state_.ignore_is_setup_ = false; } this->process_serial_(); // Receive serial data this->process_nextion_commands_(); // Process nextion return commands - if (!this->nextion_reports_is_setup_) { + if (!this->connection_state_.nextion_reports_is_setup_) { if (this->started_ms_ == 0) this->started_ms_ = App.get_loop_component_start_time(); if (this->started_ms_ + this->startup_override_ms_ < App.get_loop_component_start_time()) { ESP_LOGD(TAG, "Manual ready set"); - this->nextion_reports_is_setup_ = true; + this->connection_state_.nextion_reports_is_setup_ = true; } } @@ -665,7 +678,7 @@ void Nextion::process_nextion_commands_() { case 0x88: // system successful start up { ESP_LOGD(TAG, "System start: %zu", to_process_length); - this->nextion_reports_is_setup_ = true; + this->connection_state_.nextion_reports_is_setup_ = true; break; } case 0x89: { // start SD card upgrade @@ -1048,7 +1061,7 @@ void Nextion::add_no_result_to_queue_(const std::string &variable_name) { * @param command */ void Nextion::add_no_result_to_queue_with_command_(const std::string &variable_name, const std::string &command) { - if ((!this->is_setup() && !this->ignore_is_setup_) || command.empty()) + if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || command.empty()) return; if (this->send_command_(command)) { @@ -1091,7 +1104,7 @@ void Nextion::add_no_result_to_queue_with_pending_command_(const std::string &va bool Nextion::add_no_result_to_queue_with_ignore_sleep_printf_(const std::string &variable_name, const char *format, ...) { - if ((!this->is_setup() && !this->ignore_is_setup_)) + if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_)) return false; char buffer[256]; @@ -1116,7 +1129,7 @@ bool Nextion::add_no_result_to_queue_with_ignore_sleep_printf_(const std::string * @param ... The format arguments */ bool Nextion::add_no_result_to_queue_with_printf_(const std::string &variable_name, const char *format, ...) { - if ((!this->is_setup() && !this->ignore_is_setup_) || this->is_sleeping()) + if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || this->is_sleeping()) return false; char buffer[256]; @@ -1155,7 +1168,7 @@ void Nextion::add_no_result_to_queue_with_set(const std::string &variable_name, void Nextion::add_no_result_to_queue_with_set_internal_(const std::string &variable_name, const std::string &variable_name_to_send, int32_t state_value, bool is_sleep_safe) { - if ((!this->is_setup() && !this->ignore_is_setup_) || (!is_sleep_safe && this->is_sleeping())) + if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || (!is_sleep_safe && this->is_sleeping())) return; this->add_no_result_to_queue_with_ignore_sleep_printf_(variable_name, "%s=%" PRId32, variable_name_to_send.c_str(), @@ -1183,7 +1196,7 @@ void Nextion::add_no_result_to_queue_with_set(const std::string &variable_name, void Nextion::add_no_result_to_queue_with_set_internal_(const std::string &variable_name, const std::string &variable_name_to_send, const std::string &state_value, bool is_sleep_safe) { - if ((!this->is_setup() && !this->ignore_is_setup_) || (!is_sleep_safe && this->is_sleeping())) + if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || (!is_sleep_safe && this->is_sleeping())) return; this->add_no_result_to_queue_with_printf_(variable_name, "%s=\"%s\"", variable_name_to_send.c_str(), @@ -1200,7 +1213,7 @@ void Nextion::add_no_result_to_queue_with_set_internal_(const std::string &varia * @param component Pointer to the Nextion component that will handle the response. */ void Nextion::add_to_get_queue(NextionComponentBase *component) { - if ((!this->is_setup() && !this->ignore_is_setup_)) + if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_)) return; #ifdef USE_NEXTION_MAX_QUEUE_SIZE @@ -1240,7 +1253,7 @@ void Nextion::add_to_get_queue(NextionComponentBase *component) { * @param buffer_size The buffer data */ void Nextion::add_addt_command_to_queue(NextionComponentBase *component) { - if ((!this->is_setup() && !this->ignore_is_setup_) || this->is_sleeping()) + if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || this->is_sleeping()) return; RAMAllocator allocator; @@ -1281,7 +1294,7 @@ void Nextion::set_writer(const nextion_writer_t &writer) { this->writer_ = write ESPDEPRECATED("set_wait_for_ack(bool) deprecated, no effect", "v1.20") void Nextion::set_wait_for_ack(bool wait_for_ack) { ESP_LOGE(TAG, "Deprecated"); } -bool Nextion::is_updating() { return this->is_updating_; } +bool Nextion::is_updating() { return this->connection_state_.is_updating_; } } // namespace nextion } // namespace esphome diff --git a/esphome/components/nextion/nextion.h b/esphome/components/nextion/nextion.h index 0b77d234f5..e2c4faa1d0 100644 --- a/esphome/components/nextion/nextion.h +++ b/esphome/components/nextion/nextion.h @@ -932,21 +932,6 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe */ void set_backlight_brightness(float brightness); - /** - * Sets whether the Nextion display should skip the connection handshake process. - * @param skip_handshake True or false. When skip_connection_handshake is true, - * the connection will be established without performing the handshake. - * This can be useful when using Nextion Simulator. - * - * Example: - * ```cpp - * it.set_skip_connection_handshake(true); - * ``` - * - * When set to true, the display will be marked as connected without performing a handshake. - */ - void set_skip_connection_handshake(bool skip_handshake) { this->skip_connection_handshake_ = skip_handshake; } - /** * Sets Nextion mode between sleep and awake * @param True or false. Sleep=true to enter sleep mode or sleep=false to exit sleep mode. @@ -1179,22 +1164,43 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe void update_components_by_prefix(const std::string &prefix); /** - * Set the touch sleep timeout of the display. - * @param timeout Timeout in seconds. + * Set the touch sleep timeout of the display using the `thsp` command. + * + * Sets internal No-touch-then-sleep timer to specified value in seconds. + * Nextion will auto-enter sleep mode if and when this timer expires. + * + * @param touch_sleep_timeout Timeout in seconds. + * Range: 3 to 65535 seconds (minimum 3 seconds, maximum ~18 hours 12 minutes 15 seconds) + * Use 0 to disable touch sleep timeout. + * + * @note Once `thsp` is set, it will persist until reboot or reset. The Nextion device + * needs to exit sleep mode to issue `thsp=0` to disable sleep on no touch. + * + * @note The display will only wake up by a restart or by setting up `thup` (auto wake on touch). + * See set_auto_wake_on_touch() to configure wake behavior. * * Example: * ```cpp + * // Set 30 second touch timeout * it.set_touch_sleep_timeout(30); + * + * // Set maximum timeout (~18 hours) + * it.set_touch_sleep_timeout(65535); + * + * // Disable touch sleep timeout + * it.set_touch_sleep_timeout(0); * ``` * - * After 30 seconds the display will go to sleep. Note: the display will only wakeup by a restart or by setting up - * `thup`. + * Related Nextion instruction: `thsp=` + * + * @see set_auto_wake_on_touch() Configure automatic wake on touch + * @see sleep() Manually control sleep state */ - void set_touch_sleep_timeout(uint16_t touch_sleep_timeout); + void set_touch_sleep_timeout(uint16_t touch_sleep_timeout = 0); /** * Sets which page Nextion loads when exiting sleep mode. Note this can be set even when Nextion is in sleep mode. - * @param wake_up_page The page id, from 0 to the last page in Nextion. Set -1 (not set to any existing page) to + * @param wake_up_page The page id, from 0 to the last page in Nextion. Set 255 (not set to any existing page) to * wakes up to current page. * * Example: @@ -1204,11 +1210,12 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe * * The display will wake up to page 2. */ - void set_wake_up_page(int16_t wake_up_page = -1); + void set_wake_up_page(uint8_t wake_up_page = 255); +#ifdef USE_NEXTION_CONF_START_UP_PAGE /** * Sets which page Nextion loads when connecting to ESPHome. - * @param start_up_page The page id, from 0 to the last page in Nextion. Set -1 (not set to any existing page) to + * @param start_up_page The page id, from 0 to the last page in Nextion. Set 255 (not set to any existing page) to * wakes up to current page. * * Example: @@ -1218,7 +1225,8 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe * * The display will go to page 2 when it establishes a connection to ESPHome. */ - void set_start_up_page(int16_t start_up_page = -1) { this->start_up_page_ = start_up_page; } + void set_start_up_page(uint8_t start_up_page = 255) { this->start_up_page_ = start_up_page; } +#endif // USE_NEXTION_CONF_START_UP_PAGE /** * Sets if Nextion should auto-wake from sleep when touch press occurs. @@ -1234,20 +1242,6 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe */ void set_auto_wake_on_touch(bool auto_wake_on_touch); - /** - * Sets if Nextion should exit the active reparse mode before the "connect" command is sent - * @param exit_reparse_on_start True or false. When exit_reparse_on_start is true, the exit reparse command - * will be sent before requesting the connection from Nextion. - * - * Example: - * ```cpp - * it.set_exit_reparse_on_start(true); - * ``` - * - * The display will be requested to leave active reparse mode before setup. - */ - void set_exit_reparse_on_start(bool exit_reparse_on_start) { this->exit_reparse_on_start_ = exit_reparse_on_start; } - /** * @brief Retrieves the number of commands pending in the Nextion command queue. * @@ -1290,7 +1284,7 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe * the Nextion display. A connection is considered established when: * * - The initial handshake with the display is completed successfully, or - * - The handshake is skipped via skip_connection_handshake_ flag + * - The handshake is skipped via USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE flag * * The connection status is particularly useful when: * - Troubleshooting communication issues @@ -1300,7 +1294,7 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe * @return true if the Nextion display is connected and ready to receive commands * @return false if the display is not yet connected or connection was lost */ - bool is_connected() { return this->is_connected_; } + bool is_connected() { return this->connection_state_.is_connected_; } protected: #ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP @@ -1334,21 +1328,29 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe bool remove_from_q_(bool report_empty = true); /** - * @brief - * Sends commands ignoring of the Nextion has been setup. + * @brief Status flags for Nextion display state management + * + * Uses bitfields to pack multiple boolean states into a single byte, + * saving 5 bytes of RAM compared to individual bool variables. */ - bool ignore_is_setup_ = false; + struct { + uint8_t is_connected_ : 1; ///< Connection established with Nextion display + uint8_t sent_setup_commands_ : 1; ///< Initial setup commands have been sent + uint8_t ignore_is_setup_ : 1; ///< Temporarily ignore setup state for special operations + uint8_t nextion_reports_is_setup_ : 1; ///< Nextion has reported successful initialization + uint8_t is_updating_ : 1; ///< TFT firmware update is currently in progress + uint8_t auto_wake_on_touch_ : 1; ///< Display should wake automatically on touch (default: true) + uint8_t reserved_ : 2; ///< Reserved bits for future flag additions + } connection_state_{}; ///< Zero-initialized status flags (all start as false) - bool nextion_reports_is_setup_ = false; void process_nextion_commands_(); void process_serial_(); - bool is_updating_ = false; uint16_t touch_sleep_timeout_ = 0; - int16_t wake_up_page_ = -1; - int16_t start_up_page_ = -1; + uint8_t wake_up_page_ = 255; +#ifdef USE_NEXTION_CONF_START_UP_PAGE + uint8_t start_up_page_ = 255; +#endif // USE_NEXTION_CONF_START_UP_PAGE bool auto_wake_on_touch_ = true; - bool exit_reparse_on_start_ = false; - bool skip_connection_handshake_ = false; /** * Manually send a raw command to the display and don't wait for an acknowledgement packet. @@ -1455,10 +1457,12 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe optional writer_; optional brightness_; +#ifdef USE_NEXTION_CONFIG_DUMP_DEVICE_INFO std::string device_model_; std::string firmware_version_; std::string serial_number_; std::string flash_size_; +#endif // USE_NEXTION_CONFIG_DUMP_DEVICE_INFO void remove_front_no_sensors_(); @@ -1468,11 +1472,9 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe void reset_(bool reset_nextion = true); std::string command_data_; - bool is_connected_ = false; const uint16_t startup_override_ms_ = 8000; const uint16_t max_q_age_ms_ = 8000; uint32_t started_ms_ = 0; - bool sent_setup_commands_ = false; }; } // namespace nextion diff --git a/esphome/components/nextion/nextion_commands.cpp b/esphome/components/nextion/nextion_commands.cpp index 84aacd1868..f3a282717b 100644 --- a/esphome/components/nextion/nextion_commands.cpp +++ b/esphome/components/nextion/nextion_commands.cpp @@ -10,19 +10,20 @@ static const char *const TAG = "nextion"; // Sleep safe commands void Nextion::soft_reset() { this->send_command_("rest"); } -void Nextion::set_wake_up_page(int16_t wake_up_page) { +void Nextion::set_wake_up_page(uint8_t wake_up_page) { this->wake_up_page_ = wake_up_page; this->add_no_result_to_queue_with_set_internal_("wake_up_page", "wup", wake_up_page, true); } -void Nextion::set_touch_sleep_timeout(uint16_t touch_sleep_timeout) { - if (touch_sleep_timeout < 3) { - ESP_LOGD(TAG, "Sleep timeout out of bounds (3-65535)"); - return; +void Nextion::set_touch_sleep_timeout(const uint16_t touch_sleep_timeout) { + // Validate range: Nextion thsp command requires min 3, max 65535 seconds (0 disables) + if (touch_sleep_timeout != 0 && touch_sleep_timeout < 3) { + this->touch_sleep_timeout_ = 3; // Auto-correct to minimum valid value + } else { + this->touch_sleep_timeout_ = touch_sleep_timeout; } - this->touch_sleep_timeout_ = touch_sleep_timeout; - this->add_no_result_to_queue_with_set_internal_("touch_sleep_timeout", "thsp", touch_sleep_timeout, true); + this->add_no_result_to_queue_with_set_internal_("touch_sleep_timeout", "thsp", this->touch_sleep_timeout_, true); } void Nextion::sleep(bool sleep) { @@ -38,7 +39,7 @@ void Nextion::sleep(bool sleep) { // Protocol reparse mode bool Nextion::set_protocol_reparse_mode(bool active_mode) { ESP_LOGV(TAG, "Reparse mode: %s", YESNO(active_mode)); - this->ignore_is_setup_ = true; // if not in reparse mode setup will fail, so it should be ignored + this->connection_state_.ignore_is_setup_ = true; // if not in reparse mode setup will fail, so it should be ignored bool all_commands_sent = true; if (active_mode) { // Sets active protocol reparse mode all_commands_sent &= this->send_command_("recmod=1"); @@ -48,10 +49,10 @@ bool Nextion::set_protocol_reparse_mode(bool active_mode) { all_commands_sent &= this->send_command_("recmod=0"); // Sending recmode=0 twice is recommended all_commands_sent &= this->send_command_("recmod=0"); } - if (!this->nextion_reports_is_setup_) { // No need to connect if is already setup + if (!this->connection_state_.nextion_reports_is_setup_) { // No need to connect if is already setup all_commands_sent &= this->send_command_("connect"); } - this->ignore_is_setup_ = false; + this->connection_state_.ignore_is_setup_ = false; return all_commands_sent; } @@ -191,7 +192,7 @@ void Nextion::set_backlight_brightness(float brightness) { } void Nextion::set_auto_wake_on_touch(bool auto_wake_on_touch) { - this->auto_wake_on_touch_ = auto_wake_on_touch; + this->connection_state_.auto_wake_on_touch_ = auto_wake_on_touch; this->add_no_result_to_queue_with_set("auto_wake_on_touch", "thup", auto_wake_on_touch ? 1 : 0); } diff --git a/esphome/components/nextion/nextion_upload.cpp b/esphome/components/nextion/nextion_upload.cpp index 6a54abfed4..c47b393f99 100644 --- a/esphome/components/nextion/nextion_upload.cpp +++ b/esphome/components/nextion/nextion_upload.cpp @@ -16,8 +16,8 @@ bool Nextion::upload_end_(bool successful) { } else { ESP_LOGE(TAG, "Upload failed"); - this->is_updating_ = false; - this->ignore_is_setup_ = false; + this->connection_state_.is_updating_ = false; + this->connection_state_.ignore_is_setup_ = false; uint32_t baud_rate = this->parent_->get_baud_rate(); if (baud_rate != this->original_baud_rate_) { diff --git a/esphome/components/nextion/nextion_upload_arduino.cpp b/esphome/components/nextion/nextion_upload_arduino.cpp index 6cd03118d2..b0e5d121dd 100644 --- a/esphome/components/nextion/nextion_upload_arduino.cpp +++ b/esphome/components/nextion/nextion_upload_arduino.cpp @@ -152,7 +152,7 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) { ESP_LOGD(TAG, "Exit reparse: %s", YESNO(exit_reparse)); ESP_LOGD(TAG, "URL: %s", this->tft_url_.c_str()); - if (this->is_updating_) { + if (this->connection_state_.is_updating_) { ESP_LOGW(TAG, "Upload in progress"); return false; } @@ -162,7 +162,7 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) { return false; } - this->is_updating_ = true; + this->connection_state_.is_updating_ = true; if (exit_reparse) { ESP_LOGD(TAG, "Exit reparse mode"); @@ -203,7 +203,7 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) { begin_status = http_client.begin(*this->get_wifi_client_(), this->tft_url_.c_str()); #endif // USE_ESP8266 if (!begin_status) { - this->is_updating_ = false; + this->connection_state_.is_updating_ = false; ESP_LOGD(TAG, "Connection failed"); return false; } else { @@ -254,7 +254,7 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) { // The Nextion will ignore the upload command if it is sleeping ESP_LOGV(TAG, "Wake-up"); - this->ignore_is_setup_ = true; + this->connection_state_.ignore_is_setup_ = true; this->send_command_("sleep=0"); this->send_command_("dim=100"); delay(250); // NOLINT diff --git a/esphome/components/nextion/nextion_upload_idf.cpp b/esphome/components/nextion/nextion_upload_idf.cpp index 14ce46d0a0..78a47f9e2c 100644 --- a/esphome/components/nextion/nextion_upload_idf.cpp +++ b/esphome/components/nextion/nextion_upload_idf.cpp @@ -155,7 +155,7 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) { ESP_LOGD(TAG, "Exit reparse: %s", YESNO(exit_reparse)); ESP_LOGD(TAG, "URL: %s", this->tft_url_.c_str()); - if (this->is_updating_) { + if (this->connection_state_.is_updating_) { ESP_LOGW(TAG, "Upload in progress"); return false; } @@ -165,7 +165,7 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) { return false; } - this->is_updating_ = true; + this->connection_state_.is_updating_ = true; if (exit_reparse) { ESP_LOGD(TAG, "Exit reparse mode"); @@ -246,7 +246,7 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) { // The Nextion will ignore the upload command if it is sleeping ESP_LOGV(TAG, "Wake-up"); - this->ignore_is_setup_ = true; + this->connection_state_.ignore_is_setup_ = true; this->send_command_("sleep=0"); this->send_command_("dim=100"); vTaskDelay(pdMS_TO_TICKS(250)); // NOLINT diff --git a/esphome/components/rp2040/helpers.cpp b/esphome/components/rp2040/helpers.cpp new file mode 100644 index 0000000000..a6eac58dc6 --- /dev/null +++ b/esphome/components/rp2040/helpers.cpp @@ -0,0 +1,55 @@ +#include "esphome/core/helpers.h" +#include "esphome/core/defines.h" + +#ifdef USE_RP2040 + +#include "esphome/core/hal.h" + +#if defined(USE_WIFI) +#include +#endif +#include +#include + +namespace esphome { + +uint32_t random_uint32() { + uint32_t result = 0; + for (uint8_t i = 0; i < 32; i++) { + result <<= 1; + result |= rosc_hw->randombit; + } + return result; +} + +bool random_bytes(uint8_t *data, size_t len) { + while (len-- != 0) { + uint8_t result = 0; + for (uint8_t i = 0; i < 8; i++) { + result <<= 1; + result |= rosc_hw->randombit; + } + *data++ = result; + } + return true; +} + +// RP2040 doesn't have mutexes, but that shouldn't be an issue as it's single-core and non-preemptive OS. +Mutex::Mutex() {} +Mutex::~Mutex() {} +void Mutex::lock() {} +bool Mutex::try_lock() { return true; } +void Mutex::unlock() {} + +IRAM_ATTR InterruptLock::InterruptLock() { state_ = save_and_disable_interrupts(); } +IRAM_ATTR InterruptLock::~InterruptLock() { restore_interrupts(state_); } + +void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter) +#ifdef USE_WIFI + WiFi.macAddress(mac); +#endif +} + +} // namespace esphome + +#endif // USE_RP2040 diff --git a/esphome/components/scd4x/scd4x.cpp b/esphome/components/scd4x/scd4x.cpp index f617ffe276..06db70e3f3 100644 --- a/esphome/components/scd4x/scd4x.cpp +++ b/esphome/components/scd4x/scd4x.cpp @@ -7,6 +7,8 @@ namespace scd4x { static const char *const TAG = "scd4x"; +static const uint16_t SCD41_ID = 0x1408; +static const uint16_t SCD40_ID = 0x440; static const uint16_t SCD4X_CMD_GET_SERIAL_NUMBER = 0x3682; static const uint16_t SCD4X_CMD_TEMPERATURE_OFFSET = 0x241d; static const uint16_t SCD4X_CMD_ALTITUDE_COMPENSATION = 0x2427; @@ -23,8 +25,6 @@ static const uint16_t SCD4X_CMD_STOP_MEASUREMENTS = 0x3f86; static const uint16_t SCD4X_CMD_FACTORY_RESET = 0x3632; static const uint16_t SCD4X_CMD_GET_FEATURESET = 0x202f; static const float SCD4X_TEMPERATURE_OFFSET_MULTIPLIER = (1 << 16) / 175.0f; -static const uint16_t SCD41_ID = 0x1408; -static const uint16_t SCD40_ID = 0x440; void SCD4XComponent::setup() { ESP_LOGCONFIG(TAG, "Running setup"); @@ -51,47 +51,66 @@ void SCD4XComponent::setup() { if (!this->write_command(SCD4X_CMD_TEMPERATURE_OFFSET, (uint16_t) (temperature_offset_ * SCD4X_TEMPERATURE_OFFSET_MULTIPLIER))) { - ESP_LOGE(TAG, "Error setting temperature offset."); + ESP_LOGE(TAG, "Error setting temperature offset"); this->error_code_ = MEASUREMENT_INIT_FAILED; this->mark_failed(); return; } - // If pressure compensation available use it - // else use altitude - if (ambient_pressure_compensation_) { - if (!this->update_ambient_pressure_compensation_(ambient_pressure_)) { - ESP_LOGE(TAG, "Error setting ambient pressure compensation."); + // If pressure compensation available use it, else use altitude + if (this->ambient_pressure_) { + if (!this->update_ambient_pressure_compensation_(this->ambient_pressure_)) { + ESP_LOGE(TAG, "Error setting ambient pressure compensation"); this->error_code_ = MEASUREMENT_INIT_FAILED; this->mark_failed(); return; } } else { - if (!this->write_command(SCD4X_CMD_ALTITUDE_COMPENSATION, altitude_compensation_)) { - ESP_LOGE(TAG, "Error setting altitude compensation."); + if (!this->write_command(SCD4X_CMD_ALTITUDE_COMPENSATION, this->altitude_compensation_)) { + ESP_LOGE(TAG, "Error setting altitude compensation"); this->error_code_ = MEASUREMENT_INIT_FAILED; this->mark_failed(); return; } } - if (!this->write_command(SCD4X_CMD_AUTOMATIC_SELF_CALIBRATION, enable_asc_ ? 1 : 0)) { - ESP_LOGE(TAG, "Error setting automatic self calibration."); + if (!this->write_command(SCD4X_CMD_AUTOMATIC_SELF_CALIBRATION, this->enable_asc_ ? 1 : 0)) { + ESP_LOGE(TAG, "Error setting automatic self calibration"); this->error_code_ = MEASUREMENT_INIT_FAILED; this->mark_failed(); return; } - initialized_ = true; + this->initialized_ = true; // Finally start sensor measurements this->start_measurement_(); - ESP_LOGD(TAG, "Sensor initialized"); }); }); } void SCD4XComponent::dump_config() { - ESP_LOGCONFIG(TAG, "scd4x:"); + static const char *const MM_PERIODIC_STR = "Periodic (5s)"; + static const char *const MM_LOW_POWER_PERIODIC_STR = "Low power periodic (30s)"; + static const char *const MM_SINGLE_SHOT_STR = "Single shot"; + static const char *const MM_SINGLE_SHOT_RHT_ONLY_STR = "Single shot rht only"; + const char *measurement_mode_str = MM_PERIODIC_STR; + + switch (this->measurement_mode_) { + case PERIODIC: + // measurement_mode_str = MM_PERIODIC_STR; + break; + case LOW_POWER_PERIODIC: + measurement_mode_str = MM_LOW_POWER_PERIODIC_STR; + break; + case SINGLE_SHOT: + measurement_mode_str = MM_SINGLE_SHOT_STR; + break; + case SINGLE_SHOT_RHT_ONLY: + measurement_mode_str = MM_SINGLE_SHOT_RHT_ONLY_STR; + break; + } + + ESP_LOGCONFIG(TAG, "SCD4X:"); LOG_I2C_DEVICE(this); if (this->is_failed()) { switch (this->error_code_) { @@ -102,19 +121,23 @@ void SCD4XComponent::dump_config() { ESP_LOGW(TAG, "Measurement Initialization failed"); break; case SERIAL_NUMBER_IDENTIFICATION_FAILED: - ESP_LOGW(TAG, "Unable to read sensor firmware version"); + ESP_LOGW(TAG, "Unable to read firmware version"); break; default: ESP_LOGW(TAG, "Unknown setup error"); break; } } - ESP_LOGCONFIG(TAG, " Automatic self calibration: %s", ONOFF(this->enable_asc_)); + ESP_LOGCONFIG(TAG, + " Automatic self calibration: %s\n" + " Measurement mode: %s\n" + " Temperature offset: %.2f °C", + ONOFF(this->enable_asc_), measurement_mode_str, this->temperature_offset_); if (this->ambient_pressure_source_ != nullptr) { - ESP_LOGCONFIG(TAG, " Dynamic ambient pressure compensation using sensor '%s'", + ESP_LOGCONFIG(TAG, " Dynamic ambient pressure compensation using '%s'", this->ambient_pressure_source_->get_name().c_str()); } else { - if (this->ambient_pressure_compensation_) { + if (this->ambient_pressure_) { ESP_LOGCONFIG(TAG, " Altitude compensation disabled\n" " Ambient pressure compensation: %dmBar", @@ -126,21 +149,6 @@ void SCD4XComponent::dump_config() { this->altitude_compensation_); } } - switch (this->measurement_mode_) { - case PERIODIC: - ESP_LOGCONFIG(TAG, " Measurement mode: periodic (5s)"); - break; - case LOW_POWER_PERIODIC: - ESP_LOGCONFIG(TAG, " Measurement mode: low power periodic (30s)"); - break; - case SINGLE_SHOT: - ESP_LOGCONFIG(TAG, " Measurement mode: single shot"); - break; - case SINGLE_SHOT_RHT_ONLY: - ESP_LOGCONFIG(TAG, " Measurement mode: single shot rht only"); - break; - } - ESP_LOGCONFIG(TAG, " Temperature offset: %.2f °C", this->temperature_offset_); LOG_UPDATE_INTERVAL(this); LOG_SENSOR(" ", "CO2", this->co2_sensor_); LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); @@ -148,20 +156,20 @@ void SCD4XComponent::dump_config() { } void SCD4XComponent::update() { - if (!initialized_) { + if (!this->initialized_) { return; } if (this->ambient_pressure_source_ != nullptr) { float pressure = this->ambient_pressure_source_->state; if (!std::isnan(pressure)) { - set_ambient_pressure_compensation(pressure); + this->set_ambient_pressure_compensation(pressure); } } uint32_t wait_time = 0; if (this->measurement_mode_ == SINGLE_SHOT || this->measurement_mode_ == SINGLE_SHOT_RHT_ONLY) { - start_measurement_(); + this->start_measurement_(); wait_time = this->measurement_mode_ == SINGLE_SHOT ? 5000 : 50; // Single shot measurement takes 5 secs rht mode 50 ms } @@ -176,12 +184,12 @@ void SCD4XComponent::update() { if (!this->read_data(raw_read_status) || raw_read_status == 0x00) { this->status_set_warning(); - ESP_LOGW(TAG, "Data not ready yet!"); + ESP_LOGW(TAG, "Data not ready"); return; } if (!this->write_command(SCD4X_CMD_READ_MEASUREMENT)) { - ESP_LOGW(TAG, "Error reading measurement!"); + ESP_LOGW(TAG, "Error reading measurement"); this->status_set_warning(); return; // NO RETRY } @@ -218,19 +226,19 @@ bool SCD4XComponent::perform_forced_calibration(uint16_t current_co2_concentrati } this->set_timeout(500, [this, current_co2_concentration]() { if (this->write_command(SCD4X_CMD_PERFORM_FORCED_CALIBRATION, current_co2_concentration)) { - ESP_LOGD(TAG, "setting forced calibration Co2 level %d ppm", current_co2_concentration); + ESP_LOGD(TAG, "Setting forced calibration Co2 level %d ppm", current_co2_concentration); // frc takes 400 ms // because this method will be used very rarly // the simple approach with delay is ok - delay(400); // NOLINT' + delay(400); // NOLINT if (!this->start_measurement_()) { return false; } else { - ESP_LOGD(TAG, "forced calibration complete"); + ESP_LOGD(TAG, "Forced calibration complete"); } return true; } else { - ESP_LOGE(TAG, "force calibration failed"); + ESP_LOGE(TAG, "Force calibration failed"); this->error_code_ = FRC_FAILED; this->status_set_warning(); return false; @@ -259,27 +267,26 @@ bool SCD4XComponent::factory_reset() { } void SCD4XComponent::set_ambient_pressure_compensation(float pressure_in_hpa) { - ambient_pressure_compensation_ = true; - uint16_t new_ambient_pressure = (uint16_t) pressure_in_hpa; - if (!initialized_) { - ambient_pressure_ = new_ambient_pressure; + uint16_t new_ambient_pressure = static_cast(pressure_in_hpa); + if (!this->initialized_) { + this->ambient_pressure_ = new_ambient_pressure; return; } // Only send pressure value if it has changed since last update - if (new_ambient_pressure != ambient_pressure_) { - update_ambient_pressure_compensation_(new_ambient_pressure); - ambient_pressure_ = new_ambient_pressure; + if (new_ambient_pressure != this->ambient_pressure_) { + this->update_ambient_pressure_compensation_(new_ambient_pressure); + this->ambient_pressure_ = new_ambient_pressure; } else { - ESP_LOGD(TAG, "ambient pressure compensation skipped - no change required"); + ESP_LOGD(TAG, "Ambient pressure compensation skipped; no change required"); } } bool SCD4XComponent::update_ambient_pressure_compensation_(uint16_t pressure_in_hpa) { if (this->write_command(SCD4X_CMD_AMBIENT_PRESSURE_COMPENSATION, pressure_in_hpa)) { - ESP_LOGD(TAG, "setting ambient pressure compensation to %d hPa", pressure_in_hpa); + ESP_LOGD(TAG, "Setting ambient pressure compensation to %d hPa", pressure_in_hpa); return true; } else { - ESP_LOGE(TAG, "Error setting ambient pressure compensation."); + ESP_LOGE(TAG, "Error setting ambient pressure compensation"); return false; } } @@ -304,7 +311,7 @@ bool SCD4XComponent::start_measurement_() { static uint8_t remaining_retries = 3; while (remaining_retries) { if (!this->write_command(measurement_command)) { - ESP_LOGE(TAG, "Error starting measurements."); + ESP_LOGE(TAG, "Error starting measurements"); this->error_code_ = MEASUREMENT_INIT_FAILED; this->status_set_warning(); if (--remaining_retries == 0) diff --git a/esphome/components/scd4x/scd4x.h b/esphome/components/scd4x/scd4x.h index f2efb28ac1..ab5d72aeec 100644 --- a/esphome/components/scd4x/scd4x.h +++ b/esphome/components/scd4x/scd4x.h @@ -8,14 +8,20 @@ namespace esphome { namespace scd4x { -enum ERRORCODE { +enum ErrorCode : uint8_t { COMMUNICATION_FAILED, SERIAL_NUMBER_IDENTIFICATION_FAILED, MEASUREMENT_INIT_FAILED, FRC_FAILED, - UNKNOWN + UNKNOWN, +}; + +enum MeasurementMode : uint8_t { + PERIODIC, + LOW_POWER_PERIODIC, + SINGLE_SHOT, + SINGLE_SHOT_RHT_ONLY, }; -enum MeasurementMode { PERIODIC, LOW_POWER_PERIODIC, SINGLE_SHOT, SINGLE_SHOT_RHT_ONLY }; class SCD4XComponent : public PollingComponent, public sensirion_common::SensirionI2CDevice { public: @@ -39,21 +45,18 @@ class SCD4XComponent : public PollingComponent, public sensirion_common::Sensiri protected: bool update_ambient_pressure_compensation_(uint16_t pressure_in_hpa); bool start_measurement_(); - ERRORCODE error_code_; - bool initialized_{false}; - - float temperature_offset_; - uint16_t altitude_compensation_; - bool ambient_pressure_compensation_; - uint16_t ambient_pressure_; - bool enable_asc_; - MeasurementMode measurement_mode_{PERIODIC}; sensor::Sensor *co2_sensor_{nullptr}; sensor::Sensor *temperature_sensor_{nullptr}; sensor::Sensor *humidity_sensor_{nullptr}; - // used for compensation - sensor::Sensor *ambient_pressure_source_{nullptr}; + sensor::Sensor *ambient_pressure_source_{nullptr}; // used for compensation + float temperature_offset_; + uint16_t altitude_compensation_{0}; + uint16_t ambient_pressure_{0}; // Per datasheet, valid values are 700 to 1200 hPa; 0 is a valid sentinel value + bool initialized_{false}; + bool enable_asc_{false}; + ErrorCode error_code_; + MeasurementMode measurement_mode_{PERIODIC}; }; } // namespace scd4x diff --git a/esphome/components/sx126x/__init__.py b/esphome/components/sx126x/__init__.py new file mode 100644 index 0000000000..492febe283 --- /dev/null +++ b/esphome/components/sx126x/__init__.py @@ -0,0 +1,317 @@ +from esphome import automation, pins +import esphome.codegen as cg +from esphome.components import spi +import esphome.config_validation as cv +from esphome.const import CONF_BUSY_PIN, CONF_DATA, CONF_FREQUENCY, CONF_ID +from esphome.core import TimePeriod + +MULTI_CONF = True +CODEOWNERS = ["@swoboda1337"] +DEPENDENCIES = ["spi"] + +CONF_SX126X_ID = "sx126x_id" + +CONF_BANDWIDTH = "bandwidth" +CONF_BITRATE = "bitrate" +CONF_CODING_RATE = "coding_rate" +CONF_CRC_ENABLE = "crc_enable" +CONF_DEVIATION = "deviation" +CONF_DIO1_PIN = "dio1_pin" +CONF_HW_VERSION = "hw_version" +CONF_MODULATION = "modulation" +CONF_ON_PACKET = "on_packet" +CONF_PA_POWER = "pa_power" +CONF_PA_RAMP = "pa_ramp" +CONF_PAYLOAD_LENGTH = "payload_length" +CONF_PREAMBLE_DETECT = "preamble_detect" +CONF_PREAMBLE_SIZE = "preamble_size" +CONF_RST_PIN = "rst_pin" +CONF_RX_START = "rx_start" +CONF_RF_SWITCH = "rf_switch" +CONF_SHAPING = "shaping" +CONF_SPREADING_FACTOR = "spreading_factor" +CONF_SYNC_VALUE = "sync_value" +CONF_TCXO_VOLTAGE = "tcxo_voltage" +CONF_TCXO_DELAY = "tcxo_delay" + +sx126x_ns = cg.esphome_ns.namespace("sx126x") +SX126x = sx126x_ns.class_("SX126x", cg.Component, spi.SPIDevice) +SX126xListener = sx126x_ns.class_("SX126xListener") +SX126xBw = sx126x_ns.enum("SX126xBw") +SX126xPacketType = sx126x_ns.enum("SX126xPacketType") +SX126xTcxoCtrl = sx126x_ns.enum("SX126xTcxoCtrl") +SX126xRampTime = sx126x_ns.enum("SX126xRampTime") +SX126xPulseShape = sx126x_ns.enum("SX126xPulseShape") +SX126xLoraCr = sx126x_ns.enum("SX126xLoraCr") + +BW = { + "4_8kHz": SX126xBw.SX126X_BW_4800, + "5_8kHz": SX126xBw.SX126X_BW_5800, + "7_3kHz": SX126xBw.SX126X_BW_7300, + "9_7kHz": SX126xBw.SX126X_BW_9700, + "11_7kHz": SX126xBw.SX126X_BW_11700, + "14_6kHz": SX126xBw.SX126X_BW_14600, + "19_5kHz": SX126xBw.SX126X_BW_19500, + "23_4kHz": SX126xBw.SX126X_BW_23400, + "29_3kHz": SX126xBw.SX126X_BW_29300, + "39_0kHz": SX126xBw.SX126X_BW_39000, + "46_9kHz": SX126xBw.SX126X_BW_46900, + "58_6kHz": SX126xBw.SX126X_BW_58600, + "78_2kHz": SX126xBw.SX126X_BW_78200, + "93_8kHz": SX126xBw.SX126X_BW_93800, + "117_3kHz": SX126xBw.SX126X_BW_117300, + "156_2kHz": SX126xBw.SX126X_BW_156200, + "187_2kHz": SX126xBw.SX126X_BW_187200, + "234_3kHz": SX126xBw.SX126X_BW_234300, + "312_0kHz": SX126xBw.SX126X_BW_312000, + "373_6kHz": SX126xBw.SX126X_BW_373600, + "467_0kHz": SX126xBw.SX126X_BW_467000, + "7_8kHz": SX126xBw.SX126X_BW_7810, + "10_4kHz": SX126xBw.SX126X_BW_10420, + "15_6kHz": SX126xBw.SX126X_BW_15630, + "20_8kHz": SX126xBw.SX126X_BW_20830, + "31_3kHz": SX126xBw.SX126X_BW_31250, + "41_7kHz": SX126xBw.SX126X_BW_41670, + "62_5kHz": SX126xBw.SX126X_BW_62500, + "125_0kHz": SX126xBw.SX126X_BW_125000, + "250_0kHz": SX126xBw.SX126X_BW_250000, + "500_0kHz": SX126xBw.SX126X_BW_500000, +} + +CODING_RATE = { + "CR_4_5": SX126xLoraCr.LORA_CR_4_5, + "CR_4_6": SX126xLoraCr.LORA_CR_4_6, + "CR_4_7": SX126xLoraCr.LORA_CR_4_7, + "CR_4_8": SX126xLoraCr.LORA_CR_4_8, +} + +MOD = { + "LORA": SX126xPacketType.PACKET_TYPE_LORA, + "FSK": SX126xPacketType.PACKET_TYPE_GFSK, +} + +TCXO_VOLTAGE = { + "1_6V": SX126xTcxoCtrl.TCXO_CTRL_1_6V, + "1_7V": SX126xTcxoCtrl.TCXO_CTRL_1_7V, + "1_8V": SX126xTcxoCtrl.TCXO_CTRL_1_8V, + "2_2V": SX126xTcxoCtrl.TCXO_CTRL_2_2V, + "2_4V": SX126xTcxoCtrl.TCXO_CTRL_2_4V, + "2_7V": SX126xTcxoCtrl.TCXO_CTRL_2_7V, + "3_0V": SX126xTcxoCtrl.TCXO_CTRL_3_0V, + "3_3V": SX126xTcxoCtrl.TCXO_CTRL_3_3V, + "NONE": SX126xTcxoCtrl.TCXO_CTRL_NONE, +} + +RAMP = { + "10us": SX126xRampTime.PA_RAMP_10, + "20us": SX126xRampTime.PA_RAMP_20, + "40us": SX126xRampTime.PA_RAMP_40, + "80us": SX126xRampTime.PA_RAMP_80, + "200us": SX126xRampTime.PA_RAMP_200, + "800us": SX126xRampTime.PA_RAMP_800, + "1700us": SX126xRampTime.PA_RAMP_1700, + "3400us": SX126xRampTime.PA_RAMP_3400, +} + +SHAPING = { + "GAUSSIAN_BT_0_3": SX126xPulseShape.GAUSSIAN_BT_0_3, + "GAUSSIAN_BT_0_5": SX126xPulseShape.GAUSSIAN_BT_0_5, + "GAUSSIAN_BT_0_7": SX126xPulseShape.GAUSSIAN_BT_0_7, + "GAUSSIAN_BT_1_0": SX126xPulseShape.GAUSSIAN_BT_1_0, + "NONE": SX126xPulseShape.NO_FILTER, +} + +RunImageCalAction = sx126x_ns.class_( + "RunImageCalAction", automation.Action, cg.Parented.template(SX126x) +) +SendPacketAction = sx126x_ns.class_( + "SendPacketAction", automation.Action, cg.Parented.template(SX126x) +) +SetModeTxAction = sx126x_ns.class_( + "SetModeTxAction", automation.Action, cg.Parented.template(SX126x) +) +SetModeRxAction = sx126x_ns.class_( + "SetModeRxAction", automation.Action, cg.Parented.template(SX126x) +) +SetModeSleepAction = sx126x_ns.class_( + "SetModeSleepAction", automation.Action, cg.Parented.template(SX126x) +) +SetModeStandbyAction = sx126x_ns.class_( + "SetModeStandbyAction", automation.Action, cg.Parented.template(SX126x) +) + + +def validate_raw_data(value): + if isinstance(value, str): + return value.encode("utf-8") + if isinstance(value, list): + return cv.Schema([cv.hex_uint8_t])(value) + raise cv.Invalid( + "data must either be a string wrapped in quotes or a list of bytes" + ) + + +def validate_config(config): + lora_bws = [ + "7_8kHz", + "10_4kHz", + "15_6kHz", + "20_8kHz", + "31_3kHz", + "41_7kHz", + "62_5kHz", + "125_0kHz", + "250_0kHz", + "500_0kHz", + ] + if config[CONF_MODULATION] == "LORA": + if config[CONF_BANDWIDTH] not in lora_bws: + raise cv.Invalid(f"{config[CONF_BANDWIDTH]} is not available with LORA") + if config[CONF_PREAMBLE_SIZE] > 0 and config[CONF_PREAMBLE_SIZE] < 6: + raise cv.Invalid("Minimum preamble size is 6 with LORA") + if config[CONF_SPREADING_FACTOR] == 6 and config[CONF_PAYLOAD_LENGTH] == 0: + raise cv.Invalid("Payload length must be set when spreading factor is 6") + else: + if config[CONF_BANDWIDTH] in lora_bws: + raise cv.Invalid(f"{config[CONF_BANDWIDTH]} is not available with FSK") + if config[CONF_PREAMBLE_DETECT] > len(config[CONF_SYNC_VALUE]): + raise cv.Invalid("Preamble detection length must be <= sync value length") + return config + + +CONFIG_SCHEMA = ( + cv.Schema( + { + cv.GenerateID(): cv.declare_id(SX126x), + cv.Optional(CONF_BANDWIDTH, default="125_0kHz"): cv.enum(BW), + cv.Optional(CONF_BITRATE, default=4800): cv.int_range(min=600, max=300000), + cv.Required(CONF_BUSY_PIN): pins.internal_gpio_input_pin_schema, + cv.Optional(CONF_CODING_RATE, default="CR_4_5"): cv.enum(CODING_RATE), + cv.Optional(CONF_CRC_ENABLE, default=False): cv.boolean, + cv.Optional(CONF_DEVIATION, default=5000): cv.int_range(min=0, max=100000), + cv.Required(CONF_DIO1_PIN): pins.internal_gpio_input_pin_schema, + cv.Required(CONF_FREQUENCY): cv.int_range(min=137000000, max=1020000000), + cv.Required(CONF_HW_VERSION): cv.one_of( + "sx1261", "sx1262", "sx1268", "llcc68", lower=True + ), + cv.Required(CONF_MODULATION): cv.enum(MOD), + cv.Optional(CONF_ON_PACKET): automation.validate_automation(single=True), + cv.Optional(CONF_PA_POWER, default=17): cv.int_range(min=-3, max=22), + cv.Optional(CONF_PA_RAMP, default="40us"): cv.enum(RAMP), + cv.Optional(CONF_PAYLOAD_LENGTH, default=0): cv.int_range(min=0, max=256), + cv.Optional(CONF_PREAMBLE_DETECT, default=2): cv.int_range(min=0, max=4), + cv.Required(CONF_PREAMBLE_SIZE): cv.int_range(min=1, max=65535), + cv.Required(CONF_RST_PIN): pins.internal_gpio_output_pin_schema, + cv.Optional(CONF_RX_START, default=True): cv.boolean, + cv.Required(CONF_RF_SWITCH): cv.boolean, + cv.Optional(CONF_SHAPING, default="NONE"): cv.enum(SHAPING), + cv.Optional(CONF_SPREADING_FACTOR, default=7): cv.int_range(min=6, max=12), + cv.Optional(CONF_SYNC_VALUE, default=[]): cv.ensure_list(cv.hex_uint8_t), + cv.Optional(CONF_TCXO_VOLTAGE, default="NONE"): cv.enum(TCXO_VOLTAGE), + cv.Optional(CONF_TCXO_DELAY, default="5ms"): cv.All( + cv.positive_time_period_microseconds, + cv.Range(max=TimePeriod(microseconds=262144000)), + ), + }, + ) + .extend(cv.COMPONENT_SCHEMA) + .extend(spi.spi_device_schema(True, 8e6, "mode0")) + .add_extra(validate_config) +) + + +async def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + await cg.register_component(var, config) + await spi.register_spi_device(var, config) + if CONF_ON_PACKET in config: + await automation.build_automation( + var.get_packet_trigger(), + [ + (cg.std_vector.template(cg.uint8), "x"), + (cg.float_, "rssi"), + (cg.float_, "snr"), + ], + config[CONF_ON_PACKET], + ) + if CONF_DIO1_PIN in config: + dio1_pin = await cg.gpio_pin_expression(config[CONF_DIO1_PIN]) + cg.add(var.set_dio1_pin(dio1_pin)) + rst_pin = await cg.gpio_pin_expression(config[CONF_RST_PIN]) + cg.add(var.set_rst_pin(rst_pin)) + busy_pin = await cg.gpio_pin_expression(config[CONF_BUSY_PIN]) + cg.add(var.set_busy_pin(busy_pin)) + cg.add(var.set_bandwidth(config[CONF_BANDWIDTH])) + cg.add(var.set_frequency(config[CONF_FREQUENCY])) + cg.add(var.set_hw_version(config[CONF_HW_VERSION])) + cg.add(var.set_deviation(config[CONF_DEVIATION])) + cg.add(var.set_modulation(config[CONF_MODULATION])) + cg.add(var.set_pa_ramp(config[CONF_PA_RAMP])) + cg.add(var.set_pa_power(config[CONF_PA_POWER])) + cg.add(var.set_shaping(config[CONF_SHAPING])) + cg.add(var.set_bitrate(config[CONF_BITRATE])) + cg.add(var.set_crc_enable(config[CONF_CRC_ENABLE])) + cg.add(var.set_payload_length(config[CONF_PAYLOAD_LENGTH])) + cg.add(var.set_preamble_size(config[CONF_PREAMBLE_SIZE])) + cg.add(var.set_preamble_detect(config[CONF_PREAMBLE_DETECT])) + cg.add(var.set_coding_rate(config[CONF_CODING_RATE])) + cg.add(var.set_spreading_factor(config[CONF_SPREADING_FACTOR])) + cg.add(var.set_sync_value(config[CONF_SYNC_VALUE])) + cg.add(var.set_rx_start(config[CONF_RX_START])) + cg.add(var.set_rf_switch(config[CONF_RF_SWITCH])) + cg.add(var.set_tcxo_voltage(config[CONF_TCXO_VOLTAGE])) + cg.add(var.set_tcxo_delay(config[CONF_TCXO_DELAY])) + + +NO_ARGS_ACTION_SCHEMA = automation.maybe_simple_id( + { + cv.GenerateID(): cv.use_id(SX126x), + } +) + + +@automation.register_action( + "sx126x.run_image_cal", RunImageCalAction, NO_ARGS_ACTION_SCHEMA +) +@automation.register_action( + "sx126x.set_mode_tx", SetModeTxAction, NO_ARGS_ACTION_SCHEMA +) +@automation.register_action( + "sx126x.set_mode_rx", SetModeRxAction, NO_ARGS_ACTION_SCHEMA +) +@automation.register_action( + "sx126x.set_mode_sleep", SetModeSleepAction, NO_ARGS_ACTION_SCHEMA +) +@automation.register_action( + "sx126x.set_mode_standby", SetModeStandbyAction, NO_ARGS_ACTION_SCHEMA +) +async def no_args_action_to_code(config, action_id, template_arg, args): + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + return var + + +SEND_PACKET_ACTION_SCHEMA = cv.maybe_simple_value( + { + cv.GenerateID(): cv.use_id(SX126x), + cv.Required(CONF_DATA): cv.templatable(validate_raw_data), + }, + key=CONF_DATA, +) + + +@automation.register_action( + "sx126x.send_packet", SendPacketAction, SEND_PACKET_ACTION_SCHEMA +) +async def send_packet_action_to_code(config, action_id, template_arg, args): + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + data = config[CONF_DATA] + if isinstance(data, bytes): + data = list(data) + if cg.is_template(data): + templ = await cg.templatable(data, args, cg.std_vector.template(cg.uint8)) + cg.add(var.set_data_template(templ)) + else: + cg.add(var.set_data_static(data)) + return var diff --git a/esphome/components/sx126x/automation.h b/esphome/components/sx126x/automation.h new file mode 100644 index 0000000000..520ef99718 --- /dev/null +++ b/esphome/components/sx126x/automation.h @@ -0,0 +1,62 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/core/automation.h" +#include "esphome/components/sx126x/sx126x.h" + +namespace esphome { +namespace sx126x { + +template class RunImageCalAction : public Action, public Parented { + public: + void play(Ts... x) override { this->parent_->run_image_cal(); } +}; + +template class SendPacketAction : public Action, public Parented { + public: + void set_data_template(std::function(Ts...)> func) { + this->data_func_ = func; + this->static_ = false; + } + + void set_data_static(const std::vector &data) { + this->data_static_ = data; + this->static_ = true; + } + + void play(Ts... x) override { + if (this->static_) { + this->parent_->transmit_packet(this->data_static_); + } else { + this->parent_->transmit_packet(this->data_func_(x...)); + } + } + + protected: + bool static_{false}; + std::function(Ts...)> data_func_{}; + std::vector data_static_{}; +}; + +template class SetModeTxAction : public Action, public Parented { + public: + void play(Ts... x) override { this->parent_->set_mode_tx(); } +}; + +template class SetModeRxAction : public Action, public Parented { + public: + void play(Ts... x) override { this->parent_->set_mode_rx(); } +}; + +template class SetModeSleepAction : public Action, public Parented { + public: + void play(Ts... x) override { this->parent_->set_mode_sleep(); } +}; + +template class SetModeStandbyAction : public Action, public Parented { + public: + void play(Ts... x) override { this->parent_->set_mode_standby(STDBY_XOSC); } +}; + +} // namespace sx126x +} // namespace esphome diff --git a/esphome/components/sx126x/packet_transport/__init__.py b/esphome/components/sx126x/packet_transport/__init__.py new file mode 100644 index 0000000000..4d79b23ac1 --- /dev/null +++ b/esphome/components/sx126x/packet_transport/__init__.py @@ -0,0 +1,26 @@ +import esphome.codegen as cg +from esphome.components.packet_transport import ( + PacketTransport, + new_packet_transport, + transport_schema, +) +import esphome.config_validation as cv +from esphome.cpp_types import PollingComponent + +from .. import CONF_SX126X_ID, SX126x, SX126xListener, sx126x_ns + +SX126xTransport = sx126x_ns.class_( + "SX126xTransport", PacketTransport, PollingComponent, SX126xListener +) + +CONFIG_SCHEMA = transport_schema(SX126xTransport).extend( + { + cv.GenerateID(CONF_SX126X_ID): cv.use_id(SX126x), + } +) + + +async def to_code(config): + var, _ = await new_packet_transport(config) + sx126x = await cg.get_variable(config[CONF_SX126X_ID]) + cg.add(var.set_parent(sx126x)) diff --git a/esphome/components/sx126x/packet_transport/sx126x_transport.cpp b/esphome/components/sx126x/packet_transport/sx126x_transport.cpp new file mode 100644 index 0000000000..2cfc4b700e --- /dev/null +++ b/esphome/components/sx126x/packet_transport/sx126x_transport.cpp @@ -0,0 +1,26 @@ +#include "esphome/core/log.h" +#include "esphome/core/application.h" +#include "sx126x_transport.h" + +namespace esphome { +namespace sx126x { + +static const char *const TAG = "sx126x_transport"; + +void SX126xTransport::setup() { + PacketTransport::setup(); + this->parent_->register_listener(this); +} + +void SX126xTransport::update() { + PacketTransport::update(); + this->updated_ = true; + this->resend_data_ = true; +} + +void SX126xTransport::send_packet(const std::vector &buf) const { this->parent_->transmit_packet(buf); } + +void SX126xTransport::on_packet(const std::vector &packet, float rssi, float snr) { this->process_(packet); } + +} // namespace sx126x +} // namespace esphome diff --git a/esphome/components/sx126x/packet_transport/sx126x_transport.h b/esphome/components/sx126x/packet_transport/sx126x_transport.h new file mode 100644 index 0000000000..755d30417d --- /dev/null +++ b/esphome/components/sx126x/packet_transport/sx126x_transport.h @@ -0,0 +1,25 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/components/sx126x/sx126x.h" +#include "esphome/components/packet_transport/packet_transport.h" +#include + +namespace esphome { +namespace sx126x { + +class SX126xTransport : public packet_transport::PacketTransport, public Parented, public SX126xListener { + public: + void setup() override; + void update() override; + void on_packet(const std::vector &packet, float rssi, float snr) override; + float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } + + protected: + void send_packet(const std::vector &buf) const override; + bool should_send() override { return true; } + size_t get_max_packet_size() override { return this->parent_->get_max_packet_size(); } +}; + +} // namespace sx126x +} // namespace esphome diff --git a/esphome/components/sx126x/sx126x.cpp b/esphome/components/sx126x/sx126x.cpp new file mode 100644 index 0000000000..b1c81b324a --- /dev/null +++ b/esphome/components/sx126x/sx126x.cpp @@ -0,0 +1,523 @@ +#include "sx126x.h" +#include "esphome/core/hal.h" +#include "esphome/core/log.h" + +namespace esphome { +namespace sx126x { + +static const char *const TAG = "sx126x"; +static const uint16_t RAMP[8] = {10, 20, 40, 80, 200, 800, 1700, 3400}; +static const uint32_t BW_HZ[31] = {4800, 5800, 7300, 9700, 11700, 14600, 19500, 23400, 29300, 39000, 46900, + 58600, 78200, 93800, 117300, 156200, 187200, 234300, 312000, 373600, 467000, 7810, + 10420, 15630, 20830, 31250, 41670, 62500, 125000, 250000, 500000}; +static const uint8_t BW_LORA[10] = {LORA_BW_7810, LORA_BW_10420, LORA_BW_15630, LORA_BW_20830, LORA_BW_31250, + LORA_BW_41670, LORA_BW_62500, LORA_BW_125000, LORA_BW_250000, LORA_BW_500000}; +static const uint8_t BW_FSK[21] = { + FSK_BW_4800, FSK_BW_5800, FSK_BW_7300, FSK_BW_9700, FSK_BW_11700, FSK_BW_14600, FSK_BW_19500, + FSK_BW_23400, FSK_BW_29300, FSK_BW_39000, FSK_BW_46900, FSK_BW_58600, FSK_BW_78200, FSK_BW_93800, + FSK_BW_117300, FSK_BW_156200, FSK_BW_187200, FSK_BW_234300, FSK_BW_312000, FSK_BW_373600, FSK_BW_467000}; + +static constexpr uint32_t RESET_DELAY_HIGH_US = 5000; +static constexpr uint32_t RESET_DELAY_LOW_US = 2000; +static constexpr uint32_t SWITCHING_DELAY_US = 1; +static constexpr uint32_t TRANSMIT_TIMEOUT_MS = 4000; +static constexpr uint32_t BUSY_TIMEOUT_MS = 20; + +// OCP (Over Current Protection) values +static constexpr uint8_t OCP_80MA = 0x18; // 80 mA max current +static constexpr uint8_t OCP_140MA = 0x38; // 140 mA max current + +// LoRa low data rate optimization threshold +static constexpr float LOW_DATA_RATE_OPTIMIZE_THRESHOLD = 16.38f; // 16.38 ms + +uint8_t SX126x::read_fifo_(uint8_t offset, std::vector &packet) { + this->wait_busy_(); + this->enable(); + this->transfer_byte(RADIO_READ_BUFFER); + this->transfer_byte(offset); + uint8_t status = this->transfer_byte(0x00); + for (uint8_t &byte : packet) { + byte = this->transfer_byte(0x00); + } + this->disable(); + return status; +} + +void SX126x::write_fifo_(uint8_t offset, const std::vector &packet) { + this->wait_busy_(); + this->enable(); + this->transfer_byte(RADIO_WRITE_BUFFER); + this->transfer_byte(offset); + for (const uint8_t &byte : packet) { + this->transfer_byte(byte); + } + this->disable(); + delayMicroseconds(SWITCHING_DELAY_US); +} + +uint8_t SX126x::read_opcode_(uint8_t opcode, uint8_t *data, uint8_t size) { + this->wait_busy_(); + this->enable(); + this->transfer_byte(opcode); + uint8_t status = this->transfer_byte(0x00); + for (int32_t i = 0; i < size; i++) { + data[i] = this->transfer_byte(0x00); + } + this->disable(); + return status; +} + +void SX126x::write_opcode_(uint8_t opcode, uint8_t *data, uint8_t size) { + this->wait_busy_(); + this->enable(); + this->transfer_byte(opcode); + for (int32_t i = 0; i < size; i++) { + this->transfer_byte(data[i]); + } + this->disable(); + delayMicroseconds(SWITCHING_DELAY_US); +} + +void SX126x::read_register_(uint16_t reg, uint8_t *data, uint8_t size) { + this->wait_busy_(); + this->enable(); + this->write_byte(RADIO_READ_REGISTER); + this->write_byte((reg >> 8) & 0xFF); + this->write_byte((reg >> 0) & 0xFF); + this->write_byte(0x00); + for (int32_t i = 0; i < size; i++) { + data[i] = this->transfer_byte(0x00); + } + this->disable(); +} + +void SX126x::write_register_(uint16_t reg, uint8_t *data, uint8_t size) { + this->wait_busy_(); + this->enable(); + this->write_byte(RADIO_WRITE_REGISTER); + this->write_byte((reg >> 8) & 0xFF); + this->write_byte((reg >> 0) & 0xFF); + for (int32_t i = 0; i < size; i++) { + this->transfer_byte(data[i]); + } + this->disable(); + delayMicroseconds(SWITCHING_DELAY_US); +} + +void SX126x::setup() { + ESP_LOGCONFIG(TAG, "Running setup"); + + // setup pins + this->busy_pin_->setup(); + this->rst_pin_->setup(); + this->dio1_pin_->setup(); + + // start spi + this->spi_setup(); + + // configure rf + this->configure(); +} + +void SX126x::configure() { + uint8_t buf[8]; + + // toggle chip reset + this->rst_pin_->digital_write(true); + delayMicroseconds(RESET_DELAY_HIGH_US); + this->rst_pin_->digital_write(false); + delayMicroseconds(RESET_DELAY_LOW_US); + this->rst_pin_->digital_write(true); + delayMicroseconds(RESET_DELAY_HIGH_US); + + // wakeup + this->read_opcode_(RADIO_GET_STATUS, nullptr, 0); + + // config tcxo + if (this->tcxo_voltage_ != TCXO_CTRL_NONE) { + uint32_t delay = this->tcxo_delay_ >> 6; + buf[0] = this->tcxo_voltage_; + buf[1] = (delay >> 16) & 0xFF; + buf[2] = (delay >> 8) & 0xFF; + buf[3] = (delay >> 0) & 0xFF; + this->write_opcode_(RADIO_SET_TCXOMODE, buf, 4); + buf[0] = 0x7F; + this->write_opcode_(RADIO_CALIBRATE, buf, 1); + } + + // clear errors + buf[0] = 0x00; + buf[1] = 0x00; + this->write_opcode_(RADIO_CLR_ERROR, buf, 2); + + // rf switch + if (this->rf_switch_) { + buf[0] = 0x01; + this->write_opcode_(RADIO_SET_RFSWITCHMODE, buf, 1); + } + + // check silicon version to make sure hw is ok + this->read_register_(REG_VERSION_STRING, (uint8_t *) this->version_, 16); + if (strncmp(this->version_, "SX126", 5) != 0 && strncmp(this->version_, "LLCC68", 6) != 0) { + this->mark_failed(); + return; + } + + // setup packet type + buf[0] = this->modulation_; + this->write_opcode_(RADIO_SET_PACKETTYPE, buf, 1); + + // calibrate image + this->run_image_cal(); + + // set frequency + uint64_t freq = ((uint64_t) this->frequency_ << 25) / XTAL_FREQ; + buf[0] = (uint8_t) ((freq >> 24) & 0xFF); + buf[1] = (uint8_t) ((freq >> 16) & 0xFF); + buf[2] = (uint8_t) ((freq >> 8) & 0xFF); + buf[3] = (uint8_t) (freq & 0xFF); + this->write_opcode_(RADIO_SET_RFFREQUENCY, buf, 4); + + // configure pa + int8_t pa_power = this->pa_power_; + if (this->hw_version_ == "sx1261") { + // the following values were taken from section 13.1.14.1 table 13-21 + // in rev 2.1 of the datasheet + if (pa_power == 15) { + uint8_t cfg[4] = {0x06, 0x00, 0x01, 0x01}; + this->write_opcode_(RADIO_SET_PACONFIG, cfg, 4); + } else { + uint8_t cfg[4] = {0x04, 0x00, 0x01, 0x01}; + this->write_opcode_(RADIO_SET_PACONFIG, cfg, 4); + } + pa_power = std::max(pa_power, (int8_t) -3); + pa_power = std::min(pa_power, (int8_t) 14); + buf[0] = OCP_80MA; + this->write_register_(REG_OCP, buf, 1); + } else { + // the following values were taken from section 13.1.14.1 table 13-21 + // in rev 2.1 of the datasheet + uint8_t cfg[4] = {0x04, 0x07, 0x00, 0x01}; + this->write_opcode_(RADIO_SET_PACONFIG, cfg, 4); + pa_power = std::max(pa_power, (int8_t) -3); + pa_power = std::min(pa_power, (int8_t) 22); + buf[0] = OCP_140MA; + this->write_register_(REG_OCP, buf, 1); + } + buf[0] = pa_power; + buf[1] = this->pa_ramp_; + this->write_opcode_(RADIO_SET_TXPARAMS, buf, 2); + + // configure modem + if (this->modulation_ == PACKET_TYPE_LORA) { + // set modulation params + float duration = 1000.0f * std::pow(2, this->spreading_factor_) / BW_HZ[this->bandwidth_]; + buf[0] = this->spreading_factor_; + buf[1] = BW_LORA[this->bandwidth_ - SX126X_BW_7810]; + buf[2] = this->coding_rate_; + buf[3] = (duration > LOW_DATA_RATE_OPTIMIZE_THRESHOLD) ? 0x01 : 0x00; + this->write_opcode_(RADIO_SET_MODULATIONPARAMS, buf, 4); + + // set packet params and sync word + this->set_packet_params_(this->payload_length_); + if (this->sync_value_.size() == 2) { + this->write_register_(REG_LORA_SYNCWORD, this->sync_value_.data(), this->sync_value_.size()); + } + } else { + // set modulation params + uint32_t bitrate = ((uint64_t) XTAL_FREQ * 32) / this->bitrate_; + uint32_t fdev = ((uint64_t) this->deviation_ << 25) / XTAL_FREQ; + buf[0] = (bitrate >> 16) & 0xFF; + buf[1] = (bitrate >> 8) & 0xFF; + buf[2] = (bitrate >> 0) & 0xFF; + buf[3] = this->shaping_; + buf[4] = BW_FSK[this->bandwidth_ - SX126X_BW_4800]; + buf[5] = (fdev >> 16) & 0xFF; + buf[6] = (fdev >> 8) & 0xFF; + buf[7] = (fdev >> 0) & 0xFF; + this->write_opcode_(RADIO_SET_MODULATIONPARAMS, buf, 8); + + // set packet params and sync word + this->set_packet_params_(this->payload_length_); + if (!this->sync_value_.empty()) { + this->write_register_(REG_GFSK_SYNCWORD, this->sync_value_.data(), this->sync_value_.size()); + } + } + + // switch to rx or sleep + if (this->rx_start_) { + this->set_mode_rx(); + } else { + this->set_mode_sleep(); + } +} + +size_t SX126x::get_max_packet_size() { + if (this->payload_length_ > 0) { + return this->payload_length_; + } + return 255; +} + +void SX126x::set_packet_params_(uint8_t payload_length) { + uint8_t buf[9]; + if (this->modulation_ == PACKET_TYPE_LORA) { + buf[0] = (this->preamble_size_ >> 8) & 0xFF; + buf[1] = (this->preamble_size_ >> 0) & 0xFF; + buf[2] = (this->payload_length_ > 0) ? 0x01 : 0x00; + buf[3] = payload_length; + buf[4] = (this->crc_enable_) ? 0x01 : 0x00; + buf[5] = 0x00; + this->write_opcode_(RADIO_SET_PACKETPARAMS, buf, 6); + } else { + uint16_t preamble_size = this->preamble_size_ * 8; + buf[0] = (preamble_size >> 8) & 0xFF; + buf[1] = (preamble_size >> 0) & 0xFF; + buf[2] = (this->preamble_detect_ > 0) ? ((this->preamble_detect_ - 1) | 0x04) : 0x00; + buf[3] = this->sync_value_.size() * 8; + buf[4] = 0x00; + buf[5] = 0x00; + buf[6] = payload_length; + buf[7] = this->crc_enable_ ? 0x06 : 0x01; + buf[8] = 0x00; + this->write_opcode_(RADIO_SET_PACKETPARAMS, buf, 9); + } +} + +SX126xError SX126x::transmit_packet(const std::vector &packet) { + if (this->payload_length_ > 0 && this->payload_length_ != packet.size()) { + ESP_LOGE(TAG, "Packet size does not match config"); + return SX126xError::INVALID_PARAMS; + } + if (packet.empty() || packet.size() > this->get_max_packet_size()) { + ESP_LOGE(TAG, "Packet size out of range"); + return SX126xError::INVALID_PARAMS; + } + + SX126xError ret = SX126xError::NONE; + this->set_mode_standby(STDBY_XOSC); + if (this->payload_length_ == 0) { + this->set_packet_params_(packet.size()); + } + this->write_fifo_(0x00, packet); + this->set_mode_tx(); + + // wait until transmit completes, typically the delay will be less than 100 ms + uint32_t start = millis(); + while (!this->dio1_pin_->digital_read()) { + if (millis() - start > TRANSMIT_TIMEOUT_MS) { + ESP_LOGE(TAG, "Transmit packet failure"); + ret = SX126xError::TIMEOUT; + break; + } + } + + uint8_t buf[2]; + buf[0] = 0xFF; + buf[1] = 0xFF; + this->write_opcode_(RADIO_CLR_IRQSTATUS, buf, 2); + if (this->rx_start_) { + this->set_mode_rx(); + } else { + this->set_mode_sleep(); + } + return ret; +} + +void SX126x::call_listeners_(const std::vector &packet, float rssi, float snr) { + for (auto &listener : this->listeners_) { + listener->on_packet(packet, rssi, snr); + } + this->packet_trigger_->trigger(packet, rssi, snr); +} + +void SX126x::loop() { + if (!this->dio1_pin_->digital_read()) { + return; + } + + uint16_t status; + uint8_t buf[3]; + uint8_t rssi; + int8_t snr; + this->read_opcode_(RADIO_GET_IRQSTATUS, buf, 2); + this->write_opcode_(RADIO_CLR_IRQSTATUS, buf, 2); + status = (buf[0] << 8) | buf[1]; + if ((status & IRQ_RX_DONE) == IRQ_RX_DONE) { + if ((status & IRQ_CRC_ERROR) != IRQ_CRC_ERROR) { + this->read_opcode_(RADIO_GET_PACKETSTATUS, buf, 3); + if (this->modulation_ == PACKET_TYPE_LORA) { + rssi = buf[0]; + snr = buf[1]; + } else { + rssi = buf[2]; + snr = 0; + } + this->read_opcode_(RADIO_GET_RXBUFFERSTATUS, buf, 2); + this->packet_.resize(buf[0]); + this->read_fifo_(buf[1], this->packet_); + this->call_listeners_(this->packet_, (float) rssi / -2.0f, (float) snr / 4.0f); + } + } +} + +void SX126x::run_image_cal() { + // the following values were taken from section 9.2.1 table 9-2 + // in rev 2.1 of the datasheet + uint8_t buf[2] = {0, 0}; + if (this->frequency_ > 900000000) { + buf[0] = 0xE1; + buf[1] = 0xE9; + } else if (this->frequency_ > 850000000) { + buf[0] = 0xD7; + buf[1] = 0xD8; + } else if (this->frequency_ > 770000000) { + buf[0] = 0xC1; + buf[1] = 0xC5; + } else if (this->frequency_ > 460000000) { + buf[0] = 0x75; + buf[1] = 0x81; + } else if (this->frequency_ > 425000000) { + buf[0] = 0x6B; + buf[1] = 0x6F; + } + if (buf[0] > 0 && buf[1] > 0) { + this->write_opcode_(RADIO_CALIBRATEIMAGE, buf, 2); + } +} + +void SX126x::set_mode_rx() { + uint8_t buf[8]; + + // configure irq params + uint16_t irq = IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT | IRQ_CRC_ERROR; + buf[0] = (irq >> 8) & 0xFF; + buf[1] = (irq >> 0) & 0xFF; + buf[2] = (irq >> 8) & 0xFF; + buf[3] = (irq >> 0) & 0xFF; + buf[4] = (IRQ_RADIO_NONE >> 8) & 0xFF; + buf[5] = (IRQ_RADIO_NONE >> 0) & 0xFF; + buf[6] = (IRQ_RADIO_NONE >> 8) & 0xFF; + buf[7] = (IRQ_RADIO_NONE >> 0) & 0xFF; + this->write_opcode_(RADIO_SET_DIOIRQPARAMS, buf, 8); + + // set timeout to 0 + buf[0] = 0x00; + this->write_opcode_(RADIO_SET_LORASYMBTIMEOUT, buf, 1); + + // switch to continuous mode rx + buf[0] = 0xFF; + buf[1] = 0xFF; + buf[2] = 0xFF; + this->write_opcode_(RADIO_SET_RX, buf, 3); +} + +void SX126x::set_mode_tx() { + uint8_t buf[8]; + + // configure irq params + uint16_t irq = IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT; + buf[0] = (irq >> 8) & 0xFF; + buf[1] = (irq >> 0) & 0xFF; + buf[2] = (irq >> 8) & 0xFF; + buf[3] = (irq >> 0) & 0xFF; + buf[4] = (IRQ_RADIO_NONE >> 8) & 0xFF; + buf[5] = (IRQ_RADIO_NONE >> 0) & 0xFF; + buf[6] = (IRQ_RADIO_NONE >> 8) & 0xFF; + buf[7] = (IRQ_RADIO_NONE >> 0) & 0xFF; + this->write_opcode_(RADIO_SET_DIOIRQPARAMS, buf, 8); + + // switch to single mode tx + buf[0] = 0x00; + buf[1] = 0x00; + buf[2] = 0x00; + this->write_opcode_(RADIO_SET_TX, buf, 3); +} + +void SX126x::set_mode_sleep() { + uint8_t buf[1]; + buf[0] = 0x05; + this->write_opcode_(RADIO_SET_SLEEP, buf, 1); +} + +void SX126x::set_mode_standby(SX126xStandbyMode mode) { + uint8_t buf[1]; + buf[0] = mode; + this->write_opcode_(RADIO_SET_STANDBY, buf, 1); +} + +void SX126x::wait_busy_() { + // wait if the device is busy, the maximum delay is only be a few ms + // with most commands taking only a few us + uint32_t start = millis(); + while (this->busy_pin_->digital_read()) { + if (millis() - start > BUSY_TIMEOUT_MS) { + ESP_LOGE(TAG, "Wait busy timeout"); + this->mark_failed(); + break; + } + } +} + +void SX126x::dump_config() { + ESP_LOGCONFIG(TAG, "SX126x:"); + LOG_PIN(" CS Pin: ", this->cs_); + LOG_PIN(" BUSY Pin: ", this->busy_pin_); + LOG_PIN(" RST Pin: ", this->rst_pin_); + LOG_PIN(" DIO1 Pin: ", this->dio1_pin_); + ESP_LOGCONFIG(TAG, + " HW Version: %15s\n" + " Frequency: %" PRIu32 " Hz\n" + " Bandwidth: %" PRIu32 " Hz\n" + " PA Power: %" PRId8 " dBm\n" + " PA Ramp: %" PRIu16 " us\n" + " Payload Length: %" PRIu32 "\n" + " CRC Enable: %s\n" + " Rx Start: %s", + this->version_, this->frequency_, BW_HZ[this->bandwidth_], this->pa_power_, RAMP[this->pa_ramp_], + this->payload_length_, TRUEFALSE(this->crc_enable_), TRUEFALSE(this->rx_start_)); + if (this->modulation_ == PACKET_TYPE_GFSK) { + const char *shaping = "NONE"; + if (this->shaping_ == GAUSSIAN_BT_0_3) { + shaping = "GAUSSIAN_BT_0_3"; + } else if (this->shaping_ == GAUSSIAN_BT_0_5) { + shaping = "GAUSSIAN_BT_0_5"; + } else if (this->shaping_ == GAUSSIAN_BT_0_7) { + shaping = "GAUSSIAN_BT_0_7"; + } else if (this->shaping_ == GAUSSIAN_BT_1_0) { + shaping = "GAUSSIAN_BT_1_0"; + } + ESP_LOGCONFIG(TAG, + " Modulation: FSK\n" + " Deviation: %" PRIu32 " Hz\n" + " Shaping: %s\n" + " Preamble Size: %" PRIu16 "\n" + " Preamble Detect: %" PRIu16 "\n" + " Bitrate: %" PRIu32 "b/s", + this->deviation_, shaping, this->preamble_size_, this->preamble_detect_, this->bitrate_); + } else if (this->modulation_ == PACKET_TYPE_LORA) { + const char *cr = "4/8"; + if (this->coding_rate_ == LORA_CR_4_5) { + cr = "4/5"; + } else if (this->coding_rate_ == LORA_CR_4_6) { + cr = "4/6"; + } else if (this->coding_rate_ == LORA_CR_4_7) { + cr = "4/7"; + } + ESP_LOGCONFIG(TAG, + " Modulation: LORA\n" + " Spreading Factor: %" PRIu8 "\n" + " Coding Rate: %s\n" + " Preamble Size: %" PRIu16, + this->spreading_factor_, cr, this->preamble_size_); + } + if (!this->sync_value_.empty()) { + ESP_LOGCONFIG(TAG, " Sync Value: 0x%s", format_hex(this->sync_value_).c_str()); + } + if (this->is_failed()) { + ESP_LOGE(TAG, "Configuring SX126x failed"); + } +} + +} // namespace sx126x +} // namespace esphome diff --git a/esphome/components/sx126x/sx126x.h b/esphome/components/sx126x/sx126x.h new file mode 100644 index 0000000000..fd5c37942d --- /dev/null +++ b/esphome/components/sx126x/sx126x.h @@ -0,0 +1,140 @@ +#pragma once + +#include "esphome/components/spi/spi.h" +#include "esphome/core/automation.h" +#include "esphome/core/component.h" +#include "sx126x_reg.h" +#include +#include + +namespace esphome { +namespace sx126x { + +enum SX126xBw : uint8_t { + // FSK + SX126X_BW_4800, + SX126X_BW_5800, + SX126X_BW_7300, + SX126X_BW_9700, + SX126X_BW_11700, + SX126X_BW_14600, + SX126X_BW_19500, + SX126X_BW_23400, + SX126X_BW_29300, + SX126X_BW_39000, + SX126X_BW_46900, + SX126X_BW_58600, + SX126X_BW_78200, + SX126X_BW_93800, + SX126X_BW_117300, + SX126X_BW_156200, + SX126X_BW_187200, + SX126X_BW_234300, + SX126X_BW_312000, + SX126X_BW_373600, + SX126X_BW_467000, + // LORA + SX126X_BW_7810, + SX126X_BW_10420, + SX126X_BW_15630, + SX126X_BW_20830, + SX126X_BW_31250, + SX126X_BW_41670, + SX126X_BW_62500, + SX126X_BW_125000, + SX126X_BW_250000, + SX126X_BW_500000, +}; + +enum class SX126xError { NONE = 0, TIMEOUT, INVALID_PARAMS }; + +class SX126xListener { + public: + virtual void on_packet(const std::vector &packet, float rssi, float snr) = 0; +}; + +class SX126x : public Component, + public spi::SPIDevice { + public: + size_t get_max_packet_size(); + float get_setup_priority() const override { return setup_priority::PROCESSOR; } + void setup() override; + void loop() override; + void dump_config() override; + void set_bandwidth(SX126xBw bandwidth) { this->bandwidth_ = bandwidth; } + void set_bitrate(uint32_t bitrate) { this->bitrate_ = bitrate; } + void set_busy_pin(InternalGPIOPin *busy_pin) { this->busy_pin_ = busy_pin; } + void set_coding_rate(uint8_t coding_rate) { this->coding_rate_ = coding_rate; } + void set_crc_enable(bool crc_enable) { this->crc_enable_ = crc_enable; } + void set_deviation(uint32_t deviation) { this->deviation_ = deviation; } + void set_dio1_pin(InternalGPIOPin *dio1_pin) { this->dio1_pin_ = dio1_pin; } + void set_frequency(uint32_t frequency) { this->frequency_ = frequency; } + void set_hw_version(const std::string &hw_version) { this->hw_version_ = hw_version; } + void set_mode_rx(); + void set_mode_tx(); + void set_mode_standby(SX126xStandbyMode mode); + void set_mode_sleep(); + void set_modulation(uint8_t modulation) { this->modulation_ = modulation; } + void set_pa_power(int8_t power) { this->pa_power_ = power; } + void set_pa_ramp(uint8_t ramp) { this->pa_ramp_ = ramp; } + void set_payload_length(uint8_t payload_length) { this->payload_length_ = payload_length; } + void set_preamble_detect(uint16_t preamble_detect) { this->preamble_detect_ = preamble_detect; } + void set_preamble_size(uint16_t preamble_size) { this->preamble_size_ = preamble_size; } + void set_rst_pin(InternalGPIOPin *rst_pin) { this->rst_pin_ = rst_pin; } + void set_rx_start(bool rx_start) { this->rx_start_ = rx_start; } + void set_rf_switch(bool rf_switch) { this->rf_switch_ = rf_switch; } + void set_shaping(uint8_t shaping) { this->shaping_ = shaping; } + void set_spreading_factor(uint8_t spreading_factor) { this->spreading_factor_ = spreading_factor; } + void set_sync_value(const std::vector &sync_value) { this->sync_value_ = sync_value; } + void set_tcxo_voltage(uint8_t tcxo_voltage) { this->tcxo_voltage_ = tcxo_voltage; } + void set_tcxo_delay(uint32_t tcxo_delay) { this->tcxo_delay_ = tcxo_delay; } + void run_image_cal(); + void configure(); + SX126xError transmit_packet(const std::vector &packet); + void register_listener(SX126xListener *listener) { this->listeners_.push_back(listener); } + Trigger, float, float> *get_packet_trigger() const { return this->packet_trigger_; }; + + protected: + void configure_fsk_ook_(); + void configure_lora_(); + void set_packet_params_(uint8_t payload_length); + uint8_t read_fifo_(uint8_t offset, std::vector &packet); + void write_fifo_(uint8_t offset, const std::vector &packet); + void write_opcode_(uint8_t opcode, uint8_t *data, uint8_t size); + uint8_t read_opcode_(uint8_t opcode, uint8_t *data, uint8_t size); + void write_register_(uint16_t reg, uint8_t *data, uint8_t size); + void read_register_(uint16_t reg, uint8_t *data, uint8_t size); + void call_listeners_(const std::vector &packet, float rssi, float snr); + void wait_busy_(); + Trigger, float, float> *packet_trigger_{new Trigger, float, float>()}; + std::vector listeners_; + std::vector packet_; + std::vector sync_value_; + InternalGPIOPin *busy_pin_{nullptr}; + InternalGPIOPin *dio1_pin_{nullptr}; + InternalGPIOPin *rst_pin_{nullptr}; + std::string hw_version_; + char version_[16]; + SX126xBw bandwidth_{SX126X_BW_125000}; + uint32_t bitrate_{0}; + uint32_t deviation_{0}; + uint32_t frequency_{0}; + uint32_t payload_length_{0}; + uint32_t tcxo_delay_{0}; + uint16_t preamble_detect_{0}; + uint16_t preamble_size_{0}; + uint8_t tcxo_voltage_{0}; + uint8_t coding_rate_{0}; + uint8_t modulation_{PACKET_TYPE_LORA}; + uint8_t pa_ramp_{0}; + uint8_t shaping_{0}; + uint8_t spreading_factor_{0}; + int8_t pa_power_{0}; + bool crc_enable_{false}; + bool rx_start_{false}; + bool rf_switch_{false}; +}; + +} // namespace sx126x +} // namespace esphome diff --git a/esphome/components/sx126x/sx126x_reg.h b/esphome/components/sx126x/sx126x_reg.h new file mode 100644 index 0000000000..3b12d822b5 --- /dev/null +++ b/esphome/components/sx126x/sx126x_reg.h @@ -0,0 +1,163 @@ +#pragma once + +#include "esphome/core/hal.h" + +namespace esphome { +namespace sx126x { + +static const uint32_t XTAL_FREQ = 32000000; + +enum SX126xOpCode : uint8_t { + RADIO_GET_STATUS = 0xC0, + RADIO_WRITE_REGISTER = 0x0D, + RADIO_READ_REGISTER = 0x1D, + RADIO_WRITE_BUFFER = 0x0E, + RADIO_READ_BUFFER = 0x1E, + RADIO_SET_SLEEP = 0x84, + RADIO_SET_STANDBY = 0x80, + RADIO_SET_FS = 0xC1, + RADIO_SET_TX = 0x83, + RADIO_SET_RX = 0x82, + RADIO_SET_RXDUTYCYCLE = 0x94, + RADIO_SET_CAD = 0xC5, + RADIO_SET_TXCONTINUOUSWAVE = 0xD1, + RADIO_SET_TXCONTINUOUSPREAMBLE = 0xD2, + RADIO_SET_PACKETTYPE = 0x8A, + RADIO_GET_PACKETTYPE = 0x11, + RADIO_SET_RFFREQUENCY = 0x86, + RADIO_SET_TXPARAMS = 0x8E, + RADIO_SET_PACONFIG = 0x95, + RADIO_SET_CADPARAMS = 0x88, + RADIO_SET_BUFFERBASEADDRESS = 0x8F, + RADIO_SET_MODULATIONPARAMS = 0x8B, + RADIO_SET_PACKETPARAMS = 0x8C, + RADIO_GET_RXBUFFERSTATUS = 0x13, + RADIO_GET_PACKETSTATUS = 0x14, + RADIO_GET_RSSIINST = 0x15, + RADIO_GET_STATS = 0x10, + RADIO_RESET_STATS = 0x00, + RADIO_SET_DIOIRQPARAMS = 0x08, + RADIO_GET_IRQSTATUS = 0x12, + RADIO_CLR_IRQSTATUS = 0x02, + RADIO_CALIBRATE = 0x89, + RADIO_CALIBRATEIMAGE = 0x98, + RADIO_SET_REGULATORMODE = 0x96, + RADIO_GET_ERROR = 0x17, + RADIO_CLR_ERROR = 0x07, + RADIO_SET_TCXOMODE = 0x97, + RADIO_SET_TXFALLBACKMODE = 0x93, + RADIO_SET_RFSWITCHMODE = 0x9D, + RADIO_SET_STOPRXTIMERONPREAMBLE = 0x9F, + RADIO_SET_LORASYMBTIMEOUT = 0xA0, +}; + +enum SX126xRegister : uint16_t { + REG_VERSION_STRING = 0x0320, + REG_GFSK_SYNCWORD = 0x06C0, + REG_LORA_SYNCWORD = 0x0740, + REG_OCP = 0x08E7, +}; + +enum SX126xStandbyMode : uint8_t { + STDBY_RC = 0x00, + STDBY_XOSC = 0x01, +}; + +enum SX126xPacketType : uint8_t { + PACKET_TYPE_GFSK = 0x00, + PACKET_TYPE_LORA = 0x01, + PACKET_TYPE_LRHSS = 0x03, +}; + +enum SX126xFskBw : uint8_t { + FSK_BW_4800 = 0x1F, + FSK_BW_5800 = 0x17, + FSK_BW_7300 = 0x0F, + FSK_BW_9700 = 0x1E, + FSK_BW_11700 = 0x16, + FSK_BW_14600 = 0x0E, + FSK_BW_19500 = 0x1D, + FSK_BW_23400 = 0x15, + FSK_BW_29300 = 0x0D, + FSK_BW_39000 = 0x1C, + FSK_BW_46900 = 0x14, + FSK_BW_58600 = 0x0C, + FSK_BW_78200 = 0x1B, + FSK_BW_93800 = 0x13, + FSK_BW_117300 = 0x0B, + FSK_BW_156200 = 0x1A, + FSK_BW_187200 = 0x12, + FSK_BW_234300 = 0x0A, + FSK_BW_312000 = 0x19, + FSK_BW_373600 = 0x11, + FSK_BW_467000 = 0x09, +}; + +enum SX126xLoraBw : uint8_t { + LORA_BW_7810 = 0x00, + LORA_BW_10420 = 0x08, + LORA_BW_15630 = 0x01, + LORA_BW_20830 = 0x09, + LORA_BW_31250 = 0x02, + LORA_BW_41670 = 0x0A, + LORA_BW_62500 = 0x03, + LORA_BW_125000 = 0x04, + LORA_BW_250000 = 0x05, + LORA_BW_500000 = 0x06, +}; + +enum SX126xLoraCr : uint8_t { + LORA_CR_4_5 = 0x01, + LORA_CR_4_6 = 0x02, + LORA_CR_4_7 = 0x03, + LORA_CR_4_8 = 0x04, +}; + +enum SX126xIrqMasks : uint16_t { + IRQ_RADIO_NONE = 0x0000, + IRQ_TX_DONE = 0x0001, + IRQ_RX_DONE = 0x0002, + IRQ_PREAMBLE_DETECTED = 0x0004, + IRQ_SYNCWORD_VALID = 0x0008, + IRQ_HEADER_VALID = 0x0010, + IRQ_HEADER_ERROR = 0x0020, + IRQ_CRC_ERROR = 0x0040, + IRQ_CAD_DONE = 0x0080, + IRQ_CAD_ACTIVITY_DETECTED = 0x0100, + IRQ_RX_TX_TIMEOUT = 0x0200, + IRQ_RADIO_ALL = 0xFFFF, +}; + +enum SX126xTcxoCtrl : uint8_t { + TCXO_CTRL_1_6V = 0x00, + TCXO_CTRL_1_7V = 0x01, + TCXO_CTRL_1_8V = 0x02, + TCXO_CTRL_2_2V = 0x03, + TCXO_CTRL_2_4V = 0x04, + TCXO_CTRL_2_7V = 0x05, + TCXO_CTRL_3_0V = 0x06, + TCXO_CTRL_3_3V = 0x07, + TCXO_CTRL_NONE = 0xFF, +}; + +enum SX126xPulseShape : uint8_t { + NO_FILTER = 0x00, + GAUSSIAN_BT_0_3 = 0x08, + GAUSSIAN_BT_0_5 = 0x09, + GAUSSIAN_BT_0_7 = 0x0A, + GAUSSIAN_BT_1_0 = 0x0B, +}; + +enum SX126xRampTime : uint8_t { + PA_RAMP_10 = 0x00, + PA_RAMP_20 = 0x01, + PA_RAMP_40 = 0x02, + PA_RAMP_80 = 0x03, + PA_RAMP_200 = 0x04, + PA_RAMP_800 = 0x05, + PA_RAMP_1700 = 0x06, + PA_RAMP_3400 = 0x07, +}; + +} // namespace sx126x +} // namespace esphome diff --git a/esphome/components/sx127x/sx127x.cpp b/esphome/components/sx127x/sx127x.cpp index e41efe098c..2d2326549b 100644 --- a/esphome/components/sx127x/sx127x.cpp +++ b/esphome/components/sx127x/sx127x.cpp @@ -252,15 +252,17 @@ size_t SX127x::get_max_packet_size() { } } -void SX127x::transmit_packet(const std::vector &packet) { +SX127xError SX127x::transmit_packet(const std::vector &packet) { if (this->payload_length_ > 0 && this->payload_length_ != packet.size()) { ESP_LOGE(TAG, "Packet size does not match config"); - return; + return SX127xError::INVALID_PARAMS; } if (packet.empty() || packet.size() > this->get_max_packet_size()) { ESP_LOGE(TAG, "Packet size out of range"); - return; + return SX127xError::INVALID_PARAMS; } + + SX127xError ret = SX127xError::NONE; if (this->modulation_ == MOD_LORA) { this->set_mode_standby(); if (this->payload_length_ == 0) { @@ -278,11 +280,13 @@ void SX127x::transmit_packet(const std::vector &packet) { this->write_fifo_(packet); this->set_mode_tx(); } + // wait until transmit completes, typically the delay will be less than 100 ms uint32_t start = millis(); while (!this->dio0_pin_->digital_read()) { if (millis() - start > 4000) { ESP_LOGE(TAG, "Transmit packet failure"); + ret = SX127xError::TIMEOUT; break; } } @@ -291,6 +295,7 @@ void SX127x::transmit_packet(const std::vector &packet) { } else { this->set_mode_sleep(); } + return ret; } void SX127x::call_listeners_(const std::vector &packet, float rssi, float snr) { @@ -313,35 +318,28 @@ void SX127x::loop() { uint8_t addr = this->read_register_(REG_FIFO_RX_CURR_ADDR); uint8_t rssi = this->read_register_(REG_PKT_RSSI_VALUE); int8_t snr = (int8_t) this->read_register_(REG_PKT_SNR_VALUE); - std::vector packet(bytes); + this->packet_.resize(bytes); this->write_register_(REG_FIFO_ADDR_PTR, addr); - this->read_fifo_(packet); + this->read_fifo_(this->packet_); if (this->frequency_ > 700000000) { - this->call_listeners_(packet, (float) rssi - RSSI_OFFSET_HF, (float) snr / 4); + this->call_listeners_(this->packet_, (float) rssi - RSSI_OFFSET_HF, (float) snr / 4); } else { - this->call_listeners_(packet, (float) rssi - RSSI_OFFSET_LF, (float) snr / 4); + this->call_listeners_(this->packet_, (float) rssi - RSSI_OFFSET_LF, (float) snr / 4); } } } else if (this->packet_mode_) { - std::vector packet; uint8_t payload_length = this->payload_length_; if (payload_length == 0) { payload_length = this->read_register_(REG_FIFO); } - packet.resize(payload_length); - this->read_fifo_(packet); - this->call_listeners_(packet, 0.0f, 0.0f); + this->packet_.resize(payload_length); + this->read_fifo_(this->packet_); + this->call_listeners_(this->packet_, 0.0f, 0.0f); } } void SX127x::run_image_cal() { - uint32_t start = millis(); - uint8_t mode = this->read_register_(REG_OP_MODE); - if ((mode & MODE_MASK) != MODE_STDBY) { - ESP_LOGE(TAG, "Need to be in standby for image cal"); - return; - } - if (mode & MOD_LORA) { + if (this->modulation_ == MOD_LORA) { this->set_mode_(MOD_FSK, MODE_SLEEP); this->set_mode_(MOD_FSK, MODE_STDBY); } @@ -350,13 +348,15 @@ void SX127x::run_image_cal() { } else { this->write_register_(REG_IMAGE_CAL, IMAGE_CAL_START); } + uint32_t start = millis(); while (this->read_register_(REG_IMAGE_CAL) & IMAGE_CAL_RUNNING) { if (millis() - start > 20) { ESP_LOGE(TAG, "Image cal failure"); + this->mark_failed(); break; } } - if (mode & MOD_LORA) { + if (this->modulation_ == MOD_LORA) { this->set_mode_(this->modulation_, MODE_SLEEP); this->set_mode_(this->modulation_, MODE_STDBY); } @@ -375,6 +375,7 @@ void SX127x::set_mode_(uint8_t modulation, uint8_t mode) { } if (millis() - start > 20) { ESP_LOGE(TAG, "Set mode failure"); + this->mark_failed(); break; } } @@ -405,18 +406,6 @@ void SX127x::dump_config() { LOG_PIN(" CS Pin: ", this->cs_); LOG_PIN(" RST Pin: ", this->rst_pin_); LOG_PIN(" DIO0 Pin: ", this->dio0_pin_); - const char *shaping = "NONE"; - if (this->shaping_ == CUTOFF_BR_X_2) { - shaping = "CUTOFF_BR_X_2"; - } else if (this->shaping_ == CUTOFF_BR_X_1) { - shaping = "CUTOFF_BR_X_1"; - } else if (this->shaping_ == GAUSSIAN_BT_0_3) { - shaping = "GAUSSIAN_BT_0_3"; - } else if (this->shaping_ == GAUSSIAN_BT_0_5) { - shaping = "GAUSSIAN_BT_0_5"; - } else if (this->shaping_ == GAUSSIAN_BT_1_0) { - shaping = "GAUSSIAN_BT_1_0"; - } const char *pa_pin = "RFO"; if (this->pa_pin_ == PA_PIN_BOOST) { pa_pin = "BOOST"; @@ -427,10 +416,9 @@ void SX127x::dump_config() { " Bandwidth: %" PRIu32 " Hz\n" " PA Pin: %s\n" " PA Power: %" PRIu8 " dBm\n" - " PA Ramp: %" PRIu16 " us\n" - " Shaping: %s", + " PA Ramp: %" PRIu16 " us", TRUEFALSE(this->auto_cal_), this->frequency_, BW_HZ[this->bandwidth_], pa_pin, this->pa_power_, - RAMP[this->pa_ramp_], shaping); + RAMP[this->pa_ramp_]); if (this->modulation_ == MOD_FSK) { ESP_LOGCONFIG(TAG, " Deviation: %" PRIu32 " Hz", this->deviation_); } @@ -457,14 +445,31 @@ void SX127x::dump_config() { ESP_LOGCONFIG(TAG, " Sync Value: 0x%02x", this->sync_value_[0]); } } else { + const char *shaping = "NONE"; + if (this->modulation_ == MOD_FSK) { + if (this->shaping_ == GAUSSIAN_BT_0_3) { + shaping = "GAUSSIAN_BT_0_3"; + } else if (this->shaping_ == GAUSSIAN_BT_0_5) { + shaping = "GAUSSIAN_BT_0_5"; + } else if (this->shaping_ == GAUSSIAN_BT_1_0) { + shaping = "GAUSSIAN_BT_1_0"; + } + } else { + if (this->shaping_ == CUTOFF_BR_X_2) { + shaping = "CUTOFF_BR_X_2"; + } else if (this->shaping_ == CUTOFF_BR_X_1) { + shaping = "CUTOFF_BR_X_1"; + } + } ESP_LOGCONFIG(TAG, + " Shaping: %s\n" " Modulation: %s\n" " Bitrate: %" PRIu32 "b/s\n" " Bitsync: %s\n" " Rx Start: %s\n" " Rx Floor: %.1f dBm\n" " Packet Mode: %s", - this->modulation_ == MOD_FSK ? "FSK" : "OOK", this->bitrate_, TRUEFALSE(this->bitsync_), + shaping, this->modulation_ == MOD_FSK ? "FSK" : "OOK", this->bitrate_, TRUEFALSE(this->bitsync_), TRUEFALSE(this->rx_start_), this->rx_floor_, TRUEFALSE(this->packet_mode_)); if (this->packet_mode_) { ESP_LOGCONFIG(TAG, " CRC Enable: %s", TRUEFALSE(this->crc_enable_)); diff --git a/esphome/components/sx127x/sx127x.h b/esphome/components/sx127x/sx127x.h index fe9f60e860..0600b51201 100644 --- a/esphome/components/sx127x/sx127x.h +++ b/esphome/components/sx127x/sx127x.h @@ -34,6 +34,8 @@ enum SX127xBw : uint8_t { SX127X_BW_500_0, }; +enum class SX127xError { NONE = 0, TIMEOUT, INVALID_PARAMS }; + class SX127xListener { public: virtual void on_packet(const std::vector &packet, float rssi, float snr) = 0; @@ -79,7 +81,7 @@ class SX127x : public Component, void set_sync_value(const std::vector &sync_value) { this->sync_value_ = sync_value; } void run_image_cal(); void configure(); - void transmit_packet(const std::vector &packet); + SX127xError transmit_packet(const std::vector &packet); void register_listener(SX127xListener *listener) { this->listeners_.push_back(listener); } Trigger, float, float> *get_packet_trigger() const { return this->packet_trigger_; }; @@ -94,6 +96,7 @@ class SX127x : public Component, uint8_t read_register_(uint8_t reg); Trigger, float, float> *packet_trigger_{new Trigger, float, float>()}; std::vector listeners_; + std::vector packet_; std::vector sync_value_; InternalGPIOPin *dio0_pin_{nullptr}; InternalGPIOPin *rst_pin_{nullptr}; diff --git a/esphome/components/update/update_entity.h b/esphome/components/update/update_entity.h index 169e580457..9424e80b9f 100644 --- a/esphome/components/update/update_entity.h +++ b/esphome/components/update/update_entity.h @@ -1,5 +1,6 @@ #pragma once +#include #include "esphome/core/automation.h" #include "esphome/core/component.h" #include "esphome/core/entity_base.h" @@ -38,12 +39,19 @@ class UpdateEntity : public EntityBase, public EntityBase_DeviceClass { const UpdateState &state = state_; void add_on_state_callback(std::function &&callback) { this->state_callback_.add(std::move(callback)); } + Trigger *get_update_available_trigger() { + if (!update_available_trigger_) { + update_available_trigger_ = std::make_unique>(); + } + return update_available_trigger_.get(); + } protected: UpdateState state_{UPDATE_STATE_UNKNOWN}; UpdateInfo update_info_; CallbackManager state_callback_{}; + std::unique_ptr> update_available_trigger_{nullptr}; }; } // namespace update diff --git a/esphome/components/web_server/web_server.cpp b/esphome/components/web_server/web_server.cpp index 77c20b956b..20ff1a7c29 100644 --- a/esphome/components/web_server/web_server.cpp +++ b/esphome/components/web_server/web_server.cpp @@ -255,11 +255,7 @@ void DeferredUpdateEventSourceList::on_client_disconnect_(DeferredUpdateEventSou } #endif -WebServer::WebServer(web_server_base::WebServerBase *base) : base_(base) { -#ifdef USE_ESP32 - to_schedule_lock_ = xSemaphoreCreateMutex(); -#endif -} +WebServer::WebServer(web_server_base::WebServerBase *base) : base_(base) {} #ifdef USE_WEBSERVER_CSS_INCLUDE void WebServer::set_css_include(const char *css_include) { this->css_include_ = css_include; } @@ -308,30 +304,7 @@ void WebServer::setup() { // getting a lot of events this->set_interval(10000, [this]() { this->events_.try_send_nodefer("", "ping", millis(), 30000); }); } -void WebServer::loop() { -#ifdef USE_ESP32 - // Check atomic flag first to avoid taking semaphore when queue is empty - if (this->to_schedule_has_items_.load(std::memory_order_relaxed) && xSemaphoreTake(this->to_schedule_lock_, 0L)) { - std::function fn; - if (!to_schedule_.empty()) { - // scheduler execute things out of order which may lead to incorrect state - // this->defer(std::move(to_schedule_.front())); - // let's execute it directly from the loop - fn = std::move(to_schedule_.front()); - to_schedule_.pop_front(); - if (to_schedule_.empty()) { - this->to_schedule_has_items_.store(false, std::memory_order_relaxed); - } - } - xSemaphoreGive(this->to_schedule_lock_); - if (fn) { - fn(); - } - } -#endif - - this->events_.loop(); -} +void WebServer::loop() { this->events_.loop(); } void WebServer::dump_config() { ESP_LOGCONFIG(TAG, "Web Server:\n" @@ -526,13 +499,13 @@ void WebServer::handle_switch_request(AsyncWebServerRequest *request, const UrlM std::string data = this->switch_json(obj, obj->state, detail); request->send(200, "application/json", data.c_str()); } else if (match.method_equals("toggle")) { - this->schedule_([obj]() { obj->toggle(); }); + this->defer([obj]() { obj->toggle(); }); request->send(200); } else if (match.method_equals("turn_on")) { - this->schedule_([obj]() { obj->turn_on(); }); + this->defer([obj]() { obj->turn_on(); }); request->send(200); } else if (match.method_equals("turn_off")) { - this->schedule_([obj]() { obj->turn_off(); }); + this->defer([obj]() { obj->turn_off(); }); request->send(200); } else { request->send(404); @@ -568,7 +541,7 @@ void WebServer::handle_button_request(AsyncWebServerRequest *request, const UrlM std::string data = this->button_json(obj, detail); request->send(200, "application/json", data.c_str()); } else if (match.method_equals("press")) { - this->schedule_([obj]() { obj->press(); }); + this->defer([obj]() { obj->press(); }); request->send(200); return; } else { @@ -648,7 +621,7 @@ void WebServer::handle_fan_request(AsyncWebServerRequest *request, const UrlMatc std::string data = this->fan_json(obj, detail); request->send(200, "application/json", data.c_str()); } else if (match.method_equals("toggle")) { - this->schedule_([obj]() { obj->toggle().perform(); }); + this->defer([obj]() { obj->toggle().perform(); }); request->send(200); } else if (match.method_equals("turn_on") || match.method_equals("turn_off")) { auto call = match.method_equals("turn_on") ? obj->turn_on() : obj->turn_off(); @@ -680,7 +653,7 @@ void WebServer::handle_fan_request(AsyncWebServerRequest *request, const UrlMatc return; } } - this->schedule_([call]() mutable { call.perform(); }); + this->defer([call]() mutable { call.perform(); }); request->send(200); } else { request->send(404); @@ -729,7 +702,7 @@ void WebServer::handle_light_request(AsyncWebServerRequest *request, const UrlMa std::string data = this->light_json(obj, detail); request->send(200, "application/json", data.c_str()); } else if (match.method_equals("toggle")) { - this->schedule_([obj]() { obj->toggle().perform(); }); + this->defer([obj]() { obj->toggle().perform(); }); request->send(200); } else if (match.method_equals("turn_on")) { auto call = obj->turn_on(); @@ -786,7 +759,7 @@ void WebServer::handle_light_request(AsyncWebServerRequest *request, const UrlMa call.set_effect(effect); } - this->schedule_([call]() mutable { call.perform(); }); + this->defer([call]() mutable { call.perform(); }); request->send(200); } else if (match.method_equals("turn_off")) { auto call = obj->turn_off(); @@ -796,7 +769,7 @@ void WebServer::handle_light_request(AsyncWebServerRequest *request, const UrlMa call.set_transition_length(*transition * 1000); } } - this->schedule_([call]() mutable { call.perform(); }); + this->defer([call]() mutable { call.perform(); }); request->send(200); } else { request->send(404); @@ -881,7 +854,7 @@ void WebServer::handle_cover_request(AsyncWebServerRequest *request, const UrlMa } } - this->schedule_([call]() mutable { call.perform(); }); + this->defer([call]() mutable { call.perform(); }); request->send(200); return; } @@ -939,7 +912,7 @@ void WebServer::handle_number_request(AsyncWebServerRequest *request, const UrlM call.set_value(*value); } - this->schedule_([call]() mutable { call.perform(); }); + this->defer([call]() mutable { call.perform(); }); request->send(200); return; } @@ -1014,7 +987,7 @@ void WebServer::handle_date_request(AsyncWebServerRequest *request, const UrlMat call.set_date(value); } - this->schedule_([call]() mutable { call.perform(); }); + this->defer([call]() mutable { call.perform(); }); request->send(200); return; } @@ -1073,7 +1046,7 @@ void WebServer::handle_time_request(AsyncWebServerRequest *request, const UrlMat call.set_time(value); } - this->schedule_([call]() mutable { call.perform(); }); + this->defer([call]() mutable { call.perform(); }); request->send(200); return; } @@ -1131,7 +1104,7 @@ void WebServer::handle_datetime_request(AsyncWebServerRequest *request, const Ur call.set_datetime(value); } - this->schedule_([call]() mutable { call.perform(); }); + this->defer([call]() mutable { call.perform(); }); request->send(200); return; } @@ -1248,7 +1221,7 @@ void WebServer::handle_select_request(AsyncWebServerRequest *request, const UrlM call.set_option(option.c_str()); // NOLINT } - this->schedule_([call]() mutable { call.perform(); }); + this->defer([call]() mutable { call.perform(); }); request->send(200); return; } @@ -1335,7 +1308,7 @@ void WebServer::handle_climate_request(AsyncWebServerRequest *request, const Url call.set_target_temperature(*target_temperature); } - this->schedule_([call]() mutable { call.perform(); }); + this->defer([call]() mutable { call.perform(); }); request->send(200); return; } @@ -1452,13 +1425,13 @@ void WebServer::handle_lock_request(AsyncWebServerRequest *request, const UrlMat std::string data = this->lock_json(obj, obj->state, detail); request->send(200, "application/json", data.c_str()); } else if (match.method_equals("lock")) { - this->schedule_([obj]() { obj->lock(); }); + this->defer([obj]() { obj->lock(); }); request->send(200); } else if (match.method_equals("unlock")) { - this->schedule_([obj]() { obj->unlock(); }); + this->defer([obj]() { obj->unlock(); }); request->send(200); } else if (match.method_equals("open")) { - this->schedule_([obj]() { obj->open(); }); + this->defer([obj]() { obj->open(); }); request->send(200); } else { request->send(404); @@ -1529,7 +1502,7 @@ void WebServer::handle_valve_request(AsyncWebServerRequest *request, const UrlMa } } - this->schedule_([call]() mutable { call.perform(); }); + this->defer([call]() mutable { call.perform(); }); request->send(200); return; } @@ -1594,7 +1567,7 @@ void WebServer::handle_alarm_control_panel_request(AsyncWebServerRequest *reques return; } - this->schedule_([call]() mutable { call.perform(); }); + this->defer([call]() mutable { call.perform(); }); request->send(200); return; } @@ -1695,7 +1668,7 @@ void WebServer::handle_update_request(AsyncWebServerRequest *request, const UrlM return; } - this->schedule_([obj]() mutable { obj->perform(); }); + this->defer([obj]() mutable { obj->perform(); }); request->send(200); return; } @@ -2072,17 +2045,6 @@ void WebServer::add_sorting_group(uint64_t group_id, const std::string &group_na } #endif -void WebServer::schedule_(std::function &&f) { -#ifdef USE_ESP32 - xSemaphoreTake(this->to_schedule_lock_, portMAX_DELAY); - to_schedule_.push_back(std::move(f)); - this->to_schedule_has_items_.store(true, std::memory_order_relaxed); - xSemaphoreGive(this->to_schedule_lock_); -#else - this->defer(std::move(f)); -#endif -} - } // namespace web_server } // namespace esphome #endif diff --git a/esphome/components/web_server/web_server.h b/esphome/components/web_server/web_server.h index 82b31ab656..0c15881d1e 100644 --- a/esphome/components/web_server/web_server.h +++ b/esphome/components/web_server/web_server.h @@ -14,12 +14,6 @@ #include #include #include -#ifdef USE_ESP32 -#include -#include -#include -#include -#endif #if USE_WEBSERVER_VERSION >= 2 extern const uint8_t ESPHOME_WEBSERVER_INDEX_HTML[] PROGMEM; @@ -504,7 +498,6 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { protected: void add_sorting_info_(JsonObject &root, EntityBase *entity); - void schedule_(std::function &&f); web_server_base::WebServerBase *base_; #ifdef USE_ARDUINO DeferredUpdateEventSourceList events_; @@ -524,11 +517,6 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { const char *js_include_{nullptr}; #endif bool expose_log_{true}; -#ifdef USE_ESP32 - std::deque> to_schedule_; - SemaphoreHandle_t to_schedule_lock_; - std::atomic to_schedule_has_items_{false}; -#endif }; } // namespace web_server diff --git a/esphome/components/web_server_idf/web_server_idf.cpp b/esphome/components/web_server_idf/web_server_idf.cpp index 9478e4748c..d2447681f5 100644 --- a/esphome/components/web_server_idf/web_server_idf.cpp +++ b/esphome/components/web_server_idf/web_server_idf.cpp @@ -37,6 +37,15 @@ namespace web_server_idf { static const char *const TAG = "web_server_idf"; +// Global instance to avoid guard variable (saves 8 bytes) +// This is initialized at program startup before any threads +namespace { +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) +DefaultHeaders default_headers_instance; +} // namespace + +DefaultHeaders &DefaultHeaders::Instance() { return default_headers_instance; } + void AsyncWebServer::end() { if (this->server_) { httpd_stop(this->server_); diff --git a/esphome/components/web_server_idf/web_server_idf.h b/esphome/components/web_server_idf/web_server_idf.h index 8de25c8e96..e8e40ef9b0 100644 --- a/esphome/components/web_server_idf/web_server_idf.h +++ b/esphome/components/web_server_idf/web_server_idf.h @@ -328,10 +328,7 @@ class DefaultHeaders { void addHeader(const char *name, const char *value) { this->headers_.emplace_back(name, value); } // NOLINTNEXTLINE(readability-identifier-naming) - static DefaultHeaders &Instance() { - static DefaultHeaders instance; - return instance; - } + static DefaultHeaders &Instance(); protected: std::vector> headers_; diff --git a/esphome/core/color.cpp b/esphome/core/color.cpp index 58d995db2f..7e390b2354 100644 --- a/esphome/core/color.cpp +++ b/esphome/core/color.cpp @@ -2,10 +2,8 @@ namespace esphome { -const Color Color::BLACK(0, 0, 0, 0); -const Color Color::WHITE(255, 255, 255, 255); - -const Color COLOR_BLACK(0, 0, 0, 0); -const Color COLOR_WHITE(255, 255, 255, 255); +// C++20 constinit ensures compile-time initialization (stored in ROM) +constinit const Color Color::BLACK(0, 0, 0, 0); +constinit const Color Color::WHITE(255, 255, 255, 255); } // namespace esphome diff --git a/esphome/core/color.h b/esphome/core/color.h index 1c43fd9d3e..2b307bb438 100644 --- a/esphome/core/color.h +++ b/esphome/core/color.h @@ -5,7 +5,9 @@ namespace esphome { -inline static uint8_t esp_scale8(uint8_t i, uint8_t scale) { return (uint16_t(i) * (1 + uint16_t(scale))) / 256; } +inline static constexpr uint8_t esp_scale8(uint8_t i, uint8_t scale) { + return (uint16_t(i) * (1 + uint16_t(scale))) / 256; +} struct Color { union { @@ -31,17 +33,20 @@ struct Color { uint32_t raw_32; }; - inline Color() ESPHOME_ALWAYS_INLINE : r(0), g(0), b(0), w(0) {} // NOLINT - inline Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE : r(red), g(green), b(blue), w(0) {} + inline constexpr Color() ESPHOME_ALWAYS_INLINE : raw_32(0) {} // NOLINT + inline constexpr Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE : r(red), + g(green), + b(blue), + w(0) {} - inline Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE : r(red), - g(green), - b(blue), - w(white) {} - inline explicit Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE : r((colorcode >> 16) & 0xFF), - g((colorcode >> 8) & 0xFF), - b((colorcode >> 0) & 0xFF), - w((colorcode >> 24) & 0xFF) {} + inline constexpr Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE : r(red), + g(green), + b(blue), + w(white) {} + inline explicit constexpr Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE : r((colorcode >> 16) & 0xFF), + g((colorcode >> 8) & 0xFF), + b((colorcode >> 0) & 0xFF), + w((colorcode >> 24) & 0xFF) {} inline bool is_on() ESPHOME_ALWAYS_INLINE { return this->raw_32 != 0; } @@ -169,9 +174,4 @@ struct Color { static const Color WHITE; }; -ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK", "v1.21") -extern const Color COLOR_BLACK; -ESPDEPRECATED("Use Color::WHITE instead of COLOR_WHITE", "v1.21") -extern const Color COLOR_WHITE; - } // namespace esphome diff --git a/esphome/core/component_iterator.cpp b/esphome/core/component_iterator.cpp index b06c964b7c..aab5c2a72d 100644 --- a/esphome/core/component_iterator.cpp +++ b/esphome/core/component_iterator.cpp @@ -158,16 +158,16 @@ void ComponentIterator::advance() { } break; #endif -#ifdef USE_ESP32_CAMERA +#ifdef USE_CAMERA case IteratorState::CAMERA: - if (esp32_camera::global_esp32_camera == nullptr) { + if (camera::Camera::instance() == nullptr) { advance_platform = true; } else { - if (esp32_camera::global_esp32_camera->is_internal() && !this->include_internal_) { + if (camera::Camera::instance()->is_internal() && !this->include_internal_) { advance_platform = success = true; break; } else { - advance_platform = success = this->on_camera(esp32_camera::global_esp32_camera); + advance_platform = success = this->on_camera(camera::Camera::instance()); } } break; @@ -386,8 +386,8 @@ bool ComponentIterator::on_begin() { return true; } #ifdef USE_API bool ComponentIterator::on_service(api::UserServiceDescriptor *service) { return true; } #endif -#ifdef USE_ESP32_CAMERA -bool ComponentIterator::on_camera(esp32_camera::ESP32Camera *camera) { return true; } +#ifdef USE_CAMERA +bool ComponentIterator::on_camera(camera::Camera *camera) { return true; } #endif #ifdef USE_MEDIA_PLAYER bool ComponentIterator::on_media_player(media_player::MediaPlayer *media_player) { return true; } diff --git a/esphome/core/component_iterator.h b/esphome/core/component_iterator.h index 4b41872db7..eda786be7f 100644 --- a/esphome/core/component_iterator.h +++ b/esphome/core/component_iterator.h @@ -4,8 +4,8 @@ #include "esphome/core/controller.h" #include "esphome/core/helpers.h" -#ifdef USE_ESP32_CAMERA -#include "esphome/components/esp32_camera/esp32_camera.h" +#ifdef USE_CAMERA +#include "esphome/components/camera/camera.h" #endif namespace esphome { @@ -48,8 +48,8 @@ class ComponentIterator { #ifdef USE_API virtual bool on_service(api::UserServiceDescriptor *service); #endif -#ifdef USE_ESP32_CAMERA - virtual bool on_camera(esp32_camera::ESP32Camera *camera); +#ifdef USE_CAMERA + virtual bool on_camera(camera::Camera *camera); #endif #ifdef USE_CLIMATE virtual bool on_climate(climate::Climate *climate) = 0; @@ -125,7 +125,7 @@ class ComponentIterator { #ifdef USE_API SERVICE, #endif -#ifdef USE_ESP32_CAMERA +#ifdef USE_CAMERA CAMERA, #endif #ifdef USE_CLIMATE diff --git a/esphome/core/defines.h b/esphome/core/defines.h index 320b40dc90..4115b97391 100644 --- a/esphome/core/defines.h +++ b/esphome/core/defines.h @@ -23,6 +23,7 @@ #define USE_AREAS #define USE_BINARY_SENSOR #define USE_BUTTON +#define USE_CAMERA #define USE_CLIMATE #define USE_COVER #define USE_DATETIME @@ -144,7 +145,6 @@ #define USE_ESP32_BLE #define USE_ESP32_BLE_CLIENT #define USE_ESP32_BLE_SERVER -#define USE_ESP32_CAMERA #define USE_I2C #define USE_IMPROV #define USE_MICROPHONE diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index b4923c7af0..72722169d4 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -12,47 +12,10 @@ #include #include -#ifdef USE_HOST -#ifndef _WIN32 -#include -#include -#include -#endif -#include -#endif -#if defined(USE_ESP8266) -#include -#include -// for xt_rsil()/xt_wsr_ps() -#include -#elif defined(USE_ESP32_FRAMEWORK_ARDUINO) -#include -#elif defined(USE_ESP_IDF) -#include -#include -#include "esp_random.h" -#include "esp_system.h" -#elif defined(USE_RP2040) -#if defined(USE_WIFI) -#include -#endif -#include -#include -#elif defined(USE_HOST) -#include -#include -#endif #ifdef USE_ESP32 -#include "esp_efuse.h" -#include "esp_efuse_table.h" -#include "esp_mac.h" #include "rom/crc.h" #endif -#ifdef USE_LIBRETINY -#include // for macAddress() -#endif - namespace esphome { static const char *const TAG = "helpers"; @@ -177,70 +140,7 @@ uint32_t fnv1_hash(const std::string &str) { return hash; } -#ifdef USE_ESP32 -uint32_t random_uint32() { return esp_random(); } -#elif defined(USE_ESP8266) -uint32_t random_uint32() { return os_random(); } -#elif defined(USE_RP2040) -uint32_t random_uint32() { - uint32_t result = 0; - for (uint8_t i = 0; i < 32; i++) { - result <<= 1; - result |= rosc_hw->randombit; - } - return result; -} -#elif defined(USE_LIBRETINY) -uint32_t random_uint32() { return rand(); } -#elif defined(USE_HOST) -uint32_t random_uint32() { - std::random_device dev; - std::mt19937 rng(dev()); - std::uniform_int_distribution dist(0, std::numeric_limits::max()); - return dist(rng); -} -#endif float random_float() { return static_cast(random_uint32()) / static_cast(UINT32_MAX); } -#ifdef USE_ESP32 -bool random_bytes(uint8_t *data, size_t len) { - esp_fill_random(data, len); - return true; -} -#elif defined(USE_ESP8266) -bool random_bytes(uint8_t *data, size_t len) { return os_get_random(data, len) == 0; } -#elif defined(USE_RP2040) -bool random_bytes(uint8_t *data, size_t len) { - while (len-- != 0) { - uint8_t result = 0; - for (uint8_t i = 0; i < 8; i++) { - result <<= 1; - result |= rosc_hw->randombit; - } - *data++ = result; - } - return true; -} -#elif defined(USE_LIBRETINY) -bool random_bytes(uint8_t *data, size_t len) { - lt_rand_bytes(data, len); - return true; -} -#elif defined(USE_HOST) -bool random_bytes(uint8_t *data, size_t len) { - FILE *fp = fopen("/dev/urandom", "r"); - if (fp == nullptr) { - ESP_LOGW(TAG, "Could not open /dev/urandom, errno=%d", errno); - exit(1); - } - size_t read = fread(data, 1, len, fp); - if (read != len) { - ESP_LOGW(TAG, "Not enough data from /dev/urandom"); - exit(1); - } - fclose(fp); - return true; -} -#endif // Strings @@ -644,35 +544,6 @@ void hsv_to_rgb(int hue, float saturation, float value, float &red, float &green blue += delta; } -// System APIs -#if defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_HOST) -// ESP8266 doesn't have mutexes, but that shouldn't be an issue as it's single-core and non-preemptive OS. -Mutex::Mutex() {} -Mutex::~Mutex() {} -void Mutex::lock() {} -bool Mutex::try_lock() { return true; } -void Mutex::unlock() {} -#elif defined(USE_ESP32) || defined(USE_LIBRETINY) -Mutex::Mutex() { handle_ = xSemaphoreCreateMutex(); } -Mutex::~Mutex() {} -void Mutex::lock() { xSemaphoreTake(this->handle_, portMAX_DELAY); } -bool Mutex::try_lock() { return xSemaphoreTake(this->handle_, 0) == pdTRUE; } -void Mutex::unlock() { xSemaphoreGive(this->handle_); } -#endif - -#if defined(USE_ESP8266) -IRAM_ATTR InterruptLock::InterruptLock() { state_ = xt_rsil(15); } -IRAM_ATTR InterruptLock::~InterruptLock() { xt_wsr_ps(state_); } -#elif defined(USE_ESP32) || defined(USE_LIBRETINY) -// only affects the executing core -// so should not be used as a mutex lock, only to get accurate timing -IRAM_ATTR InterruptLock::InterruptLock() { portDISABLE_INTERRUPTS(); } -IRAM_ATTR InterruptLock::~InterruptLock() { portENABLE_INTERRUPTS(); } -#elif defined(USE_RP2040) -IRAM_ATTR InterruptLock::InterruptLock() { state_ = save_and_disable_interrupts(); } -IRAM_ATTR InterruptLock::~InterruptLock() { restore_interrupts(state_); } -#endif - uint8_t HighFrequencyLoopRequester::num_requests = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) void HighFrequencyLoopRequester::start() { if (this->started_) @@ -688,45 +559,6 @@ void HighFrequencyLoopRequester::stop() { } bool HighFrequencyLoopRequester::is_high_frequency() { return num_requests > 0; } -#if defined(USE_HOST) -void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter) - static const uint8_t esphome_host_mac_address[6] = USE_ESPHOME_HOST_MAC_ADDRESS; - memcpy(mac, esphome_host_mac_address, sizeof(esphome_host_mac_address)); -} -#elif defined(USE_ESP32) -void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter) -#if defined(CONFIG_SOC_IEEE802154_SUPPORTED) - // When CONFIG_SOC_IEEE802154_SUPPORTED is defined, esp_efuse_mac_get_default - // returns the 802.15.4 EUI-64 address, so we read directly from eFuse instead. - if (has_custom_mac_address()) { - esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48); - } else { - esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, 48); - } -#else - if (has_custom_mac_address()) { - esp_efuse_mac_get_custom(mac); - } else { - esp_efuse_mac_get_default(mac); - } -#endif -} -#elif defined(USE_ESP8266) -void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter) - wifi_get_macaddr(STATION_IF, mac); -} -#elif defined(USE_RP2040) -void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter) -#ifdef USE_WIFI - WiFi.macAddress(mac); -#endif -} -#elif defined(USE_LIBRETINY) -void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter) - WiFi.macAddress(mac); -} -#endif - std::string get_mac_address() { uint8_t mac[6]; get_mac_address_raw(mac); @@ -739,24 +571,10 @@ std::string get_mac_address_pretty() { return format_mac_address_pretty(mac); } -#ifdef USE_ESP32 -void set_mac_address(uint8_t *mac) { esp_base_mac_addr_set(mac); } +#ifndef USE_ESP32 +bool has_custom_mac_address() { return false; } #endif -bool has_custom_mac_address() { -#if defined(USE_ESP32) && !defined(USE_ESP32_IGNORE_EFUSE_CUSTOM_MAC) - uint8_t mac[6]; - // do not use 'esp_efuse_mac_get_custom(mac)' because it drops an error in the logs whenever it fails -#ifndef USE_ESP32_VARIANT_ESP32 - return (esp_efuse_read_field_blob(ESP_EFUSE_USER_DATA_MAC_CUSTOM, mac, 48) == ESP_OK) && mac_address_is_valid(mac); -#else - return (esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48) == ESP_OK) && mac_address_is_valid(mac); -#endif -#else - return false; -#endif -} - bool mac_address_is_valid(const uint8_t *mac) { bool is_all_zeros = true; bool is_all_ones = true; diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 362f3d1fa4..d92cf07702 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -32,6 +32,10 @@ #include #endif +#ifdef USE_HOST +#include +#endif + #define HOT __attribute__((hot)) #define ESPDEPRECATED(msg, when) __attribute__((deprecated(msg))) #define ESPHOME_ALWAYS_INLINE __attribute__((always_inline)) diff --git a/esphome/core/lock_free_queue.h b/esphome/core/lock_free_queue.h index 5460be0fae..f35cfa5af9 100644 --- a/esphome/core/lock_free_queue.h +++ b/esphome/core/lock_free_queue.h @@ -31,11 +31,20 @@ namespace esphome { +// Base lock-free queue without task notification template class LockFreeQueue { public: - LockFreeQueue() : head_(0), tail_(0), dropped_count_(0), task_to_notify_(nullptr) {} + LockFreeQueue() : head_(0), tail_(0), dropped_count_(0) {} bool push(T *element) { + bool was_empty; + uint8_t old_tail; + return push_internal_(element, was_empty, old_tail); + } + + protected: + // Internal push that reports queue state - for use by derived classes + bool push_internal_(T *element, bool &was_empty, uint8_t &old_tail) { if (element == nullptr) return false; @@ -51,34 +60,16 @@ template class LockFreeQueue { return false; } - // Check if queue was empty before push - bool was_empty = (current_tail == head_before); + was_empty = (current_tail == head_before); + old_tail = current_tail; buffer_[current_tail] = element; tail_.store(next_tail, std::memory_order_release); - // Notify optimization: only notify if we need to - if (task_to_notify_ != nullptr) { - if (was_empty) { - // Queue was empty - consumer might be going to sleep, must notify - xTaskNotifyGive(task_to_notify_); - } else { - // Queue wasn't empty - check if consumer has caught up to previous tail - uint8_t head_after = head_.load(std::memory_order_acquire); - if (head_after == current_tail) { - // Consumer just caught up to where tail was - might go to sleep, must notify - // Note: There's a benign race here - between reading head_after and calling - // xTaskNotifyGive(), the consumer could advance further. This would result - // in an unnecessary wake-up, but is harmless and extremely rare in practice. - xTaskNotifyGive(task_to_notify_); - } - // Otherwise: consumer is still behind, no need to notify - } - } - return true; } + public: T *pop() { uint8_t current_head = head_.load(std::memory_order_relaxed); @@ -108,11 +99,6 @@ template class LockFreeQueue { return next_tail == head_.load(std::memory_order_acquire); } - // Set the FreeRTOS task handle to notify when items are pushed to the queue - // This enables efficient wake-up of a consumer task that's waiting for data - // @param task The FreeRTOS task handle to notify, or nullptr to disable notifications - void set_task_to_notify(TaskHandle_t task) { task_to_notify_ = task; } - protected: T *buffer_[SIZE]; // Atomic: written by producer (push/increment), read+reset by consumer (get_and_reset) @@ -123,7 +109,40 @@ template class LockFreeQueue { std::atomic head_; // Atomic: written by producer (push), read by consumer (pop) to check if empty std::atomic tail_; - // Task handle for notification (optional) +}; + +// Extended queue with task notification support +template class NotifyingLockFreeQueue : public LockFreeQueue { + public: + NotifyingLockFreeQueue() : LockFreeQueue(), task_to_notify_(nullptr) {} + + bool push(T *element) { + bool was_empty; + uint8_t old_tail; + bool result = this->push_internal_(element, was_empty, old_tail); + + // Notify optimization: only notify if we need to + if (result && task_to_notify_ != nullptr && + (was_empty || this->head_.load(std::memory_order_acquire) == old_tail)) { + // Notify in two cases: + // 1. Queue was empty - consumer might be going to sleep + // 2. Consumer just caught up to where tail was - might go to sleep + // Note: There's a benign race in case 2 - between reading head and calling + // xTaskNotifyGive(), the consumer could advance further. This would result + // in an unnecessary wake-up, but is harmless and extremely rare in practice. + xTaskNotifyGive(task_to_notify_); + } + // Otherwise: consumer is still behind, no need to notify + + return result; + } + + // Set the FreeRTOS task handle to notify when items are pushed to the queue + // This enables efficient wake-up of a consumer task that's waiting for data + // @param task The FreeRTOS task handle to notify, or nullptr to disable notifications + void set_task_to_notify(TaskHandle_t task) { task_to_notify_ = task; } + + private: TaskHandle_t task_to_notify_; }; diff --git a/esphome/core/scheduler.cpp b/esphome/core/scheduler.cpp index 5c01b4f3f4..515f6fd355 100644 --- a/esphome/core/scheduler.cpp +++ b/esphome/core/scheduler.cpp @@ -73,8 +73,6 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type if (delay == SCHEDULER_DONT_RUN) return; - const auto now = this->millis_(); - // Create and populate the scheduler item auto item = make_unique(); item->component = component; @@ -83,6 +81,19 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type item->callback = std::move(func); item->remove = false; +#if !defined(USE_ESP8266) && !defined(USE_RP2040) + // Special handling for defer() (delay = 0, type = TIMEOUT) + // ESP8266 and RP2040 are excluded because they don't need thread-safe defer handling + if (delay == 0 && type == SchedulerItem::TIMEOUT) { + // Put in defer queue for guaranteed FIFO execution + LockGuard guard{this->lock_}; + this->defer_queue_.push_back(std::move(item)); + return; + } +#endif + + const auto now = this->millis_(); + // Type-specific setup if (type == SchedulerItem::INTERVAL) { item->interval = delay; @@ -209,6 +220,35 @@ optional HOT Scheduler::next_schedule_in() { return item->next_execution_ - now; } void HOT Scheduler::call() { +#if !defined(USE_ESP8266) && !defined(USE_RP2040) + // Process defer queue first to guarantee FIFO execution order for deferred items. + // Previously, defer() used the heap which gave undefined order for equal timestamps, + // causing race conditions on multi-core systems (ESP32, BK7200). + // With the defer queue: + // - Deferred items (delay=0) go directly to defer_queue_ in set_timer_common_ + // - Items execute in exact order they were deferred (FIFO guarantee) + // - No deferred items exist in to_add_, so processing order doesn't affect correctness + // ESP8266 and RP2040 don't use this queue - they fall back to the heap-based approach + // (ESP8266: single-core, RP2040: empty mutex implementation). + while (!this->defer_queue_.empty()) { + // The outer check is done without a lock for performance. If the queue + // appears non-empty, we lock and process an item. We don't need to check + // empty() again inside the lock because only this thread can remove items. + std::unique_ptr item; + { + LockGuard lock(this->lock_); + item = std::move(this->defer_queue_.front()); + this->defer_queue_.pop_front(); + } + + // Execute callback without holding lock to prevent deadlocks + // if the callback tries to call defer() again + if (!this->should_skip_item_(item.get())) { + this->execute_item_(item.get()); + } + } +#endif + const auto now = this->millis_(); this->process_to_add(); @@ -282,8 +322,6 @@ void HOT Scheduler::call() { this->pop_raw_(); continue; } - App.set_current_component(item->component); - #ifdef ESPHOME_DEBUG_SCHEDULER const char *item_name = item->get_name(); ESP_LOGV(TAG, "Running %s '%s/%s' with interval=%" PRIu32 " next_execution=%" PRIu64 " (now=%" PRIu64 ")", @@ -294,13 +332,7 @@ void HOT Scheduler::call() { // Warning: During callback(), a lot of stuff can happen, including: // - timeouts/intervals get added, potentially invalidating vector pointers // - timeouts/intervals get cancelled - { - uint32_t now_ms = millis(); - WarnIfComponentBlockingGuard guard{item->component, now_ms}; - item->callback(); - // Call finish to ensure blocking time is properly calculated and reported - guard.finish(); - } + this->execute_item_(item.get()); } { @@ -364,6 +396,26 @@ void HOT Scheduler::push_(std::unique_ptr item) { LockGuard guard{this->lock_}; this->to_add_.push_back(std::move(item)); } +// Helper function to check if item matches criteria for cancellation +bool HOT Scheduler::matches_item_(const std::unique_ptr &item, Component *component, + const char *name_cstr, SchedulerItem::Type type) { + if (item->component != component || item->type != type || item->remove) { + return false; + } + const char *item_name = item->get_name(); + return item_name != nullptr && strcmp(name_cstr, item_name) == 0; +} + +// Helper to execute a scheduler item +void HOT Scheduler::execute_item_(SchedulerItem *item) { + App.set_current_component(item->component); + + uint32_t now_ms = millis(); + WarnIfComponentBlockingGuard guard{item->component, now_ms}; + item->callback(); + guard.finish(); +} + // Common implementation for cancel operations bool HOT Scheduler::cancel_item_common_(Component *component, bool is_static_string, const void *name_ptr, SchedulerItem::Type type) { @@ -379,19 +431,28 @@ bool HOT Scheduler::cancel_item_common_(Component *component, bool is_static_str LockGuard guard{this->lock_}; bool ret = false; - for (auto &it : this->items_) { - const char *item_name = it->get_name(); - if (it->component == component && item_name != nullptr && strcmp(name_cstr, item_name) == 0 && it->type == type && - !it->remove) { - to_remove_++; - it->remove = true; + // Check all containers for matching items +#if !defined(USE_ESP8266) && !defined(USE_RP2040) + // Only check defer_queue_ on platforms that have it + for (auto &item : this->defer_queue_) { + if (this->matches_item_(item, component, name_cstr, type)) { + item->remove = true; ret = true; } } - for (auto &it : this->to_add_) { - const char *item_name = it->get_name(); - if (it->component == component && item_name != nullptr && strcmp(name_cstr, item_name) == 0 && it->type == type) { - it->remove = true; +#endif + + for (auto &item : this->items_) { + if (this->matches_item_(item, component, name_cstr, type)) { + item->remove = true; + ret = true; + this->to_remove_++; // Only track removals for heap items + } + } + + for (auto &item : this->to_add_) { + if (this->matches_item_(item, component, name_cstr, type)) { + item->remove = true; ret = true; } } diff --git a/esphome/core/scheduler.h b/esphome/core/scheduler.h index a64968932e..bf5e63cccf 100644 --- a/esphome/core/scheduler.h +++ b/esphome/core/scheduler.h @@ -2,6 +2,7 @@ #include #include +#include #include "esphome/core/component.h" #include "esphome/core/helpers.h" @@ -142,9 +143,22 @@ class Scheduler { // Common implementation for cancel operations bool cancel_item_common_(Component *component, bool is_static_string, const void *name_ptr, SchedulerItem::Type type); + private: bool cancel_item_(Component *component, const std::string &name, SchedulerItem::Type type); bool cancel_item_(Component *component, const char *name, SchedulerItem::Type type); + // Helper functions for cancel operations + bool matches_item_(const std::unique_ptr &item, Component *component, const char *name_cstr, + SchedulerItem::Type type); + + // Helper to execute a scheduler item + void execute_item_(SchedulerItem *item); + + // Helper to check if item should be skipped + bool should_skip_item_(const SchedulerItem *item) const { + return item->remove || (item->component != nullptr && item->component->is_failed()); + } + bool empty_() { this->cleanup_(); return this->items_.empty(); @@ -153,6 +167,13 @@ class Scheduler { Mutex lock_; std::vector> items_; std::vector> to_add_; +#if !defined(USE_ESP8266) && !defined(USE_RP2040) + // ESP8266 and RP2040 don't need the defer queue because: + // ESP8266: Single-core with no preemptive multitasking + // RP2040: Currently has empty mutex implementation in ESPHome + // Both platforms save 40 bytes of RAM by excluding this + std::deque> defer_queue_; // FIFO queue for defer() calls +#endif uint32_t last_millis_{0}; uint16_t millis_major_{0}; uint32_t to_remove_{0}; diff --git a/script/ci-custom.py b/script/ci-custom.py index fbabbc1e74..d0b518251f 100755 --- a/script/ci-custom.py +++ b/script/ci-custom.py @@ -559,6 +559,12 @@ def lint_relative_py_import(fname): "esphome/components/libretiny/core.cpp", "esphome/components/host/core.cpp", "esphome/components/zephyr/core.cpp", + "esphome/components/esp32/helpers.cpp", + "esphome/components/esp8266/helpers.cpp", + "esphome/components/rp2040/helpers.cpp", + "esphome/components/libretiny/helpers.cpp", + "esphome/components/host/helpers.cpp", + "esphome/components/zephyr/helpers.cpp", "esphome/components/http_request/httplib.h", ], ) diff --git a/tests/components/camera/common.yaml b/tests/components/camera/common.yaml new file mode 100644 index 0000000000..3daf1e8565 --- /dev/null +++ b/tests/components/camera/common.yaml @@ -0,0 +1,18 @@ +esphome: + includes: + - ../../../esphome/components/camera/ + +script: + - id: interface_compile_check + then: + - lambda: |- + using namespace esphome::camera; + class MockCamera : public Camera { + public: + void add_image_callback(std::function)> &&callback) override {} + CameraImageReader *create_image_reader() override { return 0; } + void request_image(CameraRequester requester) override {} + void start_stream(CameraRequester requester) override {} + void stop_stream(CameraRequester requester) override {} + }; + MockCamera* camera = new MockCamera(); diff --git a/tests/components/camera/test.esp32-ard.yaml b/tests/components/camera/test.esp32-ard.yaml new file mode 100644 index 0000000000..dade44d145 --- /dev/null +++ b/tests/components/camera/test.esp32-ard.yaml @@ -0,0 +1 @@ +<<: !include common.yaml diff --git a/tests/components/camera/test.esp32-idf.yaml b/tests/components/camera/test.esp32-idf.yaml new file mode 100644 index 0000000000..dade44d145 --- /dev/null +++ b/tests/components/camera/test.esp32-idf.yaml @@ -0,0 +1 @@ +<<: !include common.yaml diff --git a/tests/components/http_request/common.yaml b/tests/components/http_request/common.yaml index af4852901f..97961007e2 100644 --- a/tests/components/http_request/common.yaml +++ b/tests/components/http_request/common.yaml @@ -91,3 +91,5 @@ update: name: OTA Update id: ota_update source: http://my.ha.net:8123/local/esphome/manifest.json + on_update_available: + - logger.log: "A new update is available" diff --git a/tests/components/inkplate6/common.yaml b/tests/components/inkplate6/common.yaml index 31b14e6c73..6cb5d055b6 100644 --- a/tests/components/inkplate6/common.yaml +++ b/tests/components/inkplate6/common.yaml @@ -3,6 +3,9 @@ i2c: scl: 16 sda: 17 +esp32: + cpu_frequency: 240MHz + display: - platform: inkplate6 id: inkplate_display diff --git a/tests/components/sx126x/common.yaml b/tests/components/sx126x/common.yaml new file mode 100644 index 0000000000..3f888c3ce4 --- /dev/null +++ b/tests/components/sx126x/common.yaml @@ -0,0 +1,40 @@ +spi: + clk_pin: ${clk_pin} + mosi_pin: ${mosi_pin} + miso_pin: ${miso_pin} + +sx126x: + dio1_pin: ${dio1_pin} + cs_pin: ${cs_pin} + busy_pin: ${busy_pin} + rst_pin: ${rst_pin} + pa_power: 3 + bandwidth: 125_0kHz + crc_enable: true + frequency: 433920000 + modulation: LORA + rx_start: true + hw_version: sx1262 + rf_switch: true + sync_value: [0x14, 0x24] + preamble_size: 8 + spreading_factor: 7 + coding_rate: CR_4_6 + tcxo_voltage: 1_8V + tcxo_delay: 5ms + on_packet: + then: + - lambda: |- + ESP_LOGD("lambda", "packet %.2f %.2f %s", rssi, snr, format_hex(x).c_str()); + +button: + - platform: template + name: "SX126x Button" + on_press: + then: + - sx126x.set_mode_standby + - sx126x.run_image_cal + - sx126x.set_mode_sleep + - sx126x.set_mode_rx + - sx126x.send_packet: + data: [0xC5, 0x51, 0x78, 0x82, 0xB7, 0xF9, 0x9C, 0x5C] diff --git a/tests/components/sx126x/test.esp32-ard.yaml b/tests/components/sx126x/test.esp32-ard.yaml new file mode 100644 index 0000000000..9770f52229 --- /dev/null +++ b/tests/components/sx126x/test.esp32-ard.yaml @@ -0,0 +1,10 @@ +substitutions: + clk_pin: GPIO5 + mosi_pin: GPIO27 + miso_pin: GPIO19 + cs_pin: GPIO18 + rst_pin: GPIO23 + busy_pin: GPIO25 + dio1_pin: GPIO26 + +<<: !include common.yaml diff --git a/tests/components/sx126x/test.esp32-c3-ard.yaml b/tests/components/sx126x/test.esp32-c3-ard.yaml new file mode 100644 index 0000000000..91450e24ce --- /dev/null +++ b/tests/components/sx126x/test.esp32-c3-ard.yaml @@ -0,0 +1,10 @@ +substitutions: + clk_pin: GPIO5 + mosi_pin: GPIO18 + miso_pin: GPIO19 + cs_pin: GPIO1 + rst_pin: GPIO2 + busy_pin: GPIO4 + dio1_pin: GPIO3 + +<<: !include common.yaml diff --git a/tests/components/sx126x/test.esp32-c3-idf.yaml b/tests/components/sx126x/test.esp32-c3-idf.yaml new file mode 100644 index 0000000000..91450e24ce --- /dev/null +++ b/tests/components/sx126x/test.esp32-c3-idf.yaml @@ -0,0 +1,10 @@ +substitutions: + clk_pin: GPIO5 + mosi_pin: GPIO18 + miso_pin: GPIO19 + cs_pin: GPIO1 + rst_pin: GPIO2 + busy_pin: GPIO4 + dio1_pin: GPIO3 + +<<: !include common.yaml diff --git a/tests/components/sx126x/test.esp32-idf.yaml b/tests/components/sx126x/test.esp32-idf.yaml new file mode 100644 index 0000000000..9770f52229 --- /dev/null +++ b/tests/components/sx126x/test.esp32-idf.yaml @@ -0,0 +1,10 @@ +substitutions: + clk_pin: GPIO5 + mosi_pin: GPIO27 + miso_pin: GPIO19 + cs_pin: GPIO18 + rst_pin: GPIO23 + busy_pin: GPIO25 + dio1_pin: GPIO26 + +<<: !include common.yaml diff --git a/tests/components/sx126x/test.esp8266-ard.yaml b/tests/components/sx126x/test.esp8266-ard.yaml new file mode 100644 index 0000000000..d2c07c5bb7 --- /dev/null +++ b/tests/components/sx126x/test.esp8266-ard.yaml @@ -0,0 +1,10 @@ +substitutions: + clk_pin: GPIO5 + mosi_pin: GPIO13 + miso_pin: GPIO12 + cs_pin: GPIO1 + rst_pin: GPIO2 + busy_pin: GPIO4 + dio1_pin: GPIO3 + +<<: !include common.yaml diff --git a/tests/components/sx126x/test.rp2040-ard.yaml b/tests/components/sx126x/test.rp2040-ard.yaml new file mode 100644 index 0000000000..8881e96971 --- /dev/null +++ b/tests/components/sx126x/test.rp2040-ard.yaml @@ -0,0 +1,10 @@ +substitutions: + clk_pin: GPIO2 + mosi_pin: GPIO3 + miso_pin: GPIO4 + cs_pin: GPIO5 + rst_pin: GPIO6 + busy_pin: GPIO8 + dio1_pin: GPIO7 + +<<: !include common.yaml diff --git a/tests/components/update/common.yaml b/tests/components/update/common.yaml index dcb4f42527..45ed110352 100644 --- a/tests/components/update/common.yaml +++ b/tests/components/update/common.yaml @@ -26,3 +26,5 @@ update: - platform: http_request name: Firmware Update source: http://example.com/manifest.json + on_update_available: + - logger.log: "A new update is available" diff --git a/tests/integration/fixtures/defer_fifo_simple.yaml b/tests/integration/fixtures/defer_fifo_simple.yaml new file mode 100644 index 0000000000..db24ebf601 --- /dev/null +++ b/tests/integration/fixtures/defer_fifo_simple.yaml @@ -0,0 +1,109 @@ +esphome: + name: defer-fifo-simple + +host: + +logger: + level: DEBUG + +api: + services: + - service: test_set_timeout + then: + - lambda: |- + // Test set_timeout with 0 delay (direct scheduler call) + static int set_timeout_order = 0; + static bool set_timeout_passed = true; + + // Reset for this test + set_timeout_order = 0; + set_timeout_passed = true; + + ESP_LOGD("defer_test", "Testing set_timeout(0) for FIFO order..."); + for (int i = 0; i < 10; i++) { + int expected = i; + App.scheduler.set_timeout((Component*)nullptr, nullptr, 0, [expected]() { + ESP_LOGD("defer_test", "set_timeout(0) item %d executed, order %d", expected, set_timeout_order); + if (set_timeout_order != expected) { + ESP_LOGE("defer_test", "FIFO violation in set_timeout: expected %d but got execution order %d", expected, set_timeout_order); + set_timeout_passed = false; + } + set_timeout_order++; + + if (set_timeout_order == 10) { + if (set_timeout_passed) { + ESP_LOGI("defer_test", "✓ Test PASSED - set_timeout(0) maintains FIFO order"); + id(test_result)->trigger("passed"); + } else { + ESP_LOGE("defer_test", "✗ Test FAILED - set_timeout(0) executed out of order"); + id(test_result)->trigger("failed"); + } + id(test_complete)->trigger("test_finished"); + } + }); + } + + ESP_LOGD("defer_test", "Deferred 10 items using set_timeout(0), waiting for execution..."); + + - service: test_defer + then: + - lambda: |- + // Test defer() method (component method) + static int defer_order = 0; + static bool defer_passed = true; + + // Reset for this test + defer_order = 0; + defer_passed = true; + + ESP_LOGD("defer_test", "Testing defer() for FIFO order..."); + + // Create a test component class that exposes defer() + class TestComponent : public Component { + public: + void test_defer() { + for (int i = 0; i < 10; i++) { + int expected = i; + this->defer([expected]() { + ESP_LOGD("defer_test", "defer() item %d executed, order %d", expected, defer_order); + if (defer_order != expected) { + ESP_LOGE("defer_test", "FIFO violation in defer: expected %d but got execution order %d", expected, defer_order); + defer_passed = false; + } + defer_order++; + + if (defer_order == 10) { + if (defer_passed) { + ESP_LOGI("defer_test", "✓ Test PASSED - defer() maintains FIFO order"); + id(test_result)->trigger("passed"); + } else { + ESP_LOGE("defer_test", "✗ Test FAILED - defer() executed out of order"); + id(test_result)->trigger("failed"); + } + id(test_complete)->trigger("test_finished"); + } + }); + } + } + }; + + // Use a static instance so it doesn't go out of scope + static TestComponent test_component; + test_component.test_defer(); + + ESP_LOGD("defer_test", "Deferred 10 items using defer(), waiting for execution..."); + +event: + - platform: template + name: "Test Complete" + id: test_complete + device_class: button + event_types: + - "test_finished" + - platform: template + name: "Test Result" + id: test_result + device_class: button + event_types: + - "passed" + - "failed" diff --git a/tests/integration/fixtures/defer_stress.yaml b/tests/integration/fixtures/defer_stress.yaml new file mode 100644 index 0000000000..6df475229b --- /dev/null +++ b/tests/integration/fixtures/defer_stress.yaml @@ -0,0 +1,38 @@ +esphome: + name: defer-stress-test + +external_components: + - source: + type: local + path: EXTERNAL_COMPONENT_PATH + components: [defer_stress_component] + +host: + +logger: + level: VERBOSE + +defer_stress_component: + id: defer_stress + +api: + services: + - service: run_stress_test + then: + - lambda: |- + id(defer_stress)->run_multi_thread_test(); + +event: + - platform: template + name: "Test Complete" + id: test_complete + device_class: button + event_types: + - "test_finished" + - platform: template + name: "Test Result" + id: test_result + device_class: button + event_types: + - "passed" + - "failed" diff --git a/tests/integration/fixtures/external_components/defer_stress_component/__init__.py b/tests/integration/fixtures/external_components/defer_stress_component/__init__.py new file mode 100644 index 0000000000..177e595f51 --- /dev/null +++ b/tests/integration/fixtures/external_components/defer_stress_component/__init__.py @@ -0,0 +1,19 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.const import CONF_ID + +defer_stress_component_ns = cg.esphome_ns.namespace("defer_stress_component") +DeferStressComponent = defer_stress_component_ns.class_( + "DeferStressComponent", cg.Component +) + +CONFIG_SCHEMA = cv.Schema( + { + cv.GenerateID(): cv.declare_id(DeferStressComponent), + } +).extend(cv.COMPONENT_SCHEMA) + + +async def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + await cg.register_component(var, config) diff --git a/tests/integration/fixtures/external_components/defer_stress_component/defer_stress_component.cpp b/tests/integration/fixtures/external_components/defer_stress_component/defer_stress_component.cpp new file mode 100644 index 0000000000..21ca45947e --- /dev/null +++ b/tests/integration/fixtures/external_components/defer_stress_component/defer_stress_component.cpp @@ -0,0 +1,75 @@ +#include "defer_stress_component.h" +#include "esphome/core/log.h" +#include +#include +#include +#include + +namespace esphome { +namespace defer_stress_component { + +static const char *const TAG = "defer_stress"; + +void DeferStressComponent::setup() { ESP_LOGCONFIG(TAG, "DeferStressComponent setup"); } + +void DeferStressComponent::run_multi_thread_test() { + // Use member variables instead of static to avoid issues + this->total_defers_ = 0; + this->executed_defers_ = 0; + static constexpr int NUM_THREADS = 10; + static constexpr int DEFERS_PER_THREAD = 100; + + ESP_LOGI(TAG, "Starting defer stress test - multi-threaded concurrent defers"); + + // Ensure we're starting clean + ESP_LOGI(TAG, "Initial counters: total=%d, executed=%d", this->total_defers_.load(), this->executed_defers_.load()); + + // Track start time + auto start_time = std::chrono::steady_clock::now(); + + // Create threads + std::vector threads; + + ESP_LOGI(TAG, "Creating %d threads, each will defer %d callbacks", NUM_THREADS, DEFERS_PER_THREAD); + + threads.reserve(NUM_THREADS); + for (int i = 0; i < NUM_THREADS; i++) { + threads.emplace_back([this, i]() { + ESP_LOGV(TAG, "Thread %d starting", i); + // Each thread directly calls defer() without any locking + for (int j = 0; j < DEFERS_PER_THREAD; j++) { + int defer_id = this->total_defers_.fetch_add(1); + ESP_LOGV(TAG, "Thread %d calling defer for request %d", i, defer_id); + + // Capture this pointer safely for the lambda + auto *component = this; + + // Directly call defer() from this thread - no locking! + this->defer([component, i, j, defer_id]() { + component->executed_defers_.fetch_add(1); + ESP_LOGV(TAG, "Executed defer %d (thread %d, index %d)", defer_id, i, j); + }); + + ESP_LOGV(TAG, "Thread %d called defer for request %d successfully", i, defer_id); + + // Small random delay to increase contention + if (j % 10 == 0) { + std::this_thread::sleep_for(std::chrono::microseconds(100)); + } + } + ESP_LOGV(TAG, "Thread %d finished", i); + }); + } + + // Wait for all threads to complete + for (auto &t : threads) { + t.join(); + } + + auto end_time = std::chrono::steady_clock::now(); + auto thread_time = std::chrono::duration_cast(end_time - start_time).count(); + ESP_LOGI(TAG, "All threads finished in %lldms. Created %d defer requests", thread_time, this->total_defers_.load()); +} + +} // namespace defer_stress_component +} // namespace esphome diff --git a/tests/integration/fixtures/external_components/defer_stress_component/defer_stress_component.h b/tests/integration/fixtures/external_components/defer_stress_component/defer_stress_component.h new file mode 100644 index 0000000000..59b7565726 --- /dev/null +++ b/tests/integration/fixtures/external_components/defer_stress_component/defer_stress_component.h @@ -0,0 +1,20 @@ +#pragma once + +#include "esphome/core/component.h" +#include + +namespace esphome { +namespace defer_stress_component { + +class DeferStressComponent : public Component { + public: + void setup() override; + void run_multi_thread_test(); + + private: + std::atomic total_defers_{0}; + std::atomic executed_defers_{0}; +}; + +} // namespace defer_stress_component +} // namespace esphome diff --git a/tests/integration/test_defer_fifo_simple.py b/tests/integration/test_defer_fifo_simple.py new file mode 100644 index 0000000000..5a62a45786 --- /dev/null +++ b/tests/integration/test_defer_fifo_simple.py @@ -0,0 +1,117 @@ +"""Simple test that defer() maintains FIFO order.""" + +import asyncio + +from aioesphomeapi import EntityState, Event, EventInfo, UserService +import pytest + +from .types import APIClientConnectedFactory, RunCompiledFunction + + +@pytest.mark.asyncio +async def test_defer_fifo_simple( + yaml_config: str, + run_compiled: RunCompiledFunction, + api_client_connected: APIClientConnectedFactory, +) -> None: + """Test that defer() maintains FIFO order with a simple test.""" + + async with run_compiled(yaml_config), api_client_connected() as client: + # Verify we can connect + device_info = await client.device_info() + assert device_info is not None + assert device_info.name == "defer-fifo-simple" + + # List entities and services + entity_info, services = await asyncio.wait_for( + client.list_entities_services(), timeout=5.0 + ) + + # Find our test entities + test_complete_entity: EventInfo | None = None + test_result_entity: EventInfo | None = None + + for entity in entity_info: + if isinstance(entity, EventInfo): + if entity.object_id == "test_complete": + test_complete_entity = entity + elif entity.object_id == "test_result": + test_result_entity = entity + + assert test_complete_entity is not None, "test_complete event not found" + assert test_result_entity is not None, "test_result event not found" + + # Find our test services + test_set_timeout_service: UserService | None = None + test_defer_service: UserService | None = None + for service in services: + if service.name == "test_set_timeout": + test_set_timeout_service = service + elif service.name == "test_defer": + test_defer_service = service + + assert test_set_timeout_service is not None, ( + "test_set_timeout service not found" + ) + assert test_defer_service is not None, "test_defer service not found" + + # Get the event loop + loop = asyncio.get_running_loop() + + # Subscribe to states + # (events are delivered as EventStates through subscribe_states) + test_complete_future: asyncio.Future[bool] = loop.create_future() + test_result_future: asyncio.Future[bool] = loop.create_future() + + def on_state(state: EntityState) -> None: + if not isinstance(state, Event): + return + + if ( + state.key == test_complete_entity.key + and state.event_type == "test_finished" + and not test_complete_future.done() + ): + test_complete_future.set_result(True) + return + + if state.key == test_result_entity.key and not test_result_future.done(): + if state.event_type == "passed": + test_result_future.set_result(True) + elif state.event_type == "failed": + test_result_future.set_result(False) + + client.subscribe_states(on_state) + + # Test 1: Test set_timeout(0) + client.execute_service(test_set_timeout_service, {}) + + # Wait for first test completion + try: + await asyncio.wait_for(test_complete_future, timeout=5.0) + test1_passed = await asyncio.wait_for(test_result_future, timeout=1.0) + except asyncio.TimeoutError: + pytest.fail("Test set_timeout(0) did not complete within 5 seconds") + + assert test1_passed is True, ( + "set_timeout(0) FIFO test failed - items executed out of order" + ) + + # Reset futures for second test + test_complete_future = loop.create_future() + test_result_future = loop.create_future() + + # Test 2: Test defer() + client.execute_service(test_defer_service, {}) + + # Wait for second test completion + try: + await asyncio.wait_for(test_complete_future, timeout=5.0) + test2_passed = await asyncio.wait_for(test_result_future, timeout=1.0) + except asyncio.TimeoutError: + pytest.fail("Test defer() did not complete within 5 seconds") + + # Verify the test passed + assert test2_passed is True, ( + "defer() FIFO test failed - items executed out of order" + ) diff --git a/tests/integration/test_defer_stress.py b/tests/integration/test_defer_stress.py new file mode 100644 index 0000000000..f63ec8d25f --- /dev/null +++ b/tests/integration/test_defer_stress.py @@ -0,0 +1,137 @@ +"""Stress test for defer() thread safety with multiple threads.""" + +import asyncio +from pathlib import Path +import re + +from aioesphomeapi import UserService +import pytest + +from .types import APIClientConnectedFactory, RunCompiledFunction + + +@pytest.mark.asyncio +async def test_defer_stress( + yaml_config: str, + run_compiled: RunCompiledFunction, + api_client_connected: APIClientConnectedFactory, +) -> None: + """Test that defer() doesn't crash when called rapidly from multiple threads.""" + + # Get the absolute path to the external components directory + external_components_path = str( + Path(__file__).parent / "fixtures" / "external_components" + ) + + # Replace the placeholder in the YAML config with the actual path + yaml_config = yaml_config.replace( + "EXTERNAL_COMPONENT_PATH", external_components_path + ) + + # Create a future to signal test completion + loop = asyncio.get_event_loop() + test_complete_future: asyncio.Future[None] = loop.create_future() + + # Track executed defers and their order + executed_defers: set[int] = set() + thread_executions: dict[ + int, list[int] + ] = {} # thread_id -> list of indices in execution order + fifo_violations: list[str] = [] + + def on_log_line(line: str) -> None: + # Track all executed defers with thread and index info + match = re.search(r"Executed defer (\d+) \(thread (\d+), index (\d+)\)", line) + if not match: + return + + defer_id = int(match.group(1)) + thread_id = int(match.group(2)) + index = int(match.group(3)) + + executed_defers.add(defer_id) + + # Track execution order per thread + if thread_id not in thread_executions: + thread_executions[thread_id] = [] + + # Check FIFO ordering within thread + if thread_executions[thread_id] and thread_executions[thread_id][-1] >= index: + fifo_violations.append( + f"Thread {thread_id}: index {index} executed after " + f"{thread_executions[thread_id][-1]}" + ) + + thread_executions[thread_id].append(index) + + # Check if we've executed all 1000 defers (0-999) + if len(executed_defers) == 1000 and not test_complete_future.done(): + test_complete_future.set_result(None) + + async with ( + run_compiled(yaml_config, line_callback=on_log_line), + api_client_connected() as client, + ): + # Verify we can connect + device_info = await client.device_info() + assert device_info is not None + assert device_info.name == "defer-stress-test" + + # List entities and services + entity_info, services = await asyncio.wait_for( + client.list_entities_services(), timeout=5.0 + ) + + # Find our test service + run_stress_test_service: UserService | None = None + for service in services: + if service.name == "run_stress_test": + run_stress_test_service = service + break + + assert run_stress_test_service is not None, "run_stress_test service not found" + + # Call the run_stress_test service to start the test + client.execute_service(run_stress_test_service, {}) + + # Wait for all defers to execute (should be quick) + try: + await asyncio.wait_for(test_complete_future, timeout=5.0) + except asyncio.TimeoutError: + # Report how many we got + pytest.fail( + f"Stress test timed out. Only {len(executed_defers)} of " + f"1000 defers executed. Missing IDs: " + f"{sorted(set(range(1000)) - executed_defers)[:10]}..." + ) + + # Verify all defers executed + assert len(executed_defers) == 1000, ( + f"Expected 1000 defers, got {len(executed_defers)}" + ) + + # Verify we have all IDs from 0-999 + expected_ids = set(range(1000)) + missing_ids = expected_ids - executed_defers + assert not missing_ids, f"Missing defer IDs: {sorted(missing_ids)}" + + # Verify FIFO ordering was maintained within each thread + assert not fifo_violations, "FIFO ordering violations detected:\n" + "\n".join( + fifo_violations[:10] + ) + + # Verify each thread executed all its defers in order + for thread_id, indices in thread_executions.items(): + assert len(indices) == 100, ( + f"Thread {thread_id} executed {len(indices)} defers, expected 100" + ) + # Indices should be 0-99 in ascending order + assert indices == list(range(100)), ( + f"Thread {thread_id} executed indices out of order: {indices[:10]}..." + ) + + # If we got here without crashing and with proper ordering, the test passed + assert True, ( + "Test completed successfully - all 1000 defers executed with " + "FIFO ordering preserved" + )