Skip to content

Commit

Permalink
Switch spectran http source module to use the remoteconfig endpoint a…
Browse files Browse the repository at this point in the history
…s per #1354
  • Loading branch information
AlexandreRouma committed Apr 4, 2024
1 parent 065a5b4 commit 12f7efe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
10 changes: 7 additions & 3 deletions source_modules/spectran_http_source/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ class SpectranHTTPSourceModule : public ModuleManager::Instance {
_this->tryConnect();
}
else if (connected && SmGui::Button("Disconnect##spectran_http_source")) {
_this->client->onCenterFrequencyChanged.unbind(_this->onFreqChangedId);
_this->client->onCenterFrequencyChanged.unbind(_this->onSamplerateChangedId);
_this->client->close();
_this->disconnect();
}
if (_this->running) { style::endDisabled(); }

Expand All @@ -173,6 +171,12 @@ class SpectranHTTPSourceModule : public ModuleManager::Instance {
}
}

void disconnect() {
client->onCenterFrequencyChanged.unbind(onFreqChangedId);
client->onSamplerateChanged.unbind(onSamplerateChangedId);
client->close();
}

void onFreqChanged(double newFreq) {
if (lastReportedFreq == newFreq) { return; }
lastReportedFreq = newFreq;
Expand Down
22 changes: 17 additions & 5 deletions source_modules/spectran_http_source/src/spectran_http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ SpectranHTTPClient::SpectranHTTPClient(std::string host, int port, dsp::stream<d
sock = net::connect(host, port);
http = net::http::Client(sock);

// Make request
// Send sttream request
net::http::RequestHeader rqhdr(net::http::METHOD_GET, "/stream?format=float32", host);
http.sendRequestHeader(rqhdr);

// Receive for response
net::http::ResponseHeader rshdr;
http.recvResponseHeader(rshdr, 5000);

// Check the status
if (rshdr.getStatusCode() != net::http::STATUS_CODE_OK) {
flog::error("HTTP request did not return ok: {}", rshdr.getStatusString());
throw std::runtime_error("HTTP request did not return ok");
Expand Down Expand Up @@ -48,20 +51,29 @@ void SpectranHTTPClient::setCenterFrequency(uint64_t freq) {
auto controlSock = net::connect(host, port);
auto controlHttp = net::http::Client(controlSock);

// Make request
net::http::RequestHeader rqhdr(net::http::METHOD_PUT, "/control", host);
// Encode request body
net::http::RequestHeader rqhdr(net::http::METHOD_PUT, "/remoteconfig", host);
char buf[1024];
sprintf(buf, "{\"frequencyCenter\":%" PRIu64 ",\"frequencySpan\":%" PRIu64 ",\"type\":\"capture\"}", freq, _samplerate);
sprintf(buf, "{\"receiverName\": \"Block_IQDemodulator_0\", \"simpleconfig\": {\"main\": {\"centerfreq\": %" PRIu64 ", \"samplerate\": %" PRIu64 ", \"spanfreq\": %" PRIu64 "}}}", freq, _samplerate, _samplerate);
std::string data = buf;
char lenBuf[16];
sprintf(lenBuf, "%" PRIu64, (uint64_t)data.size());

// Setup request headers
rqhdr.setField("Content-Length", lenBuf);

// Send request
controlHttp.sendRequestHeader(rqhdr);
controlSock->sendstr(data);

// Receive response
net::http::ResponseHeader rshdr;
controlHttp.recvResponseHeader(rshdr, 5000);

flog::debug("Response: {}", rshdr.getStatusString());
// Log error if there is one
if (rshdr.getStatusCode() < 200 || rshdr.getStatusCode() >= 300) {
flog::debug("Response: {}", rshdr.getStatusString());
}
}

void SpectranHTTPClient::worker() {
Expand Down

0 comments on commit 12f7efe

Please sign in to comment.