Skip to content

Commit

Permalink
#27 use getters to get message payload and header
Browse files Browse the repository at this point in the history
  • Loading branch information
HusseinAbdelhamid committed Sep 25, 2024
1 parent ee96a27 commit 84e101c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,15 @@ impl<const L: usize> MessageType<L> for CanFd<L> {
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct TxMessage<T: MessageType<L>, const L: usize> {
/// first 2 bytes of Transmit Message Object
pub header: TxHeader,
pub(crate) header: TxHeader,
/// Payload bytes of Message Object
pub buff: Bytes,
pub(crate) buff: Bytes,
/// CAN message type (CAN 2.0 or CAN FD)
pub message_type: T,
pub(crate) message_type: T,
}

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 84e101c

Please sign in to comment.