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 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698
|
/*
* generic reference clock driver for several DCF/GPS/MSF/... receivers
*
* PPS notes:
* On systems that support PPSAPI (RFC 2783) PPSAPI is the
* preferred interface.
*
* Copyright (c) 1989-2015 by Frank Kardel <kardel@ntp.org>
* Copyright 2015 by the NTPsec project contributors
* SPDX-License-Identifier: BSD-3-Clause
*
* Note: some subtypes are obsolete and could probably stand to be removed
* next time this code gets serious attention. In particular, subtypes 9 and 10
* support the Trimble SVeeSix, which was discontinued before 2003. Related
* code in the parse library could also be dropped.
* Also see subtypes 3 and 4, for which no information in use since 1999 and
* 2001 respectively can be found on the web.
*
* WARNING: Most modes of this driver depend on the system clock for
* year disambiguation. They will thus not be usable for recovery if
* the system clock is trashed. The only exceptions are the Scheitzer 240x
* and the two Trimble devices.
*/
#include "config.h"
#include "ntp_types.h"
#include "timespecops.h"
/*
* This driver currently provides the support for
* - Meinberg receiver DCF77 PZF535 (TCXO version) (DCF)
* - Meinberg receiver DCF77 PZF535 (OCXO version) (DCF)
* - Meinberg receiver DCF77 PZF509 (DCF)
* - Meinberg receiver DCF77 AM receivers (e.g. C51) (DCF)
* - IGEL CLOCK (DCF)
* - ELV DCF7000 (DCF)
* - Schmid clock (DCF)
* - Conrad DCF77 receiver module (DCF)
* - FAU DCF77 NTP receiver (TimeBrick) (DCF)
* - WHARTON 400A Series clock (DCF)
*
* - Meinberg GPS receivers (GPS)
* - Trimble (TSIP and TAIP protocol) (GPS)
*
* - RCC8000 MSF Receiver (MSF)
* - VARITEXT clock (MSF)
*/
/*
* Meinberg receivers are usually connected via a
* 9600/7E1 or 19200/8N1 serial line.
*
* The Meinberg GPS receivers also have a special NTP time stamp
* format. The firmware release is Uni-Erlangen.
*
* Meinberg generic receiver setup:
* output time code every second
* Baud rate 9600 7E2S
*
* Meinberg GPS receiver setup:
* output time code every second
* Baudrate 19200 8N1
*
* This software supports the standard data formats used
* in Meinberg receivers.
*
* Special software versions are only sensible for the
* oldest GPS receiver, GPS16x. For newer receiver types
* the output string format can be configured at the device,
* and the device name is generally GPSxxx instead of GPS16x.
*
* Meinberg can be reached via: http://www.meinberg.de/
*/
#include "ntpd.h"
#include "ntp_refclock.h"
#include "ntp_calendar.h"
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
#include <termios.h>
#define TTY_GETATTR(_FD_, _ARG_) tcgetattr((_FD_), (_ARG_))
#define TTY_SETATTR(_FD_, _ARG_) tcsetattr((_FD_), TCSANOW, (_ARG_))
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#ifdef HAVE_PPSAPI
# include "ppsapi_timepps.h"
# include "refclock_pps.h"
#endif
#ifdef HAVE_LINUX_SERIAL_H
# include <linux/serial.h>
#endif
#define BUFFER_SIZE(_BUF, _PTR) ((size_t)((_BUF) + sizeof(_BUF) - (_PTR)))
#define BUFFER_SIZES(_BUF, _PTR, _SZ) ((int)((_BUF) + (_SZ) - (_PTR)))
/*
* COND_DEF can be conditionally defined as DEF or 0. If defined as DEF
* then some more parse-specific variables are flagged to be printed with
* "ntpq -c cv <assid>". This can be lengthy, so by default COND_DEF
* should be defined as 0.
*/
#if 0
# define COND_DEF DEF // enable this for testing
#else
# define COND_DEF 0 // enable this by default
#endif
#include "ntp_io.h"
#include "ntp_stdlib.h"
#include "parse.h"
#include "mbg_gps166.h"
#include "trimble.h"
#include "binio.h"
#include "ascii.h"
#include "recvbuff.h"
#define VERSION "4.81 2009/05/01 10:15:29"
/**===========================================================================
** external interface to ntp mechanism
**/
static bool parse_start (int, struct peer *);
static void parse_shutdown (struct refclockproc *);
static void parse_poll (int, struct peer *);
static void parse_control (int, const struct refclockstat *, struct refclockstat *, struct peer *);
struct refclock refclock_parse = {
"GENERIC", /* basename of driver */
parse_start, /* start up driver */
parse_shutdown, /* shut down driver */
parse_poll, /* transmit poll message */
parse_control, /* control settings */
NULL, /* init */
NULL, /* timer */
};
/*
* Definitions
*/
#define PARSEDEVICE "/dev/refclock-%u" /* device to open %d is unit number */
#define PARSEPPSDEVICE "/dev/refclockpps-%u" /* optional pps device to open %d is unit number */
#undef ABS
#define ABS(_X_) (((_X_) < 0) ? -(_X_) : (_X_))
#ifdef HAVE_PPSAPI
# define PARSE_HARDPPS_DISABLE 0
# define PARSE_HARDPPS_ENABLE 1
#endif
/**===========================================================================
** function vector for dynamically binding io handling mechanism
**/
struct parseunit; /* to keep inquiring minds happy */
typedef struct bind
{
const char *bd_description; /* name of type of binding */
int (*bd_init) (struct parseunit *); /* initialize */
void (*bd_end) (struct parseunit *); /* end */
bool (*bd_setcs) (struct parseunit *, parsectl_t *); /* set character size */
int (*bd_disable) (struct parseunit *); /* disable */
int (*bd_enable) (struct parseunit *); /* enable */
int (*bd_getfmt) (struct parseunit *, parsectl_t *); /* get format */
int (*bd_setfmt) (struct parseunit *, parsectl_t *); /* setfmt */
int (*bd_timecode) (struct parseunit *, parsectl_t *); /* get time code */
void (*bd_receive) (struct recvbuf *); /* receive operation */
int (*bd_io_input) (struct recvbuf *); /* input operation */
} bind_t;
#define PARSE_END(_X_) (*(_X_)->binding->bd_end)(_X_)
#define PARSE_SETCS(_X_, _CS_) (*(_X_)->binding->bd_setcs)(_X_, _CS_)
#define PARSE_GETFMT(_X_, _DCT_) (*(_X_)->binding->bd_getfmt)(_X_, _DCT_)
#define PARSE_SETFMT(_X_, _DCT_) (*(_X_)->binding->bd_setfmt)(_X_, _DCT_)
#define PARSE_GETTIMECODE(_X_, _DCT_) (*(_X_)->binding->bd_timecode)(_X_, _DCT_)
/*
* special handling flags
*/
#define PARSE_F_PPSONSECOND 0x00000001 /* PPS pulses are on second */
#define PARSE_F_POWERUPTRUST 0x00000100 /* POWERUP state ist trusted for */
/* trusttime after SYNC was seen */
/**===========================================================================
** error message regression handling
**
** there are quite a few errors that can occur in rapid succession such as
** noisy input data or no data at all. in order to reduce the amount of
** syslog messages in such case, we are using a backoff algorithm. We limit
** the number of error messages of a certain class to 1 per time unit. if a
** configurable number of messages is displayed that way, we move on to the
** next time unit / count for that class. a count of messages that have been
** suppressed is held and displayed whenever a corresponding message is
** displayed. the time units for a message class will also be displayed.
** whenever an error condition clears we reset the error message state,
** thus we would still generate much output on pathological conditions
** where the system oscillates between OK and NOT OK states. coping
** with that condition is currently considered too complicated.
**/
#define ERR_ALL (unsigned)~0 /* "all" errors */
#define ERR_BADDATA (unsigned)0 /* unusable input data/conversion errors */
#define ERR_NODATA (unsigned)1 /* no input data */
#define ERR_BADIO (unsigned)2 /* read/write/select errors */
#define ERR_BADSTATUS (unsigned)3 /* unsync states */
#define ERR_INTERNAL (unsigned)5 /* internal error */
#define ERR_CNT (unsigned)(ERR_INTERNAL+1)
#define ERR(_X_) if (list_err(parse, (_X_)))
struct errorregression
{
unsigned long err_count; /* number of repetitions per class */
unsigned long err_delay; /* minimum delay between messages */
};
static struct errorregression
err_baddata[] = /* error messages for bad input data */
{
{ 1, 0 }, /* output first message immediately */
{ 5, 60 }, /* output next five messages in 60 second intervals */
{ 3, SECSPERHR }, /* output next 3 messages in hour intervals */
{ 0, 12 * SECSPERHR } /* repeat messages only every 12 hours */
};
static struct errorregression
err_nodata[] = /* error messages for missing input data */
{
{ 1, 0 }, /* output first message immediately */
{ 5, 60 }, /* output next five messages in 60 second intervals */
{ 3, SECSPERHR }, /* output next 3 messages in hour intervals */
{ 0, 12 * SECSPERHR } /* repeat messages only every 12 hours */
};
static struct errorregression
err_badstatus[] = /* unsynchronized state messages */
{
{ 1, 0 }, /* output first message immediately */
{ 5, 60 }, /* output next five messages in 60 second intervals */
{ 3, SECSPERHR }, /* output next 3 messages in hour intervals */
{ 0, 12 * SECSPERHR } /* repeat messages only every 12 hours */
};
static struct errorregression
err_badio[] = /* io failures (bad reads, selects, ...) */
{
{ 1, 0 }, /* output first message immediately */
{ 5, 60 }, /* output next five messages in 60 second intervals */
{ 5, SECSPERHR }, /* output next 3 messages in hour intervals */
{ 0, 12 * SECSPERHR } /* repeat messages only every 12 hours */
};
static struct errorregression
err_badevent[] = /* non nominal events */
{
{ 20, 0 }, /* output first message immediately */
{ 6, 60 }, /* output next five messages in 60 second intervals */
{ 5, SECSPERHR }, /* output next 3 messages in hour intervals */
{ 0, 12 * SECSPERHR } /* repeat messages only every 12 hours */
};
static struct errorregression
err_internal[] = /* really bad things - basically coding/OS errors */
{
{ 0, 0 }, /* output all messages immediately */
};
static struct errorregression *
err_tbl[] =
{
err_baddata,
err_nodata,
err_badio,
err_badstatus,
err_badevent,
err_internal
};
struct errorinfo
{
uptime_t err_started; /* begin time (ntp) of error condition */
uptime_t err_last; /* last time (ntp) error occurred */
unsigned long err_cnt; /* number of error repetitions */
unsigned long err_suppressed; /* number of suppressed messages */
struct errorregression *err_stage; /* current error stage */
};
/**===========================================================================
** refclock instance data
**/
struct parseunit
{
/*
* NTP management
*/
struct peer *peer; /* backlink to peer structure - refclock inactive if 0 */
struct refclockproc *generic; /* backlink to refclockproc structure */
/*
* PARSE io
*/
bind_t *binding; /* io handling binding */
/*
* parse state
*/
parse_t parseio; /* io handling structure (user level parsing) */
/*
* type specific parameters
*/
struct parse_clockinfo *parse_type; /* link to clock description */
/*
* clock state handling/reporting
*/
uint8_t flags; /* flags (leap_control) */
uptime_t lastchange; /* time (ntp) when last state change accured */
unsigned long statetime[CEVNT_MAX+1]; /* accumulated time of clock states */
unsigned long pollneeddata; /* current_time(!=0) for receive sample expected in PPS mode */
unsigned short lastformat; /* last format used */
unsigned long lastsync; /* time (ntp) when clock was last seen fully synchronized */
unsigned long maxunsync; /* max time in seconds a receiver is trusted after losing synchronisation */
double ppsphaseadjust; /* phase adjustment of PPS time stamp */
unsigned long lastmissed; /* time (ntp) when poll didn't get data (powerup heuristic) */
unsigned long ppsserial; /* magic cookie for ppsclock serials (avoids stale ppsclock data) */
int ppsfd; /* fd to ise for PPS io */
#ifdef HAVE_PPSAPI
int hardppsstate; /* current hard pps state */
struct refclock_ppsctl ppsctl; /* PPSAPI structure */
#endif
parsetime_t timedata; /* last (parse module) data */
void *localdata; /* optional local, receiver-specific data */
unsigned long localstate; /* private local state */
struct errorinfo errors[ERR_CNT]; /* error state table for suppressing excessive error messages */
struct ctl_var *kv; /* additional pseudo variables */
uptime_t laststatistic; /* time when staticstics where output */
};
/**===========================================================================
** Clockinfo section all parameter for specific clock types
** includes NTP parameters, TTY parameters and IO handling parameters
**/
static void poll_dpoll (struct parseunit *);
static void poll_poll (struct peer *);
static bool poll_init (struct parseunit *);
typedef struct poll_info
{
unsigned long rate; /* poll once every "rate" seconds - 0 off */
const char *string; /* string to send for polling */
unsigned long count; /* number of characters in string */
} poll_info_t;
#define NO_CL_FLAGS 0
#define NO_POLL 0
#define NO_INIT 0
#define NO_END 0
#define NO_EVENT 0
#define NO_LCLDATA 0
#define NO_MESSAGE 0
#define DCF_ID "DCF" /* generic DCF */
#define DCF_A_ID "DCFa" /* AM demodulation */
#define DCF_P_ID "DCFp" /* pseudo random phase shift */
#define GPS_ID "GPS" /* GPS receiver */
#define MSF_ID "MSF" /* MSF receiver */
#define DCF_TYPE CTL_SST_TS_LF
#define GPS_TYPE CTL_SST_TS_UHF
/*
* receiver specific constants
*/
#define MBG_SPEED (B9600)
#define MBG_CFLAG (CS7|PARENB|CREAD|CLOCAL|HUPCL|CSTOPB)
#define MBG_IFLAG (IGNBRK|IGNPAR|ISTRIP)
#define MBG_OFLAG 0
#define MBG_LFLAG 0
#define MBG_FLAGS PARSE_F_PPSONSECOND
/*
* Meinberg DCF77 receivers
*/
#define DCFUA31_ROOTDELAY 0.0 /* 0 */
#define DCFUA31_BASEDELAY 0.010 /* 10.7421875ms: 10 ms (+/- 3 ms) */
#define DCFUA31_NAME "MEINBERG_C51"
#define DCFUA31_DESCRIPTION "Meinberg DCF77 C51 or compatible"
#define DCFUA31_MAXUNSYNC 60*30 /* only trust clock for 1/2 hour */
#define DCFUA31_SPEED MBG_SPEED
#define DCFUA31_CFLAG MBG_CFLAG
#define DCFUA31_IFLAG MBG_IFLAG
#define DCFUA31_OFLAG MBG_OFLAG
#define DCFUA31_LFLAG MBG_LFLAG
#define DCFUA31_SAMPLES 5
#define DCFUA31_KEEP 3
#define DCFUA31_FORMAT "Meinberg Standard"
/*
* Meinberg DCF PZF535/TCXO (FM/PZF) receiver
*/
#define DCFPZF535_ROOTDELAY 0.0
#define DCFPZF535_BASEDELAY 0.001968 /* 1.968ms +- 104us (oscilloscope) - relative to start (end of STX) */
#define DCFPZF535_NAME "MEINBERG_5XX"
#define DCFPZF535_DESCRIPTION "Meinberg DCF PZF 535/509 / TCXO"
#define DCFPZF535_MAXUNSYNC 60*60*12 /* only trust clock for 12 hours
* @ 5e-8df/f we have accumulated
* at most 2.16 ms (thus we move to
* NTP synchronisation */
#define DCFPZF535_SPEED MBG_SPEED
#define DCFPZF535_CFLAG MBG_CFLAG
#define DCFPZF535_IFLAG MBG_IFLAG
#define DCFPZF535_OFLAG MBG_OFLAG
#define DCFPZF535_LFLAG MBG_LFLAG
#define DCFPZF535_SAMPLES 5
#define DCFPZF535_KEEP 3
#define DCFPZF535_FORMAT "Meinberg Standard"
/*
* Meinberg DCF PZF535/OCXO receiver
*/
#define DCFPZF535OCXO_ROOTDELAY 0.0
#define DCFPZF535OCXO_BASEDELAY 0.001968 /* 1.968ms +- 104us (oscilloscope) - relative to start (end of STX) */
#define DCFPZF535OCXO_NAME "MEINBERG_5XX"
#define DCFPZF535OCXO_DESCRIPTION "Meinberg DCF PZF 535/509 / OCXO"
#define DCFPZF535OCXO_MAXUNSYNC 60*60*96 /* only trust clock for 4 days
* @ 5e-9df/f we have accumulated
* at most an error of 1.73 ms
* (thus we move to NTP synchronisation) */
#define DCFPZF535OCXO_SPEED MBG_SPEED
#define DCFPZF535OCXO_CFLAG MBG_CFLAG
#define DCFPZF535OCXO_IFLAG MBG_IFLAG
#define DCFPZF535OCXO_OFLAG MBG_OFLAG
#define DCFPZF535OCXO_LFLAG MBG_LFLAG
#define DCFPZF535OCXO_SAMPLES 5
#define DCFPZF535OCXO_KEEP 3
#define DCFPZF535OCXO_FORMAT "Meinberg Standard"
/*
* Meinberg GPS receivers
*/
static void gps16x_message (struct parseunit *, parsetime_t *);
static bool gps16x_poll_init (struct parseunit *);
#define GPS16X_ROOTDELAY 0.0 /* nothing here */
#define GPS16X_BASEDELAY 0.001968 /* XXX to be fixed ! 1.968ms +- 104us (oscilloscope) - relative to start (end of STX) */
#define GPS16X_NAME "GPS_MEINBERG"
#define GPS16X_DESCRIPTION "Meinberg GPS receiver"
#define GPS16X_MAXUNSYNC 60*60*96 /* only trust clock for 4 days
* @ 5e-9df/f we have accumulated
* at most an error of 1.73 ms
* (thus we move to NTP synchronisation) */
#define GPS16X_SPEED B19200
#define GPS16X_CFLAG (CS8|CREAD|CLOCAL|HUPCL)
#define GPS16X_IFLAG (IGNBRK|IGNPAR)
#define GPS16X_OFLAG MBG_OFLAG
#define GPS16X_LFLAG MBG_LFLAG
#define GPS16X_POLLRATE 6
#define GPS16X_POLLCMD ""
#define GPS16X_CMDSIZE 0
static poll_info_t gps16x_pollinfo = { GPS16X_POLLRATE, GPS16X_POLLCMD, GPS16X_CMDSIZE };
#define GPS16X_INIT gps16x_poll_init
#define GPS16X_POLL 0
#define GPS16X_END 0
#define GPS16X_DATA ((void *)(&gps16x_pollinfo))
#define GPS16X_MESSAGE gps16x_message
#define GPS16X_ID GPS_ID
#define GPS16X_FORMAT "Meinberg GPS Extended"
#define GPS16X_SAMPLES 5
#define GPS16X_KEEP 3
/*
* ELV DCF7000 Wallclock-Receiver/Switching Clock (Kit)
*
* This is really not the hottest clock - but before you have nothing ...
*/
#define DCF7000_ROOTDELAY 0.0 /* 0 */
#define DCF7000_BASEDELAY 0.405 /* slow blow */
#define DCF7000_NAME "ELV_DCF7000"
#define DCF7000_DESCRIPTION "ELV DCF7000"
#define DCF7000_MAXUNSYNC (60*5) /* sorry - but it just was not build as a clock */
#define DCF7000_SPEED (B9600)
#define DCF7000_CFLAG (CS8|CREAD|PARENB|PARODD|CLOCAL|HUPCL)
#define DCF7000_IFLAG (IGNBRK)
#define DCF7000_OFLAG 0
#define DCF7000_LFLAG 0
#define DCF7000_SAMPLES 5
#define DCF7000_KEEP 3
#define DCF7000_FORMAT "ELV DCF7000"
/*
* Schmid DCF Receiver Kit
*
* When the WSDCF clock is operating optimally we want the primary clock
* distance to come out at 300 ms. Thus, peer.distance in the WSDCF peer
* structure is set to 290 ms and we compute delays which are at least
* 10 ms long. The following are 290 ms and 10 ms expressed in unsigned fp format
*/
#define WS_POLLRATE 1 /* every second - watch interdependency with poll routine */
#define WS_POLLCMD "\163"
#define WS_CMDSIZE 1
static poll_info_t wsdcf_pollinfo = { WS_POLLRATE, WS_POLLCMD, WS_CMDSIZE };
#define WSDCF_INIT poll_init
#define WSDCF_POLL poll_dpoll
#define WSDCF_END 0
#define WSDCF_DATA ((void *)(&wsdcf_pollinfo))
#define WSDCF_ROOTDELAY 0.0 /* 0 */
#define WSDCF_BASEDELAY 0.010 /* ~ 10ms */
#define WSDCF_NAME "WSDCF"
#define WSDCF_DESCRIPTION "WS/DCF Receiver"
#define WSDCF_FORMAT "Schmid"
#define WSDCF_MAXUNSYNC (60*60) /* assume this beast hold at 1 h better than 2 ms XXX-must verify */
#define WSDCF_SPEED (B1200)
#define WSDCF_CFLAG (CS8|CREAD|CLOCAL)
#define WSDCF_IFLAG 0
#define WSDCF_OFLAG 0
#define WSDCF_LFLAG 0
#define WSDCF_SAMPLES 5
#define WSDCF_KEEP 3
/*
* RAW DCF77 - input of DCF marks via RS232 - many variants
*/
#define RAWDCF_FLAGS 0
#define RAWDCF_ROOTDELAY 0.0 /* 0 */
#define RAWDCF_BASEDELAY 0.258
#define RAWDCF_FORMAT "RAW DCF77 Timecode"
#define RAWDCF_MAXUNSYNC (0) /* sorry - its a true receiver - no signal - no time */
#define RAWDCF_SPEED (B50)
#define RAWDCF_CFLAG (CS8|CREAD|CLOCAL|PARENB)
#define RAWDCF_IFLAG (IGNPAR)
#define RAWDCF_OFLAG 0
#define RAWDCF_LFLAG 0
#define RAWDCF_SAMPLES 20
#define RAWDCF_KEEP 12
#define RAWDCF_INIT 0
/*
* RAW DCF variants
*/
/*
* Conrad receiver
*
* simplest (cheapest) DCF clock - e. g. DCF77 receiver by Conrad
* (~40DM - roughly $30 ) followed by a level converter for RS232
*/
#define CONRAD_BASEDELAY 0.292 /* Conrad receiver @ 50 Baud on a Sun */
#define CONRAD_NAME "RAWDCF_CONRAD"
#define CONRAD_DESCRIPTION "RAW DCF77 CODE (Conrad DCF77 receiver module)"
/* Gude Analog- und Digitalsystem GmbH 'Expert mouseCLOCK USB v2.0' */
#define GUDE_EMC_USB_V20_SPEED (B4800)
#define GUDE_EMC_USB_V20_BASEDELAY 0.425 /* USB serial<->USB converter FTDI232R */
#define GUDE_EMC_USB_V20_NAME "RAWDCF_MOUSECLOCK"
#define GUDE_EMC_USB_V20_DESCRIPTION "RAW DCF77 CODE (Expert mouseCLOCK USB v2.0)"
/*
* TimeBrick receiver
*/
#define TIMEBRICK_BASEDELAY 0.210 /* TimeBrick @ 50 Baud on a Sun */
#define TIMEBRICK_NAME "RAWDCF_TIMEBRICK"
#define TIMEBRICK_DESCRIPTION "RAW DCF77 CODE (TimeBrick)"
/*
* IGEL:clock receiver
*/
#define IGELCLOCK_BASEDELAY 0.258 /* IGEL:clock receiver */
#define IGELCLOCK_NAME "RAWDCF_IGEL"
#define IGELCLOCK_DESCRIPTION "RAW DCF77 CODE (IGEL:clock)"
#define IGELCLOCK_SPEED (B1200)
#define IGELCLOCK_CFLAG (CS8|CREAD|HUPCL|CLOCAL)
/*
* RAWDCF receivers that need to be powered from DTR
* (like Expert mouse clock)
*/
static bool rawdcf_init_1 (struct parseunit *);
#define RAWDCFDTRSET_NAME "RAW_DCF77"
#define RAWDCFDTRSET_DESCRIPTION "RAW DCF77 CODE (DTR SET/RTS CLR)"
#define RAWDCFDTRSET75_DESCRIPTION "RAW DCF77 CODE (DTR SET/RTS CLR @ 75 baud)"
#define RAWDCFDTRSET_INIT rawdcf_init_1
/*
* RAWDCF receivers that need to be powered from
* DTR CLR and RTS SET
*/
static bool rawdcf_init_2 (struct parseunit *);
#define RAWDCFDTRCLRRTSSET_NAME "RAW_DCF77"
#define RAWDCFDTRCLRRTSSET_DESCRIPTION "RAW DCF77 CODE (DTR CLR/RTS SET)"
#define RAWDCFDTRCLRRTSSET75_DESCRIPTION "RAW DCF77 CODE (DTR CLR/RTS SET @ 75 baud)"
#define RAWDCFDTRCLRRTSSET_INIT rawdcf_init_2
/*
* Trimble GPS receivers (TAIP and TSIP protocols)
*/
#ifndef TRIM_POLLRATE
#define TRIM_POLLRATE 0 /* only true direct polling */
#endif
#define TRIM_TAIPPOLLCMD ">SRM;FR_FLAG=F;EC_FLAG=F<>QTM<"
#define TRIM_TAIPCMDSIZE (sizeof(TRIM_TAIPPOLLCMD)-1)
static poll_info_t trimbletaip_pollinfo = { TRIM_POLLRATE, TRIM_TAIPPOLLCMD, TRIM_TAIPCMDSIZE };
static bool trimbletaip_init (struct parseunit *);
static void trimbletaip_event (struct parseunit *, int);
/* query time & UTC correction data */
static char tsipquery[] = { DLE, 0x21, DLE, ETX, DLE, 0x2F, DLE, ETX };
static poll_info_t trimbletsip_pollinfo = { TRIM_POLLRATE, tsipquery, sizeof(tsipquery) };
static bool trimbletsip_init (struct parseunit *);
static void trimbletsip_end (struct parseunit *);
static void trimbletsip_message (struct parseunit *, parsetime_t *);
static void trimbletsip_event (struct parseunit *, int);
#define TRIMBLETSIP_IDLE_TIME (300) /* 5 minutes silence at most */
#define TRIMBLE_RESET_HOLDOFF TRIMBLETSIP_IDLE_TIME
#define TRIMBLETAIP_SPEED (B4800)
#define TRIMBLETAIP_CFLAG (CS8|CREAD|CLOCAL)
#define TRIMBLETAIP_IFLAG (BRKINT|IGNPAR|ISTRIP|ICRNL|IXON)
#define TRIMBLETAIP_OFLAG (OPOST|ONLCR)
#define TRIMBLETAIP_LFLAG (0)
#define TRIMBLETSIP_SPEED (B9600)
#define TRIMBLETSIP_CFLAG (CS8|CLOCAL|CREAD|PARENB|PARODD)
#define TRIMBLETSIP_IFLAG (IGNBRK)
#define TRIMBLETSIP_OFLAG (0)
#define TRIMBLETSIP_LFLAG (ICANON)
#define TRIMBLETSIP_SAMPLES 5
#define TRIMBLETSIP_KEEP 3
#define TRIMBLETAIP_SAMPLES 5
#define TRIMBLETAIP_KEEP 3
#define TRIMBLETAIP_FLAGS (PARSE_F_PPSONSECOND)
#define TRIMBLETSIP_FLAGS (TRIMBLETAIP_FLAGS)
#define TRIMBLETAIP_POLL poll_dpoll
#define TRIMBLETSIP_POLL poll_dpoll
#define TRIMBLETAIP_INIT trimbletaip_init
#define TRIMBLETSIP_INIT trimbletsip_init
#define TRIMBLETAIP_EVENT trimbletaip_event
#define TRIMBLETSIP_EVENT trimbletsip_event
#define TRIMBLETSIP_MESSAGE trimbletsip_message
#define TRIMBLETAIP_END 0
#define TRIMBLETSIP_END trimbletsip_end
#define TRIMBLETAIP_DATA ((void *)(&trimbletaip_pollinfo))
#define TRIMBLETSIP_DATA ((void *)(&trimbletsip_pollinfo))
#define TRIMBLETAIP_ID GPS_ID
#define TRIMBLETSIP_ID GPS_ID
#define TRIMBLETAIP_FORMAT "Trimble TAIP"
#define TRIMBLETSIP_FORMAT "Trimble TSIP"
#define TRIMBLETAIP_ROOTDELAY 0x0
#define TRIMBLETSIP_ROOTDELAY 0x0
#define TRIMBLETAIP_BASEDELAY 0.0
#define TRIMBLETSIP_BASEDELAY 0.020 /* GPS time message latency */
#define TRIMBLETAIP_NAME "GPS_TAIP"
#define TRIMBLETSIP_NAME "GPS_TSIP"
#define TRIMBLETAIP_DESCRIPTION "Trimble GPS (TAIP) receiver"
#define TRIMBLETSIP_DESCRIPTION "Trimble GPS (TSIP) receiver"
#define TRIMBLETAIP_MAXUNSYNC 0
#define TRIMBLETSIP_MAXUNSYNC 0
#define TRIMBLETAIP_EOL '<'
/*
* RadioCode Clocks RCC 800 receiver
*/
#define RCC_POLLRATE 0 /* only true direct polling */
#define RCC_POLLCMD "\r"
#define RCC_CMDSIZE 1
static poll_info_t rcc8000_pollinfo = { RCC_POLLRATE, RCC_POLLCMD, RCC_CMDSIZE };
#define RCC8000_POLL poll_dpoll
#define RCC8000_INIT poll_init
#define RCC8000_END 0
#define RCC8000_DATA ((void *)(&rcc8000_pollinfo))
#define RCC8000_ROOTDELAY 0.0
#define RCC8000_BASEDELAY 0.0
#define RCC8000_ID MSF_ID
#define RCC8000_NAME "MSF_RCC8000"
#define RCC8000_DESCRIPTION "RCC 8000 MSF Receiver"
#define RCC8000_FORMAT "Radiocode RCC8000"
#define RCC8000_MAXUNSYNC (60*60) /* should be ok for an hour */
#define RCC8000_SPEED (B2400)
#define RCC8000_CFLAG (CS8|CREAD|CLOCAL)
#define RCC8000_IFLAG (IGNBRK|IGNPAR)
#define RCC8000_OFLAG 0
#define RCC8000_LFLAG 0
#define RCC8000_SAMPLES 5
#define RCC8000_KEEP 3
/*
* Hopf Radio clock 6021 Format
*
*/
#define HOPF6021_ROOTDELAY 0.0
#define HOPF6021_BASEDELAY 0.0
#define HOPF6021_NAME "HOPF_6021"
#define HOPF6021_DESCRIPTION "HOPF 6021"
#define HOPF6021_FORMAT "hopf Funkuhr 6021"
#define HOPF6021_MAXUNSYNC (60*60) /* should be ok for an hour */
#define HOPF6021_SPEED (B9600)
#define HOPF6021_CFLAG (CS8|CREAD|CLOCAL)
#define HOPF6021_IFLAG (IGNBRK|ISTRIP)
#define HOPF6021_OFLAG 0
#define HOPF6021_LFLAG 0
#define HOPF6021_FLAGS 0
#define HOPF6021_SAMPLES 5
#define HOPF6021_KEEP 3
/*
* Diem's Computime Radio Clock Receiver
*/
#define COMPUTIME_FLAGS 0
#define COMPUTIME_ROOTDELAY 0.0
#define COMPUTIME_BASEDELAY 0.0
#define COMPUTIME_ID DCF_ID
#define COMPUTIME_NAME "COMPUTIME"
#define COMPUTIME_DESCRIPTION "Diem's Computime receiver"
#define COMPUTIME_FORMAT "Diem's Computime Radio Clock"
#define COMPUTIME_TYPE DCF_TYPE
#define COMPUTIME_MAXUNSYNC (60*60) /* only trust clock for 1 hour */
#define COMPUTIME_SPEED (B9600)
#define COMPUTIME_CFLAG (CSTOPB|CS7|CREAD|CLOCAL)
#define COMPUTIME_IFLAG (IGNBRK|IGNPAR|ISTRIP)
#define COMPUTIME_OFLAG 0
#define COMPUTIME_LFLAG 0
#define COMPUTIME_SAMPLES 5
#define COMPUTIME_KEEP 3
/*
* Varitext Radio Clock Receiver
*/
#define VARITEXT_FLAGS 0
#define VARITEXT_ROOTDELAY 0.0
#define VARITEXT_BASEDELAY 0.0
#define VARITEXT_ID MSF_ID
#define VARITEXT_NAME "VARITEXT"
#define VARITEXT_DESCRIPTION "Varitext receiver"
#define VARITEXT_FORMAT "Varitext Radio Clock"
#define VARITEXT_TYPE DCF_TYPE
#define VARITEXT_MAXUNSYNC (60*60) /* only trust clock for 1 hour */
#define VARITEXT_SPEED (B9600)
#define VARITEXT_CFLAG (CS7|CREAD|CLOCAL|PARENB|PARODD)
#define VARITEXT_IFLAG (IGNPAR|IGNBRK|INPCK) /*|ISTRIP)*/
#define VARITEXT_OFLAG 0
#define VARITEXT_LFLAG 0
#define VARITEXT_SAMPLES 32
#define VARITEXT_KEEP 20
/*
* SEL240x Satellite Sychronized Clock
*/
#define SEL240X_POLLRATE 0 /* only true direct polling */
#define SEL240X_POLLCMD "BUB8"
#define SEL240X_CMDSIZE 4
static poll_info_t sel240x_pollinfo = { SEL240X_POLLRATE,
SEL240X_POLLCMD,
SEL240X_CMDSIZE };
#define SEL240X_FLAGS (PARSE_F_PPSONSECOND)
#define SEL240X_POLL poll_dpoll
#define SEL240X_INIT poll_init
#define SEL240X_END 0
#define SEL240X_DATA ((void *)(&sel240x_pollinfo))
#define SEL240X_ROOTDELAY 0.0
#define SEL240X_BASEDELAY 0.0
#define SEL240X_ID GPS_ID
#define SEL240X_NAME "SEL240X"
#define SEL240X_DESCRIPTION "SEL240x Satellite Synchronized Clock"
#define SEL240X_FORMAT "SEL B8"
#define SEL240X_MAXUNSYNC 60*60*12 /* only trust clock for 12 hours */
#define SEL240X_SPEED (B9600)
#define SEL240X_CFLAG (CS8|CREAD|CLOCAL)
#define SEL240X_IFLAG (IGNBRK|IGNPAR)
#define SEL240X_OFLAG (0)
#define SEL240X_LFLAG (0)
#define SEL240X_SAMPLES 5
#define SEL240X_KEEP 3
static struct parse_clockinfo
{
unsigned long cl_flags; /* operation flags (PPS interpretation, trust handling) */
void (*cl_poll) (struct parseunit *); /* active poll routine */
bool (*cl_init) (struct parseunit *); /* active poll init routine */
void (*cl_event) (struct parseunit *, int); /* special event handling (e.g. reset clock) */
void (*cl_end) (struct parseunit *); /* active poll end routine */
void (*cl_message) (struct parseunit *, parsetime_t *); /* process a lower layer message */
void *cl_data; /* local data area for "poll" mechanism */
double cl_rootdelay; /* rootdelay */
double cl_basedelay; /* current offset by which the RS232
time code is delayed from the actual time */
const char *cl_id; /* ID code */
const char *cl_name; /* device name (tag for logging) */
const char *cl_description; /* device description */
const char *cl_format; /* fixed format */
uint8_t cl_type; /* clock type (ntp control) */
unsigned long cl_maxunsync; /* time to trust oscillator after losing synch */
unsigned long cl_speed; /* terminal input & output baudrate */
unsigned long cl_cflag; /* terminal control flags */
unsigned long cl_iflag; /* terminal input flags */
unsigned long cl_oflag; /* terminal output flags */
unsigned long cl_lflag; /* terminal local flags */
unsigned long cl_samples; /* samples for median filter */
unsigned long cl_keep; /* samples for median filter to keep */
} parse_clockinfo[] =
{
{ /* subtype 0 */
MBG_FLAGS,
NO_POLL,
NO_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
DCFPZF535_ROOTDELAY,
DCFPZF535_BASEDELAY,
DCF_P_ID,
DCFPZF535_NAME,
DCFPZF535_DESCRIPTION,
DCFPZF535_FORMAT,
DCF_TYPE,
DCFPZF535_MAXUNSYNC,
DCFPZF535_SPEED,
DCFPZF535_CFLAG,
DCFPZF535_IFLAG,
DCFPZF535_OFLAG,
DCFPZF535_LFLAG,
DCFPZF535_SAMPLES,
DCFPZF535_KEEP
},
{ /* subtype 1 */
MBG_FLAGS,
NO_POLL,
NO_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
DCFPZF535OCXO_ROOTDELAY,
DCFPZF535OCXO_BASEDELAY,
DCF_P_ID,
DCFPZF535OCXO_NAME,
DCFPZF535OCXO_DESCRIPTION,
DCFPZF535OCXO_FORMAT,
DCF_TYPE,
DCFPZF535OCXO_MAXUNSYNC,
DCFPZF535OCXO_SPEED,
DCFPZF535OCXO_CFLAG,
DCFPZF535OCXO_IFLAG,
DCFPZF535OCXO_OFLAG,
DCFPZF535OCXO_LFLAG,
DCFPZF535OCXO_SAMPLES,
DCFPZF535OCXO_KEEP
},
{ /* subtype 2 */
MBG_FLAGS,
NO_POLL,
NO_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
DCFUA31_ROOTDELAY,
DCFUA31_BASEDELAY,
DCF_A_ID,
DCFUA31_NAME,
DCFUA31_DESCRIPTION,
DCFUA31_FORMAT,
DCF_TYPE,
DCFUA31_MAXUNSYNC,
DCFUA31_SPEED,
DCFUA31_CFLAG,
DCFUA31_IFLAG,
DCFUA31_OFLAG,
DCFUA31_LFLAG,
DCFUA31_SAMPLES,
DCFUA31_KEEP
},
{ /* subtype 3 */
MBG_FLAGS,
NO_POLL,
NO_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
DCF7000_ROOTDELAY,
DCF7000_BASEDELAY,
DCF_A_ID,
DCF7000_NAME,
DCF7000_DESCRIPTION,
DCF7000_FORMAT,
DCF_TYPE,
DCF7000_MAXUNSYNC,
DCF7000_SPEED,
DCF7000_CFLAG,
DCF7000_IFLAG,
DCF7000_OFLAG,
DCF7000_LFLAG,
DCF7000_SAMPLES,
DCF7000_KEEP
},
{ /* subtype 4 */
NO_CL_FLAGS,
WSDCF_POLL,
WSDCF_INIT,
NO_EVENT,
WSDCF_END,
NO_MESSAGE,
WSDCF_DATA,
WSDCF_ROOTDELAY,
WSDCF_BASEDELAY,
DCF_A_ID,
WSDCF_NAME,
WSDCF_DESCRIPTION,
WSDCF_FORMAT,
DCF_TYPE,
WSDCF_MAXUNSYNC,
WSDCF_SPEED,
WSDCF_CFLAG,
WSDCF_IFLAG,
WSDCF_OFLAG,
WSDCF_LFLAG,
WSDCF_SAMPLES,
WSDCF_KEEP
},
{ /* subtype 5 */
RAWDCF_FLAGS,
NO_POLL,
RAWDCF_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
RAWDCF_ROOTDELAY,
CONRAD_BASEDELAY,
DCF_A_ID,
CONRAD_NAME,
CONRAD_DESCRIPTION,
RAWDCF_FORMAT,
DCF_TYPE,
RAWDCF_MAXUNSYNC,
RAWDCF_SPEED,
RAWDCF_CFLAG,
RAWDCF_IFLAG,
RAWDCF_OFLAG,
RAWDCF_LFLAG,
RAWDCF_SAMPLES,
RAWDCF_KEEP
},
{ /* subtype 6 */
RAWDCF_FLAGS,
NO_POLL,
RAWDCF_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
RAWDCF_ROOTDELAY,
TIMEBRICK_BASEDELAY,
DCF_A_ID,
TIMEBRICK_NAME,
TIMEBRICK_DESCRIPTION,
RAWDCF_FORMAT,
DCF_TYPE,
RAWDCF_MAXUNSYNC,
RAWDCF_SPEED,
RAWDCF_CFLAG,
RAWDCF_IFLAG,
RAWDCF_OFLAG,
RAWDCF_LFLAG,
RAWDCF_SAMPLES,
RAWDCF_KEEP
},
{ /* subtype 7 */
MBG_FLAGS,
GPS16X_POLL,
GPS16X_INIT,
NO_EVENT,
GPS16X_END,
GPS16X_MESSAGE,
GPS16X_DATA,
GPS16X_ROOTDELAY,
GPS16X_BASEDELAY,
GPS16X_ID,
GPS16X_NAME,
GPS16X_DESCRIPTION,
GPS16X_FORMAT,
GPS_TYPE,
GPS16X_MAXUNSYNC,
GPS16X_SPEED,
GPS16X_CFLAG,
GPS16X_IFLAG,
GPS16X_OFLAG,
GPS16X_LFLAG,
GPS16X_SAMPLES,
GPS16X_KEEP
},
{ /* subtype 8 */
RAWDCF_FLAGS,
NO_POLL,
NO_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
RAWDCF_ROOTDELAY,
IGELCLOCK_BASEDELAY,
DCF_A_ID,
IGELCLOCK_NAME,
IGELCLOCK_DESCRIPTION,
RAWDCF_FORMAT,
DCF_TYPE,
RAWDCF_MAXUNSYNC,
IGELCLOCK_SPEED,
IGELCLOCK_CFLAG,
RAWDCF_IFLAG,
RAWDCF_OFLAG,
RAWDCF_LFLAG,
RAWDCF_SAMPLES,
RAWDCF_KEEP
},
{ /* subtype 9 */
TRIMBLETAIP_FLAGS,
#if TRIM_POLLRATE /* DHD940515: Allow user config */
NO_POLL,
#else
TRIMBLETAIP_POLL,
#endif
TRIMBLETAIP_INIT,
TRIMBLETAIP_EVENT,
TRIMBLETAIP_END,
NO_MESSAGE,
TRIMBLETAIP_DATA,
TRIMBLETAIP_ROOTDELAY,
TRIMBLETAIP_BASEDELAY,
TRIMBLETAIP_ID,
TRIMBLETAIP_NAME,
TRIMBLETAIP_DESCRIPTION,
TRIMBLETAIP_FORMAT,
GPS_TYPE,
TRIMBLETAIP_MAXUNSYNC,
TRIMBLETAIP_SPEED,
TRIMBLETAIP_CFLAG,
TRIMBLETAIP_IFLAG,
TRIMBLETAIP_OFLAG,
TRIMBLETAIP_LFLAG,
TRIMBLETAIP_SAMPLES,
TRIMBLETAIP_KEEP
},
{ /* subtype 10 */
TRIMBLETSIP_FLAGS,
#if TRIM_POLLRATE /* DHD940515: Allow user config */
NO_POLL,
#else
TRIMBLETSIP_POLL,
#endif
TRIMBLETSIP_INIT,
TRIMBLETSIP_EVENT,
TRIMBLETSIP_END,
TRIMBLETSIP_MESSAGE,
TRIMBLETSIP_DATA,
TRIMBLETSIP_ROOTDELAY,
TRIMBLETSIP_BASEDELAY,
TRIMBLETSIP_ID,
TRIMBLETSIP_NAME,
TRIMBLETSIP_DESCRIPTION,
TRIMBLETSIP_FORMAT,
GPS_TYPE,
TRIMBLETSIP_MAXUNSYNC,
TRIMBLETSIP_SPEED,
TRIMBLETSIP_CFLAG,
TRIMBLETSIP_IFLAG,
TRIMBLETSIP_OFLAG,
TRIMBLETSIP_LFLAG,
TRIMBLETSIP_SAMPLES,
TRIMBLETSIP_KEEP
},
{ /* subtype 11 */
NO_CL_FLAGS,
RCC8000_POLL,
RCC8000_INIT,
NO_EVENT,
RCC8000_END,
NO_MESSAGE,
RCC8000_DATA,
RCC8000_ROOTDELAY,
RCC8000_BASEDELAY,
RCC8000_ID,
RCC8000_NAME,
RCC8000_DESCRIPTION,
RCC8000_FORMAT,
DCF_TYPE,
RCC8000_MAXUNSYNC,
RCC8000_SPEED,
RCC8000_CFLAG,
RCC8000_IFLAG,
RCC8000_OFLAG,
RCC8000_LFLAG,
RCC8000_SAMPLES,
RCC8000_KEEP
},
{ /* subtype 12 */
HOPF6021_FLAGS,
NO_POLL,
NO_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
HOPF6021_ROOTDELAY,
HOPF6021_BASEDELAY,
DCF_ID,
HOPF6021_NAME,
HOPF6021_DESCRIPTION,
HOPF6021_FORMAT,
DCF_TYPE,
HOPF6021_MAXUNSYNC,
HOPF6021_SPEED,
HOPF6021_CFLAG,
HOPF6021_IFLAG,
HOPF6021_OFLAG,
HOPF6021_LFLAG,
HOPF6021_SAMPLES,
HOPF6021_KEEP
},
{ /* subtype 13 */
COMPUTIME_FLAGS,
NO_POLL,
NO_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
COMPUTIME_ROOTDELAY,
COMPUTIME_BASEDELAY,
COMPUTIME_ID,
COMPUTIME_NAME,
COMPUTIME_DESCRIPTION,
COMPUTIME_FORMAT,
COMPUTIME_TYPE,
COMPUTIME_MAXUNSYNC,
COMPUTIME_SPEED,
COMPUTIME_CFLAG,
COMPUTIME_IFLAG,
COMPUTIME_OFLAG,
COMPUTIME_LFLAG,
COMPUTIME_SAMPLES,
COMPUTIME_KEEP
},
{ /* subtype 14 */
RAWDCF_FLAGS,
NO_POLL,
RAWDCFDTRSET_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
RAWDCF_ROOTDELAY,
RAWDCF_BASEDELAY,
DCF_A_ID,
RAWDCFDTRSET_NAME,
RAWDCFDTRSET_DESCRIPTION,
RAWDCF_FORMAT,
DCF_TYPE,
RAWDCF_MAXUNSYNC,
RAWDCF_SPEED,
RAWDCF_CFLAG,
RAWDCF_IFLAG,
RAWDCF_OFLAG,
RAWDCF_LFLAG,
RAWDCF_SAMPLES,
RAWDCF_KEEP
},
{ /* subtype 15 */
0, /* operation flags (io modes) */
NO_POLL, /* active poll routine */
NO_INIT, /* active poll init routine */
NO_EVENT, /* special event handling (e.g. reset clock) */
NO_END, /* active poll end routine */
NO_MESSAGE, /* process a lower layer message */
NO_LCLDATA, /* local data area for "poll" mechanism */
0, /* rootdelay */
11.0 /* bits */ / 9600, /* current offset by which the RS232
time code is delayed from the actual time */
DCF_ID, /* ID code */
"WHARTON400A",
"WHARTON 400A Series clock", /* device description */
"WHARTON 400A Series clock Output Format 1", /* fixed format */
/* Must match a format-name in a libparse/clk_xxx.c file */
DCF_TYPE, /* clock type (ntp control) */
(1*60*60), /* time to trust oscillator after losing synch */
B9600, /* terminal input & output baudrate */
(CS8|CREAD|PARENB|CLOCAL|HUPCL),/* terminal control flags */
0, /* terminal input flags */
0, /* terminal output flags */
0, /* terminal local flags */
5, /* samples for median filter */
3, /* samples for median filter to keep */
},
{ /* subtype 16 - RAWDCF RTS set, DTR clr */
RAWDCF_FLAGS,
NO_POLL,
RAWDCFDTRCLRRTSSET_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
RAWDCF_ROOTDELAY,
RAWDCF_BASEDELAY,
DCF_A_ID,
RAWDCFDTRCLRRTSSET_NAME,
RAWDCFDTRCLRRTSSET_DESCRIPTION,
RAWDCF_FORMAT,
DCF_TYPE,
RAWDCF_MAXUNSYNC,
RAWDCF_SPEED,
RAWDCF_CFLAG,
RAWDCF_IFLAG,
RAWDCF_OFLAG,
RAWDCF_LFLAG,
RAWDCF_SAMPLES,
RAWDCF_KEEP
},
{ /* subtype 17 */
VARITEXT_FLAGS,
NO_POLL,
NO_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
VARITEXT_ROOTDELAY,
VARITEXT_BASEDELAY,
VARITEXT_ID,
VARITEXT_NAME,
VARITEXT_DESCRIPTION,
VARITEXT_FORMAT,
VARITEXT_TYPE,
VARITEXT_MAXUNSYNC,
VARITEXT_SPEED,
VARITEXT_CFLAG,
VARITEXT_IFLAG,
VARITEXT_OFLAG,
VARITEXT_LFLAG,
VARITEXT_SAMPLES,
VARITEXT_KEEP
},
{ /* subtype 18 */
MBG_FLAGS,
NO_POLL,
NO_INIT,
NO_EVENT,
GPS16X_END,
GPS16X_MESSAGE,
GPS16X_DATA,
GPS16X_ROOTDELAY,
GPS16X_BASEDELAY,
GPS16X_ID,
GPS16X_NAME,
GPS16X_DESCRIPTION,
GPS16X_FORMAT,
GPS_TYPE,
GPS16X_MAXUNSYNC,
GPS16X_SPEED,
GPS16X_CFLAG,
GPS16X_IFLAG,
GPS16X_OFLAG,
GPS16X_LFLAG,
GPS16X_SAMPLES,
GPS16X_KEEP
},
{ /* subtype 19 */
RAWDCF_FLAGS,
NO_POLL,
RAWDCF_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
RAWDCF_ROOTDELAY,
GUDE_EMC_USB_V20_BASEDELAY,
DCF_A_ID,
GUDE_EMC_USB_V20_NAME,
GUDE_EMC_USB_V20_DESCRIPTION,
RAWDCF_FORMAT,
DCF_TYPE,
RAWDCF_MAXUNSYNC,
GUDE_EMC_USB_V20_SPEED,
RAWDCF_CFLAG,
RAWDCF_IFLAG,
RAWDCF_OFLAG,
RAWDCF_LFLAG,
RAWDCF_SAMPLES,
RAWDCF_KEEP
},
{ /* subtype 20, like subtype 14 but driven by 75 baud */
RAWDCF_FLAGS,
NO_POLL,
RAWDCFDTRSET_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
RAWDCF_ROOTDELAY,
RAWDCF_BASEDELAY,
DCF_A_ID,
RAWDCFDTRSET_NAME,
RAWDCFDTRSET75_DESCRIPTION,
RAWDCF_FORMAT,
DCF_TYPE,
RAWDCF_MAXUNSYNC,
B75,
RAWDCF_CFLAG,
RAWDCF_IFLAG,
RAWDCF_OFLAG,
RAWDCF_LFLAG,
RAWDCF_SAMPLES,
RAWDCF_KEEP
},
{ /* subtype 21, like subtype 16 but driven by 75 baud
- RAWDCF RTS set, DTR clr */
RAWDCF_FLAGS,
NO_POLL,
RAWDCFDTRCLRRTSSET_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
RAWDCF_ROOTDELAY,
RAWDCF_BASEDELAY,
DCF_A_ID,
RAWDCFDTRCLRRTSSET_NAME,
RAWDCFDTRCLRRTSSET75_DESCRIPTION,
RAWDCF_FORMAT,
DCF_TYPE,
RAWDCF_MAXUNSYNC,
B75,
RAWDCF_CFLAG,
RAWDCF_IFLAG,
RAWDCF_OFLAG,
RAWDCF_LFLAG,
RAWDCF_SAMPLES,
RAWDCF_KEEP
},
{ /* subtype 22 - like 2 with POWERUP trust */
MBG_FLAGS | PARSE_F_POWERUPTRUST,
NO_POLL,
NO_INIT,
NO_EVENT,
NO_END,
NO_MESSAGE,
NO_LCLDATA,
DCFUA31_ROOTDELAY,
DCFUA31_BASEDELAY,
DCF_A_ID,
DCFUA31_NAME,
DCFUA31_DESCRIPTION,
DCFUA31_FORMAT,
DCF_TYPE,
DCFUA31_MAXUNSYNC,
DCFUA31_SPEED,
DCFUA31_CFLAG,
DCFUA31_IFLAG,
DCFUA31_OFLAG,
DCFUA31_LFLAG,
DCFUA31_SAMPLES,
DCFUA31_KEEP
},
{ /* subtype 23 - like 7 with POWERUP trust */
MBG_FLAGS | PARSE_F_POWERUPTRUST,
GPS16X_POLL,
GPS16X_INIT,
NO_EVENT,
GPS16X_END,
GPS16X_MESSAGE,
GPS16X_DATA,
GPS16X_ROOTDELAY,
GPS16X_BASEDELAY,
GPS16X_ID,
GPS16X_NAME,
GPS16X_DESCRIPTION,
GPS16X_FORMAT,
GPS_TYPE,
GPS16X_MAXUNSYNC,
GPS16X_SPEED,
GPS16X_CFLAG,
GPS16X_IFLAG,
GPS16X_OFLAG,
GPS16X_LFLAG,
GPS16X_SAMPLES,
GPS16X_KEEP
},
{ /* subtype 24 */
SEL240X_FLAGS,
SEL240X_POLL,
SEL240X_INIT,
NO_EVENT,
SEL240X_END,
NO_MESSAGE,
SEL240X_DATA,
SEL240X_ROOTDELAY,
SEL240X_BASEDELAY,
SEL240X_ID,
SEL240X_NAME,
SEL240X_DESCRIPTION,
SEL240X_FORMAT,
GPS_TYPE,
SEL240X_MAXUNSYNC,
SEL240X_SPEED,
SEL240X_CFLAG,
SEL240X_IFLAG,
SEL240X_OFLAG,
SEL240X_LFLAG,
SEL240X_SAMPLES,
SEL240X_KEEP
},
};
static int ncltypes = sizeof(parse_clockinfo) / sizeof(struct parse_clockinfo);
#define CLK_REALTYPE(x) ((int)(((x)->cfg.mode) & 0x7F))
/* careful, CLK_TYPE() in refclock_trimble.c is different */
#define CLK_TYPE(x) ((CLK_REALTYPE(x) >= ncltypes) ? ~0 : CLK_REALTYPE(x))
#define CLK_PPS(x) (((x)->cfg.mode) & 0x80)
/*
* Other constant stuff
*/
#define PARSEHSREFID 0x7f7f08ff /* 127.127.8.255 refid for hi strata */
#define PARSESTATISTICS (60*60) /* output state statistics every hour */
static int notice = 0;
#define PARSE_STATETIME(parse, i) ((parse->generic->currentstatus == i) ? parse->statetime[i] + current_time - parse->lastchange : parse->statetime[i])
static void parse_event (struct parseunit *, int);
static void parse_process (struct parseunit *, parsetime_t *);
static void clear_err (struct parseunit *, unsigned long);
static int list_err (struct parseunit *, unsigned long);
static char * l_mktime (unsigned long);
/**===========================================================================
** implementation error message regression module
**/
static void
clear_err(
struct parseunit *parse,
unsigned long lstate
)
{
if (lstate == ERR_ALL)
{
size_t i;
for (i = 0; i < ERR_CNT; i++)
{
parse->errors[i].err_stage = err_tbl[i];
parse->errors[i].err_cnt = 0;
parse->errors[i].err_last = 0;
parse->errors[i].err_started = 0;
parse->errors[i].err_suppressed = 0;
}
}
else
{
parse->errors[lstate].err_stage = err_tbl[lstate];
parse->errors[lstate].err_cnt = 0;
parse->errors[lstate].err_last = 0;
parse->errors[lstate].err_started = 0;
parse->errors[lstate].err_suppressed = 0;
}
}
static int
list_err(
struct parseunit *parse,
unsigned long lstate
)
{
int do_it;
struct errorinfo *err = &parse->errors[lstate];
if (err->err_started == 0)
{
err->err_started = current_time;
}
do_it = (current_time - err->err_last) >= err->err_stage->err_delay;
if (do_it)
err->err_cnt++;
if (err->err_stage->err_count &&
(err->err_cnt >= err->err_stage->err_count))
{
err->err_stage++;
err->err_cnt = 0;
}
if (!err->err_cnt && do_it)
msyslog(LOG_INFO, "REFCLOCK: PARSE receiver #%d: interval for following error message class is at least %s",
parse->peer->procptr->refclkunit, l_mktime(err->err_stage->err_delay));
if (!do_it)
err->err_suppressed++;
else
err->err_last = current_time;
if (do_it && err->err_suppressed)
{
msyslog(LOG_INFO,
"REFCLOCK: PARSE receiver #%d: %lu message%s suppressed, error "
"condition class persists for %s",
parse->peer->procptr->refclkunit, err->err_suppressed,
(err->err_suppressed == 1) ? " was" : "s where",
l_mktime(current_time - err->err_started));
err->err_suppressed = 0;
}
return do_it;
}
/*--------------------------------------------------
* mkreadable - make a printable ascii string (without
* embedded quotes so that the ntpq protocol isn't
* fooled
*/
static char *
mkreadable(
char *buffer,
long blen,
const char *src,
unsigned long srclen,
int hex
)
{
static const char ellipsis[] = "...";
char *b = buffer;
char *endb = NULL;
if (blen < 4)
return NULL; /* don't bother with mini buffers */
endb = buffer + blen - sizeof(ellipsis);
blen--; /* account for '\0' */
while (blen && srclen--)
{
if (!hex && /* no binary only */
(*src != '\\') && /* no plain \ */
(*src != '"') && /* no " */
isprint((unsigned char)*src)) /* only printables */
{ /* they are easy... */
*buffer++ = *src++;
blen--;
}
else
{
if (blen < 4)
{
while (blen--)
{
*buffer++ = '.';
}
*buffer = '\0';
return b;
}
else
{
if (*src == '\\')
{
memcpy(buffer, "\\\\", 2);
buffer += 2;
blen -= 2;
src++;
}
else
{
snprintf(buffer, (size_t)blen,
"\\x%02x", (unsigned)(*src++));
blen -= 4;
buffer += 4;
}
}
}
if (srclen && !blen && endb) /* overflow - set last chars to ... */
memcpy(endb, ellipsis, sizeof(ellipsis));
}
*buffer = '\0';
return b;
}
/*--------------------------------------------------
* mkascii - make a printable ascii string
* assumes (unless defined better) 7-bit ASCII
*/
static char *
mkascii(
char *buffer,
long blen,
const char *src,
unsigned long srclen
)
{
return mkreadable(buffer, blen, src, srclen, 0);
}
/**===========================================================================
** implementation of i/o handling methods
** (all STREAM, partial STREAM, user level)
**/
static int local_init (struct parseunit *);
static void local_end (struct parseunit *);
static int local_nop (struct parseunit *);
static bool local_setcs (struct parseunit *, parsectl_t *);
static int local_getfmt (struct parseunit *, parsectl_t *);
static int local_setfmt (struct parseunit *, parsectl_t *);
static int local_timecode (struct parseunit *, parsectl_t *);
static void local_receive (struct recvbuf *);
static int local_input (struct recvbuf *);
static bind_t io_bindings[] =
{
{
"normal",
local_init,
local_end,
local_setcs,
local_nop,
local_nop,
local_getfmt,
local_setfmt,
local_timecode,
local_receive,
local_input,
},
{
(char *)0,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
}
};
/*--------------------------------------------------
* local init
*/
static int
local_init(
struct parseunit *parse
)
{
return parse_ioinit(&parse->parseio);
}
/*--------------------------------------------------
* local end
*/
static void
local_end(
struct parseunit *parse
)
{
parse_ioend(&parse->parseio);
}
/*--------------------------------------------------
* local nop
*/
static int
local_nop(
struct parseunit *parse
)
{
UNUSED_ARG(parse);
return true;
}
/*--------------------------------------------------
* local setcs
*/
static bool
local_setcs(
struct parseunit *parse,
parsectl_t *tcl
)
{
return parse_setcs(tcl, &parse->parseio);
}
/*--------------------------------------------------
* local getfmt
*/
static int
local_getfmt(
struct parseunit *parse,
parsectl_t *tcl
)
{
return parse_getfmt(tcl, &parse->parseio);
}
/*--------------------------------------------------
* local setfmt
*/
static int
local_setfmt(
struct parseunit *parse,
parsectl_t *tcl
)
{
return parse_setfmt(tcl, &parse->parseio);
}
/*--------------------------------------------------
* local timecode
*/
static int
local_timecode(
struct parseunit *parse,
parsectl_t *tcl
)
{
return parse_timecode(tcl, &parse->parseio);
}
/*--------------------------------------------------
* local input
*/
static int
local_input(
struct recvbuf *rbufp
)
{
struct parseunit * parse;
int count;
unsigned char *s;
timestamp_t ts;
parse = (struct parseunit *)rbufp->recv_peer->procptr->unitptr;
if (!parse->peer)
return false;
/*
* eat all characters, parsing then and feeding complete samples
*/
count = (int)rbufp->recv_length;
s = (unsigned char *)rbufp->recv_buffer;
ts = rbufp->recv_time;
while (count--)
{
if (parse_ioread(&parse->parseio, (char)(*s++), &ts))
{
struct recvbuf *buf;
/*
* got something good to eat
*/
if (!PARSE_PPS(parse->parseio.parse_dtime.parse_state))
{
#ifdef HAVE_PPSAPI
if (parse->flags & PARSE_PPSAPI)
{
struct timespec pps_timeout;
pps_info_t pps_info;
pps_timeout.tv_sec = 0;
pps_timeout.tv_nsec = 0;
if (time_pps_fetch(parse->ppsctl.handle, PPS_TSFMT_TSPEC, &pps_info,
&pps_timeout) == 0)
{
if (pps_info.assert_sequence + pps_info.clear_sequence != parse->ppsserial)
{
double dtemp;
struct timespec pts;
/*
* add PPS time stamp if available via ppsclock module
* and not supplied already.
*/
if (parse->flags & PARSE_CLEAR)
pts = pps_info.clear_timestamp;
else
pts = pps_info.assert_timestamp;
setlfpuint(parse->parseio.parse_dtime.parse_ptime,
(uint64_t)pts.tv_sec + JAN_1970);
dtemp = (double) pts.tv_nsec * S_PER_NS;
if (dtemp < 0.) {
dtemp += 1;
bumplfpuint(parse->parseio.parse_dtime.parse_ptime, -1);
}
if (dtemp > 1.) {
dtemp -= 1;
bumplfpuint(parse->parseio.parse_dtime.parse_ptime, 1);
}
setlfpfrac(parse->parseio.parse_dtime.parse_ptime, (uint32_t)(dtemp * FRAC));
parse->parseio.parse_dtime.parse_state |= PARSEB_PPS|PARSEB_S_PPS;
DPRINT(4, ("parse: local_receive: fd %d PPSAPI seq %ld - PPS %s\n",
rbufp->fd,
(long)pps_info.assert_sequence + (long)pps_info.clear_sequence,
lfptoa(parse->parseio.parse_dtime.parse_ptime, 6)));
}
#ifdef DEBUG
else
{
if (debug > 3) /* SPECIAL DEBUG */
{
printf(
"parse: local_receive: fd %d PPSAPI seq assert %ld, seq clear %ld - NO PPS event\n",
rbufp->fd,
(long)pps_info.assert_sequence, (long)pps_info.clear_sequence);
}
}
#endif
parse->ppsserial = pps_info.assert_sequence + pps_info.clear_sequence;
}
#ifdef DEBUG
else
{
if (debug > 3) /* SPECIAL DEBUG */
{
printf(
"parse: local_receive: fd %d PPSAPI time_pps_fetch errno = %d\n",
rbufp->fd,
errno);
}
}
#endif
}
#endif /* !HAVE_PPSAPI */
}
if (count)
{ /* simulate receive */
// FIXME - this copy is no longer needed
// This code is the result of a simple fix for SINGLEBUFFER
// The copy used to go to add_full_recv_buffer, but that's not needed any more
// I'm not sure the local_receive below is correct
// Hal, 2018-Sep-21
buf = get_free_recv_buffer();
if (buf != NULL) {
memmove((void *)buf->recv_buffer,
(void *)&parse->parseio.parse_dtime,
sizeof(parsetime_t));
buf->recv_length = sizeof(parsetime_t);
buf->recv_time = rbufp->recv_time;
buf->srcadr = rbufp->srcadr;
buf->dstadr = rbufp->dstadr;
buf->fd = rbufp->fd;
buf->recv_peer = rbufp->recv_peer;
parse->generic->io.recvcount++;
pkt_count.packets_received++;
local_receive(buf);
freerecvbuf(buf);
}
parse_iodone(&parse->parseio);
}
else
{
memmove((void *)rbufp->recv_buffer,
(void *)&parse->parseio.parse_dtime,
sizeof(parsetime_t));
parse_iodone(&parse->parseio);
rbufp->recv_length = sizeof(parsetime_t);
return true; /* got something & in place return */
}
}
}
return false; /* nothing to pass up */
}
/*--------------------------------------------------
* local receive
*/
static void
local_receive(
struct recvbuf *rbufp
)
{
struct parseunit * parse;
parsetime_t parsetime;
parse = (struct parseunit *)rbufp->recv_peer->procptr->unitptr;
if (!parse->peer)
return;
if (rbufp->recv_length != sizeof(parsetime_t))
{
ERR(ERR_BADIO)
msyslog(LOG_ERR,
"REFCLOCK: PARSE receiver #%d: local_receive: bad size "
" (got %zu expected %zu)",
parse->peer->procptr->refclkunit, rbufp->recv_length,
sizeof(parsetime_t));
parse_event(parse, CEVNT_BADREPLY);
return;
}
clear_err(parse, ERR_BADIO);
memmove((void *)&parsetime,
(void *)rbufp->recv_buffer,
sizeof(parsetime_t));
DPRINT(4, ("PARSE receiver #%d: status %06x, state %08x, time(fp) %lx.%08lx, stime(fp) %lx.%08lx, ptime(fp) %lx.%08lx\n",
parse->peer->procptr->refclkunit,
(unsigned int)parsetime.parse_status,
(unsigned int)parsetime.parse_state,
(unsigned long)lfpuint(parsetime.parse_time),
(unsigned long)lfpfrac(parsetime.parse_time),
(unsigned long)lfpuint(parsetime.parse_stime),
(unsigned long)lfpfrac(parsetime.parse_stime),
(unsigned long)lfpuint(parsetime.parse_ptime),
(unsigned long)lfpfrac(parsetime.parse_ptime)));
parse_process(parse, &parsetime);
}
/*--------------------------------------------------
* init_iobinding - find and initialize lower layers
*/
static bind_t *
init_iobinding(
struct parseunit *parse
)
{
bind_t *b = io_bindings;
while (b->bd_description != (char *)0)
{
if ((*b->bd_init)(parse))
{
return b;
}
b++;
}
return (bind_t *)0;
}
/**===========================================================================
** support routines
**/
static NTP_PRINTF(4, 5) char *
ap(char *buffer, size_t len, char *pos, const char *fmt, ...)
{
va_list va;
int l;
size_t rem = len - (size_t)(pos - buffer);
if (rem == 0)
return pos;
va_start(va, fmt);
l = vsnprintf(pos, rem, fmt, va);
va_end(va);
if (l != -1) {
rem--;
if (rem >= (size_t)l)
pos += l;
else
pos += rem;
}
return pos;
}
/*--------------------------------------------------
* convert a flag field to a string
*/
static char *
parsestate(
unsigned long lstate,
char *buffer,
int size
)
{
static struct bits
{
unsigned long bit;
const char *name;
} flagstrings[] =
{
{ PARSEB_ANNOUNCE, "DST SWITCH WARNING" },
{ PARSEB_POWERUP, "NOT SYNCHRONIZED" },
{ PARSEB_NOSYNC, "TIME CODE NOT CONFIRMED" },
{ PARSEB_DST, "DST" },
{ PARSEB_UTC, "UTC DISPLAY" },
{ PARSEB_LEAPADD, "LEAP ADD WARNING" },
{ PARSEB_LEAPDEL, "LEAP DELETE WARNING" },
{ PARSEB_LEAPSECOND, "LEAP SECOND" },
{ PARSEB_CALLBIT, "CALL BIT" },
{ PARSEB_TIMECODE, "TIME CODE" },
{ PARSEB_PPS, "PPS" },
{ PARSEB_POSITION, "POSITION" },
{ 0, NULL }
};
static struct sbits
{
unsigned long bit;
const char *name;
} sflagstrings[] =
{
{ PARSEB_S_LEAP, "LEAP INDICATION" },
{ PARSEB_S_PPS, "PPS SIGNAL" },
{ PARSEB_S_CALLBIT, "CALLBIT" },
{ PARSEB_S_POSITION, "POSITION" },
{ 0, NULL }
};
int i;
char *s, *t;
*buffer = '\0';
s = t = buffer;
i = 0;
while (flagstrings[i].bit)
{
if (flagstrings[i].bit & lstate)
{
if (s != t)
t = ap(buffer, (size_t)size, t, "; ");
t = ap(buffer, (size_t)size, t, "%s",
flagstrings[i].name);
}
i++;
}
if (lstate & (PARSEB_S_LEAP|PARSEB_S_CALLBIT|PARSEB_S_PPS|PARSEB_S_POSITION))
{
if (s != t)
t = ap(buffer, (size_t)size, t, "; ");
t = ap(buffer, (size_t)size, t, "(");
s = t;
i = 0;
while (sflagstrings[i].bit)
{
if (sflagstrings[i].bit & lstate)
{
if (t != s)
{
t = ap(buffer, (size_t)size, t, "; ");
}
t = ap(buffer, (size_t)size, t, "%s",
sflagstrings[i].name);
}
i++;
}
ap(buffer, (size_t)size, t, ")");
}
return buffer;
}
/*--------------------------------------------------
* convert a status flag field to a string
*/
static char *
parsestatus(
unsigned long lstate,
char *buffer,
int size
)
{
static struct bits
{
unsigned long bit;
const char *name;
} flagstrings[] =
{
{ CVT_OK, "CONVERSION SUCCESSFUL" },
{ CVT_NONE, "NO CONVERSION" },
{ CVT_FAIL, "CONVERSION FAILED" },
{ CVT_BADFMT, "ILLEGAL FORMAT" },
{ CVT_BADDATE, "DATE ILLEGAL" },
{ CVT_BADTIME, "TIME ILLEGAL" },
{ CVT_ADDITIONAL, "ADDITIONAL DATA" },
{ 0, NULL }
};
int i;
char *t;
t = buffer;
*buffer = '\0';
i = 0;
while (flagstrings[i].bit)
{
if (flagstrings[i].bit & lstate)
{
if (t != buffer)
t = ap(buffer, (size_t)size, t, "; ");
t = ap(buffer, (size_t)size, t, "%s",
flagstrings[i].name);
}
i++;
}
return buffer;
}
/*--------------------------------------------------
* convert a clock status flag field to a string
*/
static const char *
clockstatus(
unsigned long lstate
)
{
static char buffer[20];
static struct status
{
unsigned long value;
const char *name;
} flagstrings[] =
{
{ CEVNT_NOMINAL, "NOMINAL" },
{ CEVNT_TIMEOUT, "NO RESPONSE" },
{ CEVNT_BADREPLY,"BAD FORMAT" },
{ CEVNT_FAULT, "FAULT" },
{ CEVNT_PROP, "PROPAGATION DELAY" },
{ CEVNT_BADDATE, "ILLEGAL DATE" },
{ CEVNT_BADTIME, "ILLEGAL TIME" },
{ (unsigned)~0L, NULL }
};
int i;
i = 0;
while (flagstrings[i].value != (unsigned int)~0)
{
if (flagstrings[i].value == lstate)
{
return flagstrings[i].name;
}
i++;
}
snprintf(buffer, sizeof(buffer), "unknown #%lu", lstate);
return buffer;
}
/*--------------------------------------------------
* l_mktime - make representation of a relative time
*/
static char *
l_mktime(
unsigned long delta
)
{
unsigned long tmp, m, s;
static char buffer[40];
char *t;
buffer[0] = '\0';
t = buffer;
if ((tmp = delta / (60*60*24)) != 0)
{
t = ap(buffer, sizeof(buffer), t, "%lud+", tmp);
delta -= tmp * 60*60*24;
}
s = delta % 60;
delta /= 60;
m = delta % 60;
delta /= 60;
ap(buffer, sizeof(buffer), t, "%02d:%02d:%02d",
(int)delta, (int)m, (int)s);
return buffer;
}
/*--------------------------------------------------
* parse_statistics - list summary of clock states
*/
static void
parse_statistics(
struct parseunit *parse
)
{
int i;
NLOG(NLOG_CLOCKSTATIST) /* conditional if clause for conditional syslog */
{
msyslog(LOG_INFO,
"REFCLOCK: PARSE receiver #%d: running time: %s",
parse->peer->procptr->refclkunit,
l_mktime(current_time - parse->generic->timestarted));
msyslog(LOG_INFO,
"REFCLOCK: PARSE receiver #%d: current status: %s",
parse->peer->procptr->refclkunit,
clockstatus(parse->generic->currentstatus));
for (i = 0; i <= CEVNT_MAX; i++)
{
unsigned long s_time;
unsigned long percent, d = current_time - parse->generic->timestarted;
percent = s_time = PARSE_STATETIME(parse, i);
while (((unsigned long)(~0) / 10000) < percent)
{
percent /= 10;
d /= 10;
}
if (d)
percent = (percent * 10000) / d;
else
percent = 10000;
if (s_time)
msyslog(LOG_INFO,
"REFCLOCK: PARSE receiver #%d: state %18s: "
"%13s (%3lu.%02lu%%)",
parse->peer->procptr->refclkunit,
clockstatus((unsigned int)i),
l_mktime(s_time),
percent / 100, percent % 100);
}
}
}
/*--------------------------------------------------
* cparse_statistics - wrapper for statistics call
*/
static void
cparse_statistics(
struct parseunit *parse
)
{
if (parse->laststatistic + PARSESTATISTICS < current_time)
parse_statistics(parse);
parse->laststatistic = current_time;
}
/**===========================================================================
** ntp interface routines
**/
/*--------------------------------------------------
* parse_shutdown - shut down a PARSE clock
*/
static void
parse_shutdown(
struct refclockproc *pp
)
{
struct parseunit *parse = NULL;
if (pp)
parse = pp->unitptr;
if (!parse)
{
/* nothing to clean up */
return;
}
if (!parse->peer)
{
msyslog(LOG_INFO, "REFCLOCK: PARSE receiver #%d: INTERNAL ERROR - unit already inactive - `shutdown ignored", pp->refclkunit);
return;
}
#ifdef HAVE_PPSAPI
if (parse->flags & PARSE_PPSAPI)
{
(void)time_pps_destroy(parse->ppsctl.handle);
}
#endif
if (parse->generic->io.fd != parse->ppsfd && parse->ppsfd != -1)
(void)close(parse->ppsfd); /* close separate PPS source */
/*
* print statistics a last time and
* stop statistics machine
*/
parse_statistics(parse);
if (parse->parse_type->cl_end)
{
parse->parse_type->cl_end(parse);
}
/*
* cleanup before leaving this world
*/
if (parse->binding)
PARSE_END(parse);
/*
* Tell the I/O module to turn us off. We're history.
*/
io_closeclock(&parse->generic->io);
free_varlist(parse->kv);
NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
msyslog(LOG_INFO, "REFCLOCK: PARSE receiver #%d: reference clock \"%s\" removed",
parse->peer->procptr->refclkunit, parse->parse_type->cl_description);
parse->peer = (struct peer *)0; /* unused now */
pp->unitptr = (void *)0;
free(parse);
}
#ifdef HAVE_PPSAPI
/*----------------------------------------
* set up HARDPPS via PPSAPI
*/
static void
parse_hardpps(
struct parseunit *parse,
int mode
)
{
if (parse->hardppsstate == mode)
return;
if (CLK_PPS(parse->peer) && (parse->flags & PARSE_PPSKERNEL)) {
int i = 0;
if (mode == PARSE_HARDPPS_ENABLE)
{
if (parse->flags & PARSE_CLEAR)
i = PPS_CAPTURECLEAR;
else
i = PPS_CAPTUREASSERT;
}
if (time_pps_kcbind(parse->ppsctl.handle, PPS_KC_HARDPPS, i,
PPS_TSFMT_TSPEC) < 0) {
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: time_pps_kcbind failed: %m",
parse->peer->procptr->refclkunit);
} else {
NLOG(NLOG_CLOCKINFO)
msyslog(LOG_INFO, "REFCLOCK: PARSE receiver #%d: kernel PPS synchronisation %sabled",
parse->peer->procptr->refclkunit, (mode == PARSE_HARDPPS_ENABLE) ? "en" : "dis");
/*
* tell the rest, that we have a kernel PPS source, iff we ever enable HARDPPS
*/
if (mode == PARSE_HARDPPS_ENABLE)
clock_ctl.hardpps_enable = true;
}
}
parse->hardppsstate = mode;
}
/*----------------------------------------
* set up PPS via PPSAPI
*/
static bool
parse_ppsapi(
struct parseunit *parse
)
{
int cap, mode_ppsoffset;
const char *cp;
parse->flags &= (uint8_t) (~PARSE_PPSAPI);
/*
* collect PPSAPI offset capability - should move into generic handling
*/
if (time_pps_getcap(parse->ppsctl.handle, &cap) < 0) {
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: parse_ppsapi: time_pps_getcap failed: %m",
parse->peer->procptr->refclkunit);
return false;
}
/*
* initialize generic PPSAPI interface
*
* we leave out CLK_FLAG3 as time_pps_kcbind()
* is handled here for now. Ideally this should also
* be part of the generic PPSAPI interface
*/
if (!refclock_params(parse->flags & (CLK_FLAG1|CLK_FLAG2|CLK_FLAG4), &parse->ppsctl))
return false;
/* nb. only turn things on, if someone else has turned something
* on before we get here, leave it alone!
*/
if (parse->flags & PARSE_CLEAR) {
cp = "CLEAR";
mode_ppsoffset = PPS_OFFSETCLEAR;
} else {
cp = "ASSERT";
mode_ppsoffset = PPS_OFFSETASSERT;
}
msyslog(LOG_INFO, "REFCLOCK: PARSE receiver #%d: initializing PPS to %s",
parse->peer->procptr->refclkunit, cp);
if (!(mode_ppsoffset & cap)) {
msyslog(LOG_WARNING,
"REFCLOCK: PARSE receiver #%u: Cannot set PPS_%sCLEAR, "
" this will increase jitter (PPS API capabilities=0x%x)",
parse->peer->procptr->refclkunit, cp, (unsigned)cap);
mode_ppsoffset = 0;
} else {
if (mode_ppsoffset == PPS_OFFSETCLEAR)
{
parse->ppsctl.pps_params.clear_offset.tv_sec = (time_t)(-parse->ppsphaseadjust);
parse->ppsctl.pps_params.clear_offset.tv_nsec = (long)(-1e9*(parse->ppsphaseadjust - (double)(long)parse->ppsphaseadjust));
}
if (mode_ppsoffset == PPS_OFFSETASSERT)
{
parse->ppsctl.pps_params.assert_offset.tv_sec = (time_t)(-parse->ppsphaseadjust);
parse->ppsctl.pps_params.assert_offset.tv_nsec = (long)(-1e9*(parse->ppsphaseadjust - (double)(long)parse->ppsphaseadjust));
}
}
parse->ppsctl.pps_params.mode |= mode_ppsoffset;
if (time_pps_setparams(parse->ppsctl.handle, &parse->ppsctl.pps_params) < 0) {
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: FAILED set PPS parameters: %m",
parse->peer->procptr->refclkunit);
return false;
}
parse->flags |= PARSE_PPSAPI;
return true;
}
#else
#define parse_hardpps(_PARSE_, _MODE_) /* empty */
#endif
/*--------------------------------------------------
* parse_start - open the PARSE devices and initialize data for processing
*/
static bool
parse_start(
int sysunit,
struct peer *peer
)
{
unsigned int unit;
int fd232;
struct termios tio; /* NEEDED FOR A LONG TIME ! */
struct parseunit * parse;
char parsedev[sizeof(PARSEDEVICE)+20];
char parseppsdev[sizeof(PARSEPPSDEVICE)+20];
parsectl_t tmp_ctl;
unsigned int type;
char *path, *ppspath;
UNUSED_ARG(sysunit);
/*
* get out Copyright information once
*/
if (!notice)
{
NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
msyslog(LOG_INFO, "REFCLOCK: NTP PARSE support: Copyright (c) 1989-2015, Frank Kardel");
notice = 1;
}
#ifdef ENABLE_CLASSIC_MODE
peer->cfg.mode = (peer->procptr->refclkunit & ~0x80) >> 2;
peer->procptr->refclkunit = peer->procptr->refclkunit & 0x03;
#endif /* ENABLE_CLASSIC_MODE */
type = (unsigned int)CLK_TYPE(peer);
unit = peer->procptr->refclkunit;
if ((type == (unsigned int)~0) || (parse_clockinfo[type].cl_description == (char *)0))
{
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%u: parse_start: "
"unsupported clock type %d (max %d)",
unit, CLK_REALTYPE(peer), ncltypes-1);
return false;
}
/*
* Unit okay, attempt to open the device.
*/
if (peer->cfg.path)
path = peer->cfg.path;
else {
(void) snprintf(parsedev, sizeof(parsedev), PARSEDEVICE, unit);
path = parsedev;
}
if (peer->cfg.ppspath)
ppspath = peer->cfg.ppspath;
else {
(void) snprintf(parseppsdev, sizeof(parsedev), PARSEPPSDEVICE, unit);
ppspath = parseppsdev;
}
fd232 = open(path, O_RDWR | O_NOCTTY | O_NONBLOCK, 0777);
if (fd232 == -1)
{
msyslog(LOG_ERR,
"REFCLOCK: PARSE receiver #%u: parse_start: open of %s failed: %m",
unit, path);
return false;
}
parse = emalloc_zero(sizeof(*parse));
parse->generic = peer->procptr; /* link up */
parse->generic->unitptr = (void *)parse; /* link down */
/*
* Set up the structures
*/
parse->generic->timestarted = current_time;
parse->lastchange = current_time;
parse->flags = 0;
parse->pollneeddata = 0;
parse->laststatistic = current_time;
parse->lastformat = (unsigned short)~0; /* assume no format known */
parse->timedata.parse_status = (unsigned short)~0; /* be sure to mark initial status change */
parse->lastmissed = 0; /* assume got everything */
parse->ppsserial = 0;
parse->ppsfd = -1;
parse->localdata = (void *)0;
parse->localstate = 0;
parse->kv = (struct ctl_var *)0;
clear_err(parse, ERR_ALL);
parse->parse_type = &parse_clockinfo[type];
parse->maxunsync = parse->parse_type->cl_maxunsync;
parse->generic->fudgetime1 = parse->parse_type->cl_basedelay;
parse->generic->fudgetime2 = 0.0;
parse->ppsphaseadjust = parse->generic->fudgetime2;
parse->generic->clockname = parse->parse_type->cl_name;
parse->generic->clockdesc = parse->parse_type->cl_description;
peer->rootdelay = parse->parse_type->cl_rootdelay;
peer->sstclktype = parse->parse_type->cl_type;
peer->precision = sys_vars.sys_precision;
peer->stratum = STRATUM_REFCLOCK;
if (peer->stratum <= 1)
memmove((char *)&parse->generic->refid, parse->parse_type->cl_id, REFIDLEN);
else
parse->generic->refid = htonl(PARSEHSREFID);
parse->generic->io.fd = fd232;
parse->peer = peer; /* marks it also as busy */
/*
* configure terminal line
*/
if (TTY_GETATTR(fd232, &tio) == -1)
{
msyslog(LOG_ERR,
"REFCLOCK: PARSE receiver #%u: parse_start: tcgetattr(%d, &tio): %m",
unit, fd232);
/* let our cleaning staff do the work */
parse_shutdown(peer->procptr);
return false;
}
else
{
#ifndef _PC_VDISABLE
memset((char *)tio.c_cc, 0, sizeof(tio.c_cc));
#else
int disablec;
errno = 0; /* pathconf can deliver -1 without changing errno ! */
disablec = fpathconf(parse->generic->io.fd, _PC_VDISABLE);
if (disablec == -1 && errno)
{
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: parse_start: fpathconf(fd, _PC_VDISABLE): %m", parse->peer->procptr->refclkunit);
memset((char *)tio.c_cc, 0, sizeof(tio.c_cc)); /* best guess */
}
else
if (disablec != -1)
memset((char *)tio.c_cc, disablec, sizeof(tio.c_cc));
#endif
if ((parse_clockinfo[type].cl_lflag & ICANON) == 0)
{
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 0;
}
tio.c_cflag = (tcflag_t) parse_clockinfo[type].cl_cflag;
tio.c_iflag = (tcflag_t) parse_clockinfo[type].cl_iflag;
tio.c_oflag = (tcflag_t) parse_clockinfo[type].cl_oflag;
tio.c_lflag = (tcflag_t) parse_clockinfo[type].cl_lflag;
if ((cfsetospeed(&tio, (speed_t) parse_clockinfo[type].cl_speed) == -1) ||
(cfsetispeed(&tio, (speed_t) parse_clockinfo[type].cl_speed) == -1))
{
msyslog(LOG_ERR,
"REFCLOCK: PARSE receiver #%u: parse_start: "
" tcset{i,o}speed(&tio, speed): %m",
unit);
/* let our cleaning staff do the work */
parse_shutdown(peer->procptr);
return false;
}
/*
* set up pps device
* if the PARSEPPSDEVICE can be opened that will be used
* for PPS else PARSEDEVICE will be used
*/
parse->ppsfd = open(ppspath, O_RDWR | O_NOCTTY | O_NONBLOCK, 0777);
if (parse->ppsfd == -1)
{
parse->ppsfd = fd232;
}
/*
* Linux PPS - the old way
*/
#if defined(HAVE_LINUX_SERIAL_H) /* Linux hack: define PPS interface */
{
struct serial_struct ss;
if (ioctl(parse->ppsfd, TIOCGSERIAL, &ss) < 0 ||
(
#ifdef ASYNC_LOW_LATENCY
ss.flags |= (int)ASYNC_LOW_LATENCY,
#endif
#ifndef HAVE_PPSAPI
#ifdef ASYNC_PPS_CD_NEG
ss.flags |= (int)ASYNC_PPS_CD_NEG,
#endif
#endif
ioctl(parse->ppsfd, TIOCSSERIAL, &ss)) < 0) {
msyslog(LOG_NOTICE,
"REFCLOCK: refclock_parse: TIOCSSERIAL fd %d, %m", parse->ppsfd);
msyslog(LOG_NOTICE,
"REFCLOCK: refclock_parse: optional PPS processing not available");
} else {
parse->flags |= PARSE_PPSAPI;
#ifdef ASYNC_PPS_CD_NEG
NLOG(NLOG_CLOCKINFO)
msyslog(LOG_INFO,
"REFCLOCK: refclock_parse: PPS detection on");
#endif
}
}
#endif
/*
* PPS via PPSAPI
*/
#if defined(HAVE_PPSAPI)
parse->hardppsstate = PARSE_HARDPPS_DISABLE;
if (CLK_PPS(parse->peer))
{
if (!refclock_ppsapi(parse->ppsfd, &parse->ppsctl))
{
msyslog(LOG_NOTICE, "REFCLOCK: PARSE receiver #%d: parse_start: could not set up PPS: %m", parse->peer->procptr->refclkunit);
}
else
{
parse_ppsapi(parse);
}
}
#endif
if (TTY_SETATTR(fd232, &tio) == -1) {
msyslog(LOG_ERR,
"REFCLOCK: PARSE receiver #%u: parse_start: tcsetattr(%d, &tio): %m",
unit, fd232);
/* let our cleaning staff do the work */
parse_shutdown(peer->procptr);
return false;
}
}
/*
* pick correct input machine
*/
parse->generic->io.srcclock = peer;
parse->generic->io.datalen = 0;
parse->binding = init_iobinding(parse);
if (parse->binding == (bind_t *)0)
{
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: parse_start: io sub system initialisation failed.", parse->peer->procptr->refclkunit);
parse_shutdown(peer->procptr); /* let our cleaning staff do the work */
return false; /* well, ok - special initialisation broke */
}
parse->generic->io.clock_recv = parse->binding->bd_receive; /* pick correct receive routine */
parse->generic->io.io_input = parse->binding->bd_io_input; /* pick correct input routine */
/*
* as we always(?) get 8 bit chars we want to be
* sure, that the upper bits are zero for less
* than 8 bit I/O - so we pass that information on.
* note that there can be only one bit count format
* per file descriptor
*/
switch (tio.c_cflag & CSIZE)
{
case CS5:
tmp_ctl.parsesetcs.parse_cs = PARSE_IO_CS5;
break;
case CS6:
tmp_ctl.parsesetcs.parse_cs = PARSE_IO_CS6;
break;
case CS7:
tmp_ctl.parsesetcs.parse_cs = PARSE_IO_CS7;
break;
case CS8:
tmp_ctl.parsesetcs.parse_cs = PARSE_IO_CS8;
break;
default:
/* huh? */
break;
}
if (!PARSE_SETCS(parse, &tmp_ctl))
{
msyslog(LOG_ERR,
"REFCLOCK: PARSE receiver #%u: parse_start: parse_setcs() FAILED.",
unit);
parse_shutdown(peer->procptr); /* let our cleaning staff do the work */
return false; /* well, ok - special initialisation broke */
}
strlcpy(tmp_ctl.parseformat.parse_buffer, parse->parse_type->cl_format, sizeof(tmp_ctl.parseformat.parse_buffer));
tmp_ctl.parseformat.parse_count = (unsigned short) strlen(tmp_ctl.parseformat.parse_buffer);
if (!PARSE_SETFMT(parse, &tmp_ctl))
{
msyslog(LOG_ERR,
"REFCLOCK: PARSE receiver #%u: parse_start: parse_setfmt() FAILED.",
unit);
parse_shutdown(peer->procptr); /* let our cleaning staff do the work */
return false; /* well, ok - special initialisation broke */
}
/*
* get rid of all IO accumulated so far
*/
(void) tcflush(parse->generic->io.fd, TCIOFLUSH);
/*
* try to do any special initializations
*/
if (parse->parse_type->cl_init)
{
if (parse->parse_type->cl_init(parse))
{
parse_shutdown(peer->procptr); /* let our cleaning staff do the work */
return false; /* well, ok - special initialisation broke */
}
}
/*
* Insert in async io device list.
*/
if (!io_addclock(&parse->generic->io))
{
msyslog(LOG_ERR,
"REFCLOCK: PARSE receiver #%d: parse_start: addclock %s fails (ABORT - clock type requires async io)", parse->peer->procptr->refclkunit, parsedev);
parse_shutdown(peer->procptr); /* let our cleaning staff do the work */
return false;
}
/*
* print out configuration
*/
NLOG(NLOG_CLOCKINFO)
{
/* conditional if clause for conditional syslog */
msyslog(LOG_INFO, "REFCLOCK: PARSE receiver #%d: reference clock \"%s\" (I/O device %s, PPS device %s) added",
parse->peer->procptr->refclkunit,
parse->parse_type->cl_description, parsedev,
(parse->ppsfd != parse->generic->io.fd) ? parseppsdev : parsedev);
msyslog(LOG_INFO, "REFCLOCK: PARSE receiver #%d: Stratum %d, trust time %s, precision %d",
parse->peer->procptr->refclkunit,
parse->peer->stratum,
l_mktime(parse->maxunsync), parse->peer->precision);
msyslog(LOG_INFO, "REFCLOCK: PARSE receiver #%d: rootdelay %.6f s, phase adjustment %.6f s, PPS phase adjustment %.6f s, %s IO handling",
parse->peer->procptr->refclkunit,
parse->parse_type->cl_rootdelay,
parse->generic->fudgetime1,
parse->ppsphaseadjust,
parse->binding->bd_description);
msyslog(LOG_INFO, "REFCLOCK: PARSE receiver #%d: Format recognition: %s", parse->peer->procptr->refclkunit,
parse->parse_type->cl_format);
msyslog(LOG_INFO, "REFCLOCK: PARSE receiver #%d: %sPPS support",
parse->peer->procptr->refclkunit,
CLK_PPS(parse->peer) ? "" : "NO ");
}
return true;
}
/*--------------------------------------------------
* parse_ctl - process changes on flags/time values
*/
static void
parse_ctl(
struct parseunit *parse,
const struct refclockstat *in
)
{
if (in)
{
if (in->haveflags & (CLK_HAVEFLAG1|CLK_HAVEFLAG2|CLK_HAVEFLAG3|CLK_HAVEFLAG4))
{
uint8_t mask = CLK_FLAG1|CLK_FLAG2|CLK_FLAG3|CLK_FLAG4;
parse->flags = (parse->flags & (uint8_t)(~mask)) | (in->flags & mask);
#if defined(HAVE_PPSAPI)
if (CLK_PPS(parse->peer))
{
parse_ppsapi(parse);
}
#endif
}
if (in->haveflags & CLK_HAVETIME1)
{
parse->generic->fudgetime1 = in->fudgetime1;
msyslog(LOG_INFO, "REFCLOCK: PARSE receiver #%d: new phase adjustment %.6f s",
parse->peer->procptr->refclkunit,
parse->generic->fudgetime1);
}
if (in->haveflags & CLK_HAVETIME2)
{
parse->generic->fudgetime2 = in->fudgetime2;
if (parse->flags & PARSE_TRUSTTIME)
{
parse->maxunsync = (unsigned long)ABS(in->fudgetime2);
msyslog(LOG_INFO, "REFCLOCK: PARSE receiver #%d: new trust time %s",
parse->peer->procptr->refclkunit,
l_mktime(parse->maxunsync));
}
else
{
parse->ppsphaseadjust = in->fudgetime2;
msyslog(LOG_INFO, "REFCLOCK: PARSE receiver #%d: new PPS phase adjustment %.6f s",
parse->peer->procptr->refclkunit,
parse->ppsphaseadjust);
#if defined(HAVE_PPSAPI)
if (CLK_PPS(parse->peer))
{
parse_ppsapi(parse);
}
#endif
}
}
}
}
/*--------------------------------------------------
* parse_poll - called by the transmit procedure
*/
static void
parse_poll(
int unit,
struct peer *peer
)
{
struct parseunit *parse = peer->procptr->unitptr;
if (peer != parse->peer)
{
msyslog(LOG_ERR,
"REFCLOCK: PARSE receiver #%d: poll: INTERNAL: peer incorrect",
unit);
return;
}
/*
* Update clock stat counters
*/
parse->generic->polls++;
if (parse->pollneeddata &&
((int)(current_time - parse->pollneeddata) > (1<<(max(min(parse->peer->hpoll, parse->peer->ppoll), parse->peer->cfg.minpoll)))))
{
/*
* start worrying when exceeding a poll interval
* bad news - didn't get a response last time
*/
parse->lastmissed = current_time;
parse_event(parse, CEVNT_TIMEOUT);
ERR(ERR_NODATA)
msyslog(LOG_WARNING, "REFCLOCK: PARSE receiver #%d: no data from device within poll interval (check receiver / wiring)", parse->peer->procptr->refclkunit);
}
/*
* we just mark that we want the next sample for the clock filter
*/
parse->pollneeddata = current_time;
if (parse->parse_type->cl_poll)
{
parse->parse_type->cl_poll(parse);
}
cparse_statistics(parse);
return;
}
#define LEN_STATES 300 /* length of state string */
/*--------------------------------------------------
* parse_control - set fudge factors, return statistics
*/
static void
parse_control(
int unit,
const struct refclockstat *in,
struct refclockstat *out,
struct peer *peer
)
{
struct parseunit *parse = peer->procptr->unitptr;
parsectl_t tmpctl;
static char outstatus[400]; /* status output buffer */
if (out)
{
out->lencode = 0;
out->p_lastcode = 0;
out->kv_list = (struct ctl_var *)0;
}
if (!parse || !parse->peer)
{
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: parse_control: unit invalid (UNIT INACTIVE)",
unit);
return;
}
unit = parse->peer->procptr->refclkunit;
/*
* handle changes
*/
parse_ctl(parse, in);
/*
* supply data
*/
if (out)
{
unsigned long sum = 0;
char *tt, *start;
int i;
outstatus[0] = '\0';
/*
* keep fudgetime2 in sync with TRUSTTIME/MAXUNSYNC flag1
*/
parse->generic->fudgetime2 = (parse->flags & PARSE_TRUSTTIME) ? (double)parse->maxunsync : parse->ppsphaseadjust;
/*
* figure out skew between PPS and RS232 - just for informational
* purposes
*/
if (PARSE_SYNC(parse->timedata.parse_state))
{
if (PARSE_PPS(parse->timedata.parse_state) && PARSE_TIMECODE(parse->timedata.parse_state))
{
l_fp off;
/*
* we have a PPS and RS232 signal - calculate the skew
* WARNING: assumes on TIMECODE == PULSE (timecode after pulse)
*/
off = parse->timedata.parse_stime;
off -= parse->timedata.parse_ptime; /* true offset */
tt = add_var(&out->kv_list, 80, RO);
snprintf(tt, 80, "refclock_ppsskew=%s", lfptoms(off, 6));
}
}
if (PARSE_PPS(parse->timedata.parse_state))
{
tt = add_var(&out->kv_list, 80, RO|DEF);
snprintf(tt, 80, "refclock_ppstime=\"%s\"", prettydate(parse->timedata.parse_ptime));
}
start = tt = add_var(&out->kv_list, 128, RO|DEF);
tt = ap(start, 128, tt, "refclock_time=\"");
if (lfpuint(parse->timedata.parse_time) == 0)
{
ap(start, 128, tt, "<UNDEFINED>\"");
}
else
{
ap(start, 128, tt, "%s\"",
prettydate(parse->timedata.parse_time));
}
if (!PARSE_GETTIMECODE(parse, &tmpctl))
{
ERR(ERR_INTERNAL)
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: parse_control: parse_timecode() FAILED", unit);
}
else
{
start = tt = add_var(&out->kv_list, 512, RO|DEF);
tt = ap(start, 512, tt, "refclock_status=\"");
/*
* copy PPS flags from last read transaction (informational only)
*/
tmpctl.parsegettc.parse_state |= parse->timedata.parse_state &
(PARSEB_PPS|PARSEB_S_PPS);
(void)parsestate(tmpctl.parsegettc.parse_state, tt, BUFFER_SIZES(start, tt, 512));
tt += strlen(tt);
ap(start, 512, tt, "\"");
if (tmpctl.parsegettc.parse_count)
mkascii(outstatus+strlen(outstatus), (int)(sizeof(outstatus)- strlen(outstatus) - 1),
tmpctl.parsegettc.parse_buffer, (unsigned)(tmpctl.parsegettc.parse_count));
}
tmpctl.parseformat.parse_format = tmpctl.parsegettc.parse_format;
if (!PARSE_GETFMT(parse, &tmpctl))
{
ERR(ERR_INTERNAL)
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: parse_control: parse_getfmt() FAILED", unit);
}
else
{
int count = tmpctl.parseformat.parse_count - 1;
start = tt = add_var(&out->kv_list, 80, RO|DEF);
tt = ap(start, 80, tt, "refclock_format=\"");
if (count > 0) {
tt = ap(start, 80, tt, "%*.*s",
count,
count,
tmpctl.parseformat.parse_buffer);
}
ap(start, 80, tt, "\"");
}
/*
* gather state statistics
*/
start = tt = add_var(&out->kv_list, LEN_STATES, RO|DEF);
tt = ap(start, LEN_STATES, tt, "refclock_states=\"");
for (i = 0; i <= CEVNT_MAX; i++)
{
unsigned long s_time;
unsigned long d = current_time - parse->generic->timestarted;
unsigned long percent;
percent = s_time = PARSE_STATETIME(parse, i);
while (((unsigned long)(~0) / 10000) < percent)
{
percent /= 10;
d /= 10;
}
if (d)
percent = (percent * 10000) / d;
else
percent = 10000;
if (s_time)
{
char item[80];
int count;
snprintf(item, 80, "%s%s%s: %s (%d.%02d%%)",
sum ? "; " : "",
(parse->generic->currentstatus == i) ? "*" : "",
clockstatus((unsigned int)i),
l_mktime(s_time),
(int)(percent / 100), (int)(percent % 100));
if ((count = (int) strlen(item)) < (LEN_STATES - 40 - (tt - start)))
{
tt = ap(start, LEN_STATES, tt,
"%s", item);
}
sum += s_time;
}
}
tt = ap(start, LEN_STATES, tt,
"; running time: %s\"", l_mktime(sum));
add_var(&out->kv_list, 32, RO);
snprintf(tt, 32, "refclock_id=\"%s\"", parse->parse_type->cl_id);
add_var(&out->kv_list, 80, RO);
snprintf(tt, 80, "refclock_iomode=\"%s\"", parse->binding->bd_description);
add_var(&out->kv_list, 128, RO);
snprintf(tt, 128, "refclock_driver_version=\"%s\"", VERSION);
{
struct ctl_var *k;
k = parse->kv;
while (k && !(k->flags & EOV))
{
set_var(&out->kv_list, k->text, strlen(k->text)+1, k->flags);
k++;
}
}
out->lencode = (unsigned short) strlen(outstatus);
out->p_lastcode = outstatus;
}
}
/**===========================================================================
** processing routines
**/
/*--------------------------------------------------
* event handling - note that nominal events will also be posted
* keep track of state dwelling times
*/
static void
parse_event(
struct parseunit *parse,
int event
)
{
if (parse->generic->currentstatus != (uint8_t) event)
{
parse->statetime[parse->generic->currentstatus] += current_time - parse->lastchange;
parse->lastchange = current_time;
if (parse->parse_type->cl_event)
parse->parse_type->cl_event(parse, event);
if (event == CEVNT_NOMINAL)
{
NLOG(NLOG_CLOCKSTATUS)
msyslog(LOG_INFO, "REFCLOCK: PARSE receiver #%d: SYNCHRONIZED",
parse->peer->procptr->refclkunit);
}
refclock_report(parse->peer, event);
}
}
/*--------------------------------------------------
* process a PARSE time sample
*/
static void
parse_process(
struct parseunit *parse,
parsetime_t *parsetime
)
{
l_fp off, rectime = 0, reftime = 0;
double fudge;
/* silence warning: integral part may be used uninitialized in this function */
ZERO(off);
/*
* check for changes in conversion status
* (only one for each new status !)
*/
if (((parsetime->parse_status & CVT_MASK) != CVT_OK) &&
((parsetime->parse_status & CVT_MASK) != CVT_NONE) &&
(parse->timedata.parse_status != parsetime->parse_status))
{
char buffer[400];
NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
msyslog(LOG_WARNING, "REFCLOCK: PARSE receiver #%d: conversion status \"%s\"",
parse->peer->procptr->refclkunit, parsestatus(parsetime->parse_status, buffer, sizeof(buffer)));
if ((parsetime->parse_status & CVT_MASK) == CVT_FAIL)
{
/*
* tell more about the story - list time code
* there is a slight change for a race condition and
* the time code might be overwritten by the next packet
*/
parsectl_t tmpctl;
if (!PARSE_GETTIMECODE(parse, &tmpctl))
{
ERR(ERR_INTERNAL)
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: parse_process: parse_timecode() FAILED", parse->peer->procptr->refclkunit);
}
else
{
ERR(ERR_BADDATA)
msyslog(LOG_WARNING, "REFCLOCK: PARSE receiver #%d: FAILED TIMECODE: \"%s\" (check receiver configuration / wiring)",
parse->peer->procptr->refclkunit, mkascii(buffer, sizeof buffer, tmpctl.parsegettc.parse_buffer, (unsigned)(tmpctl.parsegettc.parse_count - 1)));
}
/* copy status to show only changes in case of failures */
parse->timedata.parse_status = parsetime->parse_status;
}
}
/*
* examine status and post appropriate events
*/
if ((parsetime->parse_status & CVT_MASK) != CVT_OK)
{
/*
* got bad data - tell the rest of the system
*/
switch (parsetime->parse_status & CVT_MASK)
{
case CVT_NONE:
if ((parsetime->parse_status & CVT_ADDITIONAL) &&
parse->parse_type->cl_message)
parse->parse_type->cl_message(parse, parsetime);
/*
* save PPS information that comes piggyback
*/
if (PARSE_PPS(parsetime->parse_state))
{
parse->timedata.parse_state |= PARSEB_PPS|PARSEB_S_PPS;
parse->timedata.parse_ptime = parsetime->parse_ptime;
}
break; /* well, still waiting - timeout is handled at higher levels */
case CVT_FAIL:
if (parsetime->parse_status & CVT_BADFMT)
{
parse_event(parse, CEVNT_BADREPLY);
}
else if (parsetime->parse_status & CVT_BADDATE)
{
parse_event(parse, CEVNT_BADDATE);
}
else if (parsetime->parse_status & CVT_BADTIME)
{
parse_event(parse, CEVNT_BADTIME);
}
else
{
/* for the lack of something better */
parse_event(parse, CEVNT_BADREPLY);
}
break;
default:
/* huh? */
break;
}
return; /* skip the rest - useless */
}
/*
* check for format changes
* (in case somebody has swapped clocks 8-)
*/
if (parse->lastformat != parsetime->parse_format)
{
parsectl_t tmpctl;
tmpctl.parseformat.parse_format = parsetime->parse_format;
if (!PARSE_GETFMT(parse, &tmpctl))
{
ERR(ERR_INTERNAL)
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: parse_getfmt() FAILED", parse->peer->procptr->refclkunit);
}
else
{
NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
msyslog(LOG_INFO, "REFCLOCK: PARSE receiver #%d: packet format \"%s\"",
parse->peer->procptr->refclkunit, tmpctl.parseformat.parse_buffer);
}
parse->lastformat = parsetime->parse_format;
}
/*
* now, any changes ?
*/
if ((parse->timedata.parse_state ^ parsetime->parse_state) &
~(unsigned)(PARSEB_PPS|PARSEB_S_PPS))
{
char tmp1[200];
char tmp2[200];
/*
* something happened - except for PPS events
*/
(void) parsestate(parsetime->parse_state, tmp1, sizeof(tmp1));
(void) parsestate(parse->timedata.parse_state, tmp2, sizeof(tmp2));
NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
msyslog(LOG_INFO,"REFCLOCK: PARSE receiver #%d: STATE CHANGE: %s -> %s",
parse->peer->procptr->refclkunit, tmp2, tmp1);
}
/*
* carry on PPS information if still usable
*/
if (PARSE_PPS(parse->timedata.parse_state) && !PARSE_PPS(parsetime->parse_state))
{
parsetime->parse_state |= PARSEB_PPS|PARSEB_S_PPS;
parsetime->parse_ptime = parse->timedata.parse_ptime;
}
/*
* remember for future
*/
parse->timedata = *parsetime;
/*
* check to see, whether the clock did a complete powerup or lost PZF signal
* and post correct events for current condition
*/
if (PARSE_POWERUP(parsetime->parse_state))
{
/*
* this is bad, as we have completely lost synchronisation
* well this is a problem with the receiver here
* for PARSE Meinberg DCF77 receivers the lost synchronisation
* is true as it is the powerup state and the time is taken
* from a crude real time clock chip
* for the PZF/GPS series this is only partly true, as
* PARSE_POWERUP only means that the pseudo random
* phase shift sequence cannot be found. this is only
* bad, if we have never seen the clock in the SYNC
* state, where the PHASE and EPOCH are correct.
* for reporting events the above business does not
* really matter, but we can use the time code
* even in the POWERUP state after having seen
* the clock in the synchronized state (PZF class
* receivers) unless we have had a telegram disruption
* after having seen the clock in the SYNC state. we
* thus require having seen the clock in SYNC state
* *after* having missed telegrams (noresponse) from
* the clock. one problem remains: we might use erroneously
* POWERUP data if the disruption is shorter than 1 polling
* interval. fortunately powerdowns last usually longer than 64
* seconds and the receiver is at least 2 minutes in the
* POWERUP or NOSYNC state before switching to SYNC
* for GPS receivers this can mean antenna problems and other causes.
* the additional grace period can be enables by a clock
* mode having the PARSE_F_POWERUPTRUST flag in cl_flag set.
*/
parse_event(parse, CEVNT_FAULT);
NLOG(NLOG_CLOCKSTATUS)
ERR(ERR_BADSTATUS)
msyslog(LOG_ERR,"REFCLOCK: PARSE receiver #%d: NOT SYNCHRONIZED/RECEIVER PROBLEMS",
parse->peer->procptr->refclkunit);
}
else
{
/*
* we have two states left
*
* SYNC:
* this state means that the EPOCH (timecode) and PHASE
* information has be read correctly (at least two
* successive PARSE timecodes were received correctly)
* this is the best possible state - full trust
*
* NOSYNC:
* The clock should be on phase with respect to the second
* signal, but the timecode has not been received correctly within
* at least the last two minutes. this is a sort of half baked state
* for PARSE Meinberg DCF77 clocks this is bad news (clock running
* without timecode confirmation)
* PZF 535 has also no time confirmation, but the phase should be
* very precise as the PZF signal can be decoded
*/
if (PARSE_SYNC(parsetime->parse_state))
{
/*
* currently completely synchronized - best possible state
*/
parse->lastsync = current_time;
clear_err(parse, ERR_BADSTATUS);
}
else
{
/*
* we have had some problems receiving the time code
*/
parse_event(parse, CEVNT_PROP);
NLOG(NLOG_CLOCKSTATUS)
ERR(ERR_BADSTATUS)
msyslog(LOG_ERR,"REFCLOCK: PARSE receiver #%d: TIMECODE NOT CONFIRMED",
parse->peer->procptr->refclkunit);
}
}
fudge = parse->generic->fudgetime1; /* standard RS232 Fudgefactor */
if (PARSE_TIMECODE(parsetime->parse_state))
{
rectime = parsetime->parse_stime;
off = reftime = parsetime->parse_time;
off -= rectime; /* prepare for PPS adjustments logic */
DPRINT(4, ("REFCLOCK: PARSE receiver #%d: Reftime %s, Recvtime %s - initial offset %s\n",
parse->peer->procptr->refclkunit,
prettydate(reftime),
prettydate(rectime),
lfptoa(off,6)));
}
if (PARSE_PPS(parsetime->parse_state) && CLK_PPS(parse->peer))
{
l_fp offset;
double ppsphaseadjust = parse->ppsphaseadjust;
#ifdef HAVE_PPSAPI
/*
* set fudge = 0.0 if already included in PPS time stamps
*/
if (parse->ppsctl.pps_params.mode & (PPS_OFFSETCLEAR|PPS_OFFSETASSERT))
{
ppsphaseadjust = 0.0;
}
#endif
/*
* we have a PPS signal - much better than the RS232 stuff (we hope)
*/
offset = parsetime->parse_ptime;
DPRINT(4, ("REFCLOCK: PARSE receiver #%d: PPStime %s\n",
parse->peer->procptr->refclkunit,
prettydate(offset)));
if (PARSE_TIMECODE(parsetime->parse_state))
{
if (fabsl(lfptod(off)) <= 0.5)
{
fudge = ppsphaseadjust; /* pick PPS fudge factor */
/*
* RS232 offsets within [-0.5..0.5] - take PPS offsets
*/
if (parse->parse_type->cl_flags & PARSE_F_PPSONSECOND)
{
reftime = off = offset;
if (lfpfrac(reftime) & 0x80000000)
bumplfpuint(reftime, 1);
setlfpfrac(reftime, 0);
/*
* implied on second offset
*/
/* map [0.5..1[ -> [-0.5..0[ */
setlfpfrac(off, ~lfpfrac(off));
/* sign extend */
setlfpuint(off,
(unsigned long int)(
(lfpfrac(off) & 0x80000000) ?
-1 : 0));
}
else
{
/*
* time code describes pulse
*/
reftime = off = parsetime->parse_time;
off -= offset; /* true offset */
}
}
/*
* take RS232 offset when PPS when out of bounds
*/
}
else
{
fudge = ppsphaseadjust; /* pick PPS fudge factor */
/*
* Well, no time code to guide us - assume on second pulse
* and pray, that we are within [-0.5..0.5[
*/
off = offset;
reftime = offset;
if (lfpfrac(reftime) & 0x80000000)
bumplfpuint(reftime, 1);
setlfpfrac(reftime, 0);
/*
* implied on second offset
*/
/* map [0.5..1[ -> [-0.5..0[ */
setlfpfrac(off, ~lfpfrac(off));
/* sign extend */
setlfpuint(off, ((lfpfrac(off) & 0x80000000) ?
(unsigned long int)-1 : 0));
}
}
else
{
if (!PARSE_TIMECODE(parsetime->parse_state))
{
/*
* Well, no PPS, no TIMECODE, no more work ...
*/
if ((parsetime->parse_status & CVT_ADDITIONAL) &&
parse->parse_type->cl_message)
parse->parse_type->cl_message(parse, parsetime);
return;
}
}
DPRINT(4, ("REFCLOCK: PARSE receiver #%d: Reftime %s, Recvtime %s - final offset %s\n",
parse->peer->procptr->refclkunit,
prettydate(reftime),
prettydate(rectime),
lfptoa(off,6)));
rectime = reftime;
rectime -= off; /* just to keep the ntp interface happy */
DPRINT(4, ("REFCLOCK: PARSE receiver #%d: calculated Reftime %s, Recvtime %s\n",
parse->peer->procptr->refclkunit,
prettydate(reftime),
prettydate(rectime)));
if ((parsetime->parse_status & CVT_ADDITIONAL) &&
parse->parse_type->cl_message)
parse->parse_type->cl_message(parse, parsetime);
if (PARSE_SYNC(parsetime->parse_state))
{
/*
* log OK status
*/
parse_event(parse, CEVNT_NOMINAL);
}
clear_err(parse, ERR_BADIO);
clear_err(parse, ERR_BADDATA);
clear_err(parse, ERR_NODATA);
clear_err(parse, ERR_INTERNAL);
/*
* and now stick it into the clock machine
* samples are only valid iff lastsync is not too old and
* we have seen the clock in sync at least once
* after the last time we didn't see an expected data telegram
* at startup being not in sync is also bad just like
* POWERUP state unless PARSE_F_POWERUPTRUST is set
* see the clock states section above for more reasoning
*/
if (((current_time - parse->lastsync) > parse->maxunsync) ||
(parse->lastsync < parse->lastmissed) ||
((parse->lastsync == 0) && !PARSE_SYNC(parsetime->parse_state)) ||
(((parse->parse_type->cl_flags & PARSE_F_POWERUPTRUST) == 0) &&
PARSE_POWERUP(parsetime->parse_state)))
{
parse->generic->leap = LEAP_NOTINSYNC;
parse->lastsync = 0; /* wait for full sync again */
}
else
{
if (PARSE_LEAPADD(parsetime->parse_state))
{
/*
* we pick this state also for time code that pass leap warnings
* without direction information (as earth is currently slowing
* down).
*/
parse->generic->leap = (parse->flags & PARSE_LEAP_DELETE) ? LEAP_DELSECOND : LEAP_ADDSECOND;
}
else
if (PARSE_LEAPDEL(parsetime->parse_state))
{
parse->generic->leap = LEAP_DELSECOND;
}
else
{
parse->generic->leap = LEAP_NOWARNING;
}
}
if (parse->generic->leap != LEAP_NOTINSYNC)
{
/*
* only good/trusted samples are interesting
*/
DPRINT(3, ("REFCLOCK: PARSE receiver #%d: refclock_process_offset(reftime=%s, rectime=%s, Fudge=%f)\n",
parse->peer->procptr->refclkunit,
prettydate(reftime),
prettydate(rectime),
fudge));
parse->generic->lastref = reftime;
refclock_process_offset(parse->generic, reftime, rectime, fudge);
#ifdef HAVE_PPSAPI
/*
* pass PPS information on to PPS clock
*/
if (PARSE_PPS(parsetime->parse_state) && CLK_PPS(parse->peer))
{
parse->peer->cfg.flags |= (FLAG_PPS | FLAG_TSTAMP_PPS);
parse_hardpps(parse, PARSE_HARDPPS_ENABLE);
}
#endif
} else {
parse_hardpps(parse, PARSE_HARDPPS_DISABLE);
parse->peer->cfg.flags &= ~(FLAG_PPS | FLAG_TSTAMP_PPS);
}
/*
* ready, unless the machine wants a sample or
* we are in fast startup mode (peer->dist > MAXDISTANCE)
*/
if (!parse->pollneeddata && parse->peer->disp <= MAXDISTANCE)
return;
parse->pollneeddata = 0;
parse->timedata.parse_state &= ~(unsigned)(PARSEB_PPS|PARSEB_S_PPS);
refclock_receive(parse->peer);
}
/**===========================================================================
** special code for special clocks
**/
static void
mk_utcinfo(
char *t, // pointer to the output string buffer
int wnt,
int wnlsf,
int dn,
int dtls,
int dtlsf,
int size // size of the output string buffer
)
{
/*
* The week number transmitted by the GPS satellites for the leap date
* is truncated to 8 bits only. If the nearest leap second date is off
* the current date by more than +/- 128 weeks then conversion to a
* calendar date is ambiguous. On the other hand, if a leap second is
* currently being announced (i.e. dtlsf != dtls) then the week number
* wnlsf is close enough, and we can unambiguously determine the date
* for which the leap second is scheduled.
*/
if ( dtlsf != dtls )
{
time_t t_ls;
struct tm tmbuf, *tm;
int n = 0;
if (wnlsf < GPSWRAP)
wnlsf += GPSWEEKS;
if (wnt < GPSWRAP)
wnt += GPSWEEKS;
t_ls = (time_t)(wnlsf * SECSPERWEEK +
dn * SECSPERDAY +
(int)GPS_SEC_BIAS - 1);
tm = gmtime_r( &t_ls, &tmbuf );
if (tm == NULL) // gmtime_r() failed
{
snprintf( t, (size_t)size,
"** (gmtime_r() failed in mk_utcinfo())" );
return;
}
n += snprintf( t, (size_t)size,
"UTC offset transition from %is to %is due to leap second %s",
dtls, dtlsf, ( dtls < dtlsf ) ? "insertion" : "deletion" );
n += snprintf(t + n, (size_t)(size - n),
" at UTC midnight at the end of %04i-%02i-%02i",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
}
else
snprintf( t, (size_t)size,
"UTC offset parameter: %is, no leap second announced.\n",
dtls );
}
#ifdef CLOCK_MEINBERG
/**===========================================================================
** Meinberg GPS receiver support
**/
/*------------------------------------------------------------
* gps16x_message - process messages from Meinberg GPS receiver
*/
static void
gps16x_message(
struct parseunit *parse,
parsetime_t *parsetime
)
{
if (parse->timedata.parse_msglen && parsetime->parse_msg[0] == SOH)
{
GPS_MSG_HDR header;
unsigned char *bufp = (unsigned char *)parsetime->parse_msg + 1;
#ifdef DEBUG
if (debug > 2) /* SPECIAL DEBUG */
{
char msgbuffer[600];
mkreadable(msgbuffer, sizeof(msgbuffer), (char *)parsetime->parse_msg, parsetime->parse_msglen, 1);
printf("REFCLOCK: PARSE receiver #%d: received message (%d bytes) >%s<\n",
parse->peer->procptr->refclkunit,
parsetime->parse_msglen,
msgbuffer);
}
#endif
get_mbg_header(&bufp, &header);
if (header.hdr_csum == mbg_csum(parsetime->parse_msg + 1, 6) &&
(header.len == 0 ||
(header.len < sizeof(parsetime->parse_msg) &&
header.data_csum == mbg_csum(bufp, header.len))))
{
/*
* clean message
*/
switch (header.cmd)
{
case GPS_SW_REV:
{
char buffer[64];
SW_REV gps_sw_rev;
get_mbg_sw_rev(&bufp, &gps_sw_rev);
snprintf(buffer, sizeof(buffer),
"meinberg_gps_version=\"%x.%02x%s%s\"",
(unsigned)((gps_sw_rev.code >> 8) & 0xFF),
(unsigned)(gps_sw_rev.code & 0xFF),
gps_sw_rev.name[0] ? " " : "",
gps_sw_rev.name);
set_var(&parse->kv, buffer, strlen(buffer)+1,
RO|DEF);
}
break;
case GPS_BVAR_STAT:
{
static struct state
{
BVAR_STAT flag; /* status flag */
const char *string; /* bit name */
} states[] =
{
{ BVAR_CFGH_INVALID, "Configuration/Health" },
{ BVAR_ALM_NOT_COMPLETE, "Almanachs" },
{ BVAR_UTC_INVALID, "UTC Correction" },
{ BVAR_IONO_INVALID, "Ionospheric Correction" },
{ BVAR_RCVR_POS_INVALID, "Receiver Position" },
{ 0, "" }
};
BVAR_STAT status;
struct state *s = states;
char buffer[512];
char *p, *b;
status = (BVAR_STAT)get_lsb_uint16(&bufp);
p = b = buffer;
p = ap(buffer, sizeof(buffer), p,
"meinberg_gps_status=\"[0x%04x] ",
status);
if (status)
{
p = ap(buffer, sizeof(buffer), p, "incomplete buffered data: ");
b = p;
while (s->flag)
{
if (status & s->flag)
{
if (p != b)
{
p = ap(buffer, sizeof(buffer), p, ", ");
}
p = ap(buffer, sizeof(buffer), p, "%s", (const char *)s->string);
}
s++;
}
ap(buffer, sizeof(buffer), p, "\"");
}
else
{
ap(buffer, sizeof(buffer), p, "<all buffered data complete>\"");
}
set_var(&parse->kv, buffer, strlen(buffer)+1, RO|DEF);
}
break;
case GPS_POS_XYZ:
{
XYZ xyz;
char buffer[256];
get_mbg_xyz(&bufp, xyz);
snprintf(buffer, sizeof(buffer), "gps_position(XYZ)=\"%s m, %s m, %s m\"",
mfptoa(xyz[XP], 1),
mfptoa(xyz[YP], 1),
mfptoa(xyz[ZP], 1));
set_var(&parse->kv, buffer, sizeof(buffer), RO|DEF);
}
break;
case GPS_POS_LLA:
{
LLA lla;
char buffer[256];
get_mbg_lla(&bufp, lla);
snprintf(buffer, sizeof(buffer), "gps_position(LLA)=\"%s deg, %s deg, %s m\"",
mfptoa(lla[LAT], 4),
mfptoa(lla[LON], 4),
mfptoa(lla[ALT], 1));
set_var(&parse->kv, buffer, sizeof(buffer), RO|DEF);
}
break;
case GPS_TZDL:
break;
case GPS_PORT_PARM:
break;
case GPS_SYNTH:
break;
case GPS_ANT_INFO:
{
ANT_INFO antinfo;
char buffer[512];
char *p, *q;
get_mbg_antinfo(&bufp, &antinfo);
p = buffer;
p = ap(buffer, sizeof(buffer), p, "meinberg_antenna_status=\"");
switch (antinfo.status)
{
case ANT_INVALID: // No other fields valid since antenna has not yet been disconnected
p = ap(buffer, sizeof(buffer),
p, "<OK>");
break;
case ANT_DISCONN: // Antenna is disconnected, tm_reconn and delta_t not yet set
q = ap(buffer, sizeof(buffer),
p, "DISCONNECTED since ");
NLOG(NLOG_CLOCKSTATUS)
ERR(ERR_BADSTATUS)
msyslog(LOG_ERR,"REFCLOCK: PARSE receiver #%d: ANTENNA FAILURE: %s",
parse->peer->procptr->refclkunit, p);
p = q;
mbg_tm_str(&p, &antinfo.tm_disconn, BUFFER_SIZE(buffer, p), 0);
*p = '\0';
break;
case ANT_RECONN: // Antenna had been disconnect, but receiver sync. after reconnect, so all fields valid
p = ap(buffer, sizeof(buffer),
p, "SYNC AFTER RECONNECT on ");
mbg_tm_str(&p, &antinfo.tm_reconn, BUFFER_SIZE(buffer, p), 0);
p = ap(buffer, sizeof(buffer),
p, ", clock offset at reconnect %c%ld.%07ld s, disconnect time ",
(antinfo.delta_t < 0) ? '-' : '+',
(long) ABS(antinfo.delta_t) / 10000,
(long) ABS(antinfo.delta_t) % 10000);
mbg_tm_str(&p, &antinfo.tm_disconn, BUFFER_SIZE(buffer, p), 0);
*p = '\0';
break;
default:
p = ap(buffer, sizeof(buffer),
p, "bad status 0x%04x",
(unsigned)antinfo.status);
break;
}
ap(buffer, sizeof(buffer), p, "\"");
set_var(&parse->kv, buffer, sizeof(buffer), RO|DEF);
}
break;
case GPS_UCAP:
break;
case GPS_CFGH:
{
CFGH cfgh;
char buffer[512];
char *p;
get_mbg_cfgh(&bufp, &cfgh);
if (cfgh.valid)
{
const char *cp;
uint16_t tmp_val;
int i;
p = buffer;
p = ap(buffer, sizeof(buffer),
p, "gps_tot_51=\"");
mbg_tgps_str(&p, &cfgh.tot_51, BUFFER_SIZE(buffer, p));
ap(buffer, sizeof(buffer),
p, "\"");
set_var(&parse->kv, buffer, sizeof(buffer), RO|COND_DEF);
p = buffer;
p = ap(buffer, sizeof(buffer),
p, "gps_tot_63=\"");
mbg_tgps_str(&p, &cfgh.tot_63, BUFFER_SIZE(buffer, p));
ap(buffer, sizeof(buffer),
p, "\"");
set_var(&parse->kv, buffer, sizeof(buffer), RO|COND_DEF);
p = buffer;
p = ap(buffer, sizeof(buffer),
p, "gps_t0a=\"");
mbg_tgps_str(&p, &cfgh.t0a, BUFFER_SIZE(buffer, p));
ap(buffer, sizeof(buffer),
p, "\"");
set_var(&parse->kv, buffer, sizeof(buffer), RO|COND_DEF);
for (i = 0; i < N_SVNO_GPS; i++)
{
p = buffer;
p = ap(buffer, sizeof(buffer), p, "sv_info[%d]=\"PRN%d", i, i + N_SVNO_GPS);
tmp_val = cfgh.health[i]; /* a 6 bit SV health code */
p = ap(buffer, sizeof(buffer), p, "; health=0x%02x (", tmp_val);
/* "All Ones" has a special meaning" */
if (tmp_val == 0x3F) /* satellite is unusable or doesn't even exist */
cp = "SV UNAVAILABLE";
else {
/* The MSB contains a summary of the 3 MSBs of the 8 bit health code,
* indicating if the data sent by the satellite is OK or not. */
p = ap(buffer, sizeof(buffer), p, "DATA %s, ", (tmp_val & 0x20) ? "BAD" : "OK" );
/* The 5 LSBs contain the status of the different signals sent by the satellite. */
switch (tmp_val & 0x1F)
{
case 0x00: cp = "SIGNAL OK"; break;
/* codes 0x01 through 0x1B indicate that one or more
* specific signal components are weak or dead.
* We don't decode this here in detail. */
case 0x1C: cp = "SV IS TEMP OUT"; break;
case 0x1D: cp = "SV WILL BE TEMP OUT"; break;
default: cp = "TRANSMISSION PROBLEMS"; break;
}
}
p = ap(buffer, sizeof(buffer), p, "%s)", cp );
tmp_val = cfgh.cfg[i]; /* a 4 bit SV configuration/type code */
p = ap(buffer, sizeof(buffer), p, "; cfg=0x%02x (", tmp_val);
switch (tmp_val & 0x7)
{
case 0x00: cp = "(reserved)"; break;
case 0x01: cp = "BLOCK II/IIA/IIR"; break;
case 0x02: cp = "BLOCK IIR-M"; break;
case 0x03: cp = "BLOCK IIF"; break;
case 0x04: cp = "BLOCK III"; break;
default: cp = "unknown SV type"; break;
}
p = ap(buffer, sizeof(buffer), p, "%s", cp );
if (tmp_val & 0x08) /* A-S is on, P-code is encrypted */
p = ap( buffer, sizeof(buffer), p, ", A-S on" );
ap(buffer, sizeof(buffer), p, ")\"");
set_var(&parse->kv, buffer, sizeof(buffer), RO|COND_DEF);
}
}
}
break;
case GPS_ALM:
break;
case GPS_EPH:
break;
case GPS_UTC:
{
UTC utc;
char buffer[512];
char *p;
p = buffer;
get_mbg_utc(&bufp, &utc);
if (utc.valid)
{
p = ap(buffer, sizeof(buffer), p, "gps_utc_correction=\"");
mk_utcinfo(p, utc.t0t.wn, utc.WNlsf, utc.DNt,
utc.delta_tls, utc.delta_tlsf,
(int)BUFFER_SIZE(buffer, p));
p += strlen(p);
ap(buffer, sizeof(buffer), p, "\"");
}
else
{
ap(buffer, sizeof(buffer), p, "gps_utc_correction=\"<NO UTC DATA>\"");
}
set_var(&parse->kv, buffer, sizeof(buffer), RO|DEF);
}
break;
case GPS_IONO:
break;
case GPS_ASCII_MSG:
{
ASCII_MSG gps_ascii_msg;
char buffer[128];
get_mbg_ascii_msg(&bufp, &gps_ascii_msg);
strlcpy(buffer, "gps_message=", sizeof(buffer));
if (gps_ascii_msg.valid)
{
char buffer1[128];
mkreadable(buffer1, sizeof(buffer1), gps_ascii_msg.s, strlen(gps_ascii_msg.s), (int)0);
strlcat(buffer, buffer1, sizeof(buffer));
}
else
strlcat(buffer, "<None>", sizeof(buffer));
set_var(&parse->kv, buffer, sizeof(buffer), RO|DEF);
}
break;
default:
break;
}
}
else
{
msyslog(LOG_DEBUG, "REFCLOCK: PARSE receiver #%d: gps16x_message: message checksum error: hdr_csum = 0x%x (expected 0x%x), "
"data_len = %d, data_csum = 0x%x (expected 0x%x)",
parse->peer->procptr->refclkunit,
header.hdr_csum, mbg_csum(parsetime->parse_msg + 1, 6),
header.len,
header.data_csum, mbg_csum(bufp, (unsigned)((header.len < sizeof(parsetime->parse_msg)) ? header.len : 0)));
}
}
return;
}
/*------------------------------------------------------------
* gps16x_poll - query the receiver periodically
*/
static void
gps16x_poll(
struct peer *peer
)
{
struct parseunit *parse = peer->procptr->unitptr;
static GPS_MSG_HDR sequence[] =
{
{ GPS_SW_REV, 0, 0, 0 },
{ GPS_BVAR_STAT, 0, 0, 0 },
{ GPS_UTC, 0, 0, 0 },
{ GPS_ASCII_MSG, 0, 0, 0 },
{ GPS_ANT_INFO, 0, 0, 0 },
{ GPS_CFGH, 0, 0, 0 },
{ GPS_POS_XYZ, 0, 0, 0 },
{ GPS_POS_LLA, 0, 0, 0 },
{ (unsigned short)~0, 0, 0, 0 }
};
int rtc;
unsigned char cmd_buffer[64];
unsigned char *outp = cmd_buffer;
GPS_MSG_HDR *header;
if (((poll_info_t *)parse->parse_type->cl_data)->rate)
{
parse->peer->procptr->nextaction = current_time + ((poll_info_t *)parse->parse_type->cl_data)->rate;
}
if (sequence[parse->localstate].cmd == (unsigned short)~0)
parse->localstate = 0;
header = sequence + parse->localstate++;
*outp++ = SOH; /* start command */
put_mbg_header(&outp, header);
outp = cmd_buffer + 1;
header->hdr_csum = (CSUM)mbg_csum(outp, 6);
put_mbg_header(&outp, header);
#ifdef DEBUG
if (debug > 2) /* SPECIAL DEBUG */
{
char buffer[128];
mkreadable(buffer, sizeof(buffer), (char *)cmd_buffer, (unsigned)(outp - cmd_buffer), 1);
printf(
"REFCLOCK: PARSE receiver #%d: transmitted message #%lu (%d bytes) >%s<\n",
parse->peer->procptr->refclkunit,
parse->localstate - 1,
(int)(outp - cmd_buffer),
buffer);
}
#endif
rtc = (int) write(parse->generic->io.fd, cmd_buffer, (unsigned long)(outp - cmd_buffer));
if (rtc < 0)
{
ERR(ERR_BADIO)
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: gps16x_poll: failed to send cmd to clock: %m", parse->peer->procptr->refclkunit);
}
else
if (rtc != outp - cmd_buffer)
{
ERR(ERR_BADIO)
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: gps16x_poll: failed to send cmd incomplete (%d of %d bytes sent)", parse->peer->procptr->refclkunit, rtc, (int)(outp - cmd_buffer));
}
clear_err(parse, ERR_BADIO);
return;
}
/*--------------------------------------------------
* init routine - setup timer
*/
static bool
gps16x_poll_init(
struct parseunit *parse
)
{
if (((poll_info_t *)parse->parse_type->cl_data)->rate)
{
parse->peer->procptr->action = gps16x_poll;
gps16x_poll(parse->peer);
}
return false;
}
#else
static void
gps16x_message(
struct parseunit *parse,
parsetime_t *parsetime
)
{}
static bool
gps16x_poll_init(
struct parseunit *parse
)
{
return true;
}
#endif /* CLOCK_MEINBERG */
/**===========================================================================
** clock polling support
**/
/*--------------------------------------------------
* direct poll routine
*/
static void
poll_dpoll(
struct parseunit *parse
)
{
long rtc;
const char *ps = ((poll_info_t *)parse->parse_type->cl_data)->string;
long ct = (long)(((poll_info_t *)parse->parse_type->cl_data)->count);
rtc = write(parse->generic->io.fd, ps, (size_t)ct);
if (rtc < 0)
{
ERR(ERR_BADIO)
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: poll_dpoll: failed to send cmd to clock: %m", parse->peer->procptr->refclkunit);
}
else
if (rtc != ct)
{
ERR(ERR_BADIO)
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: poll_dpoll: failed to send cmd incomplete (%ld of %ld bytes sent)", parse->peer->procptr->refclkunit, rtc, ct);
}
clear_err(parse, ERR_BADIO);
}
/*--------------------------------------------------
* periodic poll routine
*/
static void
poll_poll(
struct peer *peer
)
{
struct parseunit *parse = peer->procptr->unitptr;
if (parse->parse_type->cl_poll)
parse->parse_type->cl_poll(parse);
if (((poll_info_t *)parse->parse_type->cl_data)->rate)
{
parse->peer->procptr->nextaction = current_time + ((poll_info_t *)parse->parse_type->cl_data)->rate;
}
}
/*--------------------------------------------------
* init routine - setup timer
*/
static bool
poll_init(
struct parseunit *parse
)
{
if (((poll_info_t *)parse->parse_type->cl_data)->rate)
{
parse->peer->procptr->action = poll_poll;
poll_poll(parse->peer);
}
return false;
}
/**===========================================================================
** Trimble support
**/
/*-------------------------------------------------------------
* trimble TAIP init routine - setup EOL and then do poll_init.
*/
static bool
trimbletaip_init(
struct parseunit *parse
)
{
struct termios tio;
/*
* configure terminal line for trimble receiver
*/
if (TTY_GETATTR(parse->generic->io.fd, &tio) == -1)
{
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: trimbletaip_init: tcgetattr(fd, &tio): %m", parse->peer->procptr->refclkunit);
return false;
}
else
{
tio.c_cc[VEOL] = TRIMBLETAIP_EOL;
if (TTY_SETATTR(parse->generic->io.fd, &tio) == -1)
{
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: trimbletaip_init: tcsetattr(fd, &tio): %m", parse->peer->procptr->refclkunit);
return false;
}
}
return poll_init(parse);
}
/*--------------------------------------------------
* trimble TAIP event routine - reset receiver upon data format trouble
*/
static const char *taipinit[] = {
">FPV00000000<",
">SRM;ID_FLAG=F;CS_FLAG=T;EC_FLAG=F;FR_FLAG=T;CR_FLAG=F<",
">FTM00020001<",
(char *)0
};
static void
trimbletaip_event(
struct parseunit *parse,
int event
)
{
switch (event)
{
case CEVNT_BADREPLY: /* reset on garbled input */
case CEVNT_TIMEOUT: /* reset on no input */
{
const char **iv;
iv = taipinit;
while (*iv)
{
int rtc = (int) write(parse->generic->io.fd, *iv, strlen(*iv));
if (rtc < 0)
{
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: trimbletaip_event: failed to send cmd to clock: %m", parse->peer->procptr->refclkunit);
return;
}
else
{
if (rtc != (int)strlen(*iv))
{
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: trimbletaip_event: failed to send cmd incomplete (%d of %d bytes sent)",
parse->peer->procptr->refclkunit, rtc, (int)strlen(*iv));
return;
}
}
iv++;
}
NLOG(NLOG_CLOCKINFO)
ERR(ERR_BADIO)
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: trimbletaip_event: RECEIVER INITIALIZED",
parse->peer->procptr->refclkunit);
}
break;
default: /* ignore */
break;
}
}
/*
* This driver supports the Trimble SVee Six Plus GPS receiver module.
* It should support other Trimble receivers which use the Trimble Standard
* Interface Protocol (see below).
*
* The module has a serial I/O port for command/data and a 1 pulse-per-second
* output, about 1 microsecond wide. The leading edge of the pulse is
* coincident with the change of the GPS second. This is the same as
* the change of the UTC second +/- ~1 microsecond. Some other clocks
* specifically use a feature in the data message as a timing reference, but
* the SVee Six Plus does not do this. In fact there is considerable jitter
* on the timing of the messages, so this driver only supports the use
* of the PPS pulse for accurate timing. Where it is determined that
* the offset is way off, when first starting up ntpd for example,
* the timing of the data stream is used until the offset becomes low enough
* (|offset| < CLOCK_MAX), at which point the pps offset is used.
*
* It can use either option for receiving PPS information - the 'ppsclock'
* stream pushed onto the serial data interface to timestamp the Carrier
* Detect interrupts, where the 1PPS connects to the CD line. This only
* works on SunOS 4.1.x currently. To select this, define PPSPPS in
* Config.local. The other option is to use a pulse-stretcher/level-converter
* to convert the PPS pulse into a RS232 start pulse & feed this into another
* tty port. To use this option, define PPSCLK in Config.local. The pps input,
* by whichever method, is handled in ntp_loopfilter.c
*
* The receiver uses a serial message protocol called Trimble Standard
* Interface Protocol (it can support others but this driver only supports
* TSIP). Messages in this protocol have the following form:
*
* <DLE><id> ... <data> ... <DLE><ETX>
*
* Any bytes within the <data> portion of value 10 hex (<DLE>) are doubled
* on transmission and compressed back to one on reception. Otherwise
* the values of data bytes can be anything. The serial interface is RS-422
* asynchronous using 9600 baud, 8 data bits with odd party (**note** 9 bits
* in total!), and 1 stop bit. The protocol supports byte, integer, single,
* and double datatypes. Integers are two bytes, sent most significant first.
* Singles are IEEE754 single precision floating point numbers (4 byte) sent
* sign & exponent first. Doubles are IEEE754 double precision floating point
* numbers (8 byte) sent sign & exponent first.
* The receiver supports a large set of messages, only a small subset of
* which are used here. From driver to receiver the following are used:
*
* ID Description
*
* 21 Request current time
* 22 Mode Select
* 2C Set/Request operating parameters
* 2F Request UTC info
* 35 Set/Request I/O options
* From receiver to driver the following are recognised:
*
* ID Description
*
* 41 GPS Time
* 44 Satellite selection, PDOP, mode
* 46 Receiver health
* 4B Machine code/status
* 4C Report operating parameters (debug only)
* 4F UTC correction data (used to get leap second warnings)
* 55 I/O options (debug only)
*
* All others are accepted but ignored.
*
*/
#define PI 3.1415926535898 /* lots of sig figs */
#define D2R PI/180.0
/*-------------------------------------------------------------------
* sendcmd, sendbyte, sendetx, sendflt implement the command
* interface to the receiver.
*
* CAVEAT: the sendflt routine is byte order dependent and
* float implementation dependent - these must be converted to portable
* versions !
*
* CURRENT LIMITATION: float implementation. This runs only on systems
* with IEEE754 floats as native floats
*/
typedef struct trimble
{
unsigned long last_msg; /* last message received */
unsigned long last_reset; /* last time a reset was issued */
uint8_t qtracking; /* query tracking status */
unsigned long ctrack; /* current tracking set */
unsigned long ltrack; /* last tracking set */
} trimble_t;
union uval {
uint8_t bd[8];
int iv;
float fv;
double dv;
};
struct txbuf
{
short idx; /* index to first unused byte */
uint8_t *txt; /* pointer to actual data buffer */
};
static void sendcmd (struct txbuf *buf, int c);
static void sendbyte (struct txbuf *buf, int b);
static void sendetx (struct txbuf *buf, struct parseunit *parse);
static void sendflt (struct txbuf *buf, double a);
void
sendcmd(
struct txbuf *buf,
int c
)
{
buf->txt[0] = DLE;
buf->txt[1] = (uint8_t)c;
buf->idx = 2;
}
void
sendbyte(
struct txbuf *buf,
int b
)
{
if (b == DLE)
buf->txt[buf->idx++] = DLE;
buf->txt[buf->idx++] = (uint8_t)b;
}
void
sendetx(
struct txbuf *buf,
struct parseunit *parse
)
{
buf->txt[buf->idx++] = DLE;
buf->txt[buf->idx++] = ETX;
if (write(parse->generic->io.fd, buf->txt, (size_t)buf->idx) != buf->idx)
{
ERR(ERR_BADIO)
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: sendetx: failed to send cmd to clock: %m", parse->peer->procptr->refclkunit);
}
else
{
#ifdef DEBUG
if (debug > 2) /* SPECIAL DEBUG */
{
char buffer[256];
mkreadable(buffer, sizeof(buffer), (char *)buf->txt,
(unsigned long)buf->idx, 1);
printf("REFCLOCK: PARSE receiver #%d: transmitted message (%d bytes) >%s<\n",
parse->peer->procptr->refclkunit,
buf->idx, buffer);
}
#endif
clear_err(parse, ERR_BADIO);
}
}
void
sendflt(
struct txbuf *buf,
double a
)
{
int i;
union uval uval;
uval.fv = (float) a;
#ifdef WORDS_BIGENDIAN
for (i=0; i<=3; i++)
#else
for (i=3; i>=0; i--)
#endif
sendbyte(buf, uval.bd[i]);
}
#define TRIM_POS_OPT 0x13 /* output position with high precision */
#define TRIM_TIME_OPT 0x03 /* use UTC time stamps, on second */
/*--------------------------------------------------
* trimble TSIP setup routine
*/
static bool
trimbletsip_setup(
struct parseunit *parse,
const char *reason
)
{
uint8_t buffer[256];
struct txbuf buf;
trimble_t *t = parse->localdata;
if (t && t->last_reset &&
((t->last_reset + TRIMBLE_RESET_HOLDOFF) > current_time)) {
return true; /* not yet */
}
if (t)
t->last_reset = current_time;
buf.txt = buffer;
sendcmd(&buf, CMD_CVERSION); /* request software versions */
sendetx(&buf, parse);
sendcmd(&buf, CMD_COPERPARAM); /* set operating parameters */
sendbyte(&buf, 4); /* static */
sendflt(&buf, 5.0*D2R); /* elevation angle mask = 10 deg XXX */
sendflt(&buf, 4.0); /* s/n ratio mask = 6 XXX */
sendflt(&buf, 12.0); /* PDOP mask = 12 */
sendflt(&buf, 8.0); /* PDOP switch level = 8 */
sendetx(&buf, parse);
sendcmd(&buf, CMD_CMODESEL); /* fix mode select */
sendbyte(&buf, 1); /* time transfer mode */
sendetx(&buf, parse);
sendcmd(&buf, CMD_CMESSAGE); /* request system message */
sendetx(&buf, parse);
sendcmd(&buf, CMD_CSUPER); /* superpacket fix */
sendbyte(&buf, 0x2); /* binary mode */
sendetx(&buf, parse);
sendcmd(&buf, CMD_CIOOPTIONS); /* set I/O options */
sendbyte(&buf, TRIM_POS_OPT); /* position output */
sendbyte(&buf, 0x00); /* no velocity output */
sendbyte(&buf, TRIM_TIME_OPT); /* UTC, compute on seconds */
sendbyte(&buf, 0x00); /* no raw measurements */
sendetx(&buf, parse);
sendcmd(&buf, CMD_CUTCPARAM); /* request UTC correction data */
sendetx(&buf, parse);
NLOG(NLOG_CLOCKINFO)
ERR(ERR_BADIO)
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: trimbletsip_setup: RECEIVER RE-INITIALIZED (%s)", parse->peer->procptr->refclkunit, reason);
return false;
}
/*--------------------------------------------------
* TRIMBLE TSIP check routine
*/
static void
trimble_check(
struct peer *peer
)
{
struct parseunit *parse = peer->procptr->unitptr;
trimble_t *t = parse->localdata;
uint8_t buffer[256];
struct txbuf buf;
buf.txt = buffer;
if (t)
{
if (current_time > t->last_msg + TRIMBLETSIP_IDLE_TIME)
(void)trimbletsip_setup(parse, "message timeout");
}
poll_poll(parse->peer); /* emit query string and re-arm timer */
if (t && t->qtracking)
{
unsigned long oldsats = t->ltrack & ~t->ctrack;
t->qtracking = 0;
t->ltrack = t->ctrack;
if (oldsats)
{
int i;
for (i = 0; oldsats; i++) {
if (oldsats & (1U << i))
{
sendcmd(&buf, CMD_CSTATTRACK);
sendbyte(&buf, i+1); /* old sat */
sendetx(&buf, parse);
}
oldsats &= ~(1U << i);
}
}
sendcmd(&buf, CMD_CSTATTRACK);
sendbyte(&buf, 0x00); /* current tracking set */
sendetx(&buf, parse);
}
}
/*--------------------------------------------------
* TRIMBLE TSIP end routine
*/
static void
trimbletsip_end(
struct parseunit *parse
)
{ trimble_t *t = parse->localdata;
if (t)
{
free(t);
parse->localdata = NULL;
}
parse->peer->procptr->nextaction = 0;
parse->peer->procptr->action = NULL;
}
/*--------------------------------------------------
* TRIMBLE TSIP init routine
*/
static bool
trimbletsip_init(
struct parseunit *parse
)
{
#if defined(VEOL) || defined(VEOL2)
struct termios tio; /* NEEDED FOR A LONG TIME ! */
unsigned int type;
/*
* allocate local data area
*/
if (!parse->localdata)
{
trimble_t *t;
t = emalloc_zero(sizeof(trimble_t));
parse->localdata = t;
t->last_msg = current_time;
}
parse->peer->procptr->action = trimble_check;
parse->peer->procptr->nextaction = current_time;
/*
* configure terminal line for ICANON mode with VEOL characters
*/
if (TTY_GETATTR(parse->generic->io.fd, &tio) == -1)
{
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: trimbletsip_init: tcgetattr(%d, &tio): %m", parse->peer->procptr->refclkunit, parse->generic->io.fd);
return false;
}
else
{
type = (unsigned int)CLK_TYPE(parse->peer);
if ( (type != (unsigned int)~0) &&
(parse_clockinfo[type].cl_lflag & ICANON))
{
#ifdef VEOL
tio.c_cc[VEOL] = ETX;
#endif
#ifdef VEOL2
tio.c_cc[VEOL2] = DLE;
#endif
}
if (TTY_SETATTR(parse->generic->io.fd, &tio) == -1)
{
msyslog(LOG_ERR, "REFCLOCK: PARSE receiver #%d: trimbletsip_init: tcsetattr(%d, &tio): %m", parse->peer->procptr->refclkunit, parse->generic->io.fd);
return false;
}
}
#endif
return trimbletsip_setup(parse, "initial startup");
}
/*------------------------------------------------------------
* trimbletsip_event - handle Trimble events
* simple evente handler - attempt to re-initialize receiver
*/
static void
trimbletsip_event(
struct parseunit *parse,
int event
)
{
switch (event)
{
case CEVNT_BADREPLY: /* reset on garbled input */
case CEVNT_TIMEOUT: /* reset on no input */
(void)trimbletsip_setup(parse, "event BAD_REPLY/TIMEOUT");
break;
default: /* ignore */
break;
}
}
/*
* getflt, getint convert fields in the incoming data into the
* appropriate type of item
*
* CAVEAT: these routines are currently definitely byte order dependent
* and assume Representation(float) == IEEE754
* These functions MUST be converted to portable versions (especially
* converting the float representation into ntp_fp formats in order
* to avoid floating point operations at all!
*/
static double
getflt(
uint8_t *bp
)
{
union uval uval;
#ifdef WORDS_BIGENDIAN
uval.bd[0] = *bp++;
uval.bd[1] = *bp++;
uval.bd[2] = *bp++;
uval.bd[3] = *bp;
#else /* ! WORDS_BIGENDIAN */
uval.bd[3] = *bp++;
uval.bd[2] = *bp++;
uval.bd[1] = *bp++;
uval.bd[0] = *bp;
#endif /* ! WORDS_BIGENDIAN */
return (double)uval.fv;
}
static double
getdbl(
uint8_t *bp
)
{
union uval uval;
#ifdef WORDS_BIGENDIAN
uval.bd[0] = *bp++;
uval.bd[1] = *bp++;
uval.bd[2] = *bp++;
uval.bd[3] = *bp++;
uval.bd[4] = *bp++;
uval.bd[5] = *bp++;
uval.bd[6] = *bp++;
uval.bd[7] = *bp;
#else /* ! WORDS_BIGENDIAN */
uval.bd[7] = *bp++;
uval.bd[6] = *bp++;
uval.bd[5] = *bp++;
uval.bd[4] = *bp++;
uval.bd[3] = *bp++;
uval.bd[2] = *bp++;
uval.bd[1] = *bp++;
uval.bd[0] = *bp;
#endif /* ! WORDS_BIGENDIAN */
return uval.dv;
}
/*--------------------------------------------------
* trimbletsip_message - process trimble messages
*/
#define RTOD (180.0 / 3.1415926535898)
#define mb(_X_) (buffer[2+(_X_)]) /* shortcut for buffer access */
static void
trimbletsip_message(
struct parseunit *parse,
parsetime_t *parsetime
)
{
unsigned char *buffer = parsetime->parse_msg;
unsigned int size = parsetime->parse_msglen;
if ((size < 4) ||
(buffer[0] != DLE) ||
(buffer[size-1] != ETX) ||
(buffer[size-2] != DLE))
{
#ifdef DEBUG
if (debug > 2) { /* SPECIAL DEBUG */
size_t i;
printf("TRIMBLE BAD packet, size %u:\n ", size);
for (i = 0; i < size; i++) {
printf ("%2.2x, ", buffer[i]&0xff);
if (i%16 == 15) printf("\n\t");
}
printf("\n");
}
#endif
return;
}
else
{
unsigned short var_flag;
trimble_t *tr = parse->localdata;
unsigned int cmd = buffer[1];
char pbuffer[200];
char *t = pbuffer;
cmd_info_t *s;
#ifdef DEBUG
if (debug > 3) { /* SPECIAL DEBUG */
size_t i;
printf("TRIMBLE packet 0x%02x, size %u:\n ",
cmd, size);
for (i = 0; i < size; i++) {
printf ("%2.2x, ", buffer[i]&0xff);
if (i%16 == 15) printf("\n\t");
}
printf("\n");
}
#endif
if (tr)
tr->last_msg = current_time;
s = trimble_convert(cmd, trimble_rcmds);
if (s)
{
t = ap(pbuffer, sizeof(pbuffer), t, "%s=\"", s->varname);
}
else
{
DPRINT(1, ("TRIMBLE UNKNOWN COMMAND 0x%02x\n", cmd));
return;
}
var_flag = (unsigned short) s->varmode;
switch(cmd)
{
case CMD_RCURTIME:
t = ap(pbuffer, sizeof(pbuffer), t, "%f, %d, %f",
getflt((unsigned char *)&mb(0)),
getmsb_short(&mb(4)),
getflt((unsigned char *)&mb(6)));
break;
case CMD_RBEST4:
t = ap(pbuffer, sizeof(pbuffer), t, "mode: ");
switch (mb(0) & 0xF)
{
default:
t = ap(pbuffer, sizeof(pbuffer), t,
"0x%x", (unsigned)(mb(0) & 0x7));
break;
case 1:
t = ap(pbuffer, sizeof(pbuffer), t, "0D");
break;
case 3:
t = ap(pbuffer, sizeof(pbuffer), t, "2D");
break;
case 4:
t = ap(pbuffer, sizeof(pbuffer), t, "3D");
break;
}
if (mb(0) & 0x10)
t = ap(pbuffer, sizeof(pbuffer), t, "-MANUAL, ");
else
t = ap(pbuffer, sizeof(pbuffer), t, "-AUTO, ");
t = ap(pbuffer, sizeof(pbuffer), t, "satellites %02d %02d %02d %02d, PDOP %.2f, HDOP %.2f, VDOP %.2f, TDOP %.2f",
mb(1), mb(2), mb(3), mb(4),
getflt((unsigned char *)&mb(5)),
getflt((unsigned char *)&mb(9)),
getflt((unsigned char *)&mb(13)),
getflt((unsigned char *)&mb(17)));
break;
case CMD_RVERSION:
t = ap(pbuffer, sizeof(pbuffer), t, "%d.%d (%d/%d/%d)",
mb(0)&0xff, mb(1)&0xff, 1900+(mb(4)&0xff), mb(2)&0xff, mb(3)&0xff);
break;
case CMD_RRECVHEALTH:
{
static const char *msgs[] =
{
"Battery backup failed",
"Signal processor error",
"Alignment error, channel or chip 1",
"Alignment error, channel or chip 2",
"Antenna feed line fault",
"Excessive ref freq. error",
"<BIT 6>",
"<BIT 7>"
};
int i, bits;
switch (mb(0) & 0xFF)
{
default:
t = ap(pbuffer, sizeof(pbuffer), t, "illegal value 0x%02x", mb(0) & 0xFF);
break;
case 0x00:
t = ap(pbuffer, sizeof(pbuffer), t, "doing position fixes");
break;
case 0x01:
t = ap(pbuffer, sizeof(pbuffer), t, "no GPS time yet");
break;
case 0x03:
t = ap(pbuffer, sizeof(pbuffer), t, "PDOP too high");
break;
case 0x08:
t = ap(pbuffer, sizeof(pbuffer), t, "no usable satellites");
break;
case 0x09:
t = ap(pbuffer, sizeof(pbuffer), t, "only ONE usable satellite");
break;
case 0x0A:
t = ap(pbuffer, sizeof(pbuffer), t, "only TWO usable satellites");
break;
case 0x0B:
t = ap(pbuffer, sizeof(pbuffer), t, "only THREE usable satellites");
break;
case 0x0C:
t = ap(pbuffer, sizeof(pbuffer), t, "the chosen satellite is unusable");
break;
}
bits = mb(1) & 0xFF;
for (i = 0; i < 8; i++)
if (bits & (0x1<<i))
{
t = ap(pbuffer, sizeof(pbuffer), t, ", %s", msgs[i]);
}
}
break;
case CMD_RMESSAGE:
mkreadable(t, (int)BUFFER_SIZE(pbuffer, t),
(char *)&mb(0),
(unsigned)(size - 2u -
(unsigned)(&mb(0) - buffer)),
0);
break;
case CMD_RMACHSTAT:
{
static const char *msgs[] =
{
"Synthesizer Fault",
"Battery Powered Time Clock Fault",
"A-to-D Converter Fault",
"The almanac stored in the receiver is not complete and current",
"<BIT 4>",
"<BIT 5",
"<BIT 6>",
"<BIT 7>"
};
int i, bits;
t = ap(pbuffer, sizeof(pbuffer), t, "machine id 0x%02x", mb(0) & 0xFF);
bits = mb(1) & 0xFF;
for (i = 0; i < 8; i++)
if (bits & (0x1<<i))
{
t = ap(pbuffer, sizeof(pbuffer), t, ", %s", msgs[i]);
}
t = ap(pbuffer, sizeof(pbuffer), t, ", Superpackets %ssupported", (mb(2) & 0xFF) ? "" :"un" );
}
break;
case CMD_ROPERPARAM:
t = ap(pbuffer, sizeof(pbuffer), t, "%2x %.1f %.1f %.1f %.1f",
mb(0), getflt((unsigned char *)&mb(1)), getflt((unsigned char *)&mb(5)),
getflt((unsigned char *)&mb(9)), getflt((unsigned char *)&mb(13)));
break;
case CMD_RUTCPARAM:
{
float t0t = getflt((unsigned char *)&mb(14));
short wnt = getmsb_short(&mb(18));
short dtls = getmsb_short(&mb(12));
short wnlsf = getmsb_short(&mb(20));
short dn = getmsb_short(&mb(22));
short dtlsf = getmsb_short(&mb(24));
if ((int)t0t != 0)
{
mk_utcinfo(t, wnt, wnlsf, dn, dtls, dtlsf,
(int)BUFFER_SIZE(pbuffer, t));
}
else
{
t = ap(pbuffer, sizeof(pbuffer), t, "<NO UTC DATA>");
}
}
break;
case CMD_RSAT1BIAS:
t = ap(pbuffer, sizeof(pbuffer), t, "%.1fm %.2fm/s at %.1fs",
getflt(&mb(0)), getflt(&mb(4)), getflt(&mb(8)));
break;
case CMD_RIOOPTIONS:
{
t = ap(pbuffer, sizeof(pbuffer), t, "%02x %02x %02x %02x",
mb(0), mb(1), mb(2), mb(3));
if (mb(0) != TRIM_POS_OPT ||
mb(2) != TRIM_TIME_OPT)
{
(void)trimbletsip_setup(parse, "bad io options");
}
}
break;
case CMD_RSPOSXYZ:
{
double x = getflt((unsigned char *)&mb(0));
double y = getflt((unsigned char *)&mb(4));
double z = getflt((unsigned char *)&mb(8));
double f = getflt((unsigned char *)&mb(12));
if (f > 0.0)
t = ap(pbuffer, sizeof(pbuffer), t, "x= %.1fm, y= %.1fm, z= %.1fm, time_of_fix= %f sec",
x, y, z,
f);
else
return;
}
break;
case CMD_RSLLAPOS:
{
double lat = getflt((unsigned char *)&mb(0));
double lng = getflt((unsigned char *)&mb(4));
double f = getflt((unsigned char *)&mb(12));
if (f > 0.0)
t = ap(pbuffer, sizeof(pbuffer), t, "lat %f %c, long %f %c, alt %.2fm",
((lat < 0.0) ? (-lat) : (lat))*RTOD, (lat < 0.0 ? 'S' : 'N'),
((lng < 0.0) ? (-lng) : (lng))*RTOD, (lng < 0.0 ? 'W' : 'E'),
getflt((unsigned char *)&mb(8)));
else
return;
}
break;
case CMD_RDOUBLEXYZ:
{
double x = getdbl((unsigned char *)&mb(0));
double y = getdbl((unsigned char *)&mb(8));
double z = getdbl((unsigned char *)&mb(16));
t = ap(pbuffer, sizeof(pbuffer), t, "x= %.1fm, y= %.1fm, z= %.1fm",
x, y, z);
}
break;
case CMD_RDOUBLELLA:
{
double lat = getdbl((unsigned char *)&mb(0));
double lng = getdbl((unsigned char *)&mb(8));
t = ap(pbuffer, sizeof(pbuffer), t, "lat %f %c, lon %f %c, alt %.2fm",
((lat < 0.0) ? (-lat) : (lat))*RTOD, (lat < 0.0 ? 'S' : 'N'),
((lng < 0.0) ? (-lng) : (lng))*RTOD, (lng < 0.0 ? 'W' : 'E'),
getdbl((unsigned char *)&mb(16)));
}
break;
case CMD_RALLINVIEW:
{
int i, sats;
t = ap(pbuffer, sizeof(pbuffer), t, "mode: ");
switch (mb(0) & 0x7)
{
default:
t = ap(pbuffer, sizeof(pbuffer), t, "0x%x",
(unsigned)(mb(0) & 0x7));
break;
case 3:
t = ap(pbuffer, sizeof(pbuffer), t, "2D");
break;
case 4:
t = ap(pbuffer, sizeof(pbuffer), t, "3D");
break;
}
if (mb(0) & 0x8)
t = ap(pbuffer, sizeof(pbuffer), t, "-MANUAL, ");
else
t = ap(pbuffer, sizeof(pbuffer), t, "-AUTO, ");
sats = (mb(0)>>4) & 0xF;
t = ap(pbuffer, sizeof(pbuffer), t, "PDOP %.2f, HDOP %.2f, VDOP %.2f, TDOP %.2f, %d satellite%s in view: ",
getflt((unsigned char *)&mb(1)),
getflt((unsigned char *)&mb(5)),
getflt((unsigned char *)&mb(9)),
getflt((unsigned char *)&mb(13)),
sats, (sats == 1) ? "" : "s");
for (i=0; i < sats; i++)
{
t = ap(pbuffer, sizeof(pbuffer), t, "%s%02d", i ? ", " : "", mb(17+i));
if (tr)
tr->ctrack |= (1U << (mb(17+i)-1));
}
if (tr)
{ /* mark for tracking status query */
tr->qtracking = 1;
}
}
break;
case CMD_RSTATTRACK:
{
t = ap(pbuffer, sizeof(pbuffer), t-2, "[%02d]=\"", mb(0)); /* add index to var name */
if (getflt((unsigned char *)&mb(4)) < 0.0)
{
t = ap(pbuffer, sizeof(pbuffer), t, "<NO MEASUREMENTS>");
var_flag &= (unsigned short)(~DEF);
}
else
{
t = ap(pbuffer, sizeof(pbuffer), t, "ch=%d, acq=%s, eph=%d, signal_level= %5.2f, elevation= %5.2f, azimuth= %6.2f",
(mb(1) & 0xFF)>>3,
mb(2) ? ((mb(2) == 1) ? "ACQ" : "SRCH") : "NEVER",
mb(3),
getflt((unsigned char *)&mb(4)),
getflt((unsigned char *)&mb(12)) * RTOD,
getflt((unsigned char *)&mb(16)) * RTOD);
if (mb(20))
{
var_flag &= (unsigned short)(~DEF);
t = ap(pbuffer, sizeof(pbuffer), t, ", OLD");
}
if (mb(22))
{
if (mb(22) == 1)
t = ap(pbuffer, sizeof(pbuffer), t, ", BAD PARITY");
else
if (mb(22) == 2)
t = ap(pbuffer, sizeof(pbuffer), t, ", BAD EPH HEALTH");
}
if (mb(23))
t = ap(pbuffer, sizeof(pbuffer), t, ", collecting data");
}
}
break;
default:
t = ap(pbuffer, sizeof(pbuffer), t, "<UNDECODED>");
break;
}
ap(pbuffer, sizeof(pbuffer), t, "\"");
set_var(&parse->kv, pbuffer, sizeof(pbuffer), var_flag);
}
}
/**============================================================
** RAWDCF support
**/
/*--------------------------------------------------
* rawdcf_init_1 - set up modem lines for RAWDCF receivers
* SET DTR line
*/
#if defined(TIOCMSET) && (defined(TIOCM_DTR) || defined(CIOCM_DTR))
static bool
rawdcf_init_1(
struct parseunit *parse
)
{
/* fixed 2000 for using with Linux by Wolfram Pienkoss <wp@bszh.de> */
/*
* You can use the RS232 to supply the power for a DCF77 receiver.
* Here a voltage between the DTR and the RTS line is used. Unfortunately
* the name has changed from CIOCM_DTR to TIOCM_DTR recently.
*/
int sl232;
if (ioctl(parse->generic->io.fd, TIOCMGET, (void *)&sl232) == -1)
{
msyslog(LOG_NOTICE, "REFCLOCK: PARSE receiver #%d: rawdcf_init_1: WARNING: ioctl(fd, TIOCMGET, [C|T]IOCM_DTR): %m", parse->peer->procptr->refclkunit);
return 0;
}
#ifdef TIOCM_DTR
sl232 = (sl232 & ~TIOCM_RTS) | TIOCM_DTR; /* turn on DTR, clear RTS for power supply */
#else
sl232 = (sl232 & ~CIOCM_RTS) | CIOCM_DTR; /* turn on DTR, clear RTS for power supply */
#endif
if (ioctl(parse->generic->io.fd, TIOCMSET, (void *)&sl232) == -1)
{
msyslog(LOG_NOTICE, "REFCLOCK: PARSE receiver #%d: rawdcf_init_1: WARNING: ioctl(fd, TIOCMSET, [C|T]IOCM_DTR): %m", parse->peer->procptr->refclkunit);
}
return 0;
}
#else
static int
rawdcfdtr_init_1(
struct parseunit *parse
)
{
msyslog(LOG_NOTICE, "REFCLOCK: PARSE receiver #%d: rawdcf_init_1: WARNING: OS interface incapable of setting DTR to power DCF modules", parse->peer->procptr->refclkunit);
return 0;
}
#endif /* DTR initialisation type */
/*--------------------------------------------------
* rawdcf_init_2 - set up modem lines for RAWDCF receivers
* CLR DTR line, SET RTS line
*/
#if defined(TIOCMSET) && (defined(TIOCM_RTS) || defined(CIOCM_RTS))
static bool
rawdcf_init_2(
struct parseunit *parse
)
{
/* fixed 2000 for using with Linux by Wolfram Pienkoss <wp@bszh.de> */
/*
* You can use the RS232 to supply the power for a DCF77 receiver.
* Here a voltage between the DTR and the RTS line is used. Unfortunately
* the name has changed from CIOCM_DTR to TIOCM_DTR recently.
*/
int sl232;
if (ioctl(parse->generic->io.fd, TIOCMGET, (void *)&sl232) == -1)
{
msyslog(LOG_NOTICE, "REFCLOCK: PARSE receiver #%d: rawdcf_init_2: WARNING: ioctl(fd, TIOCMGET, [C|T]IOCM_RTS): %m", parse->peer->procptr->refclkunit);
return 0;
}
#ifdef TIOCM_RTS
sl232 = (sl232 & ~TIOCM_DTR) | TIOCM_RTS; /* turn on RTS, clear DTR for power supply */
#else
sl232 = (sl232 & ~CIOCM_DTR) | CIOCM_RTS; /* turn on RTS, clear DTR for power supply */
#endif
if (ioctl(parse->generic->io.fd, TIOCMSET, (void *)&sl232) == -1)
{
msyslog(LOG_NOTICE, "REFCLOCK: PARSE receiver #%d: rawdcf_init_2: WARNING: ioctl(fd, TIOCMSET, [C|T]IOCM_RTS): %m", parse->peer->procptr->refclkunit);
}
return 0;
}
#else
static int
rawdcf_init_2(
struct parseunit *parse
)
{
msyslog(LOG_NOTICE, "REFCLOCK: PARSE receiver #%d: rawdcf_init_2: WARNING: OS interface incapable of setting RTS to power DCF modules", parse->peer->procptr->refclkunit);
return 0;
}
#endif /* DTR initialisation type */
/*
* History:
*
* refclock_parse.c,v
* Revision 4.81 2009/05/01 10:15:29 kardel
* use new refclock_ppsapi interface
*
* Revision 4.80 2007/08/11 12:06:29 kardel
* update comments wrt/ to PPS
*
* Revision 4.79 2007/08/11 11:52:23 kardel
* - terminate io bindings before io_closeclock() will close our file descriptor
*
* Revision 4.78 2006/12/22 20:08:27 kardel
* Bug 746 (RFE): add configuration for Expert mouseCLOCK USB v2.0 as mode 19
*
* Revision 4.77 2006/08/05 07:44:49 kardel
* support optionally separate PPS devices via /dev/refclockpps-{0..3}
*
* Revision 4.76 2006/06/22 18:40:47 kardel
* clean up signedness (gcc 4)
*
* Revision 4.75 2006/06/22 16:58:10 kardel
* Bug #632: call parse_ppsapi() in parse_ctl() when updating
* the PPS offset. Fix sign of offset passed to kernel.
*
* Revision 4.74 2006/06/18 21:18:37 kardel
* NetBSD Coverity CID 3796: possible NULL deref
*
* Revision 4.73 2006/05/26 14:23:46 kardel
* cleanup of copyright info
*
* Revision 4.72 2006/05/26 14:19:43 kardel
* cleanup of ioctl cruft
*
* Revision 4.71 2006/05/26 14:15:57 kardel
* delay adding refclock to async refclock io after all initializations
*
* Revision 4.70 2006/05/25 18:20:50 kardel
* bug #619
* terminate parse io engine after de-registering
* from refclock io engine
*
* Revision 4.69 2006/05/25 17:28:02 kardel
* complete refclock io structure initialization *before* inserting it into the
* refclock input machine (avoids null pointer deref) (bug #619)
*
* Revision 4.68 2006/05/01 17:02:51 kardel
* copy receiver method also for newlwy created receive buffers
*
* Revision 4.67 2006/05/01 14:37:29 kardel
* If an input buffer parses into more than one message do insert the
* parsed message in a new input buffer instead of processing it
* directly. This avoids deed complicated processing in signal
* handling.
*
* Revision 4.66 2006/03/18 00:45:30 kardel
* coverity fixes found in NetBSD coverity scan
*
* Revision 4.65 2006/01/26 06:08:33 kardel
* output errno on PPS setup failure
*
* Revision 4.64 2005/11/09 20:44:47 kardel
* utilize full PPS timestamp resolution from PPS API
*
* Revision 4.63 2005/10/07 22:10:25 kardel
* bounded buffer implementation
*
* Revision 4.62.2.2 2005/09/25 10:20:16 kardel
* avoid unexpected buffer overflows due to sprintf("%f") on strange floats:
* replace almost all str* and *printf functions be their buffer bounded
* counterparts
*
* Revision 4.62.2.1 2005/08/27 16:19:27 kardel
* limit re-set rate of trimble clocks
*
* Revision 4.62 2005/08/06 17:40:00 kardel
* cleanup size handling wrt/ to buffer boundaries
*
* Revision 4.61 2005/07/27 21:16:19 kardel
* fix a long (> 11 years) misconfiguration wrt/ Meinberg cflag factory
* default setup. CSTOPB was missing for the 7E2 default data format of
* the DCF77 clocks.
*
* Revision 4.60 2005/07/17 21:14:44 kardel
* change contents of version string to include the RCS/CVS Id
*
* Revision 4.59 2005/07/06 06:56:38 kardel
* syntax error
*
* Revision 4.58 2005/07/04 13:10:40 kardel
* fix bug 455: tripping over NULL pointer on cleanup
* fix shadow storage logic for ppsphaseadjust and trustime wrt/ time2
* fix compiler warnings for some platforms wrt/ printf formatstrings and
* varying structure element sizes
* reorder assignment in binding to avoid tripping over NULL pointers
*
* Revision 4.57 2005/06/25 09:25:19 kardel
* sort out log output sequence
*
* Revision 4.56 2005/06/14 21:47:27 kardel
* collect samples only if samples are ok (sync or trusted flywheel)
* propagate pps phase adjustment value to kernel via PPSAPI to help HARDPPS
* en- and dis-able HARDPPS in correlation to receiver sync state
*
* Revision 4.55 2005/06/02 21:28:31 kardel
* clarify trust logic
*
* Revision 4.54 2005/06/02 17:06:49 kardel
* change status reporting to use fixed refclock_report()
*
* Revision 4.53 2005/06/02 16:33:31 kardel
* fix acceptance of clocks unsync clocks right at start
*
* Revision 4.52 2005/05/26 21:55:06 kardel
* cleanup status reporting
*
* Revision 4.51 2005/05/26 19:19:14 kardel
* implement fast refclock startup
*
* Revision 4.50 2005/04/16 20:51:35 kardel
* set hardpps_enable = 1 when binding a kernel PPS source
*
* Revision 4.49 2005/04/16 17:29:26 kardel
* add non polling clock type 18 for just listenning to Meinberg clocks
*
* Revision 4.48 2005/04/16 16:22:27 kardel
* bk sync 20050415 ntp-dev
*
* Revision 4.47 2004/11/29 10:42:48 kardel
* bk sync ntp-dev 20041129
*
* Revision 4.46 2004/11/29 10:26:29 kardel
* keep fudgetime2 in sync with trusttime/ppsphaseadjust depending in flag1
*
* Revision 4.45 2004/11/14 20:53:20 kardel
* clear PPS flags after using them
*
* Revision 4.44 2004/11/14 15:29:41 kardel
* support PPSAPI, upgrade Copyright to Berkeley style
*
* Revision 4.43 2001/05/26 22:53:16 kardel
* 20010526 reconciliation
*
* Revision 4.42 2000/05/14 15:31:51 kardel
* PPSAPI && RAWDCF modemline support
*
* Revision 4.41 2000/04/09 19:50:45 kardel
* fixed rawdcfdtr_init() -> rawdcf_init_1
*
* Revision 4.40 2000/04/09 15:27:55 kardel
* modem line fiddle in rawdcf_init_2
*
* Revision 4.39 2000/03/18 09:16:55 kardel
* PPSAPI integration
*
* Revision 4.38 2000/03/05 20:25:06 kardel
* support PPSAPI
*
* Revision 4.37 2000/03/05 20:11:14 kardel
* 4.0.99g reconciliation
*
* Revision 4.36 1999/11/28 17:18:20 kardel
* disabled burst mode
*
* Revision 4.35 1999/11/28 09:14:14 kardel
* RECON_4_0_98F
*
* Revision 4.34 1999/05/14 06:08:05 kardel
* store current_time in a suitable container (unsigned long)
*
* Revision 4.33 1999/05/13 21:48:38 kardel
* double the no response timeout interval
*
* Revision 4.32 1999/05/13 20:09:13 kardel
* complain only about missing polls after a full poll interval
*
* Revision 4.31 1999/05/13 19:59:32 kardel
* add clock type 16 for RTS set DTR clr in RAWDCF
*
* Revision 4.30 1999/02/28 20:36:43 kardel
* fixed printf fmt
*
* Revision 4.29 1999/02/28 19:58:23 kardel
* updated copyright information
*
* Revision 4.28 1999/02/28 19:01:50 kardel
* improved debug out on sent Meinberg messages
*
* Revision 4.27 1999/02/28 18:05:55 kardel
* no linux/ppsclock.h stuff
*
* Revision 4.26 1999/02/28 15:27:27 kardel
* wharton clock integration
*
* Revision 4.25 1999/02/28 14:04:46 kardel
* added missing double quotes to UTC information string
*
* Revision 4.24 1999/02/28 12:06:50 kardel
* (parse_control): using gmprettydate instead of prettydate()
* (mk_utcinfo): new function for formatting GPS derived UTC information
* (gps16x_message): changed to use mk_utcinfo()
* (trimbletsip_message): changed to use mk_utcinfo()
* ignoring position information in unsynchronized mode
* (parse_start): augument linux support for optional ASYNC_LOW_LATENCY
*
* Revision 4.23 1999/02/23 19:47:53 kardel
* fixed #endifs
* (stream_receive): fixed formats
*
* Revision 4.22 1999/02/22 06:21:02 kardel
* use new autoconfig symbols
*
* Revision 4.21 1999/02/21 12:18:13 kardel
* 4.91f reconciliation
*
* Revision 4.20 1999/02/21 10:53:36 kardel
* initial Linux PPSkit version
*
* Revision 4.19 1999/02/07 09:10:45 kardel
* clarify STREAMS mitigation rules in comment
*
* Revision 4.18 1998/12/20 23:45:34 kardel
* fix types and warnings
*
* Revision 4.17 1998/11/15 21:24:51 kardel
* cannot access mbg_ routines when CLOCK_MEINBERG
* is not defined
*
* Revision 4.16 1998/11/15 20:28:17 kardel
* Release 4.0.73e13 reconciliation
*
* Revision 4.15 1998/08/22 21:56:08 kardel
* fixed IO handling for non-STREAM IO
*
* Revision 4.14 1998/08/16 19:00:48 kardel
* (gps16x_message): reduced UTC parameter information (dropped A0,A1)
* made uval a local variable (killed one of the last globals)
* (sendetx): added logging of messages when in debug mode
* (trimble_check): added periodic checks to facilitate re-initialization
* (trimbletsip_init): made use of EOL character if in non-kernel operation
* (trimbletsip_message): extended message interpretation
* (getdbl): fixed data conversion
*
* Revision 4.13 1998/08/09 22:29:13 kardel
* Trimble TSIP support
*
* Revision 4.12 1998/07/11 10:05:34 kardel
* Release 4.0.73d reconciliation
*
* Revision 4.11 1998/06/14 21:09:42 kardel
* Sun acc cleanup
*
* Revision 4.10 1998/06/13 12:36:45 kardel
* signed/unsigned, name clashes
*
* Revision 4.9 1998/06/12 15:30:00 kardel
* prototype fixes
*
* Revision 4.8 1998/06/12 11:19:42 kardel
* added direct input processing routine for refclocks in
* order to avaiod that single character io gobbles up all
* receive buffers and drops input data. (Problem started
* with fast machines so a character a buffer was possible
* one of the few cases where faster machines break existing
* allocation algorithms)
*
* Revision 4.7 1998/06/06 18:35:20 kardel
* (parse_start): added BURST mode initialisation
*
* Revision 4.6 1998/05/27 06:12:46 kardel
* RAWDCF_BASEDELAY default added
* old comment removed
* casts for ioctl()
*
* Revision 4.5 1998/05/25 22:05:09 kardel
* RAWDCF_SETDTR option removed
* clock type 14 attempts to set DTR for
* power supply of RAWDCF receivers
*
* Revision 4.4 1998/05/24 16:20:47 kardel
* updated comments referencing Meinberg clocks
* added RAWDCF clock with DTR set option as type 14
*
* Revision 4.3 1998/05/24 10:48:33 kardel
* calibrated CONRAD RAWDCF default fudge factor
*
* Revision 4.2 1998/05/24 09:59:35 kardel
* corrected version information (ntpq support)
*
* Revision 4.1 1998/05/24 09:52:31 kardel
* use fixed format only (new IO model)
* output debug to stdout instead of msyslog()
* don't include >"< in ASCII output in order not to confuse
* ntpq parsing
*
* Revision 4.0 1998/04/10 19:52:11 kardel
* Start 4.0 release version numbering
*
* Revision 1.2 1998/04/10 19:28:04 kardel
* initial NTP VERSION 4 integration of PARSE with GPS166 binary support
* derived from 3.105.1.2 from V3 tree
*
* Revision information 3.1 - 3.105 from log deleted 1998/04/10 kardel
*
*/
|