use std::any::Any;
use std::future::Future;
use crate::async_compat::Shared;
use crate::async_registry::AsyncTypedDeviceBackend;
use crate::async_streamer::{
AsyncRxStreamer, AsyncTxStreamer, DynAsyncRxStreamerBackend, DynAsyncTxStreamerBackend,
};
use crate::Args;
use crate::AsyncFutureExt;
use crate::AsyncRegistry;
use crate::BoxedFuture;
use crate::Capability;
use crate::ChannelCapabilities;
use crate::ChannelControls;
use crate::DeviceCapabilities;
use crate::Direction;
use crate::Driver;
use crate::Error;
use crate::MaybeSend;
use crate::MaybeSync;
use crate::Range;
pub type DynAsyncRxStreamer = Box<dyn DynAsyncRxStreamerBackend>;
pub type DynAsyncTxStreamer = Box<dyn DynAsyncTxStreamerBackend>;
pub trait AsyncDeviceInfo: MaybeSend + MaybeSync {
fn driver(&self) -> Driver;
fn async_id(&self) -> impl Future<Output = Result<String, Error>> + MaybeSend + '_;
fn async_info(&self) -> impl Future<Output = Result<Args, Error>> + MaybeSend + '_;
fn async_num_channels(
&self,
direction: Direction,
) -> impl Future<Output = Result<usize, Error>> + MaybeSend + '_;
fn async_full_duplex(&self) -> impl Future<Output = Result<bool, Error>> + MaybeSend + '_;
}
pub trait AsyncRxDevice: AsyncDeviceInfo {
type RxStreamer: AsyncRxStreamer;
fn async_rx_streamer<'a>(
&'a self,
channels: &'a [usize],
args: Args,
) -> impl Future<Output = Result<Self::RxStreamer, Error>> + MaybeSend + 'a;
}
pub trait AsyncTxDevice: AsyncDeviceInfo {
type TxStreamer: AsyncTxStreamer;
fn async_tx_streamer<'a>(
&'a self,
channels: &'a [usize],
args: Args,
) -> impl Future<Output = Result<Self::TxStreamer, Error>> + MaybeSend + 'a;
}
pub trait AsyncAntennaControl: AsyncDeviceInfo {
fn async_antennas(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<Vec<String>, Error>> + MaybeSend + '_;
fn async_antenna(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<String, Error>> + MaybeSend + '_;
fn async_set_antenna<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
) -> impl Future<Output = Result<(), Error>> + MaybeSend + 'a;
}
pub trait AsyncAgcControl: AsyncDeviceInfo {
fn async_agc_available(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<bool, Error>> + MaybeSend + '_;
fn async_agc_enabled(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<bool, Error>> + MaybeSend + '_;
fn async_set_agc_enabled(
&self,
direction: Direction,
channel: usize,
enabled: bool,
) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_;
}
pub trait AsyncGainControl: AsyncDeviceInfo {
fn async_gain_elements(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<Vec<String>, Error>> + MaybeSend + '_;
fn async_set_gain(
&self,
direction: Direction,
channel: usize,
gain: f64,
) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_;
fn async_gain(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<Option<f64>, Error>> + MaybeSend + '_;
fn async_gain_range(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<Range, Error>> + MaybeSend + '_;
fn async_set_gain_element<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
gain: f64,
) -> impl Future<Output = Result<(), Error>> + MaybeSend + 'a;
fn async_gain_element<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
) -> impl Future<Output = Result<Option<f64>, Error>> + MaybeSend + 'a;
fn async_gain_element_range<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
) -> impl Future<Output = Result<Range, Error>> + MaybeSend + 'a;
}
pub trait AsyncFrequencyControl: AsyncDeviceInfo {
fn async_frequency_range(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<Range, Error>> + MaybeSend + '_;
fn async_frequency(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<f64, Error>> + MaybeSend + '_;
fn async_set_frequency(
&self,
direction: Direction,
channel: usize,
frequency: f64,
args: Args,
) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_;
fn async_frequency_components(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<Vec<String>, Error>> + MaybeSend + '_;
fn async_component_frequency_range<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
) -> impl Future<Output = Result<Range, Error>> + MaybeSend + 'a;
fn async_component_frequency<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
) -> impl Future<Output = Result<f64, Error>> + MaybeSend + 'a;
fn async_set_component_frequency<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
frequency: f64,
) -> impl Future<Output = Result<(), Error>> + MaybeSend + 'a;
}
pub trait AsyncSampleRateControl: AsyncDeviceInfo {
fn async_sample_rate(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<f64, Error>> + MaybeSend + '_;
fn async_set_sample_rate(
&self,
direction: Direction,
channel: usize,
rate: f64,
) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_;
fn async_get_sample_rate_range(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<Range, Error>> + MaybeSend + '_;
}
pub trait AsyncBandwidthControl: AsyncDeviceInfo {
fn async_bandwidth(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<f64, Error>> + MaybeSend + '_;
fn async_set_bandwidth(
&self,
direction: Direction,
channel: usize,
bandwidth: f64,
) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_;
fn async_get_bandwidth_range(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<Range, Error>> + MaybeSend + '_;
}
pub trait AsyncDcOffsetControl: AsyncDeviceInfo {
fn async_dc_offset_available(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<bool, Error>> + MaybeSend + '_;
fn async_dc_offset_enabled(
&self,
direction: Direction,
channel: usize,
) -> impl Future<Output = Result<bool, Error>> + MaybeSend + '_;
fn async_set_dc_offset_enabled(
&self,
direction: Direction,
channel: usize,
enabled: bool,
) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_;
}
pub trait DynAsyncDeviceInfo: MaybeSend + MaybeSync {
fn dyn_driver(&self) -> Driver;
fn dyn_id(&self) -> BoxedFuture<'_, Result<String, Error>>;
fn dyn_info(&self) -> BoxedFuture<'_, Result<Args, Error>>;
fn dyn_num_channels(&self, direction: Direction) -> BoxedFuture<'_, Result<usize, Error>>;
fn dyn_full_duplex(&self) -> BoxedFuture<'_, Result<bool, Error>>;
}
impl<T> DynAsyncDeviceInfo for T
where
T: AsyncDeviceInfo,
{
fn dyn_driver(&self) -> Driver {
AsyncDeviceInfo::driver(self)
}
fn dyn_id(&self) -> BoxedFuture<'_, Result<String, Error>> {
AsyncDeviceInfo::async_id(self).boxed_async()
}
fn dyn_info(&self) -> BoxedFuture<'_, Result<Args, Error>> {
AsyncDeviceInfo::async_info(self).boxed_async()
}
fn dyn_num_channels(&self, direction: Direction) -> BoxedFuture<'_, Result<usize, Error>> {
AsyncDeviceInfo::async_num_channels(self, direction).boxed_async()
}
fn dyn_full_duplex(&self) -> BoxedFuture<'_, Result<bool, Error>> {
AsyncDeviceInfo::async_full_duplex(self).boxed_async()
}
}
pub trait DynAsyncRxDevice: DynAsyncDeviceInfo {
fn dyn_rx_streamer<'a>(
&'a self,
channels: &'a [usize],
args: Args,
) -> BoxedFuture<'a, Result<DynAsyncRxStreamer, Error>>;
}
impl<T> DynAsyncRxDevice for T
where
T: AsyncRxDevice,
T::RxStreamer: 'static,
{
fn dyn_rx_streamer<'a>(
&'a self,
channels: &'a [usize],
args: Args,
) -> BoxedFuture<'a, Result<DynAsyncRxStreamer, Error>> {
async move {
Ok(
Box::new(AsyncRxDevice::async_rx_streamer(self, channels, args).await?)
as DynAsyncRxStreamer,
)
}
.boxed_async()
}
}
pub trait DynAsyncTxDevice: DynAsyncDeviceInfo {
fn dyn_tx_streamer<'a>(
&'a self,
channels: &'a [usize],
args: Args,
) -> BoxedFuture<'a, Result<DynAsyncTxStreamer, Error>>;
}
impl<T> DynAsyncTxDevice for T
where
T: AsyncTxDevice,
T::TxStreamer: 'static,
{
fn dyn_tx_streamer<'a>(
&'a self,
channels: &'a [usize],
args: Args,
) -> BoxedFuture<'a, Result<DynAsyncTxStreamer, Error>> {
async move {
Ok(
Box::new(AsyncTxDevice::async_tx_streamer(self, channels, args).await?)
as DynAsyncTxStreamer,
)
}
.boxed_async()
}
}
pub trait DynAsyncAntennaControl: DynAsyncDeviceInfo {
fn dyn_antennas(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Vec<String>, Error>>;
fn dyn_antenna(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<String, Error>>;
fn dyn_set_antenna<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
) -> BoxedFuture<'a, Result<(), Error>>;
}
impl<T> DynAsyncAntennaControl for T
where
T: AsyncAntennaControl,
{
fn dyn_antennas(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Vec<String>, Error>> {
AsyncAntennaControl::async_antennas(self, direction, channel).boxed_async()
}
fn dyn_antenna(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<String, Error>> {
AsyncAntennaControl::async_antenna(self, direction, channel).boxed_async()
}
fn dyn_set_antenna<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
) -> BoxedFuture<'a, Result<(), Error>> {
AsyncAntennaControl::async_set_antenna(self, direction, channel, name).boxed_async()
}
}
pub trait DynAsyncAgcControl: DynAsyncDeviceInfo {
fn dyn_agc_available(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<bool, Error>>;
fn dyn_agc_enabled(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<bool, Error>>;
fn dyn_set_agc_enabled(
&self,
direction: Direction,
channel: usize,
enabled: bool,
) -> BoxedFuture<'_, Result<(), Error>>;
}
impl<T> DynAsyncAgcControl for T
where
T: AsyncAgcControl,
{
fn dyn_agc_available(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<bool, Error>> {
AsyncAgcControl::async_agc_available(self, direction, channel).boxed_async()
}
fn dyn_agc_enabled(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<bool, Error>> {
AsyncAgcControl::async_agc_enabled(self, direction, channel).boxed_async()
}
fn dyn_set_agc_enabled(
&self,
direction: Direction,
channel: usize,
enabled: bool,
) -> BoxedFuture<'_, Result<(), Error>> {
AsyncAgcControl::async_set_agc_enabled(self, direction, channel, enabled).boxed_async()
}
}
pub trait DynAsyncGainControl: DynAsyncDeviceInfo {
fn dyn_gain_elements(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Vec<String>, Error>>;
fn dyn_set_gain(
&self,
direction: Direction,
channel: usize,
gain: f64,
) -> BoxedFuture<'_, Result<(), Error>>;
fn dyn_gain(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Option<f64>, Error>>;
fn dyn_gain_range(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Range, Error>>;
fn dyn_set_gain_element<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
gain: f64,
) -> BoxedFuture<'a, Result<(), Error>>;
fn dyn_gain_element<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
) -> BoxedFuture<'a, Result<Option<f64>, Error>>;
fn dyn_gain_element_range<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
) -> BoxedFuture<'a, Result<Range, Error>>;
}
impl<T> DynAsyncGainControl for T
where
T: AsyncGainControl,
{
fn dyn_gain_elements(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Vec<String>, Error>> {
AsyncGainControl::async_gain_elements(self, direction, channel).boxed_async()
}
fn dyn_set_gain(
&self,
direction: Direction,
channel: usize,
gain: f64,
) -> BoxedFuture<'_, Result<(), Error>> {
AsyncGainControl::async_set_gain(self, direction, channel, gain).boxed_async()
}
fn dyn_gain(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Option<f64>, Error>> {
AsyncGainControl::async_gain(self, direction, channel).boxed_async()
}
fn dyn_gain_range(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Range, Error>> {
AsyncGainControl::async_gain_range(self, direction, channel).boxed_async()
}
fn dyn_set_gain_element<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
gain: f64,
) -> BoxedFuture<'a, Result<(), Error>> {
AsyncGainControl::async_set_gain_element(self, direction, channel, name, gain).boxed_async()
}
fn dyn_gain_element<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
) -> BoxedFuture<'a, Result<Option<f64>, Error>> {
AsyncGainControl::async_gain_element(self, direction, channel, name).boxed_async()
}
fn dyn_gain_element_range<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
) -> BoxedFuture<'a, Result<Range, Error>> {
AsyncGainControl::async_gain_element_range(self, direction, channel, name).boxed_async()
}
}
pub trait DynAsyncFrequencyControl: DynAsyncDeviceInfo {
fn dyn_frequency_range(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Range, Error>>;
fn dyn_frequency(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<f64, Error>>;
fn dyn_set_frequency(
&self,
direction: Direction,
channel: usize,
frequency: f64,
args: Args,
) -> BoxedFuture<'_, Result<(), Error>>;
fn dyn_frequency_components(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Vec<String>, Error>>;
fn dyn_component_frequency_range<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
) -> BoxedFuture<'a, Result<Range, Error>>;
fn dyn_component_frequency<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
) -> BoxedFuture<'a, Result<f64, Error>>;
fn dyn_set_component_frequency<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
frequency: f64,
) -> BoxedFuture<'a, Result<(), Error>>;
}
impl<T> DynAsyncFrequencyControl for T
where
T: AsyncFrequencyControl,
{
fn dyn_frequency_range(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Range, Error>> {
AsyncFrequencyControl::async_frequency_range(self, direction, channel).boxed_async()
}
fn dyn_frequency(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<f64, Error>> {
AsyncFrequencyControl::async_frequency(self, direction, channel).boxed_async()
}
fn dyn_set_frequency(
&self,
direction: Direction,
channel: usize,
frequency: f64,
args: Args,
) -> BoxedFuture<'_, Result<(), Error>> {
AsyncFrequencyControl::async_set_frequency(self, direction, channel, frequency, args)
.boxed_async()
}
fn dyn_frequency_components(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Vec<String>, Error>> {
AsyncFrequencyControl::async_frequency_components(self, direction, channel).boxed_async()
}
fn dyn_component_frequency_range<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
) -> BoxedFuture<'a, Result<Range, Error>> {
AsyncFrequencyControl::async_component_frequency_range(self, direction, channel, name)
.boxed_async()
}
fn dyn_component_frequency<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
) -> BoxedFuture<'a, Result<f64, Error>> {
AsyncFrequencyControl::async_component_frequency(self, direction, channel, name)
.boxed_async()
}
fn dyn_set_component_frequency<'a>(
&'a self,
direction: Direction,
channel: usize,
name: &'a str,
frequency: f64,
) -> BoxedFuture<'a, Result<(), Error>> {
AsyncFrequencyControl::async_set_component_frequency(
self, direction, channel, name, frequency,
)
.boxed_async()
}
}
pub trait DynAsyncSampleRateControl: DynAsyncDeviceInfo {
fn dyn_sample_rate(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<f64, Error>>;
fn dyn_set_sample_rate(
&self,
direction: Direction,
channel: usize,
rate: f64,
) -> BoxedFuture<'_, Result<(), Error>>;
fn dyn_get_sample_rate_range(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Range, Error>>;
}
impl<T> DynAsyncSampleRateControl for T
where
T: AsyncSampleRateControl,
{
fn dyn_sample_rate(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<f64, Error>> {
AsyncSampleRateControl::async_sample_rate(self, direction, channel).boxed_async()
}
fn dyn_set_sample_rate(
&self,
direction: Direction,
channel: usize,
rate: f64,
) -> BoxedFuture<'_, Result<(), Error>> {
AsyncSampleRateControl::async_set_sample_rate(self, direction, channel, rate).boxed_async()
}
fn dyn_get_sample_rate_range(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Range, Error>> {
AsyncSampleRateControl::async_get_sample_rate_range(self, direction, channel).boxed_async()
}
}
pub trait DynAsyncBandwidthControl: DynAsyncDeviceInfo {
fn dyn_bandwidth(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<f64, Error>>;
fn dyn_set_bandwidth(
&self,
direction: Direction,
channel: usize,
bandwidth: f64,
) -> BoxedFuture<'_, Result<(), Error>>;
fn dyn_get_bandwidth_range(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Range, Error>>;
}
impl<T> DynAsyncBandwidthControl for T
where
T: AsyncBandwidthControl,
{
fn dyn_bandwidth(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<f64, Error>> {
AsyncBandwidthControl::async_bandwidth(self, direction, channel).boxed_async()
}
fn dyn_set_bandwidth(
&self,
direction: Direction,
channel: usize,
bandwidth: f64,
) -> BoxedFuture<'_, Result<(), Error>> {
AsyncBandwidthControl::async_set_bandwidth(self, direction, channel, bandwidth)
.boxed_async()
}
fn dyn_get_bandwidth_range(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<Range, Error>> {
AsyncBandwidthControl::async_get_bandwidth_range(self, direction, channel).boxed_async()
}
}
pub trait DynAsyncDcOffsetControl: DynAsyncDeviceInfo {
fn dyn_dc_offset_available(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<bool, Error>>;
fn dyn_dc_offset_enabled(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<bool, Error>>;
fn dyn_set_dc_offset_enabled(
&self,
direction: Direction,
channel: usize,
enabled: bool,
) -> BoxedFuture<'_, Result<(), Error>>;
}
impl<T> DynAsyncDcOffsetControl for T
where
T: AsyncDcOffsetControl,
{
fn dyn_dc_offset_available(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<bool, Error>> {
AsyncDcOffsetControl::async_dc_offset_available(self, direction, channel).boxed_async()
}
fn dyn_dc_offset_enabled(
&self,
direction: Direction,
channel: usize,
) -> BoxedFuture<'_, Result<bool, Error>> {
AsyncDcOffsetControl::async_dc_offset_enabled(self, direction, channel).boxed_async()
}
fn dyn_set_dc_offset_enabled(
&self,
direction: Direction,
channel: usize,
enabled: bool,
) -> BoxedFuture<'_, Result<(), Error>> {
AsyncDcOffsetControl::async_set_dc_offset_enabled(self, direction, channel, enabled)
.boxed_async()
}
}
pub trait DynAsyncDeviceBackend: DynAsyncDeviceInfo + MaybeSend + MaybeSync + Any {
fn async_rx_device(&self) -> Option<&dyn DynAsyncRxDevice> {
None
}
fn async_tx_device(&self) -> Option<&dyn DynAsyncTxDevice> {
None
}
fn async_antenna_control(&self) -> Option<&dyn DynAsyncAntennaControl> {
None
}
fn async_agc_control(&self) -> Option<&dyn DynAsyncAgcControl> {
None
}
fn async_gain_control(&self) -> Option<&dyn DynAsyncGainControl> {
None
}
fn async_frequency_control(&self) -> Option<&dyn DynAsyncFrequencyControl> {
None
}
fn async_sample_rate_control(&self) -> Option<&dyn DynAsyncSampleRateControl> {
None
}
fn async_bandwidth_control(&self) -> Option<&dyn DynAsyncBandwidthControl> {
None
}
fn async_dc_offset_control(&self) -> Option<&dyn DynAsyncDcOffsetControl> {
None
}
}
#[derive(Clone)]
pub struct DynAsyncDevice {
inner: Shared<dyn DynAsyncDeviceBackend>,
}
async fn async_device_capabilities<D>(dev: &D) -> Result<DeviceCapabilities, Error>
where
D: DynAsyncDeviceBackend + ?Sized,
{
Ok(DeviceCapabilities {
full_duplex: dev.dyn_full_duplex().await?,
rx_channels: async_channel_capabilities(dev, Direction::Rx).await?,
tx_channels: async_channel_capabilities(dev, Direction::Tx).await?,
})
}
async fn async_channel_capabilities<D>(
dev: &D,
direction: Direction,
) -> Result<Vec<ChannelCapabilities>, Error>
where
D: DynAsyncDeviceBackend + ?Sized,
{
let channels = dev.dyn_num_channels(direction).await?;
let mut out = Vec::with_capacity(channels);
for channel in 0..channels {
out.push(ChannelCapabilities {
channel,
controls: ChannelControls {
antennas: optional_capability_async(dev.async_antenna_control(), |cap| {
cap.dyn_antennas(direction, channel)
})
.await?,
agc: capability_available_async(dev.async_agc_control(), |cap| {
cap.dyn_agc_available(direction, channel)
})
.await?,
gain_elements: optional_capability_async(dev.async_gain_control(), |cap| {
cap.dyn_gain_elements(direction, channel)
})
.await?,
gain_range: optional_capability_async(dev.async_gain_control(), |cap| {
cap.dyn_gain_range(direction, channel)
})
.await?,
frequency_components: optional_capability_async(
dev.async_frequency_control(),
|cap| cap.dyn_frequency_components(direction, channel),
)
.await?,
frequency_range: optional_capability_async(dev.async_frequency_control(), |cap| {
cap.dyn_frequency_range(direction, channel)
})
.await?,
sample_rate_range: optional_capability_async(
dev.async_sample_rate_control(),
|cap| cap.dyn_get_sample_rate_range(direction, channel),
)
.await?,
bandwidth_range: optional_capability_async(dev.async_bandwidth_control(), |cap| {
cap.dyn_get_bandwidth_range(direction, channel)
})
.await?,
dc_offset: capability_available_async(dev.async_dc_offset_control(), |cap| {
cap.dyn_dc_offset_available(direction, channel)
})
.await?,
},
});
}
Ok(out)
}
fn optional_capability<T>(result: Result<T, Error>) -> Result<Option<T>, Error> {
match result {
Ok(value) => Ok(Some(value)),
Err(e) if e.is_unsupported() => Ok(None),
Err(e) => Err(e),
}
}
async fn optional_capability_async<C: ?Sized, T>(
cap: Option<&C>,
f: impl for<'a> FnOnce(&'a C) -> BoxedFuture<'a, Result<T, Error>>,
) -> Result<Option<T>, Error> {
match cap {
Some(cap) => optional_capability(f(cap).await),
None => Ok(None),
}
}
async fn capability_available_async<C: ?Sized>(
cap: Option<&C>,
f: impl for<'a> FnOnce(&'a C) -> BoxedFuture<'a, Result<bool, Error>>,
) -> Result<bool, Error> {
match cap {
Some(cap) => match f(cap).await {
Ok(available) => Ok(available),
Err(e) if e.is_unsupported() => Ok(false),
Err(e) => Err(e),
},
None => Ok(false),
}
}
pub struct AsyncRxChannel<'a, T: ?Sized> {
dev: &'a T,
channel: usize,
}
impl<'a, T: ?Sized> AsyncRxChannel<'a, T> {
fn new(dev: &'a T, channel: usize) -> Self {
Self { dev, channel }
}
pub fn id(&self) -> usize {
self.channel
}
pub fn index(&self) -> usize {
self.channel
}
}
pub struct AsyncTxChannel<'a, T: ?Sized> {
dev: &'a T,
channel: usize,
}
impl<'a, T: ?Sized> AsyncTxChannel<'a, T> {
fn new(dev: &'a T, channel: usize) -> Self {
Self { dev, channel }
}
pub fn id(&self) -> usize {
self.channel
}
pub fn index(&self) -> usize {
self.channel
}
}
pub struct AsyncAntenna<'a, T: AsyncAntennaControl + ?Sized> {
dev: &'a T,
direction: Direction,
channel: usize,
}
impl<'a, T: AsyncAntennaControl + ?Sized> AsyncAntenna<'a, T> {
fn new(dev: &'a T, direction: Direction, channel: usize) -> Self {
Self {
dev,
direction,
channel,
}
}
pub fn ports(&self) -> impl Future<Output = Result<Vec<String>, Error>> + MaybeSend + '_ {
self.dev.async_antennas(self.direction, self.channel)
}
pub fn selected(&self) -> impl Future<Output = Result<String, Error>> + MaybeSend + '_ {
self.dev.async_antenna(self.direction, self.channel)
}
pub fn select<'b>(
&'b self,
name: &'b str,
) -> impl Future<Output = Result<(), Error>> + MaybeSend + 'b {
self.dev
.async_set_antenna(self.direction, self.channel, name)
}
}
pub struct AsyncAgc<'a, T: AsyncAgcControl + ?Sized> {
dev: &'a T,
direction: Direction,
channel: usize,
}
impl<'a, T: AsyncAgcControl + ?Sized> AsyncAgc<'a, T> {
fn new(dev: &'a T, direction: Direction, channel: usize) -> Self {
Self {
dev,
direction,
channel,
}
}
async fn ensure_available(&self) -> Result<(), Error> {
if self
.dev
.async_agc_available(self.direction, self.channel)
.await?
{
Ok(())
} else {
Err(Error::unsupported(Capability::Agc))
}
}
pub fn enabled(&self) -> impl Future<Output = Result<bool, Error>> + MaybeSend + '_ {
async move {
self.ensure_available().await?;
self.dev
.async_agc_enabled(self.direction, self.channel)
.await
}
}
pub fn enable(&self) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_ {
self.set_enabled(true)
}
pub fn disable(&self) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_ {
self.set_enabled(false)
}
pub fn set_enabled(
&self,
enabled: bool,
) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_ {
async move {
self.ensure_available().await?;
self.dev
.async_set_agc_enabled(self.direction, self.channel, enabled)
.await
}
}
}
pub struct AsyncGain<'a, T: AsyncGainControl + ?Sized> {
dev: &'a T,
direction: Direction,
channel: usize,
}
impl<'a, T: AsyncGainControl + ?Sized> AsyncGain<'a, T> {
fn new(dev: &'a T, direction: Direction, channel: usize) -> Self {
Self {
dev,
direction,
channel,
}
}
pub fn elements(&self) -> impl Future<Output = Result<Vec<String>, Error>> + MaybeSend + '_ {
self.dev.async_gain_elements(self.direction, self.channel)
}
pub fn value(&self) -> impl Future<Output = Result<Option<f64>, Error>> + MaybeSend + '_ {
self.dev.async_gain(self.direction, self.channel)
}
pub fn set(&self, gain: f64) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_ {
self.dev.async_set_gain(self.direction, self.channel, gain)
}
pub fn range(&self) -> impl Future<Output = Result<Range, Error>> + MaybeSend + '_ {
self.dev.async_gain_range(self.direction, self.channel)
}
pub fn element(&self, name: &str) -> AsyncGainElement<'a, T> {
AsyncGainElement {
dev: self.dev,
direction: self.direction,
channel: self.channel,
name: name.to_string(),
}
}
}
pub struct AsyncGainElement<'a, T: AsyncGainControl + ?Sized> {
dev: &'a T,
direction: Direction,
channel: usize,
name: String,
}
impl<'a, T: AsyncGainControl + ?Sized> AsyncGainElement<'a, T> {
pub fn value(&self) -> impl Future<Output = Result<Option<f64>, Error>> + MaybeSend + '_ {
self.dev
.async_gain_element(self.direction, self.channel, &self.name)
}
pub fn set(&self, gain: f64) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_ {
self.dev
.async_set_gain_element(self.direction, self.channel, &self.name, gain)
}
pub fn range(&self) -> impl Future<Output = Result<Range, Error>> + MaybeSend + '_ {
self.dev
.async_gain_element_range(self.direction, self.channel, &self.name)
}
}
pub struct AsyncFrequency<'a, T: AsyncFrequencyControl + ?Sized> {
dev: &'a T,
direction: Direction,
channel: usize,
}
impl<'a, T: AsyncFrequencyControl + ?Sized> AsyncFrequency<'a, T> {
fn new(dev: &'a T, direction: Direction, channel: usize) -> Self {
Self {
dev,
direction,
channel,
}
}
pub fn value(&self) -> impl Future<Output = Result<f64, Error>> + MaybeSend + '_ {
self.dev.async_frequency(self.direction, self.channel)
}
pub fn set(&self, frequency: f64) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_ {
self.set_with_args(frequency, Args::new())
}
pub fn set_with_args(
&self,
frequency: f64,
args: Args,
) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_ {
self.dev
.async_set_frequency(self.direction, self.channel, frequency, args)
}
pub fn range(&self) -> impl Future<Output = Result<Range, Error>> + MaybeSend + '_ {
self.dev.async_frequency_range(self.direction, self.channel)
}
pub fn components(&self) -> impl Future<Output = Result<Vec<String>, Error>> + MaybeSend + '_ {
self.dev
.async_frequency_components(self.direction, self.channel)
}
pub fn component(&self, name: &str) -> AsyncFrequencyComponent<'a, T> {
AsyncFrequencyComponent {
dev: self.dev,
direction: self.direction,
channel: self.channel,
name: name.to_string(),
}
}
}
pub struct AsyncFrequencyComponent<'a, T: AsyncFrequencyControl + ?Sized> {
dev: &'a T,
direction: Direction,
channel: usize,
name: String,
}
impl<'a, T: AsyncFrequencyControl + ?Sized> AsyncFrequencyComponent<'a, T> {
pub fn value(&self) -> impl Future<Output = Result<f64, Error>> + MaybeSend + '_ {
self.dev
.async_component_frequency(self.direction, self.channel, &self.name)
}
pub fn set(&self, frequency: f64) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_ {
self.dev
.async_set_component_frequency(self.direction, self.channel, &self.name, frequency)
}
pub fn range(&self) -> impl Future<Output = Result<Range, Error>> + MaybeSend + '_ {
self.dev
.async_component_frequency_range(self.direction, self.channel, &self.name)
}
}
pub struct AsyncSampleRate<'a, T: AsyncSampleRateControl + ?Sized> {
dev: &'a T,
direction: Direction,
channel: usize,
}
impl<'a, T: AsyncSampleRateControl + ?Sized> AsyncSampleRate<'a, T> {
fn new(dev: &'a T, direction: Direction, channel: usize) -> Self {
Self {
dev,
direction,
channel,
}
}
pub fn value(&self) -> impl Future<Output = Result<f64, Error>> + MaybeSend + '_ {
self.dev.async_sample_rate(self.direction, self.channel)
}
pub fn set(&self, rate: f64) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_ {
self.dev
.async_set_sample_rate(self.direction, self.channel, rate)
}
pub fn range(&self) -> impl Future<Output = Result<Range, Error>> + MaybeSend + '_ {
self.dev
.async_get_sample_rate_range(self.direction, self.channel)
}
}
pub struct AsyncBandwidth<'a, T: AsyncBandwidthControl + ?Sized> {
dev: &'a T,
direction: Direction,
channel: usize,
}
impl<'a, T: AsyncBandwidthControl + ?Sized> AsyncBandwidth<'a, T> {
fn new(dev: &'a T, direction: Direction, channel: usize) -> Self {
Self {
dev,
direction,
channel,
}
}
pub fn value(&self) -> impl Future<Output = Result<f64, Error>> + MaybeSend + '_ {
self.dev.async_bandwidth(self.direction, self.channel)
}
pub fn set(&self, bandwidth: f64) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_ {
self.dev
.async_set_bandwidth(self.direction, self.channel, bandwidth)
}
pub fn range(&self) -> impl Future<Output = Result<Range, Error>> + MaybeSend + '_ {
self.dev
.async_get_bandwidth_range(self.direction, self.channel)
}
}
pub struct AsyncDcOffset<'a, T: AsyncDcOffsetControl + ?Sized> {
dev: &'a T,
direction: Direction,
channel: usize,
}
impl<'a, T: AsyncDcOffsetControl + ?Sized> AsyncDcOffset<'a, T> {
fn new(dev: &'a T, direction: Direction, channel: usize) -> Self {
Self {
dev,
direction,
channel,
}
}
async fn ensure_available(&self) -> Result<(), Error> {
if self
.dev
.async_dc_offset_available(self.direction, self.channel)
.await?
{
Ok(())
} else {
Err(Error::unsupported(Capability::DcOffset))
}
}
pub fn enabled(&self) -> impl Future<Output = Result<bool, Error>> + MaybeSend + '_ {
async move {
self.ensure_available().await?;
self.dev
.async_dc_offset_enabled(self.direction, self.channel)
.await
}
}
pub fn enable(&self) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_ {
self.set_enabled(true)
}
pub fn disable(&self) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_ {
self.set_enabled(false)
}
pub fn set_enabled(
&self,
enabled: bool,
) -> impl Future<Output = Result<(), Error>> + MaybeSend + '_ {
async move {
self.ensure_available().await?;
self.dev
.async_set_dc_offset_enabled(self.direction, self.channel, enabled)
.await
}
}
}
#[derive(Clone)]
pub struct AsyncDevice<T> {
dev: T,
}
impl<T: AsyncDeviceInfo + Clone> AsyncDevice<T> {
pub fn from_impl(dev: T) -> Self {
Self { dev }
}
pub fn as_inner(&self) -> &T {
&self.dev
}
pub fn as_inner_mut(&mut self) -> &mut T {
&mut self.dev
}
pub fn into_inner(self) -> T {
self.dev
}
}
impl<T> AsyncDevice<T>
where
T: AsyncTypedDeviceBackend,
{
pub async fn from_args<A>(args: A) -> Result<Self, Error>
where
A: TryInto<Args> + MaybeSend + 'static,
{
let args = args
.try_into()
.map_err(|_| Error::invalid_argument("args", "failed to convert args"))?;
match args.get::<Driver>("driver") {
Ok(driver) if driver != <T as AsyncTypedDeviceBackend>::driver() => {
return Err(Error::DriverMismatch {
expected: <T as AsyncTypedDeviceBackend>::driver(),
requested: driver,
});
}
Ok(_) | Err(Error::MissingArgument { .. }) => {}
Err(e) => return Err(e),
}
Ok(Self::from_impl(T::async_open(&args).await?))
}
}
impl<T> AsyncDevice<T>
where
T: DynAsyncDeviceBackend + Clone + 'static,
{
pub fn to_dyn(&self) -> DynAsyncDevice {
DynAsyncDevice::from_impl(self.dev.clone())
}
pub fn into_dyn(self) -> DynAsyncDevice {
DynAsyncDevice::from_impl(self.dev)
}
}
impl<T> AsyncDevice<T>
where
T: DynAsyncDeviceBackend,
{
pub fn capabilities(
&self,
) -> impl Future<Output = Result<DeviceCapabilities, Error>> + MaybeSend + '_ {
async_device_capabilities(&self.dev)
}
}
impl<T: AsyncDeviceInfo> AsyncDevice<T> {
pub fn driver(&self) -> Driver {
self.dev.driver()
}
pub fn id(&self) -> impl Future<Output = Result<String, Error>> + MaybeSend + '_ {
self.dev.async_id()
}
pub fn info(&self) -> impl Future<Output = Result<Args, Error>> + MaybeSend + '_ {
self.dev.async_info()
}
pub fn num_channels(
&self,
direction: Direction,
) -> impl Future<Output = Result<usize, Error>> + MaybeSend + '_ {
self.dev.async_num_channels(direction)
}
pub fn full_duplex(&self) -> impl Future<Output = Result<bool, Error>> + MaybeSend + '_ {
self.dev.async_full_duplex()
}
}
impl<T: AsyncDeviceInfo> AsyncDeviceInfo for AsyncDevice<T> {
fn driver(&self) -> Driver {
self.dev.driver()
}
async fn async_id(&self) -> Result<String, Error> {
self.dev.async_id().await
}
async fn async_info(&self) -> Result<Args, Error> {
self.dev.async_info().await
}
async fn async_num_channels(&self, direction: Direction) -> Result<usize, Error> {
self.dev.async_num_channels(direction).await
}
async fn async_full_duplex(&self) -> Result<bool, Error> {
self.dev.async_full_duplex().await
}
}
impl<T: AsyncRxDevice> AsyncRxDevice for AsyncDevice<T> {
type RxStreamer = T::RxStreamer;
async fn async_rx_streamer(
&self,
channels: &[usize],
args: Args,
) -> Result<Self::RxStreamer, Error> {
self.dev.async_rx_streamer(channels, args).await
}
}
impl<T: AsyncTxDevice> AsyncTxDevice for AsyncDevice<T> {
type TxStreamer = T::TxStreamer;
async fn async_tx_streamer(
&self,
channels: &[usize],
args: Args,
) -> Result<Self::TxStreamer, Error> {
self.dev.async_tx_streamer(channels, args).await
}
}
impl<T: AsyncAntennaControl> AsyncAntennaControl for AsyncDevice<T> {
async fn async_antennas(
&self,
direction: Direction,
channel: usize,
) -> Result<Vec<String>, Error> {
self.dev.async_antennas(direction, channel).await
}
async fn async_antenna(&self, direction: Direction, channel: usize) -> Result<String, Error> {
self.dev.async_antenna(direction, channel).await
}
async fn async_set_antenna(
&self,
direction: Direction,
channel: usize,
name: &str,
) -> Result<(), Error> {
self.dev.async_set_antenna(direction, channel, name).await
}
}
impl<T: AsyncAgcControl> AsyncAgcControl for AsyncDevice<T> {
async fn async_agc_available(
&self,
direction: Direction,
channel: usize,
) -> Result<bool, Error> {
self.dev.async_agc_available(direction, channel).await
}
async fn async_agc_enabled(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
self.dev.async_agc_enabled(direction, channel).await
}
async fn async_set_agc_enabled(
&self,
direction: Direction,
channel: usize,
enabled: bool,
) -> Result<(), Error> {
self.dev
.async_set_agc_enabled(direction, channel, enabled)
.await
}
}
impl<T: AsyncGainControl> AsyncGainControl for AsyncDevice<T> {
async fn async_gain_elements(
&self,
direction: Direction,
channel: usize,
) -> Result<Vec<String>, Error> {
self.dev.async_gain_elements(direction, channel).await
}
async fn async_set_gain(
&self,
direction: Direction,
channel: usize,
gain: f64,
) -> Result<(), Error> {
self.dev.async_set_gain(direction, channel, gain).await
}
async fn async_gain(&self, direction: Direction, channel: usize) -> Result<Option<f64>, Error> {
self.dev.async_gain(direction, channel).await
}
async fn async_gain_range(&self, direction: Direction, channel: usize) -> Result<Range, Error> {
self.dev.async_gain_range(direction, channel).await
}
async fn async_set_gain_element(
&self,
direction: Direction,
channel: usize,
name: &str,
gain: f64,
) -> Result<(), Error> {
self.dev
.async_set_gain_element(direction, channel, name, gain)
.await
}
async fn async_gain_element(
&self,
direction: Direction,
channel: usize,
name: &str,
) -> Result<Option<f64>, Error> {
self.dev.async_gain_element(direction, channel, name).await
}
async fn async_gain_element_range(
&self,
direction: Direction,
channel: usize,
name: &str,
) -> Result<Range, Error> {
self.dev
.async_gain_element_range(direction, channel, name)
.await
}
}
impl<T: AsyncFrequencyControl> AsyncFrequencyControl for AsyncDevice<T> {
async fn async_frequency_range(
&self,
direction: Direction,
channel: usize,
) -> Result<Range, Error> {
self.dev.async_frequency_range(direction, channel).await
}
async fn async_frequency(&self, direction: Direction, channel: usize) -> Result<f64, Error> {
self.dev.async_frequency(direction, channel).await
}
async fn async_set_frequency(
&self,
direction: Direction,
channel: usize,
frequency: f64,
args: Args,
) -> Result<(), Error> {
self.dev
.async_set_frequency(direction, channel, frequency, args)
.await
}
async fn async_frequency_components(
&self,
direction: Direction,
channel: usize,
) -> Result<Vec<String>, Error> {
self.dev
.async_frequency_components(direction, channel)
.await
}
async fn async_component_frequency_range(
&self,
direction: Direction,
channel: usize,
name: &str,
) -> Result<Range, Error> {
self.dev
.async_component_frequency_range(direction, channel, name)
.await
}
async fn async_component_frequency(
&self,
direction: Direction,
channel: usize,
name: &str,
) -> Result<f64, Error> {
self.dev
.async_component_frequency(direction, channel, name)
.await
}
async fn async_set_component_frequency(
&self,
direction: Direction,
channel: usize,
name: &str,
frequency: f64,
) -> Result<(), Error> {
self.dev
.async_set_component_frequency(direction, channel, name, frequency)
.await
}
}
impl<T: AsyncSampleRateControl> AsyncSampleRateControl for AsyncDevice<T> {
async fn async_sample_rate(&self, direction: Direction, channel: usize) -> Result<f64, Error> {
self.dev.async_sample_rate(direction, channel).await
}
async fn async_set_sample_rate(
&self,
direction: Direction,
channel: usize,
rate: f64,
) -> Result<(), Error> {
self.dev
.async_set_sample_rate(direction, channel, rate)
.await
}
async fn async_get_sample_rate_range(
&self,
direction: Direction,
channel: usize,
) -> Result<Range, Error> {
self.dev
.async_get_sample_rate_range(direction, channel)
.await
}
}
impl<T: AsyncBandwidthControl> AsyncBandwidthControl for AsyncDevice<T> {
async fn async_bandwidth(&self, direction: Direction, channel: usize) -> Result<f64, Error> {
self.dev.async_bandwidth(direction, channel).await
}
async fn async_set_bandwidth(
&self,
direction: Direction,
channel: usize,
bandwidth: f64,
) -> Result<(), Error> {
self.dev
.async_set_bandwidth(direction, channel, bandwidth)
.await
}
async fn async_get_bandwidth_range(
&self,
direction: Direction,
channel: usize,
) -> Result<Range, Error> {
self.dev.async_get_bandwidth_range(direction, channel).await
}
}
impl<T: AsyncDcOffsetControl> AsyncDcOffsetControl for AsyncDevice<T> {
async fn async_dc_offset_available(
&self,
direction: Direction,
channel: usize,
) -> Result<bool, Error> {
self.dev.async_dc_offset_available(direction, channel).await
}
async fn async_dc_offset_enabled(
&self,
direction: Direction,
channel: usize,
) -> Result<bool, Error> {
self.dev.async_dc_offset_enabled(direction, channel).await
}
async fn async_set_dc_offset_enabled(
&self,
direction: Direction,
channel: usize,
enabled: bool,
) -> Result<(), Error> {
self.dev
.async_set_dc_offset_enabled(direction, channel, enabled)
.await
}
}
impl DynAsyncDevice {
pub async fn new() -> Result<Self, Error> {
let registry = AsyncRegistry::default();
let descriptors = registry.probe(Args::new()).await?;
let descriptor = descriptors.first().ok_or(Error::DeviceNotFound)?;
registry.open(descriptor).await
}
pub async fn from_args<A>(args: A) -> Result<Self, Error>
where
A: TryInto<Args> + MaybeSend + 'static,
{
let registry = AsyncRegistry::default();
registry.open_args(args).await
}
pub fn from_impl<T: DynAsyncDeviceBackend + Clone + 'static>(dev: T) -> Self {
Self {
inner: Shared::new(dev),
}
}
pub fn as_backend(&self) -> &dyn DynAsyncDeviceBackend {
self.inner.as_ref()
}
pub fn downcast_ref<D: DynAsyncDeviceBackend + 'static>(&self) -> Option<&D> {
let backend: &dyn DynAsyncDeviceBackend = self.inner.as_ref();
let any: &dyn Any = backend;
any.downcast_ref::<D>()
}
pub fn downcast_mut<D: DynAsyncDeviceBackend + 'static>(&mut self) -> Option<&mut D> {
let backend: &mut dyn DynAsyncDeviceBackend = Shared::get_mut(&mut self.inner)?;
let any: &mut dyn Any = backend;
any.downcast_mut::<D>()
}
pub fn driver(&self) -> Driver {
self.inner.dyn_driver()
}
pub fn id(&self) -> impl Future<Output = Result<String, Error>> + MaybeSend + '_ {
self.inner.dyn_id()
}
pub fn info(&self) -> impl Future<Output = Result<Args, Error>> + MaybeSend + '_ {
self.inner.dyn_info()
}
pub fn num_channels(
&self,
direction: Direction,
) -> impl Future<Output = Result<usize, Error>> + MaybeSend + '_ {
self.inner.dyn_num_channels(direction)
}
pub fn full_duplex(&self) -> impl Future<Output = Result<bool, Error>> + MaybeSend + '_ {
self.inner.dyn_full_duplex()
}
pub fn capabilities(
&self,
) -> impl Future<Output = Result<DeviceCapabilities, Error>> + MaybeSend + '_ {
async_device_capabilities(self.inner.as_ref())
}
pub async fn rx(&self, index: usize) -> Result<AsyncRxChannel<'_, Self>, Error> {
async_ensure_channel(self, Direction::Rx, index).await?;
Ok(AsyncRxChannel::new(self, index))
}
pub async fn tx(&self, index: usize) -> Result<AsyncTxChannel<'_, Self>, Error> {
async_ensure_channel(self, Direction::Tx, index).await?;
Ok(AsyncTxChannel::new(self, index))
}
pub fn rx_streamer<'a>(
&'a self,
channels: &'a [usize],
) -> impl Future<Output = Result<DynAsyncRxStreamer, Error>> + MaybeSend + 'a {
self.rx_streamer_with_args(channels, Args::new())
}
pub async fn rx_streamer_with_args<'a, A>(
&'a self,
channels: &'a [usize],
args: A,
) -> Result<DynAsyncRxStreamer, Error>
where
A: TryInto<Args> + MaybeSend + 'a,
{
for channel in channels {
async_ensure_channel(self, Direction::Rx, *channel).await?;
}
<Self as AsyncRxDevice>::async_rx_streamer(
self,
channels,
args.try_into()
.map_err(|_| Error::invalid_argument("args", "failed to convert args"))?,
)
.await
}
pub fn tx_streamer<'a>(
&'a self,
channels: &'a [usize],
) -> impl Future<Output = Result<DynAsyncTxStreamer, Error>> + MaybeSend + 'a {
self.tx_streamer_with_args(channels, Args::new())
}
pub async fn tx_streamer_with_args<'a, A>(
&'a self,
channels: &'a [usize],
args: A,
) -> Result<DynAsyncTxStreamer, Error>
where
A: TryInto<Args> + MaybeSend + 'a,
{
for channel in channels {
async_ensure_channel(self, Direction::Tx, *channel).await?;
}
<Self as AsyncTxDevice>::async_tx_streamer(
self,
channels,
args.try_into()
.map_err(|_| Error::invalid_argument("args", "failed to convert args"))?,
)
.await
}
}
impl AsyncDeviceInfo for DynAsyncDevice {
fn driver(&self) -> Driver {
self.inner.dyn_driver()
}
async fn async_id(&self) -> Result<String, Error> {
self.inner.dyn_id().await
}
async fn async_info(&self) -> Result<Args, Error> {
self.inner.dyn_info().await
}
async fn async_num_channels(&self, direction: Direction) -> Result<usize, Error> {
self.inner.dyn_num_channels(direction).await
}
async fn async_full_duplex(&self) -> Result<bool, Error> {
self.inner.dyn_full_duplex().await
}
}
impl AsyncRxDevice for DynAsyncDevice {
type RxStreamer = DynAsyncRxStreamer;
async fn async_rx_streamer(
&self,
channels: &[usize],
args: Args,
) -> Result<Self::RxStreamer, Error> {
self.inner
.async_rx_device()
.ok_or_else(|| Error::unsupported(Capability::RxStreaming))?
.dyn_rx_streamer(channels, args)
.await
}
}
impl AsyncTxDevice for DynAsyncDevice {
type TxStreamer = DynAsyncTxStreamer;
async fn async_tx_streamer(
&self,
channels: &[usize],
args: Args,
) -> Result<Self::TxStreamer, Error> {
self.inner
.async_tx_device()
.ok_or_else(|| Error::unsupported(Capability::TxStreaming))?
.dyn_tx_streamer(channels, args)
.await
}
}
macro_rules! impl_dyn_control {
($trait_name:ident, $accessor:ident, $cap:expr, $($body:item),* $(,)?) => {
impl $trait_name for DynAsyncDevice {
$($body)*
}
};
}
impl_dyn_control!(
AsyncAntennaControl,
async_antenna_control,
Capability::Antenna,
async fn async_antennas(
&self,
direction: Direction,
channel: usize,
) -> Result<Vec<String>, Error> {
self.inner
.async_antenna_control()
.ok_or_else(|| Error::unsupported(Capability::Antenna))?
.dyn_antennas(direction, channel)
.await
},
async fn async_antenna(&self, direction: Direction, channel: usize) -> Result<String, Error> {
self.inner
.async_antenna_control()
.ok_or_else(|| Error::unsupported(Capability::Antenna))?
.dyn_antenna(direction, channel)
.await
},
async fn async_set_antenna(
&self,
direction: Direction,
channel: usize,
name: &str,
) -> Result<(), Error> {
self.inner
.async_antenna_control()
.ok_or_else(|| Error::unsupported(Capability::Antenna))?
.dyn_set_antenna(direction, channel, name)
.await
}
);
impl AsyncAgcControl for DynAsyncDevice {
async fn async_agc_available(
&self,
direction: Direction,
channel: usize,
) -> Result<bool, Error> {
self.inner
.async_agc_control()
.ok_or_else(|| Error::unsupported(Capability::Agc))?
.dyn_agc_available(direction, channel)
.await
}
async fn async_agc_enabled(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
self.inner
.async_agc_control()
.ok_or_else(|| Error::unsupported(Capability::Agc))?
.dyn_agc_enabled(direction, channel)
.await
}
async fn async_set_agc_enabled(
&self,
direction: Direction,
channel: usize,
enabled: bool,
) -> Result<(), Error> {
self.inner
.async_agc_control()
.ok_or_else(|| Error::unsupported(Capability::Agc))?
.dyn_set_agc_enabled(direction, channel, enabled)
.await
}
}
impl AsyncGainControl for DynAsyncDevice {
async fn async_gain_elements(
&self,
direction: Direction,
channel: usize,
) -> Result<Vec<String>, Error> {
self.inner
.async_gain_control()
.ok_or_else(|| Error::unsupported(Capability::Gain))?
.dyn_gain_elements(direction, channel)
.await
}
async fn async_set_gain(
&self,
direction: Direction,
channel: usize,
gain: f64,
) -> Result<(), Error> {
self.inner
.async_gain_control()
.ok_or_else(|| Error::unsupported(Capability::Gain))?
.dyn_set_gain(direction, channel, gain)
.await
}
async fn async_gain(&self, direction: Direction, channel: usize) -> Result<Option<f64>, Error> {
self.inner
.async_gain_control()
.ok_or_else(|| Error::unsupported(Capability::Gain))?
.dyn_gain(direction, channel)
.await
}
async fn async_gain_range(&self, direction: Direction, channel: usize) -> Result<Range, Error> {
self.inner
.async_gain_control()
.ok_or_else(|| Error::unsupported(Capability::Gain))?
.dyn_gain_range(direction, channel)
.await
}
async fn async_set_gain_element(
&self,
direction: Direction,
channel: usize,
name: &str,
gain: f64,
) -> Result<(), Error> {
self.inner
.async_gain_control()
.ok_or_else(|| Error::unsupported(Capability::Gain))?
.dyn_set_gain_element(direction, channel, name, gain)
.await
}
async fn async_gain_element(
&self,
direction: Direction,
channel: usize,
name: &str,
) -> Result<Option<f64>, Error> {
self.inner
.async_gain_control()
.ok_or_else(|| Error::unsupported(Capability::Gain))?
.dyn_gain_element(direction, channel, name)
.await
}
async fn async_gain_element_range(
&self,
direction: Direction,
channel: usize,
name: &str,
) -> Result<Range, Error> {
self.inner
.async_gain_control()
.ok_or_else(|| Error::unsupported(Capability::Gain))?
.dyn_gain_element_range(direction, channel, name)
.await
}
}
impl AsyncFrequencyControl for DynAsyncDevice {
async fn async_frequency_range(
&self,
direction: Direction,
channel: usize,
) -> Result<Range, Error> {
self.inner
.async_frequency_control()
.ok_or_else(|| Error::unsupported(Capability::Frequency))?
.dyn_frequency_range(direction, channel)
.await
}
async fn async_frequency(&self, direction: Direction, channel: usize) -> Result<f64, Error> {
self.inner
.async_frequency_control()
.ok_or_else(|| Error::unsupported(Capability::Frequency))?
.dyn_frequency(direction, channel)
.await
}
async fn async_set_frequency(
&self,
direction: Direction,
channel: usize,
frequency: f64,
args: Args,
) -> Result<(), Error> {
self.inner
.async_frequency_control()
.ok_or_else(|| Error::unsupported(Capability::Frequency))?
.dyn_set_frequency(direction, channel, frequency, args)
.await
}
async fn async_frequency_components(
&self,
direction: Direction,
channel: usize,
) -> Result<Vec<String>, Error> {
self.inner
.async_frequency_control()
.ok_or_else(|| Error::unsupported(Capability::Frequency))?
.dyn_frequency_components(direction, channel)
.await
}
async fn async_component_frequency_range(
&self,
direction: Direction,
channel: usize,
name: &str,
) -> Result<Range, Error> {
self.inner
.async_frequency_control()
.ok_or_else(|| Error::unsupported(Capability::Frequency))?
.dyn_component_frequency_range(direction, channel, name)
.await
}
async fn async_component_frequency(
&self,
direction: Direction,
channel: usize,
name: &str,
) -> Result<f64, Error> {
self.inner
.async_frequency_control()
.ok_or_else(|| Error::unsupported(Capability::Frequency))?
.dyn_component_frequency(direction, channel, name)
.await
}
async fn async_set_component_frequency(
&self,
direction: Direction,
channel: usize,
name: &str,
frequency: f64,
) -> Result<(), Error> {
self.inner
.async_frequency_control()
.ok_or_else(|| Error::unsupported(Capability::Frequency))?
.dyn_set_component_frequency(direction, channel, name, frequency)
.await
}
}
impl AsyncSampleRateControl for DynAsyncDevice {
async fn async_sample_rate(&self, direction: Direction, channel: usize) -> Result<f64, Error> {
self.inner
.async_sample_rate_control()
.ok_or_else(|| Error::unsupported(Capability::SampleRate))?
.dyn_sample_rate(direction, channel)
.await
}
async fn async_set_sample_rate(
&self,
direction: Direction,
channel: usize,
rate: f64,
) -> Result<(), Error> {
self.inner
.async_sample_rate_control()
.ok_or_else(|| Error::unsupported(Capability::SampleRate))?
.dyn_set_sample_rate(direction, channel, rate)
.await
}
async fn async_get_sample_rate_range(
&self,
direction: Direction,
channel: usize,
) -> Result<Range, Error> {
self.inner
.async_sample_rate_control()
.ok_or_else(|| Error::unsupported(Capability::SampleRate))?
.dyn_get_sample_rate_range(direction, channel)
.await
}
}
impl AsyncBandwidthControl for DynAsyncDevice {
async fn async_bandwidth(&self, direction: Direction, channel: usize) -> Result<f64, Error> {
self.inner
.async_bandwidth_control()
.ok_or_else(|| Error::unsupported(Capability::Bandwidth))?
.dyn_bandwidth(direction, channel)
.await
}
async fn async_set_bandwidth(
&self,
direction: Direction,
channel: usize,
bandwidth: f64,
) -> Result<(), Error> {
self.inner
.async_bandwidth_control()
.ok_or_else(|| Error::unsupported(Capability::Bandwidth))?
.dyn_set_bandwidth(direction, channel, bandwidth)
.await
}
async fn async_get_bandwidth_range(
&self,
direction: Direction,
channel: usize,
) -> Result<Range, Error> {
self.inner
.async_bandwidth_control()
.ok_or_else(|| Error::unsupported(Capability::Bandwidth))?
.dyn_get_bandwidth_range(direction, channel)
.await
}
}
impl AsyncDcOffsetControl for DynAsyncDevice {
async fn async_dc_offset_available(
&self,
direction: Direction,
channel: usize,
) -> Result<bool, Error> {
self.inner
.async_dc_offset_control()
.ok_or_else(|| Error::unsupported(Capability::DcOffset))?
.dyn_dc_offset_available(direction, channel)
.await
}
async fn async_dc_offset_enabled(
&self,
direction: Direction,
channel: usize,
) -> Result<bool, Error> {
self.inner
.async_dc_offset_control()
.ok_or_else(|| Error::unsupported(Capability::DcOffset))?
.dyn_dc_offset_enabled(direction, channel)
.await
}
async fn async_set_dc_offset_enabled(
&self,
direction: Direction,
channel: usize,
enabled: bool,
) -> Result<(), Error> {
self.inner
.async_dc_offset_control()
.ok_or_else(|| Error::unsupported(Capability::DcOffset))?
.dyn_set_dc_offset_enabled(direction, channel, enabled)
.await
}
}
impl<T: AsyncDeviceInfo> AsyncDevice<T> {
pub async fn rx(&self, index: usize) -> Result<AsyncRxChannel<'_, T>, Error> {
async_ensure_channel(&self.dev, Direction::Rx, index).await?;
Ok(AsyncRxChannel::new(&self.dev, index))
}
pub async fn tx(&self, index: usize) -> Result<AsyncTxChannel<'_, T>, Error> {
async_ensure_channel(&self.dev, Direction::Tx, index).await?;
Ok(AsyncTxChannel::new(&self.dev, index))
}
}
async fn async_ensure_channel<T>(dev: &T, direction: Direction, channel: usize) -> Result<(), Error>
where
T: AsyncDeviceInfo + ?Sized,
{
let available = dev.async_num_channels(direction).await?;
if channel < available {
Ok(())
} else {
Err(Error::invalid_channel(direction, channel, available))
}
}
impl<T: AsyncRxDevice> AsyncDevice<T> {
pub fn rx_streamer<'a>(
&'a self,
channels: &'a [usize],
) -> impl Future<Output = Result<T::RxStreamer, Error>> + MaybeSend + 'a {
self.rx_streamer_with_args(channels, Args::new())
}
pub async fn rx_streamer_with_args<'a>(
&'a self,
channels: &'a [usize],
args: Args,
) -> Result<T::RxStreamer, Error> {
for channel in channels {
async_ensure_channel(&self.dev, Direction::Rx, *channel).await?;
}
self.dev.async_rx_streamer(channels, args).await
}
}
impl<T: AsyncTxDevice> AsyncDevice<T> {
pub fn tx_streamer<'a>(
&'a self,
channels: &'a [usize],
) -> impl Future<Output = Result<T::TxStreamer, Error>> + MaybeSend + 'a {
self.tx_streamer_with_args(channels, Args::new())
}
pub async fn tx_streamer_with_args<'a>(
&'a self,
channels: &'a [usize],
args: Args,
) -> Result<T::TxStreamer, Error> {
for channel in channels {
async_ensure_channel(&self.dev, Direction::Tx, *channel).await?;
}
self.dev.async_tx_streamer(channels, args).await
}
}
impl<'a, T: AsyncRxDevice + ?Sized> AsyncRxChannel<'a, T> {
pub fn streamer(&self) -> impl Future<Output = Result<T::RxStreamer, Error>> + MaybeSend + '_ {
self.streamer_with_args(Args::new())
}
pub fn streamer_with_args(
&self,
args: Args,
) -> impl Future<Output = Result<T::RxStreamer, Error>> + MaybeSend + '_ {
async move {
let channels = [self.channel];
self.dev.async_rx_streamer(&channels, args).await
}
}
}
impl<'a, T: AsyncTxDevice + ?Sized> AsyncTxChannel<'a, T> {
pub fn streamer(&self) -> impl Future<Output = Result<T::TxStreamer, Error>> + MaybeSend + '_ {
self.streamer_with_args(Args::new())
}
pub fn streamer_with_args(
&self,
args: Args,
) -> impl Future<Output = Result<T::TxStreamer, Error>> + MaybeSend + '_ {
async move {
let channels = [self.channel];
self.dev.async_tx_streamer(&channels, args).await
}
}
}
macro_rules! impl_async_channel_controls {
($channel:ident, $direction:expr) => {
impl<'a, T: AsyncAntennaControl + ?Sized> $channel<'a, T> {
pub fn antenna(&self) -> AsyncAntenna<'_, T> {
AsyncAntenna::new(self.dev, $direction, self.channel)
}
}
impl<'a, T: AsyncAgcControl + ?Sized> $channel<'a, T> {
pub fn agc(&self) -> AsyncAgc<'_, T> {
AsyncAgc::new(self.dev, $direction, self.channel)
}
}
impl<'a, T: AsyncGainControl + ?Sized> $channel<'a, T> {
pub fn gain(&self) -> AsyncGain<'_, T> {
AsyncGain::new(self.dev, $direction, self.channel)
}
}
impl<'a, T: AsyncFrequencyControl + ?Sized> $channel<'a, T> {
pub fn frequency(&self) -> AsyncFrequency<'_, T> {
AsyncFrequency::new(self.dev, $direction, self.channel)
}
}
impl<'a, T: AsyncSampleRateControl + ?Sized> $channel<'a, T> {
pub fn sample_rate(&self) -> AsyncSampleRate<'_, T> {
AsyncSampleRate::new(self.dev, $direction, self.channel)
}
}
impl<'a, T: AsyncBandwidthControl + ?Sized> $channel<'a, T> {
pub fn bandwidth(&self) -> AsyncBandwidth<'_, T> {
AsyncBandwidth::new(self.dev, $direction, self.channel)
}
}
impl<'a, T: AsyncDcOffsetControl + ?Sized> $channel<'a, T> {
pub fn dc_offset(&self) -> AsyncDcOffset<'_, T> {
AsyncDcOffset::new(self.dev, $direction, self.channel)
}
}
};
}
impl_async_channel_controls!(AsyncRxChannel, Direction::Rx);
impl_async_channel_controls!(AsyncTxChannel, Direction::Tx);
#[cfg(target_arch = "wasm32")]
#[allow(dead_code)]
mod wasm_non_send_compile_check {
use super::*;
use num_complex::Complex32;
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Clone)]
struct LocalOnly {
reads: Rc<RefCell<usize>>,
}
struct LocalRxStreamer {
reads: Rc<RefCell<usize>>,
}
impl AsyncDeviceInfo for LocalOnly {
fn driver(&self) -> Driver {
Driver::Dummy
}
async fn async_id(&self) -> Result<String, Error> {
Ok("wasm-local".to_string())
}
async fn async_info(&self) -> Result<Args, Error> {
Ok(Args::new())
}
async fn async_num_channels(&self, direction: Direction) -> Result<usize, Error> {
Ok(match direction {
Direction::Rx => 1,
Direction::Tx => 0,
})
}
async fn async_full_duplex(&self) -> Result<bool, Error> {
Ok(false)
}
}
crate::impl_dyn_async_device_backend!(LocalOnly => [rx]);
impl AsyncRxDevice for LocalOnly {
type RxStreamer = LocalRxStreamer;
async fn async_rx_streamer(
&self,
channels: &[usize],
_args: Args,
) -> Result<Self::RxStreamer, Error> {
match channels {
&[0] => Ok(LocalRxStreamer {
reads: self.reads.clone(),
}),
_ => Err(Error::invalid_argument("channels", "unsupported channel")),
}
}
}
impl AsyncRxStreamer for LocalRxStreamer {
async fn mtu(&self) -> Result<usize, Error> {
Ok(4)
}
async fn activate_at(&mut self, _time_ns: Option<i64>) -> Result<(), Error> {
Ok(())
}
async fn deactivate_at(&mut self, _time_ns: Option<i64>) -> Result<(), Error> {
Ok(())
}
async fn read<'a>(
&'a mut self,
buffers: &'a mut [&'a mut [Complex32]],
_timeout_us: i64,
) -> Result<usize, Error> {
*self.reads.borrow_mut() += 1;
for buffer in buffers.iter_mut() {
buffer.fill(Complex32::new(0.0, 0.0));
}
Ok(buffers.first().map(|buffer| buffer.len()).unwrap_or(0))
}
}
fn accepts_non_send_async_backend() {
let _dev = DynAsyncDevice::from_impl(LocalOnly {
reads: Rc::new(RefCell::new(0)),
});
}
}
#[cfg(all(test, feature = "dummy"))]
mod tests {
use super::*;
use futures::executor::block_on;
use num_complex::Complex32;
#[test]
fn async_dyn_dummy_controls_and_streams() {
block_on(async {
let dev = DynAsyncDevice::from_args("driver=dummy").await.unwrap();
let rx0 = dev.rx(0).await.unwrap();
rx0.sample_rate().set(1.0e6).await.unwrap();
rx0.frequency().set(100.0e6).await.unwrap();
rx0.agc().disable().await.unwrap();
rx0.gain().set(12.0).await.unwrap();
assert_eq!(rx0.sample_rate().value().await.unwrap(), 1.0e6);
assert_eq!(rx0.frequency().value().await.unwrap(), 100.0e6);
assert_eq!(rx0.gain().value().await.unwrap(), Some(12.0));
let mut samples = [Complex32::new(1.0, 1.0); 16];
let mut rx = rx0.streamer().await.unwrap();
rx.activate().await.unwrap();
let n = rx.read(&mut [&mut samples], 200_000).await.unwrap();
assert_eq!(n, samples.len());
assert!(samples
.iter()
.all(|sample| *sample == Complex32::new(0.0, 0.0)));
let tx0 = dev.tx(0).await.unwrap();
let mut tx = tx0.streamer().await.unwrap();
tx.activate().await.unwrap();
let n = tx.write(&[&samples], None, true, 200_000).await.unwrap();
assert_eq!(n, samples.len());
});
}
#[test]
fn async_typed_dummy_converts_and_reports_capabilities() {
block_on(async {
let dev = AsyncDevice::<crate::impls::Dummy>::from_args("driver=dummy")
.await
.unwrap();
fn assert_typed_capabilities<D>(_dev: &D)
where
D: AsyncDeviceInfo
+ AsyncRxDevice
+ AsyncTxDevice
+ AsyncAntennaControl
+ AsyncAgcControl
+ AsyncGainControl
+ AsyncFrequencyControl
+ AsyncSampleRateControl
+ AsyncBandwidthControl,
{
}
assert_typed_capabilities(&dev);
assert!(dev.capabilities().await.unwrap().full_duplex);
let dyn_dev = dev.to_dyn();
dev.rx(0)
.await
.unwrap()
.frequency()
.set(100.0e6)
.await
.unwrap();
assert_eq!(
dyn_dev
.rx(0)
.await
.unwrap()
.frequency()
.value()
.await
.unwrap(),
100.0e6
);
let dev = dev.into_dyn();
assert_eq!(dev.driver(), Driver::Dummy);
assert!(dev.full_duplex().await.unwrap());
assert!(dev.downcast_ref::<crate::impls::Dummy>().is_some());
let capabilities = dev.capabilities().await.unwrap();
assert!(capabilities.full_duplex);
assert_eq!(capabilities.rx_channels.len(), 1);
assert_eq!(capabilities.tx_channels.len(), 1);
assert_eq!(
capabilities.rx_channels[0].controls.antennas,
Some(vec!["A".to_string()])
);
assert!(capabilities.rx_channels[0].controls.agc);
});
}
}