Skip to content

bt: keep the BLE trainer link stable with a headtracker - #5

Merged
Amigache merged 5 commits into
EdgeTX:developfrom
Amigache:fix/ht-reset
Nov 26, 2026
Merged

Amigache merged 5 commits into
EdgeTX:developfrom
Amigache:fix/ht-reset

Conversation

@Amigache

@Amigache Amigache commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

bt: keep the BLE trainer link stable with a headtracker

Summary

Four fixes for the central (trainer master) path, found while validating the
headtracker reset (EdgeTX/edgetx#8136) end to end with a BLE headtracker inside
a TX16S MKII.

  1. Wrong CCCD subscribed. Descriptor discovery runs from the trainer value
    handle to the end of the service, and every 0x2902 in that range
    overwrote s_cccd_handle. The last descriptor in the service belongs to the
    override-channel characteristic (0xAFF1), so the module enabled
    notifications there instead of on the trainer characteristic (0xFFF6).
  2. AT+HTRESET silently stopped the trainer passthrough.
    parserATCommand() sets bt_client_forwardHt = false for every AT command
    and only AT+ROLE1/AT+CON set it back to true, so a reset paused the
    BLE -> UART forwarding until the trainer mode was toggled.
  3. A connection attempt could wedge the state machine.
    bt_client_connect() needs the peer address type, which is only known for
    devices kept in the last scan list. With an unknown address it returned
    silently, but the state machine had already moved to WAITING_CONNECTION,
    which only leaves on that state's scan-complete flag. With no scan running
    the module waited forever and the radio kept waiting for a Connected: line.
  4. The UART stream was overloaded. A headtracker also notifies its override
    channels as a block of zeros between frames (some append padding too), and
    the module re-announced Connected:/MTU/PHY/Board every 2 s forever.
    That came to about 2.5 kB/s plus a ~121 B text burst every 2 s, while EdgeTX
    drains its 256-byte Bluetooth FIFO once per 50 ms and declares the trainer
    lost after one second without a valid frame. The radio reported "trainer
    signal lost" and "recovered" in a loop while the BLE link was up the whole
    time (the headtracker's LED stayed solid and the module never disconnected).

Symptom (before the fix)

  • With the headtracker already on: the link connects, works, and then the radio
    starts announcing trainer lost/recovered every couple of seconds.
  • Turning the trainer mode off/on "fixed" it because it restarted the
    handshake and the passthrough.
  • Powering the radio on with the headtracker off and turning the tracker on
    later sometimes never connected until the trainer mode was toggled.

Fix

  • bt_client.c: only accept the CCCD whose characteristic is the trainer
    (chr_val_handle == s_data_handle) and clear s_cccd_handle on every
    connect.
  • terminal.c: resume forwarding after AT+HTRESET, since the link itself is
    not touched.
  • bt_client.c/terminal.c: bt_client_connect() reports whether an attempt
    started; when it did not, the state machine rescans through the auto-connect
    path. WAITING_CONNECTION gives up after 10 s (CONNECT_TIMEOUT_US) on a
    directed attempt, so an absent peer can no longer wedge the link.
  • bt_client.c: forward only the 0x7E-framed part of each notification,
    dropping padding and the zeroed override block.
  • terminal.c: re-announce Connected: at most ANNOUNCE_REPEATS (3) times
    per link instead of every 2 s forever.

No protocol change: the AT responses and the trainer wire format are unchanged.

Testing

  • pio test -e native: 23/23.
  • Clean builds: C3SuperMini, C3SuperMini_nimble, ESP32_DEV38P,
    ESP32_DEV38P_nimble.
  • Hardware: ESP32-C3 SuperMini (NimBLE) in the TX16S MKII bay, dlktdr headtracker
    on stock 2.40, EdgeTX 2.12.5 for the link tests.
    • Stream at the module TX after the filter: 72 frames/s, max gap 85 ms over
      20 s, ~1.3 kB/s (was 2.5 kB/s with 14 zero bytes between frames).
    • Headtracker off -> on with the radio running: reconnects in ~2 s with no
      manual trainer toggle.
    • AT+HTRESET from an EdgeTX Special Function: instant recenter, stream
      uninterrupted.
    • No module resets during a radio session (rst:0x1 POWERON only when the
      radio powers it).
  • doc/BENCH.md and README.md updated with the measured values.

Notes

  • The AT+STATUS handler on the port branch needs the same forwardHt
    restore; it is not part of this PR because AT+STATUS does not exist in
    develop.
  • If a later change adds more AT commands that can arrive while a trainer link
    is active, they should follow the same rule: either leave the passthrough
    running or restore bt_client_forwardHt explicitly.
  • The EdgeTX side (Special Function HT Reset on a momentary switch) is
    feat: add a special function to reset the BLE headtracker edgetx#8136.

Descriptor discovery runs from the trainer value handle to the end of the service, so every 0x2902 in that range overwrote s_cccd_handle and the module enabled the override-channel CCCD (0xAFF1) instead of the trainer one. Keep only the CCCD whose characteristic is the trainer and clear the handle on every connect.
parserATCommand() pauses the trainer passthrough for every AT command and only AT+ROLE1/AT+CON resumed it, so a headtracker reset silently stopped the trainer stream until the link was cycled. Resume forwarding after the command.
bt_client_connect() needs the peer address type, which is only known for devices kept in the last scan list. When the address was unknown (for instance after the headtracker powered off) it returned silently, but the state machine had already moved to WAITING_CONNECTION and only that state's scan-complete flag could leave it. With no scan running, the module waited forever and the radio kept waiting for a Connected: line until the trainer mode was toggled.

Connect now reports whether an attempt started: when it did not, the state machine rescans through the auto-connect path. WAITING_CONNECTION also gives up after 10 s on a directed attempt, so a peer that is off or out of range can no longer wedge the link.
With a headtracker the UART carried about 2.5 kB/s: the trainer frame plus a 14-byte block of zeros (the tracker notifies its override channels between frames) and a full Connected:/MTU/PHY/Board text block every 2 s. EdgeTX drains its 256-byte Bluetooth FIFO once per 50 ms and declares the trainer lost after one second without a valid frame, so that extra traffic made the trainer drop and come back over and over while the BLE link stayed up.

Forward only the 0x7E-framed part of each notification (dropping padding and the zeroed override block) and re-announce the connection at most three times per link instead of forever.
Add the measured headtracker results: boot timing, reconnect after a power cycle, the stream shape before and after dropping the zeroed override notifications, the capped Connected: announcements and the HT reset from an EdgeTX Special Function. Update the AT+CON notes now that an unknown address falls back to scanning.
@Amigache Amigache changed the title Fix/ht reset bt: keep the BLE trainer link stable with a headtracker Nov 26, 2026
@Amigache
Amigache merged commit 7fdbc9d into EdgeTX:develop Nov 26, 2026
2 checks passed
@Amigache
Amigache deleted the fix/ht-reset branch November 26, 2026 20:23
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