forked from billvaglienti/ProtoGen
-
Notifications
You must be signed in to change notification settings - Fork 5
/
fieldcoding.cpp
1130 lines (901 loc) · 36.3 KB
/
fieldcoding.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 "fieldcoding.h"
#include "protocolparser.h"
FieldCoding::FieldCoding(ProtocolSupport sup) :
ProtocolScaling(sup)
{
// we use 64-bit integers for floating point
if(!support.int64)
support.float64 = false;
if(support.int64)
{
typeNames = {"uint64_t", "int64_t", "uint64_t", "int64_t", "uint64_t", "int64_t", "uint64_t", "int64_t"};
typeSigNames = {"uint64" , "int64" , "uint56" , "int56" , "uint48" , "int48" , "uint40" , "int40" };
typeSizes = { 8 , 8 , 7 , 7 , 6 , 6 , 5 , 5 };
typeUnsigneds= { true , false , true , false , true , false , true , false };
}
// These types are always supported
std::vector<std::string> temp1 = {"float" , "uint32_t", "int32_t", "uint32_t", "int32_t", "uint16_t", "int16_t", "uint8_t", "int8_t"};
std::vector<std::string> temp2 = {"float32", "uint32" , "int32" , "uint24" , "int24" , "uint16" , "int16" , "uint8" , "int8" };
std::vector<int> temp3 = { 4 , 4 , 4 , 3 , 3 , 2 , 2 , 1 , 1 };
std::vector<bool> temp4 = { false , true , false , true , false , true , false , true , false };
typeNames.insert(typeNames.end(), temp1.begin(), temp1.end());
typeSigNames.insert(typeSigNames.end(), temp2.begin(), temp2.end());
typeSizes.insert(typeSizes.end(), temp3.begin(), temp3.end());
typeUnsigneds.insert(typeUnsigneds.end(), temp4.begin(), temp4.end());
if(support.float64)
{
typeNames.push_back("double");
typeSigNames.push_back("float64");
typeSizes.push_back(8);
typeUnsigneds.push_back(false);
}
if(support.specialFloat)
{
typeNames.push_back("float"); typeNames.push_back("float");
typeSigNames.push_back("float24"); typeSigNames.push_back("float16");
typeSizes.push_back(3); typeSizes.push_back(2);
typeUnsigneds.push_back(false); typeUnsigneds.push_back(false);
}
}
/*!
* Generate the source and header files for field coding
* \param fileNameList is appended with the names of the generated files
* \param filePathList is appended with the paths of the generated files
* \return true if both modules are generated
*/
bool FieldCoding::generate(std::vector<std::string>& fileNameList, std::vector<std::string>& filePathList)
{
if(generateEncodeHeader())
{
fileNameList.push_back(header.fileName());
filePathList.push_back(header.filePath());
}
else
return false;
if(generateEncodeSource())
{
fileNameList.push_back(source.fileName());
filePathList.push_back(source.filePath());
}
else
return false;
if(generateDecodeHeader())
{
fileNameList.push_back(header.fileName());
filePathList.push_back(header.filePath());
}
else
return false;
if(generateDecodeSource())
{
fileNameList.push_back(source.fileName());
filePathList.push_back(source.filePath());
}
else
return false;
return true;
}
/*!
* Generate the header file for protocol array scaling
* \return true if the file is generated.
*/
bool FieldCoding::generateEncodeHeader(void)
{
header.setModuleNameAndPath("fieldencode", support.outputpath, support.language);
// Raw string magic
header.setFileComment(R"(fieldencode provides routines to place numbers into a byte stream.
fieldencode provides routines to place numbers in local memory layout into
a big or little endian byte stream. The byte stream is simply a sequence of
bytes, as might come from the data payload of a packet.
Support is included for non-standard types such as unsigned 24. When
working with nonstandard types the data in memory are given using the next
larger standard type. For example an unsigned 24 is actually a uint32_t in
which the most significant byte is clear, and only the least significant
three bytes are placed into a byte stream
Big or Little Endian refers to the order that a computer architecture will
place the bytes of a multi-byte word into successive memory locations. For
example the 32-bit number 0x01020304 can be placed in successive memory
locations in Big Endian: [0x01][0x02][0x03][0x04]; or in Little Endian:
[0x04][0x03][0x02][0x01]. The names "Big Endian" and "Little Endian" come
from Swift's Gulliver's travels, referring to which end of an egg should be
opened. The choice of name is made to emphasize the degree to which the
choice of memory layout is un-interesting, as long as one stays within the
local memory.
When transmitting data from one computer to another that assumption no
longer holds. In computer-to-computer transmission there are three endians
to consider: the endianness of the sender, the receiver, and the protocol
between them. A protocol is Big Endian if it sends the most significant
byte first and the least significant last. If the computer and the protocol
have the same endianness then encoding data from memory into a byte stream
is a simple copy. However if the endianness is not the same then bytes must
be re-ordered for the data to be interpreted correctly.)");
header.makeLineSeparator();
header.write("\n#define __STDC_CONSTANT_MACROS\n");
header.write("#include <stdint.h>\n");
if(support.supportbool)
header.writeIncludeDirective("stdbool.h", "", true);
header.makeLineSeparator();
// Raw string magic
header.write(R"(//! Macro to limit a number to be no more than a maximum value
#define limitMax(number, max) (((number) > (max)) ? (max) : (number))
//! Macro to limit a number to be no less than a minimum value
#define limitMin(number, min) (((number) < (min)) ? (min) : (number))
//! Macro to limit a number to be no less than a minimum value and no more than a maximum value
#define limitBoth(number, min, max) (((number) > (max)) ? (max) : (limitMin((number), (min))))
//! Copy a null terminated string
void pgstrncpy(char* dst, const char* src, int maxLength);
//! Encode a null terminated string on a byte stream
void stringToBytes(const char* string, uint8_t* bytes, int* index, int maxLength, int fixedLength);
//! Copy an array of bytes to a byte stream without changing the order.
void bytesToBeBytes(const uint8_t* data, uint8_t* bytes, int* index, int num);
//! Copy an array of bytes to a byte stream while reversing the order.
void bytesToLeBytes(const uint8_t* data, uint8_t* bytes, int* index, int num);)");
if(support.int64)
{
header.makeLineSeparator();
header.write("#ifdef UINT64_MAX\n");
}
for(int i = 0; i < (int)typeNames.size(); i++)
{
if(support.int64 && (i > 0))
{
if((typeSizes.at(i) == 4) && (typeSizes.at(i-1) == 5))
header.write("\n#endif // UINT64_MAX\n");
}
if(typeSizes[i] != 1)
{
// big endian
header.makeLineSeparator();
header.write("//! " + briefEncodeComment(i, true) + "\n");
header.write(encodeSignature(i, true) + ";\n");
// little endian
header.makeLineSeparator();
header.write("//! " + briefEncodeComment(i, false) + "\n");
header.write(encodeSignature(i, false) + ";\n");
}
else
{
header.makeLineSeparator();
header.write("//! " + briefEncodeComment(i, true) + "\n");
header.write(encodeSignature(i, true) + "\n");
}
}// for all output byte counts
header.makeLineSeparator();
return header.flush();
}// FieldCoding::generateEncodeHeader
/*!
* Generate the source file for protocols caling
* \return true if the file is generated.
*/
bool FieldCoding::generateEncodeSource(void)
{
source.setModuleNameAndPath("fieldencode", support.outputpath, support.language);
if(support.specialFloat)
source.writeIncludeDirective("floatspecial");
source.makeLineSeparator();
source.write(R"(/*!
* Copy a null terminated string to a destination whose maximum length (with
* null terminator) is `maxLength`. The destination string is guaranteed to
* have a null terminator when this operation is complete. This is a
* replacement for strncpy().
* \param dst receives the string, and is guaranteed to be null terminated.
* \param src is the null terminated source string to copy.
* \param maxLength is the size of the `dst` buffer.
*/
void pgstrncpy(char* dst, const char* src, int maxLength)
{
int index = 0;
stringToBytes(src, (uint8_t*)dst, &index, maxLength, 0);
}
/*!
* Encode a null terminated string on a byte stream
* \param string is the null termianted string to encode
* \param bytes is a pointer to the byte stream which receives the encoded data.
* \param index gives the location of the first byte in the byte stream, and
* will be incremented by the number of bytes encoded when this function
* is complete.
* \param maxLength is the maximum number of bytes that can be encoded. A null
* terminator is always included in the encoding.
* \param fixedLength should be 1 to force the number of bytes encoded to be
* exactly equal to maxLength.
*/
void stringToBytes(const char* string, uint8_t* bytes, int* index, int maxLength, int fixedLength)
{
int i;
// increment byte pointer for starting point
bytes += (*index);
// Reserve the last byte for null termination
for(i = 0; i < maxLength - 1; i++)
{
if(string[i] == 0)
break;
else
bytes[i] = (uint8_t)string[i];
}
// Make sure last byte has null termination
bytes[i++] = 0;
if(fixedLength)
{
// Finish with null bytes
for(; i < maxLength; i++)
bytes[i] = 0;
}
// Return for the number of bytes we encoded
(*index) += i;
}// stringToBytes
/*!
* Copy an array of bytes to a byte stream without changing the order.
* \param data is the array of bytes to copy.
* \param bytes is a pointer to the byte stream which receives the encoded data.
* \param index gives the location of the first byte in the byte stream, and
* will be incremented by num when this function is complete.
* \param num is the number of bytes to copy
*/
void bytesToBeBytes(const uint8_t* data, uint8_t* bytes, int* index, int num)
{
// increment byte pointer for starting point
bytes += (*index);
// Increment byte index to indicate number of bytes copied
(*index) += num;
// Copy the bytes without changing the order
while(num > 0)
{
*(bytes++) = *(data++);
num--;
}
}// bytesToBeBytes
/*!
* Copy an array of bytes to a byte stream while reversing the order.
* \param data is the array of bytes to copy.
* \param bytes is a pointer to the byte stream which receives the encoded data.
* \param index gives the location of the first byte in the byte stream, and
* will be incremented by num when this function is complete.
* \param num is the number of bytes to copy
*/
void bytesToLeBytes(const uint8_t* data, uint8_t* bytes, int* index, int num)
{
// increment byte pointer for starting point
bytes += (*index);
// Increment byte index to indicate number of bytes copied
(*index) += num;
// To encode as "little endian bytes", (a nonsensical statement), reverse the byte order
bytes += (num - 1);
// Copy the bytes, reversing the order
while(num > 0)
{
*(bytes--) = *(data++);
num--;
}
}// bytesToLeBytes)");
source.makeLineSeparator();
if(support.int64)
{
source.makeLineSeparator();
source.write("#ifdef UINT64_MAX\n");
}
for(int i = 0; i < (int)typeNames.size(); i++)
{
if(support.int64 && (i > 0))
{
if((typeSizes.at(i) == 4) && (typeSizes.at(i-1) == 5))
source.write("#endif // UINT64_MAX\n");
}
if(typeSizes[i] != 1)
{
// big endian
source.makeLineSeparator();
source.write(fullEncodeComment(i, true) + "\n");
source.write(fullEncodeFunction(i, true) + "\n");
// little endian
source.makeLineSeparator();
source.write(fullEncodeComment(i, false) + "\n");
source.write(fullEncodeFunction(i, false) + "\n");
}
}
source.makeLineSeparator();
return source.flush();
}// FieldCoding::generateEncodeSource
/*!
* Get a human readable type name like "unsigned 3 byte integer".
* \param type is the type enumeration
* \return the human readable type name
*/
std::string FieldCoding::getReadableTypeName(int type)
{
std::string name;
if(contains(typeSigNames.at(type), "float64"))
{
name = "8 byte float";
}
else if(contains(typeSigNames.at(type), "float32"))
{
name = "4 byte float";
}
else
{
if(typeUnsigneds.at(type))
name = "unsigned ";
else
name = "signed ";
name += std::to_string(typeSizes.at(type));
name += " byte integer";
}
return name;
}// FieldCoding::getReadableTypeName
/*!
* Create the brief function comment, without doxygen decorations
* \param type is the enumerator for the type.
* \param bigendian should be true if the function outputs big endian byte order.
* \return The string that represents the one line function comment.
*/
std::string FieldCoding::briefEncodeComment(int type, bool bigendian)
{
std::string name = getReadableTypeName(type);
if(typeSizes[type] == 1)
{
// No endian concerns if using only 1 byte
return "Encode a " + name + " on a byte stream.";
}
else
{
std::string endian;
if(bigendian)
endian = "big";
else
endian = "little";
return "Encode a " + name + " on a " + endian + " endian byte stream.";
}// If multi-byte
}// FieldCoding::briefEncodeComment
/*!
* Create the full encode function comment, with doxygen decorations
* \param type is the enumerator for the type.
* \param bigendian should be true if the function outputs big endian byte order.
* \return The string that represents the full multi-line function comment.
*/
std::string FieldCoding::fullEncodeComment(int type, bool bigendian)
{
std::string comment = "/*!\n";
comment += ProtocolParser::outputLongComment(" * ", briefEncodeComment(type, bigendian)) + "\n";
comment += " * \\param number is the value to encode.\n";
comment += " * \\param bytes is a pointer to the byte stream which receives the encoded data.\n";
comment += " * \\param index gives the location of the first byte in the byte stream, and\n";
comment += " * will be incremented by " + std::to_string(typeSizes[type]) + " when this function is complete.\n";
if(contains(typeSigNames[type], "float24") || contains(typeSigNames[type], "float16"))
comment += " * \\param sigbits is the number of bits to use in the significand of the float.\n";
comment += " */";
return comment;
}
/*!
* Create the one line function signature, without a trailing semicolon
* \param type is the enumerator for the type.
* \param bigendian should be true if the function outputs big endian byte order.
* \return The string that represents the function signature, without a trailing semicolon
*/
std::string FieldCoding::encodeSignature(int type, bool bigendian)
{
std::string endian;
// No endian concerns if using only 1 byte
if(typeSizes[type] > 1)
{
if(bigendian)
endian = "Be";
else
endian = "Le";
if(contains(typeSigNames[type], "float24") || contains(typeSigNames[type], "float16"))
return std::string("void " + typeSigNames[type] + "To" + endian + "Bytes(" + typeNames[type] + " number, uint8_t* bytes, int* index, int sigbits)");
else
return std::string("void " + typeSigNames[type] + "To" + endian + "Bytes(" + typeNames[type] + " number, uint8_t* bytes, int* index)");
}
else
{
return std::string("#define " + typeSigNames[type] + "ToBytes(number, bytes, index) (bytes)[(*(index))++] = ((" + typeNames[type] + ")(number))");
}
}// FieldCoding::encodeSignature
/*!
* Generate the full encode function output, excluding the comment
* \param type is the enumerator for the type.
* \param bigendian should be true if the function outputs big endian byte order.
* \return the function as a string
*/
std::string FieldCoding::fullEncodeFunction(int type, bool bigendian)
{
if(contains(typeSigNames[type], "float"))
return floatEncodeFunction(type, bigendian);
else
return integerEncodeFunction(type, bigendian);
}
/*!
* Generate the full encode function output, excluding the comment, for floating point types
* \param type is the enumerator for the type.
* \param bigendian should be true if the function outputs big endian byte order.
* \return the function as a string
*/
std::string FieldCoding::floatEncodeFunction(int type, bool bigendian)
{
std::string endian;
if(bigendian)
endian = "Be";
else
endian = "Le";
std::string function = encodeSignature(type, bigendian) + "\n";
function += "{\n";
if((typeSizes[type] == 8) || (typeSizes[type] == 4))
{
function += " union\n";
function += " {\n";
if(typeSizes[type] == 8)
{
function += " double floatValue;\n";
function += " uint64_t integerValue;\n";
}
else
{
function += " float floatValue;\n";
function += " uint32_t integerValue;\n";
}
function += " }field;\n";
function += "\n";
function += " field.floatValue = number;\n";
function += "\n";
function += " uint" + std::to_string(8*typeSizes[type]) + "To" + endian + "Bytes(field.integerValue, bytes, index);\n";
}
else if(typeSizes[type] == 3)
{
function += " uint24To" + endian + "Bytes(float32ToFloat24(number, sigbits), bytes, index);\n";
}
else
function += " uint16To" + endian + "Bytes(float32ToFloat16(number, sigbits), bytes, index);\n";
function += "}\n";
return function;
}// FieldCoding::floatEncodeFunction
/*!
* Generate the full encode function output, excluding the comment, for integer types
* \param type is the enumerator for the type.
* \param bigendian should be true if the function outputs big endian byte order.
* \return the function as a string
*/
std::string FieldCoding::integerEncodeFunction(int type, bool bigendian)
{
std::string function = encodeSignature(type, bigendian) + "\n";
function += "{\n";
if(typeSizes[type] == 1)
return "// ";
else
{
function += " // increment byte pointer for starting point\n";
std::string opt;
if(bigendian)
{
function += " bytes += (*index) + " + std::to_string(typeSizes[type]-1) + ";\n";
opt = "--";
}
else
{
function += " bytes += (*index);\n";
opt = "++";
}
int offset;
function += "\n";
offset = typeSizes[type];
while(offset > 1)
{
function += " *(bytes" + opt + ") = (uint8_t)(number);\n";
function += " number = number >> 8;\n";
offset--;
}
// Finish with the most significant byte
function += " *bytes = (uint8_t)(number);\n";
function += "\n";
// Update the index value to the user
function += " (*index) += " + std::to_string(typeSizes[type]) + ";\n";
}// if multi-byte fields
function += "}\n";
return function;
}// FieldCoding::integerEncodeFunction
/*!
* Generate the header file for protocols caling
* \return true if the file is generated.
*/
bool FieldCoding::generateDecodeHeader(void)
{
header.setModuleNameAndPath("fielddecode", support.outputpath, support.language);
// Top level comment
header.setFileComment(R"(fielddecode provides routines to pull numbers from a byte stream.
fielddecode provides routines to pull numbers in local memory layout from
a big or little endian byte stream. It is the opposite operation from the
routines contained in fieldencode.h
When compressing unsigned numbers (for example 32-bits to 16-bits) the most
signficant bytes are discarded and the only requirement is that the value of
the number fits in the smaller width. When going the other direction the
most significant bytes are simply set to 0x00. However signed two's
complement numbers are more complicated.
If the signed value is a positive number that fits in the range then the
most significant byte will be zero, and we can discard it. If the signed
value is negative (in two's complement) then the most significant bytes are
0xFF and again we can throw them away. See the example below
32-bit +100 | 16-bit +100 | 8-bit +100
0x00000064 | 0x0064 | 0x64 <-- notice most significant bit clear
32-bit -100 | 16-bit -100 | 8-bit -100
0xFFFFFF9C | 0xFF9C | 0x9C <-- notice most significant bit set
The signed complication comes when going the other way. If the number is
positive setting the most significant bytes to zero is correct. However
if the number is negative the most significant bytes must be set to 0xFF.
This is the process of sign-extension. Typically this is handled by the
compiler. For example if a int16_t is assigned to an int32_t the compiler
(or the processor instruction) knows to perform the sign extension. However
in our case we can decode signed 24-bit numbers (for example) which are
returned to the caller as int32_t. In this instance fielddecode performs the
sign extension.)");
header.write("\n");
header.write("#define __STDC_CONSTANT_MACROS\n");
header.write("#include <stdint.h>\n");
if(support.supportbool)
header.writeIncludeDirective("stdbool.h", "", true);
header.makeLineSeparator();
header.write(R"(//! Decode a null terminated string from a byte stream
void stringFromBytes(char* string, const uint8_t* bytes, int* index, int maxLength, int fixedLength);
//! Copy an array of bytes from a byte stream without changing the order.
void bytesFromBeBytes(uint8_t* data, const uint8_t* bytes, int* index, int num);
//! Copy an array of bytes from a byte stream while reversing the order.
void bytesFromLeBytes(uint8_t* data, const uint8_t* bytes, int* index, int num);)");
if(support.int64)
{
header.makeLineSeparator();
header.write("#ifdef UINT64_MAX\n");
}
for(int type = 0; type < (int)typeNames.size(); type++)
{
if(support.int64 && (type > 0))
{
if((typeSizes.at(type) == 4) && (typeSizes.at(type-1) == 5))
header.write("\n#endif // UINT64_MAX\n");
}
if(typeSizes[type] != 1)
{
header.makeLineSeparator();
header.write("//! " + briefDecodeComment(type, true) + "\n");
header.write(decodeSignature(type, true) + ";\n");
header.makeLineSeparator();
header.write("//! " + briefDecodeComment(type, false) + "\n");
header.write(decodeSignature(type, false) + ";\n");
}
else
{
header.makeLineSeparator();
header.write("//! " + briefDecodeComment(type, true) + "\n");
header.write(decodeSignature(type, true) + "\n");
}
}// for all input types
header.makeLineSeparator();
return header.flush();
}// FieldCoding::generateDecodeHeader
/*!
* Generate the source file for protocols caling
* \return true if the file is generated.
*/
bool FieldCoding::generateDecodeSource(void)
{
source.setModuleNameAndPath("fielddecode", support.outputpath, support.language);
if(support.specialFloat)
source.writeIncludeDirective("floatspecial");
source.makeLineSeparator();
// Raw string magic
source.write(R"(/*!
* Decode a null terminated string from a byte stream
* \param string receives the deocded null-terminated string.
* \param bytes is a pointer to the byte stream to be decoded.
* \param index gives the location of the first byte in the byte stream, and
* will be incremented by the number of bytes decoded when this function
* is complete.
* \param maxLength is the maximum number of bytes that can be decoded.
* maxLength includes the null terminator, which is always applied.
* \param fixedLength should be 1 to force the number of bytes decoded to be
* exactly equal to maxLength.
*/
void stringFromBytes(char* string, const uint8_t* bytes, int* index, int maxLength, int fixedLength)
{
int i;
// increment byte pointer for starting point
bytes += *index;
for(i = 0; i < maxLength - 1; i++)
{
if(bytes[i] == 0)
break;
else
string[i] = (char)bytes[i];
}
// Make sure we include null terminator
string[i++] = 0;
if(fixedLength)
(*index) += maxLength;
else
(*index) += i;
}// stringFromBytes
/*!
* Copy an array of bytes from a byte stream without changing the order.
* \param data receives the copied bytes
* \param bytes is a pointer to the byte stream to be copied from.
* \param index gives the location of the first byte in the byte stream, and
* will be incremented by num when this function is complete.
* \param num is the number of bytes to copy
*/
void bytesFromBeBytes(uint8_t* data, const uint8_t* bytes, int* index, int num)
{
// increment byte pointer for starting point
bytes += (*index);
// Increment byte index to indicate number of bytes copied
(*index) += num;
// Copy the bytes without changing the order
while(num > 0)
{
*(data++) = *(bytes++);
num--;
}
}// bytesFromBeBytes
/*!
* Copy an array of bytes from a byte stream, reversing the order.
* \param data receives the copied bytes
* \param bytes is a pointer to the byte stream to be copied.
* \param index gives the location of the first byte in the byte stream, and
* will be incremented by num when this function is complete.
* \param num is the number of bytes to copy
*/
void bytesFromLeBytes(uint8_t* data, const uint8_t* bytes, int* index, int num)
{
// increment byte pointer for starting point
bytes += (*index);
// Increment byte index to indicate number of bytes copied
(*index) += num;
// To encode as "little endian bytes", (a nonsensical statement), reverse the byte order
bytes += (num - 1);
// Copy the bytes, reversing the order
while(num > 0)
{
*(data++) = *(bytes--);
num--;
}
}// bytesFromLeBytes)");
if(support.int64)
{
source.makeLineSeparator();
source.write("#ifdef UINT64_MAX\n");
}
for(int type = 0; type < (int)typeNames.size(); type++)
{
if(support.int64 && (type > 0))
{
if((typeSizes.at(type) == 4) && (typeSizes.at(type-1) == 5))
source.write("#endif // UINT64_MAX\n");
}
if(typeSizes[type] != 1)
{
// big endian unsigned
source.makeLineSeparator();
source.write(fullDecodeComment(type, true) + "\n");
source.write(fullDecodeFunction(type, true) + "\n");
// little endian unsigned
source.makeLineSeparator();
source.write(fullDecodeComment(type, false) + "\n");
source.write(fullDecodeFunction(type, false) + "\n");
}
}// for all input types
source.makeLineSeparator();
return source.flush();
}// FieldCoding::generateDecodeSource
/*!
* Create the brief decode function comment, without doxygen decorations
* \param type is the enumerator for the type.
* \param bigendian should be true if the function outputs big endian byte order.
* \return The string that represents the one line function comment.
*/
std::string FieldCoding::briefDecodeComment(int type, bool bigendian)
{
std::string name = getReadableTypeName(type);
if(typeSizes[type] == 1)
{
// No endian concerns if using only 1 byte
return std::string("Decode a " + name + " from a byte stream.");
}
else
{
std::string endian;
if(bigendian)
endian = "big";
else
endian = "little";
return std::string("Decode a " + name + " from a " + endian + " endian byte stream.");
}// If multi-byte
}// FieldCoding::briefDecodeComment
/*!
* Create the full decode function comment, with doxygen decorations
* \param type is the enumerator for the type.
* \param bigendian should be true if the function outputs big endian byte order.
* \return The string that represents the full multi-line function comment.
*/
std::string FieldCoding::fullDecodeComment(int type, bool bigendian)
{
std::string comment= ("/*!\n");
comment += ProtocolParser::outputLongComment(" * ", briefDecodeComment(type, bigendian)) + "\n";
comment += " * \\param bytes is a pointer to the byte stream which contains the encoded data.\n";
comment += " * \\param index gives the location of the first byte in the byte stream, and\n";
comment += " * will be incremented by " + std::to_string(typeSizes[type]) + " when this function is complete.\n";
if(contains(typeSigNames[type], "float24") || contains(typeSigNames[type], "float16"))
comment += " * \\param sigbits is the number of bits to use in the significand of the float.\n";
comment += " * \\return the number decoded from the byte stream\n";
comment += " */";
return comment;
}
/*!
* Create the one line decode function signature, without a trailing semicolon
* \param type is the enumerator for the type.
* \param bigendian should be true if the function outputs big endian byte order.
* \return The string that represents the function signature, without a trailing semicolon
*/
std::string FieldCoding::decodeSignature(int type, bool bigendian)
{
std::string endian;
// No endian concerns if using only 1 byte
if(typeSizes[type] > 1)
{
if(bigendian)
endian = "Be";
else
endian = "Le";
if(contains(typeSigNames[type], "float24") || contains(typeSigNames[type], "float16"))
return std::string(typeNames[type] + " " + typeSigNames[type] + "From" + endian + "Bytes(const uint8_t* bytes, int* index, int sigbits)");
else
return std::string(typeNames[type] + " " + typeSigNames[type] + "From" + endian + "Bytes(const uint8_t* bytes, int* index)");
}
else
{
return std::string("#define " + typeSigNames[type] + "FromBytes(bytes, index) (" + typeNames[type] + ")((bytes)[(*(index))++])");
}
}// FieldCoding::decodeSignature
/*!
* Generate the full decode function output, excluding the comment
* \param type is the enumerator for the type.
* \param bigendian should be true if the function outputs big endian byte order.
* \return the function as a string
*/
std::string FieldCoding::fullDecodeFunction(int type, bool bigendian)
{
if(contains(typeSigNames[type], "float"))
return floatDecodeFunction(type, bigendian);
else
return integerDecodeFunction(type, bigendian);
}
/*!
* Generate the full decode function output, excluding the comment, for floating point types
* \param type is the enumerator for the type.
* \param bigendian should be true if the function outputs big endian byte order.
* \return the function as a string
*/
std::string FieldCoding::floatDecodeFunction(int type, bool bigendian)
{
std::string endian;
if(bigendian)
endian = "Be";
else
endian = "Le";
std::string function = decodeSignature(type, bigendian) + "\n";
function += "{\n";
if((typeSizes[type] == 8) || (typeSizes[type] == 4))
{
function += " union\n";
function += " {\n";
if(typeSizes[type] == 8)
{
function += " double floatValue;\n";
function += " uint64_t integerValue;\n";
}
else
{
function += " float floatValue;\n";
function += " uint32_t integerValue;\n";
}
function += " }field;\n";
function += "\n";
function += " field.integerValue = uint" + std::to_string(8*typeSizes[type]) + "From" + endian + "Bytes(bytes, index);\n";
function += "\n";
if(support.specialFloat)
{
if(typeSizes[type] == 8)