ESPHome  2022.11.3
ble_advertising.cpp
Go to the documentation of this file.
1 #include "ble_advertising.h"
2 
3 #ifdef USE_ESP32
4 
5 #include "ble_uuid.h"
6 #include <cstring>
7 #include <cstdio>
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 = 0x20;
20  this->advertising_data_.max_interval = 0x40;
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_manufacturer_data(uint8_t *data, uint16_t size) {
46  this->advertising_data_.p_manufacturer_data = data;
47  this->advertising_data_.manufacturer_len = size;
48 }
49 
51  int num_services = this->advertising_uuids_.size();
52  if (num_services == 0) {
53  this->advertising_data_.service_uuid_len = 0;
54  } else {
55  this->advertising_data_.service_uuid_len = 16 * num_services;
56  // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
57  this->advertising_data_.p_service_uuid = new uint8_t[this->advertising_data_.service_uuid_len];
58  uint8_t *p = this->advertising_data_.p_service_uuid;
59  for (int i = 0; i < num_services; i++) {
60  ESPBTUUID uuid = this->advertising_uuids_[i];
61  memcpy(p, uuid.as_128bit().get_uuid().uuid.uuid128, 16);
62  p += 16;
63  }
64  }
65 
66  esp_err_t err;
67 
68  this->advertising_data_.set_scan_rsp = false;
69  this->advertising_data_.include_name = !this->scan_response_;
70  this->advertising_data_.include_txpower = !this->scan_response_;
71  err = esp_ble_gap_config_adv_data(&this->advertising_data_);
72  if (err != ESP_OK) {
73  ESP_LOGE(TAG, "esp_ble_gap_config_adv_data failed (Advertising): %d", err);
74  return;
75  }
76 
77  memcpy(&this->scan_response_data_, &this->advertising_data_, sizeof(esp_ble_adv_data_t));
78  this->scan_response_data_.set_scan_rsp = true;
79  this->scan_response_data_.include_name = true;
80  this->scan_response_data_.include_txpower = true;
81  this->scan_response_data_.appearance = 0;
82  this->scan_response_data_.flag = 0;
83  err = esp_ble_gap_config_adv_data(&this->scan_response_data_);
84  if (err != ESP_OK) {
85  ESP_LOGE(TAG, "esp_ble_gap_config_adv_data failed (Scan response): %d", err);
86  return;
87  }
88 
89  if (this->advertising_data_.service_uuid_len > 0) {
90  delete[] this->advertising_data_.p_service_uuid;
91  this->advertising_data_.p_service_uuid = nullptr;
92  }
93 
94  err = esp_ble_gap_start_advertising(&this->advertising_params_);
95  if (err != ESP_OK) {
96  ESP_LOGE(TAG, "esp_ble_gap_start_advertising failed: %d", err);
97  return;
98  }
99 }
100 
102  esp_err_t err = esp_ble_gap_stop_advertising();
103  if (err != ESP_OK) {
104  ESP_LOGE(TAG, "esp_ble_gap_stop_advertising failed: %d", err);
105  return;
106  }
107 }
108 
109 } // namespace esp32_ble
110 } // namespace esphome
111 
112 #endif
esp_ble_adv_params_t advertising_params_
void remove_service_uuid(ESPBTUUID uuid)
esp_ble_adv_data_t scan_response_data_
void set_manufacturer_data(uint8_t *data, uint16_t size)
Definition: a4988.cpp:4
std::vector< ESPBTUUID > advertising_uuids_
ESPBTUUID as_128bit() const
Definition: ble_uuid.cpp:100