diff --git a/esphome/components/esp32_ble/ble.cpp b/esphome/components/esp32_ble/ble.cpp index 64cef70de2..cec92a2285 100644 --- a/esphome/components/esp32_ble/ble.cpp +++ b/esphome/components/esp32_ble/ble.cpp @@ -68,6 +68,10 @@ void ESP32BLE::advertising_set_service_data(const std::vector &data) { } void ESP32BLE::advertising_set_manufacturer_data(const std::vector &data) { + this->advertising_set_manufacturer_data(std::span(data)); +} + +void ESP32BLE::advertising_set_manufacturer_data(std::span data) { this->advertising_init_(); this->advertising_->set_manufacturer_data(data); this->advertising_start(); diff --git a/esphome/components/esp32_ble/ble.h b/esphome/components/esp32_ble/ble.h index 1aa3bc86ef..b49e5d12ee 100644 --- a/esphome/components/esp32_ble/ble.h +++ b/esphome/components/esp32_ble/ble.h @@ -118,6 +118,7 @@ class ESP32BLE : public Component { void advertising_start(); void advertising_set_service_data(const std::vector &data); void advertising_set_manufacturer_data(const std::vector &data); + void advertising_set_manufacturer_data(std::span data); void advertising_set_appearance(uint16_t appearance) { this->appearance_ = appearance; } void advertising_set_service_data_and_name(std::span data, bool include_name); void advertising_add_service_uuid(ESPBTUUID uuid); diff --git a/esphome/components/esp32_ble/ble_advertising.cpp b/esphome/components/esp32_ble/ble_advertising.cpp index df70768c23..3bc0fabe7e 100644 --- a/esphome/components/esp32_ble/ble_advertising.cpp +++ b/esphome/components/esp32_ble/ble_advertising.cpp @@ -59,6 +59,10 @@ void BLEAdvertising::set_service_data(const std::vector &data) { } void BLEAdvertising::set_manufacturer_data(const std::vector &data) { + this->set_manufacturer_data(std::span(data)); +} + +void BLEAdvertising::set_manufacturer_data(std::span data) { delete[] this->advertising_data_.p_manufacturer_data; this->advertising_data_.p_manufacturer_data = nullptr; this->advertising_data_.manufacturer_len = data.size(); diff --git a/esphome/components/esp32_ble/ble_advertising.h b/esphome/components/esp32_ble/ble_advertising.h index 7a31d926f6..70d58d5ce9 100644 --- a/esphome/components/esp32_ble/ble_advertising.h +++ b/esphome/components/esp32_ble/ble_advertising.h @@ -35,6 +35,7 @@ class BLEAdvertising { void set_scan_response(bool scan_response) { this->scan_response_ = scan_response; } void set_min_preferred_interval(uint16_t interval) { this->advertising_data_.min_interval = interval; } void set_manufacturer_data(const std::vector &data); + void set_manufacturer_data(std::span data); void set_appearance(uint16_t appearance) { this->advertising_data_.appearance = appearance; } void set_service_data(const std::vector &data); void set_service_data(std::span data); diff --git a/esphome/components/esp32_ble_server/ble_server.cpp b/esphome/components/esp32_ble_server/ble_server.cpp index 25cc97eeaf..0e3a3b4a49 100644 --- a/esphome/components/esp32_ble_server/ble_server.cpp +++ b/esphome/components/esp32_ble_server/ble_server.cpp @@ -99,7 +99,8 @@ bool BLEServer::can_proceed() { return this->is_running() || !this->parent_->is_ void BLEServer::restart_advertising_() { if (this->is_running()) { - this->parent_->advertising_set_manufacturer_data(this->manufacturer_data_); + this->parent_->advertising_set_manufacturer_data( + std::span(this->manufacturer_data_.get(), this->manufacturer_data_length_)); } } diff --git a/esphome/components/esp32_ble_server/ble_server.h b/esphome/components/esp32_ble_server/ble_server.h index 6fa86dd67f..788ad377ef 100644 --- a/esphome/components/esp32_ble_server/ble_server.h +++ b/esphome/components/esp32_ble_server/ble_server.h @@ -35,7 +35,11 @@ class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEv bool is_running(); void set_manufacturer_data(const std::vector &data) { - this->manufacturer_data_ = data; + this->manufacturer_data_length_ = data.size(); + this->manufacturer_data_.reset(data.empty() ? nullptr : new uint8_t[data.size()]); + if (!data.empty()) { + memcpy(this->manufacturer_data_.get(), data.data(), data.size()); + } this->restart_advertising_(); } @@ -87,24 +91,27 @@ class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEv void remove_client_(uint16_t conn_id); void dispatch_callbacks_(CallbackType type, uint16_t conn_id); + // 4-byte aligned (pointers and vectors on 32-bit) std::vector callbacks_; - - std::vector manufacturer_data_{}; - esp_gatt_if_t gatts_if_{0}; - bool registered_{false}; - - uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]{}; - uint8_t client_count_{0}; std::vector services_{}; std::vector services_to_start_{}; + std::unique_ptr manufacturer_data_{}; BLEService *device_information_service_{}; + // 2-byte aligned + uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]{}; + + // 1-byte aligned + uint8_t manufacturer_data_length_{0}; + uint8_t client_count_{0}; + esp_gatt_if_t gatts_if_{0}; enum State : uint8_t { INIT = 0x00, REGISTERING, STARTING_SERVICE, RUNNING, } state_{INIT}; + bool registered_{false}; }; // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)