ESPHome  2024.3.2
esp32_ble_beacon.cpp
Go to the documentation of this file.
1 #include "esp32_ble_beacon.h"
2 #include "esphome/core/log.h"
3 
4 #ifdef USE_ESP32
5 
6 #include <nvs_flash.h>
7 #include <freertos/FreeRTOS.h>
8 #include <esp_bt_main.h>
9 #include <esp_bt.h>
10 #include <freertos/task.h>
11 #include <esp_gap_ble_api.h>
12 #include <cstring>
13 #include "esphome/core/hal.h"
14 
15 #ifdef USE_ARDUINO
16 #include <esp32-hal-bt.h>
17 #endif
18 
19 namespace esphome {
20 namespace esp32_ble_beacon {
21 
22 static const char *const TAG = "esp32_ble_beacon";
23 
24 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
25 static esp_ble_adv_params_t ble_adv_params = {
26  .adv_int_min = 0x20,
27  .adv_int_max = 0x40,
28  .adv_type = ADV_TYPE_NONCONN_IND,
29  .own_addr_type = BLE_ADDR_TYPE_PUBLIC,
30  .peer_addr = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
31  .peer_addr_type = BLE_ADDR_TYPE_PUBLIC,
32  .channel_map = ADV_CHNL_ALL,
33  .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
34 };
35 
36 #define ENDIAN_CHANGE_U16(x) ((((x) &0xFF00) >> 8) + (((x) &0xFF) << 8))
37 
38 static const esp_ble_ibeacon_head_t IBEACON_COMMON_HEAD = {
39  .flags = {0x02, 0x01, 0x06}, .length = 0x1A, .type = 0xFF, .company_id = {0x4C, 0x00}, .beacon_type = {0x02, 0x15}};
40 
42  ESP_LOGCONFIG(TAG, "ESP32 BLE Beacon:");
43  char uuid[37];
44  char *bpos = uuid;
45  for (int8_t ii = 0; ii < 16; ++ii) {
46  bpos += sprintf(bpos, "%02X", this->uuid_[ii]);
47  if (ii == 3 || ii == 5 || ii == 7 || ii == 9) {
48  bpos += sprintf(bpos, "-");
49  }
50  }
51  uuid[36] = '\0';
52  ESP_LOGCONFIG(TAG,
53  " UUID: %s, Major: %u, Minor: %u, Min Interval: %ums, Max Interval: %ums, Measured Power: %d"
54  ", TX Power: %ddBm",
55  uuid, this->major_, this->minor_, this->min_interval_, this->max_interval_, this->measured_power_,
56  this->tx_power_);
57 }
58 
60  ESP_LOGCONFIG(TAG, "Setting up ESP32 BLE beacon...");
62 
63  xTaskCreatePinnedToCore(ESP32BLEBeacon::ble_core_task,
64  "ble_task", // name
65  10000, // stack size (in words)
66  nullptr, // input params
67  1, // priority
68  nullptr, // Handle, not needed
69  0 // core
70  );
71 }
72 
74 void ESP32BLEBeacon::ble_core_task(void *params) {
75  ble_setup();
76 
77  while (true) {
78  delay(1000); // NOLINT
79  }
80 }
81 
83  ble_adv_params.adv_int_min = static_cast<uint16_t>(global_esp32_ble_beacon->min_interval_ / 0.625f);
84  ble_adv_params.adv_int_max = static_cast<uint16_t>(global_esp32_ble_beacon->max_interval_ / 0.625f);
85 
86  // Initialize non-volatile storage for the bluetooth controller
87  esp_err_t err = nvs_flash_init();
88  if (err != ESP_OK) {
89  ESP_LOGE(TAG, "nvs_flash_init failed: %d", err);
90  return;
91  }
92 
93 #ifdef USE_ARDUINO
94  if (!btStart()) {
95  ESP_LOGE(TAG, "btStart failed: %d", esp_bt_controller_get_status());
96  return;
97  }
98 #else
99  if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
100  // start bt controller
101  if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
102  esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
103  err = esp_bt_controller_init(&cfg);
104  if (err != ESP_OK) {
105  ESP_LOGE(TAG, "esp_bt_controller_init failed: %s", esp_err_to_name(err));
106  return;
107  }
108  while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
109  ;
110  }
111  if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
112  err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
113  if (err != ESP_OK) {
114  ESP_LOGE(TAG, "esp_bt_controller_enable failed: %s", esp_err_to_name(err));
115  return;
116  }
117  }
118  if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
119  ESP_LOGE(TAG, "esp bt controller enable failed");
120  return;
121  }
122  }
123 #endif
124 
125  esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
126 
127  err = esp_bluedroid_init();
128  if (err != ESP_OK) {
129  ESP_LOGE(TAG, "esp_bluedroid_init failed: %d", err);
130  return;
131  }
132  err = esp_bluedroid_enable();
133  if (err != ESP_OK) {
134  ESP_LOGE(TAG, "esp_bluedroid_enable failed: %d", err);
135  return;
136  }
137  err = esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV,
138  static_cast<esp_power_level_t>((global_esp32_ble_beacon->tx_power_ + 12) / 3));
139  if (err != ESP_OK) {
140  ESP_LOGE(TAG, "esp_ble_tx_power_set failed: %s", esp_err_to_name(err));
141  return;
142  }
143  err = esp_ble_gap_register_callback(ESP32BLEBeacon::gap_event_handler);
144  if (err != ESP_OK) {
145  ESP_LOGE(TAG, "esp_ble_gap_register_callback failed: %d", err);
146  return;
147  }
148 
149  esp_ble_ibeacon_t ibeacon_adv_data;
150  memcpy(&ibeacon_adv_data.ibeacon_head, &IBEACON_COMMON_HEAD, sizeof(esp_ble_ibeacon_head_t));
151  memcpy(&ibeacon_adv_data.ibeacon_vendor.proximity_uuid, global_esp32_ble_beacon->uuid_.data(),
152  sizeof(ibeacon_adv_data.ibeacon_vendor.proximity_uuid));
153  ibeacon_adv_data.ibeacon_vendor.minor = ENDIAN_CHANGE_U16(global_esp32_ble_beacon->minor_);
154  ibeacon_adv_data.ibeacon_vendor.major = ENDIAN_CHANGE_U16(global_esp32_ble_beacon->major_);
155  ibeacon_adv_data.ibeacon_vendor.measured_power = static_cast<uint8_t>(global_esp32_ble_beacon->measured_power_);
156 
157  esp_ble_gap_config_adv_data_raw((uint8_t *) &ibeacon_adv_data, sizeof(ibeacon_adv_data));
158 }
159 
160 void ESP32BLEBeacon::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
161  esp_err_t err;
162  switch (event) {
163  case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT: {
164  err = esp_ble_gap_start_advertising(&ble_adv_params);
165  if (err != ESP_OK) {
166  ESP_LOGE(TAG, "esp_ble_gap_start_advertising failed: %d", err);
167  }
168  break;
169  }
170  case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: {
171  err = param->adv_start_cmpl.status;
172  if (err != ESP_BT_STATUS_SUCCESS) {
173  ESP_LOGE(TAG, "BLE adv start failed: %s", esp_err_to_name(err));
174  }
175  break;
176  }
177  case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: {
178  err = param->adv_stop_cmpl.status;
179  if (err != ESP_BT_STATUS_SUCCESS) {
180  ESP_LOGE(TAG, "BLE adv stop failed: %s", esp_err_to_name(err));
181  } else {
182  ESP_LOGD(TAG, "BLE stopped advertising successfully");
183  }
184  break;
185  }
186  default:
187  break;
188  }
189 }
190 
191 ESP32BLEBeacon *global_esp32_ble_beacon = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
192 
193 } // namespace esp32_ble_beacon
194 } // namespace esphome
195 
196 #endif
ESP32BLEBeacon * global_esp32_ble_beacon
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26