## 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>
177 lines
5.1 KiB
C++
177 lines
5.1 KiB
C++
#include "WifiCredentialStore.h"
|
|
|
|
#include <HalStorage.h>
|
|
#include <JsonSettingsIO.h>
|
|
#include <Logging.h>
|
|
#include <ObfuscationUtils.h>
|
|
#include <Serialization.h>
|
|
|
|
// Initialize the static instance
|
|
WifiCredentialStore WifiCredentialStore::instance;
|
|
|
|
namespace {
|
|
// File format version (for binary migration)
|
|
constexpr uint8_t WIFI_FILE_VERSION = 2;
|
|
|
|
// File paths
|
|
constexpr char WIFI_FILE_BIN[] = "/.crosspoint/wifi.bin";
|
|
constexpr char WIFI_FILE_JSON[] = "/.crosspoint/wifi.json";
|
|
constexpr char WIFI_FILE_BAK[] = "/.crosspoint/wifi.bin.bak";
|
|
|
|
// Legacy obfuscation key - "CrossPoint" in ASCII (only used for binary migration)
|
|
constexpr uint8_t LEGACY_OBFUSCATION_KEY[] = {0x43, 0x72, 0x6F, 0x73, 0x73, 0x50, 0x6F, 0x69, 0x6E, 0x74};
|
|
constexpr size_t LEGACY_KEY_LENGTH = sizeof(LEGACY_OBFUSCATION_KEY);
|
|
|
|
void legacyDeobfuscate(std::string& data) {
|
|
for (size_t i = 0; i < data.size(); i++) {
|
|
data[i] ^= LEGACY_OBFUSCATION_KEY[i % LEGACY_KEY_LENGTH];
|
|
}
|
|
}
|
|
} // namespace
|
|
|
|
bool WifiCredentialStore::saveToFile() const {
|
|
Storage.mkdir("/.crosspoint");
|
|
return JsonSettingsIO::saveWifi(*this, WIFI_FILE_JSON);
|
|
}
|
|
|
|
bool WifiCredentialStore::loadFromFile() {
|
|
// Try JSON first
|
|
if (Storage.exists(WIFI_FILE_JSON)) {
|
|
String json = Storage.readFile(WIFI_FILE_JSON);
|
|
if (!json.isEmpty()) {
|
|
bool resave = false;
|
|
bool result = JsonSettingsIO::loadWifi(*this, json.c_str(), &resave);
|
|
if (result && resave) {
|
|
LOG_DBG("WCS", "Resaving JSON with obfuscated passwords");
|
|
saveToFile();
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
// Fall back to binary migration
|
|
if (Storage.exists(WIFI_FILE_BIN)) {
|
|
if (loadFromBinaryFile()) {
|
|
if (saveToFile()) {
|
|
Storage.rename(WIFI_FILE_BIN, WIFI_FILE_BAK);
|
|
LOG_DBG("WCS", "Migrated wifi.bin to wifi.json");
|
|
return true;
|
|
} else {
|
|
LOG_ERR("WCS", "Failed to save wifi during migration");
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool WifiCredentialStore::loadFromBinaryFile() {
|
|
FsFile file;
|
|
if (!Storage.openFileForRead("WCS", WIFI_FILE_BIN, file)) {
|
|
return false;
|
|
}
|
|
|
|
uint8_t version;
|
|
serialization::readPod(file, version);
|
|
if (version > WIFI_FILE_VERSION) {
|
|
LOG_DBG("WCS", "Unknown file version: %u", version);
|
|
file.close();
|
|
return false;
|
|
}
|
|
|
|
if (version >= 2) {
|
|
serialization::readString(file, lastConnectedSsid);
|
|
} else {
|
|
lastConnectedSsid.clear();
|
|
}
|
|
|
|
uint8_t count;
|
|
serialization::readPod(file, count);
|
|
|
|
credentials.clear();
|
|
for (uint8_t i = 0; i < count && i < MAX_NETWORKS; i++) {
|
|
WifiCredential cred;
|
|
serialization::readString(file, cred.ssid);
|
|
serialization::readString(file, cred.password);
|
|
legacyDeobfuscate(cred.password);
|
|
credentials.push_back(cred);
|
|
}
|
|
|
|
file.close();
|
|
// LOG_DBG("WCS", "Loaded %zu WiFi credentials from binary file", credentials.size());
|
|
return true;
|
|
}
|
|
|
|
bool WifiCredentialStore::addCredential(const std::string& ssid, const std::string& password) {
|
|
// Check if this SSID already exists and update it
|
|
const auto cred = find_if(credentials.begin(), credentials.end(),
|
|
[&ssid](const WifiCredential& cred) { return cred.ssid == ssid; });
|
|
if (cred != credentials.end()) {
|
|
cred->password = password;
|
|
LOG_DBG("WCS", "Updated credentials for: %s", ssid.c_str());
|
|
return saveToFile();
|
|
}
|
|
|
|
// Check if we've reached the limit
|
|
if (credentials.size() >= MAX_NETWORKS) {
|
|
LOG_DBG("WCS", "Cannot add more networks, limit of %zu reached", MAX_NETWORKS);
|
|
return false;
|
|
}
|
|
|
|
// Add new credential
|
|
credentials.push_back({ssid, password});
|
|
LOG_DBG("WCS", "Added credentials for: %s", ssid.c_str());
|
|
return saveToFile();
|
|
}
|
|
|
|
bool WifiCredentialStore::removeCredential(const std::string& ssid) {
|
|
const auto cred = find_if(credentials.begin(), credentials.end(),
|
|
[&ssid](const WifiCredential& cred) { return cred.ssid == ssid; });
|
|
if (cred != credentials.end()) {
|
|
credentials.erase(cred);
|
|
LOG_DBG("WCS", "Removed credentials for: %s", ssid.c_str());
|
|
if (ssid == lastConnectedSsid) {
|
|
clearLastConnectedSsid();
|
|
}
|
|
return saveToFile();
|
|
}
|
|
return false; // Not found
|
|
}
|
|
|
|
const WifiCredential* WifiCredentialStore::findCredential(const std::string& ssid) const {
|
|
const auto cred = find_if(credentials.begin(), credentials.end(),
|
|
[&ssid](const WifiCredential& cred) { return cred.ssid == ssid; });
|
|
|
|
if (cred != credentials.end()) {
|
|
return &*cred;
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
bool WifiCredentialStore::hasSavedCredential(const std::string& ssid) const { return findCredential(ssid) != nullptr; }
|
|
|
|
void WifiCredentialStore::setLastConnectedSsid(const std::string& ssid) {
|
|
if (lastConnectedSsid != ssid) {
|
|
lastConnectedSsid = ssid;
|
|
saveToFile();
|
|
}
|
|
}
|
|
|
|
const std::string& WifiCredentialStore::getLastConnectedSsid() const { return lastConnectedSsid; }
|
|
|
|
void WifiCredentialStore::clearLastConnectedSsid() {
|
|
if (!lastConnectedSsid.empty()) {
|
|
lastConnectedSsid.clear();
|
|
saveToFile();
|
|
}
|
|
}
|
|
|
|
void WifiCredentialStore::clearAll() {
|
|
credentials.clear();
|
|
lastConnectedSsid.clear();
|
|
saveToFile();
|
|
LOG_DBG("WCS", "Cleared all WiFi credentials");
|
|
}
|