A fork of https://github.com/crosspoint-reader/crosspoint-reader
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: Migrate binary settings to json (#920)

## Summary

* This PR introduces a migration from binary file storage to JSON-based
storage for application settings, state, and various credential stores.
This improves readability, maintainability, and allows for easier manual
configuration editing.
* Benefits:
- Settings files are now JSON and can be easily read/edited manually
- Easier to inspect application state and settings during development
- JSON structure is more flexible for future changes
* Drawback: around 15k of additional flash usage
* Compatibility: Seamless migration preserves existing user data

## Additional Context
1. New JSON I/O Infrastructure files:
- JsonSettingsIO: Core JSON serialization/deserialization logic using
ArduinoJson library
- ObfuscationUtils: XOR-based password obfuscation for sensitive data
2. Migrated Components (now use JSON storage with automatic binary
migration):
- CrossPointSettings (settings.json): Main application settings
- CrossPointState (state.json): Application state (open book, sleep
mode, etc.)
- WifiCredentialStore (wifi.json): WiFi network credentials (Password
Obfuscation: Sensitive data like WiFi passwords, uses XOR encryption
with fixed keys. Note: This is obfuscation, not cryptographic security -
passwords can be recovered with the key)
- KOReaderCredentialStore (koreader.json): KOReader sync credentials
- RecentBooksStore (recent.json): Recently opened books list
3. Migration Logic
- Forward Compatibility: New installations use JSON format
- Backward Compatibility: Existing binary files are automatically
migrated to JSON on first load
- Backup Safety: Original binary files are renamed with .bak extension
after successful migration
- Fallback Handling: If JSON parsing fails, system falls back to binary
loading
4. Infrastructure Updates
- HalStorage: Added rename() method for backup operations

---

### 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? _** YES**_

---------

Co-authored-by: Dave Allie <dave@daveallie.com>

authored by

jpirnay
Dave Allie
and committed by
GitHub
6e4d0e53 f62529ad

+702 -242
+48 -44
lib/KOReaderSync/KOReaderCredentialStore.cpp
··· 3 3 #include <HalStorage.h> 4 4 #include <Logging.h> 5 5 #include <MD5Builder.h> 6 + #include <ObfuscationUtils.h> 6 7 #include <Serialization.h> 8 + 9 + #include "../../src/JsonSettingsIO.h" 7 10 8 11 // Initialize the static instance 9 12 KOReaderCredentialStore KOReaderCredentialStore::instance; 10 13 11 14 namespace { 12 - // File format version 15 + // File format version (for binary migration) 13 16 constexpr uint8_t KOREADER_FILE_VERSION = 1; 14 17 15 - // KOReader credentials file path 16 - constexpr char KOREADER_FILE[] = "/.crosspoint/koreader.bin"; 18 + // File paths 19 + constexpr char KOREADER_FILE_BIN[] = "/.crosspoint/koreader.bin"; 20 + constexpr char KOREADER_FILE_JSON[] = "/.crosspoint/koreader.json"; 21 + constexpr char KOREADER_FILE_BAK[] = "/.crosspoint/koreader.bin.bak"; 17 22 18 23 // Default sync server URL 19 24 constexpr char DEFAULT_SERVER_URL[] = "https://sync.koreader.rocks:443"; 20 25 21 - // Obfuscation key - "KOReader" in ASCII 22 - // This is NOT cryptographic security, just prevents casual file reading 23 - constexpr uint8_t OBFUSCATION_KEY[] = {0x4B, 0x4F, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72}; 24 - constexpr size_t KEY_LENGTH = sizeof(OBFUSCATION_KEY); 25 - } // namespace 26 + // Legacy obfuscation key - "KOReader" in ASCII (only used for binary migration) 27 + constexpr uint8_t LEGACY_OBFUSCATION_KEY[] = {0x4B, 0x4F, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72}; 28 + constexpr size_t LEGACY_KEY_LENGTH = sizeof(LEGACY_OBFUSCATION_KEY); 26 29 27 - void KOReaderCredentialStore::obfuscate(std::string& data) const { 30 + void legacyDeobfuscate(std::string& data) { 28 31 for (size_t i = 0; i < data.size(); i++) { 29 - data[i] ^= OBFUSCATION_KEY[i % KEY_LENGTH]; 32 + data[i] ^= LEGACY_OBFUSCATION_KEY[i % LEGACY_KEY_LENGTH]; 30 33 } 31 34 } 35 + } // namespace 32 36 33 37 bool KOReaderCredentialStore::saveToFile() const { 34 - // Make sure the directory exists 35 38 Storage.mkdir("/.crosspoint"); 39 + return JsonSettingsIO::saveKOReader(*this, KOREADER_FILE_JSON); 40 + } 36 41 37 - FsFile file; 38 - if (!Storage.openFileForWrite("KRS", KOREADER_FILE, file)) { 39 - return false; 42 + bool KOReaderCredentialStore::loadFromFile() { 43 + // Try JSON first 44 + if (Storage.exists(KOREADER_FILE_JSON)) { 45 + String json = Storage.readFile(KOREADER_FILE_JSON); 46 + if (!json.isEmpty()) { 47 + bool resave = false; 48 + bool result = JsonSettingsIO::loadKOReader(*this, json.c_str(), &resave); 49 + if (result && resave) { 50 + saveToFile(); 51 + LOG_DBG("KRS", "Resaved KOReader credentials to update format"); 52 + } 53 + return result; 54 + } 40 55 } 41 56 42 - // Write header 43 - serialization::writePod(file, KOREADER_FILE_VERSION); 44 - 45 - // Write username (plaintext - not particularly sensitive) 46 - serialization::writeString(file, username); 47 - LOG_DBG("KRS", "Saving username: %s", username.c_str()); 48 - 49 - // Write password (obfuscated) 50 - std::string obfuscatedPwd = password; 51 - obfuscate(obfuscatedPwd); 52 - serialization::writeString(file, obfuscatedPwd); 53 - 54 - // Write server URL 55 - serialization::writeString(file, serverUrl); 56 - 57 - // Write match method 58 - serialization::writePod(file, static_cast<uint8_t>(matchMethod)); 57 + // Fall back to binary migration 58 + if (Storage.exists(KOREADER_FILE_BIN)) { 59 + if (loadFromBinaryFile()) { 60 + if (saveToFile()) { 61 + Storage.rename(KOREADER_FILE_BIN, KOREADER_FILE_BAK); 62 + LOG_DBG("KRS", "Migrated koreader.bin to koreader.json"); 63 + return true; 64 + } else { 65 + LOG_ERR("KRS", "Failed to save KOReader credentials during migration"); 66 + return false; 67 + } 68 + } 69 + } 59 70 60 - file.close(); 61 - LOG_DBG("KRS", "Saved KOReader credentials to file"); 62 - return true; 71 + LOG_DBG("KRS", "No credentials file found"); 72 + return false; 63 73 } 64 74 65 - bool KOReaderCredentialStore::loadFromFile() { 75 + bool KOReaderCredentialStore::loadFromBinaryFile() { 66 76 FsFile file; 67 - if (!Storage.openFileForRead("KRS", KOREADER_FILE, file)) { 68 - LOG_DBG("KRS", "No credentials file found"); 77 + if (!Storage.openFileForRead("KRS", KOREADER_FILE_BIN, file)) { 69 78 return false; 70 79 } 71 80 72 - // Read and verify version 73 81 uint8_t version; 74 82 serialization::readPod(file, version); 75 83 if (version != KOREADER_FILE_VERSION) { ··· 78 86 return false; 79 87 } 80 88 81 - // Read username 82 89 if (file.available()) { 83 90 serialization::readString(file, username); 84 91 } else { 85 92 username.clear(); 86 93 } 87 94 88 - // Read and deobfuscate password 89 95 if (file.available()) { 90 96 serialization::readString(file, password); 91 - obfuscate(password); // XOR is symmetric, so same function deobfuscates 97 + legacyDeobfuscate(password); 92 98 } else { 93 99 password.clear(); 94 100 } 95 101 96 - // Read server URL 97 102 if (file.available()) { 98 103 serialization::readString(file, serverUrl); 99 104 } else { 100 105 serverUrl.clear(); 101 106 } 102 107 103 - // Read match method 104 108 if (file.available()) { 105 109 uint8_t method; 106 110 serialization::readPod(file, method); ··· 110 114 } 111 115 112 116 file.close(); 113 - LOG_DBG("KRS", "Loaded KOReader credentials for user: %s", username.c_str()); 117 + LOG_DBG("KRS", "Loaded KOReader credentials from binary for user: %s", username.c_str()); 114 118 return true; 115 119 } 116 120
+13 -4
lib/KOReaderSync/KOReaderCredentialStore.h
··· 8 8 BINARY = 1, // Match by partial MD5 of file content (more accurate, but files must be identical) 9 9 }; 10 10 11 + class KOReaderCredentialStore; 12 + namespace JsonSettingsIO { 13 + bool saveKOReader(const KOReaderCredentialStore& store, const char* path); 14 + bool loadKOReader(KOReaderCredentialStore& store, const char* json, bool* needsResave); 15 + } // namespace JsonSettingsIO 16 + 11 17 /** 12 18 * Singleton class for storing KOReader sync credentials on the SD card. 13 - * Credentials are stored in /sd/.crosspoint/koreader.bin with basic 14 - * XOR obfuscation to prevent casual reading (not cryptographically secure). 19 + * Passwords are XOR-obfuscated with the device's unique hardware MAC address 20 + * and base64-encoded before writing to JSON (not cryptographically secure, 21 + * but prevents casual reading and ties credentials to the specific device). 15 22 */ 16 23 class KOReaderCredentialStore { 17 24 private: ··· 24 31 // Private constructor for singleton 25 32 KOReaderCredentialStore() = default; 26 33 27 - // XOR obfuscation (symmetric - same for encode/decode) 28 - void obfuscate(std::string& data) const; 34 + bool loadFromBinaryFile(); 35 + 36 + friend bool JsonSettingsIO::saveKOReader(const KOReaderCredentialStore&, const char*); 37 + friend bool JsonSettingsIO::loadKOReader(KOReaderCredentialStore&, const char*, bool*); 29 38 30 39 public: 31 40 // Delete copy constructor and assignment
+98
lib/Serialization/ObfuscationUtils.cpp
··· 1 + #include "ObfuscationUtils.h" 2 + 3 + #include <Logging.h> 4 + #include <base64.h> 5 + #include <esp_mac.h> 6 + #include <mbedtls/base64.h> 7 + 8 + #include <cstring> 9 + 10 + namespace obfuscation { 11 + 12 + namespace { 13 + constexpr size_t HW_KEY_LEN = 6; 14 + 15 + // Simple lazy init — no thread-safety concern on single-core ESP32-C3. 16 + const uint8_t* getHwKey() { 17 + static uint8_t key[HW_KEY_LEN] = {}; 18 + static bool initialized = false; 19 + if (!initialized) { 20 + esp_efuse_mac_get_default(key); 21 + initialized = true; 22 + } 23 + return key; 24 + } 25 + } // namespace 26 + 27 + void xorTransform(std::string& data) { 28 + const uint8_t* key = getHwKey(); 29 + for (size_t i = 0; i < data.size(); i++) { 30 + data[i] ^= key[i % HW_KEY_LEN]; 31 + } 32 + } 33 + 34 + void xorTransform(std::string& data, const uint8_t* key, size_t keyLen) { 35 + if (keyLen == 0 || key == nullptr) return; 36 + for (size_t i = 0; i < data.size(); i++) { 37 + data[i] ^= key[i % keyLen]; 38 + } 39 + } 40 + 41 + String obfuscateToBase64(const std::string& plaintext) { 42 + if (plaintext.empty()) return ""; 43 + std::string temp = plaintext; 44 + xorTransform(temp); 45 + return base64::encode(reinterpret_cast<const uint8_t*>(temp.data()), temp.size()); 46 + } 47 + 48 + std::string deobfuscateFromBase64(const char* encoded, bool* ok) { 49 + if (encoded == nullptr || encoded[0] == '\0') { 50 + if (ok) *ok = false; 51 + return ""; 52 + } 53 + if (ok) *ok = true; 54 + size_t encodedLen = strlen(encoded); 55 + // First call: get required output buffer size 56 + size_t decodedLen = 0; 57 + int ret = mbedtls_base64_decode(nullptr, 0, &decodedLen, reinterpret_cast<const unsigned char*>(encoded), encodedLen); 58 + if (ret != 0 && ret != MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL) { 59 + LOG_ERR("OBF", "Base64 decode size query failed (ret=%d)", ret); 60 + if (ok) *ok = false; 61 + return ""; 62 + } 63 + std::string result(decodedLen, '\0'); 64 + ret = mbedtls_base64_decode(reinterpret_cast<unsigned char*>(&result[0]), decodedLen, &decodedLen, 65 + reinterpret_cast<const unsigned char*>(encoded), encodedLen); 66 + if (ret != 0) { 67 + LOG_ERR("OBF", "Base64 decode failed (ret=%d)", ret); 68 + if (ok) *ok = false; 69 + return ""; 70 + } 71 + result.resize(decodedLen); 72 + xorTransform(result); 73 + return result; 74 + } 75 + 76 + void selfTest() { 77 + const char* testInputs[] = {"", "hello", "WiFi P@ssw0rd!", "a"}; 78 + bool allPassed = true; 79 + for (const char* input : testInputs) { 80 + String encoded = obfuscateToBase64(std::string(input)); 81 + std::string decoded = deobfuscateFromBase64(encoded.c_str()); 82 + if (decoded != input) { 83 + LOG_ERR("OBF", "FAIL: \"%s\" -> \"%s\" -> \"%s\"", input, encoded.c_str(), decoded.c_str()); 84 + allPassed = false; 85 + } 86 + } 87 + // Verify obfuscated form differs from plaintext 88 + String enc = obfuscateToBase64("test123"); 89 + if (enc == "test123") { 90 + LOG_ERR("OBF", "FAIL: obfuscated output identical to plaintext"); 91 + allPassed = false; 92 + } 93 + if (allPassed) { 94 + LOG_DBG("OBF", "Obfuscation self-test PASSED"); 95 + } 96 + } 97 + 98 + } // namespace obfuscation
+35
lib/Serialization/ObfuscationUtils.h
··· 1 + #pragma once 2 + 3 + #include <Arduino.h> 4 + 5 + #include <cstddef> 6 + #include <cstdint> 7 + #include <string> 8 + 9 + /** 10 + * Credential obfuscation utilities using the ESP32's unique hardware MAC address. 11 + * 12 + * XOR-based obfuscation with the 6-byte eFuse MAC as key. Not cryptographically 13 + * secure, but prevents casual reading of credentials on the SD card and ties 14 + * obfuscated data to the specific device (cannot be decoded on another chip or PC). 15 + * 16 + */ 17 + namespace obfuscation { 18 + 19 + // XOR obfuscate/deobfuscate in-place using hardware MAC key (symmetric operation) 20 + void xorTransform(std::string& data); 21 + 22 + // Legacy overload for binary migration (uses the old per-store hardcoded keys) 23 + void xorTransform(std::string& data, const uint8_t* key, size_t keyLen); 24 + 25 + // Obfuscate a plaintext string: XOR with hardware key, then base64-encode for JSON storage 26 + String obfuscateToBase64(const std::string& plaintext); 27 + 28 + // Decode base64 and de-obfuscate back to plaintext. 29 + // Returns empty string on invalid base64 input; sets *ok to false if decode fails. 30 + std::string deobfuscateFromBase64(const char* encoded, bool* ok = nullptr); 31 + 32 + // Self-test: verifies round-trip obfuscation with hardware key. Logs PASS/FAIL. 33 + void selfTest(); 34 + 35 + } // namespace obfuscation
+2
lib/hal/HalStorage.cpp
··· 36 36 37 37 bool HalStorage::remove(const char* path) { return SDCard.remove(path); } 38 38 39 + bool HalStorage::rename(const char* oldPath, const char* newPath) { return SDCard.rename(oldPath, newPath); } 40 + 39 41 bool HalStorage::rmdir(const char* path) { return SDCard.rmdir(path); } 40 42 41 43 bool HalStorage::openFileForRead(const char* moduleName, const char* path, FsFile& file) {
+1
lib/hal/HalStorage.h
··· 28 28 bool mkdir(const char* path, const bool pFlag = true); 29 29 bool exists(const char* path); 30 30 bool remove(const char* path); 31 + bool rename(const char* oldPath, const char* newPath); 31 32 bool rmdir(const char* path); 32 33 33 34 bool openFileForRead(const char* moduleName, const char* path, FsFile& file);
+53 -104
src/CrossPointSettings.cpp
··· 1 1 #include "CrossPointSettings.h" 2 2 3 3 #include <HalStorage.h> 4 + #include <JsonSettingsIO.h> 4 5 #include <Logging.h> 5 6 #include <Serialization.h> 6 7 ··· 22 23 23 24 namespace { 24 25 constexpr uint8_t SETTINGS_FILE_VERSION = 1; 25 - // SETTINGS_COUNT is now calculated automatically in saveToFile 26 - constexpr char SETTINGS_FILE[] = "/.crosspoint/settings.bin"; 27 - 28 - // Validate front button mapping to ensure each hardware button is unique. 29 - // If duplicates are detected, reset to the default physical order to prevent invalid mappings. 30 - void validateFrontButtonMapping(CrossPointSettings& settings) { 31 - // Snapshot the logical->hardware mapping so we can compare for duplicates. 32 - const uint8_t mapping[] = {settings.frontButtonBack, settings.frontButtonConfirm, settings.frontButtonLeft, 33 - settings.frontButtonRight}; 34 - for (size_t i = 0; i < 4; i++) { 35 - for (size_t j = i + 1; j < 4; j++) { 36 - if (mapping[i] == mapping[j]) { 37 - // Duplicate detected: restore the default physical order (Back, Confirm, Left, Right). 38 - settings.frontButtonBack = CrossPointSettings::FRONT_HW_BACK; 39 - settings.frontButtonConfirm = CrossPointSettings::FRONT_HW_CONFIRM; 40 - settings.frontButtonLeft = CrossPointSettings::FRONT_HW_LEFT; 41 - settings.frontButtonRight = CrossPointSettings::FRONT_HW_RIGHT; 42 - return; 43 - } 44 - } 45 - } 46 - } 26 + constexpr char SETTINGS_FILE_BIN[] = "/.crosspoint/settings.bin"; 27 + constexpr char SETTINGS_FILE_JSON[] = "/.crosspoint/settings.json"; 28 + constexpr char SETTINGS_FILE_BAK[] = "/.crosspoint/settings.bin.bak"; 47 29 48 30 // Convert legacy front button layout into explicit logical->hardware mapping. 49 31 void applyLegacyFrontButtonLayout(CrossPointSettings& settings) { ··· 77 59 } 78 60 } // namespace 79 61 80 - class SettingsWriter { 81 - public: 82 - bool is_counting = false; 83 - uint8_t item_count = 0; 84 - template <typename T> 85 - 86 - void writeItem(FsFile& file, const T& value) { 87 - if (is_counting) { 88 - item_count++; 89 - } else { 90 - serialization::writePod(file, value); 62 + void CrossPointSettings::validateFrontButtonMapping(CrossPointSettings& settings) { 63 + const uint8_t mapping[] = {settings.frontButtonBack, settings.frontButtonConfirm, settings.frontButtonLeft, 64 + settings.frontButtonRight}; 65 + for (size_t i = 0; i < 4; i++) { 66 + for (size_t j = i + 1; j < 4; j++) { 67 + if (mapping[i] == mapping[j]) { 68 + settings.frontButtonBack = FRONT_HW_BACK; 69 + settings.frontButtonConfirm = FRONT_HW_CONFIRM; 70 + settings.frontButtonLeft = FRONT_HW_LEFT; 71 + settings.frontButtonRight = FRONT_HW_RIGHT; 72 + return; 73 + } 91 74 } 92 75 } 93 - 94 - void writeItemString(FsFile& file, const char* value) { 95 - if (is_counting) { 96 - item_count++; 97 - } else { 98 - serialization::writeString(file, std::string(value)); 99 - } 100 - } 101 - }; 102 - 103 - uint8_t CrossPointSettings::writeSettings(FsFile& file, bool count_only) const { 104 - SettingsWriter writer; 105 - writer.is_counting = count_only; 106 - 107 - writer.writeItem(file, sleepScreen); 108 - writer.writeItem(file, extraParagraphSpacing); 109 - writer.writeItem(file, shortPwrBtn); 110 - writer.writeItem(file, statusBar); 111 - writer.writeItem(file, orientation); 112 - writer.writeItem(file, frontButtonLayout); // legacy 113 - writer.writeItem(file, sideButtonLayout); 114 - writer.writeItem(file, fontFamily); 115 - writer.writeItem(file, fontSize); 116 - writer.writeItem(file, lineSpacing); 117 - writer.writeItem(file, paragraphAlignment); 118 - writer.writeItem(file, sleepTimeout); 119 - writer.writeItem(file, refreshFrequency); 120 - writer.writeItem(file, screenMargin); 121 - writer.writeItem(file, sleepScreenCoverMode); 122 - writer.writeItemString(file, opdsServerUrl); 123 - writer.writeItem(file, textAntiAliasing); 124 - writer.writeItem(file, hideBatteryPercentage); 125 - writer.writeItem(file, longPressChapterSkip); 126 - writer.writeItem(file, hyphenationEnabled); 127 - writer.writeItemString(file, opdsUsername); 128 - writer.writeItemString(file, opdsPassword); 129 - writer.writeItem(file, sleepScreenCoverFilter); 130 - writer.writeItem(file, uiTheme); 131 - writer.writeItem(file, frontButtonBack); 132 - writer.writeItem(file, frontButtonConfirm); 133 - writer.writeItem(file, frontButtonLeft); 134 - writer.writeItem(file, frontButtonRight); 135 - writer.writeItem(file, fadingFix); 136 - writer.writeItem(file, embeddedStyle); 137 - // New fields need to be added at end for backward compatibility 138 - 139 - return writer.item_count; 140 76 } 141 77 142 78 bool CrossPointSettings::saveToFile() const { 143 - // Make sure the directory exists 144 79 Storage.mkdir("/.crosspoint"); 80 + return JsonSettingsIO::saveSettings(*this, SETTINGS_FILE_JSON); 81 + } 145 82 146 - FsFile outputFile; 147 - if (!Storage.openFileForWrite("CPS", SETTINGS_FILE, outputFile)) { 148 - return false; 83 + bool CrossPointSettings::loadFromFile() { 84 + // Try JSON first 85 + if (Storage.exists(SETTINGS_FILE_JSON)) { 86 + String json = Storage.readFile(SETTINGS_FILE_JSON); 87 + if (!json.isEmpty()) { 88 + bool resave = false; 89 + bool result = JsonSettingsIO::loadSettings(*this, json.c_str(), &resave); 90 + if (result && resave) { 91 + if (saveToFile()) { 92 + LOG_DBG("CPS", "Resaved settings to update format"); 93 + } else { 94 + LOG_ERR("CPS", "Failed to resave settings after format update"); 95 + } 96 + } 97 + return result; 98 + } 149 99 } 150 100 151 - // First pass: count the items 152 - uint8_t item_count = writeSettings(outputFile, true); // This will just count, not write 153 - 154 - // Write header 155 - serialization::writePod(outputFile, SETTINGS_FILE_VERSION); 156 - serialization::writePod(outputFile, static_cast<uint8_t>(item_count)); 157 - // Second pass: actually write the settings 158 - writeSettings(outputFile); // This will write the actual data 159 - 160 - outputFile.close(); 101 + // Fall back to binary migration 102 + if (Storage.exists(SETTINGS_FILE_BIN)) { 103 + if (loadFromBinaryFile()) { 104 + if (saveToFile()) { 105 + Storage.rename(SETTINGS_FILE_BIN, SETTINGS_FILE_BAK); 106 + LOG_DBG("CPS", "Migrated settings.bin to settings.json"); 107 + return true; 108 + } else { 109 + LOG_ERR("CPS", "Failed to save migrated settings to JSON"); 110 + return false; 111 + } 112 + } 113 + } 161 114 162 - LOG_DBG("CPS", "Settings saved to file"); 163 - return true; 115 + return false; 164 116 } 165 117 166 - bool CrossPointSettings::loadFromFile() { 118 + bool CrossPointSettings::loadFromBinaryFile() { 167 119 FsFile inputFile; 168 - if (!Storage.openFileForRead("CPS", SETTINGS_FILE, inputFile)) { 120 + if (!Storage.openFileForRead("CPS", SETTINGS_FILE_BIN, inputFile)) { 169 121 return false; 170 122 } 171 123 ··· 180 132 uint8_t fileSettingsCount = 0; 181 133 serialization::readPod(inputFile, fileSettingsCount); 182 134 183 - // load settings that exist (support older files with fewer fields) 184 135 uint8_t settingsRead = 0; 185 - // Track whether remap fields were present in the settings file. 186 136 bool frontButtonMappingRead = false; 187 137 do { 188 138 readAndValidate(inputFile, sleepScreen, SLEEP_SCREEN_MODE_COUNT); ··· 195 145 if (++settingsRead >= fileSettingsCount) break; 196 146 readAndValidate(inputFile, orientation, ORIENTATION_COUNT); 197 147 if (++settingsRead >= fileSettingsCount) break; 198 - readAndValidate(inputFile, frontButtonLayout, FRONT_BUTTON_LAYOUT_COUNT); // legacy 148 + readAndValidate(inputFile, frontButtonLayout, FRONT_BUTTON_LAYOUT_COUNT); 199 149 if (++settingsRead >= fileSettingsCount) break; 200 150 readAndValidate(inputFile, sideButtonLayout, SIDE_BUTTON_LAYOUT_COUNT); 201 151 if (++settingsRead >= fileSettingsCount) break; ··· 261 211 if (++settingsRead >= fileSettingsCount) break; 262 212 serialization::readPod(inputFile, embeddedStyle); 263 213 if (++settingsRead >= fileSettingsCount) break; 264 - // New fields added at end for backward compatibility 265 214 } while (false); 266 215 267 216 if (frontButtonMappingRead) { 268 - validateFrontButtonMapping(*this); 217 + CrossPointSettings::validateFrontButtonMapping(*this); 269 218 } else { 270 219 applyLegacyFrontButtonLayout(*this); 271 220 } 272 221 273 222 inputFile.close(); 274 - LOG_DBG("CPS", "Settings loaded from file"); 223 + LOG_DBG("CPS", "Settings loaded from binary file"); 275 224 return true; 276 225 } 277 226
+6
src/CrossPointSettings.h
··· 191 191 bool saveToFile() const; 192 192 bool loadFromFile(); 193 193 194 + static void validateFrontButtonMapping(CrossPointSettings& settings); 195 + 196 + private: 197 + bool loadFromBinaryFile(); 198 + 199 + public: 194 200 float getReaderLineCompression() const; 195 201 unsigned long getSleepTimeoutMs() const; 196 202 int getRefreshFrequency() const;
+32 -13
src/CrossPointState.cpp
··· 1 1 #include "CrossPointState.h" 2 2 3 3 #include <HalStorage.h> 4 + #include <JsonSettingsIO.h> 4 5 #include <Logging.h> 5 6 #include <Serialization.h> 6 7 7 8 namespace { 8 9 constexpr uint8_t STATE_FILE_VERSION = 4; 9 - constexpr char STATE_FILE[] = "/.crosspoint/state.bin"; 10 + constexpr char STATE_FILE_BIN[] = "/.crosspoint/state.bin"; 11 + constexpr char STATE_FILE_JSON[] = "/.crosspoint/state.json"; 12 + constexpr char STATE_FILE_BAK[] = "/.crosspoint/state.bin.bak"; 10 13 } // namespace 11 14 12 15 CrossPointState CrossPointState::instance; 13 16 14 17 bool CrossPointState::saveToFile() const { 15 - FsFile outputFile; 16 - if (!Storage.openFileForWrite("CPS", STATE_FILE, outputFile)) { 17 - return false; 18 + Storage.mkdir("/.crosspoint"); 19 + return JsonSettingsIO::saveState(*this, STATE_FILE_JSON); 20 + } 21 + 22 + bool CrossPointState::loadFromFile() { 23 + // Try JSON first 24 + if (Storage.exists(STATE_FILE_JSON)) { 25 + String json = Storage.readFile(STATE_FILE_JSON); 26 + if (!json.isEmpty()) { 27 + return JsonSettingsIO::loadState(*this, json.c_str()); 28 + } 29 + } 30 + 31 + // Fall back to binary migration 32 + if (Storage.exists(STATE_FILE_BIN)) { 33 + if (loadFromBinaryFile()) { 34 + if (saveToFile()) { 35 + Storage.rename(STATE_FILE_BIN, STATE_FILE_BAK); 36 + LOG_DBG("CPS", "Migrated state.bin to state.json"); 37 + return true; 38 + } else { 39 + LOG_ERR("CPS", "Failed to save state during migration"); 40 + return false; 41 + } 42 + } 18 43 } 19 44 20 - serialization::writePod(outputFile, STATE_FILE_VERSION); 21 - serialization::writeString(outputFile, openEpubPath); 22 - serialization::writePod(outputFile, lastSleepImage); 23 - serialization::writePod(outputFile, readerActivityLoadCount); 24 - serialization::writePod(outputFile, lastSleepFromReader); 25 - outputFile.close(); 26 - return true; 45 + return false; 27 46 } 28 47 29 - bool CrossPointState::loadFromFile() { 48 + bool CrossPointState::loadFromBinaryFile() { 30 49 FsFile inputFile; 31 - if (!Storage.openFileForRead("CPS", STATE_FILE, inputFile)) { 50 + if (!Storage.openFileForRead("CPS", STATE_FILE_BIN, inputFile)) { 32 51 return false; 33 52 } 34 53
+3
src/CrossPointState.h
··· 19 19 bool saveToFile() const; 20 20 21 21 bool loadFromFile(); 22 + 23 + private: 24 + bool loadFromBinaryFile(); 22 25 }; 23 26 24 27 // Helper macro to access settings
+281
src/JsonSettingsIO.cpp
··· 1 + #include "JsonSettingsIO.h" 2 + 3 + #include <ArduinoJson.h> 4 + #include <HalStorage.h> 5 + #include <Logging.h> 6 + #include <ObfuscationUtils.h> 7 + 8 + #include <cstring> 9 + 10 + #include "CrossPointSettings.h" 11 + #include "CrossPointState.h" 12 + #include "KOReaderCredentialStore.h" 13 + #include "RecentBooksStore.h" 14 + #include "WifiCredentialStore.h" 15 + 16 + // ---- CrossPointState ---- 17 + 18 + bool JsonSettingsIO::saveState(const CrossPointState& s, const char* path) { 19 + JsonDocument doc; 20 + doc["openEpubPath"] = s.openEpubPath; 21 + doc["lastSleepImage"] = s.lastSleepImage; 22 + doc["readerActivityLoadCount"] = s.readerActivityLoadCount; 23 + doc["lastSleepFromReader"] = s.lastSleepFromReader; 24 + 25 + String json; 26 + serializeJson(doc, json); 27 + return Storage.writeFile(path, json); 28 + } 29 + 30 + bool JsonSettingsIO::loadState(CrossPointState& s, const char* json) { 31 + JsonDocument doc; 32 + auto error = deserializeJson(doc, json); 33 + if (error) { 34 + LOG_ERR("CPS", "JSON parse error: %s", error.c_str()); 35 + return false; 36 + } 37 + 38 + s.openEpubPath = doc["openEpubPath"] | std::string(""); 39 + s.lastSleepImage = doc["lastSleepImage"] | (uint8_t)0; 40 + s.readerActivityLoadCount = doc["readerActivityLoadCount"] | (uint8_t)0; 41 + s.lastSleepFromReader = doc["lastSleepFromReader"] | false; 42 + return true; 43 + } 44 + 45 + // ---- CrossPointSettings ---- 46 + 47 + bool JsonSettingsIO::saveSettings(const CrossPointSettings& s, const char* path) { 48 + JsonDocument doc; 49 + 50 + doc["sleepScreen"] = s.sleepScreen; 51 + doc["sleepScreenCoverMode"] = s.sleepScreenCoverMode; 52 + doc["sleepScreenCoverFilter"] = s.sleepScreenCoverFilter; 53 + doc["statusBar"] = s.statusBar; 54 + doc["extraParagraphSpacing"] = s.extraParagraphSpacing; 55 + doc["textAntiAliasing"] = s.textAntiAliasing; 56 + doc["shortPwrBtn"] = s.shortPwrBtn; 57 + doc["orientation"] = s.orientation; 58 + doc["sideButtonLayout"] = s.sideButtonLayout; 59 + doc["frontButtonBack"] = s.frontButtonBack; 60 + doc["frontButtonConfirm"] = s.frontButtonConfirm; 61 + doc["frontButtonLeft"] = s.frontButtonLeft; 62 + doc["frontButtonRight"] = s.frontButtonRight; 63 + doc["fontFamily"] = s.fontFamily; 64 + doc["fontSize"] = s.fontSize; 65 + doc["lineSpacing"] = s.lineSpacing; 66 + doc["paragraphAlignment"] = s.paragraphAlignment; 67 + doc["sleepTimeout"] = s.sleepTimeout; 68 + doc["refreshFrequency"] = s.refreshFrequency; 69 + doc["screenMargin"] = s.screenMargin; 70 + doc["opdsServerUrl"] = s.opdsServerUrl; 71 + doc["opdsUsername"] = s.opdsUsername; 72 + doc["opdsPassword_obf"] = obfuscation::obfuscateToBase64(s.opdsPassword); 73 + doc["hideBatteryPercentage"] = s.hideBatteryPercentage; 74 + doc["longPressChapterSkip"] = s.longPressChapterSkip; 75 + doc["hyphenationEnabled"] = s.hyphenationEnabled; 76 + doc["uiTheme"] = s.uiTheme; 77 + doc["fadingFix"] = s.fadingFix; 78 + doc["embeddedStyle"] = s.embeddedStyle; 79 + 80 + String json; 81 + serializeJson(doc, json); 82 + return Storage.writeFile(path, json); 83 + } 84 + 85 + bool JsonSettingsIO::loadSettings(CrossPointSettings& s, const char* json, bool* needsResave) { 86 + if (needsResave) *needsResave = false; 87 + JsonDocument doc; 88 + auto error = deserializeJson(doc, json); 89 + if (error) { 90 + LOG_ERR("CPS", "JSON parse error: %s", error.c_str()); 91 + return false; 92 + } 93 + 94 + using S = CrossPointSettings; 95 + auto clamp = [](uint8_t val, uint8_t maxVal, uint8_t def) -> uint8_t { return val < maxVal ? val : def; }; 96 + 97 + s.sleepScreen = clamp(doc["sleepScreen"] | (uint8_t)S::DARK, S::SLEEP_SCREEN_MODE_COUNT, S::DARK); 98 + s.sleepScreenCoverMode = 99 + clamp(doc["sleepScreenCoverMode"] | (uint8_t)S::FIT, S::SLEEP_SCREEN_COVER_MODE_COUNT, S::FIT); 100 + s.sleepScreenCoverFilter = 101 + clamp(doc["sleepScreenCoverFilter"] | (uint8_t)S::NO_FILTER, S::SLEEP_SCREEN_COVER_FILTER_COUNT, S::NO_FILTER); 102 + s.statusBar = clamp(doc["statusBar"] | (uint8_t)S::FULL, S::STATUS_BAR_MODE_COUNT, S::FULL); 103 + s.extraParagraphSpacing = doc["extraParagraphSpacing"] | (uint8_t)1; 104 + s.textAntiAliasing = doc["textAntiAliasing"] | (uint8_t)1; 105 + s.shortPwrBtn = clamp(doc["shortPwrBtn"] | (uint8_t)S::IGNORE, S::SHORT_PWRBTN_COUNT, S::IGNORE); 106 + s.orientation = clamp(doc["orientation"] | (uint8_t)S::PORTRAIT, S::ORIENTATION_COUNT, S::PORTRAIT); 107 + s.sideButtonLayout = 108 + clamp(doc["sideButtonLayout"] | (uint8_t)S::PREV_NEXT, S::SIDE_BUTTON_LAYOUT_COUNT, S::PREV_NEXT); 109 + s.frontButtonBack = 110 + clamp(doc["frontButtonBack"] | (uint8_t)S::FRONT_HW_BACK, S::FRONT_BUTTON_HARDWARE_COUNT, S::FRONT_HW_BACK); 111 + s.frontButtonConfirm = clamp(doc["frontButtonConfirm"] | (uint8_t)S::FRONT_HW_CONFIRM, S::FRONT_BUTTON_HARDWARE_COUNT, 112 + S::FRONT_HW_CONFIRM); 113 + s.frontButtonLeft = 114 + clamp(doc["frontButtonLeft"] | (uint8_t)S::FRONT_HW_LEFT, S::FRONT_BUTTON_HARDWARE_COUNT, S::FRONT_HW_LEFT); 115 + s.frontButtonRight = 116 + clamp(doc["frontButtonRight"] | (uint8_t)S::FRONT_HW_RIGHT, S::FRONT_BUTTON_HARDWARE_COUNT, S::FRONT_HW_RIGHT); 117 + CrossPointSettings::validateFrontButtonMapping(s); 118 + s.fontFamily = clamp(doc["fontFamily"] | (uint8_t)S::BOOKERLY, S::FONT_FAMILY_COUNT, S::BOOKERLY); 119 + s.fontSize = clamp(doc["fontSize"] | (uint8_t)S::MEDIUM, S::FONT_SIZE_COUNT, S::MEDIUM); 120 + s.lineSpacing = clamp(doc["lineSpacing"] | (uint8_t)S::NORMAL, S::LINE_COMPRESSION_COUNT, S::NORMAL); 121 + s.paragraphAlignment = 122 + clamp(doc["paragraphAlignment"] | (uint8_t)S::JUSTIFIED, S::PARAGRAPH_ALIGNMENT_COUNT, S::JUSTIFIED); 123 + s.sleepTimeout = clamp(doc["sleepTimeout"] | (uint8_t)S::SLEEP_10_MIN, S::SLEEP_TIMEOUT_COUNT, S::SLEEP_10_MIN); 124 + s.refreshFrequency = 125 + clamp(doc["refreshFrequency"] | (uint8_t)S::REFRESH_15, S::REFRESH_FREQUENCY_COUNT, S::REFRESH_15); 126 + s.screenMargin = doc["screenMargin"] | (uint8_t)5; 127 + s.hideBatteryPercentage = 128 + clamp(doc["hideBatteryPercentage"] | (uint8_t)S::HIDE_NEVER, S::HIDE_BATTERY_PERCENTAGE_COUNT, S::HIDE_NEVER); 129 + s.longPressChapterSkip = doc["longPressChapterSkip"] | (uint8_t)1; 130 + s.hyphenationEnabled = doc["hyphenationEnabled"] | (uint8_t)0; 131 + s.uiTheme = doc["uiTheme"] | (uint8_t)S::LYRA; 132 + s.fadingFix = doc["fadingFix"] | (uint8_t)0; 133 + s.embeddedStyle = doc["embeddedStyle"] | (uint8_t)1; 134 + 135 + const char* url = doc["opdsServerUrl"] | ""; 136 + strncpy(s.opdsServerUrl, url, sizeof(s.opdsServerUrl) - 1); 137 + s.opdsServerUrl[sizeof(s.opdsServerUrl) - 1] = '\0'; 138 + 139 + const char* user = doc["opdsUsername"] | ""; 140 + strncpy(s.opdsUsername, user, sizeof(s.opdsUsername) - 1); 141 + s.opdsUsername[sizeof(s.opdsUsername) - 1] = '\0'; 142 + 143 + bool passOk = false; 144 + std::string pass = obfuscation::deobfuscateFromBase64(doc["opdsPassword_obf"] | "", &passOk); 145 + if (!passOk || pass.empty()) { 146 + pass = doc["opdsPassword"] | ""; 147 + if (!pass.empty() && needsResave) *needsResave = true; 148 + } 149 + strncpy(s.opdsPassword, pass.c_str(), sizeof(s.opdsPassword) - 1); 150 + s.opdsPassword[sizeof(s.opdsPassword) - 1] = '\0'; 151 + LOG_DBG("CPS", "Settings loaded from file"); 152 + return true; 153 + } 154 + 155 + // ---- KOReaderCredentialStore ---- 156 + 157 + bool JsonSettingsIO::saveKOReader(const KOReaderCredentialStore& store, const char* path) { 158 + JsonDocument doc; 159 + doc["username"] = store.getUsername(); 160 + doc["password_obf"] = obfuscation::obfuscateToBase64(store.getPassword()); 161 + doc["serverUrl"] = store.getServerUrl(); 162 + doc["matchMethod"] = static_cast<uint8_t>(store.getMatchMethod()); 163 + 164 + String json; 165 + serializeJson(doc, json); 166 + return Storage.writeFile(path, json); 167 + } 168 + 169 + bool JsonSettingsIO::loadKOReader(KOReaderCredentialStore& store, const char* json, bool* needsResave) { 170 + if (needsResave) *needsResave = false; 171 + JsonDocument doc; 172 + auto error = deserializeJson(doc, json); 173 + if (error) { 174 + LOG_ERR("KRS", "JSON parse error: %s", error.c_str()); 175 + return false; 176 + } 177 + 178 + store.username = doc["username"] | std::string(""); 179 + bool ok = false; 180 + store.password = obfuscation::deobfuscateFromBase64(doc["password_obf"] | "", &ok); 181 + if (!ok || store.password.empty()) { 182 + store.password = doc["password"] | std::string(""); 183 + if (!store.password.empty() && needsResave) *needsResave = true; 184 + } 185 + store.serverUrl = doc["serverUrl"] | std::string(""); 186 + uint8_t method = doc["matchMethod"] | (uint8_t)0; 187 + store.matchMethod = static_cast<DocumentMatchMethod>(method); 188 + 189 + LOG_DBG("KRS", "Loaded KOReader credentials for user: %s", store.username.c_str()); 190 + return true; 191 + } 192 + 193 + // ---- WifiCredentialStore ---- 194 + 195 + bool JsonSettingsIO::saveWifi(const WifiCredentialStore& store, const char* path) { 196 + JsonDocument doc; 197 + doc["lastConnectedSsid"] = store.getLastConnectedSsid(); 198 + 199 + JsonArray arr = doc["credentials"].to<JsonArray>(); 200 + for (const auto& cred : store.getCredentials()) { 201 + JsonObject obj = arr.add<JsonObject>(); 202 + obj["ssid"] = cred.ssid; 203 + obj["password_obf"] = obfuscation::obfuscateToBase64(cred.password); 204 + } 205 + 206 + String json; 207 + serializeJson(doc, json); 208 + return Storage.writeFile(path, json); 209 + } 210 + 211 + bool JsonSettingsIO::loadWifi(WifiCredentialStore& store, const char* json, bool* needsResave) { 212 + if (needsResave) *needsResave = false; 213 + JsonDocument doc; 214 + auto error = deserializeJson(doc, json); 215 + if (error) { 216 + LOG_ERR("WCS", "JSON parse error: %s", error.c_str()); 217 + return false; 218 + } 219 + 220 + store.lastConnectedSsid = doc["lastConnectedSsid"] | std::string(""); 221 + 222 + store.credentials.clear(); 223 + JsonArray arr = doc["credentials"].as<JsonArray>(); 224 + for (JsonObject obj : arr) { 225 + if (store.credentials.size() >= store.MAX_NETWORKS) break; 226 + WifiCredential cred; 227 + cred.ssid = obj["ssid"] | std::string(""); 228 + bool ok = false; 229 + cred.password = obfuscation::deobfuscateFromBase64(obj["password_obf"] | "", &ok); 230 + if (!ok || cred.password.empty()) { 231 + cred.password = obj["password"] | std::string(""); 232 + if (!cred.password.empty() && needsResave) *needsResave = true; 233 + } 234 + store.credentials.push_back(cred); 235 + } 236 + 237 + LOG_DBG("WCS", "Loaded %zu WiFi credentials from file", store.credentials.size()); 238 + return true; 239 + } 240 + 241 + // ---- RecentBooksStore ---- 242 + 243 + bool JsonSettingsIO::saveRecentBooks(const RecentBooksStore& store, const char* path) { 244 + JsonDocument doc; 245 + JsonArray arr = doc["books"].to<JsonArray>(); 246 + for (const auto& book : store.getBooks()) { 247 + JsonObject obj = arr.add<JsonObject>(); 248 + obj["path"] = book.path; 249 + obj["title"] = book.title; 250 + obj["author"] = book.author; 251 + obj["coverBmpPath"] = book.coverBmpPath; 252 + } 253 + 254 + String json; 255 + serializeJson(doc, json); 256 + return Storage.writeFile(path, json); 257 + } 258 + 259 + bool JsonSettingsIO::loadRecentBooks(RecentBooksStore& store, const char* json) { 260 + JsonDocument doc; 261 + auto error = deserializeJson(doc, json); 262 + if (error) { 263 + LOG_ERR("RBS", "JSON parse error: %s", error.c_str()); 264 + return false; 265 + } 266 + 267 + store.recentBooks.clear(); 268 + JsonArray arr = doc["books"].as<JsonArray>(); 269 + for (JsonObject obj : arr) { 270 + if (store.getCount() >= 10) break; 271 + RecentBook book; 272 + book.path = obj["path"] | std::string(""); 273 + book.title = obj["title"] | std::string(""); 274 + book.author = obj["author"] | std::string(""); 275 + book.coverBmpPath = obj["coverBmpPath"] | std::string(""); 276 + store.recentBooks.push_back(book); 277 + } 278 + 279 + LOG_DBG("RBS", "Recent books loaded from file (%d entries)", store.getCount()); 280 + return true; 281 + }
+31
src/JsonSettingsIO.h
··· 1 + #pragma once 2 + 3 + class CrossPointSettings; 4 + class CrossPointState; 5 + class WifiCredentialStore; 6 + class KOReaderCredentialStore; 7 + class RecentBooksStore; 8 + 9 + namespace JsonSettingsIO { 10 + 11 + // CrossPointSettings 12 + bool saveSettings(const CrossPointSettings& s, const char* path); 13 + bool loadSettings(CrossPointSettings& s, const char* json, bool* needsResave = nullptr); 14 + 15 + // CrossPointState 16 + bool saveState(const CrossPointState& s, const char* path); 17 + bool loadState(CrossPointState& s, const char* json); 18 + 19 + // WifiCredentialStore 20 + bool saveWifi(const WifiCredentialStore& store, const char* path); 21 + bool loadWifi(WifiCredentialStore& store, const char* json, bool* needsResave = nullptr); 22 + 23 + // KOReaderCredentialStore 24 + bool saveKOReader(const KOReaderCredentialStore& store, const char* path); 25 + bool loadKOReader(KOReaderCredentialStore& store, const char* json, bool* needsResave = nullptr); 26 + 27 + // RecentBooksStore 28 + bool saveRecentBooks(const RecentBooksStore& store, const char* path); 29 + bool loadRecentBooks(RecentBooksStore& store, const char* json); 30 + 31 + } // namespace JsonSettingsIO
+29 -24
src/RecentBooksStore.cpp
··· 2 2 3 3 #include <Epub.h> 4 4 #include <HalStorage.h> 5 + #include <JsonSettingsIO.h> 5 6 #include <Logging.h> 6 7 #include <Serialization.h> 7 8 #include <Xtc.h> ··· 12 13 13 14 namespace { 14 15 constexpr uint8_t RECENT_BOOKS_FILE_VERSION = 3; 15 - constexpr char RECENT_BOOKS_FILE[] = "/.crosspoint/recent.bin"; 16 + constexpr char RECENT_BOOKS_FILE_BIN[] = "/.crosspoint/recent.bin"; 17 + constexpr char RECENT_BOOKS_FILE_JSON[] = "/.crosspoint/recent.json"; 18 + constexpr char RECENT_BOOKS_FILE_BAK[] = "/.crosspoint/recent.bin.bak"; 16 19 constexpr int MAX_RECENT_BOOKS = 10; 17 20 } // namespace 18 21 ··· 52 55 } 53 56 54 57 bool RecentBooksStore::saveToFile() const { 55 - // Make sure the directory exists 56 58 Storage.mkdir("/.crosspoint"); 57 - 58 - FsFile outputFile; 59 - if (!Storage.openFileForWrite("RBS", RECENT_BOOKS_FILE, outputFile)) { 60 - return false; 61 - } 62 - 63 - serialization::writePod(outputFile, RECENT_BOOKS_FILE_VERSION); 64 - const uint8_t count = static_cast<uint8_t>(recentBooks.size()); 65 - serialization::writePod(outputFile, count); 66 - 67 - for (const auto& book : recentBooks) { 68 - serialization::writeString(outputFile, book.path); 69 - serialization::writeString(outputFile, book.title); 70 - serialization::writeString(outputFile, book.author); 71 - serialization::writeString(outputFile, book.coverBmpPath); 72 - } 73 - 74 - outputFile.close(); 75 - LOG_DBG("RBS", "Recent books saved to file (%d entries)", count); 76 - return true; 59 + return JsonSettingsIO::saveRecentBooks(*this, RECENT_BOOKS_FILE_JSON); 77 60 } 78 61 79 62 RecentBook RecentBooksStore::getDataFromBook(std::string path) const { ··· 107 90 } 108 91 109 92 bool RecentBooksStore::loadFromFile() { 93 + // Try JSON first 94 + if (Storage.exists(RECENT_BOOKS_FILE_JSON)) { 95 + String json = Storage.readFile(RECENT_BOOKS_FILE_JSON); 96 + if (!json.isEmpty()) { 97 + return JsonSettingsIO::loadRecentBooks(*this, json.c_str()); 98 + } 99 + } 100 + 101 + // Fall back to binary migration 102 + if (Storage.exists(RECENT_BOOKS_FILE_BIN)) { 103 + if (loadFromBinaryFile()) { 104 + saveToFile(); 105 + Storage.rename(RECENT_BOOKS_FILE_BIN, RECENT_BOOKS_FILE_BAK); 106 + LOG_DBG("RBS", "Migrated recent.bin to recent.json"); 107 + return true; 108 + } 109 + } 110 + 111 + return false; 112 + } 113 + 114 + bool RecentBooksStore::loadFromBinaryFile() { 110 115 FsFile inputFile; 111 - if (!Storage.openFileForRead("RBS", RECENT_BOOKS_FILE, inputFile)) { 116 + if (!Storage.openFileForRead("RBS", RECENT_BOOKS_FILE_BIN, inputFile)) { 112 117 return false; 113 118 } 114 119 ··· 173 178 } 174 179 175 180 inputFile.close(); 176 - LOG_DBG("RBS", "Recent books loaded from file (%d entries)", recentBooks.size()); 181 + LOG_DBG("RBS", "Recent books loaded from binary file (%d entries)", static_cast<int>(recentBooks.size())); 177 182 return true; 178 183 }
+10
src/RecentBooksStore.h
··· 11 11 bool operator==(const RecentBook& other) const { return path == other.path; } 12 12 }; 13 13 14 + class RecentBooksStore; 15 + namespace JsonSettingsIO { 16 + bool loadRecentBooks(RecentBooksStore& store, const char* json); 17 + } // namespace JsonSettingsIO 18 + 14 19 class RecentBooksStore { 15 20 // Static instance 16 21 static RecentBooksStore instance; 17 22 18 23 std::vector<RecentBook> recentBooks; 24 + 25 + friend bool JsonSettingsIO::loadRecentBooks(RecentBooksStore&, const char*); 19 26 20 27 public: 21 28 ~RecentBooksStore() = default; ··· 40 47 41 48 bool loadFromFile(); 42 49 RecentBook getDataFromBook(std::string path) const; 50 + 51 + private: 52 + bool loadFromBinaryFile(); 43 53 }; 44 54 45 55 // Helper macro to access recent books store
+46 -48
src/WifiCredentialStore.cpp
··· 1 1 #include "WifiCredentialStore.h" 2 2 3 3 #include <HalStorage.h> 4 + #include <JsonSettingsIO.h> 4 5 #include <Logging.h> 6 + #include <ObfuscationUtils.h> 5 7 #include <Serialization.h> 6 8 7 9 // Initialize the static instance 8 10 WifiCredentialStore WifiCredentialStore::instance; 9 11 10 12 namespace { 11 - // File format version 12 - constexpr uint8_t WIFI_FILE_VERSION = 2; // Increased version 13 + // File format version (for binary migration) 14 + constexpr uint8_t WIFI_FILE_VERSION = 2; 13 15 14 - // WiFi credentials file path 15 - constexpr char WIFI_FILE[] = "/.crosspoint/wifi.bin"; 16 + // File paths 17 + constexpr char WIFI_FILE_BIN[] = "/.crosspoint/wifi.bin"; 18 + constexpr char WIFI_FILE_JSON[] = "/.crosspoint/wifi.json"; 19 + constexpr char WIFI_FILE_BAK[] = "/.crosspoint/wifi.bin.bak"; 16 20 17 - // Obfuscation key - "CrossPoint" in ASCII 18 - // This is NOT cryptographic security, just prevents casual file reading 19 - constexpr uint8_t OBFUSCATION_KEY[] = {0x43, 0x72, 0x6F, 0x73, 0x73, 0x50, 0x6F, 0x69, 0x6E, 0x74}; 20 - constexpr size_t KEY_LENGTH = sizeof(OBFUSCATION_KEY); 21 - } // namespace 21 + // Legacy obfuscation key - "CrossPoint" in ASCII (only used for binary migration) 22 + constexpr uint8_t LEGACY_OBFUSCATION_KEY[] = {0x43, 0x72, 0x6F, 0x73, 0x73, 0x50, 0x6F, 0x69, 0x6E, 0x74}; 23 + constexpr size_t LEGACY_KEY_LENGTH = sizeof(LEGACY_OBFUSCATION_KEY); 22 24 23 - void WifiCredentialStore::obfuscate(std::string& data) const { 24 - LOG_DBG("WCS", "Obfuscating/deobfuscating %zu bytes", data.size()); 25 + void legacyDeobfuscate(std::string& data) { 25 26 for (size_t i = 0; i < data.size(); i++) { 26 - data[i] ^= OBFUSCATION_KEY[i % KEY_LENGTH]; 27 + data[i] ^= LEGACY_OBFUSCATION_KEY[i % LEGACY_KEY_LENGTH]; 27 28 } 28 29 } 30 + } // namespace 29 31 30 32 bool WifiCredentialStore::saveToFile() const { 31 - // Make sure the directory exists 32 33 Storage.mkdir("/.crosspoint"); 34 + return JsonSettingsIO::saveWifi(*this, WIFI_FILE_JSON); 35 + } 33 36 34 - FsFile file; 35 - if (!Storage.openFileForWrite("WCS", WIFI_FILE, file)) { 36 - return false; 37 + bool WifiCredentialStore::loadFromFile() { 38 + // Try JSON first 39 + if (Storage.exists(WIFI_FILE_JSON)) { 40 + String json = Storage.readFile(WIFI_FILE_JSON); 41 + if (!json.isEmpty()) { 42 + bool resave = false; 43 + bool result = JsonSettingsIO::loadWifi(*this, json.c_str(), &resave); 44 + if (result && resave) { 45 + LOG_DBG("WCS", "Resaving JSON with obfuscated passwords"); 46 + saveToFile(); 47 + } 48 + return result; 49 + } 37 50 } 38 51 39 - // Write header 40 - serialization::writePod(file, WIFI_FILE_VERSION); 41 - serialization::writeString(file, lastConnectedSsid); // Save last connected SSID 42 - serialization::writePod(file, static_cast<uint8_t>(credentials.size())); 43 - 44 - // Write each credential 45 - for (const auto& cred : credentials) { 46 - // Write SSID (plaintext - not sensitive) 47 - serialization::writeString(file, cred.ssid); 48 - LOG_DBG("WCS", "Saving SSID: %s, password length: %zu", cred.ssid.c_str(), cred.password.size()); 49 - 50 - // Write password (obfuscated) 51 - std::string obfuscatedPwd = cred.password; 52 - obfuscate(obfuscatedPwd); 53 - serialization::writeString(file, obfuscatedPwd); 52 + // Fall back to binary migration 53 + if (Storage.exists(WIFI_FILE_BIN)) { 54 + if (loadFromBinaryFile()) { 55 + if (saveToFile()) { 56 + Storage.rename(WIFI_FILE_BIN, WIFI_FILE_BAK); 57 + LOG_DBG("WCS", "Migrated wifi.bin to wifi.json"); 58 + return true; 59 + } else { 60 + LOG_ERR("WCS", "Failed to save wifi during migration"); 61 + return false; 62 + } 63 + } 54 64 } 55 65 56 - file.close(); 57 - LOG_DBG("WCS", "Saved %zu WiFi credentials to file", credentials.size()); 58 - return true; 66 + return false; 59 67 } 60 68 61 - bool WifiCredentialStore::loadFromFile() { 69 + bool WifiCredentialStore::loadFromBinaryFile() { 62 70 FsFile file; 63 - if (!Storage.openFileForRead("WCS", WIFI_FILE, file)) { 71 + if (!Storage.openFileForRead("WCS", WIFI_FILE_BIN, file)) { 64 72 return false; 65 73 } 66 74 67 - // Read and verify version 68 75 uint8_t version; 69 76 serialization::readPod(file, version); 70 77 if (version > WIFI_FILE_VERSION) { ··· 79 86 lastConnectedSsid.clear(); 80 87 } 81 88 82 - // Read credential count 83 89 uint8_t count; 84 90 serialization::readPod(file, count); 85 91 86 - // Read credentials 87 92 credentials.clear(); 88 93 for (uint8_t i = 0; i < count && i < MAX_NETWORKS; i++) { 89 94 WifiCredential cred; 90 - 91 - // Read SSID 92 95 serialization::readString(file, cred.ssid); 93 - 94 - // Read and deobfuscate password 95 96 serialization::readString(file, cred.password); 96 - LOG_DBG("WCS", "Loaded SSID: %s, obfuscated password length: %zu", cred.ssid.c_str(), cred.password.size()); 97 - obfuscate(cred.password); // XOR is symmetric, so same function deobfuscates 98 - LOG_DBG("WCS", "After deobfuscation, password length: %zu", cred.password.size()); 99 - 97 + legacyDeobfuscate(cred.password); 100 98 credentials.push_back(cred); 101 99 } 102 100 103 101 file.close(); 104 - LOG_DBG("WCS", "Loaded %zu WiFi credentials from file", credentials.size()); 102 + // LOG_DBG("WCS", "Loaded %zu WiFi credentials from binary file", credentials.size()); 105 103 return true; 106 104 } 107 105
+14 -5
src/WifiCredentialStore.h
··· 4 4 5 5 struct WifiCredential { 6 6 std::string ssid; 7 - std::string password; // Stored obfuscated in file 7 + std::string password; // Plaintext in memory; obfuscated with hardware key on disk 8 8 }; 9 9 10 + class WifiCredentialStore; 11 + namespace JsonSettingsIO { 12 + bool saveWifi(const WifiCredentialStore& store, const char* path); 13 + bool loadWifi(WifiCredentialStore& store, const char* json, bool* needsResave); 14 + } // namespace JsonSettingsIO 15 + 10 16 /** 11 17 * Singleton class for storing WiFi credentials on the SD card. 12 - * Credentials are stored in /sd/.crosspoint/wifi.bin with basic 13 - * XOR obfuscation to prevent casual reading (not cryptographically secure). 18 + * Passwords are XOR-obfuscated with the device's unique hardware MAC address 19 + * and base64-encoded before writing to JSON (not cryptographically secure, 20 + * but prevents casual reading and ties credentials to the specific device). 14 21 */ 15 22 class WifiCredentialStore { 16 23 private: ··· 23 30 // Private constructor for singleton 24 31 WifiCredentialStore() = default; 25 32 26 - // XOR obfuscation (symmetric - same for encode/decode) 27 - void obfuscate(std::string& data) const; 33 + bool loadFromBinaryFile(); 34 + 35 + friend bool JsonSettingsIO::saveWifi(const WifiCredentialStore&, const char*); 36 + friend bool JsonSettingsIO::loadWifi(WifiCredentialStore&, const char*, bool*); 28 37 29 38 public: 30 39 // Delete copy constructor and assignment