desperado 0.4.1

Iterate and stream I/Q samples from stdin, files, TCP streams and SDR devices
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
//! Airspy SDR I/Q Data Source Module
//! (requires the `airspy` feature)
//!
//! This module provides functionality to read I/Q samples from Airspy devices
//! (R2, Mini, HF+), both synchronously and asynchronously. It uses the `rs_spy`
//! crate to interface with the Airspy hardware.
//!
//! # Sample Format
//!
//! Airspy hardware outputs **REAL samples** from a single ADC sampling the
//! IF (Intermediate Frequency) output from the R820T tuner chip.
//!
//! This module converts real samples to complex I/Q using the `IqConverter`
//! from rs-spy, which implements libairspy-compatible Fs/4 frequency translation:
//!
//! 1. **DC Removal**: High-pass filter to remove DC offset
//! 2. **Fs/4 Translation**: Frequency shift by sample_rate/4
//! 3. **Half-band FIR Filter**: 47-tap filter for I channel
//! 4. **Delay Line**: Matches group delay for Q channel
//!
//! This provides proper I/Q samples without requiring frequency offset compensation.
//!
//! # Example
//!
//! ```no_run
//! use desperado::airspy::{AirspyConfig, AirspySdrReader};
//! use desperado::Gain;
//!
//! let config = AirspyConfig::new(0, 100_000_000, 6_000_000, Gain::Auto);
//! let reader = AirspySdrReader::new(&config)?;
//!
//! for samples in reader.take(10) {
//!     let samples = samples?;
//!     println!("Received {} samples", samples.len());
//! }
//! # Ok::<(), desperado::Error>(())
//! ```

use futures::Stream;
use num_complex::Complex;
use rs_spy::{Airspy, AirspyGainStages, IqConverter, RECOMMENDED_BUFFER_SIZE};
use tracing::{debug, info, warn};

use crate::{Gain, GainElementName, error};

/// Airspy gain preset mode
///
/// Controls which gain lookup tables are used when `Gain::Manual(dB)` is applied.
///
/// - **Sensitivity** (default): Optimizes for weak-signal reception (e.g. FM radio).
///   Uses `set_sensitivity_gain()` internally.
/// - **Linearity**: Optimizes for signal linearity, important for multi-carrier
///   modulation such as OFDM/DAB where intermodulation distortion matters more than
///   raw sensitivity. Uses `set_linearity_gain()` internally.
///   welle.io defaults to linearity gain 10 for Airspy DAB reception.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AirspyGainMode {
    /// Sensitivity-optimized gain presets (default, best for FM)
    Sensitivity,
    /// Linearity-optimized gain presets (best for DAB/OFDM)
    Linearity,
}

#[allow(clippy::derivable_impls)]
impl Default for AirspyGainMode {
    fn default() -> Self {
        AirspyGainMode::Sensitivity
    }
}

/// Device selector for Airspy devices
#[derive(Debug, Clone, PartialEq)]
pub enum DeviceSelector {
    /// Select device by index (0 for first device)
    Index(usize),
    /// Select device by serial number
    Serial(u64),
}

impl Default for DeviceSelector {
    fn default() -> Self {
        DeviceSelector::Index(0)
    }
}

/// Airspy SDR configuration
///
/// # Sample Rate Note
///
/// Sample rate selection is most reliable when using the default rate (index 0,
/// typically 6 MSPS for Airspy Mini). Switching to non-default sample rates may
/// require the device to be in a specific state. If you encounter sample rate
/// errors, try using the highest available rate (index 0) or power-cycling the device.
#[derive(Debug, Clone, PartialEq)]
pub struct AirspyConfig {
    /// Device selector (index or serial)
    pub device: DeviceSelector,
    /// Center frequency in Hz (24 MHz to 1.8 GHz for Mini)
    pub center_freq: u32,
    /// Sample rate in Hz (must match one of the device's supported rates)
    pub sample_rate: u32,
    /// Gain configuration
    pub gain: Gain,
    /// Enable bias tee / RF bias (DC on antenna port)
    pub bias_tee: bool,
    /// Enable 12-bit sample packing (default: false = 16-bit samples)
    pub packing: bool,
    /// LNA gain (0-14), None for default/auto
    pub lna_gain: Option<u8>,
    /// Mixer gain (0-15), None for default/auto
    pub mixer_gain: Option<u8>,
    /// VGA gain (0-15), None for default/auto
    pub vga_gain: Option<u8>,
    /// Gain preset mode (Sensitivity vs Linearity)
    pub gain_mode: AirspyGainMode,
}

