From 07419275ffc58df75047682972d02e403d0a2dd4 Mon Sep 17 00:00:00 2001 From: AlexandreRouma Date: Tue, 9 Nov 2021 00:00:13 +0100 Subject: [PATCH] Work on RFspace support --- CMakeLists.txt | 5 + core/src/utils/networking.cpp | 7 +- source_modules/rfspace_source/CMakeLists.txt | 25 ++ source_modules/rfspace_source/src/main.cpp | 222 ++++++++++++++++++ .../rfspace_source/src/rfspace_client.cpp | 132 +++++++++++ .../rfspace_source/src/rfspace_client.h | 130 ++++++++++ source_modules/spyserver_source/src/main.cpp | 4 +- 7 files changed, 520 insertions(+), 5 deletions(-) create mode 100644 source_modules/rfspace_source/CMakeLists.txt create mode 100644 source_modules/rfspace_source/src/main.cpp create mode 100644 source_modules/rfspace_source/src/rfspace_client.cpp create mode 100644 source_modules/rfspace_source/src/rfspace_client.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e32aae9..224dffa1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ option(OPT_BUILD_FILE_SOURCE "Wav file source" ON) option(OPT_BUILD_HACKRF_SOURCE "Build HackRF Source Module (Dependencies: libhackrf)" ON) option(OPT_BUILD_LIMESDR_SOURCE "Build LimeSDR Source Module (Dependencies: liblimesuite)" OFF) option(OPT_BUILD_SDDC_SOURCE "Build SDDC Source Module (Dependencies: libusb-1.0)" OFF) +option(OPT_BUILD_RFSPACE_SOURCE "Build RFspace Source Module no dependencies required)" ON) option(OPT_BUILD_RTL_SDR_SOURCE "Build RTL-SDR Source Module (Dependencies: librtlsdr)" ON) option(OPT_BUILD_RTL_TCP_SOURCE "Build RTL-TCP Source Module (no dependencies required)" ON) option(OPT_BUILD_SDRPLAY_SOURCE "Build SDRplay Source Module (Dependencies: libsdrplay)" OFF) @@ -79,6 +80,10 @@ if (OPT_BUILD_SDDC_SOURCE) add_subdirectory("source_modules/sddc_source") endif (OPT_BUILD_SDDC_SOURCE) +if (OPT_BUILD_RFSPACE_SOURCE) +add_subdirectory("source_modules/rfspace_source") +endif (OPT_BUILD_RFSPACE_SOURCE) + if (OPT_BUILD_RTL_SDR_SOURCE) add_subdirectory("source_modules/rtl_sdr_source") endif (OPT_BUILD_RTL_SDR_SOURCE) diff --git a/core/src/utils/networking.cpp b/core/src/utils/networking.cpp index f15cefa4..b86fb887 100644 --- a/core/src/utils/networking.cpp +++ b/core/src/utils/networking.cpp @@ -431,7 +431,7 @@ namespace net { #endif // Create a socket - sock = socket(AF_INET, SOCK_DGRAM, 0); + sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock < 0) { throw std::runtime_error("Could not create socket"); return NULL; @@ -455,7 +455,7 @@ namespace net { // Create host address struct sockaddr_in addr; - addr.sin_addr.s_addr = *naddr; + addr.sin_addr.s_addr = INADDR_ANY;//*naddr; addr.sin_family = AF_INET; addr.sin_port = htons(port); @@ -467,7 +467,8 @@ namespace net { // Bind socket if (bindSocket) { - if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { + int err = bind(sock, (struct sockaddr*)&addr, sizeof(addr)); + if (err < 0) { throw std::runtime_error("Could not bind socket"); return NULL; } diff --git a/source_modules/rfspace_source/CMakeLists.txt b/source_modules/rfspace_source/CMakeLists.txt new file mode 100644 index 00000000..30355891 --- /dev/null +++ b/source_modules/rfspace_source/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.13) +project(rfspace_source) + +file(GLOB SRC "src/*.cpp") + +add_library(rfspace_source SHARED ${SRC}) +target_link_libraries(rfspace_source PRIVATE sdrpp_core) +set_target_properties(rfspace_source PROPERTIES PREFIX "") + +target_include_directories(rfspace_source PRIVATE "src/") + +if (MSVC) + target_compile_options(rfspace_source PRIVATE /O2 /Ob2 /std:c++17 /EHsc) +elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + target_compile_options(rfspace_source PRIVATE -O3 -std=c++17 -Wno-unused-command-line-argument -undefined dynamic_lookup) +else () + target_compile_options(rfspace_source PRIVATE -O3 -std=c++17) +endif () + +if(WIN32) + target_link_libraries(rfspace_source PRIVATE wsock32 ws2_32) +endif() + +# Install directives +install(TARGETS rfspace_source DESTINATION lib/sdrpp/plugins) \ No newline at end of file diff --git a/source_modules/rfspace_source/src/main.cpp b/source_modules/rfspace_source/src/main.cpp new file mode 100644 index 00000000..7d7a97a4 --- /dev/null +++ b/source_modules/rfspace_source/src/main.cpp @@ -0,0 +1,222 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define CONCAT(a, b) ((std::string(a) + b).c_str()) + +SDRPP_MOD_INFO { + /* Name: */ "spyserver_source", + /* Description: */ "Airspy HF+ source module for SDR++", + /* Author: */ "Ryzerth", + /* Version: */ 0, 1, 0, + /* Max instances */ 1 +}; + +ConfigManager config; + +class SpyServerSourceModule : public ModuleManager::Instance { +public: + SpyServerSourceModule(std::string name) { + this->name = name; + + handler.ctx = this; + handler.selectHandler = menuSelected; + handler.deselectHandler = menuDeselected; + handler.menuHandler = menuHandler; + handler.startHandler = start; + handler.stopHandler = stop; + handler.tuneHandler = tune; + handler.stream = &stream; + + strcpy(hostname, "192.168.0.111"); + + sigpath::sourceManager.registerSource("RFspace", &handler); + } + + ~SpyServerSourceModule() { + stop(this); + sigpath::sourceManager.unregisterSource("RFspace"); + } + + 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) { + SpyServerSourceModule* _this = (SpyServerSourceModule*)ctx; + core::setInputSampleRate(_this->sampleRate); + gui::mainWindow.playButtonLocked = !(_this->client && _this->client->isOpen()); + spdlog::info("SpyServerSourceModule '{0}': Menu Select!", _this->name); + } + + static void menuDeselected(void* ctx) { + SpyServerSourceModule* _this = (SpyServerSourceModule*)ctx; + gui::mainWindow.playButtonLocked = false; + spdlog::info("SpyServerSourceModule '{0}': Menu Deselect!", _this->name); + } + + static void start(void* ctx) { + SpyServerSourceModule* _this = (SpyServerSourceModule*)ctx; + if (_this->running) { return; } + + // TODO: Start + + _this->running = true; + spdlog::info("SpyServerSourceModule '{0}': Start!", _this->name); + } + + static void stop(void* ctx) { + SpyServerSourceModule* _this = (SpyServerSourceModule*)ctx; + if (!_this->running) { return; } + + // TODO: Stop + + _this->running = false; + spdlog::info("SpyServerSourceModule '{0}': Stop!", _this->name); + } + + static void tune(double freq, void* ctx) { + SpyServerSourceModule* _this = (SpyServerSourceModule*)ctx; + if (_this->running) { + // TODO: Tune + } + _this->freq = freq; + spdlog::info("SpyServerSourceModule '{0}': Tune: {1}!", _this->name, freq); + } + + static void menuHandler(void* ctx) { + SpyServerSourceModule* _this = (SpyServerSourceModule*)ctx; + float menuWidth = ImGui::GetContentRegionAvailWidth(); + + bool connected = (_this->client && _this->client->isOpen()); + gui::mainWindow.playButtonLocked = !connected; + + if (connected) { style::beginDisabled(); } + if (ImGui::InputText(CONCAT("##_rfspace_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("##_rfspace_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##rfspace_source", ImVec2(menuWidth, 0))) { + try { + _this->client = rfspace::connect(_this->hostname, _this->port, &_this->stream); + } + catch (std::exception e) { + spdlog::error("Could not connect to SDR: {0}", e.what()); + } + } + else if (connected && ImGui::Button("Disconnect##rfspace_source", ImVec2(menuWidth, 0))) { + _this->client->close(); + } + if (_this->running) { style::endDisabled(); } + + + + if (connected) { + // TODO: Options here + + ImGui::Text("Status:"); + ImGui::SameLine(); + ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "Connected (DEV NAME HERE)"); + } + else { + ImGui::Text("Status:"); + ImGui::SameLine(); + ImGui::Text("Not connected"); + } + } + + std::string name; + bool enabled = true; + bool running = false; + double sampleRate = 1000000; + double freq; + + char hostname[1024]; + int port = 50000; + + int srId = 0; + std::vector sampleRates; + std::string sampleRatesTxt; + + dsp::stream stream; + SourceManager::SourceHandler handler; + + rfspace::RFspaceClient client; +}; + +MOD_EXPORT void _INIT_() { + json def = json({}); + def["hostname"] = "localhost"; + def["port"] = 50000; + def["devices"] = json::object(); + config.setPath(options::opts.root + "/rfspace_config.json"); + config.load(def); + config.enableAutoSave(); + + // Check config in case a user has a very old version + config.acquire(); + bool corrected = false; + if (!config.conf.contains("hostname") || !config.conf.contains("port") || !config.conf.contains("devices")) { + config.conf = def; + corrected = true; + } + config.release(corrected); +} + +MOD_EXPORT ModuleManager::Instance* _CREATE_INSTANCE_(std::string name) { + return new SpyServerSourceModule(name); +} + +MOD_EXPORT void _DELETE_INSTANCE_(ModuleManager::Instance* instance) { + delete (SpyServerSourceModule*)instance; +} + +MOD_EXPORT void _END_() { + config.disableAutoSave(); + config.save(); +} \ No newline at end of file diff --git a/source_modules/rfspace_source/src/rfspace_client.cpp b/source_modules/rfspace_source/src/rfspace_client.cpp new file mode 100644 index 00000000..84f32e63 --- /dev/null +++ b/source_modules/rfspace_source/src/rfspace_client.cpp @@ -0,0 +1,132 @@ +#include +#include +#include +#include + +using namespace std::chrono_literals; + +namespace rfspace { + RFspaceClientClass::RFspaceClientClass(net::Conn conn, net::Conn udpConn, dsp::stream* out) { + client = std::move(conn); + udpClient = std::move(udpConn); + output = out; + rbuffer = new uint8_t[RFSPACE_MAX_SIZE]; + sbuffer = new uint8_t[RFSPACE_MAX_SIZE]; + ubuffer = new uint8_t[RFSPACE_MAX_SIZE]; + + output->clearWriteStop(); + + uint8_t test = 0x5A; + udpClient->write(1, &test); + + // Start readers + client->readAsync(sizeof(tcpHeader), (uint8_t*)&tcpHeader, tcpHandler, this); + udpClient->readAsync(sizeof(udpHeader), (uint8_t*)&udpHeader, udpHandler, this); + + // Start SDR + uint8_t args[4] = {0x70, 2, 0, 0}; + setControlItem(0x18, args, 4); + } + + RFspaceClientClass::~RFspaceClientClass() { + close(); + delete[] rbuffer; + delete[] sbuffer; + delete[] ubuffer; + } + + int RFspaceClientClass::getControlItem(uint16_t item, void* param, int len) { + // Build packet + uint16_t* header = (uint16_t*)&sbuffer[0]; + uint16_t* item_val = (uint16_t*)&sbuffer[2]; + *header = 4 | (RFSPACE_MSG_TYPE_H2T_REQ_CTRL_ITEM << 13); + *item_val = item; + + // Send packet + SCIRRecv.release(); + client->write(4, sbuffer); + + // Wait for a response + if (!SCIRRecv.await(2000)) { + SCIRRecv.release(); + return -1; + } + + // Forward data + int toRead = ((SCIRSize - 4) < len) ? (SCIRSize - 4) : len; + memcpy(param, &rbuffer[4], toRead); + + // Release receiving thread + SCIRRecv.release(); + + return toRead; + } + + void RFspaceClientClass::setControlItem(uint16_t item, void* param, int len) { + // Build packet + uint16_t* header = (uint16_t*)&sbuffer[0]; + uint16_t* item_val = (uint16_t*)&sbuffer[2]; + *header = (len + 4) | (RFSPACE_MSG_TYPE_H2T_SET_CTRL_ITEM << 13); + *item_val = item; + memcpy(&sbuffer[4], param, len); + + // Send packet + client->write(len + 4, sbuffer); + } + + void RFspaceClientClass::close() { + output->stopWriter(); + client->close(); + udpClient->close(); + } + + bool RFspaceClientClass::isOpen() { + return client->isOpen(); + } + + void RFspaceClientClass::tcpHandler(int count, uint8_t* buf, void* ctx) { + RFspaceClientClass* _this = (RFspaceClientClass*)ctx; + uint8_t type = _this->tcpHeader >> 13; + uint16_t size = _this->tcpHeader & 0b1111111111111; + + // Read the rest of the data + if (size > 2) { + _this->client->read(size - 2, &_this->rbuffer[2]); + } + + spdlog::warn("TCP received: {0} {1}", type, size); + + // Process data + if (type == RFSPACE_MSG_TYPE_T2H_SET_CTRL_ITEM_RESP) { + _this->SCIRSize = size; + _this->SCIRRecv.trigger(); + } + + // Restart an async read + _this->client->readAsync(sizeof(_this->tcpHeader), (uint8_t*)&_this->tcpHeader, tcpHandler, _this); + } + + void RFspaceClientClass::udpHandler(int count, uint8_t* buf, void* ctx) { + RFspaceClientClass* _this = (RFspaceClientClass*)ctx; + uint8_t type = _this->udpHeader >> 13; + uint16_t size = _this->udpHeader & 0b1111111111111; + + spdlog::warn("UDP received: {0} {1}", type, size); + + // Read the rest of the data + if (size > 2) { + _this->client->read(size - 2, &_this->rbuffer[2]); + } + + // Restart an async read + _this->client->readAsync(sizeof(_this->udpHeader), (uint8_t*)&_this->udpHeader, udpHandler, _this); + } + + RFspaceClient connect(std::string host, uint16_t port, dsp::stream* out) { + net::Conn conn = net::connect(host, port); + if (!conn) { return NULL; } + net::Conn udpConn = net::openUDP("0.0.0.0", port, host, port, true); + if (!udpConn) { return NULL; } + return RFspaceClient(new RFspaceClientClass(std::move(conn), std::move(udpConn), out)); + } +} \ No newline at end of file diff --git a/source_modules/rfspace_source/src/rfspace_client.h b/source_modules/rfspace_source/src/rfspace_client.h new file mode 100644 index 00000000..55bbdef6 --- /dev/null +++ b/source_modules/rfspace_source/src/rfspace_client.h @@ -0,0 +1,130 @@ +#pragma once +#include +#include +#include +#include + +#define RFSPACE_MAX_SIZE 8192 + +namespace rfspace { + enum { + RFSPACE_MSG_TYPE_H2T_SET_CTRL_ITEM, + RFSPACE_MSG_TYPE_H2T_REQ_CTRL_ITEM, + RFSPACE_MSG_TYPE_H2T_REQ_CTRL_ITEM_RANGE, + RFSPACE_MSG_TYPE_H2T_DATA_ITEM_ACK_REQ, + RFSPACE_MSG_TYPE_H2T_DATA_ITEM_0, + RFSPACE_MSG_TYPE_H2T_DATA_ITEM_1, + RFSPACE_MSG_TYPE_H2T_DATA_ITEM_2, + RFSPACE_MSG_TYPE_H2T_DATA_ITEM_3, + }; + + enum { + RFSPACE_MSG_TYPE_T2H_SET_CTRL_ITEM_RESP, + RFSPACE_MSG_TYPE_T2H_UNSOL_CTRL_ITEM, + RFSPACE_MSG_TYPE_T2H_REQ_CTRL_ITEM_RANGE_RESP, + RFSPACE_MSG_TYPE_T2H_DATA_ITEM_ACK_REQ, + RFSPACE_MSG_TYPE_T2H_DATA_ITEM_0, + RFSPACE_MSG_TYPE_T2H_DATA_ITEM_1, + RFSPACE_MSG_TYPE_T2H_DATA_ITEM_2, + RFSPACE_MSG_TYPE_T2H_DATA_ITEM_3, + }; + + class SyncEvent { + public: + bool await(int timeoutMS) { + // Mark as waiting + { + std::lock_guard lck(waitingmtx); + waiting = true; + } + + // Wait for data + std::unique_lock lck(mtx); + return cnd.wait_for(lck, std::chrono::milliseconds(timeoutMS), [this](){ return triggered; }); + + // Mark as not waiting + { + std::lock_guard lck(waitingmtx); + waiting = false; + } + } + + void release() { + // Mark as not waiting, and if last notify sender + { + std::lock_guard lck(mtx); + triggered = false; + } + cnd.notify_all(); + } + + void trigger() { + // Check if a waiter is waiting + { + std::lock_guard lck(waitingmtx); + if (waiting <= 0) { return; } + } + + // Notify waiters + { + std::lock_guard lck(mtx); + triggered = true; + } + cnd.notify_all(); + + // Wait for waiter to handle + { + std::unique_lock lck(mtx); + cnd.wait(lck, [this](){ return !triggered; }); + } + } + + private: + bool triggered; + std::mutex mtx; + std::condition_variable cnd; + + bool waiting; + std::mutex waitingmtx; + + }; + + class RFspaceClientClass { + public: + RFspaceClientClass(net::Conn conn, net::Conn udpConn, dsp::stream* out); + ~RFspaceClientClass(); + + int getControlItem(uint16_t item, void* param, int len); + void setControlItem(uint16_t item, void* param, int len); + + void close(); + bool isOpen(); + + private: + void sendCommand(uint32_t command, void* data, int len); + void sendHandshake(std::string appName); + + static void tcpHandler(int count, uint8_t* buf, void* ctx); + static void udpHandler(int count, uint8_t* buf, void* ctx); + + net::Conn client; + net::Conn udpClient; + + dsp::stream* output; + + uint16_t tcpHeader; + uint16_t udpHeader; + + uint8_t* rbuffer = NULL; + uint8_t* sbuffer = NULL; + uint8_t* ubuffer = NULL; + + SyncEvent SCIRRecv; + int SCIRSize; + }; + + typedef std::unique_ptr RFspaceClient; + + RFspaceClient connect(std::string host, uint16_t port, dsp::stream* out); + +} diff --git a/source_modules/spyserver_source/src/main.cpp b/source_modules/spyserver_source/src/main.cpp index 61c3ed19..57591bab 100644 --- a/source_modules/spyserver_source/src/main.cpp +++ b/source_modules/spyserver_source/src/main.cpp @@ -161,14 +161,14 @@ private: gui::mainWindow.playButtonLocked = !connected; if (connected) { style::beginDisabled(); } - if (ImGui::InputText(CONCAT("##_rigctl_srv_host_", _this->name), _this->hostname, 1023)) { + if (ImGui::InputText(CONCAT("##_spyserver_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("##_rigctl_srv_port_", _this->name), &_this->port, 0, 0)) { + if (ImGui::InputInt(CONCAT("##_spyserver_srv_port_", _this->name), &_this->port, 0, 0)) { config.acquire(); config.conf["port"] = _this->port; config.release(true);