Skip to content

Commit

Permalink
Merge pull request 'prepare next patch version' (#90) from small-impr…
Browse files Browse the repository at this point in the history
…ovements-and-fixes into main

Reviewed-on: https://egit.irs.uni-stuttgart.de/rust/spacepackets/pulls/90
  • Loading branch information
robamu committed Apr 22, 2024
2 parents 619b22e + 8b1ccb0 commit 95158a8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

# [unreleased]

# [v0.11.1] 2024-04-20

## Fixed

- The default data length for for `SpHeader` constructors where the data field length is not
specified is now 0.
- The `SpHeader::new_from_fields` is public now.

## Added

- `SpHeader::to_vec` method.

# [v0.11.0] 2024-04-16

## Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spacepackets"
version = "0.11.0"
version = "0.11.1"
edition = "2021"
rust-version = "1.65"
authors = ["Robin Mueller <[email protected]>"]
Expand Down
37 changes: 29 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,8 @@ pub struct SpHeader {
pub type SpacePacketHeader = SpHeader;

impl Default for SpHeader {
/// The default function sets the sequence flag field to [SequenceFlags::Unsegmented]. The data
/// length field is set to 1, which denotes an empty space packets.
/// The default function sets the sequence flag field to [SequenceFlags::Unsegmented] and the
/// data length to 0.
#[inline]
fn default() -> Self {
SpHeader {
Expand All @@ -516,7 +516,7 @@ impl Default for SpHeader {
seq_flags: SequenceFlags::Unsegmented,
seq_count: 0,
},
data_len: 1,
data_len: 0,
}
}
}
Expand All @@ -532,8 +532,8 @@ impl SpHeader {
}
}

/// This constructor sets the sequence flag field to [SequenceFlags::Unsegmented]. The data
/// length field is set to 1, which denotes an empty space packets.
/// This constructor sets the sequence flag field to [SequenceFlags::Unsegmented] and the data
/// length to 0.
///
/// This constructor will panic if the APID exceeds [MAX_APID].
#[inline]
Expand All @@ -545,7 +545,7 @@ impl SpHeader {
seq_flags: SequenceFlags::Unsegmented,
seq_count: 0,
},
data_len: 1,
data_len: 0,
}
}

Expand All @@ -559,7 +559,7 @@ impl SpHeader {
seq_flags: SequenceFlags::Unsegmented,
seq_count: 0,
},
data_len: 1,
data_len: 0,
})
}

Expand All @@ -568,7 +568,7 @@ impl SpHeader {
///
/// The checked constructor variants can be used to avoid panics.
#[inline]
const fn new_from_fields(
pub const fn new_from_fields(
ptype: PacketType,
sec_header: bool,
apid: u16,
Expand Down Expand Up @@ -755,6 +755,15 @@ impl SpHeader {
.ok_or(ByteConversionError::ZeroCopyToError)?;
Ok(&mut buf[CCSDS_HEADER_LEN..])
}

/// Create a vector containing the CCSDS header.
#[cfg(feature = "alloc")]
pub fn to_vec(&self) -> alloc::vec::Vec<u8> {
let mut vec = alloc::vec![0; CCSDS_HEADER_LEN];
// This can not fail.
self.write_to_be_bytes(&mut vec[..]).unwrap();
vec
}
}

impl CcsdsPacket for SpHeader {
Expand Down Expand Up @@ -1260,12 +1269,14 @@ pub(crate) mod tests {
fn sp_header_from_apid() {
let sp_header = SpHeader::new_from_apid(0x03);
assert_eq!(sp_header.apid(), 0x03);
assert_eq!(sp_header.data_len(), 0);
}

#[test]
fn sp_header_from_apid_checked() {
let sp_header = SpHeader::new_from_apid_checked(0x03).unwrap();
assert_eq!(sp_header.apid(), 0x03);
assert_eq!(sp_header.data_len(), 0);
}

#[cfg(feature = "defmt")]
Expand All @@ -1279,4 +1290,14 @@ pub(crate) mod tests {
expected: 2,
});
}

#[test]
fn test_sp_header_as_vec() {
let sp_header = SpHeader::new_for_unseg_tc(0x42, 25, 1);
let sp_header_as_vec = sp_header.to_vec();
let sp_header_read_back = SpHeader::from_be_bytes(&sp_header_as_vec)
.expect("Error reading back SP header")
.0;
assert_eq!(sp_header, sp_header_read_back);
}
}

0 comments on commit 95158a8

Please sign in to comment.