-
Notifications
You must be signed in to change notification settings - Fork 78
/
receiver.c
409 lines (356 loc) · 12.9 KB
/
receiver.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "readsb.h"
#define RECEIVER_MAX_RANGE 800e3
uint32_t receiverHash(uint64_t id) {
uint64_t h = 0x30732349f7810465ULL ^ (4 * 0x2127599bf4325c37ULL);
h ^= mix_fasthash(id);
h -= (h >> 32);
h &= (1ULL << 32) - 1;
h -= (h >> Modes.receiver_table_hash_bits);
return h & (Modes.receiver_table_size - 1);
}
struct receiver *receiverGet(uint64_t id) {
if (!Modes.receiverTable) {
return NULL;
}
struct receiver *r = Modes.receiverTable[receiverHash(id)];
while (r && r->id != id) {
r = r->next;
}
return r;
}
struct receiver *receiverCreate(uint64_t id) {
if (!Modes.receiverTable) {
return NULL;
}
struct receiver *r = receiverGet(id);
if (r)
return r;
if (Modes.receiverCount > Modes.receiver_table_size)
return NULL;
uint32_t hash = receiverHash(id);
r = cmalloc(sizeof(struct receiver));
*r = (struct receiver) {0};
r->id = id;
r->next = Modes.receiverTable[hash];
r->firstSeen = r->lastSeen = mstime();
Modes.receiverTable[hash] = r;
Modes.receiverCount++;
if (Modes.receiverCount % (Modes.receiver_table_size / 4) == 0)
fprintf(stderr, "receiverTable fill: %0.8f\n", Modes.receiverCount / (double) Modes.receiver_table_size);
if (Modes.debug_receiver && Modes.receiverCount % 128 == 0)
fprintf(stderr, "receiverCount: %"PRIu64"\n", Modes.receiverCount);
return r;
}
static void receiverDebugPrint(struct receiver *r, char *message) {
if (1) {
return;
}
fprintf(stderr, "%016"PRIx64" %9lld %6.1f %6.1f %6.1f %6.1f %s\n",
r->id, (long long) r->positionCounter,
r->latMin, r->latMax, r->lonMin, r->lonMax,
message);
}
static void receiverMaintenance(struct receiver *r) {
double decay = 0.005 * RECEIVER_MAINTENANCE_INTERVAL / SECONDS;
receiverDebugPrint(r, "beforeDecay");
// only decay if extent is pretty large
if (r->latMax - r->latMin > 10) {
r->latMax -= decay;
r->latMin += decay;
}
if (r->lonMax - r->lonMin > 10) {
r->lonMax -= decay;
r->lonMin += decay;
}
receiverDebugPrint(r, "afterDecay");
}
void receiverTimeout(int part, int nParts, int64_t now) {
if (!Modes.receiverTable) {
return;
}
int stride = Modes.receiver_table_size / nParts;
int start = stride * part;
int end = start + stride;
//fprintf(stderr, "START: %8d END: %8d\n", start, end);
for (int i = start; i < end; i++) {
struct receiver **r = &Modes.receiverTable[i];
struct receiver *del;
while (*r) {
/*
receiver *b = *r;
fprintf(stderr, "%016"PRIx64" %9"PRu64" %4.0f %4.0f %4.0f %4.0f\n",
b->id, b->positionCounter,
b->latMin, b->latMax, b->lonMin, b->lonMax);
*/
if (
(Modes.receiverCount > Modes.receiver_table_size && (*r)->lastSeen < now - 20 * MINUTES)
|| (now > (*r)->lastSeen + 24 * HOURS)
|| ((*r)->badExtent && now > (*r)->badExtent + 30 * MINUTES)
) {
del = *r;
*r = (*r)->next;
Modes.receiverCount--;
free(del);
} else {
receiverMaintenance(*r);
r = &(*r)->next;
}
}
}
}
void receiverInit() {
if (Modes.netReceiverId || Modes.netIngest || Modes.debug_no_discard || Modes.viewadsb) {
Modes.receiver_table_hash_bits = 16;
} else {
Modes.receiver_table_hash_bits = 8;
}
Modes.receiver_table_size = 1 << Modes.receiver_table_hash_bits;
Modes.receiverTable = cmalloc(Modes.receiver_table_size * sizeof(struct receiver));
memset(Modes.receiverTable, 0x0, Modes.receiver_table_size * sizeof(struct receiver));
}
void receiverCleanup() {
if (!Modes.receiverTable) {
return;
}
for (int i = 0; i < Modes.receiver_table_size; i++) {
struct receiver *r = Modes.receiverTable[i];
struct receiver *next;
while (r) {
next = r->next;
free(r);
r = next;
}
}
sfree(Modes.receiverTable);
}
int receiverPositionReceived(struct aircraft *a, struct modesMessage *mm, double lat, double lon, int64_t now) {
uint64_t id = mm->receiverId;
if (id == 0 || lat > 85.0 || lat < -85.0 || lon < -179.9 || lon > 179.9) {
return RECEIVER_RANGE_UNCLEAR;
}
int reliabilityRequired = Modes.position_persistence * 3 / 4;
if (Modes.viewadsb || Modes.receiver_focus) {
reliabilityRequired = imin(2, Modes.position_persistence);
}
// we only use ADS-B positions in the air with sufficient reliability to affect the receiver position / extent
int noModifyReceiver = (mm->source != SOURCE_ADSB || mm->cpr_type == CPR_SURFACE
|| a->pos_reliable_odd < reliabilityRequired || a->pos_reliable_even < reliabilityRequired);
struct receiver *r = receiverGet(id);
if (!r || r->positionCounter == 0) {
if (noModifyReceiver) {
return RECEIVER_RANGE_UNCLEAR;
}
r = receiverCreate(id);
if (!r) {
return RECEIVER_RANGE_UNCLEAR;
}
r->lonMin = lon;
r->lonMax = lon;
r->latMin = lat;
r->latMax = lat;
}
// diff before applying new position (we'll just get distance zero for a new receiver, this is fine)
struct receiver before = *r;
double latDiff = before.latMax - before.latMin;
double lonDiff = before.lonMax - before.lonMin;
double rlat = r->latMin + latDiff / 2;
double rlon = r->lonMin + lonDiff / 2;
double distance = greatcircle(rlat, rlon, lat, lon, 1);
if (!noModifyReceiver) {
if (distance < RECEIVER_MAX_RANGE) {
r->lonMin = fmin(r->lonMin, lon);
r->latMin = fmin(r->latMin, lat);
r->lonMax = fmax(r->lonMax, lon);
r->latMax = fmax(r->latMax, lat);
if (
before.latMin != r->latMin
|| before.latMax != r->latMax
|| before.lonMin != r->lonMin
|| before.lonMax != r->lonMax
) {
//receiverDebugPrint(r, "growingExtent");
}
r->goodCounter++;
r->badCounter = fmax(0, r->badCounter - 0.5);
}
if (!r->badExtent && distance > RECEIVER_MAX_RANGE) {
int badExtent = 1;
for (int i = 0; i < RECEIVER_BAD_AIRCRAFT; i++) {
struct bad_ac *bad = &r->badAircraft[i];
if (bad->addr == a->addr) {
badExtent = 0;
break;
}
}
for (int i = 0; i < RECEIVER_BAD_AIRCRAFT; i++) {
struct bad_ac *bad = &r->badAircraft[i];
if (now - bad->ts > 3 * MINUTES) {
// new entry
bad->ts = now;
bad->addr = a->addr;
badExtent = 0;
break;
}
}
if (badExtent) {
r->badExtent = now;
if (Modes.debug_receiver) {
char uuid[32]; // needs 18 chars and null byte
sprint_uuid1(r->id, uuid);
fprintf(stderr, "receiverBadExtent: %0.0f nmi hex: %06x id: %s #pos: %9"PRIu64" %12.5f %12.5f %4.0f %4.0f %4.0f %4.0f\n",
distance / 1852.0, a->addr, uuid, r->positionCounter,
lat, lon,
before.latMin, before.latMax,
before.lonMin, before.lonMax);
}
}
}
}
if (!noModifyReceiver) {
r->positionCounter++;
r->lastSeen = now;
}
if (distance > RECEIVER_MAX_RANGE) {
return RECEIVER_RANGE_BAD;
}
return RECEIVER_RANGE_GOOD;
}
struct receiver *receiverGetReference(uint64_t id, double *lat, double *lon, struct aircraft *a, int noDebug) {
if (!Modes.receiverTable) {
return NULL;
}
struct receiver *r = receiverGet(id);
if (!(Modes.debug_receiver && a && a->addr == Modes.cpr_focus)) {
noDebug = 1;
}
if (!r) {
if (!noDebug) {
fprintf(stderr, "id:%016"PRIx64" NOREF: receiverId not known\n", id);
}
return NULL;
}
double latDiff = r->latMax - r->latMin;
double lonDiff = r->lonMax - r->lonMin;
*lat = r->latMin + latDiff / 2;
*lon = r->lonMin + lonDiff / 2;
uint32_t positionCounterRequired = (Modes.viewadsb || Modes.receiver_focus) ? 4 : 100;
if (r->positionCounter < positionCounterRequired || r->badExtent) {
if (!noDebug) {
fprintf(stderr, "id:%016"PRIx64" NOREF: #posCounter:%9"PRIu64" refLoc: %4.0f,%4.0f lat: %4.0f to %4.0f lon: %4.0f to %4.0f\n",
r->id, r->positionCounter,
*lat, *lon,
r->latMin, r->latMax,
r->lonMin, r->lonMax);
}
return NULL;
}
if (!noDebug) {
fprintf(stderr, "id:%016"PRIx64" #posCounter:%9"PRIu64" refLoc: %4.0f,%4.0f lat: %4.0f to %4.0f lon: %4.0f to %4.0f\n",
r->id, r->positionCounter,
*lat, *lon,
r->latMin, r->latMax,
r->lonMin, r->lonMax);
}
return r;
}
void receiverTest() {
int64_t now = mstime();
for (uint64_t i = 0; i < (1<<22); i++) {
uint64_t id = i << 22;
receiver *r = receiverGet(id);
if (!r)
r = receiverCreate(id);
if (r)
r->lastSeen = now;
}
printf("%"PRIu64"\n", Modes.receiverCount);
for (int i = 0; i < (1<<22); i++) {
receiver *r = receiverGet(i);
if (!r)
r = receiverCreate(i);
}
printf("%"PRIu64"\n", Modes.receiverCount);
receiverTimeout(0, 1, mstime());
printf("%"PRIu64"\n", Modes.receiverCount);
}
int receiverCheckBad(uint64_t id, int64_t now) {
struct receiver *r = receiverGet(id);
if (r && now < r->timedOutUntil)
return 1;
else
return 0;
}
struct receiver *receiverBad(uint64_t id, uint32_t addr, int64_t now) {
if (!Modes.receiverTable) {
return NULL;
}
struct receiver *r = receiverGet(id);
if (!r)
r = receiverCreate(id);
int64_t timeout = 12 * SECONDS;
if (r && now + (timeout * 2 / 3) > r->timedOutUntil) {
r->lastSeen = now;
r->badCounter++;
if (r->badCounter > 5.99) {
r->timedOutCounter++;
if (Modes.debug_garbage) {
char uuid[32]; // needs 18 chars and null byte
sprint_uuid1(r->id, uuid);
fprintf(stderr, "timeout receiverId: %s hex: %06x #good: %6d #bad: %5.0f #timeouts: %u\n",
uuid, addr, r->goodCounter, r->badCounter, r->timedOutCounter);
}
r->timedOutUntil = now + timeout;
r->goodCounter = 0;
r->badCounter = 0;
}
return r;
} else {
return NULL;
}
}
struct char_buffer generateReceiversJson() {
struct char_buffer cb;
int64_t now = mstime();
size_t buflen = 1*1024*1024; // The initial buffer is resized as needed
char *buf = (char *) cmalloc(buflen), *p = buf, *end = buf + buflen;
p = safe_snprintf(p, end, "{ \"now\" : %.1f,\n", now / 1000.0);
//p = safe_snprintf(p, end, " \"columns\" : [ \"receiverId\", \"\"],\n");
p = safe_snprintf(p, end, " \"receivers\" : [\n");
struct receiver *r;
if (Modes.receiverTable) {
for (int j = 0; j < Modes.receiver_table_size; j++) {
for (r = Modes.receiverTable[j]; r; r = r->next) {
// check if we have enough space
if ((p + 1000) >= end) {
int used = p - buf;
buflen *= 2;
buf = (char *) realloc(buf, buflen);
p = buf + used;
end = buf + buflen;
}
char uuid[64];
sprint_uuid1(r->id, uuid);
double elapsed = (r->lastSeen - r->firstSeen) / 1000.0 + 1.0;
p = safe_snprintf(p, end, " [ \"%s\", %6.2f, %6.2f, %6.2f, %6.2f, %7.2f, %7.2f, %d, %0.2f,%0.2f ],\n",
uuid,
r->positionCounter / elapsed,
r->timedOutCounter * 3600.0 / elapsed,
r->latMin,
r->latMax,
r->lonMin,
r->lonMax,
r->badExtent ? 1 : 0,
r->latMin + (r->latMax - r->latMin) / 2.0,
r->lonMin + (r->lonMax - r->lonMin) / 2.0);
if (p >= end)
fprintf(stderr, "buffer overrun client json\n");
}
}
}
if (*(p-2) == ',')
*(p-2) = ' ';
p = safe_snprintf(p, end, " ]\n}\n");
cb.len = p - buf;
cb.buffer = buf;
return cb;
}