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//! // Create a Profile 11 configuration
22//! let config = Profile11Config {
23//!     mode: Profile11IdMode::Nibble,
24//!     max_delta_counter: 1,
25//!     data_length: 40,
26//!     ..Default::default()
27//! };
28//!
29//! // Create the profile instance
30//! let mut profile = Profile11::new(config);
31//!
32//! // Protect data
33//! let mut data = vec![0x00, 0x00, 0x12, 0x34, 0x56]; //[CRC, counter, user data ..]
34//! profile.protect(&mut data).unwrap();
35//!
36//! // Check protected data
37//! let status = profile.check(&data).unwrap();
38//! ```
39
40use thiserror::Error;
41pub mod profile11;
42pub mod profile22;
43pub mod profile4;
44
45/// Result type for E2E operations
46pub type E2EResult<T> = Result<T, E2EError>;
47
48/// E2E Protection status enumeration
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub enum E2EStatus {
51    /// The checks of data in this cycle is successful
52    Ok,
53    /// CRC check failed - data corruption detected
54    CrcError,
55    /// Data ID check failed - incorrect addressing
56    DataIdError,
57    // Counter check failed - same counter as previous cycle 
58    Repeated,
59    // Counter check failed - counter is increated within allowed configured delta
60    OkSomeLost,
61    /// Counter check failed - possible message loss/duplication
62    WrongSequence,
63    /// Data Length check failed - incorrect length
64    DataLengthError,
65}
66
67/// E2E Error types
68#[derive(Debug, Clone, Error, PartialEq, Eq)]
69pub enum E2EError {
70    /// Invalid configuration provided
71    #[error("Invalid configuration: {0}")]
72    InvalidConfiguration(String),
73        
74    /// Invalid data format
75    #[error("Invalid data format: {0}")]
76    InvalidDataFormat(String),
77    
78    /// Profile-specific error
79    #[error("Profile-specific error: {0}")]
80    ProfileSpecificError(String),
81}
82
83// Main trait for E2E Profile implementations
84///
85/// This trait defines the common interface that all E2E profiles must implement.
86/// Each profile provides three main operations:
87/// - `protect`: Add E2E protection to data
88/// - `check`: Verify E2E protection on received data
89/// - `forward`: Forward protected data (Profile 11 specific)
90pub trait E2EProfile {
91    /// Configuration type for this profile
92    type Config;
93
94    /// Create a new instance with the given configuration
95    fn new(config: Self::Config) -> Self;
96
97    /// Add E2E protection to the given data buffer
98    ///
99    /// This function modifies the data buffer in-place by adding:
100    /// - CRC checksum
101    /// - Sequence counter
102    /// - Data ID (if applicable)
103    ///
104    /// # Arguments
105    /// * `data` - Mutable reference to the data buffer to protect
106    ///
107    /// # Returns
108    /// * `Ok(())` if protection was successfully added
109    /// * `Err(E2EError)` if an error occurred
110    fn protect(&mut self, data: &mut [u8]) -> E2EResult<()>;
111
112    /// Check E2E protection on received data
113    ///
114    /// This function verifies the integrity of the received data by checking:
115    /// - CRC checksum
116    /// - Sequence counter continuity
117    /// - Data ID (if applicable)
118    ///
119    /// # Arguments
120    /// * `data` - Reference to the received data buffer
121    ///
122    /// # Returns
123    /// * `Ok(E2EStatus)` indicating the check result
124    /// * `Err(E2EError)` if an error occurred during checking
125    fn check(&mut self, data: &[u8]) -> E2EResult<E2EStatus>;
126}
127
128#[cfg(test)]
129mod tests {
130    use super::*;
131
132    #[test]
133    fn test_e2e_status() {
134        assert_eq!(E2EStatus::Ok, E2EStatus::Ok);
135        assert_ne!(E2EStatus::Ok, E2EStatus::CrcError);
136    }
137}