Prioritize and pace private TempRadio OTA
This commit is contained in:
1 parent
f7c2d382c3
commit
cd824765b4
15 files changed
+851
-232
No files matched your search
+55
-11
@@ -153,13 +153,37 @@ static uint8_t otaTrafficPriority(const uint8_t* msg, uint16_t len) {
|
||||
return isPrimaryOtaTraffic(msg, len) ? OTA_TRANSFER_TX_PRIORITY : OTA_TX_PRIORITY;
|
||||
}
|
||||
|
||||
static bool isPacedOtaResponse(const uint8_t* msg, uint16_t len) {
|
||||
if (!msg || len == 0) return false;
|
||||
return msg[0] == ota::OTA_DATA || msg[0] == ota::OTA_PROOF;
|
||||
}
|
||||
|
||||
#if defined(ENABLE_OTA)
|
||||
static int queuedPacedOtaResponses(PacketManager* manager) {
|
||||
int paced = 0;
|
||||
const int total = manager->getOutboundTotal();
|
||||
for (int i = 0; i < total; i++) {
|
||||
Packet* packet = manager->getOutboundByIdx(i);
|
||||
if (packet != NULL && packet->getPayloadType() == PAYLOAD_TYPE_OTA
|
||||
&& isPacedOtaResponse(packet->payload, packet->payload_len)) {
|
||||
paced++;
|
||||
}
|
||||
}
|
||||
return paced;
|
||||
}
|
||||
|
||||
// Adapter so the portable OtaManager can emit packets through the mesh (message-priority, hop-capped).
|
||||
void Mesh::otaSendAdapter(void* ctx, const uint8_t* msg, uint16_t len, bool /*flood*/) {
|
||||
// DATA/PROOF admission is credit-limited; false leaves the manager's response descriptor intact.
|
||||
bool Mesh::otaSendAdapter(void* ctx, const uint8_t* msg, uint16_t len, bool /*flood*/) {
|
||||
Mesh* m = (Mesh*)ctx;
|
||||
if (!m->isTempRadioActive()) return;
|
||||
if (!m->isTempRadioActive()) return false;
|
||||
if (isPacedOtaResponse(msg, len)
|
||||
&& (queuedPacedOtaResponses(m->_mgr) >= OTA_EGRESS_QUEUE_CREDIT
|
||||
|| m->_mgr->getFreeCount() <= OTA_EGRESS_MIN_FREE)) {
|
||||
return false;
|
||||
}
|
||||
Packet* p = m->createOtaPacket(msg, len);
|
||||
if (p) m->sendOtaFlood(p);
|
||||
return p && m->sendOtaFlood(p);
|
||||
}
|
||||
|
||||
// Runtime OTA flood reach (`ota config hops`, persisted in NodePrefs): accept packets up to N hops away and
|
||||
@@ -318,6 +342,10 @@ void __attribute__((noinline)) Mesh::serviceLoopMaintenance() {
|
||||
#endif
|
||||
const bool ota_active = isTempRadioActive();
|
||||
if (!ota_active) {
|
||||
if (_ota_temp_was_active) {
|
||||
ota::ota_ctx().manager.clearPendingEgress();
|
||||
resetOtaRelayBackoff();
|
||||
}
|
||||
_ota_temp_was_active = false;
|
||||
return;
|
||||
}
|
||||
@@ -327,6 +355,8 @@ void __attribute__((noinline)) Mesh::serviceLoopMaintenance() {
|
||||
_next_ota_announce = 0;
|
||||
_ota_announce_count = 0;
|
||||
}
|
||||
ota::ota_ctx().manager.set_clock(_ms->getMillis());
|
||||
ota::ota_ctx().manager.serviceEgress();
|
||||
if (millisHasNowPassed(_next_ota_tick)) {
|
||||
// one-shot on first tick: resume an interrupted fetch left staged in flash before a reboot. Only adopt
|
||||
// a PARTIAL container (continue fetching the holes); a COMPLETE one is left for manual/auto-install,
|
||||
@@ -341,12 +371,11 @@ void __attribute__((noinline)) Mesh::serviceLoopMaintenance() {
|
||||
}
|
||||
}
|
||||
#endif
|
||||
ota::ota_ctx().manager.set_clock(_ms->getMillis()); // for discovery jitter/ages + the pending-query timer
|
||||
ota::ota_ctx().manager.loop(); // re-request still-missing OTA blocks + fire scheduled queries
|
||||
#if defined(NRF52_PLATFORM) && defined(OTA_SD_STORE)
|
||||
ota::ota_ctx().serviceSdCache(_ms->getMillis()); // archive unseen mOTAs only while the receive slot is idle
|
||||
#endif
|
||||
_next_ota_tick = futureMillis(3000);
|
||||
_next_ota_tick = futureMillis(OTA_RETRY_TICK_MS);
|
||||
}
|
||||
if (millisHasNowPassed(_next_ota_announce)) { // auto-advertise so peers discover us (tiny beacon)
|
||||
ota::OtaContext& oc = ota::ota_ctx();
|
||||
@@ -485,6 +514,7 @@ 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;
|
||||
decayOtaRelayBackoff();
|
||||
uint32_t airtime = _radio->getEstAirtimeFor(packet->getRawLength());
|
||||
if (airtime == 0) return 0;
|
||||
@@ -498,6 +528,23 @@ uint32_t Mesh::getOtaRetransmitDelay(const mesh::Packet* packet) {
|
||||
if (max_delay < min_delay) max_delay = min_delay;
|
||||
return _rng->nextInt(min_delay, max_delay + 1);
|
||||
}
|
||||
|
||||
int Mesh::calcRxDelayForPacket(const Packet* packet, float score, uint32_t air_time) {
|
||||
if (packet != NULL && packet->getPayloadType() == PAYLOAD_TYPE_OTA
|
||||
&& isTempRadioActive()) {
|
||||
return 0;
|
||||
}
|
||||
return Dispatcher::calcRxDelayForPacket(packet, score, air_time);
|
||||
}
|
||||
|
||||
uint32_t Mesh::getCADFailRetryDelay() const {
|
||||
if (!isTempRadioActive()) return _rng->nextInt(1, 4) * 120;
|
||||
uint32_t airtime = _radio->getEstAirtimeFor(MAX_TRANS_UNIT);
|
||||
uint32_t retry = airtime / 4;
|
||||
if (retry < 5) retry = 5;
|
||||
if (retry > 50) retry = 50;
|
||||
return retry;
|
||||
}
|
||||
uint32_t Mesh::getDirectRetransmitDelay(const Packet* packet) {
|
||||
return 0; // by default, no delay
|
||||
}
|
||||
@@ -582,6 +629,7 @@ uint8_t Mesh::applyFloodRetryAttemptPolicy(const Packet* packet,
|
||||
|
||||
switch (packet->getPayloadType()) {
|
||||
case PAYLOAD_TYPE_REQ:
|
||||
case PAYLOAD_TYPE_OTA:
|
||||
return 0;
|
||||
case PAYLOAD_TYPE_GRP_TXT:
|
||||
return attempts;
|
||||
@@ -654,10 +702,6 @@ void Mesh::onSendFail(Packet* packet) {
|
||||
clearPendingFloodRetryOnSendFail(packet);
|
||||
}
|
||||
|
||||
uint32_t Mesh::getCADFailRetryDelay() const {
|
||||
return _rng->nextInt(1, 4)*120;
|
||||
}
|
||||
|
||||
int Mesh::searchPeersByHash(const uint8_t* hash) {
|
||||
return 0; // not found
|
||||
}
|
||||
@@ -2639,12 +2683,12 @@ Packet* Mesh::createOtaPacket(const uint8_t* data, size_t len) {
|
||||
return packet;
|
||||
}
|
||||
|
||||
void Mesh::sendOtaFlood(Packet* packet, uint32_t delay_millis) {
|
||||
bool Mesh::sendOtaFlood(Packet* packet, uint32_t delay_millis) {
|
||||
packet->header &= ~PH_ROUTE_MASK;
|
||||
packet->header |= ROUTE_TYPE_FLOOD;
|
||||
packet->setPathHashSizeAndCount(1, 0);
|
||||
_tables->markSeen(packet); // mark as sent, in case it floods back to us
|
||||
sendPacket(packet, otaTrafficPriority(packet->payload, packet->payload_len), delay_millis);
|
||||
return sendPacket(packet, otaTrafficPriority(packet->payload, packet->payload_len), delay_millis);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
+23
-5
@@ -17,6 +17,19 @@
|
||||
#ifndef OTA_FWD_MIN_FREE
|
||||
#define OTA_FWD_MIN_FREE 4
|
||||
#endif
|
||||
// The OTA manager emits one retained response at a time, and this adapter keeps only a small number of OTA
|
||||
// responses queued behind the packet currently on air. Unrelated queued traffic does not block the primary
|
||||
// transfer. The free-pool floor preserves RX capacity and gives private-network relays a chance to forward
|
||||
// each fragment before the origin fills the channel again.
|
||||
#ifndef OTA_EGRESS_QUEUE_CREDIT
|
||||
#define OTA_EGRESS_QUEUE_CREDIT 2
|
||||
#endif
|
||||
#ifndef OTA_EGRESS_MIN_FREE
|
||||
#define OTA_EGRESS_MIN_FREE 4
|
||||
#endif
|
||||
#ifndef OTA_RETRY_TICK_MS
|
||||
#define OTA_RETRY_TICK_MS 1000
|
||||
#endif
|
||||
|
||||
namespace mesh {
|
||||
|
||||
@@ -214,10 +227,15 @@ protected:
|
||||
virtual uint32_t getRetransmitDelay(const Packet* packet);
|
||||
|
||||
/**
|
||||
* \returns Adaptive TempRadio-only OTA relay delay, from 0.25-0.5 airtimes up to a 3-airtime cap.
|
||||
* \returns TempRadio OTA relay delay. Primary transfer packets forward immediately on the private
|
||||
* network; discovery retains collision jitter.
|
||||
*/
|
||||
virtual uint32_t getOtaRetransmitDelay(const Packet* packet);
|
||||
|
||||
// Private TempRadio traffic has one intended path, so do not pay the public-flood receive holdoff. Keep
|
||||
// CAD enabled, but retry it at radio-time scale instead of the ordinary 200 ms control-traffic cadence.
|
||||
int calcRxDelayForPacket(const Packet* packet, float score, uint32_t air_time) override;
|
||||
|
||||
/**
|
||||
* \returns number of milliseconds delay to apply to retransmitting the given packet, for DIRECT mode.
|
||||
*/
|
||||
@@ -481,7 +499,7 @@ protected:
|
||||
|
||||
// OTA mesh-integration is centralized in Mesh::begin()/loop()/dispatch, so every role (repeater,
|
||||
// companion, room, sensor, ...) gets fetch/serve/apply without per-example wiring.
|
||||
static void otaSendAdapter(void* ctx, const uint8_t* msg, uint16_t len, bool flood);
|
||||
static bool otaSendAdapter(void* ctx, const uint8_t* msg, uint16_t len, bool flood);
|
||||
unsigned long _next_ota_tick = 0;
|
||||
unsigned long _next_ota_announce = 0; // advertisements are scheduled only while temp radio is active
|
||||
uint8_t _ota_announce_count = 0; // adverts sent so far (boot burst before settling to daily)
|
||||
@@ -549,9 +567,9 @@ public:
|
||||
#if defined(ENABLE_OTA)
|
||||
// Build a PAYLOAD_TYPE_OTA packet from raw OTA message bytes (route set by sendOtaFlood).
|
||||
Packet* createOtaPacket(const uint8_t* data, size_t len);
|
||||
// Flood-send at the priority derived from the OTA message: discovery is background, active transfer is
|
||||
// primary. Relay-only nodes use the same classification.
|
||||
void sendOtaFlood(Packet* packet, uint32_t delay_millis = 0);
|
||||
// The flood-shaped header is only the bounded multi-hop/dedup envelope inside TempRadio. Active transfer
|
||||
// packets are primary and do not use generic flood retries or public-network relay backoff.
|
||||
bool sendOtaFlood(Packet* packet, uint32_t delay_millis = 0);
|
||||
#endif
|
||||
Packet* createTrace(uint32_t tag, uint32_t auth_code, uint8_t flags = 0);
|
||||
Packet* createControlData(const uint8_t* data, size_t len);
|
||||
|
||||
@@ -81,8 +81,8 @@ enum OtaMsgType : uint8_t {
|
||||
OTA_MANIFEST = 0x05,
|
||||
OTA_REQ = 0x06, // request a window of blocks' DATA fragments
|
||||
OTA_DATA = 0x07, // one fragment of a block's data (self-describing by frag_off; no proof)
|
||||
OTA_REQ_PROOF = 0x08, // request the merkle proof for one block (data + proof are fetched separately)
|
||||
OTA_PROOF = 0x09, // the merkle proof for one block
|
||||
OTA_REQ_PROOF = 0x08, // request/re-request proof (legacy-source or lost proactive-proof fallback)
|
||||
OTA_PROOF = 0x09, // the merkle proof for one block (normally follows its paced DATA)
|
||||
OTA_GET_LEAVES = 0x0A, // request the target's merkle leaves[] (motatool warm-start; bitmap-fragmented)
|
||||
OTA_LEAVES = 0x0B, // a fragment of the leaves[] array (for host-side seed leaf-diff)
|
||||
};
|
||||
|
||||
+143
-43
@@ -48,6 +48,7 @@ void OtaManager::begin(uint32_t my_target_id, OtaSend send, void* ctx) {
|
||||
_resume_merkle.reset();
|
||||
_n_serve = 0; _n_src_obj = 0; _view0.valid = false; _srcv.valid = false;
|
||||
_n_src = 0; _n_cat = 0;
|
||||
clearPendingEgress();
|
||||
}
|
||||
|
||||
// ---------------- serve (multi-mota registry) ----------------
|
||||
@@ -65,6 +66,7 @@ bool OtaManager::serve(const uint8_t* mota, uint32_t len) {
|
||||
_view0.read = nullptr; _view0.read_ctx = nullptr; // payload is contiguous _view0.m.payload
|
||||
_view0.scratch = scratch; _view0.scratch_sz = OTA_PROOFGEN_SCRATCH; // <=1024 blocks (RAM .mota is small)
|
||||
_view0.valid = true;
|
||||
clearPendingEgress();
|
||||
registerSelfEntry();
|
||||
return true;
|
||||
}
|
||||
@@ -81,6 +83,7 @@ bool OtaManager::serve_self(const uint8_t* manifest, uint16_t mfl, const uint8_t
|
||||
_view0.mfl = mfl; _view0.read = read; _view0.read_ctx = ctx;
|
||||
_view0.scratch = proof_scratch; _view0.scratch_sz = proof_scratch_sz; // sized for our (large) image
|
||||
_view0.valid = true;
|
||||
clearPendingEgress();
|
||||
registerSelfEntry();
|
||||
return true;
|
||||
}
|
||||
@@ -127,6 +130,7 @@ bool OtaManager::remove_source(MotaSource* src) {
|
||||
}
|
||||
|
||||
void OtaManager::refresh_sources() {
|
||||
clearPendingEgress();
|
||||
_n_serve = 0;
|
||||
if (_view0.valid) registerSelfEntry();
|
||||
for (uint8_t s = 0; s < _n_src_obj; s++) {
|
||||
@@ -163,12 +167,14 @@ void OtaManager::refresh_sources() {
|
||||
}
|
||||
|
||||
void OtaManager::clear_sources() {
|
||||
clearPendingEgress();
|
||||
_n_src_obj = 0; _srcv.valid = false;
|
||||
_n_serve = 0;
|
||||
if (_view0.valid) registerSelfEntry();
|
||||
}
|
||||
|
||||
void OtaManager::clear_primary() {
|
||||
clearPendingEgress();
|
||||
_view0.valid = false;
|
||||
if (_n_serve > 0 && _serve[0].is_self) {
|
||||
for (uint8_t i = 1; i < _n_serve; i++) {
|
||||
@@ -377,21 +383,56 @@ static inline uint16_t frag_full_mask(uint32_t nf) {
|
||||
return (nf >= 16) ? 0xFFFFu : (uint16_t)((1u << nf) - 1);
|
||||
}
|
||||
|
||||
// Emit one block's data as self-describing DATA fragments (frag_off); only the fragments whose bit is set
|
||||
// in `want_mask` are sent (bit k = the slice at k*OTA_FRAG_DATA). The proof is fetched separately.
|
||||
void OtaManager::emitBlockData(const uint8_t* mid, uint32_t idx, const uint8_t* data, uint32_t blen,
|
||||
uint16_t want_mask) {
|
||||
uint32_t k = 0;
|
||||
for (uint32_t fo = 0; fo < blen; fo += OTA_FRAG_DATA, k++) {
|
||||
if (!(want_mask & (1u << k))) continue; // fetcher didn't ask for this fragment
|
||||
uint32_t fl = (fo + OTA_FRAG_DATA <= blen) ? OTA_FRAG_DATA : (blen - fo);
|
||||
DataMsg dm;
|
||||
memcpy(dm.manifest_id, mid, 4);
|
||||
dm.block_idx = (uint16_t)idx; dm.frag_off = (uint16_t)fo;
|
||||
dm.data = data + fo; dm.data_len = (uint16_t)fl;
|
||||
uint8_t b[MAX_PACKET_PAYLOAD];
|
||||
emit(b, encode_data(b, sizeof(b), dm), false);
|
||||
// Retain a bounded descriptor instead of allocating every DATA packet inline. Repeated requests that arrive
|
||||
// while a block is already queued merge only fragments not yet admitted to the radio queue; a later retry can
|
||||
// enqueue the block again if one of those admitted packets was actually lost over the air.
|
||||
bool OtaManager::queueServeJob(const uint8_t* mid, uint16_t block, uint16_t want_mask) {
|
||||
for (uint8_t i = 0; i < _n_serve_jobs; i++) {
|
||||
ServeJob& job = _serve_jobs[i];
|
||||
if (job.block != block || memcmp(job.mid, mid, 4) != 0) continue;
|
||||
job.pending_mask |= (uint16_t)(want_mask & ~job.emitted_mask);
|
||||
return true;
|
||||
}
|
||||
if (_n_serve_jobs >= OTA_SERVE_QUEUE) return false;
|
||||
ServeJob& job = _serve_jobs[_n_serve_jobs++];
|
||||
memcpy(job.mid, mid, 4);
|
||||
job.block = block;
|
||||
job.pending_mask = want_mask;
|
||||
job.emitted_mask = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
void OtaManager::clearPendingEgress() {
|
||||
_n_serve_jobs = 0;
|
||||
_serve_block_len = 0;
|
||||
_serve_block_loaded = false;
|
||||
}
|
||||
|
||||
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];
|
||||
_n_serve_jobs--;
|
||||
_serve_block_len = 0;
|
||||
_serve_block_loaded = false;
|
||||
}
|
||||
|
||||
bool OtaManager::loadActiveServeBlock() {
|
||||
if (_n_serve_jobs == 0) return false;
|
||||
ServeJob& job = _serve_jobs[0];
|
||||
ServeView* v = resolve(job.mid);
|
||||
if (!v || job.block >= v->m.block_count) return false;
|
||||
uint32_t bs = v->m.block_size();
|
||||
if (bs == 0 || bs > OTA_MAX_BLOCK) return false;
|
||||
uint32_t off = (uint32_t)job.block * bs;
|
||||
uint32_t blen = (off + bs <= v->m.payload_size) ? bs : (v->m.payload_size - off);
|
||||
if (v->read) {
|
||||
if (!v->read(v->read_ctx, off, _serve_block, blen)) return false;
|
||||
} else {
|
||||
memcpy(_serve_block, v->m.payload + off, blen);
|
||||
}
|
||||
_serve_block_len = (uint16_t)blen;
|
||||
_serve_block_loaded = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void OtaManager::handleReq(const uint8_t* m, uint16_t n) {
|
||||
@@ -399,33 +440,84 @@ void OtaManager::handleReq(const uint8_t* m, uint16_t n) {
|
||||
if (!decode_req(m, n, rq)) return;
|
||||
uint32_t idx = rq.block_idx;
|
||||
ServeView* v = resolve(rq.manifest_id);
|
||||
if (v) { // serve a fully-held mota (own fw or attached folder)
|
||||
if (idx >= v->m.block_count) return;
|
||||
uint32_t bs = v->m.block_size();
|
||||
uint32_t off = idx * bs;
|
||||
uint32_t blen = (off + bs <= v->m.payload_size) ? bs : (v->m.payload_size - off);
|
||||
uint8_t blk[OTA_MAX_BLOCK];
|
||||
const uint8_t* data;
|
||||
if (v->read) { if (!v->read(v->read_ctx, off, blk, blen)) return; data = blk; }
|
||||
else { data = v->m.payload + off; }
|
||||
emitBlockData(v->m.merkle_root, idx, data, blen, rq.want_mask);
|
||||
}
|
||||
if (!v || idx >= v->m.block_count || idx > 0xFFFFu) return;
|
||||
uint32_t blen = v->m.block_size();
|
||||
uint32_t off = idx * blen;
|
||||
if (off + blen > v->m.payload_size) blen = v->m.payload_size - off;
|
||||
uint16_t valid_mask = frag_full_mask((blen + OTA_FRAG_DATA - 1) / OTA_FRAG_DATA);
|
||||
uint16_t want = (uint16_t)(rq.want_mask & valid_mask);
|
||||
if (want != 0) queueServeJob(v->m.merkle_root, (uint16_t)idx, want);
|
||||
}
|
||||
|
||||
void OtaManager::handleReqProof(const uint8_t* m, uint16_t n) {
|
||||
ReqProofMsg rp;
|
||||
if (!decode_req_proof(m, n, rp)) return;
|
||||
ServeView* v = resolve(rp.manifest_id);
|
||||
if (!v) return;
|
||||
if (rp.block_idx >= v->m.block_count) return;
|
||||
if ((uint64_t)v->m.block_count * 4 > v->scratch_sz) return; // proof-gen needs block_count*4 scratch
|
||||
if (!v || rp.block_idx >= v->m.block_count) return;
|
||||
if ((uint64_t)v->m.block_count * 4 > v->scratch_sz) return;
|
||||
queueServeJob(v->m.merkle_root, rp.block_idx, 0); // proof-only, or merge with queued DATA
|
||||
}
|
||||
|
||||
void OtaManager::serviceEgress() {
|
||||
// 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. Admit at most one fallback per call.
|
||||
if (_fstate == FETCHING) {
|
||||
for (uint8_t i = 0; i < OTA_FETCH_PIPELINE; i++) {
|
||||
ReassemblySlot& slot = _reasm[i];
|
||||
if (!slot.awaiting_proof || slot.proof_request_at == 0
|
||||
|| (int32_t)(_now_ms - slot.proof_request_at) < 0) continue;
|
||||
if (requestSlot(i)) return;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (_n_serve_jobs == 0) return;
|
||||
ServeJob& job = _serve_jobs[0];
|
||||
ServeView* v = resolve(job.mid);
|
||||
if (!v || job.block >= v->m.block_count
|
||||
|| (uint64_t)v->m.block_count * 4 > v->scratch_sz) {
|
||||
popServeJob();
|
||||
return;
|
||||
}
|
||||
|
||||
if (job.pending_mask != 0) {
|
||||
if (!_serve_block_loaded && !loadActiveServeBlock()) {
|
||||
popServeJob();
|
||||
return;
|
||||
}
|
||||
uint8_t fragment = 0;
|
||||
while (fragment < 16 && !(job.pending_mask & (1u << fragment))) fragment++;
|
||||
uint32_t frag_off = (uint32_t)fragment * OTA_FRAG_DATA;
|
||||
if (fragment >= 16 || frag_off >= _serve_block_len) {
|
||||
job.pending_mask = 0;
|
||||
return;
|
||||
}
|
||||
uint32_t frag_len = _serve_block_len - frag_off;
|
||||
if (frag_len > OTA_FRAG_DATA) frag_len = OTA_FRAG_DATA;
|
||||
DataMsg dm;
|
||||
memcpy(dm.manifest_id, job.mid, 4);
|
||||
dm.block_idx = job.block;
|
||||
dm.frag_off = (uint16_t)frag_off;
|
||||
dm.data = _serve_block + frag_off;
|
||||
dm.data_len = (uint16_t)frag_len;
|
||||
uint8_t b[MAX_PACKET_PAYLOAD];
|
||||
if (emit(b, encode_data(b, sizeof(b), dm), false)) {
|
||||
const uint16_t bit = (uint16_t)(1u << fragment);
|
||||
job.pending_mask &= (uint16_t)~bit;
|
||||
job.emitted_mask |= bit;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t proof[32 * 4];
|
||||
uint8_t np = merkle_gen_proof(v->m.leaves, v->m.block_count, rp.block_idx, v->scratch, proof);
|
||||
uint8_t np = merkle_gen_proof(v->m.leaves, v->m.block_count, job.block, v->scratch, proof);
|
||||
ProofMsg pm;
|
||||
memcpy(pm.manifest_id, v->m.merkle_root, 4);
|
||||
pm.block_idx = rp.block_idx; pm.n_proof = np; pm.proof = proof;
|
||||
memcpy(pm.manifest_id, job.mid, 4);
|
||||
pm.block_idx = job.block;
|
||||
pm.n_proof = np;
|
||||
pm.proof = proof;
|
||||
uint8_t b[MAX_PACKET_PAYLOAD];
|
||||
emit(b, encode_proof(b, sizeof(b), pm), false);
|
||||
if (emit(b, encode_proof(b, sizeof(b), pm), false)) popServeJob();
|
||||
}
|
||||
|
||||
// ---------------- fetch ----------------
|
||||
@@ -613,6 +705,7 @@ void OtaManager::clearReassemblySlot(uint8_t slot) {
|
||||
_reasm[slot].mask = 0;
|
||||
_reasm[slot].need = 0;
|
||||
_reasm[slot].awaiting_proof = false;
|
||||
_reasm[slot].proof_request_at = 0;
|
||||
}
|
||||
|
||||
// Forget all blocks currently being reassembled or awaiting proofs.
|
||||
@@ -1010,10 +1103,12 @@ void OtaManager::handleData(const uint8_t* m, uint16_t n) {
|
||||
if (kf >= 16) return;
|
||||
memcpy(slot.buf + dm.frag_off, dm.data, dm.data_len);
|
||||
slot.mask |= (uint16_t)(1u << kf);
|
||||
if (slot.mask != slot.need || slot.awaiting_proof) return; // wait for all slices (or proof already asked)
|
||||
// block fully reassembled -> request its proof (data + proof are fetched separately)
|
||||
if (slot.mask != slot.need || slot.awaiting_proof) return;
|
||||
// The paced server sends a proof immediately after this block's requested DATA. Wait briefly so that proof
|
||||
// can arrive without another request/response turn. An older server, or a lost proof, falls back through
|
||||
// serviceEgress() to the existing REQ_PROOF wire message.
|
||||
slot.awaiting_proof = true;
|
||||
requestSlot((uint8_t)slot_index);
|
||||
slot.proof_request_at = _now_ms + OTA_PROOF_GRACE_MS;
|
||||
}
|
||||
|
||||
void OtaManager::handleProof(const uint8_t* m, uint16_t n) {
|
||||
@@ -1055,10 +1150,10 @@ void OtaManager::handleProof(const uint8_t* m, uint16_t n) {
|
||||
OTA_DBG("OTA: transfer %s\n", _fstate == COMPLETE ? "COMPLETE" : "FAILED(integrity/storage)");
|
||||
}
|
||||
|
||||
void OtaManager::requestSlot(uint8_t slot_index) {
|
||||
if (_fstate != FETCHING || slot_index >= OTA_FETCH_PIPELINE) return;
|
||||
bool OtaManager::requestSlot(uint8_t slot_index) {
|
||||
if (_fstate != FETCHING || slot_index >= OTA_FETCH_PIPELINE) return false;
|
||||
ReassemblySlot& slot = _reasm[slot_index];
|
||||
if (slot.block >= _fbc) return;
|
||||
if (slot.block >= _fbc) return false;
|
||||
_req_start = slot.block;
|
||||
_req_count = 0;
|
||||
for (uint8_t i = 0; i < OTA_FETCH_PIPELINE; i++) {
|
||||
@@ -1066,10 +1161,14 @@ void OtaManager::requestSlot(uint8_t slot_index) {
|
||||
}
|
||||
if (slot.awaiting_proof) {
|
||||
ReqProofMsg rp; memcpy(rp.manifest_id, _fid, 4); rp.block_idx = (uint16_t)slot.block;
|
||||
uint8_t b[16]; emit(b, encode_req_proof(b, sizeof(b), rp), false);
|
||||
OTA_DBG("OTA: REQ_PROOF block=%u (have=%u/%u window=%u)\n",
|
||||
(unsigned)slot.block, (unsigned)_have, (unsigned)_fbc, (unsigned)_req_count);
|
||||
return;
|
||||
uint8_t b[16];
|
||||
bool sent = emit(b, encode_req_proof(b, sizeof(b), rp), false);
|
||||
if (sent) {
|
||||
slot.proof_request_at = 0;
|
||||
OTA_DBG("OTA: REQ_PROOF fallback block=%u (have=%u/%u window=%u)\n",
|
||||
(unsigned)slot.block, (unsigned)_have, (unsigned)_fbc, (unsigned)_req_count);
|
||||
}
|
||||
return sent;
|
||||
}
|
||||
uint16_t want = (uint16_t)(slot.need & ~slot.mask);
|
||||
if (want == 0) want = slot.need; // safety: never send an empty request
|
||||
@@ -1079,7 +1178,7 @@ void OtaManager::requestSlot(uint8_t slot_index) {
|
||||
OTA_DBG("OTA: REQ block=%u want=%04x (have=%u/%u mask=%04x window=%u)\n",
|
||||
(unsigned)slot.block, (unsigned)want, (unsigned)_have, (unsigned)_fbc,
|
||||
(unsigned)slot.mask, (unsigned)_req_count);
|
||||
emit(b, encode_req(b, sizeof(b), rq), false);
|
||||
return emit(b, encode_req(b, sizeof(b), rq), false);
|
||||
}
|
||||
|
||||
bool OtaManager::fillPipeline() {
|
||||
@@ -1094,6 +1193,7 @@ bool OtaManager::fillPipeline() {
|
||||
slot.mask = 0;
|
||||
slot.need = frag_full_mask((blockLen(block) + OTA_FRAG_DATA - 1) / OTA_FRAG_DATA);
|
||||
slot.awaiting_proof = false;
|
||||
slot.proof_request_at = 0;
|
||||
requestSlot((uint8_t)slot_index);
|
||||
requested = true;
|
||||
}
|
||||
|
||||
@@ -14,15 +14,16 @@
|
||||
// the signed merkle root via proofs). It is portable (no Arduino / radio / Ed25519) so it can be
|
||||
// driven by a host simulation; a thin Mesh adapter wires it to PAYLOAD_TYPE_OTA on device.
|
||||
//
|
||||
// Transfer is per-block and 2-phase: a 1 KB logical block is fetched as self-describing DATA fragments
|
||||
// (frag_off), reassembled, then its merkle PROOF
|
||||
// is requested separately and verified against the signed root before the block is committed.
|
||||
// Transfer keeps 1 KB logical blocks. A server paces each block's self-describing DATA fragments through a
|
||||
// bounded response queue and follows them with the Merkle PROOF. A newer fetcher waits briefly for that
|
||||
// proactive proof; the existing REQ_PROOF message remains as a loss/legacy-server fallback.
|
||||
|
||||
namespace mesh {
|
||||
namespace ota {
|
||||
|
||||
// Emit an OTA message (one packet payload). `flood`=true for announce/query, false for direct replies.
|
||||
typedef void (*OtaSend)(void* ctx, const uint8_t* msg, uint16_t len, bool flood);
|
||||
// Try to emit one OTA message (one packet payload). `flood`=true for announce/query, false for direct
|
||||
// replies. False applies backpressure: the manager retains paced DATA/PROOF egress and tries it again.
|
||||
typedef bool (*OtaSend)(void* ctx, const uint8_t* msg, uint16_t len, bool flood);
|
||||
|
||||
// Read `len` payload bytes at offset `off` from the serve source (flash-backed self-serve); false on
|
||||
// error. nullptr means the payload is a contiguous RAM buffer (the staged `.mota`).
|
||||
@@ -128,6 +129,15 @@ typedef bool (*ServeReadFn)(void* ctx, uint32_t off, uint8_t* buf, uint32_t len)
|
||||
#ifndef OTA_FRAG_DATA
|
||||
#define OTA_FRAG_DATA 160 // data bytes per DATA fragment (<= MAX_PACKET_PAYLOAD - 9-byte header)
|
||||
#endif
|
||||
#ifndef OTA_SERVE_QUEUE
|
||||
#define OTA_SERVE_QUEUE 4 // requested blocks retained without allocating one packet per fragment
|
||||
#endif
|
||||
#if OTA_SERVE_QUEUE < 1
|
||||
#error "OTA_SERVE_QUEUE must be at least 1"
|
||||
#endif
|
||||
#ifndef OTA_PROOF_GRACE_MS
|
||||
#define OTA_PROOF_GRACE_MS 500 // wait for the server's proactive proof before legacy REQ_PROOF fallback
|
||||
#endif
|
||||
#ifndef OTA_FETCH_PIPELINE
|
||||
#define OTA_FETCH_PIPELINE 2 // concurrent client block slots; keeps one block ready behind proof/flash work
|
||||
#endif
|
||||
@@ -151,7 +161,7 @@ typedef bool (*ServeReadFn)(void* ctx, uint32_t off, uint8_t* buf, uint32_t len)
|
||||
#error "OTA_FETCH_PIPELINE_GROW_BLOCKS must be at least 1"
|
||||
#endif
|
||||
#ifndef OTA_FETCH_PIPELINE_SHRINK_TICKS
|
||||
#define OTA_FETCH_PIPELINE_SHRINK_TICKS 2 // consecutive 3-second no-progress ticks before removing one slot
|
||||
#define OTA_FETCH_PIPELINE_SHRINK_TICKS 2 // consecutive no-progress retry ticks before removing one slot
|
||||
#endif
|
||||
#if OTA_FETCH_PIPELINE_SHRINK_TICKS < 1
|
||||
#error "OTA_FETCH_PIPELINE_SHRINK_TICKS must be at least 1"
|
||||
@@ -338,6 +348,11 @@ public:
|
||||
|
||||
void on_message(const uint8_t* msg, uint16_t len); // feed one received OTA message
|
||||
void loop(); // drive fetch (re-request missing blocks)
|
||||
// Fast, non-blocking service called from the main radio loop. It admits at most one retained server
|
||||
// response or proof fallback per call; the send callback provides packet-pool/queue backpressure.
|
||||
void serviceEgress();
|
||||
void clearPendingEgress();
|
||||
uint8_t pendingServeJobs() const { return _n_serve_jobs; }
|
||||
|
||||
// Drop the current fetch session back to IDLE so a fresh `ota pull` / advert starts a new one.
|
||||
void reset_session() {
|
||||
@@ -384,7 +399,9 @@ private:
|
||||
uint16_t catalogCapacity() const { return _catalog_heap ? OTA_MAX_CATALOG : OTA_INLINE_CATALOG; }
|
||||
bool expandCatalog();
|
||||
|
||||
void emit(const uint8_t* b, uint16_t n, bool flood) { if (_send && n) _send(_ctx, b, n, flood); }
|
||||
bool emit(const uint8_t* b, uint16_t n, bool flood) {
|
||||
return _send && n && _send(_ctx, b, n, flood);
|
||||
}
|
||||
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)
|
||||
@@ -415,8 +432,9 @@ private:
|
||||
bool loadSource(const ServeEntry& e); // load an external mota into _srcv (head+leaves)
|
||||
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
|
||||
void emitBlockData(const uint8_t* mid, uint32_t idx, const uint8_t* data, uint32_t blen,
|
||||
uint16_t want_mask); // emit only the requested DATA fragments of one block
|
||||
bool queueServeJob(const uint8_t* mid, uint16_t block, uint16_t want_mask);
|
||||
bool loadActiveServeBlock();
|
||||
void popServeJob();
|
||||
void sendQuery(const uint8_t* seeder, const uint8_t* digest, uint32_t filter_target,
|
||||
uint32_t want_fragments); // ask a source for all/selected catalog fragments
|
||||
void scheduleQuery(const uint8_t* seeder, const uint8_t* digest); // jittered + suppressible
|
||||
@@ -436,7 +454,7 @@ private:
|
||||
bool storedLeavesRootMatches() const;
|
||||
void beginStagedVerification();
|
||||
void verifyStagedStep();
|
||||
void requestSlot(uint8_t slot); // request DATA holes or the proof for one slot
|
||||
bool requestSlot(uint8_t slot); // request DATA holes or the proof for one slot
|
||||
bool fillPipeline(); // assign and request blocks until the window is full
|
||||
void requestMissing(); // fill the window, or retry one stalled slot
|
||||
uint32_t blockLen(uint32_t i) const;
|
||||
@@ -465,6 +483,20 @@ private:
|
||||
uint8_t _scratch[OTA_PROOFGEN_SCRATCH]; // proof-gen / fetch root-check working buffer
|
||||
#endif
|
||||
|
||||
// Server-side response descriptors are tiny. The active 1 KB block has its own buffer so proof generation
|
||||
// or a simultaneous fetch cannot overwrite DATA retained behind radio-queue backpressure.
|
||||
struct ServeJob {
|
||||
uint8_t mid[4] = {0};
|
||||
uint16_t block = 0;
|
||||
uint16_t pending_mask = 0;
|
||||
uint16_t emitted_mask = 0;
|
||||
};
|
||||
ServeJob _serve_jobs[OTA_SERVE_QUEUE];
|
||||
uint8_t _n_serve_jobs = 0;
|
||||
uint8_t _serve_block[OTA_MAX_BLOCK];
|
||||
uint16_t _serve_block_len = 0;
|
||||
bool _serve_block_loaded = false;
|
||||
|
||||
// fetch
|
||||
OtaStore* _fetch = nullptr;
|
||||
FetchState _fstate = IDLE;
|
||||
@@ -499,6 +531,7 @@ private:
|
||||
uint16_t mask = 0; // received FRAG_DATA-slice bitmap
|
||||
uint16_t need = 0; // full bitmap for this block
|
||||
bool awaiting_proof = false;
|
||||
uint32_t proof_request_at = 0; // proactive-proof grace deadline; 0 after fallback is sent
|
||||
uint8_t buf[OTA_MAX_BLOCK];
|
||||
};
|
||||
ReassemblySlot _reasm[OTA_FETCH_PIPELINE];
|
||||
|
||||
@@ -70,7 +70,7 @@ struct ManifestMsg {
|
||||
// (saves airtime and avoids the re-REQ colliding with a multi-fragment burst on half-duplex radios).
|
||||
struct ReqMsg { uint8_t manifest_id[4]; uint16_t block_idx; uint16_t want_mask; };
|
||||
|
||||
// ---- OTA_DATA: one self-describing fragment of a block's data (proof is fetched separately) ----
|
||||
// ---- OTA_DATA: one self-describing fragment of a block's data (proof stays in a separate packet) ----
|
||||
// body: manifest_id(4) block_idx(2) frag_off(2) data[]
|
||||
// `frag_off` is the byte offset of `data` within block `block_idx` (global position = block_idx*block_size
|
||||
// + frag_off), so a fragment is self-placing when received from the selected source.
|
||||
@@ -96,7 +96,7 @@ struct LeavesMsg {
|
||||
const uint8_t* bytes; uint16_t len;
|
||||
};
|
||||
|
||||
// ---- OTA_REQ_PROOF: request the merkle proof for one (reassembled) block (direct) ----
|
||||
// ---- OTA_REQ_PROOF: proof fallback for an older source or a lost proactive proof (direct) ----
|
||||
struct ReqProofMsg { uint8_t manifest_id[4]; uint16_t block_idx; };
|
||||
|
||||
// ---- OTA_PROOF: the merkle proof (ordered sibling digests) for one block (direct) ----
|
||||
|
||||
Reference in new issue
Block a user