Skip to content

Commit

Permalink
fix: move signing mod, remove incompat_flags since it is breaking
Browse files Browse the repository at this point in the history
feat: add distinct pub fn for signing, signing in MavConnection send, add signing feature to mavlink
test: add --features signing to all test, add signing test, add Debug, Clone to SigningConfig
  • Loading branch information
pv42 committed Aug 23, 2024
1 parent a644d29 commit 002fc11
Show file tree
Hide file tree
Showing 15 changed files with 398 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- uses: actions/checkout@master
- uses: dtolnay/rust-toolchain@stable
- name: Run internal tests
run: cargo test --verbose --features ${{ matrix.dialect }} -- --nocapture
run: cargo test --verbose --features ${{ matrix.dialect }} --features signing -- --nocapture

mavlink-dump:
runs-on: ubuntu-latest
Expand Down
29 changes: 24 additions & 5 deletions mavlink-core/src/connection/direct_serial.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::connection::MavConnection;
use crate::peek_reader::PeekReader;
use crate::{read_versioned_msg, write_versioned_msg, MavHeader, MavlinkVersion, Message};
use crate::{MavHeader, MavlinkVersion, Message};
use core::ops::DerefMut;
use std::io;
use std::sync::Mutex;

use crate::error::{MessageReadError, MessageWriteError};
use serial::{prelude::*, SystemPort};

#[cfg(not(feature = "signing"))]
use crate::{read_versioned_msg, write_versioned_msg};
#[cfg(feature = "signing")]
use super::signing::{SigningConfig, SigningData};
use crate::{read_versioned_msg_signed, write_versioned_msg_signed, SigningConfig, SigningData};

