feat: unify companion builds and USB runtime modes
This commit is contained in:
1 parent
69ded9d6c7
commit
1d836c0473
31 files changed
+1425
-131
No files matched your search
@@ -359,6 +359,25 @@ void DataStore::loadPrefsInt(const char *filename, CompanionNodePrefs& _prefs, d
|
||||
>= (int)sizeof(_prefs.display_rotation_degrees)) {
|
||||
file.read((uint8_t *)&_prefs.display_rotation_degrees,
|
||||
sizeof(_prefs.display_rotation_degrees));
|
||||
if (file.available() >= (int)sizeof(_prefs.cad_enabled)) {
|
||||
file.read((uint8_t *)&_prefs.cad_enabled,
|
||||
sizeof(_prefs.cad_enabled));
|
||||
if (file.available()
|
||||
>= (int)sizeof(_prefs.cad_scan_timeout_ms)) {
|
||||
file.read((uint8_t *)&_prefs.cad_scan_timeout_ms,
|
||||
sizeof(_prefs.cad_scan_timeout_ms));
|
||||
if (file.available()
|
||||
>= (int)sizeof(_prefs.cad_retry_delay_ms)) {
|
||||
file.read((uint8_t *)&_prefs.cad_retry_delay_ms,
|
||||
sizeof(_prefs.cad_retry_delay_ms));
|
||||
if (file.available()
|
||||
>= (int)sizeof(_prefs.cad_max_duration_ms)) {
|
||||
file.read((uint8_t *)&_prefs.cad_max_duration_ms,
|
||||
sizeof(_prefs.cad_max_duration_ms));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -443,6 +462,17 @@ bool DataStore::savePrefs(const CompanionNodePrefs& _prefs, double node_lat, dou
|
||||
(uint8_t *)&_prefs.display_rotation_degrees,
|
||||
sizeof(_prefs.display_rotation_degrees))
|
||||
== sizeof(_prefs.display_rotation_degrees);
|
||||
success = success && file.write((uint8_t *)&_prefs.cad_enabled,
|
||||
sizeof(_prefs.cad_enabled)) == sizeof(_prefs.cad_enabled);
|
||||
success = success && file.write((uint8_t *)&_prefs.cad_scan_timeout_ms,
|
||||
sizeof(_prefs.cad_scan_timeout_ms))
|
||||
== sizeof(_prefs.cad_scan_timeout_ms);
|
||||
success = success && file.write((uint8_t *)&_prefs.cad_retry_delay_ms,
|
||||
sizeof(_prefs.cad_retry_delay_ms))
|
||||
== sizeof(_prefs.cad_retry_delay_ms);
|
||||
success = success && file.write((uint8_t *)&_prefs.cad_max_duration_ms,
|
||||
sizeof(_prefs.cad_max_duration_ms))
|
||||
== sizeof(_prefs.cad_max_duration_ms);
|
||||
|
||||
#if defined(NRF52_PLATFORM)
|
||||
success = file.commit(success);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <helpers/IdentityGeneration.h>
|
||||
#include "helpers/radiolib/RXPowerSaving.h"
|
||||
#include "helpers/radiolib/RxBoostedGainDefaults.h"
|
||||
#include "helpers/radiolib/CadTiming.h"
|
||||
|
||||
#if defined(ESP32_PLATFORM) && defined(BOARD_HAS_PSRAM)
|
||||
#include <esp_heap_caps.h>
|
||||
@@ -193,6 +194,10 @@ static bool save_filter(const ContactInfo& c);
|
||||
#ifndef DEFAULT_BUZZER_QUIET
|
||||
#define DEFAULT_BUZZER_QUIET 0
|
||||
#endif
|
||||
#ifndef DEFAULT_CAD_ENABLED
|
||||
// Preserve the tuned Companion behavior that preceded the runtime setting.
|
||||
#define DEFAULT_CAD_ENABLED 1
|
||||
#endif
|
||||
|
||||
#ifndef EMERGENCY_CLIENT_REPEAT_HOLD_MS
|
||||
#define EMERGENCY_CLIENT_REPEAT_HOLD_MS 120000UL
|
||||
@@ -439,7 +444,19 @@ float MyMesh::getAirtimeBudgetFactor() const {
|
||||
}
|
||||
|
||||
bool MyMesh::getCADEnabled() const {
|
||||
return true; // tuned branch behavior: hardware CAD before every companion TX
|
||||
return _prefs.cad_enabled != 0;
|
||||
}
|
||||
|
||||
uint32_t MyMesh::getCADFailRetryDelay() const {
|
||||
return _prefs.cad_retry_delay_ms != 0
|
||||
? _prefs.cad_retry_delay_ms
|
||||
: BaseChatMesh::getCADFailRetryDelay();
|
||||
}
|
||||
|
||||
uint32_t MyMesh::getCADFailMaxDuration() const {
|
||||
return _prefs.cad_max_duration_ms != 0
|
||||
? _prefs.cad_max_duration_ms
|
||||
: mesh::Dispatcher::getCADFailMaxDuration();
|
||||
}
|
||||
|
||||
int MyMesh::getInterferenceThreshold() const {
|
||||
@@ -1477,6 +1494,10 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
|
||||
_prefs.wifi_enabled = 1;
|
||||
memset(_prefs.bluetooth_name, 0, sizeof(_prefs.bluetooth_name));
|
||||
_prefs.display_rotation_degrees = 0;
|
||||
_prefs.cad_enabled = DEFAULT_CAD_ENABLED ? 1 : 0;
|
||||
_prefs.cad_scan_timeout_ms = 0;
|
||||
_prefs.cad_retry_delay_ms = 0;
|
||||
_prefs.cad_max_duration_ms = 0;
|
||||
#if defined(ENABLE_USB_INTERFACE)
|
||||
// Keep a USB Companion's primary stream exclusively framed on a fresh
|
||||
// install. Full dual-CDC builds can add a diagnostics port; single-TTY
|
||||
@@ -1601,6 +1622,12 @@ void MyMesh::begin(bool has_display, bool radio_available) {
|
||||
_prefs.powersaving_enabled = constrain(_prefs.powersaving_enabled, 0, 1);
|
||||
_prefs.wifi_enabled = constrain(_prefs.wifi_enabled, 0, 1);
|
||||
_prefs.usb_logging_enabled = constrain(_prefs.usb_logging_enabled, 0, 1);
|
||||
_prefs.cad_enabled = constrain(_prefs.cad_enabled, 0, 1);
|
||||
if (_prefs.cad_scan_timeout_ms != 0
|
||||
&& (_prefs.cad_scan_timeout_ms < mesh::CAD_SCAN_MIN_TIMEOUT_MS
|
||||
|| _prefs.cad_scan_timeout_ms > mesh::CAD_SCAN_MAX_TIMEOUT_MS)) {
|
||||
_prefs.cad_scan_timeout_ms = 0;
|
||||
}
|
||||
_prefs.rx_ps_level = constrain(_prefs.rx_ps_level, 0, 10);
|
||||
if (_prefs.rx_ps_preamble != 16 && _prefs.rx_ps_preamble != 32) {
|
||||
_prefs.rx_ps_preamble = 0;
|
||||
@@ -1717,6 +1744,8 @@ void MyMesh::configureRadioFromPrefs() {
|
||||
}
|
||||
|
||||
saved_radio_apply_pending = !applySavedRadioParams();
|
||||
radio_driver.setCADScanTimeoutMillis(_prefs.cad_scan_timeout_ms);
|
||||
_radio->setCADEnabled(_prefs.cad_enabled != 0);
|
||||
if (!saved_radio_apply_pending) {
|
||||
radio_driver.setTxPower(_prefs.tx_power_dbm);
|
||||
radio_driver.setRxBoostedGainMode(_prefs.rx_boosted_gain);
|
||||
@@ -1868,11 +1897,144 @@ void MyMesh::scheduleNormalRadio(char* reply, size_t reply_size) {
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool parseCadTimingMillis(const char* text, uint32_t minimum,
|
||||
uint32_t maximum, uint16_t& result) {
|
||||
if (strcmp(text, "auto") == 0 || strcmp(text, "0") == 0) {
|
||||
result = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
char* end = NULL;
|
||||
const unsigned long parsed = strtoul(text, &end, 10);
|
||||
if (text[0] == 0 || end == NULL || *end != 0
|
||||
|| parsed < minimum || parsed > maximum) {
|
||||
return false;
|
||||
}
|
||||
result = (uint16_t)parsed;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MyMesh::handleCadCommand(const char* command, char* reply,
|
||||
size_t reply_size) {
|
||||
if (command == NULL || reply == NULL || reply_size == 0) return false;
|
||||
|
||||
if (strcmp(command, "get radio.cad") == 0
|
||||
|| strcmp(command, "get cad") == 0) {
|
||||
char scan[32];
|
||||
char retry[24];
|
||||
char maximum[24];
|
||||
if (_prefs.cad_scan_timeout_ms == 0) {
|
||||
uint32_t effective = _radio_available
|
||||
? radio_driver.getCADScanTimeoutMillis() : 0;
|
||||
if (effective == 0) {
|
||||
effective = mesh::calculateCadScanTimeoutMillis(_prefs.sf, _prefs.bw);
|
||||
}
|
||||
snprintf(scan, sizeof(scan), "auto(%lu)", (unsigned long)effective);
|
||||
} else {
|
||||
snprintf(scan, sizeof(scan), "%u", (unsigned)_prefs.cad_scan_timeout_ms);
|
||||
}
|
||||
if (_prefs.cad_retry_delay_ms == 0) {
|
||||
snprintf(retry, sizeof(retry), "auto");
|
||||
} else {
|
||||
snprintf(retry, sizeof(retry), "%u", (unsigned)_prefs.cad_retry_delay_ms);
|
||||
}
|
||||
if (_prefs.cad_max_duration_ms == 0) {
|
||||
snprintf(maximum, sizeof(maximum), "auto");
|
||||
} else {
|
||||
snprintf(maximum, sizeof(maximum), "%u", (unsigned)_prefs.cad_max_duration_ms);
|
||||
}
|
||||
snprintf(reply, reply_size,
|
||||
"radio.cad %s, scan=%s ms, retry=%s ms, max=%s ms",
|
||||
_prefs.cad_enabled ? "on" : "off", scan, retry, maximum);
|
||||
return true;
|
||||
}
|
||||
|
||||
const char* value = NULL;
|
||||
if (strncmp(command, "set radio.cad", 13) == 0
|
||||
&& (command[13] == 0 || command[13] == ' '
|
||||
|| command[13] == '\t')) {
|
||||
value = command + 13;
|
||||
} else if (strncmp(command, "set cad", 7) == 0
|
||||
&& (command[7] == 0 || command[7] == ' '
|
||||
|| command[7] == '\t')) {
|
||||
value = command + 7;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
while (*value == ' ' || *value == '\t') value++;
|
||||
|
||||
if (strcmp(value, "on") == 0 || strcmp(value, "off") == 0) {
|
||||
const uint8_t previous = _prefs.cad_enabled;
|
||||
_prefs.cad_enabled = strcmp(value, "on") == 0 ? 1 : 0;
|
||||
if (_radio_available) _radio->setCADEnabled(_prefs.cad_enabled != 0);
|
||||
if (!savePrefs()) {
|
||||
_prefs.cad_enabled = previous;
|
||||
if (_radio_available) _radio->setCADEnabled(previous != 0);
|
||||
snprintf(reply, reply_size, "Error: CAD changed but save failed");
|
||||
} else {
|
||||
snprintf(reply, reply_size, "OK - radio.cad %s", value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strncmp(value, "timings", 7) == 0
|
||||
&& (value[7] == 0 || value[7] == ' ' || value[7] == '\t')) {
|
||||
value += 7;
|
||||
while (*value == ' ' || *value == '\t') value++;
|
||||
char scan_text[16];
|
||||
char retry_text[16];
|
||||
char max_text[16];
|
||||
char extra[2];
|
||||
uint16_t scan_ms;
|
||||
uint16_t retry_ms;
|
||||
uint16_t max_ms;
|
||||
const int fields = sscanf(value, "%15s %15s %15s %1s",
|
||||
scan_text, retry_text, max_text, extra);
|
||||
if (fields != 3
|
||||
|| !parseCadTimingMillis(scan_text,
|
||||
mesh::CAD_SCAN_MIN_TIMEOUT_MS,
|
||||
mesh::CAD_SCAN_MAX_TIMEOUT_MS, scan_ms)
|
||||
|| !parseCadTimingMillis(retry_text, 1, 60000, retry_ms)
|
||||
|| !parseCadTimingMillis(max_text, 1, 60000, max_ms)) {
|
||||
snprintf(reply, reply_size,
|
||||
"Error: use set radio.cad timings <auto|100-3500> <auto|1-60000> <auto|1-60000>");
|
||||
return true;
|
||||
}
|
||||
|
||||
const uint16_t previous_scan = _prefs.cad_scan_timeout_ms;
|
||||
const uint16_t previous_retry = _prefs.cad_retry_delay_ms;
|
||||
const uint16_t previous_max = _prefs.cad_max_duration_ms;
|
||||
if (_radio_available && !radio_driver.setCADScanTimeoutMillis(scan_ms)) {
|
||||
snprintf(reply, reply_size, "Error: CAD scan timing is unsupported");
|
||||
return true;
|
||||
}
|
||||
_prefs.cad_scan_timeout_ms = scan_ms;
|
||||
_prefs.cad_retry_delay_ms = retry_ms;
|
||||
_prefs.cad_max_duration_ms = max_ms;
|
||||
if (!savePrefs()) {
|
||||
_prefs.cad_scan_timeout_ms = previous_scan;
|
||||
_prefs.cad_retry_delay_ms = previous_retry;
|
||||
_prefs.cad_max_duration_ms = previous_max;
|
||||
if (_radio_available) radio_driver.setCADScanTimeoutMillis(previous_scan);
|
||||
snprintf(reply, reply_size, "Error: CAD timings changed but save failed");
|
||||
} else {
|
||||
snprintf(reply, reply_size, "OK - radio.cad timings saved");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
snprintf(reply, reply_size,
|
||||
"Error: use set radio.cad <on|off> or set radio.cad timings <scan_ms|auto> <retry_ms|auto> <max_ms|auto>");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MyMesh::handleLocalControlCommand(const char* command, char* reply,
|
||||
size_t reply_size) {
|
||||
if (!command || !reply || reply_size == 0) return false;
|
||||
while (*command == ' ') command++;
|
||||
|
||||
if (handleCadCommand(command, reply, reply_size)) return true;
|
||||
|
||||
if (strcmp(command, "get display.rotation") == 0) {
|
||||
if (_ui == NULL || !_ui->supportsDisplayRotation()) {
|
||||
snprintf(reply, reply_size, "Error: display rotation is unsupported");
|
||||
@@ -2453,6 +2615,7 @@ void MyMesh::onConfigBatchEnd() {
|
||||
|
||||
void MyMesh::execCommand(char* cmd, char* reply) {
|
||||
reply[0] = 0;
|
||||
if (handleCadCommand(cmd, reply, 160)) return;
|
||||
if (cmd && (strcmp(cmd, "get bluetooth.name") == 0
|
||||
|| strcmp(cmd, "get ble.name") == 0)) {
|
||||
formatBluetoothNameStatus(reply, 160);
|
||||
@@ -5311,8 +5474,9 @@ void MyMesh::handleTerminalCommand(char* command) {
|
||||
#if MESH_USB_LOGGING_AVAILABLE
|
||||
} else if (strcmp(command, "get usb.logging") == 0) {
|
||||
terminalOutput().printf(
|
||||
" usb.logging %s%s\r\n",
|
||||
" usb.logging %s; port: %s%s\r\n",
|
||||
mesh::isUsbLoggingEnabled() ? "on" : "off",
|
||||
mesh::usbLoggingPortDescription(),
|
||||
mesh::usbLoggingInterfaceRestartRequired()
|
||||
? " (reboot required to change USB interfaces)" : "");
|
||||
#endif
|
||||
@@ -5584,6 +5748,9 @@ void MyMesh::handleTerminalCommand(char* command) {
|
||||
terminalOutput().print(" get radio.rxps\r\n");
|
||||
terminalOutput().print(" get radio.rxps.config\r\n");
|
||||
terminalOutput().print(" set radio.rxps <off|on|level 1-10 [preamble 16|32]|rx_us sleep_us>\r\n");
|
||||
terminalOutput().print(" get radio.cad\r\n");
|
||||
terminalOutput().print(" set radio.cad <on|off>\r\n");
|
||||
terminalOutput().print(" set radio.cad timings <scan_ms|auto> <retry_ms|auto> <max_ms|auto>\r\n");
|
||||
terminalOutput().print(" get radio.rxgain\r\n");
|
||||
terminalOutput().print(" set radio.rxgain <on|off>\r\n");
|
||||
terminalOutput().print(" get radio.fem.rxgain\r\n");
|
||||
|
||||
@@ -198,6 +198,8 @@ protected:
|
||||
float getAirtimeBudgetFactor() const override;
|
||||
int getInterferenceThreshold() const override;
|
||||
bool getCADEnabled() const override;
|
||||
uint32_t getCADFailRetryDelay() const override;
|
||||
uint32_t getCADFailMaxDuration() const override;
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
uint32_t getRadioWatchdogMillis() const override { return 0; }
|
||||
#endif
|
||||
@@ -315,6 +317,7 @@ private:
|
||||
bool applyAndSaveFemRxGain(bool enabled);
|
||||
bool applyAndSaveFemTxGain(bool enabled);
|
||||
bool applyAndSaveRxBoostedGain(bool enabled);
|
||||
bool handleCadCommand(const char* command, char* reply, size_t reply_size);
|
||||
bool saveBluetoothNameOverride(const char* name);
|
||||
bool applyAndSaveBluetoothName(const char* value, char* reply,
|
||||
size_t reply_size);
|
||||
|
||||
@@ -58,6 +58,10 @@ struct CompanionNodePrefs { // persisted to file
|
||||
char bluetooth_name[mesh::companion::BLUETOOTH_NAME_SIZE];
|
||||
// exact BLE name; empty uses BLE_NAME_PREFIX + node_name
|
||||
uint16_t display_rotation_degrees; // 0=board default; otherwise 90/180/270
|
||||
uint8_t cad_enabled; // hardware channel activity detection
|
||||
uint16_t cad_scan_timeout_ms; // 0=derive from SF/BW
|
||||
uint16_t cad_retry_delay_ms; // 0=Mesh adaptive default
|
||||
uint16_t cad_max_duration_ms; // 0=Dispatcher default
|
||||
|
||||
// Keep the upstream repeat API while retaining the existing binary prefs
|
||||
// layout used by this branch.
|
||||
|
||||
@@ -81,12 +81,13 @@ MultiSerialInterface interface_manager;
|
||||
#if defined(ENABLE_USB_INTERFACE)
|
||||
#include <helpers/ArduinoSerialInterface.h>
|
||||
#include <helpers/CLICommandUtils.h>
|
||||
#include <helpers/UsbAsciiBinarySwitch.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
|
||||
// motatool sends this command automatically when `serve --serial` opens the
|
||||
// port. In Binary mode it is an exact, idle-parser control sequence that
|
||||
// hands USB ownership to the host-backed mOTA source.
|
||||
// port. It hands USB ownership to the host-backed mOTA source from either
|
||||
// the completed ASCII line or Binary mode's idle control-sequence parser.
|
||||
static const char USB_MOTA_START_TOKEN[] = "ota folder on";
|
||||
#endif
|
||||
ArduinoSerialInterface usb_serial_interface;
|
||||
@@ -405,6 +406,9 @@ static size_t usb_terminal_line_len = 0;
|
||||
static bool usb_terminal_discard_line = false;
|
||||
static bool usb_terminal_disconnect_armed = false;
|
||||
static bool usb_logging_terminal_mode = false;
|
||||
#if defined(COMPANION_RADIO_FULL)
|
||||
static mesh::UsbBinaryStartupProbe usb_binary_startup_probe;
|
||||
#endif
|
||||
#if COMPANION_FEATURE_USB_MOTA_SOURCE
|
||||
static bool usb_mota_mode = false;
|
||||
static char usb_mota_line[32];
|
||||
@@ -453,7 +457,22 @@ static bool isUsbTerminalDataConnected() {
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool hasObservableActiveUsbTerminalClient() {
|
||||
#if defined(ESP32) && defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE == 1 \
|
||||
&& defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT
|
||||
// HWCDC exposes only whether USB is plugged in, not whether a host process
|
||||
// has opened the terminal. Treat actual buffered activity as ownership; an
|
||||
// idle physical cable must not permanently lock TCP port 5002.
|
||||
return usb_terminal_line_len != 0 || usb_terminal_discard_line;
|
||||
#else
|
||||
return isUsbTerminalDataConnected();
|
||||
#endif
|
||||
}
|
||||
|
||||
static void enterUsbTerminalMode() {
|
||||
#if defined(COMPANION_RADIO_FULL)
|
||||
usb_binary_startup_probe.cancel();
|
||||
#endif
|
||||
the_mesh.cancelSerialResponseStream();
|
||||
usb_serial_interface.setPassthroughMode(true);
|
||||
clearUsbTerminalLine();
|
||||
@@ -500,7 +519,7 @@ static void leaveUsbMotaMode(bool acknowledge) {
|
||||
resetUsbMotaMode();
|
||||
}
|
||||
|
||||
static void enterUsbMotaMode() {
|
||||
static bool enterUsbMotaMode(mesh::UsbMotaEntryOrigin origin) {
|
||||
the_mesh.cancelSerialResponseStream();
|
||||
usb_serial_interface.setPassthroughMode(true);
|
||||
usb_mota_mode = true;
|
||||
@@ -515,11 +534,15 @@ static void enterUsbMotaMode() {
|
||||
Serial.print(reply[0] ? reply : "ERR could not enter mOTA seeder mode");
|
||||
Serial.print("\r\n");
|
||||
resetUsbMotaMode();
|
||||
return;
|
||||
if (mesh::shouldRestoreAsciiAfterMotaFailure(origin)) {
|
||||
enterUsbTerminalMode();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Serial.print("\r\n");
|
||||
Serial.print(reply);
|
||||
Serial.print("\r\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
static void serviceUsbMota() {
|
||||
@@ -588,11 +611,22 @@ static void serviceUsbTerminal() {
|
||||
}
|
||||
#endif
|
||||
if (!the_mesh.isTerminalMode()) {
|
||||
#if defined(COMPANION_RADIO_FULL)
|
||||
const mesh::UsbBinaryStartupProbe::Result probe_result =
|
||||
usb_binary_startup_probe.poll(
|
||||
millis(), usb_serial_interface.getCompletedFrameCount(),
|
||||
usb_serial_interface.getLastFrameMillis());
|
||||
if (probe_result
|
||||
== mesh::UsbBinaryStartupProbe::Result::RETURN_TO_ASCII) {
|
||||
enterUsbTerminalMode();
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
if (usb_serial_interface.takeControlSequence()) {
|
||||
enterUsbTerminalMode();
|
||||
#if COMPANION_FEATURE_USB_MOTA_SOURCE
|
||||
} else if (usb_serial_interface.takeSecondaryControlSequence()) {
|
||||
enterUsbMotaMode();
|
||||
enterUsbMotaMode(mesh::UsbMotaEntryOrigin::BINARY);
|
||||
#endif
|
||||
}
|
||||
return;
|
||||
@@ -605,6 +639,21 @@ static void serviceUsbTerminal() {
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(COMPANION_RADIO_FULL)
|
||||
// Full Companion boots as a useful ASCII terminal. MeshCLI's first framed
|
||||
// command begins with '<'; hand that byte over untouched at an empty prompt.
|
||||
// A malformed or accidental probe times out and restores the terminal.
|
||||
if (!usb_logging_terminal_mode
|
||||
&& usb_binary_startup_probe.shouldStart(
|
||||
usb_terminal_line_len == 0, usb_terminal_discard_line,
|
||||
Serial.peek())) {
|
||||
const uint32_t frame_count = usb_serial_interface.getCompletedFrameCount();
|
||||
leaveUsbTerminalMode(false);
|
||||
usb_binary_startup_probe.start(millis(), frame_count);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
while (Serial.available()) {
|
||||
int value = Serial.read();
|
||||
if (value < 0) break;
|
||||
@@ -630,6 +679,18 @@ static void serviceUsbTerminal() {
|
||||
if (c == '\r' || c == '\n') {
|
||||
if (usb_terminal_line_len == 0) continue;
|
||||
Serial.print("\r\n");
|
||||
#if COMPANION_FEATURE_USB_MOTA_SOURCE
|
||||
// motatool is deliberately text-first: `serve --serial` opens the port
|
||||
// and sends this command before its binary mOTA request/reply traffic.
|
||||
// Full Companion now boots in ASCII, so transfer ownership directly
|
||||
// instead of letting the ordinary terminal command handler leave the
|
||||
// stream in line-oriented mode.
|
||||
if (strcmp(usb_terminal_line, USB_MOTA_START_TOKEN) == 0) {
|
||||
leaveUsbTerminalMode(false);
|
||||
enterUsbMotaMode(mesh::UsbMotaEntryOrigin::ASCII);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
the_mesh.handleTerminalCommand(usb_terminal_line);
|
||||
clearUsbTerminalLine();
|
||||
#if MESH_USB_LOGGING_AVAILABLE
|
||||
@@ -661,6 +722,22 @@ static void serviceUsbTerminal() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(COMPANION_RADIO_FULL)
|
||||
static void expireUsbBinaryStartupProbeBeforeDispatch() {
|
||||
const uint32_t now = millis();
|
||||
if (!usb_binary_startup_probe.hasTimedOut(now)) return;
|
||||
|
||||
// The dispatcher normally consumes Binary Companion input before the ASCII
|
||||
// terminal service runs. Enforce the advertised deadline here so bytes that
|
||||
// are still incomplete at one second cannot complete a frame afterwards.
|
||||
// Entering passthrough resets the partial binary parser; drain only bytes
|
||||
// already queued for that expired attempt so they cannot begin a new probe.
|
||||
int pending = Serial.available();
|
||||
while (pending-- > 0) Serial.read();
|
||||
enterUsbTerminalMode();
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void halt() {
|
||||
@@ -886,6 +963,9 @@ void halt() {
|
||||
static char ota_console_line[MAX_TRANS_UNIT * 2 + 32];
|
||||
static size_t ota_console_len = 0;
|
||||
static bool ota_console_discard_line = false;
|
||||
#if COMPANION_FEATURE_NETWORK_TERMINAL && defined(ENABLE_USB_INTERFACE)
|
||||
static mesh::UsbTcpTerminalHandoff ota_console_usb_handoff;
|
||||
#endif
|
||||
|
||||
static void ota_console_clear_line() {
|
||||
memset(ota_console_line, 0, sizeof(ota_console_line));
|
||||
@@ -903,9 +983,21 @@ void halt() {
|
||||
#endif
|
||||
}
|
||||
|
||||
#if COMPANION_FEATURE_NETWORK_TERMINAL
|
||||
static void ota_console_release_terminal() {
|
||||
the_mesh.exitNetworkTerminalMode(ota_console_client);
|
||||
#if defined(ENABLE_USB_INTERFACE)
|
||||
if (ota_console_usb_handoff.shouldRestoreAscii(
|
||||
usb_serial_interface.getCompletedFrameCount())) {
|
||||
enterUsbTerminalMode();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
static void ota_console_stop() {
|
||||
#if COMPANION_FEATURE_NETWORK_TERMINAL
|
||||
the_mesh.exitNetworkTerminalMode(ota_console_client);
|
||||
ota_console_release_terminal();
|
||||
#endif
|
||||
if (ota_console_client) ota_console_client.stop();
|
||||
ota_console_server.end();
|
||||
@@ -916,7 +1008,7 @@ void halt() {
|
||||
static void ota_console_loop() {
|
||||
if (!ota_console_client || !ota_console_client.connected()) {
|
||||
#if COMPANION_FEATURE_NETWORK_TERMINAL
|
||||
the_mesh.exitNetworkTerminalMode(ota_console_client);
|
||||
ota_console_release_terminal();
|
||||
#endif
|
||||
WiFiClient c = ota_console_server.available();
|
||||
if (c) {
|
||||
@@ -924,9 +1016,31 @@ void halt() {
|
||||
ota_console_clear_line();
|
||||
ota_console_discard_line = false;
|
||||
#if COMPANION_FEATURE_NETWORK_TERMINAL
|
||||
if (!the_mesh.enterNetworkTerminalMode(ota_console_client)) {
|
||||
#if defined(ENABLE_USB_INTERFACE)
|
||||
const bool usb_ascii_selected = the_mesh.isTerminalMode();
|
||||
const bool usb_input_idle = usb_terminal_line_len == 0
|
||||
&& !usb_terminal_discard_line
|
||||
&& !usb_binary_startup_probe.isActive();
|
||||
if (!ota_console_usb_handoff.begin(
|
||||
usb_ascii_selected, hasObservableActiveUsbTerminalClient(),
|
||||
usb_input_idle,
|
||||
usb_serial_interface.getCompletedFrameCount())) {
|
||||
ota_console_client.print(
|
||||
"ERROR: USB currently owns the Full Companion terminal\r\n");
|
||||
"ERROR: active USB currently owns the Full Companion terminal\r\n");
|
||||
ota_console_client.stop();
|
||||
return;
|
||||
}
|
||||
if (usb_ascii_selected) leaveUsbTerminalMode(false);
|
||||
#endif
|
||||
if (!the_mesh.enterNetworkTerminalMode(ota_console_client)) {
|
||||
#if defined(ENABLE_USB_INTERFACE)
|
||||
if (ota_console_usb_handoff.shouldRestoreAscii(
|
||||
usb_serial_interface.getCompletedFrameCount())) {
|
||||
enterUsbTerminalMode();
|
||||
}
|
||||
#endif
|
||||
ota_console_client.print(
|
||||
"ERROR: another client currently owns the Full Companion terminal\r\n");
|
||||
ota_console_client.stop();
|
||||
}
|
||||
#else
|
||||
@@ -937,6 +1051,9 @@ void halt() {
|
||||
}
|
||||
#if COMPANION_FEATURE_NETWORK_TERMINAL
|
||||
if (!the_mesh.isNetworkTerminalMode(ota_console_client)) {
|
||||
#if defined(ENABLE_USB_INTERFACE)
|
||||
ota_console_usb_handoff.cancel();
|
||||
#endif
|
||||
ota_console_client.print(
|
||||
"\r\nERROR: terminal ownership moved to USB; closing\r\n");
|
||||
ota_console_client.stop();
|
||||
@@ -960,7 +1077,7 @@ void halt() {
|
||||
#if COMPANION_FEATURE_NETWORK_TERMINAL
|
||||
if (strcmp(ota_console_line, "disconnect") == 0) {
|
||||
ota_console_client.print(" OK - disconnecting\r\n");
|
||||
the_mesh.exitNetworkTerminalMode(ota_console_client);
|
||||
ota_console_release_terminal();
|
||||
ota_console_client.stop();
|
||||
ota_console_clear_line();
|
||||
ota_console_discard_line = false;
|
||||
@@ -1401,6 +1518,13 @@ void setup() {
|
||||
enterUsbLoggingTerminalMode();
|
||||
}
|
||||
#endif
|
||||
#if defined(COMPANION_RADIO_FULL)
|
||||
if (!the_mesh.isTerminalMode()) {
|
||||
// Full Companion's primary USB port is an ASCII CLI until a Companion
|
||||
// client presents a valid binary frame. BLE/WiFi/Ethernet stay binary.
|
||||
enterUsbTerminalMode();
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// add ethernet interface
|
||||
@@ -1449,6 +1573,12 @@ void setup() {
|
||||
void loop() {
|
||||
#if defined(NRF52_PLATFORM)
|
||||
board.feedWatchdog();
|
||||
#endif
|
||||
// Identify CDC 1 when a terminal opens it. Doing this on the connection edge
|
||||
// avoids losing the marker during boot before the host has opened the port.
|
||||
mesh::serviceUsbLoggingPort();
|
||||
#if defined(ENABLE_USB_INTERFACE) && defined(COMPANION_RADIO_FULL)
|
||||
expireUsbBinaryStartupProbeBeforeDispatch();
|
||||
#endif
|
||||
the_mesh.loop();
|
||||
#ifdef RECOVERABLE_EXTERNAL_RADIO
|
||||
|
||||
Reference in new issue
Block a user