better name for function
Some checks failed
Rust/spacepackets/pipeline/head There was a failure building this commit
Some checks failed
Rust/spacepackets/pipeline/head There was a failure building this commit
This commit is contained in:
parent
94cfe59235
commit
ef4244c8cb
@ -12,7 +12,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
|
|
||||||
- Added new `util` module which contains the following (new) helper modules:
|
- Added new `util` module which contains the following (new) helper modules:
|
||||||
- `UnsignedEnum` trait as an abstraction for unsigned byte fields with variable lengths. It is
|
- `UnsignedEnum` trait as an abstraction for unsigned byte fields with variable lengths. It is
|
||||||
not tied to the ECSS PFC value like the `EcssEnumeration` trait.
|
not tied to the ECSS PFC value like the `EcssEnumeration` trait. The method to retrieve
|
||||||
|
the size of the unsigned enumeration in bytes is now called `size`.
|
||||||
- `GenericUnsignedByteField<TYPE>` and helper typedefs `UnsignedU8`, `UnsignedU16`, `UnsignedU32`
|
- `GenericUnsignedByteField<TYPE>` and helper typedefs `UnsignedU8`, `UnsignedU16`, `UnsignedU32`
|
||||||
and `UnsignedU64` as helper types implementing `UnsignedEnum`
|
and `UnsignedU64` as helper types implementing `UnsignedEnum`
|
||||||
- `UnsignedByteField` as a type-erased helper.
|
- `UnsignedByteField` as a type-erased helper.
|
||||||
|
@ -171,26 +171,26 @@ impl CommonPduConfig {
|
|||||||
let source_id = source_id.into();
|
let source_id = source_id.into();
|
||||||
let dest_id = dest_id.into();
|
let dest_id = dest_id.into();
|
||||||
let transaction_seq_num = transaction_seq_num.into();
|
let transaction_seq_num = transaction_seq_num.into();
|
||||||
if source_id.len() != dest_id.len() {
|
if source_id.size() != dest_id.size() {
|
||||||
return Err(PduError::SourceDestIdLenMissmatch((
|
return Err(PduError::SourceDestIdLenMissmatch((
|
||||||
source_id.len(),
|
source_id.size(),
|
||||||
dest_id.len(),
|
dest_id.size(),
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
if source_id.len() != 1
|
if source_id.size() != 1
|
||||||
&& source_id.len() != 2
|
&& source_id.size() != 2
|
||||||
&& source_id.len() != 4
|
&& source_id.size() != 4
|
||||||
&& source_id.len() != 8
|
&& source_id.size() != 8
|
||||||
{
|
{
|
||||||
return Err(PduError::InvalidEntityLen(source_id.len() as u8));
|
return Err(PduError::InvalidEntityLen(source_id.size() as u8));
|
||||||
}
|
}
|
||||||
if transaction_seq_num.len() != 1
|
if transaction_seq_num.size() != 1
|
||||||
&& transaction_seq_num.len() != 2
|
&& transaction_seq_num.size() != 2
|
||||||
&& transaction_seq_num.len() != 4
|
&& transaction_seq_num.size() != 4
|
||||||
&& transaction_seq_num.len() != 8
|
&& transaction_seq_num.size() != 8
|
||||||
{
|
{
|
||||||
return Err(PduError::InvalidTransactionSeqNumLen(
|
return Err(PduError::InvalidTransactionSeqNumLen(
|
||||||
transaction_seq_num.len() as u8,
|
transaction_seq_num.size() as u8,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
@ -298,9 +298,9 @@ impl PduHeader {
|
|||||||
/// Returns only the length of the PDU header when written to a raw buffer.
|
/// Returns only the length of the PDU header when written to a raw buffer.
|
||||||
pub fn header_len(&self) -> usize {
|
pub fn header_len(&self) -> usize {
|
||||||
FIXED_HEADER_LEN
|
FIXED_HEADER_LEN
|
||||||
+ self.pdu_conf.source_entity_id.len()
|
+ self.pdu_conf.source_entity_id.size()
|
||||||
+ self.pdu_conf.transaction_seq_num.len()
|
+ self.pdu_conf.transaction_seq_num.size()
|
||||||
+ self.pdu_conf.dest_entity_id.len()
|
+ self.pdu_conf.dest_entity_id.size()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the full length of the PDU when written to a raw buffer, which is the header length
|
/// Returns the full length of the PDU when written to a raw buffer, which is the header length
|
||||||
@ -312,16 +312,16 @@ impl PduHeader {
|
|||||||
pub fn write_to_bytes(&self, buf: &mut [u8]) -> Result<usize, PduError> {
|
pub fn write_to_bytes(&self, buf: &mut [u8]) -> Result<usize, PduError> {
|
||||||
// Internal note: There is currently no way to pass a PDU configuration like this, but
|
// Internal note: There is currently no way to pass a PDU configuration like this, but
|
||||||
// this check is still kept for defensive programming.
|
// this check is still kept for defensive programming.
|
||||||
if self.pdu_conf.source_entity_id.len() != self.pdu_conf.dest_entity_id.len() {
|
if self.pdu_conf.source_entity_id.size() != self.pdu_conf.dest_entity_id.size() {
|
||||||
return Err(PduError::SourceDestIdLenMissmatch((
|
return Err(PduError::SourceDestIdLenMissmatch((
|
||||||
self.pdu_conf.source_entity_id.len(),
|
self.pdu_conf.source_entity_id.size(),
|
||||||
self.pdu_conf.dest_entity_id.len(),
|
self.pdu_conf.dest_entity_id.size(),
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
if buf.len()
|
if buf.len()
|
||||||
< FIXED_HEADER_LEN
|
< FIXED_HEADER_LEN
|
||||||
+ self.pdu_conf.source_entity_id.len()
|
+ self.pdu_conf.source_entity_id.size()
|
||||||
+ self.pdu_conf.transaction_seq_num.len()
|
+ self.pdu_conf.transaction_seq_num.size()
|
||||||
{
|
{
|
||||||
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
|
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
|
||||||
found: buf.len(),
|
found: buf.len(),
|
||||||
@ -340,22 +340,22 @@ impl PduHeader {
|
|||||||
buf[current_idx..current_idx + 2].copy_from_slice(&self.pdu_datafield_len.to_be_bytes());
|
buf[current_idx..current_idx + 2].copy_from_slice(&self.pdu_datafield_len.to_be_bytes());
|
||||||
current_idx += 2;
|
current_idx += 2;
|
||||||
buf[current_idx] = ((self.seg_ctrl as u8) << 7)
|
buf[current_idx] = ((self.seg_ctrl as u8) << 7)
|
||||||
| (((self.pdu_conf.source_entity_id.len() - 1) as u8) << 4)
|
| (((self.pdu_conf.source_entity_id.size() - 1) as u8) << 4)
|
||||||
| ((self.seg_metadata_flag as u8) << 3)
|
| ((self.seg_metadata_flag as u8) << 3)
|
||||||
| ((self.pdu_conf.transaction_seq_num.len() - 1) as u8);
|
| ((self.pdu_conf.transaction_seq_num.size() - 1) as u8);
|
||||||
current_idx += 1;
|
current_idx += 1;
|
||||||
self.pdu_conf.source_entity_id.write_to_be_bytes(
|
self.pdu_conf.source_entity_id.write_to_be_bytes(
|
||||||
&mut buf[current_idx..current_idx + self.pdu_conf.source_entity_id.len()],
|
&mut buf[current_idx..current_idx + self.pdu_conf.source_entity_id.size()],
|
||||||
)?;
|
)?;
|
||||||
current_idx += self.pdu_conf.source_entity_id.len();
|
current_idx += self.pdu_conf.source_entity_id.size();
|
||||||
self.pdu_conf.transaction_seq_num.write_to_be_bytes(
|
self.pdu_conf.transaction_seq_num.write_to_be_bytes(
|
||||||
&mut buf[current_idx..current_idx + self.pdu_conf.transaction_seq_num.len()],
|
&mut buf[current_idx..current_idx + self.pdu_conf.transaction_seq_num.size()],
|
||||||
)?;
|
)?;
|
||||||
current_idx += self.pdu_conf.transaction_seq_num.len();
|
current_idx += self.pdu_conf.transaction_seq_num.size();
|
||||||
self.pdu_conf.dest_entity_id.write_to_be_bytes(
|
self.pdu_conf.dest_entity_id.write_to_be_bytes(
|
||||||
&mut buf[current_idx..current_idx + self.pdu_conf.dest_entity_id.len()],
|
&mut buf[current_idx..current_idx + self.pdu_conf.dest_entity_id.size()],
|
||||||
)?;
|
)?;
|
||||||
current_idx += self.pdu_conf.dest_entity_id.len();
|
current_idx += self.pdu_conf.dest_entity_id.size();
|
||||||
Ok(current_idx)
|
Ok(current_idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,17 +175,17 @@ impl EntityIdTlv {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn len_value(&self) -> usize {
|
pub fn len_value(&self) -> usize {
|
||||||
self.entity_id.len()
|
self.entity_id.size()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn len_full(&self) -> usize {
|
pub fn len_full(&self) -> usize {
|
||||||
2 + self.entity_id.len()
|
2 + self.entity_id.size()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
|
pub fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
|
||||||
Self::len_check(buf)?;
|
Self::len_check(buf)?;
|
||||||
buf[0] = TlvType::EntityId as u8;
|
buf[0] = TlvType::EntityId as u8;
|
||||||
buf[1] = self.entity_id.len() as u8;
|
buf[1] = self.entity_id.size() as u8;
|
||||||
self.entity_id.write_to_be_bytes(&mut buf[2..])
|
self.entity_id.write_to_be_bytes(&mut buf[2..])
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,8 +205,8 @@ impl EntityIdTlv {
|
|||||||
pub fn to_tlv(self, buf: &mut [u8]) -> Result<Tlv, ByteConversionError> {
|
pub fn to_tlv(self, buf: &mut [u8]) -> Result<Tlv, ByteConversionError> {
|
||||||
Self::len_check(buf)?;
|
Self::len_check(buf)?;
|
||||||
self.entity_id
|
self.entity_id
|
||||||
.write_to_be_bytes(&mut buf[2..2 + self.entity_id.len()])?;
|
.write_to_be_bytes(&mut buf[2..2 + self.entity_id.size()])?;
|
||||||
Tlv::new(TlvType::EntityId, &buf[2..2 + self.entity_id.len()]).map_err(|e| match e {
|
Tlv::new(TlvType::EntityId, &buf[2..2 + self.entity_id.size()]).map_err(|e| match e {
|
||||||
TlvLvError::ByteConversionError(e) => e,
|
TlvLvError::ByteConversionError(e) => e,
|
||||||
// All other errors are impossible.
|
// All other errors are impossible.
|
||||||
_ => panic!("unexpected TLV error"),
|
_ => panic!("unexpected TLV error"),
|
||||||
|
@ -324,7 +324,7 @@ impl<TYPE> GenericEcssEnumWrapper<TYPE> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<TYPE: ToBeBytes> UnsignedEnum for GenericEcssEnumWrapper<TYPE> {
|
impl<TYPE: ToBeBytes> UnsignedEnum for GenericEcssEnumWrapper<TYPE> {
|
||||||
fn len(&self) -> usize {
|
fn size(&self) -> usize {
|
||||||
(self.pfc() / 8) as usize
|
(self.pfc() / 8) as usize
|
||||||
}
|
}
|
||||||
|
|
||||||
|
20
src/util.rs
20
src/util.rs
@ -68,9 +68,9 @@ impl ToBeBytes for u64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::len_without_is_empty)]
|
|
||||||
pub trait UnsignedEnum {
|
pub trait UnsignedEnum {
|
||||||
fn len(&self) -> usize;
|
/// Size of the unsigned enumeration in bytes.
|
||||||
|
fn size(&self) -> usize;
|
||||||
/// Write the unsigned enumeration to a raw buffer. Returns the written size on success.
|
/// Write the unsigned enumeration to a raw buffer. Returns the written size on success.
|
||||||
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError>;
|
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError>;
|
||||||
}
|
}
|
||||||
@ -160,18 +160,18 @@ impl UnsignedByteField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UnsignedEnum for UnsignedByteField {
|
impl UnsignedEnum for UnsignedByteField {
|
||||||
fn len(&self) -> usize {
|
fn size(&self) -> usize {
|
||||||
self.width
|
self.width
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
|
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
|
||||||
if buf.len() < self.len() {
|
if buf.len() < self.size() {
|
||||||
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
|
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
|
||||||
expected: self.len(),
|
expected: self.size(),
|
||||||
found: buf.len(),
|
found: buf.len(),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
match self.len() {
|
match self.size() {
|
||||||
0 => Ok(0),
|
0 => Ok(0),
|
||||||
1 => {
|
1 => {
|
||||||
let u8 = UnsignedByteFieldU8::try_from(*self).unwrap();
|
let u8 = UnsignedByteFieldU8::try_from(*self).unwrap();
|
||||||
@ -210,18 +210,18 @@ impl<TYPE> GenericUnsignedByteField<TYPE> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<TYPE: ToBeBytes> UnsignedEnum for GenericUnsignedByteField<TYPE> {
|
impl<TYPE: ToBeBytes> UnsignedEnum for GenericUnsignedByteField<TYPE> {
|
||||||
fn len(&self) -> usize {
|
fn size(&self) -> usize {
|
||||||
self.value.written_len()
|
self.value.written_len()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
|
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
|
||||||
if buf.len() < self.len() {
|
if buf.len() < self.size() {
|
||||||
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
|
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
|
||||||
found: buf.len(),
|
found: buf.len(),
|
||||||
expected: self.len(),
|
expected: self.size(),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
buf[0..self.len()].copy_from_slice(self.value.to_be_bytes().as_ref());
|
buf[0..self.size()].copy_from_slice(self.value.to_be_bytes().as_ref());
|
||||||
Ok(self.value.written_len())
|
Ok(self.value.written_len())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user