318 lines
17 KiB
C
318 lines
17 KiB
C
/* 主机单条录波适配:生成模拟样点、从冻结内存读取、保存配对文件。
|
||
* 波形及 CFG 缓冲均归调用者所有;本文件不接真实 ADC,也不创建采集任务。 */
|
||
#include "iec104_example_recording.h"
|
||
#include <errno.h>
|
||
#include <math.h>
|
||
#include <stdio.h>
|
||
#include <string.h>
|
||
#include <sys/stat.h>
|
||
#ifdef _WIN32
|
||
#include <direct.h>
|
||
#define RECORDING_MKDIR(path) _mkdir(path)
|
||
#else
|
||
#define RECORDING_MKDIR(path) mkdir(path, 0700)
|
||
#endif
|
||
|
||
/**
|
||
* 功能:取得固定正弦波表中的一个标度采样。
|
||
* 输入参数:index——本次条目、点或逻辑槽的下标。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:该采样下标对应的有符号正弦标度值。
|
||
* 说明:由四分之一周波表恢复 80 点整周,峰值 10000,避免示例依赖浮点正弦库。
|
||
*/
|
||
static int16_t Sample_Sine(unsigned index)
|
||
{
|
||
static const int16_t quarter[21] = {
|
||
0, 785, 1564, 2334, 3090, 3827, 4540, 5225, 5878, 6494,
|
||
7071, 7604, 8090, 8526, 8910, 9239, 9511, 9724, 9877, 9969, 10000
|
||
};
|
||
unsigned phase = index % 80U;
|
||
int16_t value = quarter[(phase % 40U) <= 20U ? phase % 40U : 40U - phase % 40U];
|
||
return phase < 40U ? value : (int16_t)-value;
|
||
}
|
||
|
||
static bool Recording_Format(ExampleRecording *recording,
|
||
const IEC60870_CP56Time2a_t *trigger_time, uint8_t bay, uint16_t sequence);
|
||
|
||
/**
|
||
* 功能:顺序构造一条明确标识为模拟来源的完整录波,再冻结 CFG、DAT 长度及配对文件名。
|
||
* 输入参数:recording——调用方持有的录波快照;trigger_time——录波触发时刻;bay——间隔编号;sequence——录波文件序号。
|
||
* 输出参数:recording——本次操作更新的实例或工作区。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:此入口重建对象;调用前必须结束旧对象的全部目录/读取会话,成功仅置 complete。
|
||
*/
|
||
bool Example_Recording_Demo(ExampleRecording *recording,
|
||
const IEC60870_CP56Time2a_t *trigger_time, uint8_t bay, uint16_t sequence)
|
||
{
|
||
IEC60870_FaultRecorderConfig_t capture;
|
||
IEC60870_FaultRecorderFrame_t frame;
|
||
IEC60870_CP56Time2a_t captured_time;
|
||
unsigned index, channel;
|
||
if (recording == NULL || trigger_time == NULL) { return false; }
|
||
/* 先保留输入,允许 trigger_time 指向 recording->trigger_time;
|
||
* 否则下面清空对象时会同时抹去调用方给出的触发时刻。 */
|
||
captured_time = *trigger_time;
|
||
trigger_time = &captured_time;
|
||
memset(recording, 0, sizeof(*recording));
|
||
IEC60870_FaultRecorder_Config_Default(&capture);
|
||
if (!IEC60870_FaultRecorder_Init(&recording->recorder, &capture,
|
||
recording->frames, EXAMPLE_RECORDING_FRAMES)) { return false; }
|
||
/* 默认 8 路模拟量、16 路遥信;这里一次循环造完样点,不模拟实时等待。 */
|
||
for (index = 0U; index < EXAMPLE_RECORDING_FRAMES; ++index) {
|
||
IEC60870_FaultRecorderResult_t result;
|
||
memset(&frame, 0, sizeof(frame));
|
||
for (channel = 0U; channel < 3U; ++channel) {
|
||
/* 每相平移 27 个整数样点,仅近似 120 度;用于演示,不作相位校验。
|
||
* 槽位 0..2 为 Ua/Ub/Uc,4..6 为 Ia/Ib/Ic;触发后电压减半、电流增至 5 倍。 */
|
||
int32_t wave = Sample_Sine(index + 7U + channel * 27U);
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
/* 100V线电压对应57.735Vrms相压;CT=1A/1A,故障电流0.4A升至1.3A。
|
||
* DAT保存瞬时值,峰值含sqrt(2);CFG仍按0.01V/0.001A换算。 */
|
||
frame.analog[channel] = (int16_t)(wave * 8165 / 10000);
|
||
frame.analog[channel + 4U] = (int16_t)(wave * (index < 320U ? 566 : 1838) / 10000);
|
||
#else
|
||
frame.analog[channel] = (int16_t)(index < 320U ? wave : wave / 2);
|
||
frame.analog[channel + 4U] = (int16_t)(wave / (index < 320U ? 10 : 2));
|
||
#endif
|
||
}
|
||
/* 槽位 3/7 为 U0/I0;故障发生后加入零序量。遥信 bit1 在触发点置位,
|
||
* 通用示例bit0在360置位示意10ms保护动作;DTU按源配置保持无跳闸。 */
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
/* 配置启用零序电压突变录波且门限10V:U0由0.2V升至12V;
|
||
* 故障窗口160ms短于最短500ms过流告警,默认跳闸退出,不造跳闸位。 */
|
||
frame.analog[3] = (int16_t)((int32_t)Sample_Sine(index + 7U) * (index < 320U ? 28 : 1697) / 10000);
|
||
frame.analog[7] = (int16_t)((int32_t)Sample_Sine(index + 7U) * (index < 320U ? 7 : 42) / 10000);
|
||
frame.digital[0] = (uint16_t)(0x0080U | (index >= 320U ? 2U : 0U));
|
||
#else
|
||
frame.analog[3] = index < 320U ? 0 : (int16_t)(Sample_Sine(index + 7U) / 20);
|
||
frame.analog[7] = index < 320U ? 0 : (int16_t)(Sample_Sine(index + 7U) / 10);
|
||
frame.digital[0] = (uint16_t)((index >= 320U ? 2U : 0U) | (index >= 360U ? 1U : 0U));
|
||
#endif
|
||
result = IEC60870_FaultRecorder_Push(&recording->recorder, index, &frame);
|
||
if (result != (index == 960U ? IEC60870_FAULT_RECORDER_COMPLETE : IEC60870_FAULT_RECORDER_OK)) {
|
||
return false;
|
||
}
|
||
/* Trigger 引用已接收的样点,必须先 Push;至 960 含两端共 961 点,
|
||
* 触发前保留完整 4 周波,触发后继续接收完整 8 周波。 */
|
||
if (index == 320U && IEC60870_FaultRecorder_Trigger(&recording->recorder, index) !=
|
||
IEC60870_FAULT_RECORDER_OK) { return false; }
|
||
}
|
||
return Recording_Format(recording, trigger_time, bay, sequence);
|
||
}
|
||
|
||
/* 共用冻结记录的 CFG/名称编码,保持启动 Demo 的文件内容不变。 */
|
||
static bool Recording_Format(ExampleRecording *recording,
|
||
const IEC60870_CP56Time2a_t *trigger_time, uint8_t bay, uint16_t sequence)
|
||
{
|
||
IEC60870_ComtradeConfig_t format;
|
||
unsigned index, channel;
|
||
IEC60870_Comtrade_Config_Default(&format);
|
||
/* 文件内明确标识模拟来源;DAT 保留整数样值,工程量换算系数写在 CFG 中:
|
||
* 前四路按 0.01 V/计数,后四路按 0.001 A/计数。 */
|
||
format.station = "SIMULATION";
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
format.device = "DTU_100V1A";
|
||
{
|
||
static const char *const digital_names[16] = {
|
||
"SW00_TRIP", "SW00_RECORD_START", "SW00_ALARM_1", "SW00_ALARM_2",
|
||
"SW00_ALARM_3", "SW00_ZERO_CURRENT", "SW00_ZERO_VOLTAGE", "SW00_CLOSED",
|
||
"SW00_OPEN", "D10", "D11", "D12", "D13", "D14", "D15", "D16"
|
||
};
|
||
format.digital_names = digital_names;
|
||
}
|
||
#else
|
||
format.device = "IEC104_DEMO";
|
||
#endif
|
||
format.trigger_time = *trigger_time;
|
||
for (channel = 0U; channel < 8U; ++channel) {
|
||
format.analog[channel].multiplier = channel < 4U ? "0.01" : "0.001";
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
format.analog[channel].primary = channel < 4U ? "100" : "1";
|
||
format.analog[channel].secondary = channel < 4U ? "100" : "1";
|
||
format.analog[channel].primary_or_secondary = 'S';
|
||
#endif
|
||
}
|
||
if (!IEC60870_Comtrade_Build_CFG(&recording->recorder, &format,
|
||
recording->cfg, sizeof(recording->cfg), &recording->cfg_size) ||
|
||
!IEC60870_Comtrade_Data_Size(&recording->recorder, &recording->dat_size)) { return false; }
|
||
/* 同一触发时刻和序号生成配对名称;目录返回的完整名字也用于后续文件激活。 */
|
||
for (index = 0U; index < 2U; ++index) {
|
||
if (!IEC60870_Comtrade_File_Name(bay, sequence, trigger_time, index != 0U,
|
||
recording->names[index], sizeof(recording->names[index]))) { return false; }
|
||
memcpy(recording->paths[index], "COMTRADE/", 9U);
|
||
strcpy(recording->paths[index] + 9U, recording->names[index]);
|
||
}
|
||
recording->trigger_time = *trigger_time;
|
||
/* 只有样点、编码和两份名称全部成功,才允许后续保存/读取该记录。 */
|
||
recording->complete = true;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:按触发前后的仿真二次 RMS 值重建固定频率波形,不表示 ADC 实时采样。
|
||
* 输入参数:before/after——Ua/Ub/Uc/U0/Ia/Ib/Ic/I0 的非负有限 V/A;其余参数见头文件。
|
||
* 输出参数:recording——成功后完整冻结的模拟记录,尚未保存或发布。
|
||
* 返回值:true 表示构造成功;无效输入在修改旧记录前返回 false。
|
||
* 说明:先校验时标、文件身份和全部峰值,再重建对象;没有任何异步采样或后台写入。
|
||
*/
|
||
bool Example_Recording_Capture(ExampleRecording *recording,
|
||
const IEC60870_CP56Time2a_t *trigger_time, uint8_t bay, uint16_t sequence,
|
||
const double before[8], const double after[8], uint16_t before_digital, uint16_t after_digital)
|
||
{
|
||
IEC60870_FaultRecorderConfig_t capture;
|
||
IEC60870_FaultRecorderFrame_t frame;
|
||
IEC60870_CP56Time2a_t captured_time;
|
||
IEC60870_WallTime_t start_time;
|
||
char name[IEC60870_COMTRADE_FILENAME_CAPACITY];
|
||
double peaks[2][8];
|
||
uint32_t seconds;
|
||
uint64_t start_ms;
|
||
unsigned side, channel, index;
|
||
if (recording == NULL || trigger_time == NULL || before == NULL || after == NULL) { return false; }
|
||
captured_time = *trigger_time;
|
||
/* 文件名检查同时验证 CP56 日历、IV/SU 和 bay/sequence;首点还须能回推 80ms。 */
|
||
if (!IEC60870_Comtrade_File_Name(bay, sequence, &captured_time, false, name, sizeof(name)) ||
|
||
!IEC60870_Time_CP56_To_Seconds(&captured_time, IEC60870_LOCAL_UTC_OFFSET_MINUTES, &seconds)) {
|
||
return false;
|
||
}
|
||
start_ms = (uint64_t)seconds * 1000U + captured_time.time.ms;
|
||
if (start_ms < 80U) { return false; }
|
||
start_ms -= 80U;
|
||
if (!IEC60870_Time_Seconds_To_Local((uint32_t)(start_ms / 1000U),
|
||
(uint16_t)(start_ms % 1000U), &start_time) || start_time.year < IEC60870_CP56_MIN_YEAR) {
|
||
return false;
|
||
}
|
||
for (side = 0U; side < 2U; ++side) {
|
||
const double *rms = side == 0U ? before : after;
|
||
for (channel = 0U; channel < 8U; ++channel) {
|
||
double factor = 1.4142135623730951 * (channel < 4U ? 100.0 : 1000.0);
|
||
if (!isfinite(rms[channel]) || rms[channel] < 0.0 ||
|
||
rms[channel] > (double)INT16_MAX / factor) { return false; }
|
||
peaks[side][channel] = rms[channel] * factor;
|
||
if (peaks[side][channel] > (double)INT16_MAX) { return false; }
|
||
}
|
||
}
|
||
/* 输入均已快照化,允许触发时间或 RMS 数组与旧对象存储重叠。 */
|
||
memset(recording, 0, sizeof(*recording));
|
||
IEC60870_FaultRecorder_Config_Default(&capture);
|
||
if (!IEC60870_FaultRecorder_Init(&recording->recorder, &capture,
|
||
recording->frames, EXAMPLE_RECORDING_FRAMES)) { return false; }
|
||
for (index = 0U; index < EXAMPLE_RECORDING_FRAMES; ++index) {
|
||
IEC60870_FaultRecorderResult_t result;
|
||
side = index < 320U ? 0U : 1U;
|
||
memset(&frame, 0, sizeof(frame));
|
||
for (channel = 0U; channel < 8U; ++channel) {
|
||
unsigned phase = channel % 4U < 3U ? (channel % 4U) * 27U : 0U;
|
||
/* 整数样点移相近似三相 120 度;RMS 输入不携带实测相角或谐波信息。 */
|
||
double sample = (double)Sample_Sine(index + 7U + phase) / 10000.0 * peaks[side][channel];
|
||
frame.analog[channel] = (int16_t)sample;
|
||
}
|
||
frame.digital[0] = side == 0U ? before_digital : after_digital;
|
||
result = IEC60870_FaultRecorder_Push(&recording->recorder, index, &frame);
|
||
if (result != (index == 960U ? IEC60870_FAULT_RECORDER_COMPLETE : IEC60870_FAULT_RECORDER_OK)) {
|
||
return false;
|
||
}
|
||
if (index == 320U && IEC60870_FaultRecorder_Trigger(&recording->recorder, index) !=
|
||
IEC60870_FAULT_RECORDER_OK) { return false; }
|
||
}
|
||
return Recording_Format(recording, &captured_time, bay, sequence);
|
||
}
|
||
|
||
/**
|
||
* 功能:本函数从冻结内存读取,不依赖磁盘保存状态;saved/published 由文件服务入口检查。
|
||
* 输入参数:recording——调用方持有的录波快照;index——本次条目、点或逻辑槽的下标;offset——从起点计算的字节偏移;buffer——调用方提供的数据缓冲;length——本次数据或名称的有效字节数。
|
||
* 输出参数:buffer——调用方提供的数据缓冲。
|
||
* 返回值:true表示指定范围的数据已完整读出;false表示范围非法、读取失败或短读。
|
||
* 说明:这样写磁盘和网络下载使用相同字节来源,而不会因不同编码路径出现内容差异。
|
||
*/
|
||
bool Example_Recording_Read(const ExampleRecording *recording, unsigned index,
|
||
uint32_t offset, uint8_t *buffer, size_t length)
|
||
{
|
||
size_t size;
|
||
if (recording == NULL || !recording->complete || index > 1U ||
|
||
(length != 0U && buffer == NULL)) { return false; }
|
||
size = index == 0U ? recording->cfg_size : recording->dat_size;
|
||
/* 先验证 offset 再用减法检查剩余长度,避免 offset+length 溢出。 */
|
||
if (offset > size || length > size - offset) { return false; }
|
||
if (index == 1U) { return IEC60870_Comtrade_Read_DAT(&recording->recorder, offset, buffer, length); }
|
||
if (length != 0U) { memcpy(buffer, recording->cfg + offset, length); }
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:与网络读取共用同一编码入口,小块写出而不构造整份 DAT 副本。
|
||
* 输入参数:recording——调用方持有的录波快照;index——本次条目、点或逻辑槽的下标;path——NUL结尾的宿主文件路径。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:fwrite 短写、fflush 或 fclose 失败均使本次保存失败。
|
||
*/
|
||
static bool Write_File(const ExampleRecording *recording, unsigned index, const char *path)
|
||
{
|
||
uint8_t bytes[228];
|
||
uint32_t size = index == 0U ? (uint32_t)recording->cfg_size : recording->dat_size;
|
||
uint32_t offset = 0U;
|
||
bool ok = true;
|
||
FILE *file = fopen(path, "wb");
|
||
if (file == NULL) { return false; }
|
||
while (offset < size) {
|
||
size_t length = size - offset < sizeof(bytes) ? size - offset : sizeof(bytes);
|
||
if (!Example_Recording_Read(recording, index, offset, bytes, length) ||
|
||
fwrite(bytes, 1U, length, file) != length) { ok = false; break; }
|
||
offset += (uint32_t)length;
|
||
}
|
||
if (fflush(file) != 0) { ok = false; }
|
||
if (fclose(file) != 0) { ok = false; }
|
||
return ok;
|
||
}
|
||
|
||
/**
|
||
* 功能:唯一写者先排除现有最终文件/临时文件,再写两份候选并依次发布。
|
||
* 输入参数:recording——调用方持有的录波快照;parent——已存在的宿主父目录。
|
||
* 输出参数:recording——本次操作更新的实例或工作区。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:false 时尽力回收本次创建的候选;该过程只处理普通I/O失败,不承诺掉电事务。
|
||
*/
|
||
bool Example_Recording_Save(ExampleRecording *recording, const char *parent)
|
||
{
|
||
char directory[256], paths[2][320], temporary[2][324];
|
||
struct stat existing;
|
||
unsigned index;
|
||
bool staged[2] = {false, false}, renamed_data = false;
|
||
int count;
|
||
if (recording == NULL || !recording->complete || recording->saved || parent == NULL || parent[0] == '\0') {
|
||
return false;
|
||
}
|
||
count = snprintf(directory, sizeof(directory), "%s/COMTRADE", parent);
|
||
if (count < 0 || (size_t)count >= sizeof(directory)) { return false; }
|
||
if (RECORDING_MKDIR(directory) != 0 && errno != EEXIST) { return false; }
|
||
if (stat(directory, &existing) != 0 || (existing.st_mode & S_IFMT) != S_IFDIR) { return false; }
|
||
/* 在写入任何数据之前检查两组目标名和临时名,避免覆盖已有文件。
|
||
* stat 与 fopen/rename 之间没有排他锁,因此调用方必须保证单写者。 */
|
||
for (index = 0U; index < 2U; ++index) {
|
||
count = snprintf(paths[index], sizeof(paths[index]), "%s/%s", directory, recording->names[index]);
|
||
if (count < 0 || (size_t)count >= sizeof(paths[index])) { return false; }
|
||
count = snprintf(temporary[index], sizeof(temporary[index]), "%s.tmp", paths[index]);
|
||
if (count < 0 || (size_t)count >= sizeof(temporary[index])) { return false; }
|
||
if (stat(paths[index], &existing) == 0 || errno != ENOENT ||
|
||
stat(temporary[index], &existing) == 0 || errno != ENOENT) { return false; }
|
||
}
|
||
/* 先完成两份临时文件。写入失败也标记 staged,以便清理部分写出的文件。 */
|
||
for (index = 0U; index < 2U; ++index) {
|
||
staged[index] = true;
|
||
if (!Write_File(recording, index, temporary[index])) { goto failed; }
|
||
}
|
||
/* DAT 先改为最终名,CFG 最后出现;进程内仍等 saved/published 门禁后才公开。
|
||
* 两次 rename 不是一个事务,fflush/fclose 也不构成掉电持久性保证。 */
|
||
if (rename(temporary[1], paths[1]) != 0) { goto failed; }
|
||
renamed_data = true;
|
||
if (rename(temporary[0], paths[0]) != 0) { goto failed; }
|
||
recording->saved = true;
|
||
return true;
|
||
failed:
|
||
/* 只尝试删除本次触碰的临时文件和已改名 DAT,不删除预先存在的文件。
|
||
* 清理错误不掩盖原始失败;遗留文件由后续维护处理。 */
|
||
for (index = 0U; index < 2U; ++index) { if (staged[index]) { (void)remove(temporary[index]); } }
|
||
if (renamed_data) { (void)remove(paths[1]); }
|
||
return false;
|
||
}
|