forked from billvaglienti/ProtoGen
-
Notifications
You must be signed in to change notification settings - Fork 5
/
protocolpacket.cpp
2079 lines (1696 loc) · 76.3 KB
/
protocolpacket.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "protocolpacket.h"
#include "protocolfield.h"
#include "enumcreator.h"
#include "protocolfield.h"
#include "protocolstructure.h"
#include "protocolparser.h"
#include "protocoldocumentation.h"
#include "shuntingyard.h"
#include <iostream>
/*!
* Construct the object that parses packet descriptions
* \param parse points to the global protocol parser that owns everything
* \param supported gives the supported features of the protocol
* \param protocolApi is the API string of the protocol
* \param protocolVersion is the version string of the protocol
* \param bigendian should be true to encode multi-byte fields with the most
* significant byte first.
*/
ProtocolPacket::ProtocolPacket(ProtocolParser* parse, ProtocolSupport supported, const std::string& protocolApi, const std::string& protocolVersion) :
ProtocolStructureModule(parse, supported, protocolApi, protocolVersion),
useInOtherPackets(false),
parameterFunctions(false),
structureFunctions(true)
{
// These are attributes on top of the normal structureModule that we support
std::vector<std::string> newattribs({"structureInterface", "parameterInterface", "ID", "useInOtherPackets"});
// Now append the new attributes onto our old list
// Now append the new attributes onto our old list
attriblist.insert(attriblist.end(), newattribs.begin(), newattribs.end());
}
ProtocolPacket::~ProtocolPacket(void)
{
clear();
}
/*!
* Clear out any data, resetting for next packet parse operation
*/
void ProtocolPacket::clear(void)
{
ProtocolStructureModule::clear();
ids.clear();
useInOtherPackets = false;
parameterFunctions = false;
structureFunctions = true;
// Delete all the objects in the list
for(std::size_t i = 0; i < documentList.size(); i++)
{
if(documentList[i] != nullptr)
{
delete documentList[i];
documentList[i] = nullptr;
}
}
// clear the list
documentList.clear();
// Note that data set during constructor are not changed
}
/*!
* Create the source and header files that represent a packet
*/
void ProtocolPacket::parse(void)
{
// Initialize metadata
clear();
if(e == nullptr)
return;
// Get any documentation for this packet
ProtocolDocumentation::getChildDocuments(parser, getHierarchicalName(), support, e, documentList);
// Me and all my children, which may themselves be structures, notice we
// are not parsing ProtocolStructureModule. This class is basically a
// re-implementation of ProtocolStructureModule with different rules.
ProtocolStructure::parse();
const XMLAttribute* map = e->FirstAttribute();
std::string moduleName = ProtocolParser::getAttribute("file", map);
std::string defheadermodulename = ProtocolParser::getAttribute("deffile", map);
std::string verifymodulename = ProtocolParser::getAttribute("verifyfile", map);
std::string comparemodulename = ProtocolParser::getAttribute("comparefile", map);
std::string printmodulename = ProtocolParser::getAttribute("printfile", map);
std::string mapmodulename = ProtocolParser::getAttribute("mapfile", map);
encode = !ProtocolParser::isFieldClear(ProtocolParser::getAttribute("encode", map));
decode = !ProtocolParser::isFieldClear(ProtocolParser::getAttribute("decode", map));
// It is possible to suppress the globally specified compare output
if(ProtocolParser::isFieldClear(ProtocolParser::getAttribute("compare", map)))
{
support.compare = compare = false;
comparemodulename.clear();
support.globalCompareName.clear();
}
else if(ProtocolParser::isFieldSet(ProtocolParser::getAttribute("compare", map)))
compare = true;
// It is possible to suppress the globally specified print output
if(ProtocolParser::isFieldClear(ProtocolParser::getAttribute("print", map)))
{
support.print = print = false;
printmodulename.clear();
support.globalPrintName.clear();
}
else if(ProtocolParser::isFieldSet(ProtocolParser::getAttribute("print", map)))
print = true;
// It is possible to suppress the globally specified map output
if(ProtocolParser::isFieldClear(ProtocolParser::getAttribute("map", map)))
{
support.mapEncode = mapEncode = false;
mapmodulename.clear();
support.globalMapName.clear();
}
else if(ProtocolParser::isFieldSet(ProtocolParser::getAttribute("map", map)))
mapEncode = true;
useInOtherPackets = ProtocolParser::isFieldSet("useInOtherPackets", map);
std::string redefinename = ProtocolParser::getAttribute("redefine", map);
// Typically "parameterInterface" and "structureInterface" are only ever set to "true".
// However we do handle the case where someone uses "false"
parameterFunctions = ProtocolParser::isFieldSet("parameterInterface", map);
structureFunctions = ProtocolParser::isFieldSet("structureInterface", map);
if(ProtocolParser::isFieldClear(ProtocolParser::getAttribute("parameterInterface", map)))
{
parameterFunctions = false;
structureFunctions = true;
}
if(ProtocolParser::isFieldClear(ProtocolParser::getAttribute("structureInterface", map)))
{
parameterFunctions = true;
structureFunctions = false;
}
// Its possible to have multiple ID attributes which are separated by white space or commas, etc.
ids = splitanyof(ProtocolParser::getAttribute("ID", map), " ,;:\t\n\r");
// In case the user didn't provide a comment, see if we use the comment for the ID
if(comment.empty() && (ids.size() > 0))
comment = parser->getEnumerationValueComment(ids.at(0));
// Warnings common to structures and packets
issueWarnings(map);
// Warning about maximum data size, only applies to packets
if(support.maxdatasize > 0)
{
// maxdatasize will be zero if the length string cannot be computed
int maxdatasize = (int)(ShuntingYard::computeInfix(parser->replaceEnumerationNameWithValue(encodedLength.maxEncodedLength)) + 0.5);
// Warn the user if the packet might be too big
if(maxdatasize > support.maxdatasize)
emitWarning("Maximum packet size of " + std::to_string(maxdatasize) + " bytes exceeds limit of " + std::to_string(support.maxdatasize) + " bytes");
}
// Warnings about C keywords
for(std::size_t i = 0; i < ids.size(); i++)
{
if(contains(keywords, ids.at(i), true))
{
emitWarning(ids.at(i) + " matches C keyword, changed to _" + ids.at(i));
ids[i] = "_" + ids.at(i);
}
}
if((structureFunctions == false) && (parameterFunctions == false))
{
// If the user gave us no guidance (or turned both off, which is the
// same as no guidance), make a choice based on the size of the
// encodable list. If we only have 1 parameter, there is no sense in
// wrapping it in a structure
if((getNumberOfEncodeParameters() > 1) && (getNumberOfDecodeParameters() > 1))
structureFunctions = true;
else
parameterFunctions = true;
}
if(!redefinename.empty())
{
if(redefinename == name)
emitWarning("Redefine must be different from name");
else
{
redefines = parser->lookUpStructure(support.prefix + redefinename + support.typeSuffix);
if(redefines == nullptr)
{
redefinename.clear();
emitWarning("Could not find structure to redefine");
}
}
if(redefines != nullptr)
structName = support.prefix + redefinename + support.typeSuffix;
}
// If no ID is supplied use the packet name in upper case,
// assuming that the user will define it elsewhere
if(ids.size() <= 0)
ids.push_back(toUpper(name));
// Don't output if hidden and we are omitting hidden items
if(isHidden() && !neverOmit && support.omitIfHidden)
{
std::cout << "Skipping code output for hidden packet " << getHierarchicalName() << std::endl;
return;
}
// Most of the file setup work. This will also declare the structure if
// warranted (note the details of the structure declaration will reflect
// back to this class via virtual functions).
setupFiles(moduleName, defheadermodulename, verifymodulename, comparemodulename, printmodulename, mapmodulename, structureFunctions, false);
// The functions that include structures which are children of this
// packet. These need to be declared before the main functions
createSubStructureFunctions();
// This is the constructor output, we want it to be the first function for this packet
createTopLevelInitializeFunction();
for(std::size_t i = 0; i < ids.size(); i++)
{
// The ID may be a value defined somewhere else
std::string include = parser->lookUpIncludeName(ids.at(i));
if(!include.empty())
header.writeIncludeDirective(include);
}
// The functions that encode and decode the packet from a structure.
if(structureFunctions)
createStructurePacketFunctions();
// The functions that encode and decode the packet from parameters
if(parameterFunctions)
createPacketFunctions();
// Now that the packet functions are out, do the non-packet functions
createTopLevelStructureFunctions();
// In the C language the utility functions are macros, defined just below the functions.
if(support.language == ProtocolSupport::c_language)
{
// White space is good
header.makeLineSeparator();
// Utility functions for ID, length, etc.
header.write(createUtilityFunctions(std::string()));
}
// White space is good
header.makeLineSeparator();
// Write to disk, note that duplicate flush() calls are OK
header.flush();
if(structHeader != nullptr)
structHeader->flush();
// We don't write the source to disk if we are not encoding or decoding anything
if(encode || decode)
source.flush();
else
source.clear();
// We don't write the verify files to disk if we are not initializing or verifying anything
if(hasInit() || hasVerify())
{
if(verifyHeader != nullptr)
verifyHeader->flush();
if(verifySource != nullptr)
verifySource->flush();
}
if(compare)
{
if(compareHeader != nullptr)
compareHeader->flush();
if(compareSource != nullptr)
compareSource->flush();
}
if(print)
{
if(printHeader != nullptr)
printHeader->flush();
if(printSource != nullptr)
printSource->flush();
}
if(mapEncode)
{
if(mapHeader != nullptr)
mapHeader->flush();
if(mapSource != nullptr)
mapSource->flush();
}
}// ProtocolPacket::parse
/*!
* Get the class declaration, for this structure only (not its children) for the C++ language
* \return The string that gives the class declaration
*/
std::string ProtocolPacket::getClassDeclaration_CPP(void) const
{
std::string output;
std::string structure;
// The top level comment for the class definition
if(!comment.empty())
{
output += "/*!\n";
output += ProtocolParser::outputLongComment(" * ", comment) + "\n";
output += " */\n";
}
// The opening to the class.
// In the context of C++ redefining means inheriting from a base class,
// and adding a new encode or decode function. All the other members and
// methods come from the base class
if(redefines == nullptr)
output += "class " + typeName + "\n";
else
output += "class " + typeName + " : public " + redefines->typeName + "\n";
output += "{\n";
// All members of the structure are public.
output += "public:\n";
// Function prototypes, in C++ these are part of the class definition.
// Notice that if we are not outputting structure functions, and this
// class won't be used by others, we will not have any data members and
// do not need a constructor.
if((redefines == nullptr) && (getNumberInMemory() > 0) && (useInOtherPackets || structureFunctions))
{
ProtocolFile::makeLineSeparator(output);
output += getSetToInitialValueFunctionPrototype(TAB_IN, false);
ProtocolFile::makeLineSeparator(output);
}
// Utility functions for ID, length, etc.
ProtocolFile::makeLineSeparator(output);
output += createUtilityFunctions(TAB_IN);
ProtocolFile::makeLineSeparator(output);
// The parameter functions encode parameters to/from packets
if(parameterFunctions)
{
if(encode)
{
ProtocolFile::makeLineSeparator(output);
output += getParameterPacketEncodePrototype(TAB_IN);
ProtocolFile::makeLineSeparator(output);
}
if(decode)
{
ProtocolFile::makeLineSeparator(output);
output += getParameterPacketDecodePrototype(TAB_IN);
ProtocolFile::makeLineSeparator(output);
}
}// if parameter packet functions
// The structure functions encode members of this class directly to/from packets
if(structureFunctions)
{
// In the event that there are no parameters, the parameter function
// is the same as the structure function - so don't output both
if(encode && ((getNumberOfEncodeParameters() > 0) || !parameterFunctions))
{
ProtocolFile::makeLineSeparator(output);
output += getStructurePacketEncodePrototype(TAB_IN);
ProtocolFile::makeLineSeparator(output);
}
// In the event that there are no parameters, the parameter function
// is the same as the structure function - so don't output both
if(decode && ((getNumberOfDecodeParameters() > 0) || !parameterFunctions))
{
ProtocolFile::makeLineSeparator(output);
output += getStructurePacketDecodePrototype(TAB_IN);
ProtocolFile::makeLineSeparator(output);
}
}// if structure packet functions
// Packet version of compare function
if(compare)
{
ProtocolFile::makeLineSeparator(output);
output += TAB_IN + "//! Compare two " + support.prefix + name + " packets and generate a report\n";
output += TAB_IN + "static std::string compare(std::string prename, const " + support.pointerType + " pkt1, const " + support.pointerType + " pkt2);\n";
ProtocolFile::makeLineSeparator(output);
}
// Packet version of print function
if(print)
{
ProtocolFile::makeLineSeparator(output);
output += TAB_IN + "//! Generate a string that describes the contents of a " + name + " packet\n";
output += TAB_IN + "static std::string textPrint(std::string prename, const " + support.pointerType + " pkt);\n";
ProtocolFile::makeLineSeparator(output);
}
// For use in other packets we need the ability to encode to a byte
// stream, which is what ProtocolStructure gives us
if(useInOtherPackets)
{
if(encode)
{
ProtocolFile::makeLineSeparator(output);
output += ProtocolStructure::getEncodeFunctionPrototype(TAB_IN, false);
ProtocolFile::makeLineSeparator(output);
}
if(decode)
{
ProtocolFile::makeLineSeparator(output);
output += ProtocolStructure::getDecodeFunctionPrototype(TAB_IN, false);
ProtocolFile::makeLineSeparator(output);
}
}// If structure functions that will be accessed by others
// There are cool utility functions: verify, print, read, mapencde,
// mapdecode, and compare. All of these have forms that come from
// ProtocolStructure. Thse functions are only output for the base class,
// inherited (redefined) classes do not output them, because they would be
// the same. We also do not output data members for redefined classes,
// they come from the base class
if((useInOtherPackets || structureFunctions) && (redefines == nullptr))
{
if(compare)
{
ProtocolFile::makeLineSeparator(output);
output += ProtocolStructure::getComparisonFunctionPrototype(TAB_IN, false);
ProtocolFile::makeLineSeparator(output);
}
if(print)
{
ProtocolFile::makeLineSeparator(output);
output += ProtocolStructure::getTextPrintFunctionPrototype(TAB_IN, false);
ProtocolFile::makeLineSeparator(output);
output += ProtocolStructure::getTextReadFunctionPrototype(TAB_IN, false);
ProtocolFile::makeLineSeparator(output);
}
if(hasVerify())
{
ProtocolFile::makeLineSeparator(output);
output += ProtocolStructure::getVerifyFunctionPrototype(TAB_IN, false);
ProtocolFile::makeLineSeparator(output);
}
if(mapEncode)
{
ProtocolFile::makeLineSeparator(output);
output += ProtocolStructure::getMapEncodeFunctionPrototype(TAB_IN, false);
ProtocolFile::makeLineSeparator(output);
output += ProtocolStructure::getMapDecodeFunctionPrototype(TAB_IN, false);
ProtocolFile::makeLineSeparator(output);
}
ProtocolFile::makeLineSeparator(output);
// Finally the local members of this class. Notice that if we only have
// parameter functions then we do not output these (and the class is
// esentially static). The same is true if we are redefining another class,
// in which case we use the members from the base class
if(getNumberInMemory() > 0)
{
// Now declare the members of this class
for(std::size_t i = 0; i < encodables.size(); i++)
structure += encodables[i]->getDeclaration();
// Make classes pretty with alignment goodness
output += alignStructureData(structure);
}
ProtocolFile::makeLineSeparator(output);
}// If outputting structure functions
// Close out the class
output += "}; // " + typeName + "\n";
return output;
}// ProtocolStructure::getClassDeclaration_CPP
/*!
* Create utility functions for packet ID and lengths. The structure must
* already have been parsed to give the lengths.
* \param spacing sets the amount of space to put before each line.
* \return the string which goes in the header or class definition, depending
* on the language being output
*/
std::string ProtocolPacket::createUtilityFunctions(const std::string& spacing) const
{
std::string output;
if(support.language == ProtocolSupport::c_language)
{
// The macro for the packet ID, we only emit this if the packet has a single ID, which is the normal case
if(ids.size() == 1)
{
output += spacing + "//! return the packet ID for the " + support.prefix + name + " packet\n";
output += spacing + "#define get" + support.prefix + name + support.packetParameterSuffix + "ID() (" + ids.at(0) + ")\n";
output += "\n";
}
// The macro for the minimum packet length
output += spacing + "//! return the minimum encoded length for the " + support.prefix + name + " packet\n";
output += spacing + "#define get" + support.prefix + name + "MinDataLength() ";
if(encodedLength.minEncodedLength.empty())
output += "0\n";
else
output += "("+encodedLength.minEncodedLength + ")\n";
// The macro for the maximum packet length
output += "\n";
output += spacing + "//! return the maximum encoded length for the " + support.prefix + name + " packet\n";
output += spacing + "#define get" + support.prefix + name + "MaxDataLength() ";
if(encodedLength.maxEncodedLength.empty())
output += "0\n";
else
output += "("+encodedLength.maxEncodedLength + ")\n";
}
else
{
if(ids.size() == 1)
{
output += spacing + "//! \\return the identifier for the packet\n";
output += spacing + "static uint32_t id(void) { return " + ids.at(0) + ";}\n";
output += "\n";
}
// The minimum packet length
output += spacing + "//! \\return the minimum encoded length for the packet\n";
output += spacing + "static int minLength(void) { return ";
if(encodedLength.minEncodedLength.empty())
output += "0;}\n";
else
output += "("+encodedLength.minEncodedLength + ");}\n";
// The maximum packet length
output += "\n";
output += spacing + "//! \\return the maximum encoded length for the packet\n";
output += spacing + "static int maxLength(void) { return ";
if(encodedLength.maxEncodedLength.empty())
output += "0;}\n";
else
output += "("+encodedLength.maxEncodedLength + ");}\n";
}
return output;
}// ProtocolPacket::createUtilityFunctions
/*!
* Write the initializer / constructor function for this packet only
*/
void ProtocolPacket::createTopLevelInitializeFunction(void)
{
if(hasInit() && (verifySource != nullptr) && (redefines == nullptr))
{
// In C++ this is part of the class declaration
if((support.language == ProtocolSupport::c_language) && (verifyHeader != nullptr))
{
verifyHeader->makeLineSeparator();
verifyHeader->write(getSetToInitialValueFunctionPrototype(std::string(), false));
verifyHeader->makeLineSeparator();
}
// In C++ we do not output the constructor if we have no class members
// Notice that if we are not outputting structure functions, and this
// class won't be used by others, we will not have any data members and
// do not need a constructor.
if((support.language == ProtocolSupport::c_language) || ((getNumberInMemory() > 0) && (useInOtherPackets || structureFunctions)))
{
verifySource->makeLineSeparator();
verifySource->write(getSetToInitialValueFunctionBody(false));
verifySource->makeLineSeparator();
}
}
}// ProtocolPacket::createTopLevelInitializeFunction
/*!
* Write data to the source and header files to encode and decode structure
* functions that do not use a packet. For this structure only, not its children
*/
void ProtocolPacket::createTopLevelStructureFunctions(void)
{
// If we are using this structure in other packets, we need the structure
// functions that come from ProtocolStructureModule
if(useInOtherPackets)
{
if(encode)
{
// In C++ this is part of the class declaration
if(support.language == ProtocolSupport::c_language)
{
header.makeLineSeparator();
header.write(getEncodeFunctionPrototype(std::string(), false));
}
source.makeLineSeparator();
source.write(getEncodeFunctionBody(support.bigendian, false));
}
if(decode)
{
// In C++ this is part of the class declaration
if(support.language == ProtocolSupport::c_language)
{
header.makeLineSeparator();
header.write(getDecodeFunctionPrototype(std::string(), false));
}
source.makeLineSeparator();
source.write(getDecodeFunctionBody(support.bigendian, false));
}
header.makeLineSeparator();
source.makeLineSeparator();
}// if we are not suppressing the encode and decode functions
createTopLevelStructureHelperFunctions();
}// ProtocolPacket::createTopLevelStructureFunctions
/*!
* Create the functions for encoding and decoding the packet to/from a structure
*/
void ProtocolPacket::createStructurePacketFunctions(void)
{
int numDecodes = getNumberOfDecodeParameters();
// The prototypes in the header file are only needed for C,
// in C++ these prototypes are part of the class declaration.
if(support.language == ProtocolSupport::c_language)
{
// In the event that there are no parameters, the parameter function
// is the same as the structure function - so don't output both
if(encode && ((getNumberOfEncodeParameters() > 0) || !parameterFunctions))
{
// The prototype for the structure packet encode function
header.makeLineSeparator();
header.write(getStructurePacketEncodePrototype(std::string()));
}
// In the event that there are no parameters, the parameter function
// is the same as the structure function - so don't output both
if(decode && ((numDecodes > 0) || !parameterFunctions))
{
// The prototype for the structure packet decode function
header.makeLineSeparator();
header.write(getStructurePacketDecodePrototype(std::string()));
}
if(compare && compareHeader != nullptr)
{
compareHeader->makeLineSeparator();
compareHeader->write("//! Compare two " + support.prefix + name + " packets and generate a report\n");
compareHeader->write("std::string compare" + support.prefix + name + support.packetParameterSuffix + "(std::string prename, const " + support.pointerType + " pkt1, const " + support.pointerType + " pkt2);\n");
compareHeader->makeLineSeparator();
}
if(print && printHeader != nullptr)
{
printHeader->makeLineSeparator();
printHeader->write("//! Generate a string that describes the contents of a " + name + " packet\n");
printHeader->write("std::string textPrint" + (support.prefix + name + support.packetParameterSuffix) + "(std::string prename, const " + support.pointerType + " pkt);\n");
printHeader->makeLineSeparator();
}
}// If C language
// In the event that there are no parameters, the parameter function
// is the same as the structure function - so don't output both
if(encode && ((getNumberOfEncodeParameters() > 0) || !parameterFunctions))
{
// The source function for the encode function
source.makeLineSeparator();
source.write(getStructurePacketEncodeBody());
}
// In the event that there are no parameters, the parameter function
// is the same as the structure function - so don't output both
if(decode && ((numDecodes > 0) || !parameterFunctions))
{
// The source function for the decode function
source.makeLineSeparator();
source.write(getStructurePacketDecodeBody());
}
if(compare && (compareSource != nullptr))
{
compareSource->makeLineSeparator();
compareSource->write("/*!\n");
compareSource->write(" * Compare two " + name + " packets and generate a report of any differences.\n");
compareSource->write(" * \\param _pg_prename is prepended to the name of the data field in the comparison report\n");
compareSource->write(" * \\param _pg_pkt1 is the first data to compare\n");
compareSource->write(" * \\param _pg_pkt2 is the second data to compare\n");
compareSource->write(" * \\return a string describing any differences between pk1 and pkt2. The string will be empty if there are no differences\n");
compareSource->write(" */\n");
if(support.language == ProtocolSupport::c_language)
compareSource->write("std::string compare" + (support.prefix + name + support.packetParameterSuffix) + "(std::string _pg_prename, const " + support.pointerType + " _pg_pkt1, const " + support.pointerType + " _pg_pkt2)\n");
else
compareSource->write("std::string " + typeName + "::compare(std::string _pg_prename, const " + support.pointerType + " _pg_pkt1, const " + support.pointerType + " _pg_pkt2)\n");
compareSource->write("{\n");
compareSource->write(TAB_IN + "std::string _pg_report;\n");
if(numDecodes > 0)
{
compareSource->makeLineSeparator();
compareSource->write(TAB_IN + "// Structures to decode into\n");
compareSource->write(TAB_IN + structName + " _pg_struct1, _pg_struct2;\n");
compareSource->makeLineSeparator();
compareSource->write(TAB_IN + "if(_pg_prename.empty())\n");
compareSource->write(TAB_IN + TAB_IN + "_pg_prename = \"" + name + "\";\n");
if(support.language == ProtocolSupport::c_language)
{
// In C we need explicity initializers
compareSource->makeLineSeparator();
compareSource->write(TAB_IN + "// All zeroes before decoding\n");
compareSource->write(TAB_IN + "memset(&_pg_struct1, 0, sizeof(_pg_struct1));\n");
compareSource->write(TAB_IN + "memset(&_pg_struct2, 0, sizeof(_pg_struct2));\n");
compareSource->makeLineSeparator();
compareSource->write(TAB_IN + "// Decode each packet\n");
compareSource->write(TAB_IN + "if(!decode" + extendedName() + "(_pg_pkt1, &_pg_struct1) || !decode" + extendedName() + "(_pg_pkt2, &_pg_struct2))\n");
}
else
{
compareSource->makeLineSeparator();
compareSource->write(TAB_IN + "// Decode each packet\n");
compareSource->write(TAB_IN + "if(!_pg_struct1.decode(_pg_pkt1) || !_pg_struct2.decode(_pg_pkt2))\n");
}
compareSource->write(TAB_IN + "{\n");
compareSource->write(TAB_IN + TAB_IN + "_pg_report = _pg_prename + \" packets failed to decode\\n\";\n");
compareSource->write(TAB_IN + TAB_IN + "return _pg_report;\n");
compareSource->write(TAB_IN + "}\n");
}
else
{
compareSource->makeLineSeparator();
compareSource->write(TAB_IN + "if(_pg_prename.empty())\n");
compareSource->write(TAB_IN + TAB_IN + "_pg_prename = \"" + name + "\";\n");
compareSource->makeLineSeparator();
compareSource->write(TAB_IN + "// Check packet types\n");
compareSource->write(TAB_IN + "if((get" + support.protoName + "PacketID(_pg_pkt1) != get" + support.protoName + "PacketID(_pg_pkt1)) || (get"+ support.protoName + "PacketID(_pg_pkt2) != get" + (support.prefix + name + support.packetParameterSuffix) + "ID()))\n");
compareSource->write(TAB_IN + "{\n");
compareSource->write(TAB_IN + TAB_IN + "_pg_report += _pg_prename + \" packet IDs are different\\n\";\n");
compareSource->write(TAB_IN + TAB_IN + "return _pg_report;\n");
compareSource->write(TAB_IN + "}\n");
}
compareSource->makeLineSeparator();
compareSource->write(TAB_IN + "// Check packet sizes. Even if sizes are different the packets may contain the same result\n");
compareSource->write(TAB_IN + "if(get" + support.protoName + "PacketSize(_pg_pkt1) != get" + support.protoName + "PacketSize(_pg_pkt2))\n");
compareSource->write(TAB_IN + TAB_IN + "_pg_report += _pg_prename + \" packet sizes are different\\n\";\n");
if(numDecodes > 0)
{
compareSource->makeLineSeparator();
if(support.language == ProtocolSupport::c_language)
compareSource->write(TAB_IN + "_pg_report += compare" + structName + "(_pg_prename, &_pg_struct1, &_pg_struct2);\n");
else
compareSource->write(TAB_IN + "_pg_report += _pg_struct1.compare(_pg_prename, &_pg_struct2);\n");
}
compareSource->makeLineSeparator();
compareSource->write(TAB_IN + "return _pg_report;\n");
compareSource->write("\n");
if(support.language == ProtocolSupport::c_language)
compareSource->write("}// compare" + support.prefix + name + support.packetParameterSuffix + "\n");
else
compareSource->write("}// " + typeName + "::compare\n");
}// if we need to generate compare functions
if(print && (printSource != nullptr))
{
printSource->makeLineSeparator();
printSource->write("/*!\n");
printSource->write(" * Generate a string that describes the contents of a " + name + " packet\n");
printSource->write(" * \\param _pg_prename is prepended to the name of the data field in the report\n");
printSource->write(" * \\param _pg_pkt is the data to print\n");
printSource->write(" * \\return a string describing the contents of _pg_pkt\n");
printSource->write(" */\n");
if(support.language == ProtocolSupport::c_language)
printSource->write("std::string textPrint" + (support.prefix + name + support.packetParameterSuffix) + "(std::string _pg_prename, const " + support.pointerType + " _pg_pkt)\n");
else
printSource->write("std::string " + typeName + "::textPrint(std::string _pg_prename, const " + support.pointerType + " _pg_pkt)\n");
printSource->write("{\n");
printSource->write(TAB_IN + "std::string _pg_report;\n");
if(numDecodes > 0)
{
printSource->makeLineSeparator();
printSource->write(TAB_IN + "// Structure to decode into\n");
printSource->write(TAB_IN + structName + " _pg_user;\n");
printSource->makeLineSeparator();
printSource->write(TAB_IN + "if(_pg_prename.empty())\n");
printSource->write(TAB_IN + TAB_IN + "_pg_prename = \"" + name + "\";\n");
if(support.language == ProtocolSupport::c_language)
{
// In C we need explicity initializers
printSource->makeLineSeparator();
printSource->write(TAB_IN + "// All zeroes before decoding\n");
printSource->write(TAB_IN + "memset(&_pg_user, 0, sizeof(_pg_user));\n");
printSource->makeLineSeparator();
printSource->write(TAB_IN + "// Decode packet\n");
printSource->write(TAB_IN + "if(!decode" + extendedName() + "(_pg_pkt, &_pg_user))\n");
}
else
{
printSource->makeLineSeparator();
printSource->write(TAB_IN + "// Decode packet\n");
printSource->write(TAB_IN + "if(!_pg_user.decode(_pg_pkt))\n");
}
printSource->write(TAB_IN + "{\n");
printSource->write(TAB_IN + TAB_IN + "_pg_report = _pg_prename + \" packet failed to decode\\n\";\n");
printSource->write(TAB_IN + TAB_IN + "return _pg_report;\n");
printSource->write(TAB_IN + "}\n");
}
else
{
printSource->makeLineSeparator();
printSource->write(TAB_IN + "if(_pg_prename.empty())\n");
printSource->write(TAB_IN + TAB_IN + "_pg_prename = \"" + name + "\";\n");
printSource->makeLineSeparator();
printSource->write(TAB_IN + "// Check packet type\n");
printSource->write(TAB_IN + "if(get"+ support.protoName + "PacketID(_pg_pkt) != get" + (support.prefix + name + support.packetParameterSuffix) + "ID())\n");
printSource->write(TAB_IN + "{\n");
printSource->write(TAB_IN + TAB_IN + "_pg_report += _pg_prename + \" packet ID is incorrect\\n\";\n");
printSource->write(TAB_IN + TAB_IN + "return _pg_report;\n");
printSource->write(TAB_IN + "}\n");
}
printSource->makeLineSeparator();
printSource->write(TAB_IN + "// Print the packet size\n");
printSource->write(TAB_IN + "_pg_report += _pg_prename + \" packet size is \" + std::to_string(get" + support.protoName + "PacketSize(_pg_pkt)) + \"\\n\";\n");
if(numDecodes > 0)
{
printSource->makeLineSeparator();
if(support.language == ProtocolSupport::c_language)
printSource->write(TAB_IN + "_pg_report += textPrint" + structName + "(_pg_prename, &_pg_user);\n");
else
printSource->write(TAB_IN + "_pg_report += _pg_user.textPrint(_pg_prename);\n");
}
printSource->makeLineSeparator();
printSource->write(TAB_IN + "return _pg_report;\n");
printSource->write("\n");
if(support.language == ProtocolSupport::c_language)
printSource->write("}// textPrint" + (support.prefix + name + support.packetParameterSuffix) + "\n");
else
printSource->write("}// " + typeName + "::textPrint\n");
}// if we need to generate print functions
}// createStructurePacketFunctions
/*!
* Get the signature of the packet structure encode function, without semicolon or
* comments or line feed, for the prototype or actual function.
* \param insource should be true to indicate this signature is in source code
* (i.e. not a prototype) which determines if the "_pg_" decoration is
* used as well as c++ access specifiers.
* \return the encode signature
*/
std::string ProtocolPacket::getStructurePacketEncodeSignature(bool insource) const
{
std::string output;
std::string pg;
int numEncodes = getNumberOfEncodeParameters();
if(insource)
pg = "_pg_";
if(support.language == ProtocolSupport::c_language)
{
output = "void encode" + support.prefix + name + support.packetStructureSuffix + "(" + support.pointerType + " " + pg + "pkt";
if(numEncodes > 0)
output += ", const " + structName + "* " + pg + "user";
}
else
{
// C++ class member, this function should be "const" as it does not modify
// any class members, unless it has no encode parameters, in which case it
// should be "static".
if(!insource && (numEncodes <= 0))
output += "static ";
output += "void ";
// In the source the function needs the class scope
if(insource)
output += typeName + "::";
output += "encode(" + support.pointerType + " " + pg + "pkt";
}
if(ids.size() <= 1)
output += ")";
else
output += ", uint32_t " + pg + "id)";
if((support.language == ProtocolSupport::cpp_language) && (numEncodes > 0))
output += " const";
return output;
}// ProtocolPacket::getStructurePacketEncodeSignature
/*!
* Get the prototype for the structure packet encode function
* \param spacing is the offset for each line
* \return the prototype including semicolon and line fees
*/
std::string ProtocolPacket::getStructurePacketEncodePrototype(const std::string& spacing) const
{
std::string output;
if(!encode)
return output;
output += spacing + "//! " + getPacketEncodeBriefComment() + "\n";
output += spacing + getStructurePacketEncodeSignature(false) + ";\n";;
return output;
}
/*!
* Get the body for the structure packet encode function
* \return The body of the function that encodes this packet from a structure or class.
*/
std::string ProtocolPacket::getStructurePacketEncodeBody(void) const
{
std::string output;
if(!encode)
return output;
int numEncodes = getNumberOfEncodeParameters();
// The source function for the encode function
output += "/*!\n";
output += " * \\brief " + getPacketEncodeBriefComment() + "\n";
output += " *\n";