forked from billvaglienti/ProtoGen
-
Notifications
You must be signed in to change notification settings - Fork 5
/
protocolscaling.cpp
1633 lines (1390 loc) · 57.4 KB
/
protocolscaling.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 "protocolscaling.h"
#include "protocolparser.h"
/// TODO: make scalers positive
//! Determine if type is signed
bool ProtocolScaling::isTypeSigned(inmemorytypes_t type) const
{
switch(type)
{
default:
return true;
case uint64inmemory:
case uint32inmemory:
case uint16inmemory:
case uint8inmemory:
return false;
}
}
//! Determine if type is signed
bool ProtocolScaling::isTypeSigned(encodedtypes_t type) const
{
switch(type)
{
default:
return true;
case longbitencoded:
case uint64encoded:
case uint56encoded:
case uint48encoded:
case uint40encoded:
case bitencoded:
case uint32encoded:
case uint24encoded:
case uint16encoded:
case uint8encoded:
return false;
}
}
//! Determine if type is floating point
bool ProtocolScaling::isTypeFloating(inmemorytypes_t type) const
{
switch(type)
{
default:
return false;
case float64inmemory:
case float32inmemory:
return true;
}
}
//! Determine if type is floating point
bool ProtocolScaling::isTypeFloating(encodedtypes_t type) const
{
(void)type;
return false;
}
//! Determine if type is bitfield
bool ProtocolScaling::isTypeBitfield(inmemorytypes_t type) const
{
(void)type;
return false;
}
//! Determine if type is bitfield
bool ProtocolScaling::isTypeBitfield(encodedtypes_t type) const
{
switch(type)
{
default:
return false;
case longbitencoded:
case bitencoded:
return true;
}
}
//! Convert type to signed equivalent
ProtocolScaling::inmemorytypes_t ProtocolScaling::convertTypeToSigned(inmemorytypes_t type) const
{
switch(type)
{
default: return type;
case uint64inmemory: return int64inmemory;
case uint32inmemory: return int32inmemory;
case uint16inmemory: return int16inmemory;
case uint8inmemory: return int8inmemory;
}
}
//! Convert type to signed equivalent
ProtocolScaling::encodedtypes_t ProtocolScaling::convertTypeToSigned(encodedtypes_t type) const
{
switch(type)
{
default: return type;
case longbitencoded: return int64encoded;
case uint64encoded: return int64encoded;
case uint56encoded: return int56encoded;
case uint48encoded: return int48encoded;
case uint40encoded: return int40encoded;
case bitencoded: return int32encoded;
case uint32encoded: return int32encoded;
case uint24encoded: return int24encoded;
case uint16encoded: return int16encoded;
case uint8encoded: return int8encoded;
}
}
//! Convert type to unsigned equivalent
ProtocolScaling::inmemorytypes_t ProtocolScaling::convertTypeToUnsigned(inmemorytypes_t type) const
{
switch(type)
{
default: return type;
case int64inmemory: return uint64inmemory;
case int32inmemory: return uint32inmemory;
case int16inmemory: return uint16inmemory;
case int8inmemory: return uint8inmemory;
}
}
//! Convert type to unsigned equivalent
ProtocolScaling::encodedtypes_t ProtocolScaling::convertTypeToUnsigned(encodedtypes_t type) const
{
switch(type)
{
default: return type;
case int64encoded: return uint64encoded;
case int56encoded: return uint56encoded;
case int48encoded: return uint48encoded;
case int40encoded: return uint40encoded;
case int32encoded: return uint32encoded;
case int24encoded: return uint24encoded;
case int16encoded: return uint16encoded;
case int8encoded: return uint8encoded;
}
}
//! Determine the encoded length of type
int ProtocolScaling::typeLength(inmemorytypes_t type) const
{
switch(type)
{
default:
case float64inmemory:
case uint64inmemory:
case int64inmemory:
return 8;
case float32inmemory:
case uint32inmemory:
case int32inmemory:
return 4;
case uint16inmemory:
case int16inmemory:
return 2;
case uint8inmemory:
case int8inmemory:
return 1;
}
}
//! Determine the encoded length of type
int ProtocolScaling::typeLength(encodedtypes_t type) const
{
switch(type)
{
default:
case uint64encoded:
case int64encoded:
return 8;
case uint56encoded:
case int56encoded:
return 7;
case uint48encoded:
case int48encoded:
return 6;
case longbitencoded: // actual bitfield length is variable, it just has to be more than 32 bits
case uint40encoded:
case int40encoded:
return 5;
case uint32encoded:
case int32encoded:
return 4;
case uint24encoded:
case int24encoded:
return 3;
case uint16encoded:
case int16encoded:
return 2;
case bitencoded: // actual bitfield length is variable, it just has to be more than 0 bits
case uint8encoded:
case int8encoded:
return 1;
}
}
/*!
* Create an in-memory type from discrete choices. Bitfield types not supported
* \param issigned should be true to create a signed type
* \param isfloat should be true to create a floating point type
* \param length is the type length in bytes.
* \return The in-memory type enumeration
*/
ProtocolScaling::inmemorytypes_t ProtocolScaling::createInMemoryType(bool issigned, bool isfloat, int length) const
{
if(isfloat)
{
if((length > 4) && support.float64)
return float64inmemory;
else
return float32inmemory;
}
else if(issigned)
{
if((length > 4) && support.int64)
return int64inmemory;
else if(length > 3)
return int32inmemory;
else if(length == 2)
return int16inmemory;
else
return int8inmemory;
}
else
{
if((length > 4) && support.int64)
return uint64inmemory;
else if(length > 3)
return uint32inmemory;
else if(length == 2)
return uint16inmemory;
else
return uint8inmemory;
}
}// ProtocolScaling::createInMemoryType
/*!
* Create an encoded type from discrete choices. Bitfield and float types not supported
* \param issigned should be true to create a signed type
* \param length is the type length in bytes.
* \return The encoded type enumeration
*/
ProtocolScaling::encodedtypes_t ProtocolScaling::createEncodedType(bool issigned, int length) const
{
if(issigned)
{
if((length >= 8) && support.int64)
return int64encoded;
if((length >= 7) && support.int64)
return int56encoded;
if((length >= 6) && support.int64)
return int48encoded;
if((length >= 5) && support.int64)
return int40encoded;
else if(length >= 4)
return int32encoded;
else if(length >= 3)
return int24encoded;
else if(length >= 2)
return int16encoded;
else
return int8encoded;
}
else
{
if((length >= 8) && support.int64)
return uint64encoded;
if((length >= 7) && support.int64)
return uint56encoded;
if((length >= 6) && support.int64)
return uint48encoded;
if((length >= 5) && support.int64)
return uint40encoded;
else if(length >= 4)
return uint32encoded;
else if(length >= 3)
return uint24encoded;
else if(length >= 2)
return uint16encoded;
else
return uint8encoded;
}
}// ProtocolScaling::createEncodedType
//! Return the name in code of this type
std::string ProtocolScaling::typeName(inmemorytypes_t type) const
{
switch(type)
{
default: return "unknown";
case float64inmemory: return "double";
case uint64inmemory: return "uint64_t";
case int64inmemory: return "int64_t";
case float32inmemory: return "float";
case uint32inmemory: return "uint32_t";
case int32inmemory: return "int32_t";
case uint16inmemory: return "uint16_t";
case int16inmemory: return "int16_t";
case uint8inmemory: return "uint8_t";
case int8inmemory: return "int8_t";
}
}
//! Return the name in code of this type
std::string ProtocolScaling::typeName(encodedtypes_t type) const
{
switch(type)
{
default:
return "unknown";
case longbitencoded:
case uint64encoded:
case uint56encoded:
case uint48encoded:
case uint40encoded:
return "uint64_t";
case int64encoded:
case int56encoded:
case int48encoded:
case int40encoded:
return "int64_t";
case bitencoded:
return "unsigned int";
case uint32encoded:
case uint24encoded:
return "uint32_t";
case int32encoded:
case int24encoded:
return "int32_t";
case uint16encoded: return "uint16_t";
case int16encoded: return "int16_t";
case uint8encoded: return "uint8_t";
case int8encoded: return "int8_t";
}
}
//! Return the name in function signature of this type
std::string ProtocolScaling::typeSigName(inmemorytypes_t type) const
{
switch(type)
{
default: return "unknown";
case float64inmemory: return "float64";
case uint64inmemory: return "uint64";
case int64inmemory: return "int64";
case float32inmemory: return "float32";
case uint32inmemory: return "uint32";
case int32inmemory: return "int32";
case uint16inmemory: return "uint16";
case int16inmemory: return "int16";
case uint8inmemory: return "uint8";
case int8inmemory: return "int8";
}
}
//! Return the name in function signature of this type
std::string ProtocolScaling::typeSigName(encodedtypes_t type) const
{
switch(type)
{
default: return "unknown";
case longbitencoded: return "longBitfield";
case uint64encoded: return "uint64";
case int64encoded: return "int64";
case uint56encoded: return "uint56";
case int56encoded: return "int56";
case uint48encoded: return "uint48";
case int48encoded: return "int48";
case uint40encoded: return "uint40";
case int40encoded: return "int40";
case bitencoded: return "bitfield";
case uint32encoded: return "uint32";
case int32encoded: return "int32";
case uint24encoded: return "uint24";
case int24encoded: return "int24";
case uint16encoded: return "uint16";
case int16encoded: return "int16";
case uint8encoded: return "uint8";
case int8encoded: return "int8";
}
}
//! Determine if type is supported by this protocol
bool ProtocolScaling::isTypeSupported(inmemorytypes_t type) const
{
switch(type)
{
default:
return true;
case float64inmemory:
return support.float64;
case uint64inmemory:
case int64inmemory:
return support.int64;
}
}
//! Determine if type is supported by this protocol
bool ProtocolScaling::isTypeSupported(encodedtypes_t type) const
{
switch(type)
{
default:
return true;
case longbitencoded:
return support.bitfield && support.longbitfield && support.int64;
case uint64encoded:
case int64encoded:
case uint56encoded:
case int56encoded:
case uint48encoded:
case int48encoded:
case uint40encoded:
case int40encoded:
return support.int64;
case bitencoded:
return support.bitfield;
}
}
//! Determine if both types are supported by this protocol
bool ProtocolScaling::areTypesSupported(inmemorytypes_t inmemory, encodedtypes_t encoded) const
{
return isTypeSupported(inmemory) && isTypeSupported(encoded);
}
/*!
* Construct the protocol scaling object
*/
ProtocolScaling::ProtocolScaling(ProtocolSupport sup) :
header(sup),
source(sup),
support(sup)
{
/*
// for testing
support.int64 = false;
support.bitfield = false;
support.float64 = false;
*/
}
/*!
* Generate the source and header files for protocol scaling
* \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 ProtocolScaling::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 protocols caling
* \return true if the file is generated.
*/
bool ProtocolScaling::generateEncodeHeader(void)
{
header.setModuleNameAndPath("scaledencode", support.outputpath, support.language);
// Top level comment
header.write(
"/*!\n\
* \\file\n\
* scaledencode routines place scaled numbers into a byte stream.\n\
*\n\
* scaledencode routines place scaled values into a big or little endian byte\n\
* stream. The values can be any legitimate type (double, float, uint32_t,\n\
* uint16_t, uint8_t, int32_t, int16_t, int8_t), and are encoded as either a\n\
* unsigned or signed integer from 1 to 8 bytes in length. Unsigned encodings\n\
* allow the caller to specify a minimum and a maximum value, with the only\n\
* limitation that the maximum value must be more than the minimum. Signed\n\
* encodings only allow the caller to specify a maximum value which gives\n\
* maximum absolute value that can be encoded.\n\
*\n\
* An example encoding would be: take a float that represents speed in meters\n\
* per second and encode it in two bytes from -200 to 200 meters per second.\n\
* In that example the encoding function would be:\n\
*\n\
* float32ScaledTo2SignedBeBytes(speed, bytestream, &index, 200);\n\
*\n\
* This would scale the speed according to (32767/200), and copy the resulting\n\
* two bytes to bytestream[index] as a signed 16 bit number in big endian\n\
* order. This would result in a velocity resolution of 0.006 m/s.\n\
*\n\
* Another example encoding is: take a double that represents altitude in\n\
* meters and encode it in three bytes from -1000 to 49000 meters:\n\
*\n\
* float64ScaledTo3UnsignedLeBytes(alt, bytestream, &index, -1000, 49000);\n\
*\n\
* This would transform the altitude according to (alt *(16777215/50000) + 1000)\n\
* and copy the resulting three bytes to bytestream[index] as an unsigned 24\n\
* bit number in little endian order. This would result in an altitude\n\
* resolution of 0.003 meters.\n\
* \n\
* scaledencode does not include routines that increase the resolution of the\n\
* inmemory value. For example the function floatScaledTo5UnsignedBeBytes() does\n\
* not exist, because expanding a float to 5 bytes does not make any resolution\n\
* improvement over encoding it in 4 bytes. In general the encoded format\n\
* must be equal to or less than the number of bytes of the raw data.\n\
*\n");
// Document the protocol generation options
header.write(" * Code generation for this module was affected by these global flags:\n");
if(support.int64)
header.write(" * 64-bit integers are supported.\n");
else
header.write(" * 64-bit integers are not supported.\n");
if(support.bitfield && support.longbitfield && support.int64)
header.write(" * Normal and long bitfields are supported.\n");
else if(support.bitfield)
header.write(" * Normal bitfields are supported, long bitfields are not.\n");
else
header.write(" * Bitfields are not supported.\n");
if(support.float64)
header.write(" * Double precision floating points are supported.\n");
else
header.write(" * Double precision floating points are not supported.\n");
header.write(" */\n");
header.write("\n");
header.write("#define __STDC_CONSTANT_MACROS\n");
header.write("#include <stdint.h>\n");
bool ifdefopened = false;
// Iterate all inmemorys to all encodings.
for(int i = (int)float64inmemory; i <= (int)int8inmemory; i++)
{
inmemorytypes_t inmemorytype = (inmemorytypes_t)i;
for(int j = (int)longbitencoded; j <= (int)int8encoded; j++)
{
encodedtypes_t encodedtype = (encodedtypes_t)j;
// Key concept: the encoded cannot be larger than the inmemory
if(typeLength(encodedtype) > typeLength(inmemorytype))
continue;
// Key concept: the types must be supported in the protocol
if(!areTypesSupported(inmemorytype, encodedtype))
continue;
// If the inmemory or encoded type requires 64-bit support we have
// to protect it against compilers that cannot handle that
if((ifdefopened == false) && ((typeLength(encodedtype) > 4) || (typeLength(inmemorytype) > 4)))
{
ifdefopened = true;
header.write("\n#ifdef UINT64_MAX\n");
}
else if((ifdefopened == true) && (typeLength(encodedtype) <= 4) && (typeLength(inmemorytype) <= 4))
{
ifdefopened = false;
header.write("\n#endif // UINT64_MAX\n");
}
// big endian
header.write("\n");
header.write("//! " + briefEncodeComment(inmemorytype, encodedtype, true) + "\n");
header.write(encodeSignature(inmemorytype, encodedtype, true) + ";\n");
// little endian
if((typeLength(encodedtype) > 1) && !isTypeBitfield(encodedtype))
{
header.write("\n");
header.write("//! " + briefEncodeComment(inmemorytype, encodedtype, false) + "\n");
header.write(encodeSignature(inmemorytype, encodedtype, false) + ";\n");
}
}// for all encodeds
}// for all inmemorys
header.write("\n");
if(ifdefopened)
header.write("\n#endif // UINT64_MAX\n");
return header.flush();
}// ProtocolScaling::generateEncodeHeader
/*!
* Generate the source file for protocols caling
* \return true if the file is generated.
*/
bool ProtocolScaling::generateEncodeSource(void)
{
source.setModuleNameAndPath("scaledencode", support.outputpath, support.language);
source.writeIncludeDirective("fieldencode");
source.write("\n");
bool ifdefopened = false;
// Iterate all inmemorys to all encodings.
for(int i = (int)float64inmemory; i <= (int)int8inmemory; i++)
{
inmemorytypes_t inmemorytype = (inmemorytypes_t)i;
for(int j = (int)longbitencoded; j <= (int)int8encoded; j++)
{
encodedtypes_t encodedtype = (encodedtypes_t)j;
// Key concept: the encoded cannot be larger than the inmemory
if(typeLength(encodedtype) > typeLength(inmemorytype))
continue;
// Key concept: the types must be supported in the protocol
if(!areTypesSupported(inmemorytype, encodedtype))
continue;
// If the inmemory or encoded type requires 64-bit support we have
// to protect it against compilers that cannot handle that
if((ifdefopened == false) && ((typeLength(encodedtype) > 4) || (typeLength(inmemorytype) > 4)))
{
ifdefopened = true;
source.write("\n#ifdef UINT64_MAX\n");
}
else if((ifdefopened == true) && (typeLength(encodedtype) <= 4) && (typeLength(inmemorytype) <= 4))
{
ifdefopened = false;
source.write("\n#endif // UINT64_MAX\n");
}
// big endian
source.write("\n");
source.write(fullEncodeComment(inmemorytype, encodedtype, true) + "\n");
source.write(fullEncodeFunction(inmemorytype, encodedtype, true) + "\n");
// little endian
if((typeLength(encodedtype) > 1) && !isTypeBitfield(encodedtype))
{
source.write("\n");
source.write(fullEncodeComment(inmemorytype, encodedtype, false) + "\n");
source.write(fullEncodeFunction(inmemorytype, encodedtype, false) + "\n");
}
}// for all output byte counts
}// for all input types
source.write("\n");
if(ifdefopened)
source.write("\n#endif // UINT64_MAX\n");
return source.flush();
}// ProtocolScaling::generateEncodeSource
/*!
* Create the brief function comment, without doxygen decorations.
* \param inmemory is the type information for the inmemory (in-memory) data.
* \param encoded is the type information for the encoded (encoded) data.
* \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 ProtocolScaling::briefEncodeComment(inmemorytypes_t inmemory, encodedtypes_t encoded, bool bigendian) const
{
std::string scalingtype;
if(isTypeFloating(inmemory))
scalingtype = "floating point";
else
scalingtype = "integer";
if(isTypeBitfield(encoded))
{
if(typeLength(encoded) > 4)
return std::string("Scale a " + typeName(inmemory) + " using " + scalingtype + " scaling to the base integer type used for long bitfields.");
else
return std::string("Scale a " + typeName(inmemory) + " using " + scalingtype + " scaling to the base integer type used for bitfields.");
}
else
{
if(typeLength(encoded) == 1)
{
// No endian concerns if using only 1 byte
if(isTypeSigned(encoded))
return std::string("Encode a " + typeName(inmemory) + " on a byte stream by " + scalingtype + " scaling to fit in 1 signed byte.");
else
return std::string("Encode a " + typeName(inmemory) + " on a byte stream by " + scalingtype + " scaling to fit in 1 unsigned byte.");
}
else
{
std::string byteLength = std::to_string(typeLength(encoded));
std::string endian;
if(bigendian)
endian = "big";
else
endian = "little";
if(isTypeSigned(encoded))
return std::string("Encode a " + typeName(inmemory) + " on a byte stream by " + scalingtype + " scaling to fit in " + byteLength + " signed bytes in " + endian +" endian order.");
else
return std::string("Encode a " + typeName(inmemory) + " on a byte stream by " + scalingtype + " scaling to fit in " + byteLength + " unsigned bytes in " + endian + " endian order.");
}// If multi-byte
}
}// ProtocolScaling::briefEncodeComment
/*!
* Create the full encode function comment, with doxygen decorations
* \param inmemory is the type information for the inmemory (in-memory) data.
* \param encoded is the type information for the encoded (encoded) data.
* \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 ProtocolScaling::fullEncodeComment(inmemorytypes_t inmemory, encodedtypes_t encoded, bool bigendian) const
{
std::string comment= ("/*!\n");
if(isTypeBitfield(encoded))
{
comment += ProtocolParser::outputLongComment(" * ", briefEncodeComment(inmemory, encoded, bigendian)) + "\n";
comment += " * \\param value is the number to scale.\n";
comment += " * \\param min is the minimum value that can be encoded.\n";
comment += " * \\param scaler is multiplied by value to create the encoded integer.\n";
comment += " * \\param bits is the number of bits in the bitfield, used to limit the returned value.\n";
comment += " * \\return (value-min)*scaler.\n";
}
else
{
comment += ProtocolParser::outputLongComment(" * ", briefEncodeComment(inmemory, encoded, bigendian)) + "\n";
comment += " * \\param value is the number 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(typeLength(encoded)) + " when this function is complete.\n";
if(isTypeSigned(encoded))
comment += " * \\param scaler is multiplied by value to create the encoded integer: encoded = value*scaler.\n";
else
{
comment += " * \\param min is the minimum value that can be encoded.\n";
comment += " * \\param scaler is multiplied by value to create the encoded integer: encoded = (value-min)*scaler.\n";
}
}
comment += " */";
return comment;
}// ProtocolScaling::fullEncodeComment
/*!
* Create the one line function signature, without a trailing semicolon
* \param inmemory is the type information for the inmemory (in-memory) data.
* \param encoded is the type information for the encoded (encoded) data.
* \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 ProtocolScaling::encodeSignature(inmemorytypes_t inmemory, encodedtypes_t encoded, bool bigendian) const
{
if(isTypeBitfield(encoded))
{
if(typeLength(encoded) > 4)
return std::string(typeName(encoded) + " " + typeSigName(inmemory) + "ScaledToLongBitfield(" + typeName(inmemory) + " value, " + typeName(convertTypeToSigned(inmemory)) + " min, " + typeName(convertTypeToUnsigned(inmemory)) + " scaler, int bits)");
else
return std::string(typeName(encoded) + " " + typeSigName(inmemory) + "ScaledToBitfield(" + typeName(inmemory) + " value, " + typeName(convertTypeToSigned(inmemory)) + " min, " + typeName(convertTypeToUnsigned(inmemory)) + " scaler, int bits)");
}
else if(typeLength(encoded) == 1)
{
// No endian concerns if using only 1 byte
if(isTypeSigned(encoded))
return std::string("void " + typeSigName(inmemory) + "ScaledTo1SignedBytes(" + typeName(inmemory) + " value, uint8_t* bytes, int* index, " + typeName(convertTypeToUnsigned(inmemory)) + " scaler)");
else
return std::string("void " + typeSigName(inmemory) + "ScaledTo1UnsignedBytes(" + typeName(inmemory) + " value, uint8_t* bytes, int* index, " + typeName(convertTypeToSigned(inmemory)) + " min, " + typeName(convertTypeToUnsigned(inmemory)) + " scaler)");
}
else
{
std::string byteLength = std::to_string(typeLength(encoded));
std::string endian;
if(bigendian)
endian = "Be";
else
endian = "Le";
if(isTypeSigned(encoded))
return std::string("void " + typeSigName(inmemory) + "ScaledTo" + byteLength + "Signed" + endian + "Bytes(" + typeName(inmemory) + " value, uint8_t* bytes, int* index, " + typeName(convertTypeToUnsigned(inmemory)) + " scaler)");
else
return std::string("void " + typeSigName(inmemory) + "ScaledTo" + byteLength + "Unsigned" + endian + "Bytes(" + typeName(inmemory) + " value, uint8_t* bytes, int* index, " + typeName(convertTypeToSigned(inmemory)) + " min, " + typeName(convertTypeToUnsigned(inmemory)) + " scaler)");
}// If multi-byte
}// ProtocolScaling::encodeSignature
/*!
* Generate the full function output, excluding the comment
* \param inmemory is the type information for the inmemory (in-memory) data.
* \param encoded is the type information for the encoded (encoded) data.
* \param bigendian should be true if the function outputs big endian byte order.
* \return the function as a string
*/
std::string ProtocolScaling::fullEncodeFunction(inmemorytypes_t inmemory, encodedtypes_t encoded, bool bigendian) const
{
if(isTypeBitfield(encoded))
return fullBitfieldEncodeFunction(inmemory, encoded, bigendian);
else if(isTypeFloating(inmemory))
return fullFloatEncodeFunction(inmemory, encoded, bigendian);
else
return fullIntegerEncodeFunction(inmemory, encoded, bigendian);
}
/*!
* Generate the full bitfield scaling function output, excluding the comment
* \param inmemory is the type information for the inmemory (in-memory) data.
* \param encoded is the type information for the encoded (encoded) data.
* \param bigendian should be true if the function outputs big endian byte order.
* \return the function as a string
*/
std::string ProtocolScaling::fullBitfieldEncodeFunction(inmemorytypes_t inmemory, encodedtypes_t encoded, bool bigendian) const
{
std::string constantone;
if(typeLength(encoded) > 4)
constantone = "0x1ull";
else if(typeLength(encoded) > 2)
constantone = "0x1ul";
else
constantone = "0x1u";
std::string function = encodeSignature(inmemory, encoded, bigendian) + "\n";
function += "{\n";
function += " // The largest integer the bitfield can hold\n";
function += " " + typeName(encoded) + " max = (" + constantone + " << bits) - 1;\n";
function += "\n";
if(isTypeFloating(inmemory))
{
function += " // Protect from underflow\n";
function += " if(value < min)\n";
function += " return 0;\n";
function += "\n";
function += " // Scale the number\n";
function += " value = (value - min)*scaler;\n";
function += "\n";
function += " // Protect from overflow\n";
function += " if(value > max)\n";
function += " return max;\n";
function += "\n";
if(typeLength(inmemory) > 4)
{
function += " // Account for fractional truncation\n";
function += " return (" + typeName(encoded) + ")(value + 0.5);\n";
}// if inmemory data is double precision floating point
else
{
function += " // Account for fractional truncation\n";
function += " return (" + typeName(encoded) + ")(value + 0.5f);\n";
}// else if inmemory data is single precision floating point
}// If in-memory type is floating point
else
{
function += " // Scale the number\n";
function += " " + typeName(encoded) + " number = (" + typeName(encoded) + ")((value - min)*scaler);\n";
function += "\n";
function += " // Protect from underflow\n";
function += " if(((" + typeName(convertTypeToSigned(encoded)) + ")value) < min)\n";
function += " return 0;\n";
function += "\n";
function += " // Protect from overflow\n";
function += " if(number > max)\n";
function += " return max;\n";
function += "\n";
function += " return number;\n";
}// else if inmemory type is integer
function += "}\n";
return function;
}// ProtocolScaling::fullBitfieldEncodeFunction
/*!
* Generate the full floating point scaling function output, excluding the comment
* \param inmemory is the type information for the inmemory (in-memory) data.
* \param encoded is the type information for the encoded (encoded) data.
* \param bigendian should be true if the function outputs big endian byte order.
* \return the function as a string
*/
std::string ProtocolScaling::fullFloatEncodeFunction(inmemorytypes_t inmemory, encodedtypes_t encoded, bool bigendian) const
{
std::string function = encodeSignature(inmemory, encoded, bigendian) + "\n";
std::string endian;
if(typeLength(encoded) > 1)
{
if(bigendian)
endian = "Be";
else
endian = "Le";
}
std::string bitCount = std::to_string(typeLength(encoded)*8);
std::string halfFraction;
if(typeLength(inmemory) > 4)
halfFraction = "0.5";
else
halfFraction = "0.5f";
function += "{\n";
function += " // scale the number\n";
if(isTypeSigned(encoded))
{
std::string max;
std::string min;