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;
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}
63
64/// E2E Error types
65#[derive(Debug, Clone, Error, PartialEq, Eq)]
66pub enum E2EError {
67 /// Invalid configuration provided
68 #[error("Invalid configuration: {0}")]
69 InvalidConfiguration(String),
70
71 /// Invalid data format
72 #[error("Invalid data format: {0}")]
73 InvalidDataFormat(String),
74
75 /// Profile-specific error
76 #[error("Profile-specific error: {0}")]
77 ProfileSpecificError(String),
78}
79
80// Main trait for E2E Profile implementations
81///
82/// This trait defines the common interface that all E2E profiles must implement.
83/// Each profile provides three main operations:
84/// - `protect`: Add E2E protection to data
85/// - `check`: Verify E2E protection on received data
86/// - `forward`: Forward protected data (Profile 11 specific)
87pub trait E2EProfile {
88 /// Configuration type for this profile
89 type Config;
90
91 /// Create a new instance with the given configuration
92 fn new(config: Self::Config) -> Self;
93
94 /// Add E2E protection to the given data buffer
95 ///
96 /// This function modifies the data buffer in-place by adding:
97 /// - CRC checksum
98 /// - Sequence counter
99 /// - Data ID (if applicable)
100 ///
101 /// # Arguments
102 /// * `data` - Mutable reference to the data buffer to protect
103 ///
104 /// # Returns
105 /// * `Ok(())` if protection was successfully added
106 /// * `Err(E2EError)` if an error occurred
107 fn protect(&mut self, data: &mut [u8]) -> E2EResult<()>;
108
109 /// Check E2E protection on received data
110 ///
111 /// This function verifies the integrity of the received data by checking:
112 /// - CRC checksum
113 /// - Sequence counter continuity
114 /// - Data ID (if applicable)
115 ///
116 /// # Arguments
117 /// * `data` - Reference to the received data buffer
118 ///
119 /// # Returns
120 /// * `Ok(E2EStatus)` indicating the check result
121 /// * `Err(E2EError)` if an error occurred during checking
122 fn check(&mut self, data: &[u8]) -> E2EResult<E2EStatus>;
123}
124
125#[cfg(test)]
126mod tests {
127 use super::*;
128
129 #[test]
130 fn test_e2e_status() {
131 assert_eq!(E2EStatus::Ok, E2EStatus::Ok);
132 assert_ne!(E2EStatus::Ok, E2EStatus::CrcError);
133 }
134}