Fix USB logging ownership, persistence, and reconnect safety
This commit is contained in:
1 parent
c1a533572a
commit
91d533bfff
32 files changed
+1543
-140
No files matched your search
@@ -2,6 +2,7 @@
|
||||
#include <helpers/ClientACLCLI.h>
|
||||
#include <helpers/CLICommandUtils.h>
|
||||
#include <helpers/LazyPersistence.h>
|
||||
#include <algorithm>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
@@ -220,6 +221,11 @@ static void actual_role_dispatch_handles_radio_and_local() {
|
||||
CHECK(reply[0] == 0);
|
||||
CHECK(mesh::console.output.find("ACL:") == 0);
|
||||
CHECK(mesh::console.output.find(row(admin)) != std::string::npos);
|
||||
std::string local_output = mesh::console.output;
|
||||
local_output.erase(std::remove(local_output.begin(), local_output.end(), '\r'),
|
||||
local_output.end());
|
||||
CHECK(local_output == "ACL:\n" + row(admin) + "\n"
|
||||
+ row(acl.getClientByIdx(1)) + "\n");
|
||||
strcpy(command, "get acl");
|
||||
char paged[] = "get acl 1";
|
||||
mesh::console.output.clear();
|
||||
@@ -227,6 +233,27 @@ static void actual_role_dispatch_handles_radio_and_local() {
|
||||
CHECK(mesh::console.output.empty());
|
||||
CHECK(reply == expected);
|
||||
}
|
||||
|
||||
// Also exercise every role's local full-table listing, not just remote pages.
|
||||
// The real extracted helper must emit every active key, skip deleted entries,
|
||||
// and never include the secret fields stored alongside ACL public identities.
|
||||
ClientACL full;
|
||||
full.load(&fs, self);
|
||||
std::string expected_local = "ACL:\n";
|
||||
for (unsigned i = 0; i < MAX_CLIENTS; ++i) {
|
||||
auto* client = add(full, i, i % 7 == 0 ? 0 : PERM_ACL_ADMIN);
|
||||
if (client->permissions) expected_local += row(client) + "\n";
|
||||
}
|
||||
for (auto handler : {repeaterCommand, roomCommand, sensorCommand}) {
|
||||
char command[] = "get acl", reply[160] = {};
|
||||
mesh::console.output.clear();
|
||||
handler(full, nullptr, 0, command, reply);
|
||||
CHECK(reply[0] == 0);
|
||||
std::string local_output = mesh::console.output;
|
||||
local_output.erase(std::remove(local_output.begin(), local_output.end(), '\r'),
|
||||
local_output.end());
|
||||
CHECK(local_output == expected_local);
|
||||
}
|
||||
}
|
||||
|
||||
static void repeater_delegation_stays_denied() {
|
||||
|
||||
+11
@@ -25,6 +25,17 @@
|
||||
*/
|
||||
|
||||
// Pinned framework d541301: real power handler before the build-local fix.
|
||||
// The actual disconnect path is retained too: pull-up disable alone does NOT
|
||||
// produce a POWER removal edge, so local session recovery needs this event.
|
||||
void dcd_disconnect(uint8_t rhport) {
|
||||
(void) rhport;
|
||||
NRF_USBD->USBPULLUP = 0;
|
||||
|
||||
// Disable Pull-up does not trigger Power USB Removed, in fact it have no
|
||||
// impact on the USB Power status at all -> need to submit unplugged event to the stack.
|
||||
dcd_event_bus_signal(0, DCD_EVENT_UNPLUGGED, false);
|
||||
}
|
||||
|
||||
void tusb_hal_nrf_power_event(uint32_t event);
|
||||
void tusb_hal_nrf_power_event(uint32_t event) {
|
||||
// Value is chosen to be as same as NRFX_POWER_USB_EVT_* in nrfx_power.h
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
extern uint32_t mock_millis;
|
||||
inline uint32_t millis() { return mock_millis; }
|
||||
inline void delay(uint32_t value) { mock_millis += value; }
|
||||
|
||||
class Stream {
|
||||
public:
|
||||
virtual ~Stream() = default;
|
||||
virtual int available() = 0;
|
||||
virtual int read() = 0;
|
||||
virtual int peek() = 0;
|
||||
virtual void flush() = 0;
|
||||
virtual int availableForWrite() { return 0; }
|
||||
virtual size_t write(uint8_t value) = 0;
|
||||
virtual size_t write(const uint8_t* data, size_t size) = 0;
|
||||
size_t printf(const char* format, ...) {
|
||||
char buffer[256];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
const int count = vsnprintf(buffer, sizeof(buffer), format, args);
|
||||
va_end(args);
|
||||
assert(count >= 0 && static_cast<size_t>(count) < sizeof(buffer));
|
||||
return write(reinterpret_cast<const uint8_t*>(buffer), static_cast<size_t>(count));
|
||||
}
|
||||
};
|
||||
|
||||
class RawSerial {
|
||||
public:
|
||||
int availableForWrite() { return 256; }
|
||||
template <typename... Args> void printf(Args...) { assert(false && "raw USB write"); }
|
||||
template <typename... Args> void println(Args...) { assert(false && "raw USB write"); }
|
||||
};
|
||||
extern RawSerial Serial;
|
||||
|
||||
struct MockFicr { uint32_t DEVICEID[2] = {0x123456, 0}; };
|
||||
extern MockFicr mock_ficr;
|
||||
#define NRF_FICR (&mock_ficr)
|
||||
#define NRF_SPIM1 1
|
||||
#define WB_IO2 2
|
||||
#define OUTPUT 1
|
||||
#define HIGH 1
|
||||
inline void pinMode(int, int) {}
|
||||
inline void digitalWrite(int, int) {}
|
||||
inline void vTaskDelete(void*) {}
|
||||
inline void vTaskDelay(uint32_t value) { delay(value); }
|
||||
inline uint32_t pdMS_TO_TICKS(uint32_t value) { return value; }
|
||||
inline void xTaskCreate(void (*)(void*), const char*, int, void*, int, void*) {}
|
||||
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <SPI.h>
|
||||
|
||||
class IPAddress {
|
||||
public:
|
||||
uint8_t operator[](size_t index) const {
|
||||
static const uint8_t address[4] = {192, 168, 1, 42};
|
||||
return address[index];
|
||||
}
|
||||
};
|
||||
|
||||
extern std::string ethernet_output;
|
||||
extern std::string ethernet_input;
|
||||
extern bool accept_client;
|
||||
class EthernetClient {
|
||||
bool _connected = false;
|
||||
public:
|
||||
explicit EthernetClient(bool connected = false) : _connected(connected) {}
|
||||
explicit operator bool() const { return _connected; }
|
||||
bool connected() const { return _connected; }
|
||||
void stop() { _connected = false; }
|
||||
IPAddress remoteIP() const { return {}; }
|
||||
int available() const { return static_cast<int>(ethernet_input.size()); }
|
||||
int read() {
|
||||
assert(!ethernet_input.empty());
|
||||
const char value = ethernet_input.front();
|
||||
ethernet_input.erase(0, 1);
|
||||
return value;
|
||||
}
|
||||
void print(const char* text) { ethernet_output += text; }
|
||||
void println(const char* text = "") { ethernet_output += std::string(text) + "\r\n"; }
|
||||
};
|
||||
|
||||
class EthernetServer {
|
||||
public:
|
||||
explicit EthernetServer(int) {}
|
||||
void begin() {}
|
||||
EthernetClient accept() {
|
||||
const bool accepted = accept_client;
|
||||
accept_client = false;
|
||||
return EthernetClient(accepted);
|
||||
}
|
||||
};
|
||||
|
||||
enum { EthernetNoHardware = 0, EthernetPresent = 1, LinkOFF = 0, LinkON = 1 };
|
||||
class MockEthernet {
|
||||
public:
|
||||
int failures = 0;
|
||||
int hardware = EthernetPresent;
|
||||
int link = LinkON;
|
||||
void init(SPIClass&, int) {}
|
||||
int begin(uint8_t*, int, int) {
|
||||
if (failures > 0) { --failures; return 0; }
|
||||
return 1;
|
||||
}
|
||||
int hardwareStatus() const { return hardware; }
|
||||
int linkStatus() const { return link; }
|
||||
IPAddress localIP() const { return {}; }
|
||||
void maintain() {}
|
||||
};
|
||||
extern MockEthernet Ethernet;
|
||||
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
class SPIClass {
|
||||
public:
|
||||
SPIClass(int, int, int, int) {}
|
||||
void begin() {}
|
||||
};
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
#include <helpers/NonBlockingWriteStream.h>
|
||||
#include <helpers/nrf52/EthernetCLI.h>
|
||||
#include "NetworkLoggingMacro.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
uint32_t mock_millis = 0;
|
||||
RawSerial Serial;
|
||||
MockFicr mock_ficr;
|
||||
MockEthernet Ethernet;
|
||||
std::string ethernet_output;
|
||||
std::string ethernet_input;
|
||||
bool accept_client = false;
|
||||
|
||||
class MockUsb : public Stream {
|
||||
public:
|
||||
int capacity = 256;
|
||||
bool refuse_write = false;
|
||||
unsigned int tries = 0;
|
||||
std::string output;
|
||||
int available() override { return 0; }
|
||||
int read() override { return -1; }
|
||||
int peek() override { return -1; }
|
||||
void flush() override { assert(false && "blocking flush"); }
|
||||
int availableForWrite() override { return capacity; }
|
||||
size_t write(uint8_t) override { assert(false && "blocking write"); return 0; }
|
||||
size_t write(const uint8_t*, size_t) override { assert(false && "blocking write"); return 0; }
|
||||
};
|
||||
|
||||
static MockUsb usb;
|
||||
static bool logging_enabled = true;
|
||||
static size_t tryUsbWrite(void*, const uint8_t* data, size_t size) {
|
||||
++usb.tries;
|
||||
if (usb.refuse_write || usb.capacity <= 0) return 0;
|
||||
const size_t written = size < static_cast<size_t>(usb.capacity)
|
||||
? size : static_cast<size_t>(usb.capacity);
|
||||
usb.output.append(reinterpret_cast<const char*>(data), written);
|
||||
return written;
|
||||
}
|
||||
static mesh::SingleAttemptNonBlockingStream logging_port(usb, tryUsbWrite);
|
||||
namespace mesh {
|
||||
bool isUsbLoggingEnabled() { return logging_enabled; }
|
||||
Stream& usbLoggingPort() { return logging_port; }
|
||||
}
|
||||
|
||||
static void reset() {
|
||||
usb.output.clear();
|
||||
usb.capacity = 256;
|
||||
usb.refuse_write = false;
|
||||
usb.tries = 0;
|
||||
logging_enabled = true;
|
||||
ethernet_running = false;
|
||||
ethernet_client = EthernetClient();
|
||||
ethernet_output.clear();
|
||||
ethernet_input.clear();
|
||||
accept_client = false;
|
||||
Ethernet = MockEthernet();
|
||||
mock_millis = 0;
|
||||
}
|
||||
|
||||
static void acceptClientAndReply() {
|
||||
accept_client = true;
|
||||
ethernet_loop_maintain();
|
||||
assert(ethernet_take_session_reset());
|
||||
assert(!ethernet_take_session_reset());
|
||||
ethernet_input = "eth.status\r";
|
||||
char command[80] = {};
|
||||
assert(ethernet_read_line(command, sizeof(command)));
|
||||
assert(strcmp(command, "eth.status") == 0);
|
||||
char reply[80] = {};
|
||||
assert(ethernet_handle_command(command, reply));
|
||||
ethernet_send_reply(reply);
|
||||
assert(ethernet_output == "MeshCore CLI\r\n\r\n -> ETH: 192.168.1.42:23\r\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
(void)ðernet_start_task;
|
||||
// Runtime-off diagnostics must not attempt USB, while functional Ethernet
|
||||
// commands and the client banner still work.
|
||||
reset();
|
||||
logging_enabled = false;
|
||||
ethernet_task(nullptr);
|
||||
acceptClientAndReply();
|
||||
assert(usb.output.empty() && usb.tries == 0 && mock_millis == 0);
|
||||
|
||||
reset();
|
||||
ethernet_task(nullptr);
|
||||
acceptClientAndReply();
|
||||
assert(usb.output.find("ETH: MAC: 02:92:1F:12:34:56\n") != std::string::npos);
|
||||
assert(usb.output.find("ETH: Listening on TCP port 23\n") != std::string::npos);
|
||||
assert(usb.output.find("ETH: Client connected from 192.168.1.42\n") != std::string::npos);
|
||||
assert(usb.tries == 6 && mock_millis == 0);
|
||||
|
||||
// An open-but-stalled host has no TX space. No raw write or wait is allowed.
|
||||
reset();
|
||||
usb.capacity = 0;
|
||||
ethernet_task(nullptr);
|
||||
acceptClientAndReply();
|
||||
assert(usb.output.empty() && usb.tries == 0 && mock_millis == 0);
|
||||
|
||||
// The host can also vanish after the capacity preflight. The real shared
|
||||
// single-attempt facade drops each diagnostic instead of retrying or waiting.
|
||||
reset();
|
||||
usb.refuse_write = true;
|
||||
ethernet_task(nullptr);
|
||||
acceptClientAndReply();
|
||||
assert(usb.output.empty() && usb.tries == 6 && mock_millis == 0);
|
||||
|
||||
for (int failure : {0, 1, 2}) {
|
||||
reset();
|
||||
Ethernet.failures = 1;
|
||||
if (failure == 0) Ethernet.hardware = EthernetNoHardware;
|
||||
if (failure == 1) Ethernet.link = LinkOFF;
|
||||
ethernet_task(nullptr);
|
||||
const char* expected = failure == 0 ? "Hardware not found, giving up"
|
||||
: failure == 1 ? "Cable not connected, will retry" : "DHCP failed, will retry";
|
||||
assert(usb.output.find(expected) != std::string::npos);
|
||||
assert(mock_millis == (failure == 0 ? 0u : 30000u));
|
||||
}
|
||||
|
||||
reset();
|
||||
int evaluated = 0;
|
||||
logging_enabled = false;
|
||||
NETWORK_DEBUG_PRINTLN("suppressed %d", ++evaluated);
|
||||
assert(evaluated == 0 && usb.tries == 0);
|
||||
logging_enabled = true;
|
||||
usb.capacity = 0;
|
||||
NETWORK_DEBUG_PRINTLN("stalled %d", ++evaluated);
|
||||
assert(evaluated == 0 && usb.tries == 0);
|
||||
usb.capacity = 256;
|
||||
NETWORK_DEBUG_PRINTLN("visible %d", ++evaluated);
|
||||
#if defined(MQTT_DEBUG) && MQTT_DEBUG
|
||||
assert(evaluated == 1 && usb.output == "MQTT: visible 1\n" && usb.tries == 1);
|
||||
usb.refuse_write = true;
|
||||
NETWORK_DEBUG_PRINTLN("disconnected %d", ++evaluated);
|
||||
assert(usb.tries == 2 && usb.output == "MQTT: visible 1\n");
|
||||
#else
|
||||
assert(evaluated == 0 && usb.output.empty() && usb.tries == 0);
|
||||
#endif
|
||||
assert(mock_millis == 0);
|
||||
std::cout << "PASS: gated USB diagnostics\n";
|
||||
}
|
||||
Reference in new issue
Block a user