From 14dc3f49f3df898a61e20aa3a5a3257d437bb287 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Mon, 22 Jan 2018 00:35:30 +0100 Subject: Add hardware time simulation --- CMakeLists.txt | 1 + Dummy_Clock.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ Dummy_Settings.cpp | 5 +++++ SoapyDummy.hpp | 15 +++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 Dummy_Clock.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6010eb7..b2117ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,4 +24,5 @@ SOAPY_SDR_MODULE_UTIL( Dummy_Settings.cpp Dummy_Streaming.cpp Dummy_Session.cpp + Dummy_Clock.cpp ) diff --git a/Dummy_Clock.cpp b/Dummy_Clock.cpp new file mode 100644 index 0000000..ce7f767 --- /dev/null +++ b/Dummy_Clock.cpp @@ -0,0 +1,66 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018 Matthias P. Braendli + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * 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 OR COPYRIGHT HOLDERS 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. + */ + +#include "SoapyDummy.hpp" +#include +#include +#include + +std::vector SoapyDummy::listTimeSources(void) const +{ + return {"internal"}; +} + +void SoapyDummy::setTimeSource(const std::string &source) +{ +} + +std::string SoapyDummy::getTimeSource(void) const +{ + return "internal"; +} + +bool SoapyDummy::hasHardwareTime(const std::string &what) const +{ + return true; +} + +long long SoapyDummy::getHardwareTime(const std::string &what) const +{ + using namespace std::chrono; + const auto now = steady_clock::now(); + return duration_cast(now.time_since_epoch()).count() - m_time_epoch; +} + +void SoapyDummy::setHardwareTime(const long long timeNs, const std::string &what) +{ + // From getHardwareTime() we have: + // timeNs = now - epoch + // we derive + // epoch = now - timeNs + using namespace std::chrono; + const auto now = steady_clock::now(); + m_time_epoch = + duration_cast(now.time_since_epoch()).count() - timeNs; +} diff --git a/Dummy_Settings.cpp b/Dummy_Settings.cpp index acf8614..85dc056 100644 --- a/Dummy_Settings.cpp +++ b/Dummy_Settings.cpp @@ -27,6 +27,11 @@ SoapyDummy::SoapyDummy( const SoapySDR::Kwargs &args ) { + // On startup, initialise our epoch to current time, so that + // it appears the counter just started counting + using namespace std::chrono; + const auto now = steady_clock::now(); + m_time_epoch = duration_cast(now.time_since_epoch()).count(); } SoapyDummy::~SoapyDummy( void ) diff --git a/SoapyDummy.hpp b/SoapyDummy.hpp index e1ca79a..e89932d 100644 --- a/SoapyDummy.hpp +++ b/SoapyDummy.hpp @@ -25,6 +25,7 @@ #pragma once #include #include +#include #include #include #include @@ -183,6 +184,18 @@ class SoapyDummy : public SoapySDR::Device double getBandwidth( const int direction, const size_t channel ) const; std::vector listBandwidths( const int direction, const size_t channel ) const; + /******************************************************************* + * Time API + ******************************************************************/ + + virtual std::vector listTimeSources(void) const; + virtual void setTimeSource(const std::string &source); + virtual std::string getTimeSource(void) const; + + virtual bool hasHardwareTime(const std::string &what = "") const; + virtual long long getHardwareTime(const std::string &what = "") const; + virtual void setHardwareTime(const long long timeNs, const std::string &what = ""); + /******************************************************************* * Dummy callback ******************************************************************/ @@ -204,4 +217,6 @@ class SoapyDummy : public SoapySDR::Device SoapyDummySession m_session; double m_samplerate = 0; double m_frequency = 0; + + long long m_time_epoch; }; -- cgit v1.2.3