dlms-cosem-kit 0.1.0

DLMS/COSEM (IEC 62056) in Rust: sans-I/O, no_std, client and server, security suites 0-2
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
//! Short-name referencing: the Read, Write and report services.
//!
//! Logical-name referencing names an attribute by `(class_id, logical name, index)` —
//! sixteen bytes. Short-name referencing names the same attribute by a **sixteen-bit
//! number**. An installed base of meters built before logical names became universal
//! speaks it, and nothing else.
//!
//! [`ReadRequest`], [`WriteRequest`], [`UnconfirmedWriteRequest`] and
//! [`InformationReportRequest`] are the four services. All are lists, and all address
//! their targets through [`VariableAccess`].
//!
//! There is no ACTION service: a method is invoked by *writing* its short name, with the
//! value as the parameter. [`crate::cosem::ShortName`] turns a base name into a short
//! name and back.

use crate::axdr::Data;
use crate::codec::{Decode, Encode, ErrorKind, Reader, Result, Writer};

use super::result::{DataAccessResult, List};

/// A sixteen-bit short name.
///
/// The Blue Book calls this an `ObjectName`. The low three bits of a *named-variable*
/// short name are zero, and `0xFA00` is the base name of the current association object
/// in every short-name logical device.
pub type ObjectName = u16;

/// The base name of the current association object in a short-name logical device.
pub const CURRENT_ASSOCIATION_SN: ObjectName = 0xFA00;

/// How far apart consecutive attributes of one object sit.
///
/// Confirmed twice: attribute *n* of an object based at `x` is `x + (n − 1) × 8`.
pub const ATTRIBUTE_STRIDE: u16 = 8;

/// What a Read, Write or report entry addresses.
///
/// The `detailed-access` choice `[3]` is defined in the ASN.1 and explicitly not used in
/// DLMS/COSEM, so it is not here: decoding a tag no conformant peer sends buys nothing
/// and widens a parser that faces the network.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum VariableAccess<'a> {
    /// An attribute or a method, by short name.
    VariableName(ObjectName),
    /// An attribute with a selective-access descriptor, or a method with its parameter —
    /// short-name referencing has one shape for both.
    Parameterized {
        /// Which attribute or method.
        name: ObjectName,
        /// Which selector, or which method invocation the profile defines.
        selector: u8,
        /// The selector's parameter.
        parameter: Data<'a>,
    },
    /// Ask for the next block of a long read.
    BlockNumber(u16),
    /// One block of a long write going up.
    ReadDataBlock {
        /// True when no further block follows.
        last_block: bool,
        /// This block's number.
        block_number: u16,
        /// The fragment.
        raw_data: &'a [u8],
    },
    /// Acknowledge a block of a long write.
    WriteDataBlock {
        /// True when no further block follows.
        last_block: bool,
        /// The block being acknowledged.
        block_number: u16,
    },
}

impl VariableAccess<'_> {
    /// The short name this entry addresses, for the two forms that name one.
    #[must_use]
    pub const fn name(&self) -> Option<ObjectName> {
        match self {
            Self::VariableName(n) | Self::Parameterized { name: n, .. } => Some(*n),
            _ => None,
        }
    }
}

impl Encode for VariableAccess<'_> {
    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
        match self {
            Self::VariableName(name) => {
                w.write_u8(2)?;
                w.write_u16(*name)
            }
            Self::Parameterized { name, selector, parameter } => {
                w.write_u8(4)?;
                w.write_u16(*name)?;
                w.write_u8(*selector)?;
                parameter.encode(w)
            }
            Self::BlockNumber(n) => {
                w.write_u8(5)?;
                w.write_u16(*n)
            }
            Self::ReadDataBlock { last_block, block_number, raw_data } => {
                w.write_u8(6)?;
                w.write_u8(u8::from(*last_block))?;
                w.write_u16(*block_number)?;
                w.write_length_prefixed(raw_data)
            }
            Self::WriteDataBlock { last_block, block_number } => {
                w.write_u8(7)?;
                w.write_u8(u8::from(*last_block))?;
                w.write_u16(*block_number)
            }
        }
    }
}

impl<'a> Decode<'a> for VariableAccess<'a> {
    fn decode(r: &mut Reader<'a>) -> Result<Self> {
        Ok(match r.u8()? {
            2 => Self::VariableName(r.u16()?),
            4 => Self::Parameterized { name: r.u16()?, selector: r.u8()?, parameter: Data::decode(r)? },
            5 => Self::BlockNumber(r.u16()?),
            6 => Self::ReadDataBlock {
                last_block: r.u8()? != 0,
                block_number: r.u16()?,
                raw_data: r.length_prefixed()?,
            },
            7 => Self::WriteDataBlock { last_block: r.u8()? != 0, block_number: r.u16()? },
            other => return Err(r.err_back(ErrorKind::InvalidTag(other), 1)),
        })
    }
}

/// Read one or more attributes by short name.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ReadRequest<'a> {
    /// What to read, in order. The answers come back in the same order.
    pub specification: List<'a, VariableAccess<'a>>,
}

