Repeater, room server, sensor: set radio.rxps off takes effect at once, and reception restarts after the noise-floor calibration
Changelog-cs: Repeater, room server, sensor: `set radio.rxps off` platí hned a příjem se po kalibraci šumu znovu nastartuje Changelog-topic: rxps-stall-46
This commit is contained in:
1 parent
6da05693a0
commit
63823f526b
11 files changed
+382
-25
No files matched your search
@@ -0,0 +1,238 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <Arduino.h>
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
// Minimal Arduino/LoRa shims needed to compile RadioLibWrappers.cpp on host.
|
||||
#define WITH_RX_POWERSAVING 1
|
||||
#define LORA_SF 8
|
||||
#ifndef noInterrupts
|
||||
#define noInterrupts() ((void)0)
|
||||
#endif
|
||||
#ifndef interrupts
|
||||
#define interrupts() ((void)0)
|
||||
#endif
|
||||
using std::min;
|
||||
using std::max;
|
||||
inline long random(long lo, long hi) { return lo + (hi > lo ? 0 : 0); }
|
||||
|
||||
#include "helpers/radiolib/RXPowerSaving.cpp"
|
||||
#include "helpers/radiolib/RadioLibWrappers.cpp"
|
||||
|
||||
namespace {
|
||||
|
||||
class MockBoard : public mesh::MainBoard {
|
||||
public:
|
||||
uint8_t getStartupReason() const override { return 0; }
|
||||
uint16_t getBattMilliVolts() override { return 3800; }
|
||||
const char* getManufacturerName() const override { return "mock"; }
|
||||
void reboot() override {}
|
||||
};
|
||||
|
||||
class MockRadio : public PhysicalLayer {
|
||||
public:
|
||||
void (*isr_fn)(void) = nullptr;
|
||||
RadioLibIrqFlags_t irq_flags = 0;
|
||||
size_t next_pkt_len = 0;
|
||||
int16_t read_data_err = RADIOLIB_ERR_NONE;
|
||||
bool read_data_saw_irq_intact = false;
|
||||
int clear_irq_calls = 0;
|
||||
int standby_calls = 0;
|
||||
int start_receive_calls = 0;
|
||||
int read_data_calls = 0;
|
||||
|
||||
void setPacketReceivedAction(void (*func)(void)) override { isr_fn = func; }
|
||||
void fireDio1() { if (isr_fn) isr_fn(); }
|
||||
|
||||
int16_t standby() override {
|
||||
standby_calls++;
|
||||
return RADIOLIB_ERR_NONE;
|
||||
}
|
||||
int16_t clearIrqFlags(RadioLibIrqFlags_t irq) override {
|
||||
clear_irq_calls++;
|
||||
irq_flags &= ~irq;
|
||||
return RADIOLIB_ERR_NONE;
|
||||
}
|
||||
RadioLibIrqFlags_t getIrqFlags() override { return irq_flags; }
|
||||
bool checkIrq(uint8_t irq) override { return (irq_flags & (1UL << irq)) != 0; }
|
||||
int16_t startReceive() override {
|
||||
start_receive_calls++;
|
||||
irq_flags = 0;
|
||||
return RADIOLIB_ERR_NONE;
|
||||
}
|
||||
size_t getPacketLength(bool) override { return next_pkt_len; }
|
||||
int16_t readData(uint8_t* data, size_t len) override {
|
||||
read_data_calls++;
|
||||
read_data_saw_irq_intact = (irq_flags != 0);
|
||||
if (len > 0) memset(data, 0xAA, len);
|
||||
irq_flags = 0;
|
||||
return read_data_err;
|
||||
}
|
||||
};
|
||||
|
||||
class TestRxpsWrapper : public RadioLibWrapper {
|
||||
public:
|
||||
bool receiving_latch = false;
|
||||
int reset_receiving_calls = 0;
|
||||
int arm_duty_cycle_calls = 0;
|
||||
int stop_duty_cycle_hw_calls = 0;
|
||||
int start_cont_rx_calls = 0;
|
||||
|
||||
TestRxpsWrapper(MockRadio& r, MockBoard& b) : RadioLibWrapper(r, b) {}
|
||||
|
||||
bool supportsRxPowerSaving() const override { return true; }
|
||||
void setParams(float, float, uint8_t, uint8_t) override {}
|
||||
float getCurrentRSSI() override { return -110.0f; }
|
||||
bool isReceivingPacket() override { return receiving_latch; }
|
||||
void resetReceivingState() override {
|
||||
reset_receiving_calls++;
|
||||
receiving_latch = false;
|
||||
}
|
||||
|
||||
int16_t armDutyCycle(RadioLibIrqFlags_t, RadioLibIrqFlags_t,
|
||||
uint32_t* eff_rx_us, uint32_t* eff_sleep_us) override {
|
||||
arm_duty_cycle_calls++;
|
||||
*eff_rx_us = _rx_ps_rx_us;
|
||||
*eff_sleep_us = _rx_ps_sleep_us;
|
||||
return RADIOLIB_ERR_NONE;
|
||||
}
|
||||
int16_t stopDutyCycleHardware() override {
|
||||
stop_duty_cycle_hw_calls++;
|
||||
return _radio->standby();
|
||||
}
|
||||
int16_t startContinuousReceive() override {
|
||||
start_cont_rx_calls++;
|
||||
return _radio->startReceive();
|
||||
}
|
||||
};
|
||||
|
||||
struct RxpsFixture : public ::testing::Test {
|
||||
MockRadio radio;
|
||||
MockBoard board;
|
||||
TestRxpsWrapper wrap{radio, board};
|
||||
|
||||
void SetUp() override {
|
||||
g_mock_millis = 1000;
|
||||
wrap.begin();
|
||||
ASSERT_TRUE(wrap.setRxPowerSaving(true, 20000, 60000));
|
||||
uint8_t buf[16];
|
||||
EXPECT_EQ(wrap.recvRaw(buf, sizeof(buf)), 0);
|
||||
ASSERT_TRUE(wrap.getRxPowerSavingStatus().armed);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(RxpsFixture, DisableRxpsClearsStuckHeaderLatchImmediately) {
|
||||
// Simulate mast-three stuck state: irq=0x0014 (preamble + header valid, no RX_DONE)
|
||||
// holding isReceivingPacket() == true.
|
||||
radio.irq_flags = (1UL << RADIOLIB_IRQ_PREAMBLE_DETECTED) | (1UL << RADIOLIB_IRQ_HEADER_VALID);
|
||||
wrap.receiving_latch = true;
|
||||
|
||||
EXPECT_TRUE(wrap.setRxPowerSaving(false, 20000, 60000));
|
||||
|
||||
EXPECT_FALSE(wrap.getRxPowerSavingStatus().armed);
|
||||
EXPECT_FALSE(wrap.receiving_latch);
|
||||
EXPECT_EQ(radio.irq_flags, 0u);
|
||||
EXPECT_GT(wrap.stop_duty_cycle_hw_calls, 0);
|
||||
EXPECT_GT(wrap.reset_receiving_calls, 0);
|
||||
|
||||
// Next recvRaw() must re-arm continuous RX, not duty cycle.
|
||||
int prev_cont = wrap.start_cont_rx_calls;
|
||||
uint8_t buf[16];
|
||||
EXPECT_EQ(wrap.recvRaw(buf, sizeof(buf)), 0);
|
||||
EXPECT_EQ(wrap.start_cont_rx_calls, prev_cont + 1);
|
||||
EXPECT_FALSE(wrap.getRxPowerSavingStatus().armed);
|
||||
}
|
||||
|
||||
TEST_F(RxpsFixture, DisableRxpsDeferredDuringTxThenClearsStuckLatch) {
|
||||
uint8_t tx[4] = {1, 2, 3, 4};
|
||||
ASSERT_TRUE(wrap.startSendRaw(tx, sizeof(tx)));
|
||||
|
||||
// Disable requested mid-TX must not clobber the active transmit.
|
||||
EXPECT_TRUE(wrap.setRxPowerSaving(false, 20000, 60000));
|
||||
radio.fireDio1();
|
||||
ASSERT_TRUE(wrap.isSendComplete());
|
||||
wrap.onSendFinished();
|
||||
|
||||
// Even if a stale header latch appears before loop(), disable still completes.
|
||||
radio.irq_flags = (1UL << RADIOLIB_IRQ_PREAMBLE_DETECTED) | (1UL << RADIOLIB_IRQ_HEADER_VALID);
|
||||
wrap.receiving_latch = true;
|
||||
wrap.loop();
|
||||
|
||||
EXPECT_FALSE(wrap.receiving_latch);
|
||||
EXPECT_EQ(radio.irq_flags, 0u);
|
||||
uint8_t buf[16];
|
||||
EXPECT_EQ(wrap.recvRaw(buf, sizeof(buf)), 0);
|
||||
EXPECT_FALSE(wrap.getRxPowerSavingStatus().armed);
|
||||
EXPECT_GT(wrap.start_cont_rx_calls, 0);
|
||||
}
|
||||
|
||||
TEST_F(RxpsFixture, TimeoutIrqDuringNoiseFloorCalibReArmsRxInsteadOfStayingInStandby) {
|
||||
// Start a noise-floor calibration window (after NF_CALIB_INTERVAL_MS).
|
||||
g_mock_millis += 65000;
|
||||
wrap.loop();
|
||||
ASSERT_TRUE(wrap.isRxPowerSavingCalibrationActive());
|
||||
ASSERT_TRUE(wrap.getRxPowerSavingStatus().armed);
|
||||
|
||||
// Before recvRaw() switches to continuous RX, DIO1 fires with TIMEOUT + stale
|
||||
// PREAMBLE/HEADER flags (no RX_DONE). Without the fix, startReceiveMode()
|
||||
// saw _nf_calib_active && _rx_ps_armed && isPacketPendingOrReceiving() and
|
||||
// returned ERR_NONE without re-arming RX, leaving the chip in STBY_RC.
|
||||
radio.irq_flags = (1UL << RADIOLIB_IRQ_TIMEOUT) |
|
||||
(1UL << RADIOLIB_IRQ_PREAMBLE_DETECTED) |
|
||||
(1UL << RADIOLIB_IRQ_HEADER_VALID);
|
||||
wrap.receiving_latch = true;
|
||||
radio.fireDio1();
|
||||
|
||||
int prev_cont = wrap.start_cont_rx_calls;
|
||||
uint8_t buf[16];
|
||||
EXPECT_EQ(wrap.recvRaw(buf, sizeof(buf)), 0);
|
||||
|
||||
EXPECT_FALSE(wrap.receiving_latch);
|
||||
EXPECT_EQ(radio.irq_flags, 0u);
|
||||
EXPECT_FALSE(wrap.getRxPowerSavingStatus().armed);
|
||||
EXPECT_EQ(wrap.start_cont_rx_calls, prev_cont + 1);
|
||||
EXPECT_TRUE(wrap.isInRecvMode());
|
||||
}
|
||||
|
||||
TEST_F(RxpsFixture, ValidRxDonePreservesIrqForReadDataCrcCheck) {
|
||||
radio.irq_flags = (1UL << RADIOLIB_IRQ_RX_DONE) | (1UL << RADIOLIB_IRQ_CRC_ERR);
|
||||
radio.next_pkt_len = 8;
|
||||
radio.read_data_err = RADIOLIB_ERR_CRC_MISMATCH;
|
||||
radio.fireDio1();
|
||||
|
||||
uint8_t buf[16];
|
||||
EXPECT_EQ(wrap.recvRaw(buf, sizeof(buf)), 0);
|
||||
EXPECT_EQ(radio.read_data_calls, 1);
|
||||
EXPECT_TRUE(radio.read_data_saw_irq_intact);
|
||||
EXPECT_EQ(wrap.getPacketsRecvErrors(), 1u);
|
||||
}
|
||||
|
||||
TEST_F(RxpsFixture, ValidRxDoneZeroLenStillClearsIrq) {
|
||||
radio.irq_flags = (1UL << RADIOLIB_IRQ_RX_DONE);
|
||||
radio.next_pkt_len = 0;
|
||||
radio.fireDio1();
|
||||
|
||||
uint8_t buf[16];
|
||||
EXPECT_EQ(wrap.recvRaw(buf, sizeof(buf)), 0);
|
||||
EXPECT_EQ(radio.read_data_calls, 0);
|
||||
EXPECT_EQ(radio.irq_flags, 0u);
|
||||
}
|
||||
|
||||
TEST_F(RxpsFixture, KickRxClearsIrqAndResetsReceivingState) {
|
||||
radio.irq_flags = (1UL << RADIOLIB_IRQ_PREAMBLE_DETECTED) | (1UL << RADIOLIB_IRQ_HEADER_VALID);
|
||||
wrap.receiving_latch = true;
|
||||
|
||||
wrap.kickRx();
|
||||
|
||||
EXPECT_FALSE(wrap.getRxPowerSavingStatus().armed);
|
||||
EXPECT_FALSE(wrap.receiving_latch);
|
||||
EXPECT_EQ(radio.irq_flags, 0u);
|
||||
EXPECT_GT(wrap.reset_receiving_calls, 0);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Reference in new issue
Block a user