File: refclock_parse.c

package info (click to toggle)
xntp3 5.93-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 7,644 kB
  • ctags: 7,419
  • sloc: ansic: 59,474; perl: 3,633; sh: 2,623; awk: 417; makefile: 311; asm: 37
file content (4334 lines) | stat: -rw-r--r-- 115,655 bytes parent folder | download | duplicates (2)
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
/*
 * /src/NTP/REPOSITORY/v4/xntpd/refclock_parse.c,v 3.103 1997/07/12 15:35:16 kardel Exp
 *
 * refclock_parse.c,v 3.103 1997/07/12 15:35:16 kardel Exp
 *
 * generic reference clock driver for receivers
 *
 * make use of a STREAMS module for input processing where
 * available and configured. Currently the STREAMS module
 * is only available for Suns running SunOS 4.x and SunOS5.x
 *
 * Copyright (c) 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 by Frank Kardel
 * Friedrich-Alexander Universitt Erlangen-Nrnberg, Germany
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#if defined(REFCLOCK) && defined(PARSE)

/*
 * Defines:
 *  REFCLOCK && (PARSE||PARSEPPS)
 *                    - enable this mess
 *  STREAM            - allow for STREAMS modules
 *                      ("parse", "ppsclocd", "ppsclock")
 *  PPS		      - use ppsclock module ioctl
 *
 * TTY defines:
 *  HAVE_BSD_TTYS     - currently unsupported
 *  HAVE_SYSV_TTYS    - will use termio.h
 *  HAVE_TERMIOS      - will use termios.h
 *  STREAM            - will use streams and implies HAVE_TERMIOS
 */

/*
 * This driver currently provides the support for
 *   - Meinberg DCF77 receiver DCF77 PZF 535 (TCXO version) (DCF)
 *   - Meinberg DCF77 receiver DCF77 PZF 535 (OCXO version) (DCF)
 *   - Meinberg DCF77 receiver DCF77 AM receivers           (DCF)
 *   - IGEL CLOCK                                           (DCF)
 *   - ELV DCF7000                                          (DCF)
 *   - Schmid clock                                         (DCF)
 *   - Conrad DCF77 receiver module                         (DCF)
 *   - FAU DCF77 NTP receiver (TimeBrick)                   (DCF)
 *
 *   - Meinberg GPS166                                      (GPS)
 *   - Trimble SV6 (TSIP and TAIP protocol)                 (GPS)
 *
 *   - RCC8000 MSF Receiver                                 (MSF)
 */

/*
 * Meinberg receivers are connected via a 9600 baud serial line
 *
 * Receivers that do NOT support:
 *          - leap second indication
 * 	DCF U/A 31
 *	DCF PZF535 (stock version)
 *
 * CORRECTION: Meinberg will include LEAP SECOND anounncements in the
 * telegram. This might not be true for all of their products, but
 * some of them will. So you should definitely ask for NTP and
 * LEAP SECOND announcements when ordering receivers from them.
 *
 * so...
 *          - for PZF535 please ask for revision PZFUERL4.6 or higher
 *            (support for leap second and alternate antenna)
 *
 *          - LEAP SECOND announcement / NTP support for the other
 *            receivers
 *
 * The Meinberg GPS receiver also has a special NTP time stamp
 * format. The firmware release is Uni-Erlangen. Only this
 * firmware release is supported by xntp3.
 *
 * Meinberg generic receiver setup:
 *	output time code every second
 *	Baud rate 9600 7E2S
 */

#include "ntpd.h"
#include "ntp_refclock.h"
#include "ntp_unixtime.h"	/* includes <sys/time.h> */
#include "ntp_control.h"

#include <stdio.h>
#include <ctype.h>
#ifndef TM_IN_SYS_TIME
# include <time.h>
#endif

#include <sys/errno.h>
extern int errno;

#if !defined(STREAM) && !defined(HAVE_SYSV_TTYS) && !defined(HAVE_BSD_TTYS) && !defined(HAVE_TERMIOS)
# include "Bletch:  Define one of {STREAM,HAVE_SYSV_TTYS,HAVE_TERMIOS}"
#endif

#ifdef STREAM
# include <sys/stream.h>
# include <sys/stropts.h>
# ifndef HAVE_TERMIOS
#  define HAVE_TERMIOS
# endif
#endif

#ifdef HAVE_TERMIOS
# include <termios.h>
# define TTY_GETATTR(_FD_, _ARG_) tcgetattr((_FD_), (_ARG_))
# define TTY_SETATTR(_FD_, _ARG_) tcsetattr((_FD_), TCSANOW, (_ARG_))
# undef HAVE_SYSV_TTYS
#endif

#ifdef HAVE_SYSV_TTYS
# include <termio.h>
# define TTY_GETATTR(_FD_, _ARG_) ioctl((_FD_), TCGETA, (_ARG_))
# define TTY_SETATTR(_FD_, _ARG_) ioctl((_FD_), TCSETAW, (_ARG_))
#endif

#ifdef HAVE_BSD_TTYS
/* #error CURRENTLY NO BSD TTY SUPPORT */
# include "Bletch: BSD TTY not currently supported"
#endif

#if	!defined(O_RDWR)	/* XXX SOLARIS */
# include <fcntl.h>
#endif	/* !def(O_RDWR) */

#ifdef PPS
# include <sys/ppsclock.h>
#endif

#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif

#include "ntp_io.h"
#include "ntp_select.h"
#include "ntp_stdlib.h"

#include "parse.h"

#if !defined(NO_SCCSID) && !defined(lint) && !defined(__GNUC__)
static char rcsid[]="refclock_parse.c,v 3.103 1997/07/12 15:35:16 kardel Exp";
#endif

/**===========================================================================
 ** external interface to xntp mechanism
 **/

static	void	parse_init	P((void));
static	int	parse_start	P((int, struct peer *));
static	void	parse_shutdown	P((int, struct peer *));
static	void	parse_poll	P((int, struct peer *));
static	void	parse_control	P((int, struct refclockstat *, struct refclockstat *));

#define	parse_buginfo	noentry

struct	refclock refclock_parse = {
	parse_start,
	parse_shutdown,
	parse_poll,
	parse_control,
	parse_init,
	parse_buginfo,
	NOFLAGS
};

/*
 * the unit field selects for one the prototype to be used (lower 4 bits)
 * and for the other the clock type in case of different but similar
 * receivers (bits 4-6)
 * the most significant bit encodes PPS support
 * when the most significant bit is set the pps telegrams will be used
 * for controlling the local clock (ntp_loopfilter.c)
 * receiver specific configration data is kept in the parse_clockinfo field.
 */

/*
 * Definitions
 */
#define	MAXUNITS	4	/* maximum number of "PARSE" units permitted */
#define PARSEDEVICE	"/dev/refclock-%d" /* device to open %d is unit number */

/**===========================================================================
 ** function vector for dynamically binding io handling mechanism
 **/

typedef struct bind
{
  char   *bd_description;	/* name of type of binding */
  int	(*bd_init)();		/* initialize */
  void	(*bd_end)();		/* end */
  int   (*bd_setcs)();		/* set character size */
  int	(*bd_disable)();	/* disable */
  int	(*bd_enable)();		/* enable */
  int	(*bd_getfmt)();		/* get format */
  int	(*bd_setfmt)();		/* setfmt */
  int	(*bd_timecode)();	/* get time code */
  void	(*bd_receive)();	/* receive operation */
  void	(*bd_poll)();		/* poll 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_ENABLE(_X_)		(*(_X_)->binding->bd_enable)(_X_)
#define PARSE_DISABLE(_X_)		(*(_X_)->binding->bd_disable)(_X_)
#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_)
#define PARSE_POLL(_X_)			(*(_X_)->binding->bd_poll)(_X_)

/*
 * io modes
 */
#define PARSE_F_NOPOLLONLY	0x0001 /* always do async io (possible PPS support via PARSE) */
#define PARSE_F_POLLONLY	0x0002 /* never do async io  (no PPS support via PARSE) */
#define PARSE_F_PPSPPS		0x0004 /* use loopfilter PPS code (CIOGETEV) */
#define PARSE_F_PPSONSECOND	0x0008 /* PPS pulses are on second */


/**===========================================================================
 ** 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	       ~0	/* "all" errors */
#define ERR_BADDATA	0	/* unusable input data/conversion errors */
#define ERR_NODATA	1	/* no input data */
#define ERR_BADIO	2	/* read/write/select errors */
#define ERR_BADSTATUS	3	/* unsync states */
#define ERR_BADEVENT	4	/* non nominal events */
#define ERR_INTERNAL	5	/* internal error */
#define ERR_CNT		(ERR_INTERNAL+1)

#define ERR(_X_)	if (list_err(parse, (_X_)))

struct errorregression
{
  u_long err_count;	/* number of repititions per class */
  u_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,    3600 },		/* output next 3 messages in hour intervals */
  { 0, 12*3600 }		/* 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,    3600 },		/* output next 3 messages in hour intervals */
  { 0, 12*3600 }		/* 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,    3600 },		/* output next 3 messages in hour intervals */
  { 0, 12*3600 }		/* 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,    3600 },		/* output next 3 messages in hour intervals */
  { 0, 12*3600 }		/* 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,    3600 },		/* output next 3 messages in hour intervals */
  { 0, 12*3600 }		/* 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
{
  u_long err_started;	/* begin time (xntp) of error condition */
  u_long err_last;	/* last time (xntp) error occurred */
  u_long err_cnt;	/* number of error repititions */
  u_long err_suppressed;	/* number of suppressed messages */
  struct errorregression *err_stage; /* current error stage */
};

/**===========================================================================
 ** refclock instance data
 **/

struct parseunit
{
  /*
   * XNTP 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
   */
  u_char	      flags;	        /* flags (leap_control) */
  u_long	      lastchange;       /* time (xntp) when last state change accured */
  u_long	      statetime[CEVNT_MAX+1]; /* accumulated time of clock states */
  struct event        stattimer;        /* statistics timer */

  u_char              pollonly;		/* 1 for polling only (no PPS mode) */
  u_char              pollneeddata; 	/* 1 for receive sample expected in PPS mode */
  u_long              laststatus;       /* last packet status (error indication) */
  u_short	      lastformat;       /* last format used */
  u_long              lastsync;		/* time (xntp) when clock was last seen fully synchronized */
  u_long              lastmissed;       /* time (xntp) when poll didn't get data (powerup heuristic) */
  u_long              ppsserial;        /* magic cookie for ppsclock serials (avoids stale ppsclock data) */
  parsetime_t         time;		/* last (parse module) data */
  void               *localdata;        /* optional local data */
  struct errorinfo    errors[ERR_CNT];  /* error state table for suppressing excessive error messages */
};


/**===========================================================================
 ** Clockinfo section all parameter for specific clock types
 ** includes NTP parameters, TTY parameters and IO handling parameters
 **/

static	void	poll_dpoll	P((struct parseunit *));
static	void	poll_poll	P((struct parseunit *));
static	int	poll_init	P((struct parseunit *));
static	void	poll_end	P((struct parseunit *));

typedef struct poll_info
{
  u_long rate;			/* poll rate - once every "rate" seconds - 0 off */
  char * string;		/* string to send for polling */
  u_long count;			/* number of charcters in string */
} poll_info_t;

#define NO_CL_FLAGS	0
#define NO_POLL		(void (*)())0
#define NO_INIT		(int  (*)())0
#define NO_END		(void (*)())0
#define NO_EVENT	(void (*)())0
#define NO_DATA		(void *)0
#define NO_FORMAT	""
#define NO_PPSDELAY     0

#define DCF_ID		"DCF"	/* generic DCF */
#define DCF_A_ID	"DCFa"	/* AM demodulation */
#define DCF_P_ID	"DCFp"	/* psuedo random phase shift */
#define GPS_ID		"GPS"	/* GPS receiver */

#define	NOCLOCK_ROOTDELAY	0x00000000
#define	NOCLOCK_BASEDELAY	0x00000000
#define	NOCLOCK_DESCRIPTION	((char *)0)
#define NOCLOCK_MAXUNSYNC       0
#define NOCLOCK_CFLAG           0
#define NOCLOCK_IFLAG           0
#define NOCLOCK_OFLAG           0
#define NOCLOCK_LFLAG           0
#define NOCLOCK_ID		"TILT"
#define NOCLOCK_POLL		NO_POLL
#define NOCLOCK_INIT		NO_INIT
#define NOCLOCK_END		NO_END
#define NOCLOCK_DATA		NO_DATA
#define NOCLOCK_FORMAT		NO_FORMAT
#define NOCLOCK_TYPE		CTL_SST_TS_UNSPEC
#define NOCLOCK_SAMPLES		0
#define NOCLOCK_KEEP		0 

#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)
#define MBG_IFLAG		(IGNBRK|IGNPAR|ISTRIP)
#define MBG_OFLAG		0
#define MBG_LFLAG		0
#define MBG_FLAGS               PARSE_F_NOPOLLONLY

/*
 * Meinberg DCF77 receivers
 */
#define	DCFUA31_ROOTDELAY	0x00000000  /* 0 */
#define	DCFUA31_BASEDELAY	0x02C00000  /* 10.7421875ms: 10 ms (+/- 3 ms) */
#define	DCFUA31_DESCRIPTION	"Meinberg DCF77 UA31/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

/*
 * Meinberg DCF PZF535/TCXO (FM/PZF) receiver
 */
#define	DCFPZF535_ROOTDELAY	0x00000000
#define	DCFPZF535_BASEDELAY	0x00800000  /* 1.968ms +- 104us (oscilloscope) - relative to start (end of STX) */
#define	DCFPZF535_DESCRIPTION	"Meinberg DCF PZF 535/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


/*
 * Meinberg DCF PZF535/OCXO receiver
 */
#define	DCFPZF535OCXO_ROOTDELAY	0x00000000
#define	DCFPZF535OCXO_BASEDELAY	0x00800000 /* 1.968ms +- 104us (oscilloscope) - relative to start (end of STX) */
#define	DCFPZF535OCXO_DESCRIPTION "Meinberg DCF PZF 535/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		   32
#define DCFPZF535OCXO_KEEP	           20

/*
 * Meinberg GPS166 receiver
 */
