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
|
/*
* NMEA2000 over CAN.
*
* This file is Copyright (c) 2012 by the GPSD project
* BSD terms apply: see the file COPYING in the distribution root for details.
*/
/* need this for strnlen() and struct ifreq */
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include "gpsd.h"
#include "libgps.h"
#if defined(NMEA2000_ENABLE)
#include "driver_nmea2000.h"
#include "bits.h"
#include <linux/can.h>
#include <linux/can/raw.h>
#define LOG_FILE 1
#define NMEA2000_NETS 4
#define NMEA2000_UNITS 256
#define CAN_NAMELEN 32
#define MIN(a,b) ((a < b) ? a : b)
#define NMEA2000_DEBUG_AIS 0
#define NMEA2000_FAST_DEBUG 0
static struct gps_device_t *nmea2000_units[NMEA2000_NETS][NMEA2000_UNITS];
static char can_interface_name[NMEA2000_NETS][CAN_NAMELEN+1];
typedef struct PGN
{
unsigned int pgn;
unsigned int fast;
unsigned int type;
gps_mask_t (* func)(unsigned char *bu, int len, struct PGN *pgn, struct gps_device_t *session);
const char *name;
} PGN;
#if LOG_FILE
FILE *logFile = NULL;
#endif /* of if LOG_FILE */
extern bool __attribute__ ((weak)) gpsd_add_device(const char *device_name, bool flag_nowait);
#define SHIFT32 0x100000000l
static int scale_int(int32_t var, const int64_t factor)
{
int64_t ret;
ret = var;
ret *= factor;
ret >>= 32;
return((int)ret);
}
static void print_data(struct gps_context_t *context,
unsigned char *buffer, int len, PGN *pgn)
{
#ifdef LIBGPS_DEBUG
if ((libgps_debuglevel >= LOG_IO) != 0) {
int l1, l2, ptr;
char bu[128];
ptr = 0;
l2 = sprintf(&bu[ptr], "got data:%6u:%3d: ", pgn->pgn, len);
ptr += l2;
for (l1=0;l1<len;l1++) {
if (((l1 % 20) == 0) && (l1 != 0)) {
gpsd_log(&context->errout, LOG_IO,"%s\n", bu);
ptr = 0;
l2 = sprintf(&bu[ptr], " : ");
ptr += l2;
}
l2 = sprintf(&bu[ptr], "%02ux ", (unsigned int)buffer[l1]);
ptr += l2;
}
gpsd_log(&context->errout, LOG_IO,"%s\n", bu);
}
#else
(void)context;
(void)buffer;
(void)len;
(void)pgn;
#endif
}
static gps_mask_t get_mode(struct gps_device_t *session)
{
if (session->driver.nmea2000.mode_valid & 1) {
session->newdata.mode = session->driver.nmea2000.mode;
} else {
session->newdata.mode = MODE_NOT_SEEN;
}
if (session->driver.nmea2000.mode_valid & 2) {
return MODE_SET | USED_IS;
} else {
return MODE_SET;
}
}
static int decode_ais_header(struct gps_context_t *context,
unsigned char *bu, int len, struct ais_t *ais, unsigned int mask)
{
if (len > 4) {
ais->type = (unsigned int) ( bu[0] & 0x3f);
ais->repeat = (unsigned int) ((bu[0] >> 6) & 0x03);
ais->mmsi = (unsigned int) getleu32(bu, 1);
ais->mmsi &= mask;
gpsd_log(&context->errout, LOG_INF,
"NMEA2000 AIS message type %u, MMSI %09d:\n",
ais->type, ais->mmsi);
return(1);
} else {
ais->type = 0;
ais->repeat = 0;
ais->mmsi = 0;
gpsd_log(&context->errout, LOG_ERROR,
"NMEA2000 AIS message type %u, too short message.\n",
ais->type);
}
return(0);
}
static void decode_ais_channel_info(unsigned char *bu,
int len,
unsigned int offset,
struct gps_device_t *session)
{
unsigned int pos, bpos;
uint16_t x;
pos = offset / 8;
bpos = offset % 8;
if (pos >= (unsigned int)len) {
session->driver.aivdm.ais_channel = 'A';
return;
}
x = getleu16(bu, pos);
x = (uint16_t)((x >> bpos) & 0x1f);
switch (x) {
case 1:
case 3:
session->driver.aivdm.ais_channel = 'B';
break;
default:
session->driver.aivdm.ais_channel = 'A';
break;
}
return;
}
static int ais_turn_rate(int rate)
{
if (rate < 0) {
return(-ais_turn_rate(-rate));
}
return((int)(4.733 * sqrt(rate * RAD_2_DEG * .0001 * 60.0)));
}
static double ais_direction(unsigned int val, double scale)
{
if ((val == 0xffff) && (scale == 1.0)) {
return(511.0);
}
return(val * RAD_2_DEG * 0.0001 * scale);
}
/*
* PGN 59392: ISO Acknowledgment
*/
static gps_mask_t hnd_059392(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 60928: ISO Address Claim
*/
static gps_mask_t hnd_060928(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 126208: NMEA Command/Request/Acknowledge
*/
static gps_mask_t hnd_126208(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 126464: ISO Transmit/Receive PGN List
*/
static gps_mask_t hnd_126464(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 126996: ISO Product Information
*/
static gps_mask_t hnd_126996(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 127258: GNSS Magnetic Variation
*/
static gps_mask_t hnd_127258(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 129025: GNSS Position Rapid Update
*/
static gps_mask_t hnd_129025(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
session->newdata.latitude = getles32(bu, 0) * 1e-7;
session->newdata.longitude = getles32(bu, 4) * 1e-7;
return LATLON_SET | get_mode(session);
}
/*
* PGN 129026: GNSS COG and SOG Rapid Update
*/
static gps_mask_t hnd_129026(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
session->driver.nmea2000.sid[0] = bu[0];
session->newdata.track = getleu16(bu, 2) * 1e-4 * RAD_2_DEG;
session->newdata.speed = getleu16(bu, 4) * 1e-2;
return SPEED_SET | TRACK_SET | get_mode(session);
}
/*
* PGN 126992: GNSS System Time
*/
static gps_mask_t hnd_126992(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
//uint8_t sid;
//uint8_t source;
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
//sid = bu[0];
//source = bu[1] & 0x0f;
session->newdata.time = getleu16(bu, 2)*24*60*60 + getleu32(bu, 4)/1e4;
return TIME_SET | get_mode(session);
}
static const int mode_tab[] = {MODE_NO_FIX, MODE_2D, MODE_3D, MODE_NO_FIX,
MODE_NO_FIX, MODE_NO_FIX, MODE_NO_FIX, MODE_NO_FIX};
/*
* PGN 129539: GNSS DOPs
*/
static gps_mask_t hnd_129539(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
gps_mask_t mask;
unsigned int req_mode;
unsigned int act_mode;
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
mask = 0;
session->driver.nmea2000.sid[1] = bu[0];
session->driver.nmea2000.mode_valid |= 1;
req_mode = (unsigned int)((bu[1] >> 0) & 0x07);
act_mode = (unsigned int)((bu[1] >> 3) & 0x07);
/* This is a workaround for some GARMIN plotter, actual mode auto makes no sense for me! */
if ((act_mode == 3) && (req_mode != 3)) {
act_mode = req_mode;
}
session->driver.nmea2000.mode = mode_tab[act_mode];
session->gpsdata.dop.hdop = getleu16(bu, 2) * 1e-2;
session->gpsdata.dop.vdop = getleu16(bu, 4) * 1e-2;
session->gpsdata.dop.tdop = getleu16(bu, 6) * 1e-2;
mask |= DOP_SET;
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d): sid:%02x hdop:%5.2f vdop:%5.2f tdop:%5.2f\n",
pgn->pgn,
session->driver.nmea2000.unit,
session->driver.nmea2000.sid[1],
session->gpsdata.dop.hdop,
session->gpsdata.dop.vdop,
session->gpsdata.dop.tdop);
return mask | get_mode(session);
}
/*
* PGN 129540: GNSS Satellites in View
*/
static gps_mask_t hnd_129540(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
int l1;
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
session->driver.nmea2000.sid[2] = bu[0];
session->gpsdata.satellites_visible = (int)bu[2];
memset(session->gpsdata.skyview, '\0', sizeof(session->gpsdata.skyview));
for (l1=0;l1<session->gpsdata.satellites_visible;l1++) {
int svt;
double azi, elev, snr;
elev = getles16(bu, 3+12*l1+1) * 1e-4 * RAD_2_DEG;
azi = getleu16(bu, 3+12*l1+3) * 1e-4 * RAD_2_DEG;
snr = getles16(bu, 3+12*l1+5) * 1e-2;
svt = (int)(bu[3+12*l1+11] & 0x0f);
session->gpsdata.skyview[l1].elevation = (short) (round(elev));
session->gpsdata.skyview[l1].azimuth = (short) (round(azi));
session->gpsdata.skyview[l1].ss = snr;
session->gpsdata.skyview[l1].PRN = (short)bu[3+12*l1+0];
session->gpsdata.skyview[l1].used = false;
if ((svt == 2) || (svt == 5)) {
session->gpsdata.skyview[l1].used = true;
}
}
session->driver.nmea2000.mode_valid |= 2;
return SATELLITE_SET | USED_IS;
}
/*
* PGN 129029: GNSS Positition Data
*/
static gps_mask_t hnd_129029(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
gps_mask_t mask;
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
mask = 0;
session->driver.nmea2000.sid[3] = bu[0];
session->newdata.time = getleu16(bu,1) * 24*60*60 + getleu32(bu, 3)/1e4;
mask |= TIME_SET;
session->newdata.latitude = getles64(bu, 7) * 1e-16;
session->newdata.longitude = getles64(bu, 15) * 1e-16;
mask |= LATLON_SET;
session->newdata.altitude = getles64(bu, 23) * 1e-6;
mask |= ALTITUDE_SET;
// printf("mode %x %x\n", (bu[31] >> 4) & 0x0f, bu[31]);
switch ((bu[31] >> 4) & 0x0f) {
case 0:
session->gpsdata.status = STATUS_NO_FIX;
break;
case 1:
session->gpsdata.status = STATUS_FIX;
break;
case 2:
session->gpsdata.status = STATUS_DGPS_FIX;
break;
case 3:
case 4:
case 5:
session->gpsdata.status = STATUS_FIX; /* Is this correct ? */
break;
default:
session->gpsdata.status = STATUS_NO_FIX;
break;
}
mask |= STATUS_SET;
session->gpsdata.separation = getles32(bu, 38) / 100.0;
session->newdata.altitude -= session->gpsdata.separation;
session->gpsdata.satellites_used = (int)bu[33];
session->gpsdata.dop.hdop = getleu16(bu, 34) * 0.01;
session->gpsdata.dop.pdop = getleu16(bu, 36) * 0.01;
mask |= DOP_SET;
return mask | get_mode(session);
}
/*
* PGN 129038: AIS Class A Position Report
*/
static gps_mask_t hnd_129038(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
struct ais_t *ais;
ais = &session->gpsdata.ais;
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
if (decode_ais_header(session->context, bu, len, ais, 0xffffffffU) != 0) {
ais->type1.lon = (int) scale_int(getles32(bu, 5), (int64_t)(SHIFT32 *.06L));
ais->type1.lat = (int) scale_int(getles32(bu, 9), (int64_t)(SHIFT32 *.06L));
ais->type1.accuracy = (bool) ((bu[13] >> 0) & 0x01);
ais->type1.raim = (bool) ((bu[13] >> 1) & 0x01);
ais->type1.second = (unsigned int) ((bu[13] >> 2) & 0x3f);
ais->type1.course = (unsigned int) ais_direction((unsigned int)getleu16(bu, 14), 10.0);
ais->type1.speed = (unsigned int) (getleu16(bu, 16) * MPS_TO_KNOTS * 0.01 / 0.1);
ais->type1.radio = (unsigned int) (getleu32(bu, 18) & 0x7ffff);
ais->type1.heading = (unsigned int) ais_direction((unsigned int)getleu16(bu, 21), 1.0);
ais->type1.turn = ais_turn_rate((int)getles16(bu, 23));
ais->type1.status = (unsigned int) ((bu[25] >> 0) & 0x0f);
ais->type1.maneuver = 0; /* Not transmitted ???? */
decode_ais_channel_info(bu, len, 163, session);
return(ONLINE_SET | AIS_SET);
}
return(0);
}
/*
* PGN 129039: AIS Class B Position Report
*/
static gps_mask_t hnd_129039(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
struct ais_t *ais;
ais = &session->gpsdata.ais;
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
if (decode_ais_header(session->context, bu, len, ais, 0xffffffffU) != 0) {
ais->type18.lon = (int) scale_int(getles32(bu, 5), (int64_t)(SHIFT32 *.06L));
ais->type18.lat = (int) scale_int(getles32(bu, 9), (int64_t)(SHIFT32 *.06L));
ais->type18.accuracy = (bool) ((bu[13] >> 0) & 0x01);
ais->type18.raim = (bool) ((bu[13] >> 1) & 0x01);
ais->type18.second = (unsigned int) ((bu[13] >> 2) & 0x3f);
ais->type18.course = (unsigned int) ais_direction((unsigned int) getleu16(bu, 14), 10.0);
ais->type18.speed = (unsigned int) (getleu16(bu, 16) * MPS_TO_KNOTS * 0.01 / 0.1);
ais->type18.radio = (unsigned int) (getleu32(bu, 18) & 0x7ffff);
ais->type18.heading = (unsigned int) ais_direction((unsigned int) getleu16(bu, 21), 1.0);
ais->type18.reserved = 0;
ais->type18.regional = (unsigned int) ((bu[24] >> 0) & 0x03);
ais->type18.cs = (bool) ((bu[24] >> 2) & 0x01);
ais->type18.display = (bool) ((bu[24] >> 3) & 0x01);
ais->type18.dsc = (bool) ((bu[24] >> 4) & 0x01);
ais->type18.band = (bool) ((bu[24] >> 5) & 0x01);
ais->type18.msg22 = (bool) ((bu[24] >> 6) & 0x01);
ais->type18.assigned = (bool) ((bu[24] >> 7) & 0x01);
decode_ais_channel_info(bu, len, 163, session);
return(ONLINE_SET | AIS_SET);
}
return(0);
}
/*
* PGN 129040: AIS Class B Extended Position Report
*/
/* No test case for this message at the moment */
static gps_mask_t hnd_129040(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
struct ais_t *ais;
ais = &session->gpsdata.ais;
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
if (decode_ais_header(session->context, bu, len, ais, 0xffffffffU) != 0) {
uint16_t length, beam, to_bow, to_starboard;
int l;
ais->type19.lon = (int) scale_int(getles32(bu, 5), (int64_t)(SHIFT32 *.06L));
ais->type19.lat = (int) scale_int(getles32(bu, 9), (int64_t)(SHIFT32 *.06L));
ais->type19.accuracy = (bool) ((bu[13] >> 0) & 0x01);
ais->type19.raim = (bool) ((bu[13] >> 1) & 0x01);
ais->type19.second = (unsigned int) ((bu[13] >> 2) & 0x3f);
ais->type19.course = (unsigned int) ais_direction((unsigned int) getleu16(bu, 14), 10.0);
ais->type19.speed = (unsigned int) (getleu16(bu, 16) * MPS_TO_KNOTS * 0.01 / 0.1);
ais->type19.reserved = (unsigned int) ((bu[18] >> 0) & 0xff);
ais->type19.regional = (unsigned int) ((bu[19] >> 0) & 0x0f);
ais->type19.shiptype = (unsigned int) ((bu[20] >> 0) & 0xff);
ais->type19.heading = (unsigned int) ais_direction((unsigned int) getleu16(bu, 21), 1.0);
length = getleu16(bu, 24);
beam = getleu16(bu, 26);
to_starboard = getleu16(bu, 28);
to_bow = getleu16(bu, 30);
if ((length == 0xffff) || (to_bow == 0xffff)) {
length = 0;
to_bow = 0;
}
if ((beam == 0xffff) || (to_starboard == 0xffff)) {
beam = 0;
to_starboard = 0;
}
ais->type19.to_bow = (unsigned int) (to_bow/10);
ais->type19.to_stern = (unsigned int) ((length-to_bow)/10);
ais->type19.to_port = (unsigned int) ((beam-to_starboard)/10);
ais->type19.to_starboard = (unsigned int) (to_starboard/10);
ais->type19.epfd = (unsigned int) ((bu[23] >> 4) & 0x0f);
ais->type19.dte = (unsigned int) ((bu[52] >> 0) & 0x01);
ais->type19.assigned = (bool) ((bu[52] >> 1) & 0x01);
for (l=0;l<AIS_SHIPNAME_MAXLEN;l++) {
ais->type19.shipname[l] = (char) bu[32+l];
}
ais->type19.shipname[AIS_SHIPNAME_MAXLEN] = (char) 0;
decode_ais_channel_info(bu, len, 422, session);
return(ONLINE_SET | AIS_SET);
}
return(0);
}
/*
* PGN 129793: AIS UTC and Date Report
*/
static gps_mask_t hnd_129793(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
struct ais_t *ais;
ais = &session->gpsdata.ais;
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
if (decode_ais_header(session->context, bu, len, ais, 0xffffffffU) != 0) {
uint32_t time;
uint32_t date;
time_t date1;
struct tm date2;
ais->type4.lon = (int) scale_int(getles32(bu, 5), (int64_t)(SHIFT32 *.06L));
ais->type4.lat = (int) scale_int(getles32(bu, 9), (int64_t)(SHIFT32 *.06L));
ais->type4.accuracy = (bool) ((bu[13] >> 0) & 0x01);
ais->type4.raim = (bool) ((bu[13] >> 1) & 0x01);
time = getleu32(bu, 14);
if (time != 0xffffffff) {
time = time / 10000;
ais->type4.second = time % 60; time = time / 60;
ais->type4.minute = time % 60; time = time / 60;
ais->type4.hour = time % 24;
} else {
ais->type4.second = AIS_SECOND_NOT_AVAILABLE;
ais->type4.minute = AIS_MINUTE_NOT_AVAILABLE;
ais->type4.hour = AIS_HOUR_NOT_AVAILABLE;
}
ais->type4.radio = (unsigned int) (getleu32(bu, 18) & 0x7ffff);
date = getleu16(bu, 21);
if (date != 0xffff) {
date1 = (time_t)date * (24L *60L *60L);
(void) gmtime_r(&date1, &date2);
ais->type4.year = (unsigned int) (date2.tm_year+1900);
ais->type4.month = (unsigned int) (date2.tm_mon+1);
ais->type4.day = (unsigned int) (date2.tm_mday);
} else {
ais->type4.day = AIS_DAY_NOT_AVAILABLE;
ais->type4.month = AIS_MONTH_NOT_AVAILABLE;
ais->type4.year = AIS_YEAR_NOT_AVAILABLE;
}
ais->type4.epfd = (unsigned int) ((bu[23] >> 4) & 0x0f);
decode_ais_channel_info(bu, len, 163, session);
return(ONLINE_SET | AIS_SET);
}
return(0);
}
/*
* PGN 129794: AIS Class A Static and Voyage Related Data
*/
static gps_mask_t hnd_129794(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
struct ais_t *ais;
ais = &session->gpsdata.ais;
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
if (decode_ais_header(session->context, bu, len, ais, 0xffffffffU) != 0) {
uint16_t length, beam, to_bow, to_starboard, date;
int l;
uint32_t time;
time_t date1;
struct tm date2;
int cpy_stop;
ais->type5.ais_version = (unsigned int) ((bu[73] >> 0) & 0x03);
ais->type5.imo = (unsigned int) getleu32(bu, 5);
if (ais->type5.imo == 0xffffffffU) {
ais->type5.imo = 0;
}
ais->type5.shiptype = (unsigned int) ((bu[36] >> 0) & 0xff);
length = getleu16(bu, 37);
beam = getleu16(bu, 39);
to_starboard = getleu16(bu, 41);
to_bow = getleu16(bu, 43);
if ((length == 0xffff) || (to_bow == 0xffff)) {
length = 0;
to_bow = 0;
}
if ((beam == 0xffff) || (to_starboard == 0xffff)) {
beam = 0;
to_starboard = 0;
}
ais->type5.to_bow = (unsigned int) (to_bow/10);
ais->type5.to_stern = (unsigned int) ((length-to_bow)/10);
ais->type5.to_port = (unsigned int) ((beam-to_starboard)/10);
ais->type5.to_starboard = (unsigned int) (to_starboard/10);
ais->type5.epfd = (unsigned int) ((bu[73] >> 2) & 0x0f);
date = getleu16(bu, 45);
time = getleu32(bu, 47);
date1 = (time_t) (date*24*60*60);
(void) gmtime_r(&date1, &date2);
ais->type5.month = (unsigned int) (date2.tm_mon+1);
ais->type5.day = (unsigned int) (date2.tm_mday);
ais->type5.minute = (unsigned int) (time/(10000*60));
ais->type5.hour = (unsigned int) (ais->type5.minute/60);
ais->type5.minute = (unsigned int) (ais->type5.minute-(ais->type5.hour*60));
ais->type5.draught = (unsigned int) (getleu16(bu, 51)/10);
ais->type5.dte = (unsigned int) ((bu[73] >> 6) & 0x01);
for (l=0,cpy_stop=0;l<7;l++) {
char next;
next = (char) bu[9+l];
if ((next < ' ') || (next > 0x7e)) {
cpy_stop = 1;
}
if (cpy_stop == 0) {
ais->type5.callsign[l] = next;
} else {
ais->type5.callsign[l] = 0;
}
}
ais->type5.callsign[7] = (char) 0;
for (l=0,cpy_stop=0;l<AIS_SHIPNAME_MAXLEN;l++) {
char next;
next = (char) bu[16+l];
if ((next < ' ') || (next > 0x7e)) {
cpy_stop = 1;
}
if (cpy_stop == 0) {
ais->type5.shipname[l] = next;
} else {
ais->type5.shipname[l] = 0;
}
}
ais->type5.shipname[AIS_SHIPNAME_MAXLEN] = (char) 0;
for (l=0,cpy_stop=0;l<20;l++) {
char next;
next = (char) bu[53+l];
if ((next < ' ') || (next > 0x7e)) {
cpy_stop = 1;
}
if (cpy_stop == 0) {
ais->type5.destination[l] = next;
} else {
ais->type5.destination[l] = 0;
}
}
ais->type5.destination[20] = (char) 0;
#if NMEA2000_DEBUG_AIS
printf("AIS: MMSI: %09u\n",
ais->mmsi);
printf("AIS: name: %-20.20s i:%8u c:%-8.8s b:%6u s:%6u p:%6u s:%6u dr:%4.1f\n",
ais->type5.shipname,
ais->type5.imo,
ais->type5.callsign,
ais->type5.to_bow,
ais->type5.to_stern,
ais->type5.to_port,
ais->type5.to_starboard,
ais->type5.draught/10.0);
printf("AIS: arival:%-20.20s at %02u-%02u-%04d %02u:%0u\n",
ais->type5.destination,
ais->type5.day,
ais->type5.month,
date2.tm_year+1900,
ais->type5.hour,
ais->type5.minute);
#endif /* of #if NMEA2000_DEBUG_AIS */
decode_ais_channel_info(bu, len, 592, session);
return(ONLINE_SET | AIS_SET);
}
return(0);
}
/*
* PGN 129798: AIS SAR Aircraft Position Report
*/
/* No test case for this message at the moment */
static gps_mask_t hnd_129798(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
struct ais_t *ais;
ais = &session->gpsdata.ais;
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
if (decode_ais_header(session->context, bu, len, ais, 0xffffffffU) != 0) {
ais->type9.lon = (int) scale_int(getles32(bu, 5), (int64_t)(SHIFT32 *.06L));
ais->type9.lat = (int) scale_int(getles32(bu, 9), (int64_t)(SHIFT32 *.06L));
ais->type9.accuracy = (bool) ((bu[13] >> 0) & 0x01);
ais->type9.raim = (bool) ((bu[13] >> 1) & 0x01);
ais->type9.second = (unsigned int) ((bu[13] >> 2) & 0x3f);
ais->type9.course = (unsigned int) ais_direction((unsigned int) getleu16(bu, 14), 10.0);
ais->type9.speed = (unsigned int) (getleu16(bu, 16) * MPS_TO_KNOTS * 0.01 / 0.1);
ais->type9.radio = (unsigned int) (getleu32(bu, 18) & 0x7ffff);
ais->type9.alt = (unsigned int) (getleu64(bu, 21)/1000000);
ais->type9.regional = (unsigned int) ((bu[29] >> 0) & 0xff);
ais->type9.dte = (unsigned int) ((bu[30] >> 0) & 0x01);
/* ais->type9.spare = (bu[30] >> 1) & 0x7f; */
ais->type9.assigned = 0; /* Not transmitted ???? */
decode_ais_channel_info(bu, len, 163, session);
return(ONLINE_SET | AIS_SET);
}
return(0);
}
/*
* PGN 129802: AIS Safty Related Broadcast Message
*/
/* No test case for this message at the moment */
static gps_mask_t hnd_129802(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
struct ais_t *ais;
ais = &session->gpsdata.ais;
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
if (decode_ais_header(session->context, bu, len, ais, 0x3fffffff) != 0) {
int l;
/* ais->type14.channel = (bu[ 5] >> 0) & 0x1f; */
for (l=0;l<36;l++) {
ais->type14.text[l] = (char) bu[6+l];
}
ais->type14.text[36] = (char) 0;
decode_ais_channel_info(bu, len, 40, session);
return(ONLINE_SET | AIS_SET);
}
return(0);
}
/*
* PGN 129809: AIS Class B CS Static Data Report, Part A
*/
static gps_mask_t hnd_129809(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
struct ais_t *ais;
ais = &session->gpsdata.ais;
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
if (decode_ais_header(session->context, bu, len, ais, 0xffffffffU) != 0) {
int l;
int index = session->driver.aivdm.context[0].type24_queue.index;
struct ais_type24a_t *saveptr = &session->driver.aivdm.context[0].type24_queue.ships[index];
gpsd_log(&session->context->errout, LOG_PROG,
"NMEA2000: AIS message 24A from %09u stashed.\n",
ais->mmsi);
for (l=0;l<AIS_SHIPNAME_MAXLEN;l++) {
ais->type24.shipname[l] = (char) bu[ 5+l];
saveptr->shipname[l] = (char) bu[ 5+l];
}
ais->type24.shipname[AIS_SHIPNAME_MAXLEN] = (char) 0;
saveptr->shipname[AIS_SHIPNAME_MAXLEN] = (char) 0;
saveptr->mmsi = ais->mmsi;
index += 1;
index %= MAX_TYPE24_INTERLEAVE;
session->driver.aivdm.context[0].type24_queue.index = index;
decode_ais_channel_info(bu, len, 200, session);
ais->type24.part = part_a;
return(ONLINE_SET | AIS_SET);
}
return(0);
}
/*
* PGN 129810: AIS Class B CS Static Data Report, Part B
*/
static gps_mask_t hnd_129810(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
struct ais_t *ais;
ais = &session->gpsdata.ais;
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
if (decode_ais_header(session->context, bu, len, ais, 0xffffffffU) != 0) {
int l, i;
ais->type24.shiptype = (unsigned int) ((bu[ 5] >> 0) & 0xff);
for (l=0;l<7;l++) {
ais->type24.vendorid[l] = (char) bu[ 6+l];
}
ais->type24.vendorid[7] = (char) 0;
for (l=0;l<7;l++) {
ais->type24.callsign[l] = (char) bu[13+l];
}
ais->type24.callsign[7] = (char )0;
ais->type24.model = 0;
ais->type24.serial = 0;
if (AIS_AUXILIARY_MMSI(ais->mmsi)) {
ais->type24.mothership_mmsi = (unsigned int) (getleu32(bu, 28));
} else {
uint16_t length, beam, to_bow, to_starboard;
length = getleu16(bu, 20);
beam = getleu16(bu, 22);
to_starboard = getleu16(bu, 24);
to_bow = getleu16(bu, 26);
if ((length == 0xffff) || (to_bow == 0xffff)) {
length = 0;
to_bow = 0;
}
if ((beam == 0xffff) || (to_starboard == 0xffff)) {
beam = 0;
to_starboard = 0;
}
ais->type24.dim.to_bow = (unsigned int) (to_bow/10);
ais->type24.dim.to_stern = (unsigned int) ((length-to_bow)/10);
ais->type24.dim.to_port = (unsigned int) ((beam-to_starboard)/10);
ais->type24.dim.to_starboard = (unsigned int) (to_starboard/10);
}
for (i = 0; i < MAX_TYPE24_INTERLEAVE; i++) {
if (session->driver.aivdm.context[0].type24_queue.ships[i].mmsi == ais->mmsi) {
for (l=0;l<AIS_SHIPNAME_MAXLEN;l++) {
ais->type24.shipname[l] = (char)(session->driver.aivdm.context[0].type24_queue.ships[i].shipname[l]);
}
ais->type24.shipname[AIS_SHIPNAME_MAXLEN] = (char) 0;
gpsd_log(&session->context->errout, LOG_PROG,
"NMEA2000: AIS 24B from %09u matches a 24A.\n",
ais->mmsi);
/* prevent false match if a 24B is repeated */
session->driver.aivdm.context[0].type24_queue.ships[i].mmsi = 0;
#if NMEA2000_DEBUG_AIS
printf("AIS: MMSI: %09u\n", ais->mmsi);
printf("AIS: name: %-20.20s v:%-8.8s c:%-8.8s b:%6u s:%6u p:%6u s:%6u\n",
ais->type24.shipname,
ais->type24.vendorid,
ais->type24.callsign,
ais->type24.dim.to_bow,
ais->type24.dim.to_stern,
ais->type24.dim.to_port,
ais->type24.dim.to_starboard);
#endif /* of #if NMEA2000_DEBUG_AIS */
decode_ais_channel_info(bu, len, 264, session);
ais->type24.part = both;
return(ONLINE_SET | AIS_SET);
}
}
#if NMEA2000_DEBUG_AIS
printf("AIS: MMSI : %09u\n", ais->mmsi);
printf("AIS: vendor: %-8.8s c:%-8.8s b:%6u s:%6u p:%6u s:%6u\n",
ais->type24.vendorid,
ais->type24.callsign,
ais->type24.dim.to_bow,
ais->type24.dim.to_stern,
ais->type24.dim.to_port,
ais->type24.dim.to_starboard);
#endif /* of #if NMEA2000_DEBUG_AIS */
decode_ais_channel_info(bu, len, 264, session);
ais->type24.part = part_b;
return(ONLINE_SET | AIS_SET);
}
return(0);
}
/*
* PGN 127506: PWR DC Detailed Status
*/
static gps_mask_t hnd_127506(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 127508: PWR Battery Status
*/
static gps_mask_t hnd_127508(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 127513: PWR Battery Configuration Status
*/
static gps_mask_t hnd_127513(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 127245: NAV Rudder
*/
static gps_mask_t hnd_127245(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 127250: NAV Vessel Heading
*/
static gps_mask_t hnd_127250(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
int aux;
print_data(session->context, bu, len, pgn);
session->gpsdata.attitude.heading = getleu16(bu, 1) * RAD_2_DEG * 0.0001;
// printf("ATT 0:%8.3f\n",session->gpsdata.attitude.heading);
aux = getles16(bu, 3);
if (aux != 0x07fff) {
session->gpsdata.attitude.heading += aux * RAD_2_DEG * 0.0001;
}
// printf("ATT 1:%8.3f %6x\n",session->gpsdata.attitude.heading, aux);
aux = getles16(bu, 5);
if (aux != 0x07fff) {
session->gpsdata.attitude.heading += aux * RAD_2_DEG * 0.0001;
}
// printf("ATT 2:%8.3f %6x\n",session->gpsdata.attitude.heading, aux);
session->gpsdata.attitude.mag_st = '\0';
session->gpsdata.attitude.pitch = NAN;
session->gpsdata.attitude.pitch_st = '\0';
session->gpsdata.attitude.roll = NAN;
session->gpsdata.attitude.roll_st = '\0';
session->gpsdata.attitude.yaw = NAN;
session->gpsdata.attitude.yaw_st = '\0';
session->gpsdata.attitude.dip = NAN;
session->gpsdata.attitude.mag_len = NAN;
session->gpsdata.attitude.mag_x = NAN;
session->gpsdata.attitude.mag_y = NAN;
session->gpsdata.attitude.mag_z = NAN;
session->gpsdata.attitude.acc_len = NAN;
session->gpsdata.attitude.acc_x = NAN;
session->gpsdata.attitude.acc_y = NAN;
session->gpsdata.attitude.acc_z = NAN;
session->gpsdata.attitude.gyro_x = NAN;
session->gpsdata.attitude.gyro_y = NAN;
session->gpsdata.attitude.temp = NAN;
session->gpsdata.attitude.depth = NAN;
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(ONLINE_SET | ATTITUDE_SET);
}
/*
* PGN 128259: NAV Speed
*/
static gps_mask_t hnd_128259(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 128267: NAV Water Depth
*/
static gps_mask_t hnd_128267(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
session->gpsdata.attitude.heading = NAN;
session->gpsdata.attitude.pitch = NAN;
session->gpsdata.attitude.pitch_st = '\0';
session->gpsdata.attitude.roll = NAN;
session->gpsdata.attitude.roll_st = '\0';
session->gpsdata.attitude.yaw = NAN;
session->gpsdata.attitude.yaw_st = '\0';
session->gpsdata.attitude.dip = NAN;
session->gpsdata.attitude.mag_len = NAN;
session->gpsdata.attitude.mag_x = NAN;
session->gpsdata.attitude.mag_y = NAN;
session->gpsdata.attitude.mag_z = NAN;
session->gpsdata.attitude.acc_len = NAN;
session->gpsdata.attitude.acc_x = NAN;
session->gpsdata.attitude.acc_y = NAN;
session->gpsdata.attitude.acc_z = NAN;
session->gpsdata.attitude.gyro_x = NAN;
session->gpsdata.attitude.gyro_y = NAN;
session->gpsdata.attitude.temp = NAN;
session->gpsdata.attitude.depth = getleu32(bu, 1) *.01;
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(ONLINE_SET | ATTITUDE_SET);
}
/*
* PGN 128275: NAV Distance Log
*/
static gps_mask_t hnd_128275(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 129283: NAV Cross Track Error
*/
static gps_mask_t hnd_129283(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 129284: NAV Navigation Data
*/
static gps_mask_t hnd_129284(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 129285: NAV Navigation - Route/WP Information
*/
static gps_mask_t hnd_129285(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 130306: NAV Wind Data
*/
static gps_mask_t hnd_130306(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 130310: NAV Water Temp., Outside Air Temp., Atmospheric Pressure
*/
static gps_mask_t hnd_130310(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
/*
* PGN 130311: NAV Environmental Parameters
*/
static gps_mask_t hnd_130311(unsigned char *bu, int len, PGN *pgn, struct gps_device_t *session)
{
print_data(session->context, bu, len, pgn);
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d(%3d):\n", pgn->pgn, session->driver.nmea2000.unit);
return(0);
}
static const char msg_059392[] = {"ISO Acknowledgment"};
static const char msg_060928[] = {"ISO Address Claim"};
static const char msg_126208[] = {"NMEA Command/Request/Acknowledge"};
static const char msg_126464[] = {"ISO Transmit/Receive PGN List"};
static const char msg_126992[] = {"GNSS System Time"};
static const char msg_126996[] = {"ISO Product Information"};
static const char msg_127506[] = {"PWR DC Detailed Status"};
static const char msg_127508[] = {"PWR Battery Status"};
static const char msg_127513[] = {"PWR Battery Configuration Status"};
static const char msg_127258[] = {"GNSS Magnetic Variation"};
static const char msg_129025[] = {"GNSS Position Rapid Update"};
static const char msg_129026[] = {"GNSS COG and SOG Rapid Update"};
static const char msg_129029[] = {"GNSS Positition Data"};
static const char msg_129539[] = {"GNSS DOPs"};
static const char msg_129540[] = {"GNSS Satellites in View"};
static const char msg_129038[] = {"AIS Class A Position Report"};
static const char msg_129039[] = {"AIS Class B Position Report"};
static const char msg_129040[] = {"AIS Class B Extended Position Report"};
static const char msg_129793[] = {"AIS UTC and Date report"};
static const char msg_129794[] = {"AIS Class A Static and Voyage Related Data"};
static const char msg_129798[] = {"AIS SAR Aircraft Position Report"};
static const char msg_129802[] = {"AIS Safty Related Broadcast Message"};
static const char msg_129809[] = {"AIS Class B CS Static Data Report, Part A"};
static const char msg_129810[] = {"AIS Class B CS Static Data Report, Part B"};
static const char msg_127245[] = {"NAV Rudder"};
static const char msg_127250[] = {"NAV Vessel Heading"};
static const char msg_128259[] = {"NAV Speed"};
static const char msg_128267[] = {"NAV Water Depth"};
static const char msg_128275[] = {"NAV Distance Log"};
static const char msg_129283[] = {"NAV Cross Track Error"};
static const char msg_129284[] = {"NAV Navigation Data"};
static const char msg_129285[] = {"NAV Navigation - Route/WP Information"};
static const char msg_130306[] = {"NAV Wind Data"};
static const char msg_130310[] = {"NAV Water Temp., Outside Air Temp., Atmospheric Pressure"};
static const char msg_130311[] = {"NAV Environmental Parameters"};
static const char msg_error [] = {"**error**"};
static PGN gpspgn[] = {{ 59392, 0, 0, hnd_059392, &msg_059392[0]},
{ 60928, 0, 0, hnd_060928, &msg_060928[0]},
{126208, 0, 0, hnd_126208, &msg_126208[0]},
{126464, 1, 0, hnd_126464, &msg_126464[0]},
{126992, 0, 0, hnd_126992, &msg_126992[0]},
{126996, 1, 0, hnd_126996, &msg_126996[0]},
{127258, 0, 0, hnd_127258, &msg_127258[0]},
{129025, 0, 1, hnd_129025, &msg_129025[0]},
{129026, 0, 1, hnd_129026, &msg_129026[0]},
{129029, 1, 1, hnd_129029, &msg_129029[0]},
{129283, 0, 0, hnd_129283, &msg_129283[0]},
{129284, 1, 0, hnd_129284, &msg_129284[0]},
{129285, 1, 0, hnd_129285, &msg_129285[0]},
{129539, 0, 1, hnd_129539, &msg_129539[0]},
{129540, 1, 1, hnd_129540, &msg_129540[0]},
{0 , 0, 0, NULL, &msg_error [0]}};
static PGN aispgn[] = {{ 59392, 0, 0, hnd_059392, &msg_059392[0]},
{ 60928, 0, 0, hnd_060928, &msg_060928[0]},
{126208, 0, 0, hnd_126208, &msg_126208[0]},
{126464, 1, 0, hnd_126464, &msg_126464[0]},
{126992, 0, 0, hnd_126992, &msg_126992[0]},
{126996, 1, 0, hnd_126996, &msg_126996[0]},
{129038, 1, 2, hnd_129038, &msg_129038[0]},
{129039, 1, 2, hnd_129039, &msg_129039[0]},
{129040, 1, 2, hnd_129040, &msg_129040[0]},
{129793, 1, 2, hnd_129793, &msg_129793[0]},
{129794, 1, 2, hnd_129794, &msg_129794[0]},
{129798, 1, 2, hnd_129798, &msg_129798[0]},
{129802, 1, 2, hnd_129802, &msg_129802[0]},
{129809, 1, 2, hnd_129809, &msg_129809[0]},
{129810, 1, 2, hnd_129810, &msg_129810[0]},
{0 , 0, 0, NULL, &msg_error [0]}};
static PGN pwrpgn[] = {{ 59392, 0, 0, hnd_059392, &msg_059392[0]},
{ 60928, 0, 0, hnd_060928, &msg_060928[0]},
{126208, 0, 0, hnd_126208, &msg_126208[0]},
{126464, 1, 0, hnd_126464, &msg_126464[0]},
{126992, 0, 0, hnd_126992, &msg_126992[0]},
{126996, 1, 0, hnd_126996, &msg_126996[0]},
{127506, 1, 3, hnd_127506, &msg_127506[0]},
{127508, 1, 3, hnd_127508, &msg_127508[0]},
{127513, 1, 3, hnd_127513, &msg_127513[0]},
{0 , 0, 0, NULL, &msg_error [0]}};
static PGN navpgn[] = {{ 59392, 0, 0, hnd_059392, &msg_059392[0]},
{ 60928, 0, 0, hnd_060928, &msg_060928[0]},
{126208, 0, 0, hnd_126208, &msg_126208[0]},
{126464, 1, 0, hnd_126464, &msg_126464[0]},
{126992, 0, 0, hnd_126992, &msg_126992[0]},
{126996, 1, 0, hnd_126996, &msg_126996[0]},
{127245, 0, 4, hnd_127245, &msg_127245[0]},
{127250, 0, 4, hnd_127250, &msg_127250[0]},
{127258, 0, 0, hnd_127258, &msg_127258[0]},
{128259, 0, 4, hnd_128259, &msg_128259[0]},
{128267, 0, 4, hnd_128267, &msg_128267[0]},
{128275, 1, 4, hnd_128275, &msg_128275[0]},
{129283, 0, 0, hnd_129283, &msg_129283[0]},
{129284, 1, 0, hnd_129284, &msg_129284[0]},
{129285, 1, 0, hnd_129285, &msg_129285[0]},
{130306, 0, 4, hnd_130306, &msg_130306[0]},
{130310, 0, 4, hnd_130310, &msg_130310[0]},
{130311, 0, 4, hnd_130311, &msg_130311[0]},
{0 , 0, 0, NULL, &msg_error [0]}};
static PGN *search_pgnlist(unsigned int pgn, PGN *pgnlist)
{
int l1;
PGN *work;
l1 = 0;
work = NULL;
while (pgnlist[l1].pgn != 0) {
if (pgnlist[l1].pgn == pgn) {
work = &pgnlist[l1];
break;
} else {
l1 = l1 + 1;
}
}
return work;
}
static void find_pgn(struct can_frame *frame, struct gps_device_t *session)
{
unsigned int can_net;
session->driver.nmea2000.workpgn = NULL;
can_net = session->driver.nmea2000.can_net;
if (can_net > (NMEA2000_NETS-1)) {
gpsd_log(&session->context->errout, LOG_ERROR,
"NMEA2000 find_pgn: Invalid can network %d.\n", can_net);
return;
}
if (frame->can_id & 0x80000000) {
// cppcheck-suppress unreadVariable
#ifdef __UNUSED__
unsigned int source_prio;
unsigned int daddr;
#endif
// cppcheck-suppress unreadVariable
unsigned int source_pgn;
unsigned int source_unit;
#if LOG_FILE
if (logFile != NULL) {
struct timespec msgTime;
clock_gettime(CLOCK_REALTIME, &msgTime);
(void)fprintf(logFile,
"(%010ld.%06ld) can0 %08x#",
(long)msgTime.tv_sec,
msgTime.tv_nsec / 1000,
frame->can_id & 0x1ffffff);
if ((frame->can_dlc & 0x0f) > 0) {
int l1;
for(l1=0;l1<(frame->can_dlc & 0x0f);l1++) {
(void)fprintf(logFile, "%02x", frame->data[l1]);
}
}
(void)fprintf(logFile, "\n");
}
#endif /* of if LOG_FILE */
session->driver.nmea2000.can_msgcnt += 1;
source_pgn = (frame->can_id >> 8) & 0x1ffff;
#ifdef __UNUSED__
source_prio = (frame->can_id >> 26) & 0x7;
#endif
source_unit = frame->can_id & 0x0ff;
if (((source_pgn & 0x0ff00) >> 8) < 240) {
#ifdef __UNUSED__
daddr = source_pgn & 0x000ff;
#endif
source_pgn = source_pgn & 0x1ff00;
} else {
#ifdef __UNUSED__
daddr = 0xff;
#endif
}
if (!session->driver.nmea2000.unit_valid) {
unsigned int l1, l2;
for (l1=0;l1<NMEA2000_NETS;l1++) {
for (l2=0;l2<NMEA2000_UNITS;l2++) {
if (session == nmea2000_units[l1][l2]) {
session->driver.nmea2000.unit = l2;
session->driver.nmea2000.unit_valid = true;
session->driver.nmea2000.can_net = l1;
can_net = l1;
}
}
}
}
if (!session->driver.nmea2000.unit_valid) {
session->driver.nmea2000.unit = source_unit;
session->driver.nmea2000.unit_valid = true;
nmea2000_units[can_net][source_unit] = session;
}
if (source_unit == session->driver.nmea2000.unit) {
PGN *work;
if (session->driver.nmea2000.pgnlist != NULL) {
work = search_pgnlist(source_pgn, session->driver.nmea2000.pgnlist);
} else {
PGN *pgnlist;
pgnlist = &gpspgn[0];
work = search_pgnlist(source_pgn, pgnlist);
if (work == NULL) {
pgnlist = &aispgn[0];
work = search_pgnlist(source_pgn, pgnlist);
}
if (work == NULL) {
pgnlist = &pwrpgn[0];
work = search_pgnlist(source_pgn, pgnlist);
}
if (work == NULL) {
pgnlist = &navpgn[0];
work = search_pgnlist(source_pgn, pgnlist);
}
if ((work != NULL) && (work->type > 0)) {
session->driver.nmea2000.pgnlist = pgnlist;
}
}
if (work != NULL) {
if (work->fast == 0) {
size_t l2;
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d:%s \n", work->pgn, work->name);
session->driver.nmea2000.workpgn = (void *) work;
session->lexer.outbuflen = frame->can_dlc & 0x0f;
for (l2=0;l2<session->lexer.outbuflen;l2++) {
session->lexer.outbuffer[l2]= frame->data[l2];
}
} else if ((frame->data[0] & 0x1f) == 0) {
unsigned int l2;
session->driver.nmea2000.fast_packet_len = frame->data[1];
session->driver.nmea2000.idx = frame->data[0];
#if NMEA2000_FAST_DEBUG
gpsd_log(&session->context->errout, LOG_ERROR,
"Set idx %2x %2x %2x %6d\n",
frame->data[0],
session->driver.nmea2000.unit,
frame->data[1],
source_pgn);
#endif /* of #if NMEA2000_FAST_DEBUG */
session->lexer.inbuflen = 0;
session->driver.nmea2000.idx += 1;
for (l2=2;l2<8;l2++) {
session->lexer.inbuffer[session->lexer.inbuflen++] = frame->data[l2];
}
gpsd_log(&session->context->errout, LOG_DATA,
"pgn %6d:%s \n", work->pgn, work->name);
} else if (frame->data[0] == session->driver.nmea2000.idx) {
unsigned int l2;
for (l2=1;l2<8;l2++) {
if (session->driver.nmea2000.fast_packet_len > session->lexer.inbuflen) {
session->lexer.inbuffer[session->lexer.inbuflen++] = frame->data[l2];
}
}
if (session->lexer.inbuflen == session->driver.nmea2000.fast_packet_len) {
#if NMEA2000_FAST_DEBUG
gpsd_log(&session->context->errout, LOG_ERROR,
"Fast done %2x %2x %2x %2x %6d\n",
session->driver.nmea2000.idx,
frame->data[0],
session->driver.nmea2000.unit,
(unsigned int) session->driver.nmea2000.fast_packet_len,
source_pgn);
#endif /* of #if NMEA2000_FAST_DEBUG */
session->driver.nmea2000.workpgn = (void *) work;
session->lexer.outbuflen = session->driver.nmea2000.fast_packet_len;
for(l2=0;l2 < (unsigned int)session->lexer.outbuflen; l2++) {
session->lexer.outbuffer[l2] = session->lexer.inbuffer[l2];
}
session->driver.nmea2000.fast_packet_len = 0;
} else {
session->driver.nmea2000.idx += 1;
}
} else {
gpsd_log(&session->context->errout, LOG_ERROR,
"Fast error %2x %2x %2x %2x %6d\n",
session->driver.nmea2000.idx,
frame->data[0],
session->driver.nmea2000.unit,
(unsigned int) session->driver.nmea2000.fast_packet_len,
source_pgn);
}
} else {
gpsd_log(&session->context->errout, LOG_WARN,
"PGN not found %08d %08x \n",
source_pgn, source_pgn);
}
} else {
// we got a unknown unit number
if (nmea2000_units[can_net][source_unit] == NULL) {
char buffer[32];
(void) snprintf(buffer,
sizeof(buffer),
"nmea2000://%s:%u",
can_interface_name[can_net],
source_unit);
if (gpsd_add_device != NULL) {
(void) gpsd_add_device(buffer, true);
}
}
}
} else {
// we got RTR or 2.0A CAN frame, not used
}
}
static ssize_t nmea2000_get(struct gps_device_t *session)
{
struct can_frame frame;
ssize_t status;
session->lexer.outbuflen = 0;
status = read(session->gpsdata.gps_fd, &frame, sizeof(frame));
if (status == (ssize_t)sizeof(frame)) {
session->lexer.type = NMEA2000_PACKET;
find_pgn(&frame, session);
return frame.can_dlc & 0x0f;
}
return 0;
}
static gps_mask_t nmea2000_parse_input(struct gps_device_t *session)
{
gps_mask_t mask;
PGN *work;
// printf("NMEA2000 parse_input called\n");
mask = 0;
work = (PGN *) session->driver.nmea2000.workpgn;
if (work != NULL) {
mask = (work->func)(&session->lexer.outbuffer[0], (int)session->lexer.outbuflen, work, session);
session->driver.nmea2000.workpgn = NULL;
}
session->lexer.outbuflen = 0;
return mask;
}
int nmea2000_open(struct gps_device_t *session)
{
char interface_name[strlen(session->gpsdata.dev.path)+1];
socket_t sock;
int status;
int unit_number;
int can_net;
unsigned int l;
struct ifreq ifr;
struct sockaddr_can addr;
char *unit_ptr;
INVALIDATE_SOCKET(session->gpsdata.gps_fd);
session->driver.nmea2000.can_net = 0;
can_net = -1;
unit_number = -1;
(void)strlcpy(interface_name, session->gpsdata.dev.path + 11, sizeof(interface_name));
unit_ptr = NULL;
for (l=0;l<strnlen(interface_name,sizeof(interface_name));l++) {
if (interface_name[l] == ':') {
unit_ptr = &interface_name[l+1];
interface_name[l] = 0;
continue;
}
if (unit_ptr != NULL) {
if (isdigit(interface_name[l]) == 0) {
gpsd_log(&session->context->errout, LOG_ERROR,
"NMEA2000 open: Invalid character in unit number.\n");
return -1;
}
}
}
if (unit_ptr != NULL) {
unit_number = atoi(unit_ptr);
if ((unit_number < 0) || (unit_number > (NMEA2000_UNITS-1))) {
gpsd_log(&session->context->errout, LOG_ERROR,
"NMEA2000 open: Unit number out of range.\n");
return -1;
}
for (l = 0; l < NMEA2000_NETS; l++) {
if (strncmp(can_interface_name[l],
interface_name,
MIN(sizeof(interface_name), sizeof(can_interface_name[l]))) == 0) {
can_net = l;
break;
}
}
if (can_net < 0) {
gpsd_log(&session->context->errout, LOG_ERROR,
"NMEA2000 open: CAN device not open: %s .\n", interface_name);
return -1;
}
} else {
for (l = 0; l < NMEA2000_NETS; l++) {
if (strncmp(can_interface_name[l],
interface_name,
MIN(sizeof(interface_name), sizeof(can_interface_name[l]))) == 0) {
gpsd_log(&session->context->errout, LOG_ERROR, "NMEA2000 open: CAN device duplicate open: %s .\n", interface_name);
return -1;
}
}
for (l = 0; l < NMEA2000_NETS; l++) {
if (can_interface_name[l][0] == 0) {
can_net = l;
break;
}
}
if (can_net < 0) {
gpsd_log(&session->context->errout, LOG_ERROR,
"NMEA2000 open: Too many CAN networks open.\n");
return -1;
}
}
/* Create the socket */
sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (BAD_SOCKET(sock)) {
gpsd_log(&session->context->errout, LOG_ERROR,
"NMEA2000 open: can not get socket.\n");
return -1;
}
status = fcntl(sock, F_SETFL, O_NONBLOCK);
if (status != 0) {
gpsd_log(&session->context->errout, LOG_ERROR,
"NMEA2000 open: can not set socket to O_NONBLOCK.\n");
close(sock);
return -1;
}
/* Locate the interface you wish to use */
strlcpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name));
status = ioctl(sock, SIOCGIFINDEX, &ifr); /* ifr.ifr_ifindex gets filled
* with that device's index */
if (status != 0) {
gpsd_log(&session->context->errout, LOG_ERROR,
"NMEA2000 open: can not find CAN device.\n");
close(sock);
return -1;
}
/* Select that CAN interface, and bind the socket to it. */
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
status = bind(sock, (struct sockaddr*)&addr, sizeof(addr) );
if (status != 0) {
gpsd_log(&session->context->errout, LOG_ERROR,
"NMEA2000 open: bind failed.\n");
close(sock);
return -1;
}
gpsd_switch_driver(session, "NMEA2000");
session->gpsdata.gps_fd = sock;
session->sourcetype = source_can;
session->servicetype = service_sensor;
session->driver.nmea2000.can_net = can_net;
if (unit_ptr != NULL) {
nmea2000_units[can_net][unit_number] = session;
session->driver.nmea2000.unit = unit_number;
session->driver.nmea2000.unit_valid = true;
} else {
strlcpy(can_interface_name[can_net],
interface_name,
MIN(sizeof(can_interface_name[0]), sizeof(interface_name)));
session->driver.nmea2000.unit_valid = false;
for (l=0;l<NMEA2000_UNITS;l++) {
nmea2000_units[can_net][l] = NULL;
}
}
session->gpsdata.dev.parity = 'n';
session->gpsdata.dev.baudrate = 250000;
session->gpsdata.dev.stopbits = 0;
return session->gpsdata.gps_fd;
}
void nmea2000_close(struct gps_device_t *session)
{
if (!BAD_SOCKET(session->gpsdata.gps_fd)) {
gpsd_log(&session->context->errout, LOG_SPIN,
"close(%d) in nmea2000_close(%s)\n",
session->gpsdata.gps_fd, session->gpsdata.dev.path);
(void)close(session->gpsdata.gps_fd);
INVALIDATE_SOCKET(session->gpsdata.gps_fd);
if (session->driver.nmea2000.unit_valid) {
unsigned int l1, l2;
for (l1=0;l1<NMEA2000_NETS;l1++) {
for (l2=0;l2<NMEA2000_UNITS;l2++) {
if (session == nmea2000_units[l1][l2]) {
session->driver.nmea2000.unit_valid = false;
session->driver.nmea2000.unit = 0;
session->driver.nmea2000.can_net = 0;
nmea2000_units[l1][l2] = NULL;
}
}
}
}
}
}
/* *INDENT-OFF* */
const struct gps_type_t driver_nmea2000 = {
.type_name = "NMEA2000", /* full name of type */
.packet_type = NMEA2000_PACKET, /* associated lexer packet type */
.flags = DRIVER_STICKY, /* remember this */
.trigger = NULL, /* detect their main sentence */
.channels = 12, /* not an actual GPS at all */
.probe_detect = NULL,
.get_packet = nmea2000_get, /* how to get a packet */
.parse_packet = nmea2000_parse_input, /* how to interpret a packet */
.rtcm_writer = NULL, /* Don't send RTCM to this */
.init_query = NULL, /* non-perturbing query */
.event_hook = NULL,
#ifdef RECONFIGURE_ENABLE
.speed_switcher = NULL, /* no speed switcher */
.mode_switcher = NULL, /* no mode switcher */
.rate_switcher = NULL, /* no rate switcher */
.min_cycle = 1, /* nominal 1-per-second GPS cycle */
#endif /* RECONFIGURE_ENABLE */
#ifdef CONTROLSEND_ENABLE
.control_send = NULL, /* how to send control strings */
#endif /* CONTROLSEND_ENABLE */
#ifdef TIMEHINT_ENABLE
.time_offset = NULL,
#endif /* TIMEHINT_ENABLE */
};
/* *INDENT-ON* */
/* end */
#endif /* of defined(NMEA2000_ENABLE) */
|