feat: EquipmentRuntime engine owner + secs_gemd gRPC daemon

Extract the SECS/GEM engine wiring out of the secs_server app into a
reusable class, and stand up a language-agnostic gRPC daemon on top so a
tool's software (any language) can drive the equipment without linking C++
or knowing SEMI. Foundation for replacing a vendor's SECS/GEM server.

Engine reuse:
- EquipmentRuntime (include/secsgem/gem/runtime.hpp, src/gem/runtime.cpp):
  owns io_context, passive Server, model, control-state machine, Router;
  thread-safe outbound API (set_variable/emit_event/set_alarm/clear_alarm),
  on_command hook, deliver_or_spool, run()/run_async()/poll()/stop().
- register_default_handlers (src/gem/default_handlers.cpp): the 56 GEM
  handlers + domain emitters, relocated from secs_server so the app and the
  daemon speak byte-identical GEM. secs_server.cpp reduced ~1270 -> 113 lines.
- name_index.hpp: resolve_variable(name) -> VID (the name->id binding layer).

Daemon (apps/secs_gemd.cpp, proto/secsgem/v1/equipment.proto):
- runs the engine + HSMS link on a background thread; serves the gRPC
  Equipment service. Increment 1: SetVariables (name-resolved, plain
  value->Item) and GetControlState. proto carries the full v1 surface
  (universal + carrier/recipe/job tiers); remaining RPCs + the Subscribe
  command stream are next (docs/DAEMON_ROADMAP.md).
- CMake: opt-in SECSGEM_DAEMON, protoc/grpc_cpp_plugin codegen, gracefully
  skipped where protobuf/grpc++ are absent. Dockerfile gains the grpc deps.

