Debian Codegraph @

tools/src/airspyhf_rx.c

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * Copyright 2012 Jared Boone <[email protected]>
3 * Copyright 2014-2015 Benjamin Vernoux <[email protected]>
4 * Copyright 2018 Andrea Montefusco IW0HDV <[email protected]>
5 *
6 * This file is part of AirSpyHF (based on HackRF project).
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
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <getopt.h>
29 #include <time.h>
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <limits.h>
36
37 #include <airspyhf.h>
38
39 #if !defined __cplusplus
40 #ifndef bool
41 typedef int bool;
42 #define true 1
43 #define false 0
44 #endif
45 #endif
46
47 #ifdef _WIN32
48 #include <windows.h>
49
50 #ifdef _MSC_VER
51
52 #ifdef _WIN64
53 typedef int64_t ssize_t;
54 #else
55 typedef int32_t ssize_t;
56 #endif
57
58 #define strtoull _strtoui64
59 #define snprintf _snprintf
60
61 int gettimeofday(struct timeval *tv, void* ignored)
62 {
63 FILETIME ft;
64 unsigned __int64 tmp = 0;
65 if (NULL != tv) {
66 GetSystemTimeAsFileTime(&ft);
67 tmp |= ft.dwHighDateTime;
68 tmp <<= 32;
69 tmp |= ft.dwLowDateTime;
70 tmp /= 10;
71 tmp -= 11644473600000000Ui64;
72 tv->tv_sec = (long)(tmp / 1000000UL);
73 tv->tv_usec = (long)(tmp % 1000000UL);
74 }
75 return 0;
76 }
77
78 #endif
79 #endif
80
81 #if defined(__GNUC__)
82 #include <unistd.h>
83 #include <sys/time.h>
84 #endif
85
86 #include <signal.h>
87
88 #if defined _WIN32
89 #define sleep(a) Sleep( (a*1000) )
90 #endif
91
92 #define FREQ_ONE_MHZ (1000000ul)
93 #define FREQ_ONE_MHZ_U64 (1000000ull)
94 #define FD_BUFFER_SIZE (16*1024)
95 #define DEFAULT_FREQ_HZ (7100000ul) /* 7.1 MHz */
96 #define SAMPLES_TO_XFER_MAX_U64 (0x8000000000000000ull) /* Max value */
97
98 /* WAVE or RIFF WAVE file format containing data for AirSpy compatible with SDR# Wav IQ file */
99 typedef struct
100 {
101 char groupID[4]; /* 'RIFF' */
102 uint32_t size; /* File size + 8bytes */
103 char riffType[4]; /* 'WAVE'*/
104 } t_WAVRIFF_hdr;
105
106 #define FormatID "fmt " /* chunkID for Format Chunk. NOTE: There is a space at the end of this ID. */
107
108 typedef struct {
109 char chunkID[4]; /* 'fmt ' */
110 uint32_t chunkSize; /* 16 fixed */
111
112 uint16_t wFormatTag; /* 1=PCM8/16, 3=Float32 */
113 uint16_t wChannels;
114 uint32_t dwSamplesPerSec; /* Freq Hz sampling */
115 uint32_t dwAvgBytesPerSec; /* Freq Hz sampling x 2 */
116 uint16_t wBlockAlign;
117 uint16_t wBitsPerSample;
118 } t_FormatChunk;
119
120 typedef struct
121 {
122 char chunkID[4]; /* 'data' */
123 uint32_t chunkSize; /* Size of data in bytes */
124 /* For IQ samples I(16 or 32bits) then Q(16 or 32bits), I, Q ... */
125 } t_DataChunk;
126
127 typedef struct
128 {
129 t_WAVRIFF_hdr hdr;
130 t_FormatChunk fmt_chunk;
131 t_DataChunk data_chunk;
132 } t_wav_file_hdr;
133
134 t_wav_file_hdr wave_file_hdr =
135 {
136 /* t_WAVRIFF_hdr */
137 {
138 { 'R', 'I', 'F', 'F' }, /* groupID */
139 0, /* size to update later */
140 { 'W', 'A', 'V', 'E' }
141 },
142 /* t_FormatChunk */
143 {
144 { 'f', 'm', 't', ' ' }, /* char chunkID[4]; */
145 16, /* uint32_t chunkSize; */
146 0, /* uint16_t wFormatTag; to update later */
147 0, /* uint16_t wChannels; to update later */
148 0, /* uint32_t dwSamplesPerSec; Freq Hz sampling to update later */
149 0, /* uint32_t dwAvgBytesPerSec; to update later */
150 0, /* uint16_t wBlockAlign; to update later */
151 0, /* uint16_t wBitsPerSample; to update later */
152 },
153 /* t_DataChunk */
154 {
155 { 'd', 'a', 't', 'a' }, /* char chunkID[4]; */
156 0, /* uint32_t chunkSize; to update later */
157 }
158 };
159
160 #define U64TOA_MAX_DIGIT (31)
161 typedef struct
162 {
163 char data[U64TOA_MAX_DIGIT+1];
164 } t_u64toa;
165
166
167
168 // globals used both in callback and main
169
170 volatile bool do_exit = false;
171
172 FILE* fd = NULL;
173
174 bool verbose = false;
175 bool receive = false;
176 bool receive_wav = false;
177 bool limit_num_samples = false;
178 uint64_t samples_to_xfer = 0;
179 uint64_t bytes_to_xfer = 0;
180
181 struct timeval time_start;
182 struct timeval t_start;
183
184 bool got_first_packet = false;
185 float average_rate = 0.0f;
186 float global_average_rate = 0.0f;
187 uint32_t rate_samples = 0;
188 uint32_t buffer_count = 0;
189 uint32_t sample_count = 0;
190
191
192 static float
193 TimevalDiff(const struct timeval *a, const struct timeval *b)
194 {
195 return (a->tv_sec - b->tv_sec) + 1e-6f * (a->tv_usec - b->tv_usec);
196 }
197
198 int parse_u64(char* s, uint64_t* const value) {
199 uint_fast8_t base = 10;
200 char* s_end;
201 uint64_t u64_value;
202
203 if( strlen(s) > 2 ) {
204 if( s[0] == '0' ) {
205 if( (s[1] == 'x') || (s[1] == 'X') ) {
206 base = 16;
207 s += 2;
208 } else if( (s[1] == 'b') || (s[1] == 'B') ) {
209 base = 2;
210 s += 2;
211 }
212 }
213 }
214
215 s_end = s;
216 u64_value = strtoull(s, &s_end, base);
217 if( (s != s_end) && (*s_end == 0) ) {
218 *value = u64_value;
219 return AIRSPYHF_SUCCESS;
220 } else {
221 return AIRSPYHF_ERROR;
222 }
223 }
224
225 int parse_u32(char* s, uint32_t* const value)
226 {
227 uint_fast8_t base = 10;
228 char* s_end;
229 uint64_t ulong_value;
230
231 if( strlen(s) > 2 ) {
232 if( s[0] == '0' ) {
233 if( (s[1] == 'x') || (s[1] == 'X') ) {
234 base = 16;
235 s += 2;
236 } else if( (s[1] == 'b') || (s[1] == 'B') ) {
237 base = 2;
238 s += 2;
239 }
240 }
241 }
242
243 s_end = s;
244 ulong_value = strtoul(s, &s_end, base);
245 if( (s != s_end) && (*s_end == 0) ) {
246 *value = (uint32_t)ulong_value;
247 return AIRSPYHF_SUCCESS;
248 } else {
249 return AIRSPYHF_ERROR;
250 }
251 }
252
253 static char *stringrev(char *str)
254 {
255 char *p1, *p2;
256
257 if(! str || ! *str)
258 return str;
259
260 for(p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
261 {
262 *p1 ^= *p2;
263 *p2 ^= *p1;
264 *p1 ^= *p2;
265 }
266 return str;
267 }
268
269 char* u64toa(uint64_t val, t_u64toa* str)
270 {
271 #define BASE (10ull) /* Base10 by default */
272 uint64_t sum;
273 int pos;
274 int digit;
275 int max_len;
276 char* res;
277
278 sum = val;
279 max_len = U64TOA_MAX_DIGIT;
280 pos = 0;
281
282 do {
283 digit = (sum % BASE);
284 str->data[pos] = digit + '0';
285 pos++;
286
287 sum /= BASE;
288 } while( (sum>0) && (pos < max_len) );
289
290 if( (pos == max_len) && (sum>0) )
291 return NULL;
292
293 str->data[pos] = '\0';
294 res = stringrev(str->data);
295
296 return res;
297 }
298
299 int rx_callback(airspyhf_transfer_t* transfer)
300 {
301 uint32_t bytes_to_write;
302 void* pt_rx_buffer;
303 ssize_t bytes_written;
304 struct timeval time_now;
305 float time_difference, rate;
306
307 if( fd ) {
308 // #sample * float size * I+Q
309 bytes_to_write = transfer->sample_count * 4 * 2;
310 pt_rx_buffer = transfer->samples;
311
312 gettimeofday(&time_now, NULL);
313
314 if (!got_first_packet) {
315 t_start = time_now;
316 time_start = time_now;
317 got_first_packet = true;
318 } else {
319 buffer_count++;
320 sample_count += transfer->sample_count;
321 if (buffer_count == 50) {
322 time_difference = TimevalDiff(&time_now, &time_start);
323 rate = (float) sample_count / time_difference;
324 average_rate += 0.2f * (rate - average_rate);
325 global_average_rate += average_rate;
326 rate_samples++;
327 time_start = time_now;
328 sample_count = 0;
329 buffer_count = 0;
330 }
331 }
332
333 if (limit_num_samples) {
334 if (bytes_to_write >= bytes_to_xfer) {
335 bytes_to_write = (int)bytes_to_xfer;
336 }
337 bytes_to_xfer -= bytes_to_write;
338 }
339
340 if(pt_rx_buffer) {
341 bytes_written = fwrite(pt_rx_buffer, 1, bytes_to_write, fd);
342 } else {
343 bytes_written = 0;
344 }
345 if ( (bytes_written != bytes_to_write) ||
346 ((limit_num_samples == true) && (bytes_to_xfer == 0))
347 )
348 return -1;
349 else
350 return 0;
351 } else {
352 return -1;
353 }
354 }
355
356 static void usage(void)
357 {
358 fprintf(stderr,
359 "airspyhf_rx\n"
360 "Usage:\n"
361
362 "\t-r <filename>\t\tReceive data into the file;\n"
363 "\t\t\t\tstdout emits values on standard output\n"
364
365 "\t-s <serial number>\tOpen device with specified 64bits serial number\n"
366
367 "\t-f <frequency>\t\tSet frequency in MHz between 9 kHz - 31 MHz or 60 - 260 MHz\n"
368 "\t-a <sample_rate>\tSet sample rate, at the moment only 768 kS/s supported\n"
369
370 "\t-n <#samples>\t\tNumber of samples to transfer (default is unlimited)\n"
371
372 "\t-d\t\t\tVerbose mode\n"
373
374 "\t-w\t\t\tReceive data into file with WAV header and automatic name\n"
375 "\t\t\t\tThis is for SDR# compatibility and may not work with other software\n"
376 "\t\t\t\tIt works even with HDSDR\n"
377
378 "\t-g on|off\t\tHF AGC on / off\n"
379 "\t-l high|low\t\tHF AGC threshold high / low (when AGC On)\n"
380 "\t-t <value>\t\tHF attenuator value 0..8 (each step increases 6 dB the attenuation)\n"
381 "\t-m on|off\t\ton to activate LNA (preamplifier): +6 dB gain - compensated in digital.\n"
382
383 "\t-z\t\t\tDo not attempt to use manual AGC/LNA commands\n"
384 "\t\t\t\t(useful in order to avoid errors with old firmware)\n"
385
386 );
387 }
388
389
390 #ifdef _MSC_VER
391 BOOL WINAPI
392 sighandler(int signum)
393 {
394 if (CTRL_C_EVENT == signum) {
395 fprintf(stdout, "Caught signal %d\n", signum);
396 do_exit = true;
397 return TRUE;
398 }
399 return FALSE;
400 }
401 #else
402 void sigint_callback_handler(int signum)
403 {
404 fprintf(stdout, "Caught signal %d\n", signum);
405 do_exit = true;
406 }
407 #endif
408
409
410 int main(int argc, char** argv)
411 {
412 int opt;
413 t_u64toa ascii_u64_data1;
414 t_u64toa ascii_u64_data2;
415 const char* path = 0;
416 int result;
417
418 struct timeval t_end;
419 uint32_t file_pos;
420 uint32_t wav_sample_per_sec;
421 uint32_t nsrates;
422 uint32_t *supported_samplerates;
423 uint32_t sample_rate_u32 = 768000;
424
425 struct airspyhf_device* device = 0;
426
427 bool freq = false;
428 uint32_t freq_hz = 0;
429 bool sample_rate = true;
430 uint32_t sample_rate_val = 0;
431 bool serial_number = false;
432 uint64_t serial_number_val;
433
434 bool hf_agc = true;
435 bool hf_agc_threshold = false; // false = low, true = high
436 unsigned hf_att_val = 0;
437 bool hf_lna = false; // false = off, true = on
438
439 bool do_not_use_manual_commands = false;
440
441 while( (opt = getopt(argc, argv, "r:ws:f:a:n:g:l:t:m:dhz")) != EOF )
442 {
443 result = AIRSPYHF_SUCCESS;
444 switch( opt )
445 {
446 case 'r':
447 receive = true;
448 path = optarg;
449 break;
450
451 case 'w':
452 receive_wav = true;
453 break;
454
455 case 's':
456 serial_number = true;
457 result = parse_u64(optarg, &serial_number_val);
458 break;
459
460 case 'f':
461 {
462 float freq_mhz_f;
463 if (sscanf(optarg, "%f", &freq_mhz_f) == 1) {
464 freq = true;
465 freq_hz = freq_mhz_f * 1000000; // convert MHz to Hz
466 }
467 }
468 break;
469
470 case 'a': /* Sample rate */
471 sample_rate = true;
472 result = parse_u32(optarg, &sample_rate_u32);
473 break;
474
475 case 'n':
476 limit_num_samples = true;
477 result = parse_u64(optarg, &samples_to_xfer);
478 break;
479
480 case 'g':
481 if (strcmp(optarg, "off") == 0) hf_agc = false;
482 break;
483
484 case 'l':
485 fprintf (stderr, "************** [%s]\n", optarg);
486 if (strcmp(optarg, "high") == 0) hf_agc_threshold = true;
487 break;
488
489 case 't':
490 if (sscanf(optarg, "%d", &hf_att_val) == 1) {
491 if (!(hf_att_val >=0 && hf_att_val<= 8)) {
492 hf_att_val = 0;
493 fprintf (stderr, "Bad HF attenuator value.\n");
494 goto exit_usage;
495 }
496 }
497 break;
498
499 case 'm':
500 if (strcmp(optarg, "on") == 0) hf_lna = true;
501 break;
502
503 case 'd':
504 verbose = true;
505 break;
506
507 case 'h':
508 goto exit_usage;
509
510 case 'z':
511 do_not_use_manual_commands = true;
512 break;
513
514 default:
515 fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg);
516 goto exit_usage;
517 }
518
519 if( result != AIRSPYHF_SUCCESS ) {
520 fprintf(stderr, "argument error: '-%c %s'\n", opt, optarg);
521 goto exit_usage;
522 }
523 }
524
525 if (sample_rate) sample_rate_val = sample_rate_u32;
526
527 bytes_to_xfer = samples_to_xfer * (32 * 2 / 8); // 32 bits per sample / 2 channels (I+Q) / 8 bits per byte
528
529 if (samples_to_xfer >= SAMPLES_TO_XFER_MAX_U64) {
530 fprintf(stderr, "argument error: num_samples must be less than %s/%sMio\n",
531 u64toa(SAMPLES_TO_XFER_MAX_U64, &ascii_u64_data1),
532 u64toa((SAMPLES_TO_XFER_MAX_U64/FREQ_ONE_MHZ_U64), &ascii_u64_data2) );
533 goto exit_usage;
534 }
535
536
537 /*
538 * HF coverage between 9 kHz .. 31 MHz
539 * VHF coverage between 60 .. 260 MHz
540 */
541 if( freq ) { // check frequncy value as specified on the command line
542
543 if( !( ((freq_hz >= 9000) && (freq_hz <= 31000000))
544 ||
545 ((freq_hz >= 60000000 && freq_hz <= 260000000))) ) {
546
547 fprintf(stderr, "argument error: frequency %d Hz out of range\n", freq_hz);
548 goto exit_usage;
549 }
550 } else {
551 /* Use default freq */
552 freq_hz = DEFAULT_FREQ_HZ;
553 }
554
555 if( receive_wav ) {
556 time_t rawtime;
557 struct tm * timeinfo;
558 char date_time[64];
559 char path_file[256];
560
561 time (&rawtime);
562 timeinfo = localtime (&rawtime);
563 /* File format AirSpy Year(2013), Month(11), Day(28), Hour Min Sec+Z, Freq kHz, IQ.wav */
564 strftime(date_time, sizeof(date_time), "%Y%m%d_%H%M%S", timeinfo);
565 snprintf(path_file, sizeof(path_file), "AirSpy_%sZ_%ukHz_IQ.wav", date_time, (uint32_t)(freq_hz/(1000ull)) );
566 path = path_file;
567 fprintf(stderr, "Receive wav file: [%s]\n", path);
568 }
569
570 if( path == 0 ) {
571 fprintf(stderr, "error: you shall specify at least -r <with filename> or -w option\n");
572 goto exit_usage;
573 }
574
575
576 if(verbose == true) {
577 uint32_t serial_number_msb_val;
578 uint32_t serial_number_lsb_val;
579
580 fprintf(stderr, "airspyhf_rx\n");
581 serial_number_msb_val = (uint32_t)(serial_number_val >> 32);
582 serial_number_lsb_val = (uint32_t)(serial_number_val & 0xFFFFFFFF);
583 if(serial_number)
584 fprintf(stderr, "S/N 0x%08X%08X\n", serial_number_msb_val, serial_number_lsb_val);
585 fprintf(stderr, "Frequency: -f %.6fMHz (%d Hz)\n",((double)freq_hz/(double)FREQ_ONE_MHZ), freq_hz );
586
587 if( limit_num_samples ) {
588 fprintf(stderr, "#samples -n %s (%sM)\n",
589 u64toa(samples_to_xfer, &ascii_u64_data1),
590 u64toa((samples_to_xfer/FREQ_ONE_MHZ), &ascii_u64_data2));
591 }
592 }
593
594 if(serial_number == true) {
595 if( airspyhf_open_sn(&device, serial_number_val) != AIRSPYHF_SUCCESS ) {
596 fprintf(stderr, "airspyhf_open_sn() failed\n");
597 goto exit_failure;
598 }
599 } else {
600 if( airspyhf_open(&device) != AIRSPYHF_SUCCESS ) {
601 fprintf(stderr, "airspyhf_open() failed\n");
602 goto exit_failure;
603 }
604 }
605
606 // check sample rates
607 airspyhf_get_samplerates(device, &nsrates, 0);
608 supported_samplerates = (uint32_t *) malloc(nsrates * sizeof(uint32_t));
609 airspyhf_get_samplerates(device, supported_samplerates, nsrates);
610
611 wav_sample_per_sec = 0;
612 if (sample_rate == true) {
613 int s;
614 uint32_t *ps = supported_samplerates;
615 // search into the available sample rates
616 for (s=0; s < nsrates; ++s, ++ps)
617 if (sample_rate_val == *ps) {
618 wav_sample_per_sec = sample_rate_val;
619 break;
620 }
621 } else {
622 wav_sample_per_sec = *supported_samplerates;
623 }
624 free(supported_samplerates);
625
626 // if arrives here an unsupported sample rate was reqeusted
627 if (wav_sample_per_sec == 0) {
628 fprintf(stderr, "argument error: unsupported sample rate: %d\n", sample_rate_val);
629 goto exit_failure;
630 }
631 // set sample rate
632 if (airspyhf_set_samplerate(device, wav_sample_per_sec) != AIRSPYHF_SUCCESS) {
633 fprintf(stderr, "airspyhf_set_samplerate() failed: %d\n", wav_sample_per_sec);
634 goto exit_failure;
635 }
636
637 if (verbose) {
638 fprintf(stderr, "%f MS/s %s\n", wav_sample_per_sec * 0.000001f, "IQ");
639 }
640
641 { // print receiver serial number
642 airspyhf_read_partid_serialno_t read_partid_serialno;
643
644 if (airspyhf_board_partid_serialno_read(device, &read_partid_serialno) != AIRSPYHF_SUCCESS) {
645 fprintf(stderr, "airspyhf_board_partid_serialno_read() failed\n");
646 goto exit_failure;
647 } else
648 fprintf(stderr, "Device Serial Number: 0x%08X%08X\n",
649 read_partid_serialno.serial_no[0],
650 read_partid_serialno.serial_no[1]);
651 }
652
653 // manual commands
654 if (do_not_use_manual_commands == false) {
655 /* 0 = off, 1 = on */
656 if (airspyhf_set_hf_agc(device, (hf_agc == true) ? 1:0) == AIRSPYHF_SUCCESS) {
657 fprintf (stderr, "HF AGC %s\n", (hf_agc == true) ? "ON":"OFF");
658 } else {
659 fprintf(stderr, "airspyhf_set_hf_agc failed.\n");
660 goto exit_failure;
661 }
662
663 if (hf_agc == true) {
664 /* when agc on: 0 = low, 1 = high */
665 if (airspyhf_set_hf_agc_threshold (device, hf_agc_threshold == true?1:0) == AIRSPYHF_SUCCESS) {
666 fprintf (stderr, "HF AGC threshold %s\n", (hf_agc_threshold == true) ? "High":"Low");
667 } else {
668 fprintf(stderr, "airspyhf_set_agc_threshold() failed.\n");
669 goto exit_failure;
670 }
671 } else {
672 fprintf (stderr, "HF AGC threshold ignored as AGC is disabled.\n");
673 }
674
675 if (hf_agc == false) {
676 /* when agc off: 0 .. 8 with attenuation = (value * 8) dB */
677 if (airspyhf_set_hf_att(device, hf_att_val) == AIRSPYHF_SUCCESS) {
678 fprintf (stderr, "HF Attenuator value: -%d dB\n", hf_att_val*6);
679 } else {
680 fprintf(stderr, "airspyhf_set_hf_att() failed: offending value: %d\n", hf_att_val);
681 goto exit_failure;
682 }
683 } else {
684 fprintf (stderr, "HF attenuator value ignored as AGC is enabled.\n");
685 }
686 /* 0 or 1: 1 to activate LNA (alias PreAmp): 1 = +6 dB gain - compensated in digital */
687 if (airspyhf_set_hf_lna(device, hf_lna == false?0:1) == AIRSPYHF_SUCCESS) {
688 fprintf (stderr, "HF LNA %s\n", (hf_lna == true) ? "ON":"OFF");
689 } else {
690 fprintf(stderr, "airspyhf_set_hf_lna() failed.\n");
691 goto exit_failure;
692 }
693 }
694
695 // output file management
696 if (strcmp (path, "stdout") == 0) {
697 fd = stdout;
698 } else {
699 if( !(fd = fopen(path, "wb")) ) {
700 perror (path);
701 goto exit_failure;
702 }
703 }
704
705 /* Change fd buffer to have bigger one to store data to file */
706 if( setvbuf(fd , NULL , _IOFBF , FD_BUFFER_SIZE) != 0 ) {
707 perror("setvbuf() failed:");
708 goto exit_failure;
709 }
710
711 /* Write Wav header */
712 if( receive_wav ) fwrite(&wave_file_hdr, 1, sizeof(t_wav_file_hdr), fd);
713
714
715 #ifdef _MSC_VER
716 SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
717 #else
718 signal(SIGINT, &sigint_callback_handler);
719 signal(SIGILL, &sigint_callback_handler);
720 signal(SIGFPE, &sigint_callback_handler);
721 signal(SIGSEGV, &sigint_callback_handler);
722 signal(SIGTERM, &sigint_callback_handler);
723 signal(SIGABRT, &sigint_callback_handler);
724 #endif
725
726 if( airspyhf_start(device, rx_callback, NULL) != AIRSPYHF_SUCCESS ) {
727 fprintf(stderr, "airspyhf_start() failed.\n");
728 goto exit_failure;
729 }
730
731 if( airspyhf_set_freq(device, freq_hz) != AIRSPYHF_SUCCESS ) {
732 fprintf(stderr, "airspyhf_set_freq() failed.\n");
733 goto exit_failure;
734 }
735
736 /*
737 * main loop
738 */
739 fprintf(stderr, "Stop with Ctrl-C\n");
740
741 average_rate = (float) wav_sample_per_sec;
742
743 sleep(1);
744
745 while( (airspyhf_is_streaming(device) == true) &&
746 (do_exit == false) )
747 {
748 char buf [64];
749 float average_rate_now = average_rate * 1e-6f;
750
751 if (verbose) {
752 snprintf(buf, sizeof(buf),"%2.3f", average_rate_now);
753 //average_rate_now = 9.5f;
754 fprintf(stderr, "Streaming at %5s MS/s\n", buf);
755 }
756 if ((limit_num_samples == true) && (bytes_to_xfer == 0))
757 do_exit = true;
758 else
759 sleep(1);
760 }
761
762 if (do_exit) {
763 fprintf(stderr, "\nUser cancel, exiting...\n");
764 } else {
765 fprintf(stderr,"\nExiting...\n");
766 }
767 // print time elapsed
768 {
769 float time_diff;
770 gettimeofday (&t_end, 0);
771 time_diff = TimevalDiff(&t_end, &t_start);
772 fprintf(stderr, "Total time: %5.4f s\n", time_diff);
773 }
774
775 if (rate_samples > 0)
776 fprintf(stderr, "Average speed %2.4f MS/s %s\n", (global_average_rate * 1e-6f / rate_samples), "IQ");
777
778 if(device) {
779 if( airspyhf_stop(device) != AIRSPYHF_SUCCESS ) {
780 fprintf(stderr, "airspyhf_stop() failed\n");
781 }
782
783 if( airspyhf_close(device) != AIRSPYHF_SUCCESS ) {
784 fprintf(stderr,"airspyhf_close() failed\n");
785 }
786 }
787
788 if (fd && receive_wav ) {
789 /* Get size of file */
790 file_pos = ftell(fd);
791 /* Wav Header */
792 wave_file_hdr.hdr.size = file_pos - 8;
793 /* Wav Format Chunk */
794 wave_file_hdr.fmt_chunk.wFormatTag = 3; // 3=Float32
795 wave_file_hdr.fmt_chunk.wChannels = 2; // I + Q
796 wave_file_hdr.fmt_chunk.dwSamplesPerSec = wav_sample_per_sec;
797 wave_file_hdr.fmt_chunk.dwAvgBytesPerSec = wave_file_hdr.fmt_chunk.dwSamplesPerSec * 4;
798 wave_file_hdr.fmt_chunk.wBlockAlign = 2 * (32 / 8);
799 wave_file_hdr.fmt_chunk.wBitsPerSample = 32;
800 /* Wav Data Chunk */
801 wave_file_hdr.data_chunk.chunkSize = file_pos - sizeof(t_wav_file_hdr);
802 /* Overwrite header with updated data */
803 rewind(fd);
804 fwrite(&wave_file_hdr, 1, sizeof(t_wav_file_hdr), fd);
805 }
806 if (fd != stdout) fclose(fd);
807 fprintf(stderr, "done\n");
808 return EXIT_SUCCESS;
809
810 exit_failure:
811 airspyhf_close(device);
812 return EXIT_FAILURE;
813
814 exit_usage:
815 usage();
816 return EXIT_FAILURE;
817 }