-
Notifications
You must be signed in to change notification settings - Fork 286
/
decode_mac.cc
258 lines (208 loc) · 7.88 KB
/
decode_mac.cc
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
/*
* Copyright (C) 2013, 2016 Bastian Bloessl <bloessl@ccs-labs.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ieee802_11/decode_mac.h>
#include "utils.h"
#include "viterbi_decoder/viterbi_decoder.h"
#include <gnuradio/io_signature.h>
#include <boost/crc.hpp>
#include <iomanip>
using namespace gr::ieee802_11;
#define LINKTYPE_IEEE802_11 105 /* http://www.tcpdump.org/linktypes.html */
class decode_mac_impl : public decode_mac
{
public:
decode_mac_impl(bool log, bool debug)
: block("decode_mac",
gr::io_signature::make(1, 1, 48),
gr::io_signature::make(0, 0, 0)),
d_log(log),
d_debug(debug),
d_ofdm(BPSK_1_2),
d_frame(d_ofdm, 0),
d_frame_complete(true)
{
message_port_register_out(pmt::mp("out"));
}
int general_work(int noutput_items,
gr_vector_int& ninput_items,
gr_vector_const_void_star& input_items,
gr_vector_void_star& output_items)
{
const uint8_t* in = (const uint8_t*)input_items[0];
int i = 0;
std::vector<gr::tag_t> tags;
const uint64_t nread = this->nitems_read(0);
dout << "Decode MAC: input " << ninput_items[0] << std::endl;
while (i < ninput_items[0]) {
get_tags_in_range(tags, 0, nread + i, nread + i + 1);
if (tags.size()) {
if (d_frame_complete == false) {
dout << "Warning: starting to receive new frame before old frame was "
"complete"
<< std::endl;
dout << "Already copied " << copied << " out of " << d_frame.n_sym
<< " symbols of last frame" << std::endl;
}
d_frame_complete = false;
// Enter tags into metadata dictionary
d_meta = pmt::make_dict();
for (auto tag : tags)
d_meta = pmt::dict_add(d_meta, tag.key, tag.value);
int len_data = pmt::to_uint64(pmt::dict_ref(
d_meta, pmt::mp("frame bytes"), pmt::from_uint64(MAX_PSDU_SIZE + 1)));
int encoding = pmt::to_uint64(
pmt::dict_ref(d_meta, pmt::mp("encoding"), pmt::from_uint64(0)));
ofdm_param ofdm = ofdm_param((Encoding)encoding);
frame_param frame = frame_param(ofdm, len_data);
// check for maximum frame size
if (frame.n_sym <= MAX_SYM && frame.psdu_size <= MAX_PSDU_SIZE) {
d_ofdm = ofdm;
d_frame = frame;
copied = 0;
dout << "Decode MAC: frame start -- len " << len_data << " symbols "
<< frame.n_sym << " encoding " << encoding << std::endl;
} else {
dout << "Dropping frame which is too large (symbols or bits)"
<< std::endl;
}
}
if (copied < d_frame.n_sym) {
dout << "copy one symbol, copied " << copied << " out of "
<< d_frame.n_sym << std::endl;
std::memcpy(d_rx_symbols + (copied * 48), in, 48);
copied++;
if (copied == d_frame.n_sym) {
dout << "received complete frame - decoding" << std::endl;
decode();
in += 48;
i++;
d_frame_complete = true;
break;
}
}
in += 48;
i++;
}
consume(0, i);
return 0;
}
void decode()
{
for (int i = 0; i < d_frame.n_sym * 48; i++) {
for (int k = 0; k < d_ofdm.n_bpsc; k++) {
d_rx_bits[i * d_ofdm.n_bpsc + k] = !!(d_rx_symbols[i] & (1 << k));
}
}
deinterleave();
uint8_t* decoded = d_decoder.decode(&d_ofdm, &d_frame, d_deinterleaved_bits);
descramble(decoded);
print_output();
// skip service field
boost::crc_32_type result;
result.process_bytes(out_bytes + 2, d_frame.psdu_size);
if (result.checksum() != 558161692) {
dout << "checksum wrong -- dropping" << std::endl;
return;
}
mylog("encoding: {} - length: {} - symbols: {}",
d_ofdm.encoding,
d_frame.psdu_size,
d_frame.n_sym);
// create PDU
pmt::pmt_t blob = pmt::make_blob(out_bytes + 2, d_frame.psdu_size - 4);
d_meta =
pmt::dict_add(d_meta, pmt::mp("dlt"), pmt::from_long(LINKTYPE_IEEE802_11));
message_port_pub(pmt::mp("out"), pmt::cons(d_meta, blob));
}
void deinterleave()
{
int n_cbps = d_ofdm.n_cbps;
int first[MAX_BITS_PER_SYM];
int second[MAX_BITS_PER_SYM];
int s = std::max(d_ofdm.n_bpsc / 2, 1);
for (int j = 0; j < n_cbps; j++) {
first[j] = s * (j / s) + ((j + int(floor(16.0 * j / n_cbps))) % s);
}
for (int i = 0; i < n_cbps; i++) {
second[i] = 16 * i - (n_cbps - 1) * int(floor(16.0 * i / n_cbps));
}
int count = 0;
for (int i = 0; i < d_frame.n_sym; i++) {
for (int k = 0; k < n_cbps; k++) {
d_deinterleaved_bits[i * n_cbps + second[first[k]]] =
d_rx_bits[i * n_cbps + k];
}
}
}
void descramble(uint8_t* decoded_bits)
{
int state = 0;
std::memset(out_bytes, 0, d_frame.psdu_size + 2);
for (int i = 0; i < 7; i++) {
if (decoded_bits[i]) {
state |= 1 << (6 - i);
}
}
out_bytes[0] = state;
int feedback;
int bit;
for (int i = 7; i < d_frame.psdu_size * 8 + 16; i++) {
feedback = ((!!(state & 64))) ^ (!!(state & 8));
bit = feedback ^ (decoded_bits[i] & 0x1);
out_bytes[i / 8] |= bit << (i % 8);
state = ((state << 1) & 0x7e) | feedback;
}
}
void print_output()
{
dout << std::endl;
dout << "psdu size" << d_frame.psdu_size << std::endl;
for (int i = 2; i < d_frame.psdu_size + 2; i++) {
dout << std::setfill('0') << std::setw(2) << std::hex
<< ((unsigned int)out_bytes[i] & 0xFF) << std::dec << " ";
if (i % 16 == 15) {
dout << std::endl;
}
}
dout << std::endl;
for (int i = 2; i < d_frame.psdu_size + 2; i++) {
if ((out_bytes[i] > 31) && (out_bytes[i] < 127)) {
dout << ((char)out_bytes[i]);
} else {
dout << ".";
}
}
dout << std::endl;
}
private:
bool d_debug;
bool d_log;
pmt::pmt_t d_meta;
frame_param d_frame;
ofdm_param d_ofdm;
viterbi_decoder d_decoder;
uint8_t d_rx_symbols[48 * MAX_SYM];
uint8_t d_rx_bits[MAX_ENCODED_BITS];
uint8_t d_deinterleaved_bits[MAX_ENCODED_BITS];
uint8_t out_bytes[MAX_PSDU_SIZE + 2]; // 2 for signal field
int copied;
bool d_frame_complete;
};
decode_mac::sptr decode_mac::make(bool log, bool debug)
{
return gnuradio::get_initial_sptr(new decode_mac_impl(log, debug));
}