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
#![allow(clippy::identity_op)]
use std::mem;

use anyhow::{anyhow, ensure, Result};
use modular_bitfield_msb::prelude::*;
use zerocopy::{AsBytes, FromBytes, LayoutVerified, Unaligned};

use crate::ccsds::space_packet::{self, SpacePacket};

#[bitfield(bytes = 2)]
#[derive(Debug, Default, Clone, FromBytes, AsBytes, Unaligned)]
#[repr(C)]
pub struct Header {
    #[skip]
    __: B5,
    pub first_header_pointer_raw: B11,
}

impl Header {
    pub const SIZE: usize = mem::size_of::<Self>();
}

impl Header {
    pub fn first_header_pointer(&self) -> FirstHeaderPointer {
        self.first_header_pointer_raw()
            .try_into()
            .expect("first_header_pointer_raw must be 11bits")
    }

    pub fn set_first_header_pointer(&mut self, first_header_pointer: FirstHeaderPointer) {
        self.set_first_header_pointer_raw(first_header_pointer.into());
    }
}

#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub enum FirstHeaderPointer {
    Pointer(u16),
    NoPacketStarts,
    IdleData,
}

impl FirstHeaderPointer {
    pub const ALL_ONES: u16 = 0b11111111111;
    pub const ALL_ONES_MINUS_ONE: u16 = 0b11111111110;
}

impl TryFrom<u16> for FirstHeaderPointer {
    type Error = anyhow::Error;

    fn try_from(raw: u16) -> Result<Self, Self::Error> {
        ensure!(raw <= Self::ALL_ONES, "too large first header pointer");
        match raw {
            Self::ALL_ONES => Ok(Self::NoPacketStarts),
            Self::ALL_ONES_MINUS_ONE => Ok(Self::IdleData),
            pointer => Ok(Self::Pointer(pointer)),
        }
    }
}

impl From<FirstHeaderPointer> for u16 {
    fn from(value: FirstHeaderPointer) -> Self {
        match value {
            FirstHeaderPointer::Pointer(pointer) => pointer,
            FirstHeaderPointer::NoPacketStarts => FirstHeaderPointer::ALL_ONES,
            FirstHeaderPointer::IdleData => FirstHeaderPointer::ALL_ONES_MINUS_ONE,
        }
    }
}

#[derive(Debug, Default)]
pub struct Defragmenter {
    buf: Vec<u8>,
}

impl Defragmenter {
    pub fn push(&mut self, m_pdu_bytes: &[u8]) -> Result<bool> {
        let (header, packet_zone) =
            LayoutVerified::<_, Header>::new_unaligned_from_prefix(m_pdu_bytes)
                .ok_or_else(|| anyhow!("given M_PDU is too small"))?;
        ensure!(
            packet_zone.len() > space_packet::PrimaryHeader::SIZE,
            "packet zone must be a Space Packet"
        );
        if self.buf.is_empty() {
            if let FirstHeaderPointer::Pointer(pointer) = header.first_header_pointer() {
                let offset = pointer as usize;
                let first_packet_bytes = &packet_zone
                    .get(offset..)
                    .ok_or_else(|| anyhow!("invalid first header pointer"))?;
                self.buf.extend_from_slice(first_packet_bytes);
                Ok(true)
            } else {
                Ok(false)
            }
        } else {
            match header.first_header_pointer() {
                FirstHeaderPointer::Pointer(_) | FirstHeaderPointer::NoPacketStarts => {
                    self.buf.extend_from_slice(packet_zone);
                    Ok(true)
                }
                FirstHeaderPointer::IdleData => Ok(false),
            }
        }
    }

    #[deprecated]
    pub fn read(&self) -> Option<SpacePacket<&'_ [u8]>> {
        let (_bytes, packet) = self.read_as_bytes_and_packet()?;
        Some(packet)
    }

    pub fn read_as_bytes_and_packet(&self) -> Option<(&[u8], SpacePacket<&'_ [u8]>)> {
        let buf = self.buf.as_slice();
        let (packet, trailer) = SpacePacket::new(buf)?;
        let bytes = &buf[..buf.len() - trailer.len()];
        Some((bytes, packet))
    }

    pub fn advance(&mut self) -> usize {
        let Some((packet, _trailer)) = SpacePacket::new(self.buf.as_slice()) else {
            return 0;
        };
        let size = packet.packet_size().expect(
            "packet_data.len() must be correct because it was constructed with ::new method",
        );
        self.buf.drain(..size);
        size
    }

    pub fn reset(&mut self) {
        self.buf.clear();
    }
}

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

    #[test]
    fn test() {
        let mut defrag = Defragmenter::default();
        let m_pdu1 = {
            let mut bytes = [0u8; Header::SIZE + space_packet::PrimaryHeader::SIZE + 1];
            let (mut m_pdu_hdr, pz) =
                LayoutVerified::<_, Header>::new_unaligned_from_prefix(bytes.as_mut_slice())
                    .unwrap();
            m_pdu_hdr.set_first_header_pointer_raw(0);
            let (mut ph, ud) =
                LayoutVerified::<_, space_packet::PrimaryHeader>::new_unaligned_from_prefix(pz)
                    .unwrap();
            ph.set_packet_data_length_in_bytes(1);
            ud[0] = 0xde;
            bytes
        };
        defrag.push(&m_pdu1).unwrap();
        let packet = defrag.read_as_bytes_and_packet().unwrap().1;
        assert_eq!(1, packet.packet_data.len());
        assert_eq!(0xDE, packet.packet_data[0]);
        let size = packet.packet_size().unwrap();
        assert_eq!(defrag.advance(), size);
        assert!(defrag.read_as_bytes_and_packet().is_none());
    }
}