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