├── .gitignore ├── CMakeLists.txt └── hackrf_laser.c /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | project(inspectrum) 3 | 4 | find_package(PkgConfig REQUIRED) 5 | pkg_check_modules(HACKRF REQUIRED libhackrf) 6 | pkg_check_modules(FFTW REQUIRED fftw3f) 7 | 8 | include_directories(${HACKRF_INCLUDE_DIRS} /usr/local/include/ol) 9 | 10 | add_executable(hackrf_laser hackrf_laser.c) 11 | target_link_libraries(hackrf_laser m ol ${HACKRF_LIBRARIES} ${FFTW_LIBRARIES}) 12 | -------------------------------------------------------------------------------- /hackrf_laser.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Dominic Spill 3 | * Copyright 2016 Mike Walters 4 | * Copyright 2017 Michael Ossmann 5 | * 6 | * This file is part of HackRF. 7 | * 8 | * This program is free software; you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation; either version 2, or (at your option) 11 | * any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program; see the file COPYING. If not, write to 20 | * the Free Software Foundation, Inc., 51 Franklin Street, 21 | * Boston, MA 02110-1301, USA. 22 | */ 23 | 24 | #include 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | #include 40 | 41 | #define _FILE_OFFSET_BITS 64 42 | 43 | #ifndef bool 44 | typedef int bool; 45 | #define true 1 46 | #define false 0 47 | #endif 48 | 49 | #ifdef _WIN32 50 | #define _USE_MATH_DEFINES 51 | #include 52 | #ifdef _MSC_VER 53 | 54 | #ifdef _WIN64 55 | typedef int64_t ssize_t; 56 | #else 57 | typedef int32_t ssize_t; 58 | #endif 59 | 60 | #define strtoull _strtoui64 61 | #define snprintf _snprintf 62 | 63 | int gettimeofday(struct timeval *tv, void* ignored) { 64 | FILETIME ft; 65 | unsigned __int64 tmp = 0; 66 | if (NULL != tv) { 67 | GetSystemTimeAsFileTime(&ft); 68 | tmp |= ft.dwHighDateTime; 69 | tmp <<= 32; 70 | tmp |= ft.dwLowDateTime; 71 | tmp /= 10; 72 | tmp -= 11644473600000000Ui64; 73 | tv->tv_sec = (long)(tmp / 1000000UL); 74 | tv->tv_usec = (long)(tmp % 1000000UL); 75 | } 76 | return 0; 77 | } 78 | 79 | #endif 80 | #endif 81 | 82 | #if defined(__GNUC__) 83 | #include 84 | #include 85 | #endif 86 | 87 | #include 88 | #include 89 | 90 | #define FD_BUFFER_SIZE (8*1024) 91 | 92 | #define FREQ_ONE_MHZ (1000000ull) 93 | 94 | #define FREQ_MIN_MHZ (0) /* 0 MHz */ 95 | #define FREQ_MAX_MHZ (7250) /* 7250 MHz */ 96 | 97 | #define DEFAULT_SAMPLE_RATE_HZ (20000000) /* 20MHz default sample rate */ 98 | #define DEFAULT_BASEBAND_FILTER_BANDWIDTH (15000000) /* 15MHz default */ 99 | 100 | #define TUNE_STEP (DEFAULT_SAMPLE_RATE_HZ / FREQ_ONE_MHZ) 101 | #define OFFSET 7500000 102 | 103 | #define BLOCKS_PER_TRANSFER 16 104 | #define THROWAWAY_BLOCKS 2 105 | 106 | #if defined _WIN32 107 | #define sleep(a) Sleep( (a*1000) ) 108 | #endif 109 | 110 | uint32_t num_samples = SAMPLES_PER_BLOCK; 111 | int num_ranges = 0; 112 | uint16_t frequencies[MAX_SWEEP_RANGES*2]; 113 | int step_count; 114 | 115 | static float TimevalDiff(const struct timeval *a, const struct timeval *b) { 116 | return (a->tv_sec - b->tv_sec) + 1e-6f * (a->tv_usec - b->tv_usec); 117 | } 118 | 119 | int parse_u32(char* s, uint32_t* const value) { 120 | uint_fast8_t base = 10; 121 | char* s_end; 122 | uint64_t ulong_value; 123 | 124 | if( strlen(s) > 2 ) { 125 | if( s[0] == '0' ) { 126 | if( (s[1] == 'x') || (s[1] == 'X') ) { 127 | base = 16; 128 | s += 2; 129 | } else if( (s[1] == 'b') || (s[1] == 'B') ) { 130 | base = 2; 131 | s += 2; 132 | } 133 | } 134 | } 135 | 136 | s_end = s; 137 | ulong_value = strtoul(s, &s_end, base); 138 | if( (s != s_end) && (*s_end == 0) ) { 139 | *value = (uint32_t)ulong_value; 140 | return HACKRF_SUCCESS; 141 | } else { 142 | return HACKRF_ERROR_INVALID_PARAM; 143 | } 144 | } 145 | 146 | int parse_u32_range(char* s, uint32_t* const value_min, uint32_t* const value_max) { 147 | int result; 148 | 149 | char *sep = strchr(s, ':'); 150 | if (!sep) 151 | return HACKRF_ERROR_INVALID_PARAM; 152 | 153 | *sep = 0; 154 | 155 | result = parse_u32(s, value_min); 156 | if (result != HACKRF_SUCCESS) 157 | return result; 158 | result = parse_u32(sep + 1, value_max); 159 | if (result != HACKRF_SUCCESS) 160 | return result; 161 | 162 | return HACKRF_SUCCESS; 163 | } 164 | 165 | volatile bool do_exit = false; 166 | 167 | FILE* fd = NULL; 168 | volatile uint32_t byte_count = 0; 169 | volatile uint64_t sweep_count = 0; 170 | 171 | struct timeval time_start; 172 | struct timeval t_start; 173 | struct timeval time_stamp; 174 | 175 | bool amp = false; 176 | uint32_t amp_enable; 177 | 178 | bool antenna = false; 179 | uint32_t antenna_enable; 180 | 181 | bool binary_output = false; 182 | bool ifft_output = false; 183 | bool openlase_output = false; 184 | bool one_shot = false; 185 | volatile bool sweep_started = false; 186 | 187 | int fftSize = 20; 188 | double fft_bin_width; 189 | fftwf_complex *fftwIn = NULL; 190 | fftwf_complex *fftwOut = NULL; 191 | fftwf_plan fftwPlan = NULL; 192 | fftwf_complex *ifftwIn = NULL; 193 | fftwf_complex *ifftwOut = NULL; 194 | fftwf_plan ifftwPlan = NULL; 195 | float *openlaseBuf = NULL; 196 | uint32_t ifft_idx = 0; 197 | float* pwr; 198 | float* window; 199 | 200 | float logPower(fftwf_complex in, float scale) 201 | { 202 | float re = in[0] * scale; 203 | float im = in[1] * scale; 204 | float magsq = re * re + im * im; 205 | return (float) (log2(magsq) * 10.0f / log2(10.0f)); 206 | } 207 | 208 | int rx_callback(hackrf_transfer* transfer) { 209 | int8_t* buf; 210 | uint8_t* ubuf; 211 | uint64_t frequency; /* in Hz */ 212 | uint64_t band_edge; 213 | uint32_t record_length; 214 | int i, j, ifft_bins; 215 | struct tm *fft_time; 216 | char time_str[50]; 217 | struct timeval usb_transfer_time; 218 | 219 | if(NULL == fd) { 220 | return -1; 221 | } 222 | 223 | gettimeofday(&usb_transfer_time, NULL); 224 | byte_count += transfer->valid_length; 225 | buf = (int8_t*) transfer->buffer; 226 | ifft_bins = fftSize * step_count; 227 | for(j=0; j i; i++) { 328 | ifftwIn[ifft_idx + i][0] = fftwOut[i + 1 + (fftSize*5)/8][0]; 329 | ifftwIn[ifft_idx + i][1] = fftwOut[i + 1 + (fftSize*5)/8][1]; 330 | } 331 | ifft_idx += fftSize / 2; 332 | ifft_idx %= ifft_bins; 333 | for(i = 0; (fftSize / 4) > i; i++) { 334 | ifftwIn[ifft_idx + i][0] = fftwOut[i + 1 + (fftSize/8)][0]; 335 | ifftwIn[ifft_idx + i][1] = fftwOut[i + 1 + (fftSize/8)][1]; 336 | } 337 | } else if (openlase_output) { 338 | ifft_idx = (uint32_t) round((frequency - (uint64_t)(FREQ_ONE_MHZ*frequencies[0])) 339 | / fft_bin_width); 340 | ifft_idx = (ifft_idx + ifft_bins/2) % ifft_bins; 341 | for(i = 0; (fftSize / 4) > i; i++) { 342 | openlaseBuf[ifft_idx + i] = pwr[i + 1 + (fftSize*5)/8]; 343 | } 344 | ifft_idx += fftSize / 2; 345 | ifft_idx %= ifft_bins; 346 | for(i = 0; (fftSize / 4) > i; i++) { 347 | openlaseBuf[ifft_idx + i] = pwr[i + 1 + (fftSize/8)]; 348 | } 349 | } else { 350 | time_t time_stamp_seconds = time_stamp.tv_sec; 351 | fft_time = localtime(&time_stamp_seconds); 352 | strftime(time_str, 50, "%Y-%m-%d, %H:%M:%S", fft_time); 353 | fprintf(fd, "%s.%06ld, %" PRIu64 ", %" PRIu64 ", %.2f, %u", 354 | time_str, 355 | (long int)time_stamp.tv_usec, 356 | (uint64_t)(frequency), 357 | (uint64_t)(frequency+DEFAULT_SAMPLE_RATE_HZ/4), 358 | fft_bin_width, 359 | fftSize); 360 | for(i = 0; (fftSize / 4) > i; i++) { 361 | fprintf(fd, ", %.2f", pwr[i + 1 + (fftSize*5)/8]); 362 | } 363 | fprintf(fd, "\n"); 364 | fprintf(fd, "%s.%06ld, %" PRIu64 ", %" PRIu64 ", %.2f, %u", 365 | time_str, 366 | (long int)time_stamp.tv_usec, 367 | (uint64_t)(frequency+(DEFAULT_SAMPLE_RATE_HZ/2)), 368 | (uint64_t)(frequency+((DEFAULT_SAMPLE_RATE_HZ*3)/4)), 369 | fft_bin_width, 370 | fftSize); 371 | for(i = 0; (fftSize / 4) > i; i++) { 372 | fprintf(fd, ", %.2f", pwr[i + 1 + (fftSize/8)]); 373 | } 374 | fprintf(fd, "\n"); 375 | } 376 | } 377 | return 0; 378 | } 379 | 380 | static void usage() { 381 | fprintf(stderr, "Usage:\n"); 382 | fprintf(stderr, "\t[-h] # this help\n"); 383 | fprintf(stderr, "\t[-d serial_number] # Serial number of desired HackRF\n"); 384 | fprintf(stderr, "\t[-a amp_enable] # RX RF amplifier 1=Enable, 0=Disable\n"); 385 | fprintf(stderr, "\t[-f freq_min:freq_max] # minimum and maximum frequencies in MHz\n"); 386 | fprintf(stderr, "\t[-p antenna_enable] # Antenna port power, 1=Enable, 0=Disable\n"); 387 | fprintf(stderr, "\t[-l gain_db] # RX LNA (IF) gain, 0-40dB, 8dB steps\n"); 388 | fprintf(stderr, "\t[-g gain_db] # RX VGA (baseband) gain, 0-62dB, 2dB steps\n"); 389 | fprintf(stderr, "\t[-n num_samples] # Number of samples per frequency, 8192-4294967296\n"); 390 | fprintf(stderr, "\t[-w bin_width] # FFT bin width (frequency resolution) in Hz\n"); 391 | fprintf(stderr, "\t[-1] # one shot mode\n"); 392 | fprintf(stderr, "\t[-B] # binary output\n"); 393 | fprintf(stderr, "\t[-I] # binary inverse FFT output\n"); 394 | fprintf(stderr, "\t[-L] # openlase output\n"); 395 | fprintf(stderr, "\t-r filename # output file\n"); 396 | fprintf(stderr, "\n"); 397 | fprintf(stderr, "Output fields:\n"); 398 | fprintf(stderr, "\tdate, time, hz_low, hz_high, hz_bin_width, num_samples, dB, dB, . . .\n"); 399 | } 400 | 401 | static hackrf_device* device = NULL; 402 | 403 | #ifdef _MSC_VER 404 | BOOL WINAPI 405 | sighandler(int signum) { 406 | if (CTRL_C_EVENT == signum) { 407 | fprintf(stderr, "Caught signal %d\n", signum); 408 | do_exit = true; 409 | return TRUE; 410 | } 411 | return FALSE; 412 | } 413 | #else 414 | void sigint_callback_handler(int signum) { 415 | fprintf(stderr, "Caught signal %d\n", signum); 416 | do_exit = true; 417 | } 418 | #endif 419 | 420 | int main(int argc, char** argv) { 421 | int opt, i, result = 0; 422 | const char* path = NULL; 423 | const char* serial_number = NULL; 424 | int exit_code = EXIT_SUCCESS; 425 | struct timeval time_now; 426 | float time_diff; 427 | float sweep_rate; 428 | unsigned int lna_gain=16, vga_gain=20; 429 | uint32_t freq_min = 0; 430 | uint32_t freq_max = 6000; 431 | uint32_t requested_fft_bin_width; 432 | 433 | 434 | while( (opt = getopt(argc, argv, "a:f:p:l:g:d:n:w:1BILr:h?")) != EOF ) { 435 | result = HACKRF_SUCCESS; 436 | switch( opt ) 437 | { 438 | case 'd': 439 | serial_number = optarg; 440 | break; 441 | 442 | case 'a': 443 | amp = true; 444 | result = parse_u32(optarg, &_enable); 445 | break; 446 | 447 | case 'f': 448 | result = parse_u32_range(optarg, &freq_min, &freq_max); 449 | if(freq_min >= freq_max) { 450 | fprintf(stderr, 451 | "argument error: freq_max must be greater than freq_min.\n"); 452 | usage(); 453 | return EXIT_FAILURE; 454 | } 455 | if(FREQ_MAX_MHZ 1 ) { 552 | fprintf(stderr, "argument error: amp_enable shall be 0 or 1.\n"); 553 | usage(); 554 | return EXIT_FAILURE; 555 | } 556 | } 557 | 558 | if (antenna) { 559 | if (antenna_enable > 1) { 560 | fprintf(stderr, "argument error: antenna_enable shall be 0 or 1.\n"); 561 | usage(); 562 | return EXIT_FAILURE; 563 | } 564 | } 565 | 566 | if (0 == num_ranges) { 567 | frequencies[0] = (uint16_t)freq_min; 568 | frequencies[1] = (uint16_t)freq_max; 569 | num_ranges++; 570 | } 571 | 572 | if(binary_output + ifft_output + openlase_output > 1) { 573 | fprintf(stderr, "argument error: binary output (-B), IFFT output (-I) and openlase output (-L) are mutually exclusive.\n"); 574 | return EXIT_FAILURE; 575 | } 576 | 577 | if(ifft_output && (1 < num_ranges)) { 578 | fprintf(stderr, "argument error: only one frequency range is supported in IFFT output (-I) mode.\n"); 579 | return EXIT_FAILURE; 580 | } 581 | 582 | if(4 > fftSize) { 583 | fprintf(stderr, 584 | "argument error: FFT bin width (-w) must be no more than one quarter the sample rate\n"); 585 | return EXIT_FAILURE; 586 | } 587 | 588 | if(8184 < fftSize) { 589 | fprintf(stderr, 590 | "argument error: FFT bin width (-w) too small, resulted in more than 8184 FFT bins\n"); 591 | return EXIT_FAILURE; 592 | } 593 | 594 | /* In interleaved mode, the FFT bin selection works best if the total 595 | * number of FFT bins is equal to an odd multiple of four. 596 | * (e.g. 4, 12, 20, 28, 36, . . .) 597 | */ 598 | while((fftSize + 4) % 8) { 599 | fftSize++; 600 | } 601 | 602 | fft_bin_width = (double)DEFAULT_SAMPLE_RATE_HZ / fftSize; 603 | fftwIn = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * fftSize); 604 | fftwOut = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * fftSize); 605 | fftwPlan = fftwf_plan_dft_1d(fftSize, fftwIn, fftwOut, FFTW_FORWARD, FFTW_MEASURE); 606 | pwr = (float*)fftwf_malloc(sizeof(float) * fftSize); 607 | window = (float*)fftwf_malloc(sizeof(float) * fftSize); 608 | for (i = 0; i < fftSize; i++) { 609 | window[i] = (float) (0.5f * (1.0f - cos(2 * M_PI * i / (fftSize - 1)))); 610 | } 611 | 612 | result = hackrf_init(); 613 | if( result != HACKRF_SUCCESS ) { 614 | fprintf(stderr, "hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); 615 | usage(); 616 | return EXIT_FAILURE; 617 | } 618 | 619 | result = hackrf_open_by_serial(serial_number, &device); 620 | if( result != HACKRF_SUCCESS ) { 621 | fprintf(stderr, "hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); 622 | usage(); 623 | return EXIT_FAILURE; 624 | } 625 | 626 | if((NULL == path) || (strcmp(path, "-") == 0)) { 627 | fd = stdout; 628 | } else { 629 | fd = fopen(path, "wb"); 630 | } 631 | 632 | if(NULL == fd) { 633 | fprintf(stderr, "Failed to open file: %s\n", path); 634 | return EXIT_FAILURE; 635 | } 636 | /* Change fd buffer to have bigger one to store or read data on/to HDD */ 637 | result = setvbuf(fd , NULL , _IOFBF , FD_BUFFER_SIZE); 638 | if( result != 0 ) { 639 | fprintf(stderr, "setvbuf() failed: %d\n", result); 640 | usage(); 641 | return EXIT_FAILURE; 642 | } 643 | 644 | #ifdef _MSC_VER 645 | SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE ); 646 | #else 647 | signal(SIGINT, &sigint_callback_handler); 648 | signal(SIGILL, &sigint_callback_handler); 649 | signal(SIGFPE, &sigint_callback_handler); 650 | signal(SIGSEGV, &sigint_callback_handler); 651 | signal(SIGTERM, &sigint_callback_handler); 652 | signal(SIGABRT, &sigint_callback_handler); 653 | #endif 654 | fprintf(stderr, "call hackrf_sample_rate_set(%.03f MHz)\n", 655 | ((float)DEFAULT_SAMPLE_RATE_HZ/(float)FREQ_ONE_MHZ)); 656 | result = hackrf_set_sample_rate_manual(device, DEFAULT_SAMPLE_RATE_HZ, 1); 657 | if( result != HACKRF_SUCCESS ) { 658 | fprintf(stderr, "hackrf_sample_rate_set() failed: %s (%d)\n", 659 | hackrf_error_name(result), result); 660 | usage(); 661 | return EXIT_FAILURE; 662 | } 663 | 664 | fprintf(stderr, "call hackrf_baseband_filter_bandwidth_set(%.03f MHz)\n", 665 | ((float)DEFAULT_BASEBAND_FILTER_BANDWIDTH/(float)FREQ_ONE_MHZ)); 666 | result = hackrf_set_baseband_filter_bandwidth(device, DEFAULT_BASEBAND_FILTER_BANDWIDTH); 667 | if( result != HACKRF_SUCCESS ) { 668 | fprintf(stderr, "hackrf_baseband_filter_bandwidth_set() failed: %s (%d)\n", 669 | hackrf_error_name(result), result); 670 | usage(); 671 | return EXIT_FAILURE; 672 | } 673 | 674 | result = hackrf_set_vga_gain(device, vga_gain); 675 | result |= hackrf_set_lna_gain(device, lna_gain); 676 | 677 | /* 678 | * For each range, plan a whole number of tuning steps of a certain 679 | * bandwidth. Increase high end of range if necessary to accommodate a 680 | * whole number of steps, minimum 1. 681 | */ 682 | for(i = 0; i < num_ranges; i++) { 683 | step_count = 1 + (frequencies[2*i+1] - frequencies[2*i] - 1) 684 | / TUNE_STEP; 685 | frequencies[2*i+1] = (uint16_t) (frequencies[2*i] + step_count * TUNE_STEP); 686 | fprintf(stderr, "Sweeping from %u MHz to %u MHz\n", 687 | frequencies[2*i], frequencies[2*i+1]); 688 | } 689 | 690 | if(ifft_output) { 691 | ifftwIn = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * fftSize * step_count); 692 | ifftwOut = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * fftSize * step_count); 693 | ifftwPlan = fftwf_plan_dft_1d(fftSize * step_count, ifftwIn, ifftwOut, FFTW_BACKWARD, FFTW_MEASURE); 694 | } 695 | 696 | if (openlase_output) { 697 | openlaseBuf = (float*)malloc(sizeof(float) * fftSize * step_count); 698 | OLRenderParams params; 699 | 700 | memset(¶ms, 0, sizeof params); 701 | params.rate = 48000; 702 | params.on_speed = 2.0/100.0; 703 | params.off_speed = 2.0/20.0; 704 | params.start_wait = 8; 705 | params.start_dwell = 3; 706 | params.curve_dwell = 0; 707 | params.corner_dwell = 8; 708 | params.curve_angle = cosf(30.0*(M_PI/180.0)); // 30 deg 709 | params.end_dwell = 3; 710 | params.end_wait = 7; 711 | params.snap = 1/100000.0; 712 | params.render_flags = RENDER_GRAYSCALE; 713 | 714 | if(olInit(3, 30000) < 0) 715 | return EXIT_FAILURE; 716 | 717 | olSetRenderParams(¶ms); 718 | 719 | olBegin(OL_LINESTRIP); 720 | } 721 | 722 | result |= hackrf_start_rx(device, rx_callback, NULL); 723 | if (result != HACKRF_SUCCESS) { 724 | fprintf(stderr, "hackrf_start_rx() failed: %s (%d)\n", hackrf_error_name(result), result); 725 | usage(); 726 | return EXIT_FAILURE; 727 | } 728 | 729 | result = hackrf_init_sweep(device, frequencies, num_ranges, num_samples * 2, 730 | TUNE_STEP * FREQ_ONE_MHZ, OFFSET, INTERLEAVED); 731 | if( result != HACKRF_SUCCESS ) { 732 | fprintf(stderr, "hackrf_init_sweep() failed: %s (%d)\n", 733 | hackrf_error_name(result), result); 734 | return EXIT_FAILURE; 735 | } 736 | 737 | if (amp) { 738 | fprintf(stderr, "call hackrf_set_amp_enable(%u)\n", amp_enable); 739 | result = hackrf_set_amp_enable(device, (uint8_t)amp_enable); 740 | if (result != HACKRF_SUCCESS) { 741 | fprintf(stderr, "hackrf_set_amp_enable() failed: %s (%d)\n", 742 | hackrf_error_name(result), result); 743 | usage(); 744 | return EXIT_FAILURE; 745 | } 746 | } 747 | 748 | if (antenna) { 749 | fprintf(stderr, "call hackrf_set_antenna_enable(%u)\n", antenna_enable); 750 | result = hackrf_set_antenna_enable(device, (uint8_t)antenna_enable); 751 | if (result != HACKRF_SUCCESS) { 752 | fprintf(stderr, "hackrf_set_antenna_enable() failed: %s (%d)\n", 753 | hackrf_error_name(result), result); 754 | usage(); 755 | return EXIT_FAILURE; 756 | } 757 | } 758 | 759 | gettimeofday(&t_start, NULL); 760 | 761 | fprintf(stderr, "Stop with Ctrl-C\n"); 762 | while((hackrf_is_streaming(device) == HACKRF_TRUE) && (do_exit == false)) { 763 | float time_difference; 764 | sleep(1); 765 | 766 | gettimeofday(&time_now, NULL); 767 | 768 | time_difference = TimevalDiff(&time_now, &t_start); 769 | sweep_rate = (float)sweep_count / time_difference; 770 | fprintf(stderr, "%" PRIu64 " total sweeps completed, %.2f sweeps/second\n", 771 | sweep_count, sweep_rate); 772 | 773 | if (byte_count == 0) { 774 | exit_code = EXIT_FAILURE; 775 | fprintf(stderr, "\nCouldn't transfer any data for one second.\n"); 776 | break; 777 | } 778 | byte_count = 0; 779 | } 780 | 781 | result = hackrf_is_streaming(device); 782 | if (do_exit) { 783 | fprintf(stderr, "\nExiting...\n"); 784 | } else { 785 | fprintf(stderr, "\nExiting... hackrf_is_streaming() result: %s (%d)\n", 786 | hackrf_error_name(result), result); 787 | } 788 | 789 | gettimeofday(&time_now, NULL); 790 | time_diff = TimevalDiff(&time_now, &t_start); 791 | fprintf(stderr, "Total sweeps: %" PRIu64 " in %.5f seconds (%.2f sweeps/second)\n", 792 | sweep_count, time_diff, sweep_rate); 793 | 794 | if(device != NULL) { 795 | result = hackrf_stop_rx(device); 796 | if(result != HACKRF_SUCCESS) { 797 | fprintf(stderr, "hackrf_stop_rx() failed: %s (%d)\n", 798 | hackrf_error_name(result), result); 799 | } else { 800 | fprintf(stderr, "hackrf_stop_rx() done\n"); 801 | } 802 | 803 | result = hackrf_close(device); 804 | if(result != HACKRF_SUCCESS) { 805 | fprintf(stderr, "hackrf_close() failed: %s (%d)\n", 806 | hackrf_error_name(result), result); 807 | } else { 808 | fprintf(stderr, "hackrf_close() done\n"); 809 | } 810 | 811 | hackrf_exit(); 812 | fprintf(stderr, "hackrf_exit() done\n"); 813 | } 814 | 815 | if(fd != NULL) { 816 | fclose(fd); 817 | fd = NULL; 818 | fprintf(stderr, "fclose(fd) done\n"); 819 | } 820 | 821 | if (openlase_output) 822 | olShutdown(); 823 | 824 | fftwf_free(fftwIn); 825 | fftwf_free(fftwOut); 826 | fftwf_free(pwr); 827 | fftwf_free(window); 828 | fftwf_free(ifftwIn); 829 | fftwf_free(ifftwOut); 830 | fprintf(stderr, "exit\n"); 831 | return exit_code; 832 | } 833 | --------------------------------------------------------------------------------