Files
STM32F4-Base/simulator/sim/net_cfg_store_sim.c
T

68 lines
1.6 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.
/*
* 模块名称:Network Configuration Store (模拟器后端)
* 模块功能:net_cfg 存储后端——模拟器工作目录本地文件 sim_netcfg.bin
* (磁盘格式与 MCU 端一致,两端可互读)
* 适用平台:PC 模拟器
* 作者:王建锋
* 创建日期:2026-09-10
* 修改记录:
* v1.0 2026-09-10 王建锋 创建初始版本
*/
/* 头文件包含区 */
#include <stdio.h>
#include "net_cfg_store.h"
/* 静态变量定义区 */
static const char s_path[] = "sim_netcfg.bin";
/* 函数定义区 */
/*
* 函数功能:读取配置文件
* 入口参数:p_buf - 接收缓冲(不可为 NULL) uint8_t *
* len - 文件长度 size_t
* 返回值:0 - 成功;-1 - 文件缺失/读失败 int
* 限定条件:无
*/
int net_cfg_store_read(uint8_t *p_buf, size_t len) {
FILE *fp;
size_t n;
if (p_buf == NULL) {
return -1;
}
fp = fopen(s_path, "rb");
if (fp == NULL) {
return -1;
}
n = fread(p_buf, 1u, len, fp);
fclose(fp);
return (n == len) ? 0 : -1;
}
/*
* 函数功能:写配置文件(不存在则创建,存在则覆盖)
* 入口参数:p_buf - 待写数据(不可为 NULL) const uint8_t *
* len - 数据长度 size_t
* 返回值:0 - 成功;-1 - 写失败 int
* 限定条件:无
*/
int net_cfg_store_write(const uint8_t *p_buf, size_t len) {
FILE *fp;
size_t n;
if (p_buf == NULL) {
return -1;
}
fp = fopen(s_path, "wb");
if (fp == NULL) {
return -1;
}
n = fwrite(p_buf, 1u, len, fp);
fclose(fp);
return (n == len) ? 0 : -1;
}