Tests (proof): test_runtime, test_default_handlers (S1F1->S1F2, S2F41->hook),
test_name_index. Full suite 458/458, 2795 assertions; live server<->client
GEM300 demo still passes on the refactored server.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-10 18:01:16 +02:00
parent 4b4b2ac690
commit fc898f8410
14 changed files with 2135 additions and 1183 deletions
+73
View File
@@ -0,0 +1,73 @@
#include <doctest/doctest.h>
#include <vector>
#include "secsgem/gem/default_handlers.hpp"
#include "secsgem/gem/messages.hpp"
#include "secsgem/gem/runtime.hpp"
using namespace secsgem;
namespace gem = secsgem::gem;
namespace s2 = secsgem::secs2;
#ifndef SECSGEM_DATA_DIR
#error "SECSGEM_DATA_DIR not defined; see CMakeLists.txt"
#endif
static gem::EquipmentRuntime::Config test_config() {
gem::EquipmentRuntime::Config c;
c.equipment_yaml = SECSGEM_DATA_DIR "/equipment.yaml";
c.control_state_yaml = SECSGEM_DATA_DIR "/control_state.yaml";
c.process_job_yaml = SECSGEM_DATA_DIR "/process_job_state.yaml";
c.control_job_yaml = SECSGEM_DATA_DIR "/control_job_state.yaml";
c.port = 0;
return c;
}
TEST_CASE("register_default_handlers populates the runtime's Router") {
gem::EquipmentRuntime rt(test_config());
CHECK(rt.router().size() == 0); // nothing registered yet
gem::register_default_handlers(rt);
CHECK(rt.router().size() > 40); // the full GEM handler set (~56)
}
TEST_CASE("register_default_handlers: S1F1 dispatches to S1F2 On-Line Data") {
gem::EquipmentRuntime rt(test_config());
gem::register_default_handlers(rt);
auto reply = rt.router().dispatch(s2::Message(1, 1, true)); // Are You There
REQUIRE(reply.has_value());
CHECK(reply->stream == 1);
CHECK(reply->function == 2);
}
TEST_CASE("register_default_handlers: S2F41 reaches the on_command behaviour hook") {
gem::EquipmentRuntime rt(test_config());
gem::register_default_handlers(rt);
bool ran = false;
std::string seen;
rt.on_command("START", [&](const std::string& rcmd,
const std::vector<gem::CommandParameter>&) {
ran = true;
seen = rcmd;
return gem::HostCmdAck::Accept;
});
auto reply = rt.router().dispatch(gem::s2f41_host_command("START", {}));
REQUIRE(reply.has_value());
CHECK(reply->stream == 2);
CHECK(reply->function == 42); // S2F42 Host Command Ack
CHECK(ran); // the handler ran inside the S2F41 dispatch
CHECK(seen == "START");
}
TEST_CASE("register_default_handlers: unknown command is rejected, hook not invoked") {
gem::EquipmentRuntime rt(test_config());
gem::register_default_handlers(rt);
auto reply = rt.router().dispatch(gem::s2f41_host_command("NO_SUCH_CMD", {}));
REQUIRE(reply.has_value());
CHECK(reply->stream == 2);
CHECK(reply->function == 42); // still an S2F42, carrying an error HCACK
}
+23
View File
@@ -0,0 +1,23 @@
#include <doctest/doctest.h>
#include "secsgem/config/loader.hpp"
#include "secsgem/gem/name_index.hpp"
using namespace secsgem;
namespace gem = secsgem::gem;
#ifndef SECSGEM_DATA_DIR
#error "SECSGEM_DATA_DIR not defined; see CMakeLists.txt"
#endif
TEST_CASE("resolve_variable maps config names to VIDs across SVIDs and DVIDs") {
gem::EquipmentDataModel m;
config::load_equipment(SECSGEM_DATA_DIR "/equipment.yaml", m);
CHECK(gem::resolve_variable(m, "ControlState") == 1); // SVID
CHECK(gem::resolve_variable(m, "Clock") == 2); // SVID
CHECK(gem::resolve_variable(m, "WaferCounter") == 100); // DVID
CHECK(gem::resolve_variable(m, "ChamberPressure") == 101);// DVID
CHECK_FALSE(gem::resolve_variable(m, "nonexistent").has_value());
CHECK_FALSE(gem::resolve_variable(m, "").has_value());
}
+66
View File
@@ -0,0 +1,66 @@
#include <doctest/doctest.h>
#include <vector>
#include "secsgem/gem/runtime.hpp"
#include "secsgem/secs2/item.hpp"
using namespace secsgem;
namespace gem = secsgem::gem;
namespace s2 = secsgem::secs2;
#ifndef SECSGEM_DATA_DIR
#error "SECSGEM_DATA_DIR not defined; see CMakeLists.txt"
#endif
// Port 0 binds an OS-chosen ephemeral port. These tests never call run()/
// run_async(), so the acceptor is opened but never armed — we exercise the
// outbound API by posting and draining with poll(), no host involved.
static gem::EquipmentRuntime::Config test_config() {
gem::EquipmentRuntime::Config c;
c.equipment_yaml = SECSGEM_DATA_DIR "/equipment.yaml";
c.control_state_yaml = SECSGEM_DATA_DIR "/control_state.yaml";
c.process_job_yaml = SECSGEM_DATA_DIR "/process_job_state.yaml";
c.control_job_yaml = SECSGEM_DATA_DIR "/control_job_state.yaml";
c.port = 0;
return c;
}
TEST_CASE("EquipmentRuntime loads the data dictionary from config") {
gem::EquipmentRuntime rt(test_config());
CHECK(rt.model().svids.all().size() == 3);
CHECK(rt.model().alarms.all().size() == 2);
CHECK(rt.control_state() == gem::ControlState::HostOffline);
}
TEST_CASE("EquipmentRuntime.set_variable posts onto the io thread and updates the model") {
gem::EquipmentRuntime rt(test_config());
rt.set_variable(1, s2::Item::ascii("OnlineRemote"));
CHECK(rt.model().svids.value(1) != s2::Item::ascii("OnlineRemote")); // not yet — posted
rt.poll();
CHECK(rt.model().svids.value(1) == s2::Item::ascii("OnlineRemote"));
}
TEST_CASE("EquipmentRuntime.set_alarm / clear_alarm toggle the active flag") {
gem::EquipmentRuntime rt(test_config());
rt.set_alarm(1);
rt.poll();
CHECK(rt.model().alarms.active(1));
rt.clear_alarm(1);
rt.poll();
CHECK_FALSE(rt.model().alarms.active(1));
}
TEST_CASE("EquipmentRuntime.on_command registers the behaviour hook on the model") {
gem::EquipmentRuntime rt(test_config());
bool ran = false;
rt.on_command("START", [&](const std::string& rcmd,
const std::vector<gem::CommandParameter>&) {
ran = (rcmd == "START");
return gem::HostCmdAck::Accept;
});
CHECK(rt.model().commands.has_handler("START"));
auto res = rt.model().commands.dispatch("START", {});
CHECK(ran);
CHECK(res.ack == gem::HostCmdAck::Accept);
}