changes to the build system

This commit is contained in:
Ryzerth
2020-12-22 14:50:26 +01:00
parent e90b6656c3
commit bd545feb2c
57 changed files with 1456 additions and 275 deletions

View File

@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.13)
project(spyserver_source)
if (MSVC)
set(CMAKE_CXX_FLAGS "-O2 /std:c++17 /EHsc")
else()
set(CMAKE_CXX_FLAGS "-O3 -std=c++17 -fpermissive")
endif (MSVC)
include_directories("src/")
file(GLOB SRC "src/*.cpp")
add_library(spyserver_source SHARED ${SRC})
target_link_libraries(spyserver_source PRIVATE sdrpp_core)
set_target_properties(spyserver_source PROPERTIES PREFIX "")
if(WIN32)
target_link_libraries(spyserver_source PRIVATE wsock32 ws2_32)
endif()

View File

@ -0,0 +1,178 @@
#include <spyserver_client.h>
#include <imgui.h>
#include <spdlog/spdlog.h>
#include <new_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())
SDRPP_MOD_INFO {
/* Name: */ "spyserver_source",
/* Description: */ "SpyServer source module for SDR++",
/* Author: */ "Ryzerth",
/* Version: */ 0, 1, 0,
/* Max instances */ 1
};
class SpyServerSourceModule : public ModuleManager::Instance {
public:
SpyServerSourceModule(std::string name) {
this->name = name;
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 = &client.iqStream;
sigpath::sourceManager.registerSource("SpyServer", &handler);
}
~SpyServerSourceModule() {
}
void enable() {
enabled = true;
}
void disable() {
enabled = false;
}
bool isEnabled() {
return enabled;
}
private:
static void menuSelected(void* ctx) {
SpyServerSourceModule* _this = (SpyServerSourceModule*)ctx;
core::setInputSampleRate(_this->sampleRate);
spdlog::info("SpyServerSourceModule '{0}': Menu Select!", _this->name);
}
static void menuDeselected(void* ctx) {
SpyServerSourceModule* _this = (SpyServerSourceModule*)ctx;
spdlog::info("SpyServerSourceModule '{0}': Menu Deselect!", _this->name);
}
static void start(void* ctx) {
SpyServerSourceModule* _this = (SpyServerSourceModule*)ctx;
if (_this->running) {
return;
}
if (!_this->client.connectToSpyserver(_this->ip, _this->port)) {
spdlog::error("Could not connect to {0}:{1}", _this->ip, _this->port);
return;
}
_this->client.tune(_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;
spdlog::info("SpyServerSourceModule '{0}': Start!", _this->name);
}
static void stop(void* ctx) {
SpyServerSourceModule* _this = (SpyServerSourceModule*)ctx;
if (!_this->running) {
return;
}
_this->running = false;
_this->client.disconnect();
spdlog::info("SpyServerSourceModule '{0}': Stop!", _this->name);
}
static void tune(double freq, void* ctx) {
SpyServerSourceModule* _this = (SpyServerSourceModule*)ctx;
if (_this->running) {
_this->client.tune(freq);
}
_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();
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(); }
}
std::string name;
bool enabled = true;
dsp::stream<dsp::complex_t> stream;
double sampleRate;
SourceManager::SourceHandler handler;
std::thread workerThread;
SpyServerClient client;
bool running = false;
double freq;
char ip[1024] = "localhost";
int port = 5555;
int gain = 0;
bool rtlAGC = false;
bool tunerAGC = false;
int directSamplingMode = 0;
};
MOD_EXPORT void _INIT_() {
// Do your one time init here
}
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_() {
// Do your one shutdown here
}

View File

