Files
raphael 90c177b7ce 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>
2026-06-07 21:00:32 +02:00

171 lines
6.8 KiB
C++

#pragma once
#include <cstdint>
#include <stdexcept>
#include <string>
#include <variant>
#include <vector>
namespace secsgem::secs2 {
// SECS-II data item format codes (the 6-bit value, before being shifted left
// two bits to make the format byte). Values are the canonical octal codes from
// SEMI E5.
enum class Format : uint8_t {
List = 000, // 0
Binary = 010, // 8
Boolean = 011, // 9
ASCII = 020, // 16
JIS8 = 021, // 17 — 8-bit JIS (single-byte Japanese text)
C2 = 022, // 18 — 2-byte Unicode code points (big-endian)
I8 = 030, // 24
I1 = 031, // 25
I2 = 032, // 26
I4 = 034, // 28
F8 = 040, // 32
F4 = 044, // 36
U8 = 050, // 40
U1 = 051, // 41
U2 = 052, // 42
U4 = 054, // 44
};
inline const char* format_name(Format f) {
switch (f) {
case Format::List: return "L";
case Format::Binary: return "B";
case Format::Boolean: return "BOOLEAN";
case Format::ASCII: return "A";
case Format::JIS8: return "J";
case Format::C2: return "C";
case Format::I8: return "I8";
case Format::I1: return "I1";
case Format::I2: return "I2";
case Format::I4: return "I4";
case Format::F8: return "F8";
case Format::F4: return "F4";
case Format::U8: return "U8";
case Format::U1: return "U1";
case Format::U2: return "U2";
case Format::U4: return "U4";
}
return "?";
}
// Number of bytes one element of the given format occupies on the wire.
// Lists are special (their length is an element count, not a byte count) and
// return 0 here.
inline std::size_t element_size(Format f) {
switch (f) {
case Format::List: return 0;
case Format::ASCII:
case Format::JIS8:
case Format::Binary:
case Format::Boolean:
case Format::U1:
case Format::I1: return 1;
case Format::C2:
case Format::U2:
case Format::I2: return 2;
case Format::U4:
case Format::I4:
case Format::F4: return 4;
case Format::U8:
case Format::I8:
case Format::F8: return 8;
}
return 0;
}
// A SECS-II data item: a typed, possibly nested value. Lists hold child items;
// every other format holds a homogeneous array of scalars (a single scalar is
// just an array of length one). The active variant alternative is kept in sync
// with `format_`; several formats (Binary, Boolean, U1) share the same C++
// storage type and are disambiguated by `format_`.
class Item {
public:
using List = std::vector<Item>;
using Storage = std::variant<
List, // List
std::string, // ASCII, JIS-8
std::vector<uint8_t>, // Binary, Boolean, U1
std::vector<int8_t>, // I1
std::vector<int16_t>, // I2
std::vector<int32_t>, // I4
std::vector<int64_t>, // I8
std::vector<uint16_t>, // U2, C2 (Unicode code points)
std::vector<uint32_t>, // U4
std::vector<uint64_t>, // U8
std::vector<float>, // F4
std::vector<double>>; // F8
Item() : format_(Format::List), data_(List{}) {}
Format format() const { return format_; }
bool is_list() const { return format_ == Format::List; }
// Number of elements: child count for lists, character count for ASCII,
// array length for numeric/binary formats.
std::size_t size() const {
return std::visit([](const auto& v) { return v.size(); }, data_);
}
// --- Factory functions -------------------------------------------------
static Item list(List items) { return Item(Format::List, std::move(items)); }
static Item ascii(std::string s) { return Item(Format::ASCII, std::move(s)); }
static Item jis8(std::string s) { return Item(Format::JIS8, std::move(s)); }
static Item c2(std::vector<uint16_t> code_points) { return Item(Format::C2, std::move(code_points)); }
static Item binary(std::vector<uint8_t> b) { return Item(Format::Binary, std::move(b)); }
static Item boolean(std::vector<uint8_t> b) { return Item(Format::Boolean, std::move(b)); }
static Item boolean(bool b) { return Item(Format::Boolean, std::vector<uint8_t>{static_cast<uint8_t>(b ? 1 : 0)}); }
static Item u1(std::vector<uint8_t> v) { return Item(Format::U1, std::move(v)); }
static Item u2(std::vector<uint16_t> v) { return Item(Format::U2, std::move(v)); }
static Item u4(std::vector<uint32_t> v) { return Item(Format::U4, std::move(v)); }
static Item u8(std::vector<uint64_t> v) { return Item(Format::U8, std::move(v)); }
static Item i1(std::vector<int8_t> v) { return Item(Format::I1, std::move(v)); }
static Item i2(std::vector<int16_t> v) { return Item(Format::I2, std::move(v)); }
static Item i4(std::vector<int32_t> v) { return Item(Format::I4, std::move(v)); }
static Item i8(std::vector<int64_t> v) { return Item(Format::I8, std::move(v)); }
static Item f4(std::vector<float> v) { return Item(Format::F4, std::move(v)); }
static Item f8(std::vector<double> v) { return Item(Format::F8, std::move(v)); }
// Scalar convenience overloads.
static Item u1(uint8_t v) { return u1(std::vector<uint8_t>{v}); }
static Item u2(uint16_t v) { return u2(std::vector<uint16_t>{v}); }
static Item u4(uint32_t v) { return u4(std::vector<uint32_t>{v}); }
static Item u8(uint64_t v) { return u8(std::vector<uint64_t>{v}); }
static Item i1(int8_t v) { return i1(std::vector<int8_t>{v}); }
static Item i2(int16_t v) { return i2(std::vector<int16_t>{v}); }
static Item i4(int32_t v) { return i4(std::vector<int32_t>{v}); }
static Item i8(int64_t v) { return i8(std::vector<int64_t>{v}); }
static Item f4(float v) { return f4(std::vector<float>{v}); }
static Item f8(double v) { return f8(std::vector<double>{v}); }
// Construct directly from a format and matching storage (used by the decoder).
static Item raw(Format f, Storage s) { return Item(f, std::move(s)); }
// --- Typed accessors (throw std::bad_variant_access on mismatch) --------
const List& as_list() const { return std::get<List>(data_); }
List& as_list() { return std::get<List>(data_); }
const std::string& as_ascii() const { return std::get<std::string>(data_); }
// JIS-8 shares the std::string storage slot (it's a single-byte
// encoding like ASCII); callers disambiguate via `format()`.
const std::string& as_jis8() const { return std::get<std::string>(data_); }
// C2 (Unicode) shares the std::vector<uint16_t> storage with U2.
const std::vector<uint16_t>& as_c2() const { return std::get<std::vector<uint16_t>>(data_); }
const std::vector<uint8_t>& as_bytes() const { return std::get<std::vector<uint8_t>>(data_); }
const Storage& storage() const { return data_; }
bool operator==(const Item&) const = default;
private:
Item(Format f, Storage s) : format_(f), data_(std::move(s)) {}
Format format_;
Storage data_;
};
} // namespace secsgem::secs2