-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsgmap.hpp
251 lines (201 loc) · 5.73 KB
/
msgmap.hpp
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
#pragma once
#include <algorithm>
#ifdef FREEBSD
#include <sys/endian.h>
#elif __APPLE__
#include <machine/endian.h>
#else
#include <endian.h>
#endif
#include <type_traits>
namespace mavlink {
/**
* Serialization helper wrapper for mavlink_message_t
*/
class MsgMap {
public:
explicit MsgMap(mavlink_message_t *p) :
msg(p), cmsg(p), pos(0)
{ }
explicit MsgMap(mavlink_message_t &p) :
msg(&p), cmsg(&p), pos(0)
{ }
explicit MsgMap(const mavlink_message_t *p) :
msg(nullptr), cmsg(p), pos(0)
{ }
inline void reset()
{
pos = 0;
}
inline void reset(uint32_t msgid, uint8_t len)
{
assert(msg);
msg->msgid = msgid; // necessary for finalize
msg->len = len; // needed only for deserialization w/o finalize
pos = 0;
}
template<typename _T>
void operator<< (const _T data);
template<class _T, size_t _Size>
void operator<< (const std::array<_T, _Size> &data);
template<typename _T>
void operator>> (_T &data);
template<class _T, size_t _Size>
void operator>> (std::array<_T, _Size> &data);
private:
mavlink_message_t *msg; // for serialization
const mavlink_message_t *cmsg; // for deserialization
size_t pos;
};
namespace impl {
template<size_t _N>
struct UintBufferHelper;
template<>
struct UintBufferHelper<1>
{
typedef uint8_t Type;
};
template<>
struct UintBufferHelper<2>
{
typedef uint16_t Type;
};
template<>
struct UintBufferHelper<4>
{
typedef uint32_t Type;
};
template<>
struct UintBufferHelper<8>
{
typedef uint64_t Type;
};
template<typename _T>
struct UintBuffer
{
typedef typename UintBufferHelper<sizeof(_T)>::Type Type;
};
template<typename _T>
_T to_little_endian_internal(_T);
template<>
inline uint8_t to_little_endian_internal<uint8_t>(uint8_t data)
{
return data;
}
template<>
inline uint16_t to_little_endian_internal<uint16_t>(uint16_t data)
{
return htole16(data);
}
template<>
inline uint32_t to_little_endian_internal<uint32_t>(uint32_t data)
{
return htole32(data);
}
template<>
inline uint64_t to_little_endian_internal<uint64_t>(uint64_t data)
{
return htole64(data);
}
template<typename _T>
typename std::enable_if<std::is_floating_point<_T>::value, typename UintBuffer<_T>::Type>::type to_little_endian(_T data)
{
typedef typename UintBuffer<_T>::Type ReturnType;
ReturnType buf;
memcpy(&buf, &data, sizeof(ReturnType));
return to_little_endian_internal<ReturnType>(buf);
}
template<typename _T>
typename std::enable_if<std::is_integral<_T>::value, typename UintBuffer<_T>::Type>::type to_little_endian(_T data)
{
return to_little_endian_internal<typename UintBuffer<_T>::Type>(data);
}
template<typename _T>
_T to_host_from_little_endian_internal(_T);
template<>
inline uint8_t to_host_from_little_endian_internal<uint8_t>(uint8_t data)
{
return data;
}
template<>
inline uint16_t to_host_from_little_endian_internal<uint16_t>(uint16_t data)
{
return le16toh(data);
}
template<>
inline uint32_t to_host_from_little_endian_internal<uint32_t>(uint32_t data)
{
return le32toh(data);
}
template<>
inline uint64_t to_host_from_little_endian_internal<uint64_t>(uint64_t data)
{
return le64toh(data);
}
template<typename _TOutput, typename _TInput,
class = typename std::enable_if<std::is_unsigned<_TInput>::value>::type>
typename std::enable_if<std::is_floating_point<_TOutput>::value, _TOutput>::type to_host_from_little_endian(_TInput data)
{
static_assert(sizeof(_TInput) == sizeof(_TOutput), "Size of input and output must match");
data = to_host_from_little_endian_internal(data);
_TOutput buf;
memcpy(&buf, &data, sizeof(_TOutput));
return buf;
}
template<typename _TOutput, typename _TInput,
class = typename std::enable_if<std::is_unsigned<_TInput>::value>::type>
typename std::enable_if<std::is_integral<_TOutput>::value, _TOutput>::type to_host_from_little_endian(_TInput data)
{
static_assert(sizeof(_TInput) == sizeof(_TOutput), "Size of input and output must match");
return to_host_from_little_endian_internal(data);
}
} // namespace impl
} // namespace mavlink
// implementation
template<typename _T>
void mavlink::MsgMap::operator<< (const _T data)
{
assert(msg);
assert(pos + sizeof(_T) <= MAVLINK_MAX_PAYLOAD_LEN);
auto data_le = mavlink::impl::to_little_endian<_T>(data);
memcpy(&_MAV_PAYLOAD_NON_CONST(msg)[pos], &data_le, sizeof(data_le));
pos += sizeof(_T);
}
template<class _T, size_t _Size>
void mavlink::MsgMap::operator<< (const std::array<_T, _Size> &data)
{
for (auto &v : data) {
*this << v;
}
}
template<typename _T>
void mavlink::MsgMap::operator>> (_T &data)
{
assert(cmsg);
assert(pos + sizeof(_T) <= MAVLINK_MAX_PAYLOAD_LEN);
ssize_t remaining_non_zero_data = cmsg->len - pos;
typename mavlink::impl::UintBuffer<_T>::Type buf;
if (static_cast<ssize_t>(sizeof(_T)) <= remaining_non_zero_data) { // field is not truncated
memcpy(&buf, &_MAV_PAYLOAD(cmsg)[pos], sizeof(_T));
} else if (remaining_non_zero_data <= 0) {
// there is no non-zero data left, so just fill with 0
buf = 0;
} else { // field is trimmed - pad with zeroes
// here remaining_non_zero_data < sizeof(_T) holds
size_t non_zero_count = std::max<decltype(remaining_non_zero_data)>(remaining_non_zero_data, 0);
size_t pad_zero_count = sizeof(_T) - non_zero_count;
std::array<char, sizeof(_T)> raw_buf;
memcpy(raw_buf.data(), &_MAV_PAYLOAD(cmsg)[pos], non_zero_count);
memset(raw_buf.data() + non_zero_count, 0, pad_zero_count);
memcpy(&buf, raw_buf.data(), raw_buf.size());
}
data = mavlink::impl::to_host_from_little_endian<_T>(buf);
pos += sizeof(_T);
}
template<class _T, size_t _Size>
void mavlink::MsgMap::operator>> (std::array<_T, _Size> &data)
{
for (auto &v : data) {
*this >> v;
}
}