|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*
|
|
|
|
|
* Change Logs:
|
|
|
|
|
* Date Author Notes
|
|
|
|
|
* 2025-12-01 Administrator the first version
|
|
|
|
|
*/
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
#include <dfs_posix.h>
|
|
|
|
|
#include "ini.h"
|
|
|
|
|
#include "Variable.h"
|
|
|
|
|
|
|
|
|
|
#define INI_config "/sys/828config.ini"
|
|
|
|
|
|
|
|
|
|
#define DBG_TAG "INI.config"
|
|
|
|
|
#define DBG_LVL DBG_LOG
|
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
|
|
|
|
|
void write_sample(void)
|
|
|
|
|
{
|
|
|
|
|
//检查文件
|
|
|
|
|
if (access(INI_config, F_OK) != 0)
|
|
|
|
|
{
|
|
|
|
|
LOG_E("NOT 828config");
|
|
|
|
|
int fd = open(INI_config, O_WRONLY | O_CREAT);//创建文件
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
// 初始化INI解析器
|
|
|
|
|
INI_File *ini = ini_init();
|
|
|
|
|
if (!ini) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 解析INI文件
|
|
|
|
|
if (ini_parse(ini, INI_config) != 0)
|
|
|
|
|
{
|
|
|
|
|
LOG_E("Analysis failed");
|
|
|
|
|
ini_free(ini);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LOG_I("write config");
|
|
|
|
|
// 写入各种类型的值
|
|
|
|
|
ini_set_value(ini,"CONFIG", "ID",machine_ID);
|
|
|
|
|
ini_set_value(ini,"CONFIG", "NAME",machine_name);
|
|
|
|
|
ini_save(ini,INI_config);
|
|
|
|
|
// 清理资源
|
|
|
|
|
ini_free(ini);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int read_sample(void)
|
|
|
|
|
{
|
|
|
|
|
//检查文件
|
|
|
|
|
if (access(INI_config, F_OK) != 0)
|
|
|
|
|
{
|
|
|
|
|
LOG_E("NOT 828config");
|
|
|
|
|
int fd = open(INI_config, O_WRONLY | O_CREAT);//创建文件
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
// 初始化INI解析器
|
|
|
|
|
INI_File *ini = ini_init();
|
|
|
|
|
if (!ini) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
// 解析INI文件
|
|
|
|
|
if (ini_parse(ini, INI_config) != 0)
|
|
|
|
|
{
|
|
|
|
|
LOG_E("Retry");
|
|
|
|
|
rt_thread_mdelay(100);
|
|
|
|
|
if (ini_parse(ini, INI_config) != 0)
|
|
|
|
|
{
|
|
|
|
|
LOG_E("Analysis failed");
|
|
|
|
|
ini_free(ini);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
LOG_I("read config");
|
|
|
|
|
// 读取各种类型的值
|
|
|
|
|
strncpy(machine_ID, ini_get_value(ini, "CONFIG", "ID","99"),sizeof(machine_ID));
|
|
|
|
|
strncpy(machine_name, ini_get_value(ini, "CONFIG", "NAME","SC828"),sizeof(machine_name));
|
|
|
|
|
// 清理资源
|
|
|
|
|
ini_free(ini);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
INIT_APP_EXPORT(read_sample);
|