ESPHome 2026.3.0-dev
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
21namespace esphome {
22namespace esp32_ble_server {
23
24using namespace esp32_ble;
25using namespace bytebuffer;
26
27class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEventHandler, public Parented<ESP32BLE> {
28 public:
29 void setup() override;
30 void loop() override;
31 void dump_config() override;
32 float get_setup_priority() const override;
33 bool can_proceed() override;
34
35 bool is_running();
36
37 void set_manufacturer_data(const std::vector<uint8_t> &data) {
38 this->manufacturer_data_ = data;
40 }
41
42 void set_max_clients(uint8_t max_clients) { this->max_clients_ = max_clients; }
43 uint8_t get_max_clients() const { return this->max_clients_; }
44
45 BLEService *create_service(ESPBTUUID uuid, bool advertise = false, uint16_t num_handles = 15);
46 void remove_service(ESPBTUUID uuid, uint8_t inst_id = 0);
47 BLEService *get_service(ESPBTUUID uuid, uint8_t inst_id = 0);
48 void enqueue_start_service(BLEService *service) { this->services_to_start_.push_back(service); }
50
51 esp_gatt_if_t get_gatts_if() { return this->gatts_if_; }
52 uint32_t get_connected_client_count() { return this->client_count_; }
53 const uint16_t *get_clients() const { return this->clients_; }
54 uint8_t get_client_count() const { return this->client_count_; }
55
56 void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
57 esp_ble_gatts_cb_param_t *param) override;
58
60
61 // Direct callback registration - supports multiple callbacks
62 void on_connect(std::function<void(uint16_t)> &&callback) {
63 this->callbacks_.push_back({CallbackType::ON_CONNECT, std::move(callback)});
64 }
65 void on_disconnect(std::function<void(uint16_t)> &&callback) {
66 this->callbacks_.push_back({CallbackType::ON_DISCONNECT, std::move(callback)});
67 }
68
69 protected:
70 enum class CallbackType : uint8_t {
73 };
74
77 std::function<void(uint16_t)> callback;
78 };
79
85
87
88 int8_t find_client_index_(uint16_t conn_id) const;
89 void add_client_(uint16_t conn_id);
90 void remove_client_(uint16_t conn_id);
91 void dispatch_callbacks_(CallbackType type, uint16_t conn_id);
92
93 std::vector<CallbackEntry> callbacks_;
94
95 std::vector<uint8_t> manufacturer_data_{};
96 esp_gatt_if_t gatts_if_{0};
97 bool registered_{false};
98
99 uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]{};
100 uint8_t client_count_{0};
101 uint8_t max_clients_{1};
102 std::vector<ServiceEntry> services_{};
103 std::vector<BLEService *> services_to_start_{};
105
112};
113
114// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
115extern BLEServer *global_ble_server;
116
117} // namespace esp32_ble_server
118} // namespace esphome
119
120#endif
Helper class to easily give an object a parent of type T.
Definition helpers.h:1618
std::vector< CallbackEntry > callbacks_
Definition ble_server.h:93
void ble_before_disabled_event_handler() override
const uint16_t * get_clients() const
Definition ble_server.h:53
std::vector< uint8_t > manufacturer_data_
Definition ble_server.h:95
void set_device_information_service(BLEService *service)
Definition ble_server.h:49
void set_manufacturer_data(const std::vector< uint8_t > &data)
Definition ble_server.h:37
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)
BLEService * get_service(ESPBTUUID uuid, uint8_t inst_id=0)
std::vector< BLEService * > services_to_start_
Definition ble_server.h:103
void enqueue_start_service(BLEService *service)
Definition ble_server.h:48
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_
Definition ble_server.h:102
uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]
Definition ble_server.h:99
int8_t find_client_index_(uint16_t conn_id) const
void on_connect(std::function< void(uint16_t)> &&callback)
Definition ble_server.h:62
void on_disconnect(std::function< void(uint16_t)> &&callback)
Definition ble_server.h:65
void set_max_clients(uint8_t max_clients)
Definition ble_server.h:42
uint16_t type
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Definition ble_server.h:75
CallbackType type
Definition ble_server.h:76
std::function< void(uint16_t)> callback
Definition ble_server.h:77
Definition ble_server.h:80
ESPBTUUID uuid
Definition ble_server.h:81
BLEService * service
Definition ble_server.h:83
uint8_t inst_id
Definition ble_server.h:82