libcyberradio  22.01.24
RadioHandler.cpp
1 /***************************************************************************
2  * \file RadioHandler.cpp
3  * \brief Defines the radio handler interface for the NDR551.
4  * \author DA
5  * \author NH
6  * \author MN
7  * \copyright (c) 2017 CyberRadio Solutions, Inc. All rights reserved.
8  *
9  ***************************************************************************/
10 
11 #include "LibCyberRadio/Driver/NDR324/RadioHandler.h"
12 #include "LibCyberRadio/Driver/NDR551/DataPort.h"
13 #include "LibCyberRadio/Driver/NDR551/NbddcComponent.h"
14 #include "LibCyberRadio/Driver/NDR324/TunerComponent.h"
15 #include "LibCyberRadio/Driver/NDR324/VitaIfSpec.h"
16 #include "LibCyberRadio/Driver/NDR324/WbddcComponent.h"
17 //#include "LibCyberRadio/Driver/NDR551/WbddcGroupComponent.h"
18 //#include "LibCyberRadio/Driver/NDR551/NbddcGroupComponent.h"
19 #include "LibCyberRadio/Common/Pythonesque.h"
20 #include <json/json.h>
21 #include <sstream>
22 #include <cstdio>
23 #include <iostream>
24 #include <cstring>
25 
26 static uint32_t msgCounter = 1;
27 
28 namespace LibCyberRadio
29 {
30 
31  namespace Driver
32  {
33  namespace NDR324
34  {
35 
36  RadioHandler::RadioHandler(bool debug) :
37  ::LibCyberRadio::Driver::RadioHandler(
38  /* const std::string& name */ "NDR324",
39  /* int numTuner */ 4,
40  /* int tunerIndexBase */ 0,
41  /* int numWbddc */ 4,
42  /* int wbddcIndexBase */ 0,
43  /* int numNbddc */ 0,
44  /* int nbddcIndexBase */ 0,
45  /* int numTunerBoards */ 1,
46  /* int maxTunerBw */ 6000,
47  /* int numTransmitters */ 0,
48  /* int transmitterIndexBase */ 0,
49  /* int numDuc */ 0,
50  /* int ducIndexBase */ 0,
51  /* int numWbddcGroups */ 0,
52  /* int wbddcGroupIndexBase */ 1,
53  /* int numNbddcGroups */ 0,
54  /* int nbddcGroupIndexBase */ 1,
55  /* int numDdcGroups */ 0,
56  /* int ddcGroupIndexBase */ 1,
57  /* int numDataPorts */ 4,
58  /* int dataPortIndexBase */ 0,
59  /* int numSimpleIpSetups */ 0,
60  /* double adcRate */ 1351.68e6,
61  /* VitaIfSpec ifSpec */ NDR324::VitaIfSpec(),
62  /* bool debug */ debug)
63  {
65  _connModesSupported.push_back("udp");
66  _defaultDeviceInfo = 19091;
67  _transport.setJson(true);
68 
69 
70  // Allocate tuner components
71  for (int tuner = _tunerIndexBase;
72  tuner < (_tunerIndexBase + _numTuner); tuner++)
73  {
74 
75  _tuners[tuner] = new NDR324::TunerComponent(
76  /* int index */ tuner,
77  /* RadioHandler* parent */ this,
78  /* bool debug */ _debug,
79  /* double frequency */ 800e6,
80  /* double attenuation */ 0.0,
81  /* int filter */ 0);
82  }
83  // Allocate WBDDC components
84  for (int wbddc = _wbddcIndexBase;
85  wbddc < (_wbddcIndexBase + _numWbddc); wbddc++)
86  {
87  _wbddcs[wbddc] = new NDR324::WbddcComponent(
88  /* int index */ wbddc,
89  /* RadioHandler* parent */ this,
90  /* bool debug */ _debug,
91  /* int dataPort */ 1,
92  /* int rateIndex */ 0,
93  /* int udpDestination */ 0,
94  /* int vitaEnable */ 0,
95  /* int streamId */ 0);
96  }
97 #if 0
98  // Allocate NBDDC components
99  for (int nbddc = _nbddcIndexBase;
100  nbddc < (_nbddcIndexBase + _numNbddc); nbddc++)
101  {
102  _nbddcs[nbddc] = new NDR551::NbddcComponent(
103  /* int index */ nbddc,
104  /* RadioHandler* parent */ this,
105  /* bool debug */ _debug,
106  /* int dataPort */ 1,
107  /* int rateIndex */ 0,
108  /* int udpDestination */ 0,
109  /* int vitaEnable */ 0,
110  /* int streamId */ 0,
111  /* double frequency */ 0.0,
112  /* int source */ 1);
113  }
114 
115  // Allocate WBDDC group components
116  for (int group = _wbddcGroupIndexBase;
117  group < (_wbddcGroupIndexBase + _numWbddcGroups); group++)
118  {
119  _wbddcGroups[group] = new NDR551::WbddcGroupComponent(
120  /* int index */ group,
121  /* RadioHandler* parent */ this,
122  /* bool debug */ _debug);
123  }
124  // Allocate NBDDC group components
125  for (int group = _nbddcGroupIndexBase;
126  group < (_nbddcGroupIndexBase + _numNbddcGroups); group++)
127  {
128  _nbddcGroups[group] = new NDR551::NbddcGroupComponent(
129  /* int index */ group,
130  /* RadioHandler* parent */ this,
131  /* bool debug */ _debug);
132  }
133 #endif
134  // Allocate data ports
135  for (int dataPort = _dataPortIndexBase;
136  dataPort < (_dataPortIndexBase + _numDataPorts); dataPort++)
137  {
138  _dataPorts[dataPort] = new NDR551::DataPort(
139  /* int index */ dataPort,
140  /* RadioHandler* parent */ this,
141  /* bool debug */ _debug,
142  /* const std::string& sourceIP */ "0.0.0.0");
143  }
144  }
145 
147  {
148  }
149 
150  RadioHandler::RadioHandler(const RadioHandler &other) :
151  ::LibCyberRadio::Driver::RadioHandler(other)
152  {
153  }
154  // Default implementation is the NDR308 pattern
156  {
157  //this->debug("[RadioHandler::initConfigurationDict] Called\n");
158  _config.clear();
159  _config["referenceMode"] = _referenceMode;
160  //this->debug("[RadioHandler::initConfigurationDict] Returning\n");
161  }
162 
163  RadioHandler& RadioHandler::operator=(const RadioHandler& other)
164  {
165  ::LibCyberRadio::Driver::RadioHandler::operator=(other);
166  // Block self-assignment
167  if (this != &other)
168  {
169  }
170  return *this;
171  }
172 
174  {
175  this->debug("[NDR324]::RadioHandler::queryConfiguration] Called\n");
176  // Purge the banner sent over when a connection is made.
177  //BasicStringList rsp = _transport.receive(_defaultTimeout);
178  // Call the base-class queryConfiguration() to retrieve identity info
179  // and query configuration for all components
180  Json::Value root(Json::objectValue);
181  root["msg"] = this->getMessageId();
182  root["cmd"] = "qstatus";
183  Json::Value params(Json::objectValue);
184  root["params"] = params;
185  Json::FastWriter fastWriter;
186  std::string output = fastWriter.write(root);
188  if ( _config.hasKey("referenceMode") )
189  {
190  this->executeReferenceModeQuery(_referenceMode);
191  }
192  this->debug("[NDR324::RadioHandler::queryConfiguration] Returning\n");
193 
194  for ( TunerComponentDict::iterator it = _tuners.begin();
195  it != _tuners.end(); it++)
196  {
197  it->second->queryConfiguration();
198  }
199  for ( WbddcComponentDict::iterator it = _wbddcs.begin();
200  it != _wbddcs.end(); it++)
201  {
202  it->second->queryConfiguration();
203  }
204  for ( DataPortDict::iterator it = _dataPorts.begin();
205  it != _dataPorts.end(); it++)
206  {
207  it->second->queryConfiguration();
208  }
209  this->queryVersionInfo();
210  }
211 
212  bool RadioHandler::query324Specifics()
213  {
214  Json::Value root(Json::objectValue);
215  root["msg"] = this->getMessageId();
216  root["cmd"] = "cli";
217  Json::Value params(Json::objectValue);
218  params["input"] = "version";
219  root["params"] = params;
220  Json::FastWriter fastWriter;
221  std::string output = fastWriter.write(root);
223  Json::Reader reader;
224  Json::Value returnVal;
225  std::string t = rsp.at(0);
226  bool parsingSuccessful = reader.parse( t.c_str(), returnVal ); //parse process
227  if( parsingSuccessful ){
228  std::string result = returnVal["result"].asString();
229  LibCyberRadio::BasicStringList l = Pythonesque::Split(Pythonesque::Lstrip(result), "\n"); //Pythonesque::Split(Pythonesque::Replace(result," ", ""), "\n");
230  for(int i = 0; i < l.size(); i++)
231  {
232  std::string temp = l.at(i);
233  temp = Pythonesque::Replace(temp," ", "").c_str();
234  BasicStringList J = Pythonesque::Split(temp, ":");
235  const char *unitsn = "UnitSN";
236  if ( std::strstr(temp.c_str(), unitsn) != nullptr )
237  {
238  _versionInfo["serialNumber"] = J.at(1).c_str();
239  }
240  }
241  }
242  return true;
243  }
244 
245  bool RadioHandler::queryVersionInfo()
246  {
247  this->debug("[RadioHandler::queryVersionInfo] Called\n");
248  // First, call the base-class version
249  bool ret = true;
250  Json::Value root(Json::objectValue);
251  root["msg"] = this->getMessageId();
252  root["cmd"] = "qstatus";
253  Json::Value params(Json::objectValue);
254  root["params"] = params;
255  Json::FastWriter fastWriter;
256  std::string output = fastWriter.write(root);
258  Json::Reader reader;
259  Json::Value returnVal;
260  std::string t = rsp.at(0);
261  bool parsingSuccessful = reader.parse( t.c_str(), returnVal ); //parse process
262  if( parsingSuccessful ){
263  _versionInfo["model"] = returnVal["result"]["model"].asString();
264  _versionInfo["softwareVersion"] = returnVal["result"]["sw"].asString();
265  _versionInfo["firmwareVersion"] = returnVal["result"]["fw"].asString();
266  _versionInfo["unitRevision"] = returnVal["result"]["unit"].asString();
267  _versionInfo["hardwareVersion"] = returnVal["result"]["unit"].asString();
268  _versionInfo["serialNumber"] = returnVal["result"]["sn"].asString();
269  }
270  //this->query324Specifics();
271  this->debug("[NDR324::RadioHandler::queryVersionInfo] Returning %s\n", debugBool(ret));
272  return ret;
273  }
274 
275  // NOTE: The default implementation is the NDR324 implementation,
276  // but this just makes it explicit in the code.
277  bool RadioHandler::executeQueryIDN(std::string& model,
278  std::string& serialNumber)
279  {
280  return ::LibCyberRadio::Driver::RadioHandler::executeQueryIDN(model, serialNumber);
281  }
282 
283  // NOTE: The default implementation is the NDR324 implementation,
284  // but this just makes it explicit in the code.
285  bool RadioHandler::executeQueryVER(std::string& softwareVersion,
286  std::string& firmwareVersion,
287  std::string& referenceVersion,
288  std::string& firmwareDate)
289  {
290 
291  return ::LibCyberRadio::Driver::RadioHandler::executeQueryVER(softwareVersion,
292  firmwareVersion,
293  referenceVersion,
294  firmwareDate);
295  }
296 
297  // NOTE: The default implementation is the NDR324 implementation,
298  // but this just makes it explicit in the code.
299  bool RadioHandler::executeQueryHREV(std::string& hardwareInfo)
300  {
301  return ::LibCyberRadio::Driver::RadioHandler::executeQueryHREV(hardwareInfo);
302  }
303 
304  uint32_t RadioHandler::getMessageId( void )
305  {
306  return msgCounter++;
307  }
308 
309  bool RadioHandler::executeReferenceModeQuery(int& refMode)
310  {
311  this->debug("[NDR324::RadioHandler::queryVersionInfo] Called\n");
312  // First, call the base-class version
313  bool ret = false;
314  Json::Value root(Json::objectValue);
315  root["msg"] = this->getMessageId();
316  root["cmd"] = "qref";
317  Json::Value params(Json::objectValue);
318  root["params"] = params;
319  Json::FastWriter fastWriter;
320  std::string output = fastWriter.write(root);
321  BasicStringList rsp = this->sendCommand(output);
322  Json::Reader reader;
323  Json::Value returnVal;
324  std::string t = rsp.at(0);
325  bool parsingSuccessful = reader.parse( t.c_str(), returnVal ); //parse process
326  if( parsingSuccessful ){
327  refMode = boost::lexical_cast<int>(returnVal["result"]["cfg10m"].asInt());
328  ret = true;
329  }
330  return ret;
331  }
332 
333  bool RadioHandler::executeReferenceModeCommand(int& refMode)
334  {
335  this->debug("[NDR324::RadioHandler::queryVersionInfo] Called\n");
336  // First, call the base-class version
337  bool ret = false;
338  Json::Value root(Json::objectValue);
339  root["msg"] = this->getMessageId();
340  root["cmd"] = "ref";
341  Json::Value params(Json::objectValue);
342  params["cfg10m"] = refMode;
343  root["params"] = params;
344  Json::FastWriter fastWriter;
345  std::string output = fastWriter.write(root);
346  BasicStringList rsp = this->sendCommand(output);
347  Json::Reader reader;
348  Json::Value returnVal;
349  std::string t = rsp.at(0);
350  bool parsingSuccessful = reader.parse( t.c_str(), returnVal ); //parse process
351  if( parsingSuccessful ){
352  ret = boost::lexical_cast<bool>(returnVal["success"].asBool());
353  }
354  return ret;
355  }
356 
357 
358  } /* namespace NDR324 */
359 
360  } /* namespace Driver */
361 
362 } /* namespace LibCyberRadio */
virtual void queryConfiguration()
Tells the radio to query its hardware configuration in order to create its configuration dictionary (...
Radio handler class for the NDR551.
Definition: RadioHandler.h:180
virtual bool hasKey(const std::string &key) const
Determines if the dictionary has the given key.
static std::string Replace(const std::string &str, const std::string &oldstr, const std::string &newstr, int count=INT_MAX)
Replaces occurrences of one substring with another within the given string.
Definition: Pythonesque.cpp:62
virtual void initConfigurationDict()
Initializes the configuration dictionary, defining the allowed keys.
static BasicStringList Split(const std::string &str, const std::string &sep, int maxsplit=INT_MAX)
Splits the given string into a list of string tokens.
Definition: Pythonesque.cpp:77
virtual int debug(const char *format,...)
Outputs debug information.
Definition: Debuggable.cpp:95
BASIC_LIST_CONTAINER< std::string > BasicStringList
Type representing a list of strings.
Definition: BasicList.h:25
Defines functionality for LibCyberRadio applications.
Definition: App.h:23
virtual ~RadioHandler()
Destroys a RadioHandler object.
static std::string Lstrip(const std::string &str, const std::string &chars=" \\\)
Strips leading whitespace from the given string.
Definition: Pythonesque.cpp:25
virtual const char * debugBool(bool x)
Gets a debug output string for a Boolean value.
Definition: Debuggable.cpp:126
virtual BasicStringList sendCommand(const std::string &cmdString, double timeout=-1)
Sends a command to the radio.
uint32_t getMessageId(void)
Returns a Time for the msg parameter.
void setJson(bool json)
Allows user to set JSON.
virtual void initConfigurationDict()
Initializes the configuration dictionary, defining the allowed keys.