iec61850-kit 0.5.0

IEC 61850 in Rust: GOOSE and Sampled Values on capture files and live interfaces, an MMS client and server over the full OSI stack, and SCL loading and editing. Panic-free, sans-IO, no_std-capable.
Documentation
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
//! IEC 61850 controls: the `Oper`, `SBOw` and `Cancel` structures, and what comes back.
//!
//! Operating a breaker is an MMS `Write` to a structured variable under the `CO` functional
//! constraint โ€” `IED1LD0/CSWI1$CO$Pos$Oper` โ€” whose components IEC 61850-8-1 Annex E fixes
//! (Tables E.8 `SBOw`, E.9 `Oper`, E.10 `Cancel` โœ…; the component order from libiec61850's
//! `client_control.c` ๐ŸŒ).
//!
//! ```text
//! Oper   ::= { ctlVal, [operTm], origin { orCat, orIdent }, ctlNum, T, Test, Check }
//! SBOw   ::= the same
//! Cancel ::= the same without Check
//! ```
//!
//! Two things are easy to get wrong. `Check` is a **two-bit** bit string whose bit 0 is the
//! synchrocheck and bit 1 the interlock check ๐ŸŒ โ€” the reverse of the order prose lists them
//! in. And a negative answer to an enhanced-security control is not an error response: it
//! arrives later, unsolicited, as [`LastApplError`] carrying the [`AddCause`] that says why
//! ([8-1] Tables 76 and 77).

use alloc::string::String;
use alloc::vec::Vec;

use crate::common::{Edition, Error, Result, UtcTime};

pub use crate::common::ControlModel;
use crate::proto::data::{Typed, Value};

/// Who issued a control (`orCat`, IEC 61850-7-3).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum OriginCategory {
    NotSupported,
    BayControl,
    StationControl,
    #[default]
    RemoteControl,
    AutomaticBay,
    AutomaticStation,
    AutomaticRemote,
    Maintenance,
    Process,
    /// A value outside the enumeration, kept so it re-encodes as it arrived.
    Other(i64),
}

impl OriginCategory {
    /// From the wire value.
    pub const fn from_code(code: i64) -> OriginCategory {
        match code {
            0 => OriginCategory::NotSupported,
            1 => OriginCategory::BayControl,
            2 => OriginCategory::StationControl,
            3 => OriginCategory::RemoteControl,
            4 => OriginCategory::AutomaticBay,
            5 => OriginCategory::AutomaticStation,
            6 => OriginCategory::AutomaticRemote,
            7 => OriginCategory::Maintenance,
            8 => OriginCategory::Process,
            other => OriginCategory::Other(other),
        }
    }

    /// The wire value.
    pub const fn to_code(self) -> i64 {
        match self {
            OriginCategory::NotSupported => 0,
            OriginCategory::BayControl => 1,
            OriginCategory::StationControl => 2,
            OriginCategory::RemoteControl => 3,
            OriginCategory::AutomaticBay => 4,
            OriginCategory::AutomaticStation => 5,
            OriginCategory::AutomaticRemote => 6,
            OriginCategory::Maintenance => 7,
            OriginCategory::Process => 8,
            OriginCategory::Other(v) => v,
        }
    }
}

/// The originator of a control (`origin`): a category and an opaque identifier.
///
/// The identifier is octets rather than a string on purpose โ€” IEC 62351-6 ยง5.5 fills it with
/// a certificate serial number, which is not text.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Origin {
    /// Where the command came from.
    pub category: OriginCategory,
    /// `orIdent` โ€” who, as octets.
    pub identifier: Vec<u8>,
}

impl Origin {
    /// An originator with a textual identifier, which is what a tool usually has.
    pub fn new(category: OriginCategory, identifier: &str) -> Origin {
        Origin { category, identifier: identifier.as_bytes().to_vec() }
    }

    /// The `origin` structure.
    pub fn to_value(&self) -> Value {
        Value::Structure(alloc::vec![Value::Integer(self.category.to_code()), Value::OctetString(self.identifier.clone())])
    }

    /// Read an `origin` structure.
    pub fn from_value(v: &Value) -> Option<Origin> {
        let members = v.members()?;
        let category = match members.first()? {
            Value::Integer(i) => OriginCategory::from_code(*i),
            Value::Unsigned(u) => OriginCategory::from_code(i64::try_from(*u).ok()?),
            _ => return None,
        };
        let identifier = match members.get(1)? {
            Value::OctetString(b) => b.clone(),
            Value::VisibleString(s) | Value::MmsString(s) => s.as_bytes().to_vec(),
            _ => return None,
        };
        Some(Origin { category, identifier })
    }

    /// The identifier as text, when it is text.
    pub fn identifier_str(&self) -> Option<&str> {
        core::str::from_utf8(&self.identifier).ok()
    }
}

/// The `Check` field: the two conditions a server may be asked to verify before it operates.
///
/// A two-bit bit string. **Bit 0 is the synchrocheck and bit 1 the interlock check** ๐ŸŒ โ€”
/// getting them the wrong way round asks a substation to skip the check it was meant to make.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Check {
    /// Verify that the two sides are in synchronism before closing.
    pub synchro: bool,
    /// Verify the interlocking conditions.
    pub interlock: bool,
}

impl Check {
    /// Ask for neither check โ€” which is what most direct controls send.
    pub const NONE: Check = Check { synchro: false, interlock: false };
    /// Ask for both.
    pub const BOTH: Check = Check { synchro: true, interlock: true };

    /// The two-bit bit string.
    pub fn to_value(self) -> Value {
        let byte = u8::from(self.synchro) << 7 | u8::from(self.interlock) << 6;
        Value::BitString { unused: 6, bytes: alloc::vec![byte] }
    }

    /// Read the two-bit bit string. A bit string of another width is not a `Check`.
    pub fn from_value(v: &Value) -> Option<Check> {
        match v {
            Value::BitString { unused: 6, bytes } => {
                let b = *bytes.first()?;
                Some(Check { synchro: b & 0x80 != 0, interlock: b & 0x40 != 0 })
            }
            _ => None,
        }
    }
}

/// An `Oper`, `SBOw` or `Cancel` structure.
///
/// One type for all three, because Annex E gives them the same components and `Cancel` simply
/// stops before `Check` โ€” which is exactly the kind of near-duplication that turns into two
/// implementations disagreeing about `ctlNum`.
#[derive(Clone, Debug, PartialEq)]
pub struct ControlRequest {
    /// `ctlVal` โ€” the value to write. Its type is the CDC's: a boolean for `SPC`, a `Dbpos`
    /// bit string for `DPC`, an integer for `INC`, a float for `APC`.
    pub ctl_val: Value,
    /// `operTm` โ€” operate at this time instead of now. Present only on objects engineered
    /// for time-activated operate, and its presence *changes the structure*, so it is an
    /// explicit option rather than a defaulted field.
    pub oper_tm: Option<UtcTime>,
    /// `origin` โ€” who is asking.
    pub origin: Origin,
    /// `ctlNum` โ€” the sequence number tying a select, an operate and its termination
    /// together. Every request of one control sequence carries the same number.
    pub ctl_num: u8,
    /// `T` โ€” when the client issued this.
    pub t: UtcTime,
    /// `Test` โ€” this is a test command; a server not in test mode must refuse it.
    pub test: bool,
    /// `Check` โ€” omitted from a `Cancel`.
    pub check: Check,
}

impl ControlRequest {
    /// A request with the defaults a tool wants: remote control, no checks, not a test.
    pub fn new(ctl_val: Value, ctl_num: u8, t: UtcTime) -> ControlRequest {
        ControlRequest { ctl_val, oper_tm: None, origin: Origin::default(), ctl_num, t, test: false, check: Check::NONE }
    }

    /// The `Oper` / `SBOw` structure.
    pub fn to_value(&self) -> Value {
        let mut members = Vec::with_capacity(7);
        members.push(self.ctl_val.clone());
        if let Some(tm) = self.oper_tm {
            members.push(Value::UtcTime(tm));
        }
        members.push(self.origin.to_value());
        members.push(Value::Unsigned(u64::from(self.ctl_num)));
        members.push(Value::UtcTime(self.t));
        members.push(Value::Boolean(self.test));
        members.push(self.check.to_value());
        Value::Structure(members)
    }

    /// The `Cancel` structure: the same without `Check`.
    pub fn to_cancel_value(&self) -> Value {
        let mut v = self.to_value();
        if let Value::Structure(members) = &mut v {
            members.pop();
        }
        v
    }

    /// Read an `Oper`, `SBOw` or `Cancel` structure.
    ///
    /// `operTm` and `Check` are both optional *and* untagged, so the shape is resolved by
    /// counting members and checking types rather than by position alone โ€” which is the only
    /// way, since the structure carries no field names on the wire.
    pub fn from_value(v: &Value) -> Result<ControlRequest> {
        let members = v.members().ok_or(Error::InvalidValue("a control request is a structure"))?;
        let bad = || Error::InvalidValue("control request structure");
        let ctl_val = members.first().ok_or_else(bad)?.clone();
        let mut at = 1usize;
        // `operTm` is a UtcTime where `origin` is a structure: the two cannot be confused.
        let oper_tm = match members.get(at) {
            Some(Value::UtcTime(t)) => {
                at += 1;
                Some(*t)
            }
            _ => None,
        };
        let origin = Origin::from_value(members.get(at).ok_or_else(bad)?).ok_or_else(bad)?;
        at += 1;
        // `ctlNum` is present only on objects engineered with it, and `T` โ€” a `UtcTime` โ€”
        // follows it either way, so the two are told apart by type rather than by position.
        // An `Oper` echoed by a device without `ctlNum` is a valid `Oper`, and refusing it
        // turns a command termination into an unrecognised report.
        let ctl_num = match members.get(at) {
            Some(Value::UtcTime(_)) | None => 0,
            Some(v) => {
                at += 1;
                u8::try_from(v.as_u64().ok_or_else(bad)?).map_err(|_| bad())?
            }
        };
        let t = members.get(at).ok_or_else(bad)?.as_utc_time().ok_or_else(bad)?;
        at += 1;
        let test = members.get(at).ok_or_else(bad)?.as_bool().ok_or_else(bad)?;
        at += 1;
        // Absent on a `Cancel`, present on everything else.
        let check = match members.get(at) {
            Some(v) => Check::from_value(v).ok_or_else(bad)?,
            None => Check::NONE,
        };
        Ok(ControlRequest { ctl_val, oper_tm, origin, ctl_num, t, test, check })
    }
}

/// The `error` component of [`LastApplError`] (IEC 61850-8-1 Table 76).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum ControlError {
    #[default]
    NoError,
    Unknown,
    TimeoutTestNotOk,
    OperatorTestNotOk,
    /// A value outside the enumeration.
    Other(i64),
}

impl ControlError {
    /// From the wire value.
    pub const fn from_code(code: i64) -> ControlError {
        match code {
            0 => ControlError::NoError,
            1 => ControlError::Unknown,
            2 => ControlError::TimeoutTestNotOk,
            3 => ControlError::OperatorTestNotOk,
            other => ControlError::Other(other),
        }
    }

    /// The wire value.
    pub const fn to_code(self) -> i64 {
        match self {
            ControlError::NoError => 0,
            ControlError::Unknown => 1,
            ControlError::TimeoutTestNotOk => 2,
            ControlError::OperatorTestNotOk => 3,
            ControlError::Other(v) => v,
        }
    }
}

/// Why a control was refused or abandoned (`AddCause`, IEC 61850-8-1 Table 77).
///
/// This is the field that turns "the breaker did not close" into a diagnosis, so every value
/// is named rather than left as a number.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum AddCause {
    #[default]
    Unknown,
    NotSupported,
    BlockedBySwitchingHierarchy,
    SelectFailed,
    InvalidPosition,
    PositionReached,
    ParameterChangeInExecution,
    StepLimit,
    BlockedByMode,
    BlockedByProcess,
    BlockedByInterlocking,
    BlockedBySynchrocheck,
    CommandAlreadyInExecution,
    BlockedByHealth,
    OneOfNControl,
    AbortionByCancel,
    TimeLimitOver,
    AbortionByTrip,
    ObjectNotSelected,
    ObjectAlreadySelected,
    NoAccessAuthority,
    EndedWithOvershoot,
    AbortionDueToDeviation,
    AbortionByCommunicationLoss,
    AbortionByCommand,
    None,
    InconsistentParameters,
    LockedByOtherClient,
    /// A value above 27, outside the list this crate names. Edition 2.1 lets the list be
    /// extended.
    Other(i64),
}

