feat(iec104): 在模拟器跑通共享协议核心与标准DTU点表

引入成熟 IEC 60870-5-104 子站的只读子集(App/protocol/iec60870,28 个源文件
按原字节复制,UPSTREAM.sha256 记录来源,兼容补丁保留全部断言与容量),在其上
装配单属主共享核心(Start/Poll/Stop/Get_Status),并把标准 DTU 演示点表全量
接入为唯一真源。

- iec104_core:threadless 推进,poll 零等待,通知/交互/COV 扫描全部有界;
  生命周期区分失败启动待重试、重复启动拒绝、断线保固化 RAM。
- iec104_dtu_table / iec104_source:标准 DTU 全点表注册,无数据源的点读 0,
  遥控只做模拟状态可读回,不触碰实际出口。
- iec104_sim:仅窗口与独立服务模式起真实 TCP,无头黄金模式保持确定性假统计。
- test/host/iec104_dtu:独立真实 TCP 11 项、传输层 10 项、生命周期与借用负例。

验证:lvgl_sim 真实 TCP 11/11、传输 10/10,UI 像素回归未回退;AC5 单 TU
编译 0 错误 0 诊断。板端尚未切换:完整库闭包 RAM 超 92% 门禁净增预算 25730B,
结论为停止转实施,记录在 IEC104_MCU最小可行性验证.md。旧实现与 board 服务
保持原样,未提交 MDK 工程改动,未烧板。
This commit is contained in:
2026-11-17 20:48:00 +08:00
parent f84855a559
commit 2e9272cc8a
117 changed files with 54273 additions and 10 deletions
+67
View File
@@ -0,0 +1,67 @@
# Independent IEC104 DTU acceptance matrix
## Oracle and scope
Read-only reference: `IEC60870-Slave/examples/iec104_dtu_profile.h`, `iec104_example_points.h`, and `iec104_slave_example.c`. Tests do not import the implementation model or compare two model views. Profile numeric counts are checked against independently parsed rows; runtime ranges below are traced to the original runtime, not guessed from the replacement.
- SP: profile 59 IOAs, 0x0001..0x0041 excluding 0x002c..0x0031.
- DP: runtime 16 IOAs 0x1001..0x1010 (`EXAMPLE_DOUBLE_COUNT`, GI/Read).
- Measurements: profile 30 IOAs 0x4001..0x401e, GI Type13.
- Energy: profile four metadata rows; runtime `EXAMPLE_COUNTER_COUNT=8` exposes 0x6401..0x6408 (not four). Counter interrogation is independent of GI.
- Step and bitstring: runtime eight each at 0x1201 and 0x1301; Read only, not GI.
- Commands: profile 19 plus runtime step eight, setpoint30 and bitstring eight. Some extension command forms are disabled by original default policy; visibility is not permission.
- Standard parameters: runtime 120, four groups of30 at 0x8601+group*0x20. Group0 first4 Type111, remainder Type110; groups1/2 Type111; group3 Type112. GI includes these.
- Remote parameters: profile430 metadata entries,345 configured readable in SN0;85 unconfigured remain metadata-only per Lead clarification. SN1..4 action parameters only (original model reports88). Never infer a fixed296 total.
- Example diagnostic aliases (scope pending, not silently equated to standard product points):41 measurement source aliases 0x9601..0x962c excluding0x961c..0x961e;93 digital aliases0x9141..0x919d;16 pulse aliases0xb100..0xb10f. Source: original `Example_Read_Point` lines3063..3081. Retained in oracle inventory but excluded from first standard acceptance run pending Lead scope decision.
- Original demo nonzero initial observations are NOT acceptance values: source-less visible observations must start at zero. Configured parameters retain typed configured values. Do not erase quality flags or real protection lockout.
## Tests and independent observations
Measured status: `ci-artifacts/iec104-tests/tcp-final/report.json` records 11/11 PASS on the frozen `lvgl_sim.exe` SHA256 `d2285bbc68ce4353c814dbcc455a2083b56e81fc54bf88081402e821c6a92833`. Exact GI225 includes parameter value/QPM checks; CI8 uses original default Type206; Read121 checks zero/IV or SB plus CP56 legality; remote345 compares independent original typed defaults. The run includes area changes, preset/read unchanged/commit/read changed/cancel, disconnected stage invalidation, committed RAM retained across TCP reconnect, UInt16777217 precision, unknown/unconfigured addresses, malformed typed values, APDU255, split/coalesced streams, causal SBO state/feedback and rejected-command invariance, full device time and progression, unchanged host clock, and 30-second monotonic SBO expiry. Normal pre-START TESTFR and illegal pre-START I-frame close are distinct assertions. Source/transport/core probes record 10/10 PASS in `transport-final/report.json`; these are separately linked migrated production sources, not old-library binaries.
Remaining unverified: malformed GI CA/IOA/QOI combinations; inbound APDU overlimit close; wrong TLV declared length on product TCP; every one of the 65 command entries and default-disabled extension permissions; nonzero measurement deadband reference stability; Spontaneous negative time_kind; injected Close/Destroy/GetStatus cleanup failures (no supported fixture injection); atomic platform apply rejection after validation; mapped-UShort callback overflow/fraction (no reachable mapped UShort entry). TCP tests terminate the process, so normal finite-deadline shutdown and UI are separately owned simulator evidence. The 11-case TCP suite is not an assertion that all commands/features were exhaustively tested. Diagnostic aliases150 are deliberately excluded by Lead scope decision and must return unknown rather than fabricated zero success.
Initialization contract frozen by Lead after data/core review: observations are zero with IV; step/bit simulation is zero with SB. Parameters are configuration, not sampled observations: preserve original legal typed defaults and read-only mappings. This is the chosen integration contract, not a quotation of the original user wording. Committed values survive ordinary TCP reconnect in RAM; reconnect discards selections/stages; explicit service initialization restores defaults; no persistence is claimed.
The table below is the overall design matrix; only the measured status above is a coverage claim. Items listed as remaining unverified are not passes.
| Behavior | Positive evidence | Negative/boundary evidence |
|---|---|---|
| STARTDT, STOPDT, TESTFR | Exact U confirmations | Business before START rejected/no publication |
| GI | ACTCON, complete exact SP/DP/YC/standard-parameter set, no duplicates, ACTTERM | Bad CA/IOA/QOI; PN asserted separately |
| Read | Each runtime readable IOA independently requested and decoded | Unknown address COT47 PN1 |
| Parameters200/201 | Switch area then read current/min/max | Invalid area rejected, current unchanged |
| Parameters202 | Exact configured IOA/tag/value set vs source profile; count/length/SN/PI | Unknown and unconfigured address, incorrect tag/length, NaN |
| Parameters203 | Read baseline -> preset -> read unchanged -> solidify -> read changed -> restore | Execute without preset; preset/cancel/read unchanged; disconnected stage cannot execute |
| SBO | Select -> execute -> ACTCON/ACTTERM -> state feedback/readback | Execute first, wrong state, different IOA, repeated execute, timeout, reconnect |
| Simulation safety | Feedback and action log; source audit confirms no physical output call | No claim of physical safety from socket-send success alone |
| Clock | Deliberate device-clock offset, response + subsequent event CP56/clock diagnostic | Host wall-minus-monotonic unchanged; SBO timeout still monotonic |
| TCP stream | Split start/header/body, concatenated frames, reconnect sequence reset | Complete255-byte APDU/length253/ASDU249; reject overlimit; service-side short write via separately linked Net stub |
| UI | Same executable hash used for TCP and independent golden tests | No golden updates to hide regression |
## Evidence rules
Every request/response frame is retained as hex with monotonic timestamps. Each decoded object is written with IOA/type/value. Report failures, timeouts and missing observability as failures or explicit unverified items, never passes. Record executable hash and reference hashes. Existing `test/host/iec104`, `iec104_serv`, and master RX tests remain untouched. After code settles, run corresponding unit-test hooks or `scripts/ci_local.py`; Keil and simulator builds are coordinated with the build owner, not launched concurrently.
## Reproduction and toolchain
Run from the repository root:
```text
python -B test/host/iec104_dtu/run_unit_test.py
python -B test/host/iec104_dtu/run_tcp_test.py --exe simulator/build/Debug/lvgl_sim.exe --output ci-artifacts/iec104-tests/tcp-rerun
python -B test/host/iec104_dtu/run_transport_test.py --output ci-artifacts/iec104-tests/transport-rerun
python -B test/host/iec104_dtu/run_transport_test.py --only core_lifecycle --output ci-artifacts/iec104-tests/lifecycle-rerun
python -B test/host/iec104_dtu/run_transport_test.py --only source_binding --output ci-artifacts/iec104-tests/source-rerun
python -B test/host/iec104_dtu/run_transport_test.py --only vendor_boundary --output ci-artifacts/iec104-tests/boundary-rerun
```
TCP requires Python standard library and the final same-UI simulator binary; use a distinct `--port` when tests run concurrently. Transport/probes require MSVC x64 and Windows SDK, discovered by existing `test/host/common/unit_test_harness.py::locate_toolchain` (standard VS installation directories, no `vcvars` or `vswhere` invocation). Explicit `DTU_MSVC_CL` and `DTU_WINDOWS_KITS_ROOT` overrides obey that shared harness contract. Compiler path, command, source SHA256 and per-fixture build/run logs are retained. `/W3 /WX /std:c11 /utf-8` are used without warning suppression. Idle upstream fixture has a checked size_t-to-uint32 conversion in its generated artifact copy; report preserves upstream and adapted hashes. Original library and old host tests are not edited. Preserved legacy runners were actually executed: IEC104179/179 and IEC104 SERV37/37 PASS, combined command exit0; summary `ci-artifacts/iec104-tests/legacy-regression.txt` records existing compiler warnings, so this is not a zero-warning legacy-build claim.
The self-test runner only tests the independent decoder/oracle (7 tests), not product acceptance. Existing total host discovery sees `run_unit_test.py`; real TCP and MSVC transport runners are explicit acceptance entrypoints, NOT yet registered as automatic CI coverage gates. New firmware registration/Keil build and existing host coverage gates are separately owned; this file does not claim CI/coverage/dual-target registration passed. No old tests were deleted to hide regressions.
Oracle corrections retained with failures: QPM is `{1,3,4,2}` from original enum, not group+1; original CI defaults to Type206, not15; pre-START I-frame is FATAL close per original Link; wrong-IOA EXEC does not clear a prior selection, so reselecting without cancellation invokes duplicate-select rejection. The genuine product Read Type1/3 compatibility defect was separately repaired to Type30/31 CP56 before the final binary. Earlier failing snapshots remain in artifacts.
## Requested simulator CLI
Current user-authorized basic contract: `lvgl_sim.exe --iec104 --iec104-headless --iec104-port N --iec104-duration S`; bind `0.0.0.0` to listen on all local IPv4 interfaces, READY after bind, finite lifetime. The test client still defaults to `127.0.0.1` for local acceptance. The historical `d2285bbc...c6a92833` 11-case results above were measured over loopback before this bind-address change; they do not establish external-IP or LAN reachability of the new binary. New wildcard-listen and address/handshake evidence will be recorded separately after simulator/reviewer verification; a connection from the same host to its LAN IPv4 is not proof of access from another LAN device. Lead rejected a production send-chunk switch: server-side short-write/WOULD_BLOCK must use a separately linked Net stub, while real TCP split covers inbound reconstruction. Device time is independently readable with Type103/COT5. Action counters/source audit are required for the no-physical-DO claim. No hardware, macro changes, commit or push.
@@ -0,0 +1,87 @@
/* Shared core API lifecycle against the real transport and controllable Net. */
#include "iec104_tcp_fixture.h"
#include "iec104_core.h"
#include "iec104_source.h"
static iec104_core_config_t config = {"127.0.0.1", 24040U, 1U};
static iec104_core_status_t status;
static void inspect(unsigned running, unsigned owned)
{
iec104_core_get_status(&status);
CHECK(status.running == running && status.owns_resources == owned);
}
static void turn(void) { CHECK(iec104_core_poll() == 0); }
static unsigned apply_count;
static bool parameter_read(void *ctx, uint16_t id, float *out)
{
(void)ctx;
if (id == 17U) { return false; }
*out = id == 20U ? 100.0f : 1.0f;
return true;
}
static bool parameter_apply(void *ctx, const iec104_source_change_t *v, size_t n, bool validate)
{
(void)ctx; (void)v; (void)n; (void)validate; ++apply_count; return true;
}
static void exchange(const uint8_t *frame, size_t length, uint8_t expected_cot)
{
size_t start = fake.sent_bytes, at;
unsigned i; bool found = false;
push(frame, length);
for (i = 0U; i < 20U; ++i) { turn(); }
for (at = start; at + 6U <= fake.sent_bytes; at += (size_t)fake.output[at + 1U] + 2U) {
CHECK(fake.output[at] == 0x68U);
if (at + 12U <= fake.sent_bytes && !(fake.output[at + 2U] & 1U) && fake.output[at + 6U] == 203U) {
CHECK(fake.output[at + 8U] == expected_cot); found = true;
}
}
CHECK(found);
}
static void borrowed_failure(void)
{
iec104_source_bindings_t bind = {0};
iec104_source_value_t before, after;
static const uint8_t preset[] = {0x68,19,0,0,0,0,203,1,6,17,1,0,0,0,128,0x20,0x88,0,1,1,0};
static const uint8_t solidify[] = {0x68,13,2,0,0,0,203,0,6,17,1,0,0,0,0};
bind.parameter_read = parameter_read; bind.parameter_apply = parameter_apply;
iec104_source_bind(&bind);
CHECK(iec104_source_remote_read(0U, 0x8820U, &before));
exchange(preset, sizeof(preset), 7U);
exchange(solidify, sizeof(solidify), 0x47U);
CHECK(iec104_source_remote_read(0U, 0x8820U, &after));
CHECK(before.bytes[0] == after.bytes[0] && apply_count == 0U);
}
int main(void)
{
unsigned i;
CHECK(iec104_core_poll() == -1);
CHECK(iec104_core_stop() == 0); inspect(0U, 0U);
fake.init_error = 1U;
CHECK(iec104_core_start(&config) == -1); inspect(0U, 0U);
fake.init_error = 0U; fake.listen_error = 1U;
CHECK(iec104_core_start(&config) == -1); inspect(0U, 0U);
fake.listen_error = 0U; fake.now = 1000U;
CHECK(iec104_core_start(&config) == 0); inspect(1U, 1U);
CHECK(iec104_core_start(&config) == -1); inspect(1U, 1U);
fake.accept_pending = 1U; turn();
push(start_act, sizeof(start_act));
for (i = 0U; i < 8U; ++i) { turn(); }
iec104_core_get_status(&status);
CHECK(status.connected && status.active && status.connections == 1U);
fake.recv_eof = 1U;
for (i = 0U; i < 8U; ++i) { turn(); }
iec104_core_get_status(&status); CHECK(!status.connected);
fake.recv_eof = 0U; fake.accept_pending = 1U; turn();
push(start_act, sizeof(start_act));
for (i = 0U; i < 8U; ++i) { turn(); }
iec104_core_get_status(&status); CHECK(status.connections == 2U && status.active);
borrowed_failure();
CHECK(iec104_core_stop() == 0); inspect(0U, 0U);
CHECK(status.connections == 2U);
CHECK(iec104_core_stop() == 0); inspect(0U, 0U);
CHECK(iec104_core_poll() == -1);
CHECK(iec104_core_start(&config) == 0); inspect(1U, 1U);
CHECK(status.connections == 0U);
CHECK(iec104_core_stop() == 0); inspect(0U, 0U);
puts("core init/listen failure recovery,double-start,active-EOF,restart,stop-idempotence: PASS");
return 0;
}
+40
View File
@@ -0,0 +1,40 @@
"""Independent reference extraction, never imports the production DTU model."""
import hashlib
import re
from pathlib import Path
ROOT = Path(__file__).resolve().parents[3]
EXAMPLES = ROOT / "IEC60870-Slave" / "examples"
def reference():
text = (EXAMPLES / "iec104_dtu_profile.h").read_text(encoding="utf-8-sig")
tables = {}
for name in ("signals", "measurements", "energy", "commands", "parameters"):
body = re.search(r"dtu_" + name + r"\[\]\s*=\s*\{(.*?)\n\};", text, re.S).group(1)
tables[name] = [int(value, 16) for value in re.findall(r"\{0x([0-9A-Fa-f]+)U,", body)]
parameters = {}
for ioa, tag, value, configured in re.findall(
r'\{0x([0-9A-Fa-f]+)U, (\d+)U, (NULL|"[^"]*"), (true|false),', text):
if configured == "true":
parameters[int(ioa, 16)] = (int(tag), value.strip('"'))
assert [len(tables[x]) for x in tables] == [59, 30, 4, 19, 430]
assert len(parameters) == 345
assert set(tables["signals"]) == set(range(1, 0x42)) - set(range(0x2C, 0x32))
assert set(tables["measurements"]) == set(range(0x4001, 0x401F))
gi = {ioa: 1 for ioa in tables["signals"]}
gi.update({ioa: 3 for ioa in range(0x1001, 0x1011)})
gi.update({ioa: 13 for ioa in tables["measurements"]})
for group in range(4):
for index in range(30):
gi[0x8601 + group * 0x20 + index] = 112 if group == 3 else (110 if group == 0 and index >= 4 else 111)
read = {ioa: 30 if typ == 1 else 31 if typ == 3 else typ for ioa, typ in gi.items() if typ < 100}
read.update({ioa: 5 for ioa in range(0x1201, 0x1209)})
read.update({ioa: 7 for ioa in range(0x1301, 0x1309)})
diagnostics = {ioa: 13 for ioa in range(0x9601, 0x962D) if ioa not in range(0x961C, 0x961F)}
diagnostics.update({ioa: 30 for ioa in range(0x9141, 0x919E)})
diagnostics.update({ioa: 30 for ioa in range(0xB100, 0xB110)})
return {"profile": tables, "remote": parameters, "gi": gi, "read": read, "diagnostic_aliases_pending_scope": diagnostics,
"counters": list(range(0x6401, 0x6409)),
"hashes": {name: hashlib.sha256((EXAMPLES / name).read_bytes()).hexdigest()
for name in ("iec104_dtu_profile.h", "iec104_example_points.h", "iec104_slave_example.c")}}
+364
View File
@@ -0,0 +1,364 @@
"""Real-loopback acceptance against the same lvgl_sim binary as UI tests."""
import argparse
import datetime
import hashlib
import json
import math
from pathlib import Path
import socket
import struct
import subprocess
import tempfile
import time
import traceback
from oracle import ROOT, reference
from wire import Client, asdu, cp56, header, objects, remote_objects, tlv, u24
class Acceptance:
def __init__(self, args):
self.args = args
self.oracle = reference()
self.trace, self.decoded, self.results = [], [], []
self.client = None
def connect(self):
if self.client:
self.client.close()
time.sleep(0.10) # Give the single-owner service one close/poll cycle.
self.client = Client(self.args.host, self.args.port, self.trace)
self.client.start(split=True)
return self.client
def run(self, name, function):
started = time.monotonic()
try:
self.connect()
function()
result = {"name": name, "passed": True}
except Exception as exc:
result = {"name": name, "passed": False, "error": str(exc), "traceback": traceback.format_exc()}
result["seconds"] = time.monotonic() - started
self.results.append(result)
print(("PASS " if result["passed"] else "FAIL ") + name + (": " + result["error"] if not result["passed"] else ""), flush=True)
if self.client:
self.client.close()
self.client = None
def read(self, ioa):
self.client.request(asdu(102, u24(ioa), cot=5))
data, _ = self.client.receive(lambda d: header(d)["cot"] == 5 or header(d)["pn"])
assert not header(data)["pn"], data.hex()
assert header(data)["ca"] == 1 and header(data)["oa"] == 17, data.hex()
rows = objects(data)
assert len(rows) == 1 and rows[0]["ioa"] == ioa
self.decoded.extend(rows)
return rows[0]
def remote_read(self, ioas=(), area=0):
self.client.request(asdu(202, struct.pack("<H", area) + b"".join(map(u24, ioas)), count=len(ioas)))
rows = []
while True:
data = self.client.response(202, 7)
assert int.from_bytes(data[6:8], "little") == area
assert data[8] in (0, 1), ("reserved PI flags", data.hex())
rows.extend(remote_objects(data))
if not data[8] & 1:
break
self.decoded.extend(rows)
assert len({row["ioa"] for row in rows}) == len(rows)
if ioas:
assert set(ioas) == {row["ioa"] for row in rows}
return {row["ioa"]: row for row in rows}
def preset(self, values, area=0, pn=False):
self.client.request(asdu(203, struct.pack("<H", area) + b"\x80" + b"".join(tlv(*v) for v in values), count=len(values)))
return self.client.response(203, 7, pn)
def finish(self, cancel=False, area=0, pn=False):
self.client.request(asdu(203, struct.pack("<H", area) + b"\x00", count=0, cot=8 if cancel else 6))
return self.client.response(203, 9 if cancel else 7, pn)
def test_link_start_stop_testfr_and_prestart(self):
self.client.close()
time.sleep(0.10)
self.client = Client(self.args.host, self.args.port, self.trace)
self.client.send(bytes.fromhex("680443000000"))
while self.client.frame()[2] != 0x83:
pass
self.client.request(asdu(100, u24(0) + bytes((20,))))
# Mature Link.c rejects pre-START I frames as FATAL and closes.
try:
while True:
frame = self.client.frame()
assert frame[2] & 1, ("business published before START", frame.hex())
except EOFError:
pass
self.connect()
self.client.send(bytes.fromhex("680443000000"))
while self.client.frame()[2] != 0x83:
pass
self.client.send(bytes.fromhex("680413000000"))
while self.client.frame()[2] != 0x23:
pass
self.client.send(bytes.fromhex("680443000000"))
while True:
frame = self.client.frame()
assert frame[2] & 1, ("I-frame while STOPPED", frame.hex())
if frame[2] == 0x83:
break
self.client.start()
self.client.request(asdu(201, u24(0)))
self.client.response(201, 7)
def test_gi_exact_set_and_zero_observations(self):
self.client.request(asdu(100, u24(0) + b"\x14"), split=True)
self.client.response(100, 7)
rows = []
while True:
data, _ = self.client.receive(lambda d: header(d)["cot"] in (20, 10))
if header(data)["type"] == 100:
assert header(data)["cot"] == 10 and not header(data)["pn"]
break
assert header(data)["cot"] == 20 and not header(data)["pn"]
rows.extend(objects(data))
self.decoded.extend(rows)
actual = {row["ioa"]: row["type"] for row in rows}
assert len(actual) == len(rows), "duplicate GI objects"
expected = self.oracle["gi"]
assert actual == expected, {"missing": sorted(expected.keys() - actual.keys()), "extra": sorted(actual.keys() - expected.keys()), "wrong_types": [ioa for ioa in actual.keys() & expected.keys() if actual[ioa] != expected[ioa]]}
assert all(row["value"] == 0 and row["quality"] == 128 for row in rows if row["type"] < 100), "source-less observation not zero+IV"
for row in rows:
if row["type"] >= 100:
group, index = divmod(row["ioa"] - 0x8601, 0x20)
expected_value = (200 if index < 4 else 655) if group == 0 else (-10000 if group == 1 else 10000 if group == 2 else 0)
assert row["value"] == expected_value and row["qpm"] == (1, 3, 4, 2)[group], row
def test_counter_exact_set_zero_and_invalid(self):
self.client.request(asdu(101, u24(0) + bytes((5,))))
self.client.response(101, 7)
rows = []
while True:
data, _ = self.client.receive(lambda d: header(d)["type"] in (15, 37, 206, 207, 101))
h = header(data)
assert h["ca"] == 1 and h["oa"] == 17 and not h["pn"]
if h["type"] == 101:
assert h["cot"] == 10
break
assert h["cot"] == 37
rows.extend(objects(data))
self.decoded.extend(rows)
assert len(rows) == len(self.oracle["counters"])
assert {r["ioa"] for r in rows} == set(self.oracle["counters"])
assert all(r["value"] == 0 and r["quality"] & 128 for r in rows)
def test_read_all_runtime_addresses(self):
for ioa, typ in sorted(self.oracle["read"].items()):
row = self.read(ioa)
assert row["type"] == typ, row
assert row["value"] == 0, row
assert row["quality"] == (32 if typ in (5, 7) else 128), row
def test_remote_complete_typed_snapshot(self):
actual = self.remote_read()
expected = self.oracle["remote"]
assert set(actual) == set(expected), {"missing": sorted(expected.keys() - actual.keys()), "extra": sorted(actual.keys() - expected.keys())}
for ioa, (tag, text) in expected.items():
row = actual[ioa]
assert row["tag"] == tag, row
value = text if tag == 4 else float(text) if tag == 38 else int(text)
assert math.isclose(row["value"], value, rel_tol=1e-6, abs_tol=1e-7) if tag == 38 else row["value"] == value, row
def test_area_switch_and_readback(self):
for area in (1, 4, 0):
self.client.request(asdu(200, u24(0) + struct.pack("<H", area)))
self.client.response(200, 7)
self.client.request(asdu(201, u24(0)))
reply = self.client.response(201, 7)
assert len(reply) == 15 and reply[6:9] == u24(0)
assert struct.unpack("<HHH", reply[9:]) == (area, 0, 4)
self.client.request(asdu(200, u24(0) + struct.pack("<H", 5)))
self.client.response(200, 7, True)
self.client.request(asdu(201, u24(0)))
assert self.client.response(201, 7)[9:11] == b"\x00\x00"
def test_typed_preset_commit_cancel_readback(self):
addresses = (0x8820, 0x8823, 0x882D, 0x802E)
before = self.remote_read(addresses)
change = [(0x8820, 1, 0), (0x8823, 38, 8.25), (0x882D, 45, 2), (0x802E, 35, 16777217)]
original = [(ioa, before[ioa]["tag"], before[ioa]["value"]) for ioa in addresses]
try:
self.preset(change)
assert self.remote_read(addresses) == before, "preset leaked into active values"
self.finish()
after = self.remote_read(addresses)
assert all(after[ioa]["tag"] == tag and after[ioa]["value"] == value for ioa, tag, value in change)
self.connect()
assert self.remote_read(addresses) == after, "TCP reconnect lost committed RAM values"
self.preset(original)
self.connect()
self.finish(pn=True)
assert self.remote_read(addresses) == after, "disconnected stage became active"
self.preset(original)
self.finish(cancel=True)
assert self.remote_read(addresses) == after, "cancel published stage"
self.finish(pn=True)
finally:
self.preset(original)
self.finish()
assert self.remote_read(addresses) == before
def test_unknown_and_bad_typed_requests(self):
self.client.request(asdu(102, u24(0xFFFFFF), cot=5))
self.client.response(102, 47, True)
for ioa in (0xFFFFFF, 0x9601):
self.client.request(asdu(202, b"\x00\x00" + u24(ioa)))
self.client.response(202, 47, True)
for value in ([(0x8820, 1, 2)], [(0x8823, 45, 8)], [(0x8823, 38, float("nan"))]):
self.preset(value, pn=True)
self.finish(cancel=True)
def test_maximum_apdu_and_coalesced_frames(self):
# Structurally valid 249-byte ASDU, unknown IOA, String235. Business must respond.
request = asdu(203, b"\x00\x00\x80" + u24(0xFFFFFF) + bytes((4, 235)) + b"X" * 235)
frame = self.client.pack(request)
assert len(frame) == 255 and frame[1] == 253 and len(request) == 249
self.client.send(frame, split=True)
reply = self.client.response(203, 47, True)
assert reply
self.finish(cancel=True)
first = self.client.pack(asdu(201, u24(0)))
second = self.client.pack(asdu(201, u24(0)))
self.client.send(first + second)
self.client.response(201, 7)
self.client.response(201, 7)
def command(self, value, select=False, ioa=0x6002, pn=False):
self.client.request(asdu(46, u24(ioa) + bytes((value | (128 if select else 0),))))
return self.client.response(46, 7, pn)
def position(self):
return tuple(self.read(ioa)["value"] for ioa in (0x1001, 0x0011, 0x0028))
def execute_with_feedback(self, value):
before = self.position()
self.command(value, select=True)
assert self.position() == before, "selection acted"
start = len(self.client.asdus)
self.command(value)
self.client.receive(lambda d: header(d)["type"] == 46 and header(d)["cot"] == 10)
frames = self.client.asdus[start:]
rows = [r for d in frames if header(d)["type"] in (1, 3, 30, 31) for r in objects(d)]
self.decoded.extend(rows)
expected = (value, int(value == 2), int(value == 1))
assert before != expected, "test must request a real state transition"
assert self.position() == expected
for ioa, result in zip((0x1001, 0x0011, 0x0028), expected):
assert any(r["ioa"] == ioa and r["value"] == result and r["quality"] == 32 for r in rows), ("missing simulated feedback", ioa, rows)
def test_sbo_order_feedback_and_reconnect(self):
initial = self.position()
self.command(2, pn=True)
assert self.position() == initial
self.command(2, select=True)
self.command(1, pn=True)
assert self.position() == initial
self.command(2, select=True)
self.command(2, ioa=0x6004, pn=True)
assert self.position() == initial
self.connect() # Wrong-IOA EXEC does not cancel the original selection.
self.execute_with_feedback(1 if initial[0] == 2 else 2)
after = self.position()
self.command(after[0], pn=True)
assert self.position() == after
opposite = 1 if after[0] == 2 else 2
self.command(opposite, select=True)
self.connect()
self.command(opposite, pn=True)
assert self.position() == after, "old connection selection survived"
self.client.request(asdu(46, u24(0x6002) + bytes((opposite | 128,)), ca=2))
reply, _ = self.client.receive(lambda d: header(d)["type"] == 46)
assert header(reply)["cot"] == 46 and header(reply)["pn"]
self.command(opposite, pn=True)
assert self.position() == after, "wrong CA acted or selected"
self.execute_with_feedback(opposite)
def test_clock_is_device_only_and_sbo_is_monotonic(self):
host_delta = time.time() - time.monotonic()
self.command(2, select=True)
# Year2035 is deliberately far from the host clock; no OS clock API is used.
stamp = struct.pack("<HBBBBB", 12345, 22, 11, 17, 6, 35)
self.client.request(asdu(103, u24(0) + stamp))
self.client.response(103, 7)
self.client.request(asdu(103, u24(0) + bytes(7), cot=5))
reply = self.client.response(103, 5)
actual_time = cp56(reply[-7:])
assert not actual_time["invalid"] and not actual_time["summer"]
requested_time = datetime.datetime(2035, 6, 17, 11, 22, 12, 345000)
assert 0 <= (datetime.datetime.fromisoformat(actual_time["iso"]) - requested_time).total_seconds() < 2
time.sleep(0.15)
self.client.request(asdu(103, u24(0) + bytes(7), cot=5))
later = cp56(self.client.response(103, 5)[-7:])
assert 0.1 <= (datetime.datetime.fromisoformat(later["iso"]) - datetime.datetime.fromisoformat(actual_time["iso"])).total_seconds() < 2
assert abs((time.time() - time.monotonic()) - host_delta) < 1.0, "host wall clock changed"
# A far-future device clock must not prematurely expire the selection.
self.command(2)
self.client.receive(lambda d: header(d)["type"] == 46 and header(d)["cot"] == 10)
self.command(1, select=True)
time.sleep(self.args.sbo_timeout + 0.3)
self.command(1, pn=True)
assert self.read(0x1001)["value"] == 2, "expired command changed state"
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--exe", type=Path)
parser.add_argument("--host", default="127.0.0.1")
parser.add_argument("--port", type=int, default=24040)
parser.add_argument("--sbo-timeout", type=float, default=30)
parser.add_argument("--output", type=Path, default=ROOT / "ci-artifacts" / "iec104-tests")
parser.add_argument("--only", default="")
args = parser.parse_args()
args.output.mkdir(parents=True, exist_ok=True)
suite = Acceptance(args)
process, log = None, None
binary = {}
with tempfile.TemporaryDirectory(prefix="iec104-dtu-") as cwd:
try:
if args.exe:
exe = args.exe.resolve()
binary = {"path": str(exe), "sha256": hashlib.sha256(exe.read_bytes()).hexdigest()}
log = (args.output / "simulator.log").open("w", encoding="utf-8")
process = subprocess.Popen([str(exe), "--iec104", "--iec104-headless", "--iec104-port", str(args.port), "--iec104-duration", "180"], cwd=cwd, stdout=log, stderr=subprocess.STDOUT)
deadline = time.monotonic() + 15
while True:
if process.poll() is not None:
raise RuntimeError("simulator exited before listen: " + str(process.returncode))
try:
with socket.create_connection((args.host, args.port), 0.2):
break
except OSError:
if time.monotonic() >= deadline:
raise TimeoutError("simulator did not listen")
time.sleep(0.05)
names = ["test_link_start_stop_testfr_and_prestart", "test_gi_exact_set_and_zero_observations", "test_counter_exact_set_zero_and_invalid", "test_read_all_runtime_addresses", "test_remote_complete_typed_snapshot", "test_area_switch_and_readback", "test_typed_preset_commit_cancel_readback", "test_unknown_and_bad_typed_requests", "test_maximum_apdu_and_coalesced_frames", "test_sbo_order_feedback_and_reconnect", "test_clock_is_device_only_and_sbo_is_monotonic"]
for name in names:
if not args.only or args.only in name:
suite.run(name, getattr(suite, name))
finally:
if process and process.poll() is None:
process.terminate()
process.wait(timeout=10)
if log:
log.close()
report = {"created": datetime.datetime.now(datetime.timezone.utc).isoformat(), "binary": binary, "reference": suite.oracle, "results": suite.results,
"limitations": ["Service-side short-write/WOULD_BLOCK requires separate transport unit evidence", "No-physical-DO requires source/build audit in addition to TCP feedback", "TCP process is terminated by harness after tests"]}
(args.output / "report.json").write_text(json.dumps(report, indent=2), encoding="utf-8")
(args.output / "wire.json").write_text(json.dumps(suite.trace, indent=2), encoding="utf-8")
(args.output / "objects.json").write_text(json.dumps(suite.decoded, indent=2), encoding="utf-8")
return 0 if suite.results and all(row["passed"] for row in suite.results) else 1
if __name__ == "__main__":
raise SystemExit(main())
@@ -0,0 +1,74 @@
"""Original independent Net fixtures compiled ONLY against migrated vendor code."""
import argparse
import hashlib
import json
import os
from pathlib import Path
import subprocess
import sys
from oracle import ROOT
sys.path.insert(0, str(ROOT / "test/host/common"))
from unit_test_harness import decode, locate_toolchain
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--output", type=Path, default=ROOT / "ci-artifacts/iec104-tests/transport")
parser.add_argument("--only", default="")
args = parser.parse_args()
output = args.output.resolve()
output.mkdir(parents=True, exist_ok=True)
vendor = ROOT / "App/protocol/iec60870"
original = ROOT / "IEC60870-Slave/test"
cl, inc, lib = locate_toolchain()
if not cl:
raise RuntimeError("MSVC compiler not found")
env = dict(os.environ, INCLUDE=os.pathsep.join(inc), LIB=os.pathsep.join(lib))
sources = [vendor / line.split(" ", 1)[1] for line in (vendor / "UPSTREAM.sha256").read_text().splitlines() if line.endswith(".c")]
# Each fixture includes vendor TcpServer.c and provides the Net implementation.
sources = [p for p in sources if p.name not in ("IEC104_TcpServer.c", "IEC104_Net_Windows.c")]
assert len(sources) >= 26 and all(p.is_file() for p in sources)
include = [vendor / "104", vendor / "104/business", vendor / "common", vendor / "platform", vendor / "examples", original]
names = ("iec104_response_transport_test", "iec104_ack_tracking_test", "iec104_idle_transport_test", "iec104_strict_priority_transport_test", "iec104_tcp_event_ring_test", "iec104_tcp_scheduler_test", "iec104_remote_parameter_test")
results = []
for name in names + ("source_binding_test", "vendor_boundary_test", "core_lifecycle_test"):
if args.only and args.only not in name:
continue
target = output / name
target.mkdir(exist_ok=True)
fixture = Path(__file__).parent / (name + ".c") if name in ("source_binding_test", "vendor_boundary_test", "core_lifecycle_test") else original / (name + ".c")
adaptation = None
if name == "iec104_idle_transport_test":
text = fixture.read_text(encoding="utf-8-sig")
old = " CHECK(IEC104_Receiver_Feed(&sut->connection.receiver, sut->generation, bytes, length, &consumed) =="
new = " CHECK(length <= UINT32_MAX);\n CHECK(IEC104_Receiver_Feed(&sut->connection.receiver, sut->generation, bytes, (uint32_t)length, &consumed) =="
assert text.count(old) == 1
adaptation = {"original_sha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "change": "checked size_t to uint32 length conversion for MSVC x64 only"}
fixture = target / "idle_fixture_checked.c"
fixture.write_text(text.replace(old, new), encoding="utf-8")
extra = []
if name == "source_binding_test":
extra = [ROOT / "App/protocol/iec104/iec104_source.c", ROOT / "App/protocol/iec104/iec104_dtu_table.c", vendor / "104/IEC104_Net_Windows.c", vendor / "104/IEC104_TcpServer.c"]
if name == "core_lifecycle_test":
extra = [ROOT / "App/protocol/iec104/iec104_core.c", ROOT / "App/protocol/iec104/iec104_source.c", ROOT / "App/protocol/iec104/iec104_dtu_table.c"]
flags = ["/nologo", "/std:c11", "/O2", "/W3", "/WX", "/utf-8", "/D_CRT_SECURE_NO_WARNINGS"]
if name == "iec104_remote_parameter_test":
flags += ["/DIEC104_REMOTE_PARAMETER_TEST_HOOKS", "/DIEC104_TCP_HIGH_CAPACITY=251"]
command = [cl] + flags + ["/I" + str(p) for p in include] + ["/I" + str(ROOT / "App/protocol/iec104")] + [str(fixture)] + [str(p) for p in sources + extra] + ["/Fe:" + str(target / (name + ".exe"))] + (["/link", "ws2_32.lib"] if extra else [])
build = subprocess.run(command, cwd=target, env=env, capture_output=True)
(target / "build.log").write_text(decode(build.stdout) + decode(build.stderr), encoding="utf-8")
row = {"name": name, "build_rc": build.returncode, "command": command, "fixture_sha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "adaptation": adaptation, "extra_sources_sha256": {str(p.relative_to(ROOT)): hashlib.sha256(p.read_bytes()).hexdigest() for p in extra}}
if build.returncode == 0:
run = subprocess.run([str(target / (name + ".exe"))], cwd=target, capture_output=True, timeout=120)
(target / "run.log").write_text(decode(run.stdout) + decode(run.stderr), encoding="utf-8")
row["run_rc"] = run.returncode
results.append(row)
print(name, row.get("run_rc", "build failed"), flush=True)
report = {"compiler": cl, "vendor_sources": {str(p.relative_to(ROOT)): hashlib.sha256(p.read_bytes()).hexdigest() for p in sources}, "tcp_source_sha256": hashlib.sha256((vendor / "104/IEC104_TcpServer.c").read_bytes()).hexdigest(), "results": results}
(output / "report.json").write_text(json.dumps(report, indent=2), encoding="utf-8")
return 0 if all(r.get("run_rc", -1) == 0 for r in results) else 1
if __name__ == "__main__":
raise SystemExit(main())
+57
View File
@@ -0,0 +1,57 @@
"""Test the independent acceptance oracle/decoder; not product acceptance."""
import struct
import unittest
from oracle import reference
from wire import asdu, header, objects, remote_objects, tlv
class WireOracleTests(unittest.TestCase):
def test_reference_has_independently_derived_sets(self):
ref = reference()
self.assertEqual(len(ref["gi"]), 225)
self.assertEqual(len(ref["read"]), 121)
self.assertEqual(len(ref["counters"]), 8)
self.assertEqual(len(ref["remote"]), 345)
self.assertEqual(len(ref["diagnostic_aliases_pending_scope"]), 150)
self.assertNotIn(0x9601, ref["remote"])
self.assertEqual(ref["remote"][0x8858], (38, "25"))
def test_pn_zero_positive_and_one_negative(self):
self.assertFalse(header(bytes.fromhex("64010711010000000014"))["pn"])
self.assertTrue(header(bytes.fromhex("64014711010000000014"))["pn"])
self.assertEqual(header(bytes.fromhex("64014711010000000014"))["cot"], 7)
def test_float_objects_nonsequential_and_sequential(self):
rows = objects(bytes.fromhex("0d02141101000140000000c03f00024000000020c000"))
self.assertEqual([(r["ioa"], r["value"]) for r in rows], [(0x4001, 1.5), (0x4002, -2.5)])
rows = objects(bytes.fromhex("0d82141101000140000000c03f00000020c000"))
self.assertEqual([(r["ioa"], r["value"]) for r in rows], [(0x4001, 1.5), (0x4002, -2.5)])
def test_truncation_and_trailing_bytes_rejected(self):
valid = bytes.fromhex("01011411010001000000")
for bad in (valid[:-1], valid + b"\x00"):
with self.assertRaises(AssertionError):
objects(bad)
def test_remote_type_tags_decode_independent_vector(self):
vector = bytes.fromhex("ca030711010000000020880001010123880026040000e0402d88002d020100")
rows = remote_objects(vector)
self.assertEqual([(r["ioa"], r["tag"], r["value"]) for r in rows], [(0x8820, 1, 1), (0x8823, 38, 7.0), (0x882D, 45, 1)])
self.assertEqual(tlv(0x8823, 38, 7.0), bytes.fromhex("23880026040000e040"))
def test_remote_malformed_tlv_length_rejected(self):
with self.assertRaises(AssertionError):
remote_objects(bytes.fromhex("ca01071101000000002388002603000000"))
def test_maximum_apdu_is_255_not_257(self):
payload = bytes.fromhex("000080ffffff0000") + bytes(235)
data = asdu(203, payload)
self.assertEqual(len(data), 249)
frame = bytes((0x68, len(data) + 4)) + struct.pack("<HH", 0, 0) + data
self.assertEqual(len(frame), 255)
self.assertEqual(frame[1], 253)
if __name__ == "__main__":
unittest.main(verbosity=2)
@@ -0,0 +1,58 @@
/* Independent source-binding failure injection; no hardware or product codec oracle. */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "iec104_source.h"
#define CHECK(x) do { if (!(x)) { fprintf(stderr, "line %d: %s\n", __LINE__, #x); exit(1); } } while (0)
static uint16_t special_id;
static float special_value;
static unsigned apply_count;
static bool read_parameter(void *ctx, uint16_t id, float *out)
{
(void)ctx;
if (id == special_id) { *out = special_value; return true; }
*out = id == 17U ? 250.0f : id == 20U ? 100.0f : 1.0f;
return true;
}
static bool apply_parameter(void *ctx, const iec104_source_change_t *changes, size_t count, bool validate)
{
(void)ctx; (void)changes; (void)count;
if (!validate) { ++apply_count; }
return true;
}
static bool measurement(void *ctx, uint16_t addr, float *v, uint8_t *q)
{
(void)ctx; (void)addr; *v = 12.5f; *q = 0x51U; return true;
}
static void invalid_read(uint32_t ioa, uint16_t id, float value)
{
iec104_source_value_t out;
special_id = id; special_value = value;
if (iec104_source_remote_read(0U, ioa, &out)) {
fprintf(stderr, "unexpected readable ioa=%lx semantic=%u input=%g tag=%u\n", (unsigned long)ioa, id, value, out.tag);
exit(1);
}
}
int main(void)
{
iec104_source_bindings_t bind = {0};
iec104_source_object_t object;
iec104_source_stats_t stats;
bind.parameter_read = read_parameter; bind.parameter_apply = apply_parameter;
bind.measurement = measurement;
iec104_source_init(); iec104_source_bind(&bind);
/* Mapped profile entries: bool8841 -> semantic1; float8842 ->2.
* There is no mapped UShort callback entry: do not fake one to claim coverage. */
invalid_read(0x8841U, 1U, 2.0f);
invalid_read(0x8841U, 1U, 1.5f);
invalid_read(0x8842U, 2U, NAN);
invalid_read(0x8842U, 2U, INFINITY);
special_id = 0U;
CHECK(iec104_source_find(0x4001U, &object));
CHECK(object.measurement.value == 12.5f);
CHECK(object.measurement.quality == 0x51U);
iec104_source_stats(&stats);
CHECK(stats.physical_do_actions == 0U && apply_count == 0U);
puts("source binding typed fail-closed/quality: PASS");
return 0;
}
@@ -0,0 +1,34 @@
/* Runtime macro/priority boundaries against migrated transport, no warning suppression. */
#include "iec104_tcp_fixture.h"
#include "IEC104_Command.h"
#include <limits.h>
static void check_type(int type)
{
uint16_t expected = 0U;
if (type >= 45 && type <= 51) { expected = (uint16_t)(1U << (type - 45)); }
if (type >= 58 && type <= 64) { expected = (uint16_t)(1U << (type - 51)); }
CHECK(IEC104_COMMAND_TYPE_BIT(type) == expected);
}
int main(void)
{
int type;
IEC104_SendManager_t *manager;
uint64_t generation, ticket = 0U;
IEC104_BusinessPriority_t bad[] = {(IEC104_BusinessPriority_t)-1,
IEC104_BUSINESS_PRIORITY_COUNT, (IEC104_BusinessPriority_t)UINT_MAX};
size_t i;
for (type = -128; type <= 255; ++type) { check_type(type); }
CHECK(IEC104_COMMAND_TYPE_BIT(UINT_MAX) == 0U);
CHECK(IEC104_COMMAND_TYPE_BIT(32) == 0U && IEC104_COMMAND_TYPE_BIT(31) == 0U);
CHECK(IEC104_COMMAND_TYPE_BIT(63) == (1U << 12));
begin(); manager = &sut->connection.send_manager;
generation = manager->connection_generation;
for (i = 0U; i < sizeof(bad) / sizeof(bad[0]); ++i) {
CHECK(IEC104_SendManager_Can_Submit_Response_Priority(manager, generation, sizeof(asdu), bad[i]) == IEC104_SEND_MANAGER_ERROR);
CHECK(IEC104_SendManager_Can_Submit_Idle_Priority(manager, generation, 0U, bad[i]) == IEC104_SEND_MANAGER_ERROR);
CHECK(IEC104_SendManager_Enqueue_Response_Priority(manager, generation, asdu, sizeof(asdu), &ticket, bad[i]) == IEC104_SEND_MANAGER_ERROR);
}
end();
puts("vendor macro384 values/priority negative-count-uintmax: PASS");
return 0;
}
+196
View File
@@ -0,0 +1,196 @@
"""Small independent IEC104 wire client with strict stream and sequence checks."""
import datetime
import socket
import struct
import time
def u24(value):
return value.to_bytes(3, "little")
def asdu(typ, payload=b"", count=1, cot=6, ca=1, oa=17):
return bytes((typ, count, cot, oa)) + struct.pack("<H", ca) + payload
def header(data):
assert len(data) >= 6, ("short ASDU", data.hex())
return {"type": data[0], "count": data[1] & 127, "sq": bool(data[1] & 128),
"cot": data[2] & 63, "pn": bool(data[2] & 64), "test": bool(data[2] & 128),
"oa": data[3], "ca": int.from_bytes(data[4:6], "little")}
def cp56(raw):
assert len(raw) == 7
ms, minute, hour, day, month, year = struct.unpack("<HBBBBB", raw)
assert ms < 60000 and minute & 63 < 60 and hour & 31 < 24
assert not minute & 64 and not hour & 96 and not month & 240 and not year & 128
value = datetime.datetime(2000 + (year & 127), month & 15, day & 31,
hour & 31, minute & 63, ms // 1000, (ms % 1000) * 1000)
weekday = day >> 5
assert weekday == 0 or weekday == value.isoweekday()
return {"iso": value.isoformat(), "invalid": bool(minute & 128), "summer": bool(hour & 128)}
def objects(data):
h = header(data)
widths = {1: 1, 3: 1, 5: 2, 7: 5, 9: 3, 11: 3, 13: 5, 15: 5,
30: 8, 31: 8, 34: 10, 35: 10, 36: 12, 37: 12,
110: 3, 111: 3, 112: 5, 206: 5, 207: 12}
width = widths[h["type"]]
offset, base, rows = 6, None, []
for index in range(h["count"]):
if not h["sq"] or index == 0:
assert offset + 3 <= len(data)
base = int.from_bytes(data[offset:offset + 3], "little")
offset += 3
ioa = base + index if h["sq"] else base
raw = data[offset:offset + width]
assert len(raw) == width
typ = h["type"]
if typ in (13, 36, 112, 206, 207):
value = struct.unpack("<f", raw[:4])[0]
elif typ in (9, 11, 34, 35, 110, 111):
value = int.from_bytes(raw[:2], "little", signed=True)
elif typ in (7, 15, 37):
value = int.from_bytes(raw[:4], "little", signed=typ != 7)
elif typ == 5:
value = raw[0] & 127
if value & 64:
value -= 128
else:
value = raw[0] & (1 if typ in (1, 30) else 3)
row = {"ioa": ioa, "type": typ, "value": value, "raw": raw.hex()}
if typ in (110, 111, 112):
row["qpm"] = raw[-1]
elif typ in (15, 37):
row["quality"] = raw[4] & 224
row["bcr_sequence"] = raw[4] & 31
elif typ in (1, 3, 30, 31):
row["quality"] = raw[0] & 240
else:
quality_offset = 1 if typ == 5 else 2 if typ in (9, 11, 34, 35) else 4
row["quality"] = raw[quality_offset]
if typ in (30, 31, 34, 35, 36, 37):
row["time"] = cp56(raw[-7:])
rows.append(row)
offset += width
assert offset == len(data), ("trailing object bytes", data.hex(), offset)
return rows
def remote_objects(data):
h = header(data)
assert h["type"] == 202 and not h["sq"] and len(data) >= 9
offset, rows = 9, []
for _ in range(h["count"]):
assert offset + 5 <= len(data)
ioa = int.from_bytes(data[offset:offset + 3], "little")
tag, length = data[offset + 3:offset + 5]
offset += 5
raw = data[offset:offset + length]
assert len(raw) == length
if tag == 38:
assert length == 4
value = struct.unpack("<f", raw)[0]
elif tag == 4:
value = raw.decode("ascii")
else:
assert tag in (1, 35, 45)
assert length == {1: 1, 35: 4, 45: 2}[tag]
value = int.from_bytes(raw, "little")
rows.append({"ioa": ioa, "tag": tag, "value": value, "raw": raw.hex()})
offset += length
assert offset == len(data)
return rows
def tlv(ioa, tag, value):
raw = struct.pack("<f", value) if tag == 38 else value.encode("ascii") if tag == 4 else int(value).to_bytes({1: 1, 35: 4, 45: 2}[tag], "little")
return u24(ioa) + bytes((tag, len(raw))) + raw
class Client:
def __init__(self, host, port, trace, timeout=5):
self.sock = socket.create_connection((host, port), timeout)
self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self.sock.settimeout(timeout)
self.ns = self.nr = 0
self.buffer = bytearray()
self.trace = trace
self.asdus = []
self.timeout = timeout
def close(self):
self.sock.close()
def send(self, frame, split=False):
self.trace.append({"direction": "tx", "time": time.monotonic(), "hex": frame.hex()})
if split:
for byte in frame:
self.sock.sendall(bytes((byte,)))
time.sleep(0.002)
else:
self.sock.sendall(frame)
def pack(self, data):
assert len(data) <= 249
frame = bytes((0x68, len(data) + 4)) + struct.pack("<HH", self.ns * 2, self.nr * 2) + data
self.ns = (self.ns + 1) & 32767
return frame
def request(self, data, split=False):
self.send(self.pack(data), split)
def frame(self, deadline=None):
deadline = deadline or time.monotonic() + self.timeout
while len(self.buffer) < 2 or len(self.buffer) < self.buffer[1] + 2:
self.sock.settimeout(max(0.001, deadline - time.monotonic()))
data = self.sock.recv(4096)
if not data:
raise EOFError("server closed connection")
self.buffer.extend(data)
assert self.buffer[0] == 0x68
if len(self.buffer) >= 2:
assert 4 <= self.buffer[1] <= 253
size = self.buffer[1] + 2
frame = bytes(self.buffer[:size])
del self.buffer[:size]
self.trace.append({"direction": "rx", "time": time.monotonic(), "hex": frame.hex()})
if not frame[2] & 1:
ns, nr = struct.unpack("<HH", frame[2:6])
assert ns // 2 == self.nr, ("receive sequence", ns // 2, self.nr)
assert nr // 2 <= self.ns
self.nr = (self.nr + 1) & 32767
self.send(b"\x68\x04\x01\x00" + struct.pack("<H", self.nr * 2))
elif frame[2] == 0x43:
self.send(b"\x68\x04\x83\x00\x00\x00")
return frame
def start(self, split=False):
self.send(b"\x68\x04\x07\x00\x00\x00", split)
deadline = time.monotonic() + self.timeout
while self.frame(deadline)[2] != 0x0B:
assert time.monotonic() < deadline
def receive(self, predicate, timeout=None):
deadline = time.monotonic() + (timeout or self.timeout)
observed = []
while time.monotonic() < deadline:
frame = self.frame(deadline)
if not frame[2] & 1:
data = frame[6:]
observed.append(data)
self.asdus.append(data)
if predicate(data):
return data, observed
raise TimeoutError("expected response not received")
def response(self, typ, cot=None, pn=False):
data, _ = self.receive(lambda data: header(data)["type"] == typ)
h = header(data)
assert h["pn"] == pn, ("PN", data.hex(), pn)
assert h["ca"] == 1 and h["oa"] == 17, ("address echo", data.hex())
if cot is not None:
assert h["cot"] == cot, ("COT", data.hex(), cot)
return data