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