10 #include <nvs_flash.h> 11 #include <freertos/FreeRTOSConfig.h> 12 #include <esp_bt_main.h> 14 #include <freertos/task.h> 15 #include <esp_gap_ble_api.h> 18 namespace esp32_ble_server {
20 static 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) {
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 (pair.second == this->device_information_service_) {
77 pair.second->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);
129 ESP_LOGV(TAG,
"Removing BLE service - %s %d", uuid.
to_string().c_str(), inst_id);
131 if (service ==
nullptr) {
132 ESP_LOGW(TAG,
"BLE service %s %d does not exist", uuid.
to_string().c_str(), inst_id);
135 service->do_delete();
153 esp_ble_gatts_cb_param_t *param) {
155 case ESP_GATTS_CONNECT_EVT: {
156 ESP_LOGD(TAG,
"BLE Client connected");
161 case ESP_GATTS_DISCONNECT_EVT: {
162 ESP_LOGD(TAG,
"BLE Client disconnected");
164 this->
parent_->advertising_start();
168 case ESP_GATTS_REG_EVT: {
177 for (
const auto &pair : this->
services_) {
178 pair.second->gatts_event_handler(event, gatts_if, param);
187 pair.second->do_delete();
BLEService * get_service(ESPBTUUID uuid, uint8_t inst_id=0)
void dump_config() override
const char * to_string(SHTCXType type)
float get_setup_priority() const override
bool can_proceed() override
BLEService * device_information_service_
const float AFTER_BLUETOOTH
std::unordered_map< std::string, BLEService * > services_
static std::string get_service_key(ESPBTUUID uuid, uint8_t inst_id)
void remove_client_(uint16_t conn_id)
enum esphome::esp32_ble_server::BLEServer::State INIT
void add_client_(uint16_t conn_id)
BLEServer * global_ble_server
std::unordered_set< uint16_t > clients_
void ble_before_disabled_event_handler() override
void do_create(BLEServer *server)
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) override
std::vector< BLEService * > services_to_start_
void remove_service(ESPBTUUID uuid, uint8_t inst_id=0)
std::string to_string() const
virtual void mark_failed()
Mark this component as failed.
void emit_(BLEServerEvt::EmptyEvt event, Args... args)
BLEService * create_service(ESPBTUUID uuid, bool advertise=false, uint16_t num_handles=15)
Implementation of SPI Controller mode.
void restart_advertising_()
std::vector< uint8_t > manufacturer_data_