impl AirspyConfig {
    /// Create a new Airspy configuration with device index
    ///
    /// # Arguments
    ///
    /// * `device_index` - Zero-based device index
    /// * `center_freq` - Center frequency in Hz
    /// * `sample_rate` - Sample rate in Hz (must be supported by device)
    /// * `gain` - Gain configuration (Auto, Manual dB, or per-element)
    pub fn new(device_index: usize, center_freq: u32, sample_rate: u32, gain: Gain) -> Self {
        Self {
            device: DeviceSelector::Index(device_index),
            center_freq,
            sample_rate,
            gain,
            bias_tee: false,
            packing: false,
            lna_gain: None,
            mixer_gain: None,
            vga_gain: None,
            gain_mode: AirspyGainMode::default(),
        }
    }

    /// Create a new Airspy configuration with serial number selection
    pub fn new_with_serial(serial: u64, center_freq: u32, sample_rate: u32, gain: Gain) -> Self {
        Self {
            device: DeviceSelector::Serial(serial),
            center_freq,
            sample_rate,
            gain,
            bias_tee: false,
            packing: false,
            lna_gain: None,
            mixer_gain: None,
            vga_gain: None,
            gain_mode: AirspyGainMode::default(),
        }
    }
}

/// Device information for Airspy devices
#[derive(Debug, Clone, PartialEq)]
pub struct AirspyDeviceInfo {
    /// Device index (0-based)
    pub index: usize,
    /// Board ID (0 = AIRSPY, 1 = AIRSPY MINI, etc.)
    pub board_id: u32,
    /// Firmware version string
    pub firmware_version: String,
    /// Part IDs (two u32 values)
    pub part_id: [u32; 2],
    /// Serial number
    pub serial_number: u64,
    /// Supported sample rates in Hz
    pub supported_sample_rates: Vec<u32>,
}

impl AirspyDeviceInfo {
    /// Get a human-readable board name
    pub fn board_name(&self) -> &'static str {
        match self.board_id {
            0 => "AIRSPY",
            1 => "AIRSPY MINI",
            2 => "AIRSPY HF+",
            _ => "AIRSPY (Unknown)",
        }
    }
}

/// List all available Airspy devices
///
/// Returns a list of detected Airspy devices with their information.
/// This can be used to discover what devices are available and their properties.
///
/// # Examples
///
/// ```no_run
/// use desperado::airspy::list_devices;
///
/// match list_devices() {
///     Ok(devices) => {
///         println!("Found {} Airspy device(s):", devices.len());
///         for dev in devices {
///             println!("  [{}] {} - {} (SN: 0x{:016X})",
///                      dev.index, dev.board_name(), dev.firmware_version, dev.serial_number);
///         }
///     }
///     Err(e) => eprintln!("Error listing devices: {}", e),
/// }
/// ```
pub fn list_devices() -> error::Result<Vec<AirspyDeviceInfo>> {
    let device_strings = Airspy::list_devices()
        .map_err(|e| error::Error::device(format!("Failed to enumerate Airspy devices: {}", e)))?;

    let mut devices = Vec::new();
    for (index, _) in device_strings.iter().enumerate() {
        // Open device to query info
        match Airspy::open_by_index(index) {
            Ok(device) => {
                let board_id = device.board_id().unwrap_or(0);
                let firmware_version = device.version().unwrap_or_default();
                let (part_id_1, part_id_2, serial_number) =
                    device.board_partid_serialno().unwrap_or((0, 0, 0));
                let supported_sample_rates = device.supported_sample_rates().unwrap_or_default();

                devices.push(AirspyDeviceInfo {
                    index,
                    board_id,
                    firmware_version,
                    part_id: [part_id_1, part_id_2],
                    serial_number,
                    supported_sample_rates,
                });
            }
            Err(e) => {
                // Device exists but we can't open it (permissions, busy, etc.)
                warn!(index, error = %e, "Could not open Airspy device");
            }
        }
    }

    Ok(devices)
}

