hackrf_transfer -n num_samples

This commit is contained in:
Michael Ossmann
2013-03-23 09:55:32 -06:00
parent 11db96e93f
commit 500baf9c49

View File

@ -124,17 +124,31 @@ uint64_t freq_hz;
bool amp = false; bool amp = false;
uint32_t amp_enable; uint32_t amp_enable;
bool limit_num_samples = false;
uint64_t samples_to_xfer = 0;
uint64_t bytes_to_xfer = 0;
int rx_callback(hackrf_transfer* transfer) { int rx_callback(hackrf_transfer* transfer) {
int bytes_to_write;
if( fd != NULL ) if( fd != NULL )
{ {
byte_count += transfer->valid_length; byte_count += transfer->valid_length;
const ssize_t bytes_written = fwrite(transfer->buffer, 1, transfer->valid_length, fd); bytes_to_write = transfer->valid_length;
if( bytes_written == transfer->valid_length ) { if (limit_num_samples) {
return 0; if (bytes_to_write >= bytes_to_xfer) {
} else { bytes_to_write = bytes_to_xfer;
}
bytes_to_xfer -= bytes_to_write;
}
const ssize_t bytes_written = fwrite(transfer->buffer, 1, bytes_to_write, fd);
if ((bytes_written != bytes_to_write)
|| (limit_num_samples && (bytes_to_xfer == 0))) {
fclose(fd); fclose(fd);
fd = NULL; fd = NULL;
return -1; return -1;
} else {
return 0;
} }
} else { } else {
return -1; return -1;
@ -142,16 +156,30 @@ int rx_callback(hackrf_transfer* transfer) {
} }
int tx_callback(hackrf_transfer* transfer) { int tx_callback(hackrf_transfer* transfer) {
int bytes_to_read;
if( fd != NULL ) if( fd != NULL )
{ {
byte_count += transfer->valid_length; byte_count += transfer->valid_length;
const ssize_t bytes_read = fread(transfer->buffer, 1, transfer->valid_length, fd); bytes_to_read = transfer->valid_length;
if( bytes_read == transfer->valid_length ) { if (limit_num_samples) {
return 0; if (bytes_to_read >= bytes_to_xfer) {
} else { /*
* In this condition, we probably tx some of the previous
* buffer contents at the end. :-(
*/
bytes_to_read = bytes_to_xfer;
}
bytes_to_xfer -= bytes_to_read;
}
const ssize_t bytes_read = fread(transfer->buffer, 1, bytes_to_read, fd);
if ((bytes_read != bytes_to_read)
|| (limit_num_samples && (bytes_to_xfer == 0))) {
fclose(fd); fclose(fd);
fd = NULL; fd = NULL;
return -1; return -1;
} else {
return 0;
} }
} else { } else {
return -1; return -1;
@ -164,6 +192,7 @@ static void usage() {
printf("\t-t <filename> # Transmit data from file.\n"); printf("\t-t <filename> # Transmit data from file.\n");
printf("\t[-f set_freq_hz] # Set Freq in Hz (between [%lld, %lld[).\n", FREQ_MIN_HZ, FREQ_MAX_HZ); printf("\t[-f set_freq_hz] # Set Freq in Hz (between [%lld, %lld[).\n", FREQ_MIN_HZ, FREQ_MAX_HZ);
printf("\t[-a set_amp] # Set Amp 1=Enable, 0=Disable.\n"); printf("\t[-a set_amp] # Set Amp 1=Enable, 0=Disable.\n");
printf("\t[-n num_samples] # Number of samples to transfer (default is unlimited).\n");
} }
static hackrf_device* device = NULL; static hackrf_device* device = NULL;
@ -229,7 +258,7 @@ int main(int argc, char** argv) {
const char* path = NULL; const char* path = NULL;
int result; int result;
while( (opt = getopt(argc, argv, "r:t:f:a:")) != EOF ) { while( (opt = getopt(argc, argv, "r:t:f:a:n:")) != EOF ) {
result = HACKRF_SUCCESS; result = HACKRF_SUCCESS;
switch( opt ) { switch( opt ) {
case 'r': case 'r':
@ -252,6 +281,12 @@ int main(int argc, char** argv) {
result = parse_int(optarg, &amp_enable); result = parse_int(optarg, &amp_enable);
break; break;
case 'n':
limit_num_samples = true;
result = parse_u64(optarg, &samples_to_xfer);
bytes_to_xfer = samples_to_xfer * 2;
break;
default: default:
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
@ -264,6 +299,12 @@ int main(int argc, char** argv) {
} }
} }
if (samples_to_xfer >= 0x8000000000000000ul) {
printf("argument error: num_samples must be less than %lu\n", 0x8000000000000000ul);
usage();
return EXIT_FAILURE;
}
if( freq ) { if( freq ) {
if( (freq_hz >= FREQ_MAX_HZ) || (freq_hz < FREQ_MIN_HZ) ) if( (freq_hz >= FREQ_MAX_HZ) || (freq_hz < FREQ_MIN_HZ) )
{ {
@ -358,7 +399,7 @@ int main(int argc, char** argv) {
} }
if( freq ) { if( freq ) {
printf("call hackrf_set_freq(%lld Hz)\n", freq_hz); printf("call hackrf_set_freq(%lu Hz)\n", freq_hz);
result = hackrf_set_freq(device, freq_hz); result = hackrf_set_freq(device, freq_hz);
if( result != HACKRF_SUCCESS ) { if( result != HACKRF_SUCCESS ) {
printf("hackrf_set_freq() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_set_freq() failed: %s (%d)\n", hackrf_error_name(result), result);
@ -367,7 +408,7 @@ int main(int argc, char** argv) {
} }
if( amp ) { if( amp ) {
printf("call hackrf_set_amp_enable(%ld)\n", amp_enable); printf("call hackrf_set_amp_enable(%u)\n", amp_enable);
result = hackrf_set_amp_enable(device, (uint8_t)amp_enable); result = hackrf_set_amp_enable(device, (uint8_t)amp_enable);
if( result != HACKRF_SUCCESS ) { if( result != HACKRF_SUCCESS ) {
printf("hackrf_set_amp_enable() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_set_amp_enable() failed: %s (%d)\n", hackrf_error_name(result), result);