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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
//! Backend trait definitions and factory pattern.
//!
//! This module defines the core `CanBackend` trait that all hardware backends must implement,
//! as well as the `BackendFactory` trait for creating backend instances.

use crate::{BackendConfig, BackendVersion, CanError, CanMessage, CanResult, HardwareCapability};
use std::time::Duration;

/// CAN hardware backend interface.
///
/// This trait defines the unified interface that all hardware backends must implement.
/// It provides methods for lifecycle management, message transmission/reception,
/// channel management, and capability querying.
///
/// # Thread Safety
///
/// This trait's methods require external synchronization. If you need to access the same
/// backend instance from multiple threads, the caller must provide synchronization using
/// `Mutex` or `RwLock`.
///
/// **Rationale**: External synchronization allows high-performance single-threaded usage
/// without lock overhead, while still supporting multi-threaded scenarios when needed.
///
/// ## Single-Threaded Usage (No Synchronization Needed)
///
/// ```rust,ignore
/// use canlink_hal::{CanBackend, BackendConfig};
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let mut backend = create_backend();
///     backend.initialize(&config)?;
///     backend.open_channel(0)?;
///
///     // Direct access - no locks needed
///     backend.send_message(&message)?;
///     if let Some(msg) = backend.receive_message()? {
///         println!("Received: {:?}", msg);
///     }
///
///     backend.close()?;
///     Ok(())
/// }
/// ```
///
/// ## Multi-Threaded Usage with Mutex
///
/// Use `Arc<Mutex<>>` when multiple threads need mutable access:
///
/// ```rust,ignore
/// use std::sync::{Arc, Mutex};
/// use std::thread;
/// use canlink_hal::{CanBackend, CanMessage};
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let backend = Arc::new(Mutex::new(create_backend()));
///
///     // Initialize in main thread
///     backend.lock().unwrap().initialize(&config)?;
///     backend.lock().unwrap().open_channel(0)?;
///
///     // Sender thread
///     let backend_tx = Arc::clone(&backend);
///     let tx_handle = thread::spawn(move || {
///         for i in 0..100 {
///             let msg = CanMessage::new_standard(0x100 + i, &[i as u8]).unwrap();
///             backend_tx.lock().unwrap().send_message(&msg).unwrap();
///         }
///     });
///
///     // Receiver thread
///     let backend_rx = Arc::clone(&backend);
///     let rx_handle = thread::spawn(move || {
///         let mut count = 0;
///         while count < 100 {
///             if let Some(msg) = backend_rx.lock().unwrap().receive_message().unwrap() {
///                 println!("Received: {:?}", msg);
///                 count += 1;
///             }
///         }
///     });
///
///     tx_handle.join().unwrap();
///     rx_handle.join().unwrap();
///
///     backend.lock().unwrap().close()?;
///     Ok(())
/// }
/// ```
///
/// ## Multi-Threaded Usage with `RwLock`
///
/// Use `Arc<RwLock<>>` when you have many readers and few writers:
///
/// ```rust,ignore
/// use std::sync::{Arc, RwLock};
/// use std::thread;
/// use canlink_hal::CanBackend;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let backend = Arc::new(RwLock::new(create_backend()));
///
///     // Initialize
///     backend.write().unwrap().initialize(&config)?;
///     backend.write().unwrap().open_channel(0)?;
///
///     // Multiple reader threads querying capabilities
///     let mut handles = vec![];
///     for i in 0..10 {
///         let backend_clone = Arc::clone(&backend);
///         let handle = thread::spawn(move || {
///             // Read lock allows concurrent access
///             let capability = backend_clone.read().unwrap().get_capability().unwrap();
///             println!("Thread {}: {} channels", i, capability.channel_count);
///         });
///         handles.push(handle);
///     }
///
///     // Writer thread sending messages
///     let backend_writer = Arc::clone(&backend);
///     let writer_handle = thread::spawn(move || {
///         for i in 0..10 {
///             let msg = CanMessage::new_standard(0x200, &[i]).unwrap();
///             // Write lock for exclusive access
///             backend_writer.write().unwrap().send_message(&msg).unwrap();
///         }
///     });
///
///     for handle in handles {
///         handle.join().unwrap();
///     }
///     writer_handle.join().unwrap();
///
///     backend.write().unwrap().close()?;
///     Ok(())
/// }
/// ```
///
/// ## Channel-Based Message Passing Pattern
///
/// For better performance, consider using channels to decouple threads:
///
/// ```rust,ignore
/// use std::sync::mpsc;
/// use std::thread;
/// use canlink_hal::{CanBackend, CanMessage};
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let mut backend = create_backend();
///     backend.initialize(&config)?;
///     backend.open_channel(0)?;
///
///     let (tx, rx) = mpsc::channel();
///
///     // Worker threads send messages via channel
///     for i in 0..4 {
///         let tx_clone = tx.clone();
///         thread::spawn(move || {
///             for j in 0..25 {
///                 let msg = CanMessage::new_standard(
///                     0x100 + (i * 25 + j),
///                     &[i as u8, j as u8]
///                 ).unwrap();
///                 tx_clone.send(msg).unwrap();
///             }
///         });
///     }
///     drop(tx); // Close sender
///
///     // Main thread owns backend and sends messages
///     for msg in rx {
///         backend.send_message(&msg)?;
///     }
///
///     backend.close()?;
///     Ok(())
/// }
/// ```
///
/// ## Performance Considerations
///
/// - **Single-threaded**: Zero synchronization overhead
/// - **Mutex**: Simple but serializes all access
/// - **`RwLock`**: Better for read-heavy workloads (e.g., capability queries)
/// - **Channel-based**: Best performance, avoids lock contention
///
/// ## Thread Safety Guarantees
///
/// - `CanBackend` is `Send`, so it can be moved between threads
/// - Methods require `&mut self`, enforcing exclusive access
/// - Backends do not use internal locks (external synchronization model)
/// - All backend state is protected by the caller's synchronization primitive
///
/// # Lifecycle
///
/// Backend instances follow this lifecycle:
/// 1. **Create** (via `BackendFactory::create()`)
/// 2. **Initialize** (`initialize()`)
/// 3. **Run** (call `send_message()`, `receive_message()`, etc.)
/// 4. **Close** (`close()`)
///
/// # Examples
///
/// ```rust,ignore
/// use canlink_hal::{CanBackend, BackendConfig};
///
/// // Create and initialize backend
/// let mut backend = create_backend();
/// backend.initialize(&config)?;
///
/// // Use backend
/// backend.open_channel(0)?;
/// backend.send_message(&message)?;
///
/// // Clean up
/// backend.close()?;
/// ```
pub trait CanBackend: Send {
    // ========== Lifecycle Management ==========

