"""Run actual RAK terminal shutdown hooks; no RF/current measurement or PIO.""" from pathlib import Path import unittest from test_nrf52_shutdown_leds import compile_source, lifecycle_source from test_replay_reset_integration import extract_braced ROOT = Path(__file__).resolve().parents[1] BOARDS = (("rak4631", "RAK4631Board", 37), ("rak3401", "RAK3401Board", 21)) POE_MACROS = ("ETHERNET_ENABLED", "ETHERNET_USE_RAK13800", "RAK4631_COMBINED_ETHERNET") def source_for(variant, board, enable_pin, *, mutation=None): cpp = (ROOT / f"variants/{variant}/{board}.cpp").read_text() header = (ROOT / f"variants/{variant}/{board}.h").read_text() if "void shutdownPeripherals() override;" not in header: raise AssertionError("RAK must retain the inherited power-off dispatch hook") shutdown = extract_braced(cpp, f"void {board}::shutdownPeripherals()") if mutation == "no-board-override": shutdown = f"void {board}::shutdownPeripherals() {{ NRF52Board::shutdownPeripherals(); }}" elif mutation == "ethernet-keeps-rail-on": rail = "PIN_3V3_EN" if variant == "rak3401" else "WB_IO2" shutdown = shutdown.replace(f"digitalWrite({rail}, LOW);", f"#ifndef ETHERNET_ENABLED\n digitalWrite({rail}, LOW);\n#endif") elif mutation == "wire-left-active": shutdown = shutdown.replace("Wire.end();", "// negative control") elif mutation == "spi1-ended-unstarted": shutdown = shutdown.replace("SPI1.begin();", "// negative control") elif mutation == "external-radio-pad-left-driven": shutdown = shutdown.replace("pinMode(P_LORA_NSS, INPUT);", "// negative control") elif mutation == "ignore-storage-hold": shutdown = shutdown.replace("sensors.canRemoveSharedStoragePowerForDeepSleep()", "true") protective = extract_braced(cpp, f"void {board}::initiateShutdown(uint8_t reason)") # Reuse the real base shutdown, deferred-LED-owner model and Arduino stub. # Remove its main only; the RAK cases exercise inherited virtual dispatch. source = lifecycle_source().split("int main() {", 1)[0] if variant == "rak3401": # The real external-radio nets alias RAK13800 SPI/CS/reset. Synthetic # private-radio pins would conceal unsafe timeout fallback operations. for name, old, actual in (("RESET",40,4),("BUSY",41,9),("NSS",42,26), ("DIO_1",43,10),("SCLK",44,3),("MISO",45,29),("MOSI",46,30)): source = source.replace(f"#define P_LORA_{name} {old}", f"#define P_LORA_{name} {actual}") source = source.replace("#define PIN_3V3_EN 17", "#define PIN_3V3_EN 34") source = source.replace( "struct SensorsMock { LocationMock* getLocationProvider() { return &location; } } sensors;", """struct SensorsMock { bool storage_safe=true; bool canRemoveSharedStoragePowerForDeepSleep() const { return storage_safe; } LocationMock* getLocationProvider() { return &location; } } sensors;""") source = source.replace( 'struct SpiMock { void setPins(int,int,int) {} void begin() { event("spi"); } } SPI;', '''struct SpiMock { bool begun=false; void setPins(int,int,int) {} void begin() { event("spi");begun=true; } void end() { assert(begun);event("spi-end");begun=false; } } SPI;''') source += f"\n#define SX126X_POWER_EN {enable_pin}\n#define WB_IO2 PIN_3V3_EN\n" if variant == "rak3401": source += "#define RAK_EXTERNAL_RADIO 1\n" source += r''' #include #define PIN_GPS_TX 15 #define PIN_GPS_RX 16 #define PIN_WIRE_SDA 13 #define PIN_WIRE_SCL 14 #define SHUTDOWN_REASON_LOW_VOLTAGE 0x4c #define SHUTDOWN_REASON_BOOT_PROTECT 0x42 struct { uint8_t lpcomp_ain_channel=3,lpcomp_refsel=4; } power_config; struct UartMock { bool begun=false; explicit operator bool() const { return begun; } void end() { assert(begun); // the real Adafruit end() is unsafe before begin() event("gps-uart-end");begun=false; } } Serial1; struct { bool begun=false; void begin() { event("spi1-begin");begun=true; } void end() { assert(begun);event("spi1-end");begun=false; } } SPI1; struct WireMock { void end() { event("wire-end"); } } Wire; bool ethernet_can_stop=true; bool stopEthernet(void*,uint32_t timeout) { assert(timeout==500);event("ethernet-stop");return ethernet_can_stop; } ''' source += f''' class {board} : public NRF52Board {{ public: void shutdownPeripherals() override; void initiateShutdown(uint8_t reason); void configureVoltageWake(uint8_t,uint8_t) {{ event("voltage-wake"); }} }}; ''' source += shutdown + "\n" + protective source += r''' size_t lastMode(int pin) { size_t found=events.size(); for(size_t i=0;i