rfe 0.1.0

Communicate with RF Explorer spectrum analyzers and signal generators over USB serial
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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
use std::{
    fmt::Debug,
    io,
    ops::RangeInclusive,
    sync::{Arc, Condvar, Mutex, MutexGuard, WaitTimeoutResult},
    thread,
    time::Duration,
};

use tracing::{error, info, trace, warn};

use super::{
    CalcMode, Command, Config, DspMode, InputStage, Mode, Model, Sweep, TrackingStatus, WifiBand,
};
use crate::rf_explorer::{
    COMMAND_RESPONSE_TIMEOUT, ConfigCallback, NEXT_SCREEN_DATA_TIMEOUT,
    RECEIVE_INITIAL_DEVICE_INFO_TIMEOUT, ScreenData, SerialNumber, SetupInfo, impl_rf_explorer,
};
use crate::{ConnectionError, ConnectionResult, Device, Error, Frequency, Result};

#[derive(Debug)]
/// RF Explorer spectrum analyzer device.
pub struct SpectrumAnalyzer {
    rfe: Device<MessageContainer>,
}

impl_rf_explorer!(SpectrumAnalyzer, MessageContainer);

impl SpectrumAnalyzer {
    const MIN_MAX_AMP_RANGE_DBM: RangeInclusive<i16> = -120..=35;
    const MIN_SWEEP_LEN: u16 = 112;
    const NEXT_SWEEP_TIMEOUT: Duration = Duration::from_secs(2);

    /// The serial number of the RF Explorer, if it exists.
    pub fn serial_number(&self) -> Option<String> {
        // Return the serial number if we've already received it
        if let Some(ref serial_number) = *self.messages().serial_number.0.lock().unwrap() {
            return Some(serial_number.to_string());
        }

        // If we haven't already received the serial number, request it from the RF Explorer
        self.send_command(crate::rf_explorer::Command::RequestSerialNumber)
            .ok()?;

        // Wait 2 seconds for the RF Explorer to send its serial number
        let (lock, cvar) = &self.messages().serial_number;
        tracing::trace!("Waiting to receive SerialNumber from RF Explorer");
        let _ = cvar
            .wait_timeout_while(
                lock.lock().unwrap(),
                std::time::Duration::from_secs(2),
                |serial_number| serial_number.is_none(),
            )
            .unwrap();

        (*self.messages().serial_number.0.lock().unwrap())
            .as_ref()
            .map(|sn| sn.to_string())
    }

    /// The firmware version of the RF Explorer.
    pub fn firmware_version(&self) -> String {
        self.messages()
            .setup_info
            .0
            .lock()
            .unwrap()
            .as_ref()
            .map(|setup_info| setup_info.firmware_version.clone())
            .unwrap_or_default()
    }

