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