Harden and qualify compact RAK3401 OTA updates

This commit is contained in:
mikecarper committed 2026-08-19 11:17:22 -07:00
1 parent 47993d178e
commit dc4a8808c0
22 files changed
+2156 -277

No files matched your search

+6 -1
View File
@@ -520,7 +520,12 @@ void Mesh::decayOtaRelayBackoff() {
uint32_t Mesh::getOtaRetransmitDelay(const mesh::Packet* packet) {
if (packet == NULL) return 0;
if (isPrimaryOtaTraffic(packet->payload, packet->payload_len)) return 0;
// Active transfer packets use the role's configured flood collision window.
// Deployment tooling can temporarily select txdelay 0.3 on managed relays;
// the value is airtime-scaled by the role and therefore follows SF/BW.
if (isPrimaryOtaTraffic(packet->payload, packet->payload_len)) {
return getRetransmitDelay(packet);
}
decayOtaRelayBackoff();
uint32_t airtime = _radio->getEstAirtimeFor(packet->getRawLength());
if (airtime == 0) return 0;
+2 -2
View File
@@ -227,8 +227,8 @@ protected:
virtual uint32_t getRetransmitDelay(const Packet* packet);
/**
* \returns TempRadio OTA relay delay. Primary transfer packets forward immediately on the private
* network; discovery retains collision jitter.
* \returns TempRadio OTA relay delay. Primary transfer packets use the role's configured flood delay;
* discovery uses its adaptive request-pressure window.
*/
virtual uint32_t getOtaRetransmitDelay(const Packet* packet);
+3
View File
@@ -57,6 +57,9 @@ static const uint8_t MS_STATUS_OK = 0x00;
static const uint8_t MS_STATUS_ERR = 0x01;
static const uint16_t MOTA_DESC_WIRE = 38; // existing reserved bytes carry block geometry; wire size is unchanged
// Keep host-to-device replies below common 256-byte USB CDC/UART receive rings. Manifest leaves can exceed
// that ring, and payload reads are normally 1 KB; splitting them here prevents silent overrun/checksum loss.
static const uint16_t MOTA_SEEDER_READ_MAX = 192;
static const uint16_t MOTA_SEEDER_WRITE_MAX = 512; // max data bytes per OP_WRITE/OP_SREAD (bounds frames)
} // namespace ota
+14 -6
View File
@@ -90,12 +90,20 @@ bool SerialMotaSource::describe(uint8_t idx, MotaDesc& out) {
}
bool SerialMotaSource::read(uint8_t idx, uint32_t off, uint8_t* buf, uint32_t len) {
if (len > 0xFFFF) return false; // single transaction caps at 64 KB (a block is <=1 KB)
uint8_t args[7];
args[0] = idx;
wr_u32le(args + 1, off);
args[5] = (uint8_t)(len & 0xFF); args[6] = (uint8_t)(len >> 8);
return txn(MS_OP_READ, args, 7, buf, len);
if (!buf && len != 0) return false;
if (len > UINT32_MAX - off) return false;
uint32_t done = 0;
while (done < len) {
uint16_t chunk = (uint16_t)(len - done > MOTA_SEEDER_READ_MAX
? MOTA_SEEDER_READ_MAX : len - done);
uint8_t args[7];
args[0] = idx;
wr_u32le(args + 1, off + done);
args[5] = (uint8_t)(chunk & 0xFF); args[6] = (uint8_t)(chunk >> 8);
if (!txn(MS_OP_READ, args, 7, buf + done, chunk)) return false;
done += chunk;
}
return true;
}
} // namespace ota
+109 -20
View File
@@ -362,28 +362,24 @@ void OtaManager::handleQuery(const uint8_t* m, uint16_t n) {
}
}
void OtaManager::handleGetManifest(const uint8_t* m, uint16_t n) {
bool OtaManager::handleGetManifest(const uint8_t* m, uint16_t n) {
GetManifestMsg gm;
if (!decode_get_manifest(m, n, gm)) return;
if (!decode_get_manifest(m, n, gm)) return false;
ServeView* v = resolve(gm.manifest_id);
if (!v) return;
// A signed v2 manifest (with hw_id[32]) exceeds one LoRa packet, so send it as fragments. Each carries
// up to OTA_MF_FRAG manifest bytes; the client reassembles by frag_idx. Only the fragments requested in
// want_mask are sent (0xFFFF = all), so a retry re-sends just the holes, not the whole manifest.
uint32_t mfl = v->mfl;
const uint8_t* src = v->m.manifest_start;
uint8_t ftotal = (uint8_t)((mfl + OTA_MF_FRAG - 1) / OTA_MF_FRAG); if (ftotal == 0) ftotal = 1;
for (uint8_t fi = 0; fi < ftotal; fi++) {
if (!(gm.want_mask & (1u << fi))) continue; // fetcher didn't ask for this manifest fragment
uint32_t off = (uint32_t)fi * OTA_MF_FRAG;
uint32_t fl = mfl - off; if (fl > OTA_MF_FRAG) fl = OTA_MF_FRAG;
ManifestMsg mm;
memcpy(mm.manifest_id, v->m.merkle_root, 4);
mm.frag_idx = fi; mm.frag_total = ftotal;
mm.bytes = src + off; mm.len = (uint16_t)fl;
uint8_t b[MAX_PACKET_PAYLOAD];
emit(b, encode_manifest(b, sizeof(b), mm), false);
if (!v) return false;
OTA_DBG("OTA: GET_MANIFEST want=%04x pending=%u\n",
(unsigned)gm.want_mask, (unsigned)_n_manifest_jobs);
// Retain the request and let serviceEgress admit its fragments one at a time. In particular, a deployed
// receiver with the legacy 32-second SF5 flood delay must receive both fragments from the first request;
// serializing fragment 1 behind the receiver's next GET would exhaust its manifest retry window.
const bool queued = queueManifestJob(v->m.merkle_root, gm.want_mask);
if (!queued) {
OTA_DBG("OTA: GET_MANIFEST response queue full\n");
}
// A node that successfully queued the requested MID is the terminal source. Do not also re-flood the
// request; that echo competes with the manifest on the same temporary-radio channel. If the bounded queue
// is full, return false so another source or a later retry can service it.
return queued;
}
// Serve the target's merkle leaves[] in fragments (for a motatool folder-capture warm-start). Only the
@@ -434,12 +430,101 @@ bool OtaManager::queueServeJob(const uint8_t* mid, uint16_t block, uint16_t want
return true;
}
bool OtaManager::queueManifestJob(const uint8_t* mid, uint16_t want_mask) {
for (uint8_t i = 0; i < _n_manifest_jobs; i++) {
ManifestServeJob& job = _manifest_jobs[i];
if (memcmp(job.mid, mid, 4) != 0) continue;
job.pending_mask |= (uint16_t)(want_mask & ~job.emitted_mask);
return true;
}
if (_n_manifest_jobs >= OTA_MANIFEST_SERVE_QUEUE) return false;
ManifestServeJob& job = _manifest_jobs[_n_manifest_jobs++];
memcpy(job.mid, mid, 4);
job.pending_mask = want_mask;
job.emitted_mask = 0;
job.ready_at = _now_ms + manifestEgressGapMs();
return true;
}
uint32_t OtaManager::manifestEgressGapMs() const {
if (_radio_packet_airtime_ms == 0) return OTA_MANIFEST_EGRESS_MIN_GAP_MS;
uint64_t gap = (uint64_t)_radio_packet_airtime_ms * _tx_spacing_permille;
gap = (gap + 999u) / 1000u;
if (gap < OTA_MANIFEST_EGRESS_MIN_GAP_MS) gap = OTA_MANIFEST_EGRESS_MIN_GAP_MS;
if (gap > OTA_MANIFEST_EGRESS_MAX_GAP_MS) gap = OTA_MANIFEST_EGRESS_MAX_GAP_MS;
return (uint32_t)gap;
}
void OtaManager::clearPendingEgress() {
_n_manifest_jobs = 0;
_n_serve_jobs = 0;
_serve_block_len = 0;
_serve_block_loaded = false;
}
void OtaManager::popManifestJob() {
if (_n_manifest_jobs == 0) return;
for (uint8_t i = 1; i < _n_manifest_jobs; i++) _manifest_jobs[i - 1] = _manifest_jobs[i];
_n_manifest_jobs--;
}
bool OtaManager::serviceManifestEgress() {
if (_n_manifest_jobs == 0) return false;
ManifestServeJob& job = _manifest_jobs[0];
if ((int32_t)(_now_ms - job.ready_at) < 0) {
return true;
}
ServeView* v = resolve(job.mid);
if (!v) {
popManifestJob();
return true;
}
const uint32_t mfl = v->mfl;
uint8_t ftotal = (uint8_t)((mfl + OTA_MF_FRAG - 1) / OTA_MF_FRAG);
if (ftotal == 0) ftotal = 1;
const uint16_t valid_mask = frag_full_mask(ftotal);
job.pending_mask &= valid_mask;
if (job.pending_mask == 0) {
popManifestJob();
return true;
}
uint8_t fragment = 0;
while (fragment < 16 && !(job.pending_mask & (1u << fragment))) fragment++;
if (fragment >= ftotal) {
popManifestJob();
return true;
}
const uint32_t off = (uint32_t)fragment * OTA_MF_FRAG;
uint32_t len = mfl - off;
if (len > OTA_MF_FRAG) len = OTA_MF_FRAG;
ManifestMsg message;
memcpy(message.manifest_id, v->m.merkle_root, 4);
message.frag_idx = fragment;
message.frag_total = ftotal;
message.bytes = v->m.manifest_start + off;
message.len = (uint16_t)len;
uint8_t wire[MAX_PACKET_PAYLOAD];
const uint16_t wire_len = encode_manifest(wire, sizeof(wire), message);
if (emit(wire, wire_len, false)) {
OTA_DBG("OTA: MANIFEST tx frag=%u/%u len=%u\n",
(unsigned)fragment, (unsigned)ftotal, (unsigned)len);
const uint16_t bit = (uint16_t)(1u << fragment);
job.pending_mask &= (uint16_t)~bit;
job.emitted_mask |= bit;
if (job.pending_mask == 0) {
popManifestJob();
} else {
// Follow the active packet airtime and dispatcher duty spacing while
// retaining a floor for fast radios' TX-to-RX turnaround.
job.ready_at = _now_ms + manifestEgressGapMs();
}
}
return true;
}
void OtaManager::popServeJob() {
if (_n_serve_jobs == 0) return;
for (uint8_t i = 1; i < _n_serve_jobs; i++) _serve_jobs[i - 1] = _serve_jobs[i];
@@ -497,6 +582,10 @@ bool OtaManager::handleReqProof(const uint8_t* m, uint16_t n) {
}
void OtaManager::serviceEgress() {
// Metadata comes first so an older receiver gets every manifest fragment before its retry horizon. The
// send callback supplies radio-queue backpressure, and a rejected fragment remains in this descriptor.
if (serviceManifestEgress()) return;
// A proactive proof normally follows the final DATA fragment. If it was lost, or the source is older and
// never sent one, issue the legacy proof request after a short grace. Stay RX-silent while another slot
// still expects DATA, and restart the grace on every new packet, so this fallback cannot collide with a
@@ -1586,7 +1675,7 @@ bool OtaManager::on_message(const uint8_t* msg, uint16_t len) {
case OTA_ADV: handleAdv(msg, len); return false;
case OTA_QUERY: handleQuery(msg, len); return false;
case OTA_HAVE: handleHave(msg, len); return false;
case OTA_GET_MANIFEST: handleGetManifest(msg, len); return false;
case OTA_GET_MANIFEST: return handleGetManifest(msg, len);
case OTA_MANIFEST: handleManifest(msg, len); return false;
case OTA_GET_LEAVES: handleGetLeaves(msg, len); return false;
case OTA_LEAVES: handleLeaves(msg, len); return false;
+23 -1
View File
@@ -59,6 +59,15 @@ typedef bool (*ServeReadFn)(void* ctx, uint32_t off, uint8_t* buf, uint32_t len)
#ifndef OTA_MANIFEST_MAX_RETRY
#define OTA_MANIFEST_MAX_RETRY 20 // give up (FAILED) after this many GET_MANIFEST retries - frees the slot
#endif
#ifndef OTA_MANIFEST_SERVE_QUEUE
#define OTA_MANIFEST_SERVE_QUEUE 4 // bounded manifest response descriptors (fragments are emitted one at a time)
#endif
#ifndef OTA_MANIFEST_EGRESS_MIN_GAP_MS
#define OTA_MANIFEST_EGRESS_MIN_GAP_MS 100 // minimum fast-link TX-to-RX turnaround allowance
#endif
#ifndef OTA_MANIFEST_EGRESS_MAX_GAP_MS
#define OTA_MANIFEST_EGRESS_MAX_GAP_MS 1000 // do not outrun the receiver's one-second retry observation
#endif
// Leaf-diff warm-start (motatool folder-capture only): bulk-fetch the target's leaves[], diff a seed build
// locally, pull DATA only for mismatches. OTA_LEAVES is bitmap-fragmented like OTA_MANIFEST so a want_mask
// retry never re-bursts (anti-deadlock). The mask is a fixed uint16, so leaves are capped at MAXFRAG frags.
@@ -386,6 +395,7 @@ public:
void serviceEgress();
void clearPendingEgress();
uint8_t pendingServeJobs() const { return _n_serve_jobs; }
uint8_t pendingManifestJobs() const { return _n_manifest_jobs; }
// Drop the current fetch session back to IDLE so a fresh `ota pull` / advert starts a new one.
void reset_session() {
@@ -446,7 +456,7 @@ private:
void handleAdv(const uint8_t* m, uint16_t n); // beacon -> sources table (+ query if interested)
void handleQuery(const uint8_t* m, uint16_t n); // serve: reply OTA_HAVE catalog
void handleHave(const uint8_t* m, uint16_t n); // peer: catalog rows (+ startFetch if a row matches)
void handleGetManifest(const uint8_t* m, uint16_t n);
bool handleGetManifest(const uint8_t* m, uint16_t n);
void handleManifest(const uint8_t* m, uint16_t n);
void handleGetLeaves(const uint8_t* m, uint16_t n); // serve: send the requested leaf fragments
void handleLeaves(const uint8_t* m, uint16_t n); // fetcher (validate): reassemble the target leaves
@@ -481,6 +491,10 @@ private:
void registerSelfEntry(); // (re)build entry[0] from view0
static bool srcReadTramp(void* c, uint32_t off, uint8_t* buf, uint32_t len); // source payload reader
bool queueServeJob(const uint8_t* mid, uint16_t block, uint16_t want_mask);
bool queueManifestJob(const uint8_t* mid, uint16_t want_mask);
uint32_t manifestEgressGapMs() const;
bool serviceManifestEgress();
void popManifestJob();
bool loadActiveServeBlock();
void popServeJob();
void sendQuery(const uint8_t* seeder, const uint8_t* digest, uint32_t filter_target,
@@ -544,6 +558,14 @@ private:
};
ServeJob _serve_jobs[OTA_SERVE_QUEUE];
uint8_t _n_serve_jobs = 0;
struct ManifestServeJob {
uint8_t mid[4];
uint16_t pending_mask;
uint16_t emitted_mask;
uint32_t ready_at;
};
ManifestServeJob _manifest_jobs[OTA_MANIFEST_SERVE_QUEUE];
uint8_t _n_manifest_jobs = 0;
uint8_t _serve_block[OTA_MAX_BLOCK];
uint16_t _serve_block_len = 0;
bool _serve_block_loaded = false;