mirror of
https://github.com/esphome/esphome.git
synced 2025-10-13 07:13:47 +01:00
[esp32_ble_server] Optimize manufacturer_data storage to reduce memory overhead
This commit is contained in:
@@ -68,6 +68,10 @@ void ESP32BLE::advertising_set_service_data(const std::vector<uint8_t> &data) {
|
||||
}
|
||||
|
||||
void ESP32BLE::advertising_set_manufacturer_data(const std::vector<uint8_t> &data) {
|
||||
this->advertising_set_manufacturer_data(std::span<const uint8_t>(data));
|
||||
}
|
||||
|
||||
void ESP32BLE::advertising_set_manufacturer_data(std::span<const uint8_t> data) {
|
||||
this->advertising_init_();
|
||||
this->advertising_->set_manufacturer_data(data);
|
||||
this->advertising_start();
|
||||
|
@@ -118,6 +118,7 @@ class ESP32BLE : public Component {
|
||||
void advertising_start();
|
||||
void advertising_set_service_data(const std::vector<uint8_t> &data);
|
||||
void advertising_set_manufacturer_data(const std::vector<uint8_t> &data);
|
||||
void advertising_set_manufacturer_data(std::span<const uint8_t> data);
|
||||
void advertising_set_appearance(uint16_t appearance) { this->appearance_ = appearance; }
|
||||
void advertising_set_service_data_and_name(std::span<const uint8_t> data, bool include_name);
|
||||
void advertising_add_service_uuid(ESPBTUUID uuid);
|
||||
|
@@ -59,6 +59,10 @@ void BLEAdvertising::set_service_data(const std::vector<uint8_t> &data) {
|
||||
}
|
||||
|
||||
void BLEAdvertising::set_manufacturer_data(const std::vector<uint8_t> &data) {
|
||||
this->set_manufacturer_data(std::span<const uint8_t>(data));
|
||||
}
|
||||
|
||||
void BLEAdvertising::set_manufacturer_data(std::span<const uint8_t> data) {
|
||||
delete[] this->advertising_data_.p_manufacturer_data;
|
||||
this->advertising_data_.p_manufacturer_data = nullptr;
|
||||
this->advertising_data_.manufacturer_len = data.size();
|
||||
|
@@ -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<uint8_t> &data);
|
||||
void set_manufacturer_data(std::span<const uint8_t> data);
|
||||
void set_appearance(uint16_t appearance) { this->advertising_data_.appearance = appearance; }
|
||||
void set_service_data(const std::vector<uint8_t> &data);
|
||||
void set_service_data(std::span<const uint8_t> data);
|
||||
|
@@ -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<const uint8_t>(this->manufacturer_data_.get(), this->manufacturer_data_length_));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -35,7 +35,11 @@ class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEv
|
||||
bool is_running();
|
||||
|
||||
void set_manufacturer_data(const std::vector<uint8_t> &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<CallbackEntry> callbacks_;
|
||||
|
||||
std::vector<uint8_t> 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<ServiceEntry> services_{};
|
||||
std::vector<BLEService *> services_to_start_{};
|
||||
std::unique_ptr<uint8_t[]> 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)
|
||||
|
Reference in New Issue
Block a user