Skip to content

Commit

Permalink
Merge pull request 'added additional ctors which only set the APID' (…
Browse files Browse the repository at this point in the history
…#80) from addition-sp-header-ctors into main

Reviewed-on: https://egit.irs.uni-stuttgart.de/rust/spacepackets/pulls/80
  • Loading branch information
robamu committed Apr 3, 2024
2 parents ca90393 + bbd66a6 commit 4cd40f3
Show file tree
Hide file tree
Showing 12 changed files with 325 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
to `new_for_tc_checked`, `new_for_tm_checked`, `new_for_unseg_tc_checked` and
`new_for_unseg_tm_checked`.

## Added

- `SpHeader::new_from_apid` and `SpHeader::new_from_apid_checked` constructor.
- `#[inline]` attribute for a lot of small functions.

# [v0.11.0-rc.1] 2024-04-03

Major API changes for the time API. If you are using the time API, it is strongly recommended
Expand Down
12 changes: 12 additions & 0 deletions src/cfdp/lv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub(crate) fn generic_len_check_deserialization(
}

impl<'data> Lv<'data> {
#[inline]
pub fn new(data: &[u8]) -> Result<Lv, TlvLvError> {
if data.len() > u8::MAX as usize {
return Err(TlvLvError::DataTooLarge(data.len()));
Expand All @@ -73,6 +74,7 @@ impl<'data> Lv<'data> {
}

/// Creates a LV with an empty value field.
#[inline]
pub fn new_empty() -> Lv<'data> {
Lv {
data: &[],
Expand All @@ -82,44 +84,52 @@ impl<'data> Lv<'data> {

/// Helper function to build a string LV. This is especially useful for the file or directory
/// path LVs
#[inline]
pub fn new_from_str(str_slice: &str) -> Result<Lv, TlvLvError> {
Self::new(str_slice.as_bytes())
}

/// Helper function to build a string LV. This is especially useful for the file or directory
/// path LVs
#[cfg(feature = "std")]
#[inline]
pub fn new_from_string(string: &'data String) -> Result<Lv<'data>, TlvLvError> {
Self::new(string.as_bytes())
}

/// Returns the length of the value part, not including the length byte.
#[inline]
pub fn len_value(&self) -> usize {
self.data.len()
}

/// Returns the full raw length, including the length byte.
#[inline]
pub fn len_full(&self) -> usize {
self.len_value() + 1
}

/// Checks whether the value field is empty.
#[inline]
pub fn is_empty(&self) -> bool {
self.data.len() == 0
}

#[inline]
pub fn value(&self) -> &[u8] {
self.data
}

/// If the LV was generated from a raw bytestream using [Self::from_bytes], the raw start
/// of the LV can be retrieved with this method.
#[inline]
pub fn raw_data(&self) -> Option<&[u8]> {
self.raw_data
}

/// Convenience function to extract the value as a [str]. This is useful if the LV is
/// known to contain a [str], for example being a file name.
#[inline]
pub fn value_as_str(&self) -> Option<Result<&'data str, Utf8Error>> {
if self.is_empty() {
return None;
Expand All @@ -135,6 +145,7 @@ impl<'data> Lv<'data> {
}

/// Reads a LV from a raw buffer.
#[inline]
pub fn from_bytes(buf: &'data [u8]) -> Result<Lv<'data>, ByteConversionError> {
generic_len_check_deserialization(buf, MIN_LV_LEN)?;
Self::from_be_bytes_no_len_check(buf)
Expand All @@ -151,6 +162,7 @@ impl<'data> Lv<'data> {
MIN_LV_LEN + self.data.len()
}

#[inline]
pub(crate) fn from_be_bytes_no_len_check(
buf: &'data [u8],
) -> Result<Lv<'data>, ByteConversionError> {
Expand Down
31 changes: 31 additions & 0 deletions src/cfdp/pdu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ impl Error for PduError {
}

impl From<ByteConversionError> for PduError {
#[inline]
fn from(value: ByteConversionError) -> Self {
Self::ByteConversion(value)
}
}

impl From<TlvLvError> for PduError {
#[inline]
fn from(e: TlvLvError) -> Self {
Self::TlvLvError(e)
}
Expand All @@ -179,33 +181,42 @@ pub trait WritablePduPacket {
pub trait CfdpPdu {
fn pdu_header(&self) -> &PduHeader;

#[inline]
fn source_id(&self) -> UnsignedByteField {
self.pdu_header().common_pdu_conf().source_entity_id
}

#[inline]
fn dest_id(&self) -> UnsignedByteField {
self.pdu_header().common_pdu_conf().dest_entity_id
}

#[inline]
fn transaction_seq_num(&self) -> UnsignedByteField {
self.pdu_header().common_pdu_conf().transaction_seq_num
}

#[inline]
fn transmission_mode(&self) -> TransmissionMode {
self.pdu_header().common_pdu_conf().trans_mode
}

#[inline]
fn direction(&self) -> Direction {
self.pdu_header().common_pdu_conf().direction
}

#[inline]
fn crc_flag(&self) -> CrcFlag {
self.pdu_header().common_pdu_conf().crc_flag
}

#[inline]
fn file_flag(&self) -> LargeFileFlag {
self.pdu_header().common_pdu_conf().file_flag
}

#[inline]
fn pdu_type(&self) -> PduType {
self.pdu_header().pdu_type()
}
Expand Down Expand Up @@ -234,6 +245,7 @@ pub struct CommonPduConfig {

// TODO: Builder pattern might be applicable here..
impl CommonPduConfig {
#[inline]
pub fn new(
source_id: impl Into<UnsignedByteField>,
dest_id: impl Into<UnsignedByteField>,
Expand Down Expand Up @@ -265,6 +277,7 @@ impl CommonPduConfig {
})
}

#[inline]
pub fn new_with_byte_fields(
source_id: impl Into<UnsignedByteField>,
dest_id: impl Into<UnsignedByteField>,
Expand All @@ -281,10 +294,12 @@ impl CommonPduConfig {
)
}

#[inline]
pub fn source_id(&self) -> UnsignedByteField {
self.source_entity_id
}

#[inline]
fn source_dest_id_check(
source_id: impl Into<UnsignedByteField>,
dest_id: impl Into<UnsignedByteField>,
Expand All @@ -307,6 +322,7 @@ impl CommonPduConfig {
Ok((source_id, dest_id))
}

#[inline]
pub fn set_source_and_dest_id(
&mut self,
source_id: impl Into<UnsignedByteField>,
Expand All @@ -318,6 +334,7 @@ impl CommonPduConfig {
Ok(())
}

#[inline]
pub fn dest_id(&self) -> UnsignedByteField {
self.dest_entity_id
}
Expand All @@ -326,6 +343,7 @@ impl CommonPduConfig {
impl Default for CommonPduConfig {
/// The defaults for the source ID, destination ID and the transaction sequence number is the
/// [UnsignedByteFieldU8] with an intitial value of 0
#[inline]
fn default() -> Self {
// The new function can not fail for these input parameters.
Self::new(
Expand All @@ -342,6 +360,7 @@ impl Default for CommonPduConfig {
}

impl PartialEq for CommonPduConfig {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.source_entity_id.value() == other.source_entity_id.value()
&& self.dest_entity_id.value() == other.dest_entity_id.value()
Expand Down Expand Up @@ -370,6 +389,7 @@ pub struct PduHeader {
}

impl PduHeader {
#[inline]
pub fn new_for_file_data(
pdu_conf: CommonPduConfig,
pdu_datafield_len: u16,
Expand All @@ -385,6 +405,7 @@ impl PduHeader {
)
}

#[inline]
pub fn new_for_file_data_default(pdu_conf: CommonPduConfig, pdu_datafield_len: u16) -> Self {
Self::new_generic(
PduType::FileData,
Expand All @@ -394,6 +415,7 @@ impl PduHeader {
SegmentationControl::NoRecordBoundaryPreservation,
)
}
#[inline]
pub fn new_no_file_data(pdu_conf: CommonPduConfig, pdu_datafield_len: u16) -> Self {
Self::new_generic(
PduType::FileDirective,
Expand All @@ -404,6 +426,7 @@ impl PduHeader {
)
}

#[inline]
pub fn new_generic(
pdu_type: PduType,
pdu_conf: CommonPduConfig,
Expand All @@ -421,19 +444,22 @@ impl PduHeader {
}

/// Returns only the length of the PDU header when written to a raw buffer.
#[inline]
pub fn header_len(&self) -> usize {
FIXED_HEADER_LEN
+ self.pdu_conf.source_entity_id.size()
+ self.pdu_conf.transaction_seq_num.size()
+ self.pdu_conf.dest_entity_id.size()
}

#[inline]
pub fn pdu_datafield_len(&self) -> usize {
self.pdu_datafield_len.into()
}

/// Returns the full length of the PDU when written to a raw buffer, which is the header length
/// plus the PDU datafield length.
#[inline]
pub fn pdu_len(&self) -> usize {
self.header_len() + self.pdu_datafield_len as usize
}
Expand Down Expand Up @@ -606,17 +632,22 @@ impl PduHeader {
current_idx,
))
}

#[inline]
pub fn pdu_type(&self) -> PduType {
self.pdu_type
}

#[inline]
pub fn common_pdu_conf(&self) -> &CommonPduConfig {
&self.pdu_conf
}

pub fn seg_metadata_flag(&self) -> SegmentMetadataFlag {
self.seg_metadata_flag
}

#[inline]
pub fn seg_ctrl(&self) -> SegmentationControl {
self.seg_ctrl
}
Expand Down
7 changes: 7 additions & 0 deletions src/ecss/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,13 @@ pub(crate) fn verify_crc16_ccitt_false_from_raw(raw_data: &[u8]) -> bool {
macro_rules! ccsds_impl {
() => {
delegate!(to self.sp_header {
#[inline]
fn ccsds_version(&self) -> u8;
#[inline]
fn packet_id(&self) -> crate::PacketId;
#[inline]
fn psc(&self) -> crate::PacketSequenceCtrl;
#[inline]
fn data_len(&self) -> u16;
});
}
Expand All @@ -273,8 +277,11 @@ macro_rules! ccsds_impl {
macro_rules! sp_header_impls {
() => {
delegate!(to self.sp_header {
#[inline]
pub fn set_apid(&mut self, apid: u16) -> bool;
#[inline]
pub fn set_seq_count(&mut self, seq_count: u16) -> bool;
#[inline]
pub fn set_seq_flags(&mut self, seq_flag: SequenceFlags);
});
}
Expand Down
1 change: 1 addition & 0 deletions src/ecss/scheduling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub enum SchedStatus {
}

impl From<bool> for SchedStatus {
#[inline]
fn from(value: bool) -> Self {
if value {
SchedStatus::Enabled
Expand Down
Loading

0 comments on commit 4cd40f3

Please sign in to comment.