Fully Working CCSDS parsing
+ updated ccsds config to make it work, as I expect it to at least
This commit is contained in:
3 files changed
+157
-69
No files matched your search
+37
-12
@@ -1,4 +1,4 @@
|
||||
use num_bigint::BigUint;
|
||||
use crate::formatter::format::LastByte;
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -31,10 +31,23 @@ pub const fn bit_mask(mask: u8) -> u8 {
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct ByteStream {
|
||||
data: Vec<u8>,
|
||||
reverse_bits: bool,
|
||||
}
|
||||
|
||||
impl ByteStream {
|
||||
pub fn get_bytes(&self, bit_ndx: usize, bit_count: usize) -> Result<Vec<u8>, ByteStreamError> {
|
||||
pub fn set_reverse_bits(&mut self, reverse_bits: bool) {
|
||||
self.reverse_bits = reverse_bits;
|
||||
}
|
||||
|
||||
pub fn get_reverse_bits(&self) -> bool {
|
||||
self.reverse_bits
|
||||
}
|
||||
|
||||
pub fn get_bytes<T: LastByte>(
|
||||
&self,
|
||||
bit_ndx: usize,
|
||||
bit_count: usize,
|
||||
) -> Result<Vec<u8>, ByteStreamError> {
|
||||
let byte_ndx = bit_ndx / 8;
|
||||
let bits_before = (bit_ndx % 8) as u8;
|
||||
let bits_in_last_byte = ((bit_ndx + bit_count - bits_before as usize) % 8) as u8;
|
||||
@@ -51,11 +64,17 @@ impl ByteStream {
|
||||
return Err(ByteStreamError::OutOfRange);
|
||||
}
|
||||
|
||||
let byte_stream = self.data[byte_ndx..byte_ndx + bytes_needed].to_vec();
|
||||
let mut byte_stream = self.data[byte_ndx..byte_ndx + bytes_needed].to_vec();
|
||||
|
||||
let number = BigUint::from_bytes_le(&byte_stream) >> bits_before;
|
||||
if self.reverse_bits {
|
||||
for byte in &mut byte_stream {
|
||||
*byte = byte.reverse_bits();
|
||||
}
|
||||
}
|
||||
|
||||
let mut byte_stream = number.to_bytes_le();
|
||||
let number = T::big_uint(&byte_stream) >> bits_before;
|
||||
|
||||
let mut byte_stream = T::big_u_int_to_bytes(number);
|
||||
|
||||
if bytes_needed > byte_count && bytes_needed == byte_stream.len() {
|
||||
byte_stream.pop();
|
||||
@@ -81,20 +100,26 @@ impl From<&[u8]> for ByteStream {
|
||||
|
||||
impl From<Vec<u8>> for ByteStream {
|
||||
fn from(vec: Vec<u8>) -> Self {
|
||||
ByteStream { data: vec }
|
||||
ByteStream {
|
||||
data: vec,
|
||||
reverse_bits: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ByteStream;
|
||||
use byteorder::BigEndian;
|
||||
|
||||
#[test]
|
||||
fn test_get_bytes_no_shift() {
|
||||
let bytes: Vec<u8> = vec![0xff, 0x00, 0x55];
|
||||
let bit_stream = ByteStream::from(bytes.clone());
|
||||
|
||||
let new_bytes = bit_stream.get_bytes(0, bytes.len() * 8).unwrap();
|
||||
let new_bytes = bit_stream
|
||||
.get_bytes::<BigEndian>(0, bytes.len() * 8)
|
||||
.unwrap();
|
||||
assert_eq!(bytes, new_bytes);
|
||||
}
|
||||
|
||||
@@ -103,7 +128,7 @@ mod tests {
|
||||
let bytes: Vec<u8> = vec![0x5f, 0x00, 0x55];
|
||||
let bit_stream = ByteStream::from(bytes.clone());
|
||||
|
||||
let new_bytes = bit_stream.get_bytes(4, 4).unwrap();
|
||||
let new_bytes = bit_stream.get_bytes::<BigEndian>(4, 4).unwrap();
|
||||
assert_eq!(vec![0x05], new_bytes);
|
||||
}
|
||||
|
||||
@@ -112,7 +137,7 @@ mod tests {
|
||||
let bytes: Vec<u8> = vec![0xff, 0x55];
|
||||
let bit_stream = ByteStream::from(bytes.clone());
|
||||
|
||||
let new_bytes = bit_stream.get_bytes(4, 8).unwrap();
|
||||
let new_bytes = bit_stream.get_bytes::<BigEndian>(4, 8).unwrap();
|
||||
assert_eq!(vec![0x5f], new_bytes);
|
||||
}
|
||||
|
||||
@@ -121,7 +146,7 @@ mod tests {
|
||||
let bytes: Vec<u8> = vec![0xff, 0x55];
|
||||
let bit_stream = ByteStream::from(bytes.clone());
|
||||
|
||||
let new_bytes = bit_stream.get_bytes(7, 2).unwrap();
|
||||
let new_bytes = bit_stream.get_bytes::<BigEndian>(7, 2).unwrap();
|
||||
assert_eq!(vec![0x03], new_bytes);
|
||||
}
|
||||
|
||||
@@ -130,7 +155,7 @@ mod tests {
|
||||
let bytes: Vec<u8> = vec![0xff, 0x55];
|
||||
let bit_stream = ByteStream::from(bytes.clone());
|
||||
|
||||
let new_bytes = bit_stream.get_bytes(8, 8).unwrap();
|
||||
let new_bytes = bit_stream.get_bytes::<BigEndian>(8, 8).unwrap();
|
||||
assert_eq!(vec![0x55], new_bytes);
|
||||
}
|
||||
|
||||
@@ -139,7 +164,7 @@ mod tests {
|
||||
let bytes: Vec<u8> = vec![0x55];
|
||||
let bit_stream = ByteStream::from(bytes.clone());
|
||||
|
||||
let new_bytes = bit_stream.get_bytes(0, 3).unwrap();
|
||||
let new_bytes = bit_stream.get_bytes::<BigEndian>(0, 3).unwrap();
|
||||
assert_eq!(vec![0x05], new_bytes);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user