Struct HackRfOne

Source
pub struct HackRfOne<MODE> { /* private fields */ }
Expand description

HackRF One software defined radio.

Implementations§

Source§

impl HackRfOne<UnknownMode>

Source

pub fn new() -> Option<HackRfOne<UnknownMode>>

Open a new HackRF One.

§Example
use hackrfone::{HackRfOne, UnknownMode};

let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().unwrap();
Examples found in repository?
examples/info.rs (line 4)
3fn main() {
4    let radio: HackRfOne<UnknownMode> = HackRfOne::new().expect("Failed to open HackRF One");
5    println!("Board ID: {:?}", radio.board_id());
6    println!("Version: {:?}", radio.version());
7    println!("Device version: {:?}", radio.device_version());
8}
More examples
Hide additional examples
examples/rx.rs (line 14)
13fn main() {
14    let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().expect("Failed to open HackRF One");
15
16    const FC: u64 = 915_000_000;
17    const FS: u32 = 10_000_000;
18    const DIV: u32 = 2;
19    radio
20        .set_sample_rate(FS * DIV, DIV)
21        .expect("Failed to set sample rate");
22    radio.set_freq(FC).expect("Failed to set frequency");
23    radio
24        .set_amp_enable(false)
25        .expect("Failed to disable amplifier");
26    radio
27        .set_antenna_enable(0)
28        .expect("Failed to disable antenna");
29    radio.set_lna_gain(20).expect("Failed to set LNA gain");
30    radio.set_vga_gain(32).expect("Failed to set VGA gain");
31    let mut radio: HackRfOne<RxMode> = radio.into_rx_mode().expect("Failed to enter RX mode");
32
33    let (data_tx, data_rx) = mpsc::channel();
34    let (exit_tx, exit_rx) = mpsc::channel();
35
36    let sample_thread = thread::Builder::new()
37        .name("sample".to_string())
38        .spawn(move || -> Result<(), hackrfone::Error> {
39            println!("Spawned sample thread");
40
41            loop {
42                let samples: Vec<u8> = radio.rx()?;
43                data_tx
44                    .send(samples)
45                    .expect("Failed to send buffer from sample thread");
46
47                match exit_rx.try_recv() {
48                    Ok(_) => {
49                        radio.stop_rx()?;
50                        return Ok(());
51                    }
52                    Err(TryRecvError::Disconnected) => {
53                        println!("Main thread disconnected");
54                        return Ok(());
55                    }
56                    Err(TryRecvError::Empty) => {}
57                }
58            }
59        })
60        .expect("Failed to spawn sample thread");
61
62    const NUM_SAMPLES: usize = 1024 * 1024;
63    let mut capture_buf: Vec<Complex32> = Vec::with_capacity(NUM_SAMPLES);
64
65    loop {
66        match data_rx.try_recv() {
67            Ok(buf) => buf.chunks_exact(2).for_each(|iq| {
68                capture_buf.push(iq_to_cplx_f32(iq[0], iq[1]));
69            }),
70            Err(TryRecvError::Disconnected) => {
71                println!("Sample thread disconnected");
72                break;
73            }
74            Err(TryRecvError::Empty) => {}
75        }
76
77        // ... do signal processing with capture buf in the loop
78
79        // ... or wait for the buffer to fill and do processing outside
80        if capture_buf.len() >= NUM_SAMPLES {
81            break;
82        }
83    }
84
85    println!("Shutting down sample thread");
86
87    if let Err(e) = exit_tx.send(()) {
88        println!("Failed to send exit event (receiver disconnected): {}", e);
89    }
90
91    sample_thread
92        .join()
93        .expect("Failed to join sample thread")
94        .expect("Sample thread returned an error");
95
96    println!("Done");
97}
Source§

impl<MODE> HackRfOne<MODE>

Source

pub fn device_version(&self) -> Version

Get the device version from the USB descriptor.

The HackRF C API calls the equivalent of this function hackrf_usb_api_version_read.

§Example
use hackrfone::{HackRfOne, UnknownMode, rusb};

let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().unwrap();
assert_eq!(radio.device_version(), rusb::Version(1, 0, 4));
Examples found in repository?
examples/info.rs (line 7)
3fn main() {
4    let radio: HackRfOne<UnknownMode> = HackRfOne::new().expect("Failed to open HackRF One");
5    println!("Board ID: {:?}", radio.board_id());
6    println!("Version: {:?}", radio.version());
7    println!("Device version: {:?}", radio.device_version());
8}
Source

pub fn set_timeout(&mut self, duration: Duration)

Set the timeout for USB transfers.

§Example

Set a 100ms timeout.

use hackrfone::{HackRfOne, UnknownMode};
use std::time::Duration;

let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().unwrap();
radio.set_timeout(Duration::from_millis(100))
Source

