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