7#ifdef USE_ESP32_BLE_ADVERTISING
24#include <esp_gap_ble_api.h>
25#include <esp_gattc_api.h>
26#include <esp_gatts_api.h>
33static constexpr uint8_t MAX_BLE_QUEUE_SIZE = 100;
35static constexpr uint8_t MAX_BLE_QUEUE_SIZE = 88;
40 u |= uint64_t(
address[0] & 0xFF) << 40;
41 u |= uint64_t(
address[1] & 0xFF) << 32;
42 u |= uint64_t(
address[2] & 0xFF) << 24;
43 u |= uint64_t(
address[3] & 0xFF) << 16;
44 u |= uint64_t(
address[4] & 0xFF) << 8;
45 u |= uint64_t(
address[5] & 0xFF) << 0;
64#ifdef ESPHOME_ESP32_BLE_EXTENDED_AUTH_PARAMS
94#ifdef ESPHOME_ESP32_BLE_EXTENDED_AUTH_PARAMS
101 this->advertising_cycle_time_ = advertising_cycle_time;
108 void setup()
override;
109 void loop()
override;
114 void set_name(
const char *name) { this->name_ = name; }
120#ifdef USE_ESP32_BLE_ADVERTISING
144#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
146 this->gap_event_callbacks_.add(std::forward<F>(callback));
149#ifdef ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT
151 this->gap_scan_event_callbacks_.add(std::forward<F>(callback));
154#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
156 this->gattc_event_callbacks_.add(std::forward<F>(callback));
159#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
161 this->gatts_event_callbacks_.add(std::forward<F>(callback));
164#ifdef ESPHOME_ESP32_BLE_BLE_STATUS_EVENT_HANDLER_COUNT
166 this->ble_status_event_callbacks_.add(std::forward<F>(callback));
172#ifdef USE_ESP32_BLE_SERVER
173 static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
175#ifdef USE_ESP32_BLE_CLIENT
176 static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
178 static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
193 while ((ble_event = this->ble_events_.pop()) !=
nullptr) {
194 this->ble_event_pool_.release(ble_event);
196 this->ble_events_.get_and_reset_dropped_count();
199#ifdef USE_ESP32_BLE_ADVERTISING
200 if (this->advertising_ !=
nullptr)
201 this->advertising_->
loop();
204#ifdef USE_ESP32_BLE_ADVERTISING
207 if (this->advertising_ !=
nullptr)
220#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
222 void(esp_gap_ble_cb_event_t, esp_ble_gap_cb_param_t *)>
223 gap_event_callbacks_;
225#ifdef ESPHOME_ESP32_BLE_GAP_SCAN_EVENT_HANDLER_COUNT
227 gap_scan_event_callbacks_;
229#if defined(USE_ESP32_BLE_CLIENT) && defined(ESPHOME_ESP32_BLE_GATTC_EVENT_HANDLER_COUNT)
231 void(esp_gattc_cb_event_t, esp_gatt_if_t, esp_ble_gattc_cb_param_t *)>
232 gattc_event_callbacks_;
234#if defined(USE_ESP32_BLE_SERVER) && defined(ESPHOME_ESP32_BLE_GATTS_EVENT_HANDLER_COUNT)
236 void(esp_gatts_cb_event_t, esp_gatt_if_t, esp_ble_gatts_cb_param_t *)>
237 gatts_event_callbacks_;
239#ifdef ESPHOME_ESP32_BLE_BLE_STATUS_EVENT_HANDLER_COUNT
240 StaticCallbackManager<ESPHOME_ESP32_BLE_BLE_STATUS_EVENT_HANDLER_COUNT, void()> ble_status_event_callbacks_;
254#ifdef USE_ESP32_BLE_ADVERTISING
257 const char *name_{
nullptr};
261 uint16_t appearance_{0};
264 esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE};
266 bool enable_on_boot_{};
268 esp_bd_addr_t conn_param_failed_bdas_[USE_ESP32_BLE_MAX_CONNECTIONS]{};
269#ifdef USE_ESP32_BLE_ADVERTISING
270 uint8_t advertising_ref_count_{0};
273#ifdef ESPHOME_ESP32_BLE_EXTENDED_AUTH_PARAMS
274 optional<esp_ble_auth_req_t> auth_req_mode_;
276 uint8_t max_key_size_{0};
277 uint8_t min_key_size_{0};
CallbackManager backed by StaticVector for compile-time-known callback counts.
void add_gatts_event_callback(F &&callback)
void advertising_set_manufacturer_data(const std::vector< uint8_t > &data)
void get_mac_msb_first(uint8_t out[MAC_ADDRESS_SIZE]) const
Adapter MAC in printable (MSB-first) order; all-zero until the stack is up.
ESPHOME_ALWAYS_INLINE void advertising_loop_()
void add_gap_scan_event_callback(F &&callback)
void set_enable_on_boot(bool enable_on_boot)
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
void set_auth_req(AuthReqMode req)
void add_gap_event_callback(F &&callback)
static constexpr size_t DEVICE_NAME_BUFFER_SIZE
const char * device_name(char *buffer) const
The device name; written to buffer unless the configured name is used as is.
void set_name(const char *name)
static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
void advertising_start()
Request advertising on behalf of a component.
void advertising_refresh()
Apply the current payload and request count: advertise while requested, otherwise stop.
friend void enqueue_ble_event(Args... args)
void __attribute__((noinline)) loop_handle_state_transition_not_active_()
void forget_conn_param_failure_(const uint8_t *bda)
uint8_t * conn_param_failed_slot_(const uint8_t *bda)
void advertising_register_raw_advertisement_callback(std::function< void(bool)> &&callback)
void advertising_set_service_data_and_name(std::span< const uint8_t > data, bool include_name)
void set_advertising_cycle_time(uint32_t advertising_cycle_time)
void advertising_add_service_uuid(ESPBTUUID uuid)
void advertising_cancel_start_()
void advertising_clear_priority_uuid()
void dump_config() override
void request_state_(bool enable)
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 add_ble_status_event_callback(F &&callback)
void advertising_set_priority_uuid(ESPBTUUID uuid)
Keep uuid and the service data alone in the advertising packet; applies on the next refresh.
void set_max_key_size(uint8_t key_size)
void advertising_set_service_data(const std::vector< uint8_t > &data)
void set_io_capability(IoCapability io_capability)
void advertising_set_appearance(uint16_t appearance)
ESPHOME_ALWAYS_INLINE bool is_active()
float get_setup_priority() const override
void add_gattc_event_callback(F &&callback)
void set_min_key_size(uint8_t key_size)
void advertising_stop()
Release a request made with advertising_start(); advertising stops at the last release.
uint32_t get_advertising_cycle_time() const
void advertising_remove_service_uuid(ESPBTUUID uuid)
std::vector< uint8_t > & out
@ BLE_COMPONENT_STATE_DISABLE
BLE should be disabled on next loop.
@ BLE_COMPONENT_STATE_OFF
Nothing has been initialized yet.
@ BLE_COMPONENT_STATE_ENABLE
BLE should be enabled on next loop.
@ BLE_COMPONENT_STATE_DISABLED
BLE is disabled.
@ BLE_COMPONENT_STATE_ACTIVE
BLE is active.
uint64_t ble_addr_to_uint64(const esp_bd_addr_t address)