Skip to content

Commit

Permalink
added support for hackrf. fix #6
Browse files Browse the repository at this point in the history
  • Loading branch information
dernasherbrezon committed Jan 5, 2025
1 parent 3147dec commit 238df9e
Show file tree
Hide file tree
Showing 18 changed files with 597 additions and 65 deletions.
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
"__tree": "c",
"memory_resource": "c",
"airspy.h": "c",
"cstdlib": "c"
"cstdlib": "c",
"stdlib.h": "c",
"__locale": "c",
"chrono": "c",
"functional": "c",
"typeindex": "c",
"typeinfo": "c",
"text_encoding": "c"
}
}
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_library(sdr_serverLib
${CMAKE_CURRENT_SOURCE_DIR}/src/client/tcp_client.c
${CMAKE_CURRENT_SOURCE_DIR}/src/sdr/airspy_device.c
${CMAKE_CURRENT_SOURCE_DIR}/src/sdr/rtlsdr_device.c
${CMAKE_CURRENT_SOURCE_DIR}/src/sdr/hackrf_device.c
)

# at some point homebrew changed default location
Expand All @@ -49,6 +50,11 @@ include_directories(${PC_RTLSDR_INCLUDE_DIRS})
pkg_check_modules(PC_LIBAIRSPY REQUIRED libairspy)
include_directories(${PC_LIBAIRSPY_INCLUDE_DIRS})

pkg_check_modules(PC_LIBHACKRF REQUIRED libhackrf)
include_directories(${PC_LIBHACKRF_INCLUDE_DIRS})

message("${PC_LIBHACKRF_INCLUDE_DIRS}")

pkg_check_modules(PC_ZLIB REQUIRED zlib)
include_directories(${PC_ZLIB_INCLUDE_DIRS})
link_directories(${PC_ZLIB_LIBRARY_DIRS})
Expand All @@ -67,6 +73,7 @@ add_executable(sdr_server
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
${CMAKE_CURRENT_SOURCE_DIR}/src/sdr/rtlsdr_lib.c
${CMAKE_CURRENT_SOURCE_DIR}/src/sdr/airspy_lib.c
${CMAKE_CURRENT_SOURCE_DIR}/src/sdr/hackrf_lib.c
)
target_link_libraries(sdr_server sdr_serverLib)

