Add full Companion OTA source builds

This commit is contained in:
mikecarper committed 2026-08-10 12:39:07 -07:00
1 parent 1bceda54a6
commit 88ba5b26cb
19 files changed
+1212 -86

No files matched your search

+202 -1
View File
@@ -4,6 +4,10 @@
#include <Mesh.h>
#include "helpers/radiolib/RXPowerSaving.h"
#if defined(COMPANION_RADIO_FULL)
#include <helpers/ota/OtaCli.h>
#endif
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
#include <helpers/MQTTDefaults.h>
#endif
@@ -1215,6 +1219,17 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
command_radio_cr = 0;
command_radio_repeat = 0;
command_radio_apply_deadline = 0;
#if defined(COMPANION_RADIO_FULL)
_temp_radio_set_at = 0;
_temp_radio_revert_at = 0;
_temp_radio_retry_at = 0;
_temp_radio_freq = 0.0f;
_temp_radio_bw = 0.0f;
_temp_radio_sf = 0;
_temp_radio_cr = 0;
_temp_radio_failures = 0;
_temp_radio_applied = false;
#endif
offline_queue_len = 0;
app_target_ver = 0;
clearPendingReqs();
@@ -1487,6 +1502,159 @@ void MyMesh::servicePendingRadioParamApply() {
}
}
#if defined(COMPANION_RADIO_FULL)
static bool isFullCompanionBandwidth(float bw) {
static const float supported[] = {
7.8f, 10.4f, 15.6f, 20.8f, 31.25f, 41.7f,
62.5f, 125.0f, 250.0f, 500.0f
};
for (float candidate : supported) {
if (fabsf(candidate - bw) <= 0.01f) return true;
}
return false;
}
bool MyMesh::scheduleTempRadio(float freq, float bw, uint8_t sf, uint8_t cr,
uint32_t timeout_mins, char* reply,
size_t reply_size) {
if (command_radio_apply_pending) {
snprintf(reply, reply_size, "ERR companion radio change is already pending");
return false;
}
_temp_radio_freq = freq;
_temp_radio_bw = bw;
_temp_radio_sf = sf;
_temp_radio_cr = cr;
_temp_radio_set_at = futureMillis(1500); // let the local reply drain first
_temp_radio_revert_at = futureMillis(1500 + (int)(timeout_mins * 60000UL));
_temp_radio_retry_at = 0;
_temp_radio_failures = 0;
snprintf(reply, reply_size, "OK - temp params for %lu mins",
(unsigned long)timeout_mins);
return true;
}
void MyMesh::scheduleNormalRadio(char* reply, size_t reply_size) {
_temp_radio_set_at = 0;
_temp_radio_revert_at = futureMillis(1500); // keep the reply on the active tuple
_temp_radio_retry_at = 0;
_temp_radio_failures = 0;
snprintf(reply, reply_size, "OK - normal radio restore scheduled");
}
bool MyMesh::handleFullOtaCommand(const char* command, char* reply,
size_t reply_size) {
if (!command || !reply || reply_size == 0) return false;
while (*command == ' ') command++;
if (strcmp(command, "tempradio") == 0) {
if (_temp_radio_set_at) {
snprintf(reply, reply_size, "TempRadio pending: %.3f,%.2f,%u,%u",
_temp_radio_freq, _temp_radio_bw,
(unsigned)_temp_radio_sf, (unsigned)_temp_radio_cr);
} else if (isTempRadioActive()) {
uint32_t seconds = (_temp_radio_revert_at - _ms->getMillis()) / 1000UL;
snprintf(reply, reply_size, "TempRadio active: %.3f,%.2f,%u,%u %lus left",
_temp_radio_freq, _temp_radio_bw,
(unsigned)_temp_radio_sf, (unsigned)_temp_radio_cr,
(unsigned long)seconds);
} else {
snprintf(reply, reply_size, "TempRadio inactive");
}
return true;
}
if (strncmp(command, "tempradio ", 10) == 0) {
float freq = 0.0f, bw = 0.0f;
int sf = 0, cr = 0;
unsigned long timeout_mins = 0;
char extra = 0;
if (sscanf(command + 10, "%f,%f,%d,%d,%lu%c",
&freq, &bw, &sf, &cr, &timeout_mins, &extra) != 5
|| !isfinite(freq) || !isfinite(bw)
|| freq < 150.0f || freq > 2500.0f
|| !isFullCompanionBandwidth(bw)
|| sf < 5 || sf > 12 || cr < 5 || cr > 8
|| timeout_mins == 0 || timeout_mins > 10080UL) {
snprintf(reply, reply_size,
"ERR usage: tempradio freq,bw,sf,cr,minutes (minutes 1-10080)");
return true;
}
scheduleTempRadio(freq, bw, (uint8_t)sf, (uint8_t)cr,
(uint32_t)timeout_mins, reply, reply_size);
return true;
}
if (strcmp(command, "normalradio") == 0) {
scheduleNormalRadio(reply, reply_size);
return true;
}
if (strncmp(command, "ota", 3) == 0
&& (command[3] == 0 || command[3] == ' ')) {
char ota_reply[160] = {0};
if (!mesh::ota::handle_ota_command(command, ota_reply, board)) return false;
snprintf(reply, reply_size, "%s", ota_reply);
return true;
}
return false;
}
void MyMesh::serviceTempRadio() {
const unsigned long now = _ms->getMillis();
const bool retry_ready = !_temp_radio_retry_at
|| _temp_radio_retry_at == now || millisHasNowPassed(_temp_radio_retry_at);
const bool revert_due = _temp_radio_revert_at
&& (_temp_radio_revert_at == now || millisHasNowPassed(_temp_radio_revert_at));
if (revert_due) {
if (hasOutbound() || !retry_ready) return;
mesh::RadioParamApplyResult result = tryApplyRadioParams(
_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
if (result == mesh::RadioParamApplyResult::APPLIED) {
radio_driver.setTxPower(_prefs.tx_power_dbm);
radio_driver.setRxBoostedGainMode(_prefs.rx_boosted_gain);
_temp_radio_set_at = 0;
_temp_radio_revert_at = 0;
_temp_radio_retry_at = 0;
_temp_radio_failures = 0;
_temp_radio_applied = false;
saved_radio_apply_pending = false;
MESH_DEBUG_PRINTLN("Full companion restored normal radio");
} else {
_temp_radio_retry_at = futureMillis(
nextRadioApplyRetryDelay(_temp_radio_failures));
}
return;
}
if (!_temp_radio_set_at
|| (_temp_radio_set_at != now && !millisHasNowPassed(_temp_radio_set_at))
|| hasOutbound() || !retry_ready) return;
mesh::RadioParamApplyResult result = tryApplyRadioParams(
_temp_radio_freq, _temp_radio_bw, _temp_radio_sf, _temp_radio_cr);
if (result == mesh::RadioParamApplyResult::APPLIED) {
_temp_radio_set_at = 0;
_temp_radio_retry_at = 0;
_temp_radio_failures = 0;
_temp_radio_applied = true;
MESH_DEBUG_PRINTLN("Full companion entered TempRadio");
} else if (result == mesh::RadioParamApplyResult::BUSY) {
_temp_radio_retry_at = futureMillis(250);
} else {
// A partially applied tuple must never become persistent. Restore the
// saved Companion settings through the same bounded retry path.
_temp_radio_set_at = 0;
_temp_radio_revert_at = futureMillis(1);
_temp_radio_retry_at = 0;
saved_radio_apply_pending = true;
}
}
#endif
const char *MyMesh::getNodeName() {
return _prefs.node_name;
}
@@ -2442,6 +2610,13 @@ void MyMesh::handleCmdFrame(size_t len) {
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
return;
}
#if defined(COMPANION_RADIO_FULL)
if (isTempRadioActive() || _temp_radio_set_at != 0
|| _temp_radio_revert_at != 0) {
writeErrFrame(ERR_CODE_BAD_STATE);
return;
}
#endif
int i = 1;
uint32_t freq;
memcpy(&freq, &cmd_frame[i], 4);
@@ -3275,6 +3450,14 @@ void MyMesh::handleTerminalCommand(char* command) {
while (*command == ' ') command++;
if (*command == 0) return;
#if defined(COMPANION_RADIO_FULL)
char full_reply[160];
if (handleFullOtaCommand(command, full_reply, sizeof(full_reply))) {
Serial.printf(" %s\r\n", full_reply);
return;
}
#endif
if (strncmp(command, "send ", 5) == 0) {
ContactInfo* recipient = getTerminalRecipient();
const char* text = command + 5;
@@ -3427,6 +3610,11 @@ void MyMesh::handleTerminalCommand(char* command) {
Serial.print(" advert\r\n");
Serial.print(" reset path\r\n");
Serial.print(" public <text>\r\n");
#if defined(COMPANION_RADIO_FULL)
Serial.print(" tempradio [freq,bw,sf,cr,minutes]\r\n");
Serial.print(" normalradio\r\n");
Serial.print(" ota {status|ls|announce|folder|config|...}\r\n");
#endif
Serial.print(" ver\r\n");
Serial.print(" +++MESHCORE-TERM-STOP\r\n");
} else {
@@ -3647,8 +3835,15 @@ void MyMesh::checkSerialInterface() {
}
void MyMesh::loop() {
#if defined(COMPANION_RADIO_FULL)
serviceTempRadio();
#endif
BaseChatMesh::loop();
if (!command_radio_apply_pending && saved_radio_apply_pending && !hasOutbound()
#if defined(COMPANION_RADIO_FULL)
&& !_temp_radio_applied && _temp_radio_set_at == 0
&& _temp_radio_revert_at == 0
#endif
&& (!radio_apply_retry_at || millisHasNowPassed(radio_apply_retry_at))) {
// A power-saving wake can enter begin() with a complete packet already
// waiting. Preserve that packet, then apply the persisted radio settings
@@ -3721,5 +3916,11 @@ bool MyMesh::hasPendingWork() const {
&& (!radio_apply_retry_at || millisHasNowPassed(radio_apply_retry_at)))
|| (dirty_contacts_expiry != 0 && millisHasNowPassed(dirty_contacts_expiry))
|| (emergency_client_repeat_packet != NULL
&& millisHasNowPassed(emergency_client_repeat_send_at));
&& millisHasNowPassed(emergency_client_repeat_send_at))
#if defined(COMPANION_RADIO_FULL)
|| (_temp_radio_set_at != 0 && millisHasNowPassed(_temp_radio_set_at))
|| (_temp_radio_revert_at != 0 && millisHasNowPassed(_temp_radio_revert_at))
|| (_temp_radio_retry_at != 0 && millisHasNowPassed(_temp_radio_retry_at))
#endif
;
}
+29
View File
@@ -149,9 +149,21 @@ public:
void handleTerminalCommand(char* command);
#endif
#if defined(COMPANION_RADIO_FULL)
// Local-only control surface shared by the USB terminal and WiFi port 5002.
// Supports bounded TempRadio windows plus the serve-only `ota ...` CLI.
bool handleFullOtaCommand(const char* command, char* reply, size_t reply_size);
#endif
int getRecentlyHeard(AdvertPath dest[], int max_num);
protected:
#if defined(COMPANION_RADIO_FULL)
bool isTempRadioActive() const override {
return _temp_radio_applied && _temp_radio_revert_at != 0
&& !millisHasNowPassed(_temp_radio_revert_at);
}
#endif
float getAirtimeBudgetFactor() const override;
int getInterferenceThreshold() const override;
bool getCADEnabled() const override;
@@ -272,6 +284,12 @@ private:
void finishRadioParamApply(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t repeat);
void cancelPendingRadioParamApply();
void servicePendingRadioParamApply();
#if defined(COMPANION_RADIO_FULL)
bool scheduleTempRadio(float freq, float bw, uint8_t sf, uint8_t cr,
uint32_t timeout_mins, char* reply, size_t reply_size);
void scheduleNormalRadio(char* reply, size_t reply_size);
void serviceTempRadio();
#endif
// helpers, short-cuts
void saveChannels() { _store->saveChannels(this); }
@@ -320,6 +338,17 @@ private:
uint8_t command_radio_cr;
uint8_t command_radio_repeat;
unsigned long command_radio_apply_deadline;
#if defined(COMPANION_RADIO_FULL)
unsigned long _temp_radio_set_at;
unsigned long _temp_radio_revert_at;
unsigned long _temp_radio_retry_at;
float _temp_radio_freq;
float _temp_radio_bw;
uint8_t _temp_radio_sf;
uint8_t _temp_radio_cr;
uint8_t _temp_radio_failures;
bool _temp_radio_applied;
#endif
bool send_unscoped; // force un-scoped flood (instead of using send_scope)
char cli_command[80];
uint8_t app_target_ver;
+121 -5
View File
@@ -60,6 +60,12 @@ MultiSerialInterface interface_manager;
#include <helpers/ArduinoSerialInterface.h>
static const char USB_TERMINAL_START_TOKEN[] = "+++MESHCORE-TERM-START";
static const char USB_TERMINAL_STOP_TOKEN[] = "+++MESHCORE-TERM-STOP";
#if defined(NRF52_PLATFORM) && defined(COMPANION_RADIO_FULL) && defined(OTA_FOLDER_SERIAL)
// 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.
static const char USB_MOTA_START_TOKEN[] = "ota folder on";
#endif
ArduinoSerialInterface usb_serial_interface;
#endif
@@ -120,6 +126,12 @@ static char usb_terminal_line[MAX_TRANS_UNIT * 2 + 32];
static size_t usb_terminal_line_len = 0;
static bool usb_terminal_discard_line = false;
static bool usb_terminal_disconnect_armed = false;
#if defined(NRF52_PLATFORM) && defined(COMPANION_RADIO_FULL) && defined(OTA_FOLDER_SERIAL)
static bool usb_mota_mode = false;
static char usb_mota_line[32];
static size_t usb_mota_line_len = 0;
static bool usb_mota_disconnect_armed = false;
#endif
static bool isUsbTerminalDataConnected() {
#if defined(RP2040_PLATFORM)
@@ -150,9 +162,100 @@ static void leaveUsbTerminalMode(bool acknowledge) {
usb_terminal_disconnect_armed = false;
}
#if defined(NRF52_PLATFORM) && defined(COMPANION_RADIO_FULL) && defined(OTA_FOLDER_SERIAL)
static void resetUsbMotaMode() {
usb_mota_mode = false;
usb_mota_line_len = 0;
usb_mota_line[0] = 0;
usb_mota_disconnect_armed = false;
usb_serial_interface.setPassthroughMode(false);
}
static void leaveUsbMotaMode(bool acknowledge) {
char reply[160] = {0};
the_mesh.handleFullOtaCommand("ota folder off", reply, sizeof(reply));
if (acknowledge) {
Serial.print("\r\n");
Serial.print(reply);
Serial.print("\r\nOK - Binary mode\r\n");
}
resetUsbMotaMode();
}
static void enterUsbMotaMode() {
usb_serial_interface.setPassthroughMode(true);
usb_mota_mode = true;
usb_mota_line_len = 0;
usb_mota_line[0] = 0;
usb_mota_disconnect_armed = isUsbTerminalDataConnected();
char reply[160] = {0};
if (!the_mesh.handleFullOtaCommand("ota folder on", reply, sizeof(reply))
|| strncmp(reply, "ERR", 3) == 0) {
Serial.print("\r\n");
Serial.print(reply[0] ? reply : "ERR could not enter mOTA seeder mode");
Serial.print("\r\n");
resetUsbMotaMode();
return;
}
Serial.print("\r\n");
Serial.print(reply);
Serial.print("\r\n");
}
static void serviceUsbMota() {
if (isUsbTerminalDataConnected()) {
usb_mota_disconnect_armed = true;
} else if (usb_mota_disconnect_armed) {
leaveUsbMotaMode(false);
return;
}
// SerialMotaSource consumes framed responses synchronously while serving a
// block. Bytes left here are host control text, notably motatool's automatic
// `ota folder off` on a clean shutdown.
while (Serial.available()) {
int value = Serial.read();
if (value < 0) break;
char c = (char)value;
if (c == '\r' || c == '\n') {
if (usb_mota_line_len == 0) continue;
usb_mota_line[usb_mota_line_len] = 0;
bool stop = strcmp(usb_mota_line, "ota folder off") == 0;
usb_mota_line_len = 0;
usb_mota_line[0] = 0;
if (stop) {
leaveUsbMotaMode(true);
return;
}
continue;
}
if (usb_mota_line_len < sizeof(usb_mota_line) - 1) {
usb_mota_line[usb_mota_line_len++] = c;
usb_mota_line[usb_mota_line_len] = 0;
} else {
usb_mota_line_len = 0;
usb_mota_line[0] = 0;
}
}
}
#endif
static void serviceUsbTerminal() {
#if defined(NRF52_PLATFORM) && defined(COMPANION_RADIO_FULL) && defined(OTA_FOLDER_SERIAL)
if (usb_mota_mode) {
serviceUsbMota();
return;
}
#endif
if (!the_mesh.isTerminalMode()) {
if (usb_serial_interface.takeControlSequence()) enterUsbTerminalMode();
if (usb_serial_interface.takeControlSequence()) {
enterUsbTerminalMode();
#if defined(NRF52_PLATFORM) && defined(COMPANION_RADIO_FULL) && defined(OTA_FOLDER_SERIAL)
} else if (usb_serial_interface.takeSecondaryControlSequence()) {
enterUsbMotaMode();
#endif
}
return;
}
@@ -293,10 +396,10 @@ void halt() {
}
#endif
/* WIFI OTA CONSOLE - a tiny text CLI for OTA over WiFi. A WiFi companion has no serial text console, so
without this its OTA is only reachable through the phone app. Connect with e.g. `nc <ip> 5002` and type
/* WIFI OTA CONSOLE - a tiny text CLI for OTA over WiFi. Connect with e.g. `nc <ip> 5002` and type
`ota status` / `ota ls` / `ota announce` / ... - one client at a time, on a DEDICATED port separate from
the companion (5000) and the seeder (5001). */
the companion (5000) and the seeder (5001). Full companions also accept `tempradio ...` and
`normalradio` here so a host can run an end-to-end mOTA source without occupying the binary port. */
#if defined(ESP32) && defined(WIFI_SSID) && defined(ENABLE_OTA)
#include <helpers/ota/OtaCli.h> // mesh::ota::handle_ota_command(line, reply, board)
#ifndef OTA_CONSOLE_TCP_PORT
@@ -311,7 +414,11 @@ void halt() {
if (!ota_console_client || !ota_console_client.connected()) {
WiFiClient c = ota_console_server.available();
if (c) { ota_console_client = c; ota_console_len = 0;
ota_console_client.print("OTA console - type `ota ...` (e.g. ota status / ota ls / ota announce)\r\n> "); }
ota_console_client.print("OTA console - type `ota ...`");
#if defined(COMPANION_RADIO_FULL)
ota_console_client.print(" or `tempradio freq,bw,sf,cr,minutes`");
#endif
ota_console_client.print("\r\n> "); }
return;
}
while (ota_console_client.available()) {
@@ -320,8 +427,13 @@ void halt() {
if (ota_console_len == 0) continue; // ignore blanks / the CRLF pair
ota_console_line[ota_console_len] = 0;
char reply[160]; reply[0] = 0;
#if defined(COMPANION_RADIO_FULL)
if (!the_mesh.handleFullOtaCommand(ota_console_line, reply, sizeof(reply)))
strcpy(reply, "supported: ota ... | tempradio ... | normalradio");
#else
if (!mesh::ota::handle_ota_command(ota_console_line, reply, board))
strcpy(reply, "only `ota ...` commands are supported on this console");
#endif
ota_console_client.print(" -> "); ota_console_client.print(reply); ota_console_client.print("\r\n> ");
ota_console_len = 0;
} else if (ota_console_len < sizeof(ota_console_line) - 1) {
@@ -489,7 +601,11 @@ void setup() {
// add usb interface
#if defined(ENABLE_USB_INTERFACE)
#if defined(NRF52_PLATFORM) && defined(COMPANION_RADIO_FULL) && defined(OTA_FOLDER_SERIAL)
usb_serial_interface.begin(Serial, USB_TERMINAL_START_TOKEN, USB_MOTA_START_TOKEN);
#else
usb_serial_interface.begin(Serial, USB_TERMINAL_START_TOKEN);
#endif
interface_manager.addInterface(InterfaceType::USB, &usb_serial_interface);
#endif