2021-03-20 21:53:44 +01:00
|
|
|
#include <imgui.h>
|
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
#include <module.h>
|
|
|
|
#include <gui/gui.h>
|
|
|
|
#include <signal_path/signal_path.h>
|
|
|
|
#include <core.h>
|
|
|
|
#include <gui/style.h>
|
|
|
|
#include <config.h>
|
|
|
|
#include <gui/widgets/stepped_slider.h>
|
|
|
|
#include <libbladeRF.h>
|
2022-01-21 20:22:13 +01:00
|
|
|
#include <gui/smgui.h>
|
2021-06-26 18:26:58 +02:00
|
|
|
#include <algorithm>
|
2021-03-20 21:53:44 +01:00
|
|
|
|
|
|
|
#define CONCAT(a, b) ((std::string(a) + b).c_str())
|
|
|
|
|
2021-12-19 22:11:44 +01:00
|
|
|
#define NUM_BUFFERS 128
|
|
|
|
#define NUM_TRANSFERS 1
|
2021-03-20 21:53:44 +01:00
|
|
|
|
2021-12-19 22:11:44 +01:00
|
|
|
SDRPP_MOD_INFO{
|
2021-03-20 21:53:44 +01:00
|
|
|
/* Name: */ "bladerf_source",
|
|
|
|
/* Description: */ "BladeRF source module for SDR++",
|
|
|
|
/* Author: */ "Ryzerth",
|
|
|
|
/* Version: */ 0, 1, 0,
|
|
|
|
/* Max instances */ 1
|
|
|
|
};
|
|
|
|
|
|
|
|
ConfigManager config;
|
|
|
|
|
2021-10-12 19:45:42 +02:00
|
|
|
enum BladeRFType {
|
|
|
|
BLADERF_TYPE_UNKNOWN,
|
|
|
|
BLADERF_TYPE_V1,
|
|
|
|
BLADERF_TYPE_V2
|
|
|
|
};
|
|
|
|
|
2021-03-20 21:53:44 +01:00
|
|
|
class BladeRFSourceModule : public ModuleManager::Instance {
|
|
|
|
public:
|
|
|
|
BladeRFSourceModule(std::string name) {
|
|
|
|
this->name = name;
|
|
|
|
|
2021-05-03 04:20:48 +02:00
|
|
|
sampleRate = 1000000.0;
|
2021-03-20 21:53:44 +01:00
|
|
|
|
|
|
|
handler.ctx = this;
|
|
|
|
handler.selectHandler = menuSelected;
|
|
|
|
handler.deselectHandler = menuDeselected;
|
|
|
|
handler.menuHandler = menuHandler;
|
|
|
|
handler.startHandler = start;
|
|
|
|
handler.stopHandler = stop;
|
|
|
|
handler.tuneHandler = tune;
|
|
|
|
handler.stream = &stream;
|
|
|
|
|
|
|
|
refresh();
|
|
|
|
|
|
|
|
// Select device here
|
2021-07-09 14:24:07 -04:00
|
|
|
config.acquire();
|
2021-06-26 18:26:58 +02:00
|
|
|
std::string serial = config.conf["device"];
|
|
|
|
config.release();
|
|
|
|
selectBySerial(serial);
|
2021-03-20 21:53:44 +01:00
|
|
|
|
|
|
|
sigpath::sourceManager.registerSource("BladeRF", &handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
~BladeRFSourceModule() {
|
2021-07-26 03:11:51 +02:00
|
|
|
stop(this);
|
|
|
|
sigpath::sourceManager.unregisterSource("BladeRF");
|
2021-03-20 21:53:44 +01:00
|
|
|
}
|
|
|
|
|
2021-07-26 03:11:51 +02:00
|
|
|
void postInit() {}
|
|
|
|
|
2021-03-20 21:53:44 +01:00
|
|
|
void enable() {
|
|
|
|
enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void disable() {
|
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isEnabled() {
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void refresh() {
|
|
|
|
devListTxt = "";
|
|
|
|
|
|
|
|
if (devInfoList != NULL) {
|
|
|
|
bladerf_free_device_list(devInfoList);
|
|
|
|
}
|
|
|
|
|
|
|
|
devCount = bladerf_get_device_list(&devInfoList);
|
|
|
|
if (devCount < 0) {
|
2021-10-18 00:38:02 +02:00
|
|
|
spdlog::error("Could not list devices {0}", devCount);
|
2021-03-20 21:53:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < devCount; i++) {
|
2021-05-03 04:20:48 +02:00
|
|
|
// Keep only the first 32 character of the serial number for display
|
2021-05-03 18:10:52 +02:00
|
|
|
devListTxt += std::string(devInfoList[i].serial).substr(0, 16);
|
2021-03-20 21:53:44 +01:00
|
|
|
devListTxt += '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void selectFirst() {
|
|
|
|
if (devCount > 0) { selectByInfo(&devInfoList[0]); }
|
2021-12-19 22:11:44 +01:00
|
|
|
else {
|
|
|
|
selectedSerial = "";
|
|
|
|
}
|
2021-03-20 21:53:44 +01:00
|
|
|
}
|
|
|
|
|
2021-06-26 18:26:58 +02:00
|
|
|
void selectBySerial(std::string serial, bool reloadChannelId = true) {
|
2021-12-19 22:11:44 +01:00
|
|
|
if (serial == "") {
|
2021-06-26 18:26:58 +02:00
|
|
|
selectFirst();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < devCount; i++) {
|
|
|
|
bladerf_devinfo info = devInfoList[i];
|
|
|
|
if (serial == info.serial) {
|
|
|
|
devId = i;
|
|
|
|
selectByInfo(&info, reloadChannelId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-06-30 03:13:14 +02:00
|
|
|
selectFirst();
|
2021-06-26 18:26:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void selectByInfo(bladerf_devinfo* info, bool reloadChannelId = true) {
|
2021-03-20 21:53:44 +01:00
|
|
|
int ret = bladerf_open_with_devinfo(&openDev, info);
|
|
|
|
if (ret != 0) {
|
|
|
|
spdlog::error("Could not open device {0}", info->serial);
|
2021-06-26 18:26:58 +02:00
|
|
|
selectedSerial = "";
|
2021-03-20 21:53:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-26 18:26:58 +02:00
|
|
|
selectedSerial = info->serial;
|
|
|
|
for (int i = 0; i < devCount; i++) {
|
|
|
|
if (selectedSerial == devInfoList[i].serial) { devId = i; }
|
|
|
|
}
|
|
|
|
|
2021-10-12 19:45:42 +02:00
|
|
|
// Get the board type
|
|
|
|
const char* bname = bladerf_get_board_name(openDev);
|
2021-12-19 22:11:44 +01:00
|
|
|
if (!strcmp(bname, "bladerf1")) {
|
2021-10-12 19:45:42 +02:00
|
|
|
selectedBladeType = BLADERF_TYPE_V1;
|
|
|
|
}
|
2021-12-19 22:11:44 +01:00
|
|
|
else if (!strcmp(bname, "bladerf2")) {
|
2021-10-12 19:45:42 +02:00
|
|
|
selectedBladeType = BLADERF_TYPE_V2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
selectedBladeType = BLADERF_TYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2021-05-03 04:20:48 +02:00
|
|
|
// Gather info about the BladeRF's ranges
|
2021-03-20 21:53:44 +01:00
|
|
|
channelCount = bladerf_get_channel_count(openDev, BLADERF_RX);
|
2021-06-26 18:26:58 +02:00
|
|
|
|
|
|
|
// Load the channelId if there are more than 1 channel
|
|
|
|
if (reloadChannelId) {
|
2021-07-09 14:24:07 -04:00
|
|
|
config.acquire();
|
2021-06-26 18:26:58 +02:00
|
|
|
if (channelCount > 1 && config.conf["devices"].contains(info->serial)) {
|
|
|
|
if (config.conf["devices"][info->serial].contains("channelId")) {
|
|
|
|
chanId = config.conf["devices"][info->serial]["channelId"];
|
|
|
|
}
|
2021-12-19 22:11:44 +01:00
|
|
|
else {
|
|
|
|
chanId = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
chanId = 0;
|
2021-06-26 18:26:58 +02:00
|
|
|
}
|
|
|
|
config.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
chanId = std::clamp<int>(chanId, 0, channelCount - 1);
|
|
|
|
|
|
|
|
bladerf_get_sample_rate_range(openDev, BLADERF_CHANNEL_RX(chanId), &srRange);
|
|
|
|
bladerf_get_bandwidth_range(openDev, BLADERF_CHANNEL_RX(chanId), &bwRange);
|
|
|
|
bladerf_get_gain_range(openDev, BLADERF_CHANNEL_RX(chanId), &gainRange);
|
2021-06-27 02:20:11 +02:00
|
|
|
int gainModeCount = bladerf_get_gain_modes(openDev, BLADERF_CHANNEL_RX(chanId), &gainModes);
|
2021-05-02 02:23:10 +02:00
|
|
|
|
2021-05-03 18:10:52 +02:00
|
|
|
// Generate sampleRate and Bandwidth lists
|
|
|
|
sampleRates.clear();
|
|
|
|
sampleRatesTxt = "";
|
|
|
|
sampleRates.push_back(srRange->min);
|
|
|
|
sampleRatesTxt += getBandwdithScaled(srRange->min);
|
|
|
|
sampleRatesTxt += '\0';
|
|
|
|
for (int i = 2000000; i < srRange->max; i += 2000000) {
|
|
|
|
sampleRates.push_back(i);
|
|
|
|
sampleRatesTxt += getBandwdithScaled(i);
|
|
|
|
sampleRatesTxt += '\0';
|
|
|
|
}
|
|
|
|
sampleRates.push_back(srRange->max);
|
|
|
|
sampleRatesTxt += getBandwdithScaled(srRange->max);
|
|
|
|
sampleRatesTxt += '\0';
|
|
|
|
|
|
|
|
// Generate bandwidth list
|
|
|
|
bandwidths.clear();
|
|
|
|
bandwidthsTxt = "";
|
|
|
|
bandwidths.push_back(bwRange->min);
|
|
|
|
bandwidthsTxt += getBandwdithScaled(bwRange->min);
|
|
|
|
bandwidthsTxt += '\0';
|
|
|
|
for (int i = 2000000; i < bwRange->max; i += 2000000) {
|
|
|
|
bandwidths.push_back(i);
|
|
|
|
bandwidthsTxt += getBandwdithScaled(i);
|
|
|
|
bandwidthsTxt += '\0';
|
|
|
|
}
|
|
|
|
bandwidths.push_back(bwRange->max);
|
|
|
|
bandwidthsTxt += getBandwdithScaled(bwRange->max);
|
|
|
|
bandwidthsTxt += '\0';
|
2021-05-03 20:21:30 +02:00
|
|
|
bandwidthsTxt += "Auto";
|
|
|
|
bandwidthsTxt += '\0';
|
2021-05-03 18:10:52 +02:00
|
|
|
|
2021-06-26 18:26:58 +02:00
|
|
|
// Generate list of channel names
|
|
|
|
channelNamesTxt = "";
|
|
|
|
char buf[32];
|
|
|
|
for (int i = 0; i < channelCount; i++) {
|
2021-12-19 22:11:44 +01:00
|
|
|
sprintf(buf, "RX %d", i + 1);
|
2021-06-26 18:26:58 +02:00
|
|
|
channelNamesTxt += buf;
|
|
|
|
channelNamesTxt += '\0';
|
|
|
|
}
|
|
|
|
|
2021-06-27 02:20:11 +02:00
|
|
|
// Generate gain mode list
|
|
|
|
gainModeNames.clear();
|
|
|
|
gainModesTxt = "";
|
|
|
|
for (int i = 0; i < gainModeCount; i++) {
|
|
|
|
std::string gm = gainModes[i].name;
|
|
|
|
gm[0] = gm[0] & (~0x20);
|
|
|
|
gainModeNames.push_back(gm);
|
|
|
|
gainModesTxt += gm;
|
|
|
|
gainModesTxt += '\0';
|
|
|
|
}
|
|
|
|
|
2021-06-26 18:26:58 +02:00
|
|
|
// Load settings here
|
2021-07-09 14:24:07 -04:00
|
|
|
config.acquire();
|
2021-06-27 02:20:11 +02:00
|
|
|
|
|
|
|
if (!config.conf["devices"].contains(selectedSerial)) {
|
|
|
|
config.conf["devices"][info->serial]["channelId"] = 0;
|
|
|
|
config.conf["devices"][selectedSerial]["sampleRate"] = sampleRates[0];
|
|
|
|
config.conf["devices"][selectedSerial]["bandwidth"] = bandwidths.size(); // Auto
|
|
|
|
config.conf["devices"][selectedSerial]["gainMode"] = "Manual";
|
|
|
|
config.conf["devices"][selectedSerial]["overallGain"] = gainRange->min;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load sample rate
|
|
|
|
if (config.conf["devices"][selectedSerial].contains("sampleRate")) {
|
|
|
|
bool found = false;
|
|
|
|
uint64_t sr = config.conf["devices"][selectedSerial]["sampleRate"];
|
|
|
|
for (int i = 0; i < sampleRates.size(); i++) {
|
|
|
|
if (sr == sampleRates[i]) {
|
|
|
|
srId = i;
|
|
|
|
sampleRate = sampleRates[i];
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
srId = 0;
|
|
|
|
sampleRate = sampleRates[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
srId = 0;
|
|
|
|
sampleRate = sampleRates[0];
|
|
|
|
}
|
2021-06-26 18:26:58 +02:00
|
|
|
|
2021-06-27 02:20:11 +02:00
|
|
|
// Load bandwidth
|
|
|
|
if (config.conf["devices"][selectedSerial].contains("bandwidth")) {
|
|
|
|
bwId = config.conf["devices"][selectedSerial]["bandwidth"];
|
|
|
|
bwId = std::clamp<int>(bwId, 0, bandwidths.size());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bwId = 0;
|
|
|
|
}
|
|
|
|
config.release(true);
|
|
|
|
|
|
|
|
// Load gain mode
|
|
|
|
if (config.conf["devices"][selectedSerial].contains("gainMode")) {
|
|
|
|
std::string gm = config.conf["devices"][selectedSerial]["gainMode"];
|
|
|
|
bool found = false;
|
|
|
|
for (int i = 0; i < gainModeNames.size(); i++) {
|
|
|
|
if (gainModeNames[i] == gm) {
|
|
|
|
gainMode = i;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
for (int i = 0; i < gainModeNames.size(); i++) {
|
|
|
|
if (gainModeNames[i] == "Manual") {
|
|
|
|
gainMode = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (int i = 0; i < gainModeNames.size(); i++) {
|
|
|
|
if (gainModeNames[i] == "Manual") {
|
|
|
|
gainMode = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-20 21:53:44 +01:00
|
|
|
|
2021-06-27 02:20:11 +02:00
|
|
|
// Load gain
|
|
|
|
if (config.conf["devices"][selectedSerial].contains("overallGain")) {
|
|
|
|
overallGain = config.conf["devices"][selectedSerial]["overallGain"];
|
|
|
|
overallGain = std::clamp<int>(overallGain, gainRange->min, gainRange->max);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
overallGain = gainRange->min;
|
|
|
|
}
|
2021-03-20 21:53:44 +01:00
|
|
|
|
2021-10-12 19:45:42 +02:00
|
|
|
// Load Bias-T
|
|
|
|
if (selectedBladeType == BLADERF_TYPE_V2) {
|
|
|
|
if (config.conf["devices"][selectedSerial].contains("biasT")) {
|
|
|
|
biasT = config.conf["devices"][selectedSerial]["biasT"];
|
|
|
|
}
|
|
|
|
else {
|
2021-10-13 00:22:10 +02:00
|
|
|
biasT = false;
|
2021-10-12 19:45:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 21:53:44 +01:00
|
|
|
bladerf_close(openDev);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string getBandwdithScaled(double bw) {
|
|
|
|
char buf[1024];
|
|
|
|
if (bw >= 1000000.0) {
|
|
|
|
sprintf(buf, "%.1lfMHz", bw / 1000000.0);
|
|
|
|
}
|
|
|
|
else if (bw >= 1000.0) {
|
|
|
|
sprintf(buf, "%.1lfKHz", bw / 1000.0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf(buf, "%.1lfHz", bw);
|
|
|
|
}
|
|
|
|
return std::string(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void menuSelected(void* ctx) {
|
|
|
|
BladeRFSourceModule* _this = (BladeRFSourceModule*)ctx;
|
|
|
|
core::setInputSampleRate(_this->sampleRate);
|
|
|
|
spdlog::info("BladeRFSourceModule '{0}': Menu Select!", _this->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void menuDeselected(void* ctx) {
|
|
|
|
BladeRFSourceModule* _this = (BladeRFSourceModule*)ctx;
|
|
|
|
spdlog::info("BladeRFSourceModule '{0}': Menu Deselect!", _this->name);
|
|
|
|
}
|
2021-12-19 22:11:44 +01:00
|
|
|
|
2021-03-20 21:53:44 +01:00
|
|
|
static void start(void* ctx) {
|
|
|
|
BladeRFSourceModule* _this = (BladeRFSourceModule*)ctx;
|
2021-07-26 04:16:00 +02:00
|
|
|
if (_this->running) { return; }
|
2021-03-20 21:53:44 +01:00
|
|
|
if (_this->devCount == 0) { return; }
|
|
|
|
|
|
|
|
// Open device
|
|
|
|
bladerf_devinfo info = _this->devInfoList[_this->devId];
|
|
|
|
int ret = bladerf_open_with_devinfo(&_this->openDev, &info);
|
|
|
|
if (ret != 0) {
|
|
|
|
spdlog::error("Could not open device {0}", info.serial);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-03 04:20:48 +02:00
|
|
|
// Calculate buffer size, must be a multiple of 1024
|
2021-05-02 02:23:10 +02:00
|
|
|
_this->bufferSize = _this->sampleRate / 200.0;
|
|
|
|
_this->bufferSize /= 1024;
|
|
|
|
_this->bufferSize *= 1024;
|
2021-05-03 02:08:49 +02:00
|
|
|
if (_this->bufferSize < 1024) { _this->bufferSize = 1024; }
|
2021-05-02 02:23:10 +02:00
|
|
|
|
2021-05-03 04:20:48 +02:00
|
|
|
// Setup device parameters
|
2021-06-26 18:26:58 +02:00
|
|
|
bladerf_set_sample_rate(_this->openDev, BLADERF_CHANNEL_RX(_this->chanId), _this->sampleRate, NULL);
|
|
|
|
bladerf_set_frequency(_this->openDev, BLADERF_CHANNEL_RX(_this->chanId), _this->freq);
|
2021-12-19 22:11:44 +01:00
|
|
|
bladerf_set_bandwidth(_this->openDev, BLADERF_CHANNEL_RX(_this->chanId), (_this->bwId == _this->bandwidths.size()) ? std::clamp<uint64_t>(_this->sampleRate, _this->bwRange->min, _this->bwRange->max) : _this->bandwidths[_this->bwId], NULL);
|
2021-06-27 02:20:11 +02:00
|
|
|
bladerf_set_gain_mode(_this->openDev, BLADERF_CHANNEL_RX(_this->chanId), _this->gainModes[_this->gainMode].mode);
|
|
|
|
|
2021-10-12 19:45:42 +02:00
|
|
|
if (_this->selectedBladeType == BLADERF_TYPE_V2) {
|
|
|
|
bladerf_set_bias_tee(_this->openDev, BLADERF_CHANNEL_RX(_this->chanId), _this->biasT);
|
|
|
|
}
|
|
|
|
|
2021-06-27 02:20:11 +02:00
|
|
|
// If gain mode is manual, set the gain
|
|
|
|
if (_this->gainModes[_this->gainMode].mode == BLADERF_GAIN_MANUAL) {
|
|
|
|
bladerf_set_gain(_this->openDev, BLADERF_CHANNEL_RX(_this->chanId), _this->overallGain);
|
|
|
|
}
|
2021-12-19 22:11:44 +01:00
|
|
|
|
2021-06-27 02:20:11 +02:00
|
|
|
_this->streamingEnabled = true;
|
2021-03-20 21:53:44 +01:00
|
|
|
|
2021-09-20 19:59:35 +02:00
|
|
|
// Setup synchronous transfer
|
2021-05-02 02:23:10 +02:00
|
|
|
bladerf_sync_config(_this->openDev, BLADERF_RX_X1, BLADERF_FORMAT_SC16_Q11, 16, _this->bufferSize, 8, 3500);
|
2021-03-20 21:53:44 +01:00
|
|
|
|
2021-05-03 04:20:48 +02:00
|
|
|
// Enable streaming
|
2021-06-26 18:26:58 +02:00
|
|
|
bladerf_enable_module(_this->openDev, BLADERF_CHANNEL_RX(_this->chanId), true);
|
2021-03-20 21:53:44 +01:00
|
|
|
|
|
|
|
_this->running = true;
|
|
|
|
_this->workerThread = std::thread(&BladeRFSourceModule::worker, _this);
|
|
|
|
|
|
|
|
spdlog::info("BladeRFSourceModule '{0}': Start!", _this->name);
|
|
|
|
}
|
2021-12-19 22:11:44 +01:00
|
|
|
|
2021-03-20 21:53:44 +01:00
|
|
|
static void stop(void* ctx) {
|
|
|
|
BladeRFSourceModule* _this = (BladeRFSourceModule*)ctx;
|
2021-07-26 04:16:00 +02:00
|
|
|
if (!_this->running) { return; }
|
2021-03-20 21:53:44 +01:00
|
|
|
_this->running = false;
|
|
|
|
_this->stream.stopWriter();
|
2021-05-03 04:20:48 +02:00
|
|
|
|
2021-06-27 02:20:11 +02:00
|
|
|
_this->streamingEnabled = false;
|
2021-05-03 04:20:48 +02:00
|
|
|
// Wait for read worker to terminate
|
2021-03-20 21:53:44 +01:00
|
|
|
if (_this->workerThread.joinable()) {
|
|
|
|
_this->workerThread.join();
|
|
|
|
}
|
2021-12-19 22:11:44 +01:00
|
|
|
|
2021-06-27 02:20:11 +02:00
|
|
|
// Disable streaming
|
|
|
|
bladerf_enable_module(_this->openDev, BLADERF_CHANNEL_RX(_this->chanId), false);
|
2021-03-20 21:53:44 +01:00
|
|
|
|
2021-05-03 04:20:48 +02:00
|
|
|
// Close device
|
2021-03-20 21:53:44 +01:00
|
|
|
bladerf_close(_this->openDev);
|
|
|
|
|
|
|
|
_this->stream.clearWriteStop();
|
|
|
|
spdlog::info("BladeRFSourceModule '{0}': Stop!", _this->name);
|
|
|
|
}
|
2021-12-19 22:11:44 +01:00
|
|
|
|
2021-03-20 21:53:44 +01:00
|
|
|
static void tune(double freq, void* ctx) {
|
|
|
|
BladeRFSourceModule* _this = (BladeRFSourceModule*)ctx;
|
|
|
|
_this->freq = freq;
|
|
|
|
if (_this->running) {
|
2021-06-26 18:26:58 +02:00
|
|
|
bladerf_set_frequency(_this->openDev, BLADERF_CHANNEL_RX(_this->chanId), _this->freq);
|
2021-03-20 21:53:44 +01:00
|
|
|
}
|
|
|
|
spdlog::info("BladeRFSourceModule '{0}': Tune: {1}!", _this->name, freq);
|
|
|
|
}
|
2021-12-19 22:11:44 +01:00
|
|
|
|
2021-03-20 21:53:44 +01:00
|
|
|
static void menuHandler(void* ctx) {
|
|
|
|
BladeRFSourceModule* _this = (BladeRFSourceModule*)ctx;
|
|
|
|
|
2022-01-21 20:22:13 +01:00
|
|
|
if (_this->running) { SmGui::BeginDisabled(); }
|
2021-03-20 21:53:44 +01:00
|
|
|
|
2022-01-21 20:22:13 +01:00
|
|
|
SmGui::FillWidth();
|
|
|
|
SmGui::ForceSync();
|
|
|
|
if (SmGui::Combo(CONCAT("##_balderf_dev_sel_", _this->name), &_this->devId, _this->devListTxt.c_str())) {
|
2021-06-26 18:26:58 +02:00
|
|
|
bladerf_devinfo info = _this->devInfoList[_this->devId];
|
|
|
|
_this->selectByInfo(&info);
|
2021-03-20 21:53:44 +01:00
|
|
|
core::setInputSampleRate(_this->sampleRate);
|
2021-07-09 14:24:07 -04:00
|
|
|
config.acquire();
|
2021-06-26 18:26:58 +02:00
|
|
|
config.conf["device"] = _this->selectedSerial;
|
|
|
|
config.release(true);
|
2021-03-20 21:53:44 +01:00
|
|
|
}
|
|
|
|
|
2022-01-21 20:22:13 +01:00
|
|
|
if (SmGui::Combo(CONCAT("##_balderf_sr_sel_", _this->name), &_this->srId, _this->sampleRatesTxt.c_str())) {
|
2021-05-03 18:10:52 +02:00
|
|
|
_this->sampleRate = _this->sampleRates[_this->srId];
|
2021-03-20 21:53:44 +01:00
|
|
|
core::setInputSampleRate(_this->sampleRate);
|
2021-06-27 02:20:11 +02:00
|
|
|
if (_this->selectedSerial != "") {
|
2021-07-09 14:24:07 -04:00
|
|
|
config.acquire();
|
2021-06-27 02:20:11 +02:00
|
|
|
config.conf["devices"][_this->selectedSerial]["sampleRate"] = _this->sampleRates[_this->srId];
|
|
|
|
config.release(true);
|
|
|
|
}
|
2021-03-20 21:53:44 +01:00
|
|
|
}
|
|
|
|
|
2021-06-26 18:26:58 +02:00
|
|
|
// Refresh button
|
2022-01-21 20:22:13 +01:00
|
|
|
SmGui::SameLine();
|
|
|
|
SmGui::FillWidth();
|
|
|
|
SmGui::ForceSync();
|
|
|
|
if (SmGui::Button(CONCAT("Refresh##_balderf_refr_", _this->name))) {
|
2021-03-20 21:53:44 +01:00
|
|
|
_this->refresh();
|
2021-06-26 18:26:58 +02:00
|
|
|
_this->selectBySerial(_this->selectedSerial, false);
|
2021-03-20 21:53:44 +01:00
|
|
|
core::setInputSampleRate(_this->sampleRate);
|
|
|
|
}
|
|
|
|
|
2021-06-26 18:26:58 +02:00
|
|
|
// Channel selection (only show if more than one channel)
|
2021-06-27 02:20:11 +02:00
|
|
|
if (_this->channelCount > 1) {
|
2022-01-21 20:22:13 +01:00
|
|
|
SmGui::LeftLabel("RX Channel");
|
|
|
|
SmGui::FillWidth();
|
|
|
|
SmGui::Combo(CONCAT("##_balderf_ch_sel_", _this->name), &_this->chanId, _this->channelNamesTxt.c_str());
|
2021-06-27 02:20:11 +02:00
|
|
|
if (_this->selectedSerial != "") {
|
2021-07-09 14:24:07 -04:00
|
|
|
config.acquire();
|
2021-06-27 02:20:11 +02:00
|
|
|
config.conf["devices"][_this->selectedSerial]["channelId"] = _this->chanId;
|
|
|
|
config.release(true);
|
|
|
|
}
|
|
|
|
}
|
2021-06-26 18:26:58 +02:00
|
|
|
|
2022-01-21 20:22:13 +01:00
|
|
|
if (_this->running) { SmGui::EndDisabled(); }
|
2021-03-20 21:53:44 +01:00
|
|
|
|
2022-01-21 20:22:13 +01:00
|
|
|
SmGui::LeftLabel("Bandwidth");
|
|
|
|
SmGui::FillWidth();
|
|
|
|
if (SmGui::Combo(CONCAT("##_balderf_bw_sel_", _this->name), &_this->bwId, _this->bandwidthsTxt.c_str())) {
|
2021-05-03 18:10:52 +02:00
|
|
|
if (_this->running) {
|
2021-12-19 22:11:44 +01:00
|
|
|
bladerf_set_bandwidth(_this->openDev, BLADERF_CHANNEL_RX(_this->chanId), (_this->bwId == _this->bandwidths.size()) ? std::clamp<uint64_t>(_this->sampleRate, _this->bwRange->min, _this->bwRange->max) : _this->bandwidths[_this->bwId], NULL);
|
2021-05-03 18:10:52 +02:00
|
|
|
}
|
2021-06-27 02:20:11 +02:00
|
|
|
if (_this->selectedSerial != "") {
|
2021-07-09 14:24:07 -04:00
|
|
|
config.acquire();
|
2021-06-27 02:20:11 +02:00
|
|
|
config.conf["devices"][_this->selectedSerial]["bandwidth"] = _this->bwId;
|
|
|
|
config.release(true);
|
|
|
|
}
|
2021-05-03 18:10:52 +02:00
|
|
|
}
|
|
|
|
|
2021-03-20 21:53:44 +01:00
|
|
|
// General config BS
|
2022-01-21 20:22:13 +01:00
|
|
|
SmGui::LeftLabel("Gain control mode");
|
|
|
|
SmGui::FillWidth();
|
|
|
|
SmGui::ForceSync();
|
|
|
|
if (SmGui::Combo(CONCAT("##_balderf_gm_sel_", _this->name), &_this->gainMode, _this->gainModesTxt.c_str()) && _this->selectedSerial != "") {
|
2021-05-02 02:23:10 +02:00
|
|
|
if (_this->running) {
|
2021-06-27 02:20:11 +02:00
|
|
|
bladerf_set_gain_mode(_this->openDev, BLADERF_CHANNEL_RX(_this->chanId), _this->gainModes[_this->gainMode].mode);
|
|
|
|
}
|
|
|
|
// if switched to manual, reset gains
|
|
|
|
if (_this->gainModes[_this->gainMode].mode == BLADERF_GAIN_MANUAL && _this->running) {
|
|
|
|
bladerf_set_gain(_this->openDev, BLADERF_CHANNEL_RX(_this->chanId), _this->overallGain);
|
|
|
|
}
|
|
|
|
if (_this->selectedSerial != "") {
|
2021-07-09 14:24:07 -04:00
|
|
|
config.acquire();
|
2021-06-27 02:20:11 +02:00
|
|
|
config.conf["devices"][_this->selectedSerial]["gainMode"] = _this->gainModeNames[_this->gainMode];
|
|
|
|
config.release(true);
|
2021-05-02 02:23:10 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-20 21:53:44 +01:00
|
|
|
|
2021-12-19 22:11:44 +01:00
|
|
|
if (_this->selectedSerial != "") {
|
2022-01-21 20:22:13 +01:00
|
|
|
if (_this->gainModes[_this->gainMode].mode != BLADERF_GAIN_MANUAL) { SmGui::BeginDisabled(); }
|
2021-12-19 22:11:44 +01:00
|
|
|
}
|
2022-01-21 20:22:13 +01:00
|
|
|
SmGui::LeftLabel("Gain");
|
|
|
|
SmGui::FillWidth();
|
|
|
|
if (SmGui::SliderInt("##_balderf_oag_sel_", &_this->overallGain, (_this->gainRange != NULL) ? _this->gainRange->min : 0, (_this->gainRange != NULL) ? _this->gainRange->max : 60)) {
|
2021-06-27 02:20:11 +02:00
|
|
|
if (_this->running) {
|
|
|
|
spdlog::info("Setting gain to {0}", _this->overallGain);
|
|
|
|
bladerf_set_gain(_this->openDev, BLADERF_CHANNEL_RX(_this->chanId), _this->overallGain);
|
|
|
|
}
|
|
|
|
if (_this->selectedSerial != "") {
|
2021-07-09 14:24:07 -04:00
|
|
|
config.acquire();
|
2021-06-27 02:20:11 +02:00
|
|
|
config.conf["devices"][_this->selectedSerial]["overallGain"] = _this->overallGain;
|
|
|
|
config.release(true);
|
|
|
|
}
|
|
|
|
}
|
2021-12-19 22:11:44 +01:00
|
|
|
if (_this->selectedSerial != "") {
|
2022-01-21 20:22:13 +01:00
|
|
|
if (_this->gainModes[_this->gainMode].mode != BLADERF_GAIN_MANUAL) { SmGui::EndDisabled(); }
|
2021-12-19 22:11:44 +01:00
|
|
|
}
|
2021-06-27 02:20:11 +02:00
|
|
|
|
2021-10-12 19:45:42 +02:00
|
|
|
if (_this->selectedBladeType == BLADERF_TYPE_V2) {
|
2022-01-21 20:22:13 +01:00
|
|
|
if (SmGui::Checkbox("Bias-T##_balderf_biast_", &_this->biasT)) {
|
2021-10-12 19:45:42 +02:00
|
|
|
if (_this->running) {
|
|
|
|
bladerf_set_bias_tee(_this->openDev, BLADERF_CHANNEL_RX(_this->chanId), _this->biasT);
|
|
|
|
}
|
|
|
|
config.acquire();
|
|
|
|
config.conf["devices"][_this->selectedSerial]["biasT"] = _this->biasT;
|
|
|
|
config.release(true);
|
|
|
|
}
|
|
|
|
}
|
2021-03-20 21:53:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void worker() {
|
2021-05-02 02:23:10 +02:00
|
|
|
int16_t* buffer = new int16_t[bufferSize * 2];
|
|
|
|
bladerf_metadata meta;
|
2021-12-19 22:11:44 +01:00
|
|
|
|
2021-06-27 02:20:11 +02:00
|
|
|
while (streamingEnabled) {
|
2021-05-03 04:20:48 +02:00
|
|
|
// Receive from the stream and break on error
|
2021-05-02 02:23:10 +02:00
|
|
|
int ret = bladerf_sync_rx(openDev, buffer, bufferSize, &meta, 3500);
|
2021-05-03 04:20:48 +02:00
|
|
|
if (ret != 0) { break; }
|
|
|
|
|
|
|
|
// Convert to complex float and swap buffers
|
2021-05-03 02:08:49 +02:00
|
|
|
volk_16i_s32f_convert_32f((float*)stream.writeBuf, buffer, 32768.0f, bufferSize * 2);
|
2021-05-02 02:23:10 +02:00
|
|
|
if (!stream.swap(bufferSize)) { break; }
|
2021-03-20 21:53:44 +01:00
|
|
|
}
|
2021-05-03 04:20:48 +02:00
|
|
|
|
2021-05-02 02:23:10 +02:00
|
|
|
delete[] buffer;
|
2021-03-20 21:53:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
bladerf* openDev;
|
|
|
|
bool enabled = true;
|
|
|
|
dsp::stream<dsp::complex_t> stream;
|
|
|
|
double sampleRate;
|
|
|
|
SourceManager::SourceHandler handler;
|
|
|
|
bool running = false;
|
|
|
|
double freq;
|
|
|
|
int devId = 0;
|
|
|
|
int srId = 0;
|
2021-05-03 18:10:52 +02:00
|
|
|
int bwId = 0;
|
2021-05-02 02:23:10 +02:00
|
|
|
int chanId = 0;
|
2021-06-27 02:20:11 +02:00
|
|
|
int gainMode = 0;
|
|
|
|
bool streamingEnabled = false;
|
2021-10-12 19:45:42 +02:00
|
|
|
bool biasT = false;
|
2021-03-20 21:53:44 +01:00
|
|
|
|
2021-05-02 02:23:10 +02:00
|
|
|
int channelCount;
|
|
|
|
|
2021-05-03 02:08:49 +02:00
|
|
|
const bladerf_range* srRange = NULL;
|
|
|
|
const bladerf_range* bwRange = NULL;
|
|
|
|
const bladerf_range* gainRange = NULL;
|
2021-05-02 02:23:10 +02:00
|
|
|
|
2021-05-03 18:10:52 +02:00
|
|
|
std::vector<uint64_t> sampleRates;
|
|
|
|
std::string sampleRatesTxt;
|
|
|
|
std::vector<uint64_t> bandwidths;
|
|
|
|
std::string bandwidthsTxt;
|
|
|
|
|
2021-06-26 18:26:58 +02:00
|
|
|
std::string channelNamesTxt;
|
|
|
|
|
2021-05-02 02:23:10 +02:00
|
|
|
int bufferSize;
|
2021-03-20 21:53:44 +01:00
|
|
|
struct bladerf_stream* rxStream;
|
|
|
|
|
2021-06-27 02:20:11 +02:00
|
|
|
int overallGain = 0;
|
2021-05-02 02:23:10 +02:00
|
|
|
|
2021-03-20 21:53:44 +01:00
|
|
|
std::thread workerThread;
|
|
|
|
|
|
|
|
int devCount = 0;
|
|
|
|
bladerf_devinfo* devInfoList = NULL;
|
|
|
|
std::string devListTxt;
|
2021-06-26 18:26:58 +02:00
|
|
|
|
|
|
|
std::string selectedSerial;
|
2021-06-27 02:20:11 +02:00
|
|
|
|
2021-10-12 19:45:42 +02:00
|
|
|
BladeRFType selectedBladeType = BLADERF_TYPE_UNKNOWN;
|
2021-06-27 02:20:11 +02:00
|
|
|
|
|
|
|
const bladerf_gain_modes* gainModes;
|
|
|
|
std::vector<std::string> gainModeNames;
|
|
|
|
std::string gainModesTxt;
|
|
|
|
int gainModeCount;
|
2021-03-20 21:53:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
MOD_EXPORT void _INIT_() {
|
|
|
|
json def = json({});
|
|
|
|
def["devices"] = json({});
|
|
|
|
def["device"] = "";
|
2022-02-24 20:49:53 +01:00
|
|
|
config.setPath(core::args["root"].s() + "/bladerf_config.json");
|
2021-03-20 21:53:44 +01:00
|
|
|
config.load(def);
|
|
|
|
config.enableAutoSave();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT ModuleManager::Instance* _CREATE_INSTANCE_(std::string name) {
|
|
|
|
return new BladeRFSourceModule(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT void _DELETE_INSTANCE_(ModuleManager::Instance* instance) {
|
|
|
|
delete (BladeRFSourceModule*)instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT void _END_() {
|
|
|
|
config.disableAutoSave();
|
|
|
|
config.save();
|
2021-10-13 00:22:10 +02:00
|
|
|
}
|