#define	GPS166_ROOTDELAY	0x00000000         /* nothing here */
#define	GPS166_BASEDELAY	0x00800000         /* XXX to be fixed ! 1.968ms +- 104us (oscilloscope) - relative to start (end of STX) */
#define	GPS166_DESCRIPTION      "Meinberg GPS166 receiver"
#define GPS166_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 GPS166_SPEED		MBG_SPEED
#define GPS166_CFLAG            MBG_CFLAG
#define GPS166_IFLAG            MBG_IFLAG
#define GPS166_OFLAG            MBG_OFLAG
#define GPS166_LFLAG            MBG_LFLAG
#define GPS166_POLL		NO_POLL
#define GPS166_INIT		NO_INIT
#define GPS166_END		NO_END
#define GPS166_DATA		NO_DATA
#define GPS166_ID		GPS_ID
#define GPS166_FORMAT		NO_FORMAT
#define GPS166_SAMPLES		32
#define GPS166_KEEP		20

/*
 * ELV DCF7000 Wallclock-Receiver/Switching Clock (Kit)
 *
 * This is really not the hottest clock - but before you have nothing ...
 */
#define DCF7000_ROOTDELAY	0x00000000 /* 0 */
#define DCF7000_BASEDELAY	0x67AE0000 /* 405 ms - slow blow */
#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		6
#define DCF7000_KEEP		4

/*
 * 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 u_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		poll_end
#define WSDCF_DATA		((void *)(&wsdcf_pollinfo))
#define	WSDCF_ROOTDELAY		0X00000000	/* 0 */
#define	WSDCF_BASEDELAY	 	0x028F5C29	/*  ~  10ms */
#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		6
#define WSDCF_KEEP		4

/*
 * RAW DCF77 - input of DCF marks via RS232 - many variants
 */
#define RAWDCF_FLAGS		PARSE_F_NOPOLLONLY
#define RAWDCF_ROOTDELAY	0x00000000 /* 0 */
#define RAWDCF_FORMAT		"RAW DCF77 Timecode"
#define RAWDCF_MAXUNSYNC	(0) /* sorry - its a true receiver - no signal - no time */
#define RAWDCF_SPEED		(B50)
#ifdef NO_PARENB_IGNPAR /* Was: defined(SYS_IRIX4) || defined(SYS_IRIX5) */
/* somehow doesn't grok PARENB & IGNPAR (mj) */
# define RAWDCF_CFLAG            (CS8|CREAD|CLOCAL)
#else
# define RAWDCF_CFLAG            (CS8|CREAD|CLOCAL|PARENB)
#endif
#ifdef RAWDCF_NO_IGNPAR /* Was: defined(SYS_LINUX) && defined(CLOCK_RAWDCF) */
# define RAWDCF_IFLAG		0
#else
# define RAWDCF_IFLAG		(IGNPAR)
#endif
#define RAWDCF_OFLAG		0
#define RAWDCF_LFLAG		0
#define RAWDCF_SAMPLES		6
#define RAWDCF_KEEP		4
#if defined(RAWDCF_SETDTR)
static	int	rawdcf_init	P((struct parseunit *));
#define RAWDCF_INIT		rawdcf_init
#else
#define RAWDCF_INIT		NO_INIT
#endif

/*
 * 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	0x420C49B0 /* ~258 ms - Conrad receiver @ 50 Baud on a Sun */
#define CONRAD_DESCRIPTION	"RAW DCF77 CODE (Conrad DCF77 receiver module)"

/*
 * TimeBrick receiver
 */
#define TIMEBRICK_BASEDELAY	0x35C29000 /* ~210 ms - TimeBrick @ 50 Baud on a Sun */
#define TIMEBRICK_DESCRIPTION	"RAW DCF77 CODE (TimeBrick)"

/*
 * IGEL:clock receiver
 */
#define IGELCLOCK_BASEDELAY	0x420C49B0 /* ~258 ms - IGEL:clock receiver */
#define IGELCLOCK_DESCRIPTION	"RAW DCF77 CODE (IGEL:clock)"
#define IGELCLOCK_SPEED		(B1200)
#define IGELCLOCK_CFLAG		(CS8|CREAD|HUPCL|CLOCAL)

/*
 * Trimble SV6 GPS receivers (TAIP and TSIP protocols)
 */
#define ETX	0x03
#define DLE	0x10

#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	int	trimbletaip_init	P((struct parseunit *));
static	void	trimbletaip_event	P((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	int	trimbletsip_init	P((struct parseunit *));

#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           (0)

#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|PARSE_F_NOPOLLONLY)

#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 TRIMBLETAIP_END		    poll_end
#define TRIMBLETSIP_END		    poll_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	    NO_FORMAT
#define TRIMBLETSIP_FORMAT	    "Trimble SV6/TSIP"

#define TRIMBLETAIP_ROOTDELAY        0x0
#define TRIMBLETSIP_ROOTDELAY        0x0

#define TRIMBLETAIP_BASEDELAY        0x0
#define TRIMBLETSIP_BASEDELAY        0x51EB852	/* 20 ms as a l_uf - avg GPS time message latency */

#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_FLAGS
#define RCC8000_POLL            poll_dpoll
#define RCC8000_INIT            poll_init
#define RCC8000_END             poll_end
#define RCC8000_DATA            ((void *)(&rcc8000_pollinfo))
#define RCC8000_ROOTDELAY       0x0
#define RCC8000_BASEDELAY       0x0
#define RCC8000_ID              "MSF"
#define RCC8000_DESCRIPTION     "RCC 8000 MSF Receiver"
#define RCC8000_FORMAT          NO_FORMAT
#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	0x00000000 /* 0 */
#define HOPF6021_BASEDELAY	0x00000000 /* 0 */
#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          PARSE_F_NOPOLLONLY
#define HOPF6021_SAMPLES        5
#define HOPF6021_KEEP	        3

/*
 * Diem's Computime Radio Clock Receiver
 */
#define COMPUTIME_FLAGS       PARSE_F_NOPOLLONLY
#define COMPUTIME_ROOTDELAY   0x00000000  /* 0 */
#define COMPUTIME_BASEDELAY   0x00000000  /* 0 */
#define COMPUTIME_ID          DCF_ID
#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

static struct parse_clockinfo
{
  u_long  cl_flags;		/* operation flags (io modes) */
  void  (*cl_poll)();		/* active poll routine */
  int   (*cl_init)();		/* active poll init routine */
  void  (*cl_event)();		/* special event handling (e.g. reset clock) */
  void  (*cl_end)();		/* active poll end routine */
  void   *cl_data;		/* local data area for "poll" mechanism */
  u_fp    cl_rootdelay;		/* rootdelay */
  u_long  cl_basedelay;		/* current offset - unsigned l_fp fractional part */
  u_long  cl_ppsdelay;		/* current PPS offset - unsigned l_fp fractional part */
  char   *cl_id;		/* ID code */
  char   *cl_description;	/* device name */
  char   *cl_format;		/* fixed format */
  u_char  cl_type;		/* clock type (ntp control) */
  u_long  cl_maxunsync;		/* time to trust oscillator after loosing synch */
  u_long  cl_speed;		/* terminal input & output baudrate */
  u_long  cl_cflag;             /* terminal control flags */
  u_long  cl_iflag;             /* terminal input flags */
  u_long  cl_oflag;             /* terminal output flags */
  u_long  cl_lflag;             /* terminal local flags */
  u_long  cl_samples;		/* samples for median filter */
  u_long  cl_keep;		/* samples for median filter to keep */
} parse_clockinfo[] =
{
  {				/* mode 0 */
    MBG_FLAGS,
    NO_POLL,
    NO_INIT,
    NO_EVENT,
    NO_END,
    NO_DATA,
    DCFPZF535_ROOTDELAY,
    DCFPZF535_BASEDELAY,
    NO_PPSDELAY,
    DCF_P_ID,
    DCFPZF535_DESCRIPTION,
    NO_FORMAT,
    DCF_TYPE,
    DCFPZF535_MAXUNSYNC,
    DCFPZF535_SPEED,
    DCFPZF535_CFLAG,
    DCFPZF535_IFLAG,
    DCFPZF535_OFLAG,
    DCFPZF535_LFLAG,
    DCFPZF535_SAMPLES,
    DCFPZF535_KEEP
  },
  {				/* mode 1 */

    MBG_FLAGS,
    NO_POLL,
    NO_INIT,
    NO_EVENT,
    NO_END,
    NO_DATA,
    DCFPZF535OCXO_ROOTDELAY,
    DCFPZF535OCXO_BASEDELAY,
    NO_PPSDELAY,
    DCF_P_ID,
    DCFPZF535OCXO_DESCRIPTION,
    NO_FORMAT,
    DCF_TYPE,
    DCFPZF535OCXO_MAXUNSYNC,
    DCFPZF535OCXO_SPEED,
    DCFPZF535OCXO_CFLAG,
    DCFPZF535OCXO_IFLAG,
    DCFPZF535OCXO_OFLAG,
    DCFPZF535OCXO_LFLAG,
    DCFPZF535OCXO_SAMPLES,
    DCFPZF535OCXO_KEEP
  },
  {				/* mode 2 */
    MBG_FLAGS,
    NO_POLL,
    NO_INIT,
    NO_EVENT,
    NO_END,
    NO_DATA,
    DCFUA31_ROOTDELAY,
    DCFUA31_BASEDELAY,
    NO_PPSDELAY,
    DCF_A_ID,
    DCFUA31_DESCRIPTION,
    NO_FORMAT,
    DCF_TYPE,
    DCFUA31_MAXUNSYNC,
    DCFUA31_SPEED,
    DCFUA31_CFLAG,
    DCFUA31_IFLAG,
    DCFUA31_OFLAG,
    DCFUA31_LFLAG,
    DCFUA31_SAMPLES,
    DCFUA31_KEEP
  },
  {				/* mode 3 */
    MBG_FLAGS,
    NO_POLL,
    NO_INIT,
    NO_EVENT,
    NO_END,
    NO_DATA,
    DCF7000_ROOTDELAY,
    DCF7000_BASEDELAY,
    NO_PPSDELAY,
    DCF_A_ID,
    DCF7000_DESCRIPTION,
    NO_FORMAT,
    DCF_TYPE,
    DCF7000_MAXUNSYNC,
    DCF7000_SPEED,
    DCF7000_CFLAG,
    DCF7000_IFLAG,
    DCF7000_OFLAG,
    DCF7000_LFLAG,
    DCF7000_SAMPLES,
    DCF7000_KEEP
  },
  {				/* mode 4 */
    NO_CL_FLAGS,
    WSDCF_POLL,
    WSDCF_INIT,
    NO_EVENT,
    WSDCF_END,
    WSDCF_DATA,
    WSDCF_ROOTDELAY,
    WSDCF_BASEDELAY,
    NO_PPSDELAY,
    DCF_A_ID,
    WSDCF_DESCRIPTION,
    WSDCF_FORMAT,
    DCF_TYPE,
    WSDCF_MAXUNSYNC,
    WSDCF_SPEED,
    WSDCF_CFLAG,
    WSDCF_IFLAG,
    WSDCF_OFLAG,
    WSDCF_LFLAG,
    WSDCF_SAMPLES,
    WSDCF_KEEP
  },
  {				/* mode 5 */
    RAWDCF_FLAGS,
    NO_POLL,
    RAWDCF_INIT,
    NO_EVENT,
    NO_END,
    NO_DATA,
    RAWDCF_ROOTDELAY,
    CONRAD_BASEDELAY,
    NO_PPSDELAY,
    DCF_A_ID,
    CONRAD_DESCRIPTION,
    RAWDCF_FORMAT,
    DCF_TYPE,
    RAWDCF_MAXUNSYNC,
    RAWDCF_SPEED,
    RAWDCF_CFLAG,
    RAWDCF_IFLAG,
    RAWDCF_OFLAG,
    RAWDCF_LFLAG,
    RAWDCF_SAMPLES,
    RAWDCF_KEEP
  },
  {				/* mode 6 */
    RAWDCF_FLAGS,
    NO_POLL,
    RAWDCF_INIT,
    NO_EVENT,
    NO_END,
    NO_DATA,
    RAWDCF_ROOTDELAY,
    TIMEBRICK_BASEDELAY,
    NO_PPSDELAY,
    DCF_A_ID,
    TIMEBRICK_DESCRIPTION,
    RAWDCF_FORMAT,
    DCF_TYPE,
    RAWDCF_MAXUNSYNC,
    RAWDCF_SPEED,
    RAWDCF_CFLAG,
    RAWDCF_IFLAG,
    RAWDCF_OFLAG,
    RAWDCF_LFLAG,
    RAWDCF_SAMPLES,
    RAWDCF_KEEP
  },
  {				/* mode 7 */
    MBG_FLAGS,
    GPS166_POLL,
    GPS166_INIT,
    NO_EVENT,
    GPS166_END,
    GPS166_DATA,
    GPS166_ROOTDELAY,
    GPS166_BASEDELAY,
    NO_PPSDELAY,
    GPS166_ID,
    GPS166_DESCRIPTION,
    GPS166_FORMAT,
    GPS_TYPE,
    GPS166_MAXUNSYNC,
    GPS166_SPEED,
    GPS166_CFLAG,
    GPS166_IFLAG,
    GPS166_OFLAG,
    GPS166_LFLAG,
    GPS166_SAMPLES,
    GPS166_KEEP
  },
  {				/* mode 8 */
    RAWDCF_FLAGS,
    NO_POLL,
    NO_INIT,
    NO_EVENT,
    NO_END,
    NO_DATA,
    RAWDCF_ROOTDELAY,
    IGELCLOCK_BASEDELAY,
    NO_PPSDELAY,
    DCF_A_ID,
    IGELCLOCK_DESCRIPTION,
    RAWDCF_FORMAT,
    DCF_TYPE,
    RAWDCF_MAXUNSYNC,
    IGELCLOCK_SPEED,
    IGELCLOCK_CFLAG,
    RAWDCF_IFLAG,
    RAWDCF_OFLAG,
    RAWDCF_LFLAG,
    RAWDCF_SAMPLES,
    RAWDCF_KEEP
  },
  {				/* mode 9 */
    TRIMBLETAIP_FLAGS,
#if TRIM_POLLRATE		/* DHD940515: Allow user config */
    NO_POLL,
#else
    TRIMBLETAIP_POLL,
#endif
    TRIMBLETAIP_INIT,
    TRIMBLETAIP_EVENT,
    TRIMBLETAIP_END,
    TRIMBLETAIP_DATA,
    TRIMBLETAIP_ROOTDELAY,
    TRIMBLETAIP_BASEDELAY,
    NO_PPSDELAY,
    TRIMBLETAIP_ID,
    TRIMBLETAIP_DESCRIPTION,
    TRIMBLETAIP_FORMAT,
    GPS_TYPE,
    TRIMBLETAIP_MAXUNSYNC,
    TRIMBLETAIP_SPEED,
    TRIMBLETAIP_CFLAG,
    TRIMBLETAIP_IFLAG,
    TRIMBLETAIP_OFLAG,
    TRIMBLETAIP_LFLAG,
    TRIMBLETAIP_SAMPLES,
    TRIMBLETAIP_KEEP
  },
  {				/* mode 10 */
    TRIMBLETSIP_FLAGS,
#if TRIM_POLLRATE		/* DHD940515: Allow user config */
    NO_POLL,
#else
    TRIMBLETSIP_POLL,
#endif
    TRIMBLETSIP_INIT,
    NO_EVENT,
    TRIMBLETSIP_END,
    TRIMBLETSIP_DATA,
    TRIMBLETSIP_ROOTDELAY,
    TRIMBLETSIP_BASEDELAY,
    NO_PPSDELAY,
    TRIMBLETSIP_ID,
    TRIMBLETSIP_DESCRIPTION,
    TRIMBLETSIP_FORMAT,
    GPS_TYPE,
    TRIMBLETSIP_MAXUNSYNC,
    TRIMBLETSIP_SPEED,
    TRIMBLETSIP_CFLAG,
    TRIMBLETSIP_IFLAG,
    TRIMBLETSIP_OFLAG,
    TRIMBLETSIP_LFLAG,
    TRIMBLETSIP_SAMPLES,
    TRIMBLETSIP_KEEP
  },
  {                             /* mode 11 */
    NO_CL_FLAGS,
    RCC8000_POLL,
    RCC8000_INIT,
    NO_EVENT,
    RCC8000_END,
    RCC8000_DATA,
    RCC8000_ROOTDELAY,
    RCC8000_BASEDELAY,
    NO_PPSDELAY,
    RCC8000_ID,
    RCC8000_DESCRIPTION,
    RCC8000_FORMAT,
    DCF_TYPE,
    RCC8000_MAXUNSYNC,
    RCC8000_SPEED,
    RCC8000_CFLAG,
    RCC8000_IFLAG,
    RCC8000_OFLAG,
    RCC8000_LFLAG,
    RCC8000_SAMPLES,
    RCC8000_KEEP
  },
  {                             /* mode 12 */
    HOPF6021_FLAGS,
    NO_POLL,     
    NO_INIT,
    NO_EVENT,
    NO_END,
    NO_DATA,
    HOPF6021_ROOTDELAY,
    HOPF6021_BASEDELAY,
    NO_PPSDELAY,
    DCF_ID,
    HOPF6021_DESCRIPTION,
    HOPF6021_FORMAT,
    DCF_TYPE,
    HOPF6021_MAXUNSYNC,
    HOPF6021_SPEED,
    HOPF6021_CFLAG,
    HOPF6021_IFLAG,
    HOPF6021_OFLAG,
    HOPF6021_LFLAG,
    HOPF6021_SAMPLES,
    HOPF6021_KEEP
  },
  {                            /* mode 13 */
    COMPUTIME_FLAGS,
    NO_POLL,
    NO_INIT,
    NO_EVENT,
    NO_END,
    NO_DATA,
    COMPUTIME_ROOTDELAY,
    COMPUTIME_BASEDELAY,
    NO_PPSDELAY,
    COMPUTIME_ID,
    COMPUTIME_DESCRIPTION,
    COMPUTIME_FORMAT,
    COMPUTIME_TYPE,
    COMPUTIME_MAXUNSYNC,
    COMPUTIME_SPEED,
    COMPUTIME_CFLAG,
    COMPUTIME_IFLAG,
    COMPUTIME_OFLAG,
    COMPUTIME_LFLAG,
    COMPUTIME_SAMPLES,
    COMPUTIME_KEEP
  }
};

static int ncltypes = sizeof(parse_clockinfo) / sizeof(struct parse_clockinfo);

#define CLK_REALTYPE(x) ((int)(((x)->ttl) & 0x7F))
#define CLK_TYPE(x)	((CLK_REALTYPE(x) >= ncltypes) ? ~0 : CLK_REALTYPE(x))
#define CLK_UNIT(x)	(REFCLOCKUNIT(&(x)->srcadr))
#define CLK_PPS(x)	(((x)->ttl) & 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 struct parseunit *parseunits[MAXUNITS];

extern u_long current_time;
extern s_char sys_precision;
extern struct event timerqueue[];
#ifdef ATOM
extern int fdpps;
extern int pps_sample P((l_fp *));
#endif

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   P((struct parseunit *, int));
static void parse_process P((struct parseunit *, parsetime_t *));
static void clear_err     P((struct parseunit *, u_long));
static int  list_err      P((struct parseunit *, u_long));
static char * l_mktime    P((u_long));

/**===========================================================================
 ** implementation error message regression module
 **/
static void
clear_err(parse, state)
     struct parseunit *parse;
     u_long            state;
{
  if (state == ERR_ALL)
    {
      int 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[state].err_stage   = err_tbl[state];
      parse->errors[state].err_cnt     = 0;
      parse->errors[state].err_last    = 0;
      parse->errors[state].err_started = 0;
      parse->errors[state].err_suppressed = 0;
    }
}

static int
list_err(parse, state)
     struct parseunit *parse;
     u_long            state;
{
  int do_it;
  struct errorinfo *err = &parse->errors[state];

  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, "PARSE receiver #%d: interval for following error message class is at least %s",
	   CLK_UNIT(parse->peer), 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, "PARSE receiver #%d: %d message%s suppressed, error condition class persists for %s",
	     CLK_UNIT(parse->peer), err->err_suppressed, (err->err_suppressed == 1) ? " was" : "s where",
	     l_mktime(current_time - err->err_started));
      err->err_suppressed = 0;
    }
  
  return do_it;
}

