use std::sync::Arc;
use std::time::SystemTime;
use crate::gige::proto::gvcp::{self, GvcpStatus};
use crate::gige::proto::gvsp::{self, ContentType};
pub trait TelemetrySink<TX: Send + Sized, RX: Send + Sized>: Send + Sync + 'static {
fn warmup(&self) {}
fn sent(&self, tx: &TX, timestamp: SystemTime);
fn received(&self, rx: &RX, timestamp: SystemTime);
}
impl<TX: Send, RX: Send> std::fmt::Debug for dyn TelemetrySink<TX, RX> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("dyn TelemetrySink")
}
}
pub type ControlTelemetry = Arc<dyn TelemetrySink<ControlTx, ControlRx>>;
pub type StreamTelemetry = Arc<dyn TelemetrySink<GvcpCmd, GvspPacket>>;
#[derive(Debug, Clone)]
pub struct GvcpCmd {
pub flags: u8,
pub command: u16,
pub req_id: u16,
pub payload: Box<[u8]>,
}
impl GvcpCmd {
fn parse(datagram: &[u8]) -> Option<Self> {
let cmd = gvcp::Cmd::parse(datagram)?;
Some(Self {
flags: cmd.flags,
command: cmd.command,
req_id: cmd.req_id,
payload: Box::from(cmd.payload),
})
}
}
#[derive(Debug, Clone)]
pub struct GvcpAck {
pub status: GvcpStatus,
pub answer: u16,
pub ack_id: u16,
pub payload: Box<[u8]>,
}
impl GvcpAck {
fn parse(datagram: &[u8]) -> Option<Self> {
let ack = gvcp::Ack::parse(datagram)?;
Some(Self {
status: ack.status,
answer: ack.answer,
ack_id: ack.ack_id,
payload: Box::from(ack.payload),
})
}
}
#[derive(Debug, Clone)]
pub enum ControlTx {
Cmd { cmd: GvcpCmd, retry: bool },
Ack(GvcpAck),
}
#[derive(Debug, Clone)]
pub enum ControlRx {
Ack(GvcpAck),
Cmd(GvcpCmd),
}
#[derive(Debug, Clone)]
pub struct GvspPacket {
pub status: GvcpStatus,
pub extended_ids: bool,
pub frame_id: u64,
pub packet_id: u32,
pub content_type: ContentType,
pub data: Box<[u8]>,
}
pub(crate) fn control_tx(datagram: &[u8], retry: bool) -> Option<ControlTx> {
if gvcp::is_cmd(datagram) {
GvcpCmd::parse(datagram).map(|cmd| ControlTx::Cmd { cmd, retry })
} else {
GvcpAck::parse(datagram).map(ControlTx::Ack)
}
}
pub(crate) fn control_rx(datagram: &[u8]) -> Option<ControlRx> {
if gvcp::is_cmd(datagram) {
GvcpCmd::parse(datagram).map(ControlRx::Cmd)
} else {
GvcpAck::parse(datagram).map(ControlRx::Ack)
}
}
pub(crate) fn stream_tx(datagram: &[u8]) -> Option<GvcpCmd> {
GvcpCmd::parse(datagram)
}
pub(crate) fn stream_rx(datagram: &[u8]) -> Option<GvspPacket> {
let view = gvsp::GvspView::parse(datagram)?;
Some(GvspPacket {
status: view.status,
extended_ids: view.extended_ids,
frame_id: view.frame_id,
packet_id: view.packet_id,
content_type: view.content_type,
data: Box::from(view.data),
})
}