ESPHome 2025.5.0
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
5#include <cstdio>
6#include <cstring>
7#include "ble_uuid.h"
8#include "esphome/core/log.h"
10
11namespace esphome {
12namespace 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(const std::vector<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_manufacturer_data(const std::vector<uint8_t> &data) {
58 delete[] this->advertising_data_.p_manufacturer_data;
59 this->advertising_data_.p_manufacturer_data = nullptr;
60 this->advertising_data_.manufacturer_len = data.size();
61 if (!data.empty()) {
62 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
63 this->advertising_data_.p_manufacturer_data = new uint8_t[data.size()];
64 memcpy(this->advertising_data_.p_manufacturer_data, data.data(), data.size());
65 }
66}
67
69 int num_services = this->advertising_uuids_.size();
70 if (num_services == 0) {
71 this->advertising_data_.service_uuid_len = 0;
72 } else {
73 this->advertising_data_.service_uuid_len = 16 * num_services;
74 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
75 this->advertising_data_.p_service_uuid = new uint8_t[this->advertising_data_.service_uuid_len];
76 uint8_t *p = this->advertising_data_.p_service_uuid;
77 for (int i = 0; i < num_services; i++) {
78 ESPBTUUID uuid = this->advertising_uuids_[i];
79 memcpy(p, uuid.as_128bit().get_uuid().uuid.uuid128, 16);
80 p += 16;
81 }
82 }
83
84 esp_err_t err;
85
86 this->advertising_data_.set_scan_rsp = false;
87 this->advertising_data_.include_name = !this->scan_response_;
88 this->advertising_data_.include_txpower = !this->scan_response_;
89 err = esp_ble_gap_config_adv_data(&this->advertising_data_);
90 if (err != ESP_OK) {
91 ESP_LOGE(TAG, "esp_ble_gap_config_adv_data failed (Advertising): %s", esp_err_to_name(err));
92 return err;
93 }
94
95 if (this->scan_response_) {
96 memcpy(&this->scan_response_data_, &this->advertising_data_, sizeof(esp_ble_adv_data_t));
97 this->scan_response_data_.set_scan_rsp = true;
98 this->scan_response_data_.include_name = true;
99 this->scan_response_data_.include_txpower = true;
100 this->scan_response_data_.manufacturer_len = 0;
101 this->scan_response_data_.appearance = 0;
102 this->scan_response_data_.flag = 0;
103 err = esp_ble_gap_config_adv_data(&this->scan_response_data_);
104 if (err != ESP_OK) {
105 ESP_LOGE(TAG, "esp_ble_gap_config_adv_data failed (Scan response): %s", esp_err_to_name(err));
106 return err;
107 }
108 }
109
110 if (this->advertising_data_.service_uuid_len > 0) {
111 delete[] this->advertising_data_.p_service_uuid;
112 this->advertising_data_.p_service_uuid = nullptr;
113 }
114
115 err = esp_ble_gap_start_advertising(&this->advertising_params_);
116 if (err != ESP_OK) {
117 ESP_LOGE(TAG, "esp_ble_gap_start_advertising failed: %s", esp_err_to_name(err));
118 return err;
119 }
120
121 return ESP_OK;
122}
123
125 if (this->current_adv_index_ == -1) {
127 } else {
129 }
130}
131
133 esp_err_t err = esp_ble_gap_stop_advertising();
134 if (err != ESP_OK) {
135 ESP_LOGE(TAG, "esp_ble_gap_stop_advertising failed: %d", err);
136 return;
137 }
138 if (this->current_adv_index_ != -1) {
140 }
141}
142
144 if (this->raw_advertisements_callbacks_.empty()) {
145 return;
146 }
147 const uint32_t now = App.get_loop_component_start_time();
148 if (now - this->last_advertisement_time_ > this->advertising_cycle_time_) {
149 this->stop();
150 this->current_adv_index_ += 1;
151 if (this->current_adv_index_ >= this->raw_advertisements_callbacks_.size()) {
152 this->current_adv_index_ = -1;
153 }
154 this->start();
155 this->last_advertisement_time_ = now;
156 }
157}
158
159void BLEAdvertising::register_raw_advertisement_callback(std::function<void(bool)> &&callback) {
160 this->raw_advertisements_callbacks_.push_back(std::move(callback));
161}
162
163} // namespace esp32_ble
164} // namespace esphome
165
166#endif
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:170
ESPBTUUID as_128bit() const
Definition ble_uuid.cpp:109
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Application App
Global storage of Application pointer - only one Application can exist.