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.hpp | 86 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 doc/zmq-ctrl/cpp/OdrModCtrl.hpp (limited to 'doc/zmq-ctrl/cpp/OdrModCtrl.hpp') 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; +}; -- 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/OdrModCtrl.hpp') 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/OdrModCtrl.hpp') 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