[lightning-ln882h] Add BLE SDK support#382
Open
Bl00d-B0b wants to merge 1 commit into
Open
Conversation
3064d82 to
4068230
Compare
b547219 to
1e9d45f
Compare
242ebd3 to
146963b
Compare
3 tasks
3a96f5a to
40865df
Compare
Bl00d-B0b
added a commit
to Bl00d-B0b/libretiny
that referenced
this pull request
Jun 9, 2026
c9ae8d8 to
c0df07c
Compare
Wire the LN882H BLE 5.1 stack into the LibreTiny build, gated on CFG_SUPPORT_BLE
(default off; enable via custom_options.proj_config: "CFG_SUPPORT_BLE=1").
- proj_config.h: add CFG_SUPPORT_BLE (default 0)
- builder/utils/config.py, env.py: env_load_config parser fix + proj_config
custom_options alias (so CFG_SUPPORT_BLE can be set from user config)
- builder/family/lightning-ln882h.py: ln882h_ble library (16 SDK sources +
libln882h_ble_full_stack.a), default ble_app_user_cfg.h
- cores/lightning-ln882h/ble/ble_app_user_cfg.h: observer-role default config
- cores/lightning-ln882h/base/api/lt_ble.{c,h}: lt_ble_mac_get() derives a
unique BLE MAC from the WiFi STA MAC (low-24 NIC + 1, MSB-first), returning
false if the WiFi MAC is unavailable; the SDK only provides a fixed placeholder
32585e6 to
f0f306d
Compare
Bl00d-B0b
added a commit
to Bl00d-B0b/esphome
that referenced
this pull request
Jun 12, 2026
Adds BLE support for the LN882H (LibreTiny lightning-ln882h) with the same functionality, options and defaults as esp32_ble_tracker + bluetooth_proxy (passive mode): - ln882h_ble_tracker hub: scan_parameters (interval/window/duration/active/ continuous), start_scan/stop_scan actions, on_ble_advertise / on_ble_service_data_advertise / on_ble_manufacturer_data_advertise / on_scan_end triggers; esp32_ble_tracker-compatible ESPBTDevice/listener API so existing BLE sensor platforms compile unchanged. - Bluetooth proxy (raw advertisements, batched 8/message, scanner state+mode reporting, HA runtime mode switching). - Bluedroid-style adv + scan-response merging: scannable advertisements are held briefly and merged with their scan response into one frame for both local listeners and the proxy (ESP-IDF does this inside its stack on ESP32). - Sensors: ble_presence, ble_rssi, ble_scanner, bthome_mithermometer (incl. IRK resolution and AES-CCM encrypted BTHome via mbedtls). - BLE MAC derived from the WiFi STA MAC by LibreTiny's lt_ble_mac_get() (libretiny-eu/libretiny#382); persisted via the SDK's ln_kv_ble_addr_store. - LN882H specifics: RSSI sign fix (controller intermittently reports the value negated; raw > 20 is recovered by negation — the only RSSI handling, values are otherwise forwarded as-is like ESP32), scan watchdog (30 scan cycles) for WiFi/BLE coexistence recovery on the shared single core. Requires libretiny-eu/libretiny#382 (BLE SDK build support).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
LN882H is a WiFi+BLE combo SoC — every chip variant has BLE hardware, but the BLE stack was not wired into the LibreTiny build system. This PR adds it using the same
queue.AddLibrary()pattern used by the rest oflightning-ln882h.py, and gates compilation on aCFG_SUPPORT_BLEflag so WiFi-only builds stay lean.Changes
cores/lightning-ln882h/base/config/proj_config.h#define CFG_SUPPORT_BLE 0(default off; enable viacustom_options.proj_config: "CFG_SUPPORT_BLE=1").builder/utils/config.py—env_load_configparser fixsplit(None, 2)→split(None, 1)+ skip bare defines (include guards, macros with no value). The old code crashed on#define _PROJ_CONFIG_H_and on multi-word values like((size_t)(160 * 1024)).builder/utils/env.py—proj_configcustom_options alias"proj_config": "proj_config.h"to the headers dict inenv_parse_custom_options, so builds can enable BLE viacustom_options.proj_config: "CFG_SUPPORT_BLE=1".builder/family/lightning-ln882h.pyenv.LoadConfig("$FAMILY_DIR/base/config/proj_config.h")(same pattern as beken-72xx.py).custom_options.proj_configuser overrides intoenv["CONFIG"]soenv.Cfg("CFG_SUPPORT_BLE")reflects the override at build time.ln882h_blelibrary block, active whenenv.Cfg("CFG_SUPPORT_BLE"):$SDK_DIR/components/ble/: arch init, port glue, profiles common, GAP (scan/misc/advertising), GATT common, connection/device manager, SMP, store, event, import — 16 C files total.+<.>(base_dir itself, required so sources using#include "ble_arch/arch.h"resolve) plus all BLE SDK subdirectory paths.ble_app_user_cfg.hatcores/lightning-ln882h/ble/, exposed viaAppendPublic(CPPPATH). Applications that prepend their own include path shadow this default automatically.libln882h_ble_full_stack.alinked via-Wl,--whole-archive.CFG_SUPPORT_BLE=1added to public CPPDEFINES.cores/lightning-ln882h/base/api/lt_ble.{c,h}— unique BLE MACThe BLE SDK has no factory BLE address of its own —
ln_kv_ble_pub_addr_get()returns the fixed placeholderBLE_DEFAULT_PUBLIC_ADDR(00:50:C2:00:00:00) on a blank device, identical on every chip. New platform helperlt_ble_mac_get(uint8_t out[6])derives a unique address from the WiFi STA MAC: low 24-bit NIC + 1 (wraparound, OUI unchanged), MSB-first. This reproduces theBLE = WiFi + 1pairing confirmed across the Tuya LN882H flash dumps (#381) without reading the Tuya KV, so it works on any board. Consumers (e.g. esphome/esphome#16691) call it, persist via the SDK'sln_kv_ble_addr_store()("2_ble_addr"), and pass it torw_init().Flash / RAM impact
libln882h_ble_full_stack.ais compiled with-ffunction-sections. Because--gc-sectionsis always active, functions not reachable from the entry point are stripped. The overhead depends on what the application calls — measured on real hardware (DS-101JL / generic-ln882hki, ESPHome 2026.5.3, LibreTiny 1.12.1), identical config differing only by the BLE layer:CFG_SUPPORT_BLE=0)CFG_SUPPORT_BLE=1, no tracker)A build that actively uses the BLE stack will retain all scanner/GAP/GATT code it calls — the savings from disabling BLE in that case will be correspondingly larger.
Related
esphome/esphome#16691 adds LN882H BLE scanner support and depends on this PR.