11#include <freertos/FreeRTOSConfig.h>
12#include <esp_bt_main.h>
13#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
16#include <freertos/task.h>
17#include <esp_gap_ble_api.h>
20namespace esp32_ble_server {
22static const char *
const TAG =
"esp32_ble_server";
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_) {
41 uint16_t index_to_remove = 0;
48 index_to_remove = i + 1;
52 if (index_to_remove > 0) {
54 this->services_to_start_.begin() + index_to_remove - 1);
60 esp_err_t err = esp_ble_gatts_app_register(0);
62 ESP_LOGE(TAG,
"esp_ble_gatts_app_register failed: %d", err);
76 if (entry.service == this->device_information_service_) {
79 entry.service->do_create(
this);
89 ESP_LOGD(TAG,
"BLE server setup successfully");
109#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
110 char uuid_buf[esp32_ble::UUID_STR_LEN];
112 ESP_LOGV(TAG,
"Creating BLE service - %s", uuid_buf);
116 for (; inst_id < 0xFF; inst_id++) {
121 if (inst_id == 0xFF) {
122 char warn_uuid_buf[esp32_ble::UUID_STR_LEN];
123 uuid.
to_str(warn_uuid_buf);
124 ESP_LOGW(TAG,
"Could not create BLE service %s, too many instances", warn_uuid_buf);
128 new BLEService(uuid, num_handles, inst_id, advertise);
129 this->
services_.push_back({uuid, inst_id, service});
130 if (this->
parent_->is_active() && this->registered_) {
137#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
138 char uuid_buf[esp32_ble::UUID_STR_LEN];
140 ESP_LOGV(TAG,
"Removing BLE service - %s %d", uuid_buf, inst_id);
142 for (
auto it = this->
services_.begin(); it != this->services_.end(); ++it) {
143 if (it->uuid == uuid && it->inst_id == inst_id) {
144 it->service->do_delete();
150 char warn_uuid_buf[esp32_ble::UUID_STR_LEN];
151 uuid.
to_str(warn_uuid_buf);
152 ESP_LOGW(TAG,
"BLE service %s %d does not exist", warn_uuid_buf, inst_id);
157 if (entry.uuid == uuid && entry.inst_id == inst_id) {
158 return entry.service;
166 if (entry.type ==
type) {
167 entry.callback(conn_id);
173 esp_ble_gatts_cb_param_t *param) {
175 case ESP_GATTS_CONNECT_EVT: {
176 ESP_LOGD(TAG,
"BLE Client connected");
181 case ESP_GATTS_DISCONNECT_EVT: {
182 ESP_LOGD(TAG,
"BLE Client disconnected");
184 this->
parent_->advertising_start();
188 case ESP_GATTS_REG_EVT: {
198 entry.service->gatts_event_handler(event, gatts_if, param);
219 ESP_LOGE(TAG,
"Client array full");
236 entry.service->do_delete();
virtual void mark_failed()
Mark this component as failed.
void to_str(std::span< char, UUID_STR_LEN > output) const
std::vector< CallbackEntry > callbacks_
void ble_before_disabled_event_handler() override
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 can_proceed() override
BLEService * get_service(ESPBTUUID uuid, uint8_t inst_id=0)
std::vector< BLEService * > services_to_start_
BLEService * create_service(ESPBTUUID uuid, bool advertise=false, uint16_t num_handles=15)
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) override
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_()
int8_t find_client_index_(uint16_t conn_id) const
void do_create(BLEServer *server)
BLEServer * global_ble_server
const float AFTER_BLUETOOTH
Providing packet encoding functions for exchanging data with a remote host.