Files
STM32F4-Base/App/cfg_file_store.h
T

87 lines
2.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* 模块名称:cfg_file_store
* 模块功能:cfg_file 平台存储后端接口——带路径的文件操作抽象;
* MCU 实现 = NAND 上 FatFS(cfg_file_store_mcu.c),
* 模拟器实现 = 宿主文件系统(cfg_file_store_sim.c)
* 适用平台:双端接口(实现分居两端)
* 作者:王建锋
* 创建日期:2026-09-14
* 修改记录:
* v1.0 2026-09-14 王建锋 创建初始版本
*/
#ifndef CFG_FILE_STORE_H
#define CFG_FILE_STORE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stddef.h>
/*
* 文件变更基线:size + mtime 变更令牌
* mtime 仅作等值比较(不等 = 可能变更):
* MCU 端 = FAT 打包时间(fdate<<16 | fmtime,分钟级粒度)
* 模拟器 = 宿主文件 mtime(秒级)
*/
typedef struct {
uint32_t size;
uint32_t mtime;
} cfg_file_stat_t;
/*
* 函数功能:读文件(最多 cap 字节)
* 入口参数:p_path - 文件路径 const char *
* p_buf - 接收缓冲(不可为 NULL) uint8_t *
* cap - 缓冲容量 size_t
* p_nread- 实际读字节数输出(不可为 NULL) size_t *
* 返回值:0 - 成功;-1 - 文件缺失/读失败 int
* 限定条件:调用方保证 p_buf 容量 ≥ 所需长度(引擎读上限 CFG_FILE_MAX_LEN)
*/
int cfg_store_read(const char *p_path, uint8_t *p_buf, size_t cap, size_t *p_nread);
/*
* 函数功能:原子写文件(先写临时文件,成功后改名覆盖目标)
* 入口参数:p_path - 目标文件路径 const char *
* p_buf - 待写数据(不可为 NULL) const uint8_t *
* len - 数据长度 size_t
* 返回值:0 - 成功;-1 - 写/改名失败(目标文件保持不动,临时文件清理) int
* 限定条件:中途失败不得损坏目标文件(掉电安全)
*/
int cfg_store_write_atomic(const char *p_path, const uint8_t *p_buf, size_t len);
/*
* 函数功能:文件状态(size + mtime 变更令牌)
* 入口参数:p_path - 文件路径 const char *
* p_st - 输出(不可为 NULL) cfg_file_stat_t *
* 返回值:0 - 成功;-1 - 文件缺失/读失败 int
* 限定条件:无
*/
int cfg_store_stat(const char *p_path, cfg_file_stat_t *p_st);
/*
* 函数功能:删除文件
* 入口参数:p_path - 文件路径 const char *
* 返回值:0 - 成功;-1 - 文件缺失/删除失败 int
* 限定条件:无
*/
int cfg_store_remove(const char *p_path);
/* 函数功能:取配置文件路径(如 MCU "/config.ini"、模拟器 "sim_config.ini") */
const char *cfg_store_config_path(void);
/* 函数功能:取配置文件临时文件路径(write_atomic 内部用) */
const char *cfg_store_tmp_path(void);
/* 函数功能:取旧版配置遗留文件路径(net_cfg_legacy 迁移用) */
const char *cfg_store_legacy_path(void);
#ifdef __cplusplus
}
#endif
#endif /* CFG_FILE_STORE_H */