Table/YAML-driven refactor (Layer 1 start)
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>
This commit is contained in:
+215
-285
@@ -1,7 +1,7 @@
|
||||
// Passive SECS/GEM equipment: a small simulated tool. Drives the E30 control
|
||||
// state machine, owns an in-memory data dictionary (SVIDs, DVIDs, ECIDs,
|
||||
// CEIDs, alarms, recipes), answers the GEM SxFy core, and emits S6F11 +
|
||||
// S5F1 primaries when host-subscribed events fire.
|
||||
// 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>
|
||||
@@ -10,14 +10,14 @@
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#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/secs2/item.hpp"
|
||||
#include "secsgem/gem/router.hpp"
|
||||
#include "secsgem/secs2/message.hpp"
|
||||
|
||||
using namespace secsgem;
|
||||
@@ -32,50 +32,10 @@ std::string arg(int argc, char** argv, const std::string& key, const std::string
|
||||
return def;
|
||||
}
|
||||
|
||||
constexpr const char* kModelName = "SECSGEM-SIM";
|
||||
constexpr const char* kSoftRev = "0.1.0";
|
||||
constexpr uint32_t kSvidControlState = 1;
|
||||
constexpr uint32_t kSvidClock = 2;
|
||||
|
||||
// SVIDs
|
||||
constexpr uint32_t kSvidControlState = 1;
|
||||
constexpr uint32_t kSvidClock = 2;
|
||||
constexpr uint32_t kSvidEventsEnabled = 3;
|
||||
|
||||
// ECIDs
|
||||
constexpr uint32_t kEcidTimeFormat = 10;
|
||||
constexpr uint32_t kEcidEstablishTimeout = 11;
|
||||
|
||||
// CEIDs
|
||||
constexpr uint32_t kCeidControlStateChanged = 100;
|
||||
constexpr uint32_t kCeidAlarmSetEvent = 200;
|
||||
constexpr uint32_t kCeidProcessStarted = 300;
|
||||
|
||||
// ALIDs
|
||||
constexpr uint32_t kAlarmChillerTempHigh = 1;
|
||||
constexpr uint32_t kAlarmDoorOpen = 2;
|
||||
|
||||
void populate(gem::EquipmentDataModel& m, const gem::ControlStateMachine& sm) {
|
||||
m.add_status_variable(
|
||||
{kSvidControlState, "ControlState", "", s2::Item::ascii(gem::control_state_name(sm.state()))});
|
||||
m.add_status_variable({kSvidClock, "Clock", "", s2::Item::ascii(m.current_time_string())});
|
||||
m.add_status_variable({kSvidEventsEnabled, "EventsEnabled", "", s2::Item::boolean(true)});
|
||||
|
||||
m.add_equipment_constant({kEcidTimeFormat, "TimeFormat", "code",
|
||||
s2::Item::u4(uint32_t{1}), s2::Item::u4(uint32_t{1}), "0", "1"});
|
||||
m.add_equipment_constant({kEcidEstablishTimeout, "EstablishCommTimeout", "sec",
|
||||
s2::Item::u4(uint32_t{10}), s2::Item::u4(uint32_t{10}), "1", "60"});
|
||||
|
||||
m.register_event({kCeidControlStateChanged, "ControlStateChanged"});
|
||||
m.register_event({kCeidAlarmSetEvent, "AlarmSetEvent"});
|
||||
m.register_event({kCeidProcessStarted, "ProcessStarted"});
|
||||
|
||||
m.add_alarm({kAlarmChillerTempHigh, "Chiller Temp High", 4});
|
||||
m.add_alarm({kAlarmDoorOpen, "Door Open", 1});
|
||||
|
||||
m.add_process_program("RECIPE-A", "STEP CHAMBER ARGON 30s\nSTEP CHAMBER NITROGEN 60s\nEND");
|
||||
m.add_process_program("RECIPE-B", "STEP HEATER 800C 120s\nEND");
|
||||
}
|
||||
|
||||
void refresh_dynamic_svids(gem::EquipmentDataModel& m, const gem::ControlStateMachine& sm) {
|
||||
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()));
|
||||
}
|
||||
@@ -83,24 +43,36 @@ void refresh_dynamic_svids(gem::EquipmentDataModel& m, const gem::ControlStateMa
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
Server::Config cfg;
|
||||
cfg.port = static_cast<uint16_t>(std::stoi(arg(argc, argv, "--port", "5000")));
|
||||
cfg.device_id = static_cast<uint16_t>(std::stoi(arg(argc, argv, "--device", "0")));
|
||||
|
||||
asio::io_context io;
|
||||
Server server(io, cfg);
|
||||
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; };
|
||||
server.on_log(logfn);
|
||||
|
||||
auto sm = std::make_shared<gem::ControlStateMachine>();
|
||||
auto model = std::make_shared<gem::EquipmentDataModel>();
|
||||
populate(*model, *sm);
|
||||
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>>();
|
||||
|
||||
// Emit a CEID via S6F11 to the currently-active host connection, if any
|
||||
// and if the host has subscribed to that CEID via S2F37.
|
||||
auto emit_event = [&io, active_conn, model, logfn](uint32_t ceid) {
|
||||
asio::post(io, [active_conn, model, logfn, ceid]() {
|
||||
auto conn = active_conn->lock();
|
||||
@@ -113,15 +85,10 @@ int main(int argc, char** argv) {
|
||||
logfn("emit S6F11 CEID=" + std::to_string(ceid) + " (" +
|
||||
std::to_string(reports.size()) + " reports)");
|
||||
conn->send_request(gem::s6f11_event_report(0, ceid, reports),
|
||||
[logfn, ceid](std::error_code ec, const s2::Message&) {
|
||||
if (ec)
|
||||
logfn("CEID " + std::to_string(ceid) + " emit failed: " + ec.message());
|
||||
});
|
||||
[](std::error_code, const s2::Message&) {});
|
||||
});
|
||||
};
|
||||
|
||||
// Equipment-initiated alarm set: emits S5F1 (if alarm enabled) and the
|
||||
// AlarmSetEvent CEID (if event enabled).
|
||||
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();
|
||||
@@ -130,243 +97,206 @@ int main(int argc, char** argv) {
|
||||
if (!alarm) return;
|
||||
auto alcd = model->alarm_set(alid);
|
||||
if (!alcd) return;
|
||||
if (!model->alarm_enabled(alid)) {
|
||||
logfn("alarm " + std::to_string(alid) + " not enabled; suppressed");
|
||||
} else {
|
||||
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");
|
||||
}
|
||||
emit_event(kCeidAlarmSetEvent);
|
||||
// E30: an AlarmSetEvent CEID also fires (if linked + enabled).
|
||||
});
|
||||
};
|
||||
|
||||
sm->set_state_change_handler(
|
||||
[logfn, emit_event](gem::ControlState from, gem::ControlState to, gem::ControlEvent ev) {
|
||||
[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) + ")");
|
||||
emit_event(kCeidControlStateChanged);
|
||||
if (desc.emit_on_control_change) emit_event(*desc.emit_on_control_change);
|
||||
});
|
||||
|
||||
server.on_connection([sm, model, logfn, active_conn, emit_event,
|
||||
emit_alarm_set](std::shared_ptr<Connection> conn) {
|
||||
*active_conn = conn;
|
||||
// ---- Build the SECS dispatch table once -------------------------------
|
||||
gem::Router router;
|
||||
|
||||
conn->set_closed_handler([active_conn, logfn](const std::string&) {
|
||||
active_conn->reset();
|
||||
});
|
||||
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()));
|
||||
});
|
||||
|
||||
// Host commands are registered here so they can capture emit_event /
|
||||
// emit_alarm_set without static globals.
|
||||
model->register_command("START", [logfn, emit_event](const auto& params) {
|
||||
logfn("RCMD START (" + std::to_string(params.size()) + " params)");
|
||||
emit_event(kCeidProcessStarted);
|
||||
return gem::HostCmdAck::Accept;
|
||||
});
|
||||
model->register_command("STOP",
|
||||
[](const auto&) { return gem::HostCmdAck::Accept; });
|
||||
model->register_command("PAUSE",
|
||||
[](const auto&) { return gem::HostCmdAck::CannotDoNow; });
|
||||
model->register_command("FAULT", [logfn, emit_alarm_set](const auto&) {
|
||||
logfn("RCMD FAULT triggers alarm 1");
|
||||
emit_alarm_set(kAlarmChillerTempHigh);
|
||||
return gem::HostCmdAck::Accept;
|
||||
});
|
||||
|
||||
conn->set_message_handler([sm, model, logfn](const s2::Message& msg)
|
||||
-> std::optional<s2::Message> {
|
||||
const uint8_t s = msg.stream, f = msg.function;
|
||||
|
||||
// ---- S1: equipment status ----------------------------------------
|
||||
if (s == 1 && f == 1) {
|
||||
logfn("S1F1; replying S1F2");
|
||||
return gem::s1f2_on_line_data(kModelName, kSoftRev);
|
||||
}
|
||||
if (s == 1 && f == 3) {
|
||||
refresh_dynamic_svids(*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; replying S1F4 (" + std::to_string(values.size()) + " values)");
|
||||
return gem::s1f4_selected_status_data(values);
|
||||
}
|
||||
if (s == 1 && f == 11) {
|
||||
logfn("S1F11; replying S1F12 (status namelist)");
|
||||
return gem::s1f12_status_namelist_data(model->all_status_variables());
|
||||
}
|
||||
if (s == 1 && f == 13) {
|
||||
logfn("S1F13; replying S1F14 (COMMACK=0)");
|
||||
return gem::s1f14_establish_comms_ack(gem::CommAck::Accept, kModelName, kSoftRev);
|
||||
}
|
||||
if (s == 1 && f == 15) {
|
||||
auto ack = sm->on_host_request_offline();
|
||||
logfn(std::string("S1F15; replying S1F16 (OFLACK=") +
|
||||
std::to_string(static_cast<int>(ack)) + ")");
|
||||
return gem::s1f16_offline_ack(ack);
|
||||
}
|
||||
if (s == 1 && f == 17) {
|
||||
auto ack = sm->on_host_request_online();
|
||||
logfn(std::string("S1F17; replying S1F18 (ONLACK=") +
|
||||
std::to_string(static_cast<int>(ack)) + ")");
|
||||
return gem::s1f18_online_ack(ack);
|
||||
}
|
||||
|
||||
// ---- S2: equipment control + reports -----------------------------
|
||||
if (s == 2 && f == 13) {
|
||||
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; replying S2F14 (" + std::to_string(values.size()) + " values)");
|
||||
return gem::s2f14_ec_data(values);
|
||||
}
|
||||
if (s == 2 && f == 15) {
|
||||
auto sets = gem::parse_s2f15(msg);
|
||||
if (!sets) return gem::s2f16_ec_ack(gem::EquipmentAck::Denied_OutOfRange);
|
||||
auto eac = gem::EquipmentAck::Accept;
|
||||
for (const auto& [id, val] : *sets) {
|
||||
auto r = model->set_equipment_constant_value(id, val);
|
||||
if (r != gem::EquipmentAck::Accept) eac = r;
|
||||
}
|
||||
logfn(std::string("S2F15; replying S2F16 (EAC=") +
|
||||
std::to_string(static_cast<int>(eac)) + ")");
|
||||
return gem::s2f16_ec_ack(eac);
|
||||
}
|
||||
if (s == 2 && f == 17) {
|
||||
logfn("S2F17; replying S2F18 (clock)");
|
||||
return gem::s2f18_date_time_data(model->current_time_string());
|
||||
}
|
||||
if (s == 2 && f == 29) {
|
||||
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; replying S2F30 (" + std::to_string(ecs.size()) + " EC entries)");
|
||||
return gem::s2f30_ec_namelist_data(ecs);
|
||||
}
|
||||
if (s == 2 && f == 31) {
|
||||
auto t = gem::parse_s2f31(msg);
|
||||
auto ack = t ? model->set_time_string(*t) : gem::TimeAck::Error;
|
||||
logfn(std::string("S2F31; replying S2F32 (TIACK=") +
|
||||
std::to_string(static_cast<int>(ack)) + ")");
|
||||
return gem::s2f32_date_time_ack(ack);
|
||||
}
|
||||
if (s == 2 && f == 33) {
|
||||
auto req = gem::parse_s2f33(msg);
|
||||
auto ack = req ? model->define_reports(req->reports)
|
||||
: gem::DefineReportAck::InvalidFormat;
|
||||
logfn(std::string("S2F33; replying S2F34 (DRACK=") +
|
||||
std::to_string(static_cast<int>(ack)) + ")");
|
||||
return gem::s2f34_define_report_ack(ack);
|
||||
}
|
||||
if (s == 2 && f == 35) {
|
||||
auto req = gem::parse_s2f35(msg);
|
||||
auto ack = req ? model->link_event_reports(req->links) : gem::LinkEventAck::InvalidFormat;
|
||||
logfn(std::string("S2F35; replying S2F36 (LRACK=") +
|
||||
std::to_string(static_cast<int>(ack)) + ")");
|
||||
return gem::s2f36_link_event_report_ack(ack);
|
||||
}
|
||||
if (s == 2 && f == 37) {
|
||||
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") +
|
||||
"; replying S2F38 (ERACK=" + std::to_string(static_cast<int>(ack)) + ")");
|
||||
return gem::s2f38_enable_event_ack(ack);
|
||||
}
|
||||
if (s == 2 && f == 41) {
|
||||
auto cmd = gem::parse_s2f41(msg);
|
||||
if (!cmd) return gem::s2f42_host_command_ack(gem::HostCmdAck::ParameterInvalid);
|
||||
auto ack = model->dispatch_command(cmd->rcmd, cmd->params);
|
||||
logfn("S2F41 RCMD=" + cmd->rcmd + "; replying S2F42 (HCACK=" +
|
||||
std::to_string(static_cast<int>(ack)) + ")");
|
||||
return gem::s2f42_host_command_ack(ack);
|
||||
}
|
||||
|
||||
// ---- S5: alarms --------------------------------------------------
|
||||
if (s == 5 && f == 3) {
|
||||
auto req = gem::parse_s5f3(msg);
|
||||
auto ack = req ? model->set_alarm_enabled(req->alid, req->enable)
|
||||
: gem::AlarmAck::Error;
|
||||
logfn(std::string("S5F3 ALID=") + (req ? std::to_string(req->alid) : "?") +
|
||||
(req && req->enable ? " enable" : " disable") +
|
||||
"; replying S5F4 (ACKC5=" + std::to_string(static_cast<int>(ack)) + ")");
|
||||
return gem::s5f4_enable_alarm_ack(ack);
|
||||
}
|
||||
if (s == 5 && f == 5) {
|
||||
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; replying S5F6 (" + std::to_string(alarms.size()) + " alarms)");
|
||||
return gem::s5f6_list_alarms_data(alarms, [model](uint32_t id) {
|
||||
return model->alarm_active(id);
|
||||
});
|
||||
}
|
||||
|
||||
// ---- S7: process programs ----------------------------------------
|
||||
if (s == 7 && f == 3) {
|
||||
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 + " (" + std::to_string(pp->ppbody.size()) +
|
||||
" bytes); replying S7F4 (ACKC7=0)");
|
||||
return gem::s7f4_process_program_ack(gem::ProcessProgramAck::Accept);
|
||||
}
|
||||
if (s == 7 && f == 5) {
|
||||
auto ppid = gem::parse_s7f5(msg);
|
||||
if (!ppid) return gem::s7f6_process_program_data("", "");
|
||||
auto body = model->process_program(*ppid);
|
||||
logfn("S7F5 PPID=" + *ppid + (body ? "; replying S7F6" : "; PPID not found"));
|
||||
return gem::s7f6_process_program_data(*ppid, body ? *body : "");
|
||||
}
|
||||
if (s == 7 && f == 19) {
|
||||
auto list = model->process_program_list();
|
||||
logfn("S7F19; replying S7F20 (" + std::to_string(list.size()) + " PPIDs)");
|
||||
return gem::s7f20_current_eppd_data(list);
|
||||
}
|
||||
|
||||
// ---- S10: terminal services --------------------------------------
|
||||
if (s == 10 && f == 1) {
|
||||
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);
|
||||
}
|
||||
|
||||
// Unhandled primaries expecting a reply get SxF0 (abort).
|
||||
if (msg.reply_expected) {
|
||||
logfn("unhandled " + msg.sml() + "; replying S" + std::to_string(s) + "F0");
|
||||
return s2::Message(s, 0, false);
|
||||
}
|
||||
return std::nullopt;
|
||||
});
|
||||
conn->set_message_handler(
|
||||
[&router](const s2::Message& msg) { return router.dispatch(msg); });
|
||||
});
|
||||
|
||||
server.start();
|
||||
|
||||
Reference in New Issue
Block a user