11#include <freertos/FreeRTOSConfig.h>
12#include <esp_bt_main.h>
13#ifndef CONFIG_BT_CONTROLLER_DISABLED
16#include <freertos/task.h>
17#include <esp_gap_ble_api.h>
25 if (this->
parent_->is_failed()) {
27 ESP_LOGE(TAG,
"BLE Server was marked failed by ESP32BLE");
34 if (!this->
parent_->is_active()) {
37 switch (this->state_) {
43 if (service->is_created()) {
47 if (!service->is_starting() && !service->is_running()) {
48 this->services_to_start_[write_idx++] = service;
51 this->services_to_start_.erase(this->services_to_start_.begin() + write_idx, this->services_to_start_.end());
56#ifdef USE_ESP32_BLE_SERVER_STATIC_PASSKEY
58 esp_err_t passkey_err =
59 esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &this->
passkey_,
sizeof(this->
passkey_));
60 if (passkey_err != ESP_OK) {
61 ESP_LOGE(TAG,
"Setting static passkey failed: %d", passkey_err);
64 esp_err_t err = esp_ble_gatts_app_register(0);
66 ESP_LOGE(TAG,
"esp_ble_gatts_app_register failed: %d", err);
80 if (entry.service == this->device_information_service_) {
83 entry.service->do_create(
this);
94 ESP_LOGD(TAG,
"BLE server setup successfully");
115 this->
parent_->advertising_start();
122 this->
parent_->advertising_stop();
126#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
127 char uuid_buf[esp32_ble::UUID_STR_LEN];
128 uuid.to_str(uuid_buf);
129 ESP_LOGV(TAG,
"Creating BLE service - %s", uuid_buf);
133 for (; inst_id < 0xFF; inst_id++) {
138 if (inst_id == 0xFF) {
139 char warn_uuid_buf[esp32_ble::UUID_STR_LEN];
140 uuid.to_str(warn_uuid_buf);
141 ESP_LOGW(TAG,
"Could not create BLE service %s, too many instances", warn_uuid_buf);
145 new BLEService(uuid, num_handles, inst_id, advertise);
146 this->
services_.push_back({uuid, inst_id, service});
147 if (this->
parent_->is_active() && this->registered_) {
154#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
155 char uuid_buf[esp32_ble::UUID_STR_LEN];
156 uuid.to_str(uuid_buf);
157 ESP_LOGV(TAG,
"Removing BLE service - %s %d", uuid_buf, inst_id);
159 for (
auto it = this->
services_.begin(); it != this->services_.end(); ++it) {
160 if (it->uuid == uuid && it->inst_id == inst_id) {
161 it->service->do_delete();
167 char warn_uuid_buf[esp32_ble::UUID_STR_LEN];
168 uuid.to_str(warn_uuid_buf);
169 ESP_LOGW(TAG,
"BLE service %s %d does not exist", warn_uuid_buf, inst_id);
174 if (entry.uuid == uuid && entry.inst_id == inst_id) {
175 return entry.service;
183 if (entry.type ==
type) {
184 entry.callback(conn_id);
190 esp_ble_gatts_cb_param_t *param) {
192 case ESP_GATTS_CONNECT_EVT: {
193 ESP_LOGD(TAG,
"BLE Client connected");
197 this->
parent_->advertising_refresh();
202 case ESP_GATTS_DISCONNECT_EVT: {
203 ESP_LOGD(TAG,
"BLE Client disconnected");
205 this->
parent_->advertising_refresh();
209 case ESP_GATTS_REG_EVT: {
219 entry.service->gatts_event_handler(event, gatts_if, param);
237#ifdef USE_ESP32_BLE_SERVER_SECURITY
241 if (connect.link_role == 1) {
242 memcpy(
address, connect.remote_bda,
sizeof(esp_bd_addr_t));
244 memset(
address, 0,
sizeof(esp_bd_addr_t));
250 ESP_LOGE(TAG,
"Client array full");
259#ifdef USE_ESP32_BLE_SERVER_SECURITY
265#ifdef USE_ESP32_BLE_SERVER_SECURITY
282 entry.service->do_delete();
288#ifdef USE_ESP32_BLE_SERVER_SECURITY
291 case ESP_GAP_BLE_SEC_REQ_EVT:
292 case ESP_GAP_BLE_AUTH_CMPL_EVT:
293 case ESP_GAP_BLE_PASSKEY_REQ_EVT:
294 case ESP_GAP_BLE_PASSKEY_NOTIF_EVT:
295 case ESP_GAP_BLE_NC_REQ_EVT:
302 const uint8_t *bd_addr = param->ble_security.ble_req.bd_addr;
306 case ESP_GAP_BLE_SEC_REQ_EVT:
307 esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, this->pairing_enabled_);
309 case ESP_GAP_BLE_AUTH_CMPL_EVT: {
310 const auto &auth_cmpl = param->ble_security.auth_cmpl;
311 char addr_str[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
313 if (auth_cmpl.success) {
314 ESP_LOGI(TAG,
"[%s] Authentication complete", addr_str);
316 ESP_LOGW(TAG,
"[%s] Authentication failed, reason 0x%x", addr_str, auth_cmpl.fail_reason);
320#ifdef USE_ESP32_BLE_SERVER_ON_PASSKEY_REQUEST
321 case ESP_GAP_BLE_PASSKEY_REQ_EVT:
325#ifdef USE_ESP32_BLE_SERVER_ON_PASSKEY_NOTIFICATION
326 case ESP_GAP_BLE_PASSKEY_NOTIF_EVT:
330#ifdef USE_ESP32_BLE_SERVER_ON_NUMERIC_COMPARISON_REQUEST
331 case ESP_GAP_BLE_NC_REQ_EVT:
333 param->ble_security.key_notif.passkey);
344 esp_ble_passkey_reply(bda,
true, passkey);
350 esp_ble_confirm_reply(bda, accept);
357 esp_err_t err = esp_ble_remove_bond_device(bda);
359 ESP_LOGW(TAG,
"Removing bond failed: %d", err);
367 "ESP32 BLE Server:\n"
void mark_failed()
Mark this component as failed.
std::vector< CallbackEntry > callbacks_
void add_client_(const esp_ble_gatts_cb_param_t::gatts_connect_evt_param &connect)
CallbackManager< void(uint64_t)> passkey_request_callback_
std::vector< uint8_t > manufacturer_data_
esp_bd_addr_t client_addresses_[USE_ESP32_BLE_MAX_CONNECTIONS]
BLEService * device_information_service_
void remove_client_(uint16_t conn_id)
float get_setup_priority() const override
enum esphome::esp32_ble_server::BLEServer::State INIT
void dump_config() override
void dispatch_callbacks_(CallbackType type, uint16_t conn_id)
bool advertising_required_
ESPHOME_ALWAYS_INLINE bool is_running()
void release_advertising_()
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
bool can_proceed() override
BLEService * get_service(ESPBTUUID uuid, uint8_t inst_id=0)
void passkey_reply(uint64_t address, uint32_t passkey)
Answer a passkey request from the connected client at address.
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
std::vector< BLEService * > services_to_start_
bool is_client_address_(const esp_bd_addr_t address) const
void ble_before_disabled_event_handler()
BLEService * create_service(ESPBTUUID uuid, bool advertise=false, uint16_t num_handles=15)
void remove_service(ESPBTUUID uuid, uint8_t inst_id=0)
std::vector< ServiceEntry > services_
uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]
void restart_advertising_()
CallbackManager< void(uint64_t, uint32_t)> numeric_comparison_request_callback_
void request_advertising_()
int8_t find_client_index_(uint16_t conn_id) const
void numeric_comparison_reply(uint64_t address, bool accept)
Accept or reject a numeric comparison request from the connected client at address.
void remove_bond(uint64_t address)
bool advertising_requested_
CallbackManager< void(uint64_t, uint32_t)> passkey_notification_callback_
void do_create(BLEServer *server)
void uint64_to_mac_msb_first(uint64_t address, uint8_t out[6])
Unpack a uint64 BLE address into printable (MSB-first) byte order — the order bd_addr_t / esp_bd_addr...
BLEServer * global_ble_server
uint64_t ble_addr_to_uint64(const esp_bd_addr_t address)
ESPHOME_LOG_TAG(TAG, "esp32_ble")
constexpr float AFTER_BLUETOOTH
char * format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)