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
/// Container for extended diagnostics data
///
/// The [`ExtendedDiagnostics::iter_diag_blocks()`] method can be used to iterate over the
/// diagnostics blocks contained in this data.
#[derive(Default, PartialEq, Eq)]
pub struct ExtendedDiagnostics<'a> {
    buffer: &'a mut [u8],
    length: usize,
}

impl<'a> ExtendedDiagnostics<'a> {
    /// Whether extended diagnostics are even collected.
    ///
    /// This will return `true` when a buffer for extended diagnostics exists.
    pub fn is_available(&self) -> bool {
        self.buffer.len() > 0
    }

    /// Iterate over diagnostics blocks in the extended diagnostics.
    ///
    /// The iterator yields an [`ExtDiagBlock`] for each diagnostics block.
    pub fn iter_diag_blocks(&self) -> ExtDiagBlockIter<'_> {
        // TODO: is_available() guard?
        ExtDiagBlockIter {
            ext_diag: self,
            cursor: 0,
        }
    }

    /// Access the raw extended diagnostics buffer.
    ///
    /// Returns `None` when no extended diagnostics information is not captured (because no buffer
    /// was prepared for it, see
    /// [`Peripheral::with_diag_buffer()`][`crate::dp::Peripheral::with_diag_buffer`]).
    ///
    /// Returns `Some([])` when no extended diagnostics information was reported by the peripheral.
    pub fn raw_diag_buffer(&self) -> Option<&[u8]> {
        if !self.is_available() {
            None
        } else {
            Some(&self.buffer[..self.length])
        }
    }

    pub(crate) fn from_buffer(buffer: &'a mut [u8]) -> Self {
        Self { buffer, length: 0 }
    }

    pub(crate) fn fill(&mut self, buf: &[u8]) -> bool {
        if self.buffer.len() == 0 {
            // No buffer for ext. diagnostics so we ignore them entirely.
            false
        } else if self.buffer.len() < buf.len() {
            log::warn!(
                "Buffer too small for received ext. diagnostics, ignoring. ({} < {})",
                self.buffer.len(),
                buf.len()
            );
            false
        } else {
            self.buffer[..buf.len()].copy_from_slice(buf);
            self.length = buf.len();
            true
        }
    }
}

impl<'a> core::fmt::Debug for ExtendedDiagnostics<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut dbg_list = f.debug_list();
        if self.is_available() {
            // TODO: Debug impl should also somehow display invalid diagnostics data
            for block in self.iter_diag_blocks() {
                dbg_list.entry(&block);
            }
        }
        dbg_list.finish()
    }
}

/// Data type for a channel of a module
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum ChannelDataType {
    Bit = 0b001,
    Bit2 = 0b010,
    Bit4 = 0b011,
    Byte = 0b100,
    Word = 0b101,
    DWord = 0b110,
    Invalid = 0b111,
}

impl ChannelDataType {
    fn from_diag_byte2(b: u8) -> Self {
        match b >> 5 {
            0b001 => ChannelDataType::Bit,
            0b010 => ChannelDataType::Bit2,
            0b011 => ChannelDataType::Bit4,
            0b100 => ChannelDataType::Byte,
            0b101 => ChannelDataType::Word,
            0b110 => ChannelDataType::DWord,
            _ => ChannelDataType::Invalid,
        }
    }

    fn into_diag_byte2(self) -> u8 {
        (self as u8) << 5
    }
}

/// Error diagnosed at a channel of a module
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum ChannelError {
    ShortCircuit = 1,
    UnderVoltage = 2,
    OverVoltage = 3,
    OverLoad = 4,
    OverTemperature = 5,
    LineBreak = 6,
    UpperLimitOvershoot = 7,
    LowerLimitUndershoot = 8,
    Error = 9,
    Reserved(u8),
    Vendor(u8),
}

impl ChannelError {
    fn from_diag_byte2(b: u8) -> Self {
        match b & 0x1f {
            1 => ChannelError::ShortCircuit,
            2 => ChannelError::UnderVoltage,
            3 => ChannelError::OverVoltage,
            4 => ChannelError::OverLoad,
            5 => ChannelError::OverTemperature,
            6 => ChannelError::LineBreak,
            7 => ChannelError::UpperLimitOvershoot,
            8 => ChannelError::LowerLimitUndershoot,
            9 => ChannelError::Error,
            v @ 16..=31 => ChannelError::Vendor(v),
            r @ _ => ChannelError::Reserved(r),
        }
    }

    fn into_diag_byte2(self) -> u8 {
        match self {
            ChannelError::ShortCircuit => 1,
            ChannelError::UnderVoltage => 2,
            ChannelError::OverVoltage => 3,
            ChannelError::OverLoad => 4,
            ChannelError::OverTemperature => 5,
            ChannelError::LineBreak => 6,
            ChannelError::UpperLimitOvershoot => 7,
            ChannelError::LowerLimitUndershoot => 8,
            ChannelError::Error => 9,
            ChannelError::Vendor(v) => v,
            ChannelError::Reserved(r) => r,
        }
    }
}

/// Diagnostic information for a module channel
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChannelDiagnostics {
    /// Module where the problem was reported
    ///
    /// Corresponds to the module index from the configuration telegram
    pub module: u8,
    /// Channel of the module where the problem occurred
    pub channel: u8,
    /// Whether the channel is an input
    pub input: bool,
    /// Whether the channel is an output
    pub output: bool,
    /// Data type of the channel
    pub dtype: ChannelDataType,
    /// Error diagnosed at this channel
    pub error: ChannelError,
}

/// One extended diagnostics block
#[derive(Clone, PartialEq, Eq)]
pub enum ExtDiagBlock<'a> {
    /// Identifier-based diagnostics
    ///
    /// The bit-slice contains information which modules report problems.  The index of each bit
    /// that is set in the slice indicates a problem with the corresponding module.  The indices
    /// match the ones from the configuration telegram.
    Identifier(&'a bitvec::slice::BitSlice<u8>),
    /// Channel-based diagnostics
    ///
    /// See [`ChannelDiagnostics`] for details.
    Channel(ChannelDiagnostics),
    /// Device-based diagnostics
    ///
    /// The diagnostic data needs to be interpreted using device-specific information from the GSD
    /// file.  `gsdtool` has a `diagnostics` subcommand which can dissect a device-based
    /// diagnostics buffer and print human-readable information about the diagnostics it encodes.
    Device(&'a [u8]),
}

struct IdentifierDebug<'a>(&'a bitvec::slice::BitSlice<u8>);

impl<'a> core::fmt::Debug for IdentifierDebug<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut dbg_list = f.debug_list();
        for i in self.0.iter_ones() {
            dbg_list.entry(&i);
        }
        dbg_list.finish()
    }
}

impl<'a> core::fmt::Debug for ExtDiagBlock<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            ExtDiagBlock::Identifier(i) => f
                .debug_tuple("Identifier")
                .field(&IdentifierDebug(i))
                .finish(),
            ExtDiagBlock::Channel(c) => f.debug_tuple("Channel").field(c).finish(),
            ExtDiagBlock::Device(d) => f.debug_tuple("Device").field(d).finish(),
        }
    }
}

/// Iterator over the [`ExtDiagBlock`]s contained in an [`ExtendedDiagnostics`] data buffer
pub struct ExtDiagBlockIter<'a> {
    ext_diag: &'a ExtendedDiagnostics<'a>,
    cursor: usize,
}

impl<'a> Iterator for ExtDiagBlockIter<'a> {
    type Item = ExtDiagBlock<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        let raw_buffer = self.ext_diag.raw_diag_buffer().unwrap();
        if self.cursor >= raw_buffer.len() {
            return None;
        }

