Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix more ser/deser and add tests #206

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl<M: Message> MavFrame<M> {
match self.protocol_version {
MavlinkVersion::V2 => {
let bytes: [u8; 4] = self.msg.message_id().to_le_bytes();
buf.put_slice(&bytes);
buf.put_slice(&bytes[..3]);
}
MavlinkVersion::V1 => {
buf.put_u8(self.msg.message_id() as u8); //TODO check
Expand Down
29 changes: 22 additions & 7 deletions tests/mav_frame_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,38 @@ pub mod test_shared;
mod mav_frame_tests {
// NOTE: No header
pub const HEARTBEAT_V2: &[u8] = &[
0xef, // seq 239
0x01, // sys ID
0x01, // comp ID
0x00, 0x00, 0x00, // msg ID
0x05, 0x00, 0x00, 0x00, 0x02, 0x03, 0x59, 0x03, 0x03, // payload
16, 240, // checksum
crate::test_shared::COMMON_MSG_HEADER.sequence,
crate::test_shared::COMMON_MSG_HEADER.system_id,
crate::test_shared::COMMON_MSG_HEADER.component_id,
0x00, // msg ID
0x00,
0x00,
0x05, // payload
0x00,
0x00,
0x00,
0x02,
0x03,
0x59,
0x03,
0x03,
0x10, // checksum
0xf0,
];

#[test]
pub fn test_deser() {
pub fn test_deser_ser() {
use mavlink::{common::MavMessage, MavFrame, MavlinkVersion};
let frame = MavFrame::<MavMessage>::deser(MavlinkVersion::V2, HEARTBEAT_V2)
.expect("failed to parse message");

assert_eq!(frame.header, crate::test_shared::COMMON_MSG_HEADER);
let heartbeat_msg = crate::test_shared::get_heartbeat_msg();

let mut buffer = [0u8; HEARTBEAT_V2.len()];
frame.ser(&mut buffer);
assert_eq!(buffer[..buffer.len() - 2], HEARTBEAT_V2[..buffer.len() - 2]);

let msg = match frame.msg {
MavMessage::HEARTBEAT(msg) => msg,
_ => panic!("Decoded wrong message type"),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_shared/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pub const COMMON_MSG_HEADER: mavlink::MavHeader = mavlink::MavHeader {
sequence: 239,
system_id: 1,
component_id: 1,
component_id: 2,
};

#[cfg(feature = "common")]
Expand Down
10 changes: 5 additions & 5 deletions tests/v1_encode_decode_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ mod test_v1_encode_decode {
pub const HEARTBEAT_V1: &[u8] = &[
mavlink::MAV_STX,
0x09,
0xef,
0x01,
0x01,
crate::test_shared::COMMON_MSG_HEADER.sequence,
crate::test_shared::COMMON_MSG_HEADER.system_id,
crate::test_shared::COMMON_MSG_HEADER.component_id,
0x00,
0x05,
0x00,
Expand All @@ -19,8 +19,8 @@ mod test_v1_encode_decode {
0x59,
0x03,
0x03,
0xf1,
0xd7,
0x1f,
0x50,
];

#[test]
Expand Down
10 changes: 5 additions & 5 deletions tests/v2_encode_decode_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ mod test_v2_encode_decode {
0x09, //payload len
0, //incompat flags
0, //compat flags
0xef, //seq 239
0x01, //sys ID
0x01, //comp ID
crate::test_shared::COMMON_MSG_HEADER.sequence,
crate::test_shared::COMMON_MSG_HEADER.system_id,
crate::test_shared::COMMON_MSG_HEADER.component_id,
0x00,
0x00,
0x00, //msg ID
Expand All @@ -22,8 +22,8 @@ mod test_v2_encode_decode {
0x59,
0x03,
0x03, //payload
16,
240, //checksum
46,
115, //checksum
];

#[test]
Expand Down
Loading