Skip to content

Commit 7cef99c

Browse files
committed
Registration functionality for <float> fields in commsdsl2wireshark.
1 parent caf3827 commit 7cef99c

7 files changed

Lines changed: 180 additions & 9 deletions

File tree

app/commsdsl2wireshark/src/WiresharkBitfieldField.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "commsdsl/gen/strings.h"
2424
#include "commsdsl/parse/ParseIntField.h"
2525

26+
#include <algorithm>
2627
#include <cassert>
2728
#include <iomanip>
2829
#include <sstream>
@@ -117,6 +118,17 @@ bool WiresharkBitfieldField::genPrepareImpl()
117118
}
118119

119120
m_wiresharkFields = wiresharkTransformFieldsList(genMembers());
121+
122+
auto& generator = genGenerator();
123+
m_wiresharkFields.erase(
124+
std::remove_if(
125+
m_wiresharkFields.begin(), m_wiresharkFields.end(),
126+
[&generator](auto* fPtr)
127+
{
128+
auto parseObj = fPtr->wiresharkGenField().genParseObj();
129+
return !generator.genDoesElementExist(parseObj.parseSinceVersion(), parseObj.parseDeprecatedSince(), parseObj.parseIsDeprecatedRemoved());
130+
}),
131+
m_wiresharkFields.end());
120132
return true;
121133
}
122134

app/commsdsl2wireshark/src/WiresharkBundleField.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "commsdsl/gen/util.h"
2222
#include "commsdsl/gen/strings.h"
2323

24+
#include <algorithm>
25+
2426
namespace strings = commsdsl::gen::strings;
2527
namespace util = commsdsl::gen::util;
2628

@@ -41,6 +43,16 @@ bool WiresharkBundleField::genPrepareImpl()
4143
}
4244

4345
m_wiresharkFields = wiresharkTransformFieldsList(genMembers());
46+
auto& generator = genGenerator();
47+
m_wiresharkFields.erase(
48+
std::remove_if(
49+
m_wiresharkFields.begin(), m_wiresharkFields.end(),
50+
[&generator](auto* fPtr)
51+
{
52+
auto parseObj = fPtr->wiresharkGenField().genParseObj();
53+
return !generator.genDoesElementExist(parseObj.parseSinceVersion(), parseObj.parseDeprecatedSince(), parseObj.parseIsDeprecatedRemoved());
54+
}),
55+
m_wiresharkFields.end());
4456
return true;
4557
}
4658

app/commsdsl2wireshark/src/WiresharkEnumField.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ std::string WiresharkEnumField::wiresharkValsInternal(const WiresharkField* refF
8888
continue;
8989
}
9090

91+
if (!genGenerator().genDoesElementExist(iter->second.m_sinceVersion, iter->second.m_deprecatedSince, true)) {
92+
continue;
93+
}
94+
9195
static const std::string Templ =
9296
"[#^#VAL#$#] = \"#^#NAME#$#\"";
9397

app/commsdsl2wireshark/src/WiresharkFloatField.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,20 @@
1515

1616
#include "WiresharkFloatField.h"
1717

18+
#include "Wireshark.h"
1819
#include "WiresharkGenerator.h"
1920

21+
#include "commsdsl/gen/strings.h"
22+
#include "commsdsl/gen/util.h"
23+
24+
#include <cassert>
25+
#include <cstddef>
26+
#include <cmath>
27+
#include <type_traits>
28+
29+
namespace strings = commsdsl::gen::strings;
30+
namespace util = commsdsl::gen::util;
31+
2032
namespace commsdsl2wireshark
2133
{
2234

@@ -35,4 +47,113 @@ bool WiresharkFloatField::genPrepareImpl()
3547
return true;
3648
}
3749

