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 ESP_LOGV(TAG,
"Creating BLE service - %s", uuid.
to_string().c_str());
112 for (; inst_id < 0xFF; inst_id++) {
117 if (inst_id == 0xFF) {
118 ESP_LOGW(TAG,
"Could not create BLE service %s, too many instances", uuid.
to_string().c_str());
122 new BLEService(uuid, num_handles, inst_id, advertise);
123 this->
services_.push_back({uuid, inst_id, service});
124 if (this->
parent_->is_active() && this->registered_) {
131 ESP_LOGV(TAG,
"Removing BLE service - %s %d", uuid.
to_string().c_str(), inst_id);
132 for (
auto it = this->
services_.begin(); it != this->services_.end(); ++it) {
133 if (it->uuid == uuid && it->inst_id == inst_id) {
134 it->service->do_delete();
140 ESP_LOGW(TAG,
"BLE service %s %d does not exist", uuid.
to_string().c_str(), inst_id);
145 if (entry.uuid == uuid && entry.inst_id == inst_id) {
146 return entry.service;
154 if (entry.type ==
type) {
155 entry.callback(conn_id);
161 esp_ble_gatts_cb_param_t *param) {
163 case ESP_GATTS_CONNECT_EVT: {
164 ESP_LOGD(TAG,
"BLE Client connected");
169 case ESP_GATTS_DISCONNECT_EVT: {
170 ESP_LOGD(TAG,
"BLE Client disconnected");
172 this->
parent_->advertising_start();
176 case ESP_GATTS_REG_EVT: {
186 entry.service->gatts_event_handler(event, gatts_if, param);
207 ESP_LOGE(TAG,
"Client array full");
224 entry.service->do_delete();
virtual void mark_failed()
Mark this component as failed.
std::string to_string() 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.