From 48f52a3fede43b6a0ef7784e01d3947bfdab7a06 Mon Sep 17 00:00:00 2001 From: Mictronics Date: Tue, 6 Mar 2018 20:15:22 +0100 Subject: [PATCH] Fix to build Linux. Tested on Debian 9. --- player/bladeplayer.c | 473 +++++++++++++++++++++---------------------- player/hackplayer.c | 329 ++++++++++++++---------------- 2 files changed, 381 insertions(+), 421 deletions(-) diff --git a/player/bladeplayer.c b/player/bladeplayer.c index a04bf09..afe57a7 100644 --- a/player/bladeplayer.c +++ b/player/bladeplayer.c @@ -4,11 +4,8 @@ #include #include #include -#ifdef _WIN32 #include "getopt.h" -#else -#include -#endif +#include #define TX_FREQUENCY 1575420000 #define TX_SAMPLERATE 2600000 @@ -23,293 +20,279 @@ #define AMPLITUDE (1000) // Default amplitude for 12-bit I/Q -void usage(void) -{ - fprintf(stderr, "Usage: bladeplayer [options]\n" - " -f I/Q sampling data file (required)\n" - " -b I/Q data format [1/16] (default: 16)\n" - " -g TX VGA1 gain (default: %d)\n", - TX_VGA1); +void usage(void) { + fprintf(stderr, "Usage: bladeplayer [options]\n" + " -f I/Q sampling data file (required)\n" + " -b I/Q data format [1/16] (default: 16)\n" + " -g TX VGA1 gain (default: %d)\n", + TX_VGA1); - return; + return; } -int main(int argc, char *argv[]) -{ - int status; - char *devstr = NULL; - struct bladerf *dev = NULL; - FILE *fp; - int16_t *tx_buffer; - enum state {INIT, READ_FILE, PAD_TRAILING, DONE}; - enum state state = INIT; +int main(int argc, char *argv[]) { + int status; + char *devstr = NULL; + struct bladerf *dev = NULL; - int compressed = 0; - uint8_t *read_buffer; - size_t samples_read; - int16_t lut[256][8]; - int16_t amp = AMPLITUDE; - int i,k; + FILE *fp; + int16_t *tx_buffer; - int gain = TX_VGA1; - int result; - int data_format; - char txfile[128]; + enum state { + INIT, READ_FILE, PAD_TRAILING, DONE + }; + enum state state = INIT; - // Empty TX file name - txfile[0] = 0; + int compressed = 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) { - usage(); - exit(1); - } + int gain = TX_VGA1; + int result; + int data_format; + char txfile[128]; - 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; - } - } + // Empty TX file name + txfile[0] = 0; - // Open TX file. - if (txfile[0]==0) - { - printf("ERROR: I/Q sampling data file is not specified.\n"); - exit(1); - } + if (argc < 3) { + usage(); + 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) { - fprintf(stderr, "ERROR: Failed to open TX file: %s\n", argv[1]); - exit(1); - } + // Open TX file. + if (txfile[0] == 0) { + printf("ERROR: I/Q sampling data file is not specified.\n"); + exit(1); + } - // Initializing device. - printf("Opening and initializing device...\n"); + fp = fopen(txfile, "rb"); - status = bladerf_open(&dev, devstr); - if (status != 0) { - fprintf(stderr, "Failed to open device: %s\n", bladerf_strerror(status)); - goto out; - } + if (fp == NULL) { + fprintf(stderr, "ERROR: Failed to open TX file: %s\n", argv[1]); + exit(1); + } - status = bladerf_set_frequency(dev, BLADERF_MODULE_TX, TX_FREQUENCY); - if (status != 0) { - fprintf(stderr, "Faield to set TX frequency: %s\n", bladerf_strerror(status)); - goto out; - } - else { - printf("TX frequency: %u Hz\n", TX_FREQUENCY); - } + // Initializing device. + printf("Opening and initializing device...\n"); - status = bladerf_set_sample_rate(dev, BLADERF_MODULE_TX, TX_SAMPLERATE, NULL); - if (status != 0) { - fprintf(stderr, "Failed to set TX sample rate: %s\n", bladerf_strerror(status)); - goto out; - } - else { - printf("TX sample rate: %u sps\n", TX_SAMPLERATE); - } + status = bladerf_open(&dev, devstr); + if (status != 0) { + fprintf(stderr, "Failed to open device: %s\n", bladerf_strerror(status)); + goto out; + } - status = bladerf_set_bandwidth(dev, BLADERF_MODULE_TX, TX_BANDWIDTH, NULL); - if (status != 0) { - fprintf(stderr, "Failed to set TX bandwidth: %s\n", bladerf_strerror(status)); - goto out; - } - else { - printf("TX bandwidth: %u Hz\n", TX_BANDWIDTH); - } + status = bladerf_set_frequency(dev, BLADERF_MODULE_TX, TX_FREQUENCY); + if (status != 0) { + 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_txvga1(dev, gain); - 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); - } + status = bladerf_set_sample_rate(dev, BLADERF_MODULE_TX, TX_SAMPLERATE, NULL); + if (status != 0) { + fprintf(stderr, "Failed to set TX sample rate: %s\n", bladerf_strerror(status)); + goto out; + } else { + printf("TX sample rate: %u sps\n", TX_SAMPLERATE); + } - status = bladerf_set_txvga2(dev, TX_VGA2); - if (status != 0) { - fprintf(stderr, "Failed to set TX VGA2 gain: %s\n", bladerf_strerror(status)); - goto out; - } - else { - printf("TX VGA2 gain: %d dB\n", TX_VGA2); - } + status = bladerf_set_bandwidth(dev, BLADERF_MODULE_TX, TX_BANDWIDTH, NULL); + if (status != 0) { + fprintf(stderr, "Failed to set TX bandwidth: %s\n", bladerf_strerror(status)); + goto out; + } else { + printf("TX bandwidth: %u Hz\n", TX_BANDWIDTH); + } - // Application code goes here. - printf("Running...\n"); + status = bladerf_set_txvga1(dev, gain); + 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. - tx_buffer = (int16_t*)malloc(SAMPLES_PER_BUFFER * 2 * sizeof(int16_t)); - - if (tx_buffer == NULL) { - fprintf(stderr, "Failed to allocate TX buffer.\n"); - goto out; - } + status = bladerf_set_txvga2(dev, TX_VGA2); + if (status != 0) { + fprintf(stderr, "Failed to set TX VGA2 gain: %s\n", bladerf_strerror(status)); + goto out; + } else { + printf("TX VGA2 gain: %d dB\n", TX_VGA2); + } - // if compressed - read_buffer = (uint8_t*)malloc(SAMPLES_PER_BUFFER / 4); + // Application code goes here. + printf("Running...\n"); - if (read_buffer == NULL) { - fprintf(stderr, "Failed to allocate read buffer.\n"); - goto out; - } + // Allocate a buffer to hold each block of samples to transmit. + tx_buffer = (int16_t*) malloc(SAMPLES_PER_BUFFER * 2 * sizeof (int16_t)); - for (i=0; i<256; i++) - { - for (k=0; k<8; k++) - lut[i][k] = ((i>>(7-k))&0x1)?amp:-amp; - } + if (tx_buffer == NULL) { + fprintf(stderr, "Failed to allocate TX buffer.\n"); + goto out; + } - // Configure the TX module for use with the synchronous interface. - status = bladerf_sync_config(dev, - BLADERF_MODULE_TX, - BLADERF_FORMAT_SC16_Q11, - NUM_BUFFERS, - SAMPLES_PER_BUFFER, - NUM_TRANSFERS, - TIMEOUT_MS); + // if compressed + read_buffer = (uint8_t*) malloc(SAMPLES_PER_BUFFER / 4); - if (status != 0) { - fprintf(stderr, "Failed to configure TX sync interface: %s\n", bladerf_strerror(status)); - goto out; - } + if (read_buffer == NULL) { + fprintf(stderr, "Failed to allocate read buffer.\n"); + goto out; + } - // We must always enable the modules *after* calling bladerf_sync_config(). - 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; - } + for (i = 0; i < 256; i++) { + for (k = 0; k < 8; k++) + lut[i][k] = ((i >> (7 - k))&0x1) ? amp : -amp; + } - // Keep writing samples while there is more data to send and no failures have occurred. - while (state != DONE && status == 0) { + // Configure the TX module for use with the synchronous interface. + 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; - unsigned int buffer_samples_remaining = SAMPLES_PER_BUFFER; + if (status != 0) { + fprintf(stderr, "Failed to configure TX sync interface: %s\n", bladerf_strerror(status)); + goto out; + } - // if compressed - unsigned int read_samples_remaining = SAMPLES_PER_BUFFER / 4; + // We must always enable the modules *after* calling bladerf_sync_config(). + 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 - while (buffer_samples_remaining > 0 && status == 0 && state != DONE) { - size_t samples_populated = 0; + // Keep writing samples while there is more data to send and no failures have occurred. + while (state != DONE && status == 0) { - switch(state) { - case INIT: - case READ_FILE: - // Read from the input file - if (compressed) - { - int16_t *write_buffer_current = tx_buffer; + int16_t *tx_buffer_current = tx_buffer; + unsigned int buffer_samples_remaining = SAMPLES_PER_BUFFER; - samples_read = fread(read_buffer, - sizeof(uint8_t), - read_samples_remaining, - fp); + // if compressed + unsigned int read_samples_remaining = SAMPLES_PER_BUFFER / 4; - samples_populated = samples_read * 4; - buffer_samples_remaining = read_samples_remaining * 4; + // Keep adding to the buffer until it is full or a failure occurs + while (buffer_samples_remaining > 0 && status == 0 && state != DONE) { + size_t samples_populated = 0; - // Expand compressed data into TX buffer - for (i=0; i - #include #include +#include +#include #include - -#include - -#ifdef _WIN64 -typedef int64_t ssize_t; -#else -typedef int32_t ssize_t; -#endif - -typedef int bool; -#define true 1 -#define false 0 +#include +#include static hackrf_device* device = NULL; @@ -25,199 +13,188 @@ volatile uint32_t byte_count = 0; volatile bool do_exit = false; -static transceiver_mode_t transceiver_mode = TRANSCEIVER_MODE_TX; - #define FD_BUFFER_SIZE (8*1024) #define FREQ_ONE_MHZ (1000000ull) -BOOL WINAPI sighandler(int signum) -{ - if (CTRL_C_EVENT == signum) { - fprintf(stdout, "Caught signal %d\n", signum); - do_exit = true; - return TRUE; - } - return FALSE; +static void sighandler(int signum) { + fprintf(stdout, "Caught signal %d\n", signum); + do_exit = true; } int tx_callback(hackrf_transfer* transfer) { - size_t bytes_to_read; + size_t bytes_to_read; - if( fd != NULL ) - { - ssize_t bytes_read; - byte_count += transfer->valid_length; - bytes_to_read = transfer->valid_length; - - bytes_read = fread(transfer->buffer, 1, bytes_to_read, fd); - - if (bytes_read != bytes_to_read) { - return -1; // EOF - } else { - return 0; - } - } else { - return -1; - } + if (fd != NULL) { + size_t bytes_read; + byte_count += transfer->valid_length; + bytes_to_read = transfer->valid_length; + + bytes_read = fread(transfer->buffer, 1, bytes_to_read, fd); + + if (bytes_read != bytes_to_read) { + return -1; // EOF + } else { + return 0; + } + } else { + return -1; + } } static void usage() { - fprintf(stderr, "Usage: hackplayer [options]\n" - " -t Transmit data from file (required)\n"); + fprintf(stderr, "Usage: hackplayer [options]\n" + " -t Transmit data from file (required)\n"); - return; + return; } int main(int argc, char** argv) { - int opt; - int result; - const char* path = NULL; - uint32_t sample_rate_hz = 2600000; - uint32_t baseband_filter_bw_hz = 0; - unsigned int txvga_gain=0; - uint64_t freq_hz = 1575420000; - uint32_t amp_enable = 1; + int opt; + int result; + const char* path = NULL; + uint32_t sample_rate_hz = 2600000; + uint32_t baseband_filter_bw_hz = 0; + unsigned int txvga_gain = 0; + uint64_t freq_hz = 1575420000; + uint32_t amp_enable = 1; - while( (opt = getopt(argc, argv, "t:")) != EOF ) - { - result = HACKRF_SUCCESS; - switch( opt ) - { - case 't': - path = optarg; - break; - default: - printf("unknown argument '-%c %s'\n", opt, optarg); - usage(); - return EXIT_FAILURE; - } + while ((opt = getopt(argc, argv, "t:")) != EOF) { + result = HACKRF_SUCCESS; + switch (opt) { + case 't': + path = optarg; + break; + default: + printf("unknown argument '-%c %s'\n", opt, optarg); + usage(); + return EXIT_FAILURE; + } - if( result != HACKRF_SUCCESS ) { - printf("argument error: '-%c %s' %s (%d)\n", opt, optarg, hackrf_error_name(result), result); - usage(); - return EXIT_FAILURE; - } - } + if (result != HACKRF_SUCCESS) { + printf("argument error: '-%c %s' %s (%d)\n", opt, optarg, hackrf_error_name(result), result); + usage(); + return EXIT_FAILURE; + } + } - if( path == NULL ) { - printf("specify a path to a file to transmit\n"); - usage(); - return EXIT_FAILURE; - } + if (path == NULL) { + printf("specify a path to a file to transmit\n"); + usage(); + return EXIT_FAILURE; + } - // Compute default value depending on sample rate - baseband_filter_bw_hz = hackrf_compute_baseband_filter_bw_round_down_lt(sample_rate_hz); + // Compute default value depending on sample rate + baseband_filter_bw_hz = hackrf_compute_baseband_filter_bw_round_down_lt(sample_rate_hz); - result = hackrf_init(); - if( result != HACKRF_SUCCESS ) { - printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); - usage(); - return EXIT_FAILURE; - } + result = hackrf_init(); + if (result != HACKRF_SUCCESS) { + printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); + usage(); + return EXIT_FAILURE; + } - result = hackrf_open_by_serial(NULL, &device); - if( result != HACKRF_SUCCESS ) { - printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); - usage(); - return EXIT_FAILURE; - } + result = hackrf_open_by_serial(NULL, &device); + if (result != HACKRF_SUCCESS) { + printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); + usage(); + return EXIT_FAILURE; + } - fd = fopen(path, "rb"); - if( fd == NULL ) { - printf("Failed to open file: %s\n", path); - return EXIT_FAILURE; - } + fd = fopen(path, "rb"); + if (fd == NULL) { + printf("Failed to open file: %s\n", path); + return EXIT_FAILURE; + } - // Change fd buffer to have bigger one to store or read data on/to HDD - result = setvbuf(fd , NULL , _IOFBF , FD_BUFFER_SIZE); - if( result != 0 ) { - printf("setvbuf() failed: %d\n", result); - usage(); - return EXIT_FAILURE; - } + // Change fd buffer to have bigger one to store or read data on/to HDD + result = setvbuf(fd, NULL, _IOFBF, FD_BUFFER_SIZE); + if (result != 0) { + printf("setvbuf() failed: %d\n", result); + usage(); + 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)); - result = hackrf_set_sample_rate_manual(device, sample_rate_hz, 1); - if( result != HACKRF_SUCCESS ) { - printf("hackrf_sample_rate_set() failed: %s (%d)\n", hackrf_error_name(result), result); - usage(); - return EXIT_FAILURE; - } + 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); + if (result != HACKRF_SUCCESS) { + printf("hackrf_sample_rate_set() failed: %s (%d)\n", hackrf_error_name(result), result); + usage(); + return EXIT_FAILURE; + } - printf("call hackrf_baseband_filter_bandwidth_set(%.03f MHz)\n", - ((float)baseband_filter_bw_hz/(float)FREQ_ONE_MHZ)); - result = hackrf_set_baseband_filter_bandwidth(device, baseband_filter_bw_hz); - if( result != HACKRF_SUCCESS ) { - printf("hackrf_baseband_filter_bandwidth_set() failed: %s (%d)\n", hackrf_error_name(result), result); - usage(); - return EXIT_FAILURE; - } + printf("call hackrf_baseband_filter_bandwidth_set(%.03f MHz)\n", + ((float) baseband_filter_bw_hz / (float) FREQ_ONE_MHZ)); + result = hackrf_set_baseband_filter_bandwidth(device, baseband_filter_bw_hz); + if (result != HACKRF_SUCCESS) { + printf("hackrf_baseband_filter_bandwidth_set() failed: %s (%d)\n", hackrf_error_name(result), result); + usage(); + return EXIT_FAILURE; + } - result = hackrf_set_txvga_gain(device, txvga_gain); - result |= hackrf_start_tx(device, tx_callback, NULL); + result = hackrf_set_txvga_gain(device, txvga_gain); + result |= hackrf_start_tx(device, tx_callback, NULL); - if( result != HACKRF_SUCCESS ) { - printf("hackrf_start_?x() failed: %s (%d)\n", hackrf_error_name(result), result); - usage(); - return EXIT_FAILURE; - } + if (result != HACKRF_SUCCESS) { + printf("hackrf_start_?x() failed: %s (%d)\n", hackrf_error_name(result), result); + usage(); + return EXIT_FAILURE; + } - printf("call hackrf_set_freq(%.03f MHz)\n", ((double)freq_hz/(double)FREQ_ONE_MHZ)); - result = hackrf_set_freq(device, freq_hz); - if( result != HACKRF_SUCCESS ) { - printf("hackrf_set_freq() failed: %s (%d)\n", hackrf_error_name(result), result); - usage(); - return EXIT_FAILURE; - } + printf("call hackrf_set_freq(%.03f MHz)\n", ((double) freq_hz / (double) FREQ_ONE_MHZ)); + result = hackrf_set_freq(device, freq_hz); + if (result != HACKRF_SUCCESS) { + printf("hackrf_set_freq() failed: %s (%d)\n", hackrf_error_name(result), result); + usage(); + return EXIT_FAILURE; + } - printf("call hackrf_set_amp_enable(%u)\n", amp_enable); - result = hackrf_set_amp_enable(device, (uint8_t)amp_enable); - if( result != HACKRF_SUCCESS ) { - printf("hackrf_set_amp_enable() failed: %s (%d)\n", hackrf_error_name(result), result); - usage(); - return EXIT_FAILURE; - } + printf("call hackrf_set_amp_enable(%u)\n", amp_enable); + result = hackrf_set_amp_enable(device, (uint8_t) amp_enable); + if (result != HACKRF_SUCCESS) { + printf("hackrf_set_amp_enable() failed: %s (%d)\n", hackrf_error_name(result), result); + usage(); + return EXIT_FAILURE; + } - printf("Stop with Ctrl-C\n"); - while( (hackrf_is_streaming(device) == HACKRF_TRUE) && (do_exit == false) ) { - // Show something? - } + printf("Stop with Ctrl-C\n"); + while ((hackrf_is_streaming(device) == HACKRF_TRUE) && (do_exit == false)) { + // Show something? + } - result = hackrf_is_streaming(device); - if (do_exit) { - printf("\nUser cancel, exiting...\n"); - } else { - printf("\nExiting... hackrf_is_streaming() result: %s (%d)\n", hackrf_error_name(result), result); - } + result = hackrf_is_streaming(device); + if (do_exit) { + printf("\nUser cancel, exiting...\n"); + } else { + printf("\nExiting... hackrf_is_streaming() result: %s (%d)\n", hackrf_error_name(result), result); + } - if(device != NULL) { - result = hackrf_stop_tx(device); - if( result != HACKRF_SUCCESS ) { - printf("hackrf_stop_tx() failed: %s (%d)\n", hackrf_error_name(result), result); - } else { - printf("hackrf_stop_tx() done\n"); - } + if (device != NULL) { + result = hackrf_stop_tx(device); + if (result != HACKRF_SUCCESS) { + printf("hackrf_stop_tx() failed: %s (%d)\n", hackrf_error_name(result), result); + } else { + printf("hackrf_stop_tx() done\n"); + } - result = hackrf_close(device); - if( result != HACKRF_SUCCESS ) - { - printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); - } else { - printf("hackrf_close() done\n"); - } - - hackrf_exit(); - printf("hackrf_exit() done\n"); - } + result = hackrf_close(device); + if (result != HACKRF_SUCCESS) { + printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); + } else { + printf("hackrf_close() done\n"); + } - if(fd != NULL) { - fclose(fd); - fd = NULL; - printf("fclose(fd) done\n"); - } + hackrf_exit(); + printf("hackrf_exit() done\n"); + } - printf("exit\n"); - return EXIT_SUCCESS; + if (fd != NULL) { + fclose(fd); + fd = NULL; + printf("fclose(fd) done\n"); + } + + printf("exit\n"); + return EXIT_SUCCESS; }