Skip to content

[lightning-ln882h] Add BLE SDK support#382

Open
Bl00d-B0b wants to merge 1 commit into
libretiny-eu:masterfrom
Bl00d-B0b:feature/ln882h-ble-sdk
Open

[lightning-ln882h] Add BLE SDK support#382
Bl00d-B0b wants to merge 1 commit into
libretiny-eu:masterfrom
Bl00d-B0b:feature/ln882h-ble-sdk

Conversation

@Bl00d-B0b

@Bl00d-B0b Bl00d-B0b commented May 28, 2026

Copy link
Copy Markdown
Contributor

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 of lightning-ln882h.py, and gates compilation on a CFG_SUPPORT_BLE flag so WiFi-only builds stay lean.

Changes

cores/lightning-ln882h/base/config/proj_config.h

  • Adds #define CFG_SUPPORT_BLE 0 (default off; enable via custom_options.proj_config: "CFG_SUPPORT_BLE=1").

builder/utils/config.pyenv_load_config parser fix

  • split(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.pyproj_config custom_options alias

  • Adds "proj_config": "proj_config.h" to the headers dict in env_parse_custom_options, so builds can enable BLE via custom_options.proj_config: "CFG_SUPPORT_BLE=1".

builder/family/lightning-ln882h.py

  • Calls env.LoadConfig("$FAMILY_DIR/base/config/proj_config.h") (same pattern as beken-72xx.py).
  • Merges any custom_options.proj_config user overrides into env["CONFIG"] so env.Cfg("CFG_SUPPORT_BLE") reflects the override at build time.
  • New ln882h_ble library block, active when env.Cfg("CFG_SUPPORT_BLE"):
    • Compiled sources from $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.
    • Include paths: adds +<.> (base_dir itself, required so sources using #include "ble_arch/arch.h" resolve) plus all BLE SDK subdirectory paths.
    • Default ble_app_user_cfg.h at cores/lightning-ln882h/ble/, exposed via AppendPublic(CPPPATH). Applications that prepend their own include path shadow this default automatically.
    • Pre-compiled BLE stack libln882h_ble_full_stack.a linked via -Wl,--whole-archive.
    • CFG_SUPPORT_BLE=1 added to public CPPDEFINES.

cores/lightning-ln882h/base/api/lt_ble.{c,h} — unique BLE MAC

The BLE SDK has no factory BLE address of its own — ln_kv_ble_pub_addr_get() returns the fixed placeholder BLE_DEFAULT_PUBLIC_ADDR (00:50:C2:00:00:00) on a blank device, identical on every chip. New platform helper lt_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 the BLE = WiFi + 1 pairing 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's ln_kv_ble_addr_store() ("2_ble_addr"), and pass it to rw_init().

Flash / RAM impact

libln882h_ble_full_stack.a is compiled with -ffunction-sections. Because --gc-sections is 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:

Build Flash RAM
WiFi-only (CFG_SUPPORT_BLE=0) 530,672 B (43.2%) 97,000 B (32.1%)
BLE SDK, no app code (CFG_SUPPORT_BLE=1, no tracker) 535,616 B (43.6%) 97,440 B (32.3%)
Delta (startup hooks only) +4,944 B (+4.8 KB) +440 B

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.

@Bl00d-B0b Bl00d-B0b marked this pull request as draft May 28, 2026 18:18
@Bl00d-B0b Bl00d-B0b force-pushed the feature/ln882h-ble-sdk branch 2 times, most recently from 3064d82 to 4068230 Compare May 28, 2026 19:00
@Bl00d-B0b Bl00d-B0b marked this pull request as ready for review May 29, 2026 04:31
@Bl00d-B0b Bl00d-B0b force-pushed the feature/ln882h-ble-sdk branch 5 times, most recently from b547219 to 1e9d45f Compare May 29, 2026 05:19
@Bl00d-B0b Bl00d-B0b force-pushed the feature/ln882h-ble-sdk branch 3 times, most recently from 242ebd3 to 146963b Compare June 2, 2026 14:43
@Bl00d-B0b Bl00d-B0b force-pushed the feature/ln882h-ble-sdk branch 2 times, most recently from 3a96f5a to 40865df Compare June 9, 2026 19:18
Bl00d-B0b added a commit to Bl00d-B0b/libretiny that referenced this pull request Jun 9, 2026
@Bl00d-B0b Bl00d-B0b force-pushed the feature/ln882h-ble-sdk branch 3 times, most recently from c9ae8d8 to c0df07c Compare June 9, 2026 19:48
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
@Bl00d-B0b Bl00d-B0b force-pushed the feature/ln882h-ble-sdk branch from 32585e6 to f0f306d Compare June 9, 2026 19:55
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant