meterbus-wired-datalink 0.0.1

Wired M-Bus datalink primitives and codecs
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
//! Wired M-Bus data-link control fields and communication types.
//!
//! The one-byte control field, commonly called the C-field, identifies the
//! data-link communication carried by a structured frame. It also carries two
//! direction-dependent flags. In messages from a master, those flags are the
//! frame count bit (FCB) and frame count valid bit (FCV). In messages from a
//! slave, the same bit positions are access demand (ACD) and data flow control
//! (DFC).
//!
//! [`Control`] preserves any `u8` received from the wire. Its methods explain
//! known values and expose their flags. Unknown values remain available for
//! error reporting. [`CommunicationType`] names the recognized message type, and
//! [`ControlError`] reports a control value used with the wrong frame format.
//!
//! # Bit layout
//!
//! The field is interpreted as follows for the communications represented by
//! this crate:
//!
//! ```text
//! bit:   7   6   5   4   3   2   1   0
//!      +---+---+---+---+---+---+---+---+
//!      | 0 | D | X | Y | function code |
//!      +---+---+---+---+---+---+---+---+
//! ```
//!
//! `D` distinguishes the message direction:
//!
//! | `D` | Direction | `X` (bit 5) | `Y` (bit 4) |
//! | --- | --- | --- | --- |
//! | `1` | Master to slave | FCB | FCV |
//! | `0` | Slave to master | ACD | DFC |
//!
//! [`Control::direction`] identifies `D`. Direction-specific flag
//! accessors return [`Some`] only when their interpretation applies and
//! [`None`] for the opposite direction. This avoids reporting, for example,
//! an ACD value from a master request merely because bit 5 is set.
//!
//! Bit 7 is clear for every communication supported here. The low four bits
//! carry the function code. Classification uses the complete byte rather than
//! only the function code because direction and required flag combinations
//! are part of the supported control values.
//!
//! # Supported control values
//!
//! | C-field | [`CommunicationType`] | Direction | Format | Flag state |
//! | --- | --- | --- | --- | --- |
//! | `0x40` | [`CommunicationType::SndNke`] | Master to slave | Short | FCB = 0, FCV = 0 |
//! | `0x5a`, `0x7a` | [`CommunicationType::ReqUd1`] | Master to slave | Short | FCV = 1; FCB = 0 or 1 |
//! | `0x5b`, `0x7b` | [`CommunicationType::ReqUd2`] | Master to slave | Short | FCV = 1; FCB = 0 or 1 |
//! | `0x53`, `0x73` | [`CommunicationType::SndUd`] | Master to slave | Variable | FCV = 1; bit 5 = 0 or 1 |
//! | `0x43` | [`CommunicationType::SndUd2`] | Master to slave | Variable | FCB = 0, FCV = 0 |
//! | `0x08`, `0x18`, `0x28`, `0x38` | [`CommunicationType::RspUd`] | Slave to master | Variable | ACD and DFC independently clear or set |
//!
//! Every other byte maps to [`CommunicationType::Unsupported`], while [`Control`]
//! still preserves the original value.
//!
//! # Frame compatibility
//!
//! Short frames support SND-NKE, REQ-UD1, and REQ-UD2. Variable frames support
//! SND-UD, SND-UD2, and RSP-UD. Frame constructors reject other combinations
//! with a [`ControlError`].
//!
//! [`ShortFrame::new`]: crate::ShortFrame::new
//! [`ControlFrame::new`]: crate::ControlFrame::new
//! [`LongFrame::new`]: crate::LongFrame::new
//!
//! For master messages, bit 5 is FCB and bit 4 is FCV. For slave messages, the
//! same bits are ACD and DFC. The accessors return [`None`] when a flag does not
//! apply to that direction.
//!
//! # Examples
//!
//! Inspect a REQ-UD2 control field sent by a master:
//!
//! ```
//! use meterbus_wired_datalink::{CommunicationType, Control, Direction};
//!
//! let control = Control::req_ud2(true);
//! assert_eq!(control.value(), 0x7b);
//! assert_eq!(control.communication_type(), CommunicationType::ReqUd2);
//! assert_eq!(control.direction(), Some(Direction::MasterToSlave));
//! assert_eq!(control.frame_count_bit(), Some(true));
//! assert_eq!(control.frame_count_valid(), Some(true));
//! assert_eq!(control.access_demand(), None);
//! ```
//!
//! Callers still maintain FCB state, act on ACD and DFC, and decide which reply
//! is valid for a request.

use core::fmt;

/// A data-link control field.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Control(u8);

impl Control {
    /// Creates a control field from its wire value.
    #[must_use]
    pub const fn new(value: u8) -> Self {
        Self(value)
    }

    /// Creates an SND-NKE control field.
    #[must_use]
    pub const fn snd_nke() -> Self {
        Self(0x40)
    }

    /// Creates a REQ-UD1 control field with the requested frame-count bit.
    #[must_use]
    pub const fn req_ud1(fcb: bool) -> Self {
        Self(0x5a | ((fcb as u8) << 5))
    }

