use std::collections::VecDeque;
use std::io;
use std::time::{Duration, SystemTime};
use avb_net::{MacAddress, Socket};
use crate::controller::{Config, Controller};
use crate::id::EntityId;
use crate::time::Instant;
#[cfg(target_os = "macos")]
mod apple;
#[cfg(target_os = "macos")]
pub use apple::Local;
const FRAME: usize = 1536;
#[cfg(target_os = "macos")]
const LOCAL_WAIT: Duration = Duration::from_millis(2);
pub fn avb_enabled(interface: &str) -> Option<bool> {
#[cfg(target_os = "macos")]
return apple::avb_enabled(interface);
#[cfg(not(target_os = "macos"))]
{
let _ = interface;
None
}
}
pub struct Driver {
socket: Socket,
controller: Controller,
epoch: std::time::Instant,
received: [u8; FRAME],
sending: [u8; FRAME],
frames: VecDeque<Frame>,
keep: usize,
#[cfg(target_os = "macos")]
local: Option<apple::Local>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Frame {
pub at: SystemTime,
pub sent: bool,
pub peer: MacAddress,
pub bytes: Vec<u8>,
}
impl Driver {
pub fn open(interface: &str) -> io::Result<Self> {
Self::open_with(interface, |_| {})
}
pub fn open_with(interface: &str, configure: impl FnOnce(&mut Config)) -> io::Result<Self> {
let socket = Socket::open(interface, crate::ETHERTYPE_AVTP)?;
socket.join_multicast(crate::ADP_ACMP_MULTICAST)?;
let _ =
socket.keep_payloads_starting(crate::avtp::subtype::ADP..=crate::avtp::subtype::ACMP);
let mut config = Config::new(EntityId::from_mac(socket.mac()));
let clock = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |since| since.as_nanos() as u64);
config.first_sequence_id = clock as u16;
config.random_seed = config.entity_id.0 ^ clock;
configure(&mut config);
#[cfg(target_os = "macos")]
let local = apple::Local::open(interface, config.entity_id);
#[cfg(target_os = "macos")]
if local.is_none() {
config.own_mac = Some(socket.mac());
}
let controller = Controller::new(config);
Ok(Self {
socket,
controller,
epoch: std::time::Instant::now(),
received: [0; FRAME],
sending: [0; FRAME],
frames: VecDeque::new(),
keep: 0,
#[cfg(target_os = "macos")]
local,
})
}
pub fn controller(&self) -> &Controller {
&self.controller
}
pub fn controller_mut(&mut self) -> &mut Controller {
&mut self.controller
}
pub fn keep_frames(&mut self, limit: usize) {
self.keep = limit;
while self.frames.len() > limit {
self.frames.pop_front();
}
}
pub fn poll_frame(&mut self) -> Option<Frame> {
self.frames.pop_front()
}
fn keeps(&self, bytes: &[u8]) -> bool {
self.keep > 0 && crate::pdu::is_atdecc(bytes)
}
fn kept(&mut self, sent: bool, peer: MacAddress, bytes: Vec<u8>) {
if self.frames.len() == self.keep {
self.frames.pop_front();
}
self.frames.push_back(Frame {
at: SystemTime::now(),
sent,
peer,
bytes,
});
}
pub fn now(&self) -> Instant {
Instant::from_nanos(self.epoch.elapsed().as_nanos() as u64)
}
pub fn turn(&mut self, max_wait: Duration) -> io::Result<()> {
self.flush()?;
self.take_local();
let now = self.now();
#[cfg_attr(not(target_os = "macos"), allow(unused_mut))]
let mut wait = match self.controller.poll_timeout() {
Some(deadline) => deadline.saturating_duration_since(now).min(max_wait),
None => max_wait,
};
#[cfg(target_os = "macos")]
if self.local.as_ref().is_some_and(apple::Local::waiting) {
wait = wait.min(LOCAL_WAIT);
}
if let Some(received) = self.socket.receive(&mut self.received, Some(wait))? {
let now = self.now();
let bytes = &self.received[..received.length];
let _ = self.controller.handle_frame(now, received.source, bytes);
if self.keeps(&self.received[..received.length]) {
let bytes = self.received[..received.length].to_vec();
#[cfg(target_os = "macos")]
let sent = received.source == self.socket.mac()
&& self
.local
.as_ref()
.is_some_and(|local| local.echoes(&bytes));
#[cfg(not(target_os = "macos"))]
let sent = false;
self.kept(sent, received.source, bytes);
}
}
self.take_local();
let now = self.now();
self.controller.handle_timeout(now);
self.flush()
}
fn take_local(&mut self) {
#[cfg(target_os = "macos")]
while let Some(bytes) = self.local.as_ref().and_then(apple::Local::poll) {
let now = self.now();
let source = self.socket.mac();
let _ = self.controller.handle_frame(now, source, &bytes);
if self.keeps(&bytes) {
self.kept(false, source, bytes);
}
}
}
#[cfg(target_os = "macos")]
fn send_local(&self, bytes: &[u8]) -> bool {
let Some(local) = &self.local else {
return false;
};
let own = self.socket.mac();
local.send(bytes, |entity_id| {
self.controller
.entity(entity_id)
.is_some_and(|entity| entity.mac == own)
})
}
pub fn close(mut self) -> io::Result<()> {
self.controller.depart();
self.flush()
}
fn flush(&mut self) -> io::Result<()> {
while let Some(transmit) = self
.controller
.poll_transmit(&mut self.sending)
.map_err(io::Error::other)?
{
let bytes = &self.sending[..transmit.length];
#[cfg(target_os = "macos")]
let local = self.send_local(bytes);
#[cfg(not(target_os = "macos"))]
let local = false;
if !local {
self.socket.send(transmit.destination, bytes)?;
}
if self.keeps(&self.sending[..transmit.length]) {
let bytes = self.sending[..transmit.length].to_vec();
self.kept(true, transmit.destination, bytes);
}
}
Ok(())
}
}