From 6087160593e74aff9147153c69ea23849fc8b921 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 27 Jul 2022 12:05:05 +0200 Subject: Add PrecisionWave DEXTER support --- src/output/Dexter.cpp | 301 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/output/Dexter.h | 101 +++++++++++++++++ 2 files changed, 402 insertions(+) create mode 100644 src/output/Dexter.cpp create mode 100644 src/output/Dexter.h (limited to 'src/output') diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp new file mode 100644 index 0000000..605c61a --- /dev/null +++ b/src/output/Dexter.cpp @@ -0,0 +1,301 @@ +/* + Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Her Majesty the + Queen in Right of Canada (Communications Research Center Canada) + + Copyright (C) 2022 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://opendigitalradio.org + +DESCRIPTION: + It is an output driver using libiio targeting the PrecisionWave DEXTER board. +*/ + +/* + This file is part of ODR-DabMod. + + ODR-DabMod is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + ODR-DabMod is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ODR-DabMod. If not, see . + */ + +#include "output/Dexter.h" + +#ifdef HAVE_DEXTER + +#include +#include +#include +#include + +#include "Log.h" +#include "Utils.h" + +using namespace std; + +namespace Output { + +static constexpr size_t TRANSMISSION_FRAME_LEN = (2656 + 76 * 2552) * 4; + +static string get_iio_error(int err) +{ + char dst[256]; + iio_strerror(err, dst, sizeof(dst)); + return string(dst); +} + +Dexter::Dexter(SDRDeviceConfig& config) : + SDRDevice(), + m_conf(config) +{ + etiLog.level(info) << "Dexter:Creating the device"; + + m_ctx = iio_create_local_context(); + if (!m_ctx) { + throw std::runtime_error("Dexter: Unable to create iio scan context"); + } + + m_dexter_dsp_tx = iio_context_find_device(m_ctx, "dexter_dsp_tx"); + if (!m_dexter_dsp_tx) { + throw std::runtime_error("Dexter: Unable to find dexter_dsp_tx iio device"); + } + + m_ad9957_tx0 = iio_context_find_device(m_ctx, "ad9957_tx0"); + if (!m_ad9957_tx0) { + throw std::runtime_error("Dexter: Unable to find ad9957_tx0 iio device"); + } + + int r; + + // TODO make DC offset configurable and add to RC + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "dc0", 0)) != 0) { + etiLog.level(warn) << "Failed to set dexter_dsp_tx.dc0 = false: " << get_iio_error(r); + } + + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "dc1", 0)) != 0) { + etiLog.level(warn) << "Failed to set dexter_dsp_tx.dc1 = false: " << get_iio_error(r); + } + + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", 0)) != 0) { + etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0: " << get_iio_error(r); + } + + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", m_conf.txgain)) != 0) { + etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0: " << get_iio_error(r); + } + + //iio_device_attr_read_longlong(const struct iio_device *dev, const char *attr, long long *val); + + if (m_conf.sampleRate != 2048000) { + throw std::runtime_error("Dexter: Only 2048000 samplerate supported"); + } + + tune(m_conf.lo_offset, m_conf.frequency); + // TODO m_conf.frequency = m_dexter_dsp_tx->getFrequency(SOAPY_SDR_TX, 0); + etiLog.level(info) << "Dexter:Actual frequency: " << + std::fixed << std::setprecision(3) << + m_conf.frequency / 1000.0 << " kHz."; + + // skip: Set bandwidth + + // skip: antenna + + // TODO: set H/W time + + // Prepare streams + constexpr int CHANNEL_INDEX = 0; + m_tx_channel = iio_device_get_channel(m_ad9957_tx0, CHANNEL_INDEX); + if (m_tx_channel == nullptr) { + throw std::runtime_error("Dexter: Cannot create IIO channel."); + } + + iio_channel_enable(m_tx_channel); + + m_buffer = iio_device_create_buffer(m_ad9957_tx0, TRANSMISSION_FRAME_LEN/sizeof(int16_t), 0); + if (!m_buffer) { + throw std::runtime_error("Dexter: Cannot create IIO buffer."); + } +} + +Dexter::~Dexter() +{ + if (m_ctx) { + if (m_dexter_dsp_tx) { + iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", 0); + } + + if (m_buffer) { + iio_buffer_destroy(m_buffer); + } + + iio_context_destroy(m_ctx); + m_ctx = nullptr; + } +} + +void Dexter::tune(double lo_offset, double frequency) +{ + // TODO lo_offset + long long freq = m_conf.frequency - 204800000; + int r = 0; + + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "frequency0", freq)) != 0) { + etiLog.level(warn) << "Failed to set dexter_dsp_tx.frequency0 = " << freq << " : " << get_iio_error(r); + } +} + +double Dexter::get_tx_freq(void) const +{ + long long frequency = 0; + int r = 0; + + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "frequency0", &frequency)) != 0) { + etiLog.level(warn) << "Failed to read dexter_dsp_tx.frequency0 = " << + frequency << " : " << get_iio_error(r); + return 0; + } + else { + return frequency + 204800000; + } +} + +void Dexter::set_txgain(double txgain) +{ + int r = 0; + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", txgain)) != 0) { + etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0: " << get_iio_error(r); + } + + long long txgain_readback = 0; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "gain0", &txgain_readback)) != 0) { + etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0: " << get_iio_error(r); + } + else { + m_conf.txgain = txgain_readback; + } +} + +double Dexter::get_txgain(void) const +{ + long long txgain_readback = 0; + int r = 0; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "gain0", &txgain_readback)) != 0) { + etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0: " << get_iio_error(r); + } + return txgain_readback; +} + +void Dexter::set_bandwidth(double bandwidth) +{ + // TODO +} + +double Dexter::get_bandwidth(void) const +{ + return 0; +} + +SDRDevice::RunStatistics Dexter::get_run_statistics(void) const +{ + RunStatistics rs; + rs.num_underruns = underflows; + rs.num_overruns = overflows; + rs.num_late_packets = late_packets; + rs.num_frames_modulated = num_frames_modulated; + return rs; +} + + +double Dexter::get_real_secs(void) const +{ + // TODO + return 0; +} + +void Dexter::set_rxgain(double rxgain) +{ + // TODO +} + +double Dexter::get_rxgain(void) const +{ + // TODO + return 0; +} + +size_t Dexter::receive_frame( + complexf *buf, + size_t num_samples, + struct frame_timestamp& ts, + double timeout_secs) +{ + // TODO + return 0; +} + + +bool Dexter::is_clk_source_ok() const +{ + // TODO + return true; +} + +const char* Dexter::device_name(void) const +{ + return "Dexter"; +} + +double Dexter::get_temperature(void) const +{ + // TODO + // XADC contains temperature, but value is weird + return std::numeric_limits::quiet_NaN(); +} + +void Dexter::transmit_frame(const struct FrameData& frame) +{ + long long int timeNs = frame.ts.get_ns(); + const bool has_time_spec = (m_conf.enableSync and frame.ts.timestamp_valid); + + if (frame.buf.size() != TRANSMISSION_FRAME_LEN) { + etiLog.level(debug) << "Dexter::transmit_frame Expected " << + TRANSMISSION_FRAME_LEN << " got " << frame.buf.size(); + throw std::runtime_error("Dexter: invalid buffer size"); + } + + // DabMod::launch_modulator ensures we get int16_t IQ here + //const size_t num_samples = frame.buf.size() / (2*sizeof(int16_t)); + //const int16_t *buf = reinterpret_cast(frame.buf.data()); + + memcpy(iio_buffer_start(m_buffer), frame.buf.data(), frame.buf.size()); + ssize_t pushed = iio_buffer_push(m_buffer); + if (pushed < 0) { + etiLog.level(error) << "Dexter: failed to push buffer " << get_iio_error(pushed); + } + + num_frames_modulated++; + // TODO overflows, late_packets + + long long attr_value = 0; + int r = 0; + + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "buffer_underflows0", &attr_value)) == 0) { + fprintf(stderr, "buffer_underflows0 %lld\n", attr_value); + underflows = attr_value; + } +} + +} // namespace Output + +#endif // HAVE_DEXTER + + diff --git a/src/output/Dexter.h b/src/output/Dexter.h new file mode 100644 index 0000000..7a7f6c1 --- /dev/null +++ b/src/output/Dexter.h @@ -0,0 +1,101 @@ +/* + Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Her Majesty the + Queen in Right of Canada (Communications Research Center Canada) + + Copyright (C) 2022 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://opendigitalradio.org + +DESCRIPTION: + It is an output driver using libiio targeting the PrecisionWave DEXTER board. +*/ + +/* + This file is part of ODR-DabMod. + + ODR-DabMod is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + ODR-DabMod is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ODR-DabMod. If not, see . + */ + +#pragma once + +#ifdef HAVE_CONFIG_H +# include +#endif + +#ifdef HAVE_DEXTER +#include "iio.h" + +#include +#include + +#include "output/SDR.h" +#include "ModPlugin.h" +#include "EtiReader.h" +#include "RemoteControl.h" + +namespace Output { + +class Dexter : public Output::SDRDevice +{ + public: + Dexter(SDRDeviceConfig& config); + Dexter(const Dexter& other) = delete; + Dexter& operator=(const Dexter& other) = delete; + ~Dexter(); + + virtual void tune(double lo_offset, double frequency) override; + virtual double get_tx_freq(void) const override; + virtual void set_txgain(double txgain) override; + virtual double get_txgain(void) const override; + virtual void set_bandwidth(double bandwidth) override; + virtual double get_bandwidth(void) const override; + virtual void transmit_frame(const struct FrameData& frame) override; + virtual RunStatistics get_run_statistics(void) const override; + virtual double get_real_secs(void) const override; + + virtual void set_rxgain(double rxgain) override; + virtual double get_rxgain(void) const override; + virtual size_t receive_frame( + complexf *buf, + size_t num_samples, + struct frame_timestamp& ts, + double timeout_secs) override; + + // Return true if GPS and reference clock inputs are ok + virtual bool is_clk_source_ok(void) const override; + virtual const char* device_name(void) const override; + + virtual double get_temperature(void) const override; + + private: + SDRDeviceConfig& m_conf; + + struct iio_context* m_ctx = nullptr; + struct iio_device* m_dexter_dsp_tx = nullptr; + + struct iio_device* m_ad9957_tx0 = nullptr; + struct iio_channel* m_tx_channel = nullptr; + struct iio_buffer *m_buffer = nullptr; + + size_t underflows = 0; + size_t overflows = 0; + size_t late_packets = 0; + size_t num_frames_modulated = 0; +}; + +} // namespace Output + +#endif //HAVE_DEXTER + -- cgit v1.2.3 From 0280bd327598342b5562ce11645fae8fcf649e2a Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Thu, 18 Aug 2022 13:15:57 +0200 Subject: Improve DEXTER SFN support --- src/ConfigParser.cpp | 3 +- src/DabMod.cpp | 45 ++++++++++--- src/EtiReader.cpp | 34 ++++------ src/EtiReader.h | 17 +++-- src/FicSource.cpp | 69 +++++++++----------- src/FicSource.h | 21 +++--- src/FormatConverter.cpp | 27 ++++++-- src/FormatConverter.h | 5 +- src/ModPlugin.h | 4 +- src/OutputFile.cpp | 31 ++++----- src/TimestampDecoder.cpp | 32 ++++++--- src/TimestampDecoder.h | 4 +- src/output/BladeRF.cpp | 2 +- src/output/BladeRF.h | 4 +- src/output/Dexter.cpp | 164 +++++++++++++++++++++++++++++++++++++++++------ src/output/Dexter.h | 10 ++- src/output/Feedback.cpp | 2 +- src/output/Feedback.h | 2 +- src/output/Lime.cpp | 2 +- src/output/Lime.h | 2 +- src/output/SDR.cpp | 15 +++-- src/output/SDR.h | 2 + src/output/SDRDevice.h | 5 +- src/output/Soapy.cpp | 2 +- src/output/Soapy.h | 2 +- src/output/UHD.cpp | 2 +- src/output/UHD.h | 2 +- src/output/USRPTime.cpp | 9 ++- 28 files changed, 351 insertions(+), 168 deletions(-) (limited to 'src/output') diff --git a/src/ConfigParser.cpp b/src/ConfigParser.cpp index 44d52e6..9190c60 100644 --- a/src/ConfigParser.cpp +++ b/src/ConfigParser.cpp @@ -409,7 +409,7 @@ static void parse_configfile( } -#if defined(HAVE_OUTPUT_UHD) +#if defined(HAVE_OUTPUT_UHD) || defined(HAVE_DEXTER) mod_settings.sdr_device_config.enableSync = (pt.GetInteger("delaymanagement.synchronous", 0) == 1); mod_settings.sdr_device_config.muteNoTimestamps = (pt.GetInteger("delaymanagement.mutenotimestamps", 0) == 1); if (mod_settings.sdr_device_config.enableSync) { @@ -430,7 +430,6 @@ static void parse_configfile( throw std::runtime_error("Configuration error"); } } - #endif diff --git a/src/DabMod.cpp b/src/DabMod.cpp index 15cdbaa..278f8ce 100644 --- a/src/DabMod.cpp +++ b/src/DabMod.cpp @@ -115,7 +115,7 @@ enum class run_modulator_state_t { reconfigure // Some sort of change of configuration we cannot handle happened }; -static run_modulator_state_t run_modulator(modulator_data& m); +static run_modulator_state_t run_modulator(const mod_settings_t& mod_settings, modulator_data& m); static void printModSettings(const mod_settings_t& mod_settings) { @@ -426,6 +426,10 @@ int launch_modulator(int argc, char* argv[]) if (format_converter) { flowgraph.connect(modulator, format_converter); flowgraph.connect(format_converter, output); + + if (auto o = dynamic_pointer_cast(output)) { + o->set_sample_size(format_converter->get_format_size()); + } } else { flowgraph.connect(modulator, output); @@ -435,7 +439,7 @@ int launch_modulator(int argc, char* argv[]) etiLog.level(info) << inputReader->GetPrintableInfo(); } - run_modulator_state_t st = run_modulator(m); + run_modulator_state_t st = run_modulator(mod_settings, m); etiLog.log(trace, "DABMOD,run_modulator() = %d", st); switch (st) { @@ -505,12 +509,13 @@ struct zmq_input_timeout : public std::exception } }; -static run_modulator_state_t run_modulator(modulator_data& m) +static run_modulator_state_t run_modulator(const mod_settings_t& mod_settings, modulator_data& m) { auto ret = run_modulator_state_t::failure; try { int last_eti_fct = -1; auto last_frame_received = chrono::steady_clock::now(); + frame_timestamp ts; Buffer data; if (m.inputReader) { data.setLength(6144); @@ -584,6 +589,7 @@ static run_modulator_state_t run_modulator(modulator_data& m) fct = m.etiReader->getFct(); fp = m.etiReader->getFp(); + ts = m.ediInput->ediReader.getTimestamp(); } else if (m.ediInput) { while (running and not m.ediInput->ediReader.isFrameReady()) { @@ -612,9 +618,10 @@ static run_modulator_state_t run_modulator(modulator_data& m) fct = m.ediInput->ediReader.getFct(); fp = m.ediInput->ediReader.getFp(); + ts = m.ediInput->ediReader.getTimestamp(); } - const unsigned expected_fct = (last_eti_fct + 1) % 250; + bool fct_good = false; if (last_eti_fct == -1) { if (fp != 0) { // Do not start the flowgraph before we get to FP 0 @@ -625,19 +632,37 @@ static run_modulator_state_t run_modulator(modulator_data& m) continue; } else { - last_eti_fct = fct; - m.framecount++; - m.flowgraph->run(); + fct_good = true; + } + } + else { + const unsigned expected_fct = (last_eti_fct + 1) % 250; + if (fct == expected_fct) { + fct_good = true; + } + else { + etiLog.level(info) << "ETI FCT discontinuity, expected " << + expected_fct << " received " << fct; + if (m.ediInput) { + m.ediInput->ediReader.clearFrame(); + } + return run_modulator_state_t::again; } } - else if (fct == expected_fct) { + + // timestamp is good if we run unsynchronised, or if it's in the future + bool ts_good = not mod_settings.sdr_device_config.enableSync or + (ts.timestamp_valid and ts.offset_to_system_time() > 0); + + if (fct_good and ts_good) { last_eti_fct = fct; m.framecount++; m.flowgraph->run(); } else { - etiLog.level(info) << "ETI FCT discontinuity, expected " << - expected_fct << " received " << fct; + etiLog.level(warn) << "Skipping frame " << fct << " FCT " << + (fct_good ? "good" : "bad") << " TS " << + (ts_good ? "good" : "bad"); if (m.ediInput) { m.ediInput->ediReader.clearFrame(); } diff --git a/src/EtiReader.cpp b/src/EtiReader.cpp index d1c7622..e992e62 100644 --- a/src/EtiReader.cpp +++ b/src/EtiReader.cpp @@ -78,6 +78,11 @@ unsigned EtiReader::getFct() return eti_fc.FCT; } +frame_timestamp EtiReader::getTimestamp() +{ + return myTimestampDecoder.getTimestamp(); +} + const std::vector > EtiReader::getSubchannels() const { @@ -278,22 +283,15 @@ int EtiReader::loadEtiData(const Buffer& dataIn) return dataIn.getLength() - input_size; } -bool EtiReader::sourceContainsTimestamp() -{ - return (ntohl(eti_tist.TIST) & 0xFFFFFF) != 0xFFFFFF; - /* See ETS 300 799, Annex C.2.2 */ -} - uint32_t EtiReader::getPPSOffset() { - if (!sourceContainsTimestamp()) { - //fprintf(stderr, "****** SOURCE NO TS\n"); + const uint32_t timestamp = ntohl(eti_tist.TIST) & 0xFFFFFF; + + /* See ETS 300 799, Annex C.2.2 */ + if (timestamp == 0xFFFFFF) { return 0.0; } - uint32_t timestamp = ntohl(eti_tist.TIST) & 0xFFFFFF; - //fprintf(stderr, "****** TIST 0x%x\n", timestamp); - return timestamp; } @@ -329,6 +327,11 @@ unsigned EdiReader::getFct() return m_fc.fct(); } +frame_timestamp EdiReader::getTimestamp() +{ + return m_timestamp_decoder.getTimestamp(); +} + const std::vector > EdiReader::getSubchannels() const { std::vector > sources; @@ -346,15 +349,6 @@ const std::vector > EdiReader::getSubchannels( return sources; } -bool EdiReader::sourceContainsTimestamp() -{ - if (not (m_frameReady and m_fc_valid)) { - throw std::runtime_error("Trying to get timestamp before it is ready"); - } - - return m_fc.tsta != 0xFFFFFF; -} - bool EdiReader::isFrameReady() { return m_frameReady; diff --git a/src/EtiReader.h b/src/EtiReader.h index d97acf6..fb2c84c 100644 --- a/src/EtiReader.h +++ b/src/EtiReader.h @@ -59,8 +59,8 @@ public: /* Get the current Frame Count */ virtual unsigned getFct() = 0; - /* Returns true if we have valid time stamps in the ETI*/ - virtual bool sourceContainsTimestamp() = 0; + /* Returns current Timestamp */ + virtual frame_timestamp getTimestamp() = 0; /* Return the FIC source to be used for modulation */ virtual std::shared_ptr& getFic(void); @@ -97,18 +97,17 @@ class EtiReader : public EtiSource public: EtiReader(double& tist_offset_s); - virtual unsigned getMode(); - virtual unsigned getFp(); - virtual unsigned getFct(); + virtual unsigned getMode() override; + virtual unsigned getFp() override; + virtual unsigned getFct() override; + virtual frame_timestamp getTimestamp() override; /* Read ETI data from dataIn. Returns the number of bytes * read from the buffer. */ int loadEtiData(const Buffer& dataIn); - virtual bool sourceContainsTimestamp(); - - virtual const std::vector > getSubchannels() const; + virtual const std::vector > getSubchannels() const override; private: /* Transform the ETI TIST to a PPS offset in units of 1/16384000 s */ @@ -141,7 +140,7 @@ public: virtual unsigned getMode() override; virtual unsigned getFp() override; virtual unsigned getFct() override; - virtual bool sourceContainsTimestamp() override; + virtual frame_timestamp getTimestamp() override; virtual const std::vector > getSubchannels() const override; virtual bool isFrameReady(void); diff --git a/src/FicSource.cpp b/src/FicSource.cpp index 2b95085..d824058 100644 --- a/src/FicSource.cpp +++ b/src/FicSource.cpp @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2018 + Copyright (C) 2022 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -27,7 +27,6 @@ #include "FicSource.h" #include "PcDebug.h" #include "Log.h" -#include "TimestampDecoder.h" #include #include @@ -36,46 +35,45 @@ #include -const std::vector& FicSource::get_rules() -{ - return d_puncturing_rules; -} - - FicSource::FicSource(unsigned ficf, unsigned mid) : ModInput() { // PDEBUG("FicSource::FicSource(...)\n"); // PDEBUG(" Start address: %i\n", d_start_address); -// PDEBUG(" Framesize: %i\n", d_framesize); +// PDEBUG(" Framesize: %i\n", m_framesize); // PDEBUG(" Protection: %i\n", d_protection); if (ficf == 0) { - d_framesize = 0; - d_buffer.setLength(0); + m_buffer.setLength(0); return; } if (mid == 3) { - d_framesize = 32 * 4; - d_puncturing_rules.emplace_back(29 * 16, 0xeeeeeeee); - d_puncturing_rules.emplace_back(3 * 16, 0xeeeeeeec); + m_framesize = 32 * 4; + m_puncturing_rules.emplace_back(29 * 16, 0xeeeeeeee); + m_puncturing_rules.emplace_back(3 * 16, 0xeeeeeeec); } else { - d_framesize = 24 * 4; - d_puncturing_rules.emplace_back(21 * 16, 0xeeeeeeee); - d_puncturing_rules.emplace_back(3 * 16, 0xeeeeeeec); + m_framesize = 24 * 4; + m_puncturing_rules.emplace_back(21 * 16, 0xeeeeeeee); + m_puncturing_rules.emplace_back(3 * 16, 0xeeeeeeec); } - d_buffer.setLength(d_framesize); + m_buffer.setLength(m_framesize); +} + +size_t FicSource::getFramesize() const +{ + return m_framesize; } -size_t FicSource::getFramesize() +const std::vector& FicSource::get_rules() const { - return d_framesize; + return m_puncturing_rules; } + void FicSource::loadFicData(const Buffer& fic) { - d_buffer = fic; + m_buffer = fic; } int FicSource::process(Buffer* outputData) @@ -83,34 +81,31 @@ int FicSource::process(Buffer* outputData) PDEBUG("FicSource::process (outputData: %p, outputSize: %zu)\n", outputData, outputData->getLength()); - if (d_buffer.getLength() != d_framesize) { + if (m_buffer.getLength() != m_framesize) { throw std::runtime_error( - "ERROR: FicSource::process.outputSize != d_framesize: " + - std::to_string(d_buffer.getLength()) + " != " + - std::to_string(d_framesize)); + "ERROR: FicSource::process.outputSize != m_framesize: " + + std::to_string(m_buffer.getLength()) + " != " + + std::to_string(m_framesize)); } - *outputData = d_buffer; + *outputData = m_buffer; return outputData->getLength(); } -void FicSource::loadTimestamp(const std::shared_ptr& ts) +void FicSource::loadTimestamp(const frame_timestamp& ts) { - d_ts = ts; + m_ts_valid = true; + m_ts = ts; } - meta_vec_t FicSource::process_metadata(const meta_vec_t& metadataIn) { - if (not d_ts) { - return {}; - } - - using namespace std; meta_vec_t md_vec; - flowgraph_metadata meta; - meta.ts = d_ts; - md_vec.push_back(meta); + if (m_ts_valid) { + flowgraph_metadata meta; + meta.ts = m_ts; + md_vec.push_back(meta); + } return md_vec; } diff --git a/src/FicSource.h b/src/FicSource.h index 93c1a7f..01dba2d 100644 --- a/src/FicSource.h +++ b/src/FicSource.h @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2016 + Copyright (C) 2022 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -33,6 +33,7 @@ #include "PuncturingRule.h" #include "Eti.h" #include "ModPlugin.h" +#include "TimestampDecoder.h" #include #include @@ -41,21 +42,21 @@ class FicSource : public ModInput, public ModMetadata public: FicSource(unsigned ficf, unsigned mid); - size_t getFramesize(); - const std::vector& get_rules(); + size_t getFramesize() const; + const std::vector& get_rules() const; void loadFicData(const Buffer& fic); int process(Buffer* outputData) override; const char* name() override { return "FicSource"; } - void loadTimestamp(const std::shared_ptr& ts); - virtual meta_vec_t process_metadata( - const meta_vec_t& metadataIn) override; + void loadTimestamp(const frame_timestamp& ts); + virtual meta_vec_t process_metadata(const meta_vec_t& metadataIn) override; private: - size_t d_framesize; - Buffer d_buffer; - std::shared_ptr d_ts; - std::vector d_puncturing_rules; + size_t m_framesize = 0; + Buffer m_buffer; + frame_timestamp m_ts; + bool m_ts_valid = false; + std::vector m_puncturing_rules; }; diff --git a/src/FormatConverter.cpp b/src/FormatConverter.cpp index 0f86d42..cda8a4d 100644 --- a/src/FormatConverter.cpp +++ b/src/FormatConverter.cpp @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2017 + Copyright (C) 2022 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -34,10 +34,6 @@ #include #include -#ifdef __SSE__ -# include -#endif - FormatConverter::FormatConverter(const std::string& format) : ModCodec(), m_format(format) @@ -68,7 +64,7 @@ int FormatConverter::process(Buffer* const dataIn, Buffer* dataOut) out[i] = in[i] + 128; } } - else { + else if (m_format == "s8") { dataOut->setLength(sizeIn * sizeof(int8_t)); int8_t* out = reinterpret_cast(dataOut->getData()); @@ -76,6 +72,9 @@ int FormatConverter::process(Buffer* const dataIn, Buffer* dataOut) out[i] = in[i]; } } + else { + throw std::runtime_error("FormatConverter: Invalid format " + m_format); + } return dataOut->getLength(); } @@ -85,3 +84,19 @@ const char* FormatConverter::name() return "FormatConverter"; } +size_t FormatConverter::get_format_size() const +{ + // Returns 2*sizeof(SAMPLE_TYPE) because we have I + Q + if (m_format == "s16") { + return 4; + } + else if (m_format == "u8") { + return 2; + } + else if (m_format == "s8") { + return 2; + } + else { + throw std::runtime_error("FormatConverter: Invalid format " + m_format); + } +} diff --git a/src/FormatConverter.h b/src/FormatConverter.h index cc8a606..ceb2e17 100644 --- a/src/FormatConverter.h +++ b/src/FormatConverter.h @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2017 + Copyright (C) 2022 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -40,11 +40,14 @@ class FormatConverter : public ModCodec { public: + // Allowed formats: s8, u8 and s16 FormatConverter(const std::string& format); int process(Buffer* const dataIn, Buffer* dataOut); const char* name(); + size_t get_format_size() const; + private: std::string m_format; }; diff --git a/src/ModPlugin.h b/src/ModPlugin.h index 7f03618..470508f 100644 --- a/src/ModPlugin.h +++ b/src/ModPlugin.h @@ -32,6 +32,7 @@ #include "Buffer.h" #include "ThreadsafeQueue.h" +#include "TimestampDecoder.h" #include #include #include @@ -41,9 +42,8 @@ // All flowgraph elements derive from ModPlugin, or a variant of it. // Some ModPlugins also support handling metadata. -struct frame_timestamp; struct flowgraph_metadata { - std::shared_ptr ts; + frame_timestamp ts; }; using meta_vec_t = std::vector; diff --git a/src/OutputFile.cpp b/src/OutputFile.cpp index acaebad..2ee838c 100644 --- a/src/OutputFile.cpp +++ b/src/OutputFile.cpp @@ -74,28 +74,23 @@ meta_vec_t OutputFile::process_metadata(const meta_vec_t& metadataIn) frame_timestamp first_ts; for (const auto& md : metadataIn) { - if (md.ts) { - // The following code assumes TM I, where we get called every 96ms. - // Support for other transmission modes skipped because this is mostly - // debugging code. + // The following code assumes TM I, where we get called every 96ms. + // Support for other transmission modes skipped because this is mostly + // debugging code. - if (md.ts->fp == 0 or md.ts->fp == 4) { - first_ts = *md.ts; - } + if (md.ts.fp == 0 or md.ts.fp == 4) { + first_ts = md.ts; + } - ss << " FCT=" << md.ts->fct << - " FP=" << (int)md.ts->fp; - if (md.ts->timestamp_valid) { - ss << " TS=" << md.ts->timestamp_sec << " + " << - std::fixed - << (double)md.ts->timestamp_pps / 163840000.0 << ";"; - } - else { - ss << " TS invalid;"; - } + ss << " FCT=" << md.ts.fct << + " FP=" << (int)md.ts.fp; + if (md.ts.timestamp_valid) { + ss << " TS=" << md.ts.timestamp_sec << " + " << + std::fixed + << (double)md.ts.timestamp_pps / 163840000.0 << ";"; } else { - ss << " void, "; + ss << " TS invalid;"; } } diff --git a/src/TimestampDecoder.cpp b/src/TimestampDecoder.cpp index 3cfa0cc..54a5817 100644 --- a/src/TimestampDecoder.cpp +++ b/src/TimestampDecoder.cpp @@ -36,6 +36,20 @@ //#define MDEBUG(fmt, args...) fprintf (LOG, "*****" fmt , ## args) #define MDEBUG(fmt, args...) PDEBUG(fmt, ## args) +double frame_timestamp::offset_to_system_time() const +{ + if (not timestamp_valid) { + throw new std::runtime_error("Cannot calculate offset for invalid timestamp"); + } + + struct timespec t; + if (clock_gettime(CLOCK_REALTIME, &t) != 0) { + throw std::runtime_error(std::string("Failed to retrieve CLOCK_REALTIME") + strerror(errno)); + } + + return get_real_secs() - (double)t.tv_sec - (t.tv_nsec / 1000000000.0); +} + frame_timestamp& frame_timestamp::operator+=(const double& diff) { double offset_pps, offset_secs; @@ -75,20 +89,20 @@ TimestampDecoder::TimestampDecoder(double& offset_s) : timestamp_offset << " offset"; } -std::shared_ptr TimestampDecoder::getTimestamp() +frame_timestamp TimestampDecoder::getTimestamp() { - auto ts = std::make_shared(); + frame_timestamp ts; - ts->timestamp_valid = full_timestamp_received; - ts->timestamp_sec = time_secs; - ts->timestamp_pps = time_pps; - ts->fct = latestFCT; - ts->fp = latestFP; + ts.timestamp_valid = full_timestamp_received; + ts.timestamp_sec = time_secs; + ts.timestamp_pps = time_pps; + ts.fct = latestFCT; + ts.fp = latestFP; - ts->timestamp_refresh = offset_changed; + ts.timestamp_refresh = offset_changed; offset_changed = false; - *ts += timestamp_offset; + ts += timestamp_offset; return ts; } diff --git a/src/TimestampDecoder.h b/src/TimestampDecoder.h index d083061..3616bab 100644 --- a/src/TimestampDecoder.h +++ b/src/TimestampDecoder.h @@ -56,6 +56,8 @@ struct frame_timestamp return timestamp_pps / 16384000.0; } + double offset_to_system_time() const; + double get_real_secs() const { double t = timestamp_sec; t += pps_offset(); @@ -93,7 +95,7 @@ class TimestampDecoder : public RemoteControllable */ TimestampDecoder(double& offset_s); - std::shared_ptr getTimestamp(void); + frame_timestamp getTimestamp(void); /* Update timestamp data from ETI */ void updateTimestampEti( diff --git a/src/output/BladeRF.cpp b/src/output/BladeRF.cpp index a6ad0cc..dd48736 100755 --- a/src/output/BladeRF.cpp +++ b/src/output/BladeRF.cpp @@ -269,7 +269,7 @@ double BladeRF::get_rxgain(void) const size_t BladeRF::receive_frame( complexf *buf, size_t num_samples, - struct frame_timestamp &ts, + frame_timestamp &ts, double timeout_secs) { // TODO diff --git a/src/output/BladeRF.h b/src/output/BladeRF.h index bc6db38..e048daa 100755 --- a/src/output/BladeRF.h +++ b/src/output/BladeRF.h @@ -83,7 +83,7 @@ class BladeRF : public Output::SDRDevice virtual size_t receive_frame( complexf *buf, size_t num_samples, - struct frame_timestamp& ts, + frame_timestamp& ts, double timeout_secs) override; // Return true if GPS and reference clock inputs are ok @@ -109,4 +109,4 @@ class BladeRF : public Output::SDRDevice } // namespace Output -#endif // HAVE_BLADERF \ No newline at end of file +#endif // HAVE_BLADERF diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index 605c61a..e4f672b 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -44,7 +44,11 @@ using namespace std; namespace Output { +static constexpr uint64_t DSP_CLOCK = 2048000uLL * 80; + static constexpr size_t TRANSMISSION_FRAME_LEN = (2656 + 76 * 2552) * 4; +static constexpr size_t IIO_BUFFERS = 4; +static constexpr size_t IIO_BUFFER_LEN = TRANSMISSION_FRAME_LEN / IIO_BUFFERS; static string get_iio_error(int err) { @@ -53,6 +57,13 @@ static string get_iio_error(int err) return string(dst); } +static void fill_time(struct timespec *t) +{ + if (clock_gettime(CLOCK_REALTIME, t) != 0) { + throw std::runtime_error(string("Failed to retrieve CLOCK_REALTIME") + strerror(errno)); + } +} + Dexter::Dexter(SDRDeviceConfig& config) : SDRDevice(), m_conf(config) @@ -93,8 +104,6 @@ Dexter::Dexter(SDRDeviceConfig& config) : etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0: " << get_iio_error(r); } - //iio_device_attr_read_longlong(const struct iio_device *dev, const char *attr, long long *val); - if (m_conf.sampleRate != 2048000) { throw std::runtime_error("Dexter: Only 2048000 samplerate supported"); } @@ -109,7 +118,62 @@ Dexter::Dexter(SDRDeviceConfig& config) : // skip: antenna - // TODO: set H/W time + // get H/W time + /* Procedure: + * Wait 200ms after second change, fetch pps_clks attribute + * idem at the next second, and check that pps_clks incremented by DSP_CLOCK + * If ok, store the correspondence between current second change (measured in UTC clock time) + * and the counter value at pps rising edge. */ + + etiLog.level(info) << "Dexter: Waiting for second change..."; + + struct timespec time_at_startup; + fill_time(&time_at_startup); + time_at_startup.tv_nsec = 0; + + struct timespec time_now; + do { + fill_time(&time_now); + this_thread::sleep_for(chrono::milliseconds(1)); + } while (time_at_startup.tv_sec == time_now.tv_sec); + this_thread::sleep_for(chrono::milliseconds(200)); + + long long pps_clks = 0; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "pps_clks", &pps_clks)) != 0) { + etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_clks: " << get_iio_error(r); + throw std::runtime_error("Dexter: Cannot read IIO attribute"); + } + + time_t tnow = time_now.tv_sec; + etiLog.level(info) << "Dexter: pps_clks " << pps_clks << " at UTC " << + put_time(std::gmtime(&tnow), "%Y-%m-%d %H:%M:%S"); + + time_at_startup.tv_sec = time_now.tv_sec; + do { + fill_time(&time_now); + this_thread::sleep_for(chrono::milliseconds(1)); + } while (time_at_startup.tv_sec == time_now.tv_sec); + this_thread::sleep_for(chrono::milliseconds(200)); + + long long pps_clks2 = 0; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "pps_clks", &pps_clks2)) != 0) { + etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_clks: " << get_iio_error(r); + throw std::runtime_error("Dexter: Cannot read IIO attribute"); + } + tnow = time_now.tv_sec; + etiLog.level(info) << "Dexter: pps_clks increased by " << pps_clks2 - pps_clks << " at UTC " << + put_time(std::gmtime(&tnow), "%Y-%m-%d %H:%M:%S"); + + if ((uint64_t)pps_clks + DSP_CLOCK != (uint64_t)pps_clks2) { + throw std::runtime_error("Dexter: Wrong increase of pps_clks, expected " + to_string(DSP_CLOCK)); + } + m_utc_seconds_at_startup = time_now.tv_sec; + m_clock_count_at_startup = pps_clks2; + + // Reset start_clks + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", 0)) != 0) { + etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = " << 0 << " : " << get_iio_error(r); + } // Prepare streams constexpr int CHANNEL_INDEX = 0; @@ -120,7 +184,7 @@ Dexter::Dexter(SDRDeviceConfig& config) : iio_channel_enable(m_tx_channel); - m_buffer = iio_device_create_buffer(m_ad9957_tx0, TRANSMISSION_FRAME_LEN/sizeof(int16_t), 0); + m_buffer = iio_device_create_buffer(m_ad9957_tx0, IIO_BUFFER_LEN/sizeof(int16_t), 0); if (!m_buffer) { throw std::runtime_error("Dexter: Cannot create IIO buffer."); } @@ -208,8 +272,8 @@ SDRDevice::RunStatistics Dexter::get_run_statistics(void) const { RunStatistics rs; rs.num_underruns = underflows; - rs.num_overruns = overflows; - rs.num_late_packets = late_packets; + rs.num_overruns = 0; + rs.num_late_packets = num_late; rs.num_frames_modulated = num_frames_modulated; return rs; } @@ -217,8 +281,22 @@ SDRDevice::RunStatistics Dexter::get_run_statistics(void) const double Dexter::get_real_secs(void) const { - // TODO - return 0; + struct timespec time_now; + fill_time(&time_now); + return (double)time_now.tv_sec + time_now.tv_nsec / 1000000000.0; + + /* We don't use actual device time, because we only have clock counter on pps edge available, not + * current clock counter. */ +#if 0 + long long pps_clks = 0; + int r = 0; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "pps_clks", &pps_clks)) != 0) { + etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_clks: " << get_iio_error(r); + throw std::runtime_error("Dexter: Cannot read IIO attribute"); + } + + return (double)m_utc_seconds_at_startup + (double)(pps_clks - m_clock_count_at_startup) / (double)DSP_CLOCK; +#endif } void Dexter::set_rxgain(double rxgain) @@ -235,7 +313,7 @@ double Dexter::get_rxgain(void) const size_t Dexter::receive_frame( complexf *buf, size_t num_samples, - struct frame_timestamp& ts, + frame_timestamp& ts, double timeout_secs) { // TODO @@ -263,34 +341,82 @@ double Dexter::get_temperature(void) const void Dexter::transmit_frame(const struct FrameData& frame) { - long long int timeNs = frame.ts.get_ns(); - const bool has_time_spec = (m_conf.enableSync and frame.ts.timestamp_valid); - if (frame.buf.size() != TRANSMISSION_FRAME_LEN) { etiLog.level(debug) << "Dexter::transmit_frame Expected " << TRANSMISSION_FRAME_LEN << " got " << frame.buf.size(); throw std::runtime_error("Dexter: invalid buffer size"); } + const bool has_time_spec = (m_conf.enableSync and frame.ts.timestamp_valid); + + if (has_time_spec) { + /* + uint64_t timeS = frame.ts.timestamp_sec; + etiLog.level(debug) << "Dexter: TS S " << timeS << " - " << m_utc_seconds_at_startup << " = " << + timeS - m_utc_seconds_at_startup; + */ + + // 10 because timestamp_pps is represented in 16.384 MHz clocks + constexpr uint64_t TIMESTAMP_PPS_PER_DSP_CLOCKS = DSP_CLOCK / 16384000; + uint64_t frame_ts_clocks = + // at second level + ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK + m_clock_count_at_startup + + // at subsecond level + (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS; + + long long pps_clks = 0; + int r; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "pps_clks", &pps_clks)) != 0) { + etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_clks: " << get_iio_error(r); + } + + etiLog.level(debug) << "Dexter: TS CLK " << + ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK << " + " << + m_clock_count_at_startup << " + " << + (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS << " = " << + frame_ts_clocks << " DELTA " << + frame_ts_clocks << " - " << pps_clks << " = " << + (double)((int64_t)frame_ts_clocks - pps_clks) / DSP_CLOCK; + + // Ensure we hand the frame over to HW at least 0.1s before timestamp + if (((int64_t)frame_ts_clocks - pps_clks) < (int64_t)DSP_CLOCK / 10) { + etiLog.level(warn) << "Skip frame short margin"; + num_late++; + return; + } + + + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", frame_ts_clocks)) != 0) { + etiLog.level(warn) << "Skip frame, failed to set dexter_dsp_tx.stream0_start_clks = " << frame_ts_clocks << " : " << get_iio_error(r); + num_late++; + return; + } + } + // DabMod::launch_modulator ensures we get int16_t IQ here //const size_t num_samples = frame.buf.size() / (2*sizeof(int16_t)); //const int16_t *buf = reinterpret_cast(frame.buf.data()); - memcpy(iio_buffer_start(m_buffer), frame.buf.data(), frame.buf.size()); - ssize_t pushed = iio_buffer_push(m_buffer); - if (pushed < 0) { - etiLog.level(error) << "Dexter: failed to push buffer " << get_iio_error(pushed); + for (size_t i = 0; i < IIO_BUFFERS; i++) { + constexpr size_t buflen = TRANSMISSION_FRAME_LEN / IIO_BUFFERS; + + memcpy(iio_buffer_start(m_buffer), frame.buf.data() + (i * buflen), buflen); + ssize_t pushed = iio_buffer_push(m_buffer); + if (pushed < 0) { + etiLog.level(error) << "Dexter: failed to push buffer " << get_iio_error(pushed); + } } num_frames_modulated++; - // TODO overflows, late_packets long long attr_value = 0; int r = 0; if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "buffer_underflows0", &attr_value)) == 0) { - fprintf(stderr, "buffer_underflows0 %lld\n", attr_value); - underflows = attr_value; + if ((size_t)attr_value != underflows and underflows != 0) { + etiLog.level(warn) << "Dexter: underflow! " << underflows << " -> " << attr_value; + underflows = attr_value; + } } } diff --git a/src/output/Dexter.h b/src/output/Dexter.h index 7a7f6c1..5418b73 100644 --- a/src/output/Dexter.h +++ b/src/output/Dexter.h @@ -39,6 +39,7 @@ DESCRIPTION: #include #include +#include #include "output/SDR.h" #include "ModPlugin.h" @@ -70,7 +71,7 @@ class Dexter : public Output::SDRDevice virtual size_t receive_frame( complexf *buf, size_t num_samples, - struct frame_timestamp& ts, + frame_timestamp& ts, double timeout_secs) override; // Return true if GPS and reference clock inputs are ok @@ -90,9 +91,12 @@ class Dexter : public Output::SDRDevice struct iio_buffer *m_buffer = nullptr; size_t underflows = 0; - size_t overflows = 0; - size_t late_packets = 0; + size_t num_late = 0; size_t num_frames_modulated = 0; + + uint64_t m_utc_seconds_at_startup; + uint64_t m_clock_count_at_startup = 0; + uint64_t m_clock_count_frame = 0; }; } // namespace Output diff --git a/src/output/Feedback.cpp b/src/output/Feedback.cpp index 88d8319..d112b5a 100644 --- a/src/output/Feedback.cpp +++ b/src/output/Feedback.cpp @@ -84,7 +84,7 @@ DPDFeedbackServer::~DPDFeedbackServer() void DPDFeedbackServer::set_tx_frame( const std::vector &buf, - const struct frame_timestamp &buf_ts) + const frame_timestamp &buf_ts) { if (not m_running) { throw runtime_error("DPDFeedbackServer not running"); diff --git a/src/output/Feedback.h b/src/output/Feedback.h index aef86b0..b31347f 100644 --- a/src/output/Feedback.h +++ b/src/output/Feedback.h @@ -94,7 +94,7 @@ class DPDFeedbackServer { ~DPDFeedbackServer(); void set_tx_frame(const std::vector &buf, - const struct frame_timestamp& ts); + const frame_timestamp& ts); private: // Thread that reacts to burstRequests and receives from the SDR device diff --git a/src/output/Lime.cpp b/src/output/Lime.cpp index 6f7eed5..d3e4640 100644 --- a/src/output/Lime.cpp +++ b/src/output/Lime.cpp @@ -353,7 +353,7 @@ double Lime::get_rxgain(void) const size_t Lime::receive_frame( complexf *buf, size_t num_samples, - struct frame_timestamp &ts, + frame_timestamp &ts, double timeout_secs) { // TODO diff --git a/src/output/Lime.h b/src/output/Lime.h index 72a018e..a4603c0 100644 --- a/src/output/Lime.h +++ b/src/output/Lime.h @@ -75,7 +75,7 @@ class Lime : public Output::SDRDevice virtual size_t receive_frame( complexf *buf, size_t num_samples, - struct frame_timestamp &ts, + frame_timestamp &ts, double timeout_secs) override; // Return true if GPS and reference clock inputs are ok diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index 6078fc7..f1ed2b0 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -104,6 +104,12 @@ SDR::~SDR() } } +void SDR::set_sample_size(size_t size) +{ + etiLog.level(debug) << "Setting sample size to " << size; + m_size = size; +} + int SDR::process(Buffer *dataIn) { if (not m_running) { @@ -125,6 +131,7 @@ meta_vec_t SDR::process_metadata(const meta_vec_t& metadataIn) if (m_device and m_running) { FrameData frame; frame.buf = std::move(m_frame); + frame.sampleSize = m_size; if (metadataIn.empty()) { etiLog.level(info) << @@ -138,7 +145,7 @@ meta_vec_t SDR::process_metadata(const meta_vec_t& metadataIn) * This behaviour is different to earlier versions of ODR-DabMod, * which took the timestamp from the latest ETI frame. */ - frame.ts = *(metadataIn[0].ts); + frame.ts = metadataIn[0].ts; // TODO check device running @@ -261,8 +268,6 @@ void SDR::handle_frame(struct FrameData& frame) { // Assumes m_device is valid - constexpr double tx_timeout = 20.0; - if (not m_device->is_clk_source_ok()) { sleep_through_frame(); return; @@ -298,7 +303,7 @@ void SDR::handle_frame(struct FrameData& frame) } if (last_tx_time_initialised) { - const size_t sizeIn = frame.buf.size() / sizeof(complexf); + const size_t sizeIn = frame.buf.size() / frame.sampleSize; // Checking units for the increment calculation: // samps * ticks/s / (samps/s) @@ -337,7 +342,7 @@ void SDR::handle_frame(struct FrameData& frame) etiLog.log(trace, "SDR,tist %f", time_spec.get_real_secs()); - if (time_spec.get_real_secs() + tx_timeout < device_time) { + if (time_spec.get_real_secs() < device_time) { etiLog.level(warn) << "OutputSDR: Timestamp in the past at FCT=" << frame.ts.fct << " offset: " << std::fixed << diff --git a/src/output/SDR.h b/src/output/SDR.h index ee89243..4dfde73 100644 --- a/src/output/SDR.h +++ b/src/output/SDR.h @@ -51,6 +51,7 @@ class SDR : public ModOutput, public ModMetadata, public RemoteControllable { SDR operator=(const SDR& other) = delete; virtual ~SDR(); + virtual void set_sample_size(size_t size); virtual int process(Buffer *dataIn) override; virtual meta_vec_t process_metadata(const meta_vec_t& metadataIn) override; @@ -75,6 +76,7 @@ class SDR : public ModOutput, public ModMetadata, public RemoteControllable { std::atomic m_running = ATOMIC_VAR_INIT(false); std::thread m_device_thread; + size_t m_size = sizeof(complexf); std::vector m_frame; ThreadsafeQueue m_queue; diff --git a/src/output/SDRDevice.h b/src/output/SDRDevice.h index bb63f60..0bba74a 100644 --- a/src/output/SDRDevice.h +++ b/src/output/SDRDevice.h @@ -98,10 +98,11 @@ struct SDRDeviceConfig { struct FrameData { // Buffer holding frame data std::vector buf; + size_t sampleSize = sizeof(complexf); // A full timestamp contains a TIST according to standard // and time information within MNSC with tx_second. - struct frame_timestamp ts; + frame_timestamp ts; }; @@ -132,7 +133,7 @@ class SDRDevice { virtual size_t receive_frame( complexf *buf, size_t num_samples, - struct frame_timestamp& ts, + frame_timestamp& ts, double timeout_secs) = 0; // Returns device temperature in degrees C or NaN if not available diff --git a/src/output/Soapy.cpp b/src/output/Soapy.cpp index f138e9a..50f91a4 100644 --- a/src/output/Soapy.cpp +++ b/src/output/Soapy.cpp @@ -216,7 +216,7 @@ double Soapy::get_rxgain(void) const size_t Soapy::receive_frame( complexf *buf, size_t num_samples, - struct frame_timestamp& ts, + frame_timestamp& ts, double timeout_secs) { int flags = 0; diff --git a/src/output/Soapy.h b/src/output/Soapy.h index 4ee53ca..ca2618b 100644 --- a/src/output/Soapy.h +++ b/src/output/Soapy.h @@ -74,7 +74,7 @@ class Soapy : public Output::SDRDevice virtual size_t receive_frame( complexf *buf, size_t num_samples, - struct frame_timestamp& ts, + frame_timestamp& ts, double timeout_secs) override; // Return true if GPS and reference clock inputs are ok diff --git a/src/output/UHD.cpp b/src/output/UHD.cpp index 3cf5aef..9b22dde 100644 --- a/src/output/UHD.cpp +++ b/src/output/UHD.cpp @@ -411,7 +411,7 @@ double UHD::get_rxgain() const size_t UHD::receive_frame( complexf *buf, size_t num_samples, - struct frame_timestamp& ts, + frame_timestamp& ts, double timeout_secs) { uhd::stream_cmd_t cmd( diff --git a/src/output/UHD.h b/src/output/UHD.h index 29867fb..4c1a4f0 100644 --- a/src/output/UHD.h +++ b/src/output/UHD.h @@ -88,7 +88,7 @@ class UHD : public Output::SDRDevice virtual size_t receive_frame( complexf *buf, size_t num_samples, - struct frame_timestamp& ts, + frame_timestamp& ts, double timeout_secs) override; // Return true if GPS and reference clock inputs are ok diff --git a/src/output/USRPTime.cpp b/src/output/USRPTime.cpp index d1197ec..5a11851 100644 --- a/src/output/USRPTime.cpp +++ b/src/output/USRPTime.cpp @@ -46,11 +46,14 @@ USRPTime::USRPTime( m_conf(conf), time_last_check(timepoint_t::clock::now()) { - if (m_conf.pps_src == "none") { + if (m_conf.refclk_src == "internal" and m_conf.pps_src != "none") { + etiLog.level(warn) << "OutputUHD: Unusal refclk and pps source settings. Setting time once, no monitoring."; + set_usrp_time_from_pps(); + } + else if (m_conf.pps_src == "none") { if (m_conf.enableSync) { etiLog.level(warn) << - "OutputUHD: WARNING:" - " you are using synchronous transmission without PPS input!"; + "OutputUHD: you are using synchronous transmission without PPS input!"; } set_usrp_time_from_localtime(); -- cgit v1.2.3 From 7441d427947aaa79b88e1eae157981c95d7ca5fa Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 24 Aug 2022 16:14:15 +0200 Subject: Add TS debugging output --- src/TimestampDecoder.cpp | 11 +++++++++++ src/TimestampDecoder.h | 4 +++- src/output/Dexter.cpp | 2 ++ src/output/SDR.cpp | 6 ++++++ 4 files changed, 22 insertions(+), 1 deletion(-) (limited to 'src/output') diff --git a/src/TimestampDecoder.cpp b/src/TimestampDecoder.cpp index 54a5817..c701186 100644 --- a/src/TimestampDecoder.cpp +++ b/src/TimestampDecoder.cpp @@ -50,6 +50,17 @@ double frame_timestamp::offset_to_system_time() const return get_real_secs() - (double)t.tv_sec - (t.tv_nsec / 1000000000.0); } +std::string frame_timestamp::to_string() const +{ + time_t s = timestamp_sec; + std::stringstream ss; + char timestr[100]; + if (std::strftime(timestr, sizeof(timestr), "%Y-%m-%dZ%H:%M:%S", std::gmtime(&s))) { + ss << timestr << " + " << ((double)timestamp_pps / 16384000.0); + } + return ss.str(); +} + frame_timestamp& frame_timestamp::operator+=(const double& diff) { double offset_pps, offset_secs; diff --git a/src/TimestampDecoder.h b/src/TimestampDecoder.h index 3616bab..4e94d4c 100644 --- a/src/TimestampDecoder.h +++ b/src/TimestampDecoder.h @@ -39,7 +39,7 @@ struct frame_timestamp int32_t fct; uint8_t fp; // Frame Phase - uint32_t timestamp_sec; + uint32_t timestamp_sec; // seconds in unix epoch uint32_t timestamp_pps; // In units of 1/16384000 s bool timestamp_valid = false; bool timestamp_refresh; @@ -76,6 +76,8 @@ struct frame_timestamp timestamp_pps = lrint(subsecond * 16384000.0); } + std::string to_string() const; + void print(const char* t) const { etiLog.log(debug, "%s \n", diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index e4f672b..b389b31 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -370,6 +370,7 @@ void Dexter::transmit_frame(const struct FrameData& frame) etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_clks: " << get_iio_error(r); } + /* etiLog.level(debug) << "Dexter: TS CLK " << ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK << " + " << m_clock_count_at_startup << " + " << @@ -377,6 +378,7 @@ void Dexter::transmit_frame(const struct FrameData& frame) frame_ts_clocks << " DELTA " << frame_ts_clocks << " - " << pps_clks << " = " << (double)((int64_t)frame_ts_clocks - pps_clks) / DSP_CLOCK; + */ // Ensure we hand the frame over to HW at least 0.1s before timestamp if (((int64_t)frame_ts_clocks - pps_clks) < (int64_t)DSP_CLOCK / 10) { diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index f1ed2b0..bd02cab 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -370,6 +370,12 @@ void SDR::handle_frame(struct FrameData& frame) return; } + if (frame.ts.fct == 0) { + etiLog.level(debug) << + "OutputSDR: TX FCT=" << frame.ts.fct << + " TS " << frame.ts.to_string(); + } + m_device->transmit_frame(frame); } -- cgit v1.2.3 From a2be0c3ab77dab50ded4850f38d2b796b322d0c4 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 14 Oct 2022 20:00:26 +0200 Subject: Add timestamp refresh logic --- src/DabMod.cpp | 4 ++- src/output/Dexter.cpp | 67 +++++++++++++++++++++++++++++++++++++-------------- src/output/Dexter.h | 8 ++++++ src/output/SDR.cpp | 1 + 4 files changed, 61 insertions(+), 19 deletions(-) (limited to 'src/output') diff --git a/src/DabMod.cpp b/src/DabMod.cpp index 278f8ce..5a4da9a 100644 --- a/src/DabMod.cpp +++ b/src/DabMod.cpp @@ -662,7 +662,9 @@ static run_modulator_state_t run_modulator(const mod_settings_t& mod_settings, m else { etiLog.level(warn) << "Skipping frame " << fct << " FCT " << (fct_good ? "good" : "bad") << " TS " << - (ts_good ? "good" : "bad"); + (ts_good ? "good, " : "bad, ") << + (ts.timestamp_valid ? (ts.offset_to_system_time() > 0 ? "in the future" : "in the past") : "invalid"); + if (m.ediInput) { m.ediInput->ediReader.clearFrame(); } diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index b389b31..9437ae6 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -347,9 +347,13 @@ void Dexter::transmit_frame(const struct FrameData& frame) throw std::runtime_error("Dexter: invalid buffer size"); } - const bool has_time_spec = (m_conf.enableSync and frame.ts.timestamp_valid); + const bool require_timestamped_tx = (m_conf.enableSync and frame.ts.timestamp_valid); - if (has_time_spec) { + if (not require_timestamped_tx) { + etiLog.level(debug) << "TIMESTAMP_STATE STREAMING 1"; + timestamp_state = timestamp_state_t::STREAMING; + } + else if (require_timestamped_tx and timestamp_state == timestamp_state_t::REQUIRES_SET) { /* uint64_t timeS = frame.ts.timestamp_sec; etiLog.level(debug) << "Dexter: TS S " << timeS << " - " << m_utc_seconds_at_startup << " = " << @@ -393,31 +397,58 @@ void Dexter::transmit_frame(const struct FrameData& frame) num_late++; return; } + timestamp_state = timestamp_state_t::STREAMING; + etiLog.level(debug) << "TIMESTAMP_STATE STREAMING 2"; + } + + if (frame.ts.timestamp_refresh) { + etiLog.level(debug) << "TIMESTAMP_STATE WAIT_FOR_UNDERRUN"; + timestamp_state = timestamp_state_t::WAIT_FOR_UNDERRUN; + long long attr_value = 0; + int r = 0; + + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "buffer_underflows0", &attr_value)) == 0) { + underflows = attr_value; + etiLog.level(debug) << "UNDERFLOWS CAPTURE " << underflows; + } } // DabMod::launch_modulator ensures we get int16_t IQ here //const size_t num_samples = frame.buf.size() / (2*sizeof(int16_t)); //const int16_t *buf = reinterpret_cast(frame.buf.data()); - for (size_t i = 0; i < IIO_BUFFERS; i++) { - constexpr size_t buflen = TRANSMISSION_FRAME_LEN / IIO_BUFFERS; - - memcpy(iio_buffer_start(m_buffer), frame.buf.data() + (i * buflen), buflen); - ssize_t pushed = iio_buffer_push(m_buffer); - if (pushed < 0) { - etiLog.level(error) << "Dexter: failed to push buffer " << get_iio_error(pushed); + if (timestamp_state == timestamp_state_t::STREAMING) { + for (size_t i = 0; i < IIO_BUFFERS; i++) { + constexpr size_t buflen = TRANSMISSION_FRAME_LEN / IIO_BUFFERS; + + memcpy(iio_buffer_start(m_buffer), frame.buf.data() + (i * buflen), buflen); + ssize_t pushed = iio_buffer_push(m_buffer); + if (pushed < 0) { + etiLog.level(error) << "Dexter: failed to push buffer " << get_iio_error(pushed); + etiLog.level(debug) << "TIMESTAMP_STATE REQUIRES_SET"; + timestamp_state = timestamp_state_t::REQUIRES_SET; + } } + num_frames_modulated++; } - num_frames_modulated++; - - long long attr_value = 0; - int r = 0; - - if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "buffer_underflows0", &attr_value)) == 0) { - if ((size_t)attr_value != underflows and underflows != 0) { - etiLog.level(warn) << "Dexter: underflow! " << underflows << " -> " << attr_value; - underflows = attr_value; +#warning "We should update underflows all the time" + if (timestamp_state == timestamp_state_t::WAIT_FOR_UNDERRUN) { + long long attr_value = 0; + int r = 0; + + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "buffer_underflows0", &attr_value)) == 0) { + size_t underflows_new = attr_value; + etiLog.level(debug) << "UNDERFLOWS COMPARE " << underflows_new; + + if (underflows_new != underflows and attr_value != 0) { + etiLog.level(warn) << "Dexter: underflow! " << underflows << " -> " << underflows_new; + underflows = underflows_new; + if (timestamp_state == timestamp_state_t::WAIT_FOR_UNDERRUN) { + etiLog.level(debug) << "TIMESTAMP_STATE REQUIRES_SET"; + timestamp_state = timestamp_state_t::REQUIRES_SET; + } + } } } } diff --git a/src/output/Dexter.h b/src/output/Dexter.h index 5418b73..3e9c34f 100644 --- a/src/output/Dexter.h +++ b/src/output/Dexter.h @@ -97,6 +97,14 @@ class Dexter : public Output::SDRDevice uint64_t m_utc_seconds_at_startup; uint64_t m_clock_count_at_startup = 0; uint64_t m_clock_count_frame = 0; + + enum class timestamp_state_t { + REQUIRES_SET, + STREAMING, + WAIT_FOR_UNDERRUN, + }; + + timestamp_state_t timestamp_state = timestamp_state_t::REQUIRES_SET; }; } // namespace Output diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index bd02cab..53f68c2 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -331,6 +331,7 @@ void SDR::handle_frame(struct FrameData& frame) "(" << tx_pps << ")"; frame.ts.timestamp_refresh = true; +#error "wrong, as the frame could be discarded" } } -- cgit v1.2.3 From d2c8a1f40be73417964523e5a942d7d4c558e967 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 26 Oct 2022 18:04:55 +0200 Subject: Add dexter underflow thread, still problematic --- src/DabMod.cpp | 3 +- src/output/Dexter.cpp | 76 +++++++++++++++++++++++++++++++++++---------------- src/output/Dexter.h | 29 ++++++++++++++------ src/output/SDR.cpp | 1 - 4 files changed, 73 insertions(+), 36 deletions(-) (limited to 'src/output') diff --git a/src/DabMod.cpp b/src/DabMod.cpp index 5a4da9a..45f4d0a 100644 --- a/src/DabMod.cpp +++ b/src/DabMod.cpp @@ -493,8 +493,7 @@ int launch_modulator(int argc, char* argv[]) break; } - etiLog.level(info) << m.framecount << " DAB frames encoded"; - etiLog.level(info) << ((float)m.framecount * 0.024f) << " seconds encoded"; + etiLog.level(info) << m.framecount << " DAB frames, " << ((float)m.framecount * 0.024f) << " seconds encoded"; } etiLog.level(info) << "Terminating"; diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index ad4711c..5904824 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -188,10 +188,21 @@ Dexter::Dexter(SDRDeviceConfig& config) : if (!m_buffer) { throw std::runtime_error("Dexter: Cannot create IIO buffer."); } + +#warning "TODO underflow thread" + /* Disabled because it still provokes failed to push buffer Unknown error -110 + m_running = true; + m_underflow_read_thread = std::thread(&Dexter::underflow_read_process, this); + */ } Dexter::~Dexter() { + m_running = false; + if (m_underflow_read_thread.joinable()) { + m_underflow_read_thread.join(); + } + if (m_ctx) { if (m_dexter_dsp_tx) { iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", 0); @@ -271,7 +282,10 @@ double Dexter::get_bandwidth(void) const SDRDevice::RunStatistics Dexter::get_run_statistics(void) const { RunStatistics rs; - rs.num_underruns = underflows; + { + std::unique_lock lock(m_underflows_mutex); + rs.num_underruns = underflows; + } rs.num_overruns = 0; rs.num_late_packets = num_late; rs.num_frames_modulated = num_frames_modulated; @@ -374,19 +388,18 @@ void Dexter::transmit_frame(const struct FrameData& frame) etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_clks: " << get_iio_error(r); } - /* + const double margin = (double)((int64_t)frame_ts_clocks - pps_clks) / DSP_CLOCK; + etiLog.level(debug) << "Dexter: TS CLK " << ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK << " + " << m_clock_count_at_startup << " + " << (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS << " = " << frame_ts_clocks << " DELTA " << - frame_ts_clocks << " - " << pps_clks << " = " << - (double)((int64_t)frame_ts_clocks - pps_clks) / DSP_CLOCK; - */ + frame_ts_clocks << " - " << pps_clks << " = " << margin; - // Ensure we hand the frame over to HW at least 0.1s before timestamp - if (((int64_t)frame_ts_clocks - pps_clks) < (int64_t)DSP_CLOCK / 10) { - etiLog.level(warn) << "Skip frame short margin"; + // Ensure we hand the frame over to HW at least 0.2s before timestamp + if (margin < 0.2) { + etiLog.level(warn) << "Skip frame short margin " << margin; num_late++; return; } @@ -404,13 +417,6 @@ void Dexter::transmit_frame(const struct FrameData& frame) if (m_require_timestamp_refresh) { etiLog.level(debug) << "TIMESTAMP_STATE WAIT_FOR_UNDERRUN"; timestamp_state = timestamp_state_t::WAIT_FOR_UNDERRUN; - long long attr_value = 0; - int r = 0; - - if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "buffer_underflows0", &attr_value)) == 0) { - underflows = attr_value; - etiLog.level(debug) << "UNDERFLOWS CAPTURE " << underflows; - } } // DabMod::launch_modulator ensures we get int16_t IQ here @@ -424,33 +430,55 @@ void Dexter::transmit_frame(const struct FrameData& frame) memcpy(iio_buffer_start(m_buffer), frame.buf.data() + (i * buflen), buflen); ssize_t pushed = iio_buffer_push(m_buffer); if (pushed < 0) { - etiLog.level(error) << "Dexter: failed to push buffer " << get_iio_error(pushed); + etiLog.level(error) << "Dexter: failed to push buffer " << get_iio_error(pushed) << + " after " << num_buffers_pushed << " bufs"; + num_buffers_pushed = 0; etiLog.level(debug) << "TIMESTAMP_STATE REQUIRES_SET"; timestamp_state = timestamp_state_t::REQUIRES_SET; + break; } + num_buffers_pushed++; } num_frames_modulated++; } -#warning "We should update underflows all the time" - if (timestamp_state == timestamp_state_t::WAIT_FOR_UNDERRUN) { + { + std::unique_lock lock(m_underflows_mutex); + size_t u = underflows; + lock.unlock(); + + if (u != 0 and u != prev_underflows) { + etiLog.level(warn) << "Dexter: underflow! " << prev_underflows << " -> " << u; + if (timestamp_state == timestamp_state_t::WAIT_FOR_UNDERRUN) { + etiLog.level(debug) << "TIMESTAMP_STATE REQUIRES_SET"; + timestamp_state = timestamp_state_t::REQUIRES_SET; + } + } + + prev_underflows = u; + } +} + +void Dexter::underflow_read_process() +{ + set_thread_name("dexter_underflow"); + + while (m_running) { long long attr_value = 0; int r = 0; if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "buffer_underflows0", &attr_value)) == 0) { size_t underflows_new = attr_value; - etiLog.level(debug) << "UNDERFLOWS COMPARE " << underflows_new; + std::unique_lock lock(m_underflows_mutex); + etiLog.level(debug) << "UNDERFLOWS INC BY " << attr_value - (ssize_t)underflows; if (underflows_new != underflows and attr_value != 0) { - etiLog.level(warn) << "Dexter: underflow! " << underflows << " -> " << underflows_new; underflows = underflows_new; - if (timestamp_state == timestamp_state_t::WAIT_FOR_UNDERRUN) { - etiLog.level(debug) << "TIMESTAMP_STATE REQUIRES_SET"; - timestamp_state = timestamp_state_t::REQUIRES_SET; - } } } + this_thread::sleep_for(chrono::seconds(1)); } + m_running = false; } } // namespace Output diff --git a/src/output/Dexter.h b/src/output/Dexter.h index 3e9c34f..a3c827b 100644 --- a/src/output/Dexter.h +++ b/src/output/Dexter.h @@ -40,6 +40,8 @@ DESCRIPTION: #include #include #include +#include +#include #include "output/SDR.h" #include "ModPlugin.h" @@ -54,20 +56,20 @@ class Dexter : public Output::SDRDevice Dexter(SDRDeviceConfig& config); Dexter(const Dexter& other) = delete; Dexter& operator=(const Dexter& other) = delete; - ~Dexter(); + virtual ~Dexter(); virtual void tune(double lo_offset, double frequency) override; virtual double get_tx_freq(void) const override; virtual void set_txgain(double txgain) override; - virtual double get_txgain(void) const override; + virtual double get_txgain() const override; virtual void set_bandwidth(double bandwidth) override; - virtual double get_bandwidth(void) const override; + virtual double get_bandwidth() const override; virtual void transmit_frame(const struct FrameData& frame) override; - virtual RunStatistics get_run_statistics(void) const override; - virtual double get_real_secs(void) const override; + virtual RunStatistics get_run_statistics() const override; + virtual double get_real_secs() const override; virtual void set_rxgain(double rxgain) override; - virtual double get_rxgain(void) const override; + virtual double get_rxgain() const override; virtual size_t receive_frame( complexf *buf, size_t num_samples, @@ -75,10 +77,10 @@ class Dexter : public Output::SDRDevice double timeout_secs) override; // Return true if GPS and reference clock inputs are ok - virtual bool is_clk_source_ok(void) const override; - virtual const char* device_name(void) const override; + virtual bool is_clk_source_ok() const override; + virtual const char* device_name() const override; - virtual double get_temperature(void) const override; + virtual double get_temperature() const override; private: SDRDeviceConfig& m_conf; @@ -90,10 +92,19 @@ class Dexter : public Output::SDRDevice struct iio_channel* m_tx_channel = nullptr; struct iio_buffer *m_buffer = nullptr; + /* Underflows are counted in a separate thread */ + std::atomic m_running = ATOMIC_VAR_INIT(false); + std::thread m_underflow_read_thread; + void underflow_read_process(); + mutable std::mutex m_underflows_mutex; size_t underflows = 0; + + size_t prev_underflows = 0; size_t num_late = 0; size_t num_frames_modulated = 0; + size_t num_buffers_pushed = 0; + uint64_t m_utc_seconds_at_startup; uint64_t m_clock_count_at_startup = 0; uint64_t m_clock_count_frame = 0; diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index ae09acd..2b6700f 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -106,7 +106,6 @@ SDR::~SDR() void SDR::set_sample_size(size_t size) { - etiLog.level(debug) << "Setting sample size to " << size; m_size = size; } -- cgit v1.2.3 From 4f1f002ffad12144237352ad096353b8872171be Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 1 Feb 2023 13:48:11 +0100 Subject: Remove dexter timestamp_state_t --- src/output/Dexter.cpp | 131 ++++++++++++++++++++++++++------------------------ src/output/Dexter.h | 12 ++--- 2 files changed, 72 insertions(+), 71 deletions(-) (limited to 'src/output') diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index 5904824..cc10c57 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -175,7 +175,16 @@ Dexter::Dexter(SDRDeviceConfig& config) : etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = " << 0 << " : " << get_iio_error(r); } - // Prepare streams +#warning "TODO underflow thread" + /* Disabled because it still provokes failed to push buffer Unknown error -110 + m_running = true; + m_underflow_read_thread = std::thread(&Dexter::underflow_read_process, this); + */ +} + +void Dexter::channel_up() +{ + etiLog.level(debug) << "DEXTER CHANNEL_UP"; constexpr int CHANNEL_INDEX = 0; m_tx_channel = iio_device_get_channel(m_ad9957_tx0, CHANNEL_INDEX); if (m_tx_channel == nullptr) { @@ -185,17 +194,23 @@ Dexter::Dexter(SDRDeviceConfig& config) : iio_channel_enable(m_tx_channel); m_buffer = iio_device_create_buffer(m_ad9957_tx0, IIO_BUFFER_LEN/sizeof(int16_t), 0); - if (!m_buffer) { + if (not m_buffer) { throw std::runtime_error("Dexter: Cannot create IIO buffer."); } +} -#warning "TODO underflow thread" - /* Disabled because it still provokes failed to push buffer Unknown error -110 - m_running = true; - m_underflow_read_thread = std::thread(&Dexter::underflow_read_process, this); - */ +void Dexter::channel_down() +{ + iio_channel_disable(m_tx_channel); + + etiLog.level(debug) << "DEXTER CHANNEL_DOWN"; + if (m_buffer) { + iio_buffer_destroy(m_buffer); + m_buffer = nullptr; + } } + Dexter::~Dexter() { m_running = false; @@ -208,10 +223,6 @@ Dexter::~Dexter() iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", 0); } - if (m_buffer) { - iio_buffer_destroy(m_buffer); - } - iio_context_destroy(m_ctx); m_ctx = nullptr; } @@ -363,67 +374,66 @@ void Dexter::transmit_frame(const struct FrameData& frame) const bool require_timestamped_tx = (m_conf.enableSync and frame.ts.timestamp_valid); - if (not require_timestamped_tx) { - etiLog.level(debug) << "TIMESTAMP_STATE STREAMING 1"; - timestamp_state = timestamp_state_t::STREAMING; - } - else if (require_timestamped_tx and timestamp_state == timestamp_state_t::REQUIRES_SET) { - /* - uint64_t timeS = frame.ts.timestamp_sec; - etiLog.level(debug) << "Dexter: TS S " << timeS << " - " << m_utc_seconds_at_startup << " = " << - timeS - m_utc_seconds_at_startup; - */ - - // 10 because timestamp_pps is represented in 16.384 MHz clocks - constexpr uint64_t TIMESTAMP_PPS_PER_DSP_CLOCKS = DSP_CLOCK / 16384000; - uint64_t frame_ts_clocks = - // at second level - ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK + m_clock_count_at_startup + - // at subsecond level - (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS; - - long long pps_clks = 0; - int r; - if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "pps_clks", &pps_clks)) != 0) { - etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_clks: " << get_iio_error(r); - } + if (m_buffer == nullptr) { + if (require_timestamped_tx) { + /* + uint64_t timeS = frame.ts.timestamp_sec; + etiLog.level(debug) << "Dexter: TS S " << timeS << " - " << m_utc_seconds_at_startup << " = " << + timeS - m_utc_seconds_at_startup; + */ + + // 10 because timestamp_pps is represented in 16.384 MHz clocks + constexpr uint64_t TIMESTAMP_PPS_PER_DSP_CLOCKS = DSP_CLOCK / 16384000; + uint64_t frame_ts_clocks = + // at second level + ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK + m_clock_count_at_startup + + // at subsecond level + (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS; + + long long pps_clks = 0; + int r; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "pps_clks", &pps_clks)) != 0) { + etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_clks: " << get_iio_error(r); + } - const double margin = (double)((int64_t)frame_ts_clocks - pps_clks) / DSP_CLOCK; + const double margin = (double)((int64_t)frame_ts_clocks - pps_clks) / DSP_CLOCK; - etiLog.level(debug) << "Dexter: TS CLK " << - ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK << " + " << - m_clock_count_at_startup << " + " << - (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS << " = " << - frame_ts_clocks << " DELTA " << - frame_ts_clocks << " - " << pps_clks << " = " << margin; + etiLog.level(debug) << "Dexter: TS CLK " << + ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK << " + " << + m_clock_count_at_startup << " + " << + (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS << " = " << + frame_ts_clocks << " DELTA " << + frame_ts_clocks << " - " << pps_clks << " = " << margin; - // Ensure we hand the frame over to HW at least 0.2s before timestamp - if (margin < 0.2) { - etiLog.level(warn) << "Skip frame short margin " << margin; - num_late++; - return; - } + // Ensure we hand the frame over to HW at least 0.2s before timestamp + if (margin < 0.2) { + etiLog.level(warn) << "Skip frame short margin " << margin; + num_late++; + return; + } - if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", frame_ts_clocks)) != 0) { - etiLog.level(warn) << "Skip frame, failed to set dexter_dsp_tx.stream0_start_clks = " << frame_ts_clocks << " : " << get_iio_error(r); - num_late++; - return; + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", frame_ts_clocks)) != 0) { + etiLog.level(warn) << "Skip frame, failed to set dexter_dsp_tx.stream0_start_clks = " << frame_ts_clocks << " : " << get_iio_error(r); + num_late++; + return; + } } - timestamp_state = timestamp_state_t::STREAMING; - etiLog.level(debug) << "TIMESTAMP_STATE STREAMING 2"; + + channel_up(); } if (m_require_timestamp_refresh) { - etiLog.level(debug) << "TIMESTAMP_STATE WAIT_FOR_UNDERRUN"; - timestamp_state = timestamp_state_t::WAIT_FOR_UNDERRUN; + etiLog.level(debug) << "DEXTER REQUIRE REFRESH"; + channel_down(); + m_require_timestamp_refresh = false; } // DabMod::launch_modulator ensures we get int16_t IQ here //const size_t num_samples = frame.buf.size() / (2*sizeof(int16_t)); //const int16_t *buf = reinterpret_cast(frame.buf.data()); - if (timestamp_state == timestamp_state_t::STREAMING) { + if (m_buffer) { for (size_t i = 0; i < IIO_BUFFERS; i++) { constexpr size_t buflen = TRANSMISSION_FRAME_LEN / IIO_BUFFERS; @@ -433,8 +443,7 @@ void Dexter::transmit_frame(const struct FrameData& frame) etiLog.level(error) << "Dexter: failed to push buffer " << get_iio_error(pushed) << " after " << num_buffers_pushed << " bufs"; num_buffers_pushed = 0; - etiLog.level(debug) << "TIMESTAMP_STATE REQUIRES_SET"; - timestamp_state = timestamp_state_t::REQUIRES_SET; + channel_down(); break; } num_buffers_pushed++; @@ -449,10 +458,6 @@ void Dexter::transmit_frame(const struct FrameData& frame) if (u != 0 and u != prev_underflows) { etiLog.level(warn) << "Dexter: underflow! " << prev_underflows << " -> " << u; - if (timestamp_state == timestamp_state_t::WAIT_FOR_UNDERRUN) { - etiLog.level(debug) << "TIMESTAMP_STATE REQUIRES_SET"; - timestamp_state = timestamp_state_t::REQUIRES_SET; - } } prev_underflows = u; diff --git a/src/output/Dexter.h b/src/output/Dexter.h index a3c827b..36d0ef7 100644 --- a/src/output/Dexter.h +++ b/src/output/Dexter.h @@ -83,6 +83,10 @@ class Dexter : public Output::SDRDevice virtual double get_temperature() const override; private: + + void channel_up(); + void channel_down(); + SDRDeviceConfig& m_conf; struct iio_context* m_ctx = nullptr; @@ -108,14 +112,6 @@ class Dexter : public Output::SDRDevice uint64_t m_utc_seconds_at_startup; uint64_t m_clock_count_at_startup = 0; uint64_t m_clock_count_frame = 0; - - enum class timestamp_state_t { - REQUIRES_SET, - STREAMING, - WAIT_FOR_UNDERRUN, - }; - - timestamp_state_t timestamp_state = timestamp_state_t::REQUIRES_SET; }; } // namespace Output -- cgit v1.2.3 From 05d3e26409a8f62c7f55851390d61d953f59489a Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 21 Feb 2023 18:13:26 +0100 Subject: Change mod to output queue behaviour, fix SFN dexter + B200 --- lib/ThreadsafeQueue.h | 54 ++++++++++++++++++++++++++--- src/DabMod.cpp | 90 +++++++++++++++++++++++++++++------------------- src/DabModulator.cpp | 2 ++ src/TimestampDecoder.cpp | 1 + src/TimestampDecoder.h | 2 ++ src/output/Dexter.cpp | 31 ++++++++++------- src/output/SDR.cpp | 65 ++++++++++++++++++---------------- src/output/SDR.h | 1 + src/output/Soapy.cpp | 1 + src/output/UHD.cpp | 3 +- 10 files changed, 168 insertions(+), 82 deletions(-) (limited to 'src/output') diff --git a/lib/ThreadsafeQueue.h b/lib/ThreadsafeQueue.h index 815dfe0..8b385d6 100644 --- a/lib/ThreadsafeQueue.h +++ b/lib/ThreadsafeQueue.h @@ -2,7 +2,7 @@ Copyright (C) 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2018 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li An implementation for a threadsafe queue, depends on C++11 @@ -32,6 +32,7 @@ #include #include #include +#include /* This queue is meant to be used by two threads. One producer * that pushes elements into the queue, and one consumer that @@ -69,7 +70,6 @@ public: } size_t queue_size = the_queue.size(); lock.unlock(); - the_rx_notification.notify_one(); return queue_size; @@ -93,11 +93,57 @@ public: return queue_size; } + struct push_overflow_result { bool overflowed; size_t new_size; }; + + /* Push one element into the queue, and if queue is + * full remove one element from the other end. + * + * max_size == 0 is not allowed. + * + * returns the new queue size and a flag if overflow occurred. + */ + push_overflow_result push_overflow(T const& val, size_t max_size) + { + assert(max_size > 0); + std::unique_lock lock(the_mutex); + + bool overflow = false; + while (the_queue.size() >= max_size) { + overflow = true; + the_queue.pop(); + } + the_queue.push(val); + const size_t queue_size = the_queue.size(); + lock.unlock(); + + the_rx_notification.notify_one(); + + return {overflow, queue_size}; + } + + push_overflow_result push_overflow(T&& val, size_t max_size) + { + assert(max_size > 0); + std::unique_lock lock(the_mutex); + + bool overflow = false; + while (the_queue.size() >= max_size) { + overflow = true; + the_queue.pop(); + } + the_queue.emplace(std::move(val)); + const size_t queue_size = the_queue.size(); + lock.unlock(); + + the_rx_notification.notify_one(); + + return {overflow, queue_size}; + } + + /* Push one element into the queue, but wait until the * queue size goes below the threshold. * - * Notify waiting thread. - * * returns the new queue size. */ size_t push_wait_if_full(T const& val, size_t threshold) diff --git a/src/DabMod.cpp b/src/DabMod.cpp index 4b4fda0..e5436ad 100644 --- a/src/DabMod.cpp +++ b/src/DabMod.cpp @@ -25,6 +25,7 @@ along with ODR-DabMod. If not, see . */ +#include #ifdef HAVE_CONFIG_H # include "config.h" #endif @@ -342,6 +343,27 @@ int launch_modulator(int argc, char* argv[]) printModSettings(mod_settings); + { + // This is mostly useful on ARM systems where FFTW planning takes some time. If we do it here + // it will be done before the modulator starts up + etiLog.level(debug) << "Running FFTW planning..."; + constexpr size_t fft_size = 2048; // Transmission Mode I. If different, it'll recalculate on OfdmGenerator + // initialisation + auto *fft_in = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * fft_size); + auto *fft_out = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * fft_size); + if (fft_in == nullptr or fft_out == nullptr) { + throw std::runtime_error("FFTW malloc failed"); + } + fftwf_set_timelimit(2); + fftwf_plan plan = fftwf_plan_dft_1d(fft_size, fft_in, fft_out, FFTW_FORWARD, FFTW_MEASURE); + fftwf_destroy_plan(plan); + plan = fftwf_plan_dft_1d(fft_size, fft_in, fft_out, FFTW_BACKWARD, FFTW_MEASURE); + fftwf_destroy_plan(plan); + fftwf_free(fft_in); + fftwf_free(fft_out); + etiLog.level(debug) << "FFTW planning done."; + } + shared_ptr format_converter; if (mod_settings.useFileOutput and (mod_settings.fileOutputFormat == "s8" or @@ -563,48 +585,46 @@ static run_modulator_state_t run_modulator(const mod_settings_t& mod_settings, m ts = m.ediInput->ediReader.getTimestamp(); } - bool fct_good = false; - if (last_eti_fct == -1) { - if (fp != 0) { - // Do not start the flowgraph before we get to FP 0 - // to ensure all blocks are properly aligned. - if (m.ediInput) { - m.ediInput->ediReader.clearFrame(); - } - continue; - } - else { - fct_good = true; - } + // timestamp is good if we run unsynchronised, or if margin is sufficient + bool ts_good = not mod_settings.sdr_device_config.enableSync or + (ts.timestamp_valid and ts.offset_to_system_time() > 0.2); + + if (!ts_good) { + etiLog.level(warn) << "Modulator skipping frame " << fct << + " TS " << (ts.timestamp_valid ? "valid" : "invalid") << + " offset " << (ts.timestamp_valid ? ts.offset_to_system_time() : 0); } else { - const unsigned expected_fct = (last_eti_fct + 1) % 250; - if (fct == expected_fct) { - fct_good = true; + bool modulate = true; + if (last_eti_fct == -1) { + if (fp != 0) { + // Do not start the flowgraph before we get to FP 0 + // to ensure all blocks are properly aligned. + modulate = false; + } + else { + last_eti_fct = fct; + } } else { - etiLog.level(info) << "ETI FCT discontinuity, expected " << - expected_fct << " received " << fct; - if (m.ediInput) { - m.ediInput->ediReader.clearFrame(); + const unsigned expected_fct = (last_eti_fct + 1) % 250; + if (fct == expected_fct) { + last_eti_fct = fct; + } + else { + etiLog.level(info) << "ETI FCT discontinuity, expected " << + expected_fct << " received " << fct; + if (m.ediInput) { + m.ediInput->ediReader.clearFrame(); + } + return run_modulator_state_t::again; } - return run_modulator_state_t::again; } - } - // timestamp is good if we run unsynchronised, or if margin is insufficient - bool ts_good = not mod_settings.sdr_device_config.enableSync or - (ts.timestamp_valid and ts.offset_to_system_time() > 1); - - if (fct_good and ts_good) { - last_eti_fct = fct; - m.framecount++; - m.flowgraph->run(); - } - else { - etiLog.level(warn) << "Skipping frame " << - " TS " << (ts.timestamp_valid ? "valid" : "invalid") << - " offset " << (ts.timestamp_valid ? ts.offset_to_system_time() : 0); + if (modulate) { + m.framecount++; + m.flowgraph->run(); + } } if (m.ediInput) { diff --git a/src/DabModulator.cpp b/src/DabModulator.cpp index 1f16d1d..3d8bd46 100644 --- a/src/DabModulator.cpp +++ b/src/DabModulator.cpp @@ -126,6 +126,7 @@ int DabModulator::process(Buffer* dataOut) PDEBUG("DabModulator::process(dataOut: %p)\n", dataOut); if (not myFlowgraph) { + etiLog.level(debug) << "Setting up DabModulator..."; const unsigned mode = m_settings.dabMode; setMode(mode); @@ -364,6 +365,7 @@ int DabModulator::process(Buffer* dataOut) prev_plugin = p; } } + etiLog.level(debug) << "DabModulator set up."; } //////////////////////////////////////////////////////////////////// diff --git a/src/TimestampDecoder.cpp b/src/TimestampDecoder.cpp index 674f32c..6e97af6 100644 --- a/src/TimestampDecoder.cpp +++ b/src/TimestampDecoder.cpp @@ -110,6 +110,7 @@ frame_timestamp TimestampDecoder::getTimestamp() ts.fct = latestFCT; ts.fp = latestFP; + ts.timestamp_offset = timestamp_offset; ts.offset_changed = offset_changed; offset_changed = false; diff --git a/src/TimestampDecoder.h b/src/TimestampDecoder.h index 2793e02..597b777 100644 --- a/src/TimestampDecoder.h +++ b/src/TimestampDecoder.h @@ -42,6 +42,8 @@ struct frame_timestamp uint32_t timestamp_sec; // seconds in unix epoch uint32_t timestamp_pps; // In units of 1/16384000 s bool timestamp_valid = false; + + double timestamp_offset = 0.0; // copy of the configured modulator offset bool offset_changed = false; frame_timestamp& operator+=(const double& diff); diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index cc10c57..4e24cfb 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -46,6 +46,8 @@ namespace Output { static constexpr uint64_t DSP_CLOCK = 2048000uLL * 80; +static constexpr uint64_t IIO_TIMEOUT_MS = 1000; + static constexpr size_t TRANSMISSION_FRAME_LEN = (2656 + 76 * 2552) * 4; static constexpr size_t IIO_BUFFERS = 4; static constexpr size_t IIO_BUFFER_LEN = TRANSMISSION_FRAME_LEN / IIO_BUFFERS; @@ -53,7 +55,7 @@ static constexpr size_t IIO_BUFFER_LEN = TRANSMISSION_FRAME_LEN / IIO_BUFFERS; static string get_iio_error(int err) { char dst[256]; - iio_strerror(err, dst, sizeof(dst)); + iio_strerror(-err, dst, sizeof(dst)); return string(dst); } @@ -75,6 +77,11 @@ Dexter::Dexter(SDRDeviceConfig& config) : throw std::runtime_error("Dexter: Unable to create iio scan context"); } + int r; + if ((r = iio_context_set_timeout(m_ctx, IIO_TIMEOUT_MS)) != 0) { + etiLog.level(error) << "Failed to set IIO timeout " << get_iio_error(r); + } + m_dexter_dsp_tx = iio_context_find_device(m_ctx, "dexter_dsp_tx"); if (!m_dexter_dsp_tx) { throw std::runtime_error("Dexter: Unable to find dexter_dsp_tx iio device"); @@ -85,8 +92,6 @@ Dexter::Dexter(SDRDeviceConfig& config) : throw std::runtime_error("Dexter: Unable to find ad9957_tx0 iio device"); } - int r; - // TODO make DC offset configurable and add to RC if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "dc0", 0)) != 0) { etiLog.level(warn) << "Failed to set dexter_dsp_tx.dc0 = false: " << get_iio_error(r); @@ -97,11 +102,11 @@ Dexter::Dexter(SDRDeviceConfig& config) : } if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", 0)) != 0) { - etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0: " << get_iio_error(r); + etiLog.level(error) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0: " << get_iio_error(r); } if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", m_conf.txgain)) != 0) { - etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0: " << get_iio_error(r); + etiLog.level(error) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0: " << get_iio_error(r); } if (m_conf.sampleRate != 2048000) { @@ -396,28 +401,28 @@ void Dexter::transmit_frame(const struct FrameData& frame) etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_clks: " << get_iio_error(r); } - const double margin = (double)((int64_t)frame_ts_clocks - pps_clks) / DSP_CLOCK; + const double margin_s = (double)((int64_t)frame_ts_clocks - pps_clks) / DSP_CLOCK; - etiLog.level(debug) << "Dexter: TS CLK " << + etiLog.level(debug) << "DEXTER FCT " << frame.ts.fct << " TS CLK " << ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK << " + " << m_clock_count_at_startup << " + " << (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS << " = " << frame_ts_clocks << " DELTA " << - frame_ts_clocks << " - " << pps_clks << " = " << margin; + frame_ts_clocks << " - " << pps_clks << " = " << margin_s; - // Ensure we hand the frame over to HW at least 0.2s before timestamp - if (margin < 0.2) { - etiLog.level(warn) << "Skip frame short margin " << margin; + // Ensure we hand the frame over to HW with a bit of margin + if (margin_s < 0.1) { + etiLog.level(warn) << "Skip frame short margin " << margin_s; num_late++; return; } - if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", frame_ts_clocks)) != 0) { etiLog.level(warn) << "Skip frame, failed to set dexter_dsp_tx.stream0_start_clks = " << frame_ts_clocks << " : " << get_iio_error(r); num_late++; return; } + m_require_timestamp_refresh = false; } channel_up(); @@ -469,6 +474,7 @@ void Dexter::underflow_read_process() set_thread_name("dexter_underflow"); while (m_running) { + this_thread::sleep_for(chrono::seconds(1)); long long attr_value = 0; int r = 0; @@ -481,7 +487,6 @@ void Dexter::underflow_read_process() underflows = underflows_new; } } - this_thread::sleep_for(chrono::seconds(1)); } m_running = false; } diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index 2b6700f..0b3299a 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2022 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -46,17 +46,13 @@ using namespace std; namespace Output { -// Maximum number of frames that can wait in frames +// Maximum number of frames that can wait in frames, when not using synchronised transmission static constexpr size_t FRAMES_MAX_SIZE = 8; // If the timestamp is further in the future than // 100 seconds, abort static constexpr double TIMESTAMP_ABORT_FUTURE = 100; -// Add a delay to increase buffers when -// frames are too far in the future -static constexpr double TIMESTAMP_MARGIN_FUTURE = 0.5; - SDR::SDR(SDRDeviceConfig& config, std::shared_ptr device) : ModOutput(), ModMetadata(), RemoteControllable("sdr"), m_config(config), @@ -127,6 +123,10 @@ int SDR::process(Buffer *dataIn) meta_vec_t SDR::process_metadata(const meta_vec_t& metadataIn) { + double frame_duration_s = + chrono::duration_cast( + transmission_frame_duration(m_config.dabMode)).count() / 1000.0; + if (m_device and m_running) { FrameData frame; frame.buf = std::move(m_frame); @@ -163,9 +163,22 @@ meta_vec_t SDR::process_metadata(const meta_vec_t& metadataIn) m_config.sampleRate); } - size_t num_frames = m_queue.push_wait_if_full(frame, - FRAMES_MAX_SIZE); - etiLog.log(trace, "SDR,push %zu", num_frames); + + const auto max_size = m_config.enableSync ? + (frame.ts.timestamp_offset * 4.0) / frame_duration_s + : FRAMES_MAX_SIZE; + + auto r = m_queue.push_overflow(std::move(frame), max_size); + etiLog.log(trace, "SDR,push %d %zu", r.overflowed, r.new_size); + + if (r.overflowed) { + fprintf(stderr, "o"); + } + else { + fprintf(stderr, "."); + } + + num_queue_overflows += r.overflowed ? 1 : 0; } } else { @@ -186,16 +199,13 @@ void SDR::process_thread_entry() last_tx_time_initialised = false; - size_t last_num_underflows = 0; - size_t pop_prebuffering = FRAMES_MAX_SIZE; - m_running.store(true); try { while (m_running.load()) { struct FrameData frame; etiLog.log(trace, "SDR,wait"); - m_queue.wait_and_pop(frame, pop_prebuffering); + m_queue.wait_and_pop(frame); etiLog.log(trace, "SDR,pop"); if (m_running.load() == false) { @@ -204,19 +214,6 @@ void SDR::process_thread_entry() if (m_device) { handle_frame(frame); - - const auto rs = m_device->get_run_statistics(); - - /* Ensure we fill frames after every underrun and - * at startup to reduce underrun likelihood. */ - if (last_num_underflows < rs.num_underruns) { - pop_prebuffering = FRAMES_MAX_SIZE; - } - else { - pop_prebuffering = 1; - } - - last_num_underflows = rs.num_underruns; } } } @@ -302,6 +299,7 @@ void SDR::handle_frame(struct FrameData& frame) } if (frame.ts.offset_changed) { + etiLog.level(debug) << "TS offset changed"; m_device->require_timestamp_refresh(); } @@ -354,9 +352,19 @@ void SDR::handle_frame(struct FrameData& frame) " frame " << frame.ts.fct << ", tx_second " << tx_second << ", pps " << pps_offset; + m_device->require_timestamp_refresh(); return; } + etiLog.level(debug) << + "OutputSDR: Timestamp at FCT=" << frame.ts.fct << " offset: " << + std::fixed << + time_spec.get_real_secs() - device_time << + " (" << device_time << ")" + " frame " << frame.ts.fct << + ", tx_second " << tx_second << + ", pps " << pps_offset; + if (time_spec.get_real_secs() > device_time + TIMESTAMP_ABORT_FUTURE) { etiLog.level(error) << "OutputSDR: Timestamp way too far in the future at FCT=" << frame.ts.fct << " offset: " << @@ -367,9 +375,8 @@ void SDR::handle_frame(struct FrameData& frame) } if (m_config.muting) { - etiLog.log(info, - "OutputSDR: Muting FCT=%d requested", - frame.ts.fct); + etiLog.log(info, "OutputSDR: Muting FCT=%d requested", frame.ts.fct); + m_device->require_timestamp_refresh(); return; } diff --git a/src/output/SDR.h b/src/output/SDR.h index d7f7b46..5c3b599 100644 --- a/src/output/SDR.h +++ b/src/output/SDR.h @@ -88,6 +88,7 @@ class SDR : public ModOutput, public ModMetadata, public RemoteControllable { bool last_tx_time_initialised = false; uint32_t last_tx_second = 0; uint32_t last_tx_pps = 0; + size_t num_queue_overflows = 0; bool t_last_frame_initialised = false; std::chrono::steady_clock::time_point t_last_frame; diff --git a/src/output/Soapy.cpp b/src/output/Soapy.cpp index c2c5046..c2ae88a 100644 --- a/src/output/Soapy.cpp +++ b/src/output/Soapy.cpp @@ -349,6 +349,7 @@ void Soapy::transmit_frame(const struct FrameData& frame) SoapySDR::errToStr(ret_deact)); } m_tx_stream_active = false; + m_require_timestamp_refresh = false; } if (eob_because_muting) { diff --git a/src/output/UHD.cpp b/src/output/UHD.cpp index 6e38f73..6810249 100644 --- a/src/output/UHD.cpp +++ b/src/output/UHD.cpp @@ -350,6 +350,7 @@ void UHD::transmit_frame(const struct FrameData& frame) frame.ts.timestamp_valid and m_require_timestamp_refresh and samps_to_send <= usrp_max_num_samps ); + m_require_timestamp_refresh = false; //send a single packet size_t num_tx_samps = m_tx_stream->send( @@ -359,7 +360,7 @@ void UHD::transmit_frame(const struct FrameData& frame) num_acc_samps += num_tx_samps; - md_tx.time_spec += uhd::time_spec_t(0, num_tx_samps/m_conf.sampleRate); + md_tx.time_spec += uhd::time_spec_t::from_ticks(num_tx_samps, (double)m_conf.sampleRate); if (num_tx_samps == 0) { etiLog.log(warn, -- cgit v1.2.3 From 89e4bcbcba5883355a9b4777cea2bce0a1afd53d Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 3 Mar 2023 14:24:27 +0100 Subject: Dexter: calculate frame margin from system time --- src/output/BladeRF.cpp | 2 +- src/output/BladeRF.h | 2 +- src/output/Dexter.cpp | 24 ++++++++++-------------- src/output/Dexter.h | 4 ++-- src/output/Lime.cpp | 2 +- src/output/Lime.h | 2 +- src/output/SDR.cpp | 6 +++--- src/output/SDR.h | 4 ++-- src/output/SDRDevice.h | 2 +- src/output/Soapy.cpp | 2 +- src/output/Soapy.h | 2 +- src/output/UHD.cpp | 2 +- src/output/UHD.h | 2 +- 13 files changed, 26 insertions(+), 30 deletions(-) (limited to 'src/output') diff --git a/src/output/BladeRF.cpp b/src/output/BladeRF.cpp index dd48736..db29898 100755 --- a/src/output/BladeRF.cpp +++ b/src/output/BladeRF.cpp @@ -304,7 +304,7 @@ double BladeRF::get_temperature(void) const } -void BladeRF::transmit_frame(const struct FrameData &frame) // SC16 frames +void BladeRF::transmit_frame(struct FrameData&& frame) // SC16 frames { const size_t num_samples = frame.buf.size() / (2*sizeof(int16_t)); diff --git a/src/output/BladeRF.h b/src/output/BladeRF.h index e048daa..1a63fbf 100755 --- a/src/output/BladeRF.h +++ b/src/output/BladeRF.h @@ -74,7 +74,7 @@ class BladeRF : public Output::SDRDevice virtual double get_txgain(void) const override; virtual void set_bandwidth(double bandwidth) override; virtual double get_bandwidth(void) const override; - virtual void transmit_frame(const struct FrameData& frame) override; + virtual void transmit_frame(struct FrameData&& frame) override; virtual RunStatistics get_run_statistics(void) const override; virtual double get_real_secs(void) const override; diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index 4e24cfb..b6e6700 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2022 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -369,7 +369,7 @@ double Dexter::get_temperature(void) const return std::numeric_limits::quiet_NaN(); } -void Dexter::transmit_frame(const struct FrameData& frame) +void Dexter::transmit_frame(struct FrameData&& frame) { if (frame.buf.size() != TRANSMISSION_FRAME_LEN) { etiLog.level(debug) << "Dexter::transmit_frame Expected " << @@ -379,6 +379,8 @@ void Dexter::transmit_frame(const struct FrameData& frame) const bool require_timestamped_tx = (m_conf.enableSync and frame.ts.timestamp_valid); + const double margin_s = frame.ts.offset_to_system_time(); + if (m_buffer == nullptr) { if (require_timestamped_tx) { /* @@ -387,36 +389,28 @@ void Dexter::transmit_frame(const struct FrameData& frame) timeS - m_utc_seconds_at_startup; */ - // 10 because timestamp_pps is represented in 16.384 MHz clocks constexpr uint64_t TIMESTAMP_PPS_PER_DSP_CLOCKS = DSP_CLOCK / 16384000; + // TIMESTAMP_PPS_PER_DSP_CLOCKS=10 because timestamp_pps is represented in 16.384 MHz clocks uint64_t frame_ts_clocks = // at second level ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK + m_clock_count_at_startup + // at subsecond level (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS; - long long pps_clks = 0; - int r; - if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "pps_clks", &pps_clks)) != 0) { - etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_clks: " << get_iio_error(r); - } - - const double margin_s = (double)((int64_t)frame_ts_clocks - pps_clks) / DSP_CLOCK; - etiLog.level(debug) << "DEXTER FCT " << frame.ts.fct << " TS CLK " << ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK << " + " << m_clock_count_at_startup << " + " << (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS << " = " << - frame_ts_clocks << " DELTA " << - frame_ts_clocks << " - " << pps_clks << " = " << margin_s; + frame_ts_clocks << " DELTA " << margin_s; // Ensure we hand the frame over to HW with a bit of margin - if (margin_s < 0.1) { + if (margin_s < 0.2) { etiLog.level(warn) << "Skip frame short margin " << margin_s; num_late++; return; } + int r; if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", frame_ts_clocks)) != 0) { etiLog.level(warn) << "Skip frame, failed to set dexter_dsp_tx.stream0_start_clks = " << frame_ts_clocks << " : " << get_iio_error(r); num_late++; @@ -428,6 +422,8 @@ void Dexter::transmit_frame(const struct FrameData& frame) channel_up(); } + etiLog.level(debug) << "DEXTER TX " << frame.ts.fct << " TS margin " << margin_s; + if (m_require_timestamp_refresh) { etiLog.level(debug) << "DEXTER REQUIRE REFRESH"; channel_down(); diff --git a/src/output/Dexter.h b/src/output/Dexter.h index 36d0ef7..2bd63b1 100644 --- a/src/output/Dexter.h +++ b/src/output/Dexter.h @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2022 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -64,7 +64,7 @@ class Dexter : public Output::SDRDevice virtual double get_txgain() const override; virtual void set_bandwidth(double bandwidth) override; virtual double get_bandwidth() const override; - virtual void transmit_frame(const struct FrameData& frame) override; + virtual void transmit_frame(struct FrameData&& frame) override; virtual RunStatistics get_run_statistics() const override; virtual double get_real_secs() const override; diff --git a/src/output/Lime.cpp b/src/output/Lime.cpp index d3e4640..bf466c3 100644 --- a/src/output/Lime.cpp +++ b/src/output/Lime.cpp @@ -389,7 +389,7 @@ float Lime::get_fifo_fill_percent(void) const return m_last_fifo_fill_percent * 100; } -void Lime::transmit_frame(const struct FrameData &frame) +void Lime::transmit_frame(struct FrameData&& frame) { if (not m_device) throw runtime_error("Lime device not set up"); diff --git a/src/output/Lime.h b/src/output/Lime.h index a4603c0..95c5c48 100644 --- a/src/output/Lime.h +++ b/src/output/Lime.h @@ -66,7 +66,7 @@ class Lime : public Output::SDRDevice virtual double get_txgain(void) const override; virtual void set_bandwidth(double bandwidth) override; virtual double get_bandwidth(void) const override; - virtual void transmit_frame(const struct FrameData &frame) override; + virtual void transmit_frame(struct FrameData&& frame) override; virtual RunStatistics get_run_statistics(void) const override; virtual double get_real_secs(void) const override; diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index 0b3299a..e22617e 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -213,7 +213,7 @@ void SDR::process_thread_entry() } if (m_device) { - handle_frame(frame); + handle_frame(std::move(frame)); } } } @@ -260,7 +260,7 @@ void SDR::sleep_through_frame() t_last_frame += wait_time; } -void SDR::handle_frame(struct FrameData& frame) +void SDR::handle_frame(struct FrameData&& frame) { // Assumes m_device is valid @@ -386,7 +386,7 @@ void SDR::handle_frame(struct FrameData& frame) " TS " << frame.ts.to_string(); } - m_device->transmit_frame(frame); + m_device->transmit_frame(std::move(frame)); } // ======================================= diff --git a/src/output/SDR.h b/src/output/SDR.h index 5c3b599..eb0ed9d 100644 --- a/src/output/SDR.h +++ b/src/output/SDR.h @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2022 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -69,7 +69,7 @@ class SDR : public ModOutput, public ModMetadata, public RemoteControllable { private: void process_thread_entry(void); - void handle_frame(struct FrameData &frame); + void handle_frame(struct FrameData&& frame); void sleep_through_frame(void); SDRDeviceConfig& m_config; diff --git a/src/output/SDRDevice.h b/src/output/SDRDevice.h index f4b6c34..ffa1a3b 100644 --- a/src/output/SDRDevice.h +++ b/src/output/SDRDevice.h @@ -123,7 +123,7 @@ class SDRDevice { virtual double get_tx_freq(void) const = 0; virtual void set_txgain(double txgain) = 0; virtual double get_txgain(void) const = 0; - virtual void transmit_frame(const struct FrameData& frame) = 0; + virtual void transmit_frame(struct FrameData&& frame) = 0; virtual RunStatistics get_run_statistics(void) const = 0; virtual double get_real_secs(void) const = 0; virtual void set_rxgain(double rxgain) = 0; diff --git a/src/output/Soapy.cpp b/src/output/Soapy.cpp index c2ae88a..8c52546 100644 --- a/src/output/Soapy.cpp +++ b/src/output/Soapy.cpp @@ -272,7 +272,7 @@ double Soapy::get_temperature(void) const return std::numeric_limits::quiet_NaN(); } -void Soapy::transmit_frame(const struct FrameData& frame) +void Soapy::transmit_frame(struct FrameData&& frame) { if (not m_device) throw runtime_error("Soapy device not set up"); diff --git a/src/output/Soapy.h b/src/output/Soapy.h index ca2618b..f3e1ee2 100644 --- a/src/output/Soapy.h +++ b/src/output/Soapy.h @@ -65,7 +65,7 @@ class Soapy : public Output::SDRDevice virtual double get_txgain(void) const override; virtual void set_bandwidth(double bandwidth) override; virtual double get_bandwidth(void) const override; - virtual void transmit_frame(const struct FrameData& frame) override; + virtual void transmit_frame(struct FrameData&& frame) override; virtual RunStatistics get_run_statistics(void) const override; virtual double get_real_secs(void) const override; diff --git a/src/output/UHD.cpp b/src/output/UHD.cpp index 6810249..c325272 100644 --- a/src/output/UHD.cpp +++ b/src/output/UHD.cpp @@ -315,7 +315,7 @@ double UHD::get_bandwidth(void) const return m_usrp->get_tx_bandwidth(); } -void UHD::transmit_frame(const struct FrameData& frame) +void UHD::transmit_frame(struct FrameData&& frame) { const double tx_timeout = 20.0; const size_t sizeIn = frame.buf.size() / sizeof(complexf); diff --git a/src/output/UHD.h b/src/output/UHD.h index 4c1a4f0..164254c 100644 --- a/src/output/UHD.h +++ b/src/output/UHD.h @@ -79,7 +79,7 @@ class UHD : public Output::SDRDevice virtual double get_txgain(void) const override; virtual void set_bandwidth(double bandwidth) override; virtual double get_bandwidth(void) const override; - virtual void transmit_frame(const struct FrameData& frame) override; + virtual void transmit_frame(struct FrameData&& frame) override; virtual RunStatistics get_run_statistics(void) const override; virtual double get_real_secs(void) const override; -- cgit v1.2.3 From 4b3465e0a9e79f18398819f3bd0957efc9952928 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 3 Mar 2023 14:50:57 +0100 Subject: Remove duplicate stream0_start_clks = 0 on init --- src/output/Dexter.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/output') diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index b6e6700..aa0b7ad 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -101,12 +101,8 @@ Dexter::Dexter(SDRDeviceConfig& config) : etiLog.level(warn) << "Failed to set dexter_dsp_tx.dc1 = false: " << get_iio_error(r); } - if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", 0)) != 0) { - etiLog.level(error) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0: " << get_iio_error(r); - } - if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", m_conf.txgain)) != 0) { - etiLog.level(error) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0: " << get_iio_error(r); + etiLog.level(error) << "Failed to set dexter_dsp_tx.gain0 = 0: " << get_iio_error(r); } if (m_conf.sampleRate != 2048000) { @@ -447,6 +443,9 @@ void Dexter::transmit_frame(struct FrameData&& frame) channel_down(); break; } + else { + fprintf(stderr, "p"); + } num_buffers_pushed++; } num_frames_modulated++; -- cgit v1.2.3 From ed8e8bed00d5ad47a9d4798059fd0e3a8c3f7b8c Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 24 Mar 2023 10:31:24 +0100 Subject: Fix startup hiccup due to iio buffer allocation taking several seconds --- src/output/Dexter.cpp | 105 ++++++++++++++++++++++++++++++++++---------------- src/output/Dexter.h | 3 +- src/output/SDR.cpp | 2 +- 3 files changed, 75 insertions(+), 35 deletions(-) (limited to 'src/output') diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index aa0b7ad..93f1bb6 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -48,9 +48,9 @@ static constexpr uint64_t DSP_CLOCK = 2048000uLL * 80; static constexpr uint64_t IIO_TIMEOUT_MS = 1000; -static constexpr size_t TRANSMISSION_FRAME_LEN = (2656 + 76 * 2552) * 4; -static constexpr size_t IIO_BUFFERS = 4; -static constexpr size_t IIO_BUFFER_LEN = TRANSMISSION_FRAME_LEN / IIO_BUFFERS; +static constexpr size_t TRANSMISSION_FRAME_LEN_SAMPS = (2656 + 76 * 2552) * /* I+Q */ 2; +static constexpr size_t IIO_BUFFERS = 2; +static constexpr size_t IIO_BUFFER_LEN_SAMPS = TRANSMISSION_FRAME_LEN_SAMPS / IIO_BUFFERS; static string get_iio_error(int err) { @@ -73,7 +73,7 @@ Dexter::Dexter(SDRDeviceConfig& config) : etiLog.level(info) << "Dexter:Creating the device"; m_ctx = iio_create_local_context(); - if (!m_ctx) { + if (not m_ctx) { throw std::runtime_error("Dexter: Unable to create iio scan context"); } @@ -83,12 +83,12 @@ Dexter::Dexter(SDRDeviceConfig& config) : } m_dexter_dsp_tx = iio_context_find_device(m_ctx, "dexter_dsp_tx"); - if (!m_dexter_dsp_tx) { + if (not m_dexter_dsp_tx) { throw std::runtime_error("Dexter: Unable to find dexter_dsp_tx iio device"); } m_ad9957_tx0 = iio_context_find_device(m_ctx, "ad9957_tx0"); - if (!m_ad9957_tx0) { + if (not m_ad9957_tx0) { throw std::runtime_error("Dexter: Unable to find ad9957_tx0 iio device"); } @@ -101,10 +101,6 @@ Dexter::Dexter(SDRDeviceConfig& config) : etiLog.level(warn) << "Failed to set dexter_dsp_tx.dc1 = false: " << get_iio_error(r); } - if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", m_conf.txgain)) != 0) { - etiLog.level(error) << "Failed to set dexter_dsp_tx.gain0 = 0: " << get_iio_error(r); - } - if (m_conf.sampleRate != 2048000) { throw std::runtime_error("Dexter: Only 2048000 samplerate supported"); } @@ -176,16 +172,11 @@ Dexter::Dexter(SDRDeviceConfig& config) : etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = " << 0 << " : " << get_iio_error(r); } -#warning "TODO underflow thread" - /* Disabled because it still provokes failed to push buffer Unknown error -110 - m_running = true; - m_underflow_read_thread = std::thread(&Dexter::underflow_read_process, this); - */ -} + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", 0)) != 0) { + etiLog.level(error) << "Failed to set dexter_dsp_tx.gain0 = 0" << + " : " << get_iio_error(r); + } -void Dexter::channel_up() -{ - etiLog.level(debug) << "DEXTER CHANNEL_UP"; constexpr int CHANNEL_INDEX = 0; m_tx_channel = iio_device_get_channel(m_ad9957_tx0, CHANNEL_INDEX); if (m_tx_channel == nullptr) { @@ -194,21 +185,62 @@ void Dexter::channel_up() iio_channel_enable(m_tx_channel); - m_buffer = iio_device_create_buffer(m_ad9957_tx0, IIO_BUFFER_LEN/sizeof(int16_t), 0); + m_buffer = iio_device_create_buffer(m_ad9957_tx0, IIO_BUFFER_LEN_SAMPS, 0); if (not m_buffer) { throw std::runtime_error("Dexter: Cannot create IIO buffer."); } + + // Flush the FPGA FIFO + { + constexpr size_t buflen_samps = TRANSMISSION_FRAME_LEN_SAMPS / IIO_BUFFERS; + constexpr size_t buflen = buflen_samps * sizeof(int16_t); + + memset(iio_buffer_start(m_buffer), 0, buflen); + ssize_t pushed = iio_buffer_push(m_buffer); + if (pushed < 0) { + etiLog.level(error) << "Dexter: init push buffer " << get_iio_error(pushed); + } + this_thread::sleep_for(chrono::milliseconds(200)); + } + + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", m_conf.txgain)) != 0) { + etiLog.level(error) << "Failed to set dexter_dsp_tx.gain0 = " << m_conf.txgain << + " : " << get_iio_error(r); + } + +#warning "TODO underflow thread" + /* Disabled because it still provokes failed to push buffer Unknown error -110 + m_running = true; + m_underflow_read_thread = std::thread(&Dexter::underflow_read_process, this); + */ +} + +void Dexter::channel_up() +{ + int r; + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", m_conf.txgain)) != 0) { + etiLog.level(error) << "Failed to set dexter_dsp_tx.gain0 = " << m_conf.txgain << + " : " << get_iio_error(r); + } + + m_channel_is_up = true; + etiLog.level(debug) << "DEXTER CHANNEL_UP"; } void Dexter::channel_down() { - iio_channel_disable(m_tx_channel); + int r; + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", 0)) != 0) { + etiLog.level(error) << "Failed to set dexter_dsp_tx.gain0 = 0: " << get_iio_error(r); + } - etiLog.level(debug) << "DEXTER CHANNEL_DOWN"; - if (m_buffer) { - iio_buffer_destroy(m_buffer); - m_buffer = nullptr; + // This will flush out the FIFO + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", 0)) != 0) { + etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0 : " << get_iio_error(r); } + + m_channel_is_up = false; + etiLog.level(debug) << "DEXTER CHANNEL_DOWN"; } @@ -224,6 +256,11 @@ Dexter::~Dexter() iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", 0); } + if (m_buffer) { + iio_buffer_destroy(m_buffer); + m_buffer = nullptr; + } + iio_context_destroy(m_ctx); m_ctx = nullptr; } @@ -259,12 +296,12 @@ void Dexter::set_txgain(double txgain) { int r = 0; if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", txgain)) != 0) { - etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0: " << get_iio_error(r); + etiLog.level(warn) << "Failed to set dexter_dsp_tx.gain0 = 0: " << get_iio_error(r); } long long txgain_readback = 0; if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "gain0", &txgain_readback)) != 0) { - etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0: " << get_iio_error(r); + etiLog.level(warn) << "Failed to set dexter_dsp_tx.gain0 = 0: " << get_iio_error(r); } else { m_conf.txgain = txgain_readback; @@ -276,7 +313,7 @@ double Dexter::get_txgain(void) const long long txgain_readback = 0; int r = 0; if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "gain0", &txgain_readback)) != 0) { - etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0: " << get_iio_error(r); + etiLog.level(warn) << "Failed to set dexter_dsp_tx.gain0 = 0: " << get_iio_error(r); } return txgain_readback; } @@ -367,9 +404,10 @@ double Dexter::get_temperature(void) const void Dexter::transmit_frame(struct FrameData&& frame) { - if (frame.buf.size() != TRANSMISSION_FRAME_LEN) { + constexpr size_t frame_len_bytes = TRANSMISSION_FRAME_LEN_SAMPS * sizeof(int16_t); + if (frame.buf.size() != frame_len_bytes) { etiLog.level(debug) << "Dexter::transmit_frame Expected " << - TRANSMISSION_FRAME_LEN << " got " << frame.buf.size(); + frame_len_bytes << " got " << frame.buf.size(); throw std::runtime_error("Dexter: invalid buffer size"); } @@ -377,7 +415,7 @@ void Dexter::transmit_frame(struct FrameData&& frame) const double margin_s = frame.ts.offset_to_system_time(); - if (m_buffer == nullptr) { + if (not m_channel_is_up) { if (require_timestamped_tx) { /* uint64_t timeS = frame.ts.timestamp_sec; @@ -430,9 +468,10 @@ void Dexter::transmit_frame(struct FrameData&& frame) //const size_t num_samples = frame.buf.size() / (2*sizeof(int16_t)); //const int16_t *buf = reinterpret_cast(frame.buf.data()); - if (m_buffer) { + if (m_channel_is_up) { for (size_t i = 0; i < IIO_BUFFERS; i++) { - constexpr size_t buflen = TRANSMISSION_FRAME_LEN / IIO_BUFFERS; + constexpr size_t buflen_samps = TRANSMISSION_FRAME_LEN_SAMPS / IIO_BUFFERS; + constexpr size_t buflen = buflen_samps * sizeof(int16_t); memcpy(iio_buffer_start(m_buffer), frame.buf.data() + (i * buflen), buflen); ssize_t pushed = iio_buffer_push(m_buffer); diff --git a/src/output/Dexter.h b/src/output/Dexter.h index 2bd63b1..2f0524e 100644 --- a/src/output/Dexter.h +++ b/src/output/Dexter.h @@ -83,10 +83,11 @@ class Dexter : public Output::SDRDevice virtual double get_temperature() const override; private: - void channel_up(); void channel_down(); + bool m_channel_is_up = false; + SDRDeviceConfig& m_conf; struct iio_context* m_ctx = nullptr; diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index e22617e..f53197e 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -165,7 +165,7 @@ meta_vec_t SDR::process_metadata(const meta_vec_t& metadataIn) const auto max_size = m_config.enableSync ? - (frame.ts.timestamp_offset * 4.0) / frame_duration_s + (frame.ts.timestamp_offset * 400.0) / frame_duration_s : FRAMES_MAX_SIZE; auto r = m_queue.push_overflow(std::move(frame), max_size); -- cgit v1.2.3 From d7fe3627a5f5ac1376af09a851d59c5c7919f29e Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 24 Mar 2023 10:50:09 +0100 Subject: Remove debugging printf --- src/output/Dexter.cpp | 5 ----- src/output/SDR.cpp | 16 ---------------- 2 files changed, 21 deletions(-) (limited to 'src/output') diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index 93f1bb6..d892486 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -456,8 +456,6 @@ void Dexter::transmit_frame(struct FrameData&& frame) channel_up(); } - etiLog.level(debug) << "DEXTER TX " << frame.ts.fct << " TS margin " << margin_s; - if (m_require_timestamp_refresh) { etiLog.level(debug) << "DEXTER REQUIRE REFRESH"; channel_down(); @@ -482,9 +480,6 @@ void Dexter::transmit_frame(struct FrameData&& frame) channel_down(); break; } - else { - fprintf(stderr, "p"); - } num_buffers_pushed++; } num_frames_modulated++; diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index f53197e..726fb94 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -171,13 +171,6 @@ meta_vec_t SDR::process_metadata(const meta_vec_t& metadataIn) auto r = m_queue.push_overflow(std::move(frame), max_size); etiLog.log(trace, "SDR,push %d %zu", r.overflowed, r.new_size); - if (r.overflowed) { - fprintf(stderr, "o"); - } - else { - fprintf(stderr, "."); - } - num_queue_overflows += r.overflowed ? 1 : 0; } } @@ -356,15 +349,6 @@ void SDR::handle_frame(struct FrameData&& frame) return; } - etiLog.level(debug) << - "OutputSDR: Timestamp at FCT=" << frame.ts.fct << " offset: " << - std::fixed << - time_spec.get_real_secs() - device_time << - " (" << device_time << ")" - " frame " << frame.ts.fct << - ", tx_second " << tx_second << - ", pps " << pps_offset; - if (time_spec.get_real_secs() > device_time + TIMESTAMP_ABORT_FUTURE) { etiLog.level(error) << "OutputSDR: Timestamp way too far in the future at FCT=" << frame.ts.fct << " offset: " << -- cgit v1.2.3 From fd46d43f51796a6bbdd629e08535bacbc853f283 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 29 Mar 2023 16:30:06 +0200 Subject: Replace RunStatistics to permit representation of SDR device specific stats --- src/output/BladeRF.cpp | 22 +++---- src/output/BladeRF.h | 9 +-- src/output/Dexter.cpp | 153 ++++++++++++++++++++++++++++++------------------- src/output/Dexter.h | 7 ++- src/output/Lime.cpp | 31 +++++----- src/output/Lime.h | 8 +-- src/output/SDR.cpp | 77 +++++++++++-------------- src/output/SDRDevice.h | 22 +++---- src/output/Soapy.cpp | 17 +++--- src/output/Soapy.h | 6 +- src/output/UHD.cpp | 24 ++++---- src/output/UHD.h | 4 +- 12 files changed, 197 insertions(+), 183 deletions(-) (limited to 'src/output') diff --git a/src/output/BladeRF.cpp b/src/output/BladeRF.cpp index db29898..2dd7176 100755 --- a/src/output/BladeRF.cpp +++ b/src/output/BladeRF.cpp @@ -239,13 +239,10 @@ double BladeRF::get_bandwidth(void) const return (double)bw; } -SDRDevice::RunStatistics BladeRF::get_run_statistics(void) const +SDRDevice::run_statistics_t BladeRF::get_run_statistics(void) const { - RunStatistics rs; - rs.num_underruns = underflows; - rs.num_overruns = overflows; - rs.num_late_packets = late_packets; - rs.num_frames_modulated = num_frames_modulated; + run_statistics_t rs; + rs["frames"] = num_frames_modulated; return rs; } @@ -287,23 +284,22 @@ const char *BladeRF::device_name(void) const return "BladeRF"; } -double BladeRF::get_temperature(void) const +std::optional BladeRF::get_temperature(void) const { if (not m_device) throw runtime_error("BladeRF device not set up"); float temp = 0.0; - int status = bladerf_get_rfic_temperature(m_device, &temp); - if (status < 0) - { + if (status >= 0) { + return (double)temp; + } + else { etiLog.level(error) << "Error getting BladeRF temperature: %s " << bladerf_strerror(status); + return std::nullopt; } - - return (double)temp; } - void BladeRF::transmit_frame(struct FrameData&& frame) // SC16 frames { const size_t num_samples = frame.buf.size() / (2*sizeof(int16_t)); diff --git a/src/output/BladeRF.h b/src/output/BladeRF.h index 1a63fbf..eb3e58b 100755 --- a/src/output/BladeRF.h +++ b/src/output/BladeRF.h @@ -75,7 +75,7 @@ class BladeRF : public Output::SDRDevice virtual void set_bandwidth(double bandwidth) override; virtual double get_bandwidth(void) const override; virtual void transmit_frame(struct FrameData&& frame) override; - virtual RunStatistics get_run_statistics(void) const override; + virtual run_statistics_t get_run_statistics(void) const override; virtual double get_real_secs(void) const override; virtual void set_rxgain(double rxgain) override; @@ -90,7 +90,7 @@ class BladeRF : public Output::SDRDevice virtual bool is_clk_source_ok(void) const override; virtual const char* device_name(void) const override; - virtual double get_temperature(void) const override; + virtual std::optional get_temperature(void) const override; private: @@ -99,12 +99,7 @@ class BladeRF : public Output::SDRDevice bladerf_channel m_channel = BLADERF_CHANNEL_TX(0); // channel TX0 //struct bladerf_stream* m_stream; /* used for asynchronous api */ - size_t underflows = 0; - size_t overflows = 0; - size_t late_packets = 0; size_t num_frames_modulated = 0; - //size_t num_underflows_previous = 0; - //size_t num_late_packets_previous = 0; }; } // namespace Output diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index d892486..dd882f2 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -74,7 +74,7 @@ Dexter::Dexter(SDRDeviceConfig& config) : m_ctx = iio_create_local_context(); if (not m_ctx) { - throw std::runtime_error("Dexter: Unable to create iio scan context"); + throw std::runtime_error("Dexter: Unable to create iio context"); } int r; @@ -94,11 +94,11 @@ Dexter::Dexter(SDRDeviceConfig& config) : // TODO make DC offset configurable and add to RC if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "dc0", 0)) != 0) { - etiLog.level(warn) << "Failed to set dexter_dsp_tx.dc0 = false: " << get_iio_error(r); + throw std::runtime_error("Failed to set dexter_dsp_tx.dc0 = false: " + get_iio_error(r)); } if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "dc1", 0)) != 0) { - etiLog.level(warn) << "Failed to set dexter_dsp_tx.dc1 = false: " << get_iio_error(r); + throw std::runtime_error("Failed to set dexter_dsp_tx.dc1 = false: " + get_iio_error(r)); } if (m_conf.sampleRate != 2048000) { @@ -167,14 +167,17 @@ Dexter::Dexter(SDRDeviceConfig& config) : m_utc_seconds_at_startup = time_now.tv_sec; m_clock_count_at_startup = pps_clks2; - // Reset start_clks - if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", 0)) != 0) { - etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = " << 0 << " : " << get_iio_error(r); + // The FIFO should not contain data, but setting gain=0 before setting start_clks to zero is an additional security + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", 0)) != 0) { + throw std::runtime_error("Failed to set dexter_dsp_tx.gain0 = 0 : " + get_iio_error(r)); } - if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", 0)) != 0) { - etiLog.level(error) << "Failed to set dexter_dsp_tx.gain0 = 0" << - " : " << get_iio_error(r); + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_flush_fifo_trigger", 1)) != 0) { + throw std::runtime_error("Failed to set dexter_dsp_tx.stream0_flush_fifo_trigger = 1 : " + get_iio_error(r)); + } + + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", 0)) != 0) { + throw std::runtime_error("Failed to set dexter_dsp_tx.stream0_start_clks = 0 : " + get_iio_error(r)); } constexpr int CHANNEL_INDEX = 0; @@ -208,11 +211,8 @@ Dexter::Dexter(SDRDeviceConfig& config) : " : " << get_iio_error(r); } -#warning "TODO underflow thread" - /* Disabled because it still provokes failed to push buffer Unknown error -110 m_running = true; m_underflow_read_thread = std::thread(&Dexter::underflow_read_process, this); - */ } void Dexter::channel_up() @@ -261,9 +261,18 @@ Dexter::~Dexter() m_buffer = nullptr; } + if (m_tx_channel) { + iio_channel_disable(m_tx_channel); + } + iio_context_destroy(m_ctx); m_ctx = nullptr; } + + if (m_underflow_ctx) { + iio_context_destroy(m_underflow_ctx); + m_underflow_ctx = nullptr; + } } void Dexter::tune(double lo_offset, double frequency) @@ -296,12 +305,12 @@ void Dexter::set_txgain(double txgain) { int r = 0; if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", txgain)) != 0) { - etiLog.level(warn) << "Failed to set dexter_dsp_tx.gain0 = 0: " << get_iio_error(r); + etiLog.level(warn) << "Failed to set dexter_dsp_tx.gain0 = " << txgain << ": " << get_iio_error(r); } long long txgain_readback = 0; if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "gain0", &txgain_readback)) != 0) { - etiLog.level(warn) << "Failed to set dexter_dsp_tx.gain0 = 0: " << get_iio_error(r); + etiLog.level(warn) << "Failed to read dexter_dsp_tx.gain0: " << get_iio_error(r); } else { m_conf.txgain = txgain_readback; @@ -313,14 +322,14 @@ double Dexter::get_txgain(void) const long long txgain_readback = 0; int r = 0; if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "gain0", &txgain_readback)) != 0) { - etiLog.level(warn) << "Failed to set dexter_dsp_tx.gain0 = 0: " << get_iio_error(r); + etiLog.level(warn) << "Failed to read dexter_dsp_tx.gain0: " << get_iio_error(r); } return txgain_readback; } void Dexter::set_bandwidth(double bandwidth) { - // TODO + return; } double Dexter::get_bandwidth(void) const @@ -328,38 +337,51 @@ double Dexter::get_bandwidth(void) const return 0; } -SDRDevice::RunStatistics Dexter::get_run_statistics(void) const +SDRDevice::run_statistics_t Dexter::get_run_statistics(void) const { - RunStatistics rs; + run_statistics_t rs; { - std::unique_lock lock(m_underflows_mutex); - rs.num_underruns = underflows; + std::unique_lock lock(m_attr_thread_mutex); + rs["underruns"] = underflows; + } + rs["overruns"] = 0; + rs["late_packets"] = num_late; + rs["frames"] = num_frames_modulated; + + long long clks = 0; + int r = 0; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "clks", &clks)) == 0) { + rs["clks"] = (size_t)clks; + } + else { + rs["clks"] = (ssize_t)-1; + etiLog.level(error) << "Failed to get dexter_dsp_tx.clks: " << get_iio_error(r); + } + + long long fifo_not_empty_clks = 0; + + if ((r = iio_device_attr_read_longlong( + m_dexter_dsp_tx, "stream0_fifo_not_empty_clks", &fifo_not_empty_clks)) == 0) { + rs["fifo_not_empty_clks"] = (size_t)fifo_not_empty_clks; + } + else { + rs["fifo_not_empty_clks"] = (ssize_t)-1; + etiLog.level(error) << "Failed to get dexter_dsp_tx.fifo_not_empty_clks: " << get_iio_error(r); } - rs.num_overruns = 0; - rs.num_late_packets = num_late; - rs.num_frames_modulated = num_frames_modulated; return rs; } double Dexter::get_real_secs(void) const { - struct timespec time_now; - fill_time(&time_now); - return (double)time_now.tv_sec + time_now.tv_nsec / 1000000000.0; - - /* We don't use actual device time, because we only have clock counter on pps edge available, not - * current clock counter. */ -#if 0 - long long pps_clks = 0; + long long clks = 0; int r = 0; - if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "pps_clks", &pps_clks)) != 0) { - etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_clks: " << get_iio_error(r); + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "clks", &clks)) != 0) { + etiLog.level(error) << "Failed to get dexter_dsp_tx.clks: " << get_iio_error(r); throw std::runtime_error("Dexter: Cannot read IIO attribute"); } - return (double)m_utc_seconds_at_startup + (double)(pps_clks - m_clock_count_at_startup) / (double)DSP_CLOCK; -#endif + return (double)m_utc_seconds_at_startup + (double)(clks - m_clock_count_at_startup) / (double)DSP_CLOCK; } void Dexter::set_rxgain(double rxgain) @@ -395,11 +417,10 @@ const char* Dexter::device_name(void) const return "Dexter"; } -double Dexter::get_temperature(void) const +std::optional Dexter::get_temperature(void) const { - // TODO - // XADC contains temperature, but value is weird - return std::numeric_limits::quiet_NaN(); + // TODO XADC contains temperature, but value is weird + return std::nullopt; } void Dexter::transmit_frame(struct FrameData&& frame) @@ -413,29 +434,32 @@ void Dexter::transmit_frame(struct FrameData&& frame) const bool require_timestamped_tx = (m_conf.enableSync and frame.ts.timestamp_valid); - const double margin_s = frame.ts.offset_to_system_time(); - if (not m_channel_is_up) { if (require_timestamped_tx) { - /* - uint64_t timeS = frame.ts.timestamp_sec; - etiLog.level(debug) << "Dexter: TS S " << timeS << " - " << m_utc_seconds_at_startup << " = " << - timeS - m_utc_seconds_at_startup; - */ - constexpr uint64_t TIMESTAMP_PPS_PER_DSP_CLOCKS = DSP_CLOCK / 16384000; // TIMESTAMP_PPS_PER_DSP_CLOCKS=10 because timestamp_pps is represented in 16.384 MHz clocks - uint64_t frame_ts_clocks = + uint64_t frame_start_clocks = // at second level ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK + m_clock_count_at_startup + // at subsecond level (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS; + const double margin_s = frame.ts.offset_to_system_time(); + + long long clks = 0; + int r = 0; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "clks", &clks)) != 0) { + etiLog.level(error) << "Failed to get dexter_dsp_tx.clks: " << get_iio_error(r); + throw std::runtime_error("Dexter: Cannot read IIO attribute"); + } + + const double margin_device_s = (double)(frame_start_clocks - clks) / DSP_CLOCK; + etiLog.level(debug) << "DEXTER FCT " << frame.ts.fct << " TS CLK " << ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK << " + " << m_clock_count_at_startup << " + " << (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS << " = " << - frame_ts_clocks << " DELTA " << margin_s; + frame_start_clocks << " DELTA " << margin_s << " " << margin_device_s; // Ensure we hand the frame over to HW with a bit of margin if (margin_s < 0.2) { @@ -444,9 +468,8 @@ void Dexter::transmit_frame(struct FrameData&& frame) return; } - int r; - if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", frame_ts_clocks)) != 0) { - etiLog.level(warn) << "Skip frame, failed to set dexter_dsp_tx.stream0_start_clks = " << frame_ts_clocks << " : " << get_iio_error(r); + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", frame_start_clocks)) != 0) { + etiLog.level(warn) << "Skip frame, failed to set dexter_dsp_tx.stream0_start_clks = " << frame_start_clocks << " : " << get_iio_error(r); num_late++; return; } @@ -486,7 +509,7 @@ void Dexter::transmit_frame(struct FrameData&& frame) } { - std::unique_lock lock(m_underflows_mutex); + std::unique_lock lock(m_attr_thread_mutex); size_t u = underflows; lock.unlock(); @@ -500,19 +523,29 @@ void Dexter::transmit_frame(struct FrameData&& frame) void Dexter::underflow_read_process() { + m_underflow_ctx = iio_create_local_context(); + if (not m_underflow_ctx) { + throw std::runtime_error("Dexter: Unable to create iio context for underflow"); + } + + auto dexter_dsp_tx = iio_context_find_device(m_ctx, "dexter_dsp_tx"); + if (not dexter_dsp_tx) { + throw std::runtime_error("Dexter: Unable to find dexter_dsp_tx iio device"); + } + set_thread_name("dexter_underflow"); while (m_running) { this_thread::sleep_for(chrono::seconds(1)); - long long attr_value = 0; - int r = 0; + long long underflows_attr = 0; + + int r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "buffer_underflows0", &underflows_attr); - if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "buffer_underflows0", &attr_value)) == 0) { - size_t underflows_new = attr_value; + if (r == 0) { + size_t underflows_new = underflows_attr; - std::unique_lock lock(m_underflows_mutex); - etiLog.level(debug) << "UNDERFLOWS INC BY " << attr_value - (ssize_t)underflows; - if (underflows_new != underflows and attr_value != 0) { + std::unique_lock lock(m_attr_thread_mutex); + if (underflows_new != underflows and underflows_attr != 0) { underflows = underflows_new; } } diff --git a/src/output/Dexter.h b/src/output/Dexter.h index 2f0524e..7925de7 100644 --- a/src/output/Dexter.h +++ b/src/output/Dexter.h @@ -65,7 +65,7 @@ class Dexter : public Output::SDRDevice virtual void set_bandwidth(double bandwidth) override; virtual double get_bandwidth() const override; virtual void transmit_frame(struct FrameData&& frame) override; - virtual RunStatistics get_run_statistics() const override; + virtual run_statistics_t get_run_statistics() const override; virtual double get_real_secs() const override; virtual void set_rxgain(double rxgain) override; @@ -80,7 +80,7 @@ class Dexter : public Output::SDRDevice virtual bool is_clk_source_ok() const override; virtual const char* device_name() const override; - virtual double get_temperature() const override; + virtual std::optional get_temperature() const override; private: void channel_up(); @@ -98,10 +98,11 @@ class Dexter : public Output::SDRDevice struct iio_buffer *m_buffer = nullptr; /* Underflows are counted in a separate thread */ + struct iio_context* m_underflow_ctx = nullptr; std::atomic m_running = ATOMIC_VAR_INIT(false); std::thread m_underflow_read_thread; void underflow_read_process(); - mutable std::mutex m_underflows_mutex; + mutable std::mutex m_attr_thread_mutex; size_t underflows = 0; size_t prev_underflows = 0; diff --git a/src/output/Lime.cpp b/src/output/Lime.cpp index bf466c3..83c54ad 100644 --- a/src/output/Lime.cpp +++ b/src/output/Lime.cpp @@ -323,13 +323,14 @@ double Lime::get_bandwidth(void) const return bw; } -SDRDevice::RunStatistics Lime::get_run_statistics(void) const +SDRDevice::run_statistics_t Lime::get_run_statistics(void) const { - RunStatistics rs; - rs.num_underruns = underflows; - rs.num_overruns = overflows; - rs.num_late_packets = late_packets; - rs.num_frames_modulated = num_frames_modulated; + run_statistics_t rs; + rs["underruns"] = underflows; + rs["overruns"] = overflows; + rs["dropped_packets"] = dropped_packets; + rs["frames"] = num_frames_modulated; + rs["fifo_fill"] = m_last_fifo_fill_percent * 100; return rs; } @@ -371,23 +372,21 @@ const char *Lime::device_name(void) const return "Lime"; } -double Lime::get_temperature(void) const +std::optional Lime::get_temperature(void) const { if (not m_device) throw runtime_error("Lime device not set up"); - float_type temp = numeric_limits::quiet_NaN(); - if (LMS_GetChipTemperature(m_device, 0, &temp) < 0) - { + float_type temp = 0; + if (LMS_GetChipTemperature(m_device, 0, &temp) >= 0) { + return temp; + } + else { etiLog.level(error) << "Error getting LimeSDR temperature: %s " << LMS_GetLastErrorMessage(); + return std::nullopt; } - return temp; } -float Lime::get_fifo_fill_percent(void) const -{ - return m_last_fifo_fill_percent * 100; -} void Lime::transmit_frame(struct FrameData&& frame) { @@ -411,7 +410,7 @@ void Lime::transmit_frame(struct FrameData&& frame) LMS_GetStreamStatus(&m_tx_stream, &LimeStatus); overflows += LimeStatus.overrun; underflows += LimeStatus.underrun; - late_packets += LimeStatus.droppedPackets; + dropped_packets += LimeStatus.droppedPackets; #ifdef LIMEDEBUG etiLog.level(info) << LimeStatus.fifoFilledCount << "/" << LimeStatus.fifoSize << ":" << numSamples << "Rate" << LimeStatus.linkRate / (2 * 2.0); diff --git a/src/output/Lime.h b/src/output/Lime.h index 95c5c48..e09e82d 100644 --- a/src/output/Lime.h +++ b/src/output/Lime.h @@ -67,7 +67,7 @@ class Lime : public Output::SDRDevice virtual void set_bandwidth(double bandwidth) override; virtual double get_bandwidth(void) const override; virtual void transmit_frame(struct FrameData&& frame) override; - virtual RunStatistics get_run_statistics(void) const override; + virtual run_statistics_t get_run_statistics(void) const override; virtual double get_real_secs(void) const override; virtual void set_rxgain(double rxgain) override; @@ -82,8 +82,7 @@ class Lime : public Output::SDRDevice virtual bool is_clk_source_ok(void) const override; virtual const char *device_name(void) const override; - virtual double get_temperature(void) const override; - virtual float get_fifo_fill_percent(void) const; + virtual std::optional get_temperature(void) const override; private: SDRDeviceConfig &m_conf; @@ -95,11 +94,10 @@ class Lime : public Output::SDRDevice std::vector interpolatebuf; std::vector m_i16samples; std::atomic m_last_fifo_fill_percent = ATOMIC_VAR_INIT(0); - size_t underflows = 0; size_t overflows = 0; - size_t late_packets = 0; + size_t dropped_packets = 0; size_t num_frames_modulated = 0; }; diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index 726fb94..a4f5873 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -87,6 +87,11 @@ SDR::SDR(SDRDeviceConfig& config, std::shared_ptr device) : RC_ADD_PARAMETER(fifo_fill, "A value representing the Lime FIFO fullness [percent]"); } #endif // HAVE_LIMESDR + +#ifdef HAVE_DEXTER + RC_ADD_PARAMETER(clks, "DEXTER internal clk counter value"); + RC_ADD_PARAMETER(fifo_not_empty_clks, "DEXTER internal clk counter value when FIFO was last empty"); +#endif // HAVE_DEXTER } SDR::~SDR() @@ -402,6 +407,7 @@ void SDR::set_parameter(const string& parameter, const string& value) ss >> m_config.muting; } else if (parameter == "underruns" or + parameter == "overruns" or parameter == "latepackets" or parameter == "frames" or parameter == "gpsdo_num_sv" or @@ -440,55 +446,40 @@ const string SDR::get_parameter(const string& parameter) const if (not m_device) { throw ParameterError("OutputSDR has no device"); } - const double temp = m_device->get_temperature(); - if (std::isnan(temp)) { - throw ParameterError("Temperature not available"); + const std::optional temp = m_device->get_temperature(); + if (temp) { + ss << *temp; } else { - ss << temp; + throw ParameterError("Temperature not available"); } } - else if (parameter == "underruns" or - parameter == "latepackets" or - parameter == "frames" ) { - if (not m_device) { - throw ParameterError("OutputSDR has no device"); - } - const auto stat = m_device->get_run_statistics(); - - if (parameter == "underruns") { - ss << stat.num_underruns; - } - else if (parameter == "latepackets") { - ss << stat.num_late_packets; - } - else if (parameter == "frames") { - ss << stat.num_frames_modulated; + else { + if (m_device) { + const auto stat = m_device->get_run_statistics(); + try { + const auto& value = stat.at(parameter); + if (std::holds_alternative(value)) { + ss << std::get(value); + } + else if (std::holds_alternative(value)) { + ss << std::get(value); + } + else if (std::holds_alternative(value)) { + ss << std::get(value); + } + else if (std::holds_alternative(value)) { + ss << (std::get(value) ? 1 : 0); + } + else { + throw std::logic_error("variant alternative not handled"); + } + return ss.str(); + } + catch (const std::out_of_range&) { + } } - } - else if (parameter == "gpsdo_num_sv") { - const auto stat = m_device->get_run_statistics(); - ss << stat.gpsdo_num_sv; - } - else if (parameter == "gpsdo_holdover") { - const auto stat = m_device->get_run_statistics(); - ss << (stat.gpsdo_holdover ? 1 : 0); - } -#ifdef HAVE_LIMESDR - else if (parameter == "fifo_fill") { - const auto dev = std::dynamic_pointer_cast(m_device); - if (dev) { - ss << dev->get_fifo_fill_percent(); - } - else { - ss << "Parameter '" << parameter << - "' is not exported by controllable " << get_rc_name(); - throw ParameterError(ss.str()); - } - } -#endif // HAVE_LIMESDR - else { ss << "Parameter '" << parameter << "' is not exported by controllable " << get_rc_name(); throw ParameterError(ss.str()); diff --git a/src/output/SDRDevice.h b/src/output/SDRDevice.h index ffa1a3b..337d501 100644 --- a/src/output/SDRDevice.h +++ b/src/output/SDRDevice.h @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2022 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -38,6 +38,9 @@ DESCRIPTION: #include #include #include +#include +#include +#include #include "TimestampDecoder.h" @@ -109,22 +112,15 @@ struct FrameData { // All SDR Devices must implement the SDRDevice interface class SDRDevice { public: - struct RunStatistics { - size_t num_underruns = 0; - size_t num_late_packets = 0; - size_t num_overruns = 0; - size_t num_frames_modulated = 0; - - int gpsdo_num_sv = 0; - bool gpsdo_holdover = false; - }; + using run_statistic_t = std::variant; + using run_statistics_t = std::unordered_map; virtual void tune(double lo_offset, double frequency) = 0; virtual double get_tx_freq(void) const = 0; virtual void set_txgain(double txgain) = 0; virtual double get_txgain(void) const = 0; virtual void transmit_frame(struct FrameData&& frame) = 0; - virtual RunStatistics get_run_statistics(void) const = 0; + virtual run_statistics_t get_run_statistics(void) const = 0; virtual double get_real_secs(void) const = 0; virtual void set_rxgain(double rxgain) = 0; virtual double get_rxgain(void) const = 0; @@ -136,8 +132,8 @@ class SDRDevice { frame_timestamp& ts, double timeout_secs) = 0; - // Returns device temperature in degrees C or NaN if not available - virtual double get_temperature(void) const = 0; + // Returns device temperature in degrees C + virtual std::optional get_temperature(void) const = 0; // Return true if GPS and reference clock inputs are ok virtual bool is_clk_source_ok(void) const = 0; diff --git a/src/output/Soapy.cpp b/src/output/Soapy.cpp index 8c52546..00df9dc 100644 --- a/src/output/Soapy.cpp +++ b/src/output/Soapy.cpp @@ -180,13 +180,13 @@ double Soapy::get_bandwidth(void) const return m_device->getBandwidth(SOAPY_SDR_TX, 0); } -SDRDevice::RunStatistics Soapy::get_run_statistics(void) const +SDRDevice::run_statistics_t Soapy::get_run_statistics(void) const { - RunStatistics rs; - rs.num_underruns = underflows; - rs.num_overruns = overflows; - rs.num_late_packets = late_packets; - rs.num_frames_modulated = num_frames_modulated; + run_statistics_t rs; + rs["underruns"] = underflows; + rs["overruns"] = overflows; + rs["timeouts"] = timeouts; + rs["frames"] = num_frames_modulated; return rs; } @@ -265,11 +265,11 @@ const char* Soapy::device_name(void) const return "Soapy"; } -double Soapy::get_temperature(void) const +std::optional Soapy::get_temperature(void) const { // TODO Unimplemented // LimeSDR exports 'lms7_temp' - return std::numeric_limits::quiet_NaN(); + return std::nullopt; } void Soapy::transmit_frame(struct FrameData&& frame) @@ -320,6 +320,7 @@ void Soapy::transmit_frame(struct FrameData&& frame) m_tx_stream, buffs, samps_to_send, flags, timeNs); if (num_sent == SOAPY_SDR_TIMEOUT) { + timeouts++; continue; } else if (num_sent == SOAPY_SDR_OVERFLOW) { diff --git a/src/output/Soapy.h b/src/output/Soapy.h index f3e1ee2..b98ac21 100644 --- a/src/output/Soapy.h +++ b/src/output/Soapy.h @@ -66,7 +66,7 @@ class Soapy : public Output::SDRDevice virtual void set_bandwidth(double bandwidth) override; virtual double get_bandwidth(void) const override; virtual void transmit_frame(struct FrameData&& frame) override; - virtual RunStatistics get_run_statistics(void) const override; + virtual run_statistics_t get_run_statistics(void) const override; virtual double get_real_secs(void) const override; virtual void set_rxgain(double rxgain) override; @@ -81,7 +81,7 @@ class Soapy : public Output::SDRDevice virtual bool is_clk_source_ok(void) const override; virtual const char* device_name(void) const override; - virtual double get_temperature(void) const override; + virtual std::optional get_temperature(void) const override; private: SDRDeviceConfig& m_conf; @@ -91,9 +91,9 @@ class Soapy : public Output::SDRDevice SoapySDR::Stream *m_rx_stream = nullptr; bool m_rx_stream_active = false; + size_t timeouts = 0; size_t underflows = 0; size_t overflows = 0; - size_t late_packets = 0; size_t num_frames_modulated = 0; }; diff --git a/src/output/UHD.cpp b/src/output/UHD.cpp index c325272..7f07ff2 100644 --- a/src/output/UHD.cpp +++ b/src/output/UHD.cpp @@ -377,18 +377,22 @@ void UHD::transmit_frame(struct FrameData&& frame) } -SDRDevice::RunStatistics UHD::get_run_statistics(void) const +SDRDevice::run_statistics_t UHD::get_run_statistics(void) const { - RunStatistics rs; - rs.num_underruns = num_underflows; - rs.num_overruns = num_overflows; - rs.num_late_packets = num_late_packets; - rs.num_frames_modulated = num_frames_modulated; + run_statistics_t rs; + rs["underruns"] = num_underflows; + rs["overruns"] = num_overflows; + rs["late_packets"] = num_late_packets; + rs["frames"] = num_frames_modulated; if (m_device_time) { const auto gpsdo_stat = m_device_time->get_gnss_stats(); - rs.gpsdo_holdover = gpsdo_stat.holdover; - rs.gpsdo_num_sv = gpsdo_stat.num_sv; + rs["gpsdo_holdover"] = gpsdo_stat.holdover; + rs["gpsdo_num_sv"] = gpsdo_stat.num_sv; + } + else { + rs["gpsdo_holdover"] = true; + rs["gpsdo_num_sv"] = 0; } return rs; } @@ -472,13 +476,13 @@ const char* UHD::device_name(void) const return "UHD"; } -double UHD::get_temperature(void) const +std::optional UHD::get_temperature(void) const { try { return std::round(m_usrp->get_tx_sensor("temp", 0).to_real()); } catch (const uhd::lookup_error &e) { - return std::numeric_limits::quiet_NaN(); + return std::nullopt; } } diff --git a/src/output/UHD.h b/src/output/UHD.h index 164254c..5823c0e 100644 --- a/src/output/UHD.h +++ b/src/output/UHD.h @@ -80,7 +80,7 @@ class UHD : public Output::SDRDevice virtual void set_bandwidth(double bandwidth) override; virtual double get_bandwidth(void) const override; virtual void transmit_frame(struct FrameData&& frame) override; - virtual RunStatistics get_run_statistics(void) const override; + virtual run_statistics_t get_run_statistics(void) const override; virtual double get_real_secs(void) const override; virtual void set_rxgain(double rxgain) override; @@ -95,7 +95,7 @@ class UHD : public Output::SDRDevice virtual bool is_clk_source_ok(void) const override; virtual const char* device_name(void) const override; - virtual double get_temperature(void) const override; + virtual std::optional get_temperature(void) const override; private: SDRDeviceConfig& m_conf; -- cgit v1.2.3 From 6e5fcb1054c14275ac132d9c68d5259adc3444c9 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Thu, 30 Mar 2023 12:14:49 +0200 Subject: SDR: add queued_frames_ms and fix max queue calculation --- src/output/SDR.cpp | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) (limited to 'src/output') diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index a4f5873..99ced7d 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -46,8 +46,11 @@ using namespace std; namespace Output { -// Maximum number of frames that can wait in frames, when not using synchronised transmission -static constexpr size_t FRAMES_MAX_SIZE = 8; +// Maximum number of frames that can wait in frames. +// Keep it low when not using synchronised transmission, in order to reduce delay. +// When using synchronised transmission, use a 6s buffer to give us enough margin. +static constexpr size_t FRAMES_MAX_SIZE_UNSYNC = 8; +static constexpr size_t FRAMES_MAX_SIZE_SYNC = 250; // If the timestamp is further in the future than // 100 seconds, abort @@ -81,6 +84,7 @@ SDR::SDR(SDRDeviceConfig& config, std::shared_ptr device) : RC_ADD_PARAMETER(frames, "Counter of number of frames modulated"); RC_ADD_PARAMETER(gpsdo_num_sv, "Number of Satellite Vehicles tracked by GPSDO"); RC_ADD_PARAMETER(gpsdo_holdover, "1 if the GPSDO is in holdover, 0 if it is using gnss"); + RC_ADD_PARAMETER(queued_frames_ms, "Number of frames queued, represented in milliseconds"); #ifdef HAVE_LIMESDR if (std::dynamic_pointer_cast(device)) { @@ -128,10 +132,6 @@ int SDR::process(Buffer *dataIn) meta_vec_t SDR::process_metadata(const meta_vec_t& metadataIn) { - double frame_duration_s = - chrono::duration_cast( - transmission_frame_duration(m_config.dabMode)).count() / 1000.0; - if (m_device and m_running) { FrameData frame; frame.buf = std::move(m_frame); @@ -169,10 +169,7 @@ meta_vec_t SDR::process_metadata(const meta_vec_t& metadataIn) } - const auto max_size = m_config.enableSync ? - (frame.ts.timestamp_offset * 400.0) / frame_duration_s - : FRAMES_MAX_SIZE; - + const auto max_size = m_config.enableSync ? FRAMES_MAX_SIZE_SYNC : FRAMES_MAX_SIZE_UNSYNC; auto r = m_queue.push_overflow(std::move(frame), max_size); etiLog.log(trace, "SDR,push %d %zu", r.overflowed, r.new_size); @@ -406,19 +403,10 @@ void SDR::set_parameter(const string& parameter, const string& value) else if (parameter == "muting") { ss >> m_config.muting; } - else if (parameter == "underruns" or - parameter == "overruns" or - parameter == "latepackets" or - parameter == "frames" or - parameter == "gpsdo_num_sv" or - parameter == "gpsdo_holdover" or - parameter == "fifo_fill") { - throw ParameterError("Parameter " + parameter + " is read-only."); - } else { stringstream ss_err; ss_err << "Parameter '" << parameter - << "' is not exported by controllable " << get_rc_name(); + << "' is read-only or not exported by controllable " << get_rc_name(); throw ParameterError(ss_err.str()); } } @@ -454,6 +442,11 @@ const string SDR::get_parameter(const string& parameter) const throw ParameterError("Temperature not available"); } } + else if (parameter == "queued_frames_ms") { + ss << m_queue.size() * + chrono::duration_cast(transmission_frame_duration(m_config.dabMode)) + .count(); + } else { if (m_device) { const auto stat = m_device->get_run_statistics(); -- cgit v1.2.3 From fe19871f8ee362f65d194b864eb989618b994e58 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 31 Mar 2023 11:30:47 +0200 Subject: Add more DEXTER metrics to RC --- src/output/Dexter.cpp | 172 +++++++++++++++++++++++++++++++++++++++++++------ src/output/SDR.cpp | 35 ++++++++-- src/output/SDRDevice.h | 2 +- 3 files changed, 186 insertions(+), 23 deletions(-) (limited to 'src/output') diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index dd882f2..515d810 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -344,30 +344,160 @@ SDRDevice::run_statistics_t Dexter::get_run_statistics(void) const std::unique_lock lock(m_attr_thread_mutex); rs["underruns"] = underflows; } - rs["overruns"] = 0; - rs["late_packets"] = num_late; + rs["latepackets"] = num_late; rs["frames"] = num_frames_modulated; - long long clks = 0; - int r = 0; - if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "clks", &clks)) == 0) { - rs["clks"] = (size_t)clks; + auto attr_to_stat = [&](const char* attr_name, const char* stat_name) { + long long attr_value = 0; + int r = 0; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, attr_name, &attr_value)) == 0) { + rs[stat_name] = (size_t)attr_value; + } + else { + rs[stat_name] = (ssize_t)-1; + etiLog.level(error) << "Failed to get dexter_dsp_tx." << attr_name << ": " << get_iio_error(r); + } + }; + + attr_to_stat("clks", "clks"); + attr_to_stat("stream0_fifo_not_empty_clks", "fifo_not_empty_clks"); + attr_to_stat("gpsdo_locked", "gpsdo_locked"); + attr_to_stat("pps_clk_error_hz", "pps_clk_error_hz"); + attr_to_stat("pps_cnt", "pps_cnt"); + attr_to_stat("dsp_version", "dsp_version"); + + constexpr double VMINFACT = 0.85; + constexpr double VMAXFACT = 1.15; + bool voltage_ok = true; + bool temp_ok = true; + + { + std::ifstream in("/sys/bus/i2c/devices/1-002f/hwmon/hwmon0/in2_input", std::ios::in | std::ios::binary); + if (in) { + double vcc3v3; + in >> vcc3v3; + rs["vcc3v3"] = vcc3v3 * (18+36)/36.0/1000.0; + voltage_ok = (vcc3v3 > VMINFACT * 3.3) and (vcc3v3 < VMAXFACT * 3.3); + } + else { + rs["vcc3v3"] = -1; + voltage_ok = false; + } } - else { - rs["clks"] = (ssize_t)-1; - etiLog.level(error) << "Failed to get dexter_dsp_tx.clks: " << get_iio_error(r); + { + std::ifstream in("/sys/bus/i2c/devices/1-002f/hwmon/hwmon0/in1_input", std::ios::in | std::ios::binary); + if (in) { + double vcc5v4; + in >> vcc5v4; + rs["vcc5v4"] = vcc5v4 * (51+36)/36.0/1000.0; + voltage_ok = (vcc5v4 > VMINFACT * 5.4) and (vcc5v4 < VMAXFACT * 5.4); + } + else { + rs["vcc5v4"] = -1; + voltage_ok = false; + } + } + { + std::ifstream in("/sys/bus/i2c/devices/1-002f/hwmon/hwmon0/in3_input", std::ios::in | std::ios::binary); + if (in) { + double vfan; + in >> vfan; + rs["vfan"] = vfan * (560+22)/22.0/1000.0; + } + else { + rs["vfan"] = -1; + voltage_ok = false; + } + } + { + std::ifstream in("/sys/bus/i2c/devices/1-002f/hwmon/hwmon0/in0_input", std::ios::in | std::ios::binary); + if (in) { + double vccmainin; + in >> vccmainin; + rs["vcc_main_in"] = vccmainin * (560+22)/22.0/1000.0; + voltage_ok |= vccmainin > 10.0; + } + else { + rs["vcc_main_in"] = -1; + voltage_ok = false; + } } + { + std::ifstream in("/sys/bus/i2c/devices/1-002f/hwmon/hwmon0/in4_input", std::ios::in | std::ios::binary); + if (in) { + double vcc3v3pll; + in >> vcc3v3pll; + rs["vcc3v3pll"] = vcc3v3pll * (18+36)/36.0/1000.0; + voltage_ok = (vcc3v3pll > VMINFACT * 3.3) and (vcc3v3pll < VMAXFACT * 3.3); + } + else { + rs["vcc3v3pll"] = -1; + voltage_ok = false; + } + } + { + std::ifstream in("/sys/bus/i2c/devices/1-002f/hwmon/hwmon0/in5_input", std::ios::in | std::ios::binary); + if (in) { + double vcc2v5io; + in >> vcc2v5io; + rs["vcc2v5io"] = vcc2v5io * (4.7+36)/36.0/1000.0; + voltage_ok = (vcc2v5io > VMINFACT * 2.5) and (vcc2v5io < VMAXFACT * 2.5); + } + else { + rs["vcc2v5io"] = -1; + voltage_ok = false; + } + } + { + std::ifstream in("/sys/bus/i2c/devices/1-002f/hwmon/hwmon0/in6_input", std::ios::in | std::ios::binary); + if (in) { + double vccocxo; + in >> vccocxo; + rs["vccocxo"] = vccocxo * (51+36)/36.0/1000.0; + } + else { + rs["vccocxo"] = -1; + voltage_ok = false; + } + } + + optional tfpga; + for (int i = 0; i < 100; i++) { + std::string path = "/sys/bus/iio/devices/iio:device"; + path += to_string(i); - long long fifo_not_empty_clks = 0; + std::ifstream iio_name(path + "/name", std::ios::in | std::ios::binary); + std::ostringstream sstr; + sstr << iio_name.rdbuf(); + if (sstr.str() == "xadc\n") { + std::ifstream in_scale(path + "/in_temp0_scale", std::ios::in | std::ios::binary); + std::ifstream in_offset(path + "/in_temp0_offset", std::ios::in | std::ios::binary); + std::ifstream in_temp0_raw(path + "/in_temp0_raw", std::ios::in | std::ios::binary); - if ((r = iio_device_attr_read_longlong( - m_dexter_dsp_tx, "stream0_fifo_not_empty_clks", &fifo_not_empty_clks)) == 0) { - rs["fifo_not_empty_clks"] = (size_t)fifo_not_empty_clks; + if (in_scale and in_offset and in_temp0_raw) { + double scale, offset, temp0_raw ; + in_scale >> scale; + in_offset >> offset; + in_temp0_raw >> temp0_raw; + + tfpga = (temp0_raw + offset) * scale / 1000.0; + } + break; + } + } + + if (tfpga) { + rs["tempfpga"] = *tfpga; + temp_ok |= *tfpga <= 85; } else { - rs["fifo_not_empty_clks"] = (ssize_t)-1; - etiLog.level(error) << "Failed to get dexter_dsp_tx.fifo_not_empty_clks: " << get_iio_error(r); + rs["tempfpga"] = -1; + temp_ok = false; } + + rs["voltage_alarm"] = not voltage_ok; + rs["temp_alarm"] = not temp_ok; + return rs; } @@ -408,7 +538,6 @@ size_t Dexter::receive_frame( bool Dexter::is_clk_source_ok() const { - // TODO return true; } @@ -419,8 +548,15 @@ const char* Dexter::device_name(void) const std::optional Dexter::get_temperature(void) const { - // TODO XADC contains temperature, but value is weird - return std::nullopt; + std::ifstream in("/sys/bus/i2c/devices/1-002f/hwmon/hwmon0/temp1_input", std::ios::in | std::ios::binary); + if (in) { + double tbaseboard; + in >> tbaseboard; + return tbaseboard / 1000.0; + } + else { + return {}; + } } void Dexter::transmit_frame(struct FrameData&& frame) diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index 99ced7d..cc080dc 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -25,7 +25,9 @@ */ #include "output/SDR.h" +#include "output/UHD.h" #include "output/Lime.h" +#include "output/Dexter.h" #include "PcDebug.h" #include "Log.h" @@ -82,8 +84,14 @@ SDR::SDR(SDRDeviceConfig& config, std::shared_ptr device) : RC_ADD_PARAMETER(underruns, "Counter of number of underruns"); RC_ADD_PARAMETER(latepackets, "Counter of number of late packets"); RC_ADD_PARAMETER(frames, "Counter of number of frames modulated"); - RC_ADD_PARAMETER(gpsdo_num_sv, "Number of Satellite Vehicles tracked by GPSDO"); - RC_ADD_PARAMETER(gpsdo_holdover, "1 if the GPSDO is in holdover, 0 if it is using gnss"); + +#ifdef HAVE_OUTPUT_UHD + if (std::dynamic_pointer_cast(device)) { + RC_ADD_PARAMETER(gpsdo_num_sv, "Number of Satellite Vehicles tracked by GPSDO"); + RC_ADD_PARAMETER(gpsdo_holdover, "1 if the GPSDO is in holdover, 0 if it is using gnss"); + } +#endif // HAVE_OUTPUT_UHD + RC_ADD_PARAMETER(queued_frames_ms, "Number of frames queued, represented in milliseconds"); #ifdef HAVE_LIMESDR @@ -93,8 +101,24 @@ SDR::SDR(SDRDeviceConfig& config, std::shared_ptr device) : #endif // HAVE_LIMESDR #ifdef HAVE_DEXTER - RC_ADD_PARAMETER(clks, "DEXTER internal clk counter value"); - RC_ADD_PARAMETER(fifo_not_empty_clks, "DEXTER internal clk counter value when FIFO was last empty"); + if (std::dynamic_pointer_cast(device)) { + RC_ADD_PARAMETER(clks, "DEXTER internal clk counter value"); + RC_ADD_PARAMETER(fifo_not_empty_clks, "DEXTER internal clk counter value when FIFO was last empty"); + RC_ADD_PARAMETER(gpsdo_locked, "1 if GPSDO is locked"); + RC_ADD_PARAMETER(pps_clk_error_hz, "Estimated error in Hz of clock"); + RC_ADD_PARAMETER(pps_cnt, "Number of 1PPS pulses seen from GPS"); + RC_ADD_PARAMETER(dsp_version, "Version of FPGA DSP"); + RC_ADD_PARAMETER(vcc3v3, "Voltage of VCC 3V3"); + RC_ADD_PARAMETER(vcc5v4, "Voltage of VCC 5V4"); + RC_ADD_PARAMETER(vfan, "Fan voltage"); + RC_ADD_PARAMETER(vcc_main_in, "Main input voltage"); + RC_ADD_PARAMETER(vcc3v3pll, "Voltage of VCC 3V3 PLL"); + RC_ADD_PARAMETER(vcc2v5io, "Voltage of VCC 2V5 IO"); + RC_ADD_PARAMETER(vccocxo, "OCXO voltage"); + RC_ADD_PARAMETER(tempfpga, "FPGA temperature [celsius]"); + RC_ADD_PARAMETER(voltage_alarm, "Voltage out of bounds"); + RC_ADD_PARAMETER(temp_alarm, "Temperature out of bounds"); + } #endif // HAVE_DEXTER } @@ -455,6 +479,9 @@ const string SDR::get_parameter(const string& parameter) const if (std::holds_alternative(value)) { ss << std::get(value); } + else if (std::holds_alternative(value)) { + ss << std::get(value); + } else if (std::holds_alternative(value)) { ss << std::get(value); } diff --git a/src/output/SDRDevice.h b/src/output/SDRDevice.h index 337d501..a4f551c 100644 --- a/src/output/SDRDevice.h +++ b/src/output/SDRDevice.h @@ -112,7 +112,7 @@ struct FrameData { // All SDR Devices must implement the SDRDevice interface class SDRDevice { public: - using run_statistic_t = std::variant; + using run_statistic_t = std::variant; using run_statistics_t = std::unordered_map; virtual void tune(double lo_offset, double frequency) = 0; -- cgit v1.2.3 From ae70341365e27d766c8530924209fc4826036aea Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 31 Mar 2023 14:31:32 +0200 Subject: Add JSON output to RC --- lib/RemoteControl.cpp | 94 ++++++++++++++++++++++++++++++++++++++++--- lib/RemoteControl.h | 14 +++++-- src/DabModulator.cpp | 7 ++++ src/DabModulator.h | 10 ++--- src/FIRFilter.cpp | 9 ++++- src/FIRFilter.h | 10 ++--- src/GainControl.cpp | 20 ++++++++- src/GainControl.h | 12 ++---- src/GuardIntervalInserter.cpp | 9 ++++- src/GuardIntervalInserter.h | 14 +++---- src/MemlessPoly.cpp | 10 ++++- src/MemlessPoly.h | 14 +++---- src/OfdmGenerator.cpp | 9 ++++- src/OfdmGenerator.h | 13 ++---- src/TII.cpp | 11 ++++- src/TII.h | 14 +++---- src/TimestampDecoder.cpp | 21 +++++++++- src/TimestampDecoder.h | 12 ++---- src/output/SDR.cpp | 26 ++++++++++++ src/output/SDR.h | 2 + src/output/SDRDevice.h | 3 +- src/output/UHD.h | 8 ---- 22 files changed, 255 insertions(+), 87 deletions(-) (limited to 'src/output') diff --git a/lib/RemoteControl.cpp b/lib/RemoteControl.cpp index 30dcb60..6e9af20 100644 --- a/lib/RemoteControl.cpp +++ b/lib/RemoteControl.cpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #include @@ -102,6 +104,74 @@ std::list< std::vector > RemoteControllers::get_param_list_values(c return allparams; } + +static std::string escape_json(const std::string &s) { + std::ostringstream o; + for (auto c = s.cbegin(); c != s.cend(); c++) { + switch (*c) { + case '"': o << "\\\""; break; + case '\\': o << "\\\\"; break; + case '\b': o << "\\b"; break; + case '\f': o << "\\f"; break; + case '\n': o << "\\n"; break; + case '\r': o << "\\r"; break; + case '\t': o << "\\t"; break; + default: + if ('\x00' <= *c && *c <= '\x1f') { + o << "\\u" + << std::hex << std::setw(4) << std::setfill('0') << static_cast(*c); + } else { + o << *c; + } + } + } + return o.str(); +} + +std::string RemoteControllers::get_params_json(const std::string& name) { + RemoteControllable* controllable = get_controllable_(name); + const auto& values = controllable->get_all_values(); + + std::ostringstream ss; + ss << "{ "; + size_t ix = 0; + for (const auto& element : values) { + if (ix > 0) { + ss << ","; + } + + ss << "\"" << escape_json(element.first) << "\": "; + + const auto& value = element.second; + if (std::holds_alternative(value)) { + ss << "\"" << escape_json(std::get(value)) << "\""; + } + else if (std::holds_alternative(value)) { + ss << std::defaultfloat << std::get(value); + } + else if (std::holds_alternative(value)) { + ss << std::get(value); + } + else if (std::holds_alternative(value)) { + ss << std::get(value); + } + else if (std::holds_alternative(value)) { + ss << (std::get(value) ? "true" : "false"); + } + else if (std::holds_alternative(value)) { + ss << "null"; + } + else { + throw std::logic_error("variant alternative not handled"); + } + + ix++; + } + ss << " }"; + + return ss.str(); +} + std::string RemoteControllers::get_param(const std::string& name, const std::string& param) { RemoteControllable* controllable = get_controllable_(name); return controllable->get_parameter(param); @@ -427,10 +497,15 @@ void RemoteControllerZmq::recv_all(zmq::socket_t& pSocket, std::vector +#include +#include #include #include #include @@ -113,13 +115,16 @@ class RemoteControllable { } /* Base function to set parameters. */ - virtual void set_parameter( - const std::string& parameter, - const std::string& value) = 0; + virtual void set_parameter(const std::string& parameter, const std::string& value) = 0; /* Getting a parameter always returns a string. */ virtual const std::string get_parameter(const std::string& parameter) const = 0; + using value_t = std::variant; + using map_t = std::unordered_map; + + virtual const map_t get_all_values() const = 0; + protected: std::string m_rc_name; std::list< std::vector > m_parameters; @@ -135,6 +140,7 @@ class RemoteControllers { void check_faults(); std::list< std::vector > get_param_list_values(const std::string& name); std::string get_param(const std::string& name, const std::string& param); + std::string get_params_json(const std::string& name); void set_param( const std::string& name, diff --git a/src/DabModulator.cpp b/src/DabModulator.cpp index 3d8bd46..67764ba 100644 --- a/src/DabModulator.cpp +++ b/src/DabModulator.cpp @@ -410,3 +410,10 @@ const string DabModulator::get_parameter(const string& parameter) const } return ss.str(); } + +const RemoteControllable::map_t DabModulator::get_all_values() const +{ + map_t map; + map["rate"] = m_settings.outputRate; + return map; +} diff --git a/src/DabModulator.h b/src/DabModulator.h index 00d71f5..0d46e9f 100644 --- a/src/DabModulator.h +++ b/src/DabModulator.h @@ -3,7 +3,7 @@ Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2019 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -60,11 +60,9 @@ public: EtiSource* getEtiSource() { return &myEtiSource; } /******* REMOTE CONTROL ********/ - virtual void set_parameter(const std::string& parameter, - const std::string& value) override; - - virtual const std::string get_parameter( - const std::string& parameter) const override; + virtual void set_parameter(const std::string& parameter, const std::string& value) override; + virtual const std::string get_parameter(const std::string& parameter) const override; + virtual const RemoteControllable::map_t get_all_values() const override; protected: void setMode(unsigned mode); diff --git a/src/FIRFilter.cpp b/src/FIRFilter.cpp index 89cf0da..523d405 100644 --- a/src/FIRFilter.cpp +++ b/src/FIRFilter.cpp @@ -2,7 +2,7 @@ Copyright (C) 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2017 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -347,3 +347,10 @@ const string FIRFilter::get_parameter(const string& parameter) const return ss.str(); } +const RemoteControllable::map_t FIRFilter::get_all_values() const +{ + map_t map; + map["ntaps"] = m_taps.size(); + map["tapsfile"] = m_taps_file; + return map; +} diff --git a/src/FIRFilter.h b/src/FIRFilter.h index 8d2e707..2a469aa 100644 --- a/src/FIRFilter.h +++ b/src/FIRFilter.h @@ -2,7 +2,7 @@ Copyright (C) 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2017 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -59,11 +59,9 @@ public: const char* name() override { return "FIRFilter"; } /******* REMOTE CONTROL ********/ - virtual void set_parameter(const std::string& parameter, - const std::string& value) override; - - virtual const std::string get_parameter( - const std::string& parameter) const override; + virtual void set_parameter(const std::string& parameter, const std::string& value) override; + virtual const std::string get_parameter(const std::string& parameter) const override; + virtual const RemoteControllable::map_t get_all_values() const override; protected: virtual int internal_process(Buffer* const dataIn, Buffer* dataOut) override; diff --git a/src/GainControl.cpp b/src/GainControl.cpp index b781640..c111de3 100644 --- a/src/GainControl.cpp +++ b/src/GainControl.cpp @@ -3,7 +3,7 @@ Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2017 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -574,3 +574,21 @@ const string GainControl::get_parameter(const string& parameter) const return ss.str(); } +const RemoteControllable::map_t GainControl::get_all_values() const +{ + map_t map; + map["digital"] = m_digGain; + switch (m_gainmode) { + case GainMode::GAIN_FIX: + map["mode"] = "fix"; + break; + case GainMode::GAIN_MAX: + map["mode"] = "max"; + break; + case GainMode::GAIN_VAR: + map["mode"] = "var"; + break; + } + map["var"] = m_var_variance_rc; + return map; +} diff --git a/src/GainControl.h b/src/GainControl.h index 4c9a2bc..f0fd6b6 100644 --- a/src/GainControl.h +++ b/src/GainControl.h @@ -3,7 +3,7 @@ Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2017 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -63,13 +63,9 @@ class GainControl : public PipelinedModCodec, public RemoteControllable const char* name() override { return "GainControl"; } /* Functions for the remote control */ - /* Base function to set parameters. */ - virtual void set_parameter(const std::string& parameter, - const std::string& value) override; - - /* Getting a parameter always returns a string. */ - virtual const std::string get_parameter( - const std::string& parameter) const override; + virtual void set_parameter(const std::string& parameter, const std::string& value) override; + virtual const std::string get_parameter(const std::string& parameter) const override; + virtual const RemoteControllable::map_t get_all_values() const override; protected: virtual int internal_process( diff --git a/src/GuardIntervalInserter.cpp b/src/GuardIntervalInserter.cpp index 0cd5bd5..d5c71fb 100644 --- a/src/GuardIntervalInserter.cpp +++ b/src/GuardIntervalInserter.cpp @@ -2,7 +2,7 @@ Copyright (C) 2005, 2206, 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2017 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -302,3 +302,10 @@ const std::string GuardIntervalInserter::get_parameter(const std::string& parame } return ss.str(); } + +const RemoteControllable::map_t GuardIntervalInserter::get_all_values() const +{ + map_t map; + map["windowlen"] = d_windowOverlap; + return map; +} diff --git a/src/GuardIntervalInserter.h b/src/GuardIntervalInserter.h index 02ba72c..f88bdac 100644 --- a/src/GuardIntervalInserter.h +++ b/src/GuardIntervalInserter.h @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2017 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -52,15 +52,13 @@ class GuardIntervalInserter : public ModCodec, public RemoteControllable size_t symSize, size_t& windowOverlap); - int process(Buffer* const dataIn, Buffer* dataOut); - const char* name() { return "GuardIntervalInserter"; } + int process(Buffer* const dataIn, Buffer* dataOut) override; + const char* name() override { return "GuardIntervalInserter"; } /******* REMOTE CONTROL ********/ - virtual void set_parameter(const std::string& parameter, - const std::string& value); - - virtual const std::string get_parameter( - const std::string& parameter) const; + virtual void set_parameter(const std::string& parameter, const std::string& value) override; + virtual const std::string get_parameter(const std::string& parameter) const override; + virtual const RemoteControllable::map_t get_all_values() const override; protected: void update_window(size_t new_window_overlap); diff --git a/src/MemlessPoly.cpp b/src/MemlessPoly.cpp index 905ca67..4801ba0 100644 --- a/src/MemlessPoly.cpp +++ b/src/MemlessPoly.cpp @@ -2,7 +2,7 @@ Copyright (C) 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2018 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li Andreas Steger, andreas.steger@digris.ch @@ -467,3 +467,11 @@ const string MemlessPoly::get_parameter(const string& parameter) const return ss.str(); } +const RemoteControllable::map_t MemlessPoly::get_all_values() const +{ + map_t map; + map["ncoefs"] = m_coefs_am.size(); + map["coefs"] = serialise_coefficients(); + map["coeffile"] = m_coefs_file; + return map; +} diff --git a/src/MemlessPoly.h b/src/MemlessPoly.h index 4642596..09adc13 100644 --- a/src/MemlessPoly.h +++ b/src/MemlessPoly.h @@ -2,7 +2,7 @@ Copyright (C) 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2018 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -63,17 +63,15 @@ public: MemlessPoly& operator=(const MemlessPoly& other) = delete; virtual ~MemlessPoly(); - virtual const char* name() { return "MemlessPoly"; } + virtual const char* name() override { return "MemlessPoly"; } /******* REMOTE CONTROL ********/ - virtual void set_parameter(const std::string& parameter, - const std::string& value); - - virtual const std::string get_parameter( - const std::string& parameter) const; + virtual void set_parameter(const std::string& parameter, const std::string& value) override; + virtual const std::string get_parameter(const std::string& parameter) const override; + virtual const RemoteControllable::map_t get_all_values() const override; private: - int internal_process(Buffer* const dataIn, Buffer* dataOut); + int internal_process(Buffer* const dataIn, Buffer* dataOut) override; void load_coefficients(std::istream& coefData); std::string serialise_coefficients() const; diff --git a/src/OfdmGenerator.cpp b/src/OfdmGenerator.cpp index 2e68df0..d161861 100644 --- a/src/OfdmGenerator.cpp +++ b/src/OfdmGenerator.cpp @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2017 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -457,3 +457,10 @@ const std::string OfdmGenerator::get_parameter(const std::string& parameter) con } return ss.str(); } + +const RemoteControllable::map_t OfdmGenerator::get_all_values() const +{ + map_t map; + // TODO needs rework of the values + return map; +} diff --git a/src/OfdmGenerator.h b/src/OfdmGenerator.h index 30fdff4..90e562a 100644 --- a/src/OfdmGenerator.h +++ b/src/OfdmGenerator.h @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2017 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -59,14 +59,9 @@ class OfdmGenerator : public ModCodec, public RemoteControllable const char* name() override { return "OfdmGenerator"; } /* Functions for the remote control */ - /* Base function to set parameters. */ - virtual void set_parameter( - const std::string& parameter, - const std::string& value) override; - - /* Getting a parameter always returns a string. */ - virtual const std::string get_parameter( - const std::string& parameter) const override; + virtual void set_parameter(const std::string& parameter, const std::string& value) override; + virtual const std::string get_parameter(const std::string& parameter) const override; + virtual const RemoteControllable::map_t get_all_values() const override; protected: struct cfr_iter_stat_t { diff --git a/src/TII.cpp b/src/TII.cpp index 904f3ff..b329cdb 100644 --- a/src/TII.cpp +++ b/src/TII.cpp @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2018 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -385,3 +385,12 @@ const std::string TII::get_parameter(const std::string& parameter) const return ss.str(); } +const RemoteControllable::map_t TII::get_all_values() const +{ + map_t map; + map["enable"] = m_conf.enable; + map["pattern"] = m_conf.pattern; + map["comb"] = m_conf.comb; + map["old_variant"] = m_conf.old_variant; + return map; +} diff --git a/src/TII.h b/src/TII.h index d8c785d..b0ba646 100644 --- a/src/TII.h +++ b/src/TII.h @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2018 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -83,15 +83,13 @@ class TII : public ModCodec, public RemoteControllable public: TII(unsigned int dabmode, tii_config_t& tii_config); - int process(Buffer* dataIn, Buffer* dataOut); - const char* name(); + int process(Buffer* dataIn, Buffer* dataOut) override; + const char* name() override; /******* REMOTE CONTROL ********/ - virtual void set_parameter(const std::string& parameter, - const std::string& value); - - virtual const std::string get_parameter( - const std::string& parameter) const; + virtual void set_parameter(const std::string& parameter, const std::string& value) override; + virtual const std::string get_parameter(const std::string& parameter) const override; + virtual const RemoteControllable::map_t get_all_values() const override; protected: // Fill m_Acp with the correct carriers for the pattern/comb diff --git a/src/TimestampDecoder.cpp b/src/TimestampDecoder.cpp index 6e97af6..149cd50 100644 --- a/src/TimestampDecoder.cpp +++ b/src/TimestampDecoder.cpp @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2018 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -301,3 +301,22 @@ const std::string TimestampDecoder::get_parameter( return ss.str(); } +const RemoteControllable::map_t TimestampDecoder::get_all_values() const +{ + map_t map; + map["offset"] = timestamp_offset; + if (full_timestamp_received) { + map["timestamp"] = time_secs + ((double)time_pps / 16384000.0); + } + else { + map["timestamp"] = std::nullopt; + } + + if (full_timestamp_received) { + map["timestamp0"] = time_secs_of_frame0 + ((double)time_pps_of_frame0 / 16384000.0); + } + else { + map["timestamp0"] = std::nullopt; + } + return map; +} diff --git a/src/TimestampDecoder.h b/src/TimestampDecoder.h index 597b777..dc5aa78 100644 --- a/src/TimestampDecoder.h +++ b/src/TimestampDecoder.h @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2022 + Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -118,16 +118,12 @@ class TimestampDecoder : public RemoteControllable /*********** REMOTE CONTROL ***************/ /* Base function to set parameters. */ - virtual void set_parameter(const std::string& parameter, - const std::string& value); - - /* Getting a parameter always returns a string. */ - virtual const std::string get_parameter( - const std::string& parameter) const; + virtual void set_parameter(const std::string& parameter, const std::string& value) override; + virtual const std::string get_parameter(const std::string& parameter) const override; + virtual const RemoteControllable::map_t get_all_values() const override; const char* name() { return "TS"; } - protected: /* Push a new MNSC field into the decoder */ void pushMNSCData(uint8_t framephase, uint16_t mnsc); diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index cc080dc..860d8ed 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -491,6 +491,9 @@ const string SDR::get_parameter(const string& parameter) const else if (std::holds_alternative(value)) { ss << (std::get(value) ? 1 : 0); } + else if (std::holds_alternative(value)) { + ss << ""; + } else { throw std::logic_error("variant alternative not handled"); } @@ -507,4 +510,27 @@ const string SDR::get_parameter(const string& parameter) const return ss.str(); } +const RemoteControllable::map_t SDR::get_all_values() const +{ + map_t stat = m_device->get_run_statistics(); + + stat["txgain"] = m_config.txgain; + stat["rxgain"] = m_config.rxgain; + stat["freq"] = m_config.frequency; + stat["muting"] = m_config.muting; + stat["temp"] = std::nullopt; + + if (m_device) { + const std::optional temp = m_device->get_temperature(); + if (temp) { + stat["temp"] = *temp; + } + } + stat["queued_frames_ms"] = m_queue.size() * + (size_t)chrono::duration_cast(transmission_frame_duration(m_config.dabMode)) + .count(); + + return stat; +} + } // namespace Output diff --git a/src/output/SDR.h b/src/output/SDR.h index eb0ed9d..9f08348 100644 --- a/src/output/SDR.h +++ b/src/output/SDR.h @@ -67,6 +67,8 @@ class SDR : public ModOutput, public ModMetadata, public RemoteControllable { virtual const std::string get_parameter( const std::string& parameter) const override; + virtual const RemoteControllable::map_t get_all_values() const override; + private: void process_thread_entry(void); void handle_frame(struct FrameData&& frame); diff --git a/src/output/SDRDevice.h b/src/output/SDRDevice.h index a4f551c..628372a 100644 --- a/src/output/SDRDevice.h +++ b/src/output/SDRDevice.h @@ -112,8 +112,7 @@ struct FrameData { // All SDR Devices must implement the SDRDevice interface class SDRDevice { public: - using run_statistic_t = std::variant; - using run_statistics_t = std::unordered_map; + using run_statistics_t = RemoteControllable::map_t; virtual void tune(double lo_offset, double frequency) = 0; virtual double get_tx_freq(void) const = 0; diff --git a/src/output/UHD.h b/src/output/UHD.h index 5823c0e..97a821e 100644 --- a/src/output/UHD.h +++ b/src/output/UHD.h @@ -55,14 +55,6 @@ DESCRIPTION: #include #include -// If the timestamp is further in the future than -// 100 seconds, abort -#define TIMESTAMP_ABORT_FUTURE 100 - -// Add a delay to increase buffers when -// frames are too far in the future -#define TIMESTAMP_MARGIN_FUTURE 0.5 - namespace Output { class UHD : public Output::SDRDevice -- cgit v1.2.3 From fd05ca739d94b23609e85e074fb659b5e279d358 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 26 Apr 2023 14:57:37 +0200 Subject: dexter: set frequency to ad9957, lo_offset to dsp --- src/output/Dexter.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/output') diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index 515d810..2d053aa 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -277,12 +277,18 @@ Dexter::~Dexter() void Dexter::tune(double lo_offset, double frequency) { - // TODO lo_offset - long long freq = m_conf.frequency - 204800000; + // lo_offset is applied to the DSP, and frequency is given to the ad9957 + + long long freq = frequency; int r = 0; + if ((r = iio_device_attr_write_longlong(m_ad9957_tx0, "center_frequency", freq)) != 0) { + etiLog.level(warn) << "Failed to set ad9957_tx0.center_frequency = " << freq << " : " << get_iio_error(r); + } + + long long lo_offs = lo_offset; - if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "frequency0", freq)) != 0) { - etiLog.level(warn) << "Failed to set dexter_dsp_tx.frequency0 = " << freq << " : " << get_iio_error(r); + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "frequency0", lo_offs)) != 0) { + etiLog.level(warn) << "Failed to set dexter_dsp_tx.frequency0 = " << lo_offs << " : " << get_iio_error(r); } } -- cgit v1.2.3 From b17001011204a47432a1deb970cd15466c16f0bf Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 26 Apr 2023 17:03:14 +0200 Subject: Properly set TX frequency --- src/output/Dexter.cpp | 25 +++++++++++++++++-------- src/output/Dexter.h | 1 + 2 files changed, 18 insertions(+), 8 deletions(-) (limited to 'src/output') diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index 2d053aa..4cb9cd8 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -87,6 +87,11 @@ Dexter::Dexter(SDRDeviceConfig& config) : throw std::runtime_error("Dexter: Unable to find dexter_dsp_tx iio device"); } + m_ad9957 = iio_context_find_device(m_ctx, "ad9957"); + if (not m_ad9957) { + throw std::runtime_error("Dexter: Unable to find ad9957 iio device"); + } + m_ad9957_tx0 = iio_context_find_device(m_ctx, "ad9957_tx0"); if (not m_ad9957_tx0) { throw std::runtime_error("Dexter: Unable to find ad9957_tx0 iio device"); @@ -281,8 +286,8 @@ void Dexter::tune(double lo_offset, double frequency) long long freq = frequency; int r = 0; - if ((r = iio_device_attr_write_longlong(m_ad9957_tx0, "center_frequency", freq)) != 0) { - etiLog.level(warn) << "Failed to set ad9957_tx0.center_frequency = " << freq << " : " << get_iio_error(r); + if ((r = iio_device_attr_write_longlong(m_ad9957, "center_frequency", freq)) != 0) { + etiLog.level(warn) << "Failed to set ad9957.center_frequency = " << freq << " : " << get_iio_error(r); } long long lo_offs = lo_offset; @@ -294,17 +299,21 @@ void Dexter::tune(double lo_offset, double frequency) double Dexter::get_tx_freq(void) const { - long long frequency = 0; + long long lo_offset = 0; int r = 0; - if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "frequency0", &frequency)) != 0) { - etiLog.level(warn) << "Failed to read dexter_dsp_tx.frequency0 = " << - frequency << " : " << get_iio_error(r); + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "frequency0", &lo_offset)) != 0) { + etiLog.level(warn) << "Failed to read dexter_dsp_tx.frequency0: " << get_iio_error(r); return 0; } - else { - return frequency + 204800000; + + long long frequency = 0; + if ((r = iio_device_attr_read_longlong(m_ad9957, "center_frequency", &frequency)) != 0) { + etiLog.level(warn) << "Failed to read ad9957.center_frequency: " << get_iio_error(r); + return 0; } + + return frequency + lo_offset; } void Dexter::set_txgain(double txgain) diff --git a/src/output/Dexter.h b/src/output/Dexter.h index 7925de7..3d47f87 100644 --- a/src/output/Dexter.h +++ b/src/output/Dexter.h @@ -93,6 +93,7 @@ class Dexter : public Output::SDRDevice struct iio_context* m_ctx = nullptr; struct iio_device* m_dexter_dsp_tx = nullptr; + struct iio_device* m_ad9957 = nullptr; struct iio_device* m_ad9957_tx0 = nullptr; struct iio_channel* m_tx_channel = nullptr; struct iio_buffer *m_buffer = nullptr; -- cgit v1.2.3 From 830fb3ab0a8631055b2341b8dac50b937b6e99bb Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 26 Apr 2023 18:08:54 +0200 Subject: Dexter: Add three clock states and handling --- src/ConfigParser.cpp | 14 ++- src/output/Dexter.cpp | 310 +++++++++++++++++++++++++++++++------------------- src/output/Dexter.h | 18 ++- 3 files changed, 217 insertions(+), 125 deletions(-) (limited to 'src/output') diff --git a/src/ConfigParser.cpp b/src/ConfigParser.cpp index 3e223c3..cb4dc24 100644 --- a/src/ConfigParser.cpp +++ b/src/ConfigParser.cpp @@ -278,7 +278,8 @@ static void parse_configfile( mod_settings.sdr_device_config = sdr_device_config; mod_settings.useUHDOutput = true; } -#endif +#endif // defined(HAVE_OUTPUT_UHD) + #if defined(HAVE_SOAPYSDR) else if (output_selected == "soapysdr") { auto& outputsoapy_conf = mod_settings.sdr_device_config; @@ -309,7 +310,8 @@ static void parse_configfile( mod_settings.useSoapyOutput = true; } -#endif +#endif // defined(HAVE_SOAPYSDR) + #if defined(HAVE_DEXTER) else if (output_selected == "dexter") { auto& outputdexter_conf = mod_settings.sdr_device_config; @@ -318,6 +320,7 @@ static void parse_configfile( outputdexter_conf.frequency = pt.GetReal("dexteroutput.frequency", 0); std::string chan = pt.Get("dexteroutput.channel", ""); outputdexter_conf.dabMode = mod_settings.dabMode; + outputdexter_conf.maxGPSHoldoverTime = pt.GetInteger("dexteroutput.max_gps_holdover_time", 0); if (outputdexter_conf.frequency == 0 && chan == "") { std::cerr << " dexter output enabled, but neither frequency nor channel defined.\n"; @@ -333,7 +336,8 @@ static void parse_configfile( mod_settings.useDexterOutput = true; } -#endif +#endif // defined(HAVE_DEXTER) + #if defined(HAVE_LIMESDR) else if (output_selected == "limesdr") { auto& outputlime_conf = mod_settings.sdr_device_config; @@ -363,7 +367,7 @@ static void parse_configfile( mod_settings.useLimeOutput = true; } -#endif +#endif // defined(HAVE_LIMESDR) #if defined(HAVE_BLADERF) else if (output_selected == "bladerf") { @@ -392,7 +396,7 @@ static void parse_configfile( mod_settings.useBladeRFOutput = true; } -#endif +#endif // defined(HAVE_BLADERF) #if defined(HAVE_ZEROMQ) else if (output_selected == "zmq") { diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index 4cb9cd8..59baf7e 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -120,58 +120,6 @@ Dexter::Dexter(SDRDeviceConfig& config) : // skip: antenna - // get H/W time - /* Procedure: - * Wait 200ms after second change, fetch pps_clks attribute - * idem at the next second, and check that pps_clks incremented by DSP_CLOCK - * If ok, store the correspondence between current second change (measured in UTC clock time) - * and the counter value at pps rising edge. */ - - etiLog.level(info) << "Dexter: Waiting for second change..."; - - struct timespec time_at_startup; - fill_time(&time_at_startup); - time_at_startup.tv_nsec = 0; - - struct timespec time_now; - do { - fill_time(&time_now); - this_thread::sleep_for(chrono::milliseconds(1)); - } while (time_at_startup.tv_sec == time_now.tv_sec); - this_thread::sleep_for(chrono::milliseconds(200)); - - long long pps_clks = 0; - if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "pps_clks", &pps_clks)) != 0) { - etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_clks: " << get_iio_error(r); - throw std::runtime_error("Dexter: Cannot read IIO attribute"); - } - - time_t tnow = time_now.tv_sec; - etiLog.level(info) << "Dexter: pps_clks " << pps_clks << " at UTC " << - put_time(std::gmtime(&tnow), "%Y-%m-%d %H:%M:%S"); - - time_at_startup.tv_sec = time_now.tv_sec; - do { - fill_time(&time_now); - this_thread::sleep_for(chrono::milliseconds(1)); - } while (time_at_startup.tv_sec == time_now.tv_sec); - this_thread::sleep_for(chrono::milliseconds(200)); - - long long pps_clks2 = 0; - if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "pps_clks", &pps_clks2)) != 0) { - etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_clks: " << get_iio_error(r); - throw std::runtime_error("Dexter: Cannot read IIO attribute"); - } - tnow = time_now.tv_sec; - etiLog.level(info) << "Dexter: pps_clks increased by " << pps_clks2 - pps_clks << " at UTC " << - put_time(std::gmtime(&tnow), "%Y-%m-%d %H:%M:%S"); - - if ((uint64_t)pps_clks + DSP_CLOCK != (uint64_t)pps_clks2) { - throw std::runtime_error("Dexter: Wrong increase of pps_clks, expected " + to_string(DSP_CLOCK)); - } - m_utc_seconds_at_startup = time_now.tv_sec; - m_clock_count_at_startup = pps_clks2; - // The FIFO should not contain data, but setting gain=0 before setting start_clks to zero is an additional security if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", 0)) != 0) { throw std::runtime_error("Failed to set dexter_dsp_tx.gain0 = 0 : " + get_iio_error(r)); @@ -248,35 +196,118 @@ void Dexter::channel_down() etiLog.level(debug) << "DEXTER CHANNEL_DOWN"; } - -Dexter::~Dexter() +void Dexter::handle_hw_time() { - m_running = false; - if (m_underflow_read_thread.joinable()) { - m_underflow_read_thread.join(); - } - - if (m_ctx) { - if (m_dexter_dsp_tx) { - iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", 0); - } - - if (m_buffer) { - iio_buffer_destroy(m_buffer); - m_buffer = nullptr; - } - - if (m_tx_channel) { - iio_channel_disable(m_tx_channel); - } - - iio_context_destroy(m_ctx); - m_ctx = nullptr; - } + /* + * On startup, wait until `gpsdo_locked==1` and `pps_loss_of_signal==0`, + * then do the clocks alignment and go to normal state. + * + * In normal state, if `pps_loss_of_signal==1`, go to holdover state. + * + * If we've been in holdover state for longer than the configured time, or + * if `pps_loss_of_signal==0` stop the mod and restart. + */ + int r; - if (m_underflow_ctx) { - iio_context_destroy(m_underflow_ctx); - m_underflow_ctx = nullptr; + switch (m_clock_state) { + case DexterClockState::Startup: + { + long long gpsdo_locked = 0; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "gpsdo_locked", &gpsdo_locked)) != 0) { + etiLog.level(error) << "Failed to get dexter_dsp_tx.gpsdo_locked: " << get_iio_error(r); + throw std::runtime_error("Dexter: Cannot read IIO attribute"); + } + + long long pps_loss_of_signal = 0; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "pps_loss_of_signal", &pps_loss_of_signal)) != 0) { + etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_loss_of_signal: " << get_iio_error(r); + throw std::runtime_error("Dexter: Cannot read IIO attribute"); + } + + if (gpsdo_locked == 1 and pps_loss_of_signal == 0) { + /* Procedure: + * Wait 200ms after second change, fetch pps_clks attribute + * idem at the next second, and check that pps_clks incremented by DSP_CLOCK + * If ok, store the correspondence between current second change (measured in UTC clock time) + * and the counter value at pps rising edge. */ + + etiLog.level(info) << "Dexter: Waiting for second change..."; + + struct timespec time_at_startup; + fill_time(&time_at_startup); + time_at_startup.tv_nsec = 0; + + struct timespec time_now; + do { + fill_time(&time_now); + this_thread::sleep_for(chrono::milliseconds(1)); + } while (time_at_startup.tv_sec == time_now.tv_sec); + this_thread::sleep_for(chrono::milliseconds(200)); + + long long pps_clks = 0; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "pps_clks", &pps_clks)) != 0) { + etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_clks: " << get_iio_error(r); + throw std::runtime_error("Dexter: Cannot read IIO attribute"); + } + + time_t tnow = time_now.tv_sec; + etiLog.level(info) << "Dexter: pps_clks " << pps_clks << " at UTC " << + put_time(std::gmtime(&tnow), "%Y-%m-%d %H:%M:%S"); + + time_at_startup.tv_sec = time_now.tv_sec; + do { + fill_time(&time_now); + this_thread::sleep_for(chrono::milliseconds(1)); + } while (time_at_startup.tv_sec == time_now.tv_sec); + this_thread::sleep_for(chrono::milliseconds(200)); + + long long pps_clks2 = 0; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "pps_clks", &pps_clks2)) != 0) { + etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_clks: " << get_iio_error(r); + throw std::runtime_error("Dexter: Cannot read IIO attribute"); + } + tnow = time_now.tv_sec; + etiLog.level(info) << "Dexter: pps_clks increased by " << pps_clks2 - pps_clks << " at UTC " << + put_time(std::gmtime(&tnow), "%Y-%m-%d %H:%M:%S"); + + if ((uint64_t)pps_clks + DSP_CLOCK != (uint64_t)pps_clks2) { + throw std::runtime_error("Dexter: Wrong increase of pps_clks, expected " + to_string(DSP_CLOCK)); + } + + m_utc_seconds_at_startup = time_now.tv_sec; + m_clock_count_at_startup = pps_clks2; + m_holdover_since = chrono::steady_clock::time_point::min(); + m_clock_state = DexterClockState::Normal; + } + } + break; + case DexterClockState::Normal: + { + long long pps_loss_of_signal = 0; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "pps_loss_of_signal", &pps_loss_of_signal)) != 0) { + etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_loss_of_signal: " << get_iio_error(r); + throw std::runtime_error("Dexter: Cannot read IIO attribute"); + } + + if (pps_loss_of_signal == 1) { + m_holdover_since = chrono::steady_clock::now(); + m_clock_state = DexterClockState::Holdover; + } + } + break; + case DexterClockState::Holdover: + { + using namespace chrono; + const duration d = steady_clock::now() - m_holdover_since; + const auto max_holdover_duration = seconds(m_conf.maxGPSHoldoverTime); + if (d > max_holdover_duration) { + m_clock_state = DexterClockState::Startup; + m_utc_seconds_at_startup = 0; + m_clock_count_at_startup = 0; + m_holdover_since = chrono::steady_clock::time_point::min(); + } + } + break; } } @@ -526,7 +557,14 @@ double Dexter::get_real_secs(void) const throw std::runtime_error("Dexter: Cannot read IIO attribute"); } - return (double)m_utc_seconds_at_startup + (double)(clks - m_clock_count_at_startup) / (double)DSP_CLOCK; + switch (m_clock_state) { + case DexterClockState::Startup: + return 0; + case DexterClockState::Normal: + case DexterClockState::Holdover: + return (double)m_utc_seconds_at_startup + (double)(clks - m_clock_count_at_startup) / (double)DSP_CLOCK; + } + throw std::logic_error("Unhandled switch"); } void Dexter::set_rxgain(double rxgain) @@ -585,46 +623,53 @@ void Dexter::transmit_frame(struct FrameData&& frame) const bool require_timestamped_tx = (m_conf.enableSync and frame.ts.timestamp_valid); + handle_hw_time(); + if (not m_channel_is_up) { if (require_timestamped_tx) { - constexpr uint64_t TIMESTAMP_PPS_PER_DSP_CLOCKS = DSP_CLOCK / 16384000; - // TIMESTAMP_PPS_PER_DSP_CLOCKS=10 because timestamp_pps is represented in 16.384 MHz clocks - uint64_t frame_start_clocks = - // at second level - ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK + m_clock_count_at_startup + - // at subsecond level - (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS; - - const double margin_s = frame.ts.offset_to_system_time(); - - long long clks = 0; - int r = 0; - if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "clks", &clks)) != 0) { - etiLog.level(error) << "Failed to get dexter_dsp_tx.clks: " << get_iio_error(r); - throw std::runtime_error("Dexter: Cannot read IIO attribute"); + if (m_clock_state == DexterClockState::Startup) { + return; // not ready } - - const double margin_device_s = (double)(frame_start_clocks - clks) / DSP_CLOCK; - - etiLog.level(debug) << "DEXTER FCT " << frame.ts.fct << " TS CLK " << - ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK << " + " << - m_clock_count_at_startup << " + " << - (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS << " = " << - frame_start_clocks << " DELTA " << margin_s << " " << margin_device_s; - - // Ensure we hand the frame over to HW with a bit of margin - if (margin_s < 0.2) { - etiLog.level(warn) << "Skip frame short margin " << margin_s; - num_late++; - return; - } - - if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", frame_start_clocks)) != 0) { - etiLog.level(warn) << "Skip frame, failed to set dexter_dsp_tx.stream0_start_clks = " << frame_start_clocks << " : " << get_iio_error(r); - num_late++; - return; + else { + constexpr uint64_t TIMESTAMP_PPS_PER_DSP_CLOCKS = DSP_CLOCK / 16384000; + // TIMESTAMP_PPS_PER_DSP_CLOCKS=10 because timestamp_pps is represented in 16.384 MHz clocks + uint64_t frame_start_clocks = + // at second level + ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK + m_clock_count_at_startup + + // at subsecond level + (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS; + + const double margin_s = frame.ts.offset_to_system_time(); + + long long clks = 0; + int r = 0; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "clks", &clks)) != 0) { + etiLog.level(error) << "Failed to get dexter_dsp_tx.clks: " << get_iio_error(r); + throw std::runtime_error("Dexter: Cannot read IIO attribute"); + } + + const double margin_device_s = (double)(frame_start_clocks - clks) / DSP_CLOCK; + + etiLog.level(debug) << "DEXTER FCT " << frame.ts.fct << " TS CLK " << + ((int64_t)frame.ts.timestamp_sec - (int64_t)m_utc_seconds_at_startup) * DSP_CLOCK << " + " << + m_clock_count_at_startup << " + " << + (uint64_t)frame.ts.timestamp_pps * TIMESTAMP_PPS_PER_DSP_CLOCKS << " = " << + frame_start_clocks << " DELTA " << margin_s << " " << margin_device_s; + + // Ensure we hand the frame over to HW with a bit of margin + if (margin_s < 0.2) { + etiLog.level(warn) << "Skip frame short margin " << margin_s; + num_late++; + return; + } + + if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", frame_start_clocks)) != 0) { + etiLog.level(warn) << "Skip frame, failed to set dexter_dsp_tx.stream0_start_clks = " << frame_start_clocks << " : " << get_iio_error(r); + num_late++; + return; + } + m_require_timestamp_refresh = false; } - m_require_timestamp_refresh = false; } channel_up(); @@ -704,8 +749,37 @@ void Dexter::underflow_read_process() m_running = false; } -} // namespace Output +Dexter::~Dexter() +{ + m_running = false; + if (m_underflow_read_thread.joinable()) { + m_underflow_read_thread.join(); + } -#endif // HAVE_DEXTER + if (m_ctx) { + if (m_dexter_dsp_tx) { + iio_device_attr_write_longlong(m_dexter_dsp_tx, "gain0", 0); + } + if (m_buffer) { + iio_buffer_destroy(m_buffer); + m_buffer = nullptr; + } + if (m_tx_channel) { + iio_channel_disable(m_tx_channel); + } + + iio_context_destroy(m_ctx); + m_ctx = nullptr; + } + + if (m_underflow_ctx) { + iio_context_destroy(m_underflow_ctx); + m_underflow_ctx = nullptr; + } +} + +} // namespace Output + +#endif // HAVE_DEXTER diff --git a/src/output/Dexter.h b/src/output/Dexter.h index 3d47f87..f70bb14 100644 --- a/src/output/Dexter.h +++ b/src/output/Dexter.h @@ -42,6 +42,7 @@ DESCRIPTION: #include #include #include +#include #include "output/SDR.h" #include "ModPlugin.h" @@ -50,6 +51,12 @@ DESCRIPTION: namespace Output { +enum class DexterClockState { + Startup, + Normal, + Holdover +}; + class Dexter : public Output::SDRDevice { public: @@ -85,6 +92,7 @@ class Dexter : public Output::SDRDevice private: void channel_up(); void channel_down(); + void handle_hw_time(); bool m_channel_is_up = false; @@ -112,9 +120,15 @@ class Dexter : public Output::SDRDevice size_t num_buffers_pushed = 0; - uint64_t m_utc_seconds_at_startup; + DexterClockState m_clock_state = DexterClockState::Startup; + + // Only valid when m_clock_state is not Startup + uint64_t m_utc_seconds_at_startup = 0; uint64_t m_clock_count_at_startup = 0; - uint64_t m_clock_count_frame = 0; + + // Only valid when m_clock_state Holdover + std::chrono::steady_clock::time_point m_holdover_since = + std::chrono::steady_clock::time_point::min(); }; } // namespace Output -- cgit v1.2.3 From f0bb1e24952c7e261ba13907c0a5d8c3e1d198ca Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Thu, 27 Apr 2023 13:43:22 +0200 Subject: Improve dexter clock handling --- src/DabModulator.cpp | 3 ++- src/output/BladeRF.cpp | 2 +- src/output/BladeRF.h | 2 +- src/output/Dexter.cpp | 73 +++++++++++++++++++++++++++++++++++--------------- src/output/Dexter.h | 3 ++- src/output/Lime.cpp | 2 +- src/output/Lime.h | 2 +- src/output/SDR.cpp | 29 ++------------------ src/output/SDR.h | 4 --- src/output/SDRDevice.h | 2 +- src/output/Soapy.cpp | 2 +- src/output/Soapy.h | 2 +- src/output/UHD.cpp | 2 +- src/output/UHD.h | 2 +- 14 files changed, 66 insertions(+), 64 deletions(-) (limited to 'src/output') diff --git a/src/DabModulator.cpp b/src/DabModulator.cpp index 67764ba..64ebf03 100644 --- a/src/DabModulator.cpp +++ b/src/DabModulator.cpp @@ -27,6 +27,7 @@ #include #include +#include #include "DabModulator.h" #include "PcDebug.h" @@ -348,7 +349,7 @@ int DabModulator::process(Buffer* dataOut) } shared_ptr prev_plugin = static_pointer_cast(cifSig); - const std::list > plugins({ + const std::vector > plugins({ static_pointer_cast(cifCicEq), static_pointer_cast(cifOfdm), static_pointer_cast(cifGain), diff --git a/src/output/BladeRF.cpp b/src/output/BladeRF.cpp index 2dd7176..c16b64d 100755 --- a/src/output/BladeRF.cpp +++ b/src/output/BladeRF.cpp @@ -273,7 +273,7 @@ size_t BladeRF::receive_frame( return 0; } -bool BladeRF::is_clk_source_ok() const +bool BladeRF::is_clk_source_ok() { // TODO return true; diff --git a/src/output/BladeRF.h b/src/output/BladeRF.h index eb3e58b..fa3419e 100755 --- a/src/output/BladeRF.h +++ b/src/output/BladeRF.h @@ -87,7 +87,7 @@ class BladeRF : public Output::SDRDevice double timeout_secs) override; // Return true if GPS and reference clock inputs are ok - virtual bool is_clk_source_ok(void) const override; + virtual bool is_clk_source_ok(void) override; virtual const char* device_name(void) const override; virtual std::optional get_temperature(void) const override; diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index 59baf7e..25de030 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -277,7 +277,9 @@ void Dexter::handle_hw_time() m_utc_seconds_at_startup = time_now.tv_sec; m_clock_count_at_startup = pps_clks2; m_holdover_since = chrono::steady_clock::time_point::min(); + m_holdover_since_t = 0; m_clock_state = DexterClockState::Normal; + etiLog.level(debug) << "Dexter: switch clock state Startup -> Normal"; } } break; @@ -291,20 +293,31 @@ void Dexter::handle_hw_time() if (pps_loss_of_signal == 1) { m_holdover_since = chrono::steady_clock::now(); + m_holdover_since_t = chrono::system_clock::to_time_t(chrono::system_clock::now()); m_clock_state = DexterClockState::Holdover; + etiLog.level(debug) << "Dexter: switch clock state Normal -> Holdover"; } } break; case DexterClockState::Holdover: { using namespace chrono; + + long long pps_loss_of_signal = 0; + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "pps_loss_of_signal", &pps_loss_of_signal)) != 0) { + etiLog.level(error) << "Failed to get dexter_dsp_tx.pps_loss_of_signal: " << get_iio_error(r); + throw std::runtime_error("Dexter: Cannot read IIO attribute"); + } + const duration d = steady_clock::now() - m_holdover_since; const auto max_holdover_duration = seconds(m_conf.maxGPSHoldoverTime); - if (d > max_holdover_duration) { + if (d > max_holdover_duration or pps_loss_of_signal == 0) { m_clock_state = DexterClockState::Startup; m_utc_seconds_at_startup = 0; m_clock_count_at_startup = 0; m_holdover_since = chrono::steady_clock::time_point::min(); + m_holdover_since_t = 0; + etiLog.level(debug) << "Dexter: switch clock state Holdover -> Startup"; } } break; @@ -383,6 +396,21 @@ double Dexter::get_bandwidth(void) const return 0; } +template +static void attr_to_stat( + Dexter::run_statistics_t& rs, iio_device *dexter_dsp_tx, + const char* attr_name, const char* stat_name) { + long long attr_value = 0; + int r = 0; + if ((r = iio_device_attr_read_longlong(dexter_dsp_tx, attr_name, &attr_value)) == 0) { + rs[stat_name] = (T)attr_value; + } + else { + rs[stat_name] = (ssize_t)-1; + etiLog.level(error) << "Failed to get dexter_dsp_tx." << attr_name << ": " << get_iio_error(r); + } +}; + SDRDevice::run_statistics_t Dexter::get_run_statistics(void) const { run_statistics_t rs; @@ -393,24 +421,25 @@ SDRDevice::run_statistics_t Dexter::get_run_statistics(void) const rs["latepackets"] = num_late; rs["frames"] = num_frames_modulated; - auto attr_to_stat = [&](const char* attr_name, const char* stat_name) { - long long attr_value = 0; - int r = 0; - if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, attr_name, &attr_value)) == 0) { - rs[stat_name] = (size_t)attr_value; - } - else { - rs[stat_name] = (ssize_t)-1; - etiLog.level(error) << "Failed to get dexter_dsp_tx." << attr_name << ": " << get_iio_error(r); - } - }; + attr_to_stat(rs, m_dexter_dsp_tx, "clks", "clks"); + attr_to_stat(rs, m_dexter_dsp_tx, "stream0_fifo_not_empty_clks", "fifo_not_empty_clks"); + attr_to_stat(rs, m_dexter_dsp_tx, "gpsdo_locked", "gpsdo_locked"); + attr_to_stat(rs, m_dexter_dsp_tx, "pps_clk_error_hz", "pps_clk_error_hz"); + attr_to_stat(rs, m_dexter_dsp_tx, "pps_cnt", "pps_cnt"); + attr_to_stat(rs, m_dexter_dsp_tx, "pps_loss_of_signal", "pps_loss_of_signal"); + attr_to_stat(rs, m_dexter_dsp_tx, "dsp_version", "dsp_version"); - attr_to_stat("clks", "clks"); - attr_to_stat("stream0_fifo_not_empty_clks", "fifo_not_empty_clks"); - attr_to_stat("gpsdo_locked", "gpsdo_locked"); - attr_to_stat("pps_clk_error_hz", "pps_clk_error_hz"); - attr_to_stat("pps_cnt", "pps_cnt"); - attr_to_stat("dsp_version", "dsp_version"); + rs["in_holdover_since"] = 0; + switch (m_clock_state) { + case DexterClockState::Startup: + rs["clock_state"] = "startup"; break; + case DexterClockState::Normal: + rs["clock_state"] = "normal"; break; + case DexterClockState::Holdover: + rs["clock_state"] = "holdover"; + rs["in_holdover_since"] = m_holdover_since_t; + break; + } constexpr double VMINFACT = 0.85; constexpr double VMAXFACT = 1.15; @@ -589,9 +618,11 @@ size_t Dexter::receive_frame( } -bool Dexter::is_clk_source_ok() const +bool Dexter::is_clk_source_ok() { - return true; + handle_hw_time(); + + return m_clock_state != DexterClockState::Startup; } const char* Dexter::device_name(void) const @@ -623,8 +654,6 @@ void Dexter::transmit_frame(struct FrameData&& frame) const bool require_timestamped_tx = (m_conf.enableSync and frame.ts.timestamp_valid); - handle_hw_time(); - if (not m_channel_is_up) { if (require_timestamped_tx) { if (m_clock_state == DexterClockState::Startup) { diff --git a/src/output/Dexter.h b/src/output/Dexter.h index f70bb14..d4f425f 100644 --- a/src/output/Dexter.h +++ b/src/output/Dexter.h @@ -84,7 +84,7 @@ class Dexter : public Output::SDRDevice double timeout_secs) override; // Return true if GPS and reference clock inputs are ok - virtual bool is_clk_source_ok() const override; + virtual bool is_clk_source_ok() override; virtual const char* device_name() const override; virtual std::optional get_temperature() const override; @@ -129,6 +129,7 @@ class Dexter : public Output::SDRDevice // Only valid when m_clock_state Holdover std::chrono::steady_clock::time_point m_holdover_since = std::chrono::steady_clock::time_point::min(); + std::time_t m_holdover_since_t = 0; }; } // namespace Output diff --git a/src/output/Lime.cpp b/src/output/Lime.cpp index 83c54ad..8fb90bb 100644 --- a/src/output/Lime.cpp +++ b/src/output/Lime.cpp @@ -361,7 +361,7 @@ size_t Lime::receive_frame( return 0; } -bool Lime::is_clk_source_ok() const +bool Lime::is_clk_source_ok() { // TODO return true; diff --git a/src/output/Lime.h b/src/output/Lime.h index e09e82d..4510bf2 100644 --- a/src/output/Lime.h +++ b/src/output/Lime.h @@ -79,7 +79,7 @@ class Lime : public Output::SDRDevice double timeout_secs) override; // Return true if GPS and reference clock inputs are ok - virtual bool is_clk_source_ok(void) const override; + virtual bool is_clk_source_ok(void) override; virtual const char *device_name(void) const override; virtual std::optional get_temperature(void) const override; diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index 860d8ed..11321f2 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -258,44 +258,19 @@ const char* SDR::name() return m_name.c_str(); } -void SDR::sleep_through_frame() -{ - using namespace std::chrono; - - const auto now = steady_clock::now(); - - if (not t_last_frame_initialised) { - t_last_frame = now; - t_last_frame_initialised = true; - } - - const auto delta = now - t_last_frame; - const auto wait_time = transmission_frame_duration(m_config.dabMode); - - if (wait_time > delta) { - this_thread::sleep_for(wait_time - delta); - } - - t_last_frame += wait_time; -} void SDR::handle_frame(struct FrameData&& frame) { // Assumes m_device is valid if (not m_device->is_clk_source_ok()) { - sleep_through_frame(); return; } const auto& time_spec = frame.ts; - if (m_config.enableSync and m_config.muteNoTimestamps and - not time_spec.timestamp_valid) { - sleep_through_frame(); - etiLog.log(info, - "OutputSDR: Muting sample %d : no timestamp\n", - frame.ts.fct); + if (m_config.enableSync and m_config.muteNoTimestamps and not time_spec.timestamp_valid) { + etiLog.log(info, "OutputSDR: Muting sample %d : no timestamp\n", frame.ts.fct); return; } diff --git a/src/output/SDR.h b/src/output/SDR.h index 9f08348..94c972b 100644 --- a/src/output/SDR.h +++ b/src/output/SDR.h @@ -72,7 +72,6 @@ class SDR : public ModOutput, public ModMetadata, public RemoteControllable { private: void process_thread_entry(void); void handle_frame(struct FrameData&& frame); - void sleep_through_frame(void); SDRDeviceConfig& m_config; @@ -91,9 +90,6 @@ class SDR : public ModOutput, public ModMetadata, public RemoteControllable { uint32_t last_tx_second = 0; uint32_t last_tx_pps = 0; size_t num_queue_overflows = 0; - - bool t_last_frame_initialised = false; - std::chrono::steady_clock::time_point t_last_frame; }; } diff --git a/src/output/SDRDevice.h b/src/output/SDRDevice.h index 628372a..26272be 100644 --- a/src/output/SDRDevice.h +++ b/src/output/SDRDevice.h @@ -135,7 +135,7 @@ class SDRDevice { virtual std::optional get_temperature(void) const = 0; // Return true if GPS and reference clock inputs are ok - virtual bool is_clk_source_ok(void) const = 0; + virtual bool is_clk_source_ok(void) = 0; virtual const char* device_name(void) const = 0; diff --git a/src/output/Soapy.cpp b/src/output/Soapy.cpp index 00df9dc..4d33e39 100644 --- a/src/output/Soapy.cpp +++ b/src/output/Soapy.cpp @@ -254,7 +254,7 @@ size_t Soapy::receive_frame( } -bool Soapy::is_clk_source_ok() const +bool Soapy::is_clk_source_ok() { // TODO return true; diff --git a/src/output/Soapy.h b/src/output/Soapy.h index b98ac21..4fce11a 100644 --- a/src/output/Soapy.h +++ b/src/output/Soapy.h @@ -78,7 +78,7 @@ class Soapy : public Output::SDRDevice double timeout_secs) override; // Return true if GPS and reference clock inputs are ok - virtual bool is_clk_source_ok(void) const override; + virtual bool is_clk_source_ok(void) override; virtual const char* device_name(void) const override; virtual std::optional get_temperature(void) const override; diff --git a/src/output/UHD.cpp b/src/output/UHD.cpp index 7f07ff2..6638b6c 100644 --- a/src/output/UHD.cpp +++ b/src/output/UHD.cpp @@ -439,7 +439,7 @@ size_t UHD::receive_frame( } // Return true if GPS and reference clock inputs are ok -bool UHD::is_clk_source_ok(void) const +bool UHD::is_clk_source_ok(void) { bool ok = true; diff --git a/src/output/UHD.h b/src/output/UHD.h index 97a821e..9891c7a 100644 --- a/src/output/UHD.h +++ b/src/output/UHD.h @@ -84,7 +84,7 @@ class UHD : public Output::SDRDevice double timeout_secs) override; // Return true if GPS and reference clock inputs are ok - virtual bool is_clk_source_ok(void) const override; + virtual bool is_clk_source_ok(void) override; virtual const char* device_name(void) const override; virtual std::optional get_temperature(void) const override; -- cgit v1.2.3 From 829863ab82ca3743c64c76310af5eebcaab1fc84 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 5 May 2023 14:26:16 +0200 Subject: Dexter: Only do handle_hw_time when synchronous is enabled --- src/output/Dexter.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/output') diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index 25de030..c1d8d2f 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -620,9 +620,13 @@ size_t Dexter::receive_frame( bool Dexter::is_clk_source_ok() { - handle_hw_time(); - - return m_clock_state != DexterClockState::Startup; + if (m_conf.enableSync) { + handle_hw_time(); + return m_clock_state != DexterClockState::Startup; + } + else { + return true; + } } const char* Dexter::device_name(void) const -- cgit v1.2.3 From 150b75b244602c789934f1a5094f33aa7da3c09a Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 6 Jun 2023 12:10:39 +0200 Subject: DEXTER: Add pacontrol interface --- doc/example.ini | 6 +++++ src/MemlessPoly.cpp | 4 +-- src/output/Dexter.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++++- src/output/Dexter.h | 14 +++++++++- src/output/SDRDevice.h | 4 +++ 5 files changed, 95 insertions(+), 4 deletions(-) (limited to 'src/output') diff --git a/doc/example.ini b/doc/example.ini index cd48ef4..4cc6d26 100644 --- a/doc/example.ini +++ b/doc/example.ini @@ -320,6 +320,12 @@ txgain=32768 ;frequency=234208000 channel=13C +pacontrol_config_endpoint=tcp://localhost:5558 + +; TargetPower in dBm +; Runtime-changes to this setting are directly sent to pacontrol +pacontrol_targetpower=33 + [limeoutput] ; Lime output directly runs against the LMS device driver. It does not support SFN nor predistortion. device= diff --git a/src/MemlessPoly.cpp b/src/MemlessPoly.cpp index 4801ba0..a2b0082 100644 --- a/src/MemlessPoly.cpp +++ b/src/MemlessPoly.cpp @@ -314,7 +314,7 @@ void MemlessPoly::worker_thread(MemlessPoly::worker_t *workerdata) set_thread_name("MemlessPoly"); while (true) { - worker_t::input_data_t in_data; + worker_t::input_data_t in_data = {}; try { workerdata->in_queue.wait_and_pop(in_data); } @@ -386,7 +386,7 @@ int MemlessPoly::internal_process(Buffer* const dataIn, Buffer* dataOut) // Wait for completion of the tasks for (auto& worker : m_workers) { - int ret; + int ret = 0; worker.out_queue.wait_and_pop(ret); } } diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index c1d8d2f..dd51517 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -66,9 +66,35 @@ static void fill_time(struct timespec *t) } } +static void pacontrol_set_targetpower(zmq::socket_t& sock, int targetpower_dBm) +{ + stringstream message_builder; + message_builder << "{'PA0' : {'TargetPower': "; + message_builder << targetpower_dBm; + message_builder << "}}"; + const auto message = message_builder.str(); + + try { + const auto r = sock.send(zmq::const_buffer{message.data(), message.size()}); + if (r.has_value()) { + etiLog.level(debug) << "Sent TargetPower=" << targetpower_dBm << " to pacontrol"; + } + else { + // zmq_send returned EAGAIN + etiLog.level(info) << "Send TargetPower=" << targetpower_dBm << " failed"; + } + } + catch (const zmq::error_t& err) { + etiLog.level(warn) << "Failed to send TargetPower=" << targetpower_dBm << ": " << err.what(); + } +} + + Dexter::Dexter(SDRDeviceConfig& config) : SDRDevice(), - m_conf(config) + m_conf(config), + m_zmq_context(1), + m_zmq_sock(m_zmq_context, ZMQ_PUSH) { etiLog.level(info) << "Dexter:Creating the device"; @@ -166,6 +192,41 @@ Dexter::Dexter(SDRDeviceConfig& config) : m_running = true; m_underflow_read_thread = std::thread(&Dexter::underflow_read_process, this); + + m_zmq_sock.setsockopt(ZMQ_SNDTIMEO, 0); + if (not m_conf.pacontrol_config_endpoint.empty()) { + etiLog.level(debug) << "Creating pacontrol connection to " << + m_conf.pacontrol_config_endpoint; + m_zmq_sock.connect(m_conf.pacontrol_config_endpoint.c_str()); + } + + if (m_conf.pacontrol_targetpower.has_value()) { + pacontrol_set_targetpower(m_zmq_sock, *m_conf.pacontrol_targetpower); + } + else { + etiLog.level(warn) << "Config does not defined PA target power"; + } +} + +static void pacontrol_set_mute(zmq::socket_t& sock, bool mute) +{ + string message = "{'PA0' : {'Mute': "; + message += (mute ? "True" : "False"); + message += "}}"; + + try { + const auto r = sock.send(zmq::const_buffer{message.data(), message.size()}); + if (r.has_value()) { + etiLog.level(debug) << "Sent mute=" << mute << " to pacontrol"; + } + else { + // zmq_send returned EAGAIN + etiLog.level(info) << "Send mute=" << mute << " failed"; + } + } + catch (const zmq::error_t& err) { + etiLog.level(warn) << "Failed to send mute=" << mute << ": " << err.what(); + } } void Dexter::channel_up() @@ -178,6 +239,10 @@ void Dexter::channel_up() m_channel_is_up = true; etiLog.level(debug) << "DEXTER CHANNEL_UP"; + + if (m_zmq_sock.connected()) { + pacontrol_set_mute(m_zmq_sock, false); + } } void Dexter::channel_down() @@ -194,6 +259,10 @@ void Dexter::channel_down() m_channel_is_up = false; etiLog.level(debug) << "DEXTER CHANNEL_DOWN"; + + if (m_zmq_sock.connected()) { + pacontrol_set_mute(m_zmq_sock, true); + } } void Dexter::handle_hw_time() diff --git a/src/output/Dexter.h b/src/output/Dexter.h index d4f425f..57b9798 100644 --- a/src/output/Dexter.h +++ b/src/output/Dexter.h @@ -34,8 +34,14 @@ DESCRIPTION: # include #endif -#ifdef HAVE_DEXTER +#if defined(HAVE_DEXTER) + +#if !defined(HAVE_ZEROMQ) +#error "ZeroMQ is mandatory for DEXTER" +#endif + #include "iio.h" +#include "zmq.hpp" #include #include @@ -120,6 +126,12 @@ class Dexter : public Output::SDRDevice size_t num_buffers_pushed = 0; + /* Communication with pacontrol */ + zmq::context_t m_zmq_context; + zmq::socket_t m_zmq_sock; + std::string m_pacontrol_endpoint; + + /* Clock State */ DexterClockState m_clock_state = DexterClockState::Startup; // Only valid when m_clock_state is not Startup diff --git a/src/output/SDRDevice.h b/src/output/SDRDevice.h index 26272be..f84b340 100644 --- a/src/output/SDRDevice.h +++ b/src/output/SDRDevice.h @@ -94,6 +94,10 @@ struct SDRDeviceConfig { // TCP port on which to serve TX and RX samples for the // digital pre distortion learning tool uint16_t dpdFeedbackServerPort = 0; + + // DEXTER-specific + std::string pacontrol_config_endpoint; + std::optional pacontrol_targetpower; // dBm }; // Each frame contains one OFDM frame, and its -- cgit v1.2.3 From 71740c7ea14706986507159f8a0e101940f99d4d Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 27 Jun 2023 11:43:25 +0200 Subject: Add a few cosmetic changes --- lib/RemoteControl.cpp | 2 +- src/output/Dexter.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/output') diff --git a/lib/RemoteControl.cpp b/lib/RemoteControl.cpp index 6e9af20..16359ad 100644 --- a/lib/RemoteControl.cpp +++ b/lib/RemoteControl.cpp @@ -193,7 +193,7 @@ RemoteControllable* RemoteControllers::get_controllable_(const std::string& name [&](RemoteControllable* r) { return r->get_rc_name() == name; }); if (rc == controllables.end()) { - throw ParameterError("Module name unknown"); + throw ParameterError(string{"Module name '"} + name + "' unknown"); } else { return *rc; diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index dd51517..9c16ea7 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -204,7 +204,7 @@ Dexter::Dexter(SDRDeviceConfig& config) : pacontrol_set_targetpower(m_zmq_sock, *m_conf.pacontrol_targetpower); } else { - etiLog.level(warn) << "Config does not defined PA target power"; + etiLog.level(warn) << "Config does not define PA target power"; } } -- cgit v1.2.3 From 8a547d0c0a84ea5a3464c7bc82a45f78aaae81c0 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 18 Jul 2023 17:08:04 +0200 Subject: Dexter: remove dexter_dsp_tx attributes from metrics --- src/output/Dexter.cpp | 156 -------------------------------------------------- 1 file changed, 156 deletions(-) (limited to 'src/output') diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index 9c16ea7..132636c 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -465,21 +465,6 @@ double Dexter::get_bandwidth(void) const return 0; } -template -static void attr_to_stat( - Dexter::run_statistics_t& rs, iio_device *dexter_dsp_tx, - const char* attr_name, const char* stat_name) { - long long attr_value = 0; - int r = 0; - if ((r = iio_device_attr_read_longlong(dexter_dsp_tx, attr_name, &attr_value)) == 0) { - rs[stat_name] = (T)attr_value; - } - else { - rs[stat_name] = (ssize_t)-1; - etiLog.level(error) << "Failed to get dexter_dsp_tx." << attr_name << ": " << get_iio_error(r); - } -}; - SDRDevice::run_statistics_t Dexter::get_run_statistics(void) const { run_statistics_t rs; @@ -490,14 +475,6 @@ SDRDevice::run_statistics_t Dexter::get_run_statistics(void) const rs["latepackets"] = num_late; rs["frames"] = num_frames_modulated; - attr_to_stat(rs, m_dexter_dsp_tx, "clks", "clks"); - attr_to_stat(rs, m_dexter_dsp_tx, "stream0_fifo_not_empty_clks", "fifo_not_empty_clks"); - attr_to_stat(rs, m_dexter_dsp_tx, "gpsdo_locked", "gpsdo_locked"); - attr_to_stat(rs, m_dexter_dsp_tx, "pps_clk_error_hz", "pps_clk_error_hz"); - attr_to_stat(rs, m_dexter_dsp_tx, "pps_cnt", "pps_cnt"); - attr_to_stat(rs, m_dexter_dsp_tx, "pps_loss_of_signal", "pps_loss_of_signal"); - attr_to_stat(rs, m_dexter_dsp_tx, "dsp_version", "dsp_version"); - rs["in_holdover_since"] = 0; switch (m_clock_state) { case DexterClockState::Startup: @@ -510,142 +487,9 @@ SDRDevice::run_statistics_t Dexter::get_run_statistics(void) const break; } - constexpr double VMINFACT = 0.85; - constexpr double VMAXFACT = 1.15; - bool voltage_ok = true; - bool temp_ok = true; - - { - std::ifstream in("/sys/bus/i2c/devices/1-002f/hwmon/hwmon0/in2_input", std::ios::in | std::ios::binary); - if (in) { - double vcc3v3; - in >> vcc3v3; - rs["vcc3v3"] = vcc3v3 * (18+36)/36.0/1000.0; - voltage_ok = (vcc3v3 > VMINFACT * 3.3) and (vcc3v3 < VMAXFACT * 3.3); - } - else { - rs["vcc3v3"] = -1; - voltage_ok = false; - } - } - { - std::ifstream in("/sys/bus/i2c/devices/1-002f/hwmon/hwmon0/in1_input", std::ios::in | std::ios::binary); - if (in) { - double vcc5v4; - in >> vcc5v4; - rs["vcc5v4"] = vcc5v4 * (51+36)/36.0/1000.0; - voltage_ok = (vcc5v4 > VMINFACT * 5.4) and (vcc5v4 < VMAXFACT * 5.4); - } - else { - rs["vcc5v4"] = -1; - voltage_ok = false; - } - } - { - std::ifstream in("/sys/bus/i2c/devices/1-002f/hwmon/hwmon0/in3_input", std::ios::in | std::ios::binary); - if (in) { - double vfan; - in >> vfan; - rs["vfan"] = vfan * (560+22)/22.0/1000.0; - } - else { - rs["vfan"] = -1; - voltage_ok = false; - } - } - { - std::ifstream in("/sys/bus/i2c/devices/1-002f/hwmon/hwmon0/in0_input", std::ios::in | std::ios::binary); - if (in) { - double vccmainin; - in >> vccmainin; - rs["vcc_main_in"] = vccmainin * (560+22)/22.0/1000.0; - voltage_ok |= vccmainin > 10.0; - } - else { - rs["vcc_main_in"] = -1; - voltage_ok = false; - } - } - { - std::ifstream in("/sys/bus/i2c/devices/1-002f/hwmon/hwmon0/in4_input", std::ios::in | std::ios::binary); - if (in) { - double vcc3v3pll; - in >> vcc3v3pll; - rs["vcc3v3pll"] = vcc3v3pll * (18+36)/36.0/1000.0; - voltage_ok = (vcc3v3pll > VMINFACT * 3.3) and (vcc3v3pll < VMAXFACT * 3.3); - } - else { - rs["vcc3v3pll"] = -1; - voltage_ok = false; - } - } - { - std::ifstream in("/sys/bus/i2c/devices/1-002f/hwmon/hwmon0/in5_input", std::ios::in | std::ios::binary); - if (in) { - double vcc2v5io; - in >> vcc2v5io; - rs["vcc2v5io"] = vcc2v5io * (4.7+36)/36.0/1000.0; - voltage_ok = (vcc2v5io > VMINFACT * 2.5) and (vcc2v5io < VMAXFACT * 2.5); - } - else { - rs["vcc2v5io"] = -1; - voltage_ok = false; - } - } - { - std::ifstream in("/sys/bus/i2c/devices/1-002f/hwmon/hwmon0/in6_input", std::ios::in | std::ios::binary); - if (in) { - double vccocxo; - in >> vccocxo; - rs["vccocxo"] = vccocxo * (51+36)/36.0/1000.0; - } - else { - rs["vccocxo"] = -1; - voltage_ok = false; - } - } - - optional tfpga; - for (int i = 0; i < 100; i++) { - std::string path = "/sys/bus/iio/devices/iio:device"; - path += to_string(i); - - std::ifstream iio_name(path + "/name", std::ios::in | std::ios::binary); - std::ostringstream sstr; - sstr << iio_name.rdbuf(); - if (sstr.str() == "xadc\n") { - std::ifstream in_scale(path + "/in_temp0_scale", std::ios::in | std::ios::binary); - std::ifstream in_offset(path + "/in_temp0_offset", std::ios::in | std::ios::binary); - std::ifstream in_temp0_raw(path + "/in_temp0_raw", std::ios::in | std::ios::binary); - - if (in_scale and in_offset and in_temp0_raw) { - double scale, offset, temp0_raw ; - in_scale >> scale; - in_offset >> offset; - in_temp0_raw >> temp0_raw; - - tfpga = (temp0_raw + offset) * scale / 1000.0; - } - break; - } - } - - if (tfpga) { - rs["tempfpga"] = *tfpga; - temp_ok |= *tfpga <= 85; - } - else { - rs["tempfpga"] = -1; - temp_ok = false; - } - - rs["voltage_alarm"] = not voltage_ok; - rs["temp_alarm"] = not temp_ok; - return rs; } - double Dexter::get_real_secs(void) const { long long clks = 0; -- cgit v1.2.3 From d521d4f0c5ad3b663a322453c5798626081cb1f3 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 19 Jul 2023 20:26:36 +0200 Subject: Change RC showjson command --- Makefile.am | 2 + lib/Json.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++ lib/Json.h | 66 +++++++++++++++++++++++++++ lib/RemoteControl.cpp | 94 ++++++++------------------------------ lib/RemoteControl.h | 8 ++-- src/DabModulator.cpp | 4 +- src/DabModulator.h | 2 +- src/FIRFilter.cpp | 4 +- src/FIRFilter.h | 2 +- src/GainControl.cpp | 4 +- src/GainControl.h | 2 +- src/GuardIntervalInserter.cpp | 4 +- src/GuardIntervalInserter.h | 2 +- src/MemlessPoly.cpp | 4 +- src/MemlessPoly.h | 2 +- src/OfdmGenerator.cpp | 4 +- src/OfdmGenerator.h | 2 +- src/TII.cpp | 4 +- src/TII.h | 2 +- src/TimestampDecoder.cpp | 4 +- src/TimestampDecoder.h | 2 +- src/output/SDR.cpp | 6 +-- src/output/SDR.h | 2 +- src/output/SDRDevice.h | 2 +- 24 files changed, 220 insertions(+), 110 deletions(-) create mode 100644 lib/Json.cpp create mode 100644 lib/Json.h (limited to 'src/output') diff --git a/Makefile.am b/Makefile.am index 0e09236..6e7c9ce 100644 --- a/Makefile.am +++ b/Makefile.am @@ -98,6 +98,8 @@ odr_dabmod_SOURCES = src/DabMod.cpp \ lib/RemoteControl.h \ lib/Log.cpp \ lib/Log.h \ + lib/Json.h \ + lib/Json.cpp \ lib/Globals.cpp \ lib/INIReader.h \ lib/crc.h \ diff --git a/lib/Json.cpp b/lib/Json.cpp new file mode 100644 index 0000000..9bda8c3 --- /dev/null +++ b/lib/Json.cpp @@ -0,0 +1,102 @@ +/* + Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 + Her Majesty the Queen in Right of Canada (Communications Research + Center Canada) + + Copyright (C) 2023 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://www.opendigitalradio.org + */ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ +#include +#include +#include +#include +#include +#include +#include + +#include "Json.h" + +namespace json { + static std::string escape_json(const std::string &s) { + std::ostringstream o; + for (auto c = s.cbegin(); c != s.cend(); c++) { + switch (*c) { + case '"': o << "\\\""; break; + case '\\': o << "\\\\"; break; + case '\b': o << "\\b"; break; + case '\f': o << "\\f"; break; + case '\n': o << "\\n"; break; + case '\r': o << "\\r"; break; + case '\t': o << "\\t"; break; + default: + if ('\x00' <= *c && *c <= '\x1f') { + o << "\\u" + << std::hex << std::setw(4) << std::setfill('0') << static_cast(*c); + } else { + o << *c; + } + } + } + return o.str(); + } + + std::string map_to_json(const map_t& values) { + std::ostringstream ss; + ss << "{ "; + size_t ix = 0; + for (const auto& element : values) { + if (ix > 0) { + ss << ","; + } + + ss << "\"" << escape_json(element.first) << "\": "; + + const auto& value = element.second.data; + if (std::holds_alternative(value)) { + ss << "\"" << escape_json(std::get(value)) << "\""; + } + else if (std::holds_alternative(value)) { + ss << std::defaultfloat << std::get(value); + } + else if (std::holds_alternative(value)) { + ss << std::get(value); + } + else if (std::holds_alternative(value)) { + ss << std::get(value); + } + else if (std::holds_alternative(value)) { + ss << (std::get(value) ? "true" : "false"); + } + else if (std::holds_alternative(value)) { + ss << "null"; + } + else if (std::holds_alternative(value)) { + ss << map_to_json(std::get(value)); + } + else { + throw std::logic_error("variant alternative not handled"); + } + + ix++; + } + ss << " }"; + + return ss.str(); + } +} diff --git a/lib/Json.h b/lib/Json.h new file mode 100644 index 0000000..26da9a8 --- /dev/null +++ b/lib/Json.h @@ -0,0 +1,66 @@ +/* + Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 + Her Majesty the Queen in Right of Canada (Communications Research + Center Canada) + + Copyright (C) 2023 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://www.opendigitalradio.org + + This module adds remote-control capability to some of the dabmux/dabmod modules. + */ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#pragma once + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include +#include +#include +#include +#include + +namespace json { + + using map_t = std::unordered_map; + + struct value_t { + std::variant< + map_t, + std::string, + double, + size_t, + ssize_t, + bool, + std::nullopt_t> data; + + template + value_t operator=(const T& map) { + value_t v; + v.data = map; + return v; + } + + }; + + std::string map_to_json(const map_t& values); +} diff --git a/lib/RemoteControl.cpp b/lib/RemoteControl.cpp index fd0ea77..b544461 100644 --- a/lib/RemoteControl.cpp +++ b/lib/RemoteControl.cpp @@ -105,71 +105,14 @@ std::list< std::vector > RemoteControllers::get_param_list_values(c } -static std::string escape_json(const std::string &s) { - std::ostringstream o; - for (auto c = s.cbegin(); c != s.cend(); c++) { - switch (*c) { - case '"': o << "\\\""; break; - case '\\': o << "\\\\"; break; - case '\b': o << "\\b"; break; - case '\f': o << "\\f"; break; - case '\n': o << "\\n"; break; - case '\r': o << "\\r"; break; - case '\t': o << "\\t"; break; - default: - if ('\x00' <= *c && *c <= '\x1f') { - o << "\\u" - << std::hex << std::setw(4) << std::setfill('0') << static_cast(*c); - } else { - o << *c; - } - } - } - return o.str(); -} - -std::string RemoteControllers::get_params_json(const std::string& name) { - RemoteControllable* controllable = get_controllable_(name); - const auto& values = controllable->get_all_values(); - - std::ostringstream ss; - ss << "{ "; - size_t ix = 0; - for (const auto& element : values) { - if (ix > 0) { - ss << ","; - } - - ss << "\"" << escape_json(element.first) << "\": "; - - const auto& value = element.second; - if (std::holds_alternative(value)) { - ss << "\"" << escape_json(std::get(value)) << "\""; - } - else if (std::holds_alternative(value)) { - ss << std::defaultfloat << std::get(value); - } - else if (std::holds_alternative(value)) { - ss << std::get(value); - } - else if (std::holds_alternative(value)) { - ss << std::get(value); - } - else if (std::holds_alternative(value)) { - ss << (std::get(value) ? "true" : "false"); - } - else if (std::holds_alternative(value)) { - ss << "null"; - } - else { - throw std::logic_error("variant alternative not handled"); - } - ix++; +std::string RemoteControllers::get_showjson() { + json::map_t root; + for (auto &controllable : rcs.controllables) { + root[controllable->get_rc_name()].data = controllable->get_all_values(); } - ss << " }"; - return ss.str(); + return json::map_to_json(root); } std::string RemoteControllers::get_param(const std::string& name, const std::string& param) { @@ -590,6 +533,19 @@ void RemoteControllerZmq::process() repSocket.send(zmsg, (--cohort_size > 0) ? zmq::send_flags::sndmore : zmq::send_flags::none); } } + else if (msg.size() == 1 && command == "showjson") { + try { + std::string json = rcs.get_showjson(); + + zmq::message_t zmsg(json.size()); + memcpy(zmsg.data(), json.data(), json.size()); + + repSocket.send(zmsg, zmq::send_flags::none); + } + catch (const ParameterError &err) { + send_fail_reply(repSocket, err.what()); + } + } else if (msg.size() == 2 && command == "show") { const std::string module((char*) msg[1].data(), msg[1].size()); try { @@ -608,20 +564,6 @@ void RemoteControllerZmq::process() send_fail_reply(repSocket, err.what()); } } - else if (msg.size() == 2 && command == "showjson") { - const std::string module((char*) msg[1].data(), msg[1].size()); - try { - std::string json = rcs.get_params_json(module); - - zmq::message_t zmsg(json.size()); - memcpy(zmsg.data(), json.data(), json.size()); - - repSocket.send(zmsg, zmq::send_flags::none); - } - catch (const ParameterError &err) { - send_fail_reply(repSocket, err.what()); - } - } else if (msg.size() == 3 && command == "get") { const std::string module((char*) msg[1].data(), msg[1].size()); const std::string parameter((char*) msg[2].data(), msg[2].size()); diff --git a/lib/RemoteControl.h b/lib/RemoteControl.h index 4bc3b68..26f30d9 100644 --- a/lib/RemoteControl.h +++ b/lib/RemoteControl.h @@ -48,6 +48,7 @@ #include "Log.h" #include "Socket.h" +#include "Json.h" #define RC_ADD_PARAMETER(p, desc) { \ std::vector p; \ @@ -120,10 +121,7 @@ class RemoteControllable { /* Getting a parameter always returns a string. */ virtual const std::string get_parameter(const std::string& parameter) const = 0; - using value_t = std::variant; - using map_t = std::unordered_map; - - virtual const map_t get_all_values() const = 0; + virtual const json::map_t get_all_values() const = 0; protected: std::string m_rc_name; @@ -140,7 +138,7 @@ class RemoteControllers { void check_faults(); std::list< std::vector > get_param_list_values(const std::string& name); std::string get_param(const std::string& name, const std::string& param); - std::string get_params_json(const std::string& name); + std::string get_showjson(); void set_param( const std::string& name, diff --git a/src/DabModulator.cpp b/src/DabModulator.cpp index 5213d8d..0fe9c6d 100644 --- a/src/DabModulator.cpp +++ b/src/DabModulator.cpp @@ -435,9 +435,9 @@ const string DabModulator::get_parameter(const string& parameter) const return ss.str(); } -const RemoteControllable::map_t DabModulator::get_all_values() const +const json::map_t DabModulator::get_all_values() const { - map_t map; + json::map_t map; map["rate"] = m_settings.outputRate; map["num_clipped_samples"] = m_formatConverter ? m_formatConverter->get_num_clipped_samples() : 0; return map; diff --git a/src/DabModulator.h b/src/DabModulator.h index 6381252..140f313 100644 --- a/src/DabModulator.h +++ b/src/DabModulator.h @@ -64,7 +64,7 @@ public: /******* REMOTE CONTROL ********/ virtual void set_parameter(const std::string& parameter, const std::string& value) override; virtual const std::string get_parameter(const std::string& parameter) const override; - virtual const RemoteControllable::map_t get_all_values() const override; + virtual const json::map_t get_all_values() const override; protected: void setMode(unsigned mode); diff --git a/src/FIRFilter.cpp b/src/FIRFilter.cpp index 523d405..d2a6121 100644 --- a/src/FIRFilter.cpp +++ b/src/FIRFilter.cpp @@ -347,9 +347,9 @@ const string FIRFilter::get_parameter(const string& parameter) const return ss.str(); } -const RemoteControllable::map_t FIRFilter::get_all_values() const +const json::map_t FIRFilter::get_all_values() const { - map_t map; + json::map_t map; map["ntaps"] = m_taps.size(); map["tapsfile"] = m_taps_file; return map; diff --git a/src/FIRFilter.h b/src/FIRFilter.h index 2a469aa..a4effa1 100644 --- a/src/FIRFilter.h +++ b/src/FIRFilter.h @@ -61,7 +61,7 @@ public: /******* REMOTE CONTROL ********/ virtual void set_parameter(const std::string& parameter, const std::string& value) override; virtual const std::string get_parameter(const std::string& parameter) const override; - virtual const RemoteControllable::map_t get_all_values() const override; + virtual const json::map_t get_all_values() const override; protected: virtual int internal_process(Buffer* const dataIn, Buffer* dataOut) override; diff --git a/src/GainControl.cpp b/src/GainControl.cpp index d90da45..beb93f6 100644 --- a/src/GainControl.cpp +++ b/src/GainControl.cpp @@ -583,9 +583,9 @@ const string GainControl::get_parameter(const string& parameter) const return ss.str(); } -const RemoteControllable::map_t GainControl::get_all_values() const +const json::map_t GainControl::get_all_values() const { - map_t map; + json::map_t map; map["digital"] = m_digGain; switch (m_gainmode) { case GainMode::GAIN_FIX: diff --git a/src/GainControl.h b/src/GainControl.h index f024fa2..04f6b58 100644 --- a/src/GainControl.h +++ b/src/GainControl.h @@ -66,7 +66,7 @@ class GainControl : public PipelinedModCodec, public RemoteControllable /* Functions for the remote control */ virtual void set_parameter(const std::string& parameter, const std::string& value) override; virtual const std::string get_parameter(const std::string& parameter) const override; - virtual const RemoteControllable::map_t get_all_values() const override; + virtual const json::map_t get_all_values() const override; protected: virtual int internal_process( diff --git a/src/GuardIntervalInserter.cpp b/src/GuardIntervalInserter.cpp index d5c71fb..80394b7 100644 --- a/src/GuardIntervalInserter.cpp +++ b/src/GuardIntervalInserter.cpp @@ -303,9 +303,9 @@ const std::string GuardIntervalInserter::get_parameter(const std::string& parame return ss.str(); } -const RemoteControllable::map_t GuardIntervalInserter::get_all_values() const +const json::map_t GuardIntervalInserter::get_all_values() const { - map_t map; + json::map_t map; map["windowlen"] = d_windowOverlap; return map; } diff --git a/src/GuardIntervalInserter.h b/src/GuardIntervalInserter.h index f88bdac..5aaad2b 100644 --- a/src/GuardIntervalInserter.h +++ b/src/GuardIntervalInserter.h @@ -58,7 +58,7 @@ class GuardIntervalInserter : public ModCodec, public RemoteControllable /******* REMOTE CONTROL ********/ virtual void set_parameter(const std::string& parameter, const std::string& value) override; virtual const std::string get_parameter(const std::string& parameter) const override; - virtual const RemoteControllable::map_t get_all_values() const override; + virtual const json::map_t get_all_values() const override; protected: void update_window(size_t new_window_overlap); diff --git a/src/MemlessPoly.cpp b/src/MemlessPoly.cpp index a2b0082..30d4ce9 100644 --- a/src/MemlessPoly.cpp +++ b/src/MemlessPoly.cpp @@ -467,9 +467,9 @@ const string MemlessPoly::get_parameter(const string& parameter) const return ss.str(); } -const RemoteControllable::map_t MemlessPoly::get_all_values() const +const json::map_t MemlessPoly::get_all_values() const { - map_t map; + json::map_t map; map["ncoefs"] = m_coefs_am.size(); map["coefs"] = serialise_coefficients(); map["coeffile"] = m_coefs_file; diff --git a/src/MemlessPoly.h b/src/MemlessPoly.h index 09adc13..91e6860 100644 --- a/src/MemlessPoly.h +++ b/src/MemlessPoly.h @@ -68,7 +68,7 @@ public: /******* REMOTE CONTROL ********/ virtual void set_parameter(const std::string& parameter, const std::string& value) override; virtual const std::string get_parameter(const std::string& parameter) const override; - virtual const RemoteControllable::map_t get_all_values() const override; + virtual const json::map_t get_all_values() const override; private: int internal_process(Buffer* const dataIn, Buffer* dataOut) override; diff --git a/src/OfdmGenerator.cpp b/src/OfdmGenerator.cpp index d161861..cb799d3 100644 --- a/src/OfdmGenerator.cpp +++ b/src/OfdmGenerator.cpp @@ -458,9 +458,9 @@ const std::string OfdmGenerator::get_parameter(const std::string& parameter) con return ss.str(); } -const RemoteControllable::map_t OfdmGenerator::get_all_values() const +const json::map_t OfdmGenerator::get_all_values() const { - map_t map; + json::map_t map; // TODO needs rework of the values return map; } diff --git a/src/OfdmGenerator.h b/src/OfdmGenerator.h index 90e562a..dc1ad46 100644 --- a/src/OfdmGenerator.h +++ b/src/OfdmGenerator.h @@ -61,7 +61,7 @@ class OfdmGenerator : public ModCodec, public RemoteControllable /* Functions for the remote control */ virtual void set_parameter(const std::string& parameter, const std::string& value) override; virtual const std::string get_parameter(const std::string& parameter) const override; - virtual const RemoteControllable::map_t get_all_values() const override; + virtual const json::map_t get_all_values() const override; protected: struct cfr_iter_stat_t { diff --git a/src/TII.cpp b/src/TII.cpp index b329cdb..9068630 100644 --- a/src/TII.cpp +++ b/src/TII.cpp @@ -385,9 +385,9 @@ const std::string TII::get_parameter(const std::string& parameter) const return ss.str(); } -const RemoteControllable::map_t TII::get_all_values() const +const json::map_t TII::get_all_values() const { - map_t map; + json::map_t map; map["enable"] = m_conf.enable; map["pattern"] = m_conf.pattern; map["comb"] = m_conf.comb; diff --git a/src/TII.h b/src/TII.h index b0ba646..a8d0ca9 100644 --- a/src/TII.h +++ b/src/TII.h @@ -89,7 +89,7 @@ class TII : public ModCodec, public RemoteControllable /******* REMOTE CONTROL ********/ virtual void set_parameter(const std::string& parameter, const std::string& value) override; virtual const std::string get_parameter(const std::string& parameter) const override; - virtual const RemoteControllable::map_t get_all_values() const override; + virtual const json::map_t get_all_values() const override; protected: // Fill m_Acp with the correct carriers for the pattern/comb diff --git a/src/TimestampDecoder.cpp b/src/TimestampDecoder.cpp index 149cd50..4277e55 100644 --- a/src/TimestampDecoder.cpp +++ b/src/TimestampDecoder.cpp @@ -301,9 +301,9 @@ const std::string TimestampDecoder::get_parameter( return ss.str(); } -const RemoteControllable::map_t TimestampDecoder::get_all_values() const +const json::map_t TimestampDecoder::get_all_values() const { - map_t map; + json::map_t map; map["offset"] = timestamp_offset; if (full_timestamp_received) { map["timestamp"] = time_secs + ((double)time_pps / 16384000.0); diff --git a/src/TimestampDecoder.h b/src/TimestampDecoder.h index dc5aa78..b90c328 100644 --- a/src/TimestampDecoder.h +++ b/src/TimestampDecoder.h @@ -120,7 +120,7 @@ class TimestampDecoder : public RemoteControllable /* Base function to set parameters. */ virtual void set_parameter(const std::string& parameter, const std::string& value) override; virtual const std::string get_parameter(const std::string& parameter) const override; - virtual const RemoteControllable::map_t get_all_values() const override; + virtual const json::map_t get_all_values() const override; const char* name() { return "TS"; } diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index 11321f2..4fc3277 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -450,7 +450,7 @@ const string SDR::get_parameter(const string& parameter) const if (m_device) { const auto stat = m_device->get_run_statistics(); try { - const auto& value = stat.at(parameter); + const auto& value = stat.at(parameter).data; if (std::holds_alternative(value)) { ss << std::get(value); } @@ -485,9 +485,9 @@ const string SDR::get_parameter(const string& parameter) const return ss.str(); } -const RemoteControllable::map_t SDR::get_all_values() const +const json::map_t SDR::get_all_values() const { - map_t stat = m_device->get_run_statistics(); + json::map_t stat = m_device->get_run_statistics(); stat["txgain"] = m_config.txgain; stat["rxgain"] = m_config.rxgain; diff --git a/src/output/SDR.h b/src/output/SDR.h index 94c972b..960de0c 100644 --- a/src/output/SDR.h +++ b/src/output/SDR.h @@ -67,7 +67,7 @@ class SDR : public ModOutput, public ModMetadata, public RemoteControllable { virtual const std::string get_parameter( const std::string& parameter) const override; - virtual const RemoteControllable::map_t get_all_values() const override; + virtual const json::map_t get_all_values() const override; private: void process_thread_entry(void); diff --git a/src/output/SDRDevice.h b/src/output/SDRDevice.h index f84b340..f728d8b 100644 --- a/src/output/SDRDevice.h +++ b/src/output/SDRDevice.h @@ -116,7 +116,7 @@ struct FrameData { // All SDR Devices must implement the SDRDevice interface class SDRDevice { public: - using run_statistics_t = RemoteControllable::map_t; + using run_statistics_t = json::map_t; virtual void tune(double lo_offset, double frequency) = 0; virtual double get_tx_freq(void) const = 0; -- cgit v1.2.3 From 343df6eb8792b3efd33f4426766865ae03ccf316 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 19 Jul 2023 22:12:18 +0200 Subject: Add events --- Makefile.am | 2 + doc/receive_events.py | 59 +++++++++++++++++++++++++++++ lib/Json.cpp | 2 +- lib/Json.h | 10 +---- lib/RemoteControl.cpp | 2 +- src/ConfigParser.cpp | 8 +++- src/DabMod.cpp | 3 ++ src/DabModulator.cpp | 4 +- src/Events.cpp | 87 +++++++++++++++++++++++++++++++++++++++++++ src/Events.h | 76 +++++++++++++++++++++++++++++++++++++ src/FIRFilter.cpp | 4 +- src/GainControl.cpp | 10 ++--- src/GuardIntervalInserter.cpp | 2 +- src/MemlessPoly.cpp | 6 +-- src/TII.cpp | 8 ++-- src/TimestampDecoder.cpp | 10 ++--- src/output/Dexter.cpp | 16 ++++---- src/output/SDR.cpp | 16 ++++---- src/output/Soapy.cpp | 8 ++-- src/output/UHD.cpp | 16 ++++---- 20 files changed, 287 insertions(+), 62 deletions(-) create mode 100755 doc/receive_events.py create mode 100644 src/Events.cpp create mode 100644 src/Events.h (limited to 'src/output') diff --git a/Makefile.am b/Makefile.am index 6e7c9ce..5c75c62 100644 --- a/Makefile.am +++ b/Makefile.am @@ -54,6 +54,8 @@ odr_dabmod_SOURCES = src/DabMod.cpp \ src/EtiReader.h \ src/Eti.cpp \ src/Eti.h \ + src/Events.cpp \ + src/Events.h \ src/FicSource.cpp \ src/FicSource.h \ src/PuncturingRule.cpp \ diff --git a/doc/receive_events.py b/doc/receive_events.py new file mode 100755 index 0000000..dca27cd --- /dev/null +++ b/doc/receive_events.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# +# This is an example program that shows +# how to receive runtime events from ODR-DabMod +# +# LICENSE: see bottom of file + +import sys +import zmq +import json +from pprint import pprint + +context = zmq.Context() +sock = context.socket(zmq.SUB) + +ep = "tcp://127.0.0.1:5557" +print(f"Receive from {ep}") +sock.connect(ep) + +# subscribe to all events +sock.setsockopt(zmq.SUBSCRIBE, bytes([])) + +while True: + parts = sock.recv_multipart() + if len(parts) == 2: + print("Received event '{}'".format(parts[0].decode())) + pprint(json.loads(parts[1].decode())) + + else: + print("Received strange event:") + pprint(parts) + + print() + + +# This is free and unencumbered software released into the public domain. +# +# Anyone is free to copy, modify, publish, use, compile, sell, or +# distribute this software, either in source code form or as a compiled +# binary, for any purpose, commercial or non-commercial, and by any +# means. +# +# In jurisdictions that recognize copyright laws, the author or authors +# of this software dedicate any and all copyright interest in the +# software to the public domain. We make this dedication for the benefit +# of the public at large and to the detriment of our heirs and +# successors. We intend this dedication to be an overt act of +# relinquishment in perpetuity of all present and future rights to this +# software under copyright law. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +# OTHER DEALINGS IN THE SOFTWARE. +# +# For more information, please refer to diff --git a/lib/Json.cpp b/lib/Json.cpp index 9bda8c3..da5b078 100644 --- a/lib/Json.cpp +++ b/lib/Json.cpp @@ -67,7 +67,7 @@ namespace json { ss << "\"" << escape_json(element.first) << "\": "; - const auto& value = element.second.data; + const auto& value = element.second.v; if (std::holds_alternative(value)) { ss << "\"" << escape_json(std::get(value)) << "\""; } diff --git a/lib/Json.h b/lib/Json.h index 26da9a8..ee67f35 100644 --- a/lib/Json.h +++ b/lib/Json.h @@ -51,15 +51,7 @@ namespace json { size_t, ssize_t, bool, - std::nullopt_t> data; - - template - value_t operator=(const T& map) { - value_t v; - v.data = map; - return v; - } - + std::nullopt_t> v; }; std::string map_to_json(const map_t& values); diff --git a/lib/RemoteControl.cpp b/lib/RemoteControl.cpp index b544461..fbe0662 100644 --- a/lib/RemoteControl.cpp +++ b/lib/RemoteControl.cpp @@ -109,7 +109,7 @@ std::list< std::vector > RemoteControllers::get_param_list_values(c std::string RemoteControllers::get_showjson() { json::map_t root; for (auto &controllable : rcs.controllables) { - root[controllable->get_rc_name()].data = controllable->get_all_values(); + root[controllable->get_rc_name()].v = controllable->get_all_values(); } return json::map_to_json(root); diff --git a/src/ConfigParser.cpp b/src/ConfigParser.cpp index cb4dc24..68ee74b 100644 --- a/src/ConfigParser.cpp +++ b/src/ConfigParser.cpp @@ -37,6 +37,7 @@ #include "ConfigParser.h" #include "Utils.h" #include "Log.h" +#include "Events.h" #include "DabModulator.h" #include "output/SDR.h" @@ -114,11 +115,16 @@ static void parse_configfile( mod_settings.inputTransport = pt.Get("input.transport", "file"); - mod_settings.edi_max_delay_ms = pt.GetReal("input.edi_max_delay", 0.0f); + mod_settings.edi_max_delay_ms = pt.GetReal("input.edi_max_delay", 0.0); mod_settings.inputName = pt.Get("input.source", "/dev/stdin"); // log parameters: + const string events_endpoint = pt.Get("log.events_endpoint", ""); + if (not events_endpoint.empty()) { + events.bind(events_endpoint); + } + if (pt.GetInteger("log.syslog", 0) == 1) { etiLog.register_backend(make_shared()); } diff --git a/src/DabMod.cpp b/src/DabMod.cpp index 805fab5..fdd9e93 100644 --- a/src/DabMod.cpp +++ b/src/DabMod.cpp @@ -47,6 +47,7 @@ # include #endif +#include "Events.h" #include "Utils.h" #include "Log.h" #include "DabModulator.h" @@ -324,6 +325,8 @@ int launch_modulator(int argc, char* argv[]) mod_settings_t mod_settings; parse_args(argc, argv, mod_settings); + etiLog.register_backend(make_shared()); + etiLog.level(info) << "Configuration parsed. Starting up version " << #if defined(GITVERSION) GITVERSION; diff --git a/src/DabModulator.cpp b/src/DabModulator.cpp index 0fe9c6d..4a29132 100644 --- a/src/DabModulator.cpp +++ b/src/DabModulator.cpp @@ -438,7 +438,7 @@ const string DabModulator::get_parameter(const string& parameter) const const json::map_t DabModulator::get_all_values() const { json::map_t map; - map["rate"] = m_settings.outputRate; - map["num_clipped_samples"] = m_formatConverter ? m_formatConverter->get_num_clipped_samples() : 0; + map["rate"].v = m_settings.outputRate; + map["num_clipped_samples"].v = m_formatConverter ? m_formatConverter->get_num_clipped_samples() : 0; return map; } diff --git a/src/Events.cpp b/src/Events.cpp new file mode 100644 index 0000000..d65b73a --- /dev/null +++ b/src/Events.cpp @@ -0,0 +1,87 @@ +/* + Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 + Her Majesty the Queen in Right of Canada (Communications Research + Center Canada) + + Copyright (C) 2023 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://www.opendigitalradio.org + */ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ +#include +#include +#include +#include +#include +#include +#include + +#include "Events.h" + +EventSender events; + +EventSender::EventSender() : + m_zmq_context(1), + m_socket(m_zmq_context, zmq::socket_type::pub) +{ + int linger = 2000; + m_socket.setsockopt(ZMQ_LINGER, &linger, sizeof(linger)); +} + +EventSender::~EventSender() +{ } + +void EventSender::bind(const std::string& bind_endpoint) +{ + m_socket.bind(bind_endpoint); +} + +void EventSender::send(const std::string& event_name, const json::map_t& detail) +{ + zmq::message_t zmsg1(event_name.data(), event_name.size()); + const auto detail_json = json::map_to_json(detail); + zmq::message_t zmsg2(detail_json.data(), detail_json.size()); + + try { + m_socket.send(zmsg1, zmq::send_flags::sndmore); + m_socket.send(zmsg2, zmq::send_flags::none); + } + catch (const zmq::error_t& err) { + fprintf(stderr, "Cannot send event %s: %s", event_name.c_str(), err.what()); + } +} + + +void LogToEventSender::log(log_level_t level, const std::string& message) +{ + std::string event_name; + if (level == log_level_t::warn) { event_name = "warn"; } + else if (level == log_level_t::error) { event_name = "error"; } + else if (level == log_level_t::alert) { event_name = "alert"; } + else if (level == log_level_t::emerg) { event_name = "emerg"; } + + if (not event_name.empty()) { + json::map_t detail; + detail["message"].v = message; + events.send(event_name, detail); + } +} + +std::string LogToEventSender::get_name() const +{ + return "EventSender"; +} diff --git a/src/Events.h b/src/Events.h new file mode 100644 index 0000000..215c5a8 --- /dev/null +++ b/src/Events.h @@ -0,0 +1,76 @@ +/* + Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 + Her Majesty the Queen in Right of Canada (Communications Research + Center Canada) + + Copyright (C) 2023 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://www.opendigitalradio.org + + This module adds remote-control capability to some of the dabmux/dabmod modules. + */ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#pragma once + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#if defined(HAVE_ZEROMQ) +# include "zmq.hpp" +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include "Log.h" +#include "Json.h" + +class EventSender { + public: + EventSender(); + EventSender(const EventSender& other) = delete; + const EventSender& operator=(const EventSender& other) = delete; + EventSender(EventSender&& other) = delete; + EventSender& operator=(EventSender&& other) = delete; + ~EventSender(); + + void bind(const std::string& bind_endpoint); + + void send(const std::string& event_name, const json::map_t& detail); + private: + zmq::context_t m_zmq_context; + zmq::socket_t m_socket; +}; + +class LogToEventSender: public LogBackend { + public: + virtual ~LogToEventSender() {}; + virtual void log(log_level_t level, const std::string& message); + virtual std::string get_name() const; +}; + +/* events is a singleton used in all parts of the program to output log messages. + * It is constructed in Events.cpp */ +extern EventSender events; + diff --git a/src/FIRFilter.cpp b/src/FIRFilter.cpp index d2a6121..57e7127 100644 --- a/src/FIRFilter.cpp +++ b/src/FIRFilter.cpp @@ -350,7 +350,7 @@ const string FIRFilter::get_parameter(const string& parameter) const const json::map_t FIRFilter::get_all_values() const { json::map_t map; - map["ntaps"] = m_taps.size(); - map["tapsfile"] = m_taps_file; + map["ntaps"].v = m_taps.size(); + map["tapsfile"].v = m_taps_file; return map; } diff --git a/src/GainControl.cpp b/src/GainControl.cpp index beb93f6..84cf065 100644 --- a/src/GainControl.cpp +++ b/src/GainControl.cpp @@ -586,18 +586,18 @@ const string GainControl::get_parameter(const string& parameter) const const json::map_t GainControl::get_all_values() const { json::map_t map; - map["digital"] = m_digGain; + map["digital"].v = m_digGain; switch (m_gainmode) { case GainMode::GAIN_FIX: - map["mode"] = "fix"; + map["mode"].v = "fix"; break; case GainMode::GAIN_MAX: - map["mode"] = "max"; + map["mode"].v = "max"; break; case GainMode::GAIN_VAR: - map["mode"] = "var"; + map["mode"].v = "var"; break; } - map["var"] = m_var_variance_rc; + map["var"].v = m_var_variance_rc; return map; } diff --git a/src/GuardIntervalInserter.cpp b/src/GuardIntervalInserter.cpp index 80394b7..3c2db14 100644 --- a/src/GuardIntervalInserter.cpp +++ b/src/GuardIntervalInserter.cpp @@ -306,6 +306,6 @@ const std::string GuardIntervalInserter::get_parameter(const std::string& parame const json::map_t GuardIntervalInserter::get_all_values() const { json::map_t map; - map["windowlen"] = d_windowOverlap; + map["windowlen"].v = d_windowOverlap; return map; } diff --git a/src/MemlessPoly.cpp b/src/MemlessPoly.cpp index 30d4ce9..184b5bd 100644 --- a/src/MemlessPoly.cpp +++ b/src/MemlessPoly.cpp @@ -470,8 +470,8 @@ const string MemlessPoly::get_parameter(const string& parameter) const const json::map_t MemlessPoly::get_all_values() const { json::map_t map; - map["ncoefs"] = m_coefs_am.size(); - map["coefs"] = serialise_coefficients(); - map["coeffile"] = m_coefs_file; + map["ncoefs"].v = m_coefs_am.size(); + map["coefs"].v = serialise_coefficients(); + map["coeffile"].v = m_coefs_file; return map; } diff --git a/src/TII.cpp b/src/TII.cpp index 9068630..2656cbf 100644 --- a/src/TII.cpp +++ b/src/TII.cpp @@ -388,9 +388,9 @@ const std::string TII::get_parameter(const std::string& parameter) const const json::map_t TII::get_all_values() const { json::map_t map; - map["enable"] = m_conf.enable; - map["pattern"] = m_conf.pattern; - map["comb"] = m_conf.comb; - map["old_variant"] = m_conf.old_variant; + map["enable"].v = m_conf.enable; + map["pattern"].v = m_conf.pattern; + map["comb"].v = m_conf.comb; + map["old_variant"].v = m_conf.old_variant; return map; } diff --git a/src/TimestampDecoder.cpp b/src/TimestampDecoder.cpp index 4277e55..a7972c9 100644 --- a/src/TimestampDecoder.cpp +++ b/src/TimestampDecoder.cpp @@ -304,19 +304,19 @@ const std::string TimestampDecoder::get_parameter( const json::map_t TimestampDecoder::get_all_values() const { json::map_t map; - map["offset"] = timestamp_offset; + map["offset"].v = timestamp_offset; if (full_timestamp_received) { - map["timestamp"] = time_secs + ((double)time_pps / 16384000.0); + map["timestamp"].v = time_secs + ((double)time_pps / 16384000.0); } else { - map["timestamp"] = std::nullopt; + map["timestamp"].v = std::nullopt; } if (full_timestamp_received) { - map["timestamp0"] = time_secs_of_frame0 + ((double)time_pps_of_frame0 / 16384000.0); + map["timestamp0"].v = time_secs_of_frame0 + ((double)time_pps_of_frame0 / 16384000.0); } else { - map["timestamp0"] = std::nullopt; + map["timestamp0"].v = std::nullopt; } return map; } diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index 132636c..e52f774 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -470,20 +470,20 @@ SDRDevice::run_statistics_t Dexter::get_run_statistics(void) const run_statistics_t rs; { std::unique_lock lock(m_attr_thread_mutex); - rs["underruns"] = underflows; + rs["underruns"].v = underflows; } - rs["latepackets"] = num_late; - rs["frames"] = num_frames_modulated; + rs["latepackets"].v = num_late; + rs["frames"].v = num_frames_modulated; - rs["in_holdover_since"] = 0; + rs["in_holdover_since"].v = 0; switch (m_clock_state) { case DexterClockState::Startup: - rs["clock_state"] = "startup"; break; + rs["clock_state"].v = "startup"; break; case DexterClockState::Normal: - rs["clock_state"] = "normal"; break; + rs["clock_state"].v = "normal"; break; case DexterClockState::Holdover: - rs["clock_state"] = "holdover"; - rs["in_holdover_since"] = m_holdover_since_t; + rs["clock_state"].v = "holdover"; + rs["in_holdover_since"].v = m_holdover_since_t; break; } diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index 4fc3277..6c03b53 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -450,7 +450,7 @@ const string SDR::get_parameter(const string& parameter) const if (m_device) { const auto stat = m_device->get_run_statistics(); try { - const auto& value = stat.at(parameter).data; + const auto& value = stat.at(parameter).v; if (std::holds_alternative(value)) { ss << std::get(value); } @@ -489,19 +489,19 @@ const json::map_t SDR::get_all_values() const { json::map_t stat = m_device->get_run_statistics(); - stat["txgain"] = m_config.txgain; - stat["rxgain"] = m_config.rxgain; - stat["freq"] = m_config.frequency; - stat["muting"] = m_config.muting; - stat["temp"] = std::nullopt; + stat["txgain"].v = m_config.txgain; + stat["rxgain"].v = m_config.rxgain; + stat["freq"].v = m_config.frequency; + stat["muting"].v = m_config.muting; + stat["temp"].v = std::nullopt; if (m_device) { const std::optional temp = m_device->get_temperature(); if (temp) { - stat["temp"] = *temp; + stat["temp"].v = *temp; } } - stat["queued_frames_ms"] = m_queue.size() * + stat["queued_frames_ms"].v = m_queue.size() * (size_t)chrono::duration_cast(transmission_frame_duration(m_config.dabMode)) .count(); diff --git a/src/output/Soapy.cpp b/src/output/Soapy.cpp index 4d33e39..7931860 100644 --- a/src/output/Soapy.cpp +++ b/src/output/Soapy.cpp @@ -183,10 +183,10 @@ double Soapy::get_bandwidth(void) const SDRDevice::run_statistics_t Soapy::get_run_statistics(void) const { run_statistics_t rs; - rs["underruns"] = underflows; - rs["overruns"] = overflows; - rs["timeouts"] = timeouts; - rs["frames"] = num_frames_modulated; + rs["underruns"].v = underflows; + rs["overruns"].v = overflows; + rs["timeouts"].v = timeouts; + rs["frames"].v = num_frames_modulated; return rs; } diff --git a/src/output/UHD.cpp b/src/output/UHD.cpp index 6638b6c..094e021 100644 --- a/src/output/UHD.cpp +++ b/src/output/UHD.cpp @@ -380,19 +380,19 @@ void UHD::transmit_frame(struct FrameData&& frame) SDRDevice::run_statistics_t UHD::get_run_statistics(void) const { run_statistics_t rs; - rs["underruns"] = num_underflows; - rs["overruns"] = num_overflows; - rs["late_packets"] = num_late_packets; - rs["frames"] = num_frames_modulated; + rs["underruns"].v = num_underflows; + rs["overruns"].v = num_overflows; + rs["late_packets"].v = num_late_packets; + rs["frames"].v = num_frames_modulated; if (m_device_time) { const auto gpsdo_stat = m_device_time->get_gnss_stats(); - rs["gpsdo_holdover"] = gpsdo_stat.holdover; - rs["gpsdo_num_sv"] = gpsdo_stat.num_sv; + rs["gpsdo_holdover"].v = gpsdo_stat.holdover; + rs["gpsdo_num_sv"].v = gpsdo_stat.num_sv; } else { - rs["gpsdo_holdover"] = true; - rs["gpsdo_num_sv"] = 0; + rs["gpsdo_holdover"].v = true; + rs["gpsdo_num_sv"].v = 0; } return rs; } -- cgit v1.2.3 From 738a1e859737458d3ee9c5a8e92655bdd107a07e Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 15 Aug 2023 10:05:09 +0200 Subject: Revert "DEXTER: Add pacontrol interface" This reverts commit 150b75b244602c789934f1a5094f33aa7da3c09a. --- doc/example.ini | 6 ----- src/MemlessPoly.cpp | 4 +-- src/output/Dexter.cpp | 71 +------------------------------------------------- src/output/Dexter.h | 14 +--------- src/output/SDRDevice.h | 4 --- 5 files changed, 4 insertions(+), 95 deletions(-) (limited to 'src/output') diff --git a/doc/example.ini b/doc/example.ini index 4cc6d26..cd48ef4 100644 --- a/doc/example.ini +++ b/doc/example.ini @@ -320,12 +320,6 @@ txgain=32768 ;frequency=234208000 channel=13C -pacontrol_config_endpoint=tcp://localhost:5558 - -; TargetPower in dBm -; Runtime-changes to this setting are directly sent to pacontrol -pacontrol_targetpower=33 - [limeoutput] ; Lime output directly runs against the LMS device driver. It does not support SFN nor predistortion. device= diff --git a/src/MemlessPoly.cpp b/src/MemlessPoly.cpp index 184b5bd..17a7f57 100644 --- a/src/MemlessPoly.cpp +++ b/src/MemlessPoly.cpp @@ -314,7 +314,7 @@ void MemlessPoly::worker_thread(MemlessPoly::worker_t *workerdata) set_thread_name("MemlessPoly"); while (true) { - worker_t::input_data_t in_data = {}; + worker_t::input_data_t in_data; try { workerdata->in_queue.wait_and_pop(in_data); } @@ -386,7 +386,7 @@ int MemlessPoly::internal_process(Buffer* const dataIn, Buffer* dataOut) // Wait for completion of the tasks for (auto& worker : m_workers) { - int ret = 0; + int ret; worker.out_queue.wait_and_pop(ret); } } diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index e52f774..14edace 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -66,35 +66,9 @@ static void fill_time(struct timespec *t) } } -static void pacontrol_set_targetpower(zmq::socket_t& sock, int targetpower_dBm) -{ - stringstream message_builder; - message_builder << "{'PA0' : {'TargetPower': "; - message_builder << targetpower_dBm; - message_builder << "}}"; - const auto message = message_builder.str(); - - try { - const auto r = sock.send(zmq::const_buffer{message.data(), message.size()}); - if (r.has_value()) { - etiLog.level(debug) << "Sent TargetPower=" << targetpower_dBm << " to pacontrol"; - } - else { - // zmq_send returned EAGAIN - etiLog.level(info) << "Send TargetPower=" << targetpower_dBm << " failed"; - } - } - catch (const zmq::error_t& err) { - etiLog.level(warn) << "Failed to send TargetPower=" << targetpower_dBm << ": " << err.what(); - } -} - - Dexter::Dexter(SDRDeviceConfig& config) : SDRDevice(), - m_conf(config), - m_zmq_context(1), - m_zmq_sock(m_zmq_context, ZMQ_PUSH) + m_conf(config) { etiLog.level(info) << "Dexter:Creating the device"; @@ -192,41 +166,6 @@ Dexter::Dexter(SDRDeviceConfig& config) : m_running = true; m_underflow_read_thread = std::thread(&Dexter::underflow_read_process, this); - - m_zmq_sock.setsockopt(ZMQ_SNDTIMEO, 0); - if (not m_conf.pacontrol_config_endpoint.empty()) { - etiLog.level(debug) << "Creating pacontrol connection to " << - m_conf.pacontrol_config_endpoint; - m_zmq_sock.connect(m_conf.pacontrol_config_endpoint.c_str()); - } - - if (m_conf.pacontrol_targetpower.has_value()) { - pacontrol_set_targetpower(m_zmq_sock, *m_conf.pacontrol_targetpower); - } - else { - etiLog.level(warn) << "Config does not define PA target power"; - } -} - -static void pacontrol_set_mute(zmq::socket_t& sock, bool mute) -{ - string message = "{'PA0' : {'Mute': "; - message += (mute ? "True" : "False"); - message += "}}"; - - try { - const auto r = sock.send(zmq::const_buffer{message.data(), message.size()}); - if (r.has_value()) { - etiLog.level(debug) << "Sent mute=" << mute << " to pacontrol"; - } - else { - // zmq_send returned EAGAIN - etiLog.level(info) << "Send mute=" << mute << " failed"; - } - } - catch (const zmq::error_t& err) { - etiLog.level(warn) << "Failed to send mute=" << mute << ": " << err.what(); - } } void Dexter::channel_up() @@ -239,10 +178,6 @@ void Dexter::channel_up() m_channel_is_up = true; etiLog.level(debug) << "DEXTER CHANNEL_UP"; - - if (m_zmq_sock.connected()) { - pacontrol_set_mute(m_zmq_sock, false); - } } void Dexter::channel_down() @@ -259,10 +194,6 @@ void Dexter::channel_down() m_channel_is_up = false; etiLog.level(debug) << "DEXTER CHANNEL_DOWN"; - - if (m_zmq_sock.connected()) { - pacontrol_set_mute(m_zmq_sock, true); - } } void Dexter::handle_hw_time() diff --git a/src/output/Dexter.h b/src/output/Dexter.h index 57b9798..d4f425f 100644 --- a/src/output/Dexter.h +++ b/src/output/Dexter.h @@ -34,14 +34,8 @@ DESCRIPTION: # include #endif -#if defined(HAVE_DEXTER) - -#if !defined(HAVE_ZEROMQ) -#error "ZeroMQ is mandatory for DEXTER" -#endif - +#ifdef HAVE_DEXTER #include "iio.h" -#include "zmq.hpp" #include #include @@ -126,12 +120,6 @@ class Dexter : public Output::SDRDevice size_t num_buffers_pushed = 0; - /* Communication with pacontrol */ - zmq::context_t m_zmq_context; - zmq::socket_t m_zmq_sock; - std::string m_pacontrol_endpoint; - - /* Clock State */ DexterClockState m_clock_state = DexterClockState::Startup; // Only valid when m_clock_state is not Startup diff --git a/src/output/SDRDevice.h b/src/output/SDRDevice.h index f728d8b..378829c 100644 --- a/src/output/SDRDevice.h +++ b/src/output/SDRDevice.h @@ -94,10 +94,6 @@ struct SDRDeviceConfig { // TCP port on which to serve TX and RX samples for the // digital pre distortion learning tool uint16_t dpdFeedbackServerPort = 0; - - // DEXTER-specific - std::string pacontrol_config_endpoint; - std::optional pacontrol_targetpower; // dBm }; // Each frame contains one OFDM frame, and its -- cgit v1.2.3 From 6c0732dcfc2af963e2b8577e9fc38f71307a1033 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 15 Aug 2023 10:11:06 +0200 Subject: Dexter: remove old metrics from SDR.cpp --- doc/example.ini | 9 ++++++++- src/output/SDR.cpp | 21 --------------------- 2 files changed, 8 insertions(+), 22 deletions(-) (limited to 'src/output') diff --git a/doc/example.ini b/doc/example.ini index cd48ef4..f009fbe 100644 --- a/doc/example.ini +++ b/doc/example.ini @@ -316,10 +316,17 @@ channel=13C ;dpd_port=50055 [dexteroutput] -txgain=32768 +txgain=65535 + +; channel/frequency is applied to ad9957.center_frequency ;frequency=234208000 channel=13C +; lo offset is applied to dexter_dsp_tx.frequency0 +lo_offset=0 + +max_gps_holdover_time=3600 + [limeoutput] ; Lime output directly runs against the LMS device driver. It does not support SFN nor predistortion. device= diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index 6c03b53..d2956a2 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -99,27 +99,6 @@ SDR::SDR(SDRDeviceConfig& config, std::shared_ptr device) : RC_ADD_PARAMETER(fifo_fill, "A value representing the Lime FIFO fullness [percent]"); } #endif // HAVE_LIMESDR - -#ifdef HAVE_DEXTER - if (std::dynamic_pointer_cast(device)) { - RC_ADD_PARAMETER(clks, "DEXTER internal clk counter value"); - RC_ADD_PARAMETER(fifo_not_empty_clks, "DEXTER internal clk counter value when FIFO was last empty"); - RC_ADD_PARAMETER(gpsdo_locked, "1 if GPSDO is locked"); - RC_ADD_PARAMETER(pps_clk_error_hz, "Estimated error in Hz of clock"); - RC_ADD_PARAMETER(pps_cnt, "Number of 1PPS pulses seen from GPS"); - RC_ADD_PARAMETER(dsp_version, "Version of FPGA DSP"); - RC_ADD_PARAMETER(vcc3v3, "Voltage of VCC 3V3"); - RC_ADD_PARAMETER(vcc5v4, "Voltage of VCC 5V4"); - RC_ADD_PARAMETER(vfan, "Fan voltage"); - RC_ADD_PARAMETER(vcc_main_in, "Main input voltage"); - RC_ADD_PARAMETER(vcc3v3pll, "Voltage of VCC 3V3 PLL"); - RC_ADD_PARAMETER(vcc2v5io, "Voltage of VCC 2V5 IO"); - RC_ADD_PARAMETER(vccocxo, "OCXO voltage"); - RC_ADD_PARAMETER(tempfpga, "FPGA temperature [celsius]"); - RC_ADD_PARAMETER(voltage_alarm, "Voltage out of bounds"); - RC_ADD_PARAMETER(temp_alarm, "Temperature out of bounds"); - } -#endif // HAVE_DEXTER } SDR::~SDR() -- cgit v1.2.3 From 2980f73251c631960fcbe048eda861afa7a1be89 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 15 Aug 2023 10:37:45 +0200 Subject: Dexter: Add in_holdover_since metric --- src/output/Dexter.cpp | 7 +++++++ src/output/SDR.cpp | 10 ++++++++++ 2 files changed, 17 insertions(+) (limited to 'src/output') diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index 14edace..457ec78 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -407,6 +407,7 @@ SDRDevice::run_statistics_t Dexter::get_run_statistics(void) const rs["frames"].v = num_frames_modulated; rs["in_holdover_since"].v = 0; + rs["remaining_holdover_s"].v = nullopt; switch (m_clock_state) { case DexterClockState::Startup: rs["clock_state"].v = "startup"; break; @@ -415,6 +416,12 @@ SDRDevice::run_statistics_t Dexter::get_run_statistics(void) const case DexterClockState::Holdover: rs["clock_state"].v = "holdover"; rs["in_holdover_since"].v = m_holdover_since_t; + { + using namespace std::chrono; + const auto max_holdover_duration = seconds(m_conf.maxGPSHoldoverTime); + const duration remaining = max_holdover_duration - (steady_clock::now() - m_holdover_since); + rs["remaining_holdover_s"].v = duration_cast(remaining).count(); + } break; } diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index d2956a2..d57e4d6 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -99,6 +99,16 @@ SDR::SDR(SDRDeviceConfig& config, std::shared_ptr device) : RC_ADD_PARAMETER(fifo_fill, "A value representing the Lime FIFO fullness [percent]"); } #endif // HAVE_LIMESDR + +#ifdef HAVE_DEXTER + if (std::dynamic_pointer_cast(device)) { + RC_ADD_PARAMETER(in_holdover_since, "DEXTER timestamp when holdover began"); + RC_ADD_PARAMETER(remaining_holdover_s, "DEXTER remaining number of seconds in holdover"); + RC_ADD_PARAMETER(clock_state, "DEXTER clock state: startup/normal/holdover"); + } +#endif // HAVE_DEXTER + + } SDR::~SDR() -- cgit v1.2.3 From a759d1fae861e7f0836283dae5dce49dae6528fc Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 15 Aug 2023 10:58:11 +0200 Subject: Add parameters sdr.synchronous and mainloop.running_since --- src/DabMod.cpp | 15 +++++++++------ src/Utils.cpp | 13 ++++++++++++- src/Utils.h | 3 +++ src/output/SDR.cpp | 6 ++++++ 4 files changed, 30 insertions(+), 7 deletions(-) (limited to 'src/output') diff --git a/src/DabMod.cpp b/src/DabMod.cpp index 7daa72a..d43ebd5 100644 --- a/src/DabMod.cpp +++ b/src/DabMod.cpp @@ -114,6 +114,7 @@ class ModulatorData : public RemoteControllable { ModulatorData() : RemoteControllable("mainloop") { RC_ADD_PARAMETER(num_modulator_restarts, "(Read-only) Number of mod restarts"); RC_ADD_PARAMETER(most_recent_edi_decoded, "(Read-only) UNIX Timestamp of most recently decoded EDI frame"); + RC_ADD_PARAMETER(running_since, "(Read-only) UNIX Timestamp of most recent modulator restart"); } virtual ~ModulatorData() {} @@ -127,6 +128,9 @@ class ModulatorData : public RemoteControllable { if (parameter == "num_modulator_restarts") { ss << num_modulator_restarts; } + if (parameter == "running_since") { + ss << running_since; + } else if (parameter == "most_recent_edi_decoded") { ss << most_recent_edi_decoded; } @@ -142,12 +146,14 @@ class ModulatorData : public RemoteControllable { { json::map_t map; map["num_modulator_restarts"].v = num_modulator_restarts; + map["running_since"].v = running_since; map["most_recent_edi_decoded"].v = most_recent_edi_decoded; return map; } size_t num_modulator_restarts = 0; time_t most_recent_edi_decoded = 0; + time_t running_since = 0; }; enum class run_modulator_state_t { @@ -391,6 +397,8 @@ int launch_modulator(int argc, char* argv[]) bool run_again = true; while (run_again) { + m.running_since = get_clock_realtime_seconds(); + Flowgraph flowgraph(mod_settings.showProcessTime); m.framecount = 0; @@ -544,12 +552,7 @@ static run_modulator_state_t run_modulator(const mod_settings_t& mod_settings, M break; } - struct timespec t; - if (clock_gettime(CLOCK_REALTIME, &t) != 0) { - throw std::runtime_error(std::string("Failed to retrieve CLOCK_REALTIME") + strerror(errno)); - } - - m.most_recent_edi_decoded = t.tv_sec; + m.most_recent_edi_decoded = get_clock_realtime_seconds(); fct = m.ediInput->ediReader.getFct(); fp = m.ediInput->ediReader.getFp(); ts = m.ediInput->ediReader.getTimestamp(); diff --git a/src/Utils.cpp b/src/Utils.cpp index 94f198c..20297ea 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -25,7 +25,8 @@ along with ODR-DabMod. If not, see . */ -#include "sstream" +#include +#include #include "Utils.h" #include "GainControl.h" #if defined(HAVE_PRCTL) @@ -304,3 +305,13 @@ std::chrono::milliseconds transmission_frame_duration(unsigned int dabmode) } } + +time_t get_clock_realtime_seconds() +{ + struct timespec t; + if (clock_gettime(CLOCK_REALTIME, &t) != 0) { + throw std::runtime_error(std::string("Failed to retrieve CLOCK_REALTIME") + strerror(errno)); + } + + return t.tv_sec; +} diff --git a/src/Utils.h b/src/Utils.h index 82728e9..367dd48 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -36,6 +36,7 @@ #include #include #include +#include #include #include "ConfigParser.h" @@ -59,3 +60,5 @@ double parseChannel(const std::string& chan); // dabMode is either 1, 2, 3, 4, corresponding to TM I, TM II, TM III and TM IV. // throws a runtime_error if dabMode is not one of these values. std::chrono::milliseconds transmission_frame_duration(unsigned int dabmode); + +time_t get_clock_realtime_seconds(); diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index d57e4d6..e466287 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -84,6 +84,7 @@ SDR::SDR(SDRDeviceConfig& config, std::shared_ptr device) : RC_ADD_PARAMETER(underruns, "Counter of number of underruns"); RC_ADD_PARAMETER(latepackets, "Counter of number of late packets"); RC_ADD_PARAMETER(frames, "Counter of number of frames modulated"); + RC_ADD_PARAMETER(synchronous, "1 if configured for synchronous transmission"); #ifdef HAVE_OUTPUT_UHD if (std::dynamic_pointer_cast(device)) { @@ -435,6 +436,9 @@ const string SDR::get_parameter(const string& parameter) const chrono::duration_cast(transmission_frame_duration(m_config.dabMode)) .count(); } + else if (parameter == "synchronous") { + ss << m_config.enableSync; + } else { if (m_device) { const auto stat = m_device->get_run_statistics(); @@ -494,6 +498,8 @@ const json::map_t SDR::get_all_values() const (size_t)chrono::duration_cast(transmission_frame_duration(m_config.dabMode)) .count(); + stat["synchronous"].v = m_config.enableSync; + return stat; } -- cgit v1.2.3 From 61b3fa742e24acc6b1b7747b3e863dff43670d51 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 15 Aug 2023 15:18:04 +0200 Subject: Fix compliation --- src/output/Dexter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/output') diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index 457ec78..8e2a6c5 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -420,7 +420,7 @@ SDRDevice::run_statistics_t Dexter::get_run_statistics(void) const using namespace std::chrono; const auto max_holdover_duration = seconds(m_conf.maxGPSHoldoverTime); const duration remaining = max_holdover_duration - (steady_clock::now() - m_holdover_since); - rs["remaining_holdover_s"].v = duration_cast(remaining).count(); + rs["remaining_holdover_s"].v = (ssize_t)duration_cast(remaining).count(); } break; } -- cgit v1.2.3 From b102ff50555518606b8356bbc1dd70e233d0466c Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Thu, 24 Aug 2023 15:35:30 +0200 Subject: Add channel to SDR RC --- src/ConfigParser.cpp | 10 +++++----- src/Utils.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++--- src/Utils.h | 8 ++++++-- src/output/SDR.cpp | 34 ++++++++++++++++++++++++++++++++- 4 files changed, 94 insertions(+), 11 deletions(-) (limited to 'src/output') diff --git a/src/ConfigParser.cpp b/src/ConfigParser.cpp index 68ee74b..1219ae7 100644 --- a/src/ConfigParser.cpp +++ b/src/ConfigParser.cpp @@ -251,7 +251,7 @@ static void parse_configfile( throw std::runtime_error("Configuration error"); } else if (sdr_device_config.frequency == 0) { - sdr_device_config.frequency = parseChannel(chan); + sdr_device_config.frequency = parse_channel(chan); } else if (sdr_device_config.frequency != 0 && chan != "") { std::cerr << " UHD output: cannot define both frequency and channel.\n"; @@ -305,7 +305,7 @@ static void parse_configfile( throw std::runtime_error("Configuration error"); } else if (outputsoapy_conf.frequency == 0) { - outputsoapy_conf.frequency = parseChannel(chan); + outputsoapy_conf.frequency = parse_channel(chan); } else if (outputsoapy_conf.frequency != 0 && chan != "") { std::cerr << " soapy output: cannot define both frequency and channel.\n"; @@ -333,7 +333,7 @@ static void parse_configfile( throw std::runtime_error("Configuration error"); } else if (outputdexter_conf.frequency == 0) { - outputdexter_conf.frequency = parseChannel(chan); + outputdexter_conf.frequency = parse_channel(chan); } else if (outputdexter_conf.frequency != 0 && chan != "") { std::cerr << " dexter output: cannot define both frequency and channel.\n"; @@ -362,7 +362,7 @@ static void parse_configfile( throw std::runtime_error("Configuration error"); } else if (outputlime_conf.frequency == 0) { - outputlime_conf.frequency = parseChannel(chan); + outputlime_conf.frequency = parse_channel(chan); } else if (outputlime_conf.frequency != 0 && chan != "") { std::cerr << " Lime output: cannot define both frequency and channel.\n"; @@ -391,7 +391,7 @@ static void parse_configfile( throw std::runtime_error("Configuration error"); } else if (outputbladerf_conf.frequency == 0) { - outputbladerf_conf.frequency = parseChannel(chan); + outputbladerf_conf.frequency = parse_channel(chan); } else if (outputbladerf_conf.frequency != 0 && chan != "") { std::cerr << " BladeRF output: cannot define both frequency and channel.\n"; diff --git a/src/Utils.cpp b/src/Utils.cpp index 20297ea..788d125 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -244,7 +244,7 @@ void set_thread_name(const char *name) #endif } -double parseChannel(const std::string& chan) +double parse_channel(const std::string& chan) { double freq; if (chan == "5A") freq = 174928000; @@ -286,12 +286,59 @@ double parseChannel(const std::string& chan) else if (chan == "13E") freq = 237488000; else if (chan == "13F") freq = 239200000; else { - std::cerr << " soapy output: channel " << chan << " does not exist in table\n"; - throw std::out_of_range("soapy channel selection error"); + std::cerr << "Channel " << chan << " does not exist in table\n"; + throw std::out_of_range("channel out of range"); } return freq; } +std::optional convert_frequency_to_channel(double frequency) +{ + const int freq = round(frequency); + std::string chan; + if (freq == 174928000) chan = "5A"; + else if (freq == 176640000) chan = "5B"; + else if (freq == 178352000) chan = "5C"; + else if (freq == 180064000) chan = "5D"; + else if (freq == 181936000) chan = "6A"; + else if (freq == 183648000) chan = "6B"; + else if (freq == 185360000) chan = "6C"; + else if (freq == 187072000) chan = "6D"; + else if (freq == 188928000) chan = "7A"; + else if (freq == 190640000) chan = "7B"; + else if (freq == 192352000) chan = "7C"; + else if (freq == 194064000) chan = "7D"; + else if (freq == 195936000) chan = "8A"; + else if (freq == 197648000) chan = "8B"; + else if (freq == 199360000) chan = "8C"; + else if (freq == 201072000) chan = "8D"; + else if (freq == 202928000) chan = "9A"; + else if (freq == 204640000) chan = "9B"; + else if (freq == 206352000) chan = "9C"; + else if (freq == 208064000) chan = "9D"; + else if (freq == 209936000) chan = "10A"; + else if (freq == 211648000) chan = "10B"; + else if (freq == 213360000) chan = "10C"; + else if (freq == 215072000) chan = "10D"; + else if (freq == 216928000) chan = "11A"; + else if (freq == 218640000) chan = "11B"; + else if (freq == 220352000) chan = "11C"; + else if (freq == 222064000) chan = "11D"; + else if (freq == 223936000) chan = "12A"; + else if (freq == 225648000) chan = "12B"; + else if (freq == 227360000) chan = "12C"; + else if (freq == 229072000) chan = "12D"; + else if (freq == 230784000) chan = "13A"; + else if (freq == 232496000) chan = "13B"; + else if (freq == 234208000) chan = "13C"; + else if (freq == 235776000) chan = "13D"; + else if (freq == 237488000) chan = "13E"; + else if (freq == 239200000) chan = "13F"; + else { return std::nullopt; } + + return chan; +} + std::chrono::milliseconds transmission_frame_duration(unsigned int dabmode) { using namespace std::chrono; diff --git a/src/Utils.h b/src/Utils.h index 367dd48..584a756 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -31,6 +31,7 @@ # include "config.h" #endif +#include #include #include #include @@ -54,8 +55,11 @@ int set_realtime_prio(int prio); // Set the name of the thread void set_thread_name(const char *name); -// Convert a channel like 10A to a frequency -double parseChannel(const std::string& chan); +// Convert a channel like 10A to a frequency in Hz +double parse_channel(const std::string& chan); + +// Convert a frequency in Hz to a channel. +std::optional convert_frequency_to_channel(double frequency); // dabMode is either 1, 2, 3, 4, corresponding to TM I, TM II, TM III and TM IV. // throws a runtime_error if dabMode is not one of these values. diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index e466287..91c31f0 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -78,7 +78,8 @@ SDR::SDR(SDRDeviceConfig& config, std::shared_ptr device) : RC_ADD_PARAMETER(txgain, "TX gain"); RC_ADD_PARAMETER(rxgain, "RX gain for DPD feedback"); RC_ADD_PARAMETER(bandwidth, "Analog front-end bandwidth"); - RC_ADD_PARAMETER(freq, "Transmission frequency"); + RC_ADD_PARAMETER(freq, "Transmission frequency in Hz"); + RC_ADD_PARAMETER(channel, "Transmission frequency as channel"); RC_ADD_PARAMETER(muting, "Mute the output by stopping the transmitter"); RC_ADD_PARAMETER(temp, "Temperature in degrees C of the device"); RC_ADD_PARAMETER(underruns, "Counter of number of underruns"); @@ -389,6 +390,18 @@ void SDR::set_parameter(const string& parameter, const string& value) m_device->tune(m_config.lo_offset, m_config.frequency); m_config.frequency = m_device->get_tx_freq(); } + else if (parameter == "channel") { + try { + const double frequency = parse_channel(value); + + m_config.frequency = frequency; + m_device->tune(m_config.lo_offset, m_config.frequency); + m_config.frequency = m_device->get_tx_freq(); + } + catch (const std::out_of_range& e) { + throw ParameterError("Cannot parse channel"); + } + } else if (parameter == "muting") { ss >> m_config.muting; } @@ -416,6 +429,16 @@ const string SDR::get_parameter(const string& parameter) const else if (parameter == "freq") { ss << m_config.frequency; } + else if (parameter == "channel") { + const auto maybe_freq = convert_frequency_to_channel(m_config.frequency); + + if (maybe_freq.has_value()) { + ss << *maybe_freq; + } + else { + throw ParameterError("Frequency is outside list of channels"); + } + } else if (parameter == "muting") { ss << m_config.muting; } @@ -488,6 +511,15 @@ const json::map_t SDR::get_all_values() const stat["muting"].v = m_config.muting; stat["temp"].v = std::nullopt; + const auto maybe_freq = convert_frequency_to_channel(m_config.frequency); + + if (maybe_freq.has_value()) { + stat["channel"].v = *maybe_freq; + } + else { + stat["channel"].v = std::nullopt; + } + if (m_device) { const std::optional temp = m_device->get_temperature(); if (temp) { -- cgit v1.2.3 From 4a1ef9edbf8c32d87292beac6cf96606442bf302 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 25 Aug 2023 14:01:24 +0200 Subject: Add max_gps_holdover_time to RC, allow modifying synchronous too --- src/output/SDR.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/output') diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index 91c31f0..44af875 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -86,6 +86,7 @@ SDR::SDR(SDRDeviceConfig& config, std::shared_ptr device) : RC_ADD_PARAMETER(latepackets, "Counter of number of late packets"); RC_ADD_PARAMETER(frames, "Counter of number of frames modulated"); RC_ADD_PARAMETER(synchronous, "1 if configured for synchronous transmission"); + RC_ADD_PARAMETER(max_gps_holdover_time, "Max holdover duration in seconds"); #ifdef HAVE_OUTPUT_UHD if (std::dynamic_pointer_cast(device)) { @@ -405,6 +406,14 @@ void SDR::set_parameter(const string& parameter, const string& value) else if (parameter == "muting") { ss >> m_config.muting; } + else if (parameter == "synchronous") { + uint32_t enableSync = 0; + ss >> enableSync; + m_config.enableSync = enableSync > 0; + } + else if (parameter == "max_gps_holdover_time") { + ss >> m_config.maxGPSHoldoverTime; + } else { stringstream ss_err; ss_err << "Parameter '" << parameter @@ -462,6 +471,9 @@ const string SDR::get_parameter(const string& parameter) const else if (parameter == "synchronous") { ss << m_config.enableSync; } + else if (parameter == "max_gps_holdover_time") { + ss << m_config.maxGPSHoldoverTime; + } else { if (m_device) { const auto stat = m_device->get_run_statistics(); @@ -531,6 +543,7 @@ const json::map_t SDR::get_all_values() const .count(); stat["synchronous"].v = m_config.enableSync; + stat["max_gps_holdover_time"].v = (size_t)m_config.maxGPSHoldoverTime; return stat; } -- cgit v1.2.3 From 0f11a2bc9b22a1362245459ae45ae4f86cb9374e Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 5 Sep 2023 10:11:39 +0200 Subject: Dexter: make channel_down wait for underflow to occur --- src/output/Dexter.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/output') diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index 8e2a6c5..26472e8 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -187,11 +187,33 @@ void Dexter::channel_down() etiLog.level(error) << "Failed to set dexter_dsp_tx.gain0 = 0: " << get_iio_error(r); } - // This will flush out the FIFO + // Setting stream0_start_clocks to 0 will flush out the FIFO, but we need to wait a bit before + // we "up" the channel again if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", 0)) != 0) { etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0 : " << get_iio_error(r); } + long long underflows_old = 0; + + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "buffer_underflows0", &underflows_old)) != 0) { + etiLog.level(warn) << "Failed to read dexter_dsp_tx.buffer_underflows0 : " << get_iio_error(r); + } + + long long underflows = underflows_old; + + // Limiting to 10*96ms is just a safety to avoid running into an infinite loop + for (size_t i = 0; underflows == underflows_old && i < 10; i++) { + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "buffer_underflows0", &underflows)) != 0) { + etiLog.level(warn) << "Failed to read dexter_dsp_tx.buffer_underflows0 : " << get_iio_error(r); + } + + this_thread::sleep_for(chrono::milliseconds(96)); + } + + if (underflows == underflows_old) { + etiLog.level(warn) << "DEXTER CHANNEL_DOWN, no underflow detected! " << underflows; + } + m_channel_is_up = false; etiLog.level(debug) << "DEXTER CHANNEL_DOWN"; } -- cgit v1.2.3 From 6728ddc82936d8d6223a885bf7fbcec9a00c18a6 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 10 Oct 2023 14:43:23 +0200 Subject: Don't clutter log output every 6s --- src/output/SDR.cpp | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src/output') diff --git a/src/output/SDR.cpp b/src/output/SDR.cpp index 44af875..75a7ee3 100644 --- a/src/output/SDR.cpp +++ b/src/output/SDR.cpp @@ -357,12 +357,6 @@ void SDR::handle_frame(struct FrameData&& frame) return; } - if (frame.ts.fct == 0) { - etiLog.level(debug) << - "OutputSDR: TX FCT=" << frame.ts.fct << - " TS " << frame.ts.to_string(); - } - m_device->transmit_frame(std::move(frame)); } -- cgit v1.2.3