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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
/* OpenTRAC packet decoding by Scott Miller, N1VG
Copyright (c) 2003 Scott Miller
Protocol v1.0 draft, Modified 19 Jun 2003
Released under the Modified BSD license
*/
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "listen.h"
#define MAX_UNIT_INDEX 28
const char *units[]={"Volts","Amperes","Watts","Kelvins","Meters","Seconds",
"Meters/Second","Liters","Kilograms","Bits/Second","Bytes","Radians",
"Radians/Second","Square Meters","Joules","Newtons","Pascals","Hertz",
"Meters/Sec^2","Grays","Lumens","Cubic Meters/Second",
"Pascal Seconds","Kilograms/Meter^3","Radians/Second^2","Coulombs",
"Farads","Siemens","Count"};
char origin_call[7]; /* Who's talking */
unsigned char origin_ssid;
char entity_call[7]; /* What they're talking about */
unsigned char entity_ssid;
unsigned int entity_serial;
unsigned int entity_sequence;
int extract_ssid(char *call) {
/* Strip the SSID from the callsign and return it */
int c, ssid;
for (c=ssid=0;c<6;c++) {
ssid |= (call[c] & 0x80) >> (c+2);
call[c] &= 0x7f;
}
return ssid;
}
int decode_units(unsigned int unitnum, unsigned char *element, int element_len);
/* Return values: 0 = OK, -1 = Couldn't Decode, -2 = Invalid Data */
int decode_sequence(unsigned char *element, int element_len) {
/* 0x00 Sequence number - 16 bit integer */
if (element_len != 2 && element_len != 0) return -1;
if (!element_len) {
entity_sequence++;
}
else {
entity_sequence = get16(element);
}
lprintf(T_OPENTRAC,"Sequence: %d\r\n",entity_sequence);
return 0;
}
int decode_origination(unsigned char *element, int element_len) {
/* 0x01 Originating Station - Callsign, SSID, and Sequence */
memcpy(origin_call, element, 6);
origin_call[6]=0;
origin_ssid = extract_ssid(origin_call);
if (element_len == 8) entity_sequence = get16(element+6);
else entity_sequence = 0;
strcpy(entity_call, origin_call);
entity_ssid = origin_ssid;
entity_serial = 0;
lprintf(T_OPENTRAC, "Origin: %s-%d seq %d\r\n",origin_call,origin_ssid,entity_sequence);
return 0;
}
int decode_entityid(unsigned char *element, int element_len) {
/* 0x02 Entity ID */
if (element_len > 5) {
memcpy(entity_call, element, 6);
entity_call[6]=0;
entity_ssid = extract_ssid(entity_call);
}
else {
strcpy(entity_call, origin_call);
}
switch (element_len) {
case 0:
entity_serial++;
entity_sequence = 0;
break;
case 2:
entity_serial = get16(element);
entity_sequence = 0;
break;
case 4:
entity_serial = get16(element);
entity_sequence = get16(element+2);
break;
case 6:
entity_serial = 0;
break;
case 8:
entity_serial = get16(element+6);
entity_sequence = 0;
break;
case 10:
entity_serial = get16(element+6);
entity_sequence = get16(element+8);
break;
default:
return -1;
}
lprintf(T_OPENTRAC, "Entity %s-%d:%04x #%d\r\n", entity_call, entity_ssid, entity_serial, entity_sequence);
return 0;
}
int decode_position(unsigned char *element, int element_len) {
/*
* 0x10 Position Report - Lat/Lon/<Alt>
* Lat/Lon is WGS84, 180/2^31 degrees, Alt is 1/100 meter
*/
const double semicircles = 11930464.71111111111;
double lat, lon;
float alt = 0;
lat = get32(element) / semicircles;
lon = get32(element+4) / semicircles;
if (lat >= 90 || lat <= -90 || lon >= 180 || lon <= -180) return -2;
if (element_len == 11) {
alt = ((*(element+8))<<16)+get16(element+9);
alt = (alt/100)-10000;
lprintf(T_OPENTRAC, "Position: Lat %f Lon %f Alt %f meters\r\n",
lat,lon,alt);
}
else {
lprintf(T_OPENTRAC, "Position: Lat %f Lon %f\r\n",lat,lon);
}
return 0;
}
int decode_timestamp(unsigned char *element, int element_len) {
/* 0x11 Timestamp - Unix format time (unsigned) */
long rawtime = 0;
rawtime = get32(element);
lprintf(T_OPENTRAC, "Time: %s", ctime(&rawtime));
return 0;
}
int decode_comment(unsigned char *element, int element_len) {
/* 0x12 Freeform Comment - ASCII text */
char comment[127];
strncpy(comment, (char *)element, element_len);
comment[element_len] = 0;
lprintf(T_OPENTRAC, "Text: %s\r\n", comment);
return 0;
}
int decode_courseandspeed(unsigned char *element, int element_len) {
/* 0x13 Course and Speed - Course in degrees, speed in 1/50 m/s */
unsigned int course;
unsigned int rawspeed;
float speed;
course = (*element<<1) | ((*(element+1)&0x80) >> 7);
rawspeed = get16(element+1) & 0x7fff;
speed = (float)rawspeed*0.072; /* km/h */
if (course >= 360) return -2;
lprintf(T_OPENTRAC, "Course: %d Speed: %f kph\r\n", course, speed);
return 0;
}
int decode_ambiguity(unsigned char *element, int element_len) {
/* 0x14 Positional Ambiguity - 16 bits, in meters */
int ambiguity;
ambiguity = get16(element);
lprintf(T_OPENTRAC, "Position +/- %d meters\r\n", ambiguity);
return 0;
}
int decode_country(unsigned char *element, int element_len) {
/* 0x15 Country Code - ISO 3166-1 and optionally -2 */
char country[3];
char subdivision[4];
strncpy(country, (char *)element, 2);
country[2] = 0;
if (element_len > 2) {
strncpy(subdivision, (char *)element+2, element_len-2);
subdivision[element_len-2] = 0;
lprintf(T_OPENTRAC, "Country Code %s-%s\r\n", country, subdivision);
}
else {
lprintf(T_OPENTRAC, "Country Code %s\r\n", country);
}
return 0;
}
int decode_displayname(unsigned char *element, int element_len) {
char displayname[31]; /* 0x16 - Display Name (UTF-8 text) */
strncpy(displayname, (char *)element, element_len);
displayname[element_len] = 0;
lprintf(T_OPENTRAC, "Display Name: %s\r\n", displayname);
return 0;
}
int decode_waypoint(unsigned char *element, int element_len) {
char waypoint[7]; /* 0x17 - Waypoint Name (up to 6 chars, uppercase) */
strncpy(waypoint, (char *)element, element_len);
waypoint[element_len] = 0;
lprintf(T_OPENTRAC, "Waypoint Name: %s\r\n", waypoint);
return 0;
}
int decode_symbol(unsigned char *element, int element_len) {
int c; /* 0x18 Map Symbol - Packed 4-bit integers */
lprintf(T_OPENTRAC, "Symbol: ");
for (c=0;c<element_len;c++) {
if (c>0) lprintf(T_OPENTRAC, ".");
lprintf(T_OPENTRAC, "%d", element[c] >> 4);
if (element[c] & 0x0f) lprintf(T_OPENTRAC, ".%d", element[c] & 0x0f);
}
lprintf(T_OPENTRAC, "\r\n");
return 0;
}
int decode_pathtrace(unsigned char *element, int element_len) {
char callsign[7]; /* 0x20 Path Trace - Call/SSID, Network */
int ssid, c, network;
if (element_len % 7) return -1; /* Must be multiple of 7 octets */
if (!element_len) {
lprintf(T_OPENTRAC, "Empty Path\r\n");
return 0;
}
lprintf(T_OPENTRAC, "Path: ");
for (c=0; c<element_len; c+=7) {
memcpy(callsign, element+c, 6);
ssid = extract_ssid(callsign);
network = (int)*(element+c+6);
lprintf(T_OPENTRAC, " %s-%d (%d)", callsign, ssid, network);
}
lprintf(T_OPENTRAC, "\r\n");
return 0;
}
int decode_heardby(unsigned char *element, int element_len) {
int c; /* 0x21 Heard-By List */
lprintf(T_OPENTRAC, "Heard By:");
for (c=0; c<element_len; c++) {
lprintf(T_OPENTRAC, " %d", (int)*(element+c));
}
lprintf(T_OPENTRAC, "\r\n");
return 0;
}
int decode_availablenets(unsigned char *element, int element_len) {
int c; /* 0x22 Available Networks */
lprintf(T_OPENTRAC, "Available Networks:");
for (c=0; c<element_len; c++) {
lprintf(T_OPENTRAC, " %d", (int)*(element+c));
}
lprintf(T_OPENTRAC, "\r\n");
return 0;
}
int decode_gpsquality(unsigned char *element, int element_len) {
/* 0x34 GPS Data Quality - Fix, Validity, Sats, PDOP, HDOP, VDOP */
int fixtype, validity, sats;
const char *fixstr[] = {"Unknown Fix", "No Fix", "2D Fix", "3D Fix"};
const char *validstr[] = {"Invalid", "Valid SPS", "Valid DGPS",
"Valid PPS"};
fixtype = (element[0] & 0xc0) >> 6;
validity = (element[0] & 0x30) >> 4;
sats = (element[0] & 0x0f);
lprintf(T_OPENTRAC, "GPS: %s %s, %d sats", fixstr[fixtype],
validstr[validity], sats);
if (element_len > 1)
lprintf(T_OPENTRAC, " HDOP=%.1f", (float)element[1]/10);
if (element_len > 2)
lprintf(T_OPENTRAC, " PDOP=%.1f", (float)element[2]/10);
if (element_len > 3)
lprintf(T_OPENTRAC, " VDOP=%.1f", (float)element[3]/10);
lprintf(T_OPENTRAC, "\r\n");
return 0;
}
int decode_acreg(unsigned char *element, int element_len) {
char nnumber[9]; /* 0x35 Aircraft Registration - ASCII text */
strncpy(nnumber, (char *)element, element_len);
nnumber[element_len]=0;
lprintf(T_OPENTRAC, "Aircraft ID: %s\r\n", nnumber);
return 0;
}
int decode_rivergauge(unsigned char *element, int element_len) {
unsigned int flow; /* 0x42 River Flow Gauge - 1/64 m^3/sec */
unsigned int height; /* centimeters */
float flowm;
float heightm;
flow = get16(element);
height = get16(element+2);
flowm = (float)flow / 64;
heightm = (float)height / 100;
lprintf(T_OPENTRAC, "River flow rate: %f Cu M/Sec Height: %f M\r\n",
flowm, heightm);
return 0;
}
int decode_units(unsigned int unitnum, unsigned char *element, int element_len) {
/*
* 0x0500 to 0x05ff Generic Measurement Elements
* Values may be 8-bit int, 16-bit int, single float, or double float
*/
union measurement {
char c;
float f;
double d;
} *mval;
int ival; /* too much variation in byte order and size for union */
if (unitnum > MAX_UNIT_INDEX) return -2; /* Invalid unit name */
mval = (void *)element;
switch (element_len) {
case 1:
lprintf(T_OPENTRAC, "%d %s\r\n", mval->c, units[unitnum]);
break;
case 2:
ival = get16(element);
lprintf(T_OPENTRAC, "%d %s\r\n", ival, units[unitnum]);
break;
case 4:
lprintf(T_OPENTRAC, "%f %s\r\n", mval->f, units[unitnum]);
break;
case 8:
lprintf(T_OPENTRAC, "%f %s\r\n", mval->d, units[unitnum]);
break;
default:
return -1;
}
return 0;
}
int flag_emergency(unsigned char *element, int element_len) {
/* 0x0100 - Emergency / Distress Call */
lprintf(T_ERROR, "* * * EMERGENCY * * *\r\n");
return 0;
}
int flag_attention(unsigned char *element, int element_len) {
/* 0x0101 - Attention / Ident */
lprintf(T_PROTOCOL, " - ATTENTION - \r\n");
return 0;
}
int decode_hazmat(unsigned char *element, int element_len) {
/* 0x0300 - HAZMAT (UN ID in lower 14 bits) */
int un_id;
if (element_len < 2) {
lprintf(T_OPENTRAC, "HAZMAT: Unknown Material\r\n");
}
else {
un_id = get16(element) & 0x3fff;
lprintf(T_OPENTRAC, "HAZMAT: UN%04d\r\n", un_id);
}
return 0;
}
int decode_maidenhead(unsigned char *element, int element_len) {
/* 0x32 - Maidenhead Locator (4 or 6 chars) */
char maidenhead[7];
if (element_len > 6 || !element_len) return -1;
strncpy(maidenhead, (char *)element, element_len);
maidenhead[element_len] = 0;
lprintf(T_OPENTRAC, "Grid ID: %s\r\n", maidenhead);
return 0;
}
struct {
unsigned int element_id;
unsigned char min_size;
unsigned char max_size;
int (*pDecode)(unsigned char *element, int element_len);
} element_decode[] = {{0x00, 0, 2, decode_sequence},
{0x01, 6, 8, decode_origination},
{0x02, 0, 10, decode_entityid},
{0x10, 8, 11, decode_position},
{0x11, 4, 4, decode_timestamp},
{0x12, 0, 127, decode_comment},
{0x13, 3, 3, decode_courseandspeed},
{0x14, 2, 2, decode_ambiguity},
{0x15, 2, 5, decode_country},
{0x16, 1, 30, decode_displayname},
{0x17, 1, 6, decode_waypoint},
{0x18, 1, 4, decode_symbol},
{0x20, 0, 127, decode_pathtrace},
{0x21, 0, 127, decode_heardby},
{0x22, 0, 127, decode_availablenets},
{0x32, 4, 6, decode_maidenhead},
{0x34, 1, 4, decode_gpsquality},
{0x35, 1, 8, decode_acreg},
{0x42, 4, 4, decode_rivergauge},
{0x100, 0, 127, flag_emergency},
{0x101, 0, 127, flag_attention},
{0x300, 0, 2, decode_hazmat},
{0, 0, 0, NULL}}, *element_decode_ptr;
/* Dump an OpenTRAC packet */
void opentrac_dump(unsigned char *data, int length, int hexdump)
{
int elen;
int etype;
int decoded = 0;
lprintf(T_PROTOCOL, "OpenTRAC decode (%d bytes):\r\n", length);
strcpy(origin_call, "SENDER"); /* Listen doesn't tell us the sender */
origin_ssid = 0;
entity_serial = 0;
entity_sequence = 0;
while (decoded < length) {
elen = (int)*data;
decoded += (elen & 0x7f)+1;
if (elen & 0x80) { /* See if it's got a 16-bit ID */
elen = (elen & 0x7f) - 2; /* Strip the extid flag */
etype = get16(++data);
}
else {
elen--; /* Don't count the type byte */
etype = (int)*(data+1);
}
data+=2; /* Skip to the body */
lprintf(T_OPENTRAC, "EID 0x%0x len %d: ", etype, elen);
for (element_decode_ptr = element_decode; element_decode_ptr->pDecode != NULL; element_decode_ptr++) {
if (etype == element_decode_ptr->element_id) {
if (elen < element_decode_ptr->min_size || elen > element_decode_ptr->max_size) {
lprintf(T_OPENTRAC, "Invalid element length\r\n");
}
else {
if (element_decode_ptr->pDecode(data, elen)) {
lprintf(T_OPENTRAC, "Unable to decode\r\n");
}
}
break;
}
}
if (element_decode_ptr->pDecode == NULL) {
if ((etype & 0xff00) == 0x500) {
decode_units(etype & 0x00ff, data, elen);
}
else {
lprintf(T_OPENTRAC, "Unknown element type - unable to decode.\r\n");
}
}
data+=elen;
}
}
|