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";
22 static const uint16_t DEVICE_INFORMATION_SERVICE_UUID = 0x180A;
23 static const uint16_t MODEL_UUID = 0x2A24;
24 static const uint16_t VERSION_UUID = 0x2A26;
25 static const uint16_t MANUFACTURER_UUID = 0x2A29;
28 if (this->
parent_->is_failed()) {
30 ESP_LOGE(TAG,
"BLE Server was marked failed by ESP32BLE");
37 if (!this->
parent_->is_active()) {
40 switch (this->state_) {
45 esp_err_t err = esp_ble_gatts_app_register(0);
47 ESP_LOGE(TAG,
"esp_ble_gatts_app_register failed: %d", err);
58 pair.second->do_create(
this);
77 ESP_LOGD(TAG,
"BLE server setup successfully");
109 version->
set_value(
"ESPHome " ESPHOME_VERSION);
119 ESP_LOGV(TAG,
"Creating BLE service - %s", uuid.
to_string().c_str());
122 if (service !=
nullptr) {
123 ESP_LOGW(TAG,
"BLE service %s already exists", uuid.
to_string().c_str());
126 service =
new BLEService(uuid, num_handles, inst_id, advertise);
128 service->do_create(
this);
132 ESP_LOGV(TAG,
"Removing BLE service - %s", uuid.
to_string().c_str());
134 if (service ==
nullptr) {
135 ESP_LOGW(TAG,
"BLE service %s not found", uuid.
to_string().c_str());
138 service->do_delete();
152 esp_ble_gatts_cb_param_t *param) {
154 case ESP_GATTS_CONNECT_EVT: {
155 ESP_LOGD(TAG,
"BLE Client connected");
156 this->
add_client_(param->connect.conn_id, (
void *)
this);
159 component->on_client_connect();
163 case ESP_GATTS_DISCONNECT_EVT: {
164 ESP_LOGD(TAG,
"BLE Client disconnected");
167 this->
parent_->advertising_start();
169 component->on_client_disconnect();
173 case ESP_GATTS_REG_EVT: {
182 for (
const auto &pair : this->
services_) {
183 pair.second->gatts_event_handler(event, gatts_if, param);
192 pair.second->do_delete();
value_type const & value() const
void dump_config() override
bool remove_client_(uint16_t conn_id)
float get_setup_priority() const override
bool can_proceed() override
std::unordered_map< uint16_t, void * > clients_
BLEService * device_information_service_
bool create_device_characteristics_()
void create_service(ESPBTUUID uuid, bool advertise=false, uint16_t num_handles=15, uint8_t inst_id=0)
void set_value(const uint8_t *data, size_t length)
const float AFTER_BLUETOOTH
optional< std::string > model_
void add_client_(uint16_t conn_id, void *client)
std::unordered_map< std::string, BLEService * > services_
std::vector< BLEServiceComponent * > service_components_
enum esphome::esp32_ble_server::BLEServer::State INIT
uint32_t connected_clients_
static ESPBTUUID from_uint16(uint16_t uuid)
BLEServer * global_ble_server
BLEService * get_service(ESPBTUUID uuid)
void ble_before_disabled_event_handler() override
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::string manufacturer_
std::string to_string() const
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
void remove_service(ESPBTUUID uuid)
BLECharacteristic * create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties)
static const uint32_t PROPERTY_READ
void restart_advertising_()
std::vector< uint8_t > manufacturer_data_