SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
testmi.cpp
Go to the documentation of this file.
1 // Copyright (C) 2019 Edouard Griffiths, F4EXB //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #include <string.h>
19 #include <errno.h>
20 
21 #include <QDebug>
22 #include <QNetworkReply>
23 #include <QNetworkAccessManager>
24 #include <QBuffer>
25 
26 #include "SWGDeviceSettings.h"
27 #include "SWGDeviceState.h"
28 #include "SWGTestMISettings.h"
29 
30 #include "device/deviceapi.h"
31 #include "dsp/dspcommands.h"
32 #include "dsp/dspengine.h"
34 #include "dsp/devicesamplesource.h"
35 #include "dsp/filerecord.h"
36 
37 #include "testmithread.h"
38 #include "testmi.h"
39 
43 
44 
45 TestMI::TestMI(DeviceAPI *deviceAPI) :
46  m_deviceAPI(deviceAPI),
47  m_settings(),
48  m_deviceDescription(),
49  m_running(false),
50  m_masterTimer(deviceAPI->getMasterTimer())
51 {
52  m_deviceAPI->setNbSourceStreams(2);
53  m_deviceAPI->addSourceStream(true); // Add a new source stream data set in the engine - asynchronous handling of FIFOs
54  m_deviceAPI->addSourceStream(true); // Add a new source stream data set in the engine - asynchronous handling of FIFOs
55  m_sampleSinkFifos.push_back(SampleSinkFifo(96000 * 4));
56  m_sampleSinkFifos.push_back(SampleSinkFifo(96000 * 4));
57  m_networkManager = new QNetworkAccessManager();
58  connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*)));
59 }
60 
62 {
63  disconnect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*)));
64  delete m_networkManager;
65 
66  if (m_running) {
67  stop();
68  }
69 
70  std::vector<FileRecord*>::iterator it = m_fileSinks.begin();
71  int istream = 0;
72 
73  for (; it != m_fileSinks.end(); ++it, istream++)
74  {
75  m_deviceAPI->removeAncillarySink(*it, istream);
76  delete *it;
77  }
78 
79  m_deviceAPI->removeLastSourceStream(); // Remove the last source stream data set in the engine
80  m_deviceAPI->removeLastSourceStream(); // Remove the last source stream data set in the engine
81 }
82 
84 {
85  delete this;
86 }
87 
89 {
90  m_fileSinks.push_back(new FileRecord(QString("test_0_%1.sdriq").arg(m_deviceAPI->getDeviceUID())));
91  m_fileSinks.push_back(new FileRecord(QString("test_1_%1.sdriq").arg(m_deviceAPI->getDeviceUID())));
94 
96 }
97 
99 {
100  qDebug("TestMI::start");
101  QMutexLocker mutexLocker(&m_mutex);
102 
103  if (m_running) {
104  stop();
105  }
106 
107  m_testSourceThreads.push_back(new TestMIThread(&m_sampleSinkFifos[0], 0));
108  m_testSourceThreads.back()->setSamplerate(m_settings.m_streams[0].m_sampleRate);
109  m_testSourceThreads.back()->startStop(true);
110 
111  m_testSourceThreads.push_back(new TestMIThread(&m_sampleSinkFifos[1], 1));
112  m_testSourceThreads.back()->setSamplerate(m_settings.m_streams[1].m_sampleRate);
113  m_testSourceThreads.back()->startStop(true);
114 
115  mutexLocker.unlock();
116 
117  applySettings(m_settings, true);
118  m_running = true;
119 
120  return true;
121 }
122 
124 {
125  qDebug("TestMI::stop");
126  QMutexLocker mutexLocker(&m_mutex);
127 
128  std::vector<TestMIThread*>::iterator it = m_testSourceThreads.begin();
129 
130  for (; it != m_testSourceThreads.end(); ++it)
131  {
132  (*it)->startStop(false);
133  (*it)->deleteLater();
134  }
135 
136  m_testSourceThreads.clear();
137  m_running = false;
138 }
139 
140 QByteArray TestMI::serialize() const
141 {
142  return m_settings.serialize();
143 }
144 
145 bool TestMI::deserialize(const QByteArray& data)
146 {
147  bool success = true;
148 
149  if (!m_settings.deserialize(data))
150  {
152  success = false;
153  }
154 
156  m_inputMessageQueue.push(message);
157 
158  if (m_guiMessageQueue)
159  {
161  m_guiMessageQueue->push(messageToGUI);
162  }
163 
164  return success;
165 }
166 
167 const QString& TestMI::getDeviceDescription() const
168 {
169  return m_deviceDescription;
170 }
171 
172 int TestMI::getSourceSampleRate(int index) const
173 {
174  if (index < (int) m_settings.m_streams.size()) {
175  return m_settings.m_streams[index].m_sampleRate/(1<<m_settings.m_streams[index].m_log2Decim);
176  } else {
177  return 0;
178  }
179 }
180 
181 quint64 TestMI::getSourceCenterFrequency(int index) const
182 {
183  if (index < (int) m_settings.m_streams.size()) {
184  return m_settings.m_streams[index].m_centerFrequency;
185  } else {
186  return 0;
187  }
188 }
189 
190 void TestMI::setSourceCenterFrequency(qint64 centerFrequency, int index)
191 {
192  TestMISettings settings = m_settings; // note: calls copy constructor
193 
194  if (index < (int) settings.m_streams.size())
195  {
196  settings.m_streams[index].m_centerFrequency = centerFrequency;
197 
198  MsgConfigureTestSource* message = MsgConfigureTestSource::create(settings, false);
199  m_inputMessageQueue.push(message);
200 
201  if (m_guiMessageQueue)
202  {
203  MsgConfigureTestSource* messageToGUI = MsgConfigureTestSource::create(settings, false);
204  m_guiMessageQueue->push(messageToGUI);
205  }
206  }
207 }
208 
209 bool TestMI::handleMessage(const Message& message)
210 {
211  if (MsgConfigureTestSource::match(message))
212  {
214  qDebug() << "TestMI::handleMessage: MsgConfigureTestSource";
215 
216  bool success = applySettings(conf.getSettings(), conf.getForce());
217 
218  if (!success)
219  {
220  qDebug("TestMI::handleMessage: config error");
221  }
222 
223  return true;
224  }
225  else if (MsgFileRecord::match(message))
226  {
227  MsgFileRecord& conf = (MsgFileRecord&) message;
228  qDebug() << "TestMI::handleMessage: MsgFileRecord: " << conf.getStartStop();
229  int istream = conf.getStreamIndex();
230 
231  if (conf.getStartStop())
232  {
233  if (m_settings.m_fileRecordName.size() != 0) {
234  m_fileSinks[istream]->setFileName(m_settings.m_fileRecordName + "_0.sdriq");
235  } else {
236  m_fileSinks[istream]->genUniqueFileName(m_deviceAPI->getDeviceUID(), istream);
237  }
238 
239  m_fileSinks[istream]->startRecording();
240  }
241  else
242  {
243  m_fileSinks[istream]->stopRecording();
244  }
245 
246  return true;
247  }
248  else if (MsgStartStop::match(message))
249  {
250  MsgStartStop& cmd = (MsgStartStop&) message;
251  qDebug() << "TestMI::handleMessage: MsgStartStop: " << (cmd.getStartStop() ? "start" : "stop");
252 
253  if (cmd.getStartStop())
254  {
256  {
258  }
259  }
260  else
261  {
263  }
264 
267  }
268 
269  return true;
270  }
271  else
272  {
273  return false;
274  }
275 }
276 
277 bool TestMI::applySettings(const TestMISettings& settings, bool force)
278 {
279  DeviceSettingsKeys deviceSettingsKeys;
280 
281  qDebug() << "TestMI::applySettings: common: "
282  << " m_fileRecordName: " << settings.m_fileRecordName
283  << " m_useReverseAPI: " << settings.m_useReverseAPI
284  << " m_reverseAPIAddress: " << settings.m_reverseAPIAddress
285  << " m_reverseAPIPort: " << settings.m_reverseAPIPort
286  << " m_reverseAPIDeviceIndex: " << settings.m_reverseAPIDeviceIndex;
287 
288  for (unsigned int istream = 0; (istream < m_settings.m_streams.size()) && (istream < settings.m_streams.size()); istream++)
289  {
290  qDebug() << "TestMI::applySettings: stream #" << istream << ": "
291  << " m_centerFrequency: " << settings.m_streams[istream].m_centerFrequency
292  << " m_frequencyShift: " << settings.m_streams[istream].m_frequencyShift
293  << " m_sampleRate: " << settings.m_streams[istream].m_sampleRate
294  << " m_log2Decim: " << settings.m_streams[istream].m_log2Decim
295  << " m_fcPos: " << settings.m_streams[istream].m_fcPos
296  << " m_amplitudeBits: " << settings.m_streams[istream].m_amplitudeBits
297  << " m_sampleSizeIndex: " << settings.m_streams[istream].m_sampleSizeIndex
298  << " m_autoCorrOptions: " << settings.m_streams[istream].m_autoCorrOptions
299  << " m_dcFactor: " << settings.m_streams[istream].m_dcFactor
300  << " m_iFactor: " << settings.m_streams[istream].m_iFactor
301  << " m_qFactor: " << settings.m_streams[istream].m_qFactor
302  << " m_phaseImbalance: " << settings.m_streams[istream].m_phaseImbalance
303  << " m_modulation: " << settings.m_streams[istream].m_modulation
304  << " m_amModulation: " << settings.m_streams[istream].m_amModulation
305  << " m_fmDeviation: " << settings.m_streams[istream].m_fmDeviation
306  << " m_modulationTone: " << settings.m_streams[istream].m_modulationTone;
307 
308  deviceSettingsKeys.m_streamsSettingsKeys.push_back(QList<QString>());
309  QList<QString>& reverseAPIKeys = deviceSettingsKeys.m_streamsSettingsKeys.back();
310 
311  if ((m_settings.m_streams[istream].m_autoCorrOptions != settings.m_streams[istream].m_autoCorrOptions) || force)
312  {
313  reverseAPIKeys.append("autoCorrOptions");
314 
315  switch(settings.m_streams[istream].m_autoCorrOptions)
316  {
318  m_deviceAPI->configureCorrections(true, false, istream);
319  break;
321  m_deviceAPI->configureCorrections(true, true, istream);
322  break;
324  default:
325  m_deviceAPI->configureCorrections(false, false, istream);
326  break;
327  }
328  }
329 
330  if ((m_settings.m_streams[istream].m_sampleRate != settings.m_streams[istream].m_sampleRate) || force)
331  {
332  reverseAPIKeys.append("sampleRate");
333 
334  if ((istream < m_testSourceThreads.size()) && (m_testSourceThreads[istream]))
335  {
336  m_testSourceThreads[istream]->setSamplerate(settings.m_streams[istream].m_sampleRate);
337  qDebug("TestMI::applySettings: thread on stream: %u sample rate set to %d",
338  istream, settings.m_streams[istream].m_sampleRate);
339  }
340  }
341 
342  if ((m_settings.m_streams[istream].m_log2Decim != settings.m_streams[istream].m_log2Decim) || force)
343  {
344  reverseAPIKeys.append("log2Decim");
345 
346  if ((istream < m_testSourceThreads.size()) && (m_testSourceThreads[istream]))
347  {
348  m_testSourceThreads[istream]->setLog2Decimation(settings.m_streams[istream].m_log2Decim);
349  qDebug("TestMI::applySettings: thread on stream: %u set decimation to %d",
350  istream, (1<<settings.m_streams[istream].m_log2Decim));
351  }
352  }
353 
354  if ((m_settings.m_streams[istream].m_centerFrequency != settings.m_streams[istream].m_centerFrequency)
355  || (m_settings.m_streams[istream].m_fcPos != settings.m_streams[istream].m_fcPos)
356  || (m_settings.m_streams[istream].m_frequencyShift != settings.m_streams[istream].m_frequencyShift)
357  || (m_settings.m_streams[istream].m_sampleRate != settings.m_streams[istream].m_sampleRate)
358  || (m_settings.m_streams[istream].m_log2Decim != settings.m_streams[istream].m_log2Decim) || force)
359  {
360  reverseAPIKeys.append("centerFrequency");
361  reverseAPIKeys.append("fcPos");
362  reverseAPIKeys.append("frequencyShift");
363 
364  qint64 deviceCenterFrequency = DeviceSampleSource::calculateDeviceCenterFrequency(
365  settings.m_streams[istream].m_centerFrequency,
366  0, // no transverter mode
367  settings.m_streams[istream].m_log2Decim,
368  (DeviceSampleSource::fcPos_t) settings.m_streams[istream].m_fcPos,
369  settings.m_streams[istream].m_sampleRate,
370  DeviceSampleSource::FrequencyShiftScheme::FSHIFT_STD,
371  false);
372 
373  int frequencyShift = settings.m_streams[istream].m_frequencyShift;
374  quint32 devSampleRate = settings.m_streams[istream].m_sampleRate;
375 
376  if (settings.m_streams[istream].m_log2Decim != 0)
377  {
379  settings.m_streams[istream].m_log2Decim,
380  (DeviceSampleSource::fcPos_t) settings.m_streams[istream].m_fcPos,
381  settings.m_streams[istream].m_sampleRate,
383  }
384 
385  if ((istream < m_testSourceThreads.size()) && (m_testSourceThreads[istream]))
386  {
387  m_testSourceThreads[istream]->setFcPos((int) settings.m_streams[istream].m_fcPos);
388  m_testSourceThreads[istream]->setFrequencyShift(frequencyShift);
389  qDebug() << "TestMI::applySettings:"
390  << " thread on istream: " << istream
391  << " center freq: " << settings.m_streams[istream].m_centerFrequency << " Hz"
392  << " device center freq: " << deviceCenterFrequency << " Hz"
393  << " device sample rate: " << devSampleRate << "Hz"
394  << " Actual sample rate: " << devSampleRate/(1<<m_settings.m_streams[istream].m_log2Decim) << "Hz"
395  << " f shift: " << settings.m_streams[istream].m_frequencyShift;
396  }
397  }
398 
399  if ((m_settings.m_streams[istream].m_amplitudeBits != settings.m_streams[istream].m_amplitudeBits) || force)
400  {
401  reverseAPIKeys.append("amplitudeBits");
402 
403  if ((istream < m_testSourceThreads.size()) && (m_testSourceThreads[istream])) {
404  m_testSourceThreads[istream]->setAmplitudeBits(settings.m_streams[istream].m_amplitudeBits);
405  }
406  }
407 
408  if ((m_settings.m_streams[istream].m_dcFactor != settings.m_streams[istream].m_dcFactor) || force)
409  {
410  reverseAPIKeys.append("dcFactor");
411 
412  if ((istream < m_testSourceThreads.size()) && (m_testSourceThreads[istream])) {
413  m_testSourceThreads[istream]->setDCFactor(settings.m_streams[istream].m_dcFactor);
414  }
415  }
416 
417  if ((m_settings.m_streams[istream].m_iFactor != settings.m_streams[istream].m_iFactor) || force)
418  {
419  reverseAPIKeys.append("iFactor");
420 
421  if ((istream < m_testSourceThreads.size()) && (m_testSourceThreads[istream])) {
422  m_testSourceThreads[istream]->setIFactor(settings.m_streams[istream].m_iFactor);
423  }
424  }
425 
426  if ((m_settings.m_streams[istream].m_qFactor != settings.m_streams[istream].m_qFactor) || force)
427  {
428  reverseAPIKeys.append("qFactor");
429 
430  if ((istream < m_testSourceThreads.size()) && (m_testSourceThreads[istream])) {
431  m_testSourceThreads[istream]->setQFactor(settings.m_streams[istream].m_qFactor);
432  }
433  }
434 
435  if ((m_settings.m_streams[istream].m_phaseImbalance != settings.m_streams[istream].m_phaseImbalance) || force)
436  {
437  reverseAPIKeys.append("phaseImbalance");
438 
439  if ((istream < m_testSourceThreads.size()) && (m_testSourceThreads[istream])) {
440  m_testSourceThreads[istream]->setPhaseImbalance(settings.m_streams[istream].m_phaseImbalance);
441  }
442  }
443 
444  if ((m_settings.m_streams[istream].m_sampleSizeIndex != settings.m_streams[istream].m_sampleSizeIndex) || force)
445  {
446  reverseAPIKeys.append("sampleSizeIndex");
447 
448  if ((istream < m_testSourceThreads.size()) && (m_testSourceThreads[istream])) {
449  m_testSourceThreads[istream]->setBitSize(settings.m_streams[istream].m_sampleSizeIndex);
450  }
451  }
452 
453  if ((m_settings.m_streams[istream].m_sampleRate != settings.m_streams[istream].m_sampleRate)
454  || (m_settings.m_streams[istream].m_centerFrequency != settings.m_streams[istream].m_centerFrequency)
455  || (m_settings.m_streams[istream].m_log2Decim != settings.m_streams[istream].m_log2Decim)
456  || (m_settings.m_streams[istream].m_fcPos != settings.m_streams[istream].m_fcPos) || force)
457  {
458  int sampleRate = settings.m_streams[istream].m_sampleRate/(1<<settings.m_streams[istream].m_log2Decim);
459  DSPSignalNotification notif(sampleRate, settings.m_streams[istream].m_centerFrequency);
460  m_fileSinks[istream]->handleMessage(notif); // forward to file sink
462  sampleRate, settings.m_streams[istream].m_centerFrequency, true, istream);
464  }
465 
466  if ((m_settings.m_streams[istream].m_modulationTone != settings.m_streams[istream].m_modulationTone) || force)
467  {
468  reverseAPIKeys.append("modulationTone");
469 
470  if ((istream < m_testSourceThreads.size()) && (m_testSourceThreads[istream])) {
471  m_testSourceThreads[istream]->setToneFrequency(settings.m_streams[istream].m_modulationTone * 10);
472  }
473  }
474 
475  if ((m_settings.m_streams[istream].m_modulation != settings.m_streams[istream].m_modulation) || force)
476  {
477  reverseAPIKeys.append("modulation");
478 
479  if ((istream < m_testSourceThreads.size()) && (m_testSourceThreads[istream]))
480  {
481  m_testSourceThreads[istream]->setModulation(settings.m_streams[istream].m_modulation);
482 
483  if (settings.m_streams[istream].m_modulation == TestMIStreamSettings::ModulationPattern0) {
484  m_testSourceThreads[istream]->setPattern0();
485  } else if (settings.m_streams[istream].m_modulation == TestMIStreamSettings::ModulationPattern1) {
486  m_testSourceThreads[istream]->setPattern1();
487  } else if (settings.m_streams[istream].m_modulation == TestMIStreamSettings::ModulationPattern2) {
488  m_testSourceThreads[istream]->setPattern2();
489  }
490  }
491  }
492 
493  if ((m_settings.m_streams[istream].m_amModulation != settings.m_streams[istream].m_amModulation) || force)
494  {
495  reverseAPIKeys.append("amModulation");
496 
497  if ((istream < m_testSourceThreads.size()) && (m_testSourceThreads[istream])) {
498  m_testSourceThreads[istream]->setAMModulation(settings.m_streams[istream].m_amModulation / 100.0f);
499  }
500  }
501 
502  if ((m_settings.m_streams[istream].m_fmDeviation != settings.m_streams[istream].m_fmDeviation) || force)
503  {
504  reverseAPIKeys.append("fmDeviation");
505 
506  if ((istream < m_testSourceThreads.size()) && (m_testSourceThreads[istream])) {
507  m_testSourceThreads[istream]->setFMDeviation(settings.m_streams[istream].m_fmDeviation * 100.0f);
508  }
509  }
510  } // for each stream index
511 
512  if (settings.m_useReverseAPI)
513  {
514  qDebug("TestMI::applySettings: call webapiReverseSendSettings");
515  bool fullUpdate = ((m_settings.m_useReverseAPI != settings.m_useReverseAPI) && settings.m_useReverseAPI) ||
519  webapiReverseSendSettings(deviceSettingsKeys, settings, fullUpdate || force);
520  }
521 
522  m_settings = settings;
523  return true;
524 }
525 
527  SWGSDRangel::SWGDeviceState& response,
528  QString& errorMessage)
529 {
530  (void) errorMessage;
532  return 200;
533 }
534 
536  bool run,
537  SWGSDRangel::SWGDeviceState& response,
538  QString& errorMessage)
539 {
540  (void) errorMessage;
542  MsgStartStop *message = MsgStartStop::create(run);
543  m_inputMessageQueue.push(message);
544 
545  if (m_guiMessageQueue) // forward to GUI if any
546  {
547  MsgStartStop *msgToGUI = MsgStartStop::create(run);
548  m_guiMessageQueue->push(msgToGUI);
549  }
550 
551  return 200;
552 }
553 
556  QString& errorMessage)
557 {
558  (void) errorMessage;
560  response.getTestMiSettings()->init();
562  return 200;
563 }
564 
566  bool force,
567  const QStringList& deviceSettingsKeys,
568  SWGSDRangel::SWGDeviceSettings& response, // query + response
569  QString& errorMessage)
570 {
571  (void) errorMessage;
572  TestMISettings settings = m_settings;
573 
574  if (deviceSettingsKeys.contains("streams"))
575  {
576  QList<SWGSDRangel::SWGTestMiStreamSettings*> *streamsSettings = response.getTestMiSettings()->getStreams();
577  QList<SWGSDRangel::SWGTestMiStreamSettings*>::const_iterator it = streamsSettings->begin();
578 
579  for (; it != streamsSettings->end(); ++it)
580  {
581  int istream = (*it)->getStreamIndex();
582 
583  if (deviceSettingsKeys.contains(tr("streams[%1].centerFrequency").arg(istream))) {
584  settings.m_streams[istream].m_centerFrequency = (*it)->getCenterFrequency();
585  }
586  if (deviceSettingsKeys.contains(tr("streams[%1].frequencyShift").arg(istream))) {
587  settings.m_streams[istream].m_frequencyShift = (*it)->getFrequencyShift();
588  }
589  if (deviceSettingsKeys.contains(tr("streams[%1].sampleRate").arg(istream))) {
590  settings.m_streams[istream].m_sampleRate = (*it)->getSampleRate();
591  }
592  if (deviceSettingsKeys.contains(tr("streams[%1].log2Decim").arg(istream))) {
593  settings.m_streams[istream].m_log2Decim = (*it)->getLog2Decim();
594  }
595  if (deviceSettingsKeys.contains(tr("streams[%1].fcPos").arg(istream))) {
596  int fcPos = (*it)->getFcPos();
597  fcPos = fcPos < 0 ? 0 : fcPos > 2 ? 2 : fcPos;
598  settings.m_streams[istream].m_fcPos = (TestMIStreamSettings::fcPos_t) fcPos;
599  }
600  if (deviceSettingsKeys.contains(tr("streams[%1].sampleSizeIndex").arg(istream))) {
601  int sampleSizeIndex = (*it)->getSampleSizeIndex();
602  sampleSizeIndex = sampleSizeIndex < 0 ? 0 : sampleSizeIndex > 1 ? 2 : sampleSizeIndex;
603  settings.m_streams[istream].m_sampleSizeIndex = sampleSizeIndex;
604  }
605  if (deviceSettingsKeys.contains(tr("streams[%1].amplitudeBits").arg(istream))) {
606  settings.m_streams[istream].m_amplitudeBits = (*it)->getAmplitudeBits();
607  }
608  if (deviceSettingsKeys.contains(tr("streams[%1].autoCorrOptions").arg(istream))) {
609  int autoCorrOptions = (*it)->getAutoCorrOptions();
610  autoCorrOptions = autoCorrOptions < 0 ? 0 : autoCorrOptions >= TestMIStreamSettings::AutoCorrLast ? TestMIStreamSettings::AutoCorrLast-1 : autoCorrOptions;
611  settings.m_streams[istream].m_sampleSizeIndex = (TestMIStreamSettings::AutoCorrOptions) autoCorrOptions;
612  }
613  if (deviceSettingsKeys.contains(tr("streams[%1].modulation").arg(istream))) {
614  int modulation = (*it)->getModulation();
615  modulation = modulation < 0 ? 0 : modulation >= TestMIStreamSettings::ModulationLast ? TestMIStreamSettings::ModulationLast-1 : modulation;
616  settings.m_streams[istream].m_modulation = (TestMIStreamSettings::Modulation) modulation;
617  }
618  if (deviceSettingsKeys.contains(tr("streams[%1].modulationTone").arg(istream))) {
619  settings.m_streams[istream].m_modulationTone = (*it)->getModulationTone();
620  }
621  if (deviceSettingsKeys.contains(tr("streams[%1].amModulation").arg(istream))) {
622  settings.m_streams[istream].m_amModulation = (*it)->getAmModulation();
623  };
624  if (deviceSettingsKeys.contains(tr("streams[%1].fmDeviation").arg(istream))) {
625  settings.m_streams[istream].m_fmDeviation = (*it)->getFmDeviation();
626  };
627  if (deviceSettingsKeys.contains(tr("streams[%1].dcFactor").arg(istream))) {
628  settings.m_streams[istream].m_dcFactor = (*it)->getDcFactor();
629  };
630  if (deviceSettingsKeys.contains(tr("streams[%1].iFactor").arg(istream))) {
631  settings.m_streams[istream].m_iFactor = (*it)->getIFactor();
632  };
633  if (deviceSettingsKeys.contains(tr("streams[%1].qFactor").arg(istream))) {
634  settings.m_streams[istream].m_qFactor = (*it)->getQFactor();
635  };
636  if (deviceSettingsKeys.contains(tr("streams[%1].phaseImbalance").arg(istream))) {
637  settings.m_streams[istream].m_phaseImbalance = (*it)->getPhaseImbalance();
638  };
639  }
640 
641  }
642 
643  if (deviceSettingsKeys.contains("fileRecordName")) {
644  settings.m_fileRecordName = *response.getTestMiSettings()->getFileRecordName();
645  }
646  if (deviceSettingsKeys.contains("useReverseAPI")) {
647  settings.m_useReverseAPI = response.getTestMiSettings()->getUseReverseApi() != 0;
648  }
649  if (deviceSettingsKeys.contains("reverseAPIAddress")) {
651  }
652  if (deviceSettingsKeys.contains("reverseAPIPort")) {
653  settings.m_reverseAPIPort = response.getTestMiSettings()->getReverseApiPort();
654  }
655  if (deviceSettingsKeys.contains("reverseAPIDeviceIndex")) {
657  }
658 
661 
662  if (m_guiMessageQueue) // forward to GUI if any
663  {
664  MsgConfigureTestSource *msgToGUI = MsgConfigureTestSource::create(settings, force);
665  m_guiMessageQueue->push(msgToGUI);
666  }
667 
668  webapiFormatDeviceSettings(response, settings);
669  return 200;
670 }
671 
673 {
674  std::vector<TestMIStreamSettings>::const_iterator it = settings.m_streams.begin();
675  int istream = 0;
676 
677  for (; it != settings.m_streams.end(); ++it, istream++)
678  {
679  QList<SWGSDRangel::SWGTestMiStreamSettings*> *streams = response.getTestMiSettings()->getStreams();
680  streams->append(new SWGSDRangel::SWGTestMiStreamSettings);
681  streams->back()->init();
682  streams->back()->setStreamIndex(istream);
683  streams->back()->setCenterFrequency(it->m_centerFrequency);
684  streams->back()->setFrequencyShift(it->m_frequencyShift);
685  streams->back()->setSampleRate(it->m_sampleRate);
686  streams->back()->setLog2Decim(it->m_log2Decim);
687  streams->back()->setFcPos((int) it->m_fcPos);
688  streams->back()->setSampleSizeIndex((int) it->m_sampleSizeIndex);
689  streams->back()->setAmplitudeBits(it->m_amplitudeBits);
690  streams->back()->setAutoCorrOptions((int) it->m_autoCorrOptions);
691  streams->back()->setModulation((int) it->m_modulation);
692  streams->back()->setModulationTone(it->m_modulationTone);
693  streams->back()->setAmModulation(it->m_amModulation);
694  streams->back()->setFmDeviation(it->m_fmDeviation);
695  streams->back()->setDcFactor(it->m_dcFactor);
696  streams->back()->setIFactor(it->m_iFactor);
697  streams->back()->setQFactor(it->m_qFactor);
698  streams->back()->setPhaseImbalance(it->m_phaseImbalance);
699  }
700 
701  if (response.getTestMiSettings()->getFileRecordName()) {
702  *response.getTestMiSettings()->getFileRecordName() = settings.m_fileRecordName;
703  } else {
704  response.getTestMiSettings()->setFileRecordName(new QString(settings.m_fileRecordName));
705  }
706 
707  response.getTestMiSettings()->setUseReverseApi(settings.m_useReverseAPI ? 1 : 0);
708 
709  if (response.getTestMiSettings()->getReverseApiAddress()) {
711  } else {
712  response.getTestMiSettings()->setReverseApiAddress(new QString(settings.m_reverseAPIAddress));
713  }
714 
717 }
718 
719 void TestMI::webapiReverseSendSettings(const DeviceSettingsKeys& deviceSettingsKeys, const TestMISettings& settings, bool force)
720 {
722  swgDeviceSettings->setDirection(0); // single Rx
723  swgDeviceSettings->setOriginatorIndex(m_deviceAPI->getDeviceSetIndex());
724  swgDeviceSettings->setDeviceHwType(new QString("TestSource"));
725  swgDeviceSettings->setTestMiSettings(new SWGSDRangel::SWGTestMISettings());
726  SWGSDRangel::SWGTestMISettings *swgTestMISettings = swgDeviceSettings->getTestMiSettings();
727 
728  // transfer data that has been modified. When force is on transfer all data except reverse API data
729 
730  QList<QList<QString>>::const_iterator it = deviceSettingsKeys.m_streamsSettingsKeys.begin();
731  int istream = 0;
732 
733  for (; it != deviceSettingsKeys.m_streamsSettingsKeys.end(); ++it, istream++)
734  {
735  if ((it->size() > 0) || force)
736  {
737  QList<SWGSDRangel::SWGTestMiStreamSettings*> *streams = swgTestMISettings->getStreams();
738  streams->append(new SWGSDRangel::SWGTestMiStreamSettings);
739  streams->back()->init();
740  streams->back()->setStreamIndex(istream);
741  const QList<QString>& streamSettingsKeys = *it;
742 
743  if (streamSettingsKeys.contains("centerFrequency") || force) {
744  streams->back()->setCenterFrequency(settings.m_streams[istream].m_centerFrequency);
745  }
746  if (streamSettingsKeys.contains("frequencyShift") || force) {
747  streams->back()->setFrequencyShift(settings.m_streams[istream].m_frequencyShift);
748  }
749  if (streamSettingsKeys.contains("sampleRate") || force) {
750  streams->back()->setSampleRate(settings.m_streams[istream].m_sampleRate);
751  }
752  if (streamSettingsKeys.contains("log2Decim") || force) {
753  streams->back()->setLog2Decim(settings.m_streams[istream].m_log2Decim);
754  }
755  if (streamSettingsKeys.contains("fcPos") || force) {
756  streams->back()->setFcPos((int) settings.m_streams[istream].m_fcPos);
757  }
758  if (streamSettingsKeys.contains("sampleSizeIndex") || force) {
759  streams->back()->setSampleSizeIndex(settings.m_streams[istream].m_sampleSizeIndex);
760  }
761  if (streamSettingsKeys.contains("amplitudeBits") || force) {
762  streams->back()->setAmplitudeBits(settings.m_streams[istream].m_amplitudeBits);
763  }
764  if (streamSettingsKeys.contains("autoCorrOptions") || force) {
765  streams->back()->setAutoCorrOptions((int) settings.m_streams[istream].m_sampleSizeIndex);
766  }
767  if (streamSettingsKeys.contains("modulation") || force) {
768  streams->back()->setModulation((int) settings.m_streams[istream].m_modulation);
769  }
770  if (streamSettingsKeys.contains("modulationTone")) {
771  streams->back()->setModulationTone(settings.m_streams[istream].m_modulationTone);
772  }
773  if (streamSettingsKeys.contains("amModulation") || force) {
774  streams->back()->setAmModulation(settings.m_streams[istream].m_amModulation);
775  };
776  if (streamSettingsKeys.contains("fmDeviation") || force) {
777  streams->back()->setFmDeviation(settings.m_streams[istream].m_fmDeviation);
778  };
779  if (streamSettingsKeys.contains("dcFactor") || force) {
780  streams->back()->setDcFactor(settings.m_streams[istream].m_dcFactor);
781  };
782  if (streamSettingsKeys.contains("iFactor") || force) {
783  streams->back()->setIFactor(settings.m_streams[istream].m_iFactor);
784  };
785  if (streamSettingsKeys.contains("qFactor") || force) {
786  streams->back()->setQFactor(settings.m_streams[istream].m_qFactor);
787  };
788  if (streamSettingsKeys.contains("phaseImbalance") || force) {
789  streams->back()->setPhaseImbalance(settings.m_streams[istream].m_phaseImbalance);
790  };
791  }
792  }
793 
794  if (deviceSettingsKeys.m_commonSettingsKeys.contains("fileRecordName") || force) {
795  swgTestMISettings->setFileRecordName(new QString(settings.m_fileRecordName));
796  }
797 
798  QString channelSettingsURL = QString("http://%1:%2/sdrangel/deviceset/%3/device/settings")
799  .arg(settings.m_reverseAPIAddress)
800  .arg(settings.m_reverseAPIPort)
801  .arg(settings.m_reverseAPIDeviceIndex);
802  m_networkRequest.setUrl(QUrl(channelSettingsURL));
803  m_networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
804 
805  QBuffer *buffer=new QBuffer();
806  buffer->open((QBuffer::ReadWrite));
807  buffer->write(swgDeviceSettings->asJson().toUtf8());
808  buffer->seek(0);
809 
810  // Always use PATCH to avoid passing reverse API settings
811  m_networkManager->sendCustomRequest(m_networkRequest, "PATCH", buffer);
812 
813  delete swgDeviceSettings;
814 }
815 
817 {
819  swgDeviceSettings->setDirection(0); // single Rx
820  swgDeviceSettings->setOriginatorIndex(m_deviceAPI->getDeviceSetIndex());
821  swgDeviceSettings->setDeviceHwType(new QString("TestSource"));
822 
823  QString channelSettingsURL = QString("http://%1:%2/sdrangel/deviceset/%3/device/run")
827  m_networkRequest.setUrl(QUrl(channelSettingsURL));
828  m_networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
829 
830  QBuffer *buffer=new QBuffer();
831  buffer->open((QBuffer::ReadWrite));
832  buffer->write(swgDeviceSettings->asJson().toUtf8());
833  buffer->seek(0);
834 
835  if (start) {
836  m_networkManager->sendCustomRequest(m_networkRequest, "POST", buffer);
837  } else {
838  m_networkManager->sendCustomRequest(m_networkRequest, "DELETE", buffer);
839  }
840 
841  delete swgDeviceSettings;
842 }
843 
844 void TestMI::networkManagerFinished(QNetworkReply *reply)
845 {
846  QNetworkReply::NetworkError replyError = reply->error();
847 
848  if (replyError)
849  {
850  qWarning() << "TestMI::networkManagerFinished:"
851  << " error(" << (int) replyError
852  << "): " << replyError
853  << ": " << reply->errorString();
854  return;
855  }
856 
857  QString answer = reply->readAll();
858  answer.chop(1); // remove last \n
859  qDebug("TestMI::networkManagerFinished: reply:\n%s", answer.toStdString().c_str());
860 }
861 
862 bool TestMI::isRecording(unsigned int istream) const
863 {
864  if (istream < m_fileSinks.size()) {
865  return m_fileSinks[istream]->isRecording();
866  } else {
867  return false;
868  }
869 }
QMutex m_mutex
Definition: testmi.h:158
virtual int webapiRunGet(SWGSDRangel::SWGDeviceState &response, QString &errorMessage)
Definition: testmi.cpp:526
bool startDeviceEngine()
Start the device engine corresponding to the stream type.
Definition: deviceapi.cpp:253
virtual void destroy()
Definition: testmi.cpp:83
QString m_reverseAPIAddress
virtual quint64 getSourceCenterFrequency(int index) const
Center frequency exposed by the source at index.
Definition: testmi.cpp:181
std::vector< FileRecord * > m_fileSinks
File sinks to record device I/Q output.
Definition: testmi.h:157
virtual void init()
initializations to be done when all collaborating objects are created and possibly connected ...
Definition: testmi.cpp:88
static qint32 calculateFrequencyShift(int log2Decim, fcPos_t fcPos, quint32 devSampleRate, FrequencyShiftScheme frequencyShiftScheme)
static qint64 calculateDeviceCenterFrequency(quint64 centerFrequency, qint64 transverterDeltaFrequency, int log2Decim, fcPos_t fcPos, quint32 devSampleRate, FrequencyShiftScheme frequencyShiftScheme, bool transverterMode=false)
QByteArray serialize() const
void push(Message *message, bool emitSignal=true)
Push message onto queue.
void stopDeviceEngine()
Stop the device engine corresponding to the stream type.
Definition: deviceapi.cpp:266
uint getDeviceUID() const
Return the current device engine unique ID.
Definition: deviceapi.cpp:303
uint16_t m_reverseAPIPort
void setUseReverseApi(qint32 use_reverse_api)
virtual int getSourceSampleRate(int index) const
Sample rate exposed by the source at index.
Definition: testmi.cpp:172
virtual QString asJson() override
bool applySettings(const TestMISettings &settings, bool force)
Definition: testmi.cpp:277
MessageQueue * getDeviceEngineInputMessageQueue()
Device engine message queue.
Definition: deviceapi.cpp:316
int getStreamIndex() const
Definition: testmi.h:66
Definition: testmi.h:35
bool initDeviceEngine()
Init the device engine corresponding to the stream type.
Definition: deviceapi.cpp:240
SWGTestMISettings * getTestMiSettings()
void networkManagerFinished(QNetworkReply *reply)
Definition: testmi.cpp:844
static MsgConfigureTestSource * create(const TestMISettings &settings, bool force)
Definition: testmi.h:45
void setOriginatorIndex(qint32 originator_index)
virtual int webapiRun(bool run, SWGSDRangel::SWGDeviceState &response, QString &errorMessage)
Definition: testmi.cpp:535
void webapiReverseSendSettings(const DeviceSettingsKeys &deviceSettingsKeys, const TestMISettings &settings, bool force)
Definition: testmi.cpp:719
Fixed< IntType, IntBits > arg(const std::complex< Fixed< IntType, IntBits > > &val)
Definition: fixed.h:2401
std::vector< SampleSinkFifo > m_sampleSinkFifos
Rx FIFOs.
int getDeviceSetIndex() const
Definition: deviceapi.h:131
#define MESSAGE_CLASS_DEFINITION(Name, BaseClass)
Definition: message.h:52
void addAncillarySink(BasebandSampleSink *sink, unsigned int index=0)
Adds a sink to receive full baseband and that is not a channel (e.g. spectrum)
Definition: deviceapi.cpp:89
TestMISettings m_settings
Definition: testmi.h:159
void setReverseApiDeviceIndex(qint32 reverse_api_device_index)
bool isRecording(unsigned int istream) const
Definition: testmi.cpp:862
static MsgStartStop * create(bool startStop)
Definition: testmi.h:89
QString m_deviceDescription
Definition: testmi.h:161
virtual int webapiSettingsGet(SWGSDRangel::SWGDeviceSettings &response, QString &errorMessage)
Definition: testmi.cpp:554
virtual bool deserialize(const QByteArray &data)
Definition: testmi.cpp:145
QList< QString > m_commonSettingsKeys
Definition: testmi.h:152
uint16_t m_reverseAPIDeviceIndex
static bool match(const Message *message)
Definition: message.cpp:45
virtual void stop()
Definition: testmi.cpp:123
std::vector< TestMIStreamSettings > m_streams
bool deserialize(const QByteArray &data)
virtual QByteArray serialize() const
Definition: testmi.cpp:140
void removeAncillarySink(BasebandSampleSink *sink, unsigned int index=0)
Removes it.
Definition: deviceapi.cpp:100
bool getStartStop() const
Definition: testmi.h:87
std::vector< TestMIThread * > m_testSourceThreads
Definition: testmi.h:160
virtual int webapiSettingsPutPatch(bool force, const QStringList &deviceSettingsKeys, SWGSDRangel::SWGDeviceSettings &response, QString &errorMessage)
Definition: testmi.cpp:565
void getDeviceEngineStateStr(QString &state)
Definition: deviceapi.cpp:389
QList< SWGTestMiStreamSettings * > * getStreams()
void setTestMiSettings(SWGTestMISettings *test_mi_settings)
void setReverseApiAddress(QString *reverse_api_address)
MessageQueue * m_guiMessageQueue
Input message queue to the GUI.
virtual const QString & getDeviceDescription() const
Definition: testmi.cpp:167
const TestMISettings & getSettings() const
Definition: testmi.h:42
void setFileRecordName(QString *file_record_name)
QNetworkAccessManager * m_networkManager
Definition: testmi.h:164
void webapiFormatDeviceSettings(SWGSDRangel::SWGDeviceSettings &response, const TestMISettings &settings)
Definition: testmi.cpp:672
virtual bool start()
Definition: testmi.cpp:98
DeviceAPI * m_deviceAPI
Definition: testmi.h:156
void webapiReverseSendStartStop(bool start)
Definition: testmi.cpp:816
bool m_running
Definition: testmi.h:162
void setReverseApiPort(qint32 reverse_api_port)
virtual void setSourceCenterFrequency(qint64 centerFrequency, int index)
Definition: testmi.cpp:190
MessageQueue m_inputMessageQueue
Input queue to the sink.
QList< QList< QString > > m_streamsSettingsKeys
Definition: testmi.h:153
void configureCorrections(bool dcOffsetCorrection, bool iqImbalanceCorrection, int streamIndex=0)
Configure current device engine DSP corrections (Rx)
Definition: deviceapi.cpp:355
bool getStartStop() const
Definition: testmi.h:65
void removeLastSourceStream()
Definition: deviceapi.cpp:68
void setDirection(qint32 direction)
QString m_fileRecordName
QNetworkRequest m_networkRequest
Definition: testmi.h:165
void setDeviceHwType(QString *device_hw_type)
virtual bool handleMessage(const Message &message)
Definition: testmi.cpp:209
virtual ~TestMI()
Definition: testmi.cpp:61