1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//! OCPP protocol versions and WebSocket subprotocol negotiation.
use alloc::vec::Vec;
use core::fmt;
use core::str::FromStr;
/// A supported OCPP version.
///
/// The ordering is oldest → newest, so `max()` picks the most capable version.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Version {
/// OCPP 1.6J (edition 2 + errata, plus the Security Whitepaper edition 2 extensions).
V1_6,
/// OCPP 2.0.1.
V2_0_1,
/// OCPP 2.1.
V2_1,
}
impl Version {
/// Every version this crate implements, oldest first.
pub const ALL: &'static [Version] = &[Version::V1_6, Version::V2_0_1, Version::V2_1];
/// The WebSocket subprotocol token, e.g. `ocpp2.1` (Part 4 §3.1.2).
#[must_use]
pub const fn subprotocol(self) -> &'static str {
match self {
Version::V1_6 => "ocpp1.6",
Version::V2_0_1 => "ocpp2.0.1",
Version::V2_1 => "ocpp2.1",
}
}
/// A short, filesystem-safe identifier: `v1_6`, `v2_0_1`, `v2_1`.
///
/// Used for schema directories, metric labels and log fields.
#[must_use]
pub const fn slug(self) -> &'static str {
match self {
Version::V1_6 => "v1_6",
Version::V2_0_1 => "v2_0_1",
Version::V2_1 => "v2_1",
}
}
/// Parses a WebSocket subprotocol token.
#[must_use]
pub fn from_subprotocol(token: &str) -> Option<Self> {
match token.trim() {
"ocpp1.6" => Some(Version::V1_6),
"ocpp2.0.1" => Some(Version::V2_0_1),
"ocpp2.1" => Some(Version::V2_1),
_ => None,
}
}
/// Whether this version has `CALLRESULTERROR` (5) and `SEND` (6).
#[must_use]
pub const fn has_extended_message_types(self) -> bool {
matches!(self, Version::V2_1)
}
/// Whether this version defines the `RpcFrameworkError` error code.
///
/// 1.6J does not; framing failures are reported as `GenericError` there.
#[must_use]
pub const fn has_rpc_framework_error(self) -> bool {
!matches!(self, Version::V1_6)
}
/// Whether an unreadable `MessageId` must be answered with the literal id `"-1"`
/// (2.x Part 4 §4.1.1). 1.6J has no such rule.
#[must_use]
pub const fn uses_unreadable_message_id(self) -> bool {
!matches!(self, Version::V1_6)
}
/// Whether the one-outstanding-`CALL` rule is a `SHALL NOT` (2.x, Part 4 §4.1.1) rather
/// than 1.6J's `SHOULD NOT`.
#[must_use]
pub const fn strict_synchronicity(self) -> bool {
!matches!(self, Version::V1_6)
}
/// Whether RFC 7692 `permessage-deflate` is part of the specification.
///
/// 2.1 Part 4 §3.4 Table 2: the CSMS and a Local Controller **SHALL** support it; a
/// Charging Station **MAY**. 1.6J §5.1 recommends no compression, and 2.0.1 is silent.
#[must_use]
pub const fn supports_compression(self) -> bool {
matches!(self, Version::V2_1)
}
}
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Version::V1_6 => "1.6",
Version::V2_0_1 => "2.0.1",
Version::V2_1 => "2.1",
})
}
}
impl FromStr for Version {
type Err = UnknownVersion;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value.trim() {
"1.6" | "1.6J" | "1.6j" | "ocpp1.6" => Ok(Version::V1_6),
"2.0.1" | "ocpp2.0.1" => Ok(Version::V2_0_1),
"2.1" | "ocpp2.1" => Ok(Version::V2_1),
_ => Err(UnknownVersion),
}
}
}
/// A version string this crate does not recognise.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UnknownVersion;
impl fmt::Display for UnknownVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("unknown OCPP version")
}
}
#[cfg(feature = "std")]
impl std::error::Error for UnknownVersion {}
/// The `Sec-WebSocket-Protocol` value a client offers, in preference order.
///
/// OCPP 2.1 Part 4 §3.2 recommends that a 2.1 peer also offer 2.0.1, so a station can talk
/// to a CSMS that has not migrated yet.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Subprotocol {
offered: Vec<Version>,
}
impl Subprotocol {
/// Offers the given versions, most preferred first. Duplicates are removed.
#[must_use]
pub fn new(versions: impl IntoIterator<Item = Version>) -> Self {
let mut offered = Vec::new();
for version in versions {
if !offered.contains(&version) {
offered.push(version);
}
}
Self { offered }
}
/// The versions offered, most preferred first.
#[must_use]
pub fn offered(&self) -> &[Version] {
&self.offered
}
/// The `Sec-WebSocket-Protocol` header value.
#[must_use]
pub fn header_value(&self) -> alloc::string::String {
let mut out = alloc::string::String::new();
for (index, version) in self.offered.iter().enumerate() {
if index > 0 {
out.push_str(", ");
}
out.push_str(version.subprotocol());
}
out
}
/// Picks the version a server should select: the first *client* preference that the
/// server also supports (Part 4 §3.1.2 — the server selects exactly one).
#[must_use]
pub fn select(&self, supported: &[Version]) -> Option<Version> {
self.offered.iter().copied().find(|v| supported.contains(v))
}
/// Validates a server's selection against what was offered.
///
/// Part 4 §3.1.2: the server must echo exactly one of the offered subprotocols. A server
/// that answers with something else, or with nothing, is a negotiation failure.
pub fn accept(&self, selected: Option<&str>) -> Result<Version, NegotiationError> {
let Some(token) = selected else {
return Err(NegotiationError::NoSubprotocol);
};
let version =
Version::from_subprotocol(token).ok_or(NegotiationError::UnsupportedSubprotocol)?;
if self.offered.contains(&version) {
Ok(version)
} else {
Err(NegotiationError::NotOffered(version))
}
}
}
impl Default for Subprotocol {
/// Offers every version this crate implements, newest first.
fn default() -> Self {
Self::new([Version::V2_1, Version::V2_0_1, Version::V1_6])
}
}
/// Why subprotocol negotiation failed.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum NegotiationError {
/// The server did not send `Sec-WebSocket-Protocol`. Part 4 §3.1.2 requires it; the
/// connection must be closed.
NoSubprotocol,
/// The server selected a subprotocol that is not an OCPP one.
UnsupportedSubprotocol,
/// The server selected a version the client did not offer.
NotOffered(Version),
}
impl fmt::Display for NegotiationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NegotiationError::NoSubprotocol => {
f.write_str("server did not select a WebSocket subprotocol (Part 4 §3.1.2)")
}
NegotiationError::UnsupportedSubprotocol => {
f.write_str("server selected a subprotocol that is not OCPP")
}
NegotiationError::NotOffered(version) => {
write!(f, "server selected OCPP {version}, which was not offered")
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for NegotiationError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn negotiation_follows_client_preference() {
let offer = Subprotocol::new([Version::V2_1, Version::V2_0_1, Version::V1_6]);
assert_eq!(offer.header_value(), "ocpp2.1, ocpp2.0.1, ocpp1.6");
assert_eq!(
offer.select(&[Version::V1_6, Version::V2_0_1]),
Some(Version::V2_0_1)
);
assert_eq!(offer.accept(Some("ocpp2.0.1")), Ok(Version::V2_0_1));
assert_eq!(offer.accept(None), Err(NegotiationError::NoSubprotocol));
assert_eq!(
offer.accept(Some("mqtt")),
Err(NegotiationError::UnsupportedSubprotocol)
);
let narrow = Subprotocol::new([Version::V1_6]);
assert_eq!(
narrow.accept(Some("ocpp2.1")),
Err(NegotiationError::NotOffered(Version::V2_1))
);
}
}