Expose live nRF52 advertiser and SoftDevice state in opt-in trace

This commit is contained in:
mikecarper committed 2026-10-19 20:38:12 -07:00
1 parent e0442880e1
commit 59e2b5bfe4
3 files changed
+339

No files matched your search

+10
View File
@@ -32,6 +32,8 @@ extern "C" { extern MeshBleTraceBuffer mesh_ble_trace; }
void meshBleTraceState(char* reply, size_t capacity);
void meshBleTraceIdentity(char* reply, size_t capacity);
void meshBleTraceAdvertising(char* reply, size_t capacity);
void meshBleTraceAdvertiser(char* reply, size_t capacity);
void meshBleTraceStack(char* reply, size_t capacity);
static inline void meshBleTraceMax(std::atomic<uint32_t>& value, uint32_t sample) {
uint32_t old = value.load(std::memory_order_relaxed);
while (sample > old && !value.compare_exchange_weak(old, sample,
@@ -79,6 +81,14 @@ static inline void meshBleTraceDispatchExit(uint32_t event, uint32_t handle) {
}
static inline bool meshBleTraceCommand(const char* command, char* reply,
size_t capacity) {
if (!strcmp(command, "get bluetooth.trace.stack")) {
meshBleTraceStack(reply, capacity);
return true;
}
if (!strcmp(command, "get bluetooth.trace.advertiser")) {
meshBleTraceAdvertiser(reply, capacity);
return true;
}
if (!strcmp(command, "get bluetooth.trace.advertising")
|| !strcmp(command, "get ble.trace.advertising")) {
meshBleTraceAdvertising(reply, capacity);
+66
View File
@@ -95,6 +95,72 @@ void meshBleTraceIdentity(char* reply, size_t capacity) {
(unsigned)Bluefruit.ScanResponse.count());
}
void meshBleTraceStack(char* reply, size_t capacity) {
uint8_t enabled = 0;
if (!instance || mesh_nrf52::softdeviceIsEnabled(enabled) != NRF_SUCCESS || !enabled) {
snprintf(reply, capacity, "> Bluetooth not initialized");
return;
}
// Query the running stack: a build's SDK setting or bootloader version
// label alone cannot prove which SoftDevice firmware is installed.
ble_version_t version = {};
const uint32_t status = sd_ble_version_get(&version);
__asm__ __volatile__("" ::: "memory");
if (status != NRF_SUCCESS) {
snprintf(reply, capacity, "> status=%lu", (unsigned long)status);
return;
}
snprintf(reply, capacity, "> status=0,link_layer=%u,company=%u,fwid=0x%04x",
(unsigned)version.version_number, (unsigned)version.company_id,
(unsigned)version.subversion_number);
}
void meshBleTraceAdvertiser(char* reply, size_t capacity) {
uint8_t enabled = 0;
if (!instance || mesh_nrf52::softdeviceIsEnabled(enabled) != NRF_SUCCESS || !enabled) {
snprintf(reply, capacity, "> Bluetooth not initialized");
return;
}
// Query the SoftDevice rather than Bluefruit's cached running flag. Its
// advertising handle is private; bounded read-only enumeration avoids
// assuming its value or depending on the framework's object layout.
unsigned active = 0, idle = 0, invalid = 0;
unsigned first_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
uint32_t unexpected = NRF_SUCCESS;
ble_gap_addr_t first_address = {};
for (unsigned handle = 0; handle < BLE_GAP_ADV_SET_HANDLE_NOT_SET; ++handle) {
ble_gap_addr_t address = {};
const uint32_t status = sd_ble_gap_adv_addr_get((uint8_t)handle, &address);
__asm__ __volatile__("" ::: "memory");
if (status == NRF_SUCCESS) {
if (!active) {
first_handle = handle;
first_address = address;
}
++active;
} else if (status == NRF_ERROR_INVALID_STATE) {
++idle;
} else if (status == BLE_ERROR_INVALID_ADV_HANDLE) {
++invalid;
} else {
unexpected = status;
break;
}
}
ble_gap_privacy_params_t privacy = {};
const uint32_t privacy_status = sd_ble_gap_privacy_get(&privacy);
__asm__ __volatile__("" ::: "memory");
snprintf(reply, capacity,
"> cached=%u,active=%u,idle=%u,invalid=%u,error=%lu,h=%u,t=%u,mac=%02x%02x%02x%02x%02x%02x,privacy_status=%lu,privacy=%u,cycle=%u",
(unsigned)Bluefruit.Advertising.isRunning(), active, idle, invalid,
(unsigned long)unexpected, first_handle, (unsigned)first_address.addr_type,
first_address.addr[5], first_address.addr[4], first_address.addr[3],
first_address.addr[2], first_address.addr[1], first_address.addr[0],
(unsigned long)privacy_status,
privacy_status == NRF_SUCCESS ? (unsigned)privacy.privacy_mode : 0u,
privacy_status == NRF_SUCCESS ? (unsigned)privacy.private_addr_cycle_s : 0u);
}
void meshBleTraceAdvertising(char* reply, size_t capacity) {
if (!instance) {
snprintf(reply, capacity, "> Bluetooth not initialized");