[FL-977] Internal Storage (#455)
* Add littlefs submodule * Furi: add mutex in logging, fix issues with corrupted printf * ApiHal: disable debug traces in ble glue * App-loader: more logs * Passport: fix invalid DolphinState usage * ApiHal, linker script: flash API is now aware of free space, complete abstraction layer for storage * Internal Storage: littlefs based storage services with key value API. Migrate dolphin state to new storage API.
This commit is contained in:
@@ -1,77 +1,129 @@
|
||||
#include "dolphin_state.h"
|
||||
|
||||
#include <internal-storage/internal-storage.h>
|
||||
#include <furi.h>
|
||||
#include <api-hal.h>
|
||||
#include <math.h>
|
||||
|
||||
#define DOLPHIN_STORE_KEY "dolphin_state"
|
||||
#define DOLPHIN_STORE_HEADER_MAGIC 0xD0
|
||||
#define DOLPHIN_STORE_HEADER_VERSION 0x01
|
||||
#define DOLPHIN_LVL_THRESHOLD 20.0f
|
||||
|
||||
typedef struct {
|
||||
uint8_t magic;
|
||||
uint8_t version;
|
||||
uint8_t checksum;
|
||||
uint8_t flags;
|
||||
uint32_t timestamp;
|
||||
} DolphinStoreHeader;
|
||||
|
||||
typedef struct {
|
||||
uint32_t limit_ibutton;
|
||||
uint32_t limit_nfc;
|
||||
uint32_t limit_ir;
|
||||
uint32_t limit_rfid;
|
||||
|
||||
uint32_t flags;
|
||||
uint32_t icounter;
|
||||
uint32_t butthurt;
|
||||
} DolphinStoreData;
|
||||
|
||||
typedef struct {
|
||||
DolphinStoreHeader header;
|
||||
DolphinStoreData data;
|
||||
} DolphinStore;
|
||||
|
||||
struct DolphinState {
|
||||
InternalStorage* internal_storage;
|
||||
DolphinStoreData data;
|
||||
};
|
||||
|
||||
DolphinState* dolphin_state_alloc() {
|
||||
DolphinState* dolphin_state = furi_alloc(sizeof(DolphinState));
|
||||
dolphin_state->internal_storage = furi_record_open("internal-storage");
|
||||
return dolphin_state;
|
||||
}
|
||||
|
||||
void dolphin_state_free(DolphinState* dolphin_state) {
|
||||
furi_record_close("internal-storage");
|
||||
free(dolphin_state);
|
||||
}
|
||||
|
||||
bool dolphin_state_save(DolphinState* dolphin_state) {
|
||||
if(!api_hal_flash_erase(DOLPHIN_DATA_PAGE, 1)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DolphinStore store;
|
||||
FURI_LOG_I("dolphin-state", "Saving state to internal-storage");
|
||||
// Calculate checksum
|
||||
uint8_t* source = (uint8_t*)&dolphin_state->data;
|
||||
uint8_t checksum = 0;
|
||||
for(size_t i = 0; i < sizeof(DolphinData); i++) {
|
||||
for(size_t i = 0; i < sizeof(DolphinStoreData); i++) {
|
||||
checksum += source[i];
|
||||
}
|
||||
DolphinDataHeader header;
|
||||
header.magic = DOLPHIN_DATA_HEADER_MAGIC;
|
||||
header.version = DOLPHIN_DATA_HEADER_VERSION;
|
||||
header.checksum = checksum;
|
||||
header.flags = 0;
|
||||
header.timestamp = 0;
|
||||
if(!api_hal_flash_write_dword(DOLPHIN_DATA_HEADER_ADDRESS, *(uint64_t*)&header)) {
|
||||
// Set header
|
||||
store.header.magic = DOLPHIN_STORE_HEADER_MAGIC;
|
||||
store.header.version = DOLPHIN_STORE_HEADER_VERSION;
|
||||
store.header.checksum = checksum;
|
||||
store.header.flags = 0;
|
||||
store.header.timestamp = 0;
|
||||
// Set data
|
||||
store.data = dolphin_state->data;
|
||||
// Store
|
||||
int ret = internal_storage_write_key(
|
||||
dolphin_state->internal_storage, DOLPHIN_STORE_KEY, (uint8_t*)&store, sizeof(DolphinStore));
|
||||
if(ret != sizeof(DolphinStore)) {
|
||||
FURI_LOG_E("dolphin-state", "Save failed. Storage returned: %d", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t destination[sizeof(uint64_t)];
|
||||
size_t block_count = sizeof(DolphinData) / sizeof(uint64_t) + 1;
|
||||
size_t offset = 0;
|
||||
for(size_t i = 0; i < block_count; i++) {
|
||||
for(size_t n = 0; n < sizeof(uint64_t); n++) {
|
||||
if(offset < sizeof(DolphinData)) {
|
||||
destination[n] = source[offset];
|
||||
} else {
|
||||
destination[n] = 0;
|
||||
}
|
||||
offset++;
|
||||
}
|
||||
if(!api_hal_flash_write_dword(
|
||||
DOLPHIN_DATA_DATA_ADDRESS + i * sizeof(uint64_t), *(uint64_t*)destination)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
FURI_LOG_I("dolphin-state", "Saved");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dolphin_state_load(DolphinState* dolphin_state) {
|
||||
const DolphinDataHeader* header = (const DolphinDataHeader*)DOLPHIN_DATA_HEADER_ADDRESS;
|
||||
if(header->magic == DOLPHIN_DATA_HEADER_MAGIC &&
|
||||
header->version == DOLPHIN_DATA_HEADER_VERSION) {
|
||||
DolphinStore store;
|
||||
// Read Dolphin State Store
|
||||
FURI_LOG_I("dolphin-state", "Loading state from internal-storage");
|
||||
int ret = internal_storage_read_key(
|
||||
dolphin_state->internal_storage, DOLPHIN_STORE_KEY, (uint8_t*)&store, sizeof(DolphinStore));
|
||||
if(ret != sizeof(DolphinStore)) {
|
||||
FURI_LOG_E("dolphin-state", "Load failed. Storage returned: %d", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
FURI_LOG_I("dolphin-state", "State loaded, verifying header");
|
||||
if(store.header.magic == DOLPHIN_STORE_HEADER_MAGIC &&
|
||||
store.header.version == DOLPHIN_STORE_HEADER_VERSION) {
|
||||
FURI_LOG_I(
|
||||
"dolphin-state",
|
||||
"Magic(%d) and Version(%d) match",
|
||||
store.header.magic,
|
||||
store.header.version);
|
||||
uint8_t checksum = 0;
|
||||
const uint8_t* source = (const uint8_t*)DOLPHIN_DATA_DATA_ADDRESS;
|
||||
for(size_t i = 0; i < sizeof(DolphinData); i++) {
|
||||
const uint8_t* source = (const uint8_t*)&store.data;
|
||||
for(size_t i = 0; i < sizeof(DolphinStoreData); i++) {
|
||||
checksum += source[i];
|
||||
}
|
||||
if(header->checksum == checksum) {
|
||||
memcpy(
|
||||
&dolphin_state->data, (const void*)DOLPHIN_DATA_DATA_ADDRESS, sizeof(DolphinData));
|
||||
if(store.header.checksum == checksum) {
|
||||
FURI_LOG_I("dolphin-state", "Checksum(%d) match", store.header.checksum);
|
||||
dolphin_state->data = store.data;
|
||||
return true;
|
||||
} else {
|
||||
FURI_LOG_E(
|
||||
"dolphin-state", "Checksum(%d != %d) mismatch", store.header.checksum, checksum);
|
||||
}
|
||||
} else {
|
||||
FURI_LOG_E(
|
||||
"dolphin-state",
|
||||
"Magic(%d != %d) and Version(%d != %d) mismatch",
|
||||
store.header.magic,
|
||||
DOLPHIN_STORE_HEADER_MAGIC,
|
||||
store.header.version,
|
||||
DOLPHIN_STORE_HEADER_VERSION);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void dolphin_state_clear(DolphinState* dolphin_state) {
|
||||
memset(&dolphin_state->data, 0, sizeof(DolphinData));
|
||||
memset(&dolphin_state->data, 0, sizeof(DolphinStoreData));
|
||||
}
|
||||
|
||||
void dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed) {
|
||||
|
@@ -4,38 +4,6 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define DOLPHIN_DATA_PAGE 0xC0
|
||||
#define DOLPHIN_DATA_HEADER_ADDRESS 0x080C0000U
|
||||
#define DOLPHIN_DATA_DATA_ADDRESS (DOLPHIN_DATA_HEADER_ADDRESS + sizeof(DolphinDataHeader))
|
||||
|
||||
#define DOLPHIN_DATA_HEADER_MAGIC 0xD0
|
||||
#define DOLPHIN_DATA_HEADER_VERSION 0x01
|
||||
|
||||
#define DOLPHIN_LVL_THRESHOLD 20.0f
|
||||
|
||||
typedef struct {
|
||||
uint8_t magic;
|
||||
uint8_t version;
|
||||
uint8_t checksum;
|
||||
uint8_t flags;
|
||||
uint32_t timestamp;
|
||||
} DolphinDataHeader;
|
||||
|
||||
typedef struct {
|
||||
uint32_t limit_ibutton;
|
||||
uint32_t limit_nfc;
|
||||
uint32_t limit_ir;
|
||||
uint32_t limit_rfid;
|
||||
|
||||
uint32_t flags;
|
||||
uint32_t icounter;
|
||||
uint32_t butthurt;
|
||||
} DolphinData;
|
||||
|
||||
struct DolphinState {
|
||||
DolphinData data;
|
||||
};
|
||||
|
||||
typedef struct DolphinState DolphinState;
|
||||
|
||||
DolphinState* dolphin_state_alloc();
|
||||
|
@@ -78,14 +78,14 @@ static void render_callback(Canvas* canvas, void* ctx) {
|
||||
}
|
||||
|
||||
int32_t passport(void* p) {
|
||||
DolphinState _state;
|
||||
DolphinState* dolphin_state = dolphin_state_alloc();
|
||||
ValueMutex state_mutex;
|
||||
dolphin_state_load(&_state);
|
||||
dolphin_state_load(dolphin_state);
|
||||
|
||||
osMessageQueueId_t event_queue = osMessageQueueNew(2, sizeof(AppEvent), NULL);
|
||||
furi_check(event_queue);
|
||||
|
||||
if(!init_mutex(&state_mutex, &_state, sizeof(DolphinState))) {
|
||||
if(!init_mutex(&state_mutex, dolphin_state, sizeof(DolphinState*))) {
|
||||
printf("[Passport] cannot create mutex\r\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -121,5 +121,7 @@ int32_t passport(void* p) {
|
||||
|
||||
osMessageQueueDelete(event_queue);
|
||||
|
||||
dolphin_state_free(dolphin_state);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user