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