impl AddCause {
    /// From the wire value.
    pub const fn from_code(code: i64) -> AddCause {
        match code {
            0 => AddCause::Unknown,
            1 => AddCause::NotSupported,
            2 => AddCause::BlockedBySwitchingHierarchy,
            3 => AddCause::SelectFailed,
            4 => AddCause::InvalidPosition,
            5 => AddCause::PositionReached,
            6 => AddCause::ParameterChangeInExecution,
            7 => AddCause::StepLimit,
            8 => AddCause::BlockedByMode,
            9 => AddCause::BlockedByProcess,
            10 => AddCause::BlockedByInterlocking,
            11 => AddCause::BlockedBySynchrocheck,
            12 => AddCause::CommandAlreadyInExecution,
            13 => AddCause::BlockedByHealth,
            14 => AddCause::OneOfNControl,
            15 => AddCause::AbortionByCancel,
            16 => AddCause::TimeLimitOver,
            17 => AddCause::AbortionByTrip,
            18 => AddCause::ObjectNotSelected,
            19 => AddCause::ObjectAlreadySelected,
            20 => AddCause::NoAccessAuthority,
            21 => AddCause::EndedWithOvershoot,
            22 => AddCause::AbortionDueToDeviation,
            23 => AddCause::AbortionByCommunicationLoss,
            24 => AddCause::AbortionByCommand,
            25 => AddCause::None,
            26 => AddCause::InconsistentParameters,
            27 => AddCause::LockedByOtherClient,
            other => AddCause::Other(other),
        }
    }

    /// The wire value.
    pub const fn to_code(self) -> i64 {
        match self {
            AddCause::Unknown => 0,
            AddCause::NotSupported => 1,
            AddCause::BlockedBySwitchingHierarchy => 2,
            AddCause::SelectFailed => 3,
            AddCause::InvalidPosition => 4,
            AddCause::PositionReached => 5,
            AddCause::ParameterChangeInExecution => 6,
            AddCause::StepLimit => 7,
            AddCause::BlockedByMode => 8,
            AddCause::BlockedByProcess => 9,
            AddCause::BlockedByInterlocking => 10,
            AddCause::BlockedBySynchrocheck => 11,
            AddCause::CommandAlreadyInExecution => 12,
            AddCause::BlockedByHealth => 13,
            AddCause::OneOfNControl => 14,
            AddCause::AbortionByCancel => 15,
            AddCause::TimeLimitOver => 16,
            AddCause::AbortionByTrip => 17,
            AddCause::ObjectNotSelected => 18,
            AddCause::ObjectAlreadySelected => 19,
            AddCause::NoAccessAuthority => 20,
            AddCause::EndedWithOvershoot => 21,
            AddCause::AbortionDueToDeviation => 22,
            AddCause::AbortionByCommunicationLoss => 23,
            AddCause::AbortionByCommand => 24,
            AddCause::None => 25,
            AddCause::InconsistentParameters => 26,
            AddCause::LockedByOtherClient => 27,
            AddCause::Other(v) => v,
        }
    }

    /// The edition that introduced this value.
    ///
    /// Edition 1 defines 0โ€“18, `Unknown` to `ObjectNotSelected` โš  (the IEC 61850-8-1 Ed2.1
    /// mapping table is paywalled; the boundary is engineering judgement). Edition 2 adds 19โ€“27
    /// ๐ŸŒ (libiec61850's `ControlAddCause`, and vendor documentation of the Edition 2 table).
    /// A value above 27 is read as Edition 2.1's, the edition that lets the list be extended.
    pub const fn since(self) -> Edition {
        match self.to_code() {
            0..=18 => Edition::Ed1,
            19..=27 => Edition::Ed2,
            _ => Edition::Ed2_1,
        }
    }

    /// This value, or the nearest one `edition` defines if it does not define this one.
    ///
    /// The only Edition 2 value with an Edition 1 counterpart is `ObjectAlreadySelected`,
    /// which maps to `SelectFailed`; every other newer value maps to `Unknown`, the code each
    /// edition has for "no better answer". The boundary and the fallbacks are โš  until IEC 61850-8-1 Ed2.1 Table 110 is
    /// read, so the server does **not** apply this: it sends every cause as it is, as
    /// libiec61850 does. Map with it yourself if an Edition 1 client needs it.
    #[must_use]
    pub const fn for_edition(self, edition: Edition) -> AddCause {
        let defined = matches!((self.since(), edition), (Edition::Ed1, _) | (Edition::Ed2, Edition::Ed2 | Edition::Ed2_1) | (Edition::Ed2_1, Edition::Ed2_1));
        if defined {
            self
        } else if matches!((self, edition), (AddCause::ObjectAlreadySelected, Edition::Ed1)) {
            AddCause::SelectFailed
        } else {
            AddCause::Unknown
        }
    }
}

