Skip to content

Instantly share code, notes, and snippets.

@rxseger
Created June 9, 2016 05:01
Show Gist options
  • Save rxseger/71f62fcdd764fee0132ac96f725d390e to your computer and use it in GitHub Desktop.
Save rxseger/71f62fcdd764fee0132ac96f725d390e to your computer and use it in GitHub Desktop.
dump1090_sdrplus HackRF patch
from https://github.com/itemir/dump1090_sdrplus/releases/tag/HackRF_One
diff --git a/dump1090.c b/dump1090.c
index 7fa5327..01cea88 100644
--- a/dump1090.c
+++ b/dump1090.c
@@ -2,6 +2,8 @@
*
* Copyright (C) 2012 by Salvatore Sanfilippo <antirez@gmail.com>
*
+ * HackRF One support added by Ilker Temir <ilker@ilkertemir.com>
+ *
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -44,6 +46,7 @@
#include <sys/ioctl.h>
#include <sys/select.h>
#include "rtl-sdr.h"
+#include "libhackrf/hackrf.h"
#include "anet.h"
#define MODES_DEFAULT_RATE 2000000
@@ -54,6 +57,11 @@
#define MODES_DATA_LEN (16*16384) /* 256k */
#define MODES_AUTO_GAIN -100 /* Use automatic gain. */
#define MODES_MAX_GAIN 999999 /* Use max available gain. */
+/* HackRF One Defaults */
+#define MODES_ENABLE_AMP 0
+#define MODES_LNA_GAIN 32
+#define MODES_VGA_GAIN 48
+
#define MODES_PREAMBLE_US 8 /* microseconds */
#define MODES_LONG_MSG_BITS 112
@@ -138,10 +146,20 @@ struct {
int exit; /* Exit from the main loop when true. */
/* RTLSDR */
+ int rtl_enabled;
int dev_index;
int gain;
int enable_agc;
rtlsdr_dev_t *dev;
+
+ /* HackRF One*/
+ int hackrf_enabled;
+ int enable_amp;
+ int lna_gain;
+ int vga_gain;
+ hackrf_device *hackrf;
+
+ /* SDR Common */
int freq;
/* Networking */
@@ -260,6 +278,9 @@ void modesInitConfig(void) {
Modes.gain = MODES_MAX_GAIN;
Modes.dev_index = 0;
Modes.enable_agc = 0;
+ Modes.enable_amp = MODES_ENABLE_AMP;
+ Modes.lna_gain = MODES_LNA_GAIN;
+ Modes.vga_gain = MODES_VGA_GAIN;
Modes.freq = MODES_DEFAULT_FREQ;
Modes.filename = NULL;
Modes.fix_errors = 1;
@@ -331,7 +352,7 @@ void modesInit(void) {
/* =============================== RTLSDR handling ========================== */
-void modesInitRTLSDR(void) {
+int modesInitRTLSDR(void) {
int j;
int device_count;
int ppm_error = 0;
@@ -340,7 +361,7 @@ void modesInitRTLSDR(void) {
device_count = rtlsdr_get_device_count();
if (!device_count) {
fprintf(stderr, "No supported RTLSDR devices found.\n");
- exit(1);
+ return(1);
}
fprintf(stderr, "Found %d device(s):\n", device_count);
@@ -353,7 +374,7 @@ void modesInitRTLSDR(void) {
if (rtlsdr_open(&Modes.dev, Modes.dev_index) < 0) {
fprintf(stderr, "Error opening the RTLSDR device: %s\n",
strerror(errno));
- exit(1);
+ return(1);
}
/* Set gain, frequency, sample rate, and reset the device. */
@@ -381,6 +402,52 @@ void modesInitRTLSDR(void) {
rtlsdr_reset_buffer(Modes.dev);
fprintf(stderr, "Gain reported by device: %.2f\n",
rtlsdr_get_tuner_gain(Modes.dev)/10.0);
+ Modes.rtl_enabled = 1;
+ Modes.hackrf_enabled = 0;
+ return (0);
+}
+
+/* =============================== HackRF One handling ========================== */
+int modesInitHackRF(void) {
+ #define HACKRF_STATUS(status, message) \
+ if (status != 0) { \
+ fprintf(stderr, "%s\n", message); \
+ hackrf_close(Modes.hackrf); \
+ hackrf_exit(); \
+ return (1); \
+ } \
+
+ int status;
+
+ status = hackrf_init();
+ HACKRF_STATUS(status, "hackrf_init failed.");
+
+ status = hackrf_open(&Modes.hackrf);
+ HACKRF_STATUS(status, "No HackRF compatible devices found.");
+
+ status = hackrf_set_freq(Modes.hackrf, Modes.freq);
+ HACKRF_STATUS(status, "hackrf_set_freq failed.");
+
+ status = hackrf_set_sample_rate(Modes.hackrf, MODES_DEFAULT_RATE);
+ HACKRF_STATUS(status, "hackrf_set_sample_rate failed.");
+
+ status = hackrf_set_amp_enable(Modes.hackrf, Modes.enable_amp);
+ HACKRF_STATUS(status, "hackrf_set_amp_enable failed.");
+
+ status = hackrf_set_lna_gain(Modes.hackrf, Modes.lna_gain);
+ HACKRF_STATUS(status, "hackrf_set_lna_gain failed.");
+
+ status = hackrf_set_vga_gain(Modes.hackrf, Modes.vga_gain);
+ HACKRF_STATUS(status, "hackrf_set_vga_gain failed");
+
+ fprintf (stderr, "HackRF successfully initialized "
+ "(AMP Enable: %i, LNA Gain: %i, VGA Gain: %i).\n",
+ Modes.enable_amp, Modes.lna_gain, Modes.vga_gain);
+
+ Modes.hackrf_enabled = 1;
+ Modes.rtl_enabled = 0;
+
+ return (0);
}
/* We use a thread reading data in background, while the main thread
@@ -405,6 +472,27 @@ void rtlsdrCallback(unsigned char *buf, uint32_t len, void *ctx) {
pthread_mutex_unlock(&Modes.data_mutex);
}
+int hackrfCallback (hackrf_transfer *transfer) {
+ uint32_t i;
+ pthread_mutex_lock(&Modes.data_mutex);
+ uint32_t len = transfer-> buffer_length;
+ /* HackRF One returns signed IQ values, convert them to unsigned */
+ for (i = 0; i < len; i++) {
+ transfer->buffer[i] ^= (uint8_t)0x80;
+ }
+ if (len > MODES_DATA_LEN) len = MODES_DATA_LEN;
+ /* Move the last part of the previous buffer, that was not processed,
+ * on the start of the new buffer. */
+ memcpy(Modes.data, Modes.data+MODES_DATA_LEN, (MODES_FULL_LEN-1)*4);
+ /* Read the new data. */
+ memcpy(Modes.data+(MODES_FULL_LEN-1)*4, transfer->buffer, len);
+ Modes.data_ready = 1;
+ /* Signal to the other thread that new data is ready */
+ pthread_cond_signal(&Modes.data_cond);
+ pthread_mutex_unlock(&Modes.data_mutex);
+ return (0);
+}
+
/* This is used when --ifile is specified in order to read data from file
* instead of using an RTLSDR device. */
void readDataFromFile(void) {
@@ -457,9 +545,20 @@ void *readerThreadEntryPoint(void *arg) {
MODES_NOTUSED(arg);
if (Modes.filename == NULL) {
- rtlsdr_read_async(Modes.dev, rtlsdrCallback, NULL,
+ if (Modes.rtl_enabled) {
+ rtlsdr_read_async(Modes.dev, rtlsdrCallback, NULL,
MODES_ASYNC_BUF_NUMBER,
MODES_DATA_LEN);
+ }
+ else if (Modes.hackrf_enabled) {
+ int status = hackrf_start_rx(Modes.hackrf, hackrfCallback, NULL);
+ if (status != 0) {
+ fprintf(stderr, "hackrf_start_rx failed");
+ hackrf_close(Modes.hackrf);
+ hackrf_exit();
+ exit (1);
+ }
+ }
} else {
readDataFromFile();
}
@@ -2419,8 +2518,11 @@ int getTermRows() {
void showHelp(void) {
printf(
"--device-index <index> Select RTL device (default: 0).\n"
-"--gain <db> Set gain (default: max gain. Use -100 for auto-gain).\n"
-"--enable-agc Enable the Automatic Gain Control (default: off).\n"
+"--gain <db> Set RTLSDR gain (default: max gain. Use -100 for auto-gain).\n"
+"--enable-agc Enable RTLSDR Automatic Gain Control (default: off).\n"
+"--enable-amp Enable HackRF RX/TX RF amplifier (default: off).\n"
+"--lna-gain Set HackRF RX LNA (IF) gain, 0-40dB, 8dB steps (default: 32).\n"
+"--vga-gain Set HackRF RX VGA (baseband) gain, 0-62dB, 2dB steps (default: 48).\n"
"--freq <hz> Set frequency (default: 1090 Mhz).\n"
"--ifile <filename> Read data from file (use '-' for stdin).\n"
"--interactive Interactive mode refreshing data on screen.\n"
@@ -2490,6 +2592,12 @@ int main(int argc, char **argv) {
Modes.gain = atof(argv[++j])*10; /* Gain is in tens of DBs */
} else if (!strcmp(argv[j],"--enable-agc")) {
Modes.enable_agc++;
+ } else if (!strcmp(argv[j],"--enable-amp")) {
+ Modes.enable_amp = 1;
+ } else if (!strcmp(argv[j],"--lna-gain")) {
+ Modes.lna_gain = atoi(argv[++j]);
+ } else if (!strcmp(argv[j],"--vga-gain")) {
+ Modes.vga_gain = atoi(argv[++j]);
} else if (!strcmp(argv[j],"--freq") && more) {
Modes.freq = strtoll(argv[++j],NULL,10);
} else if (!strcmp(argv[j],"--ifile") && more) {
@@ -2568,7 +2676,13 @@ int main(int argc, char **argv) {
if (Modes.net_only) {
fprintf(stderr,"Net-only mode, no RTL device or file open.\n");
} else if (Modes.filename == NULL) {
- modesInitRTLSDR();
+ if ( modesInitRTLSDR() ) {
+ /* No RTLSDR found, check for HackRF One */
+ if ( modesInitHackRF() ) {
+ //fprintf(stderr,"You need a compatible SDR device.\n");
+ exit (1);
+ }
+ }
} else {
if (Modes.filename[0] == '-' && Modes.filename[1] == '\0') {
Modes.fd = STDIN_FILENO;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment