ESPHome 2026.8.0b4
Loading...
Searching...
No Matches
ln882h_ble_tracker.h
Go to the documentation of this file.
1// BLE scanner for LN882H: implements ble_device_base::BLEHub on top of the
2// ln882h_ble controller (which owns all SDK calls and delivers scan reports on
3// the main task). Scan policy lives here: parameters, period timers with
4// per-period restart, and the adv+scan-response merge.
5
6#pragma once
7
8#ifdef USE_LIBRETINY
9
16
17#include <cstdint>
18
19#ifdef USE_OTA_STATE_LISTENER
21#endif
22
24
25// ---------------------------------------------------------------------------
26// LN882HBLETracker
27// ---------------------------------------------------------------------------
28
30 public Parented<ln882h_ble::LN882HBLE>,
32#ifdef USE_OTA_STATE_LISTENER
33 ,
35#endif
36{
37 public:
38 // ---- ESPHome Component ----
39 void setup() override;
40 void loop() override;
41 void dump_config() override;
42 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
43
44#ifdef USE_OTA_STATE_LISTENER
45 // Pause scanning while an OTA update runs (single-core WiFi/BLE/flash contention);
46 // mirrors esp32_ble_tracker.
47 void on_ota_global_state(ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) override;
48#endif
49
50 // ---- YAML configuration setters ----
52 void set_scan_interval(uint16_t scan_interval) { this->scan_interval_ = scan_interval; }
53 void set_scan_window(uint16_t scan_window) { this->scan_window_ = scan_window; }
54 void set_scan_duration(uint32_t scan_duration) { this->scan_duration_ = scan_duration; }
65 bool scan_continuous() const { return this->scan_continuous_; }
71
72 // ---- Public scan control ----
73 // Mirrors esp32_ble_tracker: set_scan_continuous() + start_scan() / stop_scan().
74 void start_scan();
75 void stop_scan();
76
77 // ---- ble_device_base::BLEHub contract ----
85 // The LN882H controller supports active scanning; adv + scan response arrive
86 // as separate reports and are merged by this tracker (Bluedroid semantics).
87 // The SDK's GATT client is not exposed.
88 // scan_mode_switch: request_scan_mode() is implemented (restart-if-running).
89 return {.active_scan = true, .merges_scan_response = true, .gatt = false, .scan_mode_switch = true};
90 }
91 // The controller stores the address LSB-first (BLE convention); the contract
92 // wants printable (MSB-first) order.
93 void get_adapter_mac(uint8_t out[MAC_ADDRESS_SIZE]) {
94 uint8_t mac[MAC_ADDRESS_SIZE];
95 this->parent_->get_mac_lsb_first(mac);
96 for (int i = 0; i < 6; i++)
97 out[i] = mac[5 - i];
98 }
99 bool scan_running() { return this->scan_running_; }
100 bool scan_active() { return this->scan_active_; }
101 bool request_scan_mode(bool active);
102
103 // ---- ln882h_ble::BLEScanListener ----
104 // Delivered by the controller's loop() on the ESPHome main task — the
105 // rw-task → main-task handoff already happened in the controller's queue.
106 // Demultiplexes advertisements vs scan responses and drives the merge.
107 void on_scan_report(const ln882h_ble::BLEScanReport &report) override;
108
109 protected:
110 void start_scan_();
111 void stop_scan_();
112 // Close a scan period: flush held advertisements (unmerged) BEFORE
113 // on_scan_end fires, then re-anchor the period clock to `now`.
114 void end_scan_period_(uint32_t now);
115
116 bool scan_running_{false};
117 bool scan_active_{false};
118 // Defaults are the LN882H SDK's recommended scan parameters
119 // (ln_ble_scan.h: SCAN_INTERVAL_DEF 0xA0, SCAN_WINDOW_DEF 0x50 → 50 % duty).
120 // uint16_t matches the controller's scan_start() parameters.
121 uint16_t scan_interval_{160}; // 160 × 0.625 ms = 100 ms (SDK SCAN_INTERVAL_DEF)
122 uint16_t scan_window_{80}; // 80 × 0.625 ms = 50 ms (SDK SCAN_WINDOW_DEF; 50/100 = 50 %)
125 bool pending_start_{false}; // start_scan() latched before the controller's setup()
126 bool scan_continuous_configured_{true}; // YAML value; stop_scan() must not lose it
127#ifdef USE_OTA_STATE_LISTENER
128 bool scan_continuous_before_ota_{false}; // continuous mode saved at OTA start, restored on OTA failure
129 bool scan_running_before_ota_{false}; // one-shot scan running at OTA start, restarted on OTA failure
130#endif
132
133 // Shared adv + scan-response merge and frame dispatch (ble_device_base).
134 // All calls run on the main task (the controller queue already crossed
135 // tasks); the merger is clocked by millis() throughout this tracker.
138
139 uint32_t scan_period_start_{0}; // millis() at start of current scan period; used to rate-limit on_scan_end()
140};
141
142} // namespace esphome::ln882h_ble_tracker
143
144#endif // USE_LIBRETINY
Helper class to easily give an object a parent of type T.
Definition helpers.h:1907
The delivery half of a split-report tracker, shared so the dispatch contract (raw-callback ordering,...
void set_raw_advertisement_callback(RawAdvertisementCallback callback)
void register_listener(ESPBTDeviceListener *listener)
Consumer interface for controller scan reports.
Definition ln882h_ble.h:48
void get_mac_lsb_first(uint8_t out[6]) const
Controller BLE address in the SDK's ln_bd_addr_t order: least-significant octet first (the BLE/HCI co...
void set_raw_advertisement_callback(ble_device_base::RawAdvertisementCallback callback)
void get_adapter_mac(uint8_t out[MAC_ADDRESS_SIZE])
void register_listener(ble_device_base::ESPBTDeviceListener *listener)
void set_scan_continuous(bool scan_continuous)
Runtime control (esp32_ble_tracker lambda parity): does not change the configured value,...
ble_device_base::ScanResponseMerger merger_
void set_configured_continuous(bool scan_continuous)
Set from YAML (scan_parameters.continuous); also the value configured_continuous() reports and a bare...
static constexpr ble_device_base::HubCapabilities get_capabilities()
void restart_scan_duration()
Re-anchor the one-shot duration clock of a running scan to now — used when an action changes the scan...
void on_scan_report(const ln882h_ble::BLEScanReport &report) override
void on_ota_global_state(ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) override
Listener interface for global OTA state changes (includes OTA component pointer).
bool state
Definition fan.h:2
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.h:55
static void uint32_t
What a tracker's controller/SDK can do — consumers branch on data, not #ifdefs.
Definition ble_hub.h:73
Subscriber slot for the raw-advertisement stream (the bluetooth_proxy path).
Definition ble_hub.h:43
One scan report from the controller, decoded from the SDK's rw-task event (RSSI already sign-correcte...
Definition ln882h_ble.h:25