/**===========================================================================
 ** implementation of i/o handling methods
 ** (all STREAM, partial STREAM, user level)
 **/

/*
 * define possible io handling methods
 */
#ifdef STREAM
static int  ppsclock_init   P((struct parseunit *));
static int  stream_init     P((struct parseunit *));
static void stream_end      P((struct parseunit *));
static int  stream_enable   P((struct parseunit *));
static int  stream_disable  P((struct parseunit *));
static int  stream_setcs    P((struct parseunit *, parsectl_t *));
static int  stream_getfmt   P((struct parseunit *, parsectl_t *));
static int  stream_setfmt   P((struct parseunit *, parsectl_t *));
static int  stream_timecode P((struct parseunit *, parsectl_t *));
static void stream_receive  P((struct recvbuf *));
static void stream_poll     P((struct parseunit *));
#endif
					 
static int  local_init     P((struct parseunit *));
static void local_end      P((struct parseunit *));
static int  local_nop      P((struct parseunit *));
static int  local_setcs    P((struct parseunit *, parsectl_t *));
static int  local_getfmt   P((struct parseunit *, parsectl_t *));
static int  local_setfmt   P((struct parseunit *, parsectl_t *));
static int  local_timecode P((struct parseunit *, parsectl_t *));
static void local_receive  P((struct recvbuf *));
static void local_poll     P((struct parseunit *));

static bind_t io_bindings[] =
{
#ifdef STREAM
  {
    "parse STREAM",
    stream_init,
    stream_end,
    stream_setcs,
    stream_disable,
    stream_enable,
    stream_getfmt,
    stream_setfmt,
    stream_timecode,
    stream_receive,
    stream_poll
  },
  {
    "ppsclock STREAM",
    ppsclock_init,
    local_end,
    local_setcs,
    local_nop,
    local_nop,
    local_getfmt,
    local_setfmt,
    local_timecode,
    local_receive,
    local_poll
  },
#endif
  {
    "normal",
    local_init,
    local_end,
    local_setcs,
    local_nop,
    local_nop,
    local_getfmt,
    local_setfmt,
    local_timecode,
    local_receive,
    local_poll
  },
  {
    (char *)0,
  }
};

#ifdef STREAM

#define fix_ts(_X_) \
  if ((&(_X_))->tv.tv_usec >= 1000000)                \
    {                                                 \
      (&(_X_))->tv.tv_usec -= 1000000;                \
      (&(_X_))->tv.tv_sec  += 1;                      \
    }

#define cvt_ts(_X_, _Y_) \
  {                                                   \
    l_fp ts;                                          \
                                                      \
    fix_ts((_X_));                                    \
    if (!buftvtots((const char *)&(&(_X_))->tv, &ts)) \
      {                                               \
	ERR(ERR_BADDATA)			      \
	  msyslog(LOG_ERR,"parse: stream_receive: timestamp conversion error (buftvtots) (%s) (%d.%06d) ", (_Y_), (&(_X_))->tv.tv_sec, (&(_X_))->tv.tv_usec);\
	return;                                       \
      }                                               \
    else                                              \
      {                                               \
	(&(_X_))->fp = ts;                            \
      }                                               \
  }

/*--------------------------------------------------
 * ppsclock STREAM init
 */
static int
ppsclock_init(parse)
     struct parseunit *parse;
{
  /*
   * now push the parse streams module
   * it will ensure exclusive access to the device
   */
  if (ioctl(parse->generic->io.fd, I_PUSH, (caddr_t)"ppsclocd") == -1 &&
      ioctl(parse->generic->io.fd, I_PUSH, (caddr_t)"ppsclock") == -1)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: ppsclock_init: ioctl(fd, I_PUSH, \"ppsclock\"): %m",
	     CLK_UNIT(parse->peer));
      return 0;
    }
  if (!local_init(parse))
    {
      (void)ioctl(parse->generic->io.fd, I_POP, (caddr_t)0);
      return 0;
    }

  parse->flags |= PARSE_PPSCLOCK;
  return 1;
}

/*--------------------------------------------------
 * parse STREAM init
 */
static int
stream_init(parse)
     struct parseunit *parse;
{
  /*
   * now push the parse streams module
   * to test whether it is there (Oh boy - neat kernel interface)
   */
  if (ioctl(parse->generic->io.fd, I_PUSH, (caddr_t)"parse") == -1)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: stream_init: ioctl(fd, I_PUSH, \"parse\"): %m", CLK_UNIT(parse->peer));
      return 0;
    }
  else
    {
      while(ioctl(parse->generic->io.fd, I_POP, (caddr_t)0) == 0)
	/* empty loop */;

      /*
       * now push it a second time after we have removed all
       * module garbage
       */
      if (ioctl(parse->generic->io.fd, I_PUSH, (caddr_t)"parse") == -1)
	{
	  msyslog(LOG_ERR, "PARSE receiver #%d: stream_init: ioctl(fd, I_PUSH, \"parse\"): %m", CLK_UNIT(parse->peer));
	  return 0;
	}
      else
	{
	  return 1;
        }
    }
}

/*--------------------------------------------------
 * parse STREAM end
 */
static void
stream_end(parse)
     struct parseunit *parse;
{
  while(ioctl(parse->generic->io.fd, I_POP, (caddr_t)0) == 0)
    /* empty loop */;
}

 /*--------------------------------------------------
 * STREAM setcs
 */
static int
stream_setcs(parse, tcl)
     struct parseunit *parse;
     parsectl_t  *tcl;
{
  struct strioctl strioc;
  
  strioc.ic_cmd     = PARSEIOC_SETCS;
  strioc.ic_timout  = 0;
  strioc.ic_dp      = (char *)tcl;
  strioc.ic_len     = sizeof (*tcl);

  if (ioctl(parse->generic->io.fd, I_STR, (caddr_t)&strioc) == -1)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: stream_setcs: ioctl(fd, I_STR, PARSEIOC_SETCS): %m", CLK_UNIT(parse->peer));
      return 0;
    }
  return 1;
}

/*--------------------------------------------------
 * STREAM enable
 */
static int
stream_enable(parse)
     struct parseunit *parse;
{
  struct strioctl strioc;
  
  strioc.ic_cmd     = PARSEIOC_ENABLE;
  strioc.ic_timout  = 0;
  strioc.ic_dp      = (char *)0;
  strioc.ic_len     = 0;

  if (ioctl(parse->generic->io.fd, I_STR, (caddr_t)&strioc) == -1)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: stream_enable: ioctl(fd, I_STR, PARSEIOC_ENABLE): %m", CLK_UNIT(parse->peer));
      return 0;
    }
  parse->generic->io.clock_recv = stream_receive; /* ok - parse input in kernel */
  return 1;
}

/*--------------------------------------------------
 * STREAM disable
 */
static int
stream_disable(parse)
     struct parseunit *parse;
{
  struct strioctl strioc;
  
  strioc.ic_cmd     = PARSEIOC_DISABLE;
  strioc.ic_timout  = 0;
  strioc.ic_dp      = (char *)0;
  strioc.ic_len     = 0;

  if (ioctl(parse->generic->io.fd, I_STR, (caddr_t)&strioc) == -1)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: stream_disable: ioctl(fd, I_STR, PARSEIOC_DISABLE): %m", CLK_UNIT(parse->peer));
      return 0;
    }
  parse->generic->io.clock_recv = local_receive; /* ok - parse input in daemon */
  return 1;
}

/*--------------------------------------------------
 * STREAM getfmt
 */
static int
stream_getfmt(parse, tcl)
     struct parseunit *parse;
     parsectl_t  *tcl;
{
  struct strioctl strioc;
  
  strioc.ic_cmd     = PARSEIOC_GETFMT;
  strioc.ic_timout  = 0;
  strioc.ic_dp      = (char *)tcl;
  strioc.ic_len     = sizeof (*tcl);
  if (ioctl(parse->generic->io.fd, I_STR, (caddr_t)&strioc) == -1)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: ioctl(fd, I_STR, PARSEIOC_GETFMT): %m", CLK_UNIT(parse->peer));
      return 0;
    }
  return 1;
}

/*--------------------------------------------------
 * STREAM setfmt
 */
static int
stream_setfmt(parse, tcl)
     struct parseunit *parse;
     parsectl_t  *tcl;
{
  struct strioctl strioc;
  
  strioc.ic_cmd     = PARSEIOC_SETFMT;
  strioc.ic_timout  = 0;
  strioc.ic_dp      = (char *)tcl;
  strioc.ic_len     = sizeof (*tcl);

  if (ioctl(parse->generic->io.fd, I_STR, (caddr_t)&strioc) == -1)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: stream_setfmt: ioctl(fd, I_STR, PARSEIOC_SETFMT): %m", CLK_UNIT(parse->peer));
      return 0;
    }
  return 1;
}


