From e03e95cc54ec93a1948cd3c7e91b22a6de087357 Mon Sep 17 00:00:00 2001 From: AlexandreRouma Date: Sun, 10 Jul 2022 18:03:17 +0200 Subject: [PATCH] Fixed autostart for networked sources --- .../sdrpp_server_source/src/main.cpp | 28 +++-- source_modules/spyserver_source/src/main.cpp | 100 ++++++++++-------- 2 files changed, 74 insertions(+), 54 deletions(-) diff --git a/source_modules/sdrpp_server_source/src/main.cpp b/source_modules/sdrpp_server_source/src/main.cpp index 0eeb7e3e..3f1d5da6 100644 --- a/source_modules/sdrpp_server_source/src/main.cpp +++ b/source_modules/sdrpp_server_source/src/main.cpp @@ -109,6 +109,12 @@ private: SDRPPServerSourceModule* _this = (SDRPPServerSourceModule*)ctx; if (_this->running) { return; } + // Try to connect if not already connected + if (!_this->client) { + _this->tryConnect(); + if (!_this->client) { return; } + } + // TODO: Set configuration here if (_this->client) { _this->client->setFrequency(_this->freq); @@ -166,15 +172,7 @@ private: 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()); - if (!strcmp(e.what(), "Server busy")) { _this->serverBusy = true; } - } + _this->tryConnect(); } else if (connected && ImGui::Button("Disconnect##sdrpp_srv_source", ImVec2(menuWidth, 0))) { _this->client->close(); @@ -231,6 +229,18 @@ private: } } + void tryConnect() { + try { + if (client) { client.reset(); } + client = server::connect(hostname, port, &stream); + deviceInit(); + } + catch (std::exception e) { + spdlog::error("Could not connect to SDR: {0}", e.what()); + if (!strcmp(e.what(), "Server busy")) { serverBusy = true; } + } + } + void deviceInit() { // Generate the config name char buf[4096]; diff --git a/source_modules/spyserver_source/src/main.cpp b/source_modules/spyserver_source/src/main.cpp index 83cc03db..6f938804 100644 --- a/source_modules/spyserver_source/src/main.cpp +++ b/source_modules/spyserver_source/src/main.cpp @@ -120,6 +120,12 @@ private: static void start(void* ctx) { SpyServerSourceModule* _this = (SpyServerSourceModule*)ctx; if (_this->running) { return; } + + // Try to connect if not already connected + if (!_this->client) { + _this->tryConnect(); + if (!_this->client) { return; } + } int srvBits = streamFormatsBitCount[_this->iqType]; _this->client->setSetting(SPYSERVER_SETTING_IQ_FORMAT, streamFormats[_this->iqType]); @@ -178,51 +184,7 @@ private: SmGui::FillWidth(); SmGui::ForceSync(); if (!connected && SmGui::Button("Connect##spyserver_source")) { - try { - if (_this->client) { _this->client.reset(); } - _this->client = spyserver::connect(_this->hostname, _this->port, &_this->stream); - - if (!_this->client->waitForDevInfo(3000)) { - spdlog::error("SpyServer didn't respond with device information"); - } - else { - char buf[1024]; - sprintf(buf, "%s [%08X]", deviceTypesStr[_this->client->devInfo.DeviceType], _this->client->devInfo.DeviceSerial); - _this->devRef = std::string(buf); - - config.acquire(); - if (!config.conf["devices"].contains(_this->devRef)) { - config.conf["devices"][_this->devRef]["sampleRateId"] = 0; - config.conf["devices"][_this->devRef]["sampleBitDepthId"] = 1; - config.conf["devices"][_this->devRef]["gainId"] = 0; - } - _this->srId = config.conf["devices"][_this->devRef]["sampleRateId"]; - _this->iqType = config.conf["devices"][_this->devRef]["sampleBitDepthId"]; - _this->gain = config.conf["devices"][_this->devRef]["gainId"]; - config.release(true); - - _this->gain = std::clamp(_this->gain, 0, _this->client->devInfo.MaximumGainIndex); - - // Refresh sample rates - _this->sampleRates.clear(); - _this->sampleRatesTxt.clear(); - for (int i = _this->client->devInfo.MinimumIQDecimation; i <= _this->client->devInfo.DecimationStageCount; i++) { - double sr = (double)_this->client->devInfo.MaximumSampleRate / ((double)(1 << i)); - _this->sampleRates.push_back(sr); - _this->sampleRatesTxt += _this->getBandwdithScaled(sr); - _this->sampleRatesTxt += '\0'; - } - - _this->srId = std::clamp(_this->srId, 0, _this->sampleRates.size() - 1); - - _this->sampleRate = _this->sampleRates[_this->srId]; - core::setInputSampleRate(_this->sampleRate); - spdlog::info("Connected to server"); - } - } - catch (std::exception e) { - spdlog::error("Could not connect to spyserver {0}", e.what()); - } + _this->tryConnect(); } else if (connected && SmGui::Button("Disconnect##spyserver_source")) { _this->client->close(); @@ -278,6 +240,54 @@ private: } } + void tryConnect() { + try { + if (client) { client.reset(); } + client = spyserver::connect(hostname, port, &stream); + + if (!client->waitForDevInfo(3000)) { + spdlog::error("SpyServer didn't respond with device information"); + } + else { + char buf[1024]; + sprintf(buf, "%s [%08X]", deviceTypesStr[client->devInfo.DeviceType], client->devInfo.DeviceSerial); + devRef = std::string(buf); + + config.acquire(); + if (!config.conf["devices"].contains(devRef)) { + config.conf["devices"][devRef]["sampleRateId"] = 0; + config.conf["devices"][devRef]["sampleBitDepthId"] = 1; + config.conf["devices"][devRef]["gainId"] = 0; + } + srId = config.conf["devices"][devRef]["sampleRateId"]; + iqType = config.conf["devices"][devRef]["sampleBitDepthId"]; + gain = config.conf["devices"][devRef]["gainId"]; + config.release(true); + + gain = std::clamp(gain, 0, client->devInfo.MaximumGainIndex); + + // Refresh sample rates + sampleRates.clear(); + sampleRatesTxt.clear(); + for (int i = client->devInfo.MinimumIQDecimation; i <= client->devInfo.DecimationStageCount; i++) { + double sr = (double)client->devInfo.MaximumSampleRate / ((double)(1 << i)); + sampleRates.push_back(sr); + sampleRatesTxt += getBandwdithScaled(sr); + sampleRatesTxt += '\0'; + } + + srId = std::clamp(srId, 0, sampleRates.size() - 1); + + sampleRate = sampleRates[srId]; + core::setInputSampleRate(sampleRate); + spdlog::info("Connected to server"); + } + } + catch (std::exception e) { + spdlog::error("Could not connect to spyserver {0}", e.what()); + } + } + std::string name; bool enabled = true; bool running = false;