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

0.10: Fix serialization and deserialization problems #208

Merged
merged 6 commits into from
Nov 21, 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
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
extern crate byteorder;
use byteorder::LittleEndian;
#[cfg(feature = "std")]
use byteorder::{ReadBytesExt, WriteBytesExt};

Check warning on line 38 in src/lib.rs

View workflow job for this annotation

GitHub Actions / build (macos-latest, x86_64-apple-darwin, --features all-dialects)

unused import: `WriteBytesExt`

Check warning on line 38 in src/lib.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, arm-unknown-linux-musleabihf, --features ardupilotmega)

unused import: `WriteBytesExt`

Check warning on line 38 in src/lib.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, armv7-unknown-linux-musleabihf, --features ardupilotmega)

unused import: `WriteBytesExt`

Check warning on line 38 in src/lib.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, x86_64-unknown-linux-musl, --features all-dialects)

unused import: `WriteBytesExt`

Check warning on line 38 in src/lib.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, x86_64-unknown-linux-musl, --features all-dialects,emit-extensions)

unused import: `WriteBytesExt`

Check warning on line 38 in src/lib.rs

View workflow job for this annotation

GitHub Actions / build (windows-latest, x86_64-pc-windows-msvc, --features all-dialects)

unused import: `WriteBytesExt`

#[cfg(feature = "std")]
mod connection;
Expand Down Expand Up @@ -145,15 +145,15 @@
let mut v = vec![];

// serialize header
v.push(self.header.sequence);
v.push(self.header.system_id);
v.push(self.header.component_id);
v.push(self.header.sequence);

// message id
match self.protocol_version {
MavlinkVersion::V2 => {
let bytes: [u8; 4] = self.msg.message_id().to_le_bytes();
v.extend_from_slice(&bytes);
v.extend_from_slice(&bytes[..3]);
}
MavlinkVersion::V1 => {
v.push(self.msg.message_id() as u8); //TODO check
Expand All @@ -170,17 +170,17 @@
pub fn deser(version: MavlinkVersion, input: &[u8]) -> Result<Self, ParserError> {
let mut buf = BytesMut::from(input);

let sequence = buf.get_u8();
let system_id = buf.get_u8();
let component_id = buf.get_u8();
let sequence = buf.get_u8();
let header = MavHeader {
system_id,
component_id,
sequence,
};

let msg_id = match version {
MavlinkVersion::V2 => buf.get_u32_le(),
MavlinkVersion::V2 => buf.get_int_le(3) as u32,
MavlinkVersion::V1 => buf.get_u8() as u32,
};

Expand Down
96 changes: 96 additions & 0 deletions tests/mav_frame_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
pub mod test_shared;

mod mav_frame_tests {
use mavlink::common::MavMessage;
use mavlink::MavFrame;
use mavlink::MavHeader;

// NOTE: No header, based over get_heartbeat_msg
pub const HEARTBEAT_V2: &[u8] = &[
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 - custom_mode
0x00, //
0x00, //
0x00, //
0x02, // mav_type
0x03, // autopilot
0x59, // base_mode
0x03, // system_status
0x03, // mavlink_version
0x10, // checksum
0xf0,
];

#[test]
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 buffer = frame.ser();
assert_eq!(buffer, HEARTBEAT_V2[..buffer.len()]);

let MavMessage::HEARTBEAT(msg) = frame.msg else {
panic!("Decoded wrong message type");
};
assert_eq!(msg.custom_mode, heartbeat_msg.custom_mode);
assert_eq!(msg.mavtype, heartbeat_msg.mavtype);
assert_eq!(msg.autopilot, heartbeat_msg.autopilot);
assert_eq!(msg.base_mode, heartbeat_msg.base_mode);
assert_eq!(msg.system_status, heartbeat_msg.system_status);
assert_eq!(msg.mavlink_version, heartbeat_msg.mavlink_version);
}

#[test]
pub fn test_deser_ser_message() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test would be awesome with proptest

let mavlink_message = mavlink_message();
let mavlink_frame = new(mavlink_message);

let buffer = mavlink_frame.ser();

let parsed_mavlink_frame =
MavFrame::<mavlink::common::MavMessage>::deser(mavlink::MavlinkVersion::V2, &buffer)
.unwrap();

assert_eq!(
format!("{mavlink_frame:?}"),
format!("{parsed_mavlink_frame:?}")
);
}

fn mavlink_message() -> mavlink::common::MavMessage {
mavlink::common::MavMessage::LINK_NODE_STATUS(mavlink::common::LINK_NODE_STATUS_DATA {
timestamp: 92197916,
tx_rate: 0x11223344,
rx_rate: 0x55667788,
messages_sent: 0x99001122,
messages_received: 0x33445566,
messages_lost: 0x77889900,
rx_parse_err: 0x1122,
tx_overflows: 0x3355,
rx_overflows: 0x5566,
tx_buf: 0xff,
rx_buf: 0x11,
})
}

fn new(msg: MavMessage) -> MavFrame<MavMessage> {
MavFrame {
header: MavHeader {
system_id: 1,
component_id: 2,
sequence: 84,
},
msg,
protocol_version: mavlink::MavlinkVersion::V2,
}
}
}
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 @@ extern crate mavlink;
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 @@ -9,9 +9,9 @@ mod test_v1_encode_decode {
pub const HEARTBEAT_V1: &'static [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 @@ -22,8 +22,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 @@ -10,9 +10,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 @@ -25,8 +25,8 @@ mod test_v2_encode_decode {
0x59,
0x03,
0x03, //payload
16,
240, //checksum
46,
115, //checksum
];

#[test]
Expand Down
Loading