-
Notifications
You must be signed in to change notification settings - Fork 5
/
uap.cpp
367 lines (294 loc) · 8.47 KB
/
uap.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* This application is free software and is released under the terms of
* the BSD license. See LICENSE file for details.
*
* Copyright (c) 2010 Volker Poplawski ([email protected])
*/
#include "uap.h"
#include <QtXml>
#include "asterix.h"
#include "scaleExpressionParser.h"
#include "global.h"
const QStringList UapDataItem::m_formatStrs = QStringList() << "fixed" << "variable" << "explicit" << "repetive" << "compound"; // must match enum UapDataItem::Format
const QStringList UapField::m_formatStrs = QStringList() << "int" << "uint" << "hex" << "octal" << "icao6str" << "ascii"; // must match enum UapField::Format
// must match enum UapField::Unit
const QStringList UapField::m_unitStrs =
QStringList() << "UNDEFINED" << "deg" << "celsius" << "deg/s" << "ft" << "ft/min" << "FL" << "kn" << "ma" <<
"m" << "m/s" << "m/s^2" << "mb" << "nmi" << "nmi/s" << "s" << "m^2" << "MHz" << "dBm";
UapField::UapField(QDomElement fieldElm) :
m_octed( Uap::parseNumber(Uap::mandAtt(fieldElm, "octed")) ),
m_msb( Uap::parseNumber(fieldElm.attribute("msb")) ),
m_lsb( Uap::parseNumber(fieldElm.attribute("lsb")) ),
m_format(HEX),
m_unit(UNDEFINED),
m_scale(1.0)
{
m_name = fieldElm.firstChildElement("name").text();
m_desc = fieldElm.firstChildElement("desc").text();
m_format = formatFromStr(fieldElm.firstChildElement("format").text());
if (not fieldElm.firstChildElement("unit").text().isEmpty())
{
m_unit = unitFromStr(fieldElm.firstChildElement("unit").text());
}
if (not fieldElm.firstChildElement("scale").text().isEmpty())
{
bool error;
m_scale = ScaleExpressionParser::parse(fieldElm.firstChildElement("scale").text(), error);
if (error)
{
logError(ScaleExpressionParser::lastError + " in <scale> expression '" + fieldElm.firstChildElement("scale").text() + "'");
m_scale = 1.0;
}
}
if (m_msb < m_lsb)
{
logWarn("Invalid msb/lsb on field '" + m_name + "'");
m_msb = m_lsb;
}
QDomNodeList enums = fieldElm.elementsByTagName("enum");
for (int i = 0; i < enums.count(); i++)
{
QDomElement enumElm = enums.at(i).toElement();
UapEnum uapEnum( Uap::parseNumber(enumElm.attribute("value")), enumElm.text() );
m_enums.insert(uapEnum.m_value, uapEnum);
}
}
const QString& UapField::formatStr(Format format)
{
return m_formatStrs[format];
}
UapField::Format UapField::formatFromStr(const QString &str)
{
int i = m_formatStrs.indexOf(str.toLower());
if (i < 0)
return HEX; // Format description unknown or empty. Default to HEX.
else
return (Format)(i);
}
const QString& UapField::unitStr(Unit unit)
{
return m_unitStrs[unit];
}
UapField::Unit UapField::unitFromStr(const QString &str)
{
int i = (Unit)m_unitStrs.indexOf(str);
if (i < 0)
{
logWarn("Unknown <unit> " + str);
return UNDEFINED; // Unit description unknown or empty.
}
else
{
return (Unit)(i);
}
}
const QString& UapDataItem::formatStr(Format format)
{
return m_formatStrs[format];
}
UapDataItem::Format UapDataItem::formatFromStr(const QString &str)
{
return (Format)m_formatStrs.indexOf(str.toLower());
}
UapDataItem::UapDataItem(QDomElement itemElm) :
m_frn( Uap::mandAtt(itemElm, "frn").toInt() ),
m_id( itemElm.attribute("id") ),
m_format( UapDataItem::formatFromStr(Uap::mandAtt(itemElm, "format")) ),
m_length( Uap::parseNumber( itemElm.attribute("length") ) )
{
m_name = itemElm.firstChildElement("name").text();
m_definition = itemElm.firstChildElement("definition").text();
m_description = extractContents(itemElm.firstChildElement("desc"));
if (m_format != COMPOUND)
{
// iterate over child elements <field>
QDomNode node = itemElm.firstChild();
while (not node.isNull())
{
if (node.toElement().tagName() == "field")
{
UapField uapField( node.toElement() );
m_fields.append(uapField);
}
node = node.nextSiblingElement();
}
}
else
{
// iterate over child elements <subfield>
QDomNode node = itemElm.firstChild();
while (not node.isNull())
{
if (node.toElement().tagName() == "subfield")
{
UapDataItem* uapItem = new UapDataItem( node.toElement() );
uapItem->m_id = m_id + "#" + QString::number(uapItem->frn());
m_subFields.insert( uapItem->frn(), uapItem);
}
node = node.nextSiblingElement();
}
}
}
QString UapDataItem::extractContents(QDomElement el)
{
QString ret;
QTextStream s(&ret);
el.save(s, 0);
return ret;
}
int UapDataItem::calcLength(const uchar *p) const
{
switch (m_format) {
case FIXED:
return m_length;
break;
case VARIABLE:
return m_length + AsterixBlock::countExtends(p+m_length-1);
break;
case EXPLICIT:
return *(const quint8*)p;
break;
case REPETIVE:
return 1 + (*(const quint8*)p * m_length);
break;
case COMPOUND:
logError("Error in specification: Compound length cannot be computed.");
break;
}
return 0;
}
bool UapDataItem::fieldPresent(const uchar *p, const UapField &uapField) const
{
switch (m_format) {
case FIXED:
return true;
break;
case VARIABLE:
return uapField.octed() <= calcLength(p);
break;
case EXPLICIT:
return true;
break;
case REPETIVE:
return true;
break;
case COMPOUND:
logError("Error in specification: Field defintion not allowed below compound.");
return false;
break;
}
return false;
}
UapCategory::UapCategory(QDomElement catEl)
{
m_category = Uap::mandAtt(catEl, "cat").toInt();
m_version = catEl.attribute("version");
QDomNode node = catEl.firstChild();
while (not node.isNull())
{
if (node.toElement().tagName() == "dataitem")
{
UapDataItem* uapItem = new UapDataItem( node.toElement() );
m_dataItems.insert( uapItem->frn(), uapItem);
}
node = node.nextSiblingElement();
}
}
/** Return FRN of Time-Of-Day/Time-Of-Track field.
** or -1 if unknown or not present.
**/
int UapCategory::timeOfDayFrn() const
{
switch (m_category)
{
case 1: return 9;
case 2: return 4;
case 4: return 4;
case 7: return 4;
case 8: return 8;
case 10: return 4;
case 11: return 4;
case 19: return 3;
case 20: return 3;
case 21: return 5;
case 23: return 4;
case 25: return 6;
case 34: return 3;
case 48: return 2;
case 62: return 4;
case 63: return 3;
case 65: return 4;
}
return -1;
}
void Uap::readXml(QFile* input)
{
QDomDocument doc;
doc.setContent(input);
QDomNodeList catElms = doc.elementsByTagName("category");
for (int i = 0; i < catElms.count(); i++)
{
UapCategory* uapCategory = new UapCategory(catElms.at(i).toElement());
uapCategory->m_sourceFile = input->fileName();
m_categories.insert(uapCategory->category(), uapCategory);
logInfo("Read cat " + QString::number(uapCategory->m_category) + " version " + uapCategory->m_version + " from file '" + uapCategory->m_sourceFile + "'");
}
}
void Uap::readXml(QString dirpath)
{
QDir dir(dirpath);
logInfo("Reading *.xml files from directory " + dir.absolutePath());
QFileInfoList fileInfos = dir.entryInfoList(QStringList() << "*.xml", QDir::Files | QDir::Readable);
if (fileInfos.isEmpty())
{
logWarn("No *.xml files found in " + dir.absolutePath());
}
foreach (QFileInfo fileInfo, fileInfos)
{
QFile file(fileInfo.filePath());
if (not file.open(QIODevice::ReadOnly))
{
logWarn("Unable to open file " + fileInfo.filePath());
continue;
}
readXml(&file);
}
}
const UapCategory *Uap::selectedUapCategory(int cat) const
{
if (m_selectedCategory.contains(cat))
{
return m_selectedCategory.value(cat);
}
else if (m_categories.contains(cat))
{
return m_categories.value(cat); // return first found
}
else
{
return nullptr;
}
}
/** Select UapCategory to be used for certain Asterix category.
** Asterix category number is extracted from uapCategory.
**/
void Uap::selectUapCategory(const UapCategory *uapCategory)
{
Q_ASSERT(uapCategory);
m_selectedCategory.insert(uapCategory->category(), uapCategory);
}
int Uap::parseNumber(const QString &number)
{
if (number.left(2) == "0x")
return number.mid( 1 ).toInt(0, 16);
else if (number.right(1) == "b")
return number.left( number.size()-1 ).toInt( 0, 2 );
else
return number.toInt();
}
QString Uap::mandAtt(QDomElement elm, const QString &att)
{
if (elm.attribute(att).isEmpty())
qFatal(qPrintable("Mandadory attribute '" + att + "'' is missing or empty on '" + elm.tagName() + "'."));
return elm.attribute(att);
}