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,8 +20,7 @@
#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"
@ -33,15 +29,18 @@ void usage(void)
return; return;
} }
int main(int argc, char *argv[])
{ int main(int argc, char *argv[]) {
int status; int status;
char *devstr = NULL; char *devstr = NULL;
struct bladerf *dev = NULL; struct bladerf *dev = NULL;
FILE *fp; FILE *fp;
int16_t *tx_buffer; int16_t *tx_buffer;
enum state {INIT, READ_FILE, PAD_TRAILING, DONE};
enum state {
INIT, READ_FILE, PAD_TRAILING, DONE
};
enum state state = INIT; enum state state = INIT;
int compressed = 0; int compressed = 0;
@ -49,7 +48,7 @@ int main(int argc, char *argv[])
size_t samples_read; size_t samples_read;
int16_t lut[256][8]; int16_t lut[256][8];
int16_t amp = AMPLITUDE; int16_t amp = AMPLITUDE;
int i,k; uint32_t i, k;
int gain = TX_VGA1; int gain = TX_VGA1;
int result; int result;
@ -59,31 +58,26 @@ int main(int argc, char *argv[])
// Empty TX file name // Empty TX file name
txfile[0] = 0; txfile[0] = 0;
if (argc<3) { if (argc < 3) {
usage(); usage();
exit(1); exit(1);
} }
while ((result=getopt(argc,argv,"g:b:f:"))!=-1) while ((result = getopt(argc, argv, "g:b:f:")) != -1) {
{ switch (result) {
switch (result)
{
case 'g': case 'g':
gain = atoi(optarg); gain = atoi(optarg);
if (gain>-4 || gain<-35) if (gain>-4 || gain<-35) {
{
printf("ERROR: Invalid TX VGA1 gain.\n"); printf("ERROR: Invalid TX VGA1 gain.\n");
exit(1); exit(1);
} }
break; break;
case 'b': case 'b':
data_format = atoi(optarg); data_format = atoi(optarg);
if (data_format!=1 && data_format!=16) if (data_format != 1 && data_format != 16) {
{
printf("ERROR: Invalid I/Q data format.\n"); printf("ERROR: Invalid I/Q data format.\n");
exit(1); exit(1);
} } else if (data_format == 1)
else if (data_format==1)
compressed = 1; compressed = 1;
break; break;
case 'f': case 'f':
@ -99,15 +93,14 @@ int main(int argc, char *argv[])
} }
// Open TX file. // Open TX file.
if (txfile[0]==0) if (txfile[0] == 0) {
{
printf("ERROR: I/Q sampling data file is not specified.\n"); printf("ERROR: I/Q sampling data file is not specified.\n");
exit(1); exit(1);
} }
fp = fopen(txfile, "rb"); fp = fopen(txfile, "rb");
if (fp==NULL) { if (fp == NULL) {
fprintf(stderr, "ERROR: Failed to open TX file: %s\n", argv[1]); fprintf(stderr, "ERROR: Failed to open TX file: %s\n", argv[1]);
exit(1); exit(1);
} }
@ -134,8 +127,7 @@ int main(int argc, char *argv[])
if (status != 0) { if (status != 0) {
fprintf(stderr, "Failed to set TX sample rate: %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 sample rate: %u sps\n", TX_SAMPLERATE);
} }
@ -143,8 +135,7 @@ int main(int argc, char *argv[])
if (status != 0) { if (status != 0) {
fprintf(stderr, "Failed to set TX bandwidth: %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 bandwidth: %u Hz\n", TX_BANDWIDTH);
} }
@ -152,8 +143,7 @@ int main(int argc, char *argv[])
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 VGA1 gain: %s\n", bladerf_strerror(status));
goto out; goto out;
} } else {
else {
printf("TX VGA1 gain: %d dB\n", gain); printf("TX VGA1 gain: %d dB\n", gain);
} }
@ -161,8 +151,7 @@ int main(int argc, char *argv[])
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 VGA2 gain: %s\n", bladerf_strerror(status));
goto out; goto out;
} } else {
else {
printf("TX VGA2 gain: %d dB\n", TX_VGA2); printf("TX VGA2 gain: %d dB\n", TX_VGA2);
} }
@ -170,7 +159,7 @@ int main(int argc, char *argv[])
printf("Running...\n"); printf("Running...\n");
// Allocate a buffer to hold each block of samples to transmit. // Allocate a buffer to hold each block of samples to transmit.
tx_buffer = (int16_t*)malloc(SAMPLES_PER_BUFFER * 2 * sizeof(int16_t)); tx_buffer = (int16_t*) malloc(SAMPLES_PER_BUFFER * 2 * sizeof (int16_t));
if (tx_buffer == NULL) { if (tx_buffer == NULL) {
fprintf(stderr, "Failed to allocate TX buffer.\n"); fprintf(stderr, "Failed to allocate TX buffer.\n");
@ -178,17 +167,16 @@ int main(int argc, char *argv[])
} }
// if compressed // if compressed
read_buffer = (uint8_t*)malloc(SAMPLES_PER_BUFFER / 4); read_buffer = (uint8_t*) malloc(SAMPLES_PER_BUFFER / 4);
if (read_buffer == NULL) { if (read_buffer == NULL) {
fprintf(stderr, "Failed to allocate read buffer.\n"); fprintf(stderr, "Failed to allocate read buffer.\n");
goto out; goto out;
} }
for (i=0; i<256; i++) for (i = 0; i < 256; i++) {
{ for (k = 0; k < 8; k++)
for (k=0; k<8; k++) lut[i][k] = ((i >> (7 - k))&0x1) ? amp : -amp;
lut[i][k] = ((i>>(7-k))&0x1)?amp:-amp;
} }
// Configure the TX module for use with the synchronous interface. // Configure the TX module for use with the synchronous interface.
@ -225,16 +213,15 @@ int main(int argc, char *argv[])
while (buffer_samples_remaining > 0 && status == 0 && state != DONE) { while (buffer_samples_remaining > 0 && status == 0 && state != DONE) {
size_t samples_populated = 0; size_t samples_populated = 0;
switch(state) { switch (state) {
case INIT: case INIT:
case READ_FILE: case READ_FILE:
// Read from the input file // Read from the input file
if (compressed) if (compressed) {
{
int16_t *write_buffer_current = tx_buffer; int16_t *write_buffer_current = tx_buffer;
samples_read = fread(read_buffer, samples_read = fread(read_buffer,
sizeof(uint8_t), sizeof (uint8_t),
read_samples_remaining, read_samples_remaining,
fp); fp);
@ -242,18 +229,15 @@ int main(int argc, char *argv[])
buffer_samples_remaining = read_samples_remaining * 4; buffer_samples_remaining = read_samples_remaining * 4;
// Expand compressed data into TX buffer // Expand compressed data into TX buffer
for (i=0; i<samples_read; i++) for (i = 0; i < samples_read; i++) {
{
memcpy(write_buffer_current, lut[read_buffer[i]], 8); memcpy(write_buffer_current, lut[read_buffer[i]], 8);
// Advance the write buffer pointer // Advance the write buffer pointer
write_buffer_current += 8; write_buffer_current += 8;
} }
} } else {
else
{
samples_populated = fread(tx_buffer_current, samples_populated = fread(tx_buffer_current,
2 * sizeof(int16_t), 2 * sizeof (int16_t),
buffer_samples_remaining, buffer_samples_remaining,
fp); fp);
} }
@ -261,8 +245,7 @@ int main(int argc, char *argv[])
// If the end of the file was reached, pad the rest of the buffer and finish. // If the end of the file was reached, pad the rest of the buffer and finish.
if (feof(fp)) { if (feof(fp)) {
state = PAD_TRAILING; state = PAD_TRAILING;
} } // Check for errors
// Check for errors
else if (ferror(fp)) { else if (ferror(fp)) {
status = errno; status = errno;
} }
@ -271,7 +254,7 @@ int main(int argc, char *argv[])
case PAD_TRAILING: case PAD_TRAILING:
// Populate the remainder of the buffer with zeros. // Populate the remainder of the buffer with zeros.
memset(tx_buffer_current, 0, buffer_samples_remaining * 2 * sizeof(uint16_t)); memset(tx_buffer_current, 0, buffer_samples_remaining * 2 * sizeof (uint16_t));
state = DONE; state = DONE;
break; break;
@ -282,7 +265,7 @@ int main(int argc, char *argv[])
} }
// Advance the buffer pointer. // Advance the buffer pointer.
buffer_samples_remaining -= (unsigned int)samples_populated; buffer_samples_remaining -= (unsigned int) samples_populated;
tx_buffer_current += (2 * samples_populated); tx_buffer_current += (2 * samples_populated);
} }
@ -311,5 +294,5 @@ 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,27 +13,19 @@ 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) {
{
if (CTRL_C_EVENT == signum) {
fprintf(stdout, "Caught signal %d\n", signum); fprintf(stdout, "Caught signal %d\n", signum);
do_exit = true; 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;
@ -74,15 +54,13 @@ int main(int argc, char** argv) {
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': case 't':
path = optarg; path = optarg;
break; break;
@ -92,14 +70,14 @@ int main(int argc, char** argv) {
return EXIT_FAILURE; 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;
@ -109,47 +87,47 @@ int main(int argc, char** argv) {
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;
@ -158,30 +136,30 @@ int main(int argc, char** argv) {
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?
} }
@ -192,17 +170,16 @@ int main(int argc, char** argv) {
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");
@ -212,7 +189,7 @@ int main(int argc, char** argv) {
printf("hackrf_exit() done\n"); printf("hackrf_exit() done\n");
} }
if(fd != NULL) { if (fd != NULL) {
fclose(fd); fclose(fd);
fd = NULL; fd = NULL;
printf("fclose(fd) done\n"); printf("fclose(fd) done\n");