补充总召唤的广播地址,电度召唤+时钟同步业务的实现
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
/* 召唤种类规则单测:直接调用无状态规则,验证限定词解析、数据COT和允许的数据类型。
|
||||
* 使用独立请求字节,不经过网络,也不执行应用的冻结或复位动作。 */
|
||||
#include "../104/business/IEC104_Interrogation_Internal.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define CHECK(x) do { if (!(x)) { fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #x); exit(1); } } while (0)
|
||||
|
||||
/* QCC同时包含RQT和FRZ;用非零IOA确认解析保留原值,业务合法性由后续校验决定。 */
|
||||
static void test_decode(void)
|
||||
{
|
||||
static const uint8_t qcc[] = {0x05, 0x45, 0x85, 0xc5, 0x01, 0x3f};
|
||||
IEC60870_AppLayerParameters_t parameters = {1, 1, 2, 0, 2, 3, 249};
|
||||
unsigned i;
|
||||
for (i = 0; i < sizeof(qcc); ++i) {
|
||||
uint8_t bytes[] = {101, 1, 6, 0x5a, 1, 0, 0x34, 0x12, 0, 0};
|
||||
IEC60870_AsduInfo_t info;
|
||||
uint32_t ioa = 0;
|
||||
uint8_t qualifier = 0, freeze = 0;
|
||||
bytes[9] = qcc[i];
|
||||
CHECK(IEC60870_Asdu_Parse(¶meters, bytes, sizeof(bytes), &info) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC104_COUNTER_INTERROGATION_RULES.decode(&info, &ioa, &qualifier, &freeze));
|
||||
CHECK(ioa == 0x1234 && qualifier == (qcc[i] & 0x3f) && freeze == (qcc[i] >> 6));
|
||||
}
|
||||
{
|
||||
uint8_t bytes[] = {100, 1, 6, 0, 1, 0, 0, 0, 0, 36};
|
||||
IEC60870_AsduInfo_t info;
|
||||
uint32_t ioa = 1;
|
||||
uint8_t qualifier = 0, freeze = 3;
|
||||
CHECK(IEC60870_Asdu_Parse(¶meters, bytes, sizeof(bytes), &info) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC104_GENERAL_INTERROGATION_RULES.decode(&info, &ioa, &qualifier, &freeze));
|
||||
CHECK(ioa == 0 && qualifier == 36 && freeze == 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* 遍历完整字节范围验证限定词边界,并检查总请求及四个分组的数据COT映射。 */
|
||||
static void test_qualifiers(void)
|
||||
{
|
||||
unsigned q;
|
||||
CHECK(IEC104_GENERAL_INTERROGATION_RULES.type_id == 100);
|
||||
CHECK(IEC104_GENERAL_INTERROGATION_RULES.has_deactivation);
|
||||
CHECK(IEC104_COUNTER_INTERROGATION_RULES.type_id == 101);
|
||||
CHECK(!IEC104_COUNTER_INTERROGATION_RULES.has_deactivation);
|
||||
for (q = 0; q < 256; ++q) {
|
||||
CHECK(IEC104_GENERAL_INTERROGATION_RULES.qualifier_valid((uint8_t)q) == (q >= 20 && q <= 36));
|
||||
CHECK(IEC104_COUNTER_INTERROGATION_RULES.qualifier_valid((uint8_t)q) == (q >= 1 && q <= 5));
|
||||
}
|
||||
for (q = 20; q <= 36; ++q) { CHECK(IEC104_GENERAL_INTERROGATION_RULES.data_cause((uint8_t)q) == q); }
|
||||
CHECK(IEC104_COUNTER_INTERROGATION_RULES.data_cause(1) == 38);
|
||||
CHECK(IEC104_COUNTER_INTERROGATION_RULES.data_cause(2) == 39);
|
||||
CHECK(IEC104_COUNTER_INTERROGATION_RULES.data_cause(3) == 40);
|
||||
CHECK(IEC104_COUNTER_INTERROGATION_RULES.data_cause(4) == 41);
|
||||
CHECK(IEC104_COUNTER_INTERROGATION_RULES.data_cause(5) == 37);
|
||||
}
|
||||
|
||||
/* 交叉验证Type/SQ/FRZ:计数量只在读取时允许Type15或非连续寻址的Type37。
|
||||
* 总召保持原九种监视类型,不能因共用引擎而接受累计量或命令类型。 */
|
||||
static void test_types(void)
|
||||
{
|
||||
static const uint8_t types[] = {1, 3, 5, 7, 9, 11, 13, 15, 16, 20, 21, 37, 100, 101};
|
||||
unsigned i, sq, freeze;
|
||||
for (i = 0; i < sizeof(types); ++i) {
|
||||
for (sq = 0; sq < 2; ++sq) {
|
||||
for (freeze = 0; freeze < 4; ++freeze) {
|
||||
IEC60870_AsduInfo_t info;
|
||||
bool expected = freeze == 0 && (types[i] == 15 || (types[i] == 37 && sq == 0));
|
||||
memset(&info, 0, sizeof(info)); info.type_id = types[i]; info.is_sequence = (uint8_t)sq;
|
||||
CHECK(IEC104_COUNTER_INTERROGATION_RULES.data_allowed(&info, (uint8_t)freeze) == expected);
|
||||
if (freeze == 0) {
|
||||
bool general = types[i] == 1 || types[i] == 3 || types[i] == 5 || types[i] == 7 ||
|
||||
types[i] == 9 || types[i] == 11 || types[i] == 13 || types[i] == 20 || types[i] == 21;
|
||||
CHECK(IEC104_GENERAL_INTERROGATION_RULES.data_allowed(&info, 0) == general);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_decode(); test_qualifiers(); test_types();
|
||||
printf("interrogation rules: 3 groups passed; request=%zu event=%zu station=%zu\n",
|
||||
sizeof(IEC104_InterrogationRequest_t), sizeof(IEC104_InterrogationEvent_t),
|
||||
sizeof(IEC104_InterrogationStationState_t));
|
||||
return 0;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -193,7 +193,7 @@ static unsigned service_drain(void)
|
||||
return count;
|
||||
}
|
||||
|
||||
static void service_open(void)
|
||||
static void service_open_with_window(uint16_t window)
|
||||
{
|
||||
IEC104_SlaveConfig_t config;
|
||||
memset(&fake, 0, sizeof(fake));
|
||||
@@ -202,10 +202,12 @@ static void service_open(void)
|
||||
CHECK(service != NULL);
|
||||
locate_test_server();
|
||||
IEC104_Slave_Config_Default(&config);
|
||||
config.protocol.link.k = IEC104_TCP_RECORD_CAPACITY;
|
||||
config.protocol.link.k = window;
|
||||
CHECK(IEC104_Slave_Init(service, &config, NULL) == IEC104_TCP_OK);
|
||||
}
|
||||
|
||||
static void service_open(void) { service_open_with_window(IEC104_TCP_RECORD_CAPACITY); }
|
||||
|
||||
static void service_connect(void)
|
||||
{
|
||||
fake.accept_pending = 1;
|
||||
@@ -370,6 +372,115 @@ static void test_invalid_inputs(void)
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* 原事件可以立即复用;普通/不排队提交只依赖独立身份及本次ASDU,接手后不借用输入。 */
|
||||
static void test_identity_response_storage(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t event;
|
||||
IEC104_SlaveEventIdentity_t identity;
|
||||
IEC60870_Asdu_t object;
|
||||
uint8_t bytes[sizeof(interrogation)];
|
||||
size_t sent;
|
||||
service_begin(); receive_request(&event, 0);
|
||||
CHECK(IEC104_Slave_Get_Event_Identity(service, &event, &identity));
|
||||
memcpy(bytes, interrogation, sizeof(bytes));
|
||||
CHECK(IEC60870_Asdu_Create_From_Buffer(&object, &slave_parameters, bytes, sizeof(bytes)) == IEC60870_ASDU_OK);
|
||||
memset(&event, 0xCC, sizeof(event));
|
||||
CHECK(IEC60870_Asdu_Set_COT(&object, 7) == IEC60870_ASDU_OK);
|
||||
sent = fake.sent_bytes;
|
||||
CHECK(IEC104_Slave_Send_Response(service, &identity, &object) == IEC104_TCP_SUBMIT_OK);
|
||||
CHECK(fake.sent_bytes == sent + 16U);
|
||||
check_reply(fake.output + sent + 6U, interrogation, 10, 0x87);
|
||||
CHECK(IEC60870_Asdu_Set_COT(&object, 10) == IEC60870_ASDU_OK);
|
||||
fake.send_blocked = 1;
|
||||
sent = fake.sent_bytes;
|
||||
CHECK(IEC104_Slave_Try_Send_Response(service, &identity, &object) == IEC104_TCP_SUBMIT_OK);
|
||||
CHECK(fake.sent_bytes == sent);
|
||||
memset(bytes, 0xCC, sizeof(bytes));
|
||||
fake.send_blocked = 0; service_poll();
|
||||
CHECK(fake.sent_bytes == sent + 16U);
|
||||
check_reply(fake.output + sent + 6U, interrogation, 10, 0x8A);
|
||||
CHECK(IEC104_Slave_Close_Connection_By_Identity(service, &identity, NULL) == IEC104_TCP_OK);
|
||||
CHECK(IEC104_Slave_Close_Connection_By_Identity(service, &identity, NULL) == IEC104_TCP_NOT_CONNECTED);
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* 压缩请求不缩小HIGH容量;普通入口容量与原接口相同,Try遇待发NORMAL不入队/覆盖。 */
|
||||
static void test_identity_response_wait(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t event;
|
||||
IEC104_SlaveEventIdentity_t identity;
|
||||
IEC104_TcpServerStatus_t before, after;
|
||||
unsigned accepted;
|
||||
service_begin(); receive_request(&event, 0);
|
||||
CHECK(IEC104_Slave_Get_Event_Identity(service, &event, &identity));
|
||||
fake.send_blocked = 1;
|
||||
for (accepted = 0; accepted < 200; ++accepted) {
|
||||
IEC104_TcpSubmitResult_t result = IEC104_Slave_Send_Response(service, &identity, &event.asdu);
|
||||
if (result == IEC104_TCP_SUBMIT_WAIT) { break; }
|
||||
CHECK(result == IEC104_TCP_SUBMIT_OK);
|
||||
}
|
||||
CHECK(accepted == 1U + IEC104_TCP_HIGH_CAPACITY / (IEC104_ASDU_HIGH_ENTRY_HEADER_LEN + sizeof(interrogation)));
|
||||
CHECK(IEC104_Slave_Get_Status(service, &before, NULL) == IEC104_TCP_OK);
|
||||
CHECK(IEC104_Slave_Try_Send_Response(service, &identity, &event.asdu) == IEC104_TCP_SUBMIT_WAIT);
|
||||
CHECK(IEC104_Slave_Get_Status(service, &after, NULL) == IEC104_TCP_OK);
|
||||
CHECK(before.high_queue.used_bytes == after.high_queue.used_bytes &&
|
||||
before.connection.pending_send_records == after.connection.pending_send_records);
|
||||
fake.send_blocked = 0; service_poll(); service_drain();
|
||||
CHECK(IEC104_Slave_Send_Response(service, &identity, &event.asdu) == IEC104_TCP_SUBMIT_OK);
|
||||
service_end();
|
||||
|
||||
service_begin(); receive_request(&event, 0);
|
||||
CHECK(IEC104_Slave_Get_Event_Identity(service, &event, &identity));
|
||||
CHECK(IEC104_Slave_Enqueue_Event(service, &event.asdu) == IEC104_TCP_SUBMIT_OK);
|
||||
CHECK(IEC104_Slave_Get_Status(service, &before, NULL) == IEC104_TCP_OK);
|
||||
CHECK(IEC104_Slave_Try_Send_Response(service, &identity, &event.asdu) == IEC104_TCP_SUBMIT_WAIT);
|
||||
CHECK(IEC104_Slave_Get_Status(service, &after, NULL) == IEC104_TCP_OK);
|
||||
CHECK(after.high_queue.used_bytes == before.high_queue.used_bytes &&
|
||||
after.normal_queue.used_bytes == before.normal_queue.used_bytes &&
|
||||
after.normal_queue.pending_asdu_count == before.normal_queue.pending_asdu_count);
|
||||
service_poll(); service_drain();
|
||||
CHECK(IEC104_Slave_Try_Send_Response(service, &identity, &event.asdu) == IEC104_TCP_SUBMIT_OK);
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* 紧凑身份入口保留原实例/代次校验、参数拒绝和FATAL语义,不因省去事件而放宽准入。 */
|
||||
static void test_identity_response_validation(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t event;
|
||||
IEC104_SlaveEventIdentity_t identity, invalid;
|
||||
IEC60870_Asdu_t object;
|
||||
IEC60870_AppLayerParameters_t parameters = {1, 1, 1, 0, 1, 1, 249};
|
||||
uint8_t bytes[] = {200, 0, 3, 1};
|
||||
size_t sent;
|
||||
service_begin(); receive_request(&event, 0);
|
||||
CHECK(IEC104_Slave_Get_Event_Identity(service, &event, &identity));
|
||||
sent = fake.sent_bytes;
|
||||
CHECK(IEC104_Slave_Send_Response(NULL, &identity, &event.asdu) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC104_Slave_Send_Response(service, NULL, &event.asdu) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC104_Slave_Try_Send_Response(service, &identity, NULL) == IEC104_TCP_SUBMIT_ERROR);
|
||||
invalid = identity; invalid.slave = NULL;
|
||||
CHECK(IEC104_Slave_Send_Response(service, &invalid, &event.asdu) == IEC104_TCP_SUBMIT_ERROR);
|
||||
invalid = identity; ++invalid.instance_id;
|
||||
CHECK(IEC104_Slave_Try_Send_Response(service, &invalid, &event.asdu) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC104_Slave_Close_Connection_By_Identity(service, &invalid, NULL) == IEC104_TCP_INVALID_ARGUMENT);
|
||||
invalid = identity; invalid.generation = 0;
|
||||
CHECK(IEC104_Slave_Send_Response(service, &invalid, &event.asdu) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC104_Slave_Close_Connection_By_Identity(service, &invalid, NULL) == IEC104_TCP_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_Close_Connection_By_Identity(NULL, &identity, NULL) == IEC104_TCP_INVALID_HANDLE);
|
||||
CHECK(IEC104_Slave_Close_Connection_By_Identity(service, NULL, NULL) == IEC104_TCP_INVALID_ARGUMENT);
|
||||
CHECK(IEC60870_Asdu_Create_From_Buffer(&object, ¶meters, bytes, sizeof(bytes)) == IEC60870_ASDU_OK);
|
||||
CHECK(IEC104_Slave_Send_Response(service, &identity, &object) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC104_Slave_Try_Send_Response(service, &identity, &object) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(fake.sent_bytes == sent);
|
||||
sut->queue_initialized = 0;
|
||||
CHECK(IEC104_Slave_Try_Send_Response(service, &identity, &event.asdu) == IEC104_TCP_SUBMIT_FATAL);
|
||||
sut->queue_initialized = 1;
|
||||
push(stop_act, sizeof(stop_act)); service_poll(); service_drain();
|
||||
CHECK(IEC104_Slave_Send_Response(service, &identity, &event.asdu) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC104_Slave_Try_Send_Response(service, &identity, &event.asdu) == IEC104_TCP_SUBMIT_ERROR);
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* 身份输出独立于事件存储;非法来源、自引用和元数据不得污染输出或复制目标。 */
|
||||
static void test_event_identity(void)
|
||||
{
|
||||
@@ -406,6 +517,9 @@ static void test_event_identity(void)
|
||||
CHECK(other != NULL);
|
||||
CHECK(!IEC104_Slave_Get_Event_Identity(other, &copied, &identity));
|
||||
CHECK(memcmp(&identity, &before, sizeof(identity)) == 0);
|
||||
CHECK(IEC104_Slave_Send_Response(other, &identity, &copied.asdu) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC104_Slave_Try_Send_Response(other, &identity, &copied.asdu) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC104_Slave_Close_Connection_By_Identity(other, &identity, NULL) == IEC104_TCP_INVALID_ARGUMENT);
|
||||
copy_identity.slave = other; /* 即使局部编号相同,其他服务身份也不能用于当前会话。 */
|
||||
CHECK(IEC104_Slave_Check_Request(service, ©_identity) == IEC104_SLAVE_REQUEST_STALE);
|
||||
CHECK(IEC104_Slave_Destroy(other, NULL) == IEC104_TCP_OK);
|
||||
@@ -482,6 +596,9 @@ static void test_stale_generation(void)
|
||||
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(IEC104_Slave_Close_Connection_By_Identity(service, &old_identity, NULL) == IEC104_TCP_STALE_GENERATION);
|
||||
CHECK(IEC104_Slave_Send_Response(service, &old_identity, ¤t.asdu) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC104_Slave_Try_Send_Response(service, &old_identity, ¤t.asdu) == IEC104_TCP_SUBMIT_ERROR);
|
||||
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);
|
||||
@@ -562,6 +679,9 @@ static void test_service_reuse(void)
|
||||
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(IEC104_Slave_Close_Connection_By_Identity(service, &old_identity, NULL) == IEC104_TCP_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_Send_Response(service, &old_identity, ¤t.asdu) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC104_Slave_Try_Send_Response(service, &old_identity, ¤t.asdu) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(fake.sent_bytes == sent);
|
||||
CHECK(IEC60870_MasterConnection_Send_ACT_CON(¤t.connection, ¤t.asdu, false));
|
||||
service_end();
|
||||
@@ -675,6 +795,489 @@ static void test_offline_enqueue(void)
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* 多于原任务池容量的站点共享一个Slave;同IOA的数据源由CA定位到不同context。 */
|
||||
static void test_station_registry(void)
|
||||
{
|
||||
IEC104_Station_t stations[13] = {0}, duplicate = {0}, boundary = {0};
|
||||
struct { uint32_t ioa; int value; } points[13];
|
||||
IEC104_Station_t *current = NULL;
|
||||
unsigned i;
|
||||
service = IEC104_Slave_Create();
|
||||
CHECK(service != NULL && IEC104_Slave_Get_Station_Count(service) == 0U);
|
||||
for (i = 0U; i < 13U; ++i) {
|
||||
points[i].ioa = 100U;
|
||||
points[i].value = (int)i;
|
||||
CHECK(IEC104_Slave_Add_Station(service, &stations[i], (uint16_t)(i + 1U), &points[i]) == IEC104_STATION_OK);
|
||||
}
|
||||
CHECK(IEC104_Slave_Get_Station_Count(service) == 13U);
|
||||
for (i = 0U; i < 13U; ++i) {
|
||||
current = IEC104_Slave_Next_Station(service, current);
|
||||
CHECK(current == &stations[i]);
|
||||
CHECK(IEC104_Slave_Get_Station(service, (uint16_t)(i + 1U)) == current);
|
||||
CHECK(IEC104_Station_Get_Common_Address(current) == i + 1U);
|
||||
CHECK(IEC104_Station_Get_Context(current) == &points[i]);
|
||||
}
|
||||
CHECK(IEC104_Slave_Next_Station(service, current) == NULL);
|
||||
CHECK(IEC104_Slave_Next_Station(service, &duplicate) == NULL);
|
||||
CHECK(IEC104_Slave_Add_Station(service, &duplicate, 7U, NULL) == IEC104_STATION_DUPLICATE_CA);
|
||||
CHECK(IEC104_Station_Get_Common_Address(&duplicate) == 0U);
|
||||
CHECK(IEC104_Slave_Add_Station(service, &stations[0], 100U, NULL) == IEC104_STATION_ALREADY_REGISTERED);
|
||||
CHECK(IEC104_Station_Get_Context(&stations[0]) == &points[0]);
|
||||
CHECK(IEC104_Slave_Add_Station(service, &boundary, 65534U, NULL) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Get_Station(service, 65534U) == &boundary);
|
||||
CHECK(IEC104_Slave_Get_Station(service, IEC104_CA_BROADCAST) == NULL);
|
||||
CHECK(IEC104_Slave_Get_Station(service, 0U) == NULL);
|
||||
CHECK(IEC104_Slave_Get_Station(service, 100U) == NULL);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &stations[6]) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Next_Station(service, &stations[5]) == &stations[7]);
|
||||
CHECK(IEC104_Slave_Add_Station(service, &duplicate, 7U, &points[6]) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Get_Station(service, 7U) == &duplicate);
|
||||
CHECK(IEC104_Slave_Next_Station(service, &boundary) == &duplicate);
|
||||
for (i = 0U; i < 13U; ++i) {
|
||||
if (i != 6U) { CHECK(IEC104_Slave_Remove_Station(service, &stations[i]) == IEC104_STATION_OK); }
|
||||
}
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &boundary) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &duplicate) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Station_Get_Common_Address(&duplicate) == 0U && IEC104_Station_Get_Context(&duplicate) == NULL);
|
||||
CHECK(IEC104_Slave_Get_Station_Count(service) == 0U && IEC104_Slave_Next_Station(service, NULL) == NULL);
|
||||
CHECK(IEC104_Slave_Destroy(service, NULL) == IEC104_TCP_OK);
|
||||
}
|
||||
|
||||
/* 引用覆盖延迟业务寿命;不占用其他站点,不允许零计数释放或计数回绕。 */
|
||||
static void test_station_references(void)
|
||||
{
|
||||
IEC104_Station_t first = {0}, second = {0};
|
||||
service = IEC104_Slave_Create();
|
||||
CHECK(service != NULL);
|
||||
CHECK(IEC104_Slave_Acquire_Station(service, &first) == IEC104_STATION_NOT_FOUND);
|
||||
CHECK(IEC104_Slave_Release_Station(service, &first) == IEC104_STATION_NOT_FOUND);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &first) == IEC104_STATION_NOT_FOUND);
|
||||
CHECK(IEC104_Slave_Add_Station(service, &first, 1U, &first) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Release_Station(service, &first) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_Acquire_Station(service, &first) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Acquire_Station(service, &first) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &first) == IEC104_STATION_OK);
|
||||
CHECK(!IEC104_Station_Is_Registered(&first) && !IEC104_Station_Is_Reusable(&first));
|
||||
CHECK(IEC104_Slave_Get_Station(service, 1U) == NULL);
|
||||
CHECK(IEC104_Slave_Get_Station_By_Id(service, IEC104_Station_Get_Id(&first)) == &first);
|
||||
CHECK(IEC104_Station_Get_Context(&first) == &first);
|
||||
CHECK(IEC104_Slave_Add_Station(service, &second, 2U, NULL) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &second) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Release_Station(service, &first) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &first) == IEC104_STATION_NOT_FOUND);
|
||||
CHECK(!IEC104_Station_Is_Reusable(&first));
|
||||
CHECK(IEC104_Slave_Release_Station(service, &first) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Station_Is_Reusable(&first));
|
||||
CHECK(IEC104_Slave_Add_Station(service, &first, 1U, &first) == IEC104_STATION_OK);
|
||||
first._users = UINT32_MAX; /* 仅测试注入溢出边界,不需要执行数十亿次Acquire。 */
|
||||
CHECK(IEC104_Slave_Acquire_Station(service, &first) == IEC104_STATION_LIMIT);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &first) == IEC104_STATION_OK);
|
||||
first._users = 0U;
|
||||
CHECK(IEC104_Slave_Acquire_Station(service, &first) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Release_Station(service, &first) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Release_Station(service, &first) == IEC104_STATION_NOT_FOUND);
|
||||
CHECK(IEC104_Slave_Destroy(service, NULL) == IEC104_TCP_OK);
|
||||
}
|
||||
|
||||
/* 固定集合允许嵌套读者及站点引用,但在最后一个读者离开前不能增删或销毁。 */
|
||||
static void test_station_iteration_lock(void)
|
||||
{
|
||||
IEC104_Station_t first = {0}, second = {0};
|
||||
service = IEC104_Slave_Create();
|
||||
CHECK(service != NULL);
|
||||
CHECK(IEC104_Slave_Unlock_Stations(service) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_Lock_Stations(service) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Destroy(service, NULL) == IEC104_TCP_INVALID_STATE);
|
||||
CHECK(IEC104_Slave_Add_Station(service, &first, 1U, NULL) == IEC104_STATION_BUSY);
|
||||
CHECK(IEC104_Slave_Unlock_Stations(service) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Add_Station(service, &first, 1U, NULL) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Lock_Stations(service) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Lock_Stations(service) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Get_Station(service, 1U) == &first);
|
||||
CHECK(IEC104_Slave_Next_Station(service, NULL) == &first);
|
||||
CHECK(IEC104_Slave_Add_Station(service, &second, 2U, NULL) == IEC104_STATION_BUSY);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &first) == IEC104_STATION_BUSY);
|
||||
CHECK(IEC104_Slave_Acquire_Station(service, &first) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Release_Station(service, &first) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Unlock_Stations(service) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &first) == IEC104_STATION_BUSY);
|
||||
CHECK(IEC104_Slave_Unlock_Stations(service) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &first) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Unlock_Stations(service) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_Destroy(service, NULL) == IEC104_TCP_OK);
|
||||
}
|
||||
|
||||
/* 站点属于服务,不属于某次TCP连接;关闭/重启保留,销毁前必须显式解除关联。 */
|
||||
static void test_station_lifecycle(void)
|
||||
{
|
||||
IEC104_Station_t station = {0};
|
||||
IEC104_TcpError_t error;
|
||||
IEC104_SlaveConfig_t config;
|
||||
service_begin();
|
||||
CHECK(IEC104_Slave_Add_Station(service, &station, 0U, NULL) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_Add_Station(service, &station, IEC104_CA_BROADCAST, NULL) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_Add_Station(NULL, &station, 1U, NULL) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_Add_Station(service, NULL, 1U, NULL) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_Add_Station(service, &station, 1U, &config) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Close(service, NULL) == IEC104_TCP_OK);
|
||||
service_drain();
|
||||
CHECK(IEC104_Slave_Get_Station(service, 1U) == &station);
|
||||
CHECK(IEC104_Slave_Destroy(service, &error) == IEC104_TCP_INVALID_STATE);
|
||||
CHECK(error.result == IEC104_TCP_INVALID_STATE && error.stage == IEC104_TCP_STAGE_VALIDATE);
|
||||
IEC104_Slave_Config_Default(&config);
|
||||
CHECK(IEC104_Slave_Init(service, &config, NULL) == IEC104_TCP_OK);
|
||||
CHECK(IEC104_Station_Get_Context(IEC104_Slave_Get_Station(service, 1U)) == &config);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &station) == IEC104_STATION_OK);
|
||||
service_end();
|
||||
CHECK(IEC104_Slave_Remove_Station(NULL, &station) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_Acquire_Station(NULL, &station) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_Release_Station(NULL, &station) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_Lock_Stations(NULL) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_Unlock_Stations(NULL) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_Get_Station(NULL, 1U) == NULL && IEC104_Slave_Get_Station_Count(NULL) == 0U);
|
||||
CHECK(IEC104_Slave_Next_Station(NULL, NULL) == NULL);
|
||||
CHECK(IEC104_Station_Get_Common_Address(NULL) == 0U && IEC104_Station_Get_Context(NULL) == NULL);
|
||||
#if IEC104_TCP_SERVER_CAPACITY >= 2U
|
||||
{
|
||||
IEC104_Slave_t *other;
|
||||
IEC104_Station_t foreign = {0};
|
||||
service = IEC104_Slave_Create();
|
||||
other = IEC104_Slave_Create();
|
||||
CHECK(service != NULL && other != NULL);
|
||||
CHECK(IEC104_Slave_Add_Station(service, &station, 7U, &station) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Add_Station(other, &station, 8U, NULL) == IEC104_STATION_ALREADY_REGISTERED);
|
||||
CHECK(IEC104_Slave_Add_Station(other, &foreign, 7U, &foreign) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Get_Station(service, 7U) == &station && IEC104_Slave_Get_Station(other, 7U) == &foreign);
|
||||
CHECK(IEC104_Slave_Acquire_Station(other, &station) == IEC104_STATION_NOT_FOUND);
|
||||
CHECK(IEC104_Slave_Release_Station(other, &station) == IEC104_STATION_NOT_FOUND);
|
||||
CHECK(IEC104_Slave_Remove_Station(other, &station) == IEC104_STATION_NOT_FOUND);
|
||||
CHECK(IEC104_Slave_Next_Station(other, &station) == NULL);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &station) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Remove_Station(other, &foreign) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Destroy(other, NULL) == IEC104_TCP_OK);
|
||||
CHECK(IEC104_Slave_Destroy(service, NULL) == IEC104_TCP_OK);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/* 遍历不冻结增删;旧引用可用,新注册编号不会混入进行中的广播,最后一个快照归还才回收。 */
|
||||
static void test_station_snapshot(void)
|
||||
{
|
||||
IEC104_Station_t first = {0}, second = {0}, replacement = {0}, added = {0};
|
||||
IEC104_StationSnapshot_t scan = {0}, nested = {0}, copy;
|
||||
uint64_t old_id;
|
||||
service = IEC104_Slave_Create();
|
||||
CHECK(service != NULL);
|
||||
CHECK(IEC104_Slave_StationSnapshot_Begin(NULL, &scan) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_StationSnapshot_End(&scan) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_StationSnapshot_Next(&scan) == NULL);
|
||||
CHECK(IEC104_Slave_Add_Station(service, &first, 1U, &first) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Add_Station(service, &second, 2U, &second) == IEC104_STATION_OK);
|
||||
old_id = IEC104_Station_Get_Id(&second);
|
||||
CHECK(IEC104_Slave_StationSnapshot_Begin(service, &scan) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_StationSnapshot_Begin(service, &scan) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
copy = scan;
|
||||
CHECK(IEC104_Slave_StationSnapshot_Next(©) == NULL);
|
||||
CHECK(IEC104_Slave_StationSnapshot_End(©) == IEC104_STATION_INVALID_ARGUMENT);
|
||||
CHECK(IEC104_Slave_StationSnapshot_Next(&scan) == &first);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &second) == IEC104_STATION_OK);
|
||||
CHECK(!IEC104_Station_Is_Reusable(&second));
|
||||
CHECK(IEC104_Slave_Add_Station(service, &replacement, 2U, &replacement) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Station_Get_Id(&replacement) > old_id);
|
||||
CHECK(IEC104_Slave_Get_Station_By_Id(service, old_id) == &second);
|
||||
CHECK(IEC104_Slave_Get_Station(service, 2U) == &replacement);
|
||||
CHECK(IEC104_Slave_Add_Station(service, &added, 3U, NULL) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_StationSnapshot_Begin(service, &nested) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_StationSnapshot_Next(&scan) == NULL);
|
||||
CHECK(IEC104_Slave_StationSnapshot_End(&scan) == IEC104_STATION_OK);
|
||||
CHECK(!IEC104_Station_Is_Reusable(&second));
|
||||
CHECK(IEC104_Slave_StationSnapshot_Next(&nested) == &first);
|
||||
CHECK(IEC104_Slave_StationSnapshot_Next(&nested) == &replacement);
|
||||
CHECK(IEC104_Slave_StationSnapshot_Next(&nested) == &added);
|
||||
CHECK(IEC104_Slave_StationSnapshot_Next(&nested) == NULL);
|
||||
CHECK(IEC104_Slave_StationSnapshot_End(&nested) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Station_Is_Reusable(&second) && IEC104_Slave_Get_Station_By_Id(service, old_id) == NULL);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &first) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &replacement) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Remove_Station(service, &added) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_StationSnapshot_Begin(service, &scan) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_StationSnapshot_Next(&scan) == NULL);
|
||||
CHECK(IEC104_Slave_Destroy(service, NULL) == IEC104_TCP_INVALID_STATE);
|
||||
CHECK(IEC104_Slave_StationSnapshot_End(&scan) == IEC104_STATION_OK);
|
||||
CHECK(IEC104_Slave_Destroy(service, NULL) == IEC104_TCP_OK);
|
||||
}
|
||||
|
||||
/* Rejection helpers retain the real transport, queue and sequence handling. */
|
||||
static const uint8_t reject_clock[] = {103, 1, 6, 0x31, 1, 0, 0, 0, 0, 0, 0, 0, 12, 28, 10, 26};
|
||||
static const uint8_t reject_read[] = {102, 1, 5, 0x32, 1, 0, 0xe9, 3, 0};
|
||||
|
||||
static void reject_push(uint16_t ns, uint16_t nr, const uint8_t *bytes, size_t length)
|
||||
{
|
||||
uint8_t frame[IEC104_MAX_APDU_LEN];
|
||||
int built = IEC104_Build_Iframe(frame, ns, nr, bytes, (uint16_t)length);
|
||||
CHECK(built == (int)(length + IEC104_APCI_LEN));
|
||||
push(frame, (size_t)built);
|
||||
}
|
||||
|
||||
static void reject_take(IEC104_SlaveEvent_t *event)
|
||||
{
|
||||
CHECK(IEC104_Slave_NextEvent(service, event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event->transport.type == IEC104_TCP_EVENT_I_FRAME && event->asdu_result == IEC60870_ASDU_OK);
|
||||
}
|
||||
|
||||
static unsigned reject_output(const uint8_t *frames[], size_t lengths[], unsigned capacity)
|
||||
{
|
||||
size_t offset = 0U;
|
||||
unsigned count = 0U;
|
||||
while (offset < fake.sent_bytes) {
|
||||
const uint8_t *frame = fake.output + offset;
|
||||
size_t length = frame[1] + 2U;
|
||||
CHECK(frame[0] == 0x68 && length >= IEC104_APCI_LEN && offset + length <= fake.sent_bytes);
|
||||
if ((frame[2] & 1U) == 0U) {
|
||||
CHECK(count < capacity);
|
||||
frames[count] = frame + IEC104_APCI_LEN;
|
||||
lengths[count++] = length - IEC104_APCI_LEN;
|
||||
}
|
||||
offset += length;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
static void reject_settle(void)
|
||||
{
|
||||
unsigned i;
|
||||
for (i = 0U; i < 8U; ++i) { service_poll(); service_drain(); }
|
||||
}
|
||||
|
||||
/* Every error cause preserves the request, including unknown object bytes and TEST. */
|
||||
static void test_reject_echo(void)
|
||||
{
|
||||
unsigned cause;
|
||||
for (cause = 44U; cause <= 47U; ++cause) {
|
||||
IEC104_SlaveEvent_t event, before;
|
||||
uint8_t request[IEC104_MAX_ASDU_LEN];
|
||||
const uint8_t *frames[2];
|
||||
size_t lengths[2];
|
||||
memset(request, 0x5a, sizeof(request));
|
||||
request[0] = 200U; request[1] = 0x82U; request[2] = 0x86U;
|
||||
service_begin();
|
||||
reject_push(0, 0, request, sizeof(request)); service_poll(); reject_take(&event);
|
||||
/* Seed OK so an implementation that resets last-submit to ERROR is caught. */
|
||||
CHECK(IEC60870_MasterConnection_Send_ASDU(&event.connection, &event.asdu));
|
||||
before = event;
|
||||
CHECK(IEC104_Slave_Reject_Request(service, &event, (int)cause) == IEC104_TCP_SUBMIT_OK);
|
||||
CHECK(memcmp(&event, &before, sizeof(event)) == 0);
|
||||
CHECK(IEC104_Slave_Get_Last_Submit_Result(&event.connection) == IEC104_TCP_SUBMIT_OK);
|
||||
CHECK(reject_output(frames, lengths, 2) == 2U && lengths[1] == sizeof(request));
|
||||
check_reply(frames[1], request, sizeof(request), (uint8_t)(0xc0U | cause));
|
||||
service_end();
|
||||
}
|
||||
}
|
||||
|
||||
static void test_reject_invalid(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t event, before, invalid;
|
||||
static const int causes[] = {-1, 7, 43, 48, 256};
|
||||
size_t sent, i;
|
||||
service_begin(); receive_request(&event, 0); before = event; sent = fake.sent_bytes;
|
||||
CHECK(IEC104_Slave_Reject_Request(NULL, &event, 44) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC104_Slave_Reject_Request(service, NULL, 44) == IEC104_TCP_SUBMIT_ERROR);
|
||||
for (i = 0U; i < sizeof(causes) / sizeof(causes[0]); ++i) {
|
||||
CHECK(IEC104_Slave_Reject_Request(service, &event, causes[i]) == IEC104_TCP_SUBMIT_ERROR);
|
||||
}
|
||||
invalid = event; /* Deliberately invalid self-references. */
|
||||
CHECK(IEC104_Slave_Reject_Request(service, &invalid, 44) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC104_Slave_Copy_Event(&invalid, &event));
|
||||
invalid.asdu_result = IEC60870_ASDU_ERROR;
|
||||
CHECK(IEC104_Slave_Reject_Request(service, &invalid, 44) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC104_Slave_Copy_Event(&invalid, &event)); ++invalid._owner_serial;
|
||||
CHECK(IEC104_Slave_Reject_Request(service, &invalid, 44) == IEC104_TCP_SUBMIT_ERROR);
|
||||
#if IEC104_TCP_SERVER_CAPACITY > 1
|
||||
{
|
||||
IEC104_Slave_t *other = IEC104_Slave_Create();
|
||||
CHECK(other != NULL);
|
||||
CHECK(IEC104_Slave_Reject_Request(other, &event, 44) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(IEC104_Slave_Destroy(other, NULL) == IEC104_TCP_OK);
|
||||
}
|
||||
#endif
|
||||
CHECK(fake.sent_bytes == sent && memcmp(&event, &before, sizeof(event)) == 0);
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* Ordinary unread I events must not block an immediate rejection. */
|
||||
static void test_reject_batch(void)
|
||||
{
|
||||
unsigned mode, i;
|
||||
for (mode = 0U; mode < 3U; ++mode) {
|
||||
const uint8_t *requests[] = {reject_clock, reject_read};
|
||||
size_t sizes[] = {sizeof(reject_clock), sizeof(reject_read)};
|
||||
const uint8_t *frames[2]; size_t lengths[2];
|
||||
IEC104_SlaveEvent_t event, before;
|
||||
IEC104_TcpServerStatus_t status;
|
||||
if (mode == 1U) { requests[0] = reject_read; sizes[0] = sizeof(reject_read);
|
||||
requests[1] = reject_clock; sizes[1] = sizeof(reject_clock); }
|
||||
if (mode == 2U) { requests[1] = reject_clock; sizes[1] = sizeof(reject_clock); }
|
||||
service_begin();
|
||||
for (i = 0U; i < 2U; ++i) { reject_push((uint16_t)i, 0, requests[i], sizes[i]); }
|
||||
service_poll();
|
||||
for (i = 0U; i < 2U; ++i) {
|
||||
reject_take(&event); before = event;
|
||||
CHECK(IEC104_Slave_Get_Status(service, &status, NULL) == IEC104_TCP_OK);
|
||||
CHECK(status.pending_state_event_count == 0U && status.pending_event_count == 1U - i);
|
||||
CHECK(IEC104_Slave_Reject_Request(service, &event, 44) == IEC104_TCP_SUBMIT_OK);
|
||||
CHECK(memcmp(&event, &before, sizeof(event)) == 0);
|
||||
}
|
||||
reject_settle();
|
||||
CHECK(reject_output(frames, lengths, 2) == 2U);
|
||||
for (i = 0U; i < 2U; ++i) {
|
||||
CHECK(lengths[i] == sizes[i]); check_reply(frames[i], requests[i], sizes[i], 0x6c);
|
||||
}
|
||||
CHECK(IEC104_Slave_Get_Status(service, &status, NULL) == IEC104_TCP_OK && status.connected);
|
||||
service_end();
|
||||
}
|
||||
}
|
||||
|
||||
/* k=1 queues the second response; reusing the source cannot change the saved bytes. */
|
||||
static void test_reject_window(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t event;
|
||||
IEC104_TcpServerStatus_t status;
|
||||
const uint8_t *frames[2]; size_t lengths[2];
|
||||
uint8_t unknown[IEC104_MAX_ASDU_LEN];
|
||||
memset(unknown, 0x5a, sizeof(unknown));
|
||||
unknown[0] = 200U; unknown[1] = 0U; unknown[2] = 0x86U;
|
||||
service_open_with_window(1U); service_connect();
|
||||
reject_push(0, 0, reject_clock, sizeof(reject_clock));
|
||||
reject_push(1, 0, unknown, sizeof(unknown)); service_poll();
|
||||
reject_take(&event); CHECK(IEC104_Slave_Reject_Request(service, &event, 44) == IEC104_TCP_SUBMIT_OK);
|
||||
reject_take(&event); CHECK(IEC104_Slave_Reject_Request(service, &event, 44) == IEC104_TCP_SUBMIT_OK);
|
||||
memset(&event, 0xa5, sizeof(event));
|
||||
CHECK(IEC104_Slave_Get_Status(service, &status, NULL) == IEC104_TCP_OK);
|
||||
CHECK(status.high_queue.pending_asdu_count == 1U && reject_output(frames, lengths, 2) == 1U);
|
||||
push_ack(1); reject_settle();
|
||||
CHECK(reject_output(frames, lengths, 2) == 2U && lengths[1] == sizeof(unknown));
|
||||
check_reply(frames[1], unknown, sizeof(unknown), 0xec);
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* Real HIGH exhaustion returns WAIT, retaining existing entries and the input event. */
|
||||
static void test_reject_full(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t event, before_event;
|
||||
IEC104_TcpServerStatus_t before, after;
|
||||
unsigned accepted;
|
||||
uint8_t high_before[IEC104_TCP_HIGH_CAPACITY];
|
||||
service_begin(); receive_request(&event, 0); fake.send_blocked = 1;
|
||||
for (accepted = 0U; accepted < 200U; ++accepted) {
|
||||
if (!IEC60870_MasterConnection_Send_ASDU(&event.connection, &event.asdu)) { break; }
|
||||
}
|
||||
CHECK(accepted > 0U && accepted < 200U);
|
||||
CHECK(IEC104_Slave_Get_Last_Submit_Result(&event.connection) == IEC104_TCP_SUBMIT_WAIT);
|
||||
before_event = event; memcpy(high_before, sut->high_bytes, sizeof(high_before));
|
||||
CHECK(IEC104_Slave_Get_Status(service, &before, NULL) == IEC104_TCP_OK);
|
||||
CHECK(IEC104_Slave_Reject_Request(service, &event, 44) == IEC104_TCP_SUBMIT_WAIT);
|
||||
CHECK(memcmp(&event, &before_event, sizeof(event)) == 0);
|
||||
CHECK(memcmp(high_before, sut->high_bytes, sizeof(high_before)) == 0);
|
||||
CHECK(IEC104_Slave_Get_Status(service, &after, NULL) == IEC104_TCP_OK && after.connected);
|
||||
CHECK(before.high_queue.used_bytes == after.high_queue.used_bytes &&
|
||||
before.high_queue.pending_asdu_count == after.high_queue.pending_asdu_count);
|
||||
fake.send_blocked = 0; reject_settle();
|
||||
service_end();
|
||||
}
|
||||
|
||||
/* State counts exclude CONNECTED; unread STOP/START blocks an old immediate response. */
|
||||
static void test_reject_state_gate(void)
|
||||
{
|
||||
unsigned restart;
|
||||
for (restart = 0U; restart < 2U; ++restart) {
|
||||
IEC104_SlaveEvent_t event, before;
|
||||
IEC104_TcpServerStatus_t status;
|
||||
const uint8_t *frames[1]; size_t lengths[1];
|
||||
service_open(); fake.accept_pending = 1; service_poll();
|
||||
CHECK(IEC104_Slave_Get_Status(service, &status, NULL) == IEC104_TCP_OK);
|
||||
CHECK(status.pending_event_count == 1U && status.pending_state_event_count == 0U);
|
||||
CHECK(IEC104_Slave_NextEvent(service, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(IEC104_Slave_Reject_Request(service, &event, 44) == IEC104_TCP_SUBMIT_ERROR);
|
||||
push(start_act, sizeof(start_act)); service_poll();
|
||||
CHECK(IEC104_Slave_Get_Status(service, &status, NULL) == IEC104_TCP_OK);
|
||||
CHECK(status.pending_state_event_count == 1U); CHECK(service_drain() == 1U);
|
||||
reject_push(0, 0, reject_clock, sizeof(reject_clock)); push(stop_act, sizeof(stop_act));
|
||||
if (restart) { push(start_act, sizeof(start_act)); }
|
||||
service_poll(); reject_take(&event); before = event;
|
||||
CHECK(IEC104_Slave_Get_Status(service, &status, NULL) == IEC104_TCP_OK);
|
||||
CHECK(status.pending_state_event_count == 1U + restart);
|
||||
CHECK(IEC104_Slave_Reject_Request(service, &event, 44) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(memcmp(&event, &before, sizeof(event)) == 0 && reject_output(frames, lengths, 1) == 0U);
|
||||
CHECK(IEC104_Slave_NextEvent(service, &event) == IEC104_TCP_EVENT_READY);
|
||||
CHECK(event.transport.type == IEC104_TCP_EVENT_DEACTIVATED);
|
||||
CHECK(IEC104_Slave_Get_Status(service, &status, NULL) == IEC104_TCP_OK);
|
||||
CHECK(status.pending_state_event_count == restart);
|
||||
service_drain();
|
||||
if (!restart) { push(start_act, sizeof(start_act)); service_poll(); service_drain(); }
|
||||
reject_push(1, 0, reject_read, sizeof(reject_read)); service_poll(); reject_take(&event);
|
||||
CHECK(IEC104_Slave_Reject_Request(service, &event, 44) == IEC104_TCP_SUBMIT_OK);
|
||||
CHECK(reject_output(frames, lengths, 1) == 1U);
|
||||
service_end();
|
||||
}
|
||||
}
|
||||
|
||||
/* STOP invalidates queued replies; a repeated START preserves them without a state event. */
|
||||
static void test_reject_queued_lifecycle(void)
|
||||
{
|
||||
unsigned stop;
|
||||
for (stop = 0U; stop < 2U; ++stop) {
|
||||
IEC104_SlaveEvent_t event;
|
||||
IEC104_TcpServerStatus_t status;
|
||||
const uint8_t *frames[2]; size_t lengths[2];
|
||||
service_open_with_window(1U); service_connect();
|
||||
reject_push(0, 0, reject_clock, sizeof(reject_clock));
|
||||
reject_push(1, 0, reject_read, sizeof(reject_read)); service_poll();
|
||||
reject_take(&event); CHECK(IEC104_Slave_Reject_Request(service, &event, 44) == IEC104_TCP_SUBMIT_OK);
|
||||
reject_take(&event); CHECK(IEC104_Slave_Reject_Request(service, &event, 44) == IEC104_TCP_SUBMIT_OK);
|
||||
push(stop ? stop_act : start_act, sizeof(start_act)); service_poll();
|
||||
CHECK(IEC104_Slave_Get_Status(service, &status, NULL) == IEC104_TCP_OK);
|
||||
CHECK(status.pending_state_event_count == stop && status.high_queue.pending_asdu_count == 1U - stop);
|
||||
service_drain(); push_ack(1); reject_settle();
|
||||
CHECK(reject_output(frames, lengths, 2) == 2U - stop);
|
||||
if (stop) {
|
||||
push(start_act, sizeof(start_act)); service_poll(); service_drain();
|
||||
reject_push(2, 1, reject_read, sizeof(reject_read)); service_poll(); reject_take(&event);
|
||||
CHECK(IEC104_Slave_Reject_Request(service, &event, 44) == IEC104_TCP_SUBMIT_OK);
|
||||
}
|
||||
CHECK(reject_output(frames, lengths, 2) == 2U);
|
||||
check_reply(frames[1], reject_read, sizeof(reject_read), 0x6c);
|
||||
service_end();
|
||||
}
|
||||
}
|
||||
|
||||
static void test_reject_stale(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t old_event, current;
|
||||
size_t sent;
|
||||
service_begin(); receive_request(&old_event, 0);
|
||||
CHECK(IEC104_Slave_Close_Connection(service, &old_event, NULL) == IEC104_TCP_OK);
|
||||
service_drain(); service_connect(); receive_request(¤t, 0); sent = fake.sent_bytes;
|
||||
CHECK(IEC104_Slave_Reject_Request(service, &old_event, 44) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(fake.sent_bytes == sent);
|
||||
CHECK(IEC104_Slave_Reject_Request(service, ¤t, 44) == IEC104_TCP_SUBMIT_OK);
|
||||
service_end(); service_begin(); receive_request(¤t, 0); sent = fake.sent_bytes;
|
||||
CHECK(IEC104_Slave_Reject_Request(service, &old_event, 44) == IEC104_TCP_SUBMIT_ERROR);
|
||||
CHECK(fake.sent_bytes == sent);
|
||||
service_end();
|
||||
}
|
||||
|
||||
static void test_reject_fatal(void)
|
||||
{
|
||||
IEC104_SlaveEvent_t event, before;
|
||||
size_t sent;
|
||||
service_begin(); receive_request(&event, 0); before = event; sent = fake.sent_bytes;
|
||||
sut->queue_initialized = 0U;
|
||||
CHECK(IEC104_Slave_Reject_Request(service, &event, 44) == IEC104_TCP_SUBMIT_FATAL);
|
||||
CHECK(memcmp(&event, &before, sizeof(event)) == 0 && fake.sent_bytes == sent);
|
||||
sut->queue_initialized = 1U; /* Restore the injected fault only for cleanup; do not retry. */
|
||||
service_end();
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_mutable_views();
|
||||
@@ -683,6 +1286,9 @@ int main(void)
|
||||
test_constructed_asdu();
|
||||
test_wait_and_retry();
|
||||
test_invalid_inputs();
|
||||
test_identity_response_storage();
|
||||
test_identity_response_wait();
|
||||
test_identity_response_validation();
|
||||
test_event_identity();
|
||||
test_request_state();
|
||||
test_stale_generation();
|
||||
@@ -692,6 +1298,20 @@ int main(void)
|
||||
test_lifecycle();
|
||||
test_external_scheduler();
|
||||
test_offline_enqueue();
|
||||
puts("Slave service: 15 groups passed (automatic replies, deferred copies, lifecycle, request validation)");
|
||||
test_station_registry();
|
||||
test_station_references();
|
||||
test_station_iteration_lock();
|
||||
test_station_lifecycle();
|
||||
test_station_snapshot();
|
||||
test_reject_echo();
|
||||
test_reject_invalid();
|
||||
test_reject_batch();
|
||||
test_reject_window();
|
||||
test_reject_full();
|
||||
test_reject_state_gate();
|
||||
test_reject_queued_lifecycle();
|
||||
test_reject_stale();
|
||||
test_reject_fatal();
|
||||
puts("Slave service: 32 groups passed (replies, identities, stations, lifecycle, backpressure, rejection)");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -106,6 +106,48 @@ class Master {
|
||||
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];
|
||||
const initialCounters = [120000, 345600, 7890, 0, 1, 65535, 2147483647, 42];
|
||||
const counterRequest = (oa, qcc = 5, cot = 6, test = false, ca = 7) =>
|
||||
[101, 1, cot | (test ? 0x80 : 0), oa, ca & 0xff, ca >> 8, 0, 0, 0, qcc];
|
||||
const clockRequest = (oa, cot = 6, test = false, ca = 7) =>
|
||||
[103, 1, cot | (test ? 0x80 : 0), oa, ca & 0xff, ca >> 8, 0, 0, 0, 0x39, 0x30, 34, 12, 28, 10, 26];
|
||||
let counterSubmitted = 0, counterCompleted = 0;
|
||||
|
||||
async function readCounters(master, oa, qcc = 5, values = initialCounters, sequence = 0) {
|
||||
const group = qcc & 0x3f;
|
||||
const first = group === 2 ? 4 : 0, end = group === 1 ? 4 : 8;
|
||||
const count = 2 + (end - first) / 4;
|
||||
for (let frame = 0; frame < count; frame++) {
|
||||
const asdu = await master.readASDU();
|
||||
if (frame === 0 || frame === count - 1) {
|
||||
assert.deepEqual([...asdu], counterRequest(oa, qcc, frame === 0 ? 7 : 10));
|
||||
} else {
|
||||
assert.equal(asdu.length, 38);
|
||||
assert.deepEqual([...asdu.subarray(0, 6)], [15, 4, group === 5 ? 37 : 37 + group, oa, 7, 0]);
|
||||
for (let point = 0; point < 4; point++) {
|
||||
const offset = 6 + 8 * point, index = first + 4 * (frame - 1) + point;
|
||||
assert.equal(asdu.readUIntLE(offset, 3), 3001 + index);
|
||||
assert.equal(asdu.readInt32LE(offset + 3), values[index]);
|
||||
assert.equal(asdu[offset + 7], sequence);
|
||||
}
|
||||
}
|
||||
}
|
||||
counterSubmitted += count;
|
||||
counterCompleted++;
|
||||
}
|
||||
|
||||
async function counterRead(master, oa, qcc = 5, values = initialCounters, sequence = 0, ca = 7) {
|
||||
await master.send(counterRequest(oa, qcc, 6, false, ca));
|
||||
await readCounters(master, oa, qcc, values, sequence);
|
||||
}
|
||||
|
||||
async function counterReply(master, payload, cot, negative = false) {
|
||||
await master.send(payload);
|
||||
const expected = [...payload]; expected[2] = (payload[2] & 0x80) | cot | (negative ? 0x40 : 0);
|
||||
assert.deepEqual([...(await master.readASDU())], expected);
|
||||
counterSubmitted++;
|
||||
if (!negative) counterCompleted++;
|
||||
}
|
||||
async function totalCall(master, oa, fragmented = false, qoi = 20, ca = 7) {
|
||||
await master.send(request(oa, qoi, ca), fragmented);
|
||||
await readTotalCall(master, oa, qoi);
|
||||
@@ -157,17 +199,21 @@ async function main() {
|
||||
assert.ifError(help.error);
|
||||
assert.equal(help.status, 0, help.stderr);
|
||||
assert(help.stdout.includes('--run-ms'));
|
||||
assert(help.stdout.includes('--clock simulated|system'));
|
||||
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'],
|
||||
const invalidClock = spawnSync(executable, ['--clock', 'invalid'], { windowsHide: true, encoding: 'utf8' });
|
||||
assert.ifError(invalidClock.error);
|
||||
assert.equal(invalidClock.status, 1);
|
||||
const child = spawn(executable, ['--port', '0', '--ca', '7', '--window', '1', '--run-ms', '8000'],
|
||||
{ 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);
|
||||
const deadline = setTimeout(() => { child.kill(); }, 15000);
|
||||
try {
|
||||
const port = await new Promise((resolve, reject) => {
|
||||
const timer = setTimeout(() => reject(new Error(`No listening port: ${output} ${errors}`)), 2000);
|
||||
@@ -186,6 +232,55 @@ async function main() {
|
||||
return master;
|
||||
}
|
||||
const master = await connect();
|
||||
// 一次write内的106延时获得和102读取逐条回显,k=1时第二条由HIGH等待首条ACK。
|
||||
const unsupportedBatch = [
|
||||
[106, 1, 0x86, 0x18, 7, 0, 0, 0, 0, 0x39, 0x30],
|
||||
[102, 1, 5, 0x19, 7, 0, 0xe9, 3, 0]
|
||||
];
|
||||
master.socket.write(Buffer.concat(unsupportedBatch.map(payload => master.frame(payload))));
|
||||
for (const payload of unsupportedBatch) {
|
||||
const expected = [...payload]; expected[2] = (payload[2] & 0x80) | 0x40 | 44;
|
||||
assert.deepEqual([...(await master.readASDU())], expected);
|
||||
}
|
||||
// 默认模拟时钟:成功、TEST拒绝、错误COT及广播实际CA确认都保持请求时标原文。
|
||||
for (const [payload, cot, negative] of [
|
||||
[clockRequest(0x10), 7, false],
|
||||
[clockRequest(0x11, 6, true), 7, true],
|
||||
[clockRequest(0x12, 5), 45, true],
|
||||
[clockRequest(0x13, 6, false, 65535), 7, false]
|
||||
]) {
|
||||
await master.send(payload);
|
||||
const expected = [...payload];
|
||||
expected[2] = (payload[2] & 0x80) | cot | (negative ? 0x40 : 0);
|
||||
expected[4] = 7; expected[5] = 0;
|
||||
assert.deepEqual([...(await master.readASDU())], expected);
|
||||
}
|
||||
// 同批103先保存执行任务,102当场兜底;Try确认必须等已提交的44之后发送。
|
||||
const clockBatch = clockRequest(0x14);
|
||||
const readBatch = [102, 1, 5, 0x15, 7, 0, 0xe9, 3, 0];
|
||||
master.socket.write(Buffer.concat([master.frame(clockBatch), master.frame(readBatch)]));
|
||||
const readRejected = [...readBatch]; readRejected[2] = 0x40 | 44;
|
||||
assert.deepEqual([...(await master.readASDU())], readRejected);
|
||||
const clockConfirmed = [...clockBatch]; clockConfirmed[2] = 7;
|
||||
assert.deepEqual([...(await master.readASDU())], clockConfirmed);
|
||||
await counterRead(master, 0x20);
|
||||
await counterReply(master, counterRequest(0x21, 0x45), 7);
|
||||
await counterRead(master, 0x22, 5, initialCounters, 1);
|
||||
await counterRead(master, 0x23, 1, initialCounters, 1);
|
||||
await counterRead(master, 0x24, 2, initialCounters, 1);
|
||||
await counterReply(master, counterRequest(0x25, 3), 7, true);
|
||||
await counterReply(master, counterRequest(0x26, 4), 7, true);
|
||||
await counterReply(master, counterRequest(0x27, 5, 8), 45, true);
|
||||
// TEST requests must not freeze or clear real counters.
|
||||
await counterReply(master, counterRequest(0x28, 0x85, 6, true), 7, true);
|
||||
await counterRead(master, 0x29, 5, initialCounters, 1, 65535);
|
||||
await counterReply(master, counterRequest(0x2a, 0x85), 7);
|
||||
await counterRead(master, 0x2b, 5, initialCounters, 2);
|
||||
await counterReply(master, counterRequest(0x2c, 0x45), 7);
|
||||
const zeroCounters = initialCounters.map(() => 0);
|
||||
await counterRead(master, 0x2d, 5, zeroCounters, 3);
|
||||
await counterReply(master, counterRequest(0x2e, 0xc5), 7);
|
||||
await counterRead(master, 0x2f, 5, zeroCounters, 3);
|
||||
await totalCall(master, 0x5a, true);
|
||||
await totalCall(master, 0x5b, false, 21);
|
||||
await totalCall(master, 0x5c, false, 22);
|
||||
@@ -220,18 +315,30 @@ async function main() {
|
||||
assert.deepEqual([...(await master.readASDU())], deactCon);
|
||||
await master.control(0x13, 0x23);
|
||||
await master.control(7, 0x0b);
|
||||
await totalCall(master, 0x6b);
|
||||
// k=1: both instances must make progress, preserving their own response order.
|
||||
master.socket.write(Buffer.concat([master.frame(request(0x6b)), master.frame(counterRequest(0x60))]));
|
||||
const generalFrames = [], counterFrames = [];
|
||||
for (let index = 0; index < 10; index++) {
|
||||
const asdu = await master.readASDU();
|
||||
(asdu[0] === 101 || asdu[0] === 15 ? counterFrames : generalFrames).push(asdu);
|
||||
}
|
||||
assert.equal(generalFrames.length, 6);
|
||||
assert.equal(counterFrames.length, 4);
|
||||
await readTotalCall({ readASDU: async () => generalFrames.shift() }, 0x6b);
|
||||
await readCounters({ readASDU: async () => counterFrames.shift() }, 0x60, 5, zeroCounters, 3);
|
||||
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, /STOP submitted=65 gi_submitted=11 /);
|
||||
assert.match(output, /deactivated=1 timeouts=0/);
|
||||
assert.match(output, /negative_submitted=\d+ dropped_requests=0/);
|
||||
assert.match(output, new RegExp(`counter_submitted=${counterSubmitted} counter_completed=${counterCompleted}`));
|
||||
assert.match(output, /clock_executed=3 clock_synchronized=3 clock_submitted=5/);
|
||||
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)');
|
||||
console.log('Slave example loopback: passed (real TCP, batched 106+102 and 103+102, simulated clock/TEST/COT/broadcast, GI and counter groups/broadcast, freeze/reset/TEST, independent k=1 progress, busy/deactivation/errors, STOP/START, reconnect, cleanup)');
|
||||
} finally {
|
||||
clearTimeout(deadline);
|
||||
for (const socket of sockets) socket.destroy();
|
||||
|
||||
Reference in New Issue
Block a user