    /// Initialize the backend.
    ///
    /// # Arguments
    ///
    /// * `config` - Backend configuration parameters
    ///
    /// # Errors
    ///
    /// * `CanError::InitializationFailed` - Hardware initialization failed
    /// * `CanError::ConfigError` - Configuration parameters are invalid
    ///
    /// # Preconditions
    ///
    /// * Backend is in `Uninitialized` state
    ///
    /// # Postconditions
    ///
    /// * Success: Backend is in `Ready` state
    /// * Failure: Backend remains in `Uninitialized` state
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let mut backend = create_backend();
    /// backend.initialize(&config)?;
    /// ```
    fn initialize(&mut self, config: &BackendConfig) -> CanResult<()>;

    /// Close the backend and release resources.
    ///
    /// This method releases all resources held by the backend, including hardware handles,
    /// memory buffers, and network connections. It should be called when the backend is
    /// no longer needed.
    ///
    /// # Errors
    ///
    /// Returns an error if closing fails, but resources will still be released on a
    /// best-effort basis.
    ///
    /// # Preconditions
    ///
    /// * Backend is in `Ready` state
    ///
    /// # Postconditions
    ///
    /// * Backend is in `Closed` state
    /// * All resources are released
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// backend.close()?;
    /// ```
    fn close(&mut self) -> CanResult<()>;

    // ========== Hardware Capability Query ==========