/// `LastApplError` โ€” why a control failed (IEC 61850-8-1 Table 76).
///
/// A five-component structure sent unsolicited, alongside the `Oper` value, in the
/// `InformationReport` that carries a negative `CommandTermination`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct LastApplError {
    /// `cntrlObj` โ€” the control object, as `LD/LN$CO$DO$Oper`.
    pub control_object: String,
    /// `error`.
    pub error: ControlError,
    /// `origin` โ€” echoed from the request.
    pub origin: Origin,
    /// `ctlNum` โ€” echoed from the request, which is what ties it to a command.
    pub ctl_num: u8,
    /// `addCause` โ€” the diagnosis.
    pub add_cause: AddCause,
}

impl LastApplError {
    /// Read the five-component structure.
    pub fn from_value(v: &Value) -> Result<LastApplError> {
        let m = v.members().ok_or(Error::InvalidValue("LastApplError is a structure"))?;
        let bad = || Error::InvalidValue("LastApplError structure");
        Ok(LastApplError {
            control_object: String::from(m.first().ok_or_else(bad)?.as_str().ok_or_else(bad)?),
            error: ControlError::from_code(m.get(1).ok_or_else(bad)?.as_i64().ok_or_else(bad)?),
            origin: Origin::from_value(m.get(2).ok_or_else(bad)?).ok_or_else(bad)?,
            ctl_num: u8::try_from(m.get(3).ok_or_else(bad)?.as_u64().ok_or_else(bad)?).map_err(|_| bad())?,
            add_cause: AddCause::from_code(m.get(4).ok_or_else(bad)?.as_i64().ok_or_else(bad)?),
        })
    }

    /// The five-component structure.
    pub fn to_value(&self) -> Value {
        Value::Structure(alloc::vec![
            Value::VisibleString(self.control_object.clone()),
            Value::Integer(self.error.to_code()),
            self.origin.to_value(),
            Value::Unsigned(u64::from(self.ctl_num)),
            Value::Integer(self.add_cause.to_code()),
        ])
    }
}

/// The final answer to an enhanced-security control, which arrives unsolicited.
///
/// A positive termination is an `InformationReport` naming only the `Oper` variable; a
/// negative one names `LastApplError` as well, and *that* is where the reason is.
#[derive(Clone, Debug, PartialEq)]
pub enum CommandTermination {
    /// The command completed. The switchgear reached the commanded position.
    Positive {
        /// The control object, as `LD/LN$CO$DO$Oper`.
        control_object: String,
        /// The `Oper` structure the server echoed back.
        request: ControlRequest,
    },
    /// The command failed or was abandoned.
    Negative {
        /// Why.
        error: LastApplError,
        /// The `Oper` structure the server echoed back, if it sent one.
        request: Option<ControlRequest>,
    },
}

impl CommandTermination {
    /// The `ctlNum` this termination belongs to, which is how a client with several commands
    /// in flight tells them apart.
    pub fn ctl_num(&self) -> u8 {
        match self {
            CommandTermination::Positive { request, .. } => request.ctl_num,
            CommandTermination::Negative { error, .. } => error.ctl_num,
        }
    }

    /// The control object this termination is about.
    pub fn control_object(&self) -> &str {
        match self {
            CommandTermination::Positive { control_object, .. } => control_object,
            CommandTermination::Negative { error, .. } => &error.control_object,
        }
    }

    /// True when the command succeeded.
    pub const fn is_positive(&self) -> bool {
        matches!(self, CommandTermination::Positive { .. })
    }

