From 7e9fa603ee00fa0ec8ebf6e258f94cadbb300856 Mon Sep 17 00:00:00 2001 From: Jörgen Scott Date: Tue, 3 Feb 2015 17:09:37 +0100 Subject: added filter for vim backup files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 85dfe44..1851b69 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ src/odr-dabmod .clang_complete .*.swp +*~ -- cgit v1.2.3 From 1ab555f832c764bd10cebeeee51d9f7ad5c4b2c6 Mon Sep 17 00:00:00 2001 From: Jörgen Scott Date: Wed, 4 Feb 2015 12:19:55 +0100 Subject: Removed magick numbers and added support for static delay in all dab modes --- src/DabMod.cpp | 1 + src/OutputUHD.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++---------- src/OutputUHD.h | 7 ++++++- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/DabMod.cpp b/src/DabMod.cpp index 82f03e5..214231c 100644 --- a/src/DabMod.cpp +++ b/src/DabMod.cpp @@ -535,6 +535,7 @@ int main(int argc, char* argv[]) outputuhd_conf.txgain = pt.get("uhdoutput.txgain", 0.0); outputuhd_conf.frequency = pt.get("uhdoutput.frequency", 0); std::string chan = pt.get("uhdoutput.channel", ""); + outputuhd_conf.dabMode = dabMode; if (outputuhd_conf.frequency == 0 && chan == "") { std::cerr << " UHD output enabled, but neither frequency nor channel defined.\n"; diff --git a/src/OutputUHD.cpp b/src/OutputUHD.cpp index 741731e..91c030f 100644 --- a/src/OutputUHD.cpp +++ b/src/OutputUHD.cpp @@ -57,11 +57,11 @@ OutputUHD::OutputUHD( // the buffers at object initialisation. first_run(true), activebuffer(1), - myDelayBuf(196608) + myDelayBuf(0) { myMuting = 0; // is remote-controllable - myStaticDelay = 0; // is remote-controllable + myStaticDelayUs = 0; // is remote-controllable #if FAKE_UHD MDEBUG("OutputUHD:Using fake UHD output"); @@ -225,6 +225,7 @@ OutputUHD::OutputUHD( uwd.check_refclk_loss = true; } + SetDelayBuffer(config.dabMode); shared_ptr b(new barrier(2)); mySyncBarrier = b; @@ -246,6 +247,35 @@ OutputUHD::~OutputUHD() } } +void OutputUHD::SetDelayBuffer(unsigned int dabMode) +{ + // find out the duration of the transmission frame (Table 2 in ETSI 300 401) + switch (dabMode) { + case 0: // could happen when called from constructor and we take the mode from ETI + myTFDurationMs = 0; + break; + case 1: + myTFDurationMs = 96; + break; + case 2: + myTFDurationMs = 24; + break; + case 3: + myTFDurationMs = 24; + break; + case 4: + myTFDurationMs = 48; + break; + default: + throw std::runtime_error("OutPutUHD: invalid DAB mode"); + } + fprintf(stderr, "DelayBuf = %d\n", myTFDurationMs * myConf.sampleRate / 1000); + // The buffer size equals the number of samples per transmission frame so + // we calculate it by multiplying the duration of the transmission frame + // with the samplerate. + myDelayBuf.resize(myTFDurationMs * myConf.sampleRate / 1000); +} + int OutputUHD::process(Buffer* dataIn, Buffer* dataOut) { struct frame_timestamp ts; @@ -281,6 +311,10 @@ int OutputUHD::process(Buffer* dataIn, Buffer* dataOut) default: break; } + // we only set the delay buffer from the dab mode signaled in ETI if the + // dab mode was not set in contructor + if (myTFDurationMs == 0) + SetDelayBuffer(myEtiReader->getMode()); activebuffer = 1; @@ -307,7 +341,7 @@ int OutputUHD::process(Buffer* dataIn, Buffer* dataOut) myEtiReader->sourceContainsTimestamp(); // calculate delay - uint32_t noSampleDelay = (myStaticDelay * 2048) / 1000; + uint32_t noSampleDelay = (myStaticDelayUs * myConf.sampleRate / 1000) / 1000; uint32_t noByteDelay = noSampleDelay * sizeof(complexf); uint8_t* pInData = (uint8_t*) dataIn->getData(); @@ -655,13 +689,13 @@ void OutputUHD::set_parameter(const string& parameter, const string& value) else if (parameter == "staticdelay") { int adjust; ss >> adjust; - int newStaticDelay = myStaticDelay + adjust; - if (newStaticDelay > 96000) - myStaticDelay = newStaticDelay - 96000; - else if (newStaticDelay < 0) - myStaticDelay = newStaticDelay + 96000; + int newStaticDelayUs = myStaticDelayUs + adjust; + if (newStaticDelayUs > myTFDurationMs * 1000) + myStaticDelayUs = newStaticDelayUs - myTFDurationMs * 1000; + else if (newStaticDelayUs < 0) + myStaticDelayUs = newStaticDelayUs + myTFDurationMs * 1000; else - myStaticDelay = newStaticDelay; + myStaticDelayUs = newStaticDelayUs; } else if (parameter == "iqbalance") { ss >> myConf.frequency; @@ -689,7 +723,7 @@ const string OutputUHD::get_parameter(const string& parameter) const ss << myMuting; } else if (parameter == "staticdelay") { - ss << myStaticDelay; + ss << myStaticDelayUs; } else { ss << "Parameter '" << parameter << diff --git a/src/OutputUHD.h b/src/OutputUHD.h index 7eb6733..d002e98 100644 --- a/src/OutputUHD.h +++ b/src/OutputUHD.h @@ -171,6 +171,7 @@ struct OutputUHDConfig { double txgain; bool enableSync; bool muteNoTimestamps; + unsigned dabMode; /* allowed values : auto, int, sma, mimo */ std::string refclk_src; @@ -231,8 +232,12 @@ class OutputUHD: public ModOutput, public RemoteControllable { bool myMuting; private: + // methods + void SetDelayBuffer(unsigned int dabMode); + // data - int myStaticDelay; + int myStaticDelayUs; // static delay in microseconds + int myTFDurationMs; // TF duration in milliseconds std::vector myDelayBuf; size_t lastLen; }; -- cgit v1.2.3 From e8e7350db3bbf1d408ca4b305329c17d545c91eb Mon Sep 17 00:00:00 2001 From: Jörgen Scott Date: Mon, 9 Feb 2015 10:17:01 +0100 Subject: Bug fix for static delay --- src/OutputUHD.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/OutputUHD.cpp b/src/OutputUHD.cpp index 91c030f..3845439 100644 --- a/src/OutputUHD.cpp +++ b/src/OutputUHD.cpp @@ -341,7 +341,7 @@ int OutputUHD::process(Buffer* dataIn, Buffer* dataOut) myEtiReader->sourceContainsTimestamp(); // calculate delay - uint32_t noSampleDelay = (myStaticDelayUs * myConf.sampleRate / 1000) / 1000; + uint32_t noSampleDelay = (myStaticDelayUs * (myConf.sampleRate / 1000)) / 1000; uint32_t noByteDelay = noSampleDelay * sizeof(complexf); uint8_t* pInData = (uint8_t*) dataIn->getData(); @@ -690,10 +690,10 @@ void OutputUHD::set_parameter(const string& parameter, const string& value) int adjust; ss >> adjust; int newStaticDelayUs = myStaticDelayUs + adjust; - if (newStaticDelayUs > myTFDurationMs * 1000) - myStaticDelayUs = newStaticDelayUs - myTFDurationMs * 1000; + if (newStaticDelayUs > (myTFDurationMs * 1000)) + myStaticDelayUs = newStaticDelayUs - (myTFDurationMs * 1000); else if (newStaticDelayUs < 0) - myStaticDelayUs = newStaticDelayUs + myTFDurationMs * 1000; + myStaticDelayUs = newStaticDelayUs + (myTFDurationMs * 1000); else myStaticDelayUs = newStaticDelayUs; } -- cgit v1.2.3 From 413aeff4088a1d5adadf07cb1833091a197dc393 Mon Sep 17 00:00:00 2001 From: Jörgen Scott Date: Mon, 9 Feb 2015 12:42:00 +0100 Subject: added reset for static delay --- src/OutputUHD.cpp | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/OutputUHD.cpp b/src/OutputUHD.cpp index 3845439..bfd24a8 100644 --- a/src/OutputUHD.cpp +++ b/src/OutputUHD.cpp @@ -269,7 +269,6 @@ void OutputUHD::SetDelayBuffer(unsigned int dabMode) default: throw std::runtime_error("OutPutUHD: invalid DAB mode"); } - fprintf(stderr, "DelayBuf = %d\n", myTFDurationMs * myConf.sampleRate / 1000); // The buffer size equals the number of samples per transmission frame so // we calculate it by multiplying the duration of the transmission frame // with the samplerate. @@ -687,15 +686,23 @@ void OutputUHD::set_parameter(const string& parameter, const string& value) ss >> myMuting; } else if (parameter == "staticdelay") { - int adjust; + int64_t adjust; ss >> adjust; - int newStaticDelayUs = myStaticDelayUs + adjust; - if (newStaticDelayUs > (myTFDurationMs * 1000)) - myStaticDelayUs = newStaticDelayUs - (myTFDurationMs * 1000); - else if (newStaticDelayUs < 0) - myStaticDelayUs = newStaticDelayUs + (myTFDurationMs * 1000); - else - myStaticDelayUs = newStaticDelayUs; + if (adjust > (myTFDurationMs * 1000)) + { // reset static delay for values outside range + myStaticDelayUs = 0; + } + else + { // the new adjust value is added to the existing delay and the result + // is wrapped around at TF duration + int newStaticDelayUs = myStaticDelayUs + adjust; + if (newStaticDelayUs > (myTFDurationMs * 1000)) + myStaticDelayUs = newStaticDelayUs - (myTFDurationMs * 1000); + else if (newStaticDelayUs < 0) + myStaticDelayUs = newStaticDelayUs + (myTFDurationMs * 1000); + else + myStaticDelayUs = newStaticDelayUs; + } } else if (parameter == "iqbalance") { ss >> myConf.frequency; -- cgit v1.2.3 From ebdd156e45a520c67201c0382b1971e39bb25b72 Mon Sep 17 00:00:00 2001 From: Jörgen Scott Date: Mon, 9 Feb 2015 12:45:21 +0100 Subject: Added c++ client API --- doc/zmq-ctrl/cpp/OdrModCtrl.cpp | 306 +++++++++++++++++++++++++++++++++++ doc/zmq-ctrl/cpp/OdrModCtrl.hpp | 86 ++++++++++ doc/zmq-ctrl/cpp/test/CMakeLists.txt | 31 ++++ doc/zmq-ctrl/cpp/test/README | 14 ++ doc/zmq-ctrl/cpp/test/ctrl_test.cpp | 88 ++++++++++ 5 files changed, 525 insertions(+) create mode 100644 doc/zmq-ctrl/cpp/OdrModCtrl.cpp create mode 100644 doc/zmq-ctrl/cpp/OdrModCtrl.hpp create mode 100755 doc/zmq-ctrl/cpp/test/CMakeLists.txt create mode 100644 doc/zmq-ctrl/cpp/test/README create mode 100644 doc/zmq-ctrl/cpp/test/ctrl_test.cpp diff --git a/doc/zmq-ctrl/cpp/OdrModCtrl.cpp b/doc/zmq-ctrl/cpp/OdrModCtrl.cpp new file mode 100644 index 0000000..9b3f8dd --- /dev/null +++ b/doc/zmq-ctrl/cpp/OdrModCtrl.cpp @@ -0,0 +1,306 @@ +/*! + * This is an implementation for the zmq ctrl API of the odr-dabmod. + * + * Copyright (c) 2015 by Jörgen Scott (jorgen.scott@paneda.se) + * + * 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 . + * + * \code + * #include "OdrModCtrl.hpp" + * #include + * ... + * zmq::context_t ctx; + * std::string error; + * COdrModCtrl *pCtrl = new COdrModCtrl(&context, // zmq context + * "tcp://127.0.0.1:9400", // zmq endpoint + * 1000); // timeout in milliseconds + * if (pCtrl->SetTxGain(50, error)) + * std::cout << "Tx gain set to 50" << std::endl; + * else + * std::cout << "An error occured: " << error << std::endl; + * delete pCtrl; // destructor will close zmq socket + * + * \endcode + **/ +#include "OdrModCtrl.hpp" +#include + +COdrModCtrl::COdrModCtrl(zmq::context_t *pContext, std::string odrEndpoint, + unsigned int timeoutMs) +{ + m_pContext = pContext; + m_odrEndpoint = odrEndpoint; + m_timeoutMs = (uint32_t) timeoutMs; +} + +COdrModCtrl::~COdrModCtrl() +{ + if (m_pReqSocket != NULL) + { + m_pReqSocket->close(); + delete m_pReqSocket; + } +} + +//// public get methods ///////////////////////////////////////////////////////// +bool COdrModCtrl::GetDigitalGain(double &gain, std::string &error) +{ + return DoGet("gain", "digital", gain, error); +} + +bool COdrModCtrl::GetTxGain(double &gain, std::string &error) +{ + return DoGet("uhd", "txgain", gain, error); +} + +bool COdrModCtrl::GetTxFrequency(double &freqHz, std::string &error) +{ + return DoGet("uhd", "freq", freqHz, error); +} + +bool COdrModCtrl::GetMuting(bool &mute, std::string &error) +{ + return DoGet("uhd", "muting", (uint32_t&) mute, error); +} + +bool COdrModCtrl::GetStaticDelay(uint32_t &delayUs, std::string &error) +{ + return DoGet("uhd", "staticdelay", delayUs, error); +} + + +//// public set methods ///////////////////////////////////////////////////////// + +bool COdrModCtrl::SetDigitalGain(const double gain, std::string &error) +{ + return DoSet("gain", "digital", gain, error); +} + +bool COdrModCtrl::SetTxGain(const double gain, std::string &error) +{ + return DoSet("uhd", "txgain", gain, error); +} + +bool COdrModCtrl::SetTxFrequency(const double freqHz, std::string &error) +{ + return DoSet("uhd", "freq", freqHz, error); +} + +bool COdrModCtrl::SetMuting(const bool mute, std::string &error) +{ + return DoSet("uhd", "muting", mute, error); +} + +bool COdrModCtrl::SetStaticDelay(const uint32_t delayUs, std::string &error) +{ + return DoSet("uhd", "staticdelay", delayUs, error); +} + + +//// private methods //////////////////////////////////////////////////////////// + +template +bool COdrModCtrl::DoSet(const std::string module, const std::string parameter, + const Type value, std::string &error) +{ + if (m_pReqSocket == NULL) + { + m_pReqSocket = new zmq::socket_t(*m_pContext, ZMQ_REQ); + if (!ConnectSocket(m_pReqSocket, m_odrEndpoint, error)) + return false; + } + + std::vector msg; + msg.push_back("set"); + msg.push_back(module); + msg.push_back(parameter); + std::stringstream ss; + ss << value; + msg.push_back(ss.str()); + + // send the message + if (!SendMessage(m_pReqSocket, msg, error)) + { + // destroy the socket according to the "Lazy Pirate Pattern" in + // the zmq guide + m_pReqSocket->close(); + delete m_pReqSocket; + m_pReqSocket = NULL; + return false; + } + + // wait for reply + if (!RecvAll(m_pReqSocket, msg, m_timeoutMs, error)) + return false; + + return ParseSetReply(msg, error); +} + +bool COdrModCtrl::ParseSetReply(const std::vector &msg, + std::string &error) +{ + error = ""; + if (msg.size() < 1) + error = "Bad reply format"; + else if (msg.size() == 1 && msg[0] == "ok") + return true; + else if (msg.size() == 2 && msg[0] == "fail") + { + error = msg[1]; + return false; + } + else + { + error = "Bad reply format"; + return false; + } +} + +template +bool COdrModCtrl::DoGet(const std::string module, const std::string parameter, + Type &value, std::string &error) +{ + if (m_pReqSocket == NULL) + { + m_pReqSocket = new zmq::socket_t(*m_pContext, ZMQ_REQ); + if (!ConnectSocket(m_pReqSocket, m_odrEndpoint, error)) + return false; + } + + std::vector msg; + msg.push_back("get"); + msg.push_back(module); + msg.push_back(parameter); + + // send the message + if (!SendMessage(m_pReqSocket, msg, error)) + { + // destroy the socket according to the "Lazy Pirate Pattern" + // in the zmq guide + m_pReqSocket->close(); + delete m_pReqSocket; + m_pReqSocket = NULL; + return false; + } + + // wait for reply + if (!RecvAll(m_pReqSocket, msg, m_timeoutMs, error)) + return false; + + return ParseGetReply(msg, value, error); +} + +template +bool COdrModCtrl::ParseGetReply(const std::vector &msg, + Type &value, std::string &error) +{ + error = ""; + if (msg.size() < 1) + error = "Bad reply format"; + else if (msg.size() == 1) + { + std::stringstream ss(msg[0]); + ss >> value; + return true; + } + else if (msg.size() == 2 && msg[0] == "fail") + { + error = msg[1]; + return false; + } + else + { + error = "Bad reply format"; + return false; + } +} + +bool COdrModCtrl::ConnectSocket(zmq::socket_t *pSocket, const std::string endpoint, + std::string &error) +{ + error = ""; + try + { + int hwm = 1; + int linger = 0; + pSocket->setsockopt(ZMQ_RCVHWM, &hwm, sizeof(hwm)); + pSocket->setsockopt(ZMQ_SNDHWM, &hwm, sizeof(hwm)); + pSocket->setsockopt(ZMQ_LINGER, &linger, sizeof(linger)); + pSocket->connect(endpoint.c_str()); + return true; + } + catch(zmq::error_t &ex) + { + error = "Failed to connect: " + endpoint + + std::string(". ZMQ: " + std::string(ex.what())); + return false; + } +} + +bool COdrModCtrl::SendMessage(zmq::socket_t* pSocket, + const std::vector &message, std::string &error) +{ + error = ""; + try + { + std::vector::size_type i = 0; + for ( ; i < message.size() - 1; i++) + { + zmq::message_t zmqMsg(message[i].length()); + memcpy ((void*) zmqMsg.data(), message[i].data(), message[i].length()); + pSocket->send(zmqMsg, ZMQ_SNDMORE); + } + zmq::message_t zmqMsg(message[i].length()); + memcpy ((void*) zmqMsg.data(), message[i].data(), message[i].length()); + pSocket->send(zmqMsg, 0); + return true; + } + catch(zmq::error_t &ex) + { + error = "ZMQ send error: " + std::string(ex.what()); + return false; + } +} + +bool COdrModCtrl::RecvAll(zmq::socket_t* pSocket, + std::vector &message, unsigned int timeoutMs, + std::string &error) +{ + error = ""; + message.clear(); + + int more = -1; + size_t more_size = sizeof(more); + zmq::pollitem_t pollItems[] = { {*pSocket, 0, ZMQ_POLLIN, 0} }; + zmq::poll(&pollItems[0], 1, timeoutMs); + + while (more != 0) + { + if (pollItems[0].revents & ZMQ_POLLIN) + { + zmq::message_t msg; + pSocket->recv(&msg); + message.push_back(std::string((char*)msg.data(), msg.size())); + pSocket->getsockopt(ZMQ_RCVMORE, &more, &more_size); + } + else + { + error = "Receive timeout"; + return false; + } + } + + return true; +} + diff --git a/doc/zmq-ctrl/cpp/OdrModCtrl.hpp b/doc/zmq-ctrl/cpp/OdrModCtrl.hpp new file mode 100644 index 0000000..0651311 --- /dev/null +++ b/doc/zmq-ctrl/cpp/OdrModCtrl.hpp @@ -0,0 +1,86 @@ +/** + * This is an interface for the zmq ctrl API of the odr-dabmod. + * The class is intended for clients that wish to control the odr-mod. + * + * Copyright (c) 2015 by Jörgen Scott (jorgen.scott@paneda.se) + * + * 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 + +#include +#include +#include +#include + +class COdrModCtrl +{ + public: + // ctors + COdrModCtrl(zmq::context_t *pContext, std::string odrEndpoint, + unsigned int timeoutMs); + virtual ~COdrModCtrl(); + + // All methods return true if successful, when false check the error + // string. + // + // IMPORTANT! All methods must be accessed from the same thread. + // + // For a detailed description of the various parameters, see + // example.ini. + virtual bool GetDigitalGain(double &gain, std::string &error); + virtual bool GetTxGain(double &gain, std::string &error); + virtual bool GetTxFrequency(double &freqHz, std::string &error); + virtual bool GetMuting(bool &mute, std::string &error); + virtual bool GetStaticDelay(uint32_t &delayUs, std::string &error); + + virtual bool SetDigitalGain(const double gain, std::string &error); + virtual bool SetTxGain(const double gain, std::string &error); + virtual bool SetTxFrequency(const double freqHz, std::string &error); + virtual bool SetMuting(const bool mute, std::string &error); + virtual bool SetStaticDelay(const uint32_t delayUs, std::string &error); + + private: + // methods + + template + bool DoSet(const std::string module, const std::string parameter, + const Type value, std::string &error); + + bool ParseSetReply(const std::vector &msg, std::string &error); + + template + bool DoGet(const std::string module, const std::string parameter, + Type &value, std::string &error); + + template + bool ParseGetReply(const std::vector &msg, Type &value, + std::string &error); + + bool ConnectSocket(zmq::socket_t *pSocket, const std::string endpoint, + std::string &error); + + bool SendMessage(zmq::socket_t* pSocket, + const std::vector &message, std::string &error); + + bool RecvAll(zmq::socket_t* pSocket, + std::vector &message, unsigned int timeoutMs, + std::string &error); + + // data + zmq::context_t *m_pContext; + std::string m_odrEndpoint; + uint32_t m_timeoutMs; + zmq::socket_t *m_pReqSocket; +}; diff --git a/doc/zmq-ctrl/cpp/test/CMakeLists.txt b/doc/zmq-ctrl/cpp/test/CMakeLists.txt new file mode 100755 index 0000000..4b877d0 --- /dev/null +++ b/doc/zmq-ctrl/cpp/test/CMakeLists.txt @@ -0,0 +1,31 @@ +cmake_minimum_required(VERSION 2.6) +project (ctrl_test) + +ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK -D_SCL_SECURE_NO_WARNINGS) + +set(BOOST_LIBRARYDIR) +set(BOOST_INCLUDEDIR) +set(BOOST_USE_MULTITHREADED ON) +set(BOOST_USE_STATIC_LIBS ON) +set(BOOST_MIN_VERSION 1.55) + +find_package( Boost ${BOOST_MIN_VERSION} REQUIRED + unit_test_framework + system + ) + +set(PROJECT_TEST_SRCS +${CMAKE_CURRENT_SOURCE_DIR}/ctrl_test.cpp +${CMAKE_CURRENT_SOURCE_DIR}/../OdrModCtrl.cpp +) + +include_directories( ${PROJECT_SOURCE_DIR}/../ ) +link_directories (/usr/local/lib) +add_executable(${PROJECT_NAME} ${PROJECT_TEST_SRCS}) + +target_link_libraries(${PROJECT_NAME} + zmq + ${Boost_LIBRARIES} + ) + +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") diff --git a/doc/zmq-ctrl/cpp/test/README b/doc/zmq-ctrl/cpp/test/README new file mode 100644 index 0000000..76a1188 --- /dev/null +++ b/doc/zmq-ctrl/cpp/test/README @@ -0,0 +1,14 @@ +Instructions for zmq ctrl api test program + +Dependencies boost, zmq (and cpp binding through zmq.hpp) + +Build instruction (make sure your in the directory of this file) +* mkdir build +* cd build +* cmake ../ +* make + +Run +* make sure the ODR-DABMOD is started and that zmq ctrl api is enabled +* make sure the zmq endpoint matches (see ctrl_test.cpp) +* run the ctrl_test diff --git a/doc/zmq-ctrl/cpp/test/ctrl_test.cpp b/doc/zmq-ctrl/cpp/test/ctrl_test.cpp new file mode 100644 index 0000000..3c38c89 --- /dev/null +++ b/doc/zmq-ctrl/cpp/test/ctrl_test.cpp @@ -0,0 +1,88 @@ +/** + * This is a test program for the zmq ctrl API of the odr-dabmod. + * + * Copyright (c) 2015 by Jörgen Scott (jorgen.scott@paneda.se) + + * This file is part of CtrlTest. + * + * 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 . + **/ +#define BOOST_TEST_MODULE "C++ unit tests for odr-mod zmq ctrl" +#include +#include "OdrModCtrl.hpp" + + +// Variables used in the test suite +struct TemplateVars +{ + std::string error; + zmq::context_t context; + COdrModCtrl modCtrl; + + // NOTE: Make sure the odr-dabmod is started before running the test and + // that the zmq endpoint matches. + TemplateVars() : context(1), modCtrl(&context, "tcp://127.0.0.1:9400", 1000) {} + ~TemplateVars() {} +}; + +// Note. The odr-mod do not validate parameters therefore there are no tests +// made for setting invalid parameters. +BOOST_FIXTURE_TEST_SUITE(test_template1, TemplateVars) + +BOOST_AUTO_TEST_CASE (DigitalGain) +{ + BOOST_CHECK(modCtrl.SetDigitalGain(0.5, error) == true); + double value; + BOOST_CHECK(modCtrl.GetDigitalGain(value, error) == true); + BOOST_CHECK(value == 0.5); +} + +BOOST_AUTO_TEST_CASE (TxGain) +{ + BOOST_CHECK(modCtrl.SetTxGain(50, error) == true); + double value; + BOOST_CHECK(modCtrl.GetTxGain(value, error) == true); + BOOST_CHECK(value == 50); +} + +BOOST_AUTO_TEST_CASE (TxFrequency) +{ + BOOST_CHECK(modCtrl.SetTxFrequency(234208000, error) == true); + double value; + BOOST_CHECK(modCtrl.GetTxFrequency(value, error) == true); + BOOST_CHECK(value == 234208000); +} + +BOOST_AUTO_TEST_CASE (Muting) +{ + BOOST_CHECK(modCtrl.SetMuting(true, error) == true); + bool value; + BOOST_CHECK(modCtrl.GetMuting(value, error) == true); + BOOST_CHECK(value == true); + BOOST_CHECK(modCtrl.SetMuting(false, error) == true); +} + +BOOST_AUTO_TEST_CASE (StaticDelay) +{ + // reset first or else test will fail on successive calls + BOOST_CHECK(modCtrl.SetStaticDelay(-1, error) == true); + BOOST_CHECK(modCtrl.SetStaticDelay(45000, error) == true); + uint32_t value; + BOOST_CHECK(modCtrl.GetStaticDelay(value, error) == true); + BOOST_CHECK(value == 45000); +} + + +BOOST_AUTO_TEST_SUITE_END() + -- cgit v1.2.3 From b31494c688ad93f2c09688a9061c7d392c6844a7 Mon Sep 17 00:00:00 2001 From: Jörgen Scott Date: Mon, 9 Feb 2015 13:36:22 +0100 Subject: added ping method + some cleaning --- doc/zmq-ctrl/cpp/OdrModCtrl.cpp | 60 ++++++++++++++++++++++++++++++------- doc/zmq-ctrl/cpp/OdrModCtrl.hpp | 1 + doc/zmq-ctrl/cpp/test/ctrl_test.cpp | 7 ++++- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/doc/zmq-ctrl/cpp/OdrModCtrl.cpp b/doc/zmq-ctrl/cpp/OdrModCtrl.cpp index 9b3f8dd..cd48fd1 100644 --- a/doc/zmq-ctrl/cpp/OdrModCtrl.cpp +++ b/doc/zmq-ctrl/cpp/OdrModCtrl.cpp @@ -36,6 +36,15 @@ #include "OdrModCtrl.hpp" #include +#define MOD_GAIN "gain" +#define MOD_UHD "uhd" + +#define PARAM_DIG_GAIN "digital" +#define PARAM_TX_GAIN "txgain" +#define PARAM_FREQ "freq" +#define PARAM_MUTE "muting" +#define PARAM_STAT_DELAY "staticdelay" + COdrModCtrl::COdrModCtrl(zmq::context_t *pContext, std::string odrEndpoint, unsigned int timeoutMs) { @@ -56,55 +65,86 @@ COdrModCtrl::~COdrModCtrl() //// public get methods ///////////////////////////////////////////////////////// bool COdrModCtrl::GetDigitalGain(double &gain, std::string &error) { - return DoGet("gain", "digital", gain, error); + return DoGet(MOD_GAIN, PARAM_DIG_GAIN, gain, error); } bool COdrModCtrl::GetTxGain(double &gain, std::string &error) { - return DoGet("uhd", "txgain", gain, error); + return DoGet(MOD_UHD, PARAM_TX_GAIN, gain, error); } bool COdrModCtrl::GetTxFrequency(double &freqHz, std::string &error) { - return DoGet("uhd", "freq", freqHz, error); + return DoGet(MOD_UHD, PARAM_FREQ, freqHz, error); } bool COdrModCtrl::GetMuting(bool &mute, std::string &error) { - return DoGet("uhd", "muting", (uint32_t&) mute, error); + return DoGet(MOD_UHD, PARAM_MUTE, (uint32_t&) mute, error); } bool COdrModCtrl::GetStaticDelay(uint32_t &delayUs, std::string &error) { - return DoGet("uhd", "staticdelay", delayUs, error); + return DoGet(MOD_UHD, PARAM_STAT_DELAY, delayUs, error); } //// public set methods ///////////////////////////////////////////////////////// +bool COdrModCtrl::Ping() +{ + std::string error; + if (m_pReqSocket == NULL) + { + m_pReqSocket = new zmq::socket_t(*m_pContext, ZMQ_REQ); + if (!ConnectSocket(m_pReqSocket, m_odrEndpoint, error)) + return false; + } + + std::vector msg; + msg.push_back("ping"); + + // send the message + if (!SendMessage(m_pReqSocket, msg, error)) + { + // destroy the socket according to the "Lazy Pirate Pattern" in + // the zmq guide + m_pReqSocket->close(); + delete m_pReqSocket; + m_pReqSocket = NULL; + return false; + } + + // wait for reply + if (!RecvAll(m_pReqSocket, msg, m_timeoutMs, error)) + return false; + + return true; +} + bool COdrModCtrl::SetDigitalGain(const double gain, std::string &error) { - return DoSet("gain", "digital", gain, error); + return DoSet(MOD_GAIN, PARAM_DIG_GAIN, gain, error); } bool COdrModCtrl::SetTxGain(const double gain, std::string &error) { - return DoSet("uhd", "txgain", gain, error); + return DoSet(MOD_UHD, PARAM_TX_GAIN, gain, error); } bool COdrModCtrl::SetTxFrequency(const double freqHz, std::string &error) { - return DoSet("uhd", "freq", freqHz, error); + return DoSet(MOD_UHD, PARAM_FREQ, freqHz, error); } bool COdrModCtrl::SetMuting(const bool mute, std::string &error) { - return DoSet("uhd", "muting", mute, error); + return DoSet(MOD_UHD, PARAM_MUTE, mute, error); } bool COdrModCtrl::SetStaticDelay(const uint32_t delayUs, std::string &error) { - return DoSet("uhd", "staticdelay", delayUs, error); + return DoSet(MOD_UHD, PARAM_STAT_DELAY, delayUs, error); } diff --git a/doc/zmq-ctrl/cpp/OdrModCtrl.hpp b/doc/zmq-ctrl/cpp/OdrModCtrl.hpp index 0651311..8b40666 100644 --- a/doc/zmq-ctrl/cpp/OdrModCtrl.hpp +++ b/doc/zmq-ctrl/cpp/OdrModCtrl.hpp @@ -39,6 +39,7 @@ class COdrModCtrl // // For a detailed description of the various parameters, see // example.ini. + virtual bool Ping(void); virtual bool GetDigitalGain(double &gain, std::string &error); virtual bool GetTxGain(double &gain, std::string &error); virtual bool GetTxFrequency(double &freqHz, std::string &error); diff --git a/doc/zmq-ctrl/cpp/test/ctrl_test.cpp b/doc/zmq-ctrl/cpp/test/ctrl_test.cpp index 3c38c89..21811d6 100644 --- a/doc/zmq-ctrl/cpp/test/ctrl_test.cpp +++ b/doc/zmq-ctrl/cpp/test/ctrl_test.cpp @@ -40,6 +40,11 @@ struct TemplateVars // made for setting invalid parameters. BOOST_FIXTURE_TEST_SUITE(test_template1, TemplateVars) +BOOST_AUTO_TEST_CASE (Ping) +{ + BOOST_CHECK(modCtrl.Ping() == true); +} + BOOST_AUTO_TEST_CASE (DigitalGain) { BOOST_CHECK(modCtrl.SetDigitalGain(0.5, error) == true); @@ -75,7 +80,7 @@ BOOST_AUTO_TEST_CASE (Muting) BOOST_AUTO_TEST_CASE (StaticDelay) { - // reset first or else test will fail on successive calls + // reset first or else test will fail on successive runs BOOST_CHECK(modCtrl.SetStaticDelay(-1, error) == true); BOOST_CHECK(modCtrl.SetStaticDelay(45000, error) == true); uint32_t value; -- cgit v1.2.3 From 958e286699348c1c2110155efd2a1682fb6a1ac4 Mon Sep 17 00:00:00 2001 From: Jörgen Scott Date: Mon, 9 Feb 2015 15:54:45 +0100 Subject: changed static delay parameter type --- doc/zmq-ctrl/cpp/OdrModCtrl.cpp | 2 +- doc/zmq-ctrl/cpp/OdrModCtrl.hpp | 2 +- doc/zmq-ctrl/cpp/test/ctrl_test.cpp | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/doc/zmq-ctrl/cpp/OdrModCtrl.cpp b/doc/zmq-ctrl/cpp/OdrModCtrl.cpp index cd48fd1..731a9af 100644 --- a/doc/zmq-ctrl/cpp/OdrModCtrl.cpp +++ b/doc/zmq-ctrl/cpp/OdrModCtrl.cpp @@ -142,7 +142,7 @@ bool COdrModCtrl::SetMuting(const bool mute, std::string &error) return DoSet(MOD_UHD, PARAM_MUTE, mute, error); } -bool COdrModCtrl::SetStaticDelay(const uint32_t delayUs, std::string &error) +bool COdrModCtrl::SetStaticDelay(const int32_t delayUs, std::string &error) { return DoSet(MOD_UHD, PARAM_STAT_DELAY, delayUs, error); } diff --git a/doc/zmq-ctrl/cpp/OdrModCtrl.hpp b/doc/zmq-ctrl/cpp/OdrModCtrl.hpp index 8b40666..e343710 100644 --- a/doc/zmq-ctrl/cpp/OdrModCtrl.hpp +++ b/doc/zmq-ctrl/cpp/OdrModCtrl.hpp @@ -50,7 +50,7 @@ class COdrModCtrl virtual bool SetTxGain(const double gain, std::string &error); virtual bool SetTxFrequency(const double freqHz, std::string &error); virtual bool SetMuting(const bool mute, std::string &error); - virtual bool SetStaticDelay(const uint32_t delayUs, std::string &error); + virtual bool SetStaticDelay(const int32_t delayUs, std::string &error); private: // methods diff --git a/doc/zmq-ctrl/cpp/test/ctrl_test.cpp b/doc/zmq-ctrl/cpp/test/ctrl_test.cpp index 21811d6..fdfd35a 100644 --- a/doc/zmq-ctrl/cpp/test/ctrl_test.cpp +++ b/doc/zmq-ctrl/cpp/test/ctrl_test.cpp @@ -80,8 +80,9 @@ BOOST_AUTO_TEST_CASE (Muting) BOOST_AUTO_TEST_CASE (StaticDelay) { - // reset first or else test will fail on successive runs - BOOST_CHECK(modCtrl.SetStaticDelay(-1, error) == true); + // reset first (by setting out of range value) or else test + // will fail on successive runs + BOOST_CHECK(modCtrl.SetStaticDelay(100000, error) == true); BOOST_CHECK(modCtrl.SetStaticDelay(45000, error) == true); uint32_t value; BOOST_CHECK(modCtrl.GetStaticDelay(value, error) == true); -- cgit v1.2.3 From 277d29a529c37a8fe59883291e43db8ff8831b22 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Thu, 12 Feb 2015 10:20:54 +0100 Subject: indentation --- src/DabMod.cpp | 2 +- src/OutputUHD.cpp | 85 ++++++++++++++++++++++++++++--------------------------- src/OutputUHD.h | 9 +++--- 3 files changed, 49 insertions(+), 47 deletions(-) diff --git a/src/DabMod.cpp b/src/DabMod.cpp index 214231c..77e5da4 100644 --- a/src/DabMod.cpp +++ b/src/DabMod.cpp @@ -535,7 +535,7 @@ int main(int argc, char* argv[]) outputuhd_conf.txgain = pt.get("uhdoutput.txgain", 0.0); outputuhd_conf.frequency = pt.get("uhdoutput.frequency", 0); std::string chan = pt.get("uhdoutput.channel", ""); - outputuhd_conf.dabMode = dabMode; + outputuhd_conf.dabMode = dabMode; if (outputuhd_conf.frequency == 0 && chan == "") { std::cerr << " UHD output enabled, but neither frequency nor channel defined.\n"; diff --git a/src/OutputUHD.cpp b/src/OutputUHD.cpp index bfd24a8..d033700 100644 --- a/src/OutputUHD.cpp +++ b/src/OutputUHD.cpp @@ -225,7 +225,7 @@ OutputUHD::OutputUHD( uwd.check_refclk_loss = true; } - SetDelayBuffer(config.dabMode); + SetDelayBuffer(config.dabMode); shared_ptr b(new barrier(2)); mySyncBarrier = b; @@ -249,30 +249,30 @@ OutputUHD::~OutputUHD() void OutputUHD::SetDelayBuffer(unsigned int dabMode) { - // find out the duration of the transmission frame (Table 2 in ETSI 300 401) + // find out the duration of the transmission frame (Table 2 in ETSI 300 401) switch (dabMode) { - case 0: // could happen when called from constructor and we take the mode from ETI - myTFDurationMs = 0; - break; - case 1: - myTFDurationMs = 96; - break; - case 2: - myTFDurationMs = 24; - break; - case 3: - myTFDurationMs = 24; - break; - case 4: - myTFDurationMs = 48; - break; - default: - throw std::runtime_error("OutPutUHD: invalid DAB mode"); + case 0: // could happen when called from constructor and we take the mode from ETI + myTFDurationMs = 0; + break; + case 1: + myTFDurationMs = 96; + break; + case 2: + myTFDurationMs = 24; + break; + case 3: + myTFDurationMs = 24; + break; + case 4: + myTFDurationMs = 48; + break; + default: + throw std::runtime_error("OutputUHD: invalid DAB mode"); } - // The buffer size equals the number of samples per transmission frame so - // we calculate it by multiplying the duration of the transmission frame - // with the samplerate. - myDelayBuf.resize(myTFDurationMs * myConf.sampleRate / 1000); + // The buffer size equals the number of samples per transmission frame so + // we calculate it by multiplying the duration of the transmission frame + // with the samplerate. + myDelayBuf.resize(myTFDurationMs * myConf.sampleRate / 1000); } int OutputUHD::process(Buffer* dataIn, Buffer* dataOut) @@ -310,10 +310,11 @@ int OutputUHD::process(Buffer* dataIn, Buffer* dataOut) default: break; } - // we only set the delay buffer from the dab mode signaled in ETI if the - // dab mode was not set in contructor - if (myTFDurationMs == 0) - SetDelayBuffer(myEtiReader->getMode()); + // we only set the delay buffer from the dab mode signaled in ETI if the + // dab mode was not set in contructor + if (myTFDurationMs == 0) { + SetDelayBuffer(myEtiReader->getMode()); + } activebuffer = 1; @@ -688,21 +689,21 @@ void OutputUHD::set_parameter(const string& parameter, const string& value) else if (parameter == "staticdelay") { int64_t adjust; ss >> adjust; - if (adjust > (myTFDurationMs * 1000)) - { // reset static delay for values outside range - myStaticDelayUs = 0; - } - else - { // the new adjust value is added to the existing delay and the result - // is wrapped around at TF duration - int newStaticDelayUs = myStaticDelayUs + adjust; - if (newStaticDelayUs > (myTFDurationMs * 1000)) - myStaticDelayUs = newStaticDelayUs - (myTFDurationMs * 1000); - else if (newStaticDelayUs < 0) - myStaticDelayUs = newStaticDelayUs + (myTFDurationMs * 1000); - else - myStaticDelayUs = newStaticDelayUs; - } + if (adjust > (myTFDurationMs * 1000)) + { // reset static delay for values outside range + myStaticDelayUs = 0; + } + else + { // the new adjust value is added to the existing delay and the result + // is wrapped around at TF duration + int newStaticDelayUs = myStaticDelayUs + adjust; + if (newStaticDelayUs > (myTFDurationMs * 1000)) + myStaticDelayUs = newStaticDelayUs - (myTFDurationMs * 1000); + else if (newStaticDelayUs < 0) + myStaticDelayUs = newStaticDelayUs + (myTFDurationMs * 1000); + else + myStaticDelayUs = newStaticDelayUs; + } } else if (parameter == "iqbalance") { ss >> myConf.frequency; diff --git a/src/OutputUHD.h b/src/OutputUHD.h index d002e98..c5d561b 100644 --- a/src/OutputUHD.h +++ b/src/OutputUHD.h @@ -171,7 +171,7 @@ struct OutputUHDConfig { double txgain; bool enableSync; bool muteNoTimestamps; - unsigned dabMode; + unsigned dabMode; /* allowed values : auto, int, sma, mimo */ std::string refclk_src; @@ -232,12 +232,13 @@ class OutputUHD: public ModOutput, public RemoteControllable { bool myMuting; private: - // methods - void SetDelayBuffer(unsigned int dabMode); + // Resize the internal delay buffer according to the dabMode and + // the sample rate. + void SetDelayBuffer(unsigned int dabMode); // data int myStaticDelayUs; // static delay in microseconds - int myTFDurationMs; // TF duration in milliseconds + int myTFDurationMs; // TF duration in milliseconds std::vector myDelayBuf; size_t lastLen; }; -- cgit v1.2.3