pub fn board_id(&self) -> Result<u8, Error>

Read the board ID.

§Example
use hackrfone::{HackRfOne, UnknownMode};

let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().unwrap();
assert_eq!(radio.board_id()?, 0x02);
Examples found in repository?
examples/info.rs (line 5)
3fn main() {
4    let radio: HackRfOne<UnknownMode> = HackRfOne::new().expect("Failed to open HackRF One");
5    println!("Board ID: {:?}", radio.board_id());
6    println!("Version: {:?}", radio.version());
7    println!("Device version: {:?}", radio.device_version());
8}
Source

pub fn version(&self) -> Result<String, Error>

Read the firmware version.

§Example
use hackrfone::{HackRfOne, UnknownMode};

let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().unwrap();
assert_eq!(radio.version()?, "2021.03.1");
Examples found in repository?
examples/info.rs (line 6)
3fn main() {
4    let radio: HackRfOne<UnknownMode> = HackRfOne::new().expect("Failed to open HackRF One");
5    println!("Board ID: {:?}", radio.board_id());
6    println!("Version: {:?}", radio.version());
7    println!("Device version: {:?}", radio.device_version());
8}
Source

pub fn set_freq(&mut self, hz: u64) -> Result<(), Error>

Set the center frequency.

§Example

Set the frequency to 915MHz.

use hackrfone::{HackRfOne, UnknownMode};

let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().unwrap();
radio.set_freq(915_000_000)?;
Examples found in repository?
examples/rx.rs (line 22)
13fn main() {
14    let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().expect("Failed to open HackRF One");
15
16    const FC: u64 = 915_000_000;
17    const FS: u32 = 10_000_000;
18    const DIV: u32 = 2;
19    radio
20        .set_sample_rate(FS * DIV, DIV)
21        .expect("Failed to set sample rate");
22    radio.set_freq(FC).expect("Failed to set frequency");
23    radio
24        .set_amp_enable(false)
25        .expect("Failed to disable amplifier");
26    radio
27        .set_antenna_enable(0)
28        .expect("Failed to disable antenna");
29    radio.set_lna_gain(20).expect("Failed to set LNA gain");
30    radio.set_vga_gain(32).expect("Failed to set VGA gain");
31    let mut radio: HackRfOne<RxMode> = radio.into_rx_mode().expect("Failed to enter RX mode");
32
33    let (data_tx, data_rx) = mpsc::channel();
34    let (exit_tx, exit_rx) = mpsc::channel();
35
36    let sample_thread = thread::Builder::new()
37        .name("sample".to_string())
38        .spawn(move || -> Result<(), hackrfone::Error> {
39            println!("Spawned sample thread");
40
41            loop {
42                let samples: Vec<u8> = radio.rx()?;
43                data_tx
44                    .send(samples)
45                    .expect("Failed to send buffer from sample thread");
46
47                match exit_rx.try_recv() {
48                    Ok(_) => {
49                        radio.stop_rx()?;
50                        return Ok(());
51                    }
52                    Err(TryRecvError::Disconnected) => {
53                        println!("Main thread disconnected");
54                        return Ok(());
55                    }
56                    Err(TryRecvError::Empty) => {}
57                }
58            }
59        })
60        .expect("Failed to spawn sample thread");
61
62    const NUM_SAMPLES: usize = 1024 * 1024;
63    let mut capture_buf: Vec<Complex32> = Vec::with_capacity(NUM_SAMPLES);
64
65    loop {
66        match data_rx.try_recv() {
67            Ok(buf) => buf.chunks_exact(2).for_each(|iq| {
68                capture_buf.push(iq_to_cplx_f32(iq[0], iq[1]));
69            }),
70            Err(TryRecvError::Disconnected) => {
71                println!("Sample thread disconnected");
72                break;
73            }
74            Err(TryRecvError::Empty) => {}
75        }
76
77        // ... do signal processing with capture buf in the loop
78
79        // ... or wait for the buffer to fill and do processing outside
80        if capture_buf.len() >= NUM_SAMPLES {
81            break;
82        }
83    }
84
85    println!("Shutting down sample thread");
86
87    if let Err(e) = exit_tx.send(()) {
88        println!("Failed to send exit event (receiver disconnected): {}", e);
89    }
90
91    sample_thread
92        .join()
93        .expect("Failed to join sample thread")
94        .expect("Sample thread returned an error");
95
96    println!("Done");
97}
Source

pub fn set_amp_enable(&mut self, en: bool) -> Result<(), Error>

Enable the RX/TX RF amplifier.

In GNU radio this is used as the RF gain, where a value of 0 dB is off, and a value of 14 dB is on.

§Example

Disable the amplifier.

use hackrfone::{HackRfOne, UnknownMode};

let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().unwrap();
radio.set_amp_enable(false)?;
Examples found in repository?
examples/rx.rs (line 24)
13fn main() {
14    let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().expect("Failed to open HackRF One");
15
16    const FC: u64 = 915_000_000;
17    const FS: u32 = 10_000_000;
18    const DIV: u32 = 2;
19    radio
20        .set_sample_rate(FS * DIV, DIV)
21        .expect("Failed to set sample rate");
22    radio.set_freq(FC).expect("Failed to set frequency");
23    radio
24        .set_amp_enable(false)
25        .expect("Failed to disable amplifier");
26    radio
27        .set_antenna_enable(0)
28        .expect("Failed to disable antenna");
29    radio.set_lna_gain(20).expect("Failed to set LNA gain");
30    radio.set_vga_gain(32).expect("Failed to set VGA gain");
31    let mut radio: HackRfOne<RxMode> = radio.into_rx_mode().expect("Failed to enter RX mode");
32
33    let (data_tx, data_rx) = mpsc::channel();
34    let (exit_tx, exit_rx) = mpsc::channel();
35
36    let sample_thread = thread::Builder::new()
37        .name("sample".to_string())
38        .spawn(move || -> Result<(), hackrfone::Error> {
39            println!("Spawned sample thread");
40
41            loop {
42                let samples: Vec<u8> = radio.rx()?;
43                data_tx
44                    .send(samples)
45                    .expect("Failed to send buffer from sample thread");
46
47                match exit_rx.try_recv() {
48                    Ok(_) => {
49                        radio.stop_rx()?;
50                        return Ok(());
51                    }
52                    Err(TryRecvError::Disconnected) => {
53                        println!("Main thread disconnected");
54                        return Ok(());
55                    }
56                    Err(TryRecvError::Empty) => {}
57                }
58            }
59        })
60        .expect("Failed to spawn sample thread");
61
62    const NUM_SAMPLES: usize = 1024 * 1024;
63    let mut capture_buf: Vec<Complex32> = Vec::with_capacity(NUM_SAMPLES);
64
65    loop {
66        match data_rx.try_recv() {
67            Ok(buf) => buf.chunks_exact(2).for_each(|iq| {
68                capture_buf.push(iq_to_cplx_f32(iq[0], iq[1]));
69            }),
70            Err(TryRecvError::Disconnected) => {
71                println!("Sample thread disconnected");
72                break;
73            }
74            Err(TryRecvError::Empty) => {}
75        }
76
77        // ... do signal processing with capture buf in the loop
78
79        // ... or wait for the buffer to fill and do processing outside
80        if capture_buf.len() >= NUM_SAMPLES {
81            break;
82        }
83    }
84
85    println!("Shutting down sample thread");
86
87    if let Err(e) = exit_tx.send(()) {
88        println!("Failed to send exit event (receiver disconnected): {}", e);
89    }
90
91    sample_thread
92        .join()
93        .expect("Failed to join sample thread")
94        .expect("Sample thread returned an error");
95
96    println!("Done");
97}
Source

pub fn set_baseband_filter_bandwidth(&mut self, hz: u32) -> Result<(), Error>

Set the baseband filter bandwidth.

This is automatically set when the sample rate is changed with set_sample_rate.

§Example

Set the filter bandwidth to 70% of the sample rate.

use hackrfone::{HackRfOne, UnknownMode};

const SAMPLE_HZ: u32 = 20_000_000;
const SAMPLE_DIV: u32 = 2;
const FILTER_BW: u32 = (0.7 * (SAMPLE_HZ as f32) / (SAMPLE_DIV as f32)) as u32;

let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().unwrap();
radio.set_sample_rate(SAMPLE_HZ, SAMPLE_DIV)?;
radio.set_baseband_filter_bandwidth(FILTER_BW)?;
Source

pub fn set_sample_rate(&mut self, hz: u32, div: u32) -> Result<(), Error>

Set the sample rate.

For anti-aliasing, the baseband filter bandwidth is automatically set to the widest available setting that is no more than 75% of the sample rate. This happens every time the sample rate is set. If you want to override the baseband filter selection, you must do so after setting the sample rate.

Limits are 8MHz - 20MHz. Preferred rates are 8, 10, 12.5, 16, 20MHz due to less jitter.

§Example

Set the sample rate to 10 MHz.

use hackrfone::{HackRfOne, UnknownMode};

let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().unwrap();
radio.set_sample_rate(20_000_000, 2)?;
Examples found in repository?
examples/rx.rs (line 20)
13fn main() {
14    let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().expect("Failed to open HackRF One");
15
16    const FC: u64 = 915_000_000;
17    const FS: u32 = 10_000_000;
18    const DIV: u32 = 2;
19    radio
20        .set_sample_rate(FS * DIV, DIV)
21        .expect("Failed to set sample rate");
22    radio.set_freq(FC).expect("Failed to set frequency");
23    radio
24        .set_amp_enable(false)
25        .expect("Failed to disable amplifier");
26    radio
27        .set_antenna_enable(0)
28        .expect("Failed to disable antenna");
29    radio.set_lna_gain(20).expect("Failed to set LNA gain");
30    radio.set_vga_gain(32).expect("Failed to set VGA gain");
31    let mut radio: HackRfOne<RxMode> = radio.into_rx_mode().expect("Failed to enter RX mode");
32
33    let (data_tx, data_rx) = mpsc::channel();
34    let (exit_tx, exit_rx) = mpsc::channel();
35
36    let sample_thread = thread::Builder::new()
37        .name("sample".to_string())
38        .spawn(move || -> Result<(), hackrfone::Error> {
39            println!("Spawned sample thread");
40
41            loop {
42                let samples: Vec<u8> = radio.rx()?;
43                data_tx
44                    .send(samples)
45                    .expect("Failed to send buffer from sample thread");
46
47                match exit_rx.try_recv() {
48                    Ok(_) => {
49                        radio.stop_rx()?;
50                        return Ok(());
51                    }
52                    Err(TryRecvError::Disconnected) => {
53                        println!("Main thread disconnected");
54                        return Ok(());
55                    }
56                    Err(TryRecvError::Empty) => {}
57                }
58            }
59        })
60        .expect("Failed to spawn sample thread");
61
62    const NUM_SAMPLES: usize = 1024 * 1024;
63    let mut capture_buf: Vec<Complex32> = Vec::with_capacity(NUM_SAMPLES);
64
65    loop {
66        match data_rx.try_recv() {
67            Ok(buf) => buf.chunks_exact(2).for_each(|iq| {
68                capture_buf.push(iq_to_cplx_f32(iq[0], iq[1]));
69            }),
70            Err(TryRecvError::Disconnected) => {
71                println!("Sample thread disconnected");
72                break;
73            }
74            Err(TryRecvError::Empty) => {}
75        }
76
77        // ... do signal processing with capture buf in the loop
78
79        // ... or wait for the buffer to fill and do processing outside
80        if capture_buf.len() >= NUM_SAMPLES {
81            break;
82        }
83    }
84
85    println!("Shutting down sample thread");
86
87    if let Err(e) = exit_tx.send(()) {
88        println!("Failed to send exit event (receiver disconnected): {}", e);
89    }
90
91    sample_thread
92        .join()
93        .expect("Failed to join sample thread")
94        .expect("Sample thread returned an error");
95
96    println!("Done");
97}
Source

pub fn set_lna_gain(&mut self, gain: u16) -> Result<(), Error>

Set the LNA (low noise amplifier) gain.

Range 0 to 40dB in 8dB steps.

This is also known as the IF gain.

§Example

Set the LNA gain to 16 dB (generally a reasonable gain to start with).

use hackrfone::{HackRfOne, UnknownMode};

let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().unwrap();
radio.set_lna_gain(16)?;
Examples found in repository?
examples/rx.rs (line 29)
13fn main() {
14    let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().expect("Failed to open HackRF One");
15
16    const FC: u64 = 915_000_000;
17    const FS: u32 = 10_000_000;
18    const DIV: u32 = 2;
19    radio
20        .set_sample_rate(FS * DIV, DIV)
21        .expect("Failed to set sample rate");
22    radio.set_freq(FC).expect("Failed to set frequency");
23    radio
24        .set_amp_enable(false)
25        .expect("Failed to disable amplifier");
26    radio
27        .set_antenna_enable(0)
28        .expect("Failed to disable antenna");
29    radio.set_lna_gain(20).expect("Failed to set LNA gain");
30    radio.set_vga_gain(32).expect("Failed to set VGA gain");
31    let mut radio: HackRfOne<RxMode> = radio.into_rx_mode().expect("Failed to enter RX mode");
32
33    let (data_tx, data_rx) = mpsc::channel();
34    let (exit_tx, exit_rx) = mpsc::channel();
35
36    let sample_thread = thread::Builder::new()
37        .name("sample".to_string())
38        .spawn(move || -> Result<(), hackrfone::Error> {
39            println!("Spawned sample thread");
40
41            loop {
42                let samples: Vec<u8> = radio.rx()?;
43                data_tx
44                    .send(samples)
45                    .expect("Failed to send buffer from sample thread");
46
47                match exit_rx.try_recv() {
48                    Ok(_) => {
49                        radio.stop_rx()?;
50                        return Ok(());
51                    }
52                    Err(TryRecvError::Disconnected) => {
53                        println!("Main thread disconnected");
54                        return Ok(());
55                    }
56                    Err(TryRecvError::Empty) => {}
57                }
58            }
59        })
60        .expect("Failed to spawn sample thread");
61
62    const NUM_SAMPLES: usize = 1024 * 1024;
63    let mut capture_buf: Vec<Complex32> = Vec::with_capacity(NUM_SAMPLES);
64
65    loop {
66        match data_rx.try_recv() {
67            Ok(buf) => buf.chunks_exact(2).for_each(|iq| {
68                capture_buf.push(iq_to_cplx_f32(iq[0], iq[1]));
69            }),
70            Err(TryRecvError::Disconnected) => {
71                println!("Sample thread disconnected");
72                break;
73            }
74            Err(TryRecvError::Empty) => {}
75        }
76
77        // ... do signal processing with capture buf in the loop
78
79        // ... or wait for the buffer to fill and do processing outside
80        if capture_buf.len() >= NUM_SAMPLES {
81            break;
82        }
83    }
84
85    println!("Shutting down sample thread");
86
87    if let Err(e) = exit_tx.send(()) {
88        println!("Failed to send exit event (receiver disconnected): {}", e);
89    }
90
91    sample_thread
92        .join()
93        .expect("Failed to join sample thread")
94        .expect("Sample thread returned an error");
95
96    println!("Done");
97}
Source

pub fn set_vga_gain(&mut self, gain: u16) -> Result<(), Error>

Set the VGA (variable gain amplifier) gain.

Range 0 to 62dB in 2dB steps.

This is also known as the baseband (BB) gain.

§Example

Set the VGA gain to 16 dB (generally a reasonable gain to start with).

use hackrfone::{HackRfOne, UnknownMode};

let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().unwrap();
radio.set_vga_gain(16)?;
Examples found in repository?
examples/rx.rs (line 30)
13fn main() {
14    let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().expect("Failed to open HackRF One");
15
16    const FC: u64 = 915_000_000;
17    const FS: u32 = 10_000_000;
18    const DIV: u32 = 2;
19    radio
20        .set_sample_rate(FS * DIV, DIV)
21        .expect("Failed to set sample rate");
22    radio.set_freq(FC).expect("Failed to set frequency");
23    radio
24        .set_amp_enable(false)
25        .expect("Failed to disable amplifier");
26    radio
27        .set_antenna_enable(0)
28        .expect("Failed to disable antenna");
29    radio.set_lna_gain(20).expect("Failed to set LNA gain");
30    radio.set_vga_gain(32).expect("Failed to set VGA gain");
31    let mut radio: HackRfOne<RxMode> = radio.into_rx_mode().expect("Failed to enter RX mode");
32
33    let (data_tx, data_rx) = mpsc::channel();
34    let (exit_tx, exit_rx) = mpsc::channel();
35
36    let sample_thread = thread::Builder::new()
37        .name("sample".to_string())
38        .spawn(move || -> Result<(), hackrfone::Error> {
39            println!("Spawned sample thread");
40
41            loop {
42                let samples: Vec<u8> = radio.rx()?;
43                data_tx
44                    .send(samples)
45                    .expect("Failed to send buffer from sample thread");
46
47                match exit_rx.try_recv() {
48                    Ok(_) => {
49                        radio.stop_rx()?;
50                        return Ok(());
51                    }
52                    Err(TryRecvError::Disconnected) => {
53                        println!("Main thread disconnected");
54                        return Ok(());
55                    }
56                    Err(TryRecvError::Empty) => {}
57                }
58            }
59        })
60        .expect("Failed to spawn sample thread");
61
62    const NUM_SAMPLES: usize = 1024 * 1024;
63    let mut capture_buf: Vec<Complex32> = Vec::with_capacity(NUM_SAMPLES);
64
65    loop {
66        match data_rx.try_recv() {
67            Ok(buf) => buf.chunks_exact(2).for_each(|iq| {
68                capture_buf.push(iq_to_cplx_f32(iq[0], iq[1]));
69            }),
70            Err(TryRecvError::Disconnected) => {
71                println!("Sample thread disconnected");
72                break;
73            }
74            Err(TryRecvError::Empty) => {}
75        }
76
77        // ... do signal processing with capture buf in the loop
78
79        // ... or wait for the buffer to fill and do processing outside
80        if capture_buf.len() >= NUM_SAMPLES {
81            break;
82        }
83    }
84
85    println!("Shutting down sample thread");
86
87    if let Err(e) = exit_tx.send(()) {
88        println!("Failed to send exit event (receiver disconnected): {}", e);
89    }
90
91    sample_thread
92        .join()
93        .expect("Failed to join sample thread")
94        .expect("Sample thread returned an error");
95
96    println!("Done");
97}
Source

pub fn set_txvga_gain(&mut self, gain: u16) -> Result<(), Error>

Set the transmit VGA gain.

Range 0 to 47dB in 1db steps.

Source

pub fn set_antenna_enable(&mut self, value: u8) -> Result<(), Error>

Antenna power port control.

The source docs are a little lacking in terms of explanations here.

Examples found in repository?
examples/rx.rs (line 27)
13fn main() {
14    let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().expect("Failed to open HackRF One");
15
16    const FC: u64 = 915_000_000;
17    const FS: u32 = 10_000_000;
18    const DIV: u32 = 2;
19    radio
20        .set_sample_rate(FS * DIV, DIV)
21        .expect("Failed to set sample rate");
22    radio.set_freq(FC).expect("Failed to set frequency");
23    radio
24        .set_amp_enable(false)
25        .expect("Failed to disable amplifier");
26    radio
27        .set_antenna_enable(0)
28        .expect("Failed to disable antenna");
29    radio.set_lna_gain(20).expect("Failed to set LNA gain");
30    radio.set_vga_gain(32).expect("Failed to set VGA gain");
31    let mut radio: HackRfOne<RxMode> = radio.into_rx_mode().expect("Failed to enter RX mode");
32
33    let (data_tx, data_rx) = mpsc::channel();
34    let (exit_tx, exit_rx) = mpsc::channel();
35
36    let sample_thread = thread::Builder::new()
37        .name("sample".to_string())
38        .spawn(move || -> Result<(), hackrfone::Error> {
39            println!("Spawned sample thread");
40
41            loop {
42                let samples: Vec<u8> = radio.rx()?;
43                data_tx
44                    .send(samples)
45                    .expect("Failed to send buffer from sample thread");
46
47                match exit_rx.try_recv() {
48                    Ok(_) => {
49                        radio.stop_rx()?;
50                        return Ok(());
51                    }
52                    Err(TryRecvError::Disconnected) => {
53                        println!("Main thread disconnected");
54                        return Ok(());
55                    }
56                    Err(TryRecvError::Empty) => {}
57                }
58            }
59        })
60        .expect("Failed to spawn sample thread");
61
62    const NUM_SAMPLES: usize = 1024 * 1024;
63    let mut capture_buf: Vec<Complex32> = Vec::with_capacity(NUM_SAMPLES);
64
65    loop {
66        match data_rx.try_recv() {
67            Ok(buf) => buf.chunks_exact(2).for_each(|iq| {
68                capture_buf.push(iq_to_cplx_f32(iq[0], iq[1]));
69            }),
70            Err(TryRecvError::Disconnected) => {
71                println!("Sample thread disconnected");
72                break;
73            }
74            Err(TryRecvError::Empty) => {}
75        }
76
77        // ... do signal processing with capture buf in the loop
78
79        // ... or wait for the buffer to fill and do processing outside
80        if capture_buf.len() >= NUM_SAMPLES {
81            break;
82        }
83    }
84
85    println!("Shutting down sample thread");
86
87    if let Err(e) = exit_tx.send(()) {
88        println!("Failed to send exit event (receiver disconnected): {}", e);
89    }
90
91    sample_thread
92        .join()
93        .expect("Failed to join sample thread")
94        .expect("Sample thread returned an error");
95
96    println!("Done");
97}
Source

pub fn set_clkout_enable(&mut self, en: bool) -> Result<(), Error>

CLKOUT enable.

The source docs are a little lacking in terms of explanations here.

Source

pub fn reset(self) -> Result<HackRfOne<UnknownMode>, Error>

Reset the HackRF radio.

§Example
use hackrfone::{HackRfOne, UnknownMode};

let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().unwrap();
let mut radio: HackRfOne<UnknownMode> = radio.reset()?;
Source

pub fn into_rx_mode(self) -> Result<HackRfOne<RxMode>, Error>

Change the radio mode to RX.

§Example
use hackrfone::{HackRfOne, RxMode, UnknownMode};

