forked from GPSBabel/gpsbabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brauniger_iq.cc
279 lines (244 loc) · 6.84 KB
/
brauniger_iq.cc
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
/*
* Serial download of barograph data from a Brauniger IQ Variometer.
*
* Copyright (C) 2004 Chris Jones
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "defs.h"
#include "gbser.h"
#include <cstdio>
static void* serial_handle;
#define MYNAME "BRAUNIGER-IQ"
#define PRESTRKNAME "PRESALTTRK"
namespace { // fix ODR violation with igc
enum state_t {
st_sync,
st_fl_num,
st_data_len,
st_ser_num,
st_pilot_name,
st_start_date,
st_start_year,
st_max_alt_1,
st_max_alt_2,
st_max_climb,
st_flight_dur,
st_log_ival,
st_start_time,
st_end_time,
st_sample_alt,
st_sample_spd,
num_states
};
}
static state_t state;
static const int reqd_bytes[num_states] = { 6, 1, 2, 2, 25, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1 };
static void rd_init(const QString& fname)
{
if (serial_handle = gbser_init(qPrintable(fname)), nullptr == serial_handle) {
fatal(MYNAME ": Can't open port '%s'\n", qPrintable(fname));
}
if (gbser_set_port(serial_handle, 9600, 8, 0, 1) != gbser_OK) {
fatal(MYNAME ": Can't configure port '%s'\n", qPrintable(fname));
}
}
static void rd_deinit()
{
gbser_deinit(serial_handle);
serial_handle = nullptr;
}
/**
* Process a data record.
* @return zero when all expected data has been received
*/
static int process_data(const unsigned char* data)
{
static int remaining = 100;
static struct tm tm;
static time_t start, creation;
static route_head* track;
static unsigned char interval;
time_t finish;
Waypoint* wpt = nullptr;
int i;
if (global_opts.debug_level >= 3) {
for (i = 0; i < reqd_bytes[state]; i++) {
printf("%.2x ", data[i]);
}
puts("");
}
remaining -= reqd_bytes[state];
switch (state) {
case st_sync:
if (memcmp(data, "\x30\x31\x32\x33\x34\x35", 6) != 0) {
fatal(MYNAME ": Could not synchronise\n");
}
break;
case st_fl_num:
if (global_opts.debug_level >= 1) {
printf(MYNAME ": Flight Number: %d\n", data[0]);
}
break;
case st_data_len:
remaining = (data[0] << 8) + data[1] - 2;
if (global_opts.debug_level >= 1) {
printf(MYNAME ": Data Length: %d\n", remaining);
}
break;
case st_ser_num:
if (global_opts.debug_level >= 1) {
printf(MYNAME ": Serial Number: %d\n", (data[0] << 8) + data[1]);
}
break;
case st_pilot_name:
if (global_opts.debug_level >= 1) {
printf(MYNAME ": Pilot Name: %.25s\n", data);
}
break;
case st_start_date:
i = (data[0] << 8) + data[1];
tm.tm_mday = i / 100;
tm.tm_mon = (i % 100) - 1;
break;
case st_start_year:
tm.tm_year = ((data[0] << 8) + data[1]) - 1900;
break;
case st_max_alt_1:
if (global_opts.debug_level >= 1) {
printf(MYNAME ": Max Altitude 1: %dm\n", (data[0] << 8) + data[1]);
}
break;
case st_max_alt_2:
if (global_opts.debug_level >= 1) {
printf(MYNAME ": Max Altitude 2: %dm\n", (data[0] << 8) + data[1]);
}
break;
case st_max_climb:
if (global_opts.debug_level >= 1) {
i = (data[0] << 8) + data[1];
printf(MYNAME ": Max climb: %d.%dm/s\n", i / 10, i % 10);
}
break;
case st_flight_dur:
if (global_opts.debug_level >= 1) {
i = (data[0] << 8) + data[1];
printf(MYNAME ": Flight Time: %d:%d\n", i / 100, i % 100);
}
break;
case st_log_ival:
interval = data[0];
if (global_opts.debug_level >= 1) {
printf(MYNAME ": Logging Interval: %ds\n", interval);
}
break;
case st_start_time:
i = (data[0] << 8) + data[1];
tm.tm_hour = i / 100;
tm.tm_min = (i % 100) - 1;
tm.tm_sec = 0;
creation = start = mktime(&tm);
if (global_opts.debug_level >= 1) {
printf(MYNAME ": Start Time: %s", ctime(&start));
}
break;
case st_end_time:
i = (data[0] << 8) + data[1];
tm.tm_hour = i / 100;
tm.tm_min = (i % 100) - 1;
finish = mktime(&tm);
if (global_opts.debug_level >= 1) {
printf(MYNAME ": End Time: %s", ctime(&finish));
}
if (remaining) {
track = new route_head;
track->rte_name = PRESTRKNAME;
track->rte_desc = "Brauniger-IQ Barograph";
track_add_head(track);
} else {
warning(MYNAME ": No barograph recorded for this flight\n");
}
break;
case st_sample_alt:
wpt = new Waypoint;
wpt->latitude = wpt->longitude = 0.0;
wpt->SetCreationTime(creation);
creation += interval;
wpt->altitude = (data[0] << 8) + data[1];
track_add_wpt(track, wpt);
if (global_opts.debug_level >= 2) {
printf(MYNAME ": remaining=%d, Altitude=%fm, ", remaining, wpt->altitude);
}
break;
case st_sample_spd:
if (global_opts.debug_level >= 2) {
printf("Airspeed=%dkmh\n", data[0]);
}
state = st_sample_alt;
return remaining;
default:
fatal(MYNAME ": Bad internal state\n");
}
// Iterating an enum isn't great and it's less greatr that reqd_byte[] is
// implicitly coupled to our state_t, but here we are with C code forged
// into C++.
state = static_cast<state_t> (state + 1);
return remaining;
}
static void data_read()
{
unsigned char ibuf[25];
if (global_opts.debug_level >= 0) {
puts(MYNAME ": Select recorded flight in memo mode.");
puts(MYNAME ": Press Memo button for two seconds...");
}
// Wait until something arrives
if (global_opts.debug_level >= 0) {
puts(MYNAME ": Downloading flight...");
}
// Read data until there is none left to read
state = st_sync;
for (;;) {
/* wait up to 5 seconds for more data */
int rd_cnt = gbser_read_wait(serial_handle, ibuf, reqd_bytes[state], 5000);
if (rd_cnt < 0) {
fatal(MYNAME ": Serial error\n");
} else if (rd_cnt < reqd_bytes[state]) {
fatal(MYNAME ": Incomplete download\n");
}
if (!process_data(ibuf)) {
if (global_opts.debug_level >= 0) {
puts(MYNAME " ...Finished");
}
return;
}
}
}
static QVector<arglist_t> brauniger_iq_args = {
};
ff_vecs_t brauniger_iq_vecs = {
ff_type_serial,
{ ff_cap_none, ff_cap_read, ff_cap_none},
rd_init,
nullptr,
rd_deinit,
nullptr,
data_read,
nullptr,
nullptr,
&brauniger_iq_args,
CET_CHARSET_UTF8, 1 /* master process: don't convert anything | CET-REVIEW */
, NULL_POS_OPS
};