Skip to content

Commit 2f7c259

Browse files
committed
Better support for <custom> layer replacing <id> in commsdsl2wireshark.
1 parent b3b7ca6 commit 2f7c259

4 files changed

Lines changed: 62 additions & 33 deletions

File tree

app/commsdsl2wireshark/src/WiresharkCustomLayer.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
#include "WiresharkCustomLayer.h"
1717

1818
#include "WiresharkGenerator.h"
19+
#include "WiresharkIdLayer.h"
1920

2021
#include "commsdsl/gen/strings.h"
2122

23+
#include <cassert>
24+
2225
namespace strings = commsdsl::gen::strings;
2326

2427
namespace commsdsl2wireshark
@@ -49,6 +52,22 @@ bool WiresharkCustomLayer::genPrepareImpl()
4952
return true;
5053
}
5154

55+
std::string WiresharkCustomLayer::wiresharkExtraDissectCodeImpl() const
56+
{
57+
auto parseObj = genCustomLayerParseObj();
58+
if (parseObj.parseSemanticLayerType() != ParseLayer::ParseKind::Id) {
59+
return strings::genEmptyString();
60+
}
61+
62+
auto* parentFrame = genParentFrame();
63+
assert(parentFrame != nullptr);
64+
auto parentNs = parentFrame->genParentNamespace();
65+
assert(parentNs != nullptr);
66+
auto messages = parentNs->genGetAllMessagesIdSorted();
67+
auto mapName = WiresharkGenerator::wiresharkCast(genGenerator()).wiresharkFuncNameFor(*this, "_msg");
68+
return WiresharkIdLayer::wiresharkMessagesMapCode(messages, mapName);
69+
}
70+
5271
bool WiresharkCustomLayer::wiresharkNeedsCrcCalcImpl() const
5372
{
5473
auto parseObj = genCustomLayerParseObj();

app/commsdsl2wireshark/src/WiresharkCustomLayer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class WiresharkCustomLayer final : public commsdsl::gen::GenCustomLayer, public
3838

3939
protected:
4040
virtual bool genPrepareImpl() override;
41+
virtual std::string wiresharkExtraDissectCodeImpl() const override;
4142
virtual bool wiresharkNeedsCrcCalcImpl() const override;
4243
};
4344