50+
std::string WiresharkFloatField::wiresharkFieldRegistrationImpl(const WiresharkField* refField) const
51+
{
52+
static const std::string Templ =
53+
"#^#SPECIALS#$#\n"
54+
"local #^#OBJ_NAME#$# = #^#CREATE_FUNC#$#(ProtoField.#^#TYPE#$#(\"#^#REF_NAME#$#\", #^#DISP_NAME#$#, #^#SPECIALS_NAME#$#, #^#DESC#$#))\n"
55+
;
56+
57+
util::GenReplacementMap repl = {
58+
{"SPECIALS", wiresharkSpecialsInternal(refField)},
59+
{"OBJ_NAME", wiresharkFieldObjName(refField)},
60+
{"CREATE_FUNC", Wireshark::wiresharkCreateFieldFuncName(WiresharkGenerator::wiresharkCast(genGenerator()))},
61+
{"TYPE", wiresharkFloatTypeInternal()},
62+
{"REF_NAME", wiresharkFieldRefName(refField)},
63+
{"DISP_NAME", wiresharkFieldNameVarNameStr(refField)},
64+
{"SPECIALS_NAME", strings::genNilStr()},
65+
{"DESC", wiresharkFieldDescriptionStr(refField)},
66+
};
67+
68+
if (!repl["SPECIALS"].empty()) {
69+
repl["SPECIALS_NAME"] = repl["OBJ_NAME"] + strings::genValsSuffixStr();
70+
}
71+
72+
assert(!repl["TYPE"].empty());
73+
return util::genProcessTemplate(Templ, repl);
74+
}
75+
76+
std::string WiresharkFloatField::wiresharkSpecialsInternal(const WiresharkField* refField) const
77+
{
78+
auto& specials = genSpecialsSortedByValue();
79+
if (specials.empty()) {
80+
return strings::genEmptyString();
81+
}
82+
83+
util::GenStringsList elems;
84+
for (auto& s : specials) {
85+
if (std::isnan(s.second.m_value)) {
86+
// Known limitation: the NaN comparing to NaN will return false, so it cannot really be found in map
87+
continue;
88+
}
89+
90+
if (!genGenerator().genDoesElementExist(s.second.m_sinceVersion, s.second.m_deprecatedSince, true)) {
91+
continue;
92+
}
93+
94+
static const std::string Templ =
95+
"[#^#VAL#$#] = \"#^#NAME#$#\"";
96+
97+
util::GenReplacementMap repl = {
98+
{"NAME", util::genDisplayName(s.second.m_displayName, s.first)},
99+
{"VAL", wiresharkSpecialValStrInternal(s.second.m_value)},
100+
};
101+
102+
elems.push_back(util::genProcessTemplate(Templ, repl));
103+
}
104+
105+
if (elems.empty()) {
106+
return strings::genEmptyString();
107+
}
108+
109+
static const std::string Templ =
110+
"local #^#NAME#$##^#SUFFIX#$# = {\n"
111+
" #^#ELEMS#$#\n"
112+
"}\n"
113+
;
114+
115+
util::GenReplacementMap repl = {
116+
{"NAME", wiresharkFieldObjName(refField)},
117+
{"SUFFIX", strings::genValsSuffixStr()},
118+
{"ELEMS", util::genStrListToString(elems, ",\n", "")},
119+
};
120+
121+
return util::genProcessTemplate(Templ, repl);
122+
}
123+
124+
std::string WiresharkFloatField::wiresharkSpecialValStrInternal(double val) const
125+
{
126+
assert(!std::isnan(val)); // Should be filtered out earlier
127+
128+
if (!std::isinf(val)) {
129+
return std::to_string(val);
130+
}
131+
132+
if (val < 0) {
133+
return "1/0";
134+
}
135+
136+
return "-1/0";
137+
}
138+
139+
const std::string& WiresharkFloatField::wiresharkFloatTypeInternal() const
140+
{
141+
static const std::string Map[] = {
142+
/* Float */ "float",
143+
/* Double */ "double",
144+
};
145+
static const std::size_t MapSize = std::extent_v<decltype(Map)>;
146+
static_assert(MapSize == static_cast<unsigned>(ParseFloatField::ParseType::NumOfValues));
147+
148+
auto parseObj = genFloatFieldParseObj();
149+
auto idx = static_cast<unsigned>(parseObj.parseType());
150+
if (MapSize <= idx) {
151+
[[maybe_unused]] static constexpr bool Should_not_happen = false;
152+
assert(Should_not_happen);
153+
idx = static_cast<decltype(idx)>(ParseFloatField::ParseType::Double);
154+
}
155+
156+
return Map[idx];
157+
}
158+
38159
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkFloatField.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include "commsdsl/gen/GenFloatField.h"
2121

