-
Notifications
You must be signed in to change notification settings - Fork 1
/
spectool_container.c
372 lines (292 loc) · 9.41 KB
/
spectool_container.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* This code 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 2 of the License, or
* (at your option) any later version.
*
* This code 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "spectool_container.h"
#include "wispy_hw_gen1.h"
#include "wispy_hw_24x.h"
#include "wispy_hw_dbx.h"
#include "ubertooth_hw_u1.h"
int spectool_get_state(spectool_phy *phydev) {
return phydev->state;
}
char *spectool_get_error(spectool_phy *phydev) {
return phydev->errstr;
}
char *spectool_phy_getname(spectool_phy *phydev) {
if (phydev->device_spec == NULL)
return NULL;
return phydev->device_spec->device_name;
}
int spectool_phy_getdevid(spectool_phy *phydev) {
if (phydev->device_spec == NULL)
return 0;
return phydev->device_spec->device_id;
}
void spectool_phy_setname(spectool_phy *phydev, char *name) {
if (phydev->device_spec == NULL)
return;
snprintf(phydev->device_spec->device_name, SPECTOOL_PHY_NAME_MAX, "%s", name);
}
int spectool_phy_open(spectool_phy *phydev) {
if (phydev->open_func == NULL)
return 0;
return (*(phydev->open_func))(phydev);
}
int spectool_phy_close(spectool_phy *phydev) {
if (phydev->close_func == NULL)
return 0;
return (*(phydev->close_func))(phydev);
}
int spectool_phy_poll(spectool_phy *phydev) {
if (phydev->poll_func == NULL)
return SPECTOOL_POLL_ERROR;
return (*(phydev->poll_func))(phydev);
}
int spectool_phy_getpollfd(spectool_phy *phydev) {
if (phydev == NULL)
return -1;
if (phydev->pollfd_func == NULL)
return -1;
return (*(phydev->pollfd_func))(phydev);
}
void spectool_phy_setcalibration(spectool_phy *phydev, int enable) {
if (phydev->setcalib_func != NULL)
return (*(phydev->setcalib_func))(phydev, enable);
}
int spectool_phy_getflags(spectool_phy *phydev) {
return phydev->device_spec->device_flags;
}
int spectool_phy_setposition(spectool_phy *phydev, int in_profile,
int start_khz, int res_hz) {
if (phydev->setposition_func != NULL)
return (*(phydev->setposition_func))(phydev, in_profile, start_khz, res_hz);
snprintf(phydev->errstr, SPECTOOL_ERROR_MAX, "Device does not support setting "
"scan position or resolution");
return -1;
}
spectool_sample_sweep *spectool_phy_getsweep(spectool_phy *phydev) {
if (phydev->getsweep_func == NULL)
return NULL;
return (*(phydev->getsweep_func))(phydev);
}
spectool_sweep_cache *spectool_cache_alloc(int nsweeps, int calc_peak, int calc_avg) {
int x;
spectool_sweep_cache *c = (spectool_sweep_cache *) malloc(sizeof(spectool_sweep_cache));
c->sweeplist =
(spectool_sample_sweep **) malloc(sizeof(spectool_sample_sweep *) * nsweeps);
c->avg = NULL;
c->peak = NULL;
c->roll_peak = NULL;
c->latest = NULL;
for (x = 0; x < nsweeps; x++) {
c->sweeplist[x] = NULL;
}
c->num_alloc = nsweeps;
c->pos = -1;
c->looped = 0;
c->num_used = 0;
c->calc_peak = calc_peak;
c->calc_avg = calc_avg;
return c;
}
void spectool_cache_free(spectool_sweep_cache *c) {
if (c->avg != NULL)
free(c->avg);
if (c->peak != NULL)
free(c->peak);
free(c->sweeplist);
}
void spectool_cache_clear(spectool_sweep_cache *c) {
if (c->avg != NULL)
free(c->avg);
c->avg = NULL;
if (c->peak != NULL)
free(c->peak);
c->peak = NULL;
if (c->roll_peak != NULL)
free(c->roll_peak);
c->roll_peak = NULL;
c->pos = 0;
c->looped = 0;
c->num_used = 0;
}
void spectool_cache_append(spectool_sweep_cache *c, spectool_sample_sweep *s) {
int x, y, sum = 0;
int *avgdata, *avgsum;
int navg;
int nsampled;
/* Make sure we don't overflow and crash, should be sufficient
* to make sure that the new sweep is reasonable */
if (c->avg != NULL && c->avg->num_samples != s->num_samples)
return;
if (c->pos == (c->num_alloc - 1)) {
c->looped = 1;
c->pos = 0;
} else {
c->pos++;
if (c->num_used < c->num_alloc)
c->num_used++;
}
if (c->sweeplist[c->pos] != NULL) {
free(c->sweeplist[c->pos]);
}
if (c->looped)
nsampled = c->num_alloc;
else
nsampled = c->pos;
c->sweeplist[c->pos] =
(spectool_sample_sweep *) malloc(SPECTOOL_SWEEP_SIZE(s->num_samples));
memcpy(c->sweeplist[c->pos], s, SPECTOOL_SWEEP_SIZE(s->num_samples));
c->latest = c->sweeplist[c->pos];
if (c->avg == NULL && c->calc_avg) {
c->avg = (spectool_sample_sweep *) malloc(SPECTOOL_SWEEP_SIZE(s->num_samples));
memcpy(c->avg, s, SPECTOOL_SWEEP_SIZE(s->num_samples));
} else if (c->calc_avg) {
/* Reset average times */
c->avg->tm_start.tv_sec = 0;
c->avg->tm_start.tv_usec = 0;
c->avg->tm_end.tv_sec = 0;
c->avg->tm_end.tv_usec = 0;
/* Allocate a large int for summing */
avgdata = (int *) malloc(sizeof(int) * c->avg->num_samples);
avgsum = (int *) malloc(sizeof(int) * c->avg->num_samples);
for (x = 0; x < c->avg->num_samples; x++) {
avgdata[x] = 0;
avgsum[x] = 0;
}
/* Sum them up */
for (x = 0; x < nsampled; x++) {
if (c->sweeplist[x] == NULL)
continue;
for (y = 0; y < c->avg->num_samples; y++) {
avgdata[y] += c->sweeplist[x]->sample_data[y];
avgsum[y]++;
}
/* Update the time of the average network */
if (c->sweeplist[x]->tm_start.tv_sec > c->avg->tm_start.tv_sec) {
c->avg->tm_start.tv_sec = c->sweeplist[x]->tm_start.tv_sec;
c->avg->tm_start.tv_usec = c->sweeplist[x]->tm_start.tv_usec;
} else if (c->sweeplist[x]->tm_start.tv_sec == c->avg->tm_start.tv_sec &&
c->sweeplist[x]->tm_start.tv_usec > c->avg->tm_start.tv_usec) {
c->avg->tm_start.tv_usec = c->sweeplist[x]->tm_start.tv_usec;
}
if (c->sweeplist[x]->tm_end.tv_sec > c->avg->tm_end.tv_sec) {
c->avg->tm_end.tv_sec = c->sweeplist[x]->tm_end.tv_sec;
c->avg->tm_end.tv_usec = c->sweeplist[x]->tm_end.tv_usec;
} else if (c->sweeplist[x]->tm_end.tv_sec == c->avg->tm_end.tv_sec &&
c->sweeplist[x]->tm_end.tv_usec > c->avg->tm_end.tv_usec) {
c->avg->tm_end.tv_usec = c->sweeplist[x]->tm_end.tv_usec;
}
}
for (x = 0; x < c->avg->num_samples; x++) {
if (avgsum[x] == 0) {
c->avg->sample_data[x] = 0;
continue;
}
c->avg->sample_data[x] = (float) avgdata[x] / (float) avgsum[x];
}
free(avgdata);
free(avgsum);
}
/* Allocate or update the peak. We don't track peak timelines */
if (c->peak == NULL && c->calc_peak) {
c->peak = (spectool_sample_sweep *) malloc(SPECTOOL_SWEEP_SIZE(s->num_samples));
memcpy(c->peak, s, SPECTOOL_SWEEP_SIZE(s->num_samples));
/* This will never be allocated if peak is not */
c->roll_peak = (spectool_sample_sweep *) malloc(SPECTOOL_SWEEP_SIZE(s->num_samples));
memcpy(c->roll_peak, s, SPECTOOL_SWEEP_SIZE(s->num_samples));
} else if (c->calc_peak) {
for (x = 0; x < c->peak->num_samples; x++) {
if (c->peak->sample_data[x] < s->sample_data[x]) {
c->peak->sample_data[x] = s->sample_data[x];
}
}
memcpy(c->roll_peak, s, SPECTOOL_SWEEP_SIZE(s->num_samples));
for (x = 0; x < nsampled; x++) {
for (y = 0; y < c->roll_peak->num_samples; y++) {
// printf("debug - compare sampled %d pos %d loop %d set %d sample %d\n", nsampled, c->pos, c->looped, x, y);
if (c->roll_peak->sample_data[y] < c->sweeplist[x]->sample_data[y]) {
c->roll_peak->sample_data[y] = c->sweeplist[x]->sample_data[y];
}
}
}
}
}
void spectool_cache_itr_init(spectool_sweep_cache *c, spectool_sweep_cache_itr *i) {
i->pos_start = c->pos;
i->pos_cur = c->pos + 1;
i->looped_start = 0;
}
spectool_sample_sweep *spectool_cache_itr_next(spectool_sweep_cache *c, spectool_sweep_cache_itr *i) {
// if we've covered it all, punt
if (i->pos_cur == i->pos_start) {
if (i->looped_start == 0)
i->looped_start = 1;
else
return NULL;
}
// 'next' is actually backwards
i->pos_cur--;
// if we've hit the bottom but we're looped, go to the max filled slot
if (i->pos_cur < 0 && c->looped) i->pos_cur = c->num_used - 1;
// Or we're done
if (i->pos_cur < 0) return NULL;
// safety
if (i->pos_cur >= c->num_used) return NULL;
return c->sweeplist[i->pos_cur];
}
void spectool_device_scan_init(spectool_device_list *list) {
list->list = (spectool_device_rec *) malloc(sizeof(spectool_device_rec) * MAX_SCAN_RESULT);
list->num_devs = 0;
list->max_devs = MAX_SCAN_RESULT;
}
int spectool_device_scan(spectool_device_list *list) {
spectool_device_scan_init(list);
if (wispy1_usb_device_scan(list) < 0) {
return -1;
}
if (wispy24x_usb_device_scan(list) < 0) {
return -1;
}
if (wispydbx_usb_device_scan(list) < 0) {
return -1;
}
if (ubertooth_u1_device_scan(list) < 0) {
return -1;
}
return list->num_devs;
}
void spectool_device_scan_free(spectool_device_list *list) {
int x;
for (x = 0; x < list->num_devs && x < list->max_devs; x++) {
if (list->list[x].hw_rec != NULL)
free(list->list[x].hw_rec);
}
list->num_devs = 0;
list->max_devs = 0;
if (list->list != NULL)
free(list->list);
}
int spectool_device_init(spectool_phy *phydev, spectool_device_rec *rec) {
return (*(rec->init_func))(phydev, rec);
}
spectool_sample_sweep *spectool_phy_getcurprofile(spectool_phy *phydev) {
if (phydev == NULL)
return NULL;
if (phydev->device_spec->cur_profile < 0 ||
phydev->device_spec->cur_profile > phydev->device_spec->num_sweep_ranges)
return NULL;
return &(phydev->device_spec->supported_ranges[phydev->device_spec->cur_profile]);
}