ESPHome  2024.11.0
ble_advertising.cpp
Go to the documentation of this file.
1 #include "ble_advertising.h"
2 
3 #ifdef USE_ESP32
4 
5 #include <cstdio>
6 #include <cstring>
7 #include "ble_uuid.h"
8 #include "esphome/core/log.h"
9 
10 namespace esphome {
11 namespace esp32_ble {
12 
13 static const char *const TAG = "esp32_ble.advertising";
14 
15 BLEAdvertising::BLEAdvertising(uint32_t advertising_cycle_time) : advertising_cycle_time_(advertising_cycle_time) {
16  this->advertising_data_.set_scan_rsp = false;
17  this->advertising_data_.include_name = true;
18  this->advertising_data_.include_txpower = true;
19  this->advertising_data_.min_interval = 0;
20  this->advertising_data_.max_interval = 0;
21  this->advertising_data_.appearance = 0x00;
22  this->advertising_data_.manufacturer_len = 0;
23  this->advertising_data_.p_manufacturer_data = nullptr;
24  this->advertising_data_.service_data_len = 0;
25  this->advertising_data_.p_service_data = nullptr;
26  this->advertising_data_.service_uuid_len = 0;
27  this->advertising_data_.p_service_uuid = nullptr;
28  this->advertising_data_.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT);
29 
30  this->advertising_params_.adv_int_min = 0x20;
31  this->advertising_params_.adv_int_max = 0x40;
32  this->advertising_params_.adv_type = ADV_TYPE_IND;
33  this->advertising_params_.own_addr_type = BLE_ADDR_TYPE_PUBLIC;
34  this->advertising_params_.channel_map = ADV_CHNL_ALL;
35  this->advertising_params_.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
36  this->advertising_params_.peer_addr_type = BLE_ADDR_TYPE_PUBLIC;
37 }
38 
39 void BLEAdvertising::add_service_uuid(ESPBTUUID uuid) { this->advertising_uuids_.push_back(uuid); }
41  this->advertising_uuids_.erase(std::remove(this->advertising_uuids_.begin(), this->advertising_uuids_.end(), uuid),
42  this->advertising_uuids_.end());
43 }
44 
45 void BLEAdvertising::set_service_data(const std::vector<uint8_t> &data) {
46  delete[] this->advertising_data_.p_service_data;
47  this->advertising_data_.p_service_data = nullptr;
48  this->advertising_data_.service_data_len = data.size();
49  if (!data.empty()) {
50  // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
51  this->advertising_data_.p_service_data = new uint8_t[data.size()];
52  memcpy(this->advertising_data_.p_service_data, data.data(), data.size());
53  }
54 }
55 
56 void BLEAdvertising::set_manufacturer_data(const std::vector<uint8_t> &data) {
57  delete[] this->advertising_data_.p_manufacturer_data;
58  this->advertising_data_.p_manufacturer_data = nullptr;
59  this->advertising_data_.manufacturer_len = data.size();
60  if (!data.empty()) {
61  // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
62  this->advertising_data_.p_manufacturer_data = new uint8_t[data.size()];
63  memcpy(this->advertising_data_.p_manufacturer_data, data.data(), data.size());
64  }
65 }
66 
68  int num_services = this->advertising_uuids_.size();
69  if (num_services == 0) {
70  this->advertising_data_.service_uuid_len = 0;
71  } else {
72  this->advertising_data_.service_uuid_len = 16 * num_services;
73  // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
74  this->advertising_data_.p_service_uuid = new uint8_t[this->advertising_data_.service_uuid_len];
75  uint8_t *p = this->advertising_data_.p_service_uuid;
76  for (int i = 0; i < num_services; i++) {
77  ESPBTUUID uuid = this->advertising_uuids_[i];
78  memcpy(p, uuid.as_128bit().get_uuid().uuid.uuid128, 16);
79  p += 16;
80  }
81  }
82 
83  esp_err_t err;
84 
85  this->advertising_data_.set_scan_rsp = false;
86  this->advertising_data_.include_name = !this->scan_response_;
87  this->advertising_data_.include_txpower = !this->scan_response_;
88  err = esp_ble_gap_config_adv_data(&this->advertising_data_);
89  if (err != ESP_OK) {
90  ESP_LOGE(TAG, "esp_ble_gap_config_adv_data failed (Advertising): %s", esp_err_to_name(err));
91  return err;
92  }
93 
94  if (this->scan_response_) {
95  memcpy(&this->scan_response_data_, &this->advertising_data_, sizeof(esp_ble_adv_data_t));
96  this->scan_response_data_.set_scan_rsp = true;
97  this->scan_response_data_.include_name = true;
98  this->scan_response_data_.include_txpower = true;
99  this->scan_response_data_.manufacturer_len = 0;
100  this->scan_response_data_.appearance = 0;
101  this->scan_response_data_.flag = 0;
102  err = esp_ble_gap_config_adv_data(&this->scan_response_data_);
103  if (err != ESP_OK) {
104  ESP_LOGE(TAG, "esp_ble_gap_config_adv_data failed (Scan response): %s", esp_err_to_name(err));
105  return err;
106  }
107  }
108 
109  if (this->advertising_data_.service_uuid_len > 0) {
110  delete[] this->advertising_data_.p_service_uuid;
111  this->advertising_data_.p_service_uuid = nullptr;
112  }
113 
114  err = esp_ble_gap_start_advertising(&this->advertising_params_);
115  if (err != ESP_OK) {
116  ESP_LOGE(TAG, "esp_ble_gap_start_advertising failed: %s", esp_err_to_name(err));
117  return err;
118  }
119 
120  return ESP_OK;
121 }
122 
124  if (this->current_adv_index_ == -1) {
125  this->services_advertisement_();
126  } else {
128  }
129 }
130 
132  esp_err_t err = esp_ble_gap_stop_advertising();
133  if (err != ESP_OK) {
134  ESP_LOGE(TAG, "esp_ble_gap_stop_advertising failed: %d", err);
135  return;
136  }
137  if (this->current_adv_index_ != -1) {
139  }
140 }
141 
143  if (this->raw_advertisements_callbacks_.empty()) {
144  return;
145  }
146  const uint32_t now = millis();
147  if (now - this->last_advertisement_time_ > this->advertising_cycle_time_) {
148  this->stop();
149  this->current_adv_index_ += 1;
150  if (this->current_adv_index_ >= this->raw_advertisements_callbacks_.size()) {
151  this->current_adv_index_ = -1;
152  }
153  this->start();
154  this->last_advertisement_time_ = now;
155  }
156 }
157 
158 void BLEAdvertising::register_raw_advertisement_callback(std::function<void(bool)> &&callback) {
159  this->raw_advertisements_callbacks_.push_back(std::move(callback));
160 }
161 
162 } // namespace esp32_ble
163 } // namespace esphome
164 
165 #endif
BLEAdvertising(uint32_t advertising_cycle_time)
void set_service_data(const std::vector< uint8_t > &data)
std::vector< std::function< void(bool)> > raw_advertisements_callbacks_
esp_ble_adv_params_t advertising_params_
void set_manufacturer_data(const std::vector< uint8_t > &data)
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
void register_raw_advertisement_callback(std::function< void(bool)> &&callback)
void remove_service_uuid(ESPBTUUID uuid)
esp_ble_adv_data_t scan_response_data_
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< ESPBTUUID > advertising_uuids_
ESPBTUUID as_128bit() const
Definition: ble_uuid.cpp:110
esp_bt_uuid_t get_uuid() const
Definition: ble_uuid.cpp:171