/
hackrf_transfer.c
1075 lines (930 loc) · 27.4 KB
/
hackrf_transfer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2012 Jared Boone <jared@sharebrained.com>
* Copyright 2013-2014 Benjamin Vernoux <titanmkd@gmail.com>
*
* This file is part of HackRF.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include <hackrf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#ifndef bool
typedef int bool;
#define true 1
#define false 0
#endif
#ifdef _WIN32
#include <windows.h>
#ifdef _MSC_VER
#ifdef _WIN64
typedef int64_t ssize_t;
#else
typedef int32_t ssize_t;
#endif
#define strtoull _strtoui64
#define snprintf _snprintf
int gettimeofday(struct timeval *tv, void* ignored)
{
FILETIME ft;
unsigned __int64 tmp = 0;
if (NULL != tv) {
GetSystemTimeAsFileTime(&ft);
tmp |= ft.dwHighDateTime;
tmp <<= 32;
tmp |= ft.dwLowDateTime;
tmp /= 10;
tmp -= 11644473600000000Ui64;
tv->tv_sec = (long)(tmp / 1000000UL);
tv->tv_usec = (long)(tmp % 1000000UL);
}
return 0;
}
#endif
#endif
#if defined(__GNUC__)
#include <unistd.h>
#include <sys/time.h>
#endif
#include <signal.h>
#define FD_BUFFER_SIZE (8*1024)
#define FREQ_ONE_MHZ (1000000ull)
#define DEFAULT_FREQ_HZ (900000000ull) /* 900MHz */
#define FREQ_MIN_HZ (0ull) /* 0 Hz */
#define FREQ_MAX_HZ (7250000000ull) /* 7250MHz */
#define IF_MIN_HZ (2150000000ull)
#define IF_MAX_HZ (2750000000ull)
#define LO_MIN_HZ (84375000ull)
#define LO_MAX_HZ (5400000000ull)
#define DEFAULT_LO_HZ (1000000000ull)
#define DEFAULT_SAMPLE_RATE_HZ (10000000) /* 10MHz default sample rate */
#define DEFAULT_BASEBAND_FILTER_BANDWIDTH (5000000) /* 5MHz default */
#define SAMPLES_TO_XFER_MAX (0x8000000000000000ull) /* Max value */
#define BASEBAND_FILTER_BW_MIN (1750000) /* 1.75 MHz min value */
#define BASEBAND_FILTER_BW_MAX (28000000) /* 28 MHz max value */
#if defined _WIN32
#define sleep(a) Sleep( (a*1000) )
#endif
/* WAVE or RIFF WAVE file format containing IQ 2x8bits data for HackRF compatible with SDR# Wav IQ file */
typedef struct
{
char groupID[4]; /* 'RIFF' */
uint32_t size; /* File size + 8bytes */
char riffType[4]; /* 'WAVE'*/
} t_WAVRIFF_hdr;
#define FormatID "fmt " /* chunkID for Format Chunk. NOTE: There is a space at the end of this ID. */
typedef struct {
char chunkID[4]; /* 'fmt ' */
uint32_t chunkSize; /* 16 fixed */
uint16_t wFormatTag; /* 1 fixed */
uint16_t wChannels; /* 2 fixed */
uint32_t dwSamplesPerSec; /* Freq Hz sampling */
uint32_t dwAvgBytesPerSec; /* Freq Hz sampling x 2 */
uint16_t wBlockAlign; /* 2 fixed */
uint16_t wBitsPerSample; /* 8 fixed */
} t_FormatChunk;
typedef struct
{
char chunkID[4]; /* 'data' */
uint32_t chunkSize; /* Size of data in bytes */
/* Samples I(8bits) then Q(8bits), I, Q ... */
} t_DataChunk;
typedef struct
{
t_WAVRIFF_hdr hdr;
t_FormatChunk fmt_chunk;
t_DataChunk data_chunk;
} t_wav_file_hdr;
t_wav_file_hdr wave_file_hdr =
{
/* t_WAVRIFF_hdr */
{
{ 'R', 'I', 'F', 'F' }, /* groupID */
0, /* size to update later */
{ 'W', 'A', 'V', 'E' }
},
/* t_FormatChunk */
{
{ 'f', 'm', 't', ' ' }, /* char chunkID[4]; */
16, /* uint32_t chunkSize; */
1, /* uint16_t wFormatTag; 1 fixed */
2, /* uint16_t wChannels; 2 fixed */
0, /* uint32_t dwSamplesPerSec; Freq Hz sampling to update later */
0, /* uint32_t dwAvgBytesPerSec; Freq Hz sampling x 2 to update later */
2, /* uint16_t wBlockAlign; 2 fixed */
8, /* uint16_t wBitsPerSample; 8 fixed */
},
/* t_DataChunk */
{
{ 'd', 'a', 't', 'a' }, /* char chunkID[4]; */
0, /* uint32_t chunkSize; to update later */
}
};
static transceiver_mode_t transceiver_mode = TRANSCEIVER_MODE_RX;
#define U64TOA_MAX_DIGIT (31)
typedef struct
{
char data[U64TOA_MAX_DIGIT+1];
} t_u64toa;
t_u64toa ascii_u64_data1;
t_u64toa ascii_u64_data2;
static float
TimevalDiff(const struct timeval *a, const struct timeval *b)
{
return (a->tv_sec - b->tv_sec) + 1e-6f * (a->tv_usec - b->tv_usec);
}
int parse_u64(char* s, uint64_t* const value) {
uint_fast8_t base = 10;
char* s_end;
uint64_t u64_value;
if( strlen(s) > 2 ) {
if( s[0] == '0' ) {
if( (s[1] == 'x') || (s[1] == 'X') ) {
base = 16;
s += 2;
} else if( (s[1] == 'b') || (s[1] == 'B') ) {
base = 2;
s += 2;
}
}
}
s_end = s;
u64_value = strtoull(s, &s_end, base);
if( (s != s_end) && (*s_end == 0) ) {
*value = u64_value;
return HACKRF_SUCCESS;
} else {
return HACKRF_ERROR_INVALID_PARAM;
}
}
int parse_u32(char* s, uint32_t* const value) {
uint_fast8_t base = 10;
char* s_end;
uint64_t ulong_value;
if( strlen(s) > 2 ) {
if( s[0] == '0' ) {
if( (s[1] == 'x') || (s[1] == 'X') ) {
base = 16;
s += 2;
} else if( (s[1] == 'b') || (s[1] == 'B') ) {
base = 2;
s += 2;
}
}
}
s_end = s;
ulong_value = strtoul(s, &s_end, base);
if( (s != s_end) && (*s_end == 0) ) {
*value = (uint32_t)ulong_value;
return HACKRF_SUCCESS;
} else {
return HACKRF_ERROR_INVALID_PARAM;
}
}
static char *stringrev(char *str)
{
char *p1, *p2;
if(! str || ! *str)
return str;
for(p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
char* u64toa(uint64_t val, t_u64toa* str)
{
#define BASE (10ull) /* Base10 by default */
uint64_t sum;
int pos;
int digit;
int max_len;
char* res;
sum = val;
max_len = U64TOA_MAX_DIGIT;
pos = 0;
do
{
digit = (sum % BASE);
str->data[pos] = digit + '0';
pos++;
sum /= BASE;
}while( (sum>0) && (pos < max_len) );
if( (pos == max_len) && (sum>0) )
return NULL;
str->data[pos] = '\0';
res = stringrev(str->data);
return res;
}
volatile bool do_exit = false;
FILE* fd = NULL;
volatile uint32_t byte_count = 0;
bool signalsource = false;
uint32_t amplitude = 0;
bool receive = false;
bool receive_wav = false;
bool transmit = false;
struct timeval time_start;
struct timeval t_start;
bool automatic_tuning = false;
uint64_t freq_hz;
bool if_freq = false;
uint64_t if_freq_hz;
bool lo_freq = false;
uint64_t lo_freq_hz = DEFAULT_LO_HZ;
bool image_reject = false;
uint32_t image_reject_selection;
bool amp = false;
uint32_t amp_enable;
bool antenna = false;
uint32_t antenna_enable;
bool sample_rate = false;
uint32_t sample_rate_hz;
bool limit_num_samples = false;
uint64_t samples_to_xfer = 0;
ssize_t bytes_to_xfer = 0;
bool baseband_filter_bw = false;
uint32_t baseband_filter_bw_hz = 0;
bool repeat = false;
bool crystal_correct = false;
uint32_t crystal_correct_ppm ;
int rx_callback(hackrf_transfer* transfer) {
ssize_t bytes_to_write;
ssize_t bytes_written;
int i;
if( fd != NULL )
{
byte_count += transfer->valid_length;
bytes_to_write = transfer->valid_length;
if (limit_num_samples) {
if (bytes_to_write >= bytes_to_xfer) {
bytes_to_write = bytes_to_xfer;
}
bytes_to_xfer -= bytes_to_write;
}
if (receive_wav) {
/* convert .wav contents from signed to unsigned */
for (i = 0; i < bytes_to_write; i++) {
transfer->buffer[i] ^= (uint8_t)0x80;
}
}
bytes_written = fwrite(transfer->buffer, 1, bytes_to_write, fd);
if ((bytes_written != bytes_to_write)
|| (limit_num_samples && (bytes_to_xfer == 0))) {
return -1;
} else {
return 0;
}
} else {
return -1;
}
}
int tx_callback(hackrf_transfer* transfer) {
ssize_t bytes_to_read;
ssize_t bytes_read;
int i;
if( fd != NULL )
{
byte_count += transfer->valid_length;
bytes_to_read = transfer->valid_length;
if (limit_num_samples) {
if (bytes_to_read >= bytes_to_xfer) {
/*
* In this condition, we probably tx some of the previous
* buffer contents at the end. :-(
*/
bytes_to_read = bytes_to_xfer;
}
bytes_to_xfer -= bytes_to_read;
}
bytes_read = fread(transfer->buffer, 1, bytes_to_read, fd);
if ((bytes_read != bytes_to_read)
|| (limit_num_samples && (bytes_to_xfer == 0))) {
if (repeat) {
printf("Input file end reached. Rewind to beginning.\n");
rewind(fd);
fread(transfer->buffer + bytes_read, 1, bytes_to_read - bytes_read, fd);
return 0;
} else {
return -1; // not loopback mode, EOF
}
} else {
return 0;
}
} else if (transceiver_mode == TRANSCEIVER_MODE_SS) {
/* Transmit continuous wave with specific amplitude */
byte_count += transfer->valid_length;
bytes_to_read = transfer->valid_length;
if (limit_num_samples) {
if (bytes_to_read >= bytes_to_xfer) {
bytes_to_read = bytes_to_xfer;
}
bytes_to_xfer -= bytes_to_read;
}
for(i = 0;i<bytes_to_read;i++)
transfer->buffer[i] = amplitude;
if (limit_num_samples && (bytes_to_xfer == 0)) {
return -1;
} else {
return 0;
}
} else {
return -1;
}
}
static void usage() {
printf("Usage:\n");
printf("\t[-d serial_number] # Serial number of desired HackRF.\n");
printf("\t-r <filename> # Receive data into file (use '-' for stdout).\n");
printf("\t-t <filename> # Transmit data from file (use '-' for stdin).\n");
printf("\t-w # Receive data into file with WAV header and automatic name.\n");
printf("\t # This is for SDR# compatibility and may not work with other software.\n");
printf("\t[-f freq_hz] # Frequency in Hz [%sMHz to %sMHz].\n",
u64toa((FREQ_MIN_HZ/FREQ_ONE_MHZ),&ascii_u64_data1),
u64toa((FREQ_MAX_HZ/FREQ_ONE_MHZ),&ascii_u64_data2));
printf("\t[-i if_freq_hz] # Intermediate Frequency (IF) in Hz [%sMHz to %sMHz].\n",
u64toa((IF_MIN_HZ/FREQ_ONE_MHZ),&ascii_u64_data1),
u64toa((IF_MAX_HZ/FREQ_ONE_MHZ),&ascii_u64_data2));
printf("\t[-o lo_freq_hz] # Front-end Local Oscillator (LO) frequency in Hz [%sMHz to %sMHz].\n",
u64toa((LO_MIN_HZ/FREQ_ONE_MHZ),&ascii_u64_data1),
u64toa((LO_MAX_HZ/FREQ_ONE_MHZ),&ascii_u64_data2));
printf("\t[-m image_reject] # Image rejection filter selection, 0=bypass, 1=low pass, 2=high pass.\n");
printf("\t[-a amp_enable] # RX/TX RF amplifier 1=Enable, 0=Disable.\n");
printf("\t[-p antenna_enable] # Antenna port power, 1=Enable, 0=Disable.\n");
printf("\t[-l gain_db] # RX LNA (IF) gain, 0-40dB, 8dB steps\n");
printf("\t[-g gain_db] # RX VGA (baseband) gain, 0-62dB, 2dB steps\n");
printf("\t[-x gain_db] # TX VGA (IF) gain, 0-47dB, 1dB steps\n");
printf("\t[-s sample_rate_hz] # Sample rate in Hz (4/8/10/12.5/16/20MHz, default %sMHz).\n",
u64toa((DEFAULT_SAMPLE_RATE_HZ/FREQ_ONE_MHZ),&ascii_u64_data1));
printf("\t[-n num_samples] # Number of samples to transfer (default is unlimited).\n");
printf("\t[-c amplitude] # CW signal source mode, amplitude 0-127 (DC value to DAC).\n");
printf("\t[-R] # Repeat TX mode (default is off) \n");
printf("\t[-b baseband_filter_bw_hz] # Set baseband filter bandwidth in Hz.\n\tPossible values: 1.75/2.5/3.5/5/5.5/6/7/8/9/10/12/14/15/20/24/28MHz, default < sample_rate_hz.\n" );
printf("\t[-C ppm] # Set Internal crystal clock error in ppm.\n");
}
static hackrf_device* device = NULL;
#ifdef _MSC_VER
BOOL WINAPI
sighandler(int signum)
{
if (CTRL_C_EVENT == signum) {
fprintf(stdout, "Caught signal %d\n", signum);
do_exit = true;
return TRUE;
}
return FALSE;
}
#else
void sigint_callback_handler(int signum)
{
fprintf(stdout, "Caught signal %d\n", signum);
do_exit = true;
}
#endif
#define PATH_FILE_MAX_LEN (FILENAME_MAX)
#define DATE_TIME_MAX_LEN (32)
int main(int argc, char** argv) {
int opt;
char path_file[PATH_FILE_MAX_LEN];
char date_time[DATE_TIME_MAX_LEN];
const char* path = NULL;
const char* serial_number = NULL;
char* endptr;
double f_hz;
int result;
time_t rawtime;
struct tm * timeinfo;
long int file_pos;
int exit_code = EXIT_SUCCESS;
struct timeval t_end;
float time_diff;
unsigned int lna_gain=8, vga_gain=20, txvga_gain=0;
while( (opt = getopt(argc, argv, "wr:t:f:i:o:m:a:p:s:n:b:l:g:x:c:d:C:R")) != EOF )
{
result = HACKRF_SUCCESS;
switch( opt )
{
case 'w':
receive_wav = true;
break;
case 'r':
receive = true;
path = optarg;
break;
case 't':
transmit = true;
path = optarg;
break;
case 'd':
serial_number = optarg;
break;
case 'f':
f_hz = strtod(optarg, &endptr);
if (optarg == endptr) {
result = HACKRF_ERROR_INVALID_PARAM;
break;
}
freq_hz = f_hz;
automatic_tuning = true;
break;
case 'i':
f_hz = strtod(optarg, &endptr);
if (optarg == endptr) {
result = HACKRF_ERROR_INVALID_PARAM;
break;
}
if_freq_hz = f_hz;
if_freq = true;
break;
case 'o':
f_hz = strtod(optarg, &endptr);
if (optarg == endptr) {
result = HACKRF_ERROR_INVALID_PARAM;
break;
}
lo_freq_hz = f_hz;
lo_freq = true;
break;
case 'm':
image_reject = true;
result = parse_u32(optarg, &image_reject_selection);
break;
case 'a':
amp = true;
result = parse_u32(optarg, &_enable);
break;
case 'p':
antenna = true;
result = parse_u32(optarg, &antenna_enable);
break;
case 'l':
result = parse_u32(optarg, &lna_gain);
break;
case 'g':
result = parse_u32(optarg, &vga_gain);
break;
case 'x':
result = parse_u32(optarg, &txvga_gain);
break;
case 's':
f_hz = strtod(optarg, &endptr);
if (optarg == endptr) {
result = HACKRF_ERROR_INVALID_PARAM;
break;
}
sample_rate_hz = f_hz;
sample_rate = true;
break;
case 'n':
limit_num_samples = true;
result = parse_u64(optarg, &samples_to_xfer);
bytes_to_xfer = samples_to_xfer * 2ull;
break;
case 'b':
f_hz = strtod(optarg, &endptr);
if (optarg == endptr) {
result = HACKRF_ERROR_INVALID_PARAM;
break;
}
baseband_filter_bw_hz = f_hz;
baseband_filter_bw = true;
break;
case 'c':
transmit = true;
signalsource = true;
result = parse_u32(optarg, &litude);
break;
case 'R':
repeat = true;
break;
case 'C':
crystal_correct = true;
result = parse_u32(optarg, &crystal_correct_ppm);
break;
default:
fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg);
usage();
return EXIT_FAILURE;
}
if( result != HACKRF_SUCCESS ) {
fprintf(stderr, "argument error: '-%c %s' %s (%d)\n", opt, optarg, hackrf_error_name(result), result);
usage();
return EXIT_FAILURE;
}
}
if (lna_gain % 8)
fprintf(stderr, "warning: lna_gain (-l) must be a multiple of 8\n");
if (vga_gain % 2)
fprintf(stderr, "warning: vga_gain (-g) must be a multiple of 2\n");
if (samples_to_xfer >= SAMPLES_TO_XFER_MAX) {
fprintf(stderr, "argument error: num_samples must be less than %s/%sMio\n",
u64toa(SAMPLES_TO_XFER_MAX,&ascii_u64_data1),
u64toa((SAMPLES_TO_XFER_MAX/FREQ_ONE_MHZ),&ascii_u64_data2));
usage();
return EXIT_FAILURE;
}
if (if_freq || lo_freq || image_reject) {
/* explicit tuning selected */
if (!if_freq) {
fprintf(stderr, "argument error: if_freq_hz must be specified for explicit tuning.\n");
usage();
return EXIT_FAILURE;
}
if (!image_reject) {
fprintf(stderr, "argument error: image_reject must be specified for explicit tuning.\n");
usage();
return EXIT_FAILURE;
}
if (!lo_freq && (image_reject_selection != RF_PATH_FILTER_BYPASS)) {
fprintf(stderr, "argument error: lo_freq_hz must be specified for explicit tuning unless image_reject is set to bypass.\n");
usage();
return EXIT_FAILURE;
}
if ((if_freq_hz > IF_MAX_HZ) || (if_freq_hz < IF_MIN_HZ)) {
fprintf(stderr, "argument error: if_freq_hz shall be between %s and %s.\n",
u64toa(IF_MIN_HZ,&ascii_u64_data1),
u64toa(IF_MAX_HZ,&ascii_u64_data2));
usage();
return EXIT_FAILURE;
}
if ((lo_freq_hz > LO_MAX_HZ) || (lo_freq_hz < LO_MIN_HZ)) {
fprintf(stderr, "argument error: lo_freq_hz shall be between %s and %s.\n",
u64toa(LO_MIN_HZ,&ascii_u64_data1),
u64toa(LO_MAX_HZ,&ascii_u64_data2));
usage();
return EXIT_FAILURE;
}
if (image_reject_selection > 2) {
fprintf(stderr, "argument error: image_reject must be 0, 1, or 2 .\n");
usage();
return EXIT_FAILURE;
}
if (automatic_tuning) {
fprintf(stderr, "warning: freq_hz ignored by explicit tuning selection.\n");
automatic_tuning = false;
}
switch (image_reject_selection) {
case RF_PATH_FILTER_BYPASS:
freq_hz = if_freq_hz;
break;
case RF_PATH_FILTER_LOW_PASS:
freq_hz = abs(if_freq_hz - lo_freq_hz);
break;
case RF_PATH_FILTER_HIGH_PASS:
freq_hz = if_freq_hz + lo_freq_hz;
break;
default:
freq_hz = DEFAULT_FREQ_HZ;
break;
}
fprintf(stderr, "explicit tuning specified for %s Hz.\n",
u64toa(freq_hz,&ascii_u64_data1));
} else if (automatic_tuning) {
if(freq_hz > FREQ_MAX_HZ)
{
fprintf(stderr, "argument error: freq_hz shall be between %s and %s.\n",
u64toa(FREQ_MIN_HZ,&ascii_u64_data1),
u64toa(FREQ_MAX_HZ,&ascii_u64_data2));
usage();
return EXIT_FAILURE;
}
} else {
/* Use default freq */
freq_hz = DEFAULT_FREQ_HZ;
automatic_tuning = true;
}
if( amp ) {
if( amp_enable > 1 )
{
fprintf(stderr, "argument error: amp_enable shall be 0 or 1.\n");
usage();
return EXIT_FAILURE;
}
}
if (antenna) {
if (antenna_enable > 1) {
fprintf(stderr, "argument error: antenna_enable shall be 0 or 1.\n");
usage();
return EXIT_FAILURE;
}
}
if( sample_rate == false )
{
sample_rate_hz = DEFAULT_SAMPLE_RATE_HZ;
}
if( baseband_filter_bw )
{
/* Compute nearest freq for bw filter */
baseband_filter_bw_hz = hackrf_compute_baseband_filter_bw(baseband_filter_bw_hz);
}else
{
/* Compute default value depending on sample rate */
baseband_filter_bw_hz = hackrf_compute_baseband_filter_bw_round_down_lt(sample_rate_hz);
}
if (baseband_filter_bw_hz > BASEBAND_FILTER_BW_MAX) {
fprintf(stderr, "argument error: baseband_filter_bw_hz must be less or equal to %u Hz/%.03f MHz\n",
BASEBAND_FILTER_BW_MAX, (float)(BASEBAND_FILTER_BW_MAX/FREQ_ONE_MHZ));
usage();
return EXIT_FAILURE;
}
if (baseband_filter_bw_hz < BASEBAND_FILTER_BW_MIN) {
fprintf(stderr, "argument error: baseband_filter_bw_hz must be greater or equal to %u Hz/%.03f MHz\n",
BASEBAND_FILTER_BW_MIN, (float)(BASEBAND_FILTER_BW_MIN/FREQ_ONE_MHZ));
usage();
return EXIT_FAILURE;
}
if( (transmit == false) && (receive == receive_wav) )
{
fprintf(stderr, "receive -r and receive_wav -w options are mutually exclusive\n");
usage();
return EXIT_FAILURE;
}
if( receive_wav == false )
{
if( transmit == receive )
{
if( transmit == true )
{
fprintf(stderr, "receive -r and transmit -t options are mutually exclusive\n");
} else
{
fprintf(stderr, "specify either transmit -t or receive -r or receive_wav -w option\n");
}
usage();
return EXIT_FAILURE;
}
}
if( receive ) {
transceiver_mode = TRANSCEIVER_MODE_RX;
}
if( transmit ) {
transceiver_mode = TRANSCEIVER_MODE_TX;
}
if (signalsource) {
transceiver_mode = TRANSCEIVER_MODE_SS;
if (amplitude >127) {
fprintf(stderr, "argument error: amplitude shall be in between 0 and 128.\n");
usage();
return EXIT_FAILURE;
}
}
if( receive_wav )
{
time (&rawtime);
timeinfo = localtime (&rawtime);
transceiver_mode = TRANSCEIVER_MODE_RX;
/* File format HackRF Year(2013), Month(11), Day(28), Hour Min Sec+Z, Freq kHz, IQ.wav */
strftime(date_time, DATE_TIME_MAX_LEN, "%Y%m%d_%H%M%S", timeinfo);
snprintf(path_file, PATH_FILE_MAX_LEN, "HackRF_%sZ_%ukHz_IQ.wav", date_time, (uint32_t)(freq_hz/(1000ull)) );
path = path_file;
fprintf(stderr, "Receive wav file: %s\n", path);
}
// In signal source mode, the PATH argument is neglected.
if (transceiver_mode != TRANSCEIVER_MODE_SS) {
if( path == NULL ) {
fprintf(stderr, "specify a path to a file to transmit/receive\n");
usage();
return EXIT_FAILURE;
}
}
// Change the freq and sample rate to correct the crystal clock error.
if( crystal_correct ) {
sample_rate_hz = (uint32_t)((double)sample_rate_hz * (1000000 - crystal_correct_ppm)/1000000+0.5);
freq_hz = freq_hz * (1000000 - crystal_correct_ppm)/1000000;
}
result = hackrf_init();
if( result != HACKRF_SUCCESS ) {
fprintf(stderr, "hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result);
usage();
return EXIT_FAILURE;
}
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);
usage();
return EXIT_FAILURE;
}
if (transceiver_mode != TRANSCEIVER_MODE_SS) {
if( transceiver_mode == TRANSCEIVER_MODE_RX )
{
if (strcmp(path, "-") == 0) {
fd = stdout;
} else {
fd = fopen(path, "wb");
}
} else {
if (strcmp(path, "-") == 0) {
fd = stdin;
} else {
fd = fopen(path, "rb");
}
}
if( fd == NULL ) {
fprintf(stderr, "Failed to open file: %s\n", path);
return EXIT_FAILURE;
}
/* Change fd buffer to have bigger one to store or read data on/to HDD */
result = setvbuf(fd , NULL , _IOFBF , FD_BUFFER_SIZE);
if( result != 0 ) {
fprintf(stderr, "setvbuf() failed: %d\n", result);
usage();
return EXIT_FAILURE;
}
}
/* Write Wav header */
if( receive_wav )
{
fwrite(&wave_file_hdr, 1, sizeof(t_wav_file_hdr), fd);
}
#ifdef _MSC_VER
SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
#else
signal(SIGINT, &sigint_callback_handler);
signal(SIGILL, &sigint_callback_handler);
signal(SIGFPE, &sigint_callback_handler);
signal(SIGSEGV, &sigint_callback_handler);
signal(SIGTERM, &sigint_callback_handler);
signal(SIGABRT, &sigint_callback_handler);
#endif
fprintf(stderr, "call hackrf_sample_rate_set(%u Hz/%.03f MHz)\n", sample_rate_hz,((float)sample_rate_hz/(float)FREQ_ONE_MHZ));
result = hackrf_set_sample_rate_manual(device, sample_rate_hz, 1);
if( result != HACKRF_SUCCESS ) {
fprintf(stderr, "hackrf_sample_rate_set() failed: %s (%d)\n", hackrf_error_name(result), result);
usage();
return EXIT_FAILURE;
}
fprintf(stderr, "call hackrf_baseband_filter_bandwidth_set(%d Hz/%.03f MHz)\n",
baseband_filter_bw_hz, ((float)baseband_filter_bw_hz/(float)FREQ_ONE_MHZ));
result = hackrf_set_baseband_filter_bandwidth(device, baseband_filter_bw_hz);
if( result != HACKRF_SUCCESS ) {
fprintf(stderr, "hackrf_baseband_filter_bandwidth_set() failed: %s (%d)\n", hackrf_error_name(result), result);
usage();
return EXIT_FAILURE;
}
if( transceiver_mode == TRANSCEIVER_MODE_RX ) {
result = hackrf_set_vga_gain(device, vga_gain);
result |= hackrf_set_lna_gain(device, lna_gain);
result |= hackrf_start_rx(device, rx_callback, NULL);
} else {
result = hackrf_set_txvga_gain(device, txvga_gain);
result |= hackrf_start_tx(device, tx_callback, NULL);
}
if( result != HACKRF_SUCCESS ) {
fprintf(stderr, "hackrf_start_?x() failed: %s (%d)\n", hackrf_error_name(result), result);
usage();
return EXIT_FAILURE;
}
if (automatic_tuning) {
fprintf(stderr, "call hackrf_set_freq(%s Hz/%.03f MHz)\n",
u64toa(freq_hz, &ascii_u64_data1),((double)freq_hz/(double)FREQ_ONE_MHZ) );
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);
usage();
return EXIT_FAILURE;
}
} else {
fprintf(stderr, "call hackrf_set_freq_explicit() with %s Hz IF, %s Hz LO, %s\n",
u64toa(if_freq_hz,&ascii_u64_data1),
u64toa(lo_freq_hz,&ascii_u64_data2),
hackrf_filter_path_name(image_reject_selection));
result = hackrf_set_freq_explicit(device, if_freq_hz, lo_freq_hz,
image_reject_selection);
if (result != HACKRF_SUCCESS) {
fprintf(stderr, "hackrf_set_freq_explicit() failed: %s (%d)\n",
hackrf_error_name(result), result);
usage();
return EXIT_FAILURE;
}
}
if( amp ) {
fprintf(stderr, "call hackrf_set_amp_enable(%u)\n", amp_enable);
result = hackrf_set_amp_enable(device, (uint8_t)amp_enable);
if( result != HACKRF_SUCCESS ) {
fprintf(stderr, "hackrf_set_amp_enable() failed: %s (%d)\n", hackrf_error_name(result), result);
usage();
return EXIT_FAILURE;
}
}
if (antenna) {
fprintf(stderr, "call hackrf_set_antenna_enable(%u)\n", antenna_enable);
result = hackrf_set_antenna_enable(device, (uint8_t)antenna_enable);
if (result != HACKRF_SUCCESS) {
fprintf(stderr, "hackrf_set_antenna_enable() failed: %s (%d)\n", hackrf_error_name(result), result);
usage();
return EXIT_FAILURE;
}
}
if( limit_num_samples ) {
fprintf(stderr, "samples_to_xfer %s/%sMio\n",
u64toa(samples_to_xfer,&ascii_u64_data1),
u64toa((samples_to_xfer/FREQ_ONE_MHZ),&ascii_u64_data2) );
}
gettimeofday(&t_start, NULL);
gettimeofday(&time_start, NULL);
fprintf(stderr, "Stop with Ctrl-C\n");
while( (hackrf_is_streaming(device) == HACKRF_TRUE) &&
(do_exit == false) )
{
uint32_t byte_count_now;
struct timeval time_now;
float time_difference, rate;
sleep(1);
gettimeofday(&time_now, NULL);
byte_count_now = byte_count;
byte_count = 0;
time_difference = TimevalDiff(&time_now, &time_start);
rate = (float)byte_count_now / time_difference;
fprintf(stderr, "%4.1f MiB / %5.3f sec = %4.1f MiB/second\n",
(byte_count_now / 1e6f), time_difference, (rate / 1e6f) );
time_start = time_now;