datalink 0.2.0

Decode aviation datalink traffic from payloads, SDR, files, and Airframes.io
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
//! VDL Mode 2 frontend.
//!
//! This module implements the `datalink vdl2` command: open a file or SDR
//! source, channelize configured VDL2 frequencies, demodulate D8PSK bursts,
//! parse AVLC frames, and emit normalized JSONL or Redis messages. Configuration
//! can come from CLI options or from the merged receiver TOML model.
//!
//! ## Default scan channels
//!
//! These channels are used when no explicit channel list is configured and the
//! source bandwidth metadata is too narrow or unavailable for automatic channel
//! selection.
//!
//! | Frequency | Support |
//! |---:|---|
//! | 136.650 MHz | USA ARINC |
//! | 136.675 MHz | Europe ARINC |
//! | 136.725 MHz | Europe ARINC |
//! | 136.775 MHz | Europe SITA |
//! | 136.825 MHz | Europe ARINC |
//! | 136.875 MHz | Europe SITA |
//! | 136.975 MHz | Worldwide common signalling channel |
//!
//! ## Known VDL Mode 2 channels
//!
//! The automatic channel selector chooses from this list when a source center
//! frequency and sample rate are known. The list is based on the SIGIDWiki
//! VDL-M2 table, the Airframes technology docs, ACARS Online notes, and
//! captured Airframes.io message counts.
//!
//! | Frequency | Support |
//! |---:|---|
//! | 136.100 MHz | USA ARINC |
//! | 136.650 MHz | USA ARINC |
//! | 136.675 MHz | Europe ARINC |
//! | 136.700 MHz | USA ARINC |
//! | 136.725 MHz | Europe ARINC |
//! | 136.750 MHz | Additional USA / new European channel |
//! | 136.775 MHz | Europe SITA |
//! | 136.800 MHz | USA SITA |
//! | 136.825 MHz | Europe ARINC |
//! | 136.850 MHz | SITA North America channel |
//! | 136.875 MHz | Europe SITA |
//! | 136.900 MHz | European secondary channel |
//! | 136.975 MHz | Worldwide common signalling channel |

use crate::event::{
    Bearer, DecodedEvent, ProtocolMessage, ReceiverMetadata, SourceClass, SourceMetadata,
};
use crate::iq_pipeline::{collect_iq_frames, FrameContext, IqPipeline};
use crate::source::{Address, Source};
use crate::util::{bytes_to_hex, expanduser, infer_capture_params, RedisPublisher};
use acars::decode::avlc::parse_avlc_frame;
use acars::demod::resample::{maybe_resample, ResampleAdapter};
use acars::demod::vdl2::{Vdl2Channel, SYMBOL_RATE};
use clap::Parser;
use desperado::dsp::channelizer::Channelizer;
use futures_util::StreamExt;
use serde::Deserialize;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::time::SystemTime;

const DEFAULT_CENTER_FREQ: u32 = 136_850_000;

/// Strongest VDL2 channels around the default 136.85 MHz center
/// With the default 1.05 Msps source rate these all fit in-band.
const DEFAULT_CHANNELS: &[u32] = &[
    // USA ARINC
    136_650_000,
    // Europe ARINC
    136_675_000,
    // Europe ARINC
    136_725_000,
    // Europe SITA
    136_775_000,
    // Europe ARINC
    136_825_000,
    // Europe SITA
    136_875_000,
    // Worldwide common signalling channel
    136_975_000,
];

/// Common VDL Mode 2 channels
const KNOWN_VDL2_CHANNELS: &[u32] = &[
    // USA ARINC
    136_100_000,
    // USA ARINC
    136_650_000,
    // Europe ARINC
    136_675_000,
    // USA ARINC
    136_700_000,
    // Europe ARINC
    136_725_000,
    // Additional USA / new European channel
    136_750_000,
    // Europe SITA
    136_775_000,
    // USA SITA
    136_800_000,
    // Europe ARINC
    136_825_000,
    // SITA North America channel
    136_850_000,
    // Europe SITA
    136_875_000,
    // European secondary channel
    136_900_000,
    // Worldwide common signalling channel
    136_975_000,
];

