ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
packml.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file packml.h
6 * @brief PackML / OMAC packaging-machine state model (ISA-TR88.00.02) - the state engine + PackTags.
7 *
8 * Reference standard: ISA-TR88.00.02-2022, "Machine and Unit States: An Implementation Example of ISA-88"
9 * (ISBN 978-1-64331-224-8) - the OMAC PackML state model and PackTags. The 17-state model, the numeric
10 * StateCurrent values, the command set, and the transition graph below follow that document.
11 *
12 * PackML is the OMAC / ISA-88 state model that packaging and process machines expose so a line
13 * controller (usually over OPC UA) can command and observe them uniformly. Two pieces:
14 *
15 * - the **state machine**: 17 states (Stopped/Idle/Execute/Held/Suspended/Complete/Aborted plus the
16 * transient "acting" states Starting/Resetting/Holding/... that auto-advance when their action
17 * finishes) driven by the control commands (Reset/Start/Stop/Hold/Unhold/Suspend/Unsuspend/Abort/
18 * Clear). Stop and Abort are the near-universal transitions to the shutdown / fault branches.
19 * - the **PackTags**: the standard Command / Status / Admin tag groups the model surfaces - the current
20 * state + unit mode, the requested/actual machine speed, and the production counters.
21 *
22 * This is a pure, fixed-BSS service: no heap, no I/O. The state engine (protocore_packml_command /
23 * protocore_packml_state_complete) is a pure transition table. The owned service (protocore_packml_svc_*) layers the
24 * PackTags + counters + timers on top for a machine to run directly. Host-tested (native_packml).
25 *
26 * @author Douglas Quigg (dstroy0)
27 * @date 2026
28 */
29
30#ifndef PROTOCORE_PACKML_H
31#define PROTOCORE_PACKML_H
32
33#include "protocore_config.h" // the entry point: protocore_types.h for the widths
34
35#if PROTOCORE_ENABLE_PACKML
36
37EMBED_BEGIN_DECLS
38
39/**
40 * @brief PackML unit / machine state. The underlying value is the ISA-TR88.00.02 StateCurrent wire number
41 * (1..17), so casting to the byte a PackTags Status.StateCurrent carries is a no-op at the boundary.
42 */
43EMBED_ENUM_BYTE(PackMlState,
44 PACK_ML_STATE_UNDEFINED = 0, ///< not yet initialized
45 PACK_ML_STATE_CLEARING = 1, ///< acting: clearing a fault (Aborted -> Stopped)
46 PACK_ML_STATE_STOPPED = 2, ///< wait: powered, safe, not producing
47 PACK_ML_STATE_STARTING = 3, ///< acting: Idle -> Execute
48 PACK_ML_STATE_IDLE = 4, ///< wait: ready to start
49 PACK_ML_STATE_SUSPENDED = 5, ///< wait: paused by an external condition (starved/blocked)
50 PACK_ML_STATE_EXECUTE = 6, ///< wait/producing: the machine does its work
51 PACK_ML_STATE_STOPPING = 7, ///< acting: -> Stopped
52 PACK_ML_STATE_ABORTING = 8, ///< acting: -> Aborted (fault)
53 PACK_ML_STATE_ABORTED = 9, ///< wait: faulted; needs Clear
54 PACK_ML_STATE_HOLDING = 10, ///< acting: Execute -> Held
55 PACK_ML_STATE_HELD = 11, ///< wait: production paused by the operator
56 PACK_ML_STATE_UNHOLDING = 12, ///< acting: Held -> Execute
57 PACK_ML_STATE_SUSPENDING = 13, ///< acting: Execute -> Suspended
58 PACK_ML_STATE_UNSUSPENDING = 14, ///< acting: Suspended -> Execute
59 PACK_ML_STATE_RESETTING = 15, ///< acting: Stopped/Complete -> Idle
60 PACK_ML_STATE_COMPLETING = 16, ///< acting: Execute (production done) -> Complete
61 PACK_ML_STATE_COMPLETE = 17 ///< wait: the production run finished
62);
63
64/** @brief PackML control command (the Command.CntrlCmd tag). Value is the conventional CntrlCmd number. */
65EMBED_ENUM_BYTE(PackMlCommand, PACK_ML_COMMAND_PROTOCORE_NONE = 0, PACK_ML_COMMAND_RESET = 1, PACK_ML_COMMAND_START = 2,
66 PACK_ML_COMMAND_STOP = 3, PACK_ML_COMMAND_HOLD = 4, PACK_ML_COMMAND_UNHOLD = 5,
67 PACK_ML_COMMAND_SUSPEND = 6, PACK_ML_COMMAND_UNSUSPEND = 7, PACK_ML_COMMAND_ABORT = 8,
68 PACK_ML_COMMAND_CLEAR = 9);
69
70/** @brief PackML unit mode (Command.UnitMode / Status.UnitModeCurrent). Producing is the full state model. */
71EMBED_ENUM_BYTE(PackMlMode, PACK_ML_MODE_PRODUCING = 1, PACK_ML_MODE_MAINTENANCE = 2, PACK_ML_MODE_MANUAL = 3);
72
73// ---------------------------------------------------------------------------
74// Pure state engine (no state stored here - the caller holds the current state)
75// ---------------------------------------------------------------------------
76
77/**
78 * @brief Apply a control command to a state and return the resulting state.
79 *
80 * Stop and Abort are the universal transitions (Abort from every state except the abort branch; Stop from
81 * every state except the abort branch and the already-stopping/stopped/clearing states). Otherwise only the
82 * command valid in @p s per the model transitions; an invalid command returns @p s unchanged.
83 */
84PackMlState protocore_packml_command(PackMlState s, PackMlCommand c);
85
86/**
87 * @brief Advance an "acting" (transient) state to its target once its action completes (the SC / State
88 * Complete transition, e.g. Starting -> Execute, Aborting -> Aborted). A "wait" state is returned
89 * unchanged - only acting states auto-advance.
90 */
91PackMlState protocore_packml_state_complete(PackMlState s);
92
93/** @brief The production cycle finished: Execute -> Completing. Any other state is returned unchanged. */
94PackMlState protocore_packml_execute_complete(PackMlState s);
95
96/** @brief True if @p s is an acting (transient, auto-advancing) state rather than a wait (stable) state. */
97embed_bool protocore_packml_is_acting(PackMlState s);
98
99/** @brief True if @p c is a legal command in @p s (i.e. protocore_packml_command would change the state). */
100embed_bool protocore_packml_command_valid(PackMlState s, PackMlCommand c);
101
102/** @brief Human-readable state name (e.g. "Execute") for an HMI / log. Never null. */
103const char *protocore_packml_state_name(PackMlState s);
104
105/** @brief Human-readable command name (e.g. "Start"). Never null. */
106const char *protocore_packml_command_name(PackMlCommand c);
107
108// ---------------------------------------------------------------------------
109// Owned service: the PackTags + counters + timers a machine runs directly (fixed BSS, one owner)
110// ---------------------------------------------------------------------------
111
112/** @brief Status PackTags snapshot (Status.*). */
113typedef struct
114{
115 PackMlState state_current; ///< Status.StateCurrent
116 PackMlMode unit_mode_current; ///< Status.UnitModeCurrent
117 float mach_speed_actual; ///< Status.MachSpeedActual
118 uint32_t state_current_ms; ///< time in the current state (ms), Admin.StateCurrentTime
119 uint32_t acc_time_since_reset_ms; ///< Admin.AccTimeSinceReset
120 uint32_t prod_processed; ///< Admin.ProdProcessedCount
121 uint32_t prod_defective; ///< Admin.ProdDefectiveCount
122} PackMlStatus;
123
124/** @brief Initialize the service: state = Stopped, unit mode @p mode, counters/speed cleared. */
125void protocore_packml_svc_init(PackMlMode mode);
126
127/**
128 * @brief Apply a control command (Command.CntrlCmd) to the running service.
129 * @return true if the command was legal in the current state and the state advanced.
130 */
131embed_bool protocore_packml_svc_command(PackMlCommand c);
132
133/**
134 * @brief Signal that the current acting state's action finished (the machine's State-Complete). Advances
135 * an acting state to its target; a no-op in a wait state.
136 * @return the new state.
137 */
138PackMlState protocore_packml_svc_state_complete(void);
139
140/**
141 * @brief Signal one production unit finished while in Execute: increments ProdProcessedCount (and, if
142 * @p defective, ProdDefectiveCount). Does not itself leave Execute (call protocore_packml_svc_complete_run
143 * to end the run).
144 */
145void protocore_packml_svc_count(embed_bool defective);
146
147/** @brief End the production run: Execute -> Completing (then State-Complete carries it to Complete). */
148embed_bool protocore_packml_svc_complete_run(void);
149
150/** @brief Request a unit-mode change. Allowed only in a stable, non-producing state (Stopped/Idle/Aborted). */
151embed_bool protocore_packml_svc_set_mode(PackMlMode mode);
152
153/** @brief Set the commanded machine speed (Command.MachSpeed); reflected as MachSpeedActual while in Execute. */
154void protocore_packml_svc_set_speed(float mach_speed);
155
156/** @brief Current state. */
157PackMlState protocore_packml_svc_state(void);
158
159/** @brief Fill @p out with the current Status/Admin tag snapshot. */
160void protocore_packml_svc_status(PackMlStatus *out);
161
162EMBED_END_DECLS
163
164#endif // PROTOCORE_ENABLE_PACKML
165
166#endif // PROTOCORE_PACKML_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.