feat(iec104): 把 104 骨架完整迁到 MCU 板端并在台架跑通总召

把 IEC104 从模拟器端整体搬到板端:协议栈原先由独立 iec104Task 驱动,
现在改为 netTask 单 owner 的 threadless 驱动,退役独立任务。

驱动与后端:
- 新增 iec104_net_mcu.c/.h,实现 IEC104_Net.h 的 11 个 ABI 函数,
  用槽表加单调句柄映射 net_socket 的单 fd 模型;CLIENT 拆除只走
  net_drop_conn_locked,绝不 net_close 监听 fd。
- net_socket 增 5 个 owner-only 接口(net_accept_locked、
  net_drop_conn_locked、net_pending_bytes、net_send_try、
  net_mono_now_ms)。
- iec104_serv.c 重写为 iec104_serv_net_tick(),在 netTask 1ms 主循环
  里驱动 core;无 osDelay/阻塞调用,喂狗是第一句。
- Src/freertos.c 删除 iec104Task 三件套,对应栈水位探针表项同步删除。

内存:目标板 192KB SRAM 装不下 vendor 默认容量档,按实测裁剪到
skeleton 档(TCP 缓冲、业务池、SOE 256->128、值槽密集化),
RW_IRAM1 118,408 B / 90.3%,留 2,178 B 余量。

台架修复(只在真硬件暴露):
- SNTP s_ctx.fd 零初始化后为 0,而 net fd 0 是合法槽位下标,
  首次代次变化时误 net_close(0) 关掉 ftpTask 的监听槽,导致 104
  连接被 FTP 的 220 banner 抢走;改为显式 .fd = -1。
- Accept 幂等:单 fd 模型下 vendor 每轮重试 Accept 会拿到同一 fd,
  后端改为会话在役时返 WOULD_BLOCK,Close 不再清槽。

验证:
- Keil 全量重建 0 error 0 warning,uvprojx 零抖动。
- 模拟器真实 TCP 13/13、transport 10/10。
- netmcu 后端单测 13/13(新增 N12)。
- 台架 serial-boot-test required 全中;板端 2404 总召 129 帧收到
  59 遥信(53 有源、全 IV)+ 16 双点 + 30 遥测(PT1 三相非零、
  UN/频率/温度按未标定置 IV)、遥控选择执行全否定、校时 COT 7。
- 期间顺带修正 UI 漂移自检文案、.serialtest required 陈旧项、
  coding-standard 对 vendor 与外部 ABI 名字的排除口径。
