diff --git a/esphome/components/esp32_ble_server/__init__.py b/esphome/components/esp32_ble_server/__init__.py index f1a1847863..cf21bfe887 100644 --- a/esphome/components/esp32_ble_server/__init__.py +++ b/esphome/components/esp32_ble_server/__init__.py @@ -7,16 +7,16 @@ import esphome.config_validation as cv from esphome.const import ( CONF_ID, CONF_MODEL, - CONF_UUID, - CONF_SERVICES, - CONF_VALUE, CONF_NOTIFY, CONF_ON_CONNECT, CONF_ON_DISCONNECT, + CONF_SERVICES, + CONF_UUID, + CONF_VALUE, ) from esphome.core import CORE -AUTO_LOAD = ["esp32_ble"] +AUTO_LOAD = ["esp32_ble", "bytebuffer"] CODEOWNERS = ["@jesserockz", "@clydebarrow", "@Rapsssito"] DEPENDENCIES = ["esp32"] @@ -67,8 +67,9 @@ BLECharacteristicSetValueAction = esp32_ble_server_automations_ns.class_( BLECharacteristicNotifyAction = esp32_ble_server_automations_ns.class_( "BLECharacteristicNotifyAction", automation.Action ) -ByteBuffer_ns = cg.esphome_ns.namespace("ByteBuffer") -ByteBuffer = cg.esphome_ns.class_("ByteBuffer") +bytebuffer_ns = cg.esphome_ns.namespace("bytebuffer") +ByteBuffer_ns = bytebuffer_ns.namespace("ByteBuffer") +ByteBuffer = bytebuffer_ns.class_("ByteBuffer") PROPERTY_MAP = { @@ -331,8 +332,7 @@ async def parse_value(value, str_encoding, buffer_id, args, byte_length=None): return await cg.templatable( value, args, - ByteBuffer, - ByteBuffer_ns.wrap, + cg.std_vector.template(cg.uint8), ) val, val_method = bytebuffer_parser_(value, str_encoding) @@ -356,11 +356,11 @@ async def parse_value(value, str_encoding, buffer_id, args, byte_length=None): def calculate_num_handles(service_config): - total = 1 - for char_conf in service_config[CONF_CHARACTERISTICS]: - total += 2 # One for the char_conf itself and one for the value - for _ in char_conf[CONF_DESCRIPTORS]: - total += 1 + total = 1 + len(service_config[CONF_CHARACTERISTICS]) * 2 + total += sum( + len(char_conf[CONF_DESCRIPTORS]) + for char_conf in service_config[CONF_CHARACTERISTICS] + ) return total @@ -416,7 +416,7 @@ async def to_code_characteristic(service_var, char_conf): value_action = await ble_server_characteristic_set_value( action_conf, char_conf[CONF_CHAR_VALUE_ACTION_ID_], - cg.TemplateArguments(None), + cg.TemplateArguments(), {}, ) cg.add(value_action.play()) diff --git a/esphome/components/esp32_ble_server/ble_characteristic.cpp b/esphome/components/esp32_ble_server/ble_characteristic.cpp index bd6997cbed..15739d60bb 100644 --- a/esphome/components/esp32_ble_server/ble_characteristic.cpp +++ b/esphome/components/esp32_ble_server/ble_characteristic.cpp @@ -32,11 +32,16 @@ BLECharacteristic::BLECharacteristic(const ESPBTUUID uuid, uint32_t properties) this->set_write_no_response_property((properties & PROPERTY_WRITE_NR) != 0); } -void BLECharacteristic::set_value(ByteBuffer buffer) { +void BLECharacteristic::set_value(ByteBuffer buffer) { this->set_value(buffer.get_data()); } + +void BLECharacteristic::set_value(const std::vector &buffer) { xSemaphoreTake(this->set_value_lock_, 0L); - this->value_ = buffer.get_data(); + this->value_ = buffer; xSemaphoreGive(this->set_value_lock_); } +void BLECharacteristic::set_value(const std::string &buffer) { + this->set_value(std::vector(buffer.begin(), buffer.end())); +} void BLECharacteristic::notify() { if (this->service_ == nullptr || this->service_->get_server() == nullptr || diff --git a/esphome/components/esp32_ble_server/ble_characteristic.h b/esphome/components/esp32_ble_server/ble_characteristic.h index 486b755284..f7ae144ab3 100644 --- a/esphome/components/esp32_ble_server/ble_characteristic.h +++ b/esphome/components/esp32_ble_server/ble_characteristic.h @@ -3,7 +3,7 @@ #include "ble_descriptor.h" #include "esphome/components/esp32_ble/ble_uuid.h" #include "esphome/core/event_emitter.h" -#include "esphome/core/bytebuffer.h" +#include "esphome/components/bytebuffer/bytebuffer.h" #include #include @@ -22,6 +22,7 @@ namespace esphome { namespace esp32_ble_server { using namespace esp32_ble; +using namespace bytebuffer; class BLEService; @@ -42,6 +43,8 @@ class BLECharacteristic : public EventEmitter &buffer); + void set_value(const std::string &buffer); void set_broadcast_property(bool value); void set_indicate_property(bool value); diff --git a/esphome/components/esp32_ble_server/ble_descriptor.h b/esphome/components/esp32_ble_server/ble_descriptor.h index 57d4c449e0..b1edc6fe85 100644 --- a/esphome/components/esp32_ble_server/ble_descriptor.h +++ b/esphome/components/esp32_ble_server/ble_descriptor.h @@ -2,7 +2,7 @@ #include "esphome/components/esp32_ble/ble_uuid.h" #include "esphome/core/event_emitter.h" -#include "esphome/core/bytebuffer.h" +#include "esphome/components/bytebuffer/bytebuffer.h" #ifdef USE_ESP32 @@ -13,6 +13,7 @@ namespace esphome { namespace esp32_ble_server { using namespace esp32_ble; +using namespace bytebuffer; class BLECharacteristic; diff --git a/esphome/components/esp32_ble_server/ble_server.cpp b/esphome/components/esp32_ble_server/ble_server.cpp index 4ce4a0394a..fbfe5fce00 100644 --- a/esphome/components/esp32_ble_server/ble_server.cpp +++ b/esphome/components/esp32_ble_server/ble_server.cpp @@ -4,7 +4,6 @@ #include "esphome/core/log.h" #include "esphome/core/application.h" #include "esphome/core/version.h" -#include "esphome/core/bytebuffer.h" #ifdef USE_ESP32 @@ -112,21 +111,20 @@ bool BLEServer::create_device_characteristics_() { if (this->model_.has_value()) { BLECharacteristic *model = this->device_information_service_->create_characteristic(MODEL_UUID, BLECharacteristic::PROPERTY_READ); - model->set_value(ByteBuffer::wrap(std::vector(this->model_.value().begin(), this->model_.value().end()))); + model->set_value(this->model_.value()); } else { BLECharacteristic *model = this->device_information_service_->create_characteristic(MODEL_UUID, BLECharacteristic::PROPERTY_READ); - model->set_value(ByteBuffer::wrap(ESPHOME_BOARD)); + model->set_value(std::string(ESPHOME_BOARD)); } BLECharacteristic *version = this->device_information_service_->create_characteristic(VERSION_UUID, BLECharacteristic::PROPERTY_READ); - version->set_value(ByteBuffer::wrap("ESPHome " ESPHOME_VERSION)); + version->set_value(std::string("ESPHome " ESPHOME_VERSION)); BLECharacteristic *manufacturer = this->device_information_service_->create_characteristic(MANUFACTURER_UUID, BLECharacteristic::PROPERTY_READ); - manufacturer->set_value( - ByteBuffer::wrap(std::vector(this->manufacturer_.begin(), this->manufacturer_.end()))); + manufacturer->set_value(this->manufacturer_); return true; } diff --git a/esphome/components/esp32_ble_server/ble_server.h b/esphome/components/esp32_ble_server/ble_server.h index 7a35937a56..6388d24bea 100644 --- a/esphome/components/esp32_ble_server/ble_server.h +++ b/esphome/components/esp32_ble_server/ble_server.h @@ -4,13 +4,10 @@ #include "ble_characteristic.h" #include "esphome/components/esp32_ble/ble.h" -#include "esphome/components/esp32_ble/ble_advertising.h" #include "esphome/components/esp32_ble/ble_uuid.h" -#include "esphome/components/esp32_ble/queue.h" -#include "esphome/core/automation.h" +#include "esphome/components/bytebuffer/bytebuffer.h" #include "esphome/core/component.h" #include "esphome/core/helpers.h" -#include "esphome/core/preferences.h" #include #include @@ -19,13 +16,13 @@ #ifdef USE_ESP32 -#include #include namespace esphome { namespace esp32_ble_server { using namespace esp32_ble; +using namespace bytebuffer; namespace BLEServerEvt { enum EmptyEvt { @@ -78,16 +75,16 @@ class BLEServer : public Component, void add_client_(uint16_t conn_id) { this->clients_.insert(conn_id); } void remove_client_(uint16_t conn_id) { this->clients_.erase(conn_id); } - std::string manufacturer_; - optional model_; - std::vector manufacturer_data_; + std::string manufacturer_{}; + optional model_{}; + std::vector manufacturer_data_{}; esp_gatt_if_t gatts_if_{0}; bool registered_{false}; std::unordered_set clients_; - std::unordered_map services_; - std::vector services_to_start_; - BLEService *device_information_service_; + std::unordered_map services_{}; + std::vector services_to_start_{}; + BLEService *device_information_service_{}; enum State : uint8_t { INIT = 0x00, diff --git a/esphome/components/esp32_ble_server/ble_server_automations.h b/esphome/components/esp32_ble_server/ble_server_automations.h index da31d61d18..63dea79820 100644 --- a/esphome/components/esp32_ble_server/ble_server_automations.h +++ b/esphome/components/esp32_ble_server/ble_server_automations.h @@ -6,7 +6,6 @@ #include "esphome/core/event_emitter.h" #include "esphome/core/automation.h" -#include "esphome/core/bytebuffer.h" #include #include @@ -59,7 +58,8 @@ class BLECharacteristicSetValueActionManager template class BLECharacteristicSetValueAction : public Action { public: BLECharacteristicSetValueAction(BLECharacteristic *characteristic) : parent_(characteristic) {} - TEMPLATABLE_VALUE(ByteBuffer, buffer) + TEMPLATABLE_VALUE(std::vector, buffer) + void set_buffer(ByteBuffer buffer) { this->set_buffer(buffer.get_data()); } void play(Ts... x) override { // If the listener is already set, do nothing if (BLECharacteristicSetValueActionManager::get_instance()->get_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 41ea25a63d..36945478a9 100644 --- a/esphome/components/esp32_improv/esp32_improv_component.cpp +++ b/esphome/components/esp32_improv/esp32_improv_component.cpp @@ -4,13 +4,15 @@ #include "esphome/components/esp32_ble_server/ble_2902.h" #include "esphome/core/application.h" #include "esphome/core/log.h" -#include "esphome/core/bytebuffer.h" +#include "esphome/components/bytebuffer/bytebuffer.h" #ifdef USE_ESP32 namespace esphome { namespace esp32_improv { +using namespace bytebuffer; + static const char *const TAG = "esp32_improv.component"; static const char *const ESPHOME_MY_LINK = "https://my.home-assistant.io/redirect/config_flow_start?domain=esphome"; diff --git a/tests/components/esp32_ble_server/common.yaml b/tests/components/esp32_ble_server/common.yaml index 502d1fce4f..4c2387a512 100644 --- a/tests/components/esp32_ble_server/common.yaml +++ b/tests/components/esp32_ble_server/common.yaml @@ -45,6 +45,9 @@ esp32_ble_server: ESP_LOGD("BLE", "Characteristic received: %s from %d", std::string(x.begin(), x.end()).c_str(), id); - ble_server.characteristic.set_value: id: test_change_characteristic - value: !lambda 'return ByteBuffer::wrap({0x00, 0x01, 0x02});' + value: !lambda 'return bytebuffer::ByteBuffer::wrap({0x00, 0x01, 0x02}).get_data();' + - ble_server.characteristic.set_value: + id: test_change_characteristic + value: [1, 2, 3, 4] - ble_server.characteristic.notify: id: test_notify_characteristic