/*--------------------------------------------------
 * STREAM timecode
 */
static int
stream_timecode(parse, tcl)
     struct parseunit *parse;
     parsectl_t  *tcl;
{
  struct strioctl strioc;
  
  strioc.ic_cmd     = PARSEIOC_TIMECODE;
  strioc.ic_timout  = 0;
  strioc.ic_dp      = (char *)tcl;
  strioc.ic_len     = sizeof (*tcl);
	
  if (ioctl(parse->generic->io.fd, I_STR, (caddr_t)&strioc) == -1)
    {
      ERR(ERR_INTERNAL)
	msyslog(LOG_ERR, "PARSE receiver #%d: stream_timecode: ioctl(fd, I_STR, PARSEIOC_TIMECODE): %m", CLK_UNIT(parse->peer));
      return 0;
    }
  clear_err(parse, ERR_INTERNAL);
  return 1;
}

/*--------------------------------------------------
 * STREAM receive
 */
static void
stream_receive(rbufp)
     struct recvbuf *rbufp;
{
  struct parseunit *parse = (struct parseunit *)rbufp->recv_srcclock;
  parsetime_t parsetime;

  if (!parse->peer)
    return;

  if (rbufp->recv_length != sizeof(parsetime_t))
    {
      ERR(ERR_BADIO)
	msyslog(LOG_ERR,"PARSE receiver #%d: parse_receive: bad size (got %d expected %d)",
	     CLK_UNIT(parse->peer), rbufp->recv_length, sizeof(parsetime_t));
      parse->generic->baddata++;
      parse_event(parse, CEVNT_BADREPLY);
      return;
    }
  clear_err(parse, ERR_BADIO);
  
  memmove((caddr_t)&parsetime,
	  (caddr_t)rbufp->recv_buffer,
	  sizeof(parsetime_t));

  /*
   * switch time stamp world - be sure to normalize small usec field
   * errors.
   */

  cvt_ts(parsetime.parse_stime, "parse_stime");

  if (PARSE_TIMECODE(parsetime.parse_state))
    {
      cvt_ts(parsetime.parse_time, "parse_time");
    }

  if (PARSE_PPS(parsetime.parse_state))
    cvt_ts(parsetime.parse_ptime, "parse_ptime");

  parse_process(parse, &parsetime);
}

/*--------------------------------------------------
 * STREAM poll
 */
static void
stream_poll(parse)
     struct parseunit *parse;
{
  int fd, i, rtc;
  fd_set fdmask;
  struct timeval timeout, starttime, curtime, selecttime;
#ifdef HAVE_GETCLOCK
  struct timespec ts;
#endif
  parsetime_t parsetime;

  /*
   * now we do the following:
   *    - read the first packet from the parse module  (OLD !!!)
   *    - read the second packet from the parse module (fresh)
   *    - compute values for xntp
   */
	
  FD_ZERO(&fdmask);
  fd = parse->generic->io.fd;
  FD_SET(fd, &fdmask);
  timeout.tv_sec = 0;
  timeout.tv_usec = 500000;	/* 0.5 sec */

  if (parse->parse_type->cl_poll)
    {
      parse->parse_type->cl_poll(parse);
    }

#ifdef HAVE_GETCLOCK
  if (getclock(TIMEOFDAY, &ts) == -1)
    {
      msyslog(LOG_ERR,"getclock failed: %m");
      exit(1);
    }
  starttime.tv_sec = ts.tv_sec;
  starttime.tv_usec = ts.tv_nsec / 1000;
#else /*  not HAVE_GETCLOCK */
  if (GETTIMEOFDAY(&starttime, 0L) == -1)
    {
      msyslog(LOG_ERR,"gettimeofday failed: %m");
      exit(1);
    }
#endif /* not HAVE_GETCLOCK */

  selecttime = timeout;

  while ((rtc = select(fd + 1, &fdmask, 0, 0, &selecttime)) != 1)
    {
      /* no data from the radio clock */

      if (rtc == -1)
	{
	  if (errno == EINTR)
	    {
#ifdef HAVE_GETCLOCK
	      if (getclock(TIMEOFDAY, &ts) == -1)
                {
                  msyslog(LOG_ERR,"getclock failed: %m");
                  exit(1);
                }
	      curtime.tv_sec = ts.tv_sec;
	      curtime.tv_usec = ts.tv_nsec / 1000;
#else /*  not HAVE_GETCLOCK */
	      if (GETTIMEOFDAY(&curtime, 0L) == -1)
		{
		  msyslog(LOG_ERR,"gettimeofday failed: %m");
		  exit(1);
		}
#endif /* not HAVE_GETCLOCK */
	      selecttime.tv_sec = curtime.tv_sec - starttime.tv_sec;
	      if (curtime.tv_usec < starttime.tv_usec)
		{
		  selecttime.tv_sec  -= 1;
		  selecttime.tv_usec  = 1000000 + curtime.tv_usec - starttime.tv_usec;
		}
	      else
		{
		  selecttime.tv_usec = curtime.tv_usec - starttime.tv_usec;
		}
	

	      if (timercmp(&selecttime, &timeout, >))
		{
		  /*
		   * elapsed real time passed timeout value - consider it timed out
		   */
		  break;
		}

	      /*
	       * calculate residual timeout value
	       */
	      selecttime.tv_sec = timeout.tv_sec - selecttime.tv_sec;

	      if (selecttime.tv_usec > timeout.tv_usec)
		{
		  selecttime.tv_sec -= 1;
		  selecttime.tv_usec = 1000000 + timeout.tv_usec - selecttime.tv_usec;
		}
	      else
		{
		  selecttime.tv_usec = timeout.tv_usec - selecttime.tv_usec;
		}
	
	      FD_SET(fd, &fdmask);
	      continue;
	    }
	  else
	    {
	      ERR(ERR_BADIO)
		msyslog(LOG_WARNING, "PARSE receiver #%d: no data[old] from device (select() error: %m)", CLK_UNIT(parse->peer));
	    }
	}
      else
	{
	  ERR(ERR_NODATA)
	    msyslog(LOG_WARNING, "PARSE receiver #%d: no data[old] from device (check receiver / cableling)", CLK_UNIT(parse->peer));
	}
      parse->generic->noreply++;
      parse->lastmissed = current_time;
      parse_event(parse, CEVNT_TIMEOUT);

      return;
    }

  while (((i = read(fd, (char *)&parsetime, sizeof(parsetime))) < sizeof(parsetime)))
    {
      /* bad packet */
      if ( i == -1)
	{
	  if (errno == EINTR)
	    {
	      continue;
	    }
	  else
	    {
	      ERR(ERR_BADIO)
		msyslog(LOG_WARNING, "PARSE receiver #%d: bad read[old] from streams module (read() error: %m)", CLK_UNIT(parse->peer), i, sizeof(parsetime));
	    }
	}
      else
	{
	  ERR(ERR_BADIO)
	    msyslog(LOG_WARNING, "PARSE receiver #%d: bad read[old] from streams module (got %d bytes - expected %d bytes)", CLK_UNIT(parse->peer), i, sizeof(parsetime));
	}
      parse->generic->baddata++;
      parse_event(parse, CEVNT_BADREPLY);

      return;
    }

  if (parse->parse_type->cl_poll)
    {
      parse->parse_type->cl_poll(parse);
    }

  timeout.tv_sec = 1;
  timeout.tv_usec = 500000;	/* 1.500 sec */
  FD_ZERO(&fdmask);
  FD_SET(fd, &fdmask);

#ifdef HAVE_GETCLOCK
  if (getclock(TIMEOFDAY, &ts) == -1)
    {
      msyslog(LOG_ERR,"getclock failed: %m");
      exit(1);
    }
  starttime.tv_sec = ts.tv_sec;
  starttime.tv_usec = ts.tv_nsec / 1000;
#else /*  not HAVE_GETCLOCK */
  if (GETTIMEOFDAY(&starttime, 0L) == -1)
    {
      msyslog(LOG_ERR,"gettimeofday failed: %m");
      exit(1);
    }
#endif /* not HAVE_GETCLOCK */

  selecttime = timeout;

  while ((rtc = select(fd + 1, &fdmask, 0, 0, &selecttime)) != 1)
    {
      /* no data from the radio clock */

      if (rtc == -1)
	{
	  if (errno == EINTR)
	    {
#ifdef HAVE_GETCLOCK
	      if (getclock(TIMEOFDAY, &ts) == -1)
                {
                  msyslog(LOG_ERR, "getclock failed: %m");
                  exit(1);
                }
	      curtime.tv_sec = ts.tv_sec;
	      curtime.tv_usec = ts.tv_nsec / 1000;
#else /*  not HAVE_GETCLOCK */
	      if (GETTIMEOFDAY(&curtime, 0L) == -1)
		{
		  msyslog(LOG_ERR,"gettimeofday failed: %m");
		  exit(1);
		}
#endif /* not HAVE_GETCLOCK */
	      selecttime.tv_sec = curtime.tv_sec - starttime.tv_sec;
	      if (curtime.tv_usec < starttime.tv_usec)
		{
		  selecttime.tv_sec  -= 1;
		  selecttime.tv_usec  = 1000000 + curtime.tv_usec - starttime.tv_usec;
		}
	      else
		{
		  selecttime.tv_usec = curtime.tv_usec - starttime.tv_usec;
		}
	

	      if (timercmp(&selecttime, &timeout, >))
		{
		  /*
		   * elapsed real time passed timeout value - consider it timed out
		   */
		  break;
		}

	      /*
	       * calculate residual timeout value
	       */
	      selecttime.tv_sec = timeout.tv_sec - selecttime.tv_sec;

	      if (selecttime.tv_usec > timeout.tv_usec)
		{
		  selecttime.tv_sec -= 1;
		  selecttime.tv_usec = 1000000 + timeout.tv_usec - selecttime.tv_usec;
		}
	      else
		{
		  selecttime.tv_usec = timeout.tv_usec - selecttime.tv_usec;
		}
	
	      FD_SET(fd, &fdmask);
	      continue;
	    }
	  else
	    {
	      ERR(ERR_BADIO)
		msyslog(LOG_WARNING, "PARSE receiver #%d: no data[new] from device (select() error: %m)", CLK_UNIT(parse->peer));
	    }
	}
      else
	{
	  ERR(ERR_NODATA)
	    msyslog(LOG_WARNING, "PARSE receiver #%d: no data[new] from device (check receiver / cableling) ", CLK_UNIT(parse->peer));
	}
	
      /*
       * we will return here iff we got a good old sample as this would
       * be misinterpreted. bad samples are passed on to be logged into the
       * state statistics
       */
      if ((parsetime.parse_status & CVT_MASK) == CVT_OK)
	{
	  parse->generic->noreply++;
	  parse->lastmissed = current_time;
	  parse_event(parse, CEVNT_TIMEOUT);
	  return;
	}
    }

  /*
   * we get here either by a possible read() (rtc == 1 - while assertion)
   * or by a timeout or a system call error. when a read() is possible we
   * get the new data, otherwise we stick with the old
   */
  if ((rtc == 1) && ((i = read(fd, (char *)&parsetime, sizeof(parsetime))) < sizeof(parsetime)))
    {
      /* bad packet */
      if ( i== -1)
	{
	  ERR(ERR_BADIO)
	    msyslog(LOG_WARNING, "PARSE receiver #%d: bad read[new] from streams module (read() error: %m)", CLK_UNIT(parse->peer), i, sizeof(parsetime));
	}
      else
	{
	  ERR(ERR_BADIO)
	    msyslog(LOG_WARNING, "PARSE receiver #%d: bad read[new] from streams module (got %d bytes - expected %d bytes)", CLK_UNIT(parse->peer), i, sizeof(parsetime));
	}
      parse->generic->baddata++;
      parse_event(parse, CEVNT_BADREPLY);

      return;
    }

  cvt_ts(parsetime.parse_stime, "parse_stime");

  if (PARSE_TIMECODE(parsetime.parse_state))
    {
      cvt_ts(parsetime.parse_time, "parse_time");
    }

  if (PARSE_PPS(parsetime.parse_state))
    cvt_ts(parsetime.parse_ptime, "parse_ptime");

  /*
   * process what we got
   */
  parse_process(parse, &parsetime);
}
#endif

/*--------------------------------------------------
 * local init
 */
static int
local_init(parse)
     struct parseunit *parse;
{
  return parse_ioinit(&parse->parseio);
}

/*--------------------------------------------------
 * local end
 */
static void
local_end(parse)
     struct parseunit *parse;
{
  parse_ioend(&parse->parseio);
}


/*--------------------------------------------------
 * local nop
 */
static int
local_nop(parse)
     struct parseunit *parse;
{
  return 1;
}

/*--------------------------------------------------
 * local setcs
 */
static int
local_setcs(parse, tcl)
     struct parseunit *parse;
     parsectl_t  *tcl;
{
  return parse_setcs(tcl, &parse->parseio);
}

/*--------------------------------------------------
 * local getfmt
 */
static int
local_getfmt(parse, tcl)
     struct parseunit *parse;
     parsectl_t  *tcl;
{
  return parse_getfmt(tcl, &parse->parseio);
}

/*--------------------------------------------------
 * local setfmt
 */
static int
local_setfmt(parse, tcl)
     struct parseunit *parse;
     parsectl_t  *tcl;
{
  return parse_setfmt(tcl, &parse->parseio);
}

/*--------------------------------------------------
 * local timecode
 */
static int
local_timecode(parse, tcl)
     struct parseunit *parse;
     parsectl_t  *tcl;
{
  return parse_timecode(tcl, &parse->parseio);
}


/*--------------------------------------------------
 * local receive
 */
static void
local_receive(rbufp)
     struct recvbuf *rbufp;
{
  struct parseunit *parse = (struct parseunit *)rbufp->recv_srcclock;
  int count;
  unsigned char *s;
  timestamp_t ts;

  if (!parse->peer)
    return;

  /*
   * eat all characters, parsing then and feeding complete samples
   */
  count = rbufp->recv_length;
  s = (unsigned char *)rbufp->recv_buffer;
  ts.fp = rbufp->recv_time;