    fn config(&'_ self) -> MutexGuard<'_, Option<Config>> {
        self.messages().config.0.lock().unwrap()
    }

    /// The start frequency of the RF Explorer's sweeps.
    pub fn start_freq(&self) -> Frequency {
        self.config()
            .as_ref()
            .map(|config| config.start_freq)
            .unwrap_or_default()
    }

    /// The step size of the RF Explorer's sweeps.
    pub fn step_size(&self) -> Frequency {
        self.config()
            .as_ref()
            .map(|config| config.step_size)
            .unwrap_or_default()
    }

    /// The stop frequency of the RF Explorer's sweeps.
    pub fn stop_freq(&self) -> Frequency {
        self.config()
            .as_ref()
            .map(|config| config.stop_freq)
            .unwrap_or_default()
    }

    /// The center frequency of the RF Explorer's sweeps.
    pub fn center_freq(&self) -> Frequency {
        self.config()
            .as_ref()
            .map(|config| config.center_freq)
            .unwrap_or_default()
    }

    /// The span of the RF Explorer's sweeps.
    pub fn span(&self) -> Frequency {
        self.config()
            .as_ref()
            .map(|config| config.span)
            .unwrap_or_default()
    }

    /// The minimum supported frequency of the RF Explorer.
    pub fn min_freq(&self) -> Frequency {
        self.config()
            .as_ref()
            .map(|config| config.min_freq)
            .unwrap_or_default()
    }

    /// The maximum supported frequency of the RF Explorer.
    pub fn max_freq(&self) -> Frequency {
        self.config()
            .as_ref()
            .map(|config| config.max_freq)
            .unwrap_or_default()
    }

    /// The maximum supported span of the RF Explorer.
    pub fn max_span(&self) -> Frequency {
        self.config()
            .as_ref()
            .map(|config| config.max_span)
            .unwrap_or_default()
    }

    /// The resolution bandwidth of the RF Explorer.
    pub fn rbw(&self) -> Option<Frequency> {
        self.config()
            .as_ref()
            .map(|config| config.rbw)
            .unwrap_or_default()
    }

    /// The minimum amplitude of sweeps displayed on the RF Explorer's screen.
    pub fn min_amp_dbm(&self) -> i16 {
        self.config()
            .as_ref()
            .map(|config| config.min_amp_dbm)
            .unwrap_or_default()
    }

    /// The maximum amplitude of sweeps displayed on the RF Explorer's screen.
    pub fn max_amp_dbm(&self) -> i16 {
        self.config()
            .as_ref()
            .map(|config| config.max_amp_dbm)
            .unwrap_or_default()
    }

    /// The amplitude offset of sweeps displayed on the RF Explorer's screen.
    pub fn amp_offset_db(&self) -> Option<i8> {
        self.config()
            .as_ref()
            .map(|config| config.amp_offset_db)
            .unwrap_or_default()
    }

    /// The number of amplitudes in the RF Explorer's sweeps.
    pub fn sweep_len(&self) -> u16 {
        self.config()
            .as_ref()
            .map(|config| config.sweep_len)
            .unwrap_or_default()
    }

    fn is_expansion_radio_module_active(&self) -> bool {
        self.config()
            .as_ref()
            .map(|config| config.is_expansion_radio_module_active)
            .unwrap_or_default()
    }

    /// The current `Mode` of the RF Explorer.
    pub fn mode(&self) -> Mode {
        self.config()
            .as_ref()
            .map(|config| config.mode)
            .unwrap_or_default()
    }

    /// The current `CalcMode` of the RF Explorer.
    pub fn calc_mode(&self) -> Option<CalcMode> {
        self.config()
            .as_ref()
            .map(|config| config.calc_mode)
            .unwrap_or_default()
    }

    /// The amplitudes of the most recent sweep measured by the RF Explorer.
    pub fn sweep(&self) -> Option<Vec<f32>> {
        self.rfe
            .messages()
            .sweep
            .0
            .lock()
            .unwrap()
            .as_ref()
            .map(|sweep| sweep.amplitudes_dbm.clone())
    }

    /// Fills the buffer with the amplitudes of the most recent sweep and returns the length of the sweep.
    pub fn fill_buf_with_sweep(&self, buf: &mut [f32]) -> Result<usize> {
        let sweep = self.messages().sweep.0.lock().unwrap();
        let Some(sweep) = sweep.as_ref() else {
            return Err(Error::InvalidOperation(
                "No sweeps have been measured by the RF Explorer".to_string(),
            ));
        };

        let sweep_len = sweep.amplitudes_dbm.len();
        if buf.len() >= sweep_len {
            buf[0..sweep_len].copy_from_slice(sweep.amplitudes_dbm.as_slice());
            Ok(sweep_len)
        } else {
            Err(Error::InvalidInput(
                "The buffer is too small to fit the sweep".to_string(),
            ))
        }
    }

    /// Waits for the RF Explorer to measure the next sweep.
    pub fn wait_for_next_sweep(&self) -> Result<Vec<f32>> {
        self.wait_for_next_sweep_with_timeout(Self::NEXT_SWEEP_TIMEOUT)
    }

    /// Waits for the RF Explorer to measure the next sweep and fills the buffer with its amplitudes.
    pub fn wait_for_next_sweep_and_fill_buf(&self, buf: &mut [f32]) -> Result<usize> {
        self.wait_for_next_sweep_with_timeout_and_fill_buf(Self::NEXT_SWEEP_TIMEOUT, buf)
    }

    /// Waits for the RF Explorer to measure the next sweep or for the timeout duration to elapse.
    pub fn wait_for_next_sweep_with_timeout(&self, timeout: Duration) -> Result<Vec<f32>> {
        let previous_sweep_timestamp = self
            .rfe
            .messages()
            .sweep
            .0
            .lock()
            .unwrap()
            .as_ref()
            .map(|sweep| sweep.timestamp);

        let (sweep, cond_var) = &self.messages().sweep;
        // Wait until the timestamp of the previous sweep and the next sweep are different
        let (sweep, wait_result) = cond_var
            .wait_timeout_while(sweep.lock().unwrap(), timeout, |sweep| {
                sweep.as_ref().map(|sweep| sweep.timestamp) == previous_sweep_timestamp
                    || sweep.is_none()
            })
            .unwrap();

        match &*sweep {
            Some(sweep) if !wait_result.timed_out() => Ok(sweep.amplitudes_dbm.clone()),
            _ => Err(Error::TimedOut(timeout)),
        }
    }

    /// Waits for the RF Explorer to measure the next sweep, or for the timeout duration to elapse,
    /// and fills the buffer with its amplitudes.
    pub fn wait_for_next_sweep_with_timeout_and_fill_buf(
        &self,
        timeout: Duration,
        buf: &mut [f32],
    ) -> Result<usize> {
        let previous_sweep_timestamp = self
            .rfe
            .messages()
            .sweep
            .0
            .lock()
            .unwrap()
            .as_ref()
            .map(|sweep| sweep.timestamp);

        let (sweep, cond_var) = &self.messages().sweep;
        // Wait until the timestamp of the previous sweep and the next sweep are different
        let (sweep, wait_result) = cond_var
            .wait_timeout_while(sweep.lock().unwrap(), timeout, |sweep| {
                sweep.as_ref().map(|sweep| sweep.timestamp) == previous_sweep_timestamp
                    || sweep.is_none()
            })
            .unwrap();
        drop(sweep);

        if !wait_result.timed_out() {
            self.fill_buf_with_sweep(buf)
        } else {
            Err(Error::TimedOut(timeout))
        }
    }

    /// Returns the most recent `ScreenData` captured by the RF Explorer.
    pub fn screen_data(&self) -> Option<ScreenData> {
        self.messages().screen_data.0.lock().unwrap().clone()
    }

    /// Waits for the RF Explorer to capture its next `ScreenData`.
    pub fn wait_for_next_screen_data(&self) -> Result<ScreenData> {
        self.wait_for_next_screen_data_with_timeout(NEXT_SCREEN_DATA_TIMEOUT)
    }

    /// Waits for the RF Explorer to capture its next `ScreenData` or for the timeout duration to elapse.
    pub fn wait_for_next_screen_data_with_timeout(&self, timeout: Duration) -> Result<ScreenData> {
        let previous_screen_data = self.screen_data();

        let (screen_data, condvar) = &self.messages().screen_data;
        let (screen_data, wait_result) = condvar
            .wait_timeout_while(screen_data.lock().unwrap(), timeout, |screen_data| {
                *screen_data == previous_screen_data || screen_data.is_none()
            })
            .unwrap();

        match &*screen_data {
            Some(screen_data) if !wait_result.timed_out() => Ok(screen_data.clone()),
            _ => Err(Error::TimedOut(timeout)),
        }
    }

    /// Returns the RF Explorer's DSP mode.
    pub fn dsp_mode(&self) -> Option<DspMode> {
        *self.messages().dsp_mode.0.lock().unwrap()
    }

    /// Returns the status of tracking mode (enabled or disabled).
    pub fn tracking_status(&self) -> Option<TrackingStatus> {
        *self.messages().tracking_status.0.lock().unwrap()
    }

    /// Returns the spectrum analyzer's input stage, if reported by the device.
    pub fn input_stage(&self) -> Option<InputStage> {
        *self.messages().input_stage.0.lock().unwrap()
    }

    /// Returns the main radio's model.
    pub fn main_radio_model(&self) -> Option<Model> {
        self.messages()
            .setup_info
            .0
            .lock()
            .unwrap()
            .as_ref()
            .unwrap()
            .main_radio_model
    }

    /// Returns the expansion radio's model (if one exists).
    pub fn expansion_radio_model(&self) -> Option<Model> {
        self.rfe
            .messages()
            .setup_info
            .0
            .lock()
            .unwrap()
            .as_ref()
            .unwrap()
            .expansion_radio_model
    }

    /// Returns the active radio module.
    pub fn active_radio_model(&self) -> Model {
        if self.is_expansion_radio_module_active() {
            self.expansion_radio_model().unwrap_or_default()
        } else {
            self.main_radio_model().unwrap_or_default()
        }
    }

    /// Returns the inactive radio module (if one exists).
    pub fn inactive_radio_model(&self) -> Option<Model> {
        let expansion_radio_model = self.expansion_radio_model();
        if expansion_radio_model.is_some() {
            if self.is_expansion_radio_module_active() {
                self.main_radio_model()
            } else {
                expansion_radio_model
            }
        } else {
            None
        }
    }

    /// Starts the spectrum analyzer's Wi-Fi analyzer.
    #[tracing::instrument]
    pub fn start_wifi_analyzer(&self, wifi_band: WifiBand) -> io::Result<()> {
        self.send_command(Command::StartWifiAnalyzer(wifi_band))
    }

    /// Stops the spectrum analyzer's Wi-Fi analyzer.
    #[tracing::instrument(skip(self))]
    pub fn stop_wifi_analyzer(&self) -> io::Result<()> {
        self.send_command(Command::StopWifiAnalyzer)
    }

    /// Requests the spectrum analyzer enter tracking mode.
    #[tracing::instrument(skip(self))]
    pub fn request_tracking(&self, start_hz: u64, step_hz: u64) -> Result<TrackingStatus> {
        // Set the tracking status to None so we can tell whether or not we've received a new
        // tracking status message by checking for Some
        *self.messages().tracking_status.0.lock().unwrap() = None;

        // Send the command to enter tracking mode
        self.send_command(Command::StartTracking {
            start: Frequency::from_hz(start_hz),
            step: Frequency::from_hz(step_hz),
        })?;

        // Wait to see if we receive a tracking status message in response
        let (lock, condvar) = &self.messages().tracking_status;
        let (tracking_status, wait_result) = condvar
            .wait_timeout_while(
                lock.lock().unwrap(),
                COMMAND_RESPONSE_TIMEOUT,
                |tracking_status| tracking_status.is_none(),
            )
            .unwrap();

        if !wait_result.timed_out() {
            Ok(tracking_status.unwrap_or_default())
        } else {
            Err(Error::TimedOut(COMMAND_RESPONSE_TIMEOUT))
        }
    }

    /// Steps over the tracking step frequency and makes a measurement.
    #[tracing::instrument(skip(self))]
    pub fn tracking_step(&self, step: u16) -> io::Result<()> {
        self.send_command(Command::TrackingStep(step))
    }

    /// Activates the RF Explorer's main radio.
    pub fn activate_main_radio(&self) -> Result<()> {
        if !self.is_expansion_radio_module_active() {
            return Err(Error::InvalidOperation(
                "Main radio module is already active.".to_string(),
            ));
        }

        self.send_command(Command::SwitchModuleMain)?;

        // Wait until config shows that the main radio module is active
        let _ = self.wait_for_config_while(|config| {
            config
                .as_ref()
                .filter(|config| !config.is_expansion_radio_module_active)
                .is_none()
        });

        if !self.is_expansion_radio_module_active() {
            Ok(())
        } else {
            Err(Error::TimedOut(COMMAND_RESPONSE_TIMEOUT))
        }
    }

    /// Activates the RF Explorer's expansion radio (if one exists).
    pub fn activate_expansion_radio(&self) -> Result<()> {
        if self.expansion_radio_model().is_none() {
            return Err(Error::InvalidOperation(
                "This RF Explorer does not contain an expansion radio module.".to_string(),
            ));
        }

        if self.is_expansion_radio_module_active() {
            return Err(Error::InvalidOperation(
                "Expansion radio module is already active.".to_string(),
            ));
        }

        self.send_command(Command::SwitchModuleExp)?;

        // Wait until config shows that the expansion radio module is active
        let _ = self.wait_for_config_while(|config| {
            config
                .as_ref()
                .filter(|config| config.is_expansion_radio_module_active)
                .is_none()
        });

        if self.is_expansion_radio_module_active() {
            Ok(())
        } else {
            Err(Error::TimedOut(COMMAND_RESPONSE_TIMEOUT))
        }
    }

    /// Sets the start and stop frequency of sweeps measured by the spectrum analyzer.
    pub fn set_start_stop(
        &self,
        start: impl Into<Frequency>,
        stop: impl Into<Frequency>,
    ) -> Result<()> {
        self.set_config(
            start.into(),
            stop.into(),
            self.min_amp_dbm(),
            self.max_amp_dbm(),
        )
    }

    /// Sets the start frequency, stop frequency, and number of points of sweeps measured by the spectrum analyzer.
    pub fn set_start_stop_sweep_len(
        &self,
        start: impl Into<Frequency>,
        stop: impl Into<Frequency>,
        sweep_len: u16,
    ) -> Result<()> {
        self.set_sweep_len(sweep_len)?;
        self.set_start_stop(start, stop)
    }

    /// Sets the center frequency and span of sweeps measured by the spectrum analyzer.
    pub fn set_center_span(
        &self,
        center: impl Into<Frequency>,
        span: impl Into<Frequency>,
    ) -> Result<()> {
        let (start, stop) = self.start_stop_from_center_span(center.into(), span.into())?;
        self.set_start_stop(start, stop)
    }

    /// Sets the center frequency, span, and number of points of sweeps measured by the spectrum analyzer.
    pub fn set_center_span_sweep_len(
        &self,
        center: impl Into<Frequency>,
        span: impl Into<Frequency>,
        sweep_len: u16,
    ) -> Result<()> {
        let (start, stop) = self.start_stop_from_center_span(center.into(), span.into())?;
        self.set_start_stop_sweep_len(start, stop, sweep_len)
    }

    /// Sets the minimum and maximum amplitudes displayed on the RF Explorer's screen.
    #[tracing::instrument(skip(self))]
    pub fn set_min_max_amps(&self, min_amp_dbm: i16, max_amp_dbm: i16) -> Result<()> {
        self.set_config(
            self.start_freq(),
            self.stop_freq(),
            min_amp_dbm,
            max_amp_dbm,
        )
    }

    /// Sets the spectrum analyzer's configuration.
    #[tracing::instrument(skip(self), ret, err)]
    fn set_config(
        &self,
        start: Frequency,
        stop: Frequency,
        min_amp_dbm: i16,
        max_amp_dbm: i16,
    ) -> Result<()> {
        self.validate_start_stop(start, stop)?;
        self.validate_min_max_amps(min_amp_dbm, max_amp_dbm)?;

        self.send_command(Command::SetConfig {
            start,
            stop,
            min_amp_dbm,
            max_amp_dbm,
        })?;

        // Check if the current config already contains the requested values
        if self
            .config()
            .as_ref()
            .unwrap_or(&Config::default())
            .contains_start_stop_amp_range(start, stop, min_amp_dbm, max_amp_dbm)
        {
            return Ok(());
        }

        // Wait until the current config contains the requested values
        trace!("Waiting to receive updated 'Config'");
        let (config, wait_result) = self.wait_for_config_while(|config| {
            let Some(config) = config else {
                return true;
            };

            !config.contains_start_stop_amp_range(start, stop, min_amp_dbm, max_amp_dbm)
        });
        drop(config);

        if !wait_result.timed_out() {
            Ok(())
        } else {
            Err(Error::TimedOut(COMMAND_RESPONSE_TIMEOUT))
        }
    }

    /// Sets the callback that is called when the spectrum analyzer receives a sweep.
    pub fn set_sweep_callback(
        &self,
        cb: impl Fn(&[f32], Frequency, Frequency) + Send + Sync + 'static,
    ) {
        *self.messages().sweep_callback.lock().unwrap() = Some(Arc::new(Box::new(cb)));
    }

    /// Removes the callback that is called when the spectrum analyzer receives a `Sweep`.
    pub fn remove_sweep_callback(&self) {
        *self.messages().sweep_callback.lock().unwrap() = None;
    }

    /// Sets the callback that is called when the spectrum analyzer receives a `Config`.
    pub fn set_config_callback(&self, cb: impl Fn(Config) + Send + Sync + 'static) {
        *self.messages().config_callback.lock().unwrap() = Some(Arc::new(Box::new(cb)));
    }

    /// Removes the callback that is called when the spectrum analyzer receives a `Config`.
    pub fn remove_config_callback(&self) {
        *self.messages().config_callback.lock().unwrap() = None;
    }

    /// Sets the number of points in each sweep measured by the spectrum analyzer.
    #[tracing::instrument(skip(self))]
    pub fn set_sweep_len(&self, sweep_len: u16) -> Result<()> {
        // Only 'Plus' models can set the number of points in a sweep
        if !self.active_radio_model().is_plus_model() {
            return Err(Error::InvalidOperation(
                "Only RF Explorer 'Plus' models support setting the number of sweep points"
                    .to_string(),
            ));
        }

        if sweep_len < Self::MIN_SWEEP_LEN {
            return Err(Error::InvalidInput(format!(
                "The sweep length must be at least {}",
                Self::MIN_SWEEP_LEN
            )));
        }

        if sweep_len <= 4096 {
            self.send_command(Command::SetSweepPointsExt(sweep_len))?;
        } else {
            self.send_command(Command::SetSweepPointsLarge(sweep_len))?;
        }

        // The requested number of sweep points gets rounded down to a number that's a multiple of 16
        let expected_sweep_len = (sweep_len / 16) * 16;

        // Check if the current config already contains the requested sweep points
        if self.sweep_len() == expected_sweep_len {
            return Ok(());
        }

        // Wait until the current config contains the requested sweep points
        info!("Waiting to receive updated config");
        let (config, wait_result) = self.wait_for_config_while(|config| {
            config
                .as_ref()
                .filter(|config| config.sweep_len == expected_sweep_len)
                .is_none()
        });
        drop(config);

        if !wait_result.timed_out() {
            Ok(())
        } else {
            warn!("Failed to receive updated config");
            Err(Error::TimedOut(COMMAND_RESPONSE_TIMEOUT))
        }
    }

    /// Sets the spectrum analyzer's calculator mode.
    #[tracing::instrument(skip(self))]
    pub fn set_calc_mode(&self, calc_mode: CalcMode) -> io::Result<()> {
        self.send_command(Command::SetCalcMode(calc_mode))
    }

    /// Sets the spectrum analyzer's input stage.
    #[tracing::instrument(skip(self))]
    pub fn set_input_stage(&self, input_stage: InputStage) -> io::Result<()> {
        self.send_command(Command::SetInputStage(input_stage))
    }

    /// Adds or subtracts an offset to the amplitudes in each sweep.
    #[tracing::instrument(skip(self))]
    pub fn set_offset_db(&self, offset_db: i8) -> io::Result<()> {
        self.send_command(Command::SetOffsetDB(offset_db))
    }

    /// Sets the spectrum analyzer's DSP mode.
    #[tracing::instrument(skip(self))]
    pub fn set_dsp_mode(&self, dsp_mode: DspMode) -> Result<()> {
        // Check to see if the DspMode is already set to the desired value
        if *self.messages().dsp_mode.0.lock().unwrap() == Some(dsp_mode) {
            return Ok(());
        }

        // Send the command to set the DSP mode
        self.send_command(Command::SetDsp(dsp_mode))?;

        // Wait to see if we receive a DSP mode message in response
        let (lock, condvar) = &self.messages().dsp_mode;
        let (dsp_mode, wait_result) = condvar
            .wait_timeout_while(
                lock.lock().unwrap(),
                COMMAND_RESPONSE_TIMEOUT,
                |new_dsp_mode| *new_dsp_mode != Some(dsp_mode),
            )
            .unwrap();
        drop(dsp_mode);

        if !wait_result.timed_out() {
            Ok(())
        } else {
            Err(Error::TimedOut(COMMAND_RESPONSE_TIMEOUT))
        }
    }

    fn wait_for_config_while(
        &'_ self,
        condition: impl FnMut(&mut Option<Config>) -> bool,
    ) -> (MutexGuard<'_, Option<Config>>, WaitTimeoutResult) {
        let (lock, condvar) = &self.messages().config;
        condvar
            .wait_timeout_while(lock.lock().unwrap(), COMMAND_RESPONSE_TIMEOUT, condition)
            .unwrap()
    }

    fn start_stop_from_center_span(
        &self,
        center: Frequency,
        span: Frequency,
    ) -> Result<(Frequency, Frequency)> {
        let half_span_hz = span.as_hz() / 2;
        if center.as_hz() < half_span_hz {
            return Err(Error::InvalidInput(
                "The span is too large for the center frequency".to_string(),
            ));
        }

        let Some(stop_hz) = center.as_hz().checked_add(half_span_hz) else {
            return Err(Error::InvalidInput(
                "The center frequency and span exceed the maximum representable frequency"
                    .to_string(),
            ));
        };

        let start = Frequency::from_hz(center.as_hz() - half_span_hz);
        let stop = Frequency::from_hz(stop_hz);
        self.validate_start_stop(start, stop)?;
        Ok((start, stop))
    }

    #[tracing::instrument(skip(self), ret, err)]
    fn validate_start_stop(&self, start: Frequency, stop: Frequency) -> Result<()> {
        if start >= stop {
            return Err(Error::InvalidInput(
                "The start frequency must be less than the stop frequency".to_string(),
            ));
        }

        let active_model = self.active_radio_model();

        let min_max_freq = active_model.min_freq()..=active_model.max_freq();
        if !min_max_freq.contains(&start) {
            return Err(Error::InvalidInput(format!(
                "The start frequency {} MHz is not within the RF Explorer's frequency range of {}-{} MHz",
                start.as_mhz_f64(),
                min_max_freq.start().as_mhz_f64(),
                min_max_freq.end().as_mhz_f64()
            )));
        } else if !min_max_freq.contains(&stop) {
            return Err(Error::InvalidInput(format!(
                "The stop frequency {} MHz is not within the RF Explorer's frequency range of {}-{} MHz",
                stop.as_mhz(),
                min_max_freq.start().as_mhz_f64(),
                min_max_freq.end().as_mhz_f64()
            )));
        }

        let min_max_span = active_model.min_span()..=active_model.max_span();
        if !min_max_span.contains(&(stop - start)) {
            return Err(Error::InvalidInput(format!(
                "The span {} MHz is not within the RF Explorer's span range of {}-{} MHz",
                (stop - start).as_mhz_f64(),
                min_max_span.start().as_mhz_f64(),
                min_max_span.end().as_mhz_f64()
            )));
        }

        Ok(())
    }

    #[tracing::instrument(skip(self), ret, err)]
    fn validate_min_max_amps(&self, min_amp_dbm: i16, max_amp_dbm: i16) -> Result<()> {
        // The bottom amplitude must be less than the top amplitude
        if min_amp_dbm >= max_amp_dbm {
            error!("");
            return Err(Error::InvalidInput(
                "The minimum amplitude must be less than the maximum amplitude".to_string(),
            ));
        }

        // The top and bottom amplitude must be within the RF Explorer's min and max amplitude range
        if !Self::MIN_MAX_AMP_RANGE_DBM.contains(&min_amp_dbm) {
            return Err(Error::InvalidInput(format!(
                "The amplitude {} dBm is not within the RF Explorer's amplitude range of {}-{} dBm",
                min_amp_dbm,
                Self::MIN_MAX_AMP_RANGE_DBM.start(),
                Self::MIN_MAX_AMP_RANGE_DBM.end()
            )));
        } else if !Self::MIN_MAX_AMP_RANGE_DBM.contains(&max_amp_dbm) {
            return Err(Error::InvalidInput(format!(
                "The amplitude {} dBm is not within the RF Explorer's amplitude range of {}-{} dBm",
                max_amp_dbm,
                Self::MIN_MAX_AMP_RANGE_DBM.start(),
                Self::MIN_MAX_AMP_RANGE_DBM.end()
            )));
        }

        Ok(())
    }
}

#[derive(Default)]
struct MessageContainer {
    pub(crate) config: (Mutex<Option<Config>>, Condvar),
    pub(crate) config_callback: Mutex<ConfigCallback<Config>>,
    pub(crate) sweep: (Mutex<Option<Sweep>>, Condvar),
    pub(crate) sweep_callback: Mutex<Option<SweepCallback>>,
    pub(crate) screen_data: (Mutex<Option<ScreenData>>, Condvar),
    pub(crate) dsp_mode: (Mutex<Option<DspMode>>, Condvar),
    pub(crate) tracking_status: (Mutex<Option<TrackingStatus>>, Condvar),
    pub(crate) input_stage: (Mutex<Option<InputStage>>, Condvar),
    pub(crate) setup_info: (Mutex<Option<SetupInfo>>, Condvar),
    pub(crate) serial_number: (Mutex<Option<SerialNumber>>, Condvar),
}

type SweepCallback = Arc<Box<dyn Fn(&[f32], Frequency, Frequency) + Send + Sync + 'static>>;

impl crate::common::MessageContainer for MessageContainer {
    type Message = super::Message;

    fn cache_message(&self, message: Self::Message) {
        match message {
            Self::Message::Config(config) => {
                *self.config.0.lock().unwrap() = Some(config);
                self.config.1.notify_one();
                if let Some(cb) = self.config_callback.lock().unwrap().clone()
                    && let Some(config) = self.config.0.lock().unwrap().clone()
                {
                    // Run the user-provided callback on a new thread so that it can't
                    // block reading from the RF Explorer
                    thread::spawn(move || {
                        cb(config);
                    });
                }
            }
            Self::Message::Sweep(sweep) => {
                *self.sweep.0.lock().unwrap() = Some(sweep);
                self.sweep.1.notify_one();
                if let Some(cb) = self.sweep_callback.lock().unwrap().clone() {
                    let (start_freq, stop_freq) = {
                        let config = self.config.0.lock().unwrap();
                        (
                            config
                                .as_ref()
                                .map(|config| config.start_freq)
                                .unwrap_or_default(),
                            config
                                .as_ref()
                                .map(|config| config.stop_freq)
                                .unwrap_or_default(),
                        )
                    };
                    if let Some(sweep) = self.sweep.0.lock().unwrap().clone() {
                        // Run the user-provided callback on a new thread so that it can't
                        // block reading from the RF Explorer
                        thread::spawn(move || {
                            cb(sweep.amplitudes_dbm.as_slice(), start_freq, stop_freq);
                        });
                    }
                }
            }
            Self::Message::ScreenData(screen_data) => {
                *self.screen_data.0.lock().unwrap() = Some(screen_data);
                self.screen_data.1.notify_one();
            }
            Self::Message::DspMode(dsp_mode) => {
                *self.dsp_mode.0.lock().unwrap() = Some(dsp_mode);
                self.dsp_mode.1.notify_one();
            }
            Self::Message::InputStage(input_stage) => {
                *self.input_stage.0.lock().unwrap() = Some(input_stage);
                self.input_stage.1.notify_one();
            }
            Self::Message::TrackingStatus(tracking_status) => {
                *self.tracking_status.0.lock().unwrap() = Some(tracking_status);
                self.tracking_status.1.notify_one();
            }
            Self::Message::SerialNumber(serial_number) => {
                *self.serial_number.0.lock().unwrap() = Some(serial_number);
                self.serial_number.1.notify_one();
            }
            Self::Message::SetupInfo(setup_info) => {
                *self.setup_info.0.lock().unwrap() = Some(setup_info);
                self.setup_info.1.notify_one();
            }
        }
    }

