forked from GPSBabel/gpsbabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enigma.cc
221 lines (194 loc) · 6.22 KB
/
enigma.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
/*
Enigma route and waypoint file format.
http://www.mglavionics.co.za/Docs/Enigma%20Waypoint%20format.pdf
Binary data are stored in little endian (Intel)
Copyright (C) 2009 Tobias Kretschmar, [email protected]
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., 59 Temple Place - Suite 330, Boston, MA 02111 USA
*/
#include "defs.h"
#include <math.h>
#include <stdlib.h>
#define MYNAME "Enigma binary route and waypoint file format"
#define WTYPE_WAYPOINT 0 // Waypoint of unspecified type
#define WTYPE_AIRPORT 1 // Typical assignment for medium sized airports
#define WTYPE_MAJORAIRPORT 2 // Typical assignment for large and international airports
#define WTYPE_SEAPLANEBASE 3
#define WTYPE_AIRFIELD 4 // Typical assignment for smaller municipal airfields, glider fields etc
#define WTYPE_PRIVATEAIRFIELD 5
#define WTYPE_ULTRALIGHTFIELD 6
#define WTYPE_INTERSECTION 7 // (reporting point, boundary crossing)
#define WTYPE_HELIPORT 8
#define WTYPE_TACAN 9
#define WTYPE_NDBDME 10
#define WTYPE_NDB 11
#define WTYPE_VORDME 12
#define WTYPE_VORTAC 13
#define WTYPE_FANMARKER 14
#define WTYPE_VOR 15
#define WTYPE_REPPT 16
#define WTYPE_LFR 17
#define WTYPE_UHFNDB 18
#define WTYPE_MNDB 19
#define WTYPE_MNDBDME 20
#define WTYPE_LOM 21
#define WTYPE_LMM 22
#define WTYPE_LOCSDF 23
#define WTYPE_MLSISMLS 24
#define WTYPE_OTHERNAV 25 // Navaid not falling into any of the above types
#define WTYPE_ALTITUDECHANGE 26 // Location at which altitude should be changed
union wpt_data {
int32_t wp_altitude; // Waypoint type 0-6,8: waypoint altitude in feet
int32_t tg_altitude; // Waypoint type 26: target altitude in feet
uint32_t frequency; // Waypoint type 9-25: freq in steps of 1000Hz (118Mhz = 180000)
int32_t dummy; // waypoint type 7, unused
};
typedef struct enigma_wpt {
int32_t latitude;
int32_t longitude;
union wpt_data data;
uint8_t waypoint_type;
uint8_t shortname_len;
char shortname[6];
uint8_t longname_len;
char longname[27];
} ENIGMA_WPT;
static gbfile* file_in, *file_out;
static void
rd_init(const char* fname)
{
file_in = gbfopen_le(fname, "rb", MYNAME);
}
int32_t decToEnigmaPosition(double val)
{
int degrees = fabs(val);
double frac = fabs(val) - degrees;
int enigmadeg = degrees * 180000;
int enigmafrac = 180000 * frac;
int sign = (val < 0) ? -1 : +1;
return sign * (enigmadeg + enigmafrac);
}
float enigmaPositionToDec(int32_t val)
{
int deg = abs(val) / 180000;
int enigmafrac = abs(val) % 180000;
double frac = (double)enigmafrac / 180000;
int sign = (val < 0) ? -1 : +1;
return sign * (deg + frac);
}
static void
data_read(void)
{
struct enigma_wpt ewpt;
route_head* route = route_head_alloc();
route_add_head(route);
while (1 == gbfread(&ewpt, sizeof(ewpt), 1, file_in)) {
Waypoint* wpt = new Waypoint;
wpt->latitude = enigmaPositionToDec(le_read32(&ewpt.latitude));
wpt->longitude = enigmaPositionToDec(le_read32(&ewpt.longitude));
char*sn = xstrndup(ewpt.shortname, ewpt.shortname_len);
wpt->shortname = sn;
xfree(sn);
char* ds = xstrndup(ewpt.longname, ewpt.longname_len);
wpt->description = ds;
xfree(ds);
switch (ewpt.waypoint_type) {
case WTYPE_WAYPOINT: // 0
case WTYPE_AIRPORT: // 1
case WTYPE_MAJORAIRPORT: // 2
case WTYPE_SEAPLANEBASE: // 3
case WTYPE_AIRFIELD: // 4
case WTYPE_PRIVATEAIRFIELD: // 5
case WTYPE_ULTRALIGHTFIELD: // 6
case WTYPE_HELIPORT: // 8
// waypoint altitude
wpt->altitude = FEET_TO_METERS(le_read32(&ewpt.data.wp_altitude) - 1000);
break;
case WTYPE_ALTITUDECHANGE: // 26
// target altitude
wpt->altitude = FEET_TO_METERS(le_read32(&ewpt.data.tg_altitude) - 1000);
break;
case WTYPE_INTERSECTION: // 7
// unused
break;
default:
// frequency
// wpt->frequency = wpt.le_readu32(ewpt.data.frequency);
;
}
route_add_wpt(route, wpt);
}
}
static void
rd_deinit(void)
{
gbfclose(file_in);
}
static void
wr_init(const char* fname)
{
file_out = gbfopen_le(fname, "wb", MYNAME);
}
#ifndef min
#define min(a,b) ((a) < (b)) ? (a) : (b)
#endif
#ifndef max
#define max(a,b) ((a) > (b)) ? (a) : (b)
#endif
static void
enigma_waypt_disp(const Waypoint* wpt)
{
struct enigma_wpt ewpt;
memset(&ewpt, 0, sizeof(ewpt));
le_write32(&ewpt.latitude, decToEnigmaPosition(wpt->latitude));
le_write32(&ewpt.longitude, decToEnigmaPosition(wpt->longitude));
ewpt.waypoint_type = WTYPE_WAYPOINT;
if (wpt->altitude != unknown_alt) {
le_write32(&ewpt.data.wp_altitude, METERS_TO_FEET(wpt->altitude) + 1000);
}
if (wpt->shortname != NULL) {
ewpt.shortname_len = min(6, strlen(CSTRc(wpt->shortname)));
strncpy(ewpt.shortname, CSTRc(wpt->shortname), 6);
}
if (wpt->description != NULL) {
ewpt.longname_len = min(27, strlen(CSTRc(wpt->description)));
strncpy(ewpt.longname, CSTRc(wpt->description), 27);
}
gbfwrite(&ewpt, sizeof(ewpt), 1, file_out);
}
static void
data_write(void)
{
route_disp_all(NULL, NULL, enigma_waypt_disp);
}
static void
wr_deinit(void)
{
gbfclose(file_out);
}
ff_vecs_t enigma_vecs = {
ff_type_file,
{
ff_cap_none, /* waypoints */
ff_cap_none, /* tracks */
(ff_cap)(ff_cap_read | ff_cap_write) /* routes */
},
rd_init,
wr_init,
rd_deinit,
wr_deinit,
data_read,
data_write,
NULL,
NULL,
CET_CHARSET_ASCII, 0 /* CET-REVIEW */
};