Modified code to support dual HackRF

This commit is contained in:
aldude999 2021-02-21 00:09:58 -06:00 committed by aldude999
parent a07764f0d9
commit 7a4454aa61

View File

@ -31,6 +31,7 @@
extern int sdr_rx_overflow; extern int sdr_rx_overflow;
static SoapySDRDevice *sdr = NULL; static SoapySDRDevice *sdr = NULL;
static SoapySDRDevice *sdr2 = NULL;
SoapySDRStream *rxStream = NULL; SoapySDRStream *rxStream = NULL;
SoapySDRStream *txStream = NULL; SoapySDRStream *txStream = NULL;
static int tx_samps_per_buff, rx_samps_per_buff; static int tx_samps_per_buff, rx_samps_per_buff;
@ -95,10 +96,11 @@ int soapy_open(size_t channel, const char *_device_args, const char *_stream_arg
SoapySDRKwargs_set(&tune_args, "OFFSET", val); SoapySDRKwargs_set(&tune_args, "OFFSET", val);
} }
/* create SoapySDR device */ /* create SoapySDR devices */
sdr = SoapySDRDevice_make(&device_args); sdr = SoapySDRDevice_make(&device_args);
if (!sdr) { sdr2 = SoapySDRDevice_make(&device_args); //Two HackRF setup for full duplex :)
PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to create SoapySDR\n"); if (!sdr && !sdr2) {
PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to create double HackRF Setup\n");
soapy_close(); soapy_close();
return -EIO; return -EIO;
} }
@ -265,7 +267,7 @@ int soapy_open(size_t channel, const char *_device_args, const char *_stream_arg
if (tx_frequency) { if (tx_frequency) {
/* get number of channels and check if requested channel is in range */ /* get number of channels and check if requested channel is in range */
num_channels = SoapySDRDevice_getNumChannels(sdr, SOAPY_SDR_TX); num_channels = SoapySDRDevice_getNumChannels(sdr2, SOAPY_SDR_TX);
PDEBUG(DSOAPY, DEBUG_DEBUG, "We have %d TX channel, selecting channel #%d\n", (int)num_channels, (int)channel); PDEBUG(DSOAPY, DEBUG_DEBUG, "We have %d TX channel, selecting channel #%d\n", (int)num_channels, (int)channel);
if (channel >= num_channels) { if (channel >= num_channels) {
PDEBUG(DSOAPY, DEBUG_ERROR, "Requested channel #%d (capable of TX) does not exist. Please select channel %d..%d!\n", (int)channel, 0, (int)num_channels - 1); PDEBUG(DSOAPY, DEBUG_ERROR, "Requested channel #%d (capable of TX) does not exist. Please select channel %d..%d!\n", (int)channel, 0, (int)num_channels - 1);
@ -279,7 +281,7 @@ int soapy_open(size_t channel, const char *_device_args, const char *_stream_arg
char **antennas; char **antennas;
size_t antennas_length; size_t antennas_length;
int i; int i;
antennas = SoapySDRDevice_listAntennas(sdr, SOAPY_SDR_TX, channel, &antennas_length); antennas = SoapySDRDevice_listAntennas(sdr2, SOAPY_SDR_TX, channel, &antennas_length);
if (!antennas) { if (!antennas) {
PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to request list of TX antennas!\n"); PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to request list of TX antennas!\n");
soapy_close(); soapy_close();
@ -287,18 +289,18 @@ int soapy_open(size_t channel, const char *_device_args, const char *_stream_arg
} }
for (i = 0; i < (int)antennas_length; i++) for (i = 0; i < (int)antennas_length; i++)
PDEBUG(DSOAPY, DEBUG_NOTICE, "TX Antenna: '%s'\n", antennas[i]); PDEBUG(DSOAPY, DEBUG_NOTICE, "TX Antenna: '%s'\n", antennas[i]);
got_antenna = SoapySDRDevice_getAntenna(sdr, SOAPY_SDR_TX, channel); got_antenna = SoapySDRDevice_getAntenna(sdr2, SOAPY_SDR_TX, channel);
PDEBUG(DSOAPY, DEBUG_NOTICE, "Default TX Antenna: '%s'\n", got_antenna); PDEBUG(DSOAPY, DEBUG_NOTICE, "Default TX Antenna: '%s'\n", got_antenna);
soapy_close(); soapy_close();
return 1; return 1;
} }
if (SoapySDRDevice_setAntenna(sdr, SOAPY_SDR_TX, channel, tx_antenna) != 0) { if (SoapySDRDevice_setAntenna(sdr2, SOAPY_SDR_TX, channel, tx_antenna) != 0) {
PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to set TX antenna to '%s'\n", tx_antenna); PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to set TX antenna to '%s'\n", tx_antenna);
soapy_close(); soapy_close();
return -EIO; return -EIO;
} }
got_antenna = SoapySDRDevice_getAntenna(sdr, SOAPY_SDR_TX, channel); got_antenna = SoapySDRDevice_getAntenna(sdr2, SOAPY_SDR_TX, channel);
if (!!strcasecmp(tx_antenna, got_antenna)) { if (!!strcasecmp(tx_antenna, got_antenna)) {
PDEBUG(DSOAPY, DEBUG_NOTICE, "Given TX antenna '%s' was accepted, but driver claims to use '%s'\n", tx_antenna, got_antenna); PDEBUG(DSOAPY, DEBUG_NOTICE, "Given TX antenna '%s' was accepted, but driver claims to use '%s'\n", tx_antenna, got_antenna);
soapy_close(); soapy_close();
@ -307,14 +309,14 @@ int soapy_open(size_t channel, const char *_device_args, const char *_stream_arg
} }
/* set rate */ /* set rate */
if (SoapySDRDevice_setSampleRate(sdr, SOAPY_SDR_TX, channel, rate) != 0) { if (SoapySDRDevice_setSampleRate(sdr2, SOAPY_SDR_TX, channel, rate) != 0) {
PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to set TX rate to %.0f Hz\n", rate); PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to set TX rate to %.0f Hz\n", rate);
soapy_close(); soapy_close();
return -EIO; return -EIO;
} }
/* see what rate actually is */ /* see what rate actually is */
got_rate = SoapySDRDevice_getSampleRate(sdr, SOAPY_SDR_TX, channel); got_rate = SoapySDRDevice_getSampleRate(sdr2, SOAPY_SDR_TX, channel);
if (fabs(got_rate - rate) > 1.0) { if (fabs(got_rate - rate) > 1.0) {
PDEBUG(DSOAPY, DEBUG_ERROR, "Given TX rate %.3f Hz is not supported, try %.3f Hz\n", rate, got_rate); PDEBUG(DSOAPY, DEBUG_ERROR, "Given TX rate %.3f Hz is not supported, try %.3f Hz\n", rate, got_rate);
soapy_close(); soapy_close();
@ -323,14 +325,14 @@ int soapy_open(size_t channel, const char *_device_args, const char *_stream_arg
if (tx_gain) { if (tx_gain) {
/* set gain */ /* set gain */
if (SoapySDRDevice_setGain(sdr, SOAPY_SDR_TX, channel, tx_gain) != 0) { if (SoapySDRDevice_setGain(sdr2, SOAPY_SDR_TX, channel, tx_gain) != 0) {
PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to set TX gain to %.0f\n", tx_gain); PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to set TX gain to %.0f\n", tx_gain);
soapy_close(); soapy_close();
return -EIO; return -EIO;
} }
/* see what gain actually is */ /* see what gain actually is */
got_gain = SoapySDRDevice_getGain(sdr, SOAPY_SDR_TX, channel); got_gain = SoapySDRDevice_getGain(sdr2, SOAPY_SDR_TX, channel);
if (fabs(got_gain - tx_gain) > 0.001) { if (fabs(got_gain - tx_gain) > 0.001) {
PDEBUG(DSOAPY, DEBUG_NOTICE, "Given TX gain %.3f is not supported, we use %.3f\n", tx_gain, got_gain); PDEBUG(DSOAPY, DEBUG_NOTICE, "Given TX gain %.3f is not supported, we use %.3f\n", tx_gain, got_gain);
tx_gain = got_gain; tx_gain = got_gain;
@ -338,14 +340,14 @@ int soapy_open(size_t channel, const char *_device_args, const char *_stream_arg
} }
/* set frequency */ /* set frequency */
if (SoapySDRDevice_setFrequency(sdr, SOAPY_SDR_TX, channel, tx_frequency, &tune_args) != 0) { if (SoapySDRDevice_setFrequency(sdr2, SOAPY_SDR_TX, channel, tx_frequency, &tune_args) != 0) {
PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to set TX frequency to %.0f Hz\n", tx_frequency); PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to set TX frequency to %.0f Hz\n", tx_frequency);
soapy_close(); soapy_close();
return -EIO; return -EIO;
} }
/* see what frequency actually is */ /* see what frequency actually is */
got_frequency = SoapySDRDevice_getFrequency(sdr, SOAPY_SDR_TX, channel); got_frequency = SoapySDRDevice_getFrequency(sdr2, SOAPY_SDR_TX, channel);
if (fabs(got_frequency - tx_frequency) > 100.0) { if (fabs(got_frequency - tx_frequency) > 100.0) {
PDEBUG(DSOAPY, DEBUG_ERROR, "Given TX frequency %.0f Hz is not supported, try %.0f Hz\n", tx_frequency, got_frequency); PDEBUG(DSOAPY, DEBUG_ERROR, "Given TX frequency %.0f Hz is not supported, try %.0f Hz\n", tx_frequency, got_frequency);
soapy_close(); soapy_close();
@ -353,14 +355,14 @@ int soapy_open(size_t channel, const char *_device_args, const char *_stream_arg
} }
/* set bandwidth */ /* set bandwidth */
if (SoapySDRDevice_setBandwidth(sdr, SOAPY_SDR_TX, channel, bandwidth) != 0) { if (SoapySDRDevice_setBandwidth(sdr2, SOAPY_SDR_TX, channel, bandwidth) != 0) {
PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to set TX bandwidth to %.0f Hz\n", bandwidth); PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to set TX bandwidth to %.0f Hz\n", bandwidth);
soapy_close(); soapy_close();
return -EIO; return -EIO;
} }
/* see what bandwidth actually is */ /* see what bandwidth actually is */
got_bandwidth = SoapySDRDevice_getBandwidth(sdr, SOAPY_SDR_TX, channel); got_bandwidth = SoapySDRDevice_getBandwidth(sdr2, SOAPY_SDR_TX, channel);
if (fabs(got_bandwidth - bandwidth) > 100.0) { if (fabs(got_bandwidth - bandwidth) > 100.0) {
PDEBUG(DSOAPY, DEBUG_ERROR, "Given TX bandwidth %.0f Hz is not supported, try %.0f Hz\n", bandwidth, got_bandwidth); PDEBUG(DSOAPY, DEBUG_ERROR, "Given TX bandwidth %.0f Hz is not supported, try %.0f Hz\n", bandwidth, got_bandwidth);
soapy_close(); soapy_close();
@ -368,14 +370,14 @@ int soapy_open(size_t channel, const char *_device_args, const char *_stream_arg
} }
/* set up streamer */ /* set up streamer */
if (SoapySDRDevice_setupStream(sdr, &txStream, SOAPY_SDR_TX, SOAPY_SDR_CF32, &channel, 1, &stream_args) != 0) { if (SoapySDRDevice_setupStream(sdr2, &txStream, SOAPY_SDR_TX, SOAPY_SDR_CF32, &channel, 1, &stream_args) != 0) {
PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to set TX streamer args\n"); PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to set TX streamer args\n");
soapy_close(); soapy_close();
return -EIO; return -EIO;
} }
/* get buffer sizes */ /* get buffer sizes */
tx_samps_per_buff = SoapySDRDevice_getStreamMTU(sdr, txStream); tx_samps_per_buff = SoapySDRDevice_getStreamMTU(sdr2, txStream);
if (tx_samps_per_buff == 0) { if (tx_samps_per_buff == 0) {
PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to get TX streamer sample buffer\n"); PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to get TX streamer sample buffer\n");
soapy_close(); soapy_close();
@ -396,7 +398,7 @@ int soapy_start(void)
} }
/* enable tx stream */ /* enable tx stream */
if (SoapySDRDevice_activateStream(sdr, txStream, 0, 0, 0) != 0) { if (SoapySDRDevice_activateStream(sdr2, txStream, 0, 0, 0) != 0) {
PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to issue TX stream command\n"); PDEBUG(DSOAPY, DEBUG_ERROR, "Failed to issue TX stream command\n");
return -EIO; return -EIO;
} }
@ -407,8 +409,8 @@ void soapy_close(void)
{ {
PDEBUG(DSOAPY, DEBUG_DEBUG, "Clean up SoapySDR\n"); PDEBUG(DSOAPY, DEBUG_DEBUG, "Clean up SoapySDR\n");
if (txStream) { if (txStream) {
SoapySDRDevice_deactivateStream(sdr, txStream, 0, 0); SoapySDRDevice_deactivateStream(sdr2, txStream, 0, 0);
SoapySDRDevice_closeStream(sdr, txStream); SoapySDRDevice_closeStream(sdr2, txStream);
txStream = NULL; txStream = NULL;
} }
if (rxStream) { if (rxStream) {
@ -418,7 +420,9 @@ void soapy_close(void)
} }
if (sdr) { if (sdr) {
SoapySDRDevice_unmake(sdr); SoapySDRDevice_unmake(sdr);
SoapySDRDevice_unmake(sdr2);
sdr = NULL; sdr = NULL;
sdr2 = NULL;
} }
} }
@ -435,7 +439,7 @@ int soapy_send(float *buff, int num)
chunk = tx_samps_per_buff; chunk = tx_samps_per_buff;
/* create tx metadata */ /* create tx metadata */
buffs_ptr[0] = buff; buffs_ptr[0] = buff;
count = SoapySDRDevice_writeStream(sdr, txStream, buffs_ptr, chunk, &flags, 0, 1000000); count = SoapySDRDevice_writeStream(sdr2, txStream, buffs_ptr, chunk, &flags, 0, 1000000);
if (count <= 0) { if (count <= 0) {
PDEBUG(DUHD, DEBUG_ERROR, "Failed to write to TX streamer (error=%d)\n", count); PDEBUG(DUHD, DEBUG_ERROR, "Failed to write to TX streamer (error=%d)\n", count);
break; break;