Skip to content

Commit

Permalink
Add manufacturer data config to BLE server (esphome#5251)
Browse files Browse the repository at this point in the history
  • Loading branch information
clydebarrow authored Aug 17, 2023
1 parent c11c4da commit 164d05f
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ esphome/components/ens210/* @itn3rd77
esphome/components/esp32/* @esphome/core
esphome/components/esp32_ble/* @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_can/* @Sympatron
esphome/components/esp32_improv/* @jesserockz
Expand Down
37 changes: 24 additions & 13 deletions esphome/components/esp32_ble/ble_advertising.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@ void BLEAdvertising::remove_service_uuid(ESPBTUUID uuid) {
this->advertising_uuids_.end());
}

void BLEAdvertising::set_manufacturer_data(uint8_t *data, uint16_t size) {
this->advertising_data_.p_manufacturer_data = data;
this->advertising_data_.manufacturer_len = size;
void BLEAdvertising::set_manufacturer_data(const std::vector<uint8_t> &data) {
delete[] this->advertising_data_.p_manufacturer_data;
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() {
Expand Down Expand Up @@ -74,16 +80,21 @@ void BLEAdvertising::start() {
return;
}

memcpy(&this->scan_response_data_, &this->advertising_data_, sizeof(esp_ble_adv_data_t));
this->scan_response_data_.set_scan_rsp = true;
this->scan_response_data_.include_name = true;
this->scan_response_data_.include_txpower = true;
this->scan_response_data_.appearance = 0;
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->scan_response_) {
memcpy(&this->scan_response_data_, &this->advertising_data_, sizeof(esp_ble_adv_data_t));
this->scan_response_data_.set_scan_rsp = true;
this->scan_response_data_.include_name = true;
this->scan_response_data_.include_txpower = true;
this->scan_response_data_.min_interval = 0;
this->scan_response_data_.max_interval = 0;
this->scan_response_data_.manufacturer_len = 0;
this->scan_response_data_.appearance = 0;
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) {
Expand Down
2 changes: 1 addition & 1 deletion esphome/components/esp32_ble/ble_advertising.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BLEAdvertising {
void remove_service_uuid(ESPBTUUID uuid);
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(uint8_t *data, uint16_t size);
void set_manufacturer_data(const std::vector<uint8_t> &data);

void start();
void stop();
Expand Down
6 changes: 5 additions & 1 deletion esphome/components/esp32_ble_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
from esphome.components.esp32 import add_idf_sdkconfig_option

AUTO_LOAD = ["esp32_ble"]
CODEOWNERS = ["@jesserockz"]
CODEOWNERS = ["@jesserockz", "@clydebarrow"]
CONFLICTS_WITH = ["esp32_ble_beacon"]
DEPENDENCIES = ["esp32"]

CONF_MANUFACTURER = "manufacturer"
CONF_MANUFACTURER_DATA = "manufacturer_data"

esp32_ble_server_ns = cg.esphome_ns.namespace("esp32_ble_server")
BLEServer = esp32_ble_server_ns.class_(
Expand All @@ -27,6 +28,7 @@
cv.GenerateID(): cv.declare_id(BLEServer),
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_DATA): cv.Schema([cv.hex_uint8_t]),
cv.Optional(CONF_MODEL): cv.string,
}
).extend(cv.COMPONENT_SCHEMA)
Expand All @@ -42,6 +44,8 @@ async def to_code(config):
cg.add(var.set_parent(parent))

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:
cg.add(var.set_model(config[CONF_MODEL]))
cg.add_define("USE_ESP32_BLE_SERVER")
Expand Down
8 changes: 8 additions & 0 deletions esphome/components/esp32_ble_server/ble_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void BLEServer::loop() {
if (this->device_information_service_->is_running()) {
this->state_ = RUNNING;
this->can_proceed_ = true;
this->restart_advertising_();
ESP_LOGD(TAG, "BLE server setup successfully");
} else if (!this->device_information_service_->is_starting()) {
this->device_information_service_->start();
Expand All @@ -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_() {
if (this->model_.has_value()) {
BLECharacteristic *model =
Expand Down
6 changes: 6 additions & 0 deletions esphome/components/esp32_ble_server/ble_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_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(uint16_t uuid, bool advertise = false);
Expand All @@ -63,6 +67,7 @@ class BLEServer : public Component, public GATTsEventHandler, public Parented<ES

protected:
bool create_device_characteristics_();
void restart_advertising_();

void add_client_(uint16_t conn_id, void *client) {
this->clients_.insert(std::pair<uint16_t, void *>(conn_id, client));
Expand All @@ -73,6 +78,7 @@ class BLEServer : public Component, public GATTsEventHandler, public Parented<ES

std::string manufacturer_;
optional<std::string> model_;
std::vector<uint8_t> manufacturer_data_;
esp_gatt_if_t gatts_if_{0};
bool registered_{false};

Expand Down
5 changes: 5 additions & 0 deletions tests/test2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -768,3 +768,8 @@ switch:
characteristic_uuid: 6490FAFE-0734-732C-8705-91B653A081FC
value: !lambda |-
return {0x13, 0x37};
esp32_ble_server:
id: ble
manufacturer_data: [0x72, 0x4, 0x00, 0x23]

0 comments on commit 164d05f

Please sign in to comment.