/// Helper function to open Airspy device based on selector
fn open_device_with_selector(selector: &DeviceSelector) -> error::Result<Airspy> {
    match selector {
        DeviceSelector::Index(idx) => {
            Airspy::open_by_index(*idx).map_err(|e| error::Error::device(e.to_string()))
        }
        DeviceSelector::Serial(serial) => {
            // Enumerate devices and find matching serial
            let devices = list_devices()?;
            for dev in devices {
                if dev.serial_number == *serial {
                    return Airspy::open_by_index(dev.index)
                        .map_err(|e| error::Error::device(e.to_string()));
                }
            }
            Err(error::Error::device(format!(
                "No Airspy device found with serial 0x{:016X}",
                serial
            )))
        }
    }
}

/// Configure gain on Airspy device based on Gain enum and gain mode
fn configure_gain(
    device: &Airspy,
    gain: &Gain,
    gain_mode: AirspyGainMode,
    lna: Option<u8>,
    mixer: Option<u8>,
    vga: Option<u8>,
) -> error::Result<()> {
    // If individual gains are specified, apply them directly
    if let Some(lna_val) = lna {
        device
            .set_lna_gain(lna_val.min(14))
            .map_err(|e| error::Error::device(format!("Failed to set LNA gain: {}", e)))?;
    }
    if let Some(mixer_val) = mixer {
        device
            .set_mixer_gain(mixer_val.min(15))
            .map_err(|e| error::Error::device(format!("Failed to set Mixer gain: {}", e)))?;
    }
    if let Some(vga_val) = vga {
        device
            .set_vga_gain(vga_val.min(15))
            .map_err(|e| error::Error::device(format!("Failed to set VGA gain: {}", e)))?;
    }

    // If individual gains are specified, skip the normal gain configuration
    if lna.is_some() || mixer.is_some() || vga.is_some() {
        // Disable AGC when using manual gains
        device
            .set_lna_agc(false)
            .map_err(|e| error::Error::device(format!("Failed to disable LNA AGC: {}", e)))?;
        device
            .set_mixer_agc(false)
            .map_err(|e| error::Error::device(format!("Failed to disable Mixer AGC: {}", e)))?;
        return Ok(());
    }

    // Otherwise use the standard gain configuration
    match gain {
        Gain::Auto => {
            // Enable AGC for both LNA and Mixer
            device
                .set_lna_agc(true)
                .map_err(|e| error::Error::device(format!("Failed to enable LNA AGC: {}", e)))?;
            device
                .set_mixer_agc(true)
                .map_err(|e| error::Error::device(format!("Failed to enable Mixer AGC: {}", e)))?;
            // VGA gain: set to middle value when using AGC
            device
                .set_vga_gain(8)
                .map_err(|e| error::Error::device(format!("Failed to set VGA gain: {}", e)))?;
        }
        Gain::Manual(db) => {
            // Map dB value (0-50 range typical) to Airspy's gain preset (0-21)
            // This is an approximation; actual dB depends on frequency and other factors
            let level = ((*db / 50.0) * 21.0).clamp(0.0, 21.0) as u8;
            match gain_mode {
                AirspyGainMode::Linearity => {
                    device.set_linearity_gain(level).map_err(|e| {
                        error::Error::device(format!("Failed to set linearity gain: {}", e))
                    })?;
                    info!(level, "Airspy linearity gain set");
                }
                AirspyGainMode::Sensitivity => {
                    device.set_sensitivity_gain(level).map_err(|e| {
                        error::Error::device(format!("Failed to set sensitivity gain: {}", e))
                    })?;
                    info!(level, "Airspy sensitivity gain set");
                }
            }
        }
        Gain::Elements(elements) => {
            // Disable AGC when using element-based gain
            device
                .set_lna_agc(false)
                .map_err(|e| error::Error::device(format!("Failed to disable LNA AGC: {}", e)))?;
            device
                .set_mixer_agc(false)
                .map_err(|e| error::Error::device(format!("Failed to disable Mixer AGC: {}", e)))?;

            for element in elements {
                match &element.name {
                    GainElementName::Lna => {
                        // LNA gain: 0-14
                        let gain = (element.value_db as u8).min(14);
                        device.set_lna_gain(gain).map_err(|e| {
                            error::Error::device(format!("Failed to set LNA gain: {}", e))
                        })?;
                    }
                    GainElementName::Mix => {
                        // Mixer gain: 0-15
                        let gain = (element.value_db as u8).min(15);
                        device.set_mixer_gain(gain).map_err(|e| {
                            error::Error::device(format!("Failed to set Mixer gain: {}", e))
                        })?;
                    }
                    GainElementName::Vga => {
                        // VGA gain: 0-15
                        let gain = (element.value_db as u8).min(15);
                        device.set_vga_gain(gain).map_err(|e| {
                            error::Error::device(format!("Failed to set VGA gain: {}", e))
                        })?;
                    }
                    other => {
                        warn!(element = ?other, "Airspy does not support gain element, ignoring");
                    }
                }
            }
        }
    }
    Ok(())
}

