spp_rust/
pri_header.rs

1use bitvec::prelude::*;
2
3use crate::errors::SPPError;
4
5#[derive(Debug)]
6pub struct PrimaryHeader {
7    version_number: BitArr!(for 3, in u8),             // 3 bits
8    id: Identification,                 // 13 bits
9    sequence_control: SequenceControl,  // 16 bits
10    pub data_length: BitArr!(for 16, in u8, Msb0),                // 16 bits
11}
12
13impl PrimaryHeader {
14    pub fn new(id: &Identification, seq: &SequenceControl) -> Self {
15        Self {version_number: bitarr![u8, LocalBits; 0; 3], id: id.clone(), sequence_control: seq.clone(), data_length: bitarr![u8, Msb0; 0; 16]}
16    }
17
18    pub fn new_from_slice(s: &BitSlice<u8>) -> Self {
19        let mut version_number = bitarr!(u8, LocalBits; 0; 3);
20        version_number[..3].copy_from_bitslice(&s[..3]);
21
22        let id = Identification::new_from_slice(&s[3..16]);
23        let sequence_control = SequenceControl::new_from_slice(&s[16..32]);
24
25        let mut data_length = bitarr!(u8, Msb0; 0; 16);
26        data_length[..16].clone_from_bitslice(&s[32..48]);
27
28        Self { version_number, id, sequence_control, data_length }
29    }
30
31    // NOTE: 4.1.3.5
32    pub fn data_lenght(&mut self, size: usize) {
33        // TODO: this does not fail if size is bigger than u16_max
34        let u = size as u16;
35        let binding = [u];
36
37        let v: &BitSlice<u16, LocalBits> = BitSlice::from_slice(&binding);
38        
39        
40        let m = self.data_length.as_mut_bitslice();
41        m.clone_from_bitslice(v);
42        m.reverse();
43    }
44
45    pub fn to_bits<'a>(&self, _aux: &'a mut BitSlice<u8>) {
46        _aux[..3].copy_from_bitslice(&self.version_number[..3]);
47
48        self.id.to_bits(&mut _aux[3..3+13]);
49
50        self.sequence_control.to_bits(&mut _aux[16..32]);
51
52        _aux[32..48].clone_from_bitslice(&self.data_length[..16]);
53    }
54
55}
56
57#[derive(Debug, Clone)]
58pub enum PacketType {
59    Telemetry,
60    Telecommand,
61}
62
63impl PacketType {
64    fn from_bool(b: bool) -> Self {
65        match b {
66            true => Self::Telecommand,
67            false => Self::Telemetry,
68        }
69    }
70
71    fn to_bool(&self) -> bool {
72        match self {
73            Self::Telecommand => true,
74            Self::Telemetry => false,
75        }
76    }
77}
78
79#[derive(Debug, Clone)]
80pub enum SecHeaderFlag {
81    Present,
82    NotPresent,
83    Idle,
84}
85
86impl SecHeaderFlag {
87    fn from_bool(b: bool) -> Self {
88        match b {
89            true => Self::Present,
90            false => Self::NotPresent,
91        }
92    }
93    fn to_bool(&self) -> bool {
94        match self {
95            Self::Present => true,
96            Self::NotPresent => false,
97            Self::Idle => false,
98        }
99    }
100}
101
102#[derive(Debug, Clone)]
103pub struct Identification {
104    pub packet_type: PacketType,
105    pub sec_header_flag: SecHeaderFlag, // For Idle Packets: '0'
106    app_process_id:  BitArray<[u8; 2]> // 11 bits // For Idle Packets: '11111111111'
107}
108
109impl<'a> Identification {
110    pub fn new(t: PacketType, head: SecHeaderFlag, app: &BitSlice<u8> ) -> Result<Self, SPPError> {
111        if app.len() != 11 {
112            return Err(SPPError::APIDLenMismatch(app.len())); // APID is more than 11 bits
113        }
114
115        let mut a = bitarr!(u8, LocalBits; 0; 11);
116        a[..11].copy_from_bitslice(app);
117
118        Ok(Self { packet_type: t, sec_header_flag: head, app_process_id: a })
119    }
120
121    pub fn new_idle(t: PacketType) -> Self {
122        Self { packet_type: t, sec_header_flag: SecHeaderFlag::Idle, 
123            app_process_id: bitarr![u8, LocalBits; 1; 11]}
124    }
125
126    pub fn new_from_slice(s: &BitSlice<u8>) -> Self {
127        let packet_type = PacketType::from_bool(s[0]);
128        let sec_header_flag = SecHeaderFlag::from_bool(s[1]);
129
130        let mut app_process_id = bitarr!(u8, LocalBits; 0; 11);
131        app_process_id[..11].copy_from_bitslice(&s[2..13]);
132        
133        Self { packet_type, sec_header_flag, app_process_id }
134    }
135    
136    pub fn to_bits(&self, _aux: &'a mut BitSlice<u8>) {
137        *_aux.get_mut(0).unwrap() = self.packet_type.to_bool();
138        *_aux.get_mut(1).unwrap() = self.sec_header_flag.to_bool();
139
140        _aux[2..2 + 11].copy_from_bitslice(&self.app_process_id[..11]);
141    }
142}
143
144// WARN: 4.1.3.4.2.3
145#[derive(Debug, Clone)]
146pub enum SeqFlags {
147    Continuation,
148    First,
149    Last,
150    Unsegmented,
151}
152
153impl SeqFlags {
154    fn from_slice(b: &BitSlice<u8>) -> Self {
155        match [b[0], b[1]] {
156            [false, false] => Self::Continuation,
157            [false, true] => Self::First,
158            [true, false] => Self::Last,
159            [true, true] => Self::Unsegmented,
160        }
161    }
162
163    fn to_bool(&self) -> &'static BitSlice<u8> {
164        match self {
165            Self::Continuation => bits![static u8, LocalBits; 0, 0],
166            Self::First => bits![static u8, LocalBits;  0, 1],
167            Self::Last => bits![static u8, LocalBits;  1, 0],
168            Self::Unsegmented => bits![static u8, LocalBits;  1, 1],
169        }
170    }
171}
172
173
174#[derive(Debug, Clone)]
175pub struct SequenceControl {
176    sequence_flags: SeqFlags,
177
178    // WARN: For a Packet with the Packet Type set to ‘0’ (i.e., a telemetry Packet), this field
179    // shall contain the Packet Sequence Count. For a Packet with the Packet Type set to ‘1’ (i.e., a
180    // telecommand Packet), this field shall contain either the Packet Sequence Count or Packet Name.
181    // WARN: This will most likely be set at the end of the 'Builder' of the packet: 4.1.3.4.3.4
182    sequence_count_pkg_name: BitArray<[u8; 2], LocalBits>, // 14 bits
183}
184
185impl SequenceControl {
186    pub fn new(flag: SeqFlags, count: BitArray<[u8; 2]>) -> Result<Self, SPPError> {
187        /*
188        Commented out because https://github.com/ferrilab/bitvec/issues/159
189        Workaround in the builder
190        if count.len() != 14 {
191            return Err(SPPError::SequenceControlLenMismatch); // Sequence count/pkg name is more than 14 bits
192        }
193        */
194        Ok(Self { sequence_flags: flag, sequence_count_pkg_name: count })
195    }
196
197    fn new_from_slice(s: &BitSlice<u8>) -> Self {
198        let sequence_flags = SeqFlags::from_slice(&s[..2]);
199
200        let mut sequence_count_pkg_name = bitarr!(u8, LocalBits; 0; 14);
201        sequence_count_pkg_name[..14].copy_from_bitslice(&s[2..16]);
202        
203        Self {sequence_flags,  sequence_count_pkg_name}
204    }
205
206    fn to_bits<'a>(&self, _aux: &'a mut BitSlice<u8>) {
207        
208        let sf = self.sequence_flags.to_bool();
209        _aux[..2].copy_from_bitslice(&sf);
210
211        _aux[2..].copy_from_bitslice(&self.sequence_count_pkg_name[..14]);
212    }
213}