forked from creaktive/flare
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathp3i_decode.c
112 lines (94 loc) · 3.11 KB
/
p3i_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
/* p3i_decode, decoder for P3I (PilotAware) radio protocol
*
* Copyright (C) 2014 Stanislaw Pusep
* Copyright (C) 2017-2021 Linar Yusupov
*
* 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 <ctype.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Protocol_P3I.h"
char *p3i_decode(const p3i_packet_t *pkt, double timestamp, float rssi, int16_t channel) {
char tmp[32];
static char out[512];
out[0] = '\0';
#define json_concat(...) \
snprintf(tmp, sizeof(tmp), ##__VA_ARGS__); \
strncat(out, tmp, sizeof(out) - sizeof(tmp) - 1);
json_concat("{\"addr\":\"%X\",", pkt->icao);
if (timestamp > 1.4e9) {
json_concat("\"time\":%.06f,", timestamp);
}
if (fabs(rssi) > 0.01) {
json_concat("\"rssi\":%.01f,", rssi);
}
if (channel > 0) {
json_concat("\"channel\":%d,", channel);
}
json_concat("\"lat\":%.07f,", pkt->latitude);
json_concat("\"lon\":%.07f,", pkt->longitude);
json_concat("\"alt\":%d,", pkt->altitude);
json_concat("\"type\":%d,", pkt->aircraft);
json_concat("\"track\":%d,", pkt->track);
json_concat("\"knots\":%d}", pkt->knots);
return out;
}
int main(int argc, char **argv) {
float rssi;
double timestamp;
int16_t channel = -1;
char *line = NULL;
char *p, *q;
uint8_t buf[33];
size_t len = 0;
uint16_t i;
uint8_t offset;
while (getline(&line, &len, stdin) != -1) {
i = 0;
for (p = line, q = p + strlen(line) - 1; p < q; p++) {
if (isxdigit(*p)
&& isxdigit(*(p + 1))
&& sscanf(p, "%2hhx", &buf[i]) == 1
) {
p++;
if (++i == sizeof(buf))
break;
} else {
if (i == 31) break;
i = 0;
}
}
if (i == 31 || i == 33) {
timestamp = rssi = channel = 0;
if (p++ < q)
sscanf(p, "%lf %f %hd", ×tamp, &rssi, &channel);
offset = (buf[0] == 0x2d && buf[1] == 0xd4 ? 2 : 0);
offset += P3I_PAYLOAD_OFFSET;
q = p3i_decode(
(p3i_packet_t *) (buf + offset),
timestamp,
rssi,
channel
);
if (q) puts(q);
fflush(stdout);
} else
fprintf(stderr, "only packets with either 31 or 33 bytes are accepted (got %d)\n", i);
}
return 0;
}