SDR++ server beta :)

This commit is contained in:
AlexandreRouma
2022-01-21 20:22:13 +01:00
parent 1185e4e114
commit 74b9d13360
37 changed files with 2731 additions and 498 deletions

View File

@ -0,0 +1,279 @@
#include "sdrpp_server_client.h"
#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 <options.h>
#include <gui/widgets/stepped_slider.h>
#include <utils/optionlist.h>
#define CONCAT(a, b) ((std::string(a) + b).c_str())
SDRPP_MOD_INFO{
/* Name: */ "sdrpp_server_source",
/* Description: */ "SDR++ Server source module for SDR++",
/* Author: */ "Ryzerth",
/* Version: */ 0, 1, 0,
/* Max instances */ 1
};
ConfigManager config;
class SDRPPServerSourceModule : public ModuleManager::Instance {
public:
SDRPPServerSourceModule(std::string name) {
this->name = name;
// Yeah no server-ception, sorry...
if (options::opts.serverMode) { return; }
// Initialize lists
sampleTypeList.define("Int8", dsp::PCM_TYPE_I8);
sampleTypeList.define("Int16", dsp::PCM_TYPE_I16);
sampleTypeList.define("Float32", dsp::PCM_TYPE_F32);
sampleTypeId = sampleTypeList.valueId(dsp::PCM_TYPE_I16);
handler.ctx = this;
handler.selectHandler = menuSelected;
handler.deselectHandler = menuDeselected;
handler.menuHandler = menuHandler;
handler.startHandler = start;
handler.stopHandler = stop;
handler.tuneHandler = tune;
handler.stream = &stream;
// Load config
config.acquire();
std::string hostStr = config.conf["hostname"];
strcpy(hostname, hostStr.c_str());
port = config.conf["port"];
config.release();
sigpath::sourceManager.registerSource("SDR++ Server", &handler);
}
~SDRPPServerSourceModule() {
stop(this);
sigpath::sourceManager.unregisterSource("SDR++ Server");
}
void postInit() {}
void enable() {
enabled = true;
}
void disable() {
enabled = false;
}
bool isEnabled() {
return enabled;
}
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) {
SDRPPServerSourceModule* _this = (SDRPPServerSourceModule*)ctx;
if (_this->client) {
core::setInputSampleRate(_this->client->getSampleRate());
}
gui::mainWindow.playButtonLocked = !(_this->client && _this->client->isOpen());
spdlog::info("SDRPPServerSourceModule '{0}': Menu Select!", _this->name);
}
static void menuDeselected(void* ctx) {
SDRPPServerSourceModule* _this = (SDRPPServerSourceModule*)ctx;
gui::mainWindow.playButtonLocked = false;
spdlog::info("SDRPPServerSourceModule '{0}': Menu Deselect!", _this->name);
}
static void start(void* ctx) {
SDRPPServerSourceModule* _this = (SDRPPServerSourceModule*)ctx;
if (_this->running) { return; }
// TODO: Set configuration here
if (_this->client) { _this->client->start(); }
_this->running = true;
spdlog::info("SDRPPServerSourceModule '{0}': Start!", _this->name);
}
static void stop(void* ctx) {
SDRPPServerSourceModule* _this = (SDRPPServerSourceModule*)ctx;
if (!_this->running) { return; }
if (_this->client) { _this->client->stop(); }
_this->running = false;
spdlog::info("SDRPPServerSourceModule '{0}': Stop!", _this->name);
}
static void tune(double freq, void* ctx) {
SDRPPServerSourceModule* _this = (SDRPPServerSourceModule*)ctx;
if (_this->running && _this->client) {
_this->client->setFrequency(freq);
}
_this->freq = freq;
spdlog::info("SDRPPServerSourceModule '{0}': Tune: {1}!", _this->name, freq);
}
static void menuHandler(void* ctx) {
SDRPPServerSourceModule* _this = (SDRPPServerSourceModule*)ctx;
float menuWidth = ImGui::GetContentRegionAvailWidth();
bool connected = (_this->client && _this->client->isOpen());
gui::mainWindow.playButtonLocked = !connected;
if (connected) { style::beginDisabled(); }
if (ImGui::InputText(CONCAT("##sdrpp_srv_srv_host_", _this->name), _this->hostname, 1023)) {
config.acquire();
config.conf["hostname"] = _this->hostname;
config.release(true);
}
ImGui::SameLine();
ImGui::SetNextItemWidth(menuWidth - ImGui::GetCursorPosX());
if (ImGui::InputInt(CONCAT("##sdrpp_srv_srv_port_", _this->name), &_this->port, 0, 0)) {
config.acquire();
config.conf["port"] = _this->port;
config.release(true);
}
if (connected) { style::endDisabled(); }
if (_this->running) { style::beginDisabled(); }
if (!connected && ImGui::Button("Connect##sdrpp_srv_source", ImVec2(menuWidth, 0))) {
try {
if (_this->client) { _this->client.reset(); }
_this->client = server::connect(_this->hostname, _this->port, &_this->stream);
_this->deviceInit();
}
catch (std::exception e) {
spdlog::error("Could not connect to SDR: {0}", e.what());
}
}
else if (connected && ImGui::Button("Disconnect##sdrpp_srv_source", ImVec2(menuWidth, 0))) {
_this->client->close();
}
if (_this->running) { style::endDisabled(); }
if (connected) {
ImGui::LeftLabel("Sample type");
ImGui::FillWidth();
if (ImGui::Combo("##sdrpp_srv_source_samp_type", &_this->sampleTypeId, _this->sampleTypeList.txt)) {
_this->client->setSampleType(_this->sampleTypeList[_this->sampleTypeId]);
// Save config
config.acquire();
config.conf["servers"][_this->devConfName]["sampleType"] = _this->sampleTypeList.key(_this->sampleTypeId);
config.release(true);
}
bool dummy = false;
style::beginDisabled();
ImGui::Checkbox("Compression", &dummy);
dummy = true;
ImGui::Checkbox("Full IQ", &dummy);
style::endDisabled();
// Calculate datarate
_this->frametimeCounter += ImGui::GetIO().DeltaTime;
if (_this->frametimeCounter >= 0.2f) {
_this->datarate = ((float)_this->client->bytes / (_this->frametimeCounter * 1024.0f * 1024.0f)) * 8;
_this->frametimeCounter = 0;
_this->client->bytes = 0;
}
ImGui::Text("Status:");
ImGui::SameLine();
ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "Connected (%.3f Mbit/s)", _this->datarate);
ImGui::CollapsingHeader("Source [REMOTE]", ImGuiTreeNodeFlags_DefaultOpen);
_this->client->showMenu();
}
else {
ImGui::Text("Status:");
ImGui::SameLine();
ImGui::Text("Not connected (--.--- Mbit/s)");
}
}
void deviceInit() {
// Generate the config name
char buf[4096];
sprintf(buf, "%s:%05d", hostname, port);
devConfName = buf;
// Load settings
sampleTypeId = sampleTypeList.valueId(dsp::PCM_TYPE_I16);
if (config.conf["servers"][devConfName].contains("sampleType")) {
std::string key = config.conf["servers"][devConfName]["sampleType"];
if (sampleTypeList.keyExists(key)) { sampleTypeId = sampleTypeList.keyId(key); }
}
// Set settings
client->setSampleType(sampleTypeList[sampleTypeId]);
}
std::string name;
bool enabled = true;
bool running = false;
double freq;
float datarate = 0;
float frametimeCounter = 0;
char hostname[1024];
int port = 50000;
std::string devConfName = "";
dsp::stream<dsp::complex_t> stream;
SourceManager::SourceHandler handler;
OptionList<std::string, dsp::PCMType> sampleTypeList;
int sampleTypeId;
server::Client client;
};
MOD_EXPORT void _INIT_() {
json def = json({});
def["hostname"] = "localhost";
def["port"] = 5259;
def["servers"] = json::object();
config.setPath(options::opts.root + "/sdrpp_server_source_config.json");
config.load(def);
config.enableAutoSave();
}
MOD_EXPORT ModuleManager::Instance* _CREATE_INSTANCE_(std::string name) {
return new SDRPPServerSourceModule(name);
}
MOD_EXPORT void _DELETE_INSTANCE_(ModuleManager::Instance* instance) {
delete (SDRPPServerSourceModule*)instance;
}
MOD_EXPORT void _END_() {
config.disableAutoSave();
config.save();
}