let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().unwrap();
let mut radio: HackRfOne<RxMode> = radio.into_rx_mode()?;
Examples found in repository?
examples/rx.rs (line 31)
13fn main() {
14    let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().expect("Failed to open HackRF One");
15
16    const FC: u64 = 915_000_000;
17    const FS: u32 = 10_000_000;
18    const DIV: u32 = 2;
19    radio
20        .set_sample_rate(FS * DIV, DIV)
21        .expect("Failed to set sample rate");
22    radio.set_freq(FC).expect("Failed to set frequency");
23    radio
24        .set_amp_enable(false)
25        .expect("Failed to disable amplifier");
26    radio
27        .set_antenna_enable(0)
28        .expect("Failed to disable antenna");
29    radio.set_lna_gain(20).expect("Failed to set LNA gain");
30    radio.set_vga_gain(32).expect("Failed to set VGA gain");
31    let mut radio: HackRfOne<RxMode> = radio.into_rx_mode().expect("Failed to enter RX mode");
32
33    let (data_tx, data_rx) = mpsc::channel();
34    let (exit_tx, exit_rx) = mpsc::channel();
35
36    let sample_thread = thread::Builder::new()
37        .name("sample".to_string())
38        .spawn(move || -> Result<(), hackrfone::Error> {
39            println!("Spawned sample thread");
40
41            loop {
42                let samples: Vec<u8> = radio.rx()?;
43                data_tx
44                    .send(samples)
45                    .expect("Failed to send buffer from sample thread");
46
47                match exit_rx.try_recv() {
48                    Ok(_) => {
49                        radio.stop_rx()?;
50                        return Ok(());
51                    }
52                    Err(TryRecvError::Disconnected) => {
53                        println!("Main thread disconnected");
54                        return Ok(());
55                    }
56                    Err(TryRecvError::Empty) => {}
57                }
58            }
59        })
60        .expect("Failed to spawn sample thread");
61
62    const NUM_SAMPLES: usize = 1024 * 1024;
63    let mut capture_buf: Vec<Complex32> = Vec::with_capacity(NUM_SAMPLES);
64
65    loop {
66        match data_rx.try_recv() {
67            Ok(buf) => buf.chunks_exact(2).for_each(|iq| {
68                capture_buf.push(iq_to_cplx_f32(iq[0], iq[1]));
69            }),
70            Err(TryRecvError::Disconnected) => {
71                println!("Sample thread disconnected");
72                break;
73            }
74            Err(TryRecvError::Empty) => {}
75        }
76
77        // ... do signal processing with capture buf in the loop
78
79        // ... or wait for the buffer to fill and do processing outside
80        if capture_buf.len() >= NUM_SAMPLES {
81            break;
82        }
83    }
84
85    println!("Shutting down sample thread");
86
87    if let Err(e) = exit_tx.send(()) {
88        println!("Failed to send exit event (receiver disconnected): {}", e);
89    }
90
91    sample_thread
92        .join()
93        .expect("Failed to join sample thread")
94        .expect("Sample thread returned an error");
95
96    println!("Done");
97}
Source§

impl HackRfOne<RxMode>

Source

pub fn rx(&mut self) -> Result<Vec<u8>, Error>

Receive data from the radio.

This uses a bulk transfer to get one MTU (maximum transmission unit) of data in a single shot. The data format is pairs of signed 8-bit IQ. Use the iq_to_cplx_i8 or iq_to_cplx_f32 helpers to convert the data to a more manageable format.

Unlike libhackrf this does not spawn a sampling thread.

§Example
use hackrfone::{HackRfOne, RxMode, UnknownMode};

let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().unwrap();
let mut radio: HackRfOne<RxMode> = radio.into_rx_mode()?;
let data: Vec<u8> = radio.rx()?;
radio.stop_rx()?;
Examples found in repository?
examples/rx.rs (line 42)
13fn main() {
14    let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().expect("Failed to open HackRF One");
15
16    const FC: u64 = 915_000_000;
17    const FS: u32 = 10_000_000;
18    const DIV: u32 = 2;
19    radio
20        .set_sample_rate(FS * DIV, DIV)
21        .expect("Failed to set sample rate");
22    radio.set_freq(FC).expect("Failed to set frequency");
23    radio
24        .set_amp_enable(false)
25        .expect("Failed to disable amplifier");
26    radio
27        .set_antenna_enable(0)
28        .expect("Failed to disable antenna");
29    radio.set_lna_gain(20).expect("Failed to set LNA gain");
30    radio.set_vga_gain(32).expect("Failed to set VGA gain");
31    let mut radio: HackRfOne<RxMode> = radio.into_rx_mode().expect("Failed to enter RX mode");
32
33    let (data_tx, data_rx) = mpsc::channel();
34    let (exit_tx, exit_rx) = mpsc::channel();
35
36    let sample_thread = thread::Builder::new()
37        .name("sample".to_string())
38        .spawn(move || -> Result<(), hackrfone::Error> {
39            println!("Spawned sample thread");
40
41            loop {
42                let samples: Vec<u8> = radio.rx()?;
43                data_tx
44                    .send(samples)
45                    .expect("Failed to send buffer from sample thread");
46
47                match exit_rx.try_recv() {
48                    Ok(_) => {
49                        radio.stop_rx()?;
50                        return Ok(());
51                    }
52                    Err(TryRecvError::Disconnected) => {
53                        println!("Main thread disconnected");
54                        return Ok(());
55                    }
56                    Err(TryRecvError::Empty) => {}
57                }
58            }
59        })
60        .expect("Failed to spawn sample thread");
61
62    const NUM_SAMPLES: usize = 1024 * 1024;
63    let mut capture_buf: Vec<Complex32> = Vec::with_capacity(NUM_SAMPLES);
64
65    loop {
66        match data_rx.try_recv() {
67            Ok(buf) => buf.chunks_exact(2).for_each(|iq| {
68                capture_buf.push(iq_to_cplx_f32(iq[0], iq[1]));
69            }),
70            Err(TryRecvError::Disconnected) => {
71                println!("Sample thread disconnected");
72                break;
73            }
74            Err(TryRecvError::Empty) => {}
75        }
76
77        // ... do signal processing with capture buf in the loop
78
79        // ... or wait for the buffer to fill and do processing outside
80        if capture_buf.len() >= NUM_SAMPLES {
81            break;
82        }
83    }
84
85    println!("Shutting down sample thread");
86
87    if let Err(e) = exit_tx.send(()) {
88        println!("Failed to send exit event (receiver disconnected): {}", e);
89    }
90
91    sample_thread
92        .join()
93        .expect("Failed to join sample thread")
94        .expect("Sample thread returned an error");
95
96    println!("Done");
97}
Source

