forked from dronekit/dronekit-la
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMsgHandler.h
226 lines (191 loc) · 6.54 KB
/
MsgHandler.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
#ifndef AP_MSGHANDLER_H
#define AP_MSGHANDLER_H
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h> // for abort()
#include <analyzer_util.h> // for is_zero
#include <math.h>
#define radians(x) (x/180*M_PI)
#include "Vector3f.h"
#include "Location.h"
#include "DataFlash/LogMessage.h"
#define LOGREADER_MAX_FIELDS 30
#ifndef streq
#define streq(x, y) (!strcmp(x, y))
#endif
class MsgHandler {
public:
// constructor - create a parser for a MavLink message format
MsgHandler(const struct log_Format &f);
// retrieve a comma-separated list of all labels
void string_for_labels(char *buffer, uint8_t bufferlen);
bool field_value(const uint8_t *msg, const char *label, Vector3f &ret);
// field_value - retrieve the value of a field from the supplied message
// these return false if the field was not found
template<typename R>
bool field_value(const uint8_t *msg, const char *label, R &ret);
bool field_value(const uint8_t *msg, const char *label,
char *buffer, uint8_t bufferlen);
template <typename R>
void require_field(const uint8_t *msg, const char *label, R &ret)
{
if (! field_value(msg, label, ret)) {
field_not_found(msg, label);
}
}
void require_field(const uint8_t *msg, const char *label, char *buffer, uint8_t bufferlen);
float require_field_float(const uint8_t *msg, const char *label);
uint8_t require_field_uint8_t(const uint8_t *msg, const char *label);
int32_t require_field_int32_t(const uint8_t *msg, const char *label);
uint32_t require_field_uint32_t(const uint8_t *msg, const char *label);
uint16_t require_field_uint16_t(const uint8_t *msg, const char *label);
int16_t require_field_int16_t(const uint8_t *msg, const char *label);
private:
void add_field(const char *_label, uint8_t _type, uint8_t _offset,
uint8_t length);
void field_value_for_type_at_offset(const uint8_t *msg, uint8_t type,
uint8_t offset, bool &ret);
template<typename R>
void field_value_for_type_at_offset(const uint8_t *msg, uint8_t type,
uint8_t offset, R &ret);
struct format_field_info { // parsed field information
char *label;
uint8_t type;
uint8_t offset;
uint8_t length;
};
struct format_field_info field_info[LOGREADER_MAX_FIELDS] = { };
uint8_t next_field;
size_t size_for_type_table[52]; // maps field type (e.g. 'f') to e.g 4 bytes
void parse_format_fields();
void init_field_types();
void add_field_type(char type, size_t size);
uint8_t size_for_type(char type);
protected:
struct format_field_info *find_field_info(const char *label);
template<typename R>
void _field_value_for_type_at_offset(const uint8_t *msg, uint8_t type,
uint8_t offset, R &ret);
struct log_Format f; // the format we are a parser for
~MsgHandler();
void location_from_msg(uint8_t *msg, Location &loc, const char *label_lat,
const char *label_long, const char *label_alt);
void ground_vel_from_msg(uint8_t *msg,
Vector3f &vel,
const char *label_speed,
const char *label_course,
const char *label_vz);
void attitude_from_msg(uint8_t *msg,
Vector3f &att,
const char *label_roll,
const char *label_pitch,
const char *label_yaw);
void field_not_found(const uint8_t *msg, const char *label);
};
template<typename R>
bool MsgHandler::field_value(const uint8_t *msg, const char *label, R &ret)
{
struct format_field_info *info = find_field_info(label);
if (info == NULL) {
return false;
}
uint8_t offset = info->offset;
if (offset == 0) {
return false;
}
field_value_for_type_at_offset(msg, info->type, offset, ret);
return true;
}
template<typename R>
inline void MsgHandler::field_value_for_type_at_offset(const uint8_t *msg,
uint8_t type,
uint8_t offset,
R &ret)
{
_field_value_for_type_at_offset(msg, type, offset, ret);
}
// handle bool return case specially so we can use is_zero on floats:
inline void MsgHandler::field_value_for_type_at_offset(const uint8_t *msg,
uint8_t type,
uint8_t offset,
bool &ret)
{
switch (type) {
case 'f': {
float value = 0;
const uint8_t to_copy = size_for_type(type);
memcpy((void *)&value, &msg[offset], to_copy);
ret = ! is_zero(value);
break;
}
default:
_field_value_for_type_at_offset(msg, type, offset, ret);
}
}
template<typename R>
inline void MsgHandler::_field_value_for_type_at_offset(const uint8_t *msg,
const uint8_t type,
const uint8_t offset,
R &ret)
{
uint8_t to_copy = size_for_type(type);
union {
uint8_t u8;
int16_t i16;
int32_t i32;
int64_t i64;
uint16_t u16;
uint32_t u32;
uint64_t u64;
float f;
} dest;
memset(&dest, 0, sizeof(dest));
if (sizeof(R) < to_copy) {
// we should never ask to e.g. store a uint16 from a msg into e.g. a uint8
::fprintf(stderr, "Internal error: destination too small");
abort();
}
memcpy(&dest, &msg[offset], to_copy);
/* we register the types - add_field_type - so can we do without
* this switch statement somehow? */
switch (type) {
case 'M':
case 'B':
ret = (R)dest.u8;
break;
case 'c':
case 'h':
ret = (R)dest.i16;
break;
case 'H':
ret = (R)dest.u16;
break;
case 'C':
ret = (R)dest.u16;
break;
case 'f':
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
ret = (R)dest.f;
#pragma GCC diagnostic pop
break;
case 'I':
case 'E':
ret = (R)dest.u32;
break;
case 'L':
case 'e':
ret = (R)dest.i32;
break;
case 'q':
ret = (R)dest.i64;
break;
case 'Q':
ret = (R)dest.u64;
break;
default:
::printf("Unhandled format type (%c)\n", type);
::abort();
}
}
#endif