Reduce contact RAM across boards and compact Full firmware

This commit is contained in:
mikecarper committed 2026-10-19 01:55:25 -07:00
1 parent 8d4264e272
commit 036661e7e4
19 files changed
+2355 -2030

No files matched your search

+4 -4
View File
@@ -38,8 +38,8 @@ bool BaseChatMesh::initializeContactStorage() {
ContactInfo* expanded_contacts = static_cast<ContactInfo*>(
heap_caps_calloc(requested_capacity, sizeof(ContactInfo),
MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
int* expanded_sort_array = static_cast<int*>(
heap_caps_calloc(requested_capacity, sizeof(int),
ContactIndex* expanded_sort_array = static_cast<ContactIndex*>(
heap_caps_calloc(requested_capacity, sizeof(ContactIndex),
MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
if (expanded_contacts == NULL || expanded_sort_array == NULL) {
if (expanded_contacts != NULL) heap_caps_free(expanded_contacts);
@@ -1056,8 +1056,8 @@ void BaseChatMesh::resetPathTo(ContactInfo& recipient) {
static ContactInfo* table; // pass via global :-(
static int cmp_contact_list_order(const void *a, const void *b) {
int a_idx = *((int *)a);
int b_idx = *((int *)b);
int a_idx = *static_cast<const ContactIndex*>(a);
int b_idx = *static_cast<const ContactIndex*>(b);
return mesh::compareContactListOrder(
table[a_idx].flags, table[a_idx].last_advert_timestamp,
table[b_idx].flags, table[b_idx].last_advert_timestamp);
+12 -4
View File
@@ -47,6 +47,14 @@ public:
#define MAX_ANON_CONTACTS 8
// These arrays hold contact-table positions, never negative sentinels. Keep
// wider indices for configurations whose complete table exceeds 16 bits.
#if MAX_CONTACTS + MAX_ANON_CONTACTS <= 65535
using ContactIndex = uint16_t;
#else
using ContactIndex = int;
#endif
#ifndef MAX_CONNECTIONS
#define MAX_CONNECTIONS 16
#endif
@@ -77,16 +85,16 @@ class BaseChatMesh : public mesh::Mesh {
};
ContactInfo* contacts;
ContactInfo contacts_fallback[CONTACT_PSRAM_FALLBACK_TOTAL_SLOTS];
int* sort_array;
int sort_array_fallback[CONTACT_PSRAM_FALLBACK_TOTAL_SLOTS];
ContactIndex* sort_array;
ContactIndex sort_array_fallback[CONTACT_PSRAM_FALLBACK_TOTAL_SLOTS];
int contact_capacity;
#else
ContactInfo contacts[MAX_CONTACTS+MAX_ANON_CONTACTS];
int sort_array[MAX_CONTACTS+MAX_ANON_CONTACTS];
ContactIndex sort_array[MAX_CONTACTS+MAX_ANON_CONTACTS];
#endif
int num_contacts = 0;
uint32_t contact_table_revision;
int matching_peer_indexes[MAX_SEARCH_RESULTS];
ContactIndex matching_peer_indexes[MAX_SEARCH_RESULTS];
unsigned long txt_send_timeout;
#ifdef MAX_GROUP_CHANNELS
ChannelDetails channels[MAX_GROUP_CHANNELS];
+8 -14
View File
@@ -5,6 +5,7 @@
#include <Mesh.h>
#include <helpers/FilePresence.h>
#include <helpers/MeshtextChat.h>
#include <helpers/PersistentStoreFormat.h>
#include <helpers/UTF8Helpers.h>
#include <Arduino.h>
#include <ctype.h>
@@ -27,14 +28,7 @@ const char* const IdPath = "/beacon_ids";
const char* const IdBackup = "/beacon_ids.bak";
const char* const IdTemp = "/beacon_ids.tmp";
uint32_t crcUpdate(uint32_t crc, const uint8_t* bytes, size_t size) {
while (size--) {
crc ^= *bytes++;
for (unsigned i = 0; i < 8; ++i)
crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320U : 0);
}
return crc;
}
using storage::updateCRC32;
void put32(uint8_t* bytes, uint32_t value) {
for (unsigned i = 0; i < 4; ++i) bytes[i] = uint8_t(value >> (8 * i));
}
@@ -230,14 +224,14 @@ BeaconManager::ReadResult BeaconManager::readConfig(
if (header[4] != 1) { file.close(); return ReadResult::Future; }
if (file.size() != ConfigSize || header[5] != SlotCount || header[6] > 1
|| !validInterval(header[7])) { file.close(); return ReadResult::Invalid; }
uint32_t crc = crcUpdate(0xffffffffU, header, HeaderSize);
uint32_t crc = updateCRC32(0xffffffffU, header, HeaderSize);
bool valid = true;
for (unsigned i = 0; i < SlotCount; ++i) {
uint8_t bytes[RecordSize];
if (file.read(bytes, sizeof(bytes)) != int(sizeof(bytes))) {
file.close(); return ReadResult::Unreadable;
}
crc = crcUpdate(crc, bytes, sizeof(bytes));
crc = updateCRC32(crc, bytes, sizeof(bytes));
BeaconSlot slot;
// Disk validity is independent of the current board's radio capabilities.
// Preserve unsupported slots so an upgrade can still query/repair them.
@@ -269,7 +263,7 @@ BeaconManager::ReadResult BeaconManager::readIds(const char* path, uint32_t& las
if (memcmp(bytes, "BCI!", 4)) return ReadResult::Invalid;
if (bytes[4] != 1) return ReadResult::Future;
if (bytes[5] || bytes[6] || bytes[7]
|| get32(bytes + 12) != crcUpdate(0xffffffffU, bytes, 12)) return ReadResult::Invalid;
|| get32(bytes + 12) != updateCRC32(0xffffffffU, bytes, 12)) return ReadResult::Invalid;
last = get32(bytes + 8);
return ReadResult::Valid;
}
@@ -313,14 +307,14 @@ bool BeaconManager::saveConfig(unsigned changed_slot, const BeaconSlot* changed,
if (!openWrite(fs_, ConfigTemp, file)) return false;
uint8_t header[HeaderSize] = {'B', 'C', 'N', '!', 1, SlotCount,
uint8_t(initialized), hours};
uint32_t crc = crcUpdate(0xffffffffU, header, sizeof(header));
uint32_t crc = updateCRC32(0xffffffffU, header, sizeof(header));
bool ok = file.write(header, sizeof(header)) == sizeof(header);
for (unsigned i = 0; i < SlotCount && ok; ++i) {
BeaconSlot empty;
const BeaconSlot& slot = clear_all ? empty
: (changed && changed_slot == i ? *changed : (slots_ ? slots_[i] : empty));
uint8_t bytes[RecordSize]; encodeSlot(slot, bytes);
crc = crcUpdate(crc, bytes, sizeof(bytes));
crc = updateCRC32(crc, bytes, sizeof(bytes));
ok = file.write(bytes, sizeof(bytes)) == sizeof(bytes);
}
uint8_t trailer[4]; put32(trailer, crc);
@@ -340,7 +334,7 @@ bool BeaconManager::saveIds(uint32_t last) {
File file = makeFile(fs_);
if (!openWrite(fs_, IdTemp, file)) { ids_hold_ = true; return false; }
uint8_t bytes[IdSize] = {'B', 'C', 'I', '!', 1, 0, 0, 0};
put32(bytes + 8, last); put32(bytes + 12, crcUpdate(0xffffffffU, bytes, 12));
put32(bytes + 8, last); put32(bytes + 12, updateCRC32(0xffffffffU, bytes, 12));
bool ok = file.write(bytes, sizeof(bytes)) == sizeof(bytes);
file.flush(); file.close();
uint32_t verified = 0;
+7 -7
View File
@@ -25,19 +25,19 @@ struct ContactInfo {
mesh::ContactPathRef path_ref;
#else
uint8_t out_path[MAX_PATH_SIZE];
#endif
// Runtime-only position in the paged companion contact store. It is not
// part of the on-disk record; the record's page/slot supplies it on load.
// Mutable keeps persistence bookkeeping out of contact protocol semantics.
// Beside the cached path handle, it fills the alignment gap before timestamps.
#if defined(NRF52_PLATFORM)
mutable uint16_t storage_slot = mesh::storage::CONTACT_SLOT_NONE;
#endif
uint32_t last_advert_timestamp = 0; // by THEIR clock
uint32_t lastmod = 0; // by OUR clock
int32_t gps_lat = 0, gps_lon = 0; // 6 dec places
uint32_t sync_since = 0;
// Runtime-only position in the paged companion contact store. It is not
// part of the on-disk record; the record's page/slot supplies it on load.
// Mutable keeps persistence bookkeeping out of contact protocol semantics.
#if defined(NRF52_PLATFORM)
mutable uint16_t storage_slot = mesh::storage::CONTACT_SLOT_NONE;
#endif
ContactInfo() : shared_secret_valid(false), tx_radio(mesh::RADIO_TX_AUTO) {}
// The pointer is a short-lived borrow. Copy bytes for a queued operation;
+2 -7
View File
@@ -3,6 +3,7 @@
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "PersistentStoreFormat.h"
namespace mesh {
@@ -95,13 +96,7 @@ class ContactPathPool : public ContactPathStorage {
bool _flushing = false;
static uint32_t checksum(const uint8_t path[64]) {
uint32_t crc = 0xffffffff;
for (size_t i = 0; i < 64; ++i) {
crc ^= path[i];
for (unsigned bit = 0; bit < 8; ++bit)
crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320UL : 0);
}
return crc;
return storage::updateCRC32(0xffffffffUL, path, 64);
}
Slot* slot(uint16_t h) {
+2 -7
View File
@@ -2,6 +2,7 @@
#include <helpers/IdentityStore.h>
#include <helpers/MorseTx.h>
#include <helpers/PersistentStoreFormat.h>
#include <stddef.h>
#include <stdint.h>
@@ -27,13 +28,7 @@ inline bool validMorseSettings(const MorseSettings& prefs) {
}
inline uint32_t morseSettingsCRC(const uint8_t* data, size_t size) {
uint32_t crc = 0xffffffffUL;
while (size--) {
crc ^= *data++;
for (uint8_t bit = 0; bit < 8; ++bit)
crc = (crc >> 1) ^ ((crc & 1U) ? 0xedb88320UL : 0);
}
return crc;
return storage::updateCRC32(0xffffffffUL, data, size);
}
inline void encodeMorseSettings(const MorseSettings& prefs,
+2 -6
View File
@@ -5,6 +5,7 @@
#endif
#include "RadioProfileCommandUtils.h"
#include "FilePresence.h"
#include "PersistentStoreFormat.h"
#include "radiolib/RXPowerSaving.h"
#include "radiolib/HilTxFrequencyGuard.h"
#include <Arduino.h>
@@ -21,12 +22,7 @@ const char* const TempPath = "/radio_profiles.tmp";
// acknowledgement leaves the previous saved radio untouched.
const char* const ReplyPath = "/radio_profiles.reply";
uint32_t checksum(const uint8_t* p, size_t n) {
uint32_t crc = 0xffffffffU;
while (n--) {
crc ^= *p++;
for (int i = 0; i < 8; ++i) crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320U : 0);
}
return crc;
return storage::updateCRC32(0xffffffffU, p, n);
}
const char* modeName(RadioProfileMode m) {
return m == RadioProfileMode::RxTx ? "rxtx" : m == RadioProfileMode::Rx ? "rx" : "off";
File diff suppressed because it is too large. Load diff
+11 -6
View File
@@ -15,21 +15,25 @@ static constexpr uint8_t kChapterVerses[] = {
struct Reference { uint8_t chapter; uint8_t verse; };
enum class ParseResult { NoMatch, Invalid, Valid };
struct BlockIndex {
uint32_t offset;
template <typename Offset> struct BlockIndexT {
Offset offset;
uint16_t packed;
uint16_t plain;
uint16_t first_verse;
};
struct Corpus {
template <typename Offset> struct CorpusT {
const char* translation;
const char* attribution;
const uint8_t* data;
uint32_t data_size;
const BlockIndex* blocks;
const BlockIndexT<Offset>* blocks;
uint16_t block_count;
uint16_t verse_count;
};
// Keep the original general-purpose types available. Generated corpora select
// a narrower offset only when every block start fits, without a runtime branch.
using BlockIndex = BlockIndexT<uint32_t>;
using Corpus = CorpusT<uint32_t>;
enum class LookupResult { Found, Missing, Invalid, Corrupt };
inline bool space(char c) { return c == ' ' || c == '\t'; }
@@ -84,7 +88,8 @@ inline ParseResult parse(const char* command, Reference& ref) {
// One independently compressed block is decoded on demand. Const tables/data
// stay in memory-mapped flash on both target families; there is no heap/cache.
inline LookupResult lookup(const Corpus& corpus, Reference ref,
template <typename Offset>
inline LookupResult lookup(const CorpusT<Offset>& corpus, Reference ref,
char* scratch, size_t capacity, const char*& text) {
text = nullptr;
uint16_t index;
@@ -97,7 +102,7 @@ inline LookupResult lookup(const Corpus& corpus, Reference ref,
unsigned block_index = 0;
while (block_index + 1 < corpus.block_count &&
corpus.blocks[block_index + 1].first_verse <= index) ++block_index;
const BlockIndex block = corpus.blocks[block_index];
const BlockIndexT<Offset> block = corpus.blocks[block_index];
if (block.plain == 0 || block.plain > kBlockSize || block.packed == 0 ||
block.offset > corpus.data_size || block.packed > corpus.data_size - block.offset ||
block.first_verse > index) return LookupResult::Corrupt;