/// Serial MAVLINK connection
Expand Down Expand Up @@ -65,7 +67,15 @@ impl<M: Message> MavConnection<M> for SerialConnection {
fn recv(&self) -> Result<(MavHeader, M), MessageReadError> {
let mut port = self.port.lock().unwrap();
loop {
match read_versioned_msg(port.deref_mut(), self.protocol_version) {
#[cfg(not(feature = "signing"))]
let result = read_versioned_msg(port.deref_mut(), self.protocol_version);
#[cfg(feature = "signing")]
let result = read_versioned_msg_signed(
port.deref_mut(),
self.protocol_version,
self.signing_data.as_ref(),
);
match result {
ok @ Ok(..) => {
return ok;
}
Expand All @@ -84,15 +94,24 @@ impl<M: Message> MavConnection<M> for SerialConnection {
let mut sequence = self.sequence.lock().unwrap();

let header = MavHeader {
incompat_flags: 0,
sequence: *sequence,
system_id: header.system_id,
component_id: header.component_id,
};

*sequence = sequence.wrapping_add(1);

write_versioned_msg(port.reader_mut(), self.protocol_version, header, data)
#[cfg(not(feature = "signing"))]
let result = write_versioned_msg(port.reader_mut(), self.protocol_version, header, data);
#[cfg(feature = "signing")]
let result = write_versioned_msg_signed(
port.reader_mut(),
self.protocol_version,
header,
data,
self.signing_data.as_ref(),
);
result
}

fn set_protocol_version(&mut self, version: MavlinkVersion) {
Expand Down
16 changes: 13 additions & 3 deletions mavlink-core/src/connection/file.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crate::connection::MavConnection;
use crate::error::{MessageReadError, MessageWriteError};
use crate::peek_reader::PeekReader;
use crate::{read_versioned_msg, MavHeader, MavlinkVersion, Message};
use crate::{MavHeader, MavlinkVersion, Message};
use core::ops::DerefMut;
use std::fs::File;
use std::io;
use std::sync::Mutex;

#[cfg(not(feature = "signing"))]
use crate::read_versioned_msg;
#[cfg(feature = "signing")]
use super::signing::{SigningConfig, SigningData};
use crate::{read_versioned_msg_signed, SigningConfig, SigningData};

/// File MAVLINK connection
Expand Down Expand Up @@ -37,7 +39,15 @@ impl<M: Message> MavConnection<M> for FileConnection {
let mut file = self.file.lock().unwrap();

loop {
match read_versioned_msg(file.deref_mut(), self.protocol_version) {
#[cfg(not(feature = "signing"))]
let result = read_versioned_msg(file.deref_mut(), self.protocol_version);
#[cfg(feature = "signing")]
let result = read_versioned_msg_signed(
file.deref_mut(),
self.protocol_version,
self.signing_data.as_ref(),
);
match result {
ok @ Ok(..) => {
return ok;
}
Expand Down
4 changes: 1 addition & 3 deletions mavlink-core/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ mod udp;
mod direct_serial;

#[cfg(feature = "signing")]
pub(crate) mod signing;
#[cfg(feature = "signing")]
pub use signing::SigningConfig;
use crate::SigningConfig;

mod file;

Expand Down
30 changes: 25 additions & 5 deletions mavlink-core/src/connection/tcp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::connection::MavConnection;
use crate::peek_reader::PeekReader;
use crate::{read_versioned_msg, write_versioned_msg, MavHeader, MavlinkVersion, Message};
use crate::{MavHeader, MavlinkVersion, Message};
use core::ops::DerefMut;
use std::io;
use std::net::ToSocketAddrs;
Expand All @@ -10,8 +10,11 @@ use std::time::Duration;

use super::get_socket_addr;

#[cfg(not(feature = "signing"))]
use crate::{read_versioned_msg, write_versioned_msg};

#[cfg(feature = "signing")]
use super::signing::{SigningConfig, SigningData};
use crate::{read_versioned_msg_signed, write_versioned_msg_signed, SigningConfig, SigningData};

/// TCP MAVLink connection
Expand Down Expand Up @@ -97,21 +100,38 @@ struct TcpWrite {
impl<M: Message> MavConnection<M> for TcpConnection {
fn recv(&self) -> Result<(MavHeader, M), crate::error::MessageReadError> {
let mut reader = self.reader.lock().unwrap();
read_versioned_msg(reader.deref_mut(), self.protocol_version)
#[cfg(not(feature = "signing"))]
let result = read_versioned_msg(reader.deref_mut(), self.protocol_version);
#[cfg(feature = "signing")]
let result = read_versioned_msg_signed(
reader.deref_mut(),
self.protocol_version,
self.signing_data.as_ref(),
);
result
}

fn send(&self, header: &MavHeader, data: &M) -> Result<usize, crate::error::MessageWriteError> {
let mut lock = self.writer.lock().unwrap();

let header = MavHeader {
incompat_flags: 0,
sequence: lock.sequence,
system_id: header.system_id,
component_id: header.component_id,
};

lock.sequence = lock.sequence.wrapping_add(1);
write_versioned_msg(&mut lock.socket, self.protocol_version, header, data)
#[cfg(not(feature = "signing"))]
let result = write_versioned_msg(&mut lock.socket, self.protocol_version, header, data);
#[cfg(feature = "signing")]
let result = write_versioned_msg_signed(
&mut lock.socket,
self.protocol_version,
header,
data,
self.signing_data.as_ref(),
);
result
}

fn set_protocol_version(&mut self, version: MavlinkVersion) {
Expand Down
34 changes: 30 additions & 4 deletions mavlink-core/src/connection/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::VecDeque;

use crate::connection::MavConnection;
use crate::peek_reader::PeekReader;
use crate::{read_versioned_msg, write_versioned_msg, MavHeader, MavlinkVersion, Message};
use crate::{MavHeader, MavlinkVersion, Message};
use core::ops::DerefMut;
use std::io::{self, Read};
use std::net::ToSocketAddrs;
Expand All @@ -11,8 +11,11 @@ use std::sync::Mutex;

use super::get_socket_addr;

#[cfg(not(feature = "signing"))]
use crate::{read_versioned_msg, write_versioned_msg};

#[cfg(feature = "signing")]
use super::signing::{SigningConfig, SigningData};
use crate::{read_versioned_msg_signed, write_versioned_msg_signed, SigningConfig, SigningData};

/// UDP MAVLink connection
Expand Down Expand Up @@ -124,7 +127,14 @@ impl<M: Message> MavConnection<M> for UdpConnection {
let mut reader = self.reader.lock().unwrap();

loop {
#[cfg(not(feature = "signing"))]
let result = read_versioned_msg(reader.deref_mut(), self.protocol_version);
#[cfg(feature = "signing")]
let result = read_versioned_msg_signed(
reader.deref_mut(),
self.protocol_version,
self.signing_data.as_ref(),
);
if self.server {
if let addr @ Some(_) = reader.reader_ref().last_recv_address {
self.writer.lock().unwrap().dest = addr;
Expand All @@ -141,7 +151,6 @@ impl<M: Message> MavConnection<M> for UdpConnection {
let state = &mut *guard;

let header = MavHeader {
incompat_flags: 0,
sequence: state.sequence,
system_id: header.system_id,
component_id: header.component_id,
Expand All @@ -151,7 +160,24 @@ impl<M: Message> MavConnection<M> for UdpConnection {

let len = if let Some(addr) = state.dest {
let mut buf = Vec::new();
write_versioned_msg(&mut buf, self.protocol_version, header, data)?;
#[cfg(not(feature = "signing"))]
write_versioned_msg(
&mut buf,
self.protocol_version,
header,
data,
#[cfg(feature = "signing")]
self.signing_data.as_ref(),
)?;
#[cfg(feature = "signing")]
write_versioned_msg_signed(
&mut buf,
self.protocol_version,
header,
data,
#[cfg(feature = "signing")]
self.signing_data.as_ref(),
)?;
state.socket.send_to(&buf, addr)?
} else {
0
Expand Down
Loading

0 comments on commit 002fc11

Please sign in to comment.