fork/GramFirmware
git clone https://git.julia.blue/fork/GramFirmware
818d962
gram
1/* 2* This file is part of HayBox 3* Copyright (C) 2024 Jonathan Haylett 4* 5* HayBox is free software: you can redistribute it and/or modify 6* it under the terms of the GNU General Public License version 3 as 7* published by the Free Software Foundation. 8* 9* This software is distributed in the hope that it will be useful, 10* but WITHOUT ANY WARRANTY; without even the implied warranty of 11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12* GNU General Public License for more details. 13* 14* You should have received a copy of the GNU General Public License 15* along with this software. If not, see <http://www.gnu.org/licenses/>. 16*/ 17 18#include "core/Persistence.hpp" 19 20#include "stdlib.hpp" 21 22#include <CRC32.h> 23#include <LittleFS.h> 24#include <pb_arduino.h> 25#include <pb_decode.h> 26#include <pb_encode.h> 27 28Persistence ::Persistence () { 29LittleFS .begin (); 30} 31 32Persistence ::~Persistence () { 33LittleFS .end (); 34} 35 36bool Persistence ::SaveConfig (Config & config ) { 37// Make sure config encodes correctly. 38size_t encoded_size ; 39if (!pb_get_encoded_size (& encoded_size ,Config_fields ,& config )) { 40return false; 41 } 42 43// Open file to store config data in. 44File config_file = LittleFS .open (config_filename ,"w+" ); 45if (!config_file ) { 46return false; 47 } 48 49// Write empty header to start with. 50ConfigHeader header = { .config_size = 0 , .config_crc = 0 }; 51config_file .write ((uint8_t * )& header ,sizeof (ConfigHeader )); 52 53// Encode Protobuf data directly into file body. 54pb_ostream_t ostream = as_pb_ostream (config_file ); 55if (!pb_encode (& ostream ,Config_fields ,& config )) { 56config_file .close (); 57return false; 58 } 59 60// Calculate checksum. 61config_file .seek (config_offset ); 62CRC32 crc ; 63int value ; 64while ((value = config_file .read ())!= -1 ) { 65crc .update ((uint8_t )value ); 66 } 67 68// Update header. 69header .config_size = ostream .bytes_written ; 70header .config_crc = crc .finalize (); 71config_file .seek (0 ); 72config_file .write ((uint8_t * )& header ,sizeof (ConfigHeader )); 73 74// Persist changes. 75config_file .close (); 76 77return true; 78} 79 80bool Persistence ::LoadConfig (Config & config ) { 81// Open file to load config data from. 82File config_file = LittleFS .open (config_filename ,"r" ); 83if (!config_file ) { 84return false; 85 } 86 87if (!CheckSavedConfig (config_file )) { 88config_file .close (); 89return false; 90 } 91 92// Seek to start of Protobuf data. 93if (!config_file .seek (config_offset )) { 94config_file .close (); 95return false; 96 } 97 98// Reset config defaults first, so config is completely replaced rather than merged with 99// defaults. 100config = Config_init_default ; 101 102// Decode streamed Protobuf data into config struct. 103pb_istream_t istream = as_pb_istream (config_file , (size_t )config_file .available ()); 104if (!pb_decode (& istream ,Config_fields ,& config )) { 105config_file .close (); 106return false; 107 } 108 109config_file .close (); 110return true; 111} 112 113bool Persistence ::CheckSavedConfig () { 114// Open file to load config data from. 115File config_file = LittleFS .open (config_filename ,"r" ); 116if (!config_file ) { 117return false; 118 } 119 120bool is_valid = CheckSavedConfig (config_file ); 121config_file .close (); 122return is_valid ; 123} 124 125size_t Persistence ::LoadConfigRaw (& out ,bool validate ) { 126// Open file to load config data from. 127File config_file = LittleFS .open (config_filename ,"r" ); 128if (!config_file ) { 129return false; 130 } 131 132// Optionally perform validation. 133if (validate && !CheckSavedConfig (config_file )) { 134config_file .close (); 135return false; 136 } 137 138// Seek to start of Protobuf data. 139if (!config_file .seek (config_offset )) { 140config_file .close (); 141return false; 142 } 143 144// Write raw Protobuf encoded data to output stream. 145int value ; 146while ((value = config_file .read ())!= -1 ) { 147out .write ((uint8_t )value ); 148 } 149 150config_file .close (); 151return true; 152} 153 154bool Persistence ::CheckSavedConfig (File & config_file ) { 155size_t file_size = config_file .size (); 156 157// Read file header. 158ConfigHeader header ; 159size_t bytes_read = config_file .read ((uint8_t * )& header ,sizeof (ConfigHeader )); 160if (bytes_read < sizeof (ConfigHeader )) { 161return false; 162 } 163 164// Validate config length. 165size_t config_size = file_size - config_offset ; 166if (config_size != header .config_size ) { 167return false; 168 } 169 170// Calculate CRC for file contents and compare with CRC in header. 171CRC32 crc ; 172int value ; 173while ((value = config_file .read ())!= -1 ) { 174crc .update ((uint8_t )value ); 175 } 176if (crc .finalize ()!= header .config_crc ) { 177return false; 178 } 179 180return true; 181} 182 183Persistence persistence ;