Add manufacturer data config to BLE server (#5251)

This commit is contained in:
Clyde Stubbs 2023-08-18 06:05:25 +10:00 committed by GitHub
parent c11c4dad2f
commit 164d05fdce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 50 additions and 16 deletions

View File

@ -87,7 +87,7 @@ esphome/components/ens210/* @itn3rd77
esphome/components/esp32/* @esphome/core esphome/components/esp32/* @esphome/core
esphome/components/esp32_ble/* @jesserockz esphome/components/esp32_ble/* @jesserockz
esphome/components/esp32_ble_client/* @jesserockz esphome/components/esp32_ble_client/* @jesserockz
esphome/components/esp32_ble_server/* @jesserockz esphome/components/esp32_ble_server/* @clydebarrow @jesserockz
esphome/components/esp32_camera_web_server/* @ayufan esphome/components/esp32_camera_web_server/* @ayufan
esphome/components/esp32_can/* @Sympatron esphome/components/esp32_can/* @Sympatron
esphome/components/esp32_improv/* @jesserockz esphome/components/esp32_improv/* @jesserockz

View File

@ -42,9 +42,15 @@ void BLEAdvertising::remove_service_uuid(ESPBTUUID uuid) {
this->advertising_uuids_.end()); this->advertising_uuids_.end());
} }
void BLEAdvertising::set_manufacturer_data(uint8_t *data, uint16_t size) { void BLEAdvertising::set_manufacturer_data(const std::vector<uint8_t> &data) {
this->advertising_data_.p_manufacturer_data = data; delete[] this->advertising_data_.p_manufacturer_data;
this->advertising_data_.manufacturer_len = size; this->advertising_data_.p_manufacturer_data = nullptr;
this->advertising_data_.manufacturer_len = data.size();
if (!data.empty()) {
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
this->advertising_data_.p_manufacturer_data = new uint8_t[data.size()];
memcpy(this->advertising_data_.p_manufacturer_data, data.data(), data.size());
}
} }
void BLEAdvertising::start() { void BLEAdvertising::start() {
@ -74,16 +80,21 @@ void BLEAdvertising::start() {
return; return;
} }
memcpy(&this->scan_response_data_, &this->advertising_data_, sizeof(esp_ble_adv_data_t)); if (this->scan_response_) {
this->scan_response_data_.set_scan_rsp = true; memcpy(&this->scan_response_data_, &this->advertising_data_, sizeof(esp_ble_adv_data_t));
this->scan_response_data_.include_name = true; this->scan_response_data_.set_scan_rsp = true;
this->scan_response_data_.include_txpower = true; this->scan_response_data_.include_name = true;
this->scan_response_data_.appearance = 0; this->scan_response_data_.include_txpower = true;
this->scan_response_data_.flag = 0; this->scan_response_data_.min_interval = 0;
err = esp_ble_gap_config_adv_data(&this->scan_response_data_); this->scan_response_data_.max_interval = 0;
if (err != ESP_OK) { this->scan_response_data_.manufacturer_len = 0;
ESP_LOGE(TAG, "esp_ble_gap_config_adv_data failed (Scan response): %d", err); this->scan_response_data_.appearance = 0;
return; this->scan_response_data_.flag = 0;
err = esp_ble_gap_config_adv_data(&this->scan_response_data_);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_ble_gap_config_adv_data failed (Scan response): %d", err);
return;
}
} }
if (this->advertising_data_.service_uuid_len > 0) { if (this->advertising_data_.service_uuid_len > 0) {

View File

@ -20,7 +20,7 @@ class BLEAdvertising {
void remove_service_uuid(ESPBTUUID uuid); void remove_service_uuid(ESPBTUUID uuid);
void set_scan_response(bool scan_response) { this->scan_response_ = scan_response; } 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_min_preferred_interval(uint16_t interval) { this->advertising_data_.min_interval = interval; }
void set_manufacturer_data(uint8_t *data, uint16_t size); void set_manufacturer_data(const std::vector<uint8_t> &data);
void start(); void start();
void stop(); void stop();

View File

@ -6,11 +6,12 @@ from esphome.core import CORE
from esphome.components.esp32 import add_idf_sdkconfig_option from esphome.components.esp32 import add_idf_sdkconfig_option
AUTO_LOAD = ["esp32_ble"] AUTO_LOAD = ["esp32_ble"]
CODEOWNERS = ["@jesserockz"] CODEOWNERS = ["@jesserockz", "@clydebarrow"]
CONFLICTS_WITH = ["esp32_ble_beacon"] CONFLICTS_WITH = ["esp32_ble_beacon"]
DEPENDENCIES = ["esp32"] DEPENDENCIES = ["esp32"]
CONF_MANUFACTURER = "manufacturer" CONF_MANUFACTURER = "manufacturer"
CONF_MANUFACTURER_DATA = "manufacturer_data"
esp32_ble_server_ns = cg.esphome_ns.namespace("esp32_ble_server") esp32_ble_server_ns = cg.esphome_ns.namespace("esp32_ble_server")
BLEServer = esp32_ble_server_ns.class_( BLEServer = esp32_ble_server_ns.class_(
@ -27,6 +28,7 @@ CONFIG_SCHEMA = cv.Schema(
cv.GenerateID(): cv.declare_id(BLEServer), cv.GenerateID(): cv.declare_id(BLEServer),
cv.GenerateID(esp32_ble.CONF_BLE_ID): cv.use_id(esp32_ble.ESP32BLE), cv.GenerateID(esp32_ble.CONF_BLE_ID): cv.use_id(esp32_ble.ESP32BLE),
cv.Optional(CONF_MANUFACTURER, default="ESPHome"): cv.string, cv.Optional(CONF_MANUFACTURER, default="ESPHome"): cv.string,
cv.Optional(CONF_MANUFACTURER_DATA): cv.Schema([cv.hex_uint8_t]),
cv.Optional(CONF_MODEL): cv.string, cv.Optional(CONF_MODEL): cv.string,
} }
).extend(cv.COMPONENT_SCHEMA) ).extend(cv.COMPONENT_SCHEMA)
@ -42,6 +44,8 @@ async def to_code(config):
cg.add(var.set_parent(parent)) cg.add(var.set_parent(parent))
cg.add(var.set_manufacturer(config[CONF_MANUFACTURER])) cg.add(var.set_manufacturer(config[CONF_MANUFACTURER]))
if CONF_MANUFACTURER_DATA in config:
cg.add(var.set_manufacturer_data(config[CONF_MANUFACTURER_DATA]))
if CONF_MODEL in config: if CONF_MODEL in config:
cg.add(var.set_model(config[CONF_MODEL])) cg.add(var.set_model(config[CONF_MODEL]))
cg.add_define("USE_ESP32_BLE_SERVER") cg.add_define("USE_ESP32_BLE_SERVER")

View File

@ -68,6 +68,7 @@ void BLEServer::loop() {
if (this->device_information_service_->is_running()) { if (this->device_information_service_->is_running()) {
this->state_ = RUNNING; this->state_ = RUNNING;
this->can_proceed_ = true; this->can_proceed_ = true;
this->restart_advertising_();
ESP_LOGD(TAG, "BLE server setup successfully"); ESP_LOGD(TAG, "BLE server setup successfully");
} else if (!this->device_information_service_->is_starting()) { } else if (!this->device_information_service_->is_starting()) {
this->device_information_service_->start(); this->device_information_service_->start();
@ -77,6 +78,13 @@ void BLEServer::loop() {
} }
} }
void BLEServer::restart_advertising_() {
if (this->state_ == RUNNING) {
esp32_ble::global_ble->get_advertising()->set_manufacturer_data(this->manufacturer_data_);
esp32_ble::global_ble->get_advertising()->start();
}
}
bool BLEServer::create_device_characteristics_() { bool BLEServer::create_device_characteristics_() {
if (this->model_.has_value()) { if (this->model_.has_value()) {
BLECharacteristic *model = BLECharacteristic *model =

View File

@ -45,6 +45,10 @@ class BLEServer : public Component, public GATTsEventHandler, public Parented<ES
void set_manufacturer(const std::string &manufacturer) { this->manufacturer_ = manufacturer; } void set_manufacturer(const std::string &manufacturer) { this->manufacturer_ = manufacturer; }
void set_model(const std::string &model) { this->model_ = model; } void set_model(const std::string &model) { this->model_ = model; }
void set_manufacturer_data(const std::vector<uint8_t> &data) {
this->manufacturer_data_ = data;
this->restart_advertising_();
}
std::shared_ptr<BLEService> create_service(const uint8_t *uuid, bool advertise = false); std::shared_ptr<BLEService> create_service(const uint8_t *uuid, bool advertise = false);
std::shared_ptr<BLEService> create_service(uint16_t uuid, bool advertise = false); std::shared_ptr<BLEService> create_service(uint16_t uuid, bool advertise = false);
@ -63,6 +67,7 @@ class BLEServer : public Component, public GATTsEventHandler, public Parented<ES
protected: protected:
bool create_device_characteristics_(); bool create_device_characteristics_();
void restart_advertising_();
void add_client_(uint16_t conn_id, void *client) { void add_client_(uint16_t conn_id, void *client) {
this->clients_.insert(std::pair<uint16_t, void *>(conn_id, client)); this->clients_.insert(std::pair<uint16_t, void *>(conn_id, client));
@ -73,6 +78,7 @@ class BLEServer : public Component, public GATTsEventHandler, public Parented<ES
std::string manufacturer_; std::string manufacturer_;
optional<std::string> model_; optional<std::string> model_;
std::vector<uint8_t> manufacturer_data_;
esp_gatt_if_t gatts_if_{0}; esp_gatt_if_t gatts_if_{0};
bool registered_{false}; bool registered_{false};

View File

@ -768,3 +768,8 @@ switch:
characteristic_uuid: 6490FAFE-0734-732C-8705-91B653A081FC characteristic_uuid: 6490FAFE-0734-732C-8705-91B653A081FC
value: !lambda |- value: !lambda |-
return {0x13, 0x37}; return {0x13, 0x37};
esp32_ble_server:
id: ble
manufacturer_data: [0x72, 0x4, 0x00, 0x23]