ESPHome 2026.8.0b4
Loading...
Searching...
No Matches
ble_hub.h
Go to the documentation of this file.
1// ble_hub.h
2//
3// The platform-neutral BLE tracker contract: shared types plus the method
4// surface every tracker provides (documented below). Exactly one tracker
5// exists per build, so BLEHub is a compile-time alias (ble_hub_impl.h), not
6// an abstract interface — no vtable, every hub call inlinable. Consumers
7// include ble_hub_impl.h and bind in YAML via cv.use_id(BLEHub).
8//
9// Chip differences are expressed as data (HubCapabilities), never as
10// platform conditionals in consumers.
11
12#pragma once
13
14#include "ble_device.h"
16
17#include <concepts>
18#include <cstdint>
19
21
27 uint64_t address;
28 const uint8_t *data;
29 uint16_t data_len;
30 int8_t rssi; // signed dBm
31 uint8_t addr_type;
32};
33
44 void *instance{nullptr};
45 void (*fn)(void *instance, const RawAdvertisement &adv){nullptr};
47 bool is_set() const { return this->fn != nullptr; }
48 void invoke(const RawAdvertisement &adv) const { this->fn(this->instance, adv); }
49};
50
53enum class ScannerState : uint8_t {
54 IDLE = 0,
55 STARTING = 1,
56 RUNNING = 2,
57 FAILED = 3,
58 STOPPING = 4,
59 STOPPED = 5,
60};
61
66 void *instance{nullptr};
67 void (*fn)(void *instance, ScannerState state){nullptr};
68 bool is_set() const { return this->fn != nullptr; }
69 void invoke(ScannerState state) const { this->fn(this->instance, state); }
70};
71
91
92// The BLEHub method surface, asserted where ble_hub_impl.h binds the alias.
93// Semantics beyond the signatures:
94// - register_listener: parsed-advertisement consumers (sensors, triggers).
95// - set_raw_advertisement_callback: raw stream, one consumer at a time.
96// - get_adapter_mac: printable order, out[0] = MSB.
97// - scan_active: the current/configured mode sends scan requests.
98// - request_scan_mode: false = cannot honor, state untouched (the caller
99// reports the real state back); true = applied immediately, restarting a
100// running scan. Honoring is advertised by HubCapabilities::scan_mode_switch.
101// Push hubs additionally provide set_scanner_state_callback(ScannerStateCallback)
102// and get_scanner_state() under USE_BLE_SCANNER_STATE_CALLBACK; the concept
103// requires both exactly when that define is set. A push hub must emit a
104// transition for every accepted or refused mode request - consumers skip
105// their own mode report on push builds.
106template<typename T>
107concept BLEHubContract = requires(T hub, ESPBTDeviceListener *listener, RawAdvertisementCallback raw_callback,
108 uint8_t *mac) {
109 hub.register_listener(listener);
110 hub.set_raw_advertisement_callback(raw_callback);
111 { T::get_capabilities() } -> std::same_as<HubCapabilities>;
112 hub.get_adapter_mac(mac);
113 { hub.scan_running() } -> std::same_as<bool>;
114 { hub.scan_active() } -> std::same_as<bool>;
115 { hub.request_scan_mode(true) } -> std::same_as<bool>;
116#ifdef USE_BLE_SCANNER_STATE_CALLBACK
117 hub.set_scanner_state_callback(ScannerStateCallback{});
118 { hub.get_scanner_state() } -> std::same_as<ScannerState>;
119#endif
120};
121
122} // namespace esphome::ble_device_base
bool state
Definition fan.h:2
ScannerState
Scanner lifecycle, wire-value aligned with the api enum so consumers cast directly (pinned by static_...
Definition ble_hub.h:53
What a tracker's controller/SDK can do — consumers branch on data, not #ifdefs.
Definition ble_hub.h:73
bool merges_scan_response
Controller (or tracker) delivers advertisement + scan response as one merged frame.
Definition ble_hub.h:79
bool active_scan
Controller can send scan requests (active scanning).
Definition ble_hub.h:75
bool scan_mode_switch
request_scan_mode() is honored at runtime.
Definition ble_hub.h:89
bool gatt
GATT client connections are available: the platform has a bluetooth_connection backend (rp2 binds the...
Definition ble_hub.h:84
Subscriber slot for the raw-advertisement stream (the bluetooth_proxy path).
Definition ble_hub.h:43
bool is_set() const
A default-constructed slot is "no subscriber"; hubs must guard on this.
Definition ble_hub.h:47
void invoke(const RawAdvertisement &adv) const
Definition ble_hub.h:48
void(* fn)(void *instance, const RawAdvertisement &adv)
Definition ble_hub.h:45
One raw advertisement as delivered by the controller — a borrowed view, valid only for the duration o...
Definition ble_hub.h:24
uint64_t address
Producers convert their native byte order at the emit site, so no byte-order convention crosses this ...
Definition ble_hub.h:27
Subscriber slot for scanner-state transitions; same shape as RawAdvertisementCallback,...
Definition ble_hub.h:65
void invoke(ScannerState state) const
Definition ble_hub.h:69
void(* fn)(void *instance, ScannerState state)
Definition ble_hub.h:67