/// Synchronous Airspy I/Q Reader
///
/// Provides an iterator-based interface for reading I/Q samples from Airspy devices.
/// Each call to `next()` returns a chunk of `Complex<f32>` samples.
///
/// # Example
///
/// ```no_run
/// use desperado::airspy::{AirspyConfig, AirspySdrReader};
/// use desperado::Gain;
///
/// let config = AirspyConfig::new(0, 100_000_000, 6_000_000, Gain::Auto);
/// let mut reader = AirspySdrReader::new(&config)?;
///
/// for result in reader.take(10) {
///     let samples = result?;
///     println!("Got {} samples", samples.len());
/// }
/// # Ok::<(), desperado::Error>(())
/// ```
pub struct AirspySdrReader {
    device: Airspy,
    buf: Vec<u8>,
    float_buf: Vec<f32>,
    iq_converter: IqConverter,
    packing: bool,
}

/// Convert raw Airspy USB bytes to f32 samples.
///
/// Airspy hardware stores 12-bit ADC values in the lower 12 bits of each 16-bit word
/// (upper 4 bits are always zero). The correct formula matches libairspy's
/// `convert_samples_float`: `(raw - 2048) * (1/2048)`.
fn convert_airspy_real_to_f32(bytes: &[u8], out: &mut [f32]) -> usize {
    let num_samples = bytes.len() / 2;
    for (i, chunk) in bytes.chunks_exact(2).enumerate() {
        let raw = u16::from_le_bytes([chunk[0], chunk[1]]);
        out[i] = (raw as f32 - 2048.0) / 2048.0;
    }
    num_samples
}

