Skip to content
Snippets Groups Projects
Commit 12558c28 authored by Federico Lolli's avatar Federico Lolli
Browse files

Added sealed module to suppress warnings

parent 96287274
No related branches found
No related tags found
1 merge request!13Refactored Mavlink Communication for Improved Reliability
......@@ -8,6 +8,25 @@ mod error;
pub mod ethernet;
pub mod serial;
use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
use ring_channel::{RingReceiver, TryRecvError};
use sealed::MessageTransceiver;
use skyward_mavlink::mavlink::MavFrame;
use crate::mavlink::{MavMessage, TimedMessage};
// Re-exports
pub use error::{CommunicationError, ConnectionError};
pub use ethernet::EthernetConfiguration;
pub use serial::SerialConfiguration;
const MAX_STORED_MSGS: usize = 1000; // e.g., 192 bytes each = 192 KB
mod sealed {
use std::{
num::NonZeroUsize,
sync::{
......@@ -17,7 +36,7 @@ use std::{
};
use enum_dispatch::enum_dispatch;
use ring_channel::{RingReceiver, TryRecvError, ring_channel};
use ring_channel::ring_channel;
use skyward_mavlink::mavlink::{
MavFrame,
error::{MessageReadError, MessageWriteError},
......@@ -28,33 +47,15 @@ use crate::{
mavlink::{MavMessage, TimedMessage},
};
use ethernet::EthernetTransceiver;
use serial::SerialTransceiver;
// Re-exports
pub use error::{CommunicationError, ConnectionError};
pub use ethernet::EthernetConfiguration;
pub use serial::SerialConfiguration;
const MAX_STORED_MSGS: usize = 1000; // e.g., 192 bytes each = 192 KB
/// Trait to abstract common configuration types.
pub trait TransceiverConfig: TransceiverConfigSealed {}
trait TransceiverConfigSealed {}
impl<T: TransceiverConfigSealed> TransceiverConfig for T {}
impl<T: Connectable> TransceiverConfigSealed for T {}
use super::{
CommunicationError, Connection, ConnectionError, MAX_STORED_MSGS,
ethernet::EthernetTransceiver, serial::SerialTransceiver,
};
/// Extension trait to open a connection directly from a configuration.
pub trait TransceiverConfigExt: Connectable {
/// Opens a connection and returns a handle to it.
fn open_connection(&self) -> Result<Connection, ConnectionError> {
Ok(self.connect()?.open_listening_connection())
}
}
impl<T: Connectable> TransceiverConfigExt for T {}
pub trait TransceiverConfigSealed {}
/// Trait representing an entity that can be connected.
trait Connectable {
pub trait Connectable {
type Connected: MessageTransceiver;
/// Establishes a connection based on the configuration.
......@@ -66,7 +67,7 @@ trait Connectable {
/// It also provides a default implementation for opening a listening connection, while
/// being transparent to the actual Transceiver type.
#[enum_dispatch(Transceivers)]
trait MessageTransceiver: Send + Sync + Into<Transceivers> {
pub trait MessageTransceiver: Send + Sync + Into<Transceivers> {
/// Blocks until a valid message is received.
fn wait_for_message(&self) -> Result<TimedMessage, MessageReadError>;
......@@ -115,14 +116,29 @@ trait MessageTransceiver: Send + Sync + Into<Transceivers> {
/// Enum representing the different types of transceivers.
#[enum_dispatch]
enum Transceivers {
pub(super) enum Transceivers {
Serial(SerialTransceiver),
Ethernet(EthernetTransceiver),
}
}
/// Trait to abstract common configuration types.
pub trait TransceiverConfig: sealed::TransceiverConfigSealed {}
impl<T: sealed::TransceiverConfigSealed> TransceiverConfig for T {}
impl<T: sealed::Connectable> sealed::TransceiverConfigSealed for T {}
/// Extension trait to open a connection directly from a configuration.
pub trait TransceiverConfigExt: sealed::Connectable {
/// Opens a connection and returns a handle to it.
fn open_connection(&self) -> Result<Connection, ConnectionError> {
Ok(self.connect()?.open_listening_connection())
}
}
impl<T: sealed::Connectable> TransceiverConfigExt for T {}
/// Represents an active connection with buffered messages.
pub struct Connection {
transceiver: Arc<Transceivers>,
transceiver: Arc<sealed::Transceivers>,
rx_ring_channel: RingReceiver<TimedMessage>,
running_flag: Arc<AtomicBool>,
}
......
......@@ -14,7 +14,10 @@ use tracing::{debug, trace};
use crate::mavlink::{MAX_MSG_SIZE, MavMessage, TimedMessage, peek_reader::PeekReader};
use super::{Connectable, ConnectionError, MessageTransceiver};
use super::{
ConnectionError,
sealed::{Connectable, MessageTransceiver},
};
/// Configuration for an Ethernet connection.
#[derive(Debug, Clone)]
......
......@@ -18,7 +18,10 @@ use crate::{
mavlink::{MavMessage, TimedMessage, peek_reader::PeekReader},
};
use super::{Connectable, ConnectionError, MessageTransceiver};
use super::{
ConnectionError,
sealed::{Connectable, MessageTransceiver},
};
const SERIAL_PORT_TIMEOUT_MS: u64 = 100;
pub const DEFAULT_BAUD_RATE: u32 = 115200;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment