1266 lines
66 KiB
C
1266 lines
66 KiB
C
/* 主示例统一INI V2:标准参数、远程参数和TEMP文件一次提交。
|
||
* V1和旧旁文件仅用于迁移读取;调用方串行访问,不分配C堆。
|
||
* INI格式与接口约束见头文件,旧TEMP字节布局见迁移实现区。
|
||
* 运行生效、文件保存和网络确认由调用方分别处理。 */
|
||
#include "iec104_example_config.h"
|
||
#include "iec104_example_points.h"
|
||
#include <windows.h>
|
||
#include <errno.h>
|
||
#include <float.h>
|
||
#include <locale.h>
|
||
#include <math.h>
|
||
#include <stdint.h>
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
|
||
#define CONFIG_MAX_FILE_BYTES 65536U
|
||
#define CONFIG_LINE_SIZE 512U
|
||
#define CONFIG_READ_SIZE 256U
|
||
#define CONFIG_TEMP_PATH_SIZE 1024U
|
||
#define CONFIG_TEMP_HEADER_BYTES 23U
|
||
#define CONFIG_TEMP_MAX_BYTES 4096U
|
||
#define CONFIG_TEMP_MAX_NAME 223U
|
||
#define CONFIG_HEX_CHUNK_BYTES 128U
|
||
#define CONFIG_HEX_CHUNKS (CONFIG_TEMP_MAX_BYTES / CONFIG_HEX_CHUNK_BYTES)
|
||
|
||
_Static_assert(sizeof(float) == sizeof(uint32_t) && FLT_RADIX == 2 &&
|
||
FLT_MANT_DIG == 24 && FLT_MAX_EXP == 128, "Config requires IEEE754 binary32 float");
|
||
|
||
static unsigned long config_temp_serial; /* 串行调用;仅命名临时文件,不写入配置。 */
|
||
|
||
typedef struct {
|
||
HANDLE file;
|
||
uint8_t bytes[CONFIG_READ_SIZE];
|
||
DWORD remaining, position, available;
|
||
bool first;
|
||
} ConfigReader;
|
||
|
||
/**
|
||
* 功能:校验当前区号、共享死区及五个区的复归/X时间范围,浮点范围比较同时排除NaN。
|
||
* 输入参数:values——五区远程定值快照。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:当前区号、共享死区和五套定值均有效时返回true,否则返回false。
|
||
* 说明:无。
|
||
*/
|
||
static bool config_remote_valid(const ExampleRemoteConfig *values);
|
||
/**
|
||
* 功能:校验显式长度的TEMP/文件名,后缀只允许字母、数字、下划线、连字符和点。
|
||
* 输入参数:name——文件名或显式长度的协议名称;length——本次数据或名称的有效字节数。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:名称属于允许的TEMP单层文件名时返回true,否则返回false。
|
||
* 说明:后缀不得以点开头或包含路径分隔符;不要求输入以NUL结尾。
|
||
*/
|
||
static bool config_temp_name_valid(const uint8_t *name, uint8_t length);
|
||
/**
|
||
* 功能:按旧二进制快照的规范头、名称和正文计算统一INI使用的CRC,仅成功时填写checksum。
|
||
* 输入参数:ca——公共地址;temp——已提交或待提交的TEMP快照;encoded_time——可选原始CP56编码;checksum——CRC校验值回写位置。
|
||
* 输出参数:checksum——CRC校验值回写位置。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:读取路径可提供原始encoded_time覆盖重编码时标,避免CP56中未保留的位改变校验输入。
|
||
*/
|
||
static bool config_temp_checksum(uint16_t ca, const ExampleTempConfig *temp,
|
||
const uint8_t *encoded_time, uint32_t *checksum);
|
||
/**
|
||
* 功能:将已校验的共享远程定值及全部五区写入统一INI,浮点文本按规范小数点编码。
|
||
* 输入参数:file——已打开的同步文件句柄;values——五区远程定值快照。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:格式化或写入失败返回false,已写片段留给上层丢弃整个候选文件。
|
||
*/
|
||
static bool config_remote_write(HANDLE file, const ExampleRemoteConfig *values);
|
||
/**
|
||
* 功能:写TEMP存在标志,或已校验快照的元数据、CRC和每块最多128字节的十六进制正文。
|
||
* 输入参数:file——已打开的同步文件句柄;temp——已提交或待提交的TEMP快照;checksum——已计算的CRC校验值。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:任一格式化/写入失败即返回false,候选文件的发布或丢弃由外层原子保存处理。
|
||
*/
|
||
static bool config_temp_write(HANDLE file, const ExampleTempConfig *temp, uint32_t checksum);
|
||
/**
|
||
* 功能:只读加载旧.remote.ini迁移文件,区分不存在、内容非法和I/O错误。
|
||
* 输入参数:base——主配置文件路径;ca——站点公共地址;values——五区远程定值快照。
|
||
* 输出参数:values——五区远程定值快照。
|
||
* 返回值:配置操作状态:IO_ERROR、INVALID、NOT_FOUND、OK;OK为成功,其他值保留具体失败原因。
|
||
* 说明:在文件解析及关闭均成功后才改写values,不回写或删除旧文件。
|
||
*/
|
||
static ExampleConfigResult config_legacy_remote_load(const char *base, uint16_t ca, ExampleRemoteConfig *values);
|
||
/**
|
||
* 功能:只读校验旧TEMP快照的版本、CA、名称、精确文件长度、CP56及CRC后发布元数据。
|
||
* 输入参数:base——主配置文件路径;ca——站点公共地址;name——文件名或显式长度的协议名称;name_length——文件名长度回写位置;bytes——字节数据缓冲;size——实际字节数回写位置;time——CP56日历时标回写位置。
|
||
* 输出参数:name——文件名或显式长度的协议名称;name_length——文件名长度回写位置;bytes——字节数据缓冲;size——实际字节数回写位置;time——CP56日历时标回写位置。
|
||
* 返回值:配置操作状态:IO_ERROR、INVALID、NOT_FOUND、OK;OK为成功,其他值保留具体失败原因。
|
||
* 说明:正文先读入调用方bytes候选区,失败可能留下部分正文;名称、长度和时标仅OK时发布。
|
||
*/
|
||
static ExampleConfigResult config_legacy_temp_load(const char *base, uint16_t ca,
|
||
uint8_t *name, uint8_t *name_length, uint8_t *bytes, uint32_t *size, IEC60870_CP56Time2a_t *time);
|
||
|
||
/**
|
||
* 功能:调用参数先校验,不打开文件也不修改输出;INI以IOA命名节,因此同一CA内IOA必须唯一。
|
||
* 输入参数:path——NUL结尾的宿主文件路径;points——参数点表;values——与点表同序的参数运行值数组;count——参数点表元素数。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:路径、点表和值域入口约束全部满足时返回true,否则返回false。
|
||
* 说明:无。
|
||
*/
|
||
static bool config_arguments_valid(const char *path, const IEC104_ParameterPoint_t *points,
|
||
const IEC104_ParameterValue_t *values, size_t count)
|
||
{
|
||
size_t i, j;
|
||
if (path == NULL || path[0] == '\0' || points == NULL || values == NULL ||
|
||
count == 0U || count > EXAMPLE_CONFIG_MAX_POINTS) { return false; }
|
||
for (i = 0U; i < count; ++i) {
|
||
const IEC104_ParameterPoint_t *point = &points[i];
|
||
if (point->common_address == 0U || point->common_address == UINT16_MAX ||
|
||
point->common_address != points[0].common_address || point->ioa == 0U ||
|
||
point->ioa > UINT32_C(0xffffff) || point->type_id < 110U || point->type_id > 112U ||
|
||
!((point->kind >= 1U && point->kind <= 4U) ||
|
||
(point->kind >= 32U && point->kind <= 63U))) { return false; }
|
||
for (j = 0U; j < i; ++j) { if (points[j].ioa == point->ioa) { return false; } }
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:从带长度边界的读取器取得下一个配置字节。
|
||
* 输入参数:reader——带文件长度和读取游标的配置读取器;value——单字节读取结果回写位置。
|
||
* 输出参数:reader——本次操作更新的实例或工作区;value——单字节读取结果回写位置。
|
||
* 返回值:1表示取得一字节,0表示到达长度边界,-1表示读取失败。
|
||
* 说明:返回1/0/-1分别为取得一字节、已读到文件长度边界、I/O失败;短读不当作正常EOF。
|
||
* 文件打开时不共享写权限,解析过程中不会与原地写入者混用两份数据。
|
||
*/
|
||
static int config_read_byte(ConfigReader *reader, uint8_t *value)
|
||
{
|
||
if (reader->position == reader->available) {
|
||
DWORD wanted;
|
||
if (reader->remaining == 0U) { return 0; }
|
||
wanted = reader->remaining < CONFIG_READ_SIZE ? reader->remaining : CONFIG_READ_SIZE;
|
||
if (!ReadFile(reader->file, reader->bytes, wanted, &reader->available, NULL) ||
|
||
reader->available == 0U || reader->available > wanted) { return -1; }
|
||
reader->remaining -= reader->available;
|
||
reader->position = 0U;
|
||
}
|
||
*value = reader->bytes[reader->position++];
|
||
return 1;
|
||
}
|
||
|
||
/**
|
||
* 功能:读取并验证下一条配置文本行。
|
||
* 输入参数:reader——带文件长度和读取游标的配置读取器;line——配置行缓冲。
|
||
* 输出参数:reader——本次操作更新的实例或工作区;line——配置行缓冲。
|
||
* 返回值:1表示取得完整行,0表示无后续行,-1表示I/O失败,-2表示文本非法。
|
||
* 说明:返回1为完整行(末行可无换行),0为无后续行,-1为I/O失败,-2为非法文本。
|
||
* 起始BOM只认一次;NUL、孤立CR和其他控制字节不能靠截断字符串绕过后续校验。
|
||
*/
|
||
static int config_read_line(ConfigReader *reader, char *line)
|
||
{
|
||
uint8_t byte;
|
||
size_t length = 0U;
|
||
int result = config_read_byte(reader, &byte);
|
||
if (result <= 0) { return result; }
|
||
if (reader->first) {
|
||
reader->first = false;
|
||
if (byte == 0xefU) {
|
||
result = config_read_byte(reader, &byte);
|
||
if (result != 1 || byte != 0xbbU) { return result < 0 ? -1 : -2; }
|
||
result = config_read_byte(reader, &byte);
|
||
if (result != 1 || byte != 0xbfU) { return result < 0 ? -1 : -2; }
|
||
result = config_read_byte(reader, &byte);
|
||
if (result <= 0) { return result; }
|
||
}
|
||
}
|
||
for (;;) {
|
||
if (byte == '\r') {
|
||
result = config_read_byte(reader, &byte);
|
||
if (result != 1 || byte != '\n') { return result < 0 ? -1 : -2; }
|
||
line[length] = '\0'; return 1;
|
||
}
|
||
if (byte == '\n') { line[length] = '\0'; return 1; }
|
||
if ((byte < 0x20U && byte != '\t') || byte == 0x7fU ||
|
||
length == CONFIG_LINE_SIZE - 1U) { return -2; }
|
||
line[length++] = (char)byte;
|
||
result = config_read_byte(reader, &byte);
|
||
if (result <= 0) { line[length] = '\0'; return result < 0 ? -1 : 1; }
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 功能:原地去掉首尾空格和制表符,返回首个有效字符;返回指针可能位于原缓冲内部。
|
||
* 输入参数:text——文本或格式化缓冲。
|
||
* 输出参数:text——文本或格式化缓冲。
|
||
* 返回值:指向首个有效字符的借用指针,可能位于原缓冲内部。
|
||
* 说明:无。
|
||
*/
|
||
static char *config_trim(char *text)
|
||
{
|
||
char *end;
|
||
while (*text == ' ' || *text == '\t') { ++text; }
|
||
end = text + strlen(text);
|
||
while (end > text && (end[-1] == ' ' || end[-1] == '\t')) { --end; }
|
||
*end = '\0';
|
||
return text;
|
||
}
|
||
|
||
/**
|
||
* 功能:元数据只接受无符号十进制,先验乘加上界,防止长数字回绕后意外匹配合法版本/身份。
|
||
* 输入参数:text——文本或格式化缓冲;maximum——允许的数值上限;value——解析结果回写位置。
|
||
* 输出参数:value——解析结果回写位置。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:无。
|
||
*/
|
||
static bool config_unsigned(const char *text, uint32_t maximum, uint32_t *value)
|
||
{
|
||
uint32_t number = 0U;
|
||
if (*text < '0' || *text > '9') { return false; }
|
||
do {
|
||
uint32_t digit = (uint32_t)(*text - '0');
|
||
if (number > maximum / 10U ||
|
||
(number == maximum / 10U && digit > maximum % 10U)) { return false; }
|
||
number = number * 10U + digit;
|
||
++text;
|
||
} while (*text >= '0' && *text <= '9');
|
||
if (*text != '\0') { return false; }
|
||
*value = number;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:严格解析可带正负号的十进制int16,不接受尾部字符;仅成功时改写value。
|
||
* 输入参数:text——文本或格式化缓冲;value——解析结果回写位置。
|
||
* 输出参数:value——解析结果回写位置。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:无。
|
||
*/
|
||
static bool config_signed(const char *text, int16_t *value)
|
||
{
|
||
bool negative = *text == '-';
|
||
uint32_t magnitude;
|
||
if (*text == '-' || *text == '+') { ++text; }
|
||
if (!config_unsigned(text, negative ? 32768U : 32767U, &magnitude)) { return false; }
|
||
*value = (int16_t)(negative ? -(int32_t)magnitude : (int32_t)magnitude);
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:先限制为十进制语法,再交CRT舍入到float;不能让strtof接受hex、NaN、Inf或尾部垃圾。
|
||
* 输入参数:text——文本或格式化缓冲;value——解析结果回写位置。
|
||
* 输出参数:value——解析结果回写位置。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:用当前CRT小数点替换输入中的点号,不修改全局locale;调用方串行且不得并发setlocale。
|
||
* ERANGE可能只是可表示的subnormal,不能一律拒绝;非零尾数下溢为0仍按错误处理。
|
||
*/
|
||
static bool config_float(const char *text, float *value)
|
||
{
|
||
const char *cursor = text, *dot = NULL, *decimal;
|
||
char localized[CONFIG_LINE_SIZE], *end;
|
||
size_t digits = 0U, prefix, decimal_length, suffix;
|
||
bool nonzero = false;
|
||
float number;
|
||
if (*cursor == '-' || *cursor == '+') { ++cursor; }
|
||
while (*cursor >= '0' && *cursor <= '9') {
|
||
nonzero = nonzero || *cursor != '0'; ++digits; ++cursor;
|
||
}
|
||
if (*cursor == '.') {
|
||
dot = cursor++;
|
||
while (*cursor >= '0' && *cursor <= '9') {
|
||
nonzero = nonzero || *cursor != '0'; ++digits; ++cursor;
|
||
}
|
||
}
|
||
if (digits == 0U) { return false; }
|
||
if (*cursor == 'e' || *cursor == 'E') {
|
||
++cursor;
|
||
if (*cursor == '-' || *cursor == '+') { ++cursor; }
|
||
if (*cursor < '0' || *cursor > '9') { return false; }
|
||
do { ++cursor; } while (*cursor >= '0' && *cursor <= '9');
|
||
}
|
||
if (*cursor != '\0') { return false; }
|
||
decimal = localeconv()->decimal_point;
|
||
if (decimal == NULL || decimal[0] == '\0') { return false; }
|
||
if (dot != NULL) {
|
||
prefix = (size_t)(dot - text); decimal_length = strlen(decimal); suffix = strlen(dot + 1);
|
||
if (prefix + decimal_length + suffix >= sizeof(localized)) { return false; }
|
||
memcpy(localized, text, prefix);
|
||
memcpy(localized + prefix, decimal, decimal_length);
|
||
memcpy(localized + prefix + decimal_length, dot + 1, suffix + 1U);
|
||
} else {
|
||
if (strlen(text) >= sizeof(localized)) { return false; }
|
||
strcpy(localized, text);
|
||
}
|
||
errno = 0;
|
||
number = strtof(localized, &end);
|
||
if (*end != '\0' || !isfinite(number) || (errno != 0 && errno != ERANGE) ||
|
||
(nonzero && number == 0.0f)) { return false; }
|
||
*value = number;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:单个十六进制字符转0..15,兼容大小写;其他字符返回-1。
|
||
* 输入参数:ch——待解码字符。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:合法十六进制字符返回0..15,否则返回-1。
|
||
* 说明:无。
|
||
*/
|
||
static int config_hex_digit(char ch)
|
||
{
|
||
if (ch >= '0' && ch <= '9') { return ch - '0'; }
|
||
if (ch >= 'a' && ch <= 'f') { return ch - 'a' + 10; }
|
||
if (ch >= 'A' && ch <= 'F') { return ch - 'A' + 10; }
|
||
return -1;
|
||
}
|
||
|
||
/**
|
||
* 功能:将恰好size*2个十六进制字符解码为size字节,不允许空格或额外字符。
|
||
* 输入参数:text——文本或格式化缓冲;bytes——字节数据缓冲;size——本次数据的字节数。
|
||
* 输出参数:bytes——字节数据缓冲。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:中途遇到非法字符返回false,输出前缀可能已写入,调用方须使用暂存缓冲。
|
||
*/
|
||
static bool config_hex_bytes(const char *text, uint8_t *bytes, size_t size)
|
||
{
|
||
size_t i;
|
||
if (strlen(text) != size * 2U) { return false; }
|
||
for (i = 0U; i < size; ++i) {
|
||
int high = config_hex_digit(text[i * 2U]), low = config_hex_digit(text[i * 2U + 1U]);
|
||
if (high < 0 || low < 0) { return false; }
|
||
bytes[i] = (uint8_t)((unsigned)high * 16U + (unsigned)low);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:解析统一INI中的标准参数、远程定值和TEMP候选。
|
||
* 输入参数:reader——带文件长度和读取游标的配置读取器;points——参数点表;prepared——尚未发布的配置候选;count——参数点表元素数;state——持久配置与TEMP候选状态。
|
||
* 输出参数:prepared——尚未发布的配置候选;state——持久配置与TEMP候选状态。
|
||
* 返回值:配置操作状态:INVALID、IO_ERROR、OK;OK为成功,其他值保留具体失败原因。
|
||
* 说明:标准参数的掩码:1=Type/版本,2=KPA/CA,4=值/数量,8=参数节出现。
|
||
* 节和键可乱序,TEMP直接写调用方暂存;全文件验证后才能发布。
|
||
* parameter_后的数字按十进制解析,并按调用方当前点表匹配实际IOA;
|
||
* 主示例新点表不含旧地址5001,因此旧节名被拒绝,不在加载时猜测或迁移映射。
|
||
*/
|
||
static ExampleConfigResult config_parse(ConfigReader *reader, const IEC104_ParameterPoint_t *points,
|
||
IEC104_ParameterValue_t *prepared, size_t count, ExamplePersistentConfig *state)
|
||
{
|
||
char line[CONFIG_LINE_SIZE];
|
||
uint8_t masks[EXAMPLE_CONFIG_MAX_POINTS] = {0};
|
||
uint8_t areas[5] = {0}, chunk_lengths[CONFIG_HEX_CHUNKS] = {0}, temp_time[IEC60870_CP56_LEN] = {0};
|
||
unsigned config_mask = 0U, remote_mask = 0U, temp_mask = 0U;
|
||
uint32_t version = 0U, stored_crc = 0U;
|
||
size_t selected = count + 8U, i;
|
||
bool config_seen = false, temp_seen = false;
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
uint8_t dtu_seen[DTU_SETTINGS_AREAS][DTU_SETTINGS_COUNT] = {{0}};
|
||
uint8_t dtu_areas = 0U, dtu_mask = 0U;
|
||
selected = count + 14U;
|
||
#endif
|
||
int result;
|
||
memset(prepared, 0, count * sizeof(*prepared));
|
||
while ((result = config_read_line(reader, line)) == 1) {
|
||
char *text, *delimiter, *key, *value;
|
||
uint32_t number;
|
||
unsigned bit;
|
||
delimiter = strpbrk(line, ";#");
|
||
if (delimiter != NULL) { *delimiter = '\0'; }
|
||
text = config_trim(line);
|
||
if (*text == '\0') { continue; }
|
||
if (*text == '[') {
|
||
delimiter = strchr(text, ']');
|
||
if (delimiter == NULL || delimiter[1] != '\0') { return EXAMPLE_CONFIG_INVALID; }
|
||
*delimiter = '\0'; text = config_trim(text + 1);
|
||
if (strcmp(text, "config") == 0) {
|
||
if (config_seen) { return EXAMPLE_CONFIG_INVALID; }
|
||
config_seen = true; selected = count;
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
} else if (strcmp(text, "dtu") == 0) {
|
||
if (dtu_mask != 0U) { return EXAMPLE_CONFIG_INVALID; }
|
||
dtu_mask = 2U; selected = count + 8U;
|
||
} else if (strlen(text) == 10U && strncmp(text, "dtu_area_", 9U) == 0 &&
|
||
text[9] >= '0' && text[9] <= '4') {
|
||
unsigned area = (unsigned)(text[9] - '0');
|
||
if ((dtu_areas & (1U << area)) != 0U) { return EXAMPLE_CONFIG_INVALID; }
|
||
dtu_areas |= (uint8_t)(1U << area); selected = count + 9U + area;
|
||
#endif
|
||
} else if (strcmp(text, "remote") == 0) {
|
||
if (remote_mask != 0U) { return EXAMPLE_CONFIG_INVALID; }
|
||
remote_mask = 4U; selected = count + 1U;
|
||
} else if (strcmp(text, "temp") == 0) {
|
||
if (temp_seen) { return EXAMPLE_CONFIG_INVALID; }
|
||
temp_seen = true; selected = count + 2U;
|
||
state->temp.present = false; state->temp.name_length = 0U; state->temp.size = 0U;
|
||
memset(&state->temp.time, 0, sizeof(state->temp.time));
|
||
} else if (strlen(text) == 6U && strncmp(text, "area_", 5U) == 0 &&
|
||
text[5] >= '0' && text[5] <= '4') {
|
||
i = (size_t)(text[5] - '0');
|
||
if (areas[i] != 0U) { return EXAMPLE_CONFIG_INVALID; }
|
||
areas[i] = 4U; selected = count + 3U + i;
|
||
} else {
|
||
if (strncmp(text, "parameter_", 10U) != 0 ||
|
||
!config_unsigned(text + 10U, UINT32_C(0xffffff), &number)) {
|
||
return EXAMPLE_CONFIG_INVALID;
|
||
}
|
||
for (i = 0U; i < count && points[i].ioa != number; ++i) { }
|
||
if (i == count || masks[i] != 0U) { return EXAMPLE_CONFIG_INVALID; }
|
||
masks[i] = 8U; selected = i;
|
||
}
|
||
continue;
|
||
}
|
||
delimiter = strchr(text, '=');
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
if (delimiter == NULL || selected > count + 13U) { return EXAMPLE_CONFIG_INVALID; }
|
||
#else
|
||
if (delimiter == NULL || selected > count + 7U) { return EXAMPLE_CONFIG_INVALID; }
|
||
#endif
|
||
*delimiter = '\0'; key = config_trim(text); value = config_trim(delimiter + 1);
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
if (selected == count + 8U) {
|
||
if (strcmp(key, "current_area") != 0 || (dtu_mask & 1U) != 0U ||
|
||
!config_unsigned(value, 4U, &number)) { return EXAMPLE_CONFIG_INVALID; }
|
||
state->dtu->current_area = (uint16_t)number; dtu_mask |= 1U; continue;
|
||
}
|
||
if (selected >= count + 9U) {
|
||
uint16_t area = (uint16_t)(selected - count - 9U);
|
||
double parsed;
|
||
if (!config_unsigned(key, UINT32_C(0xffffff), &number)) { return EXAMPLE_CONFIG_INVALID; }
|
||
i = Dtu_Settings_Index(number);
|
||
if (!Dtu_Settings_Visible(i, area) || dtu_seen[area][i] != 0U) { return EXAMPLE_CONFIG_INVALID; }
|
||
if (Dtu_Settings_Tag(i) == 38U) {
|
||
float floating;
|
||
if (!config_float(value, &floating)) { return EXAMPLE_CONFIG_INVALID; }
|
||
parsed = floating;
|
||
} else {
|
||
if (!config_unsigned(value, UINT32_MAX, &number)) { return EXAMPLE_CONFIG_INVALID; }
|
||
parsed = (double)number;
|
||
}
|
||
if (!Dtu_Settings_Value_Valid(i, parsed)) { return EXAMPLE_CONFIG_INVALID; }
|
||
state->dtu->values[area][i] = parsed; dtu_seen[area][i] = 1U; continue;
|
||
}
|
||
#endif
|
||
if (selected == count) {
|
||
uint32_t expected;
|
||
if (strcmp(key, "version") == 0) {
|
||
if ((config_mask & 1U) != 0U || !config_unsigned(value, 2U, &version) || version == 0U) {
|
||
return EXAMPLE_CONFIG_INVALID;
|
||
}
|
||
config_mask |= 1U; continue;
|
||
} else if (strcmp(key, "common_address") == 0) { bit = 2U; expected = points[0].common_address; }
|
||
else if (strcmp(key, "point_count") == 0) { bit = 4U; expected = (uint32_t)count; }
|
||
else { return EXAMPLE_CONFIG_INVALID; }
|
||
if ((config_mask & bit) != 0U || !config_unsigned(value, expected, &number) || number != expected) {
|
||
return EXAMPLE_CONFIG_INVALID;
|
||
}
|
||
config_mask |= bit;
|
||
} else if (selected < count) {
|
||
if (strcmp(key, "type") == 0) {
|
||
bit = 1U;
|
||
if (!config_unsigned(value, UINT8_MAX, &number) || number != points[selected].type_id) {
|
||
return EXAMPLE_CONFIG_INVALID;
|
||
}
|
||
} else if (strcmp(key, "kpa") == 0) {
|
||
bit = 2U;
|
||
if (!config_unsigned(value, UINT8_MAX, &number) || number != points[selected].kind) {
|
||
return EXAMPLE_CONFIG_INVALID;
|
||
}
|
||
} else if (strcmp(key, "value") == 0) {
|
||
bit = 4U;
|
||
if (points[selected].type_id == 112U) {
|
||
if (!config_float(value, &prepared[selected].short_float)) { return EXAMPLE_CONFIG_INVALID; }
|
||
} else {
|
||
int16_t signed_value;
|
||
if (!config_signed(value, &signed_value)) { return EXAMPLE_CONFIG_INVALID; }
|
||
if (points[selected].type_id == 110U) { prepared[selected].normalized = signed_value; }
|
||
else { prepared[selected].scaled = signed_value; }
|
||
}
|
||
} else { return EXAMPLE_CONFIG_INVALID; }
|
||
if ((masks[selected] & bit) != 0U) { return EXAMPLE_CONFIG_INVALID; }
|
||
masks[selected] |= (uint8_t)bit;
|
||
} else if (selected == count + 1U) {
|
||
if (strcmp(key, "current_area") == 0) {
|
||
if (!config_unsigned(value, 4U, &number)) { return EXAMPLE_CONFIG_INVALID; }
|
||
state->remote.current_area = (uint16_t)number; bit = 1U;
|
||
} else if (strcmp(key, "deadband") == 0) {
|
||
if (!config_float(value, &state->remote.deadband)) { return EXAMPLE_CONFIG_INVALID; }
|
||
bit = 2U;
|
||
} else { return EXAMPLE_CONFIG_INVALID; }
|
||
if ((remote_mask & bit) != 0U) { return EXAMPLE_CONFIG_INVALID; }
|
||
remote_mask |= bit;
|
||
} else if (selected >= count + 3U) {
|
||
i = selected - count - 3U;
|
||
if (strcmp(key, "reset_seconds") == 0) {
|
||
if (!config_unsigned(value, 3600U, &number)) { return EXAMPLE_CONFIG_INVALID; }
|
||
state->remote.reset_seconds[i] = (uint16_t)number; bit = 1U;
|
||
} else if (strcmp(key, "x_time") == 0) {
|
||
if (!config_float(value, &state->remote.x_time[i])) { return EXAMPLE_CONFIG_INVALID; }
|
||
bit = 2U;
|
||
} else { return EXAMPLE_CONFIG_INVALID; }
|
||
if ((areas[i] & bit) != 0U) { return EXAMPLE_CONFIG_INVALID; }
|
||
areas[i] |= (uint8_t)bit;
|
||
} else {
|
||
if (strcmp(key, "present") == 0) {
|
||
if (!config_unsigned(value, 1U, &number)) { return EXAMPLE_CONFIG_INVALID; }
|
||
state->temp.present = number != 0U; bit = 1U;
|
||
} else if (strcmp(key, "name") == 0) {
|
||
size_t length = strlen(value);
|
||
if (length > CONFIG_TEMP_MAX_NAME ||
|
||
!config_temp_name_valid((const uint8_t *)value, (uint8_t)length)) { return EXAMPLE_CONFIG_INVALID; }
|
||
state->temp.name_length = (uint8_t)length;
|
||
memcpy(state->temp.name, value, length); bit = 2U;
|
||
} else if (strcmp(key, "size") == 0) {
|
||
if (!config_unsigned(value, CONFIG_TEMP_MAX_BYTES, &state->temp.size)) { return EXAMPLE_CONFIG_INVALID; }
|
||
bit = 4U;
|
||
} else if (strcmp(key, "time") == 0) {
|
||
if (!config_hex_bytes(value, temp_time, sizeof(temp_time)) ||
|
||
!IEC60870_Time_From_CP56(temp_time, sizeof(temp_time), &state->temp.time)) { return EXAMPLE_CONFIG_INVALID; }
|
||
bit = 8U;
|
||
} else if (strcmp(key, "crc32") == 0) {
|
||
uint8_t encoded[4];
|
||
if (!config_hex_bytes(value, encoded, sizeof(encoded))) { return EXAMPLE_CONFIG_INVALID; }
|
||
stored_crc = ((uint32_t)encoded[0] << 24) | ((uint32_t)encoded[1] << 16) |
|
||
((uint32_t)encoded[2] << 8) | encoded[3];
|
||
bit = 16U;
|
||
} else if (strncmp(key, "data_", 5U) == 0) {
|
||
size_t length = strlen(value);
|
||
if (!config_unsigned(key + 5U, CONFIG_HEX_CHUNKS - 1U, &number) ||
|
||
length == 0U || length > CONFIG_HEX_CHUNK_BYTES * 2U || (length & 1U) != 0U ||
|
||
chunk_lengths[number] != 0U ||
|
||
!config_hex_bytes(value, state->temp.bytes + number * CONFIG_HEX_CHUNK_BYTES, length / 2U)) {
|
||
return EXAMPLE_CONFIG_INVALID;
|
||
}
|
||
chunk_lengths[number] = (uint8_t)(length / 2U); continue;
|
||
} else { return EXAMPLE_CONFIG_INVALID; }
|
||
if ((temp_mask & bit) != 0U) { return EXAMPLE_CONFIG_INVALID; }
|
||
temp_mask |= bit;
|
||
}
|
||
}
|
||
if (result == -1) { return EXAMPLE_CONFIG_IO_ERROR; }
|
||
if (result != 0 || !config_seen || config_mask != 7U) { return EXAMPLE_CONFIG_INVALID; }
|
||
for (i = 0U; i < count; ++i) { if (masks[i] != 15U) { return EXAMPLE_CONFIG_INVALID; } }
|
||
if (version == 1U) {
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
if (dtu_mask != 0U || dtu_areas != 0U) { return EXAMPLE_CONFIG_INVALID; }
|
||
#endif
|
||
if (remote_mask != 0U || temp_seen) { return EXAMPLE_CONFIG_INVALID; }
|
||
for (i = 0U; i < 5U; ++i) { if (areas[i] != 0U) { return EXAMPLE_CONFIG_INVALID; } }
|
||
state->legacy = true;
|
||
} else {
|
||
uint32_t crc = 0U, remaining = state->temp.present ? state->temp.size : 0U;
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
size_t area;
|
||
if (dtu_mask != 3U || dtu_areas != 31U || !Dtu_Settings_Valid(state->dtu) ||
|
||
state->dtu->current_area != state->remote.current_area) { return EXAMPLE_CONFIG_INVALID; }
|
||
for (area = 0U; area < DTU_SETTINGS_AREAS; ++area) {
|
||
for (i = 0U; i < DTU_SETTINGS_COUNT; ++i) {
|
||
if ((dtu_seen[area][i] != 0U) != Dtu_Settings_Visible(i, (uint16_t)area)) {
|
||
return EXAMPLE_CONFIG_INVALID;
|
||
}
|
||
}
|
||
}
|
||
#endif
|
||
if (remote_mask != 7U || !config_remote_valid(&state->remote) || !temp_seen ||
|
||
temp_mask != (state->temp.present ? 31U : 1U)) { return EXAMPLE_CONFIG_INVALID; }
|
||
for (i = 0U; i < 5U; ++i) { if (areas[i] != 7U) { return EXAMPLE_CONFIG_INVALID; } }
|
||
for (i = 0U; i < CONFIG_HEX_CHUNKS; ++i) {
|
||
uint32_t expected = remaining < CONFIG_HEX_CHUNK_BYTES ? remaining : CONFIG_HEX_CHUNK_BYTES;
|
||
if (chunk_lengths[i] != expected) { return EXAMPLE_CONFIG_INVALID; }
|
||
remaining -= expected;
|
||
}
|
||
if (state->temp.present && (!config_temp_checksum(points[0].common_address, &state->temp, temp_time, &crc) ||
|
||
crc != stored_crc)) {
|
||
return EXAMPLE_CONFIG_INVALID;
|
||
}
|
||
state->legacy = false;
|
||
}
|
||
return EXAMPLE_CONFIG_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:加载统一INI;缺失或V1配置才尝试迁移旧remote/TEMP旁文件,V2是唯一权威来源。
|
||
* 输入参数:path——NUL结尾的宿主文件路径;points——参数点表;values——与点表同序的参数运行值数组;count——参数点表元素数;state——持久配置与TEMP候选状态。
|
||
* 输出参数:values——与点表同序的参数运行值数组;state——持久配置与TEMP候选状态。
|
||
* 返回值:配置操作状态:IO_ERROR、INVALID、OK、NOT_FOUND;OK为成功,其他值保留具体失败原因。
|
||
* 说明:全部校验及关闭文件成功后发布values/state;TEMP内容缓冲仍可能在失败前被暂写,
|
||
* 因此调用方须提供候选TEMP缓冲,不能把结构未发布误解为缓冲内容完全未变。
|
||
*/
|
||
ExampleConfigResult Example_Config_Load(const char *path,
|
||
const IEC104_ParameterPoint_t *points, IEC104_ParameterValue_t *values, size_t count,
|
||
ExamplePersistentConfig *state)
|
||
{
|
||
IEC104_ParameterValue_t prepared[EXAMPLE_CONFIG_MAX_POINTS];
|
||
ExamplePersistentConfig prepared_state;
|
||
ConfigReader reader = {0};
|
||
LARGE_INTEGER size;
|
||
ExampleConfigResult result = EXAMPLE_CONFIG_IO_ERROR;
|
||
bool found = false;
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
DtuSettings prepared_dtu;
|
||
DtuSettings *published_dtu;
|
||
if (state == NULL || state->dtu == NULL) { return EXAMPLE_CONFIG_INVALID; }
|
||
prepared_dtu = *state->dtu;
|
||
published_dtu = state->dtu;
|
||
#endif
|
||
if (!config_arguments_valid(path, points, values, count) || state == NULL ||
|
||
state->temp.name == NULL || state->temp.bytes == NULL) { return EXAMPLE_CONFIG_INVALID; }
|
||
memcpy(prepared, values, count * sizeof(*values));
|
||
prepared_state = *state;
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
prepared_state.dtu = &prepared_dtu;
|
||
#endif
|
||
prepared_state.legacy = true;
|
||
/* 允许原子换名而不允许原地写:已打开的读者继续读取同一份完整旧文件。 */
|
||
reader.file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE,
|
||
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||
if (reader.file == INVALID_HANDLE_VALUE) {
|
||
if (GetLastError() != ERROR_FILE_NOT_FOUND) { return EXAMPLE_CONFIG_IO_ERROR; }
|
||
} else {
|
||
if (GetFileType(reader.file) == FILE_TYPE_DISK && GetFileSizeEx(reader.file, &size)) {
|
||
if (size.QuadPart <= 0 || size.QuadPart > CONFIG_MAX_FILE_BYTES) { result = EXAMPLE_CONFIG_INVALID; }
|
||
else {
|
||
reader.remaining = (DWORD)size.QuadPart;
|
||
reader.first = true;
|
||
result = config_parse(&reader, points, prepared, count, &prepared_state);
|
||
}
|
||
}
|
||
if (!CloseHandle(reader.file)) { result = EXAMPLE_CONFIG_IO_ERROR; }
|
||
if (result != EXAMPLE_CONFIG_OK) { return result; }
|
||
found = true;
|
||
}
|
||
/* V2是唯一权威文件;旧旁文件即使残留或损坏也不能影响后续启动。 */
|
||
if (prepared_state.legacy) {
|
||
result = config_legacy_remote_load(path, points[0].common_address, &prepared_state.remote);
|
||
if (result != EXAMPLE_CONFIG_OK && result != EXAMPLE_CONFIG_NOT_FOUND) { return result; }
|
||
found = found || result == EXAMPLE_CONFIG_OK;
|
||
result = config_legacy_temp_load(path, points[0].common_address,
|
||
prepared_state.temp.name, &prepared_state.temp.name_length,
|
||
prepared_state.temp.bytes, &prepared_state.temp.size, &prepared_state.temp.time);
|
||
if (result != EXAMPLE_CONFIG_OK && result != EXAMPLE_CONFIG_NOT_FOUND) { return result; }
|
||
if (result == EXAMPLE_CONFIG_OK) { prepared_state.temp.present = true; found = true; }
|
||
}
|
||
if (!found) { return EXAMPLE_CONFIG_NOT_FOUND; }
|
||
memcpy(values, prepared, count * sizeof(*values));
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
*published_dtu = prepared_dtu;
|
||
prepared_state.dtu = published_dtu;
|
||
#endif
|
||
*state = prepared_state;
|
||
return EXAMPLE_CONFIG_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:同步写出内部已限长的NUL结尾文本,不写NUL;API失败或短写均返回false。
|
||
* 输入参数:file——已打开的同步文件句柄;text——文本或格式化缓冲。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:无。
|
||
*/
|
||
static bool config_write_text(HANDLE file, const char *text)
|
||
{
|
||
DWORD written;
|
||
size_t length = strlen(text);
|
||
return WriteFile(file, text, (DWORD)length, &written, NULL) && written == length;
|
||
}
|
||
|
||
/**
|
||
* 功能:%.9g足以往返binary32;格式化后把CRT小数点规范化为点号,不把区域设置写进文件。
|
||
* 输入参数:value——本次待处理的数值;text——文本或格式化缓冲;capacity——目标缓冲容量。
|
||
* 输出参数:text——文本或格式化缓冲。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:无。
|
||
*/
|
||
static bool config_float_text(float value, char *text, size_t capacity)
|
||
{
|
||
const char *decimal;
|
||
char *dot;
|
||
size_t decimal_length;
|
||
int length;
|
||
if (!isfinite(value)) { return false; }
|
||
length = snprintf(text, capacity, "%.9g", (double)value);
|
||
if (length < 0 || (size_t)length >= capacity) { return false; }
|
||
decimal = localeconv()->decimal_point;
|
||
if (decimal == NULL || decimal[0] == '\0') { return false; }
|
||
decimal_length = strlen(decimal);
|
||
dot = strstr(text, decimal);
|
||
if (dot != NULL) {
|
||
*dot = '.';
|
||
memmove(dot + 1, dot + decimal_length, strlen(dot + decimal_length) + 1U);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:将标准参数点表和值写入INI候选。
|
||
* 输入参数:file——已打开的同步文件句柄;points——参数点表;values——与点表同序的参数运行值数组;count——参数点表元素数。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:保存时为主示例已知IOA补充用途;其他点仍有Type/KPA身份,可复用此小型存储接口。
|
||
* 集中基址参与参数到测量点的数值映射,不是只替换显示文字;
|
||
* 节名仍以十进制写出实际IOA,例如0x8601写为parameter_34305,与加载规则一致。
|
||
*/
|
||
static bool config_write_ini(HANDLE file, const IEC104_ParameterPoint_t *points,
|
||
const IEC104_ParameterValue_t *values, size_t count)
|
||
{
|
||
char line[CONFIG_LINE_SIZE];
|
||
size_t i;
|
||
int length = snprintf(line, sizeof(line),
|
||
"; IEC104 example persistent state: INI version 2.\n"
|
||
"; Edit only while the example is stopped; the master uses IEC104 as usual.\n"
|
||
"; Saving rewrites formatting and custom comments. Do not change section/type/kpa identities.\n"
|
||
"; Type110 value is the raw integer (ratio = value / 32768); Type112 smoothing is in seconds.\n"
|
||
"[config]\nversion=2\ncommon_address=%u\npoint_count=%lu\n",
|
||
(unsigned)points[0].common_address, (unsigned long)count);
|
||
if (length < 0 || (size_t)length >= sizeof(line) || !config_write_text(file, line)) { return false; }
|
||
for (i = 0U; i < count; ++i) {
|
||
const IEC104_ParameterPoint_t *point = &points[i];
|
||
char number[48];
|
||
const char *purpose = "application parameter";
|
||
uint32_t base = 0U;
|
||
if (point->kind == 1U) { purpose = "change threshold"; base = EXAMPLE_IOA_PARAMETER_BASE; }
|
||
else if (point->kind == 2U) { purpose = "smoothing time in seconds"; base = EXAMPLE_IOA_PARAMETER_BASE + 3U * EXAMPLE_IOA_PARAMETER_STRIDE; }
|
||
else if (point->kind == 3U) { purpose = "low limit"; base = EXAMPLE_IOA_PARAMETER_BASE + EXAMPLE_IOA_PARAMETER_STRIDE; }
|
||
else if (point->kind == 4U) { purpose = "high limit"; base = EXAMPLE_IOA_PARAMETER_BASE + 2U * EXAMPLE_IOA_PARAMETER_STRIDE; }
|
||
if (base != 0U && point->ioa >= base && point->ioa < base + 8U) {
|
||
length = snprintf(line, sizeof(line), "\n; Measurement %lu: %s.\n",
|
||
(unsigned long)(EXAMPLE_IOA_MEASUREMENT_BASE + point->ioa - base), purpose);
|
||
} else { length = snprintf(line, sizeof(line), "\n; %s.\n", purpose); }
|
||
if (length < 0 || (size_t)length >= sizeof(line) || !config_write_text(file, line)) { return false; }
|
||
if (point->type_id == 112U) {
|
||
if (!config_float_text(values[i].short_float, number, sizeof(number))) { return false; }
|
||
} else {
|
||
int value = point->type_id == 110U ? values[i].normalized : values[i].scaled;
|
||
length = snprintf(number, sizeof(number), "%d", value);
|
||
if (length < 0 || (size_t)length >= sizeof(number)) { return false; }
|
||
}
|
||
length = snprintf(line, sizeof(line), "[parameter_%lu]\ntype=%u\nkpa=%u\nvalue=%s\n",
|
||
(unsigned long)point->ioa, (unsigned)point->type_id, (unsigned)point->kind, number);
|
||
if (length < 0 || (size_t)length >= sizeof(line) || !config_write_text(file, line)) { return false; }
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:独占创建用于原子替换配置的临时文件。
|
||
* 输入参数:path——NUL结尾的宿主文件路径;temporary——本次独占临时文件路径;capacity——目标缓冲容量;file——新建文件句柄回写位置。
|
||
* 输出参数:temporary——本次独占临时文件路径;file——新建文件句柄回写位置。
|
||
* 返回值:配置操作状态:INVALID、OK、IO_ERROR;OK为成功,其他值保留具体失败原因。
|
||
* 说明:CREATE_NEW避免覆盖其他文件或重用异常退出遗留的临时内容。
|
||
* 进程号和可回绕序号只用于命名,独占性由CREATE_NEW保证。
|
||
*/
|
||
static ExampleConfigResult config_atomic_create(const char *path, char *temporary,
|
||
size_t capacity, HANDLE *file)
|
||
{
|
||
unsigned attempt;
|
||
for (attempt = 0U; attempt < 32U; ++attempt) {
|
||
int length = snprintf(temporary, capacity, "%s.%lu.%lu.tmp", path,
|
||
(unsigned long)GetCurrentProcessId(), ++config_temp_serial);
|
||
if (length < 0 || (size_t)length >= capacity) { return EXAMPLE_CONFIG_INVALID; }
|
||
*file = CreateFileA(temporary, GENERIC_WRITE, 0U, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
|
||
if (*file != INVALID_HANDLE_VALUE) { return EXAMPLE_CONFIG_OK; }
|
||
if (GetLastError() != ERROR_FILE_EXISTS && GetLastError() != ERROR_ALREADY_EXISTS) {
|
||
return EXAMPLE_CONFIG_IO_ERROR;
|
||
}
|
||
}
|
||
return EXAMPLE_CONFIG_IO_ERROR;
|
||
}
|
||
|
||
/**
|
||
* 功能:完成候选文件同步与关闭,并在完整成功后替换目标配置。
|
||
* 输入参数:path——NUL结尾的宿主文件路径;temporary——本次独占临时文件路径;file——已打开的同步文件句柄;complete——候选内容是否已完整写入。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:配置操作状态:IO_ERROR、OK;OK为成功,其他值保留具体失败原因。
|
||
* 说明:替换前完成写入、刷盘和关闭,不允许跨卷复制回退。
|
||
* 失败仅删除本次独占创建的临时文件;遗留.tmp不参与后续加载。
|
||
*/
|
||
static ExampleConfigResult config_atomic_finish(const char *path, const char *temporary,
|
||
HANDLE file, bool complete)
|
||
{
|
||
if (complete) { complete = FlushFileBuffers(file) != 0; }
|
||
if (!CloseHandle(file)) { complete = false; }
|
||
if (!complete || !MoveFileExA(temporary, path, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) {
|
||
(void)DeleteFileA(temporary);
|
||
return EXAMPLE_CONFIG_IO_ERROR;
|
||
}
|
||
return EXAMPLE_CONFIG_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:校验标准参数、远程定值及TEMP快照,写入独占临时文件后刷盘并原子替换统一INI。
|
||
* 输入参数:path——NUL结尾的宿主文件路径;points——参数点表;values——与点表同序的参数运行值数组;count——参数点表元素数;state——持久配置与TEMP候选状态。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:EXAMPLE_CONFIG_OK表示完整保存成功,INVALID表示参数或快照非法,IO_ERROR表示创建、写入、同步、关闭或替换失败。
|
||
* 说明:三类状态共用一次文件提交;不负责修改运行时配置或发送协议确认。
|
||
*/
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
static bool config_dtu_write(HANDLE file, const DtuSettings *settings)
|
||
{
|
||
char line[CONFIG_LINE_SIZE], number[48];
|
||
size_t area, index;
|
||
int length = snprintf(line, sizeof(line), "\n[dtu]\ncurrent_area=%u\n",
|
||
(unsigned)settings->current_area);
|
||
if (length <= 0 || (size_t)length >= sizeof(line) || !config_write_text(file, line)) { return false; }
|
||
for (area = 0U; area < DTU_SETTINGS_AREAS; ++area) {
|
||
length = snprintf(line, sizeof(line), "\n[dtu_area_%lu]\n", (unsigned long)area);
|
||
if (length <= 0 || (size_t)length >= sizeof(line) || !config_write_text(file, line)) { return false; }
|
||
for (index = 0U; index < DTU_SETTINGS_COUNT; ++index) {
|
||
double value;
|
||
if (!Dtu_Settings_Visible(index, (uint16_t)area)) { continue; }
|
||
value = settings->values[area][index];
|
||
if (Dtu_Settings_Tag(index) == 38U) {
|
||
if (!config_float_text((float)value, number, sizeof(number))) { return false; }
|
||
} else {
|
||
length = snprintf(number, sizeof(number), "%lu", (unsigned long)value);
|
||
if (length <= 0 || (size_t)length >= sizeof(number)) { return false; }
|
||
}
|
||
length = snprintf(line, sizeof(line), "%lu=%s\n", (unsigned long)Dtu_Settings_Ioa(index), number);
|
||
if (length <= 0 || (size_t)length >= sizeof(line) || !config_write_text(file, line)) { return false; }
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
#endif
|
||
|
||
ExampleConfigResult Example_Config_Save(const char *path,
|
||
const IEC104_ParameterPoint_t *points, const IEC104_ParameterValue_t *values, size_t count,
|
||
const ExamplePersistentConfig *state)
|
||
{
|
||
char temporary[CONFIG_TEMP_PATH_SIZE];
|
||
HANDLE file = INVALID_HANDLE_VALUE;
|
||
ExampleConfigResult result;
|
||
size_t i;
|
||
uint32_t checksum = 0U;
|
||
bool complete;
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
if (state == NULL || !Dtu_Settings_Valid(state->dtu) ||
|
||
state->dtu->current_area != state->remote.current_area) { return EXAMPLE_CONFIG_INVALID; }
|
||
#endif
|
||
if (!config_arguments_valid(path, points, values, count) || state == NULL ||
|
||
!config_remote_valid(&state->remote) ||
|
||
(state->temp.present && !config_temp_checksum(points[0].common_address, &state->temp, NULL, &checksum))) {
|
||
return EXAMPLE_CONFIG_INVALID;
|
||
}
|
||
for (i = 0U; i < count; ++i) {
|
||
if (points[i].type_id == 112U && !isfinite(values[i].short_float)) { return EXAMPLE_CONFIG_INVALID; }
|
||
}
|
||
result = config_atomic_create(path, temporary, sizeof(temporary), &file);
|
||
if (result != EXAMPLE_CONFIG_OK) { return result; }
|
||
complete = config_write_ini(file, points, values, count) &&
|
||
config_remote_write(file, &state->remote) && config_temp_write(file, &state->temp, checksum);
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
complete = complete && config_dtu_write(file, state->dtu);
|
||
#endif
|
||
return config_atomic_finish(path, temporary, file, complete);
|
||
}
|
||
|
||
/**
|
||
* 功能:返回结果码对应的静态ASCII名称供日志使用,未知值返回unknown,不转移字符串所有权。
|
||
* 输入参数:result——待判定的处理结果。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:与结果码对应的静态ASCII名称;未知枚举返回unknown,无需释放。
|
||
* 说明:无。
|
||
*/
|
||
const char *Example_Config_Result_Name(ExampleConfigResult result)
|
||
{
|
||
switch (result) {
|
||
case EXAMPLE_CONFIG_OK: return "ok";
|
||
case EXAMPLE_CONFIG_NOT_FOUND: return "not-found";
|
||
case EXAMPLE_CONFIG_INVALID: return "invalid";
|
||
case EXAMPLE_CONFIG_IO_ERROR: return "io-error";
|
||
default: return "unknown";
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 功能:旧旁文件只读迁移,路径从命令行配置路径派生,线上名称不参与宿主路径拼接。
|
||
* 输入参数:base——主配置文件路径;suffix——旧旁文件后缀;ca——公共地址;path——调用方提供的路径缓冲;capacity——目标缓冲容量。
|
||
* 输出参数:path——调用方提供的路径缓冲。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:无。
|
||
*/
|
||
static bool config_sidecar_path(const char *base, const char *suffix, uint16_t ca,
|
||
char *path, size_t capacity)
|
||
{
|
||
int length;
|
||
if (base == NULL || base[0] == '\0' || ca == 0U || ca == UINT16_MAX) { return false; }
|
||
length = snprintf(path, capacity, "%s%s", base, suffix);
|
||
return length >= 0 && (size_t)length < capacity;
|
||
}
|
||
|
||
/**
|
||
* 功能:校验当前区号、共享死区及五个区的复归/X时间范围,浮点范围比较同时排除NaN。
|
||
* 输入参数:values——五区远程定值快照。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:当前区号、共享死区和五套定值均有效时返回true,否则返回false。
|
||
* 说明:无。
|
||
*/
|
||
static bool config_remote_valid(const ExampleRemoteConfig *values)
|
||
{
|
||
size_t i;
|
||
if (values == NULL || values->current_area > 4U ||
|
||
!(values->deadband >= 0.0f && values->deadband <= 10.0f)) { return false; }
|
||
for (i = 0U; i < 5U; ++i) {
|
||
if (values->reset_seconds[i] > 3600U ||
|
||
!(values->x_time[i] >= 0.0f && values->x_time[i] <= 100.0f)) { return false; }
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:解析旧remote.ini到候选结构,核对版本/CA、全部五区、字段唯一性及完整性。
|
||
* 输入参数:reader——带文件长度和读取游标的配置读取器;ca——公共地址;prepared——尚未发布的配置候选。
|
||
* 输出参数:prepared——尚未发布的配置候选。
|
||
* 返回值:配置操作状态:INVALID、IO_ERROR、OK;OK为成功,其他值保留具体失败原因。
|
||
* 说明:输入节可乱序,但未知或重复键拒绝;失败时prepared可能已被部分填写。
|
||
*/
|
||
static ExampleConfigResult config_remote_parse(ConfigReader *reader, uint16_t ca,
|
||
ExampleRemoteConfig *prepared)
|
||
{
|
||
char line[CONFIG_LINE_SIZE];
|
||
uint8_t masks[5] = {0};
|
||
unsigned remote_mask = 0U, selected = 6U;
|
||
bool remote_seen = false;
|
||
int result;
|
||
size_t i;
|
||
memset(prepared, 0, sizeof(*prepared));
|
||
while ((result = config_read_line(reader, line)) == 1) {
|
||
char *text, *delimiter, *key, *value;
|
||
uint32_t number;
|
||
unsigned bit;
|
||
delimiter = strpbrk(line, ";#");
|
||
if (delimiter != NULL) { *delimiter = '\0'; }
|
||
text = config_trim(line);
|
||
if (*text == '\0') { continue; }
|
||
if (*text == '[') {
|
||
delimiter = strchr(text, ']');
|
||
if (delimiter == NULL || delimiter[1] != '\0') { return EXAMPLE_CONFIG_INVALID; }
|
||
*delimiter = '\0'; text = config_trim(text + 1);
|
||
if (strcmp(text, "remote") == 0) {
|
||
if (remote_seen) { return EXAMPLE_CONFIG_INVALID; }
|
||
remote_seen = true; selected = 5U;
|
||
} else {
|
||
if (strlen(text) != 6U || strncmp(text, "area_", 5U) != 0 ||
|
||
text[5] < '0' || text[5] > '4') { return EXAMPLE_CONFIG_INVALID; }
|
||
selected = (unsigned)(text[5] - '0');
|
||
if (masks[selected] != 0U) { return EXAMPLE_CONFIG_INVALID; }
|
||
masks[selected] = 4U;
|
||
}
|
||
continue;
|
||
}
|
||
delimiter = strchr(text, '=');
|
||
if (delimiter == NULL || selected > 5U) { return EXAMPLE_CONFIG_INVALID; }
|
||
*delimiter = '\0'; key = config_trim(text); value = config_trim(delimiter + 1);
|
||
if (selected == 5U) {
|
||
if (strcmp(key, "version") == 0) {
|
||
if (!config_unsigned(value, 1U, &number) || number != 1U) { return EXAMPLE_CONFIG_INVALID; }
|
||
bit = 1U;
|
||
} else if (strcmp(key, "common_address") == 0) {
|
||
if (!config_unsigned(value, 65534U, &number) || number != ca) { return EXAMPLE_CONFIG_INVALID; }
|
||
bit = 2U;
|
||
} else if (strcmp(key, "current_area") == 0) {
|
||
if (!config_unsigned(value, 4U, &number)) { return EXAMPLE_CONFIG_INVALID; }
|
||
prepared->current_area = (uint16_t)number; bit = 4U;
|
||
} else if (strcmp(key, "deadband") == 0) {
|
||
if (!config_float(value, &prepared->deadband)) { return EXAMPLE_CONFIG_INVALID; }
|
||
bit = 8U;
|
||
} else { return EXAMPLE_CONFIG_INVALID; }
|
||
if ((remote_mask & bit) != 0U) { return EXAMPLE_CONFIG_INVALID; }
|
||
remote_mask |= bit;
|
||
} else {
|
||
if (strcmp(key, "reset_seconds") == 0) {
|
||
if (!config_unsigned(value, 3600U, &number)) { return EXAMPLE_CONFIG_INVALID; }
|
||
prepared->reset_seconds[selected] = (uint16_t)number; bit = 1U;
|
||
} else if (strcmp(key, "x_time") == 0) {
|
||
if (!config_float(value, &prepared->x_time[selected])) { return EXAMPLE_CONFIG_INVALID; }
|
||
bit = 2U;
|
||
} else { return EXAMPLE_CONFIG_INVALID; }
|
||
if ((masks[selected] & bit) != 0U) { return EXAMPLE_CONFIG_INVALID; }
|
||
masks[selected] |= (uint8_t)bit;
|
||
}
|
||
}
|
||
if (result == -1) { return EXAMPLE_CONFIG_IO_ERROR; }
|
||
if (result != 0 || !remote_seen || remote_mask != 15U || !config_remote_valid(prepared)) {
|
||
return EXAMPLE_CONFIG_INVALID;
|
||
}
|
||
for (i = 0U; i < 5U; ++i) { if (masks[i] != 7U) { return EXAMPLE_CONFIG_INVALID; } }
|
||
return EXAMPLE_CONFIG_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:只读加载旧.remote.ini迁移文件,区分不存在、内容非法和I/O错误。
|
||
* 输入参数:base——主配置文件路径;common_address——站点公共地址;values——五区远程定值快照。
|
||
* 输出参数:values——五区远程定值快照。
|
||
* 返回值:配置操作状态:IO_ERROR、INVALID、NOT_FOUND、OK;OK为成功,其他值保留具体失败原因。
|
||
* 说明:在文件解析及关闭均成功后才改写values,不回写或删除旧文件。
|
||
*/
|
||
static ExampleConfigResult config_legacy_remote_load(const char *base, uint16_t common_address,
|
||
ExampleRemoteConfig *values)
|
||
{
|
||
char path[CONFIG_TEMP_PATH_SIZE];
|
||
ExampleRemoteConfig prepared;
|
||
ConfigReader reader = {0};
|
||
LARGE_INTEGER size;
|
||
ExampleConfigResult result = EXAMPLE_CONFIG_IO_ERROR;
|
||
if (values == NULL || !config_sidecar_path(base, ".remote.ini", common_address, path, sizeof(path))) {
|
||
return EXAMPLE_CONFIG_INVALID;
|
||
}
|
||
reader.file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE,
|
||
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||
if (reader.file == INVALID_HANDLE_VALUE) {
|
||
return GetLastError() == ERROR_FILE_NOT_FOUND ? EXAMPLE_CONFIG_NOT_FOUND : EXAMPLE_CONFIG_IO_ERROR;
|
||
}
|
||
if (GetFileType(reader.file) == FILE_TYPE_DISK && GetFileSizeEx(reader.file, &size)) {
|
||
if (size.QuadPart <= 0 || size.QuadPart > CONFIG_MAX_FILE_BYTES) { result = EXAMPLE_CONFIG_INVALID; }
|
||
else {
|
||
reader.remaining = (DWORD)size.QuadPart; reader.first = true;
|
||
result = config_remote_parse(&reader, common_address, &prepared);
|
||
}
|
||
}
|
||
if (!CloseHandle(reader.file)) { result = EXAMPLE_CONFIG_IO_ERROR; }
|
||
if (result == EXAMPLE_CONFIG_OK) { *values = prepared; }
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 功能:将已校验的共享远程定值及全部五区写入统一INI,浮点文本按规范小数点编码。
|
||
* 输入参数:file——已打开的同步文件句柄;values——五区远程定值快照。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:格式化或写入失败返回false,已写片段留给上层丢弃整个候选文件。
|
||
*/
|
||
static bool config_remote_write(HANDLE file, const ExampleRemoteConfig *values)
|
||
{
|
||
char line[CONFIG_LINE_SIZE], number[48];
|
||
size_t i;
|
||
int length;
|
||
if (!config_float_text(values->deadband, number, sizeof(number))) { return false; }
|
||
length = snprintf(line, sizeof(line),
|
||
"\n; Remote settings: all five areas are required.\n"
|
||
"; Deadband is shared. Each area stores reset_seconds and x_time in seconds.\n"
|
||
"[remote]\ncurrent_area=%u\ndeadband=%s\n",
|
||
(unsigned)values->current_area, number);
|
||
if (length < 0 || (size_t)length >= sizeof(line) || !config_write_text(file, line)) { return false; }
|
||
for (i = 0U; i < 5U; ++i) {
|
||
if (!config_float_text(values->x_time[i], number, sizeof(number))) { return false; }
|
||
length = snprintf(line, sizeof(line), "\n[area_%lu]\nreset_seconds=%u\nx_time=%s\n",
|
||
(unsigned long)i, (unsigned)values->reset_seconds[i], number);
|
||
if (length < 0 || (size_t)length >= sizeof(line) || !config_write_text(file, line)) { return false; }
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/* 旧TEMP快照V1:magic[8]、version[1]、CA[2]、name_length[1]、size[4]、
|
||
* CP56[7]、name[name_length]、data[size]、CRC32[4];多字节整数为小端。
|
||
* CRC32/ISO-HDLC覆盖校验码之前的全部字节;统一INI沿用同一CRC输入。 */
|
||
static const uint8_t config_temp_magic[8] = {'I', 'E', 'C', '1', '0', '4', 'T', 'P'};
|
||
|
||
/**
|
||
* 功能:跨片段累积CRC-32未取反状态,首段使用UINT32_MAX,最终异或由调用方完成。
|
||
* 输入参数:crc——跨片段CRC累积值;data——字节数据缓冲;size——本次数据的字节数。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:本片段处理后的CRC中间状态,最终异或由调用方执行。
|
||
* 说明:无。
|
||
*/
|
||
static uint32_t config_crc32(uint32_t crc, const uint8_t *data, size_t size)
|
||
{
|
||
size_t i;
|
||
unsigned bit;
|
||
for (i = 0U; i < size; ++i) {
|
||
crc ^= data[i];
|
||
for (bit = 0U; bit < 8U; ++bit) {
|
||
crc = (crc >> 1) ^ ((crc & 1U) != 0U ? UINT32_C(0xedb88320) : 0U);
|
||
}
|
||
}
|
||
return crc;
|
||
}
|
||
|
||
/**
|
||
* 功能:将整数显式写为4字节小端,目标容量由调用方保证,不要求地址对齐。
|
||
* 输入参数:data——字节数据缓冲;value——本次待处理的数值。
|
||
* 输出参数:data——字节数据缓冲。
|
||
* 返回值:无。
|
||
* 说明:无。
|
||
*/
|
||
static void config_u32_write(uint8_t *data, uint32_t value)
|
||
{
|
||
data[0] = (uint8_t)value; data[1] = (uint8_t)(value >> 8);
|
||
data[2] = (uint8_t)(value >> 16); data[3] = (uint8_t)(value >> 24);
|
||
}
|
||
|
||
/**
|
||
* 功能:从已保证至少4字节的缓冲读取小端整数,不依赖宿主端序或对齐。
|
||
* 输入参数:data——字节数据缓冲。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:解码后的32位无符号整数。
|
||
* 说明:无。
|
||
*/
|
||
static uint32_t config_u32_read(const uint8_t *data)
|
||
{
|
||
return (uint32_t)data[0] | ((uint32_t)data[1] << 8) |
|
||
((uint32_t)data[2] << 16) | ((uint32_t)data[3] << 24);
|
||
}
|
||
|
||
/**
|
||
* 功能:校验显式长度的TEMP/文件名,后缀只允许字母、数字、下划线、连字符和点。
|
||
* 输入参数:name——文件名或显式长度的协议名称;length——本次数据或名称的有效字节数。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:名称属于允许的TEMP单层文件名时返回true,否则返回false。
|
||
* 说明:后缀不得以点开头或包含路径分隔符;不要求输入以NUL结尾。
|
||
*/
|
||
static bool config_temp_name_valid(const uint8_t *name, uint8_t length)
|
||
{
|
||
size_t i;
|
||
if (name == NULL || length <= 5U || length > CONFIG_TEMP_MAX_NAME ||
|
||
memcmp(name, "TEMP/", 5U) != 0 || name[5] == '.') { return false; }
|
||
for (i = 5U; i < length; ++i) {
|
||
uint8_t ch = name[i];
|
||
if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
|
||
(ch >= '0' && ch <= '9') || ch == '_' || ch == '-' || ch == '.')) { return false; }
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:循环读满指定字节数;I/O错误、提前EOF或异常读取长度均失败,输出可能已有前缀。
|
||
* 输入参数:file——已打开的同步文件句柄;bytes——字节数据缓冲;length——本次数据或名称的有效字节数。
|
||
* 输出参数:bytes——字节数据缓冲。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:无。
|
||
*/
|
||
static bool config_binary_read(HANDLE file, uint8_t *bytes, DWORD length)
|
||
{
|
||
while (length != 0U) {
|
||
DWORD read;
|
||
if (!ReadFile(file, bytes, length, &read, NULL) || read == 0U || read > length) { return false; }
|
||
bytes += read; length -= read;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:只读校验旧TEMP快照的版本、CA、名称、精确文件长度、CP56及CRC后发布元数据。
|
||
* 输入参数:base——主配置文件路径;common_address——站点公共地址;name——文件名或显式长度的协议名称;name_length——文件名长度回写位置;bytes——字节数据缓冲;size——实际字节数回写位置;time——CP56日历时标回写位置。
|
||
* 输出参数:name——文件名或显式长度的协议名称;name_length——文件名长度回写位置;bytes——字节数据缓冲;size——实际字节数回写位置;time——CP56日历时标回写位置。
|
||
* 返回值:配置操作状态:IO_ERROR、INVALID、NOT_FOUND、OK;OK为成功,其他值保留具体失败原因。
|
||
* 说明:正文先读入调用方bytes候选区,失败可能留下部分正文;名称、长度和时标仅OK时发布。
|
||
*/
|
||
static ExampleConfigResult config_legacy_temp_load(const char *base, uint16_t common_address,
|
||
uint8_t *name, uint8_t *name_length, uint8_t *bytes, uint32_t *size,
|
||
IEC60870_CP56Time2a_t *time)
|
||
{
|
||
char path[CONFIG_TEMP_PATH_SIZE];
|
||
uint8_t header[CONFIG_TEMP_HEADER_BYTES], prepared_name[CONFIG_TEMP_MAX_NAME], checksum[4];
|
||
IEC60870_CP56Time2a_t prepared_time;
|
||
uint32_t prepared_size = 0U, crc;
|
||
HANDLE file;
|
||
LARGE_INTEGER file_size;
|
||
ExampleConfigResult result = EXAMPLE_CONFIG_IO_ERROR;
|
||
if (name == NULL || name_length == NULL || bytes == NULL || size == NULL || time == NULL ||
|
||
!config_sidecar_path(base, ".temp.bin", common_address, path, sizeof(path))) { return EXAMPLE_CONFIG_INVALID; }
|
||
file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE,
|
||
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||
if (file == INVALID_HANDLE_VALUE) {
|
||
return GetLastError() == ERROR_FILE_NOT_FOUND ? EXAMPLE_CONFIG_NOT_FOUND : EXAMPLE_CONFIG_IO_ERROR;
|
||
}
|
||
if (GetFileType(file) != FILE_TYPE_DISK || !GetFileSizeEx(file, &file_size)) { goto done; }
|
||
result = EXAMPLE_CONFIG_INVALID;
|
||
if (file_size.QuadPart < CONFIG_TEMP_HEADER_BYTES + 4U ||
|
||
file_size.QuadPart > CONFIG_TEMP_HEADER_BYTES + CONFIG_TEMP_MAX_NAME + CONFIG_TEMP_MAX_BYTES + 4U) { goto done; }
|
||
result = EXAMPLE_CONFIG_IO_ERROR;
|
||
if (!config_binary_read(file, header, sizeof(header))) { goto done; }
|
||
result = EXAMPLE_CONFIG_INVALID;
|
||
prepared_size = config_u32_read(header + 12U);
|
||
if (memcmp(header, config_temp_magic, sizeof(config_temp_magic)) != 0 || header[8] != 1U ||
|
||
((uint16_t)header[9] | ((uint16_t)header[10] << 8)) != common_address ||
|
||
header[11] <= 5U || header[11] > CONFIG_TEMP_MAX_NAME || prepared_size > CONFIG_TEMP_MAX_BYTES ||
|
||
file_size.QuadPart != (LONGLONG)(sizeof(header) + header[11] + prepared_size + sizeof(checksum)) ||
|
||
!IEC60870_Time_From_CP56(header + 16U, IEC60870_CP56_LEN, &prepared_time)) { goto done; }
|
||
result = EXAMPLE_CONFIG_IO_ERROR;
|
||
if (!config_binary_read(file, prepared_name, header[11])) { goto done; }
|
||
result = EXAMPLE_CONFIG_INVALID;
|
||
if (!config_temp_name_valid(prepared_name, header[11])) { goto done; }
|
||
result = EXAMPLE_CONFIG_IO_ERROR;
|
||
if (!config_binary_read(file, bytes, prepared_size) || !config_binary_read(file, checksum, sizeof(checksum))) { goto done; }
|
||
crc = config_crc32(UINT32_MAX, header, sizeof(header));
|
||
crc = config_crc32(crc, prepared_name, header[11]);
|
||
crc = config_crc32(crc, bytes, prepared_size) ^ UINT32_MAX;
|
||
result = crc == config_u32_read(checksum) ? EXAMPLE_CONFIG_OK : EXAMPLE_CONFIG_INVALID;
|
||
done:
|
||
if (!CloseHandle(file)) { result = EXAMPLE_CONFIG_IO_ERROR; }
|
||
if (result == EXAMPLE_CONFIG_OK) {
|
||
memcpy(name, prepared_name, header[11]);
|
||
*name_length = header[11]; *size = prepared_size; *time = prepared_time;
|
||
}
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 功能:按旧二进制快照的规范头、名称和正文计算统一INI使用的CRC,仅成功时填写checksum。
|
||
* 输入参数:ca——公共地址;temp——已提交或待提交的TEMP快照;encoded_time——可选原始CP56编码;checksum——CRC校验值回写位置。
|
||
* 输出参数:checksum——CRC校验值回写位置。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:读取路径可提供原始encoded_time覆盖重编码时标,避免CP56中未保留的位改变校验输入。
|
||
*/
|
||
static bool config_temp_checksum(uint16_t ca, const ExampleTempConfig *temp,
|
||
const uint8_t *encoded_time, uint32_t *checksum)
|
||
{
|
||
uint8_t header[CONFIG_TEMP_HEADER_BYTES] = {0};
|
||
uint32_t crc;
|
||
if (!config_temp_name_valid(temp->name, temp->name_length) || temp->size > CONFIG_TEMP_MAX_BYTES ||
|
||
(temp->size != 0U && temp->bytes == NULL) ||
|
||
!IEC60870_Time_To_CP56(&temp->time, header + 16U, IEC60870_CP56_LEN)) { return false; }
|
||
/* 读取校验覆盖文件原字节;CP56解码不保留GEN和原始星期,不能重编码后替代原文。 */
|
||
if (encoded_time != NULL) { memcpy(header + 16U, encoded_time, IEC60870_CP56_LEN); }
|
||
memcpy(header, config_temp_magic, sizeof(config_temp_magic));
|
||
header[8] = 1U; header[9] = (uint8_t)ca; header[10] = (uint8_t)(ca >> 8);
|
||
header[11] = temp->name_length; config_u32_write(header + 12U, temp->size);
|
||
crc = config_crc32(UINT32_MAX, header, sizeof(header));
|
||
crc = config_crc32(crc, temp->name, temp->name_length);
|
||
*checksum = config_crc32(crc, temp->bytes, temp->size) ^ UINT32_MAX;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:将size字节编码为大写十六进制并追加NUL;调用方保证text至少容纳size*2+1字节。
|
||
* 输入参数:bytes——字节数据缓冲;size——本次数据的字节数;text——文本或格式化缓冲。
|
||
* 输出参数:text——文本或格式化缓冲。
|
||
* 返回值:无。
|
||
* 说明:无。
|
||
*/
|
||
static void config_hex_text(const uint8_t *bytes, size_t size, char *text)
|
||
{
|
||
static const char digits[] = "0123456789ABCDEF";
|
||
size_t i;
|
||
for (i = 0U; i < size; ++i) {
|
||
text[i * 2U] = digits[bytes[i] >> 4];
|
||
text[i * 2U + 1U] = digits[bytes[i] & 15U];
|
||
}
|
||
text[size * 2U] = '\0';
|
||
}
|
||
|
||
/**
|
||
* 功能:写TEMP存在标志,或已校验快照的元数据、CRC和每块最多128字节的十六进制正文。
|
||
* 输入参数:file——已打开的同步文件句柄;temp——已提交或待提交的TEMP快照;checksum——已计算的CRC校验值。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:任一格式化/写入失败即返回false,候选文件的发布或丢弃由外层原子保存处理。
|
||
*/
|
||
static bool config_temp_write(HANDLE file, const ExampleTempConfig *temp, uint32_t checksum)
|
||
{
|
||
char line[CONFIG_LINE_SIZE], hex[CONFIG_HEX_CHUNK_BYTES * 2U + 1U];
|
||
uint8_t encoded[IEC60870_CP56_LEN];
|
||
uint32_t offset;
|
||
int length;
|
||
if (!temp->present) { return config_write_text(file, "\n[temp]\npresent=0\n"); }
|
||
if (!IEC60870_Time_To_CP56(&temp->time, encoded, sizeof(encoded))) { return false; }
|
||
config_hex_text(encoded, sizeof(encoded), hex);
|
||
length = snprintf(line, sizeof(line),
|
||
"\n; TEMP snapshot: do not edit its metadata or data without updating crc32.\n"
|
||
"[temp]\npresent=1\nname=%.*s\nsize=%lu\ntime=%s\ncrc32=%08lX\n",
|
||
(int)temp->name_length, (const char *)temp->name, (unsigned long)temp->size,
|
||
hex, (unsigned long)checksum);
|
||
if (length < 0 || (size_t)length >= sizeof(line) || !config_write_text(file, line)) { return false; }
|
||
for (offset = 0U; offset < temp->size; offset += CONFIG_HEX_CHUNK_BYTES) {
|
||
uint32_t size = temp->size - offset;
|
||
if (size > CONFIG_HEX_CHUNK_BYTES) { size = CONFIG_HEX_CHUNK_BYTES; }
|
||
config_hex_text(temp->bytes + offset, size, hex);
|
||
length = snprintf(line, sizeof(line), "data_%lu=%s\n", (unsigned long)(offset / CONFIG_HEX_CHUNK_BYTES), hex);
|
||
if (length < 0 || (size_t)length >= sizeof(line) || !config_write_text(file, line)) { return false; }
|
||
}
|
||
return true;
|
||
}
|