Files
sil/ET120_SIL/otherFiles/param_mgr.c
T

316 lines
5.6 KiB
C
Raw Normal View History

2020-09-30 11:44:05 +08:00
#include "param_mgr.h"
2020-12-20 15:19:07 +08:00
#include "app_config.h"
2020-09-30 11:44:05 +08:00
#include <string.h>
2020-12-20 15:19:07 +08:00
#include <stdint.h>
#include <stdbool.h>
2020-09-30 11:44:05 +08:00
2020-12-20 15:19:07 +08:00
#ifndef PARAM_PROPS_MAXLEN
2020-10-22 10:50:04 +08:00
#define PARAM_PROPS_MAXLEN (350)
2020-12-20 15:19:07 +08:00
#endif
2020-09-30 11:44:05 +08:00
param_prop props[PARAM_PROPS_MAXLEN];
uint16_t param_count = 0u;
param_prop *param_get_by_idx(uint16_t idx)
{
if (idx < param_count)
{
return props + idx;
}
return NULL;
}
uint16_t param_get_count(void)
{
return param_count;
}
static int16_t find_idx_by_name2(uint16_t b, uint16_t e, const char name[16])
{
if (b + 1u == e)
{
return e;
}
uint16_t m;
m = (b + e) >> 1;
int cmp = strncmp(name, props[m].name, 16);
if (cmp < 0)
{
return find_idx_by_name2(b, m, name);
}
else
{
return find_idx_by_name2(m, e, name);
}
}
uint16_t find_idx_by_name(const char name[16])
{
int cmp;
if (param_count == 0u)
{
return 0u;
}
else if (param_count == 1u)
{
cmp = strncmp(name, props[0].name, 16);
if (cmp < 0)
{
return 0u;
}
else
{
return 1u;
}
}
else
{
cmp = strncmp(name, props[0].name, 16);
if (cmp < 0)
{
return 0;
}
else
{
cmp = strncmp(name, props[param_count - 1].name, 16);
if (cmp < 0)
{
return find_idx_by_name2(0, param_count - 1, name);
}
else
{
return param_count;
}
}
}
}
param_prop *param_get_by_name(const char name[16])
{
uint16_t idx = find_idx_by_name(name);
if (idx > 0u && idx <= param_count && strncmp(name, props[idx - 1].name, 16) == 0)
{
return props + (idx - 1);
}
return NULL;
}
int16_t param_get_idx_by_name(const char name[16])
{
uint16_t idx = find_idx_by_name(name);
if (idx > 0u && idx <= param_count && strncmp(name, props[idx - 1].name, 16) == 0)
{
return idx - 1;
}
return -1;
}
int param_mgr_regist(param_prop *prop)
{
if (param_count < PARAM_PROPS_MAXLEN)
{
uint16_t idx = find_idx_by_name(prop->name);
for (uint16_t i = param_count; i > idx; --i)
{
memcpy(&props[i], &props[i - 1u], sizeof(param_prop));
}
memcpy(&props[idx], prop, sizeof(param_prop));
++param_count;
return 0;
}
return -1;
}
int param_get_size(param_prop *prop)
{
switch (prop->typ)
{
case PARAM_TYPE_UINT8:
case PARAM_TYPE_INT8:
return 1;
case PARAM_TYPE_UINT16:
case PARAM_TYPE_INT16:
return 2;
default:
return 4;
}
}
void param_set_value(param_prop *prop, const uint8_t value[4])
{
param_value_ptr_t val_ptr;
2020-10-22 10:50:04 +08:00
val_ptr.B = (uint8_t *)value;
2020-09-30 11:44:05 +08:00
if (prop->setter_ptr)
{
(*props->setter_ptr)(val_ptr);
}
else
{
memcpy(prop->val_ptr.B, value, param_get_size(prop));
}
}
void param_get_value(param_prop *prop, uint8_t value[4])
{
float val;
param_value_ptr_t val_ptr;
if (prop->getter_ptr)
{
val_ptr.B = value;
(*props->getter_ptr)(val_ptr);
}
else
{
val_ptr = prop->val_ptr;
memcpy(value, val_ptr.B, param_get_size(prop));
}
}
2020-12-20 15:19:07 +08:00
#if defined ( __GNUC__ ) && !defined (__CC_ARM) /* GNU Compiler */
#ifndef __weak
#define __weak __attribute__((weak))
#endif /* __weak */
#else
#define __weak
#endif /* __GNUC__ */
__weak bool param_NVRAM_write(uint32_t offset, uint8_t *buf, uint32_t size)
{
return false;
}
__weak bool param_NVRAM_read(uint32_t offset, uint8_t *buf, uint32_t size)
{
return false;
}
bool save_param(void)
{
uint16_t n = param_get_count();
uint8_t param_buff[20];
for (uint16_t i = 0u; i < n; ++i)
{
param_prop *p = param_get_by_idx(i);
if (p)
{
memset(param_buff, 0, 20);
param_get_value(p, &param_buff[16]);
strncpy((char *)param_buff, p->name, 16);
if (!param_NVRAM_write(i * 20, param_buff, 20))
{
goto onError;
}
}
else
{
goto onError;
}
}
return true;
onError:
return false;
}
bool param_save(int16_t i, param_prop *p)
{
uint16_t n = param_get_count();
uint8_t param_buff[20];
if (i >= 0 && i < n && p)
{
memset(param_buff, 0, 20);
param_get_value(p, &param_buff[16]);
strncpy((char *)param_buff, p->name, 16);
if (!param_NVRAM_write(i * 20, param_buff, 20))
{
goto onError;
}
}
return true;
onError:
return false;
}
bool restore_param(void)
{
uint16_t n = param_get_count();
uint8_t param_buff[20];
for (uint16_t i = 0u; i < n; ++i)
{
param_prop *p = param_get_by_idx(i);
if (p)
{
if (param_NVRAM_read(i * 20, param_buff, 20))
{
if (strncmp(p->name, (char *)param_buff, 16) == 0)
{
if (strncmp(p->name, "ap_ver", 6) == 0)
{
if (*(p->val_ptr.I) != *((uint32_t *)&param_buff[16]))
{
goto onError;
}
else
{
break;
}
}
}
else
{
goto onError;
}
}
else
{
goto onError;
}
}
else
{
goto onError;
}
}
for (uint16_t i = 0u; i < n; ++i)
{
param_prop *p = param_get_by_idx(i);
if (p)
{
if (param_NVRAM_read(i * 20, param_buff, 20))
{
if (strncmp(p->name, (char *)param_buff, 16) == 0)
{
param_set_value(p, &param_buff[16]);
}
else
{
goto onError;
}
}
else
{
goto onError;
}
}
else
{
goto onError;
}
}
return true;
onError:
return false;
}