Skip to main content

iec61850_kit/common/
quality.rs

1/// The `validity` component of [`Quality`] (IEC 61850-7-3).
2#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
3pub enum Validity {
4    /// Good.
5    #[default]
6    Good,
7    /// Invalid.
8    Invalid,
9    /// Reserved value (2) — never emitted, accepted on decode.
10    Reserved,
11    /// Questionable.
12    Questionable,
13}
14
15/// The `source` component of [`Quality`].
16#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
17pub enum Source {
18    /// Value comes from the process.
19    #[default]
20    Process,
21    /// Value was substituted.
22    Substituted,
23}
24
25/// IEC 61850-7-3 `Quality`: the 13-bit packed bit string, plus the 14th bit `derived`
26/// that the 9-2LE guideline adds for sampled values.
27///
28/// Bit numbering follows the standard (bit 0 is the most significant bit of the first
29/// octet): 0–1 validity, 2 overflow, 3 outOfRange, 4 badReference, 5 oscillatory,
30/// 6 failure, 7 oldData, 8 inconsistent, 9 inaccurate, 10 source, 11 test,
31/// 12 operatorBlocked, 13 derived (9-2LE).
32#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
33pub struct Quality {
34    /// Validity.
35    pub validity: Validity,
36    /// Overflow.
37    pub overflow: bool,
38    /// Out of range.
39    pub out_of_range: bool,
40    /// Bad reference.
41    pub bad_reference: bool,
42    /// Oscillatory.
43    pub oscillatory: bool,
44    /// Failure.
45    pub failure: bool,
46    /// Old data.
47    pub old_data: bool,
48    /// Inconsistent.
49    pub inconsistent: bool,
50    /// Inaccurate.
51    pub inaccurate: bool,
52    /// Source.
53    pub source: Source,
54    /// Test.
55    pub test: bool,
56    /// Operator blocked.
57    pub operator_blocked: bool,
58    /// Derived (9-2LE, sampled values only).
59    pub derived: bool,
60}
61
62impl Quality {
63    /// Good quality, no flags.
64    pub const GOOD: Quality = Quality {
65        validity: Validity::Good,
66        overflow: false,
67        out_of_range: false,
68        bad_reference: false,
69        oscillatory: false,
70        failure: false,
71        old_data: false,
72        inconsistent: false,
73        inaccurate: false,
74        source: Source::Process,
75        test: false,
76        operator_blocked: false,
77        derived: false,
78    };
79
80    /// Number of significant bits without `derived` (IEC 61850-7-3).
81    pub const BITS: u8 = 13;
82    /// Number of significant bits with `derived` (9-2LE sampled values).
83    pub const BITS_SV: u8 = 14;
84
85    /// Pack into an integer whose bit 31 is quality bit 0 (the layout of the first octets
86    /// of the BER bit string and of the 32-bit quality word in 9-2LE sample data).
87    pub const fn to_bits_msb(self) -> u32 {
88        let validity = match self.validity {
89            Validity::Good => 0,
90            Validity::Invalid => 1,
91            Validity::Reserved => 2,
92            Validity::Questionable => 3,
93        };
94        let mut v: u32 = validity << 30;
95        macro_rules! bit {
96            ($field:expr, $n:expr) => {
97                if $field {
98                    v |= 1 << (31 - $n);
99                }
100            };
101        }
102        bit!(self.overflow, 2);
103        bit!(self.out_of_range, 3);
104        bit!(self.bad_reference, 4);
105        bit!(self.oscillatory, 5);
106        bit!(self.failure, 6);
107        bit!(self.old_data, 7);
108        bit!(self.inconsistent, 8);
109        bit!(self.inaccurate, 9);
110        bit!(matches!(self.source, Source::Substituted), 10);
111        bit!(self.test, 11);
112        bit!(self.operator_blocked, 12);
113        bit!(self.derived, 13);
114        v
115    }
116
117    /// Unpack from the layout produced by [`Quality::to_bits_msb`].
118    pub const fn from_bits_msb(v: u32) -> Quality {
119        const fn bit(v: u32, n: u32) -> bool {
120            (v >> (31 - n)) & 1 == 1
121        }
122        Quality {
123            validity: match v >> 30 {
124                0 => Validity::Good,
125                1 => Validity::Invalid,
126                2 => Validity::Reserved,
127                _ => Validity::Questionable,
128            },
129            overflow: bit(v, 2),
130            out_of_range: bit(v, 3),
131            bad_reference: bit(v, 4),
132            oscillatory: bit(v, 5),
133            failure: bit(v, 6),
134            old_data: bit(v, 7),
135            inconsistent: bit(v, 8),
136            inaccurate: bit(v, 9),
137            source: if bit(v, 10) { Source::Substituted } else { Source::Process },
138            test: bit(v, 11),
139            operator_blocked: bit(v, 12),
140            derived: bit(v, 13),
141        }
142    }
143
144    /// The two octets of the BER bit-string contents (without the unused-bits octet), for
145    /// the 13-bit MMS encoding.
146    pub const fn to_octets(self) -> [u8; 2] {
147        let v = self.to_bits_msb() & !(1 << (31 - 13));
148        [(v >> 24) as u8, (v >> 16) as u8]
149    }
150
151    /// Decode from up to four bit-string content octets (bit 0 = MSB of the first octet).
152    pub fn from_octets(octets: &[u8]) -> Quality {
153        let mut v: u32 = 0;
154        for (i, b) in octets.iter().take(4).enumerate() {
155            v |= u32::from(*b) << (24 - 8 * i);
156        }
157        Quality::from_bits_msb(v)
158    }
159
160    /// True if validity is `Good` and no flag other than `derived` is set.
161    pub const fn is_good(self) -> bool {
162        matches!(self.validity, Validity::Good) && (self.to_bits_msb() & !(1 << (31 - 13))) == 0
163    }
164}
165
166#[cfg(test)]
167mod tests {
168    use super::*;
169
170    #[test]
171    fn round_trip_bits() {
172        let q = Quality { validity: Validity::Questionable, test: true, derived: true, ..Quality::GOOD };
173        let v = q.to_bits_msb();
174        assert_eq!(v >> 30, 3);
175        assert_eq!(Quality::from_bits_msb(v), q);
176        assert_eq!(Quality::from_octets(&q.to_octets()), Quality { derived: false, ..q });
177        assert!(Quality::GOOD.is_good());
178        assert!(!q.is_good());
179        assert!(Quality { derived: true, ..Quality::GOOD }.is_good());
180    }
181}