Let USB repeaters and Full Companions manage direct OTA updates

This commit is contained in:
mikecarper committed 2026-10-26 19:46:05 -07:00
1 parent 5741006b3e
commit 435c740e5a
15 files changed
+1382 -15

No files matched your search

+59 -1
View File
@@ -2526,6 +2526,27 @@ uint8_t MyMesh::handleTrackerStatusRequest(const ContactInfo& from, uint32_t tag
}
#endif
#if COMPANION_FEATURE_OTA_CLI
uint8_t MyMesh::getContactTxRadio(const mesh::Identity& peer) const {
return _ota_direct_admin.selected(peer, _ms->getMillis())
? static_cast<uint8_t>(mesh::RADIO_TX_PRIMARY) : BaseChatMesh::getContactTxRadio(peer);
}
void MyMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx,
const uint8_t* secret, uint8_t* data, size_t len) {
if (!_ota_direct_admin.accepts(packet, type, secret, _ms->getMillis())) return;
BaseChatMesh::onPeerDataRecv(packet, type, sender_idx, secret, data, len);
}
bool MyMesh::onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret,
uint8_t* path, uint8_t path_len, uint8_t extra_type,
uint8_t* extra, uint8_t extra_len) {
if (!_ota_direct_admin.acceptsPath(packet, secret, path_len, _ms->getMillis())) return false;
return BaseChatMesh::onPeerPathRecv(packet, sender_idx, secret, path, path_len,
extra_type, extra, extra_len);
}
#endif
void MyMesh::onContactResponse(const ContactInfo &contact, const uint8_t *data, uint8_t len) {
const uint32_t now = _ms->getMillis();
servicePendingSerialReply(now); // Expire before considering any radio callback.
@@ -6243,7 +6264,7 @@ void MyMesh::handleCmdFrame(size_t len) {
|| (cmd_frame[0] == CMD_SEND_ANON_REQ && len > 1 + PUB_KEY_SIZE)
|| (cmd_frame[0] == CMD_SEND_STATUS_REQ && len >= 1 + PUB_KEY_SIZE)
|| (cmd_frame[0] == CMD_SEND_PATH_DISCOVERY_REQ
&& len >= 2 + PUB_KEY_SIZE)
&& len >= 2 + PUB_KEY_SIZE && cmd_frame[1] == 0)
|| (cmd_frame[0] == CMD_SEND_TELEMETRY_REQ
&& len >= 4 + PUB_KEY_SIZE)
|| (cmd_frame[0] == CMD_SEND_BINARY_REQ
@@ -7033,6 +7054,9 @@ void MyMesh::handleCmdFrame(size_t len) {
char *password = (char *)&cmd_frame[1 + PUB_KEY_SIZE];
cmd_frame[len] = 0; // ensure null terminator in password
if (recipient) {
#if COMPANION_FEATURE_OTA_CLI
_ota_direct_admin.beginLogin(recipient->id, _ms->getMillis());
#endif
if (!beginPendingRequest(mesh::CompanionDelayedReplies::Login, *recipient)) {
writeErrFrame(ERR_CODE_BAD_STATE);
return;
@@ -7086,6 +7110,25 @@ void MyMesh::handleCmdFrame(size_t len) {
} else {
writeErrFrame(ERR_CODE_NOT_FOUND); // contact not found
}
#if COMPANION_FEATURE_OTA_CLI
} else if (cmd_frame[0] == CMD_SEND_PATH_DISCOVERY_REQ
&& len == 2 + PUB_KEY_SIZE && cmd_frame[1] >= 1 && cmd_frame[1] <= 3) {
// Mode 0 remains the existing flood path discovery. These opt-in modes
// never emit flood RF and stock firmware rejects them without sending RF.
ContactInfo* recipient = lookupContactByPubKey(&cmd_frame[2], PUB_KEY_SIZE);
if (!recipient) {
writeErrFrame(ERR_CODE_NOT_FOUND);
} else if (cmd_frame[1] == 1) {
_ota_direct_admin.arm(recipient->id, recipient->getSharedSecret(self_id), _ms->getMillis());
writeOKFrame();
} else if (cmd_frame[1] == 2) {
if (_ota_direct_admin.sendReturnPath(*this, recipient->id, _ms->getMillis())) writeOKFrame();
else writeErrFrame(ERR_CODE_BAD_STATE);
} else {
if (_ota_direct_admin.matches(recipient->id, _ms->getMillis())) _ota_direct_admin.clear();
writeOKFrame();
}
#endif
} else if (cmd_frame[0] == CMD_SEND_PATH_DISCOVERY_REQ && len >= 2 + PUB_KEY_SIZE && cmd_frame[1] == 0) {
uint8_t *pub_key = &cmd_frame[2];
ContactInfo *recipient = lookupContactByPubKey(pub_key, PUB_KEY_SIZE);
@@ -7798,6 +7841,12 @@ void MyMesh::handleCmdFrame(size_t len) {
if (pkt) {
uint8_t priority = cmd_frame[1];
if (tryParsePacket(pkt, &cmd_frame[2], len - 2)) {
#if COMPANION_FEATURE_OTA_CLI
if (pkt->getPayloadType() == PAYLOAD_TYPE_OTA
&& _ota_direct_admin.active(_ms->getMillis())) {
pkt->tx_radio = mesh::RADIO_TX_PRIMARY;
}
#endif
if (pkt->getPayloadType() == PAYLOAD_TYPE_RAW_CUSTOM) {
const uint8_t policy = mesh::companion::trafficPolicy(
mesh::companion::rawTrafficKind(pkt->payload, pkt->payload_len),
@@ -10778,6 +10827,15 @@ bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp,
size_t reply_capacity = 160;
while (*command == ' ' || *command == '\t') command++;
#if COMPANION_FEATURE_OTA_CLI
if (sender_timestamp == 0 && !strcmp(command, "get ota.direct")) {
snprintf(reply, reply_capacity, "> ota.direct v=1 active=%u busy=%u",
_ota_direct_admin.active(_ms->getMillis()) ? 1 : 0,
hasPendingRadioWork() ? 1 : 0);
return true;
}
#endif
if (strlen(command) > 3 && command[2] == '|') {
// Optional two-character request prefix used by Companion CLI clients.
memcpy(reply, command, 3);
+14
View File
@@ -64,6 +64,9 @@ namespace mesh { class BeaconManager; }
#endif
#include <helpers/TrackerProtocol.h>
#include <helpers/CompanionMotaControl.h>
#if COMPANION_FEATURE_OTA_CLI
#include <helpers/CompanionDirectAdminGuard.h>
#endif
#include <helpers/IdentityStore.h>
#include <helpers/LogicalMessageCache.h>
#include <helpers/SimpleMeshTables.h>
@@ -527,6 +530,14 @@ protected:
bool onContactOverwrite(const ContactInfo& contact) override;
void onContactReferenceChanged(const ContactInfo* previous, ContactInfo* replacement) override;
bool onContactPathRecv(ContactInfo& from, uint8_t* in_path, uint8_t in_path_len, uint8_t* out_path, uint8_t out_path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override;
#if COMPANION_FEATURE_OTA_CLI
uint8_t getContactTxRadio(const mesh::Identity& peer) const override;
void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx,
const uint8_t* secret, uint8_t* data, size_t len) override;
bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret,
uint8_t* path, uint8_t path_len, uint8_t extra_type,
uint8_t* extra, uint8_t extra_len) override;
#endif
void onDiscoveredContact(ContactInfo &contact, bool is_new, uint8_t path_len, const uint8_t* path) override;
void onContactPathUpdated(const ContactInfo &contact) override;
#if COMPANION_FEATURE_TEXT_TERMINAL
@@ -636,6 +647,9 @@ public:
bool canRecoverUsbLogging() const;
private:
#if COMPANION_FEATURE_OTA_CLI
mesh::CompanionDirectAdminGuard _ota_direct_admin;
#endif
#if MESH_ENABLE_FLEET_CONTROL
// Exact ownership of the optional radio copy during two-part admission.
mesh::Packet* _fleet_admission_original = nullptr;
+34
View File
@@ -2895,6 +2895,11 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
int MyMesh::searchPeersByHash(const uint8_t *hash) {
int n = 0;
#if MESH_ENABLE_DIRECT_OTA_ADMIN
direct_ota_admin.tick(millis());
if (direct_ota_admin.matchesHash(hash))
matching_peer_indexes[n++] = mesh::DirectOtaAdminClient::PEER_INDEX;
#endif
#if defined(WITH_MQTT_NEIGHBORS)
// While a neighbor-scope discovery is active, overlay the heard neighbours
// that are NOT already ACL clients so their RESPONSE packets can be decoded.
@@ -2913,6 +2918,9 @@ int MyMesh::searchPeersByHash(const uint8_t *hash) {
#endif
for (int i = 0; i < acl.getNumClients() && n < MAX_CLIENTS; i++) {
const auto* client = acl.getClientByIdx(i);
#if MESH_ENABLE_DIRECT_OTA_ADMIN
if (direct_ota_admin.matchesTarget(client->id)) continue;
#endif
if (!guest_login.enabled()
&& (client->permissions & PERM_ACL_ROLE_MASK) == PERM_ACL_GUEST) continue;
if (client->id.isHashMatch(hash)) {
@@ -2924,6 +2932,12 @@ int MyMesh::searchPeersByHash(const uint8_t *hash) {
void MyMesh::getPeerSharedSecret(uint8_t *dest_secret, int peer_idx) {
int i = matching_peer_indexes[peer_idx];
#if MESH_ENABLE_DIRECT_OTA_ADMIN
if (i == mesh::DirectOtaAdminClient::PEER_INDEX) {
direct_ota_admin.sharedSecret(dest_secret);
return;
}
#endif
#if defined(WITH_MQTT_NEIGHBORS)
// Overlay entries have no precomputed shared secret; derive it on the fly.
if (neighbor_discover_active && i >= NEIGHBOR_DISCOVER_PEER_BASE) {
@@ -3002,6 +3016,12 @@ void MyMesh::onGroupPacketRecv(mesh::Packet* packet) {
void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, const uint8_t *secret,
uint8_t *data, size_t len) {
int i = matching_peer_indexes[sender_idx];
#if MESH_ENABLE_DIRECT_OTA_ADMIN
if (i == mesh::DirectOtaAdminClient::PEER_INDEX) {
direct_ota_admin.onData(*this, packet, type, data, len, millis());
return;
}
#endif
#if defined(WITH_MQTT_NEIGHBORS)
// Overlay response: a heard neighbour (not an ACL client) answering our
// anon-regions scope query. Consume it and stop -- it is not a client packet.
@@ -3584,6 +3604,9 @@ bool MyMesh::onPeerPathRecv(mesh::Packet *packet, int sender_idx, const uint8_t
#define CTL_TYPE_NODE_DISCOVER_RESP 0x90
void MyMesh::onControlDataRecv(mesh::Packet* packet) {
#if MESH_ENABLE_DIRECT_OTA_ADMIN
direct_ota_admin.onDiscovery(packet, millis());
#endif
uint8_t type = packet->payload[0] & 0xF0; // just test upper 4 bits
if (type == CTL_TYPE_NODE_DISCOVER_REQ && packet->payload_len >= 6
&& (_prefs.discovery_responses & mesh::discovery::NODE_RESPONSES)
@@ -12396,6 +12419,14 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char *
char *reply, int gpio_client_index,
uint8_t gpio_path_hash_size, bool usb_origin) {
_wireless_usb_command = usb_origin;
#if MESH_ENABLE_DIRECT_OTA_ADMIN
if (mesh::DirectOtaAdminClient::isCommand(command)) {
if (!usb_origin || sender_timestamp || sender != nullptr)
strcpy(reply, "ERR ota.peer requires physical USB");
else direct_ota_admin.handleCommand(*this, command, reply, 160, millis());
return;
}
#endif
#if defined(ESP32_PLATFORM) || defined(USER_GPIO_CONTROL)
_gpio_reply_tracker.beginCommand(gpio_client_index, gpio_path_hash_size,
sender == NULL ? NULL : sender->id.pub_key);
@@ -13361,6 +13392,9 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char *
}
void MyMesh::loop() {
#if MESH_ENABLE_DIRECT_OTA_ADMIN
direct_ota_admin.service(*this, millis());
#endif
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
_local_cli_output.service();
#endif
+16
View File
@@ -12,6 +12,16 @@
#if defined(ENABLE_OTA)
#include <helpers/ota/OtaContext.h>
#endif
#ifndef MESH_ENABLE_DIRECT_OTA_ADMIN
#if defined(ESP32_PLATFORM) && defined(ENABLE_OTA)
#define MESH_ENABLE_DIRECT_OTA_ADMIN 1
#else
#define MESH_ENABLE_DIRECT_OTA_ADMIN 0
#endif
#endif
#if MESH_ENABLE_DIRECT_OTA_ADMIN
#include <helpers/DirectOtaAdminClient.h>
#endif
#include <RTClib.h>
#include <CayenneLPP.h>
#include <target.h>
@@ -401,6 +411,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
bool handleGuestCommand(const char* command, char* reply, size_t capacity);
bool handleDiscoveryResponseCommand(const char* command, char* reply, size_t capacity);
ClientACL acl;
#if MESH_ENABLE_DIRECT_OTA_ADMIN
mesh::DirectOtaAdminClient direct_ota_admin;
#endif
CommonCLI _cli;
#if defined(ESP32_PLATFORM) || defined(USER_GPIO_CONTROL)
UserGpioReplyTracker _gpio_reply_tracker;
@@ -1350,6 +1363,9 @@ public:
void handleUsbCommand(char* command, char* reply) {
handleCommand(0, NULL, command, reply, -1, 1, true);
}
#if MESH_ENABLE_DIRECT_OTA_ADMIN
void closeDirectOtaAdmin() { direct_ota_admin.reset(); }
#endif
bool handleReplayResetCommand(ClientInfo* sender, const char* command,
char* reply, bool usb_origin);
#if MESH_ENABLE_HOST_CLI
+67 -6
View File
@@ -153,6 +153,9 @@ static constexpr size_t LOCAL_SERIAL_COMMAND_MAX = 159U;
#endif
static char command[LOCAL_SERIAL_COMMAND_MAX + 2U];
static bool command_overflow = false;
#if MESH_ENABLE_DIRECT_OTA_ADMIN
static bool command_sensitive = false;
#endif
#ifdef ETHERNET_ENABLED
static char ethernet_command[160];
#endif
@@ -480,16 +483,48 @@ static void __attribute__((noinline)) serviceCommandInterfaces() {
if (mesh::takeUsbTerminalSessionReset()) {
#if MESH_ENABLE_HOST_KISS
mesh::hostkiss::resetUsbSession();
#endif
#if MESH_ENABLE_DIRECT_OTA_ADMIN
mesh::DirectOtaAdminClient::clearInput(command, sizeof(command));
command_sensitive = false;
the_mesh.closeDirectOtaAdmin();
#if defined(MESH_OTA_FOLDER_SERIAL_SHARED_CONSOLE)
mesh::ota::OtaContext::serialFolderSource().resetSessionState();
#endif
#endif
command[0] = 0;
command_overflow = false;
the_mesh.cancelPendingSerialOutput();
}
#endif
Stream& console = mesh::usbConsolePort();
#if MESH_ENABLE_DIRECT_OTA_ADMIN && defined(MESH_OTA_FOLDER_SERIAL_SHARED_CONSOLE)
auto& control_source = mesh::ota::OtaContext::serialFolderSource();
control_source.enableSharedTextControl();
auto console_available = [&]() { return control_source.availableControlBytes(); };
auto console_read = [&]() { return control_source.readControlByte(); };
#else
auto console_available = [&]() { return console.available(); };
auto console_read = [&]() { return console.read(); };
#endif
#if MESH_ENABLE_HOST_KISS
const auto kiss_can_read = [&]() {
#if MESH_ENABLE_DIRECT_OTA_ADMIN && defined(MESH_OTA_FOLDER_SERIAL_SHARED_CONSOLE)
return control_source.canReleaseControlInput();
#else
return true;
#endif
};
#endif
#if MESH_ENABLE_HOST_KISS
// Claim binary input before a pending terminal listing can append text.
// Keep the rest of this function alive for Ethernet and board housekeeping.
if (mesh::hostkiss::serviceUsb()) {
if (kiss_can_read() && mesh::hostkiss::serviceUsb()) {
#if MESH_ENABLE_DIRECT_OTA_ADMIN
mesh::DirectOtaAdminClient::clearInput(command, sizeof(command));
command_sensitive = false;
the_mesh.closeDirectOtaAdmin();
#endif
command[0] = 0;
command_overflow = false;
#if MESH_USB_CONSOLE_COOPERATIVE
@@ -506,7 +541,6 @@ static void __attribute__((noinline)) serviceCommandInterfaces() {
usb_ready = usb_ready && !the_mesh.hasPendingSerialOutput()
&& mesh::canAcceptUsbConsoleCommand();
#endif
Stream& console = mesh::usbConsolePort();
#if defined(MESH_HIL_STARTUP_TRACE) && MESH_HIL_STARTUP_TRACE
if (usb_ready && first_cli_ready) {
first_cli_ready = false;
@@ -525,14 +559,33 @@ static void __attribute__((noinline)) serviceCommandInterfaces() {
bool line_complete = false;
bool overlong_line_complete = false;
size_t read_budget = 256;
while (usb_ready && read_budget-- > 0 && console.available()) {
while (usb_ready && read_budget-- > 0 && console_available()) {
#if MESH_ENABLE_HOST_KISS
// Bytes may arrive after the loop-top KISS poll. Preserve its first FEND.
if (mesh::hostkiss::usbInputReserved(console.peek())) break;
// Reserve raw FEND only at a complete mOTA/text boundary. A late binary
// response body can contain FEND and must stay with its source framer.
if (kiss_can_read() && mesh::hostkiss::usbInputReserved(console.peek())) break;
#endif
char c = console.read();
const int value = console_read();
if (value == -2) continue; // retained mOTA response byte, never CLI text
if (value < 0) break;
if (value == 0) { // source control-queue overflow invalidates the whole line
#if MESH_ENABLE_DIRECT_OTA_ADMIN
mesh::DirectOtaAdminClient::clearInput(command, sizeof(command));
#else
command[0] = 0;
#endif
len = 0;
command_overflow = true;
continue;
}
char c = static_cast<char>(value);
if (c == '\n') continue;
#if MESH_ENABLE_DIRECT_OTA_ADMIN
command_sensitive = command_sensitive || mesh::DirectOtaAdminClient::sensitiveInput(command);
console.print(command_sensitive && c != '\r' ? '*' : c);
#else
console.print(c);
#endif
if (command_overflow) {
if (c == '\r') {
@@ -559,6 +612,10 @@ static void __attribute__((noinline)) serviceCommandInterfaces() {
}
if (overlong_line_complete) {
#if MESH_ENABLE_DIRECT_OTA_ADMIN
mesh::DirectOtaAdminClient::clearInput(command, sizeof(command));
command_sensitive = false;
#endif
console.print('\n');
console.println(" -> Err - command too long");
command[0] = 0;
@@ -602,6 +659,10 @@ static void __attribute__((noinline)) serviceCommandInterfaces() {
console.printf(" -> %s\r\n", reply);
}
#if MESH_ENABLE_DIRECT_OTA_ADMIN
if (command_sensitive) mesh::DirectOtaAdminClient::clearInput(command, sizeof(command));
command_sensitive = false;
#endif
command[0] = 0; // reset command buffer
}