1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! # `CANLink` Hardware Abstraction Layer
//!
//! This crate provides a unified hardware abstraction layer for CAN bus interfaces.
//! It allows applications to work with different CAN hardware backends through a
//! common trait-based interface.
//!
//! ## Features
//!
//! - **Unified Interface**: Single trait ([`CanBackend`]) for all hardware backends
//! - **Backend Registry**: Runtime registration and discovery of hardware backends
//! - **Hardware Capabilities**: Query hardware features at runtime
//! - **Zero-Cost Abstraction**: Minimal performance overhead (< 5%)
//! - **Type Safety**: Compile-time guarantees through Rust's type system
//! - **Async Support**: Optional async API through feature flags
//!
//! ## Quick Start
//!
//! ```rust,ignore
//! use canlink_hal::{BackendConfig, CanBackend, CanMessage, CanId};
//! use canlink_mock::MockBackend;
//!
//! // Create and initialize backend
//! let mut backend = MockBackend::new();
//! let config = BackendConfig::new("mock");
//! backend.initialize(&config)?;
//!
//! // Open a channel
//! backend.open_channel(0)?;
//!
//! // Send a message
//! let msg = CanMessage::new_standard(0x123, &[1, 2, 3, 4])?;
//! backend.send_message(&msg)?;
//!
//! // Receive a message
//! if let Some(msg) = backend.receive_message()? {
//! println!("Received: ID={:?}, Data={:?}", msg.id(), msg.data());
//! }
//!
//! // Clean up
//! backend.close_channel(0)?;
//! backend.close()?;
//! ```
//!
//! ## Architecture
//!
//! The abstraction layer consists of several key components:
//!
//! ### Core Traits
//!
//! - **[`CanBackend`]**: Core interface that all backends must implement
//! - `initialize()` - Initialize the backend
//! - `send_message()` - Send a CAN message
//! - `receive_message()` - Receive a CAN message
//! - `open_channel()` / `close_channel()` - Channel management
//! - `get_capability()` - Query hardware capabilities
//!
//! - **[`BackendFactory`]**: Factory trait for creating backend instances
//!
//! ### Key Types
//!
//! - **[`CanMessage`]**: Unified CAN message representation
//! - Supports standard (11-bit) and extended (29-bit) IDs
//! - Supports CAN 2.0 and CAN-FD frames
//! - Supports remote frames
//!
//! - **[`CanId`]**: CAN identifier (standard or extended)
//!
//! - **[`HardwareCapability`]**: Hardware feature description
//! - Channel count
//! - CAN-FD support
//! - Supported bitrates
//! - Hardware filter count
//! - Timestamp precision
//!
//! - **[`BackendRegistry`]**: Manages backend registration and instantiation
//!
//! - **[`CanError`]**: Common error types across all backends
//!
//! ## Usage Examples
//!
//! ### Basic Message Sending
//!
//! ```rust,ignore
//! use canlink_hal::{CanBackend, CanMessage};
//! use canlink_mock::MockBackend;
//!
//! let mut backend = MockBackend::new();
//! backend.initialize(&config)?;
//! backend.open_channel(0)?;
//!
//! // Standard CAN message
//! let msg = CanMessage::new_standard(0x123, &[0xAA, 0xBB, 0xCC, 0xDD])?;
//! backend.send_message(&msg)?;
//!
//! // Extended ID message
//! let msg = CanMessage::new_extended(0x12345678, &[1, 2, 3, 4, 5, 6, 7, 8])?;
//! backend.send_message(&msg)?;
//!
//! // CAN-FD message (if supported)
//! let data = vec![0; 64]; // Up to 64 bytes
//! let msg = CanMessage::new_fd(CanId::Standard(0x200), &data)?;
//! backend.send_message(&msg)?;
//! ```
//!
//! ### Capability-Based Adaptation
//!
//! ```rust,ignore
//! use canlink_hal::{CanBackend, CanMessage, CanId};
//!
//! let capability = backend.get_capability()?;
//!
//! // Adapt message type based on hardware support
//! let data = vec![0; 12];
//! let msg = if capability.supports_canfd {
//! CanMessage::new_fd(CanId::Standard(0x123), &data)?
//! } else {
//! // Split into multiple CAN 2.0 messages
//! CanMessage::new_standard(0x123, &data[..8])?
//! };
//!
//! // Check bitrate support
//! if capability.supports_bitrate(1_000_000) {
//! println!("1 Mbps is supported");
//! }
//!
//! // Check channel availability
//! if capability.has_channel(2) {
//! backend.open_channel(2)?;
//! }
//! ```
//!
//! ### Error Handling
//!
//! ```rust,ignore
//! use canlink_hal::{CanError, CanBackend};
//!
//! match backend.send_message(&msg) {
//! Ok(_) => println!("Message sent successfully"),
//! Err(CanError::SendFailed { reason }) => {
//! eprintln!("Send failed: {}", reason);
//! }
//! Err(CanError::BusError { kind }) => {
//! eprintln!("Bus error: {:?}", kind);
//! }
//! Err(e) => eprintln!("Other error: {}", e),
//! }
//! ```
//!
//! ### Backend Registry
//!
//! ```rust,ignore
//! use canlink_hal::{BackendRegistry, BackendConfig};
//! use canlink_mock::MockBackendFactory;
//! use std::sync::Arc;
//!
//! // Get global registry
//! let registry = BackendRegistry::global();
//!
//! // Register a backend
//! let factory = Arc::new(MockBackendFactory::new());
//! registry.register(factory)?;
//!
//! // List available backends
//! for name in registry.list_backends() {
//! println!("Available backend: {}", name);
//! }
//!
//! // Create backend instance
//! let config = BackendConfig::new("mock");
//! let mut backend = registry.create("mock", &config)?;
//! ```
//!
//! ## Testing
//!
//! The crate includes a mock backend for testing without hardware:
//!
//! ```rust,ignore
//! use canlink_hal::{CanBackend, CanMessage, CanId};
//! use canlink_mock::MockBackend;
//!
//! #[test]
//! fn test_can_communication() {
//! let mut backend = MockBackend::new();
//! backend.initialize(&config).unwrap();
//! backend.open_channel(0).unwrap();
//!
//! // Send message
//! let msg = CanMessage::new_standard(0x123, &[1, 2, 3]).unwrap();
//! backend.send_message(&msg).unwrap();
//!
//! // Verify message was sent
//! assert!(backend.verify_message_sent(CanId::Standard(0x123)));
//! }
//! ```
//!
//! ## Feature Flags
//!
//! - `async` - Enable async API support
//! - `async-tokio` - Use tokio runtime for async operations
//! - `async-async-std` - Use async-std runtime for async operations
//! - `tracing` - Enable logging support via tracing framework
//! - `hot-reload` - Enable configuration hot-reload support
//! - `full` - Enable all features
//!
//! ## Performance
//!
//! The abstraction layer is designed for minimal overhead:
//!
//! - Trait method calls are typically inlined
//! - Zero-copy message passing where possible
//! - Abstraction overhead < 5% compared to direct hardware access
//!
//! ## Thread Safety
//!
//! Backend implementations follow an external synchronization model:
//!
//! - Backends are `Send` but not `Sync`
//! - Each thread should have its own backend instance
//! - Use channels or locks for cross-thread communication
//!
//! ## Supported Backends
//!
//! - **Mock**: Software simulation for testing (included)
//! - **`SocketCAN`**: Linux CAN interface (planned)
//! - **PCAN**: PEAK-System CAN adapters (planned)
//! - **IXXAT**: HMS IXXAT CAN interfaces (planned)
//! - **Kvaser**: Kvaser CAN devices (planned)
//!
//! ## See Also
//!
//! - [`canlink-mock`](https://docs.rs/canlink-mock) - Mock backend for testing
//! - [`canlink-cli`](https://docs.rs/canlink-cli) - Command-line interface
//! - Examples: see the workspace `examples/` directory
//!
// Core modules
// New modules introduced in v0.2.0 (003-async-and-filtering)
// Conditional modules
// Periodic message sending (004 spec FR-001 to FR-006)
// ISO-TP protocol support (004 spec FR-007 to FR-019)
// Resource management documentation
// Re-exports
pub use CanBackendAsync;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use BackendState;
pub use BackendVersion;
// Periodic message sending re-exports (004 spec)
pub use ;
// ISO-TP protocol re-exports (004 spec)
pub use ;