Skip to content

Commit

Permalink
Merge pull request #28 from atlas-aero/27-change-txmessage-data-membe…
Browse files Browse the repository at this point in the history
…rs-to-public

#27 make TxMessage data members public
  • Loading branch information
marius-meissner authored Sep 26, 2024
2 parents 4910813 + bb4050f commit 8885078
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub struct TxMessage<T: MessageType<L>, const L: usize> {
}

impl<T: MessageType<L>, const L: usize> TxMessage<T, L> {
/// Create new CAN message
pub fn new(message_type: T, data: Bytes, identifier: Id) -> Result<Self, MessageError> {
let mut header = TxHeader::new();

Expand Down Expand Up @@ -202,6 +203,16 @@ impl<T: MessageType<L>, const L: usize> TxMessage<T, L> {
message_type,
})
}

/// Returns payload as a `&[u8]`
pub fn get_payload(&self) -> &[u8] {
self.buff.as_ref()
}

/// Returns Header register of Transmit Message Object
pub fn get_header(&self) -> &TxHeader {
&self.header
}
}

/// Receive message object header
Expand Down
12 changes: 12 additions & 0 deletions src/tests/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,15 @@ fn test_payload_greater_than_generic_type_args() {
assert_eq!(message_2_0.unwrap_err(), MessageError::InvalidLength(5));
assert_eq!(message_fd.unwrap_err(), MessageError::InvalidLength(23));
}

#[test]
fn test_get_payload() {
let payload_bytes = Bytes::copy_from_slice(&[1u8; 8]);
let standard_id = StandardId::new(STANDARD_ID).unwrap();

let msg_type = Can20::<8> {};

let message = TxMessage::new(msg_type, payload_bytes, Id::Standard(standard_id)).unwrap();

assert_eq!(message.get_payload(), &[1u8; 8]);
}

0 comments on commit 8885078

Please sign in to comment.