fix: keep unavailable radios out of identity and settings recovery
This commit is contained in:
1 parent
076b2a3208
commit
40a4161782
6 files changed
+202
-6
No files matched your search
@@ -468,6 +468,7 @@ jobs:
|
||||
python3 -B test/test_companion_channel_storage.py -v
|
||||
python3 -B test/test_nrf52_extrafs_contract.py -v
|
||||
python3 -B test/test_xiao_qspi_recovery.py -v
|
||||
python3 -B test/test_xiao_identity_recovery.py -v
|
||||
python3 -B test/test_companion_ota_config.py -v
|
||||
python3 -B test/test_ota_identity_policy.py -v
|
||||
python3 -B test/test_companion_radio_gain_restore.py -v
|
||||
|
||||
@@ -11529,6 +11529,7 @@ void MyMesh::loop() {
|
||||
serviceBinaryTraceReply();
|
||||
if (!mesh::morseTimingActive(_radio)) serviceSigningSession();
|
||||
if (!command_radio_apply_pending && saved_radio_apply_pending && !hasOutbound()
|
||||
&& _radio_available
|
||||
#if COMPANION_FEATURE_TEMP_RADIO
|
||||
&& !_temp_radio_applied && _temp_radio_set_at == 0
|
||||
&& _temp_radio_revert_at == 0
|
||||
@@ -11717,7 +11718,7 @@ mesh::powerdiag::Mask MyMesh::pendingWorkReason() const {
|
||||
if (sign_data != NULL) return mask(Reason::Mesh);
|
||||
if (hasQueuedWorkDue()) return mask(Reason::Queue);
|
||||
if (hasRetryWorkDue()) return mask(Reason::Retry);
|
||||
if (saved_radio_apply_pending
|
||||
if (saved_radio_apply_pending && _radio_available
|
||||
#if COMPANION_FEATURE_TEMP_RADIO
|
||||
// Match loop()'s restore gate: a lease-blocked rollback is not
|
||||
// runnable work. The lease's own timers below wake it when due.
|
||||
|
||||
@@ -41,12 +41,13 @@ struct Board {
|
||||
bool isLoRaFemLnaEnabled(){return false;}
|
||||
} board;
|
||||
struct Driver {
|
||||
bool gain=false,supported=true;
|
||||
bool gain=false,supported=true,hardware_present=true;
|
||||
int power=0;
|
||||
unsigned gain_calls=0,power_calls=0;
|
||||
std::deque<bool> gain_results,power_results;
|
||||
bool supportsRxBoostedGainMode(){return supported;}
|
||||
bool setRxBoostedGainMode(bool value){
|
||||
assert(hardware_present);
|
||||
++gain_calls;
|
||||
bool ok=nextResult(gain_results,true);
|
||||
if(ok)gain=value;
|
||||
@@ -54,13 +55,14 @@ struct Driver {
|
||||
}
|
||||
bool getRxBoostedGainMode(){return gain;}
|
||||
bool setTxPower(int value){
|
||||
assert(hardware_present);
|
||||
++power_calls;
|
||||
bool ok=nextResult(power_results,true);
|
||||
if(ok)power=value;
|
||||
return ok;
|
||||
}
|
||||
bool supportsRxPowerSaving(){return false;}
|
||||
bool setRxPowerSaving(bool,uint32_t,uint32_t){return true;}
|
||||
bool setRxPowerSaving(bool,uint32_t,uint32_t){assert(hardware_present);return true;}
|
||||
void setCADScanTimeoutMillis(int){}
|
||||
} radio_driver;
|
||||
struct Radio {
|
||||
@@ -87,6 +89,8 @@ struct MyMesh {
|
||||
bool millisHasNowPassed(uint32_t at){return int32_t(now_ms-at)>0;}
|
||||
uint32_t futureMillis(uint32_t delay){return now_ms+delay;}
|
||||
Apply tryApplyRadioParams(float freq,float,uint8_t,uint8_t,bool=false,uint16_t=0){
|
||||
if(!_radio_available)return Apply::FAILED;
|
||||
assert(radio_driver.hardware_present);
|
||||
++applies;
|
||||
Apply result=nextResult(apply_results,Apply::APPLIED);
|
||||
if(result==Apply::APPLIED)live_freq=freq;
|
||||
@@ -141,6 +145,22 @@ static void checkStartupAndRetry(){
|
||||
m.configureRadioFromPrefs();
|
||||
assert(!m.saved_radio_apply_pending && !m.applies && !radio_driver.gain_calls);
|
||||
}
|
||||
{
|
||||
// A rejected command may retain saved settings while hardware is absent.
|
||||
// Do not touch the missing radio or consume that intent during retries.
|
||||
MyMesh m;m._radio_available=false;radio_driver.hardware_present=false;
|
||||
m._prefs.rx_boosted_gain=1;m.saved_radio_apply_pending=true;
|
||||
for(int i=0;i<3;++i){
|
||||
m.recover();now_ms+=1000;
|
||||
assert(m.saved_radio_apply_pending && !m.radio_apply_retry_at && !m.radio_apply_failures);
|
||||
assert(!m.applies && !radio_driver.gain_calls && !radio_driver.power_calls);
|
||||
}
|
||||
m._radio_available=true;radio_driver.hardware_present=true;
|
||||
m.recover();
|
||||
assert(!m.saved_radio_apply_pending && radio_driver.gain);
|
||||
assert(m.live_freq==m._prefs.freq && radio_driver.power==m._prefs.tx_power_dbm);
|
||||
assert(m.applies==1 && radio_driver.gain_calls==1 && radio_driver.power_calls==1 && !m.saves);
|
||||
}
|
||||
{
|
||||
MyMesh m;radio_driver.supported=false;radio_driver.gain_results={false};
|
||||
m.configureRadioFromPrefs();
|
||||
|
||||
@@ -289,6 +289,12 @@ int main() {
|
||||
assert(!m.hasPendingWork()); // A source flag alone must not keep it awake.
|
||||
m.store._contact_recovery=0;
|
||||
m.saved_radio_apply_pending=true;
|
||||
m._radio_available=false;
|
||||
assert(!m.hasPendingWork()); // Absent hardware cannot service the saved intent.
|
||||
m.radio_apply_retry_at=now_ms;
|
||||
assert(!m.hasPendingWork()); // An overdue retry must not spin in recovery mode.
|
||||
m._radio_available=true;
|
||||
m.radio_apply_retry_at=0;
|
||||
assert(m.hasPendingWork());
|
||||
m.radio_apply_retry_at=now_ms+10;
|
||||
assert(!m.hasPendingWork());
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
"""Execute XIAO's actual target source with a radio that forbids absent I/O."""
|
||||
from pathlib import Path
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
IDENTITY = r"""#pragma once
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
namespace mesh {
|
||||
struct RNG { virtual ~RNG() = default; virtual void random(uint8_t*, size_t) = 0; };
|
||||
struct Identity { uint8_t pub_key[32] = {}; };
|
||||
struct LocalIdentity : Identity {
|
||||
LocalIdentity() = default;
|
||||
explicit LocalIdentity(RNG* rng) { rng->random(pub_key, sizeof(pub_key)); }
|
||||
};
|
||||
}
|
||||
"""
|
||||
|
||||
TARGET = r"""#pragma once
|
||||
#include <helpers/IdentityGeneration.h>
|
||||
#include <helpers/NRF52Crypto.h>
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
int SPI, Wire;
|
||||
unsigned radio_bytes, prng_bytes, crypto_calls, progress_calls, init_calls, rtc_calls;
|
||||
long random(long minimum, long maximum) {
|
||||
assert(minimum == 0 && maximum == 256); ++prng_bytes; return 0x3c;
|
||||
}
|
||||
class PhysicalLayer {
|
||||
public:
|
||||
bool available = false;
|
||||
uint8_t randomByte() {
|
||||
if (!available) throw std::logic_error("absent radio I/O");
|
||||
++radio_bytes; return 0x5a;
|
||||
}
|
||||
};
|
||||
@RADIO_NOISE_LISTENER@
|
||||
struct Module { Module(int, int, int, int, int&) {} };
|
||||
struct FakeRadio : PhysicalLayer {
|
||||
std::unique_ptr<Module> module;
|
||||
bool next_result = false;
|
||||
FakeRadio(Module* m) : module(m) {}
|
||||
bool std_init(int* spi) {
|
||||
assert(spi == &SPI); ++init_calls; available = next_result; return next_result;
|
||||
}
|
||||
};
|
||||
struct XiaoNrf52Board {};
|
||||
struct FakeWrapper { FakeWrapper(FakeRadio&, XiaoNrf52Board&) {} };
|
||||
struct VolatileRTCClock {};
|
||||
struct AutoDiscoverRTCClock {
|
||||
AutoDiscoverRTCClock(VolatileRTCClock&) {}
|
||||
void begin(int& wire) { assert(&wire == &Wire); ++rtc_calls; }
|
||||
};
|
||||
struct EnvironmentSensorManager {};
|
||||
#define RADIO_CLASS FakeRadio
|
||||
#define WRAPPER_CLASS FakeWrapper
|
||||
#define P_LORA_NSS 4
|
||||
#define P_LORA_DIO_1 1
|
||||
#define P_LORA_RESET 2
|
||||
#define P_LORA_BUSY 3
|
||||
"""
|
||||
|
||||
HARNESS = r"""
|
||||
#include "target.cpp"
|
||||
#ifdef USE_CC310_HW_CRYPTO
|
||||
namespace mesh {
|
||||
void mixCC310Random(uint8_t* dest, size_t size) {
|
||||
++crypto_calls;
|
||||
for (size_t i = 0; i < size; ++i) dest[i] ^= 0xa5;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
static void progress(void*) { ++progress_calls; }
|
||||
static void freshIdentity(bool radio_ready) {
|
||||
radio_bytes = prng_bytes = crypto_calls = progress_calls = 0;
|
||||
mesh::LocalIdentity identity;
|
||||
mesh::ScopedIdentityGenerationProgress hook(progress, nullptr);
|
||||
assert(mesh::generateUsableLocalIdentity(identity, radio_new_identity));
|
||||
assert(prng_bytes == 32 && radio_bytes == (radio_ready ? 32u : 0u));
|
||||
assert(progress_calls == 34); // Before/after generation plus every RNG byte.
|
||||
uint8_t expected = radio_ready ? 0x66 : 0x3c;
|
||||
#ifdef USE_CC310_HW_CRYPTO
|
||||
expected ^= 0xa5;
|
||||
assert(crypto_calls == 1);
|
||||
#else
|
||||
assert(crypto_calls == 0);
|
||||
#endif
|
||||
for (uint8_t value : identity.pub_key) assert(value == expected);
|
||||
}
|
||||
int main() {
|
||||
freshIdentity(false); // No successful initialization has occurred yet.
|
||||
radio.next_result = true; assert(radio_init()); freshIdentity(true);
|
||||
radio.next_result = false; assert(!radio_init()); freshIdentity(false);
|
||||
radio.next_result = true; assert(radio_init()); freshIdentity(true);
|
||||
assert(init_calls == 3);
|
||||
#ifdef DISABLE_I2C_RTC_SCAN
|
||||
assert(rtc_calls == 0);
|
||||
#else
|
||||
assert(rtc_calls == 3);
|
||||
#endif
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
class XiaoIdentityRecoveryTests(unittest.TestCase):
|
||||
def test_absent_ready_failed_and_recovered_with_both_crypto_configs(self):
|
||||
compiler = os.environ.get("CXX") or shutil.which("c++")
|
||||
self.assertIsNotNone(compiler)
|
||||
wrappers = (ROOT / "src/helpers/radiolib/RadioLibWrappers.h").read_text()
|
||||
start = wrappers.index("class RadioNoiseListener :")
|
||||
listener = wrappers[start:wrappers.index("\n};", start) + 3]
|
||||
for crypto in (False, True):
|
||||
for rtc_scan in (False, True):
|
||||
with self.subTest(crypto=crypto, rtc_scan=rtc_scan), tempfile.TemporaryDirectory(prefix="xiao-identity-") as temp:
|
||||
work = Path(temp)
|
||||
(work / "helpers").mkdir()
|
||||
(work / "Arduino.h").write_text("#pragma once\n", encoding="ascii")
|
||||
(work / "Identity.h").write_text(IDENTITY, encoding="ascii")
|
||||
(work / "helpers/ArduinoHelpers.h").write_text("#pragma once\n", encoding="ascii")
|
||||
(work / "target.h").write_text(TARGET.replace("@RADIO_NOISE_LISTENER@", listener), encoding="ascii")
|
||||
(work / "target.cpp").write_bytes((ROOT / "variants/xiao_nrf52/target.cpp").read_bytes())
|
||||
(work / "check.cpp").write_text(HARNESS, encoding="ascii")
|
||||
flags = ["-std=c++17", "-Wall", "-Wextra", "-Werror", "-I", str(work), "-I", str(ROOT / "src")]
|
||||
if os.name != "nt":
|
||||
flags += ["-fsanitize=address,undefined", "-fno-pie", "-no-pie"]
|
||||
if crypto:
|
||||
flags += ["-DUSE_CC310_HW_CRYPTO=1"]
|
||||
if not rtc_scan:
|
||||
flags += ["-DDISABLE_I2C_RTC_SCAN=1"]
|
||||
binary = work / "check"
|
||||
built = subprocess.run([compiler, *flags, str(work / "check.cpp"), "-o", str(binary)], capture_output=True, text=True)
|
||||
self.assertEqual(built.returncode, 0, built.stderr)
|
||||
result = subprocess.run([str(binary)], capture_output=True, text=True)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -1,6 +1,11 @@
|
||||
#include <Arduino.h>
|
||||
#include "target.h"
|
||||
#include <helpers/ArduinoHelpers.h>
|
||||
#ifdef USE_CC310_HW_CRYPTO
|
||||
#include <helpers/NRF52Crypto.h>
|
||||
#endif
|
||||
|
||||
static bool target_radio_available = false;
|
||||
|
||||
#ifdef DISPLAY_CLASS
|
||||
DISPLAY_CLASS display;
|
||||
@@ -27,10 +32,29 @@ bool radio_init() {
|
||||
rtc_clock.begin(Wire);
|
||||
#endif
|
||||
|
||||
return radio.std_init(&SPI);
|
||||
target_radio_available = radio.std_init(&SPI);
|
||||
return target_radio_available;
|
||||
}
|
||||
|
||||
mesh::LocalIdentity radio_new_identity() {
|
||||
RadioNoiseListener rng(radio);
|
||||
return mesh::LocalIdentity(&rng); // create new random identity
|
||||
if (target_radio_available) {
|
||||
RadioNoiseListener rng(radio);
|
||||
return mesh::LocalIdentity(&rng);
|
||||
}
|
||||
|
||||
// Recovery-mode identity creation must not access a failed/absent carrier.
|
||||
// Preserve the seeded PRNG and independent CryptoCell entropy used by RAK3401.
|
||||
class XiaoIdentityRNG : public mesh::RNG {
|
||||
public:
|
||||
void random(uint8_t* dest, size_t size) override {
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
dest[i] = static_cast<uint8_t>(::random(0, 256));
|
||||
mesh::serviceIdentityGenerationProgress();
|
||||
}
|
||||
#ifdef USE_CC310_HW_CRYPTO
|
||||
mesh::mixCC310Random(dest, size);
|
||||
#endif
|
||||
}
|
||||
} rng;
|
||||
return mesh::LocalIdentity(&rng);
|
||||
}
|
||||
Reference in new issue
Block a user