2023-05-29 12:49:30 +00:00
|
|
|
|
/*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2023-06-17 10:26:40 +00:00
|
|
|
|
//#include "../packages/minIni-v1.2.0/dev/minIni.h"
|
|
|
|
|
#include "minIni.h"
|
2023-05-29 12:49:30 +00:00
|
|
|
|
|
|
|
|
|
#ifdef PKG_USING_MININI
|
|
|
|
|
#define LJW_CFG_FILE_NAME "/cfg/cfg.ini"
|
|
|
|
|
|
2023-05-30 08:53:31 +00:00
|
|
|
|
#define LOG_TAG "cfg"
|
|
|
|
|
#define LOG_LVL LOG_LVL_DBG
|
|
|
|
|
#include <ulog.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "cfg.h"
|
2023-06-01 02:23:08 +00:00
|
|
|
|
//extern struct rt_messagequeue update_cfg;//main线程
|
2023-05-30 08:53:31 +00:00
|
|
|
|
|
2023-06-01 02:23:08 +00:00
|
|
|
|
struct rt_event update_cfg;
|
|
|
|
|
void iniEvent(void)
|
|
|
|
|
{
|
|
|
|
|
/* 事 件 控 制 块 */
|
2023-05-30 08:53:31 +00:00
|
|
|
|
|
2023-06-06 07:45:13 +00:00
|
|
|
|
rt_err_t result = rt_event_init(&update_cfg, "cfg", RT_IPC_FLAG_FIFO);
|
2023-06-01 02:23:08 +00:00
|
|
|
|
if (result != RT_EOK)
|
|
|
|
|
{
|
|
|
|
|
LOG_E("init event failed.\n");
|
|
|
|
|
// return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* 导出到自动初始化 */
|
2023-06-06 07:45:13 +00:00
|
|
|
|
//INIT_COMPONENT_EXPORT(iniEvent);
|
2023-06-01 02:23:08 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置config项
|
|
|
|
|
* @param s
|
|
|
|
|
* @param k
|
|
|
|
|
* @param v
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2023-05-30 08:53:31 +00:00
|
|
|
|
int set_cfg(const char *k, const char*v)
|
|
|
|
|
{
|
2023-06-01 02:23:08 +00:00
|
|
|
|
if (rt_strcmp(v,"NULL") == 0) {//delete key
|
|
|
|
|
v = NULL;
|
|
|
|
|
}
|
2023-05-30 08:53:31 +00:00
|
|
|
|
int rst = ini_puts("config",k,v,LJW_CFG_FILE_NAME);
|
|
|
|
|
if (rst == 1) {
|
|
|
|
|
|
|
|
|
|
LOG_I("set value success.");
|
|
|
|
|
|
|
|
|
|
//以下消息队列通知各线程更新参数
|
2023-06-01 02:23:08 +00:00
|
|
|
|
// CFG_MSG msg;
|
2023-06-06 07:45:13 +00:00
|
|
|
|
// rt_err_t result;
|
2023-05-30 08:53:31 +00:00
|
|
|
|
|
2023-06-01 02:23:08 +00:00
|
|
|
|
|
2023-06-02 06:37:58 +00:00
|
|
|
|
rt_event_send(&update_cfg, CFGCHANGEED|COMPRESSTYPE);
|
|
|
|
|
rt_event_send(&update_cfg, CFGCHANGEED|MAXSIZEPERFILE);
|
2023-05-30 08:53:31 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
LOG_E("set value fault.");
|
|
|
|
|
}
|
|
|
|
|
return rst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int get_cfg(const char *k)
|
|
|
|
|
{
|
2023-06-01 09:10:00 +00:00
|
|
|
|
// 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) {
|
2023-05-30 08:53:31 +00:00
|
|
|
|
LOG_W("no such KEY:%s",k);
|
2023-06-01 09:10:00 +00:00
|
|
|
|
return -RT_ERROR;
|
2023-05-30 08:53:31 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2023-06-01 09:10:00 +00:00
|
|
|
|
// LOG_I("%s = %s",k,buf);
|
2023-06-19 07:56:45 +00:00
|
|
|
|
// LOG_I("%s = %d",k,rst);
|
2023-05-30 08:53:31 +00:00
|
|
|
|
}
|
|
|
|
|
return rst;
|
|
|
|
|
}
|
2023-06-01 02:23:08 +00:00
|
|
|
|
|
|
|
|
|
void get_cfg_all(void)
|
2023-05-30 08:53:31 +00:00
|
|
|
|
{
|
2023-06-01 09:10:00 +00:00
|
|
|
|
char buf[MAX_KEY_LEN];
|
|
|
|
|
char kstr[MAX_KEY_LEN];
|
2023-06-01 02:23:08 +00:00
|
|
|
|
|
|
|
|
|
LOG_I("%23s","---CONFIG---");
|
2023-06-01 09:10:00 +00:00
|
|
|
|
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);
|
2023-06-01 02:23:08 +00:00
|
|
|
|
LOG_I("%16s = %s",kstr,buf);
|
2023-05-30 08:53:31 +00:00
|
|
|
|
}
|
2023-06-01 02:23:08 +00:00
|
|
|
|
|
|
|
|
|
|
2023-05-30 08:53:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-01 02:23:08 +00:00
|
|
|
|
|
|
|
|
|
|
2023-05-30 08:53:31 +00:00
|
|
|
|
void cfg(int argc, char ** argv)
|
|
|
|
|
{
|
|
|
|
|
if (argc == 1) {//无键无值,遍历
|
2023-06-01 02:23:08 +00:00
|
|
|
|
get_cfg_all();
|
2023-05-30 08:53:31 +00:00
|
|
|
|
}
|
|
|
|
|
if (argc == 2) {//有键无值,查询
|
|
|
|
|
get_cfg(argv[1]);
|
|
|
|
|
}
|
|
|
|
|
if (argc == 3) {//有键有值,设置
|
|
|
|
|
set_cfg(argv[1], argv[2]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-01 02:23:08 +00:00
|
|
|
|
static void get_sta_all(void)
|
|
|
|
|
{
|
2023-06-01 09:10:00 +00:00
|
|
|
|
char buf[MAX_KEY_LEN];
|
|
|
|
|
char kstr[MAX_KEY_LEN];
|
2023-06-01 02:23:08 +00:00
|
|
|
|
LOG_I("%23s","---STATS---");
|
2023-06-01 09:10:00 +00:00
|
|
|
|
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);
|
2023-06-01 02:23:08 +00:00
|
|
|
|
LOG_I("%16s = %s",kstr,buf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
MSH_CMD_EXPORT(cfg, config params. 配置系统参数,支持参数)
|
|
|
|
|
|
|
|
|
|
//以下针对stats
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param k
|
|
|
|
|
* return 成功返回值,错误返回RT_ERROR
|
|
|
|
|
*/
|
2023-06-02 02:23:26 +00:00
|
|
|
|
long get_val(const char *k)
|
2023-06-01 02:23:08 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-02 02:23:26 +00:00
|
|
|
|
int set_val(const char *k, long v)
|
2023-06-01 02:23:08 +00:00
|
|
|
|
{
|
|
|
|
|
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);
|
2023-06-19 07:56:45 +00:00
|
|
|
|
rt_thread_mdelay(100);
|
2023-06-01 02:23:08 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-30 08:53:31 +00:00
|
|
|
|
|
2023-06-01 02:23:08 +00:00
|
|
|
|
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, 重置系统统计数据)
|
2023-05-30 08:53:31 +00:00
|
|
|
|
|
|
|
|
|
|
2023-06-01 09:10:00 +00:00
|
|
|
|
/**
|
|
|
|
|
* 添加文件到待发列表,指定待发包
|
|
|
|
|
* @param f
|
|
|
|
|
* @param v
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
int setFileToSend(const char *f, int v)
|
|
|
|
|
{
|
|
|
|
|
int rst = ini_putl(SECTION_TO_SEND, f, v, LJW_CFG_FILE_NAME);
|
|
|
|
|
if (!rst) {
|
|
|
|
|
LOG_E("add file to send error.");
|
|
|
|
|
}
|
|
|
|
|
return rst;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 获取待发送文件列表
|
|
|
|
|
* @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];
|
|
|
|
|
size_t len=0;
|
|
|
|
|
for (size_t k = 0; ini_getkey(SECTION_TO_SEND, k, kstr[len], MAX_KEY_LEN, LJW_CFG_FILE_NAME) > 0; k++) {
|
|
|
|
|
v[len] = ini_getl(SECTION_TO_SEND, kstr[len], -1, LJW_CFG_FILE_NAME);
|
|
|
|
|
len +=1;
|
|
|
|
|
}
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 清空待发送文件记录
|
|
|
|
|
*/
|
|
|
|
|
int clearFileToSend(const char *k)
|
|
|
|
|
{
|
|
|
|
|
int rst = ini_puts(SECTION_TO_SEND, k, NULL, LJW_CFG_FILE_NAME);
|
|
|
|
|
if (!rst) {
|
|
|
|
|
LOG_E("clear file to send error.");
|
|
|
|
|
}
|
|
|
|
|
return rst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void gf()
|
|
|
|
|
{
|
|
|
|
|
int v[MAX_KEY_LEN];
|
|
|
|
|
char kstr[10][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]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void add(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
setFileToSend(argv[1],atoi(argv[2]));
|
|
|
|
|
gf();
|
|
|
|
|
clearFileToSend(argv[1]);
|
|
|
|
|
gf();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MSH_CMD_EXPORT(gf, 查看待发送文件列表)
|
2023-06-18 01:34:09 +00:00
|
|
|
|
//MSH_CMD_EXPORT_ALIAS(add, cf,查看待发送文件列表)
|
2023-06-01 09:10:00 +00:00
|
|
|
|
|
2023-06-01 02:23:08 +00:00
|
|
|
|
//set_if()
|
2023-05-29 12:49:30 +00:00
|
|
|
|
#endif
|