mirror of
https://github.com/crosspoint-reader/crosspoint-reader.git
synced 2026-04-29 10:26:52 -07:00
## Summary * **What is the goal of this PR?** fix edge case for definition ## Additional Context If loadFromFile() returns false (no state file exists — first boot, or SD missing), lastSleepImage is never set and contains garbage. [SleepActivity.cpp:83] then uses it in a while comparison to avoid repeating the same image. The JSON path (doc["lastSleepImage"] | (uint8_t)0) handles it, but only if the file exists. --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? _** NO **_
82 lines
2.0 KiB
C++
82 lines
2.0 KiB
C++
#include "CrossPointState.h"
|
|
|
|
#include <HalStorage.h>
|
|
#include <JsonSettingsIO.h>
|
|
#include <Logging.h>
|
|
#include <Serialization.h>
|
|
|
|
namespace {
|
|
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;
|
|
|
|
bool CrossPointState::saveToFile() const {
|
|
Storage.mkdir("/.crosspoint");
|
|
return JsonSettingsIO::saveState(*this, STATE_FILE_JSON);
|
|
}
|
|
|
|
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() {
|
|
FsFile inputFile;
|
|
if (!Storage.openFileForRead("CPS", STATE_FILE_BIN, inputFile)) {
|
|
return false;
|
|
}
|
|
|
|
uint8_t version;
|
|
serialization::readPod(inputFile, version);
|
|
if (version > STATE_FILE_VERSION) {
|
|
LOG_ERR("CPS", "Deserialization failed: Unknown version %u", version);
|
|
inputFile.close();
|
|
return false;
|
|
}
|
|
|
|
serialization::readString(inputFile, openEpubPath);
|
|
if (version >= 2) {
|
|
serialization::readPod(inputFile, lastSleepImage);
|
|
} else {
|
|
lastSleepImage = UINT8_MAX;
|
|
}
|
|
|
|
if (version >= 3) {
|
|
serialization::readPod(inputFile, readerActivityLoadCount);
|
|
}
|
|
|
|
if (version >= 4) {
|
|
serialization::readPod(inputFile, lastSleepFromReader);
|
|
} else {
|
|
lastSleepFromReader = false;
|
|
}
|
|
|
|
inputFile.close();
|
|
return true;
|
|
}
|