fix(nrf52): stabilize full companion bluetooth

This commit is contained in:
mikecarper committed 2026-09-22 05:54:46 -07:00
1 parent 21f877befc
commit 3282b7898e
10 files changed
+283 -59

No files matched your search

+1 -1
View File
@@ -3,7 +3,7 @@
// Canonical USB-capable images compile at least one of these two diagnostics.
// One runtime gate covers every diagnostic category that writes to the USB
// Serial stream, so a separate logging firmware is unnecessary.
#if defined(ARDUINO) && \
#if !defined(MESH_USB_LOGGING_DISABLED) && defined(ARDUINO) && \
((defined(MESH_DEBUG) && MESH_DEBUG) || \
(defined(MESH_PACKET_LOGGING) && MESH_PACKET_LOGGING))
#define MESH_USB_LOGGING_AVAILABLE 1
+76 -43
View File
@@ -407,6 +407,7 @@ bool SerialBLEInterface::begin(const char* prefix, const char* name,
// or configuration and a reboot.
if (_begin_attempted) return _begin_ready;
_begin_attempted = true;
_begin_failure[0] = 0;
instance = this;
_successfulConnectionPending.store(false, std::memory_order_release);
_successfulConnectionStarted.store(0, std::memory_order_relaxed);
@@ -422,16 +423,34 @@ bool SerialBLEInterface::begin(const char* prefix, const char* name,
// If we want to control BLE LED ourselves, uncomment this:
// Bluefruit.autoConnLed(false);
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
// Configure individual connection values rather than the all-or-nothing
// Bluefruit preset. Defaults are identical to BANDWIDTH_MAX; constrained
// board profiles can lower event/buffer reservations without shrinking the
// Companion's negotiated 247-byte MTU.
Bluefruit.configPrphConn(COMPANION_BLE_PRPH_MTU,
COMPANION_BLE_PRPH_EVENT_LENGTH,
COMPANION_BLE_PRPH_HVN_QUEUE,
COMPANION_BLE_PRPH_WRCMD_QUEUE);
// The pinned nRF52 core is built with LTO. Its internal xTaskCreate() calls
// are resolved before the application's linker wrappers, so a wrapper-based
// task observation incorrectly reports that the BLE/SOC workers never
// started. Bluefruit.begin() is the reliable startup result available to
// this application.
if (!Bluefruit.begin()) {
uint8_t softdevice_enabled = 0;
const uint32_t softdevice_status =
sd_softdevice_is_enabled(&softdevice_enabled);
ble_gap_addr_t address = {};
const uint32_t gap_status = softdevice_enabled
? sd_ble_gap_addr_get(&address) : NRF_ERROR_INVALID_STATE;
snprintf(_begin_failure, sizeof(_begin_failure),
"Bluefruit begin failed (SD %lu/%u, GAP %lu)",
(unsigned long)softdevice_status,
(unsigned)softdevice_enabled,
(unsigned long)gap_status);
instance = nullptr;
mesh::usbLoggingPort().println(
"Bluetooth startup failed; reboot required");
_begin_failure);
return false;
}
@@ -439,6 +458,9 @@ bool SerialBLEInterface::begin(const char* prefix, const char* name,
if (custom_address != nullptr) {
if (!mesh::companion::isValidBluetoothMac(custom_address)) {
strncpy(_begin_failure, "custom Bluetooth MAC is invalid",
sizeof(_begin_failure) - 1);
_begin_failure[sizeof(_begin_failure) - 1] = 0;
instance = nullptr;
BLE_DEBUG_PRINTLN("Custom Bluetooth MAC is invalid");
return false;
@@ -452,6 +474,9 @@ bool SerialBLEInterface::begin(const char* prefix, const char* name,
}
const uint32_t address_error = sd_ble_gap_addr_set(&address);
if (address_error != NRF_SUCCESS) {
snprintf(_begin_failure, sizeof(_begin_failure),
"custom Bluetooth MAC rejected (%lu)",
(unsigned long)address_error);
instance = nullptr;
BLE_DEBUG_PRINTLN("Custom Bluetooth MAC failed: %lu", address_error);
return false;
@@ -474,6 +499,9 @@ bool SerialBLEInterface::begin(const char* prefix, const char* name,
const int dev_name_len = snprintf(dev_name, sizeof(dev_name), "%s%s",
prefix, suffix);
if (dev_name_len < 0 || dev_name_len >= (int)sizeof(dev_name)) {
strncpy(_begin_failure, "Bluetooth device name is too long",
sizeof(_begin_failure) - 1);
_begin_failure[sizeof(_begin_failure) - 1] = 0;
instance = nullptr;
BLE_DEBUG_PRINTLN("Bluetooth device name is too long");
return false;
@@ -514,57 +542,61 @@ bool SerialBLEInterface::begin(const char* prefix, const char* name,
bleuart.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM);
if (bleuart.begin() != ERROR_NONE) {
strncpy(_begin_failure, "Bluetooth UART service registration failed",
sizeof(_begin_failure) - 1);
_begin_failure[sizeof(_begin_failure) - 1] = 0;
instance = nullptr;
BLE_DEBUG_PRINTLN("Bluetooth UART service begin failed");
return false;
}
bleuart.setRxCallback(onBleUartRX);
#if COMPANION_FEATURE_BLE_MOTA_SOURCE
_mota_stream.setSender(sendMotaRequest, this);
_mota_stream.setActive(false);
_mota_service.setPermission(SECMODE_ENC_WITH_MITM,
SECMODE_ENC_WITH_MITM);
if (_mota_service.begin() != ERROR_NONE) {
instance = nullptr;
BLE_DEBUG_PRINTLN("Bluetooth mOTA service begin failed");
return false;
}
_mota_request.setProperties(CHR_PROPS_NOTIFY);
_mota_request.setPermission(SECMODE_ENC_WITH_MITM,
SECMODE_NO_ACCESS);
_mota_request.setMaxLen(mesh::ota::BLE_MOTA_REQUEST_MAX);
_mota_request.setUserDescriptor("mOTA device request");
if (_mota_request.begin() != ERROR_NONE) {
instance = nullptr;
BLE_DEBUG_PRINTLN("Bluetooth mOTA request characteristic begin failed");
return false;
}
_mota_response.setProperties(CHR_PROPS_WRITE);
_mota_response.setPermission(SECMODE_NO_ACCESS,
SECMODE_ENC_WITH_MITM);
_mota_response.setMaxLen(mesh::ota::BLE_MOTA_RESPONSE_MAX);
_mota_response.setUserDescriptor("mOTA host response");
_mota_response.setWriteCallback(onMotaResponse);
if (_mota_response.begin() != ERROR_NONE) {
instance = nullptr;
BLE_DEBUG_PRINTLN("Bluetooth mOTA response characteristic begin failed");
return false;
// Register the legacy DFU service before the optional mOTA service. A
// fielded SoftDevice has a fixed GATT table: reserve the ordinary Companion
// and firmware-recovery transports first, then use whatever remains for
// the larger mOTA extension. Do not attempt a new service after an mOTA
// registration failure because Bluefruit cannot remove attributes that did
// fit from its active table.
#if COMPANION_FEATURE_BLE_DFU
bledfu.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM);
if (bledfu.begin() != ERROR_NONE) {
mesh::usbLoggingPort().println(
"Bluetooth DFU service unavailable; normal Companion Bluetooth continues");
}
#endif
#if COMPANION_FEATURE_BLE_MOTA_SOURCE
_mota_stream.setSender(sendMotaRequest, this);
_mota_stream.setActive(false);
_mota_available = false;
// Register DFU on the main BLE stack so paired clients can discover it
// without switching the device into a separate OTA-only BLE mode first.
bledfu.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM);
if (bledfu.begin() != ERROR_NONE) {
instance = nullptr;
BLE_DEBUG_PRINTLN("Bluetooth DFU service begin failed");
return false;
_mota_service.setPermission(SECMODE_ENC_WITH_MITM,
SECMODE_ENC_WITH_MITM);
if (_mota_service.begin() == ERROR_NONE) {
_mota_request.setProperties(CHR_PROPS_NOTIFY);
_mota_request.setPermission(SECMODE_ENC_WITH_MITM,
SECMODE_NO_ACCESS);
_mota_request.setMaxLen(mesh::ota::BLE_MOTA_REQUEST_MAX);
_mota_request.setUserDescriptor("mOTA device request");
if (_mota_request.begin() == ERROR_NONE) {
_mota_response.setProperties(CHR_PROPS_WRITE);
_mota_response.setPermission(SECMODE_NO_ACCESS,
SECMODE_ENC_WITH_MITM);
_mota_response.setMaxLen(mesh::ota::BLE_MOTA_RESPONSE_MAX);
_mota_response.setUserDescriptor("mOTA host response");
_mota_response.setWriteCallback(onMotaResponse);
_mota_available = _mota_response.begin() == ERROR_NONE;
}
}
if (!_mota_available) {
// GATT attributes cannot be removed after Bluefruit starts, so do not
// retry here. Continuing with the already-registered UART service keeps
// the phone and USB recovery paths available; a corrected image can add
// the optional mOTA service on the next reboot.
mesh::usbLoggingPort().println(
"Bluetooth mOTA unavailable; normal Companion Bluetooth continues");
}
#endif
Bluefruit.Advertising.setType(
BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED);
@@ -985,7 +1017,8 @@ bool SerialBLEInterface::isConnected() const {
#if COMPANION_FEATURE_BLE_MOTA_SOURCE
bool SerialBLEInterface::isMotaChannelReady() {
return isConnected() && _conn_handle != BLE_CONN_HANDLE_INVALID
return _mota_available && isConnected()
&& _conn_handle != BLE_CONN_HANDLE_INVALID
&& _mota_request.notifyEnabled(_conn_handle);
}
#endif
+38
View File
@@ -16,8 +16,38 @@
#define BLE_TX_POWER 4
#endif
// Bluefruit's BANDWIDTH_MAX preset reserves a 100-unit connection event and
// three notification buffers. That is useful on boards with roomy RAM, but
// it exceeds the usable SoftDevice reservation of some bootloader layouts.
// Keep the existing preset as the default while allowing constrained boards
// to retain the 247-byte MTU with a smaller event/buffer reservation.
#ifndef COMPANION_BLE_PRPH_MTU
#define COMPANION_BLE_PRPH_MTU 247
#endif
#ifndef COMPANION_BLE_PRPH_EVENT_LENGTH
#define COMPANION_BLE_PRPH_EVENT_LENGTH 100
#endif
#ifndef COMPANION_BLE_PRPH_HVN_QUEUE
#define COMPANION_BLE_PRPH_HVN_QUEUE 3
#endif
#ifndef COMPANION_BLE_PRPH_WRCMD_QUEUE
#define COMPANION_BLE_PRPH_WRCMD_QUEUE 1
#endif
// Nordic UART is the required Companion service. The legacy Nordic DFU
// service is an optional extension that some constrained field combinations
// cannot register alongside the Companion GATT table.
#ifndef COMPANION_FEATURE_BLE_DFU
#define COMPANION_FEATURE_BLE_DFU 1
#endif
class SerialBLEInterface : public BaseSerialInterface {
#if COMPANION_FEATURE_BLE_DFU
BLEDfu bledfu;
#endif
BLEUart bleuart;
#if COMPANION_FEATURE_BLE_MOTA_SOURCE
BLEService _mota_service = BLEService(mesh::ota::BLE_MOTA_SERVICE_UUID);
@@ -26,10 +56,15 @@ class SerialBLEInterface : public BaseSerialInterface {
BLECharacteristic _mota_response =
BLECharacteristic(mesh::ota::BLE_MOTA_RESPONSE_UUID);
mesh::ota::BleMotaStream _mota_stream;
// The normal Nordic UART service is the Companion transport. mOTA is an
// optional Full-Companion extension and must never take that transport down
// if a fielded SoftDevice rejects one of its extra GATT attributes.
bool _mota_available = false;
#endif
bool _isEnabled;
bool _begin_attempted = false;
bool _begin_ready = false;
char _begin_failure[96] = {};
bool _isDeviceConnected;
uint16_t _conn_handle;
unsigned long _last_health_check;
@@ -129,6 +164,9 @@ public:
bool clear_bonds = false, bool stealth_pair_once = false,
const mesh::companion::BluetoothPeerIdentity*
bonded_only_peer = nullptr);
// Kept after a failed begin so a normal Companion terminal can report the
// cause even when early USB logging was not available.
const char* beginFailure() const { return _begin_failure; }
void disconnect();
void enable() override;