Harden nRF52 contact and settings persistence
This commit is contained in:
1 parent
249c3680b3
commit
2ce59b58ed
23 files changed
+11024
-383
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -28,23 +28,31 @@ class DataStore {
|
||||
#if defined(NRF52_PLATFORM)
|
||||
mesh::storage::ContactSlotMap _contact_slots;
|
||||
mesh::storage::DirtyPageSet _dirty_contact_pages;
|
||||
mesh::storage::DirtyPageSet _unread_contact_pages;
|
||||
bool _contact_load_incomplete = false;
|
||||
bool _identity_creation_blocked = false;
|
||||
bool _primary_storage_unavailable = false;
|
||||
bool _secondary_authority_unknown = false;
|
||||
bool _prefs_load_incomplete = false;
|
||||
uint32_t _contact_page_generations[mesh::storage::CONTACT_PAGE_COUNT];
|
||||
bool _legacy_contacts_pending_cleanup;
|
||||
bool _legacy_migration_ready;
|
||||
uint16_t _legacy_contact_count;
|
||||
|
||||
bool prepareLegacyContactMigration();
|
||||
bool loadContactPages(DataStoreHost* host, uint16_t minimum_slot = 0);
|
||||
bool loadContactPages(DataStoreHost* host, uint16_t minimum_slot,
|
||||
uint32_t expected_page_mask);
|
||||
bool writeContactPage(DataStoreHost* host, uint8_t page,
|
||||
bool (*filter)(const ContactInfo& c));
|
||||
bool truncateLegacyContacts(uint16_t remaining_contacts);
|
||||
void resetContactPageState();
|
||||
void resetContactPageState(bool clear_incomplete = false);
|
||||
#if defined(EXTRAFS) && !defined(QSPIFLASH)
|
||||
bool reinitializeInternalExtraFS();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void loadPrefsInt(const char *filename, CompanionNodePrefs& prefs, double& node_lat, double& node_lon);
|
||||
bool loadPrefsInt(const char *filename, CompanionNodePrefs& prefs,
|
||||
double& node_lat, double& node_lon);
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
void checkAdvBlobFile();
|
||||
#endif
|
||||
@@ -57,18 +65,23 @@ public:
|
||||
bool repairInternalExtraFS();
|
||||
FILESYSTEM* getPrimaryFS() const { return _fs; }
|
||||
FILESYSTEM* getSecondaryFS() const { return _fsExtra; }
|
||||
void disableSecondaryFS() { _fsExtra = nullptr; }
|
||||
void markPrimaryFSUnavailable();
|
||||
void disableSecondaryFS(bool authority_unknown = true);
|
||||
bool loadMainIdentity(mesh::LocalIdentity &identity);
|
||||
bool canCreateMainIdentity() const;
|
||||
bool saveMainIdentity(const mesh::LocalIdentity &identity);
|
||||
void loadPrefs(CompanionNodePrefs& prefs, double& node_lat, double& node_lon);
|
||||
bool loadPrefs(CompanionNodePrefs& prefs, double& node_lat,
|
||||
double& node_lon);
|
||||
bool savePrefs(const CompanionNodePrefs& prefs, double node_lat, double node_lon);
|
||||
void loadContacts(DataStoreHost* host);
|
||||
bool saveContacts(DataStoreHost* host, bool (*filter)(const ContactInfo& c) = NULL);
|
||||
bool markContactDirty(const ContactInfo& contact);
|
||||
bool releaseContact(const ContactInfo& contact);
|
||||
bool restoreContactSlot(const ContactInfo& contact, uint16_t slot);
|
||||
bool serviceContactWrites(DataStoreHost* host, bool (*filter)(const ContactInfo& c) = NULL);
|
||||
bool flushContactWrites(DataStoreHost* host, bool (*filter)(const ContactInfo& c) = NULL);
|
||||
bool hasPendingContactWrites() const;
|
||||
bool hasIncompleteContactLoad() const;
|
||||
void loadChannels(DataStoreHost* host);
|
||||
bool saveChannels(DataStoreHost* host);
|
||||
bool migrateToSecondaryFS();
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -250,8 +250,9 @@ protected:
|
||||
bool shouldAutoAddContactType(uint8_t type) const override;
|
||||
bool shouldOverwriteWhenFull() const override;
|
||||
uint8_t getAutoAddMaxHops() const override;
|
||||
bool canMutateContacts() const override;
|
||||
void onContactsFull() override;
|
||||
void onContactOverwrite(const uint8_t* pub_key) override;
|
||||
bool onContactOverwrite(const ContactInfo& contact) 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;
|
||||
void onDiscoveredContact(ContactInfo &contact, bool is_new, uint8_t path_len, const uint8_t* path) override;
|
||||
void onContactPathUpdated(const ContactInfo &contact) override;
|
||||
@@ -325,9 +326,9 @@ private:
|
||||
BaseSerialInterface* route = nullptr);
|
||||
size_t writePendingSerialFrame(const uint8_t frame[], size_t len);
|
||||
void writeDisabledFrame();
|
||||
void writeContactRespFrame(uint8_t code, const ContactInfo &contact);
|
||||
bool writeContactRespFrame(uint8_t code, const ContactInfo &contact);
|
||||
void stopContactsIterator();
|
||||
void updateContactFromFrame(ContactInfo &contact, uint32_t& last_mod, const uint8_t *frame, int len);
|
||||
bool updateContactFromFrame(ContactInfo &contact, uint32_t& last_mod, const uint8_t *frame, int len);
|
||||
void addToOfflineQueue(const uint8_t frame[], int len);
|
||||
int getFromOfflineQueue(uint8_t frame[]);
|
||||
int getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) override {
|
||||
@@ -421,10 +422,17 @@ private:
|
||||
bool flushContactsBeforeReboot();
|
||||
bool prepareForOtaReboot() override;
|
||||
bool prepareForUiShutdown() override;
|
||||
void scheduleContactWrite(const ContactInfo& contact);
|
||||
void scheduleContactWriteAfterRelease(const ContactInfo& contact);
|
||||
bool scheduleContactWrite(const ContactInfo& contact);
|
||||
bool scheduleContactWriteAfterRelease(const ContactInfo& contact,
|
||||
uint16_t& released_slot);
|
||||
bool restoreContactWriteAfterRelease(const ContactInfo& contact,
|
||||
uint16_t released_slot);
|
||||
#if defined(NRF52_PLATFORM) && defined(EXTRAFS) && !defined(QSPIFLASH)
|
||||
void repairInternalExtraFS(Stream& output);
|
||||
#if defined(MESHCORE_EXTRAFS_HIL)
|
||||
bool handleExtraFsHilCommand(const char* command, char* reply,
|
||||
size_t reply_size);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
DataStore* _store;
|
||||
@@ -455,10 +463,16 @@ private:
|
||||
AbstractUITask* _ui;
|
||||
|
||||
ContactsIterator _iter;
|
||||
ContactInfo _iter_pending_contact;
|
||||
uint32_t _iter_filter_since;
|
||||
uint32_t _iter_next_frame_at;
|
||||
uint32_t _iter_total_count;
|
||||
uint32_t _iter_table_revision;
|
||||
uint32_t _most_recent_lastmod;
|
||||
uint32_t _active_ble_pin;
|
||||
bool _iter_started;
|
||||
bool _iter_start_pending;
|
||||
bool _iter_contact_pending;
|
||||
bool _cli_rescue;
|
||||
#ifdef ENABLE_USB_INTERFACE
|
||||
bool _terminal_mode;
|
||||
|
||||
@@ -156,13 +156,23 @@ static bool isNetworkTerminalActive();
|
||||
// platform file system
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
#include <InternalFileSystem.h>
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#include <flash/flash_nrf5x.h>
|
||||
#include <helpers/nrf52/InternalSecondaryFsRepair.h>
|
||||
#if defined(NRF52840_XXAA)
|
||||
static const uint32_t INTERNAL_PRIMARY_FS_START = 0x000ED000UL;
|
||||
#else
|
||||
static const uint32_t INTERNAL_PRIMARY_FS_START = 0x0006D000UL;
|
||||
#endif
|
||||
static const uint32_t INTERNAL_PRIMARY_FS_SIZE =
|
||||
7UL * FLASH_NRF52_PAGE_SIZE;
|
||||
#endif
|
||||
#if defined(QSPIFLASH)
|
||||
#include <CustomLFS_QSPIFlash.h>
|
||||
DataStore store(InternalFS, QSPIFlash, rtc_clock);
|
||||
#else
|
||||
#if defined(EXTRAFS)
|
||||
#include <CustomLFS.h>
|
||||
#include <helpers/nrf52/InternalSecondaryFsRepair.h>
|
||||
extern "C" uint32_t __flash_arduino_end[];
|
||||
CustomLFS ExtraFS(0xD4000, 0x19000, 128);
|
||||
DataStore store(InternalFS, ExtraFS, rtc_clock);
|
||||
@@ -2072,7 +2082,46 @@ void setup() {
|
||||
#endif
|
||||
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM)
|
||||
// InternalFileSystem::begin() auto-formats the entire primary store after a
|
||||
// mount failure. The base mount plus a full raw scan distinguishes a virgin
|
||||
// erased device from nonblank/corrupt identity storage. Only proven-erased
|
||||
// media may be initialized automatically; format is followed by a required
|
||||
// base remount because Adafruit_LittleFS::format() does not remount an
|
||||
// already-unmounted instance.
|
||||
const mesh::storage::InternalSecondaryFsBootResult primary_fs_boot =
|
||||
mesh::storage::prepareInternalSecondaryFilesystem(
|
||||
[]() -> bool {
|
||||
return InternalFS.Adafruit_LittleFS::begin();
|
||||
},
|
||||
[]() -> bool {
|
||||
return mesh::storage::isErasedFlashRange(
|
||||
INTERNAL_PRIMARY_FS_START, INTERNAL_PRIMARY_FS_SIZE,
|
||||
[](uint32_t address) -> uint32_t {
|
||||
return *reinterpret_cast<const volatile uint32_t*>(address);
|
||||
});
|
||||
},
|
||||
[]() -> bool {
|
||||
InternalFS.end();
|
||||
return InternalFS.format();
|
||||
});
|
||||
if (primary_fs_boot
|
||||
== mesh::storage::InternalSecondaryFsBootResult::PreservedNonBlank
|
||||
|| primary_fs_boot
|
||||
== mesh::storage::InternalSecondaryFsBootResult::InitializationFailed) {
|
||||
if (primary_fs_boot
|
||||
== mesh::storage::InternalSecondaryFsBootResult::PreservedNonBlank) {
|
||||
MESH_DEBUG_PRINTLN(
|
||||
"InternalFS: mount failed; preserving nonblank primary storage and blocking startup writes");
|
||||
} else {
|
||||
MESH_DEBUG_PRINTLN(
|
||||
"InternalFS: erased primary storage initialization failed; blocking startup writes");
|
||||
}
|
||||
store.markPrimaryFSUnavailable();
|
||||
}
|
||||
#else
|
||||
InternalFS.begin();
|
||||
#endif
|
||||
#if defined(QSPIFLASH)
|
||||
if (!QSPIFlash.begin()) {
|
||||
// debug output might not be available at this point, might be too early. maybe should fall back to InternalFS here?
|
||||
@@ -2086,7 +2135,7 @@ void setup() {
|
||||
NRF_QSPI->TASKS_DEACTIVATE = 1;
|
||||
NRF_QSPI->ENABLE = QSPI_ENABLE_ENABLE_Disabled;
|
||||
#endif
|
||||
store.disableSecondaryFS();
|
||||
store.disableSecondaryFS(true);
|
||||
} else {
|
||||
MESH_DEBUG_PRINTLN("CustomLFS_QSPIFlash: initialized successfully");
|
||||
}
|
||||
@@ -2134,7 +2183,10 @@ void setup() {
|
||||
} else {
|
||||
MESH_DEBUG_PRINTLN("CustomLFS: blank internal ExtraFS initialization failed; using primary storage");
|
||||
}
|
||||
store.disableSecondaryFS();
|
||||
const bool secondary_authority_unknown = !extra_fs_geometry_valid
|
||||
|| extra_fs_boot
|
||||
== mesh::storage::InternalSecondaryFsBootResult::PreservedNonBlank;
|
||||
store.disableSecondaryFS(secondary_authority_unknown);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user