-
Notifications
You must be signed in to change notification settings - Fork 25
/
fft_eval.c
324 lines (274 loc) · 8.89 KB
/
fft_eval.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* SPDX-License-Identifier: GPL-2.0-only
* SPDX-FileCopyrightText: 2012 Simon Wunderlich <[email protected]>
* SPDX-FileCopyrightText: 2012 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* SPDX-FileCopyrightText: 2013 Gui Iribarren <[email protected]>
* SPDX-FileCopyrightText: 2017 Nico Pace <[email protected]>
*/
/*
* This program has been created to aid open source spectrum
* analyzer development for Qualcomm/Atheros AR92xx and AR93xx
* based chipsets.
*/
#include "fft_eval.h"
#if defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define CONVERT_BE16(val) val = OSSwapBigToHostInt16(val)
#define CONVERT_BE32(val) val = OSSwapBigToHostInt32(val)
#define CONVERT_BE64(val) val = OSSwapBigToHostInt64(val)
#elif defined(_WIN32)
#define __USE_MINGW_ANSI_STDIO 1
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define CONVERT_BE16(val) val = _byteswap_ushort(val)
#define CONVERT_BE32(val) val = _byteswap_ulong(val)
#define CONVERT_BE64(val) val = _byteswap_uint64(val)
#elif __BYTE_ORDER == __BIG_ENDIAN
#define CONVERT_BE16(val)
#define CONVERT_BE32(val)
#define CONVERT_BE64(val)
#else
#error Endianess undefined
#endif
#else
#ifdef __FreeBSD__
#include <sys/endian.h>
#else
#include <endian.h>
#endif /* __FreeBSD__ */
#define CONVERT_BE16(val) val = be16toh(val)
#define CONVERT_BE32(val) val = be32toh(val)
#define CONVERT_BE64(val) val = be64toh(val)
#endif
#include <errno.h>
#include <stdio.h>
#include <math.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct scanresult *result_list;
int scanresults_n;
/* read_file - reads an file into a big buffer and returns it
*
* @fname: file name
*
* returns the buffer with the files content
*/
static char *read_file(char *fname, size_t *size)
{
FILE *fp;
char *buf = NULL;
char *newbuf;
size_t ret;
fp = fopen(fname, "rb");
if (!fp)
return NULL;
*size = 0;
while (!feof(fp)) {
newbuf = realloc(buf, *size + 4097);
if (!newbuf) {
free(buf);
return NULL;
}
buf = newbuf;
ret = fread(buf + *size, 1, 4096, fp);
*size += ret;
}
fclose(fp);
if (buf)
buf[*size] = '\0';
return buf;
}
/*
* read_scandata - reads the fft scandata and compiles a linked list of datasets
*
* @fname: file name
*
* returns 0 on success, -1 on error.
*/
int fft_eval_init(char *fname)
{
char *pos, *scandata;
size_t len, sample_len;
size_t rel_pos, remaining_len;
struct scanresult *result;
struct fft_sample_tlv *tlv;
struct scanresult *tail = result_list;
int handled, bins;
scandata = read_file(fname, &len);
if (!scandata)
return -1;
pos = scandata;
while ((uintptr_t)(pos - scandata) < len) {
rel_pos = pos - scandata;
remaining_len = len - rel_pos;
if (remaining_len < sizeof(*tlv)) {
fprintf(stderr, "Found incomplete TLV header at position 0x%zx\n", rel_pos);
break;
}
tlv = (struct fft_sample_tlv *) pos;
CONVERT_BE16(tlv->length);
sample_len = sizeof(*tlv) + tlv->length;
pos += sample_len;
if (remaining_len < sample_len) {
fprintf(stderr, "Found incomplete TLV at position 0x%zx\n", rel_pos);
break;
}
if (sample_len > sizeof(*result)) {
fprintf(stderr, "sample length %zu too long\n", sample_len);
continue;
}
result = malloc(sizeof(*result));
if (!result)
continue;
memset(result, 0, sizeof(*result));
memcpy(&result->sample, tlv, sample_len);
handled = 0;
switch (tlv->type) {
case ATH_FFT_SAMPLE_HT20:
if (sample_len != sizeof(result->sample.ht20)) {
fprintf(stderr, "wrong sample length (have %zd, expected %zd)\n",
sample_len, sizeof(result->sample.ht20));
break;
}
CONVERT_BE16(result->sample.ht20.freq);
CONVERT_BE16(result->sample.ht20.max_magnitude);
CONVERT_BE64(result->sample.ht20.tsf);
handled = 1;
break;
case ATH_FFT_SAMPLE_HT20_40:
if (sample_len != sizeof(result->sample.ht40)) {
fprintf(stderr, "wrong sample length (have %zd, expected %zd)\n",
sample_len, sizeof(result->sample.ht40));
break;
}
CONVERT_BE16(result->sample.ht40.freq);
CONVERT_BE64(result->sample.ht40.tsf);
CONVERT_BE16(result->sample.ht40.lower_max_magnitude);
CONVERT_BE16(result->sample.ht40.upper_max_magnitude);
handled = 1;
break;
case ATH_FFT_SAMPLE_ATH10K:
if (sample_len < sizeof(result->sample.ath10k.header)) {
fprintf(stderr, "wrong sample length (have %zd, expected at least %zd)\n",
sample_len, sizeof(result->sample.ath10k.header));
break;
}
bins = sample_len - sizeof(result->sample.ath10k.header);
if (bins != 64 &&
bins != 128 &&
bins != 256) {
fprintf(stderr, "invalid bin length %d\n", bins);
break;
}
/*
* Zero noise level should not happen in a real environment
* but some datasets contain it which creates bogus results.
*/
if (result->sample.ath10k.header.noise == 0)
break;
CONVERT_BE16(result->sample.ath10k.header.freq1);
CONVERT_BE16(result->sample.ath10k.header.freq2);
CONVERT_BE16(result->sample.ath10k.header.noise);
CONVERT_BE16(result->sample.ath10k.header.max_magnitude);
CONVERT_BE16(result->sample.ath10k.header.total_gain_db);
CONVERT_BE16(result->sample.ath10k.header.base_pwr_db);
CONVERT_BE64(result->sample.ath10k.header.tsf);
handled = 1;
break;
case ATH_FFT_SAMPLE_ATH11K:
if (sample_len < sizeof(result->sample.ath11k.header)) {
fprintf(stderr, "wrong sample length (have %zd, expected at least %zd)\n",
sample_len, sizeof(result->sample.ath11k.header));
break;
}
bins = sample_len - sizeof(result->sample.ath11k.header);
if (bins != 16 &&
bins != 32 &&
bins != 64 &&
bins != 128 &&
bins != 256 &&
bins != 512) {
fprintf(stderr, "invalid bin length %d\n", bins);
break;
}
/*
* Zero noise level should not happen in a real environment
* but some datasets contain it which creates bogus results.
*/
if (result->sample.ath11k.header.noise == 0)
break;
CONVERT_BE16(result->sample.ath11k.header.freq1);
CONVERT_BE16(result->sample.ath11k.header.freq2);
CONVERT_BE16(result->sample.ath11k.header.max_magnitude);
CONVERT_BE16(result->sample.ath11k.header.rssi);
CONVERT_BE32(result->sample.ath11k.header.tsf);
CONVERT_BE32(result->sample.ath11k.header.noise);
handled = 1;
break;
default:
fprintf(stderr, "unknown sample type (%d)\n", tlv->type);
break;
}
if (!handled) {
free(result);
continue;
}
if (tail)
tail->next = result;
else
result_list = result;
tail = result;
scanresults_n++;
}
fprintf(stderr, "read %d scan results\n", scanresults_n);
free(scandata);
return 0;
}
void fft_eval_exit(void)
{
struct scanresult *list = result_list;
struct scanresult *next;
while (list) {
next = list->next;
free(list);
list = next;
}
result_list = NULL;
}
void fft_eval_usage(const char *prog)
{
if (!prog)
prog = "fft_eval";
fprintf(stderr, "\n");
fprintf(stderr, "scanfile is generated by the spectral analyzer feature\n");
fprintf(stderr, "of your wifi card. If you have a AR92xx or AR93xx based\n");
fprintf(stderr, "card, try:\n");
fprintf(stderr, "\n");
fprintf(stderr, "ip link set dev wlan0 up\n");
fprintf(stderr, "echo chanscan > /sys/kernel/debug/ieee80211/phy0/ath9k/spectral_scan_ctl\n");
fprintf(stderr, "iw dev wlan0 scan\n");
fprintf(stderr, "cat /sys/kernel/debug/ieee80211/phy0/ath9k/spectral_scan0 > /tmp/fft_results\n");
fprintf(stderr, "echo disable > /sys/kernel/debug/ieee80211/phy0/ath9k/spectral_scan_ctl\n");
fprintf(stderr, "%s /tmp/fft_results\n", prog);
fprintf(stderr, "\n");
fprintf(stderr, "for AR98xx based cards, you may use:\n");
fprintf(stderr, "ip link set dev wlan0 up\n");
fprintf(stderr, "echo background > /sys/kernel/debug/ieee80211/phy0/ath10k/spectral_scan_ctl\n");
fprintf(stderr, "echo trigger > /sys/kernel/debug/ieee80211/phy0/ath10k/spectral_scan_ctl\n");
fprintf(stderr, "iw dev wlan0 scan\n");
fprintf(stderr, "echo disable > /sys/kernel/debug/ieee80211/phy0/ath10k/spectral_scan_ctl\n");
fprintf(stderr, "cat /sys/kernel/debug/ieee80211/phy0/ath10k/spectral_scan0 > samples\n");
fprintf(stderr, "\n");
fprintf(stderr, "(NOTE: maybe debugfs must be mounted first: mount -t debugfs none /sys/kernel/debug/ )\n");
fprintf(stderr, "\n");
fprintf(stderr, "For ath11k based cards, use:\n");
fprintf(stderr, "ip link set dev wlan0 up\n");
fprintf(stderr, "echo background > /sys/kernel/debug/ieee80211/phy0/ath11k/spectral_scan_ctl\n");
fprintf(stderr, "echo trigger > /sys/kernel/debug/ieee80211/phy0/ath11k/spectral_scan_ctl\n");
fprintf(stderr, "iw dev wlan0 scan\n");
fprintf(stderr, "echo disable > /sys/kernel/debug/ieee80211/phy0/ath11k/spectral_scan_ctl\n");
fprintf(stderr, "cat /sys/kernel/debug/ieee80211/phy0/ath11k/spectral_scan0 > samples\n");
fprintf(stderr, "\n");
fprintf(stderr, "(NOTE: maybe debugfs must be mounted first: mount -t debugfs none /sys/kernel/debug/ )\n");
fprintf(stderr, "\n");
}