通信协议代码
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
/* 公共ASDU与104传输的集成回归:独立测试中的字节向量仍用于期望值。
|
||||
* 公共套件单独构建时不链接104实现;本文件专门检查I帧接入边界。 */
|
||||
#define main common_asdu_suite_main
|
||||
#include "iec60870_asdu_test.c"
|
||||
#undef main
|
||||
#include "IEC104_Codec.h"
|
||||
|
||||
/**
|
||||
* 功能:验证公共对象构造的ASDU可按标准104布局封装并交付给公共解析器。
|
||||
* 输入参数:无。
|
||||
* 输出参数:无。
|
||||
* 返回值:全部检查成功返回1,断言失败返回0。
|
||||
* 说明:复用30种类型的独立载荷向量,覆盖42种类型/SQ组合及真实I帧序号。
|
||||
*/
|
||||
static int test_common_asdu_in_iframe(void)
|
||||
{
|
||||
const IEC60870_AppLayerParameters_t parameters = IEC104_APP_LAYER_PARAMETERS_DEFAULT;
|
||||
static const uint8_t addresses[2][3] = {{0x56, 0x34, 0x12}, {0x57, 0x34, 0x12}};
|
||||
size_t v;
|
||||
unsigned int layouts = 0;
|
||||
CHECK(parameters.sizeOfTypeId == 1 && parameters.sizeOfVSQ == 1);
|
||||
CHECK(parameters.sizeOfCOT == 2 && parameters.sizeOfCA == 2 && parameters.sizeOfIOA == 3);
|
||||
CHECK(parameters.originatorAddress == 0 && parameters.maxSizeOfASDU == 249);
|
||||
for (v = 0; v < COUNT(vectors); ++v) {
|
||||
const Vector *vector = &vectors[v];
|
||||
int sq;
|
||||
for (sq = 0; sq <= (vector->command ? 0 : 1); ++sq) {
|
||||
uint8_t frame[64], expected[64], apdu[IEC104_MAX_APDU_LEN];
|
||||
IEC60870_AsduHeader_t header = Header();
|
||||
IEC60870_Asdu_t asdu;
|
||||
IEC60870_AsduInfo_t info;
|
||||
IEC104_ApciInfo_t apci;
|
||||
TestObject input, output;
|
||||
const uint8_t *data = NULL;
|
||||
uint16_t length = 0, n;
|
||||
int i, count = vector->command ? 1 : 2;
|
||||
header.cause_of_transmission = vector->command ? 6 : 3;
|
||||
header.is_negative_confirm = vector->command;
|
||||
n = Wire_Header(¶meters, vector->type, (uint8_t)(count | (sq << 7)),
|
||||
vector->command ? 0xC6 : 0x83, expected);
|
||||
CHECK(IEC60870_Asdu_Create(&asdu, ¶meters, frame, sizeof(frame), &header, sq) == IEC60870_ASDU_OK);
|
||||
for (i = 0; i < count; ++i) {
|
||||
CHECK(Sample_Object(vector->type, 0x123456U + (uint32_t)i, &input) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, Object_Base(&input, vector->type)) == IEC60870_ASDU_OK);
|
||||
if (!sq || i == 0) {
|
||||
memcpy(expected + n, addresses[i], 3);
|
||||
n += 3;
|
||||
}
|
||||
memcpy(expected + n, vector->payload, vector->size);
|
||||
n += vector->size;
|
||||
}
|
||||
CHECK(IEC60870_Asdu_Get_Data(&asdu, &data, &length) == IEC60870_ASDU_OK);
|
||||
CHECK(length == n && memcmp(data, expected, n) == 0);
|
||||
CHECK(IEC104_Build_Iframe(apdu, 0x1234, 0x2345, data, length) == length + IEC104_APCI_LEN);
|
||||
CHECK(apdu[0] == 0x68 && apdu[1] == length + 4);
|
||||
CHECK(apdu[2] == 0x68 && apdu[3] == 0x24 && apdu[4] == 0x8A && apdu[5] == 0x46);
|
||||
CHECK(IEC104_Parse_APCI(apdu, (uint16_t)(length + IEC104_APCI_LEN), &apci));
|
||||
CHECK(apci.frame_type == IEC104_FRAME_TYPE_I && apci.send_number == 0x1234 && apci.receive_number == 0x2345);
|
||||
CHECK(apci.asdu_len == length && memcmp(apci.asdu, expected, n) == 0);
|
||||
CHECK(IEC60870_Asdu_Parse(¶meters, apci.asdu, apci.asdu_len, &info) == IEC60870_ASDU_OK);
|
||||
for (i = 0; i < count; ++i) {
|
||||
CHECK(IEC60870_Asdu_Get_Element(&info, (uint8_t)i, &output, Object_Size(vector->type)) == IEC60870_ASDU_OK);
|
||||
CHECK(Object_Base(&output, vector->type)->ioa == 0x123456U + (uint32_t)i);
|
||||
CHECK(Check_Object(vector, &output));
|
||||
}
|
||||
++layouts;
|
||||
}
|
||||
}
|
||||
CHECK(layouts == 42);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 功能:验证公共ASDU容量与104帧容量分别生效。
|
||||
* 输入参数:无。
|
||||
* 输出参数:无。
|
||||
* 返回值:全部检查成功返回1,断言失败返回0。
|
||||
* 说明:公共层可构造254字节,104仍只接受至249字节;拒绝时输出保持不变。
|
||||
*/
|
||||
static int test_transport_length_boundary(void)
|
||||
{
|
||||
IEC60870_AppLayerParameters_t parameters = IEC104_APP_LAYER_PARAMETERS_DEFAULT;
|
||||
IEC60870_AsduHeader_t header = Header();
|
||||
IEC60870_Asdu_t asdu;
|
||||
IEC60870_AsduInfo_t info;
|
||||
IEC60870_MeasuredValueNormalized_t value;
|
||||
IEC104_ApciInfo_t apci;
|
||||
uint8_t frame[IEC60870_MAX_ASDU_LEN], apdu[260], before[260];
|
||||
unsigned int i;
|
||||
CHECK(IEC60870_MeasuredValueNormalized_Create(&value, 0, -1, 0) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Create(&asdu, ¶meters, frame, sizeof(frame), &header, 1) == IEC60870_ASDU_OK);
|
||||
for (i = 0; i < 80; ++i) {
|
||||
value.object.ioa = i;
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, &value.object) == IEC60870_ASDU_OK);
|
||||
}
|
||||
CHECK(asdu.length == 249);
|
||||
CHECK(IEC104_Build_Iframe(apdu, 0, 0, frame, asdu.length) == 255);
|
||||
CHECK(IEC104_Parse_APCI(apdu, 255, &apci));
|
||||
CHECK(apci.asdu_len == 249 && memcmp(apci.asdu, frame, 249) == 0);
|
||||
|
||||
parameters.sizeOfCOT = 1; parameters.sizeOfCA = 1;
|
||||
parameters.sizeOfIOA = 2; parameters.maxSizeOfASDU = 254;
|
||||
header.common_address = 0x67;
|
||||
CHECK(IEC60870_Asdu_Create(&asdu, ¶meters, frame, sizeof(frame), &header, 0) == IEC60870_ASDU_OK);
|
||||
for (i = 0; i < 50; ++i) {
|
||||
value.object.ioa = i;
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, &value.object) == IEC60870_ASDU_OK);
|
||||
}
|
||||
CHECK(asdu.length == 254);
|
||||
CHECK(IEC60870_Asdu_Parse(¶meters, frame, asdu.length, &info) == IEC60870_ASDU_OK);
|
||||
memset(apdu, 0xA5, sizeof(apdu));
|
||||
memcpy(before, apdu, sizeof(before));
|
||||
CHECK(IEC104_Build_Iframe(apdu, 0, 0, frame, 250) == -1);
|
||||
CHECK(memcmp(before, apdu, sizeof(apdu)) == 0);
|
||||
CHECK(IEC104_Build_Iframe(apdu, 0, 0, frame, 254) == -1);
|
||||
CHECK(memcmp(before, apdu, sizeof(apdu)) == 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
if (!test_common_asdu_in_iframe() || !test_transport_length_boundary()) { return 1; }
|
||||
puts("104 ASDU transport: 2 groups passed (42 layouts, 249/254-byte boundary)");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/* Windows回环验证:Pending_Input只读快照、部分读取、耗尽与EOF。 */
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#include "IEC104_Net.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define CHECK(x) do { if (!(x)) { fprintf(stderr, "line %d: %s\n", __LINE__, #x); exit(1); } } while (0)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
IEC104_NetSocket_t listener, accepted;
|
||||
IEC104_NetPeer_t peer;
|
||||
IEC104_NetIoResult_t received;
|
||||
SOCKET client;
|
||||
struct sockaddr_in address;
|
||||
uint8_t sent[64], copy[64];
|
||||
uint16_t port;
|
||||
uint32_t count, ready, index;
|
||||
uint64_t deadline;
|
||||
int error;
|
||||
for (index = 0; index < sizeof(sent); ++index) { sent[index] = (uint8_t)(index * 7U); }
|
||||
CHECK(IEC104_Net_Init(&error) == IEC104_NET_OK);
|
||||
CHECK(IEC104_Net_Listen("127.0.0.1", 0, &listener, &port, &error) == IEC104_NET_OK);
|
||||
client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
CHECK(client != INVALID_SOCKET);
|
||||
memset(&address, 0, sizeof(address));
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
address.sin_port = htons(port);
|
||||
CHECK(connect(client, (const struct sockaddr *)&address, sizeof(address)) == 0);
|
||||
CHECK(IEC104_Net_Wait(listener, IEC104_NET_INVALID_SOCKET, IEC104_NET_EVENT_ACCEPT, 1000,
|
||||
&ready, &error) == IEC104_NET_OK);
|
||||
CHECK(ready & IEC104_NET_EVENT_ACCEPT);
|
||||
CHECK(IEC104_Net_Accept(listener, &accepted, &peer, &error) == IEC104_NET_OK);
|
||||
CHECK(IEC104_Net_Pending_Input(accepted, &count, &error) == IEC104_NET_OK && count == 0);
|
||||
CHECK(send(client, (const char *)sent, sizeof(sent), 0) == sizeof(sent));
|
||||
deadline = IEC104_Net_Now_Ms() + 1000;
|
||||
do {
|
||||
CHECK(IEC104_Net_Pending_Input(accepted, &count, &error) == IEC104_NET_OK);
|
||||
if (count == sizeof(sent)) { break; }
|
||||
Sleep(1);
|
||||
} while (IEC104_Net_Now_Ms() < deadline);
|
||||
CHECK(count == sizeof(sent));
|
||||
CHECK(IEC104_Net_Pending_Input(accepted, &count, &error) == IEC104_NET_OK && count == sizeof(sent));
|
||||
received = IEC104_Net_Recv(accepted, copy, 17);
|
||||
CHECK(received.result == IEC104_NET_OK && received.transferred == 17);
|
||||
CHECK(IEC104_Net_Pending_Input(accepted, &count, &error) == IEC104_NET_OK && count == sizeof(sent) - 17);
|
||||
received = IEC104_Net_Recv(accepted, copy + 17, sizeof(copy) - 17);
|
||||
CHECK(received.result == IEC104_NET_OK && received.transferred == sizeof(copy) - 17);
|
||||
CHECK(memcmp(sent, copy, sizeof(copy)) == 0);
|
||||
CHECK(IEC104_Net_Pending_Input(accepted, &count, &error) == IEC104_NET_OK && count == 0);
|
||||
CHECK(shutdown(client, SD_SEND) == 0);
|
||||
CHECK(IEC104_Net_Wait(IEC104_NET_INVALID_SOCKET, accepted, IEC104_NET_EVENT_READ, 1000,
|
||||
&ready, &error) == IEC104_NET_OK && (ready & IEC104_NET_EVENT_READ));
|
||||
CHECK(IEC104_Net_Pending_Input(accepted, &count, &error) == IEC104_NET_OK && count == 0);
|
||||
CHECK(IEC104_Net_Recv(accepted, copy, sizeof(copy)).result == IEC104_NET_EOF);
|
||||
count = 123;
|
||||
CHECK(IEC104_Net_Pending_Input(IEC104_NET_INVALID_SOCKET, &count, &error) == IEC104_NET_ERROR);
|
||||
CHECK(count == 123 && error != 0);
|
||||
CHECK(IEC104_Net_Pending_Input(accepted, NULL, &error) == IEC104_NET_ERROR);
|
||||
CHECK(IEC104_Net_Close(accepted, &error) == IEC104_NET_OK);
|
||||
CHECK(IEC104_Net_Close(listener, &error) == IEC104_NET_OK);
|
||||
CHECK(closesocket(client) == 0);
|
||||
IEC104_Net_Cleanup();
|
||||
puts("Windows Net pending input: loopback passed");
|
||||
return 0;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,697 @@
|
||||
/* 共用子站发送接口回归:复用可控Net后端,验证真实104提交路径。
|
||||
* 不另行链接TcpServer.c、Net后端或事件环测试翻译单元。 */
|
||||
#define main event_ring_suite_main
|
||||
#include "iec104_tcp_event_ring_test.c"
|
||||
#undef main
|
||||
#include "IEC104_Slave.h"
|
||||
|
||||
static const IEC60870_AppLayerParameters_t slave_parameters = IEC104_APP_LAYER_PARAMETERS_DEFAULT;
|
||||
/* Type100、TEST=1、OA=0x5A、CA=0x1234、IOA=0、QOI=20。 */
|
||||
static const uint8_t interrogation[] = {100, 1, 0x86, 0x5A, 0x34, 0x12, 0, 0, 0, 20};
|
||||
|
||||
/**
|
||||
* 功能:检查期望报文只改动COT首字节。
|
||||
* 输入参数:actual——实际ASDU;original——输入副本;length——长度;cot——期望COT字节。
|
||||
* 输出参数:无。
|
||||
* 返回值:无,断言失败时终止测试。
|
||||
*/
|
||||
static void check_reply(const uint8_t *actual, const uint8_t *original, size_t length, uint8_t cot)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < length; ++i) { CHECK(actual[i] == (i == 2U ? cot : original[i])); }
|
||||
}
|
||||
|
||||
/**
|
||||
* 功能:验证12种字段宽度下的原文借用、原地修改和失败原子性。
|
||||
* 输入参数:无。
|
||||
* 输出参数:无。
|
||||
* 返回值:无。
|
||||
*/
|
||||
static void test_mutable_views(void)
|
||||
{
|
||||
int cot, ca, ioa;
|
||||
for (cot = 1; cot <= 2; ++cot) {
|
||||
for (ca = 1; ca <= 2; ++ca) {
|
||||
for (ioa = 1; ioa <= 3; ++ioa) {
|
||||
IEC60870_AppLayerParameters_t p = {1, 1, cot, 0, ca, ioa, 254};
|
||||
uint8_t bytes[16] = {100, 1, 0xC6}, original[16];
|
||||
uint16_t length = (uint16_t)(2 + cot + ca + ioa + 1), exported = 0;
|
||||
const uint8_t *data = NULL;
|
||||
IEC60870_Asdu_t object, snapshot;
|
||||
IEC60870_InterrogationCommand_t command;
|
||||
if (cot == 2) { bytes[3] = 0x5A; }
|
||||
bytes[2 + cot] = 0x34;
|
||||
if (ca == 2) { bytes[3 + cot] = 0x12; }
|
||||
bytes[length - 1U] = 20;
|
||||
memcpy(original, bytes, sizeof(bytes));
|
||||
CHECK(IEC60870_Asdu_Create_From_Buffer(&object, &p, bytes, length) == IEC60870_ASDU_OK);
|
||||
CHECK(object.buffer == bytes && object.capacity == length && object.is_from_buffer);
|
||||
CHECK(memcmp(bytes, original, sizeof(bytes)) == 0);
|
||||
CHECK(IEC60870_Asdu_Set_COT(&object, 7) == IEC60870_ASDU_OK);
|
||||
check_reply(bytes, original, length, 0xC7);
|
||||
CHECK(IEC60870_Asdu_Set_Negative(&object, 0) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Get_Data(&object, &data, &exported) == IEC60870_ASDU_OK);
|
||||
CHECK(data == bytes && exported == length);
|
||||
check_reply(data, original, length, 0x87);
|
||||
snapshot = object;
|
||||
CHECK(IEC60870_Asdu_Set_COT(&object, -1) == IEC60870_ASDU_ERROR);
|
||||
CHECK(IEC60870_Asdu_Set_COT(&object, 64) == IEC60870_ASDU_ERROR);
|
||||
CHECK(IEC60870_Asdu_Set_Negative(&object, 2) == IEC60870_ASDU_ERROR);
|
||||
CHECK(IEC60870_Asdu_Create_From_Buffer(&object, &p, bytes, length - 1U) == IEC60870_ASDU_ERROR);
|
||||
CHECK(memcmp(&object, &snapshot, sizeof(object)) == 0);
|
||||
CHECK(IEC60870_InterrogationCommand_Create(&command, 0, 20) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&object, &command.object) == IEC60870_ASDU_ERROR);
|
||||
CHECK(memcmp(&object, &snapshot, sizeof(object)) == 0);
|
||||
check_reply(bytes, original, length, 0x87);
|
||||
CHECK(memcmp(bytes + length, original + length, sizeof(bytes) - length) == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
/* 未知类型及零对象可原文回显,但不能作为已校验业务对象执行。 */
|
||||
IEC60870_AppLayerParameters_t p = {1, 1, 1, 0, 1, 1, 254};
|
||||
uint8_t bytes[254] = {200, 0, 0x86, 1};
|
||||
IEC60870_Asdu_t object, snapshot;
|
||||
IEC60870_AsduInfo_t info;
|
||||
const uint8_t *data = NULL;
|
||||
uint16_t length = 0;
|
||||
CHECK(IEC60870_Asdu_Create_From_Buffer(&object, &p, bytes, 4) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Parse(&p, bytes, 4, &info) == IEC60870_ASDU_UNSUPPORTED);
|
||||
CHECK(IEC60870_Asdu_Set_COT(&object, 44) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Get_Data(&object, &data, &length) == IEC60870_ASDU_OK && length == 4);
|
||||
CHECK(data == bytes && bytes[2] == 0xAC);
|
||||
CHECK(IEC60870_Asdu_Create_From_Buffer(&object, &p, bytes, 254) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Get_Data(&object, &data, &length) == IEC60870_ASDU_OK && length == 254);
|
||||
snapshot = object;
|
||||
CHECK(IEC60870_Asdu_Create_From_Buffer(&object, &p, bytes, 255) == IEC60870_ASDU_ERROR);
|
||||
CHECK(IEC60870_Asdu_Create_From_Buffer(&object, NULL, bytes, 4) == IEC60870_ASDU_ERROR);
|
||||
CHECK(IEC60870_Asdu_Create_From_Buffer(&object, &p, NULL, 4) == IEC60870_ASDU_ERROR);
|
||||
CHECK(IEC60870_Asdu_Create_From_Buffer(NULL, &p, bytes, 4) == IEC60870_ASDU_ERROR);
|
||||
CHECK(memcmp(&object, &snapshot, sizeof(object)) == 0);
|
||||
CHECK(IEC60870_Asdu_Set_COT(NULL, 7) == IEC60870_ASDU_ERROR);
|
||||
CHECK(IEC60870_Asdu_Set_Negative(NULL, 0) == IEC60870_ASDU_ERROR);
|
||||
object.is_from_buffer = 2;
|
||||
CHECK(IEC60870_Asdu_Set_COT(&object, 7) == IEC60870_ASDU_ERROR && bytes[2] == 0xAC);
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
IEC60870_MasterConnection_t connection;
|
||||
IEC60870_AppLayerParameters_t parameters;
|
||||
IEC60870_Asdu_t *seen;
|
||||
unsigned calls;
|
||||
bool negative;
|
||||
bool result;
|
||||
} TestConnection;
|
||||
|
||||
/* 测试接入只记录分派参数和返回值,用来确认共用层不替代协议实现。 */
|
||||
static bool record_asdu(IEC60870_MasterConnection_t *self, IEC60870_Asdu_t *object)
|
||||
{
|
||||
TestConnection *test = (TestConnection *)self->object;
|
||||
CHECK(self == &test->connection);
|
||||
test->seen = object;
|
||||
test->calls += 1U;
|
||||
return test->result;
|
||||
}
|
||||
static bool record_con(IEC60870_MasterConnection_t *self, IEC60870_Asdu_t *object, bool negative)
|
||||
{
|
||||
TestConnection *test = (TestConnection *)self->object;
|
||||
test->negative = negative;
|
||||
test->calls += 10U;
|
||||
return record_asdu(self, object);
|
||||
}
|
||||
static bool record_term(IEC60870_MasterConnection_t *self, IEC60870_Asdu_t *object)
|
||||
{
|
||||
TestConnection *test = (TestConnection *)self->object;
|
||||
test->calls += 100U;
|
||||
return record_asdu(self, object);
|
||||
}
|
||||
static const IEC60870_AppLayerParameters_t *record_parameters(IEC60870_MasterConnection_t *self)
|
||||
{
|
||||
return &((TestConnection *)self->object)->parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* 功能:验证共用接口的回调分派、布尔返回和非104参数传递。
|
||||
* 输入参数:无。
|
||||
* 输出参数:无。
|
||||
* 返回值:无。
|
||||
*/
|
||||
static void test_common_dispatch(void)
|
||||
{
|
||||
TestConnection test = {0};
|
||||
IEC60870_MasterConnection_t empty = {0};
|
||||
IEC60870_Asdu_t object = {0};
|
||||
test.parameters = (IEC60870_AppLayerParameters_t){1, 1, 1, 0, 1, 2, 100};
|
||||
test.connection.object = &test;
|
||||
test.connection.send_asdu = record_asdu;
|
||||
test.connection.send_act_con = record_con;
|
||||
test.connection.send_act_term = record_term;
|
||||
test.connection.get_application_layer_parameters = record_parameters;
|
||||
test.result = true;
|
||||
CHECK(IEC60870_MasterConnection_Send_ASDU(&test.connection, &object));
|
||||
CHECK(IEC60870_MasterConnection_Send_ACT_CON(&test.connection, &object, true));
|
||||
CHECK(test.negative);
|
||||
CHECK(IEC60870_MasterConnection_Send_ACT_TERM(&test.connection, &object));
|
||||
CHECK(test.calls == 113U && test.seen == &object);
|
||||
CHECK(IEC60870_MasterConnection_Get_Application_Layer_Parameters(&test.connection) == &test.parameters);
|
||||
test.result = false;
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_CON(&test.connection, &object, false));
|
||||
CHECK(!test.negative && test.calls == 124U);
|
||||
CHECK(!IEC60870_MasterConnection_Send_ASDU(NULL, &object));
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_CON(NULL, &object, false));
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_TERM(NULL, &object));
|
||||
CHECK(!IEC60870_MasterConnection_Send_ASDU(&empty, &object));
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_CON(&empty, &object, false));
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_TERM(&empty, &object));
|
||||
CHECK(IEC60870_MasterConnection_Get_Application_Layer_Parameters(NULL) == NULL);
|
||||
CHECK(IEC60870_MasterConnection_Get_Application_Layer_Parameters(&empty) == NULL);
|
||||
}
|
||||
|
||||
static IEC104_Slave_t *service;
|
||||
|
||||
/* 仅测试为了注入底层故障取得池中对象;正常业务测试只调用Slave公开接口。 */
|
||||
static void locate_test_server(void)
|
||||
{
|
||||
size_t i;
|
||||
sut = NULL;
|
||||
for (i = 0U; i < IEC104_TCP_SERVER_CAPACITY; ++i) {
|
||||
if (server_pool[i].in_use != 0U) { sut = &server_pool[i].server; break; }
|
||||
}
|
||||
CHECK(sut != NULL);
|
||||
}
|
||||
|
||||
static void service_poll(void) { CHECK(IEC104_Slave_Poll(service, 0, NULL) == IEC104_TCP_OK); }
|
||||
|
||||
static unsigned service_drain(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t event;
|
||||
IEC104_TcpEventResult_t result;
|
||||
unsigned count = 0;
|
||||
while ((result = IEC104_Slave_NextEvent(service, &event)) == IEC104_TCP_EVENT_READY) { ++count; }
|
||||
CHECK(result == IEC104_TCP_EVENT_EMPTY);
|
||||
return count;
|
||||
}
|
||||
|
||||
static void service_open(void)
|
||||
{
|
||||
IEC104_SlaveConfig_t config;
|
||||
memset(&fake, 0, sizeof(fake));
|
||||
fake.now = 1000;
|
||||
service = IEC104_Slave_Create();
|
||||
CHECK(service != NULL);
|
||||
locate_test_server();
|
||||
IEC104_Slave_Config_Default(&config);
|
||||
config.protocol.link.k = IEC104_TCP_RECORD_CAPACITY;
|
||||
CHECK(IEC104_Slave_Init(service, &config, NULL) == IEC104_TCP_OK);
|
||||
}
|
||||
|
||||
static void service_connect(void)
|
||||
{
|
||||
fake.accept_pending = 1;
|
||||
service_poll();
|
||||
CHECK(service_drain() == 1);
|
||||
push(start_act, sizeof(start_act));
|
||||
service_poll();
|
||||
CHECK(service_drain() == 1);
|
||||
}
|
||||
|
||||
static void service_begin(void) { service_open(); service_connect(); }
|
||||
|
||||
static void service_end(void)
|
||||
{
|
||||
CHECK(IEC104_Slave_Close(service, NULL) == IEC104_TCP_OK);
|
||||
service_drain();
|
||||
CHECK(IEC104_Slave_Destroy(service, NULL) == IEC104_TCP_OK);
|
||||
service = NULL;
|
||||
}
|
||||
|
||||
static void receive_request(IEC104_SlaveEvent_t *event, uint16_t ns)
|
||||
{
|
||||
uint8_t input[16];
|
||||
CHECK(IEC104_Build_Iframe(input, ns, 0, interrogation, sizeof(interrogation)) == 16);
|
||||
push(input, sizeof(input));
|
||||
service_poll();
|
||||
CHECK(IEC104_Slave_NextEvent(service, event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event->transport.type == IEC104_TCP_EVENT_I_FRAME);
|
||||
CHECK(event->asdu_result == IEC60870_ASDU_OK);
|
||||
}
|
||||
|
||||
/* 无需手动Init连接或解析ASDU,直接确认;检查完整报文及出队失败的输出保留。 */
|
||||
static void test_event_replies(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t event, snapshot;
|
||||
const IEC60870_AppLayerParameters_t *parameters;
|
||||
size_t offset;
|
||||
unsigned i;
|
||||
static const uint8_t expected_cot[] = {0x87, 0xC7, 0x8A};
|
||||
service_begin();
|
||||
receive_request(&event, 0);
|
||||
CHECK(event.asdu.buffer == event.transport.data.i_frame.apdu + IEC104_APCI_LEN);
|
||||
parameters = IEC60870_MasterConnection_Get_Application_Layer_Parameters(&event.connection);
|
||||
CHECK(parameters != NULL && parameters->sizeOfCOT == 2 && parameters->sizeOfCA == 2 &&
|
||||
parameters->sizeOfIOA == 3 && parameters->maxSizeOfASDU == 249);
|
||||
CHECK(parameters == IEC104_Slave_Get_Application_Layer_Parameters(service));
|
||||
offset = fake.sent_bytes;
|
||||
CHECK(IEC60870_MasterConnection_Send_ACT_CON(&event.connection, &event.asdu, false));
|
||||
CHECK(IEC60870_MasterConnection_Send_ACT_CON(&event.connection, &event.asdu, true));
|
||||
CHECK(IEC60870_MasterConnection_Send_ACT_TERM(&event.connection, &event.asdu));
|
||||
CHECK(IEC104_Slave_Get_Last_Submit_Result(&event.connection) == IEC104_TCP_SUBMIT_OK);
|
||||
CHECK(fake.sent_bytes == offset + 48U);
|
||||
for (i = 0; i < 3; ++i) {
|
||||
const uint8_t *frame = fake.output + offset + i * 16U;
|
||||
CHECK(frame[0] == 0x68 && frame[1] == 14 && frame[2] == 2U * i && frame[3] == 0);
|
||||
CHECK(frame[4] == 2 && frame[5] == 0);
|
||||
check_reply(frame + 6, interrogation, 10, expected_cot[i]);
|
||||
}
|
||||
check_reply(event.asdu.buffer, interrogation, 10, 0x8A);
|
||||
memcpy(&snapshot, &event, sizeof(snapshot));
|
||||
CHECK(IEC104_Slave_NextEvent(service, &event) == IEC104_TCP_EVENT_EMPTY);
|
||||
CHECK(memcmp(&snapshot, &event, sizeof(event)) == 0);
|
||||
CHECK(IEC104_Slave_NextEvent(NULL, &event) == IEC104_TCP_EVENT_ERROR);
|
||||
CHECK(memcmp(&snapshot, &event, sizeof(event)) == 0);
|
||||
CHECK(IEC104_Slave_NextEvent(service, NULL) == IEC104_TCP_EVENT_ERROR);
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* 同一自动绑定的连接也可提交业务另行构造的ASDU。 */
|
||||
static void test_constructed_asdu(void)
|
||||
{
|
||||
uint8_t buffer[249];
|
||||
IEC104_SlaveEvent_t event;
|
||||
IEC60870_Asdu_t object;
|
||||
IEC60870_AsduHeader_t header = {3, 0, 0, 0, 1};
|
||||
IEC60870_SinglePointInformation_t point;
|
||||
const uint8_t *data = NULL;
|
||||
uint16_t length = 0;
|
||||
service_begin();
|
||||
receive_request(&event, 0);
|
||||
CHECK(IEC60870_Asdu_Create(&object, &slave_parameters, buffer, sizeof(buffer), &header, 0) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Set_COT(&object, 20) == IEC60870_ASDU_OK);
|
||||
CHECK(!IEC60870_MasterConnection_Send_ASDU(&event.connection, &object));
|
||||
CHECK(IEC104_Slave_Get_Last_Submit_Result(&event.connection) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC60870_SinglePointInformation_Create(&point, 1, 1, 0) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&object, &point.object) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Get_Data(&object, &data, &length) == IEC60870_ASDU_OK && length == 10);
|
||||
CHECK(IEC60870_MasterConnection_Send_ASDU(&event.connection, &object));
|
||||
CHECK(fake.sent_bytes == 22);
|
||||
check_reply(fake.output + 12, asdu, 10, 20);
|
||||
CHECK(memcmp(fake.output + 12, data, length) == 0);
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* HIGH满后的WAIT不接手,复制事件保留已修改字段后可在容量恢复时重试。 */
|
||||
static void test_wait_and_retry(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t event, pending;
|
||||
IEC104_TcpServerStatus_t before, after;
|
||||
unsigned accepted;
|
||||
service_begin();
|
||||
receive_request(&event, 0);
|
||||
fake.send_blocked = 1;
|
||||
for (accepted = 0; accepted < 200; ++accepted) {
|
||||
if (!IEC60870_MasterConnection_Send_ASDU(&event.connection, &event.asdu)) { break; }
|
||||
}
|
||||
CHECK(accepted > 0 && accepted < 200);
|
||||
CHECK(IEC104_Slave_Get_Last_Submit_Result(&event.connection) == IEC104_TCP_SUBMIT_WAIT);
|
||||
CHECK(IEC104_Slave_Get_Status(service, &before, NULL) == IEC104_TCP_OK);
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_CON(&event.connection, &event.asdu, true));
|
||||
CHECK(IEC104_Slave_Get_Last_Submit_Result(&event.connection) == IEC104_TCP_SUBMIT_WAIT);
|
||||
check_reply(event.asdu.buffer, interrogation, 10, 0xC7);
|
||||
CHECK(IEC104_Slave_Get_Status(service, &after, NULL) == IEC104_TCP_OK);
|
||||
CHECK(before.high_queue.pending_asdu_count == after.high_queue.pending_asdu_count);
|
||||
CHECK(IEC104_Slave_Copy_Event(&pending, &event));
|
||||
memset(&event, 0, sizeof(event));
|
||||
fake.send_blocked = 0;
|
||||
fake.now += 100;
|
||||
service_poll();
|
||||
CHECK(IEC60870_MasterConnection_Send_ACT_CON(&pending.connection, &pending.asdu, true));
|
||||
CHECK(IEC104_Slave_Get_Last_Submit_Result(&pending.connection) == IEC104_TCP_SUBMIT_OK);
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* 保留101布局/254长度/NULL拒绝,以及复制自引用接口后的明确失败。 */
|
||||
static void test_invalid_inputs(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t event, snapshot, destination, before;
|
||||
IEC60870_AppLayerParameters_t p = {1, 1, 1, 0, 1, 1, 254};
|
||||
IEC60870_Asdu_t object;
|
||||
uint8_t bytes[254] = {200, 0, 0x86, 1};
|
||||
size_t sent;
|
||||
service_begin();
|
||||
receive_request(&event, 0);
|
||||
snapshot = event; /* 故意模拟错误的按值复制。 */
|
||||
CHECK(IEC104_Slave_Get_Last_Submit_Result(NULL) == IEC104_TCP_SUBMIT_ERROR);
|
||||
sent = fake.sent_bytes;
|
||||
CHECK(IEC60870_Asdu_Create_From_Buffer(&object, &p, bytes, 4) == IEC60870_ASDU_OK);
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_CON(&event.connection, &object, false));
|
||||
CHECK(bytes[2] == 0x87);
|
||||
CHECK(IEC104_Slave_Get_Last_Submit_Result(&event.connection) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC104_Slave_Enqueue_Event(service, &object) == IEC104_TCP_SUBMIT_ERROR);
|
||||
p = slave_parameters;
|
||||
p.maxSizeOfASDU = 254;
|
||||
CHECK(IEC60870_Asdu_Create_From_Buffer(&object, &p, bytes, 254) == IEC60870_ASDU_OK);
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_TERM(&event.connection, &object));
|
||||
CHECK(bytes[2] == 0x8A);
|
||||
CHECK(!IEC60870_MasterConnection_Send_ASDU(&event.connection, NULL));
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_CON(&event.connection, NULL, false));
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_TERM(&event.connection, NULL));
|
||||
CHECK(IEC104_Slave_Enqueue_Event(service, NULL) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(fake.sent_bytes == sent);
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_TERM(&snapshot.connection, &object));
|
||||
CHECK(IEC60870_MasterConnection_Get_Application_Layer_Parameters(&snapshot.connection) == NULL);
|
||||
CHECK(IEC104_Slave_Get_Last_Submit_Result(&snapshot.connection) == IEC104_TCP_SUBMIT_ERROR);
|
||||
memset(&destination, 0x5A, sizeof(destination));
|
||||
memcpy(&before, &destination, sizeof(before));
|
||||
CHECK(!IEC104_Slave_Copy_Event(&destination, &snapshot));
|
||||
CHECK(!IEC104_Slave_Copy_Event(&destination, NULL));
|
||||
CHECK(!IEC104_Slave_Copy_Event(NULL, &event));
|
||||
CHECK(memcmp(&destination, &before, sizeof(destination)) == 0);
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* 身份输出独立于事件存储;非法来源、自引用和元数据不得污染输出或复制目标。 */
|
||||
static void test_event_identity(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t event, copied, moved;
|
||||
IEC104_SlaveEventIdentity_t identity, before, copy_identity;
|
||||
size_t sent;
|
||||
service_begin();
|
||||
receive_request(&event, 0);
|
||||
sent = fake.sent_bytes;
|
||||
CHECK(IEC104_Slave_Get_Event_Identity(service, &event, &identity));
|
||||
CHECK(identity.slave == service && identity.instance_id != 0U &&
|
||||
identity.generation == event.transport.generation);
|
||||
before = identity;
|
||||
CHECK(!IEC104_Slave_Get_Event_Identity(NULL, &event, &identity));
|
||||
CHECK(!IEC104_Slave_Get_Event_Identity(service, NULL, &identity));
|
||||
CHECK(!IEC104_Slave_Get_Event_Identity(service, &event, NULL));
|
||||
moved = event; /* 未修复自引用的按值副本必须拒绝。 */
|
||||
CHECK(!IEC104_Slave_Get_Event_Identity(service, &moved, &identity));
|
||||
CHECK(memcmp(&identity, &before, sizeof(identity)) == 0);
|
||||
CHECK(IEC104_Slave_Copy_Event(&copied, &event));
|
||||
++copied.transport.generation; /* 两份连接代次不一致,不能接受或继续传播。 */
|
||||
CHECK(!IEC104_Slave_Get_Event_Identity(service, &copied, &identity));
|
||||
CHECK(!IEC104_Slave_Copy_Event(&moved, &copied));
|
||||
CHECK(memcmp(&identity, &before, sizeof(identity)) == 0);
|
||||
CHECK(IEC104_Slave_Copy_Event(&copied, &event));
|
||||
memset(&event, 0, sizeof(event));
|
||||
CHECK(IEC104_Slave_Get_Event_Identity(service, &copied, ©_identity));
|
||||
CHECK(copy_identity.slave == identity.slave && copy_identity.instance_id == identity.instance_id &&
|
||||
copy_identity.generation == identity.generation);
|
||||
CHECK(IEC104_Slave_Check_Request(service, &identity) == IEC104_SLAVE_REQUEST_READY);
|
||||
#if IEC104_TCP_SERVER_CAPACITY > 1
|
||||
{
|
||||
IEC104_Slave_t *other = IEC104_Slave_Create();
|
||||
CHECK(other != NULL);
|
||||
CHECK(!IEC104_Slave_Get_Event_Identity(other, &copied, &identity));
|
||||
CHECK(memcmp(&identity, &before, sizeof(identity)) == 0);
|
||||
copy_identity.slave = other; /* 即使局部编号相同,其他服务身份也不能用于当前会话。 */
|
||||
CHECK(IEC104_Slave_Check_Request(service, ©_identity) == IEC104_SLAVE_REQUEST_STALE);
|
||||
CHECK(IEC104_Slave_Destroy(other, NULL) == IEC104_TCP_OK);
|
||||
}
|
||||
#endif
|
||||
CHECK(fake.sent_bytes == sent);
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* 通知FIFO优先于当前传输状态:不能跨过尚未消费的STOP/START继续旧业务。 */
|
||||
static void test_request_state(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t event;
|
||||
IEC104_SlaveEventIdentity_t identity = {0};
|
||||
size_t sent;
|
||||
service_open();
|
||||
CHECK(IEC104_Slave_Check_Request(NULL, &identity) == IEC104_SLAVE_REQUEST_ERROR);
|
||||
CHECK(IEC104_Slave_Check_Request(service, NULL) == IEC104_SLAVE_REQUEST_ERROR);
|
||||
CHECK(IEC104_Slave_Check_Request(service, &identity) == IEC104_SLAVE_REQUEST_STALE);
|
||||
fake.accept_pending = 1;
|
||||
service_poll();
|
||||
CHECK(IEC104_Slave_Check_Request(service, &identity) == IEC104_SLAVE_REQUEST_WAIT_EVENTS);
|
||||
CHECK(IEC104_Slave_NextEvent(service, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.transport.type == IEC104_TCP_EVENT_CONNECTED);
|
||||
CHECK(IEC104_Slave_Get_Event_Identity(service, &event, &identity));
|
||||
CHECK(IEC104_Slave_Check_Request(service, &identity) == IEC104_SLAVE_REQUEST_STALE);
|
||||
push(start_act, sizeof(start_act)); service_poll();
|
||||
CHECK(IEC104_Slave_Check_Request(service, &identity) == IEC104_SLAVE_REQUEST_WAIT_EVENTS);
|
||||
CHECK(service_drain() == 1);
|
||||
CHECK(IEC104_Slave_Check_Request(service, &identity) == IEC104_SLAVE_REQUEST_READY);
|
||||
push(stop_act, sizeof(stop_act)); service_poll();
|
||||
CHECK(IEC104_Slave_Check_Request(service, &identity) == IEC104_SLAVE_REQUEST_WAIT_EVENTS);
|
||||
CHECK(service_drain() == 1);
|
||||
CHECK(IEC104_Slave_Check_Request(service, &identity) == IEC104_SLAVE_REQUEST_STALE);
|
||||
push(start_act, sizeof(start_act)); service_poll();
|
||||
CHECK(service_drain() == 1);
|
||||
push(stop_act, sizeof(stop_act)); service_poll();
|
||||
push(start_act, sizeof(start_act)); service_poll();
|
||||
CHECK(IEC104_Slave_Check_Request(service, &identity) == IEC104_SLAVE_REQUEST_WAIT_EVENTS);
|
||||
CHECK(IEC104_Slave_NextEvent(service, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.transport.type == IEC104_TCP_EVENT_DEACTIVATED);
|
||||
CHECK(IEC104_Slave_Check_Request(service, &identity) == IEC104_SLAVE_REQUEST_WAIT_EVENTS);
|
||||
CHECK(IEC104_Slave_NextEvent(service, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.transport.type == IEC104_TCP_EVENT_ACTIVATED);
|
||||
sent = fake.sent_bytes;
|
||||
CHECK(IEC104_Slave_Check_Request(service, &identity) == IEC104_SLAVE_REQUEST_READY);
|
||||
CHECK(IEC104_Slave_Check_Request(service, &identity) == IEC104_SLAVE_REQUEST_READY);
|
||||
CHECK(fake.sent_bytes == sent && service_drain() == 0U);
|
||||
CHECK(IEC104_Slave_Close_Connection(service, &event, NULL) == IEC104_TCP_OK);
|
||||
CHECK(IEC104_Slave_Check_Request(service, &identity) == IEC104_SLAVE_REQUEST_WAIT_EVENTS);
|
||||
CHECK(service_drain() == 1);
|
||||
CHECK(IEC104_Slave_Check_Request(service, &identity) == IEC104_SLAVE_REQUEST_STALE);
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* 旧事件的发送和关闭操作均不能作用于新连接,业务无须提取或回传generation。 */
|
||||
static void test_stale_generation(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t old_event, current;
|
||||
IEC104_SlaveEventIdentity_t old_identity, current_identity;
|
||||
size_t sent;
|
||||
service_begin();
|
||||
receive_request(&old_event, 0);
|
||||
CHECK(IEC104_Slave_Get_Event_Identity(service, &old_event, &old_identity));
|
||||
CHECK(IEC104_Slave_Close_Connection(service, &old_event, NULL) == IEC104_TCP_OK);
|
||||
service_drain();
|
||||
service_connect();
|
||||
receive_request(¤t, 0);
|
||||
CHECK(old_event.transport.generation != current.transport.generation);
|
||||
CHECK(IEC104_Slave_Get_Event_Identity(service, &old_event, ¤t_identity));
|
||||
CHECK(current_identity.generation == old_identity.generation); /* 历史事件仍可识别,但不能继续业务。 */
|
||||
CHECK(IEC104_Slave_Check_Request(service, &old_identity) == IEC104_SLAVE_REQUEST_STALE);
|
||||
CHECK(IEC104_Slave_Get_Event_Identity(service, ¤t, ¤t_identity));
|
||||
CHECK(IEC104_Slave_Check_Request(service, ¤t_identity) == IEC104_SLAVE_REQUEST_READY);
|
||||
sent = fake.sent_bytes;
|
||||
CHECK(IEC104_Slave_Close_Connection(service, &old_event, NULL) == IEC104_TCP_STALE_GENERATION);
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_CON(&old_event.connection, &old_event.asdu, false));
|
||||
CHECK(IEC104_Slave_Get_Last_Submit_Result(&old_event.connection) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(fake.sent_bytes == sent && old_event.asdu.buffer[2] == 0x87);
|
||||
CHECK(IEC60870_MasterConnection_Send_ACT_CON(¤t.connection, ¤t.asdu, false));
|
||||
CHECK(fake.sent_bytes == sent + 16U);
|
||||
service_end();
|
||||
}
|
||||
|
||||
static void test_fatal_result(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t event;
|
||||
service_begin();
|
||||
receive_request(&event, 0);
|
||||
/* 故障注入只在测试中访问底层,不为业务增加内部句柄访问接口。 */
|
||||
sut->queue_initialized = 0;
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_CON(&event.connection, &event.asdu, false));
|
||||
CHECK(IEC104_Slave_Get_Last_Submit_Result(&event.connection) == IEC104_TCP_SUBMIT_FATAL);
|
||||
CHECK(event.asdu.buffer[2] == 0x87);
|
||||
sut->queue_initialized = 1;
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* 源事件被后续请求覆盖后,任务副本仍持有自己的原文与应答接口。 */
|
||||
static void test_deferred_event_copy(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t source, pending;
|
||||
uint64_t original_serial;
|
||||
service_begin();
|
||||
receive_request(&source, 0);
|
||||
original_serial = source.transport.data.i_frame.receive_serial;
|
||||
CHECK(IEC60870_Asdu_Set_COT(&source.asdu, 7) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC104_Slave_Copy_Event(&pending, &source));
|
||||
CHECK(IEC104_Slave_Copy_Event(&pending, &pending));
|
||||
CHECK(pending.asdu.buffer != source.asdu.buffer);
|
||||
CHECK(pending.connection.object == &pending);
|
||||
receive_request(&source, 1);
|
||||
CHECK(source.transport.data.i_frame.receive_serial != original_serial);
|
||||
CHECK(pending.transport.data.i_frame.receive_serial == original_serial);
|
||||
CHECK(source.asdu.buffer[2] == 0x86 && pending.asdu.buffer[2] == 0x87);
|
||||
CHECK(IEC60870_MasterConnection_Send_ACT_TERM(&pending.connection, &pending.asdu));
|
||||
CHECK(source.asdu.buffer[2] == 0x86 && pending.asdu.buffer[2] == 0x8A);
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* 服务重启、销毁和池槽复用后,仍存活的任务副本都不得向新实例发送。 */
|
||||
static void test_service_reuse(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t old_event, current;
|
||||
IEC104_SlaveEventIdentity_t old_identity, identity;
|
||||
IEC104_SlaveConfig_t config;
|
||||
IEC104_Slave_t *old_service;
|
||||
size_t sent;
|
||||
service_begin();
|
||||
receive_request(&old_event, 0);
|
||||
CHECK(IEC104_Slave_Get_Event_Identity(service, &old_event, &old_identity));
|
||||
CHECK(IEC104_Slave_Close(service, NULL) == IEC104_TCP_OK);
|
||||
CHECK(IEC104_Slave_Init(service, NULL, NULL) == IEC104_TCP_INVALID_ARGUMENT);
|
||||
service_drain();
|
||||
IEC104_Slave_Config_Default(&config);
|
||||
CHECK(IEC104_Slave_Init(service, &config, NULL) == IEC104_TCP_OK);
|
||||
service_connect();
|
||||
receive_request(¤t, 0);
|
||||
CHECK(IEC104_Slave_Check_Request(service, &old_identity) == IEC104_SLAVE_REQUEST_STALE);
|
||||
sent = fake.sent_bytes;
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_CON(&old_event.connection, &old_event.asdu, false));
|
||||
CHECK(fake.sent_bytes == sent);
|
||||
CHECK(IEC60870_MasterConnection_Send_ACT_CON(¤t.connection, ¤t.asdu, false));
|
||||
old_service = service;
|
||||
service_end();
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_TERM(&old_event.connection, &old_event.asdu));
|
||||
service_begin();
|
||||
CHECK(service == old_service); /* 默认先复用门面第一个槽。 */
|
||||
receive_request(¤t, 0);
|
||||
CHECK(IEC104_Slave_Get_Event_Identity(service, ¤t, &identity));
|
||||
CHECK(old_identity.instance_id != identity.instance_id);
|
||||
CHECK(!IEC104_Slave_Get_Event_Identity(service, &old_event, &identity));
|
||||
CHECK(IEC104_Slave_Check_Request(service, &old_identity) == IEC104_SLAVE_REQUEST_STALE);
|
||||
sent = fake.sent_bytes;
|
||||
CHECK(!IEC60870_MasterConnection_Send_ACT_CON(&old_event.connection, &old_event.asdu, false));
|
||||
CHECK(IEC104_Slave_Close_Connection(service, &old_event, NULL) == IEC104_TCP_INVALID_ARGUMENT);
|
||||
CHECK(fake.sent_bytes == sent);
|
||||
CHECK(IEC60870_MasterConnection_Send_ACT_CON(¤t.connection, ¤t.asdu, false));
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* 两层池共享容量;创建失败不泄漏,Close后的收尾通知不能被Destroy悄悄丢弃。 */
|
||||
static void test_lifecycle(void)
|
||||
{
|
||||
IEC104_Slave_t *slots[IEC104_TCP_SERVER_CAPACITY];
|
||||
IEC104_TcpServer_t *tcp_slots[IEC104_TCP_SERVER_CAPACITY];
|
||||
IEC104_SlaveEvent_t event;
|
||||
IEC104_TcpError_t error;
|
||||
IEC104_SlaveConfig_t config;
|
||||
IEC104_TcpServerStatus_t status;
|
||||
size_t i;
|
||||
for (i = 0; i < IEC104_TCP_SERVER_CAPACITY; ++i) {
|
||||
slots[i] = IEC104_Slave_Create();
|
||||
CHECK(slots[i] != NULL);
|
||||
}
|
||||
CHECK(IEC104_Slave_Create() == NULL && IEC104_TcpServer_Create() == NULL);
|
||||
for (i = 0; i < IEC104_TCP_SERVER_CAPACITY; ++i) {
|
||||
CHECK(IEC104_Slave_Destroy(slots[i], NULL) == IEC104_TCP_OK);
|
||||
}
|
||||
for (i = 0; i < IEC104_TCP_SERVER_CAPACITY; ++i) {
|
||||
tcp_slots[i] = IEC104_TcpServer_Create();
|
||||
CHECK(tcp_slots[i] != NULL);
|
||||
}
|
||||
CHECK(IEC104_Slave_Create() == NULL);
|
||||
for (i = 0; i < IEC104_TCP_SERVER_CAPACITY; ++i) {
|
||||
CHECK(IEC104_TcpServer_Destroy(tcp_slots[i], NULL) == IEC104_TCP_OK);
|
||||
}
|
||||
service = IEC104_Slave_Create();
|
||||
CHECK(service != NULL);
|
||||
CHECK(IEC104_Slave_Get_Status(service, &status, NULL) == IEC104_TCP_OK && !status.listening);
|
||||
IEC104_Slave_Config_Default(&config);
|
||||
config.protocol.link.k = 0;
|
||||
CHECK(IEC104_Slave_Init(service, &config, &error) == IEC104_TCP_INVALID_CONFIG);
|
||||
CHECK(error.result == IEC104_TCP_INVALID_CONFIG && error.stage == IEC104_TCP_STAGE_VALIDATE);
|
||||
CHECK(IEC104_Slave_Destroy(service, NULL) == IEC104_TCP_OK);
|
||||
CHECK(IEC104_Slave_Destroy(service, &error) == IEC104_TCP_INVALID_HANDLE);
|
||||
CHECK(error.result == IEC104_TCP_INVALID_HANDLE);
|
||||
service_begin();
|
||||
CHECK(IEC104_Slave_Destroy(service, NULL) == IEC104_TCP_INVALID_STATE);
|
||||
CHECK(IEC104_Slave_Close(service, NULL) == IEC104_TCP_OK);
|
||||
CHECK(IEC104_Slave_Destroy(service, NULL) == IEC104_TCP_EVENTS_PENDING);
|
||||
CHECK(IEC104_Slave_NextEvent(service, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.transport.type == IEC104_TCP_EVENT_DISCONNECTED);
|
||||
CHECK(event.asdu_result == IEC60870_ASDU_UNSUPPORTED && event.connection.send_asdu == NULL);
|
||||
CHECK(IEC104_Slave_Destroy(service, NULL) == IEC104_TCP_OK);
|
||||
CHECK(IEC104_Slave_Get_Application_Layer_Parameters(NULL) == NULL);
|
||||
}
|
||||
|
||||
/* 仅通过公开门面接入外部事件循环,取得请求、对端地址和只读状态。 */
|
||||
static void test_external_scheduler(void)
|
||||
{
|
||||
IEC104_TcpWaitInfo_t wait;
|
||||
IEC104_TcpReady_t ready;
|
||||
IEC104_TcpPeerInfo_t peer;
|
||||
IEC104_TcpServerStatus_t status;
|
||||
IEC104_TcpError_t error;
|
||||
IEC104_SlaveEvent_t event;
|
||||
uint8_t input[16];
|
||||
uint32_t i;
|
||||
service_begin();
|
||||
CHECK(IEC104_Slave_Get_Remote_Address(service, &peer, NULL) == IEC104_TCP_OK);
|
||||
CHECK(strcmp(peer.address, "127.0.0.1") == 0 && peer.port == 12000);
|
||||
CHECK(IEC104_Slave_Prepare(service, &wait, NULL) == IEC104_TCP_OK);
|
||||
memset(&ready, 0, sizeof(ready));
|
||||
ready.service_token = wait.service_token;
|
||||
for (i = 0; i < wait.socket_count; ++i) {
|
||||
ready.sockets[i] = wait.sockets[i];
|
||||
ready.sockets[i].events = IEC104_TCP_IO_READ;
|
||||
}
|
||||
ready.socket_count = wait.socket_count;
|
||||
CHECK(IEC104_Build_Iframe(input, 0, 0, interrogation, sizeof(interrogation)) == 16);
|
||||
push(input, sizeof(input));
|
||||
CHECK(IEC104_Slave_Process(service, &ready, NULL) == IEC104_TCP_OK);
|
||||
CHECK(IEC104_Slave_NextEvent(service, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.asdu_result == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_MasterConnection_Send_ACT_CON(&event.connection, &event.asdu, false));
|
||||
CHECK(IEC104_Slave_Get_Status(service, &status, NULL) == IEC104_TCP_OK);
|
||||
CHECK(status.connected && status.connection.data_transfer_started);
|
||||
CHECK(IEC104_Slave_Process(service, NULL, &error) == IEC104_TCP_INVALID_ARGUMENT);
|
||||
CHECK(error.result == IEC104_TCP_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_Prepare(NULL, &wait, &error) == IEC104_TCP_INVALID_HANDLE);
|
||||
CHECK(error.stage == IEC104_TCP_STAGE_VALIDATE && error.socket_error == 0);
|
||||
CHECK(IEC104_Slave_Close_Connection(service, NULL, NULL) == IEC104_TCP_INVALID_ARGUMENT);
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* 主动上报只需Slave和ASDU;离线入NORMAL后在连接激活时正常发送。 */
|
||||
static void test_offline_enqueue(void)
|
||||
{
|
||||
IEC60870_Asdu_t object;
|
||||
IEC104_TcpServerStatus_t status;
|
||||
uint8_t bytes[sizeof(asdu)];
|
||||
service_open();
|
||||
memcpy(bytes, asdu, sizeof(bytes));
|
||||
CHECK(IEC60870_Asdu_Create_From_Buffer(&object,
|
||||
IEC104_Slave_Get_Application_Layer_Parameters(service), bytes, sizeof(bytes)) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC104_Slave_Enqueue_Event(service, &object) == IEC104_TCP_SUBMIT_OK);
|
||||
CHECK(fake.sent_bytes == 0);
|
||||
CHECK(IEC104_Slave_Get_Status(service, &status, NULL) == IEC104_TCP_OK);
|
||||
CHECK(status.normal_queue.pending_asdu_count == 1);
|
||||
service_connect();
|
||||
service_poll();
|
||||
CHECK(fake.sent_bytes == 22);
|
||||
CHECK(memcmp(fake.output + 12, asdu, sizeof(asdu)) == 0);
|
||||
CHECK(IEC104_Slave_Get_Status(service, &status, NULL) == IEC104_TCP_OK);
|
||||
CHECK(status.normal_queue.pending_asdu_count == 0 && status.normal_queue.stored_asdu_count == 1);
|
||||
service_end();
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_mutable_views();
|
||||
test_common_dispatch();
|
||||
test_event_replies();
|
||||
test_constructed_asdu();
|
||||
test_wait_and_retry();
|
||||
test_invalid_inputs();
|
||||
test_event_identity();
|
||||
test_request_state();
|
||||
test_stale_generation();
|
||||
test_fatal_result();
|
||||
test_deferred_event_copy();
|
||||
test_service_reuse();
|
||||
test_lifecycle();
|
||||
test_external_scheduler();
|
||||
test_offline_enqueue();
|
||||
puts("Slave service: 15 groups passed (automatic replies, deferred copies, lifecycle, request validation)");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,865 @@
|
||||
/* 变长通知FIFO回归:可控Net后端,无真实等待。
|
||||
* 为验证跨尾部和损坏数据的事务性出队,直接包含实现;不要另行链接TcpServer.c或Net后端。
|
||||
* TCP_EVENT_RING_BENCHMARK只运行公共接收路径,可用-I指定修改前实现进行同机对照。 */
|
||||
#include <IEC104_TcpServer.c>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#define CHECK(x) do { if (!(x)) { fprintf(stderr, "%s:%d: %s\n", __func__, __LINE__, #x); exit(1); } } while (0)
|
||||
|
||||
static struct {
|
||||
uint64_t now;
|
||||
uint8_t input[65536];
|
||||
uint8_t output[65536];
|
||||
size_t length, offset, recv_limit, sent_bytes, send_limit;
|
||||
unsigned accept_pending, recv_calls, send_calls, stop_confirmations;
|
||||
unsigned snapshot_calls, send_blocked, recv_eof, recv_retry, pending_error;
|
||||
} fake;
|
||||
static IEC104_TcpServer_t *sut;
|
||||
static const uint8_t start_act[] = {0x68, 4, 7, 0, 0, 0};
|
||||
#ifndef TCP_EVENT_RING_BENCHMARK
|
||||
static const uint8_t stop_act[] = {0x68, 4, 0x13, 0, 0, 0};
|
||||
static const uint8_t asdu[] = {1, 1, 3, 0, 1, 0, 1, 0, 0, 1};
|
||||
#endif
|
||||
|
||||
static IEC104_NetResult_t net_result(IEC104_NetResult_t result, int *error)
|
||||
{
|
||||
if (error != NULL) { *error = 0; }
|
||||
return result;
|
||||
}
|
||||
IEC104_NetResult_t IEC104_Net_Init(int *error) { return net_result(IEC104_NET_OK, error); }
|
||||
void IEC104_Net_Cleanup(void) {}
|
||||
int IEC104_Net_Invalid_Argument_Error(void) { return 22; }
|
||||
IEC104_NetResult_t IEC104_Net_Listen(const char *address, uint16_t port,
|
||||
IEC104_NetSocket_t *listener, uint16_t *bound_port, int *error)
|
||||
{
|
||||
(void)address;
|
||||
*listener = 1;
|
||||
*bound_port = port;
|
||||
return net_result(IEC104_NET_OK, error);
|
||||
}
|
||||
IEC104_NetResult_t IEC104_Net_Accept(IEC104_NetSocket_t listener,
|
||||
IEC104_NetSocket_t *client, IEC104_NetPeer_t *peer, int *error)
|
||||
{
|
||||
CHECK(listener == 1);
|
||||
*client = IEC104_NET_INVALID_SOCKET;
|
||||
if (!fake.accept_pending) { return net_result(IEC104_NET_WOULD_BLOCK, error); }
|
||||
fake.accept_pending = 0;
|
||||
*client = 2;
|
||||
memset(peer, 0, sizeof(*peer));
|
||||
memcpy(peer->address, "127.0.0.1", sizeof("127.0.0.1"));
|
||||
peer->port = 12000;
|
||||
return net_result(IEC104_NET_OK, error);
|
||||
}
|
||||
IEC104_NetIoResult_t IEC104_Net_Recv(IEC104_NetSocket_t socket, void *data, size_t capacity)
|
||||
{
|
||||
IEC104_NetIoResult_t result = {IEC104_NET_WOULD_BLOCK, 0, 0};
|
||||
size_t count = fake.length - fake.offset;
|
||||
CHECK(socket == 2);
|
||||
++fake.recv_calls;
|
||||
if (fake.recv_retry) { --fake.recv_retry; result.result = IEC104_NET_RETRY; return result; }
|
||||
if (count == 0) {
|
||||
if (fake.recv_eof) { result.result = IEC104_NET_EOF; }
|
||||
return result;
|
||||
}
|
||||
if (count > capacity) { count = capacity; }
|
||||
if (fake.recv_limit && count > fake.recv_limit) { count = fake.recv_limit; }
|
||||
memcpy(data, fake.input + fake.offset, count);
|
||||
fake.offset += count;
|
||||
result.result = IEC104_NET_OK;
|
||||
result.transferred = count;
|
||||
return result;
|
||||
}
|
||||
IEC104_NetResult_t IEC104_Net_Pending_Input(
|
||||
IEC104_NetSocket_t socket, uint32_t *available, int *error)
|
||||
{
|
||||
CHECK(socket == 2 && available != NULL);
|
||||
++fake.snapshot_calls;
|
||||
if (fake.pending_error) { return net_result(IEC104_NET_ERROR, error); }
|
||||
*available = (uint32_t)(fake.length - fake.offset);
|
||||
return net_result(IEC104_NET_OK, error);
|
||||
}
|
||||
IEC104_NetIoResult_t IEC104_Net_Send(IEC104_NetSocket_t socket, const void *data, size_t length)
|
||||
{
|
||||
IEC104_NetIoResult_t result = {IEC104_NET_OK, length, 0};
|
||||
const uint8_t *bytes = (const uint8_t *)data;
|
||||
CHECK(socket == 2 && data != NULL && length > 0);
|
||||
++fake.send_calls;
|
||||
if (fake.send_blocked) { result.result = IEC104_NET_WOULD_BLOCK; result.transferred = 0; return result; }
|
||||
if (fake.send_limit && length > fake.send_limit) { length = fake.send_limit; result.transferred = length; }
|
||||
#ifndef TCP_EVENT_RING_BENCHMARK
|
||||
CHECK(fake.sent_bytes + length <= sizeof(fake.output));
|
||||
memcpy(fake.output + fake.sent_bytes, data, length);
|
||||
#endif
|
||||
fake.sent_bytes += length;
|
||||
if (length == IEC104_APCI_LEN && bytes[2] == 0x23) { ++fake.stop_confirmations; }
|
||||
return result;
|
||||
}
|
||||
IEC104_NetResult_t IEC104_Net_Wait(IEC104_NetSocket_t listener, IEC104_NetSocket_t client,
|
||||
uint32_t interest, uint32_t timeout, uint32_t *ready, int *error)
|
||||
{
|
||||
(void)listener; (void)client; (void)timeout;
|
||||
*ready = 0;
|
||||
if (fake.accept_pending && (interest & IEC104_NET_EVENT_ACCEPT)) { *ready |= IEC104_NET_EVENT_ACCEPT; }
|
||||
if (fake.offset < fake.length && (interest & IEC104_NET_EVENT_READ)) { *ready |= IEC104_NET_EVENT_READ; }
|
||||
if (interest & IEC104_NET_EVENT_WRITE) { *ready |= IEC104_NET_EVENT_WRITE; }
|
||||
return net_result(IEC104_NET_OK, error);
|
||||
}
|
||||
IEC104_NetResult_t IEC104_Net_Close(IEC104_NetSocket_t socket, int *error)
|
||||
{
|
||||
(void)socket;
|
||||
return net_result(IEC104_NET_OK, error);
|
||||
}
|
||||
uint64_t IEC104_Net_Now_Ms(void) { return fake.now; }
|
||||
|
||||
static void push(const uint8_t *bytes, size_t length)
|
||||
{
|
||||
if (fake.offset == fake.length) { fake.offset = fake.length = 0; }
|
||||
CHECK(fake.length + length <= sizeof(fake.input));
|
||||
memcpy(fake.input + fake.length, bytes, length);
|
||||
fake.length += length;
|
||||
}
|
||||
static void push_i(unsigned ns, unsigned nr, uint16_t length)
|
||||
{
|
||||
uint8_t frame[IEC104_MAX_APDU_LEN];
|
||||
CHECK(length >= IEC104_APCI_LEN && length <= sizeof(frame));
|
||||
memset(frame, 0x5a, sizeof(frame));
|
||||
ns &= IEC104_SEQUENCE_MASK;
|
||||
nr &= IEC104_SEQUENCE_MASK;
|
||||
frame[0] = 0x68; frame[1] = (uint8_t)(length - 2U);
|
||||
frame[2] = (uint8_t)(ns << 1); frame[3] = (uint8_t)(ns >> 7);
|
||||
frame[4] = (uint8_t)(nr << 1); frame[5] = (uint8_t)(nr >> 7);
|
||||
push(frame, length);
|
||||
}
|
||||
static void poll_once(void) { CHECK(IEC104_TcpServer_Poll(sut, 0, NULL) == IEC104_TCP_OK); }
|
||||
static unsigned drain(void)
|
||||
{
|
||||
unsigned count = 0;
|
||||
IEC104_TcpEvent_t event;
|
||||
IEC104_TcpEventResult_t result;
|
||||
while ((result = IEC104_TcpServer_NextEvent(sut, &event)) == IEC104_TCP_EVENT_READY) { ++count; }
|
||||
CHECK(result == IEC104_TCP_EVENT_EMPTY);
|
||||
return count;
|
||||
}
|
||||
static void begin(void)
|
||||
{
|
||||
IEC104_TcpServerConfig_t config;
|
||||
memset(&fake, 0, sizeof(fake));
|
||||
fake.now = 1000;
|
||||
fake.accept_pending = 1;
|
||||
sut = IEC104_TcpServer_Create();
|
||||
CHECK(sut != NULL);
|
||||
IEC104_TcpServer_Config_Default(&config);
|
||||
config.protocol.link.k = IEC104_TCP_RECORD_CAPACITY;
|
||||
CHECK(IEC104_TcpServer_Init(sut, &config, NULL) == IEC104_TCP_OK);
|
||||
poll_once();
|
||||
CHECK(drain() == 1);
|
||||
push(start_act, sizeof(start_act));
|
||||
poll_once();
|
||||
CHECK(drain() == 1);
|
||||
CHECK(sut->connection.link_state.dt_state == IEC104_DT_STARTED);
|
||||
}
|
||||
static void end(void)
|
||||
{
|
||||
CHECK(IEC104_TcpServer_Close(sut, NULL) == IEC104_TCP_OK);
|
||||
drain();
|
||||
CHECK(IEC104_TcpServer_Destroy(sut, NULL) == IEC104_TCP_OK);
|
||||
}
|
||||
|
||||
#ifndef TCP_EVENT_RING_BENCHMARK
|
||||
static void push_ack(unsigned nr)
|
||||
{
|
||||
uint8_t frame[] = {0x68, 4, 1, 0, 0, 0};
|
||||
frame[4] = (uint8_t)(nr << 1); frame[5] = (uint8_t)(nr >> 7);
|
||||
push(frame, sizeof(frame));
|
||||
}
|
||||
static void send_one(void)
|
||||
{
|
||||
CHECK(IEC104_TcpServer_Send_Response(sut, sut->generation, asdu, sizeof(asdu)) == IEC104_TCP_SUBMIT_OK);
|
||||
}
|
||||
static void fill_input(unsigned count, uint16_t length)
|
||||
{
|
||||
unsigned index;
|
||||
for (index = 0; index < count; ++index) { push_i(index, 0, length); }
|
||||
for (index = 0; index < count + 16U && !sut->rx_event_pending &&
|
||||
(fake.offset < fake.length || sut->rx_begin != sut->rx_end); ++index) { poll_once(); }
|
||||
CHECK(sut->client_socket != IEC104_NET_INVALID_SOCKET);
|
||||
}
|
||||
static void raw_begin(void)
|
||||
{
|
||||
sut = IEC104_TcpServer_Create();
|
||||
CHECK(sut != NULL);
|
||||
if (sut->generation == 0U) { sut->generation = 1; }
|
||||
}
|
||||
static void raw_end(void)
|
||||
{
|
||||
drain();
|
||||
CHECK(IEC104_TcpServer_Destroy(sut, NULL) == IEC104_TCP_OK);
|
||||
}
|
||||
static IEC104_TcpIFrameEvent_t frame_payload(uint16_t length)
|
||||
{
|
||||
IEC104_TcpIFrameEvent_t frame;
|
||||
uint16_t index;
|
||||
memset(&frame, 0, sizeof(frame));
|
||||
frame.receive_serial = 123;
|
||||
frame.apdu_length = length;
|
||||
frame.send_number = 17;
|
||||
frame.receive_number = 3;
|
||||
for (index = 0; index < length; ++index) { frame.apdu[index] = (uint8_t)(index * 7U); }
|
||||
return frame;
|
||||
}
|
||||
static void append_frame(uint16_t length)
|
||||
{
|
||||
IEC104_TcpIFrameEvent_t frame = frame_payload(length);
|
||||
CHECK(Append_Event(sut, IEC104_TCP_EVENT_I_FRAME, &frame, (uint16_t)(TCP_EVENT_I_PREFIX + length)));
|
||||
}
|
||||
static void check_event(const IEC104_TcpEvent_t *actual, const IEC104_TcpEvent_t *expected)
|
||||
{
|
||||
CHECK(actual->type == expected->type && actual->generation == expected->generation);
|
||||
CHECK(memcmp(&actual->data, &expected->data, sizeof(actual->data)) == 0);
|
||||
}
|
||||
|
||||
/* 每个可能起点都覆盖短/长I帧、RECORD及所有连接通知,验证头/载荷跨尾、零填充和旧代次。 */
|
||||
static void test_wrap_roundtrip(void)
|
||||
{
|
||||
uint32_t index;
|
||||
unsigned kind;
|
||||
raw_begin();
|
||||
for (index = 0; index < sizeof(sut->event_bytes); ++index) {
|
||||
for (kind = 0; kind < 9; ++kind) {
|
||||
IEC104_TcpEvent_t expected, actual;
|
||||
uint16_t length;
|
||||
memset(&expected, 0, sizeof(expected));
|
||||
expected.generation = sut->generation;
|
||||
sut->event_head = index;
|
||||
if (kind < 4) {
|
||||
static const uint16_t lengths[] = {IEC104_APCI_LEN, 16, 22, IEC104_MAX_APDU_LEN};
|
||||
expected.type = IEC104_TCP_EVENT_I_FRAME;
|
||||
expected.data.i_frame = frame_payload(lengths[kind]);
|
||||
length = (uint16_t)(TCP_EVENT_I_PREFIX + lengths[kind]);
|
||||
} else if (kind == 4) {
|
||||
expected.type = IEC104_TCP_EVENT_RECORD;
|
||||
expected.data.record.value.frame_serial = 91;
|
||||
expected.data.record.value.apdu_length = IEC104_MAX_APDU_LEN;
|
||||
expected.data.record.value.local_send_completed = 1;
|
||||
expected.data.record.closing = 1;
|
||||
length = sizeof(expected.data.record);
|
||||
} else {
|
||||
static const IEC104_TcpEventType_t types[] = {IEC104_TCP_EVENT_CONNECTED,
|
||||
IEC104_TCP_EVENT_ACTIVATED, IEC104_TCP_EVENT_DEACTIVATED, IEC104_TCP_EVENT_DISCONNECTED};
|
||||
expected.type = types[kind - 5];
|
||||
expected.data.connection.peer.generation = expected.generation;
|
||||
expected.data.connection.peer.port = 2404;
|
||||
expected.data.connection.status_valid = 1;
|
||||
length = sizeof(expected.data.connection);
|
||||
}
|
||||
CHECK(Append_Event(sut, expected.type, &expected.data, length));
|
||||
CHECK(sut->event_used == sizeof(TcpEventHeader_t) + length);
|
||||
++sut->generation;
|
||||
memset(&actual, 0xa5, sizeof(actual));
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &actual) == IEC104_TCP_EVENT_READY);
|
||||
check_event(&actual, &expected);
|
||||
CHECK(sut->event_used == 0 && sut->event_count == 0 && sut->state_event_count == 0);
|
||||
CHECK(sut->event_head == (index + sizeof(TcpEventHeader_t) + length) % sizeof(sut->event_bytes));
|
||||
}
|
||||
}
|
||||
raw_end();
|
||||
}
|
||||
|
||||
static void test_full_reserves(void)
|
||||
{
|
||||
unsigned index;
|
||||
IEC104_TcpConnectionEvent_t connection = {0};
|
||||
IEC104_TcpRecordEvent_t record = {0};
|
||||
IEC104_TcpEvent_t event;
|
||||
IEC104_TcpServer_t saved;
|
||||
raw_begin();
|
||||
sut->event_head = (uint32_t)sizeof(sut->event_bytes) - 1U;
|
||||
for (index = 0; index < IEC104_TCP_EVENT_NORMAL_CAPACITY; ++index) {
|
||||
CHECK(Ordinary_Event_Room(sut, TCP_EVENT_I_MAX_BYTES));
|
||||
append_frame(IEC104_MAX_APDU_LEN);
|
||||
}
|
||||
CHECK(!Ordinary_Event_Room(sut, TCP_EVENT_I_MIN_BYTES));
|
||||
for (index = 0; index < IEC104_TCP_EVENT_STATE_RESERVE; ++index) {
|
||||
CHECK(State_Event_Room(sut) && Transfer_Event_Room(sut));
|
||||
CHECK(Append_Event(sut, IEC104_TCP_EVENT_ACTIVATED, &connection, sizeof(connection)));
|
||||
}
|
||||
CHECK(!State_Event_Room(sut) && !Transfer_Event_Room(sut));
|
||||
CHECK(!Append_Event(sut, IEC104_TCP_EVENT_DEACTIVATED, &connection, sizeof(connection)));
|
||||
CHECK(sut->event_used == TCP_EVENT_CAPACITY_BYTES - TCP_EVENT_CLOSE_BYTES);
|
||||
append_frame(IEC104_MAX_APDU_LEN);
|
||||
record.closing = 1;
|
||||
for (index = 0; index < IEC104_TCP_RECORD_CAPACITY; ++index) {
|
||||
record.value.frame_serial = index + 1U;
|
||||
CHECK(Append_Event(sut, IEC104_TCP_EVENT_RECORD, &record, sizeof(record)));
|
||||
}
|
||||
CHECK(Append_Event(sut, IEC104_TCP_EVENT_DISCONNECTED, &connection, sizeof(connection)));
|
||||
CHECK(sut->event_used == sizeof(sut->event_bytes) && sut->event_count == IEC104_TCP_EVENT_CAPACITY);
|
||||
saved = *sut;
|
||||
CHECK(!Append_Event(sut, IEC104_TCP_EVENT_RECORD, &record, sizeof(record)));
|
||||
CHECK(memcmp(&saved, sut, sizeof(saved)) == 0);
|
||||
CHECK(!Can_Accept_Connection(sut));
|
||||
for (index = 0; index < IEC104_TCP_EVENT_NORMAL_CAPACITY; ++index) {
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.type == IEC104_TCP_EVENT_I_FRAME);
|
||||
}
|
||||
CHECK(!Can_Accept_Connection(sut)); /* 空间已足够,旧启停通知仍阻挡新连接。 */
|
||||
for (index = 0; index < IEC104_TCP_EVENT_STATE_RESERVE; ++index) {
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.type == IEC104_TCP_EVENT_ACTIVATED);
|
||||
}
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.type == IEC104_TCP_EVENT_I_FRAME);
|
||||
for (index = 0; index < IEC104_TCP_RECORD_CAPACITY; ++index) {
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.type == IEC104_TCP_EVENT_RECORD && event.data.record.closing);
|
||||
CHECK(event.data.record.value.frame_serial == index + 1U);
|
||||
}
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.type == IEC104_TCP_EVENT_DISCONNECTED);
|
||||
CHECK(Can_Accept_Connection(sut));
|
||||
raw_end();
|
||||
}
|
||||
|
||||
static void test_room_boundaries(void)
|
||||
{
|
||||
unsigned index;
|
||||
IEC104_TcpConnectionEvent_t connection = {0};
|
||||
IEC104_TcpServer_t saved;
|
||||
raw_begin();
|
||||
for (index = 0; index + 1U < IEC104_TCP_EVENT_NORMAL_CAPACITY; ++index) {
|
||||
append_frame(IEC104_MAX_APDU_LEN);
|
||||
}
|
||||
append_frame((uint16_t)(IEC104_MAX_APDU_LEN - TCP_EVENT_CONNECTION_BYTES));
|
||||
CHECK(Ordinary_Event_Room(sut, TCP_EVENT_CONNECTION_BYTES));
|
||||
CHECK(Can_Accept_Connection(sut));
|
||||
CHECK(!Ordinary_Event_Room(sut, TCP_EVENT_I_MAX_BYTES));
|
||||
append_frame(IEC104_APCI_LEN);
|
||||
CHECK(!Can_Accept_Connection(sut));
|
||||
raw_end();
|
||||
|
||||
raw_begin();
|
||||
for (index = 0; index < IEC104_TCP_EVENT_NORMAL_CAPACITY; ++index) {
|
||||
append_frame(IEC104_MAX_APDU_LEN);
|
||||
}
|
||||
/* 模拟旧会话收尾项占用普通区以外的空间;即使启停条数为0,也不能挤占新的收尾预留。 */
|
||||
while (sut->event_used + TCP_EVENT_CONNECTION_BYTES <= TCP_EVENT_CAPACITY_BYTES - TCP_EVENT_CLOSE_BYTES) {
|
||||
CHECK(Append_Event(sut, IEC104_TCP_EVENT_DISCONNECTED, &connection, sizeof(connection)));
|
||||
}
|
||||
CHECK(sut->state_event_count == 0 && !State_Event_Room(sut) && !Transfer_Event_Room(sut));
|
||||
saved = *sut;
|
||||
CHECK(!Append_Event(sut, IEC104_TCP_EVENT_ACTIVATED, &connection, sizeof(connection)));
|
||||
CHECK(!Notify_Transfer_State(sut, IEC104_DT_STARTED));
|
||||
CHECK(memcmp(sut, &saved, sizeof(saved)) == 0);
|
||||
raw_end();
|
||||
}
|
||||
|
||||
static void expect_error_unchanged(void)
|
||||
{
|
||||
IEC104_TcpServer_t saved = *sut;
|
||||
IEC104_TcpEvent_t output, original;
|
||||
memset(&output, 0xa5, sizeof(output));
|
||||
original = output;
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &output) == IEC104_TCP_EVENT_ERROR);
|
||||
CHECK(memcmp(&output, &original, sizeof(output)) == 0);
|
||||
CHECK(memcmp(sut, &saved, sizeof(saved)) == 0);
|
||||
}
|
||||
static void test_invalid_entries(void)
|
||||
{
|
||||
IEC104_TcpServer_t saved;
|
||||
TcpEventHeader_t header;
|
||||
unsigned index;
|
||||
raw_begin();
|
||||
sut->event_head = (uint32_t)sizeof(sut->event_bytes) - 3U;
|
||||
append_frame(16);
|
||||
saved = *sut;
|
||||
for (index = 0; index < 14; ++index) {
|
||||
*sut = saved;
|
||||
Event_Ring_Read(sut, sut->event_head, &header, sizeof(header));
|
||||
switch (index) {
|
||||
case 0: header.type = (IEC104_TcpEventType_t)99; break;
|
||||
case 1: header.generation = 0; break;
|
||||
case 2: header.payload_length = 0; break;
|
||||
case 3: header.payload_length = UINT16_MAX; break;
|
||||
case 4: header.type = IEC104_TCP_EVENT_RECORD; break;
|
||||
case 5: header.type = IEC104_TCP_EVENT_CONNECTED; break;
|
||||
case 6: --header.payload_length; break;
|
||||
case 7: sut->event_head = sizeof(sut->event_bytes); break;
|
||||
case 8: sut->event_used = sizeof(sut->event_bytes) + 1U; break;
|
||||
case 9: sut->event_count = 0; break;
|
||||
case 10: sut->event_count = 2; break;
|
||||
case 11: sut->state_event_count = 1; break;
|
||||
case 12: --sut->event_used; break;
|
||||
case 13: ++sut->event_used; break;
|
||||
}
|
||||
if (index < 7) { Event_Ring_Write(sut, sut->event_head, &header, sizeof(header)); }
|
||||
expect_error_unchanged();
|
||||
}
|
||||
for (index = 0; index < 4; ++index) {
|
||||
static const uint16_t bad_lengths[] = {0, IEC104_APCI_LEN - 1, 17, IEC104_MAX_APDU_LEN + 1};
|
||||
*sut = saved;
|
||||
Event_Ring_Write(sut, Event_Ring_Advance(sut->event_head,
|
||||
sizeof(header) + offsetof(IEC104_TcpIFrameEvent_t, apdu_length)), &bad_lengths[index], sizeof(uint16_t));
|
||||
expect_error_unchanged();
|
||||
}
|
||||
*sut = saved;
|
||||
raw_end();
|
||||
raw_begin();
|
||||
{
|
||||
IEC104_TcpConnectionEvent_t connection = {0};
|
||||
CHECK(Append_Event(sut, IEC104_TCP_EVENT_ACTIVATED, &connection, sizeof(connection)));
|
||||
saved = *sut;
|
||||
sut->state_event_count = 0;
|
||||
expect_error_unchanged();
|
||||
*sut = saved;
|
||||
Event_Ring_Read(sut, sut->event_head, &header, sizeof(header));
|
||||
header.type = IEC104_TCP_EVENT_CONNECTED;
|
||||
Event_Ring_Write(sut, sut->event_head, &header, sizeof(header));
|
||||
expect_error_unchanged();
|
||||
*sut = saved;
|
||||
}
|
||||
raw_end();
|
||||
}
|
||||
|
||||
static void test_short_capacity(void)
|
||||
{
|
||||
unsigned count = (unsigned)(TCP_EVENT_NORMAL_BYTES / (sizeof(TcpEventHeader_t) + TCP_EVENT_I_PREFIX + 16));
|
||||
unsigned index;
|
||||
IEC104_TcpEvent_t event;
|
||||
IEC104_TcpWaitInfo_t wait;
|
||||
IEC104_TcpServerStatus_t status;
|
||||
begin();
|
||||
fill_input(count + 1U, 16);
|
||||
CHECK(sut->event_count == count && sut->rx_event_pending);
|
||||
CHECK(IEC104_TcpServer_Prepare(sut, &wait, NULL) == IEC104_TCP_OK);
|
||||
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
|
||||
CHECK(wait.receive_paused && status.receive_paused && wait.pending_event_count == count);
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.type == IEC104_TCP_EVENT_I_FRAME && event.data.i_frame.send_number == 0);
|
||||
poll_once();
|
||||
CHECK(!sut->rx_event_pending && sut->event_count == count);
|
||||
for (index = 1; index <= count; ++index) {
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.type == IEC104_TCP_EVENT_I_FRAME && event.data.i_frame.send_number == index);
|
||||
}
|
||||
printf("Short APDU capacity: %u queued + one retained in Receiver\n", count);
|
||||
end();
|
||||
}
|
||||
|
||||
static void test_pending_frame_before_record(void)
|
||||
{
|
||||
unsigned index;
|
||||
IEC104_TcpEvent_t event;
|
||||
begin();
|
||||
send_one();
|
||||
fill_input(IEC104_TCP_EVENT_NORMAL_CAPACITY - 1U, IEC104_MAX_APDU_LEN);
|
||||
push_i(IEC104_TCP_EVENT_NORMAL_CAPACITY - 1U, 0, 16);
|
||||
poll_once();
|
||||
CHECK(Ordinary_Event_Room(sut, TCP_EVENT_RECORD_BYTES));
|
||||
CHECK(!Ordinary_Event_Room(sut, TCP_EVENT_I_MAX_BYTES));
|
||||
push_i(IEC104_TCP_EVENT_NORMAL_CAPACITY, 1, IEC104_MAX_APDU_LEN);
|
||||
poll_once();
|
||||
CHECK(sut->rx_event_pending && sut->connection.send_manager.record_count == 1);
|
||||
CHECK(sut->event_count == IEC104_TCP_EVENT_NORMAL_CAPACITY);
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
poll_once();
|
||||
poll_once();
|
||||
CHECK(!sut->rx_event_pending && sut->connection.send_manager.record_count == 0);
|
||||
for (index = 1; index <= IEC104_TCP_EVENT_NORMAL_CAPACITY; ++index) {
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.type == IEC104_TCP_EVENT_I_FRAME && event.data.i_frame.send_number == index);
|
||||
}
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.type == IEC104_TCP_EVENT_RECORD && !event.data.record.closing);
|
||||
CHECK(event.data.record.value.peer_acknowledged);
|
||||
end();
|
||||
}
|
||||
|
||||
static void test_delayed_receipt(int budget_limited)
|
||||
{
|
||||
unsigned count = budget_limited ? TCP_EVENT_BUDGET :
|
||||
(unsigned)(TCP_EVENT_NORMAL_BYTES / (sizeof(TcpEventHeader_t) + TCP_EVENT_I_PREFIX + 16));
|
||||
unsigned index, seen_state = 0, seen_record = 0;
|
||||
IEC104_TcpEvent_t event;
|
||||
begin();
|
||||
send_one();
|
||||
if (budget_limited) {
|
||||
for (index = 0; index < count; ++index) { push_i(index, index + 1U == count ? 1U : 0U, 16); }
|
||||
push(stop_act, sizeof(stop_act));
|
||||
poll_once();
|
||||
CHECK(sut->event_count == count && sut->connection.send_manager.record_count == 1);
|
||||
} else {
|
||||
fill_input(count, 16);
|
||||
CHECK(!Ordinary_Event_Room(sut, TCP_EVENT_RECORD_BYTES));
|
||||
push_ack(1);
|
||||
push(stop_act, sizeof(stop_act));
|
||||
}
|
||||
poll_once();
|
||||
CHECK(sut->state_event_count == 1 && fake.stop_confirmations == 1);
|
||||
if (!budget_limited) {
|
||||
CHECK(sut->connection.send_manager.record_count == 1);
|
||||
do {
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.type == IEC104_TCP_EVENT_I_FRAME);
|
||||
} while (!Ordinary_Event_Room(sut, TCP_EVENT_RECORD_BYTES));
|
||||
poll_once();
|
||||
}
|
||||
CHECK(sut->connection.send_manager.record_count == 0);
|
||||
while (IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY) {
|
||||
if (event.type == IEC104_TCP_EVENT_DEACTIVATED) { CHECK(!seen_record); ++seen_state; }
|
||||
if (event.type == IEC104_TCP_EVENT_RECORD) { CHECK(seen_state == 1); ++seen_record; }
|
||||
}
|
||||
CHECK(seen_state == 1 && seen_record == 1);
|
||||
end();
|
||||
}
|
||||
|
||||
static void test_close_with_pending_frame_and_all_records(void)
|
||||
{
|
||||
unsigned index;
|
||||
uint64_t generation;
|
||||
IEC104_TcpEvent_t event;
|
||||
begin();
|
||||
generation = sut->generation;
|
||||
for (index = 0; index < IEC104_TCP_RECORD_CAPACITY; ++index) { send_one(); }
|
||||
CHECK(sut->connection.send_manager.record_count == IEC104_TCP_RECORD_CAPACITY);
|
||||
fill_input(IEC104_TCP_EVENT_NORMAL_CAPACITY + 1U, IEC104_MAX_APDU_LEN);
|
||||
CHECK(sut->rx_event_pending && sut->event_count == IEC104_TCP_EVENT_NORMAL_CAPACITY);
|
||||
CHECK(IEC104_TcpServer_Close_Connection(sut, generation, NULL) == IEC104_TCP_OK);
|
||||
CHECK(sut->event_count == IEC104_TCP_EVENT_NORMAL_CAPACITY + IEC104_TCP_EVENT_CLOSE_RESERVE);
|
||||
for (index = 0; index <= IEC104_TCP_EVENT_NORMAL_CAPACITY; ++index) {
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.type == IEC104_TCP_EVENT_I_FRAME && event.generation == generation);
|
||||
CHECK(event.data.i_frame.send_number == index && event.data.i_frame.apdu_length == IEC104_MAX_APDU_LEN);
|
||||
}
|
||||
for (index = 0; index < IEC104_TCP_RECORD_CAPACITY; ++index) {
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.type == IEC104_TCP_EVENT_RECORD && event.data.record.closing);
|
||||
CHECK(event.data.record.value.local_send_completed && !event.data.record.value.peer_acknowledged);
|
||||
}
|
||||
fake.accept_pending = 1;
|
||||
poll_once();
|
||||
CHECK(sut->generation > generation);
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.type == IEC104_TCP_EVENT_DISCONNECTED && event.generation == generation);
|
||||
CHECK(event.data.connection.peer.generation == generation);
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.type == IEC104_TCP_EVENT_CONNECTED && event.generation == sut->generation);
|
||||
end();
|
||||
}
|
||||
|
||||
/* 核对真正写出的完整U帧;调用次数包含WOULD_BLOCK/部分写,不能代替CON数量。
|
||||
* 调用方另行断言总字节数,既检查帧内容,也防止多回或少回CON。 */
|
||||
static void check_state_confirmation(size_t offset, int started)
|
||||
{
|
||||
const uint8_t expected[] = {0x68, 4, started ? 0x0b : 0x23, 0, 0, 0};
|
||||
CHECK(offset <= fake.sent_bytes && sizeof(expected) <= fake.sent_bytes - offset);
|
||||
CHECK(memcmp(fake.output + offset, expected, sizeof(expected)) == 0);
|
||||
}
|
||||
|
||||
/* 以下场景没有业务I帧或故障;逐字段检查事件产生时的快照,不能用当前连接状态替代。
|
||||
* 主动关闭的DISCONNECTED也保存Close前的ACTIVE阶段,并保留原连接的地址和代次。 */
|
||||
static void check_connection_event(const IEC104_TcpEvent_t *event,
|
||||
IEC104_TcpEventType_t type, uint64_t generation, int started)
|
||||
{
|
||||
CHECK(event->type == type && event->generation == generation);
|
||||
CHECK(event->data.connection.peer.generation == generation);
|
||||
CHECK(strcmp(event->data.connection.peer.address, "127.0.0.1") == 0);
|
||||
CHECK(event->data.connection.peer.port == 12000);
|
||||
CHECK(event->data.connection.status_valid == 1 && event->data.connection.socket_error == 0);
|
||||
CHECK(event->data.connection.status.phase == IEC104_TCP_SESSION_ACTIVE);
|
||||
CHECK(event->data.connection.status.fault == IEC104_TCP_FAULT_NONE);
|
||||
CHECK(event->data.connection.status.pending_send_records == 0);
|
||||
CHECK(event->data.connection.status.data_transfer_started == started);
|
||||
}
|
||||
|
||||
/* 配额只暂缓下一次状态变化;已受理ACT的CON仍发送,通知不能合并或覆盖。 */
|
||||
static void test_state_reserve_backpressure(void)
|
||||
{
|
||||
unsigned index;
|
||||
const int started_before_wait = IEC104_TCP_EVENT_STATE_RESERVE % 2U == 0U;
|
||||
size_t confirmed_bytes;
|
||||
uint64_t generation;
|
||||
IEC104_TcpEvent_t event;
|
||||
begin();
|
||||
generation = sut->generation;
|
||||
for (index = 0; index < IEC104_TCP_EVENT_STATE_RESERVE; ++index) {
|
||||
push(index % 2U == 0U ? stop_act : start_act, sizeof(start_act));
|
||||
poll_once();
|
||||
}
|
||||
CHECK(sut->state_event_count == IEC104_TCP_EVENT_STATE_RESERVE);
|
||||
/* 配额耗尽那一轮可能先让出;再推进一次,检查包括begin中START在内的全部CON。
|
||||
* 时钟固定且没有I帧,本用例的输出只能由这些6字节CON组成。 */
|
||||
poll_once();
|
||||
confirmed_bytes = (size_t)(IEC104_TCP_EVENT_STATE_RESERVE + 1U) * IEC104_APCI_LEN;
|
||||
CHECK(fake.sent_bytes == confirmed_bytes);
|
||||
check_state_confirmation(0, 1);
|
||||
for (index = 0; index < IEC104_TCP_EVENT_STATE_RESERVE; ++index) {
|
||||
check_state_confirmation((size_t)(index + 1U) * IEC104_APCI_LEN, index % 2U != 0U);
|
||||
}
|
||||
|
||||
/* 下一个ACT要求反向切换。多推进一轮仍不得提前改变状态、占槽或回CON。 */
|
||||
push(started_before_wait ? stop_act : start_act, sizeof(start_act));
|
||||
poll_once();
|
||||
poll_once();
|
||||
CHECK(sut->rx_blocked && !sut->rx_event_pending);
|
||||
CHECK(sut->state_event_count == IEC104_TCP_EVENT_STATE_RESERVE);
|
||||
CHECK(sut->event_count == IEC104_TCP_EVENT_STATE_RESERVE);
|
||||
CHECK((sut->connection.link_state.dt_state == IEC104_DT_STARTED) == started_before_wait);
|
||||
CHECK(fake.sent_bytes == confirmed_bytes);
|
||||
|
||||
/* NextEvent只释放一个槽;下一轮Poll才重试原ACT并补入一条通知。 */
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
check_connection_event(&event, IEC104_TCP_EVENT_DEACTIVATED, generation, 0);
|
||||
CHECK(sut->state_event_count == IEC104_TCP_EVENT_STATE_RESERVE - 1U);
|
||||
CHECK(fake.sent_bytes == confirmed_bytes);
|
||||
poll_once();
|
||||
CHECK(!sut->rx_blocked && !sut->rx_event_pending);
|
||||
CHECK(sut->state_event_count == IEC104_TCP_EVENT_STATE_RESERVE);
|
||||
CHECK((sut->connection.link_state.dt_state == IEC104_DT_STARTED) == !started_before_wait);
|
||||
poll_once();
|
||||
CHECK(fake.sent_bytes == confirmed_bytes + IEC104_APCI_LEN);
|
||||
check_state_confirmation(confirmed_bytes, !started_before_wait);
|
||||
|
||||
/* 旧通知仍逐条在前,刚受理的变化追加到末尾;每条快照保持当时的启停状态。 */
|
||||
for (index = 1; index <= IEC104_TCP_EVENT_STATE_RESERVE; ++index) {
|
||||
int started = index % 2U != 0U;
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
check_connection_event(&event, started ? IEC104_TCP_EVENT_ACTIVATED : IEC104_TCP_EVENT_DEACTIVATED,
|
||||
generation, started);
|
||||
}
|
||||
CHECK(sut->state_event_count == 0 && sut->event_count == 0);
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_EMPTY);
|
||||
end();
|
||||
}
|
||||
|
||||
/* 同状态ACT只确认、不通知;分别从STOPPED和STARTED开始,使空队列和满配额下
|
||||
* 两个方向都被覆盖,不依赖STATE_RESERVE的奇偶。STOP先完成上一轮CON,确保处于IDLE。 */
|
||||
static void test_repeated_state_act(void)
|
||||
{
|
||||
unsigned initial_started, index;
|
||||
IEC104_TcpEvent_t event;
|
||||
for (initial_started = 0; initial_started <= 1U; ++initial_started) {
|
||||
int started = (int)initial_started;
|
||||
size_t confirmed_bytes;
|
||||
uint64_t generation;
|
||||
begin();
|
||||
generation = sut->generation;
|
||||
if (!started) {
|
||||
push(stop_act, sizeof(stop_act));
|
||||
poll_once();
|
||||
poll_once();
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
check_connection_event(&event, IEC104_TCP_EVENT_DEACTIVATED, generation, 0);
|
||||
}
|
||||
CHECK(sut->state_event_count == 0 && sut->event_count == 0);
|
||||
confirmed_bytes = (started ? 1U : 2U) * IEC104_APCI_LEN;
|
||||
CHECK(fake.sent_bytes == confirmed_bytes);
|
||||
|
||||
/* 没有未读启停通知时,重复START/STOP各追加一个CON,FIFO保持为空。 */
|
||||
push(started ? start_act : stop_act, sizeof(start_act));
|
||||
poll_once();
|
||||
CHECK(fake.sent_bytes == confirmed_bytes + IEC104_APCI_LEN);
|
||||
check_state_confirmation(confirmed_bytes, started);
|
||||
CHECK(!sut->rx_blocked && !sut->rx_event_pending);
|
||||
CHECK((sut->connection.link_state.dt_state == IEC104_DT_STARTED) == started);
|
||||
CHECK(sut->state_event_count == 0 && sut->event_count == 0);
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_EMPTY);
|
||||
|
||||
/* 用真实状态变化填满配额;保留全部通知,再发送当前状态对应的重复ACT。 */
|
||||
confirmed_bytes = fake.sent_bytes;
|
||||
for (index = 0; index < IEC104_TCP_EVENT_STATE_RESERVE; ++index) {
|
||||
started = !started;
|
||||
push(started ? start_act : stop_act, sizeof(start_act));
|
||||
poll_once();
|
||||
}
|
||||
poll_once();
|
||||
CHECK(fake.sent_bytes == confirmed_bytes + (size_t)IEC104_TCP_EVENT_STATE_RESERVE * IEC104_APCI_LEN);
|
||||
CHECK(sut->state_event_count == IEC104_TCP_EVENT_STATE_RESERVE);
|
||||
confirmed_bytes = fake.sent_bytes;
|
||||
push(started ? start_act : stop_act, sizeof(start_act));
|
||||
poll_once();
|
||||
CHECK(fake.sent_bytes == confirmed_bytes + IEC104_APCI_LEN);
|
||||
check_state_confirmation(confirmed_bytes, started);
|
||||
CHECK(!sut->rx_blocked && !sut->rx_event_pending);
|
||||
CHECK((sut->connection.link_state.dt_state == IEC104_DT_STARTED) == started);
|
||||
CHECK(sut->state_event_count == IEC104_TCP_EVENT_STATE_RESERVE);
|
||||
CHECK(sut->event_count == IEC104_TCP_EVENT_STATE_RESERVE);
|
||||
|
||||
/* 重复ACT未占槽,也没有替换先前保留的任一通知。 */
|
||||
started = (int)initial_started;
|
||||
for (index = 0; index < IEC104_TCP_EVENT_STATE_RESERVE; ++index) {
|
||||
started = !started;
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
check_connection_event(&event, started ? IEC104_TCP_EVENT_ACTIVATED : IEC104_TCP_EVENT_DEACTIVATED,
|
||||
generation, started);
|
||||
}
|
||||
CHECK(sut->state_event_count == 0 && sut->event_count == 0);
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_EMPTY);
|
||||
end();
|
||||
}
|
||||
}
|
||||
|
||||
/* 接入限制要求读完旧启停通知,不要求读完旧DISCONNECTED。
|
||||
* 启停与建连/断开共用FIFO才有以下跨代次顺序;改成同步回调时需重新定义跨通道时序。 */
|
||||
static void test_generation_state_order(void)
|
||||
{
|
||||
unsigned index;
|
||||
uint64_t old_generation, new_generation;
|
||||
IEC104_TcpEvent_t event;
|
||||
begin();
|
||||
old_generation = sut->generation;
|
||||
for (index = 0; index < IEC104_TCP_EVENT_STATE_RESERVE; ++index) {
|
||||
push(index % 2U == 0U ? stop_act : start_act, sizeof(start_act));
|
||||
poll_once();
|
||||
}
|
||||
poll_once();
|
||||
CHECK(sut->state_event_count == IEC104_TCP_EVENT_STATE_RESERVE);
|
||||
CHECK(IEC104_TcpServer_Close_Connection(sut, old_generation, NULL) == IEC104_TCP_OK);
|
||||
CHECK(sut->event_count == IEC104_TCP_EVENT_STATE_RESERVE + 1U);
|
||||
/* 普通区有足够空间,接入被阻止应仅因旧启停通知尚未消费。 */
|
||||
CHECK(Ordinary_Event_Room(sut, TCP_EVENT_CONNECTION_BYTES));
|
||||
fake.accept_pending = 1;
|
||||
for (index = 0; index < IEC104_TCP_EVENT_STATE_RESERVE; ++index) {
|
||||
int started = index % 2U != 0U;
|
||||
poll_once();
|
||||
CHECK(fake.accept_pending == 1 && sut->client_socket == IEC104_NET_INVALID_SOCKET);
|
||||
CHECK(sut->generation == old_generation);
|
||||
CHECK(sut->state_event_count == IEC104_TCP_EVENT_STATE_RESERVE - index);
|
||||
CHECK(sut->event_count == IEC104_TCP_EVENT_STATE_RESERVE - index + 1U);
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
check_connection_event(&event, started ? IEC104_TCP_EVENT_ACTIVATED : IEC104_TCP_EVENT_DEACTIVATED,
|
||||
old_generation, started);
|
||||
}
|
||||
|
||||
/* 最后一条启停通知读走后,保留DISCONNECTED也允许新接入和START。 */
|
||||
CHECK(sut->state_event_count == 0 && sut->event_count == 1);
|
||||
poll_once();
|
||||
CHECK(fake.accept_pending == 0 && sut->client_socket != IEC104_NET_INVALID_SOCKET);
|
||||
new_generation = sut->generation;
|
||||
CHECK(new_generation > old_generation && sut->event_count == 2);
|
||||
CHECK(sut->connection.link_state.dt_state == IEC104_DT_STOPPED);
|
||||
push(start_act, sizeof(start_act));
|
||||
poll_once();
|
||||
CHECK(sut->event_count == 3 && sut->state_event_count == 1);
|
||||
CHECK(sut->connection.link_state.dt_state == IEC104_DT_STARTED);
|
||||
|
||||
/* 当前连接已STARTED,但CONNECTED快照仍为初始STOPPED,旧事件代次也不改变。 */
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
check_connection_event(&event, IEC104_TCP_EVENT_DISCONNECTED, old_generation,
|
||||
IEC104_TCP_EVENT_STATE_RESERVE % 2U == 0U);
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
check_connection_event(&event, IEC104_TCP_EVENT_CONNECTED, new_generation, 0);
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
|
||||
check_connection_event(&event, IEC104_TCP_EVENT_ACTIVATED, new_generation, 1);
|
||||
CHECK(sut->state_event_count == 0 && sut->event_count == 0);
|
||||
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_EMPTY);
|
||||
end();
|
||||
}
|
||||
|
||||
static void test_fragmented_receive_and_budget(void)
|
||||
{
|
||||
unsigned index, frames = 0, calls;
|
||||
const unsigned batch = IEC104_TCP_EVENT_NORMAL_CAPACITY < TCP_EVENT_BUDGET ?
|
||||
IEC104_TCP_EVENT_NORMAL_CAPACITY : TCP_EVENT_BUDGET;
|
||||
IEC104_TcpEvent_t event;
|
||||
begin();
|
||||
calls = fake.recv_calls;
|
||||
for (index = 0; index < batch; ++index) { push_i(index, 0, IEC104_MAX_APDU_LEN); }
|
||||
poll_once();
|
||||
CHECK(fake.recv_calls == calls + 1U && sut->event_count == batch);
|
||||
CHECK(drain() == batch);
|
||||
fake.recv_limit = 17;
|
||||
push_i(batch, 0, IEC104_MAX_APDU_LEN);
|
||||
for (index = 0; index < IEC104_MAX_APDU_LEN && frames == 0; ++index) {
|
||||
poll_once();
|
||||
while (IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY) {
|
||||
CHECK(event.type == IEC104_TCP_EVENT_I_FRAME && event.data.i_frame.apdu_length == IEC104_MAX_APDU_LEN);
|
||||
CHECK(event.data.i_frame.send_number == batch);
|
||||
++frames;
|
||||
}
|
||||
}
|
||||
CHECK(frames == 1);
|
||||
end();
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("Sizes: header=%zu normal=%zu state=%zu close=%zu ring=%zu rx=%u combined=%zu server=%zu\n",
|
||||
sizeof(TcpEventHeader_t), TCP_EVENT_NORMAL_BYTES, TCP_EVENT_STATE_BYTES, TCP_EVENT_CLOSE_BYTES,
|
||||
TCP_EVENT_CAPACITY_BYTES, (unsigned)IEC104_TCP_RX_CAPACITY,
|
||||
TCP_EVENT_CAPACITY_BYTES + IEC104_TCP_RX_CAPACITY, sizeof(*sut));
|
||||
test_wrap_roundtrip();
|
||||
test_full_reserves();
|
||||
test_room_boundaries();
|
||||
test_invalid_entries();
|
||||
test_short_capacity();
|
||||
test_pending_frame_before_record();
|
||||
test_delayed_receipt(0);
|
||||
test_delayed_receipt(1);
|
||||
test_close_with_pending_frame_and_all_records();
|
||||
test_state_reserve_backpressure();
|
||||
test_repeated_state_act();
|
||||
test_generation_state_order();
|
||||
test_fragmented_receive_and_budget();
|
||||
puts("TCP event ring: 13 cases passed");
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
int main(void)
|
||||
{
|
||||
static const uint16_t lengths[] = {16, IEC104_MAX_APDU_LEN};
|
||||
unsigned kind;
|
||||
printf("Server=%zu rx=%u\n", sizeof(*sut), (unsigned)IEC104_TCP_RX_CAPACITY);
|
||||
for (kind = 0; kind < sizeof(lengths) / sizeof(lengths[0]); ++kind) {
|
||||
unsigned batch, index, calls, frames = 0;
|
||||
const unsigned batches = 200000;
|
||||
clock_t started;
|
||||
begin();
|
||||
calls = fake.recv_calls;
|
||||
started = clock();
|
||||
for (batch = 0; batch < batches; ++batch) {
|
||||
unsigned delivered = 0, attempts = 0;
|
||||
for (index = 0; index < TCP_EVENT_BUDGET; ++index) {
|
||||
push_i(batch * TCP_EVENT_BUDGET + index, 0, lengths[kind]);
|
||||
}
|
||||
do {
|
||||
poll_once();
|
||||
if (sut->client_socket == IEC104_NET_INVALID_SOCKET) {
|
||||
IEC104_TcpServerStatus_t status;
|
||||
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
|
||||
fprintf(stderr, "benchmark closed: batch=%u APDU=%u fault=%d\n", batch, lengths[kind],
|
||||
status.connection.fault);
|
||||
}
|
||||
CHECK(sut->client_socket != IEC104_NET_INVALID_SOCKET);
|
||||
delivered += drain();
|
||||
CHECK(++attempts <= TCP_IO_BUDGET);
|
||||
} while (delivered < TCP_EVENT_BUDGET);
|
||||
CHECK(delivered == TCP_EVENT_BUDGET);
|
||||
frames += delivered;
|
||||
/* 模拟主站等待S确认后再发下一批;给原有调度一次无新输入的发送机会。 */
|
||||
poll_once();
|
||||
CHECK(drain() == 0);
|
||||
}
|
||||
CHECK(frames == batches * TCP_EVENT_BUDGET);
|
||||
CHECK(fake.recv_calls - calls == 2U * batches);
|
||||
printf("APDU=%u frames=%u recv=%u us_per_frame=%.3f\n", lengths[kind], frames,
|
||||
fake.recv_calls - calls, 1000000.0 * (double)(clock() - started) / CLOCKS_PER_SEC / frames);
|
||||
end();
|
||||
}
|
||||
{
|
||||
unsigned index, calls, frames = 0;
|
||||
begin();
|
||||
for (index = 0; index < 2U * TCP_EVENT_BUDGET; ++index) { push_i(index, 0, IEC104_MAX_APDU_LEN); }
|
||||
calls = fake.recv_calls;
|
||||
for (index = 0; index < TCP_IO_BUDGET && frames < 2U * TCP_EVENT_BUDGET; ++index) {
|
||||
poll_once();
|
||||
frames += drain();
|
||||
}
|
||||
CHECK(frames == 2U * TCP_EVENT_BUDGET);
|
||||
printf("Burst: APDU=%u frames=%u recv=%u\n", IEC104_MAX_APDU_LEN, frames, fake.recv_calls - calls);
|
||||
end();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,353 @@
|
||||
/* 调度回归复用可控Net后端;不另行链接TcpServer.c、Net或字节环测试的翻译单元。 */
|
||||
#define main event_ring_suite_main
|
||||
#include "iec104_tcp_event_ring_test.c"
|
||||
#undef main
|
||||
|
||||
static IEC104_TcpServerStatus_t scheduler_status(void)
|
||||
{
|
||||
IEC104_TcpServerStatus_t status;
|
||||
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
|
||||
CHECK(status.connection_status_valid);
|
||||
return status;
|
||||
}
|
||||
|
||||
static void scheduler_begin(uint32_t t1, uint32_t local_timeout)
|
||||
{
|
||||
IEC104_TcpServerConfig_t config;
|
||||
memset(&fake, 0, sizeof(fake));
|
||||
fake.now = 1000;
|
||||
fake.accept_pending = 1;
|
||||
sut = IEC104_TcpServer_Create();
|
||||
CHECK(sut != NULL);
|
||||
IEC104_TcpServer_Config_Default(&config);
|
||||
config.protocol.link.t1_ms = t1;
|
||||
config.protocol.link.t2_ms = 50;
|
||||
config.protocol.local_send_timeout_ms = local_timeout;
|
||||
config.protocol.stop_timeout_ms = 100;
|
||||
CHECK(IEC104_TcpServer_Init(sut, &config, NULL) == IEC104_TCP_OK);
|
||||
poll_once();
|
||||
CHECK(drain() == 1);
|
||||
push(start_act, sizeof(start_act));
|
||||
poll_once();
|
||||
CHECK(drain() == 1 && fake.sent_bytes == sizeof(start_act));
|
||||
}
|
||||
|
||||
static void scheduler_send_one(void)
|
||||
{
|
||||
CHECK(IEC104_TcpServer_Enqueue_Event(sut, asdu, sizeof(asdu)) == IEC104_TCP_SUBMIT_OK);
|
||||
poll_once();
|
||||
CHECK(fake.sent_bytes == sizeof(start_act) + IEC104_APCI_LEN + sizeof(asdu));
|
||||
}
|
||||
|
||||
static void check_connected(void)
|
||||
{
|
||||
IEC104_TcpServerStatus_t status = scheduler_status();
|
||||
CHECK(status.connected && status.connection.fault == IEC104_TCP_FAULT_NONE);
|
||||
}
|
||||
|
||||
static void check_fault(IEC104_TcpFault_t fault)
|
||||
{
|
||||
IEC104_TcpServerStatus_t status = scheduler_status();
|
||||
CHECK(!status.connected && status.connection.fault == fault);
|
||||
CHECK(!sut->rx_snapshot_active && sut->rx_snapshot_remaining == 0);
|
||||
CHECK(!sut->connection.receive_batch_active && !sut->connection.receive_batch_pending);
|
||||
}
|
||||
|
||||
static void check_immediate_continuation(void)
|
||||
{
|
||||
IEC104_TcpWaitInfo_t info;
|
||||
CHECK(IEC104_TcpServer_Prepare(sut, &info, NULL) == IEC104_TCP_OK);
|
||||
CHECK(info.need_process && info.timer_active && info.timeout_ms == 0);
|
||||
CHECK(!sut->connection.receive_batch_active && sut->connection.receive_batch_pending);
|
||||
}
|
||||
|
||||
/* 每轮仍最多交付8个I事件;ACK可能在同一recv尾部或任意分段后的内核队列中。 */
|
||||
static void test_ack_boundary(unsigned preceding_i, size_t recv_limit, uint16_t apdu_length)
|
||||
{
|
||||
unsigned index, frames = 0, records = 0;
|
||||
IEC104_TcpEvent_t event;
|
||||
scheduler_begin(100, 5000);
|
||||
scheduler_send_one();
|
||||
for (index = 0; index < preceding_i; ++index) { push_i(index, 0, apdu_length); }
|
||||
push_ack(1);
|
||||
fake.recv_limit = recv_limit;
|
||||
fake.now = 1100;
|
||||
for (index = 0; index < 2000 && !records; ++index) {
|
||||
unsigned round_frames = 0;
|
||||
poll_once();
|
||||
check_connected();
|
||||
if (sut->rx_snapshot_active) { check_immediate_continuation(); }
|
||||
while (IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY) {
|
||||
if (event.type == IEC104_TCP_EVENT_I_FRAME) {
|
||||
CHECK(event.data.i_frame.send_number == frames);
|
||||
CHECK(event.data.i_frame.apdu_length == apdu_length);
|
||||
++frames;
|
||||
++round_frames;
|
||||
} else {
|
||||
CHECK(event.type == IEC104_TCP_EVENT_RECORD && !event.data.record.closing);
|
||||
CHECK(event.data.record.value.local_send_completed && event.data.record.value.peer_acknowledged);
|
||||
++records;
|
||||
}
|
||||
}
|
||||
CHECK(round_frames <= TCP_EVENT_BUDGET);
|
||||
}
|
||||
CHECK(frames == preceding_i && records == 1);
|
||||
CHECK(!sut->rx_snapshot_active && !sut->connection.receive_batch_pending);
|
||||
CHECK(fake.offset == fake.length && sut->rx_end == 0);
|
||||
end();
|
||||
}
|
||||
|
||||
/* 快照之外的新流量不能延长旧ACK期限;覆盖用户态尾部与跨多次recv的内核快照。 */
|
||||
static void test_fixed_snapshot(size_t recv_limit, int control_only)
|
||||
{
|
||||
unsigned index, rounds = 0, old_count = control_only ? 3U * TCP_IO_BUDGET : 33U;
|
||||
unsigned next_sequence = old_count, snapshots;
|
||||
scheduler_begin(100, 5000);
|
||||
scheduler_send_one();
|
||||
snapshots = fake.snapshot_calls;
|
||||
for (index = 0; index < old_count; ++index) {
|
||||
if (control_only) { push_ack(0); } else { push_i(index, 0, 16); }
|
||||
}
|
||||
fake.recv_limit = recv_limit;
|
||||
fake.now = 1100;
|
||||
poll_once();
|
||||
check_connected();
|
||||
check_immediate_continuation();
|
||||
CHECK(fake.snapshot_calls == snapshots + 1);
|
||||
snapshots = fake.snapshot_calls;
|
||||
drain();
|
||||
while (sut->client_socket != IEC104_NET_INVALID_SOCKET && rounds++ < old_count + 10U) {
|
||||
for (index = 0; index < 10; ++index) {
|
||||
if (control_only) { push_ack(0); } else { push_i(next_sequence++, 0, 16); }
|
||||
}
|
||||
poll_once();
|
||||
drain();
|
||||
}
|
||||
check_fault(IEC104_TCP_FAULT_I_ACK_TIMEOUT);
|
||||
CHECK(fake.snapshot_calls == snapshots && fake.offset < fake.length);
|
||||
end();
|
||||
}
|
||||
|
||||
static void test_missing_ack(void)
|
||||
{
|
||||
scheduler_begin(100, 5000);
|
||||
scheduler_send_one();
|
||||
fake.now = 1100;
|
||||
poll_once();
|
||||
check_fault(IEC104_TCP_FAULT_I_ACK_TIMEOUT);
|
||||
end();
|
||||
}
|
||||
|
||||
static void test_writable_stream(size_t send_limit)
|
||||
{
|
||||
unsigned round, index;
|
||||
size_t written;
|
||||
scheduler_begin(1000, 100);
|
||||
fake.send_limit = send_limit;
|
||||
written = fake.sent_bytes;
|
||||
for (round = 0; round < 8; ++round) {
|
||||
fake.now = 1000 + round * 50U;
|
||||
for (index = 0; index < TCP_EVENT_BUDGET; ++index) {
|
||||
push_i(round * TCP_EVENT_BUDGET + index, 0, 16);
|
||||
}
|
||||
poll_once();
|
||||
check_connected();
|
||||
CHECK(drain() == TCP_EVENT_BUDGET);
|
||||
CHECK(fake.sent_bytes > written);
|
||||
written = fake.sent_bytes;
|
||||
}
|
||||
end();
|
||||
}
|
||||
|
||||
static void test_business_output_after_yield(void)
|
||||
{
|
||||
unsigned index;
|
||||
size_t written;
|
||||
scheduler_begin(1000, 100);
|
||||
for (index = 0; index < TCP_EVENT_BUDGET; ++index) { push_i(index, 0, 16); }
|
||||
CHECK(IEC104_TcpServer_Enqueue_Event(sut, asdu, sizeof(asdu)) == IEC104_TCP_SUBMIT_OK);
|
||||
written = fake.sent_bytes;
|
||||
poll_once();
|
||||
check_connected();
|
||||
CHECK(fake.sent_bytes >= written + IEC104_APCI_LEN + sizeof(asdu));
|
||||
CHECK(sut->connection.send_manager.record_count == 1);
|
||||
end();
|
||||
}
|
||||
|
||||
static void test_blocked_output(int recover)
|
||||
{
|
||||
unsigned index, calls;
|
||||
size_t written;
|
||||
scheduler_begin(1000, 100);
|
||||
written = fake.sent_bytes;
|
||||
fake.send_blocked = 1;
|
||||
for (index = 0; index < TCP_EVENT_BUDGET; ++index) { push_i(index, 0, 16); }
|
||||
calls = fake.send_calls;
|
||||
poll_once();
|
||||
check_connected();
|
||||
CHECK(fake.send_calls == calls + 1 && fake.sent_bytes == written);
|
||||
drain();
|
||||
if (recover) {
|
||||
fake.now = 1050;
|
||||
fake.send_blocked = 0;
|
||||
poll_once();
|
||||
check_connected();
|
||||
CHECK(fake.sent_bytes > written);
|
||||
} else {
|
||||
fake.now = 1100;
|
||||
for (index = 0; index < 9; ++index) { push_i(TCP_EVENT_BUDGET + index, 0, 16); }
|
||||
poll_once();
|
||||
check_fault(IEC104_TCP_FAULT_LOCAL_SEND_TIMEOUT);
|
||||
}
|
||||
end();
|
||||
}
|
||||
|
||||
static void test_testfr_boundary(int provide_confirmation)
|
||||
{
|
||||
static const uint8_t test_con[] = {0x68, 4, 0x83, 0, 0, 0};
|
||||
unsigned index;
|
||||
scheduler_begin(100, 5000);
|
||||
/* 等到t3,真实推进TESTFR_ACT发送并启动探测t1。 */
|
||||
fake.now += sut->config.protocol.link.t3_ms;
|
||||
poll_once();
|
||||
CHECK(sut->connection.testfr_timer_active);
|
||||
for (index = 0; index < 9; ++index) { push_i(index, 0, 16); }
|
||||
if (provide_confirmation) { push(test_con, sizeof(test_con)); }
|
||||
fake.now += 100;
|
||||
poll_once();
|
||||
check_connected();
|
||||
check_immediate_continuation();
|
||||
drain();
|
||||
poll_once();
|
||||
if (provide_confirmation) {
|
||||
check_connected();
|
||||
CHECK(!sut->connection.testfr_timer_active);
|
||||
} else {
|
||||
check_fault(IEC104_TCP_FAULT_TESTFR_TIMEOUT);
|
||||
}
|
||||
end();
|
||||
}
|
||||
|
||||
/* 原有容量策略:真正空间不足仍暂停接收,不用预算续收标志掩盖严格期限。 */
|
||||
static void test_real_backpressure(int state_full)
|
||||
{
|
||||
unsigned index;
|
||||
unsigned count = (unsigned)(TCP_EVENT_NORMAL_BYTES /
|
||||
(sizeof(TcpEventHeader_t) + TCP_EVENT_I_PREFIX + 16));
|
||||
scheduler_begin(100, 5000);
|
||||
scheduler_send_one();
|
||||
if (state_full) {
|
||||
for (index = 0; index < IEC104_TCP_EVENT_STATE_RESERVE; ++index) {
|
||||
push(index % 2 == 0 ? stop_act : start_act, sizeof(start_act));
|
||||
poll_once();
|
||||
}
|
||||
push(IEC104_TCP_EVENT_STATE_RESERVE % 2 == 0 ? stop_act : start_act, sizeof(start_act));
|
||||
} else {
|
||||
for (index = 0; index < count; ++index) { push_i(index, 0, 16); poll_once(); }
|
||||
push_i(count, 0, 16);
|
||||
}
|
||||
push_ack(1);
|
||||
poll_once();
|
||||
check_connected();
|
||||
CHECK(state_full ? sut->rx_blocked : sut->rx_event_pending);
|
||||
CHECK(!sut->connection.receive_batch_pending);
|
||||
fake.now = 1100;
|
||||
poll_once();
|
||||
check_fault(IEC104_TCP_FAULT_I_ACK_TIMEOUT);
|
||||
end();
|
||||
}
|
||||
|
||||
static void test_response_during_continuation(void)
|
||||
{
|
||||
scheduler_begin(100, 5000);
|
||||
scheduler_send_one();
|
||||
push_i(0, 0, 16);
|
||||
push_ack(1);
|
||||
fake.recv_limit = 16;
|
||||
fake.now = 1100;
|
||||
poll_once();
|
||||
check_immediate_continuation();
|
||||
CHECK(drain() == 1);
|
||||
CHECK(IEC104_TcpServer_Send_Response(sut, sut->generation, asdu, sizeof(asdu)) == IEC104_TCP_SUBMIT_OK);
|
||||
poll_once();
|
||||
check_connected();
|
||||
CHECK(drain() == 1);
|
||||
push_ack(2);
|
||||
fake.now = 1200;
|
||||
poll_once();
|
||||
check_connected();
|
||||
CHECK(drain() == 1);
|
||||
end();
|
||||
}
|
||||
|
||||
static void test_invalid_ack(void)
|
||||
{
|
||||
unsigned index;
|
||||
scheduler_begin(100, 5000);
|
||||
scheduler_send_one();
|
||||
for (index = 0; index < TCP_EVENT_BUDGET; ++index) { push_i(index, 0, 16); }
|
||||
push_ack(2);
|
||||
fake.now = 1100;
|
||||
poll_once();
|
||||
check_immediate_continuation();
|
||||
drain();
|
||||
poll_once();
|
||||
check_fault(IEC104_TCP_FAULT_PROTOCOL);
|
||||
end();
|
||||
}
|
||||
|
||||
static void test_snapshot_failure(void)
|
||||
{
|
||||
scheduler_begin(100, 5000);
|
||||
push_i(0, 0, 16);
|
||||
fake.pending_error = 1;
|
||||
poll_once();
|
||||
check_fault(IEC104_TCP_FAULT_TRANSPORT);
|
||||
end();
|
||||
}
|
||||
|
||||
static void test_eof_response(void)
|
||||
{
|
||||
scheduler_begin(100, 5000);
|
||||
push_i(0, 0, 16);
|
||||
fake.recv_eof = 1;
|
||||
poll_once();
|
||||
check_connected();
|
||||
CHECK(!sut->rx_eof && drain() == 1);
|
||||
CHECK(IEC104_TcpServer_Send_Response(sut, sut->generation, asdu, sizeof(asdu)) == IEC104_TCP_SUBMIT_OK);
|
||||
CHECK(fake.sent_bytes == sizeof(start_act) + IEC104_APCI_LEN + sizeof(asdu));
|
||||
poll_once();
|
||||
CHECK(!scheduler_status().connected);
|
||||
end();
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_ack_boundary(7, 0, 16);
|
||||
test_ack_boundary(8, 0, 16);
|
||||
test_ack_boundary(9, 0, 16);
|
||||
test_ack_boundary(33, 0, 16);
|
||||
test_ack_boundary(1, 16, 16);
|
||||
test_ack_boundary(1, 1, 16);
|
||||
test_ack_boundary(16, 0, IEC104_MAX_APDU_LEN);
|
||||
test_ack_boundary(33, 17, IEC104_MAX_APDU_LEN);
|
||||
test_fixed_snapshot(0, 0);
|
||||
test_fixed_snapshot(16, 0);
|
||||
test_fixed_snapshot(0, 1);
|
||||
test_missing_ack();
|
||||
test_writable_stream(0);
|
||||
test_writable_stream(2);
|
||||
test_business_output_after_yield();
|
||||
test_blocked_output(1);
|
||||
test_blocked_output(0);
|
||||
test_testfr_boundary(1);
|
||||
test_testfr_boundary(0);
|
||||
test_real_backpressure(0);
|
||||
test_real_backpressure(1);
|
||||
test_response_during_continuation();
|
||||
test_invalid_ack();
|
||||
test_snapshot_failure();
|
||||
test_eof_response();
|
||||
puts("TCP scheduler: 25 cases passed (including strict backpressure policy checks)");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,604 @@
|
||||
/* 新增37种类型的独立报文字节向量及边界测试。
|
||||
* 期望字节手工给出,不通过生产编码器计算;联合体仅用于测试存储。 */
|
||||
#include "IEC60870_Asdu.h"
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define CHECK(c) do { if (!(c)) { fprintf(stderr, "%s:%d: %s\n", __func__, __LINE__, #c); return 0; } } while (0)
|
||||
#define COUNT(a) (sizeof(a) / sizeof((a)[0]))
|
||||
#define SHORT_TIME_BYTES 0xD5, 0xDD, 0xA2
|
||||
#define LONG_TIME_BYTES 0xD5, 0xDD, 0xA2, 0x8C, 0x9D, 0x02, 0x18
|
||||
|
||||
static const IEC60870_CP24Time2a_t short_time = {56789, 34, 1};
|
||||
static const IEC60870_CP56Time2a_t long_time = {{2024,2,29,12,34,56,789,4},1,1,1};
|
||||
static const IEC60870_CP16Time2a_t elapsed = {0xBEEF};
|
||||
static const IEC60870_BinaryCounterReading_t counter = {INT32_MIN,21,1,1,1};
|
||||
static const uint8_t segment_data[] = {0xDE,0xAD,0xBE,0xEF};
|
||||
/* 公共测试显式提供布局,不引入104传输头文件。 */
|
||||
static const IEC60870_AppLayerParameters_t default_parameters = {1, 1, 2, 0, 2, 3, 249};
|
||||
|
||||
typedef union {
|
||||
IEC60870_StepPositionInformation_t v5;
|
||||
IEC60870_BitString32_t v7;
|
||||
IEC60870_SinglePointWithCP24Time2a_t v2;
|
||||
IEC60870_DoublePointWithCP24Time2a_t v4;
|
||||
IEC60870_StepPositionWithCP24Time2a_t v6;
|
||||
IEC60870_Bitstring32WithCP24Time2a_t v8;
|
||||
IEC60870_MeasuredValueNormalizedWithCP24Time2a_t v10;
|
||||
IEC60870_MeasuredValueScaledWithCP24Time2a_t v12;
|
||||
IEC60870_MeasuredValueShortWithCP24Time2a_t v14;
|
||||
IEC60870_IntegratedTotalsWithCP24Time2a_t v16;
|
||||
IEC60870_StepPositionWithCP56Time2a_t v32;
|
||||
IEC60870_Bitstring32WithCP56Time2a_t v33;
|
||||
IEC60870_EventOfProtectionEquipment_t v17;
|
||||
IEC60870_PackedStartEventsOfProtectionEquipment_t v18;
|
||||
IEC60870_PackedOutputCircuitInfo_t v19;
|
||||
IEC60870_EventOfProtectionEquipmentWithCP56Time2a_t v38;
|
||||
IEC60870_PackedStartEventsOfProtectionEquipmentWithCP56Time2a_t v39;
|
||||
IEC60870_PackedOutputCircuitInfoWithCP56Time2a_t v40;
|
||||
IEC60870_PackedSinglePointWithSCD_t v20;
|
||||
IEC60870_MeasuredValueNormalizedWithoutQuality_t v21;
|
||||
IEC60870_Bitstring32Command_t v51;
|
||||
IEC60870_Bitstring32CommandWithCP56Time2a_t v64;
|
||||
IEC60870_EndOfInitialization_t v70;
|
||||
IEC60870_TestCommand_t v104;
|
||||
IEC60870_DelayAcquisitionCommand_t v106;
|
||||
IEC60870_ParameterNormalizedValue_t v110;
|
||||
IEC60870_ParameterScaledValue_t v111;
|
||||
IEC60870_ParameterFloatValue_t v112;
|
||||
IEC60870_ParameterActivation_t v113;
|
||||
IEC60870_FileReady_t v120;
|
||||
IEC60870_SectionReady_t v121;
|
||||
IEC60870_FileCallOrSelect_t v122;
|
||||
IEC60870_FileACK_t v124;
|
||||
IEC60870_FileLastSegmentOrSection_t v123;
|
||||
IEC60870_FileSegment_t v125;
|
||||
IEC60870_FileDirectory_t v126;
|
||||
IEC60870_QueryLog_t v127;
|
||||
} Object;
|
||||
|
||||
typedef struct {
|
||||
uint8_t type, length, single, sequence;
|
||||
uint8_t payload[16];
|
||||
} Vector;
|
||||
|
||||
static const Vector vectors[] = {
|
||||
{2, 4, 0, 1, {0xF1, SHORT_TIME_BYTES}},
|
||||
{4, 4, 0, 1, {0x93, SHORT_TIME_BYTES}},
|
||||
{5, 2, 0, 1, {0xFE, 0x81}},
|
||||
{6, 5, 0, 1, {0xFE, 0x81, SHORT_TIME_BYTES}},
|
||||
{7, 5, 0, 1, {0xEF, 0xCD, 0xAB, 0x89, 0x51}},
|
||||
{8, 8, 0, 1, {0xEF, 0xCD, 0xAB, 0x89, 0x51, SHORT_TIME_BYTES}},
|
||||
{10, 6, 0, 1, {0, 0x80, 0x81, SHORT_TIME_BYTES}},
|
||||
{12, 6, 0, 1, {0xFE, 0xFF, 0x10, SHORT_TIME_BYTES}},
|
||||
{14, 8, 0, 1, {0, 0, 0xC0, 0x3F, 0x41, SHORT_TIME_BYTES}},
|
||||
{16, 8, 0, 1, {0, 0, 0, 0x80, 0xF5, SHORT_TIME_BYTES}},
|
||||
{17, 6, 0, 1, {0x9A, 0xEF, 0xBE, SHORT_TIME_BYTES}},
|
||||
{18, 7, 0, 1, {0x21, 0x98, 0xEF, 0xBE, SHORT_TIME_BYTES}},
|
||||
{19, 7, 0, 1, {9, 0x98, 0xEF, 0xBE, SHORT_TIME_BYTES}},
|
||||
{20, 5, 0, 1, {0x34, 0x12, 0xCD, 0xAB, 0x81}},
|
||||
{21, 2, 0, 1, {0, 0x80}},
|
||||
{32, 9, 0, 1, {0xFE, 0x81, LONG_TIME_BYTES}},
|
||||
{33, 12, 0, 1, {0xEF, 0xCD, 0xAB, 0x89, 0x51, LONG_TIME_BYTES}},
|
||||
{38, 10, 0, 1, {0x9A, 0xEF, 0xBE, LONG_TIME_BYTES}},
|
||||
{39, 11, 0, 1, {0x21, 0x98, 0xEF, 0xBE, LONG_TIME_BYTES}},
|
||||
{40, 11, 0, 1, {9, 0x98, 0xEF, 0xBE, LONG_TIME_BYTES}},
|
||||
{51, 4, 1, 0, {0xEF, 0xCD, 0xAB, 0x89}},
|
||||
{64, 11, 1, 0, {0xEF, 0xCD, 0xAB, 0x89, LONG_TIME_BYTES}},
|
||||
{70, 1, 1, 0, {0x82}},
|
||||
{104, 2, 1, 0, {0xAA, 0x55}},
|
||||
{106, 2, 1, 0, {0xEF, 0xBE}},
|
||||
{110, 3, 0, 0, {0, 0x80, 0xC3}},
|
||||
{111, 3, 0, 0, {0xFE, 0xFF, 0xC3}},
|
||||
{112, 5, 0, 0, {0, 0, 0xC0, 0x3F, 0xC3}},
|
||||
{113, 1, 0, 0, {0x82}},
|
||||
{120, 6, 1, 0, {0x34, 0x12, 1, 0xCD, 0xAB, 0x80}},
|
||||
{121, 7, 1, 0, {0x34, 0x12, 0x56, 1, 0xCD, 0xAB, 0x80}},
|
||||
{122, 4, 1, 0, {0x34, 0x12, 0x56, 0x12}},
|
||||
{123, 5, 1, 0, {0x34, 0x12, 0x56, 1, 0xA5}},
|
||||
{124, 4, 1, 0, {0x34, 0x12, 0x56, 0x12}},
|
||||
{125, 8, 1, 0, {0x34, 0x12, 0x56, 4, 0xDE, 0xAD, 0xBE, 0xEF}},
|
||||
{126, 13, 0, 1, {0x34, 0x12, 1, 0xCD, 0xAB, 0xE3, LONG_TIME_BYTES}},
|
||||
{127, 16, 1, 0, {0x34, 0x12, LONG_TIME_BYTES, LONG_TIME_BYTES}},
|
||||
};
|
||||
|
||||
static IEC60870_AsduResult_t Create_Object(uint8_t type, uint32_t ioa, Object *out)
|
||||
{
|
||||
switch (type) {
|
||||
case 2: return IEC60870_SinglePointWithCP24Time2a_Create(&out->v2, ioa, 1, 0xF0, &short_time);
|
||||
case 4: return IEC60870_DoublePointWithCP24Time2a_Create(&out->v4, ioa, 3, 0x90, &short_time);
|
||||
case 5: return IEC60870_StepPositionInformation_Create(&out->v5, ioa, -2, 1, 0x81);
|
||||
case 6: return IEC60870_StepPositionWithCP24Time2a_Create(&out->v6, ioa, -2, 1, 0x81, &short_time);
|
||||
case 7: return IEC60870_BitString32_Create(&out->v7, ioa, UINT32_C(0x89ABCDEF), 0x51);
|
||||
case 8: return IEC60870_Bitstring32WithCP24Time2a_Create(&out->v8, ioa, UINT32_C(0x89ABCDEF), 0x51, &short_time);
|
||||
case 10: return IEC60870_MeasuredValueNormalizedWithCP24Time2a_Create(&out->v10, ioa, INT16_MIN, 0x81, &short_time);
|
||||
case 12: return IEC60870_MeasuredValueScaledWithCP24Time2a_Create(&out->v12, ioa, -2, 0x10, &short_time);
|
||||
case 14: return IEC60870_MeasuredValueShortWithCP24Time2a_Create(&out->v14, ioa, 1.5f, 0x41, &short_time);
|
||||
case 16: return IEC60870_IntegratedTotalsWithCP24Time2a_Create(&out->v16, ioa, &counter, &short_time);
|
||||
case 17: return IEC60870_EventOfProtectionEquipment_Create(&out->v17, ioa, 2, 0x98, &elapsed, &short_time);
|
||||
case 18: return IEC60870_PackedStartEventsOfProtectionEquipment_Create(&out->v18, ioa, 0x21, 0x98, &elapsed, &short_time);
|
||||
case 19: return IEC60870_PackedOutputCircuitInfo_Create(&out->v19, ioa, 9, 0x98, &elapsed, &short_time);
|
||||
case 20: return IEC60870_PackedSinglePointWithSCD_Create(&out->v20, ioa, 0x1234, 0xABCD, 0x81);
|
||||
case 21: return IEC60870_MeasuredValueNormalizedWithoutQuality_Create(&out->v21, ioa, INT16_MIN);
|
||||
case 32: return IEC60870_StepPositionWithCP56Time2a_Create(&out->v32, ioa, -2, 1, 0x81, &long_time);
|
||||
case 33: return IEC60870_Bitstring32WithCP56Time2a_Create(&out->v33, ioa, UINT32_C(0x89ABCDEF), 0x51, &long_time);
|
||||
case 38: return IEC60870_EventOfProtectionEquipmentWithCP56Time2a_Create(&out->v38, ioa, 2, 0x98, &elapsed, &long_time);
|
||||
case 39: return IEC60870_PackedStartEventsOfProtectionEquipmentWithCP56Time2a_Create(&out->v39, ioa, 0x21, 0x98, &elapsed, &long_time);
|
||||
case 40: return IEC60870_PackedOutputCircuitInfoWithCP56Time2a_Create(&out->v40, ioa, 9, 0x98, &elapsed, &long_time);
|
||||
case 51: return IEC60870_Bitstring32Command_Create(&out->v51, ioa, UINT32_C(0x89ABCDEF));
|
||||
case 64: return IEC60870_Bitstring32CommandWithCP56Time2a_Create(&out->v64, ioa, UINT32_C(0x89ABCDEF), &long_time);
|
||||
case 70: return IEC60870_EndOfInitialization_Create(&out->v70, ioa, 0x82);
|
||||
case 104: return IEC60870_TestCommand_Create(&out->v104, ioa);
|
||||
case 106: return IEC60870_DelayAcquisitionCommand_Create(&out->v106, ioa, &elapsed);
|
||||
case 110: return IEC60870_ParameterNormalizedValue_Create(&out->v110, ioa, INT16_MIN, 0xC3);
|
||||
case 111: return IEC60870_ParameterScaledValue_Create(&out->v111, ioa, -2, 0xC3);
|
||||
case 112: return IEC60870_ParameterFloatValue_Create(&out->v112, ioa, 1.5f, 0xC3);
|
||||
case 113: return IEC60870_ParameterActivation_Create(&out->v113, ioa, 0x82);
|
||||
case 120: return IEC60870_FileReady_Create(&out->v120, ioa, 0x1234, UINT32_C(0xABCD01), 0x80);
|
||||
case 121: return IEC60870_SectionReady_Create(&out->v121, ioa, 0x1234, 0x56, UINT32_C(0xABCD01), 0x80);
|
||||
case 122: return IEC60870_FileCallOrSelect_Create(&out->v122, ioa, 0x1234, 0x56, 0x12);
|
||||
case 123: return IEC60870_FileLastSegmentOrSection_Create(&out->v123, ioa, 0x1234, 0x56, 1, 0xA5);
|
||||
case 124: return IEC60870_FileACK_Create(&out->v124, ioa, 0x1234, 0x56, 0x12);
|
||||
case 125: return IEC60870_FileSegment_Create(&out->v125, ioa, 0x1234, 0x56, segment_data, sizeof(segment_data));
|
||||
case 126: return IEC60870_FileDirectory_Create(&out->v126, ioa, 0x1234, UINT32_C(0xABCD01), 0xE3, &long_time);
|
||||
case 127: return IEC60870_QueryLog_Create(&out->v127, ioa, 0x1234, &long_time, &long_time);
|
||||
default: return IEC60870_ASDU_UNSUPPORTED;
|
||||
}
|
||||
}
|
||||
|
||||
static IEC60870_AsduResult_t Parse_Object(uint8_t type, uint32_t ioa, const uint8_t *payload, uint16_t length, Object *out)
|
||||
{
|
||||
switch (type) {
|
||||
case 5: return IEC60870_StepPositionInformation_Get_From_Buffer(&out->v5, ioa, payload, length);
|
||||
case 7: return IEC60870_BitString32_Get_From_Buffer(&out->v7, ioa, payload, length);
|
||||
case 2: return IEC60870_SinglePointWithCP24Time2a_Get_From_Buffer(&out->v2, ioa, payload, length);
|
||||
case 4: return IEC60870_DoublePointWithCP24Time2a_Get_From_Buffer(&out->v4, ioa, payload, length);
|
||||
case 6: return IEC60870_StepPositionWithCP24Time2a_Get_From_Buffer(&out->v6, ioa, payload, length);
|
||||
case 8: return IEC60870_Bitstring32WithCP24Time2a_Get_From_Buffer(&out->v8, ioa, payload, length);
|
||||
case 10: return IEC60870_MeasuredValueNormalizedWithCP24Time2a_Get_From_Buffer(&out->v10, ioa, payload, length);
|
||||
case 12: return IEC60870_MeasuredValueScaledWithCP24Time2a_Get_From_Buffer(&out->v12, ioa, payload, length);
|
||||
case 14: return IEC60870_MeasuredValueShortWithCP24Time2a_Get_From_Buffer(&out->v14, ioa, payload, length);
|
||||
case 16: return IEC60870_IntegratedTotalsWithCP24Time2a_Get_From_Buffer(&out->v16, ioa, payload, length);
|
||||
case 32: return IEC60870_StepPositionWithCP56Time2a_Get_From_Buffer(&out->v32, ioa, payload, length);
|
||||
case 33: return IEC60870_Bitstring32WithCP56Time2a_Get_From_Buffer(&out->v33, ioa, payload, length);
|
||||
case 17: return IEC60870_EventOfProtectionEquipment_Get_From_Buffer(&out->v17, ioa, payload, length);
|
||||
case 18: return IEC60870_PackedStartEventsOfProtectionEquipment_Get_From_Buffer(&out->v18, ioa, payload, length);
|
||||
case 19: return IEC60870_PackedOutputCircuitInfo_Get_From_Buffer(&out->v19, ioa, payload, length);
|
||||
case 38: return IEC60870_EventOfProtectionEquipmentWithCP56Time2a_Get_From_Buffer(&out->v38, ioa, payload, length);
|
||||
case 39: return IEC60870_PackedStartEventsOfProtectionEquipmentWithCP56Time2a_Get_From_Buffer(&out->v39, ioa, payload, length);
|
||||
case 40: return IEC60870_PackedOutputCircuitInfoWithCP56Time2a_Get_From_Buffer(&out->v40, ioa, payload, length);
|
||||
case 20: return IEC60870_PackedSinglePointWithSCD_Get_From_Buffer(&out->v20, ioa, payload, length);
|
||||
case 21: return IEC60870_MeasuredValueNormalizedWithoutQuality_Get_From_Buffer(&out->v21, ioa, payload, length);
|
||||
case 51: return IEC60870_Bitstring32Command_Get_From_Buffer(&out->v51, ioa, payload, length);
|
||||
case 64: return IEC60870_Bitstring32CommandWithCP56Time2a_Get_From_Buffer(&out->v64, ioa, payload, length);
|
||||
case 70: return IEC60870_EndOfInitialization_Get_From_Buffer(&out->v70, ioa, payload, length);
|
||||
case 104: return IEC60870_TestCommand_Get_From_Buffer(&out->v104, ioa, payload, length);
|
||||
case 106: return IEC60870_DelayAcquisitionCommand_Get_From_Buffer(&out->v106, ioa, payload, length);
|
||||
case 110: return IEC60870_ParameterNormalizedValue_Get_From_Buffer(&out->v110, ioa, payload, length);
|
||||
case 111: return IEC60870_ParameterScaledValue_Get_From_Buffer(&out->v111, ioa, payload, length);
|
||||
case 112: return IEC60870_ParameterFloatValue_Get_From_Buffer(&out->v112, ioa, payload, length);
|
||||
case 113: return IEC60870_ParameterActivation_Get_From_Buffer(&out->v113, ioa, payload, length);
|
||||
case 120: return IEC60870_FileReady_Get_From_Buffer(&out->v120, ioa, payload, length);
|
||||
case 121: return IEC60870_SectionReady_Get_From_Buffer(&out->v121, ioa, payload, length);
|
||||
case 122: return IEC60870_FileCallOrSelect_Get_From_Buffer(&out->v122, ioa, payload, length);
|
||||
case 124: return IEC60870_FileACK_Get_From_Buffer(&out->v124, ioa, payload, length);
|
||||
case 123: return IEC60870_FileLastSegmentOrSection_Get_From_Buffer(&out->v123, ioa, payload, length);
|
||||
case 125: return IEC60870_FileSegment_Get_From_Buffer(&out->v125, ioa, payload, length);
|
||||
case 126: return IEC60870_FileDirectory_Get_From_Buffer(&out->v126, ioa, payload, length);
|
||||
case 127: return IEC60870_QueryLog_Get_From_Buffer(&out->v127, ioa, payload, length);
|
||||
default: return IEC60870_ASDU_UNSUPPORTED;
|
||||
}
|
||||
}
|
||||
|
||||
static IEC60870_InformationObject_t *Base(Object *object)
|
||||
{
|
||||
return (IEC60870_InformationObject_t *)object;
|
||||
}
|
||||
|
||||
/* 独立按协议宽度拼接地址,未调用生产工具。 */
|
||||
static void Write_Address(uint8_t *out, uint32_t value, int width)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < width; ++i) { out[i] = (uint8_t)(value >> (8 * i)); }
|
||||
}
|
||||
|
||||
static uint16_t Expected_Frame(const Vector *v, const IEC60870_AppLayerParameters_t *p,
|
||||
int sq, int count, uint32_t first, uint8_t *out)
|
||||
{
|
||||
uint16_t n = (uint16_t)(2 + p->sizeOfCOT + p->sizeOfCA);
|
||||
int i;
|
||||
out[0] = v->type;
|
||||
out[1] = (uint8_t)((sq << 7) | count);
|
||||
out[2] = 0xC3;
|
||||
if (p->sizeOfCOT == 2) { out[3] = 0xA7; }
|
||||
Write_Address(out + 2 + p->sizeOfCOT, p->sizeOfCA == 2 ? 0x1234U : 0x34U, p->sizeOfCA);
|
||||
for (i = 0; i < count; ++i) {
|
||||
if (i == 0 || !sq) {
|
||||
Write_Address(out + n, first + (uint32_t)(sq ? i : i * 7), p->sizeOfIOA);
|
||||
n = (uint16_t)(n + p->sizeOfIOA);
|
||||
}
|
||||
memcpy(out + n, v->payload, v->length);
|
||||
n = (uint16_t)(n + v->length);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static int Reject_Frame(const IEC60870_AppLayerParameters_t *p, const uint8_t *frame, uint16_t length)
|
||||
{
|
||||
IEC60870_AsduInfo_t info;
|
||||
uint8_t saved[sizeof(info)];
|
||||
memset(&info, 0xA5, sizeof(info));
|
||||
memcpy(saved, &info, sizeof(info));
|
||||
CHECK(IEC60870_Asdu_Parse(p, frame, length, &info) == IEC60870_ASDU_ERROR);
|
||||
CHECK(memcmp(saved, &info, sizeof(info)) == 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_all_layouts(void)
|
||||
{
|
||||
int cot, ca, ioa, sq;
|
||||
unsigned int cases = 0;
|
||||
size_t k;
|
||||
for (cot = 1; cot <= 2; ++cot) for (ca = 1; ca <= 2; ++ca) for (ioa = 1; ioa <= 3; ++ioa) {
|
||||
IEC60870_AppLayerParameters_t p = default_parameters;
|
||||
IEC60870_AsduHeader_t header = {3,1,1,0xA7,0};
|
||||
uint32_t first = ioa == 1 ? 0x40U : ioa == 2 ? 0x3440U : 0x123440U;
|
||||
p.sizeOfCOT = cot; p.sizeOfCA = ca; p.sizeOfIOA = ioa; p.maxSizeOfASDU = 254;
|
||||
header.common_address = ca == 1 ? 0x34 : 0x1234;
|
||||
for (k = 0; k < COUNT(vectors); ++k) for (sq = 0; sq <= vectors[k].sequence; ++sq) {
|
||||
const Vector *v = &vectors[k];
|
||||
IEC60870_Asdu_t asdu, saved_asdu;
|
||||
IEC60870_AsduInfo_t info;
|
||||
Object object, parsed, saved_object;
|
||||
uint8_t bytes[260], expected[260], saved_bytes[260], encoded[262];
|
||||
const uint8_t *data;
|
||||
uint16_t length, wire_length, object_length;
|
||||
int count = v->single ? 1 : 2, i;
|
||||
memset(bytes, 0xA5, sizeof(bytes));
|
||||
CHECK(IEC60870_Asdu_Create(&asdu, &p, bytes, sizeof(bytes), &header, sq) == IEC60870_ASDU_OK);
|
||||
for (i = 0; i < count; ++i) {
|
||||
uint32_t address = first + (uint32_t)(sq ? i : i * 7);
|
||||
CHECK(Create_Object(v->type, address, &object) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_InformationObject_Get_Type(Base(&object)) == v->type);
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, Base(&object)) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_InformationObject_Encode(Base(&object), &p, 0, encoded, sizeof(encoded), &object_length) == IEC60870_ASDU_OK);
|
||||
CHECK(object_length == ioa + v->length);
|
||||
CHECK(memcmp(encoded + ioa, v->payload, v->length) == 0);
|
||||
}
|
||||
CHECK(IEC60870_Asdu_Get_Data(&asdu, &data, &length) == IEC60870_ASDU_OK);
|
||||
wire_length = Expected_Frame(v, &p, sq, count, first, expected);
|
||||
CHECK(length == wire_length && memcmp(data, expected, length) == 0);
|
||||
CHECK(bytes[length] == 0xA5);
|
||||
CHECK(IEC60870_Asdu_Parse(&p, expected, length, &info) == IEC60870_ASDU_OK);
|
||||
CHECK(info.object_count == count && info.is_sequence == sq && info.common_address == header.common_address);
|
||||
for (i = 0; i < count; ++i) {
|
||||
CHECK(IEC60870_Asdu_Get_Element(&info, (uint8_t)i, &parsed, sizeof(parsed)) == IEC60870_ASDU_OK);
|
||||
CHECK(Base(&parsed)->ioa == first + (uint32_t)(sq ? i : i * 7));
|
||||
CHECK(IEC60870_InformationObject_Encode(Base(&parsed), &p, 0, encoded, sizeof(encoded), &object_length) == IEC60870_ASDU_OK);
|
||||
CHECK(memcmp(encoded + ioa, v->payload, v->length) == 0);
|
||||
}
|
||||
memset(&parsed, 0xA5, sizeof(parsed)); memcpy(&saved_object, &parsed, sizeof(parsed));
|
||||
CHECK(IEC60870_Asdu_Get_Element(&info, 0, &parsed, 1) == IEC60870_ASDU_ERROR);
|
||||
CHECK(memcmp(&parsed, &saved_object, sizeof(parsed)) == 0);
|
||||
CHECK(Reject_Frame(&p, expected, (uint16_t)(length - 1)));
|
||||
expected[length] = 0;
|
||||
CHECK(Reject_Frame(&p, expected, (uint16_t)(length + 1)));
|
||||
for (i = 0; i < v->length; ++i) {
|
||||
CHECK(Parse_Object(v->type, first, v->payload, (uint16_t)i, &parsed) == IEC60870_ASDU_ERROR);
|
||||
CHECK(memcmp(&parsed, &saved_object, sizeof(parsed)) == 0);
|
||||
}
|
||||
CHECK(Parse_Object(v->type, first, v->payload, v->length, &parsed) == IEC60870_ASDU_OK);
|
||||
/* 原对象存储可复用,加入ASDU后保存的字节必须独立。 */
|
||||
memset(&object, 0, sizeof(object));
|
||||
CHECK(memcmp(data, expected, length) == 0);
|
||||
saved_asdu = asdu; memcpy(saved_bytes, bytes, sizeof(bytes));
|
||||
CHECK(Create_Object(v->type, first, &object) == IEC60870_ASDU_OK);
|
||||
if (v->single) {
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, Base(&object)) == IEC60870_ASDU_FULL);
|
||||
} else if (sq) {
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, Base(&object)) == IEC60870_ASDU_ERROR);
|
||||
} else {
|
||||
asdu.capacity = asdu.length; saved_asdu = asdu;
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, Base(&object)) == IEC60870_ASDU_FULL);
|
||||
}
|
||||
CHECK(memcmp(&asdu, &saved_asdu, sizeof(asdu)) == 0);
|
||||
CHECK(memcmp(bytes, saved_bytes, sizeof(bytes)) == 0);
|
||||
++cases;
|
||||
}
|
||||
}
|
||||
printf("Extended ASDU: %u independent wire layouts verified\n", cases);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sequence_and_counts(void)
|
||||
{
|
||||
size_t k;
|
||||
uint8_t frame[260], buffer[260], saved[260];
|
||||
IEC60870_AsduHeader_t header = {3,1,1,0xA7,0x1234};
|
||||
IEC60870_Asdu_t asdu, before;
|
||||
Object object;
|
||||
for (k = 0; k < COUNT(vectors); ++k) {
|
||||
const Vector *v = &vectors[k];
|
||||
uint16_t n = Expected_Frame(v, &default_parameters, 0, 1, 0x40, frame);
|
||||
frame[1] = 0;
|
||||
CHECK(Reject_Frame(&default_parameters, frame, n));
|
||||
if (!v->sequence) {
|
||||
n = Expected_Frame(v, &default_parameters, 1, 1, 0x40, frame);
|
||||
CHECK(Reject_Frame(&default_parameters, frame, n));
|
||||
memset(buffer, 0xA5, sizeof(buffer));
|
||||
CHECK(IEC60870_Asdu_Create(&asdu, &default_parameters, buffer, sizeof(buffer), &header, 1) == IEC60870_ASDU_OK);
|
||||
before = asdu; memcpy(saved, buffer, sizeof(buffer));
|
||||
CHECK(Create_Object(v->type, 0x40, &object) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, Base(&object)) == IEC60870_ASDU_ERROR);
|
||||
CHECK(memcmp(&asdu, &before, sizeof(asdu)) == 0 && memcmp(buffer, saved, sizeof(buffer)) == 0);
|
||||
}
|
||||
if (v->single) {
|
||||
n = Expected_Frame(v, &default_parameters, 0, 2, 0x40, frame);
|
||||
CHECK(Reject_Frame(&default_parameters, frame, n));
|
||||
}
|
||||
if (v->sequence) {
|
||||
IEC60870_AppLayerParameters_t p = default_parameters;
|
||||
p.sizeOfIOA = 1;
|
||||
n = Expected_Frame(v, &p, 1, 2, 255, frame);
|
||||
CHECK(Reject_Frame(&p, frame, n));
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_invalid_fields(void)
|
||||
{
|
||||
/* 每项给出独立的非法保留位/状态/时间修改,不依赖生产描述表。 */
|
||||
static const struct { uint8_t type, offset, mask; } faults[] = {
|
||||
{2,0,2},{4,0,4},{5,1,2},{6,1,2},{7,4,2},{8,4,2},
|
||||
{10,2,2},{12,2,2},{14,4,2},{16,7,0x40},
|
||||
{17,0,4},{18,0,0x40},{19,0,0x10},{20,4,2},
|
||||
{32,1,2},{33,4,2},{38,0,4},{39,1,4},{40,0,0x10},
|
||||
{104,0,1},{120,5,1},{121,6,1},{125,3,1},
|
||||
{126,11,0x80},{127,14,0x80}
|
||||
};
|
||||
size_t i, k;
|
||||
for (i = 0; i < COUNT(faults); ++i) {
|
||||
Object object, before;
|
||||
uint8_t payload[17], frame[260];
|
||||
for (k = 0; k < COUNT(vectors) && vectors[k].type != faults[i].type; ++k) {}
|
||||
CHECK(k < COUNT(vectors));
|
||||
memcpy(payload, vectors[k].payload, vectors[k].length);
|
||||
payload[faults[i].offset] |= faults[i].mask;
|
||||
memset(&object, 0xA5, sizeof(object)); memcpy(&before, &object, sizeof(object));
|
||||
CHECK(Parse_Object(vectors[k].type, 1, payload, vectors[k].length, &object) == IEC60870_ASDU_ERROR);
|
||||
CHECK(memcmp(&object, &before, sizeof(object)) == 0);
|
||||
{
|
||||
uint16_t n = Expected_Frame(&vectors[k], &default_parameters, 0, 1, 1, frame);
|
||||
memcpy(frame + 9, payload, vectors[k].length);
|
||||
CHECK(Reject_Frame(&default_parameters, frame, n));
|
||||
}
|
||||
}
|
||||
/* 多对象中的第二个非法对象也必须导致整条ASDU解析失败。 */
|
||||
{
|
||||
IEC60870_AsduInfo_t info;
|
||||
uint8_t frame[] = {5,0x82,3,0,1,0,1,0,0,0xFE,0x81,0xFE,0x83};
|
||||
CHECK(Reject_Frame(&default_parameters, frame, sizeof(frame)));
|
||||
frame[12] = 0x81;
|
||||
CHECK(IEC60870_Asdu_Parse(&default_parameters, frame, sizeof(frame), &info) == IEC60870_ASDU_OK);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_short_times(void)
|
||||
{
|
||||
IEC60870_CP16Time2a_t cp16, parsed16 = {0};
|
||||
IEC60870_CP24Time2a_t cp24 = {59999,59,1}, parsed24, before24;
|
||||
uint8_t bytes[4] = {0xA5,0xA5,0xA5,0xA5}, saved[4];
|
||||
uint32_t i;
|
||||
for (i = 0; i <= 65535U; ++i) {
|
||||
cp16.milliseconds = (uint16_t)i;
|
||||
CHECK(IEC60870_Time_To_CP16(&cp16, bytes, 2));
|
||||
CHECK(bytes[0] == (uint8_t)i && bytes[1] == (uint8_t)(i >> 8));
|
||||
CHECK(IEC60870_Time_From_CP16(bytes, 2, &parsed16) && parsed16.milliseconds == i);
|
||||
}
|
||||
CHECK(IEC60870_Time_To_CP24(&cp24, bytes, 3));
|
||||
CHECK(bytes[0] == 0x5F && bytes[1] == 0xEA && bytes[2] == 0xBB && bytes[3] == 0xA5);
|
||||
CHECK(IEC60870_Time_From_CP24(bytes, 3, &parsed24));
|
||||
CHECK(parsed24.milliseconds == 59999 && parsed24.minute == 59 && parsed24.is_invalid == 1);
|
||||
before24 = parsed24;
|
||||
memcpy(saved, bytes, sizeof(bytes));
|
||||
cp24.milliseconds = 60000;
|
||||
CHECK(!IEC60870_Time_To_CP24(&cp24, bytes, sizeof(bytes)));
|
||||
CHECK(memcmp(bytes, saved, sizeof(bytes)) == 0);
|
||||
cp24.milliseconds = 0; cp24.minute = 60;
|
||||
CHECK(!IEC60870_Time_To_CP24(&cp24, bytes, sizeof(bytes)));
|
||||
cp24.minute = 0; cp24.is_invalid = 2;
|
||||
CHECK(!IEC60870_Time_To_CP24(&cp24, bytes, sizeof(bytes)));
|
||||
bytes[2] |= 0x40;
|
||||
CHECK(!IEC60870_Time_From_CP24(bytes, 3, &parsed24));
|
||||
CHECK(memcmp(&parsed24, &before24, sizeof(parsed24)) == 0);
|
||||
bytes[0] = 0x60; bytes[1] = 0xEA; bytes[2] = 0;
|
||||
CHECK(!IEC60870_Time_From_CP24(bytes, 3, &parsed24));
|
||||
bytes[0] = 0; bytes[1] = 0; bytes[2] = 60;
|
||||
CHECK(!IEC60870_Time_From_CP24(bytes, 3, &parsed24));
|
||||
CHECK(!IEC60870_Time_From_CP24(bytes, 2, &parsed24));
|
||||
CHECK(!IEC60870_Time_From_CP24(NULL, 3, &parsed24));
|
||||
CHECK(!IEC60870_Time_From_CP16(bytes, 1, &parsed16));
|
||||
CHECK(!IEC60870_Time_To_CP16(&cp16, bytes, 1));
|
||||
CHECK(!IEC60870_Time_To_CP24(NULL, bytes, 3));
|
||||
cp24.milliseconds = 0; cp24.minute = 0; cp24.is_invalid = 0;
|
||||
CHECK(IEC60870_Time_To_CP24(&cp24, bytes, 3));
|
||||
CHECK(bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_constructor_atomicity(void)
|
||||
{
|
||||
Object object, before;
|
||||
size_t i;
|
||||
uint8_t encoded[262], saved_bytes[262];
|
||||
uint16_t length = 0xA55A;
|
||||
IEC60870_CP24Time2a_t bad_time = {60000,0,0};
|
||||
memset(&object, 0xA5, sizeof(object)); memcpy(&before, &object, sizeof(object));
|
||||
for (i = 0; i < COUNT(vectors); ++i) {
|
||||
CHECK(Create_Object(vectors[i].type, UINT32_C(0x1000000), &object) == IEC60870_ASDU_ERROR);
|
||||
CHECK(memcmp(&object, &before, sizeof(object)) == 0);
|
||||
CHECK(Parse_Object(vectors[i].type, 1, NULL, vectors[i].length, &object) == IEC60870_ASDU_ERROR);
|
||||
CHECK(memcmp(&object, &before, sizeof(object)) == 0);
|
||||
}
|
||||
#define REJECT_CREATE(call) do { CHECK((call) == IEC60870_ASDU_ERROR); CHECK(memcmp(&object, &before, sizeof(object)) == 0); } while (0)
|
||||
REJECT_CREATE(IEC60870_StepPositionInformation_Create(&object.v5, 1, -65, 0, 0));
|
||||
REJECT_CREATE(IEC60870_StepPositionInformation_Create(&object.v5, 1, 64, 0, 0));
|
||||
REJECT_CREATE(IEC60870_StepPositionInformation_Create(&object.v5, 1, 0, 2, 0));
|
||||
REJECT_CREATE(IEC60870_BitString32_Create(&object.v7, 1, 0, 2));
|
||||
REJECT_CREATE(IEC60870_SinglePointWithCP24Time2a_Create(&object.v2, 1, 1, 0, &bad_time));
|
||||
REJECT_CREATE(IEC60870_SinglePointWithCP24Time2a_Create(&object.v2, 1, 1, 0, NULL));
|
||||
REJECT_CREATE(IEC60870_EventOfProtectionEquipment_Create(&object.v17, 1, 4, 0, &elapsed, &short_time));
|
||||
REJECT_CREATE(IEC60870_EventOfProtectionEquipment_Create(&object.v17, 1, 1, 4, &elapsed, &short_time));
|
||||
REJECT_CREATE(IEC60870_PackedStartEventsOfProtectionEquipment_Create(&object.v18, 1, 64, 0, &elapsed, &short_time));
|
||||
REJECT_CREATE(IEC60870_PackedOutputCircuitInfo_Create(&object.v19, 1, 16, 0, &elapsed, &short_time));
|
||||
REJECT_CREATE(IEC60870_EndOfInitialization_Create(&object.v70, 1, 256));
|
||||
REJECT_CREATE(IEC60870_DelayAcquisitionCommand_Create(&object.v106, 1, NULL));
|
||||
REJECT_CREATE(IEC60870_ParameterNormalizedValue_Create(&object.v110, 1, 0, -1));
|
||||
REJECT_CREATE(IEC60870_FileReady_Create(&object.v120, 1, 1, UINT32_C(0x1000000), 0));
|
||||
REJECT_CREATE(IEC60870_SectionReady_Create(&object.v121, 1, 1, 1, 0, 1));
|
||||
REJECT_CREATE(IEC60870_FileSegment_Create(&object.v125, 1, 1, 1, NULL, 1));
|
||||
REJECT_CREATE(IEC60870_FileSegment_Create(&object.v125, 1, 1, 1, segment_data, 256));
|
||||
REJECT_CREATE(IEC60870_FileDirectory_Create(&object.v126, 1, 1, 0, 0, NULL));
|
||||
REJECT_CREATE(IEC60870_QueryLog_Create(&object.v127, 1, 1, &long_time, NULL));
|
||||
#undef REJECT_CREATE
|
||||
CHECK(Create_Object(5, 1, &object) == IEC60870_ASDU_OK);
|
||||
object.v5.value = 100;
|
||||
memset(encoded, 0xA5, sizeof(encoded)); memcpy(saved_bytes, encoded, sizeof(encoded));
|
||||
CHECK(IEC60870_InformationObject_Encode(Base(&object), &default_parameters, 0, encoded, sizeof(encoded), &length) == IEC60870_ASDU_ERROR);
|
||||
CHECK(memcmp(encoded, saved_bytes, sizeof(encoded)) == 0 && length == 0xA55A);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_file_segments(void)
|
||||
{
|
||||
int cot, ca, ioa, max;
|
||||
uint8_t input[255], bytes[270], before_bytes[270], encoded[262];
|
||||
IEC60870_AsduHeader_t header = {13,0,0,0,1};
|
||||
unsigned int i;
|
||||
CHECK(IEC60870_FileSegment_Get_Max_Data_Size(&default_parameters) == 236);
|
||||
CHECK(IEC60870_FileSegment_Get_Max_Data_Size(NULL) == -1);
|
||||
for (i = 0; i < sizeof(input); ++i) { input[i] = (uint8_t)(i ^ 0x5A); }
|
||||
for (cot = 1; cot <= 2; ++cot) for (ca = 1; ca <= 2; ++ca) for (ioa = 1; ioa <= 3; ++ioa) {
|
||||
IEC60870_AppLayerParameters_t p = default_parameters;
|
||||
IEC60870_FileSegment_t segment, parsed;
|
||||
IEC60870_Asdu_t asdu, before;
|
||||
IEC60870_AsduInfo_t info;
|
||||
const uint8_t *wire;
|
||||
uint16_t n, object_length;
|
||||
int h = 2 + cot + ca;
|
||||
p.sizeOfCOT = cot; p.sizeOfCA = ca; p.sizeOfIOA = ioa; p.maxSizeOfASDU = 254;
|
||||
max = IEC60870_FileSegment_Get_Max_Data_Size(&p);
|
||||
CHECK(max == 254 - h - ioa - 4);
|
||||
CHECK(IEC60870_FileSegment_Create(&segment, 1, 0x1234, 7, input, (uint16_t)max) == IEC60870_ASDU_OK);
|
||||
memset(bytes, 0xA5, sizeof(bytes));
|
||||
CHECK(IEC60870_Asdu_Create(&asdu, &p, bytes, sizeof(bytes), &header, 0) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, &segment.object) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Get_Data(&asdu, &wire, &n) == IEC60870_ASDU_OK && n == 254);
|
||||
CHECK(wire[h+ioa] == 0x34 && wire[h+ioa+1] == 0x12 && wire[h+ioa+2] == 7 && wire[h+ioa+3] == max);
|
||||
CHECK(memcmp(wire+h+ioa+4, input, (size_t)max) == 0 && bytes[254] == 0xA5);
|
||||
CHECK(IEC60870_Asdu_Parse(&p, wire, n, &info) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Get_Element(&info, 0, &parsed, sizeof(parsed)) == IEC60870_ASDU_OK);
|
||||
CHECK(parsed.length == max && memcmp(parsed.data, input, (size_t)max) == 0);
|
||||
memset(bytes+h+ioa+4, 0, (size_t)max);
|
||||
CHECK(memcmp(parsed.data, input, (size_t)max) == 0); /* 解析结果拥有独立数据。 */
|
||||
CHECK(IEC60870_FileSegment_Create(&segment, 1, 1, 1, input, (uint16_t)(max+1)) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Create(&asdu, &p, bytes, sizeof(bytes), &header, 0) == IEC60870_ASDU_OK);
|
||||
before = asdu; memcpy(before_bytes, bytes, sizeof(bytes));
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, &segment.object) == IEC60870_ASDU_FULL);
|
||||
CHECK(memcmp(&asdu, &before, sizeof(asdu)) == 0 && memcmp(bytes, before_bytes, sizeof(bytes)) == 0);
|
||||
CHECK(IEC60870_FileSegment_Create(&segment, 1, 1, 1, input, 255) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_InformationObject_Encode(&segment.object, &p, 0, encoded, sizeof(encoded), &object_length) == IEC60870_ASDU_OK);
|
||||
CHECK(object_length == ioa+259 && encoded[ioa+3] == 255);
|
||||
CHECK(IEC60870_FileSegment_Get_From_Buffer(&parsed, 1, encoded+ioa, 259) == IEC60870_ASDU_OK);
|
||||
CHECK(parsed.length == 255 && memcmp(parsed.data, input, sizeof(input)) == 0);
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, &segment.object) == IEC60870_ASDU_FULL);
|
||||
CHECK(IEC60870_FileSegment_Create(&segment, 1, 1, 1, NULL, 0) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, &segment.object) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Get_Data(&asdu, &wire, &n) == IEC60870_ASDU_OK && n == h+ioa+4);
|
||||
CHECK(IEC60870_Asdu_Parse(&p, wire, n, &info) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Get_Element(&info, 0, &parsed, sizeof(parsed)) == IEC60870_ASDU_OK && parsed.length == 0);
|
||||
p.maxSizeOfASDU = h+ioa+4;
|
||||
CHECK(IEC60870_FileSegment_Get_Max_Data_Size(&p) == 0);
|
||||
p.maxSizeOfASDU--;
|
||||
CHECK(IEC60870_FileSegment_Get_Max_Data_Size(&p) == -1);
|
||||
}
|
||||
/* 默认104配置:236字节恰好249;237字节必须返回FULL。 */
|
||||
{
|
||||
IEC60870_FileSegment_t segment;
|
||||
IEC60870_Asdu_t asdu;
|
||||
const uint8_t *wire;
|
||||
uint16_t n;
|
||||
CHECK(IEC60870_Asdu_Create(&asdu, &default_parameters, bytes, sizeof(bytes), &header, 0) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_FileSegment_Create(&segment, 1, 1, 1, input, 237) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, &segment.object) == IEC60870_ASDU_FULL);
|
||||
CHECK(IEC60870_FileSegment_Create(&segment, 1, 1, 1, input, 236) == IEC60870_ASDU_OK);
|
||||
memset(input, 0, sizeof(input));
|
||||
CHECK(segment.data[0] == 0x5A); /* 构造结果也不借用源数据。 */
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, &segment.object) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Get_Data(&asdu, &wire, &n) == IEC60870_ASDU_OK && n == 249);
|
||||
bytes[12]++; /* LOS称有237字节,但原文只有236字节。 */
|
||||
CHECK(Reject_Frame(&default_parameters, bytes, n));
|
||||
CHECK(IEC60870_Asdu_Get_Data(&asdu, &wire, &n) == IEC60870_ASDU_ERROR);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_numeric_and_parameter_fields(void)
|
||||
{
|
||||
IEC60870_StepPositionInformation_t step;
|
||||
IEC60870_ParameterNormalizedValue_t parameter;
|
||||
IEC60870_ParameterFloatValue_t floating;
|
||||
IEC60870_PackedSinglePointWithSCD_t packed;
|
||||
IEC60870_FileReady_t file;
|
||||
IEC60870_Asdu_t asdu;
|
||||
IEC60870_AsduInfo_t info;
|
||||
IEC60870_AsduHeader_t header = {6,0,0,0,1};
|
||||
uint8_t bytes[254], payload[16], raw_float[] = {0x23,0x01,0xC0,0x7F,0xFF};
|
||||
uint16_t n;
|
||||
const uint8_t *wire;
|
||||
int v;
|
||||
for (v = -64; v <= 63; ++v) {
|
||||
CHECK(IEC60870_StepPositionInformation_Create(&step, 1, v, 0, 0) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_InformationObject_Encode(&step.object, &default_parameters, 1, payload, sizeof(payload), &n) == IEC60870_ASDU_OK);
|
||||
CHECK(n == 2 && payload[0] == ((unsigned int)v & 0x7FU) && payload[1] == 0);
|
||||
CHECK(IEC60870_StepPositionInformation_Get_From_Buffer(&step, 1, payload, 2) == IEC60870_ASDU_OK && step.value == v);
|
||||
}
|
||||
CHECK(IEC60870_PackedSinglePointWithSCD_Get_From_Buffer(&packed, 1, (const uint8_t[]){0x34,0x12,0xCD,0xAB,0x81}, 5) == IEC60870_ASDU_OK);
|
||||
CHECK(packed.status == 0x1234 && packed.change == 0xABCD && packed.quality == 0x81);
|
||||
CHECK(IEC60870_FileReady_Create(&file, 1, 1, UINT32_C(0xFFFFFF), 0) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_InformationObject_Encode(&file.object, &default_parameters, 0, payload, sizeof(payload), &n) == IEC60870_ASDU_OK);
|
||||
CHECK(payload[5] == 255 && payload[6] == 255 && payload[7] == 255);
|
||||
CHECK(IEC60870_ParameterFloatValue_Get_From_Buffer(&floating, 1, raw_float, sizeof(raw_float)) == IEC60870_ASDU_OK);
|
||||
CHECK(floating.qualifier == 255);
|
||||
CHECK(IEC60870_InformationObject_Encode(&floating.object, &default_parameters, 0, payload, sizeof(payload), &n) == IEC60870_ASDU_OK);
|
||||
CHECK(memcmp(payload+3, raw_float, sizeof(raw_float)) == 0); /* NaN位模式与QPM全部保留。 */
|
||||
CHECK(IEC60870_Asdu_Create(&asdu, &default_parameters, bytes, sizeof(bytes), &header, 0) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_ParameterNormalizedValue_Create(¶meter, 1, INT16_MIN, 0xFF) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, ¶meter.object) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_ParameterNormalizedValue_Create(¶meter, 999, INT16_MAX, 0) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Add_InformationObject(&asdu, ¶meter.object) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Get_Data(&asdu, &wire, &n) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC60870_Asdu_Parse(&default_parameters, wire, n, &info) == IEC60870_ASDU_OK && info.object_count == 2);
|
||||
CHECK(IEC60870_Asdu_Get_Element(&info, 1, ¶meter, sizeof(parameter)) == IEC60870_ASDU_OK);
|
||||
CHECK(parameter.object.ioa == 999 && parameter.value == INT16_MAX && parameter.qualifier == 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int passed = 0;
|
||||
#define RUN(test) do { if (!(test)()) { return 1; } ++passed; } while (0)
|
||||
RUN(test_all_layouts);
|
||||
RUN(test_sequence_and_counts);
|
||||
RUN(test_invalid_fields);
|
||||
RUN(test_short_times);
|
||||
RUN(test_constructor_atomicity);
|
||||
RUN(test_file_segments);
|
||||
RUN(test_numeric_and_parameter_fields);
|
||||
printf("Extended ASDU: %d groups passed (37 additional types, short times, variable file segments)\n", passed);
|
||||
return 0;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,242 @@
|
||||
/* 独立主站字节测试:启动示例自己的Windows Net后端,仅连接127.0.0.1随机端口。
|
||||
* Node内置模块即可运行,不安装包、不复用待测ASDU编码器,不更改系统时钟。
|
||||
*/
|
||||
'use strict';
|
||||
const assert = require('node:assert/strict');
|
||||
const net = require('node:net');
|
||||
const { spawn, spawnSync } = require('node:child_process');
|
||||
const { once } = require('node:events');
|
||||
const { setTimeout: delay } = require('node:timers/promises');
|
||||
const executable = process.argv[2];
|
||||
if (!executable) throw new Error('Usage: node slave_example_loopback_test.cjs <example.exe>');
|
||||
|
||||
class Master {
|
||||
constructor(socket) {
|
||||
this.socket = socket;
|
||||
this.buffer = Buffer.alloc(0);
|
||||
this.frames = [];
|
||||
this.waiters = [];
|
||||
this.ns = this.nr = 0;
|
||||
socket.on('data', data => {
|
||||
this.buffer = Buffer.concat([this.buffer, data]);
|
||||
try {
|
||||
while (this.buffer.length >= 2) {
|
||||
assert.equal(this.buffer[0], 0x68);
|
||||
assert(this.buffer[1] >= 4 && this.buffer[1] <= 253);
|
||||
const length = this.buffer[1] + 2;
|
||||
if (this.buffer.length < length) break;
|
||||
const frame = this.buffer.subarray(0, length);
|
||||
this.buffer = this.buffer.subarray(length);
|
||||
const waiter = this.waiters.shift();
|
||||
if (waiter) { clearTimeout(waiter.timer); waiter.resolve(frame); }
|
||||
else this.frames.push(frame);
|
||||
}
|
||||
} catch (error) { this.fail(error); socket.destroy(); }
|
||||
});
|
||||
socket.on('error', error => this.fail(error));
|
||||
socket.on('close', () => this.fail(new Error('Connection closed')));
|
||||
}
|
||||
fail(error) {
|
||||
this.error = error;
|
||||
for (const waiter of this.waiters.splice(0)) {
|
||||
clearTimeout(waiter.timer);
|
||||
waiter.reject(error);
|
||||
}
|
||||
}
|
||||
next() {
|
||||
if (this.frames.length) return Promise.resolve(this.frames.shift());
|
||||
if (this.error) return Promise.reject(this.error);
|
||||
return new Promise((resolve, reject) => {
|
||||
const waiter = { resolve, reject };
|
||||
waiter.timer = setTimeout(() => {
|
||||
this.waiters.splice(this.waiters.indexOf(waiter), 1);
|
||||
reject(new Error('Timed out waiting for an APDU'));
|
||||
}, 1500);
|
||||
this.waiters.push(waiter);
|
||||
});
|
||||
}
|
||||
async control(code, expected) {
|
||||
this.socket.write(Buffer.from([0x68, 4, code, 0, 0, 0]));
|
||||
for (let i = 0; i < 8; i++) {
|
||||
const frame = await this.next();
|
||||
if (frame[2] === 1) continue; // 主站I帧的S确认允许穿插。
|
||||
assert.deepEqual([...frame], [0x68, 4, expected, 0, 0, 0]);
|
||||
return;
|
||||
}
|
||||
throw new Error('Missing U-frame confirmation');
|
||||
}
|
||||
frame(payload) {
|
||||
const frame = Buffer.alloc(payload.length + 6);
|
||||
frame[0] = 0x68;
|
||||
frame[1] = payload.length + 4;
|
||||
frame.writeUInt16LE(this.ns++ << 1, 2);
|
||||
frame.writeUInt16LE(this.nr << 1, 4);
|
||||
Buffer.from(payload).copy(frame, 6);
|
||||
return frame;
|
||||
}
|
||||
async send(payload, fragmented = false) {
|
||||
const frame = this.frame(payload);
|
||||
if (fragmented) {
|
||||
this.socket.write(frame.subarray(0, 3));
|
||||
await delay(10);
|
||||
this.socket.write(frame.subarray(3));
|
||||
} else this.socket.write(frame);
|
||||
}
|
||||
async readASDU() {
|
||||
for (let i = 0; i < 8; i++) {
|
||||
const frame = await this.next();
|
||||
if (frame[2] === 1) continue;
|
||||
assert.equal(frame[2] & 1, 0);
|
||||
assert.equal(frame.readUInt16LE(2) >> 1, this.nr++);
|
||||
assert.equal(frame.readUInt16LE(4) >> 1, this.ns);
|
||||
const ack = Buffer.from([0x68, 4, 1, 0, 0, 0]);
|
||||
ack.writeUInt16LE(this.nr << 1, 4);
|
||||
this.socket.write(ack); // k=1,每帧ACK后服务才可继续发下一帧。
|
||||
return frame.subarray(6);
|
||||
}
|
||||
throw new Error('Missing I frame');
|
||||
}
|
||||
async close() {
|
||||
const closed = once(this.socket, 'close');
|
||||
this.socket.end();
|
||||
await closed;
|
||||
}
|
||||
}
|
||||
|
||||
const signals = [0, 1, 0, 1, 1, 0, 1, 0];
|
||||
const measures = [2300, 2310, 2290, 5000, -125, 0, 1234, 42];
|
||||
const request = (oa, qoi = 20, ca = 7) => [100, 1, 0x86, oa, ca & 0xff, ca >> 8, 0, 0, 0, qoi];
|
||||
async function totalCall(master, oa, fragmented = false, qoi = 20, ca = 7) {
|
||||
await master.send(request(oa, qoi, ca), fragmented);
|
||||
await readTotalCall(master, oa, qoi);
|
||||
}
|
||||
|
||||
async function readTotalCall(master, oa, qoi = 20, busyRequests = []) {
|
||||
const original = request(oa, qoi);
|
||||
const count = qoi === 20 ? 6 : 4;
|
||||
const busyReplies = busyRequests.map(payload => {
|
||||
const expected = [...payload]; expected[2] = 0xc7;
|
||||
if (expected[4] === 0xff && expected[5] === 0xff) { expected[4] = 7; expected[5] = 0; }
|
||||
return Buffer.from(expected);
|
||||
});
|
||||
for (let f = 0; f < count || busyReplies.length;) {
|
||||
const asdu = await master.readASDU();
|
||||
if ((asdu[2] & 0x40) !== 0) {
|
||||
const index = busyReplies.findIndex(expected => expected.equals(asdu));
|
||||
assert(index >= 0, `Unexpected negative reply: ${asdu.toString('hex')}`);
|
||||
busyReplies.splice(index, 1);
|
||||
continue;
|
||||
}
|
||||
assert(f < count, 'Unexpected response after ACT_TERM');
|
||||
if (f === 0 || f === count - 1) {
|
||||
const expected = [...original];
|
||||
expected[2] = f === 0 ? 0x87 : 0x8a;
|
||||
assert.deepEqual([...asdu], expected);
|
||||
} else {
|
||||
const digital = qoi === 20 ? f < 3 : qoi === 21;
|
||||
const width = digital ? 4 : 6;
|
||||
const first = (qoi === 20 && !digital ? f - 3 : f - 1) * 4;
|
||||
assert.equal(asdu.length, 6 + width * 4);
|
||||
assert.deepEqual([...asdu.subarray(0, 6)], [digital ? 1 : 11, 4, 0x80 | qoi, oa, 7, 0]);
|
||||
for (let p = 0; p < 4; p++) {
|
||||
const offset = 6 + p * width;
|
||||
assert.equal(asdu.readUIntLE(offset, 3), (digital ? 1001 : 2001) + first + p);
|
||||
if (digital) assert.equal(asdu[offset + 3], signals[first + p]);
|
||||
else {
|
||||
assert.equal(asdu.readInt16LE(offset + 3), measures[first + p]);
|
||||
assert.equal(asdu[offset + 5], 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
f++;
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const help = spawnSync(executable, ['--help'], { windowsHide: true, encoding: 'utf8' });
|
||||
assert.ifError(help.error);
|
||||
assert.equal(help.status, 0, help.stderr);
|
||||
assert(help.stdout.includes('--run-ms'));
|
||||
const invalid = spawnSync(executable, ['--ca', '0'], { windowsHide: true, encoding: 'utf8' });
|
||||
assert.ifError(invalid.error);
|
||||
assert.equal(invalid.status, 1);
|
||||
const child = spawn(executable, ['--port', '0', '--ca', '7', '--window', '1', '--run-ms', '4000'],
|
||||
{ windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
||||
let output = '', errors = '';
|
||||
const sockets = [];
|
||||
child.stdout.on('data', data => { output += data.toString(); });
|
||||
child.stderr.on('data', data => { errors += data.toString(); });
|
||||
const exit = once(child, 'exit');
|
||||
const deadline = setTimeout(() => { child.kill(); }, 9000);
|
||||
try {
|
||||
const port = await new Promise((resolve, reject) => {
|
||||
const timer = setTimeout(() => reject(new Error(`No listening port: ${output} ${errors}`)), 2000);
|
||||
child.once('error', error => { clearTimeout(timer); reject(error); });
|
||||
child.stdout.on('data', () => {
|
||||
const match = output.match(/LISTEN 127\.0\.0\.1:(\d+) CA=7/);
|
||||
if (match) { clearTimeout(timer); resolve(Number(match[1])); }
|
||||
});
|
||||
});
|
||||
async function connect() {
|
||||
const socket = net.connect({ host: '127.0.0.1', port });
|
||||
sockets.push(socket);
|
||||
const master = new Master(socket);
|
||||
await once(socket, 'connect');
|
||||
await master.control(7, 0x0b);
|
||||
return master;
|
||||
}
|
||||
const master = await connect();
|
||||
await totalCall(master, 0x5a, true);
|
||||
await totalCall(master, 0x5b, false, 21);
|
||||
await totalCall(master, 0x5c, false, 22);
|
||||
// 广播请求的确认、数据和终止均按实际本站CA=7核对。
|
||||
await totalCall(master, 0x5d, true, 20, 65535);
|
||||
await totalCall(master, 0x5e, false, 21, 65535);
|
||||
await totalCall(master, 0x5f, false, 22, 65535);
|
||||
// 连续提交不同QOI:只执行第一个,后两个否定;结束后由主站重发才执行。
|
||||
const burst = [[0x31, 22, 65535], [0x32, 20, 65535], [0x33, 21, 7]];
|
||||
master.socket.write(Buffer.concat(burst.map(([oa, qoi, ca]) => master.frame(request(oa, qoi, ca)))));
|
||||
await readTotalCall(master, 0x31, 22, burst.slice(1).map(([oa, qoi, ca]) => request(oa, qoi, ca)));
|
||||
for (const [oa, qoi] of burst.slice(1)) await totalCall(master, oa, false, qoi);
|
||||
for (const [index, value, cot] of [[4, 8, 46], [9, 37, 7], [9, 36, 7], [0, 200, 44]]) {
|
||||
const invalidRequest = request(0x55);
|
||||
invalidRequest[index] = value;
|
||||
if (index === 9) { invalidRequest[4] = invalidRequest[5] = 0xff; }
|
||||
await master.send(invalidRequest);
|
||||
const expected = [...invalidRequest];
|
||||
expected[2] = 0xc0 | cot;
|
||||
if (index === 9) { expected[4] = 7; expected[5] = 0; }
|
||||
assert.deepEqual([...(await master.readASDU())], expected);
|
||||
}
|
||||
// 同一接收批次的激活/撤销在驱动业务前按FIFO处理;随后重复撤销应否定确认。
|
||||
const activation = request(0x4a, 21, 65535);
|
||||
const deactivation = request(0x4a, 21); deactivation[2] = 0x88;
|
||||
master.socket.write(Buffer.concat([master.frame(activation), master.frame(deactivation)]));
|
||||
const deactCon = [...deactivation]; deactCon[2] = 0x89;
|
||||
assert.deepEqual([...(await master.readASDU())], deactCon);
|
||||
deactivation[4] = deactivation[5] = 0xff;
|
||||
await master.send(deactivation);
|
||||
deactCon[2] = 0xc9;
|
||||
assert.deepEqual([...(await master.readASDU())], deactCon);
|
||||
await master.control(0x13, 0x23);
|
||||
await master.control(7, 0x0b);
|
||||
await totalCall(master, 0x6b);
|
||||
await master.close();
|
||||
const next = await connect();
|
||||
await totalCall(next, 0x7c);
|
||||
await next.close();
|
||||
const [code] = await exit;
|
||||
assert.equal(code, 0, `${output}\n${errors}`);
|
||||
assert.match(output, /STOP submitted=62 gi_submitted=11 /);
|
||||
assert.match(output, /deactivated=1 timeouts=0/);
|
||||
assert.match(output, /negative_submitted=\d+ dropped_requests=0/);
|
||||
assert.equal(errors, '');
|
||||
console.log('Slave example loopback: passed (real TCP, station/groups and broadcast CA, busy rejection and retry, broadcast/directed deactivation, fragmented input, k=1 ACK, errors, STOP/START, reconnect, cleanup)');
|
||||
} finally {
|
||||
clearTimeout(deadline);
|
||||
for (const socket of sockets) socket.destroy();
|
||||
if (child.exitCode === null) child.kill();
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(error => { console.error(error); process.exitCode = 1; });
|
||||
Reference in New Issue
Block a user