/* * 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 void ini_test(void) { char buf[16]; char ip[16] = {0}, mask[16] = {0}, gw[16] = {0}; int len; /* ** IP地址,保存在config段 */ len = ini_gets("config", "IPAddr", "000000", buf, 16, LJW_CFG_FILE_NAME); if(strcmp(buf, "000000") == 0) { // 采用默认值 len = ini_puts("config", "IPAddr", "192.168.1.253", LJW_CFG_FILE_NAME); } /* ** 子网掩码,保存在config段 */ len = ini_gets("config", "IPMask", "000000", buf, 16, LJW_CFG_FILE_NAME); if(strcmp(buf, "000000") == 0) { // 采用默认值 len = ini_puts("config", "IPMask", "255.255.255.0", LJW_CFG_FILE_NAME); } /* ** 网关,保存在config段 */ len = ini_gets("config", "IPGateWay", "000000", buf, 16, LJW_CFG_FILE_NAME); if(strcmp(buf, "000000") == 0) { // 采用默认值 len = ini_puts("config", "IPGateWay", "192.168.1.1", LJW_CFG_FILE_NAME); } // 读取IP地址 ini_gets("config", "IPAddr", "000000", ip, 16, LJW_CFG_FILE_NAME); ini_gets("config", "IPMask", "000000", mask, 16, LJW_CFG_FILE_NAME); ini_gets("config", "IPGateWay", "000000", gw, 16, LJW_CFG_FILE_NAME); // set_if("e0",ip, gw,mask); } #ifdef RT_USING_FINSH #include MSH_CMD_EXPORT(ini_test, "IP address save as ini files, write and read it") #endif #include "cfg.h" extern struct rt_messagequeue update_cfg;//main线程 int set_cfg(const char *k, const char*v) { 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_strcpy(msg.key, k); rt_strcpy(msg.value, v); result = rt_mq_send(&update_cfg, &msg, sizeof(msg)); if (result == -RT_EFULL) { /* 消息队列满 */ LOG_E("message queue full!\n"); } } 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 getAll(void) { char buf[16]; char kstr[10]; for (size_t k = 0; ini_getkey("config", k, kstr, 10, LJW_CFG_FILE_NAME) > 0; k++) { int rst = ini_gets("config",kstr,"000000",buf,16,LJW_CFG_FILE_NAME); LOG_I("%10s = %s",kstr,buf); } } void cfg(int argc, char ** argv) { if (argc == 1) {//无键无值,遍历 getAll(); } if (argc == 2) {//有键无值,查询 get_cfg(argv[1]); } if (argc == 3) {//有键有值,设置 set_cfg(argv[1], argv[2]); } } MSH_CMD_EXPORT(cfg, "config params. 配置系统参数,支持参数") #endif