1use crate::{
3 DeviceDescriptor, DirectSampling, GainConfig, Result, TunerKind,
4 config::{Config, ConfigBuilder, F32Iq, RawIq, SampleMode},
5 discovery::Selector,
6 maybe_future::{MaybeFutureExt, ready},
7 rtl2832u::Applied,
8 rx::RxStream,
9 session::{RxStreamClaim, Session},
10 usb::NusbTransport,
11};
12use nusb::MaybeFuture;
13use std::sync::Arc;
14
15#[derive(Clone, Debug, Eq, PartialEq)]
17pub struct DeviceInfo {
18 pub descriptor: DeviceDescriptor,
20 pub tuner: TunerKind,
22 pub rtl_sdr_blog_v4: bool,
24}
25
26#[derive(Debug)]
29pub struct Device<M: SampleMode = F32Iq> {
30 session: Arc<Session>,
31 config: Config<M>,
32 applied: Applied,
33}
34impl Device<F32Iq> {
35 pub fn list() -> impl MaybeFuture<Output = Result<Vec<DeviceDescriptor>>> {
37 crate::discovery::list()
38 }
39 pub fn builder() -> DeviceBuilder {
41 DeviceBuilder::default()
42 }
43 pub fn open() -> impl MaybeFuture<Output = Result<Self>> {
45 Self::builder().open()
46 }
47 pub fn open_serial(serial: impl Into<String>) -> impl MaybeFuture<Output = Result<Self>> {
49 Self::builder().serial(serial).open()
50 }
51 #[cfg(target_arch = "wasm32")]
53 pub async fn request_permission() -> Result<()> {
54 Self::builder().request_permission().await
55 }
56}
57impl<M: SampleMode> Device<M> {
58 pub fn info(&self) -> &DeviceInfo {
60 self.session.info.get().expect("initialized metadata")
61 }
62 pub fn config(&self) -> &Config<M> {
65 &self.config
66 }
67 pub fn actual_frequency_hz(&self) -> u64 {
69 self.applied.frequency_hz
70 }
71 pub fn actual_sample_rate_hz(&self) -> u32 {
73 self.applied.sample_rate_hz
74 }
75 pub fn configure(&mut self, config: &Config<M>) -> impl MaybeFuture<Output = Result<()>> + '_ {
82 self.update(config.clone())
83 }
84 fn update(&mut self, proposed: Config<M>) -> impl MaybeFuture<Output = Result<()>> + '_ {
85 ready(proposed.settings.validate()).and_then(move |_| {
86 self.session
87 .configure(proposed.settings.clone())
88 .map(move |result| {
89 self.applied = result?;
90 self.config = proposed;
91 Ok(())
92 })
93 })
94 }
95 pub fn set_frequency_hz(&mut self, hz: u64) -> impl MaybeFuture<Output = Result<()>> + '_ {
97 let mut config = self.config.clone();
98 config.settings.frequency_hz = hz;
99 self.update(config)
100 }
101 pub fn set_sample_rate_hz(&mut self, hz: u32) -> impl MaybeFuture<Output = Result<()>> + '_ {
103 let mut config = self.config.clone();
104 config.settings.sample_rate_hz = hz;
105 self.update(config)
106 }
107 pub fn set_gain(&mut self, gain: GainConfig) -> impl MaybeFuture<Output = Result<()>> + '_ {
109 let mut config = self.config.clone();
110 config.settings.gain = gain;
111 self.update(config)
112 }
113 pub fn set_correction_ppm(&mut self, ppm: i32) -> impl MaybeFuture<Output = Result<()>> + '_ {
115 let mut config = self.config.clone();
116 config.settings.correction_ppm = ppm;
117 self.update(config)
118 }
119 pub fn set_bias_tee(&mut self, enabled: bool) -> impl MaybeFuture<Output = Result<()>> + '_ {
121 let mut config = self.config.clone();
122 config.settings.bias_tee = enabled;
123 self.update(config)
124 }
125 pub fn set_direct_sampling(
127 &mut self,
128 mode: DirectSampling,
129 ) -> impl MaybeFuture<Output = Result<()>> + '_ {
130 let mut config = self.config.clone();
131 config.settings.direct_sampling = mode;
132 self.update(config)
133 }
134 pub fn rx_stream(&self) -> Result<RxStream<M>> {
137 self.session.ensure_configured()?;
138 Ok(RxStream::new(RxStreamClaim::acquire(&self.session)?))
139 }
140 pub fn shutdown(&mut self) -> impl MaybeFuture<Output = Result<()>> + '_ {
151 self.session.shutdown()
152 }
153}
154
155#[derive(Clone, Debug)]
157pub struct DeviceBuilder<M: SampleMode = F32Iq> {
158 selector: Selector,
159 config: ConfigBuilder<M>,
160}
161impl Default for DeviceBuilder<F32Iq> {
162 fn default() -> Self {
163 Self {
164 selector: Selector::default(),
165 config: ConfigBuilder::default(),
166 }
167 }
168}
169impl<M: SampleMode> DeviceBuilder<M> {
170 pub fn serial(mut self, serial: impl Into<String>) -> Self {
172 self.selector.serial = Some(serial.into());
173 self.selector.index = None;
174 self
175 }
176 pub fn index(mut self, index: usize) -> Self {
178 self.selector.index = Some(index);
179 self.selector.serial = None;
180 self
181 }
182 pub fn usb_id(mut self, vid: u16, pid: u16) -> Self {
184 self.selector.usb_id = Some((vid, pid));
185 self
186 }
187 pub fn frequency_hz(mut self, value: u64) -> Self {
189 self.config = self.config.frequency_hz(value);
190 self
191 }
192 pub fn sample_rate_hz(mut self, value: u32) -> Self {
194 self.config = self.config.sample_rate_hz(value);
195 self
196 }
197 pub fn gain(mut self, value: GainConfig) -> Self {
199 self.config = self.config.gain(value);
200 self
201 }
202 pub fn correction_ppm(mut self, value: i32) -> Self {
204 self.config = self.config.correction_ppm(value);
205 self
206 }
207 pub fn bias_tee(mut self, value: bool) -> Self {
209 self.config = self.config.bias_tee(value);
210 self
211 }
212 pub fn direct_sampling(mut self, value: DirectSampling) -> Self {
214 self.config = self.config.direct_sampling(value);
215 self
216 }
217 pub fn config(self) -> Result<Config<M>> {
219 self.config.build()
220 }
221 pub fn open(self) -> impl MaybeFuture<Output = Result<Device<M>>> {
224 ready(self.config.build()).and_then(move |config| {
225 NusbTransport::open(self.selector).and_then(move |(control, descriptor)| {
226 Session::initialize(control, descriptor, config.settings.clone()).map_ok(
227 move |(session, applied)| Device {
228 session,
229 config,
230 applied,
231 },
232 )
233 })
234 })
235 }
236 #[cfg(target_arch = "wasm32")]
239 pub async fn request_permission(&self) -> Result<()> {
240 crate::discovery::request_permission(&self.selector).await
241 }
242}
243impl DeviceBuilder<F32Iq> {
244 pub fn raw_iq(self) -> DeviceBuilder<RawIq> {
246 DeviceBuilder {
247 selector: self.selector,
248 config: self.config.raw_iq(),
249 }
250 }
251}