This repository has been archived on 2025-04-02. You can view files and clone it, but cannot push or open issues or pull requests.
controller-hart/User/application/inc/file_storage.h

126 lines
5.6 KiB
C

/**
* @file file_storage.h
* @brief 此文件定义了文件存储相关的数据结构和函数接口,用于管理文件存储块、模拟数据以及操作文件存储的基本功能。
* @details
* 该文件主要定义了以下几个关键部分:
* 1. 模拟数据结构体 `file_analog_data_t`,用于存储温度和湿度数据。
* 2. 文件存储块枚举 `file_storage_block_e`,定义了不同类型的文件存储块,如诊断日志、模式诊断日志等。
* 3. 手动测试标志联合体 `file_manual_flag_u`,包含复位、写入数据和读取数据等标志。
* 4. 文件存储块参数结构体 `file_storage_block_params_t`,用于描述文件存储块的基本参数,如节点大小、已使用节点等。
* 5. 文件存储记录时间结构体 `file_storage_record_time_t`,记录文件存储的日期、时间和时间戳。
* 6. 文件存储块结构体 `file_storage_block_t`,整合了手动测试标志、存储块参数、最后记录时间和写入使用时间等信息。
* 7. 文件存储结构体 `file_storage_t`,包含多个文件存储块、文件参数和读写操作函数指针。
* 8. 一系列文件存储操作函数,如初始化、创建、重置、读写等。
*
* @author [xxx]
* @date 2025-02-11
* @copyright Copyright (c) 2025
*
* @note
* - 使用此文件前,请确保 `main.h` 和 `board.h` 文件存在且包含必要的定义。
* - 对于文件存储块的操作,需要根据实际情况传入正确的参数,避免出现越界或错误操作。
* - 读写操作函数指针需要在初始化时正确赋值,以确保文件存储功能正常工作。
*/
#ifndef __FILE_STORAGE_H__
#define __FILE_STORAGE_H__
#include "lib.h"
#include "board.h"
typedef struct
{
float32 temperature; ///< 温度
float32 humidity; ///< 湿度
} file_analog_data_t;
typedef enum
{
FILE_BLOCK_DIAGNOSIS_LOG, ///< 诊断报警日志
FILE_BLOCK_EQUIPMENT_TRACEABILITY_INFORMATION, ///< 设备溯源信息
// more code here
FILE_BLOCK_MAX,
} file_storage_block_e;
typedef union
{
uint8_t data;
struct
{
uint8_t init : 1; ///< 初始化标志
uint8_t reset : 1; ///< 复位标志
uint8_t write_data : 1; ///< 模拟数据写入标志
uint8_t read_data : 1; ///< 读取数据标志
uint8_t cycle_write : 1;
} bits;
} file_manual_flag_u;
typedef struct
{
BOOL is_cycle_write; ///< 循环写入标志,目前该标志位只实现node
BOOL is_node; ///< 使用节点,写入数据是固定大小
uint8_t node_size; ///< 节点大小
uint16_t start_node; ///< 节点起始偏移索引,is_cycle_write为TRUE时有效
uint16_t used_node; ///< 已使用节点
uint32_t node_max; ///< 最大节点
uint32_t start_addr; ///< 起始地址
uint32_t end_addr; ///< 结束地址
uint32_t used_size; ///< 已使用大小
} file_storage_block_params_t;
typedef struct
{
rtc_date_t dd;
rtc_time_t tt;
uint32_t timestamp;
} file_storage_record_time_t;
typedef struct
{
file_manual_flag_u flag;
file_storage_block_e fb;
file_storage_block_params_t params;
file_storage_record_time_t last_record_time;
uint16_t write_used_time;
} file_storage_block_t;
typedef struct
{
file_storage_block_t *blocks;
struct
{
uint8_t block_size;
uint32_t file_size;
uint32_t used_size;
uint32_t free_size;
} params;
struct
{
BOOL(*read)
(uint32_t addr, uint8_t *buf, uint16_t size);
BOOL(*write)
(uint32_t addr, uint8_t *buf, uint16_t size);
} ops;
} file_storage_t;
typedef BOOL file_storage_write_callback(uint32_t addr, uint8_t *buf, uint16_t size);
typedef BOOL file_storage_read_callback(uint32_t addr, uint8_t *buf, uint16_t size);
void file_storage_init(file_storage_t *fs, uint32_t file_size, uint8_t block_size, file_storage_read_callback read_cb,
file_storage_write_callback write_cb); ///< 初始化文件存储
void file_storage_block_create(file_storage_t *fs, file_storage_block_e fb, file_storage_block_params_t *params); ///< 创建文件存储块
BOOL file_storage_block_is_active(file_storage_t *fs, file_storage_block_e fb); ///< 文件存储块是否激活
void file_storage_block_reset(file_storage_t *fs, file_storage_block_e fb); ///< 重置文件存储块
file_storage_block_t *file_storage_block_get(file_storage_t *fs, file_storage_block_e fb); ///< 获取文件存储块
BOOL file_storage_block_is_full(file_storage_t *fs, file_storage_block_e fb); ///< 文件存储块是否已满
BOOL file_storage_block_write(file_storage_t *fs, file_storage_block_e fb, uint32_t offset, const uint8_t *const data, uint16_t len); ///< 写入文件存储块数据
BOOL file_storage_block_write_node_append(file_storage_t *fs, file_storage_block_e fb, const uint8_t *const data); ///< 写入文件存储块节点
BOOL file_storage_block_read(file_storage_t *fs, file_storage_block_e fb, uint32_t offset, uint8_t *const data, uint8_t len); ///< 读取文件存储块数据
BOOL file_storage_block_read_node(file_storage_t *fs, file_storage_block_e fb, uint32_t offset, uint8_t *const data); ///< 读取文件存储块节点
void file_storage_block_read_nodes(file_storage_t *fs, file_storage_block_e fb, uint32_t node_index, uint8_t *const data, uint8_t count); ///< 读取文件存储块节点(多个)
#endif // __FILE_STORAGE_H__