TT12-MCU/applications/cfg.c

466 lines
9.6 KiB
C
Raw Normal View History

/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-05-29 murmur the first version
*/
//#include "../packages/minIni-v1.2.0/dev/minIni.h"
#include "minIni.h"
#ifdef PKG_USING_MININI
#define LJW_CFG_FILE_NAME "/cfg.ini"
#define FILE_TO_SEND "/sd/tosend.ini"//避免读写出错造成系统配置文件丢失
#define LOG_TAG "cfg"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>
#include "cfg.h"
static uint8_t islock=0;
static void setLock()
{
while(islock)
{
rt_thread_mdelay(1000);
}
islock=1;
}
static void clearLock()
{
islock=0;
}
extern rt_sem_t cfgUpdate;
int get_cfg(const char *k);
static struct rt_messagequeue upfilelist;
typedef struct
{
char fname[60];
uint8_t index;
}FILE_INFO;
static uint8_t msg_pool[512] ;
void addToList_thread_entry(void *parameter);
static void iniUFMsg(void)
{
/* 初始化消息队列 */
rt_mq_init(&upfilelist, "uplist",
msg_pool, /* 存放消息的缓冲区 */
sizeof(FILE_INFO), /* 一条消息的最大长度 */
sizeof(msg_pool), /* 存放消息的缓冲区大小 */
RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
/* 创建 serial 线程 */
rt_thread_t thread = rt_thread_create("filelist", addToList_thread_entry, RT_NULL, 1024*3, 30-2, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
LOG_E("thread 'updatelist' create failure.");
}
}
/* 导出到自动初始化 */
INIT_APP_EXPORT(iniUFMsg);
/**
* config项
* @param s
* @param k
* @param v
* @return
*/
2023-08-03 03:04:50 +00:00
int set_cfg(const char *k, long v)
{
// setLock();
if (rt_strcmp(v,"NULL") == 0) {//delete key
v = NULL;
2023-08-03 03:04:50 +00:00
}
// clearLock();
if (v == get_cfg(k)) {
LOG_D("nothing needs to change.");
clearLock();
return 1;
}
setLock();
2023-08-03 03:04:50 +00:00
// int rst = ini_puts("config",k,v,LJW_CFG_FILE_NAME);
int rst = ini_putl("config", k, v, LJW_CFG_FILE_NAME);
if (rst == 1) {
LOG_I("set value success.");
//以下消息队列通知各线程更新参数
rt_sem_release(cfgUpdate);
}
else {
LOG_E("set value fault.");
}
clearLock();
return rst;
}
int set_cfgs(const char *k, const char * v)
{
setLock();
if (rt_strcmp(v,"NULL") == 0) {//delete key
v = NULL;
}
int rst = ini_puts("config",k,v,LJW_CFG_FILE_NAME);
2023-08-03 03:04:50 +00:00
// int rst = ini_putl("config", k, v, LJW_CFG_FILE_NAME);
if (rst == 1) {
LOG_I("set value success.");
//以下消息队列通知各线程更新参数
rt_sem_release(cfgUpdate);
}
else {
LOG_E("set value fault.");
}
clearLock();
return rst;
}
/**
*
* @param k
* @return
*/
int get_cfg(const char *k)
{
setLock();
// char buf[MAX_KEY_LEN];
// int rst = ini_gets("config",k,"000000",buf,MAX_KEY_LEN,LJW_CFG_FILE_NAME);
// if(strcmp(buf, "000000") == 0) {
// // 采用默认值
int rst = ini_getl("config", k, -1, LJW_CFG_FILE_NAME);
if (rst == -1) {
LOG_W("no such KEY:%s",k);
clearLock();
return -RT_ERROR;
}
else {
// LOG_I("%s = %s",k,buf);
2023-06-19 07:56:45 +00:00
// LOG_I("%s = %d",k,rst);
}
clearLock();
return rst;
}
2023-08-03 03:04:50 +00:00
int get_cfgs(const char *k)
{
setLock();
char buf[MAX_KEY_LEN];
int rst = ini_gets("config",k,"000000",buf,MAX_KEY_LEN,LJW_CFG_FILE_NAME);
if(strcmp(buf, "000000") == 0) {
// // 采用默认值
// int rst = ini_getl("config", k, -1, LJW_CFG_FILE_NAME);
// if (rst == -1) {
LOG_W("no such KEY:%s",k);
clearLock();
return -RT_ERROR;
}
else {
// LOG_I("%s = %s",k,buf);
// LOG_I("%s = %d",k,rst);
}
clearLock();
return rst;
}
static void get_cfg_all(void)
{
char buf[MAX_KEY_LEN];
char kstr[MAX_KEY_LEN];
LOG_I("%23s","---CONFIG---");
for (size_t k = 0; ini_getkey("config", k, kstr, MAX_KEY_LEN, LJW_CFG_FILE_NAME) > 0; k++) {
int rst = ini_gets("config",kstr,"000000",buf,MAX_KEY_LEN,LJW_CFG_FILE_NAME);
LOG_I("%16s = %s",kstr,buf);
}
}
static void cfg(int argc, char ** argv)
{
if (argc == 1) {//无键无值,遍历
get_cfg_all();
}
if (argc == 2) {//有键无值,查询
get_cfg(argv[1]);
}
if (argc == 3) {//有键有值,设置
set_cfgs(argv[1], argv[2]);
}
}
static void get_sta_all(void)
{
char buf[MAX_KEY_LEN];
char kstr[MAX_KEY_LEN];
LOG_I("%23s","---STATS---");
for (size_t k = 0; ini_getkey("stats", k, kstr, MAX_KEY_LEN, LJW_CFG_FILE_NAME) > 0; k++) {
int rst = ini_gets("stats",kstr,"000000",buf,MAX_KEY_LEN,LJW_CFG_FILE_NAME);
LOG_I("%16s = %s",kstr,buf);
}
}
MSH_CMD_EXPORT(cfg, config params. )
//以下针对stats
/**
* stats节信息
* @param k
* return RT_ERROR
*/
long get_val(const char *k)
{
setLock();
long v= ini_getl("stats", k, -1, LJW_CFG_FILE_NAME);
if( v == -1)
{
LOG_W("no such KEY:%s",k);
clearLock();
return -RT_ERROR;
}
else {
clearLock();
return v;
}
}
int set_val(const char *k, long v)
{
setLock();
if(!ini_putl("stats",k,v,LJW_CFG_FILE_NAME))
{
LOG_E("write %s error.",k);
clearLock();
return -RT_ERROR;
}
clearLock();
return RT_EOK;
}
/**
* 1
* @param k
* @return
*/
int add_val(const char *k)
{
// setLock();
long ori = get_val(k);
2023-06-19 07:56:45 +00:00
rt_thread_mdelay(100);
if (ori != -1) {
int ret = set_val(k, ori+1);
rt_thread_mdelay(100);
return ret;
}
else {
return ori;
}
// clearLock();
}
static void get_sta(const char *k)
{
long v = get_val(k);
if (!v) {
LOG_I("%s = %d",k,v);
}
}
static void sta(int argc, char ** argv)
{
if (argc == 1)
{ //无键无值,遍历
get_sta_all();
}
if (argc == 2)
{ //有键无值,查询
get_sta(argv[1]);
}
if (argc == 3)
{
LOG_W("READ ONLY.");
}
}
MSH_CMD_EXPORT(sta, )
static void clear_sta(void)
{
char buf[16];
char kstr[16];
for (size_t k = 0; ini_getkey("stats", k, kstr, 16, LJW_CFG_FILE_NAME) > 0; k++) {
if (rt_strcmp("swCnt",kstr) != 0) {
set_val(kstr, NULL);
}
}
}
MSH_CMD_EXPORT_ALIAS(clear_sta,clsSta, )
static uint8_t nislock=0;
static void nsetLock()
{
while(nislock)
{
rt_thread_mdelay(1000);
}
nislock=1;
}
static void nclearLock()
{
nislock=0;
}
/**
*
* @param f
* @param v
* @return
*/
int setFileToSend(const char *f, int v)
{
nsetLock();
int rst = ini_putl(SECTION_TO_SEND, f, v, FILE_TO_SEND);
if (!rst) {
LOG_E("add file to send error.");
nclearLock();
return RT_ERROR;
}
nclearLock();
return RT_EOK;
}
/**
*
* @param kstr
* @param v 0
* @return
*/
int getFilesToSend(char (*kstr)[MAX_KEY_LEN], int *v)
{
// char buf[MAX_KEY_LEN];
// char kstr[MAX_KEY_LEN];
nsetLock();
size_t len=0;
for (size_t k = 0; ini_getkey(SECTION_TO_SEND, k, kstr[len], MAX_KEY_LEN, FILE_TO_SEND) > 0; k++) {
v[len] = ini_getl(SECTION_TO_SEND, kstr[len], -1, FILE_TO_SEND);
len +=1;
}
nclearLock();
return len;
}
/**
*
*/
int clearFileToSend(const char *k)
{
nsetLock();
int rst = ini_puts(SECTION_TO_SEND, k, NULL, FILE_TO_SEND);
if (!rst)
{
LOG_E("clear file to send error.");
}
nclearLock();
return rst;
}
static void gf()
{
int v[MAX_KEY_LEN];
char kstr[30][MAX_KEY_LEN];
size_t cnt = getFilesToSend(kstr, v);
for (size_t var = 0; var < cnt; var++)
{
LOG_I("%s -- %d", kstr[var], v[var]);
}
}
static void add(int argc, char **argv)
{
setFileToSend(argv[1],atoi(argv[2]));
gf();
clearFileToSend(argv[1]);
gf();
}
void addToList_thread_entry(void *parameter)
{
FILE_INFO msg;
while(1)
{
rt_memset(&msg, 0, sizeof(msg));
if (rt_mq_recv(&upfilelist, &msg, sizeof(msg), RT_WAITING_FOREVER) == RT_EOK) {
LOG_D("get %s",msg.fname);
setFileToSend(msg.fname,msg.index);
2023-08-03 03:04:50 +00:00
fileIsReady();
}
}
}
void postFileInfo(const char *fin, uint8_t index)
{
// setFileToSend(fin,0);
FILE_INFO msg;
strcpy(msg.fname,fin);
msg.index=index;
rt_mq_send(&upfilelist, &msg, sizeof(msg));
}
int updateLstFile(const char *fin)
{
nsetLock();
int rst = ini_puts(SECTION_LST_FILE, "lst", fin, FILE_TO_SEND);
if (!rst) {
LOG_E("update lst file error.");
nclearLock();
return RT_ERROR;
}
nclearLock();
return RT_EOK;
}
/**
*
* @param k
* @return
*/
int getLstFile(const char *k)
{
nsetLock();
char tmp[60];
int rst = ini_gets(SECTION_LST_FILE, "lst", "000",tmp,60, FILE_TO_SEND);
if (rst == 3) {
rst=0;
}
strcpy(k,tmp);
nclearLock();
return rst;
}
MSH_CMD_EXPORT(gf, )
MSH_CMD_EXPORT_ALIAS(add, cf,)
//set_if()
#endif