        let remainder = &raw_buffer[self.cursor..];
        let header = remainder[0];
        match header >> 6 {
            // Identifier-based Diagnostics
            0b01 => {
                let length = usize::from(header & 0x3f);
                if remainder.len() < length {
                    log::warn!("Diagnostics cut off: {:?}", remainder);
                    self.cursor = raw_buffer.len();
                    return None;
                }

                self.cursor += length;
                Some(ExtDiagBlock::Identifier(
                    bitvec::slice::BitSlice::from_slice(&remainder[1..length]),
                ))
            }
            // Channel-based Diagnostics
            0b10 => {
                if remainder.len() < 3 {
                    log::warn!("Diagnostics cut off: {:?}", remainder);
                    self.cursor = raw_buffer.len();
                    return None;
                }

                self.cursor += 3;
                Some(ExtDiagBlock::Channel(ChannelDiagnostics {
                    module: remainder[0] & 0x3f,
                    channel: remainder[1] & 0x3f,
                    input: remainder[1] & 0x40 != 0,
                    output: remainder[1] & 0x80 != 0,
                    dtype: ChannelDataType::from_diag_byte2(remainder[2]),
                    error: ChannelError::from_diag_byte2(remainder[2]),
                }))
            }
            // Device-based Diagnostics
            0b00 => {
                let length = usize::from(header & 0x3f);
                if remainder.len() < length {
                    log::warn!("Diagnostics cut off: {:?}", remainder);
                    self.cursor = raw_buffer.len();
                    return None;
                }

                self.cursor += length;
                Some(ExtDiagBlock::Device(&remainder[1..length]))
            }
            // Reserved
            0b11 => {
                log::warn!("Unexpected ext diag block: {:?}", remainder);
                self.cursor = raw_buffer.len();
                None
            }
            _ => unreachable!(),
        }
    }
}

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

    #[test]
    fn test_diag_byte2() {
        for b in 0..=255u8 {
            // Filter edge cases
            if b & 0xe0 == 0 {
                continue;
            }

            let err = ChannelError::from_diag_byte2(b);
            let dtype = ChannelDataType::from_diag_byte2(b);

            let b_again = err.into_diag_byte2() | dtype.into_diag_byte2();
            assert_eq!(b, b_again);
        }
    }

    #[test]
    fn test_diag_iter() {
        let mut buffer = [
            0x44, 0x00, 0x01, 0x00, 0x88, 0x41, 0x21, 0x04, 0x10, 0x20, 0x30,
        ];
        let ext_diag = ExtendedDiagnostics {
            length: buffer.len(),
            buffer: &mut buffer[..],
        };

        let blocks: Vec<ExtDiagBlock> = ext_diag.iter_diag_blocks().collect();

        if let ExtDiagBlock::Identifier(i) = &blocks[0] {
            assert!(i.get(8).unwrap());
            assert!(i.count_ones() == 1);
            assert_eq!(i.len(), 24);
        } else {
            panic!("wrong diag block 0 {:?}", blocks[0]);
        }

        if let ExtDiagBlock::Channel(c) = &blocks[1] {
            assert_eq!(
                c,
                &ChannelDiagnostics {
                    module: 8,
                    channel: 1,
                    input: true,
                    output: false,
                    dtype: ChannelDataType::Bit,
                    error: ChannelError::ShortCircuit
                }
            );
        } else {
            panic!("wrong diag block 1 {:?}", blocks[1]);
        }

        if let ExtDiagBlock::Device(d) = &blocks[2] {
            assert_eq!(d, &[0x10, 0x20, 0x30]);
        } else {
            panic!("wrong diag block 2 {:?}", blocks[2]);
        }

        assert_eq!(blocks.len(), 3);
    }

    #[test]
    fn test_diag_iter_invalid() {
        let mut buffer = [0x44, 0x00, 0x01, 0x00, 0xff, 0x12, 0x34];
        let ext_diag = ExtendedDiagnostics {
            length: buffer.len(),
            buffer: &mut buffer[..],
        };

        let blocks: Vec<ExtDiagBlock> = ext_diag.iter_diag_blocks().collect();

        if let ExtDiagBlock::Identifier(i) = &blocks[0] {
            assert!(i.get(8).unwrap());
            assert!(i.count_ones() == 1);
            assert_eq!(i.len(), 24);
        } else {
            panic!("wrong diag block 0 {:?}", blocks[0]);
        }

        assert_eq!(blocks.len(), 1);
    }

    #[test]
    fn test_diag_iter_short() {
        // Identifier-based
        let mut buffer = [0x48, 0x00, 0x01, 0x00];
        let ext_diag = ExtendedDiagnostics {
            length: buffer.len(),
            buffer: &mut buffer[..],
        };

        let blocks = ext_diag.iter_diag_blocks().count();
        assert_eq!(blocks, 0);

        // Channel-based
        let mut buffer = [0x88, 0x00];
        let ext_diag = ExtendedDiagnostics {
            length: buffer.len(),
            buffer: &mut buffer[..],
        };

        let blocks = ext_diag.iter_diag_blocks().count();
        assert_eq!(blocks, 0);

        // Device-based
        let mut buffer = [0x08, 0x00, 0x01, 0x00];
        let ext_diag = ExtendedDiagnostics {
            length: buffer.len(),
            buffer: &mut buffer[..],
        };

        let blocks = ext_diag.iter_diag_blocks().count();
        assert_eq!(blocks, 0);
    }
}