-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSettingsManager.cpp
273 lines (211 loc) · 6.8 KB
/
SettingsManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "SettingsManager.h"
#include "MultiChannelForwarder.h"
#include <ZeroMQSink.h>
#include <QSettings>
#define EXTRA_BW_FACTOR 1.1
template<typename ... arg>
void
SettingsManager::error(const char *fmt, arg ... args)
{
int size_s = std::snprintf(nullptr, 0, fmt, args...) + 1;
if (size_s <= 0)
throw Suscan::Exception(
__FILE__,
__LINE__,
"Failed to measure size in snprintf");
auto size = static_cast<size_t>(size_s);
char buffer[size];
std::snprintf(buffer, size, fmt, args...);
emit loadError(QString(buffer));
}
SettingsManager::SettingsManager(QObject *parent) : QObject(parent)
{
}
void
SettingsManager::abortLoad()
{
m_aborted = true;
}
bool
SettingsManager::loadSettings(const char *path)
{
QSettings settings(path, QSettings::IniFormat);
m_aborted = false;
switch (settings.status()) {
case QSettings::Status::AccessError:
error("Cannot open file %s. File access error", path);
return false;
case QSettings::Status::FormatError:
error("Cannot load settings from %s (invalid format)", path);
return false;
default:
break;
}
m_zmqAddress = settings.value("zmq_address").value<QString>();
m_tunerFreq = settings.value("center_frequency").value<qint64>();
m_lnbFreq = settings.value("mix_offset").value<qint64>();
m_correctDc = settings.value("correct_dc_bias").value<bool>();
// Read master VFOs
int msize = settings.beginReadArray("main_vfos");
for (int i = 0; i < msize && !m_aborted; ++i) {
settings.setArrayIndex(i);
// From this, we deduce frequency, bandwidth and name
auto vfo_out_rate = settings.value("out_rate").value<qint64>();
auto vfo_freq = settings.value("frequency").value<qint64>();
auto out_topic = settings.value("zmq_topic").value<QString>();
bool disabled = settings.value("SigDigger.disabled").value<bool>();
if (out_topic.size() == 0)
out_topic = "MASTER_" + QString::number(i + 1);
auto masterName = out_topic.toStdString();
auto bandwidth = vfo_out_rate * EXTRA_BW_FACTOR;
if (vfo_freq == 0) {
error(
"Central frequency of a master channel `%s' cannot be undefined (or zero)",
masterName.c_str());
return false;
}
if (vfo_out_rate == 0) {
error(
"Bandwidth of master channel `%s' cannot be undefined (or zero)",
masterName.c_str());
return false;
}
emit createMaster(out_topic, vfo_freq, bandwidth, !disabled);
}
settings.endArray();
if (m_aborted)
return false;
// Read VFOs
int size = settings.beginReadArray("vfos");
for (int i = 0; i < size && !m_aborted; ++i) {
settings.setArrayIndex(i);
// And again, from this we deduce frequency, bandwidth and name
auto filterbw = settings.value("filter_bandwidth").value<qint64>();
auto fiterbw = settings.value("fiter_bandwidth").value<qint64>();
auto vfo_freq = settings.value("frequency").value<qint64>();
auto out_topic = settings.value("topic").value<QString>();
auto demod = settings.value("SigDigger.demod").value<QString>();
auto vfo_out_rate = settings.value("out_rate").value<qint64>();
auto data_rate = settings.value("data_rate").value<qint64>();
bool disabled = settings.value("SigDigger.disabled").value<bool>();
auto channelName = out_topic.toStdString();
// Assume USB if not present
if (demod == "")
demod = "audio:usb";
if (channelName.size() == 0) {
error("Anonymous channels are not yet supported");
return false;
}
if (filterbw == 0)
filterbw = fiterbw;
if (vfo_out_rate == 0) {
switch (data_rate){
case 600:
vfo_out_rate = 12000;
break;
case 1200:
vfo_out_rate = 24000;
break;
default:
vfo_out_rate = 48000;
break;
}
}
if (filterbw == 0)
filterbw = vfo_out_rate;
if (demod == "audio:usb")
vfo_freq += filterbw / 2;
else if (demod == "audio:lsb")
vfo_freq -= filterbw / 2;
emit createVFO(out_topic, vfo_freq, filterbw, demod, vfo_out_rate, !disabled);
}
if (m_aborted)
return false;
settings.endArray();
return true;
}
QString
SettingsManager::getZmqAddres() const
{
return m_zmqAddress;
}
bool
SettingsManager::getCorrectDC() const
{
return m_correctDc;
}
SUFREQ
SettingsManager::getTunerFreq() const
{
return m_tunerFreq;
}
SUFREQ
SettingsManager::getLNBFreq() const
{
return m_lnbFreq;
}
void
SettingsManager::setZmqAddress(QString addr)
{
m_zmqAddress = addr;
}
void
SettingsManager::setCorrectDC(bool dc)
{
m_correctDc = dc;
}
void
SettingsManager::setTunerFreq(SUFREQ freq)
{
m_tunerFreq = freq;
}
void
SettingsManager::setLNBFreq(SUFREQ off)
{
m_lnbFreq = off;
}
bool
SettingsManager::saveSettings(const char *path, const MultiChannelForwarder *fwd)
{
QSettings settings(path, QSettings::IniFormat);
int ndx;
settings.setValue("zmq_address", m_zmqAddress);
settings.setValue("center_fequency", static_cast<qint64>(m_tunerFreq));
settings.setValue("mix_offset", static_cast<qint64>(m_lnbFreq));
settings.setValue("correct_dc_bias", m_correctDc);
settings.beginWriteArray("main_vfos");
ndx = 0;
for (auto i = fwd->cbegin(); i != fwd->cend(); ++i) {
const MasterChannel *master = *i;
settings.setArrayIndex(ndx++);
settings.setValue("frequency", static_cast<qint64>(master->frequency));
settings.setValue("out_rate", static_cast<qint64>(master->bandwidth / EXTRA_BW_FACTOR));
settings.setValue("out_topic", QString::fromStdString(master->name));
settings.setValue("SigDigger.disabled", !master->enabled);
}
settings.endArray();
settings.beginWriteArray("vfos");
ndx = 0;
for (ChannelHashConstIterator i = fwd->cChanHashBegin(); i != fwd->cChanHashEnd(); ++i) {
const ChannelDescription *channel = i->second;
ZeroMQConsumer *consumer = static_cast<ZeroMQConsumer *>(channel->consumer);
QString demod = QString::fromStdString(consumer->getChannelType());
qint64 bandwidth = static_cast<qint64>(channel->bandwidth);
qint64 frequency = static_cast<qint64>(channel->parent->frequency + channel->offset);
settings.setArrayIndex(ndx++);
if (demod == "audio:usb")
frequency -= bandwidth / 2;
else if (demod == "audio:lsb")
frequency += bandwidth / 2;
settings.setValue("frequency", frequency);
settings.setValue("filter_bandwidth", bandwidth);
settings.setValue("topic", QString::fromStdString(channel->name));
settings.setValue("SigDigger.demod", demod);
settings.setValue("out_rate", static_cast<qint64>(consumer->getSampRate()));
settings.setValue("SigDigger.disabled", !consumer->isEnabled());
}
settings.endArray();
settings.sync();
auto status = settings.status();
return status == QSettings::Status::NoError;
}