mirror of
https://github.com/meshtastic/firmware.git
synced 2025-12-23 11:10:52 +00:00
Backup / restore preferences method
This commit is contained in:
@@ -1597,6 +1597,81 @@ UserLicenseStatus NodeDB::getLicenseStatus(uint32_t nodeNum)
|
||||
return info->user.is_licensed ? UserLicenseStatus::Licensed : UserLicenseStatus::NotLicensed;
|
||||
}
|
||||
|
||||
bool NodeDB::backupPreferences(meshtastic_AdminMessage_BackupLocation location)
|
||||
{
|
||||
bool success = false;
|
||||
#ifdef FSCom
|
||||
if (location == meshtastic_AdminMessage_BackupLocation_FLASH) {
|
||||
meshtastic_BackupPreferences backup = meshtastic_BackupPreferences_init_zero;
|
||||
backup.version = DEVICESTATE_CUR_VER;
|
||||
backup.timestamp = getValidTime(RTCQuality::RTCQualityDevice, false);
|
||||
backup.has_config = true;
|
||||
backup.config = config;
|
||||
backup.has_module_config = true;
|
||||
backup.module_config = moduleConfig;
|
||||
backup.has_channels = true;
|
||||
backup.channels = channelFile;
|
||||
backup.has_owner = true;
|
||||
backup.owner = owner;
|
||||
|
||||
size_t backupSize;
|
||||
pb_get_encoded_size(&backupSize, meshtastic_BackupPreferences_fields, &backup);
|
||||
|
||||
spiLock->lock();
|
||||
FSCom.mkdir("/backups");
|
||||
spiLock->unlock();
|
||||
success = saveProto(backupFileName, backupSize, &meshtastic_BackupPreferences_msg, &backup);
|
||||
|
||||
if (success) {
|
||||
LOG_INFO("Saved backup preferences");
|
||||
} else {
|
||||
LOG_ERROR("Failed to save backup preferences to file");
|
||||
}
|
||||
} else if (location == meshtastic_AdminMessage_BackupLocation_SD) {
|
||||
// TODO: After more mainline SD card support
|
||||
}
|
||||
#endif
|
||||
return success;
|
||||
}
|
||||
|
||||
bool NodeDB::restorePreferences(meshtastic_AdminMessage_BackupLocation location)
|
||||
{
|
||||
bool success = false;
|
||||
#ifdef FSCom
|
||||
if (location == meshtastic_AdminMessage_BackupLocation_FLASH) {
|
||||
spiLock->lock();
|
||||
if (!FSCom.exists(backupFileName)) {
|
||||
spiLock->unlock();
|
||||
LOG_ERROR("Could not restore. No backup file found");
|
||||
return false;
|
||||
} else {
|
||||
spiLock->unlock();
|
||||
}
|
||||
meshtastic_BackupPreferences backup = meshtastic_BackupPreferences_init_zero;
|
||||
success = loadProto(backupFileName, meshtastic_BackupPreferences_size, sizeof(meshtastic_BackupPreferences),
|
||||
&meshtastic_BackupPreferences_msg, &backup);
|
||||
if (success) {
|
||||
config = backup.config;
|
||||
moduleConfig = backup.module_config;
|
||||
channelFile = backup.channels;
|
||||
owner = backup.owner;
|
||||
success = saveToDisk(SEGMENT_DEVICESTATE | SEGMENT_CONFIG | SEGMENT_MODULECONFIG | SEGMENT_CHANNELS);
|
||||
if (!success) {
|
||||
LOG_ERROR("Failed to save restored preferences to flash");
|
||||
} else {
|
||||
LOG_INFO("Restored preferences from backup... Rebooting");
|
||||
rebootAtMsec = millis() + 1000;
|
||||
}
|
||||
} else {
|
||||
LOG_ERROR("Failed to restore preferences from backup file");
|
||||
}
|
||||
} else if (location == meshtastic_AdminMessage_BackupLocation_SD) {
|
||||
// TODO: After more mainline SD card support
|
||||
}
|
||||
return success;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Record an error that should be reported via analytics
|
||||
void recordCriticalError(meshtastic_CriticalErrorCode code, uint32_t address, const char *filename)
|
||||
{
|
||||
@@ -1619,4 +1694,4 @@ void recordCriticalError(meshtastic_CriticalErrorCode code, uint32_t address, co
|
||||
LOG_ERROR("A critical failure occurred, portduino is exiting");
|
||||
exit(2);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user