impl AirspySdrReader {
    /// Create a new synchronous Airspy reader
    ///
    /// Opens the device, configures it according to the provided config,
    /// and starts streaming.
    pub fn new(config: &AirspyConfig) -> error::Result<Self> {
        let device = open_device_with_selector(&config.device)?;

        // Set frequency directly - no offset needed!
        // The IqConverter performs Fs/4 translation which brings the IF signal to baseband.
        device
            .set_freq(config.center_freq)
            .map_err(|e| error::Error::device(format!("Failed to set frequency: {}", e)))?;

        // Configure gain
        configure_gain(
            &device,
            &config.gain,
            config.gain_mode,
            config.lna_gain,
            config.mixer_gain,
            config.vga_gain,
        )?;

        // Set RF bias (Bias-T)
        device
            .set_rf_bias(config.bias_tee)
            .map_err(|e| error::Error::device(format!("Failed to set RF bias: {}", e)))?;

        // Set packing mode
        device
            .set_packing(config.packing)
            .map_err(|e| error::Error::device(format!("Failed to set packing: {}", e)))?;

        // Set requested sample rate BEFORE starting RX.
        // Note: sample rate setting may fail on some firmware versions before streaming starts;
        // if it fails here, we retry after start_rx().
        let mut sample_rate_set = false;
        match device.set_sample_rate_for_iq(config.sample_rate) {
            Ok(()) => sample_rate_set = true,
            Err(_e) => {
                // Will try again after start_rx
                // Some firmware versions require streaming to be active first
            }
        }

        // Start streaming
        device
            .start_rx()
            .map_err(|e| error::Error::device(format!("Failed to start RX: {}", e)))?;

        // If sample rate wasn't set before, try now after start_rx
        if !sample_rate_set {
            match device.set_sample_rate_for_iq(config.sample_rate) {
                Ok(()) => {}
                Err(e) => {
                    let supported = device.supported_sample_rates().unwrap_or_default();
                    return Err(error::Error::device(format!(
                        "Failed to set sample rate after start_rx: {}. \
                        Supported rates: {:?}. \
                        Requested IQ: {} Hz",
                        e, supported, config.sample_rate
                    )));
                }
            }
        }

        // Pre-allocate float buffer for sample conversion
        // RECOMMENDED_BUFFER_SIZE bytes / 2 bytes per sample = number of samples
        let max_samples = RECOMMENDED_BUFFER_SIZE / 2;

        Ok(Self {
            device,
            buf: vec![0u8; RECOMMENDED_BUFFER_SIZE],
            float_buf: vec![0.0f32; max_samples],
            iq_converter: IqConverter::new(),
            packing: config.packing,
        })
    }

    /// Get device information
    pub fn device_info(&self) -> error::Result<AirspyDeviceInfo> {
        let board_id = self
            .device
            .board_id()
            .map_err(|e| error::Error::device(e.to_string()))?;
        let firmware_version = self
            .device
            .version()
            .map_err(|e| error::Error::device(e.to_string()))?;
        let (part_id_1, part_id_2, serial_number) = self
            .device
            .board_partid_serialno()
            .map_err(|e| error::Error::device(e.to_string()))?;
        let supported_sample_rates = self
            .device
            .supported_sample_rates()
            .map_err(|e| error::Error::device(e.to_string()))?;

        Ok(AirspyDeviceInfo {
            index: 0, // We don't track the index after opening
            board_id,
            firmware_version,
            part_id: [part_id_1, part_id_2],
            serial_number,
            supported_sample_rates,
        })
    }
}

impl Iterator for AirspySdrReader {
    type Item = error::Result<Vec<Complex<f32>>>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.device.read_sync(&mut self.buf) {
            Ok(bytes_read) => {
                if bytes_read == 0 {
                    return None; // End of stream
                }

                if self.packing {
                    // TODO: Implement 12-bit unpacking for packed mode
                    warn!("12-bit unpacking not yet implemented, data may be incorrect");
                }

                let num_samples =
                    convert_airspy_real_to_f32(&self.buf[..bytes_read], &mut self.float_buf);

                // Process through IqConverter (Fs/4 translation)
                // This converts real samples to interleaved I/Q
                let samples = self
                    .iq_converter
                    .process_to_complex(&mut self.float_buf[..num_samples]);

                Some(Ok(samples))
            }
            Err(e) => Some(Err(error::Error::device(format!("Read error: {}", e)))),
        }
    }
}

