b871cd9da2
Move equipment capabilities and the E30 control state machine out of C++
code and into YAML data files; introduce a Router for SECS dispatch;
consolidate small files.
Behavioural changes: none. Demo identical (15 SxFy transactions +
3 equipment-initiated primaries), 67 test cases / 384 assertions still
all green. Structural changes only.
Why
---
The previous server.cpp held the equipment data dictionary (3 SVIDs,
2 ECIDs, 3 CEIDs, 2 alarms, 2 recipes, 4 host commands) as imperative
C++ in a 50-line `populate()` function, and routed inbound messages
through a 150-line if-ladder. Adding a new SVID required a recompile.
Adding a new state transition required editing two switch statements
(`operator_*` and `on_host_request_*`). The control state machine's
behavioural rules were spread across imperative code in two methods.
This is exactly what implementation_plan.md calls out as the wrong
shape: behavioural rules should live in versioned data, and every
runtime/test/analyzer should read from that data rather than re-encode
it. This commit starts that move.
What's new
----------
data/equipment.yaml
Equipment data dictionary. Declarative SVIDs / ECIDs / CEIDs /
alarms / recipes / host commands. Host commands carry their HCACK
ack code plus optional `emit_ceid` and `set_alarm` side-effects.
Adding a new SVID or command is a YAML edit, no recompile.
data/control_state.yaml
The E30 §6.2 control state transition table as data. Each row is
(from, on) -> (to [, then] [, ack]). `then` chains an auto-advance
through the transient AttemptOnline state. The previous
imperative switch is gone.
include/secsgem/config/loader.hpp + src/config/loader.cpp
yaml-cpp-backed loader. `load_control_state(path)` returns a
ControlTransitionTable + initial state; `load_equipment(path, model)`
populates the EquipmentDataModel and returns the device descriptor
(id, MDLN, SOFTREV, optional auto-emit CEID). Surfaces config
errors with file path + field name via ConfigError.
include/secsgem/gem/router.hpp (header-only)
Small (stream, function) -> handler map. Server registers all
handlers once at startup, then the Connection's message handler is
just `router.dispatch(msg)`. Unhandled primaries with W set get
SxF0 by default. Replaces the if-ladder in secs_server.cpp.
include/secsgem/gem/control_state.hpp + .cpp
ControlTransitionTable is the new pure data type. ControlStateMachine
is now a thin engine over the table: `fire(event)` looks up the row,
optionally transitions, optionally chains a `then` transition, returns
the ack code. Behaviour rules no longer live in C++ switches.
The default in-code table matches data/control_state.yaml row for row;
tests rely on it so they don't need the YAML file.
include/secsgem/gem/data_model.hpp + .cpp
`register_command(rcmd, CommandSpec)` replaces the function-handler
signature. CommandSpec = (HostCmdAck, optional emit_ceid, optional
set_alarm). `dispatch_command` returns a CommandResult so the server
can fire the side-effects after S2F42 is sent.
apps/secs_server.cpp
No populate(), no if-ladder. Loads equipment.yaml + control_state.yaml
at startup (clean error on bad config), wires the Router once,
delegates dispatch. Sm change handler reads emit_on_control_change
from the YAML. Welcome S10F3 removed for parity with config (a future
YAML rule could re-introduce it declaratively).
tests/test_loader.cpp (new)
Verifies the YAML loader produces the same shape as the in-code
default table, and that equipment.yaml populates every section
(SVIDs/ECIDs/CEIDs/alarms/recipes/commands). SECSGEM_DATA_DIR
CMake define points at ${CMAKE_SOURCE_DIR}/data so tests don't
depend on cwd.
CMakeLists.txt, Dockerfile
find_package(yaml-cpp) and link. libyaml-cpp-dev added to the
Ubuntu base image (yaml-cpp 0.8 ships the modern target name).
File consolidation
------------------
Five small files removed; their content lives in fewer headers:
- secs2/item.cpp -> inline in secs2/item.hpp
- secs2/message.cpp -> inline in secs2/message.hpp
- hsms/types.hpp -> merged into hsms/header.hpp
- hsms/frame.hpp -> merged into hsms/header.hpp
- hsms/frame.cpp -> merged into hsms/header.cpp
hsms/header.hpp is now "the HSMS wire format" in one place: SType + status
enums + Timers + Header + Frame + constants. All includers updated.
Net effect
----------
Before: equipment data dictionary lived in 50 lines of imperative
populate() inside secs_server.cpp; dispatch in a 20-branch if-ladder.
After: equipment data dictionary lives in 47 lines of YAML; dispatch
is a Router built once. Adding a new capability is now a YAML edit
in the common case.
Test count up to 67 cases / 384 assertions (+4 cases / +106 assertions)
covering the loader and the new table-driven SM paths.
What's NOT changed
------------------
The per-SxFy reply construction still lives in C++ (each message has a
unique body shape). Moving those into YAML/JSON message-shape
definitions is the next refactor step but requires a generic typed
encoder/decoder driven by shape descriptors; out of scope here.
Spooling, the S9 error stream, S1F19/F20, and the other gaps in
COMPLIANCE.md remain unchanged.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
306 lines
12 KiB
C++
306 lines
12 KiB
C++
// Passive SECS/GEM equipment. Capabilities (SVIDs, ECIDs, CEIDs, alarms,
|
|
// recipes, host commands) come from data/equipment.yaml; the E30 control
|
|
// state machine comes from data/control_state.yaml. Dispatch is a Router
|
|
// table. No imperative if-ladder; no in-code device data dictionary.
|
|
|
|
#include <asio.hpp>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "secsgem/config/loader.hpp"
|
|
#include "secsgem/endpoint.hpp"
|
|
#include "secsgem/gem/control_state.hpp"
|
|
#include "secsgem/gem/data_model.hpp"
|
|
#include "secsgem/gem/messages.hpp"
|
|
#include "secsgem/gem/router.hpp"
|
|
#include "secsgem/secs2/message.hpp"
|
|
|
|
using namespace secsgem;
|
|
namespace s2 = secsgem::secs2;
|
|
namespace gem = secsgem::gem;
|
|
|
|
namespace {
|
|
|
|
std::string arg(int argc, char** argv, const std::string& key, const std::string& def) {
|
|
for (int i = 1; i + 1 < argc; ++i)
|
|
if (key == argv[i]) return argv[i + 1];
|
|
return def;
|
|
}
|
|
|
|
constexpr uint32_t kSvidControlState = 1;
|
|
constexpr uint32_t kSvidClock = 2;
|
|
|
|
void refresh(gem::EquipmentDataModel& m, const gem::ControlStateMachine& sm) {
|
|
m.set_status_value(kSvidControlState, s2::Item::ascii(gem::control_state_name(sm.state())));
|
|
m.set_status_value(kSvidClock, s2::Item::ascii(m.current_time_string()));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
const auto port = static_cast<uint16_t>(std::stoi(arg(argc, argv, "--port", "5000")));
|
|
const auto equipment_yaml = arg(argc, argv, "--config", "/app/data/equipment.yaml");
|
|
const auto state_yaml = arg(argc, argv, "--state-table", "/app/data/control_state.yaml");
|
|
|
|
auto logfn = [](const std::string& m) { std::cout << "[equip] " << m << std::endl; };
|
|
|
|
auto model = std::make_shared<gem::EquipmentDataModel>();
|
|
config::EquipmentDescriptor desc;
|
|
config::ControlStateConfig sm_cfg;
|
|
try {
|
|
desc = config::load_equipment(equipment_yaml, *model);
|
|
sm_cfg = config::load_control_state(state_yaml);
|
|
} catch (const std::exception& e) {
|
|
std::cerr << "[equip] config error: " << e.what() << std::endl;
|
|
return 1;
|
|
}
|
|
logfn("loaded " + std::to_string(model->all_status_variables().size()) + " SVIDs, " +
|
|
std::to_string(model->all_equipment_constants().size()) + " ECIDs, " +
|
|
std::to_string(model->all_events().size()) + " CEIDs, " +
|
|
std::to_string(model->all_alarms().size()) + " alarms");
|
|
|
|
auto sm = std::make_shared<gem::ControlStateMachine>(sm_cfg.table, sm_cfg.initial);
|
|
|
|
asio::io_context io;
|
|
Server::Config server_cfg{port, desc.device_id, {}};
|
|
Server server(io, server_cfg);
|
|
server.on_log(logfn);
|
|
|
|
auto active_conn = std::make_shared<std::weak_ptr<Connection>>();
|
|
|
|
auto emit_event = [&io, active_conn, model, logfn](uint32_t ceid) {
|
|
asio::post(io, [active_conn, model, logfn, ceid]() {
|
|
auto conn = active_conn->lock();
|
|
if (!conn) return;
|
|
if (!model->is_event_enabled(ceid)) {
|
|
logfn("CEID " + std::to_string(ceid) + " not enabled; suppressed");
|
|
return;
|
|
}
|
|
auto reports = model->compose_reports_for(ceid);
|
|
logfn("emit S6F11 CEID=" + std::to_string(ceid) + " (" +
|
|
std::to_string(reports.size()) + " reports)");
|
|
conn->send_request(gem::s6f11_event_report(0, ceid, reports),
|
|
[](std::error_code, const s2::Message&) {});
|
|
});
|
|
};
|
|
|
|
auto emit_alarm_set = [&io, active_conn, model, logfn, emit_event](uint32_t alid) {
|
|
asio::post(io, [active_conn, model, logfn, emit_event, alid]() {
|
|
auto conn = active_conn->lock();
|
|
if (!conn) return;
|
|
auto alarm = model->alarm(alid);
|
|
if (!alarm) return;
|
|
auto alcd = model->alarm_set(alid);
|
|
if (!alcd) return;
|
|
if (model->alarm_enabled(alid)) {
|
|
logfn("emit S5F1 alarm set ALID=" + std::to_string(alid));
|
|
conn->send_request(gem::s5f1_alarm_report(*alcd, alid, alarm->text),
|
|
[](std::error_code, const s2::Message&) {});
|
|
} else {
|
|
logfn("alarm " + std::to_string(alid) + " not enabled; suppressed");
|
|
}
|
|
// E30: an AlarmSetEvent CEID also fires (if linked + enabled).
|
|
});
|
|
};
|
|
|
|
sm->set_state_change_handler(
|
|
[logfn, emit_event, desc](gem::ControlState from, gem::ControlState to, gem::ControlEvent ev) {
|
|
logfn(std::string("control: ") + gem::control_state_name(from) + " -> " +
|
|
gem::control_state_name(to) + " (" + gem::control_event_name(ev) + ")");
|
|
if (desc.emit_on_control_change) emit_event(*desc.emit_on_control_change);
|
|
});
|
|
|
|
// ---- Build the SECS dispatch table once -------------------------------
|
|
gem::Router router;
|
|
|
|
router.on(1, 1, [desc, logfn](const s2::Message&) {
|
|
logfn("S1F1 -> S1F2");
|
|
return gem::s1f2_on_line_data(desc.model_name, desc.software_rev);
|
|
});
|
|
router.on(1, 3, [model, sm, logfn](const s2::Message& msg) -> std::optional<s2::Message> {
|
|
refresh(*model, *sm);
|
|
auto svids = gem::parse_s1f3(msg);
|
|
if (!svids) return s2::Message(1, 0, false);
|
|
std::vector<std::optional<s2::Item>> values;
|
|
if (svids->empty()) {
|
|
for (const auto& sv : model->all_status_variables()) values.push_back(sv.value);
|
|
} else {
|
|
for (auto id : *svids) {
|
|
auto sv = model->status_variable(id);
|
|
values.push_back(sv ? std::optional<s2::Item>(sv->value) : std::nullopt);
|
|
}
|
|
}
|
|
logfn("S1F3 -> S1F4 (" + std::to_string(values.size()) + " values)");
|
|
return gem::s1f4_selected_status_data(values);
|
|
});
|
|
router.on(1, 11, [model, logfn](const s2::Message&) {
|
|
logfn("S1F11 -> S1F12 (namelist)");
|
|
return gem::s1f12_status_namelist_data(model->all_status_variables());
|
|
});
|
|
router.on(1, 13, [desc, logfn](const s2::Message&) {
|
|
logfn("S1F13 -> S1F14");
|
|
return gem::s1f14_establish_comms_ack(gem::CommAck::Accept, desc.model_name, desc.software_rev);
|
|
});
|
|
router.on(1, 15, [sm, logfn](const s2::Message&) {
|
|
auto ack = sm->on_host_request_offline();
|
|
logfn("S1F15 -> S1F16 OFLACK=" + std::to_string(static_cast<int>(ack)));
|
|
return gem::s1f16_offline_ack(ack);
|
|
});
|
|
router.on(1, 17, [sm, logfn](const s2::Message&) {
|
|
auto ack = sm->on_host_request_online();
|
|
logfn("S1F17 -> S1F18 ONLACK=" + std::to_string(static_cast<int>(ack)));
|
|
return gem::s1f18_online_ack(ack);
|
|
});
|
|
|
|
router.on(2, 13, [model, logfn](const s2::Message& msg) -> std::optional<s2::Message> {
|
|
auto ids = gem::parse_u4_list_body(msg);
|
|
if (!ids) return s2::Message(2, 0, false);
|
|
std::vector<s2::Item> values;
|
|
for (auto id : *ids) {
|
|
auto ec = model->equipment_constant(id);
|
|
values.push_back(ec ? ec->value : s2::Item::list({}));
|
|
}
|
|
logfn("S2F13 -> S2F14 (" + std::to_string(values.size()) + " values)");
|
|
return gem::s2f14_ec_data(values);
|
|
});
|
|
router.on(2, 15, [model, logfn](const s2::Message& msg) {
|
|
auto sets = gem::parse_s2f15(msg);
|
|
auto eac = gem::EquipmentAck::Accept;
|
|
if (!sets) eac = gem::EquipmentAck::Denied_OutOfRange;
|
|
else
|
|
for (const auto& [id, val] : *sets) {
|
|
auto r = model->set_equipment_constant_value(id, val);
|
|
if (r != gem::EquipmentAck::Accept) eac = r;
|
|
}
|
|
logfn("S2F15 -> S2F16 EAC=" + std::to_string(static_cast<int>(eac)));
|
|
return gem::s2f16_ec_ack(eac);
|
|
});
|
|
router.on(2, 17, [model, logfn](const s2::Message&) {
|
|
logfn("S2F17 -> S2F18 (clock)");
|
|
return gem::s2f18_date_time_data(model->current_time_string());
|
|
});
|
|
router.on(2, 29, [model, logfn](const s2::Message& msg) {
|
|
auto ids = gem::parse_u4_list_body(msg);
|
|
std::vector<gem::EquipmentConstant> ecs;
|
|
if (ids && ids->empty()) ecs = model->all_equipment_constants();
|
|
else if (ids)
|
|
for (auto id : *ids) {
|
|
auto ec = model->equipment_constant(id);
|
|
if (ec) ecs.push_back(*ec);
|
|
}
|
|
logfn("S2F29 -> S2F30 (" + std::to_string(ecs.size()) + " ECs)");
|
|
return gem::s2f30_ec_namelist_data(ecs);
|
|
});
|
|
router.on(2, 31, [model, logfn](const s2::Message& msg) {
|
|
auto t = gem::parse_s2f31(msg);
|
|
auto ack = t ? model->set_time_string(*t) : gem::TimeAck::Error;
|
|
logfn("S2F31 -> S2F32 TIACK=" + std::to_string(static_cast<int>(ack)));
|
|
return gem::s2f32_date_time_ack(ack);
|
|
});
|
|
router.on(2, 33, [model, logfn](const s2::Message& msg) {
|
|
auto req = gem::parse_s2f33(msg);
|
|
auto ack = req ? model->define_reports(req->reports)
|
|
: gem::DefineReportAck::InvalidFormat;
|
|
logfn("S2F33 -> S2F34 DRACK=" + std::to_string(static_cast<int>(ack)));
|
|
return gem::s2f34_define_report_ack(ack);
|
|
});
|
|
router.on(2, 35, [model, logfn](const s2::Message& msg) {
|
|
auto req = gem::parse_s2f35(msg);
|
|
auto ack = req ? model->link_event_reports(req->links) : gem::LinkEventAck::InvalidFormat;
|
|
logfn("S2F35 -> S2F36 LRACK=" + std::to_string(static_cast<int>(ack)));
|
|
return gem::s2f36_link_event_report_ack(ack);
|
|
});
|
|
router.on(2, 37, [model, logfn](const s2::Message& msg) {
|
|
auto req = gem::parse_s2f37(msg);
|
|
auto ack = req ? model->enable_events(req->enable, req->ceids)
|
|
: gem::EnableEventAck::UnknownCeid;
|
|
logfn(std::string("S2F37 ") + (req && req->enable ? "enable" : "disable") +
|
|
" -> S2F38 ERACK=" + std::to_string(static_cast<int>(ack)));
|
|
return gem::s2f38_enable_event_ack(ack);
|
|
});
|
|
router.on(2, 41, [model, logfn, emit_event, emit_alarm_set](const s2::Message& msg) {
|
|
auto cmd = gem::parse_s2f41(msg);
|
|
if (!cmd) return gem::s2f42_host_command_ack(gem::HostCmdAck::ParameterInvalid);
|
|
auto result = model->dispatch_command(cmd->rcmd, cmd->params);
|
|
logfn("S2F41 RCMD=" + cmd->rcmd + " -> S2F42 HCACK=" +
|
|
std::to_string(static_cast<int>(result.ack)));
|
|
if (result.ack == gem::HostCmdAck::Accept) {
|
|
if (result.emit_ceid) emit_event(*result.emit_ceid);
|
|
if (result.set_alarm) emit_alarm_set(*result.set_alarm);
|
|
}
|
|
return gem::s2f42_host_command_ack(result.ack);
|
|
});
|
|
|
|
router.on(5, 3, [model, logfn](const s2::Message& msg) {
|
|
auto req = gem::parse_s5f3(msg);
|
|
auto ack = req ? model->set_alarm_enabled(req->alid, req->enable)
|
|
: gem::AlarmAck::Error;
|
|
logfn(std::string("S5F3 -> S5F4 ACKC5=") + std::to_string(static_cast<int>(ack)));
|
|
return gem::s5f4_enable_alarm_ack(ack);
|
|
});
|
|
router.on(5, 5, [model, logfn](const s2::Message& msg) {
|
|
auto ids = gem::parse_u4_list_body(msg);
|
|
std::vector<gem::Alarm> alarms;
|
|
if (ids && ids->empty()) alarms = model->all_alarms();
|
|
else if (ids)
|
|
for (auto id : *ids) {
|
|
auto a = model->alarm(id);
|
|
if (a) alarms.push_back(*a);
|
|
}
|
|
logfn("S5F5 -> S5F6 (" + std::to_string(alarms.size()) + " alarms)");
|
|
return gem::s5f6_list_alarms_data(
|
|
alarms, [model](uint32_t id) { return model->alarm_active(id); });
|
|
});
|
|
|
|
router.on(7, 3, [model, logfn](const s2::Message& msg) {
|
|
auto pp = gem::parse_s7f3(msg);
|
|
if (!pp) return gem::s7f4_process_program_ack(gem::ProcessProgramAck::LengthError);
|
|
model->add_process_program(pp->ppid, pp->ppbody);
|
|
logfn("S7F3 PPID=" + pp->ppid + " -> S7F4 (Accept)");
|
|
return gem::s7f4_process_program_ack(gem::ProcessProgramAck::Accept);
|
|
});
|
|
router.on(7, 5, [model, logfn](const s2::Message& msg) {
|
|
auto ppid = gem::parse_s7f5(msg);
|
|
if (!ppid) return gem::s7f6_process_program_data("", "");
|
|
auto body = model->process_program(*ppid);
|
|
logfn("S7F5 PPID=" + *ppid + " -> S7F6");
|
|
return gem::s7f6_process_program_data(*ppid, body ? *body : "");
|
|
});
|
|
router.on(7, 19, [model, logfn](const s2::Message&) {
|
|
auto list = model->process_program_list();
|
|
logfn("S7F19 -> S7F20 (" + std::to_string(list.size()) + " PPIDs)");
|
|
return gem::s7f20_current_eppd_data(list);
|
|
});
|
|
|
|
router.on(10, 1, [logfn](const s2::Message& msg) {
|
|
auto td = gem::parse_terminal_display(msg);
|
|
if (td) logfn("TERMINAL[" + std::to_string(td->tid) + "] " + td->text);
|
|
return gem::s10f2_terminal_display_ack(gem::TerminalAck::Accepted);
|
|
});
|
|
|
|
logfn("registered " + std::to_string(router.size()) + " (stream,function) handlers");
|
|
|
|
// ---- Wire the router into accepted connections -----------------------
|
|
server.on_connection([sm, model, logfn, active_conn, &router, desc](
|
|
std::shared_ptr<Connection> conn) {
|
|
*active_conn = conn;
|
|
conn->set_closed_handler([active_conn](const std::string&) { active_conn->reset(); });
|
|
|
|
conn->set_selected_handler([logfn, sm]() {
|
|
logfn(std::string("host is online; control=") + gem::control_state_name(sm->state()));
|
|
});
|
|
|
|
conn->set_message_handler(
|
|
[&router](const s2::Message& msg) { return router.dispatch(msg); });
|
|
});
|
|
|
|
server.start();
|
|
io.run();
|
|
return 0;
|
|
}
|