Fix linting errors

This commit is contained in:
Rapsssito 2024-07-13 10:23:15 +02:00
parent 7942ec4e70
commit 4be588c749
8 changed files with 78 additions and 44 deletions

View file

@ -106,12 +106,10 @@ SERVICE_CHARACTERISTIC_SCHEMA = cv.Schema(
cv.Optional(CONF_INDICATE, default=False): cv.boolean, cv.Optional(CONF_INDICATE, default=False): cv.boolean,
cv.Optional(CONF_WRITE_NO_RESPONSE, default=False): cv.boolean, cv.Optional(CONF_WRITE_NO_RESPONSE, default=False): cv.boolean,
cv.Optional(CONF_VALUE): CHARACTERISTIC_VALUE_SCHEMA, 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 BLECharacteristicSetValueAction
), ),
cv.Optional(CONF_DESCRIPTORS, default=[]): cv.ensure_list( cv.Optional(CONF_DESCRIPTORS, default=[]): cv.ensure_list(DESCRIPTOR_SCHEMA),
DESCRIPTOR_SCHEMA
),
cv.Optional(CONF_ON_WRITE): automation.validate_automation( cv.Optional(CONF_ON_WRITE): automation.validate_automation(
{cv.GenerateID(): cv.declare_id(BLECharacteristic)}, single=True {cv.GenerateID(): cv.declare_id(BLECharacteristic)}, single=True
), ),
@ -262,7 +260,9 @@ async def to_code(config):
if CONF_ON_WRITE in char_conf: if CONF_ON_WRITE in char_conf:
on_write_conf = char_conf[CONF_ON_WRITE] on_write_conf = char_conf[CONF_ON_WRITE]
if not char_conf[CONF_WRITE] and not char_conf[CONF_WRITE_NO_RESPONSE]: 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( await automation.build_automation(
BLETriggers_ns.create_on_write_trigger(char_var), BLETriggers_ns.create_on_write_trigger(char_var),
[(cg.std_vector.template(cg.uint8), "x")], [(cg.std_vector.template(cg.uint8), "x")],
@ -273,10 +273,17 @@ async def to_code(config):
CONF_ID: char_conf[CONF_ID], CONF_ID: char_conf[CONF_ID],
CONF_VALUE: char_conf[CONF_VALUE], 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()) cg.add(value_action.play())
for descriptor_conf in char_conf[CONF_DESCRIPTORS]: 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( desc_var = cg.new_Pvariable(
descriptor_conf[CONF_ID], descriptor_conf[CONF_ID],
parse_uuid(descriptor_conf[CONF_UUID]), parse_uuid(descriptor_conf[CONF_UUID]),
@ -284,6 +291,7 @@ async def to_code(config):
) )
if CONF_VALUE in descriptor_conf: if CONF_VALUE in descriptor_conf:
cg.add(desc_var.set_value(descriptor_value)) 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(var.enqueue_start_service(service_var))
cg.add_define("USE_ESP32_BLE_SERVER") cg.add_define("USE_ESP32_BLE_SERVER")
if CORE.using_esp_idf: if CORE.using_esp_idf:
@ -292,7 +300,12 @@ async def to_code(config):
async def parse_characteristic_value(value, args): async def parse_characteristic_value(value, args):
if isinstance(value, cv.Lambda): 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): if isinstance(value, list):
return cg.std_vector.template(cg.uint8)(value) return cg.std_vector.template(cg.uint8)(value)
# Transform the value into a vector of bytes # Transform the value into a vector of bytes

View file

@ -249,7 +249,7 @@ void BLECharacteristic::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt
if (!param->write.is_prep) { if (!param->write.is_prep) {
this->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::emit( this->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::emit(
BLECharacteristicEvt::VectorEvt::ON_WRITE, this->value_); BLECharacteristicEvt::VectorEvt::ON_WRITE, this->value_);
} }
break; break;
@ -261,7 +261,7 @@ void BLECharacteristic::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt
this->write_event_ = false; this->write_event_ = false;
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) { if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
this->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::emit( this->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::emit(
BLECharacteristicEvt::VectorEvt::ON_WRITE, this->value_); BLECharacteristicEvt::VectorEvt::ON_WRITE, this->value_);
} }
esp_err_t err = esp_err_t err =
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, nullptr); esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, nullptr);

