-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
sdr.c
1811 lines (1555 loc) · 52.7 KB
/
sdr.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
/** @file
SDR input from RTLSDR or SoapySDR.
Copyright (C) 2018 Christian Zuckschwerdt
based on code
Copyright (C) 2012 by Steve Markgraf <[email protected]>
Copyright (C) 2014 by Kyle Keen <[email protected]>
Copyright (C) 2016 by Robert X. Seger
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 of the License, or
(at your option) any later version.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "sdr.h"
#include "r_util.h"
#include "optparse.h"
#include "logger.h"
#include "fatal.h"
#include "compat_pthread.h"
#ifdef RTLSDR
#include <rtl-sdr.h>
#if defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
// not available in rtlsdr 0.5.3, allow weak link for Linux
int __attribute__((weak)) rtlsdr_set_bias_tee(rtlsdr_dev_t *dev, int on);
#endif
#ifdef LIBUSB1
#include <libusb.h> /* libusb_error_name(), libusb_strerror() */
#endif
#endif
#ifdef SOAPYSDR
#include <SoapySDR/Version.h>
#include <SoapySDR/Device.h>
#include <SoapySDR/Formats.h>
#include <SoapySDR/Logger.h>
#if (SOAPY_SDR_API_VERSION < 0x00080000)
#define SoapySDR_free(ptr) free(ptr)
#endif
#endif
#ifndef _MSC_VER
#include <unistd.h>
#endif
#ifdef _WIN32
#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0600)
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0600 /* Needed to pull in 'struct sockaddr_storage' */
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#define SHUT_RDWR SD_BOTH
#define perror(str) ws2_perror(str)
static void ws2_perror (const char *str)
{
if (str && *str)
fprintf(stderr, "%s: ", str);
fprintf(stderr, "Winsock error %d.\n", WSAGetLastError());
}
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#define SOCKET int
#define INVALID_SOCKET (-1)
#define closesocket(x) close(x)
#endif
#define GAIN_STR_MAX_SIZE 64
struct sdr_dev {
SOCKET rtl_tcp;
uint32_t rtl_tcp_freq; ///< last known center frequency, rtl_tcp only.
uint32_t rtl_tcp_rate; ///< last known sample rate, rtl_tcp only.
#ifdef SOAPYSDR
SoapySDRDevice *soapy_dev;
SoapySDRStream *soapy_stream;
double fullScale;
#endif
#ifdef RTLSDR
rtlsdr_dev_t *rtlsdr_dev;
sdr_event_cb_t rtlsdr_cb;
void *rtlsdr_cb_ctx;
#endif
char *dev_info;
int running;
uint8_t *buffer; ///< sdr data buffer current and past frames
size_t buffer_size; ///< sdr data buffer overall size (num * len)
size_t buffer_pos; ///< sdr data buffer next write position
int sample_size;
int sample_signed;
uint32_t sample_rate;
uint32_t center_frequency;
#ifdef THREADS
pthread_t thread;
pthread_mutex_t lock; ///< lock for exit_acquire
int exit_acquire;
// acquire thread args
sdr_event_cb_t async_cb;
void *async_ctx;
uint32_t buf_num;
uint32_t buf_len;
#endif
};
/* rtl_tcp helpers */
#pragma pack(push, 1)
struct rtl_tcp_info {
char magic[4]; // "RTL0"
uint32_t tuner_number; // big endian
uint32_t tuner_gain_count; // big endian
};
#pragma pack(pop)
static int rtltcp_open(sdr_dev_t **out_dev, char const *dev_query, int verbose)
{
UNUSED(verbose);
char const *host = "localhost";
char const *port = "1234";
char hostport[280]; // 253 chars DNS name plus extra chars
char *param = arg_param(dev_query); // strip scheme
hostport[0] = '\0';
if (param) {
snprintf(hostport, sizeof(hostport), "%s", param);
}
hostport_param(hostport, &host, &port);
print_logf(LOG_CRITICAL, "SDR", "rtl_tcp input from %s port %s", host, port);
#ifdef _WIN32
WSADATA wsa;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
perror("WSAStartup()");
return -1;
}
#endif
struct addrinfo hints, *res, *res0;
int ret;
SOCKET sock;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
hints.ai_flags = AI_ADDRCONFIG;
ret = getaddrinfo(host, port, &hints, &res0);
if (ret) {
print_log(LOG_ERROR, __func__, gai_strerror(ret));
return -1;
}
sock = INVALID_SOCKET;
for (res = res0; res; res = res->ai_next) {
sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sock >= 0) {
ret = connect(sock, res->ai_addr, res->ai_addrlen);
if (ret == -1) {
perror("connect");
closesocket(sock);
sock = INVALID_SOCKET;
}
else
break; // success
}
}
freeaddrinfo(res0);
if (sock == INVALID_SOCKET) {
perror("socket");
return -1;
}
//int const value_one = 1;
//ret = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&value_one, sizeof(value_one));
//if (ret < 0)
// fprintf(stderr, "rtl_tcp TCP_NODELAY failed\n");
struct rtl_tcp_info info;
ret = recv(sock, (char *)&info, sizeof (info), 0);
if (ret != 12) {
print_logf(LOG_ERROR, __func__, "Bad rtl_tcp header (%d)", ret);
return -1;
}
if (strncmp(info.magic, "RTL0", 4)) {
info.tuner_number = 0; // terminate magic
print_logf(LOG_ERROR, __func__, "Bad rtl_tcp header magic \"%s\"", info.magic);
return -1;
}
unsigned tuner_number = ntohl(info.tuner_number);
//int tuner_gain_count = ntohl(info.tuner_gain_count);
char const *tuner_names[] = { "Unknown", "E4000", "FC0012", "FC0013", "FC2580", "R820T", "R828D" };
char const *tuner_name = tuner_number > sizeof (tuner_names) ? "Invalid" : tuner_names[tuner_number];
print_logf(LOG_CRITICAL, "SDR", "rtl_tcp connected to %s:%s (Tuner: %s)", host, port, tuner_name);
sdr_dev_t *dev = calloc(1, sizeof(sdr_dev_t));
if (!dev) {
WARN_CALLOC("rtltcp_open()");
return -1; // NOTE: returns error on alloc failure.
}
#ifdef THREADS
pthread_mutex_init(&dev->lock, NULL);
#endif
dev->rtl_tcp = sock;
dev->sample_size = sizeof(uint8_t) * 2; // CU8
dev->sample_signed = 0;
*out_dev = dev;
return 0;
}
static int rtltcp_close(SOCKET sock)
{
int ret = shutdown(sock, SHUT_RDWR);
if (ret == -1) {
perror("shutdown");
return -1;
}
ret = closesocket(sock);
if (ret == -1) {
perror("close");
return -1;
}
return 0;
}
static int rtltcp_read_loop(sdr_dev_t *dev, sdr_event_cb_t cb, void *ctx, uint32_t buf_num, uint32_t buf_len)
{
size_t buffer_size = (size_t)buf_num * buf_len;
if (dev->buffer_size != buffer_size) {
free(dev->buffer);
dev->buffer = malloc(buffer_size);
if (!dev->buffer) {
WARN_MALLOC("rtltcp_read_loop()");
return -1; // NOTE: returns error on alloc failure.
}
dev->buffer_size = buffer_size;
dev->buffer_pos = 0;
}
dev->running = 1;
do {
if (dev->buffer_pos + buf_len > buffer_size)
dev->buffer_pos = 0;
uint8_t *buffer = &dev->buffer[dev->buffer_pos];
dev->buffer_pos += buf_len;
unsigned n_read = 0;
int r;
do {
r = recv(dev->rtl_tcp, &buffer[n_read], buf_len - n_read, MSG_WAITALL);
if (r <= 0)
break;
n_read += r;
//fprintf(stderr, "readStream ret=%d (of %u)\n", r, n_read);
} while (n_read < buf_len);
//fprintf(stderr, "readStream ret=%d (read %u)\n", r, n_read);
if (r < 0) {
print_logf(LOG_WARNING, __func__, "sync read failed. %d", r);
}
if (n_read == 0) {
perror("rtl_tcp");
dev->running = 0;
}
#ifdef THREADS
pthread_mutex_lock(&dev->lock);
#endif
uint32_t sample_rate = dev->sample_rate;
uint32_t center_frequency = dev->center_frequency;
#ifdef THREADS
pthread_mutex_unlock(&dev->lock);
#endif
sdr_event_t ev = {
.ev = SDR_EV_DATA,
.sample_rate = sample_rate,
.center_frequency = center_frequency,
.buf = buffer,
.len = n_read,
};
#ifdef THREADS
pthread_mutex_lock(&dev->lock);
int exit_acquire = dev->exit_acquire;
pthread_mutex_unlock(&dev->lock);
if (exit_acquire) {
break; // do not deliver any more events
}
#endif
if (n_read > 0) // prevent a crash in callback
cb(&ev, ctx);
} while (dev->running);
return 0;
}
#pragma pack(push, 1)
struct command {
unsigned char cmd;
unsigned int param;
};
#pragma pack(pop)
// rtl_tcp API
#define RTLTCP_SET_FREQ 0x01
#define RTLTCP_SET_SAMPLE_RATE 0x02
#define RTLTCP_SET_GAIN_MODE 0x03
#define RTLTCP_SET_GAIN 0x04
#define RTLTCP_SET_FREQ_CORRECTION 0x05
#define RTLTCP_SET_IF_TUNER_GAIN 0x06
#define RTLTCP_SET_TEST_MODE 0x07
#define RTLTCP_SET_AGC_MODE 0x08
#define RTLTCP_SET_DIRECT_SAMPLING 0x09
#define RTLTCP_SET_OFFSET_TUNING 0x0a
#define RTLTCP_SET_RTL_XTAL 0x0b
#define RTLTCP_SET_TUNER_XTAL 0x0c
#define RTLTCP_SET_TUNER_GAIN_BY_ID 0x0d
#define RTLTCP_SET_BIAS_TEE 0x0e
static int rtltcp_command(sdr_dev_t *dev, char cmd, int param)
{
struct command command;
command.cmd = cmd;
command.param = htonl(param);
return sizeof(command) == send(dev->rtl_tcp, (const char*) &command, sizeof(command), 0) ? 0 : -1;
}
/* RTL-SDR helpers */
#ifdef RTLSDR
static int sdr_open_rtl(sdr_dev_t **out_dev, char const *dev_query, int verbose)
{
uint32_t device_count = rtlsdr_get_device_count();
if (!device_count) {
print_log(LOG_CRITICAL, "SDR", "No supported devices found.");
return -1;
}
if (verbose)
print_logf(LOG_NOTICE, "SDR", "Found %u device(s)", device_count);
int dev_index = 0;
// select rtlsdr device by serial (-d :<serial>)
if (dev_query && *dev_query == ':') {
dev_index = rtlsdr_get_index_by_serial(&dev_query[1]);
if (dev_index < 0) {
if (verbose)
print_logf(LOG_ERROR, "SDR", "Could not find device with serial '%s' (err %d)",
&dev_query[1], dev_index);
return -1;
}
}
// select rtlsdr device by number (-d <n>)
else if (dev_query) {
dev_index = atoi(dev_query);
// check if 0 is a parsing error?
if (dev_index < 0) {
// select first available rtlsdr device
dev_index = 0;
dev_query = NULL;
}
}
char vendor[256] = "n/a", product[256] = "n/a", serial[256] = "n/a";
int r = -1;
sdr_dev_t *dev = calloc(1, sizeof(sdr_dev_t));
if (!dev) {
WARN_CALLOC("sdr_open_rtl()");
return -1; // NOTE: returns error on alloc failure.
}
#ifdef THREADS
pthread_mutex_init(&dev->lock, NULL);
#endif
for (uint32_t i = dev_query ? dev_index : 0;
//cast quiets -Wsign-compare; if dev_index were < 0, would have returned -1 above
i < (dev_query ? (unsigned)dev_index + 1 : device_count);
i++) {
rtlsdr_get_device_usb_strings(i, vendor, product, serial);
if (verbose)
print_logf(LOG_NOTICE, "SDR", "trying device %u: %s, %s, SN: %s",
i, vendor, product, serial);
r = rtlsdr_open(&dev->rtlsdr_dev, i);
if (r < 0) {
if (verbose)
print_logf(LOG_ERROR, __func__, "Failed to open rtlsdr device #%u.", i);
}
else {
if (verbose)
print_logf(LOG_CRITICAL, "SDR", "Using device %u: %s, %s, SN: %s, \"%s\"",
i, vendor, product, serial, rtlsdr_get_device_name(i));
dev->sample_size = sizeof(uint8_t) * 2; // CU8
dev->sample_signed = 0;
size_t info_len = 41 + strlen(vendor) + strlen(product) + strlen(serial);
dev->dev_info = malloc(info_len);
if (!dev->dev_info)
FATAL_MALLOC("sdr_open_rtl");
snprintf(dev->dev_info, info_len, "{\"vendor\":\"%s\", \"product\":\"%s\", \"serial\":\"%s\"}",
vendor, product, serial);
break;
}
}
if (r < 0) {
free(dev);
if (verbose)
print_log(LOG_ERROR, __func__, "Unable to open a device");
}
else {
*out_dev = dev;
}
return r;
}
static int rtlsdr_find_tuner_gain(sdr_dev_t *dev, int centigain, int verbose)
{
/* Get allowed gains */
int gains_count = rtlsdr_get_tuner_gains(dev->rtlsdr_dev, NULL);
if (gains_count < 0) {
if (verbose)
print_log(LOG_WARNING, __func__, "Unable to get exact gains");
return centigain;
}
if (gains_count < 1) {
if (verbose)
print_log(LOG_WARNING, __func__, "No exact gains");
return centigain;
}
if (gains_count > 29) {
print_log(LOG_ERROR, __func__, "Unexpected gain count, notify maintainers please!");
return centigain;
}
// We known the maximum nunmber of gains is 29.
// Let's not waste an alloc
int gains[29] = {0};
rtlsdr_get_tuner_gains(dev->rtlsdr_dev, gains);
/* Find allowed gain */
for (int i = 0; i < gains_count; ++i) {
if (centigain <= gains[i]) {
centigain = gains[i];
break;
}
}
if (centigain > gains[gains_count - 1]) {
centigain = gains[gains_count - 1];
}
return centigain;
}
static void rtlsdr_read_cb(unsigned char *iq_buf, uint32_t len, void *ctx)
{
sdr_dev_t *dev = ctx;
//fprintf(stderr, "rtlsdr_read_cb enter...\n");
#ifdef THREADS
pthread_mutex_lock(&dev->lock);
int exit_acquire = dev->exit_acquire;
pthread_mutex_unlock(&dev->lock);
if (exit_acquire) {
// we get one more call after rtlsdr_cancel_async(),
// it then takes a full second until rtlsdr_read_async() ends.
//fprintf(stderr, "rtlsdr_read_cb stopping...\n");
return; // do not deliver any more events
}
#endif
if (dev->buffer_pos + len > dev->buffer_size)
dev->buffer_pos = 0;
uint8_t *buffer = &dev->buffer[dev->buffer_pos];
dev->buffer_pos += len;
// NOTE: we need to copy the buffer, it might go away on cancel_async
memcpy(buffer, iq_buf, len);
#ifdef THREADS
pthread_mutex_lock(&dev->lock);
#endif
uint32_t sample_rate = dev->sample_rate;
uint32_t center_frequency = dev->center_frequency;
#ifdef THREADS
pthread_mutex_unlock(&dev->lock);
#endif
sdr_event_t ev = {
.ev = SDR_EV_DATA,
.sample_rate = sample_rate,
.center_frequency = center_frequency,
.buf = buffer,
.len = len,
};
//fprintf(stderr, "rtlsdr_read_cb cb...\n");
if (len > 0) // prevent a crash in callback
dev->rtlsdr_cb(&ev, dev->rtlsdr_cb_ctx);
//fprintf(stderr, "rtlsdr_read_cb cb done.\n");
// NOTE: we actually need to copy the buffer to prevent it going away on cancel_async
}
static int rtlsdr_read_loop(sdr_dev_t *dev, sdr_event_cb_t cb, void *ctx, uint32_t buf_num, uint32_t buf_len)
{
size_t buffer_size = (size_t)buf_num * buf_len;
if (dev->buffer_size != buffer_size) {
free(dev->buffer);
dev->buffer = malloc(buffer_size);
if (!dev->buffer) {
WARN_MALLOC("rtlsdr_read_loop()");
return -1; // NOTE: returns error on alloc failure.
}
dev->buffer_size = buffer_size;
dev->buffer_pos = 0;
}
int r = 0;
dev->rtlsdr_cb = cb;
dev->rtlsdr_cb_ctx = ctx;
dev->running = 1;
r = rtlsdr_read_async(dev->rtlsdr_dev, rtlsdr_read_cb, dev, buf_num, buf_len);
// rtlsdr_read_async() returns possible error codes from:
// if (!dev) return -1;
// if (RTLSDR_INACTIVE != dev->async_status) return -2;
// r = libusb_submit_transfer(dev->xfer[i]);
// r = libusb_handle_events_timeout_completed(dev->ctx, &tv,
// r = libusb_cancel_transfer(dev->xfer[i]);
// We can safely assume it's an libusb error.
if (r < 0) {
#ifdef LIBUSB1
print_logf(LOG_ERROR, __func__, "%s: %s! "
"Check your RTL-SDR dongle, USB cables, and power supply.",
libusb_error_name(r), libusb_strerror(r));
#else
print_logf(LOG_ERROR, __func__, "LIBUSB_ERROR: %d! "
"Check your RTL-SDR dongle, USB cables, and power supply.",
r);
#endif
dev->running = 0;
}
print_log(LOG_DEBUG, __func__, "rtlsdr_read_async done");
return r;
}
#endif
/* SoapySDR helpers */
#ifdef SOAPYSDR
static int soapysdr_set_bandwidth(SoapySDRDevice *dev, uint32_t bandwidth)
{
int r;
r = (int)SoapySDRDevice_setBandwidth(dev, SOAPY_SDR_RX, 0, (double)bandwidth);
uint32_t applied_bw = 0;
if (r != 0) {
print_log(LOG_WARNING, "SDR", "Failed to set bandwidth.");
}
else if (bandwidth > 0) {
applied_bw = (uint32_t)SoapySDRDevice_getBandwidth(dev, SOAPY_SDR_RX, 0);
if (applied_bw)
print_logf(LOG_NOTICE, "SDR", "Bandwidth parameter %u Hz resulted in %u Hz.", bandwidth, applied_bw);
else
print_logf(LOG_NOTICE, "SDR", "Set bandwidth parameter %u Hz.", bandwidth);
}
else {
print_logf(LOG_NOTICE, "SDR", "Bandwidth set to automatic resulted in %u Hz.", applied_bw);
}
return r;
}
static int soapysdr_direct_sampling(SoapySDRDevice *dev, int on)
{
int r = 0;
char const *value;
if (on == 0)
value = "0";
else if (on == 1)
value = "1";
else if (on == 2)
value = "2";
else
return -1;
SoapySDRDevice_writeSetting(dev, "direct_samp", value);
char *set_value = SoapySDRDevice_readSetting(dev, "direct_samp");
if (set_value == NULL) {
print_log(LOG_ERROR, __func__, "Failed to set direct sampling moden");
return r;
}
int set_num = atoi(set_value);
if (set_num == 0) {
print_log(LOG_CRITICAL, "SDR", "Direct sampling mode disabled.");}
else if (set_num == 1) {
print_log(LOG_CRITICAL, "SDR", "Enabled direct sampling mode, input 1/I.");}
else if (set_num == 2) {
print_log(LOG_CRITICAL, "SDR", "Enabled direct sampling mode, input 2/Q.");}
else if (set_num == 3) {
print_log(LOG_CRITICAL, "SDR", "Enabled no-mod direct sampling mode.");}
SoapySDR_free(set_value);
return r;
}
static int soapysdr_offset_tuning(SoapySDRDevice *dev)
{
int r = 0;
SoapySDRDevice_writeSetting(dev, "offset_tune", "true");
char *set_value = SoapySDRDevice_readSetting(dev, "offset_tune");
if (strcmp(set_value, "true") != 0) {
/* TODO: detection of failure modes
if (r == -2)
print_log(LOG_WARNING, __func__, "Failed to set offset tuning: tuner doesn't support offset tuning!");
else if (r == -3)
print_log(LOG_WARNING, __func__, "Failed to set offset tuning: direct sampling not combinable with offset tuning!");
else
*/
print_log(LOG_WARNING, __func__, "Failed to set offset tuning.");
}
else {
print_log(LOG_CRITICAL, "SDR", "Offset tuning mode enabled.");
}
SoapySDR_free(set_value);
return r;
}
static int soapysdr_auto_gain(SoapySDRDevice *dev, int verbose)
{
int r = 0;
r = SoapySDRDevice_hasGainMode(dev, SOAPY_SDR_RX, 0);
if (r) {
r = SoapySDRDevice_setGainMode(dev, SOAPY_SDR_RX, 0, 1);
if (r != 0) {
print_log(LOG_WARNING, __func__, "Failed to enable automatic gain.");
}
else {
if (verbose)
print_log(LOG_CRITICAL, "SDR", "Tuner set to automatic gain.");
}
}
// Per-driver hacks TODO: clean this up
char *driver = SoapySDRDevice_getDriverKey(dev);
if (strcmp(driver, "HackRF") == 0) {
// HackRF has three gains LNA, VGA, and AMP, setting total distributes amongst, 116.0 dB seems to work well,
// even though it logs HACKRF_ERROR_INVALID_PARAM? https://github.com/rxseger/rx_tools/issues/9
// Total gain is distributed amongst all gains, 116 = 37,65,1; the LNA is OK (<40) but VGA is out of range (65 > 62)
// TODO: generic means to set all gains, of any SDR? string parsing LNA=#,VGA=#,AMP=#?
r = SoapySDRDevice_setGainElement(dev, SOAPY_SDR_RX, 0, "LNA", 40.); // max 40
if (r != 0) {
print_log(LOG_WARNING, __func__, "Failed to set LNA tuner gain.");
}
r = SoapySDRDevice_setGainElement(dev, SOAPY_SDR_RX, 0, "VGA", 20.); // max 65
if (r != 0) {
print_log(LOG_WARNING, __func__, "Failed to set VGA tuner gain.");
}
r = SoapySDRDevice_setGainElement(dev, SOAPY_SDR_RX, 0, "AMP", 0.); // on or off
if (r != 0) {
print_log(LOG_WARNING, __func__, "Failed to set AMP tuner gain.");
}
}
SoapySDR_free(driver);
// otherwise leave unset, hopefully the driver has good defaults
return r;
}
static int soapysdr_gain_str_set(SoapySDRDevice *dev, char const *gain_str, int verbose)
{
if (!gain_str || !*gain_str || strlen(gain_str) >= GAIN_STR_MAX_SIZE)
return -1;
int r = 0;
// Disable automatic gain
r = SoapySDRDevice_hasGainMode(dev, SOAPY_SDR_RX, 0);
if (r) {
r = SoapySDRDevice_setGainMode(dev, SOAPY_SDR_RX, 0, 0);
if (r != 0) {
print_log(LOG_WARNING, __func__, "Failed to disable automatic gain.");
}
else {
if (verbose)
print_log(LOG_NOTICE, "SDR", "Tuner set to manual gain.");
}
}
if (strchr(gain_str, '=')) {
char gain_cpy[GAIN_STR_MAX_SIZE];
snprintf(gain_cpy, sizeof(gain_cpy), "%s", gain_str);
char *gain_p = gain_cpy;
// Set each gain individually (more control)
char *name;
char *value;
while (getkwargs(&gain_p, &name, &value)) {
double num = atof(value);
if (verbose)
print_logf(LOG_NOTICE, "SDR", "Setting gain element %s: %f dB", name, num);
r = SoapySDRDevice_setGainElement(dev, SOAPY_SDR_RX, 0, name, num);
if (r != 0) {
print_logf(LOG_WARNING, __func__, "setGainElement(%s, %f) failed: %d", name, num, r);
}
}
}
else {
// Set overall gain and let SoapySDR distribute amongst components
double value = atof(gain_str);
r = SoapySDRDevice_setGain(dev, SOAPY_SDR_RX, 0, value);
if (r != 0) {
print_log(LOG_WARNING, __func__, "Failed to set tuner gain.");
}
else {
if (verbose)
print_logf(LOG_NOTICE, __func__, "Tuner gain set to %0.2f dB.", value);
}
// read back and print each individual gain element
if (verbose) {
size_t len = 0;
char **gains = SoapySDRDevice_listGains(dev, SOAPY_SDR_RX, 0, &len);
fprintf(stderr, "Gain elements: ");
for (size_t i = 0; i < len; ++i) {
double gain = SoapySDRDevice_getGain(dev, SOAPY_SDR_RX, 0);
fprintf(stderr, "%s=%g ", gains[i], gain);
}
fprintf(stderr, "\n");
SoapySDRStrings_clear(&gains, len);
}
}
return r;
}
static void soapysdr_show_device_info(SoapySDRDevice *dev)
{
size_t len = 0, i = 0;
char *hwkey;
SoapySDRKwargs args;
char **antennas;
char **gains;
char **frequencies;
SoapySDRRange *frequencyRanges;
SoapySDRRange *rates;
SoapySDRRange *bandwidths;
double fullScale;
char **stream_formats;
char *native_stream_format;
int direction = SOAPY_SDR_RX;
size_t channel = 0;
hwkey = SoapySDRDevice_getHardwareKey(dev);
fprintf(stderr, "Using device %s: ", hwkey);
SoapySDR_free(hwkey);
args = SoapySDRDevice_getHardwareInfo(dev);
for (i = 0; i < args.size; ++i) {
fprintf(stderr, "%s=%s ", args.keys[i], args.vals[i]);
}
fprintf(stderr, "\n");
SoapySDRKwargs_clear(&args);
antennas = SoapySDRDevice_listAntennas(dev, direction, channel, &len);
fprintf(stderr, "Found %zu antenna(s): ", len);
for (i = 0; i < len; ++i) {
fprintf(stderr, "%s ", antennas[i]);
}
fprintf(stderr, "\n");
SoapySDRStrings_clear(&antennas, len);
gains = SoapySDRDevice_listGains(dev, direction, channel, &len);
fprintf(stderr, "Found %zu gain(s): ", len);
for (i = 0; i < len; ++i) {
SoapySDRRange gainRange = SoapySDRDevice_getGainRange(dev, direction, channel);
fprintf(stderr, "%s %.0f - %.0f (step %.0f) ", gains[i], gainRange.minimum, gainRange.maximum, gainRange.step);
}
fprintf(stderr, "\n");
SoapySDRStrings_clear(&gains, len);
frequencies = SoapySDRDevice_listFrequencies(dev, direction, channel, &len);
fprintf(stderr, "Found %zu frequencies: ", len);
for (i = 0; i < len; ++i) {
fprintf(stderr, "%s ", frequencies[i]);
}
fprintf(stderr, "\n");
SoapySDRStrings_clear(&frequencies, len);
frequencyRanges = SoapySDRDevice_getFrequencyRange(dev, direction, channel, &len);
fprintf(stderr, "Found %zu frequency range(s): ", len);
for (i = 0; i < len; ++i) {
fprintf(stderr, "%.0f - %.0f (step %.0f) ", frequencyRanges[i].minimum, frequencyRanges[i].maximum, frequencyRanges[i].step);
}
fprintf(stderr, "\n");
SoapySDR_free(frequencyRanges);
rates = SoapySDRDevice_getSampleRateRange(dev, direction, channel, &len);
fprintf(stderr, "Found %zu sample rate range(s): ", len);
for (i = 0; i < len; ++i) {
if (rates[i].minimum == rates[i].maximum)
fprintf(stderr, "%.0f ", rates[i].minimum);
else
fprintf(stderr, "%.0f - %.0f (step %.0f) ", rates[i].minimum, rates[i].maximum, rates[i].step);
}
fprintf(stderr, "\n");
SoapySDR_free(rates);
bandwidths = SoapySDRDevice_getBandwidthRange(dev, direction, channel, &len);
fprintf(stderr, "Found %zu bandwidth range(s): ", len);
for (i = 0; i < len; ++i) {
fprintf(stderr, "%.0f - %.0f (step %.0f) ", bandwidths[i].minimum, bandwidths[i].maximum, bandwidths[i].step);
}
fprintf(stderr, "\n");
SoapySDR_free(bandwidths);
double bandwidth = SoapySDRDevice_getBandwidth(dev, direction, channel);
fprintf(stderr, "Found current bandwidth %.0f\n", bandwidth);
stream_formats = SoapySDRDevice_getStreamFormats(dev, direction, channel, &len);
fprintf(stderr, "Found %zu stream format(s): ", len);
for (i = 0; i < len; ++i) {
fprintf(stderr, "%s ", stream_formats[i]);
}
fprintf(stderr, "\n");
SoapySDRStrings_clear(&stream_formats, len);
native_stream_format = SoapySDRDevice_getNativeStreamFormat(dev, direction, channel, &fullScale);
fprintf(stderr, "Found native stream format: %s (full scale: %.1f)\n", native_stream_format, fullScale);
SoapySDR_free(native_stream_format);
}
static int sdr_open_soapy(sdr_dev_t **out_dev, char const *dev_query, int verbose)
{
if (verbose)
SoapySDR_setLogLevel(SOAPY_SDR_DEBUG);
sdr_dev_t *dev = calloc(1, sizeof(sdr_dev_t));
if (!dev) {
WARN_CALLOC("sdr_open_soapy()");
return -1; // NOTE: returns error on alloc failure.
}
#ifdef THREADS
pthread_mutex_init(&dev->lock, NULL);
#endif
dev->soapy_dev = SoapySDRDevice_makeStrArgs(dev_query);
if (!dev->soapy_dev) {
if (verbose)
print_logf(LOG_ERROR, __func__, "Failed to open sdr device matching '%s'.", dev_query);
free(dev);
return -1;
}
if (verbose)
soapysdr_show_device_info(dev->soapy_dev);
// select a stream format, in preference order: native CU8, CS8, CS16, forced CS16
// stream_formats = SoapySDRDevice_getStreamFormats(dev->soapy_dev, SOAPY_SDR_RX, 0, &len);
char *native_format = SoapySDRDevice_getNativeStreamFormat(dev->soapy_dev, SOAPY_SDR_RX, 0, &dev->fullScale);
char const *selected_format;
if (!strcmp(SOAPY_SDR_CU8, native_format)) {
// actually not supported by SoapySDR
selected_format = SOAPY_SDR_CU8;
dev->sample_size = sizeof(uint8_t); // CU8
dev->sample_signed = 0;
}
// else if (!strcmp(SOAPY_SDR_CS8, native_format)) {
// // TODO: CS8 needs conversion to CU8
// // e.g. RTL-SDR (8 bit), scale is 128.0
// selected_format = SOAPY_SDR_CS8;
// dev->sample_size = sizeof(int8_t) * 2; // CS8
// dev->sample_signed = 1;
// }
else if (!strcmp(SOAPY_SDR_CS16, native_format)) {
// e.g. LimeSDR-mini (12 bit), native scale is 2048.0
// e.g. SDRplay RSP1A (14 bit), native scale is 32767.0
selected_format = SOAPY_SDR_CS16;
dev->sample_size = sizeof(int16_t) * 2; // CS16
dev->sample_signed = 1;
}
else {
// force CS16
selected_format = SOAPY_SDR_CS16;
dev->sample_size = sizeof(int16_t) * 2; // CS16
dev->sample_signed = 1;
dev->fullScale = 32768.0; // assume max for SOAPY_SDR_CS16
}
SoapySDR_free(native_format);
SoapySDRKwargs args = SoapySDRDevice_getHardwareInfo(dev->soapy_dev);
size_t info_len = 2;
for (size_t i = 0; i < args.size; ++i) {
info_len += strlen(args.keys[i]) + strlen(args.vals[i]) + 6;
}
char *p = dev->dev_info = malloc(info_len);
if (!dev->dev_info)
FATAL_MALLOC("sdr_open_soapy");
for (size_t i = 0; i < args.size; ++i) {
p += sprintf(p, "%s\"%s\":\"%s\"", i ? "," : "{", args.keys[i], args.vals[i]);
}
sprintf(p, "}");
SoapySDRKwargs_clear(&args);
SoapySDRKwargs stream_args = {0};
int r;
#if SOAPY_SDR_API_VERSION >= 0x00080000
// API version 0.8
#undef SoapySDRDevice_setupStream
dev->soapy_stream = SoapySDRDevice_setupStream(dev->soapy_dev, SOAPY_SDR_RX, selected_format, NULL, 0, &stream_args);
r = dev->soapy_stream == NULL;
#else
// API version 0.7
r = SoapySDRDevice_setupStream(dev->soapy_dev, &dev->soapy_stream, SOAPY_SDR_RX, selected_format, NULL, 0, &stream_args);
#endif
if (r != 0) {
if (verbose)
print_log(LOG_ERROR, __func__, "Failed to setup sdr device");
free(dev);
return -3;
}
*out_dev = dev;
return 0;
}
// the buffer sizes can't be proven to be correct
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunknown-warning-option"
#pragma GCC diagnostic ignored "-Wanalyzer-allocation-size"
static int soapysdr_read_loop(sdr_dev_t *dev, sdr_event_cb_t cb, void *ctx, uint32_t buf_num, uint32_t buf_len)
{
size_t buffer_size = (size_t)buf_num * buf_len;
if (dev->buffer_size != buffer_size) {
free(dev->buffer);
dev->buffer = malloc(buffer_size);
if (!dev->buffer) {
WARN_MALLOC("soapysdr_read_loop()");
return -1; // NOTE: returns error on alloc failure.
}
dev->buffer_size = buffer_size;
dev->buffer_pos = 0;
}
size_t buf_elems = buf_len / dev->sample_size;
dev->running = 1;
do {
if (dev->buffer_pos + buf_len > buffer_size)
dev->buffer_pos = 0;
int16_t *buffer = (void *)&dev->buffer[dev->buffer_pos];
dev->buffer_pos += buf_len;
void *buffs[] = {buffer};
int flags = 0;
long long timeNs = 0;
long timeoutUs = 1000000; // 1 second
unsigned n_read = 0, i;
int r;
do {
buffs[0] = &buffer[n_read * 2];
r = SoapySDRDevice_readStream(dev->soapy_dev, dev->soapy_stream, buffs, buf_elems - n_read, &flags, &timeNs, timeoutUs);
if (r < 0)
break;
n_read += r; // r is number of elements read, elements=complex pairs, so buffer length is twice
//fprintf(stderr, "readStream ret=%d, flags=%d, timeNs=%lld (%zu - %u)\n", r, flags, timeNs, buf_elems, n_read);
} while (n_read < buf_elems);