impl Drop for AirspySdrReader {
    fn drop(&mut self) {
        // Stop streaming when reader is dropped
        let _ = self.device.stop_rx();
    }
}

/// Asynchronous Airspy I/Q Reader
///
/// Control message for dynamic Airspy parameter adjustment during streaming.
#[derive(Debug, Clone)]
pub enum AirspyMessage {
    /// Retune to a new center frequency (Hz)
    Frequency(u32),
    /// Change gain configuration
    Gain(Gain),
}

/// Convert a `Gain` to raw `AirspyGainStages` for the control channel.
fn gain_to_stages(
    gain: &Gain,
    gain_mode: AirspyGainMode,
    lna: Option<u8>,
    mixer: Option<u8>,
    vga: Option<u8>,
) -> AirspyGainStages {
    match gain {
        Gain::Auto => AirspyGainStages {
            lna_agc: true,
            mixer_agc: true,
            lna: 0,
            mixer: 0,
            vga: vga.unwrap_or(15),
        },
        Gain::Manual(db) => {
            // Map 0–50 dB to gain preset 0–21; matches configure_gain()
            let level = ((*db as u32 * 21) / 50).min(21) as u8;
            let idx = (21 - level) as usize;
            match gain_mode {
                AirspyGainMode::Linearity => {
                    const LINEARITY_VGA: [u8; 22] = [
                        13, 12, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 8, 7, 6,
                        5, 4,
                    ];
                    const LINEARITY_MIXER: [u8; 22] = [
                        12, 12, 11, 9, 8, 7, 6, 6, 5, 0, 0, 1, 0, 0, 2, 2, 1, 1, 1, 1, 0, 0,
                    ];
                    const LINEARITY_LNA: [u8; 22] = [
                        14, 14, 14, 13, 12, 10, 9, 9, 8, 9, 8, 6, 5, 3, 1, 0, 0, 0, 0, 0, 0, 0,
                    ];
                    AirspyGainStages {
                        lna_agc: false,
                        mixer_agc: false,
                        lna: LINEARITY_LNA[idx],
                        mixer: LINEARITY_MIXER[idx],
                        vga: LINEARITY_VGA[idx],
                    }
                }
                AirspyGainMode::Sensitivity => {
                    const SENSITIVITY_VGA: [u8; 22] = [
                        13, 12, 11, 10, 9, 8, 7, 6, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4,
                    ];
                    const SENSITIVITY_MIXER: [u8; 22] = [
                        12, 12, 12, 12, 11, 10, 10, 9, 9, 8, 7, 4, 4, 4, 3, 2, 2, 1, 0, 0, 0, 0,
                    ];
                    const SENSITIVITY_LNA: [u8; 22] = [
                        14, 14, 14, 14, 14, 14, 14, 14, 14, 13, 12, 12, 9, 9, 8, 7, 6, 5, 3, 2, 1,
                        0,
                    ];
                    AirspyGainStages {
                        lna_agc: false,
                        mixer_agc: false,
                        lna: SENSITIVITY_LNA[idx],
                        mixer: SENSITIVITY_MIXER[idx],
                        vga: SENSITIVITY_VGA[idx],
                    }
                }
            }
        }
        Gain::Elements(elements) => {
            let mut s = AirspyGainStages {
                lna_agc: false,
                mixer_agc: false,
                lna: lna.unwrap_or(14),
                mixer: mixer.unwrap_or(12),
                vga: vga.unwrap_or(13),
            };
            for elem in elements {
                match &elem.name {
                    GainElementName::Lna => s.lna = (elem.value_db as u8).min(14),
                    GainElementName::Mix => s.mixer = (elem.value_db as u8).min(15),
                    GainElementName::Vga => s.vga = (elem.value_db as u8).min(15),
                    _ => {}
                }
            }
            s
        }
    }
}

