Reduce shared RAM usage without lowering firmware capacities
Specialize repeater ACL session state, compact recent and flood records, stream Merkle proofs, and release cold OTA buffers safely. Preserve saved formats, stack sizes, table capacities, and OTA source limits. Account for lazy buffers when active and add compatibility, ARM alignment, allocation failure, and queued-response tests. Validated six representative board/role builds, 1736 native test cases, sanitizer fixtures, and strict documentation. Document the preexisting Wio-E5 Mini flash overflow separately.
This commit is contained in:
1 parent
76ddc06e56
commit
4b3e419eb0
30 files changed
+1783
-150
No files matched your search
+15
-1
@@ -16,6 +16,16 @@
|
||||
#define OUT_PATH_FORCE_FLOOD 0xFE
|
||||
#define OUT_PATH_UNKNOWN 0xFF
|
||||
|
||||
// The source-role pre-script sets this for the complete build, including
|
||||
// ClientACL.cpp. Never infer it from a local MyMesh header: that would give
|
||||
// different translation units incompatible ClientInfo layouts.
|
||||
#ifndef MESH_CLIENT_REPEATER_ONLY
|
||||
#define MESH_CLIENT_REPEATER_ONLY 0
|
||||
#endif
|
||||
#if MESH_CLIENT_REPEATER_ONLY != 0 && MESH_CLIENT_REPEATER_ONLY != 1
|
||||
#error "MESH_CLIENT_REPEATER_ONLY must be 0 or 1"
|
||||
#endif
|
||||
|
||||
struct ClientInfo {
|
||||
mesh::Identity id;
|
||||
uint8_t permissions;
|
||||
@@ -34,12 +44,15 @@ struct ClientInfo {
|
||||
union {
|
||||
struct {
|
||||
uint32_t sync_since; // sync messages SINCE this timestamp (by OUR clock)
|
||||
#if !MESH_CLIENT_REPEATER_ONLY
|
||||
uint32_t last_post_timestamp; // sender timestamp for room posts only (transient)
|
||||
uint32_t pending_ack;
|
||||
uint32_t push_post_timestamp;
|
||||
unsigned long ack_timeout;
|
||||
uint8_t push_failures;
|
||||
#endif
|
||||
} room;
|
||||
#if !MESH_CLIENT_REPEATER_ONLY
|
||||
struct {
|
||||
uint32_t expiry_timestamp; // epoch seconds
|
||||
uint32_t push_tag;
|
||||
@@ -48,6 +61,7 @@ struct ClientInfo {
|
||||
uint8_t min_deltas[14]; // LPP encoded
|
||||
uint8_t prev_telem[14]; // LPP encoded
|
||||
} sensor;
|
||||
#endif
|
||||
} extra;
|
||||
|
||||
bool isAdmin() const { return (permissions & PERM_ACL_ROLE_MASK) == PERM_ACL_ADMIN; }
|
||||
@@ -60,7 +74,7 @@ struct ClientInfo {
|
||||
#define MAX_CLIENTS 32
|
||||
#endif
|
||||
|
||||
static_assert(sizeof(void*) != 4 || sizeof(ClientInfo) <= 320,
|
||||
static_assert(sizeof(void*) != 4 || sizeof(ClientInfo) <= (MESH_CLIENT_REPEATER_ONLY ? 284 : 320),
|
||||
"Update the client-table runtime RAM budget in check_firmware_ram.py");
|
||||
|
||||
struct ClientLoginReplayClampResult {
|
||||
|
||||
@@ -49,6 +49,41 @@ enum ChannelScopeGate {
|
||||
CHANNEL_SCOPE_REQUIRED_REJECTED,
|
||||
};
|
||||
|
||||
// Rules can be reordered/compacted in RAM without changing their identity.
|
||||
// Compare configured fields explicitly: padding and live rate-window state
|
||||
// must never decide whether an unnumbered setter reuses an existing slot.
|
||||
template<typename Entry>
|
||||
bool sameRuleConfiguration(const Entry& left, const Entry& right) {
|
||||
return left.active == right.active
|
||||
&& left.payload_type == right.payload_type
|
||||
&& left.min_hops == right.min_hops
|
||||
&& left.max_hops == right.max_hops
|
||||
&& left.suspend_on_temp_radio == right.suspend_on_temp_radio
|
||||
&& memcmp(left.scope_name, right.scope_name, sizeof(left.scope_name)) == 0
|
||||
&& left.match_blacklisted_path == right.match_blacklisted_path
|
||||
&& left.scope_uses_slow_timing == right.scope_uses_slow_timing
|
||||
&& left.incoming_scope_kind == right.incoming_scope_kind
|
||||
&& memcmp(left.incoming_scope_name, right.incoming_scope_name,
|
||||
sizeof(left.incoming_scope_name)) == 0
|
||||
&& left.channel_key_len == right.channel_key_len
|
||||
&& left.channel_hash == right.channel_hash
|
||||
&& memcmp(left.channel_secret, right.channel_secret,
|
||||
sizeof(left.channel_secret)) == 0
|
||||
&& memcmp(left.channel_name, right.channel_name, sizeof(left.channel_name)) == 0
|
||||
&& left.path_hash_size == right.path_hash_size
|
||||
&& left.path_hops == right.path_hops
|
||||
&& memcmp(left.path, right.path, sizeof(left.path)) == 0
|
||||
&& memcmp(left.target_region_name, right.target_region_name,
|
||||
sizeof(left.target_region_name)) == 0
|
||||
&& left.drop_on_match == right.drop_on_match
|
||||
&& left.rate_limit_enabled == right.rate_limit_enabled
|
||||
&& left.rate_per_minute == right.rate_per_minute
|
||||
&& left.priority == right.priority
|
||||
&& left.stop_on_match == right.stop_on_match
|
||||
&& left.retry_on_match == right.retry_on_match
|
||||
&& left.transport_modes == right.transport_modes;
|
||||
}
|
||||
|
||||
inline bool channelKeyLengthSupported(uint8_t key_len) {
|
||||
return key_len == 0 || key_len == CHANNEL_HASH_ONLY_LEN
|
||||
|| key_len == CHANNEL_KEY_128_LEN
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <Mesh.h>
|
||||
#include <type_traits>
|
||||
#if ARDUINO
|
||||
#include <Arduino.h>
|
||||
#endif
|
||||
@@ -29,6 +30,17 @@
|
||||
#define ACK_VALID_BYTES ((MAX_PACKET_ACKS + 7) / 8)
|
||||
#define MAX_ROUTE_HASH_BYTES 3
|
||||
|
||||
inline int routeHashPrefixBytesCompare(const uint8_t* a, const uint8_t* b,
|
||||
uint8_t length) {
|
||||
const volatile uint8_t* left = a;
|
||||
const volatile uint8_t* right = b;
|
||||
for (uint8_t i = 0; i < length; ++i) {
|
||||
const uint8_t av = left[i], bv = right[i];
|
||||
if (av != bv) return (int)av - (int)bv;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline bool routeHashPrefixesOverlap(const uint8_t* a, uint8_t a_len,
|
||||
const uint8_t* b, uint8_t b_len) {
|
||||
if (a == NULL || b == NULL || a_len == 0 || b_len == 0
|
||||
@@ -36,18 +48,72 @@ inline bool routeHashPrefixesOverlap(const uint8_t* a, uint8_t a_len,
|
||||
return false;
|
||||
}
|
||||
uint8_t compare_len = a_len < b_len ? a_len : b_len;
|
||||
return memcmp(a, b, compare_len) == 0;
|
||||
return routeHashPrefixBytesCompare(a, b, compare_len) == 0;
|
||||
}
|
||||
|
||||
class SimpleMeshTables : public mesh::MeshTables {
|
||||
public:
|
||||
// Recent rows belong to the mesh loop, including CLI requests marshaled from
|
||||
// network tasks. They must not be read or changed concurrently by an ISR or
|
||||
// worker. Byte access avoids unaligned uint32_t loads in the compact rows;
|
||||
// volatile here constrains access width, not synchronization or atomicity.
|
||||
struct RecentRepeaterMillis {
|
||||
uint8_t bytes[4];
|
||||
|
||||
operator uint32_t() const {
|
||||
const volatile uint8_t* value = bytes;
|
||||
return (uint32_t)value[0]
|
||||
| ((uint32_t)value[1] << 8)
|
||||
| ((uint32_t)value[2] << 16)
|
||||
| ((uint32_t)value[3] << 24);
|
||||
}
|
||||
|
||||
RecentRepeaterMillis& operator=(uint32_t millis) {
|
||||
volatile uint8_t* value = bytes;
|
||||
value[0] = (uint8_t)millis;
|
||||
value[1] = (uint8_t)(millis >> 8);
|
||||
value[2] = (uint8_t)(millis >> 16);
|
||||
value[3] = (uint8_t)(millis >> 24);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
struct RecentRepeaterInfo {
|
||||
// Identity and link quality for a next-hop path prefix.
|
||||
uint8_t prefix[MAX_ROUTE_HASH_BYTES];
|
||||
uint8_t prefix_len;
|
||||
int8_t snr_x4;
|
||||
uint32_t last_heard_millis;
|
||||
volatile uint8_t prefix_len;
|
||||
volatile int8_t snr_x4;
|
||||
RecentRepeaterMillis last_heard_millis;
|
||||
};
|
||||
static_assert(sizeof(RecentRepeaterMillis) == 4 && alignof(RecentRepeaterMillis) == 1,
|
||||
"Recent timestamps must retain all 32 bits without alignment padding");
|
||||
static_assert(sizeof(RecentRepeaterInfo) == 9 && alignof(RecentRepeaterInfo) == 1,
|
||||
"Recent rows must not add padding to the externally allocated table");
|
||||
static_assert(std::is_trivially_copyable<RecentRepeaterInfo>::value,
|
||||
"Recent rows must support memset, compaction, and cursor copies");
|
||||
|
||||
// Use these helpers instead of implicit row copies or memcpy/memset. M4
|
||||
// compilers may otherwise widen byte-aligned aggregate operations, which
|
||||
// faults when unaligned accesses trap. Keep every compact-row access a byte,
|
||||
// even when LTO can see the length or a concrete source/destination address.
|
||||
static void copyRecentRepeaterInfo(RecentRepeaterInfo& destination,
|
||||
const RecentRepeaterInfo& source) {
|
||||
volatile uint8_t* out = reinterpret_cast<volatile uint8_t*>(&destination);
|
||||
const volatile uint8_t* in = reinterpret_cast<const volatile uint8_t*>(&source);
|
||||
for (size_t i = 0; i < sizeof(RecentRepeaterInfo); ++i) out[i] = in[i];
|
||||
}
|
||||
|
||||
static void clearRecentRepeaterInfo(RecentRepeaterInfo& row) {
|
||||
volatile uint8_t* out = reinterpret_cast<volatile uint8_t*>(&row);
|
||||
for (size_t i = 0; i < sizeof(RecentRepeaterInfo); ++i) out[i] = 0;
|
||||
}
|
||||
|
||||
static void copyRecentRepeaterPrefix(uint8_t* destination,
|
||||
const RecentRepeaterInfo& source) {
|
||||
volatile uint8_t* out = destination;
|
||||
const volatile uint8_t* in = source.prefix;
|
||||
for (size_t i = 0; i < MAX_ROUTE_HASH_BYTES; ++i) out[i] = in[i];
|
||||
}
|
||||
|
||||
private:
|
||||
uint8_t _hashes[MAX_PACKET_HASHES*MAX_HASH_SIZE];
|
||||
@@ -182,7 +248,7 @@ private:
|
||||
if (a.snr_x4 != b.snr_x4) {
|
||||
return a.snr_x4 > b.snr_x4; // Highest SNR first within each prefix length.
|
||||
}
|
||||
int cmp = memcmp(a.prefix, b.prefix, a.prefix_len);
|
||||
int cmp = routeHashPrefixBytesCompare(a.prefix, b.prefix, a.prefix_len);
|
||||
if (cmp != 0) {
|
||||
return cmp < 0;
|
||||
}
|
||||
@@ -261,9 +327,7 @@ public:
|
||||
memset(_ack_valid, 0, sizeof(_ack_valid));
|
||||
_next_ack_idx = 0;
|
||||
_direct_dups = _flood_dups = 0;
|
||||
if (_max_recent_repeaters > 0) {
|
||||
memset(_recent_repeaters, 0, _max_recent_repeaters * sizeof(RecentRepeaterInfo));
|
||||
}
|
||||
clearRecentRepeaters();
|
||||
}
|
||||
|
||||
#ifdef ESP32
|
||||
@@ -398,7 +462,8 @@ public:
|
||||
// path prefix does not collapse 2/3-byte repeaters sharing its first byte.
|
||||
for (int i = 0; i < _recent_repeater_count; i++) {
|
||||
RecentRepeaterInfo& existing = _recent_repeaters[i];
|
||||
if (existing.prefix_len != prefix_len || memcmp(existing.prefix, prefix, prefix_len) != 0) {
|
||||
if (existing.prefix_len != prefix_len
|
||||
|| routeHashPrefixBytesCompare(existing.prefix, prefix, prefix_len) != 0) {
|
||||
#if ARDUINO
|
||||
uint32_t age = (uint32_t)(now - existing.last_heard_millis);
|
||||
if (!have_oldest || age > oldest_age) {
|
||||
@@ -428,8 +493,11 @@ public:
|
||||
}
|
||||
|
||||
RecentRepeaterInfo& slot = _recent_repeaters[slot_idx];
|
||||
memset(slot.prefix, 0, sizeof(slot.prefix));
|
||||
memcpy(slot.prefix, prefix, prefix_len);
|
||||
volatile uint8_t* out_prefix = slot.prefix;
|
||||
const volatile uint8_t* in_prefix = prefix;
|
||||
for (uint8_t i = 0; i < MAX_ROUTE_HASH_BYTES; ++i) {
|
||||
out_prefix[i] = i < prefix_len ? in_prefix[i] : 0;
|
||||
}
|
||||
slot.prefix_len = prefix_len;
|
||||
slot.snr_x4 = snr_x4;
|
||||
#if ARDUINO
|
||||
@@ -467,9 +535,10 @@ public:
|
||||
}
|
||||
// Advance a cooperative live view in one bounded table scan. Unlike a
|
||||
// rank lookup, this never replays every earlier rank as the list grows.
|
||||
// The caller copies the last accepted row: intervening RX may change the
|
||||
// live ordering, but cannot invalidate that saved sort key or force a
|
||||
// snapshot allocation. The existing paginated/rank APIs are unchanged.
|
||||
// The caller uses copyRecentRepeaterInfo for the last accepted row:
|
||||
// intervening RX may change the live ordering, but cannot invalidate that
|
||||
// saved sort key or force a snapshot allocation. The existing paginated/rank
|
||||
// APIs are unchanged.
|
||||
const RecentRepeaterInfo* getNextRecentRepeaterBySortKey(
|
||||
const RecentRepeaterInfo* after, int after_index, int& result_index) const {
|
||||
const RecentRepeaterInfo* best = NULL;
|
||||
@@ -523,7 +592,8 @@ public:
|
||||
const RecentRepeaterInfo* best = NULL;
|
||||
for (int i = 0; i < _recent_repeater_count; i++) {
|
||||
const RecentRepeaterInfo* info = &_recent_repeaters[i];
|
||||
if (info->prefix_len == hash_len && memcmp(info->prefix, hash, hash_len) == 0) {
|
||||
if (info->prefix_len == hash_len
|
||||
&& routeHashPrefixBytesCompare(info->prefix, hash, hash_len) == 0) {
|
||||
return info;
|
||||
}
|
||||
if (routeHashPrefixesOverlap(info->prefix, info->prefix_len, hash, hash_len)) {
|
||||
@@ -536,9 +606,7 @@ public:
|
||||
return best;
|
||||
}
|
||||
void clearRecentRepeaters() {
|
||||
if (_max_recent_repeaters > 0) {
|
||||
memset(_recent_repeaters, 0, _max_recent_repeaters * sizeof(RecentRepeaterInfo));
|
||||
}
|
||||
for (int i = 0; i < _max_recent_repeaters; ++i) clearRecentRepeaterInfo(_recent_repeaters[i]);
|
||||
_recent_repeater_count = 0;
|
||||
}
|
||||
int expireRecentRepeaters(uint32_t now_millis, uint32_t max_age_millis) {
|
||||
@@ -552,9 +620,9 @@ public:
|
||||
if ((uint32_t)(now_millis - info.last_heard_millis) > max_age_millis) {
|
||||
_recent_repeater_count--;
|
||||
if (i != _recent_repeater_count) {
|
||||
info = _recent_repeaters[_recent_repeater_count];
|
||||
copyRecentRepeaterInfo(info, _recent_repeaters[_recent_repeater_count]);
|
||||
}
|
||||
memset(&_recent_repeaters[_recent_repeater_count], 0, sizeof(RecentRepeaterInfo));
|
||||
clearRecentRepeaterInfo(_recent_repeaters[_recent_repeater_count]);
|
||||
expired++;
|
||||
} else {
|
||||
i++;
|
||||
|
||||
@@ -105,26 +105,29 @@ bool merkle_verify_from_leaf(const uint8_t leaf[4], uint32_t index,
|
||||
|
||||
uint8_t merkle_gen_proof(const uint8_t* leaves, uint32_t count, uint32_t index,
|
||||
uint8_t* scratch, uint8_t* out_siblings) {
|
||||
if (count == 0 || index >= count) return 0;
|
||||
memcpy(scratch, leaves, (size_t)count * 4);
|
||||
(void)scratch;
|
||||
if (!leaves || !out_siblings || count == 0 || index >= count) return 0;
|
||||
uint32_t n = count, idx = index;
|
||||
uint64_t width = 1;
|
||||
uint8_t p = 0;
|
||||
MerkleAccumulator accumulator;
|
||||
while (n > 1) {
|
||||
bool is_last_odd = (n & 1u) && (idx == n - 1);
|
||||
if (!is_last_odd) {
|
||||
uint32_t s = (idx & 1u) ? idx - 1 : idx + 1;
|
||||
memcpy(out_siblings + (size_t)p * 4, scratch + (size_t)s * 4, 4);
|
||||
const uint64_t first = (uint64_t)s * width;
|
||||
uint64_t end = first + width;
|
||||
if (end > count) end = count;
|
||||
accumulator.reset();
|
||||
for (uint64_t leaf = first; leaf < end; ++leaf) {
|
||||
if (!accumulator.add(leaves + (size_t)leaf * 4)) return 0;
|
||||
}
|
||||
if (!accumulator.finish(out_siblings + (size_t)p * 4)) return 0;
|
||||
p++;
|
||||
}
|
||||
// reduce one level in place (parent m written from children 2m,2m+1; m <= i so it's safe)
|
||||
uint32_t m = 0;
|
||||
for (uint32_t i = 0; i < n; i += 2) {
|
||||
if (i + 1 < n) merkle_combine(scratch + (size_t)m * 4, scratch + (size_t)i * 4, scratch + (size_t)(i + 1) * 4);
|
||||
else memmove(scratch + (size_t)m * 4, scratch + (size_t)i * 4, 4);
|
||||
m++;
|
||||
}
|
||||
idx >>= 1;
|
||||
n = (n + 1) >> 1;
|
||||
n = (n >> 1) + (n & 1u);
|
||||
width <<= 1;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
@@ -55,7 +55,9 @@ bool merkle_verify_from_leaf(const uint8_t leaf[4], uint32_t index,
|
||||
const uint8_t root[4], uint32_t count);
|
||||
|
||||
// Generate the proof (ordered sibling digests) for block `index`, for a server holding leaves[].
|
||||
// `scratch` must be >= count*4 bytes (working buffer); `out_siblings` >= 32*4 bytes.
|
||||
// Sibling subtrees are reduced with O(log count) storage. `scratch` is retained
|
||||
// for source compatibility but is unused and may be nullptr; `out_siblings`
|
||||
// must hold 32*4 bytes. Input leaves are never modified.
|
||||
// Returns the number of 4-byte siblings written. Output matches the wire form merkle_verify expects.
|
||||
uint8_t merkle_gen_proof(const uint8_t* leaves, uint32_t count, uint32_t index,
|
||||
uint8_t* scratch, uint8_t* out_siblings);
|
||||
|
||||
@@ -160,8 +160,8 @@ struct OtaContext {
|
||||
bool self_serve_supported = false;
|
||||
// flash-backed self-serve: cached merkle leaves (heap, freed on re-serve) + assembled manifest of our
|
||||
// own running firmware. The payload is read from flash per block; only the metadata is held in RAM.
|
||||
// serve_self_proof is the proof-gen working buffer (>= block_count*4) - sized to OUR image's block
|
||||
// count (the manager's fixed 4 KiB scratch covers <=1024 blocks, about 2 MiB at the new default).
|
||||
// serve_self_proof is the encoded-DATA output buffer on encoder-enabled
|
||||
// builds. Streaming Merkle proofs need no full copy of the leaf table.
|
||||
uint8_t* serve_self_leaves = nullptr;
|
||||
uint8_t* serve_self_proof = nullptr;
|
||||
|
||||
|
||||
@@ -13,12 +13,8 @@ namespace ota {
|
||||
OtaManager::~OtaManager() {
|
||||
free(_catalog_heap);
|
||||
free(_leaves_buf);
|
||||
#if defined(ESP32_PLATFORM)
|
||||
// A shared Companion workspace is destroyed between mOTA sessions. Return
|
||||
// the lazy source/proof buffers before its queue slots are reused.
|
||||
free(_src_leaves);
|
||||
free(_scratch);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool OtaManager::set_speed(float speed) {
|
||||
@@ -61,24 +57,56 @@ bool OtaManager::expandCatalog() {
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t* OtaManager::ensureSourceLeaves() {
|
||||
#if defined(ESP32_PLATFORM)
|
||||
if (!_src_leaves) {
|
||||
_src_leaves = static_cast<uint8_t*>(malloc(OTA_PROOFGEN_SCRATCH));
|
||||
uint8_t* OtaManager::ensureSourceLeaves(uint32_t bytes) {
|
||||
if (!bytes || bytes > OTA_PROOFGEN_SCRATCH) return nullptr;
|
||||
if (_src_leaves_capacity < bytes) {
|
||||
// loadSource revoked the cached view first. Free before growing, so even
|
||||
// an allocation failure cannot overlap two images' maximum leaf caches.
|
||||
free(_src_leaves);
|
||||
_src_leaves = nullptr;
|
||||
_src_leaves_capacity = 0;
|
||||
_src_leaves = static_cast<uint8_t*>(malloc(bytes));
|
||||
if (_src_leaves) _src_leaves_capacity = bytes;
|
||||
}
|
||||
#endif
|
||||
return _src_leaves;
|
||||
}
|
||||
|
||||
uint8_t* OtaManager::ensureScratch() {
|
||||
#if defined(ESP32_PLATFORM)
|
||||
if (!_scratch) {
|
||||
_scratch = static_cast<uint8_t*>(malloc(OTA_PROOFGEN_SCRATCH));
|
||||
_scratch = static_cast<uint8_t*>(malloc(OTA_MAX_BLOCK));
|
||||
}
|
||||
#endif
|
||||
return _scratch;
|
||||
}
|
||||
|
||||
void OtaManager::releaseColdBuffers() {
|
||||
// Both queues pin their selected source and any encoded DATA behind radio
|
||||
// backpressure. No cached view or wire pointer may outlive its allocation.
|
||||
if (_n_serve_jobs || _n_manifest_jobs) return;
|
||||
_srcv.valid = false;
|
||||
_srcv.m.leaves = nullptr;
|
||||
_srcv.scratch = nullptr;
|
||||
_srcv.scratch_sz = 0;
|
||||
if (_view0.scratch == _scratch) {
|
||||
_view0.scratch = nullptr;
|
||||
_view0.scratch_sz = 0;
|
||||
}
|
||||
_serve_block_loaded = false;
|
||||
_serve_wire_block = nullptr;
|
||||
_serve_wire_len = 0;
|
||||
free(_src_leaves);
|
||||
_src_leaves = nullptr;
|
||||
_src_leaves_capacity = 0;
|
||||
free(_scratch);
|
||||
_scratch = nullptr;
|
||||
}
|
||||
|
||||
void OtaManager::releaseIdleColdBuffers() {
|
||||
if ((_src_leaves || _scratch) &&
|
||||
(uint32_t)(_now_ms - _source_buffer_used_at) >= OTA_SOURCE_BUFFER_IDLE_MS) {
|
||||
releaseColdBuffers();
|
||||
}
|
||||
}
|
||||
|
||||
void OtaManager::begin(uint32_t my_target_id, OtaSend send, void* ctx) {
|
||||
_target = my_target_id; _send = send; _ctx = ctx;
|
||||
_migration_target = 0;
|
||||
@@ -94,6 +122,7 @@ void OtaManager::begin(uint32_t my_target_id, OtaSend send, void* ctx) {
|
||||
memset(_src_advertised, 0, sizeof(_src_advertised));
|
||||
_n_src = 0; _n_cat = 0;
|
||||
clearPendingEgress();
|
||||
releaseColdBuffers();
|
||||
_adaptive_packet_speed = 1.0f;
|
||||
_pace_clean_blocks = 0;
|
||||
_pace_has_mid = false;
|
||||
@@ -112,17 +141,16 @@ bool OtaManager::serve(const uint8_t* mota, uint32_t len) {
|
||||
if (!mota || !mota_parse(mota, len, parsed)) return false;
|
||||
if (parsed.block_size() == 0 || parsed.block_size() > OTA_MAX_BLOCK ||
|
||||
(uint64_t)parsed.block_count * 4 > OTA_PROOFGEN_SCRATCH) return false;
|
||||
uint8_t* scratch = ensureScratch();
|
||||
if (!scratch) return false;
|
||||
// Parsing can fail after writing fields and pointers. Publish the replacement
|
||||
// only after all checks pass, preserving the old image and its queued replies.
|
||||
_view0.m = parsed;
|
||||
_view0.mfl = (uint16_t)(_view0.m.leaves - _view0.m.manifest_start); // contiguous container
|
||||
_view0.read = nullptr; _view0.read_ctx = nullptr; // payload is contiguous _view0.m.payload
|
||||
_view0.read_deflated = nullptr; _view0.deflate_ctx = nullptr;
|
||||
_view0.scratch = scratch; _view0.scratch_sz = OTA_PROOFGEN_SCRATCH; // <=1024 leaves/blocks
|
||||
_view0.scratch = nullptr; _view0.scratch_sz = 0; // allocate only if compressed DATA is requested
|
||||
_view0.valid = true;
|
||||
clearPendingEgress();
|
||||
releaseColdBuffers();
|
||||
if (_n_src_obj) refresh_sources(); else registerSelfEntry();
|
||||
return true;
|
||||
}
|
||||
@@ -131,7 +159,7 @@ bool OtaManager::serve_self(const uint8_t* manifest, uint16_t mfl, const uint8_t
|
||||
uint32_t block_count, uint8_t* proof_scratch, uint32_t proof_scratch_sz,
|
||||
ServeReadFn read, void* ctx) {
|
||||
if (!manifest || mfl != MOTA_MFL || !leaves || !proof_scratch || !read) return false;
|
||||
if (proof_scratch_sz < (uint64_t)block_count * 4) return false; // proof-gen needs count*4 working bytes
|
||||
if (proof_scratch_sz == 0) return false;
|
||||
MotaManifest parsed;
|
||||
if (!mota_parse_manifest(manifest, mfl, parsed)) return false; // fixed fields: root, image_hash, sizes
|
||||
if (parsed.block_size() == 0 || parsed.block_size() > OTA_MAX_BLOCK ||
|
||||
@@ -143,9 +171,10 @@ bool OtaManager::serve_self(const uint8_t* manifest, uint16_t mfl, const uint8_t
|
||||
_view0.m.block_count = block_count;
|
||||
_view0.mfl = mfl; _view0.read = read; _view0.read_ctx = ctx;
|
||||
_view0.read_deflated = nullptr; _view0.deflate_ctx = nullptr;
|
||||
_view0.scratch = proof_scratch; _view0.scratch_sz = proof_scratch_sz; // sized for our (large) image
|
||||
_view0.scratch = proof_scratch; _view0.scratch_sz = proof_scratch_sz; // optional compression output
|
||||
_view0.valid = true;
|
||||
clearPendingEgress();
|
||||
releaseColdBuffers();
|
||||
if (_n_src_obj) refresh_sources(); else registerSelfEntry();
|
||||
return true;
|
||||
}
|
||||
@@ -203,6 +232,7 @@ bool OtaManager::remove_source(MotaSource* src) {
|
||||
|
||||
void OtaManager::refresh_sources() {
|
||||
clearPendingEgress();
|
||||
releaseColdBuffers();
|
||||
_n_serve = 0;
|
||||
if (_view0.valid) registerSelfEntry();
|
||||
for (uint8_t s = 0; s < _n_src_obj; s++) {
|
||||
@@ -258,6 +288,7 @@ bool OtaManager::sourceStats(const MotaSource* src, uint16_t& offered, uint16_t&
|
||||
|
||||
void OtaManager::clear_sources() {
|
||||
clearPendingEgress();
|
||||
releaseColdBuffers();
|
||||
_n_src_obj = 0; _srcv.valid = false;
|
||||
memset(_src_list, 0, sizeof(_src_list));
|
||||
memset(_src_offered, 0, sizeof(_src_offered));
|
||||
@@ -269,6 +300,7 @@ void OtaManager::clear_sources() {
|
||||
void OtaManager::clear_primary() {
|
||||
clearPendingEgress();
|
||||
_view0.valid = false;
|
||||
releaseColdBuffers();
|
||||
if (_n_src_obj) refresh_sources();
|
||||
else _n_serve = 0;
|
||||
}
|
||||
@@ -281,7 +313,10 @@ int OtaManager::serveEntryIndex(const uint8_t* mid) const {
|
||||
|
||||
OtaManager::ServeView* OtaManager::resolve(const uint8_t* mid) {
|
||||
if (_view0.valid && memcmp(mid, _view0.m.merkle_root, 4) == 0) return &_view0;
|
||||
if (_srcv.valid && memcmp(mid, _srcv_mid, 4) == 0) return &_srcv;
|
||||
if (_srcv.valid && memcmp(mid, _srcv_mid, 4) == 0) {
|
||||
_source_buffer_used_at = _now_ms;
|
||||
return &_srcv;
|
||||
}
|
||||
int i = serveEntryIndex(mid);
|
||||
if (i < 0) return nullptr;
|
||||
if (_serve[i].is_self) return _view0.valid ? &_view0 : nullptr;
|
||||
@@ -299,12 +334,12 @@ bool OtaManager::loadSource(const ServeEntry& e) {
|
||||
uint16_t mfl = (uint16_t)(d.leaves_off - 8);
|
||||
if (mfl == 0 || mfl > sizeof(_src_manifest)) return false;
|
||||
if (d.block_count == 0 || (uint64_t)d.block_count * 4 > OTA_PROOFGEN_SCRATCH) return false;
|
||||
uint8_t* src_leaves = ensureSourceLeaves();
|
||||
uint8_t* scratch = ensureScratch();
|
||||
if (!src_leaves || !scratch) return false;
|
||||
// Both images share these buffers. Even a failed read can overwrite part of
|
||||
// the old manifest/leaves, so its cached MID must stop resolving immediately.
|
||||
_srcv.valid = false;
|
||||
uint8_t* src_leaves = ensureSourceLeaves(d.block_count * 4u);
|
||||
if (!src_leaves) return false;
|
||||
_source_buffer_used_at = _now_ms;
|
||||
bool ok = e.src->read(e.src_idx, 8, _src_manifest, mfl);
|
||||
if (!ok || !mota_parse_manifest(_src_manifest, mfl, _srcv.m)) return false;
|
||||
if (_srcv.m.block_size() == 0 || _srcv.m.block_size() > OTA_MAX_BLOCK) return false;
|
||||
@@ -322,7 +357,7 @@ bool OtaManager::loadSource(const ServeEntry& e) {
|
||||
_srcv.read_deflated = (d.source_caps & MOTA_SOURCE_CAP_DEFLATE_BLOCK)
|
||||
? srcDeflateReadTramp : nullptr;
|
||||
_srcv.deflate_ctx = _srcv.read_deflated ? &_srcv_rdctx : nullptr;
|
||||
_srcv.scratch = scratch; _srcv.scratch_sz = OTA_PROOFGEN_SCRATCH;
|
||||
_srcv.scratch = nullptr; _srcv.scratch_sz = 0;
|
||||
memcpy(_srcv_mid, d.mid, 4);
|
||||
_srcv.valid = true;
|
||||
return true;
|
||||
@@ -562,6 +597,10 @@ void OtaManager::clearPendingEgress() {
|
||||
_serve_wire_len = 0;
|
||||
_serve_wire_deflated = false;
|
||||
memset(_serve_wire_id, 0, sizeof(_serve_wire_id));
|
||||
// Explicit cancellation also runs when the temporary OTA radio ends, when
|
||||
// loop() is no longer called. Keep source descriptors advertised, but drop
|
||||
// their reloadable working cache rather than pinning heap until next use.
|
||||
releaseColdBuffers();
|
||||
}
|
||||
|
||||
void OtaManager::popManifestJob() {
|
||||
@@ -663,8 +702,13 @@ bool OtaManager::loadActiveServeBlock() {
|
||||
_serve_wire_deflated = false;
|
||||
|
||||
// A host-backed MotaSource can provide a pre-compressed block without putting an encoder in firmware.
|
||||
// Other sources may opt into a caller-supplied encoder. Both write into the proof scratch, which is safe
|
||||
// until every DATA fragment has left; proof generation reuses it only after that point.
|
||||
// Other sources may opt into a caller-supplied encoder. Encoded DATA remains
|
||||
// pinned until every fragment has left. Streaming proofs need no output buffer.
|
||||
if (job.wire_v2 && job.allow_deflate && (v->read_deflated || _deflate_encode) && !v->scratch) {
|
||||
v->scratch = ensureScratch();
|
||||
v->scratch_sz = v->scratch ? OTA_MAX_BLOCK : 0;
|
||||
}
|
||||
_source_buffer_used_at = _now_ms;
|
||||
if (job.wire_v2 && job.allow_deflate && v->scratch && v->scratch_sz >= blen) {
|
||||
uint16_t encoded_len = 0;
|
||||
bool encoded = false;
|
||||
@@ -765,7 +809,6 @@ bool OtaManager::handleReqProof(const uint8_t* m, uint16_t n) {
|
||||
if (!decode_req_proof(m, n, rp)) return false;
|
||||
ServeView* v = resolve(rp.manifest_id);
|
||||
if (!v || rp.block_idx >= v->m.block_count) return false;
|
||||
if ((uint64_t)v->m.block_count * 4 > v->scratch_sz) return false;
|
||||
return queueServeJob(v->m.merkle_root, rp.block_idx, 0); // proof-only, or merge with queued DATA
|
||||
}
|
||||
|
||||
@@ -796,8 +839,7 @@ void OtaManager::serviceEgress(OtaReplyRouteValid route_valid, void* route_ctx)
|
||||
return;
|
||||
}
|
||||
ServeView* v = resolve(job.mid);
|
||||
if (!v || job.block >= v->m.block_count
|
||||
|| (uint64_t)v->m.block_count * 4 > v->scratch_sz) {
|
||||
if (!v || job.block >= v->m.block_count) {
|
||||
popServeJob();
|
||||
return;
|
||||
}
|
||||
@@ -2179,6 +2221,7 @@ uint32_t OtaManager::pickMissingBlock() {
|
||||
}
|
||||
|
||||
void OtaManager::loop() {
|
||||
releaseIdleColdBuffers();
|
||||
// Each source owns its own pending/retry state, so a burst of advertisements cannot overwrite another
|
||||
// seeder's query. Initial requests ask for all; recovery asks only for missing HAVE fragments.
|
||||
for (uint8_t i = 0; i < _n_src; i++) {
|
||||
|
||||
@@ -63,14 +63,20 @@ typedef bool (*ServeDeflateReadFn)(void* ctx, uint16_t block, uint8_t* dst,
|
||||
|
||||
#ifndef OTA_PROOFGEN_SCRATCH
|
||||
#if defined(ESP32_PLATFORM)
|
||||
#define OTA_PROOFGEN_SCRATCH 16384 // heap-backed; can seed <=4096 blocks (8 MiB at 2 KiB/block)
|
||||
#define OTA_PROOFGEN_SCRATCH 16384 // maximum source leaves; <=4096 blocks (8 MiB at 2 KiB/block)
|
||||
#elif defined(OTA_SD_STORE)
|
||||
#define OTA_PROOFGEN_SCRATCH 8192 // SD archive can seed <=2048 blocks (about 4 MiB at 2 KiB/block)
|
||||
#else
|
||||
#define OTA_PROOFGEN_SCRATCH 4096 // server proof-gen working buffer (supports up to 1024 blocks)
|
||||
#define OTA_PROOFGEN_SCRATCH 4096 // maximum source leaves (supports up to 1024 blocks)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef OTA_SOURCE_BUFFER_IDLE_MS
|
||||
#define OTA_SOURCE_BUFFER_IDLE_MS 30000u // retain an unpinned source cache briefly between requests
|
||||
#endif
|
||||
static_assert(OTA_SOURCE_BUFFER_IDLE_MS > 0u && OTA_SOURCE_BUFFER_IDLE_MS < 0x80000000u,
|
||||
"OTA source-buffer idle interval must support wrap-safe elapsed time");
|
||||
|
||||
#ifndef OTA_MAX_BLOCK
|
||||
#define OTA_MAX_BLOCK 2048 // largest logical block (Merkle leaf / independent DEFLATE unit)
|
||||
#endif
|
||||
@@ -291,7 +297,7 @@ public:
|
||||
void* read_ctx = nullptr;
|
||||
ServeDeflateReadFn read_deflated = nullptr; // optional host/source-generated raw-DEFLATE block
|
||||
void* deflate_ctx = nullptr;
|
||||
uint8_t* scratch = nullptr; // proof-gen working buffer (>= block_count*4)
|
||||
uint8_t* scratch = nullptr; // optional encoded DATA output, not Merkle proof storage
|
||||
uint32_t scratch_sz = 0;
|
||||
};
|
||||
// A lightweight catalog entry: what we advertise per mota + how to load its ServeView on demand.
|
||||
@@ -686,15 +692,13 @@ private:
|
||||
uint16_t _src_advertised[OTA_MAX_SOURCE_OBJ] = {0};
|
||||
uint8_t _n_src_obj = 0;
|
||||
uint8_t _src_manifest[OTA_SRC_MANIFEST_MAX]; // manifest-minus-leaves of the loaded source mota
|
||||
#if defined(ESP32_PLATFORM)
|
||||
// Folder-source leaves and proof scratch are cold-path working storage. Keep
|
||||
// them off classic ESP32 .bss and allocate them on first use.
|
||||
// Cold source leaves are sized to the selected image, within the unchanged
|
||||
// capacity limit. Encoded DATA needs at most one logical block. These buffers
|
||||
// remain pinned until queued egress drains, then return to the heap at idle.
|
||||
uint8_t* _src_leaves = nullptr;
|
||||
uint32_t _src_leaves_capacity = 0;
|
||||
uint8_t* _scratch = nullptr;
|
||||
#else
|
||||
uint8_t _src_leaves[OTA_PROOFGEN_SCRATCH]; // leaves[] of the loaded source mota (<=1024 blocks)
|
||||
uint8_t _scratch[OTA_PROOFGEN_SCRATCH]; // proof-gen / fetch root-check working buffer
|
||||
#endif
|
||||
uint32_t _source_buffer_used_at = 0;
|
||||
|
||||
// Server-side response descriptors are tiny. The active logical block has its own buffer so proof generation
|
||||
// or a simultaneous fetch cannot overwrite DATA retained behind radio-queue backpressure.
|
||||
@@ -827,8 +831,10 @@ private:
|
||||
bool _diffing = false; // leaves are in; diffing the seed a batch per tick
|
||||
uint32_t _diff_idx = 0; // next block index to diff against the seed
|
||||
|
||||
uint8_t* ensureSourceLeaves();
|
||||
uint8_t* ensureSourceLeaves(uint32_t bytes);
|
||||
uint8_t* ensureScratch();
|
||||
void releaseColdBuffers();
|
||||
void releaseIdleColdBuffers();
|
||||
|
||||
// discovery: heard sources (beacon senders) + the catalog assembled from their OTA_HAVE replies
|
||||
struct Source {
|
||||
|
||||
@@ -228,11 +228,10 @@ bool ota_serve_self(OtaContext& c, uint32_t fw_version) {
|
||||
c.serve_self_leaves = (uint8_t*)malloc((size_t)bc * 4);
|
||||
size_t proof_size = (size_t)bc * 4;
|
||||
#if MESHCORE_OTA_DEVICE_DEFLATE
|
||||
// Proof generation and transport encoding use this buffer at different
|
||||
// times. Small nRF52 images can have <2 KiB of leaves: keep enough room for
|
||||
// a whole encoded block without allocating a second input/output buffer.
|
||||
// Receiver-only/Companion diagnostic raw exports retain the old allocation.
|
||||
if (proof_size < BS) proof_size = BS;
|
||||
// Streaming proofs no longer need a copy of the leaf table. This buffer
|
||||
// holds only one encoded DATA block, regardless of the firmware's size.
|
||||
// Receiver-only/Companion diagnostic raw exports retain their allocation.
|
||||
proof_size = BS;
|
||||
#endif
|
||||
c.serve_self_proof = (uint8_t*)malloc(proof_size);
|
||||
if (!c.serve_self_leaves || !c.serve_self_proof) {
|
||||
|
||||
Reference in new issue
Block a user