Initial commit: C++20 SECS-II / HSMS / GEM client + server
A fully containerised SECS/GEM toolchain. Single docker compose project,
no host build tools. 63 unit-test cases / 278 assertions, two demo
executables, end-to-end two-container demo exercising every implemented
capability.
Architecture (bottom-up):
secs2/ E5 SECS-II codec
Item variant over L/A/B/BOOLEAN/I1-8/U1-8/F4/F8
encode/decode big-endian, 1/2/3-byte length encoding
Message SxFy + W-bit + optional root item
to_sml human-readable text rendering
hsms/ E37 HSMS transport (TCP)
Header 10-byte header + SType enum (Data/Select/Deselect/
Linktest/Reject/Separate)
Frame 4-byte length prefix + payload encode/decode
Connection async Asio TCP, NOT-SELECTED -> SELECTED state machine,
T3/T5/T6/T7/T8 timers, system-bytes reply correlation,
graceful close-after-flush separation
endpoint active Client (connect with T5 retry) and passive Server
(accept loop) wrappers over Connection
gem/ E30 GEM logic
ControlStateMachine 5-state E30 control model with operator
actions, host requests, SEMI-mandated ack
codes (OnlineAck, OfflineAck, CommAck), and
a state-change handler
EquipmentDataModel in-memory dictionary: SVIDs, DVIDs, ECIDs
(with EAC), CEIDs, report defs, CEID->report
links, enabled-events set, alarm table
(ALCD, enabled, active), process programs,
host command registry, clock (16-char
YYYYMMDDhhmmsscc with offset)
messages.hpp builders + parsers for every SxFy below
GEM message coverage (full list):
S1F1/F2 Are You There / On Line Data
S1F3/F4 Selected Equipment Status Request / Data
S1F11/F12 Status Variable Namelist Request / Data
S1F13/F14 Establish Communications (+ CommAck)
S1F15/F16 Request OFFLINE (+ OfflineAck)
S1F17/F18 Request ONLINE (+ OnlineAck)
S2F13/F14 Equipment Constant Request / Data
S2F15/F16 EC Send + EquipmentAck (Accept/UnknownEcid/Busy/OutOfRange)
S2F17/F18 Date and Time Request / Data
S2F29/F30 Equipment Constant Namelist Request / Data
S2F31/F32 Date and Time Set Request / TimeAck
S2F33/F34 Define Report + DefineReportAck (5 enum values)
S2F35/F36 Link Event Report + LinkEventAck
S2F37/F38 Enable / Disable Event Report + EnableEventAck
S2F41/F42 Host Command + HostCmdAck (7 values) + per-param CPACKs
S5F1/F2 Alarm Report Send + AlarmAck (ALCD bit-7 set/cleared
+ lower-7 category)
S5F3/F4 Enable/Disable Alarm Send + AlarmAck
S5F5/F6 List Alarms Request / Data (active alarms tagged in ALCD)
S6F11/F12 Event Report Send (equipment-initiated CEID emission
with full report data) + EventReportAck
S7F3/F4 Process Program Send + ProcessProgramAck (7 values)
S7F5/F6 Process Program Request / Data
S7F19/F20 Current EPPD List Request / Data
S10F1/F2 Terminal Display Single (host->equipment) + TerminalAck
S10F3/F4 Terminal Display Single (equipment->host)
Demo apps:
apps/secs_server.cpp passive equipment. Populates the data model
with 3 SVIDs (ControlState, Clock,
EventsEnabled), 2 ECIDs, 3 CEIDs
(ControlStateChanged, AlarmSetEvent,
ProcessStarted), 2 alarms (Chiller Temp High
cat 4, Door Open cat 1), 2 recipes
(RECIPE-A, RECIPE-B), and 4 host commands
(START, STOP, PAUSE, FAULT). Emits S6F11 on
every control state transition + on START;
emits S5F1 + the AlarmSetEvent CEID on FAULT.
Pushes an S10F3 welcome message when the host
comes online.
apps/secs_client.cpp active host. Walks 17 steps: Establish ->
Online -> S1F11 SVID namelist -> S1F3 read ->
S2F29 EC namelist -> S2F13 read ->
S2F17 clock -> S2F33/S2F35/S2F37 dynamic
event subscription -> S2F41 START
(-> receives S6F11) -> S5F5 alarm list ->
S5F3 enable alarm 1 -> S2F41 FAULT
(-> receives S5F1 + S6F11) -> S7F19/S7F5
recipe list + body -> S10F1 terminal ->
S1F15 Offline -> Separate. Handles inbound
S6F11, S5F1, S10F3 primaries.
Testing:
tests/test_secs2.cpp codec round-trip for every format,
byte-layout assertions for known values,
truncation/trailing-byte rejection,
nested list round-trip, SML rendering
tests/test_hsms.cpp header byte layout, data + control
header round-trip, full frame round-
trip with length prefix, short-payload
rejection
tests/test_control_state.cpp every (state, event) pair in the E30
control state machine, including
AlreadyOnline / NotAccept rejections
and idempotent offline-while-offline
tests/test_data_model.cpp SVID/ECID/Alarm/Recipe CRUD, clock
format + parse, host command registry,
full event-report pipeline (define ->
link -> enable -> compose) with
every error path (InvalidVid,
UnknownCeid, UnknownRptid), alarm
set/clear with ALCD bit-7 semantics
tests/test_messages.cpp round-trip + byte-layout for every
builder/parser pair, including S6F11
event reports with mixed item types
Toolchain:
Dockerfile Ubuntu 24.04, g++-13, CMake, Ninja, libasio-dev
docker-compose.yml builder / tests / server / client services,
source bind-mounted, build artifacts in a
named volume so the host tree stays clean
CMakeLists.txt C++20, -Wall -Wextra -Wpedantic, standalone
Asio (ASIO_STANDALONE), doctest via FetchContent
Documentation:
README.md architecture, quick start, demo log
COMPLIANCE.md honest per-capability E5/E30/E37 audit with
spec section refs. Calls out what's implemented,
what's partial (Reject.req, Alarms missing F7/F8,
EC range validation, PP without verify, terminal
single-line only), and what's intentionally not
yet implemented (spooling, S9 error stream,
Documentation S1F19/F20+F21/F22, limits monitoring,
trace data collection, multi-block, material
movement). Does NOT claim "100% GEM-compliant" and
lists the work required to honestly make that claim.
This is Layer 0 + the start of Layer 1 from implementation_plan.md.
The transition-table-driven "spec-as-data" architecture (Layer 1
proper) is not yet implemented; the current code uses imperative
state machines that are structurally ready to be refactored onto
tables.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "secsgem/gem/control_state.hpp"
|
||||
|
||||
using namespace secsgem::gem;
|
||||
|
||||
namespace {
|
||||
|
||||
struct Recorder {
|
||||
std::vector<std::tuple<ControlState, ControlState, ControlEvent>> changes;
|
||||
|
||||
ControlStateMachine::StateChangeHandler handler() {
|
||||
return [this](ControlState from, ControlState to, ControlEvent ev) {
|
||||
changes.emplace_back(from, to, ev);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("default initial state is HostOffline") {
|
||||
ControlStateMachine sm;
|
||||
CHECK(sm.state() == ControlState::HostOffline);
|
||||
CHECK_FALSE(sm.online());
|
||||
}
|
||||
|
||||
TEST_CASE("custom initial state") {
|
||||
ControlStateMachine sm({.initial = ControlState::EquipmentOffline});
|
||||
CHECK(sm.state() == ControlState::EquipmentOffline);
|
||||
}
|
||||
|
||||
TEST_CASE("host request online from HostOffline -> OnlineRemote") {
|
||||
Recorder rec;
|
||||
ControlStateMachine sm;
|
||||
sm.set_state_change_handler(rec.handler());
|
||||
|
||||
CHECK(sm.on_host_request_online() == OnlineAck::Accept);
|
||||
CHECK(sm.state() == ControlState::OnlineRemote);
|
||||
CHECK(sm.online());
|
||||
|
||||
REQUIRE(rec.changes.size() == 2);
|
||||
CHECK(std::get<1>(rec.changes[0]) == ControlState::AttemptOnline);
|
||||
CHECK(std::get<2>(rec.changes[0]) == ControlEvent::HostRequestOnline);
|
||||
CHECK(std::get<1>(rec.changes[1]) == ControlState::OnlineRemote);
|
||||
CHECK(std::get<2>(rec.changes[1]) == ControlEvent::AttemptComplete);
|
||||
}
|
||||
|
||||
TEST_CASE("host request online when already online -> AlreadyOnline, no transition") {
|
||||
ControlStateMachine sm;
|
||||
sm.on_host_request_online();
|
||||
REQUIRE(sm.state() == ControlState::OnlineRemote);
|
||||
|
||||
Recorder rec;
|
||||
sm.set_state_change_handler(rec.handler());
|
||||
CHECK(sm.on_host_request_online() == OnlineAck::AlreadyOnline);
|
||||
CHECK(sm.state() == ControlState::OnlineRemote);
|
||||
CHECK(rec.changes.empty());
|
||||
}
|
||||
|
||||
TEST_CASE("host request online from EquipmentOffline -> NotAccept") {
|
||||
ControlStateMachine sm({.initial = ControlState::EquipmentOffline});
|
||||
CHECK(sm.on_host_request_online() == OnlineAck::NotAccept);
|
||||
CHECK(sm.state() == ControlState::EquipmentOffline);
|
||||
}
|
||||
|
||||
TEST_CASE("host request offline from OnlineRemote -> HostOffline") {
|
||||
ControlStateMachine sm;
|
||||
sm.on_host_request_online();
|
||||
REQUIRE(sm.state() == ControlState::OnlineRemote);
|
||||
|
||||
Recorder rec;
|
||||
sm.set_state_change_handler(rec.handler());
|
||||
CHECK(sm.on_host_request_offline() == OfflineAck::Accept);
|
||||
CHECK(sm.state() == ControlState::HostOffline);
|
||||
|
||||
REQUIRE(rec.changes.size() == 1);
|
||||
CHECK(std::get<1>(rec.changes[0]) == ControlState::HostOffline);
|
||||
CHECK(std::get<2>(rec.changes[0]) == ControlEvent::HostRequestOffline);
|
||||
}
|
||||
|
||||
TEST_CASE("host request offline when already offline is idempotent Accept") {
|
||||
ControlStateMachine sm;
|
||||
Recorder rec;
|
||||
sm.set_state_change_handler(rec.handler());
|
||||
CHECK(sm.on_host_request_offline() == OfflineAck::Accept);
|
||||
CHECK(sm.state() == ControlState::HostOffline);
|
||||
CHECK(rec.changes.empty());
|
||||
}
|
||||
|
||||
TEST_CASE("operator online from EquipmentOffline -> OnlineLocal by default") {
|
||||
ControlStateMachine sm({.initial = ControlState::EquipmentOffline});
|
||||
CHECK(sm.operator_online());
|
||||
CHECK(sm.state() == ControlState::OnlineLocal);
|
||||
}
|
||||
|
||||
TEST_CASE("operator online with default_remote -> OnlineRemote") {
|
||||
ControlStateMachine sm({.initial = ControlState::HostOffline, .operator_default_remote = true});
|
||||
CHECK(sm.operator_online());
|
||||
CHECK(sm.state() == ControlState::OnlineRemote);
|
||||
}
|
||||
|
||||
TEST_CASE("operator online when already online is rejected") {
|
||||
ControlStateMachine sm({.initial = ControlState::OnlineLocal});
|
||||
CHECK_FALSE(sm.operator_online());
|
||||
CHECK(sm.state() == ControlState::OnlineLocal);
|
||||
}
|
||||
|
||||
TEST_CASE("operator offline from any online state -> HostOffline") {
|
||||
ControlStateMachine sm({.initial = ControlState::OnlineRemote});
|
||||
CHECK(sm.operator_offline());
|
||||
CHECK(sm.state() == ControlState::HostOffline);
|
||||
}
|
||||
|
||||
TEST_CASE("operator local toggles only from OnlineRemote") {
|
||||
ControlStateMachine sm({.initial = ControlState::OnlineRemote});
|
||||
CHECK(sm.operator_local());
|
||||
CHECK(sm.state() == ControlState::OnlineLocal);
|
||||
CHECK_FALSE(sm.operator_local()); // already local
|
||||
}
|
||||
|
||||
TEST_CASE("operator remote toggles only from OnlineLocal") {
|
||||
ControlStateMachine sm({.initial = ControlState::OnlineLocal});
|
||||
CHECK(sm.operator_remote());
|
||||
CHECK(sm.state() == ControlState::OnlineRemote);
|
||||
CHECK_FALSE(sm.operator_remote()); // already remote
|
||||
}
|
||||
|
||||
TEST_CASE("is_online classifier") {
|
||||
CHECK_FALSE(is_online(ControlState::EquipmentOffline));
|
||||
CHECK_FALSE(is_online(ControlState::AttemptOnline));
|
||||
CHECK_FALSE(is_online(ControlState::HostOffline));
|
||||
CHECK(is_online(ControlState::OnlineLocal));
|
||||
CHECK(is_online(ControlState::OnlineRemote));
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
#include "secsgem/gem/data_model.hpp"
|
||||
|
||||
using namespace secsgem::gem;
|
||||
|
||||
TEST_CASE("SVID add / get / list / set value") {
|
||||
EquipmentDataModel m;
|
||||
m.add_status_variable({1, "Clock", "", s2::Item::ascii("19700101000000")});
|
||||
m.add_status_variable({2, "EventsEnabled", "", s2::Item::boolean(true)});
|
||||
|
||||
CHECK(m.status_variable(1).has_value());
|
||||
CHECK(m.status_variable(99).has_value() == false);
|
||||
CHECK(m.all_status_variables().size() == 2);
|
||||
|
||||
m.set_status_value(2, s2::Item::boolean(false));
|
||||
CHECK(m.status_variable(2)->value == s2::Item::boolean(false));
|
||||
}
|
||||
|
||||
TEST_CASE("ECID set rejects unknown id") {
|
||||
EquipmentDataModel m;
|
||||
m.add_equipment_constant({10, "MaxSpool", "msgs", s2::Item::u4(uint32_t{100}),
|
||||
s2::Item::u4(uint32_t{100}), "0", "1000"});
|
||||
CHECK(m.set_equipment_constant_value(10, s2::Item::u4(uint32_t{50})) ==
|
||||
EquipmentAck::Accept);
|
||||
CHECK(m.equipment_constant(10)->value == s2::Item::u4(uint32_t{50}));
|
||||
CHECK(m.set_equipment_constant_value(999, s2::Item::u4(uint32_t{1})) ==
|
||||
EquipmentAck::Denied_UnknownEcid);
|
||||
}
|
||||
|
||||
TEST_CASE("clock string has 16 digit characters") {
|
||||
EquipmentDataModel m;
|
||||
auto s = m.current_time_string();
|
||||
REQUIRE(s.size() == 16);
|
||||
for (char c : s) CHECK(c >= '0');
|
||||
}
|
||||
|
||||
TEST_CASE("set_time_string accepts well-formed and rejects malformed") {
|
||||
EquipmentDataModel m;
|
||||
CHECK(m.set_time_string("20260101000000") == TimeAck::Accept);
|
||||
CHECK(m.set_time_string("2026010100000000") == TimeAck::Accept);
|
||||
CHECK(m.set_time_string("not-a-date-12345") == TimeAck::Error);
|
||||
CHECK(m.set_time_string("short") == TimeAck::Error);
|
||||
}
|
||||
|
||||
TEST_CASE("host command registry") {
|
||||
EquipmentDataModel m;
|
||||
m.register_command("START", [](const auto&) { return HostCmdAck::Accept; });
|
||||
m.register_command("STOP", [](const auto&) { return HostCmdAck::CannotDoNow; });
|
||||
|
||||
CHECK(m.has_command("START"));
|
||||
CHECK_FALSE(m.has_command("PAUSE"));
|
||||
CHECK(m.dispatch_command("START", {}) == HostCmdAck::Accept);
|
||||
CHECK(m.dispatch_command("STOP", {}) == HostCmdAck::CannotDoNow);
|
||||
CHECK(m.dispatch_command("UNKNOWN", {}) == HostCmdAck::InvalidCommand);
|
||||
}
|
||||
|
||||
// ---- Event reports -------------------------------------------------------
|
||||
|
||||
TEST_CASE("define report rejects unknown VID, accepts known VIDs") {
|
||||
EquipmentDataModel m;
|
||||
m.add_status_variable({1, "A", "", s2::Item::u4(uint32_t{10})});
|
||||
m.add_status_variable({2, "B", "", s2::Item::u4(uint32_t{20})});
|
||||
|
||||
CHECK(m.define_reports({{100, {1, 2}}}) == DefineReportAck::Accept);
|
||||
CHECK(m.define_reports({{101, {1, 999}}}) == DefineReportAck::InvalidVid);
|
||||
}
|
||||
|
||||
TEST_CASE("link / enable / compose: full event-report pipeline") {
|
||||
EquipmentDataModel m;
|
||||
m.add_status_variable({1, "ControlState", "", s2::Item::ascii("OnlineRemote")});
|
||||
m.add_status_variable({2, "Clock", "", s2::Item::ascii("19700101000000")});
|
||||
m.register_event({100, "ControlStateChanged"});
|
||||
m.register_event({200, "AlarmSetEvent"});
|
||||
|
||||
REQUIRE(m.define_reports({{1000, {1}}, {1001, {1, 2}}}) == DefineReportAck::Accept);
|
||||
REQUIRE(m.link_event_reports({{100, {1000, 1001}}, {200, {1001}}}) == LinkEventAck::Accept);
|
||||
CHECK_FALSE(m.is_event_enabled(100));
|
||||
|
||||
REQUIRE(m.enable_events(true, {100}) == EnableEventAck::Accept);
|
||||
CHECK(m.is_event_enabled(100));
|
||||
CHECK_FALSE(m.is_event_enabled(200));
|
||||
|
||||
auto reports = m.compose_reports_for(100);
|
||||
REQUIRE(reports.size() == 2);
|
||||
CHECK(reports[0].rptid == 1000);
|
||||
REQUIRE(reports[0].values.size() == 1);
|
||||
CHECK(reports[0].values[0] == s2::Item::ascii("OnlineRemote"));
|
||||
CHECK(reports[1].rptid == 1001);
|
||||
REQUIRE(reports[1].values.size() == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("enable_events with empty CEID list enables all registered events") {
|
||||
EquipmentDataModel m;
|
||||
m.register_event({1, "a"});
|
||||
m.register_event({2, "b"});
|
||||
CHECK(m.enable_events(true, {}) == EnableEventAck::Accept);
|
||||
CHECK(m.is_event_enabled(1));
|
||||
CHECK(m.is_event_enabled(2));
|
||||
CHECK(m.enable_events(false, {}) == EnableEventAck::Accept);
|
||||
CHECK_FALSE(m.is_event_enabled(1));
|
||||
}
|
||||
|
||||
TEST_CASE("link_event_reports rejects unknown CEID or RPTID") {
|
||||
EquipmentDataModel m;
|
||||
m.add_status_variable({1, "A", "", s2::Item::u4(uint32_t{0})});
|
||||
m.register_event({100, "x"});
|
||||
m.define_reports({{500, {1}}});
|
||||
|
||||
CHECK(m.link_event_reports({{999, {500}}}) == LinkEventAck::UnknownCeid);
|
||||
CHECK(m.link_event_reports({{100, {999}}}) == LinkEventAck::UnknownRptid);
|
||||
CHECK(m.link_event_reports({{100, {500}}}) == LinkEventAck::Accept);
|
||||
}
|
||||
|
||||
TEST_CASE("define_reports empty list clears reports and links") {
|
||||
EquipmentDataModel m;
|
||||
m.add_status_variable({1, "A", "", s2::Item::u4(uint32_t{0})});
|
||||
m.register_event({100, "x"});
|
||||
m.define_reports({{500, {1}}});
|
||||
m.link_event_reports({{100, {500}}});
|
||||
|
||||
CHECK(m.define_reports({}) == DefineReportAck::Accept);
|
||||
CHECK(m.all_reports().empty());
|
||||
CHECK(m.compose_reports_for(100).empty());
|
||||
}
|
||||
|
||||
// ---- Alarms --------------------------------------------------------------
|
||||
|
||||
TEST_CASE("alarm enable / set / clear / list") {
|
||||
EquipmentDataModel m;
|
||||
m.add_alarm({1, "Chiller Temp High", 4});
|
||||
m.add_alarm({2, "Door Open", 1});
|
||||
|
||||
CHECK(m.set_alarm_enabled(1, true) == AlarmAck::Accept);
|
||||
CHECK(m.alarm_enabled(1));
|
||||
CHECK(m.set_alarm_enabled(999, true) == AlarmAck::Error);
|
||||
|
||||
auto alcd_set = m.alarm_set(1);
|
||||
REQUIRE(alcd_set.has_value());
|
||||
CHECK((*alcd_set & 0x80) != 0);
|
||||
CHECK((*alcd_set & 0x7F) == 4);
|
||||
CHECK(m.alarm_active(1));
|
||||
|
||||
auto alcd_clr = m.alarm_clear(1);
|
||||
REQUIRE(alcd_clr.has_value());
|
||||
CHECK((*alcd_clr & 0x80) == 0);
|
||||
CHECK_FALSE(m.alarm_active(1));
|
||||
|
||||
CHECK(m.all_alarms().size() == 2);
|
||||
}
|
||||
|
||||
// ---- Process programs ---------------------------------------------------
|
||||
|
||||
TEST_CASE("process program CRUD") {
|
||||
EquipmentDataModel m;
|
||||
m.add_process_program("RECIPE-A", "step1\nstep2\n");
|
||||
m.add_process_program("RECIPE-B", "alt body");
|
||||
CHECK(m.process_program_list().size() == 2);
|
||||
CHECK(m.process_program("RECIPE-A").value() == "step1\nstep2\n");
|
||||
CHECK_FALSE(m.process_program("UNKNOWN").has_value());
|
||||
|
||||
CHECK(m.delete_process_program("RECIPE-A") == ProcessProgramAck::Accept);
|
||||
CHECK_FALSE(m.process_program("RECIPE-A").has_value());
|
||||
CHECK(m.delete_process_program("RECIPE-A") == ProcessProgramAck::PpidNotFound);
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
#include "secsgem/hsms/frame.hpp"
|
||||
#include "secsgem/hsms/header.hpp"
|
||||
#include "secsgem/secs2/codec.hpp"
|
||||
|
||||
using namespace secsgem::hsms;
|
||||
namespace s2 = secsgem::secs2;
|
||||
|
||||
TEST_CASE("data message header round-trip") {
|
||||
Header h = Header::data_message(0x1234, 1, 13, true, 0xAABBCCDD);
|
||||
auto bytes = h.encode();
|
||||
Header back = Header::decode(bytes.data());
|
||||
|
||||
CHECK(back == h);
|
||||
CHECK(back.stype == SType::Data);
|
||||
CHECK(back.w_bit());
|
||||
CHECK(back.stream() == 1);
|
||||
CHECK(back.function() == 13);
|
||||
CHECK(back.session_id == 0x1234);
|
||||
CHECK(back.system_bytes == 0xAABBCCDD);
|
||||
}
|
||||
|
||||
TEST_CASE("data message header byte layout") {
|
||||
Header h = Header::data_message(0x0001, 1, 1, true, 0x00000001);
|
||||
auto b = h.encode();
|
||||
std::array<uint8_t, 10> expected{0x00, 0x01, 0x81, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
|
||||
CHECK(b == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("W-bit clear when no reply expected") {
|
||||
Header h = Header::data_message(0, 6, 11, false, 7);
|
||||
CHECK_FALSE(h.w_bit());
|
||||
CHECK((h.byte2 & 0x80) == 0);
|
||||
CHECK(h.stream() == 6);
|
||||
}
|
||||
|
||||
TEST_CASE("control header layout (Select.req)") {
|
||||
Header h = Header::control(SType::SelectReq, 0x00000005);
|
||||
auto b = h.encode();
|
||||
std::array<uint8_t, 10> expected{0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05};
|
||||
CHECK(b == expected);
|
||||
|
||||
Header back = Header::decode(b.data());
|
||||
CHECK(back == h);
|
||||
CHECK(back.stype == SType::SelectReq);
|
||||
}
|
||||
|
||||
TEST_CASE("select.rsp carries status in byte3") {
|
||||
Header h = Header::control(SType::SelectRsp, 5, kControlSessionId, 0,
|
||||
static_cast<uint8_t>(SelectStatus::Ok));
|
||||
CHECK(h.byte3 == 0);
|
||||
Header back = Header::decode(h.encode().data());
|
||||
CHECK(back.stype == SType::SelectRsp);
|
||||
CHECK(back.byte3 == static_cast<uint8_t>(SelectStatus::Ok));
|
||||
}
|
||||
|
||||
TEST_CASE("frame round-trip with body") {
|
||||
s2::Item body_item = s2::Item::list({s2::Item::ascii("MDLN"), s2::Item::ascii("1.0")});
|
||||
std::vector<uint8_t> body = s2::encode(body_item);
|
||||
|
||||
Frame f(Header::data_message(0, 1, 2, false, 42), body);
|
||||
auto wire = f.encode();
|
||||
|
||||
// length prefix counts header + body
|
||||
uint32_t len = (wire[0] << 24) | (wire[1] << 16) | (wire[2] << 8) | wire[3];
|
||||
CHECK(len == kHeaderSize + body.size());
|
||||
CHECK(wire.size() == kLengthPrefixSize + len);
|
||||
|
||||
Frame back = Frame::decode(wire.data() + kLengthPrefixSize, len);
|
||||
CHECK(back.header == f.header);
|
||||
CHECK(back.body == body);
|
||||
CHECK(s2::decode(back.body) == body_item);
|
||||
}
|
||||
|
||||
TEST_CASE("control frame has empty body") {
|
||||
Frame f(Header::control(SType::LinktestReq, 99));
|
||||
auto wire = f.encode();
|
||||
uint32_t len = (wire[0] << 24) | (wire[1] << 16) | (wire[2] << 8) | wire[3];
|
||||
CHECK(len == kHeaderSize);
|
||||
|
||||
Frame back = Frame::decode(wire.data() + kLengthPrefixSize, len);
|
||||
CHECK(back.body.empty());
|
||||
CHECK(back.header.stype == SType::LinktestReq);
|
||||
}
|
||||
|
||||
TEST_CASE("frame decode rejects short payload") {
|
||||
std::vector<uint8_t> tooShort(5, 0);
|
||||
CHECK_THROWS_AS(Frame::decode(tooShort.data(), tooShort.size()), FrameError);
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include <doctest/doctest.h>
|
||||
@@ -0,0 +1,224 @@
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
#include "secsgem/gem/messages.hpp"
|
||||
|
||||
using namespace secsgem::gem;
|
||||
|
||||
TEST_CASE("S1F3 round-trip preserves SVID list") {
|
||||
auto m = s1f3_selected_status_request({1, 2, 99});
|
||||
CHECK(m.stream == 1);
|
||||
CHECK(m.function == 3);
|
||||
CHECK(m.reply_expected);
|
||||
auto parsed = parse_s1f3(m);
|
||||
REQUIRE(parsed.has_value());
|
||||
CHECK(*parsed == std::vector<uint32_t>{1, 2, 99});
|
||||
}
|
||||
|
||||
TEST_CASE("S1F4 substitutes empty list for unknown SVIDs") {
|
||||
std::vector<std::optional<s2::Item>> values = {s2::Item::u4(uint32_t{42}), std::nullopt,
|
||||
s2::Item::ascii("ok")};
|
||||
auto m = s1f4_selected_status_data(values);
|
||||
REQUIRE(m.body.has_value());
|
||||
const auto& children = m.body->as_list();
|
||||
REQUIRE(children.size() == 3);
|
||||
CHECK(children[0] == s2::Item::u4(uint32_t{42}));
|
||||
CHECK(children[1] == s2::Item::list({}));
|
||||
CHECK(children[2] == s2::Item::ascii("ok"));
|
||||
}
|
||||
|
||||
TEST_CASE("S1F12 round-trip preserves SVID, name, units") {
|
||||
std::vector<StatusVariable> svs = {{1, "Clock", "sec", s2::Item::ascii("0")},
|
||||
{2, "Power", "W", s2::Item::u4(uint32_t{0})}};
|
||||
auto m = s1f12_status_namelist_data(svs);
|
||||
auto parsed = parse_s1f12(m);
|
||||
REQUIRE(parsed.has_value());
|
||||
REQUIRE(parsed->size() == 2);
|
||||
CHECK((*parsed)[0].id == 1);
|
||||
CHECK((*parsed)[0].name == "Clock");
|
||||
CHECK((*parsed)[0].units == "sec");
|
||||
CHECK((*parsed)[1].id == 2);
|
||||
CHECK((*parsed)[1].name == "Power");
|
||||
}
|
||||
|
||||
TEST_CASE("S2F15 round-trip preserves ECID-value pairs") {
|
||||
std::vector<std::pair<uint32_t, s2::Item>> sets = {
|
||||
{10, s2::Item::u4(uint32_t{50})},
|
||||
{11, s2::Item::ascii("YYYYMMDDhhmmsscc")},
|
||||
};
|
||||
auto m = s2f15_ec_send(sets);
|
||||
auto parsed = parse_s2f15(m);
|
||||
REQUIRE(parsed.has_value());
|
||||
REQUIRE(parsed->size() == 2);
|
||||
CHECK((*parsed)[0].first == 10);
|
||||
CHECK((*parsed)[0].second == s2::Item::u4(uint32_t{50}));
|
||||
CHECK((*parsed)[1].first == 11);
|
||||
CHECK((*parsed)[1].second == s2::Item::ascii("YYYYMMDDhhmmsscc"));
|
||||
}
|
||||
|
||||
TEST_CASE("S2F16 EAC ack round-trip") {
|
||||
auto m = s2f16_ec_ack(EquipmentAck::Denied_OutOfRange);
|
||||
auto byte = ack_byte(m);
|
||||
REQUIRE(byte.has_value());
|
||||
CHECK(*byte == static_cast<uint8_t>(EquipmentAck::Denied_OutOfRange));
|
||||
}
|
||||
|
||||
TEST_CASE("S2F18 carries 16-char time string") {
|
||||
auto m = s2f18_date_time_data("2026052812345678");
|
||||
auto t = parse_s2f18(m);
|
||||
REQUIRE(t.has_value());
|
||||
CHECK(*t == "2026052812345678");
|
||||
}
|
||||
|
||||
TEST_CASE("S2F41 round-trip with parameters") {
|
||||
std::vector<EquipmentDataModel::CommandParam> params = {
|
||||
{"LOTID", s2::Item::ascii("LOT-42")},
|
||||
{"PPID", s2::Item::ascii("RECIPE-A")},
|
||||
};
|
||||
auto m = s2f41_host_command("START", params);
|
||||
auto parsed = parse_s2f41(m);
|
||||
REQUIRE(parsed.has_value());
|
||||
CHECK(parsed->rcmd == "START");
|
||||
REQUIRE(parsed->params.size() == 2);
|
||||
CHECK(parsed->params[0].name == "LOTID");
|
||||
CHECK(parsed->params[0].value == s2::Item::ascii("LOT-42"));
|
||||
CHECK(parsed->params[1].name == "PPID");
|
||||
}
|
||||
|
||||
TEST_CASE("S2F42 round-trip with HCACK and CPACKs") {
|
||||
auto m = s2f42_host_command_ack(HostCmdAck::ParameterInvalid,
|
||||
{{"LOTID", 0}, {"PPID", 3}});
|
||||
auto parsed = parse_s2f42(m);
|
||||
REQUIRE(parsed.has_value());
|
||||
CHECK(parsed->hcack == HostCmdAck::ParameterInvalid);
|
||||
REQUIRE(parsed->cpacks.size() == 2);
|
||||
CHECK(parsed->cpacks[0].first == "LOTID");
|
||||
CHECK(parsed->cpacks[0].second == 0);
|
||||
CHECK(parsed->cpacks[1].first == "PPID");
|
||||
CHECK(parsed->cpacks[1].second == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("S2F42 no-params variant") {
|
||||
auto m = s2f42_host_command_ack(HostCmdAck::Accept);
|
||||
auto parsed = parse_s2f42(m);
|
||||
REQUIRE(parsed.has_value());
|
||||
CHECK(parsed->hcack == HostCmdAck::Accept);
|
||||
CHECK(parsed->cpacks.empty());
|
||||
}
|
||||
|
||||
TEST_CASE("S10F3 terminal display round-trip") {
|
||||
auto m = s10f3_terminal_display_single(1, "ALARM: chiller temperature high");
|
||||
auto parsed = parse_terminal_display(m);
|
||||
REQUIRE(parsed.has_value());
|
||||
CHECK(parsed->tid == 1);
|
||||
CHECK(parsed->text == "ALARM: chiller temperature high");
|
||||
}
|
||||
|
||||
// ---- Extended GEM message round-trips ------------------------------------
|
||||
|
||||
TEST_CASE("S2F33 define-report round-trip") {
|
||||
auto m = s2f33_define_report(7, {{1000, {1, 2, 3}}, {1001, {4}}});
|
||||
auto parsed = parse_s2f33(m);
|
||||
REQUIRE(parsed.has_value());
|
||||
CHECK(parsed->dataid == 7);
|
||||
REQUIRE(parsed->reports.size() == 2);
|
||||
CHECK(parsed->reports[0].first == 1000);
|
||||
CHECK(parsed->reports[0].second == std::vector<uint32_t>{1, 2, 3});
|
||||
CHECK(parsed->reports[1].first == 1001);
|
||||
CHECK(parsed->reports[1].second == std::vector<uint32_t>{4});
|
||||
}
|
||||
|
||||
TEST_CASE("S2F35 link-event round-trip") {
|
||||
auto m = s2f35_link_event_report(0, {{100, {1000, 1001}}, {200, {1001}}});
|
||||
auto parsed = parse_s2f35(m);
|
||||
REQUIRE(parsed.has_value());
|
||||
CHECK(parsed->dataid == 0);
|
||||
REQUIRE(parsed->links.size() == 2);
|
||||
CHECK(parsed->links[0].first == 100);
|
||||
CHECK(parsed->links[0].second == std::vector<uint32_t>{1000, 1001});
|
||||
}
|
||||
|
||||
TEST_CASE("S2F37 enable-event round-trip") {
|
||||
auto enable = s2f37_enable_event(true, {100, 200});
|
||||
auto disable = s2f37_enable_event(false, {});
|
||||
auto pe = parse_s2f37(enable);
|
||||
auto pd = parse_s2f37(disable);
|
||||
REQUIRE(pe.has_value());
|
||||
REQUIRE(pd.has_value());
|
||||
CHECK(pe->enable);
|
||||
CHECK(pe->ceids == std::vector<uint32_t>{100, 200});
|
||||
CHECK_FALSE(pd->enable);
|
||||
CHECK(pd->ceids.empty());
|
||||
}
|
||||
|
||||
TEST_CASE("S5F1 alarm-report round-trip") {
|
||||
auto m = s5f1_alarm_report(0x84, 7, "Chiller temp high");
|
||||
auto parsed = parse_s5f1(m);
|
||||
REQUIRE(parsed.has_value());
|
||||
CHECK(parsed->alid == 7);
|
||||
CHECK(parsed->altx == "Chiller temp high");
|
||||
CHECK(parsed->alcd == 0x84);
|
||||
CHECK(parsed->is_set());
|
||||
CHECK(parsed->category() == 4);
|
||||
}
|
||||
|
||||
TEST_CASE("S5F3 enable/disable alarm send round-trip") {
|
||||
auto on = s5f3_enable_alarm(true, 42);
|
||||
auto off = s5f3_enable_alarm(false, 42);
|
||||
auto pon = parse_s5f3(on);
|
||||
auto poff = parse_s5f3(off);
|
||||
REQUIRE(pon.has_value());
|
||||
REQUIRE(poff.has_value());
|
||||
CHECK(pon->enable);
|
||||
CHECK_FALSE(poff->enable);
|
||||
CHECK(pon->alid == 42);
|
||||
}
|
||||
|
||||
TEST_CASE("S5F6 alarm list builder marks active alarms with bit 7") {
|
||||
std::vector<Alarm> alarms = {{1, "Door", 1}, {2, "Heater", 4}};
|
||||
auto m = s5f6_list_alarms_data(alarms, [](uint32_t id) { return id == 2; });
|
||||
REQUIRE(m.body.has_value());
|
||||
REQUIRE(m.body->is_list());
|
||||
REQUIRE(m.body->as_list().size() == 2);
|
||||
const auto& row0 = m.body->as_list()[0].as_list();
|
||||
const auto& row1 = m.body->as_list()[1].as_list();
|
||||
CHECK(row0[0].as_bytes()[0] == 0x01); // door inactive: cat 1, bit 7 clear
|
||||
CHECK(row1[0].as_bytes()[0] == 0x84); // heater active: cat 4, bit 7 set
|
||||
}
|
||||
|
||||
TEST_CASE("S6F11 event-report round-trip preserves DATAID, CEID, reports") {
|
||||
std::vector<ReportData> reports = {
|
||||
{1000, {s2::Item::ascii("OnlineRemote")}},
|
||||
{1001, {s2::Item::u4(uint32_t{42}), s2::Item::ascii("hello")}},
|
||||
};
|
||||
auto m = s6f11_event_report(7, 100, reports);
|
||||
auto parsed = parse_s6f11(m);
|
||||
REQUIRE(parsed.has_value());
|
||||
CHECK(parsed->dataid == 7);
|
||||
CHECK(parsed->ceid == 100);
|
||||
REQUIRE(parsed->reports.size() == 2);
|
||||
CHECK(parsed->reports[0].rptid == 1000);
|
||||
CHECK(parsed->reports[0].values[0] == s2::Item::ascii("OnlineRemote"));
|
||||
CHECK(parsed->reports[1].rptid == 1001);
|
||||
CHECK(parsed->reports[1].values[0] == s2::Item::u4(uint32_t{42}));
|
||||
}
|
||||
|
||||
TEST_CASE("S7F3 process-program send round-trip") {
|
||||
auto m = s7f3_process_program_send("RECIPE-A", "step1\nstep2\n");
|
||||
auto parsed = parse_s7f3(m);
|
||||
REQUIRE(parsed.has_value());
|
||||
CHECK(parsed->ppid == "RECIPE-A");
|
||||
CHECK(parsed->ppbody == "step1\nstep2\n");
|
||||
}
|
||||
|
||||
TEST_CASE("S7F19 / S7F20 EPPD list") {
|
||||
auto req = s7f19_current_eppd_request();
|
||||
CHECK(req.stream == 7);
|
||||
CHECK(req.function == 19);
|
||||
CHECK(req.reply_expected);
|
||||
CHECK_FALSE(req.body.has_value());
|
||||
|
||||
auto data = s7f20_current_eppd_data({"RECIPE-A", "RECIPE-B"});
|
||||
auto parsed = parse_s7f20(data);
|
||||
REQUIRE(parsed.has_value());
|
||||
CHECK(*parsed == std::vector<std::string>{"RECIPE-A", "RECIPE-B"});
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
#include "secsgem/secs2/codec.hpp"
|
||||
#include "secsgem/secs2/item.hpp"
|
||||
#include "secsgem/secs2/message.hpp"
|
||||
|
||||
using namespace secsgem::secs2;
|
||||
|
||||
TEST_CASE("encode known byte layouts") {
|
||||
CHECK(encode(Item::ascii("ABC")) ==
|
||||
std::vector<uint8_t>{0x41, 0x03, 'A', 'B', 'C'});
|
||||
CHECK(encode(Item::u1(uint8_t{255})) ==
|
||||
std::vector<uint8_t>{0xA5, 0x01, 0xFF});
|
||||
CHECK(encode(Item::u4(uint32_t{0x01020304})) ==
|
||||
std::vector<uint8_t>{0xB1, 0x04, 0x01, 0x02, 0x03, 0x04});
|
||||
CHECK(encode(Item::boolean(true)) ==
|
||||
std::vector<uint8_t>{0x25, 0x01, 0x01});
|
||||
CHECK(encode(Item::binary({0xDE, 0xAD})) ==
|
||||
std::vector<uint8_t>{0x21, 0x02, 0xDE, 0xAD});
|
||||
|
||||
// L [2] <A "MD"> <U2 258>
|
||||
CHECK(encode(Item::list({Item::ascii("MD"), Item::u2(uint16_t{258})})) ==
|
||||
std::vector<uint8_t>{0x01, 0x02, 0x41, 0x02, 'M', 'D', 0xA9, 0x02, 0x01, 0x02});
|
||||
}
|
||||
|
||||
TEST_CASE("round-trip scalar items") {
|
||||
auto rt = [](const Item& i) { return decode(encode(i)); };
|
||||
|
||||
CHECK(rt(Item::ascii("hello world")) == Item::ascii("hello world"));
|
||||
CHECK(rt(Item::ascii("")) == Item::ascii(""));
|
||||
CHECK(rt(Item::u1(uint8_t{0})) == Item::u1(uint8_t{0}));
|
||||
CHECK(rt(Item::u2(uint16_t{0xBEEF})) == Item::u2(uint16_t{0xBEEF}));
|
||||
CHECK(rt(Item::u4(uint32_t{0xDEADBEEF})) == Item::u4(uint32_t{0xDEADBEEF}));
|
||||
CHECK(rt(Item::u8(uint64_t{0x0102030405060708ULL})) == Item::u8(uint64_t{0x0102030405060708ULL}));
|
||||
CHECK(rt(Item::i1(int8_t{-5})) == Item::i1(int8_t{-5}));
|
||||
CHECK(rt(Item::i2(int16_t{-1234})) == Item::i2(int16_t{-1234}));
|
||||
CHECK(rt(Item::i4(int32_t{-123456})) == Item::i4(int32_t{-123456}));
|
||||
CHECK(rt(Item::i8(int64_t{-9000000000LL})) == Item::i8(int64_t{-9000000000LL}));
|
||||
CHECK(rt(Item::binary({1, 2, 3, 255, 0})) == Item::binary({1, 2, 3, 255, 0}));
|
||||
CHECK(rt(Item::boolean({1, 0, 1})) == Item::boolean({1, 0, 1}));
|
||||
}
|
||||
|
||||
TEST_CASE("round-trip floating point preserves bit pattern") {
|
||||
CHECK(decode(encode(Item::f4(3.14159f))) == Item::f4(3.14159f));
|
||||
CHECK(decode(encode(Item::f8(2.718281828459045))) == Item::f8(2.718281828459045));
|
||||
CHECK(decode(encode(Item::f4({1.0f, -2.5f, 0.0f}))) == Item::f4({1.0f, -2.5f, 0.0f}));
|
||||
}
|
||||
|
||||
TEST_CASE("round-trip nested lists") {
|
||||
Item nested = Item::list({
|
||||
Item::ascii("EQUIP-01"),
|
||||
Item::list({Item::u4(uint32_t{12}), Item::u4(uint32_t{34})}),
|
||||
Item::list({}), // empty list
|
||||
Item::binary({0xFF}),
|
||||
});
|
||||
CHECK(decode(encode(nested)) == nested);
|
||||
}
|
||||
|
||||
TEST_CASE("multi-element numeric arrays") {
|
||||
auto v = Item::u2({1, 2, 3, 4, 5});
|
||||
CHECK(v.size() == 5);
|
||||
CHECK(decode(encode(v)) == v);
|
||||
}
|
||||
|
||||
TEST_CASE("decode rejects truncated input") {
|
||||
CHECK_THROWS_AS(decode(std::vector<uint8_t>{0x41, 0x03, 'A'}), CodecError); // ascii len 3, 1 byte
|
||||
CHECK_THROWS_AS(decode(std::vector<uint8_t>{0x41}), CodecError); // missing length byte
|
||||
CHECK_THROWS_AS(decode(std::vector<uint8_t>{}), CodecError); // empty
|
||||
}
|
||||
|
||||
TEST_CASE("decode rejects trailing bytes") {
|
||||
CHECK_THROWS_AS(decode(std::vector<uint8_t>{0xA5, 0x01, 0xFF, 0x00}), CodecError);
|
||||
}
|
||||
|
||||
TEST_CASE("message body round-trip") {
|
||||
Item body = Item::list({Item::ascii("MDLN-1"), Item::ascii("1.0.0")});
|
||||
Message m(1, 2, false, body);
|
||||
|
||||
auto bytes = m.encode_body();
|
||||
Message decoded = Message::from_body(1, 2, false, bytes);
|
||||
|
||||
REQUIRE(decoded.body.has_value());
|
||||
CHECK(*decoded.body == body);
|
||||
CHECK(decoded.stream == 1);
|
||||
CHECK(decoded.function == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("empty message body") {
|
||||
Message m(1, 1, true); // S1F1 W, header-only
|
||||
CHECK(m.encode_body().empty());
|
||||
|
||||
Message decoded = Message::from_body(1, 1, true, {});
|
||||
CHECK_FALSE(decoded.body.has_value());
|
||||
}
|
||||
|
||||
TEST_CASE("SML rendering") {
|
||||
Item body = Item::list({Item::ascii("MDLN"), Item::u4(uint32_t{42})});
|
||||
CHECK(to_sml(body) == "<L [2] <A \"MDLN\" > <U4 42 > >");
|
||||
}
|
||||
Reference in New Issue
Block a user