canlink-hal 0.3.2

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
//! Bounded queue implementation (FR-011, FR-017)
//!
//! Provides a bounded message queue with configurable overflow policies.

use std::collections::VecDeque;
use std::sync::atomic::{AtomicU64, Ordering};

use crate::error::QueueError;
use crate::message::CanMessage;

use super::QueueOverflowPolicy;

/// Default queue capacity (FR-017)
pub const DEFAULT_QUEUE_CAPACITY: usize = 1000;

/// Queue statistics
///
/// Tracks queue operations for monitoring and debugging.
#[derive(Debug, Clone, Default)]
pub struct QueueStats {
    /// Total messages enqueued
    pub enqueued: u64,
    /// Total messages dequeued
    pub dequeued: u64,
    /// Total messages dropped due to overflow
    pub dropped: u64,
    /// Number of times the queue was full
    pub overflow_count: u64,
}

/// Internal atomic stats for thread-safe updates
struct AtomicQueueStats {
    enqueued: AtomicU64,
    dequeued: AtomicU64,
    dropped: AtomicU64,
    overflow_count: AtomicU64,
}

impl AtomicQueueStats {
    fn new() -> Self {
        Self {
            enqueued: AtomicU64::new(0),
            dequeued: AtomicU64::new(0),
            dropped: AtomicU64::new(0),
            overflow_count: AtomicU64::new(0),
        }
    }

    fn snapshot(&self) -> QueueStats {
        QueueStats {
            enqueued: self.enqueued.load(Ordering::Relaxed),
            dequeued: self.dequeued.load(Ordering::Relaxed),
            dropped: self.dropped.load(Ordering::Relaxed),
            overflow_count: self.overflow_count.load(Ordering::Relaxed),
        }
    }

    fn inc_enqueued(&self) {
        self.enqueued.fetch_add(1, Ordering::Relaxed);
    }

    fn inc_dequeued(&self) {
        self.dequeued.fetch_add(1, Ordering::Relaxed);
    }

    fn inc_dropped(&self) {
        self.dropped.fetch_add(1, Ordering::Relaxed);
    }

    fn inc_overflow(&self) {
        self.overflow_count.fetch_add(1, Ordering::Relaxed);
    }
}

/// Bounded message queue
///
/// A queue with a fixed maximum capacity and configurable overflow policy.
///
/// # Example
///
/// ```rust
/// use canlink_hal::queue::{BoundedQueue, QueueOverflowPolicy};
/// use canlink_hal::message::CanMessage;
///
/// // Create queue with default policy (DropOldest)
/// let mut queue = BoundedQueue::new(100);
///
/// // Create queue with custom policy
/// let mut queue = BoundedQueue::with_policy(100, QueueOverflowPolicy::DropNewest);
/// ```
pub struct BoundedQueue {
    buffer: VecDeque<CanMessage>,
    capacity: usize,
    policy: QueueOverflowPolicy,
    stats: AtomicQueueStats,
}

impl BoundedQueue {
    /// Create a new bounded queue with default overflow policy
    ///
    /// # Arguments
    ///
    /// * `capacity` - Maximum number of messages the queue can hold
    ///
    /// # Example
    ///
    /// ```rust
    /// use canlink_hal::queue::BoundedQueue;
    ///
    /// let queue = BoundedQueue::new(1000);
    /// assert_eq!(queue.capacity(), 1000);
    /// ```
    #[must_use]
    pub fn new(capacity: usize) -> Self {
        Self::with_policy(capacity, QueueOverflowPolicy::default())
    }

    /// Create a new bounded queue with specified overflow policy
    ///
    /// # Arguments
    ///
    /// * `capacity` - Maximum number of messages the queue can hold
    /// * `policy` - Overflow handling policy
    #[must_use]
    pub fn with_policy(capacity: usize, policy: QueueOverflowPolicy) -> Self {
        Self {
            buffer: VecDeque::with_capacity(capacity),
            capacity,
            policy,
            stats: AtomicQueueStats::new(),
        }
    }

    /// Get the queue capacity
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Get the current number of messages in the queue
    pub fn len(&self) -> usize {
        self.buffer.len()
    }

    /// Check if the queue is empty
    pub fn is_empty(&self) -> bool {
        self.buffer.is_empty()
    }

    /// Check if the queue is full
    pub fn is_full(&self) -> bool {
        self.buffer.len() >= self.capacity
    }

    /// Get the overflow policy
    pub fn policy(&self) -> QueueOverflowPolicy {
        self.policy
    }

    /// Get queue statistics
    pub fn stats(&self) -> QueueStats {
        self.stats.snapshot()
    }

    /// Push a message to the queue
    ///
    /// Behavior depends on the overflow policy:
    /// - `DropOldest`: Removes the oldest message if full
    /// - `DropNewest`: Rejects the new message if full
    /// - `Block`: Returns `QueueError::QueueFull` (async version handles blocking)
    ///
    /// # Errors
    ///
    /// - Returns `QueueError::QueueFull` if using `Block` policy and queue is full
    /// - Returns `QueueError::MessageDropped` if using `DropNewest` policy and queue is full
    pub fn push(&mut self, message: CanMessage) -> Result<(), QueueError> {
        if self.is_full() {
            self.stats.inc_overflow();

            match self.policy {
                QueueOverflowPolicy::DropOldest => {
                    // Remove oldest message
                    #[allow(unused_variables)]
                    if let Some(dropped) = self.buffer.pop_front() {
                        self.stats.inc_dropped();
                        #[cfg(feature = "tracing")]
                        crate::log_queue_overflow!(self.policy, dropped.id().raw());
                    }
                    // Continue to add new message
                }
                QueueOverflowPolicy::DropNewest => {
                    self.stats.inc_dropped();
                    #[cfg(feature = "tracing")]
                    crate::log_queue_overflow!(self.policy, message.id().raw());
                    return Err(QueueError::MessageDropped {
                        id: message.id().raw(),
                        reason: "Queue full, DropNewest policy".to_string(),
                    });
                }
                QueueOverflowPolicy::Block { .. } => {
                    // Synchronous push cannot block, return error
                    return Err(QueueError::QueueFull {
                        capacity: self.capacity,
                    });
                }
            }
        }

        self.buffer.push_back(message);
        self.stats.inc_enqueued();
        Ok(())
    }

    /// Pop a message from the queue
    ///
    /// Returns `None` if the queue is empty.
    pub fn pop(&mut self) -> Option<CanMessage> {
        let msg = self.buffer.pop_front();
        if msg.is_some() {
            self.stats.inc_dequeued();
        }
        msg
    }

    /// Peek at the next message without removing it
    pub fn peek(&self) -> Option<&CanMessage> {
        self.buffer.front()
    }

    /// Clear all messages from the queue
    pub fn clear(&mut self) {
        self.buffer.clear();
    }

    /// Adjust the queue capacity
    ///
    /// If the new capacity is smaller than the current number of messages,
    /// excess messages are removed according to the overflow policy.
    ///
    /// # Arguments
    ///
    /// * `new_capacity` - New maximum capacity
    pub fn adjust_capacity(&mut self, new_capacity: usize) {
        while self.buffer.len() > new_capacity {
            match self.policy {
                QueueOverflowPolicy::DropOldest | QueueOverflowPolicy::Block { .. } => {
                    if self.buffer.pop_front().is_some() {
                        self.stats.inc_dropped();
                    }
                }
                QueueOverflowPolicy::DropNewest => {
                    if self.buffer.pop_back().is_some() {
                        self.stats.inc_dropped();
                    }
                }
            }
        }
        self.capacity = new_capacity;
    }

    /// Iterate over messages without removing them
    pub fn iter(&self) -> impl Iterator<Item = &CanMessage> {
        self.buffer.iter()
    }
}

