mirror of
https://github.com/AlexandreRouma/SDRPlusPlus.git
synced 2025-06-25 20:07:51 +02:00
Finished soapy module + added file source + added RTL_TCP source (windows only rn)
This commit is contained in:
20
rtl_tcp_source/CMakeLists.txt
Normal file
20
rtl_tcp_source/CMakeLists.txt
Normal file
@ -0,0 +1,20 @@
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
project(rtl_tcp_source)
|
||||
|
||||
if (MSVC)
|
||||
set(CMAKE_CXX_FLAGS "-O2 /std:c++17")
|
||||
else()
|
||||
set(CMAKE_CXX_FLAGS "-O3 -std=c++17 -fpermissive")
|
||||
endif (MSVC)
|
||||
|
||||
include_directories("src/")
|
||||
|
||||
file(GLOB SRC "src/*.cpp")
|
||||
|
||||
add_library(rtl_tcp_source SHARED ${SRC})
|
||||
target_link_libraries(rtl_tcp_source PRIVATE sdrpp_core)
|
||||
set_target_properties(rtl_tcp_source PROPERTIES PREFIX "")
|
||||
|
||||
if(WIN32)
|
||||
target_link_libraries(rtl_tcp_source PRIVATE wsock32 ws2_32)
|
||||
endif()
|
188
rtl_tcp_source/src/main.cpp
Normal file
188
rtl_tcp_source/src/main.cpp
Normal file
@ -0,0 +1,188 @@
|
||||
#include <rtltcp_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>
|
||||
|
||||
#define CONCAT(a, b) ((std::string(a) + b).c_str())
|
||||
|
||||
MOD_INFO {
|
||||
/* Name: */ "fike_source",
|
||||
/* Description: */ "File input module for SDR++",
|
||||
/* Author: */ "Ryzerth",
|
||||
/* Version: */ "0.1.0"
|
||||
};
|
||||
|
||||
class RTLTCPSourceModule {
|
||||
public:
|
||||
RTLTCPSourceModule(std::string name) {
|
||||
this->name = name;
|
||||
|
||||
stream.init(100);
|
||||
|
||||
sampleRate = 2560000.0;
|
||||
|
||||
handler.ctx = this;
|
||||
handler.selectHandler = menuSelected;
|
||||
handler.deselectHandler = menuDeselected;
|
||||
handler.menuHandler = menuHandler;
|
||||
handler.startHandler = start;
|
||||
handler.stopHandler = stop;
|
||||
handler.tuneHandler = tune;
|
||||
handler.stream = &stream;
|
||||
sigpath::sourceManager.registerSource("RTL-TCP", &handler);
|
||||
|
||||
spdlog::info("RTLTCPSourceModule '{0}': Instance created!", name);
|
||||
}
|
||||
|
||||
~RTLTCPSourceModule() {
|
||||
spdlog::info("RTLTCPSourceModule '{0}': Instance deleted!", name);
|
||||
}
|
||||
|
||||
private:
|
||||
static void menuSelected(void* ctx) {
|
||||
RTLTCPSourceModule* _this = (RTLTCPSourceModule*)ctx;
|
||||
core::setInputSampleRate(_this->sampleRate);
|
||||
spdlog::info("RTLTCPSourceModule '{0}': Menu Select!", _this->name);
|
||||
}
|
||||
|
||||
static void menuDeselected(void* ctx) {
|
||||
RTLTCPSourceModule* _this = (RTLTCPSourceModule*)ctx;
|
||||
spdlog::info("RTLTCPSourceModule '{0}': Menu Deselect!", _this->name);
|
||||
}
|
||||
|
||||
static void start(void* ctx) {
|
||||
RTLTCPSourceModule* _this = (RTLTCPSourceModule*)ctx;
|
||||
if (!_this->client.connectToRTL(_this->ip, _this->port)) {
|
||||
spdlog::error("Could not connect to {0}:{1}", _this->ip, _this->port);
|
||||
return;
|
||||
}
|
||||
_this->client.setFrequency(_this->freq);
|
||||
_this->client.setSampleRate(_this->sampleRate);
|
||||
_this->client.setGainIndex(_this->gain);
|
||||
_this->client.setGainMode(!_this->tunerAGC);
|
||||
_this->client.setDirectSampling(_this->directSamplingMode);
|
||||
_this->client.setAGCMode(_this->rtlAGC);
|
||||
_this->running = true;
|
||||
_this->workerThread = std::thread(worker, _this);
|
||||
spdlog::info("RTLTCPSourceModule '{0}': Start!", _this->name);
|
||||
}
|
||||
|
||||
static void stop(void* ctx) {
|
||||
RTLTCPSourceModule* _this = (RTLTCPSourceModule*)ctx;
|
||||
_this->running = false;
|
||||
_this->stream.stopWriter();
|
||||
_this->workerThread.join();
|
||||
_this->stream.clearWriteStop();
|
||||
_this->client.disconnect();
|
||||
spdlog::info("RTLTCPSourceModule '{0}': Stop!", _this->name);
|
||||
}
|
||||
|
||||
static void tune(double freq, void* ctx) {
|
||||
RTLTCPSourceModule* _this = (RTLTCPSourceModule*)ctx;
|
||||
if (_this->running) {
|
||||
_this->client.setFrequency(freq);
|
||||
}
|
||||
_this->freq = freq;
|
||||
spdlog::info("RTLTCPSourceModule '{0}': Tune: {1}!", _this->name, freq);
|
||||
}
|
||||
|
||||
static void menuHandler(void* ctx) {
|
||||
RTLTCPSourceModule* _this = (RTLTCPSourceModule*)ctx;
|
||||
float menuWidth = ImGui::GetContentRegionAvailWidth();
|
||||
float portWidth = ImGui::CalcTextSize("00000").x + 20;
|
||||
|
||||
ImGui::SetNextItemWidth(menuWidth - portWidth);
|
||||
ImGui::InputText(CONCAT("##_ip_select_", _this->name), _this->ip, 1024);
|
||||
ImGui::SameLine();
|
||||
ImGui::SetNextItemWidth(portWidth);
|
||||
ImGui::InputInt(CONCAT("##_port_select_", _this->name), &_this->port, 0);
|
||||
|
||||
ImGui::SetNextItemWidth(ImGui::CalcTextSize("OOOOOOOOOO").x);
|
||||
if (ImGui::Combo("Direct sampling", &_this->directSamplingMode, "Disabled\0I branch\0Q branch\0")) {
|
||||
if (_this->running) {
|
||||
_this->client.setDirectSampling(_this->directSamplingMode);
|
||||
}
|
||||
}
|
||||
|
||||
if (ImGui::Checkbox("RTL AGC", &_this->rtlAGC)) {
|
||||
if (_this->running) {
|
||||
_this->client.setAGCMode(_this->rtlAGC);
|
||||
}
|
||||
}
|
||||
|
||||
if (ImGui::Checkbox("Tuner AGC", &_this->tunerAGC)) {
|
||||
if (_this->running) {
|
||||
_this->client.setGainMode(!_this->tunerAGC);
|
||||
if (!_this->tunerAGC) {
|
||||
_this->client.setGainIndex(_this->gain);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (_this->tunerAGC) { style::beginDisabled(); }
|
||||
ImGui::SetNextItemWidth(menuWidth);
|
||||
if (ImGui::SliderInt(CONCAT("##_gain_select_", _this->name), &_this->gain, 0, 29, "")) {
|
||||
if (_this->running) {
|
||||
_this->client.setGainIndex(_this->gain);
|
||||
}
|
||||
}
|
||||
if (_this->tunerAGC) { style::endDisabled(); }
|
||||
}
|
||||
|
||||
static void worker(void* ctx) {
|
||||
RTLTCPSourceModule* _this = (RTLTCPSourceModule*)ctx;
|
||||
int blockSize = _this->sampleRate / 200.0;
|
||||
uint8_t* inBuf = new uint8_t[blockSize * 2];
|
||||
dsp::complex_t* outBuf = new dsp::complex_t[blockSize];
|
||||
|
||||
_this->stream.setMaxLatency(blockSize * 2);
|
||||
|
||||
while (true) {
|
||||
// Read samples here
|
||||
_this->client.receiveData(inBuf, blockSize * 2);
|
||||
for (int i = 0; i < blockSize; i++) {
|
||||
outBuf[i].q = ((double)inBuf[i * 2] - 128.0) / 128.0;
|
||||
outBuf[i].i = ((double)inBuf[(i * 2) + 1] - 128.0) / 128.0;
|
||||
}
|
||||
if (_this->stream.write(outBuf, blockSize) < 0) { break; };
|
||||
}
|
||||
|
||||
delete[] inBuf;
|
||||
delete[] outBuf;
|
||||
}
|
||||
|
||||
std::string name;
|
||||
dsp::stream<dsp::complex_t> stream;
|
||||
double sampleRate;
|
||||
SourceManager::SourceHandler handler;
|
||||
std::thread workerThread;
|
||||
RTLTCPClient client;
|
||||
bool running = false;
|
||||
double freq;
|
||||
char ip[1024] = "localhost";
|
||||
int port = 1234;
|
||||
int gain = 0;
|
||||
bool rtlAGC = false;
|
||||
bool tunerAGC = false;
|
||||
int directSamplingMode = 0;
|
||||
};
|
||||
|
||||
MOD_EXPORT void _INIT_() {
|
||||
// Do your one time init here
|
||||
}
|
||||
|
||||
MOD_EXPORT void* _CREATE_INSTANCE_(std::string name) {
|
||||
return new RTLTCPSourceModule(name);
|
||||
}
|
||||
|
||||
MOD_EXPORT void _DELETE_INSTANCE_(void* instance) {
|
||||
delete (RTLTCPSourceModule*)instance;
|
||||
}
|
||||
|
||||
MOD_EXPORT void _STOP_() {
|
||||
// Do your one shutdown here
|
||||
}
|
120
rtl_tcp_source/src/rtltcp_client.h
Normal file
120
rtl_tcp_source/src/rtltcp_client.h
Normal file
@ -0,0 +1,120 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
#include <WinSock2.h>
|
||||
#include <WS2tcpip.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define __attribute__(x)
|
||||
#pragma pack(push, 1)
|
||||
#endif
|
||||
struct command_t{
|
||||
unsigned char cmd;
|
||||
unsigned int param;
|
||||
}__attribute__((packed));
|
||||
#ifdef _WIN32
|
||||
#pragma pack(pop)
|
||||
#endif
|
||||
|
||||
class RTLTCPClient {
|
||||
public:
|
||||
RTLTCPClient() {
|
||||
|
||||
}
|
||||
|
||||
bool connectToRTL(char* host, uint16_t port) {
|
||||
struct addrinfo *result = NULL;
|
||||
struct addrinfo *ptr = NULL;
|
||||
struct addrinfo hints;
|
||||
|
||||
ZeroMemory( &hints, sizeof(hints) );
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_TCP;
|
||||
|
||||
char buf[128];
|
||||
sprintf(buf, "%hu", port);
|
||||
|
||||
int iResult = getaddrinfo(host, buf, &hints, &result);
|
||||
if (iResult != 0) {
|
||||
// TODO: log error
|
||||
WSACleanup();
|
||||
return false;
|
||||
}
|
||||
ptr = result;
|
||||
|
||||
sock = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
|
||||
|
||||
if (sock == INVALID_SOCKET) {
|
||||
// TODO: log error
|
||||
freeaddrinfo(result);
|
||||
WSACleanup();
|
||||
return false;
|
||||
}
|
||||
|
||||
iResult = connect(sock, ptr->ai_addr, (int)ptr->ai_addrlen);
|
||||
if (iResult == SOCKET_ERROR) {
|
||||
closesocket(sock);
|
||||
freeaddrinfo(result);
|
||||
WSACleanup();
|
||||
return false;
|
||||
}
|
||||
freeaddrinfo(result);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void disconnect() {
|
||||
closesocket(sock);
|
||||
WSACleanup();
|
||||
}
|
||||
|
||||
// struct command_t {
|
||||
// uint8_t cmd;
|
||||
// uint32_t arg;
|
||||
// };
|
||||
|
||||
void sendCommand(uint8_t command, uint32_t param) {
|
||||
command_t cmd;
|
||||
cmd.cmd = command;
|
||||
cmd.param = htonl(param);
|
||||
send(sock, (char*)&cmd, sizeof(command_t), 0);
|
||||
}
|
||||
|
||||
void receiveData(uint8_t* buf, size_t count) {
|
||||
recv(sock, (char*)buf, count, 0);
|
||||
}
|
||||
|
||||
void setFrequency(double freq) {
|
||||
sendCommand(1, freq);
|
||||
}
|
||||
|
||||
void setSampleRate(double sr) {
|
||||
sendCommand(2, sr);
|
||||
}
|
||||
|
||||
void setGainMode(int mode) {
|
||||
sendCommand(3, mode);
|
||||
}
|
||||
|
||||
void setGain(double gain) {
|
||||
sendCommand(4, gain);
|
||||
}
|
||||
|
||||
void setAGCMode(int mode) {
|
||||
sendCommand(8, mode);
|
||||
}
|
||||
|
||||
void setDirectSampling(int mode) {
|
||||
sendCommand(9, mode);
|
||||
}
|
||||
|
||||
void setGainIndex(int index) {
|
||||
sendCommand(13, index);
|
||||
}
|
||||
|
||||
private:
|
||||
SOCKET sock;
|
||||
|
||||
};
|
Reference in New Issue
Block a user