  while (count--)
    {
      if (parse_ioread(&parse->parseio, (unsigned int)(*s++), &ts))
	{
	  /*
	   * got something good to eat
	   */
#ifdef PPS
	  if (!PARSE_PPS(parse->parseio.parse_dtime.parse_state) &&
	      (parse->flags & PARSE_PPSCLOCK))
	    {
	      l_fp ts;
	      struct ppsclockev ev;

	      if (ioctl(parse->generic->io.fd, CIOGETEV, (caddr_t)&ev) == 0)
		{
		  if (ev.serial != parse->ppsserial)
		    {
		      /*
                       * add PPS time stamp if available via ppsclock module
		       * and not supplied already.
		       */
		      if (!buftvtots((const char *)&ev.tv, &ts))
			{
			  ERR(ERR_BADDATA)
			    msyslog(LOG_ERR,"parse: local_receive: timestamp conversion error (buftvtots) (ppsclockev.tv)");
			}
		      else
			{
		          parse->parseio.parse_dtime.parse_ptime.fp = ts;
			  parse->parseio.parse_dtime.parse_state |= PARSEB_PPS|PARSEB_S_PPS;
			}
		    }
		  parse->ppsserial = ev.serial;
	       }
	    }
#endif
	  parse_process(parse, &parse->parseio.parse_dtime);
	  parse_iodone(&parse->parseio);
	}
    }
}

/*--------------------------------------------------
 * local poll
 */
static void
local_poll(parse)
     struct parseunit *parse;
{
  int fd, i, rtc;
  fd_set fdmask;
  struct timeval timeout, starttime, curtime, selecttime;
#ifdef HAVE_GETCLOCK
        struct timespec ts;
#endif
  static struct timeval null_time = { 0, 0};
  timestamp_t st;

  FD_ZERO(&fdmask);
  fd = parse->generic->io.fd;
  FD_SET(fd, &fdmask);
  timeout.tv_sec  = 1;
  timeout.tv_usec = 500000;	/* 1.5 sec */

  if (parse->parse_type->cl_poll)
    {
      parse->parse_type->cl_poll(parse);
    }

#ifdef HAVE_GETCLOCK
  if (getclock(TIMEOFDAY, &ts) == -1)
    {
      msyslog(LOG_ERR, "getclock failed: %m");
      exit(1);
    }
  starttime.tv_sec = ts.tv_sec;
  starttime.tv_usec = ts.tv_nsec / 1000;
#else /*  not HAVE_GETCLOCK */
  if (GETTIMEOFDAY(&starttime, 0L) == -1)
    {
      msyslog(LOG_ERR,"gettimeofday failed: %m");
      exit(1);
    }
#endif /* not HAVE_GETCLOCK */

  selecttime = timeout;

  do
    {
      while ((rtc = select(fd + 1, &fdmask, 0, 0, &selecttime)) != 1)
	{
	  /* no data from the radio clock */

	  if (rtc == -1)
	    {
	      if (errno == EINTR)
		{
#ifdef HAVE_GETCLOCK
		  if (getclock(TIMEOFDAY, &ts) == -1)
                    {
                      msyslog(LOG_ERR, "getclock failed: %m");
                      exit(1);
                    }
		  curtime.tv_sec = ts.tv_sec;
		  curtime.tv_usec = ts.tv_nsec / 1000;
#else /*  not HAVE_GETCLOCK */
		  if (GETTIMEOFDAY(&curtime, 0L) == -1)
		    {
		      msyslog(LOG_ERR,"gettimeofday failed: %m");
		      exit(1);
		    }
#endif /* not HAVE_GETCLOCK */
		  selecttime.tv_sec = curtime.tv_sec - starttime.tv_sec;
		  if (curtime.tv_usec < starttime.tv_usec)
		    {
		      selecttime.tv_sec  -= 1;
		      selecttime.tv_usec  = 1000000 + curtime.tv_usec - starttime.tv_usec;
		    }
		  else
		    {
		      selecttime.tv_usec = curtime.tv_usec - starttime.tv_usec;
		    }
	

		  if (!timercmp(&selecttime, &timeout, >))
		    {
		      /*
		       * calculate residual timeout value
		       */
		      selecttime.tv_sec = timeout.tv_sec - selecttime.tv_sec;

		      if (selecttime.tv_usec > timeout.tv_usec)
			{
			  selecttime.tv_sec -= 1;
			  selecttime.tv_usec = 1000000 + timeout.tv_usec - selecttime.tv_usec;
			}
		      else
			{
			  selecttime.tv_usec = timeout.tv_usec - selecttime.tv_usec;
			}
	
		      FD_SET(fd, &fdmask);
		      continue;
		    }
		}
	      else
		{
		  ERR(ERR_BADIO)
		    msyslog(LOG_WARNING, "PARSE receiver #%d: no data from device (select() error: %m)", CLK_UNIT(parse->peer));
		}
	    }
	  else
	    {
	      ERR(ERR_NODATA)
		msyslog(LOG_WARNING, "PARSE receiver #%d: no data from device (check receiver / cableling) ", CLK_UNIT(parse->peer));
	    }

	  parse->generic->noreply++;
	  parse->lastmissed = current_time;
	  parse_event(parse, CEVNT_TIMEOUT);

	  return;
	}

      /*
       * at least 1 character is available - gobble everthing up that is available
       */
      do
	{
	  char inbuf[256];

	  char *s = inbuf;

	  rtc = i = read(fd, inbuf, sizeof(inbuf));

	  get_systime(&st.fp);

	  while (i-- > 0)
	    {
	      if (parse_ioread(&parse->parseio, (unsigned int)(*s++), &st))
		{
		  /*
		   * got something good to eat
		   */
		  parse_process(parse, &parse->parseio.parse_dtime);
		  parse_iodone(&parse->parseio);
		  /*
		   * done if no more characters are available
		   */
		  FD_SET(fd, &fdmask);
		  if ((i == 0) &&
		      (select(fd + 1, &fdmask, 0, 0, &null_time) == 0))
		    return;
		}
	    }
	  FD_SET(fd, &fdmask);
	} while ((rtc = select(fd + 1, &fdmask, 0, 0, &null_time)) == 1);
      FD_SET(fd, &fdmask);
    } while (1);
}

/*--------------------------------------------------
 * init_iobinding - find and initialize lower layers
 */
static bind_t *
init_iobinding(parse)
     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
 **/

/*--------------------------------------------------
 * convert a flag field to a string
 */
static char *
parsestate(state, buffer)
  u_long state;
  char *buffer;
{
  static struct bits
    {
      u_long bit;
      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_ALTERNATE,"ALTERNATE ANTENNA" },
      { PARSEB_TIMECODE, "TIME CODE" },
      { PARSEB_PPS,      "PPS" },
      { PARSEB_POSITION, "POSITION" },
      { 0 }
    };

  static struct sbits
    {
      u_long bit;
      char         *name;
    } sflagstrings[] =
    {
      { PARSEB_S_LEAP,     "LEAP INDICATION" },
      { PARSEB_S_PPS,      "PPS SIGNAL" },
      { PARSEB_S_ANTENNA,  "ANTENNA" },
      { PARSEB_S_POSITION, "POSITION" },
      { 0 }
    };
  int i;

  *buffer = '\0';

  i = 0;
  while (flagstrings[i].bit)
    {
      if (flagstrings[i].bit & state)
	{
	  if (buffer[0])
	    strcat(buffer, "; ");
	  strcat(buffer, flagstrings[i].name);
	}
      i++;
    }

  if (state & (PARSEB_S_LEAP|PARSEB_S_ANTENNA|PARSEB_S_PPS|PARSEB_S_POSITION))
    {
      char *s, *t;

      if (buffer[0])
	strcat(buffer, "; ");

      strcat(buffer, "(");

      t = s = buffer + strlen(buffer);

      i = 0;
      while (sflagstrings[i].bit)
	{
	  if (sflagstrings[i].bit & state)
	    {
	      if (t != s)
		{
		  strcpy(t, "; ");
		  t += 2;
		}
	
	      strcpy(t, sflagstrings[i].name);
	      t += strlen(t);
	    }
	  i++;
	}
      strcpy(t, ")");
    }
  return buffer;
}

/*--------------------------------------------------
 * convert a status flag field to a string
 */
static char *
parsestatus(state, buffer)
  u_long state;
  char *buffer;
{
  static struct bits
    {
      u_long bit;
      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" },
      { 0 }
    };
  int i;

  *buffer = '\0';

  i = 0;
  while (flagstrings[i].bit)
    {
      if (flagstrings[i].bit & state)
	{
	  if (buffer[0])
	    strcat(buffer, "; ");
	  strcat(buffer, flagstrings[i].name);
	}
      i++;
    }

  return buffer;
}

/*--------------------------------------------------
 * convert a clock status flag field to a string
 */
static char *
clockstatus(state)
  u_long state;
{
  static char buffer[20];
  static struct status
    {
      u_long value;
      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" },
      { ~0UL }
    };
  int i;

  i = 0;
  while (flagstrings[i].value != ~0)
    {
      if (flagstrings[i].value == state)
	{
	  return flagstrings[i].name;
	}
      i++;
    }

  sprintf(buffer, "unknown #%ld", (u_long)state);

  return buffer;
}

/*--------------------------------------------------
 * mkascii - make a printable ascii string
 * assumes (unless defined better) 7-bit ASCII
 */
#ifndef isprint
#define isprint(_X_) (((_X_) > 0x1F) && ((_X_) < 0x7F))
#endif

static char *
mkascii(buffer, blen, src, srclen)
  char  *buffer;
  long  blen;
  char  *src;
  long  srclen;
{
  char *b    = buffer;
  char *endb = (char *)0;

  if (blen < 4)
    return (char *)0;		/* don't bother with mini buffers */

  endb = buffer + blen - 4;

  blen--;			/* account for '\0' */

  while (blen && srclen--)
    {
      if ((*src != '\\') && isprint(*src))
	{			/* printables are easy... */
	  *buffer++ = *src++;
	  blen--;
	}
      else
	{
	  if (blen < 4)
	    {
	      while (blen--)
		{
		  *buffer++ = '.';
		}
	      *buffer = '\0';
	      return b;
	    }
	  else
	    {
	      if (*src == '\\')
		{
		  strcpy(buffer,"\\\\");
		  buffer += 2;
		  blen   -= 2;
		}
	      else
		{
		  sprintf(buffer, "\\x%02x", *src++);
		  blen   -= 4;
		  buffer += 4;
		}
	    }
	}
      if (srclen && !blen && endb) /* overflow - set last chars to ... */
	strcpy(endb, "...");
    }

  *buffer = '\0';
  return b;
}


/*--------------------------------------------------
 * l_mktime - make representation of a relative time
 */
static char *
l_mktime(delta)
  u_long delta;
{
  u_long tmp, m, s;
  static char buffer[40];

  buffer[0] = '\0';

  if ((tmp = delta / (60*60*24)) != 0)
    {
      sprintf(buffer, "%ldd+", (u_long)tmp);
      delta -= tmp * 60*60*24;
    }

  s = delta % 60;
  delta /= 60;
  m = delta % 60;
  delta /= 60;

  sprintf(buffer+strlen(buffer), "%02d:%02d:%02d",
	  (int)delta, (int)m, (int)s);

  return buffer;
}


/*--------------------------------------------------
 * parse_statistics - list summary of clock states
 */
static void
parse_statistics(parse)
  struct parseunit *parse;
{
  int i;

  NLOG(NLOG_CLOCKSTATIST) /* conditional if clause for conditional syslog */
    {
      msyslog(LOG_INFO, "PARSE receiver #%d: running time: %s",
	     CLK_UNIT(parse->peer),
	     l_mktime(current_time - parse->generic->timestarted));

      msyslog(LOG_INFO, "PARSE receiver #%d: current status: %s",
	     CLK_UNIT(parse->peer),
	     clockstatus(parse->generic->currentstatus));

      for (i = 0; i <= CEVNT_MAX; i++)
	{
	  u_long stime;
	  u_long percent, div = current_time - parse->generic->timestarted;

	  percent = stime = PARSE_STATETIME(parse, i);

	  while (((u_long)(~0) / 10000) < percent)
	    {
	      percent /= 10;
	      div     /= 10;
	    }

	  if (div)
	    percent = (percent * 10000) / div;
	  else
	    percent = 10000;

	  if (stime)
	    msyslog(LOG_INFO, "PARSE receiver #%d: state %18s: %13s (%3d.%02d%%)",
		   CLK_UNIT(parse->peer),
		   clockstatus(i),
		   l_mktime(stime),
		   percent / 100, percent % 100);
	}
    }
}

/*--------------------------------------------------
 * cparse_statistics - wrapper for statistics call
 */
static void
cparse_statistics(peer)
  struct peer *peer;
{
  struct parseunit *parse = (struct parseunit *)peer;

  parse_statistics(parse);
  parse->stattimer.event_time    = current_time + PARSESTATISTICS;
  TIMER_ENQUEUE(timerqueue, &parse->stattimer);
}

/**===========================================================================
 ** xntp interface routines
 **/

/*--------------------------------------------------
 * parse_init - initialize internal parse driver data
 */
static void
parse_init()
{
  memset((caddr_t)parseunits, 0, sizeof parseunits);
}


/*--------------------------------------------------
 * parse_shutdown - shut down a PARSE clock
 */
static void
parse_shutdown(unit, peer)
     int unit;
     struct peer *peer;
{
  struct parseunit *parse;

  unit = CLK_UNIT(peer);
	
  if (unit >= MAXUNITS)
    {
      msyslog(LOG_ERR,
	     "PARSE receiver #%d: parse_shutdown: INTERNAL ERROR, unit invalid (max %d)",
	     unit,MAXUNITS);
      return;
    }

  parse = parseunits[unit];
	
  if (parse && !parse->peer)
    {
      msyslog(LOG_ERR,
	     "PARSE receiver #%d: parse_shutdown: INTERNAL ERROR, unit not in use", unit);
      return;
    }

  /*
   * print statistics a last time and
   * stop statistics machine
   */
  parse_statistics(parse);
  TIMER_DEQUEUE(&parse->stattimer);
	
#if ATOM
  {
    /*
     * kill possible PPS association
     */
    if (fdpps == parse->generic->io.fd)
      fdpps = -1;
  }
#endif

  if (parse->parse_type->cl_end)
    {
      parse->parse_type->cl_end(parse);
    }
	
  if (parse->binding)
    PARSE_END(parse);

  /*
   * Tell the I/O module to turn us off.  We're history.
   */
  if (!parse->pollonly)
    io_closeclock(&parse->generic->io);
  else
    (void) close(parse->generic->io.fd);

  NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
    msyslog(LOG_INFO, "PARSE receiver #%d: reference clock \"%s\" removed",
	 CLK_UNIT(parse->peer), parse->parse_type->cl_description);

  parse->peer = (struct peer *)0; /* unused now */
}

