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 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479
|
#!/usr/bin/perl
use warnings;
###########################################################################
#
#
# XASTIR, Amateur Station Tracking and Information Reporting
# Copyright (C) 2000-2023 The Xastir Group
#
# "ais.pl", a Perl script to connect aisdecoder and Xastir for receiving
# packets from ships. This script will create a UDP server at localhost:10110
# for aisdecoder to send packets into. It decodes AIS sentences, creates APRS
# packets out of them and sends them to Xastir's server port (2023) via UDP.
#
# AIS is the international tracking system for ships. They transmit on Marine
# VHF frequencies between 156.025 and 162.025, using a low power output of 1W,
# high power output of either 5W or 12.5W, at a bit rate of 9600 bits/s using
# NRZI data encoding and GMSK/FM at 0.5 modulation index.
#
# The two default AIS channels are:
# Ch 87B, AIS-1: 161.975 MHz
# Ch 88B, AIS-2: 162.025 MHz
#
# The two long-range channels are:
# Ch 75: 156.775 MHz
# Ch 76: 156.825 MHz
#
#
# You'll need "libusb", "libpthread", "librtlsdr" installed to be able to
# compile "rtl_ais".
#
# Get aisdecoder (rtl-ais).
# git clone https://github.com/dgiardini/rtl-ais/
# cd rtl_ais
# make
#
#
# Run "rtl_ais" like this (create a simple script!) for the two default channels:
#
# ./rtl_ais -h 127.0.0.1 -P 10110 -d 0 -l 161.975M -r 162.025M -n -p -2
#
# Change to marine channels 75 (156.775 MHz) and 76 (156.825 MHz) to pick
# up long-range AIS (Type 27 packets), perhaps with another dongle:
#
# ./rtl_ais -h 127.0.0.1 -P 10110 -d 1 -l 156.775M -r 156.825M -n -p -2
#
# Note that "-p -2" is the frequency error of the RTL dongle.
# Set that to the proper number determined from "Kal".
#
# "-d 0" or "-d 1" selects which dongle you're attaching to.
#
# Don't use "-g <number>" and instead use auto-gain, which appears to work
# better. This also works better than enabling AGC in the chipset itself.
#
#
# Run "ais.pl" like this (again, create a simple script!):
# ./ais.pl boats <passcode> [--pipe] [--logging]
#
# You may add the "--pipe" flag in order to have "ais.pl" accept data
# from a pipe instead of setting up a UDP server for "rtl_ais" to send
# packets to. This is very useful for debugging or for processing
# stored log files.
#
# If you add " --logging" to the end, this script will save the APRS portion of
# the output to a file called "~/.xastir/logs/ships.log". You can later suck
# this file back in to see the ships move around the map in hyperspeed. Useful
# for a quick demo.
#
#
# Antenna length should be:
# 1/2 wave dipole: 2.89' -or- 2' 10.7" -or- 2' 10 5/8"
# 1/4 wave: 1.44' -or- 1' 5.3" -or- 1' 5 5/16"
#
#
# Excellent AIS info:
# http://catb.org/gpsd/AIVDM.html # Eric Raymond/GPSD
# https://www.itu.int/dms_pubrec/itu-r/rec/m/R-REC-M.1371-5-201402-I!!PDF-E.pdf # ITU Spec
# http://www.navcen.uscg.gov/pdf/AIS/AIS_Special_Notice_and_AIS_Encoding_Guide_2012.pdf
#
# Example packets w/decoding of fields, plus large sample NMEA data file:
# http://fossies.org/linux/gpsd/test/sample.aivdm
#
# Possible live data feed (Too many connections when I try):
# ais.exploratorium.edu:80
#
###########################################################################
use IO::Socket;
use Storable;
#use File::HomeDir;
#use Data::Dumper; # Only used for debugging vessel_hash
# Enable sending APRS packets to Xastir
$enable_tx = 1;
# Enable the printing of a statistics line every 100 messages
# The format is: total type1 type2 ... type27
# Example: 3600 62 0 15 857 0 0 0 0 0 0 0 0 0 0 113 0 0 0 0 685 1868 0 0 0 0 0 0
$statistics_mode = 1;
# Turn on/off printing of various types of messages. Enables debugging of a few
# sentence types at a time w/o other messages cluttering up the output.
$print_123 = 1;
$print_4 = 1;
$print_5 = 1; # (includes vesselName and shipType)
$print_9 = 1;
$print_18 = 1;
$print_19 = 1; # (includes vesselName)
$print_21 = 1;
$print_24_A = 1; # A Variant (includes vesselName)
$print_24_B = 1; # B Variant (includes shipType
$print_27 = 1;
$print_others = 1; # Not decoded yet
# Keep statistics on each type (28 slots)
@message_type_count = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
$statistics_count = 0; # Number of messages before printing stats
# Hash to store tac-calls in, to minimize tac-call assignment packets
# This stores the complete tactical call, including whatever name
# the ship goes by currently + the country of registry.
my %tactical_hash;
# Create external copy of vessel_hash for persistent store.
# This contains only the vessel name, not the country of registry
# or the 9-digit MMSI number.
$home = `echo ~`;
chomp $home;
$persistentFileSpec = "$home/.xastir/config/vessel_hash";
#$persistentFileSpec = File::HomeDir->my_home . "/.xastir/config/vessel_hash";
#
#
# Hash to store vessel names in, for assigning tactical calls
# If it doesn't exist, create it with some initial values
# For final release this should simply create a dummy file
my %vessel_hash;
if ( !(-e $persistentFileSpec )) {
%vessel_hash = (
);
# Create the new hash file
store \%vessel_hash , $persistentFileSpec;
}
# Retrieve the cache
%vessel_hash = %{retrieve($persistentFileSpec)};
# Debug - print the hash values
#print Dumper(\%vessel_hash);
$udp_client = "xastir_udp_client";
$xastir_host = "localhost"; # Server where Xastir is running
$xastir_port = 2023; # 2023 is Xastir default UDP port
$log_file = "~/.xastir/logs/ships.log";
%countries = (
"201" => "Albania", #"Albania (Republic of)"
"202" => "Andorra", # "Andorra (Principality of)"
"203" => "Austria",
"204" => "Azores", # "Azores - Portugal"
"205" => "Belgium",
"206" => "Belarus", # "Belarus (Republic of)"
"207" => "Bulgaria", # "Bulgaria (Republic of)"
"208" => "Vatican", # "Vatican City State"
"209" => "Cyprus", # "Cyprus (Republic of)"
"210" => "Cyprus", # "Cyprus (Republic of)"
"211" => "Germany", # "Germany (Federal Republic of)"
"212" => "Cyprus", # "Cyprus (Republic of)"
"213" => "Georgia",
"214" => "Moldova", # "Moldova (Republic of)"
"215" => "Malta",
"216" => "Armenia", # "Armenia (Republic of)"
"218" => "Germany", # "Germany (Federal Republic of)"
"219" => "Denmark",
"220" => "Denmark",
"224" => "Spain",
"225" => "Spain",
"226" => "France",
"227" => "France",
"228" => "France",
"229" => "Malta",
"230" => "Finland",
"231" => "Faroe Is.", # "Faroe Islands - Denmark"
"232" => "U.K.", # "United Kingdom of Great Britain and Northern Ireland"
"233" => "U.K.", # "United Kingdom of Great Britain and Northern Ireland"
"234" => "U.K.", # "United Kingdom of Great Britain and Northern Ireland"
"235" => "U.K.", # "United Kingdom of Great Britain and Northern Ireland"
"236" => "Gibraltar", # "Gibraltar - United Kingdom of Great Britain and Northern Ireland"
"237" => "Greece",
"238" => "Croatia", # "Croatia (Republic of)"
"239" => "Greece",
"240" => "Greece",
"241" => "Greece",
"242" => "Morocco", # "Morocco (Kingdom of)"
"243" => "Hungary",
"244" => "Netherlands", # "Netherlands (Kingdom of the)"
"245" => "Netherlands", # "Netherlands (Kingdom of the)"
"246" => "Netherlands", # "Netherlands (Kingdom of the)"
"247" => "Italy",
"248" => "Malta",
"249" => "Malta",
"250" => "Ireland",
"251" => "Iceland",
"252" => "Liechtenstein", # "Liechtenstein (Principality of)"
"253" => "Luxembourg",
"254" => "Monaco", # "Monaco (Principality of)"
"255" => "Madeira", # "Madeira - Portugal"
"256" => "Malta",
"257" => "Norway",
"258" => "Norway",
"259" => "Norway",
"261" => "Poland", # "Poland (Republic of)"
"262" => "Montenegro",
"263" => "Portugal",
"264" => "Romania",
"265" => "Sweden",
"266" => "Sweden",
"267" => "Slovakia", # "Slovak Republic"
"268" => "San Marino", # "San Marino (Republic of)"
"269" => "Switzerland", # "Switzerland (Confederation of)"
"270" => "Czech Rep.", # "Czech Republic"
"271" => "Turkey",
"272" => "Ukraine",
"273" => "Russian Fed.", # "Russian Federation"
"274" => "Macedonia", # "The Former Yugoslav Republic of Macedonia"
"275" => "Latvia", # "Latvia (Republic of)"
"276" => "Estonia", # "Estonia (Republic of)"
"277" => "Lithuania", # "Lithuania (Republic of)"
"278" => "Slovenia", # "Slovenia (Republic of)"
"279" => "Serbia", # "Serbia (Republic of)"
"301" => "Anguilla", # "Anguilla - United Kingdom of Great Britain and Northern Ireland"
"303" => "Alaska,U.S.", # "Alaska (State of) - United States of America"
"304" => "Antigua & Barbuda", # "Antigua and Barbuda"
"305" => "Antigua & Barbuda", # "Antigua and Barbuda"
"306" => "Sint Maarten", # "Sint Maarten (Dutch part) - Netherlands (Kingdom of the)"
"306" => "Bonaire, Sint Eustatius & Saba", # "Bonaire, Sint Eustatius and Saba - Netherlands (Kingdom of the)"
"306" => "Curaao", # "Curaao - Netherlands (Kingdom of the)"
"307" => "Aruba", # "Aruba - Netherlands (Kingdom of the)"
"308" => "Bahamas", # "Bahamas (Commonwealth of the)"
"309" => "Bahamas", # "Bahamas (Commonwealth of the)"
"310" => "Bermuda", # "Bermuda - United Kingdom of Great Britain and Northern Ireland"
"311" => "Bahamas", # "Bahamas (Commonwealth of the)"
"312" => "Belize",
"314" => "Barbados",
"316" => "Canada",
"319" => "Cayman Is.", # "Cayman Islands - United Kingdom of Great Britain and Northern Ireland"
"321" => "Costa Rica",
"323" => "Cuba",
"325" => "Dominica", # "Dominica (Commonwealth of)"
"327" => "Dominican Rep.", # "Dominican Republic"
"329" => "Guadeloupe", # "Guadeloupe (French Department of) - France"
"330" => "Grenada",
"331" => "Greenland", # "Greenland - Denmark"
"332" => "Guatemala", # "Guatemala (Republic of)"
"334" => "Honduras", # "Honduras (Republic of)"
"336" => "Haiti", # "Haiti (Republic of)"
"338" => "U.S.", # "United States of America"
"339" => "Jamaica",
"341" => "St. Kitts & Nevis", # "Saint Kitts and Nevis (Federation of)"
"343" => "St. Lucia",
"345" => "Mexico",
"347" => "Martinique", # "Martinique (French Department of) - France"
"348" => "Montserrat", # "Montserrat - United Kingdom of Great Britain and Northern Ireland"
"350" => "Nicaragua",
"351" => "Panama", # "Panama (Republic of)"
"352" => "Panama", # "Panama (Republic of)"
"353" => "Panama", # "Panama (Republic of)"
"354" => "Panama", # "Panama (Republic of)"
"355" => "Panama", # "Panama (Republic of)"
"356" => "Panama", # "Panama (Republic of)"
"357" => "Panama", # "Panama (Republic of)"
"358" => "Puerto Rico,U.S.", # "Puerto Rico - United States of America"
"359" => "El Salvador", # "El Salvador (Republic of)"
"361" => "St. Pierre & Miquelon", # "Saint Pierre and Miquelon (Territorial Collectivity of) - France"
"362" => "Trinidad & Tobago", # "Trinidad and Tobago"
"364" => "Turks & Caicos Is.", # "Turks and Caicos Islands - United Kingdom of Great Britain and Northern Ireland"
"366" => "U.S.", # "United States of America"
"367" => "U.S.", # "United States of America"
"368" => "U.S.", # "United States of America"
"369" => "U.S.", # "United States of America"
"370" => "Panama", # "Panama (Republic of)"
"371" => "Panama", # "Panama (Republic of)"
"372" => "Panama", # "Panama (Republic of)"
"373" => "Panama", # "Panama (Republic of)"
"374" => "Panama", # "Panama (Republic of)"
"375" => "St. Vincent & Grenadines", # "Saint Vincent and the Grenadines"
"376" => "St. Vincent & Grenadines", # "Saint Vincent and the Grenadines"
"377" => "St. Vincent & Grenadines", # "Saint Vincent and the Grenadines"
"378" => "British Virgin Is.", # "British Virgin Islands - United Kingdom of Great Britain and Northern Ireland"
"379" => "U.S. Virgin Is.", # "United States Virgin Islands - United States of America"
"401" => "Afghanistan",
"403" => "Saudi Arabia", # "Saudi Arabia (Kingdom of)"
"405" => "Bangladesh", # "Bangladesh (People's Republic of)"
"408" => "Bahrain", # "Bahrain (Kingdom of)"
"410" => "Bhutan", # "Bhutan (Kingdom of)"
"412" => "China", # "China (People's Republic of)"
"413" => "China", # "China (People's Republic of)"
"414" => "China", # "China (People's Republic of)"
"416" => "Taiwan", # "Taiwan (Province of China) - China (People's Republic of)"
"417" => "Sri Lanka", # "Sri Lanka (Democratic Socialist Republic of)"
"419" => "India", # "India (Republic of)"
"422" => "Iran", # "Iran (Islamic Republic of)"
"423" => "Azerbaijan", # "Azerbaijan (Republic of)"
"425" => "Iraq", # "Iraq (Republic of)"
"428" => "Israel", # "Israel (State of)"
"431" => "Japan",
"432" => "Japan",
"434" => "Turkmenistan",
"436" => "Kazakhstan", # "Kazakhstan (Republic of)"
"437" => "Uzbekistan", # "Uzbekistan (Republic of)"
"438" => "Jordan", # "Jordan (Hashemite Kingdom of)"
"440" => "Korea", # "Korea (Republic of)"
"441" => "Korea", # "Korea (Republic of)"
"443" => "Palestine", # "State of Palestine (In accordance with Resolution 99 Rev. Guadalajara, 2010)"
"445" => "N. Korea", # "Democratic People's Republic of Korea"
"447" => "Kuwait", # "Kuwait (State of)"
"450" => "Lebanon",
"451" => "Kyrgyzstan", # "Kyrgyz Republic"
"453" => "Macao", # "Macao (Special Administrative Region of China) - China (People's Republic of)"
"455" => "Maldives", # "Maldives (Republic of)"
"457" => "Mongolia",
"459" => "Nepal", # "Nepal (Federal Democratic Republic of)"
"461" => "Oman", # "Oman (Sultanate of)"
"463" => "Pakistan", # "Pakistan (Islamic Republic of)"
"466" => "Qatar", # "Qatar (State of)"
"468" => "Syria", # "Syrian Arab Republic"
"470" => "United Arab Emirates",
"471" => "United Arab Emirates",
"472" => "Tajikistan", # "Tajikistan (Republic of)"
"473" => "Yemen", # "Yemen (Republic of)"
"475" => "Yemen", # "Yemen (Republic of)"
"477" => "Hong Kong", # "Hong Kong (Special Administrative Region of China) - China (People's Republic of)"
"478" => "Bosnia & Herzegovina", # "Bosnia and Herzegovina"
"501" => "Adelie Land", # "Adelie Land - France"
"503" => "Australia",
"506" => "Myanmar", # "Myanmar (Union of)"
"508" => "Brunei Darussalam",
"510" => "Micronesia", # "Micronesia (Federated States of)"
"511" => "Palau", # "Palau (Republic of)"
"512" => "New Zealand",
"514" => "Cambodia", # "Cambodia (Kingdom of)"
"515" => "Cambodia", # "Cambodia (Kingdom of)"
"516" => "Christmas Is.", # "Christmas Island (Indian Ocean) - Australia"
"518" => "Cook Is.", # "Cook Islands - New Zealand"
"520" => "Fiji", # "Fiji (Republic of)",
"523" => "Cocos (Keeling) Is.", # "Cocos (Keeling) Islands - Australia"
"525" => "Indonesia", # "Indonesia (Republic of)"
"529" => "Kiribati", # "Kiribati (Republic of)"
"531" => "Laos", # "Lao People's Democratic Republic"
"533" => "Malaysia",
"536" => "N. Mariana Is,U.S.", # "Northern Mariana Islands (Commonwealth of the) - United States of America"
"538" => "Marshall Is.", # "Marshall Islands (Republic of the)"
"540" => "New Caledonia", # "New Caledonia - France"
"542" => "Niue", # "Niue - New Zealand"
"544" => "Nauru", # "Nauru (Republic of)"
"546" => "French Polynesia", # "French Polynesia - France"
"548" => "Philippines", # "Philippines (Republic of the)"
"553" => "Papua New Guinea",
"555" => "Pitcairn Is.", # "Pitcairn Island - United Kingdom of Great Britain and Northern Ireland"
"557" => "Solomon Islands",
"559" => "American Samoa,U.S.", # "American Samoa - United States of America"
"561" => "Samoa", # "Samoa (Independent State of)"
"563" => "Singapore", # "Singapore (Republic of)"
"564" => "Singapore", # "Singapore (Republic of)"
"565" => "Singapore", # "Singapore (Republic of)"
"566" => "Singapore", # "Singapore (Republic of)"
"567" => "Thailand",
"570" => "Tonga", # "Tonga (Kingdom of)"
"572" => "Tuvalu",
"574" => "Viet Nam", # "Viet Nam (Socialist Republic of)"
"576" => "Vanuatu", # "Vanuatu (Republic of)"
"577" => "Vanuatu", # "Vanuatu (Republic of)"
"578" => "Wallis & Futuna Is.", # "Wallis and Futuna Islands - France"
"601" => "S. Africa", # "South Africa (Republic of)"
"603" => "Angola", # "Angola (Republic of)"
"605" => "Algeria", # "Algeria (People's Democratic Republic of)"
"607" => "St. Paul & Amsterdam Is.", # "Saint Paul and Amsterdam Islands - France"
"608" => "Ascension Is.", # "Ascension Island - United Kingdom of Great Britain and Northern Ireland"
"609" => "Burundi", # "Burundi (Republic of)"
"610" => "Benin", # "Benin (Republic of)"
"611" => "Botswana", # "Botswana (Republic of)"
"612" => "Central African Rep.", # "Central African Republic"
"613" => "Cameroon", # "Cameroon (Republic of)"
"615" => "Congo", # "Congo (Republic of the)"
"616" => "Comoros", # "Comoros (Union of the)"
"617" => "Cape Verde", # "Cabo Verde (Republic of)"
"618" => "Crozet Archipelago", # "Crozet Archipelago - France"
"619" => "Cte d'Ivoire", # "Cte d'Ivoire (Republic of)"
"620" => "Comoros", # "Comoros (Union of the)"
"621" => "Djibouti", # "Djibouti (Republic of)"
"622" => "Egypt", # "Egypt (Arab Republic of)"
"624" => "Ethiopia", # "Ethiopia (Federal Democratic Republic of)"
"625" => "Eritrea",
"626" => "Gabonese Rep.", # "Gabonese Republic"
"627" => "Ghana",
"629" => "Gambia", # "Gambia (Republic of the)"
"630" => "Guinea-Bissau", # "Guinea-Bissau (Republic of)"
"631" => "Equatorial Guinea", # "Equatorial Guinea (Republic of)"
"632" => "Guinea", # "Guinea (Republic of)"
"633" => "Burkina Faso",
"634" => "Kenya", # "Kenya (Republic of)"
"635" => "Kerguelen Is.", # "Kerguelen Islands - France"
"636" => "Liberia", # "Liberia (Republic of)"
"637" => "Liberia", # "Liberia (Republic of)"
"638" => "S. Sudan", # "South Sudan (Republic of)"
"642" => "Libya",
"644" => "Lesotho", # "Lesotho (Kingdom of)"
"645" => "Mauritius", # "Mauritius (Republic of)"
"647" => "Madagascar", # "Madagascar (Republic of)"
"649" => "Mali", # "Mali (Republic of)"
"650" => "Mozambique", # "Mozambique (Republic of)"
"654" => "Mauritania", # "Mauritania (Islamic Republic of)"
"655" => "Malawi",
"656" => "Niger", # "Niger (Republic of the)"
"657" => "Nigeria", # "Nigeria (Federal Republic of)"
"659" => "Namibia", # "Namibia (Republic of)"
"660" => "Reunion", # "Reunion (French Department of) - France"
"661" => "Rwanda", # "Rwanda (Republic of)"
"662" => "Sudan", # "Sudan (Republic of the)"
"663" => "Senegal", # "Senegal (Republic of)"
"664" => "Seychelles", # "Seychelles (Republic of)"
"665" => "St. Helena", # "Saint Helena - United Kingdom of Great Britain and Northern Ireland"
"666" => "Somalia", # "Somalia (Federal Republic of)"
"667" => "Sierra Leone",
"668" => "Sao Tome & Principe", # "Sao Tome and Principe (Democratic Republic of)"
"669" => "Swaziland", # "Swaziland (Kingdom of)"
"670" => "Chad", # "Chad (Republic of)"
"671" => "Togolese Rep.", # "Togolese Republic"
"672" => "Tunisia",
"674" => "Tanzania", # "Tanzania (United Republic of)"
"675" => "Uganda", # "Uganda (Republic of)",
"676" => "Dem. Rep. of Congo", # "Democratic Republic of the Congo"
"677" => "Tanzania", # "Tanzania (United Republic of)"
"678" => "Zambia", # "Zambia (Republic of)"
"679" => "Zimbabwe", # "Zimbabwe (Republic of)"
"701" => "Argentine Rep.", # "Argentine Republic"
"710" => "Brazil", # "Brazil (Federative Republic of)"
"720" => "Bolivia", # "Bolivia (Plurinational State of)"
"725" => "Chile",
"730" => "Columbia", # "Colombia (Republic of)"
"735" => "Ecuador",
"740" => "Falkland Is.", # "Falkland Islands (Malvinas) - United Kingdom of Great Britain and Northern Ireland"
"745" => "Guiana", # "Guiana (French Department of) - France"
"750" => "Guyana",
"755" => "Paraguay", # "Paraguay (Republic of)"
"760" => "Peru",
"765" => "Suriname", # "Suriname (Republic of)"
"770" => "Uruguay", # "Uruguay (Eastern Republic of)"
"775" => "Venezuela", # "Venezuela (Bolivarian Republic of)"
);
$xastir_user = shift;
if (defined($xastir_user)) {
chomp $xastir_user;
}
if ( (!defined($xastir_user)) || ($xastir_user eq "") ) {
print "Please enter a callsign for Xastir injection, but not Xastir's callsign/SSID!\n";
die;
}
$xastir_user =~ tr/a-z/A-Z/;
$xastir_pass = shift;
if (defined($xastir_pass)) {
chomp $xastir_pass;
}
if ( (!defined($xastir_pass)) || ($xastir_pass eq "") ) {
print "Please enter a passcode for Xastir injection\n";
die;
}
# Enable pipe mode by adding the "--pipe" flag, then can pipe
# packets into the program's STDIN for testing. Without this flag
# we'll get our packets from "rtl_ais" via a UDP server we set up.
# Enable logging mode by adding the "--logging" flag.
$pipe_mode = 0;
$logging_mode = 0;
$pipe_flag = shift;
if (defined($pipe_flag) && $pipe_flag ne "") {
chomp $pipe_flag;
if ($pipe_flag eq "--pipe") { $pipe_mode = 1; }
if ($pipe_flag eq "--logging") { $logging_mode = 1; }
}
# Try again in case there are two flags
$pipe_flag = shift;
if (defined($pipe_flag) && $pipe_flag ne "") {
chomp $pipe_flag;
if ($pipe_flag eq "--pipe") { $pipe_mode = 1; }
if ($pipe_flag eq "--logging") { $logging_mode = 1; }
}
if ($enable_tx) {
# Check Xastir's callsign/SSID to make sure we don't have a collision. This
# will prevent Xastir adopting the Items as its own and retransmitting them.
# "xastir_udp_client localhost 2023 <callsign> <passcode> -identify"
# "Received: WE7U-13"
#
$injection_call = $xastir_user;
$injection_call =~ s/-\d+//; # Get rid of dash and numbers
$injection_ssid = $xastir_user;
$injection_ssid =~ s/\w+//; # Get rid of letters
$injection_ssid =~ s/-//; # Get rid of dash
if ($injection_ssid eq "") { $injection_ssid = 0; }
# Find out Callsign/SSID of Xastir instance
$result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass -identify`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
($remote_call, $remote_ssid) = split('-', $result);
chomp($remote_call);
$remote_call =~ s/Received:\s+//;
if ( !defined($remote_ssid) ) { $remote_ssid = ""; }
if ($remote_ssid ne "") { chomp($remote_ssid); }
if ($remote_ssid eq "") { $remote_ssid = 0; }
#print "$remote_call $remote_ssid $injection_call $injection_ssid\n";
#if ($remote_call eq $injection_call) { print "Call matches\n"; }
#if ($remote_ssid == $injection_ssid) { print "SSID matches\n"; }
if ( ($remote_call eq $injection_call)
&& ($remote_ssid == $injection_ssid) ) {
$remote_ssid++;
$remote_ssid%= 16; # Increment by 1 mod 16
$xastir_user = "$remote_call-$remote_ssid";
print "Injection conflict. Corrected. New user = $xastir_user\n";
}
}
if ($pipe_mode) {
#
# Debug mode:
# Read from pipe in a loop
#
while (<STDIN>) {
&process_line($_);
}
}
else {
#
# Not debug mode:
# Create UDP server which connects to rtl_ais to get AIS data
# Listen on localhost port 10110 for UDP packets
#
my $host = "localhost";
my $port = 10110;
my $protocol = 'udp';
my $server = IO::Socket::INET->new(
#PeerAddr => $host,
LocalPort => $port,
Proto => $protocol,
Type => SOCK_DGRAM
) or die "Socket could not be created, failed with error: $!\n";
# Main processing loop. Fetch packets as they are received via
# UDP from port 10110. Create APRS packets out of the interesting
# ones and inject them into Xastir at port 2023 using UDP.
#
while ($server->recv($received_data, 1024)) {
#my $peer_address = $server->peerhost();
#my $peer_port = $server->peerport();
#print "Message was received from: $peer_address, $peer_port\n";
#print "$received_data";
&process_line($received_data);
} # End of main processing loop
}
sub process_line () {
$received_data = shift;
if ($pipe_mode) {
$received_data =~ s/\n//; # Remove LF if present
$received_data =~ s/\r//; # Remove CR if present
}
else { # Live mode, receiving from rtl-ais. CRLF line-ends.
chop($received_data);
chop($received_data);
}
# Check for empty line
if ($received_data eq "") { return; }
# Verify the checksum: Refuse to process a message if it's bad.
# Computed on entire sentence including the AIVDM tag but excluding
# the leading "!" character.
my $str = $received_data;
$str =~ s/^!//; # Chop off leading '!'
#$str =~ s/(,\d)\*../$1/; # Chop to '*' char with leading digit and two trailing chars
$str =~ s/(,\d)\*...*/$1/; # Chop to '*' char with leading digit and at least two trailing chars
#print "$str\n";
# Received checksum. Catches the correct bytes except
# for those cases where the NMEA sentences continues
# on past the checksum.
my $str2 = $received_data;
#$str2 =~ s/.*(..)/$1/; # Catches only a checksum at the end
$str2 =~ s/.*,\d\*(..).*/$1/; # Catches checksum at end or middle
# Compute the checksum on the string between the '!' and the '*' chars.
$j = length($str);
my $sum = 0;
my $i;
for ($i = 0; $i <= $j; $i++) {
my $c = ord( substr($str, $i, 1) );
$sum = $sum ^ $c;
}
my $sum2 = sprintf( "%02X", $sum);
if ($str2 ne $sum2) {
#print "$received_data\n";
#print "$str\t$str2\t$sum2\n";
printf("***** Bad checksum: Received:%s Computed:%s $received_data\n\n", $str2, $sum2);
return(); # Skip this sentence
}
else {
#print "$str2\t$sum2\n";
}
@pieces = split( ',', $received_data);
# $pieces[0] = NMEA message type
# $pieces[1] = Number of sentences
# $pieces[2] = Sequential Message ID
# $pieces[3] = Sentence number
# $pieces[4] = AIS Channel
# $pieces[5] = Encoded AIS data
# $pieces[6] = End of data & NMEA Checksum
$data = $pieces[5];
# Encoded AIS data: Each ASCII character represents 6 bits.
# Subtract 48. If still a decimal number > 40, subtract 8.
# Convert to binary.
$bin_string = "";
$len = length($data);
for ($i = 0; $i < $len; $i++) {
$c = substr($data, $i, 1);
#print("$c");
$d = ord($c) - 48;
if ($d > 40) { $d = $d - 8; }
#print "\t$d\n";
# Convert each character to 6 bits of binary.
# Add the binary strings together to make one
# long binary string. Pull out the bits we need
# to decode each piece of the message.
#
$b = &dec2bin($d);
#print "\t$b\n";
$bin_string = $bin_string . $b;
}
#print "$bin_string\n";
#printf("%d\n", length($data) );
#printf("%d\n", length($bin_string) );
# If first six bits are 000001 (message type 1):
# MMSI Number - bit 8 for 30 bits
# HDG - bit 128 for 9 bits
# COG - bit 116 for 12 bits (and divide by 10)
# SOG - bit 50 for 10 bits (and divide by 10)
# Lat - bit 89 for 27 bits (a signed binary number, divide by 600,000)
# Lon - bit 61 for 28 bits (a signed binary number, divide by 600,000)
#
#if (substr($bin_string,0,6) eq "000001") {
# print "Message Type: 1\n";
#}
#else {
# printf"Message Type: %s\n", substr($bin_string,0,6);
# die;
#}
$bmessage_type = substr($bin_string, 0, 6);
$message_type = &bin2dec($bmessage_type);
if ($statistics_mode) {
# Keep a count of the received message types
if ($message_type < 28) {
$message_type_count[$message_type] += 1;
}
# Since there's no message type 0 use it's array location for total
$message_type_count[0] += 1;
# Print the totals every 100 messages
if ( ++$statistics_count >= 100 ) {
printf("Stats: ");
for (my $jj = 0; $jj < 28; $jj++) {
if ($jj == 0) { printf("%d", $message_type_count[$jj]); }
else { printf(",%d", $message_type_count[$jj]); }
}
print "\n\n";
$statistics_count = 0;
}
}
# The escape clause for bad message types is here
# so that they get included in the total count above.
# Don't move this escape above that code.
if ( ($message_type > 27) || ($message_type == 0) ) {
# print "***** Undefined Msg Type $message_type: $received_data\n\n";
return();
}
#print "Message Type: $message_type\n";
# If not messages types 1, 2, 3, skip for now
if ( $message_type != 1
&& $message_type != 2
&& $message_type != 3
&& $message_type != 4
&& $message_type != 5
&& $message_type != 9
&& $message_type != 18
&& $message_type != 19
&& $message_type != 21
&& $message_type != 24
&& $message_type != 27
) {
# Not something we decode so don't process the sentence.
if ($print_others == 1) { print " Msg Type: $message_type\n\n"; }
return;
}
if ( $message_type == 1
|| $message_type == 2
|| $message_type == 3 ) {
if ($print_123) { print " Msg Type: $message_type\n"; }
&process_types_1_2_3($message_type);
}
elsif ( $message_type == 4 ) {
if ($print_4) { print " Msg Type: $message_type\n"; }
&process_type_4($message_type);
}
elsif ( $message_type == 5 ) {
if ($print_5) { print " Msg Type: $message_type\n"; }
&process_type_5($message_type);
}
elsif ( $message_type == 9 ) {
if ($print_9) { print " Msg Type: $message_type\n"; }
&process_type_9($message_type);
}
elsif ( $message_type == 18 ) {
if ($print_18) { print " Msg Type: $message_type\n"; }
&process_type_18($message_type);
}
elsif ( $message_type == 19 ) {
if ($print_19) { print " Msg Type: $message_type\n"; }
&process_type_19($message_type);
}
elsif ( $message_type == 21 ) {
if ($print_21) { print " Msg Type: $message_type\n"; }
&process_type_21($message_type);
}
elsif ( $message_type == 24 ) {
#if ($print_24) { print " Msg Type: $message_type\t"; } # This is printed from the subroutine instead
&process_type_24($message_type);
}
elsif ( $message_type == 27 ) {
if ($print_27) { print " Msg Type: $message_type\n"; }
if ($print_27) { print "$received_data\n"; }
&process_type_27($message_type);
}
} # End of "process_line"
# Message types 1, 2 and 3: Position Report Class A
# These messages should have 168 bits total
#
sub process_types_1_2_3() {
#if ( length($bin_string) < 168 ) {
if ( length($bin_string) < 128 ) {
#printf("123\t%s\t%d\n", $bin_string, length($bin_string));
#printf("123\t%d\n", length($bin_string));
if ($print_123) { print "Msg too short\n\n"; }
return;
}
# substr($bin_string, 0, 6); # Message Type
#my $brepeat_indicator = substr($bin_string, 6, 2);
my $bUserID = substr($bin_string, 8, 30); # MMSI
my $bnavStatus = substr($bin_string, 38, 4);
#my $brateOfTurn = substr($bin_string, 42, 8);
my $bSpeedOverGnd = substr($bin_string, 50, 10); #####
#my $bpositionAccuracy = substr($bin_string, 60, 1);
my $bLongitude = substr($bin_string, 61, 28); #####
my $bLatitude = substr($bin_string, 89, 27); #####
my $bCourseOverGnd = substr($bin_string, 116, 12); #####
#my $btrueHeading = substr($bin_string, 128, 9);
#my $btimestamp = substr($bin_string, 137, 6);
#my $bspecialManeuver = substr($bin_string, 143, 2);
#my $bspare = substr($bin_string, 145, 3);
#my $bRAIM = substr($bin_string, 148, 1);
#my $bCommState = substr($bin_string, 149, 19);
my $userID = &bin2dec($bUserID);
if ($print_123) { print " User ID: $userID\n"; }
my $country = &decode_MID($userID);
if ($print_123) { print " Country: $country\n"; }
my $navStatus = &bin2dec($bnavStatus);
my $navStatusTxt = "";
if ($navStatus == 0) { $navStatusTxt = "Under_way:engine"; }
elsif ($navStatus == 1) { $navStatusTxt = "At_anchor"; }
elsif ($navStatus == 2) { $navStatusTxt = "Not_under_command"; }
elsif ($navStatus == 3) { $navStatusTxt = "Restricted_maneuverability"; }
elsif ($navStatus == 4) { $navStatusTxt = "Constrained_by_draught"; }
elsif ($navStatus == 5) { $navStatusTxt = "Moored"; }
elsif ($navStatus == 6) { $navStatusTxt = "Aground"; }
elsif ($navStatus == 7) { $navStatusTxt = "Fishing"; }
elsif ($navStatus == 8) { $navStatusTxt = "Under_way:sailing"; }
#elsif ($navStatus == 9) { $navStatusTxt = "Reserved"; }
#elsif ($navStatus == 10) { $navStatusTxt = "Reserved"; }
#elsif ($navStatus == 11) { $navStatusTxt = "Reserved"; }
#elsif ($navStatus == 12) { $navStatusTxt = "Reserved"; }
#elsif ($navStatus == 13) { $navStatusTxt = "Reserved"; }
elsif ($navStatus == 14) { $navStatusTxt = "AIS-SART_is_active"; }
#elsif ($navStatus == 15) { $navStatusTxt = "Not defined"; }
# NOTE: 0-359 degrees
# NOTE: 360 = N/A
# APRS spec says can set to "000", "...", or " " if unknown
my $courseOverGnd = &bin2dec($bCourseOverGnd) / 10 ;
my $course = "";
if ($courseOverGnd == 360) {
$course = "...";
}
elsif ($courseOverGnd == 0) {
$course = "360";
}
else {
$course = sprintf("%03d", $courseOverGnd);
}
if ($print_123) { print " Course: $courseOverGnd\n"; }
# NOTE: 0 to 102 knots
# NOTE: 102.3 = N/A
# NOTE: 102.2 = 102.2 knots or higher
# APRS spec says can set to "000", "...", or " " if unknown
my $speedOverGnd = &bin2dec($bSpeedOverGnd) / 10;
my $speed = "";
if ($speedOverGnd == 102.3) {
$speed = "...";
}
else {
$speed = sprintf("%03d", $speedOverGnd);
}
if ($print_123) { print " Speed: $speedOverGnd\n"; }
# NOTE: -90 to +90
# NOTE: 91 = N/A
my $latitude = &signedBin2dec($bLatitude) / 600000.0;
if ($latitude == 91) {
if ($print_123) { print "\n"; }
return();
}
my $NS;
if ($print_123) { printf(" Latitude: %07.5f\n", $latitude); }
if ($latitude >= 0.0) {
$NS = 'N';
} else {
$NS = 'S';
$latitude = abs($latitude);
}
my $latdeg = int($latitude);
my $latmins = $latitude - $latdeg;
my $latmins2 = $latmins * 60.0;
my $lat = sprintf("%02d%05.2f%s", $latdeg, $latmins2, $NS);
# NOTE: -180 to +180
# NOTE: 181 = N/A
my $longitude = &signedBin2dec($bLongitude) / 600000.0;
if ($longitude == 181) {
if ($print_123) { print "\n"; }
return();
}
my $EW;
if ($print_123) { printf("Longitude: %08.5f\n", $longitude); }
if ($longitude >= 0.0) {
$EW = 'E';
} else {
$EW = 'W';
$longitude = abs($longitude);
}
my $londeg = int($longitude);
my $lonmins = $longitude - $londeg;
my $lonmins2 = $lonmins * 60.0;
my $lon = sprintf("%03d%05.2f%s", $londeg, $lonmins2, $EW);
my $symbol = "s";
# Set up to add country or vessel_name + country to comment field
my $vesselTag = " ($country)";
if ( defined($vessel_hash{$userID}) ) { $vesselTag = " " . $vessel_hash{$userID} . " (" . $country . ")"; }
my $aprs= &escape_from_shell("$xastir_user>APRS:)$userID!$lat/$lon$symbol$course/$speed $navStatusTxt$vesselTag");
if ($print_123) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
my $result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
# Assign tactical call = "$userID + $country" or "$vesselName + $country"
# Max tactical call in Xastir is 57 chars (56 + terminator?)
#
my $temp;
if ( defined( $vessel_hash{$userID}) ) {
$temp = substr($vessel_hash{$userID} . " (" . $country . ")", 0, 56); # Chop at 56 chars
}
else {
# No vessel name
# N7IPB: Display country of registry
$temp = substr("($country)", 0, 56); # Chop at 56 chars
# WE7U: Display the 9-digit MMSI + country of registry
#$temp = substr($userID . " (" . $country . ")", 0, 56); # Chop at 56 chars
}
if ( !defined($tactical_hash{$userID}) ) {
$tactical_hash{$userID} = $temp;
$aprs = &escape_from_shell($xastir_user . '>' . "APRS::TACTICAL :" . $userID . "=" . $temp);
if ($print_123) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
$result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
}
if ($print_123) { print "\n"; }
return();
}
# Message type 4: Base station report
# These messages should have 168 bits total
#
sub process_type_4() {
#if ( length($bin_string) < 168 ) {
if ( length($bin_string) < 128 ) {
#printf("123\t%s\t%d\n", $bin_string, length($bin_string));
#printf("123\t%d\n", length($bin_string));
if ($print_4) { print "Msg too short\n\n"; }
return;
}
my $bUserID = substr($bin_string, 8, 30); # MMSI
my $bLongitude = substr($bin_string, 79, 28); #####
my $bLatitude = substr($bin_string, 107, 27); #####
my $userID = &bin2dec($bUserID);
if ($print_4) { print " User ID: $userID\n"; }
my $country = &decode_MID($userID);
if ($print_4) { print " Country: $country\n"; }
# NOTE: -90 to +90
# NOTE: 91 = N/A
my $latitude = &signedBin2dec($bLatitude) / 600000.0;
if ($latitude == 91) { return(); }
my $NS;
if ($print_4) { printf(" Latitude: %07.5f\n", $latitude); }
if ($latitude >= 0.0) {
$NS = 'N';
} else {
$NS = 'S';
$latitude = abs($latitude);
}
my $latdeg = int($latitude);
my $latmins = $latitude - $latdeg;
my $latmins2 = $latmins * 60.0;
my $lat = sprintf("%02d%05.2f%s", $latdeg, $latmins2, $NS);
# NOTE: -180 to +180
# NOTE: 181 = N/A
my $longitude = &signedBin2dec($bLongitude) / 600000.0;
if ($longitude == 181) { return(); }
my $EW;
if ($print_4) { printf("Longitude: %08.5f\n", $longitude); }
if ($longitude >= 0.0) {
$EW = 'E';
} else {
$EW = 'W';
$longitude = abs($longitude);
}
my $londeg = int($longitude);
my $lonmins = $longitude - $londeg;
my $lonmins2 = $lonmins * 60.0;
my $lon = sprintf("%03d%05.2f%s", $londeg, $lonmins2, $EW);
my $symbol = "/";
# Set up to add country or vessel_name + country to comment field
my $vesselTag = " Fixed Base Station";
if ( defined($vessel_hash{$userID}) ) { $vesselTag = " " . $vessel_hash{$userID} . " (" . $country . ")"; }
my $aprs= &escape_from_shell("$xastir_user>APRS:)$userID!$lat/$lon$symbol $vesselTag");
if ($print_4) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
my $result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
# Assign tactical call = "$userID + $country" or "$vesselName + $country"
# Max tactical call in Xastir is 57 chars (56 + terminator?)
#
my $temp;
# if ( defined( $vessel_hash{$userID}) ) {
# $temp = substr($vessel_hash{$userID} . " (" . $country . ")", 0, 56); # Chop at 56 chars
# }
# else {
# # No vessel name
# # N7IPB: Display country of registry
# $temp = substr("($country)", 0, 56); # Chop at 56 chars
#
# # WE7U: Display the 9-digit MMSI + country of registry
# #$temp = substr($userID . " (" . $country . ")", 0, 56); # Chop at 56 chars
# }
# Force the tactical call to be FS since there are no names for Base Stations
#
$temp = "FS";
if ( !defined($tactical_hash{$userID}) ) {
$tactical_hash{$userID} = $temp;
$aprs = &escape_from_shell($xastir_user . '>' . "APRS::TACTICAL :" . $userID . "=" . $temp);
if ($print_4) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
$result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
}
if ($print_4) { print "\n"; }
return();
}
# Message type 5: Static and Voyage Related Data (Vessel name, callsign, ship type)
# This message should have 424 bits total
#
sub process_type_5() {
#if ( length($bin_string) < 424 ) {
if ( length($bin_string) < 240 ) {
#printf("5\t%s\t%d\n", $bin_string, length($bin_string));
#printf("5\t%d\n", length($bin_string));
if ($print_5) { print "Msg too short\n\n"; }
return;
}
# substr($bin_string, 0, 6); # Message Type
# my $brepeat_indicator = substr($bin_string, 6, 2); # Repeat Indicator
my $bUserID = substr($bin_string, 8, 30); # MMSI
# substr($bin_string, 38, 2); # AIS Version
# substr($bin_string, 40, 30); # IMO Number
#my $bCallsign = substr($bin_string, 70, 42); # Call Sign
my $bVesselName = substr($bin_string, 112, 120); # Vessel Name #####
my $bShipType = substr($bin_string, 232, 8); # Ship Type: Page 114 of ITU spec, Table 53
# substr($bin_string, 240, 9); # Dimension to Bow
# substr($bin_string, 249, 9); # Dimension to Stern
# substr($bin_string, 258, 6); # Dimension to Port
# substr($bin_string, 264, 6); # Dimension to Starboard
# substr($bin_string, 270, 4); # Position Fix Type
# substr($bin_string, 274, 4); # ETA month (UTC)
# substr($bin_string, 278, 5); # ETA day (UTC)
# substr($bin_string, 283, 5); # ETA hour (UC)
# substr($bin_string, 288, 6); # ETA minute (UTC)
# substr($bin_string, 294, 8); # Draught
# substr($bin_string, 302, 120); # Destination
# substr($bin_string, 422, 1); # DTE
# substr($bin_string, 423, 1); # Spare
my $userID = &bin2dec($bUserID);
if ($print_5) { print " User ID: $userID\n"; }
#my $callSign = &bin2text($bCallsign);
#print "Callsign: $callSign\n";
my $country = &decode_MID($userID);
if ($print_5) { print " Country: $country\n"; }
my $vesselName = &bin2text($bVesselName);
$vesselName =~ s/\s+$//; # Remove extra spaces at end
$vessel_hash{$userID} = $vesselName;
# Update the persistent store
store \%vessel_hash , $persistentFileSpec;
if ($print_5) { print " Vessel: $vesselName\n"; }
my $shipTypeTxt = &decodeShipType($bShipType);
if ($print_5) { print "Ship Type: $shipTypeTxt\n"; }
# Assign tactical call = $vesselName + $country
# Max tactical call in Xastir is 57 chars (56 + terminator?)
#
# my $temp = substr($vesselName . " (" . $country . ")", 0, 56); # Chop at 56 chars
my $temp = substr($vesselName . " ($shipTypeTxt:$country)", 0, 56); # Chop at 56 chars
if ( !defined($tactical_hash{$userID}) || $tactical_hash{$userID} ne $temp ) {
$tactical_hash{$userID} = $temp;
$aprs = &escape_from_shell($xastir_user . '>' . "APRS::TACTICAL :" . $userID . "=" . $temp);
if ($print_5) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
$result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
}
if ($print_5) { print "\n"; }
return();
}
# Message type 9: Standard SAR Aircraft Position Report
# This message should have 168 bits total
#
sub process_type_9() {
#if ( length($bin_string) < 168 ) {
if ( length($bin_string) < 128 ) {
#printf("9\t%s\t%d\n", $bin_string, length($bin_string));
#printf("9\t%d\n", length($bin_string));
if ($print_9) { print "Msg too short\n\n"; }
return;
}
# substr($bin_string, 0, 6); # Message Type
# my $brepeat_indicator = substr($bin_string, 6, 2); # Repeat Indicator
my $bUserID = substr($bin_string, 8, 30); # MMSI
my $bAltitude = substr($bin_string, 38, 12); # Altitude
my $bSpeedOverGnd = substr($bin_string, 50, 10); # SOG
# substr($bin_string, 60, 1); # Position Accuracy
my $bLongitude = substr($bin_string, 61, 28); # Longitude
my $bLatitude = substr($bin_string, 89, 27); # Latitude
my $bCourseOverGnd = substr($bin_string, 116, 12); # Course Over Ground
# substr($bin_string, 128, 6); # Time Stamp
# substr($bin_string, 134, 1); # Altitude sensor
# substr($bin_string, 135, 7); # Spare
# substr($bin_string, 142, 1); # DTE
# substr($bin_string, 143, 3); # Spare
# substr($bin_string, 146, 1); # Assigned mode
# substr($bin_string, 147, 1); # RAIM flag
# substr($bin_string, 148, 1); # Comm state selector flag
# substr($bin_string, 149, 19); # Radio status
my $userID = &bin2dec($bUserID);
if ($print_9) { print " User ID: $userID\n"; }
my $country = &decode_MID($userID);
if ($print_9) { print " Country: $country\n"; }
# NOTE: 4095 = N/A
# NOTE: 4094 = 4094 meters or higher.
my $altitude_meters = &bin2dec($bAltitude);
my $altitude = "";
if ($altitude_meters != 4095) {
if ($altitude_meters == 4094) {
$altitude = sprintf( "%s%06d%s", " /A=", $altitude_meters * 3.28084, "(Higher than)" );
}
else {
$altitude = sprintf( "%s%06d", " /A=", $altitude_meters * 3.28084 );
}
if ($print_9) { print " Altitude: $altitude\n"; }
}
# NOTE: 0-359 degrees
# NOTE: 360 = N/A
# APRS spec says can set to "000", "...", or " " if unknown
my $courseOverGnd = &bin2dec($bCourseOverGnd) / 10 ;
my $course = "";
if ($courseOverGnd == 360) {
$course = "...";
}
elsif ($courseOverGnd == 0) {
$course = "360";
}
else {
$course = sprintf("%03d", $courseOverGnd);
}
if ($print_9) { print " Course: $courseOverGnd\n"; }
# NOTE: 0 to 102 knots
# NOTE: 102.3 = N/A
# NOTE: 102.2 = 102.2 knots or higher
# APRS spec says can set to "000", "...", or " " if unknown
my $speedOverGnd = &bin2dec($bSpeedOverGnd) / 10;
my $speed = "";
if ($speedOverGnd == 102.3) {
$speed = "...";
}
else {
$speed = sprintf("%03d", $speedOverGnd);
}
if ($print_9) { print " Speed: $speedOverGnd\n"; }
# NOTE: -90 to +90
# NOTE: 91 = N/A
my $latitude = &signedBin2dec($bLatitude) / 600000.0;
if ($latitude == 91) {
if ($print_9) { print "\n"; }
return();
}
my $NS;
if ($print_9) { printf(" Latitude: %07.5f\n", $latitude); }
if ($latitude >= 0.0) {
$NS = 'N';
} else {
$NS = 'S';
$latitude = abs($latitude);
}
my $latdeg = int($latitude);
my $latmins = $latitude - $latdeg;
my $latmins2 = $latmins * 60.0;
my $lat = sprintf("%02d%05.2f%s", $latdeg, $latmins2, $NS);
# NOTE: -180 to +180
# NOTE: 181 = N/A
my $longitude = &signedBin2dec($bLongitude) / 600000.0;
if ($longitude == 181) {
if ($print_9) { print "\n"; }
return();
}
my $EW;
if ($print_9) { printf("Longitude: %08.5f\n", $longitude); }
if ($longitude >= 0.0) {
$EW = 'E';
} else {
$EW = 'W';
$longitude = abs($longitude);
}
my $londeg = int($longitude);
my $lonmins = $longitude - $londeg;
my $lonmins2 = $lonmins * 60.0;
my $lon = sprintf("%03d%05.2f%s", $londeg, $lonmins2, $EW);
my $symbol = "^"; # "^" = Large Aircraft. Could be "'" for small aircraft, "X" for helicopter.
my $vesselTag = " ($country)";
if ( defined($vessel_hash{$userID}) ) { $vesselTag = " " . $vessel_hash{$userID} . " (" . $country . ")"; }
my $aprs = &escape_from_shell("$xastir_user>APRS:)$userID!$lat/$lon$symbol$course/$speed$altitude SAR Aircraft$vesselTag");
if ($print_9) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
my $result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
# Assign tactical call = "$userID + $country" or "$vesselName + $country"
# Max tactical call in Xastir is 57 chars (56 + terminator?)
#
my $temp;
if ( defined( $vessel_hash{$userID}) ) {
$temp = substr($vessel_hash{$userID} . " (" . $country . ")", 0, 56); # Chop at 56 chars
}
else {
# No vessel name
# N7IPB: Display country of registry
$temp = substr("($country)", 0, 56); # Chop at 56 chars
# WE7U: Display the 9-digit MMSI + country of registry
#$temp = substr($userID . " (" . $country . ")", 0, 56); # Chop at 56 chars
}
if ( !defined($tactical_hash{$userID}) ) {
$tactical_hash{$userID} = $temp;
$aprs = &escape_from_shell($xastir_user . '>' . "APRS::TACTICAL :" . $userID . "=" . $temp);
if ($print_9) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
$result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
}
if ($print_9) { print "\n"; }
return();
}
# Message type 18: Standard Class B CS Position Report
# This message should have 168 bits total
#
sub process_type_18() {
#if ( length($bin_string) < 168 ) {
if ( length($bin_string) < 124 ) {
#printf("18\t%s\t%d\n", $bin_string, length($bin_string));
#printf("18\t%d\n", length($bin_string));
if ($print_18) { print "Msg too short\n\n"; }
return;
}
# substr($bin_string, 0, 6); # Message Type
# my $brepeat_indicator = substr($bin_string, 6, 2); # Repeat Indicator
my $bUserID = substr($bin_string, 8, 30); # MMSI
# substr($bin_string, 38, 8); # Spare
my $bSpeedOverGnd = substr($bin_string, 46, 10); # Speed Over Ground
# substr($bin_string, 56, 1); # Position Accuracy
my $bLongitude = substr($bin_string, 57, 28); # Longitude
my $bLatitude = substr($bin_string, 85, 27); # Latitude
my $bCourseOverGnd = substr($bin_string, 112, 12); # Course Over Ground
# substr($bin_string, 124, 9); # True Heading
# substr($bin_string, 133, 6); # Time Stamp
# substr($bin_string, 139, 2); # Spare
# substr($bin_string, 141, 1); # CS Unit
# substr($bin_string, 142, 1); # Display flag
# substr($bin_string, 143, 1); # DSC Flag
# substr($bin_string, 144, 1); # Band flag
# substr($bin_string, 145, 1); # Message 22 flag
# substr($bin_string, 146, 1); # Assigned mode
# substr($bin_string, 147, 1); # RAIM flag
# substr($bin_string, 148, 1); # Comm state selector flag
# substr($bin_string, 149, 19); # Radio status
my $userID = &bin2dec($bUserID);
if ($print_18) { print " User ID: $userID\n"; }
my $country = &decode_MID($userID);
if ($print_18) { print " Country: $country\n"; }
# NOTE: 0-359 degrees
# NOTE: 360 = N/A
# APRS spec says can set to "000", "...", or " " if unknown
my $courseOverGnd = &bin2dec($bCourseOverGnd) / 10 ;
my $course = "";
if ($courseOverGnd == 360) {
$course = "...";
}
elsif ($courseOverGnd == 0) {
$course = "360";
}
else {
$course = sprintf("%03d", $courseOverGnd);
}
if ($print_18) { print " Course: $courseOverGnd\n"; }
# NOTE: 0 to 102 knots
# NOTE: 102.3 = N/A
# NOTE: 102.2 = 102.2 knots or higher
# APRS spec says can set to "000", "...", or " " if unknown
my $speedOverGnd = &bin2dec($bSpeedOverGnd) / 10;
my $speed = "";
if ($speedOverGnd == 102.3) {
$speed = "...";
}
else {
$speed = sprintf("%03d", $speedOverGnd);
}
if ($print_18) { print " Speed: $speedOverGnd\n"; }
# NOTE: -90 to +90
# NOTE: 91 = N/A
my $latitude = &signedBin2dec($bLatitude) / 600000.0;
if ($latitude == 91) {
if ($print_18) { print "\n"; }
return();
}
my $NS;
if ($print_18) { printf(" Latitude: %07.5f\n", $latitude); }
if ($latitude >= 0.0) {
$NS = 'N';
} else {
$NS = 'S';
$latitude = abs($latitude);
}
my $latdeg = int($latitude);
my $latmins = $latitude - $latdeg;
my $latmins2 = $latmins * 60.0;
my $lat = sprintf("%02d%05.2f%s", $latdeg, $latmins2, $NS);
# NOTE: -180 to +180
# NOTE: 181 = N/A
my $longitude = &signedBin2dec($bLongitude) / 600000.0;
if ($longitude == 181) {
if ($print_18) { print "\n"; }
return();
}
my $EW;
if ($print_18) { printf("Longitude: %08.5f\n", $longitude); }
if ($longitude >= 0.0) {
$EW = 'E';
} else {
$EW = 'W';
$longitude = abs($longitude);
}
my $londeg = int($longitude);
my $lonmins = $longitude - $londeg;
my $lonmins2 = $lonmins * 60.0;
my $lon = sprintf("%03d%05.2f%s", $londeg, $lonmins2, $EW);
my $symbol = "s";
my $vesselTag = " ($country)";
if ( defined($vessel_hash{$userID}) ) { $vesselTag = " " . $vessel_hash{$userID} . " (" . $country . ")"; }
my $aprs = &escape_from_shell("$xastir_user>APRS:)$userID!$lat/$lon$symbol$course/$speed$vesselTag");
if ($print_18) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
my $result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
# Assign tactical call = "$userID + $country" or "$vesselName + $country"
# Max tactical call in Xastir is 57 chars (56 + terminator?)
#
my $temp;
if ( defined( $vessel_hash{$userID}) ) {
$temp = substr($vessel_hash{$userID} . " (" . $country . ")", 0, 56); # Chop at 56 chars
}
else {
# No vessel name
# N7IPB: Display country of registry
$temp = substr("($country)", 0, 56); # Chop at 56 chars
# WE7U: Display the 9-digit MMSI + country of registry
#$temp = substr($userID . " (" . $country . ")", 0, 56); # Chop at 56 chars
}
if ( !defined($tactical_hash{$userID}) ) {
$tactical_hash{$userID} = $temp;
$aprs = &escape_from_shell($xastir_user . '>' . "APRS::TACTICAL :" . $userID . "=" . $temp);
if ($print_18) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
$result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
}
if ($print_18) { print "\n"; }
return();
}
# Message type 19: Extended Class B CS Position Report
# This message should have 312 bits total
#
sub process_type_19() {
#if ( length($bin_string) < 312 ) {
if ( length($bin_string) < 263 ) {
#printf("19\t%s\t%d\n", $bin_string, length($bin_string));
#printf("19\t%d\n", length($bin_string));
if ($print_19) { print "Msg too short\n\n"; }
return;
}
# substr($bin_string, 0, 6); # Message Type
# my $brepeat_indicator = substr($bin_string, 6, 2); # Repeat Indicator
my $bUserID = substr($bin_string, 8, 30); # MMSI
# substr($bin_string, 38, 8); # Spare
my $bSpeedOverGnd = substr($bin_string, 46, 10); # Speed Over Ground
# substr($bin_string, 56, 1); # Position Accuracy
my $bLongitude = substr($bin_string, 57, 28); # Longitude
my $bLatitude = substr($bin_string, 85, 27); # Latitude
my $bCourseOverGnd = substr($bin_string, 112, 12); # Course Over Ground
# substr($bin_string, 124, 9); # True Heading
# substr($bin_string, 133, 6); # Time Stamp
# substr($bin_string, 139, 4); # Spare
my $bVesselName = substr($bin_string, 143, 120); # Name
# substr($bin_string, 263, 8); # Type of ship and cargo
# substr($bin_string, 271, 9); # Dimension to Bow
# substr($bin_string, 280, 9); # Dimension to Stern
# substr($bin_string, 289, 6); # Dimension to Port
# substr($bin_string, 295, 6); # Dimension to Starboard
# substr($bin_string, 301, 4); # Position Fix Type
# substr($bin_string, 305, 1); # RAIM flag
# substr($bin_string, 306, 1); # DTE
# substr($bin_string, 307, 1); # Assigned mode flag
# substr($bin_string, 308, 4); # Spare
my $userID = &bin2dec($bUserID);
if ($print_19) { print " User ID: $userID\n"; }
my $country = &decode_MID($userID);
if ($print_19) { print " Country: $country\n"; }
# NOTE: 0-359 degrees
# NOTE: 360 = N/A
# APRS spec says can set to "000", "...", or " " if unknown
my $courseOverGnd = &bin2dec($bCourseOverGnd) / 10 ;
my $course = "";
if ($courseOverGnd == 360) {
$course = "...";
}
elsif ($courseOverGnd == 0) {
$course = "360";
}
else {
$course = sprintf("%03d", $courseOverGnd);
}
if ($print_19) { print " Course: $courseOverGnd\n"; }
# NOTE: 0 to 102 knots
# NOTE: 102.3 = N/A
# NOTE: 102.2 = 102.2 knots or higher
# APRS spec says can set to "000", "...", or " " if unknown
my $speedOverGnd = &bin2dec($bSpeedOverGnd) / 10;
my $speed = "";
if ($speedOverGnd == 102.3) {
$speed = "...";
}
else {
$speed = sprintf("%03d", $speedOverGnd);
}
if ($print_19) { print " Speed: $speedOverGnd\n"; }
# NOTE: -90 to +90
# NOTE: 91 = N/A
my $latitude = &signedBin2dec($bLatitude) / 600000.0;
if ($latitude == 91) {
if ($print_19) { print "\n"; }
return();
}
my $NS;
if ($print_19) { printf(" Latitude: %07.5f\n", $latitude); }
if ($latitude >= 0.0) {
$NS = 'N';
} else {
$NS = 'S';
$latitude = abs($latitude);
}
my $latdeg = int($latitude);
my $latmins = $latitude - $latdeg;
my $latmins2 = $latmins * 60.0;
my $lat = sprintf("%02d%05.2f%s", $latdeg, $latmins2, $NS);
# NOTE: -180 to +180
# NOTE: 181 = N/A
my $longitude = &signedBin2dec($bLongitude) / 600000.0;
if ($longitude == 181) {
if ($print_19) { print "\n"; }
return();
}
my $EW;
if ($print_19) { printf("Longitude: %08.5f\n", $longitude); }
if ($longitude >= 0.0) {
$EW = 'E';
} else {
$EW = 'W';
$longitude = abs($longitude);
}
my $londeg = int($longitude);
my $lonmins = $longitude - $londeg;
my $lonmins2 = $lonmins * 60.0;
my $lon = sprintf("%03d%05.2f%s", $londeg, $lonmins2, $EW);
my $vesselName = "";
if ($bVesselName ne "") {
$vesselName = &bin2text($bVesselName);
$vesselName =~ s/\s+$//; # Remove extra spaces at end
}
my $symbol = "s";
my $vesselTag = " ($country)";
if ( defined($vessel_hash{$userID}) ) { $vesselTag = " " . $vessel_hash{$userID} . " (" . $country . ")"; }
my $aprs = &escape_from_shell("$xastir_user>APRS:)$userID!$lat/$lon$symbol$course/$speed$vesselTag");
if ($print_19) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
my $result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
if ($vesselName ne "") {
$vessel_hash{$userID} = $vesselName;
# Update the persistent store
store \%vessel_hash , $persistentFileSpec;
if ($print_19) { print " Vessel: $vesselName\n"; }
# Assign tactical call = $vesselName
# Max tactical call in Xastir is 57 chars (56 + terminator?)
#
my $temp = substr($vesselName . " (" . $country . ")", 0, 56); # Chop at 56 chars
if ( !defined($tactical_hash{$userID}) || $tactical_hash{$userID} ne $temp ) {
$tactical_hash{$userID} = $temp;
$aprs = &escape_from_shell($xastir_user . '>' . "APRS::TACTICAL :" . $userID . "=" . $temp);
if ($print_19) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
$result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
}
}
if ($print_19) { print "\n"; }
return();
}
# Message type 21: Aid to Navigation report
# These messages should have between 272 and 360 bits total
# We don't use the full message and don't parse the name extension
#
sub process_type_21() {
#if ( length($bin_string) < 168 ) {
if ( length($bin_string) < 272 ) {
#printf("21\t%s\t%d\n", $bin_string, length($bin_string));
#printf("21\t%d\n", length($bin_string));
if ($print_21) { print "Msg too short\n\n"; }
return;
}
my $bUserID = substr($bin_string, 8, 30); # MMSI
my $bAidType = substr($bin_string, 38, 5);
my $bVesselName = substr($bin_string, 43, 120); # Vessel Name #####
my $bLongitude = substr($bin_string, 164, 28); #####
my $bLatitude = substr($bin_string, 192, 27); #####
my $userID = &bin2dec($bUserID);
if ($print_21) { print " User ID: $userID\n"; }
my $country = &decode_MID($userID);
if ($print_21) { print " Country: $country\n"; }
my $vesselName = &bin2text($bVesselName);
$vesselName =~ s/\s+$//; # Remove extra spaces at end
$vessel_hash{$userID} = $vesselName;
# Update the persistent store
store \%vessel_hash , $persistentFileSpec;
if ($print_21) { print " Vessel: $vesselName\n"; }
my $AidTypeTxt = &decodeAidType($bAidType);
if ($print_21) { print "Navaid Type: $AidTypeTxt\n"; }
# NOTE: -90 to +90
# NOTE: 91 = N/A
my $latitude = &signedBin2dec($bLatitude) / 600000.0;
if ($latitude == 91) { return(); }
my $NS;
if ($print_21) { printf(" Latitude: %07.5f\n", $latitude); }
if ($latitude >= 0.0) {
$NS = 'N';
} else {
$NS = 'S';
$latitude = abs($latitude);
}
my $latdeg = int($latitude);
my $latmins = $latitude - $latdeg;
my $latmins2 = $latmins * 60.0;
my $lat = sprintf("%02d%05.2f%s", $latdeg, $latmins2, $NS);
# NOTE: -180 to +180
# NOTE: 181 = N/A
my $longitude = &signedBin2dec($bLongitude) / 600000.0;
if ($longitude == 181) { return(); }
my $EW;
if ($print_21) { printf("Longitude: %08.5f\n", $longitude); }
if ($longitude >= 0.0) {
$EW = 'E';
} else {
$EW = 'W';
$longitude = abs($longitude);
}
my $londeg = int($longitude);
my $lonmins = $longitude - $londeg;
my $lonmins2 = $lonmins * 60.0;
my $lon = sprintf("%03d%05.2f%s", $londeg, $lonmins2, $EW);
my $symbol = "N";
# Set up to add country or vessel_name + country to comment field
my $vesselTag = " (nav-aid)";
if ( defined($vessel_hash{$userID}) ) { $vesselTag = " " . $vessel_hash{$userID} . " (" . $country . ")"; }
my $aprs= &escape_from_shell("$xastir_user>APRS:)$userID!$lat\\$lon$symbol $AidTypeTxt");
if ($print_21) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
my $result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
# Assign tactical call = $vesselName + $country
# Max tactical call in Xastir is 57 chars (56 + terminator?)
#
my $temp = substr($vesselName, 0, 56); # Chop at 56 chars
if ( !defined($tactical_hash{$userID}) || $tactical_hash{$userID} ne $temp ) {
$tactical_hash{$userID} = $temp;
$aprs = &escape_from_shell($xastir_user . '>' . "APRS::TACTICAL :" . $userID . "=" . $temp);
if ($print_21) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
$result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
}
if ($print_21) { print "\n"; }
return();
}
# Message type 24: Static Data Report (Vessel name, callsign, ship type)
# This one has three variations:
#
# If Part Number field is 0, it's a Type A message.
# If Part Number is 1, it's a Type B.
#
# Part B has two variations as well:
# If the MMSI is that of an auxiliary craft, then the Mothership MMSI
# is that of the mothership.
# If not, then those 30 bits represent vessel dimmensions.
#
# This message should have:
# Type A: 160 bits total
# Type B: 168 bits total
#
sub process_type_24() {
#if ( length($bin_string) < 160 ) {
if ( length($bin_string) < 160 ) {
#printf("24\t%s\t%d\n", $bin_string, length($bin_string));
#printf("24\t%d\n", length($bin_string));
if ($print_24_A || $print_24_B) { print "Msg too short\n\n"; }
return;
}
# substr($bin_string, 0, 6); # Message Type
# my $brepeat_indicator = substr($bin_string, 6, 2); # Repeat Indicator
my $bUserID = substr($bin_string, 8, 30); # MMSI
my $bPartNumber = substr($bin_string, 38, 2); # Part Number
my $userID = &bin2dec($bUserID);
# Check for Type A/B Variant (Type B also has two variants!)
$PartNumber = &bin2dec($bPartNumber);
if ($PartNumber == 0) {
# It is a Type A Variant
if ($print_24_A) { print " Msg Type: $message_type\tType A Variant\n"; }
if ($print_24_A) { print " User ID: $userID\n"; }
my $country = &decode_MID($userID);
if ($print_24_A) { print " Country: $country\n"; }
# Type A Variant format starts at bit 40:
my $bVesselName = substr($bin_string, 40, 120); # Vessel Name
# substr($bin_string, 160, 8); # Spare.
my $vesselName = "";
if ($bVesselName ne "") {
$vesselName = &bin2text($bVesselName);
$vesselName =~ s/\s+$//; # Remove extra spaces at end
}
if ($vesselName ne "") {
$vessel_hash{$userID} = $vesselName;
# Update the persistent store
store \%vessel_hash , $persistentFileSpec;
if ($print_24_A) { print " Vessel: $vesselName\n"; }
# Assign tactical call = $vesselName
# Max tactical call in Xastir is 57 chars (56 + terminator?)
#
my $temp = substr($vesselName . " (" . $country . ")", 0, 56); # Chop at 56 chars
if ( !defined($tactical_hash{$userID}) || $tactical_hash{$userID} ne $temp ) {
$tactical_hash{$userID} = $temp;
$aprs = &escape_from_shell($xastir_user . '>' . "APRS::TACTICAL :" . $userID . "=" . $temp);
if ($print_24_A) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
$result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
}
}
if ($print_24_A) { print "\n"; }
}
else {
# It is a Type B Variant. There are two variants of this type!
if ($print_24_B) { print " Msg Type: $message_type\tType B Variant\n"; }
if ($print_24_B) { print " User ID: $userID\n"; }
my $country = &decode_MID($userID);
if ($print_24_B) { print " Country: $country\n"; }
# Check the UserID to see if it is an Auxiliary Craft,
# to determine which Type B Variant to decode.
# Auxiliary craft: MMSI of form 98XXXYYYY, the XXX digits are the country code.
if ( !($userID =~ m/^98/)) {
# It is NOT an auxiliary craft:
# Alternate format starting at bit 40:
$bShipType = substr($bin_string, 40, 8); # Ship Type: Page 114 of ITU spec, Table 53
# substr($bin_string, 48, 18); # Vendor ID
# substr($bin_string, 66, 4); # Unit Model Code
# substr($bin_string, 70, 20); # Serial Number)
# substr($bin_string, 90, 42); # Call Sign
# substr($bin_string, 132, 9); # Dimension to bow
# substr($bin_string, 141, 9); # Dimension to Stern
# substr($bin_string, 150, 6); # Dimension to Port
# substr($bin_string, 156, 6); # Dimension to Starboard
# substr($bin_string, 162, 4); # Type of fix
# substr($bin_string, 166, 2); # Spare
my $shipTypeTxt = &decodeShipType($bShipType);
if ($print_24_B) { print "Ship Type: $shipTypeTxt\n"; }
# Assign tactical call = $vesselName + $shipTypeTxt
# Max tactical call in Xastir is 57 chars (56 + terminator?)
#
#my $temp = substr($vesselName . " (" . $country . ")", 0, 56); # Chop at 56 chars
my $temp;
if (defined($vessel_hash{$userID}) ) {
$temp = substr($vessel_hash{$userID} . " ($shipTypeTxt:$country)", 0, 56); # Chop at 56 chars
}
else {
$temp = substr("($shipTypeTxt:$country)", 0, 56); # Chop at 56 chars
}
if ( !defined($tactical_hash{$userID}) || $tactical_hash{$userID} ne $temp ) {
$tactical_hash{$userID} = $temp;
$aprs = &escape_from_shell($xastir_user . '>' . "APRS::TACTICAL :" . $userID . "=" . $temp);
if ($print_24_A) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
$result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
}
}
else {
# It IS an auxiliary craft:
# Alternate format starting at bit 132:
# substr($bin_string, 132, 30); # Mothership MMSI
# substr($bin_string, 162, 6); # Spare
}
if ($print_24_B) { print "\n"; }
}
return();
}
# Message type 27: Position Report For Long-Range Applications (rare)
# This message should have 96 bits total
#
sub process_type_27() {
#if ( length($bin_string) < 96 ) {
if ( length($bin_string) < 94 ) {
#printf("27\t%s\t%d\n", $bin_string, length($bin_string));
#printf("27\t%d\n", length($bin_string));
if ($print_27) { print "Msg too short\n\n"; }
return;
}
# substr($bin_string, 0, 6); # Message Type
# my $brepeat_indicator = substr($bin_string, 6, 2); # Repeat Indicator
my $bUserID = substr($bin_string, 8, 30); # MMSI
# substr($bin_string, 38, 1); # Position Accuracy
# substr($bin_string, 39, 1); # RAIM flag
my $bNavStatus = substr($bin_string, 40, 4); # Navigation Status
my $bLongitude = substr($bin_string, 44, 18); # Longitude
my $bLatitude = substr($bin_string, 62, 17); # Latitude
my $bSpeedOverGnd = substr($bin_string, 79, 6); # Speed Over Ground
my $bCourseOverGnd = substr($bin_string, 85, 9); # Course Over Ground
# substr($bin_string, 94, 1); # GNSS Position latency
# substr($bin_string, 95, 1); # Spare
my $userID = &bin2dec($bUserID);
if ($print_27) { print " User ID: $userID\n"; }
my $country = &decode_MID($userID);
if ($print_27) { print " Country: $country\n"; }
my $navStatus = &bin2dec($bNavStatus);
my $navStatusTxt = "";
if ($navStatus == 0) { $navStatusTxt = "Under_way:engine"; }
elsif ($navStatus == 1) { $navStatusTxt = "At_anchor"; }
elsif ($navStatus == 2) { $navStatusTxt = "Not_under_command"; }
elsif ($navStatus == 3) { $navStatusTxt = "Restricted_maneuverability"; }
elsif ($navStatus == 4) { $navStatusTxt = "Constrained_by_draught"; }
elsif ($navStatus == 5) { $navStatusTxt = "Moored"; }
elsif ($navStatus == 6) { $navStatusTxt = "Aground"; }
elsif ($navStatus == 7) { $navStatusTxt = "Fishing"; }
elsif ($navStatus == 8) { $navStatusTxt = "Under_way:sailing"; }
#elsif ($navStatus == 9) { $navStatusTxt = "Reserved"; }
#elsif ($navStatus == 10) { $navStatusTxt = "Reserved"; }
#elsif ($navStatus == 11) { $navStatusTxt = "Reserved"; }
#elsif ($navStatus == 12) { $navStatusTxt = "Reserved"; }
#elsif ($navStatus == 13) { $navStatusTxt = "Reserved"; }
elsif ($navStatus == 14) { $navStatusTxt = "AIS-SART_is_active"; }
#elsif ($navStatus == 15) { $navStatusTxt = "Not defined"; }
# NOTE: 0-359 degrees
# NOTE: 511 = N/A
# APRS spec says can set to "000", "...", or " " if unknown
my $courseOverGnd = &bin2dec($bCourseOverGnd);
my $course = "";
if ($courseOverGnd == 511) {
$course = "...";
}
elsif ($courseOverGnd == 0) {
$course = "360";
}
else {
$course = sprintf("%03d", $courseOverGnd);
}
if ($print_27) { print " Course: $courseOverGnd\n"; }
# NOTE: 0-62 knots
# NOTE: 63 = N/A
# APRS spec says can set to "000", "...", or " " if unknown
my $speedOverGnd = &bin2dec($bSpeedOverGnd);
my $speed = "";
if ($speedOverGnd == 63) {
$speed = "...";
}
else {
$speed = sprintf("%03d", $speedOverGnd);
}
if ($print_27) { print " Speed: $speedOverGnd\n"; }
# NOTE: -90 to +90
# NOTE: 91 = N/A = D548h or 54600 decimal. Divide by 91 to get 600 (our factor).
my $latitude = &signedBin2dec($bLatitude) / 600.0;
if ($latitude == 91) {
if ($print_27) { print "\n"; }
return();
}
if ($print_27) {
#printf(" bLatitude: %s\n", $bLatitude);
printf(" Latitude: %07.5f\n", $latitude);
}
my $NS;
if ($latitude >= 0.0) {
$NS = 'N';
} else {
$NS = 'S';
$latitude = abs($latitude);
}
my $latdeg = int($latitude);
my $latmins = $latitude - $latdeg;
my $latmins2 = $latmins * 60.0;
my $lat = sprintf("%02d%05.2f%s", $latdeg, $latmins2, $NS);
# NOTE: -180 to +180
# NOTE: 181 = N/A = 1A838h or 108600 decimal. Divide by 181 to get 600 (our factor).
my $longitude = &signedBin2dec($bLongitude) / 600.0;
if ($longitude == 181) {
if ($print_27) { print "\n"; }
return();
}
if ($print_27) {
#printf("bLongitude: %s\n", $bLongitude);
printf("Longitude: %08.5f\n", $longitude);
}
my $EW;
if ($longitude >= 0.0) {
$EW = 'E';
} else {
$EW = 'W';
$longitude = abs($longitude);
}
my $londeg = int($longitude);
my $lonmins = $longitude - $londeg;
my $lonmins2 = $lonmins * 60.0;
my $lon = sprintf("%03d%05.2f%s", $londeg, $lonmins2, $EW);
my $symbol = "s";
my $vesselTag = " ($country)";
if ( defined($vessel_hash{$userID}) ) { $vesselTag = " " . $vessel_hash{$userID} . " (" . $country . ")"; }
my $aprs = &escape_from_shell("$xastir_user>APRS:)$userID!$lat/$lon$symbol$course/$speed $navStatusTxt$vesselTag");
if ($print_27) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
my $result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
# Assign tactical call = "$userID + $country" or "$vesselName + $country"
# Max tactical call in Xastir is 57 chars (56 + terminator?)
#
my $temp;
if ( defined( $vessel_hash{$userID}) ) {
$temp = substr($vessel_hash{$userID} . " (" . $country . ")", 0, 56); # Chop at 56 chars
}
else {
# No vessel name
# N7IPB: Display country of registry
$temp = substr("($country)", 0, 56); # Chop at 56 chars
# WE7U: Display the 9-digit MMSI + country of registry
#$temp = substr($userID . " (" . $country . ")", 0, 56); # Chop at 56 chars
}
if ( !defined($tactical_hash{$userID}) ) {
$tactical_hash{$userID} = $temp;
$aprs = &escape_from_shell($xastir_user . '>' . "APRS::TACTICAL :" . $userID . "=" . $temp);
if ($print_27) { print " APRS: $aprs\n"; }
&log_aprs($aprs);
if ($enable_tx) {
$result = `$udp_client $xastir_host $xastir_port $xastir_user $xastir_pass \"$aprs\"`;
if ($result =~ m/NACK/) {
die "Received NACK from Xastir: Callsign/Passcode don't match?\n";
}
}
}
if ($print_27) { print "\n"; }
return();
}
# Convert binary string $bShipType into text string $shipType
#
sub decodeAidType() {
my $bAidType = shift;
my $aidType = sprintf("%02d", &bin2dec($bAidType));
my $aidTypeTxt = "$aidType:";
# Special Craft
if ($aidType == 1) { $aidTypeTxt = $aidTypeTxt . "Reference point"; }
elsif ($aidType == 2) { $aidTypeTxt = $aidTypeTxt . "RACON"; }
elsif ($aidType == 3) { $aidTypeTxt = $aidTypeTxt . "Fixed structure off shore"; }
elsif ($aidType == 4) { $aidTypeTxt = $aidTypeTxt . "Reserved"; }
elsif ($aidType == 5) { $aidTypeTxt = $aidTypeTxt . "Light, without sectors"; }
elsif ($aidType == 6) { $aidTypeTxt = $aidTypeTxt . "Light, with sectors"; }
elsif ($aidType == 7) { $aidTypeTxt = $aidTypeTxt . "Leading Light Front"; }
elsif ($aidType == 8) { $aidTypeTxt = $aidTypeTxt . "Leading Light Rear"; }
elsif ($aidType == 9) { $aidTypeTxt = $aidTypeTxt . "Beacon, Cardinal N"; }
elsif ($aidType == 10) { $aidTypeTxt = $aidTypeTxt . "Beacon, Cardinal E"; }
elsif ($aidType == 11) { $aidTypeTxt = $aidTypeTxt . "Beacon, Cardinal S"; }
elsif ($aidType == 12) { $aidTypeTxt = $aidTypeTxt . "Beacon, Cardinal W"; }
elsif ($aidType == 13) { $aidTypeTxt = $aidTypeTxt . "Beacon, Port hand"; }
elsif ($aidType == 14) { $aidTypeTxt = $aidTypeTxt . "Beacon, Starboard hand"; }
elsif ($aidType == 15) { $aidTypeTxt = $aidTypeTxt . "Beacon, Preferred Channel port hand"; }
elsif ($aidType == 16) { $aidTypeTxt = $aidTypeTxt . "Beacon, Preferred Channel starboard hand"; }
elsif ($aidType == 17) { $aidTypeTxt = $aidTypeTxt . "Beacon, Isolated danger"; }
elsif ($aidType == 18) { $aidTypeTxt = $aidTypeTxt . "Beacon, Safe water"; }
elsif ($aidType == 19) { $aidTypeTxt = $aidTypeTxt . "Beacon, Special mark"; }
elsif ($aidType == 20) { $aidTypeTxt = $aidTypeTxt . "Cardinal Mark N"; }
elsif ($aidType == 21) { $aidTypeTxt = $aidTypeTxt . "Cardinal Mark E"; }
elsif ($aidType == 22) { $aidTypeTxt = $aidTypeTxt . "Cardinal Mark S"; }
elsif ($aidType == 23) { $aidTypeTxt = $aidTypeTxt . "Cardinal Mark W"; }
elsif ($aidType == 24) { $aidTypeTxt = $aidTypeTxt . "Port hand Mark"; }
elsif ($aidType == 25) { $aidTypeTxt = $aidTypeTxt . "Starboard hand Mark"; }
elsif ($aidType == 26) { $aidTypeTxt = $aidTypeTxt . "Preferred Channel Port hand"; }
elsif ($aidType == 27) { $aidTypeTxt = $aidTypeTxt . "Preferred Channel Starboard hand"; }
elsif ($aidType == 28) { $aidTypeTxt = $aidTypeTxt . "Isolated danger"; }
elsif ($aidType == 29) { $aidTypeTxt = $aidTypeTxt . "Safe Water"; }
elsif ($aidType == 30) { $aidTypeTxt = $aidTypeTxt . "Special Mark"; }
elsif ($aidType == 31) { $aidTypeTxt = $aidTypeTxt . "Light Vessel / LANBY / Rigs"; }
return($aidTypeTxt);
}
# Convert binary string $bShipType into text string $shipType
#
sub decodeShipType() {
my $bShipType = shift;
my $shipType = sprintf("%02d", &bin2dec($bShipType));
my $shipTypeTxt = "$shipType:";
# Special Craft
if ($shipType == 50) { $shipTypeTxt = $shipTypeTxt . "Pilot_Vessel"; }
elsif ($shipType == 51) { $shipTypeTxt = $shipTypeTxt . "Search_and_Rescue"; }
elsif ($shipType == 52) { $shipTypeTxt = $shipTypeTxt . "Harbor_Tug"; }
elsif ($shipType == 53) { $shipTypeTxt = $shipTypeTxt . "Fish,Offshore_or_Port_Tender"; }
elsif ($shipType == 54) { $shipTypeTxt = $shipTypeTxt . "Anti-pollution"; }
elsif ($shipType == 55) { $shipTypeTxt = $shipTypeTxt . "Law_Enforcement"; }
elsif ($shipType == 56) { $shipTypeTxt = $shipTypeTxt . "Local"; }
elsif ($shipType == 57) { $shipTypeTxt = $shipTypeTxt . "Local"; }
elsif ($shipType == 58) { $shipTypeTxt = $shipTypeTxt . "Medical_Transport_or_Public_Safety"; }
elsif ($shipType == 59) { $shipTypeTxt = $shipTypeTxt . "Neutral_State_Vessel"; }
# U.S. Specific Vessels
elsif ($shipType =~ m/^2/) {
if ($shipType =~ m/0$/) { $shipTypeTxt = $shipTypeTxt . "Wing_in_Ground"; }
if ($shipType =~ m/1$/) { $shipTypeTxt = $shipTypeTxt . "Tow-Push"; }
if ($shipType =~ m/2$/) { $shipTypeTxt = $shipTypeTxt . "Tow-Push"; }
if ($shipType =~ m/3$/) { $shipTypeTxt = $shipTypeTxt . "Light_Boat"; }
if ($shipType =~ m/4$/) { $shipTypeTxt = $shipTypeTxt . "Mobile_Offshore_Drilling"; }
if ($shipType =~ m/5$/) { $shipTypeTxt = $shipTypeTxt . "Offshore_Supply_Vessel"; }
if ($shipType =~ m/6$/) { $shipTypeTxt = $shipTypeTxt . "Processing_Vessel"; }
if ($shipType =~ m/7$/) { $shipTypeTxt = $shipTypeTxt . "School/Scientific/Research/Training_Vessel"; }
if ($shipType =~ m/8$/) { $shipTypeTxt = $shipTypeTxt . "U.S._Public_or_Govt"; }
if ($shipType =~ m/9$/) { $shipTypeTxt = $shipTypeTxt . "Autonomous_or_Remotely-Operated"; }
}
# Other Vessels
elsif ($shipType =~ m/^3/) {
if ($shipType =~ m/0$/) { $shipTypeTxt = $shipTypeTxt . "Fishing"; }
if ($shipType =~ m/1$/) { $shipTypeTxt = $shipTypeTxt . "Towing-Pull"; }
if ($shipType =~ m/2$/) { $shipTypeTxt = $shipTypeTxt . "Towing-Big"; }
if ($shipType =~ m/3$/) { $shipTypeTxt = $shipTypeTxt . "Dredging_or_Underwater_Ops"; }
if ($shipType =~ m/4$/) { $shipTypeTxt = $shipTypeTxt . "Diving_Ops"; }
if ($shipType =~ m/5$/) { $shipTypeTxt = $shipTypeTxt . "Military_Ops"; }
if ($shipType =~ m/6$/) { $shipTypeTxt = $shipTypeTxt . "Sailing"; }
if ($shipType =~ m/7$/) { $shipTypeTxt = $shipTypeTxt . "Pleasure_Craft"; }
if ($shipType =~ m/8$/) { $shipTypeTxt = $shipTypeTxt . "Other:Reserved"; }
if ($shipType =~ m/9$/) { $shipTypeTxt = $shipTypeTxt . "Other:Reserved"; }
}
elsif ($shipType =~ m/^0/) { $shipTypeTxt = $shipTypeTxt . "Not-Available"; }
elsif ($shipType =~ m/^1/) { $shipTypeTxt = $shipTypeTxt . "Reserved"; }
elsif ($shipType =~ m/^4/) { $shipTypeTxt = $shipTypeTxt . "Passenger_or_High_Speed"; }
elsif ($shipType =~ m/^6/) { $shipTypeTxt = $shipTypeTxt . "Passenger-Big"; }
elsif ($shipType =~ m/^7/) { $shipTypeTxt = $shipTypeTxt . "Cargo"; }
elsif ($shipType =~ m/^8/) { $shipTypeTxt = $shipTypeTxt . "Tanker"; }
elsif ($shipType =~ m/^9/) { $shipTypeTxt = $shipTypeTxt . "Other"; }
# Decode 2nd digit for 1/4/6/7/8/9 types:
if ( ($shipType =~ m/^1/)
|| ($shipType =~ m/^4/)
|| ($shipType =~ m/^6/)
|| ($shipType =~ m/^7/)
|| ($shipType =~ m/^8/)
|| ($shipType =~ m/^9/) ) {
if ($shipType =~ m/0$/) { $shipTypeTxt = $shipTypeTxt . ""; }
if ($shipType =~ m/1$/) { $shipTypeTxt = $shipTypeTxt . "-Haz_Cat:A/X"; }
if ($shipType =~ m/2$/) { $shipTypeTxt = $shipTypeTxt . "-Haz_Cat:B/Y"; }
if ($shipType =~ m/3$/) { $shipTypeTxt = $shipTypeTxt . "-Haz_Cat:C/Z"; }
if ($shipType =~ m/4$/) { $shipTypeTxt = $shipTypeTxt . "-Haz_Cat:D/O"; }
if ($shipType =~ m/5$/) { $shipTypeTxt = $shipTypeTxt . "-Reserved"; }
if ($shipType =~ m/6$/) { $shipTypeTxt = $shipTypeTxt . "-Reserved"; }
if ($shipType =~ m/7$/) { $shipTypeTxt = $shipTypeTxt . "-Reserved"; }
if ($shipType =~ m/8$/) { $shipTypeTxt = $shipTypeTxt . "-Reserved"; }
if ($shipType =~ m/9$/) { $shipTypeTxt = $shipTypeTxt . ""; }
}
return($shipTypeTxt);
}
# Convert a 6-digit binary string to ASCII text
# Encoded AIS data: Each 6 bits represents one ASCII character.
#
# Each six-bit nibble maps to an ASCII character.
# Decimal 0-31 map to "@" ( ASCII 64) through "\_" (ASCII 95)
# Decimal 32-63 map to " " (ASCII 32) though "?" (ASCII 63)
# Lowercase, backtick, right/left braces, pipe, tilde and DEL can't be encoded
sub bin2text() {
my $input = shift;
my $final_string = "";
my $len = length($input);
my $i;
for ($i = 0; $i < $len; $i+=6) {
my $binary_char = substr($input, $i, 6);
# Convert from binary to decimal (ord)
my $d = unpack("N", pack("B32", substr("00000000000000000000000000000000" . $binary_char, -32)));
if ($d > 0) { # Skip NULL characters
if ($d < 32) { $d += 64; }
$final_string = $final_string . chr($d);
}
}
$final_string =~ s/\s\s/ /g;
return($final_string);
}
## Convert a signed binary string of ASCII 0's and 1's to a decimal
#sub signedBin2dec {
# my $input = shift;
#
# if ( substr($input, 0, 1) eq "1") { # Negative number
# # Pad to 32 bits with 1's
## return unpack("i", pack("B32", substr("11111111111111111111111111111111" . $input, -32)));
# # Invert all the bits. Add 1. Convert to decimal. Negate.
# my $ii;
# my $input2 = "";
# my $len = length($input);
# for ($ii = 0; $ii < $len; $ii++) {
# my $j = substr($input, $ii, 1);
# if ($j eq "0") { $j = "1"; }
# else { $j = "0"; }
# $input2 = $input2 . $j;
# }
##print "$input\n";
##print "$input2\n";
# my $binary = pack("B32", substr("00000000000000000000000000000000" . $input2, -32, 32));
# my $decimal = unpack("N", $binary);
# $decimal++;
# $decimal = -$decimal;
# return $decimal;
# }
# else { # Positive number
# # Pad to 32 bits with 0's
# return unpack("N", pack("B32", substr("00000000000000000000000000000000" . $input, -32))); #####
#
# }
#}
# Convert a signed binary string of ASCII 0's and 1's to a decimal the hard way.
# This subroutine should be platform-agnostic as it does all the work internally.
sub signedBin2dec {
my $input = shift;
my $input2 = "";
my $decimal;
my $multiplier;
#print "\n$input\n";
if ( substr($input, 0, 1) eq "1") { # Negative number
# Invert all the bits. Add 1. Convert to decimal. Negate.
my $ii;
my $len = length($input);
for ($ii = 0; $ii < $len; $ii++) {
my $j = substr($input, $ii, 1);
if ($j eq "0") { $j = "1"; }
else { $j = "0"; }
$input2 = $input2 . $j;
}
$decimal = &toDecimal($input2);
$decimal++;
$decimal = -$decimal;
return $decimal;
}
else { # Positive number
return &toDecimal($input);
}
}
# Convert to decimal from positive binary string.
# Used by above signedBin2dec subroutine.
sub toDecimal {
my $input = shift;
my $multiplier = 1;
my $decimal = 0;
my $len = length($input);
# Start at LSB bit, work toward MSB bit
for (my $ii = $len-1; $ii >= 0; $ii--) {
if (substr($input, $ii, 1) == 1) {
$decimal = $decimal + $multiplier;
#print "1\tMult: $multiplier\n";
}
else {
#print "0\n";
}
$multiplier = $multiplier * 2 ;
}
return $decimal;
}
# Convert an unsigned binary string of ASCII 0's and 1's to a decimal
sub bin2dec {
my $temp = shift;
if (!defined($temp) || $temp eq "") { return; }
return unpack("N", pack("B32", substr("00000000000000000000000000000000" . $temp, -32))); #####
}
# Convert a decimal string (ASCII) to a binary string (ASCII)
sub dec2bin {
my $input = "00000000000000000000000000" . shift;
my $str = unpack("B32", pack("N", $input));
my $str2 = substr($str, -6);
return $str2;
}
# Decode MID out of MMSI ($userID) too:
# 8MIDXXXXX Diver's radio (not used in the U.S. in 2013)
# MIDXXXXXX Ship
# 0MIDXXXXX Group of ships; the U.S. Coast Guard, for example, is 03699999
# 00MIDXXXX Coastal stations
# 111MIDXXX SAR (Search and Rescue) aircraft
# 99MIDXXXX Aids to Navigation
# 98MIDXXXX Auxiliary craft associated with a parent ship
# 970MIDXXX AIS SART (Search and Rescue Transmitter)
# 972XXXXXX MOB (Man Overboard) device
# 974XXXXXX EPIRB (Emergency Position Indicating Radio Beacon) AIS
#
# NOTE: U.S. ships sometimes incorrectly send "669" for those first 3 digits.
# http://www.itu.int/online/mms/glad/cga_mids.sh?lng=E
#
sub decode_MID {
my $userID = shift;
my $MID = $userID;
if ($userID =~ m/^8.*/) { # Diver's radio
$MID =~ s/^8(...).*/$1/;
}
elsif ($userID =~ m/^00.*/) { # Coastal station
$MID =~ s/^00(...).*/$1/;
}
elsif ($userID =~ m/^0.*/) { # Group of ships
$MID =~ s/^0(...).*/$1/;
}
elsif ($userID =~ m/^111.*/) { # SAR aircraft
$MID =~ s/^111(...).*/$1/;
}
elsif ($userID =~ m/^99.*/) { # Navigation aid
$MID =~ s/^99(...).*/$1/;
}
elsif ($userID =~ m/^98.*/) { # Auxiliary craft
$MID =~ s/^98(...).*/$1/;
}
elsif ($userID =~ m/^970.*/) { # AIS SART
$MID =~ s/^970(...).*/$1/;
}
elsif ($userID =~ m/^972.*/) { # Man overboard device
$MID = "MOB";
}
elsif ($userID =~ m/^974.*/) { # EPIRB
$MID = "EPIRB";
}
else { # Ship
$MID =~ s/^(...).*/$1/;
}
my $country = "";
if (defined($countries{$MID})) {
$country = $countries{$MID};
}
else {
$country = "Unknown";
}
return $country;
}
# Escape characters from the shell
#
# Between double quotes the literal value of all characters is preserved except
# for dollar sign, backticks (backward single quotes, ``) and backslash.
#
# The backslash retains its meaning only when followed by dollar, backtick, double quote,
# backslash or newline. Within double quotes, the backslashes are removed from the input
# stream when followed by one of these characters. Backslashes preceding characters that
# don't have a special meaning are left unmodified for processing by the shell interpreter.
#
sub escape_from_shell {
$temp = shift;
$temp =~ s/\\/\\/g; # Look for \ and escape it. Do this one FIRST!
$temp =~ s/"/\\"/g; # Look for " and escape it
$temp =~ s/\$/\\\$/g; # Look for $ and escape it
$temp =~ s/\`/\\`/g; # Look for ` and escape it
return ($temp);
}
# Log messages to log file if logging enabled
#
sub log_aprs {
$sentence = shift;
if ($logging_mode) {
`echo \"$sentence\" >> $log_file`;
}
}
|