diff --git a/esphome/components/esp32_ble_server/__init__.py b/esphome/components/esp32_ble_server/__init__.py index 20868eb493..f1a1847863 100644 --- a/esphome/components/esp32_ble_server/__init__.py +++ b/esphome/components/esp32_ble_server/__init__.py @@ -35,7 +35,7 @@ CONF_DESCRIPTORS = "descriptors" CONF_STRING_ENCODING = "string_encoding" 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_CUD_ID_ = "cud_id_" 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 for desc in char_config[CONF_DESCRIPTORS]: 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 char_config[CONF_DESCRIPTORS].append( { diff --git a/esphome/components/esp32_ble_server/ble_characteristic.cpp b/esphome/components/esp32_ble_server/ble_characteristic.cpp index 07fd337e75..bd6997cbed 100644 --- a/esphome/components/esp32_ble_server/ble_characteristic.cpp +++ b/esphome/components/esp32_ble_server/ble_characteristic.cpp @@ -55,8 +55,8 @@ void BLECharacteristic::notify() { ESP_LOGW(TAG, "INDICATE acknowledgment is not yet supported (i.e. it works as a NOTIFY)"); require_ack = false; } - esp_err_t err = esp_ble_gatts_send_indicate(this->service_->get_server()->get_gatts_if(), client, - this->handle_, length, this->value_.data(), require_ack); + esp_err_t err = esp_ble_gatts_send_indicate(this->service_->get_server()->get_gatts_if(), client, this->handle_, + length, this->value_.data(), require_ack); if (err != ESP_OK) { ESP_LOGE(TAG, "esp_ble_gatts_send_indicate failed %d", err); return; @@ -67,21 +67,18 @@ void BLECharacteristic::notify() { 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 (descriptor->get_uuid() == ESPBTUUID::from_uint16(ESP_GATT_UUID_CHAR_CLIENT_CONFIG)) { - descriptor->on( - BLEDescriptorEvt::VectorEvt::ON_WRITE, - [this](const std::vector &value, uint16_t conn_id) { - if (value.size() != 2) - return; - uint16_t cccd = encode_uint16(value[1], value[0]); - bool notify = (cccd & 1) != 0; - bool indicate = (cccd & 2) != 0; - if (notify || indicate) { - this->clients_to_notify_[conn_id] = indicate; - } else { - this->clients_to_notify_.erase(conn_id); - } + descriptor->on(BLEDescriptorEvt::VectorEvt::ON_WRITE, [this](const std::vector &value, uint16_t conn_id) { + if (value.size() != 2) + return; + uint16_t cccd = encode_uint16(value[1], value[0]); + bool notify = (cccd & 1) != 0; + bool indicate = (cccd & 2) != 0; + if (notify || indicate) { + this->clients_to_notify_[conn_id] = indicate; + } else { + this->clients_to_notify_.erase(conn_id); } - ); + }); } 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) break; // For some reason you can request a read but not want a response - this->EventEmitter::emit_( - BLECharacteristicEvt::EmptyEvt::ON_READ, param->read.conn_id); + this->EventEmitter::emit_(BLECharacteristicEvt::EmptyEvt::ON_READ, + param->read.conn_id); uint16_t max_offset = 22; diff --git a/esphome/components/esp32_ble_server/ble_descriptor.cpp b/esphome/components/esp32_ble_server/ble_descriptor.cpp index 095ace1755..b8374f5c89 100644 --- a/esphome/components/esp32_ble_server/ble_descriptor.cpp +++ b/esphome/components/esp32_ble_server/ble_descriptor.cpp @@ -72,11 +72,9 @@ void BLEDescriptor::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_ break; this->value_.attr_len = param->write.len; memcpy(this->value_.attr_value, param->write.value, param->write.len); - this->emit_( - BLEDescriptorEvt::VectorEvt::ON_WRITE, - std::vector(param->write.value, param->write.value + param->write.len), - param->write.conn_id - ); + this->emit_(BLEDescriptorEvt::VectorEvt::ON_WRITE, + std::vector(param->write.value, param->write.value + param->write.len), + param->write.conn_id); break; } default: diff --git a/esphome/components/esp32_ble_server/ble_server.h b/esphome/components/esp32_ble_server/ble_server.h index 97c0b88fa8..7a35937a56 100644 --- a/esphome/components/esp32_ble_server/ble_server.h +++ b/esphome/components/esp32_ble_server/ble_server.h @@ -34,8 +34,11 @@ enum EmptyEvt { }; } // namespace BLEServerEvt -class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEventHandler, - public Parented, public EventEmitter { +class BLEServer : public Component, + public GATTsEventHandler, + public BLEStatusEventHandler, + public Parented, + public EventEmitter { public: void setup() override; void loop() override; diff --git a/esphome/components/esp32_ble_server/ble_server_automations.cpp b/esphome/components/esp32_ble_server/ble_server_automations.cpp index d373b8f17f..41ef2b8bfe 100644 --- a/esphome/components/esp32_ble_server/ble_server_automations.cpp +++ b/esphome/components/esp32_ble_server/ble_server_automations.cpp @@ -9,7 +9,8 @@ namespace esp32_ble_server_automations { using namespace esp32_ble; -Trigger, uint16_t> *BLETriggers::create_characteristic_on_write_trigger(BLECharacteristic *characteristic) { +Trigger, uint16_t> *BLETriggers::create_characteristic_on_write_trigger( + BLECharacteristic *characteristic) { Trigger, uint16_t> *on_write_trigger = // NOLINT(cppcoreguidelines-owning-memory) new Trigger, uint16_t>(); characteristic->EventEmitter, uint16_t>::on( @@ -29,13 +30,15 @@ Trigger, uint16_t> *BLETriggers::create_descriptor_on_write Trigger *BLETriggers::create_server_on_connect_trigger(BLEServer *server) { Trigger *on_connect_trigger = new Trigger(); // 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; } Trigger *BLETriggers::create_server_on_disconnect_trigger(BLEServer *server) { Trigger *on_disconnect_trigger = new Trigger(); // 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; } @@ -50,7 +53,7 @@ void BLECharacteristicSetValueActionManager::set_listener(BLECharacteristic *cha EventEmitterListenerID old_pre_notify_listener_id = listener_pairs.second; // Remove the previous listener characteristic->EventEmitter::off(BLECharacteristicEvt::EmptyEvt::ON_READ, - old_listener_id); + old_listener_id); // Remove the pre-notify listener this->off(BLECharacteristicSetValueActionEvt::PRE_NOTIFY, old_pre_notify_listener_id); } diff --git a/esphome/components/esp32_ble_server/ble_server_automations.h b/esphome/components/esp32_ble_server/ble_server_automations.h index f45451cfe9..da31d61d18 100644 --- a/esphome/components/esp32_ble_server/ble_server_automations.h +++ b/esphome/components/esp32_ble_server/ble_server_automations.h @@ -23,7 +23,8 @@ using namespace esp32_ble; class BLETriggers { public: - static Trigger, uint16_t> *create_characteristic_on_write_trigger(BLECharacteristic *characteristic); + static Trigger, uint16_t> *create_characteristic_on_write_trigger( + BLECharacteristic *characteristic); static Trigger, uint16_t> *create_descriptor_on_write_trigger(BLEDescriptor *descriptor); static Trigger *create_server_on_connect_trigger(BLEServer *server); static Trigger *create_server_on_disconnect_trigger(BLEServer *server); diff --git a/esphome/components/esp32_improv/esp32_improv_component.cpp b/esphome/components/esp32_improv/esp32_improv_component.cpp index 6bffd69ca2..7db3c0f9cc 100644 --- a/esphome/components/esp32_improv/esp32_improv_component.cpp +++ b/esphome/components/esp32_improv/esp32_improv_component.cpp @@ -41,8 +41,8 @@ void ESP32ImprovComponent::setup_characteristics() { this->error_->add_descriptor(error_descriptor); 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) { + this->rpc_->EventEmitter, uint16_t>::on( + BLECharacteristicEvt::VectorEvt::ON_WRITE, [this](const std::vector &data, uint16_t id) { if (!data.empty()) { this->incoming_data_.insert(this->incoming_data_.end(), data.begin(), data.end()); }