1
mirror of https://github.com/DarkFlippers/unleashed-firmware.git synced 2025-12-12 20:49:49 +04:00

Merge remote-tracking branch 'OFW/dev' into dev

This commit is contained in:
MX
2024-03-25 23:20:10 +03:00
14 changed files with 184 additions and 42 deletions

View File

@@ -13,7 +13,12 @@ typedef struct {
uint32_t timestamp;
} SavedStructHeader;
bool saved_struct_save(const char* path, void* data, size_t size, uint8_t magic, uint8_t version) {
bool saved_struct_save(
const char* path,
const void* data,
size_t size,
uint8_t magic,
uint8_t version) {
furi_check(path);
furi_check(data);
furi_check(size);
@@ -35,7 +40,7 @@ bool saved_struct_save(const char* path, void* data, size_t size, uint8_t magic,
if(result) {
// Calculate checksum
uint8_t checksum = 0;
uint8_t* source = data;
const uint8_t* source = data;
for(size_t i = 0; i < size; i++) {
checksum += source[i];
}
@@ -63,6 +68,10 @@ bool saved_struct_save(const char* path, void* data, size_t size, uint8_t magic,
}
bool saved_struct_load(const char* path, void* data, size_t size, uint8_t magic, uint8_t version) {
furi_check(path);
furi_check(data);
furi_check(size);
FURI_LOG_I(TAG, "Loading \"%s\"", path);
SavedStructHeader header;
@@ -126,13 +135,12 @@ bool saved_struct_load(const char* path, void* data, size_t size, uint8_t magic,
return result;
}
bool saved_struct_get_payload_size(
bool saved_struct_get_metadata(
const char* path,
uint8_t magic,
uint8_t version,
uint8_t* magic,
uint8_t* version,
size_t* payload_size) {
furi_check(path);
furi_check(payload_size);
SavedStructHeader header;
Storage* storage = furi_record_open(RECORD_STORAGE);
@@ -146,26 +154,22 @@ bool saved_struct_get_payload_size(
break;
}
size_t bytes_count = storage_file_read(file, &header, sizeof(SavedStructHeader));
if(bytes_count != sizeof(SavedStructHeader)) {
if(storage_file_read(file, &header, sizeof(SavedStructHeader)) !=
sizeof(SavedStructHeader)) {
FURI_LOG_E(TAG, "Failed to read header");
break;
}
if((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);
break;
if(magic) {
*magic = header.magic;
}
if(version) {
*version = header.version;
}
if(payload_size) {
uint64_t file_size = storage_file_size(file);
*payload_size = file_size - sizeof(SavedStructHeader);
}
uint64_t file_size = storage_file_size(file);
*payload_size = file_size - sizeof(SavedStructHeader);
result = true;
} while(false);

View File

@@ -1,3 +1,8 @@
/**
* @file saved_struct.h
* @brief SavedStruct - data serialization/de-serialization
*
*/
#pragma once
#include <stdint.h>
@@ -8,14 +13,50 @@
extern "C" {
#endif
/** Load data from the file in saved structure format
*
* @param[in] path The path to the file
* @param[out] data Pointer to the memory where to load data
* @param[in] size The size of the data
* @param[in] magic The magic to embed into metadata
* @param[in] version The version to embed into metadata
*
* @return true on success, false otherwise
*/
bool saved_struct_load(const char* path, void* data, size_t size, uint8_t magic, uint8_t version);
bool saved_struct_save(const char* path, void* data, size_t size, uint8_t magic, uint8_t version);
bool saved_struct_get_payload_size(
/** Save data in saved structure format
*
* @param[in] path The path to the file
* @param[in] data Pointer to the memory where data
* @param[in] size The size of the data
* @param[in] magic The magic to embed into metadata
* @param[in] version The version to embed into metadata
*
* @return true on success, false otherwise
*/
bool saved_struct_save(
const char* path,
const void* data,
size_t size,
uint8_t magic,
uint8_t version,
uint8_t version);
/** Get SavedStructure file metadata
*
* @param[in] path The path to the file
* @param[out] magic Pointer to store magic or NULL if you don't need it
* @param[out] version Pointer to store version or NULL if you don't need
* it
* @param[out] payload_size Pointer to store payload size or NULL if you don't
* need it
*
* @return true on success, false otherwise
*/
bool saved_struct_get_metadata(
const char* path,
uint8_t* magic,
uint8_t* version,
size_t* payload_size);
#ifdef __cplusplus