| 1 |
/* OpenTRAC packet decoding by Scott Miller, N1VG |
| 2 |
Copyright (c) 2003 Scott Miller |
| 3 |
Protocol v1.0 draft, Modified 19 Jun 2003 |
| 4 |
Released under the Modified BSD license |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
#include <time.h> |
| 9 |
#include <string.h> |
| 10 |
#include "listen.h" |
| 11 |
|
| 12 |
#define MAX_UNIT_INDEX 28 |
| 13 |
|
| 14 |
static const char *units[] ={ |
| 15 |
"Volts","Amperes","Watts","Kelvins","Meters","Seconds", |
| 16 |
"Meters/Second","Liters","Kilograms","Bits/Second","Bytes","Radians", |
| 17 |
"Radians/Second","Square Meters","Joules","Newtons","Pascals","Hertz", |
| 18 |
"Meters/Sec^2","Grays","Lumens","Cubic Meters/Second", |
| 19 |
"Pascal Seconds","Kilograms/Meter^3","Radians/Second^2","Coulombs", |
| 20 |
"Farads","Siemens","Count" |
| 21 |
}; |
| 22 |
|
| 23 |
static char origin_call[7]; /* Who's talking */ |
| 24 |
static unsigned char origin_ssid; |
| 25 |
static char entity_call[7]; /* What they're talking about */ |
| 26 |
static unsigned char entity_ssid; |
| 27 |
static unsigned int entity_serial; |
| 28 |
static unsigned int entity_sequence; |
| 29 |
|
| 30 |
static int extract_ssid(char *call) |
| 31 |
{ |
| 32 |
/* Strip the SSID from the callsign and return it */ |
| 33 |
int c, ssid; |
| 34 |
|
| 35 |
for (c=ssid=0;c<6;c++) { |
| 36 |
ssid |= (call[c] & 0x80) >> (c+2); |
| 37 |
call[c] &= 0x7f; |
| 38 |
} |
| 39 |
|
| 40 |
return ssid; |
| 41 |
} |
| 42 |
|
| 43 |
/* Return values: 0 = OK, -1 = Couldn't Decode, -2 = Invalid Data */ |
| 44 |
|
| 45 |
static int decode_sequence(unsigned char *element, int element_len) |
| 46 |
{ |
| 47 |
/* 0x00 Sequence number - 16 bit integer */ |
| 48 |
if (element_len != 2 && element_len != 0) return -1; |
| 49 |
|
| 50 |
if (!element_len) { |
| 51 |
entity_sequence++; |
| 52 |
} |
| 53 |
else { |
| 54 |
entity_sequence = get16(element); |
| 55 |
} |
| 56 |
|
| 57 |
lprintf(T_OPENTRAC,"Sequence: %d\r\n",entity_sequence); |
| 58 |
|
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
static int decode_origination(unsigned char *element, int element_len) |
| 63 |
{ |
| 64 |
/* 0x01 Originating Station - Callsign, SSID, and Sequence */ |
| 65 |
memcpy(origin_call, element, 6); |
| 66 |
origin_call[6]=0; |
| 67 |
origin_ssid = extract_ssid(origin_call); |
| 68 |
if (element_len == 8) entity_sequence = get16(element+6); |
| 69 |
else entity_sequence = 0; |
| 70 |
strcpy(entity_call, origin_call); |
| 71 |
entity_ssid = origin_ssid; |
| 72 |
entity_serial = 0; |
| 73 |
lprintf(T_OPENTRAC, "Origin: %s-%d seq %d\r\n",origin_call,origin_ssid,entity_sequence); |
| 74 |
|
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
static int decode_entityid(unsigned char *element, int element_len) |
| 79 |
{ |
| 80 |
/* 0x02 Entity ID */ |
| 81 |
if (element_len > 5) { |
| 82 |
memcpy(entity_call, element, 6); |
| 83 |
entity_call[6]=0; |
| 84 |
entity_ssid = extract_ssid(entity_call); |
| 85 |
} |
| 86 |
else { |
| 87 |
strcpy(entity_call, origin_call); |
| 88 |
} |
| 89 |
|
| 90 |
switch (element_len) { |
| 91 |
case 0: |
| 92 |
entity_serial++; |
| 93 |
entity_sequence = 0; |
| 94 |
break; |
| 95 |
case 2: |
| 96 |
entity_serial = get16(element); |
| 97 |
entity_sequence = 0; |
| 98 |
break; |
| 99 |
case 4: |
| 100 |
entity_serial = get16(element); |
| 101 |
entity_sequence = get16(element+2); |
| 102 |
break; |
| 103 |
case 6: |
| 104 |
entity_serial = 0; |
| 105 |
break; |
| 106 |
case 8: |
| 107 |
entity_serial = get16(element+6); |
| 108 |
entity_sequence = 0; |
| 109 |
break; |
| 110 |
case 10: |
| 111 |
entity_serial = get16(element+6); |
| 112 |
entity_sequence = get16(element+8); |
| 113 |
break; |
| 114 |
default: |
| 115 |
return -1; |
| 116 |
} |
| 117 |
|
| 118 |
lprintf(T_OPENTRAC, "Entity %s-%d:%04x #%d\r\n", entity_call, entity_ssid, entity_serial, entity_sequence); |
| 119 |
|
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
static int decode_position(unsigned char *element, int element_len) |
| 124 |
{ |
| 125 |
/* |
| 126 |
* 0x10 Position Report - Lat/Lon/<Alt> |
| 127 |
* Lat/Lon is WGS84, 180/2^31 degrees, Alt is 1/100 meter |
| 128 |
*/ |
| 129 |
const double semicircles = 11930464.71111111111; |
| 130 |
double lat, lon; |
| 131 |
float alt = 0; |
| 132 |
|
| 133 |
lat = get32(element) / semicircles; |
| 134 |
lon = get32(element+4) / semicircles; |
| 135 |
if (lat >= 90 || lat <= -90 || lon >= 180 || lon <= -180) return -2; |
| 136 |
if (element_len == 11) { |
| 137 |
alt = ((*(element+8))<<16)+get16(element+9); |
| 138 |
alt = (alt/100)-10000; |
| 139 |
lprintf(T_OPENTRAC, "Position: Lat %f Lon %f Alt %f meters\r\n", |
| 140 |
lat,lon,alt); |
| 141 |
} |
| 142 |
else { |
| 143 |
lprintf(T_OPENTRAC, "Position: Lat %f Lon %f\r\n",lat,lon); |
| 144 |
} |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
static int decode_timestamp(unsigned char *element, int element_len) |
| 149 |
{ |
| 150 |
/* 0x11 Timestamp - Unix format time (unsigned) */ |
| 151 |
long rawtime = 0; |
| 152 |
|
| 153 |
rawtime = get32(element); |
| 154 |
lprintf(T_OPENTRAC, "Time: %s", ctime(&rawtime)); |
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
static int decode_comment(unsigned char *element, int element_len) |
| 159 |
{ |
| 160 |
/* 0x12 Freeform Comment - ASCII text */ |
| 161 |
char comment[127]; |
| 162 |
|
| 163 |
strncpy(comment, (char *)element, element_len); |
| 164 |
comment[element_len] = 0; |
| 165 |
lprintf(T_OPENTRAC, "Text: %s\r\n", comment); |
| 166 |
|
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
static int decode_courseandspeed(unsigned char *element, int element_len) |
| 171 |
{ |
| 172 |
/* 0x13 Course and Speed - Course in degrees, speed in 1/50 m/s */ |
| 173 |
unsigned int course; |
| 174 |
unsigned int rawspeed; |
| 175 |
float speed; |
| 176 |
|
| 177 |
course = (*element<<1) | ((*(element+1)&0x80) >> 7); |
| 178 |
rawspeed = get16(element+1) & 0x7fff; |
| 179 |
speed = (float)rawspeed*0.072; /* km/h */ |
| 180 |
if (course >= 360) return -2; |
| 181 |
lprintf(T_OPENTRAC, "Course: %d Speed: %f kph\r\n", course, speed); |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
static int decode_ambiguity(unsigned char *element, int element_len) |
| 187 |
{ |
| 188 |
/* 0x14 Positional Ambiguity - 16 bits, in meters */ |
| 189 |
int ambiguity; |
| 190 |
|
| 191 |
ambiguity = get16(element); |
| 192 |
lprintf(T_OPENTRAC, "Position +/- %d meters\r\n", ambiguity); |
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
static int decode_country(unsigned char *element, int element_len) |
| 197 |
{ |
| 198 |
/* 0x15 Country Code - ISO 3166-1 and optionally -2 */ |
| 199 |
char country[3]; |
| 200 |
char subdivision[4]; |
| 201 |
|
| 202 |
strncpy(country, (char *)element, 2); |
| 203 |
country[2] = 0; |
| 204 |
if (element_len > 2) { |
| 205 |
strncpy(subdivision, (char *)element+2, element_len-2); |
| 206 |
subdivision[element_len-2] = 0; |
| 207 |
lprintf(T_OPENTRAC, "Country Code %s-%s\r\n", country, subdivision); |
| 208 |
} |
| 209 |
else { |
| 210 |
lprintf(T_OPENTRAC, "Country Code %s\r\n", country); |
| 211 |
} |
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
static int decode_displayname(unsigned char *element, int element_len) |
| 216 |
{ |
| 217 |
char displayname[31]; /* 0x16 - Display Name (UTF-8 text) */ |
| 218 |
|
| 219 |
strncpy(displayname, (char *)element, element_len); |
| 220 |
displayname[element_len] = 0; |
| 221 |
|
| 222 |
lprintf(T_OPENTRAC, "Display Name: %s\r\n", displayname); |
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
static int decode_waypoint(unsigned char *element, int element_len) |
| 227 |
{ |
| 228 |
char waypoint[7]; /* 0x17 - Waypoint Name (up to 6 chars, uppercase) */ |
| 229 |
|
| 230 |
strncpy(waypoint, (char *)element, element_len); |
| 231 |
waypoint[element_len] = 0; |
| 232 |
|
| 233 |
lprintf(T_OPENTRAC, "Waypoint Name: %s\r\n", waypoint); |
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
static int decode_symbol(unsigned char *element, int element_len) |
| 238 |
{ |
| 239 |
int c; /* 0x18 Map Symbol - Packed 4-bit integers */ |
| 240 |
|
| 241 |
lprintf(T_OPENTRAC, "Symbol: "); |
| 242 |
for (c=0;c<element_len;c++) { |
| 243 |
if (c>0) lprintf(T_OPENTRAC, "."); |
| 244 |
lprintf(T_OPENTRAC, "%d", element[c] >> 4); |
| 245 |
if (element[c] & 0x0f) lprintf(T_OPENTRAC, ".%d", element[c] & 0x0f); |
| 246 |
} |
| 247 |
lprintf(T_OPENTRAC, "\r\n"); |
| 248 |
|
| 249 |
return 0; |
| 250 |
} |
| 251 |
|
| 252 |
static int decode_pathtrace(unsigned char *element, int element_len) |
| 253 |
{ |
| 254 |
char callsign[7]; /* 0x20 Path Trace - Call/SSID, Network */ |
| 255 |
int ssid, c, network; |
| 256 |
|
| 257 |
if (element_len % 7) return -1; /* Must be multiple of 7 octets */ |
| 258 |
if (!element_len) { |
| 259 |
lprintf(T_OPENTRAC, "Empty Path\r\n"); |
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
lprintf(T_OPENTRAC, "Path: "); |
| 264 |
for (c=0; c<element_len; c+=7) { |
| 265 |
memcpy(callsign, element+c, 6); |
| 266 |
ssid = extract_ssid(callsign); |
| 267 |
network = (int)*(element+c+6); |
| 268 |
lprintf(T_OPENTRAC, " %s-%d (%d)", callsign, ssid, network); |
| 269 |
} |
| 270 |
lprintf(T_OPENTRAC, "\r\n"); |
| 271 |
|
| 272 |
return 0; |
| 273 |
} |
| 274 |
|
| 275 |
static int decode_heardby(unsigned char *element, int element_len) |
| 276 |
{ |
| 277 |
int c; /* 0x21 Heard-By List */ |
| 278 |
|
| 279 |
lprintf(T_OPENTRAC, "Heard By:"); |
| 280 |
for (c=0; c<element_len; c++) { |
| 281 |
lprintf(T_OPENTRAC, " %d", (int)*(element+c)); |
| 282 |
} |
| 283 |
lprintf(T_OPENTRAC, "\r\n"); |
| 284 |
|
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
static int decode_availablenets(unsigned char *element, int element_len) |
| 289 |
{ |
| 290 |
int c; /* 0x22 Available Networks */ |
| 291 |
|
| 292 |
lprintf(T_OPENTRAC, "Available Networks:"); |
| 293 |
for (c=0; c<element_len; c++) { |
| 294 |
lprintf(T_OPENTRAC, " %d", (int)*(element+c)); |
| 295 |
} |
| 296 |
lprintf(T_OPENTRAC, "\r\n"); |
| 297 |
|
| 298 |
return 0; |
| 299 |
} |
| 300 |
|
| 301 |
static int decode_gpsquality(unsigned char *element, int element_len) |
| 302 |
{ |
| 303 |
/* 0x34 GPS Data Quality - Fix, Validity, Sats, PDOP, HDOP, VDOP */ |
| 304 |
int fixtype, validity, sats; |
| 305 |
const char *fixstr[] = {"Unknown Fix", "No Fix", "2D Fix", "3D Fix"}; |
| 306 |
const char *validstr[] = {"Invalid", "Valid SPS", "Valid DGPS", |
| 307 |
"Valid PPS"}; |
| 308 |
|
| 309 |
fixtype = (element[0] & 0xc0) >> 6; |
| 310 |
validity = (element[0] & 0x30) >> 4; |
| 311 |
sats = (element[0] & 0x0f); |
| 312 |
lprintf(T_OPENTRAC, "GPS: %s %s, %d sats", fixstr[fixtype], |
| 313 |
validstr[validity], sats); |
| 314 |
if (element_len > 1) |
| 315 |
lprintf(T_OPENTRAC, " HDOP=%.1f", (float)element[1]/10); |
| 316 |
if (element_len > 2) |
| 317 |
lprintf(T_OPENTRAC, " PDOP=%.1f", (float)element[2]/10); |
| 318 |
if (element_len > 3) |
| 319 |
lprintf(T_OPENTRAC, " VDOP=%.1f", (float)element[3]/10); |
| 320 |
lprintf(T_OPENTRAC, "\r\n"); |
| 321 |
|
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
static int decode_acreg(unsigned char *element, int element_len) |
| 326 |
{ |
| 327 |
char nnumber[9]; /* 0x35 Aircraft Registration - ASCII text */ |
| 328 |
|
| 329 |
strncpy(nnumber, (char *)element, element_len); |
| 330 |
nnumber[element_len]=0; |
| 331 |
lprintf(T_OPENTRAC, "Aircraft ID: %s\r\n", nnumber); |
| 332 |
|
| 333 |
return 0; |
| 334 |
} |
| 335 |
|
| 336 |
static int decode_rivergauge(unsigned char *element, int element_len) |
| 337 |
{ |
| 338 |
unsigned int flow; /* 0x42 River Flow Gauge - 1/64 m^3/sec */ |
| 339 |
unsigned int height; /* centimeters */ |
| 340 |
float flowm; |
| 341 |
float heightm; |
| 342 |
|
| 343 |
flow = get16(element); |
| 344 |
height = get16(element+2); |
| 345 |
flowm = (float)flow / 64; |
| 346 |
heightm = (float)height / 100; |
| 347 |
lprintf(T_OPENTRAC, "River flow rate: %f Cu M/Sec Height: %f M\r\n", |
| 348 |
flowm, heightm); |
| 349 |
|
| 350 |
return 0; |
| 351 |
} |
| 352 |
|
| 353 |
static int decode_units(unsigned int unitnum, unsigned char *element, |
| 354 |
int element_len) |
| 355 |
{ |
| 356 |
/* |
| 357 |
* 0x0500 to 0x05ff Generic Measurement Elements |
| 358 |
* Values may be 8-bit int, 16-bit int, single float, or double float |
| 359 |
*/ |
| 360 |
union measurement { |
| 361 |
char c; |
| 362 |
float f; |
| 363 |
double d; |
| 364 |
} *mval; |
| 365 |
int ival; /* too much variation in byte order and size for union */ |
| 366 |
|
| 367 |
if (unitnum > MAX_UNIT_INDEX) return -2; /* Invalid unit name */ |
| 368 |
mval = (void *)element; |
| 369 |
switch (element_len) { |
| 370 |
case 1: |
| 371 |
lprintf(T_OPENTRAC, "%d %s\r\n", mval->c, units[unitnum]); |
| 372 |
break; |
| 373 |
case 2: |
| 374 |
ival = get16(element); |
| 375 |
lprintf(T_OPENTRAC, "%d %s\r\n", ival, units[unitnum]); |
| 376 |
break; |
| 377 |
case 4: |
| 378 |
lprintf(T_OPENTRAC, "%f %s\r\n", mval->f, units[unitnum]); |
| 379 |
break; |
| 380 |
case 8: |
| 381 |
lprintf(T_OPENTRAC, "%f %s\r\n", mval->d, units[unitnum]); |
| 382 |
break; |
| 383 |
default: |
| 384 |
return -1; |
| 385 |
} |
| 386 |
|
| 387 |
return 0; |
| 388 |
} |
| 389 |
|
| 390 |
static int flag_emergency(unsigned char *element, int element_len) |
| 391 |
{ |
| 392 |
/* 0x0100 - Emergency / Distress Call */ |
| 393 |
lprintf(T_ERROR, "* * * EMERGENCY * * *\r\n"); |
| 394 |
return 0; |
| 395 |
} |
| 396 |
|
| 397 |
static int flag_attention(unsigned char *element, int element_len) |
| 398 |
{ |
| 399 |
/* 0x0101 - Attention / Ident */ |
| 400 |
lprintf(T_PROTOCOL, " - ATTENTION - \r\n"); |
| 401 |
return 0; |
| 402 |
} |
| 403 |
|
| 404 |
static int decode_hazmat(unsigned char *element, int element_len) |
| 405 |
{ |
| 406 |
/* 0x0300 - HAZMAT (UN ID in lower 14 bits) */ |
| 407 |
int un_id; |
| 408 |
|
| 409 |
if (element_len < 2) { |
| 410 |
lprintf(T_OPENTRAC, "HAZMAT: Unknown Material\r\n"); |
| 411 |
} |
| 412 |
else { |
| 413 |
un_id = get16(element) & 0x3fff; |
| 414 |
lprintf(T_OPENTRAC, "HAZMAT: UN%04d\r\n", un_id); |
| 415 |
} |
| 416 |
return 0; |
| 417 |
} |
| 418 |
|
| 419 |
static int decode_maidenhead(unsigned char *element, int element_len) |
| 420 |
{ |
| 421 |
/* 0x32 - Maidenhead Locator (4 or 6 chars) */ |
| 422 |
char maidenhead[7]; |
| 423 |
|
| 424 |
if (element_len > 6 || !element_len) return -1; |
| 425 |
|
| 426 |
strncpy(maidenhead, (char *)element, element_len); |
| 427 |
maidenhead[element_len] = 0; |
| 428 |
|
| 429 |
lprintf(T_OPENTRAC, "Grid ID: %s\r\n", maidenhead); |
| 430 |
return 0; |
| 431 |
} |
| 432 |
|
| 433 |
static struct { |
| 434 |
unsigned int element_id; |
| 435 |
unsigned char min_size; |
| 436 |
unsigned char max_size; |
| 437 |
int (*pDecode)(unsigned char *element, int element_len); |
| 438 |
} element_decode[] = {{0x00, 0, 2, decode_sequence}, |
| 439 |
{0x01, 6, 8, decode_origination}, |
| 440 |
{0x02, 0, 10, decode_entityid}, |
| 441 |
{0x10, 8, 11, decode_position}, |
| 442 |
{0x11, 4, 4, decode_timestamp}, |
| 443 |
{0x12, 0, 127, decode_comment}, |
| 444 |
{0x13, 3, 3, decode_courseandspeed}, |
| 445 |
{0x14, 2, 2, decode_ambiguity}, |
| 446 |
{0x15, 2, 5, decode_country}, |
| 447 |
{0x16, 1, 30, decode_displayname}, |
| 448 |
{0x17, 1, 6, decode_waypoint}, |
| 449 |
{0x18, 1, 4, decode_symbol}, |
| 450 |
{0x20, 0, 127, decode_pathtrace}, |
| 451 |
{0x21, 0, 127, decode_heardby}, |
| 452 |
{0x22, 0, 127, decode_availablenets}, |
| 453 |
{0x32, 4, 6, decode_maidenhead}, |
| 454 |
{0x34, 1, 4, decode_gpsquality}, |
| 455 |
{0x35, 1, 8, decode_acreg}, |
| 456 |
{0x42, 4, 4, decode_rivergauge}, |
| 457 |
{0x100, 0, 127, flag_emergency}, |
| 458 |
{0x101, 0, 127, flag_attention}, |
| 459 |
{0x300, 0, 2, decode_hazmat}, |
| 460 |
{0, 0, 0, NULL}}, *element_decode_ptr; |
| 461 |
|
| 462 |
/* Dump an OpenTRAC packet */ |
| 463 |
void opentrac_dump(unsigned char *data, int length, int hexdump) |
| 464 |
{ |
| 465 |
int elen; |
| 466 |
int etype; |
| 467 |
int decoded = 0; |
| 468 |
|
| 469 |
lprintf(T_PROTOCOL, "OpenTRAC decode (%d bytes):\r\n", length); |
| 470 |
strcpy(origin_call, "SENDER"); /* Listen doesn't tell us the sender */ |
| 471 |
origin_ssid = 0; |
| 472 |
entity_serial = 0; |
| 473 |
entity_sequence = 0; |
| 474 |
while (decoded < length) { |
| 475 |
elen = (int)*data; |
| 476 |
decoded += (elen & 0x7f)+1; |
| 477 |
if (elen & 0x80) { /* See if it's got a 16-bit ID */ |
| 478 |
elen = (elen & 0x7f) - 2; /* Strip the extid flag */ |
| 479 |
etype = get16(++data); |
| 480 |
} |
| 481 |
else { |
| 482 |
elen--; /* Don't count the type byte */ |
| 483 |
etype = (int)*(data+1); |
| 484 |
} |
| 485 |
data+=2; /* Skip to the body */ |
| 486 |
lprintf(T_OPENTRAC, "EID 0x%0x len %d: ", etype, elen); |
| 487 |
for (element_decode_ptr = element_decode; element_decode_ptr->pDecode != NULL; element_decode_ptr++) { |
| 488 |
if (etype == element_decode_ptr->element_id) { |
| 489 |
if (elen < element_decode_ptr->min_size || elen > element_decode_ptr->max_size) { |
| 490 |
lprintf(T_OPENTRAC, "Invalid element length\r\n"); |
| 491 |
} |
| 492 |
else { |
| 493 |
if (element_decode_ptr->pDecode(data, elen)) { |
| 494 |
lprintf(T_OPENTRAC, "Unable to decode\r\n"); |
| 495 |
} |
| 496 |
} |
| 497 |
break; |
| 498 |
} |
| 499 |
} |
| 500 |
if (element_decode_ptr->pDecode == NULL) { |
| 501 |
if ((etype & 0xff00) == 0x500) { |
| 502 |
decode_units(etype & 0x00ff, data, elen); |
| 503 |
} |
| 504 |
else { |
| 505 |
lprintf(T_OPENTRAC, "Unknown element type - unable to decode.\r\n"); |
| 506 |
} |
| 507 |
} |
| 508 |
data+=elen; |
| 509 |
} |
| 510 |
} |