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.
preset.cpp
Go to the documentation of this file.
1 // Copyright (C) 2015-2019 Edouard Griffiths, F4EXB. //
3 // //
4 // Swagger server adapter interface //
5 // //
6 // This program is free software; you can redistribute it and/or modify //
7 // it under the terms of the GNU General Public License as published by //
8 // the Free Software Foundation as version 3 of the License, or //
9 // (at your option) any later version. //
10 // //
11 // This program is distributed in the hope that it will be useful, //
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
14 // GNU General Public License V3 for more details. //
15 // //
16 // You should have received a copy of the GNU General Public License //
17 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
19 
20 #include "util/simpleserializer.h"
21 #include "settings/preset.h"
22 
23 #include <QDebug>
24 
26 {
28 }
29 
31 {
32  m_sourcePreset = true;
33  m_group = "default";
34  m_description = "no name";
36  m_spectrumConfig.clear();
37  m_layout.clear();
38  m_channelConfigs.clear();
39  m_dcOffsetCorrection = false;
41 }
42 
43 QByteArray Preset::serialize() const
44 {
45 // qDebug("Preset::serialize: m_group: %s mode: %s m_description: %s m_centerFrequency: %llu",
46 // qPrintable(m_group),
47 // m_sourcePreset ? "Rx" : "Tx",
48 // qPrintable(m_description),
49 // m_centerFrequency);
50 
51  SimpleSerializer s(1);
52 
53  s.writeString(1, m_group);
56  s.writeBlob(4, m_layout);
59 
60  s.writeS32(20, m_deviceConfigs.size());
61 
62  for (int i = 0; i < m_deviceConfigs.size(); i++)
63  {
64  s.writeString(24 + i*4, m_deviceConfigs[i].m_deviceId);
65  s.writeString(25 + i*4, m_deviceConfigs[i].m_deviceSerial);
66  s.writeS32(26 + i*4, m_deviceConfigs[i].m_deviceSequence);
67  s.writeBlob(27 + i*4, m_deviceConfigs[i].m_config);
68 
69 // qDebug("Preset::serialize: source: id: %s, ser: %s, seq: %d",
70 // qPrintable(m_deviceConfigs[i].m_deviceId),
71 // qPrintable(m_deviceConfigs[i].m_deviceSerial),
72 // m_deviceConfigs[i].m_deviceSequence);
73 
74  if (i >= (200-23)/4) // full!
75  {
76  qWarning("Preset::serialize: too many sources");
77  break;
78  }
79  }
80 
81  s.writeS32(200, m_channelConfigs.size());
82 
83  for(int i = 0; i < m_channelConfigs.size(); i++)
84  {
85 // qDebug("Preset::serialize: channel: id: %s", qPrintable(m_channelConfigs[i].m_channel));
86 
87  s.writeString(201 + i * 2, m_channelConfigs[i].m_channelIdURI);
88  s.writeBlob(202 + i * 2, m_channelConfigs[i].m_config);
89  }
90 
91  return s.final();
92 }
93 
94 bool Preset::deserialize(const QByteArray& data)
95 {
96  SimpleDeserializer d(data);
97 
98  if (!d.isValid())
99  {
100  resetToDefaults();
101  return false;
102  }
103 
104  if (d.getVersion() == 1)
105  {
106  d.readString(1, &m_group, "default");
107  d.readString(2, &m_description, "no name");
108  d.readU64(3, &m_centerFrequency, 0);
109  d.readBlob(4, &m_layout);
110  d.readBlob(5, &m_spectrumConfig);
111  d.readBool(6, &m_sourcePreset, true);
112 
113 // qDebug("Preset::deserialize: m_group: %s mode: %s m_description: %s m_centerFrequency: %llu",
114 // qPrintable(m_group),
115 // m_sourcePreset ? "Rx" : "Tx",
116 // qPrintable(m_description),
117 // m_centerFrequency);
118 
119  qint32 sourcesCount = 0;
120  d.readS32(20, &sourcesCount, 0);
121 
122  if (sourcesCount >= (200-23)/4) // limit was hit!
123  {
124  sourcesCount = ((200-23)/4) - 1;
125  }
126 
127  m_deviceConfigs.clear();
128 
129  for (int i = 0; i < sourcesCount; i++)
130  {
131  QString sourceId, sourceSerial;
132  int sourceSequence;
133  QByteArray sourceConfig;
134 
135  d.readString(24 + i*4, &sourceId, "");
136  d.readString(25 + i*4, &sourceSerial, "");
137  d.readS32(26 + i*4, &sourceSequence, 0);
138  d.readBlob(27 + i*4, &sourceConfig);
139 
140  if (!sourceId.isEmpty())
141  {
142 // qDebug("Preset::deserialize: source: id: %s, ser: %s, seq: %d",
143 // qPrintable(sourceId),
144 // qPrintable(sourceSerial),
145 // sourceSequence);
146 
147  m_deviceConfigs.append(DeviceConfig(sourceId, sourceSerial, sourceSequence, sourceConfig));
148  }
149  }
150 
151  qint32 channelCount;
152  d.readS32(200, &channelCount, 0);
153 
154  m_channelConfigs.clear();
155 
156  for (int i = 0; i < channelCount; i++)
157  {
158  QString channel;
159  QByteArray config;
160 
161  d.readString(201 + i * 2, &channel, "unknown-channel");
162  d.readBlob(202 + i * 2, &config);
163 
164 // qDebug("Preset::deserialize: channel: id: %s", qPrintable(channel));
165  m_channelConfigs.append(ChannelConfig(channel, config));
166  }
167 
168  return true;
169  }
170  else
171  {
172  resetToDefaults();
173  return false;
174  }
175 }
176 
177 void Preset::addOrUpdateDeviceConfig(const QString& sourceId,
178  const QString& sourceSerial,
179  int sourceSequence,
180  const QByteArray& config)
181 {
182  DeviceeConfigs::iterator it = m_deviceConfigs.begin();
183 
184  for (; it != m_deviceConfigs.end(); ++it)
185  {
186  if (it->m_deviceId == sourceId)
187  {
188  if (sourceSerial.isNull() || sourceSerial.isEmpty())
189  {
190  if (it->m_deviceSequence == sourceSequence)
191  {
192  break;
193  }
194  }
195  else
196  {
197  if (it->m_deviceSerial == sourceSerial)
198  {
199  break;
200  }
201  }
202  }
203  }
204 
205  if (it == m_deviceConfigs.end())
206  {
207  m_deviceConfigs.append(DeviceConfig(sourceId, sourceSerial, sourceSequence, config));
208  }
209  else
210  {
211  it->m_config = config;
212  }
213 }
214 
215 const QByteArray* Preset::findBestDeviceConfig(const QString& sourceId,
216  const QString& sourceSerial,
217  int sourceSequence) const
218 {
219  // Special case for SoapySDR based on serial (driver name)
220  if (sourceId == "sdrangel.samplesource.soapysdrinput") {
221  return findBestDeviceConfigSoapy(sourceId, sourceSerial);
222  } else if (sourceId == "sdrangel.samplesource.soapysdroutput") {
223  return findBestDeviceConfigSoapy(sourceId, sourceSerial);
224  }
225 
226  DeviceeConfigs::const_iterator it = m_deviceConfigs.begin();
227  DeviceeConfigs::const_iterator itFirstOfKind = m_deviceConfigs.end();
228  DeviceeConfigs::const_iterator itMatchSequence = m_deviceConfigs.end();
229 
230  for (; it != m_deviceConfigs.end(); ++it)
231  {
232  if (it->m_deviceId == sourceId)
233  {
234  if (itFirstOfKind == m_deviceConfigs.end())
235  {
236  itFirstOfKind = it;
237  }
238 
239  if (sourceSerial.isNull() || sourceSerial.isEmpty())
240  {
241  if (it->m_deviceSequence == sourceSequence)
242  {
243  break;
244  }
245  }
246  else
247  {
248  if (it->m_deviceSerial == sourceSerial)
249  {
250  break;
251  }
252  else if(it->m_deviceSequence == sourceSequence)
253  {
254  itMatchSequence = it;
255  }
256  }
257  }
258  }
259 
260  if (it == m_deviceConfigs.end()) // no exact match
261  {
262  if (itMatchSequence != m_deviceConfigs.end()) // match sequence ?
263  {
264  qDebug("Preset::findBestSourceConfig: sequence matched: id: %s ser: %s seq: %d",
265  qPrintable(itMatchSequence->m_deviceId), qPrintable(itMatchSequence->m_deviceSerial), itMatchSequence->m_deviceSequence);
266  return &(itMatchSequence->m_config);
267  }
268  else if (itFirstOfKind != m_deviceConfigs.end()) // match source type ?
269  {
270  qDebug("Preset::findBestSourceConfig: first of kind matched: id: %s ser: %s seq: %d",
271  qPrintable(itFirstOfKind->m_deviceId), qPrintable(itFirstOfKind->m_deviceSerial), itFirstOfKind->m_deviceSequence);
272  return &(itFirstOfKind->m_config);
273  }
274  else // definitely not found !
275  {
276  qDebug("Preset::findBestSourceConfig: no match");
277  return 0;
278  }
279  }
280  else // exact match
281  {
282  qDebug("Preset::findBestSourceConfig: serial matched (exact): id: %s ser: %s",
283  qPrintable(it->m_deviceId), qPrintable(it->m_deviceSerial));
284  return &(it->m_config);
285  }
286 }
287 
288 const QByteArray* Preset::findBestDeviceConfigSoapy(const QString& sourceId, const QString& sourceSerial) const
289 {
290  QStringList sourceSerialPieces = sourceSerial.split("-");
291 
292  if (sourceSerialPieces.size() == 0) {
293  return 0; // unable to process
294  }
295 
296  DeviceeConfigs::const_iterator it = m_deviceConfigs.begin();
297  DeviceeConfigs::const_iterator itFirstOfKind = m_deviceConfigs.end();
298 
299  for (; it != m_deviceConfigs.end(); ++it)
300  {
301  if (it->m_deviceId != sourceId) // skip non matching device
302  {
303  continue;
304  }
305  else if (it->m_deviceSerial == sourceSerial) // exact match
306  {
307  break;
308  }
309  else // try to find best match on driver id (first part of serial)
310  {
311  QStringList serialPieces = it->m_deviceSerial.split("-");
312 
313  if (serialPieces.size() == 0)
314  {
315  continue;
316  }
317  else if (sourceSerialPieces[0] == serialPieces[0])
318  {
319  if (itFirstOfKind == m_deviceConfigs.end())
320  {
321  itFirstOfKind = it;
322  break;
323  }
324  }
325  }
326  }
327 
328  if (it == m_deviceConfigs.end()) // no exact match
329  {
330  if (itFirstOfKind == m_deviceConfigs.end())
331  {
332  qDebug("Preset::findBestDeviceConfigSoapy: no match");
333  return 0;
334  }
335  else
336  {
337  qDebug("Preset::findBestSourceConfig: first of kind matched: id: %s ser: %s seq: %d",
338  qPrintable(itFirstOfKind->m_deviceId), qPrintable(itFirstOfKind->m_deviceSerial), itFirstOfKind->m_deviceSequence);
339  return &(itFirstOfKind->m_config);
340  }
341  }
342  else // exact match
343  {
344  qDebug("Preset::findBestDeviceConfigSoapy: serial matched (exact): id: %s ser: %s seq: %d",
345  qPrintable(it->m_deviceId), qPrintable(it->m_deviceSerial), it->m_deviceSequence);
346  return &(it->m_config);
347  }
348 }
Preset()
Definition: preset.cpp:25
void resetToDefaults()
Definition: preset.cpp:30
const QByteArray * findBestDeviceConfig(const QString &deviceId, const QString &deviceSerial, int deviceSequence) const
Definition: preset.cpp:215
quint64 m_centerFrequency
Definition: preset.h:127
void addOrUpdateDeviceConfig(const QString &deviceId, const QString &deviceSerial, int deviceSequence, const QByteArray &config)
Definition: preset.cpp:177
void writeBlob(quint32 id, const QByteArray &value)
QByteArray m_spectrumConfig
Definition: preset.h:130
bool m_sourcePreset
Definition: preset.h:122
bool readString(quint32 id, QString *result, const QString &def=QString::null) const
bool readBool(quint32 id, bool *result, bool def=false) const
bool isValid() const
DeviceeConfigs m_deviceConfigs
Definition: preset.h:140
bool readS32(quint32 id, qint32 *result, qint32 def=0) const
bool readBlob(quint32 id, QByteArray *result, const QByteArray &def=QByteArray()) const
QByteArray serialize() const
Definition: preset.cpp:43
int32_t i
Definition: decimators.h:244
const QByteArray * findBestDeviceConfigSoapy(const QString &sourceId, const QString &deviceSerial) const
Definition: preset.cpp:288
void writeS32(quint32 id, qint32 value)
quint32 getVersion() const
QString m_group
Definition: preset.h:125
QByteArray m_layout
Definition: preset.h:143
bool readU64(quint32 id, quint64 *result, quint64 def=0) const
QString m_description
Definition: preset.h:126
void writeBool(quint32 id, bool value)
bool deserialize(const QByteArray &data)
Definition: preset.cpp:94
bool m_dcOffsetCorrection
Definition: preset.h:133
void writeString(quint32 id, const QString &value)
const QByteArray & final()
bool m_iqImbalanceCorrection
Definition: preset.h:134
ChannelConfigs m_channelConfigs
Definition: preset.h:137
void writeU64(quint32 id, quint64 value)