impl Encode for ReadRequest<'_> {
    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
        self.specification.encode(w)
    }
}

impl<'a> Decode<'a> for ReadRequest<'a> {
    fn decode(r: &mut Reader<'a>) -> Result<Self> {
        Ok(Self { specification: List::decode(r)? })
    }
}

/// One answer inside a [`ReadResponse`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ReadResult<'a> {
    /// The value.
    Data(Data<'a>),
    /// Why there is none.
    Error(DataAccessResult),
    /// One fragment of a value too large for the negotiated PDU size. Ask for the next
    /// with [`VariableAccess::BlockNumber`].
    Block {
        /// True when no further block follows.
        last_block: bool,
        /// This block's number.
        block_number: u16,
        /// The fragment.
        raw_data: &'a [u8],
    },
    /// The server has accepted a block of a long *write* and names it.
    BlockNumber(u16),
}

impl Encode for ReadResult<'_> {
    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
        match self {
            Self::Data(d) => {
                w.write_u8(0)?;
                d.encode(w)
            }
            Self::Error(e) => {
                w.write_u8(1)?;
                w.write_u8(e.as_u8())
            }
            Self::Block { last_block, block_number, raw_data } => {
                w.write_u8(2)?;
                w.write_u8(u8::from(*last_block))?;
                w.write_u16(*block_number)?;
                w.write_length_prefixed(raw_data)
            }
            Self::BlockNumber(n) => {
                w.write_u8(3)?;
                w.write_u16(*n)
            }
        }
    }
}

impl<'a> Decode<'a> for ReadResult<'a> {
    fn decode(r: &mut Reader<'a>) -> Result<Self> {
        Ok(match r.u8()? {
            0 => Self::Data(Data::decode(r)?),
            1 => Self::Error(DataAccessResult::from_u8(r.u8()?)),
            2 => Self::Block {
                last_block: r.u8()? != 0,
                block_number: r.u16()?,
                raw_data: r.length_prefixed()?,
            },
            3 => Self::BlockNumber(r.u16()?),
            other => return Err(r.err_back(ErrorKind::InvalidTag(other), 1)),
        })
    }
}

/// The answer to a [`ReadRequest`]: one result per entry, in order.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ReadResponse<'a> {
    /// One per entry of the request.
    pub results: List<'a, ReadResult<'a>>,
}

impl Encode for ReadResponse<'_> {
    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
        self.results.encode(w)
    }
}

impl<'a> Decode<'a> for ReadResponse<'a> {
    fn decode(r: &mut Reader<'a>) -> Result<Self> {
        Ok(Self { results: List::decode(r)? })
    }
}

/// Write one or more attributes by short name.
///
/// The two lists are **positional**: value *i* is written to entry *i*. A request whose
/// lists are different lengths names writes whose values cannot be found, and is refused
/// rather than paired up as far as it goes.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WriteRequest<'a> {
    /// What to write to, in order.
    pub specification: List<'a, VariableAccess<'a>>,
    /// The values, in the same order.
    pub values: List<'a, Data<'a>>,
}

impl Encode for WriteRequest<'_> {
    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
        self.specification.encode(w)?;
        self.values.encode(w)
    }
}

impl<'a> Decode<'a> for WriteRequest<'a> {
    fn decode(r: &mut Reader<'a>) -> Result<Self> {
        Ok(Self { specification: List::decode(r)?, values: List::decode(r)? })
    }
}

/// One outcome inside a [`WriteResponse`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WriteResult {
    /// The write succeeded.
    Success,
    /// Why it did not.
    Error(DataAccessResult),
    /// A block of a long write was accepted, and this is its number.
    BlockNumber(u16),
}

impl Encode for WriteResult {
    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
        match self {
            Self::Success => w.write_u8(0),
            Self::Error(e) => {
                w.write_u8(1)?;
                w.write_u8(e.as_u8())
            }
            Self::BlockNumber(n) => {
                w.write_u8(2)?;
                w.write_u16(*n)
            }
        }
    }

    fn encoded_len(&self) -> usize {
        match self {
            Self::Success => 1,
            Self::Error(_) => 2,
            Self::BlockNumber(_) => 3,
        }
    }
}

impl<'a> Decode<'a> for WriteResult {
    fn decode(r: &mut Reader<'a>) -> Result<Self> {
        Ok(match r.u8()? {
            0 => Self::Success,
            1 => Self::Error(DataAccessResult::from_u8(r.u8()?)),
            2 => Self::BlockNumber(r.u16()?),
            other => return Err(r.err_back(ErrorKind::InvalidTag(other), 1)),
        })
    }
}

/// The answer to a [`WriteRequest`]: one outcome per entry, in order.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WriteResponse<'a> {
    /// One per entry of the request.
    pub results: List<'a, WriteResult>,
}

impl Encode for WriteResponse<'_> {
    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
        self.results.encode(w)
    }
}

impl<'a> Decode<'a> for WriteResponse<'a> {
    fn decode(r: &mut Reader<'a>) -> Result<Self> {
        Ok(Self { results: List::decode(r)? })
    }
}

