-
Notifications
You must be signed in to change notification settings - Fork 25
/
sdr_beast.c
203 lines (175 loc) · 6.09 KB
/
sdr_beast.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
// Part of readsb, a Mode-S/ADSB/TIS message decoder.
//
// sdr_beast.c: Mode-S Beast and GNS5894 support
//
// Copyright (c) 2020 Michael Wolf <[email protected]>
//
// This file 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
// any later version.
//
// This file 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 <termios.h>
#include "readsb.h"
#include "sdr_beast.h"
static struct {
bool filter_df045;
bool filter_df1117;
bool mode_ac;
bool mlat_timestamp;
bool fec;
bool crc;
uint16_t padding;
} BeastSettings;
static void beastSetOption(char opt) {
char optionsmsg[3] = {0x1a, '1', opt};
if (write(Modes.beast_fd, optionsmsg, 3) < 3) {
fprintf(stderr, "Beast failed to set option: %s\n", strerror(errno));
}
}
void beastInitConfig(void) {
free(Modes.beast_serial);
Modes.beast_serial = strdup("/dev/ttyUSB0");
Modes.beast_baudrate = 0;
BeastSettings.filter_df045 = false;
BeastSettings.filter_df1117 = false;
BeastSettings.mode_ac = false;
Modes.mode_ac = 0;
BeastSettings.mlat_timestamp = true;
BeastSettings.fec = true;
BeastSettings.crc = true;
}
bool beastHandleOption(int argc, char *argv) {
switch (argc) {
case OptBeastSerial:
free(Modes.beast_serial);
Modes.beast_serial = strdup(argv);
break;
case OptBeastBaudrate:
Modes.beast_baudrate = (int) strtoll(argv, NULL, 10);
break;
case OptBeastDF1117:
BeastSettings.filter_df1117 = true;
break;
case OptBeastDF045:
BeastSettings.filter_df045 = true;
break;
case OptBeastMlatTimeOff:
BeastSettings.mlat_timestamp = false;
break;
case OptBeastCrcOff:
BeastSettings.crc = false;
break;
case OptBeastFecOff:
BeastSettings.fec = false;
break;
case OptBeastModeAc:
BeastSettings.mode_ac = true;
Modes.mode_ac = 1;
Modes.mode_ac_auto = 0;
break;
}
return true;
}
bool beastOpen(void) {
struct termios tios;
speed_t baud = B3000000;
Modes.beast_fd = open(Modes.beast_serial, O_RDWR | O_NOCTTY);
if (Modes.beast_fd < 0) {
fprintf(stderr, "Failed to open serial device %s: %s\n",
Modes.beast_serial, strerror(errno));
fprintf(stderr, "In case of permission denied try: sudo chmod a+rw %s\nor permanent permission: sudo adduser readsb dialout\n", Modes.beast_serial);
return false;
}
if (tcgetattr(Modes.beast_fd, &tios) < 0) {
fprintf(stderr, "tcgetattr(%s): %s\n", Modes.beast_serial, strerror(errno));
return false;
}
tios.c_iflag = IGNPAR;
tios.c_oflag = 0;
tios.c_lflag = 0;
tios.c_cflag = CS8 | CRTSCTS;
tios.c_cc[VMIN] = 11;
tios.c_cc[VTIME] = 0;
if (Modes.sdr_type == SDR_GNS) {
baud = B921600;
}
if (Modes.beast_baudrate) {
baud = Modes.beast_baudrate;
}
if (cfsetispeed(&tios, baud) < 0) {
fprintf(stderr, "Beast cfsetispeed(%s, %d): %s\n",
Modes.beast_serial, baud, strerror(errno));
return false;
}
if (cfsetospeed(&tios, baud) < 0) {
fprintf(stderr, "Beast cfsetospeed(%s, %d): %s\n",
Modes.beast_serial, baud, strerror(errno));
return false;
}
tcflush(Modes.beast_fd, TCIFLUSH);
if (tcsetattr(Modes.beast_fd, TCSANOW, &tios) < 0) {
fprintf(stderr, "Beast tcsetattr(%s): %s\n",
Modes.beast_serial, strerror(errno));
return false;
}
if (Modes.sdr_type == SDR_MODESBEAST) {
/* set options */
beastSetOption('C'); /* use binary format */
beastSetOption('H'); /* RTS enabled */
if (BeastSettings.filter_df1117)
beastSetOption('D'); /* enable DF11/17-only filter*/
else
beastSetOption('d'); /* disable DF11/17-only filter, deliver all messages */
if (BeastSettings.mlat_timestamp)
beastSetOption('E'); /* enable mlat timestamps */
else
beastSetOption('e'); /* disable mlat timestamps */
if (BeastSettings.crc)
beastSetOption('f'); /* enable CRC checks */
else
beastSetOption('F'); /* disable CRC checks */
if (BeastSettings.filter_df045)
beastSetOption('G'); /* enable DF0/4/5 filter */
else
beastSetOption('g'); /* disable DF0/4/5 filter, deliver all messages */
if (Modes.nfix_crc || BeastSettings.fec)
beastSetOption('i'); /* FEC enabled */
else
beastSetOption('I'); /* FEC disbled */
if (Modes.mode_ac || BeastSettings.mode_ac)
beastSetOption('J'); /* Mode A/C enabled */
else
beastSetOption('j'); /* Mode A/C disabled */
}
// Request firmware message from GNS HULC
if (Modes.sdr_type == SDR_GNS) {
char optionsmsg[4] = {'#', '0', '0', '\r'};
if (write(Modes.beast_fd, optionsmsg, 4) < 4) {
fprintf(stderr, "GNS HULC request firmware failed: %s\n", strerror(errno));
}
}
/* Kick on handshake and start reception */
int RTSDTR_flag = TIOCM_RTS | TIOCM_DTR;
ioctl(Modes.beast_fd, TIOCMBIS, &RTSDTR_flag); //Set RTS&DTR pin
if (Modes.sdr_type == SDR_MODESBEAST) {
fprintf(stderr, "Running Mode-S Beast via USB.\n");
} else {
fprintf(stderr, "Running GNS HULC via USB.\n");
}
return true;
}
void beastRun() {
}
void beastClose() {
/* Beast device will be closed in the main cleanup_and_exit function when
* clients are freed.
*/
}