/*--------------------------------------------------
 * parse_start - open the PARSE devices and initialize data for processing
 */
static int
parse_start(sysunit, peer)
     int sysunit;
     struct peer *peer;
{
  u_int unit;
  int fd232, i;
#ifdef HAVE_TERMIOS
  struct termios tio;		/* NEEDED FOR A LONG TIME ! */
#endif
#ifdef HAVE_SYSV_TTYS
  struct termio tio;		/* NEEDED FOR A LONG TIME ! */
#endif
  struct parseunit * parse;
  char parsedev[sizeof(PARSEDEVICE)+20];
  parsectl_t tmp_ctl;
  u_int type;

  type = CLK_TYPE(peer);
  unit = CLK_UNIT(peer);

  if (unit >= MAXUNITS)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: parse_start: unit number invalid (max %d)",
	     unit, MAXUNITS-1);
      return 0;
    }

  if ((type == ~0) || (parse_clockinfo[type].cl_description == (char *)0))
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: parse_start: unsupported clock type %d (max %d)",
	     unit, CLK_REALTYPE(peer), ncltypes-1);
      return 0;
    }

  if (parseunits[unit] && parseunits[unit]->peer)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: parse_start: unit in use", unit);
      return 0;
    }

  /*
   * Unit okay, attempt to open the device.
   */
  (void) sprintf(parsedev, PARSEDEVICE, unit);

#ifndef O_NOCTTY
#define O_NOCTTY 0
#endif

  fd232 = open(parsedev, O_RDWR | O_NOCTTY
#ifdef O_NONBLOCK
	       | O_NONBLOCK
#endif
	       , 0777);

  if (fd232 == -1)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: parse_start: open of %s failed: %m", unit, parsedev);
      return 0;
    }

  /*
   * Looks like this might succeed.  Find memory for the structure.
   * Look to see if there are any unused ones, if not we malloc()
   * one.
   */
  if (parseunits[unit])
    {
      parse = parseunits[unit];	/* The one we want is okay - and free */
    }
  else
    {
      for (i = 0; i < MAXUNITS; i++)
	{
	  if (parseunits[i] && !parseunits[i]->peer)
	    break;
	}
      if (i < MAXUNITS)
	{
	  /*
	   * Reclaim this one
	   */
	  parse = parseunits[i];
	  parseunits[i] = (struct parseunit *)0;
	}
      else
	{
	  parse = (struct parseunit *)
	    emalloc(sizeof(struct parseunit));
	}
    }

  memset((char *)parse, 0, sizeof(struct parseunit));
  parseunits[unit] = parse;

  parse->generic = peer->procptr;

  /*
   * Set up the structures
   */
  parse->generic->timestarted    = current_time;
  parse->lastchange     = current_time;
  /*
   * we want to filter input for the sake of
   * getting an impression on dispersion
   * also we like to average the median range
   */
  parse->generic->currentstatus	        = CEVNT_TIMEOUT; /* expect the worst */

  parse->flags          = 0;
  parse->pollneeddata   = 0;
  parse->pollonly       = 1;	/* go for default polling mode */
  parse->lastformat     = ~0;	/* assume no format known */
  parse->laststatus     = ~0;	/* be sure to mark initial status change */
  parse->lastmissed     = 0;	/* assume got everything */
  parse->ppsserial      = 0;
  parse->localdata      = (void *)0;

  clear_err(parse, ERR_ALL);
  
  parse->parse_type     = &parse_clockinfo[type];

  parse->generic->fudgetime1.l_ui = 0;	/* we can only pre-configure delays less than 1 second */
  parse->generic->fudgetime1.l_uf = parse->parse_type->cl_basedelay;

  parse->generic->fudgetime2.l_ui  = 0;	/* we can only pre-configure delays less than 1 second */
  parse->generic->fudgetime2.l_uf  = parse->parse_type->cl_ppsdelay;

  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_precision;
  peer->stratum         = STRATUM_REFCLOCK;
  if (peer->stratum <= 1)
    memmove((char *)&parse->generic->refid, parse->parse_type->cl_id, 4);
  else
    parse->generic->refid = htonl(PARSEHSREFID);
	
  parse->generic->io.fd = fd232;
	
  parse->peer = peer;		/* marks it also as busy */

  parse->binding = init_iobinding(parse);

  if (parse->binding == (bind_t *)0)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: parse_start: io sub system initialisation failed.");
      parse_shutdown(CLK_UNIT(parse->peer), peer); /* let our cleaning staff do the work */
      return 0;			/* well, ok - special initialisation broke */
    }      

  /*
   * configure terminal line
   */
  if (TTY_GETATTR(fd232, &tio) == -1)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: parse_start: tcgetattr(%d, &tio): %m", unit, fd232);
      parse_shutdown(CLK_UNIT(parse->peer), peer); /* let our cleaning staff do the work */
      return 0;
    }
  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, "PARSE receiver #%d: parse_start: fpathconf(fd, _PC_VDISABLE): %m", CLK_UNIT(parse->peer));
          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 defined (VMIN) || defined(VTIME)
      if ((parse_clockinfo[type].cl_lflag & ICANON) == 0)
	{
#ifdef VMIN
          tio.c_cc[VMIN]   = 1;
#endif
#ifdef VTIME
          tio.c_cc[VTIME]  = 0;
#endif
        }
#endif

      tio.c_cflag = parse_clockinfo[type].cl_cflag;
      tio.c_iflag = parse_clockinfo[type].cl_iflag;
      tio.c_oflag = parse_clockinfo[type].cl_oflag;
      tio.c_lflag = parse_clockinfo[type].cl_lflag;
	

#ifdef HAVE_TERMIOS
      if ((cfsetospeed(&tio, parse_clockinfo[type].cl_speed) == -1) ||
	  (cfsetispeed(&tio, parse_clockinfo[type].cl_speed) == -1))
	{
	  msyslog(LOG_ERR, "PARSE receiver #%d: parse_start: tcset{i,o}speed(&tio, speed): %m", unit);
	  parse_shutdown(CLK_UNIT(parse->peer), peer); /* let our cleaning staff do the work */
	  return 0;
	}
#else
      tio.c_cflag     |= parse_clockinfo[type].cl_speed;
#endif

      if (TTY_SETATTR(fd232, &tio) == -1)
	{
	  msyslog(LOG_ERR, "PARSE receiver #%d: parse_start: tcsetattr(%d, &tio): %m", unit, fd232);
	  parse_shutdown(CLK_UNIT(parse->peer), peer); /* let our cleaning staff do the work */
	  return 0;
	}
    }

  /*
   * 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;
    }

  if (!PARSE_SETCS(parse, &tmp_ctl))
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: parse_start: parse_setcs() FAILED.", unit);
      parse_shutdown(CLK_UNIT(parse->peer), peer); /* let our cleaning staff do the work */
      return 0;			/* well, ok - special initialisation broke */
    }
  
  strcpy(tmp_ctl.parseformat.parse_buffer, parse->parse_type->cl_format);
  tmp_ctl.parseformat.parse_count = strlen(tmp_ctl.parseformat.parse_buffer);

  if (!PARSE_SETFMT(parse, &tmp_ctl))
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: parse_start: parse_setfmt() FAILED.", unit);
      parse_shutdown(CLK_UNIT(parse->peer), peer); /* let our cleaning staff do the work */
      return 0;			/* well, ok - special initialisation broke */
    }
  
  /*
   * get rid of all IO accumulated so far
   */
#ifdef HAVE_TERMIOS
  (void) tcflush(parse->generic->io.fd, TCIOFLUSH);
#else
#ifdef TCFLSH
  {
#ifndef TCIOFLUSH
#define TCIOFLUSH 2
#endif
    int flshcmd = TCIOFLUSH;

    (void) ioctl(parse->generic->io.fd, TCFLSH, (caddr_t)&flshcmd);
  }
#endif
#endif
  
  /*
   * try to do any special initializations
   */
  if (parse->parse_type->cl_init)
    {
      if (parse->parse_type->cl_init(parse))
	{
	  parse_shutdown(CLK_UNIT(parse->peer), peer); /* let our cleaning staff do the work */
	  return 0;		/* well, ok - special initialisation broke */
	}
    }

  if (!(parse->parse_type->cl_flags & PARSE_F_POLLONLY) &&
      (CLK_PPS(parse->peer) || (parse->parse_type->cl_flags & PARSE_F_NOPOLLONLY)))
    {
      /*
       * Insert in async io device list.
       */
      parse->generic->io.clock_recv = parse->binding->bd_receive; /* pick correct receive routine */
      parse->generic->io.srcclock = (caddr_t)parse;
      parse->generic->io.datalen = 0;

      if (!io_addclock(&parse->generic->io))
	{
	  if (parse->parse_type->cl_flags & PARSE_F_NOPOLLONLY)
	    {
	      msyslog(LOG_ERR,
		     "PARSE receiver #%d: parse_start: addclock %s fails (ABORT - clock type requires async io)", CLK_UNIT(parse->peer), parsedev);
	      parse_shutdown(CLK_UNIT(parse->peer), peer); /* let our cleaning staff do the work */
	      return 0;
	    }
	  else
	    {
	      msyslog(LOG_ERR,
		     "PARSE receiver #%d: parse_start: addclock %s fails (switching to polling mode)", CLK_UNIT(parse->peer), parsedev);
	    }
	}
      else
	{
	  parse->pollonly = 0;	/*
				 * update at receipt of time_stamp - also
				 * supports PPS processing
				 */
	}
    }

#ifdef ATOM
  if (parse->pollonly && (parse->parse_type->cl_flags & PARSE_F_PPSPPS))
    {
      if (fdpps == -1)
	{
	  fdpps = parse->generic->io.fd;
	  if (!PARSE_DISABLE(parse))
	    {
	      msyslog(LOG_ERR, "PARSE receiver #%d: parse_start: parse_disable() FAILED", CLK_UNIT(parse->peer));
	      parse_shutdown(CLK_UNIT(parse->peer), peer); /* let our cleaning staff do the work */
	      return 0;
	    }
	}
      else
	{
	  msyslog(LOG_NOTICE, "PARSE receiver #%d: parse_start: loopfilter PPS already active - no PPS via CIOGETEV", CLK_UNIT(parse->peer));
	}
    }
#endif

  /*
   * wind up statistics timer
   */
  parse->stattimer.peer = (struct peer *)parse; /* we know better, but what the heck */
  parse->stattimer.event_handler = cparse_statistics;
  parse->stattimer.event_time    = current_time + PARSESTATISTICS;
  TIMER_ENQUEUE(timerqueue, &parse->stattimer);

  /*
   * get out Copyright information once
   */
  if (!notice)
    {
      NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
	msyslog(LOG_INFO, "NTP PARSE support: Copyright (c) 1989-1996, Frank Kardel");
      notice = 1;
    }

  /*
   * print out configuration
   */
  NLOG(NLOG_CLOCKINFO)
    {
      /* conditional if clause for conditional syslog */
      msyslog(LOG_INFO, "PARSE receiver #%d: reference clock \"%s\" (device %s) added",
	     CLK_UNIT(parse->peer),
	     parse->parse_type->cl_description, parsedev);

      msyslog(LOG_INFO, "PARSE receiver #%d:  Stratum %d, %sPPS support, trust time %s, precision %d",
	     CLK_UNIT(parse->peer),
	     parse->peer->stratum, (parse->pollonly || !CLK_PPS(parse->peer)) ? "no " : "",
	     l_mktime(parse->parse_type->cl_maxunsync), parse->peer->precision);

      msyslog(LOG_INFO, "PARSE receiver #%d:  rootdelay %s s, phaseadjust %s s, %s IO handling",
	     CLK_UNIT(parse->peer),
	     ufptoa(parse->parse_type->cl_rootdelay, 6),
	     lfptoa(&parse->generic->fudgetime1, 8),
	     parse->binding->bd_description);

      msyslog(LOG_INFO, "PARSE receiver #%d:  Format recognition: %s", CLK_UNIT(parse->peer),
	     !(*parse->parse_type->cl_format) ? "<AUTOMATIC>" : parse->parse_type->cl_format);
#ifdef ATOM
      msyslog(LOG_INFO, "PARSE receiver #%d: %sCD PPS support",
	     CLK_UNIT(parse->peer),
	     (fdpps == parse->generic->io.fd) ? "" : "NO ");
#endif
    }

  return 1;
}

/*--------------------------------------------------
 * parse_poll - called by the transmit procedure
 */
static void
parse_poll(unit, peer)
	int unit;
	struct peer *peer;
{
  struct parseunit *parse;

  unit = CLK_UNIT(peer);

  if (unit >= MAXUNITS)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: poll: INTERNAL: unit invalid",
	     unit);
      return;
    }

  parse = parseunits[unit];

  if (!parse->peer)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: poll: INTERNAL: unit unused",
	     unit);
      return;
    }

  if (peer != parse->peer)
    {
      msyslog(LOG_ERR,
	     "PARSE receiver #%d: poll: INTERNAL: peer incorrect",
	     unit);
      return;
    }

  /*
   * Update clock stat counters
   */
  parse->generic->polls++;

  /*
   * in PPS mode we just mark that we want the next sample
   * for the clock filter
   */
  if (!parse->pollonly)
    {
      if (parse->pollneeddata)
	{
	  /*
	   * bad news - didn't get a response last time
	   */
	  parse->generic->noreply++;
	  parse->lastmissed = current_time;
	  parse_event(parse, CEVNT_TIMEOUT);

	  ERR(ERR_NODATA)
	    msyslog(LOG_WARNING, "PARSE receiver #%d: no data from device within poll interval (check receiver / cableling)", CLK_UNIT(parse->peer));
	}
      parse->pollneeddata = 1;
      if (parse->parse_type->cl_poll)
	{
	  parse->parse_type->cl_poll(parse);
	}
      return;
    }

  /*
   * the following code is only executed only when polling is used
   */

  PARSE_POLL(parse);
}

#define LEN_STATES 300		/* length of state string */

/*--------------------------------------------------
 * parse_control - set fudge factors, return statistics
 */
static void
parse_control(unit, in, out)
  int unit;
  struct refclockstat *in;
  struct refclockstat *out;
{
  struct parseunit *parse;
  parsectl_t tmpctl;
  u_long type;
  static char outstatus[400];	/* status output buffer */
  char *start;

  if (out)
    {
      out->lencode       = 0;
      out->p_lastcode    = 0;
      out->kv_list       = (struct ctl_var *)0;
    }

