Skip to content

Commit

Permalink
feat: add read_v2_raw_message_async_signed
Browse files Browse the repository at this point in the history
  • Loading branch information
pv42 committed Aug 22, 2024
1 parent 283657b commit 27469bf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
3 changes: 2 additions & 1 deletion mavlink-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ sha2 = { version = "0.10", optional = true }
"serde" = ["dep:serde", "dep:serde_arrays"]
"tokio-1" = ["dep:tokio"]
"signing" = ["dep:sha2"]
default = ["std", "tcp", "udp", "direct-serial", "serde"]
# DO NOT COMMIT
default = ["std", "tcp", "udp", "direct-serial", "serde", "tokio-1", "signing"]
34 changes: 32 additions & 2 deletions mavlink-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,10 +878,21 @@ fn read_v2_raw_message_inner<M: Message, R: Read>(
}

/// Async read a raw buffer with the mavlink message
///
/// V2 maximum size is 280 bytes: `<https://mavlink.io/en/guide/serialization.html>`
#[cfg(feature = "tokio-1")]
pub async fn read_v2_raw_message_async<M: Message, R: tokio::io::AsyncReadExt + Unpin>(
reader: &mut R,
) -> Result<MAVLinkV2MessageRaw, error::MessageReadError> {
read_v2_raw_message_async_inner::<M,R>(reader, None).await
}

/// Async read a raw buffer with the mavlink message
/// V2 maximum size is 280 bytes: `<https://mavlink.io/en/guide/serialization.html>`
#[cfg(feature = "tokio-1")]
async fn read_v2_raw_message_async_inner<M: Message, R: tokio::io::AsyncReadExt + Unpin>(
reader: &mut R,
signing_data: Option<&SigningData>,
) -> Result<MAVLinkV2MessageRaw, error::MessageReadError> {
loop {
loop {
Expand All @@ -906,12 +917,31 @@ pub async fn read_v2_raw_message_async<M: Message, R: tokio::io::AsyncReadExt +
.read_exact(message.mut_payload_and_checksum_and_sign())
.await?;

if message.has_valid_crc::<M>() {
return Ok(message);
if !message.has_valid_crc::<M>() {
continue;
}

#[cfg(feature = "signing")]
if let Some(signing_data) = signing_data {
if !signing_data.verify_signature(&message) {
continue;
}
}

return Ok(message);
}
}

/// Async read a raw buffer with the mavlink message with signing support
/// V2 maximum size is 280 bytes: `<https://mavlink.io/en/guide/serialization.html>`
#[cfg(all(feature = "tokio-1", feature = "signing"))]
pub async fn read_v2_raw_message_async_signed<M: Message, R: tokio::io::AsyncReadExt + Unpin>(
reader: &mut R,
signing_data: Option<&SigningData>,
) -> Result<MAVLinkV2MessageRaw, error::MessageReadError> {
read_v2_raw_message_async_inner::<M, R>(reader, signing_data).await
}

/// Async read a raw buffer with the mavlink message
/// V2 maximum size is 280 bytes: `<https://mavlink.io/en/guide/serialization.html>`
///
Expand Down

0 comments on commit 27469bf

Please sign in to comment.