Fix linting issues

This commit is contained in:
Rapsssito 2024-10-20 11:48:25 +02:00
parent 9c64b377c8
commit 6bb7a10deb
7 changed files with 41 additions and 34 deletions

View file

@ -35,7 +35,7 @@ CONF_DESCRIPTORS = "descriptors"
CONF_STRING_ENCODING = "string_encoding" CONF_STRING_ENCODING = "string_encoding"
CONF_DESCRIPTION = "description" CONF_DESCRIPTION = "description"
CONF_CHAR_VALUE_ACTION_ID_ = "characteristic_value_action_id_" CONF_CHAR_VALUE_ACTION_ID_ = "char_value_action_id_"
CONF_VALUE_BUFFER_ = "value_buffer_" CONF_VALUE_BUFFER_ = "value_buffer_"
CONF_CUD_ID_ = "cud_id_" CONF_CUD_ID_ = "cud_id_"
CONF_CUD_VALUE_BUFFER_ = "cud_value_buffer_" CONF_CUD_VALUE_BUFFER_ = "cud_value_buffer_"
@ -149,7 +149,12 @@ def create_notify_cccd(char_config):
# If the CCCD descriptor is already present, return the config # If the CCCD descriptor is already present, return the config
for desc in char_config[CONF_DESCRIPTORS]: for desc in char_config[CONF_DESCRIPTORS]:
if desc[CONF_UUID] == 0x2902: if desc[CONF_UUID] == 0x2902:
return # Check if the WRITE property is set
if not desc[CONF_WRITE]:
raise cv.Invalid(
f"Characteristic {char_config[CONF_UUID]} has notify actions, but the CCCD descriptor does not have the {CONF_WRITE} property set"
)
return char_config
# Manually add the CCCD descriptor # Manually add the CCCD descriptor
char_config[CONF_DESCRIPTORS].append( char_config[CONF_DESCRIPTORS].append(
{ {

View file

@ -55,8 +55,8 @@ void BLECharacteristic::notify() {
ESP_LOGW(TAG, "INDICATE acknowledgment is not yet supported (i.e. it works as a NOTIFY)"); ESP_LOGW(TAG, "INDICATE acknowledgment is not yet supported (i.e. it works as a NOTIFY)");
require_ack = false; require_ack = false;
} }
esp_err_t err = esp_ble_gatts_send_indicate(this->service_->get_server()->get_gatts_if(), client, esp_err_t err = esp_ble_gatts_send_indicate(this->service_->get_server()->get_gatts_if(), client, this->handle_,
this->handle_, length, this->value_.data(), require_ack); length, this->value_.data(), require_ack);
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_ble_gatts_send_indicate failed %d", err); ESP_LOGE(TAG, "esp_ble_gatts_send_indicate failed %d", err);
return; return;
@ -67,9 +67,7 @@ void BLECharacteristic::notify() {
void BLECharacteristic::add_descriptor(BLEDescriptor *descriptor) { void BLECharacteristic::add_descriptor(BLEDescriptor *descriptor) {
// If the descriptor is the CCCD descriptor, listen to its write event to know if the client wants to be notified // If the descriptor is the CCCD descriptor, listen to its write event to know if the client wants to be notified
if (descriptor->get_uuid() == ESPBTUUID::from_uint16(ESP_GATT_UUID_CHAR_CLIENT_CONFIG)) { if (descriptor->get_uuid() == ESPBTUUID::from_uint16(ESP_GATT_UUID_CHAR_CLIENT_CONFIG)) {
descriptor->on( descriptor->on(BLEDescriptorEvt::VectorEvt::ON_WRITE, [this](const std::vector<uint8_t> &value, uint16_t conn_id) {
BLEDescriptorEvt::VectorEvt::ON_WRITE,
[this](const std::vector<uint8_t> &value, uint16_t conn_id) {
if (value.size() != 2) if (value.size() != 2)
return; return;
uint16_t cccd = encode_uint16(value[1], value[0]); uint16_t cccd = encode_uint16(value[1], value[0]);
@ -80,8 +78,7 @@ void BLECharacteristic::add_descriptor(BLEDescriptor *descriptor) {
} else { } else {
this->clients_to_notify_.erase(conn_id); this->clients_to_notify_.erase(conn_id);
} }
} });
);
} }
this->descriptors_.push_back(descriptor); this->descriptors_.push_back(descriptor);
} }
@ -204,8 +201,8 @@ void BLECharacteristic::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt
if (!param->read.need_rsp) if (!param->read.need_rsp)
break; // For some reason you can request a read but not want a response break; // For some reason you can request a read but not want a response
this->EventEmitter<BLECharacteristicEvt::EmptyEvt, uint16_t>::emit_( this->EventEmitter<BLECharacteristicEvt::EmptyEvt, uint16_t>::emit_(BLECharacteristicEvt::EmptyEvt::ON_READ,
BLECharacteristicEvt::EmptyEvt::ON_READ, param->read.conn_id); param->read.conn_id);
uint16_t max_offset = 22; uint16_t max_offset = 22;

View file

@ -72,11 +72,9 @@ void BLEDescriptor::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
break; break;
this->value_.attr_len = param->write.len; this->value_.attr_len = param->write.len;
memcpy(this->value_.attr_value, param->write.value, param->write.len); memcpy(this->value_.attr_value, param->write.value, param->write.len);
this->emit_( this->emit_(BLEDescriptorEvt::VectorEvt::ON_WRITE,
BLEDescriptorEvt::VectorEvt::ON_WRITE,
std::vector<uint8_t>(param->write.value, param->write.value + param->write.len), std::vector<uint8_t>(param->write.value, param->write.value + param->write.len),
param->write.conn_id param->write.conn_id);
);
break; break;
} }
default: default:

View file

@ -34,8 +34,11 @@ enum EmptyEvt {
}; };
} // namespace BLEServerEvt } // namespace BLEServerEvt
class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEventHandler, class BLEServer : public Component,
public Parented<ESP32BLE>, public EventEmitter<BLEServerEvt::EmptyEvt, uint16_t> { public GATTsEventHandler,
public BLEStatusEventHandler,
public Parented<ESP32BLE>,
public EventEmitter<BLEServerEvt::EmptyEvt, uint16_t> {
public: public:
void setup() override; void setup() override;
void loop() override; void loop() override;

View file

@ -9,7 +9,8 @@ namespace esp32_ble_server_automations {
using namespace esp32_ble; using namespace esp32_ble;
Trigger<std::vector<uint8_t>, uint16_t> *BLETriggers::create_characteristic_on_write_trigger(BLECharacteristic *characteristic) { Trigger<std::vector<uint8_t>, uint16_t> *BLETriggers::create_characteristic_on_write_trigger(
BLECharacteristic *characteristic) {
Trigger<std::vector<uint8_t>, uint16_t> *on_write_trigger = // NOLINT(cppcoreguidelines-owning-memory) Trigger<std::vector<uint8_t>, uint16_t> *on_write_trigger = // NOLINT(cppcoreguidelines-owning-memory)
new Trigger<std::vector<uint8_t>, uint16_t>(); new Trigger<std::vector<uint8_t>, uint16_t>();
characteristic->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>, uint16_t>::on( characteristic->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>, uint16_t>::on(
@ -29,13 +30,15 @@ Trigger<std::vector<uint8_t>, uint16_t> *BLETriggers::create_descriptor_on_write
Trigger<uint16_t> *BLETriggers::create_server_on_connect_trigger(BLEServer *server) { Trigger<uint16_t> *BLETriggers::create_server_on_connect_trigger(BLEServer *server) {
Trigger<uint16_t> *on_connect_trigger = new Trigger<uint16_t>(); // NOLINT(cppcoreguidelines-owning-memory) Trigger<uint16_t> *on_connect_trigger = new Trigger<uint16_t>(); // NOLINT(cppcoreguidelines-owning-memory)
server->on(BLEServerEvt::EmptyEvt::ON_CONNECT, [on_connect_trigger](uint16_t conn_id) { on_connect_trigger->trigger(conn_id); }); server->on(BLEServerEvt::EmptyEvt::ON_CONNECT,
[on_connect_trigger](uint16_t conn_id) { on_connect_trigger->trigger(conn_id); });
return on_connect_trigger; return on_connect_trigger;
} }
Trigger<uint16_t> *BLETriggers::create_server_on_disconnect_trigger(BLEServer *server) { Trigger<uint16_t> *BLETriggers::create_server_on_disconnect_trigger(BLEServer *server) {
Trigger<uint16_t> *on_disconnect_trigger = new Trigger<uint16_t>(); // NOLINT(cppcoreguidelines-owning-memory) Trigger<uint16_t> *on_disconnect_trigger = new Trigger<uint16_t>(); // NOLINT(cppcoreguidelines-owning-memory)
server->on(BLEServerEvt::EmptyEvt::ON_DISCONNECT, [on_disconnect_trigger](uint16_t conn_id) { on_disconnect_trigger->trigger(conn_id); }); server->on(BLEServerEvt::EmptyEvt::ON_DISCONNECT,
[on_disconnect_trigger](uint16_t conn_id) { on_disconnect_trigger->trigger(conn_id); });
return on_disconnect_trigger; return on_disconnect_trigger;
} }

View file

@ -23,7 +23,8 @@ using namespace esp32_ble;
class BLETriggers { class BLETriggers {
public: public:
static Trigger<std::vector<uint8_t>, uint16_t> *create_characteristic_on_write_trigger(BLECharacteristic *characteristic); static Trigger<std::vector<uint8_t>, uint16_t> *create_characteristic_on_write_trigger(
BLECharacteristic *characteristic);
static Trigger<std::vector<uint8_t>, uint16_t> *create_descriptor_on_write_trigger(BLEDescriptor *descriptor); static Trigger<std::vector<uint8_t>, uint16_t> *create_descriptor_on_write_trigger(BLEDescriptor *descriptor);
static Trigger<uint16_t> *create_server_on_connect_trigger(BLEServer *server); static Trigger<uint16_t> *create_server_on_connect_trigger(BLEServer *server);
static Trigger<uint16_t> *create_server_on_disconnect_trigger(BLEServer *server); static Trigger<uint16_t> *create_server_on_disconnect_trigger(BLEServer *server);

View file

@ -41,8 +41,8 @@ void ESP32ImprovComponent::setup_characteristics() {
this->error_->add_descriptor(error_descriptor); this->error_->add_descriptor(error_descriptor);
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>, uint16_t>::on(
BLECharacteristicEvt::VectorEvt::ON_WRITE, [this](const std::vector<uint8_t> &data) { BLECharacteristicEvt::VectorEvt::ON_WRITE, [this](const std::vector<uint8_t> &data, uint16_t id) {
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());
} }