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/test/ctrl_test.cpp | 88 +++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 doc/zmq-ctrl/cpp/test/ctrl_test.cpp (limited to 'doc/zmq-ctrl/cpp/test/ctrl_test.cpp') 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(-) (limited to 'doc/zmq-ctrl/cpp/test/ctrl_test.cpp') 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(-) (limited to 'doc/zmq-ctrl/cpp/test/ctrl_test.cpp') 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