/// A write the server does not answer.
///
/// The same body as [`WriteRequest`] under a tag of its own. Worth having as a distinct
/// type rather than a flag: a caller that sends one must not then wait for a reply, and
/// a type that cannot produce a response is how that is said.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct UnconfirmedWriteRequest<'a> {
    /// What to write to, in order.
    pub specification: List<'a, VariableAccess<'a>>,
    /// The values, in the same order.
    pub values: List<'a, Data<'a>>,
}

impl Encode for UnconfirmedWriteRequest<'_> {
    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
        self.specification.encode(w)?;
        self.values.encode(w)
    }
}

impl<'a> Decode<'a> for UnconfirmedWriteRequest<'a> {
    fn decode(r: &mut Reader<'a>) -> Result<Self> {
        Ok(Self { specification: List::decode(r)?, values: List::decode(r)? })
    }
}

/// The short-name push: values a server sends unasked.
///
/// The sibling of `DataNotification`, from the era before logical names. `current_time`
/// is an `OPTIONAL GeneralizedTime`, so A-XDR puts a usage flag in front of it; the time
/// itself is carried as the octets the peer sent, because the encoding of a
/// `GeneralizedTime` inside A-XDR is not stated in material this project can read and a
/// timestamp decoded under a guessed grammar is a reading dated wrongly.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct InformationReportRequest<'a> {
    /// When, as the peer sent it, or `None` when it did not say.
    pub current_time: Option<&'a [u8]>,
    /// What the values are, in order.
    pub specification: List<'a, VariableAccess<'a>>,
    /// The values, in the same order.
    pub values: List<'a, Data<'a>>,
}

impl Encode for InformationReportRequest<'_> {
    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
        match self.current_time {
            None => w.write_u8(0)?,
            Some(t) => {
                w.write_u8(1)?;
                w.write_length_prefixed(t)?;
            }
        }
        self.specification.encode(w)?;
        self.values.encode(w)
    }
}

impl<'a> Decode<'a> for InformationReportRequest<'a> {
    fn decode(r: &mut Reader<'a>) -> Result<Self> {
        let current_time = if r.u8()? == 0 { None } else { Some(r.length_prefixed()?) };
        Ok(Self { current_time, specification: List::decode(r)?, values: List::decode(r)? })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::codec::SliceWriter;

    fn round_trip<'a, T: Encode + Decode<'a> + PartialEq + core::fmt::Debug>(v: &T, buf: &'a mut [u8]) {
        let n = {
            let mut w = SliceWriter::new(buf);
            v.encode(&mut w).unwrap();
            w.written()
        };
        assert_eq!(v.encoded_len(), n, "encoded_len must agree with encode");
        let decoded = T::from_bytes(buf.get(..n).unwrap()).unwrap();
        assert_eq!(&decoded, v);
    }

    #[test]
    fn every_variable_access_form_round_trips() {
        let forms = [
            VariableAccess::VariableName(0xFA00),
            VariableAccess::Parameterized { name: 0x0028, selector: 1, parameter: Data::LongUnsigned(7) },
            VariableAccess::BlockNumber(3),
            VariableAccess::ReadDataBlock { last_block: false, block_number: 2, raw_data: &[1, 2, 3] },
            VariableAccess::WriteDataBlock { last_block: true, block_number: 9 },
        ];
        for form in forms {
            let mut buf = [0u8; 32];
            round_trip(&form, &mut buf);
        }
    }

    /// The one encoding worth pinning by hand: `variable-name` is choice 2 and a
    /// big-endian sixteen-bit number, which is the whole point of short names.
    #[test]
    fn a_variable_name_is_three_bytes() {
        let mut buf = [0u8; 8];
        let mut w = SliceWriter::new(&mut buf);
        VariableAccess::VariableName(0xFA00).encode(&mut w).unwrap();
        assert_eq!(w.as_slice(), [0x02, 0xFA, 0x00]);
    }

    #[test]
    fn every_read_result_form_round_trips() {
        for form in [
            ReadResult::Data(Data::DoubleLongUnsigned(1234)),
            ReadResult::Error(DataAccessResult::ObjectUndefined),
            ReadResult::Block { last_block: true, block_number: 1, raw_data: &[0xAA] },
            ReadResult::BlockNumber(4),
        ] {
            let mut buf = [0u8; 32];
            round_trip(&form, &mut buf);
        }
    }

    #[test]
    fn every_write_result_form_round_trips() {
        for form in [
            WriteResult::Success,
            WriteResult::Error(DataAccessResult::ReadWriteDenied),
            WriteResult::BlockNumber(2),
        ] {
            let mut buf = [0u8; 8];
            round_trip(&form, &mut buf);
        }
    }

    #[test]
    fn a_report_without_a_time_still_says_so() {
        let mut buf = [0u8; 64];
        let mut w = SliceWriter::new(&mut buf);
        InformationReportRequest {
            current_time: None,
            specification: List::from_raw(0, &[]),
            values: List::from_raw(0, &[]),
        }
        .encode(&mut w)
        .unwrap();
        assert_eq!(w.as_slice(), [0x00, 0x00, 0x00], "usage flag, then two empty lists");
    }
}