ESPHome 2026.5.1
Loading...
Searching...
No Matches
ble_server.h
Go to the documentation of this file.
1#pragma once
2
3#include "ble_service.h"
5
11
12#include <memory>
13#include <vector>
14#include <unordered_map>
15#include <functional>
16
17#ifdef USE_ESP32
18
19#include <esp_gatts_api.h>
20
22
23using namespace esp32_ble;
24using namespace bytebuffer;
25
26class BLEServer : public Component, public Parented<ESP32BLE> {
27 public:
28 void setup() override;
29 void loop() override;
30 void dump_config() override;
31 float get_setup_priority() const override;
32 bool can_proceed() override;
33
34 ESPHOME_ALWAYS_INLINE bool is_running() { return this->parent_->is_active() && this->state_ == RUNNING; }
35
36 void set_manufacturer_data(const std::vector<uint8_t> &data) {
37 this->manufacturer_data_ = data;
39 }
40
41 void set_max_clients(uint8_t max_clients) { this->max_clients_ = max_clients; }
42 uint8_t get_max_clients() const { return this->max_clients_; }
43
44 BLEService *create_service(ESPBTUUID uuid, bool advertise = false, uint16_t num_handles = 15);
45 void remove_service(ESPBTUUID uuid, uint8_t inst_id = 0);
46 BLEService *get_service(ESPBTUUID uuid, uint8_t inst_id = 0);
47 void enqueue_start_service(BLEService *service) { this->services_to_start_.push_back(service); }
49
50 esp_gatt_if_t get_gatts_if() { return this->gatts_if_; }
52 const uint16_t *get_clients() const { return this->clients_; }
53 uint8_t get_client_count() const { return this->client_count_; }
54
55 void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
56
58
59 // Direct callback registration - supports multiple callbacks
60 void on_connect(std::function<void(uint16_t)> &&callback) {
61 this->callbacks_.push_back({CallbackType::ON_CONNECT, std::move(callback)});
62 }
63 void on_disconnect(std::function<void(uint16_t)> &&callback) {
64 this->callbacks_.push_back({CallbackType::ON_DISCONNECT, std::move(callback)});
65 }
66
67 protected:
68 enum class CallbackType : uint8_t {
71 };
72
75 std::function<void(uint16_t)> callback;
76 };
77
83
85
86 int8_t find_client_index_(uint16_t conn_id) const;
87 void add_client_(uint16_t conn_id);
88 void remove_client_(uint16_t conn_id);
89 void dispatch_callbacks_(CallbackType type, uint16_t conn_id);
90
91 std::vector<CallbackEntry> callbacks_;
92
93 std::vector<uint8_t> manufacturer_data_{};
94 esp_gatt_if_t gatts_if_{0};
95 bool registered_{false};
96
97 uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]{};
98 uint8_t client_count_{0};
99 uint8_t max_clients_{1};
100 std::vector<ServiceEntry> services_{};
101 std::vector<BLEService *> services_to_start_{};
103
110};
111
112// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
113extern BLEServer *global_ble_server;
114
115} // namespace esphome::esp32_ble_server
116
117#endif
Helper class to easily give an object a parent of type T.
Definition helpers.h:1861
std::vector< CallbackEntry > callbacks_
Definition ble_server.h:91
const uint16_t * get_clients() const
Definition ble_server.h:52
std::vector< uint8_t > manufacturer_data_
Definition ble_server.h:93
void set_device_information_service(BLEService *service)
Definition ble_server.h:48
void set_manufacturer_data(const std::vector< uint8_t > &data)
Definition ble_server.h:36
void remove_client_(uint16_t conn_id)
float get_setup_priority() const override
enum esphome::esp32_ble_server::BLEServer::State INIT
void dispatch_callbacks_(CallbackType type, uint16_t conn_id)
ESPHOME_ALWAYS_INLINE bool is_running()
Definition ble_server.h:34
BLEService * get_service(ESPBTUUID uuid, uint8_t inst_id=0)
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
std::vector< BLEService * > services_to_start_
Definition ble_server.h:101
void enqueue_start_service(BLEService *service)
Definition ble_server.h:47
BLEService * create_service(ESPBTUUID uuid, bool advertise=false, uint16_t num_handles=15)
void remove_service(ESPBTUUID uuid, uint8_t inst_id=0)
void add_client_(uint16_t conn_id)
std::vector< ServiceEntry > services_
Definition ble_server.h:100
uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]
Definition ble_server.h:97
int8_t find_client_index_(uint16_t conn_id) const
void on_connect(std::function< void(uint16_t)> &&callback)
Definition ble_server.h:60
void on_disconnect(std::function< void(uint16_t)> &&callback)
Definition ble_server.h:63
void set_max_clients(uint8_t max_clients)
Definition ble_server.h:41
uint16_t type
static void uint32_t
Definition ble_server.h:73
CallbackType type
Definition ble_server.h:74
std::function< void(uint16_t)> callback
Definition ble_server.h:75
Definition ble_server.h:78
ESPBTUUID uuid
Definition ble_server.h:79
BLEService * service
Definition ble_server.h:81
uint8_t inst_id
Definition ble_server.h:80