@ -0,0 +1,254 @@
#include <spyserver_client.h>
#include <spdlog/spdlog.h>
SpyServerClient::SpyServerClient() {
}
bool SpyServerClient::connectToSpyserver(char* host, int port) {
if (connected) {
return true;
}
#ifdef _WIN32
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
printf("A");
WSACleanup();
return false;
}
ptr = result;
sock = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (sock == INVALID_SOCKET) {
// TODO: log error
printf("B");
freeaddrinfo(result);
WSACleanup();
return false;
}
iResult = connect(sock, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("C");
closesocket(sock);
freeaddrinfo(result);
WSACleanup();
return false;
}
freeaddrinfo(result);
#else
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
// TODO: Log error
return false;
}
struct hostent *server = gethostbyname(host);
struct sockaddr_in serv_addr;
bzero(&serv_addr, sizeof(struct sockaddr_in));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(port);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) {
// TODO: log error
return false;
}
#endif
// Switch to non-blocking mode
#ifdef _WIN32
unsigned long mode = 1;
ioctlsocket(sock, FIONBIO, &mode);
#else
fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL, 0) | O_NONBLOCK)
#endif
connected = true;
waiting = true;
workerThread = std::thread(&SpyServerClient::worker, this);
printf("Connected");
return true;
}
bool SpyServerClient::disconnect() {
if (!connected) {
return false;
}
waiting = false;
if (workerThread.joinable()) {
workerThread.join();
}
#ifdef _WIN32
closesocket(sock);
WSACleanup();
#else
close(sockfd);
#endif
connected = false;
return true;
}
void SpyServerClient::setSampleRate(uint32_t setSampleRate) {
}
void SpyServerClient::tune(uint32_t freq) {
}
int SpyServerClient::receive(char* buf, int count) {
#ifdef _WIN32
return checkError(recv(sock, (char*)buf, count, 0), count);
#else
return checkError(read(sockfd, buf, count));
#endif
}
int SpyServerClient::checkError(int len, int expected) {
#ifdef _WIN32
if (len != SOCKET_ERROR) { return len; }
int code = WSAGetLastError();
if (code == WSAEWOULDBLOCK) { return 0; }
spdlog::error("{0}", code);
return -1;
#else
if (len <= expected) {
return len;
}
if (len == EAGAIN || len == EWOULDBLOCK) { return 0; }
return -1;
#endif
}
int SpyServerClient::receiveSync(char* buf, int count) {
int len = receive(buf, count);
while (len == 0 && waiting) {
len = receive(buf, count);
}
if (!waiting) {
return 0;
}
return len;
}
void SpyServerClient::worker() {
// Allocate dummy buffer
char* dummyBuf = (char*)malloc(1000000);
// Send hello
hello();
// SETTING_STREAMING_MODE = 0,
// SETTING_STREAMING_ENABLED = 1,
// SETTING_GAIN = 2,
// SETTING_IQ_FORMAT = 100, // 0x64
// SETTING_IQ_FREQUENCY = 101, // 0x65
// SETTING_IQ_DECIMATION = 102, // 0x66
// SETTING_IQ_DIGITAL_GAIN = 103, // 0x67
// Set settings
setSetting(SETTING_STREAMING_MODE, STREAM_MODE_IQ_ONLY);
setSetting(SETTING_GAIN, 5);
setSetting(SETTING_IQ_FORMAT, STREAM_FORMAT_FLOAT);
setSetting(SETTING_IQ_FREQUENCY, 2000000);
setSetting(SETTING_IQ_DECIMATION, 1);
setSetting(SETTING_IQ_DIGITAL_GAIN, 1);
// Enable stream
setSetting(SETTING_STREAMING_ENABLED, 1);
// Main message receive loop
while (true) {
MessageHeader msgHeader;
int len = receiveSync((char*)&msgHeader, sizeof(MessageHeader));
if (len < 0) {
spdlog::error("Socket error");
return;
}
if (len == 0) { return; }
int type = (msgHeader.MessageType & 0xFFFF);
if (type == MSG_TYPE_DEVICE_INFO) {
DeviceInfo devInfo;
len = receiveSync((char*)&devInfo, sizeof(DeviceInfo));
if (len < 0) {
spdlog::error("A Socket error");
return;
}
if (len == 0) { return; }
spdlog::warn("Dev type: {0}", devInfo.DeviceType);
}
// else if (type == MSG_TYPE_FLOAT_IQ) {
// //if (iqStream.aquire() < 0) { return; }
// len = receiveSync(dummyBuf, msgHeader.BodySize);
// //iqStream.write(msgHeader.BodySize);
// if (len < 0) {
// spdlog::error("T Socket error");
// return;
// }
// if (len == 0) { return; }
// }
else if (msgHeader.BodySize > 0) {
len = receiveSync(dummyBuf, msgHeader.BodySize);
if (len < 0) {
spdlog::error("B Socket error {0}", msgHeader.ProtocolID);
return;
}
if (len == 0) { return; }
}
}
free(dummyBuf);
}
void SpyServerClient::sendCommand(uint32_t cmd, void* body, size_t bodySize) {
int size = sizeof(CommandHeader) + bodySize;
char* buf = new char[size];
CommandHeader* cmdHdr = (CommandHeader*)buf;
memcpy(&buf[sizeof(CommandHeader)], body, bodySize);
cmdHdr->CommandType = cmd;
cmdHdr->BodySize = bodySize;
#ifdef _WIN32
send(sock, buf, size, 0);
#else
write(sockfd, buf, size);
#endif
delete[] buf;
}
void SpyServerClient::setSetting(uint32_t setting, uint32_t value) {
char buf[sizeof(SettingTarget) + sizeof(uint32_t)];
SettingTarget* tgt = (SettingTarget*)buf;
uint32_t* val = (uint32_t*)&buf[sizeof(SettingTarget)];
tgt->SettingType = setting;
*val = value;
sendCommand(CMD_SET_SETTING, buf, sizeof(SettingTarget) + sizeof(uint32_t));
}
void SpyServerClient::hello() {
char buf[1024];
ClientHandshake* handshake = (ClientHandshake*)buf;
handshake->ProtocolVersion = SPYSERVER_PROTOCOL_VERSION;
strcpy(&buf[sizeof(ClientHandshake)], "sdr++");
sendCommand(CMD_HELLO, buf, sizeof(ClientHandshake) + 5);
}