/// Provides a `futures::Stream`-based interface for reading I/Q samples.
/// Internally spawns a background thread that reads from the device and
/// sends samples through a tokio channel.
///
/// # Example
///
/// ```no_run
/// use desperado::airspy::{AirspyConfig, AsyncAirspySdrReader};
/// use desperado::Gain;
/// use futures::StreamExt;
///
/// # async fn example() -> Result<(), desperado::Error> {
/// let config = AirspyConfig::new(0, 100_000_000, 6_000_000, Gain::Auto);
/// let mut reader = AsyncAirspySdrReader::new(&config)?;
///
/// while let Some(result) = reader.next().await {
///     let samples = result?;
///     println!("Got {} samples", samples.len());
/// }
/// # Ok(())
/// # }
/// ```
pub struct AsyncAirspySdrReader {
    rx: tokio::sync::mpsc::Receiver<error::Result<Vec<Complex<f32>>>>,
    ctrl: rs_spy::transport::AsyncReadControlHandle,
    gain_mode: AirspyGainMode,
    _handle: std::thread::JoinHandle<()>,
}

impl AsyncAirspySdrReader {
    /// Create a new asynchronous Airspy reader
    ///
    /// Opens the device in a background thread and streams samples through
    /// an async channel.
    pub fn new(config: &AirspyConfig) -> error::Result<Self> {
        let (tx, rx) = tokio::sync::mpsc::channel::<error::Result<Vec<Complex<f32>>>>(32);
        let cfg = config.clone();

        let device = open_device_with_selector(&cfg.device)?;

        // Debug: print device info
        if let Ok(rates) = device.supported_sample_rates() {
            info!(rates = ?rates, "Airspy supported sample rates");
        }
        if let Ok((_, _, sn)) = device.board_partid_serialno() {
            info!(serial = format!("0x{:016X}", sn), "Airspy serial number");
        }

        device
            .set_freq(cfg.center_freq)
            .map_err(|e| error::Error::device(format!("Failed to set frequency: {}", e)))?;

        configure_gain(
            &device,
            &cfg.gain,
            cfg.gain_mode,
            cfg.lna_gain,
            cfg.mixer_gain,
            cfg.vga_gain,
        )?;

        device
            .set_rf_bias(cfg.bias_tee)
            .map_err(|e| error::Error::device(format!("Failed to set RF bias: {}", e)))?;

        device
            .set_packing(cfg.packing)
            .map_err(|e| error::Error::device(format!("Failed to set packing: {}", e)))?;

        let mut sample_rate_set = false;
        if device.set_sample_rate_for_iq(cfg.sample_rate).is_ok() {
            sample_rate_set = true;
        }

        device
            .start_rx()
            .map_err(|e| error::Error::device(format!("Failed to start RX: {}", e)))?;

        if !sample_rate_set {
            device
                .set_sample_rate_for_iq(cfg.sample_rate)
                .map_err(|e| error::Error::device(format!("Failed to set sample rate: {}", e)))?;
        }

        // Use 15 concurrent bulk transfers (same as RTL-SDR's DEFAULT_ASYNC_BUF_NUMBER)
        // to keep the device FIFO from overflowing between reads.
        // At 6 Msps (12 MB/s), a single sequential read leaves the FIFO exposed for ~21 ms;
        // with 15 in-flight transfers the gap is <2 ms.
        let async_reader = device
            .into_multi_transfer_reader(15, RECOMMENDED_BUFFER_SIZE)
            .map_err(|e| error::Error::device(format!("Failed to start async reader: {}", e)))?;
        let ctrl = async_reader.control_handle();

        let handle = std::thread::spawn(move || {
            let reader = async_reader;
            let max_samples = RECOMMENDED_BUFFER_SIZE / 2;
            let mut float_buf = vec![0.0f32; max_samples];
            let mut iq_converter = IqConverter::new();
            let mut chunk_count = 0usize;

            while let Some(chunk_res) = reader.recv() {
                match chunk_res {
                    Ok(bytes) => {
                        if bytes.is_empty() {
                            continue;
                        }

                        if cfg.packing {
                            warn!("12-bit unpacking not yet implemented");
                        }

                        let num_samples = convert_airspy_real_to_f32(&bytes, &mut float_buf);

                        let samples =
                            iq_converter.process_to_complex(&mut float_buf[..num_samples]);
                        chunk_count += 1;
                        if chunk_count <= 5 {
                            debug!(
                                chunk = chunk_count,
                                bytes = bytes.len(),
                                num_samples,
                                iq_samples = samples.len(),
                                "Airspy sending chunk"
                            );
                        }
                        // Use blocking_send for backpressure (lesson 20.1:
                        // never use try_send in real-time signal processing
                        // pipelines — dropped samples corrupt protocol framing).
                        if tx.blocking_send(Ok(samples)).is_err() {
                            debug!("Airspy channel closed, exiting");
                            break;
                        }
                    }
                    Err(e) => {
                        let _ = tx
                            .blocking_send(Err(error::Error::device(format!("Read error: {}", e))));
                        break;
                    }
                }
            }
            debug!(chunks = chunk_count, "Airspy background thread exiting");
        });

        Ok(Self {
            rx,
            ctrl,
            gain_mode: cfg.gain_mode,
            _handle: handle,
        })
    }

