|
| 1 | +#include "hackrf_source.h" |
| 2 | + |
| 3 | +#include <pthread.h> |
| 4 | + |
| 5 | +#include <cstring> |
| 6 | +#include <iostream> |
| 7 | + |
| 8 | +#include <util/error.h> |
| 9 | + |
| 10 | +std::unique_ptr<HackRF> HackRF::open(uint32_t index) { |
| 11 | + struct hackrf_device* dev = nullptr; |
| 12 | + auto rv_init = hackrf_init(); |
| 13 | + if (rv_init < 0) { |
| 14 | + std::cerr << "Unable to init hackrf" << std::endl; |
| 15 | + exit(1); |
| 16 | + } |
| 17 | + |
| 18 | + auto rv = hackrf_open(&dev); |
| 19 | + |
| 20 | + if (rv < 0) { |
| 21 | + std::cerr << "Unable to open HackRF device: " |
| 22 | + << hackrf_error_name((enum hackrf_error)rv) << std::endl; |
| 23 | + exit(1); |
| 24 | + } |
| 25 | + |
| 26 | + return std::make_unique<HackRF>(dev); |
| 27 | +} |
| 28 | + |
| 29 | +HackRF::HackRF(struct hackrf_device* dev) : dev_(dev) { |
| 30 | + // Load list of supported sample rates |
| 31 | + sampleRates_ = loadSampleRates(); |
| 32 | +} |
| 33 | + |
| 34 | +HackRF::~HackRF() { |
| 35 | + if (dev_ != nullptr) { |
| 36 | + hackrf_close(dev_); |
| 37 | + hackrf_exit(); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +std::vector<uint32_t> HackRF::loadSampleRates() { |
| 42 | + return {8000000, 10000000, 12500000, 16000000, 20000000}; |
| 43 | +} |
| 44 | + |
| 45 | +void HackRF::setFrequency(uint32_t freq) { |
| 46 | + ASSERT(dev_ != nullptr); |
| 47 | + auto rv = hackrf_set_freq(dev_, freq); |
| 48 | + ASSERT(rv >= 0); |
| 49 | +} |
| 50 | + |
| 51 | +void HackRF::setSampleRate(uint32_t rate) { |
| 52 | + ASSERT(dev_ != nullptr); |
| 53 | + auto rv = hackrf_set_sample_rate(dev_, rate); |
| 54 | + ASSERT(rv >= 0); |
| 55 | + sampleRate_ = rate; |
| 56 | +} |
| 57 | + |
| 58 | +uint32_t HackRF::getSampleRate() const { |
| 59 | + return sampleRate_; |
| 60 | +} |
| 61 | + |
| 62 | +void HackRF::setIfGain(int gain) { |
| 63 | + ASSERT(dev_ != nullptr); |
| 64 | + auto rv = hackrf_set_lna_gain(dev_, gain); |
| 65 | + ASSERT(rv >= 0); |
| 66 | +} |
| 67 | + |
| 68 | +void HackRF::setRfAmplifier(bool on) { |
| 69 | + ASSERT(dev_ != nullptr); |
| 70 | + auto rv = hackrf_set_amp_enable(dev_, on); |
| 71 | + ASSERT(rv >= 0); |
| 72 | +} |
| 73 | + |
| 74 | +void HackRF::setBbGain(int gain) { |
| 75 | + ASSERT(dev_ != nullptr); |
| 76 | + auto rv = hackrf_set_vga_gain(dev_, gain); |
| 77 | + ASSERT(rv >= 0); |
| 78 | +} |
| 79 | + |
| 80 | +void HackRF::setBiasTee(bool on) { |
| 81 | + ASSERT(dev_ != nullptr); |
| 82 | + auto rv = hackrf_set_antenna_enable(dev_, on ? 1 : 0); |
| 83 | + ASSERT(rv >= 0); |
| 84 | +} |
| 85 | + |
| 86 | +static int hackrf_callback(hackrf_transfer* transfer) { |
| 87 | + auto hackrf_context = reinterpret_cast<HackRF*>(transfer->rx_ctx); |
| 88 | + hackrf_context->handle(transfer); |
| 89 | + return 0; |
| 90 | +} |
| 91 | + |
| 92 | +void HackRF::start(const std::shared_ptr<Queue<Samples> >& queue) { |
| 93 | + ASSERT(dev_ != nullptr); |
| 94 | + queue_ = queue; |
| 95 | + thread_ = std::thread([&] { |
| 96 | + auto rv = hackrf_start_rx(dev_, &hackrf_callback, this); |
| 97 | + ASSERT(rv == 0); |
| 98 | + }); |
| 99 | +#ifdef __APPLE__ |
| 100 | + pthread_setname_np("hackrf"); |
| 101 | +#else |
| 102 | + pthread_setname_np(thread_.native_handle(), "hackrf"); |
| 103 | +#endif |
| 104 | +} |
| 105 | + |
| 106 | +void HackRF::stop() { |
| 107 | + ASSERT(dev_ != nullptr); |
| 108 | + auto rv = hackrf_stop_rx(dev_); |
| 109 | + ASSERT(rv >= 0); |
| 110 | + |
| 111 | + // Wait for thread to terminate |
| 112 | + thread_.join(); |
| 113 | + |
| 114 | + // Close queue to signal downstream |
| 115 | + queue_->close(); |
| 116 | + |
| 117 | + // Clear reference to queue |
| 118 | + queue_.reset(); |
| 119 | +} |
| 120 | + |
| 121 | +void HackRF::process(size_t nsamples, unsigned char* buf, std::complex<float>* fo) { |
| 122 | + for (uint32_t i = 0; i < nsamples; i++) { |
| 123 | + fo[i].real((static_cast<int8_t>(buf[i * 2 + 0]) / 128.0f)); |
| 124 | + fo[i].imag((static_cast<int8_t>(buf[i * 2 + 1]) / 128.0f)); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +void HackRF::handle(const hackrf_transfer* transfer) { |
| 129 | + uint32_t nsamples = transfer->valid_length / 2; |
| 130 | + |
| 131 | + // Expect multiple of 2 |
| 132 | + ASSERT((nsamples & 0x2) == 0); |
| 133 | + |
| 134 | + // Grab buffer from queue |
| 135 | + auto out = queue_->popForWrite(); |
| 136 | + out->resize(nsamples); |
| 137 | + |
| 138 | + // Convert unsigned char to std::complex<float> |
| 139 | + process(nsamples, transfer->buffer, out->data()); |
| 140 | + |
| 141 | + // Publish output if applicable |
| 142 | + if (samplePublisher_) { |
| 143 | + samplePublisher_->publish(*out); |
| 144 | + } |
| 145 | + |
| 146 | + // Return buffer to queue |
| 147 | + queue_->pushWrite(std::move(out)); |
| 148 | +} |
0 commit comments