-
Notifications
You must be signed in to change notification settings - Fork 1
/
MultiChannelForwarder.h
288 lines (231 loc) · 7.16 KB
/
MultiChannelForwarder.h
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//
// MultiChannelForwarder.h: Forward samples from multiple channels
// Copyright (C) 2023 Gonzalo José Carracedo Carballal
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>
//
#ifndef MULTICHANNELFORWARDER_H
#define MULTICHANNELFORWARDER_H
#include <Suscan/Analyzer.h>
#include <Suscan/Messages/InspectorMessage.h>
#include <map>
#include <list>
#include <unordered_map>
struct ChannelDescription;
class MultiChannelForwarder;
class ChannelConsumer {
bool m_enabled = true;
public:
virtual void opened(
Suscan::Analyzer *,
Suscan::Handle,
ChannelDescription const &,
Suscan::Config const &) = 0;
virtual void samples(const SUCOMPLEX *, SUSCOUNT) = 0;
virtual void closed() = 0;
virtual void enableStateChanged(bool) = 0;
void setEnabled(bool);
bool isEnabled() const;
virtual ~ChannelConsumer();
};
struct MasterChannel;
typedef std::list<ChannelDescription>::iterator ChannelListIterator;
typedef std::list<MasterChannel *>::iterator MasterListIterator;
typedef std::list<MasterChannel *>::const_iterator MasterListConstIterator;
typedef std::unordered_map<std::string, ChannelDescription *>::const_iterator ChannelHashConstIterator;
struct ChannelDescription {
MasterChannel *parent;
std::string name;
SUFREQ offset;
SUFLOAT bandwidth;
SUFLOAT sampRate;
std::string inspClass;
Suscan::Config inspConfig;
ChannelConsumer *consumer = 0;
ChannelListIterator iter;
Suscan::Handle handle = SUSCAN_INVALID_HANDLE_VALUE;
Suscan::RequestId reqId;
bool opening = false;
bool deleted = false;
inline bool
isOpen() const
{
return handle != SUSCAN_INVALID_HANDLE_VALUE;
}
~ChannelDescription();
};
struct MasterChannel {
MultiChannelForwarder *owner;
std::string name;
SUFREQ frequency;
SUFLOAT bandwidth;
bool enabled = true;
std::list<ChannelDescription> channels;
MasterListIterator iter;
Suscan::Handle handle = SUSCAN_INVALID_HANDLE_VALUE;
Suscan::RequestId reqId;
Suscan::Config config;
bool opening = false;
unsigned int open_count = 0;
bool deleted = false;
void setEnabled(bool);
inline bool
isOpen() const
{
return handle != SUSCAN_INVALID_HANDLE_VALUE;
}
inline bool
isEmpty() const
{
return channels.empty();
}
};
class MultiChannelForwarder
{
Suscan::Analyzer *m_analyzer = nullptr;
bool m_opening = false;
bool m_opened = false;
SUFREQ m_freqMin = INFINITY;
SUFREQ m_freqMax = -INFINITY;
std::string m_errors;
bool m_failed = false;
SUFLOAT m_maxBandwidth = 2e5;
// Owner: This holds the structure of the channels to open
std::list<MasterChannel *> masterList;
// Fast lookup of master and channels
std::unordered_map<std::string, MasterChannel *> masterHash;
std::unordered_map<std::string, ChannelDescription *> channelHash;
// This is a map that enumerates opened masters
std::map<Suscan::Handle, MasterChannel *> masterMap;
std::map<Suscan::RequestId, MasterChannel *> pendingMasterMap;
bool promoteMaster(
Suscan::RequestId,
Suscan::Handle,
const suscan_config_t *);
// This is a map that relates opened channels with consumers
std::map<Suscan::Handle, ChannelDescription *> channelMap;
std::map<Suscan::RequestId, ChannelDescription *> pendingChannelMap;
bool promoteChannel(Suscan::RequestId, Suscan::Handle);
void keepOpening();
MasterChannel *getMasterFromRequest(Suscan::RequestId) const;
ChannelDescription *getChannelFromRequest(Suscan::RequestId) const;
ChannelDescription *getChannelFromHandle(Suscan::Handle) const;
MasterListIterator deleteMaster(MasterListIterator);
ChannelListIterator deleteChannel(ChannelListIterator);
// This makes sure that all channels get back to a sane state
void reset();
// Hahaha oh my God the syntax
template<typename ... arg> void error(const char *fmt, arg ...);
public:
MasterListIterator
begin()
{
return masterList.begin();
}
MasterListIterator
end()
{
return masterList.end();
}
MasterListConstIterator
cbegin() const
{
return masterList.cbegin();
}
MasterListConstIterator
cend() const
{
return masterList.cend();
}
ChannelHashConstIterator
cChanHashBegin() const
{
return channelHash.cbegin();
}
ChannelHashConstIterator
cChanHashEnd() const
{
return channelHash.cend();
}
MasterChannel *
findMaster(const char *name) const
{
auto it = masterHash.find(name);
if (it == masterHash.cend())
return nullptr;
if (it->second->deleted)
return nullptr;
return it->second;
}
ChannelDescription *
findChannel(const char *name) const
{
auto it = channelHash.find(name);
if (it == channelHash.cend())
return nullptr;
if (it->second->deleted)
return nullptr;
return it->second;
}
bool
removeMaster(const char *name)
{
auto it = masterHash.find(name);
if (it == masterHash.cend())
return false;
return removeMaster(it->second->iter);
}
bool
empty() const
{
return masterList.empty();
}
bool failed() const; // Something went wrong
std::string getErrors() const;
void clearErrors();
bool canOpen() const; // Returns if channels can be opened
bool canCenter() const; // Returns if all masters fit
bool center(); // Center masters
SUFREQ span() const;
SUFREQ getCenter() const;
bool isOpen() const;
bool isPartiallyOpen() const;
void setMaxBandwidth(SUFLOAT max);
// If track tuner is enabled, we call this periodically to update the
// LO of each master. No need to touch the channels.
void adjustLo();
void updateMasterConfig(MasterChannel *);
void setAnalyzer(Suscan::Analyzer *); // Used to update changes
void openAll(); // Used to open all masters and channels
void closeAll(); // Used to close all masters and channels
bool processMessage(Suscan::InspectorMessage const &);
bool feedSamplesMessage(Suscan::SamplesMessage const &);
MasterChannel *makeMaster(const char *, SUFREQ freq, SUFLOAT bw);
bool removeMaster(MasterListIterator);
bool removeMaster(MasterChannel *);
bool removeAll();
MasterListConstIterator findMaster(SUFREQ freq, SUFLOAT bw) const;
ChannelDescription *makeChannel(
const char *,
SUFREQ freq,
SUFLOAT bw,
const char *inspClass,
ChannelConsumer *);
bool removeChannel(ChannelListIterator);
bool removeChannel(ChannelDescription *);
MultiChannelForwarder();
~MultiChannelForwarder();
};
#endif // MULTICHANNELFORWARDER_H