canlink-hal 0.3.3

Hardware abstraction layer for CAN bus interfaces
Documentation
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! Error types for the CAN hardware abstraction layer.
//!
//! This module provides unified error types that are hardware-independent.
//! All backends use these error types for consistent error handling.

use thiserror::Error;

/// Unified CAN error type.
///
/// This enum represents all possible errors that can occur when working with
/// CAN hardware through the abstraction layer. Error codes are organized into
/// ranges:
///
/// - 1000-1999: Hardware-related errors
/// - 2000-2999: Protocol-related errors
/// - 3000-3999: Configuration-related errors
/// - 4000-4999: System-related errors
///
/// # Examples
///
/// ```
/// use canlink_hal::CanError;
///
/// let err = CanError::InvalidId { value: 0x800, max: 0x7FF };
/// assert!(matches!(err, CanError::InvalidId { .. }));
/// ```
#[derive(Error, Debug)]
pub enum CanError {
    // Hardware-related errors (1000-1999)
    /// Backend not found (1001)
    #[error("[1001] Backend not found: {name}")]
    BackendNotFound {
        /// Backend name that was not found
        name: String,
    },

    /// Backend already registered (1002)
    #[error("[1002] Backend '{name}' is already registered")]
    BackendAlreadyRegistered {
        /// Backend name that is already registered
        name: String,
    },

    /// Backend initialization failed (1003)
    #[error("[1003] Backend initialization failed: {reason}")]
    InitializationFailed {
        /// Reason for initialization failure
        reason: String,
    },

    /// Device not found (1004)
    #[error("[1004] Device not found: {device}")]
    DeviceNotFound {
        /// Device identifier
        device: String,
    },

    /// Channel not found (1005)
    #[error("[1005] Channel {channel} does not exist (max: {max})")]
    ChannelNotFound {
        /// Channel number that was requested
        channel: u8,
        /// Maximum channel number available
        max: u8,
    },

    /// Channel already open (1006)
    #[error("[1006] Channel {channel} is already open")]
    ChannelAlreadyOpen {
        /// Channel number
        channel: u8,
    },

    /// Channel not open (1007)
    #[error("[1007] Channel {channel} is not open")]
    ChannelNotOpen {
        /// Channel number
        channel: u8,
    },

    // Protocol-related errors (2000-2999)
    /// Invalid CAN ID (2001)
    #[error("[2001] Invalid CAN ID: {value:#X} (max: {max:#X})")]
    InvalidId {
        /// The invalid ID value
        value: u32,
        /// Maximum allowed ID value
        max: u32,
    },

    /// Invalid data length (2002)
    #[error("[2002] Invalid data length: expected max {expected}, got {actual}")]
    InvalidDataLength {
        /// Expected maximum length
        expected: usize,
        /// Actual length provided
        actual: usize,
    },

    /// Invalid message format (2003)
    #[error("[2003] Invalid message format: {reason}")]
    InvalidFormat {
        /// Reason for format error
        reason: String,
    },

    // Configuration-related errors (3000-3999)
    /// Configuration error (3001)
    #[error("[3001] Configuration error: {reason}")]
    ConfigError {
        /// Reason for configuration error
        reason: String,
    },

    /// Invalid parameter (3002)
    #[error("[3002] Invalid parameter '{parameter}': {reason}")]
    InvalidParameter {
        /// Parameter name
        parameter: String,
        /// Reason why parameter is invalid
        reason: String,
    },

    /// Version incompatible (3003)
    #[error("[3003] Version incompatible: backend {backend_version}, expected {expected_version}")]
    VersionIncompatible {
        /// Backend version
        backend_version: String,
        /// Expected version
        expected_version: String,
    },

    // System-related errors (4000-4999)
    /// Operation timed out (4001)
    #[error("[4001] Operation timed out after {timeout_ms}ms")]
    Timeout {
        /// Timeout duration in milliseconds
        timeout_ms: u64,
    },

    /// Insufficient resources (4002)
    #[error("[4002] Insufficient resources: {resource}")]
    InsufficientResources {
        /// Resource that is insufficient
        resource: String,
    },

    /// Permission denied (4003)
    #[error("[4003] Permission denied: {operation}")]
    PermissionDenied {
        /// Operation that was denied
        operation: String,
    },

    // Operation errors
    /// Send operation failed
    #[error("Send failed: {reason}")]
    SendFailed {
        /// Reason for send failure
        reason: String,
    },

    /// Receive operation failed
    #[error("Receive failed: {reason}")]
    ReceiveFailed {
        /// Reason for receive failure
        reason: String,
    },

    /// Bus error occurred
    #[error("Bus error: {kind:?}")]
    BusError {
        /// Type of bus error
        kind: BusErrorKind,
    },

    /// Feature not supported by hardware
    #[error("Unsupported feature: {feature}")]
    UnsupportedFeature {
        /// Feature that is not supported
        feature: String,
    },

    /// Backend is in wrong state for operation
    #[error("Invalid state: expected {expected}, current {current}")]
    InvalidState {
        /// Expected state
        expected: String,
        /// Current state
        current: String,
    },

    /// Other error
    #[error("Other error: {message}")]
    Other {
        /// Error message
        message: String,
    },
}

/// Bus error types.
///
/// These represent various types of errors that can occur on the CAN bus
/// at the physical and data link layers.
///
/// # Examples
///
/// ```
/// use canlink_hal::BusErrorKind;
///
/// let error = BusErrorKind::BitError;
/// assert_eq!(format!("{:?}", error), "BitError");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BusErrorKind {
    /// Bit error - transmitted bit differs from monitored bit
    BitError,

    /// Stuff error - more than 5 consecutive bits of same value
    StuffError,

    /// CRC error - calculated CRC differs from received CRC
    CrcError,

    /// ACK error - no acknowledgment received
    AckError,

    /// Form error - fixed-form bit field contains illegal value
    FormError,

    /// Bus-off - error counter exceeded threshold
    BusOff,

    /// Error passive - error counter in passive range
    ErrorPassive,

    /// Error warning - error counter in warning range
    ErrorWarning,
}

impl BusErrorKind {
    /// Get a human-readable description of the error.
    ///
    /// # Examples
    ///
    /// ```
    /// use canlink_hal::BusErrorKind;
    ///
    /// let error = BusErrorKind::BitError;
    /// assert_eq!(error.description(), "Bit error");
    /// ```
    #[must_use]
    pub fn description(&self) -> &'static str {
        match self {
            Self::BitError => "Bit error",
            Self::StuffError => "Stuff error",
            Self::CrcError => "CRC error",
            Self::AckError => "ACK error",
            Self::FormError => "Form error",
            Self::BusOff => "Bus-off",
            Self::ErrorPassive => "Error passive",
            Self::ErrorWarning => "Error warning",
        }
    }
}

/// Result type alias for CAN operations.
///
/// This is a convenience alias for `Result<T, CanError>`.
///
/// # Examples
///
/// ```
/// use canlink_hal::{CanResult, CanMessage};
///
/// fn send_message(msg: &CanMessage) -> CanResult<()> {
///     // Implementation
///     Ok(())
/// }
/// ```
pub type CanResult<T> = Result<T, CanError>;

// ============================================================================
// Filter Errors (FR-005 to FR-009)
// ============================================================================

/// Filter-related errors
///
/// These errors occur during message filter operations.
#[derive(Error, Debug)]
pub enum FilterError {
    /// Invalid filter configuration
    #[error("Invalid filter configuration: {reason}")]
    InvalidConfig {
        /// Reason for invalid configuration
        reason: String,
    },

    /// Filter ID out of range
    #[error("Filter ID {id:#X} out of range (max: {max:#X})")]
    IdOutOfRange {
        /// The invalid ID
        id: u32,
        /// Maximum allowed ID
        max: u32,
    },

    /// Invalid ID range (start > end)
    #[error("Invalid ID range: start {start:#X} > end {end:#X}")]
    InvalidRange {
        /// Start ID
        start: u32,
        /// End ID
        end: u32,
    },

    /// Hardware filter limit exceeded
    #[error("Hardware filter limit exceeded: max {max}, requested {requested}")]
    HardwareFilterLimitExceeded {
        /// Maximum hardware filters supported
        max: usize,
        /// Number of filters requested
        requested: usize,
    },

    /// Filter not found
    #[error("Filter not found at index {index}")]
    FilterNotFound {
        /// Index that was not found
        index: usize,
    },
}

/// Result type alias for filter operations
pub type FilterResult<T> = Result<T, FilterError>;

// ============================================================================
// Queue Errors (FR-011, FR-017)
// ============================================================================

/// Queue-related errors
///
/// These errors occur during message queue operations.
#[derive(Error, Debug)]
pub enum QueueError {
    /// Queue is full (Block policy timeout)
    #[error("Queue full: capacity {capacity}")]
    QueueFull {
        /// Queue capacity
        capacity: usize,
    },

    /// Message was dropped due to overflow policy
    #[error("Message dropped (ID: {id:#X}): {reason}")]
    MessageDropped {
        /// ID of the dropped message
        id: u32,
        /// Reason for dropping
        reason: String,
    },

    /// Invalid queue capacity
    #[error("Invalid queue capacity: {capacity} (min: 1)")]
    InvalidCapacity {
        /// The invalid capacity value
        capacity: usize,
    },

    /// Queue operation timeout
    #[error("Queue operation timed out after {timeout_ms}ms")]
    Timeout {
        /// Timeout in milliseconds
        timeout_ms: u64,
    },
}

/// Result type alias for queue operations
pub type QueueResult<T> = Result<T, QueueError>;

// ============================================================================
// Monitor Errors (FR-010)
// ============================================================================

/// Monitor-related errors
///
/// These errors occur during connection monitoring operations.
#[derive(Error, Debug)]
pub enum MonitorError {
    /// Reconnection failed
    #[error("Reconnect failed: {reason}")]
    ReconnectFailed {
        /// Reason for failure
        reason: String,
    },

    /// Monitor not started
    #[error("Monitor not started")]
    NotStarted,

    /// Monitor already running
    #[error("Monitor already running")]
    AlreadyRunning,

    /// Backend error during monitoring
    #[error("Backend error: {0}")]
    BackendError(#[from] CanError),

    /// Invalid monitor configuration
    #[error("Invalid monitor configuration: {reason}")]
    InvalidConfig {
        /// Reason for invalid configuration
        reason: String,
    },

    /// Heartbeat timeout
    #[error("Heartbeat timeout after {timeout_ms}ms")]
    HeartbeatTimeout {
        /// Timeout in milliseconds
        timeout_ms: u64,
    },
}

/// Result type alias for monitor operations
pub type MonitorResult<T> = Result<T, MonitorError>;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_error_display() {
        let err = CanError::InvalidId {
            value: 0x800,
            max: 0x7FF,
        };
        let msg = format!("{err}");
        assert!(msg.contains("0x800"));
        assert!(msg.contains("0x7FF"));
    }

    #[test]
    fn test_bus_error_kind() {
        let error = BusErrorKind::BitError;
        assert_eq!(error.description(), "Bit error");
    }

    #[test]
    fn test_error_codes() {
        let err = CanError::BackendNotFound {
            name: "test".to_string(),
        };
        let msg = format!("{err}");
        assert!(msg.contains("[1001]"));
    }
}