    /// Creates a REQ-UD2 control field with the requested frame-count bit.
    #[must_use]
    pub const fn req_ud2(fcb: bool) -> Self {
        Self(0x5b | ((fcb as u8) << 5))
    }

    /// Creates an SND-UD control field with bit 5 clear or set.
    #[must_use]
    pub const fn snd_ud(bit5: bool) -> Self {
        Self(0x53 | ((bit5 as u8) << 5))
    }

    /// Creates an SND-UD2 control field.
    #[must_use]
    pub const fn snd_ud2() -> Self {
        Self(0x43)
    }

    /// Creates an RSP-UD control field with access-demand and data-flow-control flags.
    #[must_use]
    pub const fn rsp_ud(acd: bool, dfc: bool) -> Self {
        Self(0x08 | ((acd as u8) << 5) | ((dfc as u8) << 4))
    }

    /// Returns the wire value.
    #[must_use]
    pub const fn value(self) -> u8 {
        self.0
    }

    /// Returns the supported communication type represented by this field.
    #[must_use]
    pub const fn communication_type(self) -> CommunicationType {
        match self.0 {
            0x40 => CommunicationType::SndNke,
            0x43 => CommunicationType::SndUd2,
            0x53 | 0x73 => CommunicationType::SndUd,
            0x5a | 0x7a => CommunicationType::ReqUd1,
            0x5b | 0x7b => CommunicationType::ReqUd2,
            0x08 | 0x18 | 0x28 | 0x38 => CommunicationType::RspUd,
            _ => CommunicationType::Unsupported,
        }
    }

    /// Returns the direction of a supported communication.
    #[must_use]
    pub const fn direction(self) -> Option<Direction> {
        match self.communication_type() {
            CommunicationType::SndNke
            | CommunicationType::ReqUd1
            | CommunicationType::ReqUd2
            | CommunicationType::SndUd
            | CommunicationType::SndUd2 => Some(Direction::MasterToSlave),
            CommunicationType::RspUd => Some(Direction::SlaveToMaster),
            CommunicationType::Unsupported => None,
        }
    }

    /// Returns the frame-count bit for a message sent by a master.
    #[must_use]
    pub const fn frame_count_bit(self) -> Option<bool> {
        if matches!(self.direction(), Some(Direction::MasterToSlave)) {
            Some(self.0 & 0x20 != 0)
        } else {
            None
        }
    }

    /// Returns the frame-count-valid bit for a message sent by a master.
    #[must_use]
    pub const fn frame_count_valid(self) -> Option<bool> {
        if matches!(self.direction(), Some(Direction::MasterToSlave)) {
            Some(self.0 & 0x10 != 0)
        } else {
            None
        }
    }

    /// Returns the access-demand bit for a message sent by a slave.
    #[must_use]
    pub const fn access_demand(self) -> Option<bool> {
        if matches!(self.direction(), Some(Direction::SlaveToMaster)) {
            Some(self.0 & 0x20 != 0)
        } else {
            None
        }
    }

    /// Returns the data-flow-control bit for a message sent by a slave.
    #[must_use]
    pub const fn data_flow_control(self) -> Option<bool> {
        if matches!(self.direction(), Some(Direction::SlaveToMaster)) {
            Some(self.0 & 0x10 != 0)
        } else {
            None
        }
    }

    pub(crate) const fn validate_short_frame(self) -> Result<(), ControlError> {
        if matches!(
            self.communication_type(),
            CommunicationType::SndNke | CommunicationType::ReqUd1 | CommunicationType::ReqUd2
        ) {
            Ok(())
        } else {
            Err(ControlError::InvalidForShortFrame { value: self.0 })
        }
    }

    pub(crate) const fn validate_variable_frame(self) -> Result<(), ControlError> {
        if matches!(
            self.communication_type(),
            CommunicationType::SndUd | CommunicationType::SndUd2 | CommunicationType::RspUd
        ) {
            Ok(())
        } else {
            Err(ControlError::InvalidForVariableFrame { value: self.0 })
        }
    }
}

/// Direction of a supported data-link communication.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum Direction {
    /// A request or command sent by a master.
    MasterToSlave,
    /// A response sent by a slave.
    SlaveToMaster,
}

impl From<u8> for Control {
    fn from(value: u8) -> Self {
        Self::new(value)
    }
}

/// A supported wired M-Bus communication type.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum CommunicationType {
    /// Normalize the link layer.
    SndNke,
    /// Request time-critical data.
    ReqUd1,
    /// Request standard data.
    ReqUd2,
    /// Send user data.
    SndUd,
    /// Send user data and request a response.
    SndUd2,
    /// Respond with user data.
    RspUd,
    /// A control value unsupported by wired M-Bus.
    Unsupported,
}

