From 82c78e698193d2f78f6c4e84c7fca29fa8a03f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Mon, 20 Nov 2023 10:08:31 -0300 Subject: [PATCH 1/4] tests: Use COMMON_MSG_HEADER values over hardcoded numbers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- tests/mav_frame_tests.rs | 23 +++++++++++++++++------ tests/v1_encode_decode_tests.rs | 6 +++--- tests/v2_encode_decode_tests.rs | 6 +++--- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/tests/mav_frame_tests.rs b/tests/mav_frame_tests.rs index ad4fcfbec5..50e900f8cd 100644 --- a/tests/mav_frame_tests.rs +++ b/tests/mav_frame_tests.rs @@ -3,12 +3,23 @@ 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] diff --git a/tests/v1_encode_decode_tests.rs b/tests/v1_encode_decode_tests.rs index 0f17e74426..bf0efeda2d 100644 --- a/tests/v1_encode_decode_tests.rs +++ b/tests/v1_encode_decode_tests.rs @@ -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, diff --git a/tests/v2_encode_decode_tests.rs b/tests/v2_encode_decode_tests.rs index db254607ab..46a8c63d02 100644 --- a/tests/v2_encode_decode_tests.rs +++ b/tests/v2_encode_decode_tests.rs @@ -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 From 37155f0658e91570ff7b38a37f46db1720148800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Mon, 20 Nov 2023 10:09:12 -0300 Subject: [PATCH 2/4] tests: Change value of component id to check for correct ser/deser logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- tests/test_shared/mod.rs | 2 +- tests/v1_encode_decode_tests.rs | 4 ++-- tests/v2_encode_decode_tests.rs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_shared/mod.rs b/tests/test_shared/mod.rs index 4532b6bd40..4cba46907a 100644 --- a/tests/test_shared/mod.rs +++ b/tests/test_shared/mod.rs @@ -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")] diff --git a/tests/v1_encode_decode_tests.rs b/tests/v1_encode_decode_tests.rs index bf0efeda2d..db7ea6fcaf 100644 --- a/tests/v1_encode_decode_tests.rs +++ b/tests/v1_encode_decode_tests.rs @@ -19,8 +19,8 @@ mod test_v1_encode_decode { 0x59, 0x03, 0x03, - 0xf1, - 0xd7, + 0x1f, + 0x50, ]; #[test] diff --git a/tests/v2_encode_decode_tests.rs b/tests/v2_encode_decode_tests.rs index 46a8c63d02..0aae35bf55 100644 --- a/tests/v2_encode_decode_tests.rs +++ b/tests/v2_encode_decode_tests.rs @@ -22,8 +22,8 @@ mod test_v2_encode_decode { 0x59, 0x03, 0x03, //payload - 16, - 240, //checksum + 46, + 115, //checksum ]; #[test] From f39cd289c01410fd4cc8cc7f60749e1077418c18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Mon, 20 Nov 2023 12:42:43 -0300 Subject: [PATCH 3/4] lib: Fix serialization of mavlink v2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Only 3 bytes are used by message id Signed-off-by: Patrick José Pereira --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 871006f13b..e770161cfd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -162,7 +162,7 @@ impl MavFrame { 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 From 49686b79d374cd412e86ce6298d3695b7ee85b1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Mon, 20 Nov 2023 12:48:10 -0300 Subject: [PATCH 4/4] tests: Add serialization test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- tests/mav_frame_tests.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/mav_frame_tests.rs b/tests/mav_frame_tests.rs index 50e900f8cd..ef20068ffe 100644 --- a/tests/mav_frame_tests.rs +++ b/tests/mav_frame_tests.rs @@ -23,7 +23,7 @@ mod mav_frame_tests { ]; #[test] - pub fn test_deser() { + pub fn test_deser_ser() { use mavlink::{common::MavMessage, MavFrame, MavlinkVersion}; let frame = MavFrame::::deser(MavlinkVersion::V2, HEARTBEAT_V2) .expect("failed to parse message"); @@ -31,6 +31,10 @@ mod mav_frame_tests { 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"),