autosar_e2e/
lib.rs

1//! # AUTOSAR E2E Protection Library
2//!
3//! This library implements the AUTOSAR E2E (End-to-End) protection mechanism
4//! as specified in the AUTOSAR standard.
5//!
6//! ## Overview
7//!
8//! The E2E protection mechanism provides end-to-end data protection for
9//! safety-critical automotive communication. It detects errors in data
10//! transmission including:
11//! - Data corruption (via CRC)
12//! - Message loss, duplication, or reordering (via sequence counter)
13//! - Incorrect addressing (via Data ID)
14//!
15//! ## Example
16//!
17//! ```rust
18//! use autosar_e2e::{E2EProfile, E2EResult};
19//! use autosar_e2e::profile11::{Profile11, Profile11Config, Profile11IdMode};
20//!
21//! # fn main() -> E2EResult<()> {
22//! // Create a Profile 11 configuration
23//! let config = Profile11Config {
24//!     mode: Profile11IdMode::Nibble,
25//!     max_delta_counter: 1,
26//!     data_length: 40,
27//!     ..Default::default()
28//! };
29//!
30//! // Create the profile instance
31//! let mut profile = Profile11::new(config)?;
32//!
33//! // Protect data
34//! let mut data = vec![0x00, 0x00, 0x12, 0x34, 0x56]; //[CRC, counter, user data ..]
35//! profile.protect(&mut data)?;
36//!
37//! // Check protected data
38//! let status = profile.check(&data)?;
39//! # Ok(())
40//! # }
41//! ```
42
43use thiserror::Error;
44
45mod profiles;
46pub use profiles::profile11;
47pub use profiles::profile22;
48pub use profiles::profile4;
49pub use profiles::profile4m;
50pub use profiles::profile5;
51pub use profiles::profile6;
52pub use profiles::profile7;
53pub use profiles::profile7m;
54pub use profiles::profile8;
55
56/// Result type for E2E operations
57pub type E2EResult<T> = Result<T, E2EError>;
58
59/// E2E Protection status enumeration
60#[derive(Debug, Clone, Copy, PartialEq, Eq)]
61pub enum E2EStatus {
62    /// The checks of data in this cycle is successful
63    Ok,
64    /// CRC check failed - data corruption detected
65    CrcError,
66    /// Data ID check failed - incorrect addressing
67    DataIdError,
68    /// Counter check failed - same counter as previous cycle
69    Repeated,
70    /// Counter check failed - counter is increased within allowed configured delta
71    OkSomeLost,
72    /// Counter check failed - possible message loss/duplication
73    WrongSequence,
74    /// Data Length check failed - incorrect length
75    DataLengthError,
76    /// Source ID check failed - incorrect addressing
77    SourceIdError,
78    /// Message Type check failed
79    MessageTypeError,
80    /// Message Result check failed
81    MessageResultError,
82}
83
84/// E2E Error types
85#[derive(Debug, Clone, Error, PartialEq, Eq)]
86pub enum E2EError {
87    /// Invalid configuration provided
88    #[error("Invalid configuration: {0}")]
89    InvalidConfiguration(String),
90
91    /// Invalid data format
92    #[error("Invalid data format: {0}")]
93    InvalidDataFormat(String),
94}
95
96// Main trait for E2E Profile implementations
97///
98/// This trait defines the common interface that all E2E profiles must implement.
99/// Each profile provides three main operations:
100/// - `protect`: Add E2E protection to data
101/// - `check`: Verify E2E protection on received data
102/// - `forward`: Forward protected data (Profile 11 specific)
103pub trait E2EProfile {
104    /// Configuration type for this profile
105    type Config;
106
107    /// Create a new instance with the given configuration
108    ///
109    /// # Errors
110    /// Returns `E2EError::InvalidConfiguration` if the configuration is invalid
111    fn new(config: Self::Config) -> E2EResult<Self>
112    where
113        Self: Sized;
114
115    /// Add E2E protection to the given data buffer
116    ///
117    /// This function modifies the data buffer in-place by adding:
118    /// - CRC checksum
119    /// - Sequence counter
120    /// - Data ID (if applicable)
121    ///
122    /// # Arguments
123    /// * `data` - Mutable reference to the data buffer to protect
124    ///
125    /// # Returns
126    /// * `Ok(())` if protection was successfully added
127    /// * `Err(E2EError)` if an error occurred
128    fn protect(&mut self, data: &mut [u8]) -> E2EResult<()>;
129
130    /// Check E2E protection on received data
131    ///
132    /// This function verifies the integrity of the received data by checking:
133    /// - CRC checksum
134    /// - Sequence counter continuity
135    /// - Data ID (if applicable)
136    ///
137    /// # Arguments
138    /// * `data` - Reference to the received data buffer
139    ///
140    /// # Returns
141    /// * `Ok(E2EStatus)` indicating the check result
142    /// * `Err(E2EError)` if an error occurred during checking
143    fn check(&mut self, data: &[u8]) -> E2EResult<E2EStatus>;
144}
145
146#[cfg(test)]
147mod tests {
148    use super::*;
149
150    #[test]
151    fn test_e2e_status() {
152        assert_eq!(E2EStatus::Ok, E2EStatus::Ok);
153        assert_ne!(E2EStatus::Ok, E2EStatus::CrcError);
154    }
155}