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
//! Generate data packets.

use bits;
use data::{self, payload, coder, interleave};

/// Construct a confirmed data packet with the given header, payload blocks, and serial
/// number generator. The returned value is the coded, interleaved set of dibit symbols
/// that make up the packet.
pub fn confirmed<S>(header: data::ConfirmedHeader, payload: data::ConfirmedPayload,
                    mut sn: S)
    -> Vec<bits::Dibit> where S: Iterator<Item = u8>
{
    let mut pkt = vec![];

    // Add in the header.
    pkt.extend({
        let (fields, checksum) = header.build();

        coder::DibitCoder::new()
            .feed_bytes(fields.iter().cloned())
            .feed_bytes(checksum.iter().cloned())
            .finish()
            .iter().cloned()
    });

    // Add in the normal data blocks.
    for block in payload.iter() {
        pkt.extend({
            let (data, pads) = block.build();
            let header = payload::ConfirmedBlockHeader::new(sn.next().unwrap(),
                data, pads.clone()).build();

            interleave::Interleaver::new(coder::TribitCoder::new()
                .feed_bytes(header.iter().cloned()
                    .chain(data.iter().cloned())
                    .chain(pads.map(|_| 0)))
                .finish())
        });
    }

    // Add in the tail block.
    pkt.extend({
        let (data, pads, checksum) = payload.tail().build();
        let header = payload::ConfirmedBlockHeader::new(sn.next().unwrap(),
            data, pads.clone()).build();

        interleave::Interleaver::new(coder::TribitCoder::new()
            .feed_bytes(header.iter().cloned()
                .chain(data.iter().cloned())
                .chain(pads.map(|_| 0))
                .chain(checksum.iter().cloned()))
            .finish())
    });

    pkt
}

/// Construct an unconfirmed data packet with the given header and payload blocks. The
/// returned value is the coded, interleaved set of dibit symbols that make up the packet.
pub fn unconfirmed(header: data::UnconfirmedHeader, payload: data::UnconfirmedPayload)
    -> Vec<bits::Dibit>
{
    let mut pkt = vec![];

    pkt.extend({
        let (fields, checksum) = header.build();

        coder::DibitCoder::new()
            .feed_bytes(fields.iter().cloned())
            .feed_bytes(checksum.iter().cloned())
            .finish()
            .iter().cloned()
    });

    for block in payload.iter() {
        let (data, pads) = block.build();

        pkt.extend({
            interleave::Interleaver::new(coder::DibitCoder::new()
                .feed_bytes(data.iter().cloned())
                .feed_bytes(pads.map(|_| 0))
                .finish())
        });
    }

    pkt.extend({
        let (data, pads, checksum) = payload.tail().build();

        interleave::Interleaver::new(coder::DibitCoder::new()
            .feed_bytes(data.iter().cloned())
            .feed_bytes(pads.map(|_| 0))
            .feed_bytes(checksum.iter().cloned())
            .finish())
    });

    pkt
}