-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdecode.c
224 lines (188 loc) · 5.62 KB
/
decode.c
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
#include <stdint.h>
#include <string.h>
#include "channel.h"
#include "correlator/autocorrelator.h"
#include "correlator/correlator.h"
#include "decode.h"
#include "diffcode/diffcode.h"
#include "deinterleave/deinterleave.h"
#include "ecc/descramble.h"
#include "ecc/rs.h"
#include "ecc/viterbi.h"
#include "protocol/cadu.h"
#include "parser/mpdu_parser.h"
#include "parser/mcu_parser.h"
#include "utils.h"
static int read_samples(int (*read)(int8_t *dst, size_t len), int8_t *dst, size_t len);
static int _rs, _vit;
static uint32_t _vcdu_seq;
static int _diffcoded;
static int _interleaved;
static enum { READ, PARSE_MPDU, VIT_SECOND } _state;
#ifndef NDEBUG
FILE *_vcdu;
#endif
void
decode_init(int diffcoded, int interleaved)
{
uint64_t convolved_syncword;
/* Initialize subsystems */
conv_encode_u32(&convolved_syncword, 0, SYNCWORD);
correlator_init(convolved_syncword);
viterbi_init();
descramble_init();
rs_init();
mpdu_parser_init();
#ifndef NDEBUG
_vcdu = fopen("/tmp/vcdu.data", "wb");
#endif
_rs = 0;
_vit = 0;
_vcdu_seq = 0;
_diffcoded = diffcoded;
_interleaved = interleaved;
_state = READ;
}
DecoderState
decode_soft_cadu(Mpdu *dst, int (*read)(int8_t *dst, size_t len))
{
static int8_t soft_cadu[INTER_SIZE(2*CADU_SOFT_LEN)];
static int offset;
static int vit;
static Cadu cadu;
uint8_t hard_cadu[CONV_CADU_LEN];
int errors;
unsigned int i;
enum phase rotation;
switch (_state) {
case READ:
/* Read a CADU worth of samples */
for (i=0; i<CADU_SOFT_LEN; i+=CADU_SOFT_CHUNK) {
if (read_samples(read, soft_cadu+i, CADU_SOFT_CHUNK)) return EOF_REACHED;
}
/* Differentially decode if necessary */
if (_diffcoded) diff_decode(soft_cadu, CADU_SOFT_LEN);
/* Perform correlation and advance to the next state */
soft_to_hard(hard_cadu, soft_cadu, CADU_SOFT_LEN);
offset = correlate(&rotation, hard_cadu, CONV_CADU_LEN);
/* Read more samples to get a full CADU */
if (offset > 0) {
if (read_samples(read, soft_cadu+CADU_SOFT_LEN, offset)) return EOF_REACHED;
if (_diffcoded) diff_decode(soft_cadu+CADU_SOFT_LEN, offset);
}
/* Derotate */
soft_derotate(soft_cadu+offset, CADU_SOFT_LEN, rotation);
/* Finish decoding the past frame (output is VITERBI_DELAY bits late) */
vit = viterbi_decode(((uint8_t*)&cadu) + sizeof(Cadu)-VITERBI_DELAY,
soft_cadu+offset,
VITERBI_DELAY);
/* Descramble and error correct */
descramble(&cadu);
errors = rs_fix(&cadu.data);
_rs = errors;
#ifndef NDEBUG
fwrite(&cadu, sizeof(cadu), 1, _vcdu);
fflush(_vcdu);
#endif
/* If RS reports failure, reinitialize the internal state of the
* MPDU decoder and finish up the Viterbi decode process. */
if (errors < 0) {
mpdu_parser_init();
_state = VIT_SECOND;
return STATS_ONLY;
}
_vcdu_seq = vcdu_counter(&cadu.data);
_state = PARSE_MPDU;
__attribute__((fallthrough));
case PARSE_MPDU:
/* Parse the next MPDU in the decoded VCDU */
switch (mpdu_reconstruct(dst, &cadu.data)) {
case PARSED:
return MPDU_READY;
case PROCEED:
_state = VIT_SECOND;
break;
default:
break;
}
break;
case VIT_SECOND:
/* Viterbi decode (2/2) */
vit += viterbi_decode((uint8_t*)&cadu,
soft_cadu+offset+2*8*VITERBI_DELAY,
sizeof(Cadu)-VITERBI_DELAY);
_vit = vit / sizeof(Cadu);
_state = READ;
break;
default:
_state = READ;
break;
}
return NOT_READY;
}
int
decode_get_rs()
{
return _rs;
}
int
decode_get_vit()
{
return _vit;
}
uint32_t
decode_get_vcdu_seq()
{
return _vcdu_seq;
}
static int
read_samples(int (*read)(int8_t *dst, size_t len), int8_t *dst, size_t len)
{
static int offset;
static int8_t from_prev[INTER_MARKER_STRIDE];
int deint_offset;
int num_samples;
uint8_t hard[INTER_SIZE(len)];
static enum phase rotation;
/* If not interleaved, directly read and return */
if (!_interleaved) return !read(dst, len);
/* Retrieve enough samples so that the deinterleaver will output
* $len samples. Use the internal cache first */
num_samples = deinterleave_num_samples(len);
if (offset) {
memcpy(dst, from_prev, MIN(offset, num_samples));
memcpy(from_prev, from_prev+offset, offset-MIN(offset, num_samples));
}
if (num_samples-offset > 0 && !read(dst+offset, num_samples-offset)) return 1;
offset -= MIN(offset, num_samples);
if (num_samples < INTER_MARKER_STRIDE*8) {
/* Not enough bytes to reliably find sync marker offset: assume the
* offset is correct, and just derotate and deinterleave what we read */
soft_derotate(dst, num_samples, rotation);
deinterleave(dst, dst, len);
} else {
/* Find synchronization marker (offset with the best autocorrelation) */
soft_to_hard(hard, dst, num_samples & ~0x7);
offset = autocorrelate(&rotation, INTER_MARKER_STRIDE/8, hard, num_samples/8);
/* Get where the deinterleaver expects the next marker to be */
deint_offset = deinterleave_expected_sync_offset();
/* Compute the delta between the expected marker position and the
* one found by the correlator */
offset = (offset - deint_offset + INTER_MARKER_INTERSAMPS + 1) % INTER_MARKER_STRIDE;
offset = offset > INTER_MARKER_STRIDE/2 ? offset-INTER_MARKER_STRIDE : offset;
/* If the offset is positive, read more
* bits to get $num_samples valid samples. If the offset is negative,
* copy the last few bytes into the local cache */
if (offset > 0) {
if (!read(dst+num_samples, offset)) return 1;
} else {
memcpy(from_prev, dst+num_samples+offset, -offset);
}
/* Correct rotation for these samples */
soft_derotate(dst, num_samples+offset, rotation);
/* Deinterleave */
deinterleave(dst, dst+offset, len);
offset = offset < 0 ? -offset : 0;
}
return 0;
}