2222#include " commsdsl/gen/util.h"
2323#include " commsdsl/gen/strings.h"
2424
25+ #include < algorithm>
2526#include < cassert>
2627#include < fstream>
2728#include < type_traits>
@@ -68,12 +69,24 @@ std::string Wireshark::wiresharkExtractorsMapName(const WiresharkGenerator& gene
6869 return wiresharkProtocolObjName (generator) + " _extractors_map" ;
6970}
7071
71- std::string Wireshark::wiresharkStatusCodeStr (const WiresharkGenerator& generator, StatusCode code)
72+ std::string Wireshark::wiresharkStatusCodeStr (const WiresharkGenerator& generator, WiresharkStatusCode code)
7273{
7374 Wireshark obj (generator);
7475 return obj.wiresharkStatusCodeNameInternal () + " ." + wiresharkStatusCodeStrInternal (code);
7576}
7677
78+ std::string Wireshark::wiresharkOptModeStr (const WiresharkGenerator& generator, WiresharkOptMode code)
79+ {
80+ Wireshark obj (generator);
81+ return obj.wiresharkOptModeNameInternal () + " ." + wiresharkOptModeStrInternal (code);
82+ }
83+
84+ std::string Wireshark::wiresharkOptModeValsName (const WiresharkGenerator& generator)
85+ {
86+ Wireshark obj (generator);
87+ return obj.wiresharkOptModeNameInternal () + strings::genValsSuffixStr ();
88+ }
89+
7790std::string Wireshark::wiresharkFieldValueFuncName (const WiresharkGenerator& generator)
7891{
7992 return wiresharkProtocolObjName (generator) + " _field_value" ;
@@ -96,6 +109,7 @@ bool Wireshark::wiresharkWriteInternal() const
96109 " #^#GEN_COMMENT#$#\n "
97110 " #^#PROTOCOL#$#\n "
98111 " #^#STATUS_CODE#$#\n "
112+ " #^#OPT_MODE#$#\n "
99113 " #^#FIELDS_REG#$#\n "
100114 " #^#EXTRACTORS_DECL#$#\n "
101115 " #^#FIELD_VALUE_FUNC#$#\n "
@@ -116,6 +130,7 @@ bool Wireshark::wiresharkWriteInternal() const
116130 {" FIELDS_LIST" , wiresharkFieldsListName (m_wiresharkGenerator)},
117131 {" CODE" , wiresharkCodeInternal ()},
118132 {" STATUS_CODE" , wiresharkStatusCodeDefInternal ()},
133+ {" OPT_MODE" , wiresharkOptionalModeDefInternal ()},
119134 {" EXTRACTORS_DECL" , wiresharkExtractorsDeclInternal ()},
120135 {" EXTRACTORS_REG" , wiresharkExtractorsRegCodeInternal ()},
121136 {" FIELD_VALUE_FUNC" , wiresharkFieldValueFuncInternal ()},
@@ -227,7 +242,7 @@ std::string Wireshark::wiresharkDissectFuncBodyInternal() const
227242
228243 util::GenReplacementMap frameRepl = {
229244 {" NAME" , wiresharkFrame.wiresharkDissectName ()},
230- {" SUCCESS" , wiresharkStatusCodeStr (m_wiresharkGenerator, StatusCode ::Success)},
245+ {" SUCCESS" , wiresharkStatusCodeStr (m_wiresharkGenerator, WiresharkStatusCode ::Success)},
231246 };
232247
233248 elems.push_back (util::genProcessTemplate (FrameTempl, frameRepl));
@@ -260,8 +275,8 @@ std::string Wireshark::wiresharkDissectFuncBodyInternal() const
260275 util:: GenReplacementMap repl = {
261276 {" NAME" , wiresharkProtocolObjName (m_wiresharkGenerator)},
262277 {" FRAMES" , util::genStrListToString (elems, " \n " , " " )},
263- {" SUCCESS" , wiresharkStatusCodeStr (m_wiresharkGenerator, StatusCode ::Success)},
264- {" NOT_ENOUGH_DATA" , wiresharkStatusCodeStr (m_wiresharkGenerator, StatusCode ::NotEnoughData)},
278+ {" SUCCESS" , wiresharkStatusCodeStr (m_wiresharkGenerator, WiresharkStatusCode ::Success)},
279+ {" NOT_ENOUGH_DATA" , wiresharkStatusCodeStr (m_wiresharkGenerator, WiresharkStatusCode ::NotEnoughData)},
265280 };
266281
267282 return util::genProcessTemplate (Templ, repl);
@@ -276,8 +291,8 @@ std::string Wireshark::wiresharkStatusCodeDefInternal() const
276291{
277292 util::GenStringsList vals;
278293
279- for (auto idx = 0U ; idx < static_cast <unsigned >(StatusCode ::ValuesLimit); ++idx) {
280- vals.push_back (wiresharkStatusCodeStrInternal (static_cast <StatusCode >(idx)) + " = " + std::to_string (idx));
294+ for (auto idx = 0U ; idx < static_cast <unsigned >(WiresharkStatusCode ::ValuesLimit); ++idx) {
295+ vals.push_back (wiresharkStatusCodeStrInternal (static_cast <WiresharkStatusCode >(idx)) + " = " + std::to_string (idx));
281296 }
282297
283298 const std::string Templ =
@@ -294,6 +309,56 @@ std::string Wireshark::wiresharkStatusCodeDefInternal() const
294309 return util::genProcessTemplate (Templ, repl);
295310}
296311
312+ std::string Wireshark::wiresharkOptModeNameInternal () const
313+ {
314+ return wiresharkProtocolObjName (m_wiresharkGenerator) + " _OptMode" ;
315+ }
316+
317+ std::string Wireshark::wiresharkOptionalModeDefInternal () const
318+ {
319+ auto & schemas = m_wiresharkGenerator.genSchemas ();
320+ bool requiresOptMode =
321+ std::any_of (
322+ schemas.begin (), schemas.end (),
323+ [](auto & sPtr )
324+ {
325+ assert (sPtr );
326+ return WiresharkSchema::wiresharkCast (*sPtr ).wiresharkNeedsOptionalModeDefinition ();
327+ });
328+
329+ if (!requiresOptMode) {
330+ return strings::genEmptyString ();
331+ }
332+
333+ util::GenStringsList vals;
334+ util::GenStringsList valNames;
335+
336+ for (auto idx = 0U ; idx < static_cast <unsigned >(WiresharkOptMode::ValuesLimit); ++idx) {
337+ auto & optModeStr = wiresharkOptModeStrInternal (static_cast <WiresharkOptMode>(idx));
338+ vals.push_back (optModeStr + " = " + std::to_string (idx));
339+ valNames.push_back (" [" + std::to_string (idx) + " ] = \" " + optModeStr + " \" " );
340+ }
341+
342+ const std::string Templ =
343+ " local #^#NAME#$# = {\n "
344+ " #^#VALS#$#\n "
345+ " }\n "
346+ " \n "
347+ " local #^#VALS_NAME#$# = {\n "
348+ " #^#VAL_NAMES#$#\n "
349+ " }\n "
350+ ;
351+
352+ util::GenReplacementMap repl = {
353+ {" NAME" , wiresharkOptModeNameInternal ()},
354+ {" VALS" , util::genStrListToString (vals, " ,\n " , " " )},
355+ {" VALS_NAME" , wiresharkOptModeValsName (m_wiresharkGenerator)},
356+ {" VAL_NAMES" , util::genStrListToString (valNames, " ,\n " , " " )},
357+ };
358+
359+ return util::genProcessTemplate (Templ, repl);
360+ }
361+
297362std::string Wireshark::wiresharkExtractorsDeclInternal () const
298363{
299364 const std::string Templ =
@@ -348,7 +413,7 @@ std::string Wireshark::wiresharkFieldValueFuncInternal() const
348413 return util::genProcessTemplate (Templ, repl);
349414}
350415
351- const std::string& Wireshark::wiresharkStatusCodeStrInternal (StatusCode code)
416+ const std::string& Wireshark::wiresharkStatusCodeStrInternal (WiresharkStatusCode code)
352417{
353418 static const std::string Map[] = {
354419 /* Success */ " SUCCESS" ,
@@ -359,7 +424,22 @@ const std::string& Wireshark::wiresharkStatusCodeStrInternal(StatusCode code)
359424 /* CodegenError */ " CODEGEN_ERROR" ,
360425 };
361426 static const std::size_t MapSize = std::extent_v<decltype (Map)>;
362- static_assert (MapSize == static_cast <unsigned >(StatusCode::ValuesLimit));
427+ static_assert (MapSize == static_cast <unsigned >(WiresharkStatusCode::ValuesLimit));
428+
429+ auto idx = static_cast <unsigned >(code);
430+ assert (idx < MapSize);
431+ return Map[idx];
432+ }
433+
434+ const std::string& Wireshark::wiresharkOptModeStrInternal (WiresharkOptMode code)
435+ {
436+ static const std::string Map[] = {
437+ /* Tentative */ " TENTATIVE" ,
438+ /* Exists */ " EXISTS" ,
439+ /* Missing */ " MISSING" ,
440+ };
441+ static const std::size_t MapSize = std::extent_v<decltype (Map)>;
442+ static_assert (MapSize == static_cast <unsigned >(WiresharkOptMode::ValuesLimit));
363443
364444 auto idx = static_cast <unsigned >(code);
365445 assert (idx < MapSize);
0 commit comments