  if (unit >= MAXUNITS)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: parse_control: unit invalid (max %d)",
	     unit, MAXUNITS-1);
      return;
    }


  /*
   * XXX - UGLY - unit needs to be eliminated in favor of a peer *
   */

  parse = parseunits[unit];

  if (!parse || !parse->peer)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: parse_control: unit invalid (UNIT INACTIVE)",
	     unit);
      return;
    }

  type = CLK_TYPE(parse->peer);
  unit = CLK_UNIT(parse->peer);

  if (in)
    {
      if (in->haveflags & (CLK_HAVEFLAG1|CLK_HAVEFLAG2|CLK_HAVEFLAG3|CLK_HAVEFLAG4))
	{
	  parse->flags = in->flags & (CLK_FLAG1|CLK_FLAG2|CLK_FLAG3|CLK_FLAG4);
	}
    }

  if (out)
    {
      u_long sum = 0;
      char *t, *tt;
      struct tm *tm;
      short utcoff;
      char sign;
      int i;
      time_t tim;

      outstatus[0] = '\0';

      out->type       = REFCLK_PARSE;
      out->haveflags |= CLK_HAVETIME2;

      /*
       * figure out skew between PPS and RS232 - just for informational
       * purposes - returned in time2 value
       */
       if (PARSE_SYNC(parse->time.parse_state))
	 {
	   if (PARSE_PPS(parse->time.parse_state) && PARSE_TIMECODE(parse->time.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->time.parse_stime.fp;
	       L_SUB(&off, &parse->time.parse_ptime.fp); /* true offset */
	       tt = add_var(&out->kv_list, 80, RO);
	       sprintf(tt, "refclock_ppsskew=%s", lfptoms(&off, 6));
	     }
	 }

      if (PARSE_PPS(parse->time.parse_state))
	{
	  tt = add_var(&out->kv_list, 80, RO|DEF);
	  sprintf(tt, "refclock_ppstime=\"%s\"", prettydate(&parse->time.parse_ptime.fp));
	}

      /*
       * all this for just finding out the +-xxxx part (there are always
       * new and changing fields in the standards 8-().
       *
       * but we do it for the human user...
       */
      tim  = parse->time.parse_time.fp.l_ui - JAN_1970;
      tm = gmtime(&tim);
      utcoff = tm->tm_hour * 60 + tm->tm_min;
      tm = localtime(&tim);
      utcoff = tm->tm_hour * 60 + tm->tm_min - utcoff + 12 * 60;
      utcoff += 24 * 60;
      utcoff %= 24 * 60;
      utcoff -= 12 * 60;
      if (utcoff < 0)
	{
	  utcoff = -utcoff;
	  sign = '-';
	}
      else
	{
	  sign = '+';
	}

      tt = add_var(&out->kv_list, 128, RO|DEF);
      sprintf(tt, "refclock_time=\"");
      tt += strlen(tt);

      if (parse->time.parse_time.fp.l_ui == 0)
	{
	  strcpy(tt, "<UNDEFINED>\"");
	}
      else
	{
	  strcpy(tt, prettydate(&parse->time.parse_time.fp));
	  t = tt + strlen(tt);
	  
	  sprintf(t, " (%c%02d%02d)\"", sign, utcoff / 60, utcoff % 60);
	}

      if (!PARSE_GETTIMECODE(parse, &tmpctl))
	{
	  ERR(ERR_INTERNAL)
	    msyslog(LOG_ERR, "PARSE receiver #%d: parse_control: parse_timecode() FAILED", unit);
	}
      else
	{
	  tt = add_var(&out->kv_list, 512, RO|DEF);
	  sprintf(tt, "refclock_status=\"");
	  tt += strlen(tt);

	  /*
	   * copy PPS flags from last read transaction (informational only)
	   */
	  tmpctl.parsegettc.parse_state |= parse->time.parse_state &
					   (PARSEB_PPS|PARSEB_S_PPS);

	  (void) parsestate(tmpctl.parsegettc.parse_state, tt);

	  strcat(tt, "\"");

	  if (tmpctl.parsegettc.parse_count)
	    mkascii(outstatus+strlen(outstatus), sizeof(outstatus)- strlen(outstatus) - 1,
		    tmpctl.parsegettc.parse_buffer, tmpctl.parsegettc.parse_count - 1);

	  parse->generic->badformat += tmpctl.parsegettc.parse_badformat;
	}
	
      tmpctl.parseformat.parse_format = tmpctl.parsegettc.parse_format;
	
      if (!PARSE_GETFMT(parse, &tmpctl))
	{
	  ERR(ERR_INTERNAL)
	    msyslog(LOG_ERR, "PARSE receiver #%d: parse_control: parse_getfmt() FAILED", unit);
	}
      else
	{
	  tt = add_var(&out->kv_list, 80, RO|DEF);
	  sprintf(tt, "refclock_format=\"");

	  strncat(tt, tmpctl.parseformat.parse_buffer, tmpctl.parseformat.parse_count);
	  strcat(tt,"\"");
	}

      /*
       * gather state statistics
       */

      start = tt = add_var(&out->kv_list, LEN_STATES, RO|DEF);
      strcpy(tt, "refclock_states=\"");
      tt += strlen(tt);

      for (i = 0; i <= CEVNT_MAX; i++)
	{
	  u_long stime;
	  u_long div = current_time - parse->generic->timestarted;
	  u_long percent;

	  percent = stime = PARSE_STATETIME(parse, i);

	  while (((u_long)(~0) / 10000) < percent)
	    {
	      percent /= 10;
	      div     /= 10;
	    }
	
	  if (div)
	    percent = (percent * 10000) / div;
	  else
	    percent = 10000;

	  if (stime)
	    {
	      char item[80];
	      int count;
	      
	      sprintf(item, "%s%s%s: %s (%d.%02d%%)",
		      sum ? "; " : "",
                      (parse->generic->currentstatus == i) ? "*" : "",
		      clockstatus(i),
		      l_mktime(stime),
		      (int)(percent / 100), (int)(percent % 100));
	      if ((count = strlen(item)) < (LEN_STATES - 40 - (tt - start)))
		{
		  strcpy(tt, item);
		  tt  += count;
		}
	      sum += stime;
	    }
	}

      sprintf(tt, "; running time: %s\"", l_mktime(sum));

      tt = add_var(&out->kv_list, 32, RO);
      sprintf(tt, "refclock_id=\"%s\"", parse->parse_type->cl_id);

      tt = add_var(&out->kv_list, 80, RO);
      sprintf(tt, "refclock_iomode=\"%s\"", parse->binding->bd_description);

      tt = add_var(&out->kv_list, 128, RO);
      sprintf(tt, "refclock_driver_version=\"refclock_parse.c,v 3.103 1997/07/12 15:35:16 kardel Exp\"");

      out->lencode       = strlen(outstatus);
      out->p_lastcode    = outstatus;
    }
}

/**===========================================================================
 ** processing routines
 **/

/*--------------------------------------------------
 * event handling - note that nominal events will also be posted
 */
static void
parse_event(parse, event)
  struct parseunit *parse;
  int event;
{
  if (parse->generic->currentstatus != (u_char) event)
    {
      parse->statetime[parse->generic->currentstatus] += current_time - parse->lastchange;
      parse->lastchange              = current_time;

      parse->generic->currentstatus    = (u_char)event;

      if (parse->parse_type->cl_event)
	parse->parse_type->cl_event(parse, event);
      
      if (event != CEVNT_NOMINAL)
	{
	  parse->generic->lastevent = parse->generic->currentstatus;
	}
      else
	{
	  NLOG(NLOG_CLOCKSTATUS)
	    msyslog(LOG_INFO, "PARSE receiver #%d: SYNCHRONIZED",
		   CLK_UNIT(parse->peer));
	}

      if (event == CEVNT_FAULT)
	{
	  NLOG(NLOG_CLOCKEVENT) /* conditional if clause for conditional syslog */
	    ERR(ERR_BADEVENT)
	      msyslog(LOG_ERR,
		     "clock %s fault '%s' (0x%02x)", refnumtoa(parse->peer->srcadr.sin_addr.s_addr), ceventstr(event),
		     (u_int)event);
	}
      else
	{
	  NLOG(NLOG_CLOCKEVENT) /* conditional if clause for conditional syslog */
	    if (event == CEVNT_NOMINAL || list_err(parse, ERR_BADEVENT))
	      msyslog(LOG_INFO,
		     "clock %s event '%s' (0x%02x)", refnumtoa(parse->peer->srcadr.sin_addr.s_addr), ceventstr(event),
		     (u_int)event);
	}

      report_event(EVNT_PEERCLOCK, parse->peer);
      report_event(EVNT_CLOCKEXCPT, parse->peer);
    }
}

/*--------------------------------------------------
 * process a PARSE time sample
 */
static void
parse_process(parse, parsetime)
  struct parseunit *parse;
  parsetime_t      *parsetime;
{
  unsigned char leap;
  l_fp off, rectime, reftime;

  /*
   * check for changes in conversion status
   * (only one for each new status !)
   */
  if (parse->laststatus != parsetime->parse_status)
    {
      char buffer[400];

      NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
	msyslog(LOG_WARNING, "PARSE receiver #%d: conversion status \"%s\"",
	     CLK_UNIT(parse->peer), parsestatus(parsetime->parse_status, 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, "PARSE receiver #%d: parse_process: parse_timecode() FAILED", CLK_UNIT(parse->peer));
	    }
	  else
	    {
	      ERR(ERR_BADDATA)
		msyslog(LOG_WARNING, "PARSE receiver #%d: FAILED TIMECODE: \"%s\" (check receiver configuration / cableling)",
		     CLK_UNIT(parse->peer), mkascii(buffer, sizeof buffer, tmpctl.parsegettc.parse_buffer, tmpctl.parsegettc.parse_count - 1));
	      parse->generic->badformat += tmpctl.parsegettc.parse_badformat;
	    }
	}

      parse->laststatus = 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:
	  break;		/* well, still waiting - timeout is handled at higher levels */

	case CVT_FAIL:
	  parse->generic->badformat++;
	  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
		{
		  parse_event(parse, CEVNT_BADREPLY); /* for the lack of something better */
		}
	}
      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, "PARSE receiver #%d: parse_getfmt() FAILED", CLK_UNIT(parse->peer));
	}
      else
	{
	  NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
	    msyslog(LOG_INFO, "PARSE receiver #%d: new packet format \"%s\"",
		 CLK_UNIT(parse->peer), tmpctl.parseformat.parse_buffer);
	}
      parse->lastformat = parsetime->parse_format;
    }

  /*
   * now, any changes ?
   */
  if (parse->time.parse_state != parsetime->parse_state)
    {
      char tmp1[200];
      char tmp2[200];
      /*
       * something happend
       */
	
      (void) parsestate(parsetime->parse_state, tmp1);
      (void) parsestate(parse->time.parse_state, tmp2);
	
      NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
	msyslog(LOG_INFO,"PARSE receiver #%d: STATE CHANGE: %s -> %s",
	     CLK_UNIT(parse->peer), tmp2, tmp1);
    }

  /*
   * remember for future
   */
  parse->time = *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 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
       */
      parse_event(parse, CEVNT_FAULT);
      NLOG(NLOG_CLOCKSTATUS)
	ERR(ERR_BADSTATUS)
	  msyslog(LOG_ERR,"PARSE receiver #%d: NOT SYNCHRONIZED",
		         CLK_UNIT(parse->peer));
    }
  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,"PARSE receiver #%d: TIMECODE NOT CONFIRMED",
			     CLK_UNIT(parse->peer));
	}
    }

  if (PARSE_TIMECODE(parsetime->parse_state))
    {
      l_fp offset;

      /*
       * calculate time offset including systematic delays
       * off = PARSE-timestamp + propagation delay - kernel time stamp
       */
      offset = parse->generic->fudgetime1;
    
      off = parsetime->parse_time.fp;

      reftime = off;

      L_ADD(&off, &offset);
      rectime = off;		/* this makes org time and xmt time somewhat artificial */
    
      L_SUB(&off, &parsetime->parse_stime.fp);
    }

  if (PARSE_PPS(parsetime->parse_state) && CLK_PPS(parse->peer))
    {
      l_fp offset;

      /*
       * we have a PPS signal - much better than the RS232 stuff (we hope)
       */
      offset = parsetime->parse_ptime.fp;

      L_ADD(&offset, &parse->generic->fudgetime2);

      if (PARSE_TIMECODE(parsetime->parse_state))
	{
	  if (M_ISGEQ(off.l_i, off.l_f, -1, 0x80000000) &&
	      M_ISGEQ(0, 0x7fffffff, off.l_i, off.l_f))
	    {
	      /*
	       * RS232 offsets within [-0.5..0.5[ - take PPS offsets
	       */

	      if (parse->parse_type->cl_flags & PARSE_F_PPSONSECOND)
		{
		  reftime = off = offset;
		  rectime = offset;
		  /*
		   * implied on second offset
		   */
		  off.l_uf = ~off.l_uf; /* map [0.5..1[ -> [-0.5..0[ */
		  off.l_ui = (off.l_f < 0) ? ~0 : 0; /* sign extend */
		}
	      else
		{
		  /*
		   * time code describes pulse
		   */
		  off = parsetime->parse_time.fp;

		  rectime = reftime = off; /* take reference time - fake rectime */

		  L_SUB(&off, &offset); /* true offset */
		}
	    }
	  /*
	   * take RS232 offset when PPS when out of bounds
	   */
	}
      else
	{
	  /*
	   * Well, no time code to guide us - assume on second pulse
	   * and pray, that we are within [-0.5..0.5[
	   */
	  reftime = off = offset;
	  rectime = offset;
	  /*
	   * implied on second offset
	   */
	  off.l_uf = ~off.l_uf; /* map [0.5..1[ -> [-0.5..0[ */
	  off.l_ui = (off.l_f < 0) ? ~0 : 0; /* sign extend */
	}
    }
  else
    {
      if (!PARSE_TIMECODE(parsetime->parse_state))
	{
	  /*
	   * Well, no PPS, no TIMECODE, no more work ...
	   */
	  return;
	}
    }

    parse->generic->lasttime = current_time;

    if (!refclock_sample(&off, parse->generic, parse->parse_type->cl_samples, parse->parse_type->cl_keep))
      {
	parse_event(parse, CEVNT_BADTIME);
	return;
      }

