mirror of
https://github.com/alexhopeoconnor/firmware.git
synced 2026-10-21 18:52:36 +10:00
* feat: add Nordic nRF54L15-DK variant (Zephyr + BLE + LoRa)
Adds a community hardware variant for the Nordic nRF54L15-DK (PCA10156)
with an external EBYTE E22-900M30S (SX1262) LoRa module. First Meshtastic
port running on the Zephyr RTOS; all other Nordic targets use the nRF5
SoftDevice stack.
Scope
-----
- New Zephyr-based platform layer under src/platform/nrf54l15/ providing
Arduino-compatible shims (Arduino.h, SPI, Wire, Print, Stream) over the
Zephyr APIs plus a LittleFS-backed InternalFileSystem on SPIM20.
- Bluetooth LE peripheral (NRF54L15Bluetooth.*) built on the Zephyr BT
host stack, exposing the Meshtastic GATT service with legacy
connectable advertising, just-works pairing, dynamic MTU exchange
(up to 247 bytes), and iOS connection-parameter tweaks.
- Variant directory variants/nrf54l15/nrf54l15dk/ with pin map for the
E22 module on connector J1, PlatformIO env (nrf54l15dk), Zephyr
DT overlay and a wiring README.
- Zephyr project config (zephyr/prj.conf + board overlay) tuned for
BT + LoRa: 16 KB main stack, 4 KB BT RX thread, RTT logging in
immediate mode, newlib-nano heap sized to leave room for the GATT
pools while still allowing ATT MTU=247.
- extra_scripts/nrf54l15_linker.py works around a PlatformIO + old Ninja
issue where Zephyr's two-pass linker script generation does not run
automatically; the post-script parses build.ninja and invokes the
gcc -E step directly before the final link.
- boards/nrf54l15dk.json board definition (PlatformIO needs it for the
DK; the Seeed platform only ships the XIAO variants).
- variants/rp2350/rp2350.ini excludes platform/nrf54l15/ from RP2350
build_src_filter so the shared platform tree does not leak between
targets.
- .gitignore: add nRF J-Link / RTT debug artifacts (flash.jlink,
rtt_*.txt).
Shared source changes
---------------------
- src/main.{cpp,h}, src/RedirectablePrint.cpp, src/FSCommon.{cpp,h},
src/mesh/{Channels,NodeDB,RadioLibInterface,MeshService,PhoneAPI}.cpp,
src/mesh/RadioLibInterface.h, src/modules/AdminModule.cpp: add small
guards / helpers so the Zephyr build compiles alongside the Arduino
targets. Behavior on existing boards is unchanged.
Hardware model
--------------
HW_VENDOR maps to meshtastic_HardwareModel_PRIVATE_HW until a dedicated
protobuf enum value is assigned upstream. The variant declares
custom_meshtastic_hw_model = 132 so the maintainers can wire the new
enum value through the protobufs repo after merge.
Hardware note
-------------
The E22-900M30S does not connect its DIO2 pin to TXEN internally — a
wire/solder bridge between DIO2 and TXEN on the module is required for
TX to work. Details and full pin map are in the variant README.
Validation
----------
Built clean against develop. On real hardware (April 2026) the port
passes end-to-end: iOS companion app pairs and connects, configuration
round-trip works, LoRa TX/RX reaches a canonical tbeam on the same mesh
channel, NodeDB updates propagate both ways, and traceroute completes.
* fix(nrf54l15): use atomic fs_rename instead of copy fallback
Zephyr LittleFS on nrf54l15 supports fs_rename natively, so route it
through the same atomic path as ESP32. The previous copyFile+remove
fallback truncated the destination before copying, leaving 0-byte files
if interrupted mid-write.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(nrf54l15): expand storage_partition from 36KB to 700KB
LittleFS on the default 9-block (36KB) storage_partition ran out of
space during copy-on-write of config.proto, causing fs_write to return
ENOSPC and pb_encode to surface "io error" when saving configuration
via the mobile app.
Reclaim slot1_partition (the MCUboot secondary slot — unused since we
flash directly via J-Link) and grow storage_partition to span
0xb6000..0x165000 (~175 blocks).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(nrf54l15): drop USERPREFS_LORACONFIG_* so LoRa config stays mutable
NodeDB rewrites LoRa config from USERPREFS_LORACONFIG_* on every boot,
which prevented reconfiguration via the BLE/serial app. Drop the
variant-level defaults; users configure region and modem preset through
the app like every other variant.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(nrf54l15): enforce MITM passkey pairing on GATT service
- Add MESH_PERM_READ/MESH_PERM_WRITE macros (READ_AUTHEN/WRITE_AUTHEN)
on all mesh service characteristics so clients must complete passkey
exchange before accessing fromNum/fromRadio/toRadio/logRadio.
- Wire FIXED_PIN mode to bt_passkey_set() so the device advertises a
known PIN (config.bluetooth.fixed_pin); RANDOM_PIN keeps default
per-pairing random passkey.
- Reduce BleDeferredThread HARD_WATCHDOG_MS from 3min to 1min.
- prj.conf: CONFIG_BT_SMP_ENFORCE_MITM=y, CONFIG_BT_FIXED_PASSKEY=y,
CONFIG_BT_SMP_SC_PAIR_ONLY=n (legacy fallback for clients that abort
SC pairing with reason 0x01 within 150ms).
* fix(nrf54l15): resolve develop-merge conflict + cppcheck warnings
The `Merge branch 'develop'` left two ~RadioLibInterface() declarations
in src/mesh/RadioLibInterface.h: the inline version added upstream by
PR #10254 (which independently applied the same UAF guard this PR was
carrying) and the out-of-line version this PR introduced. GCC rejects
the duplicate, breaking every platform build. Drop the out-of-line
declaration + definition; keep upstream's inline form.
Also silence the 13 cppcheck low warnings introduced by the new
nrf54l15 Arduino shim — Arduino's `String`/`SPISettings` API contract
relies on implicit single-arg constructors used pervasively by
existing Meshtastic code, so suppress `noExplicitConstructor` inline
with a comment instead of breaking the API. The few mechanical wins
(`const tmp[2]`, `const uint32_t *sp`) are applied directly.
* fmt: fix Trunk Check lint issues on nrf54l15-port
- extra_scripts/nrf54l15_linker.py: move regular imports above
Import("env") to silence E402, add trunk-ignore-all(F821) for the
PIO/SCons SConstruct injection (matches esp32_pre.py / nrf52_extra.py
convention)
- src/platform/nrf54l15/NRF54L15Bluetooth.cpp: clang-format 16.0.3
- boards/nrf54l15dk.json + variants/nrf54l15/nrf54l15dk/README.md:
prettier 3.8.3 (also resolves markdownlint MD060 on README tables)
No behavior change.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(nrf54l15): address Copilot review comments + correct clang-format style
Six review threads from the 2026-04-30 Copilot review:
- src/platform/nrf54l15/nrf54l15_main.cpp: validate PSP against the nRF54L15
SRAM range (0x20000000..0x20040000) and 4-byte alignment before walking the
faulting thread's stack, and clamp the walk so it never reads past the end
of RAM. Prevents a second fault inside the fatal handler when PSP is
corrupted (common in real faults).
- src/platform/nrf54l15/nrf54l15_arduino.cpp: gate the bring-up printk traces
in digitalWrite/digitalRead (CS/NRESET toggle log, BUSY-before-NRESET
snapshot, BUSY periodic timeline) behind a new -DNRF54L15_GPIO_DEBUG flag
that is off by default. The "dev NOT READY" message stays unconditional —
it indicates a genuine hardware/DTS misconfig.
- src/modules/AdminModule.cpp: don't mutate config.device.output_gpio_enabled
from handleGetConfig(). Reflect the live pin state in the response payload
only — a getter must not write back to disk-persisted state.
- src/platform/nrf54l15/InternalFileSystem.h: derive totalBytes() from
FIXED_PARTITION_SIZE(storage_partition) at compile time so it tracks the
DK overlay's ~700 KB partition instead of the stale 36 KB hard-coded value.
Updated the file header comment accordingly.
- extra_scripts/nrf54l15_linker.py: make _extract_gcc_command() handle the
POSIX Ninja COMMAND format (no `cmd.exe /C "..."` wrapper) in addition to
the Windows form, so the script doesn't hard-fail on Linux/macOS hosts.
- src/platform/nrf54l15/NRF54L15Bluetooth.cpp: clamp NO_PIN to RANDOM_PIN
with a one-shot LOG_WARN. The mesh GATT permissions are declared with
BT_GATT_PERM_*_AUTHEN and prj.conf sets CONFIG_BT_SMP_ENFORCE_MITM=y, so
NO_PIN with no auth callbacks would leave every characteristic returning
BT_ATT_ERR_AUTHENTICATION. Falling back to RANDOM_PIN keeps the link
usable instead of silently broken. Also re-formatted this file with the
project's .trunk/configs/.clang-format (Linux braces, 4-space indent,
130-col) — the previous lint-fix commit a2aca3234 accidentally used the
default LLVM style here, which CI's clang-format would have rejected.
Build verified: pio run -e nrf54l15dk passes, RAM 47.4%, Flash 28.6%.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(nrf54l15): address remaining Copilot review threads
Round 2/3 review fixes — bugs first, then docs/portability:
BLE concurrency (NRF54L15Bluetooth.cpp):
- onNowHasData / sendLog / BleDeferredThread / shutdown: acquire
active_conn under ble_mutex via new acquire_active_conn() helper so
disconnected_cb can't free the conn between the null check and
bt_conn_ref/bt_gatt_notify (use-after-unref).
- write_toradio: reject writes that exceed MAX_TO_FROM_RADIO_SIZE with
ATT_ERR_INVALID_ATTRIBUTE_LEN instead of returning success and silently
dropping the payload (would hide failed config writes from the phone).
- start_advertising: truncate the device name to fit the 31-byte legacy
scan-response limit and switch to BT_DATA_NAME_SHORTENED so
bt_le_adv_start() doesn't reject the payload when the name approaches
CONFIG_BT_DEVICE_NAME_MAX=32.
Linker / portability:
- main.h: drop the rp2040Loop() forward declaration that had no
definition and no callers — would surface as a link error if any RP2040
build added a call to the symbol.
- nrf54l15_arduino.cpp: transfer16() now uses static __aligned(4) DMA
buffers (matching transfer()), removing the EasyDMA-reachability hazard
of caller-stack buffers on this part.
Filesystem (InternalFileSystem.h):
- usedBytes(): return real usage from fs_statvfs() instead of 0 so OTA
/ range-test free-space guards work.
- rewindDirectory(): close the dir before reopening — Zephyr fs_dir_t has
no rewind, and re-fs_opendir on an open handle leaks LittleFS state.
Crash handler (nrf54l15_main.cpp):
- After the stack walk, busy-wait 50 ms to flush RTT/printk and call
sys_reboot(SYS_REBOOT_COLD) directly so the saved_crash record is
actually reported on the next boot. Default Zephyr config has
RESET_ON_FATAL_ERROR=n, so the previous k_fatal_halt() spun forever.
Generalization / config:
- PhoneAPI.cpp: replace the NRF54L15_DK ifdef with a
MESHTASTIC_EXCLUDE_FILES_MANIFEST capability flag (defined in the
nrf54l15dk env) so future variants can opt in/out without touching
shared code.
- variants/nrf54l15/nrf54l15.ini: parameterize libdeps include paths via
${PIOENV} so additional nRF54L15 envs sharing nrf54l15_base don't break.
- prj.conf: drop the stale "36 KB storage_partition" comments — the DK
overlay reclaims slot1 to ~700 KB and runtime size comes from
FIXED_PARTITION_SIZE.
- nrf54l15dk overlay: remove the zephyr,console / zephyr,shell-uart
chosen entries that conflicted with CONFIG_UART_CONSOLE=n + RTT
console; keep uart30 enabled so swapping the console is one Kconfig
flip away.
Build: nrf54l15dk SUCCESS (flash 28.6%, RAM 47.4%); wiznet_5500_evb_pico2
SUCCESS (verifies the shared main.h change).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(nrf54l15dk): use PRIVATE_HW (255) for custom_meshtastic_hw_model
Per @thebentern's review: the nRF54L15-DK is a development kit, not a
canonical Meshtastic SKU, so it falls under HardwareModel::PRIVATE_HW
(255) — the same enum value already used at runtime via HW_VENDOR. The
placeholder 132 is removed; no dedicated enum number will be assigned
for DK boards. Slug stays NRF54L15_DK as a human-readable identifier.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(nrf54l15): unstarve bt_long_wq so SC pairing completes
bt_pub_key_gen() runs the ECC P256 key generation on bt_long_wq. At default
prio=10 (preemptible) and stack=1400 it gets starved by Meshtastic app
threads at boot — sc_public_key stays NULL for minutes, smp_public_key()
defers with SMP_FLAG_PKEY_SEND, and every SC pairing attempt stalls right
after the public-key exchange. iOS shows "Connecting…" forever with no
PIN prompt; bleak/CLI fails the first CCC notify write with
"Protocol Error 0x05: Insufficient Authentication".
Set CONFIG_BT_LONG_WQ_PRIO=0 (highest preemptible, ties with main) and
CONFIG_BT_LONG_WQ_STACK_SIZE=4096 (margin for the P256M driver frames).
Validated E2E with iOS Meshtastic app: bt_smp_pkey_ready fires within ~40 s
of boot, 20 SC Passkey Entry rounds complete with matching pcnf/cfm,
encrypt 0x01 / sec_level 0x04 (Authenticated MITM), bonded=1.
* feat(nrf54l15): hardware I2C bus via TWIM30 + sensor telemetry
Adds the Arduino TwoWire layer for the nRF54L15-DK so Meshtastic's
sensor drivers can talk to external I2C devices over the hardware
TWIM30 peripheral.
Bus binding:
- &uart30 disabled in the board overlay (peripheral instance 30 is
shared between UARTE30 / TWIM30 / SPIM30 — pick one). Console stays
on RTT via CONFIG_RTT_CONSOLE.
- New i2c30_default / i2c30_sleep pinctrl with SDA=P0.03 / SCL=P0.04.
External 4.7 kOhm pull-ups required on both lines.
- &i2c30 enabled at I2C_BITRATE_FAST (400 kHz).
- button_3 (SW3, P0.04) deleted from DTS so the pad can be claimed by
i2c30 pinctrl; SW3 is still wired to the pad on the DK, do not press
it during I2C use or it will short SCL to GND.
Arduino layer:
- src/platform/nrf54l15/Wire.cpp resolves the DT node at compile time
via DEVICE_DT_GET(DT_NODELABEL(i2c30)) and dispatches Arduino's
beginTransmission / write / endTransmission / requestFrom to
i2c_write / i2c_write_read / i2c_read. Buffer is sized to 256 bytes
for forward compatibility with the SE050 secure element on the
custom PCB.
- Wire.h drops the prior compile-only stubs and exposes the real
TwoWire surface.
- Arduino.h: BitOrder becomes an enum (not #define) so Adafruit_BusIO's
`typedef BitOrder BusIOBitOrder;` compiles.
Variant + build flags:
- nrf54l15.ini flips HAS_WIRE / HAS_SENSOR / HAS_TELEMETRY from 0 to 1
and cherry-picks the sensor libs Meshtastic needs (BusIO, Sensor,
BMP280, BME280, INA219/226/260/3221, SHT4X). The full
environmental_base group is avoided because it pulls
Adafruit_SSD1306 / Adafruit_GFX which rely on Arduino pin macros the
Zephyr shim does not implement.
- nrf54l15dk variant.h defines PIN_WIRE_SDA / PIN_WIRE_SCL for parity
with the Arduino convention used by other variants. The actual bus
wiring is fixed by the overlay pinctrl above.
Validated 2026-05-14/15 on the DK with BMP280 @ 0x76 (temperature +
barometric pressure) and INA3221 @ 0x42 (rail voltage / current);
EnvironmentTelemetry / PowerTelemetry packets transmit successfully
over LoRa.
Footprint cost on nrf54l15dk: +45 KB flash, +1.7 KB RAM.
* feat(nodedb): honor USERPREFS for environment telemetry on first boot
installDefaultConfig() now respects two new compile-time prefs:
USERPREFS_CONFIG_ENV_TELEM_UPDATE_INTERVAL
USERPREFS_CONFIG_ENVIRONMENT_MEASUREMENT_ENABLED
The mobile apps enforce a 30 min floor on environment_update_interval
in the settings UI, which makes short-interval bring-up testing of new
sensor hardware painful — you have to wait half an hour for the first
LoRa packet to confirm wiring + driver. With these prefs baked into
the variant, the firmware can ship a freshly-flashed device that
broadcasts on a shorter cadence (e.g. 900 s) the moment storage_partition
is empty.
Both prefs are gated on #ifdef so the behavior is unchanged for any
variant that does not opt in. Documented in userPrefs.jsonc with the
existing telemetry-interval pref block.
* fix(nrf54l15): allow multiple bonded BLE peers
CONFIG_BT_MAX_PAIRED defaults to 1, so once the first peer (e.g. an
iOS phone) has paired and bonded, every subsequent pairing attempt
from a different MAC fails inside bt_keys_get_addr() with no free
key slot — the host returns BT_SECURITY_ERR_KEY_DOES_NOT_EXIST and
the second peer never gets past SMP.
Raise the slot count to 4 so the device can simultaneously hold an
iOS phone, a Windows host, a Linux host, and one spare bond. Add
BT_KEYS_OVERWRITE_OLDEST so that once the table fills, the LRU peer
is evicted on the next pairing rather than rejecting the new peer.
This matches the behavior other Meshtastic ports already provide
(nRF52 uses CONFIG_BT_PERIPHERAL_PRIO_CONN with similar semantics).
Discovered while bringing up the Python CLI on Windows alongside
the existing iOS bond.
* fix(nrf54l15): zero-initialize TwoWire buffers + clang-format Wire
cppcheck on every CI target (esp32s3, rp2040, rp2350, nrf52840, ...) was
failing the build with two `uninitMemberVar` warnings on TwoWire's
constructor: `txBuf` and `rxBuf` (256-byte arrays) were not initialized.
Even though the buffers are only read after txLen/rxLen is set, leaving
them uninitialized is a footgun if any future caller bypasses the
len-set step. Use C++11 value-initialization in the member initializer
list — costs ~512 B of memset at boot, gains a clean cppcheck pass and
defensive-against-future-changes semantics.
Also reformat Wire.{cpp,h} with the project's `.trunk/configs/.clang-format`
config so the Trunk Check Runner passes — clang-format moved the
`<errno.h>` include before the Zephyr-namespaced ones in Wire.cpp and
collapsed two inline overloads to single lines in Wire.h.
* fix(AdminModule): remove dead OUTPUT_GPIO_PIN/GpioOutputModule references
OUTPUT_GPIO_PIN is never defined and modules/GpioOutputModule.h doesn't
exist in the codebase; all #ifdef OUTPUT_GPIO_PIN branches were dead code
introduced by the nRF54L15-DK variant commit. Strips the include, the
output_gpio_enabled OFF→ON/ON→OFF transition logic in handleSetConfig(),
and the digitalRead() reflection in handleGetConfig().
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
806 lines
33 KiB
C++
806 lines
33 KiB
C++
// NRF54L15Bluetooth.cpp — Zephyr BLE GATT peripheral for Meshtastic nRF54L15
|
|
//
|
|
// GATT profile (identical UUIDs to the nRF52 / NimBLE implementations):
|
|
// Service: 6ba1b218-15a8-461f-9fa8-5dcae273eafd
|
|
// fromNum: ed9da18c-a800-4f66-a670-aa7547e34453 READ | NOTIFY
|
|
// fromRadio: 2c55e69e-4993-11ed-b878-0242ac120002 READ
|
|
// toRadio: f75c76d2-129e-4dad-a1dd-7866124401e7 WRITE
|
|
// logRadio: 5a3d6e49-06e6-4423-9944-e9de8cdf9547 READ | NOTIFY | INDICATE
|
|
//
|
|
// Threading model:
|
|
// - BT RX thread: connected_cb / disconnected_cb / GATT read_/write_
|
|
// callbacks
|
|
// - Meshtastic OSThread scheduler (cooperative, main thread):
|
|
// BleDeferredThread
|
|
// polls pendingToRadio and runs the zombie-connection watchdog every 100 ms
|
|
// - PhoneAPI::onNowHasData: sends fromNum notify synchronously from whichever
|
|
// thread pushed the packet (bt_gatt_notify is thread-safe in Zephyr)
|
|
// - active_conn protected by ble_mutex where needed
|
|
|
|
#include "NRF54L15Bluetooth.h"
|
|
#include "BluetoothCommon.h"
|
|
#include "BluetoothStatus.h"
|
|
#include "PowerFSM.h"
|
|
#include "concurrency/OSThread.h"
|
|
#include "configuration.h"
|
|
#include "main.h"
|
|
#include "mesh/PhoneAPI.h"
|
|
#include "mesh/mesh-pb-constants.h"
|
|
|
|
#include <zephyr/bluetooth/bluetooth.h>
|
|
#include <zephyr/bluetooth/conn.h>
|
|
#include <zephyr/bluetooth/gatt.h>
|
|
#include <zephyr/bluetooth/hci.h>
|
|
#include <zephyr/bluetooth/uuid.h>
|
|
#include <zephyr/kernel.h>
|
|
#include <zephyr/settings/settings.h>
|
|
#include <zephyr/sys/reboot.h>
|
|
|
|
// ── UUID definitions (little-endian per Bluetooth spec)
|
|
// ─────────────────────── Syntax: replace hyphens with commas, prefix 0x —
|
|
// matches BT_UUID_128_ENCODE doc.
|
|
|
|
#define MESH_SVC_UUID_VAL BT_UUID_128_ENCODE(0x6ba1b218, 0x15a8, 0x461f, 0x9fa8, 0x5dcae273eafd)
|
|
#define FROMNUM_UUID_VAL BT_UUID_128_ENCODE(0xed9da18c, 0xa800, 0x4f66, 0xa670, 0xaa7547e34453)
|
|
#define FROMRADIO_UUID_VAL BT_UUID_128_ENCODE(0x2c55e69e, 0x4993, 0x11ed, 0xb878, 0x0242ac120002)
|
|
#define TORADIO_UUID_VAL BT_UUID_128_ENCODE(0xf75c76d2, 0x129e, 0x4dad, 0xa1dd, 0x7866124401e7)
|
|
#define LOGRADIO_UUID_VAL BT_UUID_128_ENCODE(0x5a3d6e49, 0x06e6, 0x4423, 0x9944, 0xe9de8cdf9547)
|
|
|
|
static const struct bt_uuid_128 mesh_svc_uuid = BT_UUID_INIT_128(MESH_SVC_UUID_VAL);
|
|
static const struct bt_uuid_128 fromnum_uuid = BT_UUID_INIT_128(FROMNUM_UUID_VAL);
|
|
static const struct bt_uuid_128 fromradio_uuid = BT_UUID_INIT_128(FROMRADIO_UUID_VAL);
|
|
static const struct bt_uuid_128 toradio_uuid = BT_UUID_INIT_128(TORADIO_UUID_VAL);
|
|
static const struct bt_uuid_128 logradio_uuid = BT_UUID_INIT_128(LOGRADIO_UUID_VAL);
|
|
|
|
// ── Module state ─────────────────────────────────────────────────────────────
|
|
|
|
static struct bt_conn *active_conn = nullptr;
|
|
static K_MUTEX_DEFINE(ble_mutex);
|
|
|
|
// Take a reference to active_conn under ble_mutex. Returns nullptr if there is
|
|
// no active connection. Caller MUST bt_conn_unref() when done.
|
|
//
|
|
// Reading `active_conn` outside this lock races with disconnected_cb which can
|
|
// unref + null it on the BT RX thread — touching the freed pointer (even just
|
|
// to bt_conn_ref it) is a use-after-free.
|
|
static struct bt_conn *acquire_active_conn()
|
|
{
|
|
struct bt_conn *conn = nullptr;
|
|
k_mutex_lock(&ble_mutex, K_FOREVER);
|
|
if (active_conn) {
|
|
conn = bt_conn_ref(active_conn);
|
|
}
|
|
k_mutex_unlock(&ble_mutex);
|
|
return conn;
|
|
}
|
|
|
|
static bool bt_initialized = false; // bt_enable() called at most once
|
|
static bool ble_enabled = false; // set by setup(), cleared by shutdown()
|
|
|
|
// Forward declarations — BT_GATT_SERVICE_DEFINE(mesh_svc, ...) is below, but
|
|
// read_fromradio() (defined earlier) needs to reference the service to notify
|
|
// on fromNum after each non-empty read.
|
|
#define FROMNUM_ATTR_IDX 2
|
|
#define LOGRADIO_ATTR_IDX 9
|
|
extern const struct bt_gatt_service_static mesh_svc;
|
|
|
|
static void start_advertising(); // forward declaration (defined in advertising
|
|
// section below)
|
|
|
|
// Work item for advertising restart after disconnect.
|
|
//
|
|
// disconnected_cb runs on the BT RX thread (the same thread that processes
|
|
// HCI Command Complete events). Calling bt_le_adv_start() →
|
|
// bt_hci_cmd_send_sync() directly from that thread deadlocks: the thread blocks
|
|
// on k_sem_take waiting for Command Complete, but it is the very thread that
|
|
// would process it. After 10 s the host panics with "Controller unresponsive,
|
|
// opcode 0x2006 timeout".
|
|
//
|
|
// Fix: submit a k_work item. The system workqueue runs bt_adv_restart_work_fn
|
|
// on its own thread → no deadlock.
|
|
static struct k_work adv_restart_work;
|
|
|
|
static void adv_restart_work_fn(struct k_work *work)
|
|
{
|
|
if (ble_enabled) {
|
|
start_advertising();
|
|
}
|
|
}
|
|
|
|
// CCC state: 0=off, BT_GATT_CCC_NOTIFY=notify, BT_GATT_CCC_INDICATE=indicate
|
|
static uint16_t fromnum_ccc_val = 0;
|
|
static uint16_t logradio_ccc_val = 0;
|
|
|
|
// Scratch buffers — only one BLE operation at a time
|
|
static uint8_t fromRadioBytes[meshtastic_FromRadio_size];
|
|
static size_t fromRadioLen = 0;
|
|
static uint8_t toRadioBytes[meshtastic_ToRadio_size];
|
|
static uint8_t lastToRadio[MAX_TO_FROM_RADIO_SIZE];
|
|
static uint32_t fromNumValue = 0;
|
|
|
|
// Deferred ToRadio processing
|
|
//
|
|
// write_toradio() runs on the BT RX workqueue thread (6 KB stack). Calling
|
|
// phoneAPI->handleToRadio() directly triggers handleStartConfig →
|
|
// getFiles("/", 10) → nanopb encode, which overflows the stack on the exact
|
|
// "Client wants config" write. Instead we copy the payload into a pending
|
|
// buffer under a mutex and let BleDeferredThread (running on the Meshtastic
|
|
// OSThread scheduler, 24 KB stack) do the actual call outside the lock.
|
|
//
|
|
// The mutex makes the producer/consumer handoff race-free — producer may
|
|
// overwrite a pending buffer the consumer hasn't read yet (dropped packet),
|
|
// but partial reads / torn writes are impossible.
|
|
K_MUTEX_DEFINE(pendingToRadioMutex);
|
|
static uint8_t pendingToRadioBuf[MAX_TO_FROM_RADIO_SIZE];
|
|
static size_t pendingToRadioLen = 0;
|
|
static bool pendingToRadio = false;
|
|
|
|
// Zombie-connection watchdog state.
|
|
//
|
|
// The nRF54L15 Zephyr 4.2.1 SW-LL occasionally fails to forward an
|
|
// LE Disconnection Complete event to the host: when iOS tears down the link
|
|
// (either explicitly by the user or via supervision timeout), the LL layer
|
|
// drops the connection but disconnected_cb never fires, active_conn stays
|
|
// non-null and advertising never restarts — the device vanishes from scans
|
|
// until power cycle. Track the connected timestamp and the last time we
|
|
// observed ATT traffic; a long ATT idle on an "active" connection means we
|
|
// are zombied. A cold reboot is the only path that reliably recovers (any
|
|
// bt_hci_cmd_send_sync after this state, e.g. bt_le_adv_start or
|
|
// bt_conn_disconnect, hangs in k_sem_take and later panics with "Controller
|
|
// unresponsive, opcode 0x2006 timeout").
|
|
static uint32_t connect_time_ms = 0;
|
|
static uint32_t last_att_time_ms = 0;
|
|
|
|
// ── BluetoothPhoneAPI
|
|
// ─────────────────────────────────────────────────────────
|
|
|
|
class BluetoothPhoneAPI : public PhoneAPI
|
|
{
|
|
virtual void onNowHasData(uint32_t fromRadioNum) override;
|
|
virtual bool checkIsConnected() override;
|
|
|
|
public:
|
|
BluetoothPhoneAPI() { api_type = TYPE_BLE; }
|
|
};
|
|
|
|
static BluetoothPhoneAPI *phoneAPI = nullptr;
|
|
|
|
// ── CCC change callbacks
|
|
// ──────────────────────────────────────────────────────
|
|
|
|
static void fromnum_ccc_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
|
{
|
|
fromnum_ccc_val = value;
|
|
LOG_INFO("BLE fromNum CCC: %u", value);
|
|
}
|
|
|
|
static void logradio_ccc_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
|
{
|
|
logradio_ccc_val = value;
|
|
LOG_INFO("BLE logRadio CCC: %u", value);
|
|
}
|
|
|
|
// ── GATT attribute callbacks
|
|
// ──────────────────────────────────────────────────
|
|
|
|
static ssize_t read_fromnum(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset)
|
|
{
|
|
LOG_INFO("GATT read_fromnum: fromNum=%u offset=%u", fromNumValue, offset);
|
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, &fromNumValue, sizeof(fromNumValue));
|
|
}
|
|
|
|
static ssize_t read_fromradio(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset)
|
|
{
|
|
if (offset == 0) {
|
|
// First chunk: pull the next packet from the queue.
|
|
// Subsequent chunks (offset > 0) are ATT_READ_BLOB continuations of the
|
|
// same value and must reuse fromRadioBytes untouched.
|
|
fromRadioLen = phoneAPI ? phoneAPI->getFromRadio(fromRadioBytes) : 0;
|
|
LOG_DEBUG("GATT read_fromradio len=%u", (unsigned)fromRadioLen);
|
|
}
|
|
last_att_time_ms = k_uptime_get_32();
|
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, fromRadioBytes, fromRadioLen);
|
|
}
|
|
|
|
static ssize_t read_logradio(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset)
|
|
{
|
|
// logRadio is write-only from the device side (notify/indicate).
|
|
// Return an empty read so GATT discovery doesn't fail with NOT_PERMITTED.
|
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, NULL, 0);
|
|
}
|
|
|
|
static ssize_t write_toradio(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, uint16_t len,
|
|
uint16_t offset, uint8_t flags)
|
|
{
|
|
// Writes >MTU-3 arrive here with offset=0 and
|
|
// flags=BT_GATT_WRITE_FLAG_EXECUTE after Zephyr reassembles the ATT Prepare
|
|
// Write fragments (CONFIG_BT_ATT_PREPARE_COUNT>0). Single writes arrive with
|
|
// flags=0.
|
|
LOG_DEBUG("GATT write_toradio len=%u flags=0x%x", len, flags);
|
|
if (offset != 0) {
|
|
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
|
|
}
|
|
// Reject any write that won't fit in the dedup buffer (lastToRadio) or the
|
|
// pending handoff buffer (pendingToRadioBuf), both sized
|
|
// MAX_TO_FROM_RADIO_SIZE. Returning success while silently dropping a
|
|
// payload would let the phone believe a config write was applied.
|
|
if (len > sizeof(toRadioBytes) || len > MAX_TO_FROM_RADIO_SIZE) {
|
|
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
|
|
}
|
|
|
|
// Deduplicate — drop packet if identical to the last one we processed
|
|
if (memcmp(lastToRadio, buf, len) != 0) {
|
|
memcpy(lastToRadio, buf, len);
|
|
if (len < MAX_TO_FROM_RADIO_SIZE) {
|
|
memset(lastToRadio + len, 0, MAX_TO_FROM_RADIO_SIZE - len);
|
|
}
|
|
// Defer handleToRadio() to BleDeferredThread (24 KB stack).
|
|
// Running it here on bt_workq (6 KB) overflows during handleStartConfig.
|
|
// Always overwrite pending — we already dedup'd above via lastToRadio,
|
|
// so any new write here is genuinely new data that must be delivered.
|
|
k_mutex_lock(&pendingToRadioMutex, K_FOREVER);
|
|
memcpy(pendingToRadioBuf, buf, len);
|
|
pendingToRadioLen = len;
|
|
pendingToRadio = true;
|
|
k_mutex_unlock(&pendingToRadioMutex);
|
|
}
|
|
last_att_time_ms = k_uptime_get_32();
|
|
return (ssize_t)len;
|
|
}
|
|
|
|
// ── GATT service definition (static, linked at compile time)
|
|
// ──────────────────
|
|
//
|
|
// Attribute indices (0-based):
|
|
// [0] Primary Service declaration
|
|
// [1] fromNum characteristic declaration
|
|
// [2] fromNum value ← notify target (FROMNUM_ATTR_IDX)
|
|
// [3] fromNum CCC descriptor
|
|
// [4] fromRadio characteristic declaration
|
|
// [5] fromRadio value
|
|
// [6] toRadio characteristic declaration
|
|
// [7] toRadio value
|
|
// [8] logRadio characteristic declaration
|
|
// [9] logRadio value ← notify target (LOGRADIO_ATTR_IDX)
|
|
// [10] logRadio CCC descriptor
|
|
|
|
// All user characteristics require authenticated encryption (MITM passkey)
|
|
// before the client can read/write. This mirrors the nrf52
|
|
// SECMODE_ENC_WITH_MITM service permission. The stack returns "Insufficient
|
|
// Authentication" on the first access attempt, prompting the client to pair
|
|
// with the configured PIN.
|
|
#define MESH_PERM_READ (BT_GATT_PERM_READ | BT_GATT_PERM_READ_AUTHEN)
|
|
#define MESH_PERM_WRITE (BT_GATT_PERM_WRITE | BT_GATT_PERM_WRITE_AUTHEN)
|
|
|
|
BT_GATT_SERVICE_DEFINE(mesh_svc, BT_GATT_PRIMARY_SERVICE(&mesh_svc_uuid.uuid),
|
|
|
|
// fromNum: READ | NOTIFY — packet-counter triggers phone to read fromRadio
|
|
BT_GATT_CHARACTERISTIC(&fromnum_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, MESH_PERM_READ,
|
|
read_fromnum, NULL, &fromNumValue),
|
|
BT_GATT_CCC(fromnum_ccc_changed, MESH_PERM_READ | MESH_PERM_WRITE),
|
|
|
|
// fromRadio: READ — phone polls this after receiving a fromNum notification
|
|
BT_GATT_CHARACTERISTIC(&fromradio_uuid.uuid, BT_GATT_CHRC_READ, MESH_PERM_READ, read_fromradio, NULL,
|
|
NULL),
|
|
|
|
// toRadio: WRITE — phone sends protobuf packets to the device
|
|
BT_GATT_CHARACTERISTIC(&toradio_uuid.uuid, BT_GATT_CHRC_WRITE, MESH_PERM_WRITE, NULL, write_toradio, NULL),
|
|
|
|
// logRadio: READ | NOTIFY | INDICATE — log stream to phone when connected
|
|
BT_GATT_CHARACTERISTIC(&logradio_uuid.uuid,
|
|
BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_INDICATE, MESH_PERM_READ,
|
|
read_logradio, NULL, NULL),
|
|
BT_GATT_CCC(logradio_ccc_changed, MESH_PERM_READ | MESH_PERM_WRITE), );
|
|
|
|
// ── Advertising
|
|
// ───────────────────────────────────────────────────────────────
|
|
//
|
|
// Use legacy advertising (bt_le_adv_start / HCI 0x2006 path).
|
|
//
|
|
// History: we previously used bt_le_ext_adv_create (true extended advertising)
|
|
// because bt_le_adv_start() with CONFIG_BT_EXT_ADV=y was translated internally
|
|
// to the extended HCI path with LEGACY-bit (0x2036), which produced
|
|
// non-connectable PDUs on the nRF54L15 SW-LL. The true extended path
|
|
// (0x203x, AUX_ADV_IND) was connectable but caused two problems:
|
|
// 1. iOS CoreBluetooth does not reliably complete GATT after connecting via
|
|
// extended advertising (zero ATT PDUs observed in all test sessions).
|
|
// 2. After each connection the controller auto-stops the advertising set, and
|
|
// the subsequent bt_le_ext_adv_delete() sends LE Remove Advertising Set
|
|
// (0x203c) which times out → kernel oops at hci_core.c:506.
|
|
//
|
|
// With CONFIG_BT_EXT_ADV=n the host uses pure legacy HCI commands — the same
|
|
// path Nordic NCS uses in all nRF54L15 examples (peripheral_uart,
|
|
// peripheral_lbs) and which is universally iOS-compatible. The legacy data
|
|
// payload is 31 bytes:
|
|
// FLAGS (3B) + UUID128 (18B) = 21B in adv; NAME in scan-response (17B).
|
|
|
|
static void start_advertising()
|
|
{
|
|
// IMPORTANT: BT_DATA_BYTES() uses C99 compound literals that GCC C++ treats
|
|
// as temporaries; with -Os the compiler may elide writes, leaving stack
|
|
// uninitialized. Use static const arrays for stable data (flags, UUID)
|
|
// and a runtime pointer for the dynamic device name.
|
|
static const uint8_t adv_flags_val[] = {BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR};
|
|
static const uint8_t adv_uuid128_val[] = {MESH_SVC_UUID_VAL};
|
|
|
|
const char *name = bt_get_name();
|
|
size_t full_name_len = strlen(name);
|
|
|
|
// Legacy scan-response payload is 31 bytes total. Each AD entry costs 2
|
|
// bytes (length + type), leaving 29 bytes for the name. With
|
|
// CONFIG_BT_DEVICE_NAME_MAX=32 the name can exceed that — truncate and
|
|
// mark as SHORTENED so bt_le_adv_start() doesn't reject the payload.
|
|
constexpr size_t LEGACY_SCAN_RSP_NAME_MAX = 31 - 2;
|
|
bool name_shortened = full_name_len > LEGACY_SCAN_RSP_NAME_MAX;
|
|
uint8_t name_len = (uint8_t)(name_shortened ? LEGACY_SCAN_RSP_NAME_MAX : full_name_len);
|
|
|
|
// Primary advertising data: FLAGS + Meshtastic service UUID128 (21 bytes
|
|
// total)
|
|
struct bt_data ad[] = {
|
|
{BT_DATA_FLAGS, sizeof(adv_flags_val), adv_flags_val},
|
|
{BT_DATA_UUID128_ALL, sizeof(adv_uuid128_val), adv_uuid128_val},
|
|
};
|
|
// Scan response: device name (discovered after scan request)
|
|
struct bt_data sd[] = {
|
|
{(uint8_t)(name_shortened ? BT_DATA_NAME_SHORTENED : BT_DATA_NAME_COMPLETE), name_len, (const uint8_t *)name},
|
|
};
|
|
|
|
// BT_LE_ADV_OPT_CONN = connectable legacy ADV_IND + stops after first
|
|
// connection (replaces deprecated
|
|
// CONNECTABLE|ONE_TIME in Zephyr 4.2.1;
|
|
// BT_LE_ADV_OPT_CONN = BIT(0)|BIT(1))
|
|
// BT_LE_ADV_OPT_USE_IDENTITY = use static random identity address (stable
|
|
// across reboots) Advertising restart after disconnect is via
|
|
// adv_restart_work (system workqueue) so calling bt_le_adv_start() from the
|
|
// BT RX thread context is avoided.
|
|
int err = bt_le_adv_start(BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONN | BT_LE_ADV_OPT_USE_IDENTITY, BT_GAP_ADV_FAST_INT_MIN_2,
|
|
BT_GAP_ADV_FAST_INT_MAX_2, NULL),
|
|
ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
|
|
|
|
if (err == -EALREADY) {
|
|
return;
|
|
}
|
|
if (err) {
|
|
LOG_WARN("BLE adv start failed: %d", err);
|
|
} else {
|
|
LOG_INFO("BLE advertising as '%s'", bt_get_name());
|
|
}
|
|
}
|
|
|
|
static void stop_advertising()
|
|
{
|
|
bt_le_adv_stop();
|
|
}
|
|
|
|
// ── Connection callbacks
|
|
// ──────────────────────────────────────────────────────
|
|
|
|
static void connected_cb(struct bt_conn *conn, uint8_t err)
|
|
{
|
|
if (err) {
|
|
LOG_WARN("BLE connection failed, err=0x%02x", err);
|
|
return;
|
|
}
|
|
|
|
k_mutex_lock(&ble_mutex, K_FOREVER);
|
|
active_conn = bt_conn_ref(conn);
|
|
k_mutex_unlock(&ble_mutex);
|
|
|
|
memset(lastToRadio, 0, sizeof(lastToRadio));
|
|
connect_time_ms = k_uptime_get_32();
|
|
last_att_time_ms = connect_time_ms;
|
|
|
|
char addr[BT_ADDR_LE_STR_LEN];
|
|
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
|
|
LOG_INFO("BLE connected: %s", addr);
|
|
|
|
meshtastic::BluetoothStatus newStatus(meshtastic::BluetoothStatus::ConnectionState::CONNECTED);
|
|
bluetoothStatus->updateStatus(&newStatus);
|
|
|
|
// nRF54L15-DK has no screen — cannot display a PIN to the user.
|
|
// Requesting BT_SECURITY_L2 causes the OS to show a pairing dialog that
|
|
// the user dismisses, triggering disconnect + advertising restart failure.
|
|
// Skip security negotiation; the Meshtastic app works over plain GATT.
|
|
// (Security can be re-enabled once a display or NFC OOB path is available.)
|
|
}
|
|
|
|
static void disconnected_cb(struct bt_conn *conn, uint8_t reason)
|
|
{
|
|
LOG_INFO("BLE disconnected, reason=0x%02x", reason);
|
|
|
|
k_mutex_lock(&ble_mutex, K_FOREVER);
|
|
if (active_conn) {
|
|
bt_conn_unref(active_conn);
|
|
active_conn = nullptr;
|
|
}
|
|
k_mutex_unlock(&ble_mutex);
|
|
|
|
fromnum_ccc_val = 0;
|
|
logradio_ccc_val = 0;
|
|
connect_time_ms = 0;
|
|
last_att_time_ms = 0;
|
|
|
|
if (phoneAPI) {
|
|
phoneAPI->close();
|
|
}
|
|
memset(lastToRadio, 0, sizeof(lastToRadio));
|
|
|
|
meshtastic::BluetoothStatus newStatus(meshtastic::BluetoothStatus::ConnectionState::DISCONNECTED);
|
|
bluetoothStatus->updateStatus(&newStatus);
|
|
|
|
// Schedule advertising restart via work queue — NOT from this callback
|
|
// directly. disconnected_cb runs on the BT RX thread; calling
|
|
// bt_le_adv_start() here would deadlock (see adv_restart_work comment above).
|
|
if (ble_enabled) {
|
|
k_work_submit(&adv_restart_work);
|
|
}
|
|
}
|
|
|
|
#if defined(CONFIG_BT_SMP)
|
|
static void security_changed_cb(struct bt_conn *conn, bt_security_t level, enum bt_security_err err)
|
|
{
|
|
if (err == BT_SECURITY_ERR_PIN_OR_KEY_MISSING) {
|
|
// Phone has a stale bond (device was wiped/reflashed). Unpair the stale
|
|
// entry so the phone re-pairs cleanly on the next connection attempt.
|
|
LOG_WARN("BLE stale bond detected (key missing) — unpairing");
|
|
bt_unpair(BT_ID_DEFAULT, bt_conn_get_dst(conn));
|
|
bt_conn_disconnect(conn, BT_HCI_ERR_AUTH_FAIL);
|
|
} else if (err) {
|
|
LOG_WARN("BLE security change failed: level=%d err=%d", (int)level, (int)err);
|
|
} else {
|
|
LOG_INFO("BLE security level %d established", (int)level);
|
|
}
|
|
}
|
|
#endif /* CONFIG_BT_SMP */
|
|
|
|
BT_CONN_CB_DEFINE(conn_callbacks) = {
|
|
.connected = connected_cb,
|
|
.disconnected = disconnected_cb,
|
|
#if defined(CONFIG_BT_SMP)
|
|
.security_changed = security_changed_cb,
|
|
#endif
|
|
};
|
|
|
|
// ── Pairing / auth callbacks
|
|
// ──────────────────────────────────────────────────
|
|
|
|
#if defined(CONFIG_BT_SMP)
|
|
static uint32_t configuredPasskey;
|
|
|
|
static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey)
|
|
{
|
|
char passkey_str[7];
|
|
snprintf(passkey_str, sizeof(passkey_str), "%06u", passkey);
|
|
configuredPasskey = passkey;
|
|
LOG_INFO("BLE pairing PIN: %s", passkey_str);
|
|
powerFSM.trigger(EVENT_BLUETOOTH_PAIR);
|
|
|
|
std::string textkey(passkey_str);
|
|
meshtastic::BluetoothStatus pairingStatus(textkey);
|
|
bluetoothStatus->updateStatus(&pairingStatus);
|
|
}
|
|
|
|
static void auth_cancel(struct bt_conn *conn)
|
|
{
|
|
LOG_WARN("BLE pairing cancelled");
|
|
}
|
|
|
|
static struct bt_conn_auth_cb auth_cb = {
|
|
.passkey_display = auth_passkey_display,
|
|
.passkey_entry = NULL,
|
|
.cancel = auth_cancel,
|
|
};
|
|
|
|
static void pairing_complete_cb(struct bt_conn *conn, bool bonded)
|
|
{
|
|
LOG_INFO("BLE pairing complete, bonded=%d", (int)bonded);
|
|
meshtastic::BluetoothStatus newStatus(meshtastic::BluetoothStatus::ConnectionState::CONNECTED);
|
|
bluetoothStatus->updateStatus(&newStatus);
|
|
}
|
|
|
|
static void pairing_failed_cb(struct bt_conn *conn, enum bt_security_err reason)
|
|
{
|
|
LOG_WARN("BLE pairing failed, reason=%d", (int)reason);
|
|
meshtastic::BluetoothStatus newStatus(meshtastic::BluetoothStatus::ConnectionState::DISCONNECTED);
|
|
bluetoothStatus->updateStatus(&newStatus);
|
|
}
|
|
|
|
static struct bt_conn_auth_info_cb auth_info_cb = {
|
|
.pairing_complete = pairing_complete_cb,
|
|
.pairing_failed = pairing_failed_cb,
|
|
};
|
|
#endif /* CONFIG_BT_SMP */
|
|
|
|
// ── BluetoothPhoneAPI methods
|
|
// ─────────────────────────────────────────────────
|
|
|
|
void BluetoothPhoneAPI::onNowHasData(uint32_t fromRadioNum)
|
|
{
|
|
PhoneAPI::onNowHasData(fromRadioNum);
|
|
fromNumValue = fromRadioNum;
|
|
|
|
if (!(fromnum_ccc_val & BT_GATT_CCC_NOTIFY))
|
|
return;
|
|
|
|
// active_conn may be torn down on another thread while we're dispatching
|
|
// this notify — acquire under ble_mutex so disconnected_cb can't free the
|
|
// conn between the null check and bt_conn_ref.
|
|
struct bt_conn *conn = acquire_active_conn();
|
|
if (!conn)
|
|
return;
|
|
bt_gatt_notify(conn, &mesh_svc.attrs[FROMNUM_ATTR_IDX], &fromNumValue, sizeof(fromNumValue));
|
|
bt_conn_unref(conn);
|
|
}
|
|
|
|
bool BluetoothPhoneAPI::checkIsConnected()
|
|
{
|
|
return active_conn != nullptr;
|
|
}
|
|
|
|
// ── Deferred ToRadio processor + zombie-connection watchdog ──────────────────
|
|
//
|
|
// write_toradio() runs on the BT RX workqueue thread (CONFIG_BT_RX_STACK_SIZE)
|
|
// and cannot execute phoneAPI->handleToRadio() directly: handleStartConfig
|
|
// recurses through nanopb encode + state machine init and overflows the RX
|
|
// stack. This thread runs on the Meshtastic OSThread scheduler (24 KB stack),
|
|
// picks up the pending ToRadio buffer flagged by write_toradio(), and calls
|
|
// handleToRadio() with plenty of headroom.
|
|
//
|
|
// Real-time fromNum notifications are sent synchronously from
|
|
// BluetoothPhoneAPI::onNowHasData() (called by PhoneAPI when new data is
|
|
// queued).
|
|
//
|
|
// Zombie-connection detection has two tiers:
|
|
//
|
|
// (1) Liveness probe. After IDLE_BEFORE_PROBE_MS without ATT traffic, send
|
|
// a bt_gatt_notify to fromNum every PROBE_INTERVAL_MS. If the
|
|
// controller replies -ENOTCONN the LL link is definitely dead but the
|
|
// host didn't forward LE Disconnection Complete → reboot. We avoid
|
|
// probing during normal activity so iOS isn't woken up unnecessarily
|
|
// (each probe wakes iOS → triggers a zero-byte FromRadio drain).
|
|
//
|
|
// (2) Hard watchdog. Absolute HARD_WATCHDOG_MS ceiling on ATT idle as a
|
|
// fallback if probes somehow don't detect the zombie.
|
|
class BleDeferredThread : public concurrency::OSThread
|
|
{
|
|
static constexpr uint32_t IDLE_BEFORE_PROBE_MS = 30000; // 30 s: start probing
|
|
static constexpr uint32_t PROBE_INTERVAL_MS = 5000; // 5 s: between probes
|
|
static constexpr uint32_t HARD_WATCHDOG_MS = 60000; // 1 min: last resort
|
|
|
|
uint32_t last_probe_ms = 0;
|
|
|
|
public:
|
|
BleDeferredThread() : concurrency::OSThread("BleDeferred") {}
|
|
|
|
protected:
|
|
int32_t runOnce() override
|
|
{
|
|
// Snapshot the pending ToRadio buffer under the mutex, then release
|
|
// the lock before calling into handleToRadio (which can be slow and
|
|
// must not block the BT RX thread producer).
|
|
uint8_t buf[MAX_TO_FROM_RADIO_SIZE];
|
|
size_t n = 0;
|
|
bool have_pending = false;
|
|
k_mutex_lock(&pendingToRadioMutex, K_FOREVER);
|
|
if (pendingToRadio) {
|
|
memcpy(buf, pendingToRadioBuf, pendingToRadioLen);
|
|
n = pendingToRadioLen;
|
|
pendingToRadio = false;
|
|
have_pending = true;
|
|
}
|
|
k_mutex_unlock(&pendingToRadioMutex);
|
|
if (have_pending && phoneAPI) {
|
|
phoneAPI->handleToRadio(buf, n);
|
|
}
|
|
|
|
// Take a reference to active_conn so it can't be freed underneath us
|
|
// if disconnected_cb fires on another thread while we're dispatching.
|
|
struct bt_conn *conn = acquire_active_conn();
|
|
if (!conn || connect_time_ms == 0) {
|
|
if (conn)
|
|
bt_conn_unref(conn);
|
|
last_probe_ms = 0;
|
|
return 100;
|
|
}
|
|
|
|
uint32_t now = k_uptime_get_32();
|
|
uint32_t att_idle = now - last_att_time_ms;
|
|
|
|
// Liveness probe — only when ATT has been quiet for a while.
|
|
if (att_idle > IDLE_BEFORE_PROBE_MS && (now - last_probe_ms) >= PROBE_INTERVAL_MS &&
|
|
(fromnum_ccc_val & BT_GATT_CCC_NOTIFY)) {
|
|
last_probe_ms = now;
|
|
int err = bt_gatt_notify(conn, &mesh_svc.attrs[FROMNUM_ATTR_IDX], &fromNumValue, sizeof(fromNumValue));
|
|
if (err == -ENOTCONN) {
|
|
LOG_WARN("BLE zombie (probe ENOTCONN); rebooting");
|
|
bt_conn_unref(conn);
|
|
k_sleep(K_MSEC(50)); // flush log
|
|
sys_reboot(SYS_REBOOT_COLD);
|
|
}
|
|
}
|
|
bt_conn_unref(conn);
|
|
|
|
// Hard ceiling — last-resort reboot if probes miss the zombie.
|
|
if (att_idle > HARD_WATCHDOG_MS && (now - connect_time_ms) > HARD_WATCHDOG_MS) {
|
|
LOG_WARN("BLE zombie (hard watchdog %us); rebooting", HARD_WATCHDOG_MS / 1000);
|
|
k_sleep(K_MSEC(50));
|
|
sys_reboot(SYS_REBOOT_COLD);
|
|
}
|
|
return 100;
|
|
}
|
|
};
|
|
|
|
static BleDeferredThread *bleDeferredThread = nullptr;
|
|
|
|
// ── BT stack pre-initializer (call from main thread before OSThreads start) ──
|
|
//
|
|
// bt_enable() requires substantially more stack than a Meshtastic OSThread
|
|
// (PowerFSMThread) provides — calling it there causes a stack overflow.
|
|
// Call this from nrf54l15Setup() (main Zephyr thread, CONFIG_MAIN_STACK_SIZE)
|
|
// so that by the time NRF54L15Bluetooth::setup() runs from PowerFSMThread,
|
|
// bt_initialized is already true and bt_enable() is skipped.
|
|
|
|
void nrf54l15_bt_preinit()
|
|
{
|
|
if (!bt_initialized) {
|
|
int err = bt_enable(NULL);
|
|
if (err) {
|
|
LOG_ERROR("BLE pre-init failed: %d", err);
|
|
return;
|
|
}
|
|
bt_initialized = true;
|
|
LOG_INFO("BLE stack pre-initialized on main thread");
|
|
|
|
// Phase 7: load bonding keys from LittleFS (/lfs/bt_settings).
|
|
// LittleFS is already mounted by fsInit() before nrf54l15Setup() runs.
|
|
// On first boot the file doesn't exist — settings_load() returns 0 (OK).
|
|
// On subsequent boots, previously bonded peers are restored so the
|
|
// phone can reconnect without re-pairing.
|
|
err = settings_load();
|
|
if (err) {
|
|
LOG_WARN("settings_load failed: %d (OK on first boot)", err);
|
|
} else {
|
|
LOG_INFO("BT settings loaded from /lfs/bt_settings");
|
|
}
|
|
}
|
|
}
|
|
|
|
// ── NRF54L15Bluetooth public methods ─────────────────────────────────────────
|
|
|
|
// Shared init: idempotent setup of work item, OSThread, auth callbacks,
|
|
// bt_enable, and device name. Leaves advertising control to the caller.
|
|
static bool nrf54l15_bt_init_common()
|
|
{
|
|
k_work_init(&adv_restart_work, adv_restart_work_fn);
|
|
|
|
if (!bleDeferredThread) {
|
|
bleDeferredThread = new BleDeferredThread();
|
|
}
|
|
|
|
if (!phoneAPI) {
|
|
phoneAPI = new BluetoothPhoneAPI();
|
|
}
|
|
|
|
#if defined(CONFIG_BT_SMP)
|
|
// NO_PIN is unsupported on this platform: the mesh GATT permissions are
|
|
// declared with BT_GATT_PERM_*_AUTHEN, prj.conf sets
|
|
// CONFIG_BT_SMP_ENFORCE_MITM=y, and the build pulls in the SMP/passkey path.
|
|
// If a user requested NO_PIN we'd register no auth callbacks → no display
|
|
// path for the passkey → every GATT access returns BT_ATT_ERR_AUTHENTICATION
|
|
// and the link is unusable. Fall back to RANDOM_PIN behavior with a warning
|
|
// instead of leaving BLE silently broken.
|
|
if (config.bluetooth.mode == meshtastic_Config_BluetoothConfig_PairingMode_NO_PIN) {
|
|
LOG_WARN("BLE: NO_PIN not supported on nRF54L15-DK (MITM-only build); "
|
|
"treating as RANDOM_PIN");
|
|
}
|
|
|
|
bt_conn_auth_cb_register(&auth_cb);
|
|
bt_conn_auth_info_cb_register(&auth_info_cb);
|
|
|
|
// FIXED_PIN — register the configured passkey so the mobile app prompts
|
|
// the user for that specific number instead of a random display-only PIN.
|
|
// RANDOM_PIN (and clamped NO_PIN) keeps the default behavior: Zephyr
|
|
// generates a fresh passkey on each pairing attempt and fires
|
|
// auth_passkey_display with it.
|
|
if (config.bluetooth.mode == meshtastic_Config_BluetoothConfig_PairingMode_FIXED_PIN) {
|
|
configuredPasskey = config.bluetooth.fixed_pin;
|
|
int rc = bt_passkey_set(configuredPasskey);
|
|
if (rc) {
|
|
LOG_WARN("bt_passkey_set(%u) failed: %d", configuredPasskey, rc);
|
|
} else {
|
|
LOG_INFO("BLE fixed PIN: %06u", configuredPasskey);
|
|
}
|
|
} else {
|
|
bt_passkey_set(BT_PASSKEY_INVALID); // random per-pair
|
|
}
|
|
#endif /* CONFIG_BT_SMP */
|
|
|
|
if (!bt_initialized) {
|
|
int err = bt_enable(NULL);
|
|
if (err) {
|
|
LOG_ERROR("BLE enable failed: %d", err);
|
|
return false;
|
|
}
|
|
bt_initialized = true;
|
|
LOG_INFO("BLE stack enabled");
|
|
}
|
|
|
|
bt_set_name(getDeviceName());
|
|
return true;
|
|
}
|
|
|
|
void NRF54L15Bluetooth::setup()
|
|
{
|
|
LOG_INFO("NRF54L15Bluetooth::setup()");
|
|
if (!nrf54l15_bt_init_common()) {
|
|
return;
|
|
}
|
|
ble_enabled = true;
|
|
start_advertising();
|
|
}
|
|
|
|
void NRF54L15Bluetooth::shutdown()
|
|
{
|
|
LOG_INFO("NRF54L15Bluetooth::shutdown()");
|
|
ble_enabled = false;
|
|
stop_advertising();
|
|
|
|
struct bt_conn *conn = acquire_active_conn();
|
|
if (conn) {
|
|
bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
|
|
bt_conn_unref(conn);
|
|
}
|
|
}
|
|
|
|
void NRF54L15Bluetooth::startDisabled()
|
|
{
|
|
// Initialize BT stack but leave advertising off until resumeAdvertising().
|
|
if (!nrf54l15_bt_init_common()) {
|
|
return;
|
|
}
|
|
ble_enabled = false;
|
|
LOG_INFO("BLE initialized, advertising stopped (startDisabled)");
|
|
}
|
|
|
|
void NRF54L15Bluetooth::resumeAdvertising()
|
|
{
|
|
ble_enabled = true;
|
|
start_advertising();
|
|
}
|
|
|
|
void NRF54L15Bluetooth::clearBonds()
|
|
{
|
|
LOG_INFO("BLE clear bonds");
|
|
bt_unpair(BT_ID_DEFAULT, BT_ADDR_LE_ANY);
|
|
}
|
|
|
|
bool NRF54L15Bluetooth::isConnected()
|
|
{
|
|
return active_conn != nullptr;
|
|
}
|
|
|
|
int NRF54L15Bluetooth::getRssi()
|
|
{
|
|
return 0; // TODO: Zephyr has no direct bt_conn_get_rssi; use HCI RSSI read
|
|
// command
|
|
}
|
|
|
|
void NRF54L15Bluetooth::sendLog(const uint8_t *logMessage, size_t length)
|
|
{
|
|
if (length > 512 || logradio_ccc_val == 0) {
|
|
return;
|
|
}
|
|
// Acquire a reference under ble_mutex so disconnected_cb can't free the
|
|
// connection between the null check and bt_gatt_notify.
|
|
struct bt_conn *conn = acquire_active_conn();
|
|
if (!conn) {
|
|
return;
|
|
}
|
|
// Send as notify regardless of whether client subscribed to NOTIFY or
|
|
// INDICATE — bt_gatt_indicate() requires a params struct with a callback;
|
|
// notify is simpler and the app accepts both. Change to indicate if
|
|
// compatibility issues arise.
|
|
bt_gatt_notify(conn, &mesh_svc.attrs[LOGRADIO_ATTR_IDX], logMessage, (uint16_t)length);
|
|
bt_conn_unref(conn);
|
|
}
|