const DEFAULT_CHUNK_SIZE: usize = 65_536;

/// When no channels are explicitly configured, return known VDL2 channels that
/// fall within the recording's bandwidth. Falls back to DEFAULT_CHANNELS when
/// the bandwidth is too narrow or metadata is missing.
fn auto_channels(src: &Source) -> Vec<u32> {
    let center = src.center_freq_or(DEFAULT_CENTER_FREQ);
    let sr = src.sample_rate();
    let half_bw = (sr as f64 * 0.45) as u32;
    let lo = center.saturating_sub(half_bw);
    let hi = center.saturating_add(half_bw);
    let candidates: Vec<u32> = KNOWN_VDL2_CHANNELS
        .iter()
        .copied()
        .filter(|&ch| ch >= lo && ch <= hi)
        .collect();

    if candidates.is_empty() {
        DEFAULT_CHANNELS.to_vec()
    } else {
        candidates
    }
}

/// Internal VDL2 configuration after merging TOML, source URL, and CLI values.
#[derive(Debug, Default, Clone, Deserialize)]
pub(crate) struct Options {
    #[serde(default)]
    output: Option<String>,
    #[serde(default)]
    stats: bool,
    #[serde(default)]
    window_start_sec: Option<f64>,
    #[serde(default)]
    window_end_sec: Option<f64>,
    #[serde(default)]
    sync_threshold: Option<f32>,
    #[serde(default)]
    format: Option<String>,
    #[serde(default)]
    center_freq: Option<u32>,
    #[serde(default)]
    sample_rate: Option<u32>,
    #[serde(default, alias = "channels")]
    channel: Option<Vec<u32>>,
    #[serde(default)]
    redis_url: Option<String>,
    #[serde(default)]
    redis_retry_interval: Option<u64>,
    #[serde(default)]
    source: Option<Source>,
}

/// Command-line options for the standalone `datalink vdl2` frontend.
#[derive(Debug, Default, Clone, Parser)]
#[command(about = "VDL2 frontend for I/Q and SDR inputs")]
pub(crate) struct Cli {
    /// Dump a copy of decoded AVLC frames as JSONL
    #[arg(short, long)]
    output: Option<String>,
    /// Print demod/decode counters to stderr at end
    #[arg(long)]
    stats: bool,
    /// Output lower bound (seconds into recording)
    #[arg(long)]
    window_start_sec: Option<f64>,
    /// Output upper bound (seconds into recording)
    #[arg(long)]
    window_end_sec: Option<f64>,
    /// Preamble sync threshold
    #[arg(long)]
    sync_threshold: Option<f32>,
    /// I/Q sample format for file input: cu8, cs8, cs16, cf32
    #[arg(long)]
    format: Option<String>,
    /// Center frequency for file and SDR sources
    #[arg(long)]
    center_freq: Option<u32>,
    /// Sample rate for file and SDR sources
    #[arg(long)]
    sample_rate: Option<u32>,
    /// VDL2 channel frequencies in Hz
    #[arg(long, num_args = 1..)]
    channel: Option<Vec<u32>>,
    /// Publish decoded application messages to Redis pub/sub topics
    #[arg(long, value_name = "REDIS URL")]
    redis_url: Option<String>,
    /// Retry interval (seconds) when publishing to Redis fails; 0 disables retry
    #[arg(long)]
    redis_retry_interval: Option<u64>,
    /// Source URL: file://, rtlsdr://, airspy://, hackrf://, soapy://
    source: Option<Source>,
}

impl Options {
    fn apply_cli_overrides(&mut self, cli: Cli) {
        if cli.output.is_some() {
            self.output = cli.output;
        }
        if cli.stats {
            self.stats = true;
        }
        if cli.window_start_sec.is_some() {
            self.window_start_sec = cli.window_start_sec;
        }
        if cli.window_end_sec.is_some() {
            self.window_end_sec = cli.window_end_sec;
        }
        if cli.sync_threshold.is_some() {
            self.sync_threshold = cli.sync_threshold;
        }
        if cli.format.is_some() {
            self.format = cli.format;
        }
        if cli.center_freq.is_some() {
            self.center_freq = cli.center_freq;
        }
        if cli.sample_rate.is_some() {
            self.sample_rate = cli.sample_rate;
        }
        if cli.channel.is_some() {
            self.channel = cli.channel;
        }
        if cli.redis_url.is_some() {
            self.redis_url = cli.redis_url;
        }
        if cli.redis_retry_interval.is_some() {
            self.redis_retry_interval = cli.redis_retry_interval;
        }
        if cli.source.is_some() {
            self.source = cli.source;
        }
        apply_source_overrides(self);
    }
}

#[derive(Default)]
struct DecodeStats {
    demod_frames: u64,
    avlc_ok: u64,
    avlc_fcs_ok: u64,
    avlc_fcs_fail: u64,
    avlc_parse_fail: u64,
}

/// Run the standalone VDL2 frontend from CLI arguments.
pub(crate) async fn run(cli: Cli) -> anyhow::Result<()> {
    let mut options = Options::default();
    options.apply_cli_overrides(cli);

    anyhow::ensure!(
        options.source.is_some(),
        "missing source; pass an explicit source such as file://capture.rtl, -, or rtlsdr://"
    );
    run_options(options, "vdl2").await
}

async fn run_options(options: Options, stats_name: &str) -> anyhow::Result<()> {
    let mut output = if let Some(path) = options.output.as_deref() {
        Some(BufWriter::new(File::create(expanduser(path))?))
    } else {
        None
    };
    let mut redis = if let Some(url) = options.redis_url.as_deref() {
        Some(
            RedisPublisher::connect_with_prefix(
                url,
                options.redis_retry_interval.unwrap_or(5),
                "datalink vdl2",
            )
            .await?,
        )
    } else {
        None
    };

    let mut total = DecodeStats::default();
    let src = options.source.as_ref().expect("source checked before run");
    let source_meta = SourceMetadata {
        id: "vdl2_cli".into(),
        name: src.label(),
        class: SourceClass::Iq,
        format: None,
    };
    let stats = decode_source(
        src,
        &options,
        output.as_mut(),
        redis.as_mut(),
        None,
        &source_meta,
        Bearer::Vdl2,
    )
    .await?;
    total.demod_frames += stats.demod_frames;
    total.avlc_ok += stats.avlc_ok;
    total.avlc_fcs_ok += stats.avlc_fcs_ok;
    total.avlc_fcs_fail += stats.avlc_fcs_fail;
    total.avlc_parse_fail += stats.avlc_parse_fail;

    if let Some(w) = output.as_mut() {
        w.flush()?;
    }
    if options.stats {
        eprintln!(
            "{} stats: demod_frames={} avlc_ok={} avlc_fcs_ok={} avlc_fcs_fail={} avlc_parse_fail={}",
            stats_name, total.demod_frames, total.avlc_ok, total.avlc_fcs_ok, total.avlc_fcs_fail, total.avlc_parse_fail
        );
    }

    Ok(())
}

fn apply_source_overrides(options: &mut Options) {
    let Some(source) = options.source.as_mut() else {
        return;
    };
    if options.center_freq.is_some() {
        source.center_freq = options.center_freq;
    }
    if options.sample_rate.is_some() {
        source.sample_rate = options.sample_rate;
    }
    if options.channel.is_some() {
        source.channels = options.channel.clone();
    }
    if options.format.is_some() {
        source.format = options.format.clone();
    }
}

