-
Notifications
You must be signed in to change notification settings - Fork 16
/
protocolbitfield.cpp
578 lines (503 loc) · 24.6 KB
/
protocolbitfield.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
#include "protocolbitfield.h"
#include <iomanip>
#include <sstream>
void ProtocolBitfield::generatetest(ProtocolSupport support)
{
if(!support.bitfieldtest)
return;
ProtocolHeaderFile header(support);
ProtocolSourceFile source(support);
// We assume that the ProtocolParser has already generated the encode/decode
// functions in the bitfieldtest module, we just need to exercise them.
// Prototype for testing bitfields, note the files have already been flushed, so we are appending
header.setModuleNameAndPath("bitfieldtest", support.outputpath, support.language);
header.makeLineSeparator();
header.write("//! Test the bit fields\n");
header.write("int testBitfield(void);\n");
header.write("\n");
header.flush();
// Now the source code
source.setModuleNameAndPath("bitfieldtest", support.outputpath, support.language);
source.makeLineSeparator();
source.writeIncludeDirective("string.h", std::string(), true);
source.writeIncludeDirective("limits.h", std::string(), true);
source.writeIncludeDirective("math.h", std::string(), true);
if(support.language == ProtocolSupport::c_language)
{
source.write("/*!\n");
source.write(" * Test the bit field encode and decode logic\n");
source.write(" * \\return 1 if the test passes, else 0\n");
source.write(" */\n");
source.write("int testBitfield(void)\n");
source.write("{\n");
source.write(" bitfieldtest" + support.typeSuffix + " test = {1, 2, 12, 0xABC, 0, 3, 4, 0xC87654321ULL};\n");
source.write(" bitfieldtest2" + support.typeSuffix + " test2 = {1, 2, 12, 0xABC, 0, 3, 4, 0xC87654321ULL};\n");
source.write("\n");
source.write(" bitfieldtest3" + support.typeSuffix + " test3 = {12.5f, 12.5f, 3.14159, 0, 0, 50};\n");
source.write("\n");
source.write(" uint8_t data[20];\n");
source.write(" int index = 0;\n");
source.write("\n");
source.write(" // Fill the data with 1s so we can be sure the encoder sets all bits correctly\n");
source.write(" memset(data, UCHAR_MAX, sizeof(data));\n");
source.write("\n");
source.write(" encodebitfieldtest_t(data, &index, &test);\n");
source.write("\n");
source.write(" // Clear the in-memory data so we can be sure the decoder sets all bits correctly\n");
source.write(" memset(&test, 0, sizeof(test));\n");
source.write("\n");
source.write(" index = 0;\n");
source.write(" if(!decodebitfieldtest_t(data, &index, &test))\n");
source.write(" return 0;\n");
source.write("\n");
source.write(" if(test.test1 != 1)\n");
source.write(" return 0;\n");
source.write(" else if(test.test2 != 2)\n");
source.write(" return 0;\n");
source.write(" else if(test.test3 != 7) // This value was overflow, 7 is the max\n");
source.write(" return 0;\n");
source.write(" else if(test.test12 != 0xABC)\n");
source.write(" return 0;\n");
source.write(" else if(test.testa != 0)\n");
source.write(" return 0;\n");
source.write(" else if(test.testb != 3)\n");
source.write(" return 0;\n");
source.write(" else if(test.testc != 4)\n");
source.write(" return 0;\n");
source.write(" else if(test.testd != 0xC87654321ULL)\n");
source.write(" return 0;\n");
source.write("\n");
source.write(" // Fill the data with 1s so we can be sure the encoder sets all bits correctly\n");
source.write(" memset(data, UCHAR_MAX, sizeof(data));\n");
source.write("\n");
source.write(" index = 0;\n");
source.write(" encodebitfieldtest2_t(data, &index, &test2);\n");
source.write("\n");
source.write(" // Clear the in-memory data so we can be sure the decoder sets all bits correctly\n");
source.write(" memset(&test2, 0, sizeof(test2));\n");
source.write("\n");
source.write(" index = 0;\n");
source.write(" if(!decodebitfieldtest2_t(data, &index, &test2))\n");
source.write(" return 0;\n");
source.write("\n");
source.write(" if(test2.test1 != 1)\n");
source.write(" return 0;\n");
source.write(" else if(test2.test2 != 2)\n");
source.write(" return 0;\n");
source.write(" else if(test2.test3 != 7) // This value was overflow, 7 is the max\n");
source.write(" return 0;\n");
source.write(" else if(test2.test12 != 0xABC)\n");
source.write(" return 0;\n");
source.write(" else if(test2.testa != 0)\n");
source.write(" return 0;\n");
source.write(" else if(test2.testb != 3)\n");
source.write(" return 0;\n");
source.write(" else if(test2.testc != 4)\n");
source.write(" return 0;\n");
source.write(" else if(test2.testd != 0xC87654321ULL)\n");
source.write(" return 0;\n");
source.write("\n");
source.write(" // Fill the data with 1s so we can be sure the encoder sets all bits correctly\n");
source.write(" memset(data, UCHAR_MAX, sizeof(data));\n");
source.write("\n");
source.write(" index = 0;\n");
source.write(" encodebitfieldtest3_t(data, &index, &test3);\n");
source.write("\n");
source.write(" // Clear the in-memory data so we can be sure the decoder sets all bits correctly\n");
source.write(" memset(&test3, 0, sizeof(test3));\n");
source.write("\n");
source.write(" index = 0;\n");
source.write(" if(!decodebitfieldtest3_t(data, &index, &test3))\n");
source.write(" return 0;\n");
source.write("\n");
source.write(" if(fabs(test3.test1 - 25.0f) > 1.0/200.0) // underflow, min is 25\n");
source.write(" return 0;\n");
source.write(" else if(fabs(test3.test2 - 12.5f) > 1.0/100.0)\n");
source.write(" return 0;\n");
source.write(" else if(fabs(test3.test12 - 3.14159) > 1.0/1024.0)\n");
source.write(" return 0;\n");
source.write(" else if(test3.testa != 1)\n");
source.write(" return 0;\n");
source.write(" else if(fabs(test3.testc - 3.1415926535898) > 1.0/200.0)\n");
source.write(" return 0;\n");
source.write(" else if(test3.testd != 0)\n");
source.write(" return 0;\n");
source.write(" else\n");
source.write(" return 1;\n");
source.write("\n");
source.write("}// testBitfield\n");
}
else if(support.language == ProtocolSupport::cpp_language)
{
source.write("/*!\n");
source.write(" * Test the bit field encode and decode logic\n");
source.write(" * \\return 1 if the test passes, else 0\n");
source.write(" */\n");
source.write("int testBitfield(void)\n");
source.write("{\n");
source.write(" bitfieldtest" + support.typeSuffix + " test;\n");
source.write(" bitfieldtest2" + support.typeSuffix + " test2;\n");
source.write(" bitfieldtest3" + support.typeSuffix + " test3;\n");
source.write(" uint8_t data[20];\n");
source.write(" int index = 0;\n");
source.write("\n");
source.write(" // Fill the data with 1s so we can be sure the encoder sets all bits correctly\n");
source.write(" memset(data, UCHAR_MAX, sizeof(data));\n");
source.write("\n");
source.write(" test.encode(data, &index);\n");
source.write("\n");
source.write(" // Clear the in-memory data so we can be sure the decoder sets all bits correctly\n");
source.write(" memset(&test, 0, sizeof(test));\n");
source.write("\n");
source.write(" index = 0;\n");
source.write(" if(!test.decode(data, &index))\n");
source.write(" return 0;\n");
source.write("\n");
source.write(" if(test.test1 != 1)\n");
source.write(" return 0;\n");
source.write(" else if(test.test2 != 2)\n");
source.write(" return 0;\n");
source.write(" else if(test.test3 != 7) // This value was overflow, 7 is the max\n");
source.write(" return 0;\n");
source.write(" else if(test.test12 != 0xABC)\n");
source.write(" return 0;\n");
source.write(" else if(test.testa != 0)\n");
source.write(" return 0;\n");
source.write(" else if(test.testb != 3)\n");
source.write(" return 0;\n");
source.write(" else if(test.testc != 4)\n");
source.write(" return 0;\n");
source.write(" else if(test.testd != 0xC87654321ULL)\n");
source.write(" return 0;\n");
source.write("\n");
source.write(" // Fill the data with 1s so we can be sure the encoder sets all bits correctly\n");
source.write(" memset(data, UCHAR_MAX, sizeof(data));\n");
source.write("\n");
source.write(" index = 0;\n");
source.write(" test2.encode(data, &index);\n");
source.write("\n");
source.write(" // Clear the in-memory data so we can be sure the decoder sets all bits correctly\n");
source.write(" memset(&test2, 0, sizeof(test2));\n");
source.write("\n");
source.write(" index = 0;\n");
source.write(" if(!test2.decode(data, &index))\n");
source.write(" return 0;\n");
source.write("\n");
source.write(" if(test2.test1 != 1)\n");
source.write(" return 0;\n");
source.write(" else if(test2.test2 != 2)\n");
source.write(" return 0;\n");
source.write(" else if(test2.test3 != 7) // This value was overflow, 7 is the max\n");
source.write(" return 0;\n");
source.write(" else if(test2.test12 != 0xABC)\n");
source.write(" return 0;\n");
source.write(" else if(test2.testa != 0)\n");
source.write(" return 0;\n");
source.write(" else if(test2.testb != 3)\n");
source.write(" return 0;\n");
source.write(" else if(test2.testc != 4)\n");
source.write(" return 0;\n");
source.write(" else if(test2.testd != 0xC87654321ULL)\n");
source.write(" return 0;\n");
source.write("\n");
source.write(" // Fill the data with 1s so we can be sure the encoder sets all bits correctly\n");
source.write(" memset(data, UCHAR_MAX, sizeof(data));\n");
source.write("\n");
source.write(" index = 0;\n");
source.write(" test3.encode(data, &index);\n");
source.write("\n");
source.write(" // Clear the in-memory data so we can be sure the decoder sets all bits correctly\n");
source.write(" memset(&test3, 0, sizeof(test3));\n");
source.write("\n");
source.write(" index = 0;\n");
source.write(" if(!test3.decode(data, &index))\n");
source.write(" return 0;\n");
source.write("\n");
source.write(" if(fabs(test3.test1 - 25.0f) > 1.0/200.0) // underflow, min is 25\n");
source.write(" return 0;\n");
source.write(" else if(fabs(test3.test2 - 12.5f) > 1.0/100.0)\n");
source.write(" return 0;\n");
source.write(" else if(fabs(test3.test12 - 3.14159) > 1.0/1024.0)\n");
source.write(" return 0;\n");
source.write(" else if(test3.testa != 1)\n");
source.write(" return 0;\n");
source.write(" else if(fabs(test3.testc - 3.1415926535898) > 1.0/200.0)\n");
source.write(" return 0;\n");
source.write(" else if(test3.testd != 0)\n");
source.write(" return 0;\n");
source.write(" else\n");
source.write(" return 1;\n");
source.write("\n");
source.write("}// testBitfield\n");
}// else if c++
source.flush();
}// ProtocolBitfield::generatetest
/*!
* Compute the maximum value of a field
* \param numbits is the bit width of the field
* \return the maximum value, which will be zero if numbits is zero
*/
uint64_t ProtocolBitfield::maxvalueoffield(int numbits)
{
// Do not omit the "ull" or you will be limited to shifting the number of bits in an int
return (0x1ull << numbits) - 1;
}
/*!
* Get the decode string for a bitfield, which may or may not cross byte boundaries
* \param spacing is the spacing at the start of each line
* \param argument is the string describing the field of bits
* \param cast is the string used to cast to the arguments type. This can be empty
* \param dataname is the string describing the array of bytes
* \param dataindex is the string describing the index into the array of bytes
* \param bitcount is the current bitcount of this field
* \param numbits is the number of bits in this field
* \return the string that is the decoding code
*/
std::string ProtocolBitfield::getDecodeString(const std::string& spacing, const std::string& argument, const std::string& cast, const std::string& dataname, const std::string& dataindex, int bitcount, int numbits)
{
if((numbits > 1) && (((bitcount % 8) + numbits) > 8))
return getComplexDecodeString(spacing, argument, dataname, dataindex, bitcount, numbits);
else
return spacing + argument + " = " + cast + getInnerDecodeString(dataname, dataindex, bitcount, numbits) + ";\n";
}// ProtocolBitfield::getDecodeString
/*!
* Get the inner string that does a simple bitfield decode
* \param dataname is the string describing the array of bytes
* \param dataindex is the string describing the index into the array of bytes
* \param bitcount is the current bitcount of this field
* \param numbits is the number of bits in this field
* \return the string that is the decoding code
*/
std::string ProtocolBitfield::getInnerDecodeString(const std::string& dataname, const std::string& dataindex, int bitcount, int numbits)
{
// This is the easiest case, we can just decode it directly
std::string rightshift;
std::string offset;
std::string mask;
// Don't do shifting by zero bits
int right = 8 - (bitcount%8 + numbits);
if(right > 0)
rightshift = " >> " + std::to_string(8 - (bitcount%8 + numbits));
// This mask protects against any other bits we don't want. We don't
// need the mask if we are grabbing the most significant bit of this byte
if((numbits + right) < 8)
{
// This exists because of a bug in GCC which prevents this from working correctly:
// mask = " & 0x" + toUpper((std::stringstream() << std::hex << maxvalueoffield(numbits)).str());
std::stringstream stream;
stream << std::hex;
stream << maxvalueoffield(numbits);
mask = " & 0x" + toUpper(stream.str());
}
// The value of the bit count after moving all the bits
int bitoffset = bitcount + numbits;
// The byte offset of this bitfield
int byteoffset = (bitoffset-1) >> 3;
if(byteoffset > 0)
offset = " + " + std::to_string(byteoffset);
if(mask.empty() && rightshift.empty())
return dataname + "[" + dataindex + offset + "]";
else if(mask.empty())
return "(" + dataname + "[" + dataindex + offset + "]" + rightshift + ")";
else
return "((" + dataname + "[" + dataindex + offset + "]" + rightshift + ")" + mask + ")";
}
/*!
* Get the decode string for a complex bitfield (crossing byte boundaries)
* \param spacing is the spacing at the start of each line
* \param argument is the string describing the field of bits
* \param dataname is the string describing the array of bytes
* \param dataindex is the string describing the index into the array of bytes
* \param bitcount is the current bitcount of this field
* \param numbits is the number of bits in this field
* \return the string that is the encoding code
*/
std::string ProtocolBitfield::getComplexDecodeString(const std::string& spacing, const std::string& argument, const std::string& dataname, const std::string& dataindex, int bitcount, int numbits)
{
std::string output;
// Bits are encoded left-to-right from most-significant to least-significant.
// The most significant bits are moved first, as that allows us to keep the
// shifts to 8 bits or less, which some compilers need.
// The byteoffset of the most significant block of 8 bits
int byteoffset = bitcount>>3;
int leadingbits = (8 - (bitcount % 8)%8);
// First get any most significant bits that are partially encoded in the previous byte
if(leadingbits)
{
std::string offset;
if(byteoffset)
offset = " + " + std::to_string(byteoffset);
std::stringstream stream;
stream << std::hex;
stream << maxvalueoffield(leadingbits);
// This mask protects against any other bits we don't want
std::string mask = " & 0x" + toUpper(stream.str());
output += spacing + argument + " = (" + dataname + "[" + dataindex + offset + "]" + mask + ");\n\n";
// These bits are done
numbits -= leadingbits;
byteoffset++;
// Shift the argument up for the next decode
if(numbits >= 8)
output += spacing + argument + " <<= 8;\n";
else if(numbits)
output += spacing + argument + " <<= " + std::to_string(numbits) + ";\n";
}
while(numbits >= 8)
{
std::string offset;
if(byteoffset)
offset = " + " + std::to_string(byteoffset);
// Make space in the argument for the next most significant 8 bits
output += spacing + argument + " |= " + dataname + "[" + dataindex + offset + "];\n\n";
byteoffset++;
numbits -= 8;
// Shift the argument up for the next decode
if(numbits >= 8)
output += spacing + argument + " <<= 8;\n";
else if(numbits)
output += spacing + argument + " <<= " + std::to_string(numbits) + ";\n";
}
// Handle the final remainder bits
if(numbits)
{
std::string offset;
if(byteoffset > 0)
offset = " + " + std::to_string(byteoffset);
// The least significant bits of value, encoded in the most
// significant bits of the last byte we are going to use. By
// Definition we don't need a mask because we are grabbing the
// most significant bit in this case.
output += spacing + argument + " |= (" + dataname + "[" + dataindex + offset + "] >> " + std::to_string(8-numbits) + ");\n\n";
}
return output;
}// ProtocolBitfield::getComplexDecodeString
/*!
* Get the encode string for a bitfield, which may or may not cross byte boundaries
* \param spacing is the spacing at the start of each line
* \param argument is the string describing the field of bits
* \param dataname is the string describing the array of bytes
* \param dataindex is the string describing the index into the array of bytes
* \param bitcount is the current bitcount of this field
* \param numbits is the number of bits in this field
* \return the string that is the encoding code
*/
std::string ProtocolBitfield::getEncodeString(const std::string& spacing, const std::string& argument, const std::string& dataname, const std::string& dataindex, int bitcount, int numbits)
{
if((numbits > 1) && (((bitcount % 8) + numbits) > 8))
return getComplexEncodeString(spacing, argument, dataname, dataindex, bitcount, numbits);
else
{
// This is the easiest case, we can just encode it directly
std::string leftshift;
std::string offset;
// Don't do any shifting by zero bits
int left = 8 - (bitcount%8 + numbits);
if(left > 0)
leftshift = " << " + std::to_string(8 - (bitcount%8 + numbits));
// The value of the bit count after moving all the bits
int bitoffset = bitcount + numbits;
// The byte offset of this bitfield
int byteoffset = (bitoffset-1) >> 3;
if(byteoffset > 0)
offset = " + " + std::to_string(byteoffset);
// If this is the first bit of this byte then we assign rather than or-equal
if((bitcount%8) == 0)
{
// If the argument is the string "0" then we don't need to be shifting
if(argument == "0")
return spacing + dataname + "[" + dataindex + offset + "] = 0;\n";
else
return spacing + dataname + "[" + dataindex + offset + "] = (uint8_t)" + argument + leftshift + ";\n";
}
else
{
// If the thing we are or-equaling is the string "0" then we can just skip the entire line
if(argument == "0")
return std::string();
else
return spacing + dataname + "[" + dataindex + offset + "] |= (uint8_t)" + argument + leftshift + ";\n";
}
}
}// ProtocolBitfield::getEncodeString
/*!
* Get the encode string for a complex bitfield (crossing byte boundaries)
* \param spacing is the spacing at the start of each line
* \param argument is the string describing the field of bits
* \param dataname is the string describing the array of bytes
* \param dataindex is the string describing the index into the array of bytes
* \param bitcount is the current bitcount of this field
* \param numbits is the number of bits in this field
* \return the string that is the encoding code
*/
std::string ProtocolBitfield::getComplexEncodeString(const std::string& spacing, const std::string& argument, const std::string& dataname, const std::string& dataindex, int bitcount, int numbits)
{
std::string output;
// Bits are encoded left-to-right from most-significant to least-significant.
// The least significant bits are moved first, as that allows us to keep the
// shifts to 8 bits or less, which some compilers need.
// The value of the bit count after moving all the bits
int bitoffset = bitcount + numbits;
// The byte offset of the least significant block of 8 bits to move
int byteoffset = (bitoffset-1) >> 3;
// The remainder bits (modulo 8) which are the least significant bits to move
int remainder = bitoffset & 0x07;
if(remainder)
{
std::string offset;
if(byteoffset > 0)
offset = " + " + std::to_string(byteoffset);
if(argument == "0")
{
// If the argument is the constant string "0" then shifting is not needed
output += spacing + dataname + "[" + dataindex + offset + "] = 0;\n\n";
}
else
{
// The least significant bits of value, encoded in the most
// significant bits of the last byte we are going to use.
output += spacing + dataname + "[" + dataindex + offset + "] = (uint8_t)("+argument+" << "+std::to_string(8-remainder)+");\n\n";
}
// Discard these bits, we have encoded them
numbits -= remainder;
// Shift the field down for the next byte of bits
if((numbits > 0) && (argument != "0"))
output += spacing + argument + " >>= " + std::to_string(remainder) + ";\n";
remainder = 0;
byteoffset--;
}
// Now aligned on a byte boundary, move full bytes
while(numbits >= 8)
{
std::string offset;
if(byteoffset > 0)
offset = " + " + std::to_string(byteoffset);
if(argument == "0")
{
// If the argument is the constant string "0" then shifting is not needed
output += spacing + dataname + "[" + dataindex + offset + "] = 0;\n\n";
byteoffset--;
numbits -= 8;
}
else
{
output += spacing + dataname + "[" + dataindex + offset + "] = (uint8_t)"+argument+";\n\n";
byteoffset--;
numbits -= 8;
// Shift the field down for the next byte of bits
if(numbits > 0)
output += spacing + argument + " >>= 8;\n";
}
}// while still bits to encode
// Finally finish any remaining most significant bits. There are
// some valid bits in the most signficant bit locations.
if(numbits > 0)
{
std::string offset;
if(byteoffset > 0)
offset = " + " + std::to_string(byteoffset);
// If the thing we are or-equaling is the string "0" then we can just skip the entire line
if(argument != "0")
output += spacing + dataname + "[" + dataindex + offset + "] |= (uint8_t)" + argument + ";\n";
}
return output;
}// ProtocolBitfield::getEncodeString