ESPHome  2024.3.1
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";
14 
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): %d", err);
91  return;
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): %d", err);
105  return;
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: %d", err);
117  return;
118  }
119 }
120 
122  esp_err_t err = esp_ble_gap_stop_advertising();
123  if (err != ESP_OK) {
124  ESP_LOGE(TAG, "esp_ble_gap_stop_advertising failed: %d", err);
125  return;
126  }
127 }
128 
129 } // namespace esp32_ble
130 } // namespace esphome
131 
132 #endif
void set_service_data(const std::vector< uint8_t > &data)
esp_ble_adv_params_t advertising_params_
void set_manufacturer_data(const std::vector< uint8_t > &data)
void remove_service_uuid(ESPBTUUID uuid)
esp_ble_adv_data_t scan_response_data_
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
std::vector< ESPBTUUID > advertising_uuids_
ESPBTUUID as_128bit() const
Definition: ble_uuid.cpp:103
esp_bt_uuid_t get_uuid() const
Definition: ble_uuid.cpp:164