impl Default for BoundedQueue {
    fn default() -> Self {
        Self::new(DEFAULT_QUEUE_CAPACITY)
    }
}

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

    fn make_test_message(id: u16) -> CanMessage {
        CanMessage::new_standard(id, &[0u8; 8]).unwrap()
    }

    #[test]
    fn test_new_queue() {
        let queue = BoundedQueue::new(100);
        assert_eq!(queue.capacity(), 100);
        assert!(queue.is_empty());
        assert!(!queue.is_full());
    }

    #[test]
    fn test_push_pop() {
        let mut queue = BoundedQueue::new(10);
        let msg = make_test_message(0x123);

        assert!(queue.push(msg.clone()).is_ok());
        assert_eq!(queue.len(), 1);

        let popped = queue.pop();
        assert!(popped.is_some());
        assert_eq!(popped.unwrap().id(), CanId::Standard(0x123));
        assert!(queue.is_empty());
    }

    #[test]
    fn test_drop_oldest_policy() {
        let mut queue = BoundedQueue::with_policy(3, QueueOverflowPolicy::DropOldest);

        // Fill the queue
        queue.push(make_test_message(1)).unwrap();
        queue.push(make_test_message(2)).unwrap();
        queue.push(make_test_message(3)).unwrap();
        assert!(queue.is_full());

        // Push one more - should drop oldest (1)
        queue.push(make_test_message(4)).unwrap();
        assert_eq!(queue.len(), 3);

        // Verify oldest was dropped
        assert_eq!(queue.pop().unwrap().id(), CanId::Standard(2));
        assert_eq!(queue.pop().unwrap().id(), CanId::Standard(3));
        assert_eq!(queue.pop().unwrap().id(), CanId::Standard(4));

        let stats = queue.stats();
        assert_eq!(stats.dropped, 1);
        assert_eq!(stats.overflow_count, 1);
    }

    #[test]
    fn test_drop_newest_policy() {
        let mut queue = BoundedQueue::with_policy(3, QueueOverflowPolicy::DropNewest);

        // Fill the queue
        queue.push(make_test_message(1)).unwrap();
        queue.push(make_test_message(2)).unwrap();
        queue.push(make_test_message(3)).unwrap();

        // Push one more - should reject it
        let result = queue.push(make_test_message(4));
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            QueueError::MessageDropped { .. }
        ));

        // Verify queue unchanged
        assert_eq!(queue.pop().unwrap().id(), CanId::Standard(1));
        assert_eq!(queue.pop().unwrap().id(), CanId::Standard(2));
        assert_eq!(queue.pop().unwrap().id(), CanId::Standard(3));
    }

    #[test]
    fn test_block_policy_sync() {
        use std::time::Duration;

        let mut queue = BoundedQueue::with_policy(
            2,
            QueueOverflowPolicy::Block {
                timeout: Duration::from_millis(100),
            },
        );

        queue.push(make_test_message(1)).unwrap();
        queue.push(make_test_message(2)).unwrap();

        // Sync push should return QueueFull error
        let result = queue.push(make_test_message(3));
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), QueueError::QueueFull { .. }));
    }

    #[test]
    fn test_adjust_capacity() {
        let mut queue = BoundedQueue::new(10);

        // Add 5 messages
        for i in 0..5u16 {
            queue.push(make_test_message(i)).unwrap();
        }

        // Reduce capacity to 3
        queue.adjust_capacity(3);
        assert_eq!(queue.capacity(), 3);
        assert_eq!(queue.len(), 3);

        // With DropOldest, oldest messages should be removed
        assert_eq!(queue.pop().unwrap().id(), CanId::Standard(2));
        assert_eq!(queue.pop().unwrap().id(), CanId::Standard(3));
        assert_eq!(queue.pop().unwrap().id(), CanId::Standard(4));
    }

    #[test]
    fn test_stats() {
        let mut queue = BoundedQueue::with_policy(2, QueueOverflowPolicy::DropOldest);

        queue.push(make_test_message(1)).unwrap();
        queue.push(make_test_message(2)).unwrap();
        queue.push(make_test_message(3)).unwrap(); // Causes overflow
        queue.pop();

        let stats = queue.stats();
        assert_eq!(stats.enqueued, 3);
        assert_eq!(stats.dequeued, 1);
        assert_eq!(stats.dropped, 1);
        assert_eq!(stats.overflow_count, 1);
    }

    #[test]
    fn test_default_queue() {
        let queue = BoundedQueue::default();
        assert_eq!(queue.capacity(), DEFAULT_QUEUE_CAPACITY);
    }
}