View File

@ -0,0 +1,233 @@
#include "sdrpp_server_client.h"
#include <volk/volk.h>
#include <cstring>
#include <spdlog/spdlog.h>
#include <core.h>
using namespace std::chrono_literals;
namespace server {
ClientClass::ClientClass(net::Conn conn, dsp::stream<dsp::complex_t>* out) {
client = std::move(conn);
output = out;
// Allocate buffers
rbuffer = new uint8_t[SERVER_MAX_PACKET_SIZE];
sbuffer = new uint8_t[SERVER_MAX_PACKET_SIZE];
// Initialize headers
r_pkt_hdr = (PacketHeader*)rbuffer;
r_pkt_data = &rbuffer[sizeof(PacketHeader)];
r_cmd_hdr = (CommandHeader*)r_pkt_data;
r_cmd_data = &rbuffer[sizeof(PacketHeader) + sizeof(CommandHeader)];
s_pkt_hdr = (PacketHeader*)sbuffer;
s_pkt_data = &sbuffer[sizeof(PacketHeader)];
s_cmd_hdr = (CommandHeader*)s_pkt_data;
s_cmd_data = &sbuffer[sizeof(PacketHeader) + sizeof(CommandHeader)];
// Initialize DSP
decomp.init(&decompIn);
link.init(&decomp.out, output);
decomp.start();
link.start();
// Start readers
client->readAsync(sizeof(PacketHeader), rbuffer, tcpHandler, this);
// Default configuration
stop();
// Ask for a UI
getUI();
}
ClientClass::~ClientClass() {
close();
delete[] rbuffer;
delete[] sbuffer;
}
void ClientClass::showMenu() {
std::string diffId = "";
SmGui::DrawListElem diffValue;
bool syncRequired = false;
{
std::lock_guard<std::mutex> lck(dlMtx);
dl.draw(diffId, diffValue, syncRequired);
}
if (!diffId.empty()) {
// Save ID
SmGui::DrawListElem elemId;
elemId.type = SmGui::DRAW_LIST_ELEM_TYPE_STRING;
elemId.str = diffId;
// Encore packet
int size = 0;
s_cmd_data[size++] = syncRequired;
size += SmGui::DrawList::storeItem(elemId, &s_cmd_data[size], SERVER_MAX_PACKET_SIZE - size);
size += SmGui::DrawList::storeItem(diffValue, &s_cmd_data[size], SERVER_MAX_PACKET_SIZE - size);
// Send
if (syncRequired) {
spdlog::warn("Action requires resync");
auto waiter = awaitCommandAck(COMMAND_UI_ACTION);
sendCommand(COMMAND_UI_ACTION, size);
if (waiter->await(PROTOCOL_TIMEOUT_MS)) {
std::lock_guard lck(dlMtx);
dl.load(r_cmd_data, r_pkt_hdr->size - sizeof(PacketHeader) - sizeof(CommandHeader));
}
else {
spdlog::error("Timeout out after asking for UI");
}
waiter->handled();
spdlog::warn("Resync done");
}
else {
spdlog::warn("Action does not require resync");
sendCommand(COMMAND_UI_ACTION, size);
}
}
}
void ClientClass::setFrequency(double freq) {
if (!client || !client->isOpen()) { return; }
*(double*)s_cmd_data = freq;
sendCommand(COMMAND_SET_FREQUENCY, sizeof(double));
auto waiter = awaitCommandAck(COMMAND_SET_FREQUENCY);
waiter->await(PROTOCOL_TIMEOUT_MS);
waiter->handled();
}
double ClientClass::getSampleRate() {
return currentSampleRate;
}
void ClientClass::setSampleType(dsp::PCMType type) {
s_cmd_data[0] = type;
sendCommand(COMMAND_SET_SAMPLE_TYPE, 1);
}
void ClientClass::start() {
if (!client || !client->isOpen()) { return; }
sendCommand(COMMAND_START, 0);
getUI();
}
void ClientClass::stop() {
if (!client || !client->isOpen()) { return; }
sendCommand(COMMAND_STOP, 0);
getUI();
}
void ClientClass::close() {
decomp.stop();
link.stop();
client->close();
}
bool ClientClass::isOpen() {
return client->isOpen();
}
void ClientClass::tcpHandler(int count, uint8_t* buf, void* ctx) {
ClientClass* _this = (ClientClass*)ctx;
// Read the rest of the data (TODO: CHECK SIZE OR SHIT WILL BE FUCKED)
int len = 0;
int read = 0;
int goal = _this->r_pkt_hdr->size - sizeof(PacketHeader);
while (len < goal) {
read = _this->client->read(goal - len, &buf[sizeof(PacketHeader) + len]);
if (read < 0) {
return;
};
len += read;
}
_this->bytes += _this->r_pkt_hdr->size;
if (_this->r_pkt_hdr->type == PACKET_TYPE_COMMAND) {
// TODO: Move to command handler
if (_this->r_cmd_hdr->cmd == COMMAND_SET_SAMPLERATE && _this->r_pkt_hdr->size == sizeof(PacketHeader) + sizeof(CommandHeader) + sizeof(double)) {
_this->currentSampleRate = *(double*)_this->r_cmd_data;
core::setInputSampleRate(_this->currentSampleRate);
}
}
else if (_this->r_pkt_hdr->type == PACKET_TYPE_COMMAND_ACK) {
// Notify waiters
std::vector<PacketWaiter*> toBeRemoved;
for (auto& [waiter, cmd] : _this->commandAckWaiters) {
if (cmd != _this->r_cmd_hdr->cmd) { continue; }
waiter->notify();
toBeRemoved.push_back(waiter);
}
// Remove handled waiters
for (auto& waiter : toBeRemoved) {
_this->commandAckWaiters.erase(waiter);
delete waiter;
}
}
else if (_this->r_pkt_hdr->type == PACKET_TYPE_BASEBAND) {
memcpy(_this->decompIn.writeBuf, &buf[sizeof(PacketHeader)], _this->r_pkt_hdr->size - sizeof(PacketHeader));
_this->decompIn.swap(_this->r_pkt_hdr->size - sizeof(PacketHeader));
}
else if (_this->r_pkt_hdr->type == PACKET_TYPE_ERROR) {
spdlog::error("SDR++ Server Error: {0}", buf[sizeof(PacketHeader)]);
}
else {
spdlog::error("Invalid packet type: {0}", _this->r_pkt_hdr->type);
}
// Restart an async read
_this->client->readAsync(sizeof(PacketHeader), _this->rbuffer, tcpHandler, _this);
}
void ClientClass::getUI() {
auto waiter = awaitCommandAck(COMMAND_GET_UI);
sendCommand(COMMAND_GET_UI, 0);
if (waiter->await(PROTOCOL_TIMEOUT_MS)) {
std::lock_guard lck(dlMtx);
dl.load(r_cmd_data, r_pkt_hdr->size - sizeof(PacketHeader) - sizeof(CommandHeader));
}
else {
spdlog::error("Timeout out after asking for UI");
}
waiter->handled();
}
void ClientClass::sendPacket(PacketType type, int len) {
s_pkt_hdr->type = type;
s_pkt_hdr->size = sizeof(PacketHeader) + len;
client->write(s_pkt_hdr->size, sbuffer);
}
void ClientClass::sendCommand(Command cmd, int len) {
s_cmd_hdr->cmd = cmd;
sendPacket(PACKET_TYPE_COMMAND, sizeof(CommandHeader) + len);
}
void ClientClass::sendCommandAck(Command cmd, int len) {
s_cmd_hdr->cmd = cmd;
sendPacket(PACKET_TYPE_COMMAND_ACK, sizeof(CommandHeader) + len);
}
PacketWaiter* ClientClass::awaitCommandAck(Command cmd) {
PacketWaiter* waiter = new PacketWaiter;
commandAckWaiters[waiter] = cmd;
return waiter;
}
void ClientClass::dHandler(dsp::complex_t *data, int count, void *ctx) {
ClientClass* _this = (ClientClass*)ctx;
memcpy(_this->output->writeBuf, data, count * sizeof(dsp::complex_t));
_this->output->swap(count);
}
Client connect(std::string host, uint16_t port, dsp::stream<dsp::complex_t>* out) {
net::Conn conn = net::connect(host, port);
if (!conn) { return NULL; }
return Client(new ClientClass(std::move(conn), out));
}
}

View File

@ -0,0 +1,134 @@
#pragma once
#include <utils/networking.h>
#include <dsp/stream.h>
#include <dsp/types.h>
#include <atomic>
#include <queue>
#include <server_protocol.h>
#include <atomic>
#include <map>
#include <vector>
#include <dsp/compression.h>
#include <dsp/sink.h>
#include <dsp/link.h>
#define RFSPACE_MAX_SIZE 8192
#define RFSPACE_HEARTBEAT_INTERVAL_MS 1000
#define RFSPACE_TIMEOUT_MS 3000
#define PROTOCOL_TIMEOUT_MS 10000
namespace server {
class PacketWaiter {
public:
bool await(int timeout) {
std::unique_lock lck(readyMtx);
return readyCnd.wait_for(lck, std::chrono::milliseconds(timeout), [=](){ return dataReady; });
}
void handled() {
{
std::lock_guard lck(handledMtx);
dataHandled = true;
}
handledCnd.notify_all();
}
void notify() {
// Tell waiter that data is ready
{
std::lock_guard lck(readyMtx);
dataReady = true;
}
readyCnd.notify_all();
// Wait for waiter to handle the request
{
std::unique_lock lck(readyMtx);
handledCnd.wait(lck, [=](){ return dataHandled; });
}
}
void reset() {
std::lock_guard lck1(readyMtx);
std::lock_guard lck2(handledMtx);
dataReady = false;
dataHandled = false;
}
private:
bool dataReady = false;
bool dataHandled = false;
std::condition_variable readyCnd;
std::condition_variable handledCnd;
std::mutex readyMtx;
std::mutex handledMtx;
};
class ClientClass {
public:
ClientClass(net::Conn conn, dsp::stream<dsp::complex_t>* out);
~ClientClass();
void showMenu();
void setFrequency(double freq);
double getSampleRate();
void setSampleType(dsp::PCMType type);
void start();
void stop();
void close();
bool isOpen();
int bytes = 0;
private:
static void tcpHandler(int count, uint8_t* buf, void* ctx);
void getUI();
void sendPacket(PacketType type, int len);
void sendCommand(Command cmd, int len);
void sendCommandAck(Command cmd, int len);
PacketWaiter* awaitCommandAck(Command cmd);
void commandAckHandled(PacketWaiter* waiter);
std::map<PacketWaiter*, Command> commandAckWaiters;
static void dHandler(dsp::complex_t *data, int count, void *ctx);
net::Conn client;
dsp::stream<uint8_t> decompIn;
dsp::DynamicRangeDecompressor decomp;
dsp::Link<dsp::complex_t> link;
dsp::stream<dsp::complex_t>* output;
uint8_t* rbuffer = NULL;
uint8_t* sbuffer = NULL;
PacketHeader* r_pkt_hdr = NULL;
uint8_t* r_pkt_data = NULL;
CommandHeader* r_cmd_hdr = NULL;
uint8_t* r_cmd_data = NULL;
PacketHeader* s_pkt_hdr = NULL;
uint8_t* s_pkt_data = NULL;
CommandHeader* s_cmd_hdr = NULL;
uint8_t* s_cmd_data = NULL;
SmGui::DrawList dl;
std::mutex dlMtx;
double currentSampleRate = 1000000.0;
};
typedef std::unique_ptr<ClientClass> Client;
Client connect(std::string host, uint16_t port, dsp::stream<dsp::complex_t>* out);
}