use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HardwareCapability {
pub channel_count: u8,
pub supports_canfd: bool,
pub max_bitrate: u32,
pub supported_bitrates: Vec<u32>,
pub filter_count: u8,
pub timestamp_precision: TimestampPrecision,
}
impl HardwareCapability {
#[must_use]
pub fn new(
channel_count: u8,
supports_canfd: bool,
max_bitrate: u32,
supported_bitrates: Vec<u32>,
filter_count: u8,
timestamp_precision: TimestampPrecision,
) -> Self {
Self {
channel_count,
supports_canfd,
max_bitrate,
supported_bitrates,
filter_count,
timestamp_precision,
}
}
#[must_use]
pub fn supports_bitrate(&self, bitrate: u32) -> bool {
self.supported_bitrates.contains(&bitrate)
}
#[must_use]
pub fn has_channel(&self, channel: u8) -> bool {
channel < self.channel_count
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TimestampPrecision {
Microsecond,
Millisecond,
None,
}
impl TimestampPrecision {
#[must_use]
pub const fn resolution_us(&self) -> Option<u64> {
match self {
Self::Microsecond => Some(1),
Self::Millisecond => Some(1000),
Self::None => None,
}
}
#[must_use]
pub const fn is_supported(&self) -> bool {
!matches!(self, Self::None)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hardware_capability() {
let capability = HardwareCapability::new(
2,
true,
8_000_000,
vec![125_000, 250_000, 500_000, 1_000_000],
16,
TimestampPrecision::Microsecond,
);
assert_eq!(capability.channel_count, 2);
assert!(capability.supports_canfd);
assert_eq!(capability.max_bitrate, 8_000_000);
assert_eq!(capability.filter_count, 16);
}
#[test]
fn test_supports_bitrate() {
let capability = HardwareCapability::new(
2,
true,
8_000_000,
vec![125_000, 250_000, 500_000, 1_000_000],
16,
TimestampPrecision::Microsecond,
);
assert!(capability.supports_bitrate(500_000));
assert!(!capability.supports_bitrate(2_000_000));
}
#[test]
fn test_has_channel() {
let capability = HardwareCapability::new(
2,
true,
8_000_000,
vec![125_000, 250_000, 500_000, 1_000_000],
16,
TimestampPrecision::Microsecond,
);
assert!(capability.has_channel(0));
assert!(capability.has_channel(1));
assert!(!capability.has_channel(2));
}
#[test]
fn test_timestamp_precision() {
assert_eq!(TimestampPrecision::Microsecond.resolution_us(), Some(1));
assert_eq!(TimestampPrecision::Millisecond.resolution_us(), Some(1000));
assert_eq!(TimestampPrecision::None.resolution_us(), None);
assert!(TimestampPrecision::Microsecond.is_supported());
assert!(TimestampPrecision::Millisecond.is_supported());
assert!(!TimestampPrecision::None.is_supported());
}
}