forked from flightaware/beast-splitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modes_message.h
194 lines (165 loc) · 6.07 KB
/
modes_message.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
// -*- c++ -*-
// Copyright (c) 2015-2016, FlightAware LLC.
// Copyright (c) 2015, Oliver Jowett <[email protected]>
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef MODES_MESSAGE_H
#define MODES_MESSAGE_H
#include <cstdint>
#include <cassert>
#include <vector>
#include <ostream>
#include <functional>
#include <map>
namespace modes {
// the type of one message
enum class MessageType {
INVALID,
MODE_AC,
MODE_S_SHORT,
MODE_S_LONG,
STATUS,
POSITION
};
enum class TimestampType { UNKNOWN, TWELVEMEG, GPS };
inline std::ostream& operator<<(std::ostream &os, const MessageType &t) {
switch (t) {
case MessageType::MODE_AC: return (os << "MODE_AC");
case MessageType::MODE_S_SHORT: return (os << "MODE_S_SHORT");
case MessageType::MODE_S_LONG: return (os << "MODE_S_LONG");
case MessageType::STATUS: return (os << "STATUS");
case MessageType::POSITION: return (os << "POSITION");
default: return (os << "INVALID");
}
}
inline std::size_t message_size(MessageType type)
{
// return the expected number of data bytes for a message of the given type
switch (type) {
case MessageType::MODE_AC: return 2;
case MessageType::MODE_S_SHORT: return 7;
case MessageType::MODE_S_LONG: return 14;
case MessageType::STATUS: return 14;
case MessageType::POSITION: return 14;
default: return 0;
}
}
extern std::uint32_t crc_table[256];
template <class InputIterator>
std::uint32_t crc(InputIterator first, InputIterator last) {
std::uint32_t c = 0;
for (InputIterator i = first; i != last; ++i) {
c = (c << 8) ^ crc_table[*i ^ ((c & 0xff0000) >> 16)];
}
return c & 0x00FFFFFF;
}
// a single message
class Message {
public:
Message()
: m_type(MessageType::INVALID),
m_timestamp_type(TimestampType::UNKNOWN),
m_timestamp(0),
m_signal(0),
residual(0xFFFFFFFF)
{}
Message(MessageType type_,
TimestampType timestamp_type_, std::uint64_t timestamp_,
std::uint8_t signal_, std::vector<std::uint8_t> &&data_)
: m_type(type_),
m_timestamp_type(timestamp_type_),
m_timestamp(timestamp_),
m_signal(signal_),
m_data(std::move(data_)),
residual(0xFFFFFFFF)
{
assert (m_data.size() == message_size(m_type));
}
Message(MessageType type_,
TimestampType timestamp_type_, std::uint64_t timestamp_,
std::uint8_t signal_, const std::vector<std::uint8_t> &data_)
: m_type(type_),
m_timestamp_type(timestamp_type_),
m_timestamp(timestamp_),
m_signal(signal_),
m_data(data_),
residual(0xFFFFFFFF)
{
assert (m_data.size() == message_size(m_type));
}
MessageType type() const {
return m_type;
}
std::uint64_t timestamp() const {
return m_timestamp;
}
TimestampType timestamp_type() const {
return m_timestamp_type;
}
std::uint8_t signal() const {
return m_signal;
}
const std::vector<std::uint8_t> &data() const {
return m_data;
}
int df() const {
switch (m_type) {
case MessageType::MODE_S_SHORT:
case MessageType::MODE_S_LONG:
return (m_data[0] >> 3) & 31;
default:
return -1;
}
}
bool crc_bad() const {
switch (df()) {
case 11:
return (crc_residual() & 0xFFFF80) != 0;
case 17:
case 18:
return crc_residual() != 0;
default:
return false;
}
}
private:
std::uint32_t crc_residual() const {
if (residual == 0xFFFFFFFF) {
std::size_t len = m_data.size();
if (len <= 3) {
residual = 0;
} else {
residual = crc(m_data.begin(), m_data.end() - 3);
residual ^= (m_data[len-3] << 16);
residual ^= (m_data[len-2] << 8);
residual ^= (m_data[len-1]);
}
}
return residual;
}
MessageType m_type;
TimestampType m_timestamp_type;
std::uint64_t m_timestamp;
std::uint8_t m_signal;
std::vector<std::uint8_t> m_data;
mutable std::uint32_t residual;
};
std::ostream& operator<<(std::ostream &os, const Message &message);
};
#endif