    fn wait_for_device_info(&self) -> ConnectionResult<()> {
        let (config_lock, config_cvar) = &self.config;
        let (setup_info_lock, setup_info_cvar) = &self.setup_info;

        // Check to see if we've already received a Config and SetupInfo
        if config_lock.lock().unwrap().is_some() && setup_info_lock.lock().unwrap().is_some() {
            return Ok(());
        }

        // Wait to see if we receive a Config and SetupInfo before timing out
        if config_cvar
            .wait_timeout_while(
                config_lock.lock().unwrap(),
                RECEIVE_INITIAL_DEVICE_INFO_TIMEOUT,
                |config| config.is_none(),
            )
            .unwrap()
            .0
            .is_some()
            && setup_info_cvar
                .wait_timeout_while(
                    setup_info_lock.lock().unwrap(),
                    RECEIVE_INITIAL_DEVICE_INFO_TIMEOUT,
                    |setup_info| setup_info.is_none(),
                )
                .unwrap()
                .0
                .is_some()
        {
            Ok(())
        } else {
            Err(ConnectionError::DeviceInfoNotReceived)
        }
    }
}

impl Debug for MessageContainer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MessageContainer")
            .field("config", &self.config.0.lock().unwrap())
            .field("sweep", &self.sweep.0.lock().unwrap())
            .field("screen_data", &self.screen_data.0.lock().unwrap())
            .field("dsp_mode", &self.dsp_mode.0.lock().unwrap())
            .field("tracking_status", &self.tracking_status.0.lock().unwrap())
            .field("input_stage", &self.input_stage.0.lock().unwrap())
            .field("setup_info", &self.setup_info.0.lock().unwrap())
            .field("serial_number", &self.serial_number.0.lock().unwrap())
            .finish()
    }
}