    /// Query hardware capabilities.
    ///
    /// Returns information about the hardware's capabilities, including supported
    /// channel count, CAN-FD support, maximum bitrate, and timestamp precision.
    ///
    /// Applications should query capabilities at startup to adapt their behavior
    /// to the available hardware features. This enables writing portable code that
    /// works across different CAN hardware backends.
    ///
    /// # Performance Requirements
    ///
    /// * Response time < 1ms (SC-004)
    /// * Should be cached by the backend for fast repeated queries
    ///
    /// # Use Cases
    ///
    /// * **Feature Detection**: Check if CAN-FD is supported before sending FD frames
    /// * **Channel Validation**: Verify channel numbers before opening
    /// * **Bitrate Selection**: Choose from supported bitrates
    /// * **Filter Planning**: Determine available hardware filters
    /// * **Timestamp Handling**: Adapt to available timestamp precision
    ///
    /// # Examples
    ///
    /// Basic capability query:
    ///
    /// ```rust,ignore
    /// let capability = backend.get_capability()?;
    /// println!("Hardware: {} channels, CAN-FD: {}",
    ///          capability.channel_count,
    ///          capability.supports_canfd);
    /// ```
    ///
    /// Adaptive message sending:
    ///
    /// ```rust,ignore
    /// let capability = backend.get_capability()?;
    ///
    /// // Use CAN-FD if available, otherwise fall back to CAN 2.0
    /// let message = if capability.supports_canfd {
    ///     CanMessage::new_canfd(0x123, &data, false)?
    /// } else {
    ///     CanMessage::new_standard(0x123, &data[..8])?
    /// };
    /// backend.send_message(&message)?;
    /// ```
    ///
    /// Bitrate validation:
    ///
    /// ```rust,ignore
    /// let capability = backend.get_capability()?;
    /// let desired_bitrate = 500_000;
    ///
    /// if !capability.supports_bitrate(desired_bitrate) {
    ///     eprintln!("Bitrate {} not supported", desired_bitrate);
    ///     eprintln!("Supported bitrates: {:?}", capability.supported_bitrates);
    ///     return Err(CanError::UnsupportedFeature("bitrate".into()));
    /// }
    /// ```
    ///
    /// Channel validation:
    ///
    /// ```rust,ignore
    /// let capability = backend.get_capability()?;
    /// let channel = 2;
    ///
    /// if !capability.has_channel(channel) {
    ///     return Err(CanError::InvalidChannel(channel));
    /// }
    /// backend.open_channel(channel)?;
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if the capability query fails (rare, typically only on
    /// uninitialized or closed backends).
    fn get_capability(&self) -> CanResult<HardwareCapability>;

    // ========== Message Transmission/Reception ==========

    /// Send a CAN message.
    ///
    /// Transmits a CAN message on the bus. The message must be valid and the hardware
    /// must support the message type (e.g., CAN-FD messages require CAN-FD support).
    ///
    /// # Arguments
    ///
    /// * `message` - The message to send
    ///
    /// # Errors
    ///
    /// * `CanError::SendFailed` - Send failed (e.g., bus Bus-Off)
    /// * `CanError::UnsupportedFeature` - Hardware doesn't support this message type
    /// * `CanError::InvalidDataLength` - Data length exceeds limits
    /// * `CanError::InvalidState` - Backend not in Ready state
    ///
    /// # Preconditions
    ///
    /// * Backend is in `Ready` state
    /// * Message format is valid
    /// * At least one channel is open
    ///
    /// # Postconditions
    ///
    /// * Success: Message is sent to the bus
    /// * Failure: Message is not sent, backend state unchanged
    ///
    /// # Performance Requirements
    ///
    /// * Support 1000 messages/second throughput
    /// * Abstraction layer overhead < 5% (SC-005)
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let msg = CanMessage::new_standard(0x123, &[0x01, 0x02, 0x03])?;
    /// backend.send_message(&msg)?;
    /// ```
    fn send_message(&mut self, message: &CanMessage) -> CanResult<()>;

    /// Receive a CAN message (non-blocking).
    ///
    /// Attempts to receive a message from the receive queue. Returns immediately
    /// with `None` if no messages are available.
    ///
    /// # Returns
    ///
    /// * `Ok(Some(message))` - A message was received
    /// * `Ok(None)` - No messages currently available
    /// * `Err(CanError)` - Reception failed
    ///
    /// # Errors
    ///
    /// * `CanError::ReceiveFailed` - Reception failed
    /// * `CanError::InvalidState` - Backend not in Ready state
    ///
    /// # Preconditions
    ///
    /// * Backend is in `Ready` state
    ///
    /// # Postconditions
    ///
    /// * Success: Returned message is removed from receive queue
    /// * Failure: Receive queue state unchanged
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// if let Some(msg) = backend.receive_message()? {
    ///     println!("Received: {:?}", msg);
    /// }
    /// ```
    fn receive_message(&mut self) -> CanResult<Option<CanMessage>>;

    // ========== Channel Management ==========

    /// Open a CAN channel.
    ///
    /// Opens the specified CAN channel for communication. The channel index must be
    /// valid (less than the channel count reported by `get_capability()`).
    ///
    /// # Arguments
    ///
    /// * `channel` - Channel index (0-based)
    ///
    /// # Errors
    ///
    /// * `CanError::ChannelNotFound` - Channel doesn't exist
    /// * `CanError::ChannelAlreadyOpen` - Channel is already open
    ///
    /// # Preconditions
    ///
    /// * Backend is in `Ready` state
    /// * Channel index is valid (< `capability.channel_count`)
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// backend.open_channel(0)?;
    /// ```
    fn open_channel(&mut self, channel: u8) -> CanResult<()>;

    /// Close a CAN channel.
    ///
    /// Closes the specified CAN channel and stops communication on that channel.
    ///
    /// # Arguments
    ///
    /// * `channel` - Channel index
    ///
    /// # Errors
    ///
    /// * `CanError::ChannelNotFound` - Channel doesn't exist
    /// * `CanError::ChannelNotOpen` - Channel is not open
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// backend.close_channel(0)?;
    /// ```
    fn close_channel(&mut self, channel: u8) -> CanResult<()>;

    // ========== Version Information ==========

    /// Get the backend version.
    ///
    /// Returns the semantic version number of the backend implementation.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let version = backend.version();
    /// println!("Backend version: {}", version);
    /// ```
    fn version(&self) -> BackendVersion;

    /// Get the backend name.
    ///
    /// Returns the unique identifier name of the backend (e.g., "`TSMaster`", "mock").
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let name = backend.name();
    /// println!("Using backend: {}", name);
    /// ```
    fn name(&self) -> &str;
}

/// Backend factory trait.
///
/// This trait defines the factory pattern for creating backend instances.
/// Each backend implementation should provide a factory that implements this trait.
///
/// # Examples
///
/// ```rust,ignore
/// use canlink_hal::{BackendFactory, BackendConfig};
///
/// struct MockBackendFactory;
///
/// impl BackendFactory for MockBackendFactory {
///     fn create(&self, config: &BackendConfig) -> CanResult<Box<dyn CanBackend>> {
///         Ok(Box::new(MockBackend::new()))
///     }
///
///     fn name(&self) -> &str {
///         "mock"
///     }
///
///     fn version(&self) -> BackendVersion {
///         BackendVersion::new(0, 1, 0)
///     }
/// }
/// ```
pub trait BackendFactory: Send + Sync {
    /// Create a new backend instance.
    ///
    /// # Arguments
    ///
    /// * `config` - Backend configuration
    ///
    /// # Returns
    ///
    /// A boxed backend instance ready for initialization.
    ///
    /// # Errors
    ///
    /// * `CanError::ConfigError` - Invalid configuration
    /// * `CanError::Other` - Factory-specific errors
    fn create(&self, config: &BackendConfig) -> CanResult<Box<dyn CanBackend>>;

