Created
July 12, 2021 15:07
-
-
Save AlexFWulff/7d6e75aaffb7215c2a175a2992db70e1 to your computer and use it in GitHub Desktop.
Loosely based on hackrf_read.c (https://gist.github.com/beurtschipper/a8c0b472392da212bf03b08f372a8f7d). This code creates a fault state in the most recent HackRF release.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// HackRF lib | |
#include <hackrf.h> | |
// Normal system libs | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <unistd.h> | |
#include <signal.h> | |
// Device object | |
static hackrf_device* device = NULL; | |
// Cross-thread boolean trigger | |
volatile bool do_exit = false; | |
// File for output | |
FILE *fp; | |
// Settings | |
#define DEFAULT_SAMPLE_RATE_HZ 20e6 | |
#define RADIO_FREQ 95300000 | |
#define OUTPUT_FILE "output_samples.raw" | |
bool started = false; | |
// Break on signals like ctrl+c | |
void sigint_callback_handler(int signum) | |
{ | |
fprintf(stderr, "Caught signal %d\n", signum); | |
do_exit = true; | |
} | |
// Callback for data receive | |
int rx_callback(hackrf_transfer* transfer) | |
{ | |
started = true; | |
fprintf(stderr, "Valid length: %i\n", transfer->valid_length); | |
return 0; | |
} | |
int main(int argc, char** argv) | |
{ | |
int result = 0; | |
// Frequency in hertz | |
const uint64_t freq_hz = RADIO_FREQ; | |
// Open output file | |
fp = fopen(OUTPUT_FILE, "w"); | |
if( fp == NULL ) | |
{ | |
fprintf(stderr, "Error opening output file"); | |
return EXIT_FAILURE; | |
} | |
// Init HackRF | |
result = hackrf_init(); | |
if( result != HACKRF_SUCCESS ) | |
{ | |
fprintf(stderr, "hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); | |
return EXIT_FAILURE; | |
} | |
// Open device | |
const char* serial_number = NULL; | |
result = hackrf_open_by_serial(serial_number, &device); | |
if( result != HACKRF_SUCCESS ) | |
{ | |
fprintf(stderr, "hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); | |
return EXIT_FAILURE; | |
} | |
// Set frequency | |
result = hackrf_set_freq(device, freq_hz); | |
if ( result != HACKRF_SUCCESS ) | |
{ | |
fprintf(stderr, "hackrf_set_freq() failed: %s (%d)\n", hackrf_error_name(result), result); | |
return EXIT_FAILURE; | |
} | |
// Set sample rate | |
result = hackrf_set_sample_rate(device, DEFAULT_SAMPLE_RATE_HZ); | |
if( result != HACKRF_SUCCESS ) { | |
fprintf(stderr, "hackrf_sample_rate_set() failed: %s (%d)\n", hackrf_error_name(result), result); | |
return EXIT_FAILURE; | |
} | |
// Handle signals (For ctrl+c and kill signals) | |
signal(SIGINT, &sigint_callback_handler); | |
// Start recieving | |
while (!do_exit) { | |
fprintf(stderr,"Start\n"); | |
result |= hackrf_start_rx(device, rx_callback, NULL); | |
if (result != HACKRF_SUCCESS) { | |
fprintf(stderr, "hackrf_start_rx() failed: %s (%d)\n", | |
hackrf_error_name(result), result); | |
} | |
int restart_count = 0; | |
while (!started && !do_exit) { | |
fprintf(stderr, "Not started yet\n"); | |
usleep(100000); | |
} | |
if (do_exit) break; | |
fprintf(stderr, "Finally started\n"); | |
started = false; | |
result = hackrf_stop_rx(device); | |
if (result != HACKRF_SUCCESS) { | |
fprintf(stderr, "hackrf_start_rx() failed: %s (%d)\n", | |
hackrf_error_name(result), result); | |
} | |
} | |
// Stop HackRF and clean up | |
if(device != NULL) | |
{ | |
// Stop recieving | |
result = hackrf_stop_rx(device); | |
if(result != HACKRF_SUCCESS) | |
fprintf(stderr, "hackrf_stop_rx() failed: %s (%d)\n", | |
hackrf_error_name(result), result); | |
// Close device | |
result = hackrf_close(device); | |
if(result != HACKRF_SUCCESS) | |
fprintf(stderr, "hackrf_close() failed: %s (%d)\n", | |
hackrf_error_name(result), result); | |
// Exit HackRF and clean up | |
hackrf_exit(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment