fix(companion): recover radio profile settings after flash

This commit is contained in:
mikecarper committed 2026-09-21 18:21:36 -07:00
1 parent 6b62fe4f8b
commit 7bad5f9b30
7 files changed
+139 -68

No files matched your search

-20
View File
@@ -4,17 +4,6 @@
#include <FreeRTOS.h>
#include <task.h>
#include <string.h>
#include <helpers/nrf52/BleTaskStartup.h>
namespace mesh {
namespace nrf52 {
static uint8_t ble_task_mask;
void resetBleTaskStartup() { ble_task_mask = 0; }
bool bleTasksStarted() { return ble_task_mask == 3; }
} // namespace nrf52
} // namespace mesh
#ifndef MESH_NRF52_LOOP_STACK_WORDS
#define MESH_NRF52_LOOP_STACK_WORDS 2048
#endif
@@ -44,15 +33,6 @@ extern "C" BaseType_t __wrap_xTaskCreate(
}
const BaseType_t result = __real_xTaskCreate(
task_code, task_name, adjusted_depth, parameters, priority, created_task);
if (task_name != NULL) {
const uint8_t mask = strcmp(task_name, "BLE") == 0 ? 1
: strcmp(task_name, "SOC") == 0 ? 2 : 0;
if (result == pdPASS) {
mesh::nrf52::ble_task_mask |= mask;
} else {
mesh::nrf52::ble_task_mask &= ~mask;
}
}
return result;
}
+61 -10
View File
@@ -56,23 +56,34 @@ uint32_t remainingMillis(uint32_t end, uint32_t now) {
}
}
bool RadioProfileCLI::readImage(const char* path, uint8_t* bytes, size_t size) {
RadioProfileCLI::ImageReadResult RadioProfileCLI::readImage(
const char* path, uint8_t* bytes, size_t size) {
if (!fs_) return ImageReadResult::Unreadable;
#if defined(NRF52_PLATFORM)
File file(*fs_);
if (!file.open(path, FILE_O_READ)) return false;
if (!file.open(path, FILE_O_READ)) {
return fs_->exists(path) ? ImageReadResult::Unreadable
: ImageReadResult::Missing;
}
#elif defined(STM32_PLATFORM)
File file = fs_->open(path, FILE_O_READ);
#else
File file = fs_->open(path, "r");
#endif
if (!file) return false;
bool ok = file.size() == size && file.read(bytes, size) == (int)size;
if (!file) {
return fs_->exists(path) ? ImageReadResult::Unreadable
: ImageReadResult::Missing;
}
const bool right_size = file.size() == size;
const bool read_complete = right_size && file.read(bytes, size) == (int)size;
file.close();
if (!ok) return false;
if (!right_size) return ImageReadResult::Invalid;
if (!read_complete) return ImageReadResult::Unreadable;
uint32_t stored;
memcpy(&stored, bytes + size - 4, 4);
return bytes[0] == 'R' && bytes[1] == '2' && bytes[2] == 1
&& stored == checksum(bytes, size - 4);
&& stored == checksum(bytes, size - 4)
? ImageReadResult::Valid : ImageReadResult::Invalid;
}
bool RadioProfileCLI::writeImage(const char* path, const uint8_t* bytes, size_t size) {
@@ -90,13 +101,33 @@ bool RadioProfileCLI::writeImage(const char* path, const uint8_t* bytes, size_t
file.flush();
file.close();
uint8_t verify[ImageSize];
return ok && readImage(path, verify, size) && memcmp(bytes, verify, size) == 0;
return ok && readImage(path, verify, size) == ImageReadResult::Valid
&& memcmp(bytes, verify, size) == 0;
}
bool RadioProfileCLI::save(const RadioProfileConfig& config, uint16_t preamble, RadioCrossMode cross) {
if (!discardCorruptSavedImagesForWrite()) return false;
return prepareSavedImage(TempPath, config, preamble, cross) && commitSavedImage(TempPath);
}
bool RadioProfileCLI::discardCorruptSavedImagesForWrite() {
if (!hold_) return true;
if (!fs_ || !recoverable_corrupt_store_) return false;
// This is deliberately a user-triggered recovery, never a boot-time cleanup.
// The flag is set only after every present candidate was read completely and
// proved invalid, so no valid radio2 configuration is discarded. This store
// contains only radio-profile settings; node identity, contacts, and normal
// Companion preferences live elsewhere.
const char* const paths[] = {ImagePath, BackupPath};
for (const char* path : paths) {
if (fs_->exists(path) && !fs_->remove(path)) return false;
}
hold_ = false;
recoverable_corrupt_store_ = false;
return true;
}
bool RadioProfileCLI::prepareSavedImage(const char* path, const RadioProfileConfig& config,
uint16_t preamble, RadioCrossMode cross) {
if (!fs_ || hold_) return false;
@@ -129,6 +160,8 @@ bool RadioProfileCLI::commitSavedImage(const char* path) {
void RadioProfileCLI::begin(FILESYSTEM* fs, Radio* radio, RTCClock* rtc, bool infrastructure_replies) {
fs_ = fs; radio_ = radio; rtc_ = rtc; last_ms_ = millis();
hold_ = false;
recoverable_corrupt_store_ = false;
infrastructure_replies_ = infrastructure_replies;
if (radio_ && radio_->profiles()) {
radio_->profiles()->reply_tx = infrastructure_replies ? RADIO_TX_BOTH : RADIO_TX_AUTO;
@@ -136,8 +169,11 @@ void RadioProfileCLI::begin(FILESYSTEM* fs, Radio* radio, RTCClock* rtc, bool in
}
if (!fs_ || !radio_ || !radio_->profiles()) return;
uint8_t bytes[ImageSize];
bool loaded = readImage(ImagePath, bytes, sizeof(bytes));
if (!loaded && readImage(BackupPath, bytes, sizeof(bytes))) {
const ImageReadResult primary_image = readImage(ImagePath, bytes, sizeof(bytes));
bool loaded = primary_image == ImageReadResult::Valid;
ImageReadResult backup_image = ImageReadResult::Missing;
if (!loaded) backup_image = readImage(BackupPath, bytes, sizeof(bytes));
if (!loaded && backup_image == ImageReadResult::Valid) {
// Use the verified backup even when repairing the interrupted save is
// impossible. Keep writes held so the sole committed image stays intact.
loaded = true;
@@ -145,7 +181,15 @@ void RadioProfileCLI::begin(FILESYSTEM* fs, Radio* radio, RTCClock* rtc, bool in
|| !fs_->rename(BackupPath, ImagePath);
}
if (!loaded) {
hold_ = fs_->exists(ImagePath) || fs_->exists(BackupPath);
hold_ = primary_image != ImageReadResult::Missing
|| backup_image != ImageReadResult::Missing;
// An unreadable file may be a transient filesystem fault. Do not remove
// it. A later explicit setter can recreate only files proven corrupt.
recoverable_corrupt_store_ = hold_
&& primary_image != ImageReadResult::Unreadable
&& backup_image != ImageReadResult::Unreadable
&& (primary_image == ImageReadResult::Invalid
|| backup_image == ImageReadResult::Invalid);
return;
}
saved_.mode = (RadioProfileMode)bytes[3];
@@ -159,7 +203,14 @@ void RadioProfileCLI::begin(FILESYSTEM* fs, Radio* radio, RTCClock* rtc, bool in
if ((uint8_t)saved_.mode > 2 || (uint8_t)cross_ > 2
|| (saved_.mode != RadioProfileMode::Off && !radio_->validateProfile(saved_.params))
|| (primary_preamble_ && (primary_preamble_ < 8 || primary_preamble_ > RadioProfiles::MaxPreamble))) {
// A CRC-valid image can still be semantically stale (for example, after
// an older build wrote a now-unsupported value). Do not recreate it if a
// structurally valid backup exists; that backup is the last known-good
// radio-profile image and must remain available for manual recovery.
const ImageReadResult backup_image = readImage(BackupPath, bytes, sizeof(bytes));
hold_ = true; saved_ = {}; primary_preamble_ = 0; cross_ = RadioCrossMode::Auto;
recoverable_corrupt_store_ = backup_image != ImageReadResult::Valid
&& backup_image != ImageReadResult::Unreadable;
reply_setting_ = 0; // reject the entire image, including its reply override
return;
}
+6 -1
View File
@@ -36,7 +36,11 @@ class RadioProfileCLI {
uint32_t schedule_retry_ms_ = 0;
bool temp_pending_ = false, temp_active_ = false;
bool hold_ = false;
// Set only after the active image is conclusively malformed and there is no
// readable backup. Read/I/O failures and an intact backup remain read-only.
bool recoverable_corrupt_store_ = false;
bool publish_pending_ = false;
enum class ImageReadResult : uint8_t { Missing, Valid, Invalid, Unreadable };
enum class RemoteMutation : uint8_t { None, Saved, Temporary, Off, TempOff, DeleteTemp };
RemoteMutation remote_mutation_ = RemoteMutation::None;
RadioProfileConfig remote_config_;
@@ -51,8 +55,9 @@ class RadioProfileCLI {
bool prepareSavedImage(const char* path, const RadioProfileConfig& config,
uint16_t preamble, RadioCrossMode cross);
bool commitSavedImage(const char* path);
bool discardCorruptSavedImagesForWrite();
bool applyReplyMutation();
bool readImage(const char* path, uint8_t* bytes, size_t size);
ImageReadResult readImage(const char* path, uint8_t* bytes, size_t size);
bool writeImage(const char* path, const uint8_t* bytes, size_t size);
void publish();
void formatConfig(char* reply, size_t capacity, const RadioProfileConfig& config,
-13
View File
@@ -1,13 +0,0 @@
#pragma once
namespace mesh {
namespace nrf52 {
// The pinned Bluefruit core does not check xTaskCreate's return value for its
// BLE and SOC workers. Nrf52LoopStack.cpp observes those calls through the
// existing linker wrapper so a nominally successful begin cannot hide OOM.
void resetBleTaskStartup();
bool bleTasksStarted();
} // namespace nrf52
} // namespace mesh
+7 -4
View File
@@ -1,5 +1,4 @@
#include "SerialBLEInterface.h"
#include "BleTaskStartup.h"
#include "../BluetoothMac.h"
#include "../CompanionFrameQueue.h"
#include <stdio.h>
@@ -424,11 +423,15 @@ 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);
mesh::nrf52::resetBleTaskStartup();
if (!Bluefruit.begin() || !mesh::nrf52::bleTasksStarted()) {
// 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()) {
instance = nullptr;
mesh::usbLoggingPort().println(
"Bluetooth startup failed (SoftDevice/tasks); check runtime heap and reboot");
"Bluetooth startup failed; reboot required");
return false;
}