11#include <freertos/FreeRTOSConfig.h>
12#include <esp_bt_main.h>
14#include <freertos/task.h>
15#include <esp_gap_ble_api.h>
18namespace esp32_ble_server {
20static const char *
const TAG =
"esp32_ble_server";
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_) {
39 uint16_t index_to_remove = 0;
46 index_to_remove = i + 1;
50 if (index_to_remove > 0) {
52 this->services_to_start_.begin() + index_to_remove - 1);
58 esp_err_t err = esp_ble_gatts_app_register(0);
60 ESP_LOGE(TAG,
"esp_ble_gatts_app_register failed: %d", err);
74 if (entry.service == this->device_information_service_) {
77 entry.service->do_create(
this);
87 ESP_LOGD(TAG,
"BLE server setup successfully");
107 ESP_LOGV(TAG,
"Creating BLE service - %s", uuid.
to_string().c_str());
110 for (; inst_id < 0xFF; inst_id++) {
115 if (inst_id == 0xFF) {
116 ESP_LOGW(TAG,
"Could not create BLE service %s, too many instances", uuid.
to_string().c_str());
120 new BLEService(uuid, num_handles, inst_id, advertise);
121 this->
services_.push_back({uuid, inst_id, service});
122 if (this->
parent_->is_active() && this->registered_) {
129 ESP_LOGV(TAG,
"Removing BLE service - %s %d", uuid.
to_string().c_str(), inst_id);
130 for (
auto it = this->
services_.begin(); it != this->services_.end(); ++it) {
131 if (it->uuid == uuid && it->inst_id == inst_id) {
132 it->service->do_delete();
138 ESP_LOGW(TAG,
"BLE service %s %d does not exist", uuid.
to_string().c_str(), inst_id);
143 if (entry.uuid == uuid && entry.inst_id == inst_id) {
144 return entry.service;
151 esp_ble_gatts_cb_param_t *param) {
153 case ESP_GATTS_CONNECT_EVT: {
154 ESP_LOGD(TAG,
"BLE Client connected");
159 case ESP_GATTS_DISCONNECT_EVT: {
160 ESP_LOGD(TAG,
"BLE Client disconnected");
162 this->
parent_->advertising_start();
166 case ESP_GATTS_REG_EVT: {
176 entry.service->gatts_event_handler(event, gatts_if, param);
185 entry.service->do_delete();
virtual void mark_failed()
Mark this component as failed.
std::string to_string() const
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
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_
void restart_advertising_()
std::unordered_set< uint16_t > clients_
void do_create(BLEServer *server)
void emit_(BLEServerEvt::EmptyEvt event, Args... args)
BLEServer * global_ble_server
const float AFTER_BLUETOOTH
Providing packet encoding functions for exchanging data with a remote host.