5 #ifdef USE_ESP32_VARIANT_ESP32C6 7 #endif // USE_ESP32_VARIANT_ESP32C6 13 #include <esp_bt_device.h> 14 #include <esp_bt_main.h> 15 #include <esp_gap_ble_api.h> 16 #include <freertos/FreeRTOS.h> 17 #include <freertos/FreeRTOSConfig.h> 18 #include <freertos/task.h> 19 #include <nvs_flash.h> 22 #include <esp32-hal-bt.h> 28 static const char *
const TAG =
"esp32_ble";
35 ESP_LOGCONFIG(TAG,
"Setting up BLE...");
38 ESP_LOGE(TAG,
"BLE could not be prepared for configuration");
102 esp_err_t err = nvs_flash_init();
104 ESP_LOGE(TAG,
"nvs_flash_init failed: %d", err);
123 ESP_LOGE(TAG,
"btStart failed: %d", esp_bt_controller_get_status());
127 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
129 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
130 #ifdef USE_ESP32_VARIANT_ESP32C6 131 esp_bt_controller_config_t cfg = BT_CONTROLLER_CONFIG;
133 esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
135 err = esp_bt_controller_init(&cfg);
137 ESP_LOGE(TAG,
"esp_bt_controller_init failed: %s", esp_err_to_name(err));
140 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
143 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
144 err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
146 ESP_LOGE(TAG,
"esp_bt_controller_enable failed: %s", esp_err_to_name(err));
150 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
151 ESP_LOGE(TAG,
"esp bt controller enable failed");
157 esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
159 err = esp_bluedroid_init();
161 ESP_LOGE(TAG,
"esp_bluedroid_init failed: %d", err);
164 err = esp_bluedroid_enable();
166 ESP_LOGE(TAG,
"esp_bluedroid_enable failed: %d", err);
173 ESP_LOGE(TAG,
"esp_ble_gap_register_callback failed: %d", err);
181 ESP_LOGE(TAG,
"esp_ble_gatts_register_callback failed: %d", err);
189 ESP_LOGE(TAG,
"esp_ble_gattc_register_callback failed: %d", err);
202 if (name.length() > 20) {
204 name.erase(name.begin() + 13, name.end() - 7);
206 name = name.substr(0, 20);
211 err = esp_ble_gap_set_device_name(name.c_str());
213 ESP_LOGE(TAG,
"esp_ble_gap_set_device_name failed: %d", err);
217 err = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &(this->
io_cap_),
sizeof(uint8_t));
219 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param failed: %d", err);
230 esp_err_t err = esp_bluedroid_disable();
232 ESP_LOGE(TAG,
"esp_bluedroid_disable failed: %d", err);
235 err = esp_bluedroid_deinit();
237 ESP_LOGE(TAG,
"esp_bluedroid_deinit failed: %d", err);
243 ESP_LOGE(TAG,
"btStop failed: %d", esp_bt_controller_get_status());
247 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
249 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
250 err = esp_bt_controller_disable();
252 ESP_LOGE(TAG,
"esp_bt_controller_disable failed: %s", esp_err_to_name(err));
255 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED)
258 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
259 err = esp_bt_controller_deinit();
261 ESP_LOGE(TAG,
"esp_bt_controller_deinit failed: %s", esp_err_to_name(err));
265 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
266 ESP_LOGE(TAG,
"esp bt controller disable failed");
280 ESP_LOGD(TAG,
"Disabling BLE...");
283 ble_event_handler->ble_before_disabled_event_handler();
287 ESP_LOGE(TAG,
"BLE could not be dismantled");
295 ESP_LOGD(TAG,
"Enabling BLE...");
299 ESP_LOGE(TAG,
"BLE could not be set up");
312 while (ble_event !=
nullptr) {
313 switch (ble_event->
type_) {
328 ble_event->~BLEEvent();
339 if (new_event ==
nullptr) {
343 new (new_event)
BLEEvent(event, param);
348 ESP_LOGV(TAG,
"(BLE) gap_event_handler - %d", event);
350 gap_handler->gap_event_handler(event, param);
355 esp_ble_gatts_cb_param_t *param) {
357 if (new_event ==
nullptr) {
361 new (new_event)
BLEEvent(event, gatts_if, param);
366 esp_ble_gatts_cb_param_t *param) {
367 ESP_LOGV(TAG,
"(BLE) gatts_event [esp_gatt_if: %d] - %d", gatts_if, event);
369 gatts_handler->gatts_event_handler(event, gatts_if, param);
374 esp_ble_gattc_cb_param_t *param) {
376 if (new_event ==
nullptr) {
380 new (new_event)
BLEEvent(event, gattc_if, param);
385 esp_ble_gattc_cb_param_t *param) {
386 ESP_LOGV(TAG,
"(BLE) gattc_event [esp_gatt_if: %d] - %d", gattc_if, event);
388 gattc_handler->gattc_event_handler(event, gattc_if, param);
395 const uint8_t *mac_address = esp_bt_dev_get_address();
397 const char *io_capability_s;
400 io_capability_s =
"display_only";
403 io_capability_s =
"display_yes_no";
406 io_capability_s =
"keyboard_only";
408 case ESP_IO_CAP_NONE:
409 io_capability_s =
"none";
411 case ESP_IO_CAP_KBDISP:
412 io_capability_s =
"keyboard_display";
415 io_capability_s =
"invalid";
418 ESP_LOGCONFIG(TAG,
"ESP32 BLE:");
419 ESP_LOGCONFIG(TAG,
" MAC address: %02X:%02X:%02X:%02X:%02X:%02X", mac_address[0], mac_address[1], mac_address[2],
420 mac_address[3], mac_address[4], mac_address[5]);
421 ESP_LOGCONFIG(TAG,
" IO Capability: %s", io_capability_s);
423 ESP_LOGCONFIG(TAG,
"ESP32 BLE: bluetooth stack is not enabled");
429 u |= uint64_t(address[0] & 0xFF) << 40;
430 u |= uint64_t(address[1] & 0xFF) << 32;
431 u |= uint64_t(address[2] & 0xFF) << 24;
432 u |= uint64_t(address[3] & 0xFF) << 16;
433 u |= uint64_t(address[4] & 0xFF) << 8;
434 u |= uint64_t(address[5] & 0xFF) << 0;
value_type const & value() const
uint64_t ble_addr_to_uint64(const esp_bd_addr_t address)
void set_min_preferred_interval(uint16_t interval)
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
void set_service_data(const std::vector< uint8_t > &data)
struct esphome::esp32_ble::BLEEvent::@77::gap_event gap
void advertising_remove_service_uuid(ESPBTUUID uuid)
std::vector< GAPEventHandler * > gap_event_handlers_
std::vector< GATTcEventHandler * > gattc_event_handlers_
struct esphome::esp32_ble::BLEEvent::@77::gattc_event gattc
Queue< BLEEvent > ble_events_
void set_manufacturer_data(const std::vector< uint8_t > &data)
uint32_t advertising_cycle_time_
std::vector< GATTsEventHandler * > gatts_event_handlers_
void register_raw_advertisement_callback(std::function< void(bool)> &&callback)
union esphome::esp32_ble::BLEEvent::@77 event_
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
optional< std::string > name_
void real_gattc_event_handler_(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
float get_setup_priority() const override
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Nothing has been initialized yet.
void real_gatts_event_handler_(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
void advertising_register_raw_advertisement_callback(std::function< void(bool)> &&callback)
void advertising_set_service_data(const std::vector< uint8_t > &data)
Application App
Global storage of Application pointer - only one Application can exist.
std::vector< BLEStatusEventHandler * > ble_status_event_handlers_
enum esphome::esp32_ble::BLEEvent::ble_event_t type_
const std::string & get_name() const
Get the name of this Application set by pre_setup().
void remove_service_uuid(ESPBTUUID uuid)
void deallocate(T *p, size_t n)
bool is_name_add_mac_suffix_enabled() const
void dump_config() override
void advertising_set_manufacturer_data(const std::vector< uint8_t > &data)
virtual void mark_failed()
Mark this component as failed.
void real_gap_event_handler_(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
Implementation of SPI Controller mode.
BLE should be disabled on next loop.
void add_service_uuid(ESPBTUUID uuid)
struct esphome::esp32_ble::BLEEvent::@77::gatts_event gatts
BLE should be enabled on next loop.
An STL allocator that uses SPI or internal RAM.
void advertising_add_service_uuid(ESPBTUUID uuid)
void IRAM_ATTR HOT delay(uint32_t ms)
BLEAdvertising * advertising_
void set_scan_response(bool scan_response)