-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_serialization.h
331 lines (286 loc) · 7.56 KB
/
data_serialization.h
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
/**
* @brief Data serialization
* @author Rodrigo Munoz
* @date 2016
*
* Data Serialization
* ------------------
*
* Functions for data serialization.
*
*/
#ifndef DATA_SERIALIZATION_H
#define DATA_SERIALIZATION_H
#define __STDC_LIMIT_MACROS
#include <stdint.h>
/**
* @brief Base virtual class for messages
*/
class Message
{
public:
virtual uint8_t serialize(uint8_t *outbuffer) const = 0;
virtual uint8_t deserialize(uint8_t *inbuffer) = 0;
};
//-----------------------------------------------------------------
/**
* UInt8
*/
class UInt8: public Message
{
public:
uint8_t data;
UInt8(): data(0U) {}
virtual uint8_t serialize(uint8_t *outbuffer) const
{
uint8_t offset = 0U;
*(outbuffer + offset + 0U) = (this->data >> (8U * 0U)) & 0xFF;
offset += sizeof(this->data);
return offset;
}
virtual uint8_t deserialize(uint8_t *inbuffer)
{
uint8_t offset = 0U;
this->data = ((uint8_t) (*(inbuffer + offset)));
offset += sizeof(this->data);
return offset;
}
typedef uint8_t type;
static const uint8_t max = UINT8_MAX;
static const uint8_t min = 0U;
};
//---------------------------------------------------------------------------
/**
* Int8
*/
class Int8: public Message
{
public:
int8_t data;
Int8(): data(0U) {}
virtual uint8_t serialize(uint8_t *outbuffer) const
{
uint8_t offset = 0U;
union {
int8_t real;
uint8_t base;
} u_data;
u_data.real = this->data;
*(outbuffer + offset + 0U) = (u_data.base >> (8U * 0U)) & 0xFF;
offset += sizeof(this->data);
return offset;
}
virtual uint8_t deserialize(uint8_t *inbuffer)
{
uint8_t offset = 0U;
union {
int8_t real;
uint8_t base;
} u_data;
u_data.base = 0U;
u_data.base |= ((uint8_t) (*(inbuffer + offset + 0U))) << (8U * 0U);
this->data = u_data.real;
offset += sizeof(this->data);
return offset;
}
typedef int8_t type;
};
//---------------------------------------------------------------------------
/**
* UInt16
*/
class UInt16 : public Message
{
public:
uint16_t data;
UInt16(): data(0U) {}
virtual uint8_t serialize(uint8_t *outbuffer) const
{
uint8_t offset = 0U;
*(outbuffer + offset + 0U) = (this->data >> (8U * 0U)) & 0xFF;
*(outbuffer + offset + 1U) = (this->data >> (8U * 1U)) & 0xFF;
offset += sizeof(this->data);
return offset;
}
virtual uint8_t deserialize(uint8_t *inbuffer)
{
uint8_t offset = 0;
this->data = ((uint16_t) (*(inbuffer + offset)));
this->data |= ((uint16_t) (*(inbuffer + offset + 1U))) << (8U * 1U);
offset += sizeof(this->data);
return offset;
}
typedef uint16_t type;
};
//---------------------------------------------------------------------------
/**
* Int16
*/
class Int16 : public Message
{
public:
int16_t data;
Int16(): data(0) {}
virtual uint8_t serialize(uint8_t *outbuffer) const
{
uint8_t offset = 0U;
union {
int16_t real;
uint16_t base;
} u_data;
u_data.real = this->data;
*(outbuffer + offset + 0U) = (u_data.base >> (8U * 0U)) & 0xFF;
*(outbuffer + offset + 1U) = (u_data.base >> (8U * 1U)) & 0xFF;
offset += sizeof(this->data);
return offset;
}
virtual uint8_t deserialize(uint8_t *inbuffer)
{
uint8_t offset = 0;
union {
int16_t real;
uint16_t base;
} u_data;
u_data.base = 0;
u_data.base |= ((uint16_t) (*(inbuffer + offset + 0U))) << (8U * 0U);
u_data.base |= ((uint16_t) (*(inbuffer + offset + 1U))) << (8U * 1U);
this->data = u_data.real;
offset += sizeof(this->data);
return offset;
}
typedef int16_t type;
};
//---------------------------------------------------------------------------
/**
* UInt32
*/
class UInt32: public Message
{
public:
uint32_t data;
UInt32(): data(0UL) {}
virtual uint8_t serialize(uint8_t *outbuffer) const
{
uint8_t offset = 0;
*(outbuffer + offset + 0U) = (this->data >> (8U * 0U)) & 0xFF;
*(outbuffer + offset + 1U) = (this->data >> (8U * 1U)) & 0xFF;
*(outbuffer + offset + 2U) = (this->data >> (8U * 2U)) & 0xFF;
*(outbuffer + offset + 3U) = (this->data >> (8U * 3U)) & 0xFF;
offset += sizeof(this->data);
return offset;
}
virtual uint8_t deserialize(uint8_t *inbuffer)
{
uint8_t offset = 0U;
this->data = ((uint32_t) (*(inbuffer + offset + 0U))) << (8U * 0U);
this->data |= ((uint32_t) (*(inbuffer + offset + 1U))) << (8U * 1U);
this->data |= ((uint32_t) (*(inbuffer + offset + 2U))) << (8U * 2U);
this->data |= ((uint32_t) (*(inbuffer + offset + 3U))) << (8U * 3U);
offset += sizeof(this->data);
return offset;
}
typedef uint32_t type;
};
//---------------------------------------------------------------------------
/**
* Int32
*/
class Int32 : public Message
{
public:
int32_t data;
Int32():
data(0UL)
{
}
virtual uint8_t serialize(uint8_t *outbuffer) const
{
uint8_t offset = 0U;
union {
int32_t real;
uint32_t base;
} u_data;
u_data.real = this->data;
*(outbuffer + offset + 0U) = (u_data.base >> (8U * 0U)) & 0xFF;
*(outbuffer + offset + 1U) = (u_data.base >> (8U * 1U)) & 0xFF;
*(outbuffer + offset + 2U) = (u_data.base >> (8U * 2U)) & 0xFF;
*(outbuffer + offset + 3U) = (u_data.base >> (8U * 3U)) & 0xFF;
offset += sizeof(this->data);
return offset;
}
virtual uint8_t deserialize(uint8_t *inbuffer)
{
uint8_t offset = 0U;
union {
int32_t real;
uint32_t base;
} u_data;
u_data.base = 0U;
u_data.base |= ((uint32_t) (*(inbuffer + offset + 0U))) << (8U * 0U);
u_data.base |= ((uint32_t) (*(inbuffer + offset + 1U))) << (8U * 1U);
u_data.base |= ((uint32_t) (*(inbuffer + offset + 2U))) << (8U * 2U);
u_data.base |= ((uint32_t) (*(inbuffer + offset + 3U))) << (8U * 3U);
this->data = u_data.real;
offset += sizeof(this->data);
return offset;
}
typedef int32_t type;
};
//---------------------------------------------------------------------------
/**
* Float32
*/
class Float32 : public Message
{
public:
float data;
Float32():
data(0.0f)
{
}
virtual uint8_t serialize(uint8_t *outbuffer) const
{
uint8_t offset = 0U;
union {
float real;
uint32_t base;
} u_data;
u_data.real = this->data;
*(outbuffer + offset + 0U) = (u_data.base >> (8U * 0U)) & 0xFF;
*(outbuffer + offset + 1U) = (u_data.base >> (8U * 1U)) & 0xFF;
*(outbuffer + offset + 2U) = (u_data.base >> (8U * 2U)) & 0xFF;
*(outbuffer + offset + 3U) = (u_data.base >> (8U * 3U)) & 0xFF;
offset += sizeof(this->data);
return offset;
}
virtual uint8_t deserialize(uint8_t *inbuffer)
{
uint8_t offset = 0U;
union {
float real;
uint32_t base;
} u_data;
u_data.base = 0;
u_data.base |= ((uint32_t) (*(inbuffer + offset + 0U))) << (8U * 0U);
u_data.base |= ((uint32_t) (*(inbuffer + offset + 1U))) << (8U * 1U);
u_data.base |= ((uint32_t) (*(inbuffer + offset + 2U))) << (8U * 2U);
u_data.base |= ((uint32_t) (*(inbuffer + offset + 3U))) << (8U * 3U);
this->data = u_data.real;
offset += sizeof(this->data);
return offset;
}
typedef float type;
};
//---------------------------------------------------------------------------
/**
* Constants values
*/
template<int32_t Num, int32_t Den = 1UL>
struct ConstRatio{
static const float value = 1.0f*Num/Den;
};
template<typename T, T val>
struct ConstInt{
static const T value = val;
};
#endif