seify/
device.rs

1#![allow(dead_code)]
2#![allow(unused_variables)]
3use std::any::Any;
4use std::sync::Arc;
5
6use crate::Args;
7use crate::Direction;
8use crate::Driver;
9use crate::Error;
10use crate::Range;
11use crate::RxStreamer;
12use crate::TxStreamer;
13
14/// Central trait, implemented by hardware drivers.
15pub trait DeviceTrait: Any + Send {
16    /// Associated RX streamer
17    type RxStreamer: RxStreamer;
18    /// Associated TX streamer
19    type TxStreamer: TxStreamer;
20
21    /// Cast to Any for downcasting.
22    fn as_any(&self) -> &dyn Any;
23    /// Cast to Any for downcasting to a mutable reference.
24    fn as_any_mut(&mut self) -> &mut dyn Any;
25
26    /// SDR [driver](Driver)
27    fn driver(&self) -> Driver;
28    /// Identifier for the device, e.g., its serial.
29    fn id(&self) -> Result<String, Error>;
30    /// Device info that can be displayed to the user.
31    fn info(&self) -> Result<Args, Error>;
32    /// Number of supported Channels.
33    fn num_channels(&self, direction: Direction) -> Result<usize, Error>;
34    /// Full Duplex support.
35    fn full_duplex(&self, direction: Direction, channel: usize) -> Result<bool, Error>;
36
37    //================================ STREAMER ============================================
38    /// Create an RX streamer.
39    fn rx_streamer(&self, channels: &[usize], args: Args) -> Result<Self::RxStreamer, Error>;
40    /// Create a TX streamer.
41    fn tx_streamer(&self, channels: &[usize], args: Args) -> Result<Self::TxStreamer, Error>;
42
43    //================================ ANTENNA ============================================
44    /// List of available antenna ports.
45    fn antennas(&self, direction: Direction, channel: usize) -> Result<Vec<String>, Error>;
46    /// Currently used antenna port.
47    fn antenna(&self, direction: Direction, channel: usize) -> Result<String, Error>;
48    /// Set antenna port.
49    fn set_antenna(&self, direction: Direction, channel: usize, name: &str) -> Result<(), Error>;
50
51    //================================ AGC ============================================
52    /// Does the device support automatic gain control?
53    fn supports_agc(&self, direction: Direction, channel: usize) -> Result<bool, Error>;
54
55    /// Enable or disable automatic gain control.
56    fn enable_agc(&self, direction: Direction, channel: usize, agc: bool) -> Result<(), Error>;
57
58    /// Returns true, if automatic gain control is enabled
59    fn agc(&self, direction: Direction, channel: usize) -> Result<bool, Error>;
60
61    //================================ GAIN ============================================
62    /// List of available gain elements.
63    ///
64    /// Elements should be in order RF to baseband.
65    fn gain_elements(&self, direction: Direction, channel: usize) -> Result<Vec<String>, Error>;
66    /// Set the overall amplification in a chain.
67    ///
68    /// The gain will be distributed automatically across available elements.
69    ///
70    /// `gain`: the new amplification value in dB
71    fn set_gain(&self, direction: Direction, channel: usize, gain: f64) -> Result<(), Error>;
72
73    /// Get the overall value of the gain elements in a chain in dB.
74    fn gain(&self, direction: Direction, channel: usize) -> Result<Option<f64>, Error>;
75
76    /// Get the overall [`Range`] of possible gain values.
77    fn gain_range(&self, direction: Direction, channel: usize) -> Result<Range, Error>;
78
79    /// Set the value of a amplification element in a chain.
80    ///
81    /// ## Arguments
82    /// * `name`: the name of an amplification element from `Device::list_gains`
83    /// * `gain`: the new amplification value in dB
84    fn set_gain_element(
85        &self,
86        direction: Direction,
87        channel: usize,
88        name: &str,
89        gain: f64,
90    ) -> Result<(), Error>;
91
92    /// Get the value of an individual amplification element in a chain in dB.
93    fn gain_element(
94        &self,
95        direction: Direction,
96        channel: usize,
97        name: &str,
98    ) -> Result<Option<f64>, Error>;
99
100    /// Get the range of possible gain values for a specific element.
101    fn gain_element_range(
102        &self,
103        direction: Direction,
104        channel: usize,
105        name: &str,
106    ) -> Result<Range, Error>;
107
108    //================================ FREQUENCY ============================================
109
110    /// Get the ranges of overall frequency values.
111    fn frequency_range(&self, direction: Direction, channel: usize) -> Result<Range, Error>;
112
113    /// Get the overall center frequency of the chain.
114    ///
115    ///   - For RX, this specifies the down-conversion frequency.
116    ///   - For TX, this specifies the up-conversion frequency.
117    ///
118    /// Returns the center frequency in Hz.
119    fn frequency(&self, direction: Direction, channel: usize) -> Result<f64, Error>;
120
121    /// Set the center frequency of the chain.
122    ///
123    ///   - For RX, this specifies the down-conversion frequency.
124    ///   - For TX, this specifies the up-conversion frequency.
125    ///
126    /// The default implementation of `set_frequency` will tune the "RF"
127    /// component as close as possible to the requested center frequency in Hz.
128    /// Tuning inaccuracies will be compensated for with the "BB" component.
129    ///
130    /// The `args` can be used to augment the tuning algorithm.
131    ///
132    ///   - Use `"OFFSET"` to specify an "RF" tuning offset,
133    ///     usually with the intention of moving the LO out of the passband.
134    ///     The offset will be compensated for using the "BB" component.
135    ///   - Use the name of a component for the key and a frequency in Hz
136    ///     as the value (any format) to enforce a specific frequency.
137    ///     The other components will be tuned with compensation
138    ///     to achieve the specified overall frequency.
139    ///   - Use the name of a component for the key and the value `"IGNORE"`
140    ///     so that the tuning algorithm will avoid altering the component.
141    ///   - Vendor specific implementations can also use the same args to augment
142    ///     tuning in other ways such as specifying fractional vs integer N tuning.
143    ///
144    fn set_frequency(
145        &self,
146        direction: Direction,
147        channel: usize,
148        frequency: f64,
149        args: Args,
150    ) -> Result<(), Error>;
151
152    /// List available tunable elements in the chain.
153    ///
154    /// Elements should be in order RF to baseband.
155    fn frequency_components(
156        &self,
157        direction: Direction,
158        channel: usize,
159    ) -> Result<Vec<String>, Error>;
160
161    /// Get the range of tunable values for the specified element.
162    fn component_frequency_range(
163        &self,
164        direction: Direction,
165        channel: usize,
166        name: &str,
167    ) -> Result<Range, Error>;
168
169    /// Get the frequency of a tunable element in the chain.
170    fn component_frequency(
171        &self,
172        direction: Direction,
173        channel: usize,
174        name: &str,
175    ) -> Result<f64, Error>;
176
177    /// Tune the center frequency of the specified element.
178    ///
179    ///   - For RX, this specifies the down-conversion frequency.
180    ///   - For TX, this specifies the up-conversion frequency.
181    fn set_component_frequency(
182        &self,
183        direction: Direction,
184        channel: usize,
185        name: &str,
186        frequency: f64,
187    ) -> Result<(), Error>;
188
189    //================================ SAMPLE RATE ============================================
190
191    /// Get the baseband sample rate of the chain in samples per second.
192    fn sample_rate(&self, direction: Direction, channel: usize) -> Result<f64, Error>;
193
194    /// Set the baseband sample rate of the chain in samples per second.
195    fn set_sample_rate(&self, direction: Direction, channel: usize, rate: f64)
196        -> Result<(), Error>;
197
198    /// Get the range of possible baseband sample rates.
199    fn get_sample_rate_range(&self, direction: Direction, channel: usize) -> Result<Range, Error>;
200
201    //================================ BANDWIDTH ============================================
202
203    /// Get the hardware bandwidth filter, if available.
204    ///
205    /// Returns `Err(Error::NotSupported)` if unsupported in underlying driver.
206    fn bandwidth(&self, direction: Direction, channel: usize) -> Result<f64, Error>;
207
208    /// Set the hardware bandwidth filter, if available.
209    ///
210    /// Returns `Err(Error::NotSupported)` if unsupported in underlying driver.
211    fn set_bandwidth(&self, direction: Direction, channel: usize, bw: f64) -> Result<(), Error>;
212
213    /// Get the range of possible bandwidth filter values, if available.
214    ///
215    /// Returns `Err(Error::NotSupported)` if unsupported in underlying driver.
216    fn get_bandwidth_range(&self, direction: Direction, channel: usize) -> Result<Range, Error>;
217
218    //========================= AUTOMATIC DC OFFSET CORRECTIONS ===============================
219
220    /// Returns true if automatic corrections are supported
221    fn has_dc_offset_mode(&self, direction: Direction, channel: usize) -> Result<bool, Error>;
222
223    /// Enable or disable automatic DC offset corrections mode.
224    fn set_dc_offset_mode(
225        &self,
226        direction: Direction,
227        channel: usize,
228        automatic: bool,
229    ) -> Result<(), Error>;
230
231    /// Returns true if automatic DC offset mode is enabled
232    fn dc_offset_mode(&self, direction: Direction, channel: usize) -> Result<bool, Error>;
233}
234
235/// Wrapps a driver, implementing the [DeviceTrait].
236///
237/// Implements a more ergonomic version of the [`DeviceTrait`], e.g., using `Into<Args>`, which
238/// would not be possible in traits.
239#[derive(Clone)]
240pub struct Device<T: DeviceTrait + Clone> {
241    dev: T,
242}
243
244impl Device<GenericDevice> {
245    /// Creates a [`GenericDevice`] opening the first device discovered through
246    /// [`enumerate`](crate::enumerate).
247    pub fn new() -> Result<Self, Error> {
248        let mut devs = crate::enumerate()?;
249        if devs.is_empty() {
250            return Err(Error::NotFound);
251        }
252        Self::from_args(devs.remove(0))
253    }
254
255    /// Create a generic device from a device implementation.
256    pub fn generic_from_impl<T: DeviceTrait + Clone + Sync>(dev: T) -> Self {
257        Self {
258            dev: Arc::new(DeviceWrapper { dev }),
259        }
260    }
261
262    /// Creates a [`GenericDevice`] opening the first device with a given `driver`, specified in
263    /// the `args` or the first device discovered through [`enumerate`](crate::enumerate) that
264    /// matches the args.
265    pub fn from_args<A: TryInto<Args>>(args: A) -> Result<Self, Error> {
266        let args = args.try_into().map_err(|_| Error::ValueError)?;
267        let driver = match args.get::<Driver>("driver") {
268            Ok(d) => Some(d),
269            Err(Error::NotFound) => None,
270            Err(e) => return Err(e),
271        };
272        #[cfg(all(feature = "aaronia_http", not(target_arch = "wasm32")))]
273        {
274            if driver.is_none() || matches!(driver, Some(Driver::AaroniaHttp)) {
275                match crate::impls::AaroniaHttp::open(&args) {
276                    Ok(d) => {
277                        return Ok(Device {
278                            dev: Arc::new(DeviceWrapper { dev: d }),
279                        })
280                    }
281                    Err(Error::NotFound) => {
282                        if driver.is_some() {
283                            return Err(Error::NotFound);
284                        }
285                    }
286                    Err(e) => return Err(e),
287                }
288            }
289        }
290        #[cfg(all(feature = "rtlsdr", not(target_arch = "wasm32")))]
291        {
292            if driver.is_none() || matches!(driver, Some(Driver::RtlSdr)) {
293                match crate::impls::RtlSdr::open(&args) {
294                    Ok(d) => {
295                        return Ok(Device {
296                            dev: Arc::new(DeviceWrapper { dev: d }),
297                        })
298                    }
299                    Err(Error::NotFound) => {
300                        if driver.is_some() {
301                            return Err(Error::NotFound);
302                        }
303                    }
304                    Err(e) => return Err(e),
305                }
306            }
307        }
308        #[cfg(all(feature = "soapy", not(target_arch = "wasm32")))]
309        {
310            if driver.is_none() || matches!(driver, Some(Driver::Soapy)) {
311                match crate::impls::Soapy::open(&args) {
312                    Ok(d) => {
313                        return Ok(Device {
314                            dev: Arc::new(DeviceWrapper { dev: d }),
315                        })
316                    }
317                    Err(Error::NotFound) => {
318                        if driver.is_some() {
319                            return Err(Error::NotFound);
320                        }
321                    }
322                    Err(e) => return Err(e),
323                }
324            }
325        }
326        #[cfg(all(feature = "hackrfone", not(target_arch = "wasm32")))]
327        {
328            if driver.is_none() || matches!(driver, Some(Driver::HackRf)) {
329                match crate::impls::HackRfOne::open(&args) {
330                    Ok(d) => {
331                        return Ok(Device {
332                            dev: Arc::new(DeviceWrapper { dev: d }),
333                        })
334                    }
335                    Err(Error::NotFound) => {
336                        if driver.is_some() {
337                            return Err(Error::NotFound);
338                        }
339                    }
340                    Err(e) => return Err(e),
341                }
342            }
343        }
344        #[cfg(feature = "dummy")]
345        {
346            if driver.is_none() || matches!(driver, Some(Driver::Dummy)) {
347                match crate::impls::Dummy::open(&args) {
348                    Ok(d) => {
349                        return Ok(Device {
350                            dev: Arc::new(DeviceWrapper { dev: d }),
351                        })
352                    }
353                    Err(Error::NotFound) => {
354                        if driver.is_some() {
355                            return Err(Error::NotFound);
356                        }
357                    }
358                    Err(e) => return Err(e),
359                }
360            }
361        }
362
363        Err(Error::NotFound)
364    }
365}
366
367/// Type for a generic/wrapped hardware driver, implementing the [`DeviceTrait`].
368///
369/// This is usually used to create a hardware-independent `Device<GenericDevice>`, for example,
370/// through [`Device::new`], which doesn't know a priori which implementation will be used.
371/// The type abstracts over the `DeviceTrait` implementation as well as the associated
372/// streamer implementations.
373pub type GenericDevice =
374    Arc<dyn DeviceTrait<RxStreamer = Box<dyn RxStreamer>, TxStreamer = Box<dyn TxStreamer>> + Sync>;
375
376impl<T: DeviceTrait + Clone> Device<T> {
377    /// Create a device from the device implementation.
378    pub fn from_impl(dev: T) -> Self {
379        Self { dev }
380    }
381    /// Try to downcast to a given device implementation `D`, either directly (from `Device<D>`)
382    /// or indirectly (from a `Device<GenericDevice>` that wraps a `D`).
383    pub fn impl_ref<D: DeviceTrait>(&self) -> Result<&D, Error> {
384        if let Some(d) = self.dev.as_any().downcast_ref::<D>() {
385            return Ok(d);
386        }
387
388        let d = self
389            .dev
390            .as_any()
391            .downcast_ref::<Arc<
392                dyn DeviceTrait<
393                        RxStreamer = Box<dyn RxStreamer + 'static>,
394                        TxStreamer = Box<dyn TxStreamer + 'static>,
395                    > + Sync
396                    + 'static,
397            >>()
398            .ok_or(Error::ValueError)?;
399
400        let d = (**d)
401            .as_any()
402            .downcast_ref::<DeviceWrapper<D>>()
403            .ok_or(Error::ValueError)?;
404        Ok(&d.dev)
405    }
406    /// Try to downcast mutably to a given device implementation `D`, either directly
407    /// (from `Device<D>`) or indirectly (from a `Device<GenericDevice>` that wraps a `D`).
408    pub fn impl_mut<D: DeviceTrait>(&mut self) -> Result<&mut D, Error> {
409        // work around borrow checker limitation
410        if let Some(d) = self.dev.as_any().downcast_ref::<D>() {
411            Ok(self.dev.as_any_mut().downcast_mut::<D>().unwrap())
412        } else {
413            let d = self
414                .dev
415                .as_any_mut()
416                .downcast_mut::<Box<
417                    dyn DeviceTrait<
418                            RxStreamer = Box<dyn RxStreamer + 'static>,
419                            TxStreamer = Box<dyn TxStreamer + 'static>,
420                        > + 'static,
421                >>()
422                .ok_or(Error::ValueError)?;
423
424            let d = (**d)
425                .as_any_mut()
426                .downcast_mut::<DeviceWrapper<D>>()
427                .ok_or(Error::ValueError)?;
428            Ok(&mut d.dev)
429        }
430    }
431}
432
433struct DeviceWrapper<D: DeviceTrait> {
434    dev: D,
435}
436
437impl<
438        R: RxStreamer + 'static,
439        T: TxStreamer + 'static,
440        D: DeviceTrait<RxStreamer = R, TxStreamer = T>,
441    > DeviceTrait for DeviceWrapper<D>
442{
443    type RxStreamer = Box<dyn RxStreamer>;
444    type TxStreamer = Box<dyn TxStreamer>;
445
446    fn as_any(&self) -> &dyn Any {
447        self
448    }
449    fn as_any_mut(&mut self) -> &mut dyn Any {
450        self
451    }
452
453    fn driver(&self) -> Driver {
454        self.dev.driver()
455    }
456    fn id(&self) -> Result<String, Error> {
457        self.dev.id()
458    }
459    fn info(&self) -> Result<Args, Error> {
460        self.dev.info()
461    }
462    fn num_channels(&self, direction: Direction) -> Result<usize, Error> {
463        self.dev.num_channels(direction)
464    }
465    fn full_duplex(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
466        self.dev.full_duplex(direction, channel)
467    }
468
469    fn rx_streamer(&self, channels: &[usize], args: Args) -> Result<Self::RxStreamer, Error> {
470        Ok(Box::new(self.dev.rx_streamer(channels, args)?))
471    }
472    fn tx_streamer(&self, channels: &[usize], args: Args) -> Result<Self::TxStreamer, Error> {
473        Ok(Box::new(self.dev.tx_streamer(channels, args)?))
474    }
475
476    fn antennas(&self, direction: Direction, channel: usize) -> Result<Vec<String>, Error> {
477        self.dev.antennas(direction, channel)
478    }
479
480    fn antenna(&self, direction: Direction, channel: usize) -> Result<String, Error> {
481        self.dev.antenna(direction, channel)
482    }
483
484    fn set_antenna(&self, direction: Direction, channel: usize, name: &str) -> Result<(), Error> {
485        self.dev.set_antenna(direction, channel, name)
486    }
487
488    fn gain_elements(&self, direction: Direction, channel: usize) -> Result<Vec<String>, Error> {
489        self.dev.gain_elements(direction, channel)
490    }
491
492    fn supports_agc(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
493        self.dev.supports_agc(direction, channel)
494    }
495
496    fn enable_agc(&self, direction: Direction, channel: usize, agc: bool) -> Result<(), Error> {
497        self.dev.enable_agc(direction, channel, agc)
498    }
499
500    fn agc(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
501        self.dev.agc(direction, channel)
502    }
503
504    fn set_gain(&self, direction: Direction, channel: usize, gain: f64) -> Result<(), Error> {
505        self.dev.set_gain(direction, channel, gain)
506    }
507
508    fn gain(&self, direction: Direction, channel: usize) -> Result<Option<f64>, Error> {
509        self.dev.gain(direction, channel)
510    }
511
512    fn gain_range(&self, direction: Direction, channel: usize) -> Result<Range, Error> {
513        self.dev.gain_range(direction, channel)
514    }
515
516    fn set_gain_element(
517        &self,
518        direction: Direction,
519        channel: usize,
520        name: &str,
521        gain: f64,
522    ) -> Result<(), Error> {
523        self.dev.set_gain_element(direction, channel, name, gain)
524    }
525
526    fn gain_element(
527        &self,
528        direction: Direction,
529        channel: usize,
530        name: &str,
531    ) -> Result<Option<f64>, Error> {
532        self.dev.gain_element(direction, channel, name)
533    }
534
535    fn gain_element_range(
536        &self,
537        direction: Direction,
538        channel: usize,
539        name: &str,
540    ) -> Result<Range, Error> {
541        self.dev.gain_element_range(direction, channel, name)
542    }
543
544    fn frequency_range(&self, direction: Direction, channel: usize) -> Result<Range, Error> {
545        self.dev.frequency_range(direction, channel)
546    }
547
548    fn frequency(&self, direction: Direction, channel: usize) -> Result<f64, Error> {
549        self.dev.frequency(direction, channel)
550    }
551
552    fn set_frequency(
553        &self,
554        direction: Direction,
555        channel: usize,
556        frequency: f64,
557        args: Args,
558    ) -> Result<(), Error> {
559        self.dev.set_frequency(direction, channel, frequency, args)
560    }
561
562    fn frequency_components(
563        &self,
564        direction: Direction,
565        channel: usize,
566    ) -> Result<Vec<String>, Error> {
567        self.dev.frequency_components(direction, channel)
568    }
569
570    fn component_frequency_range(
571        &self,
572        direction: Direction,
573        channel: usize,
574        name: &str,
575    ) -> Result<Range, Error> {
576        self.dev.component_frequency_range(direction, channel, name)
577    }
578
579    fn component_frequency(
580        &self,
581        direction: Direction,
582        channel: usize,
583        name: &str,
584    ) -> Result<f64, Error> {
585        self.dev.component_frequency(direction, channel, name)
586    }
587
588    fn set_component_frequency(
589        &self,
590        direction: Direction,
591        channel: usize,
592        name: &str,
593        frequency: f64,
594    ) -> Result<(), Error> {
595        self.dev
596            .set_component_frequency(direction, channel, name, frequency)
597    }
598
599    fn sample_rate(&self, direction: Direction, channel: usize) -> Result<f64, Error> {
600        self.dev.sample_rate(direction, channel)
601    }
602
603    fn set_sample_rate(
604        &self,
605        direction: Direction,
606        channel: usize,
607        rate: f64,
608    ) -> Result<(), Error> {
609        self.dev.set_sample_rate(direction, channel, rate)
610    }
611
612    fn get_sample_rate_range(&self, direction: Direction, channel: usize) -> Result<Range, Error> {
613        self.dev.get_sample_rate_range(direction, channel)
614    }
615
616    fn bandwidth(&self, direction: Direction, channel: usize) -> Result<f64, Error> {
617        self.dev.bandwidth(direction, channel)
618    }
619
620    fn set_bandwidth(&self, direction: Direction, channel: usize, bw: f64) -> Result<(), Error> {
621        self.dev.set_bandwidth(direction, channel, bw)
622    }
623
624    fn get_bandwidth_range(&self, direction: Direction, channel: usize) -> Result<Range, Error> {
625        self.dev.get_bandwidth_range(direction, channel)
626    }
627
628    fn has_dc_offset_mode(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
629        self.dev.has_dc_offset_mode(direction, channel)
630    }
631
632    fn set_dc_offset_mode(
633        &self,
634        direction: Direction,
635        channel: usize,
636        automatic: bool,
637    ) -> Result<(), Error> {
638        self.dev.set_dc_offset_mode(direction, channel, automatic)
639    }
640
641    fn dc_offset_mode(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
642        self.dev.dc_offset_mode(direction, channel)
643    }
644}
645
646#[doc(hidden)]
647impl DeviceTrait for GenericDevice {
648    type RxStreamer = Box<dyn RxStreamer>;
649    type TxStreamer = Box<dyn TxStreamer>;
650
651    fn as_any(&self) -> &dyn Any {
652        self
653    }
654
655    fn as_any_mut(&mut self) -> &mut dyn Any {
656        self
657    }
658
659    fn driver(&self) -> Driver {
660        self.as_ref().driver()
661    }
662    fn id(&self) -> Result<String, Error> {
663        self.as_ref().id()
664    }
665    fn info(&self) -> Result<Args, Error> {
666        self.as_ref().info()
667    }
668    fn num_channels(&self, direction: Direction) -> Result<usize, Error> {
669        self.as_ref().num_channels(direction)
670    }
671    fn full_duplex(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
672        self.as_ref().full_duplex(direction, channel)
673    }
674
675    fn rx_streamer(&self, channels: &[usize], args: Args) -> Result<Self::RxStreamer, Error> {
676        Ok(Box::new(self.as_ref().rx_streamer(channels, args)?))
677    }
678
679    fn tx_streamer(&self, channels: &[usize], args: Args) -> Result<Self::TxStreamer, Error> {
680        Ok(Box::new(self.as_ref().tx_streamer(channels, args)?))
681    }
682
683    fn antennas(&self, direction: Direction, channel: usize) -> Result<Vec<String>, Error> {
684        self.as_ref().antennas(direction, channel)
685    }
686
687    fn antenna(&self, direction: Direction, channel: usize) -> Result<String, Error> {
688        self.as_ref().antenna(direction, channel)
689    }
690
691    fn set_antenna(&self, direction: Direction, channel: usize, name: &str) -> Result<(), Error> {
692        self.as_ref().set_antenna(direction, channel, name)
693    }
694
695    fn gain_elements(&self, direction: Direction, channel: usize) -> Result<Vec<String>, Error> {
696        self.as_ref().gain_elements(direction, channel)
697    }
698
699    fn supports_agc(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
700        self.as_ref().supports_agc(direction, channel)
701    }
702
703    fn enable_agc(&self, direction: Direction, channel: usize, agc: bool) -> Result<(), Error> {
704        self.as_ref().enable_agc(direction, channel, agc)
705    }
706
707    fn agc(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
708        self.as_ref().agc(direction, channel)
709    }
710
711    fn set_gain(&self, direction: Direction, channel: usize, gain: f64) -> Result<(), Error> {
712        self.as_ref().set_gain(direction, channel, gain)
713    }
714
715    fn gain(&self, direction: Direction, channel: usize) -> Result<Option<f64>, Error> {
716        self.as_ref().gain(direction, channel)
717    }
718
719    fn gain_range(&self, direction: Direction, channel: usize) -> Result<Range, Error> {
720        self.as_ref().gain_range(direction, channel)
721    }
722
723    fn set_gain_element(
724        &self,
725        direction: Direction,
726        channel: usize,
727        name: &str,
728        gain: f64,
729    ) -> Result<(), Error> {
730        self.as_ref()
731            .set_gain_element(direction, channel, name, gain)
732    }
733
734    fn gain_element(
735        &self,
736        direction: Direction,
737        channel: usize,
738        name: &str,
739    ) -> Result<Option<f64>, Error> {
740        self.as_ref().gain_element(direction, channel, name)
741    }
742
743    fn gain_element_range(
744        &self,
745        direction: Direction,
746        channel: usize,
747        name: &str,
748    ) -> Result<Range, Error> {
749        self.as_ref().gain_element_range(direction, channel, name)
750    }
751
752    fn frequency_range(&self, direction: Direction, channel: usize) -> Result<Range, Error> {
753        self.as_ref().frequency_range(direction, channel)
754    }
755
756    fn frequency(&self, direction: Direction, channel: usize) -> Result<f64, Error> {
757        self.as_ref().frequency(direction, channel)
758    }
759
760    fn set_frequency(
761        &self,
762        direction: Direction,
763        channel: usize,
764        frequency: f64,
765        args: Args,
766    ) -> Result<(), Error> {
767        self.as_ref()
768            .set_frequency(direction, channel, frequency, args)
769    }
770
771    fn frequency_components(
772        &self,
773        direction: Direction,
774        channel: usize,
775    ) -> Result<Vec<String>, Error> {
776        self.as_ref().frequency_components(direction, channel)
777    }
778
779    fn component_frequency_range(
780        &self,
781        direction: Direction,
782        channel: usize,
783        name: &str,
784    ) -> Result<Range, Error> {
785        self.as_ref()
786            .component_frequency_range(direction, channel, name)
787    }
788
789    fn component_frequency(
790        &self,
791        direction: Direction,
792        channel: usize,
793        name: &str,
794    ) -> Result<f64, Error> {
795        self.as_ref().component_frequency(direction, channel, name)
796    }
797
798    fn set_component_frequency(
799        &self,
800        direction: Direction,
801        channel: usize,
802        name: &str,
803        frequency: f64,
804    ) -> Result<(), Error> {
805        self.as_ref()
806            .set_component_frequency(direction, channel, name, frequency)
807    }
808
809    fn sample_rate(&self, direction: Direction, channel: usize) -> Result<f64, Error> {
810        self.as_ref().sample_rate(direction, channel)
811    }
812
813    fn set_sample_rate(
814        &self,
815        direction: Direction,
816        channel: usize,
817        rate: f64,
818    ) -> Result<(), Error> {
819        self.as_ref().set_sample_rate(direction, channel, rate)
820    }
821
822    fn get_sample_rate_range(&self, direction: Direction, channel: usize) -> Result<Range, Error> {
823        self.as_ref().get_sample_rate_range(direction, channel)
824    }
825
826    fn bandwidth(&self, direction: Direction, channel: usize) -> Result<f64, Error> {
827        self.as_ref().bandwidth(direction, channel)
828    }
829
830    fn set_bandwidth(&self, direction: Direction, channel: usize, bw: f64) -> Result<(), Error> {
831        self.as_ref().set_bandwidth(direction, channel, bw)
832    }
833
834    fn get_bandwidth_range(&self, direction: Direction, channel: usize) -> Result<Range, Error> {
835        self.as_ref().get_bandwidth_range(direction, channel)
836    }
837
838    fn has_dc_offset_mode(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
839        self.as_ref().has_dc_offset_mode(direction, channel)
840    }
841
842    fn set_dc_offset_mode(
843        &self,
844        direction: Direction,
845        channel: usize,
846        automatic: bool,
847    ) -> Result<(), Error> {
848        self.as_ref()
849            .set_dc_offset_mode(direction, channel, automatic)
850    }
851
852    fn dc_offset_mode(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
853        self.as_ref().dc_offset_mode(direction, channel)
854    }
855}
856
857impl<
858        R: RxStreamer + 'static,
859        T: TxStreamer + 'static,
860        D: DeviceTrait<RxStreamer = R, TxStreamer = T> + Clone + 'static,
861    > Device<D>
862{
863    /// SDR [driver](Driver)
864    pub fn driver(&self) -> Driver {
865        self.dev.driver()
866    }
867    /// Identifier for the device, e.g., its serial.
868    pub fn id(&self) -> Result<String, Error> {
869        self.dev.id()
870    }
871    /// Device info that can be displayed to the user.
872    pub fn info(&self) -> Result<Args, Error> {
873        self.dev.info()
874    }
875    /// Number of supported Channels.
876    pub fn num_channels(&self, direction: Direction) -> Result<usize, Error> {
877        self.dev.num_channels(direction)
878    }
879    /// Full Duplex support.
880    pub fn full_duplex(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
881        self.dev.full_duplex(direction, channel)
882    }
883
884    //================================ STREAMER ============================================
885    /// Create an RX streamer.
886    pub fn rx_streamer(&self, channels: &[usize]) -> Result<R, Error> {
887        self.dev.rx_streamer(channels, Args::new())
888    }
889    /// Create an RX streamer, using `args`.
890    pub fn rx_streamer_with_args(&self, channels: &[usize], args: Args) -> Result<R, Error> {
891        self.dev.rx_streamer(channels, args)
892    }
893    /// Create a TX Streamer.
894    pub fn tx_streamer(&self, channels: &[usize]) -> Result<T, Error> {
895        self.dev.tx_streamer(channels, Args::new())
896    }
897    /// Create a TX Streamer, using `args`.
898    pub fn tx_streamer_with_args(&self, channels: &[usize], args: Args) -> Result<T, Error> {
899        self.dev.tx_streamer(channels, args)
900    }
901
902    //================================ ANTENNA ============================================
903    /// List of available antenna ports.
904    pub fn antennas(&self, direction: Direction, channel: usize) -> Result<Vec<String>, Error> {
905        self.dev.antennas(direction, channel)
906    }
907    /// Currently used antenna port.
908    pub fn antenna(&self, direction: Direction, channel: usize) -> Result<String, Error> {
909        self.dev.antenna(direction, channel)
910    }
911    /// Set antenna port.
912    pub fn set_antenna(
913        &self,
914        direction: Direction,
915        channel: usize,
916        name: &str,
917    ) -> Result<(), Error> {
918        self.dev.set_antenna(direction, channel, name)
919    }
920
921    //================================ AGC ============================================
922    /// Does the device support automatic gain control?
923    pub fn supports_agc(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
924        self.dev.supports_agc(direction, channel)
925    }
926    /// Enable or disable automatic gain control.
927    pub fn enable_agc(&self, direction: Direction, channel: usize, agc: bool) -> Result<(), Error> {
928        self.dev.enable_agc(direction, channel, agc)
929    }
930    /// Returns true, if automatic gain control is enabled
931    pub fn agc(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
932        self.dev.agc(direction, channel)
933    }
934
935    //================================ GAIN ============================================
936    /// List of available gain elements.
937    ///
938    /// Elements should be in order RF to baseband.
939    pub fn gain_elements(
940        &self,
941        direction: Direction,
942        channel: usize,
943    ) -> Result<Vec<String>, Error> {
944        self.dev.gain_elements(direction, channel)
945    }
946
947    /// Set the overall amplification in a chain.
948    ///
949    /// The gain will be distributed automatically across available elements.
950    ///
951    /// `gain`: the new amplification value in dB
952    pub fn set_gain(&self, direction: Direction, channel: usize, gain: f64) -> Result<(), Error> {
953        self.dev.set_gain(direction, channel, gain)
954    }
955
956    /// Get the overall value of the gain elements in a chain in dB.
957    pub fn gain(&self, direction: Direction, channel: usize) -> Result<Option<f64>, Error> {
958        self.dev.gain(direction, channel)
959    }
960
961    /// Get the overall [`Range`] of possible gain values.
962    pub fn gain_range(&self, direction: Direction, channel: usize) -> Result<Range, Error> {
963        self.dev.gain_range(direction, channel)
964    }
965
966    /// Set the value of a amplification element in a chain.
967    ///
968    /// ## Arguments
969    /// * `name`: the name of an amplification element from `Device::list_gains`
970    /// * `gain`: the new amplification value in dB
971    pub fn set_gain_element(
972        &self,
973        direction: Direction,
974        channel: usize,
975        name: &str,
976        gain: f64,
977    ) -> Result<(), Error> {
978        self.dev.set_gain_element(direction, channel, name, gain)
979    }
980
981    /// Get the value of an individual amplification element in a chain in dB.
982    pub fn gain_element(
983        &self,
984        direction: Direction,
985        channel: usize,
986        name: &str,
987    ) -> Result<Option<f64>, Error> {
988        self.dev.gain_element(direction, channel, name)
989    }
990
991    /// Get the range of possible gain values for a specific element.
992    pub fn gain_element_range(
993        &self,
994        direction: Direction,
995        channel: usize,
996        name: &str,
997    ) -> Result<Range, Error> {
998        self.dev.gain_element_range(direction, channel, name)
999    }
1000
1001    //================================ FREQUENCY ============================================
1002
1003    /// Get the ranges of overall frequency values.
1004    pub fn frequency_range(&self, direction: Direction, channel: usize) -> Result<Range, Error> {
1005        self.dev.frequency_range(direction, channel)
1006    }
1007
1008    /// Get the overall center frequency of the chain.
1009    ///
1010    ///   - For RX, this specifies the down-conversion frequency.
1011    ///   - For TX, this specifies the up-conversion frequency.
1012    ///
1013    /// Returns the center frequency in Hz.
1014    pub fn frequency(&self, direction: Direction, channel: usize) -> Result<f64, Error> {
1015        self.dev.frequency(direction, channel)
1016    }
1017
1018    /// Set the center frequency of the chain.
1019    ///
1020    ///   - For RX, this specifies the down-conversion frequency.
1021    ///   - For TX, this specifies the up-conversion frequency.
1022    ///
1023    /// The default implementation of `set_frequency` will tune the "RF"
1024    /// component as close as possible to the requested center frequency in Hz.
1025    /// Tuning inaccuracies will be compensated for with the "BB" component.
1026    ///
1027    pub fn set_frequency(
1028        &self,
1029        direction: Direction,
1030        channel: usize,
1031        frequency: f64,
1032    ) -> Result<(), Error> {
1033        self.dev
1034            .set_frequency(direction, channel, frequency, Args::new())
1035    }
1036
1037    /// Like [`set_frequency`](Self::set_frequency) but using `args` to augment the tuning algorithm.
1038    ///
1039    ///   - Use `"OFFSET"` to specify an "RF" tuning offset,
1040    ///     usually with the intention of moving the LO out of the passband.
1041    ///     The offset will be compensated for using the "BB" component.
1042    ///   - Use the name of a component for the key and a frequency in Hz
1043    ///     as the value (any format) to enforce a specific frequency.
1044    ///     The other components will be tuned with compensation
1045    ///     to achieve the specified overall frequency.
1046    ///   - Use the name of a component for the key and the value `"IGNORE"`
1047    ///     so that the tuning algorithm will avoid altering the component.
1048    ///   - Vendor specific implementations can also use the same args to augment
1049    ///     tuning in other ways such as specifying fractional vs integer N tuning.
1050    ///
1051    pub fn set_frequency_with_args(
1052        &self,
1053        direction: Direction,
1054        channel: usize,
1055        frequency: f64,
1056        args: Args,
1057    ) -> Result<(), Error> {
1058        self.dev.set_frequency(direction, channel, frequency, args)
1059    }
1060
1061    /// List available tunable elements in the chain.
1062    ///
1063    /// Elements should be in order RF to baseband.
1064    pub fn frequency_components(
1065        &self,
1066        direction: Direction,
1067        channel: usize,
1068    ) -> Result<Vec<String>, Error> {
1069        self.dev.frequency_components(direction, channel)
1070    }
1071
1072    /// Get the range of tunable values for the specified element.
1073    pub fn component_frequency_range(
1074        &self,
1075        direction: Direction,
1076        channel: usize,
1077        name: &str,
1078    ) -> Result<Range, Error> {
1079        self.dev.component_frequency_range(direction, channel, name)
1080    }
1081
1082    /// Get the frequency of a tunable element in the chain.
1083    pub fn component_frequency(
1084        &self,
1085        direction: Direction,
1086        channel: usize,
1087        name: &str,
1088    ) -> Result<f64, Error> {
1089        self.dev.component_frequency(direction, channel, name)
1090    }
1091
1092    /// Tune the center frequency of the specified element.
1093    ///
1094    ///   - For RX, this specifies the down-conversion frequency.
1095    ///   - For TX, this specifies the up-conversion frequency.
1096    pub fn set_component_frequency(
1097        &self,
1098        direction: Direction,
1099        channel: usize,
1100        name: &str,
1101        frequency: f64,
1102    ) -> Result<(), Error> {
1103        self.dev
1104            .set_component_frequency(direction, channel, name, frequency)
1105    }
1106
1107    //================================ SAMPLE RATE ============================================
1108
1109    /// Get the baseband sample rate of the chain in samples per second.
1110    pub fn sample_rate(&self, direction: Direction, channel: usize) -> Result<f64, Error> {
1111        self.dev.sample_rate(direction, channel)
1112    }
1113
1114    /// Set the baseband sample rate of the chain in samples per second.
1115    pub fn set_sample_rate(
1116        &self,
1117        direction: Direction,
1118        channel: usize,
1119        rate: f64,
1120    ) -> Result<(), Error> {
1121        self.dev.set_sample_rate(direction, channel, rate)
1122    }
1123
1124    /// Get the range of possible baseband sample rates.
1125    pub fn get_sample_rate_range(
1126        &self,
1127        direction: Direction,
1128        channel: usize,
1129    ) -> Result<Range, Error> {
1130        self.dev.get_sample_rate_range(direction, channel)
1131    }
1132
1133    //================================ BANDWIDTH ============================================
1134
1135    /// Get the hardware bandwidth filter, if available.
1136    ///
1137    /// Returns `Err(Error::NotSupported)` if unsupported in underlying driver.
1138    pub fn bandwidth(&self, direction: Direction, channel: usize) -> Result<f64, Error> {
1139        self.dev.bandwidth(direction, channel)
1140    }
1141
1142    /// Set the hardware bandwidth filter, if available.
1143    ///
1144    /// Returns `Err(Error::NotSupported)` if unsupported in underlying driver.
1145    pub fn set_bandwidth(
1146        &self,
1147        direction: Direction,
1148        channel: usize,
1149        bw: f64,
1150    ) -> Result<(), Error> {
1151        self.dev.set_bandwidth(direction, channel, bw)
1152    }
1153
1154    /// Get the range of possible bandwidth filter values, if available.
1155    ///
1156    /// Returns `Err(Error::NotSupported)` if unsupported in underlying driver.
1157    pub fn get_bandwidth_range(
1158        &self,
1159        direction: Direction,
1160        channel: usize,
1161    ) -> Result<Range, Error> {
1162        self.dev.get_bandwidth_range(direction, channel)
1163    }
1164
1165    //========================= AUTOMATIC DC OFFSET CORRECTIONS ===============================
1166    /// Returns true if automatic corrections are supported
1167    pub fn has_dc_offset_mode(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
1168        self.dev.has_dc_offset_mode(direction, channel)
1169    }
1170
1171    /// Set the automatic DC offset correction mode
1172    pub fn set_dc_offset_mode(
1173        &self,
1174        direction: Direction,
1175        channel: usize,
1176        automatic: bool,
1177    ) -> Result<(), Error> {
1178        self.dev.set_dc_offset_mode(direction, channel, automatic)
1179    }
1180
1181    /// Returns true if automatic DC offset mode is enabled
1182    pub fn dc_offset_mode(&self, direction: Direction, channel: usize) -> Result<bool, Error> {
1183        self.dev.dc_offset_mode(direction, channel)
1184    }
1185}