From 3761380f38f873b7b982d007ba716cab9bb6f7bd Mon Sep 17 00:00:00 2001 From: Julian Schmid Date: Mon, 17 Jun 2024 15:27:18 +0200 Subject: [PATCH] Add definition for file transfer packages --- README.md | 1 + src/ft/dlt_ft_data_pkg.rs | 20 +++++++++ src/ft/dlt_ft_end_pkg.rs | 13 ++++++ src/ft/dlt_ft_error_code.rs | 5 +++ src/ft/dlt_ft_error_pkg.rs | 35 +++++++++++++++ src/ft/dlt_ft_file_not_exist_error_pkg.rs | 20 +++++++++ src/ft/dlt_ft_header_pkg.rs | 28 ++++++++++++ src/ft/dlt_ft_info_pkg.rs | 28 ++++++++++++ src/ft/dlt_ft_int.rs | 52 +++++++++++++++++++++++ src/ft/dlt_ft_pkg.rs | 20 +++++++++ src/ft/dlt_ft_uint.rs | 52 +++++++++++++++++++++++ src/ft/mod.rs | 29 +++++++++++++ src/lib.rs | 3 ++ 13 files changed, 306 insertions(+) create mode 100644 src/ft/dlt_ft_data_pkg.rs create mode 100644 src/ft/dlt_ft_end_pkg.rs create mode 100644 src/ft/dlt_ft_error_code.rs create mode 100644 src/ft/dlt_ft_error_pkg.rs create mode 100644 src/ft/dlt_ft_file_not_exist_error_pkg.rs create mode 100644 src/ft/dlt_ft_header_pkg.rs create mode 100644 src/ft/dlt_ft_info_pkg.rs create mode 100644 src/ft/dlt_ft_int.rs create mode 100644 src/ft/dlt_ft_pkg.rs create mode 100644 src/ft/dlt_ft_uint.rs create mode 100644 src/ft/mod.rs diff --git a/README.md b/README.md index 7c66983..f4937a1 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,7 @@ An complete example which includes the parsing of the ethernet & udp headers can ## References * [Log and Trace Protocol Specification](https://www.autosar.org/fileadmin/standards/foundation/1-3/AUTOSAR_PRS_LogAndTraceProtocol.pdf) +* [COVESA DLT Filetransfer](https://github.com/COVESA/dlt-daemon/blob/603f0e4bb87478f7d3e95c89b37790e55ff1e4e5/doc/dlt_filetransfer.md) ## License Licensed under either of Apache License, Version 2.0 or MIT license at your option. The corresponding license texts can be found in the LICENSE-APACHE file and the LICENSE-MIT file. diff --git a/src/ft/dlt_ft_data_pkg.rs b/src/ft/dlt_ft_data_pkg.rs new file mode 100644 index 0000000..07ad6b2 --- /dev/null +++ b/src/ft/dlt_ft_data_pkg.rs @@ -0,0 +1,20 @@ +use super::*; + +/// Package containing a chunk of data of a file. +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +pub struct DltFtDataPkg<'a> { + /// File serial number (usually inode). + pub file_serial_number: DltFtUInt, + + /// Transfered package number. + pub package_nr: DltFtUInt, + + /// Transfered data. + pub data: &'a [u8], +} + + +impl<'a> DltFtDataPkg<'a> { + /// Verbose string at the start and end of the "DLT File Transfer Data" package. + pub const PKG_FLAG: &'static str = "FLDA"; +} diff --git a/src/ft/dlt_ft_end_pkg.rs b/src/ft/dlt_ft_end_pkg.rs new file mode 100644 index 0000000..5916c86 --- /dev/null +++ b/src/ft/dlt_ft_end_pkg.rs @@ -0,0 +1,13 @@ +use super::*; + +/// Package sent after a file transfer is complete. +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +pub struct DltFtEndPkg { + /// File serial number (usually inode). + pub file_serial_number: DltFtUInt, +} + +impl DltFtEndPkg { + /// Verbose string at the start and end of the "DLT File Transfer End" package. + pub const PKG_FLAG: &'static str = "FLFI"; +} diff --git a/src/ft/dlt_ft_error_code.rs b/src/ft/dlt_ft_error_code.rs new file mode 100644 index 0000000..dc20d96 --- /dev/null +++ b/src/ft/dlt_ft_error_code.rs @@ -0,0 +1,5 @@ +use super::*; + +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)] +pub struct DltFtErrorCode(pub DltFtInt); + diff --git a/src/ft/dlt_ft_error_pkg.rs b/src/ft/dlt_ft_error_pkg.rs new file mode 100644 index 0000000..7d4c76c --- /dev/null +++ b/src/ft/dlt_ft_error_pkg.rs @@ -0,0 +1,35 @@ +use super::*; + +/// Error package sent when an error occured with an +/// existing file. +/// +/// If a files does not exist +/// [`crate::ft::DltFileNotExistErrorPkg`] is sent instead. +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +pub struct DltFtErrorPkg<'a, 'b> { + /// Error code. + pub error_code: DltFtErrorCode, + + /// Standard linux error code. + pub linux_error_code: DltFtInt, + + /// File serial number (usually inode). + pub file_serial_number: DltFtUInt, + + /// Absolute path to the file. + pub file_name: &'a str, + + /// Size of the file. + pub file_size: DltFtUInt, + + /// File creaton date. + pub creation_date: &'b str, + + /// Number of packages that will be used to transfer the file. + pub number_of_packages: DltFtUInt, +} + +impl<'a, 'b> DltFtErrorPkg<'a, 'b> { + /// Verbose string at the start and end of the "DLT File Transfer Error" package. + pub const PKG_FLAG: &'static str = "FLER"; +} diff --git a/src/ft/dlt_ft_file_not_exist_error_pkg.rs b/src/ft/dlt_ft_file_not_exist_error_pkg.rs new file mode 100644 index 0000000..76375c0 --- /dev/null +++ b/src/ft/dlt_ft_file_not_exist_error_pkg.rs @@ -0,0 +1,20 @@ +use super::*; + +/// Error package sent if a file that should have been +/// transfered does not exists. +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +pub struct DltFtFileNotExistErrorPkg<'a> { + /// Error code. + pub error_code: DltFtErrorCode, + + /// Standard linux error code. + pub linux_error_code: DltFtInt, + + /// Absolute path to the file. + pub file_name: &'a str, +} + +impl<'a> DltFtFileNotExistErrorPkg<'a> { + /// Verbose string at the start and end of the "DLT File Transfer Error" package. + pub const PKG_FLAG: &'static str = "FLER"; +} diff --git a/src/ft/dlt_ft_header_pkg.rs b/src/ft/dlt_ft_header_pkg.rs new file mode 100644 index 0000000..29b19aa --- /dev/null +++ b/src/ft/dlt_ft_header_pkg.rs @@ -0,0 +1,28 @@ +use super::*; + +/// Packet sent at the start of a file transfer. +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +pub struct DltFtHeaderPkg<'a, 'b> { + /// File serial number (usually inode). + pub file_serial_number: DltFtUInt, + + /// Absolute path to the file. + pub file_name: &'a str, + + /// Size of the file. + pub file_size: DltFtUInt, + + /// File creaton date. + pub creation_date: &'b str, + + /// Number of packages that will be used to transfer the file. + pub number_of_packages: DltFtUInt, + + /// Needed buffer size to reconsturct the file. + pub buffer_size: DltFtUInt, +} + +impl<'a, 'b> DltFtHeaderPkg<'a, 'b> { + /// Verbose string at the start and end of the "DLT File Transfer Header" package. + pub const PKG_FLAG: &'static str = "FLST"; +} diff --git a/src/ft/dlt_ft_info_pkg.rs b/src/ft/dlt_ft_info_pkg.rs new file mode 100644 index 0000000..21298c3 --- /dev/null +++ b/src/ft/dlt_ft_info_pkg.rs @@ -0,0 +1,28 @@ +use super::*; + +/// Info packet for a file if only metadat is sent. +/// +/// This packet is sent if only informations about a file +/// are sent without the file contents. +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +pub struct DltFtInfoPkg<'a, 'b> { + /// File serial number (usually inode). + pub file_serial_number: DltFtUInt, + + /// Absolute path to the file. + pub file_name: &'a str, + + /// Size of the file. + pub file_size: DltFtUInt, + + /// File creaton date. + pub creation_date: &'b str, + + /// Number of packages that will be used to transfer the file. + pub number_of_packages: DltFtUInt, +} + +impl<'a, 'b> DltFtInfoPkg<'a, 'b> { + /// Verbose string at the start and end of the "DLT File Transfer Info" package. + pub const PKG_FLAG: &'static str = "FLIF"; +} diff --git a/src/ft/dlt_ft_int.rs b/src/ft/dlt_ft_int.rs new file mode 100644 index 0000000..0e26e27 --- /dev/null +++ b/src/ft/dlt_ft_int.rs @@ -0,0 +1,52 @@ + +/// Signed integer (either 32 or 64 bit). +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)] +pub enum DltFtInt { + I32(i32), + I64(i64), +} + +impl From for DltFtInt { + fn from(value: i32) -> Self { + DltFtInt::I32(value) + } +} + +impl From for DltFtInt { + fn from(value: i64) -> Self { + DltFtInt::I64(value) + } +} + +#[cfg(target_pointer_width = "32")] +impl From for DltFtInt { + fn from(value: isize) -> Self { + DltFtInt::I32(value as u32) + } +} + +#[cfg(target_pointer_width = "64")] +impl From for DltFtInt { + fn from(value: isize) -> Self { + DltFtInt::I64(value as i64) + } +} + +#[cfg(target_pointer_width = "64")] +impl From for isize { + fn from(value: DltFtInt) -> Self { + match value { + DltFtInt::I32(v) => v as isize, + DltFtInt::I64(v) => v as isize, + } + } +} + +impl From for i64 { + fn from(value: DltFtInt) -> Self { + match value { + DltFtInt::I32(v) => v as i64, + DltFtInt::I64(v) => v, + } + } +} diff --git a/src/ft/dlt_ft_pkg.rs b/src/ft/dlt_ft_pkg.rs new file mode 100644 index 0000000..9eed1c8 --- /dev/null +++ b/src/ft/dlt_ft_pkg.rs @@ -0,0 +1,20 @@ +use super::*; + +/// DLT file transfer package. +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +pub enum DltFtPkg<'a, 'b> { + /// Packet sent at the start of a file transfer. + Header(DltFtHeaderPkg<'a, 'b>), + /// Package containing a chunk of data of a file. + Data(DltFtDataPkg<'a>), + /// Package sent after a file transfer is complete. + End(DltFtEndPkg), + /// Info packet for a file if only metadat is sent. + Info(DltFtInfoPkg<'a, 'b>), + /// Error package sent when an error occured with an + /// existing file. + Error(DltFtErrorPkg<'a, 'b>), + /// Error package sent if a file that should have been + /// transfered does not exists. + FileNotExistsError(DltFtFileNotExistErrorPkg<'a>), +} diff --git a/src/ft/dlt_ft_uint.rs b/src/ft/dlt_ft_uint.rs new file mode 100644 index 0000000..8977d41 --- /dev/null +++ b/src/ft/dlt_ft_uint.rs @@ -0,0 +1,52 @@ + +/// Unsigned integer (either 32 or 64 bit). +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)] +pub enum DltFtUInt { + U32(u32), + U64(u64), +} + +impl From for DltFtUInt { + fn from(value: u32) -> Self { + DltFtUInt::U32(value) + } +} + +impl From for DltFtUInt { + fn from(value: u64) -> Self { + DltFtUInt::U64(value) + } +} + +#[cfg(target_pointer_width = "32")] +impl From for DltFtUInt { + fn from(value: usize) -> Self { + DltFtUInt::U32(value as u32) + } +} + +#[cfg(target_pointer_width = "64")] +impl From for DltFtUInt { + fn from(value: usize) -> Self { + DltFtUInt::U64(value as u64) + } +} + +#[cfg(target_pointer_width = "64")] +impl From for usize { + fn from(value: DltFtUInt) -> Self { + match value { + DltFtUInt::U32(v) => v as usize, + DltFtUInt::U64(v) => v as usize, + } + } +} + +impl From for u64 { + fn from(value: DltFtUInt) -> Self { + match value { + DltFtUInt::U32(v) => v as u64, + DltFtUInt::U64(v) => v, + } + } +} diff --git a/src/ft/mod.rs b/src/ft/mod.rs new file mode 100644 index 0000000..826939a --- /dev/null +++ b/src/ft/mod.rs @@ -0,0 +1,29 @@ +mod dlt_ft_data_pkg; +pub use dlt_ft_data_pkg::*; + +mod dlt_ft_end_pkg; +pub use dlt_ft_end_pkg::*; + +mod dlt_ft_error_code; +pub use dlt_ft_error_code::*; + +mod dlt_ft_error_pkg; +pub use dlt_ft_error_pkg::*; + +mod dlt_ft_file_not_exist_error_pkg; +pub use dlt_ft_file_not_exist_error_pkg::*; + +mod dlt_ft_header_pkg; +pub use dlt_ft_header_pkg::*; + +mod dlt_ft_info_pkg; +pub use dlt_ft_info_pkg::*; + +mod dlt_ft_int; +pub use dlt_ft_int::*; + +mod dlt_ft_pkg; +pub use dlt_ft_pkg::*; + +mod dlt_ft_uint; +pub use dlt_ft_uint::*; diff --git a/src/lib.rs b/src/lib.rs index 1edcf96..1f82d12 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -227,6 +227,9 @@ pub mod control; /// Errors that can be returned by functions in dlt_parse. pub mod error; +/// DLT file transfer related structs & functions (used to tranfer files via DLT). +pub mod ft; + /// Module containing "verbose DLT" encoding & decoding structs & functions. pub mod verbose;