diff --git a/config/config-hackrf.yml b/config/config-hackrf.yml index cba6840..21a8c9a 100644 --- a/config/config-hackrf.yml +++ b/config/config-hackrf.yml @@ -4,16 +4,15 @@ capture: device: type: "HackRF" serial: - - "FIRST_DEVICE_SERIAL_NUMBER" - - "SECOND_DEVICE_SERIAL_NUMBER" + - "REFERENCE_DEVICE_SERIAL_NUMBER" + - "SURVILLANCE_DEVICE_SERIAL_NUMBER" gain_lna: [32, 32] gain_vga: [30, 30] amp_enable: [false, false] - replay: state: false loop: true - file: '/opt/blah2/replay/file.rspduo' + file: '/opt/blah2/replay/file.hackrf' process: data: diff --git a/src/capture/Capture.cpp b/src/capture/Capture.cpp index f1a7844..ad636d7 100644 --- a/src/capture/Capture.cpp +++ b/src/capture/Capture.cpp @@ -94,23 +94,28 @@ std::unique_ptr Capture::factory_source(const std::string& type, c4::yml else if (type == VALID_TYPE[2]) { std::vector serial; - std::vector gainLna, gainVga; + std::vector gainLna, gainVga; std::vector ampEnable; std::string _serial; - uint8_t _gainLna, _gainVga; + uint32_t gain; + int _gain; bool _ampEnable; config["serial"][0] >> _serial; serial.push_back(_serial); config["serial"][1] >> _serial; serial.push_back(_serial); - config["gain_lna"][0] >> _gainLna; - gainLna.push_back(_gainLna); - config["gain_lna"][1] >> _gainLna; - gainLna.push_back(_gainLna); - config["gain_vga"][0] >> _gainVga; - gainVga.push_back(_gainVga); - config["gain_vga"][1] >> _gainVga; - gainVga.push_back(_gainVga); + config["gain_lna"][0] >> _gain; + gain = static_cast (_gain); + gainLna.push_back(gain); + config["gain_lna"][1] >> _gain; + gain = static_cast(_gain); + gainLna.push_back(gain); + config["gain_vga"][0] >> _gain; + gain = static_cast(_gain); + gainVga.push_back(gain); + config["gain_vga"][1] >> _gain; + gain = static_cast(_gain); + gainVga.push_back(gain); config["amp_enable"][0] >> _ampEnable; ampEnable.push_back(_ampEnable); config["amp_enable"][1] >> _ampEnable; diff --git a/src/capture/hackrf/HackRf.cpp b/src/capture/hackrf/HackRf.cpp index 42ad7b5..4aefa01 100644 --- a/src/capture/hackrf/HackRf.cpp +++ b/src/capture/hackrf/HackRf.cpp @@ -8,7 +8,7 @@ // constructor HackRf::HackRf(std::string _type, uint32_t _fc, uint32_t _fs, std::string _path, bool *_saveIq, std::vector _serial, - std::vector _gainLna, std::vector _gainVga, + std::vector _gainLna, std::vector _gainVga, std::vector _ampEnable) : Source(_type, _fc, _fs, _path, _saveIq) { @@ -16,11 +16,11 @@ HackRf::HackRf(std::string _type, uint32_t _fc, uint32_t _fs, ampEnable = _ampEnable; // validate LNA gain - std::unordered_set validLna; - for (uint8_t gain = 0; gain <= 40; gain += 8) { + std::unordered_set validLna; + for (uint32_t gain = 0; gain <= 40; gain += 8) { validLna.insert(gain); } - for (uint8_t gain : _gainLna) { + for (uint32_t gain : _gainLna) { if (validLna.find(gain) == validLna.end()) { throw std::invalid_argument("Invalid LNA gain value"); } @@ -28,11 +28,11 @@ HackRf::HackRf(std::string _type, uint32_t _fc, uint32_t _fs, gainLna = _gainLna; // validate VGA gain - std::unordered_set validVga; - for (uint8_t gain = 0; gain <= 62; gain += 2) { + std::unordered_set validVga; + for (uint32_t gain = 0; gain <= 62; gain += 2) { validVga.insert(gain); } - for (uint8_t gain : _gainVga) { + for (uint32_t gain : _gainVga) { if (validVga.find(gain) == validVga.end()) { throw std::invalid_argument("Invalid LNA gain value"); } @@ -56,25 +56,11 @@ void HackRf::start() check_status(status, "Failed to initialise HackRF"); hackrf_device_list_t *list; list = hackrf_device_list(); - if (list->devicecount < 2) + if (!list || list->devicecount < 2) { check_status(-1, "Failed to find 2 HackRF devices."); } - // reference config - status = hackrf_open_by_serial(serial[0].c_str(), &dev[0]); - check_status(status, "Failed to open device."); - status = hackrf_set_freq(dev[0], fc); - check_status(status, "Failed to set frequency."); - status = hackrf_set_sample_rate(dev[0], fs); - check_status(status, "Failed to set sample rate."); - status = hackrf_set_amp_enable(dev[0], ampEnable[0] ? 1 : 0); - check_status(status, "Failed to set AMP status."); - status = hackrf_set_lna_gain(dev[0], gainLna[0]); - check_status(status, "Failed to set LNA gain."); - status = hackrf_set_vga_gain(dev[0], gainVga[0]); - check_status(status, "Failed to set VGA gain."); - // surveillance config status = hackrf_open_by_serial(serial[1].c_str(), &dev[1]); check_status(status, "Failed to open device."); @@ -90,6 +76,20 @@ void HackRf::start() check_status(status, "Failed to set VGA gain."); status = hackrf_set_hw_sync_mode(dev[1], 1); check_status(status, "Failed to enable hardware synchronising."); + + // reference config + status = hackrf_open_by_serial(serial[0].c_str(), &dev[0]); + check_status(status, "Failed to open device."); + status = hackrf_set_freq(dev[0], fc); + check_status(status, "Failed to set frequency."); + status = hackrf_set_sample_rate(dev[0], fs); + check_status(status, "Failed to set sample rate."); + status = hackrf_set_amp_enable(dev[0], ampEnable[0] ? 1 : 0); + check_status(status, "Failed to set AMP status."); + status = hackrf_set_lna_gain(dev[0], gainLna[0]); + check_status(status, "Failed to set LNA gain."); + status = hackrf_set_vga_gain(dev[0], gainVga[0]); + check_status(status, "Failed to set VGA gain."); } void HackRf::stop() @@ -104,10 +104,14 @@ void HackRf::stop() void HackRf::process(IqData *buffer1, IqData *buffer2) { int status; - status = hackrf_start_rx(dev[1], &rx_callback, buffer2); - check_status(status, "Failed to start RX streaming."); - status = hackrf_start_rx(dev[0], &rx_callback, buffer1); - check_status(status, "Failed to start RX streaming."); + + while (true) + { + status = hackrf_start_rx(dev[1], rx_callback, buffer2); + check_status(status, "Failed to start RX streaming."); + status = hackrf_start_rx(dev[0], rx_callback, buffer1); + check_status(status, "Failed to start RX streaming."); + } } int HackRf::rx_callback(hackrf_transfer* transfer) @@ -116,14 +120,14 @@ int HackRf::rx_callback(hackrf_transfer* transfer) int8_t* buffer_hackrf = (int8_t*) transfer->buffer; buffer_blah2->lock(); - - for (size_t i = 0; i < transfer->valid_length; i+2) + + for (size_t i = 0; i < transfer->buffer_length; i=i+2) { double iqi = static_cast(buffer_hackrf[i]); double iqq = static_cast(buffer_hackrf[i+1]); buffer_blah2->push_back({iqi, iqq}); } - + buffer_blah2->unlock(); return 0; @@ -133,3 +137,4 @@ void HackRf::replay(IqData *buffer1, IqData *buffer2, std::string _file, bool _l { return; } + diff --git a/src/capture/hackrf/HackRf.h b/src/capture/hackrf/HackRf.h index 6bb1329..42bbb74 100644 --- a/src/capture/hackrf/HackRf.h +++ b/src/capture/hackrf/HackRf.h @@ -25,22 +25,23 @@ private: std::vector serial; /// @brief RX LNA (IF) gain, 0-40dB, 8dB steps. - std::vector gainLna; + std::vector gainLna; /// @brief RX VGA (baseband) gain, 0-62dB, 2dB steps. - std::vector gainVga; + std::vector gainVga; /// @brief Enable extra amplifier U13 on receive. std::vector ampEnable; - /// @brief Vector of pointers to HackRF devices. - std::vector dev; - /// @brief Check status of HackRF API returns. /// @param status Return code of API call. /// @param message Message if API call error. void check_status(uint8_t status, std::string message); +protected: + /// @brief Array of pointers to HackRF devices. + hackrf_device* dev[2]; + /// @brief Callback function for HackRF samples. /// @param transfer HackRF transfer object. /// @return Void. @@ -54,7 +55,7 @@ public: /// @return The object. HackRf(std::string type, uint32_t fc, uint32_t fs, std::string path, bool *saveIq, std::vector serial, - std::vector gainLna, std::vector gainVga, + std::vector gainLna, std::vector gainVga, std::vector ampEnable); /// @brief Implement capture function on HackRF. @@ -81,4 +82,4 @@ public: }; -#endif \ No newline at end of file +#endif