Keep RAK Companion Ethernet waits off the mesh loop
Run DHCP and renewal in a cooperative worker, bound socket commands, and retain SPI ownership until cleanup is safe. Preserve framing deadlines, response ownership and whole-frame replay, including pending-ACK queue pinning. Budget the worker additively and retain all OTA names with an exact-checked Full-only compact index. Add full-driver, sanitizer, queue, shutdown, storage and memory regression coverage.
This commit is contained in:
1 parent
d923de379d
commit
114a0d98c5
29 files changed
+2406
-491
No files matched your search
@@ -729,6 +729,7 @@ jobs:
|
||||
python3 -B test/test_ethernet_terminal_shutdown.py -v
|
||||
python3 -B test/test_rak_sd_receiver.py -v
|
||||
python3 -B test/test_rak15002_shared_spi.py -v
|
||||
python3 -B test/test_rak_companion_ethernet_worker.py -v
|
||||
python3 -B test/test_rak_sd_context_handoff.py -v
|
||||
python3 -B test/test_rak_sd_room_ethernet.py -v
|
||||
|
||||
|
||||
@@ -71,6 +71,13 @@ set eth on
|
||||
set eth off
|
||||
```
|
||||
|
||||
Startup and shutdown are asynchronous. `get eth` reports `starting`, `retrying`,
|
||||
`online`, `stopping`, or a failure state; accepting `set eth on` does not prove
|
||||
a DHCP lease. USB, BLE, and radio service continue while network operations
|
||||
wait. After requesting OFF, wait until `get eth` reports `off` before accessing SD.
|
||||
An already-entered vendor DHCP or renewal call cannot be cancelled midway;
|
||||
OFF keeps the SPI reservation until that call finishes and cleanup is safe.
|
||||
|
||||
Remove the SD card and end/cancel active OTA cache, transfer, serve, or host
|
||||
folder activity before `set eth on`. The command refuses unsafe states. Turn
|
||||
Ethernet off before inserting or accessing a card; an insertion during
|
||||
@@ -602,6 +609,9 @@ Binary WiFi/Ethernet frames must declare 1–176 payload bytes. Illegal lengths
|
||||
close the connection immediately, without waiting for the announced body or
|
||||
executing a truncated command. An incomplete binary header or payload has a
|
||||
five-second deadline once reception starts; reconnect to send a fresh frame.
|
||||
RAK13800 worker-owned SPI intervals pause this deadline without changing the
|
||||
connection or its queued replies. The remaining receive budget resumes when
|
||||
the worker hands the controller back; an already-expired frame is not revived.
|
||||
Ethernet raw-line terminal input has no typing deadline, but an overlong line
|
||||
also closes the connection instead of executing its prefix.
|
||||
|
||||
|
||||
@@ -292,6 +292,21 @@ hold the mesh loop waiting for an acknowledgement. DHCP and listener management
|
||||
run in the Ethernet worker. This is synthetic coverage and a build experiment,
|
||||
not a physical Ethernet/PoE soak qualification or a replacement release image.
|
||||
|
||||
RAK13800 Companion transport also uses a 1024-word worker by default. Its
|
||||
existing four-frame queue stays in the linked object; it adds no raw CLI TX
|
||||
ring. Reserve the worker's 4096-byte stack plus 256 bytes for its TCB and
|
||||
allocation metadata in addition to OTA source workspaces: USB/BLE folder
|
||||
sources can coexist with Companion Ethernet. Stack overrides raise both the
|
||||
total and contiguous allocation requirements. Worker shutdown must complete
|
||||
before its stack is reclaimed or the shared SPI/storage owner is released.
|
||||
|
||||
Full RAK Companion keeps every OTA target name while storing an eight-bit
|
||||
candidate fingerprint instead of a four-byte ID for each name. A lookup
|
||||
reconstructs candidate names in the existing 4096-byte heap workspace and
|
||||
checks their complete SHA-256:4 IDs before returning a match. Other targets
|
||||
retain their existing index. This trades bounded, infrequent display lookup
|
||||
work for flash space; no target names or firmware capabilities are removed.
|
||||
|
||||
The [small-screen message layout](../test-results/v4_pixel5_font_trial.md) retains complete
|
||||
160-byte messages. Its expanded preview records add 2,816 bytes to the
|
||||
startup allowance and increase the contiguous history allocation budget.
|
||||
|
||||
@@ -678,7 +678,7 @@ bool handleRak4631PeripheralCommand(const char* command, char* reply, size_t cap
|
||||
snprintf(reply, capacity, "Error: %s", ethernet_interface.last_error());
|
||||
} else {
|
||||
snprintf(reply, capacity, "%s eth=%s; RAK13800 (this boot)", get ? ">" : "OK -",
|
||||
ethernet_interface.hardwareActive() ? "on" : "off");
|
||||
ethernet_interface.status());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -4217,6 +4217,11 @@ void loop() {
|
||||
#elif defined(NRF52_PLATFORM)
|
||||
can_sleep = can_sleep && !board.isUsbHostConnected();
|
||||
#endif
|
||||
#if defined(NRF52_PLATFORM) && defined(ETHERNET_USE_RAK13800)
|
||||
// Polled Ethernet has no GPIO wake for a client or parked worker. OFF also
|
||||
// disables its logical transport before main-thread resource release ends.
|
||||
can_sleep = can_sleep && !ethernet_interface.hardwareActive();
|
||||
#endif
|
||||
#if defined(MOMENTARY_BUTTON_WAKE_FROM_SLEEP) \
|
||||
&& MOMENTARY_BUTTON_WAKE_FROM_SLEEP \
|
||||
&& defined(PIN_USER_BTN) && defined(DISPLAY_CLASS)
|
||||
|
||||
@@ -158,8 +158,11 @@ def requirements(platform, defines, target):
|
||||
parts["display_dma_and_bus"] = 16384
|
||||
mqtt = None
|
||||
combined_rak_ethernet = "RAK4631_COMBINED_ETHERNET" in defines
|
||||
cooperative_rak_ethernet = combined_rak_ethernet or (
|
||||
"OTA_RAK15002_SD_STORE" in defines and "ETHERNET_ENABLED" in defines and not companion)
|
||||
cooperative_rak_ethernet = not companion and (combined_rak_ethernet or (
|
||||
"OTA_RAK15002_SD_STORE" in defines and "ETHERNET_ENABLED" in defines))
|
||||
companion_rak_ethernet = (platform == "NRF52_PLATFORM" and companion
|
||||
and "ETHERNET_ENABLED" in defines and ("ETHERNET_USE_RAK13800" in defines
|
||||
or {"OTA_RAK15002_SD_STORE", "RAK_4631"}.issubset(defines)))
|
||||
if combined_rak_ethernet and (platform != "NRF52_PLATFORM"
|
||||
or not {"RAK_4631", "ETHERNET_ENABLED", "OTA_RAK_AUTO_STORE"}.issubset(defines)):
|
||||
raise ValueError("combined RAK Ethernet budget requires RAK4631 adaptive nRF52 storage")
|
||||
@@ -290,6 +293,15 @@ def requirements(platform, defines, target):
|
||||
# running nRF52840 app is <1 MiB: <=512 2-KiB leaves. Compression needs
|
||||
# one block; raw diagnostic serving retains a leaf-sized output.
|
||||
parts["ota_self_source_scratch"] = 2 * 2048 + 2 * 16
|
||||
if companion_rak_ethernet:
|
||||
words = integer(defines, "RAK4631_ETHERNET_TASK_STACK_WORDS", 1024)
|
||||
if not 1024 <= words <= 65535:
|
||||
raise ValueError("RAK Companion Ethernet task requires 1024..65535 stack words")
|
||||
# Companion retains its existing statically allocated frame queue.
|
||||
# Its worker can coexist with USB/BLE folder-source buffers, so this
|
||||
# stack and TCB allowance is additive, never an exclusive OTA maximum.
|
||||
# The source bounds StaticTask_t plus allocator metadata to 256 bytes.
|
||||
parts["ethernet_companion_worker"] = words * 4 + 256
|
||||
if cooperative_rak_ethernet:
|
||||
words = integer(defines, "RAK4631_ETHERNET_TASK_STACK_WORDS", 1024)
|
||||
if words < 1024:
|
||||
@@ -345,7 +357,8 @@ def requirements(platform, defines, target):
|
||||
largest = max(largest, 8192 + parts["expanded_message_previews"])
|
||||
for name in ("client_table", "flood_filter_table", "room_flood_rule_table", "neighbor_history",
|
||||
"ota_context", "screen_objects_and_history", "ota_encoder_workspace",
|
||||
"ota_generic_source_buffers", "ethernet_or_external_ota_workspaces", "ota_target_name_lookup"):
|
||||
"ota_generic_source_buffers", "ethernet_or_external_ota_workspaces", "ota_target_name_lookup",
|
||||
"ethernet_companion_worker"):
|
||||
largest = max(largest, parts.get(name, 0))
|
||||
if "room_browser_mailbox" in parts:
|
||||
largest = max(largest, 8192, parts["room_browser_mailbox"])
|
||||
|
||||
@@ -1,38 +1,79 @@
|
||||
#include "RAK13800EthernetInterface.h"
|
||||
#include "../../nrf52/EthernetMac.h"
|
||||
#include "../../nrf52/TerminalPeripheralShutdown.h"
|
||||
#include "../../nrf52/EthernetCliTransmit.h"
|
||||
#include <SPI.h>
|
||||
#include <EthernetUdp.h>
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
#include "../../ota/OtaSdSpiBusNrf52.h"
|
||||
#endif
|
||||
|
||||
#define PIN_SPI1_MISO (29) // (0 + 29)
|
||||
#define PIN_SPI1_MOSI (30) // (0 + 30)
|
||||
#define PIN_SPI1_SCK (3) // (0 + 3)
|
||||
|
||||
SPIClass ETHERNET_SPI_PORT(NRF_SPIM1, PIN_SPI1_MISO, PIN_SPI1_SCK, PIN_SPI1_MOSI);
|
||||
|
||||
#define PIN_ETHERNET_POWER_EN WB_IO2 // output, high to enable
|
||||
SPIClass ETHERNET_SPI_PORT(NRF_SPIM1, 29, 3, 30);
|
||||
#define PIN_ETHERNET_RESET 21
|
||||
#define PIN_ETHERNET_SS 26
|
||||
#ifndef RAK4631_ETHERNET_TASK_STACK_WORDS
|
||||
#define RAK4631_ETHERNET_TASK_STACK_WORDS 1024
|
||||
#endif
|
||||
static_assert(RAK4631_ETHERNET_TASK_STACK_WORDS >= 1024 && RAK4631_ETHERNET_TASK_STACK_WORDS <= 65535,
|
||||
"Ethernet worker stack budget");
|
||||
static_assert(sizeof(StaticTask_t) + 32 <= 256, "Ethernet task metadata budget");
|
||||
|
||||
bool RAK13800EthernetInterface::begin() {
|
||||
if (!mesh::nrf52::registerTerminalEthernetShutdown(terminalShutdown, this)) return false;
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
// Rare shared-CS Ethernet is explicitly enabled after the SD/NOR context
|
||||
// has supplied its pin/transfer ownership hooks. Boot remains USB/BLE-only.
|
||||
return mesh::nrf52::registerTerminalEthernetShutdown(terminalShutdown, this);
|
||||
return true; // Shared CS26 stays untouched until explicit ownership admission.
|
||||
#else
|
||||
return beginHardware();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool RAK13800EthernetInterface::hardwareActive() const {
|
||||
return _task != nullptr
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
|| _ownsSharedSpi || _releasePending
|
||||
#endif
|
||||
;
|
||||
}
|
||||
|
||||
const char* RAK13800EthernetInterface::status() const {
|
||||
static const char* const names[] = {
|
||||
"off", "starting", "retrying", "online", "stopping", "faulted", "no hardware", "start failed"
|
||||
};
|
||||
return names[static_cast<uint8_t>(_state.load())];
|
||||
}
|
||||
|
||||
void RAK13800EthernetInterface::notifyWorker() {
|
||||
taskENTER_CRITICAL();
|
||||
if (_task != nullptr) xTaskNotifyGive(_task);
|
||||
taskEXIT_CRITICAL();
|
||||
}
|
||||
|
||||
bool RAK13800EthernetInterface::storageConflict() {
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
if (_storageFault.load()) return true;
|
||||
if (_ownsSharedSpi && (mesh::ota::ota_rak15002_sd_card_present()
|
||||
|| !mesh::ota::rak4631_ethernet_owns_spi())) {
|
||||
// A card inserted during a vendor call cannot cancel that call. Exclude
|
||||
// every subsequent transaction and retain the bus claim until safe OFF.
|
||||
_storageFault.store(true);
|
||||
_state.store(State::Faulted);
|
||||
notifyWorker();
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RAK13800EthernetInterface::tryEnable() {
|
||||
if (_terminalStopping) { _spiError = "terminal shutdown"; return false; }
|
||||
if (_spiStarted) { SerialEthernetInterface::enable(); return true; }
|
||||
if (_ownsSharedSpi) { _spiError = "release stopped Ethernet with set eth off first"; return false; }
|
||||
if (_storageFault.load()) { _spiError = "SD inserted; set eth off first"; return false; }
|
||||
const State state = _state.load();
|
||||
if (state == State::Stopping) { _spiError = "Ethernet is stopping"; return false; }
|
||||
if (_task && !_stopped.load()) {
|
||||
if (!_wanted.load() || (state != State::Starting && state != State::Retrying && state != State::Online)) {
|
||||
_spiError = status(); return false;
|
||||
}
|
||||
SerialEthernetInterface::enable(); return true;
|
||||
}
|
||||
if (!finishRelease()) { _spiError = "OTA storage release pending"; return false; }
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
if (!_prepareSpi || !_canReleaseSpi || !_releasedSpi) {
|
||||
_spiError = "OTA ownership hooks unavailable"; return false;
|
||||
}
|
||||
@@ -41,103 +82,92 @@ bool RAK13800EthernetInterface::tryEnable() {
|
||||
}
|
||||
if (!_prepareSpi(_spiContext)) { _spiError = "OTA storage busy"; return false; }
|
||||
if (!mesh::ota::rak4631_try_acquire_spi(mesh::ota::RakSharedSpiOwner::Ethernet)) {
|
||||
(void)_releasedSpi(_spiContext);
|
||||
_releasePending = !_releasedSpi(_spiContext);
|
||||
_spiError = "WisBlock SPI busy"; return false;
|
||||
}
|
||||
_ownsSharedSpi = true;
|
||||
if (!beginHardware()) {
|
||||
(void)shutdownForPowerOff(0);
|
||||
_terminalStopping = false;
|
||||
(void)_releasedSpi(_spiContext);
|
||||
_spiError = "Ethernet startup failed"; return false;
|
||||
#endif
|
||||
return beginHardware();
|
||||
}
|
||||
|
||||
bool RAK13800EthernetInterface::beginHardware() {
|
||||
if (_terminalStopping) return false;
|
||||
_stopped.store(false);
|
||||
_storageFault.store(false);
|
||||
_closeRequested.store(false);
|
||||
_eventPending.store(false);
|
||||
_ready.store(false);
|
||||
_socket = 0xFF;
|
||||
_listener = 0xFF;
|
||||
_listenerCommand = 0;
|
||||
_ip.store(0);
|
||||
_checkCommands = false;
|
||||
_txAccepted = 0;
|
||||
_transmit.reset();
|
||||
_access.store(Access::Worker);
|
||||
_state.store(State::Starting);
|
||||
_wanted.store(true);
|
||||
// Handle publication, notifications and deletion share this scheduler guard.
|
||||
taskENTER_CRITICAL();
|
||||
const BaseType_t created = xTaskCreate(workerTask, "eth_comp", RAK4631_ETHERNET_TASK_STACK_WORDS,
|
||||
this, 1, &_task);
|
||||
taskEXIT_CRITICAL();
|
||||
if (created != pdPASS) {
|
||||
_task = nullptr;
|
||||
_wanted.store(false);
|
||||
(void)finishRelease();
|
||||
_state.store(State::StartFailed);
|
||||
_spiError = "Ethernet task allocation failed";
|
||||
return false;
|
||||
}
|
||||
SerialEthernetInterface::enable();
|
||||
_spiError = "on";
|
||||
_spiError = "starting";
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RAK13800EthernetInterface::tryDisable() {
|
||||
if (_terminalStopping) { _spiError = "terminal shutdown"; return false; }
|
||||
if (!_ownsSharedSpi) {
|
||||
SerialEthernetInterface::disable();
|
||||
_spiError = "off";
|
||||
return true;
|
||||
}
|
||||
if (_ownsSharedSpi && (!_canReleaseSpi || !_canReleaseSpi(_spiContext))) {
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
if ((_ownsSharedSpi || _releasePending) && (!_canReleaseSpi || !_canReleaseSpi(_spiContext))) {
|
||||
_spiError = "OTA storage busy"; return false;
|
||||
}
|
||||
(void)shutdownForPowerOff(0);
|
||||
_terminalStopping = false;
|
||||
if (_releasedSpi && !_releasedSpi(_spiContext)) {
|
||||
_spiError = "OTA storage release pending"; return false;
|
||||
}
|
||||
_spiError = "off";
|
||||
return true;
|
||||
#endif
|
||||
SerialEthernetInterface::disable();
|
||||
_wanted.store(false);
|
||||
_state.store(State::Stopping);
|
||||
notifyWorker();
|
||||
if (finishRelease()) _state.store(State::Off);
|
||||
_spiError = status();
|
||||
return true; // Accepted; an entered vendor call cooperatively finishes first.
|
||||
}
|
||||
|
||||
#endif
|
||||
bool RAK13800EthernetInterface::beginHardware() {
|
||||
if (_terminalStopping) return false;
|
||||
if (!mesh::nrf52::registerTerminalEthernetShutdown(terminalShutdown, this)) return false;
|
||||
// Preserve existing startup/link timing. PoE supplies separate 5V through
|
||||
// the base's VCC_IN; WB_IO2 only supplies the W5100S's switched 3V3_S.
|
||||
#ifdef PIN_ETHERNET_RESET
|
||||
pinMode(PIN_ETHERNET_RESET, OUTPUT);
|
||||
digitalWrite(PIN_ETHERNET_RESET, HIGH);
|
||||
#endif
|
||||
|
||||
// generate mac address
|
||||
uint8_t mac[6];
|
||||
generateEthernetMac(mac);
|
||||
ETHERNET_DEBUG_PRINTLN(
|
||||
"Ethernet MAC: %02X:%02X:%02X:%02X:%02X:%02X",
|
||||
mac[0],
|
||||
mac[1],
|
||||
mac[2],
|
||||
mac[3],
|
||||
mac[4],
|
||||
mac[5]);
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
// OFF releases the shared pads to inputs. The vendor W5100 init is cached
|
||||
// after its first success and will not restore CS direction on reacquire.
|
||||
// Only tryEnable() reaches here, after storage and SPI ownership admission.
|
||||
digitalWrite(PIN_ETHERNET_SS, HIGH);
|
||||
pinMode(PIN_ETHERNET_SS, OUTPUT);
|
||||
#endif
|
||||
ETHERNET_SPI_PORT.begin();
|
||||
_spiStarted = true;
|
||||
Ethernet.init(ETHERNET_SPI_PORT, PIN_ETHERNET_SS);
|
||||
|
||||
// Use static IP if build flags are defined, otherwise DHCP
|
||||
#if defined(ETHERNET_STATIC_IP) && defined(ETHERNET_STATIC_GATEWAY) && defined(ETHERNET_STATIC_SUBNET) && defined(ETHERNET_STATIC_DNS)
|
||||
IPAddress ip(ETHERNET_STATIC_IP);
|
||||
IPAddress gateway(ETHERNET_STATIC_GATEWAY);
|
||||
IPAddress subnet(ETHERNET_STATIC_SUBNET);
|
||||
IPAddress dns(ETHERNET_STATIC_DNS);
|
||||
Ethernet.begin(mac, ip, dns, gateway, subnet);
|
||||
#else
|
||||
if (Ethernet.begin(mac) == 0) {
|
||||
ETHERNET_DEBUG_PRINTLN("Failed to initialize RAK13800 hardware.");
|
||||
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
||||
ETHERNET_DEBUG_PRINTLN("Ethernet hardware not found.");
|
||||
} else if (Ethernet.linkStatus() == LinkOFF) {
|
||||
ETHERNET_DEBUG_PRINTLN("Ethernet cable not connected.");
|
||||
} else {
|
||||
ETHERNET_DEBUG_PRINTLN("DHCP failed for unknown reason.");
|
||||
}
|
||||
return false;
|
||||
bool RAK13800EthernetInterface::finishRelease() {
|
||||
if (_task) {
|
||||
if (!_stopped.load()) return false;
|
||||
taskENTER_CRITICAL();
|
||||
TaskHandle_t task = _task;
|
||||
_task = nullptr;
|
||||
taskEXIT_CRITICAL();
|
||||
// Reclaim synchronously, before the OTA release hook can allocate storage.
|
||||
vTaskDelete(task);
|
||||
_isConnected = false;
|
||||
onClientDisconnected();
|
||||
mesh::nrf52::releaseRakEthernetBusPads();
|
||||
}
|
||||
#endif
|
||||
|
||||
ETHERNET_DEBUG_PRINTLN("Ethernet begin complete");
|
||||
ETHERNET_DEBUG_PRINT_IP("IP Address", Ethernet.localIP());
|
||||
ETHERNET_DEBUG_PRINT_IP("Subnet Mask", Ethernet.subnetMask());
|
||||
ETHERNET_DEBUG_PRINT_IP("Gateway", Ethernet.gatewayIP());
|
||||
ETHERNET_DEBUG_PRINT_IP("DNS", Ethernet.dnsServerIP());
|
||||
|
||||
server.begin();
|
||||
ETHERNET_DEBUG_PRINTLN("listening on TCP port: %d", ETHERNET_TCP_PORT);
|
||||
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
if (_ownsSharedSpi) {
|
||||
// Do not expose storage until its main-thread context accepts the handoff.
|
||||
if (!_releasedSpi || !_releasedSpi(_spiContext)) return false;
|
||||
mesh::ota::rak4631_release_spi(mesh::ota::RakSharedSpiOwner::Ethernet);
|
||||
_ownsSharedSpi = false;
|
||||
}
|
||||
if (_releasePending) {
|
||||
if (!_releasedSpi || !_releasedSpi(_spiContext)) return false;
|
||||
_releasePending = false;
|
||||
}
|
||||
#endif
|
||||
_storageFault.store(false);
|
||||
_eventPending.store(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -146,111 +176,350 @@ bool RAK13800EthernetInterface::terminalShutdown(void* context, uint32_t timeout
|
||||
}
|
||||
|
||||
bool RAK13800EthernetInterface::shutdownForPowerOff(uint32_t timeout_ms) {
|
||||
(void)timeout_ms; // All operations below are finite register transfers.
|
||||
_terminalStopping = true;
|
||||
// disable() cancels framed/session producers, but disconnectClient() must
|
||||
// not enter the vendor socket-stop/ACK wait during terminal shutdown.
|
||||
SerialEthernetInterface::disable();
|
||||
if (_spiStarted) {
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
// A newly inserted card also selects on CS26. Do not clock Ethernet
|
||||
// socket commands into it during recovery or terminal shutdown.
|
||||
if (!mesh::ota::ota_rak15002_sd_card_present())
|
||||
#endif
|
||||
for (uint8_t socket = 0; socket < 4; ++socket) {
|
||||
(void)mesh::nrf52::EthernetCliTransmit::close(socket);
|
||||
}
|
||||
client = EthernetClient();
|
||||
ETHERNET_SPI_PORT.end();
|
||||
_spiStarted = false;
|
||||
mesh::nrf52::releaseRakEthernetBusPads();
|
||||
_wanted.store(false);
|
||||
_state.store(State::Stopping);
|
||||
notifyWorker();
|
||||
const uint32_t started = millis();
|
||||
do {
|
||||
if (finishRelease()) { _state.store(State::Off); return true; }
|
||||
if (static_cast<uint32_t>(millis() - started) >= timeout_ms) return false;
|
||||
vTaskDelay(pdMS_TO_TICKS(1));
|
||||
} while (true);
|
||||
}
|
||||
|
||||
RAK13800EthernetInterface::ClientAccess::ClientAccess(RAK13800EthernetInterface& owner)
|
||||
: _owner(owner), _acquired(false) {
|
||||
if (!owner._wanted.load() || owner.storageConflict()) return;
|
||||
Access expected = Access::Idle;
|
||||
_acquired = owner._access.compare_exchange_strong(expected, Access::Client);
|
||||
if (_acquired && (!owner._wanted.load() || owner.storageConflict())) {
|
||||
owner._access.store(Access::Idle);
|
||||
_acquired = false;
|
||||
}
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
if (_ownsSharedSpi) {
|
||||
mesh::ota::rak4631_release_spi(mesh::ota::RakSharedSpiOwner::Ethernet);
|
||||
_ownsSharedSpi = false;
|
||||
}
|
||||
|
||||
RAK13800EthernetInterface::ClientAccess::~ClientAccess() {
|
||||
if (!_acquired) return;
|
||||
if (_owner._checkCommands || _owner._transmit.pending() || _owner._transmit.failed()) {
|
||||
_owner._access.store(Access::Worker);
|
||||
_owner.serviceIOAvailability();
|
||||
_owner.notifyWorker();
|
||||
} else {
|
||||
_owner._access.store(Access::Idle);
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RAK13800EthernetInterface::isIOUnavailable() const {
|
||||
return _access.load() != Access::Idle || _storageFault.load();
|
||||
}
|
||||
|
||||
int RAK13800EthernetInterface::available() {
|
||||
if (!_spiStarted || _terminalStopping) return 0;
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
if (mesh::ota::ota_rak15002_sd_card_present()
|
||||
|| !mesh::ota::rak4631_ethernet_owns_spi()) return 0;
|
||||
#endif
|
||||
return client.available();
|
||||
ClientAccess access(*this);
|
||||
if (!access || !_isConnected) return 0;
|
||||
const uint8_t socket = _socket;
|
||||
const int result = mesh::nrf52::EthernetCliTransmit::available(socket);
|
||||
if (!mesh::nrf52::EthernetCliTransmit::commandReady(socket)) requireCommandCheck();
|
||||
return result;
|
||||
}
|
||||
|
||||
int RAK13800EthernetInterface::read() {
|
||||
if (!_spiStarted || _terminalStopping) return -1;
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
if (mesh::ota::ota_rak15002_sd_card_present()
|
||||
|| !mesh::ota::rak4631_ethernet_owns_spi()) return -1;
|
||||
#endif
|
||||
return client.read();
|
||||
ClientAccess access(*this);
|
||||
if (!access || !_isConnected) return -1;
|
||||
const uint8_t socket = _socket;
|
||||
const int result = mesh::nrf52::EthernetCliTransmit::read(socket);
|
||||
if (!mesh::nrf52::EthernetCliTransmit::commandReady(socket)) requireCommandCheck();
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t RAK13800EthernetInterface::write(const uint8_t *buf, size_t size) {
|
||||
if (!_spiStarted || _terminalStopping) return 0;
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
if (mesh::ota::ota_rak15002_sd_card_present()
|
||||
|| !mesh::ota::rak4631_ethernet_owns_spi()) return 0;
|
||||
#endif
|
||||
return client.write(buf, size);
|
||||
ClientAccess access(*this);
|
||||
if (!access || !_isConnected) return 0;
|
||||
if (_txAccepted) {
|
||||
// The base retains its frame/offset while the worker waits for ACK. A
|
||||
// replacement resets this count and replays the whole frame on the new
|
||||
// stream, matching the old synchronous vendor write's queue semantics.
|
||||
if (_transmit.pending() || _transmit.failed()) return 0;
|
||||
const size_t accepted = _txAccepted;
|
||||
_txAccepted = 0;
|
||||
return accepted;
|
||||
}
|
||||
_txAccepted = _transmit.write(_socket, buf, size, millis());
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool RAK13800EthernetInterface::isConnected() const {
|
||||
return isEnabled() && _isConnected;
|
||||
// DHCP and a stalled vendor socket call are backpressure, not disconnects.
|
||||
return isEnabled() && _isConnected && !_storageFault.load();
|
||||
}
|
||||
|
||||
void RAK13800EthernetInterface::disconnectClient() {
|
||||
_isConnected = false;
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
if (mesh::ota::ota_rak15002_sd_card_present()
|
||||
|| !mesh::ota::rak4631_ethernet_owns_spi()) return;
|
||||
#endif
|
||||
if (_spiStarted && !_terminalStopping) client.stop();
|
||||
_closeRequested.store(true);
|
||||
notifyWorker();
|
||||
}
|
||||
|
||||
void RAK13800EthernetInterface::loop() {
|
||||
if (!_spiStarted || _terminalStopping) return;
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
if (mesh::ota::ota_rak15002_sd_card_present()) {
|
||||
// Fail closed on insertion while Ethernet owns the bus. Do not send a
|
||||
// socket-close command on shared CS26 or switch a live internal OTA store.
|
||||
_terminalStopping = true;
|
||||
SerialEthernetInterface::disable();
|
||||
_terminalStopping = false;
|
||||
digitalWrite(PIN_ETHERNET_SS, HIGH);
|
||||
ETHERNET_SPI_PORT.end();
|
||||
_spiStarted = false;
|
||||
mesh::nrf52::releaseRakEthernetBusPads();
|
||||
serviceIOAvailability();
|
||||
(void)storageConflict();
|
||||
if (_storageFault.load()) {
|
||||
_spiError = "SD inserted; set eth off before SD access";
|
||||
return; // Retain Ethernet's claim until the context permits release.
|
||||
SerialEthernetInterface::disable();
|
||||
}
|
||||
if (!mesh::ota::rak4631_ethernet_owns_spi()) return;
|
||||
#endif
|
||||
Ethernet.maintain();
|
||||
if (!isEnabled()) return;
|
||||
if (_isConnected && !client.connected()) {
|
||||
if (_eventPending.load()) {
|
||||
const uint8_t socket = _socket;
|
||||
_isConnected = false;
|
||||
onClientDisconnected();
|
||||
if (_wanted.load() && isEnabled() && socket < 4 && !_storageFault.load()) {
|
||||
onClientConnected(_eventIp);
|
||||
_isConnected = true;
|
||||
} else {
|
||||
onClientDisconnected();
|
||||
}
|
||||
// Until this acknowledgement, worker retains SPI and no frame can reach
|
||||
// a replacement owner before main-thread response cancellation/replay.
|
||||
_eventPending.store(false);
|
||||
notifyWorker();
|
||||
}
|
||||
|
||||
auto newClient = server.accept();
|
||||
if (newClient) {
|
||||
IPAddress remoteIp = newClient.remoteIP();
|
||||
uint16_t remotePort = newClient.remotePort();
|
||||
ETHERNET_DEBUG_PRINTLN("New client accepted %u.%u.%u.%u:%u", remoteIp[0], remoteIp[1], remoteIp[2], remoteIp[3], remotePort);
|
||||
disconnectClient();
|
||||
onClientConnected((uint32_t)remoteIp);
|
||||
// Finish old-owner cancellation before publishing the replacement socket.
|
||||
client = newClient;
|
||||
if (!_wanted.load() && finishRelease()) _state.store(State::Off);
|
||||
if (_wanted.load() && !_storageFault.load() && _state.load() == State::Online) {
|
||||
if (_ready.load()) {
|
||||
// Both edges are observed on main, even if MultiSerial has temporarily
|
||||
// pinned another reply route and skips Ethernet frame polling.
|
||||
_access.store(Access::Idle);
|
||||
_ready.store(false);
|
||||
_pollAt = millis() + 20;
|
||||
} else if (static_cast<int32_t>(static_cast<uint32_t>(millis()) - _pollAt) >= 0) {
|
||||
Access expected = Access::Idle;
|
||||
if (_access.compare_exchange_strong(expected, Access::Worker)) notifyWorker();
|
||||
}
|
||||
}
|
||||
|
||||
_isConnected = client.connected();
|
||||
if (!_isConnected) onClientDisconnected();
|
||||
|
||||
serviceIOAvailability();
|
||||
}
|
||||
|
||||
void RAK13800EthernetInterface::takeWorkerAccess() {
|
||||
while (_access.load() != Access::Worker) {
|
||||
Access expected = Access::Idle;
|
||||
if (_access.compare_exchange_strong(expected, Access::Worker)) break;
|
||||
vTaskDelay(pdMS_TO_TICKS(1));
|
||||
}
|
||||
}
|
||||
|
||||
void RAK13800EthernetInterface::requireCommandCheck() {
|
||||
if (!_checkCommands) { _checkCommands = true; _checkStarted = millis(); }
|
||||
}
|
||||
|
||||
bool RAK13800EthernetInterface::commandsIdle() {
|
||||
if (storageConflict()) return false;
|
||||
SPIClass* spi = W5100.getSPI();
|
||||
if (!spi) return false;
|
||||
spi->beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
bool idle = true;
|
||||
for (uint8_t socket = 0; socket < 4; ++socket) {
|
||||
if (W5100.readSnCR(socket) != 0) idle = false;
|
||||
}
|
||||
if (idle && _listenerCommand) {
|
||||
const uint8_t status = W5100.readSnSR(_listener);
|
||||
if (_listenerCommand == Sock_OPEN) idle = status == SnSR::INIT;
|
||||
else if (_listenerCommand == Sock_CLOSE) idle = status == SnSR::CLOSED;
|
||||
else idle = status == SnSR::LISTEN || status == SnSR::SYNRECV ||
|
||||
status == SnSR::ESTABLISHED || status == SnSR::CLOSE_WAIT;
|
||||
if (idle) _listenerCommand = 0;
|
||||
}
|
||||
spi->endTransaction();
|
||||
return idle;
|
||||
}
|
||||
|
||||
bool RAK13800EthernetInterface::closeSocket(uint8_t socket) {
|
||||
if (socket >= 4) return true;
|
||||
if (!mesh::nrf52::EthernetCliTransmit::close(socket)) return false;
|
||||
requireCommandCheck();
|
||||
return true;
|
||||
}
|
||||
|
||||
void RAK13800EthernetInterface::publishClient(uint8_t socket, uint32_t ip) {
|
||||
_socket = socket;
|
||||
_eventIp = ip;
|
||||
_eventPending.store(true);
|
||||
while (_eventPending.load() && _wanted.load() && !storageConflict()) {
|
||||
(void)ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(20));
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t RAK13800EthernetInterface::acceptClient() {
|
||||
// OPEN/LISTEN are controller commands, not network waits. Issue each only
|
||||
// once and reuse the finite command-ack watchdog before its next phase.
|
||||
// Avoid virtual EthernetServer/Client objects: their unused Stream methods
|
||||
// otherwise retain the blocking TCP implementation in small Full images.
|
||||
if (storageConflict()) return 0xFF;
|
||||
SPIClass* spi = W5100.getSPI();
|
||||
if (!spi || W5100.getChip() != 51) { _state.store(State::Faulted); return 0xFF; }
|
||||
spi->beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
uint8_t accepted = 0xFF;
|
||||
if (_listener < 4) {
|
||||
const uint8_t status = W5100.readSnSR(_listener);
|
||||
if (status == SnSR::ESTABLISHED || status == SnSR::CLOSE_WAIT) {
|
||||
accepted = _listener;
|
||||
_listener = 0xFF;
|
||||
} else if (status == SnSR::CLOSED) {
|
||||
_listener = 0xFF;
|
||||
} else if (status == SnSR::INIT) {
|
||||
if (W5100.readSnCR(_listener) == 0) {
|
||||
W5100.writeSnCR(_listener, Sock_LISTEN);
|
||||
_listenerCommand = Sock_LISTEN;
|
||||
}
|
||||
requireCommandCheck();
|
||||
}
|
||||
}
|
||||
if (_listener >= 4) {
|
||||
uint8_t closing = 0xFF;
|
||||
for (uint8_t socket = 0; socket < 4; ++socket) {
|
||||
if (socket == accepted || socket == _socket) continue;
|
||||
const uint8_t status = W5100.readSnSR(socket);
|
||||
if (status == SnSR::CLOSED) {
|
||||
if (W5100.readSnCR(socket) != 0) { requireCommandCheck(); break; }
|
||||
_listener = socket;
|
||||
delayMicroseconds(250); // Preserve the pinned vendor's pre-OPEN settling guard.
|
||||
W5100.writeSnMR(socket, SnMR::TCP);
|
||||
W5100.writeSnIR(socket, 0xFF);
|
||||
W5100.writeSnPORT(socket, ETHERNET_TCP_PORT);
|
||||
W5100.writeSnCR(socket, Sock_OPEN);
|
||||
_listenerCommand = Sock_OPEN;
|
||||
requireCommandCheck();
|
||||
break;
|
||||
}
|
||||
if (status == SnSR::LAST_ACK || status == SnSR::TIME_WAIT ||
|
||||
status == SnSR::FIN_WAIT || status == SnSR::CLOSING) closing = socket;
|
||||
}
|
||||
if (_listener >= 4 && closing < 4) {
|
||||
// Match the vendor allocator's fallback without overwriting a live
|
||||
// client or DHCP UDP socket. OPEN follows on a later acknowledged turn.
|
||||
if (W5100.readSnCR(closing) == 0) {
|
||||
W5100.writeSnCR(closing, Sock_CLOSE);
|
||||
_listener = closing;
|
||||
_listenerCommand = Sock_CLOSE;
|
||||
}
|
||||
requireCommandCheck();
|
||||
}
|
||||
}
|
||||
spi->endTransaction();
|
||||
return accepted;
|
||||
}
|
||||
|
||||
void RAK13800EthernetInterface::workerTask(void* context) {
|
||||
static_cast<RAK13800EthernetInterface*>(context)->runWorker();
|
||||
}
|
||||
|
||||
void RAK13800EthernetInterface::runWorker() {
|
||||
bool fault = false, hardware = false;
|
||||
if (_wanted.load() && !storageConflict()) {
|
||||
pinMode(PIN_ETHERNET_RESET, OUTPUT);
|
||||
digitalWrite(PIN_ETHERNET_RESET, HIGH);
|
||||
// The pinned W5100 init is cached and does not restore CS after OFF.
|
||||
digitalWrite(PIN_ETHERNET_SS, HIGH);
|
||||
pinMode(PIN_ETHERNET_SS, OUTPUT);
|
||||
ETHERNET_SPI_PORT.begin();
|
||||
_spiStarted = true;
|
||||
Ethernet.init(ETHERNET_SPI_PORT, PIN_ETHERNET_SS);
|
||||
uint8_t mac[6];
|
||||
generateEthernetMac(mac);
|
||||
while (_wanted.load() && !storageConflict()) {
|
||||
#if defined(ETHERNET_STATIC_IP) && defined(ETHERNET_STATIC_GATEWAY) && defined(ETHERNET_STATIC_SUBNET) && defined(ETHERNET_STATIC_DNS)
|
||||
Ethernet.begin(mac, IPAddress(ETHERNET_STATIC_IP), IPAddress(ETHERNET_STATIC_DNS),
|
||||
IPAddress(ETHERNET_STATIC_GATEWAY), IPAddress(ETHERNET_STATIC_SUBNET));
|
||||
const bool online = true;
|
||||
#else
|
||||
const bool online = Ethernet.begin(mac, 10000, 2000) != 0;
|
||||
#endif
|
||||
if (storageConflict() || !_wanted.load()) break;
|
||||
hardware = Ethernet.hardwareStatus() != EthernetNoHardware;
|
||||
if (!hardware) { _state.store(State::NoHardware); break; }
|
||||
if (online) {
|
||||
(void)acceptClient();
|
||||
if (storageConflict() || !_wanted.load()) break;
|
||||
_ip.store(static_cast<uint32_t>(Ethernet.localIP()));
|
||||
_state.store(State::Online);
|
||||
break;
|
||||
}
|
||||
_state.store(State::Retrying);
|
||||
(void)ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(30000));
|
||||
}
|
||||
}
|
||||
uint32_t lastMaintain = millis();
|
||||
while (_wanted.load() && _state.load() == State::Online && !storageConflict()) {
|
||||
if (storageConflict() || !_wanted.load()) break;
|
||||
if (_checkCommands) {
|
||||
if (commandsIdle()) _checkCommands = false;
|
||||
else if (static_cast<uint32_t>(millis() - _checkStarted) >= 250) { fault = true; break; }
|
||||
else { (void)ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(10)); continue; }
|
||||
}
|
||||
_transmit.poll(millis());
|
||||
if (_closeRequested.exchange(false) || _transmit.failed()) {
|
||||
if (!closeSocket(_socket)) { fault = true; break; }
|
||||
_transmit.reset();
|
||||
_txAccepted = 0;
|
||||
publishClient(0xFF);
|
||||
continue;
|
||||
}
|
||||
if (_transmit.pending()) { (void)ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(10)); continue; }
|
||||
if (static_cast<uint32_t>(millis() - lastMaintain) >= 1000) {
|
||||
(void)Ethernet.maintain();
|
||||
lastMaintain = millis();
|
||||
if (storageConflict() || !_wanted.load()) break;
|
||||
_ip.store(static_cast<uint32_t>(Ethernet.localIP()));
|
||||
}
|
||||
// All listener commands are bounded and run only behind the worker gate.
|
||||
const uint8_t socket = acceptClient();
|
||||
if (storageConflict() || !_wanted.load()) break;
|
||||
if (socket < 4) {
|
||||
uint8_t address[4];
|
||||
SPIClass* spi = W5100.getSPI();
|
||||
spi->beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.readSnDIPR(socket, address);
|
||||
spi->endTransaction();
|
||||
const uint32_t remote = static_cast<uint32_t>(address[0]) |
|
||||
(static_cast<uint32_t>(address[1]) << 8) |
|
||||
(static_cast<uint32_t>(address[2]) << 16) |
|
||||
(static_cast<uint32_t>(address[3]) << 24);
|
||||
if (_socket != socket && !closeSocket(_socket)) { fault = true; break; }
|
||||
_transmit.reset();
|
||||
_txAccepted = 0;
|
||||
// A close/reject requested during blocking accept must also cancel the
|
||||
// just-accepted socket rather than briefly resurrecting the session.
|
||||
if (_closeRequested.exchange(false)) {
|
||||
if (!closeSocket(socket)) { fault = true; break; }
|
||||
publishClient(0xFF);
|
||||
} else {
|
||||
publishClient(socket, remote);
|
||||
}
|
||||
} else if (_socket < 4 && !mesh::nrf52::EthernetCliTransmit::connected(_socket)) {
|
||||
if (!closeSocket(_socket)) { fault = true; break; }
|
||||
_transmit.reset();
|
||||
_txAccepted = 0;
|
||||
publishClient(0xFF);
|
||||
}
|
||||
if (_checkCommands) continue;
|
||||
_ready.store(true);
|
||||
while (_ready.load() && _wanted.load() && !storageConflict()) {
|
||||
(void)ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(20));
|
||||
}
|
||||
while (_access.load() != Access::Worker && _wanted.load() && !storageConflict()) {
|
||||
(void)ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(20));
|
||||
}
|
||||
}
|
||||
takeWorkerAccess();
|
||||
if (_spiStarted) {
|
||||
if (!storageConflict() && hardware) {
|
||||
for (uint8_t socket = 0; socket < 4; ++socket) (void)closeSocket(socket);
|
||||
}
|
||||
ETHERNET_SPI_PORT.end();
|
||||
digitalWrite(PIN_ETHERNET_SS, HIGH);
|
||||
_spiStarted = false;
|
||||
}
|
||||
_socket = 0xFF;
|
||||
_eventPending.store(true);
|
||||
if (fault || _storageFault.load()) _state.store(State::Faulted);
|
||||
// No controller access follows this flag. Main reclaims the parked task.
|
||||
_stopped.store(true);
|
||||
for (;;) vTaskDelay(portMAX_DELAY);
|
||||
}
|
||||
@@ -1,58 +1,92 @@
|
||||
#pragma once
|
||||
|
||||
#include "../SerialEthernetInterface.h"
|
||||
#include "../../nrf52/EthernetCliTransmit.h"
|
||||
#include <SPI.h>
|
||||
#include <RAK13800_W5100S.h>
|
||||
#include <atomic>
|
||||
|
||||
class RAK13800EthernetInterface : public SerialEthernetInterface {
|
||||
|
||||
bool _isConnected;
|
||||
bool _spiStarted = false;
|
||||
bool _terminalStopping = false;
|
||||
EthernetServer server;
|
||||
EthernetClient client;
|
||||
enum class Access : uint8_t { Idle, Client, Worker };
|
||||
enum class State : uint8_t { Off, Starting, Retrying, Online, Stopping, Faulted, NoHardware, StartFailed };
|
||||
std::atomic<Access> _access{Access::Worker};
|
||||
std::atomic<State> _state{State::Off};
|
||||
std::atomic<bool> _wanted{false}, _stopped{false};
|
||||
std::atomic<bool> _storageFault{false}, _closeRequested{false}, _eventPending{false};
|
||||
std::atomic<bool> _ready{false};
|
||||
// Stop may revoke an unacknowledged event, so the socket remains atomic.
|
||||
std::atomic<uint8_t> _socket{0xFF};
|
||||
uint8_t _listener = 0xFF; // Worker-only; no virtual TCP stream is needed.
|
||||
uint8_t _listenerCommand = 0; // Acknowledgement also verifies the expected SR.
|
||||
uint32_t _eventIp = 0; // Published by _eventPending, unchanged during stop.
|
||||
std::atomic<uint32_t> _ip{0};
|
||||
TaskHandle_t _task = nullptr;
|
||||
bool _isConnected = false, _terminalStopping = false, _spiStarted = false;
|
||||
bool _checkCommands = false;
|
||||
uint32_t _checkStarted = 0;
|
||||
uint32_t _pollAt = 0; // Main-thread scheduling; worker never takes an idle bus.
|
||||
std::atomic<size_t> _txAccepted{0}; // Also pins the framed FIFO until ACK retirement.
|
||||
mesh::nrf52::EthernetCliTransmit _transmit;
|
||||
|
||||
class ClientAccess {
|
||||
RAK13800EthernetInterface& _owner;
|
||||
bool _acquired;
|
||||
public:
|
||||
explicit ClientAccess(RAK13800EthernetInterface& owner);
|
||||
~ClientAccess();
|
||||
explicit operator bool() const { return _acquired; }
|
||||
};
|
||||
bool beginHardware();
|
||||
bool storageConflict();
|
||||
bool finishRelease();
|
||||
void notifyWorker();
|
||||
void takeWorkerAccess();
|
||||
void requireCommandCheck();
|
||||
bool commandsIdle();
|
||||
bool closeSocket(uint8_t socket);
|
||||
uint8_t acceptClient();
|
||||
void publishClient(uint8_t socket, uint32_t ip = 0);
|
||||
void runWorker();
|
||||
static void workerTask(void* context);
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
bool (*_prepareSpi)(void*) = nullptr;
|
||||
bool (*_canReleaseSpi)(void*) = nullptr;
|
||||
bool (*_releasedSpi)(void*) = nullptr;
|
||||
void* _spiContext = nullptr;
|
||||
bool _ownsSharedSpi = false;
|
||||
bool _ownsSharedSpi = false, _releasePending = false;
|
||||
#endif
|
||||
const char* _spiError = "off";
|
||||
#endif
|
||||
|
||||
protected:
|
||||
void disconnectClient() override;
|
||||
bool shutdownForPowerOff(uint32_t timeout_ms);
|
||||
static bool terminalShutdown(void* context, uint32_t timeout_ms);
|
||||
protected:
|
||||
void disconnectClient() override;
|
||||
bool isIOUnavailable() const override;
|
||||
bool hasInFlightFrame() const override { return _txAccepted.load() != 0; }
|
||||
bool shutdownForPowerOff(uint32_t timeout_ms);
|
||||
static bool terminalShutdown(void* context, uint32_t timeout_ms);
|
||||
|
||||
public:
|
||||
RAK13800EthernetInterface() : server(EthernetServer(ETHERNET_TCP_PORT)) {
|
||||
_isConnected = false;
|
||||
}
|
||||
|
||||
bool begin();
|
||||
void loop() override;
|
||||
public:
|
||||
RAK13800EthernetInterface() = default;
|
||||
bool begin();
|
||||
void loop() override;
|
||||
bool tryEnable();
|
||||
bool tryDisable();
|
||||
bool hardwareActive() const;
|
||||
const char* last_error() const { return _spiError; }
|
||||
const char* status() const;
|
||||
uint32_t cachedIP() const { return _ip.load(); }
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
void setSpiOwnershipHooks(bool (*prepare)(void*), bool (*can_release)(void*),
|
||||
bool (*released)(void*), void* context) {
|
||||
_prepareSpi = prepare; _canReleaseSpi = can_release;
|
||||
_releasedSpi = released; _spiContext = context;
|
||||
}
|
||||
bool tryEnable();
|
||||
bool tryDisable();
|
||||
bool hardwareActive() const { return _spiStarted; }
|
||||
const char* last_error() const { return _spiError; }
|
||||
// The transport manager enables every registered interface at boot. Do
|
||||
// not turn that logical operation into a physical shared-bus acquisition.
|
||||
void enable() override { if (_spiStarted) SerialEthernetInterface::enable(); }
|
||||
void disable() override { (void)tryDisable(); }
|
||||
void setSpiOwnershipHooks(bool (*prepare)(void*), bool (*can_release)(void*),
|
||||
bool (*released)(void*), void* context) {
|
||||
_prepareSpi = prepare; _canReleaseSpi = can_release;
|
||||
_releasedSpi = released; _spiContext = context;
|
||||
}
|
||||
// Logical transport registration must not acquire the optional shared bus.
|
||||
void enable() override { if (_wanted.load()) SerialEthernetInterface::enable(); }
|
||||
#endif
|
||||
|
||||
// BaseSerialInterface methods
|
||||
bool isConnected() const override;
|
||||
|
||||
int available() override;
|
||||
int read() override;
|
||||
size_t write(const uint8_t *buf, size_t size) override;
|
||||
void disable() override { (void)tryDisable(); }
|
||||
bool isConnected() const override;
|
||||
bool hasPendingIO() const override { return hardwareActive() || SerialEthernetInterface::hasPendingIO(); }
|
||||
int available() override;
|
||||
int read() override;
|
||||
size_t write(const uint8_t *buf, size_t size) override;
|
||||
};
|
||||
@@ -40,7 +40,7 @@ size_t SerialEthernetInterface::writeFrame(const uint8_t src[], size_t len) {
|
||||
if (_isEnabled && isConnected() && src != nullptr && len > 0) {
|
||||
// A partly written frame must remain at the head even if a new response
|
||||
// displaces or reorders lower-priority frames in the waiting tail.
|
||||
const int pinned = send_offset != 0 ? 1 : 0;
|
||||
const int pinned = send_queue_len > 0 && (send_offset != 0 || hasInFlightFrame()) ? 1 : 0;
|
||||
int tail_len = send_queue_len - pinned;
|
||||
if (!mesh::enqueueCompanionFrame(send_queue + pinned, tail_len, FRAME_QUEUE_SIZE - pinned,
|
||||
src, len)) {
|
||||
@@ -54,7 +54,7 @@ size_t SerialEthernetInterface::writeFrame(const uint8_t src[], size_t len) {
|
||||
}
|
||||
|
||||
bool SerialEthernetInterface::isWriteBusy() const {
|
||||
return send_queue_len >= (FRAME_QUEUE_SIZE * 2 / 3);
|
||||
return isIOUnavailable() || send_queue_len >= (FRAME_QUEUE_SIZE * 2 / 3);
|
||||
}
|
||||
|
||||
bool SerialEthernetInterface::isReadBusy() const {
|
||||
@@ -96,9 +96,34 @@ void SerialEthernetInterface::rejectInput() {
|
||||
onClientDisconnected();
|
||||
}
|
||||
|
||||
bool SerialEthernetInterface::updateIOPause(uint32_t now) {
|
||||
const bool unavailable = isIOUnavailable();
|
||||
#if !ETHERNET_RAW_LINE
|
||||
if (!isReadBusy()) {
|
||||
_io_paused = false;
|
||||
_io_paused_at = 0;
|
||||
} else if (unavailable && !_io_paused) {
|
||||
// Exclude only time owned by the worker. Never revive a frame whose
|
||||
// original budget was already exhausted when backpressure was observed.
|
||||
if ((uint32_t)(now - _rx_started) < ETHERNET_FRAME_TIMEOUT_MS) {
|
||||
_io_paused_at = now;
|
||||
_io_paused = true;
|
||||
}
|
||||
} else if (!unavailable && _io_paused) {
|
||||
_rx_started += (uint32_t)(now - _io_paused_at);
|
||||
_io_paused_at = 0;
|
||||
_io_paused = false;
|
||||
}
|
||||
#else
|
||||
(void)now;
|
||||
#endif
|
||||
return unavailable;
|
||||
}
|
||||
|
||||
size_t SerialEthernetInterface::checkRecvFrame(uint8_t dest[]) {
|
||||
if (!_isEnabled) return 0;
|
||||
if (isConnected()) {
|
||||
if (updateIOPause((uint32_t)millis())) return 0;
|
||||
#if !ETHERNET_RAW_LINE
|
||||
if (isReadBusy() && (uint32_t)((uint32_t)millis() - _rx_started) >= ETHERNET_FRAME_TIMEOUT_MS) {
|
||||
rejectInput();
|
||||
@@ -130,6 +155,8 @@ size_t SerialEthernetInterface::checkRecvFrame(uint8_t dest[]) {
|
||||
#endif
|
||||
const size_t remaining = total - send_offset;
|
||||
const size_t written = write(pkt + send_offset, remaining);
|
||||
// A bounded backend write may hand socket completion to its worker.
|
||||
(void)updateIOPause((uint32_t)millis());
|
||||
send_offset += written < remaining ? written : remaining;
|
||||
if (send_offset < total) return 0;
|
||||
send_offset = 0;
|
||||
@@ -138,7 +165,11 @@ size_t SerialEthernetInterface::checkRecvFrame(uint8_t dest[]) {
|
||||
send_queue[i] = send_queue[i + 1];
|
||||
}
|
||||
} else {
|
||||
while (available()) {
|
||||
while (true) {
|
||||
// A read may likewise hand its receive-ACK command to the worker;
|
||||
// leave the rest of the frame untouched until hardware is available.
|
||||
if (updateIOPause((uint32_t)millis()) || !available()) break;
|
||||
if (updateIOPause((uint32_t)millis())) break;
|
||||
int c = read();
|
||||
if (c < 0) break;
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ class SerialEthernetInterface : public BaseSerialInterface {
|
||||
uint16_t _rx_len;
|
||||
uint8_t _rx_buf[MAX_FRAME_SIZE];
|
||||
uint32_t _rx_started = 0;
|
||||
uint32_t _io_paused_at = 0;
|
||||
bool _io_paused = false;
|
||||
size_t send_offset = 0;
|
||||
bool session_active = false;
|
||||
bool queue_has_ip = false;
|
||||
@@ -45,11 +47,24 @@ class SerialEthernetInterface : public BaseSerialInterface {
|
||||
_frame_len = 0;
|
||||
_rx_len = 0;
|
||||
_rx_started = 0;
|
||||
_io_paused_at = 0;
|
||||
_io_paused = false;
|
||||
}
|
||||
void rejectInput();
|
||||
bool updateIOPause(uint32_t now);
|
||||
|
||||
protected:
|
||||
virtual void disconnectClient() = 0;
|
||||
// A worker may own the hardware temporarily without ending the logical
|
||||
// client session. Do not poll sockets or expire input while it does.
|
||||
virtual bool isIOUnavailable() const { return false; }
|
||||
// A backend can have transmitted bytes before returning their ACKed count.
|
||||
// Keep that frame pinned even while send_offset still reads zero.
|
||||
virtual bool hasInFlightFrame() const { return false; }
|
||||
// Main-loop ownership handoffs must also service the timeout when another
|
||||
// transport's locked response route keeps checkRecvFrame() from running.
|
||||
// Workers must not call this: framing and session state remain main-only.
|
||||
void serviceIOAvailability() { (void)updateIOPause((uint32_t)millis()); }
|
||||
|
||||
public:
|
||||
SerialEthernetInterface() {
|
||||
|
||||
+180
-96
@@ -21,6 +21,7 @@
|
||||
#include "tinf/tinf.h"
|
||||
#if defined(OTA_RAK15002_SD_STORE) && defined(COMPANION_RADIO_FULL)
|
||||
#include <stdlib.h>
|
||||
#include "Utils.h"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -34,95 +35,54 @@ enum { OTA_TARGET_ENV_NAME_CAPACITY = 68 };
|
||||
inline const char* ota_target_env_name(uint32_t target_id, char* buffer, size_t capacity) {
|
||||
if (!buffer || !capacity) return nullptr;
|
||||
buffer[0] = 0;
|
||||
static const uint32_t IDS[] = {
|
||||
0x149bcb23, 0x30d4e442, 0x263a8d80, 0x9070ae2c, 0xe0aea3d5, 0xf190d834, 0x0773c386, 0x32890982,
|
||||
0xacb599d5, 0x82d3b273, 0xe96a6af8, 0x180bbe1f, 0xf9986af5, 0x98519a1d, 0x06b71719, 0x6384719d,
|
||||
0x4c0461c8, 0x4fa88992, 0xcd6604d0, 0xc54ea258, 0x217958b4, 0x03d9704a, 0x8456f753, 0xc8104197,
|
||||
0xf5eb61ec, 0x70d2d590, 0xd5e04824, 0xa416bb20, 0x875f3744, 0xdaf349be, 0x93b93199, 0x710abb59,
|
||||
0x5293da13, 0xc6dd7723, 0xf2eec98c, 0x48a986f1, 0xd2cf978c, 0x53626334, 0xefe4f19f, 0xfcaf3730,
|
||||
0xd90deef7, 0x2d2a4089, 0x89ea5c4e, 0xc0eb712a, 0x23b8a187, 0x67293f43, 0x4cc23248, 0x3f364910,
|
||||
0xac4b4eb5, 0x822e96ca, 0xe077fb8d, 0x2bbe7fd8, 0x9e40157c, 0x8d8bde08, 0xb12e6e01, 0x930c9fa0,
|
||||
0xf6f835ec, 0x7fab017f, 0x9e23928a, 0xf310a4d1, 0xdd5953ec, 0x97574bc2, 0x2d918a3c, 0xd1755faa,
|
||||
0x89c28271, 0x8a6ceaf0, 0xba2943f4, 0x57f69746, 0xbc05d6a7, 0x5bad80f1, 0xb4f727a8, 0x112d34c4,
|
||||
0xf10a26df, 0xc5409ced, 0xf9022545, 0x75f74679, 0x2023036d, 0x6600f959, 0x4abd24af, 0x35d42b35,
|
||||
0x9f736ca7, 0x3fbb1498, 0x42b0818c, 0x80dc72a9, 0xafeed10c, 0x98411ac6, 0xa14d9f4a, 0x6494fda0,
|
||||
0x32bcdd14, 0xfe937f14, 0x5b9b1933, 0x1b4399de, 0xf7e47568, 0xb1153422, 0x388f2804, 0x075ca3f9,
|
||||
0x2f9f8e7d, 0xc9dc5d8c, 0x34b0e5f3, 0xb4d0ad97, 0xcfa0e233, 0x17d5e1ff, 0xc99313c6, 0xffb3854f,
|
||||
0x21a73973, 0x3067f656, 0xfdb25057, 0x836e5931, 0x0f1767cd, 0x57172fcc, 0xa529b4f5, 0x85a8c944,
|
||||
0x3c60586d, 0x0a9dbbf0, 0xb519a89f, 0xa3e8d8ce, 0xb16009a0, 0xdc780e80, 0x28b8007c, 0x4aa63180,
|
||||
0x6fddcbf1, 0xf4fb5484, 0x2e5384e2, 0xb62a1e75, 0x9e64845d, 0x19232e8b, 0x10f41878, 0xfaf164f4,
|
||||
0x38b948b4, 0x462ddb9b, 0xed21ca4f, 0x9f5f39a7, 0x081d2219, 0x7c498bc4, 0xc283ad60, 0x4fbdc1e1,
|
||||
0xc797fd73, 0xab9871c8, 0x2075687c, 0x5d726067, 0x9969bbc9, 0x804f7a3a, 0x0823a21a, 0x22f900de,
|
||||
0xaca0e153, 0x516c9c13, 0xcb1c2e65, 0xd1b29b18, 0x644bb68d, 0xd19a9759, 0xde40f56f, 0x6dc15ab3,
|
||||
0xc59d294e, 0xceeddfaf, 0x21519537, 0x382bb181, 0xad1bcd63, 0x5a2980e6, 0x90581c83, 0x90a6a854,
|
||||
0x356df17c, 0x99a084fc, 0x64c9dfc9, 0x2a2ffc92, 0x2c13b8e9, 0x1ee7d867, 0xb016979a, 0xb49b6b55,
|
||||
0x01d8f124, 0x64140fd9, 0x86fb85f0, 0xd33b0986, 0x1a4d0096, 0xa8f5940f, 0x1cf5e14c, 0x2707d3d1,
|
||||
0x86ac3b1b, 0x7af848da, 0xd522a14f, 0x76657d76, 0x21ee03bd, 0x5977d941, 0xe2a1907b, 0xbb0ca8db,
|
||||
0xf75feb3e, 0xb0a1e71f, 0xbc9cdc70, 0x19e31fc6, 0xa01b330e, 0x36c78d86, 0xdf722326, 0xfa901c87,
|
||||
0xeebe3469, 0xb0f5aff6, 0x5729ad07, 0xaa2aef42, 0xb4cb035a, 0xbb429dde, 0x883e3edc, 0x4a9d1c6c,
|
||||
0xa0965a6b, 0x59ea25fa, 0x1758bdb9, 0x1313f16d, 0x0372b0f1, 0xa2fb4192, 0xdd39457b, 0x7964461c,
|
||||
0x1d1c0d92, 0x5fc52bcb, 0x0dacb683, 0xe792a051, 0x2d5ea842, 0x30c59b1d, 0xab74db9e, 0xeed78ce4,
|
||||
0x7778b7e1, 0x0b6847ed, 0xab3e1ad3, 0x04a8b763, 0xc456437e, 0xb3ecafca, 0xf3afcde6, 0x87513d56,
|
||||
0xca812b41, 0x18594231, 0x188f3ac1, 0x402055ec, 0x12393f1f, 0x16fbab07, 0xed3ef5b8, 0x8ea86381,
|
||||
0x09f57289, 0x148aeaab, 0xcbb2bf38, 0x6e9f7fec, 0x9a340cd3, 0x6a25d024, 0xc5a2cb41, 0xc4a11710,
|
||||
0x753947a4, 0xe6fa98b4, 0xf8c27cf4, 0xa139c6ae, 0x90e227d9, 0x89e8d957, 0x812f5e52, 0x61f961c5,
|
||||
0x1ddd15c8, 0x49d67d0d, 0xbc003322, 0x835b70c0, 0xd7460575, 0x36acced5, 0x33e2c171, 0x5be91148,
|
||||
0xa165ae99, 0x04c5c2b7, 0x6c1ca659, 0xa295fb04, 0x61c405ca, 0xe893fe91, 0x2b599d31, 0xf1442d25,
|
||||
0x15082ede, 0x6ff350d1, 0xe03d0cd9, 0x82e1eb2c, 0x0ee19b12, 0x6c67ed89, 0xd1c05d0d, 0xb90150b0,
|
||||
0x7285bdb3, 0x77cadc99, 0xc177e6cd, 0xf7a38993, 0x83762f33, 0x86cadda9, 0x7e49da49, 0x305d6f92,
|
||||
0xbc6b870b, 0x78845fdd, 0x6d35f434, 0xa52d76a8, 0xd8de9639, 0x8ab5c51b, 0x80097676, 0xffe70df5,
|
||||
0x3da80417, 0x47ca3c13, 0x28b499c7, 0xf1ab4c28, 0xe5331f02, 0xfba7177b, 0xde7dc472, 0x3625bb80,
|
||||
0x69df7765, 0x988cc4e1, 0xe9fbc9be, 0x40818863, 0x65deac33, 0xd86cf67a, 0xed73ac03, 0xab0d550a,
|
||||
0x1bd2a266, 0x3655c748, 0x9a5ae8fc, 0x1e9eb97a, 0x139536f4, 0x0716c724, 0x8de23fa7, 0xb92ec96b,
|
||||
0x420ebc63, 0x967ccaee, 0x8021afa2, 0x5f7c7688, 0xbd04e6e7, 0x1aeac884, 0x07849209, 0x0c79b100,
|
||||
0xbfb60e7c, 0x8a63de3a, 0x96036a68, 0xb76a745c, 0x43023193, 0xa7d97db7, 0x468a133e, 0xb59ea2ed,
|
||||
0xcb75495b, 0xbe372c6d, 0xb762985a, 0x6215e9eb, 0x07907102, 0x3898ea56, 0x2fb7e218, 0x8c61d4c8,
|
||||
0x4f722e70, 0x694eed25, 0x85b8b9d7, 0xe8421e36, 0x82eaaf9e, 0x9f9caba4, 0xe4a2de74, 0x582c7826,
|
||||
0xaeb637f1, 0x86813271, 0x67b2ff84, 0xd0e2e616, 0x3f9edfe3, 0xc23f8082, 0x0e74ae16, 0x8f256e56,
|
||||
0x6915c6e1, 0xf1407f39, 0x1b4dc5ab, 0xe90a3181, 0x3b1b4237, 0x21c6d08d, 0x34302c14, 0x268d6fc1,
|
||||
0xeabe3b40, 0x3c172d27, 0xf7c5d584, 0x81960cc0, 0x504020ea, 0x9d8b60ae, 0xa2ce002f, 0xba4f47f3,
|
||||
0x6f16479e, 0x1018e5d1, 0xc0ab5040, 0xa2bf90bd, 0xe6bff8f9, 0xb9cd26cb, 0x9bc34bd5, 0x144504b0,
|
||||
0xdbfe2675, 0xb00241d0, 0x88e77fee, 0xae2d9caa, 0xa0043e00, 0x26bbf4ed, 0xdcc0f158, 0xe3320463,
|
||||
0x852b928a, 0x0ec870e9, 0x8f00c1f2, 0xcea95e20, 0xd45277ff, 0x2cad9a9d, 0x02a4a8c1, 0x4a709348,
|
||||
0x35d960bd, 0x62977023, 0x17a7ed8c, 0x07f5f5b5, 0xfd0d4b8f, 0x8d88f33c, 0xf5afb7dd, 0x3a0ea830,
|
||||
0x6ebf28cb, 0x2d589527, 0x77d0810c, 0x11b131e7, 0x54850763, 0x0fa61805, 0x8039574f, 0xa65555d6,
|
||||
0x22b1804c, 0xa9f631b1, 0x49d11eca, 0xd41f0638, 0xe3105eb8, 0xba76cc33, 0x944263f8, 0xc7a8c754,
|
||||
0xf8259880, 0x063f1f00, 0x70d71f92, 0xa8a79da5, 0xc0d3815b, 0x966ac88e, 0x104b3f8b, 0x23b49348,
|
||||
0xc42cc90d, 0xc24791c3, 0x15e11067, 0x2dbb2669, 0x90799c32, 0x881a6a48, 0xf26af51c, 0x8174cbdc,
|
||||
0x0bf0f061, 0x2509dd80, 0x56293b24, 0x8c81078c, 0xbd6a1c97, 0xf4d98c12, 0xccac4283, 0xa68c09e9,
|
||||
0x5f606274, 0x84923735, 0x2c0b8b05, 0xdba69401, 0xcb6555ee, 0xd60daf25, 0x410bd800, 0x9aca380e,
|
||||
0xd8641710, 0x4d0b1601, 0xab2ac09e, 0xa17fc4f7, 0xe4b2bd22, 0x90c4eb3f, 0x19d9ce91, 0x121cc72f,
|
||||
0xfa8510fa, 0xd34d8461, 0x98077550, 0x11628e29, 0x2ed959aa, 0xf1e7cadb, 0xced7e127, 0x6925aaf0,
|
||||
0x2fa509c1, 0xa52ba3c8, 0xaf0b81dd, 0x9f4d58ac, 0x7a55dec3, 0x5e69738a, 0x48d3b5d4, 0x14e6965d,
|
||||
0x76f3c984, 0x41e33a97, 0x29a0da19, 0x29bc38dc, 0xd5fc71d4, 0x05f5ffae, 0xc62da371, 0x96fc4c26,
|
||||
0x8a6dc63b, 0xb48950bb, 0xf1d3c5a8, 0xc6d55752, 0x60683191, 0x8a7cb79b, 0x4fa4ae55, 0xd1d61a8b,
|
||||
0x9cb0b232, 0x51fa1a49, 0xdb73638b, 0xd904c2d5, 0x3a14aac7, 0x620569f4, 0x08483f6b, 0x0db48f35,
|
||||
0x20532f6c, 0x293ced32, 0xb38ebf22, 0x7c89c73d, 0x2d7c11e8, 0x5bb567cd, 0x61b32037, 0xad1cd53d,
|
||||
0x0e18b360, 0x3caf966e, 0xcb59ea4e, 0xe3a28284, 0xa8cb8e8b, 0x05747eb6, 0x75587f24, 0xf2128c81,
|
||||
0x1b34e7b4, 0x8e549407, 0x6ca142f4, 0x1deac038, 0x2a2f303b, 0x0ba4453d, 0xf8d1958f, 0x75e85141,
|
||||
0xd62ed92e, 0x7abaf0d5, 0x87edc08b, 0xc50b0e48, 0x3c49caf8, 0x58689ef9, 0x91878dd8, 0x72a93c45,
|
||||
0x347e087a, 0x4dbf035e, 0xff4102d6, 0x2f9a9185, 0x000fc519, 0x7b071fa0, 0x3e55e34d, 0x54606a4f,
|
||||
0x16482630, 0xce0f07ad, 0x01c43d49, 0x51fe3801, 0x857149e5, 0xd9d64938, 0xcb7da3f5, 0x9952fbe3,
|
||||
0x3729cd74, 0xdc27c37c, 0x9b9f8775, 0xb27514a3, 0xe9ce038c, 0xe295a7a4, 0x189660b3, 0x99907475,
|
||||
0x2082078d, 0x24a94c81, 0x3750e80d, 0xe4b49973, 0xe1372b52, 0x5a79bac1, 0x6f339514, 0x8eaa289e,
|
||||
0x70e91bd9, 0x5a7e9c2d, 0x1211b151, 0x0a32044b, 0xa13fe853, 0x79b9ff01, 0xc3decea2, 0x3c012018,
|
||||
0x6afd6f75, 0x7517d400, 0xd7598668, 0x61bdb52a, 0x413287d8, 0xe8cd2377, 0x3309bdaf, 0x7330afbb,
|
||||
0x9df48ce6, 0xa5cb783a, 0x4b9f8283, 0x311846c9, 0x8b6b29b7, 0x37928233, 0x88ac45da, 0x087f99c0,
|
||||
0xdf41affe, 0x2afe6c63, 0xf2d61a1c, 0x14b1b097, 0xec334f3f, 0x1da30414, 0xae8fe1c9, 0x67acd73f,
|
||||
0xfb118ed8, 0xe4213729, 0x1298038d, 0x2c47acff, 0x7d641646, 0x51efdbfe, 0xf2aada5b, 0xe59c9373,
|
||||
0x236f605c, 0x93994fed, 0x66c47cfd, 0x48061a01, 0x7ed7137d, 0xa01688a7, 0xa5f4a562, 0xef83222b,
|
||||
0xc36ec867, 0xb932238e, 0x92f5bbea, 0x26d91ab2, 0x708ac4dc, 0xdc51bf13, 0xc27f8304, 0x7190df40,
|
||||
0x609d50b3, 0x5718c7ea, 0x7bbf1081, 0xb151c2aa, 0x3b0174fd, 0xc1fd2578, 0xeb440352, 0xa297f85a,
|
||||
0xcbe54778, 0x0343bdf0, 0x3de413b1, 0x37443a5b, 0x603384a7, 0x76456c88, 0x28c4948b, 0xb34a83c5,
|
||||
0x63fe9591, 0xce3ef40f, 0x475a2a6a, 0xf4175d2c, 0x3d831006, 0x556eb1bc, 0xb67370b3, 0x0c689b90,
|
||||
0x91eac424, 0x7a84a8c4, 0x67843a5f, 0x0e83fd51, 0xfad357ab, 0x16af5c67, 0x8c105104, 0xa80042a0,
|
||||
0x4bff0748, 0x77c8f2b5, 0x71ee0e15, 0x8824cd0c, 0xe5439198, 0xe90a6e74, 0x833e5cdc, 0x93145fbb,
|
||||
0x8d3dd6d5, 0x33a154ff, 0x6658b418, 0x0cfb9c00, 0x964f4f5c, 0x0a5dc760, 0xd08496ce, 0x293ac2c8,
|
||||
0x23b70842, 0x60ebfa87, 0x0ea78cab, 0x60f5fdc2, 0xe8f79d22, 0xb97bbb15, 0xd466590c, 0x9f27be15,
|
||||
0x396fecc7, 0xf94e3407, 0xe1930103, 0xa4740508, 0x9f19bcd1, 0xfcebff61, 0xac5c1405, 0xccb734e2,
|
||||
0x634ff632, 0xf967a300, 0xa0cc1c36,
|
||||
};
|
||||
#if defined(OTA_RAK15002_SD_STORE) && defined(COMPANION_RADIO_FULL)
|
||||
// Fingerprints filter candidates only. Reconstructed names must match the entire
|
||||
// existing SHA-256:4 target ID, so collisions never produce a wrong target name.
|
||||
static const uint8_t FINGERPRINTS[] = {
|
||||
0x23, 0x42, 0x80, 0x2c, 0xd5, 0x34, 0x86, 0x82, 0xd5, 0x73, 0xf8, 0x1f, 0xf5, 0x1d, 0x19, 0x9d,
|
||||
0xc8, 0x92, 0xd0, 0x58, 0xb4, 0x4a, 0x53, 0x97, 0xec, 0x90, 0x24, 0x20, 0x44, 0xbe, 0x99, 0x59,
|
||||
0x13, 0x23, 0x8c, 0xf1, 0x8c, 0x34, 0x9f, 0x30, 0xf7, 0x89, 0x4e, 0x2a, 0x87, 0x43, 0x48, 0x10,
|
||||
0xb5, 0xca, 0x8d, 0xd8, 0x7c, 0x08, 0x01, 0xa0, 0xec, 0x7f, 0x8a, 0xd1, 0xec, 0xc2, 0x3c, 0xaa,
|
||||
0x71, 0xf0, 0xf4, 0x46, 0xa7, 0xf1, 0xa8, 0xc4, 0xdf, 0xed, 0x45, 0x79, 0x6d, 0x59, 0xaf, 0x35,
|
||||
0xa7, 0x98, 0x8c, 0xa9, 0x0c, 0xc6, 0x4a, 0xa0, 0x14, 0x14, 0x33, 0xde, 0x68, 0x22, 0x04, 0xf9,
|
||||
0x7d, 0x8c, 0xf3, 0x97, 0x33, 0xff, 0xc6, 0x4f, 0x73, 0x56, 0x57, 0x31, 0xcd, 0xcc, 0xf5, 0x44,
|
||||
0x6d, 0xf0, 0x9f, 0xce, 0xa0, 0x80, 0x7c, 0x80, 0xf1, 0x84, 0xe2, 0x75, 0x5d, 0x8b, 0x78, 0xf4,
|
||||
0xb4, 0x9b, 0x4f, 0xa7, 0x19, 0xc4, 0x60, 0xe1, 0x73, 0xc8, 0x7c, 0x67, 0xc9, 0x3a, 0x1a, 0xde,
|
||||
0x53, 0x13, 0x65, 0x18, 0x8d, 0x59, 0x6f, 0xb3, 0x4e, 0xaf, 0x37, 0x81, 0x63, 0xe6, 0x83, 0x54,
|
||||
0x7c, 0xfc, 0xc9, 0x92, 0xe9, 0x67, 0x9a, 0x55, 0x24, 0xd9, 0xf0, 0x86, 0x96, 0x0f, 0x4c, 0xd1,
|
||||
0x1b, 0xda, 0x4f, 0x76, 0xbd, 0x41, 0x7b, 0xdb, 0x3e, 0x1f, 0x70, 0xc6, 0x0e, 0x86, 0x26, 0x87,
|
||||
0x69, 0xf6, 0x07, 0x42, 0x5a, 0xde, 0xdc, 0x6c, 0x6b, 0xfa, 0xb9, 0x6d, 0xf1, 0x92, 0x7b, 0x1c,
|
||||
0x92, 0xcb, 0x83, 0x51, 0x42, 0x1d, 0x9e, 0xe4, 0xe1, 0xed, 0xd3, 0x63, 0x7e, 0xca, 0xe6, 0x56,
|
||||
0x41, 0x31, 0xc1, 0xec, 0x1f, 0x07, 0xb8, 0x81, 0x89, 0xab, 0x38, 0xec, 0xd3, 0x24, 0x41, 0x10,
|
||||
0xa4, 0xb4, 0xf4, 0xae, 0xd9, 0x57, 0x52, 0xc5, 0xc8, 0x0d, 0x22, 0xc0, 0x75, 0xd5, 0x71, 0x48,
|
||||
0x99, 0xb7, 0x59, 0x04, 0xca, 0x91, 0x31, 0x25, 0xde, 0xd1, 0xd9, 0x2c, 0x12, 0x89, 0x0d, 0xb0,
|
||||
0xb3, 0x99, 0xcd, 0x93, 0x33, 0xa9, 0x49, 0x92, 0x0b, 0xdd, 0x34, 0xa8, 0x39, 0x1b, 0x76, 0xf5,
|
||||
0x17, 0x13, 0xc7, 0x28, 0x02, 0x7b, 0x72, 0x80, 0x65, 0xe1, 0xbe, 0x63, 0x33, 0x7a, 0x03, 0x0a,
|
||||
0x66, 0x48, 0xfc, 0x7a, 0xf4, 0x24, 0xa7, 0x6b, 0x63, 0xee, 0xa2, 0x88, 0xe7, 0x84, 0x09, 0x00,
|
||||
0x7c, 0x3a, 0x68, 0x5c, 0x93, 0xb7, 0x3e, 0xed, 0x5b, 0x6d, 0x5a, 0xeb, 0x02, 0x56, 0x18, 0xc8,
|
||||
0x70, 0x25, 0xd7, 0x36, 0x9e, 0xa4, 0x74, 0x26, 0xf1, 0x71, 0x84, 0x16, 0xe3, 0x82, 0x16, 0x56,
|
||||
0xe1, 0x39, 0xab, 0x81, 0x37, 0x8d, 0x14, 0xc1, 0x40, 0x27, 0x84, 0xc0, 0xea, 0xae, 0x2f, 0xf3,
|
||||
0x9e, 0xd1, 0x40, 0xbd, 0xf9, 0xcb, 0xd5, 0xb0, 0x75, 0xd0, 0xee, 0xaa, 0x00, 0xed, 0x58, 0x63,
|
||||
0x8a, 0xe9, 0xf2, 0x20, 0xff, 0x9d, 0xc1, 0x48, 0xbd, 0x23, 0x8c, 0xb5, 0x8f, 0x3c, 0xdd, 0x30,
|
||||
0xcb, 0x27, 0x0c, 0xe7, 0x63, 0x05, 0x4f, 0xd6, 0x4c, 0xb1, 0xca, 0x38, 0xb8, 0x33, 0xf8, 0x54,
|
||||
0x80, 0x00, 0x92, 0xa5, 0x5b, 0x8e, 0x8b, 0x48, 0x0d, 0xc3, 0x67, 0x69, 0x32, 0x48, 0x1c, 0xdc,
|
||||
0x61, 0x80, 0x24, 0x8c, 0x97, 0x12, 0x83, 0xe9, 0x74, 0x35, 0x05, 0x01, 0xee, 0x25, 0x00, 0x0e,
|
||||
0x10, 0x01, 0x9e, 0xf7, 0x22, 0x3f, 0x91, 0x2f, 0xfa, 0x61, 0x50, 0x29, 0xaa, 0xdb, 0x27, 0xf0,
|
||||
0xc1, 0xc8, 0xdd, 0xac, 0xc3, 0x8a, 0xd4, 0x5d, 0x84, 0x97, 0x19, 0xdc, 0xd4, 0xae, 0x71, 0x26,
|
||||
0x3b, 0xbb, 0xa8, 0x52, 0x91, 0x9b, 0x55, 0x8b, 0x32, 0x49, 0x8b, 0xd5, 0xc7, 0xf4, 0x6b, 0x35,
|
||||
0x6c, 0x32, 0x22, 0x3d, 0xe8, 0xcd, 0x37, 0x3d, 0x60, 0x6e, 0x4e, 0x84, 0x8b, 0xb6, 0x24, 0x81,
|
||||
0xb4, 0x07, 0xf4, 0x38, 0x3b, 0x3d, 0x8f, 0x41, 0x2e, 0xd5, 0x8b, 0x48, 0xf8, 0xf9, 0xd8, 0x45,
|
||||
0x7a, 0x5e, 0xd6, 0x85, 0x19, 0xa0, 0x4d, 0x4f, 0x30, 0xad, 0x49, 0x01, 0xe5, 0x38, 0xf5, 0xe3,
|
||||
0x74, 0x7c, 0x75, 0xa3, 0x8c, 0xa4, 0xb3, 0x75, 0x8d, 0x81, 0x0d, 0x73, 0x52, 0xc1, 0x14, 0x9e,
|
||||
0xd9, 0x2d, 0x51, 0x4b, 0x53, 0x01, 0xa2, 0x18, 0x75, 0x00, 0x68, 0x2a, 0xd8, 0x77, 0xaf, 0xbb,
|
||||
0xe6, 0x3a, 0x83, 0xc9, 0xb7, 0x33, 0xda, 0xc0, 0xfe, 0x63, 0x1c, 0x97, 0x3f, 0x14, 0xc9, 0x3f,
|
||||
0xd8, 0x29, 0x8d, 0xff, 0x46, 0xfe, 0x5b, 0x73, 0x5c, 0xed, 0xfd, 0x01, 0x7d, 0xa7, 0x62, 0x2b,
|
||||
0x67, 0x8e, 0xea, 0xb2, 0xdc, 0x13, 0x04, 0x40, 0xb3, 0xea, 0x81, 0xaa, 0xfd, 0x78, 0x52, 0x5a,
|
||||
0x78, 0xf0, 0xb1, 0x5b, 0xa7, 0x88, 0x8b, 0xc5, 0x91, 0x0f, 0x6a, 0x2c, 0x06, 0xbc, 0xb3, 0x90,
|
||||
0x24, 0xc4, 0x5f, 0x51, 0xab, 0x67, 0x04, 0xa0, 0x48, 0xb5, 0x15, 0x0c, 0x98, 0x74, 0xdc, 0xbb,
|
||||
0xd5, 0xff, 0x18, 0x00, 0x5c, 0x60, 0xce, 0xc8, 0x42, 0x87, 0xab, 0xc2, 0x22, 0x15, 0x0c, 0x15,
|
||||
0xc7, 0x07, 0x03, 0x08, 0xd1, 0x61, 0x05, 0xe2, 0x32, 0x00, 0x36,
|
||||
};
|
||||
enum { DECODE_CHUNK_BYTES = 4096 };
|
||||
static const uint16_t OFFSETS[] = {
|
||||
0, 1066, 2165, 2715,
|
||||
@@ -306,7 +266,138 @@ inline const char* ota_target_env_name(uint32_t target_id, char* buffer, size_t
|
||||
0x3d, 0xf5, 0xef, 0xd7, 0x02, 0xea, 0x56, 0xe3, 0x5e, 0x4f, 0xe3, 0xbe, 0xaa, 0x8e, 0x86, 0xa3,
|
||||
0xf1, 0x80, 0xa7, 0xb1, 0x3e, 0xd3, 0x58, 0xaf, 0xde, 0xae, 0xaf,
|
||||
};
|
||||
// Rare display lookup: one heap buffer, each candidate chunk decoded once.
|
||||
// Keep complete names private until both ID and caller capacity are validated.
|
||||
struct Scratch { uint8_t* bytes; ~Scratch() { free(bytes); } } scratch = {nullptr};
|
||||
char name[OTA_TARGET_ENV_NAME_CAPACITY];
|
||||
const uint8_t fingerprint = (uint8_t)target_id;
|
||||
for (unsigned group = 0; group < sizeof(RAW_LENGTHS) / sizeof(RAW_LENGTHS[0]); group++) {
|
||||
unsigned candidate = FIRSTS[group];
|
||||
while (candidate < FIRSTS[group + 1] && FINGERPRINTS[candidate] != fingerprint) candidate++;
|
||||
if (candidate == FIRSTS[group + 1]) continue;
|
||||
if (!scratch.bytes) scratch.bytes = (uint8_t*)malloc(DECODE_CHUNK_BYTES);
|
||||
if (!scratch.bytes) return nullptr;
|
||||
uint8_t* raw = scratch.bytes;
|
||||
unsigned int produced = DECODE_CHUNK_BYTES;
|
||||
if (tinf_uncompress_exact(raw, &produced, NAMES + OFFSETS[group],
|
||||
OFFSETS[group + 1] - OFFSETS[group]) != TINF_OK ||
|
||||
produced != RAW_LENGTHS[group]) return nullptr;
|
||||
const uint8_t* record = raw;
|
||||
const uint8_t* end = raw + produced;
|
||||
size_t previous_length = 0;
|
||||
for (unsigned row = FIRSTS[group]; row < FIRSTS[group + 1]; row++) {
|
||||
if (record == end) return nullptr;
|
||||
size_t length = *record++;
|
||||
if (length > previous_length || length >= sizeof(name)) return nullptr;
|
||||
while (record < end && *record) {
|
||||
if (length + 1 >= sizeof(name)) return nullptr;
|
||||
name[length++] = (char)*record++;
|
||||
}
|
||||
if (record == end) return nullptr;
|
||||
record++;
|
||||
name[length] = 0;
|
||||
previous_length = length;
|
||||
if (FINGERPRINTS[row] != fingerprint) continue;
|
||||
uint8_t hash[4];
|
||||
mesh::Utils::sha256(hash, sizeof(hash), (const uint8_t*)name, length);
|
||||
const uint32_t id = (uint32_t)hash[0] | (uint32_t)hash[1] << 8 |
|
||||
(uint32_t)hash[2] << 16 | (uint32_t)hash[3] << 24;
|
||||
if (id != target_id) continue;
|
||||
if (length >= capacity) return nullptr;
|
||||
memcpy(buffer, name, length + 1);
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
#else
|
||||
static const uint32_t IDS[] = {
|
||||
0x149bcb23, 0x30d4e442, 0x263a8d80, 0x9070ae2c, 0xe0aea3d5, 0xf190d834, 0x0773c386, 0x32890982,
|
||||
0xacb599d5, 0x82d3b273, 0xe96a6af8, 0x180bbe1f, 0xf9986af5, 0x98519a1d, 0x06b71719, 0x6384719d,
|
||||
0x4c0461c8, 0x4fa88992, 0xcd6604d0, 0xc54ea258, 0x217958b4, 0x03d9704a, 0x8456f753, 0xc8104197,
|
||||
0xf5eb61ec, 0x70d2d590, 0xd5e04824, 0xa416bb20, 0x875f3744, 0xdaf349be, 0x93b93199, 0x710abb59,
|
||||
0x5293da13, 0xc6dd7723, 0xf2eec98c, 0x48a986f1, 0xd2cf978c, 0x53626334, 0xefe4f19f, 0xfcaf3730,
|
||||
0xd90deef7, 0x2d2a4089, 0x89ea5c4e, 0xc0eb712a, 0x23b8a187, 0x67293f43, 0x4cc23248, 0x3f364910,
|
||||
0xac4b4eb5, 0x822e96ca, 0xe077fb8d, 0x2bbe7fd8, 0x9e40157c, 0x8d8bde08, 0xb12e6e01, 0x930c9fa0,
|
||||
0xf6f835ec, 0x7fab017f, 0x9e23928a, 0xf310a4d1, 0xdd5953ec, 0x97574bc2, 0x2d918a3c, 0xd1755faa,
|
||||
0x89c28271, 0x8a6ceaf0, 0xba2943f4, 0x57f69746, 0xbc05d6a7, 0x5bad80f1, 0xb4f727a8, 0x112d34c4,
|
||||
0xf10a26df, 0xc5409ced, 0xf9022545, 0x75f74679, 0x2023036d, 0x6600f959, 0x4abd24af, 0x35d42b35,
|
||||
0x9f736ca7, 0x3fbb1498, 0x42b0818c, 0x80dc72a9, 0xafeed10c, 0x98411ac6, 0xa14d9f4a, 0x6494fda0,
|
||||
0x32bcdd14, 0xfe937f14, 0x5b9b1933, 0x1b4399de, 0xf7e47568, 0xb1153422, 0x388f2804, 0x075ca3f9,
|
||||
0x2f9f8e7d, 0xc9dc5d8c, 0x34b0e5f3, 0xb4d0ad97, 0xcfa0e233, 0x17d5e1ff, 0xc99313c6, 0xffb3854f,
|
||||
0x21a73973, 0x3067f656, 0xfdb25057, 0x836e5931, 0x0f1767cd, 0x57172fcc, 0xa529b4f5, 0x85a8c944,
|
||||
0x3c60586d, 0x0a9dbbf0, 0xb519a89f, 0xa3e8d8ce, 0xb16009a0, 0xdc780e80, 0x28b8007c, 0x4aa63180,
|
||||
0x6fddcbf1, 0xf4fb5484, 0x2e5384e2, 0xb62a1e75, 0x9e64845d, 0x19232e8b, 0x10f41878, 0xfaf164f4,
|
||||
0x38b948b4, 0x462ddb9b, 0xed21ca4f, 0x9f5f39a7, 0x081d2219, 0x7c498bc4, 0xc283ad60, 0x4fbdc1e1,
|
||||
0xc797fd73, 0xab9871c8, 0x2075687c, 0x5d726067, 0x9969bbc9, 0x804f7a3a, 0x0823a21a, 0x22f900de,
|
||||
0xaca0e153, 0x516c9c13, 0xcb1c2e65, 0xd1b29b18, 0x644bb68d, 0xd19a9759, 0xde40f56f, 0x6dc15ab3,
|
||||
0xc59d294e, 0xceeddfaf, 0x21519537, 0x382bb181, 0xad1bcd63, 0x5a2980e6, 0x90581c83, 0x90a6a854,
|
||||
0x356df17c, 0x99a084fc, 0x64c9dfc9, 0x2a2ffc92, 0x2c13b8e9, 0x1ee7d867, 0xb016979a, 0xb49b6b55,
|
||||
0x01d8f124, 0x64140fd9, 0x86fb85f0, 0xd33b0986, 0x1a4d0096, 0xa8f5940f, 0x1cf5e14c, 0x2707d3d1,
|
||||
0x86ac3b1b, 0x7af848da, 0xd522a14f, 0x76657d76, 0x21ee03bd, 0x5977d941, 0xe2a1907b, 0xbb0ca8db,
|
||||
0xf75feb3e, 0xb0a1e71f, 0xbc9cdc70, 0x19e31fc6, 0xa01b330e, 0x36c78d86, 0xdf722326, 0xfa901c87,
|
||||
0xeebe3469, 0xb0f5aff6, 0x5729ad07, 0xaa2aef42, 0xb4cb035a, 0xbb429dde, 0x883e3edc, 0x4a9d1c6c,
|
||||
0xa0965a6b, 0x59ea25fa, 0x1758bdb9, 0x1313f16d, 0x0372b0f1, 0xa2fb4192, 0xdd39457b, 0x7964461c,
|
||||
0x1d1c0d92, 0x5fc52bcb, 0x0dacb683, 0xe792a051, 0x2d5ea842, 0x30c59b1d, 0xab74db9e, 0xeed78ce4,
|
||||
0x7778b7e1, 0x0b6847ed, 0xab3e1ad3, 0x04a8b763, 0xc456437e, 0xb3ecafca, 0xf3afcde6, 0x87513d56,
|
||||
0xca812b41, 0x18594231, 0x188f3ac1, 0x402055ec, 0x12393f1f, 0x16fbab07, 0xed3ef5b8, 0x8ea86381,
|
||||
0x09f57289, 0x148aeaab, 0xcbb2bf38, 0x6e9f7fec, 0x9a340cd3, 0x6a25d024, 0xc5a2cb41, 0xc4a11710,
|
||||
0x753947a4, 0xe6fa98b4, 0xf8c27cf4, 0xa139c6ae, 0x90e227d9, 0x89e8d957, 0x812f5e52, 0x61f961c5,
|
||||
0x1ddd15c8, 0x49d67d0d, 0xbc003322, 0x835b70c0, 0xd7460575, 0x36acced5, 0x33e2c171, 0x5be91148,
|
||||
0xa165ae99, 0x04c5c2b7, 0x6c1ca659, 0xa295fb04, 0x61c405ca, 0xe893fe91, 0x2b599d31, 0xf1442d25,
|
||||
0x15082ede, 0x6ff350d1, 0xe03d0cd9, 0x82e1eb2c, 0x0ee19b12, 0x6c67ed89, 0xd1c05d0d, 0xb90150b0,
|
||||
0x7285bdb3, 0x77cadc99, 0xc177e6cd, 0xf7a38993, 0x83762f33, 0x86cadda9, 0x7e49da49, 0x305d6f92,
|
||||
0xbc6b870b, 0x78845fdd, 0x6d35f434, 0xa52d76a8, 0xd8de9639, 0x8ab5c51b, 0x80097676, 0xffe70df5,
|
||||
0x3da80417, 0x47ca3c13, 0x28b499c7, 0xf1ab4c28, 0xe5331f02, 0xfba7177b, 0xde7dc472, 0x3625bb80,
|
||||
0x69df7765, 0x988cc4e1, 0xe9fbc9be, 0x40818863, 0x65deac33, 0xd86cf67a, 0xed73ac03, 0xab0d550a,
|
||||
0x1bd2a266, 0x3655c748, 0x9a5ae8fc, 0x1e9eb97a, 0x139536f4, 0x0716c724, 0x8de23fa7, 0xb92ec96b,
|
||||
0x420ebc63, 0x967ccaee, 0x8021afa2, 0x5f7c7688, 0xbd04e6e7, 0x1aeac884, 0x07849209, 0x0c79b100,
|
||||
0xbfb60e7c, 0x8a63de3a, 0x96036a68, 0xb76a745c, 0x43023193, 0xa7d97db7, 0x468a133e, 0xb59ea2ed,
|
||||
0xcb75495b, 0xbe372c6d, 0xb762985a, 0x6215e9eb, 0x07907102, 0x3898ea56, 0x2fb7e218, 0x8c61d4c8,
|
||||
0x4f722e70, 0x694eed25, 0x85b8b9d7, 0xe8421e36, 0x82eaaf9e, 0x9f9caba4, 0xe4a2de74, 0x582c7826,
|
||||
0xaeb637f1, 0x86813271, 0x67b2ff84, 0xd0e2e616, 0x3f9edfe3, 0xc23f8082, 0x0e74ae16, 0x8f256e56,
|
||||
0x6915c6e1, 0xf1407f39, 0x1b4dc5ab, 0xe90a3181, 0x3b1b4237, 0x21c6d08d, 0x34302c14, 0x268d6fc1,
|
||||
0xeabe3b40, 0x3c172d27, 0xf7c5d584, 0x81960cc0, 0x504020ea, 0x9d8b60ae, 0xa2ce002f, 0xba4f47f3,
|
||||
0x6f16479e, 0x1018e5d1, 0xc0ab5040, 0xa2bf90bd, 0xe6bff8f9, 0xb9cd26cb, 0x9bc34bd5, 0x144504b0,
|
||||
0xdbfe2675, 0xb00241d0, 0x88e77fee, 0xae2d9caa, 0xa0043e00, 0x26bbf4ed, 0xdcc0f158, 0xe3320463,
|
||||
0x852b928a, 0x0ec870e9, 0x8f00c1f2, 0xcea95e20, 0xd45277ff, 0x2cad9a9d, 0x02a4a8c1, 0x4a709348,
|
||||
0x35d960bd, 0x62977023, 0x17a7ed8c, 0x07f5f5b5, 0xfd0d4b8f, 0x8d88f33c, 0xf5afb7dd, 0x3a0ea830,
|
||||
0x6ebf28cb, 0x2d589527, 0x77d0810c, 0x11b131e7, 0x54850763, 0x0fa61805, 0x8039574f, 0xa65555d6,
|
||||
0x22b1804c, 0xa9f631b1, 0x49d11eca, 0xd41f0638, 0xe3105eb8, 0xba76cc33, 0x944263f8, 0xc7a8c754,
|
||||
0xf8259880, 0x063f1f00, 0x70d71f92, 0xa8a79da5, 0xc0d3815b, 0x966ac88e, 0x104b3f8b, 0x23b49348,
|
||||
0xc42cc90d, 0xc24791c3, 0x15e11067, 0x2dbb2669, 0x90799c32, 0x881a6a48, 0xf26af51c, 0x8174cbdc,
|
||||
0x0bf0f061, 0x2509dd80, 0x56293b24, 0x8c81078c, 0xbd6a1c97, 0xf4d98c12, 0xccac4283, 0xa68c09e9,
|
||||
0x5f606274, 0x84923735, 0x2c0b8b05, 0xdba69401, 0xcb6555ee, 0xd60daf25, 0x410bd800, 0x9aca380e,
|
||||
0xd8641710, 0x4d0b1601, 0xab2ac09e, 0xa17fc4f7, 0xe4b2bd22, 0x90c4eb3f, 0x19d9ce91, 0x121cc72f,
|
||||
0xfa8510fa, 0xd34d8461, 0x98077550, 0x11628e29, 0x2ed959aa, 0xf1e7cadb, 0xced7e127, 0x6925aaf0,
|
||||
0x2fa509c1, 0xa52ba3c8, 0xaf0b81dd, 0x9f4d58ac, 0x7a55dec3, 0x5e69738a, 0x48d3b5d4, 0x14e6965d,
|
||||
0x76f3c984, 0x41e33a97, 0x29a0da19, 0x29bc38dc, 0xd5fc71d4, 0x05f5ffae, 0xc62da371, 0x96fc4c26,
|
||||
0x8a6dc63b, 0xb48950bb, 0xf1d3c5a8, 0xc6d55752, 0x60683191, 0x8a7cb79b, 0x4fa4ae55, 0xd1d61a8b,
|
||||
0x9cb0b232, 0x51fa1a49, 0xdb73638b, 0xd904c2d5, 0x3a14aac7, 0x620569f4, 0x08483f6b, 0x0db48f35,
|
||||
0x20532f6c, 0x293ced32, 0xb38ebf22, 0x7c89c73d, 0x2d7c11e8, 0x5bb567cd, 0x61b32037, 0xad1cd53d,
|
||||
0x0e18b360, 0x3caf966e, 0xcb59ea4e, 0xe3a28284, 0xa8cb8e8b, 0x05747eb6, 0x75587f24, 0xf2128c81,
|
||||
0x1b34e7b4, 0x8e549407, 0x6ca142f4, 0x1deac038, 0x2a2f303b, 0x0ba4453d, 0xf8d1958f, 0x75e85141,
|
||||
0xd62ed92e, 0x7abaf0d5, 0x87edc08b, 0xc50b0e48, 0x3c49caf8, 0x58689ef9, 0x91878dd8, 0x72a93c45,
|
||||
0x347e087a, 0x4dbf035e, 0xff4102d6, 0x2f9a9185, 0x000fc519, 0x7b071fa0, 0x3e55e34d, 0x54606a4f,
|
||||
0x16482630, 0xce0f07ad, 0x01c43d49, 0x51fe3801, 0x857149e5, 0xd9d64938, 0xcb7da3f5, 0x9952fbe3,
|
||||
0x3729cd74, 0xdc27c37c, 0x9b9f8775, 0xb27514a3, 0xe9ce038c, 0xe295a7a4, 0x189660b3, 0x99907475,
|
||||
0x2082078d, 0x24a94c81, 0x3750e80d, 0xe4b49973, 0xe1372b52, 0x5a79bac1, 0x6f339514, 0x8eaa289e,
|
||||
0x70e91bd9, 0x5a7e9c2d, 0x1211b151, 0x0a32044b, 0xa13fe853, 0x79b9ff01, 0xc3decea2, 0x3c012018,
|
||||
0x6afd6f75, 0x7517d400, 0xd7598668, 0x61bdb52a, 0x413287d8, 0xe8cd2377, 0x3309bdaf, 0x7330afbb,
|
||||
0x9df48ce6, 0xa5cb783a, 0x4b9f8283, 0x311846c9, 0x8b6b29b7, 0x37928233, 0x88ac45da, 0x087f99c0,
|
||||
0xdf41affe, 0x2afe6c63, 0xf2d61a1c, 0x14b1b097, 0xec334f3f, 0x1da30414, 0xae8fe1c9, 0x67acd73f,
|
||||
0xfb118ed8, 0xe4213729, 0x1298038d, 0x2c47acff, 0x7d641646, 0x51efdbfe, 0xf2aada5b, 0xe59c9373,
|
||||
0x236f605c, 0x93994fed, 0x66c47cfd, 0x48061a01, 0x7ed7137d, 0xa01688a7, 0xa5f4a562, 0xef83222b,
|
||||
0xc36ec867, 0xb932238e, 0x92f5bbea, 0x26d91ab2, 0x708ac4dc, 0xdc51bf13, 0xc27f8304, 0x7190df40,
|
||||
0x609d50b3, 0x5718c7ea, 0x7bbf1081, 0xb151c2aa, 0x3b0174fd, 0xc1fd2578, 0xeb440352, 0xa297f85a,
|
||||
0xcbe54778, 0x0343bdf0, 0x3de413b1, 0x37443a5b, 0x603384a7, 0x76456c88, 0x28c4948b, 0xb34a83c5,
|
||||
0x63fe9591, 0xce3ef40f, 0x475a2a6a, 0xf4175d2c, 0x3d831006, 0x556eb1bc, 0xb67370b3, 0x0c689b90,
|
||||
0x91eac424, 0x7a84a8c4, 0x67843a5f, 0x0e83fd51, 0xfad357ab, 0x16af5c67, 0x8c105104, 0xa80042a0,
|
||||
0x4bff0748, 0x77c8f2b5, 0x71ee0e15, 0x8824cd0c, 0xe5439198, 0xe90a6e74, 0x833e5cdc, 0x93145fbb,
|
||||
0x8d3dd6d5, 0x33a154ff, 0x6658b418, 0x0cfb9c00, 0x964f4f5c, 0x0a5dc760, 0xd08496ce, 0x293ac2c8,
|
||||
0x23b70842, 0x60ebfa87, 0x0ea78cab, 0x60f5fdc2, 0xe8f79d22, 0xb97bbb15, 0xd466590c, 0x9f27be15,
|
||||
0x396fecc7, 0xf94e3407, 0xe1930103, 0xa4740508, 0x9f19bcd1, 0xfcebff61, 0xac5c1405, 0xccb734e2,
|
||||
0x634ff632, 0xf967a300, 0xa0cc1c36,
|
||||
};
|
||||
enum { DECODE_CHUNK_BYTES = 512 };
|
||||
static const uint16_t OFFSETS[] = {
|
||||
0, 227, 448, 665, 896, 1116, 1323, 1537, 1779, 1926, 2134, 2376,
|
||||
@@ -615,19 +706,11 @@ inline const char* ota_target_env_name(uint32_t target_id, char* buffer, size_t
|
||||
0xf4, 0xd8, 0x18, 0x73, 0x3c, 0x9a, 0x72, 0x5c, 0xef, 0x3b, 0xae, 0x3b, 0x92, 0xcc, 0xe4, 0x5e,
|
||||
0x8a, 0xc1, 0xbf, 0xe7, 0xa0, 0x05,
|
||||
};
|
||||
#endif
|
||||
for (unsigned i = 0; i < sizeof(IDS) / sizeof(IDS[0]); i++) {
|
||||
if (IDS[i] != target_id) continue;
|
||||
unsigned group = 0;
|
||||
while (i >= FIRSTS[group + 1]) group++;
|
||||
#if defined(OTA_RAK15002_SD_STORE) && defined(COMPANION_RADIO_FULL)
|
||||
// Rare display lookup: larger chunks save flash without growing the loop stack.
|
||||
uint8_t* raw = (uint8_t*)malloc(DECODE_CHUNK_BYTES);
|
||||
if (!raw) return nullptr;
|
||||
struct Scratch { uint8_t* bytes; ~Scratch() { free(bytes); } } scratch = {raw};
|
||||
#else
|
||||
uint8_t raw[DECODE_CHUNK_BYTES];
|
||||
#endif
|
||||
unsigned int produced = DECODE_CHUNK_BYTES;
|
||||
if (tinf_uncompress_exact(raw, &produced, NAMES + OFFSETS[group],
|
||||
OFFSETS[group + 1] - OFFSETS[group]) != TINF_OK ||
|
||||
@@ -652,6 +735,7 @@ inline const char* ota_target_env_name(uint32_t target_id, char* buffer, size_t
|
||||
return buffer;
|
||||
}
|
||||
return nullptr;
|
||||
#endif
|
||||
}
|
||||
#else
|
||||
// target_id -> env name, or nullptr if unknown. Linear scan; lookups are rare.
|
||||
|
||||
+74
-11
@@ -1,30 +1,93 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "Utils.h"
|
||||
#include "helpers/ota/tinf/tinf.h"
|
||||
|
||||
static unsigned hash_calls;
|
||||
static unsigned decoded_count;
|
||||
static const void* decoded_sources[64];
|
||||
static unsigned allocation_calls;
|
||||
static void* active_allocation;
|
||||
|
||||
void target_name_sha256(uint8_t* hash, size_t size, const uint8_t* name, int length) {
|
||||
++hash_calls;
|
||||
mesh::Utils::sha256(hash, size, name, length);
|
||||
}
|
||||
|
||||
int target_name_uncompress_exact(void* dest, unsigned* size, const void* source, unsigned length) {
|
||||
assert(decoded_count < sizeof(decoded_sources) / sizeof(decoded_sources[0]));
|
||||
for (unsigned i = 0; i < decoded_count; ++i) assert(decoded_sources[i] != source);
|
||||
decoded_sources[decoded_count++] = source;
|
||||
return tinf_uncompress_exact(dest, size, source, length);
|
||||
}
|
||||
|
||||
void* target_name_malloc(size_t bytes) {
|
||||
assert(bytes == 4096 && !active_allocation && ++allocation_calls == 1);
|
||||
#if OTA_TEST_MALLOC_FAIL
|
||||
return nullptr;
|
||||
#else
|
||||
active_allocation = malloc(bytes);
|
||||
return active_allocation;
|
||||
#endif
|
||||
}
|
||||
|
||||
void target_name_free(void* bytes) {
|
||||
if (bytes) {
|
||||
assert(bytes == active_allocation);
|
||||
active_allocation = nullptr;
|
||||
}
|
||||
free(bytes);
|
||||
}
|
||||
|
||||
#include "helpers/ota/OtaTargets.h"
|
||||
|
||||
using mesh::ota::ota_target_env_name;
|
||||
|
||||
#if OTA_TARGET_NAME_TABLE && OTA_TARGET_NAME_FRONT_CODED
|
||||
static const char* lookup(uint32_t id, char* buffer, size_t capacity, unsigned candidates) {
|
||||
assert(!active_allocation);
|
||||
hash_calls = decoded_count = allocation_calls = 0;
|
||||
const char* result = ota_target_env_name(id, buffer, capacity);
|
||||
assert(!active_allocation);
|
||||
#if defined(OTA_RAK15002_SD_STORE) && defined(COMPANION_RADIO_FULL)
|
||||
assert(hash_calls <= candidates && decoded_count <= 3);
|
||||
if (result) assert(hash_calls > 0 && allocation_calls == 1);
|
||||
#else
|
||||
(void)candidates;
|
||||
assert(hash_calls == 0 && decoded_count <= 1 && allocation_calls == 0);
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
int main() {
|
||||
unsigned id;
|
||||
while (scanf("%x", &id) == 1) {
|
||||
unsigned id, candidates;
|
||||
while (scanf("%x %u", &id, &candidates) == 2) {
|
||||
#if OTA_TARGET_NAME_TABLE && OTA_TARGET_NAME_FRONT_CODED
|
||||
char buffer[mesh::ota::OTA_TARGET_ENV_NAME_CAPACITY];
|
||||
const char* name = ota_target_env_name(id, buffer, sizeof buffer);
|
||||
memset(buffer, 'X', sizeof buffer);
|
||||
const char* name = lookup(id, buffer, sizeof buffer, candidates);
|
||||
if (!name) assert(buffer[0] == 0);
|
||||
if (name) {
|
||||
const size_t length = strlen(name);
|
||||
char exact[mesh::ota::OTA_TARGET_ENV_NAME_CAPACITY];
|
||||
assert(ota_target_env_name(id, exact, length + 1) == exact);
|
||||
assert(lookup(id, exact, length + 1, candidates) == exact);
|
||||
assert(strcmp(name, exact) == 0);
|
||||
// A failed or unrelated lookup must not clobber another caller's result.
|
||||
char short_buffer[mesh::ota::OTA_TARGET_ENV_NAME_CAPACITY];
|
||||
memset(short_buffer, 'X', sizeof short_buffer);
|
||||
assert(ota_target_env_name(id, short_buffer, length) == nullptr);
|
||||
assert(short_buffer[0] == 0);
|
||||
assert(short_buffer[length] == 'X');
|
||||
assert(ota_target_env_name(id, nullptr, sizeof buffer) == nullptr);
|
||||
assert(ota_target_env_name(id, short_buffer, 0) == nullptr);
|
||||
assert(ota_target_env_name(0, short_buffer, sizeof short_buffer) == nullptr);
|
||||
const size_t short_capacities[] = {1, length / 2, length};
|
||||
for (size_t capacity : short_capacities) {
|
||||
if (capacity == 0) continue;
|
||||
memset(short_buffer, 'X', sizeof short_buffer);
|
||||
assert(lookup(id, short_buffer, capacity, candidates) == nullptr);
|
||||
assert(short_buffer[0] == 0);
|
||||
assert(short_buffer[capacity] == 'X');
|
||||
}
|
||||
assert(lookup(id, nullptr, sizeof buffer, candidates) == nullptr);
|
||||
assert(lookup(id, short_buffer, 0, candidates) == nullptr);
|
||||
assert(lookup(0, short_buffer, sizeof short_buffer, OTA_TEST_TARGET_COUNT) == nullptr);
|
||||
assert(short_buffer[0] == 0);
|
||||
assert(strcmp(name, exact) == 0);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "RuntimeMocks.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "RuntimeMocks.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "RuntimeMocks.h"
|
||||
@@ -0,0 +1,152 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
|
||||
class Print {
|
||||
public:
|
||||
virtual ~Print() = default;
|
||||
virtual size_t write(uint8_t) = 0;
|
||||
virtual size_t write(const uint8_t* bytes, size_t count) {
|
||||
size_t used = 0;
|
||||
while (used < count && write(bytes[used])) ++used;
|
||||
return used;
|
||||
}
|
||||
};
|
||||
class Stream : public Print {
|
||||
public:
|
||||
virtual int available() = 0;
|
||||
virtual int read() = 0;
|
||||
virtual int peek() = 0;
|
||||
virtual void flush() = 0;
|
||||
virtual int availableForWrite() { return 0; }
|
||||
};
|
||||
|
||||
constexpr int INPUT = 0, OUTPUT = 1, INPUT_PULLUP = 2, LOW = 0, HIGH = 1;
|
||||
constexpr int NRF_SPIM1 = 1, NRF_SPIM2 = 2, NRF_SPIM3 = 3;
|
||||
extern std::atomic<uint32_t> mock_millis;
|
||||
inline uint32_t millis() { return mock_millis.load(); }
|
||||
void delay(uint32_t);
|
||||
void delayMicroseconds(uint32_t);
|
||||
void pinMode(int, int);
|
||||
void digitalWrite(int, int);
|
||||
int digitalRead(int);
|
||||
struct MockFicr { uint32_t DEVICEID[2] = {0x123456, 0}; };
|
||||
extern MockFicr mock_ficr;
|
||||
#define NRF_FICR (&mock_ficr)
|
||||
|
||||
struct MockTask;
|
||||
using TaskHandle_t = MockTask*;
|
||||
using BaseType_t = int;
|
||||
struct StaticTask_t { uint8_t bytes[192]; };
|
||||
constexpr int pdPASS = 1, pdTRUE = 1, pdFALSE = 0;
|
||||
constexpr uint32_t portMAX_DELAY = UINT32_MAX;
|
||||
inline uint32_t pdMS_TO_TICKS(uint32_t value) { return value; }
|
||||
extern std::recursive_mutex mock_scheduler_mutex;
|
||||
#define taskENTER_CRITICAL() mock_scheduler_mutex.lock()
|
||||
#define taskEXIT_CRITICAL() mock_scheduler_mutex.unlock()
|
||||
BaseType_t xTaskCreate(void (*)(void*), const char*, unsigned, void*, int, TaskHandle_t*);
|
||||
void xTaskNotifyGive(TaskHandle_t);
|
||||
uint32_t ulTaskNotifyTake(int, uint32_t);
|
||||
void vTaskDelay(uint32_t);
|
||||
void vTaskDelete(TaskHandle_t);
|
||||
|
||||
struct SPISettings {};
|
||||
#define SPI_ETHERNET_SETTINGS SPISettings{}
|
||||
class SPIClass {
|
||||
public:
|
||||
SPIClass(int, int, int, int) {}
|
||||
void begin();
|
||||
void end();
|
||||
void beginTransaction(const SPISettings&);
|
||||
void endTransaction();
|
||||
};
|
||||
extern SPIClass SPI, SPI1;
|
||||
|
||||
class IPAddress {
|
||||
uint32_t value_;
|
||||
public:
|
||||
explicit IPAddress(uint32_t value = 0x2a01a8c0) : value_(value) {}
|
||||
IPAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
|
||||
: value_(uint32_t(a) | (uint32_t(b) << 8) | (uint32_t(c) << 16) | (uint32_t(d) << 24)) {}
|
||||
uint8_t operator[](size_t offset) const { return (value_ >> (8 * offset)) & 255; }
|
||||
explicit operator uint32_t() const { return value_; }
|
||||
};
|
||||
class EthernetClient {
|
||||
uint8_t socket_ = 255;
|
||||
public:
|
||||
EthernetClient() = default;
|
||||
explicit EthernetClient(uint8_t value) : socket_(value) {}
|
||||
explicit operator bool() const { return socket_ < 4; }
|
||||
uint8_t getSocketNumber() const { return socket_; }
|
||||
bool connected() const;
|
||||
IPAddress remoteIP() const;
|
||||
uint16_t remotePort() const { return 12345; }
|
||||
void stop() { assert(false && "blocking vendor stop must not run"); }
|
||||
int available() { assert(false && "vendor receive must not run on main"); return 0; }
|
||||
int read() { assert(false && "vendor receive must not run on main"); return -1; }
|
||||
size_t write(const uint8_t*, size_t) {
|
||||
assert(false && "blocking vendor write must not run"); return 0;
|
||||
}
|
||||
};
|
||||
class EthernetServer {
|
||||
public:
|
||||
explicit EthernetServer(int port) { assert(port == 5000); }
|
||||
void begin();
|
||||
EthernetClient accept();
|
||||
};
|
||||
enum { EthernetNoHardware = 0, EthernetPresent = 1, LinkOFF = 0, LinkON = 1 };
|
||||
struct MockEthernet {
|
||||
std::atomic<int> begin_count{0}, maintain_count{0}, accept_count{0}, listen_count{0};
|
||||
std::atomic<int> begin_result{1}, maintain_result{0}, hardware{EthernetPresent};
|
||||
std::atomic<bool> begin_blocked{false}, maintain_blocked{false}, accept_blocked{false};
|
||||
void init(SPIClass&, int);
|
||||
int begin(uint8_t*, unsigned long = 60000, unsigned long = 4000);
|
||||
void begin(uint8_t*, IPAddress, IPAddress, IPAddress, IPAddress);
|
||||
int maintain();
|
||||
int hardwareStatus() const;
|
||||
int linkStatus() const { return LinkON; }
|
||||
IPAddress localIP() const;
|
||||
};
|
||||
extern MockEthernet Ethernet;
|
||||
|
||||
enum SockCMD { Sock_OPEN = 1, Sock_LISTEN = 2, Sock_CLOSE = 0x10, Sock_SEND = 0x20, Sock_RECV = 0x40 };
|
||||
struct SnMR { enum { TCP = 1 }; };
|
||||
struct SnSR { enum { CLOSED = 0, INIT = 0x13, LISTEN = 0x14, SYNSENT = 0x15,
|
||||
SYNRECV = 0x16, ESTABLISHED = 0x17, FIN_WAIT = 0x18, CLOSING = 0x1a,
|
||||
TIME_WAIT = 0x1b, CLOSE_WAIT = 0x1c, LAST_ACK = 0x1d, UDP = 0x22 }; };
|
||||
struct SnIR { enum { SEND_OK = 0x10, TIMEOUT = 8 }; };
|
||||
class MockW5100 {
|
||||
public:
|
||||
static constexpr uint16_t SSIZE = 2048, SMASK = SSIZE - 1;
|
||||
std::array<std::array<uint8_t, SSIZE>, 4> memory{};
|
||||
uint16_t tx_wr[4] = {}, committed[4] = {}, rx_rd[4] = {};
|
||||
uint8_t interrupt[4] = {}, command[4] = {};
|
||||
uint8_t getChip() const { return 51; }
|
||||
SPIClass* getSPI() const;
|
||||
uint16_t SBASE(uint8_t socket) const { return 0x4000 + socket * SSIZE; }
|
||||
uint16_t RBASE(uint8_t socket) const { return 0x6000 + socket * SSIZE; }
|
||||
uint8_t readSnSR(uint8_t) const;
|
||||
void readSnDIPR(uint8_t, uint8_t*);
|
||||
void writeSnMR(uint8_t, uint8_t);
|
||||
void writeSnPORT(uint8_t, uint16_t);
|
||||
uint8_t readSnIR(uint8_t);
|
||||
uint8_t readSnCR(uint8_t);
|
||||
uint16_t readSnTX_FSR(uint8_t) const;
|
||||
uint16_t readSnTX_WR(uint8_t) const;
|
||||
uint16_t readSnRX_RSR(uint8_t) const;
|
||||
uint16_t readSnRX_RD(uint8_t) const;
|
||||
void writeSnRX_RD(uint8_t, uint16_t);
|
||||
void writeSnTX_WR(uint8_t, uint16_t);
|
||||
void writeSnIR(uint8_t, uint8_t);
|
||||
void writeSnCR(uint8_t, uint8_t);
|
||||
uint16_t read(uint16_t, uint8_t*, uint16_t);
|
||||
uint16_t write(uint16_t, const uint8_t*, uint16_t);
|
||||
static void execCmdSn(uint8_t, SockCMD); // Intentionally undefined.
|
||||
};
|
||||
extern MockW5100 W5100;
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "RuntimeMocks.h"
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
namespace mesh {
|
||||
inline bool isUsbDebugLoggingEnabled() { return false; }
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "RuntimeMocks.h"
|
||||
@@ -0,0 +1,676 @@
|
||||
// Complete production Companion driver + framing on a deterministic fake RTOS.
|
||||
// Vendor waits block only their worker; the main-thread checks keep running.
|
||||
#include <Arduino.h>
|
||||
#include <helpers/ethernet/RAK13800/RAK13800EthernetInterface.h>
|
||||
#include <helpers/MultiSerialInterface.h>
|
||||
#include <helpers/nrf52/TerminalPeripheralShutdown.h>
|
||||
#include <helpers/nrf52/Rak4631SharedSpi.h>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using Bytes = std::vector<uint8_t>;
|
||||
static const auto main_thread = std::this_thread::get_id();
|
||||
std::atomic<uint32_t> mock_millis{100};
|
||||
MockFicr mock_ficr;
|
||||
std::recursive_mutex mock_scheduler_mutex;
|
||||
MockEthernet Ethernet;
|
||||
MockW5100 W5100;
|
||||
SPIClass SPI(NRF_SPIM3, 45, 43, 44), SPI1(NRF_SPIM2, 9, 10, 11);
|
||||
static SPIClass* ethernet_spi = nullptr;
|
||||
static std::atomic<bool> spi_active{false}, card_fitted{false}, defer_start{false};
|
||||
static std::atomic<int> transactions{0}, hardware_ops{0}, spi_begins{0}, spi_ends{0};
|
||||
static std::atomic<int> main_hardware_ops{0};
|
||||
static std::atomic<int> live_tasks{0}, task_deletions{0};
|
||||
static std::atomic<int> parked_tasks{0};
|
||||
static std::atomic<int> pad_releases{0};
|
||||
static std::atomic<bool> fail_create{false}, ack_send{true}, stuck_command{false};
|
||||
static std::atomic<uint8_t> open_status{SnSR::INIT}, listen_status{SnSR::LISTEN}, close_status{SnSR::CLOSED};
|
||||
static std::array<std::atomic<int>, 64> pin_modes{}, pin_values{};
|
||||
static std::mutex library_mutex;
|
||||
static std::condition_variable library_changed;
|
||||
struct Socket {
|
||||
std::mutex mutex;
|
||||
std::atomic<bool> connected{false};
|
||||
std::atomic<int> capacity{2048}, closes{0};
|
||||
std::atomic<uint32_t> ip{0x1401a8c0};
|
||||
std::atomic<uint8_t> status{SnSR::CLOSED};
|
||||
Bytes input, output;
|
||||
};
|
||||
static Socket sockets[4];
|
||||
static std::deque<uint8_t> accepted;
|
||||
struct TaskDeleted {};
|
||||
struct MockTask {
|
||||
std::thread thread;
|
||||
std::mutex mutex;
|
||||
std::condition_variable changed;
|
||||
unsigned notifications = 0;
|
||||
bool deleted = false;
|
||||
};
|
||||
static thread_local MockTask* current_task = nullptr;
|
||||
static std::vector<std::unique_ptr<MockTask>> tasks;
|
||||
|
||||
static bool reserved() {
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
return mesh::ota::rak4631_ethernet_owns_spi();
|
||||
#else
|
||||
return true; // Dedicated Ethernet legacy profiles have no shared-bus arbiter.
|
||||
#endif
|
||||
}
|
||||
static void on_main() { assert(std::this_thread::get_id() == main_thread); }
|
||||
static void worker_only() {
|
||||
assert(current_task != nullptr && "blocking vendor operation entered the mesh thread");
|
||||
assert(std::this_thread::get_id() != main_thread);
|
||||
assert(reserved() && spi_active.load());
|
||||
++hardware_ops;
|
||||
}
|
||||
static void transfer() {
|
||||
assert(spi_active.load() && reserved());
|
||||
assert(transactions.load() == 1 && "register IO requires an exclusive SPI transaction");
|
||||
++hardware_ops;
|
||||
if (!current_task) ++main_hardware_ops;
|
||||
}
|
||||
static void wait_until(const std::function<bool()>& predicate,
|
||||
const std::function<void()>& pump = {}) {
|
||||
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(4);
|
||||
while (!predicate()) {
|
||||
assert(std::chrono::steady_clock::now() < deadline && "worker did not reach expected state");
|
||||
if (pump) pump();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
}
|
||||
static void unblock(std::atomic<bool>& blocked) {
|
||||
std::lock_guard<std::mutex> lock(library_mutex);
|
||||
blocked.store(false);
|
||||
library_changed.notify_all();
|
||||
}
|
||||
void pinMode(int pin, int mode) {
|
||||
assert(pin >= 0 && pin < 64);
|
||||
if (pin == 26 && mode == OUTPUT) {
|
||||
assert(reserved());
|
||||
assert(pin_values[26].load() == HIGH && "CS must be preloaded before driving it");
|
||||
}
|
||||
if (mode == INPUT && pin != 10) { assert(!spi_active.load()); ++pad_releases; }
|
||||
pin_modes[pin].store(mode);
|
||||
}
|
||||
void digitalWrite(int pin, int value) { pin_values[pin].store(value); }
|
||||
int digitalRead(int pin) { assert(pin == 10); return card_fitted.load() ? LOW : HIGH; }
|
||||
void delay(uint32_t ticks) { vTaskDelay(ticks); }
|
||||
void delayMicroseconds(uint32_t us) { assert(current_task && us == 250); }
|
||||
void SPIClass::begin() {
|
||||
assert(current_task != nullptr && "SPI startup entered the mesh thread");
|
||||
assert(reserved() && !spi_active.exchange(true));
|
||||
assert(pin_modes[26].load() == OUTPUT && pin_values[26].load() == HIGH);
|
||||
++spi_begins;
|
||||
}
|
||||
void SPIClass::end() {
|
||||
assert(reserved() && spi_active.exchange(false));
|
||||
assert(transactions.load() == 0);
|
||||
++spi_ends;
|
||||
}
|
||||
void SPIClass::beginTransaction(const SPISettings&) {
|
||||
assert(spi_active.load() && transactions.fetch_add(1) == 0);
|
||||
}
|
||||
void SPIClass::endTransaction() { assert(transactions.fetch_sub(1) == 1); }
|
||||
BaseType_t xTaskCreate(void (*fn)(void*), const char*, unsigned words,
|
||||
void* argument, int priority, TaskHandle_t* handle) {
|
||||
on_main(); assert(words == 1024 && priority == 1);
|
||||
if (fail_create.load()) return 0;
|
||||
auto task = std::make_unique<MockTask>();
|
||||
MockTask* raw = task.get(); *handle = raw; ++live_tasks;
|
||||
raw->thread = std::thread([raw, fn, argument] {
|
||||
current_task = raw;
|
||||
try {
|
||||
{ std::lock_guard<std::recursive_mutex> lock(mock_scheduler_mutex); }
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(library_mutex);
|
||||
library_changed.wait(lock, [] { return !defer_start.load(); });
|
||||
}
|
||||
fn(argument);
|
||||
assert(false && "worker must park for synchronous main-thread reclamation");
|
||||
} catch (const TaskDeleted&) {}
|
||||
});
|
||||
tasks.push_back(std::move(task));
|
||||
return pdPASS;
|
||||
}
|
||||
void xTaskNotifyGive(TaskHandle_t task) {
|
||||
assert(task);
|
||||
std::lock_guard<std::mutex> lock(task->mutex);
|
||||
assert(!task->deleted && "notification used a deleted worker handle");
|
||||
++task->notifications; task->changed.notify_all();
|
||||
}
|
||||
uint32_t ulTaskNotifyTake(int clear, uint32_t ticks) {
|
||||
assert(current_task && clear == pdTRUE);
|
||||
std::unique_lock<std::mutex> lock(current_task->mutex);
|
||||
const auto ready = [] { return current_task->notifications || current_task->deleted; };
|
||||
if (ticks == portMAX_DELAY) current_task->changed.wait(lock, ready);
|
||||
else current_task->changed.wait_for(lock, std::chrono::milliseconds(ticks), ready);
|
||||
if (current_task->deleted) throw TaskDeleted{};
|
||||
const auto count = current_task->notifications; current_task->notifications = 0;
|
||||
return count;
|
||||
}
|
||||
void vTaskDelay(uint32_t ticks) {
|
||||
if (!current_task) {
|
||||
mock_millis.fetch_add(ticks);
|
||||
std::this_thread::yield();
|
||||
return;
|
||||
}
|
||||
if (ticks == portMAX_DELAY) {
|
||||
++parked_tasks;
|
||||
std::unique_lock<std::mutex> lock(current_task->mutex);
|
||||
current_task->changed.wait(lock, [] { return current_task->deleted; });
|
||||
--parked_tasks;
|
||||
throw TaskDeleted{};
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(ticks));
|
||||
}
|
||||
void vTaskDelete(TaskHandle_t task) {
|
||||
on_main(); assert(task && !spi_active.load());
|
||||
assert(reserved());
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(task->mutex);
|
||||
assert(!task->deleted); task->deleted = true; task->changed.notify_all();
|
||||
}
|
||||
task->thread.join(); --live_tasks; ++task_deletions;
|
||||
}
|
||||
void MockEthernet::init(SPIClass& bus, int cs) { worker_only(); assert(cs == 26); ethernet_spi = &bus; }
|
||||
int MockEthernet::begin(uint8_t* mac, unsigned long timeout, unsigned long response) {
|
||||
worker_only(); assert(mac[0] == 2 && mac[3] == 0x12 && mac[4] == 0x34 && mac[5] == 0x56);
|
||||
assert(timeout == 10000 && response == 2000);
|
||||
++begin_count;
|
||||
std::unique_lock<std::mutex> lock(library_mutex);
|
||||
library_changed.wait(lock, [this] { return !begin_blocked.load(); });
|
||||
return begin_result.load();
|
||||
}
|
||||
int MockEthernet::maintain() {
|
||||
worker_only(); ++maintain_count;
|
||||
std::unique_lock<std::mutex> lock(library_mutex);
|
||||
library_changed.wait(lock, [this] { return !maintain_blocked.load(); });
|
||||
return maintain_result.load();
|
||||
}
|
||||
void MockEthernet::begin(uint8_t* mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) {
|
||||
worker_only(); assert(mac[0] == 2 && mac[5] == 0x56);
|
||||
assert(ip[0] == 192 && ip[3] == 42 && dns[0] == 8 && dns[3] == 8);
|
||||
assert(gateway[0] == 192 && gateway[3] == 1 && subnet[0] == 255 && subnet[3] == 0);
|
||||
++begin_count;
|
||||
std::unique_lock<std::mutex> lock(library_mutex);
|
||||
library_changed.wait(lock, [this] { return !begin_blocked.load(); });
|
||||
}
|
||||
int MockEthernet::hardwareStatus() const { worker_only(); return hardware.load(); }
|
||||
IPAddress MockEthernet::localIP() const { worker_only(); return IPAddress{}; }
|
||||
bool EthernetClient::connected() const {
|
||||
worker_only(); return socket_ < 4 && sockets[socket_].connected.load();
|
||||
}
|
||||
IPAddress EthernetClient::remoteIP() const {
|
||||
worker_only(); assert(socket_ < 4); return IPAddress(sockets[socket_].ip.load());
|
||||
}
|
||||
void EthernetServer::begin() { worker_only(); ++Ethernet.listen_count; }
|
||||
EthernetClient EthernetServer::accept() {
|
||||
worker_only(); ++Ethernet.accept_count;
|
||||
std::unique_lock<std::mutex> lock(library_mutex);
|
||||
library_changed.wait(lock, [] { return !Ethernet.accept_blocked.load(); });
|
||||
if (accepted.empty()) return {};
|
||||
const auto next = accepted.front(); accepted.pop_front();
|
||||
return EthernetClient(next);
|
||||
}
|
||||
SPIClass* MockW5100::getSPI() const { return ethernet_spi; }
|
||||
uint8_t MockW5100::readSnSR(uint8_t s) const {
|
||||
transfer();
|
||||
if (sockets[s].connected) { sockets[s].status.store(SnSR::ESTABLISHED); return SnSR::ESTABLISHED; }
|
||||
const uint8_t status = sockets[s].status.load();
|
||||
return status == SnSR::ESTABLISHED ? uint8_t(SnSR::CLOSED) : status;
|
||||
}
|
||||
void MockW5100::readSnDIPR(uint8_t s, uint8_t* ip) {
|
||||
worker_only(); transfer(); const uint32_t value = sockets[s].ip.load();
|
||||
for (unsigned i = 0; i < 4; ++i) ip[i] = value >> (i * 8);
|
||||
}
|
||||
void MockW5100::writeSnMR(uint8_t, uint8_t protocol) { worker_only(); transfer(); assert(protocol == SnMR::TCP); }
|
||||
void MockW5100::writeSnPORT(uint8_t, uint16_t port) { worker_only(); transfer(); assert(port == 5000); }
|
||||
uint8_t MockW5100::readSnIR(uint8_t s) {
|
||||
transfer(); if (ack_send.load() && committed[s] == tx_wr[s]) interrupt[s] |= SnIR::SEND_OK;
|
||||
return interrupt[s];
|
||||
}
|
||||
uint8_t MockW5100::readSnCR(uint8_t s) {
|
||||
transfer();
|
||||
if (!stuck_command.load() && (!(command[s] == Sock_OPEN || command[s] == Sock_LISTEN)
|
||||
|| !Ethernet.accept_blocked.load())) {
|
||||
if (command[s] == Sock_OPEN) sockets[s].status.store(open_status.load());
|
||||
if (command[s] == Sock_LISTEN) sockets[s].status.store(listen_status.load());
|
||||
command[s] = 0;
|
||||
}
|
||||
return command[s];
|
||||
}
|
||||
uint16_t MockW5100::readSnTX_FSR(uint8_t s) const { transfer(); return sockets[s].capacity.load(); }
|
||||
uint16_t MockW5100::readSnTX_WR(uint8_t s) const { transfer(); return tx_wr[s]; }
|
||||
uint16_t MockW5100::readSnRX_RSR(uint8_t s) const {
|
||||
transfer(); std::lock_guard<std::mutex> lock(sockets[s].mutex); return sockets[s].input.size();
|
||||
}
|
||||
uint16_t MockW5100::readSnRX_RD(uint8_t s) const { transfer(); return rx_rd[s]; }
|
||||
void MockW5100::writeSnRX_RD(uint8_t s, uint16_t n) { transfer(); rx_rd[s] = n; }
|
||||
void MockW5100::writeSnTX_WR(uint8_t s, uint16_t n) { transfer(); tx_wr[s] = n; }
|
||||
void MockW5100::writeSnIR(uint8_t s, uint8_t n) { transfer(); interrupt[s] &= ~n; }
|
||||
void MockW5100::writeSnCR(uint8_t s, uint8_t n) {
|
||||
transfer(); assert(command[s] == 0 && "a pending controller command was overwritten");
|
||||
if (n == Sock_CLOSE) {
|
||||
++sockets[s].closes; sockets[s].connected.store(false); sockets[s].status.store(close_status.load());
|
||||
} else if (n == Sock_OPEN || n == Sock_LISTEN) {
|
||||
worker_only();
|
||||
if (n == Sock_OPEN) ++Ethernet.accept_count;
|
||||
else ++Ethernet.listen_count;
|
||||
command[s] = n;
|
||||
return;
|
||||
} else if (n == Sock_RECV) {
|
||||
std::lock_guard<std::mutex> lock(sockets[s].mutex);
|
||||
assert(!sockets[s].input.empty()); sockets[s].input.erase(sockets[s].input.begin());
|
||||
} else {
|
||||
assert(n == Sock_SEND);
|
||||
std::lock_guard<std::mutex> lock(sockets[s].mutex);
|
||||
for (uint16_t p = committed[s]; p != tx_wr[s]; ++p) sockets[s].output.push_back(memory[s][p & SMASK]);
|
||||
committed[s] = tx_wr[s]; interrupt[s] = ack_send.load() ? SnIR::SEND_OK : 0;
|
||||
}
|
||||
command[s] = stuck_command.load() ? n : 0;
|
||||
}
|
||||
uint16_t MockW5100::write(uint16_t address, const uint8_t* bytes, uint16_t count) {
|
||||
transfer(); const auto relative = address - 0x4000;
|
||||
const auto s = relative / SSIZE, offset = relative % SSIZE;
|
||||
assert(s < 4 && offset + count <= SSIZE); memcpy(memory[s].data() + offset, bytes, count); return count;
|
||||
}
|
||||
uint16_t MockW5100::read(uint16_t address, uint8_t* bytes, uint16_t count) {
|
||||
transfer(); const auto s = (address - 0x6000) / SSIZE;
|
||||
std::lock_guard<std::mutex> lock(sockets[s].mutex);
|
||||
assert(s < 4 && count == 1 && !sockets[s].input.empty()); bytes[0] = sockets[s].input.front(); return count;
|
||||
}
|
||||
|
||||
static constexpr uint32_t A = 0x1401a8c0, B = 0x1e01a8c0;
|
||||
static Bytes request(const Bytes& bytes) {
|
||||
Bytes result{'<', uint8_t(bytes.size()), uint8_t(bytes.size() >> 8)};
|
||||
result.insert(result.end(), bytes.begin(), bytes.end()); return result;
|
||||
}
|
||||
static Bytes response(const Bytes& bytes) {
|
||||
auto result = request(bytes); result[0] = '>'; return result;
|
||||
}
|
||||
static void feed(uint8_t s, const Bytes& bytes) {
|
||||
std::lock_guard<std::mutex> lock(sockets[s].mutex);
|
||||
sockets[s].input.insert(sockets[s].input.end(), bytes.begin(), bytes.end());
|
||||
}
|
||||
static Bytes output(uint8_t s) { std::lock_guard<std::mutex> lock(sockets[s].mutex); return sockets[s].output; }
|
||||
static bool other_owner(void*, uint32_t) { return true; }
|
||||
struct Fixture {
|
||||
RAK13800EthernetInterface owner;
|
||||
MultiSerialInterface routes;
|
||||
uint8_t received[MAX_FRAME_SIZE] = {};
|
||||
bool prepare_allowed = true, off_allowed = true, release_allowed = true, pending = false;
|
||||
unsigned prepares = 0, releases = 0, callbacks = 0, polls = 0;
|
||||
explicit Fixture(bool hooks = true) {
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
if (hooks) {
|
||||
owner.setSpiOwnershipHooks([](void* p) {
|
||||
on_main(); auto& f = *static_cast<Fixture*>(p); ++f.prepares;
|
||||
return f.prepare_allowed;
|
||||
}, [](void* p) { on_main(); return static_cast<Fixture*>(p)->off_allowed; }, [](void* p) {
|
||||
on_main(); auto& f = *static_cast<Fixture*>(p); ++f.releases;
|
||||
assert(!spi_active.load() && !live_tasks.load()); return f.release_allowed;
|
||||
}, this);
|
||||
}
|
||||
#else
|
||||
(void)hooks;
|
||||
#endif
|
||||
owner.setSessionChangedCallback([](void* p) {
|
||||
on_main(); auto& f = *static_cast<Fixture*>(p);
|
||||
assert(!f.owner.isConnected()); ++f.callbacks; f.pending = false;
|
||||
f.routes.forgetReplyRouteForDisconnected(&f.owner);
|
||||
}, this);
|
||||
assert(owner.begin()); assert(routes.addInterface(InterfaceType::Ethernet, &owner)); routes.enable();
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
assert(!owner.hardwareActive() && !owner.isConnected() && !spi_active && !live_tasks);
|
||||
#else
|
||||
assert(owner.hardwareActive() && live_tasks == 1); // Legacy begins automatically.
|
||||
#endif
|
||||
}
|
||||
size_t tick(uint32_t elapsed = 1) {
|
||||
mock_millis.fetch_add(elapsed); ++polls; owner.loop(); return routes.checkRecvFrame(received);
|
||||
}
|
||||
void pump(unsigned count) { while (count--) tick(); }
|
||||
void online() {
|
||||
assert(owner.tryEnable());
|
||||
wait_until([&] { return std::strcmp(owner.status(), "online") == 0; }, [&] { tick(); });
|
||||
}
|
||||
void connect(uint8_t s = 0, uint32_t ip = A) {
|
||||
wait_until([&] { return sockets[s].status == SnSR::LISTEN; }, [&] { tick(); });
|
||||
sockets[s].connected.store(true); sockets[s].ip.store(ip);
|
||||
{ std::lock_guard<std::mutex> lock(library_mutex); accepted.push_back(s); }
|
||||
wait_until([&] { return owner.isConnected(); }, [&] { tick(); });
|
||||
}
|
||||
void replace(uint8_t s, uint32_t ip) {
|
||||
wait_until([&] { return sockets[s].status == SnSR::LISTEN; }, [&] { tick(); });
|
||||
const auto before = callbacks;
|
||||
sockets[s].connected.store(true); sockets[s].ip.store(ip);
|
||||
{ std::lock_guard<std::mutex> lock(library_mutex); accepted.push_back(s); }
|
||||
wait_until([&] { return ip == A ? !output(s).empty() : callbacks > before; }, [&] { tick(); });
|
||||
pump(10);
|
||||
}
|
||||
void off() {
|
||||
assert(owner.tryDisable());
|
||||
wait_until([&] {
|
||||
bool released = live_tasks.load() == 0 && !spi_active.load();
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
released = released && !mesh::ota::rak4631_ethernet_owns_spi();
|
||||
#endif
|
||||
return released;
|
||||
}, [&] { tick(); });
|
||||
assert(pin_modes[26].load() == INPUT);
|
||||
}
|
||||
void drain(uint8_t s, const Bytes& expected) {
|
||||
wait_until([&] { return output(s) == expected; }, [&] { tick(); });
|
||||
pump(5);
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
assert(argc == 2); const auto scenario = std::atoi(argv[1]);
|
||||
if (scenario == 22 || scenario == 23) {
|
||||
if (scenario == 23) assert(mesh::nrf52::registerTerminalEthernetShutdown(other_owner, nullptr));
|
||||
RAK13800EthernetInterface owner; assert(!owner.begin());
|
||||
assert(!spi_begins && !spi_ends && !pad_releases && !live_tasks);
|
||||
std::printf("PASS: RAK Companion Ethernet worker scenario %d\n", scenario); return 0;
|
||||
}
|
||||
if (scenario == 20) Ethernet.begin_blocked.store(true);
|
||||
Fixture f(scenario != 21);
|
||||
const Bytes reply{1, 0x21, 0x22, 0x23};
|
||||
switch (scenario) {
|
||||
#if defined(OTA_RAK15002_SD_STORE)
|
||||
case 0: { // Admission and task-allocation failures cannot touch SPI.
|
||||
card_fitted.store(true); assert(!f.owner.tryEnable() && !f.prepares);
|
||||
card_fitted.store(false); f.prepare_allowed = false; assert(!f.owner.tryEnable());
|
||||
f.prepare_allowed = true;
|
||||
assert(mesh::ota::rak4631_try_acquire_spi(mesh::ota::RakSharedSpiOwner::Sd));
|
||||
assert(!f.owner.tryEnable()); mesh::ota::rak4631_release_spi(mesh::ota::RakSharedSpiOwner::Sd);
|
||||
fail_create.store(true); assert(!f.owner.tryEnable());
|
||||
assert(!spi_begins && !live_tasks && !mesh::ota::rak4631_ethernet_owns_spi());
|
||||
break;
|
||||
}
|
||||
case 1: { // Slow startup never blocks the main loop, and OFF waits for its entered call.
|
||||
Ethernet.begin_blocked.store(true); assert(f.owner.tryEnable());
|
||||
wait_until([] { return Ethernet.begin_count.load() == 1; });
|
||||
const auto ops = hardware_ops.load(); f.pump(1000);
|
||||
assert(f.polls == 1000 && hardware_ops.load() == ops && !f.owner.isConnected());
|
||||
assert(f.owner.tryDisable()); f.pump(100);
|
||||
assert(live_tasks == 1 && spi_active && mesh::ota::rak4631_ethernet_owns_spi());
|
||||
unblock(Ethernet.begin_blocked); f.off();
|
||||
assert(f.releases == 1 && task_deletions == 1); break;
|
||||
}
|
||||
case 2: { // OFF before worker scheduling performs no hardware initialization.
|
||||
defer_start.store(true); assert(f.owner.tryEnable()); assert(f.owner.tryDisable());
|
||||
unblock(defer_start); f.off(); assert(spi_begins == 0 && Ethernet.begin_count == 0); break;
|
||||
}
|
||||
case 3: { // Production framing retains partial writes and delivers a command.
|
||||
f.online(); f.connect(); sockets[0].capacity.store(2);
|
||||
assert(f.owner.writeFrame(reply.data(), reply.size()) == reply.size()); f.tick();
|
||||
assert(output(0) == Bytes({'>', uint8_t(reply.size())}));
|
||||
sockets[0].capacity.store(0); f.pump(5); assert(output(0).size() == 2);
|
||||
sockets[0].capacity.store(2048); f.drain(0, response(reply));
|
||||
feed(0, request(Bytes{4, 5}));
|
||||
wait_until([&] { return f.tick() == 2; }); assert(f.received[0] == 4 && f.received[1] == 5);
|
||||
assert(f.routes.captureReplyRoute() == &f.owner); f.off(); break;
|
||||
}
|
||||
case 4: { // Renewal is backpressure: no false disconnect, no leaked reply, no parser timeout.
|
||||
f.online(); f.connect(); feed(0, request(Bytes{4}));
|
||||
wait_until([&] { return f.tick() == 1; }); f.routes.lockReplyRoute(); f.pending = true;
|
||||
feed(0, Bytes{'<', 2, 0, 0x41}); f.tick(); assert(f.owner.isReadBusy());
|
||||
Ethernet.maintain_blocked.store(true); const auto before = Ethernet.maintain_count.load();
|
||||
f.tick(1001); wait_until([&] { return Ethernet.maintain_count.load() > before; }, [&] { f.tick(); });
|
||||
const auto ops = hardware_ops.load(); f.pump(10000);
|
||||
assert(f.owner.isConnected() && f.routes.isReplyRouteAvailable(&f.owner));
|
||||
assert(!f.callbacks && f.pending && hardware_ops.load() == ops);
|
||||
assert(f.routes.writeFrame(reply.data(), reply.size()) == reply.size());
|
||||
assert(output(0).empty()); unblock(Ethernet.maintain_blocked);
|
||||
f.drain(0, response(reply)); feed(0, Bytes{0x42});
|
||||
wait_until([&] { return f.tick() == 2; });
|
||||
assert(f.received[0] == 0x41 && f.received[1] == 0x42 && !f.callbacks); f.off(); break;
|
||||
}
|
||||
case 5: { // Bounded listener transitions preserve the old session until their fault deadline.
|
||||
f.online(); f.connect();
|
||||
wait_until([] { return sockets[1].status == SnSR::LISTEN; }, [&] { f.tick(); });
|
||||
Ethernet.accept_blocked.store(true); sockets[1].connected.store(true); sockets[1].ip.store(A);
|
||||
const auto before = Ethernet.accept_count.load();
|
||||
wait_until([&] { return Ethernet.accept_count.load() > before; }, [&] { f.tick(); });
|
||||
const auto ops = main_hardware_ops.load();
|
||||
for (unsigned i = 0; i < 10000; ++i) f.tick(0);
|
||||
assert(f.owner.isConnected() && !f.callbacks && main_hardware_ops.load() == ops);
|
||||
f.tick(251); wait_until([&] { return !f.owner.isConnected(); }, [&] { f.tick(); });
|
||||
assert(f.callbacks == 1); unblock(Ethernet.accept_blocked); f.off(); break;
|
||||
}
|
||||
case 6: case 7: { // Preserve same-IP replay, but cancel different-IP reply ownership first.
|
||||
f.online(); f.connect(); feed(0, request(Bytes{4}));
|
||||
wait_until([&] { return f.tick() == 1; }); f.routes.lockReplyRoute(); f.pending = true;
|
||||
sockets[0].capacity.store(2); assert(f.owner.writeFrame(reply.data(), reply.size()) == reply.size()); f.tick();
|
||||
f.replace(1, scenario == 6 ? A : B);
|
||||
assert(f.callbacks == unsigned(scenario == 7) && f.pending == (scenario == 6));
|
||||
f.drain(1, scenario == 6 ? response(reply) : Bytes{});
|
||||
if (scenario == 7) assert(f.routes.captureReplyRoute() == nullptr);
|
||||
f.off(); break;
|
||||
}
|
||||
case 8: case 9: { // Missing ACK and stuck SEND cannot hang radio/USB service.
|
||||
f.online(); f.connect(); ack_send.store(false); stuck_command.store(scenario == 9);
|
||||
assert(f.owner.writeFrame(reply.data(), reply.size()) == reply.size()); f.tick();
|
||||
f.pending = true; f.pump(300);
|
||||
wait_until([&] { return !f.owner.isConnected(); }, [&] { f.tick(); });
|
||||
assert(f.callbacks == 1 && !f.pending); stuck_command.store(false); f.off(); break;
|
||||
}
|
||||
case 10: { // A stuck RECV command defers consumption and faults with a finite deadline.
|
||||
f.online(); f.connect(); stuck_command.store(true); feed(0, Bytes{'<'});
|
||||
f.tick(); f.pump(300);
|
||||
wait_until([&] { return !f.owner.isConnected(); }, [&] { f.tick(); });
|
||||
assert(f.callbacks == 1); stuck_command.store(false); f.off(); break;
|
||||
}
|
||||
case 11: { // SD insertion during an entered DHCP call permits no new vendor operation.
|
||||
Ethernet.begin_blocked.store(true); assert(f.owner.tryEnable());
|
||||
wait_until([] { return Ethernet.begin_count == 1; }); card_fitted.store(true); f.pump(5);
|
||||
unblock(Ethernet.begin_blocked);
|
||||
wait_until([] { return !spi_active.load(); }, [&] { f.tick(); });
|
||||
assert(mesh::ota::rak4631_ethernet_owns_spi() && live_tasks == 1);
|
||||
assert(!Ethernet.listen_count && !Ethernet.accept_count && !Ethernet.maintain_count);
|
||||
f.off(); assert(!f.owner.tryEnable()); card_fitted.store(false); f.online(); f.off(); break;
|
||||
}
|
||||
case 12: { // Online insertion revokes the session, retains its claim, and gates release.
|
||||
f.online(); f.connect(); f.pending = true; card_fitted.store(true); f.tick();
|
||||
wait_until([] { return !spi_active.load(); }, [&] { f.tick(); });
|
||||
assert(!f.owner.isConnected() && f.callbacks == 1 && !f.pending);
|
||||
f.off_allowed = false; assert(!f.owner.tryDisable());
|
||||
assert(mesh::ota::rak4631_ethernet_owns_spi());
|
||||
f.off_allowed = true; f.off(); card_fitted.store(false); f.online(); f.off(); break;
|
||||
}
|
||||
case 13: { // Repeated acquisitions restore CS and reclaim each worker synchronously.
|
||||
for (unsigned i = 0; i < 5; ++i) { f.online(); f.off(); assert(task_deletions == int(i + 1)); }
|
||||
assert(spi_begins == 5 && spi_ends == 5 && f.releases == 5); break;
|
||||
}
|
||||
case 14: { // Terminal timeout never deletes a worker inside a vendor call.
|
||||
Ethernet.begin_blocked.store(true); assert(f.owner.tryEnable());
|
||||
wait_until([] { return Ethernet.begin_count == 1; });
|
||||
assert(!mesh::nrf52::terminalEthernetShutdown(0));
|
||||
assert(live_tasks == 1 && spi_active && !task_deletions);
|
||||
unblock(Ethernet.begin_blocked);
|
||||
wait_until([&] { return mesh::nrf52::terminalEthernetShutdown(0); }, [&] { f.tick(); });
|
||||
assert(!live_tasks && !spi_active && !mesh::ota::rak4631_ethernet_owns_spi()); break;
|
||||
}
|
||||
case 15: { // OTA release races keep ownership until the main-thread hook succeeds.
|
||||
f.online(); f.release_allowed = false; assert(f.owner.tryDisable());
|
||||
wait_until([] { return live_tasks == 0 && !spi_active.load(); }, [&] { f.tick(); });
|
||||
assert(mesh::ota::rak4631_ethernet_owns_spi());
|
||||
f.release_allowed = true; f.off(); break;
|
||||
}
|
||||
case 16: { // Missing hardware stops cleanly without polling a non-existent controller.
|
||||
Ethernet.hardware.store(EthernetNoHardware); Ethernet.begin_result.store(0);
|
||||
assert(f.owner.tryEnable());
|
||||
wait_until([&] { return std::strcmp(f.owner.status(), "no hardware") == 0 && !spi_active; }, [&] { f.tick(); });
|
||||
const auto calls = Ethernet.begin_count.load(); f.pump(1000);
|
||||
assert(calls == 1 && Ethernet.begin_count == calls && !f.owner.isConnected()); f.off(); break;
|
||||
}
|
||||
case 17: { // OFF wakes the 30-second DHCP retry without waiting for its timer.
|
||||
Ethernet.begin_result.store(0); assert(f.owner.tryEnable());
|
||||
wait_until([&] { return std::strcmp(f.owner.status(), "retrying") == 0; }, [&] { f.tick(); });
|
||||
f.off(); assert(Ethernet.begin_count == 1); break;
|
||||
}
|
||||
case 18: { // An occupied OTA owner rejects OFF without cancelling the connected session.
|
||||
f.online(); f.connect(); f.off_allowed = false; f.pending = true;
|
||||
assert(!f.owner.tryDisable()); f.pump(50);
|
||||
assert(f.owner.isConnected() && !f.callbacks && f.pending && spi_active && live_tasks == 1);
|
||||
f.off_allowed = true; f.off(); break;
|
||||
}
|
||||
case 19: { // A failed ACK retains the current complete frame for same-IP reconnect.
|
||||
f.online(); f.connect(); ack_send.store(false);
|
||||
assert(f.owner.writeFrame(reply.data(), reply.size()) == reply.size()); f.tick();
|
||||
wait_until([&] { return output(0) == response(reply); }, [&] { f.tick(); });
|
||||
f.pump(300);
|
||||
wait_until([&] { return !f.owner.isConnected(); }, [&] { f.tick(); });
|
||||
assert(f.callbacks == 1); ack_send.store(true); f.connect(1, A);
|
||||
f.drain(1, response(reply)); f.pump(30); assert(output(1) == response(reply)); f.off(); break;
|
||||
}
|
||||
case 21: { // Missing safety hooks must refuse startup before driving shared pads.
|
||||
assert(!f.owner.tryEnable() && !f.prepares && !spi_begins && !live_tasks);
|
||||
assert(!mesh::ota::rak4631_ethernet_owns_spi() && pin_modes[26] == INPUT); break;
|
||||
}
|
||||
case 24: { // Never-started terminal shutdown leaves optional storage pins untouched.
|
||||
assert(mesh::nrf52::terminalEthernetShutdown(0));
|
||||
assert(!spi_begins && !spi_ends && !pad_releases && !live_tasks && !f.releases);
|
||||
assert(mesh::nrf52::terminalEthernetShutdown(0) && !f.owner.tryEnable()); break;
|
||||
}
|
||||
case 25: { // Online terminal shutdown reclaims once, releases pads, and cancels once.
|
||||
f.online(); f.connect(); f.pending = true;
|
||||
wait_until([&] { return mesh::nrf52::terminalEthernetShutdown(0); }, [&] { f.tick(); });
|
||||
assert(f.callbacks == 1 && !f.pending && pad_releases == 5 && task_deletions == 1);
|
||||
assert(f.releases == 1 && !live_tasks && !spi_active && !reserved());
|
||||
assert(mesh::nrf52::terminalEthernetShutdown(0));
|
||||
assert(pad_releases == 5 && task_deletions == 1 && f.releases == 1); break;
|
||||
}
|
||||
case 26: { // A refused OTA handoff remains safe after terminal hardware shutdown.
|
||||
f.online(); f.release_allowed = false;
|
||||
assert(!mesh::nrf52::terminalEthernetShutdown(0));
|
||||
wait_until([] { return !spi_active.load() && !live_tasks; }, [&] { (void)mesh::nrf52::terminalEthernetShutdown(0); });
|
||||
assert(!mesh::nrf52::terminalEthernetShutdown(0));
|
||||
assert(reserved() && !live_tasks && pad_releases == 5);
|
||||
f.release_allowed = true; assert(mesh::nrf52::terminalEthernetShutdown(0) && !reserved()); break;
|
||||
}
|
||||
case 27: { // The shutdown deadline is bounded across millis rollover.
|
||||
Ethernet.begin_blocked.store(true); assert(f.owner.tryEnable());
|
||||
wait_until([] { return Ethernet.begin_count == 1; }); mock_millis.store(UINT32_MAX - 3);
|
||||
assert(!mesh::nrf52::terminalEthernetShutdown(5));
|
||||
assert(mock_millis < 5 && live_tasks == 1 && spi_active && !task_deletions);
|
||||
unblock(Ethernet.begin_blocked);
|
||||
wait_until([&] { return mesh::nrf52::terminalEthernetShutdown(0); }, [&] { f.tick(); }); break;
|
||||
}
|
||||
case 28: case 30: { // A cleared command register is not proof of the requested state transition.
|
||||
if (scenario == 28) open_status.store(SnSR::CLOSED);
|
||||
else listen_status.store(SnSR::INIT);
|
||||
f.online(); f.pump(300);
|
||||
wait_until([&] { return std::strcmp(f.owner.status(), "faulted") == 0
|
||||
|| Ethernet.accept_count > 1 || Ethernet.listen_count > 1; }, [&] { f.tick(); });
|
||||
assert(std::strcmp(f.owner.status(), "faulted") == 0 && "wrong listener status must fault once");
|
||||
assert(Ethernet.accept_count == 1 && Ethernet.listen_count == int(scenario == 30));
|
||||
assert(!f.owner.isConnected()); f.off(); break;
|
||||
}
|
||||
case 29: { // A fast SYNRECV after LISTEN is healthy, not a missed LISTEN response.
|
||||
listen_status.store(SnSR::SYNRECV); f.online();
|
||||
wait_until([] { return sockets[0].status == SnSR::SYNRECV; }, [&] { f.tick(); });
|
||||
f.pump(300); assert(std::strcmp(f.owner.status(), "online") == 0);
|
||||
assert(Ethernet.accept_count == 1 && Ethernet.listen_count == 1);
|
||||
sockets[0].connected.store(true); sockets[0].ip.store(A);
|
||||
wait_until([&] { return f.owner.isConnected(); }, [&] { f.tick(); });
|
||||
assert(f.owner.writeFrame(reply.data(), reply.size()) == reply.size());
|
||||
f.drain(0, response(reply)); f.off(); break;
|
||||
}
|
||||
case 31: case 32: { // Reclaim one closing socket, never a DHCP socket or live peer.
|
||||
sockets[0].status.store(SnSR::UDP);
|
||||
sockets[1].connected.store(true); sockets[1].status.store(SnSR::ESTABLISHED);
|
||||
sockets[2].status.store(SnSR::SYNRECV); sockets[3].status.store(SnSR::FIN_WAIT);
|
||||
if (scenario == 32) close_status.store(SnSR::TIME_WAIT);
|
||||
f.online();
|
||||
if (scenario == 31) {
|
||||
f.connect(3); assert(sockets[3].closes == 1 && Ethernet.accept_count == 1);
|
||||
assert(sockets[0].closes == 0 && sockets[1].closes == 0 && sockets[2].closes == 0);
|
||||
assert(f.owner.writeFrame(reply.data(), reply.size()) == reply.size()); f.drain(3, response(reply));
|
||||
} else {
|
||||
f.pump(300);
|
||||
wait_until([&] { return std::strcmp(f.owner.status(), "faulted") == 0; }, [&] { f.tick(); });
|
||||
// One fallback CLOSE, plus at most the normal terminal cleanup CLOSE.
|
||||
assert(sockets[3].closes >= 1 && sockets[3].closes <= 2 && Ethernet.accept_count == 0);
|
||||
close_status.store(SnSR::CLOSED);
|
||||
}
|
||||
f.off(); break;
|
||||
}
|
||||
case 33: { // Direct restart must cancel an unobserved stopped-fault session before clearing its event.
|
||||
f.online(); f.connect(); feed(0, request(Bytes{4}));
|
||||
wait_until([&] { return f.tick() == 1; }); f.routes.lockReplyRoute(); f.pending = true;
|
||||
stuck_command.store(true); ack_send.store(false);
|
||||
assert(f.owner.writeFrame(reply.data(), reply.size()) == reply.size());
|
||||
wait_until([&] { return output(0) == response(reply); }, [&] { f.tick(); });
|
||||
mock_millis.fetch_add(300); xTaskNotifyGive(tasks.back().get());
|
||||
wait_until([] { return parked_tasks == 1; }); // Deliberately no driver.loop().
|
||||
assert(f.callbacks == 0 && f.pending && f.owner.isConnected());
|
||||
stuck_command.store(false); ack_send.store(true); assert(f.owner.tryEnable());
|
||||
assert(f.callbacks == 1 && !f.pending && !f.owner.isConnected());
|
||||
assert(f.routes.captureReplyRoute() == nullptr && task_deletions == 1);
|
||||
f.online(); f.off(); break;
|
||||
}
|
||||
#if defined(MESH_TEST_PRIVATE_ACCESS)
|
||||
case 34: { // Hold the real worker before entry and reproduce atomic-publication gaps deterministically.
|
||||
defer_start.store(true); assert(f.owner.tryEnable());
|
||||
const auto prepares = f.prepares;
|
||||
f.owner._state.store(RAK13800EthernetInterface::State::Faulted);
|
||||
assert(!f.owner.tryEnable() && f.prepares == prepares && live_tasks == 1);
|
||||
f.owner._state.store(RAK13800EthernetInterface::State::Online); f.owner._wanted.store(false);
|
||||
assert(!f.owner.tryEnable() && f.prepares == prepares && live_tasks == 1);
|
||||
unblock(defer_start); f.off(); assert(!spi_begins); break;
|
||||
}
|
||||
#endif
|
||||
case 35: case 36: case 37: { // A SEND awaiting ACK must pin its frame before send_offset advances.
|
||||
const Bytes head{0x80, 0x41, 0x42}, best_effort{0x81, 0x51}, required{0x82, 0x61}, second{2, 0x31};
|
||||
f.online(); f.connect(); ack_send.store(false);
|
||||
assert(f.owner.writeFrame(head.data(), head.size()) == head.size());
|
||||
wait_until([&] { return output(0) == response(head); }, [&] { f.tick(); });
|
||||
if (scenario == 37) {
|
||||
ack_send.store(true);
|
||||
wait_until([&] { f.owner.loop(); return !f.owner.isWriteBusy(); }, [&] { mock_millis.fetch_add(1); });
|
||||
// No base checkRecvFrame(): ACK has arrived, but the head's credit is not retired.
|
||||
}
|
||||
assert(f.owner.writeFrame(best_effort.data(), best_effort.size()) == best_effort.size());
|
||||
assert(f.owner.writeFrame(required.data(), required.size()) == required.size());
|
||||
assert(f.owner.writeFrame(reply.data(), reply.size()) == reply.size());
|
||||
assert(f.owner.writeFrame(second.data(), second.size()) == second.size());
|
||||
assert(f.owner.writeFrame(required.data(), required.size()) == 0); // Never evict the pinned head.
|
||||
Bytes expected;
|
||||
for (const auto& bytes : {head, reply, second, required}) {
|
||||
const auto frame = response(bytes); expected.insert(expected.end(), frame.begin(), frame.end());
|
||||
}
|
||||
if (scenario == 36) {
|
||||
mock_millis.fetch_add(300); xTaskNotifyGive(tasks.back().get());
|
||||
wait_until([&] { return !f.owner.isConnected(); }, [&] { f.tick(); });
|
||||
assert(f.callbacks == 1); ack_send.store(true); f.connect(1, A); f.drain(1, expected);
|
||||
} else {
|
||||
ack_send.store(true); f.drain(0, expected);
|
||||
}
|
||||
f.off(); break;
|
||||
}
|
||||
#else
|
||||
case 20: { // Legacy boot automatically starts the worker, never requiring SD hooks.
|
||||
wait_until([] { return Ethernet.begin_count.load() == 1; });
|
||||
const auto ops = hardware_ops.load(); f.pump(1000);
|
||||
assert(f.polls == 1000 && hardware_ops.load() == ops && !f.owner.isConnected());
|
||||
unblock(Ethernet.begin_blocked); f.online(); f.connect();
|
||||
feed(0, request(Bytes{4, 5})); wait_until([&] { return f.tick() == 2; });
|
||||
assert(f.received[0] == 4 && f.received[1] == 5 && f.routes.captureReplyRoute() == &f.owner);
|
||||
assert(f.owner.writeFrame(reply.data(), reply.size()) == reply.size());
|
||||
f.drain(0, response(reply)); f.off(); assert(f.prepares == 0 && f.releases == 0); break;
|
||||
}
|
||||
#endif
|
||||
default: assert(false);
|
||||
}
|
||||
for (auto& task : tasks) assert(!task->thread.joinable());
|
||||
assert(live_tasks == 0 && !spi_active.load());
|
||||
std::printf("PASS: RAK Companion Ethernet worker scenario %d\n", scenario);
|
||||
}
|
||||
+257
-1
@@ -38,25 +38,54 @@ static Bytes command(const Bytes& payload) {
|
||||
|
||||
struct Transport : SerialEthernetInterface {
|
||||
bool connected = false;
|
||||
bool io_unavailable = false;
|
||||
bool pause_after_write = false;
|
||||
bool pause_after_read = false;
|
||||
bool pause_after_available = false;
|
||||
int available_calls = 0;
|
||||
int write_calls = 0;
|
||||
int disconnect_calls = 0;
|
||||
Socket client;
|
||||
Socket next_client;
|
||||
bool isIOUnavailable() const override { return io_unavailable; }
|
||||
void loop() override { serviceIOAvailability(); }
|
||||
bool isConnected() const override { return isEnabled() && connected; }
|
||||
void disconnectClient() override {
|
||||
++disconnect_calls;
|
||||
connected = false;
|
||||
if (client) client->connected = false;
|
||||
}
|
||||
int available() override { return client ? int(client->received.size()) : 0; }
|
||||
int available() override {
|
||||
assert(!io_unavailable);
|
||||
++available_calls;
|
||||
if (pause_after_available) {
|
||||
io_unavailable = true;
|
||||
pause_after_available = false;
|
||||
}
|
||||
return client ? int(client->received.size()) : 0;
|
||||
}
|
||||
int read() override {
|
||||
assert(!io_unavailable);
|
||||
if (!client || client->received.empty()) return -1;
|
||||
const int value = client->received.front();
|
||||
client->received.pop_front();
|
||||
++client->read_count;
|
||||
if (pause_after_read) {
|
||||
io_unavailable = true;
|
||||
pause_after_read = false;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
size_t write(const uint8_t* src, size_t size) override {
|
||||
assert(!io_unavailable);
|
||||
++write_calls;
|
||||
if (!isConnected() || !client) return 0;
|
||||
size = std::min(size, client->write_limit);
|
||||
client->sent.insert(client->sent.end(), src, src + size);
|
||||
if (pause_after_write) {
|
||||
io_unavailable = true;
|
||||
pause_after_write = false;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
Socket connect(uint32_t ip, const Bytes& input = {}) {
|
||||
@@ -244,6 +273,233 @@ int main(int argc, char** argv) {
|
||||
socket->received.insert(socket->received.end(), data.begin(), data.end());
|
||||
assert(f.transport.checkRecvFrame(nullptr) == 0);
|
||||
assert(!socket->connected && f.callbacks == 1);
|
||||
} else if (scenario == 16) {
|
||||
f.enqueue(reply);
|
||||
socket->write_limit = 2;
|
||||
assert(f.tick() == 0);
|
||||
const Bytes prefix = socket->sent;
|
||||
assert(prefix.size() == 2);
|
||||
const Bytes request = command(other);
|
||||
socket->received.insert(socket->received.end(), request.begin(), request.end());
|
||||
const int available_calls = f.transport.available_calls;
|
||||
const int write_calls = f.transport.write_calls;
|
||||
const int disconnect_calls = f.transport.disconnect_calls;
|
||||
f.transport.io_unavailable = true;
|
||||
assert(f.transport.isConnected());
|
||||
assert(f.transport.hasPendingIO());
|
||||
assert(f.transport.isWriteBusy());
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
mock_millis += 10000;
|
||||
assert(f.tick() == 0);
|
||||
}
|
||||
assert(f.transport.available_calls == available_calls);
|
||||
assert(f.transport.write_calls == write_calls);
|
||||
assert(f.transport.disconnect_calls == disconnect_calls);
|
||||
assert(socket->sent == prefix && socket->read_count == 0);
|
||||
assert(f.callbacks == 0 && f.transport.isConnected());
|
||||
f.transport.io_unavailable = false;
|
||||
assert(!f.transport.isWriteBusy());
|
||||
socket->write_limit = 100;
|
||||
assert(f.tick() == 0);
|
||||
assert(socket->sent == framed({reply}));
|
||||
assert(f.tick() == other.size());
|
||||
assert(f.input[0] == other[0] && f.callbacks == 0);
|
||||
} else if (scenario == 17 || scenario == 18 || scenario == 19 || scenario == 24) {
|
||||
if (scenario == 19) mock_millis = UINT32_MAX - 3000;
|
||||
if (scenario == 24) mock_millis = UINT32_MAX - 1999;
|
||||
Bytes partial = command(Bytes{'a', 'b'});
|
||||
partial.pop_back();
|
||||
socket->received.insert(socket->received.end(), partial.begin(), partial.end());
|
||||
assert(f.tick() == 0 && f.transport.isReadBusy());
|
||||
mock_millis += scenario == 18 ? 5000 : 2000;
|
||||
f.transport.io_unavailable = true;
|
||||
const int disconnect_calls = f.transport.disconnect_calls;
|
||||
assert(f.tick() == 0 && socket->connected);
|
||||
// Repeated polls must preserve the first pause boundary, including a
|
||||
// boundary at millis()==0 and a pause that crosses the 32-bit rollover.
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
mock_millis += 4000;
|
||||
assert(f.tick() == 0 && socket->connected);
|
||||
}
|
||||
assert(f.transport.disconnect_calls == disconnect_calls);
|
||||
f.transport.io_unavailable = false;
|
||||
#if !ETHERNET_RAW_LINE
|
||||
if (scenario == 18) {
|
||||
assert(f.tick() == 0 && !socket->connected);
|
||||
assert(f.callbacks == 1 && !f.transport.isReadBusy());
|
||||
} else {
|
||||
assert(f.tick() == 0 && socket->connected);
|
||||
mock_millis += 1499;
|
||||
assert(f.tick() == 0 && socket->connected);
|
||||
// A second long pause must preserve the 1501ms remaining, not grant a
|
||||
// new five-second budget on each DHCP/renewal worker operation.
|
||||
f.transport.io_unavailable = true;
|
||||
assert(f.tick() == 0 && socket->connected);
|
||||
mock_millis += 20000;
|
||||
assert(f.tick() == 0 && socket->connected);
|
||||
f.transport.io_unavailable = false;
|
||||
assert(f.tick() == 0 && socket->connected);
|
||||
mock_millis += 1500;
|
||||
assert(f.tick() == 0 && socket->connected);
|
||||
mock_millis += 1;
|
||||
assert(f.tick() == 0 && !socket->connected);
|
||||
assert(f.callbacks == 1 && !f.transport.isReadBusy());
|
||||
}
|
||||
#else
|
||||
// Human terminal input has no binary framing deadline, even when already
|
||||
// older than five seconds at the start of worker ownership.
|
||||
mock_millis += 60000;
|
||||
socket->received.push_back('\n');
|
||||
assert(f.tick() == 2 && f.input[0] == 'a' && f.input[1] == 'b');
|
||||
assert(f.callbacks == 0 && !f.transport.isReadBusy());
|
||||
#endif
|
||||
} else if (scenario >= 20 && scenario <= 23) {
|
||||
Bytes partial = command(Bytes{'a', 'b'});
|
||||
partial.pop_back();
|
||||
socket->received.insert(socket->received.end(), partial.begin(), partial.end());
|
||||
assert(f.tick() == 0 && f.transport.isReadBusy());
|
||||
f.enqueue(reply);
|
||||
socket->write_limit = 2;
|
||||
assert(f.tick() == 0 && socket->sent.size() == 2);
|
||||
mock_millis += 2000;
|
||||
f.transport.io_unavailable = true;
|
||||
assert(f.tick() == 0);
|
||||
mock_millis += 20000;
|
||||
Socket replacement;
|
||||
if (scenario == 20) {
|
||||
f.transport.disconnectClient();
|
||||
f.transport.onClientDisconnected();
|
||||
replacement = f.transport.connect(A);
|
||||
assert(f.callbacks == 1);
|
||||
} else if (scenario == 21) {
|
||||
f.transport.disable();
|
||||
f.transport.enable();
|
||||
replacement = f.transport.connect(A);
|
||||
assert(f.callbacks == 1);
|
||||
} else {
|
||||
replacement = f.transport.connect(scenario == 22 ? A : B);
|
||||
assert(f.callbacks == (scenario == 22 ? 0 : 1));
|
||||
}
|
||||
assert(!f.transport.isReadBusy());
|
||||
assert(f.tick() == 0 && replacement->connected);
|
||||
mock_millis += 20000;
|
||||
assert(f.tick() == 0 && replacement->connected);
|
||||
f.transport.io_unavailable = false;
|
||||
f.drain(replacement);
|
||||
assert(replacement->sent == ((scenario == 20 || scenario == 22)
|
||||
? framed({reply}) : Bytes{}));
|
||||
// The replacement's frame must receive its own full timeout and must not
|
||||
// inherit any bytes or suspended time from the displaced stream.
|
||||
replacement->received.insert(replacement->received.end(), partial.begin(), partial.end());
|
||||
assert(f.tick() == 0 && f.transport.isReadBusy());
|
||||
mock_millis += 4999;
|
||||
assert(f.tick() == 0 && replacement->connected);
|
||||
replacement->received.push_back(
|
||||
#if ETHERNET_RAW_LINE
|
||||
'\n'
|
||||
#else
|
||||
'b'
|
||||
#endif
|
||||
);
|
||||
assert(f.tick() == 2 && f.input[0] == 'a' && f.input[1] == 'b');
|
||||
} else if (scenario == 25) {
|
||||
f.transport.io_unavailable = true;
|
||||
assert(f.tick() == 0);
|
||||
mock_millis += 20000;
|
||||
assert(f.tick() == 0);
|
||||
f.transport.io_unavailable = false;
|
||||
Bytes partial = command(Bytes{'a', 'b'});
|
||||
partial.pop_back();
|
||||
socket->received.insert(socket->received.end(), partial.begin(), partial.end());
|
||||
assert(f.tick() == 0 && f.transport.isReadBusy());
|
||||
mock_millis += 4999;
|
||||
assert(f.tick() == 0 && socket->connected);
|
||||
mock_millis += 1;
|
||||
#if ETHERNET_RAW_LINE
|
||||
socket->received.push_back('\n');
|
||||
assert(f.tick() == 2 && f.callbacks == 0);
|
||||
#else
|
||||
assert(f.tick() == 0 && !socket->connected && f.callbacks == 1);
|
||||
#endif
|
||||
} else if (scenario == 26) {
|
||||
Bytes partial = command(Bytes{'a', 'b'});
|
||||
partial.pop_back();
|
||||
socket->received.insert(socket->received.end(), partial.begin(), partial.end());
|
||||
assert(f.tick() == 0 && f.transport.isReadBusy());
|
||||
f.enqueue(reply);
|
||||
socket->write_limit = 2;
|
||||
mock_millis += 2000;
|
||||
f.transport.pause_after_write = true;
|
||||
assert(f.tick() == 0 && socket->sent.size() == 2);
|
||||
assert(f.transport.io_unavailable);
|
||||
mock_millis += 12000;
|
||||
f.transport.io_unavailable = false;
|
||||
socket->write_limit = 100;
|
||||
assert(f.tick() == 0 && socket->connected);
|
||||
assert(socket->sent == framed({reply}));
|
||||
mock_millis += 2999;
|
||||
socket->received.push_back(
|
||||
#if ETHERNET_RAW_LINE
|
||||
'\n'
|
||||
#else
|
||||
'b'
|
||||
#endif
|
||||
);
|
||||
assert(f.tick() == 2 && f.input[0] == 'a' && f.input[1] == 'b');
|
||||
assert(f.callbacks == 0);
|
||||
} else if (scenario == 27 || scenario == 28) {
|
||||
const Bytes request = command(Bytes{'a', 'b'});
|
||||
socket->received.insert(socket->received.end(), request.begin(), request.end());
|
||||
if (scenario == 27) f.transport.pause_after_read = true;
|
||||
else f.transport.pause_after_available = true;
|
||||
assert(f.tick() == 0 && socket->connected);
|
||||
assert(f.transport.io_unavailable);
|
||||
assert(socket->read_count == (scenario == 27 ? 1U : 0U));
|
||||
assert(f.transport.isReadBusy() == (scenario == 27));
|
||||
mock_millis += 12000;
|
||||
f.transport.io_unavailable = false;
|
||||
assert(f.tick() == 2 && f.input[0] == 'a' && f.input[1] == 'b');
|
||||
assert(f.callbacks == 0 && !f.transport.isReadBusy());
|
||||
} else if (scenario == 29) {
|
||||
Bytes partial = command(Bytes{'a', 'b'});
|
||||
partial.pop_back();
|
||||
socket->received.insert(socket->received.end(), partial.begin(), partial.end());
|
||||
assert(f.tick() == 0 && f.transport.isReadBusy());
|
||||
// Exercise the actual MultiSerial routing path: another interface owns a
|
||||
// multi-frame response, so Ethernet receives loop() service but no frame
|
||||
// polls until that producer releases its route.
|
||||
Transport other_transport;
|
||||
assert(f.routes.addInterface(InterfaceType::USB, &other_transport));
|
||||
other_transport.enable();
|
||||
auto other_socket = other_transport.connect(B, command(Bytes{0x07}));
|
||||
assert(f.tick() == 1 && f.input[0] == 0x07);
|
||||
assert(f.routes.captureReplyRoute() == &other_transport);
|
||||
f.routes.lockReplyRoute();
|
||||
mock_millis += 2000;
|
||||
f.transport.io_unavailable = true;
|
||||
f.routes.loop(); // Main-side ownership handoff, not a frame poll.
|
||||
const int available_calls = f.transport.available_calls;
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
mock_millis += 4000;
|
||||
f.routes.loop();
|
||||
assert(f.tick() == 0 && socket->connected);
|
||||
}
|
||||
assert(f.transport.available_calls == available_calls);
|
||||
assert(f.transport.isReadBusy() && f.callbacks == 0);
|
||||
f.transport.io_unavailable = false;
|
||||
f.routes.loop(); // Main-side completion releases the preserved budget.
|
||||
f.routes.unlockReplyRoute();
|
||||
mock_millis += 2999;
|
||||
assert(f.tick() == 0 && socket->connected);
|
||||
mock_millis += 1;
|
||||
#if ETHERNET_RAW_LINE
|
||||
socket->received.push_back('\n');
|
||||
assert(f.tick() == 2 && f.input[0] == 'a' && f.input[1] == 'b');
|
||||
assert(f.callbacks == 0);
|
||||
#else
|
||||
assert(f.tick() == 0 && !socket->connected && f.callbacks == 1);
|
||||
#endif
|
||||
assert(other_socket->connected);
|
||||
} else {
|
||||
assert(false && "unknown scenario");
|
||||
}
|
||||
|
||||
@@ -126,6 +126,56 @@ class CompanionContactSleepContractTest(unittest.TestCase):
|
||||
self.assertIn("event-driven sleep instead of timed sleep", body)
|
||||
self.assertIn("sd_app_evt_wait();", body)
|
||||
|
||||
def test_polled_rak_ethernet_holds_event_sleep_through_pending_off(self):
|
||||
main = (ROOT / "examples/companion_radio/main.cpp").read_text()
|
||||
start = main.index("bool can_sleep =")
|
||||
gate = main[start:main.index("if (can_sleep)", start)]
|
||||
fixture = r'''
|
||||
#include <cassert>
|
||||
struct Prefs { bool powersaving_enabled; };
|
||||
struct Mesh {
|
||||
Prefs prefs; bool pending;
|
||||
Prefs* getNodePrefs() { return &prefs; }
|
||||
bool hasPendingWork() const { return pending; }
|
||||
} the_mesh;
|
||||
struct Board { bool usb; bool isUsbHostConnected() const { return usb; } } board;
|
||||
struct Ethernet { bool active; bool hardwareActive() const { return active; } } ethernet_interface;
|
||||
namespace mesh {
|
||||
bool watchdog;
|
||||
bool isUsbLoggingWatchdogArmed() { return watchdog; }
|
||||
namespace wireless {
|
||||
struct Control { bool work; bool pending() const { return work; } } instance;
|
||||
Control& control() { return instance; }
|
||||
} }
|
||||
int main() {
|
||||
for (unsigned flags = 0; flags < 128; ++flags) {
|
||||
the_mesh.prefs.powersaving_enabled = !(flags & 1);
|
||||
the_mesh.pending = flags & 2;
|
||||
bool morse_timing = flags & 4;
|
||||
mesh::watchdog = flags & 8;
|
||||
mesh::wireless::instance.work = flags & 16;
|
||||
board.usb = flags & 32;
|
||||
ethernet_interface.active = flags & 64; // includes OFF accepted but unreleased
|
||||
@GATE@
|
||||
#if defined(ETHERNET_USE_RAK13800)
|
||||
assert(can_sleep == (flags == 0));
|
||||
#else
|
||||
assert(can_sleep == ((flags & 63) == 0));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
'''.replace('@GATE@', gate)
|
||||
with tempfile.TemporaryDirectory(prefix='companion-ethernet-sleep-') as directory:
|
||||
source, binary = Path(directory) / 'sleep.cpp', Path(directory) / 'sleep'
|
||||
source.write_text(fixture, encoding='ascii')
|
||||
for ethernet in (False, True):
|
||||
with self.subTest(ethernet=ethernet):
|
||||
subprocess.run([os.environ.get('CXX', 'g++'), '-std=c++17',
|
||||
'-Wall', '-Wextra', '-Werror', '-DNRF52_PLATFORM=1',
|
||||
*(['-DETHERNET_USE_RAK13800=1'] if ethernet else []),
|
||||
str(source), '-o', str(binary)], check=True, timeout=30)
|
||||
subprocess.run([str(binary)], check=True, timeout=5)
|
||||
|
||||
def test_all_runtime_reboots_flush_contacts_first(self):
|
||||
mesh = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text()
|
||||
header = (ROOT / "examples/companion_radio/MyMesh.h").read_text()
|
||||
|
||||
@@ -6,6 +6,7 @@ import tempfile
|
||||
import unittest
|
||||
|
||||
from test_replay_reset_integration import extract_braced
|
||||
from test_rak_companion_ethernet_worker import build_worker
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
ARDUINO = """#pragma once
|
||||
@@ -272,50 +273,19 @@ int main(){
|
||||
run_sources(self, (LEGACY.replace("@LEGACY@", source[start:end]),), ("0","1","2","3","4"))
|
||||
|
||||
def test_rak3401_rejects_electrically_conflicting_ethernet_before_hardware(self):
|
||||
source = SYNC.split("int main(",1)[0]
|
||||
stem = ROOT/"src/helpers/ethernet/RAK13800/RAK13800EthernetInterface"
|
||||
header = stem.with_suffix(".h").read_text()
|
||||
cpp = stem.with_suffix(".cpp").read_text()
|
||||
cls = extract_braced(header,"class RAK13800EthernetInterface")+";"
|
||||
# Only the actual begin() and virtual methods are needed; the complete
|
||||
# implementation is already exercised above. Reuse its method builder.
|
||||
methods = "\n".join(extract_braced(cpp,sig) for sig in (
|
||||
"bool RAK13800EthernetInterface::begin()",
|
||||
"bool RAK13800EthernetInterface::beginHardware()",
|
||||
"bool RAK13800EthernetInterface::terminalShutdown(",
|
||||
"bool RAK13800EthernetInterface::shutdownForPowerOff(",
|
||||
"void RAK13800EthernetInterface::disconnectClient()",
|
||||
"bool RAK13800EthernetInterface::isConnected() const",
|
||||
"int RAK13800EthernetInterface::available()",
|
||||
"int RAK13800EthernetInterface::read()",
|
||||
"size_t RAK13800EthernetInterface::write(",
|
||||
"void RAK13800EthernetInterface::loop()",
|
||||
)).replace("uint16_t remotePort = newClient.remotePort();", "(void)newClient.remotePort();")
|
||||
source = "#define RAK_3401 1\n"+source.replace("@CLASS@",cls).replace("@METHODS@",methods)
|
||||
source += "int main(){RAK13800EthernetInterface owner;assert(!owner.begin());assert(!spi&&begins==0&&pads==0);}\n"
|
||||
run_sources(self,(source,))
|
||||
with tempfile.TemporaryDirectory(prefix="rak3401-ethernet-terminal-") as folder:
|
||||
binary = build_worker(self, folder, adaptive=False, rak3401=True)
|
||||
result = subprocess.run([str(binary), "22"], text=True, capture_output=True, timeout=10)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
|
||||
def test_actual_synchronous_companion_shutdown_is_bounded_and_initialized_only(self):
|
||||
stem = ROOT/"src/helpers/ethernet/RAK13800/RAK13800EthernetInterface"
|
||||
header = stem.with_suffix(".h").read_text()
|
||||
cpp = stem.with_suffix(".cpp").read_text()
|
||||
cls = extract_braced(header,"class RAK13800EthernetInterface")+";"
|
||||
signatures = (
|
||||
"bool RAK13800EthernetInterface::begin()",
|
||||
"bool RAK13800EthernetInterface::beginHardware()",
|
||||
"bool RAK13800EthernetInterface::terminalShutdown(",
|
||||
"bool RAK13800EthernetInterface::shutdownForPowerOff(",
|
||||
"void RAK13800EthernetInterface::disconnectClient()",
|
||||
"bool RAK13800EthernetInterface::isConnected() const",
|
||||
"int RAK13800EthernetInterface::available()",
|
||||
"int RAK13800EthernetInterface::read()",
|
||||
"size_t RAK13800EthernetInterface::write(",
|
||||
"void RAK13800EthernetInterface::loop()",
|
||||
)
|
||||
methods = "\n".join(extract_braced(cpp,sig) for sig in signatures)
|
||||
# Disabled debug logging intentionally discards remotePort.
|
||||
methods = methods.replace("uint16_t remotePort = newClient.remotePort();", "(void)newClient.remotePort();")
|
||||
run_sources(self,(SYNC.replace("@CLASS@",cls).replace("@METHODS@",methods),), ("0","1","2","3"))
|
||||
def test_actual_async_companion_shutdown_is_bounded_and_initialized_only(self):
|
||||
with tempfile.TemporaryDirectory(prefix="rak-companion-ethernet-terminal-") as folder:
|
||||
binary = build_worker(self, folder)
|
||||
for scenario in (14, 23, 24, 25, 26, 27):
|
||||
with self.subTest(scenario=scenario):
|
||||
result = subprocess.run([str(binary), str(scenario)], text=True,
|
||||
capture_output=True, timeout=10)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -761,7 +761,7 @@ int main() {
|
||||
self.assertEqual(sd["components"]["ota_generic_source_buffers"],
|
||||
base["components"]["ota_generic_source_buffers"])
|
||||
|
||||
def test_optional_sd_room_ethernet_counts_exclusive_worker_but_companion_has_no_worker(self):
|
||||
def test_optional_sd_room_excludes_workspaces_but_companion_counts_worker_additively(self):
|
||||
flags = {"ENABLE_OTA": 1, "OTA_SD_STORE": 1, "OTA_RAK15002_SD_STORE": 1,
|
||||
"ETHERNET_ENABLED": 1, "RAK_4631": 1, "OTA_RAK_AUTO_STORE": 1,
|
||||
"MESHCORE_OTA_DEVICE_DEFLATE": 1}
|
||||
@@ -778,6 +778,38 @@ int main() {
|
||||
"RAK_4631_companion_radio_full")
|
||||
self.assertNotIn("ethernet_or_external_ota_workspaces", companion["components"])
|
||||
self.assertIn("ota_generic_source_buffers", companion["components"])
|
||||
self.assertEqual(companion["components"]["ethernet_companion_worker"], 4096 + 256)
|
||||
without_eth = ram.requirements("NRF52_PLATFORM", {
|
||||
**{key: value for key, value in flags.items() if key != "ETHERNET_ENABLED"},
|
||||
"OTA_SEEDER_ONLY": 1}, "RAK_4631_companion_radio_full")
|
||||
self.assertEqual(companion["required_heap_bytes"] - without_eth["required_heap_bytes"], 4352)
|
||||
self.assertEqual(companion["components"]["ota_generic_source_buffers"],
|
||||
without_eth["components"]["ota_generic_source_buffers"])
|
||||
|
||||
def test_companion_rak_worker_budget_covers_legacy_and_stack_overrides(self):
|
||||
flags = {"ETHERNET_ENABLED": 1, "ETHERNET_USE_RAK13800": 1}
|
||||
base = ram.requirements("NRF52_PLATFORM", {}, "RAK_4631_companion_radio_ethernet")
|
||||
for words in (1024, 2048, 8192):
|
||||
with self.subTest(words=words):
|
||||
worker = ram.requirements("NRF52_PLATFORM", {
|
||||
**flags, "RAK4631_ETHERNET_TASK_STACK_WORDS": words},
|
||||
"RAK_4631_companion_radio_ethernet")
|
||||
expected = words * 4 + 256
|
||||
self.assertEqual(worker["required_heap_bytes"] - base["required_heap_bytes"], expected)
|
||||
self.assertEqual(worker["components"]["ethernet_companion_worker"], expected)
|
||||
self.assertGreaterEqual(worker["required_contiguous_bytes"], expected)
|
||||
for words in (0, 1023, 65536):
|
||||
with self.subTest(words=words), self.assertRaisesRegex(ValueError, "1024..65535"):
|
||||
ram.requirements("NRF52_PLATFORM", {
|
||||
**flags, "RAK4631_ETHERNET_TASK_STACK_WORDS": words},
|
||||
"RAK_4631_companion_radio_ethernet")
|
||||
for platform, defines, target in (
|
||||
("ESP32_PLATFORM", flags, "esp32_companion_radio_full"),
|
||||
("NRF52_PLATFORM", {"ETHERNET_ENABLED": 1}, "other_companion"),
|
||||
("NRF52_PLATFORM", flags, "RAK_4631_repeater")):
|
||||
with self.subTest(platform=platform, defines=defines, target=target):
|
||||
self.assertNotIn("ethernet_companion_worker",
|
||||
ram.requirements(platform, defines, target)["components"])
|
||||
|
||||
def test_only_rak_sd_full_companion_counts_large_name_lookup(self):
|
||||
flags = {"ENABLE_OTA": 1, "OTA_SD_STORE": 1, "OTA_RAK15002_SD_STORE": 1,
|
||||
|
||||
@@ -7,12 +7,14 @@ import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import zlib
|
||||
from collections import Counter
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path[:0] = [str(ROOT / "tools" / "mota"), str(ROOT / "scripts")]
|
||||
import gen_targets
|
||||
from test_serial_wifi_sessions import SANITIZER_FLAGS
|
||||
|
||||
HEADER = ROOT / "src" / "helpers" / "ota" / "OtaTargets.h"
|
||||
HARNESS = ROOT / "test" / "fixtures" / "ota_target_names" / "test.cpp"
|
||||
@@ -28,26 +30,50 @@ class OtaTargetNameTest(unittest.TestCase):
|
||||
def compile_and_lookup(self, flags, ids, header=None):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
executable = Path(temp_dir) / "target_names"
|
||||
if header is not None:
|
||||
alternate = Path(temp_dir) / "helpers/ota"
|
||||
alternate.mkdir(parents=True)
|
||||
(alternate / "OtaTargets.h").write_text(header, encoding="utf-8")
|
||||
(alternate / "tinf").mkdir()
|
||||
(alternate / "tinf/tinf.h").write_text(
|
||||
(ROOT / "src/helpers/ota/tinf/tinf.h").read_text(), encoding="utf-8")
|
||||
# Keep production SHA-256 and tinf implementations. Instrument
|
||||
# only their call boundaries to assert candidate filtering,
|
||||
# one decode per chunk, one bounded allocation and cleanup.
|
||||
header = self.text if header is None else header
|
||||
rows = [(int(target, 16), name) for target, name in re.findall(
|
||||
r'\{ 0x([0-9a-fA-F]{8}), "([^"]+)" \},', header)]
|
||||
fingerprints = Counter(target & 255 for target, _ in rows)
|
||||
instrumented = header.replace("mesh::Utils::sha256(", "target_name_sha256(")
|
||||
instrumented = instrumented.replace("tinf_uncompress_exact(", "target_name_uncompress_exact(")
|
||||
instrumented = instrumented.replace("malloc(DECODE_CHUNK_BYTES)", "target_name_malloc(DECODE_CHUNK_BYTES)")
|
||||
instrumented = instrumented.replace("free(bytes)", "target_name_free(bytes)")
|
||||
alternate = Path(temp_dir) / "helpers/ota"
|
||||
alternate.mkdir(parents=True)
|
||||
(alternate / "OtaTargets.h").write_text(instrumented, encoding="ascii")
|
||||
(alternate / "tinf").mkdir()
|
||||
(alternate / "tinf/tinf.h").write_text(
|
||||
(ROOT / "src/helpers/ota/tinf/tinf.h").read_text(), encoding="utf-8")
|
||||
candidates = [Path(os.environ["MESHCORE_CRYPTO_DIR"])] if os.environ.get("MESHCORE_CRYPTO_DIR") else []
|
||||
candidates += sorted((ROOT / ".pio/libdeps").glob("*/Crypto"))
|
||||
crypto = next((path for path in candidates if (path / "SHA256.cpp").is_file()), None)
|
||||
self.assertIsNotNone(crypto, "Install rweather/Crypto or set MESHCORE_CRYPTO_DIR (no hash mock fallback)")
|
||||
decoder = Path(temp_dir) / "tinf.o"
|
||||
subprocess.run([
|
||||
built = subprocess.run([
|
||||
os.environ.get("CC") or shutil.which("cc") or "gcc",
|
||||
"-Os", "-DMESHCORE_TINF_IMPLEMENTATION=1", "-c",
|
||||
"-Os", *SANITIZER_FLAGS, "-DMESHCORE_TINF_IMPLEMENTATION=1", "-c",
|
||||
str(ROOT / "src/helpers/ota/tinf/tinflate.c"), "-o", str(decoder),
|
||||
], check=True)
|
||||
subprocess.run([
|
||||
"c++", "-std=c++11", "-O2", "-Wall", "-Wextra", "-Werror",
|
||||
*flags, f"-I{temp_dir}", f"-I{ROOT / 'src'}", str(HARNESS), str(decoder), "-o", str(executable),
|
||||
], check=True)
|
||||
], capture_output=True, text=True, timeout=60)
|
||||
self.assertEqual(built.returncode, 0, built.stdout + built.stderr)
|
||||
built = subprocess.run([
|
||||
os.environ.get("CXX") or shutil.which("g++") or "c++",
|
||||
"-std=c++17", "-O1", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter", "-DHOST_BUILD",
|
||||
"-ffunction-sections", "-fdata-sections", "-Wl,--gc-sections", *SANITIZER_FLAGS,
|
||||
*flags, f"-DOTA_TEST_TARGET_COUNT={len(rows)}", f"-I{temp_dir}", f"-I{ROOT / 'src'}", f"-I{crypto}",
|
||||
"-isystem", str(ROOT / "test/mocks"),
|
||||
str(HARNESS), str(ROOT / "src/Utils.cpp"),
|
||||
*[str(crypto / name) for name in ("SHA256.cpp", "Hash.cpp", "Crypto.cpp",
|
||||
"AES128.cpp", "AESCommon.cpp", "BlockCipher.cpp")],
|
||||
str(decoder), "-o", str(executable),
|
||||
], capture_output=True, text=True, timeout=60)
|
||||
self.assertEqual(built.returncode, 0, built.stdout + built.stderr)
|
||||
result = subprocess.run(
|
||||
[str(executable)], input="\n".join(f"{target:08x}" for target in ids),
|
||||
text=True, capture_output=True, check=True)
|
||||
[str(executable)], input="\n".join(f"{target:08x} {fingerprints[target & 255]}" for target in ids),
|
||||
text=True, capture_output=True, timeout=30)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
return result.stdout.splitlines()
|
||||
|
||||
def test_generator_reproduces_checked_in_header_and_all_ids(self):
|
||||
@@ -105,8 +131,29 @@ class OtaTargetNameTest(unittest.TestCase):
|
||||
self.assertLessEqual(max(lengths), 4096)
|
||||
self.assertEqual(firsts[-1], len(self.rows))
|
||||
self.assertEqual(offsets[-1], len(data))
|
||||
no_heap = '#include <stdlib.h>\n#define malloc(bytes) nullptr\n' + self.text
|
||||
self.assertEqual(self.compile_and_lookup(flags, [self.rows[0][0], 0], no_heap), ["?", "?"])
|
||||
self.assertEqual(self.compile_and_lookup(flags + ["-DOTA_TEST_MALLOC_FAIL=1"],
|
||||
[self.rows[0][0], 0]), ["?", "?"])
|
||||
|
||||
def test_rak_full_fingerprint_collisions_verify_the_entire_target_id(self):
|
||||
flags = ["-DOTA_TARGET_NAME_FRONT_CODED=1", "-DOTA_RAK15002_SD_STORE=1", "-DCOMPANION_RADIO_FULL=1"]
|
||||
known = {target for target, _ in self.rows}
|
||||
buckets = Counter(target & 255 for target in known)
|
||||
self.assertGreater(max(buckets.values()), 1)
|
||||
# Preserve every real target's low-byte fingerprint while changing
|
||||
# another ID byte. No filtered candidate may be accepted by itself.
|
||||
unknown = {target ^ 0x100 for target in known} - known
|
||||
# Also cover every bucket, including buckets containing no targets.
|
||||
unknown.update(0xa5a50000 | fingerprint for fingerprint in range(256))
|
||||
unknown.difference_update(known)
|
||||
self.assertGreater(len(unknown), len(self.rows))
|
||||
self.assertEqual(self.compile_and_lookup(flags, sorted(unknown)), ["?"] * len(unknown))
|
||||
|
||||
def test_rak_full_index_keeps_one_byte_per_target_without_changing_names(self):
|
||||
encoded = re.search(r"static const uint8_t FINGERPRINTS\[\] = \{(.*?)\n \};", self.text, re.S)
|
||||
self.assertIsNotNone(encoded)
|
||||
fingerprints = [int(value, 16) for value in re.findall(r"0x([0-9a-f]{2})", encoded[1])]
|
||||
self.assertEqual(fingerprints, [target & 255 for target, _ in self.rows])
|
||||
self.assertGreater(4 * len(self.rows) - len(fingerprints), 2000)
|
||||
|
||||
def test_table_disabled_preserves_optional_local_name(self):
|
||||
local_id, local_name = self.rows[0]
|
||||
@@ -122,7 +169,8 @@ class OtaTargetNameTest(unittest.TestCase):
|
||||
|
||||
def test_cpp_decoder_fails_closed_on_invalid_records_and_trailing_bytes(self):
|
||||
from zopfli_compress import raw_deflate_compress
|
||||
rows = [(1, "hello")]
|
||||
rows = gen_targets.target_rows(["hello"])
|
||||
target = rows[0][0]
|
||||
header = gen_targets.generate_header(rows)
|
||||
original, _, _, _ = gen_targets.compressed_name_chunks(rows)
|
||||
invalid = [
|
||||
@@ -143,7 +191,7 @@ class OtaTargetNameTest(unittest.TestCase):
|
||||
with self.subTest(stream=stream.hex()):
|
||||
for flags in (["-DOTA_TARGET_NAME_FRONT_CODED=1"],
|
||||
["-DOTA_TARGET_NAME_FRONT_CODED=1", "-DOTA_RAK15002_SD_STORE=1", "-DCOMPANION_RADIO_FULL=1"]):
|
||||
self.assertEqual(self.compile_and_lookup(flags, [1], altered), ["?"])
|
||||
self.assertEqual(self.compile_and_lookup(flags, [target], altered), ["?"])
|
||||
|
||||
def test_generator_rejects_unencodable_names(self):
|
||||
for name in ("x\0y", "x" * 256):
|
||||
|
||||
@@ -8,7 +8,7 @@ import tempfile
|
||||
import unittest
|
||||
|
||||
from test_replay_reset_integration import extract_braced
|
||||
from test_ethernet_terminal_shutdown import SYNC
|
||||
from test_rak_companion_ethernet_worker import build_worker
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
FLAGS = ["NRF52_PLATFORM", "RAK_4631", "OTA_SD_STORE", "OTA_RAK15002_SD_STORE",
|
||||
@@ -400,7 +400,13 @@ int main(){
|
||||
defines=FLAGS + ["OTA_SEEDER_ONLY"])
|
||||
|
||||
def test_actual_companion_eth_is_opt_in_and_busy_handoff_never_touches_spi(self):
|
||||
self._run_companion_eth_probe()
|
||||
with tempfile.TemporaryDirectory(prefix="rak-companion-shared-spi-") as folder:
|
||||
binary = build_worker(self, folder)
|
||||
for scenario in (0, 11, 12, 13, 15):
|
||||
with self.subTest(scenario=scenario):
|
||||
result = subprocess.run([str(binary), str(scenario)], text=True,
|
||||
capture_output=True, timeout=10)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
|
||||
def test_companion_eth_cs_reacquisition_negative_controls(self):
|
||||
for mutation in ("missing_cs_direction", "missing_cs_preload"):
|
||||
@@ -408,86 +414,22 @@ int main(){
|
||||
self._run_companion_eth_probe(mutation)
|
||||
|
||||
def _run_companion_eth_probe(self, mutation=None):
|
||||
stem = ROOT / "src/helpers/ethernet/RAK13800/RAK13800EthernetInterface"
|
||||
cls = extract_braced(stem.with_suffix(".h").read_text(), "class RAK13800EthernetInterface") + ";"
|
||||
cpp = stem.with_suffix(".cpp").read_text()
|
||||
cpp = (ROOT / "src/helpers/ethernet/RAK13800/RAK13800EthernetInterface.cpp").read_text()
|
||||
if mutation:
|
||||
statement = (" pinMode(PIN_ETHERNET_SS, OUTPUT);" if mutation == "missing_cs_direction"
|
||||
else " digitalWrite(PIN_ETHERNET_SS, HIGH);")
|
||||
startup = extract_braced(cpp, "bool RAK13800EthernetInterface::beginHardware()")
|
||||
statement = (" digitalWrite(PIN_ETHERNET_SS, HIGH);\n"
|
||||
" pinMode(PIN_ETHERNET_SS, OUTPUT);")
|
||||
removed = (" pinMode(PIN_ETHERNET_SS, OUTPUT);" if mutation == "missing_cs_direction"
|
||||
else " digitalWrite(PIN_ETHERNET_SS, HIGH);")
|
||||
startup = extract_braced(cpp, "void RAK13800EthernetInterface::runWorker()")
|
||||
self.assertEqual(startup.count(statement), 1)
|
||||
cpp = cpp.replace(startup, startup.replace(statement, ""))
|
||||
signatures = ("bool RAK13800EthernetInterface::begin()", "bool RAK13800EthernetInterface::beginHardware()",
|
||||
"bool RAK13800EthernetInterface::tryEnable()", "bool RAK13800EthernetInterface::tryDisable()",
|
||||
"bool RAK13800EthernetInterface::terminalShutdown(", "bool RAK13800EthernetInterface::shutdownForPowerOff(",
|
||||
"void RAK13800EthernetInterface::disconnectClient()", "bool RAK13800EthernetInterface::isConnected() const",
|
||||
"int RAK13800EthernetInterface::available()", "int RAK13800EthernetInterface::read()",
|
||||
"size_t RAK13800EthernetInterface::write(", "void RAK13800EthernetInterface::loop()")
|
||||
methods = "\n".join(extract_braced(cpp, name) for name in signatures)
|
||||
methods = methods.replace("uint16_t remotePort = newClient.remotePort();", "(void)newClient.remotePort();")
|
||||
source = SYNC.split("int main(", 1)[0]
|
||||
source = source.replace("#include <Arduino.h>", "#include <Arduino.h>\n#include <helpers/ota/OtaSdSpiBusNrf52.h>")
|
||||
source = source.replace("void pinMode(int,int mode){if(mode==INPUT){assert(!spi);++pads;}}", "")
|
||||
source = source.replace("void digitalWrite(int,int){}", "")
|
||||
source = source.replace("struct SPIClass {void begin(){assert(!spi);spi=true;++begins;} void end(){assert(spi);spi=false;++ends;}} ETHERNET_SPI_PORT;",
|
||||
"struct EthSpi{void begin(){assert(!spi);spi=true;++begins;}void end(){assert(spi);spi=false;++ends;}} ETHERNET_SPI_PORT;")
|
||||
source = source.replace("void init(SPIClass&,int)", "void init(EthSpi&,int)")
|
||||
# The pinned W5100 init returns early after its first success, so
|
||||
# only that first call configures CS. GPIO writes do not restore an
|
||||
# input's output direction on nRF52; each acquisition must do so.
|
||||
source = source.replace("int begin(uint8_t*){return result;}",
|
||||
"int begin(uint8_t*){static bool initialized=false;"
|
||||
"if(!initialized){pinMode(26,OUTPUT);digitalWrite(26,HIGH);initialized=true;}"
|
||||
"assert(mock_pin_modes[26]==OUTPUT && mock_pin_values[26]==HIGH);return result;}")
|
||||
source = source.replace("void disable(){disconnectClient();++cancels;}", "virtual void enable(){}\n virtual void disable(){disconnectClient();++cancels;}")
|
||||
source = source.replace("@CLASS@", cls).replace("@METHODS@", methods)
|
||||
source = source.replace("assert(spi);++closes;", "assert(spi&&!inserted);++closes;")
|
||||
source += r'''
|
||||
using namespace mesh::ota;
|
||||
static bool busy=false,release_busy=false;
|
||||
static unsigned prepared=0,released=0;
|
||||
bool prepare(void*){++prepared;return !busy;}
|
||||
bool canRelease(void*){return !release_busy;}
|
||||
bool release(void*){assert(!spi&&!rak4631_ethernet_owns_spi());++released;return true;}
|
||||
int main(){
|
||||
RAK13800EthernetInterface owner;assert(owner.begin());owner.enable();
|
||||
assert(!owner.hardwareActive()&&!spi&&!begins&&!owner.tryEnable());
|
||||
assert(mock_cs_driven==0);
|
||||
owner.setSpiOwnershipHooks(prepare,canRelease,release,nullptr);
|
||||
inserted=true;assert(!owner.tryEnable()&&!prepared&&!begins);
|
||||
inserted=false;busy=true;assert(!owner.tryEnable()&&prepared==1&&!begins);
|
||||
busy=false;assert(rak4631_try_acquire_spi(RakSharedSpiOwner::Sd));
|
||||
assert(!owner.tryEnable()&&!begins&&rak4631_spi_owner()==RakSharedSpiOwner::Sd);
|
||||
assert(mock_cs_driven==0); // Rejected admission must never drive shared CS.
|
||||
rak4631_release_spi(RakSharedSpiOwner::Sd);
|
||||
assert(owner.tryEnable()&&owner.hardwareActive()&&rak4631_ethernet_owns_spi());
|
||||
release_busy=true;assert(!owner.tryDisable()&&owner.hardwareActive()&&ends==0);
|
||||
release_busy=false;assert(owner.tryDisable()&&!owner.hardwareActive()&&ends==1&&closes==4);
|
||||
assert(mock_pin_modes[26]==INPUT);
|
||||
assert(!rak4631_ethernet_owns_spi()&&released==2);
|
||||
Ethernet.result=0;assert(!owner.tryEnable()&&!owner.hardwareActive()&&ends==2&&!rak4631_ethernet_owns_spi());
|
||||
Ethernet.result=1;assert(owner.tryEnable());inserted=true;const auto calls=reads;
|
||||
assert(owner.available()==0&&owner.read()==-1&&owner.write(nullptr,0)==0&&reads==calls);
|
||||
owner.loop();assert(!owner.hardwareActive()&&rak4631_ethernet_owns_spi()&&reads==calls);
|
||||
assert(!owner.tryEnable());assert(owner.tryDisable()&&!rak4631_ethernet_owns_spi());
|
||||
assert(mock_pin_modes[26]==INPUT);
|
||||
inserted=false;assert(owner.tryEnable()&&owner.hardwareActive());
|
||||
assert(mock_pin_modes[26]==OUTPUT); // Also recover after SD insertion and removal.
|
||||
assert(owner.tryDisable()&&mock_pin_modes[26]==INPUT);
|
||||
}
|
||||
'''
|
||||
# The existing fixture's pad counter is irrelevant to the new mapped
|
||||
# GPIO mock; ownership/SPI/socket traffic remains production code.
|
||||
source = source.replace("ends=0,pads=0,cancels", "ends=0,cancels")
|
||||
spi = SPI.replace("inline void pinMode(int pin,int){touched.push_back(pin);}",
|
||||
"inline int mock_pin_modes[64]={},mock_pin_values[64]={};\n"
|
||||
"inline unsigned mock_cs_driven=0;\n"
|
||||
"inline void pinMode(int pin,int mode){"
|
||||
"if(pin==26&&mode==OUTPUT){assert(mock_pin_values[pin]==HIGH);++mock_cs_driven;}"
|
||||
"mock_pin_modes[pin]=mode;touched.push_back(pin);}")
|
||||
spi = spi.replace("inline void digitalWrite(int pin,int){touched.push_back(pin);}",
|
||||
"inline void digitalWrite(int pin,int value){mock_pin_values[pin]=value;touched.push_back(pin);}")
|
||||
run_native(self, source, {"SPI.h": spi}, negative=mutation is not None)
|
||||
cpp = cpp.replace(startup, startup.replace(statement, statement.replace(removed, "")))
|
||||
with tempfile.TemporaryDirectory(prefix="rak-companion-cs-negative-") as folder:
|
||||
binary = build_worker(self, folder, cpp)
|
||||
result = subprocess.run([str(binary), "13"], text=True,
|
||||
capture_output=True, timeout=10)
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertIn("CS must be preloaded" if mutation == "missing_cs_preload"
|
||||
else "pin_modes[26].load() == OUTPUT", result.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
"""Run the actual RAK Companion driver, framing and routes against a fake RTOS."""
|
||||
|
||||
from pathlib import Path
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from test_replay_reset_integration import extract_braced
|
||||
from test_serial_wifi_sessions import SANITIZER_FLAGS
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
FIXTURE = ROOT / "test/fixtures/rak_companion_ethernet_worker"
|
||||
SCENARIOS = (
|
||||
"admission and task allocation failures",
|
||||
"blocked initial DHCP and cooperative OFF",
|
||||
"OFF before worker scheduling",
|
||||
"framed short writes, backpressure and routing",
|
||||
"blocked renewal preserves session, replies and parser",
|
||||
"stalled listener command is bounded and preserves cached session",
|
||||
"same-IP replacement replays complete response",
|
||||
"different-IP replacement cancels reply ownership",
|
||||
"missing TCP acknowledgement",
|
||||
"stuck SEND controller command",
|
||||
"stuck RECV controller command",
|
||||
"SD insertion during DHCP and reacquisition",
|
||||
"online SD insertion and pinned release",
|
||||
"repeated acquisitions reclaim tasks and restore CS",
|
||||
"terminal timeout retains blocked worker",
|
||||
"release hook failure retains SPI ownership",
|
||||
"missing Ethernet hardware",
|
||||
"OFF wakes DHCP retry promptly",
|
||||
"occupied OTA owner refuses OFF without cancelling session",
|
||||
"failed ACK retains complete response for same-IP reconnect",
|
||||
)
|
||||
FLAGS = (
|
||||
"NRF52_PLATFORM", "RAK_4631", "OTA_SD_STORE", "OTA_RAK15002_SD_STORE",
|
||||
"SPI_INTERFACES_COUNT=1", "PIN_SPI_MISO=29", "PIN_SPI_SCK=3", "PIN_SPI_MOSI=30",
|
||||
"WB_IO6=10", "ETHERNET_ENABLED=1", "ETHERNET_RAW_LINE=0",
|
||||
)
|
||||
|
||||
|
||||
def build_worker(test, folder, production=None, *, adaptive=True, static_ip=False, rak3401=False, whitebox=False):
|
||||
compiler = os.environ.get("CXX") or shutil.which("g++") or shutil.which("clang++")
|
||||
if compiler is None:
|
||||
test.skipTest("a host C++17 compiler is required")
|
||||
driver = ROOT / "src/helpers/ethernet/RAK13800/RAK13800EthernetInterface.cpp"
|
||||
binary = Path(folder) / "worker"
|
||||
if production is None:
|
||||
source = str(driver)
|
||||
stdin = None
|
||||
else:
|
||||
# Keep the production include's relative helper paths intact while
|
||||
# compiling a deliberate behavioral mutation from stdin.
|
||||
source = "-"
|
||||
stdin = production
|
||||
flags = list(FLAGS)
|
||||
if not adaptive:
|
||||
flags = [item for item in flags if item not in ("OTA_SD_STORE", "OTA_RAK15002_SD_STORE")]
|
||||
if rak3401:
|
||||
flags = [item for item in flags if item != "RAK_4631"] + ["RAK_3401"]
|
||||
if static_ip:
|
||||
flags.extend(("ETHERNET_STATIC_IP=192,168,1,42", "ETHERNET_STATIC_DNS=8,8,8,8",
|
||||
"ETHERNET_STATIC_GATEWAY=192,168,1,1", "ETHERNET_STATIC_SUBNET=255,255,255,0"))
|
||||
access_flags = ["-fno-access-control", "-DMESH_TEST_PRIVATE_ACCESS=1"] if whitebox else []
|
||||
result = subprocess.run([
|
||||
compiler, "-std=c++17", "-pthread", "-Wall", "-Wextra", "-Werror",
|
||||
*SANITIZER_FLAGS, *access_flags, *["-D" + item for item in flags],
|
||||
f"-I{FIXTURE / 'mocks'}", f"-I{ROOT / 'src'}", f"-I{driver.parent}",
|
||||
str(FIXTURE / "test.cpp"),
|
||||
str(ROOT / "src/helpers/ethernet/SerialEthernetInterface.cpp"),
|
||||
"-x", "c++", source, "-o", str(binary),
|
||||
], input=stdin, text=True, capture_output=True, timeout=60)
|
||||
test.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
return binary
|
||||
|
||||
|
||||
class RakCompanionEthernetWorkerTests(unittest.TestCase):
|
||||
def test_active_worker_publication_gaps_refuse_restart(self):
|
||||
with tempfile.TemporaryDirectory(prefix="meshcore-companion-ethernet-publication-") as folder:
|
||||
# Access control is relaxed only to place the actual atomic fields
|
||||
# in the two publication windows while the fake task is held at
|
||||
# its real entry barrier. Production methods and layout are intact.
|
||||
binary = build_worker(self, folder, whitebox=True)
|
||||
result = subprocess.run([str(binary), "34"], text=True, capture_output=True, timeout=10)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
def test_dedicated_ethernet_boot_and_static_ip(self):
|
||||
for adaptive, static_ip, scenario in ((False, False, 20), (False, True, 20), (True, True, 3)):
|
||||
with self.subTest(adaptive=adaptive, static_ip=static_ip), tempfile.TemporaryDirectory(
|
||||
prefix="meshcore-companion-ethernet-legacy-") as folder:
|
||||
binary = build_worker(self, folder, adaptive=adaptive, static_ip=static_ip)
|
||||
result = subprocess.run([str(binary), str(scenario)], text=True,
|
||||
capture_output=True, timeout=10)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
self.assertIn(f"PASS: RAK Companion Ethernet worker scenario {scenario}", result.stdout)
|
||||
|
||||
def test_actual_companion_worker_with_framing_and_routes(self):
|
||||
with tempfile.TemporaryDirectory(prefix="meshcore-companion-ethernet-") as folder:
|
||||
binary = build_worker(self, folder)
|
||||
for scenario, description in enumerate(SCENARIOS):
|
||||
with self.subTest(scenario=description):
|
||||
result = subprocess.run([str(binary), str(scenario)], text=True,
|
||||
capture_output=True, timeout=10)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
self.assertIn(f"PASS: RAK Companion Ethernet worker scenario {scenario}", result.stdout)
|
||||
for scenario, description in ((21, "missing hooks"), (28, "wrong OPEN state"),
|
||||
(29, "fast SYNRECV"), (30, "wrong LISTEN state"),
|
||||
(31, "closing socket fallback"), (32, "wrong CLOSE state"),
|
||||
(33, "direct restart cancels stopped-fault session"),
|
||||
(35, "unACKed head survives priority insertion and full queue"),
|
||||
(36, "priority queue survives unACKed head reconnect"),
|
||||
(37, "ACKed but unretired head remains pinned")):
|
||||
with self.subTest(scenario=description):
|
||||
result = subprocess.run([str(binary), str(scenario)], text=True, capture_output=True, timeout=10)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
|
||||
def test_synchronous_start_and_busy_disconnect_negative_controls(self):
|
||||
driver = ROOT / "src/helpers/ethernet/RAK13800/RAK13800EthernetInterface.cpp"
|
||||
source = driver.read_text()
|
||||
startup = extract_braced(source, "bool RAK13800EthernetInterface::beginHardware()")
|
||||
statement = (" const BaseType_t created = xTaskCreate(workerTask, \"eth_comp\", RAK4631_ETHERNET_TASK_STACK_WORDS,\n"
|
||||
" this, 1, &_task);")
|
||||
self.assertEqual(startup.count(statement), 1)
|
||||
synchronous = source.replace(startup, startup.replace(statement,
|
||||
" const BaseType_t created = (workerTask(this), pdPASS);"))
|
||||
connected = extract_braced(source, "bool RAK13800EthernetInterface::isConnected() const")
|
||||
statement = "return isEnabled() && _isConnected && !_storageFault.load();"
|
||||
self.assertEqual(connected.count(statement), 1)
|
||||
false_eof = source.replace(connected, connected.replace(statement,
|
||||
"return isEnabled() && _isConnected && !_storageFault.load() && _access.load() == Access::Idle;"))
|
||||
commands = extract_braced(source, "bool RAK13800EthernetInterface::commandsIdle()")
|
||||
status_guard = extract_braced(commands, "if (idle && _listenerCommand)")
|
||||
unchecked_status = source.replace(commands, commands.replace(status_guard, "_listenerCommand = 0;"))
|
||||
for name, mutation, scenario, error in (
|
||||
("synchronous startup", synchronous, 1, "SPI startup entered the mesh thread"),
|
||||
("busy false EOF", false_eof, 4, "f.owner.isConnected()"),
|
||||
("unchecked listener state", unchecked_status, 28, "wrong listener status must fault once")):
|
||||
with self.subTest(mutation=name), tempfile.TemporaryDirectory(
|
||||
prefix="meshcore-companion-ethernet-negative-") as folder:
|
||||
binary = build_worker(self, folder, mutation)
|
||||
result = subprocess.run([str(binary), str(scenario)], text=True,
|
||||
capture_output=True, timeout=10)
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertIn(error, result.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -147,10 +147,12 @@ class RakSdContextHandoffTests(unittest.TestCase):
|
||||
struct Board {} board;
|
||||
struct Ethernet {
|
||||
bool active = false, allowed = true;
|
||||
const char* state = "off";
|
||||
unsigned enables = 0, disables = 0;
|
||||
bool tryEnable() { ++enables; if (!allowed) return false; active = true; return true; }
|
||||
bool tryDisable() { ++disables; if (!allowed) return false; active = false; return true; }
|
||||
bool tryEnable() { ++enables; if (!allowed) return false; active = true; state = "starting"; return true; }
|
||||
bool tryDisable() { ++disables; if (!allowed) return false; state = "stopping"; return true; }
|
||||
bool hardwareActive() const { return active; }
|
||||
const char* status() const { return state; }
|
||||
const char* last_error() const { return "storage busy"; }
|
||||
} ethernet_interface;
|
||||
namespace mesh { namespace ota {
|
||||
@@ -183,10 +185,19 @@ int main() {
|
||||
assert(strstr(reply, "storage busy") && !ethernet_interface.active);
|
||||
ethernet_interface.allowed = true;
|
||||
assert(handleRak4631PeripheralCommand("set eth on", reply, sizeof reply));
|
||||
assert(strstr(reply, "eth=on") && ethernet_interface.active);
|
||||
assert(strstr(reply, "eth=starting") && ethernet_interface.active);
|
||||
ethernet_interface.state = "online";
|
||||
assert(handleRak4631PeripheralCommand("get eth", reply, sizeof reply));
|
||||
assert(strstr(reply, "eth=online"));
|
||||
assert(handleRak4631PeripheralCommand("get sdcard free", reply, sizeof reply));
|
||||
assert(strstr(reply, "Ethernet off") && !mesh::ota::acquires);
|
||||
assert(handleRak4631PeripheralCommand("set eth off", reply, sizeof reply));
|
||||
assert(strstr(reply, "eth=stopping") && ethernet_interface.active);
|
||||
assert(handleRak4631PeripheralCommand("get sdcard", reply, sizeof reply));
|
||||
assert(strstr(reply, "Ethernet off") && !mesh::ota::acquires);
|
||||
ethernet_interface.active = false; ethernet_interface.state = "off";
|
||||
assert(handleRak4631PeripheralCommand("get eth", reply, sizeof reply));
|
||||
assert(strstr(reply, "eth=off"));
|
||||
assert(!ethernet_interface.active);
|
||||
mesh::ota::admission = false;
|
||||
assert(handleRak4631PeripheralCommand("get sdcard", reply, sizeof reply));
|
||||
|
||||
@@ -32,6 +32,20 @@ SCENARIOS = (
|
||||
"fragmented maximum frame and next command parse intact",
|
||||
"incomplete command deadline survives millis rollover",
|
||||
"null output never dispatches or dereferences a command",
|
||||
"worker ownership blocks socket IO but preserves logical session and queue",
|
||||
"worker pause retains only remaining framed input deadline",
|
||||
"worker pause cannot revive an already expired frame",
|
||||
"worker pause and remaining deadline survive millis rollover",
|
||||
"disconnect resets worker pause while replaying whole same-IP backlog",
|
||||
"disable resets worker pause and clears backlog",
|
||||
"same-IP replacement resets partial input and pause while replaying backlog",
|
||||
"different-IP replacement resets partial input pause and backlog",
|
||||
"worker pause beginning at zero preserves remaining deadline",
|
||||
"worker ownership without partial input cannot extend next frame deadline",
|
||||
"write command ownership pauses input deadline immediately",
|
||||
"read command ownership pauses before polling the socket again",
|
||||
"availability command ownership prevents a following socket read",
|
||||
"main-loop ownership service preserves timeout behind another locked route",
|
||||
)
|
||||
|
||||
|
||||
@@ -67,12 +81,25 @@ class SerialEthernetSessionTests(unittest.TestCase):
|
||||
name = driver.split("/")[-1]
|
||||
loop = extract_braced(source, f"void {name}::loop()")
|
||||
with self.subTest(driver=name):
|
||||
self.assertLess(loop.index("disconnectClient();"), loop.index("onClientConnected("))
|
||||
self.assertLess(loop.index("onClientConnected((uint32_t)remoteIp);"),
|
||||
loop.index("client = newClient;"))
|
||||
close = extract_braced(source, f"void {name}::disconnectClient()")
|
||||
self.assertIn("_isConnected = false;", close)
|
||||
self.assertIn("client.stop();", close)
|
||||
if name == "RAK13800EthernetInterface":
|
||||
# The worker owns the physical socket until main commits
|
||||
# the session transition and acknowledges its event. The
|
||||
# logical route must remain unavailable during callbacks.
|
||||
self.assertLess(loop.index("_isConnected = false;"),
|
||||
loop.index("onClientConnected("))
|
||||
self.assertLess(loop.index("onClientConnected("),
|
||||
loop.index("_isConnected = true;"))
|
||||
self.assertLess(loop.index("_isConnected = true;"),
|
||||
loop.index("_eventPending.store(false);"))
|
||||
self.assertIn("_closeRequested.store(true);", close)
|
||||
self.assertNotIn("client.stop();", close)
|
||||
else:
|
||||
self.assertLess(loop.index("disconnectClient();"), loop.index("onClientConnected("))
|
||||
self.assertLess(loop.index("onClientConnected((uint32_t)remoteIp);"),
|
||||
loop.index("client = newClient;"))
|
||||
self.assertIn("client.stop();", close)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
+56
-10
@@ -167,6 +167,7 @@ def generate_header(rows):
|
||||
' #include "tinf/tinf.h"',
|
||||
" #if defined(OTA_RAK15002_SD_STORE) && defined(COMPANION_RADIO_FULL)",
|
||||
" #include <stdlib.h>",
|
||||
' #include "Utils.h"',
|
||||
" #endif",
|
||||
"#endif",
|
||||
"",
|
||||
@@ -180,26 +181,70 @@ def generate_header(rows):
|
||||
"inline const char* ota_target_env_name(uint32_t target_id, char* buffer, size_t capacity) {",
|
||||
" if (!buffer || !capacity) return nullptr;",
|
||||
" buffer[0] = 0;",
|
||||
"#if defined(OTA_RAK15002_SD_STORE) && defined(COMPANION_RADIO_FULL)",
|
||||
" // Fingerprints filter candidates only. Reconstructed names must match the entire",
|
||||
" // existing SHA-256:4 target ID, so collisions never produce a wrong target name.",
|
||||
" static const uint8_t FINGERPRINTS[] = {",
|
||||
]
|
||||
out += [" " + ", ".join(f"0x{tid & 0xff:02x}" for tid, _ in rows[i:i + 16]) + ","
|
||||
for i in range(0, len(rows), 16)]
|
||||
out += [" };"] + chunk_tables(RAK_FULL_NAME_DECODE_CHUNK_SIZE)
|
||||
out += [
|
||||
" // Rare display lookup: one heap buffer, each candidate chunk decoded once.",
|
||||
" // Keep complete names private until both ID and caller capacity are validated.",
|
||||
" struct Scratch { uint8_t* bytes; ~Scratch() { free(bytes); } } scratch = {nullptr};",
|
||||
" char name[OTA_TARGET_ENV_NAME_CAPACITY];",
|
||||
" const uint8_t fingerprint = (uint8_t)target_id;",
|
||||
" for (unsigned group = 0; group < sizeof(RAW_LENGTHS) / sizeof(RAW_LENGTHS[0]); group++) {",
|
||||
" unsigned candidate = FIRSTS[group];",
|
||||
" while (candidate < FIRSTS[group + 1] && FINGERPRINTS[candidate] != fingerprint) candidate++;",
|
||||
" if (candidate == FIRSTS[group + 1]) continue;",
|
||||
" if (!scratch.bytes) scratch.bytes = (uint8_t*)malloc(DECODE_CHUNK_BYTES);",
|
||||
" if (!scratch.bytes) return nullptr;",
|
||||
" uint8_t* raw = scratch.bytes;",
|
||||
" unsigned int produced = DECODE_CHUNK_BYTES;",
|
||||
" if (tinf_uncompress_exact(raw, &produced, NAMES + OFFSETS[group],",
|
||||
" OFFSETS[group + 1] - OFFSETS[group]) != TINF_OK ||",
|
||||
" produced != RAW_LENGTHS[group]) return nullptr;",
|
||||
" const uint8_t* record = raw;",
|
||||
" const uint8_t* end = raw + produced;",
|
||||
" size_t previous_length = 0;",
|
||||
" for (unsigned row = FIRSTS[group]; row < FIRSTS[group + 1]; row++) {",
|
||||
" if (record == end) return nullptr;",
|
||||
" size_t length = *record++;",
|
||||
" if (length > previous_length || length >= sizeof(name)) return nullptr;",
|
||||
" while (record < end && *record) {",
|
||||
" if (length + 1 >= sizeof(name)) return nullptr;",
|
||||
" name[length++] = (char)*record++;",
|
||||
" }",
|
||||
" if (record == end) return nullptr;",
|
||||
" record++;",
|
||||
" name[length] = 0;",
|
||||
" previous_length = length;",
|
||||
" if (FINGERPRINTS[row] != fingerprint) continue;",
|
||||
" uint8_t hash[4];",
|
||||
" mesh::Utils::sha256(hash, sizeof(hash), (const uint8_t*)name, length);",
|
||||
" const uint32_t id = (uint32_t)hash[0] | (uint32_t)hash[1] << 8 |",
|
||||
" (uint32_t)hash[2] << 16 | (uint32_t)hash[3] << 24;",
|
||||
" if (id != target_id) continue;",
|
||||
" if (length >= capacity) return nullptr;",
|
||||
" memcpy(buffer, name, length + 1);",
|
||||
" return buffer;",
|
||||
" }",
|
||||
" }",
|
||||
" return nullptr;",
|
||||
"#else",
|
||||
" static const uint32_t IDS[] = {",
|
||||
]
|
||||
out += [" " + ", ".join(f"0x{tid:08x}" for tid, _ in rows[i:i + 8]) + ","
|
||||
for i in range(0, len(rows), 8)]
|
||||
out += [" };", "#if defined(OTA_RAK15002_SD_STORE) && defined(COMPANION_RADIO_FULL)"]
|
||||
out += chunk_tables(RAK_FULL_NAME_DECODE_CHUNK_SIZE)
|
||||
out += ["#else"] + chunk_tables(NAME_DECODE_CHUNK_SIZE) + ["#endif"]
|
||||
out += [" };"] + chunk_tables(NAME_DECODE_CHUNK_SIZE)
|
||||
out += [
|
||||
" for (unsigned i = 0; i < sizeof(IDS) / sizeof(IDS[0]); i++) {",
|
||||
" if (IDS[i] != target_id) continue;",
|
||||
" unsigned group = 0;",
|
||||
" while (i >= FIRSTS[group + 1]) group++;",
|
||||
"#if defined(OTA_RAK15002_SD_STORE) && defined(COMPANION_RADIO_FULL)",
|
||||
" // Rare display lookup: larger chunks save flash without growing the loop stack.",
|
||||
" uint8_t* raw = (uint8_t*)malloc(DECODE_CHUNK_BYTES);",
|
||||
" if (!raw) return nullptr;",
|
||||
" struct Scratch { uint8_t* bytes; ~Scratch() { free(bytes); } } scratch = {raw};",
|
||||
"#else",
|
||||
" uint8_t raw[DECODE_CHUNK_BYTES];",
|
||||
"#endif",
|
||||
" unsigned int produced = DECODE_CHUNK_BYTES;",
|
||||
" if (tinf_uncompress_exact(raw, &produced, NAMES + OFFSETS[group],",
|
||||
" OFFSETS[group + 1] - OFFSETS[group]) != TINF_OK ||",
|
||||
@@ -224,6 +269,7 @@ def generate_header(rows):
|
||||
" return buffer;",
|
||||
" }",
|
||||
" return nullptr;",
|
||||
"#endif",
|
||||
"}",
|
||||
"#else",
|
||||
"// target_id -> env name, or nullptr if unknown. Linear scan; lookups are rare.",
|
||||
|
||||
Reference in new issue
Block a user