    pub fn tune(&self, center_freq: u32) -> error::Result<()> {
        self.adjust(AirspyMessage::Frequency(center_freq))
    }

    /// Send a control message to the device thread (tune or gain change).
    pub fn adjust(&self, message: AirspyMessage) -> error::Result<()> {
        match message {
            AirspyMessage::Frequency(freq) => self
                .ctrl
                .tune(freq)
                .map_err(|e| error::Error::device(format!("Airspy async tune failed: {}", e))),
            AirspyMessage::Gain(gain) => {
                let stages = gain_to_stages(&gain, self.gain_mode, None, None, None);
                self.ctrl.set_gain(stages).map_err(|e| {
                    error::Error::device(format!("Airspy async set_gain failed: {}", e))
                })
            }
        }
    }
}

impl Stream for AsyncAirspySdrReader {
    type Item = error::Result<Vec<Complex<f32>>>;

    fn poll_next(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Option<Self::Item>> {
        match self.rx.poll_recv(cx) {
            std::task::Poll::Ready(Some(item)) => std::task::Poll::Ready(Some(item)),
            std::task::Poll::Ready(None) => std::task::Poll::Ready(None),
            std::task::Poll::Pending => std::task::Poll::Pending,
        }
    }
}

/// Get the index of the first available Airspy device
///
/// Returns 0, which is the conventional index for the first device.
pub fn get_first_device_index() -> usize {
    0
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_device_selector_default() {
        assert_eq!(DeviceSelector::default(), DeviceSelector::Index(0));
    }

    #[test]
    fn test_airspy_config_new() {
        let config = AirspyConfig::new(0, 100_000_000, 6_000_000, Gain::Auto);
        assert_eq!(config.device, DeviceSelector::Index(0));
        assert_eq!(config.center_freq, 100_000_000);
        assert_eq!(config.sample_rate, 6_000_000);
        assert_eq!(config.gain, Gain::Auto);
        assert!(!config.bias_tee);
        assert!(!config.packing);
    }

    #[test]
    fn test_airspy_config_with_serial() {
        let config =
            AirspyConfig::new_with_serial(0x35AC63DC2D8C7A4F, 100_000_000, 6_000_000, Gain::Auto);
        assert_eq!(config.device, DeviceSelector::Serial(0x35AC63DC2D8C7A4F));
    }

    #[test]
    fn test_device_info_board_name() {
        let info = AirspyDeviceInfo {
            index: 0,
            board_id: 1,
            firmware_version: "test".to_string(),
            part_id: [0, 0],
            serial_number: 0,
            supported_sample_rates: vec![],
        };
        assert_eq!(info.board_name(), "AIRSPY MINI");
    }
}