pub fn stop_rx(self) -> Result<HackRfOne<UnknownMode>, Error>

Stop receiving.

§Example
use hackrfone::{HackRfOne, RxMode, UnknownMode};

let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().unwrap();
let mut radio: HackRfOne<RxMode> = radio.into_rx_mode()?;
let data: Vec<u8> = radio.rx()?;
radio.stop_rx()?;
Examples found in repository?
examples/rx.rs (line 49)
13fn main() {
14    let mut radio: HackRfOne<UnknownMode> = HackRfOne::new().expect("Failed to open HackRF One");
15
16    const FC: u64 = 915_000_000;
17    const FS: u32 = 10_000_000;
18    const DIV: u32 = 2;
19    radio
20        .set_sample_rate(FS * DIV, DIV)
21        .expect("Failed to set sample rate");
22    radio.set_freq(FC).expect("Failed to set frequency");
23    radio
24        .set_amp_enable(false)
25        .expect("Failed to disable amplifier");
26    radio
27        .set_antenna_enable(0)
28        .expect("Failed to disable antenna");
29    radio.set_lna_gain(20).expect("Failed to set LNA gain");
30    radio.set_vga_gain(32).expect("Failed to set VGA gain");
31    let mut radio: HackRfOne<RxMode> = radio.into_rx_mode().expect("Failed to enter RX mode");
32
33    let (data_tx, data_rx) = mpsc::channel();
34    let (exit_tx, exit_rx) = mpsc::channel();
35
36    let sample_thread = thread::Builder::new()
37        .name("sample".to_string())
38        .spawn(move || -> Result<(), hackrfone::Error> {
39            println!("Spawned sample thread");
40
41            loop {
42                let samples: Vec<u8> = radio.rx()?;
43                data_tx
44                    .send(samples)
45                    .expect("Failed to send buffer from sample thread");
46
47                match exit_rx.try_recv() {
48                    Ok(_) => {
49                        radio.stop_rx()?;
50                        return Ok(());
51                    }
52                    Err(TryRecvError::Disconnected) => {
53                        println!("Main thread disconnected");
54                        return Ok(());
55                    }
56                    Err(TryRecvError::Empty) => {}
57                }
58            }
59        })
60        .expect("Failed to spawn sample thread");
61
62    const NUM_SAMPLES: usize = 1024 * 1024;
63    let mut capture_buf: Vec<Complex32> = Vec::with_capacity(NUM_SAMPLES);
64
65    loop {
66        match data_rx.try_recv() {
67            Ok(buf) => buf.chunks_exact(2).for_each(|iq| {
68                capture_buf.push(iq_to_cplx_f32(iq[0], iq[1]));
69            }),
70            Err(TryRecvError::Disconnected) => {
71                println!("Sample thread disconnected");
72                break;
73            }
74            Err(TryRecvError::Empty) => {}
75        }
76
77        // ... do signal processing with capture buf in the loop
78
79        // ... or wait for the buffer to fill and do processing outside
80        if capture_buf.len() >= NUM_SAMPLES {
81            break;
82        }
83    }
84
85    println!("Shutting down sample thread");
86
87    if let Err(e) = exit_tx.send(()) {
88        println!("Failed to send exit event (receiver disconnected): {}", e);
89    }
90
91    sample_thread
92        .join()
93        .expect("Failed to join sample thread")
94        .expect("Sample thread returned an error");
95
96    println!("Done");
97}

Auto Trait Implementations§

§

impl<MODE> !Freeze for HackRfOne<MODE>

§

impl<MODE> RefUnwindSafe for HackRfOne<MODE>
where MODE: RefUnwindSafe,

§

impl<MODE> Send for HackRfOne<MODE>
where MODE: Send,

§

impl<MODE> Sync for HackRfOne<MODE>
where MODE: Sync,

§

impl<MODE> Unpin for HackRfOne<MODE>
where MODE: Unpin,

§

impl<MODE> UnwindSafe for HackRfOne<MODE>
where MODE: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.