1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
#include "saved_struct.h"
#include <furi.h>
#include <stdint.h>
#include <storage/storage.h>
#define TAG "SavedStruct"
typedef struct {
uint8_t magic;
uint8_t version;
uint8_t checksum;
uint8_t flags;
uint32_t timestamp;
} SavedStructHeader;
bool saved_struct_save(const char* path, void* data, size_t size, uint8_t magic, uint8_t version) {
furi_assert(path);
furi_assert(data);
furi_assert(size);
SavedStructHeader header;
FURI_LOG_I(TAG, "Saving \"%s\"", path);
// Store
Storage* storage = furi_record_open("storage");
File* file = storage_file_alloc(storage);
bool result = true;
bool saved = storage_file_open(file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS);
if(!saved) {
FURI_LOG_E(
TAG, "Open failed \"%s\". Error: \'%s\'", path, storage_file_get_error_desc(file));
result = false;
}
if(result) {
// Calculate checksum
uint8_t checksum = 0;
uint8_t* source = data;
for(size_t i = 0; i < size; i++) {
checksum += source[i];
}
// Set header
header.magic = magic;
header.version = version;
header.checksum = checksum;
header.flags = 0;
header.timestamp = 0;
uint16_t bytes_count = storage_file_write(file, &header, sizeof(header));
bytes_count += storage_file_write(file, data, size);
if(bytes_count != (size + sizeof(header))) {
FURI_LOG_E(
TAG, "Write failed \"%s\". Error: \'%s\'", path, storage_file_get_error_desc(file));
result = false;
}
}
storage_file_close(file);
storage_file_free(file);
furi_record_close("storage");
return result;
}
bool saved_struct_load(const char* path, void* data, size_t size, uint8_t magic, uint8_t version) {
FURI_LOG_I(TAG, "Loading \"%s\"", path);
SavedStructHeader header;
uint8_t* data_read = malloc(size);
Storage* storage = furi_record_open("storage");
File* file = storage_file_alloc(storage);
bool result = true;
bool loaded = storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING);
if(!loaded) {
FURI_LOG_E(
TAG, "Failed to read \"%s\". Error: %s", path, storage_file_get_error_desc(file));
result = false;
}
if(result) {
uint16_t bytes_count = storage_file_read(file, &header, sizeof(SavedStructHeader));
bytes_count += storage_file_read(file, data_read, size);
if(bytes_count != (sizeof(SavedStructHeader) + size)) {
FURI_LOG_E(TAG, "Size mismatch of file \"%s\"", path);
result = false;
}
}
if(result && (header.magic != magic || header.version != version)) {
FURI_LOG_E(
TAG,
"Magic(%d != %d) or Version(%d != %d) mismatch of file \"%s\"",
header.magic,
magic,
header.version,
version,
path);
result = false;
}
if(result) {
uint8_t checksum = 0;
const uint8_t* source = (const uint8_t*)data_read;
for(size_t i = 0; i < size; i++) {
checksum += source[i];
}
if(header.checksum != checksum) {
FURI_LOG_E(
TAG, "Checksum(%d != %d) mismatch of file \"%s\"", header.checksum, checksum, path);
result = false;
}
}
if(result) {
memcpy(data, data_read, size);
}
storage_file_close(file);
storage_file_free(file);
furi_record_close("storage");
free(data_read);
return result;
}
|