E40 Process Jobs + E94 Control Jobs + E30 communication state
GEM300 layer: SEMI E40-0705 Process Job and E94-0705 Control Job state machines, plus the E30 §6.1 communication-state machine that sits between HSMS SELECT and full GEM communication. Data-driven via data/process_job_state.yaml and data/control_job_state.yaml, mirroring the existing control_state.yaml pattern. Wire coverage: S14F9/F10 CreateObject (CJ) host -> equipment S14F11/F12 DeleteObject (CJ) host -> equipment S16F5/F6 PRJobCommand host -> equipment S16F9 PRJobAlert equipment -> host S16F11/F12 PRJobCreate (simplified body) host -> equipment S16F13/F14 PRJobDequeue host -> equipment S16F27/F28 CJobCommand host -> equipment Process Job FSM exposes 8 states matching PRJOBSTATE bytes (E40 §10.3.2); HOQ is reorder-aware (move-to-head against an insertion-order vector); Stop/Abort on a Queued PJ routes through ABORTING so the host observes PRJOBSTATE=7 on the wire (§6.3); alert_enabled is settable per-PJ for PRALERT control; FSM dispatches through ProcessJobStore::on_change_ dynamically so a late set_state_change_handler() reaches existing PJs. Hardening: loader rejects NoState (sentinel) as initial/from/to and rejects `on: created` rows; static_asserts pin enum values to wire bytes; ProcessJobStore is non-movable to keep the per-PJ this-capture safe. Server simulator cascades the full CJ -> PJ lifecycle on CJSTART so the wire trace exercises every legal state. CEIDs 400/401 fire on CJ state changes via the existing event-report pipeline. Tests: 60+ new assertions across test_process_jobs, test_control_jobs, test_communication_state, test_hsms_connection, plus loader and messages round-trip coverage. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -242,4 +242,89 @@ EquipmentDescriptor load_equipment(const std::string& path, gem::EquipmentDataMo
|
||||
return desc;
|
||||
}
|
||||
|
||||
ProcessJobStateConfig load_process_job_state(const std::string& path) {
|
||||
YAML::Node root = load(path);
|
||||
|
||||
// NoState is a sentinel for "no PJ" used in the synthetic create
|
||||
// notification; it must never appear in the FSM table or as the
|
||||
// initial state.
|
||||
auto reject_nostate = [&path](const std::string& where, gem::ProcessJobState s) {
|
||||
if (s == gem::ProcessJobState::NoState)
|
||||
fail(where, "`NoState` is a sentinel, not a valid table state");
|
||||
};
|
||||
|
||||
ProcessJobStateConfig cfg;
|
||||
if (auto initial = root["initial"]) {
|
||||
auto parsed = gem::parse_process_job_state(initial.as<std::string>());
|
||||
if (!parsed) fail(path, "unknown initial state `" + initial.as<std::string>() + "`");
|
||||
reject_nostate(path + " initial", *parsed);
|
||||
cfg.initial = *parsed;
|
||||
}
|
||||
|
||||
const auto transitions = root["transitions"];
|
||||
if (!transitions || !transitions.IsSequence())
|
||||
fail(path, "missing or non-sequence `transitions`");
|
||||
|
||||
for (std::size_t i = 0; i < transitions.size(); ++i) {
|
||||
const auto& row = transitions[i];
|
||||
const auto where = path + " transitions[" + std::to_string(i) + "]";
|
||||
|
||||
auto from = gem::parse_process_job_state(req_as<std::string>(row["from"], where, "from"));
|
||||
auto on = gem::parse_process_job_event(req_as<std::string>(row["on"], where, "on"));
|
||||
if (!from) fail(where, "unknown `from` state");
|
||||
if (!on) fail(where, "unknown `on` event");
|
||||
reject_nostate(where + " from", *from);
|
||||
|
||||
gem::ProcessJobTransition t{*from, *on, std::nullopt, std::nullopt};
|
||||
if (auto to = row["to"]) {
|
||||
auto s = gem::parse_process_job_state(to.as<std::string>());
|
||||
if (!s) fail(where, "unknown `to` state");
|
||||
reject_nostate(where + " to", *s);
|
||||
t.to = *s;
|
||||
}
|
||||
if (auto ack = row["ack"]) {
|
||||
t.ack_code = static_cast<uint8_t>(parse_hcack(ack.as<std::string>(), where));
|
||||
}
|
||||
cfg.table.add(t);
|
||||
}
|
||||
return cfg;
|
||||
}
|
||||
|
||||
ControlJobStateConfig load_control_job_state(const std::string& path) {
|
||||
YAML::Node root = load(path);
|
||||
|
||||
ControlJobStateConfig cfg;
|
||||
if (auto initial = root["initial"]) {
|
||||
auto parsed = gem::parse_control_job_state(initial.as<std::string>());
|
||||
if (!parsed) fail(path, "unknown initial state `" + initial.as<std::string>() + "`");
|
||||
cfg.initial = *parsed;
|
||||
}
|
||||
|
||||
const auto transitions = root["transitions"];
|
||||
if (!transitions || !transitions.IsSequence())
|
||||
fail(path, "missing or non-sequence `transitions`");
|
||||
|
||||
for (std::size_t i = 0; i < transitions.size(); ++i) {
|
||||
const auto& row = transitions[i];
|
||||
const auto where = path + " transitions[" + std::to_string(i) + "]";
|
||||
|
||||
auto from = gem::parse_control_job_state(req_as<std::string>(row["from"], where, "from"));
|
||||
auto on = gem::parse_control_job_event(req_as<std::string>(row["on"], where, "on"));
|
||||
if (!from) fail(where, "unknown `from` state");
|
||||
if (!on) fail(where, "unknown `on` event");
|
||||
|
||||
gem::ControlJobTransition t{*from, *on, std::nullopt, std::nullopt};
|
||||
if (auto to = row["to"]) {
|
||||
auto s = gem::parse_control_job_state(to.as<std::string>());
|
||||
if (!s) fail(where, "unknown `to` state");
|
||||
t.to = *s;
|
||||
}
|
||||
if (auto ack = row["ack"]) {
|
||||
t.ack_code = static_cast<uint8_t>(parse_hcack(ack.as<std::string>(), where));
|
||||
}
|
||||
cfg.table.add(t);
|
||||
}
|
||||
return cfg;
|
||||
}
|
||||
|
||||
} // namespace secsgem::config
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
#include "secsgem/gem/communication_state.hpp"
|
||||
|
||||
namespace secsgem::gem {
|
||||
|
||||
const char* comm_state_name(CommState s) {
|
||||
switch (s) {
|
||||
case CommState::Disabled: return "DISABLED";
|
||||
case CommState::WaitCRA: return "WAIT-CRA";
|
||||
case CommState::WaitDelay: return "WAIT-DELAY";
|
||||
case CommState::Communicating: return "COMMUNICATING";
|
||||
}
|
||||
return "?";
|
||||
}
|
||||
|
||||
CommunicationStateMachine::CommunicationStateMachine(CommTimers timers, Initiator initiator)
|
||||
: timers_(timers), initiator_(initiator) {}
|
||||
|
||||
void CommunicationStateMachine::transition(CommState next, const std::string& reason) {
|
||||
if (state_ == next) return;
|
||||
const CommState prev = state_;
|
||||
state_ = next;
|
||||
if (on_change_) on_change_(prev, next, reason);
|
||||
}
|
||||
|
||||
void CommunicationStateMachine::enable() {
|
||||
if (state_ != CommState::Disabled) return;
|
||||
// Cancel any stale timers from a previous lifetime.
|
||||
if (env_.cancel_timers) env_.cancel_timers();
|
||||
|
||||
if (initiator_ == Initiator::Equipment) {
|
||||
// Equipment-initiated: send S1F13 immediately and wait for S1F14.
|
||||
transition(CommState::WaitCRA, "enabled; equipment-initiated S1F13");
|
||||
if (env_.arm_t_cra) env_.arm_t_cra(timers_.t_cra);
|
||||
if (env_.send_s1f13) env_.send_s1f13();
|
||||
} else {
|
||||
// Host-initiated: we stay non-communicating until the host sends
|
||||
// S1F13; we model the wait as WAIT-CRA (no T_CRA armed since the
|
||||
// wait is indefinite from our side).
|
||||
transition(CommState::WaitCRA, "enabled; awaiting host S1F13");
|
||||
}
|
||||
}
|
||||
|
||||
void CommunicationStateMachine::disable() {
|
||||
if (state_ == CommState::Disabled) return;
|
||||
if (env_.cancel_timers) env_.cancel_timers();
|
||||
transition(CommState::Disabled, "disabled by operator");
|
||||
}
|
||||
|
||||
void CommunicationStateMachine::on_s1f14_received(uint8_t commack) {
|
||||
if (state_ != CommState::WaitCRA) return; // unexpected S1F14; ignore
|
||||
if (env_.cancel_timers) env_.cancel_timers();
|
||||
if (commack == 0) {
|
||||
transition(CommState::Communicating, "S1F14 COMMACK=Accept");
|
||||
} else {
|
||||
transition(CommState::WaitDelay,
|
||||
"S1F14 COMMACK=" + std::to_string(commack) + " (denied)");
|
||||
if (env_.arm_t_delay) env_.arm_t_delay(timers_.t_delay);
|
||||
}
|
||||
}
|
||||
|
||||
void CommunicationStateMachine::on_s1f13_received() {
|
||||
// Inbound establishment from the host. Spec allows this in any
|
||||
// ENABLED substate (the host can re-establish). Disabled equipment
|
||||
// would reply with COMMACK=Denied; that's the embedder's call, not
|
||||
// ours — we just record the transition if the embedder accepts.
|
||||
if (state_ == CommState::Disabled) return;
|
||||
if (env_.cancel_timers) env_.cancel_timers();
|
||||
transition(CommState::Communicating, "host S1F13 received");
|
||||
}
|
||||
|
||||
void CommunicationStateMachine::on_connection_lost() {
|
||||
if (state_ == CommState::Disabled) return;
|
||||
if (env_.cancel_timers) env_.cancel_timers();
|
||||
// Per E30: a transport drop returns us to NOT-COMMUNICATING. We
|
||||
// model that as WAIT-DELAY (so we retry after T_DELAY) when we're
|
||||
// an equipment-initiator, and as WAIT-CRA (awaiting host S1F13)
|
||||
// otherwise.
|
||||
if (initiator_ == Initiator::Equipment) {
|
||||
transition(CommState::WaitDelay, "transport dropped");
|
||||
if (env_.arm_t_delay) env_.arm_t_delay(timers_.t_delay);
|
||||
} else {
|
||||
transition(CommState::WaitCRA, "transport dropped; awaiting host S1F13");
|
||||
}
|
||||
}
|
||||
|
||||
void CommunicationStateMachine::on_cra_timeout() {
|
||||
if (state_ != CommState::WaitCRA) return;
|
||||
transition(CommState::WaitDelay, "T_CRA timeout");
|
||||
if (env_.arm_t_delay) env_.arm_t_delay(timers_.t_delay);
|
||||
}
|
||||
|
||||
void CommunicationStateMachine::on_delay_elapsed() {
|
||||
if (state_ != CommState::WaitDelay) return;
|
||||
// T_DELAY elapsed; re-attempt S1F13 if equipment-initiated.
|
||||
if (initiator_ == Initiator::Equipment) {
|
||||
transition(CommState::WaitCRA, "T_DELAY elapsed; re-attempting S1F13");
|
||||
if (env_.arm_t_cra) env_.arm_t_cra(timers_.t_cra);
|
||||
if (env_.send_s1f13) env_.send_s1f13();
|
||||
} else {
|
||||
transition(CommState::WaitCRA, "T_DELAY elapsed; awaiting host S1F13");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace secsgem::gem
|
||||
@@ -0,0 +1,160 @@
|
||||
#include "secsgem/gem/control_job_state.hpp"
|
||||
|
||||
namespace secsgem::gem {
|
||||
|
||||
const char* control_job_state_name(ControlJobState s) {
|
||||
switch (s) {
|
||||
case ControlJobState::Queued: return "Queued";
|
||||
case ControlJobState::Selected: return "Selected";
|
||||
case ControlJobState::WaitingForStart: return "WaitingForStart";
|
||||
case ControlJobState::Executing: return "Executing";
|
||||
case ControlJobState::Paused: return "Paused";
|
||||
case ControlJobState::Completed: return "Completed";
|
||||
case ControlJobState::Stopping: return "Stopping";
|
||||
case ControlJobState::Aborting: return "Aborting";
|
||||
case ControlJobState::NoState: return "NoState";
|
||||
}
|
||||
return "?";
|
||||
}
|
||||
|
||||
std::optional<ControlJobState> parse_control_job_state(const std::string& s) {
|
||||
if (s == "Queued") return ControlJobState::Queued;
|
||||
if (s == "Selected") return ControlJobState::Selected;
|
||||
if (s == "WaitingForStart") return ControlJobState::WaitingForStart;
|
||||
if (s == "Executing") return ControlJobState::Executing;
|
||||
if (s == "Paused") return ControlJobState::Paused;
|
||||
if (s == "Completed") return ControlJobState::Completed;
|
||||
if (s == "Stopping") return ControlJobState::Stopping;
|
||||
if (s == "Aborting") return ControlJobState::Aborting;
|
||||
if (s == "NoState") return ControlJobState::NoState;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const char* control_job_event_name(ControlJobEvent e) {
|
||||
switch (e) {
|
||||
case ControlJobEvent::Created: return "Created";
|
||||
case ControlJobEvent::Select: return "Select";
|
||||
case ControlJobEvent::SetupComplete: return "SetupComplete";
|
||||
case ControlJobEvent::Start: return "Start";
|
||||
case ControlJobEvent::Pause: return "Pause";
|
||||
case ControlJobEvent::Resume: return "Resume";
|
||||
case ControlJobEvent::Stop: return "Stop";
|
||||
case ControlJobEvent::Abort: return "Abort";
|
||||
case ControlJobEvent::AllJobsComplete: return "AllJobsComplete";
|
||||
case ControlJobEvent::AbortComplete: return "AbortComplete";
|
||||
}
|
||||
return "?";
|
||||
}
|
||||
|
||||
std::optional<ControlJobEvent> parse_control_job_event(const std::string& s) {
|
||||
if (s == "created") return ControlJobEvent::Created;
|
||||
if (s == "select") return ControlJobEvent::Select;
|
||||
if (s == "setup_complete") return ControlJobEvent::SetupComplete;
|
||||
if (s == "start") return ControlJobEvent::Start;
|
||||
if (s == "pause") return ControlJobEvent::Pause;
|
||||
if (s == "resume") return ControlJobEvent::Resume;
|
||||
if (s == "stop") return ControlJobEvent::Stop;
|
||||
if (s == "abort") return ControlJobEvent::Abort;
|
||||
if (s == "all_jobs_complete") return ControlJobEvent::AllJobsComplete;
|
||||
if (s == "abort_complete") return ControlJobEvent::AbortComplete;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<ControlJobEvent> ctl_cmd_to_event(const std::string& cmd) {
|
||||
if (cmd == "CJSTART" || cmd == "START") return ControlJobEvent::Start;
|
||||
if (cmd == "CJPAUSE" || cmd == "PAUSE") return ControlJobEvent::Pause;
|
||||
if (cmd == "CJRESUME" || cmd == "RESUME") return ControlJobEvent::Resume;
|
||||
if (cmd == "CJSTOP" || cmd == "STOP") return ControlJobEvent::Stop;
|
||||
if (cmd == "CJABORT" || cmd == "ABORT") return ControlJobEvent::Abort;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void ControlJobTransitionTable::add(ControlJobTransition row) {
|
||||
rows_.push_back(row);
|
||||
}
|
||||
|
||||
const ControlJobTransition* ControlJobTransitionTable::find(
|
||||
ControlJobState from, ControlJobEvent on) const {
|
||||
for (const auto& r : rows_) {
|
||||
if (r.from == from && r.on == on) return &r;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ControlJobTransitionTable ControlJobTransitionTable::default_table() {
|
||||
using S = ControlJobState;
|
||||
using E = ControlJobEvent;
|
||||
ControlJobTransitionTable t;
|
||||
|
||||
// QUEUED:
|
||||
t.add({S::Queued, E::Select, S::Selected, std::nullopt});
|
||||
t.add({S::Queued, E::Stop, S::Completed, std::nullopt}); // empty CJ stop
|
||||
t.add({S::Queued, E::Abort, S::Completed, std::nullopt});
|
||||
|
||||
// SELECTED:
|
||||
t.add({S::Selected, E::SetupComplete, S::WaitingForStart, std::nullopt});
|
||||
t.add({S::Selected, E::Stop, S::Stopping, std::nullopt});
|
||||
t.add({S::Selected, E::Abort, S::Aborting, std::nullopt});
|
||||
|
||||
// WAITING-FOR-START:
|
||||
t.add({S::WaitingForStart, E::Start, S::Executing, std::nullopt});
|
||||
t.add({S::WaitingForStart, E::Stop, S::Stopping, std::nullopt});
|
||||
t.add({S::WaitingForStart, E::Abort, S::Aborting, std::nullopt});
|
||||
|
||||
// EXECUTING:
|
||||
t.add({S::Executing, E::Pause, S::Paused, std::nullopt});
|
||||
t.add({S::Executing, E::Stop, S::Stopping, std::nullopt});
|
||||
t.add({S::Executing, E::Abort, S::Aborting, std::nullopt});
|
||||
t.add({S::Executing, E::AllJobsComplete, S::Completed, std::nullopt});
|
||||
|
||||
// PAUSED:
|
||||
t.add({S::Paused, E::Resume, S::Executing, std::nullopt});
|
||||
t.add({S::Paused, E::Stop, S::Stopping, std::nullopt});
|
||||
t.add({S::Paused, E::Abort, S::Aborting, std::nullopt});
|
||||
|
||||
// STOPPING:
|
||||
t.add({S::Stopping, E::AllJobsComplete, S::Completed, std::nullopt});
|
||||
t.add({S::Stopping, E::Abort, S::Aborting, std::nullopt});
|
||||
|
||||
// ABORTING:
|
||||
t.add({S::Aborting, E::AbortComplete, S::Completed, std::nullopt});
|
||||
|
||||
// COMPLETED: terminal.
|
||||
return t;
|
||||
}
|
||||
|
||||
ControlJobStateMachine::ControlJobStateMachine()
|
||||
: ControlJobStateMachine(ControlJobTransitionTable::default_table(),
|
||||
ControlJobState::Queued) {}
|
||||
|
||||
ControlJobStateMachine::ControlJobStateMachine(ControlJobTransitionTable table,
|
||||
ControlJobState initial)
|
||||
: table_(std::move(table)), state_(initial) {}
|
||||
|
||||
void ControlJobStateMachine::transition(ControlJobState next,
|
||||
ControlJobEvent trigger) {
|
||||
if (state_ == next) return;
|
||||
const ControlJobState prev = state_;
|
||||
state_ = next;
|
||||
if (on_change_) on_change_(prev, next, trigger);
|
||||
}
|
||||
|
||||
const ControlJobTransition* ControlJobStateMachine::fire(ControlJobEvent on) {
|
||||
const ControlJobTransition* row = table_.find(state_, on);
|
||||
if (!row) return nullptr;
|
||||
if (row->to) transition(*row->to, on);
|
||||
return row;
|
||||
}
|
||||
|
||||
HostCmdAck ControlJobStateMachine::on_host_command(ControlJobEvent event) {
|
||||
const auto* row = fire(event);
|
||||
if (!row) return HostCmdAck::CannotDoNow;
|
||||
if (row->ack_code) return static_cast<HostCmdAck>(*row->ack_code);
|
||||
return HostCmdAck::Accept;
|
||||
}
|
||||
|
||||
bool ControlJobStateMachine::on_internal(ControlJobEvent event) {
|
||||
return fire(event) != nullptr;
|
||||
}
|
||||
|
||||
} // namespace secsgem::gem
|
||||
@@ -0,0 +1,183 @@
|
||||
#include "secsgem/gem/process_job_state.hpp"
|
||||
|
||||
namespace secsgem::gem {
|
||||
|
||||
// PRJOBSTATE on the wire is a single byte per E40-0705 §10.3.2. Pin the
|
||||
// enum values to the spec so a future reorder fails the build instead of
|
||||
// silently corrupting S16F9 emissions.
|
||||
static_assert(static_cast<uint8_t>(ProcessJobState::Queued) == 0);
|
||||
static_assert(static_cast<uint8_t>(ProcessJobState::SettingUp) == 1);
|
||||
static_assert(static_cast<uint8_t>(ProcessJobState::WaitingForStart) == 2);
|
||||
static_assert(static_cast<uint8_t>(ProcessJobState::Processing) == 3);
|
||||
static_assert(static_cast<uint8_t>(ProcessJobState::ProcessComplete) == 4);
|
||||
static_assert(static_cast<uint8_t>(ProcessJobState::Paused) == 5);
|
||||
static_assert(static_cast<uint8_t>(ProcessJobState::Stopping) == 6);
|
||||
static_assert(static_cast<uint8_t>(ProcessJobState::Aborting) == 7);
|
||||
|
||||
const char* process_job_state_name(ProcessJobState s) {
|
||||
switch (s) {
|
||||
case ProcessJobState::Queued: return "Queued";
|
||||
case ProcessJobState::SettingUp: return "SettingUp";
|
||||
case ProcessJobState::WaitingForStart: return "WaitingForStart";
|
||||
case ProcessJobState::Processing: return "Processing";
|
||||
case ProcessJobState::ProcessComplete: return "ProcessComplete";
|
||||
case ProcessJobState::Paused: return "Paused";
|
||||
case ProcessJobState::Stopping: return "Stopping";
|
||||
case ProcessJobState::Aborting: return "Aborting";
|
||||
case ProcessJobState::NoState: return "NoState";
|
||||
}
|
||||
return "?";
|
||||
}
|
||||
|
||||
std::optional<ProcessJobState> parse_process_job_state(const std::string& s) {
|
||||
if (s == "Queued") return ProcessJobState::Queued;
|
||||
if (s == "SettingUp") return ProcessJobState::SettingUp;
|
||||
if (s == "WaitingForStart") return ProcessJobState::WaitingForStart;
|
||||
if (s == "Processing") return ProcessJobState::Processing;
|
||||
if (s == "ProcessComplete") return ProcessJobState::ProcessComplete;
|
||||
if (s == "Paused") return ProcessJobState::Paused;
|
||||
if (s == "Stopping") return ProcessJobState::Stopping;
|
||||
if (s == "Aborting") return ProcessJobState::Aborting;
|
||||
if (s == "NoState") return ProcessJobState::NoState;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const char* process_job_event_name(ProcessJobEvent e) {
|
||||
switch (e) {
|
||||
case ProcessJobEvent::Created: return "Created";
|
||||
case ProcessJobEvent::Select: return "Select";
|
||||
case ProcessJobEvent::SetupComplete: return "SetupComplete";
|
||||
case ProcessJobEvent::Start: return "Start";
|
||||
case ProcessJobEvent::Pause: return "Pause";
|
||||
case ProcessJobEvent::Resume: return "Resume";
|
||||
case ProcessJobEvent::Stop: return "Stop";
|
||||
case ProcessJobEvent::Abort: return "Abort";
|
||||
case ProcessJobEvent::HeadOfQueue: return "HeadOfQueue";
|
||||
case ProcessJobEvent::ProcessComplete: return "ProcessComplete";
|
||||
case ProcessJobEvent::AbortComplete: return "AbortComplete";
|
||||
}
|
||||
return "?";
|
||||
}
|
||||
|
||||
std::optional<ProcessJobEvent> parse_process_job_event(const std::string& s) {
|
||||
// `Created` is intentionally not parseable — it's a synthetic observer
|
||||
// signal, never a table row. Returning nullopt makes the loader reject
|
||||
// `on: created` with "unknown `on` event".
|
||||
if (s == "select") return ProcessJobEvent::Select;
|
||||
if (s == "setup_complete") return ProcessJobEvent::SetupComplete;
|
||||
if (s == "start") return ProcessJobEvent::Start;
|
||||
if (s == "pause") return ProcessJobEvent::Pause;
|
||||
if (s == "resume") return ProcessJobEvent::Resume;
|
||||
if (s == "stop") return ProcessJobEvent::Stop;
|
||||
if (s == "abort") return ProcessJobEvent::Abort;
|
||||
if (s == "hoq") return ProcessJobEvent::HeadOfQueue;
|
||||
if (s == "process_complete") return ProcessJobEvent::ProcessComplete;
|
||||
if (s == "abort_complete") return ProcessJobEvent::AbortComplete;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<ProcessJobEvent> pr_cmd_to_event(const std::string& prcmd) {
|
||||
// PRCMD strings per E40-0705 §10.2.5.
|
||||
if (prcmd == "PJSTART" || prcmd == "START") return ProcessJobEvent::Start;
|
||||
if (prcmd == "PJPAUSE" || prcmd == "PAUSE") return ProcessJobEvent::Pause;
|
||||
if (prcmd == "PJRESUME" || prcmd == "RESUME") return ProcessJobEvent::Resume;
|
||||
if (prcmd == "PJSTOP" || prcmd == "STOP") return ProcessJobEvent::Stop;
|
||||
if (prcmd == "PJABORT" || prcmd == "ABORT") return ProcessJobEvent::Abort;
|
||||
if (prcmd == "PJHOQ" || prcmd == "HOQ") return ProcessJobEvent::HeadOfQueue;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void ProcessJobTransitionTable::add(ProcessJobTransition row) {
|
||||
rows_.push_back(row);
|
||||
}
|
||||
|
||||
const ProcessJobTransition* ProcessJobTransitionTable::find(
|
||||
ProcessJobState from, ProcessJobEvent on) const {
|
||||
for (const auto& r : rows_) {
|
||||
if (r.from == from && r.on == on) return &r;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ProcessJobTransitionTable ProcessJobTransitionTable::default_table() {
|
||||
using S = ProcessJobState;
|
||||
using E = ProcessJobEvent;
|
||||
ProcessJobTransitionTable t;
|
||||
|
||||
// QUEUED:
|
||||
t.add({S::Queued, E::Select, S::SettingUp, std::nullopt});
|
||||
t.add({S::Queued, E::HeadOfQueue, std::nullopt, std::nullopt}); // reorder only
|
||||
// Stop/Abort on a Queued PJ routes through Aborting so the host
|
||||
// observes PRJOBSTATE=7 on the wire (E40-0705 §6.3).
|
||||
t.add({S::Queued, E::Stop, S::Aborting, std::nullopt});
|
||||
t.add({S::Queued, E::Abort, S::Aborting, std::nullopt});
|
||||
|
||||
// SETTING-UP:
|
||||
t.add({S::SettingUp, E::SetupComplete, S::WaitingForStart, std::nullopt});
|
||||
t.add({S::SettingUp, E::Stop, S::Stopping, std::nullopt});
|
||||
t.add({S::SettingUp, E::Abort, S::Aborting, std::nullopt});
|
||||
|
||||
// WAITING-FOR-START:
|
||||
t.add({S::WaitingForStart, E::Start, S::Processing, std::nullopt});
|
||||
t.add({S::WaitingForStart, E::Stop, S::Stopping, std::nullopt});
|
||||
t.add({S::WaitingForStart, E::Abort, S::Aborting, std::nullopt});
|
||||
|
||||
// PROCESSING:
|
||||
t.add({S::Processing, E::Pause, S::Paused, std::nullopt});
|
||||
t.add({S::Processing, E::Stop, S::Stopping, std::nullopt});
|
||||
t.add({S::Processing, E::Abort, S::Aborting, std::nullopt});
|
||||
t.add({S::Processing, E::ProcessComplete, S::ProcessComplete, std::nullopt});
|
||||
|
||||
// PAUSED:
|
||||
t.add({S::Paused, E::Resume, S::Processing, std::nullopt});
|
||||
t.add({S::Paused, E::Stop, S::Stopping, std::nullopt});
|
||||
t.add({S::Paused, E::Abort, S::Aborting, std::nullopt});
|
||||
|
||||
// STOPPING:
|
||||
t.add({S::Stopping, E::ProcessComplete, S::ProcessComplete, std::nullopt});
|
||||
t.add({S::Stopping, E::Abort, S::Aborting, std::nullopt});
|
||||
|
||||
// ABORTING:
|
||||
t.add({S::Aborting, E::AbortComplete, S::ProcessComplete, std::nullopt});
|
||||
|
||||
// PROCESS-COMPLETE: terminal. No further transitions; deletion is via
|
||||
// the ProcessJobStore (which removes the FSM entirely).
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
ProcessJobStateMachine::ProcessJobStateMachine()
|
||||
: ProcessJobStateMachine(ProcessJobTransitionTable::default_table(),
|
||||
ProcessJobState::Queued) {}
|
||||
|
||||
ProcessJobStateMachine::ProcessJobStateMachine(ProcessJobTransitionTable table,
|
||||
ProcessJobState initial)
|
||||
: table_(std::move(table)), state_(initial) {}
|
||||
|
||||
void ProcessJobStateMachine::transition(ProcessJobState next,
|
||||
ProcessJobEvent trigger) {
|
||||
if (state_ == next) return;
|
||||
const ProcessJobState prev = state_;
|
||||
state_ = next;
|
||||
if (on_change_) on_change_(prev, next, trigger);
|
||||
}
|
||||
|
||||
const ProcessJobTransition* ProcessJobStateMachine::fire(ProcessJobEvent on) {
|
||||
const ProcessJobTransition* row = table_.find(state_, on);
|
||||
if (!row) return nullptr;
|
||||
if (row->to) transition(*row->to, on);
|
||||
return row;
|
||||
}
|
||||
|
||||
HostCmdAck ProcessJobStateMachine::on_host_command(ProcessJobEvent event) {
|
||||
const auto* row = fire(event);
|
||||
if (!row) return HostCmdAck::CannotDoNow;
|
||||
if (row->ack_code) return static_cast<HostCmdAck>(*row->ack_code);
|
||||
return HostCmdAck::Accept;
|
||||
}
|
||||
|
||||
bool ProcessJobStateMachine::on_internal(ProcessJobEvent event) {
|
||||
return fire(event) != nullptr;
|
||||
}
|
||||
|
||||
} // namespace secsgem::gem
|
||||
+67
-12
@@ -107,21 +107,60 @@ void Connection::on_payload(std::error_code ec, std::size_t) {
|
||||
close("read payload: " + ec.message());
|
||||
return;
|
||||
}
|
||||
Frame frame;
|
||||
try {
|
||||
handle_frame(Frame::decode(payload_.data(), payload_.size()));
|
||||
frame = Frame::decode(payload_.data(), payload_.size());
|
||||
} catch (const std::exception& e) {
|
||||
close(std::string("decode: ") + e.what());
|
||||
return;
|
||||
}
|
||||
// E37 §7.7: a non-SECS-II PType must be rejected with Reject.req.
|
||||
// Convention (matches the EntityNotSelected emission below): the
|
||||
// offending header byte is echoed in byte2 and the reason code goes
|
||||
// in byte3.
|
||||
if (frame.header.ptype != kPTypeSecsII) {
|
||||
log("!! unsupported PType=" + std::to_string(frame.header.ptype) +
|
||||
"; emitting Reject.req");
|
||||
send_frame(Frame(Header::control(
|
||||
SType::RejectReq, frame.header.system_bytes, frame.header.session_id,
|
||||
frame.header.ptype,
|
||||
static_cast<uint8_t>(RejectReason::PtypeNotSupported))));
|
||||
read_length();
|
||||
return;
|
||||
}
|
||||
handle_frame(std::move(frame));
|
||||
read_length();
|
||||
}
|
||||
|
||||
void Connection::handle_frame(Frame frame) {
|
||||
if (frame.header.stype == SType::Data) {
|
||||
handle_data(frame);
|
||||
} else {
|
||||
handle_control(frame);
|
||||
return;
|
||||
}
|
||||
// E37 §7.7: any SType the receiver does not implement must be answered
|
||||
// with Reject.req (reason=StypeNotSupported). Defined SType values are
|
||||
// {1,2,3,4,5,6,7,9}; anything else here is unsupported.
|
||||
switch (frame.header.stype) {
|
||||
case SType::SelectReq:
|
||||
case SType::SelectRsp:
|
||||
case SType::DeselectReq:
|
||||
case SType::DeselectRsp:
|
||||
case SType::LinktestReq:
|
||||
case SType::LinktestRsp:
|
||||
case SType::RejectReq:
|
||||
case SType::SeparateReq:
|
||||
handle_control(frame);
|
||||
return;
|
||||
case SType::Data:
|
||||
return; // already handled above
|
||||
}
|
||||
const uint8_t bad_stype = static_cast<uint8_t>(frame.header.stype);
|
||||
log("!! unsupported SType=" + std::to_string(bad_stype) +
|
||||
"; emitting Reject.req");
|
||||
send_frame(Frame(Header::control(
|
||||
SType::RejectReq, frame.header.system_bytes, frame.header.session_id,
|
||||
bad_stype,
|
||||
static_cast<uint8_t>(RejectReason::StypeNotSupported))));
|
||||
}
|
||||
|
||||
void Connection::handle_data(const Frame& frame) {
|
||||
@@ -210,11 +249,19 @@ void Connection::handle_control(const Frame& frame) {
|
||||
const Header& h = frame.header;
|
||||
switch (h.stype) {
|
||||
case SType::SelectReq: {
|
||||
log("<- Select.req");
|
||||
// E37 §7.2: a Select.req while already SELECTED is answered with
|
||||
// SelectStatus::AlreadyActive; we do NOT transition (we're already
|
||||
// there) and we do NOT call the selected handler twice.
|
||||
const auto status = (state_ == State::Selected)
|
||||
? SelectStatus::AlreadyActive
|
||||
: SelectStatus::Ok;
|
||||
log(std::string("<- Select.req") +
|
||||
(status == SelectStatus::AlreadyActive ? " (already SELECTED)" : ""));
|
||||
send_frame(Frame(Header::control(SType::SelectRsp, h.system_bytes, h.session_id, 0,
|
||||
static_cast<uint8_t>(SelectStatus::Ok))));
|
||||
log("-> Select.rsp (Ok)");
|
||||
enter_selected();
|
||||
static_cast<uint8_t>(status))));
|
||||
log(std::string("-> Select.rsp (") +
|
||||
(status == SelectStatus::Ok ? "Ok" : "AlreadyActive") + ")");
|
||||
if (status == SelectStatus::Ok) enter_selected();
|
||||
break;
|
||||
}
|
||||
case SType::SelectRsp: {
|
||||
@@ -231,11 +278,19 @@ void Connection::handle_control(const Frame& frame) {
|
||||
break;
|
||||
}
|
||||
case SType::DeselectReq: {
|
||||
log("<- Deselect.req");
|
||||
// E37 §7.4: Deselect.req while NOT_SELECTED returns NotEstablished
|
||||
// and leaves state untouched.
|
||||
const auto status = (state_ == State::Selected)
|
||||
? DeselectStatus::Ok
|
||||
: DeselectStatus::NotEstablished;
|
||||
log(std::string("<- Deselect.req") +
|
||||
(status == DeselectStatus::NotEstablished ? " (not SELECTED)" : ""));
|
||||
send_frame(Frame(Header::control(SType::DeselectRsp, h.system_bytes, h.session_id, 0,
|
||||
static_cast<uint8_t>(DeselectStatus::Ok))));
|
||||
state_ = State::NotSelected;
|
||||
arm_t7();
|
||||
static_cast<uint8_t>(status))));
|
||||
if (status == DeselectStatus::Ok) {
|
||||
state_ = State::NotSelected;
|
||||
arm_t7();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SType::DeselectRsp: {
|
||||
@@ -268,7 +323,7 @@ void Connection::handle_control(const Frame& frame) {
|
||||
break;
|
||||
}
|
||||
case SType::Data:
|
||||
break; // unreachable
|
||||
break; // unreachable: SType::Data is dispatched to handle_data
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -128,6 +128,10 @@ Item decode_at(const uint8_t* data, std::size_t len, std::size_t& pos) {
|
||||
switch (fmt) {
|
||||
case Format::ASCII:
|
||||
return Item::ascii(std::string(reinterpret_cast<const char*>(p), length));
|
||||
case Format::JIS8:
|
||||
return Item::jis8(std::string(reinterpret_cast<const char*>(p), length));
|
||||
case Format::C2:
|
||||
return Item::c2(read_array<uint16_t>(p, length));
|
||||
case Format::Binary:
|
||||
return Item::binary(std::vector<uint8_t>(p, p + length));
|
||||
case Format::Boolean:
|
||||
|
||||
Reference in New Issue
Block a user