2557 lines
138 KiB
C
2557 lines
138 KiB
C
/**
|
||
* 子站两类生命周期ASDU字节环:HIGH保存待发应答;NORMAL保留待发及已交接、仍待ACK的事件。
|
||
* 条目在逻辑上连续,头与原文均可跨数组尾部回绕,不为尾部空隙浪费容量。
|
||
* HIGH空间不足时拒绝;NORMAL覆盖最旧原文,包括已提交条目,但保留其待确认计数。
|
||
* SendBuffer接手I帧且Manager建立记录后,Mark_Submitted才完成队列记账:HIGH消费原文,NORMAL保留原文。
|
||
* Manager初始化成功、受理新STOP或从STOPPED受理START时清空HIGH;已启用时重复START保留HIGH。
|
||
* Rebind清空HIGH并恢复NORMAL现存原文。
|
||
* 本模块负责存储、条目身份及环内候选选择;跨环比较、分配N(S)、确认合法性由Manager和Link处理。
|
||
* 兼容模式HIGH只存长度,票据由FIFO位置隐式表示;NORMAL以已提交前缀加待发后缀存储。
|
||
* 严格模式另存等级与共同受理顺序,按真实条目查找交接/ACK身份;两类编号均与I帧N(S)不同。
|
||
* 可选严格业务调度在相同字节预算内记录1..12级,HIGH逐级票据独立,NORMAL允许提交状态交错。
|
||
* 严格NORMAL覆盖在途原文只释放payload,保留精确身份头直到ACK;任意位置删除采用有界原地压紧。
|
||
* 可选SOE区另借固定26字节槽与精确退休身份表;普通NORMAL不能覆盖SOE,二者对外仍为同一生命周期。
|
||
* SOE占位占保留额度但尚不可交接;重绑保留占位,只有应用Resolve才能发布或取消。
|
||
* 所有存储由调用方提供;同连接操作串行,输入/输出与内部存储不得重叠。
|
||
*/
|
||
#include "IEC104_AsduQueue.h"
|
||
|
||
#include <stddef.h>
|
||
#include <string.h>
|
||
|
||
/*
|
||
* 兼容NORMAL编号环为1..UINT64_MAX,跳过无效值0;活跃编号到next_entry_serial的前向距离至多半环。
|
||
* 队列活跃编号包括仍有原文的条目和NORMAL覆盖待ACK条目,不能按普通整数大小判断先后。
|
||
* 兼容HIGH另用不回绕的隐式序号;严格模式不用这套半环比较,Manager仍用frame_id和N(S)记账。
|
||
*/
|
||
#define ENTRY_SERIAL_WINDOW_MAX (UINT64_MAX / 2U)
|
||
/* 队列记账状态,对应公开快照is_submitted的0/1值;不是NORMAL条目头里的编码。
|
||
* PENDING表示队列尚未登记发送交接,不能据此断言SendBuffer是否已经接手。
|
||
* HIGH在Mark_Submitted中直接消费原文,因此环内没有RETAINED_UNACKED状态。 */
|
||
enum {
|
||
QUEUE_ENTRY_PENDING = 0,
|
||
QUEUE_ENTRY_RETAINED_UNACKED = 1
|
||
};
|
||
|
||
/* NORMAL条目头的实际存储编码:1为队列待发,2为已登记交接且保留原文等待ACK。 */
|
||
enum {
|
||
NORMAL_HEADER_PENDING = 1,
|
||
NORMAL_HEADER_RETAINED_UNACKED = 2,
|
||
NORMAL_HEADER_OVERWRITTEN = 3 /* 严格模式:只保留身份头,原文已经覆盖,仍等待真实ACK。 */
|
||
};
|
||
|
||
/* 编译期检查:NORMAL头的asdu_length仅8位,可表示0..255,当前ASDU长度上限249满足要求。
|
||
* 若以后将上限提高到255以上,此处会编译报错,提醒同步调整长度字段及该约束,避免长度被截断。
|
||
* 此检查不生成运行时代码,也不增加条目存储开销。 */
|
||
_Static_assert(IEC104_MAX_ASDU_LEN <= UINT8_MAX, "NORMAL ASDU length must fit in eight bits");
|
||
|
||
/* SOE槽的低两位为状态,高四位为业务等级;其余位保持0。 */
|
||
enum { SOE_FREE = 0, SOE_PENDING = 1, SOE_UNACKED = 2, SOE_DEFERRED = 3 };
|
||
|
||
/**
|
||
* 功能:取得指定普通字节环的物理队首,不访问独立SOE区。
|
||
* 输入参数:queue/priority——队列及生命周期。
|
||
* 输出参数:info——成功时写入普通环的队首视图。
|
||
* 返回值:OK、EMPTY或ERROR。
|
||
* 说明:供普通环覆盖和ACK回收使用,不能用跨区逻辑队首替代物理队首。
|
||
*/
|
||
static IEC104_AsduQueueResult_t Peek_Other_Head(const IEC104_AsduQueue_t *queue,
|
||
IEC104_AsduPriority_t priority, IEC104_AsduHeadInfo_t *info);
|
||
|
||
/**
|
||
* 功能:取得兼容模式指定生命周期的固定管理头长度;严格模式使用Strict_Header_Length。
|
||
* 输入参数:priority——调用方已验证为HIGH或NORMAL。
|
||
* 输出参数:无。
|
||
* 返回值:HIGH为sizeof(uint16_t),NORMAL为sizeof(IEC104_AsduNormalEntryHeader_t),单位为字节。
|
||
* 说明:长度不包含ASDU原文;NORMAL结果包含当前编译器的结构体填充,不是固定的协议长度。
|
||
*/
|
||
static uint32_t Entry_Header_Length(IEC104_AsduPriority_t priority)
|
||
{
|
||
return priority == IEC104_ASDU_PRIORITY_HIGH ?
|
||
IEC104_ASDU_HIGH_ENTRY_HEADER_LEN : IEC104_ASDU_NORMAL_ENTRY_HEADER_LEN;
|
||
}
|
||
|
||
/**
|
||
* 功能:计算编号环中的后继编号,跳过保留值0。
|
||
* 输入参数:serial——1..UINT64_MAX内的有效编号。
|
||
* 输出参数:无,不修改队列或连接代次。
|
||
* 返回值:普通编号加1;UINT64_MAX的后继为1。
|
||
* 说明:0留作无效编号;回绕只计算编号,不会清空队列或触发连接重绑。
|
||
*/
|
||
static uint64_t Entry_Serial_Next(uint64_t serial)
|
||
{
|
||
return serial == UINT64_MAX ? 1U : serial + 1U;
|
||
}
|
||
|
||
/**
|
||
* 功能:计算从from沿编号递增方向到to所需的步数,允许跨上限回绕。
|
||
* 输入参数:from/to——调用方保证均在1..UINT64_MAX内。
|
||
* 输出参数:无。
|
||
* 返回值:0..UINT64_MAX-1内的前向距离;编号相同时为0。
|
||
* 说明:有效编号共UINT64_MAX个,按此环长计算;例如UINT64_MAX到1的距离为1。
|
||
* 直接用uint64_t减法会按2^64取模,包含已跳过的0,不能用于本编号环。
|
||
*/
|
||
static uint64_t Entry_Serial_Distance(uint64_t from, uint64_t to)
|
||
{
|
||
return to >= from ? to - from : UINT64_MAX - from + to;
|
||
}
|
||
|
||
/**
|
||
* 功能:按半环规则判断older是否严格早于newer。
|
||
* 输入参数:older/newer——待比较的内部条目编号,0按无效编号处理。
|
||
* 输出参数:无。
|
||
* 返回值:两者非零且前向距离在1..ENTRY_SERIAL_WINDOW_MAX内时为1,否则0。
|
||
* 说明:先后关系仅适用于跨度受限的活跃窗口,不能判断任意历史编号的年代。
|
||
* UINT64_MAX早于1,但1不早于UINT64_MAX;相同编号不构成先后关系。
|
||
* 限制在半环内是为了避免回绕时混淆活跃编号的新旧关系;这里比较的是编号距离,不是条目数量。
|
||
*/
|
||
static int Entry_Serial_Before(uint64_t older, uint64_t newer)
|
||
{
|
||
uint64_t distance;
|
||
|
||
if (older == 0U || newer == 0U) {
|
||
return 0;
|
||
}
|
||
distance = Entry_Serial_Distance(older, newer);
|
||
return distance != 0U && distance <= ENTRY_SERIAL_WINDOW_MAX;
|
||
}
|
||
|
||
/**
|
||
* 功能:检查业务优先级是否为HIGH或NORMAL。
|
||
* 输入参数:priority——调用方传入的优先级。
|
||
* 输出参数:无。
|
||
* 返回值:有效返回1,否则0;COUNT只用于数组长度,不能作为业务优先级。
|
||
* 说明:直接处理传入的priority,不经过其他函数调用。
|
||
*/
|
||
static int Priority_Valid(IEC104_AsduPriority_t priority)
|
||
{
|
||
return priority == IEC104_ASDU_PRIORITY_HIGH || priority == IEC104_ASDU_PRIORITY_NORMAL;
|
||
}
|
||
|
||
/**
|
||
* 功能:按队列当前编号域比较两个NORMAL身份的先后。
|
||
* 输入参数:queue——队列;older/newer——内部受理身份。
|
||
* 输出参数:无。
|
||
* 返回值:older有效且严格早于newer时为1,否则0。
|
||
* 说明:绑定SOE后使用不回绕的线性编号;未绑定时保留原半环比较规则。
|
||
*/
|
||
static int Normal_Serial_Before(const IEC104_AsduQueue_t *queue, uint64_t older, uint64_t newer)
|
||
{
|
||
return queue->soe.capacity != 0U ? older != 0U && older < newer : Entry_Serial_Before(older, newer);
|
||
}
|
||
|
||
/**
|
||
* 功能:沿字节环前进指定长度,跨数组尾部时从下标0继续。
|
||
* 输入参数:bucket——容量非零的桶;index——有效下标;length——0..byte_capacity。
|
||
* 输出参数:无。
|
||
* 返回值:前进后的有效数组下标,length等于总容量时回到原位置。
|
||
* 说明:先比较尾部剩余长度再计算,避免index+length在32位边界溢出。
|
||
* 例如容量100、index为90、length为20,结果为10;本函数不校验占用范围或移动实际字节。
|
||
*/
|
||
static uint32_t Ring_Advance(const IEC104_AsduPriorityQueue_t *bucket, uint32_t index, uint32_t length)
|
||
{
|
||
uint32_t tail_bytes = bucket->byte_capacity - index;
|
||
return length >= tail_bytes ? length - tail_bytes : index + length;
|
||
}
|
||
|
||
/**
|
||
* 功能:从环中读取一段逻辑连续字节,必要时分两次复制。
|
||
* 输入参数:bucket/index——已验证的字节环及起点;length——不超过总容量的读取长度。
|
||
* 输出参数:destination——至少length字节、与环不重叠的可写区。
|
||
* 返回值:无。
|
||
* 说明:调用方先验证逻辑占用范围;本函数只处理物理回绕,不检查条目身份。
|
||
* 例如容量100,从90读取20字节,先复制尾部10字节,再复制开头10字节。
|
||
* 通过memcpy读取到调用方对象,条目头跨尾部或环内地址未对齐时也无需强转结构体指针。
|
||
*/
|
||
static void Ring_Read(const IEC104_AsduPriorityQueue_t *bucket, uint32_t index,
|
||
void *destination, uint32_t length)
|
||
{
|
||
uint32_t first_length = bucket->byte_capacity - index;
|
||
if (first_length > length) {
|
||
first_length = length;
|
||
}
|
||
memcpy(destination, bucket->bytes + index, first_length);
|
||
if (length > first_length) {
|
||
memcpy((uint8_t *)destination + first_length, bucket->bytes, length - first_length);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 功能:将一段逻辑连续字节写入环,必要时分两次复制。
|
||
* 输入参数:bucket/index——已验证的目标环及起点;source——不与环重叠的输入;
|
||
* length——不超过总容量且已获得空间保障的长度。
|
||
* 输出参数:bucket->bytes——写入对应的尾部和起始部分,不改变任何游标。
|
||
* 返回值:无。
|
||
* 说明:Enqueue先完成全部容量检查,再写头和原文,最后统一发布控制字段。
|
||
* 本函数只复制字节,不决定哪些旧条目可覆盖,也不更新used_bytes或条目计数。
|
||
*/
|
||
static void Ring_Write(const IEC104_AsduPriorityQueue_t *bucket, uint32_t index,
|
||
const void *source, uint32_t length)
|
||
{
|
||
uint32_t first_length = bucket->byte_capacity - index;
|
||
if (first_length > length) {
|
||
first_length = length;
|
||
}
|
||
memcpy(bucket->bytes + index, source, first_length);
|
||
if (length > first_length) {
|
||
memcpy(bucket->bytes, (const uint8_t *)source + first_length, length - first_length);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 功能:逻辑向前压缩:destination在本次占用区内严格早于source,但物理下标可更大。
|
||
* 输入参数:bucket——IEC104_AsduPriorityQueue_t *所指数据。
|
||
* destination——目标存储。
|
||
* source——源数据。
|
||
* length——字节长度。
|
||
* 输出参数:bucket——IEC104_AsduPriorityQueue_t *所指数据的更新结果。
|
||
* 返回值:无。
|
||
* 说明:每段同时限制源、目标的环尾;段内使用memmove处理重叠。按逻辑先后处理各段,
|
||
* 所有写入均落在已读取范围或更早位置,不破坏下一段/下一条目的原文,无需大临时数组。
|
||
*/
|
||
static void Move_Normal_Entry_Forward(IEC104_AsduPriorityQueue_t *bucket,
|
||
uint32_t destination, uint32_t source, uint32_t length);
|
||
|
||
/**
|
||
* 功能:检查业务等级是否为可入队的有效等级。
|
||
* 输入参数:priority——待检查的业务等级。
|
||
* 输出参数:无。
|
||
* 返回值:位于NONE与COUNT之间返回1,否则返回0。
|
||
* 说明:NONE仅供上层入口选择默认等级,不能作为严格队列的实际分道等级。
|
||
*/
|
||
static int Business_Priority_Valid(IEC104_BusinessPriority_t priority)
|
||
{
|
||
return priority > IEC104_BUSINESS_PRIORITY_NONE && priority < IEC104_BUSINESS_PRIORITY_COUNT;
|
||
}
|
||
|
||
/**
|
||
* 功能:从未对齐SOE字节槽读取64位受理身份。
|
||
* 输入参数:slot——完整26字节槽。
|
||
* 输出参数:无。
|
||
* 返回值:槽内serial,0表示空槽。
|
||
* 说明:使用memcpy,不把字节数组强转成结构体或uint64_t指针。
|
||
*/
|
||
static uint64_t Soe_Serial(const uint8_t *slot)
|
||
{
|
||
uint64_t serial;
|
||
memcpy(&serial, slot, sizeof(serial));
|
||
return serial;
|
||
}
|
||
|
||
/**
|
||
* 功能:检查独立SOE入口要求的固定报文形状。
|
||
* 输入参数:bytes/length——完整ASDU原文及长度。
|
||
* 输出参数:无。
|
||
* 返回值:满足Type30/31、单对象SQ0、COT3/PN0及合法地址时为1,否则0。
|
||
* 说明:保留TEST;值、品质和CP56由生产者校验,此处不拒绝时间IV标志。
|
||
*/
|
||
static int Soe_Asdu_Valid(const uint8_t *bytes, uint16_t length)
|
||
{
|
||
unsigned ca;
|
||
if (bytes == NULL || length != IEC104_ASDU_SOE_ASDU_LEN ||
|
||
(bytes[0] != 30U && bytes[0] != 31U) || bytes[1] != 1U || (bytes[2] & 0x7fU) != 3U) { return 0; }
|
||
ca = bytes[4] | ((unsigned)bytes[5] << 8);
|
||
return ca != 0U && ca != UINT16_MAX && (bytes[6] | bytes[7] | bytes[8]) != 0U;
|
||
}
|
||
|
||
/**
|
||
* 功能:验证外借SOE存储及所有有效槽的状态、身份和固定ASDU形状。
|
||
* 输入参数:queue——非NULL队列。
|
||
* 输出参数:无。
|
||
* 返回值:未绑定或全部状态一致返回1,否则0。
|
||
* 说明:绑定SOE后全局serial不回绕;离线不允许存在待ACK原文或退休身份。
|
||
*/
|
||
static int Soe_Valid(const IEC104_AsduQueue_t *queue)
|
||
{
|
||
const IEC104_AsduSoeStorage_t *soe = &queue->soe;
|
||
uint16_t i;
|
||
if (soe->capacity == 0U) {
|
||
return soe->slots == NULL && soe->retired == NULL && soe->retired_capacity == 0U;
|
||
}
|
||
if (soe->slots == NULL || soe->retired == NULL || soe->capacity > IEC104_ASDU_SOE_MAX_CAPACITY ||
|
||
soe->retired_capacity == 0U) { return 0; }
|
||
for (i = 0U; i < soe->capacity; ++i) {
|
||
const uint8_t *slot = soe->slots + (uint32_t)i * IEC104_ASDU_SOE_SLOT_LEN;
|
||
unsigned state = slot[8] & 3U;
|
||
uint64_t serial = Soe_Serial(slot);
|
||
if (state == SOE_FREE) { if (serial != 0U || slot[8] != 0U) { return 0; } continue; }
|
||
if (serial == 0U || (serial & 1U) == 0U || serial >= queue->next_entry_serial || (slot[8] & 0xc0U) != 0U ||
|
||
!Business_Priority_Valid((IEC104_BusinessPriority_t)(slot[8] >> 2)) ||
|
||
!Soe_Asdu_Valid(slot + 9U, IEC104_ASDU_SOE_ASDU_LEN) ||
|
||
(queue->connection_generation == 0U && state == SOE_UNACKED)) { return 0; }
|
||
}
|
||
for (i = 0U; i < soe->retired_capacity; ++i) {
|
||
if (soe->retired[i] != 0U &&
|
||
(queue->connection_generation == 0U || (soe->retired[i] & 1U) == 0U ||
|
||
soe->retired[i] >= queue->next_entry_serial)) { return 0; }
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
/**
|
||
* 功能:按精确serial查找仍保留原文的SOE槽。
|
||
* 输入参数:queue——已验证队列;serial——非零受理身份。
|
||
* 输出参数:无。
|
||
* 返回值:槽地址,未找到为NULL。
|
||
* 说明:物理槽可循环复用,不能把槽下标当作事件身份。
|
||
*/
|
||
static uint8_t *Soe_Find(const IEC104_AsduQueue_t *queue, uint64_t serial)
|
||
{
|
||
uint16_t i;
|
||
for (i = 0U; i < queue->soe.capacity; ++i) {
|
||
uint8_t *slot = queue->soe.slots + (uint32_t)i * IEC104_ASDU_SOE_SLOT_LEN;
|
||
if ((slot[8] & 3U) != SOE_FREE && Soe_Serial(slot) == serial) { return slot; }
|
||
}
|
||
return NULL;
|
||
}
|
||
|
||
/**
|
||
* 功能:扫描SOE区生成以事件为单位的状态快照。
|
||
* 输入参数:queue——已验证队列。
|
||
* 输出参数:status——容量、当前计数及累计丢弃数。
|
||
* 返回值:无。
|
||
* 说明:退休身份不计原文条数,延迟占位单列且不计可发送pending。
|
||
*/
|
||
static void Soe_Status(const IEC104_AsduQueue_t *queue, IEC104_AsduSoeStatus_t *status)
|
||
{
|
||
uint16_t i;
|
||
memset(status, 0, sizeof(*status));
|
||
status->capacity = queue->soe.capacity; status->retired_capacity = queue->soe.retired_capacity;
|
||
status->dropped_pending_count = queue->soe.dropped_pending_count;
|
||
status->overwritten_unacked_count = queue->soe.overwritten_unacked_count;
|
||
status->discarded_count = queue->soe.discarded_count;
|
||
for (i = 0U; i < queue->soe.capacity; ++i) {
|
||
unsigned state = queue->soe.slots[(uint32_t)i * IEC104_ASDU_SOE_SLOT_LEN + 8U] & 3U;
|
||
if (state != SOE_FREE) { ++status->stored_count; }
|
||
if (state == SOE_PENDING) { ++status->pending_count; }
|
||
if (state == SOE_UNACKED) { ++status->retained_unacked_count; }
|
||
if (state == SOE_DEFERRED) { ++status->deferred_count; }
|
||
}
|
||
for (i = 0U; i < queue->soe.retired_capacity; ++i) { if (queue->soe.retired[i] != 0U) { ++status->retired_count; } }
|
||
}
|
||
|
||
/**
|
||
* 功能:把固定SOE槽转换成NORMAL借用视图。
|
||
* 输入参数:queue/slot——有效队列及非空槽。
|
||
* 输出参数:info——完整身份、等级、长度和原文借用。
|
||
* 返回值:无。
|
||
* 说明:serial同时是全局受理顺序;9字节管理头不属于协议原文。
|
||
*/
|
||
static void Soe_Info(const IEC104_AsduQueue_t *queue, const uint8_t *slot, IEC104_AsduHeadInfo_t *info)
|
||
{
|
||
memset(info, 0, sizeof(*info));
|
||
info->entry_id.connection_generation = queue->connection_generation;
|
||
info->entry_id.entry_serial = Soe_Serial(slot);
|
||
info->admission_order = info->entry_id.entry_serial;
|
||
info->priority = IEC104_ASDU_PRIORITY_NORMAL;
|
||
info->business_priority = (IEC104_BusinessPriority_t)(slot[8] >> 2);
|
||
info->is_submitted = (slot[8] & 3U) == SOE_UNACKED;
|
||
info->entry_header_length = 9U;
|
||
info->asdu_first = slot + 9U;
|
||
info->asdu_first_length = info->asdu_length = IEC104_ASDU_SOE_ASDU_LEN;
|
||
}
|
||
|
||
/**
|
||
* 功能:按受理顺序查找SOE原文,或选取不受同级占位阻挡的待发候选。
|
||
* 输入参数:queue——有效队列;pending——仅待发候选;after——查询原文时排除不大于此身份的槽。
|
||
* 输出参数:info——成功时写入候选视图。
|
||
* 返回值:OK找到,EMPTY无候选;调用者负责队列验证。
|
||
* 说明:严格模式优先等级再serial;兼容模式按serial,同级占位始终阻挡后续SOE。
|
||
*/
|
||
static IEC104_AsduQueueResult_t Soe_Peek(const IEC104_AsduQueue_t *queue, int pending,
|
||
uint64_t after, IEC104_AsduHeadInfo_t *info)
|
||
{
|
||
const uint8_t *best = NULL;
|
||
uint16_t i;
|
||
unsigned rank;
|
||
if (pending && (queue->startup_pending != 0U || queue->normal_send_limited != 0U)) { return IEC104_ASDU_QUEUE_EMPTY; }
|
||
if (!pending) {
|
||
for (i = 0U; i < queue->soe.capacity; ++i) {
|
||
const uint8_t *slot = queue->soe.slots + (uint32_t)i * IEC104_ASDU_SOE_SLOT_LEN;
|
||
if ((slot[8] & 3U) != SOE_FREE && Soe_Serial(slot) > after &&
|
||
(best == NULL || Soe_Serial(slot) < Soe_Serial(best))) { best = slot; }
|
||
}
|
||
} else {
|
||
for (rank = 1U; rank < IEC104_BUSINESS_PRIORITY_COUNT; ++rank) {
|
||
const uint8_t *first = NULL;
|
||
for (i = 0U; i < queue->soe.capacity; ++i) {
|
||
const uint8_t *slot = queue->soe.slots + (uint32_t)i * IEC104_ASDU_SOE_SLOT_LEN;
|
||
unsigned state = slot[8] & 3U;
|
||
if ((state == SOE_PENDING || state == SOE_DEFERRED) && (slot[8] >> 2) == rank &&
|
||
(first == NULL || Soe_Serial(slot) < Soe_Serial(first))) { first = slot; }
|
||
}
|
||
if (first != NULL && (first[8] & 3U) == SOE_PENDING &&
|
||
(best == NULL || (queue->strict_business_priority == 0U && Soe_Serial(first) < Soe_Serial(best)))) {
|
||
best = first;
|
||
if (queue->strict_business_priority != 0U) { break; }
|
||
}
|
||
}
|
||
}
|
||
if (best == NULL) { return IEC104_ASDU_QUEUE_EMPTY; }
|
||
Soe_Info(queue, best, info);
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/* 严格模式仍使用原来的两个字节区;业务等级只在描述头中占少量元数据。
|
||
* 两环物理顺序都保持入队先后,NORMAL提交状态允许交错;覆盖头的长度为0,不作为原文返回给业务。
|
||
* serial在HIGH是逐级交接票据,在NORMAL是条目身份;admission_order才是跨环同级FIFO的比较依据。 */
|
||
typedef struct {
|
||
uint64_t serial, admission_order;
|
||
uint16_t length;
|
||
uint8_t rank, state;
|
||
} StrictEntry;
|
||
|
||
/**
|
||
* 功能:取得严格优先级模式下指定生命周期的条目管理头长度。
|
||
* 输入参数:priority——调用方已验证为HIGH或NORMAL。
|
||
* 输出参数:无。
|
||
* 返回值:HIGH返回STRICT_HIGH_ENTRY_HEADER_LEN,NORMAL返回NORMAL_ENTRY_HEADER_LEN,单位为字节。
|
||
* 说明:结果不包含ASDU原文,供严格队列计算条目占用和载荷位置。
|
||
*/
|
||
static uint32_t Strict_Header_Length(IEC104_AsduPriority_t priority)
|
||
{
|
||
return priority == IEC104_ASDU_PRIORITY_HIGH ? IEC104_ASDU_STRICT_HIGH_ENTRY_HEADER_LEN :
|
||
IEC104_ASDU_NORMAL_ENTRY_HEADER_LEN;
|
||
}
|
||
|
||
/**
|
||
* 功能:按所选物理环解码一个描述头,remaining包含该头及其后仍可读取的字节。
|
||
* 输入参数:queue——ASDU队列。
|
||
* priority——优先级。
|
||
* index——索引。
|
||
* remaining——剩余量。
|
||
* entry——StrictEntry *所指数据。
|
||
* 输出参数:entry——StrictEntry *所指数据的更新结果。
|
||
* 返回值: 只在返回1时可使用entry;
|
||
* 返回0时可能已填写部分字段,但不修改环或移动任何游标。
|
||
* 说明:只在返回1时可使用entry;返回0时可能已填写部分字段,但不修改环或移动任何游标。
|
||
* 覆盖身份头仅允许零长度,其余条目必须含完整ASDU;这里不代替整环的顺序/计数校验。
|
||
*/
|
||
static int Strict_Read(const IEC104_AsduQueue_t *queue, IEC104_AsduPriority_t priority,
|
||
uint32_t index, uint32_t remaining, StrictEntry *entry)
|
||
{
|
||
const IEC104_AsduPriorityQueue_t *bucket = &queue->priority_queues[priority];
|
||
uint32_t header_length = Strict_Header_Length(priority);
|
||
IEC104_AsduNormalEntryHeader_t normal;
|
||
if (remaining < header_length) { return 0; }
|
||
if (priority == IEC104_ASDU_PRIORITY_HIGH) {
|
||
Ring_Read(bucket, index, &entry->serial, sizeof(entry->serial));
|
||
Ring_Read(bucket, Ring_Advance(bucket, index, 8U), &entry->admission_order, sizeof(entry->admission_order));
|
||
Ring_Read(bucket, Ring_Advance(bucket, index, 16U), &entry->length, sizeof(entry->length));
|
||
Ring_Read(bucket, Ring_Advance(bucket, index, 18U), &entry->rank, sizeof(entry->rank));
|
||
entry->state = NORMAL_HEADER_PENDING;
|
||
if ((entry->serial & 15U) != entry->rank) { return 0; }
|
||
} else {
|
||
Ring_Read(bucket, index, &normal, sizeof(normal));
|
||
entry->serial = normal.entry_serial;
|
||
entry->admission_order = normal.entry_serial;
|
||
entry->length = (uint16_t)normal.asdu_length;
|
||
entry->rank = (uint8_t)normal.business_priority;
|
||
entry->state = (uint8_t)normal.entry_state;
|
||
if (entry->state != NORMAL_HEADER_PENDING && entry->state != NORMAL_HEADER_RETAINED_UNACKED &&
|
||
entry->state != NORMAL_HEADER_OVERWRITTEN) { return 0; }
|
||
}
|
||
if (entry->serial == 0U || entry->admission_order == 0U || entry->admission_order >= queue->next_entry_serial ||
|
||
!Business_Priority_Valid((IEC104_BusinessPriority_t)entry->rank) ||
|
||
(entry->state == NORMAL_HEADER_OVERWRITTEN ? entry->length != 0U :
|
||
(entry->length < IEC104_ASDU_HEADER_LEN || entry->length > IEC104_MAX_ASDU_LEN))) { return 0; }
|
||
return entry->length <= remaining - header_length;
|
||
}
|
||
|
||
/**
|
||
* 功能:预检全部严格条目后才允许压紧字节区;失败不改变字节、票据或输出。
|
||
* 输入参数:queue——ASDU队列。
|
||
* 输出参数:无。
|
||
* 返回值:1表示全部严格队列条目的布局、顺序、计数和分级票据区间一致,否则0。
|
||
* 说明:票据同级按16递增,队列中每级的未交接票据必须覆盖其完整head..next区间。
|
||
* 同时核对物理受理顺序与状态计数,防止丢头或跳号被误报成已交接;全环扫描不依赖待ACK前缀假设。
|
||
*/
|
||
static int Strict_Queue_Valid(const IEC104_AsduQueue_t *queue)
|
||
{
|
||
unsigned p, rank;
|
||
uint32_t overwritten = 0U, high_count = 0U;
|
||
uint32_t seen[IEC104_BUSINESS_PRIORITY_COUNT - 1] = {0U};
|
||
if (queue->priority_state == NULL || queue->strict_business_priority != 1U ||
|
||
queue->is_initialized != 1U || queue->next_entry_serial == 0U ||
|
||
queue->high_next_sequence == 0U || queue->normal_send_limited > 1U || queue->startup_pending > 1U) { return 0; }
|
||
for (rank = 1U; rank < IEC104_BUSINESS_PRIORITY_COUNT; ++rank) {
|
||
const IEC104_AsduPriorityTicketState_t *lane = &queue->priority_state->lanes[rank - 1U];
|
||
uint64_t count;
|
||
if (lane->head_sequence == 0U || lane->next_sequence < lane->head_sequence ||
|
||
lane->known_from == 0U || lane->known_from > lane->head_sequence ||
|
||
(lane->known_from & 15U) != rank ||
|
||
(lane->head_sequence & 15U) != rank || (lane->next_sequence & 15U) != rank ||
|
||
((lane->next_sequence - lane->head_sequence) & 15U) != 0U ||
|
||
((lane->cancel_begin != 0U || lane->cancel_end != 0U) &&
|
||
((lane->cancel_begin & 15U) != rank || (lane->cancel_end & 15U) != rank ||
|
||
lane->cancel_begin < lane->known_from || lane->cancel_begin >= lane->cancel_end ||
|
||
lane->cancel_end > lane->head_sequence))) { return 0; }
|
||
count = (lane->next_sequence - lane->head_sequence) / 16U;
|
||
if (count > UINT32_MAX - high_count) { return 0; }
|
||
high_count += (uint32_t)count;
|
||
}
|
||
for (p = 0U; p < IEC104_ASDU_PRIORITY_COUNT; ++p) {
|
||
const IEC104_AsduPriorityQueue_t *bucket = &queue->priority_queues[p];
|
||
uint32_t index = bucket->head_index, remaining = bucket->used_bytes, entries = 0U, pending = 0U;
|
||
uint32_t first_pending = bucket->tail_index;
|
||
uint64_t previous = 0U;
|
||
if (bucket->bytes == NULL || bucket->byte_capacity < Strict_Header_Length((IEC104_AsduPriority_t)p) + IEC104_ASDU_HEADER_LEN ||
|
||
index >= bucket->byte_capacity || bucket->tail_index >= bucket->byte_capacity ||
|
||
bucket->unsubmitted_index >= bucket->byte_capacity || remaining > bucket->byte_capacity ||
|
||
Ring_Advance(bucket, index, remaining) != bucket->tail_index ||
|
||
(remaining == 0U && (index != 0U || bucket->tail_index != 0U))) { return 0; }
|
||
while (remaining != 0U) {
|
||
StrictEntry entry;
|
||
uint32_t size;
|
||
if (!Strict_Read(queue, (IEC104_AsduPriority_t)p, index, remaining, &entry)) { return 0; }
|
||
size = Strict_Header_Length((IEC104_AsduPriority_t)p) + entry.length;
|
||
if (p == IEC104_ASDU_PRIORITY_HIGH) {
|
||
const IEC104_AsduPriorityTicketState_t *lane = &queue->priority_state->lanes[entry.rank - 1U];
|
||
if (entry.serial < lane->head_sequence || entry.serial >= lane->next_sequence ||
|
||
entry.serial >= queue->high_next_sequence ||
|
||
entry.serial != lane->head_sequence + (uint64_t)seen[entry.rank - 1U] * 16U) { return 0; }
|
||
++seen[entry.rank - 1U];
|
||
}
|
||
if (entry.admission_order <= previous) { return 0; }
|
||
previous = entry.admission_order;
|
||
if (entry.state == NORMAL_HEADER_OVERWRITTEN) { ++overwritten; }
|
||
else {
|
||
++entries;
|
||
if (entry.state == NORMAL_HEADER_PENDING) {
|
||
if (pending == 0U) { first_pending = index; }
|
||
++pending;
|
||
}
|
||
}
|
||
index = Ring_Advance(bucket, index, size);
|
||
remaining -= size;
|
||
}
|
||
if (entries != bucket->entry_count || pending != bucket->unsubmitted_count ||
|
||
first_pending != bucket->unsubmitted_index ||
|
||
(p == IEC104_ASDU_PRIORITY_HIGH && entries != high_count)) { return 0; }
|
||
}
|
||
if (overwritten != queue->overwritten_unacked_count) { return 0; }
|
||
return queue->connection_generation != 0U ||
|
||
(high_count == 0U && overwritten == 0U && queue->normal_send_limited == 0U &&
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL].entry_count ==
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL].unsubmitted_count);
|
||
}
|
||
|
||
/**
|
||
* 功能:将已验证的描述头转换成只读借用视图,跨环尾原文分为first/second两段,不复制ASDU。
|
||
* 输入参数:queue——ASDU队列。
|
||
* priority——优先级。
|
||
* index——索引。
|
||
* entry——const StrictEntry *所指数据。
|
||
* info——查询信息。
|
||
* 输出参数:info——查询信息的更新结果。
|
||
* 返回值:无。
|
||
* 说明:NORMAL身份用于原文ACK回收,HIGH票据用于交接查询,两者不能互换;后续修改队列前须结束字节借用。
|
||
*/
|
||
static void Strict_Fill_Info(const IEC104_AsduQueue_t *queue, IEC104_AsduPriority_t priority,
|
||
uint32_t index, const StrictEntry *entry, IEC104_AsduHeadInfo_t *info)
|
||
{
|
||
const IEC104_AsduPriorityQueue_t *bucket = &queue->priority_queues[priority];
|
||
uint32_t data_index = Ring_Advance(bucket, index, Strict_Header_Length(priority));
|
||
uint32_t first = bucket->byte_capacity - data_index;
|
||
memset(info, 0, sizeof(*info));
|
||
if (first > entry->length) { first = entry->length; }
|
||
info->entry_id.connection_generation = queue->connection_generation;
|
||
info->entry_id.entry_serial = priority == IEC104_ASDU_PRIORITY_NORMAL ? entry->serial : 0U;
|
||
info->priority = priority;
|
||
info->business_priority = (IEC104_BusinessPriority_t)entry->rank;
|
||
info->response_ticket = priority == IEC104_ASDU_PRIORITY_HIGH ? entry->serial : 0U;
|
||
info->admission_order = entry->admission_order;
|
||
info->asdu_first = bucket->bytes + data_index;
|
||
info->asdu_first_length = (uint16_t)first;
|
||
info->asdu_second = first < entry->length ? bucket->bytes : NULL;
|
||
info->asdu_length = entry->length;
|
||
info->entry_header_length = (uint16_t)Strict_Header_Length(priority);
|
||
info->is_submitted = entry->state == NORMAL_HEADER_RETAINED_UNACKED ? 1U : 0U;
|
||
}
|
||
|
||
/**
|
||
* 功能:pending_only时挑最小等级并保持同级最早入队项;否则按保留原文序位查看,跳过覆盖身份头。
|
||
* 输入参数:queue——ASDU队列。
|
||
* priority——优先级。
|
||
* pending_only——int值。
|
||
* offset——偏移。
|
||
* info——查询信息。
|
||
* selected_index——选中索引。
|
||
* 输出参数:info——查询信息的更新结果。
|
||
* selected_index——选中索引的更新结果。
|
||
* 返回值:按处理分支返回IEC104_ASDU_QUEUE_EMPTY、IEC104_ASDU_QUEUE_ERROR、IEC104_ASDU_QUEUE_OK。
|
||
* 说明:同级不替换已有候选,物理受理顺序因此保持FIFO;Manager再用admission_order比较两个环的同级候选。
|
||
* 这里只借用原文字节,不移动条目;重排发生在交接后的删除/压紧,不改变已经编号的I帧。
|
||
*/
|
||
static IEC104_AsduQueueResult_t Strict_Peek(const IEC104_AsduQueue_t *queue, IEC104_AsduPriority_t priority,
|
||
int pending_only, uint32_t offset, IEC104_AsduHeadInfo_t *info,
|
||
uint32_t *selected_index)
|
||
{
|
||
const IEC104_AsduPriorityQueue_t *bucket = &queue->priority_queues[priority];
|
||
uint32_t index = bucket->head_index, remaining = bucket->used_bytes, selected = 0U;
|
||
StrictEntry best;
|
||
int found = 0;
|
||
if (pending_only && priority == IEC104_ASDU_PRIORITY_NORMAL &&
|
||
(queue->startup_pending != 0U || queue->normal_send_limited != 0U)) { return IEC104_ASDU_QUEUE_EMPTY; }
|
||
while (remaining != 0U) {
|
||
StrictEntry entry;
|
||
uint32_t size;
|
||
if (!Strict_Read(queue, priority, index, remaining, &entry)) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
size = Strict_Header_Length(priority) + entry.length;
|
||
if (entry.state != NORMAL_HEADER_OVERWRITTEN && (!pending_only || entry.state == NORMAL_HEADER_PENDING)) {
|
||
if (pending_only) {
|
||
if (!found || entry.rank < best.rank) { best = entry; selected = index; found = 1; }
|
||
} else if (offset-- == 0U) {
|
||
best = entry; selected = index; found = 1; break;
|
||
}
|
||
}
|
||
index = Ring_Advance(bucket, index, size);
|
||
remaining -= size;
|
||
}
|
||
if (!found) { return IEC104_ASDU_QUEUE_EMPTY; }
|
||
Strict_Fill_Info(queue, priority, selected, &best, info);
|
||
if (selected_index != NULL) { *selected_index = selected; }
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:删除逻辑连续区间,只压紧其后的字节;不需要另一份ASDU数组,也不按物理下标大小判断逻辑先后。
|
||
* 输入参数:bucket——IEC104_AsduPriorityQueue_t *所指数据。
|
||
* index——索引。
|
||
* size——存储大小。
|
||
* 输出参数:bucket——IEC104_AsduPriorityQueue_t *所指数据的更新结果。
|
||
* 返回值:无。
|
||
* 说明:压紧可能移动调用方尚未选中的条目,统一使本环此前全部原文借用失效;不能只失效被删除项。
|
||
*/
|
||
static void Strict_Delete_Bytes(IEC104_AsduPriorityQueue_t *bucket, uint32_t index, uint32_t size)
|
||
{
|
||
uint32_t offset = index >= bucket->head_index ? index - bucket->head_index :
|
||
bucket->byte_capacity - bucket->head_index + index;
|
||
uint32_t suffix = bucket->used_bytes - offset - size;
|
||
if (index == bucket->head_index) { bucket->head_index = Ring_Advance(bucket, index, size); }
|
||
else if (suffix != 0U) {
|
||
Move_Normal_Entry_Forward(bucket, index, Ring_Advance(bucket, index, size), suffix);
|
||
}
|
||
bucket->used_bytes -= size;
|
||
bucket->tail_index = Ring_Advance(bucket, bucket->head_index, bucket->used_bytes);
|
||
if (bucket->used_bytes == 0U) { bucket->head_index = bucket->tail_index = 0U; }
|
||
}
|
||
|
||
/**
|
||
* 功能:修改后恢复公共计数;已覆盖头只占字节和overwritten计数,不冒充仍保留的ASDU。
|
||
* 输入参数:queue——ASDU队列。
|
||
* priority——优先级。
|
||
* 输出参数:queue——ASDU队列的更新结果。
|
||
* 返回值:无。
|
||
* 说明:因而entry_count为0不意味着used_bytes为0;未收到真实ACK时仍不能复用身份头所占空间。
|
||
*/
|
||
static void Strict_Recount(IEC104_AsduQueue_t *queue, IEC104_AsduPriority_t priority)
|
||
{
|
||
IEC104_AsduPriorityQueue_t *bucket = &queue->priority_queues[priority];
|
||
uint32_t index = bucket->head_index, remaining = bucket->used_bytes, overwritten = 0U;
|
||
bucket->entry_count = bucket->unsubmitted_count = 0U;
|
||
bucket->unsubmitted_index = bucket->tail_index;
|
||
while (remaining != 0U) {
|
||
StrictEntry entry = {0U, 0U, 0U, 0U, 0U};
|
||
uint32_t size;
|
||
(void)Strict_Read(queue, priority, index, remaining, &entry);
|
||
size = Strict_Header_Length(priority) + entry.length;
|
||
if (entry.state == NORMAL_HEADER_OVERWRITTEN) { ++overwritten; }
|
||
else {
|
||
++bucket->entry_count;
|
||
if (entry.state == NORMAL_HEADER_PENDING) {
|
||
if (bucket->unsubmitted_count == 0U) { bucket->unsubmitted_index = index; }
|
||
++bucket->unsubmitted_count;
|
||
}
|
||
}
|
||
index = Ring_Advance(bucket, index, size);
|
||
remaining -= size;
|
||
}
|
||
if (priority == IEC104_ASDU_PRIORITY_NORMAL) { queue->overwritten_unacked_count = overwritten; }
|
||
}
|
||
|
||
/**
|
||
* 功能:检查兼容模式单个字节环的边界、占用量和待发游标是否自洽。
|
||
* 输入参数:bucket——非NULL控制结构;priority——已验证的优先级,用于确定本级头长。
|
||
* 输出参数:无,不读取条目头或修改字节。
|
||
* 返回值:控制字段自洽返回1,否则0。
|
||
* 说明:used_bytes只计条目头和ASDU,无回绕空隙;它区分head==tail时的满与空。
|
||
* head==tail且used_bytes为0表示空环,为byte_capacity表示满环。
|
||
* 检查为常数时间,不验证每条记录的头;完整条目布局由Bucket_Entries_Valid检查。
|
||
*/
|
||
static int Bucket_Valid(const IEC104_AsduPriorityQueue_t *bucket, IEC104_AsduPriority_t priority)
|
||
{
|
||
uint32_t unsubmitted_distance;
|
||
uint32_t minimum_size = Entry_Header_Length(priority) + IEC104_ASDU_HEADER_LEN;
|
||
|
||
if (bucket->bytes == NULL || bucket->byte_capacity < minimum_size ||
|
||
bucket->head_index >= bucket->byte_capacity || bucket->tail_index >= bucket->byte_capacity ||
|
||
bucket->unsubmitted_index >= bucket->byte_capacity || bucket->used_bytes > bucket->byte_capacity ||
|
||
bucket->unsubmitted_count > bucket->entry_count ||
|
||
bucket->entry_count > bucket->used_bytes / minimum_size) {
|
||
return 0;
|
||
}
|
||
if (bucket->entry_count == 0U) {
|
||
return bucket->used_bytes == 0U && bucket->head_index == 0U &&
|
||
bucket->tail_index == 0U && bucket->unsubmitted_index == 0U;
|
||
}
|
||
/* 条目紧接存放,从队首经过全部占用字节后必须正好到队尾。 */
|
||
if (Ring_Advance(bucket, bucket->head_index, bucket->used_bytes) != bucket->tail_index) {
|
||
return 0;
|
||
}
|
||
if (bucket->unsubmitted_count == 0U) {
|
||
return bucket->unsubmitted_index == bucket->tail_index;
|
||
}
|
||
if (bucket->unsubmitted_count == bucket->entry_count) {
|
||
return bucket->unsubmitted_index == bucket->head_index;
|
||
}
|
||
/* 同时存在保留待ACK前缀和待发后缀时,待发游标应严格位于占用区内部。 */
|
||
unsubmitted_distance = bucket->unsubmitted_index >= bucket->head_index ?
|
||
bucket->unsubmitted_index - bucket->head_index :
|
||
bucket->byte_capacity - bucket->head_index + bucket->unsubmitted_index;
|
||
return unsubmitted_distance != 0U && unsubmitted_distance < bucket->used_bytes;
|
||
}
|
||
|
||
/**
|
||
* 功能:检查业务队列的初始化标志、连接身份及两个字节环。
|
||
* 输入参数:queue——待检查对象,允许NULL。
|
||
* 输出参数:无。
|
||
* 返回值:状态合法返回1,否则0;兼容模式检查控制字段,严格模式还须逐条验证布局,不检查TCP状态。
|
||
* 说明:NORMAL覆盖待确认计数与身份边界须一致;原文已覆盖不代表协议已确认。
|
||
* HIGH原文在登记交接时出队,因此其保留条目数必须等于待发条目数。
|
||
* 代次0表示离线缓存,HIGH、已提交记账及EOF发送限制必须为空;具体代次匹配由入口核对。
|
||
*/
|
||
static int Queue_State_Valid(const IEC104_AsduQueue_t *queue)
|
||
{
|
||
uint64_t overwritten_span;
|
||
|
||
if (queue == NULL || queue->is_initialized != 1U || !Soe_Valid(queue)) { return 0; }
|
||
if (queue != NULL && queue->strict_business_priority != 0U) { return Strict_Queue_Valid(queue); }
|
||
|
||
if (queue == NULL || queue->is_initialized != 1U ||
|
||
queue->next_entry_serial == 0U ||
|
||
queue->high_head_sequence == 0U || queue->high_next_sequence < queue->high_head_sequence ||
|
||
queue->high_next_sequence - queue->high_head_sequence !=
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_HIGH].entry_count ||
|
||
queue->high_known_from == 0U || queue->high_known_from > queue->high_head_sequence ||
|
||
!Bucket_Valid(&queue->priority_queues[IEC104_ASDU_PRIORITY_HIGH], IEC104_ASDU_PRIORITY_HIGH) ||
|
||
!Bucket_Valid(&queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL], IEC104_ASDU_PRIORITY_NORMAL) ||
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_HIGH].entry_count !=
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_HIGH].unsubmitted_count) {
|
||
return 0;
|
||
}
|
||
if (queue->normal_send_limited > 1U || queue->startup_pending > 1U) {
|
||
return 0;
|
||
}
|
||
if (queue->connection_generation == 0U &&
|
||
(queue->priority_queues[IEC104_ASDU_PRIORITY_HIGH].entry_count != 0U ||
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL].entry_count !=
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL].unsubmitted_count ||
|
||
queue->overwritten_unacked_count != 0U || queue->normal_send_limited != 0U)) {
|
||
return 0;
|
||
}
|
||
if (queue->overwritten_unacked_count == 0U) {
|
||
/* 覆盖前缀已清完时不保留旧边界,避免已确认的历史编号继续占住窗口。 */
|
||
return queue->overwritten_unacked_serial_end == 0U && queue->overwritten_unacked_serial_begin == 0U;
|
||
}
|
||
if (!Normal_Serial_Before(queue, queue->overwritten_unacked_serial_begin, queue->next_entry_serial) ||
|
||
!Normal_Serial_Before(queue, queue->overwritten_unacked_serial_end, queue->next_entry_serial)) {
|
||
return 0;
|
||
}
|
||
overwritten_span = Entry_Serial_Distance(queue->overwritten_unacked_serial_begin,
|
||
queue->overwritten_unacked_serial_end);
|
||
/* 区间含两端且止于next之前;丢弃未提交NORMAL可留下间隔,数量不超过span+1。 */
|
||
return overwritten_span < Entry_Serial_Distance(queue->overwritten_unacked_serial_begin, queue->next_entry_serial) &&
|
||
queue->overwritten_unacked_count <= overwritten_span + 1U;
|
||
}
|
||
|
||
/**
|
||
* 功能:读取条目并核对预期的队列旧状态,返回最多两段ASDU原文的借用指针。
|
||
* 输入参数:queue/priority——已通过容器和优先级检查的队列与桶;
|
||
* index——候选条目头下标;expected_queue_state——预期的QUEUE_ENTRY_*记账状态。
|
||
* 输出参数:info——非NULL,仅成功时写入身份、长度及两段只读借用指针。
|
||
* 返回值:条目头和占用范围通过检查返回1,否则0,失败不改info;不校验ASDU业务内容。
|
||
* 说明:先检查整个条目头属于占用区,再用Ring_Read处理回绕和未对齐数值。
|
||
* index由有效游标或条目边界推导;不扫描前序条目,也不移动原文。
|
||
* 该参数只校验读取时的状态,不设置新状态,也不检查SendBuffer或发送结果。
|
||
* NORMAL头编码映射为记账状态后比较;HIGH环内条目恒为PENDING且没有条目编号。
|
||
* NORMAL头内的1/2对应输出is_submitted的0/1,不能直接拿头内编码与预期记账状态比较。
|
||
* 本函数仅供兼容模式使用;NORMAL编号须位于next之前的半环窗口内,并晚于已覆盖待ACK前缀。
|
||
* 输出指针指向队列存储,不复制ASDU;调用方只能在条目借用仍有效期间读取原文。
|
||
*/
|
||
static int Read_Entry_With_Expected_State(const IEC104_AsduQueue_t *queue, IEC104_AsduPriority_t priority,
|
||
uint32_t index, uint8_t expected_queue_state,
|
||
IEC104_AsduHeadInfo_t *info)
|
||
{
|
||
const IEC104_AsduPriorityQueue_t *bucket = &queue->priority_queues[priority];
|
||
IEC104_AsduHeadInfo_t prepared;
|
||
IEC104_AsduNormalEntryHeader_t header;
|
||
uint32_t header_length = Entry_Header_Length(priority);
|
||
uint32_t distance, asdu_index, first_length;
|
||
uint16_t encoded_length;
|
||
|
||
memset(&prepared, 0, sizeof(prepared));
|
||
|
||
if (index >= bucket->byte_capacity) {
|
||
return 0;
|
||
}
|
||
distance = index >= bucket->head_index ? index - bucket->head_index :
|
||
bucket->byte_capacity - bucket->head_index + index;
|
||
if (distance >= bucket->used_bytes || bucket->used_bytes - distance < header_length) {
|
||
return 0;
|
||
}
|
||
if (priority == IEC104_ASDU_PRIORITY_HIGH) {
|
||
Ring_Read(bucket, index, &encoded_length, sizeof(encoded_length));
|
||
prepared.asdu_length = encoded_length;
|
||
prepared.entry_id.entry_serial = 0U;
|
||
prepared.is_submitted = QUEUE_ENTRY_PENDING;
|
||
} else {
|
||
Ring_Read(bucket, index, &header, sizeof(header));
|
||
if ((header.entry_state != NORMAL_HEADER_PENDING && header.entry_state != NORMAL_HEADER_RETAINED_UNACKED) ||
|
||
!Normal_Serial_Before(queue, header.entry_serial, queue->next_entry_serial) ||
|
||
(queue->overwritten_unacked_count != 0U &&
|
||
!Normal_Serial_Before(queue, queue->overwritten_unacked_serial_end, header.entry_serial))) {
|
||
return 0;
|
||
}
|
||
prepared.asdu_length = (uint16_t)header.asdu_length;
|
||
prepared.entry_id.entry_serial = header.entry_serial;
|
||
prepared.is_submitted = header.entry_state == NORMAL_HEADER_RETAINED_UNACKED;
|
||
}
|
||
if (prepared.asdu_length < IEC104_ASDU_HEADER_LEN || prepared.asdu_length > IEC104_MAX_ASDU_LEN ||
|
||
(uint32_t)prepared.asdu_length + header_length > bucket->used_bytes - distance ||
|
||
prepared.is_submitted != expected_queue_state) {
|
||
return 0;
|
||
}
|
||
asdu_index = Ring_Advance(bucket, index, header_length);
|
||
first_length = bucket->byte_capacity - asdu_index;
|
||
if (first_length > prepared.asdu_length) {
|
||
first_length = prepared.asdu_length;
|
||
}
|
||
prepared.entry_id.connection_generation = queue->connection_generation;
|
||
prepared.entry_header_length = (uint16_t)header_length;
|
||
prepared.priority = priority;
|
||
prepared.business_priority = priority == IEC104_ASDU_PRIORITY_HIGH ?
|
||
IEC104_BUSINESS_PRIORITY_INTERROGATION : IEC104_BUSINESS_PRIORITY_SEQUENCE_EVENT;
|
||
prepared.response_ticket = priority == IEC104_ASDU_PRIORITY_HIGH ? queue->high_head_sequence : 0U;
|
||
prepared.admission_order = priority == IEC104_ASDU_PRIORITY_NORMAL ? prepared.entry_id.entry_serial : 0U;
|
||
prepared.asdu_first = bucket->bytes + asdu_index;
|
||
prepared.asdu_first_length = (uint16_t)first_length;
|
||
prepared.asdu_second = first_length < prepared.asdu_length ? bucket->bytes : NULL;
|
||
*info = prepared;
|
||
return 1;
|
||
}
|
||
|
||
/**
|
||
* 功能:在入队写字节前,检查分配本次编号后能否继续维持半环窗口。
|
||
* 输入参数:queue——非NULL、控制字段已检查的副本,已完成本次NORMAL覆盖预处理。
|
||
* 输出参数:无,不修改副本或字节数组。
|
||
* 返回值:OK允许推进;FULL表示旧编号占住窗口;ERROR表示物理队首条目非法。
|
||
* 说明:next是NORMAL本次入队后的下一编号;NORMAL物理队首及覆盖区间下界均须早于它。
|
||
* 此预检仅用于兼容NORMAL的FIFO编号,只需检查队首及覆盖前缀,开销与条目总数无关。
|
||
* 已丢弃的未提交条目不再占窗口;已覆盖但待ACK的条目仍占窗口。
|
||
* FULL时须先处理旧条目或ACK再重试,不能越过窗口继续分配编号。
|
||
* 本函数只预检,不推进next_entry_serial;因此即使字节空间足够,也可能因编号窗口返回FULL。
|
||
*/
|
||
static IEC104_AsduQueueResult_t Check_Entry_Serial_Advance(const IEC104_AsduQueue_t *queue)
|
||
{
|
||
IEC104_AsduHeadInfo_t oldest;
|
||
const IEC104_AsduPriorityQueue_t *bucket = &queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL];
|
||
uint64_t next = Entry_Serial_Next(queue->next_entry_serial);
|
||
|
||
if (queue->soe.capacity != 0U) {
|
||
return queue->next_entry_serial == UINT64_MAX ? IEC104_ASDU_QUEUE_FULL : IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
if (queue->overwritten_unacked_count != 0U &&
|
||
!Entry_Serial_Before(queue->overwritten_unacked_serial_begin, next)) {
|
||
return IEC104_ASDU_QUEUE_FULL;
|
||
}
|
||
if (bucket->entry_count != 0U) {
|
||
if (!Read_Entry_With_Expected_State(queue, IEC104_ASDU_PRIORITY_NORMAL, bucket->head_index,
|
||
bucket->entry_count > bucket->unsubmitted_count ?
|
||
QUEUE_ENTRY_RETAINED_UNACKED : QUEUE_ENTRY_PENDING, &oldest)) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
if (!Entry_Serial_Before(oldest.entry_id.entry_serial, next)) {
|
||
return IEC104_ASDU_QUEUE_FULL;
|
||
}
|
||
}
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:计算当前条目之后的位置,完整条目按实际字节数跨尾部回绕。
|
||
* 输入参数:bucket——有效字节环;index/info为已验证条目位置和含实际头长的描述。
|
||
* 输出参数:无。
|
||
* 返回值:下一逻辑位置,可能是下一条目或队尾。
|
||
* 说明:条目之间没有填充;有后继条目时,由调用方另行验证其头和占用范围。
|
||
*/
|
||
static uint32_t Next_Entry_Index(const IEC104_AsduPriorityQueue_t *bucket, uint32_t index,
|
||
const IEC104_AsduHeadInfo_t *info)
|
||
{
|
||
return Ring_Advance(bucket, index, (uint32_t)info->entry_header_length + info->asdu_length);
|
||
}
|
||
|
||
/**
|
||
* 功能:调用方已验证非回绕的HIGH编号;仅保存最新一次非空取消区间[begin,end)。
|
||
* 输入参数:queue——ASDU队列。
|
||
* begin——uint64_t值。
|
||
* end——uint64_t值。
|
||
* 输出参数:queue——ASDU队列的更新结果。
|
||
* 返回值:无。
|
||
* 说明:覆盖旧区间前,把可信历史下界推进到旧end;否则旧取消票会因已小于head而误报HANDED。
|
||
* 更早票统一保守视为CANCELLED,空区间不冲掉仍有用的历史;这里不移动队列或处理ACK。
|
||
*/
|
||
static void Record_High_Cancellation(IEC104_AsduQueue_t *queue, uint64_t begin, uint64_t end)
|
||
{
|
||
if (begin == end) { return; }
|
||
if (queue->high_cancel_end > queue->high_known_from) { queue->high_known_from = queue->high_cancel_end; }
|
||
queue->high_cancel_begin = begin;
|
||
queue->high_cancel_end = end;
|
||
}
|
||
|
||
/**
|
||
* 功能:将NORMAL条目头从待发状态改为保留原文等待ACK。
|
||
* 输入参数:bucket/index——有效NORMAL字节环和已验证的待发条目头位置。
|
||
* 输出参数:bucket->bytes中的entry_state改为NORMAL_HEADER_RETAINED_UNACKED,编号、长度及原文不变。
|
||
* 返回值:无;本函数不再检查身份和旧状态,调用方须先完成相关预检及发送交接。
|
||
* 说明:写入的是头内编码2;对应公开视图is_submitted为1,两者数值含义不同。
|
||
* 只更新条目头,待发计数和游标由Mark_Submitted随后更新;memcpy处理未对齐及回绕。
|
||
*/
|
||
static void Mark_Normal_Entry_Retained_Unacked(const IEC104_AsduPriorityQueue_t *bucket, uint32_t index)
|
||
{
|
||
IEC104_AsduNormalEntryHeader_t header;
|
||
|
||
Ring_Read(bucket, index, &header, sizeof(header));
|
||
header.entry_state = NORMAL_HEADER_RETAINED_UNACKED;
|
||
Ring_Write(bucket, index, &header, sizeof(header));
|
||
}
|
||
|
||
/**
|
||
* 功能:绑定调用方提供的两块字节数组,建立空的两级业务队列。
|
||
* 输入参数:high_bytes/normal_bytes——持续有效且互不重叠的数组;
|
||
* high_capacity/normal_capacity——字节容量,分别至少对应优先级的MIN_CAPACITY;
|
||
* connection_generation——0建立服务离线队列;非零值表示绑定到不复用的连接代次。
|
||
* 输出参数:queue——非NULL,仅成功时整体写入初始化结果。
|
||
* 返回值:OK表示成功,ERROR表示参数非法;失败不修改queue或字节数组。
|
||
* 说明:不分配内存,不清零原文字节;重新Init会丢弃旧条目引用,恢复业务应使用Rebind。
|
||
* 最小容量只保证最短ASDU;支持最大HIGH ASDU需251字节。
|
||
* 数组在队列使用期间须持续有效;本函数只检查两起始地址不同,完整地址区间不重叠由调用方保证。
|
||
* NORMAL的next_entry_serial从1开始;HIGH从非零入参起点开始,旧票保守取消;各队列初始为空。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Init(
|
||
IEC104_AsduQueue_t *queue, uint8_t *high_bytes, uint32_t high_capacity,
|
||
uint8_t *normal_bytes, uint32_t normal_capacity, uint64_t connection_generation, uint64_t high_next_sequence)
|
||
{
|
||
IEC104_AsduQueue_t prepared;
|
||
|
||
if (queue == NULL || high_bytes == NULL || normal_bytes == NULL || high_bytes == normal_bytes ||
|
||
high_capacity < IEC104_ASDU_HIGH_QUEUE_MIN_CAPACITY ||
|
||
normal_capacity < IEC104_ASDU_NORMAL_QUEUE_MIN_CAPACITY || high_next_sequence == 0U) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
memset(&prepared, 0, sizeof(prepared));
|
||
prepared.priority_queues[IEC104_ASDU_PRIORITY_HIGH].bytes = high_bytes;
|
||
prepared.priority_queues[IEC104_ASDU_PRIORITY_HIGH].byte_capacity = high_capacity;
|
||
prepared.priority_queues[IEC104_ASDU_PRIORITY_NORMAL].bytes = normal_bytes;
|
||
prepared.priority_queues[IEC104_ASDU_PRIORITY_NORMAL].byte_capacity = normal_capacity;
|
||
prepared.connection_generation = connection_generation;
|
||
prepared.next_entry_serial = 1U;
|
||
prepared.high_head_sequence = high_next_sequence;
|
||
prepared.high_next_sequence = high_next_sequence;
|
||
prepared.high_known_from = high_next_sequence;
|
||
prepared.is_initialized = 1U;
|
||
*queue = prepared;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:绑定调用方持有的逐等级票据存储,只保存指针;存储须独占且覆盖队列使用寿命。
|
||
* 输入参数:queue——ASDU队列。
|
||
* state——当前状态。
|
||
* 输出参数:queue——ASDU队列的更新结果。
|
||
* 返回值:按处理分支返回IEC104_ASDU_QUEUE_ERROR、IEC104_ASDU_QUEUE_OK。
|
||
* 说明:仅空队列的兼容状态允许绑定,切换严格模式时才初始化各等级,失败不替换已有绑定。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Bind_Priority_State(
|
||
IEC104_AsduQueue_t *queue, IEC104_AsduQueuePriorityState_t *state)
|
||
{
|
||
if (!Queue_State_Valid(queue) || state == NULL || queue->strict_business_priority != 0U ||
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_HIGH].used_bytes != 0U ||
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL].used_bytes != 0U ||
|
||
queue->overwritten_unacked_count != 0U) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
queue->priority_state = state;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:只在两个原文环及覆盖身份均为空时切换,避免用新头格式解释旧条目。
|
||
* 输入参数:queue——ASDU队列。
|
||
* enabled——使能标志。
|
||
* 输出参数:queue——ASDU队列的更新结果。
|
||
* 返回值:按处理分支返回IEC104_ASDU_QUEUE_ERROR、IEC104_ASDU_QUEUE_OK。
|
||
* 说明:启用时从已有高水位之后建立各等级票据区间;关闭不回退高水位,不复用已对外发布的票号。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Set_Strict_Priority(IEC104_AsduQueue_t *queue, uint8_t enabled)
|
||
{
|
||
uint64_t base;
|
||
unsigned rank;
|
||
IEC104_AsduSoeStatus_t soe;
|
||
if (!Queue_State_Valid(queue) || enabled > 1U ||
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_HIGH].used_bytes != 0U ||
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL].used_bytes != 0U ||
|
||
queue->overwritten_unacked_count != 0U) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
Soe_Status(queue, &soe);
|
||
if (soe.stored_count != 0U || soe.retired_count != 0U) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
if (enabled == queue->strict_business_priority) { return IEC104_ASDU_QUEUE_OK; }
|
||
if (enabled != 0U) {
|
||
if (queue->priority_state == NULL ||
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_HIGH].byte_capacity <
|
||
IEC104_ASDU_STRICT_HIGH_ENTRY_HEADER_LEN + IEC104_ASDU_HEADER_LEN ||
|
||
queue->high_next_sequence > UINT64_MAX - 31U) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
/* 起点向上取整,每个等级占一个模16余数;所有新票严格高于此前保存的票据高水位。 */
|
||
base = (queue->high_next_sequence + 15U) & ~(uint64_t)15U;
|
||
memset(queue->priority_state, 0, sizeof(*queue->priority_state));
|
||
for (rank = 1U; rank < IEC104_BUSINESS_PRIORITY_COUNT; ++rank) {
|
||
IEC104_AsduPriorityTicketState_t *lane = &queue->priority_state->lanes[rank - 1U];
|
||
lane->head_sequence = lane->next_sequence = lane->known_from = base + rank;
|
||
}
|
||
}
|
||
queue->high_head_sequence = queue->high_known_from = queue->high_next_sequence;
|
||
queue->high_cancel_begin = queue->high_cancel_end = 0U;
|
||
queue->strict_business_priority = enabled;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:读取连接代次及ASDU条目数,区分原文仍保留、原文仍保留且已提交待确认、原文已覆盖且待确认。
|
||
* 输入参数:queue——已初始化的业务队列。
|
||
* 输出参数:status——非NULL,仅OK时写入数值副本。
|
||
* 返回值:OK或ERROR;失败不修改输出。
|
||
* 说明:HIGH提交时出队,其retained_unacked_count恒为0,未确认HIGH数量由Manager发送记录维护。
|
||
* NORMAL的retained_unacked_count加overwritten_unacked_count得到NORMAL已提交待确认条目数。
|
||
* is_storage_empty只表示两个ASDU原文环为空,HIGH或NORMAL仍可能有对应I帧未ACK;查询不预留容量。
|
||
* 各count以ASDU条目为单位,不是字节数;NORMAL汇总普通环及单对象SOE,占位计保留原文但不计已提交。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Get_Status(
|
||
const IEC104_AsduQueue_t *queue, IEC104_AsduQueueStatus_t *status)
|
||
{
|
||
IEC104_AsduQueueStatus_t prepared;
|
||
IEC104_AsduSoeStatus_t soe;
|
||
unsigned i;
|
||
|
||
if (!Queue_State_Valid(queue) || status == NULL) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
prepared.connection_generation = queue->connection_generation;
|
||
prepared.discarded_asdu_count = queue->discarded_asdu_count;
|
||
prepared.overwritten_unacked_count = queue->overwritten_unacked_count;
|
||
prepared.is_storage_empty = 1U;
|
||
for (i = 0U; i < IEC104_ASDU_PRIORITY_COUNT; ++i) {
|
||
const IEC104_AsduPriorityQueue_t *bucket = &queue->priority_queues[i];
|
||
prepared.entry_count[i] = bucket->entry_count;
|
||
prepared.retained_unacked_count[i] = bucket->entry_count - bucket->unsubmitted_count;
|
||
if (bucket->entry_count != 0U) {
|
||
prepared.is_storage_empty = 0U;
|
||
}
|
||
}
|
||
Soe_Status(queue, &soe);
|
||
prepared.entry_count[IEC104_ASDU_PRIORITY_NORMAL] += soe.stored_count;
|
||
prepared.retained_unacked_count[IEC104_ASDU_PRIORITY_NORMAL] += soe.retained_unacked_count;
|
||
prepared.overwritten_unacked_count += soe.retired_count;
|
||
if (soe.stored_count != 0U) { prepared.is_storage_empty = 0U; }
|
||
*status = prepared;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:给空SOE区绑定独立槽和精确退休身份表。
|
||
* 输入参数:queue——已初始化队列;slots/capacity——1..256个26字节槽;retired/retired_capacity——非空身份表。
|
||
* 输出参数:queue保存借用指针;成功时清零两个外借数组及SOE累计统计。
|
||
* 返回值:OK成功;ERROR参数非法、普通NORMAL非空或SOE仍有原文/退休身份。
|
||
* 说明:初次绑定时普通NORMAL必须为空,避免把旧循环编号切入不回绕的共享编号域。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Bind_Soe_Storage(IEC104_AsduQueue_t *queue,
|
||
uint8_t *slots, uint16_t capacity, uint64_t *retired, uint16_t retired_capacity)
|
||
{
|
||
IEC104_AsduSoeStatus_t state;
|
||
if (!Queue_State_Valid(queue) || slots == NULL || retired == NULL ||
|
||
capacity == 0U || capacity > IEC104_ASDU_SOE_MAX_CAPACITY || retired_capacity == 0U ||
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL].used_bytes != 0U ||
|
||
queue->overwritten_unacked_count != 0U) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
Soe_Status(queue, &state);
|
||
if (state.stored_count != 0U || state.retired_count != 0U) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
memset(slots, 0, (size_t)capacity * IEC104_ASDU_SOE_SLOT_LEN);
|
||
memset(retired, 0, (size_t)retired_capacity * sizeof(*retired));
|
||
memset(&queue->soe, 0, sizeof(queue->soe));
|
||
queue->soe.slots = slots; queue->soe.capacity = capacity;
|
||
queue->soe.retired = retired; queue->soe.retired_capacity = retired_capacity;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:将完整单对象SOE复制到独立槽,满时仅淘汰最旧SOE。
|
||
* 输入参数:queue/generation——已绑定SOE区及当前代次;bytes/length——17字节Type30/31 COT3;rank——业务等级;deferred——是否先占位。
|
||
* 输出参数:serial——成功时返回受理身份;queue及外借存储——成功时提交覆盖与新事件。
|
||
* 返回值:OK、FULL或ERROR;失败保持队列、原文及serial输出。
|
||
* 说明:覆盖已交接事件先预留退休身份;不撤回发送帧,不对同点事件去重。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Enqueue_Soe(IEC104_AsduQueue_t *queue,
|
||
uint64_t generation, const uint8_t *bytes, uint16_t length, IEC104_BusinessPriority_t rank,
|
||
bool deferred, uint64_t *serial)
|
||
{
|
||
uint8_t *target = NULL, *oldest = NULL;
|
||
uint64_t *retired = NULL;
|
||
uint64_t allocated;
|
||
uint16_t i;
|
||
unsigned old_state;
|
||
if (!Queue_State_Valid(queue) || queue->soe.capacity == 0U || serial == NULL ||
|
||
generation != queue->connection_generation || !Business_Priority_Valid(rank) ||
|
||
!Soe_Asdu_Valid(bytes, length)) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
allocated = queue->next_entry_serial | UINT64_C(1);
|
||
if (allocated == UINT64_MAX) { return IEC104_ASDU_QUEUE_FULL; }
|
||
for (i = 0U; i < queue->soe.capacity; ++i) {
|
||
uint8_t *slot = queue->soe.slots + (uint32_t)i * IEC104_ASDU_SOE_SLOT_LEN;
|
||
if ((slot[8] & 3U) == SOE_FREE) { if (target == NULL) { target = slot; } }
|
||
else if (oldest == NULL || Soe_Serial(slot) < Soe_Serial(oldest)) { oldest = slot; }
|
||
}
|
||
if (target == NULL) { target = oldest; }
|
||
old_state = target[8] & 3U;
|
||
if (old_state == SOE_UNACKED) {
|
||
for (i = 0U; i < queue->soe.retired_capacity; ++i) {
|
||
if (queue->soe.retired[i] == 0U) { retired = &queue->soe.retired[i]; break; }
|
||
}
|
||
if (retired == NULL) { return IEC104_ASDU_QUEUE_FULL; }
|
||
}
|
||
if (retired != NULL) {
|
||
*retired = Soe_Serial(target);
|
||
++queue->soe.overwritten_unacked_count;
|
||
} else if (old_state != SOE_FREE) { ++queue->soe.dropped_pending_count; }
|
||
memcpy(target, &allocated, sizeof(allocated));
|
||
target[8] = (uint8_t)(((unsigned)rank << 2) | (deferred ? SOE_DEFERRED : SOE_PENDING));
|
||
memcpy(target + 9U, bytes, IEC104_ASDU_SOE_ASDU_LEN);
|
||
queue->next_entry_serial = allocated + 1U;
|
||
*serial = allocated;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:发布或取消一个尚未决定的SOE占位。
|
||
* 输入参数:queue——有效队列;serial——占位身份;publish——true转待发、false删除。
|
||
* 输出参数:queue及槽——成功时改变占位状态,取消累计显式丢弃。
|
||
* 返回值:OK成功;EMPTY占位已经消失或已决定;ERROR参数或状态非法。
|
||
* 说明:不重建已覆盖身份;跨重连仍保留的占位可继续按原serial决定。
|
||
* Abort/Init后调用方须丢弃旧serial,跨服务期隔离由上层票据校验。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Resolve_Soe(IEC104_AsduQueue_t *queue,
|
||
uint64_t serial, bool publish)
|
||
{
|
||
uint8_t *slot;
|
||
if (!Queue_State_Valid(queue) || serial == 0U) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
slot = Soe_Find(queue, serial);
|
||
if (slot == NULL || (slot[8] & 3U) != SOE_DEFERRED) { return IEC104_ASDU_QUEUE_EMPTY; }
|
||
if (publish) { slot[8] = (uint8_t)((slot[8] & ~3U) | SOE_PENDING); }
|
||
else {
|
||
memset(slot, 0, IEC104_ASDU_SOE_SLOT_LEN);
|
||
++queue->soe.discarded_count; ++queue->discarded_asdu_count;
|
||
}
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:只读查询独立SOE区的容量、原文及占位数量和覆盖统计。
|
||
* 输入参数:queue——已初始化队列。
|
||
* 输出参数:status——成功时写入快照,未绑定时各项为0。
|
||
* 返回值:OK成功;ERROR参数或状态非法,失败保留输出。
|
||
* 说明:计数单位为单对象事件;原文覆盖后的退休身份不计stored_count。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Get_Soe_Status(const IEC104_AsduQueue_t *queue,
|
||
IEC104_AsduSoeStatus_t *status)
|
||
{
|
||
if (!Queue_State_Valid(queue) || status == NULL) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
Soe_Status(queue, status);
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:核对身份及预期的队列旧状态后,释放指定优先级的物理队首。
|
||
* 输入参数:queue/priority/entry_id——目标队首;expected_queue_state——删除前预期的QUEUE_ENTRY_*状态。
|
||
* 输出参数:queue——成功时更新字节占用、条目数和相关游标,不清零原文字节。
|
||
* 返回值:OK或ERROR;先在副本中检查新状态,失败不修改原队列。
|
||
* 说明:expected_queue_state只是删除前的校验条件,不是出队原因,也不是要写入的新状态。
|
||
* 出队原因由调用方决定:HIGH发送交接完成、NORMAL有效ACK、主动取消或容量覆盖。
|
||
* 本函数不访问SendBuffer或验证ACK;调用方须先满足对应前提。只移除队首,不修改覆盖待确认元数据。
|
||
* HIGH只核对代次及0编号,调用方不得跨队首变化复用旧Peek结果。
|
||
* 多个调用路径共用此入口,因此自行复核队首;即使上层刚Peek过,也保留这里的身份和状态检查。
|
||
*/
|
||
static IEC104_AsduQueueResult_t Remove_Head_With_Expected_State(
|
||
IEC104_AsduQueue_t *queue, IEC104_AsduPriority_t priority,
|
||
IEC104_AsduEntryId_t entry_id, uint8_t expected_queue_state)
|
||
{
|
||
IEC104_AsduHeadInfo_t info, next_info;
|
||
IEC104_AsduPriorityQueue_t prepared;
|
||
IEC104_AsduQueue_t updated;
|
||
uint32_t next, size;
|
||
|
||
if (Peek_Other_Head(queue, priority, &info) != IEC104_ASDU_QUEUE_OK ||
|
||
info.entry_id.connection_generation != entry_id.connection_generation ||
|
||
info.entry_id.entry_serial != entry_id.entry_serial || info.is_submitted != expected_queue_state) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
prepared = queue->priority_queues[priority];
|
||
size = (uint32_t)info.entry_header_length + info.asdu_length;
|
||
next = Next_Entry_Index(&prepared, prepared.head_index, &info);
|
||
if (prepared.entry_count > 1U && !Read_Entry_With_Expected_State(queue, priority, next,
|
||
prepared.entry_count - prepared.unsubmitted_count > 1U ?
|
||
QUEUE_ENTRY_RETAINED_UNACKED : QUEUE_ENTRY_PENDING, &next_info)) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
prepared.used_bytes -= size;
|
||
prepared.head_index = next;
|
||
prepared.entry_count--;
|
||
/* 删除待发队首时同步推进待发区;删除待ACK前缀时,待发区的位置和数量保持不变。 */
|
||
if (expected_queue_state == QUEUE_ENTRY_PENDING) {
|
||
prepared.unsubmitted_count--;
|
||
prepared.unsubmitted_index = next;
|
||
}
|
||
/* 最后一条移除后满足空队列的归零约束,下次从下标0追加;不清零原文字节。 */
|
||
if (prepared.entry_count == 0U) {
|
||
prepared.head_index = 0U;
|
||
prepared.tail_index = 0U;
|
||
prepared.unsubmitted_index = 0U;
|
||
prepared.used_bytes = 0U;
|
||
}
|
||
if (!Bucket_Valid(&prepared, priority)) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
updated = *queue;
|
||
updated.priority_queues[priority] = prepared;
|
||
if (priority == IEC104_ASDU_PRIORITY_HIGH) {
|
||
++updated.high_head_sequence;
|
||
}
|
||
*queue = updated;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:按兼容模式复制一条ASDU到指定字节环队尾,建立尚未登记发送交接的待发条目。
|
||
* 输入参数:queue/connection_generation——有效队列及匹配代次;priority——HIGH或NORMAL;
|
||
* asdu/asdu_length——完整ASDU及6..249字节长度,输入区不得与队列及其存储重叠。
|
||
* 输出参数:queue及对应字节数组——仅OK时追加条目,NORMAL另分配条目编号;不存业务标识。
|
||
* 返回值:OK成功,NORMAL可能已覆盖旧条目且不另行通知;FULL表示HIGH空间不足或NORMAL编号窗口受阻;
|
||
* ERROR表示条目超过目标桶总容量,或参数、状态、身份/覆盖计数非法。
|
||
* 说明:FULL/ERROR不修改字节、控制字段或编号;生产者仅在OK后推进业务游标。
|
||
* FULL须先释放字节空间或处理占住编号窗口的旧条目/ACK;条目超过总容量不能等待ACK解决。
|
||
* NORMAL按最旧顺序覆盖,含已提交原文;对应I帧及确认记录不删除,仍须完成和等待ACK。
|
||
* 先在控制副本上验证所有必要删除,成功后才写新字节并发布状态,避免失败时丢弃旧业务。
|
||
* 本接口不分配N(S),不解释业务对象;未提交条目被覆盖时不会产生逐帧回执。
|
||
* OK后队列已有独立的ASDU副本,调用方可复用输入缓冲;条目占用为本级头长加实际ASDU长度。
|
||
*/
|
||
static IEC104_AsduQueueResult_t Enqueue(
|
||
IEC104_AsduQueue_t *queue, uint64_t connection_generation, IEC104_AsduPriority_t priority,
|
||
const uint8_t *asdu, uint16_t asdu_length, uint64_t *ticket)
|
||
{
|
||
IEC104_AsduQueue_t prepared;
|
||
IEC104_AsduQueueResult_t result;
|
||
IEC104_AsduHeadInfo_t oldest;
|
||
IEC104_AsduNormalEntryHeader_t header;
|
||
IEC104_AsduPriorityQueue_t *bucket;
|
||
uint32_t size, index, available, header_length;
|
||
uint16_t encoded_length;
|
||
uint64_t sequence = 0U;
|
||
|
||
if (!Queue_State_Valid(queue) || connection_generation != queue->connection_generation ||
|
||
!Priority_Valid(priority) || (priority == IEC104_ASDU_PRIORITY_HIGH && connection_generation == 0U) ||
|
||
asdu == NULL || asdu_length < IEC104_ASDU_HEADER_LEN ||
|
||
asdu_length > IEC104_MAX_ASDU_LEN) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
if (priority == IEC104_ASDU_PRIORITY_NORMAL && queue->soe.capacity != 0U &&
|
||
queue->next_entry_serial == UINT64_MAX) { return IEC104_ASDU_QUEUE_FULL; }
|
||
bucket = &queue->priority_queues[priority];
|
||
header_length = Entry_Header_Length(priority);
|
||
size = header_length + (uint32_t)asdu_length;
|
||
/* 空队列也装不下属于配置与条目不匹配,不能报告为可等待空间恢复的FULL。 */
|
||
if (size > bucket->byte_capacity) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
if (priority == IEC104_ASDU_PRIORITY_HIGH && queue->high_next_sequence == UINT64_MAX) {
|
||
return IEC104_ASDU_QUEUE_ERROR; /* 上界保留为next哨兵;禁止回绕复用票号。 */
|
||
}
|
||
available = bucket->byte_capacity - bucket->used_bytes;
|
||
if (size > available && priority == IEC104_ASDU_PRIORITY_HIGH) {
|
||
return IEC104_ASDU_QUEUE_FULL;
|
||
}
|
||
prepared = *queue;
|
||
bucket = &prepared.priority_queues[priority];
|
||
/* prepared只复制控制结构,bytes仍指向原数组;此处只模拟删除,不写共享字节。
|
||
* 任一旧头或后继非法时直接返回,原字节、游标及编号都保持原样。 */
|
||
while (size > bucket->byte_capacity - bucket->used_bytes) {
|
||
if (Peek_Other_Head(&prepared, priority, &oldest) != IEC104_ASDU_QUEUE_OK ||
|
||
(oldest.is_submitted != 0U && prepared.overwritten_unacked_count == UINT32_MAX) ||
|
||
Remove_Head_With_Expected_State(&prepared, priority, oldest.entry_id,
|
||
oldest.is_submitted) != IEC104_ASDU_QUEUE_OK) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
if (oldest.is_submitted != 0U) {
|
||
if (prepared.overwritten_unacked_count == 0U) {
|
||
/* 首次覆盖已提交原文时建立区间;已有待ACK前缀时保留原下界。 */
|
||
prepared.overwritten_unacked_serial_begin = oldest.entry_id.entry_serial;
|
||
}
|
||
prepared.overwritten_unacked_count++;
|
||
prepared.overwritten_unacked_serial_end = oldest.entry_id.entry_serial;
|
||
}
|
||
}
|
||
/* 副本中的覆盖尚未发布;窗口检查失败时,原队列及原文字节仍保持调用前状态。 */
|
||
if (priority == IEC104_ASDU_PRIORITY_NORMAL) {
|
||
result = Check_Entry_Serial_Advance(&prepared);
|
||
if (result != IEC104_ASDU_QUEUE_OK) {
|
||
return result;
|
||
}
|
||
}
|
||
if (priority == IEC104_ASDU_PRIORITY_HIGH) {
|
||
sequence = prepared.high_next_sequence++;
|
||
} else if (prepared.soe.capacity != 0U) {
|
||
/* SOE使用奇数,普通NORMAL使用偶数;同一next域内跳过另一类别,不回绕。 */
|
||
prepared.next_entry_serial += prepared.next_entry_serial & UINT64_C(1);
|
||
}
|
||
/* 总空闲已足够,直接分段写入;即使条目头本身跨尾部,也无需额外保留空间。 */
|
||
index = bucket->tail_index;
|
||
if (priority == IEC104_ASDU_PRIORITY_HIGH) {
|
||
encoded_length = asdu_length;
|
||
Ring_Write(bucket, index, &encoded_length, sizeof(encoded_length));
|
||
} else {
|
||
memset(&header, 0, sizeof(header));
|
||
header.entry_serial = prepared.next_entry_serial;
|
||
header.entry_state = NORMAL_HEADER_PENDING;
|
||
/* 入口已检查长度为6..249,显式收窄为8位后写入条目头。 */
|
||
header.asdu_length = (uint8_t)asdu_length;
|
||
Ring_Write(bucket, index, &header, sizeof(header));
|
||
}
|
||
Ring_Write(bucket, Ring_Advance(bucket, index, header_length), asdu, asdu_length);
|
||
if (bucket->unsubmitted_count == 0U) {
|
||
bucket->unsubmitted_index = index;
|
||
}
|
||
bucket->tail_index = Ring_Advance(bucket, index, size);
|
||
bucket->used_bytes += size;
|
||
bucket->entry_count++;
|
||
bucket->unsubmitted_count++;
|
||
/* NORMAL循环编号单独推进;HIGH的隐式票号已在写字节前预留。 */
|
||
if (priority == IEC104_ASDU_PRIORITY_NORMAL) {
|
||
prepared.next_entry_serial = Entry_Serial_Next(prepared.next_entry_serial);
|
||
}
|
||
*queue = prepared;
|
||
if (ticket != NULL) { *ticket = sequence; }
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:严格模式先核对完整布局、编号和可回收空间,再修改原环;空间不足时不留下部分覆盖。
|
||
* 输入参数:queue——ASDU队列。
|
||
* generation——连接代次。
|
||
* priority——优先级。
|
||
* asdu——ASDU报文。
|
||
* length——字节长度。
|
||
* ticket——应答交接票据。
|
||
* rank——业务等级。
|
||
* 输出参数:queue——ASDU队列的更新结果。
|
||
* ticket——应答交接票据的更新结果。
|
||
* 返回值:按处理分支返回IEC104_ASDU_QUEUE_ERROR、IEC104_ASDU_QUEUE_FULL、IEC104_ASDU_QUEUE_OK。
|
||
* 说明:已提交NORMAL只回收payload,身份头持续占用原字节预算,直到对应ACK或连接重绑。
|
||
* 预检仅累计可回收字节,不先删除低等级条目试探容量;FULL/ERROR必须保住调用前所有原文和票据。
|
||
*/
|
||
static IEC104_AsduQueueResult_t Strict_Enqueue(
|
||
IEC104_AsduQueue_t *queue, uint64_t generation, IEC104_AsduPriority_t priority,
|
||
const uint8_t *asdu, uint16_t length, uint64_t *ticket, IEC104_BusinessPriority_t rank)
|
||
{
|
||
IEC104_AsduPriorityQueue_t *bucket;
|
||
IEC104_AsduPriorityTicketState_t *lane = NULL;
|
||
IEC104_AsduNormalEntryHeader_t normal;
|
||
uint32_t size, index, remaining, available;
|
||
uint64_t serial, admission;
|
||
uint8_t encoded_rank = (uint8_t)rank;
|
||
if (!Queue_State_Valid(queue) || !Priority_Valid(priority) || !Business_Priority_Valid(rank) ||
|
||
generation != queue->connection_generation ||
|
||
(priority == IEC104_ASDU_PRIORITY_HIGH && generation == 0U) ||
|
||
asdu == NULL || length < IEC104_ASDU_HEADER_LEN || length > IEC104_MAX_ASDU_LEN) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
bucket = &queue->priority_queues[priority];
|
||
size = Strict_Header_Length(priority) + length;
|
||
if (size > bucket->byte_capacity) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
if (queue->next_entry_serial == UINT64_MAX) { return IEC104_ASDU_QUEUE_FULL; }
|
||
admission = queue->next_entry_serial;
|
||
if (priority == IEC104_ASDU_PRIORITY_NORMAL && queue->soe.capacity != 0U) { admission += admission & UINT64_C(1); }
|
||
if (priority == IEC104_ASDU_PRIORITY_HIGH) {
|
||
lane = &queue->priority_state->lanes[(unsigned)rank - 1U];
|
||
if (lane->next_sequence > UINT64_MAX - 16U) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
serial = lane->next_sequence;
|
||
} else {
|
||
serial = admission;
|
||
}
|
||
available = bucket->byte_capacity - bucket->used_bytes;
|
||
if (priority == IEC104_ASDU_PRIORITY_HIGH && available < size) { return IEC104_ASDU_QUEUE_FULL; }
|
||
index = bucket->head_index;
|
||
remaining = bucket->used_bytes;
|
||
while (available < size && remaining != 0U) {
|
||
StrictEntry entry = {0U, 0U, 0U, 0U, 0U};
|
||
uint32_t old_size;
|
||
(void)Strict_Read(queue, priority, index, remaining, &entry);
|
||
old_size = Strict_Header_Length(priority) + entry.length;
|
||
if (entry.state == NORMAL_HEADER_PENDING) { available += old_size; }
|
||
else if (entry.state == NORMAL_HEADER_RETAINED_UNACKED) { available += entry.length; }
|
||
index = Ring_Advance(bucket, index, old_size);
|
||
remaining -= old_size;
|
||
}
|
||
if (available < size) { return IEC104_ASDU_QUEUE_FULL; }
|
||
|
||
/* 预检已保证全部后续步骤可提交;覆盖顺序仍为最早保留的原文,不按业务等级驱逐。 */
|
||
index = bucket->head_index;
|
||
while (bucket->byte_capacity - bucket->used_bytes < size) {
|
||
StrictEntry entry = {0U, 0U, 0U, 0U, 0U};
|
||
uint32_t offset = index >= bucket->head_index ? index - bucket->head_index :
|
||
bucket->byte_capacity - bucket->head_index + index;
|
||
(void)Strict_Read(queue, priority, index, bucket->used_bytes - offset, &entry);
|
||
if (entry.state == NORMAL_HEADER_OVERWRITTEN) {
|
||
index = Ring_Advance(bucket, index, IEC104_ASDU_NORMAL_ENTRY_HEADER_LEN);
|
||
} else if (entry.state == NORMAL_HEADER_PENDING) {
|
||
int at_head = index == bucket->head_index;
|
||
Strict_Delete_Bytes(bucket, index, IEC104_ASDU_NORMAL_ENTRY_HEADER_LEN + entry.length);
|
||
if (at_head) { index = bucket->head_index; }
|
||
} else {
|
||
/* 发送顺序可与物理顺序不同,不能仅留覆盖数量再由队首猜ACK对象。
|
||
* 保留原serial/rank,清除原文长度;后续ACK仍按Manager记录中的精确身份回收此头。 */
|
||
Ring_Read(bucket, index, &normal, sizeof(normal));
|
||
normal.entry_state = NORMAL_HEADER_OVERWRITTEN;
|
||
normal.asdu_length = 0U;
|
||
Ring_Write(bucket, index, &normal, sizeof(normal));
|
||
index = Ring_Advance(bucket, index, IEC104_ASDU_NORMAL_ENTRY_HEADER_LEN);
|
||
Strict_Delete_Bytes(bucket, index, entry.length);
|
||
}
|
||
}
|
||
index = bucket->tail_index;
|
||
if (priority == IEC104_ASDU_PRIORITY_HIGH) {
|
||
Ring_Write(bucket, index, &serial, sizeof(serial));
|
||
Ring_Write(bucket, Ring_Advance(bucket, index, 8U), &queue->next_entry_serial, sizeof(queue->next_entry_serial));
|
||
Ring_Write(bucket, Ring_Advance(bucket, index, 16U), &length, sizeof(length));
|
||
Ring_Write(bucket, Ring_Advance(bucket, index, 18U), &encoded_rank, sizeof(encoded_rank));
|
||
lane->next_sequence += 16U;
|
||
if (queue->high_next_sequence < lane->next_sequence) { queue->high_next_sequence = lane->next_sequence; }
|
||
} else {
|
||
memset(&normal, 0, sizeof(normal));
|
||
normal.entry_serial = serial;
|
||
normal.entry_state = NORMAL_HEADER_PENDING;
|
||
normal.asdu_length = (uint8_t)length;
|
||
normal.business_priority = encoded_rank;
|
||
Ring_Write(bucket, index, &normal, sizeof(normal));
|
||
}
|
||
queue->next_entry_serial = admission + 1U;
|
||
Ring_Write(bucket, Ring_Advance(bucket, index, Strict_Header_Length(priority)), asdu, length);
|
||
bucket->used_bytes += size;
|
||
bucket->tail_index = Ring_Advance(bucket, index, size);
|
||
Strict_Recount(queue, priority);
|
||
if (ticket != NULL) { *ticket = serial; }
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:按HIGH或NORMAL生命周期复制ASDU,使用该生命周期的默认业务等级。
|
||
* 输入参数:queue/connection_generation——队列及当前绑定代次;priority——生命周期。
|
||
* asdu/asdu_length——待复制的ASDU原文及字节数。
|
||
* 输出参数:无。
|
||
* 返回值:OK已复制接手;FULL暂时无法入队;ERROR为参数、代次或存储状态非法。
|
||
* 说明:严格模式下HIGH默认总召等级、NORMAL默认顺序事件等级;本入口不分配N(S)。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Enqueue(
|
||
IEC104_AsduQueue_t *queue, uint64_t connection_generation, IEC104_AsduPriority_t priority,
|
||
const uint8_t *asdu, uint16_t asdu_length)
|
||
{
|
||
if (queue != NULL && queue->strict_business_priority != 0U) {
|
||
return Strict_Enqueue(queue, connection_generation, priority, asdu, asdu_length, NULL,
|
||
priority == IEC104_ASDU_PRIORITY_HIGH ? IEC104_BUSINESS_PRIORITY_INTERROGATION :
|
||
IEC104_BUSINESS_PRIORITY_SEQUENCE_EVENT);
|
||
}
|
||
return Enqueue(queue, connection_generation, priority, asdu, asdu_length, NULL);
|
||
}
|
||
|
||
/**
|
||
* 功能:将应答复制到HIGH队列,并按需返回用于查询交接状态的票据。
|
||
* 输入参数:queue/connection_generation——队列及当前绑定代次;asdu/asdu_length——应答原文及长度。
|
||
* 输出参数:ticket——可为NULL,仅入队成功时写入票据。
|
||
* 返回值:OK已接手;FULL表示HIGH暂无空间;ERROR表示输入或队列状态非法。
|
||
* 说明:严格模式使用总召业务等级;票据只跟踪取消或交接,不代表对端ACK。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Enqueue_Response(
|
||
IEC104_AsduQueue_t *queue, uint64_t connection_generation,
|
||
const uint8_t *asdu, uint16_t asdu_length, uint64_t *ticket)
|
||
{
|
||
if (queue != NULL && queue->strict_business_priority != 0U) {
|
||
return Strict_Enqueue(queue, connection_generation, IEC104_ASDU_PRIORITY_HIGH, asdu, asdu_length,
|
||
ticket, IEC104_BUSINESS_PRIORITY_INTERROGATION);
|
||
}
|
||
return Enqueue(queue, connection_generation, IEC104_ASDU_PRIORITY_HIGH, asdu, asdu_length, ticket);
|
||
}
|
||
|
||
/**
|
||
* 功能:按指定生命周期和业务等级复制ASDU到队列。
|
||
* 输入参数:queue/connection_generation——队列及代次;priority——HIGH或NORMAL生命周期。
|
||
* asdu/asdu_length——原文及长度;business_priority——NONE与COUNT之间的业务等级。
|
||
* 输出参数:无。
|
||
* 返回值:OK已接手;FULL暂时无法入队;ERROR为业务等级、输入或队列状态非法。
|
||
* 说明:兼容模式也校验业务等级,但仍使用原有HIGH/NORMAL队列顺序。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Enqueue_Priority(
|
||
IEC104_AsduQueue_t *queue, uint64_t connection_generation, IEC104_AsduPriority_t priority,
|
||
const uint8_t *asdu, uint16_t asdu_length, IEC104_BusinessPriority_t business_priority)
|
||
{
|
||
if (!Business_Priority_Valid(business_priority)) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
if (queue != NULL && queue->strict_business_priority != 0U) {
|
||
return Strict_Enqueue(queue, connection_generation, priority, asdu, asdu_length, NULL, business_priority);
|
||
}
|
||
return Enqueue(queue, connection_generation, priority, asdu, asdu_length, NULL);
|
||
}
|
||
|
||
/**
|
||
* 功能:按指定业务等级将应答复制到HIGH队列,并按需返回交接票据。
|
||
* 输入参数:queue/connection_generation——队列及代次;asdu/asdu_length——应答原文及长度。
|
||
* business_priority——NONE与COUNT之间的有效等级。
|
||
* 输出参数:ticket——可为NULL,仅成功时写入票据。
|
||
* 返回值:OK已接手;FULL暂无入队空间;ERROR为等级、输入或队列状态非法。
|
||
* 说明:生命周期始终为HIGH;兼容模式校验等级但不按等级重新排序。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Enqueue_Response_Priority(
|
||
IEC104_AsduQueue_t *queue, uint64_t connection_generation, const uint8_t *asdu,
|
||
uint16_t asdu_length, uint64_t *ticket, IEC104_BusinessPriority_t business_priority)
|
||
{
|
||
if (!Business_Priority_Valid(business_priority)) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
if (queue != NULL && queue->strict_business_priority != 0U) {
|
||
return Strict_Enqueue(queue, connection_generation, IEC104_ASDU_PRIORITY_HIGH, asdu, asdu_length,
|
||
ticket, business_priority);
|
||
}
|
||
return Enqueue(queue, connection_generation, IEC104_ASDU_PRIORITY_HIGH, asdu, asdu_length, ticket);
|
||
}
|
||
|
||
/**
|
||
* 功能:只预检当前等级的票据连续性,不预留票号,也不检查其他等级、链路窗口或当前发送帧。
|
||
* 输入参数:queue——ASDU队列。
|
||
* generation——连接代次。
|
||
* priority——优先级。
|
||
* 输出参数:无。
|
||
* 返回值:按处理分支返回IEC104_ASDU_QUEUE_ERROR、lane->head_sequence == lane->next_sequence ? IEC104_ASDU_QUEUE_OK : IEC104_ASDU_QUEUE_FULL。
|
||
* 说明:FULL仅表示同级尚有排队前驱;Manager须补齐全局准入检查,再串行完成准备帧与登记。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Can_Record_Direct_Response(
|
||
const IEC104_AsduQueue_t *queue, uint64_t generation, IEC104_BusinessPriority_t priority)
|
||
{
|
||
const IEC104_AsduPriorityTicketState_t *lane;
|
||
if (!Queue_State_Valid(queue) || queue->strict_business_priority == 0U ||
|
||
generation == 0U || generation != queue->connection_generation || !Business_Priority_Valid(priority)) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
lane = &queue->priority_state->lanes[(unsigned)priority - 1U];
|
||
if (lane->next_sequence > UINT64_MAX - 16U) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
return lane->head_sequence == lane->next_sequence ? IEC104_ASDU_QUEUE_OK : IEC104_ASDU_QUEUE_FULL;
|
||
}
|
||
|
||
/**
|
||
* 功能:调用前SendBuffer必须已接手本帧;仅OK写非零ticket,失败保持输出原值。
|
||
* 输入参数:queue——ASDU队列。
|
||
* generation——连接代次。
|
||
* priority——优先级。
|
||
* ticket——应答交接票据。
|
||
* 输出参数:queue——ASDU队列的更新结果。
|
||
* ticket——应答交接票据的更新结果。
|
||
* 返回值:按处理分支返回IEC104_ASDU_QUEUE_ERROR、checked、IEC104_ASDU_QUEUE_OK。
|
||
* 说明:不保存原文且不触发发送,已交接帧不能因登记失败按WAIT重新提交,调用方须按致命故障收尾。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Record_Direct_Response(
|
||
IEC104_AsduQueue_t *queue, uint64_t generation, IEC104_BusinessPriority_t priority, uint64_t *ticket)
|
||
{
|
||
IEC104_AsduPriorityTicketState_t *lane;
|
||
IEC104_AsduQueueResult_t checked = IEC104_AsduQueue_Can_Record_Direct_Response(queue, generation, priority);
|
||
if (ticket == NULL) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
if (checked != IEC104_ASDU_QUEUE_OK) { return checked; }
|
||
lane = &queue->priority_state->lanes[(unsigned)priority - 1U];
|
||
/* 此级没有排队前驱且帧已交接,head与next同步越过新票,查询即可得到HANDED。
|
||
* 不占字节或受理顺序,但仍推进全局票据高水位,保证Close/Init后不会复用这个直发票号。 */
|
||
*ticket = lane->next_sequence;
|
||
lane->next_sequence += 16U;
|
||
lane->head_sequence = lane->next_sequence;
|
||
if (queue->high_next_sequence < lane->next_sequence) { queue->high_next_sequence = lane->next_sequence; }
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:查询应答票据对应的待交接、已交接或取消状态。
|
||
* 输入参数:queue——保留票据历史的队列;ticket——应答提交时取得的票据。
|
||
* 输出参数:无,不修改队列。
|
||
* 返回值:PENDING尚未交接;HANDED已交接;CANCELLED已取消或历史不可判定;ERROR为非法票据/状态。
|
||
* 说明:ticket为0表示兼容直交完成,返回HANDED;HANDED不表示TCP已写完或对端已确认。
|
||
*/
|
||
IEC104_ResponseState_t IEC104_AsduQueue_Get_Response_State(const IEC104_AsduQueue_t *queue, uint64_t ticket)
|
||
{
|
||
if (queue == NULL) { return IEC104_RESPONSE_ERROR; }
|
||
if (ticket == 0U) { return IEC104_RESPONSE_HANDED; }
|
||
/* Abort清零后只能保守取消;TcpServer另用保存的next校验未曾分配的票号。 */
|
||
if (queue->is_initialized == 0U) { return IEC104_RESPONSE_CANCELLED; }
|
||
if (!Queue_State_Valid(queue) || ticket >= queue->high_next_sequence) { return IEC104_RESPONSE_ERROR; }
|
||
if (ticket < queue->high_known_from) { return IEC104_RESPONSE_CANCELLED; }
|
||
if (queue->strict_business_priority != 0U) {
|
||
/* 全局高水位只能排除从未分配的上界;某高级票先交接,不证明较低级票已经交接。
|
||
* 必须按票号余数找到本级进度,再区分待发、已知取消与已交接。 */
|
||
unsigned rank = (unsigned)(ticket & 15U);
|
||
const IEC104_AsduPriorityTicketState_t *lane;
|
||
if (rank == 0U || rank >= IEC104_BUSINESS_PRIORITY_COUNT) { return IEC104_RESPONSE_ERROR; }
|
||
lane = &queue->priority_state->lanes[rank - 1U];
|
||
if (ticket >= lane->next_sequence) { return IEC104_RESPONSE_ERROR; }
|
||
if (ticket < lane->known_from) { return IEC104_RESPONSE_CANCELLED; }
|
||
if (ticket >= lane->head_sequence) { return IEC104_RESPONSE_PENDING; }
|
||
return ticket >= lane->cancel_begin && ticket < lane->cancel_end ?
|
||
IEC104_RESPONSE_CANCELLED : IEC104_RESPONSE_HANDED;
|
||
}
|
||
if (ticket >= queue->high_head_sequence) { return IEC104_RESPONSE_PENDING; }
|
||
if (ticket >= queue->high_cancel_begin && ticket < queue->high_cancel_end) {
|
||
return IEC104_RESPONSE_CANCELLED;
|
||
}
|
||
return IEC104_RESPONSE_HANDED;
|
||
}
|
||
|
||
/**
|
||
* 功能:查看指定优先级最旧的保留条目;NORMAL队首可能已登记交接并正在等待ACK。
|
||
* 输入参数:queue——有效队列;priority——HIGH或NORMAL。
|
||
* 输出参数:head_info——非NULL,仅OK时提供身份、原文借用及是否已提交。
|
||
* 返回值:OK有条目,EMPTY为空,ERROR表示参数、状态或条目非法。
|
||
* 说明:不移动任何游标;选择第一条未提交ASDU应调用Peek_Unsubmitted。
|
||
* 兼容HIGH队首也是下一待发项;严格模式最旧原文与最高等级候选可以不同,不能混用两个接口。
|
||
* 返回的是原文借用,不是副本;条目被覆盖或释放后不能继续使用返回的指针。
|
||
*/
|
||
static IEC104_AsduQueueResult_t Peek_Other_Head(
|
||
const IEC104_AsduQueue_t *queue, IEC104_AsduPriority_t priority, IEC104_AsduHeadInfo_t *head_info)
|
||
{
|
||
const IEC104_AsduPriorityQueue_t *bucket;
|
||
|
||
if (!Queue_State_Valid(queue) || !Priority_Valid(priority) || head_info == NULL) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
if (queue->strict_business_priority != 0U) { return Strict_Peek(queue, priority, 0, 0U, head_info, NULL); }
|
||
bucket = &queue->priority_queues[priority];
|
||
if (bucket->entry_count == 0U) {
|
||
return IEC104_ASDU_QUEUE_EMPTY;
|
||
}
|
||
return Read_Entry_With_Expected_State(queue, priority, bucket->head_index,
|
||
bucket->entry_count > bucket->unsubmitted_count ?
|
||
QUEUE_ENTRY_RETAINED_UNACKED : QUEUE_ENTRY_PENDING, head_info) ?
|
||
IEC104_ASDU_QUEUE_OK : IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
|
||
/**
|
||
* 功能:只读查看指定生命周期中当前允许交接的下一条ASDU。
|
||
* 输入参数:queue——有效队列;priority——HIGH或NORMAL。
|
||
* 输出参数:head_info——调用方提供的非NULL指针;仅返回OK时写入,is_submitted为0。
|
||
* 返回值:OK表示找到当前允许交接的未提交条目;EMPTY表示没有可交接条目,不代表整个队列为空;
|
||
* ERROR表示参数、状态或条目非法。
|
||
* 说明:兼容模式通过unsubmitted_index定位;严格模式在校验后扫描全环,挑最高业务等级的最早待发项。
|
||
* 不移除条目,不改变记账状态;Manager交接过程中的内部复核也使用本接口。
|
||
* 本队列不查询SendBuffer,调用方不能把is_submitted为0当作发送模块尚未接手的证明。
|
||
* NORMAL全部已提交、启动暂停或EOF停止选取时返回EMPTY,Peek_Head仍可查看实际保留原文。
|
||
* 把暂停落实在共用选帧入口,Manager的排队调度和直发准入都看到同一可发送集合。
|
||
* 离线查询可返回代次0的NORMAL原文视图,真正提交发送须先Rebind到非零代次。
|
||
*/
|
||
static IEC104_AsduQueueResult_t Peek_Other_Unsubmitted(
|
||
const IEC104_AsduQueue_t *queue, IEC104_AsduPriority_t priority, IEC104_AsduHeadInfo_t *head_info)
|
||
{
|
||
const IEC104_AsduPriorityQueue_t *bucket;
|
||
|
||
if (!Queue_State_Valid(queue) || !Priority_Valid(priority) || head_info == NULL) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
if (queue->strict_business_priority != 0U) { return Strict_Peek(queue, priority, 1, 0U, head_info, NULL); }
|
||
bucket = &queue->priority_queues[priority];
|
||
if (bucket->unsubmitted_count == 0U ||
|
||
(priority == IEC104_ASDU_PRIORITY_NORMAL &&
|
||
(queue->normal_send_limited != 0U || queue->startup_pending != 0U))) {
|
||
return IEC104_ASDU_QUEUE_EMPTY;
|
||
}
|
||
return Read_Entry_With_Expected_State(queue, priority, bucket->unsubmitted_index,
|
||
QUEUE_ENTRY_PENDING, head_info) ?
|
||
IEC104_ASDU_QUEUE_OK : IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
|
||
/**
|
||
* 功能:把Peek或Get_Entry返回的最多两段ASDU复制到调用方连续缓冲。
|
||
* 输入参数:head_info——借用仍有效的条目描述;capacity——输出缓冲实际字节容量。
|
||
* 输出参数:destination——非NULL、与两段原文及描述不重叠的可写数组,仅OK时写入。
|
||
* 返回值:OK复制了asdu_length字节;ERROR表示描述或容量非法,此时不改输出。
|
||
* 说明:只检查分段描述,不重新查询队列或核对代次;调用方保证条目未覆盖、释放、Detach、Rebind或Abort。
|
||
* 连续ASDU也可使用本接口;正常发送仅在跨界时才需要额外拼接。
|
||
* 复制结果仅含ASDU,不添加APCI;复制成功也不会移除条目或登记发送交接。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Copy_Asdu(
|
||
const IEC104_AsduHeadInfo_t *head_info, uint8_t *destination, uint16_t capacity)
|
||
{
|
||
if (head_info == NULL || destination == NULL ||
|
||
head_info->asdu_length < IEC104_ASDU_HEADER_LEN || head_info->asdu_length > IEC104_MAX_ASDU_LEN ||
|
||
head_info->asdu_first == NULL || head_info->asdu_first_length == 0U ||
|
||
head_info->asdu_first_length > head_info->asdu_length || capacity < head_info->asdu_length ||
|
||
(head_info->asdu_first_length == head_info->asdu_length ?
|
||
head_info->asdu_second != NULL : head_info->asdu_second == NULL)) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
memcpy(destination, head_info->asdu_first, head_info->asdu_first_length);
|
||
if (head_info->asdu_second != NULL) {
|
||
memcpy(destination + head_info->asdu_first_length, head_info->asdu_second,
|
||
head_info->asdu_length - head_info->asdu_first_length);
|
||
}
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:在发送模块接手后登记队列交接;HIGH消费当前原文,NORMAL保留原文并推进待发游标。
|
||
* 输入参数:queue/priority——目标队列与优先级;entry_id——NORMAL完整身份,HIGH为当前代次及0编号。
|
||
* 输出参数:queue——HIGH释放所选条目;NORMAL更新头内状态、unsubmitted_count和unsubmitted_index。
|
||
* 返回值:OK或ERROR;失败不修改队列。
|
||
* 说明:调用方须先把完整I帧交给SendBuffer并建立Manager记录,再串行调用本函数。
|
||
* 调用前队列仍记录PENDING;HIGH不在环内建立已交接状态,直接消费原文完成记账。
|
||
* 本函数不验证发送缓冲是否接手,也不构帧或执行发送,不能提前调用来丢弃待发数据。
|
||
* HIGH原文借用在成功出队后失效;SendBuffer报文及Manager记录继续有效,仍须完成发送和ACK。
|
||
* HIGH仍要求entry_serial等于0,用于拒绝非法标识;它不能区分同一代次中先后入队的两条HIGH。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Mark_Submitted(
|
||
IEC104_AsduQueue_t *queue, IEC104_AsduPriority_t priority, IEC104_AsduEntryId_t entry_id)
|
||
{
|
||
IEC104_AsduHeadInfo_t info, next_info;
|
||
IEC104_AsduPriorityQueue_t *bucket;
|
||
uint32_t next;
|
||
|
||
if (queue != NULL && queue->soe.capacity != 0U && priority == IEC104_ASDU_PRIORITY_NORMAL) {
|
||
uint8_t *slot;
|
||
if (entry_id.connection_generation == 0U || entry_id.connection_generation != queue->connection_generation ||
|
||
IEC104_AsduQueue_Peek_Unsubmitted(queue, priority, &info) != IEC104_ASDU_QUEUE_OK ||
|
||
info.entry_id.entry_serial != entry_id.entry_serial) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
slot = Soe_Find(queue, entry_id.entry_serial);
|
||
if (slot != NULL) {
|
||
if ((slot[8] & 3U) != SOE_PENDING) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
slot[8] = (uint8_t)((slot[8] & ~3U) | SOE_UNACKED);
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
}
|
||
if (queue != NULL && queue->strict_business_priority != 0U) {
|
||
uint32_t index;
|
||
if (!Queue_State_Valid(queue) || !Priority_Valid(priority) || entry_id.connection_generation == 0U ||
|
||
entry_id.connection_generation != queue->connection_generation ||
|
||
Strict_Peek(queue, priority, 1, 0U, &info, &index) != IEC104_ASDU_QUEUE_OK ||
|
||
entry_id.entry_serial != info.entry_id.entry_serial) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
bucket = &queue->priority_queues[priority];
|
||
if (priority == IEC104_ASDU_PRIORITY_HIGH) {
|
||
IEC104_AsduPriorityTicketState_t *lane =
|
||
&queue->priority_state->lanes[(unsigned)info.business_priority - 1U];
|
||
if (info.response_ticket != lane->head_sequence) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
/* 只推进被选等级的交接边界;其他等级虽有更小票号,也可能仍在字节环中待发。 */
|
||
lane->head_sequence += 16U;
|
||
Strict_Delete_Bytes(bucket, index, (uint32_t)info.entry_header_length + info.asdu_length);
|
||
} else {
|
||
Mark_Normal_Entry_Retained_Unacked(bucket, index);
|
||
}
|
||
Strict_Recount(queue, priority);
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
if (entry_id.connection_generation == 0U ||
|
||
IEC104_AsduQueue_Peek_Unsubmitted(queue, priority, &info) != IEC104_ASDU_QUEUE_OK ||
|
||
info.entry_id.connection_generation != entry_id.connection_generation ||
|
||
info.entry_id.entry_serial != entry_id.entry_serial) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
if (priority == IEC104_ASDU_PRIORITY_HIGH) {
|
||
/* 发送模块已接手;PENDING仅核对删除前的队列旧状态,随后消费原文完成交接记账。 */
|
||
return Remove_Head_With_Expected_State(queue, priority, entry_id, QUEUE_ENTRY_PENDING);
|
||
}
|
||
bucket = &queue->priority_queues[priority];
|
||
next = Next_Entry_Index(bucket, bucket->unsubmitted_index, &info);
|
||
if (bucket->unsubmitted_count > 1U &&
|
||
!Read_Entry_With_Expected_State(queue, priority, next, QUEUE_ENTRY_PENDING, &next_info)) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
/* 登记NORMAL交接:头编码改为保留待ACK,待发游标移到下一条;原文不出队。 */
|
||
{
|
||
IEC104_AsduQueue_t prepared = *queue;
|
||
IEC104_AsduPriorityQueue_t *normal = &prepared.priority_queues[priority];
|
||
--normal->unsubmitted_count;
|
||
normal->unsubmitted_index = normal->unsubmitted_count == 0U ? normal->tail_index : next;
|
||
Mark_Normal_Entry_Retained_Unacked(bucket, bucket->unsubmitted_index);
|
||
*queue = prepared;
|
||
}
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:完成NORMAL下一条有效ACK的存储清理,兼容原文已被覆盖;HIGH的ACK只更新Manager记录。
|
||
* 输入参数:queue——有效队列;priority——只允许NORMAL;entry_id——有效ACK对应Manager记录的真实身份。
|
||
* 输出参数:queue——仍有原文时释放匹配队首;已覆盖时更新待确认计数和区间边界,不移除当前队首。
|
||
* 返回值:OK或ERROR;ERROR表示代次、编号区间、覆盖计数或已提交队首身份不符合约束。
|
||
* 说明:仅供Manager在Link验证ACK后按发送顺序调用;本函数不验证协议N(R)。
|
||
* 兼容模式以覆盖数量和循环区间检查,覆盖前缀清完才允许确认保留队首,末个身份须与区间结束边界相等。
|
||
* 严格模式发送顺序可跨物理位置,须查找匹配serial的待ACK原文或覆盖身份头,不能确认物理队首代替它。
|
||
* 只清理ASDU存储及覆盖计数,不自行推进协议确认边界或释放Manager回执记录。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Confirm_Next_Normal_Entry(
|
||
IEC104_AsduQueue_t *queue, IEC104_AsduPriority_t priority, IEC104_AsduEntryId_t entry_id)
|
||
{
|
||
uint64_t confirmed_offset, overwritten_span;
|
||
|
||
if (queue != NULL && queue->soe.capacity != 0U) {
|
||
uint8_t *slot;
|
||
uint16_t i;
|
||
if (!Queue_State_Valid(queue) || priority != IEC104_ASDU_PRIORITY_NORMAL ||
|
||
entry_id.connection_generation == 0U || entry_id.connection_generation != queue->connection_generation ||
|
||
entry_id.entry_serial == 0U) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
slot = Soe_Find(queue, entry_id.entry_serial);
|
||
if (slot != NULL) {
|
||
if ((slot[8] & 3U) != SOE_UNACKED) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
memset(slot, 0, IEC104_ASDU_SOE_SLOT_LEN);
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
for (i = 0U; i < queue->soe.retired_capacity; ++i) {
|
||
if (queue->soe.retired[i] == entry_id.entry_serial) {
|
||
queue->soe.retired[i] = 0U;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
}
|
||
/* 已ACK或已丢弃的SOE身份不能误入兼容OTHER覆盖区间中的编号空洞。 */
|
||
if ((entry_id.entry_serial & 1U) != 0U) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
}
|
||
|
||
if (queue != NULL && queue->strict_business_priority != 0U) {
|
||
IEC104_AsduPriorityQueue_t *bucket;
|
||
uint32_t index, remaining;
|
||
if (!Queue_State_Valid(queue) || priority != IEC104_ASDU_PRIORITY_NORMAL ||
|
||
entry_id.connection_generation == 0U || entry_id.connection_generation != queue->connection_generation) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
bucket = &queue->priority_queues[priority];
|
||
index = bucket->head_index;
|
||
remaining = bucket->used_bytes;
|
||
while (remaining != 0U) {
|
||
StrictEntry entry = {0U, 0U, 0U, 0U, 0U};
|
||
uint32_t size;
|
||
(void)Strict_Read(queue, priority, index, remaining, &entry);
|
||
size = IEC104_ASDU_NORMAL_ENTRY_HEADER_LEN + entry.length;
|
||
if (entry.serial == entry_id.entry_serial) {
|
||
if (entry.state == NORMAL_HEADER_PENDING) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
Strict_Delete_Bytes(bucket, index, size);
|
||
Strict_Recount(queue, priority);
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
index = Ring_Advance(bucket, index, size);
|
||
remaining -= size;
|
||
}
|
||
return IEC104_ASDU_QUEUE_ERROR; /* 重复ACK、未分配身份和已丢弃未提交原文均不可伪装成覆盖确认。 */
|
||
}
|
||
|
||
if (!Queue_State_Valid(queue) || priority != IEC104_ASDU_PRIORITY_NORMAL ||
|
||
entry_id.connection_generation != queue->connection_generation ||
|
||
!Normal_Serial_Before(queue, entry_id.entry_serial, queue->next_entry_serial)) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
if (queue->overwritten_unacked_count != 0U) {
|
||
/* 以区间下界为起点比较循环距离,使跨UINT64_MAX的覆盖区间仍能按同一规则检查。 */
|
||
confirmed_offset = Entry_Serial_Distance(queue->overwritten_unacked_serial_begin, entry_id.entry_serial);
|
||
overwritten_span = Entry_Serial_Distance(queue->overwritten_unacked_serial_begin,
|
||
queue->overwritten_unacked_serial_end);
|
||
/*
|
||
* ACK须落在区间内,且只有最后一次ACK才可命中上界;后续编号位置须容得下剩余计数。
|
||
* 此处只检查区间和数量,具体ACK身份是否属于发送记录仍由Manager保证。
|
||
*/
|
||
if (confirmed_offset > overwritten_span ||
|
||
((queue->overwritten_unacked_count == 1U) !=
|
||
(entry_id.entry_serial == queue->overwritten_unacked_serial_end)) ||
|
||
queue->overwritten_unacked_count - 1U > overwritten_span - confirmed_offset) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
/* 原文可能已被新条目占用,绝不能把当前物理队首当成旧帧释放。 */
|
||
queue->overwritten_unacked_count--;
|
||
/* 排除已确认及更早的编号;下界可落在编号间隔中,不一定就是下一待ACK条目的编号。 */
|
||
queue->overwritten_unacked_serial_begin = Entry_Serial_Next(entry_id.entry_serial);
|
||
if (queue->overwritten_unacked_count == 0U) {
|
||
/* 最后一个覆盖条目已确认,清除两端边界,释放这段编号窗口。 */
|
||
queue->overwritten_unacked_serial_end = 0U;
|
||
queue->overwritten_unacked_serial_begin = 0U;
|
||
}
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
return Remove_Head_With_Expected_State(queue, priority, entry_id, QUEUE_ENTRY_RETAINED_UNACKED);
|
||
}
|
||
|
||
/**
|
||
* 功能:在发送交接开始前,主动丢弃仍处于队列待发状态的最旧保留原文ASDU。
|
||
* 输入参数:queue/priority——目标字节环;entry_id——NORMAL完整身份,HIGH为当前代次及0编号。
|
||
* 输出参数:queue——成功时释放队首并推进待发游标;不清零原文字节,被丢弃条目的借用失效。
|
||
* 返回值:OK或ERROR;队首为空、身份不符或已登记交接时ERROR且不修改队列。
|
||
* 说明:兼容模式只删除物理队首;严格模式跳过覆盖身份头,但不能越过待ACK原文,也不按业务等级挑选。
|
||
* 本函数只检查队列记账状态,无法判断SendBuffer是否已经接手;调用方必须在发送交接前使用,
|
||
* 不得插入Manager的交接过程,也不能用它撤回已编号I帧。断线恢复使用Rebind。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Discard_Unsubmitted_Head(
|
||
IEC104_AsduQueue_t *queue, IEC104_AsduPriority_t priority, IEC104_AsduEntryId_t entry_id)
|
||
{
|
||
uint64_t sequence;
|
||
IEC104_AsduQueueResult_t result;
|
||
if (queue == NULL) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
if (queue->soe.capacity != 0U && priority == IEC104_ASDU_PRIORITY_NORMAL) {
|
||
IEC104_AsduHeadInfo_t info;
|
||
uint8_t *slot;
|
||
if (entry_id.connection_generation != queue->connection_generation ||
|
||
IEC104_AsduQueue_Peek_Head(queue, priority, &info) != IEC104_ASDU_QUEUE_OK ||
|
||
info.entry_id.entry_serial != entry_id.entry_serial || info.is_submitted != 0U) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
slot = Soe_Find(queue, entry_id.entry_serial);
|
||
if (slot != NULL) {
|
||
memset(slot, 0, IEC104_ASDU_SOE_SLOT_LEN);
|
||
++queue->soe.discarded_count; ++queue->discarded_asdu_count;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
}
|
||
if (queue->strict_business_priority != 0U) {
|
||
IEC104_AsduHeadInfo_t info;
|
||
uint32_t index;
|
||
if (!Queue_State_Valid(queue) || !Priority_Valid(priority) ||
|
||
entry_id.connection_generation != queue->connection_generation ||
|
||
Strict_Peek(queue, priority, 0, 0U, &info, &index) != IEC104_ASDU_QUEUE_OK ||
|
||
info.is_submitted != 0U || entry_id.entry_serial != info.entry_id.entry_serial) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
if (priority == IEC104_ASDU_PRIORITY_HIGH) {
|
||
IEC104_AsduPriorityTicketState_t *lane =
|
||
&queue->priority_state->lanes[(unsigned)info.business_priority - 1U];
|
||
if (info.response_ticket != lane->head_sequence) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
if (lane->cancel_end > lane->known_from) { lane->known_from = lane->cancel_end; }
|
||
lane->cancel_begin = lane->head_sequence;
|
||
lane->head_sequence += 16U;
|
||
lane->cancel_end = lane->head_sequence;
|
||
}
|
||
Strict_Delete_Bytes(&queue->priority_queues[priority], index,
|
||
(uint32_t)info.entry_header_length + info.asdu_length);
|
||
Strict_Recount(queue, priority);
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
sequence = queue->high_head_sequence;
|
||
result = Remove_Head_With_Expected_State(queue, priority, entry_id, QUEUE_ENTRY_PENDING);
|
||
if (result == IEC104_ASDU_QUEUE_OK && priority == IEC104_ASDU_PRIORITY_HIGH) {
|
||
Record_High_Cancellation(queue, sequence, sequence + 1U);
|
||
}
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 功能:按保留顺序查看指定偏移的原始ASDU,供检查和恢复使用。
|
||
* 输入参数:queue/priority——目标字节环;offset——从最旧保留条目起的条目序位,0为队首,单位不是字节。
|
||
* 输出参数:head_info——非NULL,仅OK时写入对应条目及其已提交状态。
|
||
* 返回值:OK找到条目,EMPTY表示偏移超出已有条目,ERROR表示参数或经过的条目非法。
|
||
* 说明:兼容模式从队首读取offset+1条记录;严格模式完整校验后按保留原文计数,跳过覆盖身份头。
|
||
* 同时包含NORMAL待ACK与待发原文;序位不是严格发送次序,正常发送须使用Peek_Unsubmitted。
|
||
* 输出原文的借用有效期与Peek一致,不生成副本。
|
||
*/
|
||
static IEC104_AsduQueueResult_t Get_Other_Entry_By_Offset(
|
||
const IEC104_AsduQueue_t *queue, IEC104_AsduPriority_t priority,
|
||
uint32_t offset, IEC104_AsduHeadInfo_t *head_info)
|
||
{
|
||
const IEC104_AsduPriorityQueue_t *bucket;
|
||
IEC104_AsduHeadInfo_t info;
|
||
uint32_t i, index;
|
||
|
||
if (!Queue_State_Valid(queue) || !Priority_Valid(priority) || head_info == NULL) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
if (queue->strict_business_priority != 0U) { return Strict_Peek(queue, priority, 0, offset, head_info, NULL); }
|
||
bucket = &queue->priority_queues[priority];
|
||
if (offset >= bucket->entry_count) {
|
||
return IEC104_ASDU_QUEUE_EMPTY;
|
||
}
|
||
index = bucket->head_index;
|
||
for (i = 0U; i <= offset; ++i) {
|
||
if (!Read_Entry_With_Expected_State(queue, priority, index,
|
||
i < bucket->entry_count - bucket->unsubmitted_count ?
|
||
QUEUE_ENTRY_RETAINED_UNACKED : QUEUE_ENTRY_PENDING, &info)) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
index = Next_Entry_Index(bucket, index, &info);
|
||
}
|
||
if (priority == IEC104_ASDU_PRIORITY_HIGH) { info.response_ticket = queue->high_head_sequence + offset; }
|
||
*head_info = info;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:按全局受理顺序查询普通环及SOE区共同保留的NORMAL原文。
|
||
* 输入参数:queue/priority——有效队列和生命周期;offset——保留原文序位,0为最旧。
|
||
* 输出参数:head_info——成功时写入借用视图。
|
||
* 返回值:OK找到;EMPTY越界;ERROR参数或状态非法。
|
||
* 说明:SOE占位仍属保留原文,退休身份不占序位;HIGH及未绑定SOE的旧路径保持原规则。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Get_Entry_By_Offset(const IEC104_AsduQueue_t *queue,
|
||
IEC104_AsduPriority_t priority, uint32_t offset, IEC104_AsduHeadInfo_t *head_info)
|
||
{
|
||
IEC104_AsduHeadInfo_t other, soe, selected;
|
||
uint32_t other_offset = 0U, i;
|
||
uint64_t after = 0U;
|
||
if (!Queue_State_Valid(queue) || !Priority_Valid(priority) || head_info == NULL) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
if (priority != IEC104_ASDU_PRIORITY_NORMAL || queue->soe.capacity == 0U) {
|
||
return Get_Other_Entry_By_Offset(queue, priority, offset, head_info);
|
||
}
|
||
for (i = 0U; i <= offset; ++i) {
|
||
IEC104_AsduQueueResult_t a = Get_Other_Entry_By_Offset(queue, priority, other_offset, &other);
|
||
IEC104_AsduQueueResult_t b = Soe_Peek(queue, 0, after, &soe);
|
||
if (a == IEC104_ASDU_QUEUE_ERROR || b == IEC104_ASDU_QUEUE_ERROR) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
if (a == IEC104_ASDU_QUEUE_EMPTY && b == IEC104_ASDU_QUEUE_EMPTY) { return IEC104_ASDU_QUEUE_EMPTY; }
|
||
if (b == IEC104_ASDU_QUEUE_OK && (a != IEC104_ASDU_QUEUE_OK || soe.admission_order < other.admission_order)) {
|
||
selected = soe; after = soe.admission_order;
|
||
} else { selected = other; ++other_offset; }
|
||
}
|
||
*head_info = selected;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:查看两类NORMAL存储中最早保留的原文,HIGH仍查看普通环队首。
|
||
* 输入参数:queue/priority——有效队列及生命周期。
|
||
* 输出参数:head_info——成功时写入原文视图。
|
||
* 返回值:OK、EMPTY或ERROR。
|
||
* 说明:不受启动门禁影响,SOE占位可用于检查或显式清除,但不能由Mark_Submitted交接。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Peek_Head(const IEC104_AsduQueue_t *queue,
|
||
IEC104_AsduPriority_t priority, IEC104_AsduHeadInfo_t *head_info)
|
||
{
|
||
return IEC104_AsduQueue_Get_Entry_By_Offset(queue, priority, 0U, head_info);
|
||
}
|
||
|
||
/**
|
||
* 功能:在普通环和独立SOE区之间选取当前允许交接的NORMAL候选。
|
||
* 输入参数:queue/priority——有效队列及生命周期。
|
||
* 输出参数:head_info——成功时写入待发原文、身份与业务等级。
|
||
* 返回值:OK找到;EMPTY无可发候选;ERROR参数或状态非法。
|
||
* 说明:兼容模式按serial,严格模式先等级再serial;两区共同受启动及EOF门禁,SOE同级占位只阻挡其后SOE。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Peek_Unsubmitted(const IEC104_AsduQueue_t *queue,
|
||
IEC104_AsduPriority_t priority, IEC104_AsduHeadInfo_t *head_info)
|
||
{
|
||
IEC104_AsduHeadInfo_t other, soe;
|
||
IEC104_AsduQueueResult_t a, b;
|
||
if (!Queue_State_Valid(queue) || !Priority_Valid(priority) || head_info == NULL) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
a = Peek_Other_Unsubmitted(queue, priority, &other);
|
||
if (priority != IEC104_ASDU_PRIORITY_NORMAL || queue->soe.capacity == 0U) {
|
||
if (a == IEC104_ASDU_QUEUE_OK) { *head_info = other; }
|
||
return a;
|
||
}
|
||
b = Soe_Peek(queue, 1, 0U, &soe);
|
||
if (a == IEC104_ASDU_QUEUE_ERROR || b == IEC104_ASDU_QUEUE_ERROR) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
if (a != IEC104_ASDU_QUEUE_OK && b != IEC104_ASDU_QUEUE_OK) { return IEC104_ASDU_QUEUE_EMPTY; }
|
||
if (b == IEC104_ASDU_QUEUE_OK && (a != IEC104_ASDU_QUEUE_OK ||
|
||
(queue->strict_business_priority != 0U && soe.business_priority < other.business_priority) ||
|
||
((queue->strict_business_priority == 0U || soe.business_priority == other.business_priority) &&
|
||
soe.admission_order < other.admission_order))) { *head_info = soe; }
|
||
else { *head_info = other; }
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:查询指定优先级当前未占用的字节数。
|
||
* 输入参数:queue/priority——目标字节环。
|
||
* 输出参数:free_bytes——非NULL,仅OK时写入容量减used_bytes。
|
||
* 返回值:OK或ERROR,失败不改输出。
|
||
* 说明:按HIGH2字节及NORMAL实际结构头长核算;NORMAL另检查编号窗口。
|
||
* NORMAL这里只报告普通字节环;SOE固定槽及退休身份容量通过Get_Soe_Status另查,不能合并挪用。
|
||
* 空闲不足时NORMAL仍可能通过覆盖旧原文入队;本查询不预留空间,也不能代替Enqueue的结果检查。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Get_Free_Bytes(
|
||
const IEC104_AsduQueue_t *queue, IEC104_AsduPriority_t priority, uint32_t *free_bytes)
|
||
{
|
||
if (!Queue_State_Valid(queue) || !Priority_Valid(priority) || free_bytes == NULL) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
*free_bytes = queue->priority_queues[priority].byte_capacity - queue->priority_queues[priority].used_bytes;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:逐条验证完整占用区及提交分界;NORMAL另验证条目身份顺序和头内状态。
|
||
* 输入参数:queue/priority——控制字段与优先级均已验证的队列和目标桶。
|
||
* 输出参数:无,不修改控制字段或原文字节。
|
||
* 返回值:全部条目恰好组成有效FIFO返回1,否则0。
|
||
* 说明:Rebind在更新控制字段前验证两个桶;remaining防止错误长度绕回已读区。
|
||
* Read_Entry_With_Expected_State先限制各编号到next的距离,再按循环先后验证相邻条目,允许UINT64_MAX接1。
|
||
* 耗时随条目数增长;只检查存储布局、身份及记账状态,不解析ASDU业务对象。
|
||
*/
|
||
static int Bucket_Entries_Valid(const IEC104_AsduQueue_t *queue, IEC104_AsduPriority_t priority)
|
||
{
|
||
const IEC104_AsduPriorityQueue_t *bucket = &queue->priority_queues[priority];
|
||
IEC104_AsduHeadInfo_t info;
|
||
uint32_t index = bucket->head_index;
|
||
uint32_t remaining = bucket->used_bytes;
|
||
uint32_t retained_unacked_count = bucket->entry_count - bucket->unsubmitted_count;
|
||
uint64_t previous_serial = 0U;
|
||
uint32_t i, size;
|
||
|
||
for (i = 0U; i < bucket->entry_count; ++i) {
|
||
if (remaining < Entry_Header_Length(priority) + IEC104_ASDU_HEADER_LEN ||
|
||
(i == retained_unacked_count && index != bucket->unsubmitted_index) ||
|
||
!Read_Entry_With_Expected_State(queue, priority, index,
|
||
i < retained_unacked_count ? QUEUE_ENTRY_RETAINED_UNACKED : QUEUE_ENTRY_PENDING, &info)) {
|
||
return 0;
|
||
}
|
||
size = (uint32_t)info.entry_header_length + info.asdu_length;
|
||
if (size > remaining || (priority == IEC104_ASDU_PRIORITY_NORMAL && i != 0U &&
|
||
!Normal_Serial_Before(queue, previous_serial, info.entry_id.entry_serial))) {
|
||
return 0;
|
||
}
|
||
remaining -= size;
|
||
previous_serial = info.entry_id.entry_serial;
|
||
index = Ring_Advance(bucket, index, size);
|
||
}
|
||
return remaining == 0U && index == bucket->tail_index;
|
||
}
|
||
|
||
/**
|
||
* 功能:已完整验证条目布局后,读取104固定公共头进行筛选;不限制COT、对象数或业务载荷。
|
||
* 输入参数:bucket——const IEC104_AsduPriorityQueue_t *所指数据。
|
||
* entry_index——索引。
|
||
* filter——筛选条件。
|
||
* 输出参数:无。
|
||
* 返回值:返回common_address == filter->common_address && (filter->type_bitmap[header[0] / 8U] & (uint8_t)(1U << (header[0] % 8U))) != 0U的结果。
|
||
* 说明:CA字节也可能跨环尾,必须经Ring_Read取得,不能假定原文连续。
|
||
*/
|
||
static int Normal_Entry_Matches(const IEC104_AsduPriorityQueue_t *bucket,
|
||
uint32_t entry_index, const IEC104_AsduNormalFilter_t *filter)
|
||
{
|
||
uint8_t header[IEC104_ASDU_HEADER_LEN];
|
||
uint16_t common_address;
|
||
Ring_Read(bucket, Ring_Advance(bucket, entry_index, IEC104_ASDU_NORMAL_ENTRY_HEADER_LEN),
|
||
header, sizeof(header));
|
||
common_address = (uint16_t)((uint16_t)header[4] | ((uint16_t)header[5] << 8));
|
||
return common_address == filter->common_address &&
|
||
(filter->type_bitmap[header[0] / 8U] & (uint8_t)(1U << (header[0] % 8U))) != 0U;
|
||
}
|
||
|
||
/**
|
||
* 功能:只读预检:完整验证NORMAL(含待ACK原文及严格模式覆盖身份头)后才计数;失败不写输出。
|
||
* 输入参数:queue——ASDU队列。
|
||
* connection_generation——连接代次。
|
||
* filter——筛选条件。
|
||
* count——条目数量。
|
||
* 输出参数:count——条目数量的更新结果。
|
||
* 返回值:按处理分支返回IEC104_ASDU_QUEUE_ERROR、IEC104_ASDU_QUEUE_OK。
|
||
* 说明:即使位图全0或待发区为空,也不绕过布局/身份检查,避免把损坏误报成“没有匹配”。
|
||
* 位图只判断Type和CA,允许通用Enqueue接受的任意6..249字节ASDU原文。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Count_Unsubmitted_Normal(
|
||
const IEC104_AsduQueue_t *queue, uint64_t connection_generation,
|
||
const IEC104_AsduNormalFilter_t *filter, uint32_t *count)
|
||
{
|
||
const IEC104_AsduPriorityQueue_t *bucket;
|
||
IEC104_AsduNormalEntryHeader_t header;
|
||
uint32_t index, entry, matched = 0U;
|
||
uint16_t soe_index;
|
||
if (count == NULL || filter == NULL || filter->common_address == 0U ||
|
||
filter->common_address == UINT16_MAX || !Queue_State_Valid(queue) ||
|
||
connection_generation != queue->connection_generation ||
|
||
(queue->strict_business_priority == 0U && !Bucket_Entries_Valid(queue, IEC104_ASDU_PRIORITY_NORMAL))) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
for (soe_index = 0U; soe_index < queue->soe.capacity; ++soe_index) {
|
||
const uint8_t *slot = queue->soe.slots + (uint32_t)soe_index * IEC104_ASDU_SOE_SLOT_LEN;
|
||
const uint8_t *bytes = slot + 9U;
|
||
unsigned state = slot[8] & 3U;
|
||
if ((state == SOE_PENDING || state == SOE_DEFERRED) &&
|
||
(unsigned)(bytes[4] | ((unsigned)bytes[5] << 8)) == filter->common_address &&
|
||
(filter->type_bitmap[bytes[0] / 8U] & (1U << (bytes[0] % 8U))) != 0U) { ++matched; }
|
||
}
|
||
bucket = &queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL];
|
||
if (queue->strict_business_priority != 0U) {
|
||
uint32_t remaining = bucket->used_bytes;
|
||
index = bucket->head_index;
|
||
while (remaining != 0U) {
|
||
uint32_t size;
|
||
Ring_Read(bucket, index, &header, sizeof(header));
|
||
size = IEC104_ASDU_NORMAL_ENTRY_HEADER_LEN + (uint32_t)header.asdu_length;
|
||
if (header.entry_state == NORMAL_HEADER_PENDING && Normal_Entry_Matches(bucket, index, filter)) { ++matched; }
|
||
index = Ring_Advance(bucket, index, size);
|
||
remaining -= size;
|
||
}
|
||
*count = matched;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
index = bucket->unsubmitted_index;
|
||
for (entry = 0U; entry < bucket->unsubmitted_count; ++entry) {
|
||
Ring_Read(bucket, index, &header, sizeof(header));
|
||
if (Normal_Entry_Matches(bucket, index, filter)) { ++matched; }
|
||
index = Ring_Advance(bucket, index, IEC104_ASDU_NORMAL_ENTRY_HEADER_LEN + (uint32_t)header.asdu_length);
|
||
}
|
||
*count = matched;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:逻辑向前压缩:destination在本次占用区内严格早于source,但物理下标可更大。
|
||
* 输入参数:bucket——IEC104_AsduPriorityQueue_t *所指数据。
|
||
* destination——目标存储。
|
||
* source——源数据。
|
||
* length——字节长度。
|
||
* 输出参数:bucket——IEC104_AsduPriorityQueue_t *所指数据的更新结果。
|
||
* 返回值:无。
|
||
* 说明:每段同时限制源、目标的环尾;段内使用memmove处理重叠。按逻辑先后处理各段,
|
||
* 所有写入均落在已读取范围或更早位置,不破坏下一段/下一条目的原文,无需大临时数组。
|
||
*/
|
||
static void Move_Normal_Entry_Forward(IEC104_AsduPriorityQueue_t *bucket,
|
||
uint32_t destination, uint32_t source, uint32_t length)
|
||
{
|
||
while (length != 0U) {
|
||
uint32_t part = bucket->byte_capacity - source;
|
||
if (part > bucket->byte_capacity - destination) { part = bucket->byte_capacity - destination; }
|
||
if (part > length) { part = length; }
|
||
memmove(bucket->bytes + destination, bucket->bytes + source, part);
|
||
source = Ring_Advance(bucket, source, part);
|
||
destination = Ring_Advance(bucket, destination, part);
|
||
length -= part;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 功能:先完成全部可失败检查,再稳定压缩删除匹配的待发条目;已交接原文及覆盖身份头均保留。
|
||
* 输入参数:queue——ASDU队列。
|
||
* connection_generation——连接代次。
|
||
* filter——筛选条件。
|
||
* removed——删除数量。
|
||
* 输出参数:queue——ASDU队列的更新结果。
|
||
* removed——删除数量的更新结果。
|
||
* 返回值:开始移动后没有返回ERROR的分支。
|
||
* 说明:兼容模式仅压缩未提交后缀;严格模式状态交错,可能移动已交接条目,但不会改变其身份与ACK义务。
|
||
* 清除不涉及发送记录,不修改next_entry_serial、覆盖待ACK记账、HIGH或EOF发送限制。
|
||
* 串行调用契约保证预检与移动之间无人改队列;开始移动后没有返回ERROR的分支。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Discard_Unsubmitted_Normal(
|
||
IEC104_AsduQueue_t *queue, uint64_t connection_generation,
|
||
const IEC104_AsduNormalFilter_t *filter, uint32_t *removed)
|
||
{
|
||
IEC104_AsduPriorityQueue_t *bucket;
|
||
IEC104_AsduNormalEntryHeader_t header;
|
||
uint32_t matched, source, destination, entry, size, removed_bytes = 0U;
|
||
uint32_t soe_removed = 0U;
|
||
uint16_t soe_index;
|
||
if (removed == NULL || IEC104_AsduQueue_Count_Unsubmitted_Normal(
|
||
queue, connection_generation, filter, &matched) != IEC104_ASDU_QUEUE_OK) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
if (matched == 0U) {
|
||
*removed = 0U;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
for (soe_index = 0U; soe_index < queue->soe.capacity; ++soe_index) {
|
||
uint8_t *slot = queue->soe.slots + (uint32_t)soe_index * IEC104_ASDU_SOE_SLOT_LEN;
|
||
const uint8_t *bytes = slot + 9U;
|
||
unsigned state = slot[8] & 3U;
|
||
if ((state == SOE_PENDING || state == SOE_DEFERRED) &&
|
||
(unsigned)(bytes[4] | ((unsigned)bytes[5] << 8)) == filter->common_address &&
|
||
(filter->type_bitmap[bytes[0] / 8U] & (1U << (bytes[0] % 8U))) != 0U) {
|
||
memset(slot, 0, IEC104_ASDU_SOE_SLOT_LEN); ++soe_removed;
|
||
}
|
||
}
|
||
queue->soe.discarded_count += soe_removed;
|
||
queue->discarded_asdu_count += soe_removed;
|
||
matched -= soe_removed;
|
||
if (matched == 0U) { *removed = soe_removed; return IEC104_ASDU_QUEUE_OK; }
|
||
bucket = &queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL];
|
||
if (queue->strict_business_priority != 0U) {
|
||
uint32_t remaining = bucket->used_bytes;
|
||
source = destination = bucket->head_index;
|
||
while (remaining != 0U) {
|
||
Ring_Read(bucket, source, &header, sizeof(header));
|
||
size = IEC104_ASDU_NORMAL_ENTRY_HEADER_LEN + (uint32_t)header.asdu_length;
|
||
if (header.entry_state == NORMAL_HEADER_PENDING && Normal_Entry_Matches(bucket, source, filter)) {
|
||
removed_bytes += size;
|
||
} else {
|
||
if (source != destination) { Move_Normal_Entry_Forward(bucket, destination, source, size); }
|
||
destination = Ring_Advance(bucket, destination, size);
|
||
}
|
||
source = Ring_Advance(bucket, source, size);
|
||
remaining -= size;
|
||
}
|
||
bucket->used_bytes -= removed_bytes;
|
||
bucket->tail_index = destination;
|
||
if (bucket->used_bytes == 0U) { bucket->head_index = bucket->tail_index = 0U; }
|
||
Strict_Recount(queue, IEC104_ASDU_PRIORITY_NORMAL);
|
||
queue->discarded_asdu_count += matched;
|
||
*removed = matched + soe_removed;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
source = bucket->unsubmitted_index;
|
||
destination = source;
|
||
for (entry = 0U; entry < bucket->unsubmitted_count; ++entry) {
|
||
/* 移动可能覆盖原条目头,先保存长度;source遍历全部条目,destination只跨过保留条目。 */
|
||
Ring_Read(bucket, source, &header, sizeof(header));
|
||
size = IEC104_ASDU_NORMAL_ENTRY_HEADER_LEN + (uint32_t)header.asdu_length;
|
||
if (Normal_Entry_Matches(bucket, source, filter)) {
|
||
removed_bytes += size;
|
||
} else {
|
||
if (destination != source) { Move_Normal_Entry_Forward(bucket, destination, source, size); }
|
||
destination = Ring_Advance(bucket, destination, size);
|
||
}
|
||
source = Ring_Advance(bucket, source, size);
|
||
}
|
||
bucket->tail_index = destination;
|
||
bucket->used_bytes -= removed_bytes;
|
||
bucket->entry_count -= matched;
|
||
bucket->unsubmitted_count -= matched;
|
||
if (bucket->entry_count == 0U) {
|
||
bucket->head_index = 0U;
|
||
bucket->tail_index = 0U;
|
||
bucket->unsubmitted_index = 0U;
|
||
} else if (bucket->unsubmitted_count == 0U) {
|
||
/* 仍有待ACK前缀时不能把整个桶归零,待发游标仅移到压缩后的尾端。 */
|
||
bucket->unsubmitted_index = bucket->tail_index;
|
||
}
|
||
queue->discarded_asdu_count += matched;
|
||
*removed = matched + soe_removed;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:将HIGH桶恢复为空环,供Reset_High和Rebind在预检成功后调用。
|
||
* 输入参数:queue——已验证的队列,调用方已满足清空当前HIGH业务的前提。
|
||
* 输出参数:HIGH的头尾下标、字节占用、条目数及待发游标归零,所有HIGH原文借用失效。
|
||
* 返回值:无;此内部函数不重复检查参数或连接代次。
|
||
* 说明:非空时记取消区间并令HIGH head=next;保留数组、容量、NORMAL及两个next进度。
|
||
*/
|
||
static void Reset_High_Bucket(IEC104_AsduQueue_t *queue)
|
||
{
|
||
IEC104_AsduPriorityQueue_t *bucket = &queue->priority_queues[IEC104_ASDU_PRIORITY_HIGH];
|
||
|
||
if (queue->strict_business_priority != 0U) {
|
||
unsigned rank;
|
||
for (rank = 0U; rank < IEC104_BUSINESS_PRIORITY_COUNT - 1U; ++rank) {
|
||
IEC104_AsduPriorityTicketState_t *lane = &queue->priority_state->lanes[rank];
|
||
if (lane->head_sequence != lane->next_sequence) {
|
||
/* 每级只记最近非空取消区间,空清理不覆盖历史。
|
||
* 新取消替代旧区间后,将不可再判定的早期票据保守视为取消,避免误报HANDED。 */
|
||
if (lane->cancel_end > lane->known_from) { lane->known_from = lane->cancel_end; }
|
||
lane->cancel_begin = lane->head_sequence;
|
||
lane->cancel_end = lane->next_sequence;
|
||
lane->head_sequence = lane->next_sequence;
|
||
}
|
||
}
|
||
} else {
|
||
Record_High_Cancellation(queue, queue->high_head_sequence, queue->high_next_sequence);
|
||
}
|
||
queue->high_head_sequence = queue->high_next_sequence;
|
||
|
||
bucket->head_index = 0U;
|
||
bucket->tail_index = 0U;
|
||
bucket->used_bytes = 0U;
|
||
bucket->entry_count = 0U;
|
||
bucket->unsubmitted_index = 0U;
|
||
bucket->unsubmitted_count = 0U;
|
||
}
|
||
|
||
/**
|
||
* 功能:清空当前连接的HIGH待发条目,供Manager初始化、受理新STOP及从STOPPED受理START时调用。
|
||
* 输入参数:queue——有效队列;connection_generation——匹配当前绑定的连接代次。
|
||
* 输出参数:仅HIGH桶的条目计数、字节占用及游标清零;所有HIGH原文借用失效。
|
||
* 返回值:OK或ERROR;非法状态或旧代次不修改队列。
|
||
* 说明:HIGH已提交原文已经出队,因此不影响当前I帧、发送记录、ACK义务和NORMAL数据。
|
||
* 不影响NORMAL的next_entry_serial;HIGH只有代次,没有可跨队首变化保存的条目身份。
|
||
* 这是同一代次内的清理,不执行重连,也不会把NORMAL待ACK条目重新变成待发。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Reset_High(
|
||
IEC104_AsduQueue_t *queue, uint64_t connection_generation)
|
||
{
|
||
if (!Queue_State_Valid(queue) || connection_generation != queue->connection_generation) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
Reset_High_Bucket(queue);
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:EOF后不再可能收到新I帧的ACK,停止选取全部尚未交接的NORMAL,原文留给下一连接。
|
||
* 输入参数:queue——ASDU队列。
|
||
* connection_generation——连接代次。
|
||
* 输出参数:queue——ASDU队列的更新结果。
|
||
* 返回值:按处理分支返回IEC104_ASDU_QUEUE_ERROR、IEC104_ASDU_QUEUE_OK。
|
||
* 说明:不改已提交条目、SendBuffer或HIGH;当前已编号帧仍由发送模块完成。
|
||
* 入队、覆盖、取消条目及重复调用都不解除限制,只有Detach/Rebind恢复选取。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Limit_Normal_Send(
|
||
IEC104_AsduQueue_t *queue, uint64_t connection_generation)
|
||
{
|
||
if (!Queue_State_Valid(queue) || connection_generation == 0U ||
|
||
connection_generation != queue->connection_generation) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
queue->normal_send_limited = 1U;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:设置后续选帧资格,不改入队、覆盖、已提交原文或ACK回收;暂停期间仍可积累新事件。
|
||
* 输入参数:queue——ASDU队列。
|
||
* connection_generation——连接代次。
|
||
* pending——待处理状态。
|
||
* 输出参数:queue——ASDU队列的更新结果。
|
||
* 返回值:按处理分支返回IEC104_ASDU_QUEUE_ERROR、IEC104_ASDU_QUEUE_OK。
|
||
* 说明:pending只能为0/1,代次须匹配(离线0也可);校验失败不改队列,成功也不自行唤醒网络。
|
||
* 启动暂停与EOF是独立原因;解除启动暂停不能恢复EOF连接上的NORMAL发送。
|
||
* 队列不知道业务完成或实际START轮次,调用方须在上层校验activation_id后才能传pending=0。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Set_Startup_Pending(
|
||
IEC104_AsduQueue_t *queue, uint64_t connection_generation, uint8_t pending)
|
||
{
|
||
if (!Queue_State_Valid(queue) || pending > 1U || connection_generation != queue->connection_generation) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
queue->startup_pending = pending;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:Detach/Rebind共用的绑定转换:入口已核对旧状态及目标代次,先验证全部条目再统一修改。
|
||
* 输入参数:queue——ASDU队列。
|
||
* connection_generation——连接代次。
|
||
* 输出参数:queue——ASDU队列的更新结果。
|
||
* 返回值: 0表示解除绑定,非零表示新会话;
|
||
* 说明:0表示解除绑定,非零表示新会话;均恢复现存NORMAL,清空HIGH和旧连接独有的记账。
|
||
* 严格模式只丢弃无原文的覆盖身份头,稳定保留其余条目的serial、业务等级和受理顺序;新连接另分配N(S)。
|
||
* 旧I/O和Manager必须先结束,才可删除旧ACK身份;恢复原文不等于对端从未收到,重放可重复。
|
||
* EOF随旧连接结束而解除;startup_pending有意保留,绑定原文不等于应用已完成新一轮启动。
|
||
*/
|
||
static IEC104_AsduQueueResult_t Reset_Connection_Binding(
|
||
IEC104_AsduQueue_t *queue, uint64_t connection_generation)
|
||
{
|
||
unsigned i;
|
||
IEC104_AsduPriorityQueue_t *normal;
|
||
IEC104_AsduNormalEntryHeader_t header;
|
||
uint32_t index, submitted_count, entry;
|
||
|
||
if (queue->strict_business_priority != 0U) {
|
||
uint32_t source, destination, remaining, kept = 0U;
|
||
if (!Strict_Queue_Valid(queue)) { return IEC104_ASDU_QUEUE_ERROR; }
|
||
normal = &queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL];
|
||
source = destination = normal->head_index;
|
||
remaining = normal->used_bytes;
|
||
while (remaining != 0U) {
|
||
uint32_t size;
|
||
Ring_Read(normal, source, &header, sizeof(header));
|
||
size = IEC104_ASDU_NORMAL_ENTRY_HEADER_LEN + (uint32_t)header.asdu_length;
|
||
if (header.entry_state != NORMAL_HEADER_OVERWRITTEN) {
|
||
if (destination != source) { Move_Normal_Entry_Forward(normal, destination, source, size); }
|
||
header.entry_state = NORMAL_HEADER_PENDING;
|
||
Ring_Write(normal, destination, &header, sizeof(header));
|
||
destination = Ring_Advance(normal, destination, size);
|
||
kept += size;
|
||
}
|
||
source = Ring_Advance(normal, source, size);
|
||
remaining -= size;
|
||
}
|
||
normal->used_bytes = kept;
|
||
normal->tail_index = destination;
|
||
if (kept == 0U) { normal->head_index = normal->tail_index = 0U; }
|
||
Strict_Recount(queue, IEC104_ASDU_PRIORITY_NORMAL);
|
||
Reset_High_Bucket(queue);
|
||
for (i = 0U; i < queue->soe.capacity; ++i) {
|
||
uint8_t *slot = queue->soe.slots + (uint32_t)i * IEC104_ASDU_SOE_SLOT_LEN;
|
||
if ((slot[8] & 3U) == SOE_UNACKED) { slot[8] = (uint8_t)((slot[8] & ~3U) | SOE_PENDING); }
|
||
}
|
||
if (queue->soe.retired_capacity != 0U) {
|
||
memset(queue->soe.retired, 0, (size_t)queue->soe.retired_capacity * sizeof(*queue->soe.retired));
|
||
}
|
||
queue->connection_generation = connection_generation;
|
||
queue->overwritten_unacked_serial_begin = queue->overwritten_unacked_serial_end = 0U;
|
||
queue->normal_send_limited = 0U;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
for (i = 0U; i < IEC104_ASDU_PRIORITY_COUNT; ++i) {
|
||
if (!Bucket_Entries_Valid(queue, (IEC104_AsduPriority_t)i)) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
}
|
||
/* 全部预检通过后才写头。后续没有可失败分支,保持ERROR不改字节的契约。 */
|
||
normal = &queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL];
|
||
index = normal->head_index;
|
||
submitted_count = normal->entry_count - normal->unsubmitted_count;
|
||
for (entry = 0U; entry < submitted_count; ++entry) {
|
||
Ring_Read(normal, index, &header, sizeof(header));
|
||
header.entry_state = NORMAL_HEADER_PENDING;
|
||
Ring_Write(normal, index, &header, sizeof(header));
|
||
index = Ring_Advance(normal, index, IEC104_ASDU_NORMAL_ENTRY_HEADER_LEN + (uint32_t)header.asdu_length);
|
||
}
|
||
/* NORMAL全部保留条目恢复为待发;HIGH直接清空。 */
|
||
Reset_High_Bucket(queue);
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL].unsubmitted_index =
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL].head_index;
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL].unsubmitted_count =
|
||
queue->priority_queues[IEC104_ASDU_PRIORITY_NORMAL].entry_count;
|
||
for (i = 0U; i < queue->soe.capacity; ++i) {
|
||
uint8_t *slot = queue->soe.slots + (uint32_t)i * IEC104_ASDU_SOE_SLOT_LEN;
|
||
if ((slot[8] & 3U) == SOE_UNACKED) { slot[8] = (uint8_t)((slot[8] & ~3U) | SOE_PENDING); }
|
||
}
|
||
if (queue->soe.retired_capacity != 0U) {
|
||
memset(queue->soe.retired, 0, (size_t)queue->soe.retired_capacity * sizeof(*queue->soe.retired));
|
||
}
|
||
queue->connection_generation = connection_generation;
|
||
queue->overwritten_unacked_count = 0U;
|
||
queue->overwritten_unacked_serial_end = 0U;
|
||
queue->overwritten_unacked_serial_begin = 0U;
|
||
queue->normal_send_limited = 0U;
|
||
return IEC104_ASDU_QUEUE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:结束旧会话绑定,立即恢复服务级离线缓存状态。
|
||
* 输入参数:queue——有效队列;connection_generation——要解除的当前非零代次。
|
||
* 输出参数:queue——ASDU队列的更新结果。
|
||
* 返回值:OK保留现存NORMAL并切换到代次0;ERROR不修改队列。
|
||
* 说明:先结束旧I/O及Manager,处理已知ACK事实;随后允许新NORMAL以代次0入队。
|
||
* 清空HIGH、覆盖待ACK记账及EOF限制,不恢复已被容量覆盖的原文,也不复制或移动ASDU。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Detach(
|
||
IEC104_AsduQueue_t *queue, uint64_t connection_generation)
|
||
{
|
||
if (!Queue_State_Valid(queue) || connection_generation == 0U ||
|
||
connection_generation != queue->connection_generation) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
return Reset_Connection_Binding(queue, 0U);
|
||
}
|
||
|
||
/**
|
||
* 功能:把现存NORMAL绑定到新会话,全部恢复为待发,供新STARTDT完成后重新组帧。
|
||
* 输入参数:queue——有效队列,可以尚未绑定;connection_generation——未使用过的非零新代次。
|
||
* 输出参数:queue——ASDU队列的更新结果。
|
||
* 返回值:OK或ERROR;先完整验证两个环,ERROR不修改控制字段或字节。
|
||
* 说明:跨连接不复用代次由调用方保证;旧I/O及Manager必须先结束,已知ACK事实先处理。
|
||
* 保留原文顺序、serial及next_entry_serial,旧身份和借用失效;清空HIGH、覆盖记账和EOF限制。
|
||
* 未ACK不等于对端未收到,原文重发可能重复;容量已覆盖的原文不能恢复。
|
||
*/
|
||
IEC104_AsduQueueResult_t IEC104_AsduQueue_Rebind(
|
||
IEC104_AsduQueue_t *queue, uint64_t connection_generation)
|
||
{
|
||
if (!Queue_State_Valid(queue) || connection_generation == 0U ||
|
||
connection_generation == queue->connection_generation) {
|
||
return IEC104_ASDU_QUEUE_ERROR;
|
||
}
|
||
return Reset_Connection_Binding(queue, connection_generation);
|
||
}
|
||
|
||
/**
|
||
* 功能:清除业务队列控制结构,明确丢弃所有保留条目的引用。
|
||
* 输入参数:queue——待终止对象,允许NULL。
|
||
* 输出参数:queue——非NULL时清零;调用方提供的字节数组不清零、不释放。
|
||
* 返回值:无。
|
||
* 说明:先完成业务回收并结束所有借用;Manager.Abort不会隐式调用此接口。
|
||
* 清零后队列不再处于已初始化状态,重新使用须Init;如需保留NORMAL原文恢复连接,
|
||
* 应结束旧Manager后使用Rebind,不应先调用本接口丢弃队列引用。
|
||
*/
|
||
void IEC104_AsduQueue_Abort(IEC104_AsduQueue_t *queue)
|
||
{
|
||
if (queue != NULL) {
|
||
memset(queue, 0, sizeof(*queue));
|
||
}
|
||
}
|