mavlink-core 0.21.0

Implements the MAVLink data interchange format for UAVs.
Documentation
//! File MAVLINK connection

use super::config::FileConfig;

use crate::Dialect;
use crate::MavlinkReader;
use crate::connection::sync::{Connectable, DialectConnection, SyncTransport, Transport};
use std::fs::File;
use std::io::{self, Sink};
use std::path::PathBuf;
use std::sync::Mutex;

pub fn open(file_path: &PathBuf) -> io::Result<FileConnection> {
    Ok(FileConnection {
        file: Mutex::new(MavlinkReader::new(File::open(file_path)?)),
    })
}

pub struct FileConnection {
    file: Mutex<MavlinkReader<File>>,
}

impl SyncTransport for FileConnection {
    type Reader = File;
    type Writer = Sink;

    fn reader(&self) -> std::sync::MutexGuard<'_, MavlinkReader<Self::Reader>> {
        self.file.lock().unwrap()
    }

    fn lock_writer(&self) -> Option<std::sync::MutexGuard<'_, Self::Writer>> {
        None
    }

    fn retry_receive(&self, error: &crate::error::MessageReadError) -> bool {
        !matches!(error, crate::error::MessageReadError::Io(error) if error.kind() == io::ErrorKind::UnexpectedEof)
    }
}

impl Connectable for FileConfig {
    fn connect_with_dialect<D: Dialect + Send + Sync>(
        &self,
        dialect: D,
    ) -> io::Result<DialectConnection<D>> {
        Ok(DialectConnection::new(
            Transport::File(open(&self.address)?),
            dialect,
        ))
    }
}