Improve companion setup and USB session recovery
This commit is contained in:
1 parent
ecaa5ac2a0
commit
cf70478c51
34 files changed
+6283
-2147
No files matched your search
@@ -65,11 +65,18 @@ enum class CompanionWiFiDisplayState : uint8_t {
|
||||
// distinguish network state from a stale or incorrect LCD frame.
|
||||
void noteCompanionWiFiDisplayState(CompanionWiFiDisplayState state);
|
||||
void formatCompanionWiFiDisplayStatus(char* reply, size_t reply_size);
|
||||
#ifdef WITH_WEBCONFIG
|
||||
// Use Companion ownership state when formatting an otherwise ambiguous idle or
|
||||
// disconnected station; WebConfig being stopped does not mean WiFi is off.
|
||||
void formatCompanionWiFiStatus(char* reply, size_t reply_size);
|
||||
#endif
|
||||
|
||||
// A display callback only queues this request. The main loop owns the actual
|
||||
// WebConfig/WiFi transition so touch handling never tears down network services
|
||||
// from the UI task.
|
||||
// Display callbacks only queue these requests. The main loop owns the actual
|
||||
// WebConfig/WiFi transitions so input handling never tears down network
|
||||
// services from the UI task. Neither request changes the saved WebUI or WiFi
|
||||
// preference; the setup AP is controlled only for the current boot session.
|
||||
void requestCompanionWiFiSetup();
|
||||
void requestCompanionWiFiSetupStop();
|
||||
|
||||
// Reload saved credentials after a text-terminal update. The reconnect is
|
||||
// deferred so the command reply can leave USB/TCP before WiFi is restarted.
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <helpers/CompanionHardwareCommandCompat.h>
|
||||
#include <helpers/CompanionStatusResponse.h>
|
||||
#include <helpers/IdentityGeneration.h>
|
||||
#include <helpers/StorageLayout.h>
|
||||
#include <helpers/UsbAsciiBinarySwitch.h>
|
||||
#include <helpers/UsbLogging.h>
|
||||
#include "helpers/radiolib/RXPowerSaving.h"
|
||||
@@ -2118,6 +2119,12 @@ bool MyMesh::handleLocalControlCommand(const char* command, char* reply,
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strncmp(command, "get ", 4) == 0 &&
|
||||
mesh::cli::handleStorageLayoutGet(command + 4, board, reply,
|
||||
reply_size)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(command, "get pwrmgt.bootreason") == 0
|
||||
|| strcmp(command, "powerlog") == 0) {
|
||||
if (!board.isPowerManagementInitialized()) {
|
||||
@@ -2396,7 +2403,7 @@ bool MyMesh::handleLocalControlCommand(const char* command, char* reply,
|
||||
WebConfigServer::formatWiFiSSID(reply, reply_size);
|
||||
return true;
|
||||
case mesh::cli::StandaloneWiFiKey::Status:
|
||||
WebConfigServer::formatWiFiStatus(reply, reply_size);
|
||||
formatCompanionWiFiStatus(reply, reply_size);
|
||||
return true;
|
||||
case mesh::cli::StandaloneWiFiKey::CLI:
|
||||
// Companion WebConfig deliberately has no browser command terminal:
|
||||
@@ -3302,6 +3309,22 @@ bool MyMesh::hasPendingReqs() const {
|
||||
|| pending_discovery != 0 || pending_req != 0;
|
||||
}
|
||||
|
||||
bool MyMesh::hasFiniteDelayedReplyForRoute(BaseSerialInterface* route) const {
|
||||
if (route == NULL) return false;
|
||||
if (pending_serial_reply_route == route
|
||||
|| (command_radio_apply_pending && command_radio_reply_route == route)
|
||||
|| (binary_trace_pending && binary_trace_reply_route == route)) {
|
||||
return true;
|
||||
}
|
||||
for (int i = 0; i < EXPECTED_ACK_TABLE_SIZE; ++i) {
|
||||
if (expected_ack_table[i].ack != 0
|
||||
&& expected_ack_table[i].reply_route == route) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void MyMesh::servicePendingSerialReply() {
|
||||
if (pending_serial_reply_route == NULL) return;
|
||||
if (!_serial->isReplyRouteAvailable(pending_serial_reply_route)
|
||||
@@ -6331,6 +6354,7 @@ void MyMesh::handleTerminalCommand(char* command) {
|
||||
terminalOutput().print("Commands:\r\n");
|
||||
terminalOutput().print(" board\r\n");
|
||||
terminalOutput().print(" version\r\n");
|
||||
terminalOutput().print(" get storage.layout\r\n");
|
||||
terminalOutput().print(" get pwrmgt.bootreason\r\n");
|
||||
#if COMPANION_FEATURE_MEMORY_DIAGNOSTICS
|
||||
terminalOutput().print(" memory\r\n");
|
||||
|
||||
@@ -134,6 +134,7 @@ public:
|
||||
void startInterface(BaseSerialInterface &serial);
|
||||
void cancelSerialResponseStream();
|
||||
void cancelSerialOperationsForRoute(BaseSerialInterface* route);
|
||||
bool hasFiniteDelayedReplyForRoute(BaseSerialInterface* route) const;
|
||||
void resetUsbHostSessionInput();
|
||||
|
||||
const char *getNodeName();
|
||||
@@ -187,6 +188,9 @@ public:
|
||||
#ifdef ENABLE_USB_INTERFACE
|
||||
void enterTerminalMode();
|
||||
void exitTerminalMode();
|
||||
// Clear state owned by the current text-terminal host without changing the
|
||||
// protocol owner or printing a new banner.
|
||||
void resetTerminalSession();
|
||||
bool isTerminalMode() const { return _terminal_mode; }
|
||||
void handleTerminalCommand(char* command);
|
||||
#if COMPANION_FEATURE_NETWORK_TERMINAL
|
||||
@@ -362,7 +366,6 @@ private:
|
||||
#ifdef ENABLE_USB_INTERFACE
|
||||
Stream& terminalOutput();
|
||||
bool hasTerminalOutput() const { return _terminal_output != NULL; }
|
||||
void resetTerminalSession();
|
||||
void printTerminalBanner(bool show_binary_stop);
|
||||
ContactInfo* getTerminalRecipient();
|
||||
void printTerminalPath(const ContactInfo& recipient);
|
||||
|
||||
@@ -93,6 +93,7 @@ MultiSerialInterface interface_manager;
|
||||
#include <helpers/ArduinoSerialInterface.h>
|
||||
#include <helpers/CLICommandUtils.h>
|
||||
#include <helpers/UsbAsciiBinarySwitch.h>
|
||||
#include <helpers/UsbLogging.h>
|
||||
static const char USB_TERMINAL_START_TOKEN[] = "+++MESHCORE-TERM-START";
|
||||
static const char USB_TERMINAL_STOP_TOKEN[] = "+++MESHCORE-TERM-STOP";
|
||||
#if COMPANION_FEATURE_USB_MOTA_SOURCE
|
||||
@@ -107,6 +108,32 @@ MultiSerialInterface interface_manager;
|
||||
// for targets which cannot report DTR (see setConnectedCheck below)
|
||||
#define USB_CLIENT_IDLE_TIMEOUT (10*60*1000UL)
|
||||
#endif
|
||||
#ifndef USB_FRAME_REPLY_GRACE_MS
|
||||
// A complete command frame proves that a Binary Companion client is
|
||||
// listening even if an ESP32 HWCDC host-presence flag briefly flaps while
|
||||
// the response is being queued. Keep this much time to finish the reply.
|
||||
#define USB_FRAME_REPLY_GRACE_MS 2000UL
|
||||
#endif
|
||||
#ifndef USB_HOST_LOSS_GRACE_MS
|
||||
// The bundled ESP32 HWCDC SOF detector declares loss after only a few
|
||||
// missed ticks. Debounce that advisory signal before ending a host session.
|
||||
#define USB_HOST_LOSS_GRACE_MS 2000UL
|
||||
#endif
|
||||
#ifndef USB_HOST_LOSS_EDGE_MS
|
||||
// HWCDC's framework filter declares loss after roughly 5 ms without SOFs.
|
||||
// Require a longer continuous gap before using that signal as a fallback
|
||||
// session boundary when the finite BUS_RESET event queue drops its event.
|
||||
#define USB_HOST_LOSS_EDGE_MS 100UL
|
||||
#endif
|
||||
#ifndef USB_TRANSPORT_RESET_RETRY_MS
|
||||
// A failed purge can spend up to roughly 150 ms waiting on the live HWCDC
|
||||
// writer/ring. Back off enough to keep WiFi and mesh service responsive.
|
||||
#define USB_TRANSPORT_RESET_RETRY_MS 1000UL
|
||||
#endif
|
||||
#if defined(ESP32) && defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE == 1 \
|
||||
&& defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT
|
||||
mesh::UsbHostPresenceDebouncer usb_hwcdc_host_presence;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if COMPANION_FEATURE_NETWORK_TERMINAL
|
||||
@@ -502,7 +529,10 @@ static void queueUsbTerminalControlReply(const char* reply) {
|
||||
static void drainUsbTerminalOutputBeforeProtocolSwitch() {
|
||||
const uint32_t started = millis();
|
||||
while (mesh::hasPendingUsbTerminalOutput()
|
||||
#if !(defined(ESP32) && defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE == 1 \
|
||||
&& defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT)
|
||||
&& isUsbTerminalDataConnected()
|
||||
#endif
|
||||
&& (uint32_t)(millis() - started)
|
||||
< USB_TERMINAL_PROTOCOL_DRAIN_MS) {
|
||||
mesh::serviceUsbTerminalPort();
|
||||
@@ -605,6 +635,12 @@ static bool enterUsbMotaMode(mesh::UsbMotaEntryOrigin origin) {
|
||||
usb_mota_line_len = 0;
|
||||
usb_mota_line[0] = 0;
|
||||
usb_mota_disconnect_armed = isUsbTerminalDataConnected();
|
||||
#if defined(ESP32) && defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE == 1 \
|
||||
&& defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT
|
||||
// A valid attach command is stronger host proof than HWCDC's advisory SOF
|
||||
// sample, which may be low in the same loop as the received command.
|
||||
usb_hwcdc_host_presence.noteClientActivity();
|
||||
#endif
|
||||
|
||||
char reply[160] = {0};
|
||||
if (!the_mesh.handleLocalControlCommand("ota folder on", reply, sizeof(reply))
|
||||
@@ -631,12 +667,15 @@ static bool enterUsbMotaMode(mesh::UsbMotaEntryOrigin origin) {
|
||||
}
|
||||
|
||||
static void serviceUsbMota() {
|
||||
#if !(defined(ESP32) && defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE == 1 \
|
||||
&& defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT)
|
||||
if (isUsbTerminalDataConnected()) {
|
||||
usb_mota_disconnect_armed = true;
|
||||
} else if (usb_mota_disconnect_armed) {
|
||||
leaveUsbMotaMode(false);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
// SerialMotaSource consumes framed responses synchronously while serving a
|
||||
// block. Bytes left here are host control text, notably motatool's automatic
|
||||
@@ -669,51 +708,113 @@ static void serviceUsbMota() {
|
||||
}
|
||||
#endif
|
||||
|
||||
// DTR polling alone cannot distinguish a continuously open handle from a
|
||||
// close/reopen that happened entirely between two loop iterations. Consume the
|
||||
// TinyUSB owner-task edge before any protocol parser runs, so no line, login,
|
||||
// recipient, queued text, or mOTA attachment survives into the next host.
|
||||
// Consume an exact TinyUSB owner-task edge, or HWCDC's framework-debounced SOF
|
||||
// loss edge, before any protocol parser runs. No line, login, recipient, queued
|
||||
// text, or mOTA attachment may survive into the next host session.
|
||||
static bool usb_terminal_host_reset_completion_pending = false;
|
||||
static uint32_t usb_terminal_host_reset_retry_at = 0;
|
||||
|
||||
static void resetUsbTerminalHostSession(bool preserve_ascii_terminal) {
|
||||
// Stop only work routed to the old USB host; a simultaneous BLE/WiFi
|
||||
// transaction is independent and must continue. Clear the route after
|
||||
// stopping its producer so no response can fall back to another transport.
|
||||
cancelUsbSerialOperations();
|
||||
the_mesh.resetUsbHostSessionInput();
|
||||
bool protocol_owner_reset = false;
|
||||
#if COMPANION_FEATURE_USB_MOTA_SOURCE
|
||||
if (usb_mota_mode) {
|
||||
leaveUsbMotaMode(false);
|
||||
protocol_owner_reset = true;
|
||||
}
|
||||
#endif
|
||||
if (!protocol_owner_reset && the_mesh.isTerminalMode()) {
|
||||
if (preserve_ascii_terminal) {
|
||||
// Full Companion's ordinary HWCDC terminal is the default protocol.
|
||||
// Clear the old host's input/output without turning the next physical
|
||||
// session into a silent Binary-only port.
|
||||
the_mesh.resetTerminalSession();
|
||||
mesh::discardUsbTerminalOutput();
|
||||
clearUsbTerminalLine();
|
||||
usb_terminal_discard_line = false;
|
||||
usb_terminal_disconnect_armed = false;
|
||||
#if defined(COMPANION_RADIO_FULL)
|
||||
usb_binary_startup_probe.cancel();
|
||||
#endif
|
||||
} else {
|
||||
leaveUsbTerminalMode(false);
|
||||
}
|
||||
protocol_owner_reset = true;
|
||||
}
|
||||
|
||||
if (!protocol_owner_reset) {
|
||||
mesh::discardUsbTerminalOutput();
|
||||
usb_serial_interface.setPassthroughMode(false);
|
||||
clearUsbTerminalLine();
|
||||
usb_terminal_discard_line = false;
|
||||
usb_terminal_disconnect_armed = false;
|
||||
usb_logging_terminal_mode = false;
|
||||
#if defined(COMPANION_RADIO_FULL)
|
||||
usb_binary_startup_probe.cancel();
|
||||
#endif
|
||||
}
|
||||
// setPassthroughMode() clears parser/queue state but frame ownership is a
|
||||
// separate proof. Revoke all three explicitly at the session boundary.
|
||||
usb_serial_interface.resetSessionState();
|
||||
#if defined(ESP32) && defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE == 1 \
|
||||
&& defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT
|
||||
// Purge HWCDC's queues behind a temporary diagnostics gate. This keeps the
|
||||
// core's shared RTOS objects alive while other firmware tasks are running.
|
||||
if (!mesh::resetUsbCompanionTransport()) {
|
||||
// Keep the producer gate and PHY quarantined, then retry from later loops.
|
||||
// Rebooting on repeatable allocation/contention failures creates a host-
|
||||
// enumeration restart loop and still does not prove stale bytes were gone.
|
||||
usb_terminal_host_reset_completion_pending = true;
|
||||
usb_terminal_host_reset_retry_at = millis() + USB_TRANSPORT_RESET_RETRY_MS;
|
||||
return;
|
||||
}
|
||||
usb_terminal_host_reset_completion_pending = false;
|
||||
usb_hwcdc_host_presence.reset();
|
||||
#endif
|
||||
}
|
||||
|
||||
static void serviceUsbTerminalHostSessionReset() {
|
||||
#if defined(NRF52_PLATFORM)
|
||||
if (mesh::takeUsbTerminalSessionReset()) {
|
||||
usb_terminal_host_reset_completion_pending = true;
|
||||
// A rapid reopen can make USB look connected again before this loop. Stop
|
||||
// only work routed to the old USB host; a simultaneous BLE transaction is
|
||||
// independent and must continue. Clear the route after stopping its
|
||||
// producer so no remaining response can fall back to another transport.
|
||||
cancelUsbSerialOperations();
|
||||
the_mesh.resetUsbHostSessionInput();
|
||||
bool protocol_owner_reset = false;
|
||||
#if COMPANION_FEATURE_USB_MOTA_SOURCE
|
||||
if (usb_mota_mode) {
|
||||
leaveUsbMotaMode(false);
|
||||
protocol_owner_reset = true;
|
||||
}
|
||||
#endif
|
||||
if (!protocol_owner_reset && the_mesh.isTerminalMode()) {
|
||||
leaveUsbTerminalMode(false);
|
||||
protocol_owner_reset = true;
|
||||
}
|
||||
|
||||
if (!protocol_owner_reset) {
|
||||
mesh::discardUsbTerminalOutput();
|
||||
usb_serial_interface.setPassthroughMode(false);
|
||||
clearUsbTerminalLine();
|
||||
usb_terminal_discard_line = false;
|
||||
usb_terminal_disconnect_armed = false;
|
||||
usb_logging_terminal_mode = false;
|
||||
#if defined(COMPANION_RADIO_FULL)
|
||||
usb_binary_startup_probe.cancel();
|
||||
#endif
|
||||
}
|
||||
resetUsbTerminalHostSession(false);
|
||||
}
|
||||
|
||||
if (usb_terminal_host_reset_completion_pending
|
||||
&& mesh::tryCompleteUsbTerminalSessionReset()) {
|
||||
usb_terminal_host_reset_completion_pending = false;
|
||||
}
|
||||
#elif defined(ESP32) && defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE == 1 \
|
||||
&& defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT
|
||||
if (usb_terminal_host_reset_completion_pending) {
|
||||
if ((int32_t)(millis() - usb_terminal_host_reset_retry_at) >= 0) {
|
||||
if (mesh::resetUsbCompanionTransport()) {
|
||||
usb_terminal_host_reset_completion_pending = false;
|
||||
usb_hwcdc_host_presence.reset();
|
||||
} else {
|
||||
usb_terminal_host_reset_retry_at =
|
||||
millis() + USB_TRANSPORT_RESET_RETRY_MS;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const bool host_present = board.isUsbHostConnected();
|
||||
(void)usb_hwcdc_host_presence.observeHost(
|
||||
host_present, millis(), USB_HOST_LOSS_EDGE_MS,
|
||||
USB_HOST_LOSS_GRACE_MS);
|
||||
const bool hardware_bus_reset = mesh::takeUsbTerminalSessionReset();
|
||||
const bool physical_host_loss =
|
||||
usb_hwcdc_host_presence.takeHostLossEdge();
|
||||
const bool sustained_host_loss =
|
||||
usb_hwcdc_host_presence.takeSustainedHostLoss();
|
||||
if (hardware_bus_reset || physical_host_loss || sustained_host_loss) {
|
||||
resetUsbTerminalHostSession(true);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -940,6 +1041,7 @@ void halt() {
|
||||
static bool companion_wifi_disable_in_progress = false;
|
||||
static bool companion_wifi_services_stopped = false;
|
||||
static volatile bool companion_wifi_setup_requested = false;
|
||||
static volatile bool companion_wifi_setup_stop_requested = false;
|
||||
static bool companion_wifi_credential_reload_pending = false;
|
||||
static unsigned long companion_wifi_credential_reload_at = 0;
|
||||
static bool companion_wifi_power_save_loaded = false;
|
||||
@@ -1313,10 +1415,27 @@ void halt() {
|
||||
WiFi.localIP().toString().c_str());
|
||||
}
|
||||
|
||||
#ifdef WITH_WEBCONFIG
|
||||
void formatCompanionWiFiStatus(char* reply, size_t reply_size) {
|
||||
const mesh::wifi::CompanionWiFiRuntimeState runtime = {
|
||||
companion_wifi_requested,
|
||||
companion_wifi_active,
|
||||
companion_wifi_credential_reload_pending,
|
||||
};
|
||||
WebConfigServer::formatWiFiStatus(reply, reply_size, &runtime);
|
||||
}
|
||||
#endif
|
||||
|
||||
void requestCompanionWiFiSetup() {
|
||||
companion_wifi_setup_stop_requested = false;
|
||||
companion_wifi_setup_requested = true;
|
||||
}
|
||||
|
||||
void requestCompanionWiFiSetupStop() {
|
||||
companion_wifi_setup_requested = false;
|
||||
companion_wifi_setup_stop_requested = true;
|
||||
}
|
||||
|
||||
bool toggleCompanionWiFi() {
|
||||
#if defined(COMPANION_EXCLUSIVE_WIFI_BLE)
|
||||
const CompanionTransportMode current = getCompanionTransportMode();
|
||||
@@ -1847,7 +1966,37 @@ void halt() {
|
||||
#endif
|
||||
|
||||
void setup() {
|
||||
#if defined(ESP32) && defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE == 1 \
|
||||
&& defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT \
|
||||
&& defined(ENABLE_USB_INTERFACE)
|
||||
// Configure HWCDC before begin() allocates its mutex and enables the USB ISR.
|
||||
// The bundled setTxBufferSize() deletes/recreates its ring without taking the
|
||||
// TX mutex or masking that ISR, so even an otherwise-quiet post-begin resize
|
||||
// can race the interrupt handler.
|
||||
// A failed resize deletes HWCDC's existing ring. Try progressively smaller
|
||||
// queues while setup still has no producers, and tell reset cleanup the exact
|
||||
// capacity it must observe before reopening a host session.
|
||||
static const size_t usb_tx_sizes[] = {
|
||||
MESH_ESP32_USB_TX_BUFFER_SIZE, 2048, 1024, 512, 256};
|
||||
size_t usb_tx_capacity = 0;
|
||||
for (size_t candidate : usb_tx_sizes) {
|
||||
usb_tx_capacity = Serial.setTxBufferSize(candidate);
|
||||
if (usb_tx_capacity == candidate) break;
|
||||
}
|
||||
Serial.setTxTimeoutMs(5);
|
||||
#endif
|
||||
Serial.begin(115200);
|
||||
#if defined(ESP32) && defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE == 1 \
|
||||
&& defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT \
|
||||
&& defined(ENABLE_USB_INTERFACE)
|
||||
// If every pre-begin allocation failed, begin() may have recovered by
|
||||
// creating its built-in 256-byte ring. Discover that ring without replacing
|
||||
// it after the ISR is live; a zero result leaves reset cleanup quarantined.
|
||||
if (usb_tx_capacity == 0) {
|
||||
usb_tx_capacity = Serial.availableForWrite();
|
||||
}
|
||||
mesh::setUsbCompanionTxBufferCapacity(usb_tx_capacity);
|
||||
#endif
|
||||
#if MESH_PACKET_LOGGING
|
||||
mesh::serialLogBegin();
|
||||
#endif
|
||||
@@ -2115,19 +2264,25 @@ void setup() {
|
||||
usb_serial_interface.enableFlowControl(true);
|
||||
#if defined(ESP32) && defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE == 1 \
|
||||
&& defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT
|
||||
// a 256 byte TX buffer overflows during a contact sync, and write() blocks
|
||||
// up to tx_timeout_ms per call against a stalled host (same reasoning as
|
||||
// the kiss_modem tuning)
|
||||
Serial.setTxBufferSize(4096);
|
||||
Serial.setTxTimeoutMs(5);
|
||||
// The ESP32 USB-Serial-JTAG peripheral (HWCDC) has no DTR concept at all,
|
||||
// and its bool operator is only a transient RX/TX activity heuristic. A real
|
||||
// Binary Companion client therefore has to prove ownership by sending
|
||||
// frames; an idle physical cable must not claim the interface.
|
||||
// The ESP32 USB-Serial-JTAG peripheral (HWCDC) has no DTR concept at all.
|
||||
// Its bool operator is a transient RX/TX activity heuristic, and even the
|
||||
// SOF-based host-presence flag can briefly flap around a bus reset. A
|
||||
// complete command frame is stronger proof that a client is listening, so
|
||||
// grant it a short lease in which the response can be queued and drained.
|
||||
// Outside that lease, require the physical host signal as well as recent
|
||||
// Binary Companion activity so an idle cable does not claim the interface.
|
||||
// The idle cap stays absolute: an abandoned multi-frame response must not
|
||||
// lock BLE or WiFi dispatch forever merely because SOFs continue.
|
||||
usb_serial_interface.setConnectedCheck([]() {
|
||||
uint32_t last = usb_serial_interface.getLastFrameMillis();
|
||||
return (bool)Serial && usb_serial_interface.hasReceivedFrame()
|
||||
&& (millis() - last) < USB_CLIENT_IDLE_TIMEOUT;
|
||||
return usb_hwcdc_host_presence.isClientConnected(
|
||||
board.isUsbHostConnected(), millis(),
|
||||
usb_serial_interface.hasReceivedFrame(),
|
||||
usb_serial_interface.getLastFrameMillis(),
|
||||
usb_serial_interface.getCompletedFrameCount(),
|
||||
USB_FRAME_REPLY_GRACE_MS, USB_CLIENT_IDLE_TIMEOUT,
|
||||
USB_HOST_LOSS_EDGE_MS,
|
||||
USB_HOST_LOSS_GRACE_MS,
|
||||
the_mesh.hasFiniteDelayedReplyForRoute(&usb_serial_interface));
|
||||
});
|
||||
#elif (defined(ESP32) && defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT) \
|
||||
|| defined(NRF52_PLATFORM) || defined(RP2040_PLATFORM)
|
||||
@@ -2317,13 +2472,15 @@ void loop() {
|
||||
#if defined(ESP32) && defined(WIFI_SSID)
|
||||
#ifdef WITH_WEBCONFIG
|
||||
the_mesh.serviceWebConfig();
|
||||
if (companion_wifi_setup_stop_requested) {
|
||||
companion_wifi_setup_stop_requested = false;
|
||||
the_mesh.stopWebConfig();
|
||||
}
|
||||
if (companion_wifi_setup_requested) {
|
||||
companion_wifi_setup_requested = false;
|
||||
if (companion_wifi_requested) {
|
||||
char web_reply[160];
|
||||
if (!the_mesh.startWebConfig(true, web_reply)) {
|
||||
WIFI_DEBUG_PRINTLN("Display WiFi setup request: %s", web_reply);
|
||||
}
|
||||
char web_reply[160];
|
||||
if (!the_mesh.startWebConfig(true, web_reply)) {
|
||||
WIFI_DEBUG_PRINTLN("Display WiFi setup request: %s", web_reply);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -118,6 +118,46 @@ static void drawCompanionTransportChoice(DisplayDriver& display,
|
||||
#endif
|
||||
|
||||
#if UI_WIFI_SETUP_HOME_PAGE == 1
|
||||
static bool drawCompactCompanionWiFiSetupPage(
|
||||
DisplayDriver& display, bool setup_active, const char* setup_ssid,
|
||||
const char* setup_ip, bool wifi_enabled, bool wifi_connected,
|
||||
bool wifi_configured) {
|
||||
if (display.width() > 128 || display.height() > 64) return false;
|
||||
|
||||
// Four small rows fit below the shared title bar and page dots. The OLED
|
||||
// deliberately uses text instead of a QR: at this scale the SSID, portal IP,
|
||||
// and physical-button action are more useful and remain readable.
|
||||
display.setCompactText(true);
|
||||
display.setTextSize(1);
|
||||
display.setColor(UIColor::primary_txt);
|
||||
display.drawTextCentered(
|
||||
display.width() / 2, 20,
|
||||
setup_active ? "SETUP AP ACTIVE" : "SETUP AP INACTIVE");
|
||||
|
||||
if (setup_active) {
|
||||
display.drawTextEllipsized(0, 31, display.width(), setup_ssid);
|
||||
char open_ip[22];
|
||||
snprintf(open_ip, sizeof(open_ip), "OPEN %s", setup_ip);
|
||||
display.setColor(UIColor::secondary_txt);
|
||||
display.drawTextCentered(display.width() / 2, 42, open_ip);
|
||||
} else {
|
||||
const char* wifi_status = !wifi_enabled
|
||||
? "WIFI OFF"
|
||||
: wifi_connected
|
||||
? "WIFI READY"
|
||||
: wifi_configured ? "WIFI CONNECTING" : "WIFI NOT CONFIGURED";
|
||||
display.setColor(UIColor::secondary_txt);
|
||||
display.drawTextCentered(display.width() / 2, 37, wifi_status);
|
||||
}
|
||||
|
||||
display.setColor(UIColor::warning_txt);
|
||||
display.drawTextCentered(
|
||||
display.width() / 2, 53,
|
||||
setup_active ? "HOLD STOP AP" : "HOLD START AP");
|
||||
display.setCompactText(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void drawCompanionWiFiSetupPage(DisplayDriver& display) {
|
||||
char setup_ssid[33] = {0};
|
||||
char setup_ip[16] = {0};
|
||||
@@ -147,6 +187,12 @@ static void drawCompanionWiFiSetupPage(DisplayDriver& display) {
|
||||
}
|
||||
noteCompanionWiFiDisplayState(state);
|
||||
|
||||
if (drawCompactCompanionWiFiSetupPage(
|
||||
display, setup_active, setup_ssid, setup_ip, wifi_enabled,
|
||||
wifi_connected, wifi_configured)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Keep the setup content below the shared title bar and page dots. Compact
|
||||
// text makes its physical size identical on the 480 canvas and the
|
||||
// 320-to-480 emergency canvas.
|
||||
@@ -456,6 +502,10 @@ public:
|
||||
#endif
|
||||
}
|
||||
|
||||
#if UI_WIFI_SETUP_HOME_PAGE == 1
|
||||
bool isWiFiSetupPage() const { return _page == HomePage::WIFI_SETUP; }
|
||||
#endif
|
||||
|
||||
void poll() override {
|
||||
#if UI_WIFI_SETUP_HOME_PAGE == 1
|
||||
const bool wifi_setup_active = WebConfigServer::getSetupInfo(
|
||||
@@ -1055,10 +1105,12 @@ public:
|
||||
#endif
|
||||
#if UI_WIFI_SETUP_HOME_PAGE == 1
|
||||
if (c == KEY_ENTER && _page == HomePage::WIFI_SETUP) {
|
||||
if (isCompanionWiFiEnabled() && !isCompanionWiFiConnected()
|
||||
&& !WebConfigServer::getSetupInfo(nullptr, 0, nullptr, 0)) {
|
||||
if (WebConfigServer::getSetupInfo(nullptr, 0, nullptr, 0)) {
|
||||
requestCompanionWiFiSetupStop();
|
||||
_task->showAlert("Stopping setup AP", 1000);
|
||||
} else {
|
||||
requestCompanionWiFiSetup();
|
||||
_task->showAlert("Starting WiFi setup", 1000);
|
||||
_task->showAlert("Starting setup AP", 1000);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -1943,6 +1995,13 @@ char UITask::checkDisplayOn(char c) {
|
||||
}
|
||||
|
||||
char UITask::handleLongPress(char c) {
|
||||
#if UI_WIFI_SETUP_HOME_PAGE == 1
|
||||
// A setup AP may focus this page immediately at boot. Its documented HOLD
|
||||
// action must win over the otherwise-global early-boot CLI rescue gesture.
|
||||
if (curr == home && static_cast<HomeScreen*>(home)->isWiFiSetupPage()) {
|
||||
return c;
|
||||
}
|
||||
#endif
|
||||
if (millis() - ui_started_at < 8000) { // long press in first 8 seconds since startup -> CLI/rescue
|
||||
the_mesh.enterCLIRescue();
|
||||
c = 0; // consume event
|
||||
|
||||
Reference in new issue
Block a user