Expand All @@ -92,6 +99,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/test/unity-2.5.2/src/)
add_library(sdr_serverTestLib
${CMAKE_CURRENT_SOURCE_DIR}/test/rtlsdr_lib_mock.c
${CMAKE_CURRENT_SOURCE_DIR}/test/airspy_lib_mock.c
${CMAKE_CURRENT_SOURCE_DIR}/test/hackrf_lib_mock.c
${CMAKE_CURRENT_SOURCE_DIR}/src/client/tcp_client.c
${CMAKE_CURRENT_SOURCE_DIR}/test/utils.c
${CMAKE_CURRENT_SOURCE_DIR}/test/unity-2.5.2/src/unity.c
Expand Down
135 changes: 79 additions & 56 deletions src/config.c
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
#include <libconfig.h>
#include "config.h"

#include <errno.h>
#include <stdlib.h>
#include <libconfig.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"

#define AIRSPY_BUFFER_SIZE 262144

char *read_and_copy_str(const config_setting_t *setting, const char *default_value) {
const char *value;
if (setting == NULL) {
if (default_value == NULL) {
return NULL;
}
value = default_value;
} else {
value = config_setting_get_string(setting);
}
char *bind_address;
size_t length = strlen(value);
char *str_bind_address = malloc(sizeof(char) * length + 1);
if (str_bind_address == NULL) {
return NULL;
const char *value;
if (setting == NULL) {
if (default_value == NULL) {
return NULL;
}
strncpy(str_bind_address, value, length);
str_bind_address[length] = '\0';
bind_address = str_bind_address;
return bind_address;
value = default_value;
} else {
value = config_setting_get_string(setting);
}
char *bind_address;
size_t length = strlen(value);
char *str_bind_address = malloc(sizeof(char) * length + 1);
if (str_bind_address == NULL) {
return NULL;
}
strncpy(str_bind_address, value, length);
str_bind_address[length] = '\0';
bind_address = str_bind_address;
return bind_address;
}

int config_read_int(config_t *libconfig, const char *config_name, int default_value) {
Expand All @@ -48,7 +48,7 @@ float config_read_float(config_t *libconfig, const char *config_name, float defa
if (setting == NULL) {
result = default_value;
} else {
result = (float) config_setting_get_float(setting);
result = (float)config_setting_get_float(setting);
}
fprintf(stdout, "%s: %f\n", config_name, result);
return result;
Expand All @@ -72,7 +72,7 @@ uint32_t config_read_uint32_t(config_t *libconfig, const char *config_name, uint
if (setting == NULL) {
result = default_value;
} else {
result = (uint32_t) config_setting_get_int(setting);
result = (uint32_t)config_setting_get_int(setting);
}
fprintf(stdout, "%s: %d\n", config_name, result);
return result;
Expand All @@ -84,7 +84,7 @@ int create_server_config(struct server_config **config, const char *path) {
if (result == NULL) {
return -ENOMEM;
}
*result = (struct server_config) {0};
*result = (struct server_config){0};

config_t libconfig;
config_init(&libconfig);
Expand All @@ -100,7 +100,7 @@ int create_server_config(struct server_config **config, const char *path) {
result->sdr_type = config_read_int(&libconfig, "sdr_type", 0);
result->bias_t = config_read_int(&libconfig, "bias_t", 0);
result->gain_mode = config_read_int(&libconfig, "gain_mode", 0);
result->gain = (int) (config_read_float(&libconfig, "gain", 0) * 10);
result->gain = (int)(config_read_float(&libconfig, "gain", 0) * 10);
result->ppm = config_read_int(&libconfig, "ppm", 0);

result->airspy_gain_mode = config_read_int(&libconfig, "airspy_gain_mode", AIRSPY_GAIN_MANUAL);
Expand Down Expand Up @@ -140,6 +140,29 @@ int create_server_config(struct server_config **config, const char *path) {
return -1;
}

result->hackrf_bias_t = config_read_int(&libconfig, "hackrf_bias_t", 0);
result->hackrf_amp = config_read_int(&libconfig, "hackrf_amp", 0);
if (result->hackrf_amp > 1) {
fprintf(stderr, "<3>hackrf_amp is either turned on (1) or off (0)\n");
config_destroy(&libconfig);
free(result);
return -1;
}
result->hackrf_lna_gain = config_read_int(&libconfig, "hackrf_lna_gain", 16);
if (result->hackrf_lna_gain < 0 || result->hackrf_lna_gain > 40) {
fprintf(stderr, "<3>invalid hackrf_lna_gain configuration\n");
config_destroy(&libconfig);
free(result);
return -1;
}
result->hackrf_vga_gain = config_read_int(&libconfig, "hackrf_vga_gain", 16);
if (result->hackrf_vga_gain < 0 || result->hackrf_vga_gain > 62) {
fprintf(stderr, "<3>invalid hackrf_vga_gain configuration\n");
config_destroy(&libconfig);
free(result);
return -1;
}

result->queue_size = config_read_int(&libconfig, "queue_size", 64);
if (result->queue_size <= 0) {
fprintf(stderr, "<3>queue size should be positive: %d\n", result->queue_size);
Expand All @@ -148,25 +171,25 @@ int create_server_config(struct server_config **config, const char *path) {
return -1;
}

const config_setting_t *setting = config_lookup(&libconfig, "band_sampling_rate");
if (setting == NULL) {
fprintf(stderr, "<3>missing required configuration: band_sampling_rate\n");
config_destroy(&libconfig);
free(result);
return -1;
}
uint32_t band_sampling_rate = (uint32_t) config_setting_get_int(setting);
fprintf(stdout, "band sampling rate: %d\n", band_sampling_rate);
result->band_sampling_rate = band_sampling_rate;

result->device_index = config_read_int(&libconfig, "device_index", 0);
result->device_serial = read_and_copy_str(config_lookup(&libconfig, "device_serial"), NULL);
if (result->device_serial != NULL) {
fprintf(stdout, "device_serial: %s\n", result->device_serial);
}
const config_setting_t *setting = config_lookup(&libconfig, "band_sampling_rate");
if (setting == NULL) {
fprintf(stderr, "<3>missing required configuration: band_sampling_rate\n");
config_destroy(&libconfig);
free(result);
return -1;
}
uint32_t band_sampling_rate = (uint32_t)config_setting_get_int(setting);
fprintf(stdout, "band sampling rate: %d\n", band_sampling_rate);
result->band_sampling_rate = band_sampling_rate;

result->device_index = config_read_int(&libconfig, "device_index", 0);
result->device_serial = read_and_copy_str(config_lookup(&libconfig, "device_serial"), NULL);
if (result->device_serial != NULL) {
fprintf(stdout, "device_serial: %s\n", result->device_serial);
}

result->buffer_size = config_read_uint32_t(&libconfig, "buffer_size", 262144);
if( result->sdr_type == SDR_TYPE_AIRSPY && result->buffer_size != AIRSPY_BUFFER_SIZE ) {
if (result->sdr_type == SDR_TYPE_AIRSPY && result->buffer_size != AIRSPY_BUFFER_SIZE) {
result->buffer_size = AIRSPY_BUFFER_SIZE;
fprintf(stdout, "force airspy buffer_size to: %d\n", result->buffer_size);
}
Expand Down Expand Up @@ -216,17 +239,17 @@ int create_server_config(struct server_config **config, const char *path) {
}

void destroy_server_config(struct server_config *config) {
if (config == NULL) {
return;
}
if (config->bind_address != NULL) {
free(config->bind_address);
}
if (config->base_path != NULL) {
free(config->base_path);
}
if (config->device_serial != NULL) {
free(config->device_serial);
}
free(config);
if (config == NULL) {
return;
}
if (config->bind_address != NULL) {
free(config->bind_address);
}
if (config->base_path != NULL) {
free(config->base_path);
}
if (config->device_serial != NULL) {
free(config->device_serial);
}
free(config);
}
11 changes: 9 additions & 2 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

typedef enum {
SDR_TYPE_RTL = 0,
SDR_TYPE_AIRSPY = 1
SDR_TYPE_AIRSPY = 1,
SDR_TYPE_HACKRF = 2
} sdr_type_t;

typedef enum {
Expand All @@ -21,12 +22,12 @@ struct server_config {
char *bind_address;
int port;
int read_timeout_seconds;
int device_index;
char *device_serial;

sdr_type_t sdr_type;

// rtl-sdr settings
int device_index;
int gain_mode;
int gain;
int ppm;
Expand All @@ -45,6 +46,12 @@ struct server_config {
int airspy_linearity_gain;
int airspy_sensitivity_gain;

// hackrf settings
uint8_t hackrf_bias_t;
int hackrf_amp;
int hackrf_lna_gain;
int hackrf_vga_gain;

// output settings
char *base_path;
bool use_gzip;
Expand Down
4 changes: 4 additions & 0 deletions src/dsp_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ static void *callback(void *arg) {
break;
}
switch (config->sdr_type) {
case SDR_TYPE_HACKRF: {
process_cs8((const int8_t *)input, input_len, &filter_output, &filter_output_len, worker->filter);
break;
}
case SDR_TYPE_RTL: {
process_cu8(input, input_len, &filter_output, &filter_output_len, worker->filter);
break;
Expand Down
28 changes: 22 additions & 6 deletions src/resources/config.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ port=8090
# same buffer setting for passing data between threads
# the bigger buffer the less context switching, but
# bigger latency for RF messages
# AIRSPY has fixed buffer = 262144
# AIRSPY and HackRF have fixed buffer = 262144
buffer_size=262144

# number of elements in the DSP queue
Expand Down Expand Up @@ -43,6 +43,11 @@ lpf_cutoff_rate=5
# Each type has it's own settings. See the following sections for fune tuning
sdr_type=0

# Use device serial number (SN) instead of device index
# Serial number can be changed and used to uniquely identify dongles
# Can be used by rtl-sdr and hackrf
#device_serial="00000100"

##### Generic SDR settings #####
# clients can select the band freq,
# but server controls the sample rate of the band
Expand Down Expand Up @@ -71,10 +76,6 @@ ppm=0
# device index
device_index=0

# Use device serial number (SN) instead of device index
# Serial number can be changed and used to uniquely identify dongles
#device_serial="00000100"

##### Airspy settings #####
# controls the gain mode:
# 0 - auto
Expand All @@ -100,4 +101,19 @@ airspy_linearity_gain=0

# Predefined combination of VGA, MIXER and LNA gains to archive better sensitivity.
# From 0 to 21
airspy_sensitivity_gain=0
airspy_sensitivity_gain=0

##### HackRF settings #####

# Enable / disable the ~11dB RF RX/TX amplifiers U13/U25 via controlling switches U9 and U14.
# enable (1) or disable (0) amplifier
hackrf_amp=0

# Set the RF RX gain of the MAX2837 transceiver IC ("IF" gain setting) in decibels. Must be in range 0-40dB, with 8dB steps.
hackrf_lna_gain=16

# Set baseband RX gain of the MAX2837 transceier IC ("BB" or "VGA" gain setting) in decibels. Must be in range 0-62dB with 2dB steps.
hackrf_vga_gain=16

# Enable or disable the **3.3V (max 50mA)** bias-tee (antenna port power). Defaults to disabled.
hackrf_bias_t=0
Loading

0 comments on commit 238df9e

Please sign in to comment.