10#include <freertos/FreeRTOSConfig.h>
11#include <esp_bt_main.h>
12#ifndef CONFIG_BT_CONTROLLER_DISABLED
15#include <freertos/task.h>
16#include <esp_gap_ble_api.h>
23 if (this->
parent_->is_failed()) {
25 ESP_LOGE(TAG,
"BLE Server was marked failed by ESP32BLE");
32 if (!this->
parent_->is_active()) {
35 switch (this->state_) {
41 if (service->is_created()) {
45 if (!service->is_starting() && !service->is_running()) {
46 this->services_to_start_[write_idx++] = service;
49 this->services_to_start_.erase(this->services_to_start_.begin() + write_idx, this->services_to_start_.end());
54 esp_err_t err = esp_ble_gatts_app_register(0);
56 ESP_LOGE(TAG,
"esp_ble_gatts_app_register failed: %d", err);
70 if (entry.service == this->device_information_service_) {
73 entry.service->do_create(
this);
84 ESP_LOGD(TAG,
"BLE server setup successfully");
105 this->
parent_->advertising_start();
112 this->
parent_->advertising_stop();
116#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
117 char uuid_buf[esp32_ble::UUID_STR_LEN];
118 uuid.to_str(uuid_buf);
119 ESP_LOGV(TAG,
"Creating BLE service - %s", uuid_buf);
123 for (; inst_id < 0xFF; inst_id++) {
128 if (inst_id == 0xFF) {
129 char warn_uuid_buf[esp32_ble::UUID_STR_LEN];
130 uuid.to_str(warn_uuid_buf);
131 ESP_LOGW(TAG,
"Could not create BLE service %s, too many instances", warn_uuid_buf);
135 new BLEService(uuid, num_handles, inst_id, advertise);
136 this->
services_.push_back({uuid, inst_id, service});
137 if (this->
parent_->is_active() && this->registered_) {
144#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
145 char uuid_buf[esp32_ble::UUID_STR_LEN];
146 uuid.to_str(uuid_buf);
147 ESP_LOGV(TAG,
"Removing BLE service - %s %d", uuid_buf, inst_id);
149 for (
auto it = this->
services_.begin(); it != this->services_.end(); ++it) {
150 if (it->uuid == uuid && it->inst_id == inst_id) {
151 it->service->do_delete();
157 char warn_uuid_buf[esp32_ble::UUID_STR_LEN];
158 uuid.to_str(warn_uuid_buf);
159 ESP_LOGW(TAG,
"BLE service %s %d does not exist", warn_uuid_buf, inst_id);
164 if (entry.uuid == uuid && entry.inst_id == inst_id) {
165 return entry.service;
173 if (entry.type ==
type) {
174 entry.callback(conn_id);
180 esp_ble_gatts_cb_param_t *param) {
182 case ESP_GATTS_CONNECT_EVT: {
183 ESP_LOGD(TAG,
"BLE Client connected");
187 this->
parent_->advertising_refresh();
192 case ESP_GATTS_DISCONNECT_EVT: {
193 ESP_LOGD(TAG,
"BLE Client disconnected");
195 this->
parent_->advertising_refresh();
199 case ESP_GATTS_REG_EVT: {
209 entry.service->gatts_event_handler(event, gatts_if, param);
230 ESP_LOGE(TAG,
"Client array full");
249 entry.service->do_delete();
259 "ESP32 BLE Server:\n"
void mark_failed()
Mark this component as failed.
std::vector< CallbackEntry > callbacks_
std::vector< uint8_t > manufacturer_data_
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_()
bool can_proceed() override
BLEService * get_service(ESPBTUUID uuid, uint8_t inst_id=0)
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_
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)
void add_client_(uint16_t conn_id)
std::vector< ServiceEntry > services_
uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]
void restart_advertising_()
void request_advertising_()
int8_t find_client_index_(uint16_t conn_id) const
bool advertising_requested_
void do_create(BLEServer *server)
BLEServer * global_ble_server
ESPHOME_LOG_TAG(TAG, "esp32_ble")
constexpr float AFTER_BLUETOOTH