#if defined(ATOM)
  if (CLK_PPS(parse->peer) && !parse->pollonly && PARSE_SYNC(parsetime->parse_state))
    {
      /*
       * only provide PPS information when clock
       * is in sync
       * thus PHASE and EPOCH are correct and PPS is not
       * done via the CIOGETEV loopfilter mechanism
       */
      if (fdpps != parse->generic->io.fd)
	(void) pps_sample(&parse->generic->offset);
    }
#endif /* ATOM */

  /*
   * ready, unless the machine wants a sample
   */
  if (!parse->pollonly && !parse->pollneeddata)
    return;

  if (PARSE_SYNC(parsetime->parse_state))
    {
      /*
       * log OK status
       */
       parse_event(parse, CEVNT_NOMINAL);
    }

  parse->pollneeddata = 0;

  clear_err(parse, ERR_BADIO);
  clear_err(parse, ERR_BADDATA);
  clear_err(parse, ERR_NODATA);
  clear_err(parse, ERR_INTERNAL);
  
  parse->generic->lastrec = rectime;
  parse->generic->lastref = reftime;
  
  /*
   * 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
   * see the clock states section above for more reasoning
   */
  if (((current_time - parse->lastsync) > parse->parse_type->cl_maxunsync) ||
      (parse->lastsync <= parse->lastmissed))
    {
      leap = LEAP_NOTINSYNC;
    }
  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).
	   */
	  leap = (parse->flags & PARSE_LEAP_DELETE) ? LEAP_DELSECOND : LEAP_ADDSECOND;
	}
      else
        if (PARSE_LEAPDEL(parsetime->parse_state))
	  {
	    leap = LEAP_DELSECOND;
	  }
	else
	  {
	    leap = LEAP_NOWARNING;
	  }
    }
  
  refclock_receive(parse->peer, &parse->generic->offset, 0,
		   parse->generic->dispersion, &reftime, &rectime,
		   leap | REFCLOCK_OWN_STATES);
}

/**===========================================================================
 ** clock polling support
 **/

struct poll_timer
{
  struct event timer;		/* we'd like to poll a a higher rate than 1/64s */
};

typedef struct poll_timer poll_timer_t;

/*--------------------------------------------------
 * direct poll routine
 */
static void
poll_dpoll(parse)
  struct parseunit *parse;
{
  int rtc;
  char *ps = ((poll_info_t *)parse->parse_type->cl_data)->string;
  int   ct = ((poll_info_t *)parse->parse_type->cl_data)->count;

  rtc = write(parse->generic->io.fd, ps, ct);
  if (rtc < 0)
    {
      ERR(ERR_BADIO)
	msyslog(LOG_ERR, "PARSE receiver #%d: poll_dpoll: failed to send cmd to clock: %m", CLK_UNIT(parse->peer));
    }
  else
    if (rtc != ct)
      {
	ERR(ERR_BADIO)
	  msyslog(LOG_ERR, "PARSE receiver #%d: poll_dpoll: failed to send cmd incomplete (%d of %d bytes sent)", CLK_UNIT(parse->peer), rtc, ct);
      }
  clear_err(parse, ERR_BADIO);
}

/*--------------------------------------------------
 * periodic poll routine
 */
static void
poll_poll(parse)
  struct parseunit *parse;
{
  poll_timer_t *pt = (poll_timer_t *)parse->localdata;

  poll_dpoll(parse);

  if (pt != (poll_timer_t *)0)
    {
      pt->timer.event_time = current_time + ((poll_info_t *)parse->parse_type->cl_data)->rate;
      TIMER_ENQUEUE(timerqueue, &pt->timer);
    }
}

/*--------------------------------------------------
 * init routine - setup timer
 */
static int
poll_init(parse)
  struct parseunit *parse;
{
  poll_timer_t *pt;

  if (((poll_info_t *)parse->parse_type->cl_data)->rate)
    {
      parse->localdata = (void *)emalloc(sizeof(poll_timer_t));
      memset((char *)parse->localdata, 0, sizeof(poll_timer_t));
  
      pt = (poll_timer_t *)parse->localdata;
      
      pt->timer.peer          = (struct peer *)parse; /* well, only we know what it is */
      pt->timer.event_handler = (void (*) P((struct peer *))) poll_poll;
      poll_poll(parse);
    }
  else
    {
      parse->localdata = (void *)0;
    }

  return 0;
}

/*--------------------------------------------------
 * end routine - clean up timer
 */
static void
poll_end(parse)
  struct parseunit *parse;
{
  if (parse->localdata != (void *)0)
    {
      TIMER_DEQUEUE(&((poll_timer_t *)parse->localdata)->timer);
      free((char *)parse->localdata);
      parse->localdata = (void *)0;
    }
}

/**===========================================================================
 ** special code for special clocks
 **/


/*--------------------------------------------------
 * trimble TAIP init routine - setup EOL and then do poll_init.
 */
static int
trimbletaip_init(parse)
  struct parseunit *parse;
{
#ifdef HAVE_TERMIOS
  struct termios tio;
#endif
#ifdef HAVE_SYSV_TTYS
  struct termio tio;
#endif
  /*
   * configure terminal line for trimble receiver
   */
  if (TTY_GETATTR(parse->generic->io.fd, &tio) == -1)
    {
      msyslog(LOG_ERR, "PARSE receiver #%d: trimbletaip_init: tcgetattr(fd, &tio): %m", CLK_UNIT(parse->peer));
      return 0;
    }
  else
    {
      tio.c_cc[VEOL] = TRIMBLETAIP_EOL;
	
      if (TTY_SETATTR(parse->generic->io.fd, &tio) == -1)
	{
	  msyslog(LOG_ERR, "PARSE receiver #%d: trimbletaip_init: tcsetattr(fd, &tio): %m", CLK_UNIT(parse->peer));
	  return 0;
	}
    }
  return poll_init(parse);
}

/*--------------------------------------------------
 * trimble TAIP event routine - reset receiver upon data format trouble
 */
static 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(parse, event)
  struct parseunit *parse;
  int event;
{
  switch (event)
    {
    case CEVNT_BADREPLY:	/* reset on garbled input */
    case CEVNT_TIMEOUT:		/* reset on no input */
      {
	char **iv;

	iv = taipinit;
	while (*iv)
	  {
	    int rtc = write(parse->generic->io.fd, *iv, strlen(*iv));
	    if (rtc < 0)
	      {
		msyslog(LOG_ERR, "PARSE receiver #%d: trimbletaip_event: failed to send cmd to clock: %m", CLK_UNIT(parse->peer));
		return;
	      }
	    else
	      {
		if (rtc != strlen(*iv))
		  {
		    msyslog(LOG_ERR, "PARSE receiver #%d: trimbletaip_event: failed to send cmd incomplete (%d of %d bytes sent)",
			   CLK_UNIT(parse->peer), rtc, strlen(*iv));
		    return;
		  }
	      }
	    iv++;
	  }

	NLOG(NLOG_CLOCKINFO)
	  ERR(ERR_BADIO)
	    msyslog(LOG_ERR, "PARSE receiver #%d: trimbletaip_event: RECEIVER INITIALIZED",
		   CLK_UNIT(parse->peer));
      }
      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 xntpd 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, sendint implement the command
 * interface to the receiver.
 *
 * CAVEAT: the sendflt, sendint routines are byte order dependend and
 * float implementation dependend - these must be converted to portable
 * versions !
 */

union {
    u_char  bd[8];
    int     iv;
    float   fv;
    double  dv;
}  uval;
  
struct txbuf
{
  short idx;			/* index to first unused byte */
  u_char *txt;			/* pointer to actual data buffer */
};

void
sendcmd(buf, c)
  struct txbuf *buf;
  u_char c;
{
  buf->txt[0] = DLE;
  buf->txt[1] = c;
  buf->idx = 2;
}

void sendbyte(buf, b)
  struct txbuf *buf;
  u_char b;
{
  if (b == DLE)
    buf->txt[buf->idx++] = DLE;
  buf->txt[buf->idx++] = b;
}

void
sendetx(buf, parse)
  struct txbuf *buf;
  struct parseunit *parse;
{
  buf->txt[buf->idx++] = DLE;
  buf->txt[buf->idx++] = ETX;

  if (write(parse->generic->io.fd, buf->txt, buf->idx) != buf->idx)
    {
      ERR(ERR_BADIO)
	msyslog(LOG_ERR, "PARSE receiver #%d: sendetx: failed to send cmd to clock: %m", CLK_UNIT(parse->peer));
    }
  else
    {
      clear_err(parse, ERR_BADIO);
    }
}

void  
sendint(buf, a)
  struct txbuf *buf;
  int a;
{
  /* send 16bit int, msbyte first */
  sendbyte(buf, (a>>8) & 0xff);
  sendbyte(buf, a & 0xff);
}

void
sendflt(buf, a)
  struct txbuf *buf;
  float a;
{
  int i;

  uval.fv = a;
#ifdef XNTP_BIG_ENDIAN
  for (i=0; i<=3; i++)
#else
  for (i=3; i>=0; i--)
#endif
    sendbyte(buf, uval.bd[i]);
}

/*--------------------------------------------------
 * trimble TSIP init routine
 */
static int
trimbletsip_init(parse)
  struct parseunit *parse;
{
  u_char buffer[256];
  struct txbuf buf;

  buf.txt = buffer;
  
  if (!poll_init(parse))
    {
      sendcmd(&buf, 0x1f);	/* request software versions */
      sendetx(&buf, parse);

      sendcmd(&buf, 0x2c);	/* 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, 0x22);	/* fix mode select */
      sendbyte(&buf, 0);	/* automatic */
      sendetx(&buf, parse);

      sendcmd(&buf, 0x28);	/* request system message */
      sendetx(&buf, parse);

      sendcmd(&buf, 0x8e);	/* superpacket fix */
      sendbyte(&buf, 0x2);	/* binary mode */
      sendetx(&buf, parse);

      sendcmd(&buf, 0x35);	/* set I/O options */
      sendbyte(&buf, 0);	/* no position output */
      sendbyte(&buf, 0);	/* no velocity output */
      sendbyte(&buf, 7);	/* UTC, compute on seconds, send only on request */
      sendbyte(&buf, 0);	/* no raw measurements */
      sendetx(&buf, parse);

      sendcmd(&buf, 0x2f);	/* request UTC correction data */
      sendetx(&buf, parse);
      return 0;
    }
  else
    return 1;
}

#if defined(RAWDCF_SETDTR)
/*--------------------------------------------------
 * rawdcf_init - set up modem lines for RAWDCF receivers
 */
#if defined(TIOCMSET) && (defined(TIOCM_DTR) || defined(CIOCM_DTR))
static int
rawdcf_init(parse)
  struct parseunit *parse;
{
  /*
   * 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.
   */
	
#ifdef TIOCM_DTR
  int sl232 = TIOCM_DTR;	/* turn on DTR for power supply */
#else
  int sl232 = CIOCM_DTR;	/* turn on DTR for power supply */
#endif

  if (ioctl(parse->generic->io.fd, TIOCMSET, &sl232) == -1)
    {
      msyslog(LOG_NOTICE, "PARSE receiver #%d: rawdcf_init: WARNING: ioctl(fd, TIOCMSET, [C|T]IOCM_DTR): %m", CLK_UNIT(parse->peer));
    }
  return 0;
}
#else
static int
rawdcf_init(parse)
  struct parseunit *parse;
{
  msyslog(LOG_NOTICE, "PARSE receiver #%d: rawdcf_init: WARNING: OS interface incapable of setting DTR to power DCF modules", CLK_UNIT(parse->peer));
  return 0;
}
#endif  /* DTR initialisation type */

#endif  /* RAWDCF SET DTR option */

#else	/* defined(REFCLOCK) && defined(PARSE) */
int refclock_parse_bs;
#endif	/* defined(REFCLOCK) && defined(PARSE) */

/*
 * History:
 *
 * refclock_parse.c,v
 * Revision 3.103  1997/07/12 15:35:16  kardel
 * fixed allocation failure for refclock_states string
 *
 * Revision 3.102  1997/07/06 14:11:55  kardel
 * incread internal buffers
 * changed filter paramters for PZF535 and GPS166 for more filtering
 * removed old useless code fragments
 *
 * Revision 3.101  1997/04/13 10:05:37  kardel
 * 3.5.90 reconcilation
 *
 * Revision 3.100  1997/04/06 17:37:29  kardel
 * Make Hopf clock a fixed format to cope with Meinberg clocks
 *
 * Revision 3.99  1997/02/08 00:14:11  kardel
 * 3.5.89.3 reconcilation
 *
 * Revision 3.98  1997/01/19 14:11:59  kardel
 * removed superfluous functions
 *
 * Revision 3.97  1997/01/19 12:46:01  kardel
 * 3-5.88.1 reconcilation
 *
 * Revision 3.96  1996/12/01 16:05:49  kardel
 * freeze for 5.86.12.2 PARSE-Patch
 *
 * Revision 3.95  1996/12/01 13:13:47  kardel
 * POP "parse" streams module at shutdown
 *
 * Revision 3.94  1996/11/24 20:10:54  kardel
 * RELEASE_5_86_12_2 reconcilation
 *
 * Revision 3.93  1996/11/16 19:13:47  kardel
 * Added DIEM receiver
 *
 * Revision 3.92  1996/10/05 13:30:29  kardel
 * general update
 *
 * Revision 3.91  1996/06/01 17:05:53  kardel
 * cut back revision log
 *
 * Revision 3.90  1996/06/01 16:45:19  kardel
 * cleaned up DTR setup for RAWDCF
 *
 * Revision 3.88  1995/12/18 00:11:24  kardel
 * undo RCC8000 modification
 *
 * Revision 3.87  1995/12/17  22:54:34  kardel
 * fixed HOPF description
 *
 * Revision 3.86  1995/12/17  22:24:29  kardel
 * HOPF 6021 Funkuhr added
 *
 * Revision 3.85  1995/10/16  01:49:50  duwe
 * look for sys/ppsclock.h in the right place
 *
 * Revision 3.84  1995/10/06  15:58:01  kardel
 * auto init for Trimble TAIP
 *
 * Revision 3.83  1995/09/09  16:52:26  kardel
 * more space for refclock variables
 *
 * Revision 3.82  1995/09/03  17:54:47  kardel
 * use unsigned characters for input
 *
 * Revision 3.81  1995/08/26  07:27:20  kardel
 * Trimble TAIP NOPOLL + RECOVERY
 *
 * Revision 3.80  1995/07/29  08:04:17  kardel
 * unsigned char/int problem
 *
 * Revision information 3.1 - 3.79 from log deleted 1996/06/01 kardel
 *
 */