From 4be588c749c36e34d3eb26595f8925c590975f54 Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Sat, 13 Jul 2024 10:23:15 +0200 Subject: [PATCH] Fix linting errors --- .../components/esp32_ble_server/__init__.py | 29 ++++++++--- .../esp32_ble_server/ble_characteristic.cpp | 4 +- .../esp32_ble_server/ble_characteristic.h | 2 +- .../ble_server_automations.cpp | 6 +-- .../esp32_ble_server/ble_server_automations.h | 8 +-- .../esp32_improv/esp32_improv_component.cpp | 8 +-- esphome/core/event_emitter.h | 51 ++++++++++++------- esphome/core/helpers.cpp | 14 +++-- 8 files changed, 78 insertions(+), 44 deletions(-) diff --git a/esphome/components/esp32_ble_server/__init__.py b/esphome/components/esp32_ble_server/__init__.py index a3fcb0d74e..1c2c112885 100644 --- a/esphome/components/esp32_ble_server/__init__.py +++ b/esphome/components/esp32_ble_server/__init__.py @@ -106,12 +106,10 @@ SERVICE_CHARACTERISTIC_SCHEMA = cv.Schema( cv.Optional(CONF_INDICATE, default=False): cv.boolean, cv.Optional(CONF_WRITE_NO_RESPONSE, default=False): cv.boolean, cv.Optional(CONF_VALUE): CHARACTERISTIC_VALUE_SCHEMA, - cv.GenerateID(CONF_VALUE_ACTION_ID): cv.declare_id( + cv.GenerateID(CONF_VALUE_ACTION_ID_): cv.declare_id( BLECharacteristicSetValueAction ), - cv.Optional(CONF_DESCRIPTORS, default=[]): cv.ensure_list( - DESCRIPTOR_SCHEMA - ), + cv.Optional(CONF_DESCRIPTORS, default=[]): cv.ensure_list(DESCRIPTOR_SCHEMA), cv.Optional(CONF_ON_WRITE): automation.validate_automation( {cv.GenerateID(): cv.declare_id(BLECharacteristic)}, single=True ), @@ -262,7 +260,9 @@ async def to_code(config): if CONF_ON_WRITE in char_conf: on_write_conf = char_conf[CONF_ON_WRITE] if not char_conf[CONF_WRITE] and not char_conf[CONF_WRITE_NO_RESPONSE]: - raise cv.Invalid(f"on_write requires the {CONF_WRITE} or {CONF_WRITE_NO_RESPONSE} property to be set") + raise cv.Invalid( + f"on_write requires the {CONF_WRITE} or {CONF_WRITE_NO_RESPONSE} property to be set" + ) await automation.build_automation( BLETriggers_ns.create_on_write_trigger(char_var), [(cg.std_vector.template(cg.uint8), "x")], @@ -273,10 +273,17 @@ async def to_code(config): CONF_ID: char_conf[CONF_ID], CONF_VALUE: char_conf[CONF_VALUE], } - value_action = await ble_server_characteristic_set_value(action_conf, char_conf[CONF_VALUE_ACTION_ID_], cg.TemplateArguments(None), {}) + value_action = await ble_server_characteristic_set_value( + action_conf, + char_conf[CONF_VALUE_ACTION_ID_], + cg.TemplateArguments(None), + {}, + ) cg.add(value_action.play()) for descriptor_conf in char_conf[CONF_DESCRIPTORS]: - descriptor_value, max_length = parse_descriptor_value(descriptor_conf[CONF_VALUE]) + descriptor_value, max_length = parse_descriptor_value( + descriptor_conf[CONF_VALUE] + ) desc_var = cg.new_Pvariable( descriptor_conf[CONF_ID], parse_uuid(descriptor_conf[CONF_UUID]), @@ -284,6 +291,7 @@ async def to_code(config): ) if CONF_VALUE in descriptor_conf: cg.add(desc_var.set_value(descriptor_value)) + cg.add(char_var.add_descriptor(desc_var)) cg.add(var.enqueue_start_service(service_var)) cg.add_define("USE_ESP32_BLE_SERVER") if CORE.using_esp_idf: @@ -292,7 +300,12 @@ async def to_code(config): async def parse_characteristic_value(value, args): if isinstance(value, cv.Lambda): - return await cg.templatable(value, args, cg.std_vector.template(cg.uint8), cg.std_vector.template(cg.uint8)) + return await cg.templatable( + value, + args, + cg.std_vector.template(cg.uint8), + cg.std_vector.template(cg.uint8), + ) if isinstance(value, list): return cg.std_vector.template(cg.uint8)(value) # Transform the value into a vector of bytes diff --git a/esphome/components/esp32_ble_server/ble_characteristic.cpp b/esphome/components/esp32_ble_server/ble_characteristic.cpp index 2c85bfb032..200092b39a 100644 --- a/esphome/components/esp32_ble_server/ble_characteristic.cpp +++ b/esphome/components/esp32_ble_server/ble_characteristic.cpp @@ -249,7 +249,7 @@ void BLECharacteristic::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt if (!param->write.is_prep) { this->EventEmitter>::emit( - BLECharacteristicEvt::VectorEvt::ON_WRITE, this->value_); + BLECharacteristicEvt::VectorEvt::ON_WRITE, this->value_); } break; @@ -261,7 +261,7 @@ void BLECharacteristic::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt this->write_event_ = false; if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) { this->EventEmitter>::emit( - BLECharacteristicEvt::VectorEvt::ON_WRITE, this->value_); + BLECharacteristicEvt::VectorEvt::ON_WRITE, this->value_); } esp_err_t err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, nullptr); diff --git a/esphome/components/esp32_ble_server/ble_characteristic.h b/esphome/components/esp32_ble_server/ble_characteristic.h index 467079dcb6..220d89d0a7 100644 --- a/esphome/components/esp32_ble_server/ble_characteristic.h +++ b/esphome/components/esp32_ble_server/ble_characteristic.h @@ -32,7 +32,7 @@ enum VectorEvt { enum EmptyEvt { ON_READ, }; -} +} // namespace BLECharacteristicEvt class BLECharacteristic : public EventEmitter>, public EventEmitter { diff --git a/esphome/components/esp32_ble_server/ble_server_automations.cpp b/esphome/components/esp32_ble_server/ble_server_automations.cpp index 66e54bb793..202d15de17 100644 --- a/esphome/components/esp32_ble_server/ble_server_automations.cpp +++ b/esphome/components/esp32_ble_server/ble_server_automations.cpp @@ -11,10 +11,10 @@ using namespace esp32_ble; Trigger> *BLETriggers::create_on_write_trigger(BLECharacteristic *characteristic) { Trigger> *on_write_trigger = - new Trigger>(); // NOLINT(cppcoreguidelines-owning-memory) + new Trigger>(); // NOLINT(cppcoreguidelines-owning-memory) characteristic->EventEmitter>::on( - BLECharacteristicEvt::VectorEvt::ON_WRITE, - [on_write_trigger](const std::vector &data) { on_write_trigger->trigger(data); }); + BLECharacteristicEvt::VectorEvt::ON_WRITE, + [on_write_trigger](const std::vector &data) { on_write_trigger->trigger(data); }); return on_write_trigger; } diff --git a/esphome/components/esp32_ble_server/ble_server_automations.h b/esphome/components/esp32_ble_server/ble_server_automations.h index 97030dcf68..c7483c7ae5 100644 --- a/esphome/components/esp32_ble_server/ble_server_automations.h +++ b/esphome/components/esp32_ble_server/ble_server_automations.h @@ -49,10 +49,10 @@ template class BLECharacteristicSetValueAction : public Actionparent_->set_value(this->value_.value(x...)); // Set the listener for read events this->listener_id_ = this->parent_->EventEmitter::on( - BLECharacteristicEvt::EmptyEvt::ON_READ, [this, x...](void) { - // Set the value of the characteristic every time it is read - this->parent_->set_value(this->value_.value(x...)); - }); + BLECharacteristicEvt::EmptyEvt::ON_READ, [this, x...](void) { + // Set the value of the characteristic every time it is read + this->parent_->set_value(this->value_.value(x...)); + }); // Set the listener in the global manager so only one BLECharacteristicSetValueAction is set for each characteristic BLECharacteristicSetValueActionManager::get_instance()->set_listener(this->parent_, this->listener_id_); } diff --git a/esphome/components/esp32_improv/esp32_improv_component.cpp b/esphome/components/esp32_improv/esp32_improv_component.cpp index c78fdd0032..e35a441957 100644 --- a/esphome/components/esp32_improv/esp32_improv_component.cpp +++ b/esphome/components/esp32_improv/esp32_improv_component.cpp @@ -41,10 +41,10 @@ void ESP32ImprovComponent::setup_characteristics() { this->rpc_ = this->service_->create_characteristic(improv::RPC_COMMAND_UUID, BLECharacteristic::PROPERTY_WRITE); this->rpc_->EventEmitter>::on( - BLECharacteristicEvt::VectorEvt::ON_WRITE, [this](const std::vector &data) { - if (!data.empty()) { - this->incoming_data_.insert(this->incoming_data_.end(), data.begin(), data.end()); - } + BLECharacteristicEvt::VectorEvt::ON_WRITE, [this](const std::vector &data) { + if (!data.empty()) { + this->incoming_data_.insert(this->incoming_data_.end(), data.begin(), data.end()); + } }); BLEDescriptor *rpc_descriptor = new BLE2902(); this->rpc_->add_descriptor(rpc_descriptor); diff --git a/esphome/core/event_emitter.h b/esphome/core/event_emitter.h index 6338ad37e5..5da9ab888b 100644 --- a/esphome/core/event_emitter.h +++ b/esphome/core/event_emitter.h @@ -1,9 +1,9 @@ #pragma once -#include +#include #include #include -#include -#include + +#include "esphome/core/log.h" namespace esphome { @@ -11,27 +11,22 @@ using EventEmitterListenerID = uint32_t; // EventEmitter class that can emit events with a specific name (it is highly recommended to use an enum class for this) and a list of arguments. // Supports multiple listeners for each event. -template -class EventEmitter { +template class EventEmitter { public: - EventEmitterListenerID on(EvtNames event, std::function listener) { - listeners_[event].emplace_back(++current_id_, [listener](Args... args) { listener(args...); }); - return current_id_; -} + EventEmitterListenerID on(EvtType event, std::function listener) { + EventEmitterListenerID listener_id = get_next_id(event); + listeners_[event][listener_id] = listener; + return listener_id; + } - void off(EvtNames event, EventEmitterListenerID id) { - if (this->listeners_.count(event) == 0) + void off(EvtType event, EventEmitterListenerID id) { + if (listeners_.count(event) == 0) return; - auto &vec = this->listeners_[event]; - vec.erase(std::remove_if(vec.begin(), vec.end(), - [id](const std::pair> &pair) { - return pair.first == id; - }), - vec.end()); + listeners_[event].erase(id); } protected: - void emit(EvtNames event, Args... args) { + void emit(EvtType event, Args... args) { if (listeners_.count(event) == 0) return; for (const auto &listener : listeners_[event]) { @@ -39,8 +34,26 @@ class EventEmitter { } } + EventEmitterListenerID get_next_id(EvtType event) { + // Check if the map is full + if (listeners_[event].size() == std::numeric_limits::max()) { + // Raise an error if the map is full + ESP_LOGE("event_emitter", "EventEmitter has reached the maximum number of listeners for event %d", event); + ESP_LOGW("event_emitter", "Removing listener with ID %d for event %d", 0, event); + off(event, 0); + return 0; + } + // Get the next ID for the given event. + EventEmitterListenerID next_id = (current_id_ + 1) % std::numeric_limits::max(); + while (listeners_[event].count(next_id) > 0) { + next_id = (next_id + 1) % std::numeric_limits::max(); + } + current_id_ = next_id; + return current_id_; + } + private: - std::map>>> listeners_; + std::unordered_map>> listeners_; EventEmitterListenerID current_id_ = 0; }; diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 3764f9b292..855597e0ec 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -534,7 +534,7 @@ std::vector base64_decode(const std::string &encoded_string) { return ret; } -std::vector to_vector(bool value) { return {value ? (uint8_t)1 : (uint8_t)0}; } +std::vector to_vector(bool value) { return {value ? (uint8_t) 1 : (uint8_t) 0}; } std::vector to_vector(uint8_t value) { return {value}; } std::vector to_vector(uint16_t value) { return {uint8_t(value >> 8), uint8_t(value & 0xFF)}; } std::vector to_vector(uint32_t value) { @@ -546,8 +546,16 @@ std::vector to_vector(uint64_t value) { uint8_t((value >> 8) & 0xFF), uint8_t(value & 0xFF)}; } std::vector to_vector(int value) { return to_vector(static_cast(value)); } -std::vector to_vector(float value) { return to_vector(*reinterpret_cast(&value)); } -std::vector to_vector(double value) { return to_vector(*reinterpret_cast(&value)); } +std::vector to_vector(float value) { + uint32_t val; + memcpy(&val, &value, sizeof(val)); + return to_vector(val); +} +std::vector to_vector(double value) { + uint64_t val; + memcpy(&val, &value, sizeof(val)); + return to_vector(val); +} std::vector to_vector(const std::string &value) { return std::vector(value.begin(), value.end()); } // Colors