    /// Get the factory name.
    ///
    /// Returns the unique identifier for this backend type.
    fn name(&self) -> &str;

    /// Get the factory version.
    ///
    /// Returns the version of the backend implementation.
    fn version(&self) -> BackendVersion;
}

/// Helper function to retry backend initialization.
///
/// This function implements the retry logic specified in FR-009. It attempts to
/// initialize a backend multiple times with a fixed interval between attempts.
///
/// # Arguments
///
/// * `backend` - The backend to initialize
/// * `config` - Backend configuration
/// * `retry_count` - Number of retry attempts (default: 3)
/// * `retry_interval` - Interval between retries in milliseconds (default: 1000)
///
/// # Returns
///
/// * `Ok(())` - Initialization succeeded
/// * `Err(CanError::InitializationFailed)` - All retry attempts failed
///
/// # Errors
///
/// Returns `CanError::InitializationFailed` if all retry attempts fail.
///
/// # Examples
///
/// ```rust,ignore
/// let mut backend = create_backend();
/// retry_initialize(&mut backend, &config, 3, 1000)?;
/// ```
pub fn retry_initialize(
    backend: &mut dyn CanBackend,
    config: &BackendConfig,
    retry_count: u32,
    retry_interval_ms: u64,
) -> CanResult<()> {
    let mut errors = Vec::new();
    let start_time = std::time::Instant::now();

    for attempt in 0..=retry_count {
        match backend.initialize(config) {
            Ok(()) => return Ok(()),
            Err(e) => {
                errors.push(format!("Attempt {}: {}", attempt + 1, e));
                if attempt < retry_count {
                    std::thread::sleep(Duration::from_millis(retry_interval_ms));
                }
            }
        }
    }

    let total_time = start_time.elapsed();
    Err(CanError::InitializationFailed {
        reason: format!(
            "Failed after {} attempts in {:?}. Errors: {}",
            retry_count + 1,
            total_time,
            errors.join("; ")
        ),
    })
}

/// Switch from one backend to another (FR-015).
///
/// This function performs a clean switch between backends:
/// 1. Closes the old backend (discarding any unprocessed messages)
/// 2. Initializes the new backend
///
/// **Important**: Any messages in the old backend's queue are discarded.
/// Users should process all pending messages before calling this function
/// if message preservation is required.
///
/// # Arguments
///
/// * `old_backend` - The currently active backend to close
/// * `new_backend` - The new backend to initialize
/// * `config` - Configuration for the new backend
///
/// # Errors
///
/// Returns an error if:
/// - The old backend fails to close (warning only, continues)
/// - The new backend fails to initialize
///
/// # Examples
///
/// ```rust,ignore
/// use canlink_hal::{switch_backend, BackendConfig};
///
/// // Process any remaining messages first
/// while let Some(msg) = old_backend.receive_message()? {
///     process_message(msg);
/// }
///
/// // Then switch backends
/// switch_backend(&mut old_backend, &mut new_backend, &new_config)?;
/// ```
pub fn switch_backend(
    old_backend: &mut dyn CanBackend,
    new_backend: &mut dyn CanBackend,
    config: &BackendConfig,
) -> CanResult<()> {
    // Get names upfront to avoid borrow issues (used for logging)
    #[allow(unused_variables)]
    let old_name = old_backend.name().to_string();
    #[allow(unused_variables)]
    let new_name = new_backend.name().to_string();

    // Log the switch (if tracing is enabled)
    #[cfg(feature = "tracing")]
    tracing::info!("Switching backend from '{}' to '{}'", old_name, new_name);

    // Close the old backend - ignore errors but log them
    #[allow(unused_variables)]
    if let Err(e) = old_backend.close() {
        #[cfg(feature = "tracing")]
        tracing::warn!("Error closing old backend '{}': {}", old_name, e);
        // Continue anyway - we want to switch even if close fails
    }

    // Initialize the new backend
    #[cfg(feature = "tracing")]
    {
        new_backend.initialize(config).map_err(|e| {
            tracing::error!("Failed to initialize new backend '{}': {}", new_name, e);
            e
        })?;
    }
    #[cfg(not(feature = "tracing"))]
    {
        new_backend.initialize(config)?;
    }

    #[cfg(feature = "tracing")]
    tracing::info!("Successfully switched to backend '{}'", new_name);

    Ok(())
}

// ============================================================================
// Async Backend Trait (feature-gated)
// ============================================================================

/// Async CAN hardware backend interface.
///
/// This trait provides asynchronous versions of the core message operations.
/// It is only available when the `async` feature is enabled.
///
/// # Feature Flags
///
/// - `async` - Enable async trait (requires runtime selection)
/// - `async-tokio` - Use tokio runtime
/// - `async-async-std` - Use async-std runtime
///
/// # Thread Safety
///
/// Like [`CanBackend`], this trait requires external synchronization.
/// Use `Arc<Mutex<>>` or `Arc<RwLock<>>` for shared access across tasks.
///
/// # Examples
///
/// ```rust,ignore
/// use canlink_hal::{CanBackendAsync, CanMessage};
///
/// async fn send_messages(backend: &mut impl CanBackendAsync) -> Result<(), Box<dyn std::error::Error>> {
///     let msg = CanMessage::new_standard(0x123, &[1, 2, 3, 4])?;
///     backend.send_message_async(&msg).await?;
///
///     if let Some(received) = backend.receive_message_async(Some(Duration::from_secs(1))).await? {
///         println!("Received: {:?}", received);
///     }
///     Ok(())
/// }
/// ```
#[cfg(feature = "async")]
#[allow(async_fn_in_trait)]
pub trait CanBackendAsync: CanBackend {
    /// Send a CAN message asynchronously.
    ///
    /// This is the async version of [`CanBackend::send_message`].
    ///
    /// # Arguments
    ///
    /// * `message` - The CAN message to send
    ///
    /// # Returns
    ///
    /// * `Ok(())` - Message sent successfully
    /// * `Err(CanError)` - Send failed
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let msg = CanMessage::new_standard(0x123, &[0x01, 0x02])?;
    /// backend.send_message_async(&msg).await?;
    /// ```
    async fn send_message_async(&mut self, message: &CanMessage) -> CanResult<()>;

