84 lines
2.2 KiB
C
84 lines
2.2 KiB
C
//
|
||
// Created by cfif on 07.10.22.
|
||
//
|
||
|
||
#include <CmsisRtosThreadUtils.h>
|
||
#include <memory.h>
|
||
#include "DeviceStorage.h"
|
||
#include "DataNonVolatile.h"
|
||
#include "StorageOnFlashFlagchip.h"
|
||
#include "SystemDelayInterface.h"
|
||
|
||
#define LOGGER env->logger
|
||
#define LOG_SIGN "Хран."
|
||
|
||
#define VARIABLE_GROUP_UNTRACKED 0
|
||
#define VARIABLE_GROUP_FLASH (1 << 0)
|
||
|
||
void DeviceStorage_InitVariablesTable(tDeviceStorage *env) {
|
||
|
||
VariablesTableInitStatic(&env->publicVariablesTable, env->mem.vartab);
|
||
|
||
DeviceDataRuntime_InitDefaults(&env->runtime);
|
||
|
||
DeviceDataNonVolatile_AddToVarTab(env->nvm, &env->publicVariablesTable, VARIABLE_GROUP_FLASH);
|
||
DeviceDataRuntime_AddToVarTab(&env->runtime, &env->publicVariablesTable, VARIABLE_GROUP_UNTRACKED);
|
||
}
|
||
|
||
bool DeviceStorage_LoadNonVolatile(tDeviceStorage *env) {
|
||
|
||
bool isLoad = VarsTabDumpObserver_Load(&env->dumpObserver);
|
||
|
||
if (isLoad) {
|
||
if (env->nvm->version == DEVICE_DATA_NO_VOLATILE_VERSION) {
|
||
return true;
|
||
}
|
||
}
|
||
|
||
LoggerInfoStatic(LOGGER, LOG_SIGN, "Сброс настроек");
|
||
|
||
DeviceDataNonVolatile_InitDefaults(env->nvm);
|
||
|
||
return VarsTabDumpObserver_Dump(&env->dumpObserver);
|
||
|
||
}
|
||
|
||
void DeviceStorage_ForceDump(tDeviceStorage *env) {
|
||
VarsTabDumpObserver_Dump(&env->dumpObserver);
|
||
}
|
||
|
||
void DeviceStorage_DelayedDump(tDeviceStorage *env) {
|
||
env->publicVariablesTable.changes = 1;
|
||
}
|
||
|
||
void DeviceStorage_InitCommon(tDeviceStorage *env, tStorageInterface *storageInterface) {
|
||
VarsTabDumpObserver_Init(
|
||
&env->dumpObserver,
|
||
storageInterface,
|
||
5000,
|
||
env->nvm,
|
||
// sizeof(env->nvm),
|
||
16000,
|
||
&env->publicVariablesTable,
|
||
VARIABLE_GROUP_FLASH
|
||
);
|
||
|
||
|
||
DeviceStorage_InitVariablesTable(env);
|
||
|
||
}
|
||
|
||
void DeviceStorage_InitDefaults(tDeviceStorage *env) {
|
||
DeviceStorage_InitCommon(env, NULL);
|
||
DeviceDataNonVolatile_InitDefaults(env->nvm);
|
||
}
|
||
|
||
uint8_t dataReservedStore[16000];
|
||
|
||
bool DeviceStorage_Init(tDeviceStorage *env, tStorageInterface *storageInterface) {
|
||
env->nvm = (tDeviceDataNonVolatile *) dataReservedStore;
|
||
|
||
DeviceStorage_InitCommon(env, storageInterface);
|
||
return DeviceStorage_LoadNonVolatile(env);
|
||
}
|