View File

@ -0,0 +1,69 @@
#pragma once
#include <spyserver_protocol.h>
#include <dsp/stream.h>
#include <dsp/types.h>
#include <thread>
#include <string>
#ifdef _WIN32
#include <WinSock2.h>
#include <WS2tcpip.h>
#else
#include <unistd.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#endif
#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 SpyServerClient {
public:
SpyServerClient();
bool connectToSpyserver(char* host, int port);
bool disconnect();
void start();
void stop();
void setSampleRate(uint32_t setSampleRate);
void tune(uint32_t freq);
dsp::stream<dsp::complex_t> iqStream;
private:
int receive(char* buf, int count);
int receiveSync(char* buf, int count);
int checkError(int err, int expected);
void worker();
void sendCommand(uint32_t cmd, void* body, size_t bodySize);
void setSetting(uint32_t setting, uint32_t value);
void hello();
#ifdef _WIN32
SOCKET sock;
#else
int sockfd;
#endif
bool connected = false;
bool waiting = false;
std::thread workerThread;
};

View File

@ -0,0 +1,188 @@
/*
SPY Server protocol structures and constants
Copyright (C) 2017 Youssef Touil youssef@live.com
*/
#pragma once
#include <stdint.h>
#include <limits.h>
#define SPYSERVER_PROTOCOL_VERSION (((2) << 24) | ((0) << 16) | (1700))
#define SPYSERVER_MAX_COMMAND_BODY_SIZE (256)
#define SPYSERVER_MAX_MESSAGE_BODY_SIZE (1 << 20)
#define SPYSERVER_MAX_DISPLAY_PIXELS (1 << 15)
#define SPYSERVER_MIN_DISPLAY_PIXELS (100)
#define SPYSERVER_MAX_FFT_DB_RANGE (150)
#define SPYSERVER_MIN_FFT_DB_RANGE (10)
#define SPYSERVER_MAX_FFT_DB_OFFSET (100)
enum DeviceType
{
DEVICE_INVALID = 0,
DEVICE_AIRSPY_ONE = 1,
DEVICE_AIRSPY_HF = 2,
DEVICE_RTLSDR = 3,
};
enum CommandType
{
CMD_HELLO = 0,
CMD_GET_SETTING = 1,
CMD_SET_SETTING = 2,
CMD_PING = 3,
};
enum SettingType
{
SETTING_STREAMING_MODE = 0,
SETTING_STREAMING_ENABLED = 1,
SETTING_GAIN = 2,
SETTING_IQ_FORMAT = 100, // 0x64
SETTING_IQ_FREQUENCY = 101, // 0x65
SETTING_IQ_DECIMATION = 102, // 0x66
SETTING_IQ_DIGITAL_GAIN = 103, // 0x67
SETTING_FFT_FORMAT = 200, // 0xc8
SETTING_FFT_FREQUENCY = 201, // 0xc9
SETTING_FFT_DECIMATION = 202, // 0xca
SETTING_FFT_DB_OFFSET = 203, // 0xcb
SETTING_FFT_DB_RANGE = 204, // 0xcc
SETTING_FFT_DISPLAY_PIXELS = 205, // 0xcd
};
enum StreamType
{
STREAM_TYPE_STATUS = 0,
STREAM_TYPE_IQ = 1,
STREAM_TYPE_AF = 2,
STREAM_TYPE_FFT = 4,
};
enum StreamingMode
{
STREAM_MODE_IQ_ONLY = STREAM_TYPE_IQ, // 0x01
STREAM_MODE_AF_ONLY = STREAM_TYPE_AF, // 0x02
STREAM_MODE_FFT_ONLY = STREAM_TYPE_FFT, // 0x04
STREAM_MODE_FFT_IQ = STREAM_TYPE_FFT | STREAM_TYPE_IQ, // 0x05
STREAM_MODE_FFT_AF = STREAM_TYPE_FFT | STREAM_TYPE_AF, // 0x06
};
enum StreamFormat
{
STREAM_FORMAT_INVALID = 0,
STREAM_FORMAT_UINT8 = 1,
STREAM_FORMAT_INT16 = 2,
STREAM_FORMAT_INT24 = 3,
STREAM_FORMAT_FLOAT = 4,
STREAM_FORMAT_DINT4 = 5,
};
enum MessageType
{
MSG_TYPE_DEVICE_INFO = 0,
MSG_TYPE_CLIENT_SYNC = 1,
MSG_TYPE_PONG = 2,
MSG_TYPE_READ_SETTING = 3,
MSG_TYPE_UINT8_IQ = 100, // 0x64
MSG_TYPE_INT16_IQ = 101, // 0x65
MSG_TYPE_INT24_IQ = 102, // 0x66
MSG_TYPE_FLOAT_IQ = 103, // 0x67
MSG_TYPE_UINT8_AF = 200, // 0xc8
MSG_TYPE_INT16_AF = 201, // 0xc9
MSG_TYPE_INT24_AF = 202, // 0xca
MSG_TYPE_FLOAT_AF = 203, // 0xcb
MSG_TYPE_DINT4_FFT = 300, //0x12C
MSG_TYPE_UINT8_FFT = 301, //0x12D
};
#ifdef _WIN32
#define __attribute__(x)
#pragma pack(push, 1)
#endif
struct ClientHandshake
{
uint32_t ProtocolVersion;
// SDR# doesn't seem to send this
//uint32_t ClientNameLength;
}__attribute__((packed));
struct CommandHeader
{
uint32_t CommandType;
uint32_t BodySize;
}__attribute__((packed));
struct SettingTarget
{
// Again, not used
//uint32_t StreamType;
uint32_t SettingType;
}__attribute__((packed));
struct MessageHeader
{
uint32_t ProtocolID;
uint32_t MessageType;
uint32_t StreamType;
uint32_t SequenceNumber;
uint32_t BodySize;
}__attribute__((packed));
struct DeviceInfo
{
uint32_t DeviceType;
uint32_t DeviceSerial;
uint32_t MaximumSampleRate;
uint32_t MaximumBandwidth;
uint32_t DecimationStageCount;
uint32_t GainStageCount;
uint32_t MaximumGainIndex;
uint32_t MinimumFrequency;
uint32_t MaximumFrequency;
uint32_t Resolution;
uint32_t MinimumIQDecimation;
uint32_t ForcedIQFormat;
}__attribute__((packed));
struct ClientSync
{
uint32_t CanControl;
uint32_t Gain;
uint32_t DeviceCenterFrequency;
uint32_t IQCenterFrequency;
uint32_t FFTCenterFrequency;
uint32_t MinimumIQCenterFrequency;
uint32_t MaximumIQCenterFrequency;
uint32_t MinimumFFTCenterFrequency;
uint32_t MaximumFFTCenterFrequency;
}__attribute__((packed));
struct ComplexInt16
{
int16_t real;
int16_t imag;
}__attribute__((packed));
struct ComplexUInt8
{
uint8_t real;
uint8_t imag;
}__attribute__((packed));
#ifdef _WIN32
#pragma pack(pop)
#endif
enum ParserPhase {
AcquiringHeader,
ReadingData
};