    /// Receive a CAN message asynchronously with optional timeout.
    ///
    /// This is the async version of [`CanBackend::receive_message`].
    /// Unlike the sync version, this method can wait for a message with a timeout.
    ///
    /// # Arguments
    ///
    /// * `timeout` - Optional timeout duration. If `None`, returns immediately
    ///   like the sync version. If `Some(duration)`, waits up to that duration
    ///   for a message.
    ///
    /// # Returns
    ///
    /// * `Ok(Some(message))` - A message was received
    /// * `Ok(None)` - No message available (timeout expired or no timeout and queue empty)
    /// * `Err(CanError)` - Reception failed
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// // Non-blocking receive
    /// if let Some(msg) = backend.receive_message_async(None).await? {
    ///     println!("Received: {:?}", msg);
    /// }
    ///
    /// // Receive with 1 second timeout
    /// match backend.receive_message_async(Some(Duration::from_secs(1))).await? {
    ///     Some(msg) => println!("Received: {:?}", msg),
    ///     None => println!("Timeout - no message received"),
    /// }
    /// ```
    async fn receive_message_async(
        &mut self,
        timeout: Option<Duration>,
    ) -> CanResult<Option<CanMessage>>;
}

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

    // Mock backend for testing
    struct TestBackend {
        initialized: bool,
        fail_count: u32,
    }