app/commsdsl2wireshark/src/WiresharkIdLayer.cpp

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,39 +39,8 @@ WiresharkIdLayer::WiresharkIdLayer(WiresharkGenerator& generator, ParseLayer par
3939
{
4040
}
4141

42-
std::string WiresharkIdLayer::wiresharkDissectBodyImpl() const
43-
{
44-
static const std::string Templ =
45-
"#^#FIELD#$#\n"
46-
"local id = #^#VALUE_FUNC#$#()\n"
47-
"local msg = #^#MAP#$#[id]\n"
48-
"#^#OFFSET#$# = #^#NEXT_OFFSET#$#\n"
49-
"#^#NEXT#$#\n"
50-
;
51-
52-
auto* field = wiresharkField();
53-
assert(field != nullptr);
54-
55-
util::GenReplacementMap repl = {
56-
{"FIELD", wiresharkDissectFieldCode()},
57-
{"NEXT", wiresharkNextFuncCode()},
58-
{"MAP", wiresharkMsgMapNameInternal()},
59-
{"VALUE_FUNC", field->wiresharkValueFuncName()},
60-
{"OFFSET", WiresharkField::wiresharkOffsetStr()},
61-
{"NEXT_OFFSET", WiresharkField::wiresharkNextOffsetStr()},
62-
};
63-
64-
return util::genProcessTemplate(Templ, repl);
65-
}
66-
67-
std::string WiresharkIdLayer::wiresharkExtraDissectCodeImpl() const
42+
std::string WiresharkIdLayer::wiresharkMessagesMapCode(const GenMessagesAccessList& messages, const std::string& mapName)
6843
{
69-
auto* parentFrame = genParentFrame();
70-
assert(parentFrame != nullptr);
71-
auto parentNs = parentFrame->genParentNamespace();
72-
assert(parentNs != nullptr);
73-
auto messages = parentNs->genGetAllMessagesIdSorted();
74-
7544
using DissectMap = std::map<std::uintmax_t /*id */, util::GenStringsList /*dissect_funcs*/>;
7645

7746
DissectMap map;
@@ -106,13 +75,49 @@ std::string WiresharkIdLayer::wiresharkExtraDissectCodeImpl() const
10675
;
10776

10877
util::GenReplacementMap repl = {
109-
{"NAME", wiresharkMsgMapNameInternal()},
78+
{"NAME", mapName},
11079
{"ELEMS", util::genStrListToString(elems, ",\n", "")},
11180
};
11281

11382
return util::genProcessTemplate(Templ, repl);
11483
}
11584

85+
std::string WiresharkIdLayer::wiresharkDissectBodyImpl() const
86+
{
87+
static const std::string Templ =
88+
"#^#FIELD#$#\n"
89+
"local id = #^#VALUE_FUNC#$#()\n"
90+
"local msg = #^#MAP#$#[id]\n"
91+
"#^#OFFSET#$# = #^#NEXT_OFFSET#$#\n"
92+
"#^#NEXT#$#\n"
93+
;
94+
95+
auto* field = wiresharkField();
96+
assert(field != nullptr);
97+
98+
util::GenReplacementMap repl = {
99+
{"FIELD", wiresharkDissectFieldCode()},
100+
{"NEXT", wiresharkNextFuncCode()},
101+
{"MAP", wiresharkMsgMapNameInternal()},
102+
{"VALUE_FUNC", field->wiresharkValueFuncName()},
103+
{"OFFSET", WiresharkField::wiresharkOffsetStr()},
104+
{"NEXT_OFFSET", WiresharkField::wiresharkNextOffsetStr()},
105+
};
106+
107+
return util::genProcessTemplate(Templ, repl);
108+
}
109+
110+
std::string WiresharkIdLayer::wiresharkExtraDissectCodeImpl() const
111+
{
112+
auto* parentFrame = genParentFrame();
113+
assert(parentFrame != nullptr);
114+
auto parentNs = parentFrame->genParentNamespace();
115+
assert(parentNs != nullptr);
116+
auto messages = parentNs->genGetAllMessagesIdSorted();
117+
118+
return wiresharkMessagesMapCode(messages, wiresharkMsgMapNameInternal());
119+
}
120+
116121
std::string WiresharkIdLayer::wiresharkMsgMapNameInternal() const
117122
{
118123
return WiresharkGenerator::wiresharkCast(genGenerator()).wiresharkFuncNameFor(*this, "_msg");

app/commsdsl2wireshark/src/WiresharkIdLayer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "WiresharkLayer.h"
1919

2020
#include "commsdsl/gen/GenIdLayer.h"
21+
#include "commsdsl/gen/GenMessage.h"
2122

2223
#include <string>
2324

@@ -33,8 +34,11 @@ class WiresharkIdLayer final : public commsdsl::gen::GenIdLayer, public Wireshar
3334
public:
3435
using ParseLayer = commsdsl::parse::ParseLayer;
3536
using GenElem = commsdsl::gen::GenElem;
37+
using GenMessage = commsdsl::gen::GenMessage;
38+
using GenMessagesAccessList = std::vector<const GenMessage*>;
3639

3740
WiresharkIdLayer(WiresharkGenerator& generator, ParseLayer parseObj, GenElem* parent);
41+
static std::string wiresharkMessagesMapCode(const GenMessagesAccessList& messages, const std::string& mapName);
3842

3943
protected:
4044
virtual std::string wiresharkDissectBodyImpl() const override;

0 commit comments

Comments
 (0)