ESPHome 2025.12.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 BLEService *create_service(ESPBTUUID uuid, bool advertise = false, uint16_t num_handles = 15);
43 void remove_service(ESPBTUUID uuid, uint8_t inst_id = 0);
44 BLEService *get_service(ESPBTUUID uuid, uint8_t inst_id = 0);
45 void enqueue_start_service(BLEService *service) { this->services_to_start_.push_back(service); }
47
48 esp_gatt_if_t get_gatts_if() { return this->gatts_if_; }
49 uint32_t get_connected_client_count() { return this->client_count_; }
50 const uint16_t *get_clients() const { return this->clients_; }
51 uint8_t get_client_count() const { return this->client_count_; }
52
53 void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
54 esp_ble_gatts_cb_param_t *param) override;
55
57
58 // Direct callback registration - supports multiple callbacks
59 void on_connect(std::function<void(uint16_t)> &&callback) {
60 this->callbacks_.push_back({CallbackType::ON_CONNECT, std::move(callback)});
61 }
62 void on_disconnect(std::function<void(uint16_t)> &&callback) {
63 this->callbacks_.push_back({CallbackType::ON_DISCONNECT, std::move(callback)});
64 }
65
66 protected:
67 enum class CallbackType : uint8_t {
70 };
71
74 std::function<void(uint16_t)> callback;
75 };
76
82
84
85 int8_t find_client_index_(uint16_t conn_id) const;
86 void add_client_(uint16_t conn_id);
87 void remove_client_(uint16_t conn_id);
88 void dispatch_callbacks_(CallbackType type, uint16_t conn_id);
89
90 std::vector<CallbackEntry> callbacks_;
91
92 std::vector<uint8_t> manufacturer_data_{};
93 esp_gatt_if_t gatts_if_{0};
94 bool registered_{false};
95
96 uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]{};
97 uint8_t client_count_{0};
98 std::vector<ServiceEntry> services_{};
99 std::vector<BLEService *> services_to_start_{};
101
108};
109
110// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
111extern BLEServer *global_ble_server;
112
113} // namespace esp32_ble_server
114} // namespace esphome
115
116#endif
Helper class to easily give an object a parent of type T.
Definition helpers.h:918
std::vector< CallbackEntry > callbacks_
Definition ble_server.h:90
void ble_before_disabled_event_handler() override
const uint16_t * get_clients() const
Definition ble_server.h:50
std::vector< uint8_t > manufacturer_data_
Definition ble_server.h:92
void set_device_information_service(BLEService *service)
Definition ble_server.h:46
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:99
void enqueue_start_service(BLEService *service)
Definition ble_server.h:45
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:98
uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]
Definition ble_server.h:96
int8_t find_client_index_(uint16_t conn_id) const
void on_connect(std::function< void(uint16_t)> &&callback)
Definition ble_server.h:59
void on_disconnect(std::function< void(uint16_t)> &&callback)
Definition ble_server.h:62
uint16_t type
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Definition ble_server.h:72
CallbackType type
Definition ble_server.h:73
std::function< void(uint16_t)> callback
Definition ble_server.h:74
Definition ble_server.h:77
ESPBTUUID uuid
Definition ble_server.h:78
BLEService * service
Definition ble_server.h:80
uint8_t inst_id
Definition ble_server.h:79