-
Notifications
You must be signed in to change notification settings - Fork 1
/
HackRF_Settings.cpp
710 lines (599 loc) · 24.6 KB
/
HackRF_Settings.cpp
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2016 Wei Jiang
* Copyright (c) 2015-2017 Josh Blum
* Copyright (c) 2017-2023 Kevin Mehall
* Copyright (c) 2023 Tom Cully
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "SoapyHackRFDuplex.hpp"
#include <iostream>
std::set<std::string> &HackRF_getClaimedSerials(void) {
static std::set<std::string> serials;
return serials;
}
SoapyHackRFDuplex::SoapyHackRFDuplex(const SoapySDR::Kwargs &args) {
SoapySDR_logf(SOAPY_SDR_DEBUG, "Initialising SoapyHackRFDuplex");
_rx_stream.vga_gain = 16;
_rx_stream.lna_gain = 16;
_rx_stream.amp_gain = 0;
_rx_stream.frequency = 0;
_rx_stream.samplerate = 0;
_rx_stream.bandwidth = 0;
_rx_stream.overflow = false;
_tx_stream.vga_gain = 0;
_tx_stream.amp_gain = 0;
_tx_stream.frequency = 0;
_tx_stream.samplerate = 0;
_tx_stream.bandwidth = 0;
_tx_stream.burst_samps = 0;
_tx_stream.burst_end = false;
_tx_stream.underflow = false;
_rx_active = HACKRF_TRANSCEIVER_MODE_OFF;
_tx_active = HACKRF_TRANSCEIVER_MODE_OFF;
_rx_auto_bandwidth = true;
_tx_auto_bandwidth = true;
_rx_dev = nullptr;
_tx_dev = nullptr;
SoapySDR_logf(SOAPY_SDR_DEBUG, "Checking rx_serial, tx_serial");
if (args.count("rx_serial") == 0)
throw std::runtime_error("rx_serial not supplied");
if (args.count("tx_serial") == 0)
throw std::runtime_error("tx_serial not supplied");
_rx_serial = args.at("rx_serial");
_tx_serial = args.at("tx_serial");
_tx_current_amp = 0;
_rx_current_amp = 0;
_rx_current_frequency = 0;
_tx_current_frequency = 0;
_rx_current_samplerate = 0;
_tx_current_samplerate = 0;
_rx_current_bandwidth = 0;
_tx_current_bandwidth = 0;
SoapySDR_logf(SOAPY_SDR_DEBUG, "Opening Devices...");
int ret = hackrf_open_by_serial(_rx_serial.c_str(), &_rx_dev);
if (ret != HACKRF_SUCCESS) {
SoapySDR_logf(SOAPY_SDR_ERROR, "Could not Open HackRF RX Device");
throw std::runtime_error("hackrf open failed");
}
ret = hackrf_open_by_serial(_tx_serial.c_str(), &_tx_dev);
if (ret != HACKRF_SUCCESS) {
SoapySDR_logf(SOAPY_SDR_ERROR, "Could not Open HackRF TX Device");
throw std::runtime_error("hackrf open failed");
}
SoapySDR_logf(SOAPY_SDR_DEBUG, "Opened Devices");
HackRF_getClaimedSerials().insert(_rx_serial);
HackRF_getClaimedSerials().insert(_tx_serial);
}
SoapyHackRFDuplex::~SoapyHackRFDuplex(void) {
HackRF_getClaimedSerials().erase(_rx_serial);
HackRF_getClaimedSerials().erase(_tx_serial);
/* cleanup device handles */
if (_rx_dev) hackrf_close(_rx_dev);
if (_tx_dev) hackrf_close(_tx_dev);
std::cout << "Closed Devices\n";
}
/*******************************************************************
* Identification API
******************************************************************/
std::string SoapyHackRFDuplex::getDriverKey(void) const {
return ("HackRFDuplex");
}
std::string SoapyHackRFDuplex::getHardwareKey(void) const {
// TODO: Return a virtual hardware key, not just the RX device
std::lock_guard<std::mutex> lock(_rx_device_mutex);
uint8_t board_id = BOARD_ID_INVALID;
hackrf_board_id_read(_rx_dev, &board_id);
return (hackrf_board_id_name((hackrf_board_id)board_id));
}
SoapySDR::Kwargs SoapyHackRFDuplex::getHardwareInfo(void) const {
std::lock_guard<std::mutex> rx_lock(_rx_device_mutex);
std::lock_guard<std::mutex> tx_lock(_tx_device_mutex);
SoapySDR::Kwargs info;
char version_str[100];
hackrf_version_string_read(_rx_dev, &version_str[0], 100);
info["rx version"] = version_str;
hackrf_version_string_read(_tx_dev, &version_str[0], 100);
info["tx version"] = version_str;
char part_id_str[100];
char serial_str[100];
read_partid_serialno_t read_partid_serialno;
hackrf_board_partid_serialno_read(_rx_dev, &read_partid_serialno);
sprintf(part_id_str, "%08x%08x", read_partid_serialno.part_id[0],
read_partid_serialno.part_id[1]);
info["rx part id"] = part_id_str;
sprintf(serial_str, "%08x%08x%08x%08x", read_partid_serialno.serial_no[0],
read_partid_serialno.serial_no[1], read_partid_serialno.serial_no[2],
read_partid_serialno.serial_no[3]);
info["rx serial"] = serial_str;
hackrf_board_partid_serialno_read(_tx_dev, &read_partid_serialno);
sprintf(part_id_str, "%08x%08x", read_partid_serialno.part_id[0],
read_partid_serialno.part_id[1]);
info["tx part id"] = part_id_str;
sprintf(serial_str, "%08x%08x%08x%08x", read_partid_serialno.serial_no[0],
read_partid_serialno.serial_no[1], read_partid_serialno.serial_no[2],
read_partid_serialno.serial_no[3]);
info["tx serial"] = serial_str;
uint16_t clock;
hackrf_si5351c_read(_rx_dev, 0, &clock);
info["rx clock source"] = (clock == 0x51) ? "internal" : "external";
hackrf_si5351c_read(_tx_dev, 0, &clock);
info["tx clock source"] = (clock == 0x51) ? "internal" : "external";
return (info);
}
/*******************************************************************
* Channels API
******************************************************************/
size_t SoapyHackRFDuplex::getNumChannels(const int dir) const { return (1); }
bool SoapyHackRFDuplex::getFullDuplex(const int direction,
const size_t channel) const {
return (true);
}
/*******************************************************************
* Settings API
******************************************************************/
SoapySDR::ArgInfoList SoapyHackRFDuplex::getSettingInfo(void) const {
SoapySDR::ArgInfoList setArgs;
SoapySDR::ArgInfo biastxArg;
biastxArg.key = "bias_tx";
biastxArg.value = "false";
biastxArg.name = "Antenna Bias";
biastxArg.description = "Antenna port power control.";
biastxArg.type = SoapySDR::ArgInfo::BOOL;
setArgs.push_back(biastxArg);
return setArgs;
}
void SoapyHackRFDuplex::writeSetting(const std::string &key,
const std::string &value) {
if (key == "bias_tx") {
std::lock_guard<std::mutex> lock(_tx_device_mutex);
_tx_stream.bias = (value == "true") ? true : false;
int ret = hackrf_set_antenna_enable(_tx_dev, _tx_stream.bias);
if (ret != HACKRF_SUCCESS) {
SoapySDR_logf(SOAPY_SDR_INFO, "Failed to apply antenna bias voltage");
}
}
}
std::string SoapyHackRFDuplex::readSetting(const std::string &key) const {
if (key == "bias_tx") {
return _tx_stream.bias ? "true" : "false";
}
return "";
}
/*******************************************************************
* Antenna API
******************************************************************/
std::vector<std::string> SoapyHackRFDuplex::listAntennas(
const int direction, const size_t channel) const {
std::vector<std::string> options;
options.push_back("TX/RX");
return (options);
}
void SoapyHackRFDuplex::setAntenna(const int direction, const size_t channel,
const std::string &name) {
/* TODO delete this function or throw if name != RX... */
}
std::string SoapyHackRFDuplex::getAntenna(const int direction,
const size_t channel) const {
return ("TX/RX");
}
/*******************************************************************
* Frontend corrections API
******************************************************************/
bool SoapyHackRFDuplex::hasDCOffsetMode(const int direction,
const size_t channel) const {
return (false);
}
/*******************************************************************
* Gain API
******************************************************************/
std::vector<std::string> SoapyHackRFDuplex::listGains(
const int direction, const size_t channel) const {
std::vector<std::string> options;
if (direction == SOAPY_SDR_RX) {
// in gr-osmosdr/lib/soapy/ soapy_sink_c.cc and soapy_source_c.cc expect
// if_gain at front and bb_gain at back
options.push_back("LNA"); // RX: if_gain
options.push_back("AMP"); // RX: rf_gain
options.push_back("VGA"); // RX: bb_gain
} else {
options.push_back("VGA"); // TX: if_gain
options.push_back("AMP"); // TX: rf_gain
}
return (options);
/*
* list available gain elements,
* the functions below have a "name" parameter
*/
}
void SoapyHackRFDuplex::setGainMode(const int direction, const size_t channel,
const bool automatic) {
/* enable AGC if the hardware supports it, or remove this function */
}
bool SoapyHackRFDuplex::getGainMode(const int direction,
const size_t channel) const {
return (false);
/* ditto for the AGC */
}
void SoapyHackRFDuplex::setGain(const int direction, const size_t channel,
const double value) {
int32_t ret(0), gain(0);
gain = value;
SoapySDR_logf(SOAPY_SDR_DEBUG, "setGain RF %s, channel %d, gain %d",
direction == SOAPY_SDR_RX ? "RX" : "TX", channel, gain);
if (direction == SOAPY_SDR_RX) {
std::lock_guard<std::mutex> lock(_rx_device_mutex);
if (gain <= 0) {
_rx_stream.lna_gain = 0;
_rx_stream.vga_gain = 0;
_rx_current_amp = 0;
} else if (gain <=
(HACKRF_RX_LNA_MAX_DB / 2) + (HACKRF_RX_VGA_MAX_DB / 2)) {
_rx_stream.vga_gain = (gain / 3) & ~0x1;
_rx_stream.lna_gain = gain - _rx_stream.vga_gain;
_rx_current_amp = 0;
} else if (gain <= ((HACKRF_RX_LNA_MAX_DB / 2) +
(HACKRF_RX_VGA_MAX_DB / 2) + HACKRF_AMP_MAX_DB)) {
_rx_current_amp = HACKRF_AMP_MAX_DB;
_rx_stream.vga_gain = ((gain - _rx_current_amp) / 3) & ~0x1;
_rx_stream.lna_gain = gain - _rx_current_amp - _rx_stream.vga_gain;
} else if (gain <= HACKRF_RX_LNA_MAX_DB + HACKRF_RX_VGA_MAX_DB +
HACKRF_AMP_MAX_DB) {
_rx_current_amp = HACKRF_AMP_MAX_DB;
_rx_stream.vga_gain = (gain - _rx_current_amp) *
double(HACKRF_RX_LNA_MAX_DB) /
double(HACKRF_RX_VGA_MAX_DB);
_rx_stream.lna_gain = gain - _rx_current_amp - _rx_stream.vga_gain;
}
_rx_stream.amp_gain = _rx_current_amp;
ret = hackrf_set_lna_gain(_rx_dev, _rx_stream.lna_gain);
ret |= hackrf_set_vga_gain(_rx_dev, _rx_stream.vga_gain);
ret |= hackrf_set_amp_enable(_rx_dev, (_rx_current_amp > 0) ? 1 : 0);
} else if (direction == SOAPY_SDR_TX) {
std::lock_guard<std::mutex> lock(_tx_device_mutex);
if (gain <= 0) {
_tx_current_amp = 0;
_tx_stream.vga_gain = 0;
} else if (gain <= (HACKRF_TX_VGA_MAX_DB / 2)) {
_tx_current_amp = 0;
_tx_stream.vga_gain = gain;
} else if (gain <= HACKRF_TX_VGA_MAX_DB + HACKRF_AMP_MAX_DB) {
_tx_current_amp = HACKRF_AMP_MAX_DB;
_tx_stream.vga_gain = gain - HACKRF_AMP_MAX_DB;
}
_tx_stream.amp_gain = _tx_current_amp;
ret = hackrf_set_txvga_gain(_tx_dev, _tx_stream.vga_gain);
ret |= hackrf_set_amp_enable(_tx_dev, (_tx_current_amp > 0) ? 1 : 0);
}
if (ret != HACKRF_SUCCESS) {
SoapySDR::logf(SOAPY_SDR_ERROR, "setGain(%f) returned %s", value,
hackrf_error_name((hackrf_error)ret));
}
}
void SoapyHackRFDuplex::setGain(const int direction, const size_t channel,
const std::string &name, const double value) {
SoapySDR_logf(SOAPY_SDR_DEBUG, "setGain %s %s, channel %d, gain %d",
name.c_str(), direction == SOAPY_SDR_RX ? "RX" : "TX", channel,
(int)value);
if (name == "AMP") {
if (direction == SOAPY_SDR_RX) {
std::lock_guard<std::mutex> lock(_rx_device_mutex);
_rx_current_amp = value;
_rx_current_amp = (_rx_current_amp > 0) ? HACKRF_AMP_MAX_DB
: 0; // clip to possible values
_rx_stream.amp_gain = _rx_current_amp;
if (_rx_dev != NULL) {
int ret = hackrf_set_amp_enable(_rx_dev, (_rx_current_amp > 0) ? 1 : 0);
if (ret != HACKRF_SUCCESS) {
SoapySDR::logf(SOAPY_SDR_ERROR,
"hackrf_set_amp_enable(%f) returned %s",
_rx_current_amp, hackrf_error_name((hackrf_error)ret));
}
}
} else if (direction == SOAPY_SDR_TX) {
std::lock_guard<std::mutex> lock(_tx_device_mutex);
_tx_current_amp = value;
_tx_current_amp = (_tx_current_amp > 0) ? HACKRF_AMP_MAX_DB
: 0; // clip to possible values
_tx_stream.amp_gain = _tx_current_amp;
if (_tx_dev != NULL) {
int ret = hackrf_set_amp_enable(_tx_dev, (_tx_current_amp > 0) ? 1 : 0);
if (ret != HACKRF_SUCCESS) {
SoapySDR::logf(SOAPY_SDR_ERROR,
"hackrf_set_amp_enable(%f) returned %s",
_tx_current_amp, hackrf_error_name((hackrf_error)ret));
}
}
}
} else if (direction == SOAPY_SDR_RX and name == "LNA") {
std::lock_guard<std::mutex> lock(_rx_device_mutex);
_rx_stream.lna_gain = value;
if (_rx_dev != NULL) {
int ret = hackrf_set_lna_gain(_rx_dev, _rx_stream.lna_gain);
if (ret != HACKRF_SUCCESS) {
SoapySDR::logf(SOAPY_SDR_ERROR, "hackrf_set_lna_gain(%f) returned %s",
_rx_stream.lna_gain,
hackrf_error_name((hackrf_error)ret));
}
}
} else if (direction == SOAPY_SDR_RX and name == "VGA") {
std::lock_guard<std::mutex> lock(_rx_device_mutex);
_rx_stream.vga_gain = value;
if (_rx_dev != NULL) {
int ret = hackrf_set_vga_gain(_rx_dev, _rx_stream.vga_gain);
if (ret != HACKRF_SUCCESS) {
SoapySDR::logf(SOAPY_SDR_ERROR, "hackrf_set_vga_gain(%f) returned %s",
_rx_stream.vga_gain,
hackrf_error_name((hackrf_error)ret));
}
}
} else if (direction == SOAPY_SDR_TX and name == "VGA") {
std::lock_guard<std::mutex> lock(_tx_device_mutex);
_tx_stream.vga_gain = value;
if (_tx_dev != NULL) {
int ret = hackrf_set_txvga_gain(_tx_dev, _tx_stream.vga_gain);
if (ret != HACKRF_SUCCESS) {
SoapySDR::logf(SOAPY_SDR_ERROR, "hackrf_set_txvga_gain(%f) returned %s",
_tx_stream.vga_gain,
hackrf_error_name((hackrf_error)ret));
}
}
}
/* set individual gain element by name */
}
double SoapyHackRFDuplex::getGain(const int direction, const size_t channel,
const std::string &name) const {
double gain = 0.0;
if (direction == SOAPY_SDR_RX and name == "AMP") {
std::lock_guard<std::mutex> lock(_rx_device_mutex);
gain = _rx_stream.amp_gain;
} else if (direction == SOAPY_SDR_TX and name == "AMP") {
std::lock_guard<std::mutex> lock(_tx_device_mutex);
gain = _tx_stream.amp_gain;
} else if (direction == SOAPY_SDR_RX and name == "LNA") {
std::lock_guard<std::mutex> lock(_rx_device_mutex);
gain = _rx_stream.lna_gain;
} else if (direction == SOAPY_SDR_RX and name == "VGA") {
std::lock_guard<std::mutex> lock(_rx_device_mutex);
gain = _rx_stream.vga_gain;
} else if (direction == SOAPY_SDR_TX and name == "VGA") {
std::lock_guard<std::mutex> lock(_tx_device_mutex);
gain = _tx_stream.vga_gain;
}
return (gain);
}
SoapySDR::Range SoapyHackRFDuplex::getGainRange(const int direction,
const size_t channel,
const std::string &name) const {
if (name == "AMP") {
return (SoapySDR::Range(0, HACKRF_AMP_MAX_DB, HACKRF_AMP_MAX_DB));
} else if (direction == SOAPY_SDR_RX and name == "LNA") {
return (SoapySDR::Range(0, HACKRF_RX_LNA_MAX_DB, 8.0));
} else if (direction == SOAPY_SDR_RX and name == "VGA") {
return (SoapySDR::Range(0, HACKRF_RX_VGA_MAX_DB, 2.0));
} else if (direction == SOAPY_SDR_TX and name == "VGA") {
return (SoapySDR::Range(0, HACKRF_TX_VGA_MAX_DB, 1.0));
}
return (SoapySDR::Range(0, 0));
}
/*******************************************************************
* Frequency API
******************************************************************/
void SoapyHackRFDuplex::setFrequency(const int direction, const size_t channel,
const std::string &name,
const double frequency,
const SoapySDR::Kwargs &args) {
SoapySDR_logf(SOAPY_SDR_DEBUG, "Setting Frequency %s Channel %d, Freq. %f Hz...",
direction == SOAPY_SDR_RX ? "RX" : direction == SOAPY_SDR_TX ? "TX" : "<Unknown>",
channel, frequency);
if (name == "BB") return;
if (name != "RF")
throw std::runtime_error("setFrequency(" + name + ") unknown name");
if (direction == SOAPY_SDR_RX) {
std::lock_guard<std::mutex> lock(_rx_device_mutex);
_rx_current_frequency = frequency;
_rx_stream.frequency = _rx_current_frequency;
if (_rx_dev != NULL) {
int ret = hackrf_set_freq(_rx_dev, _rx_current_frequency);
if (ret != HACKRF_SUCCESS) {
SoapySDR::logf(SOAPY_SDR_ERROR, "RX hackrf_set_freq(%f) returned %s",
_rx_current_frequency,
hackrf_error_name((hackrf_error)ret));
}
}
} else if (direction == SOAPY_SDR_TX) {
std::lock_guard<std::mutex> lock(_tx_device_mutex);
_tx_current_frequency = frequency;
_tx_stream.frequency = _tx_current_frequency;
if (_tx_dev != NULL) {
int ret = hackrf_set_freq(_tx_dev, _tx_current_frequency);
if (ret != HACKRF_SUCCESS) {
SoapySDR::logf(SOAPY_SDR_ERROR, "TX hackrf_set_freq(%f) returned %s",
_tx_current_frequency,
hackrf_error_name((hackrf_error)ret));
}
}
}
}
double SoapyHackRFDuplex::getFrequency(const int direction,
const size_t channel,
const std::string &name) const {
if (name == "BB") return (0.0);
if (name != "RF")
throw std::runtime_error("getFrequency(" + name + ") unknown name");
double freq(0.0);
if (direction == SOAPY_SDR_RX) {
std::lock_guard<std::mutex> lock(_rx_device_mutex);
freq = _rx_stream.frequency;
} else if (direction == SOAPY_SDR_TX) {
std::lock_guard<std::mutex> lock(_tx_device_mutex);
freq = _tx_stream.frequency;
}
return (freq);
}
SoapySDR::ArgInfoList SoapyHackRFDuplex::getFrequencyArgsInfo(
const int direction, const size_t channel) const {
SoapySDR::ArgInfoList freqArgs;
// TODO: frequency arguments
return freqArgs;
}
std::vector<std::string> SoapyHackRFDuplex::listFrequencies(
const int direction, const size_t channel) const {
std::vector<std::string> names;
names.push_back("RF");
return (names);
}
SoapySDR::RangeList SoapyHackRFDuplex::getFrequencyRange(
const int direction, const size_t channel, const std::string &name) const {
if (name == "BB") return (SoapySDR::RangeList(1, SoapySDR::Range(0.0, 0.0)));
if (name != "RF")
throw std::runtime_error("getFrequencyRange(" + name + ") unknown name");
return (SoapySDR::RangeList(1, SoapySDR::Range(0, 7250000000ull)));
}
/*******************************************************************
* Sample Rate API
******************************************************************/
void SoapyHackRFDuplex::setSampleRate(const int direction, const size_t channel,
const double rate) {
if (direction == SOAPY_SDR_RX) {
std::lock_guard<std::mutex> lock(_rx_device_mutex);
_rx_current_samplerate = rate;
_rx_stream.samplerate = _rx_current_samplerate;
if (_rx_dev != NULL) {
int ret = hackrf_set_sample_rate(_rx_dev, _rx_current_samplerate);
if (ret != HACKRF_SUCCESS) {
SoapySDR::logf(
SOAPY_SDR_ERROR, "hackrf_set_sample_rate(%f) returned %s",
_rx_current_samplerate, hackrf_error_name((hackrf_error)ret));
throw std::runtime_error("setSampleRate()");
}
}
} else if (direction == SOAPY_SDR_TX) {
std::lock_guard<std::mutex> lock(_tx_device_mutex);
_tx_current_samplerate = rate;
_tx_stream.samplerate = _tx_current_samplerate;
if (_tx_dev != NULL) {
int ret = hackrf_set_sample_rate(_tx_dev, _tx_current_samplerate);
if (ret != HACKRF_SUCCESS) {
SoapySDR::logf(
SOAPY_SDR_ERROR, "hackrf_set_sample_rate(%f) returned %s",
_tx_current_samplerate, hackrf_error_name((hackrf_error)ret));
throw std::runtime_error("setSampleRate()");
}
}
}
}
double SoapyHackRFDuplex::getSampleRate(const int direction,
const size_t channel) const {
double samp(0.0);
if (direction == SOAPY_SDR_RX) {
std::lock_guard<std::mutex> lock(_rx_device_mutex);
samp = _rx_stream.samplerate;
}
if (direction == SOAPY_SDR_TX) {
std::lock_guard<std::mutex> lock(_tx_device_mutex);
samp = _tx_stream.samplerate;
}
return (samp);
}
std::vector<double> SoapyHackRFDuplex::listSampleRates(
const int direction, const size_t channel) const {
std::vector<double> options;
for (double r = 1e6; r <= 20e6; r += 1e6) {
options.push_back(r);
}
return (options);
}
void SoapyHackRFDuplex::setBandwidth(const int direction, const size_t channel,
const double bw) {
if (direction == SOAPY_SDR_RX) {
std::lock_guard<std::mutex> lock(_rx_device_mutex);
_rx_current_bandwidth = bw;
_rx_stream.bandwidth = _rx_current_bandwidth;
if (_rx_current_bandwidth > 0) {
_rx_auto_bandwidth = false;
if (_rx_dev != NULL) {
int ret = hackrf_set_baseband_filter_bandwidth(_rx_dev,
_rx_current_bandwidth);
if (ret != HACKRF_SUCCESS) {
SoapySDR::logf(SOAPY_SDR_ERROR,
"hackrf_set_baseband_filter_bandwidth(%f) returned %s",
_rx_current_bandwidth,
hackrf_error_name((hackrf_error)ret));
throw std::runtime_error("setBandwidth()");
}
}
} else {
_rx_auto_bandwidth = true;
}
} else if (direction == SOAPY_SDR_TX) {
std::lock_guard<std::mutex> lock(_tx_device_mutex);
_tx_current_bandwidth = bw;
_tx_stream.bandwidth = _tx_current_bandwidth;
if (_tx_current_bandwidth > 0) {
_tx_auto_bandwidth = false;
if (_tx_dev != NULL) {
int ret = hackrf_set_baseband_filter_bandwidth(_tx_dev,
_tx_current_bandwidth);
if (ret != HACKRF_SUCCESS) {
SoapySDR::logf(SOAPY_SDR_ERROR,
"hackrf_set_baseband_filter_bandwidth(%f) returned %s",
_tx_current_bandwidth,
hackrf_error_name((hackrf_error)ret));
throw std::runtime_error("setBandwidth()");
}
}
} else {
_tx_auto_bandwidth = true;
}
}
}
double SoapyHackRFDuplex::getBandwidth(const int direction,
const size_t channel) const {
double bw(0.0);
if (direction == SOAPY_SDR_RX) {
std::lock_guard<std::mutex> lock(_rx_device_mutex);
bw = _rx_stream.bandwidth;
}
if (direction == SOAPY_SDR_TX) {
std::lock_guard<std::mutex> lock(_tx_device_mutex);
bw = _tx_stream.bandwidth;
}
return (bw);
}
std::vector<double> SoapyHackRFDuplex::listBandwidths(
const int direction, const size_t channel) const {
std::vector<double> options;
options.push_back(1750000);
options.push_back(2500000);
options.push_back(3500000);
options.push_back(5000000);
options.push_back(5500000);
options.push_back(6000000);
options.push_back(7000000);
options.push_back(8000000);
options.push_back(9000000);
options.push_back(10000000);
options.push_back(12000000);
options.push_back(14000000);
options.push_back(15000000);
options.push_back(20000000);
options.push_back(24000000);
options.push_back(28000000);
return (options);
}