    impl TestBackend {
        fn new(fail_count: u32) -> Self {
            Self {
                initialized: false,
                fail_count,
            }
        }
    }

    impl CanBackend for TestBackend {
        fn initialize(&mut self, _config: &BackendConfig) -> CanResult<()> {
            if self.fail_count > 0 {
                self.fail_count -= 1;
                Err(CanError::InitializationFailed {
                    reason: "Test failure".to_string(),
                })
            } else {
                self.initialized = true;
                Ok(())
            }
        }

        fn close(&mut self) -> CanResult<()> {
            Ok(())
        }

        fn get_capability(&self) -> CanResult<HardwareCapability> {
            unimplemented!()
        }

        fn send_message(&mut self, _message: &CanMessage) -> CanResult<()> {
            unimplemented!()
        }

        fn receive_message(&mut self) -> CanResult<Option<CanMessage>> {
            unimplemented!()
        }

        fn open_channel(&mut self, _channel: u8) -> CanResult<()> {
            unimplemented!()
        }

        fn close_channel(&mut self, _channel: u8) -> CanResult<()> {
            unimplemented!()
        }

        fn version(&self) -> BackendVersion {
            BackendVersion::new(0, 1, 0)
        }

        fn name(&self) -> &'static str {
            "test"
        }
    }

    #[test]
    fn test_retry_initialize_success_first_attempt() {
        let mut backend = TestBackend::new(0);
        let config = BackendConfig::new("test");

        let result = retry_initialize(&mut backend, &config, 3, 10);
        assert!(result.is_ok());
        assert!(backend.initialized);
    }

    #[test]
    fn test_retry_initialize_success_after_retries() {
        let mut backend = TestBackend::new(2); // Fail 2 times, succeed on 3rd
        let config = BackendConfig::new("test");

        let result = retry_initialize(&mut backend, &config, 3, 10);
        assert!(result.is_ok());
        assert!(backend.initialized);
    }

    #[test]
    fn test_retry_initialize_failure_all_attempts() {
        let mut backend = TestBackend::new(10); // Fail all attempts
        let config = BackendConfig::new("test");

        let result = retry_initialize(&mut backend, &config, 3, 10);
        assert!(result.is_err());
        assert!(!backend.initialized);

        if let Err(CanError::InitializationFailed { reason }) = result {
            assert!(reason.contains("Failed after 4 attempts"));
        } else {
            panic!("Expected InitializationFailed error");
        }
    }
}

