Fix to build Linux. Tested on Debian 9.

This commit is contained in:
Mictronics 2018-03-06 20:15:22 +01:00
parent c08b035efc
commit 48f52a3fed
2 changed files with 381 additions and 421 deletions

View File

@ -4,11 +4,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <libbladeRF.h> #include <libbladeRF.h>
#ifdef _WIN32
#include "getopt.h" #include "getopt.h"
#else #include <errno.h>
#include <unistd.h>
#endif
#define TX_FREQUENCY 1575420000 #define TX_FREQUENCY 1575420000
#define TX_SAMPLERATE 2600000 #define TX_SAMPLERATE 2600000
@ -23,293 +20,279 @@
#define AMPLITUDE (1000) // Default amplitude for 12-bit I/Q #define AMPLITUDE (1000) // Default amplitude for 12-bit I/Q
void usage(void) void usage(void) {
{ fprintf(stderr, "Usage: bladeplayer [options]\n"
fprintf(stderr, "Usage: bladeplayer [options]\n" " -f <tx_file> I/Q sampling data file (required)\n"
" -f <tx_file> I/Q sampling data file (required)\n" " -b <iq_bits> I/Q data format [1/16] (default: 16)\n"
" -b <iq_bits> I/Q data format [1/16] (default: 16)\n" " -g <tx_vga1> TX VGA1 gain (default: %d)\n",
" -g <tx_vga1> TX VGA1 gain (default: %d)\n", TX_VGA1);
TX_VGA1);
return; return;
} }
int main(int argc, char *argv[])
{
int status;
char *devstr = NULL;
struct bladerf *dev = NULL;
FILE *fp; int main(int argc, char *argv[]) {
int16_t *tx_buffer; int status;
enum state {INIT, READ_FILE, PAD_TRAILING, DONE}; char *devstr = NULL;
enum state state = INIT; struct bladerf *dev = NULL;
int compressed = 0; FILE *fp;
uint8_t *read_buffer; int16_t *tx_buffer;
size_t samples_read;
int16_t lut[256][8];
int16_t amp = AMPLITUDE;
int i,k;
int gain = TX_VGA1; enum state {
int result; INIT, READ_FILE, PAD_TRAILING, DONE
int data_format; };
char txfile[128]; enum state state = INIT;
// Empty TX file name int compressed = 0;
txfile[0] = 0; uint8_t *read_buffer;
size_t samples_read;
int16_t lut[256][8];
int16_t amp = AMPLITUDE;
uint32_t i, k;
if (argc<3) { int gain = TX_VGA1;
usage(); int result;
exit(1); int data_format;
} char txfile[128];
while ((result=getopt(argc,argv,"g:b:f:"))!=-1) // Empty TX file name
{ txfile[0] = 0;
switch (result)
{
case 'g':
gain = atoi(optarg);
if (gain>-4 || gain<-35)
{
printf("ERROR: Invalid TX VGA1 gain.\n");
exit(1);
}
break;
case 'b':
data_format = atoi(optarg);
if (data_format!=1 && data_format!=16)
{
printf("ERROR: Invalid I/Q data format.\n");
exit(1);
}
else if (data_format==1)
compressed = 1;
break;
case 'f':
strcpy(txfile, optarg);
break;
case ':':
case '?':
usage();
exit(1);
default:
break;
}
}
// Open TX file. if (argc < 3) {
if (txfile[0]==0) usage();
{ exit(1);
printf("ERROR: I/Q sampling data file is not specified.\n"); }
exit(1);
}
fp = fopen(txfile, "rb"); while ((result = getopt(argc, argv, "g:b:f:")) != -1) {
switch (result) {
case 'g':
gain = atoi(optarg);
if (gain>-4 || gain<-35) {
printf("ERROR: Invalid TX VGA1 gain.\n");
exit(1);
}
break;
case 'b':
data_format = atoi(optarg);
if (data_format != 1 && data_format != 16) {
printf("ERROR: Invalid I/Q data format.\n");
exit(1);
} else if (data_format == 1)
compressed = 1;
break;
case 'f':
strcpy(txfile, optarg);
break;
case ':':
case '?':
usage();
exit(1);
default:
break;
}
}
if (fp==NULL) { // Open TX file.
fprintf(stderr, "ERROR: Failed to open TX file: %s\n", argv[1]); if (txfile[0] == 0) {
exit(1); printf("ERROR: I/Q sampling data file is not specified.\n");
} exit(1);
}
// Initializing device. fp = fopen(txfile, "rb");
printf("Opening and initializing device...\n");
status = bladerf_open(&dev, devstr); if (fp == NULL) {
if (status != 0) { fprintf(stderr, "ERROR: Failed to open TX file: %s\n", argv[1]);
fprintf(stderr, "Failed to open device: %s\n", bladerf_strerror(status)); exit(1);
goto out; }
}
status = bladerf_set_frequency(dev, BLADERF_MODULE_TX, TX_FREQUENCY); // Initializing device.
if (status != 0) { printf("Opening and initializing device...\n");
fprintf(stderr, "Faield to set TX frequency: %s\n", bladerf_strerror(status));
goto out;
}
else {
printf("TX frequency: %u Hz\n", TX_FREQUENCY);
}
status = bladerf_set_sample_rate(dev, BLADERF_MODULE_TX, TX_SAMPLERATE, NULL); status = bladerf_open(&dev, devstr);
if (status != 0) { if (status != 0) {
fprintf(stderr, "Failed to set TX sample rate: %s\n", bladerf_strerror(status)); fprintf(stderr, "Failed to open device: %s\n", bladerf_strerror(status));
goto out; goto out;
} }
else {
printf("TX sample rate: %u sps\n", TX_SAMPLERATE);
}
status = bladerf_set_bandwidth(dev, BLADERF_MODULE_TX, TX_BANDWIDTH, NULL); status = bladerf_set_frequency(dev, BLADERF_MODULE_TX, TX_FREQUENCY);
if (status != 0) { if (status != 0) {
fprintf(stderr, "Failed to set TX bandwidth: %s\n", bladerf_strerror(status)); fprintf(stderr, "Faield to set TX frequency: %s\n", bladerf_strerror(status));
goto out; goto out;
} }
else { else {
printf("TX bandwidth: %u Hz\n", TX_BANDWIDTH); printf("TX frequency: %u Hz\n", TX_FREQUENCY);
} }
status = bladerf_set_txvga1(dev, gain); status = bladerf_set_sample_rate(dev, BLADERF_MODULE_TX, TX_SAMPLERATE, NULL);
if (status != 0) { if (status != 0) {
fprintf(stderr, "Failed to set TX VGA1 gain: %s\n", bladerf_strerror(status)); fprintf(stderr, "Failed to set TX sample rate: %s\n", bladerf_strerror(status));
goto out; goto out;
} } else {
else { printf("TX sample rate: %u sps\n", TX_SAMPLERATE);
printf("TX VGA1 gain: %d dB\n", gain); }
}
status = bladerf_set_txvga2(dev, TX_VGA2); status = bladerf_set_bandwidth(dev, BLADERF_MODULE_TX, TX_BANDWIDTH, NULL);
if (status != 0) { if (status != 0) {
fprintf(stderr, "Failed to set TX VGA2 gain: %s\n", bladerf_strerror(status)); fprintf(stderr, "Failed to set TX bandwidth: %s\n", bladerf_strerror(status));
goto out; goto out;
} } else {
else { printf("TX bandwidth: %u Hz\n", TX_BANDWIDTH);
printf("TX VGA2 gain: %d dB\n", TX_VGA2); }
}
// Application code goes here. status = bladerf_set_txvga1(dev, gain);
printf("Running...\n"); if (status != 0) {
fprintf(stderr, "Failed to set TX VGA1 gain: %s\n", bladerf_strerror(status));
goto out;
} else {
printf("TX VGA1 gain: %d dB\n", gain);
}
// Allocate a buffer to hold each block of samples to transmit. status = bladerf_set_txvga2(dev, TX_VGA2);
tx_buffer = (int16_t*)malloc(SAMPLES_PER_BUFFER * 2 * sizeof(int16_t)); if (status != 0) {
fprintf(stderr, "Failed to set TX VGA2 gain: %s\n", bladerf_strerror(status));
if (tx_buffer == NULL) { goto out;
fprintf(stderr, "Failed to allocate TX buffer.\n"); } else {
goto out; printf("TX VGA2 gain: %d dB\n", TX_VGA2);
} }
// if compressed // Application code goes here.
read_buffer = (uint8_t*)malloc(SAMPLES_PER_BUFFER / 4); printf("Running...\n");
if (read_buffer == NULL) { // Allocate a buffer to hold each block of samples to transmit.
fprintf(stderr, "Failed to allocate read buffer.\n"); tx_buffer = (int16_t*) malloc(SAMPLES_PER_BUFFER * 2 * sizeof (int16_t));
goto out;
}
for (i=0; i<256; i++) if (tx_buffer == NULL) {
{ fprintf(stderr, "Failed to allocate TX buffer.\n");
for (k=0; k<8; k++) goto out;
lut[i][k] = ((i>>(7-k))&0x1)?amp:-amp; }
}
// Configure the TX module for use with the synchronous interface. // if compressed
status = bladerf_sync_config(dev, read_buffer = (uint8_t*) malloc(SAMPLES_PER_BUFFER / 4);
BLADERF_MODULE_TX,
BLADERF_FORMAT_SC16_Q11,
NUM_BUFFERS,
SAMPLES_PER_BUFFER,
NUM_TRANSFERS,
TIMEOUT_MS);
if (status != 0) { if (read_buffer == NULL) {
fprintf(stderr, "Failed to configure TX sync interface: %s\n", bladerf_strerror(status)); fprintf(stderr, "Failed to allocate read buffer.\n");
goto out; goto out;
} }
// We must always enable the modules *after* calling bladerf_sync_config(). for (i = 0; i < 256; i++) {
status = bladerf_enable_module(dev, BLADERF_MODULE_TX, true); for (k = 0; k < 8; k++)
if (status != 0) { lut[i][k] = ((i >> (7 - k))&0x1) ? amp : -amp;
fprintf(stderr, "Failed to enable TX module: %s\n", bladerf_strerror(status)); }
goto out;
}
// Keep writing samples while there is more data to send and no failures have occurred. // Configure the TX module for use with the synchronous interface.
while (state != DONE && status == 0) { status = bladerf_sync_config(dev,
BLADERF_MODULE_TX,
BLADERF_FORMAT_SC16_Q11,
NUM_BUFFERS,
SAMPLES_PER_BUFFER,
NUM_TRANSFERS,
TIMEOUT_MS);
int16_t *tx_buffer_current = tx_buffer; if (status != 0) {
unsigned int buffer_samples_remaining = SAMPLES_PER_BUFFER; fprintf(stderr, "Failed to configure TX sync interface: %s\n", bladerf_strerror(status));
goto out;
}
// if compressed // We must always enable the modules *after* calling bladerf_sync_config().
unsigned int read_samples_remaining = SAMPLES_PER_BUFFER / 4; status = bladerf_enable_module(dev, BLADERF_MODULE_TX, true);
if (status != 0) {
fprintf(stderr, "Failed to enable TX module: %s\n", bladerf_strerror(status));
goto out;
}
// Keep adding to the buffer until it is full or a failure occurs // Keep writing samples while there is more data to send and no failures have occurred.
while (buffer_samples_remaining > 0 && status == 0 && state != DONE) { while (state != DONE && status == 0) {
size_t samples_populated = 0;
switch(state) { int16_t *tx_buffer_current = tx_buffer;
case INIT: unsigned int buffer_samples_remaining = SAMPLES_PER_BUFFER;
case READ_FILE:
// Read from the input file
if (compressed)
{
int16_t *write_buffer_current = tx_buffer;
samples_read = fread(read_buffer, // if compressed
sizeof(uint8_t), unsigned int read_samples_remaining = SAMPLES_PER_BUFFER / 4;
read_samples_remaining,
fp);
samples_populated = samples_read * 4; // Keep adding to the buffer until it is full or a failure occurs
buffer_samples_remaining = read_samples_remaining * 4; while (buffer_samples_remaining > 0 && status == 0 && state != DONE) {
size_t samples_populated = 0;
// Expand compressed data into TX buffer switch (state) {
for (i=0; i<samples_read; i++) case INIT:
{ case READ_FILE:
memcpy(write_buffer_current, lut[read_buffer[i]], 8); // Read from the input file
if (compressed) {
// Advance the write buffer pointer int16_t *write_buffer_current = tx_buffer;
write_buffer_current += 8;
}
}
else
{
samples_populated = fread(tx_buffer_current,
2 * sizeof(int16_t),
buffer_samples_remaining,
fp);
}
// If the end of the file was reached, pad the rest of the buffer and finish. samples_read = fread(read_buffer,
if (feof(fp)) { sizeof (uint8_t),
state = PAD_TRAILING; read_samples_remaining,
} fp);
// Check for errors
else if (ferror(fp)) {
status = errno;
}
break; samples_populated = samples_read * 4;
buffer_samples_remaining = read_samples_remaining * 4;
case PAD_TRAILING: // Expand compressed data into TX buffer
// Populate the remainder of the buffer with zeros. for (i = 0; i < samples_read; i++) {
memset(tx_buffer_current, 0, buffer_samples_remaining * 2 * sizeof(uint16_t)); memcpy(write_buffer_current, lut[read_buffer[i]], 8);
state = DONE; // Advance the write buffer pointer
break; write_buffer_current += 8;
}
} else {
samples_populated = fread(tx_buffer_current,
2 * sizeof (int16_t),
buffer_samples_remaining,
fp);
}
case DONE: // If the end of the file was reached, pad the rest of the buffer and finish.
default: if (feof(fp)) {
break; state = PAD_TRAILING;
} } // Check for errors
else if (ferror(fp)) {
status = errno;
}
// Advance the buffer pointer. break;
buffer_samples_remaining -= (unsigned int)samples_populated;
tx_buffer_current += (2 * samples_populated);
}
// If there were no errors, transmit the data buffer. case PAD_TRAILING:
if (status == 0) { // Populate the remainder of the buffer with zeros.
bladerf_sync_tx(dev, tx_buffer, SAMPLES_PER_BUFFER, NULL, TIMEOUT_MS); memset(tx_buffer_current, 0, buffer_samples_remaining * 2 * sizeof (uint16_t));
}
}
// Disable TX module, shutting down our underlying TX stream. state = DONE;
status = bladerf_enable_module(dev, BLADERF_MODULE_TX, false); break;
if (status != 0) {
fprintf(stderr, "Failed to disable TX module: %s\n", bladerf_strerror(status));
}
// Free up our resources case DONE:
free(tx_buffer); default:
break;
}
// if compressed // Advance the buffer pointer.
free(read_buffer); buffer_samples_remaining -= (unsigned int) samples_populated;
tx_buffer_current += (2 * samples_populated);
}
// Close TX file // If there were no errors, transmit the data buffer.
fclose(fp); if (status == 0) {
bladerf_sync_tx(dev, tx_buffer, SAMPLES_PER_BUFFER, NULL, TIMEOUT_MS);
}
}
// Disable TX module, shutting down our underlying TX stream.
status = bladerf_enable_module(dev, BLADERF_MODULE_TX, false);
if (status != 0) {
fprintf(stderr, "Failed to disable TX module: %s\n", bladerf_strerror(status));
}
// Free up our resources
free(tx_buffer);
// if compressed
free(read_buffer);
// Close TX file
fclose(fp);
out: out:
printf("Closing device...\n"); printf("Closing device...\n");
bladerf_close(dev); bladerf_close(dev);
return(0); return (0);
} }

View File

@ -1,22 +1,10 @@
#define _CRT_SECURE_NO_WARNINGS
#include <hackrf.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h>
#include <getopt.h> #include <getopt.h>
#include <signal.h>
#include <windows.h> #include <hackrf.h>
#ifdef _WIN64
typedef int64_t ssize_t;
#else
typedef int32_t ssize_t;
#endif
typedef int bool;
#define true 1
#define false 0
static hackrf_device* device = NULL; static hackrf_device* device = NULL;
@ -25,199 +13,188 @@ volatile uint32_t byte_count = 0;
volatile bool do_exit = false; volatile bool do_exit = false;
static transceiver_mode_t transceiver_mode = TRANSCEIVER_MODE_TX;
#define FD_BUFFER_SIZE (8*1024) #define FD_BUFFER_SIZE (8*1024)
#define FREQ_ONE_MHZ (1000000ull) #define FREQ_ONE_MHZ (1000000ull)
BOOL WINAPI sighandler(int signum) static void sighandler(int signum) {
{ fprintf(stdout, "Caught signal %d\n", signum);
if (CTRL_C_EVENT == signum) { do_exit = true;
fprintf(stdout, "Caught signal %d\n", signum);
do_exit = true;
return TRUE;
}
return FALSE;
} }
int tx_callback(hackrf_transfer* transfer) { int tx_callback(hackrf_transfer* transfer) {
size_t bytes_to_read; size_t bytes_to_read;
if( fd != NULL ) if (fd != NULL) {
{ size_t bytes_read;
ssize_t bytes_read; byte_count += transfer->valid_length;
byte_count += transfer->valid_length; bytes_to_read = transfer->valid_length;
bytes_to_read = transfer->valid_length;
bytes_read = fread(transfer->buffer, 1, bytes_to_read, fd);
bytes_read = fread(transfer->buffer, 1, bytes_to_read, fd);
if (bytes_read != bytes_to_read) {
if (bytes_read != bytes_to_read) { return -1; // EOF
return -1; // EOF } else {
} else { return 0;
return 0; }
} } else {
} else { return -1;
return -1; }
}
} }
static void usage() { static void usage() {
fprintf(stderr, "Usage: hackplayer [options]\n" fprintf(stderr, "Usage: hackplayer [options]\n"
" -t <filename> Transmit data from file (required)\n"); " -t <filename> Transmit data from file (required)\n");
return; return;
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
int opt; int opt;
int result; int result;
const char* path = NULL; const char* path = NULL;
uint32_t sample_rate_hz = 2600000; uint32_t sample_rate_hz = 2600000;
uint32_t baseband_filter_bw_hz = 0; uint32_t baseband_filter_bw_hz = 0;
unsigned int txvga_gain=0; unsigned int txvga_gain = 0;
uint64_t freq_hz = 1575420000; uint64_t freq_hz = 1575420000;
uint32_t amp_enable = 1; uint32_t amp_enable = 1;
while( (opt = getopt(argc, argv, "t:")) != EOF ) while ((opt = getopt(argc, argv, "t:")) != EOF) {
{ result = HACKRF_SUCCESS;
result = HACKRF_SUCCESS; switch (opt) {
switch( opt ) case 't':
{ path = optarg;
case 't': break;
path = optarg; default:
break; printf("unknown argument '-%c %s'\n", opt, optarg);
default: usage();
printf("unknown argument '-%c %s'\n", opt, optarg); return EXIT_FAILURE;
usage(); }
return EXIT_FAILURE;
}
if( result != HACKRF_SUCCESS ) { if (result != HACKRF_SUCCESS) {
printf("argument error: '-%c %s' %s (%d)\n", opt, optarg, hackrf_error_name(result), result); printf("argument error: '-%c %s' %s (%d)\n", opt, optarg, hackrf_error_name(result), result);
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
if( path == NULL ) { if (path == NULL) {
printf("specify a path to a file to transmit\n"); printf("specify a path to a file to transmit\n");
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// Compute default value depending on sample rate // Compute default value depending on sample rate
baseband_filter_bw_hz = hackrf_compute_baseband_filter_bw_round_down_lt(sample_rate_hz); baseband_filter_bw_hz = hackrf_compute_baseband_filter_bw_round_down_lt(sample_rate_hz);
result = hackrf_init(); result = hackrf_init();
if( result != HACKRF_SUCCESS ) { if (result != HACKRF_SUCCESS) {
printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result);
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
result = hackrf_open_by_serial(NULL, &device); result = hackrf_open_by_serial(NULL, &device);
if( result != HACKRF_SUCCESS ) { if (result != HACKRF_SUCCESS) {
printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result);
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
fd = fopen(path, "rb"); fd = fopen(path, "rb");
if( fd == NULL ) { if (fd == NULL) {
printf("Failed to open file: %s\n", path); printf("Failed to open file: %s\n", path);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// Change fd buffer to have bigger one to store or read data on/to HDD // Change fd buffer to have bigger one to store or read data on/to HDD
result = setvbuf(fd , NULL , _IOFBF , FD_BUFFER_SIZE); result = setvbuf(fd, NULL, _IOFBF, FD_BUFFER_SIZE);
if( result != 0 ) { if (result != 0) {
printf("setvbuf() failed: %d\n", result); printf("setvbuf() failed: %d\n", result);
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE ); signal(SIGINT, sighandler);
printf("call hackrf_sample_rate_set(%.03f MHz)\n", ((float)sample_rate_hz/(float)FREQ_ONE_MHZ)); printf("call hackrf_sample_rate_set(%.03f MHz)\n", ((float) sample_rate_hz / (float) FREQ_ONE_MHZ));
result = hackrf_set_sample_rate_manual(device, sample_rate_hz, 1); result = hackrf_set_sample_rate_manual(device, sample_rate_hz, 1);
if( result != HACKRF_SUCCESS ) { if (result != HACKRF_SUCCESS) {
printf("hackrf_sample_rate_set() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_sample_rate_set() failed: %s (%d)\n", hackrf_error_name(result), result);
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
printf("call hackrf_baseband_filter_bandwidth_set(%.03f MHz)\n", printf("call hackrf_baseband_filter_bandwidth_set(%.03f MHz)\n",
((float)baseband_filter_bw_hz/(float)FREQ_ONE_MHZ)); ((float) baseband_filter_bw_hz / (float) FREQ_ONE_MHZ));
result = hackrf_set_baseband_filter_bandwidth(device, baseband_filter_bw_hz); result = hackrf_set_baseband_filter_bandwidth(device, baseband_filter_bw_hz);
if( result != HACKRF_SUCCESS ) { if (result != HACKRF_SUCCESS) {
printf("hackrf_baseband_filter_bandwidth_set() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_baseband_filter_bandwidth_set() failed: %s (%d)\n", hackrf_error_name(result), result);
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
result = hackrf_set_txvga_gain(device, txvga_gain); result = hackrf_set_txvga_gain(device, txvga_gain);
result |= hackrf_start_tx(device, tx_callback, NULL); result |= hackrf_start_tx(device, tx_callback, NULL);
if( result != HACKRF_SUCCESS ) { if (result != HACKRF_SUCCESS) {
printf("hackrf_start_?x() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_start_?x() failed: %s (%d)\n", hackrf_error_name(result), result);
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
printf("call hackrf_set_freq(%.03f MHz)\n", ((double)freq_hz/(double)FREQ_ONE_MHZ)); printf("call hackrf_set_freq(%.03f MHz)\n", ((double) freq_hz / (double) FREQ_ONE_MHZ));
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);
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
printf("call hackrf_set_amp_enable(%u)\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);
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
printf("Stop with Ctrl-C\n"); printf("Stop with Ctrl-C\n");
while( (hackrf_is_streaming(device) == HACKRF_TRUE) && (do_exit == false) ) { while ((hackrf_is_streaming(device) == HACKRF_TRUE) && (do_exit == false)) {
// Show something? // Show something?
} }
result = hackrf_is_streaming(device); result = hackrf_is_streaming(device);
if (do_exit) { if (do_exit) {
printf("\nUser cancel, exiting...\n"); printf("\nUser cancel, exiting...\n");
} else { } else {
printf("\nExiting... hackrf_is_streaming() result: %s (%d)\n", hackrf_error_name(result), result); printf("\nExiting... hackrf_is_streaming() result: %s (%d)\n", hackrf_error_name(result), result);
} }
if(device != NULL) { if (device != NULL) {
result = hackrf_stop_tx(device); result = hackrf_stop_tx(device);
if( result != HACKRF_SUCCESS ) { if (result != HACKRF_SUCCESS) {
printf("hackrf_stop_tx() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_stop_tx() failed: %s (%d)\n", hackrf_error_name(result), result);
} else { } else {
printf("hackrf_stop_tx() done\n"); printf("hackrf_stop_tx() done\n");
} }
result = hackrf_close(device); result = hackrf_close(device);
if( result != HACKRF_SUCCESS ) if (result != HACKRF_SUCCESS) {
{ printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result);
printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); } else {
} else { printf("hackrf_close() done\n");
printf("hackrf_close() done\n"); }
}
hackrf_exit();
printf("hackrf_exit() done\n");
}
if(fd != NULL) { hackrf_exit();
fclose(fd); printf("hackrf_exit() done\n");
fd = NULL; }
printf("fclose(fd) done\n");
}
printf("exit\n"); if (fd != NULL) {
return EXIT_SUCCESS; fclose(fd);
fd = NULL;
printf("fclose(fd) done\n");
}
printf("exit\n");
return EXIT_SUCCESS;
} }