use crate::volume::VolumeRange;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeviceInfo {
pub vendor_id: u16,
pub product_id: u16,
pub product_name: String,
pub manufacturer_name: Option<String>,
pub device_name: Option<String>,
pub claimed_interfaces: Vec<u8>,
pub configuration_id: Option<u8>,
pub unavailable_feedback_interfaces: Vec<u8>,
}
impl DeviceInfo {
pub fn new(vendor_id: u16, product_id: u16, product_name: impl Into<String>) -> Self {
Self {
vendor_id,
product_id,
product_name: product_name.into(),
manufacturer_name: None,
device_name: None,
claimed_interfaces: Vec::new(),
configuration_id: None,
unavailable_feedback_interfaces: Vec::new(),
}
}
pub fn with_claimed_interfaces(mut self, interfaces: impl Into<Vec<u8>>) -> Self {
self.claimed_interfaces = interfaces.into();
self
}
pub fn with_device_name(mut self, name: impl Into<String>) -> Self {
self.device_name = Some(name.into());
self
}
pub fn with_manufacturer_name(mut self, name: impl Into<String>) -> Self {
self.manufacturer_name = Some(name.into());
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UacVersion {
V1,
V2,
V3,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LinkSpeed {
Low,
Full,
High,
Super,
SuperPlus,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SyncKind {
Asynchronous,
Adaptive,
Synchronous,
None,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Pacing {
ExplicitFeedback { endpoint: u8 },
ImplicitFeedback {
interface_number: u8,
alt_setting: u8,
endpoint: u8,
},
Adaptive,
Synchronous,
Unsynchronized,
}
impl Pacing {
pub fn code(self) -> &'static str {
match self {
Self::ExplicitFeedback { .. } => "explicit_feedback",
Self::ImplicitFeedback { .. } => "implicit_feedback",
Self::Adaptive => "adaptive",
Self::Synchronous => "synchronous",
Self::Unsynchronized => "unsynchronized",
}
}
pub fn feedback_endpoint(self) -> Option<u8> {
match self {
Self::ExplicitFeedback { endpoint } => Some(endpoint),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EndpointUsage {
Data,
Feedback,
FeedbackData,
Reserved,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AltFormat {
Pcm,
PcmU8,
PcmFloat32,
Dop,
Dsd,
Other,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RateRange {
pub min_hz: u32,
pub max_hz: u32,
pub step_hz: u32,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct AltSetting {
pub interface_number: u8,
pub alt_setting: u8,
pub endpoint_address: u8,
pub format: AltFormat,
pub channels: u16,
pub subslot_bytes: u8,
pub valid_bits: u8,
pub rate_ranges: Vec<RateRange>,
pub sample_rates: Vec<u32>,
pub sync_type: SyncKind,
pub feedback_endpoint: Option<u8>,
pub max_packet_bytes: usize,
pub service_interval_us: u32,
pub uac_version: UacVersion,
pub endpoint_interval: u8,
pub usage: EndpointUsage,
pub refresh: u8,
pub synch_address: u8,
pub pacing: Option<Pacing>,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Capabilities {
pub exclusion_snapshot_complete: bool,
pub native_only_aliases: Vec<(u8, u8, u8)>,
pub complete_uac1_rate_alternates: Vec<(u8, u8, u8)>,
pub uac_version: UacVersion,
pub link_speed: LinkSpeed,
pub alt_settings: Vec<AltSetting>,
pub pcm_sample_rates: Vec<u32>,
pub pcm_bit_depths: Vec<u8>,
pub channel_counts: Vec<u16>,
pub supports_pcm: bool,
pub supports_dop: bool,
pub dop_max_dsd_bit_rate: Option<u32>,
pub supports_native_dsd: bool,
pub native_dsd_bit_rates: Vec<u32>,
pub has_explicit_feedback: bool,
pub volume: Option<VolumeRange>,
pub has_hardware_volume: bool,
pub has_mute: bool,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn device_info_builder_sets_the_optional_fields() {
let info = DeviceInfo::new(1, 2, "Dac")
.with_claimed_interfaces(vec![0u8, 1, 2])
.with_device_name("/dev/bus/usb/001/002")
.with_manufacturer_name("Acme");
assert_eq!(info.claimed_interfaces, vec![0, 1, 2]);
assert_eq!(info.device_name.as_deref(), Some("/dev/bus/usb/001/002"));
assert_eq!(info.manufacturer_name.as_deref(), Some("Acme"));
}
}
#[derive(Debug, Clone, Copy)]
pub struct ShippedSupport {
pub uac_versions: &'static [UacVersion],
pub playback: bool,
pub pcm_capture: bool,
pub duplex: bool,
pub max_open_devices: Option<usize>,
pub vendor_protocols: bool,
pub type_i_formats: &'static [&'static str],
}
pub const SHIPPED_SUPPORT: ShippedSupport = ShippedSupport {
uac_versions: &[UacVersion::V1, UacVersion::V2, UacVersion::V3],
playback: true,
pcm_capture: true,
duplex: true,
max_open_devices: None,
vendor_protocols: false,
type_i_formats: &["signed_pcm", "unsigned_pcm8", "float32"],
};