// ============================================================================
// High-Frequency Message Rate Monitor (FR-016)
// ============================================================================

/// Message rate monitor for detecting high-frequency message scenarios.
///
/// This utility helps backends detect when message rates exceed a threshold
/// and log warnings as specified in FR-016. It does not perform any automatic
/// throttling or backpressure - it only logs warnings for user awareness.
///
/// # Usage
///
/// Backends can use this to monitor receive rates:
///
/// ```rust,ignore
/// use canlink_hal::backend::MessageRateMonitor;
///
/// let mut monitor = MessageRateMonitor::new(1000); // Warn above 1000 msg/s
///
/// // In receive loop:
/// if let Some(msg) = backend.receive_message()? {
///     monitor.record_message();
///     process(msg);
/// }
/// ```
///
/// # Thread Safety
///
/// This struct is not thread-safe. Each thread should have its own monitor
/// or use external synchronization.
#[derive(Debug)]
pub struct MessageRateMonitor {
    /// Threshold in messages per second
    threshold_per_second: u32,
    /// Message count in current window
    message_count: u32,
    /// Start of current measurement window
    window_start: std::time::Instant,
    /// Whether we've already warned in this window
    warned_this_window: bool,
}

impl MessageRateMonitor {
    /// Create a new message rate monitor.
    ///
    /// # Arguments
    ///
    /// * `threshold_per_second` - Message rate threshold that triggers warnings
    ///
    /// # Examples
    ///
    /// ```
    /// use canlink_hal::backend::MessageRateMonitor;
    ///
    /// // Warn when rate exceeds 1000 messages/second
    /// let monitor = MessageRateMonitor::new(1000);
    /// ```
    #[must_use]
    pub fn new(threshold_per_second: u32) -> Self {
        Self {
            threshold_per_second,
            message_count: 0,
            window_start: std::time::Instant::now(),
            warned_this_window: false,
        }
    }

    /// Record a message and check if rate exceeds threshold.
    ///
    /// Returns `true` if the rate exceeds the threshold (warning should be logged).
    /// Only returns `true` once per measurement window to avoid log spam.
    ///
    /// # Examples
    ///
    /// ```
    /// use canlink_hal::backend::MessageRateMonitor;
    ///
    /// let mut monitor = MessageRateMonitor::new(1000);
    ///
    /// // Record messages
    /// if monitor.record_message() {
    ///     // Rate exceeded threshold - handle warning
    ///     eprintln!("Warning: High message rate detected");
    /// }
    /// ```
    pub fn record_message(&mut self) -> bool {
        self.message_count += 1;

        let elapsed = self.window_start.elapsed();

        // Check every second
        if elapsed >= Duration::from_secs(1) {
            let exceeded = self.message_count > self.threshold_per_second;

            // Log warning if exceeded and haven't warned yet
            if exceeded && !self.warned_this_window {
                #[cfg(feature = "tracing")]
                tracing::warn!(
                    "High message rate detected: {} messages/second (threshold: {})",
                    self.message_count,
                    self.threshold_per_second
                );
                self.warned_this_window = true;
            }

            // Reset for next window
            self.message_count = 0;
            self.window_start = std::time::Instant::now();
            self.warned_this_window = false;

            exceeded
        } else {
            false
        }
    }

    /// Get the current message count in this window.
    #[must_use]
    pub fn current_count(&self) -> u32 {
        self.message_count
    }

    /// Get the configured threshold.
    #[must_use]
    pub fn threshold(&self) -> u32 {
        self.threshold_per_second
    }

    /// Reset the monitor state.
    pub fn reset(&mut self) {
        self.message_count = 0;
        self.window_start = std::time::Instant::now();
        self.warned_this_window = false;
    }
}

impl Default for MessageRateMonitor {
    /// Create a monitor with default threshold of 10000 messages/second.
    fn default() -> Self {
        Self::new(10000)
    }
}