View file

@ -32,7 +32,7 @@ enum VectorEvt {
enum EmptyEvt { enum EmptyEvt {
ON_READ, ON_READ,
}; };
} } // namespace BLECharacteristicEvt
class BLECharacteristic : public EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>, class BLECharacteristic : public EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>,
public EventEmitter<BLECharacteristicEvt::EmptyEvt> { public EventEmitter<BLECharacteristicEvt::EmptyEvt> {

View file

@ -11,10 +11,10 @@ using namespace esp32_ble;
Trigger<std::vector<uint8_t>> *BLETriggers::create_on_write_trigger(BLECharacteristic *characteristic) { Trigger<std::vector<uint8_t>> *BLETriggers::create_on_write_trigger(BLECharacteristic *characteristic) {
Trigger<std::vector<uint8_t>> *on_write_trigger = Trigger<std::vector<uint8_t>> *on_write_trigger =
new Trigger<std::vector<uint8_t>>(); // NOLINT(cppcoreguidelines-owning-memory) new Trigger<std::vector<uint8_t>>(); // NOLINT(cppcoreguidelines-owning-memory)
characteristic->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::on( characteristic->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::on(
BLECharacteristicEvt::VectorEvt::ON_WRITE, BLECharacteristicEvt::VectorEvt::ON_WRITE,
[on_write_trigger](const std::vector<uint8_t> &data) { on_write_trigger->trigger(data); }); [on_write_trigger](const std::vector<uint8_t> &data) { on_write_trigger->trigger(data); });
return on_write_trigger; return on_write_trigger;
} }

View file

@ -49,10 +49,10 @@ template<typename... Ts> class BLECharacteristicSetValueAction : public Action<T
this->parent_->set_value(this->value_.value(x...)); this->parent_->set_value(this->value_.value(x...));
// Set the listener for read events // Set the listener for read events
this->listener_id_ = this->parent_->EventEmitter<BLECharacteristicEvt::EmptyEvt>::on( this->listener_id_ = this->parent_->EventEmitter<BLECharacteristicEvt::EmptyEvt>::on(
BLECharacteristicEvt::EmptyEvt::ON_READ, [this, x...](void) { BLECharacteristicEvt::EmptyEvt::ON_READ, [this, x...](void) {
// Set the value of the characteristic every time it is read // Set the value of the characteristic every time it is read
this->parent_->set_value(this->value_.value(x...)); this->parent_->set_value(this->value_.value(x...));
}); });
// Set the listener in the global manager so only one BLECharacteristicSetValueAction is set for each characteristic // 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_); BLECharacteristicSetValueActionManager::get_instance()->set_listener(this->parent_, this->listener_id_);
} }

View file

