Files
STM32F4-Base/IEC60870-Slave/test/periodic_scan_lib60870_client.c
T
STF bdcc8b6500
CI / L0 静态门禁 (pre-commit) (push) Failing after 7m46s
CI / L0.5 主机侧单测 (MSVC) (push) Successful in 21s
添加遥控、参数装载、文件传输等业务
2026-11-16 22:19:17 -08:00

73 lines
3.6 KiB
C

/* Optional external master. Build with official lib60870-C, not this library. */
#include "cs104_connection.h"
#include "hal_thread.h"
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
static atomic_uint seen;
static atomic_int failed;
static int expected_sequence;
#define EXPECT(x) do { if (!(x)) { fprintf(stderr, "line %d: %s\n", __LINE__, #x); atomic_store(&failed, 1); return false; } } while (0)
/**
* 功能:[INTEROP/RECEIVE] 使用外部库解析线上对象,核对CA/OA、SQ、类型与COT组合、IOA、值及品质。
* 输入参数:context——业务回调的测试上下文;address——监听地址或接收报文的公共地址;asdu——ASDU 容器。
* 输出参数:无。
* 返回值:固定返回 true;报文核对失败通过原子失败标志交给主线程处理。
* 说明:seen三个位分别标记Type20背景、Type21周期和Type21背景;回调与主线程通过原子量交接结果。
*/
static bool receive_asdu(void *context, int address, CS101_ASDU asdu)
{
int type = CS101_ASDU_getTypeID(asdu), cot = CS101_ASDU_getCOT(asdu), i;
(void)context; (void)address;
EXPECT(CS101_ASDU_getCA(asdu) == 1);
EXPECT(CS101_ASDU_getOA(asdu) == 0);
EXPECT(CS101_ASDU_getNumberOfElements(asdu) == 2);
EXPECT(CS101_ASDU_isSequence(asdu) == (expected_sequence != 0));
EXPECT(!CS101_ASDU_isTest(asdu) && !CS101_ASDU_isNegative(asdu));
EXPECT((type == 20 && cot == 2) || (type == 21 && (cot == 1 || cot == 2)));
for (i = 0; i < 2; ++i) {
InformationObject object = CS101_ASDU_getElement(asdu, i);
EXPECT(object != NULL);
EXPECT(InformationObject_getObjectAddress(object) == (type == 20 ? 100 : 200) + i);
if (type == 20) {
StatusAndStatusChangeDetection scd = PackedSinglePointWithSCD_getSCD((PackedSinglePointWithSCD)object);
EXPECT(StatusAndStatusChangeDetection_getSTn(scd) == (uint16_t)(0x1234 + i));
EXPECT(StatusAndStatusChangeDetection_getCDn(scd) == (uint16_t)(0x40 + i));
EXPECT(PackedSinglePointWithSCD_getQuality((PackedSinglePointWithSCD)object) == 0x80);
} else {
float value = MeasuredValueNormalizedWithoutQuality_getValue((MeasuredValueNormalizedWithoutQuality)object);
EXPECT(fabsf(value - (i == 0 ? 0.5f : -0.25f)) < 0.0001f);
}
InformationObject_destroy(object);
}
atomic_fetch_or(&seen, type == 20 ? 1U : cot == 1 ? 2U : 4U);
return true;
}
/**
* 功能:[ENTRY/MAIN] 主站连接并启动数据传输,有界等待三类数据全部出现。
* 输入参数:argc——命令行参数数量;argv——命令行参数数组。
* 输出参数:无。
* 返回值:正常完成返回 0;参数、运行或断言失败以非零状态结束进程。
* 说明:任一断言失败使进程返回失败。
*/
int main(int argc, char **argv)
{
CS104_Connection connection;
unsigned n;
if (argc != 3) return 2;
expected_sequence = atoi(argv[2]);
connection = CS104_Connection_create("127.0.0.1", atoi(argv[1]));
if (connection == NULL) return 2;
CS104_Connection_setASDUReceivedHandler(connection, receive_asdu, NULL);
if (!CS104_Connection_connect(connection)) { CS104_Connection_destroy(connection); return 1; }
CS104_Connection_sendStartDT(connection);
for (n = 0; n < 500U && atomic_load(&seen) != 7U && !atomic_load(&failed); ++n) Thread_sleep(10);
CS104_Connection_close(connection);
CS104_Connection_destroy(connection);
if (atomic_load(&failed) || atomic_load(&seen) != 7U) return 1;
printf("lib60870-C SQ=%d: PASS Type20/21 COT1/2 IOA/value/quality\n", expected_sequence);
return 0;
}