/// Error produced when a control field is incompatible with a frame.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ControlError {
    /// The control value cannot be used in a short frame.
    InvalidForShortFrame {
        /// The rejected control-field value.
        value: u8,
    },
    /// The control value cannot be used in a variable-format frame.
    InvalidForVariableFrame {
        /// The rejected control-field value.
        value: u8,
    },
}

impl fmt::Display for ControlError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidForShortFrame { value } => write!(
                formatter,
                "control value 0x{value:02x} is invalid for a short frame"
            ),
            Self::InvalidForVariableFrame { value } => write!(
                formatter,
                "control value 0x{value:02x} is invalid for a variable-format frame"
            ),
        }
    }
}

impl core::error::Error for ControlError {}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use super::*;
    #[cfg(feature = "alloc")]
    use alloc::string::ToString;

    #[test]
    fn classifies_supported_communication_types() {
        let cases = [
            (0x40, CommunicationType::SndNke),
            (0x43, CommunicationType::SndUd2),
            (0x53, CommunicationType::SndUd),
            (0x73, CommunicationType::SndUd),
            (0x5a, CommunicationType::ReqUd1),
            (0x7a, CommunicationType::ReqUd1),
            (0x5b, CommunicationType::ReqUd2),
            (0x7b, CommunicationType::ReqUd2),
            (0x08, CommunicationType::RspUd),
            (0x18, CommunicationType::RspUd),
            (0x28, CommunicationType::RspUd),
            (0x38, CommunicationType::RspUd),
            (0xff, CommunicationType::Unsupported),
        ];

        for (value, communication_type) in cases {
            let control = Control::from(value);
            assert_eq!(control.value(), value);
            assert_eq!(control.communication_type(), communication_type);
        }
    }

    #[test]
    fn exposes_master_and_slave_flags() {
        let master = Control::new(0x7b);
        assert_eq!(master.direction(), Some(Direction::MasterToSlave));
        assert_eq!(master.frame_count_bit(), Some(true));
        assert_eq!(master.frame_count_valid(), Some(true));
        assert_eq!(master.access_demand(), None);
        assert_eq!(master.data_flow_control(), None);

        let slave = Control::new(0x38);
        assert_eq!(slave.direction(), Some(Direction::SlaveToMaster));
        assert_eq!(slave.frame_count_bit(), None);
        assert_eq!(slave.frame_count_valid(), None);
        assert_eq!(slave.access_demand(), Some(true));
        assert_eq!(slave.data_flow_control(), Some(true));
    }

    #[test]
    fn constructors_produce_named_control_values() {
        assert_eq!(Control::snd_nke().value(), 0x40);
        assert_eq!(Control::req_ud1(false).value(), 0x5a);
        assert_eq!(Control::req_ud1(true).value(), 0x7a);
        assert_eq!(Control::req_ud2(false).value(), 0x5b);
        assert_eq!(Control::req_ud2(true).value(), 0x7b);
        assert_eq!(Control::snd_ud(false).value(), 0x53);
        assert_eq!(Control::snd_ud(true).value(), 0x73);
        assert_eq!(Control::snd_ud2().value(), 0x43);
        assert_eq!(Control::rsp_ud(false, false).value(), 0x08);
        assert_eq!(Control::rsp_ud(false, true).value(), 0x18);
        assert_eq!(Control::rsp_ud(true, false).value(), 0x28);
        assert_eq!(Control::rsp_ud(true, true).value(), 0x38);
    }

    #[test]
    fn all_control_bytes_expose_flags_only_for_supported_direction() {
        for value in u8::MIN..=u8::MAX {
            let control = Control::new(value);
            match control.direction() {
                Some(Direction::MasterToSlave) => {
                    assert!(control.frame_count_bit().is_some());
                    assert!(control.frame_count_valid().is_some());
                    assert_eq!(control.access_demand(), None);
                    assert_eq!(control.data_flow_control(), None);
                }
                Some(Direction::SlaveToMaster) => {
                    assert_eq!(control.frame_count_bit(), None);
                    assert_eq!(control.frame_count_valid(), None);
                    assert!(control.access_demand().is_some());
                    assert!(control.data_flow_control().is_some());
                }
                None => {
                    assert_eq!(control.frame_count_bit(), None);
                    assert_eq!(control.frame_count_valid(), None);
                    assert_eq!(control.access_demand(), None);
                    assert_eq!(control.data_flow_control(), None);
                }
            }
        }
    }

    #[test]
    fn validates_frame_compatibility() {
        assert_eq!(Control::new(0x40).validate_short_frame(), Ok(()));
        assert_eq!(Control::new(0x53).validate_variable_frame(), Ok(()));

        let error = Control::new(0x53).validate_short_frame().unwrap_err();
        assert_eq!(error, ControlError::InvalidForShortFrame { value: 0x53 });
        #[cfg(feature = "alloc")]
        assert_eq!(
            error.to_string(),
            "control value 0x53 is invalid for a short frame"
        );
        assert_eq!(
            Control::new(0x40).validate_variable_frame(),
            Err(ControlError::InvalidForVariableFrame { value: 0x40 })
        );
    }
}