@ -41,10 +41,10 @@ void ESP32ImprovComponent::setup_characteristics() {
this->rpc_ = this->service_->create_characteristic(improv::RPC_COMMAND_UUID, BLECharacteristic::PROPERTY_WRITE); this->rpc_ = this->service_->create_characteristic(improv::RPC_COMMAND_UUID, BLECharacteristic::PROPERTY_WRITE);
this->rpc_->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::on( this->rpc_->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::on(
BLECharacteristicEvt::VectorEvt::ON_WRITE, [this](const std::vector<uint8_t> &data) { BLECharacteristicEvt::VectorEvt::ON_WRITE, [this](const std::vector<uint8_t> &data) {
if (!data.empty()) { if (!data.empty()) {
this->incoming_data_.insert(this->incoming_data_.end(), data.begin(), data.end()); this->incoming_data_.insert(this->incoming_data_.end(), data.begin(), data.end());
} }
}); });
BLEDescriptor *rpc_descriptor = new BLE2902(); BLEDescriptor *rpc_descriptor = new BLE2902();
this->rpc_->add_descriptor(rpc_descriptor); this->rpc_->add_descriptor(rpc_descriptor);

View file

@ -1,9 +1,9 @@
#pragma once #pragma once
#include <map> #include <unordered_map>
#include <vector> #include <vector>
#include <functional> #include <functional>
#include <string>
#include <algorithm> #include "esphome/core/log.h"
namespace esphome { 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. // 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. // Supports multiple listeners for each event.
template <typename EvtNames, typename... Args> template <typename EvtType, typename... Args> class EventEmitter {
class EventEmitter {
public: public:
EventEmitterListenerID on(EvtNames event, std::function<void(Args...)> listener) { EventEmitterListenerID on(EvtType event, std::function<void(Args...)> listener) {
listeners_[event].emplace_back(++current_id_, [listener](Args... args) { listener(args...); }); EventEmitterListenerID listener_id = get_next_id(event);
return current_id_; listeners_[event][listener_id] = listener;
} return listener_id;
}
void off(EvtNames event, EventEmitterListenerID id) { void off(EvtType event, EventEmitterListenerID id) {
if (this->listeners_.count(event) == 0) if (listeners_.count(event) == 0)
return; return;
auto &vec = this->listeners_[event]; listeners_[event].erase(id);
vec.erase(std::remove_if(vec.begin(), vec.end(),
[id](const std::pair<EventEmitterListenerID, std::function<void(Args...)>> &pair) {
return pair.first == id;
}),
vec.end());
} }
protected: protected:
void emit(EvtNames event, Args... args) { void emit(EvtType event, Args... args) {
if (listeners_.count(event) == 0) if (listeners_.count(event) == 0)
return; return;
for (const auto &listener : listeners_[event]) { 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<EventEmitterListenerID>::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<EventEmitterListenerID>::max();
while (listeners_[event].count(next_id) > 0) {
next_id = (next_id + 1) % std::numeric_limits<EventEmitterListenerID>::max();
}
current_id_ = next_id;
return current_id_;
}
private: private:
std::map<EvtNames, std::vector<std::pair<EventEmitterListenerID, std::function<void(Args...)>>>> listeners_; std::unordered_map<EvtType, std::unordered_map<EventEmitterListenerID, std::function<void(Args...)>>> listeners_;
EventEmitterListenerID current_id_ = 0; EventEmitterListenerID current_id_ = 0;
}; };

View file

@ -534,7 +534,7 @@ std::vector<uint8_t> base64_decode(const std::string &encoded_string) {
return ret; return ret;
} }
std::vector<uint8_t> to_vector(bool value) { return {value ? (uint8_t)1 : (uint8_t)0}; } std::vector<uint8_t> to_vector(bool value) { return {value ? (uint8_t) 1 : (uint8_t) 0}; }
std::vector<uint8_t> to_vector(uint8_t value) { return {value}; } std::vector<uint8_t> to_vector(uint8_t value) { return {value}; }
std::vector<uint8_t> to_vector(uint16_t value) { return {uint8_t(value >> 8), uint8_t(value & 0xFF)}; } std::vector<uint8_t> to_vector(uint16_t value) { return {uint8_t(value >> 8), uint8_t(value & 0xFF)}; }
std::vector<uint8_t> to_vector(uint32_t value) { std::vector<uint8_t> to_vector(uint32_t value) {
@ -546,8 +546,16 @@ std::vector<uint8_t> to_vector(uint64_t value) {
uint8_t((value >> 8) & 0xFF), uint8_t(value & 0xFF)}; uint8_t((value >> 8) & 0xFF), uint8_t(value & 0xFF)};
} }
std::vector<uint8_t> to_vector(int value) { return to_vector(static_cast<uint32_t>(value)); } std::vector<uint8_t> to_vector(int value) { return to_vector(static_cast<uint32_t>(value)); }
std::vector<uint8_t> to_vector(float value) { return to_vector(*reinterpret_cast<uint32_t *>(&value)); } std::vector<uint8_t> to_vector(float value) {
std::vector<uint8_t> to_vector(double value) { return to_vector(*reinterpret_cast<uint64_t *>(&value)); } uint32_t val;
memcpy(&val, &value, sizeof(val));
return to_vector(val);
}
std::vector<uint8_t> to_vector(double value) {
uint64_t val;
memcpy(&val, &value, sizeof(val));
return to_vector(val);
}
std::vector<uint8_t> to_vector(const std::string &value) { return std::vector<uint8_t>(value.begin(), value.end()); } std::vector<uint8_t> to_vector(const std::string &value) { return std::vector<uint8_t>(value.begin(), value.end()); }
// Colors // Colors