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
@@ -107,6 +107,11 @@ public:
|
||||
|
||||
virtual void setCADEnabled(bool enable) { }
|
||||
|
||||
// LoRa wrappers shadow these with their configurable CAD timeout. Other
|
||||
// transports (such as ESP-NOW) support only the automatic/default value.
|
||||
bool setCADScanTimeoutMillis(uint32_t timeout_ms) { return timeout_ms == 0; }
|
||||
uint32_t getCADScanTimeoutMillis() const { return 0; }
|
||||
|
||||
virtual void resetAGC() { }
|
||||
|
||||
virtual uint8_t getRadioState() const { return 0; }
|
||||
|
||||
@@ -6,10 +6,18 @@
|
||||
#define RECV_STATE_LEN1_FOUND 2
|
||||
#define RECV_STATE_LEN2_FOUND 3
|
||||
|
||||
void ArduinoSerialInterface::resetReceiveState() {
|
||||
_state = RECV_STATE_IDLE;
|
||||
void ArduinoSerialInterface::resetControlSequenceState() {
|
||||
_controlSequencePos = 0;
|
||||
_secondaryControlSequencePos = 0;
|
||||
_controlSequenceCandidate =
|
||||
_controlSequence != nullptr && _controlSequence[0] != 0;
|
||||
_secondaryControlSequenceCandidate =
|
||||
_secondaryControlSequence != nullptr && _secondaryControlSequence[0] != 0;
|
||||
}
|
||||
|
||||
void ArduinoSerialInterface::resetReceiveState() {
|
||||
_state = RECV_STATE_IDLE;
|
||||
resetControlSequenceState();
|
||||
_frame_len = 0;
|
||||
rx_len = 0;
|
||||
_last_rx_byte_ms = 0;
|
||||
@@ -111,23 +119,33 @@ void ArduinoSerialInterface::serviceTransmit() {
|
||||
}
|
||||
}
|
||||
|
||||
bool ArduinoSerialInterface::checkControlSequence(uint8_t c,
|
||||
const char* sequence,
|
||||
size_t& position,
|
||||
bool& received) {
|
||||
if (sequence == nullptr || sequence[0] == 0) return false;
|
||||
bool ArduinoSerialInterface::checkControlLineByte(uint8_t c) {
|
||||
if (c == '\r' || c == '\n') {
|
||||
const bool primary = _controlSequenceCandidate
|
||||
&& _controlSequence[_controlSequencePos] == 0;
|
||||
const bool secondary = _secondaryControlSequenceCandidate
|
||||
&& _secondaryControlSequence[_secondaryControlSequencePos] == 0;
|
||||
resetControlSequenceState();
|
||||
if (primary) _controlSequenceReceived = true;
|
||||
if (secondary) _secondaryControlSequenceReceived = true;
|
||||
return primary || secondary;
|
||||
}
|
||||
|
||||
if (c == (uint8_t)sequence[position]) {
|
||||
position++;
|
||||
if (sequence[position] == 0) {
|
||||
position = 0;
|
||||
received = true;
|
||||
return true;
|
||||
if (_controlSequenceCandidate) {
|
||||
if (_controlSequence[_controlSequencePos] != 0
|
||||
&& c == (uint8_t)_controlSequence[_controlSequencePos]) {
|
||||
++_controlSequencePos;
|
||||
} else {
|
||||
_controlSequenceCandidate = false;
|
||||
}
|
||||
}
|
||||
if (_secondaryControlSequenceCandidate) {
|
||||
if (_secondaryControlSequence[_secondaryControlSequencePos] != 0
|
||||
&& c == (uint8_t)_secondaryControlSequence[_secondaryControlSequencePos]) {
|
||||
++_secondaryControlSequencePos;
|
||||
} else {
|
||||
_secondaryControlSequenceCandidate = false;
|
||||
}
|
||||
} else {
|
||||
// Preserve a possible new match when this byte is also the first byte of
|
||||
// the sequence (notably useful for sequences beginning with "+++").
|
||||
position = c == (uint8_t)sequence[0] ? 1 : 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -235,11 +253,7 @@ size_t ArduinoSerialInterface::checkRecvFrame(uint8_t dest[]) {
|
||||
|
||||
switch (_state) {
|
||||
case RECV_STATE_IDLE:
|
||||
if (checkControlSequence((uint8_t)c, _controlSequence,
|
||||
_controlSequencePos, _controlSequenceReceived)
|
||||
|| checkControlSequence((uint8_t)c, _secondaryControlSequence,
|
||||
_secondaryControlSequencePos,
|
||||
_secondaryControlSequenceReceived)) {
|
||||
if (checkControlLineByte((uint8_t)c)) {
|
||||
// Leave any following bytes buffered for the passthrough consumer.
|
||||
return 0;
|
||||
}
|
||||
@@ -255,6 +269,7 @@ size_t ArduinoSerialInterface::checkRecvFrame(uint8_t dest[]) {
|
||||
_frame_len |= ((uint16_t)c) << 8; // MSB
|
||||
rx_len = 0;
|
||||
_state = _frame_len > 0 ? RECV_STATE_LEN2_FOUND : RECV_STATE_IDLE;
|
||||
if (_state == RECV_STATE_IDLE) resetControlSequenceState();
|
||||
break;
|
||||
default:
|
||||
if (rx_len < MAX_FRAME_SIZE) {
|
||||
@@ -265,7 +280,9 @@ size_t ArduinoSerialInterface::checkRecvFrame(uint8_t dest[]) {
|
||||
if (_frame_len > MAX_FRAME_SIZE) _frame_len = MAX_FRAME_SIZE; // truncate
|
||||
memcpy(dest, rx_buf, _frame_len);
|
||||
_state = RECV_STATE_IDLE; // reset state, for next frame
|
||||
resetControlSequenceState();
|
||||
_last_frame_ms = millis(); // a real client is talking to us
|
||||
++_completed_frame_count;
|
||||
_has_received_frame = true;
|
||||
return _frame_len;
|
||||
}
|
||||
|
||||
@@ -25,9 +25,12 @@ private:
|
||||
uint8_t _state;
|
||||
size_t _controlSequencePos;
|
||||
size_t _secondaryControlSequencePos;
|
||||
bool _controlSequenceCandidate;
|
||||
bool _secondaryControlSequenceCandidate;
|
||||
uint16_t _frame_len;
|
||||
uint16_t rx_len;
|
||||
uint32_t _last_frame_ms;
|
||||
uint32_t _completed_frame_count;
|
||||
uint32_t _last_rx_byte_ms;
|
||||
bool _has_received_frame;
|
||||
Stream* _serial;
|
||||
@@ -39,8 +42,8 @@ private:
|
||||
uint8_t _tx_queue_len;
|
||||
uint16_t _tx_offset;
|
||||
|
||||
bool checkControlSequence(uint8_t c, const char* sequence,
|
||||
size_t& position, bool& received);
|
||||
void resetControlSequenceState();
|
||||
bool checkControlLineByte(uint8_t c);
|
||||
void resetReceiveState();
|
||||
void serviceReceiveTimeout();
|
||||
void resetTransmitState();
|
||||
@@ -52,8 +55,10 @@ public:
|
||||
: _isEnabled(false), _passthroughMode(false),
|
||||
_controlSequenceReceived(false), _secondaryControlSequenceReceived(false),
|
||||
_flow_ctl(false), _state(0), _controlSequencePos(0),
|
||||
_secondaryControlSequencePos(0), _frame_len(0), rx_len(0),
|
||||
_last_frame_ms(0), _last_rx_byte_ms(0), _has_received_frame(false),
|
||||
_secondaryControlSequencePos(0), _controlSequenceCandidate(false),
|
||||
_secondaryControlSequenceCandidate(false), _frame_len(0), rx_len(0),
|
||||
_last_frame_ms(0), _completed_frame_count(0), _last_rx_byte_ms(0),
|
||||
_has_received_frame(false),
|
||||
_serial(nullptr), _controlSequence(nullptr),
|
||||
_secondaryControlSequence(nullptr), _conn_check(nullptr),
|
||||
_tx_queue_len(0), _tx_offset(0) {}
|
||||
@@ -67,6 +72,7 @@ public:
|
||||
_controlSequenceReceived = false;
|
||||
_secondaryControlSequenceReceived = false;
|
||||
_last_frame_ms = 0;
|
||||
_completed_frame_count = 0;
|
||||
_has_received_frame = false;
|
||||
resetReceiveState();
|
||||
resetTransmitState();
|
||||
@@ -93,6 +99,7 @@ public:
|
||||
// Useful as an activity-based connection check where no DTR state exists.
|
||||
uint32_t getLastFrameMillis() const { return _last_frame_ms; }
|
||||
bool hasReceivedFrame() const { return _has_received_frame; }
|
||||
uint32_t getCompletedFrameCount() const { return _completed_frame_count; }
|
||||
|
||||
// Optional: queue complete frames and drain one at a time according to the
|
||||
// stream's TX capacity, so short writes cannot discard or interleave bytes.
|
||||
|
||||
@@ -29,6 +29,34 @@ static bool is_value_char(char c) {
|
||||
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || c == '-' || c == '.';
|
||||
}
|
||||
|
||||
// ConfigSerializer writes floating-point preferences as fixed-point decimal
|
||||
// text, and its tokenizer intentionally accepts neither exponent notation nor
|
||||
// a leading '+'. Parsing that small grammar locally avoids linking Newlib's
|
||||
// general-purpose strtod implementation (and its locale/bignum support) into
|
||||
// flash-constrained firmware.
|
||||
static double parse_fixed_decimal(const char* text) {
|
||||
bool negative = false;
|
||||
if (*text == '-') {
|
||||
negative = true;
|
||||
++text;
|
||||
}
|
||||
|
||||
double value = 0.0;
|
||||
while (*text >= '0' && *text <= '9') {
|
||||
value = value * 10.0 + (*text++ - '0');
|
||||
}
|
||||
|
||||
if (*text == '.') {
|
||||
double place = 0.1;
|
||||
while (*++text >= '0' && *text <= '9') {
|
||||
value += (*text - '0') * place;
|
||||
place *= 0.1;
|
||||
}
|
||||
}
|
||||
|
||||
return negative ? -value : value;
|
||||
}
|
||||
|
||||
#define EXPECT_OPEN_BRACE 0
|
||||
#define EXPECT_KEY 1
|
||||
#define EXPECT_VAL_OR_OBJ 2
|
||||
@@ -290,7 +318,7 @@ void ConfigSerializer::def(const char* key, double& value) {
|
||||
}
|
||||
} else {
|
||||
if (_context->keyMatch(_depth, key)) {
|
||||
value = atof(_context->getToken());
|
||||
value = parse_fixed_decimal(_context->getToken());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -307,7 +335,7 @@ void ConfigSerializer::def(const char* key, float& value) {
|
||||
}
|
||||
} else {
|
||||
if (_context->keyMatch(_depth, key)) {
|
||||
value = (float) atof(_context->getToken());
|
||||
value = (float) parse_fixed_decimal(_context->getToken());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace mesh {
|
||||
|
||||
// Coordinates the Full Companion USB startup handoff without consuming the
|
||||
// '<' byte. ArduinoSerialInterface remains the only binary protocol parser.
|
||||
class UsbBinaryStartupProbe {
|
||||
public:
|
||||
enum class Result : uint8_t {
|
||||
INACTIVE,
|
||||
WAITING,
|
||||
BINARY_CONFIRMED,
|
||||
RETURN_TO_ASCII,
|
||||
};
|
||||
|
||||
static constexpr uint32_t TIMEOUT_MS = 1000;
|
||||
|
||||
private:
|
||||
bool _active = false;
|
||||
uint32_t _started_at = 0;
|
||||
uint32_t _frame_count_at_start = 0;
|
||||
|
||||
public:
|
||||
bool shouldStart(bool line_empty, bool discarding_line,
|
||||
int next_byte) const {
|
||||
return !_active && line_empty && !discarding_line && next_byte == '<';
|
||||
}
|
||||
|
||||
void start(uint32_t now, uint32_t completed_frame_count) {
|
||||
_active = true;
|
||||
_started_at = now;
|
||||
_frame_count_at_start = completed_frame_count;
|
||||
}
|
||||
|
||||
void cancel() { _active = false; }
|
||||
bool isActive() const { return _active; }
|
||||
|
||||
bool hasTimedOut(uint32_t now) const {
|
||||
return _active && (uint32_t)(now - _started_at) >= TIMEOUT_MS;
|
||||
}
|
||||
|
||||
Result poll(uint32_t now, uint32_t completed_frame_count,
|
||||
uint32_t completed_frame_at = 0) {
|
||||
if (!_active) return Result::INACTIVE;
|
||||
if (completed_frame_count != _frame_count_at_start) {
|
||||
_active = false;
|
||||
// A frame parsed before the deadline remains valid even if other mesh
|
||||
// work delays this poll. A frame completed at/after the boundary loses to
|
||||
// the timeout, which keeps the advertised one-second window strict.
|
||||
return (uint32_t)(completed_frame_at - _started_at) < TIMEOUT_MS
|
||||
? Result::BINARY_CONFIRMED : Result::RETURN_TO_ASCII;
|
||||
}
|
||||
if (hasTimedOut(now)) {
|
||||
_active = false;
|
||||
return Result::RETURN_TO_ASCII;
|
||||
}
|
||||
return Result::WAITING;
|
||||
}
|
||||
};
|
||||
|
||||
// Tracks the narrow case where TCP temporarily borrows an idle startup ASCII
|
||||
// terminal. A complete USB Binary frame during the TCP session wins ownership,
|
||||
// so closing TCP must not force that USB client back to ASCII.
|
||||
class UsbTcpTerminalHandoff {
|
||||
bool _restore_ascii = false;
|
||||
uint32_t _frame_count_at_start = 0;
|
||||
|
||||
public:
|
||||
bool begin(bool usb_ascii_selected, bool usb_data_connected,
|
||||
bool usb_input_idle, uint32_t completed_frame_count) {
|
||||
_restore_ascii = false;
|
||||
if (!usb_ascii_selected) return true;
|
||||
if (usb_data_connected || !usb_input_idle) return false;
|
||||
_restore_ascii = true;
|
||||
_frame_count_at_start = completed_frame_count;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool shouldRestoreAscii(uint32_t completed_frame_count) {
|
||||
const bool restore = _restore_ascii
|
||||
&& completed_frame_count == _frame_count_at_start;
|
||||
_restore_ascii = false;
|
||||
return restore;
|
||||
}
|
||||
|
||||
void cancel() { _restore_ascii = false; }
|
||||
bool isBorrowingAscii() const { return _restore_ascii; }
|
||||
};
|
||||
|
||||
enum class UsbMotaEntryOrigin : uint8_t { BINARY, ASCII };
|
||||
|
||||
inline bool shouldRestoreAsciiAfterMotaFailure(UsbMotaEntryOrigin origin) {
|
||||
return origin == UsbMotaEntryOrigin::ASCII;
|
||||
}
|
||||
|
||||
} // namespace mesh
|
||||
@@ -59,6 +59,7 @@ static NullUsbLoggingStream null_usb_logging_stream;
|
||||
#if defined(MESH_DUAL_CDC_LOGGING)
|
||||
static bool dedicated_usb_logging_port_configured = false;
|
||||
static bool dedicated_usb_logging_port_started = false;
|
||||
static bool dedicated_usb_logging_port_connected = false;
|
||||
#if defined(MESH_NRF52_DUAL_CDC_LOGGING)
|
||||
static Adafruit_USBD_CDC dedicated_usb_logging_port;
|
||||
#elif defined(MESH_ESP32_DUAL_CDC_LOGGING)
|
||||
@@ -162,6 +163,9 @@ void beginUsbLoggingPort() {
|
||||
dedicated_usb_logging_port->begin(115200);
|
||||
#elif defined(MESH_NRF52_DUAL_CDC_LOGGING)
|
||||
dedicated_usb_logging_port.begin(115200);
|
||||
// `begin()` installs the core's generic "TinyUSB Serial" name. Replace it
|
||||
// before (re-)enumeration so host USB tools can distinguish the log endpoint.
|
||||
dedicated_usb_logging_port.setStringDescriptor("MeshCore Logging");
|
||||
dedicated_usb_logging_port_configured = true;
|
||||
#endif
|
||||
dedicated_usb_logging_port_started = true;
|
||||
@@ -180,6 +184,28 @@ void beginUsbLoggingPort() {
|
||||
#endif
|
||||
}
|
||||
|
||||
void serviceUsbLoggingPort() {
|
||||
#if defined(MESH_DUAL_CDC_LOGGING)
|
||||
bool connected = false;
|
||||
#if defined(MESH_NRF52_DUAL_CDC_LOGGING)
|
||||
connected = dedicated_usb_logging_port_started
|
||||
&& dedicated_usb_logging_port.dtr();
|
||||
#elif defined(MESH_ESP32_DUAL_CDC_LOGGING)
|
||||
connected = dedicated_usb_logging_port_started
|
||||
&& dedicated_usb_logging_port != nullptr
|
||||
&& (bool)*dedicated_usb_logging_port;
|
||||
#endif
|
||||
|
||||
if (connected && !dedicated_usb_logging_port_connected
|
||||
&& isUsbLoggingEnabled()) {
|
||||
Stream& port = usbLoggingPort();
|
||||
port.println("MeshCore USB logging port");
|
||||
port.println("USB CDC 1; interface 02; Linux stable suffix: -if02");
|
||||
}
|
||||
dedicated_usb_logging_port_connected = connected;
|
||||
#endif
|
||||
}
|
||||
|
||||
Stream& usbLoggingPort() {
|
||||
#if defined(MESH_DUAL_CDC_LOGGING)
|
||||
#if defined(MESH_NRF52_DUAL_CDC_LOGGING)
|
||||
@@ -223,5 +249,13 @@ bool usbLoggingInterfaceRestartRequired() {
|
||||
#endif
|
||||
}
|
||||
|
||||
const char* usbLoggingPortDescription() {
|
||||
#if defined(MESH_DUAL_CDC_LOGGING)
|
||||
return "dedicated USB CDC 1, interface 02 (Linux: *-if02; tty/COM name is host-assigned)";
|
||||
#else
|
||||
return "primary USB serial port (tty/COM name is host-assigned)";
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace mesh
|
||||
#endif
|
||||
@@ -29,10 +29,15 @@ bool saveUsbLoggingBootPreference(bool enabled);
|
||||
// Start the optional dedicated USB logging interface. Ordinary and single-TTY
|
||||
// builds use Serial; dual-CDC Full Companion builds use a second CDC ACM port.
|
||||
void beginUsbLoggingPort();
|
||||
// Emit a short identity marker whenever a host opens the dedicated logging
|
||||
// endpoint. The device can report its USB interface, but the host alone chooses
|
||||
// names such as /dev/ttyACM1 or COM7.
|
||||
void serviceUsbLoggingPort();
|
||||
Stream& usbLoggingPort();
|
||||
bool hasDedicatedUsbLoggingPort();
|
||||
bool isDedicatedUsbLoggingPortConfigured();
|
||||
bool usbLoggingInterfaceRestartRequired();
|
||||
const char* usbLoggingPortDescription();
|
||||
|
||||
} // namespace mesh
|
||||
#endif
|
||||
@@ -153,7 +153,7 @@ protected:
|
||||
break;
|
||||
}
|
||||
scan_result = performChannelScanWithTimeout(
|
||||
mesh::calculateCadScanTimeoutMillis(scan_sfs[i], bw));
|
||||
cadScanTimeoutMillis(scan_sfs[i], bw));
|
||||
// Each individual CAD can finish inside the helper's one-second service
|
||||
// cadence while the combined multi-SF pass still exceeds it.
|
||||
_board->serviceWatchdog();
|
||||
|
||||
@@ -33,6 +33,7 @@ protected:
|
||||
int32_t _noise_floor_centi_dbm;
|
||||
float _last_rssi, _last_snr;
|
||||
bool _cad_enabled;
|
||||
uint32_t _cad_scan_timeout_override_ms;
|
||||
bool _noise_floor_valid;
|
||||
bool _nf_refresh_requested;
|
||||
uint16_t _num_floor_samples;
|
||||
@@ -107,10 +108,16 @@ protected:
|
||||
void cacheParams(float freq, float bw, uint8_t sf, uint8_t cr) {
|
||||
_cur_freq = freq; _cur_bw = bw; _cur_sf = sf; _cur_cr = cr; _params_valid = true;
|
||||
}
|
||||
unsigned long cadScanTimeoutMillis(uint8_t sf, float bw) const {
|
||||
if (_cad_scan_timeout_override_ms != 0) {
|
||||
return _cad_scan_timeout_override_ms;
|
||||
}
|
||||
return mesh::calculateCadScanTimeoutMillis(sf, bw);
|
||||
}
|
||||
unsigned long cadScanTimeoutMillis() const {
|
||||
const uint8_t sf = _params_valid ? _cur_sf : getSpreadingFactor();
|
||||
const float bw = _params_valid ? _cur_bw : static_cast<float>(LORA_BW);
|
||||
return mesh::calculateCadScanTimeoutMillis(sf, bw);
|
||||
return cadScanTimeoutMillis(sf, bw);
|
||||
}
|
||||
int16_t performChannelScanWithTimeout(unsigned long timeout_ms);
|
||||
virtual int startReceiveMode();
|
||||
@@ -153,6 +160,7 @@ public:
|
||||
last_recv_millis = 0;
|
||||
last_radio_interrupt_millis = 0;
|
||||
_cad_enabled = false;
|
||||
_cad_scan_timeout_override_ms = 0;
|
||||
}
|
||||
|
||||
void begin() override;
|
||||
@@ -210,6 +218,18 @@ public:
|
||||
void triggerNoiseFloorCalibrate(int threshold) override;
|
||||
void recalibrateNoiseFloor() override;
|
||||
void setCADEnabled(bool enable) override { _cad_enabled = enable; }
|
||||
bool setCADScanTimeoutMillis(uint32_t timeout_ms) {
|
||||
if (timeout_ms != 0
|
||||
&& (timeout_ms < mesh::CAD_SCAN_MIN_TIMEOUT_MS
|
||||
|| timeout_ms > mesh::CAD_SCAN_MAX_TIMEOUT_MS)) {
|
||||
return false;
|
||||
}
|
||||
_cad_scan_timeout_override_ms = timeout_ms;
|
||||
return true;
|
||||
}
|
||||
uint32_t getCADScanTimeoutMillis() const {
|
||||
return cadScanTimeoutMillis();
|
||||
}
|
||||
void resetAGC() override;
|
||||
|
||||
void loop() override;
|
||||
|
||||
Reference in new issue
Block a user