Files

82 lines
2.0 KiB
C++
Raw Permalink Normal View History

2025-12-04 00:07:25 +11:00
#include "CrossPointState.h"
2026-02-08 21:29:14 +01:00
#include <HalStorage.h>
#include <JsonSettingsIO.h>
2026-02-13 12:16:39 +01:00
#include <Logging.h>
2025-12-04 00:07:25 +11:00
#include <Serialization.h>
namespace {
2026-02-08 21:01:30 +03:00
constexpr uint8_t STATE_FILE_VERSION = 4;
constexpr char STATE_FILE_BIN[] = "/.crosspoint/state.bin";
constexpr char STATE_FILE_JSON[] = "/.crosspoint/state.json";
constexpr char STATE_FILE_BAK[] = "/.crosspoint/state.bin.bak";
} // namespace
CrossPointState CrossPointState::instance;
2025-12-04 00:07:25 +11:00
2025-12-06 12:56:39 +11:00
bool CrossPointState::saveToFile() const {
Storage.mkdir("/.crosspoint");
return JsonSettingsIO::saveState(*this, STATE_FILE_JSON);
2025-12-04 00:07:25 +11:00
}
2025-12-06 12:56:39 +11:00
bool CrossPointState::loadFromFile() {
// Try JSON first
if (Storage.exists(STATE_FILE_JSON)) {
String json = Storage.readFile(STATE_FILE_JSON);
if (!json.isEmpty()) {
return JsonSettingsIO::loadState(*this, json.c_str());
}
}
// Fall back to binary migration
if (Storage.exists(STATE_FILE_BIN)) {
if (loadFromBinaryFile()) {
if (saveToFile()) {
Storage.rename(STATE_FILE_BIN, STATE_FILE_BAK);
LOG_DBG("CPS", "Migrated state.bin to state.json");
return true;
} else {
LOG_ERR("CPS", "Failed to save state during migration");
return false;
}
}
}
return false;
}
bool CrossPointState::loadFromBinaryFile() {
2025-12-30 15:09:30 +10:00
FsFile inputFile;
if (!Storage.openFileForRead("CPS", STATE_FILE_BIN, inputFile)) {
return false;
}
2025-12-04 00:07:25 +11:00
uint8_t version;
2025-12-06 12:56:39 +11:00
serialization::readPod(inputFile, version);
if (version > STATE_FILE_VERSION) {
2026-02-13 12:16:39 +01:00
LOG_ERR("CPS", "Deserialization failed: Unknown version %u", version);
2025-12-06 12:56:39 +11:00
inputFile.close();
return false;
2025-12-04 00:07:25 +11:00
}
2025-12-06 12:56:39 +11:00
serialization::readString(inputFile, openEpubPath);
if (version >= 2) {
serialization::readPod(inputFile, lastSleepImage);
} else {
lastSleepImage = UINT8_MAX;
}
2025-12-04 00:07:25 +11:00
if (version >= 3) {
serialization::readPod(inputFile, readerActivityLoadCount);
}
2026-02-08 21:01:30 +03:00
if (version >= 4) {
serialization::readPod(inputFile, lastSleepFromReader);
} else {
lastSleepFromReader = false;
}
2025-12-04 00:07:25 +11:00
inputFile.close();
2025-12-06 12:56:39 +11:00
return true;
2025-12-04 00:07:25 +11:00
}