    /// Why the command failed, for a negative termination.
    pub const fn add_cause(&self) -> Option<AddCause> {
        match self {
            CommandTermination::Negative { error, .. } => Some(error.add_cause),
            CommandTermination::Positive { .. } => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::TimeQuality;
    use crate::proto::data::Dbpos;

    fn now() -> UtcTime {
        UtcTime::from_unix(1_700_000_000, 0, TimeQuality::SYNCHRONIZED)
    }

    #[test]
    fn an_oper_round_trips_with_and_without_oper_tm() {
        let mut r = ControlRequest::new(Value::dbpos(Dbpos::On), 4, now());
        r.origin = Origin::new(OriginCategory::StationControl, "hmi-1");
        r.check = Check::BOTH;
        let v = r.to_value();
        assert_eq!(v.members().map(<[Value]>::len), Some(6), "ctlVal, origin, ctlNum, T, Test, Check");
        assert_eq!(ControlRequest::from_value(&v).unwrap(), r);

        // With a time-activated operate the structure grows a member, and the decoder has to
        // notice โ€” `operTm` is untagged and sits exactly where `origin` otherwise would.
        r.oper_tm = Some(now());
        let v = r.to_value();
        assert_eq!(v.members().map(<[Value]>::len), Some(7));
        assert_eq!(ControlRequest::from_value(&v).unwrap(), r);
    }

    #[test]
    fn an_oper_from_a_device_without_ctl_num_still_decodes() {
        // `ctlNum` is present only when the object was engineered with it. A device that
        // omits it echoes `{ctlVal, origin, T, Test, Check}`, and a decoder that insists on
        // reading the fourth member as a number turns a command termination into an
        // unrecognised report โ€” the one place a caller is waiting for an answer.
        let v = Value::Structure(alloc::vec![
            Value::Boolean(true),
            Origin::new(OriginCategory::StationControl, "hmi").to_value(),
            Value::UtcTime(now()),
            Value::Boolean(false),
            Check::NONE.to_value(),
        ]);
        let r = ControlRequest::from_value(&v).unwrap();
        assert_eq!((r.ctl_num, r.t, r.oper_tm), (0, now(), None));
        assert_eq!(r.ctl_val, Value::Boolean(true));
    }

    #[test]
    fn a_cancel_is_an_oper_without_the_check() {
        let r = ControlRequest::new(Value::Boolean(true), 9, now());
        let v = r.to_cancel_value();
        assert_eq!(v.members().map(<[Value]>::len), Some(5));
        let back = ControlRequest::from_value(&v).unwrap();
        assert_eq!(back.check, Check::NONE, "a cancel asks for no checks because it carries none");
        assert_eq!((back.ctl_num, back.ctl_val), (9, Value::Boolean(true)));
    }

    #[test]
    fn the_check_bits_are_synchro_then_interlock() {
        // Bit 0 is the synchrocheck and bit 1 the interlock check. Swapping them asks a
        // substation to skip the check it was told to make, which is the whole point of
        // pinning it in a test.
        assert_eq!(Check { synchro: true, interlock: false }.to_value(), Value::BitString { unused: 6, bytes: alloc::vec![0b1000_0000] });
        assert_eq!(Check { synchro: false, interlock: true }.to_value(), Value::BitString { unused: 6, bytes: alloc::vec![0b0100_0000] });
        for c in [Check::NONE, Check::BOTH, Check { synchro: true, interlock: false }, Check { synchro: false, interlock: true }] {
            assert_eq!(Check::from_value(&c.to_value()), Some(c));
        }
        // A quality is thirteen bits, so it is not a Check. A `Dbpos` *is* two bits and is
        // therefore indistinguishable from one on the wire โ€” only its position in the
        // structure says which it is, which is worth knowing rather than papering over.
        assert_eq!(Check::from_value(&Value::quality(crate::common::Quality::GOOD)), None);
        assert_eq!(Check::from_value(&Value::Boolean(true)), None);
        assert_eq!(Check::from_value(&Value::dbpos(Dbpos::On)), Some(Check { synchro: true, interlock: false }));
    }

    #[test]
    fn last_appl_error_names_the_reason_a_breaker_did_not_move() {
        let e = LastApplError {
            control_object: String::from("IED1LD0/CSWI1$CO$Pos$Oper"),
            error: ControlError::Unknown,
            origin: Origin::new(OriginCategory::RemoteControl, "scada"),
            ctl_num: 4,
            add_cause: AddCause::BlockedByInterlocking,
        };
        assert_eq!(LastApplError::from_value(&e.to_value()).unwrap(), e);
        assert_eq!(e.add_cause.to_code(), 10);
        // The whole table round-trips, including a value Ed2.1 may have added since.
        for code in 0..=27 {
            assert_eq!(AddCause::from_code(code).to_code(), code);
        }
        assert_eq!(AddCause::from_code(99), AddCause::Other(99));
        for code in 0..=3 {
            assert_eq!(ControlError::from_code(code).to_code(), code);
        }
    }

    #[test]
    fn the_control_model_says_which_services_are_legal() {
        assert!(!ControlModel::DirectNormal.needs_select());
        assert!(ControlModel::SboNormal.needs_select() && !ControlModel::SboNormal.select_carries_value());
        assert!(ControlModel::SboEnhanced.select_carries_value());
        assert!(ControlModel::SboEnhanced.enhanced_security() && ControlModel::DirectEnhanced.enhanced_security());
        assert!(!ControlModel::DirectNormal.enhanced_security());
        for code in 0..=4 {
            assert_eq!(ControlModel::from_code(code).unwrap().to_code(), code);
        }
        assert_eq!(ControlModel::from_code(5), None);
    }

    #[test]
    fn an_origin_identifier_may_be_octets_or_text() {
        let o = Origin::new(OriginCategory::Maintenance, "tool");
        assert_eq!(Origin::from_value(&o.to_value()), Some(o.clone()));
        assert_eq!(o.identifier_str(), Some("tool"));
        // IEC 62351-6 fills it with a certificate serial number, which is not text.
        let binary = Origin { category: OriginCategory::Process, identifier: alloc::vec![0x00, 0xFF, 0x80] };
        assert_eq!(binary.identifier_str(), None);
        assert_eq!(Origin::from_value(&binary.to_value()), Some(binary));
        assert_eq!(OriginCategory::from_code(42), OriginCategory::Other(42));
    }

    #[test]
    fn a_termination_is_tied_to_its_command_by_ctl_num() {
        let r = ControlRequest::new(Value::Boolean(true), 7, now());
        let positive = CommandTermination::Positive { control_object: String::from("IED1LD0/CSWI1$CO$Pos$Oper"), request: r.clone() };
        assert!(positive.is_positive());
        assert_eq!(positive.ctl_num(), 7);
        let negative = CommandTermination::Negative {
            error: LastApplError {
                control_object: String::from("IED1LD0/CSWI1$CO$Pos$Oper"),
                ctl_num: 7,
                add_cause: AddCause::TimeLimitOver,
                ..Default::default()
            },
            request: Some(r),
        };
        assert!(!negative.is_positive());
        assert_eq!((negative.ctl_num(), negative.control_object()), (7, "IED1LD0/CSWI1$CO$Pos$Oper"));
    }

    /// Every value, at every edition: the table in [`AddCause::for_edition`], row by row.
    #[test]
    fn every_add_cause_is_sent_as_its_edition_defines_it() {
        use AddCause as A;
        let ed1_fallback = |c: A| match c {
            A::ObjectAlreadySelected => A::SelectFailed,
            _ => A::Unknown,
        };
        for code in 0..=28 {
            let c = A::from_code(code);
            assert_eq!(c.to_code(), code, "round trip of {code}");
            let (ed1, ed2, ed21) = (c.for_edition(Edition::Ed1), c.for_edition(Edition::Ed2), c.for_edition(Edition::Ed2_1));
            match code {
                0..=18 => {
                    assert_eq!(c.since(), Edition::Ed1, "{code}");
                    assert_eq!((ed1, ed2, ed21), (c, c, c), "{code}");
                }
                19..=27 => {
                    assert_eq!(c.since(), Edition::Ed2, "{code}");
                    assert_eq!((ed1, ed2, ed21), (ed1_fallback(c), c, c), "{code}");
                }
                _ => {
                    assert_eq!(c.since(), Edition::Ed2_1, "{code}");
                    assert_eq!((ed1, ed2, ed21), (A::Unknown, A::Unknown, c), "{code}");
                }
            }
            for e in [Edition::Ed1, Edition::Ed2, Edition::Ed2_1] {
                assert!(c.for_edition(e).since() <= e, "{code} at {e:?} is a value that edition defines");
            }
        }
        assert_eq!(A::ObjectAlreadySelected.for_edition(Edition::Ed1).to_code(), 3);
        assert_eq!(A::LockedByOtherClient.for_edition(Edition::Ed1).to_code(), 0);
    }
}