Expand description
Layer 2 — the sans-I/O protocol engine.
Engine is a pure state machine. It owns no socket, reads no clock and spawns no task:
the driver feeds it Inputs and drains Outputs. That is what makes every timing
rule in OCPP — message timeouts, the linear transaction-retry schedule, boot back-off —
testable in microseconds, and what lets the same code run on Tokio, inside an embassy
firmware loop, or in a browser.
Every entry point takes the driver’s current Instant: the engine has no clock of its
own, and a deadline armed against a stale one fires the moment it is armed.
use ocpp_kit::Version;
use ocpp_kit::engine::{Engine, EngineConfig, Input, Instant, Output, Role};
use serde_json::value::RawValue;
let now = Instant::ZERO;
let mut engine = Engine::new(EngineConfig::new(Role::ChargingStation, Version::V2_1));
engine.handle(now, Input::Connected { version: Version::V2_1 });
let payload = RawValue::from_string(
r#"{"reason":"PowerUp","chargingStation":{"model":"M1","vendorName":"ACME"}}"#.into(),
)
.unwrap();
engine.call(now, "BootNotification", payload).unwrap();
let frame = engine
.drain()
.into_iter()
.find_map(|output| match output {
Output::Transmit(text) => Some(text),
_ => None,
})
.unwrap();
assert!(frame.starts_with(r#"[2,"#));In a test, Sim owns the clock for you and the same session reads
as a transcript.
§Rules this layer enforces
- One outstanding
CALLper direction (2.x Part 4 §4.1.1SHALL NOT; 1.6J §4.1.1SHOULD NOT— henceInboundConcurrency). Our own extra calls are queued, never rejected. SENDis exempt and is never answered (Part 4 §4.2.4, FR.07).- Unknown message type numbers are ignored on 1.6J and 2.1 and answered with
MessageTypeNotSupportedon 2.0.1 (§4.4). - Message timeout, freeing the slot and starting the next queued call.
- Transaction-message retries only, on the linear schedule of 1.6 §3.7.1 and the 2.x
MessageAttempts[TransactionEvent]variables, in chronological order, skipping a message once its attempts are exhausted. - Durable offline queueing of transaction messages (E04/E08/E12) via
MessageStore—MemStorein memory,FileStoreon disk, or your own. - The boot state machine (B01–B04) on both sides, including the CSMS’s
SecurityErroranswer to an unsolicited call from aPendingstation (B02.FR.09). - Graceful drain: finish the in-flight call, flush the queue, then close.
Structs§
- Backoff
- The three
OCPPCommCtrlrvariables that drive reconnect timing (Part 4 §5.4). - Call
Options - Per-call overrides.
- Call
Outcome - The final result of a call the application started.
- Call
Token - A handle to a call the application asked for.
- Clock
Sample - An authoritative timestamp observed in a
BootNotificationResponseorHeartbeatResponse. - Engine
- The sans-I/O OCPP protocol engine.
- Engine
Config - Engine configuration.
- File
Store - A
MessageStorethat survives a power cut, for the station side. - Incoming
Request - A request the peer sent that the application must handle.
- Instant
- A point on a monotonic clock, in milliseconds since an arbitrary origin.
- MemStore
- An in-memory
MessageStore. The default, and all a CSMS ever needs. - Offline
Policy - What happens to outgoing calls while the connection is down.
- Queued
Call - One queued outgoing message.
- Retry
Policy - How often, and how far apart, transaction-related messages are retried.
- Store
Error - A durable store failed.
Enums§
- Answer
- How a message the application started ended, when it ended well.
- Boot
State - The boot state machine (OCPP 2.x block B; 1.6 §4.2).
- Call
Failure - Why a call did not succeed.
- Close
Reason - Why the engine wants the connection closed.
- Engine
Error - Why a command was refused.
- Heartbeat
Policy - Whether the engine sends
Heartbeaton its own. - Inbound
Concurrency - What to do when the peer breaks the one-outstanding-
CALLrule. - Input
- Events the driver feeds into the engine, via
Engine::handle. - Output
- What the driver must do next.
- Protocol
Violation - Something the peer did that the specification forbids, but which is not fatal.
- Role
- Which side of the connection this engine is.
- Timer
- The timers the engine asks the driver to run.
Traits§
- Message
Store - Durable storage for transaction-related messages.
Type Aliases§
- Seq
- Monotonic sequence number of a stored message.