Repository navigation
Expand file tree
/
Copy pathField.cpp
More file actions
349 lines (314 loc) · 9.44 KB
/
Copy pathField.cpp
File metadata and controls
349 lines (314 loc) · 9.44 KB
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
/*!
* @file Field.cpp
* @brief Implementations of the functions defined in \a Field.h
* @author lhm
* @date 29/08/2019
*/
#include <bitset>
#include <cmath>
#include <limits.h>
#include "Logger.h"
#include "Header.h"
#include "Exception.h"
#include "Conf_format.h"
#include "time_util.h"
#include "Field.h"
#include "Field_size.h"
#include "Field_time.h"
#include "Field_id.h"
namespace ModGen {
using namespace std;
map<Field::_ENDIAN, string> Field::endianString = {
{_ENDIAN::_LITTLE_ENDIAN, "LITTLE_ENDIAN" },
{_ENDIAN::_BIG_ENDIAN, "BIG_ENDIAN" },
{_ENDIAN::_UNDEFINED, "UNDEFINED" }
};
////////////////////////////////////////////////////////////////////////
vector<uint8_t> FieldHelper::int32ToChar(int32_t &p_int)
{
vector<uint8_t> l_return(4);
for (int i = 0; i < 4; i++)
l_return[3 - i] = (p_int >> (i * 8));
return l_return;
}
////////////////////////////////////////////////////////////////////////
uint32_t FieldHelper::getMSB(int64_t p_int)
{
uint32_t l_return = 0;
if(p_int != 0)
{
while (p_int != 0)
{
p_int /= 2;
l_return++;
}
}
return l_return;
}
////////////////////////////////////////////////////////////////////////
void FieldHelper::addBin(uint8_t p_value,
uint32_t p_pos,
vector<uint8_t>& p_mesg)
{
if( p_pos <= 8*p_mesg.size()-1 )
{
p_mesg[p_pos/8] |= (p_value << (7 - p_pos%8));
}
}
////////////////////////////////////////////////////////////////////////
void FieldHelper::printBits(vector<uint8_t>& p_mesg,
const string& p_optTitle)
{
string l_return;
for(uint32_t i = 0; i < p_mesg.size(); i++)
{
l_return += std::bitset<8>(p_mesg[i]).to_string() + " ";
}
DEBUG(p_optTitle + l_return);
}
////////////////////////////////////////////////////////////////////////
template <typename T> T FieldHelper::swap_endian(T p_value)
{
static_assert (CHAR_BIT == 8, "CHAR_BIT != 8");
union
{
T val;
uint8_t u8[sizeof(T)];
} source, dest;
source.val = p_value;
for (size_t k = 0; k < sizeof(T); k++)
{
dest.u8[k] = source.u8[sizeof(T) - k - 1];
}
return dest.val;
}
////////////////////////////////////////////////////////////////////////
Field* Field_Creator::Create(const string& p_name)
{
if (p_name.compare(Field_format::balise) == 0)
{
// DEBUG("Creating new simple Field");
return new Field;
}
else if(p_name.compare(Field_size_format::balise) == 0)
{
// DEBUG("Creating new size Field");
return new Field_size;
}
else if(p_name.compare(Field_time_format::balise) == 0)
{
// DEBUG("Creating new time Field");
return new Field_time;
}
else if(p_name.compare(Field_id_format::balise) == 0)
{
// DEBUG("Creating new id Field");
return new Field_id;
}
throw Exception::ParsingFileParamError<Header>(p_name);
}
////////////////////////////////////////////////////////////////////////
Field::Field():
name(),
pos(-1),
size(-1),
value(-1),
endianness(_ENDIAN::_UNDEFINED),
swap(false),
invert(false)
{}
////////////////////////////////////////////////////////////////////////
Field::Field(const Field& p_other) :
name(p_other.name),
pos(p_other.pos),
size(p_other.size),
value(p_other.value),
endianness(p_other.endianness),
swap(p_other.swap),
invert(p_other.invert)
{}
////////////////////////////////////////////////////////////////////////
Field& Field::operator = (const Field &p_other)
{
name = p_other.name;
pos = p_other.pos;
size = p_other.size;
value = p_other.value;
endianness = p_other.endianness;
swap = p_other.swap;
invert = p_other.invert;
return *this;
}
////////////////////////////////////////////////////////////////////////
Field::~Field()
{}
////////////////////////////////////////////////////////////////////////
bool Field::isValid() const
{
return ( !name.empty() &&
pos != -1 &&
size != -1 &&
value != -1 &&
endianness != _UNDEFINED
);
}
////////////////////////////////////////////////////////////////////////
void Field::setParam(const string& p_name,
const string& p_value)
{
if (p_name.compare(Field_format::name) == 0)
{
name = p_value;
}
else if(p_name.compare(Field_format::pos) == 0)
{
pos = static_cast<int32_t>(stoi(p_value));
}
else if(p_name.compare(Field_format::size) == 0)
{
size = static_cast<int32_t>(stoi(p_value));
}
else if(p_name.compare(Field_format::value) == 0)
{
value = static_cast<int32_t>(stoi(p_value));
}
else if(p_name.compare(Field_format::endian) == 0)
{
if(p_value.compare("LE") == 0)
{
endianness = _LITTLE_ENDIAN;
}
else if(p_value.compare("BE") == 0)
{
endianness = _BIG_ENDIAN;
}
else
{
throw Exception::UnimplementedElement<_ENDIAN>(p_value);
}
}
else if(p_name.compare(Field_format::swap) == 0)
{
if(p_value.compare("TRUE") == 0)
{
swap = true;
}
else if(p_value.compare("FALSE") == 0)
{
swap = false;
}
else
{
throw Exception::UnimplementedElement<bool>(p_value);
}
}
else if(p_name.compare(Field_format::invert) == 0)
{
if(p_value.compare("TRUE") == 0)
{
invert = true;
}
else if(p_value.compare("FALSE") == 0)
{
invert = false;
}
else
{
throw Exception::UnimplementedElement<bool>(p_value);
}
}
else
{
throw Exception::ParsingFileParamError<Field>(p_name);
}
}
////////////////////////////////////////////////////////////////////////
string Field::getDesc() const
{
return (
"Field " + name +
"\n\t-pos = " + to_string(pos) +
"\n\t-size = " + to_string(size) +
"\n\t-value = " + to_string(value) +
"\n\t-endianness = " + endianString[endianness] +
"\n\t-swap = " + (swap ? "true":"false") +
"\n\t-invert = " + (invert ? "true":"false") + "\n"
);
}
////////////////////////////////////////////////////////////////////////
void Field::addToMessage(vector<uint8_t>& p_mesg)
{
// Change endianness if needed
switch(endianness)
{
case _BIG_ENDIAN:
value = FieldHelper::swap_endian<int64_t>(value);
break;
case _LITTLE_ENDIAN:
break;
default:
throw Exception::UnimplementedElement<_ENDIAN>(_UNDEFINED);
}
// Swap the value by 16-bit words if needed
if(swap)
{
value = (value & 0x0000FFFF) << 16 | (value & 0xFFFF0000) >> 16;
}
// vector<uint8_t> l_mesgBytes = FieldHelper::int32ToChar(value);
uint32_t l_msbValue = FieldHelper::getMSB(value);
int l_startInd = static_cast<int>(l_msbValue);
int l_inv = -1;
//FieldHelper::printBits(l_mesgBytes, "Value of the field " + name + ": ");
// Requiered size is over the specified value bit size representation
if( static_cast<uint32_t>(size) > l_msbValue )
{
if(Logger::isDebugEnabled())
{
DEBUG("Field ID " + name + ": The specified size (" + to_string(size)
+ " bits) is over the value (" + to_string(value)
+ ") bits representation number ("+ to_string(l_msbValue) + " bits)" + "- the value has been filled "
"with 0s to fit the specified size" );
}
l_startInd = size;
}
// Requiered size is under the specified value bit size representation (troncate)
else if (static_cast<uint32_t>(size) < l_msbValue )
{
if(Logger::isDebugEnabled())
{
DEBUG("Field ID " + name + ": The specified size (" + to_string(size)
+ " bits) is below the value ("+ to_string(value)
+ ") bits representation number ("+ to_string(l_msbValue) + " bits) - the value will be troncated.");
}
l_startInd = size;
}
// Extend the message if necessary
uint32_t l_endPos = 1 + static_cast<uint32_t>(pos+size-1)/8;
while(p_mesg.size() < l_endPos)
{
p_mesg.push_back(0);
}
// Change bits order if "invert"
if(invert)
{
l_startInd = -1;
l_inv = 1;
}
// Fill the bits with the specified value
for(uint32_t i = 0; i < static_cast<uint32_t>(size); i++)
{
uint8_t l_currBit = (value >> (l_startInd + l_inv*(i + 1)) ) & 1;
FieldHelper::addBin(l_currBit,
static_cast<uint32_t>(pos)+i,
p_mesg);
}
}
////////////////////////////////////////////////////////////////////////
void Field::updateHeaderSize(uint32_t& p_headerSize)
{
if(p_headerSize < static_cast<uint32_t>(pos + size) )
{
p_headerSize = static_cast<uint32_t>(pos + size);
}
}
} // namespace ModGen