22+
#include <string>
23+
2224
namespace commsdsl2wireshark
2325
{
2426

@@ -30,12 +32,20 @@ class WiresharkFloatField final : public commsdsl::gen::GenFloatField, public Wi
3032

3133
public:
3234
using ParseField = commsdsl::parse::ParseField;
35+
using ParseFloatField = commsdsl::parse::ParseFloatField;
3336
using GenElem = commsdsl::gen::GenElem;
3437

3538
WiresharkFloatField(WiresharkGenerator& generator, ParseField parseObj, GenElem* parent);
3639

3740
protected:
3841
virtual bool genPrepareImpl() override;
42+
43+
virtual std::string wiresharkFieldRegistrationImpl(const WiresharkField* refField) const override;
44+
45+
private:
46+
std::string wiresharkSpecialsInternal(const WiresharkField* refField) const;
47+
std::string wiresharkSpecialValStrInternal(double val) const;
48+
const std::string& wiresharkFloatTypeInternal() const;
3949
};
4050

4151
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkIntField.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ const std::string& WiresharkIntField::wiresharkIntegralType(ParseIntField::Parse
9898
return iter->second;
9999
}
100100

101+
bool WiresharkIntField::genPrepareImpl()
102+
{
103+
if ((!GenBase::genPrepareImpl()) ||
104+
(!WiresharkBase::wiresharkPrepare())) {
105+
return false;
106+
}
107+
return true;
108+
}
109+
101110
std::string WiresharkIntField::wiresharkFieldRegistrationImpl(const WiresharkField* refField) const
102111
{
103112
static const std::string Templ =
@@ -142,6 +151,10 @@ std::string WiresharkIntField::wiresharkSpecialsInternal(const WiresharkField* r
142151

143152
util::GenStringsList elems;
144153
for (auto& s : specials) {
154+
if (!genGenerator().genDoesElementExist(s.second.m_sinceVersion, s.second.m_deprecatedSince, true)) {
155+
continue;
156+
}
157+
145158
static const std::string Templ =
146159
"[#^#VAL#$#] = \"#^#NAME#$#\"";
147160

@@ -157,6 +170,10 @@ std::string WiresharkIntField::wiresharkSpecialsInternal(const WiresharkField* r
157170
elems.push_back(util::genProcessTemplate(Templ, repl));
158171
}
159172

173+
if (elems.empty()) {
174+
return strings::genEmptyString();
175+
}
176+
160177
static const std::string Templ =
161178
"local #^#NAME#$##^#SUFFIX#$# = {\n"
162179
" #^#ELEMS#$#\n"
@@ -172,13 +189,4 @@ std::string WiresharkIntField::wiresharkSpecialsInternal(const WiresharkField* r
172189
return util::genProcessTemplate(Templ, repl);
173190
}
174191

175-
bool WiresharkIntField::genPrepareImpl()
176-
{
177-
if ((!GenBase::genPrepareImpl()) ||
178-
(!WiresharkBase::wiresharkPrepare())) {
179-
return false;
180-
}
181-
return true;
182-
}
183-
184192
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkSetField.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ std::string WiresharkSetField::wiresharkBitsInternal(const WiresharkField* refFi
8787

8888
auto& bitInfo = *iter;
8989

90+
if (!genGenerator().genDoesElementExist(bitInfo.second.m_sinceVersion, bitInfo.second.m_deprecatedSince, true)) {
91+
continue;
92+
}
93+
9094
static const std::string Templ =
9195
"local #^#BIT_OBJ_NAME#$# = #^#CREATE_FUNC#$#(ProtoField.bool(\"#^#REF_NAME#$#\", \"#^#DISP_NAME#$#\", #^#PARENT_WIDTH#$#, #^#NIL#$#, #^#MASK#$#, #^#DESC#$#))\n"
9296
;

0 commit comments

Comments
 (0)