esphome-api-client 0.5.0
Modern C++17 client for the ESPHome native API (plaintext + Noise)
Loading...
Searching...
No Matches
bluetooth_proxy.hpp
Go to the documentation of this file.
1#pragma once
2
5
8
9#include <cstdint>
10#include <functional>
11#include <string>
12#include <unordered_map>
13#include <vector>
14
15namespace esphome::api {
16
19 std::string uuid;
20 std::string data;
21};
22
25 std::string uuid;
26 std::string data;
27};
28
31 std::uint64_t address = 0;
32 std::string name;
33 std::int32_t rssi = 0;
34 std::uint32_t address_type = 0;
35 std::vector<BleServiceData> service_data;
36 std::vector<std::string> service_uuids;
37 std::vector<BleManufacturerData> manufacturer_data;
38};
39
42 std::uint64_t address = 0;
43 std::int32_t rssi = 0;
44 std::uint32_t address_type = 0;
45 std::string data;
46};
47
54
57 std::uint64_t address = 0;
58 bool connected = false;
59 std::uint32_t mtu = 0;
60 std::int32_t error = 0;
61};
62
65 std::uint32_t free = 0;
66 std::uint32_t limit = 0;
67 std::vector<std::uint64_t> allocated;
68};
69
72 std::uint64_t address = 0;
73 std::uint32_t handle = 0;
74 std::string data;
75};
76
80 std::uint64_t address = 0;
81 std::uint32_t handle = 0;
82 std::string data;
83};
84
87 std::vector<std::uint64_t> uuid;
88 std::uint32_t handle = 0;
89};
90
93 bool broadcast = false;
94 bool read = false;
96 bool write = false;
97 bool notify = false;
98 bool indicate = false;
100 bool extended_properties = false;
101
102 [[nodiscard]] static BleGattCharProperties from_bits(const std::uint32_t b) {
103 return {(b & 0x01) != 0,
104 (b & 0x02) != 0,
105 (b & 0x04) != 0,
106 (b & 0x08) != 0,
107 (b & 0x10) != 0,
108 (b & 0x20) != 0,
109 (b & 0x40) != 0,
110 (b & 0x80) != 0};
111 }
112};
113
116 std::vector<std::uint64_t> uuid;
117 std::uint32_t handle = 0;
118 std::uint32_t properties = 0;
119 std::vector<BleGattDescriptor> descriptors;
120
122 [[nodiscard]] BleGattCharProperties props() const {
124 }
125};
126
129 std::vector<std::uint64_t> uuid;
130 std::uint32_t handle = 0;
131 std::vector<BleGattCharacteristic> characteristics;
132};
133
137 std::uint64_t address = 0;
138 std::vector<BleGattService> services;
139};
140
143 std::uint64_t address = 0;
144 std::uint32_t handle = 0;
145 std::int32_t error = 0;
146};
147
150 std::uint64_t address = 0;
151 std::uint32_t handle = 0;
152 bool ok = false;
153 std::string data;
154 std::int32_t error = 0;
155};
156
159 std::uint64_t address = 0;
160 std::uint32_t handle = 0;
161 bool ok = false;
162 std::int32_t error = 0;
163};
164
167 std::uint64_t address = 0;
168 bool ok = false;
169 std::vector<BleGattService> services;
170 std::int32_t error = 0;
171};
172
175class BluetoothProxy : public Subsystem {
176public:
177 using AdvertisementHandler = std::function<void(const BleAdvertisement&)>;
178 using RawAdvertisementHandler = std::function<void(const std::vector<BleRawAdvertisement>&)>;
179 using ScannerStateHandler = std::function<void(const BleScannerState&)>;
180 using ConnectionHandler = std::function<void(const BleConnection&)>;
181 using ConnectionsFreeHandler = std::function<void(const BleConnectionsFree&)>;
182 using GattReadHandler = std::function<void(const BleGattRead&)>;
183 using GattNotifyDataHandler = std::function<void(const BleGattNotifyData&)>;
184 using GattServicesHandler = std::function<void(const BleGattServices&)>;
185 using GattServicesDoneHandler = std::function<void(std::uint64_t address)>;
186 using GattErrorHandler = std::function<void(const BleGattError&)>;
187 using ReadResultHandler = std::function<void(const BleGattReadResult&)>;
188 using WriteResultHandler = std::function<void(const BleGattWriteResult&)>;
189 using ServicesResultHandler = std::function<void(const BleGattServicesResult&)>;
190
191 explicit BluetoothProxy(Client& client) : Subsystem(client) {}
192
193 // --- Scanning ---------------------------------------------------------
194
197 void subscribe_advertisements(AdvertisementHandler handler, std::uint32_t flags = 0);
198
201
204
207
210
211 // --- Connections ------------------------------------------------------
212
215 void request_connection(std::uint64_t address, bool with_cache = true) const;
216
218 void disconnect(std::uint64_t address) const;
219
222
225
228
229 // --- GATT (one-shot, request/response) --------------------------------
230 // These correlate the device's response to your call and deliver it (or a
231 // GATT error) to `done`. Drive the event loop to receive it.
232
234 void read(std::uint64_t address, std::uint32_t handle, ReadResultHandler done);
235
237 void write(std::uint64_t address,
238 std::uint32_t handle,
239 const std::string& data,
240 bool response,
241 WriteResultHandler done);
242
244 void get_services(std::uint64_t address, ServicesResultHandler done);
245
246 // --- GATT (raw / advanced, fire-and-forget) ---------------------------
247 // Send the request and observe responses via the on_* streaming callbacks.
248
250 void read(std::uint64_t address, std::uint32_t handle) const;
251
253 void write(std::uint64_t address,
254 std::uint32_t handle,
255 const std::string& data,
256 bool response = true) const;
257
259 void notify(std::uint64_t address, std::uint32_t handle, bool enable) const;
260
262 void get_services(std::uint64_t address) const;
263
266
269
272
275
278
279private:
282 void ensure_gatt_handlers();
283 static std::uint64_t gatt_key(const std::uint64_t address, const std::uint32_t handle) {
284 return (address << 16) | (handle & 0xFFFFU);
285 }
286
287 AdvertisementHandler advertisement_handler_;
288 RawAdvertisementHandler raw_advertisement_handler_;
289 ScannerStateHandler scanner_state_handler_;
290 ConnectionHandler connection_handler_;
291 ConnectionsFreeHandler connections_free_handler_;
292 GattReadHandler read_handler_;
293 GattNotifyDataHandler notify_data_handler_;
294 GattServicesHandler services_handler_;
295 GattServicesDoneHandler services_done_handler_;
296 GattErrorHandler error_handler_;
297
298 bool gatt_registered_ = false;
299 std::unordered_map<std::uint64_t, std::vector<ReadResultHandler>> pending_reads_;
300 std::unordered_map<std::uint64_t, std::vector<WriteResultHandler>> pending_writes_;
301 struct ServicesAccumulator {
302 std::vector<BleGattService> services;
303 std::vector<ServicesResultHandler> handlers;
304 };
305 std::unordered_map<std::uint64_t, ServicesAccumulator> pending_services_;
306};
307
308} // namespace esphome::api
Scans for, connects to, and talks GATT to BLE devices through the device's Bluetooth proxy.
Definition bluetooth_proxy.hpp:175
std::function< void(const BleGattRead &)> GattReadHandler
Definition bluetooth_proxy.hpp:182
void notify(std::uint64_t address, std::uint32_t handle, bool enable) const
Enable or disable notifications for a GATT characteristic.
std::function< void(const BleScannerState &)> ScannerStateHandler
Definition bluetooth_proxy.hpp:179
void write(std::uint64_t address, std::uint32_t handle, const std::string &data, bool response, WriteResultHandler done)
Write a GATT characteristic value; done fires once with the result.
void read(std::uint64_t address, std::uint32_t handle) const
Read a GATT characteristic value (response via on_read / on_error).
void on_connections_free(ConnectionsFreeHandler handler)
Register a callback for free-connection-slot updates.
void subscribe_connections_free() const
Subscribe to the number of free connection slots.
void unsubscribe_advertisements() const
Stop receiving BLE advertisements.
void write(std::uint64_t address, std::uint32_t handle, const std::string &data, bool response=true) const
Write a GATT characteristic value. response requests a write response.
std::function< void(const BleGattNotifyData &)> GattNotifyDataHandler
Definition bluetooth_proxy.hpp:183
void on_error(GattErrorHandler handler)
Register a callback for GATT operation errors.
void request_connection(std::uint64_t address, bool with_cache=true) const
Ask the proxy to connect to address.
void on_raw_advertisements(RawAdvertisementHandler handler)
Subscribe to raw (unparsed) BLE advertisement batches.
void on_services_done(GattServicesDoneHandler handler)
Register a callback fired when the GATT service table is fully sent.
void on_services(GattServicesHandler handler)
Register a callback for batches of GATT services.
std::function< void(const BleGattServicesResult &)> ServicesResultHandler
Definition bluetooth_proxy.hpp:189
std::function< void(const BleAdvertisement &)> AdvertisementHandler
Definition bluetooth_proxy.hpp:177
std::function< void(const BleConnectionsFree &)> ConnectionsFreeHandler
Definition bluetooth_proxy.hpp:181
void get_services(std::uint64_t address, ServicesResultHandler done)
Discover the full GATT service table; done fires once when complete.
std::function< void(const std::vector< BleRawAdvertisement > &)> RawAdvertisementHandler
Definition bluetooth_proxy.hpp:178
std::function< void(const BleGattServices &)> GattServicesHandler
Definition bluetooth_proxy.hpp:184
void subscribe_advertisements(AdvertisementHandler handler, std::uint32_t flags=0)
Subscribe to parsed BLE advertisements.
std::function< void(const BleConnection &)> ConnectionHandler
Definition bluetooth_proxy.hpp:180
std::function< void(std::uint64_t address)> GattServicesDoneHandler
Definition bluetooth_proxy.hpp:185
void on_scanner_state(ScannerStateHandler handler)
Register a callback for scanner state changes.
void read(std::uint64_t address, std::uint32_t handle, ReadResultHandler done)
Read a GATT characteristic value; done fires once with the result.
void on_read(GattReadHandler handler)
Register a callback for characteristic read results.
void get_services(std::uint64_t address) const
Request the GATT service table (responses via on_services / on_services_done).
std::function< void(const BleGattError &)> GattErrorHandler
Definition bluetooth_proxy.hpp:186
BluetoothProxy(Client &client)
Definition bluetooth_proxy.hpp:191
void disconnect(std::uint64_t address) const
Disconnect from address.
void on_notify_data(GattNotifyDataHandler handler)
Register a callback for characteristic notification payloads.
std::function< void(const BleGattWriteResult &)> WriteResultHandler
Definition bluetooth_proxy.hpp:188
void set_scanner_mode(BluetoothScannerMode mode) const
Set the scanner mode (passive or active).
std::function< void(const BleGattReadResult &)> ReadResultHandler
Definition bluetooth_proxy.hpp:187
void on_connection(ConnectionHandler handler)
Register a callback for connection state changes.
Async client for a single device.
Definition client.hpp:38
Shared base for the subsystems exposed by Client (log streaming, the various proxies,...
Definition subsystem.hpp:18
Typed mirrors of the ESPHome protobuf enums.
Definition bytes.hpp:10
BluetoothScannerState
Mirror of proto enum BluetoothScannerState.
Definition enums.hpp:282
BluetoothScannerMode
Mirror of proto enum BluetoothScannerMode.
Definition enums.hpp:292
A parsed BLE advertisement (api: BluetoothLEAdvertisementResponse).
Definition bluetooth_proxy.hpp:30
std::int32_t rssi
Definition bluetooth_proxy.hpp:33
std::string name
Definition bluetooth_proxy.hpp:32
std::vector< BleManufacturerData > manufacturer_data
Definition bluetooth_proxy.hpp:37
std::vector< BleServiceData > service_data
Definition bluetooth_proxy.hpp:35
std::uint64_t address
Definition bluetooth_proxy.hpp:31
std::vector< std::string > service_uuids
Definition bluetooth_proxy.hpp:36
std::uint32_t address_type
Definition bluetooth_proxy.hpp:34
Connection state update for a device (api: BluetoothDeviceConnectionResponse).
Definition bluetooth_proxy.hpp:56
std::uint64_t address
Definition bluetooth_proxy.hpp:57
std::uint32_t mtu
Definition bluetooth_proxy.hpp:59
std::int32_t error
Definition bluetooth_proxy.hpp:60
bool connected
Definition bluetooth_proxy.hpp:58
Number of free connection slots (api: BluetoothConnectionsFreeResponse).
Definition bluetooth_proxy.hpp:64
std::vector< std::uint64_t > allocated
Definition bluetooth_proxy.hpp:67
std::uint32_t free
Definition bluetooth_proxy.hpp:65
std::uint32_t limit
Definition bluetooth_proxy.hpp:66
Typed view of the standard BLE GATT characteristic property bits.
Definition bluetooth_proxy.hpp:92
static BleGattCharProperties from_bits(const std::uint32_t b)
Definition bluetooth_proxy.hpp:102
bool indicate
0x20
Definition bluetooth_proxy.hpp:98
bool write
0x08
Definition bluetooth_proxy.hpp:96
bool extended_properties
0x80
Definition bluetooth_proxy.hpp:100
bool read
0x02
Definition bluetooth_proxy.hpp:94
bool write_without_response
0x04
Definition bluetooth_proxy.hpp:95
bool broadcast
0x01
Definition bluetooth_proxy.hpp:93
bool notify
0x10
Definition bluetooth_proxy.hpp:97
bool authenticated_signed_writes
0x40
Definition bluetooth_proxy.hpp:99
One characteristic within a GATT service.
Definition bluetooth_proxy.hpp:115
std::vector< std::uint64_t > uuid
Definition bluetooth_proxy.hpp:116
std::vector< BleGattDescriptor > descriptors
Definition bluetooth_proxy.hpp:119
BleGattCharProperties props() const
Typed view of properties.
Definition bluetooth_proxy.hpp:122
std::uint32_t properties
Raw property bitmask (advanced).
Definition bluetooth_proxy.hpp:118
std::uint32_t handle
Definition bluetooth_proxy.hpp:117
One descriptor within a GATT characteristic.
Definition bluetooth_proxy.hpp:86
std::vector< std::uint64_t > uuid
Definition bluetooth_proxy.hpp:87
std::uint32_t handle
Definition bluetooth_proxy.hpp:88
A GATT operation error (api: BluetoothGATTErrorResponse).
Definition bluetooth_proxy.hpp:142
std::uint64_t address
Definition bluetooth_proxy.hpp:143
std::int32_t error
Definition bluetooth_proxy.hpp:145
std::uint32_t handle
Definition bluetooth_proxy.hpp:144
Notification payload from a GATT characteristic (api: BluetoothGATTNotifyDataResponse).
Definition bluetooth_proxy.hpp:79
std::uint64_t address
Definition bluetooth_proxy.hpp:80
std::string data
Definition bluetooth_proxy.hpp:82
std::uint32_t handle
Definition bluetooth_proxy.hpp:81
Result of a one-shot GATT read: either data (ok) or error (!ok).
Definition bluetooth_proxy.hpp:149
std::string data
Definition bluetooth_proxy.hpp:153
std::uint32_t handle
Definition bluetooth_proxy.hpp:151
std::int32_t error
Definition bluetooth_proxy.hpp:154
std::uint64_t address
Definition bluetooth_proxy.hpp:150
bool ok
Definition bluetooth_proxy.hpp:152
Result of a GATT characteristic read (api: BluetoothGATTReadResponse).
Definition bluetooth_proxy.hpp:71
std::uint64_t address
Definition bluetooth_proxy.hpp:72
std::uint32_t handle
Definition bluetooth_proxy.hpp:73
std::string data
Definition bluetooth_proxy.hpp:74
One GATT service exposed by a device.
Definition bluetooth_proxy.hpp:128
std::vector< std::uint64_t > uuid
Definition bluetooth_proxy.hpp:129
std::vector< BleGattCharacteristic > characteristics
Definition bluetooth_proxy.hpp:131
std::uint32_t handle
Definition bluetooth_proxy.hpp:130
Result of a one-shot GATT service discovery (all batches collected).
Definition bluetooth_proxy.hpp:166
std::uint64_t address
Definition bluetooth_proxy.hpp:167
std::vector< BleGattService > services
Definition bluetooth_proxy.hpp:169
std::int32_t error
Definition bluetooth_proxy.hpp:170
bool ok
Definition bluetooth_proxy.hpp:168
A batch of GATT services for a device (api: BluetoothGATTGetServicesResponse).
Definition bluetooth_proxy.hpp:136
std::vector< BleGattService > services
Definition bluetooth_proxy.hpp:138
std::uint64_t address
Definition bluetooth_proxy.hpp:137
Result of a one-shot GATT write.
Definition bluetooth_proxy.hpp:158
std::uint32_t handle
Definition bluetooth_proxy.hpp:160
std::int32_t error
Definition bluetooth_proxy.hpp:162
std::uint64_t address
Definition bluetooth_proxy.hpp:159
bool ok
Definition bluetooth_proxy.hpp:161
Manufacturer-specific data blob attached to an advertisement.
Definition bluetooth_proxy.hpp:24
std::string data
Definition bluetooth_proxy.hpp:26
std::string uuid
Definition bluetooth_proxy.hpp:25
A single raw, unparsed BLE advertisement (api: BluetoothLERawAdvertisement).
Definition bluetooth_proxy.hpp:41
std::uint32_t address_type
Definition bluetooth_proxy.hpp:44
std::int32_t rssi
Definition bluetooth_proxy.hpp:43
std::uint64_t address
Definition bluetooth_proxy.hpp:42
std::string data
Definition bluetooth_proxy.hpp:45
State of the device's BLE scanner (api: BluetoothScannerStateResponse).
Definition bluetooth_proxy.hpp:49
BluetoothScannerMode configured_mode
Definition bluetooth_proxy.hpp:52
BluetoothScannerMode mode
Definition bluetooth_proxy.hpp:51
BluetoothScannerState state
Definition bluetooth_proxy.hpp:50
Service or manufacturer data blob attached to an advertisement.
Definition bluetooth_proxy.hpp:18
std::string data
Definition bluetooth_proxy.hpp:20
std::string uuid
Definition bluetooth_proxy.hpp:19
Common base for the Client-owned subsystems.