Merge pull request #15 from 30hours/sdn-ninja-sdn-ninja-hackrf-fixes

Sdn ninja sdn ninja hackrf fixes
This commit is contained in:
30hours 2024-05-09 21:13:33 +09:30 committed by GitHub
commit bf29d1431f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 60 additions and 50 deletions

View file

@ -4,16 +4,15 @@ capture:
device: device:
type: "HackRF" type: "HackRF"
serial: serial:
- "FIRST_DEVICE_SERIAL_NUMBER" - "REFERENCE_DEVICE_SERIAL_NUMBER"
- "SECOND_DEVICE_SERIAL_NUMBER" - "SURVILLANCE_DEVICE_SERIAL_NUMBER"
gain_lna: [32, 32] gain_lna: [32, 32]
gain_vga: [30, 30] gain_vga: [30, 30]
amp_enable: [false, false] amp_enable: [false, false]
replay: replay:
state: false state: false
loop: true loop: true
file: '/opt/blah2/replay/file.rspduo' file: '/opt/blah2/replay/file.hackrf'
process: process:
data: data:

View file

@ -94,23 +94,28 @@ std::unique_ptr<Source> Capture::factory_source(const std::string& type, c4::yml
else if (type == VALID_TYPE[2]) else if (type == VALID_TYPE[2])
{ {
std::vector<std::string> serial; std::vector<std::string> serial;
std::vector<uint8_t> gainLna, gainVga; std::vector<uint32_t> gainLna, gainVga;
std::vector<bool> ampEnable; std::vector<bool> ampEnable;
std::string _serial; std::string _serial;
uint8_t _gainLna, _gainVga; uint32_t gain;
int _gain;
bool _ampEnable; bool _ampEnable;
config["serial"][0] >> _serial; config["serial"][0] >> _serial;
serial.push_back(_serial); serial.push_back(_serial);
config["serial"][1] >> _serial; config["serial"][1] >> _serial;
serial.push_back(_serial); serial.push_back(_serial);
config["gain_lna"][0] >> _gainLna; config["gain_lna"][0] >> _gain;
gainLna.push_back(_gainLna); gain = static_cast<uint32_t> (_gain);
config["gain_lna"][1] >> _gainLna; gainLna.push_back(gain);
gainLna.push_back(_gainLna); config["gain_lna"][1] >> _gain;
config["gain_vga"][0] >> _gainVga; gain = static_cast<uint32_t>(_gain);
gainVga.push_back(_gainVga); gainLna.push_back(gain);
config["gain_vga"][1] >> _gainVga; config["gain_vga"][0] >> _gain;
gainVga.push_back(_gainVga); gain = static_cast<uint32_t>(_gain);
gainVga.push_back(gain);
config["gain_vga"][1] >> _gain;
gain = static_cast<uint32_t>(_gain);
gainVga.push_back(gain);
config["amp_enable"][0] >> _ampEnable; config["amp_enable"][0] >> _ampEnable;
ampEnable.push_back(_ampEnable); ampEnable.push_back(_ampEnable);
config["amp_enable"][1] >> _ampEnable; config["amp_enable"][1] >> _ampEnable;

View file

@ -8,7 +8,7 @@
// constructor // constructor
HackRf::HackRf(std::string _type, uint32_t _fc, uint32_t _fs, HackRf::HackRf(std::string _type, uint32_t _fc, uint32_t _fs,
std::string _path, bool *_saveIq, std::vector<std::string> _serial, std::string _path, bool *_saveIq, std::vector<std::string> _serial,
std::vector<uint8_t> _gainLna, std::vector<uint8_t> _gainVga, std::vector<uint32_t> _gainLna, std::vector<uint32_t> _gainVga,
std::vector<bool> _ampEnable) std::vector<bool> _ampEnable)
: Source(_type, _fc, _fs, _path, _saveIq) : Source(_type, _fc, _fs, _path, _saveIq)
{ {
@ -16,11 +16,11 @@ HackRf::HackRf(std::string _type, uint32_t _fc, uint32_t _fs,
ampEnable = _ampEnable; ampEnable = _ampEnable;
// validate LNA gain // validate LNA gain
std::unordered_set<uint8_t> validLna; std::unordered_set<uint32_t> validLna;
for (uint8_t gain = 0; gain <= 40; gain += 8) { for (uint32_t gain = 0; gain <= 40; gain += 8) {
validLna.insert(gain); validLna.insert(gain);
} }
for (uint8_t gain : _gainLna) { for (uint32_t gain : _gainLna) {
if (validLna.find(gain) == validLna.end()) { if (validLna.find(gain) == validLna.end()) {
throw std::invalid_argument("Invalid LNA gain value"); 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; gainLna = _gainLna;
// validate VGA gain // validate VGA gain
std::unordered_set<uint8_t> validVga; std::unordered_set<uint32_t> validVga;
for (uint8_t gain = 0; gain <= 62; gain += 2) { for (uint32_t gain = 0; gain <= 62; gain += 2) {
validVga.insert(gain); validVga.insert(gain);
} }
for (uint8_t gain : _gainVga) { for (uint32_t gain : _gainVga) {
if (validVga.find(gain) == validVga.end()) { if (validVga.find(gain) == validVga.end()) {
throw std::invalid_argument("Invalid LNA gain value"); throw std::invalid_argument("Invalid LNA gain value");
} }
@ -56,25 +56,11 @@ void HackRf::start()
check_status(status, "Failed to initialise HackRF"); check_status(status, "Failed to initialise HackRF");
hackrf_device_list_t *list; hackrf_device_list_t *list;
list = hackrf_device_list(); list = hackrf_device_list();
if (list->devicecount < 2) if (!list || list->devicecount < 2)
{ {
check_status(-1, "Failed to find 2 HackRF devices."); 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 // surveillance config
status = hackrf_open_by_serial(serial[1].c_str(), &dev[1]); status = hackrf_open_by_serial(serial[1].c_str(), &dev[1]);
check_status(status, "Failed to open device."); check_status(status, "Failed to open device.");
@ -90,6 +76,20 @@ void HackRf::start()
check_status(status, "Failed to set VGA gain."); check_status(status, "Failed to set VGA gain.");
status = hackrf_set_hw_sync_mode(dev[1], 1); status = hackrf_set_hw_sync_mode(dev[1], 1);
check_status(status, "Failed to enable hardware synchronising."); 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() void HackRf::stop()
@ -104,10 +104,14 @@ void HackRf::stop()
void HackRf::process(IqData *buffer1, IqData *buffer2) void HackRf::process(IqData *buffer1, IqData *buffer2)
{ {
int status; int status;
status = hackrf_start_rx(dev[1], &rx_callback, buffer2);
check_status(status, "Failed to start RX streaming."); while (true)
status = hackrf_start_rx(dev[0], &rx_callback, buffer1); {
check_status(status, "Failed to start RX streaming."); 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) int HackRf::rx_callback(hackrf_transfer* transfer)
@ -117,7 +121,7 @@ int HackRf::rx_callback(hackrf_transfer* transfer)
buffer_blah2->lock(); 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<double>(buffer_hackrf[i]); double iqi = static_cast<double>(buffer_hackrf[i]);
double iqq = static_cast<double>(buffer_hackrf[i+1]); double iqq = static_cast<double>(buffer_hackrf[i+1]);
@ -133,3 +137,4 @@ void HackRf::replay(IqData *buffer1, IqData *buffer2, std::string _file, bool _l
{ {
return; return;
} }

View file

@ -25,22 +25,23 @@ private:
std::vector<std::string> serial; std::vector<std::string> serial;
/// @brief RX LNA (IF) gain, 0-40dB, 8dB steps. /// @brief RX LNA (IF) gain, 0-40dB, 8dB steps.
std::vector<uint8_t> gainLna; std::vector<uint32_t> gainLna;
/// @brief RX VGA (baseband) gain, 0-62dB, 2dB steps. /// @brief RX VGA (baseband) gain, 0-62dB, 2dB steps.
std::vector<uint8_t> gainVga; std::vector<uint32_t> gainVga;
/// @brief Enable extra amplifier U13 on receive. /// @brief Enable extra amplifier U13 on receive.
std::vector<bool> ampEnable; std::vector<bool> ampEnable;
/// @brief Vector of pointers to HackRF devices.
std::vector<hackrf_device*> dev;
/// @brief Check status of HackRF API returns. /// @brief Check status of HackRF API returns.
/// @param status Return code of API call. /// @param status Return code of API call.
/// @param message Message if API call error. /// @param message Message if API call error.
void check_status(uint8_t status, std::string message); 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. /// @brief Callback function for HackRF samples.
/// @param transfer HackRF transfer object. /// @param transfer HackRF transfer object.
/// @return Void. /// @return Void.
@ -54,7 +55,7 @@ public:
/// @return The object. /// @return The object.
HackRf(std::string type, uint32_t fc, uint32_t fs, std::string path, HackRf(std::string type, uint32_t fc, uint32_t fs, std::string path,
bool *saveIq, std::vector<std::string> serial, bool *saveIq, std::vector<std::string> serial,
std::vector<uint8_t> gainLna, std::vector<uint8_t> gainVga, std::vector<uint32_t> gainLna, std::vector<uint32_t> gainVga,
std::vector<bool> ampEnable); std::vector<bool> ampEnable);
/// @brief Implement capture function on HackRF. /// @brief Implement capture function on HackRF.