1
use crate::errors::{parity, Error, Result};
2
use crate::word::{CommandWord, DataWord, StatusWord, Word};
3
use crate::WordType;
4

            
5
/// A packet of data parsed from binary
6
///
7
/// Incoming data is parsed into a triplet of (sync,data,parity)
8
/// using this struct, and then may be further parsed as an
9
/// explicit command, status, or data word.
10
///
11
/// ## Example
12
///
13
/// ```rust
14
/// # use mil_std_1553b::*;
15
/// # fn main() -> Result<()> {
16
///     let packet = Packet::new(
17
///         0b100,
18
///         [0b01000000, 0b00100000],
19
///         1
20
///     );
21
///     assert!(packet.is_service());
22
/// # Ok(())
23
/// # }
24
/// ```
25
///
26
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
27
pub struct Packet {
28
    /// The 3-bit sync pattern of a word
29
    pub sync: u8,
30

            
31
    /// The 16-bit body of a word
32
    pub body: [u8; 2],
33

            
34
    /// The 1-bit parity of a word
35
    pub parity: u8,
36
}
37

            
38
impl Packet {
39
    /// The leading sync pattern for a data word
40
    pub const DATA_SYNC: u8 = 0b001;
41

            
42
    /// The leading sync pattern for a non-data word
43
    pub const SERV_SYNC: u8 = 0b100;
44

            
45
    /// Create a new packet from sync, bytes, and parity
46
    ///
47
    /// # Arguments
48
    ///
49
    /// * `sync` - The leading 3 bit sync field as a u8
50
    /// * `body` - Two bytes of data following sync
51
    /// * `parity` - One bit parity field for the data as u8
52
    ///
53
248
    pub fn new(sync: u8, body: [u8; 2], parity: u8) -> Self {
54
248
        Self { sync, body, parity }
55
248
    }
56

            
57
    /// Parse a slice of bytes into sync, body, and parity
58
    ///
59
    /// This method interpretes the first 20 bits of the byte
60
    /// array as a triplet: 3-bit sync, 16-bit body, and 1-bit
61
    /// parity, given a bit offset at which to parse.
62
    ///
63
    /// # Arguments
64
    ///
65
    /// * `data` - A slice of bytes to parse
66
    /// * `offset` - The **bit** offset at which to begin parsing
67
    ///
68
    /// ## Example
69
    ///
70
    /// ```rust
71
    /// # use mil_std_1553b::*;
72
    /// # fn main() -> Result<()> {
73
    ///    let packet = Packet::read(&[
74
    ///        0b00000000,
75
    ///        0b00001111,
76
    ///        0b00000000,
77
    ///        0b00000011
78
    ///    ],12)?;
79
    ///
80
    ///    assert_eq!(packet.sync, 0b00000111);
81
    ///    assert_eq!(packet.body, [0b10000000,0b00000001]);
82
    ///    assert_eq!(packet.parity, 0b00000001);
83
    /// # Ok(())
84
    /// # }
85
    #[allow(clippy::if_same_then_else)]
86
102
    pub fn read(data: &[u8], offset: usize) -> Result<Self> {
87
102
        // if the offset won't fit in a u32
88
102
        if offset > 12 {
89
2
            return Err(Error::OutOfBounds);
90
        }
91
        // if the offset requires 4 bytes and
92
        // they weren't given
93
100
        else if offset > 4 && data.len() < 4 {
94
2
            return Err(Error::OutOfBounds);
95
        }
96
        // if the offset requires 3 bytes and
97
        // they weren't given
98
98
        else if data.len() < 3 {
99
2
            return Err(Error::OutOfBounds);
100
96
        }
101
96

            
102
96
        let mut buf: [u8; 4] = [0, 0, 0, 0];
103
96

            
104
96
        buf[0] = data.first().cloned().unwrap_or(0);
105
96
        buf[1] = data.get(1).cloned().unwrap_or(0);
106
96
        buf[2] = data.get(2).cloned().unwrap_or(0);
107
96
        buf[3] = data.get(3).cloned().unwrap_or(0);
108
96

            
109
96
        let mut v: u32 = u32::from_be_bytes(buf);
110
96

            
111
96
        v <<= offset;
112
96
        v >>= 12;
113
96

            
114
96
        let s = ((v & 0b11100000000000000000) >> 17) as u8;
115
96
        let w1 = ((v & 0b00011111111000000000) >> 9) as u8;
116
96
        let w2 = ((v & 0b00000000000111111110) >> 1) as u8;
117
96
        let p = (v & 0b00000000000000000001) as u8;
118
96

            
119
96
        Ok(Self::new(s, [w1, w2], p))
120
102
    }
121

            
122
    /// Write the packet to a byte array
123
    ///
124
    /// # Arguments
125
    ///
126
    /// * `data` - A slice of bytes to parse
127
    /// * `offset` - The **bit** offset at which to write
128
    ///
129
    #[allow(clippy::if_same_then_else)]
130
36
    pub fn write(&self, data: &mut [u8], offset: usize) -> Result<()> {
131
36
        // if the offset requires 4 bytes and
132
36
        // they weren't given
133
36
        if offset > 4 && data.len() < 4 {
134
2
            return Err(Error::OutOfBounds);
135
        }
136
        // if the offset requires 3 bytes and
137
        // they weren't given
138
34
        else if data.len() < 3 {
139
2
            return Err(Error::OutOfBounds);
140
32
        }
141
32

            
142
32
        let mut v: u32 = 0;
143
32
        let mut m: u32 = 0;
144
32
        let o = offset.clamp(0, 12);
145
32

            
146
32
        v |= ((self.sync & 0b00000111) as u32) << 29;
147
32
        v |= (self.body[0] as u32) << 21;
148
32
        v |= (self.body[1] as u32) << 13;
149
32
        v |= ((self.parity & 0b00000001) as u32) << 12;
150
32

            
151
32
        v >>= o;
152
32

            
153
32
        m |= (data[0] as u32) << 24;
154
32
        m |= (data[1] as u32) << 16;
155
32
        v |= m & !(u32::MAX >> o);
156

            
157
32
        let e = if offset > 4 { 4 } else { 3 };
158
32
        let result = v.to_be_bytes();
159
32

            
160
32
        data[..e].copy_from_slice(&result[..e]);
161
32

            
162
32
        Ok(())
163
36
    }
164

            
165
    /// Check the parity flag is correct
166
    #[must_use = "Result of check is never used"]
167
64
    pub fn check_parity(&self) -> bool {
168
64
        parity(u16::from_be_bytes(self.body)) == self.parity
169
64
    }
170

            
171
    /// Check the sync flag is correct
172
    #[must_use = "Result of check is never used"]
173
32
    pub fn check_sync(&self) -> bool {
174
32
        self.sync == Self::DATA_SYNC || self.sync == Self::SERV_SYNC
175
32
    }
176

            
177
    /// Check if this packet is a data packet
178
    #[must_use = "Result of check is never used"]
179
56
    pub fn is_data(&self) -> bool {
180
56
        self.sync == Self::DATA_SYNC
181
56
    }
182

            
183
    /// Check if this packet is a service packet
184
    #[must_use = "Result of check is never used"]
185
56
    pub fn is_service(&self) -> bool {
186
56
        self.sync == Self::SERV_SYNC
187
56
    }
188

            
189
    /// Check if this packet has correct parity and sync
190
    #[must_use = "Result of check is never used"]
191
32
    pub fn is_valid(&self) -> bool {
192
32
        self.check_parity() && self.check_sync()
193
32
    }
194

            
195
    /// Convert this packet into a word
196
40
    pub fn as_word<T: Word>(&self) -> Result<T> {
197
40
        T::new()
198
40
            .with_bytes(self.body)
199
40
            .with_parity(self.parity)
200
40
            .build()
201
40
    }
202
}
203

            
204
impl TryFrom<&WordType> for Packet {
205
    type Error = Error;
206

            
207
34
    fn try_from(word: &WordType) -> Result<Self> {
208
34
        match word {
209
2
            WordType::None => Err(Error::InvalidWord),
210
            _ => Ok(Self::new(
211
32
                match word.is_data() {
212
18
                    true => Self::DATA_SYNC,
213
14
                    false => Self::SERV_SYNC,
214
                },
215
32
                word.bytes(),
216
32
                word.parity(),
217
            )),
218
        }
219
34
    }
220
}
221

            
222
impl TryFrom<WordType> for Packet {
223
    type Error = Error;
224

            
225
10
    fn try_from(word: WordType) -> Result<Self> {
226
10
        Self::try_from(&word)
227
10
    }
228
}
229

            
230
impl TryFrom<Packet> for CommandWord {
231
    type Error = Error;
232

            
233
16
    fn try_from(value: Packet) -> Result<Self> {
234
16
        if value.is_service() {
235
4
            value.as_word()
236
        } else {
237
12
            Err(Error::InvalidPacket)
238
        }
239
16
    }
240
}
241

            
242
impl TryFrom<Packet> for StatusWord {
243
    type Error = Error;
244

            
245
16
    fn try_from(value: Packet) -> Result<Self> {
246
16
        if value.is_service() {
247
4
            value.as_word()
248
        } else {
249
12
            Err(Error::InvalidPacket)
250
        }
251
16
    }
252
}
253

            
254
impl TryFrom<Packet> for DataWord {
255
    type Error = Error;
256

            
257
32
    fn try_from(value: Packet) -> Result<Self> {
258
32
        if value.is_data() {
259
20
            value.as_word()
260
        } else {
261
12
            Err(Error::InvalidPacket)
262
        }
263
32
    }
264
}
265

            
266
#[cfg(test)]
267
mod tests {
268
    use super::*;
269

            
270
    // ----------------------------------------------------------
271
    // Packet
272

            
273
8
    fn test_packet_write_success(offset: usize, input: &[u8], expected: &[u8]) {
274
8
        let mut buffer = [0; 4];
275
8

            
276
8
        // read then write the packet
277
8
        let packet = Packet::read(input, offset).unwrap();
278
8
        packet.write(&mut buffer, 0).unwrap();
279
8

            
280
8
        // compare the output with the original data
281
8
        assert_eq!(&buffer[..3], expected);
282
8
    }
283

            
284
    #[test]
285
2
    fn test_packet_write_success_0() {
286
2
        test_packet_write_success(
287
2
            0,
288
2
            &[0b11110000, 0b00000000, 0b00110000],
289
2
            &[0b11110000, 0b00000000, 0b00110000],
290
2
        );
291
2
    }
292

            
293
    #[test]
294
2
    fn test_packet_write_success_1() {
295
2
        test_packet_write_success(
296
2
            4,
297
2
            &[0b00001111, 0b00000000, 0b00000011],
298
2
            &[0b11110000, 0b00000000, 0b00110000],
299
2
        );
300
2
    }
301

            
302
    #[test]
303
2
    fn test_packet_write_success_2() {
304
2
        test_packet_write_success(
305
2
            6,
306
2
            &[0b00000011, 0b11000000, 0b00000000, 0b11000000],
307
2
            &[0b11110000, 0b00000000, 0b00110000],
308
2
        );
309
2
    }
310

            
311
    #[test]
312
2
    fn test_packet_write_success_3() {
313
2
        test_packet_write_success(
314
2
            12,
315
2
            &[0b00000000, 0b00001111, 0b00000000, 0b00000011],
316
2
            &[0b11110000, 0b00000000, 0b00110000],
317
2
        );
318
2
    }
319

            
320
4
    fn test_packet_write_fail(offset: usize, input: &[u8], output: &mut [u8], expected: bool) {
321
4
        let packet = Packet::read(input, 0).unwrap();
322
4
        let result = packet.write(output, offset);
323
4
        assert_eq!(result.is_ok(), expected);
324
4
    }
325

            
326
    #[test]
327
2
    fn test_packet_write_fail_0() {
328
2
        test_packet_write_fail(
329
2
            5,
330
2
            &[0b11110000, 0b00000000, 0b00110000],
331
2
            &mut [0, 0, 0],
332
2
            false,
333
2
        );
334
2
    }
335

            
336
    #[test]
337
2
    fn test_packet_write_fail_1() {
338
2
        test_packet_write_fail(0, &[0b11110000, 0b00000000, 0b00110000], &mut [0, 0], false);
339
2
    }
340

            
341
8
    fn test_packet_read_success(offset: usize, input: &[u8], sync: u8, body: [u8; 2], parity: u8) {
342
8
        let packet = Packet::read(input, offset).unwrap();
343
8
        assert_eq!(packet.sync, sync);
344
8
        assert_eq!(packet.body, body);
345
8
        assert_eq!(packet.parity, parity);
346
8
    }
347

            
348
    #[test]
349
2
    fn test_packet_read_success_0() {
350
2
        test_packet_read_success(
351
2
            0,
352
2
            &[0b11110000, 0b00000000, 0b00110000],
353
2
            0b00000111,
354
2
            [0b10000000, 0b00000001],
355
2
            0b00000001,
356
2
        );
357
2
    }
358

            
359
    #[test]
360
2
    fn test_packet_read_success_1() {
361
2
        test_packet_read_success(
362
2
            4,
363
2
            &[0b00001111, 0b00000000, 0b00000011],
364
2
            0b00000111,
365
2
            [0b10000000, 0b00000001],
366
2
            0b00000001,
367
2
        );
368
2
    }
369

            
370
    #[test]
371
2
    fn test_packet_read_success_2() {
372
2
        test_packet_read_success(
373
2
            6,
374
2
            &[0b00000011, 0b11000000, 0b00000000, 0b11000000],
375
2
            0b00000111,
376
2
            [0b10000000, 0b00000001],
377
2
            0b00000001,
378
2
        );
379
2
    }
380

            
381
    #[test]
382
2
    fn test_packet_read_success_3() {
383
2
        test_packet_read_success(
384
2
            12,
385
2
            &[0b00000000, 0b00001111, 0b00000000, 0b00000011],
386
2
            0b00000111,
387
2
            [0b10000000, 0b00000001],
388
2
            0b00000001,
389
2
        );
390
2
    }
391

            
392
6
    fn test_packet_read_fail(offset: usize, input: &[u8], expected: bool) {
393
6
        let result = Packet::read(input, offset);
394
6
        assert_eq!(result.is_ok(), expected);
395
6
    }
396

            
397
    #[test]
398
2
    fn test_packet_read_fail_0() {
399
2
        test_packet_read_fail(
400
2
            13,
401
2
            &[0b00000000, 0b00000111, 0b10000000, 0b00000001, 0b10000000],
402
2
            false,
403
2
        );
404
2
    }
405

            
406
    #[test]
407
2
    fn test_packet_read_fail_1() {
408
2
        test_packet_read_fail(12, &[0b00000000, 0b00000111, 0b10000000], false);
409
2
    }
410

            
411
    #[test]
412
2
    fn test_packet_read_fail_2() {
413
2
        test_packet_read_fail(0, &[0b11110000, 0b00110000], false);
414
2
    }
415

            
416
32
    fn test_packet_check_parity(sync: u8, body: [u8; 2], parity: u8, expected: bool) {
417
32
        let packet = Packet::new(sync, body, parity);
418
32
        assert_eq!(packet.check_parity(), expected);
419
32
    }
420

            
421
    #[test]
422
2
    fn test_packet_check_parity_0() {
423
2
        test_packet_check_parity(0b100, [0b10000000, 0b00000001], 1, true);
424
2
    }
425

            
426
    #[test]
427
2
    fn test_packet_check_parity_1() {
428
2
        test_packet_check_parity(0b100, [0b10000100, 0b00000001], 0, true);
429
2
    }
430

            
431
    #[test]
432
2
    fn test_packet_check_parity_2() {
433
2
        test_packet_check_parity(0b100, [0b10000100, 0b00001001], 1, true);
434
2
    }
435

            
436
    #[test]
437
2
    fn test_packet_check_parity_3() {
438
2
        test_packet_check_parity(0b100, [0b10000100, 0b10001001], 0, true);
439
2
    }
440

            
441
    #[test]
442
2
    fn test_packet_check_parity_4() {
443
2
        test_packet_check_parity(0b001, [0b10000000, 0b00000001], 1, true);
444
2
    }
445

            
446
    #[test]
447
2
    fn test_packet_check_parity_5() {
448
2
        test_packet_check_parity(0b001, [0b10000100, 0b00000001], 0, true);
449
2
    }
450

            
451
    #[test]
452
2
    fn test_packet_check_parity_6() {
453
2
        test_packet_check_parity(0b001, [0b10000100, 0b00001001], 1, true);
454
2
    }
455

            
456
    #[test]
457
2
    fn test_packet_check_parity_7() {
458
2
        test_packet_check_parity(0b001, [0b10000100, 0b10001001], 0, true);
459
2
    }
460

            
461
    #[test]
462
2
    fn test_packet_check_parity_8() {
463
2
        test_packet_check_parity(0b100, [0b10000000, 0b00000001], 0, false);
464
2
    }
465

            
466
    #[test]
467
2
    fn test_packet_check_parity_9() {
468
2
        test_packet_check_parity(0b100, [0b10000100, 0b00000001], 1, false);
469
2
    }
470

            
471
    #[test]
472
2
    fn test_packet_check_parity_10() {
473
2
        test_packet_check_parity(0b100, [0b10000100, 0b00001001], 0, false);
474
2
    }
475

            
476
    #[test]
477
2
    fn test_packet_check_parity_11() {
478
2
        test_packet_check_parity(0b100, [0b10000100, 0b10001001], 1, false);
479
2
    }
480

            
481
    #[test]
482
2
    fn test_packet_check_parity_12() {
483
2
        test_packet_check_parity(0b001, [0b10000000, 0b00000001], 0, false);
484
2
    }
485

            
486
    #[test]
487
2
    fn test_packet_check_parity_13() {
488
2
        test_packet_check_parity(0b001, [0b10000100, 0b00000001], 1, false);
489
2
    }
490

            
491
    #[test]
492
2
    fn test_packet_check_parity_14() {
493
2
        test_packet_check_parity(0b001, [0b10000100, 0b00001001], 0, false);
494
2
    }
495

            
496
    #[test]
497
2
    fn test_packet_check_parity_15() {
498
2
        test_packet_check_parity(0b001, [0b10000100, 0b10001001], 1, false);
499
2
    }
500

            
501
8
    fn test_packet_check_sync(sync: u8, body: [u8; 2], parity: u8, expected: bool) {
502
8
        let packet = Packet::new(sync, body, parity);
503
8
        assert_eq!(packet.check_sync(), expected);
504
8
    }
505

            
506
    #[test]
507
2
    fn test_packet_check_sync_0() {
508
2
        test_packet_check_sync(0b100, [0b10000000, 0b00000001], 1, true);
509
2
    }
510

            
511
    #[test]
512
2
    fn test_packet_check_sync_1() {
513
2
        test_packet_check_sync(0b100, [0b10000000, 0b10000001], 0, true);
514
2
    }
515

            
516
    #[test]
517
2
    fn test_packet_check_sync_2() {
518
2
        test_packet_check_sync(0b001, [0b10000000, 0b00000001], 1, true);
519
2
    }
520

            
521
    #[test]
522
2
    fn test_packet_check_sync_3() {
523
2
        test_packet_check_sync(0b001, [0b10000000, 0b10000001], 0, true);
524
2
    }
525

            
526
24
    fn test_packet_is_data(sync: u8, body: [u8; 2], parity: u8, expected: bool) {
527
24
        let packet = Packet::new(sync, body, parity);
528
24
        assert_eq!(packet.is_data(), expected);
529
24
    }
530

            
531
    #[test]
532
2
    fn test_packet_is_data_0() {
533
2
        test_packet_is_data(0b001, [0b10000000, 0b00000001], 1, true);
534
2
    }
535

            
536
    #[test]
537
2
    fn test_packet_is_data_1() {
538
2
        test_packet_is_data(0b001, [0b10000000, 0b10000001], 0, true);
539
2
    }
540

            
541
    #[test]
542
2
    fn test_packet_is_data_2() {
543
2
        test_packet_is_data(0b011, [0b10000000, 0b00000001], 1, false);
544
2
    }
545

            
546
    #[test]
547
2
    fn test_packet_is_data_3() {
548
2
        test_packet_is_data(0b011, [0b10000000, 0b10000001], 0, false);
549
2
    }
550

            
551
    #[test]
552
2
    fn test_packet_is_data_4() {
553
2
        test_packet_is_data(0b101, [0b10000000, 0b00000001], 1, false);
554
2
    }
555

            
556
    #[test]
557
2
    fn test_packet_is_data_5() {
558
2
        test_packet_is_data(0b101, [0b10000000, 0b10000001], 0, false);
559
2
    }
560

            
561
    #[test]
562
2
    fn test_packet_is_data_6() {
563
2
        test_packet_is_data(0b110, [0b10000000, 0b00000001], 1, false);
564
2
    }
565

            
566
    #[test]
567
2
    fn test_packet_is_data_7() {
568
2
        test_packet_is_data(0b110, [0b10000000, 0b10000001], 0, false);
569
2
    }
570

            
571
    #[test]
572
2
    fn test_packet_is_data_8() {
573
2
        test_packet_is_data(0b111, [0b10000000, 0b00000001], 1, false);
574
2
    }
575

            
576
    #[test]
577
2
    fn test_packet_is_data_9() {
578
2
        test_packet_is_data(0b111, [0b10000000, 0b10000001], 0, false);
579
2
    }
580

            
581
    #[test]
582
2
    fn test_packet_is_data_10() {
583
2
        test_packet_is_data(0b100, [0b10000000, 0b00000001], 1, false);
584
2
    }
585

            
586
    #[test]
587
2
    fn test_packet_is_data_11() {
588
2
        test_packet_is_data(0b100, [0b10000000, 0b10000001], 0, false);
589
2
    }
590

            
591
24
    fn test_packet_is_service(sync: u8, body: [u8; 2], parity: u8, expected: bool) {
592
24
        let packet = Packet::new(sync, body, parity);
593
24
        assert_eq!(packet.is_service(), expected);
594
24
    }
595

            
596
    #[test]
597
2
    fn test_packet_is_service_0() {
598
2
        test_packet_is_service(0b001, [0b10000000, 0b00000001], 1, false);
599
2
    }
600

            
601
    #[test]
602
2
    fn test_packet_is_service_1() {
603
2
        test_packet_is_service(0b001, [0b10000000, 0b10000001], 0, false);
604
2
    }
605

            
606
    #[test]
607
2
    fn test_packet_is_service_2() {
608
2
        test_packet_is_service(0b011, [0b10000000, 0b00000001], 1, false);
609
2
    }
610

            
611
    #[test]
612
2
    fn test_packet_is_service_3() {
613
2
        test_packet_is_service(0b011, [0b10000000, 0b10000001], 0, false);
614
2
    }
615

            
616
    #[test]
617
2
    fn test_packet_is_service_4() {
618
2
        test_packet_is_service(0b101, [0b10000000, 0b00000001], 1, false);
619
2
    }
620

            
621
    #[test]
622
2
    fn test_packet_is_service_5() {
623
2
        test_packet_is_service(0b101, [0b10000000, 0b10000001], 0, false);
624
2
    }
625

            
626
    #[test]
627
2
    fn test_packet_is_service_6() {
628
2
        test_packet_is_service(0b110, [0b10000000, 0b00000001], 1, false);
629
2
    }
630

            
631
    #[test]
632
2
    fn test_packet_is_service_7() {
633
2
        test_packet_is_service(0b110, [0b10000000, 0b10000001], 0, false);
634
2
    }
635

            
636
    #[test]
637
2
    fn test_packet_is_service_8() {
638
2
        test_packet_is_service(0b111, [0b10000000, 0b00000001], 1, false);
639
2
    }
640

            
641
    #[test]
642
2
    fn test_packet_is_service_9() {
643
2
        test_packet_is_service(0b111, [0b10000000, 0b10000001], 0, false);
644
2
    }
645

            
646
    #[test]
647
2
    fn test_packet_is_service_10() {
648
2
        test_packet_is_service(0b100, [0b10000000, 0b00000001], 1, true);
649
2
    }
650

            
651
    #[test]
652
2
    fn test_packet_is_service_11() {
653
2
        test_packet_is_service(0b100, [0b10000000, 0b10000001], 0, true);
654
2
    }
655

            
656
32
    fn test_packet_is_valid(sync: u8, body: [u8; 2], parity: u8, expected: bool) {
657
32
        let packet = Packet::new(sync, body, parity);
658
32
        assert_eq!(packet.is_valid(), expected);
659
32
    }
660

            
661
    #[test]
662
2
    fn test_packet_is_valid_0() {
663
2
        test_packet_is_valid(0b001, [0b10000000, 0b00000001], 1, true);
664
2
    }
665

            
666
    #[test]
667
2
    fn test_packet_is_valid_1() {
668
2
        test_packet_is_valid(0b001, [0b10000000, 0b10000001], 0, true);
669
2
    }
670

            
671
    #[test]
672
2
    fn test_packet_is_valid_2() {
673
2
        test_packet_is_valid(0b001, [0b10001000, 0b00000000], 0, false);
674
2
    }
675

            
676
    #[test]
677
2
    fn test_packet_is_valid_3() {
678
2
        test_packet_is_valid(0b001, [0b10000011, 0b10000001], 1, false);
679
2
    }
680

            
681
    #[test]
682
2
    fn test_packet_is_valid_4() {
683
2
        test_packet_is_valid(0b011, [0b10000000, 0b00000001], 1, false);
684
2
    }
685

            
686
    #[test]
687
2
    fn test_packet_is_valid_5() {
688
2
        test_packet_is_valid(0b011, [0b10000000, 0b10000001], 0, false);
689
2
    }
690

            
691
    #[test]
692
2
    fn test_packet_is_valid_6() {
693
2
        test_packet_is_valid(0b101, [0b10000000, 0b00000001], 1, false);
694
2
    }
695

            
696
    #[test]
697
2
    fn test_packet_is_valid_7() {
698
2
        test_packet_is_valid(0b101, [0b10000000, 0b10000001], 0, false);
699
2
    }
700

            
701
    #[test]
702
2
    fn test_packet_is_valid_8() {
703
2
        test_packet_is_valid(0b110, [0b10000000, 0b00000001], 1, false);
704
2
    }
705

            
706
    #[test]
707
2
    fn test_packet_is_valid_9() {
708
2
        test_packet_is_valid(0b110, [0b10000000, 0b10000001], 0, false);
709
2
    }
710

            
711
    #[test]
712
2
    fn test_packet_is_valid_10() {
713
2
        test_packet_is_valid(0b111, [0b10000000, 0b00000001], 1, false);
714
2
    }
715

            
716
    #[test]
717
2
    fn test_packet_is_valid_11() {
718
2
        test_packet_is_valid(0b111, [0b10000000, 0b10000001], 0, false);
719
2
    }
720

            
721
    #[test]
722
2
    fn test_packet_is_valid_12() {
723
2
        test_packet_is_valid(0b100, [0b10000000, 0b00000001], 1, true);
724
2
    }
725

            
726
    #[test]
727
2
    fn test_packet_is_valid_13() {
728
2
        test_packet_is_valid(0b100, [0b10000000, 0b10000001], 0, true);
729
2
    }
730

            
731
    #[test]
732
2
    fn test_packet_is_valid_14() {
733
2
        test_packet_is_valid(0b100, [0b10000000, 0b00100000], 0, false);
734
2
    }
735

            
736
    #[test]
737
2
    fn test_packet_is_valid_15() {
738
2
        test_packet_is_valid(0b100, [0b10000001, 0b10000000], 1, false);
739
2
    }
740

            
741
    // ----------------------------------------------------------
742
    // Derives
743

            
744
    #[test]
745
2
    fn test_packet_clone() {
746
2
        let word = WordType::Command(CommandWord::new());
747
2
        let packet1 = Packet::try_from(word).unwrap();
748
2
        let packet2 = packet1.clone();
749
2
        assert_eq!(packet1, packet2);
750
2
    }
751

            
752
    // ----------------------------------------------------------
753

            
754
    // ----------------------------------------------------------
755
    // Traits
756

            
757
8
    fn test_packet_try_from_word(word: WordType, expected: bool) {
758
8
        let result = Packet::try_from(word);
759
8
        assert_eq!(result.is_ok(), expected);
760
8
    }
761

            
762
    #[test]
763
2
    fn test_packet_try_from_word_0() {
764
2
        test_packet_try_from_word(WordType::Command(CommandWord::new()), true);
765
2
    }
766

            
767
    #[test]
768
2
    fn test_packet_try_from_word_1() {
769
2
        test_packet_try_from_word(WordType::Status(StatusWord::new()), true);
770
2
    }
771

            
772
    #[test]
773
2
    fn test_packet_try_from_word_2() {
774
2
        test_packet_try_from_word(WordType::Data(DataWord::new()), true);
775
2
    }
776

            
777
    #[test]
778
2
    fn test_packet_try_from_word_3() {
779
2
        test_packet_try_from_word(WordType::None, false);
780
2
    }
781

            
782
16
    fn test_command_try_from_packet(input: &[u8], expected: bool) {
783
16
        let result = CommandWord::try_from(Packet::read(input, 0).unwrap());
784
16
        assert_eq!(result.is_ok(), expected);
785
16
    }
786

            
787
    #[test]
788
2
    fn test_command_try_from_packet_0() {
789
2
        test_command_try_from_packet(&[0b00100000, 0b00000000, 0b00010000], false);
790
2
    }
791

            
792
    #[test]
793
2
    fn test_command_try_from_packet_1() {
794
2
        test_command_try_from_packet(&[0b01000000, 0b00000000, 0b00010000], false);
795
2
    }
796

            
797
    #[test]
798
2
    fn test_command_try_from_packet_2() {
799
2
        test_command_try_from_packet(&[0b01100000, 0b00000000, 0b00010000], false);
800
2
    }
801

            
802
    #[test]
803
2
    fn test_command_try_from_packet_3() {
804
2
        test_command_try_from_packet(&[0b10100000, 0b00000000, 0b00010000], false);
805
2
    }
806

            
807
    #[test]
808
2
    fn test_command_try_from_packet_4() {
809
2
        test_command_try_from_packet(&[0b11000000, 0b00000000, 0b00010000], false);
810
2
    }
811

            
812
    #[test]
813
2
    fn test_command_try_from_packet_5() {
814
2
        test_command_try_from_packet(&[0b10100000, 0b00000000, 0b00010000], false);
815
2
    }
816

            
817
    #[test]
818
2
    fn test_command_try_from_packet_6() {
819
2
        test_command_try_from_packet(&[0b10000000, 0b00000000, 0b00010000], true);
820
2
    }
821

            
822
    #[test]
823
2
    fn test_command_try_from_packet_7() {
824
2
        test_command_try_from_packet(&[0b10000000, 0b00000000, 0b00010000], true);
825
2
    }
826

            
827
16
    fn test_status_try_from_packet(input: &[u8], expected: bool) {
828
16
        let result = StatusWord::try_from(Packet::read(input, 0).unwrap());
829
16
        assert_eq!(result.is_ok(), expected);
830
16
    }
831

            
832
    #[test]
833
2
    fn test_status_try_from_packet_0() {
834
2
        test_status_try_from_packet(&[0b00100000, 0b00000000, 0b00010000], false);
835
2
    }
836

            
837
    #[test]
838
2
    fn test_status_try_from_packet_1() {
839
2
        test_status_try_from_packet(&[0b01000000, 0b00000000, 0b00010000], false);
840
2
    }
841

            
842
    #[test]
843
2
    fn test_status_try_from_packet_2() {
844
2
        test_status_try_from_packet(&[0b01100000, 0b00000000, 0b00010000], false);
845
2
    }
846

            
847
    #[test]
848
2
    fn test_status_try_from_packet_3() {
849
2
        test_status_try_from_packet(&[0b10100000, 0b00000000, 0b00010000], false);
850
2
    }
851

            
852
    #[test]
853
2
    fn test_status_try_from_packet_4() {
854
2
        test_status_try_from_packet(&[0b11000000, 0b00000000, 0b00010000], false);
855
2
    }
856

            
857
    #[test]
858
2
    fn test_status_try_from_packet_5() {
859
2
        test_status_try_from_packet(&[0b10100000, 0b00000000, 0b00010000], false);
860
2
    }
861

            
862
    #[test]
863
2
    fn test_status_try_from_packet_6() {
864
2
        test_status_try_from_packet(&[0b10000000, 0b00000000, 0b00010000], true);
865
2
    }
866

            
867
    #[test]
868
2
    fn test_status_try_from_packet_7() {
869
2
        test_status_try_from_packet(&[0b10000000, 0b00000000, 0b00010000], true);
870
2
    }
871

            
872
16
    fn test_data_try_from_packet(input: &[u8], expected: bool) {
873
16
        let result = DataWord::try_from(Packet::read(input, 0).unwrap());
874
16
        assert_eq!(result.is_ok(), expected);
875
16
    }
876

            
877
    #[test]
878
2
    fn test_data_try_from_packet_0() {
879
2
        test_data_try_from_packet(&[0b01000000, 0b00000000, 0b00010000], false);
880
2
    }
881

            
882
    #[test]
883
2
    fn test_data_try_from_packet_1() {
884
2
        test_data_try_from_packet(&[0b01100000, 0b00000000, 0b00010000], false);
885
2
    }
886

            
887
    #[test]
888
2
    fn test_data_try_from_packet_2() {
889
2
        test_data_try_from_packet(&[0b10000000, 0b00000000, 0b00010000], false);
890
2
    }
891

            
892
    #[test]
893
2
    fn test_data_try_from_packet_3() {
894
2
        test_data_try_from_packet(&[0b10100000, 0b00000000, 0b00010000], false);
895
2
    }
896

            
897
    #[test]
898
2
    fn test_data_try_from_packet_4() {
899
2
        test_data_try_from_packet(&[0b11000000, 0b00000000, 0b00010000], false);
900
2
    }
901

            
902
    #[test]
903
2
    fn test_data_try_from_packet_5() {
904
2
        test_data_try_from_packet(&[0b10100000, 0b00000000, 0b00010000], false);
905
2
    }
906

            
907
    #[test]
908
2
    fn test_data_try_from_packet_6() {
909
2
        test_data_try_from_packet(&[0b00100000, 0b00000000, 0b00010000], true);
910
2
    }
911

            
912
    #[test]
913
2
    fn test_data_try_from_packet_7() {
914
2
        test_data_try_from_packet(&[0b00100000, 0b00000000, 0b00010000], true);
915
2
    }
916

            
917
    // ----------------------------------------------------------
918
}