/// Decode a VDL2 file source and collect common decoded events for merged mode.
pub(crate) async fn decode_file_values(
    file: &str,
    format: Option<&str>,
    center_freq: Option<u32>,
    sample_rate: Option<u32>,
    channels: Option<&[u32]>,
    source_meta: &SourceMetadata,
    receiver_bearer: Bearer,
) -> anyhow::Result<Vec<DecodedEvent>> {
    let src = Source {
        address: Address::File {
            file: file.to_string(),
        },
        name: None,
        center_freq,
        sample_rate,
        channels: channels.map(<[u32]>::to_vec),
        gain: None,
        bias_tee: None,
        amp_enable: None,
        rf_gain: None,
        lna_gain: None,
        mixer_gain: None,
        vga_gain: None,
        format: format.map(str::to_string),
    };
    let options = Options {
        source: Some(src.clone()),
        ..Options::default()
    };
    let mut out = Vec::new();
    decode_source(
        &src,
        &options,
        None,
        None,
        Some(&mut out),
        source_meta,
        receiver_bearer,
    )
    .await?;
    Ok(out)
}

#[allow(clippy::too_many_arguments)]
async fn decode_source(
    src: &Source,
    options: &Options,
    mut output: Option<&mut BufWriter<File>>,
    mut redis: Option<&mut RedisPublisher>,
    mut collect: Option<&mut Vec<DecodedEvent>>,
    source_meta: &SourceMetadata,
    receiver_bearer: Bearer,
) -> anyhow::Result<DecodeStats> {
    if let Address::File { file } = &src.address {
        if file.to_ascii_lowercase().ends_with(".wav") {
            return decode_wav_source(
                src,
                file,
                options,
                output,
                redis,
                collect,
                source_meta,
                receiver_bearer,
            )
            .await;
        }
    }

    let effective_src = effective_file_source(src, None);
    let center_freq = effective_src.center_freq_or(DEFAULT_CENTER_FREQ);
    let raw_sample_rate = effective_src.sample_rate();
    let channels = effective_src.channels_with(auto_channels);
    let sync_threshold = options.sync_threshold.unwrap_or(3.2);
    let demod_sample_rate = SYMBOL_RATE * 100; // 1.05 Msps, then Vdl2Channel decimates to 105 ksps.
    let mut channelizer = Channelizer::from_absolute_frequencies(
        center_freq,
        raw_sample_rate,
        demod_sample_rate,
        &channels,
    )
    .map_err(|err| anyhow::anyhow!(err))?;

    let mut demods: Vec<Vdl2Channel> = channels
        .iter()
        .map(|&ch_freq| {
            let mut d = Vdl2Channel::new(demod_sample_rate as f32, 0.0, ch_freq as f32);
            d.set_sync_threshold(sync_threshold);
            d
        })
        .collect();

    let mut stream = open_source(&effective_src).await?;
    let run_start = SystemTime::now();
    let mut sample_indices = vec![0_u64; channels.len()];
    let mut stats = DecodeStats::default();

    while let Some(chunk_result) = stream.next().await {
        let chunk = chunk_result?;
        for channel_chunk in channelizer.process(&chunk) {
            let idx = channel_chunk.channel_index;
            for sample in channel_chunk.samples {
                let one = std::slice::from_mut(&mut demods[idx]);
                let mut passthrough = ResampleAdapter::new(None);
                let mut pipeline = IqPipeline::new(
                    &mut passthrough,
                    one,
                    &mut sample_indices[idx],
                    demod_sample_rate,
                    run_start,
                );
                let frames =
                    collect_iq_frames(&mut pipeline, sample.re, sample.im, |d, re, im| {
                        Ok(d.process_sample(re, im))
                    })?;
                for (mut ctx, demod_frame) in frames {
                    ctx.channel_index = idx;
                    handle_avlc_frame(
                        &channels,
                        options,
                        output.as_deref_mut(),
                        redis.as_deref_mut(),
                        collect.as_deref_mut(),
                        &mut stats,
                        ctx,
                        demod_frame,
                        source_meta,
                        receiver_bearer,
                    )
                    .await?;
                }
            }
        }
    }
    Ok(stats)
}