This commit is contained in:
2026-11-18 10:29:18 +08:00
parent 2e9272cc8a
commit 90cc9a7d99
125 changed files with 7463 additions and 5335 deletions
+1 -1
View File
@@ -28,7 +28,7 @@ typedef struct {
#define IEC104_ASDU_STRICT_HIGH_ENTRY_HEADER_LEN 19U /* 票号8、受理顺序8、长度2、等级1,逐字段memcpy。 */
#define IEC104_ASDU_SOE_SLOT_LEN 26U /* serial8、state/rank1、完整单对象ASDU17;逐字节存储,无ABI填充。 */
#define IEC104_ASDU_SOE_ASDU_LEN 17U
#define IEC104_ASDU_SOE_MAX_CAPACITY 256U
#define IEC104_ASDU_SOE_MAX_CAPACITY 128U
/* SOE统计以单对象事件为单位;pending_count不含仍待应用决定的deferred_count。 */
typedef struct {
+1 -1
View File
@@ -656,7 +656,7 @@ IEC104_TcpSubmitResult_t IEC104_Slave_Enqueue_Event(IEC104_Slave_t *self, IEC608
* 输入参数:self/asdu——从站与标准104原文;priority——业务等级;deferred——是否先保留补报占位。
* 输出参数:ticket——可选身份输出,仅OK写入;deferred为true时不可为空。
* 返回值:OK已保存,WAIT未接手,ERROR输入非法,FATAL服务队列失效。
* 说明:完整保存17字节原文;256条满后仅覆盖最旧SOE,普通缓存不能挤占;成功不表示ACK。
* 说明:完整保存17字节原文;128条满后仅覆盖最旧SOE,普通缓存不能挤占;成功不表示ACK。
*/
IEC104_TcpSubmitResult_t IEC104_Slave_Enqueue_Soe(IEC104_Slave_t *self, IEC60870_Asdu_t *asdu,
IEC104_BusinessPriority_t priority, bool deferred, IEC104_SoeTicket_t *ticket);
+1 -1
View File
@@ -149,7 +149,7 @@ typedef struct {
_Static_assert(IEC104_TCP_SERVER_CAPACITY > 0U, "Server pool must not be empty");
_Static_assert(IEC104_TCP_SOE_CAPACITY == IEC104_ASDU_SOE_MAX_CAPACITY,
"SOE storage must retain 256 events");
"SOE storage must retain 128 events");
_Static_assert(IEC104_TCP_ACK_WATCH_CAPACITY > 0U, "ACK watch pool must not be empty");
_Static_assert(sizeof(((IEC104_TcpPeerInfo_t *)0)->address) == sizeof(((IEC104_NetPeer_t *)0)->address),
"Public peer address storage must match the network endpoint");
+1 -1
View File
@@ -172,7 +172,7 @@ extern IEC104_TcpSubmitResult_t IEC104_TcpServer_Enqueue_Event(
IEC104_TcpServer_t *server, const uint8_t *asdu, uint16_t asdu_length);
/**
* 功能:将单对象Type30/31、COT3原文保存到独立256条SOE区。
* 功能:将单对象Type30/31、COT3原文保存到独立128条SOE区。
* 输入参数:server——服务;asdu/asdu_length——标准104原文,长度17;priority——业务等级;deferred——暂缓补报。
* 输出参数:ticket——可为NULL,仅OK写入服务期身份;deferred为true时必填。
* 返回值:OK已保存;WAIT未接手;ERROR参数或阶段非法;FATAL队列失效。
@@ -26,7 +26,7 @@
#define IEC104_TCP_NORMAL_CAPACITY 4096U /* 字节数:包含NORMAL条目头和原文;空间不足可覆盖最旧原文。 */
#endif
/* 独立SOE区按事件计数;单对象Type30/31每条固定26字节,另按发送记录槽数预留退休身份。 */
#define IEC104_TCP_SOE_CAPACITY 256U
#define IEC104_TCP_SOE_CAPACITY 128U
#ifndef IEC104_TCP_CONTROL_CAPACITY
#define IEC104_TCP_CONTROL_CAPACITY 8U /* 槽数:待发及正在发送的S/U帧,不占用业务I帧记录槽。 */
#endif
@@ -222,6 +222,9 @@ typedef struct {
struct IEC104_Command {
IEC104_Slave_t *slave; /* 借用的所属服务,寿命必须覆盖本实例。 */
/* [vendor 兼容补丁] 实例站点CA:首次受理请求时绑定,表不再逐点存CA。 */
uint16_t station_ca;
bool ca_bound; /* station_ca是否已绑定;绑定后不再改变。 */
IEC104_SlaveEventIdentity_t session; /* 已按FIFO消费的连接/启停通知所确定的会话身份。 */
IEC104_CommandConfig_t config; /* Create时按值复制;其中points数组借用至Destroy,期间不可变。 */
IEC104_CommandStatus_t status; /* 累计计数及模块活动状态;调度字段由Get_Status即时计算。 */
@@ -260,6 +263,8 @@ static bool Owned(const IEC104_Command_t *self)
* 输出参数:无。
* 返回值:(config->points == NULL) != (config->point_count == 0U) || config->point_count > SIZE_MAX / sizeof(*config->points)时返回false;其余分支返回false、true。
* 说明:全局禁用的类型可以保留在共享点表中;未知类型及51/64的选择模式不能入表。
* [vendor 兼容补丁] 点表不再逐点存CA,故此处只校验IOA唯一/有序;
* 请求CA与本实例站点的匹配改由Find_Point在受理时判定。
*/
static bool Valid_Points(const IEC104_CommandConfig_t *config)
{
@@ -268,40 +273,40 @@ static bool Valid_Points(const IEC104_CommandConfig_t *config)
config->point_count > SIZE_MAX / sizeof(*config->points)) { return false; }
for (i = 0U; i < config->point_count; ++i) {
const IEC104_CommandPoint_t *point = &config->points[i];
if (point->common_address == 0U || point->common_address == UINT16_MAX ||
point->ioa == 0U || point->ioa > UINT32_C(0xffffff) ||
/* [vendor 兼容补丁] 点表不再逐点存CA,CA合法性由实例站点与Slave注册表保证。 */
if (point->ioa == 0U || point->ioa > UINT32_C(0xffffff) ||
point->type_mask == 0U || (point->type_mask & (uint16_t)~COMMAND_SUPPORTED_MASK) != 0U ||
(point->mode != IEC104_COMMAND_MODE_DIRECT && point->mode != IEC104_COMMAND_MODE_SELECT_EXECUTE) ||
(point->mode == IEC104_COMMAND_MODE_SELECT_EXECUTE &&
(point->type_mask & COMMAND_DIRECT_ONLY_MASK) != 0U)) { return false; }
if (i != 0U) {
const IEC104_CommandPoint_t *previous = &config->points[i - 1U];
if (previous->common_address > point->common_address ||
(previous->common_address == point->common_address && previous->ioa >= point->ioa)) { return false; }
if (previous->ioa >= point->ioa) { return false; }
}
}
return true;
}
/**
* 功能:点表按CA/IOA排序,借用期间不可修改。
* 输入参数:self——本模块实例。
* ca——ASDU公共地址。
* 功能:点表按IOA排序,借用期间不可修改。
* 输入参数:self——本模块实例(提供表起点与长度)。
* ca——ASDU公共地址;[vendor 兼容补丁] 仅做站点归属校验,不参与点表比较。
* ioa——信息体地址。
* 输出参数:无。
* 返回值:二分查找不分配存储,空表自然返回NULL。
* 返回值:二分查找不分配存储,空表或CA与本实例站点不符时自然返回NULL。
* 说明:二分查找不分配存储,空表自然返回NULL。
* 表内不再逐点存CA,CA匹配由调用方的站点注册表判定;本函数在此保留CA参数
* 只为维持原有拒绝语义(非本实例CA的请求一律当作未知IOA处理)。
*/
static const IEC104_CommandPoint_t *Find_Point(const IEC104_Command_t *self, uint16_t ca, uint32_t ioa)
{
size_t first = 0U, last = self->config.point_count;
if (ca != self->station_ca) { return NULL; }
while (first < last) {
size_t middle = first + (last - first) / 2U;
const IEC104_CommandPoint_t *point = &self->config.points[middle];
if (point->common_address == ca && point->ioa == ioa) { return point; }
if (point->common_address < ca || (point->common_address == ca && point->ioa < ioa)) {
first = middle + 1U;
} else { last = middle; }
if (point->ioa == ioa) { return point; }
if (point->ioa < ioa) { first = middle + 1U; } else { last = middle; }
}
return NULL;
}
@@ -1242,6 +1247,10 @@ static IEC104_CommandEventResult_t Request(IEC104_Command_t *self, const IEC104_
}
ioa = Read_IOA(data);
station = IEC104_Slave_Get_Station(self->slave, info.common_address);
/* [vendor 兼容补丁] 表按IOA排序、不逐点存CA:首次受理时把请求CA绑定为实例CA。
* 站点已在编译期注册且CS104不允许运行期增删,故绑定结果与旧实现的逐点CA一致;
* 之后任何与该CA不符的请求都在Find_Point处按未知IOA处理,保持原拒绝路径不变。 */
if (!self->ca_bound && station != NULL) { self->station_ca = info.common_address; self->ca_bound = true; }
is_select = Is_Select(layout, data);
if (info.cause_of_transmission != IEC60870_ASDU_COT_ACTIVATION &&
info.cause_of_transmission != IEC60870_ASDU_COT_DEACTIVATION) {
@@ -1442,7 +1451,7 @@ void IEC104_Command_Config_Default(IEC104_CommandConfig_t *config)
* 输入参数:slave为有效服务句柄,寿命覆盖本实例;config按值复制,但其中点表借用至Destroy。
* 输出参数:成功时初始化空事务、空回复池和零统计,不建立网络监听,也不调用应用代码。
* 返回值:成功为池内不透明句柄;点表/类型位/期限配置非法、重复绑定、池满或实例号耗尽返回NULL。
* 说明:约束:必须早于消费Slave通知创建;点表有序、唯一且实例存活期间不可变,
* 说明:约束:必须早于消费Slave通知创建;点表按IOA唯一且有序、实例存活期间不可变,
* 当前允许45..51及58..64;51/64只允许直接执行,时区为-1439..1439。
*/
IEC104_Command_t *IEC104_Command_Create(IEC104_Slave_t *slave, const IEC104_CommandConfig_t *config)
@@ -55,11 +55,15 @@ typedef enum {
IEC104_COMMAND_MODE_SELECT_EXECUTE /* 须先选择,执行必须匹配仍有效的本事务选择。 */
} IEC104_CommandMode_t;
/* 同一CA/IOA仅一项;按common_address、ioa严格升序排列,Create校验、受理时二分查找。
/* 同一IOA仅一项;按ioa严格升序排列,Create校验、受理时二分查找。
* 表由应用拥有,从Create到Destroy须保持有效且只读;可放在静态const存储中。
* 库不复制点表、不持有设备指针。动态闭锁、权限和数值限值仍由应用在Accept前判断。 */
* 库不复制点表、不持有设备指针。动态闭锁、权限和数值限值仍由应用在Accept前判断。
*
* 公共地址不逐点存储:实例的站点CA即表的CA(自Create起恒定)。
* 因此一张点位相同的表可被不同CA的实例共用,不必按CA复制。
* [vendor 兼容补丁] ARM Compiler 5 下 uint16_t 为2字节对齐,
* 去掉 common_address 后本结构由12字节降到8字节。 */
typedef struct {
uint16_t common_address; /* 1..65534;允许先配置,受理时该CA仍必须已注册。 */
uint32_t ioa; /* 1..0xFFFFFF,控制点地址。 */
uint16_t type_mask; /* 非零,限14种已知类型;实际可用类型与全局type_mask取交集。 */
IEC104_CommandMode_t mode; /* 此点所有允许类型共用同一种执行方式。 */
@@ -92,7 +96,7 @@ typedef struct {
uint32_t time_tag_max_delay_ms; /* 默认10000;0只关闭时差检查,仍拒绝IV=1/UTC换算失败。 */
int16_t master_utc_offset_minutes; /* 主站标准时区,默认480,范围-1439..1439;SU另由公共层处理。 */
bool negative_on_expired; /* TEST=0默认true否定COT49,受理/动作前均适用;false静默。TEST始终否定。 */
const IEC104_CommandPoint_t *points; /* 按CA/IOA排序的只读点表,默认NULL;模式由各点决定。 */
const IEC104_CommandPoint_t *points; /* 按IOA排序的只读点表,默认NULL;模式由各点决定。 */
size_t point_count; /* 点数,默认0;须与points同时为空或同时非空。 */
} IEC104_CommandConfig_t;
@@ -215,7 +215,7 @@ void IEC104_Spontaneous_Clear_Time_Base(IEC104_Spontaneous_t *self);
* 原后缀,保留值、品质和采集时刻;双报的未完成块可能已交接PLAIN,重试允许该副本重复。
* Type207冻结事件使用TIME_UTC或TIME_MONOTONIC;后者按本次UTC基准换算,AT_PUBLISH重新取时。
* 有效句柄的count=0直接成功,允许changes=NULL且不要求已Init;QUEUE/SUBMIT应恢复服务。
* 显式PLAIN或双报仍用通用缓存,不享有独立256条SOE保留额度。成功表示接手,仍可被新变化覆盖。
* 显式PLAIN或双报仍用通用缓存,不享有独立128条SOE保留额度。成功表示接手,仍可被新变化覆盖。
*/
IEC104_SpontaneousPublishResult_t IEC104_Spontaneous_Publish(
IEC104_Spontaneous_t *self, const IEC104_SpontaneousChange_t *changes,
+60 -5
View File
@@ -11,8 +11,8 @@ recorded in the directory manifests before local compatibility edits.
| Check | Status |
| --- | --- |
| Original byte provenance | `UPSTREAM.sha256`; independent final 84-file check passed, six controlled source differences |
| C99 assertions | 43 original invocations unchanged; independent AC5 true passes / false fails, invalid capacities including SOE128 rejected |
| Original byte provenance | `UPSTREAM.sha256`; independent final 84-file check passed, seventeen locally differing files: twelve reviewed patches and five trailing-blank-line-only. The command-table patch spans two files in one directory (`IEC104_Command.h` and `.c`) and both are counted. The SOE-128 change touches `IEC104_TcpServer_Config.h` and `IEC104_AsduQueue.h` (capacity) plus `IEC104_TcpServer.c` (assert message text), with three comment-only corrections in `IEC104_Slave.h`, `IEC104_TcpServer.h`, `IEC104_Spontaneous.h` (stale "256" SOE wording fixed to 128, quantifiers only, semantics untouched; Lead decision 2026-11-18). |
| C99 assertions | 43 original invocations remain; every assertion condition unchanged (one message text tracks the SOE capacity macro); independent AC5 true passes / false fails, invalid capacities including SOE128 rejected |
| Final compiler diagnostics | Independent AC5 default/compact: 62 translation-unit compilations, RC0 and zero diagnostics; simulator and runtime tests tracked separately |
| Static memory / runtime integration | Separate build-verifier and integration task evidence; no board runtime claim |
| License | No license grant confirmed; redistribution not authorized by this import |
@@ -30,7 +30,10 @@ Local edits use the Edit tool only.
standalone Net header. In pre-C11 C, each existing assertion expands to a
line-unique typedef with array bound `(condition) ? 1 : -1`; false assertions
are compile errors. C11 targets retain native assertions. All 43 assertion
invocations and their conditions/messages remain unchanged, including SOE=256.
invocations remain, and every assertion CONDITION is unchanged. The only
message text that differs is the SOE guard's event count, which tracks the
capacity macro this product sets to 128 (see below); on C99 the message is
discarded by the shim, so no assertion semantics move with it.
- `104/IEC104_TcpServer.c`: two explicit `IEC104_TcpEventType_t` casts when
expanding the byte-sized ring header into event validation helpers; no
condition, capacity, storage layout or runtime behavior is changed.
@@ -38,16 +41,68 @@ Local edits use the Edit tool only.
convert nonnegative enum range checks to unsigned upper-bound comparisons.
Invalid negative values still convert above the valid range and are rejected;
AC5 no longer diagnoses pointless unsigned comparisons against zero.
- `104/business/IEC104_Command.h`: mask each TYPE_BIT shift count with 15.
- `104/business/IEC104_Command.h` + `104/business/IEC104_Command.c`: mask each
TYPE_BIT shift count with 15, and remove the per-point `common_address`.
Selected valid branches already use shifts 0..13; range guards still return
zero for unsupported types. This avoids MSVC diagnostics about unsigned
underflow in unselected conditional branches, not a proven executed runtime UB.
The point table now sorts by IOA only; the instance binds its station CA on the
first accepted request and `Find_Point` rejects any other CA before searching,
so a CA mismatch still takes the unchanged unknown-IOA path. Point entry size
drops from 12 to 8 bytes on ARM Compiler 5, letting the application hold the
table in const Flash instead of static RAM. No capacity, SOE, ordering or
acceptance semantics change; see `ci-artifacts/iec104-mcu-skeleton/core-trim/`
for the element-wise and lookup equivalence probes.
- Added `common/IEC60870_FaultEvent.c` as an exact-copy link dependency of
`IEC104_Spontaneous_Publish_Fault_Event`, with Lead authorization.
- `UPSTREAM.sha256`: 84 original byte hashes (29 implementations, 54 directory
headers and the declarative profile header). Paths are relative to this
directory and to `IEC60870-Slave/`; hashes describe upstream bytes, not the
six locally patched files. The compatibility header is new and not upstream.
twelve locally differing files. The compatibility header is new and not upstream.
- Trailing blank lines: five files (`common/IEC60870_Codec.c`,
`common/IEC60870_InformationObjects.c`, `common/IEC60870_InformationObjects.h`,
`common/IEC60870_InformationObjects_Internal.h`,
`examples/iec104_dtu_profile.h`) differ from upstream by exactly one
end-of-file blank line, removed by the repository's `end-of-file-fixer`
pre-commit hook (it does not exempt this tree). Content normalized for that
single line is byte-identical to upstream, and `verify_vendor.py` asserts as
much: its `BLANK` class may only list such files, and a semantic edit carried
into one of them fails the check instead of hiding behind the class.
- SOE capacity: this product sets `IEC104_TCP_SOE_CAPACITY` and
`IEC104_ASDU_SOE_MAX_CAPACITY` to `128U` (upstream `256U`) to fit the RAM
budget, and the guard message text follows the new count. IEC 60870-5-101/104
does not specify SOE depth; the only numeric lower bound in play is the
Swedish Svk TR02-04-04 §7.4.1 guidance of 100 entries, so 128 stays compliant.
The assertion CONDITION (`SOE_CAPACITY == ASDU_SOE_MAX_CAPACITY`) is untouched,
so the invariant it guards still holds.
## Capacity override surface (skeleton profile)
Eleven per-module capacity macros are already wrapped in `#ifndef`, so a build may
override them with `-D` **without editing any source file**. This is what keeps the
MCU skeleton profile small while the simulator keeps every business module enabled.
`verify_vendor.py` compiles both the default and compact configurations and would
catch an undefined-macro regression, but note that no `-D` is baked into any source.
| Macro | Default | File |
| --- | --- | --- |
| `IEC104_INTERROGATION_TASK_CAPACITY` | `4U` | `104/business/IEC104_Interrogation.c` |
| `IEC104_INTERROGATION_EVENT_CAPACITY` | `4U` | `104/business/IEC104_Interrogation.c` |
| `IEC104_SYSTEM_COMMAND_TASK_CAPACITY` | `4U` | `104/business/IEC104_SystemCommand.c` |
| `IEC104_SYSTEM_COMMAND_RESET_CAPACITY` | `4U` | `104/business/IEC104_SystemCommand.c` |
| `IEC104_PERIODIC_SCAN_GROUP_CAPACITY` | `4U` | `104/business/IEC104_PeriodicScan.c` |
| `IEC104_PARAMETER_TASK_CAPACITY` | `4U` | `104/business/IEC104_Parameter.c` |
| `IEC104_REMOTE_PARAMETER_TASK_CAPACITY` | `2U` | `104/business/IEC104_RemoteParameter.c` |
| `IEC104_CLOCK_SYNCHRONIZATION_TASK_CAPACITY` | `4U` | `104/business/IEC104_ClockSynchronization.c` |
| `IEC104_READ_TASK_CAPACITY` | `4U` | `104/business/IEC104_Read.c` |
| `IEC104_COMMAND_REPLY_CAPACITY` | `4U` | `104/business/IEC104_Command.c` |
| `IEC104_SPONTANEOUS_CAPACITY` | `IEC104_TCP_SERVER_CAPACITY` | `104/business/IEC104_Spontaneous.c` |
`IEC104_SPONTANEOUS_CAPACITY` is overrideable but yields no RAM saving (its pool
element is callbacks/state, not a capacity-sized array), so the skeleton profile
leaves it at its default. The 92% gate is unaffected: these macros size single
instance work queues inside a business module, not the TCP server pool, SOE=128,
k/w windows or concurrency semantics.
- Independent hash/diff and AC5 true/false assertion verification is performed
by build-verifier; product integration and simulator build have separate owners.