2025-12-04 00:07:25 +11:00
|
|
|
#include "CrossPointState.h"
|
|
|
|
|
|
2026-02-08 21:29:14 +01:00
|
|
|
#include <HalStorage.h>
|
2026-02-22 07:18:25 +01:00
|
|
|
#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>
|
|
|
|
|
|
2025-12-17 23:32:18 +11:00
|
|
|
namespace {
|
2026-02-08 21:01:30 +03:00
|
|
|
constexpr uint8_t STATE_FILE_VERSION = 4;
|
2026-02-22 07:18:25 +01:00
|
|
|
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";
|
2025-12-17 23:32:18 +11:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
CrossPointState CrossPointState::instance;
|
2025-12-04 00:07:25 +11:00
|
|
|
|
2025-12-06 12:56:39 +11:00
|
|
|
bool CrossPointState::saveToFile() const {
|
2026-02-22 07:18:25 +01:00
|
|
|
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() {
|
2026-02-22 07:18:25 +01:00
|
|
|
// 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;
|
2026-02-22 07:18:25 +01:00
|
|
|
if (!Storage.openFileForRead("CPS", STATE_FILE_BIN, inputFile)) {
|
2025-12-23 14:14:10 +11:00
|
|
|
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);
|
2026-01-14 05:05:08 -05:00
|
|
|
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);
|
2026-01-14 05:05:08 -05:00
|
|
|
if (version >= 2) {
|
|
|
|
|
serialization::readPod(inputFile, lastSleepImage);
|
|
|
|
|
} else {
|
2026-03-12 00:37:13 +01:00
|
|
|
lastSleepImage = UINT8_MAX;
|
2026-01-14 05:05:08 -05:00
|
|
|
}
|
2025-12-04 00:07:25 +11:00
|
|
|
|
2026-02-05 19:45:09 +08: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
|
|
|
}
|