12#include <unordered_map>
59 std::uint32_t
mtu = 0;
87 std::vector<std::uint64_t>
uuid;
103 return {(b & 0x01) != 0,
116 std::vector<std::uint64_t>
uuid;
129 std::vector<std::uint64_t>
uuid;
238 std::uint32_t handle,
239 const std::string& data,
250 void read(std::uint64_t address, std::uint32_t handle)
const;
254 std::uint32_t handle,
255 const std::string& data,
256 bool response =
true)
const;
259 void notify(std::uint64_t address, std::uint32_t handle,
bool enable)
const;
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);
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;
305 std::unordered_map<std::uint64_t, ServicesAccumulator> pending_services_;
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.
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.