/// Parse one demodulated AVLC frame and emit it to configured sinks.
#[allow(clippy::too_many_arguments)]
async fn handle_avlc_frame(
    channels: &[u32],
    options: &Options,
    mut output: Option<&mut BufWriter<File>>,
    redis: Option<&mut RedisPublisher>,
    mut collect: Option<&mut Vec<DecodedEvent>>,
    stats: &mut DecodeStats,
    ctx: FrameContext,
    demod_frame: acars::demod::vdl2::DemodFrame,
    source_meta: &SourceMetadata,
    receiver_bearer: Bearer,
) -> anyhow::Result<()> {
    stats.demod_frames += 1;
    match parse_avlc_frame(&demod_frame.bytes) {
        Ok(avlc) => {
            stats.avlc_ok += 1;
            if avlc.fcs_ok {
                stats.avlc_fcs_ok += 1;
            } else {
                stats.avlc_fcs_fail += 1;
            }
            if !avlc.fcs_ok {
                return Ok(());
            }
            if !in_window(
                ctx.seconds_into_recording,
                options.window_start_sec,
                options.window_end_sec,
            ) {
                return Ok(());
            }

            let pmsg = ProtocolMessage::Avlc(Box::new(avlc));

            let event = DecodedEvent {
                event: "message".to_string(),
                timestamp: Some(ctx.timestamp_unix),
                bearer: receiver_bearer,
                source: source_meta.clone(),
                receiver: Some(ReceiverMetadata {
                    bearer: receiver_bearer,
                    channel_hz: Some(channels[ctx.channel_index]),
                }),
                aircraft: crate::merged::aircraft_summary(&pmsg),
                kinematics: pmsg.kinematics(),
                raw_frame_hex: Some(bytes_to_hex(&demod_frame.bytes)),
                message: pmsg,
            };

            if let Some(values) = collect.as_mut() {
                values.push(event);
            } else {
                let line = serde_json::to_string(&event)?;
                println!("{line}");
                if let Some(w) = output.as_mut() {
                    writeln!(w, "{line}")?;
                }
                if let Some(redis) = redis {
                    redis
                        .publish(crate::util::redis_topic_for_record(&event.message), &line)
                        .await;
                }
            }
        }
        Err(_err) => {
            stats.avlc_parse_fail += 1;
        }
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
async fn decode_wav_source(
    src: &Source,
    file: &str,
    options: &Options,
    mut output: Option<&mut BufWriter<File>>,
    mut redis: Option<&mut RedisPublisher>,
    mut collect: Option<&mut Vec<DecodedEvent>>,
    source_meta: &SourceMetadata,
    receiver_bearer: Bearer,
) -> anyhow::Result<DecodeStats> {
    let mut reader = hound::WavReader::open(expanduser(file))?;
    let spec = reader.spec();
    anyhow::ensure!(spec.channels == 2, "VDL2 WAV input must be stereo I/Q");
    anyhow::ensure!(
        spec.sample_format == hound::SampleFormat::Int && spec.bits_per_sample == 16,
        "VDL2 WAV input currently supports 16-bit PCM stereo I/Q"
    );
    let raw_sample_rate = spec.sample_rate;
    let effective_src = effective_file_source(src, Some(raw_sample_rate));
    let center_freq = effective_src.center_freq_or(DEFAULT_CENTER_FREQ);
    let channels = effective_src.channels_with(auto_channels);
    let sync_threshold = options.sync_threshold.unwrap_or(3.2);
    let vdl2_decimated_rate = SYMBOL_RATE * 10;
    let (sample_rate, resample_rs) = maybe_resample(raw_sample_rate, vdl2_decimated_rate);
    let mut adapter = ResampleAdapter::new(resample_rs);
    let mut demods: Vec<Vdl2Channel> = channels
        .iter()
        .map(|&ch_freq| {
            let mut d = Vdl2Channel::new(
                sample_rate as f32,
                ch_freq as f32 - center_freq as f32,
                ch_freq as f32,
            );
            d.set_sync_threshold(sync_threshold);
            d
        })
        .collect();
    let run_start = SystemTime::now();
    let mut sample_index: u64 = 0;
    let mut stats = DecodeStats::default();
    let mut samples = reader.samples::<i16>();
    while let (Some(i), Some(q)) = (samples.next(), samples.next()) {
        let i = i? as f32 / 32768.0;
        let q = q? as f32 / 32768.0;
        let mut pipeline = IqPipeline::new(
            &mut adapter,
            &mut demods,
            &mut sample_index,
            sample_rate,
            run_start,
        );
        let frames =
            collect_iq_frames(
                &mut pipeline,
                i,
                q,
                |d, re, im| Ok(d.process_sample(re, im)),
            )?;
        for (ctx, demod_frame) in frames {
            handle_avlc_frame(
                &channels,
                options,
                output.as_deref_mut(),
                redis.as_deref_mut(),
                collect.as_deref_mut(),
                &mut stats,
                ctx,
                demod_frame,
                source_meta,
                receiver_bearer,
            )
            .await?;
        }
    }
    Ok(stats)
}

fn effective_file_source(src: &Source, sample_rate_override: Option<u32>) -> Source {
    let inferred = file_source_path(src).and_then(infer_capture_params);
    let mut effective_src = src.clone();
    if effective_src.center_freq.is_none() {
        effective_src.center_freq = inferred.map(|params| params.center_freq);
    }
    effective_src.sample_rate = sample_rate_override
        .or(effective_src.sample_rate)
        .or_else(|| inferred.and_then(|params| params.sample_rate));
    if effective_src.format.is_none() {
        effective_src.format = inferred.and_then(|params| params.format.map(str::to_string));
    }
    effective_src
}

fn file_source_path(src: &Source) -> Option<&str> {
    match &src.address {
        Address::File { file } => Some(file.as_str()),
        _ => None,
    }
}

/// Open a VDL2-compatible file, stdin, or SDR source as an async I/Q stream.
pub(crate) async fn open_source(src: &Source) -> anyhow::Result<desperado::IqAsyncSource> {
    use desperado::{DeviceConfig, IqAsyncSource};

    let center_freq = src.center_freq_or(DEFAULT_CENTER_FREQ);
    let sample_rate = src.sample_rate();
    match &src.address {
        Address::File { file } if file == "-" => Ok(IqAsyncSource::from_stdin(
            center_freq,
            sample_rate,
            DEFAULT_CHUNK_SIZE,
            src.iq_format(),
        )),
        Address::File { file } => Ok(IqAsyncSource::from_file(
            expanduser(file),
            center_freq,
            sample_rate,
            DEFAULT_CHUNK_SIZE,
            src.iq_format(),
        )
        .await?),
        #[cfg(feature = "rtlsdr")]
        Address::Rtlsdr { device, serial } => {
            let selector = if let Some(serial) = serial {
                desperado::rtlsdr::DeviceSelector::Filter {
                    manufacturer: None,
                    product: None,
                    serial: Some(serial.clone()),
                }
            } else {
                desperado::rtlsdr::DeviceSelector::Index(device.unwrap_or(0))
            };
            let cfg = desperado::rtlsdr::RtlSdrConfig {
                device: selector,
                center_freq,
                sample_rate,
                gain: src.gain_or(desperado::Gain::Manual(49.6)),
                bias_tee: src.bias_tee.unwrap_or(false),
                freq_correction_ppm: 0,
            };
            Ok(IqAsyncSource::from_device_config(&DeviceConfig::RtlSdr(cfg)).await?)
        }
        #[cfg(feature = "airspy")]
        Address::Airspy { device, serial } => {
            let selector = if let Some(serial) = serial {
                desperado::airspy::DeviceSelector::Serial(crate::util::parse_airspy_serial(serial)?)
            } else {
                desperado::airspy::DeviceSelector::Index(device.unwrap_or(0))
            };
            let cfg = desperado::airspy::AirspyConfig {
                device: selector,
                center_freq,
                sample_rate,
                gain: src.gain_or(desperado::Gain::Manual(50.0)),
                bias_tee: src.bias_tee.unwrap_or(false),
                packing: false,
                lna_gain: src.lna_gain.map(|v| v as u8),
                mixer_gain: src.mixer_gain.map(|v| v as u8),
                vga_gain: src.vga_gain.map(|v| v as u8),
                gain_mode: desperado::airspy::AirspyGainMode::Sensitivity,
            };
            Ok(IqAsyncSource::from_device_config(&DeviceConfig::Airspy(cfg)).await?)
        }
        #[cfg(feature = "hackrf")]
        Address::Hackrf { device } => {
            let cfg = desperado::hackrf::HackRfConfig {
                device_index: device.unwrap_or(0),
                center_freq: center_freq as u64,
                sample_rate,
                gain: crate::util::hackrf_gain(src),
                amp_enable: src.hackrf_amp_enable(),
                bias_tee: src.bias_tee.unwrap_or(false),
            };
            Ok(IqAsyncSource::from_device_config(&DeviceConfig::HackRf(cfg)).await?)
        }
        #[cfg(feature = "soapy")]
        Address::Soapy { soapy } => {
            let cfg = desperado::soapy::SoapyConfig {
                args: soapy.clone(),
                center_freq: center_freq as f64,
                sample_rate: sample_rate as f64,
                channel: 0,
                gain: src.gain_or(desperado::Gain::Manual(49.6)),
                bias_tee: src.bias_tee.unwrap_or(false),
            };
            Ok(IqAsyncSource::from_device_config(&DeviceConfig::Soapy(cfg)).await?)
        }
        #[allow(unreachable_patterns)]
        _ => Err(anyhow::anyhow!("source type is not enabled in this build")),
    }
}

fn in_window(seconds: f64, start: Option<f64>, end: Option<f64>) -> bool {
    if let Some(s) = start {
        if seconds < s {
            return false;
        }
    }
    if let Some(e) = end {
        if seconds > e {
            return false;
        }
    }
    true
}

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

    fn file_source(file: &str) -> Source {
        Source {
            address: Address::File {
                file: file.to_string(),
            },
            name: None,
            center_freq: None,
            sample_rate: None,
            channels: None,
            gain: None,
            bias_tee: None,
            amp_enable: None,
            rf_gain: None,
            lna_gain: None,
            mixer_gain: None,
            vga_gain: None,
            format: None,
        }
    }

    #[test]
    fn file_auto_channels_use_gqrx_inferred_sample_rate() {
        let src = file_source("gqrx_20260518_114025_136500000_1800000_fc.raw");
        let effective = effective_file_source(&src, None);
        assert_eq!(effective.center_freq_or(DEFAULT_CENTER_FREQ), 136_500_000);
        assert_eq!(effective.sample_rate(), 1_800_000);
        assert_eq!(effective.format.as_deref(), Some("cf32"));
        assert_eq!(
            effective.channels_with(auto_channels).first(),
            Some(&136_100_000)
        );
        assert_eq!(
            effective.channels_with(auto_channels).last(),
            Some(&136_975_000)
        );
    }

    #[test]
    fn wav_auto_channels_use_inferred_center_and_wav_sample_rate() {
        let src = file_source("SDRuno_20200908_152020Z_136650kHz.wav");
        let effective = effective_file_source(&src, Some(250_000));
        assert_eq!(effective.center_freq_or(DEFAULT_CENTER_FREQ), 136_650_000);
        assert_eq!(effective.sample_rate(), 250_000);
        assert_eq!(
            effective.channels_with(auto_channels),
            vec![
                136_650_000,
                136_675_000,
                136_700_000,
                136_725_000,
                136_750_000,
            ]
        );
    }
}