Skip to content

Commit 3708729

Browse files
GabrielDertonipatrickelectric
authored andcommitted
fix: correctly deserialize MavFrame
This commit addresses two bugs present in the `MavFrame::deser` implementation. It parsed the sequence number after system and component ids, but should instead parse it before. Furthermore the implementation used the `get_u32_le` method to parse the message id in mavlink v2. However the MAVLink spec specifies that this field should have 3 bytes, not 4 (like `u32`).
1 parent 12a811d commit 3708729

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -180,18 +180,18 @@ impl<M: Message> MavFrame<M> {
180180
pub fn deser(version: MavlinkVersion, input: &[u8]) -> Result<Self, ParserError> {
181181
let mut buf = Bytes::new(input);
182182

183+
let sequence = buf.get_u8();
183184
let system_id = buf.get_u8();
184185
let component_id = buf.get_u8();
185-
let sequence = buf.get_u8();
186186
let header = MavHeader {
187187
system_id,
188188
component_id,
189189
sequence,
190190
};
191191

192192
let msg_id = match version {
193-
MavlinkVersion::V2 => buf.get_u32_le(),
194-
MavlinkVersion::V1 => buf.get_u8().into(),
193+
MavlinkVersion::V2 => buf.get_u24_le(),
194+
MavlinkVersion::V1 => buf.get_u8() as u32,
195195
};
196196

197197
match M::parse(version, msg_id, buf.remaining_bytes()) {

tests/mav_frame_tests.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
pub mod test_shared;
2+
3+
mod mav_frame_tests {
4+
// NOTE: No header
5+
pub const HEARTBEAT_V2: &[u8] = &[
6+
0xef, // seq 239
7+
0x01, // sys ID
8+
0x01, // comp ID
9+
0x00, 0x00, 0x00, // msg ID
10+
0x05, 0x00, 0x00, 0x00, 0x02, 0x03, 0x59, 0x03, 0x03, // payload
11+
16, 240, // checksum
12+
];
13+
14+
#[test]
15+
pub fn test_deser() {
16+
use mavlink::{common::MavMessage, MavFrame, MavlinkVersion};
17+
let frame = MavFrame::<MavMessage>::deser(MavlinkVersion::V2, HEARTBEAT_V2)
18+
.expect("failed to parse message");
19+
20+
assert_eq!(frame.header, crate::test_shared::COMMON_MSG_HEADER);
21+
let heartbeat_msg = crate::test_shared::get_heartbeat_msg();
22+
23+
let msg = match frame.msg {
24+
MavMessage::HEARTBEAT(msg) => msg,
25+
_ => panic!("Decoded wrong message type"),
26+
};
27+
assert_eq!(msg.custom_mode, heartbeat_msg.custom_mode);
28+
assert_eq!(msg.mavtype, heartbeat_msg.mavtype);
29+
assert_eq!(msg.autopilot, heartbeat_msg.autopilot);
30+
assert_eq!(msg.base_mode, heartbeat_msg.base_mode);
31+
assert_eq!(msg.system_status, heartbeat_msg.system_status);
32+
assert_eq!(msg.mavlink_version, heartbeat_msg.mavlink_version);
33+
}
34+
}

0 commit comments

Comments
 (0)