forked from creaktive/flare
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathogntp_decode.cpp
129 lines (106 loc) · 3.78 KB
/
ogntp_decode.cpp
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
/* ogntp_decode, decoder for OGNTP 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 "OGN/ogn.h"
typedef struct {
/* Dummy type definition. Actual Tx/Rx packet format is defined in ogn.h */
} ogntp_packet_t;
static OGN_RxPacket Rx;
char *ogntp_decode(const ogntp_packet_t *pkt, double timestamp, float rssi, int16_t channel) {
char tmp[32];
static char out[512];
Rx.recvBytes((uint8_t *) pkt);
Rx.Packet.Dewhiten();
out[0] = '\0';
#define json_concat(...) \
snprintf(tmp, sizeof(tmp), ##__VA_ARGS__); \
strncat(out, tmp, sizeof(out) - sizeof(tmp) - 1);
json_concat("{\"addr\":\"%X\",", Rx.Packet.Header.Address);
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("{\"addr type\":\"%X\",", Rx.Packet.Header.AddrType);
// json_concat("{\"other\":\"%X\",", Rx.Packet.Header.Other);
// json_concat("{\"parity\":\"%X\",", Rx.Packet.Header.Parity);
// json_concat("{\"relay count\":\"%X\",", Rx.Packet.Header.RelayCount);
// json_concat("{\"encrypted\":\"%X\",", Rx.Packet.Header.Encrypted);
// json_concat("{\"emergency\":\"%X\",", Rx.Packet.Header.Emergency);
json_concat("\"lat\":%.07f,", 0.0001/60*Rx.Packet.DecodeLatitude());
json_concat("\"lon\":%.07f,", 0.0001/60*Rx.Packet.DecodeLongitude());
json_concat("\"alt\":%d,", Rx.Packet.DecodeAltitude());
json_concat("\"type\":%d,", Rx.Packet.Position.AcftType);
json_concat("\"stealth\":%d}", Rx.Packet.Position.Stealth);
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[29];
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 == 24) break;
i = 0;
}
}
if (i == 24 || i == 29) {
timestamp = rssi = channel = 0;
if (p++ < q)
sscanf(p, "%lf %f %hd", ×tamp, &rssi, &channel);
offset = buf[0] == 0xf3 && buf[1] == 0x65 && buf[2] == 0x6c
? 3
: 0;
q = ogntp_decode(
(ogntp_packet_t *) (buf + offset),
timestamp,
rssi,
channel
);
if (q) puts(q);
fflush(stdout);
} else
fprintf(stderr, "only packets with either 24 or 29 bytes are accepted (got %d)\n", i);
}
return 0;
}