fork/GramFirmware

git clone https://git.julia.blue/fork/GramFirmware

CarVacrebase to configurator818d962

gram
4.7 KiB183 linesraw
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() {
29    LittleFS.begin();
30}
31
32Persistence::~Persistence() {
33    LittleFS.end();
34}
35
36bool Persistence::SaveConfig(Config &config) {
37    // Make sure config encodes correctly.
38    size_t encoded_size;
39    if (!pb_get_encoded_size(&encoded_size, Config_fields, &config)) {
40        return false;
41    }
42
43    // Open file to store config data in.
44    File config_file = LittleFS.open(config_filename, "w+");
45    if (!config_file) {
46        return false;
47    }
48
49    // Write empty header to start with.
50    ConfigHeader header = { .config_size = 0, .config_crc = 0 };
51    config_file.write((uint8_t *)&header, sizeof(ConfigHeader));
52
53    // Encode Protobuf data directly into file body.
54    pb_ostream_t ostream = as_pb_ostream(config_file);
55    if (!pb_encode(&ostream, Config_fields, &config)) {
56        config_file.close();
57        return false;
58    }
59
60    // Calculate checksum.
61    config_file.seek(config_offset);
62    CRC32 crc;
63    int value;
64    while ((value = config_file.read()) != -1) {
65        crc.update((uint8_t)value);
66    }
67
68    // Update header.
69    header.config_size = ostream.bytes_written;
70    header.config_crc = crc.finalize();
71    config_file.seek(0);
72    config_file.write((uint8_t *)&header, sizeof(ConfigHeader));
73
74    // Persist changes.
75    config_file.close();
76
77    return true;
78}
79
80bool Persistence::LoadConfig(Config &config) {
81    // Open file to load config data from.
82    File config_file = LittleFS.open(config_filename, "r");
83    if (!config_file) {
84        return false;
85    }
86
87    if (!CheckSavedConfig(config_file)) {
88        config_file.close();
89        return false;
90    }
91
92    // Seek to start of Protobuf data.
93    if (!config_file.seek(config_offset)) {
94        config_file.close();
95        return false;
96    }
97
98    // Reset config defaults first, so config is completely replaced rather than merged with
99    // defaults.
100    config = Config_init_default;
101
102    // Decode streamed Protobuf data into config struct.
103    pb_istream_t istream = as_pb_istream(config_file, (size_t)config_file.available());
104    if (!pb_decode(&istream, Config_fields, &config)) {
105        config_file.close();
106        return false;
107    }
108
109    config_file.close();
110    return true;
111}
112
113bool Persistence::CheckSavedConfig() {
114    // Open file to load config data from.
115    File config_file = LittleFS.open(config_filename, "r");
116    if (!config_file) {
117        return false;
118    }
119
120    bool is_valid = CheckSavedConfig(config_file);
121    config_file.close();
122    return is_valid;
123}
124
125size_t Persistence::LoadConfigRaw(Print &out, bool validate) {
126    // Open file to load config data from.
127    File config_file = LittleFS.open(config_filename, "r");
128    if (!config_file) {
129        return false;
130    }
131
132    // Optionally perform validation.
133    if (validate && !CheckSavedConfig(config_file)) {
134        config_file.close();
135        return false;
136    }
137
138    // Seek to start of Protobuf data.
139    if (!config_file.seek(config_offset)) {
140        config_file.close();
141        return false;
142    }
143
144    // Write raw Protobuf encoded data to output stream.
145    int value;
146    while ((value = config_file.read()) != -1) {
147        out.write((uint8_t)value);
148    }
149
150    config_file.close();
151    return true;
152}
153
154bool Persistence::CheckSavedConfig(File &config_file) {
155    size_t file_size = config_file.size();
156
157    // Read file header.
158    ConfigHeader header;
159    size_t bytes_read = config_file.read((uint8_t *)&header, sizeof(ConfigHeader));
160    if (bytes_read < sizeof(ConfigHeader)) {
161        return false;
162    }
163
164    // Validate config length.
165    size_t config_size = file_size - config_offset;
166    if (config_size != header.config_size) {
167        return false;
168    }
169
170    // Calculate CRC for file contents and compare with CRC in header.
171    CRC32 crc;
172    int value;
173    while ((value = config_file.read()) != -1) {
174        crc.update((uint8_t)value);
175    }
176    if (crc.finalize() != header.config_crc) {
177        return false;
178    }
179
180    return true;
181}
182
183Persistence persistence;