8cb31be86b
看门狗不工作 待更新cfg中ini_puts和ini_gets函数为ini_putl和ini_getl
210 lines
4.1 KiB
C
210 lines
4.1 KiB
C
/*
|
||
* 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"
|
||
|
||
#ifdef PKG_USING_MININI
|
||
#define LJW_CFG_FILE_NAME "/cfg/cfg.ini"
|
||
|
||
#define LOG_TAG "cfg"
|
||
#define LOG_LVL LOG_LVL_DBG
|
||
#include <ulog.h>
|
||
|
||
|
||
#include "cfg.h"
|
||
//extern struct rt_messagequeue update_cfg;//main线程
|
||
|
||
struct rt_event update_cfg;
|
||
void iniEvent(void)
|
||
{
|
||
/* 事 件 控 制 块 */
|
||
|
||
rt_err_t result = rt_event_init(&update_cfg, "update_cfg", RT_IPC_FLAG_FIFO);
|
||
if (result != RT_EOK)
|
||
{
|
||
LOG_E("init event failed.\n");
|
||
// return -1;
|
||
}
|
||
}
|
||
/* 导出到自动初始化 */
|
||
INIT_COMPONENT_EXPORT(iniEvent);
|
||
|
||
/**
|
||
* 设置config项
|
||
* @param s
|
||
* @param k
|
||
* @param v
|
||
* @return
|
||
*/
|
||
int set_cfg(const char *k, const char*v)
|
||
{
|
||
if (rt_strcmp(v,"NULL") == 0) {//delete key
|
||
v = NULL;
|
||
}
|
||
int rst = ini_puts("config",k,v,LJW_CFG_FILE_NAME);
|
||
if (rst == 1) {
|
||
|
||
LOG_I("set value success.");
|
||
|
||
//以下消息队列通知各线程更新参数
|
||
// CFG_MSG msg;
|
||
rt_err_t result;
|
||
|
||
|
||
// rt_event_send(&update_cfg, CFGCHANGEED|COMPRESSTYPE);
|
||
// rt_event_send(&update_cfg, CFGCHANGEED|MAXSIZEPERFILE);
|
||
|
||
}
|
||
else {
|
||
LOG_E("set value fault.");
|
||
}
|
||
return rst;
|
||
}
|
||
|
||
int get_cfg(const char *k)
|
||
{
|
||
char buf[16];
|
||
int rst = ini_gets("config",k,"000000",buf,16,LJW_CFG_FILE_NAME);
|
||
if(strcmp(buf, "000000") == 0) {
|
||
// 采用默认值
|
||
LOG_W("no such KEY:%s",k);
|
||
}
|
||
else {
|
||
LOG_I("%s = %s",k,buf);
|
||
}
|
||
return rst;
|
||
}
|
||
|
||
void get_cfg_all(void)
|
||
{
|
||
char buf[16];
|
||
char kstr[16];
|
||
|
||
LOG_I("%23s","---CONFIG---");
|
||
for (size_t k = 0; ini_getkey("config", k, kstr, 16, LJW_CFG_FILE_NAME) > 0; k++) {
|
||
int rst = ini_gets("config",kstr,"000000",buf,16,LJW_CFG_FILE_NAME);
|
||
LOG_I("%16s = %s",kstr,buf);
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
void cfg(int argc, char ** argv)
|
||
{
|
||
if (argc == 1) {//无键无值,遍历
|
||
get_cfg_all();
|
||
}
|
||
if (argc == 2) {//有键无值,查询
|
||
get_cfg(argv[1]);
|
||
}
|
||
if (argc == 3) {//有键有值,设置
|
||
set_cfg(argv[1], argv[2]);
|
||
}
|
||
}
|
||
|
||
static void get_sta_all(void)
|
||
{
|
||
char buf[16];
|
||
char kstr[16];
|
||
LOG_I("%23s","---STATS---");
|
||
for (size_t k = 0; ini_getkey("stats", k, kstr, 16, LJW_CFG_FILE_NAME) > 0; k++) {
|
||
int rst = ini_gets("stats",kstr,"000000",buf,16,LJW_CFG_FILE_NAME);
|
||
LOG_I("%16s = %s",kstr,buf);
|
||
}
|
||
}
|
||
MSH_CMD_EXPORT(cfg, config params. 配置系统参数,支持参数)
|
||
|
||
//以下针对stats
|
||
/**
|
||
*
|
||
* @param k
|
||
* return 成功返回值,错误返回RT_ERROR
|
||
*/
|
||
static long get_val(const char *k)
|
||
{
|
||
long v= ini_getl("stats", k, -1, LJW_CFG_FILE_NAME);
|
||
if( v == -1)
|
||
{
|
||
LOG_W("no such KEY:%s",k);
|
||
return -RT_ERROR;
|
||
}
|
||
else {
|
||
return v;
|
||
}
|
||
}
|
||
|
||
static int set_val(const char *k, long v)
|
||
{
|
||
if(!ini_putl("stats",k,v,LJW_CFG_FILE_NAME))
|
||
{
|
||
LOG_E("write %s error.",k);
|
||
return -RT_ERROR;
|
||
}
|
||
return RT_EOK;
|
||
}
|
||
|
||
/**
|
||
* 更新统计值,每次自加1
|
||
* @param k
|
||
* @return
|
||
*/
|
||
int add_val(const char *k)
|
||
{
|
||
long ori = get_val(k);
|
||
if (ori != -1) {
|
||
return set_val(k, ori+1);
|
||
}
|
||
else {
|
||
return ori;
|
||
}
|
||
}
|
||
static void get_sta(const char *k)
|
||
{
|
||
long v = get_val(k);
|
||
if (!v) {
|
||
LOG_I("%s = %d",k,v);
|
||
}
|
||
}
|
||
|
||
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, 重置系统统计数据)
|
||
|
||
|
||
//set_if()
|
||
#endif
|