ESPHome 2025.5.0
Loading...
Searching...
No Matches
ble.h
Go to the documentation of this file.
1#pragma once
2
3#include "ble_advertising.h"
4#include "ble_uuid.h"
5
6#include <functional>
7
12
13#include "ble_event.h"
14#include "queue.h"
15
16#ifdef USE_ESP32
17
18#include <esp_gap_ble_api.h>
19#include <esp_gattc_api.h>
20#include <esp_gatts_api.h>
21
22namespace esphome {
23namespace esp32_ble {
24
25uint64_t ble_addr_to_uint64(const esp_bd_addr_t address);
26
27// NOLINTNEXTLINE(modernize-use-using)
28typedef struct {
31 uint16_t mtu;
33
35 IO_CAP_OUT = ESP_IO_CAP_OUT,
36 IO_CAP_IO = ESP_IO_CAP_IO,
37 IO_CAP_IN = ESP_IO_CAP_IN,
38 IO_CAP_NONE = ESP_IO_CAP_NONE,
39 IO_CAP_KBDISP = ESP_IO_CAP_KBDISP,
40};
41
54
56 public:
57 virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) = 0;
58};
59
61 public:
62 virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
63 esp_ble_gattc_cb_param_t *param) = 0;
64};
65
67 public:
68 virtual void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
69 esp_ble_gatts_cb_param_t *param) = 0;
70};
71
73 public:
75};
76
77class ESP32BLE : public Component {
78 public:
79 void set_io_capability(IoCapability io_capability) { this->io_cap_ = (esp_ble_io_cap_t) io_capability; }
80
81 void set_advertising_cycle_time(uint32_t advertising_cycle_time) {
82 this->advertising_cycle_time_ = advertising_cycle_time;
83 }
84 uint32_t get_advertising_cycle_time() const { return this->advertising_cycle_time_; }
85
86 void enable();
87 void disable();
88 bool is_active();
89 void setup() override;
90 void loop() override;
91 void dump_config() override;
92 float get_setup_priority() const override;
93 void set_name(const std::string &name) { this->name_ = name; }
94
95 void advertising_start();
96 void advertising_set_service_data(const std::vector<uint8_t> &data);
97 void advertising_set_manufacturer_data(const std::vector<uint8_t> &data);
98 void advertising_set_appearance(uint16_t appearance) { this->appearance_ = appearance; }
101 void advertising_register_raw_advertisement_callback(std::function<void(bool)> &&callback);
102
103 void register_gap_event_handler(GAPEventHandler *handler) { this->gap_event_handlers_.push_back(handler); }
104 void register_gattc_event_handler(GATTcEventHandler *handler) { this->gattc_event_handlers_.push_back(handler); }
105 void register_gatts_event_handler(GATTsEventHandler *handler) { this->gatts_event_handlers_.push_back(handler); }
107 this->ble_status_event_handlers_.push_back(handler);
108 }
109 void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
110
111 protected:
112 static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
113 static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
114 static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
115
116 void real_gatts_event_handler_(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
117 void real_gattc_event_handler_(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
118 void real_gap_event_handler_(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
119
120 bool ble_setup_();
121 bool ble_dismantle_();
122 bool ble_pre_setup_();
123 void advertising_init_();
124
125 std::vector<GAPEventHandler *> gap_event_handlers_;
126 std::vector<GATTcEventHandler *> gattc_event_handlers_;
127 std::vector<GATTsEventHandler *> gatts_event_handlers_;
128 std::vector<BLEStatusEventHandler *> ble_status_event_handlers_;
130
133 esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE};
137 uint16_t appearance_{0};
138};
139
140// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
141extern ESP32BLE *global_ble;
142
143template<typename... Ts> class BLEEnabledCondition : public Condition<Ts...> {
144 public:
145 bool check(Ts... x) override { return global_ble->is_active(); }
146};
147
148template<typename... Ts> class BLEEnableAction : public Action<Ts...> {
149 public:
150 void play(Ts... x) override { global_ble->enable(); }
151};
152
153template<typename... Ts> class BLEDisableAction : public Action<Ts...> {
154 public:
155 void play(Ts... x) override { global_ble->disable(); }
156};
157
158} // namespace esp32_ble
159} // namespace esphome
160
161#endif
uint8_t address
Definition bl0906.h:4
Base class for all automation conditions.
Definition automation.h:75
void play(Ts... x) override
Definition ble.h:155
void play(Ts... x) override
Definition ble.h:150
bool check(Ts... x) override
Definition ble.h:145
std::vector< GATTsEventHandler * > gatts_event_handlers_
Definition ble.h:127
void advertising_set_manufacturer_data(const std::vector< uint8_t > &data)
Definition ble.cpp:74
BLEComponentState state_
Definition ble.h:129
void register_gap_event_handler(GAPEventHandler *handler)
Definition ble.h:103
void set_enable_on_boot(bool enable_on_boot)
Definition ble.h:109
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
Definition ble.cpp:330
optional< std::string > name_
Definition ble.h:136
BLEAdvertising * advertising_
Definition ble.h:132
void register_gattc_event_handler(GATTcEventHandler *handler)
Definition ble.h:104
static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
Definition ble.cpp:366
void advertising_register_raw_advertisement_callback(std::function< void(bool)> &&callback)
Definition ble.cpp:80
std::vector< GAPEventHandler * > gap_event_handlers_
Definition ble.h:125
void real_gap_event_handler_(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
Definition ble.cpp:340
void set_advertising_cycle_time(uint32_t advertising_cycle_time)
Definition ble.h:81
void register_ble_status_event_handler(BLEStatusEventHandler *handler)
Definition ble.h:106
uint32_t advertising_cycle_time_
Definition ble.h:134
void advertising_add_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:85
void dump_config() override
Definition ble.cpp:387
std::vector< BLEStatusEventHandler * > ble_status_event_handlers_
Definition ble.h:128
void loop() override
Definition ble.cpp:267
void real_gattc_event_handler_(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
Definition ble.cpp:377
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
Definition ble.cpp:347
void register_gatts_event_handler(GATTsEventHandler *handler)
Definition ble.h:105
Queue< BLEEvent > ble_events_
Definition ble.h:131
void advertising_set_service_data(const std::vector< uint8_t > &data)
Definition ble.cpp:68
void set_io_capability(IoCapability io_capability)
Definition ble.h:79
void advertising_set_appearance(uint16_t appearance)
Definition ble.h:98
void real_gatts_event_handler_(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
Definition ble.cpp:358
float get_setup_priority() const override
Definition ble.cpp:385
void setup() override
Definition ble.cpp:29
void set_name(const std::string &name)
Definition ble.h:93
std::vector< GATTcEventHandler * > gattc_event_handlers_
Definition ble.h:126
uint32_t get_advertising_cycle_time() const
Definition ble.h:84
esp_ble_io_cap_t io_cap_
Definition ble.h:133
void advertising_remove_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:91
virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)=0
virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)=0
virtual void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)=0
ESP32BLE * global_ble
Definition ble.cpp:431
uint64_t ble_addr_to_uint64(const esp_bd_addr_t address)
Definition ble.cpp:420
@ BLE_COMPONENT_STATE_DISABLE
BLE should be disabled on next loop.
Definition ble.h:46
@ BLE_COMPONENT_STATE_OFF
Nothing has been initialized yet.
Definition ble.h:44
@ BLE_COMPONENT_STATE_ENABLE
BLE should be enabled on next loop.
Definition ble.h:50
@ BLE_COMPONENT_STATE_DISABLED
BLE is disabled.
Definition ble.h:48
@ BLE_COMPONENT_STATE_ACTIVE
BLE is active.
Definition ble.h:52
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5