1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
|
/*
* refclock_jjy - clock driver for JJY receivers
*/
/**********************************************************************/
/* */
/* Copyright (C) 2001-2015, Takao Abe. All rights reserved. */
/* */
/* Permission to use, copy, modify, and distribute this software */
/* and its documentation for any purpose is hereby granted */
/* without fee, provided that the following conditions are met: */
/* */
/* One retains the entire copyright notice properly, and both the */
/* copyright notice and this license. in the documentation and/or */
/* other materials provided with the distribution. */
/* */
/* This software and the name of the author must not be used to */
/* endorse or promote products derived from this software without */
/* prior written permission. */
/* */
/* THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESSED OR IMPLIED */
/* WARRANTIES OF ANY KIND, INCLUDING, BUT NOT LIMITED TO, THE */
/* IMPLIED WARRANTIES OF MERCHANTABLILITY AND FITNESS FOR A */
/* PARTICULAR PURPOSE. */
/* IN NO EVENT SHALL THE AUTHOR TAKAO ABE BE LIABLE FOR ANY DIRECT, */
/* INDIRECT, GENERAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
/* ( INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
/* GOODS OR SERVICES; LOSS OF USE, DATA OR PROFITS; OR BUSINESS */
/* INTERRUPTION ) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, */
/* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ( INCLUDING */
/* NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF */
/* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/* This driver is developed in my private time, and is opened as */
/* voluntary contributions for the NTP. */
/* The manufacturer of the JJY receiver has not participated in */
/* a development of this driver. */
/* The manufacturer does not warrant anything about this driver, */
/* and is not liable for anything about this driver. */
/* */
/**********************************************************************/
/* */
/* Author Takao Abe */
/* Email takao_abe@xurb.jp */
/* Homepage http://www.bea.hi-ho.ne.jp/abetakao/ */
/* */
/* The email address abetakao@bea.hi-ho.ne.jp is never read */
/* from 2010, because a few filtering rule are provided by the */
/* "hi-ho.ne.jp", and lots of spam mail are reached. */
/* New email address for supporting the refclock_jjy is */
/* takao_abe@xurb.jp */
/* */
/**********************************************************************/
/* */
/* History */
/* */
/* 2001/07/15 */
/* [New] Support the Tristate Ltd. JJY receiver */
/* */
/* 2001/08/04 */
/* [Change] Log to clockstats even if bad reply */
/* [Fix] PRECISION = (-3) (about 100 ms) */
/* [Add] Support the C-DEX Co.Ltd. JJY receiver */
/* */
/* 2001/12/04 */
/* [Fix] C-DEX JST2000 ( fukusima@goto.info.waseda.ac.jp ) */
/* */
/* 2002/07/12 */
/* [Fix] Portability for FreeBSD ( patched by the user ) */
/* */
/* 2004/10/31 */
/* [Change] Command send timing for the Tristate Ltd. JJY receiver */
/* JJY-01 ( Firmware version 2.01 ) */
/* Thanks to Andy Taki for testing under FreeBSD */
/* */
/* 2004/11/28 */
/* [Add] Support the Echo Keisokuki LT-2000 receiver */
/* */
/* 2006/11/04 */
/* [Fix] C-DEX JST2000 */
/* Thanks to Hideo Kuramatsu for the patch */
/* */
/* 2009/04/05 */
/* [Add] Support the CITIZEN T.I.C JJY-200 receiver */
/* */
/* 2010/11/20 */
/* [Change] Bug 1618 ( Harmless ) */
/* Code clean up ( Remove unreachable codes ) in */
/* jjy_start() */
/* [Change] Change clockstats format of the Tristate JJY01/02 */
/* Issues more command to get the status of the receiver */
/* when "fudge 127.127.40.X flag1 1" is specified */
/* ( DATE,STIM -> DCST,STUS,DATE,STIM ) */
/* */
/* 2011/04/30 */
/* [Add] Support the Tristate Ltd. TS-GPSclock-01 */
/* */
/* 2015/03/29 */
/* [Add] Support the Telephone JJY */
/* [Change] Split the start up routine into each JJY receivers. */
/* Change raw data internal bufferring process */
/* Change over midnight handling of TS-JJY01 and TS-GPS01 */
/* to put DATE command between before and after TIME's. */
/* Unify the writing clockstats of all JJY receivers. */
/* */
/* 2015/05/15 */
/* [Add] Support the SEIKO TIME SYSTEMS TDC-300 */
/* */
/* 2016/05/08 */
/* [Fix] C-DEX JST2000 */
/* Thanks to Mr. Kuramatsu for the report and the patch. */
/* */
/**********************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#if defined(REFCLOCK) && defined(CLOCK_JJY)
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include "ntpd.h"
#include "ntp_io.h"
#include "ntp_tty.h"
#include "ntp_refclock.h"
#include "ntp_calendar.h"
#include "ntp_stdlib.h"
/**********************************************************************/
/*
* Interface definitions
*/
#define DEVICE "/dev/jjy%d" /* device name and unit */
#define SPEED232_TRISTATE_JJY01 B9600 /* UART speed (9600 baud) */
#define SPEED232_CDEX_JST2000 B9600 /* UART speed (9600 baud) */
#define SPEED232_ECHOKEISOKUKI_LT2000 B9600 /* UART speed (9600 baud) */
#define SPEED232_CITIZENTIC_JJY200 B4800 /* UART speed (4800 baud) */
#define SPEED232_TRISTATE_GPSCLOCK01 B38400 /* USB speed (38400 baud) */
#define SPEED232_SEIKO_TIMESYS_TDC_300 B2400 /* UART speed (2400 baud) */
#define SPEED232_TELEPHONE B2400 /* UART speed (4800 baud) */
#define REFID "JJY" /* reference ID */
#define DESCRIPTION "JJY Receiver"
#define PRECISION (-3) /* precision assumed (about 100 ms) */
/*
* JJY unit control structure
*/
struct jjyRawDataBreak {
const char * pString ;
int iLength ;
} ;
#define MAX_TIMESTAMP 6
#define MAX_RAWBUF 100
#define MAX_LOOPBACK 5
struct jjyunit {
/* Set up by the function "jjy_start_xxxxxxxx" */
char unittype ; /* UNITTYPE_XXXXXXXXXX */
short operationmode ; /* Echo Keisokuki LT-2000 */
int linespeed ; /* SPEED232_XXXXXXXXXX */
short linediscipline ; /* LDISC_CLK or LDISC_RAW */
/* Receiving data */
char bInitError ; /* Set by jjy_start if any error during initialization */
short iProcessState ; /* JJY_PROCESS_STATE_XXXXXX */
char bReceiveFlag ; /* Set and reset by jjy_receive */
char bLineError ; /* Reset by jjy_poll / Set by jjy_receive_xxxxxxxx*/
short iCommandSeq ; /* 0:Idle Non-Zero:Issued */
short iReceiveSeq ;
int iLineCount ;
int year, month, day, hour, minute, second, msecond ;
int leapsecond ;
int iTimestampCount ; /* TS-JJY01, TS-GPS01, Telephone-JJY */
int iTimestamp [ MAX_TIMESTAMP ] ; /* Serial second ( 0 - 86399 ) */
/* LDISC_RAW only */
char sRawBuf [ MAX_RAWBUF ] ;
int iRawBufLen ;
struct jjyRawDataBreak *pRawBreak ;
char bWaitBreakString ;
char sLineBuf [ MAX_RAWBUF ] ;
int iLineBufLen ;
char sTextBuf [ MAX_RAWBUF ] ;
int iTextBufLen ;
char bSkipCntrlCharOnly ;
/* Telephone JJY auto measurement of the loopback delay */
char bLoopbackMode ;
short iLoopbackCount ;
struct timeval sendTime[MAX_LOOPBACK], delayTime[MAX_LOOPBACK] ;
char bLoopbackTimeout[MAX_LOOPBACK] ;
short iLoopbackValidCount ;
/* Telephone JJY timer */
short iTeljjySilentTimer ;
short iTeljjyStateTimer ;
/* Telephone JJY control finite state machine */
short iClockState ;
short iClockEvent ;
short iClockCommandSeq ;
/* Modem timer */
short iModemSilentCount ;
short iModemSilentTimer ;
short iModemStateTimer ;
/* Modem control finite state machine */
short iModemState ;
short iModemEvent ;
short iModemCommandSeq ;
};
#define UNITTYPE_TRISTATE_JJY01 1
#define UNITTYPE_CDEX_JST2000 2
#define UNITTYPE_ECHOKEISOKUKI_LT2000 3
#define UNITTYPE_CITIZENTIC_JJY200 4
#define UNITTYPE_TRISTATE_GPSCLOCK01 5
#define UNITTYPE_SEIKO_TIMESYS_TDC_300 6
#define UNITTYPE_TELEPHONE 100
#define JJY_PROCESS_STATE_IDLE 0
#define JJY_PROCESS_STATE_POLL 1
#define JJY_PROCESS_STATE_RECEIVE 2
#define JJY_PROCESS_STATE_DONE 3
#define JJY_PROCESS_STATE_ERROR 4
/**********************************************************************/
/*
* Function calling structure
*
* jjy_start
* |-- jjy_start_tristate_jjy01
* |-- jjy_start_cdex_jst2000
* |-- jjy_start_echokeisokuki_lt2000
* |-- jjy_start_citizentic_jjy200
* |-- jjy_start_tristate_gpsclock01
* |-- jjy_start_seiko_tsys_tdc_300
* |-- jjy_start_telephone
*
* jjy_shutdown
*
* jjy_poll
* |-- jjy_poll_tristate_jjy01
* |-- jjy_poll_cdex_jst2000
* |-- jjy_poll_echokeisokuki_lt2000
* |-- jjy_poll_citizentic_jjy200
* |-- jjy_poll_tristate_gpsclock01
* |-- jjy_poll_seiko_tsys_tdc_300
* |-- jjy_poll_telephone
* |-- teljjy_control
* |-- teljjy_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
* |-- modem_connect
* |-- modem_control
* |-- modem_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
*
* jjy_receive
* |
* |-- jjy_receive_tristate_jjy01
* | |-- jjy_synctime
* |-- jjy_receive_cdex_jst2000
* | |-- jjy_synctime
* |-- jjy_receive_echokeisokuki_lt2000
* | |-- jjy_synctime
* |-- jjy_receive_citizentic_jjy200
* | |-- jjy_synctime
* |-- jjy_receive_tristate_gpsclock01
* | |-- jjy_synctime
* |-- jjy_receive_seiko_tsys_tdc_300
* | |-- jjy_synctime
* |-- jjy_receive_telephone
* |-- modem_receive
* | |-- modem_control
* | |-- modem_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
* |-- teljjy_control
* |-- teljjy_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
* |-- jjy_synctime
* |-- modem_disconnect
* |-- modem_control
* |-- modem_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
*
* jjy_timer
* |-- jjy_timer_telephone
* |-- modem_timer
* | |-- modem_control
* | |-- modem_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
* |-- teljjy_control
* |-- teljjy_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
* |-- modem_disconnect
* |-- modem_control
* |-- modem_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
*
* Function prototypes
*/
static int jjy_start (int, struct peer *);
static int jjy_start_tristate_jjy01 (int, struct peer *, struct jjyunit *);
static int jjy_start_cdex_jst2000 (int, struct peer *, struct jjyunit *);
static int jjy_start_echokeisokuki_lt2000 (int, struct peer *, struct jjyunit *);
static int jjy_start_citizentic_jjy200 (int, struct peer *, struct jjyunit *);
static int jjy_start_tristate_gpsclock01 (int, struct peer *, struct jjyunit *);
static int jjy_start_seiko_tsys_tdc_300 (int, struct peer *, struct jjyunit *);
static int jjy_start_telephone (int, struct peer *, struct jjyunit *);
static void jjy_shutdown (int, struct peer *);
static void jjy_poll (int, struct peer *);
static void jjy_poll_tristate_jjy01 (int, struct peer *);
static void jjy_poll_cdex_jst2000 (int, struct peer *);
static void jjy_poll_echokeisokuki_lt2000 (int, struct peer *);
static void jjy_poll_citizentic_jjy200 (int, struct peer *);
static void jjy_poll_tristate_gpsclock01 (int, struct peer *);
static void jjy_poll_seiko_tsys_tdc_300 (int, struct peer *);
static void jjy_poll_telephone (int, struct peer *);
static void jjy_receive (struct recvbuf *);
static int jjy_receive_tristate_jjy01 (struct recvbuf *);
static int jjy_receive_cdex_jst2000 (struct recvbuf *);
static int jjy_receive_echokeisokuki_lt2000 (struct recvbuf *);
static int jjy_receive_citizentic_jjy200 (struct recvbuf *);
static int jjy_receive_tristate_gpsclock01 (struct recvbuf *);
static int jjy_receive_seiko_tsys_tdc_300 (struct recvbuf *);
static int jjy_receive_telephone (struct recvbuf *);
static void jjy_timer (int, struct peer *);
static void jjy_timer_telephone (int, struct peer *);
static void jjy_synctime ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static void jjy_write_clockstats ( struct peer *, int, const char* ) ;
static int getRawDataBreakPosition ( struct jjyunit *, int ) ;
static short getModemState ( struct jjyunit * ) ;
static int isModemStateConnect ( short ) ;
static int isModemStateDisconnect ( short ) ;
static int isModemStateTimerOn ( struct jjyunit * ) ;
static void modem_connect ( int, struct peer * ) ;
static void modem_disconnect ( int, struct peer * ) ;
static int modem_receive ( struct recvbuf * ) ;
static void modem_timer ( int, struct peer * );
static void printableString ( char*, int, const char*, int ) ;
/*
* Transfer vector
*/
struct refclock refclock_jjy = {
jjy_start, /* start up driver */
jjy_shutdown, /* shutdown driver */
jjy_poll, /* transmit poll message */
noentry, /* not used */
noentry, /* not used */
noentry, /* not used */
jjy_timer /* 1 second interval timer */
};
/*
* Start up driver return code
*/
#define RC_START_SUCCESS 1
#define RC_START_ERROR 0
/*
* Local constants definition
*/
#define MAX_LOGTEXT 100
#ifndef TRUE
#define TRUE (0==0)
#endif
#ifndef FALSE
#define FALSE (!TRUE)
#endif
/* Local constants definition for the return code of the jjy_receive_xxxxxxxx */
#define JJY_RECEIVE_DONE 0
#define JJY_RECEIVE_SKIP 1
#define JJY_RECEIVE_UNPROCESS 2
#define JJY_RECEIVE_WAIT 3
#define JJY_RECEIVE_ERROR 4
/* Local constants definition for the 2nd parameter of the jjy_write_clockstats */
#define JJY_CLOCKSTATS_MARK_NONE 0
#define JJY_CLOCKSTATS_MARK_JJY 1
#define JJY_CLOCKSTATS_MARK_SEND 2
#define JJY_CLOCKSTATS_MARK_RECEIVE 3
#define JJY_CLOCKSTATS_MARK_INFORMATION 4
#define JJY_CLOCKSTATS_MARK_ATTENTION 5
#define JJY_CLOCKSTATS_MARK_WARNING 6
#define JJY_CLOCKSTATS_MARK_ERROR 7
/* Local constants definition for the clockstats messages */
#define JJY_CLOCKSTATS_MESSAGE_ECHOBACK "* Echoback"
#define JJY_CLOCKSTATS_MESSAGE_IGNORE_REPLY "* Ignore replay : [%s]"
#define JJY_CLOCKSTATS_MESSAGE_OVER_MIDNIGHT_2 "* Over midnight : timestamp=%d, %d"
#define JJY_CLOCKSTATS_MESSAGE_OVER_MIDNIGHT_3 "* Over midnight : timestamp=%d, %d, %d"
#define JJY_CLOCKSTATS_MESSAGE_TIMESTAMP_UNSURE "* Unsure timestamp : %s"
#define JJY_CLOCKSTATS_MESSAGE_LOOPBACK_DELAY "* Loopback delay : %d.%03d mSec."
#define JJY_CLOCKSTATS_MESSAGE_DELAY_ADJUST "* Delay adjustment : %d mSec. ( valid=%hd/%d )"
#define JJY_CLOCKSTATS_MESSAGE_DELAY_UNADJUST "* Delay adjustment : None ( valid=%hd/%d )"
#define JJY_CLOCKSTATS_MESSAGE_UNEXPECTED_REPLY "# Unexpected reply : [%s]"
#define JJY_CLOCKSTATS_MESSAGE_INVALID_LENGTH "# Invalid length : length=%d"
#define JJY_CLOCKSTATS_MESSAGE_TOO_MANY_REPLY "# Too many reply : count=%d"
#define JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY "# Invalid reply : [%s]"
#define JJY_CLOCKSTATS_MESSAGE_SLOW_REPLY_2 "# Slow reply : timestamp=%d, %d"
#define JJY_CLOCKSTATS_MESSAGE_SLOW_REPLY_3 "# Slow reply : timestamp=%d, %d, %d"
#define JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATE "# Invalid date : rc=%d year=%d month=%d day=%d"
#define JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_TIME "# Invalid time : rc=%d hour=%d minute=%d second=%d"
#define JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATETIME "# Invalid time : rc=%d year=%d month=%d day=%d hour=%d minute=%d second=%d"
#define JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_LEAP "# Invalid leap : leapsecond=[%s]"
#define JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_STATUS "# Invalid status : status=[%s]"
/* Debug print macro */
#ifdef DEBUG
#define DEBUG_PRINTF_JJY_RECEIVE(sFunc,iLen) { if ( debug ) { printf ( "refclock_jjy.c : %s : iProcessState=%d bLineError=%d iCommandSeq=%d iLineCount=%d iTimestampCount=%d iLen=%d\n", sFunc, up->iProcessState, up->bLineError, up->iCommandSeq, up->iLineCount, up->iTimestampCount, iLen ) ; } }
#else
#define DEBUG_PRINTF_JJY_RECEIVE(sFunc,iLen)
#endif
/**************************************************************************************************/
/* jjy_start - open the devices and initialize data for processing */
/**************************************************************************************************/
static int
jjy_start ( int unit, struct peer *peer )
{
struct refclockproc *pp ;
struct jjyunit *up ;
int rc ;
int fd ;
char sDeviceName [ sizeof(DEVICE) + 10 ], sLog [ 60 ] ;
#ifdef DEBUG
if ( debug ) {
printf( "refclock_jjy.c : jjy_start : %s mode=%d dev=%s unit=%d\n",
ntoa(&peer->srcadr), peer->ttl, DEVICE, unit ) ;
}
#endif
/* Allocate memory for the unit structure */
up = emalloc( sizeof(*up) ) ;
if ( up == NULL ) {
msyslog ( LOG_ERR, "refclock_jjy.c : jjy_start : emalloc" ) ;
return RC_START_ERROR ;
}
memset ( up, 0, sizeof(*up) ) ;
up->bInitError = FALSE ;
up->iProcessState = JJY_PROCESS_STATE_IDLE ;
up->bReceiveFlag = FALSE ;
up->iCommandSeq = 0 ;
up->iLineCount = 0 ;
up->iTimestampCount = 0 ;
up->bWaitBreakString = FALSE ;
up->iRawBufLen = up->iLineBufLen = up->iTextBufLen = 0 ;
up->bSkipCntrlCharOnly = TRUE ;
/* Set up the device name */
snprintf( sDeviceName, sizeof(sDeviceName), DEVICE, unit ) ;
snprintf( sLog, sizeof(sLog), "mode=%d dev=%s", peer->ttl, sDeviceName ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, sLog ) ;
/*
* peer->ttl is a mode number specified by "127.127.40.X mode N" in the ntp.conf
*/
switch ( peer->ttl ) {
case 0 :
case 1 :
rc = jjy_start_tristate_jjy01 ( unit, peer, up ) ;
break ;
case 2 :
rc = jjy_start_cdex_jst2000 ( unit, peer, up ) ;
break ;
case 3 :
rc = jjy_start_echokeisokuki_lt2000 ( unit, peer, up ) ;
break ;
case 4 :
rc = jjy_start_citizentic_jjy200 ( unit, peer, up ) ;
break ;
case 5 :
rc = jjy_start_tristate_gpsclock01 ( unit, peer, up ) ;
break ;
case 6 :
rc = jjy_start_seiko_tsys_tdc_300 ( unit, peer, up ) ;
break ;
case 100 :
rc = jjy_start_telephone ( unit, peer, up ) ;
break ;
default :
if ( 101 <= peer->ttl && peer->ttl <= 180 ) {
rc = jjy_start_telephone ( unit, peer, up ) ;
} else {
msyslog ( LOG_ERR, "JJY receiver [ %s mode %d ] : Unsupported mode",
ntoa(&peer->srcadr), peer->ttl ) ;
free ( (void*) up ) ;
return RC_START_ERROR ;
}
}
if ( rc != 0 ) {
msyslog ( LOG_ERR, "JJY receiver [ %s mode %d ] : Initialize error",
ntoa(&peer->srcadr), peer->ttl ) ;
free ( (void*) up ) ;
return RC_START_ERROR ;
}
/* Open the device */
fd = refclock_open ( sDeviceName, up->linespeed, up->linediscipline ) ;
if ( fd <= 0 ) {
free ( (void*) up ) ;
return RC_START_ERROR ;
}
/*
* Initialize variables
*/
pp = peer->procptr ;
pp->clockdesc = DESCRIPTION ;
pp->unitptr = up ;
pp->io.clock_recv = jjy_receive ;
pp->io.srcclock = peer ;
pp->io.datalen = 0 ;
pp->io.fd = fd ;
if ( ! io_addclock(&pp->io) ) {
close ( fd ) ;
pp->io.fd = -1 ;
free ( up ) ;
pp->unitptr = NULL ;
return RC_START_ERROR ;
}
memcpy( (char*)&pp->refid, REFID, strlen(REFID) ) ;
peer->precision = PRECISION ;
snprintf( sLog, sizeof(sLog), "minpoll=%d maxpoll=%d", peer->minpoll, peer->maxpoll ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, sLog ) ;
return RC_START_SUCCESS ;
}
/**************************************************************************************************/
/* jjy_shutdown - shutdown the clock */
/**************************************************************************************************/
static void
jjy_shutdown ( int unit, struct peer *peer )
{
struct jjyunit *up;
struct refclockproc *pp;
char sLog [ 60 ] ;
pp = peer->procptr ;
up = pp->unitptr ;
if ( -1 != pp->io.fd ) {
io_closeclock ( &pp->io ) ;
}
if ( NULL != up ) {
free ( up ) ;
}
snprintf( sLog, sizeof(sLog), "JJY stopped. unit=%d mode=%d", unit, peer->ttl ) ;
record_clock_stats( &peer->srcadr, sLog ) ;
}
/**************************************************************************************************/
/* jjy_receive - receive data from the serial interface */
/**************************************************************************************************/
static void
jjy_receive ( struct recvbuf *rbufp )
{
#ifdef DEBUG
static const char *sFunctionName = "jjy_receive" ;
#endif
struct jjyunit *up ;
struct refclockproc *pp ;
struct peer *peer;
l_fp tRecvTimestamp; /* arrival timestamp */
int rc ;
char *pBuf, sLogText [ MAX_LOGTEXT ] ;
size_t iLen, iCopyLen ;
int i, j, iReadRawBuf, iBreakPosition ;
/*
* Initialize pointers and read the timecode and timestamp
*/
peer = rbufp->recv_peer ;
pp = peer->procptr ;
up = pp->unitptr ;
/*
* Get next input line
*/
if ( up->linediscipline == LDISC_RAW ) {
pp->lencode = refclock_gtraw ( rbufp, pp->a_lastcode, BMAX-1, &tRecvTimestamp ) ;
/* 3rd argument can be BMAX, but the coverity scan tool claim "Memory - corruptions (OVERRUN)" */
/* "a_lastcode" is defined as "char a_lastcode[BMAX]" in the ntp_refclock.h */
/* To avoid its claim, pass the value BMAX-1. */
/*
* Append received charaters to temporary buffer
*/
for ( i = 0 ;
i < pp->lencode && up->iRawBufLen < MAX_RAWBUF - 2 ;
i ++ , up->iRawBufLen ++ ) {
up->sRawBuf[up->iRawBufLen] = pp->a_lastcode[i] ;
}
up->sRawBuf[up->iRawBufLen] = 0 ;
} else {
pp->lencode = refclock_gtlin ( rbufp, pp->a_lastcode, BMAX, &tRecvTimestamp ) ;
}
#ifdef DEBUG
printf( "\nrefclock_jjy.c : %s : Len=%d ", sFunctionName, pp->lencode ) ;
for ( i = 0 ; i < pp->lencode ; i ++ ) {
if ( iscntrl( (u_char)(pp->a_lastcode[i] & 0x7F) ) ) {
printf( "<x%02X>", pp->a_lastcode[i] & 0xFF ) ;
} else {
printf( "%c", pp->a_lastcode[i] ) ;
}
}
printf( "\n" ) ;
#endif
/*
* The reply with <CR><LF> gives a blank line
*/
if ( pp->lencode == 0 ) return ;
/*
* Receiving data is not expected
*/
if ( up->iProcessState == JJY_PROCESS_STATE_IDLE
|| up->iProcessState == JJY_PROCESS_STATE_DONE
|| up->iProcessState == JJY_PROCESS_STATE_ERROR ) {
/* Discard received data */
up->iRawBufLen = 0 ;
#ifdef DEBUG
if ( debug ) {
printf( "refclock_jjy.c : %s : Discard received data\n", sFunctionName ) ;
}
#endif
return ;
}
/*
* We get down to business
*/
pp->lastrec = tRecvTimestamp ;
up->iLineCount ++ ;
up->iProcessState = JJY_PROCESS_STATE_RECEIVE ;
up->bReceiveFlag = TRUE ;
iReadRawBuf = 0 ;
iBreakPosition = up->iRawBufLen - 1 ;
for ( ; up->iProcessState == JJY_PROCESS_STATE_RECEIVE ; ) {
if ( up->linediscipline == LDISC_RAW ) {
if ( up->bWaitBreakString ) {
iBreakPosition = getRawDataBreakPosition( up, iReadRawBuf ) ;
if ( iBreakPosition == -1 ) {
/* Break string have not come yet */
if ( up->iRawBufLen < MAX_RAWBUF - 2
|| iReadRawBuf > 0 ) {
/* Temporary buffer is not full */
break ;
} else {
/* Temporary buffer is full */
iBreakPosition = up->iRawBufLen - 1 ;
}
}
} else {
iBreakPosition = up->iRawBufLen - 1 ;
}
/* Copy charaters from temporary buffer to process buffer */
up->iLineBufLen = up->iTextBufLen = 0 ;
for ( i = iReadRawBuf ; i <= iBreakPosition ; i ++ ) {
/* Copy all characters */
up->sLineBuf[up->iLineBufLen] = up->sRawBuf[i] ;
up->iLineBufLen ++ ;
/* Copy printable characters */
if ( ! iscntrl( (u_char)up->sRawBuf[i] ) ) {
up->sTextBuf[up->iTextBufLen] = up->sRawBuf[i] ;
up->iTextBufLen ++ ;
}
}
up->sLineBuf[up->iLineBufLen] = 0 ;
up->sTextBuf[up->iTextBufLen] = 0 ;
#ifdef DEBUG
printf( "refclock_jjy.c : %s : up->iLineBufLen=%d up->iTextBufLen=%d\n",
sFunctionName, up->iLineBufLen, up->iTextBufLen ) ;
#endif
if ( up->bSkipCntrlCharOnly && up->iTextBufLen == 0 ) {
#ifdef DEBUG
printf( "refclock_jjy.c : %s : Skip cntrl char only : up->iRawBufLen=%d iReadRawBuf=%d iBreakPosition=%d\n",
sFunctionName, up->iRawBufLen, iReadRawBuf, iBreakPosition ) ;
#endif
if ( iBreakPosition + 1 < up->iRawBufLen ) {
iReadRawBuf = iBreakPosition + 1 ;
continue ;
} else {
break ;
}
}
}
if ( up->linediscipline == LDISC_RAW ) {
pBuf = up->sLineBuf ;
iLen = up->iLineBufLen ;
} else {
pBuf = pp->a_lastcode ;
iLen = pp->lencode ;
}
iCopyLen = ( iLen <= sizeof(sLogText)-1 ? iLen : sizeof(sLogText)-1 ) ;
memcpy( sLogText, pBuf, iCopyLen ) ;
sLogText[iCopyLen] = '\0' ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_RECEIVE, sLogText ) ;
switch ( up->unittype ) {
case UNITTYPE_TRISTATE_JJY01 :
rc = jjy_receive_tristate_jjy01 ( rbufp ) ;
break ;
case UNITTYPE_CDEX_JST2000 :
rc = jjy_receive_cdex_jst2000 ( rbufp ) ;
break ;
case UNITTYPE_ECHOKEISOKUKI_LT2000 :
rc = jjy_receive_echokeisokuki_lt2000 ( rbufp ) ;
break ;
case UNITTYPE_CITIZENTIC_JJY200 :
rc = jjy_receive_citizentic_jjy200 ( rbufp ) ;
break ;
case UNITTYPE_TRISTATE_GPSCLOCK01 :
rc = jjy_receive_tristate_gpsclock01 ( rbufp ) ;
break ;
case UNITTYPE_SEIKO_TIMESYS_TDC_300 :
rc = jjy_receive_seiko_tsys_tdc_300 ( rbufp ) ;
break ;
case UNITTYPE_TELEPHONE :
rc = jjy_receive_telephone ( rbufp ) ;
break ;
default :
rc = JJY_RECEIVE_ERROR ;
break ;
}
switch ( rc ) {
case JJY_RECEIVE_DONE :
case JJY_RECEIVE_SKIP :
up->iProcessState = JJY_PROCESS_STATE_DONE ;
break ;
case JJY_RECEIVE_ERROR :
up->iProcessState = JJY_PROCESS_STATE_ERROR ;
break ;
default :
break ;
}
if ( up->linediscipline == LDISC_RAW ) {
if ( rc == JJY_RECEIVE_UNPROCESS ) {
break ;
}
iReadRawBuf = iBreakPosition + 1 ;
if ( iReadRawBuf >= up->iRawBufLen ) {
/* Processed all received data */
break ;
}
}
if ( up->linediscipline == LDISC_CLK ) {
break ;
}
}
if ( up->linediscipline == LDISC_RAW && iReadRawBuf > 0 ) {
for ( i = 0, j = iReadRawBuf ; j < up->iRawBufLen ; i ++, j++ ) {
up->sRawBuf[i] = up->sRawBuf[j] ;
}
up->iRawBufLen -= iReadRawBuf ;
if ( up->iRawBufLen < 0 ) {
up->iRawBufLen = 0 ;
}
}
up->bReceiveFlag = FALSE ;
}
/**************************************************************************************************/
static int
getRawDataBreakPosition ( struct jjyunit *up, int iStart )
{
int i, j ;
if ( iStart >= up->iRawBufLen ) {
#ifdef DEBUG
printf( "refclock_jjy.c : getRawDataBreakPosition : iStart=%d return=-1\n", iStart ) ;
#endif
return -1 ;
}
for ( i = iStart ; i < up->iRawBufLen ; i ++ ) {
for ( j = 0 ; up->pRawBreak[j].pString != NULL ; j ++ ) {
if ( i + up->pRawBreak[j].iLength <= up->iRawBufLen ) {
if ( strncmp( up->sRawBuf + i,
up->pRawBreak[j].pString,
up->pRawBreak[j].iLength ) == 0 ) {
#ifdef DEBUG
printf( "refclock_jjy.c : getRawDataBreakPosition : iStart=%d return=%d\n",
iStart, i + up->pRawBreak[j].iLength - 1 ) ;
#endif
return i + up->pRawBreak[j].iLength - 1 ;
}
}
}
}
#ifdef DEBUG
printf( "refclock_jjy.c : getRawDataBreakPosition : iStart=%d return=-1\n", iStart ) ;
#endif
return -1 ;
}
/**************************************************************************************************/
/* jjy_poll - called by the transmit procedure */
/**************************************************************************************************/
static void
jjy_poll ( int unit, struct peer *peer )
{
char sLog [ 40 ], sReach [ 9 ] ;
struct jjyunit *up;
struct refclockproc *pp;
pp = peer->procptr;
up = pp->unitptr ;
if ( up->bInitError ) {
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, "Ignore polling because of error during initializing" ) ;
return ;
}
if ( pp->polls > 0 && up->iLineCount == 0 ) {
/*
* No reply for last command
*/
refclock_report ( peer, CEVNT_TIMEOUT ) ;
}
pp->polls ++ ;
sReach[0] = peer->reach & 0x80 ? '1' : '0' ;
sReach[1] = peer->reach & 0x40 ? '1' : '0' ;
sReach[2] = peer->reach & 0x20 ? '1' : '0' ;
sReach[3] = peer->reach & 0x10 ? '1' : '0' ;
sReach[4] = peer->reach & 0x08 ? '1' : '0' ;
sReach[5] = peer->reach & 0x04 ? '1' : '0' ;
sReach[6] = peer->reach & 0x02 ? '1' : '0' ;
sReach[7] = 0 ; /* This poll */
sReach[8] = 0 ;
snprintf( sLog, sizeof(sLog), "polls=%ld reach=%s", pp->polls, sReach ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ATTENTION, sLog ) ;
up->iProcessState = JJY_PROCESS_STATE_POLL ;
up->iCommandSeq = 0 ;
up->iReceiveSeq = 0 ;
up->iLineCount = 0 ;
up->bLineError = FALSE ;
up->iRawBufLen = 0 ;
switch ( up->unittype ) {
case UNITTYPE_TRISTATE_JJY01 :
jjy_poll_tristate_jjy01 ( unit, peer ) ;
break ;
case UNITTYPE_CDEX_JST2000 :
jjy_poll_cdex_jst2000 ( unit, peer ) ;
break ;
case UNITTYPE_ECHOKEISOKUKI_LT2000 :
jjy_poll_echokeisokuki_lt2000 ( unit, peer ) ;
break ;
case UNITTYPE_CITIZENTIC_JJY200 :
jjy_poll_citizentic_jjy200 ( unit, peer ) ;
break ;
case UNITTYPE_TRISTATE_GPSCLOCK01 :
jjy_poll_tristate_gpsclock01 ( unit, peer ) ;
break ;
case UNITTYPE_SEIKO_TIMESYS_TDC_300 :
jjy_poll_seiko_tsys_tdc_300 ( unit, peer ) ;
break ;
case UNITTYPE_TELEPHONE :
jjy_poll_telephone ( unit, peer ) ;
break ;
default :
break ;
}
}
/**************************************************************************************************/
/* jjy_timer - called at one-second intervals */
/**************************************************************************************************/
static void
jjy_timer ( int unit, struct peer *peer )
{
struct refclockproc *pp ;
struct jjyunit *up ;
#ifdef DEBUG
if ( debug ) {
printf ( "refclock_jjy.c : jjy_timer\n" ) ;
}
#endif
pp = peer->procptr ;
up = pp->unitptr ;
if ( up->bReceiveFlag ) {
#ifdef DEBUG
if ( debug ) {
printf ( "refclock_jjy.c : jjy_timer : up->bReceiveFlag= TRUE : Timer skipped.\n" ) ;
}
#endif
return ;
}
switch ( up->unittype ) {
case UNITTYPE_TELEPHONE :
jjy_timer_telephone ( unit, peer ) ;
break ;
default :
break ;
}
}
/**************************************************************************************************/
/* jjy_synctime */
/**************************************************************************************************/
static void
jjy_synctime ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
char sLog [ 80 ], cStatus ;
const char *pStatus ;
pp->year = up->year ;
pp->day = ymd2yd( up->year, up->month, up->day ) ;
pp->hour = up->hour ;
pp->minute = up->minute ;
pp->second = up->second ;
pp->nsec = up->msecond * 1000000 ;
/*
* JST to UTC
*/
pp->hour -= 9 ;
if ( pp->hour < 0 ) {
pp->hour += 24 ;
pp->day -- ;
if ( pp->day < 1 ) {
pp->year -- ;
pp->day = ymd2yd( pp->year, 12, 31 ) ;
}
}
/*
* Process the new sample in the median filter and determine the
* timecode timestamp.
*/
if ( ! refclock_process( pp ) ) {
refclock_report( peer, CEVNT_BADTIME ) ;
return ;
}
pp->lastref = pp->lastrec ;
refclock_receive( peer ) ;
/*
* Write into the clockstats file
*/
snprintf ( sLog, sizeof(sLog),
"%04d/%02d/%02d %02d:%02d:%02d.%03d JST ( %04d/%03d %02d:%02d:%02d.%03d UTC )",
up->year, up->month, up->day,
up->hour, up->minute, up->second, up->msecond,
pp->year, pp->day, pp->hour, pp->minute, pp->second,
(int)(pp->nsec/1000000) ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ATTENTION, sLog ) ;
cStatus = ' ' ;
pStatus = "" ;
switch ( peer->status ) {
case 0 : cStatus = ' ' ; pStatus = "Reject" ; break ;
case 1 : cStatus = 'x' ; pStatus = "FalseTick" ; break ;
case 2 : cStatus = '.' ; pStatus = "Excess" ; break ;
case 3 : cStatus = '-' ; pStatus = "Outlier" ; break ;
case 4 : cStatus = '+' ; pStatus = "Candidate" ; break ;
case 5 : cStatus = '#' ; pStatus = "Selected" ; break ;
case 6 : cStatus = '*' ; pStatus = "Sys.Peer" ; break ;
case 7 : cStatus = 'o' ; pStatus = "PPS.Peer" ; break ;
default : break ;
}
snprintf ( sLog, sizeof(sLog),
"status %d [%c] %s : offset %3.3f mSec. : jitter %3.3f mSec.",
peer->status, cStatus, pStatus, peer->offset * 1000, peer->jitter * 1000 ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_INFORMATION, sLog ) ;
}
/*################################################################################################*/
/*################################################################################################*/
/*## ##*/
/*## The Tristate Ltd. JJY receiver TS-JJY01, TS-JJY02 ##*/
/*## ##*/
/*## server 127.127.40.X mode 1 ##*/
/*## ##*/
/*################################################################################################*/
/*################################################################################################*/
/* */
/* Command Response Remarks */
/* -------------------- ---------------------------------------- ---------------------------- */
/* dcst<CR><LF> VALID<CR><LF> or INVALID<CR><LF> */
/* stus<CR><LF> ADJUSTED<CR><LF> or UNADJUSTED<CR><LF> */
/* date<CR><LF> YYYY/MM/DD XXX<CR><LF> XXX is the day of the week */
/* time<CR><LF> HH:MM:SS<CR><LF> Not used by this driver */
/* stim<CR><LF> HH:MM:SS<CR><LF> Reply at just second */
/* */
/*################################################################################################*/
#define TS_JJY01_COMMAND_NUMBER_DATE 1
#define TS_JJY01_COMMAND_NUMBER_TIME 2
#define TS_JJY01_COMMAND_NUMBER_STIM 3
#define TS_JJY01_COMMAND_NUMBER_STUS 4
#define TS_JJY01_COMMAND_NUMBER_DCST 5
#define TS_JJY01_REPLY_DATE "yyyy/mm/dd www"
#define TS_JJY01_REPLY_STIM "hh:mm:ss"
#define TS_JJY01_REPLY_STUS_ADJUSTED "adjusted"
#define TS_JJY01_REPLY_STUS_UNADJUSTED "unadjusted"
#define TS_JJY01_REPLY_DCST_VALID "valid"
#define TS_JJY01_REPLY_DCST_INVALID "invalid"
#define TS_JJY01_REPLY_LENGTH_DATE 14 /* Length without <CR><LF> */
#define TS_JJY01_REPLY_LENGTH_TIME 8 /* Length without <CR><LF> */
#define TS_JJY01_REPLY_LENGTH_STIM 8 /* Length without <CR><LF> */
#define TS_JJY01_REPLY_LENGTH_STUS_ADJUSTED 8 /* Length without <CR><LF> */
#define TS_JJY01_REPLY_LENGTH_STUS_UNADJUSTED 10 /* Length without <CR><LF> */
#define TS_JJY01_REPLY_LENGTH_DCST_VALID 5 /* Length without <CR><LF> */
#define TS_JJY01_REPLY_LENGTH_DCST_INVALID 7 /* Length without <CR><LF> */
static struct
{
const char commandNumber ;
const char *command ;
int commandLength ;
int iExpectedReplyLength [ 2 ] ;
} tristate_jjy01_command_sequence[] =
{
{ 0, NULL, 0, { 0, 0 } }, /* Idle */
{ TS_JJY01_COMMAND_NUMBER_DCST, "dcst\r\n", 6, { TS_JJY01_REPLY_LENGTH_DCST_VALID , TS_JJY01_REPLY_LENGTH_DCST_INVALID } },
{ TS_JJY01_COMMAND_NUMBER_STUS, "stus\r\n", 6, { TS_JJY01_REPLY_LENGTH_STUS_ADJUSTED, TS_JJY01_REPLY_LENGTH_STUS_UNADJUSTED } },
{ TS_JJY01_COMMAND_NUMBER_TIME, "time\r\n", 6, { TS_JJY01_REPLY_LENGTH_TIME , TS_JJY01_REPLY_LENGTH_TIME } },
{ TS_JJY01_COMMAND_NUMBER_DATE, "date\r\n", 6, { TS_JJY01_REPLY_LENGTH_DATE , TS_JJY01_REPLY_LENGTH_DATE } },
{ TS_JJY01_COMMAND_NUMBER_STIM, "stim\r\n", 6, { TS_JJY01_REPLY_LENGTH_STIM , TS_JJY01_REPLY_LENGTH_STIM } },
/* End of command */
{ 0, NULL, 0, { 0, 0 } }
} ;
/**************************************************************************************************/
static int
jjy_start_tristate_jjy01 ( int unit, struct peer *peer, struct jjyunit *up )
{
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, "Refclock: Tristate Ltd. TS-JJY01, TS-JJY02" ) ;
up->unittype = UNITTYPE_TRISTATE_JJY01 ;
up->linespeed = SPEED232_TRISTATE_JJY01 ;
up->linediscipline = LDISC_CLK ;
return 0 ;
}
/**************************************************************************************************/
static int
jjy_receive_tristate_jjy01 ( struct recvbuf *rbufp )
{
struct jjyunit *up ;
struct refclockproc *pp ;
struct peer *peer;
char * pBuf ;
char sLog [ 100 ] ;
int iLen ;
int rc ;
const char * pCmd ;
int iCmdLen ;
/* Initialize pointers */
peer = rbufp->recv_peer ;
pp = peer->procptr ;
up = pp->unitptr ;
if ( up->linediscipline == LDISC_RAW ) {
pBuf = up->sTextBuf ;
iLen = up->iTextBufLen ;
} else {
pBuf = pp->a_lastcode ;
iLen = pp->lencode ;
}
DEBUG_PRINTF_JJY_RECEIVE( "jjy_receive_tristate_jjy01", iLen ) ;
/* Check expected reply */
if ( tristate_jjy01_command_sequence[up->iCommandSeq].command == NULL ) {
/* Command sequence has not been started, or has been completed */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_UNEXPECTED_REPLY,
pBuf ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
/* Check reply length */
if ( iLen != tristate_jjy01_command_sequence[up->iCommandSeq].iExpectedReplyLength[0]
&& iLen != tristate_jjy01_command_sequence[up->iCommandSeq].iExpectedReplyLength[1] ) {
/* Unexpected reply length */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_LENGTH,
iLen ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
/* Parse reply */
switch ( tristate_jjy01_command_sequence[up->iCommandSeq].commandNumber ) {
case TS_JJY01_COMMAND_NUMBER_DATE : /* YYYY/MM/DD WWW */
rc = sscanf ( pBuf, "%4d/%2d/%2d",
&up->year, &up->month, &up->day ) ;
if ( rc != 3 || up->year < 2000 || 2099 <= up->year
|| up->month < 1 || 12 < up->month
|| up->day < 1 || 31 < up->day ) {
/* Invalid date */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATE,
rc, up->year, up->month, up->day ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
break ;
case TS_JJY01_COMMAND_NUMBER_TIME : /* HH:MM:SS */
case TS_JJY01_COMMAND_NUMBER_STIM : /* HH:MM:SS */
if ( up->iTimestampCount >= 2 ) {
/* Too many time reply */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_TOO_MANY_REPLY,
up->iTimestampCount ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
rc = sscanf ( pBuf, "%2d:%2d:%2d",
&up->hour, &up->minute, &up->second ) ;
if ( rc != 3 || up->hour > 23 || up->minute > 59 ||
up->second > 60 ) {
/* Invalid time */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_TIME,
rc, up->hour, up->minute, up->second ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
up->iTimestamp[up->iTimestampCount] = ( up->hour * 60 + up->minute ) * 60 + up->second ;
up->iTimestampCount++ ;
up->msecond = 0 ;
break ;
case TS_JJY01_COMMAND_NUMBER_STUS :
if ( strncmp( pBuf, TS_JJY01_REPLY_STUS_ADJUSTED,
TS_JJY01_REPLY_LENGTH_STUS_ADJUSTED ) == 0
|| strncmp( pBuf, TS_JJY01_REPLY_STUS_UNADJUSTED,
TS_JJY01_REPLY_LENGTH_STUS_UNADJUSTED ) == 0 ) {
/* Good */
} else {
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY,
pBuf ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
break ;
case TS_JJY01_COMMAND_NUMBER_DCST :
if ( strncmp( pBuf, TS_JJY01_REPLY_DCST_VALID,
TS_JJY01_REPLY_LENGTH_DCST_VALID ) == 0
|| strncmp( pBuf, TS_JJY01_REPLY_DCST_INVALID,
TS_JJY01_REPLY_LENGTH_DCST_INVALID ) == 0 ) {
/* Good */
} else {
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY,
pBuf ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
break ;
default : /* Unexpected reply */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY,
pBuf ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
if ( up->iTimestampCount == 2 ) {
/* Process date and time */
if ( up->iTimestamp[1] - 2 <= up->iTimestamp[0]
&& up->iTimestamp[0] <= up->iTimestamp[1] ) {
/* 3 commands (time,date,stim) was excuted in two seconds */
jjy_synctime( peer, pp, up ) ;
return JJY_RECEIVE_DONE ;
} else if ( up->iTimestamp[0] > up->iTimestamp[1] ) {
/* Over midnight, and date is unsure */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_OVER_MIDNIGHT_2,
up->iTimestamp[0], up->iTimestamp[1] ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_INFORMATION, sLog ) ;
return JJY_RECEIVE_SKIP ;
} else {
/* Slow reply */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SLOW_REPLY_2,
up->iTimestamp[0], up->iTimestamp[1] ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
}
/* Issue next command */
if ( tristate_jjy01_command_sequence[up->iCommandSeq].command != NULL ) {
up->iCommandSeq ++ ;
}
if ( tristate_jjy01_command_sequence[up->iCommandSeq].command == NULL ) {
/* Command sequence completed */
return JJY_RECEIVE_DONE ;
}
pCmd = tristate_jjy01_command_sequence[up->iCommandSeq].command ;
iCmdLen = tristate_jjy01_command_sequence[up->iCommandSeq].commandLength ;
if ( write ( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
refclock_report ( peer, CEVNT_FAULT ) ;
}
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
return JJY_RECEIVE_WAIT ;
}
/**************************************************************************************************/
static void
jjy_poll_tristate_jjy01 ( int unit, struct peer *peer )
{
#ifdef DEBUG
static const char *sFunctionName = "jjy_poll_tristate_jjy01" ;
#endif
struct refclockproc *pp ;
struct jjyunit *up ;
const char * pCmd ;
int iCmdLen ;
pp = peer->procptr;
up = pp->unitptr ;
up->bLineError = FALSE ;
up->iTimestampCount = 0 ;
if ( ( pp->sloppyclockflag & CLK_FLAG1 ) == 0 ) {
/* Skip "dcst" and "stus" commands */
up->iCommandSeq = 2 ;
up->iLineCount = 2 ;
}
#ifdef DEBUG
if ( debug ) {
printf ( "%s (refclock_jjy.c) : flag1=%X CLK_FLAG1=%X up->iLineCount=%d\n",
sFunctionName, pp->sloppyclockflag, CLK_FLAG1,
up->iLineCount ) ;
}
#endif
/*
* Send a first command
*/
up->iCommandSeq ++ ;
pCmd = tristate_jjy01_command_sequence[up->iCommandSeq].command ;
iCmdLen = tristate_jjy01_command_sequence[up->iCommandSeq].commandLength ;
if ( write ( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
refclock_report ( peer, CEVNT_FAULT ) ;
}
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
}
/*################################################################################################*/
/*################################################################################################*/
/*## ##*/
/*## The C-DEX Co. Ltd. JJY receiver JST2000 ##*/
/*## ##*/
/*## server 127.127.40.X mode 2 ##*/
/*## ##*/
/*################################################################################################*/
/*################################################################################################*/
/* */
/* Command Response Remarks */
/* -------------------- ---------------------------------------- ---------------------------- */
/* <ENQ>1J<ETX> <STX>JYYMMDD HHMMSSS<ETX> J is a fixed character */
/* */
/*################################################################################################*/
static struct jjyRawDataBreak cdex_jst2000_raw_break [ ] =
{
{ "\x03", 1 }, { NULL, 0 }
} ;
/**************************************************************************************************/
static int
jjy_start_cdex_jst2000 ( int unit, struct peer *peer, struct jjyunit *up )
{
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, "Refclock: C-DEX Co. Ltd. JST2000" ) ;
up->unittype = UNITTYPE_CDEX_JST2000 ;
up->linespeed = SPEED232_CDEX_JST2000 ;
up->linediscipline = LDISC_RAW ;
up->pRawBreak = cdex_jst2000_raw_break ;
up->bWaitBreakString = TRUE ;
up->bSkipCntrlCharOnly = FALSE ;
return 0 ;
}
/**************************************************************************************************/
static int
jjy_receive_cdex_jst2000 ( struct recvbuf *rbufp )
{
struct jjyunit *up ;
struct refclockproc *pp ;
struct peer *peer ;
char *pBuf, sLog [ 100 ] ;
int iLen ;
int rc ;
/* Initialize pointers */
peer = rbufp->recv_peer ;
pp = peer->procptr ;
up = pp->unitptr ;
if ( up->linediscipline == LDISC_RAW ) {
pBuf = up->sTextBuf ;
iLen = up->iTextBufLen ;
} else {
pBuf = pp->a_lastcode ;
iLen = pp->lencode ;
}
DEBUG_PRINTF_JJY_RECEIVE( "jjy_receive_cdex_jst2000", iLen ) ;
/* Check expected reply */
if ( up->iCommandSeq != 1 ) {
/* Command sequence has not been started, or has been completed */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_UNEXPECTED_REPLY,
pBuf ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
/* Wait until ETX comes */
if ( up->iLineBufLen < 17 || up->sLineBuf[up->iLineBufLen-1] != 0x03 ) {
return JJY_RECEIVE_UNPROCESS ;
}
/* Check reply length */
if ( iLen != 15 ) {
/* Unexpected reply length */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_LENGTH,
iLen ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
/* JYYMMDDWHHMMSSS */
rc = sscanf ( pBuf, "J%2d%2d%2d%*1d%2d%2d%2d%1d",
&up->year, &up->month, &up->day,
&up->hour, &up->minute, &up->second,
&up->msecond ) ;
if ( rc != 7 || up->month < 1 || up->month > 12 ||
up->day < 1 || up->day > 31 || up->hour > 23 ||
up->minute > 59 || up->second > 60 ) {
/* Invalid date and time */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATETIME,
rc, up->year, up->month, up->day,
up->hour, up->minute, up->second ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
up->year += 2000 ;
up->msecond *= 100 ;
jjy_synctime( peer, pp, up ) ;
return JJY_RECEIVE_DONE ;
}
/**************************************************************************************************/
static void
jjy_poll_cdex_jst2000 ( int unit, struct peer *peer )
{
struct refclockproc *pp ;
struct jjyunit *up ;
pp = peer->procptr ;
up = pp->unitptr ;
up->bLineError = FALSE ;
up->iRawBufLen = 0 ;
up->iLineBufLen = 0 ;
up->iTextBufLen = 0 ;
/*
* Send "<ENQ>1J<ETX>" command
*/
up->iCommandSeq ++ ;
if ( write ( pp->io.fd, "\0051J\003", 4 ) != 4 ) {
refclock_report ( peer, CEVNT_FAULT ) ;
}
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, "\0051J\003" ) ;
}
/*################################################################################################*/
/*################################################################################################*/
/*## ##*/
/*## The Echo Keisokuki Co. Ltd. JJY receiver LT2000 ##*/
/*## ##*/
/*## server 127.127.40.X mode 3 ##*/
/*## ##*/
/*################################################################################################*/
/*################################################################################################*/
/* */
/* Command Response Remarks */
/* -------------------- ---------------------------------------- ---------------------------- */
/* # Mode 1 ( Request & Send ) */
/* T YYMMDDWHHMMSS<BCC1><BCC2><CR> */
/* C Mode 2 ( Continuous ) */
/* YYMMDDWHHMMSS<ST1><ST2><ST3><ST4><CR> 0.5 sec before time stamp */
/* <SUB> Second signal */
/* */
/*################################################################################################*/
#define ECHOKEISOKUKI_LT2000_MODE_REQUEST_SEND 1
#define ECHOKEISOKUKI_LT2000_MODE_CONTINUOUS 2
#define ECHOKEISOKUKI_LT2000_MODE_SWITCHING_CONTINUOUS 3
#define ECHOKEISOKUKI_LT2000_COMMAND_REQUEST_SEND "#"
#define ECHOKEISOKUKI_LT2000_COMMAND_REQUEST_TIME "T"
#define ECHOKEISOKUKI_LT2000_COMMAND_CONTINUOUS "C"
/**************************************************************************************************/
static int
jjy_start_echokeisokuki_lt2000 ( int unit, struct peer *peer, struct jjyunit *up )
{
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, "Refclock: Echo Keisokuki Co. Ltd. LT2000" ) ;
up->unittype = UNITTYPE_ECHOKEISOKUKI_LT2000 ;
up->linespeed = SPEED232_ECHOKEISOKUKI_LT2000 ;
up->linediscipline = LDISC_CLK ;
up->operationmode = ECHOKEISOKUKI_LT2000_MODE_SWITCHING_CONTINUOUS ;
return 0 ;
}
/**************************************************************************************************/
static int
jjy_receive_echokeisokuki_lt2000 ( struct recvbuf *rbufp )
{
struct jjyunit *up ;
struct refclockproc *pp ;
struct peer *peer;
char *pBuf, sLog [ 100 ], sErr [ 60 ] ;
int iLen ;
int rc ;
int i, ibcc, ibcc1, ibcc2 ;
/* Initialize pointers */
peer = rbufp->recv_peer ;
pp = peer->procptr ;
up = pp->unitptr ;
if ( up->linediscipline == LDISC_RAW ) {
pBuf = up->sTextBuf ;
iLen = up->iTextBufLen ;
} else {
pBuf = pp->a_lastcode ;
iLen = pp->lencode ;
}
DEBUG_PRINTF_JJY_RECEIVE( "jjy_receive_echokeisokuki_lt2000", iLen ) ;
/* Check reply length */
if ( ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_REQUEST_SEND
&& iLen != 15 )
|| ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_CONTINUOUS
&& iLen != 17 )
|| ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_SWITCHING_CONTINUOUS
&& iLen != 17 ) ) {
/* Unexpected reply length */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_LENGTH,
iLen ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
if ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_REQUEST_SEND && iLen == 15 ) {
/* YYMMDDWHHMMSS<BCC1><BCC2> */
for ( i = ibcc = 0 ; i < 13 ; i ++ ) {
ibcc ^= pBuf[i] ;
}
ibcc1 = 0x30 | ( ( ibcc >> 4 ) & 0xF ) ;
ibcc2 = 0x30 | ( ( ibcc ) & 0xF ) ;
if ( pBuf[13] != ibcc1 || pBuf[14] != ibcc2 ) {
snprintf( sErr, sizeof(sErr)-1, " BCC error : Recv=%02X,%02X / Calc=%02X,%02X ",
pBuf[13] & 0xFF, pBuf[14] & 0xFF,
ibcc1, ibcc2 ) ;
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY,
sErr ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
}
if ( ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_REQUEST_SEND
&& iLen == 15 )
|| ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_CONTINUOUS
&& iLen == 17 )
|| ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_SWITCHING_CONTINUOUS
&& iLen == 17 ) ) {
/* YYMMDDWHHMMSS<BCC1><BCC2> or YYMMDDWHHMMSS<ST1><ST2><ST3><ST4> */
rc = sscanf ( pBuf, "%2d%2d%2d%*1d%2d%2d%2d",
&up->year, &up->month, &up->day,
&up->hour, &up->minute, &up->second ) ;
if ( rc != 6 || up->month < 1 || up->month > 12
|| up->day < 1 || up->day > 31
|| up->hour > 23 || up->minute > 59 || up->second > 60 ) {
/* Invalid date and time */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATETIME,
rc, up->year, up->month, up->day,
up->hour, up->minute, up->second ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
up->year += 2000 ;
if ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_CONTINUOUS
|| up->operationmode == ECHOKEISOKUKI_LT2000_MODE_SWITCHING_CONTINUOUS ) {
/* A time stamp comes on every 0.5 second in the mode 2 of the LT-2000. */
up->msecond = 500 ;
up->second -- ;
if ( up->second < 0 ) {
up->second = 59 ;
up->minute -- ;
if ( up->minute < 0 ) {
up->minute = 59 ;
up->hour -- ;
if ( up->hour < 0 ) {
up->hour = 23 ;
up->day -- ;
if ( up->day < 1 ) {
up->month -- ;
if ( up->month < 1 ) {
up->month = 12 ;
up->year -- ;
}
}
}
}
}
}
jjy_synctime( peer, pp, up ) ;
}
if (up->operationmode == ECHOKEISOKUKI_LT2000_MODE_SWITCHING_CONTINUOUS ) {
/* Switch from mode 2 to mode 1 in order to restraint of useless time stamp. */
iLen = strlen( ECHOKEISOKUKI_LT2000_COMMAND_REQUEST_SEND ) ;
if ( write ( pp->io.fd, ECHOKEISOKUKI_LT2000_COMMAND_REQUEST_SEND, iLen ) != iLen ) {
refclock_report ( peer, CEVNT_FAULT ) ;
}
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, ECHOKEISOKUKI_LT2000_COMMAND_REQUEST_SEND ) ;
}
return JJY_RECEIVE_DONE ;
}
/**************************************************************************************************/
static void
jjy_poll_echokeisokuki_lt2000 ( int unit, struct peer *peer )
{
struct refclockproc *pp ;
struct jjyunit *up ;
char sCmd[2] ;
pp = peer->procptr ;
up = pp->unitptr ;
up->bLineError = FALSE ;
/*
* Send "T" or "C" command
*/
switch ( up->operationmode ) {
case ECHOKEISOKUKI_LT2000_MODE_REQUEST_SEND :
sCmd[0] = 'T' ;
break ;
case ECHOKEISOKUKI_LT2000_MODE_CONTINUOUS :
case ECHOKEISOKUKI_LT2000_MODE_SWITCHING_CONTINUOUS :
sCmd[0] = 'C' ;
break ;
}
sCmd[1] = 0 ;
if ( write ( pp->io.fd, sCmd, 1 ) != 1 ) {
refclock_report ( peer, CEVNT_FAULT ) ;
}
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, sCmd ) ;
}
/*################################################################################################*/
/*################################################################################################*/
/*## ##*/
/*## The CITIZEN T.I.C CO., LTD. JJY receiver JJY200 ##*/
/*## ##*/
/*## server 127.127.40.X mode 4 ##*/
/*## ##*/
/*################################################################################################*/
/*################################################################################################*/
/* */
/* Command Response Remarks */
/* -------------------- ---------------------------------------- ---------------------------- */
/* 'XX YY/MM/DD W HH:MM:SS<CR> XX:OK|NG|ER W:0(Mon)-6(Sun) */
/* */
/*################################################################################################*/
static int
jjy_start_citizentic_jjy200 ( int unit, struct peer *peer, struct jjyunit *up )
{
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, "Refclock: CITIZEN T.I.C CO. LTD. JJY200" ) ;
up->unittype = UNITTYPE_CITIZENTIC_JJY200 ;
up->linespeed = SPEED232_CITIZENTIC_JJY200 ;
up->linediscipline = LDISC_CLK ;
return 0 ;
}
/**************************************************************************************************/
static int
jjy_receive_citizentic_jjy200 ( struct recvbuf *rbufp )
{
struct jjyunit *up ;
struct refclockproc *pp ;
struct peer *peer;
char *pBuf, sLog [ 100 ], sMsg [ 16 ] ;
int iLen ;
int rc ;
char cApostrophe, sStatus[3] ;
int iWeekday ;
/* Initialize pointers */
peer = rbufp->recv_peer ;
pp = peer->procptr ;
up = pp->unitptr ;
if ( up->linediscipline == LDISC_RAW ) {
pBuf = up->sTextBuf ;
iLen = up->iTextBufLen ;
} else {
pBuf = pp->a_lastcode ;
iLen = pp->lencode ;
}
DEBUG_PRINTF_JJY_RECEIVE( "jjy_receive_citizentic_jjy200", iLen ) ;
/*
* JJY-200 sends a timestamp every second.
* So, a timestamp is ignored unless it is right after polled.
*/
if ( up->iProcessState != JJY_PROCESS_STATE_RECEIVE ) {
return JJY_RECEIVE_SKIP ;
}
/* Check reply length */
if ( iLen != 23 ) {
/* Unexpected reply length */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_LENGTH,
iLen ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
/* 'XX YY/MM/DD W HH:MM:SS<CR> */
rc = sscanf ( pBuf, "%c%2s %2d/%2d/%2d %1d %2d:%2d:%2d",
&cApostrophe, sStatus,
&up->year, &up->month, &up->day, &iWeekday,
&up->hour, &up->minute, &up->second ) ;
sStatus[2] = 0 ;
if ( rc != 9 || cApostrophe != '\''
|| ( strcmp( sStatus, "OK" ) != 0
&& strcmp( sStatus, "NG" ) != 0
&& strcmp( sStatus, "ER" ) != 0 )
|| up->month < 1 || up->month > 12 || up->day < 1 || up->day > 31
|| iWeekday > 6
|| up->hour > 23 || up->minute > 59 || up->second > 60 ) {
/* Invalid date and time */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATETIME,
rc, up->year, up->month, up->day,
up->hour, up->minute, up->second ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
} else if ( strcmp( sStatus, "NG" ) == 0
|| strcmp( sStatus, "ER" ) == 0 ) {
/* Timestamp is unsure */
snprintf( sMsg, sizeof(sMsg)-1, "status=%s", sStatus ) ;
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_TIMESTAMP_UNSURE,
sMsg ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_WARNING, sLog ) ;
return JJY_RECEIVE_SKIP ;
}
up->year += 2000 ;
up->msecond = 0 ;
jjy_synctime( peer, pp, up ) ;
return JJY_RECEIVE_DONE ;
}
/**************************************************************************************************/
static void
jjy_poll_citizentic_jjy200 ( int unit, struct peer *peer )
{
struct refclockproc *pp ;
struct jjyunit *up ;
pp = peer->procptr ;
up = pp->unitptr ;
up->bLineError = FALSE ;
}
/*################################################################################################*/
/*################################################################################################*/
/*## ##*/
/*## The Tristate Ltd. GPS clock TS-GPS01 ##*/
/*## ##*/
/*## server 127.127.40.X mode 5 ##*/
/*## ##*/
/*################################################################################################*/
/*################################################################################################*/
/* */
/* This clock has NMEA mode and command/respose mode. */
/* When this jjy driver are used, set to command/respose mode of this clock */
/* by the onboard switch SW4, and make sure the LED-Y is tured on. */
/* Other than this JJY driver, the refclock driver type 20, generic NMEA driver, */
/* works with the NMEA mode of this clock. */
/* */
/* Command Response Remarks */
/* -------------------- ---------------------------------------- ---------------------------- */
/* stus<CR><LF> *R|*G|*U|+U<CR><LF> */
/* date<CR><LF> YY/MM/DD<CR><LF> */
/* time<CR><LF> HH:MM:SS<CR><LF> */
/* */
/*################################################################################################*/
#define TS_GPS01_COMMAND_NUMBER_DATE 1
#define TS_GPS01_COMMAND_NUMBER_TIME 2
#define TS_GPS01_COMMAND_NUMBER_STUS 4
#define TS_GPS01_REPLY_DATE "yyyy/mm/dd"
#define TS_GPS01_REPLY_TIME "hh:mm:ss"
#define TS_GPS01_REPLY_STUS_RTC "*R"
#define TS_GPS01_REPLY_STUS_GPS "*G"
#define TS_GPS01_REPLY_STUS_UTC "*U"
#define TS_GPS01_REPLY_STUS_PPS "+U"
#define TS_GPS01_REPLY_LENGTH_DATE 10 /* Length without <CR><LF> */
#define TS_GPS01_REPLY_LENGTH_TIME 8 /* Length without <CR><LF> */
#define TS_GPS01_REPLY_LENGTH_STUS 2 /* Length without <CR><LF> */
static struct
{
char commandNumber ;
const char *command ;
int commandLength ;
int iExpectedReplyLength ;
} tristate_gps01_command_sequence[] =
{
{ 0, NULL, 0, 0 }, /* Idle */
{ TS_GPS01_COMMAND_NUMBER_STUS, "stus\r\n", 6, TS_GPS01_REPLY_LENGTH_STUS },
{ TS_GPS01_COMMAND_NUMBER_TIME, "time\r\n", 6, TS_GPS01_REPLY_LENGTH_TIME },
{ TS_GPS01_COMMAND_NUMBER_DATE, "date\r\n", 6, TS_GPS01_REPLY_LENGTH_DATE },
{ TS_GPS01_COMMAND_NUMBER_TIME, "time\r\n", 6, TS_GPS01_REPLY_LENGTH_TIME },
/* End of command */
{ 0, NULL, 0, 0 }
} ;
/**************************************************************************************************/
static int
jjy_start_tristate_gpsclock01 ( int unit, struct peer *peer, struct jjyunit *up )
{
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, "Refclock: Tristate Ltd. TS-GPS01" ) ;
up->unittype = UNITTYPE_TRISTATE_GPSCLOCK01 ;
up->linespeed = SPEED232_TRISTATE_GPSCLOCK01 ;
up->linediscipline = LDISC_CLK ;
return 0 ;
}
/**************************************************************************************************/
static int
jjy_receive_tristate_gpsclock01 ( struct recvbuf *rbufp )
{
#ifdef DEBUG
static const char *sFunctionName = "jjy_receive_tristate_gpsclock01" ;
#endif
struct jjyunit *up ;
struct refclockproc *pp ;
struct peer *peer;
char * pBuf ;
char sLog [ 100 ] ;
int iLen ;
int rc ;
const char * pCmd ;
int iCmdLen ;
/* Initialize pointers */
peer = rbufp->recv_peer ;
pp = peer->procptr ;
up = pp->unitptr ;
if ( up->linediscipline == LDISC_RAW ) {
pBuf = up->sTextBuf ;
iLen = up->iTextBufLen ;
} else {
pBuf = pp->a_lastcode ;
iLen = pp->lencode ;
}
DEBUG_PRINTF_JJY_RECEIVE( "jjy_receive_tristate_gpsclock01", iLen ) ;
/* Ignore NMEA data stream */
if ( iLen > 5
&& ( strncmp( pBuf, "$GP", 3 ) == 0 || strncmp( pBuf, "$PFEC", 5 ) == 0 ) ) {
#ifdef DEBUG
if ( debug ) {
printf ( "%s (refclock_jjy.c) : Skip NMEA stream [%s]\n",
sFunctionName, pBuf ) ;
}
#endif
return JJY_RECEIVE_WAIT ;
}
/*
* Skip command prompt '$Cmd>' from the TS-GPSclock-01
*/
if ( iLen == 5 && strncmp( pBuf, "$Cmd>", 5 ) == 0 ) {
return JJY_RECEIVE_WAIT ;
} else if ( iLen > 5 && strncmp( pBuf, "$Cmd>", 5 ) == 0 ) {
pBuf += 5 ;
iLen -= 5 ;
}
/*
* Ignore NMEA data stream after command prompt
*/
if ( iLen > 5
&& ( strncmp( pBuf, "$GP", 3 ) == 0 || strncmp( pBuf, "$PFEC", 5 ) == 0 ) ) {
#ifdef DEBUG
if ( debug ) {
printf ( "%s (refclock_jjy.c) : Skip NMEA stream [%s]\n",
sFunctionName, pBuf ) ;
}
#endif
return JJY_RECEIVE_WAIT ;
}
/* Check expected reply */
if ( tristate_gps01_command_sequence[up->iCommandSeq].command == NULL ) {
/* Command sequence has not been started, or has been completed */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_UNEXPECTED_REPLY,
pBuf ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
/* Check reply length */
if ( iLen != tristate_gps01_command_sequence[up->iCommandSeq].iExpectedReplyLength ) {
/* Unexpected reply length */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_LENGTH,
iLen ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
/* Parse reply */
switch ( tristate_gps01_command_sequence[up->iCommandSeq].commandNumber ) {
case TS_GPS01_COMMAND_NUMBER_DATE : /* YYYY/MM/DD */
rc = sscanf ( pBuf, "%4d/%2d/%2d", &up->year, &up->month, &up->day ) ;
if ( rc != 3 || up->year < 2000 || 2099 <= up->year
|| up->month < 1 || 12 < up->month
|| up->day < 1 || 31 < up->day ) {
/* Invalid date */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATE,
rc, up->year, up->month, up->day ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
break ;
case TS_GPS01_COMMAND_NUMBER_TIME : /* HH:MM:SS */
if ( up->iTimestampCount >= 2 ) {
/* Too many time reply */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_TOO_MANY_REPLY,
up->iTimestampCount ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
rc = sscanf ( pBuf, "%2d:%2d:%2d",
&up->hour, &up->minute, &up->second ) ;
if ( rc != 3
|| up->hour > 23 || up->minute > 59 || up->second > 60 ) {
/* Invalid time */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_TIME,
rc, up->hour, up->minute, up->second ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
up->iTimestamp[up->iTimestampCount] = ( up->hour * 60 + up->minute ) * 60 + up->second ;
up->iTimestampCount++ ;
up->msecond = 0 ;
break ;
case TS_GPS01_COMMAND_NUMBER_STUS :
if ( strncmp( pBuf, TS_GPS01_REPLY_STUS_RTC, TS_GPS01_REPLY_LENGTH_STUS ) == 0
|| strncmp( pBuf, TS_GPS01_REPLY_STUS_GPS, TS_GPS01_REPLY_LENGTH_STUS ) == 0
|| strncmp( pBuf, TS_GPS01_REPLY_STUS_UTC, TS_GPS01_REPLY_LENGTH_STUS ) == 0
|| strncmp( pBuf, TS_GPS01_REPLY_STUS_PPS, TS_GPS01_REPLY_LENGTH_STUS ) == 0 ) {
/* Good */
} else {
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY,
pBuf ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
break ;
default : /* Unexpected reply */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY,
pBuf ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
if ( up->iTimestampCount == 2 ) {
/* Process date and time */
if ( up->iTimestamp[1] - 2 <= up->iTimestamp[0]
&& up->iTimestamp[0] <= up->iTimestamp[1] ) {
/* 3 commands (time,date,stim) was excuted in two seconds */
jjy_synctime( peer, pp, up ) ;
return JJY_RECEIVE_DONE ;
} else if ( up->iTimestamp[0] > up->iTimestamp[1] ) {
/* Over midnight, and date is unsure */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_OVER_MIDNIGHT_2,
up->iTimestamp[0], up->iTimestamp[1] ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_INFORMATION, sLog ) ;
return JJY_RECEIVE_SKIP ;
} else {
/* Slow reply */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SLOW_REPLY_2,
up->iTimestamp[0], up->iTimestamp[1] ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
}
if ( tristate_gps01_command_sequence[up->iCommandSeq].command == NULL ) {
/* Command sequence completed */
jjy_synctime( peer, pp, up ) ;
return JJY_RECEIVE_DONE ;
}
/* Issue next command */
if ( tristate_gps01_command_sequence[up->iCommandSeq].command != NULL ) {
up->iCommandSeq ++ ;
}
if ( tristate_gps01_command_sequence[up->iCommandSeq].command == NULL ) {
/* Command sequence completed */
up->iProcessState = JJY_PROCESS_STATE_DONE ;
return JJY_RECEIVE_DONE ;
}
pCmd = tristate_gps01_command_sequence[up->iCommandSeq].command ;
iCmdLen = tristate_gps01_command_sequence[up->iCommandSeq].commandLength ;
if ( write ( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
refclock_report ( peer, CEVNT_FAULT ) ;
}
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
return JJY_RECEIVE_WAIT ;
}
/**************************************************************************************************/
static void
jjy_poll_tristate_gpsclock01 ( int unit, struct peer *peer )
{
#ifdef DEBUG
static const char *sFunctionName = "jjy_poll_tristate_gpsclock01" ;
#endif
struct refclockproc *pp ;
struct jjyunit *up ;
const char * pCmd ;
int iCmdLen ;
pp = peer->procptr ;
up = pp->unitptr ;
up->iTimestampCount = 0 ;
if ( ( pp->sloppyclockflag & CLK_FLAG1 ) == 0 ) {
/* Skip "stus" command */
up->iCommandSeq = 1 ;
up->iLineCount = 1 ;
}
#ifdef DEBUG
if ( debug ) {
printf ( "%s (refclock_jjy.c) : flag1=%X CLK_FLAG1=%X up->iLineCount=%d\n",
sFunctionName, pp->sloppyclockflag, CLK_FLAG1,
up->iLineCount ) ;
}
#endif
/*
* Send a first command
*/
up->iCommandSeq ++ ;
pCmd = tristate_gps01_command_sequence[up->iCommandSeq].command ;
iCmdLen = tristate_gps01_command_sequence[up->iCommandSeq].commandLength ;
if ( write ( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
refclock_report ( peer, CEVNT_FAULT ) ;
}
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
}
/*################################################################################################*/
/*################################################################################################*/
/*## ##*/
/*## The SEIKO TIME SYSTEMS TDC-300 ##*/
/*## ##*/
/*## server 127.127.40.X mode 6 ##*/
/*## ##*/
/*################################################################################################*/
/*################################################################################################*/
/* */
/* Type Response Remarks */
/* -------------------- ---------------------------------------- ---------------------------- */
/* Type 1 <STX>HH:MM:SS<ETX> */
/* Type 2 <STX>YYMMDDHHMMSSWLSCU<ETX> W:0(Sun)-6(Sat) */
/* Type 3 <STX>YYMMDDWHHMMSS<ETX> W:0(Sun)-6(Sat) */
/* <STX><xE5><ETX> 5 to 10 mSec. before second */
/* */
/*################################################################################################*/
static struct jjyRawDataBreak seiko_tsys_tdc_300_raw_break [ ] =
{
{ "\x03", 1 }, { NULL, 0 }
} ;
/**************************************************************************************************/
static int
jjy_start_seiko_tsys_tdc_300 ( int unit, struct peer *peer, struct jjyunit *up )
{
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, "Refclock: SEIKO TIME SYSTEMS TDC-300" ) ;
up->unittype = UNITTYPE_SEIKO_TIMESYS_TDC_300 ;
up->linespeed = SPEED232_SEIKO_TIMESYS_TDC_300 ;
up->linediscipline = LDISC_RAW ;
up->pRawBreak = seiko_tsys_tdc_300_raw_break ;
up->bWaitBreakString = TRUE ;
up->bSkipCntrlCharOnly = FALSE ;
return 0 ;
}
/**************************************************************************************************/
static int
jjy_receive_seiko_tsys_tdc_300 ( struct recvbuf *rbufp )
{
struct peer *peer;
struct refclockproc *pp ;
struct jjyunit *up ;
char *pBuf, sLog [ 100 ] ;
int iLen, i ;
int rc, iWeekday ;
time_t now ;
struct tm *pTime ;
/* Initialize pointers */
peer = rbufp->recv_peer ;
pp = peer->procptr ;
up = pp->unitptr ;
if ( up->linediscipline == LDISC_RAW ) {
pBuf = up->sTextBuf ;
iLen = up->iTextBufLen ;
} else {
pBuf = pp->a_lastcode ;
iLen = pp->lencode ;
}
DEBUG_PRINTF_JJY_RECEIVE( "jjy_receive_seiko_tsys_tdc_300", iLen ) ;
/*
* TDC-300 sends a timestamp every second.
* So, a timestamp is ignored unless it is right after polled.
*/
if ( up->iProcessState != JJY_PROCESS_STATE_RECEIVE ) {
return JJY_RECEIVE_SKIP ;
}
/* Process timestamp */
up->iReceiveSeq ++ ;
switch ( iLen ) {
case 8 : /* Type 1 : <STX>HH:MM:SS<ETX> */
for ( i = 0 ; i < iLen ; i ++ ) {
pBuf[i] &= 0x7F ;
}
rc = sscanf ( pBuf+1, "%2d:%2d:%2d",
&up->hour, &up->minute, &up->second ) ;
if ( rc != 3
|| up->hour > 23 || up->minute > 59 || up->second > 60 ) {
/* Invalid time */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_TIME,
rc, up->hour, up->minute, up->second ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
} else if ( up->hour == 23 && up->minute == 59 && up->second >= 55 ) {
/* Uncertainty date guard */
return JJY_RECEIVE_WAIT ;
}
time( &now ) ;
pTime = localtime( &now ) ;
up->year = pTime->tm_year ;
up->month = pTime->tm_mon + 1 ;
up->day = pTime->tm_mday ;
break ;
case 17 : /* Type 2 : <STX>YYMMDDHHMMSSWLSCU<ETX> */
for ( i = 0 ; i < iLen ; i ++ ) {
pBuf[i] &= 0x7F ;
}
rc = sscanf ( pBuf+1, "%2d%2d%2d%2d%2d%2d%1d",
&up->year, &up->month, &up->day,
&up->hour, &up->minute, &up->second, &iWeekday ) ;
if ( rc != 7
|| up->month < 1 || up->month > 12 || up->day < 1 || up->day > 31
|| iWeekday > 6
|| up->hour > 23 || up->minute > 59 || up->second > 60 ) {
/* Invalid date and time */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATETIME,
rc, up->year, up->month, up->day,
up->hour, up->minute, up->second ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
break ;
case 13 : /* Type 3 : <STX>YYMMDDWHHMMSS<ETX> */
rc = sscanf ( pBuf, "%2d%2d%2d%1d%2d%2d%2d",
&up->year, &up->month, &up->day, &iWeekday,
&up->hour, &up->minute, &up->second ) ;
if ( rc != 7
|| up->month < 1 || up->month > 12 || up->day < 1 || up->day > 31
|| iWeekday > 6
|| up->hour > 23 || up->minute > 59 || up->second > 60 ) {
/* Invalid date and time */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATETIME,
rc, up->year, up->month, up->day,
up->hour, up->minute, up->second ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
return JJY_RECEIVE_WAIT ;
case 1 : /* Type 3 : <STX><xE5><ETX> */
if ( ( *pBuf & 0xFF ) != 0xE5 ) {
/* Invalid second signal */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY,
up->sLineBuf ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
} else if ( up->iReceiveSeq == 1 ) {
/* Wait for next timestamp */
up->iReceiveSeq -- ;
return JJY_RECEIVE_WAIT ;
} else if ( up->iReceiveSeq >= 3 ) {
/* Unexpected second signal */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_UNEXPECTED_REPLY,
up->sLineBuf ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
break ;
default : /* Unexpected reply length */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_LENGTH,
iLen ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
return JJY_RECEIVE_ERROR ;
}
up->year += 2000 ;
up->msecond = 0 ;
jjy_synctime( peer, pp, up ) ;
return JJY_RECEIVE_DONE ;
}
/**************************************************************************************************/
static void
jjy_poll_seiko_tsys_tdc_300 ( int unit, struct peer *peer )
{
struct refclockproc *pp ;
struct jjyunit *up ;
pp = peer->procptr ;
up = pp->unitptr ;
up->bLineError = FALSE ;
}
/*################################################################################################*/
/*################################################################################################*/
/*## ##*/
/*## Telephone JJY ##*/
/*## ##*/
/*## server 127.127.40.X mode 100 to 180 ##*/
/*## ##*/
/*################################################################################################*/
/*################################################################################################*/
/* */
/* Prompt Command Response Remarks */
/* -------------------- -------------------- -------------------- -------------------------- */
/* Name<SP>?<SP> TJJY<CR> Welcome messages TJJY is a guest user ID */
/* > 4DATE<CR> YYYYMMDD<CR> */
/* > LEAPSEC<CR> XX<CR> One of <SP>0, +1, -1 */
/* > TIME<CR> HHMMSS<CR> 3 times on second */
/* > BYE<CR> Sayounara messages */
/* */
/*################################################################################################*/
static struct jjyRawDataBreak teljjy_raw_break [ ] =
{
{ "\r\n", 2 },
{ "\r" , 1 },
{ "\n" , 1 },
{ "Name ? ", 7 },
{ ">" , 1 },
{ "+++" , 3 },
{ NULL , 0 }
} ;
#define TELJJY_STATE_IDLE 0
#define TELJJY_STATE_DAILOUT 1
#define TELJJY_STATE_LOGIN 2
#define TELJJY_STATE_CONNECT 3
#define TELJJY_STATE_BYE 4
#define TELJJY_EVENT_NULL 0
#define TELJJY_EVENT_START 1
#define TELJJY_EVENT_CONNECT 2
#define TELJJY_EVENT_DISCONNECT 3
#define TELJJY_EVENT_COMMAND 4
#define TELJJY_EVENT_LOGIN 5 /* Posted by the jjy_receive_telephone */
#define TELJJY_EVENT_PROMPT 6 /* Posted by the jjy_receive_telephone */
#define TELJJY_EVENT_DATA 7 /* Posted by the jjy_receive_telephone */
#define TELJJY_EVENT_ERROR 8 /* Posted by the jjy_receive_telephone */
#define TELJJY_EVENT_SILENT 9 /* Posted by the jjy_timer_telephone */
#define TELJJY_EVENT_TIMEOUT 10 /* Posted by the jjy_timer_telephone */
static void teljjy_control ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_idle_ignore ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_idle_dialout ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_dial_ignore ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_dial_login ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_dial_disc ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_login_ignore ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_login_disc ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_login_conn ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_login_login ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_login_silent ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_login_error ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_conn_ignore ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_conn_disc ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_conn_send ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_conn_data ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_conn_silent ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_conn_error ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_bye_ignore ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_bye_disc ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int teljjy_bye_modem ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
static int ( *pTeljjyHandler [ ] [ 5 ] ) ( struct peer *, struct refclockproc *, struct jjyunit *) =
{ /*STATE_IDLE STATE_DAILOUT STATE_LOGIN STATE_CONNECT STATE_BYE */
/* NULL */ { teljjy_idle_ignore , teljjy_dial_ignore, teljjy_login_ignore, teljjy_conn_ignore, teljjy_bye_ignore },
/* START */ { teljjy_idle_dialout, teljjy_dial_ignore, teljjy_login_ignore, teljjy_conn_ignore, teljjy_bye_ignore },
/* CONNECT */ { teljjy_idle_ignore , teljjy_dial_login , teljjy_login_ignore, teljjy_conn_ignore, teljjy_bye_ignore },
/* DISCONNECT */ { teljjy_idle_ignore , teljjy_dial_disc , teljjy_login_disc , teljjy_conn_disc , teljjy_bye_disc },
/* COMMAND */ { teljjy_idle_ignore , teljjy_dial_ignore, teljjy_login_ignore, teljjy_conn_ignore, teljjy_bye_modem },
/* LOGIN */ { teljjy_idle_ignore , teljjy_dial_ignore, teljjy_login_login , teljjy_conn_error , teljjy_bye_ignore },
/* PROMPT */ { teljjy_idle_ignore , teljjy_dial_ignore, teljjy_login_conn , teljjy_conn_send , teljjy_bye_ignore },
/* DATA */ { teljjy_idle_ignore , teljjy_dial_ignore, teljjy_login_ignore, teljjy_conn_data , teljjy_bye_ignore },
/* ERROR */ { teljjy_idle_ignore , teljjy_dial_ignore, teljjy_login_error , teljjy_conn_error , teljjy_bye_ignore },
/* SILENT */ { teljjy_idle_ignore , teljjy_dial_ignore, teljjy_login_silent, teljjy_conn_silent, teljjy_bye_ignore },
/* TIMEOUT */ { teljjy_idle_ignore , teljjy_dial_disc , teljjy_login_error , teljjy_conn_error , teljjy_bye_modem }
} ;
static short iTeljjyNextState [ ] [ 5 ] =
{ /*STATE_IDLE STATE_DAILOUT STATE_LOGIN STATE_CONNECT STATE_BYE */
/* NULL */ { TELJJY_STATE_IDLE , TELJJY_STATE_DAILOUT, TELJJY_STATE_LOGIN , TELJJY_STATE_CONNECT, TELJJY_STATE_BYE },
/* START */ { TELJJY_STATE_DAILOUT, TELJJY_STATE_DAILOUT, TELJJY_STATE_LOGIN , TELJJY_STATE_CONNECT, TELJJY_STATE_BYE },
/* CONNECT */ { TELJJY_STATE_IDLE , TELJJY_STATE_LOGIN , TELJJY_STATE_LOGIN , TELJJY_STATE_CONNECT, TELJJY_STATE_BYE },
/* DISCONNECT */ { TELJJY_STATE_IDLE , TELJJY_STATE_IDLE , TELJJY_STATE_IDLE , TELJJY_STATE_IDLE , TELJJY_STATE_IDLE },
/* COMMAND */ { TELJJY_STATE_IDLE , TELJJY_STATE_DAILOUT, TELJJY_STATE_LOGIN , TELJJY_STATE_CONNECT, TELJJY_STATE_BYE },
/* LOGIN */ { TELJJY_STATE_IDLE , TELJJY_STATE_DAILOUT, TELJJY_STATE_LOGIN , TELJJY_STATE_BYE , TELJJY_STATE_BYE },
/* PROMPT */ { TELJJY_STATE_IDLE , TELJJY_STATE_DAILOUT, TELJJY_STATE_CONNECT, TELJJY_STATE_BYE , TELJJY_STATE_BYE },
/* DATA */ { TELJJY_STATE_IDLE , TELJJY_STATE_DAILOUT, TELJJY_STATE_LOGIN , TELJJY_STATE_CONNECT, TELJJY_STATE_BYE },
/* ERROR */ { TELJJY_STATE_IDLE , TELJJY_STATE_DAILOUT, TELJJY_STATE_BYE , TELJJY_STATE_BYE , TELJJY_STATE_BYE },
/* SILENT */ { TELJJY_STATE_IDLE , TELJJY_STATE_DAILOUT, TELJJY_STATE_LOGIN , TELJJY_STATE_CONNECT, TELJJY_STATE_BYE },
/* TIMEOUT */ { TELJJY_STATE_IDLE , TELJJY_STATE_IDLE , TELJJY_STATE_BYE , TELJJY_STATE_BYE , TELJJY_STATE_BYE }
} ;
static short iTeljjyPostEvent [ ] [ 5 ] =
{ /*STATE_IDLE STATE_DAILOUT STATE_LOGIN STATE_CONNECT STATE_BYE */
/* NULL */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_NULL , TELJJY_EVENT_NULL },
/* START */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_NULL , TELJJY_EVENT_NULL },
/* CONNECT */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_NULL , TELJJY_EVENT_NULL },
/* DISCONNECT */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_NULL , TELJJY_EVENT_NULL },
/* COMMAND */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_NULL , TELJJY_EVENT_NULL },
/* LOGIN */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_COMMAND, TELJJY_EVENT_NULL },
/* PROMPT */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_PROMPT , TELJJY_EVENT_COMMAND, TELJJY_EVENT_NULL },
/* DATA */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_NULL , TELJJY_EVENT_NULL },
/* ERROR */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_COMMAND, TELJJY_EVENT_COMMAND, TELJJY_EVENT_NULL },
/* SILENT */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_NULL , TELJJY_EVENT_NULL },
/* TIMEOUT */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_COMMAND, TELJJY_EVENT_COMMAND, TELJJY_EVENT_NULL }
} ;
static short iTeljjySilentTimeout [ 5 ] = { 0, 0, 10, 5, 0 } ;
static short iTeljjyStateTimeout [ 5 ] = { 0, 120, 60, 60, 40 } ;
#define TELJJY_STAY_CLOCK_STATE 0
#define TELJJY_CHANGE_CLOCK_STATE 1
/* Command and replay */
#define TELJJY_REPLY_NONE 0
#define TELJJY_REPLY_4DATE 1
#define TELJJY_REPLY_TIME 2
#define TELJJY_REPLY_LEAPSEC 3
#define TELJJY_REPLY_LOOP 4
#define TELJJY_REPLY_PROMPT 5
#define TELJJY_REPLY_LOOPBACK 6
#define TELJJY_REPLY_COM 7
#define TELJJY_COMMAND_START_SKIP_LOOPBACK 7
static struct
{
const char *command ;
int commandLength ;
int iEchobackReplyLength ;
int iExpectedReplyType ;
int iExpectedReplyLength ;
} teljjy_command_sequence[] =
{
{ NULL, 0, 0, 0, 0 }, /* Idle */
{ "LOOP\r" , 5, 4, TELJJY_REPLY_LOOP , 0 }, /* Getting into loopback mode */
{ ">" , 1, 1, TELJJY_REPLY_LOOPBACK, 0 }, /* Loopback measuring of delay time */
{ ">" , 1, 1, TELJJY_REPLY_LOOPBACK, 0 }, /* Loopback measuring of delay time */
{ ">" , 1, 1, TELJJY_REPLY_LOOPBACK, 0 }, /* Loopback measuring of delay time */
{ ">" , 1, 1, TELJJY_REPLY_LOOPBACK, 0 }, /* Loopback measuring of delay time */
{ ">" , 1, 1, TELJJY_REPLY_LOOPBACK, 0 }, /* Loopback measuring of delay time */
{ "COM\r" , 4, 3, TELJJY_REPLY_COM , 0 }, /* Exit from loopback mode */
/* TELJJY_COMMAND_START_SKIP_LOOPBACK */
{ "TIME\r" , 5, 4, TELJJY_REPLY_TIME , 6 },
{ "4DATE\r" , 6, 5, TELJJY_REPLY_4DATE , 8 },
{ "LEAPSEC\r", 8, 7, TELJJY_REPLY_LEAPSEC , 2 },
{ "TIME\r" , 5, 4, TELJJY_REPLY_TIME , 6 },
{ "BYE\r" , 4, 3, TELJJY_REPLY_NONE , 0 },
/* End of command */
{ NULL, 0, 0, 0, 0 }
} ;
#define TELJJY_LOOPBACK_DELAY_THRESHOLD 700 /* Milli second */
#ifdef DEBUG
#define DEBUG_TELJJY_PRINTF(sFunc) { if ( debug ) { printf ( "refclock_jjy.c : %s : iClockState=%d iClockEvent=%d iTeljjySilentTimer=%d iTeljjyStateTimer=%d iClockCommandSeq=%d\n", sFunc, up->iClockState, up->iClockEvent, up->iTeljjySilentTimer, up->iTeljjyStateTimer, up->iClockCommandSeq ) ; } }
#else
#define DEBUG_TELJJY_PRINTF(sFunc)
#endif
/**************************************************************************************************/
static int
jjy_start_telephone ( int unit, struct peer *peer, struct jjyunit *up )
{
char sLog [ 80 ], sFirstThreeDigits [ 4 ] ;
int iNumberOfDigitsOfPhoneNumber, iCommaCount, iCommaPosition ;
size_t i ;
size_t iFirstThreeDigitsCount ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, "Refclock: Telephone JJY" ) ;
up->unittype = UNITTYPE_TELEPHONE ;
up->linespeed = SPEED232_TELEPHONE ;
up->linediscipline = LDISC_RAW ;
up->pRawBreak = teljjy_raw_break ;
up->bWaitBreakString = TRUE ;
up->bSkipCntrlCharOnly = TRUE ;
up->iClockState = TELJJY_STATE_IDLE ;
up->iClockEvent = TELJJY_EVENT_NULL ;
/* Check the telephone number */
if ( sys_phone[0] == NULL ) {
msyslog( LOG_ERR, "refclock_jjy.c : jjy_start_telephone : phone in the ntpd.conf must be specified." ) ;
up->bInitError = TRUE ;
return 1 ;
}
if ( sys_phone[1] != NULL ) {
msyslog( LOG_ERR, "refclock_jjy.c : jjy_start_telephone : phone in the ntpd.conf should be only one." ) ;
up->bInitError = TRUE ;
return 1 ;
}
iNumberOfDigitsOfPhoneNumber = iCommaCount = iCommaPosition = iFirstThreeDigitsCount = 0 ;
for ( i = 0 ; i < strlen( sys_phone[0] ) ; i ++ ) {
if ( isdigit( (u_char)sys_phone[0][i] ) ) {
if ( iFirstThreeDigitsCount < sizeof(sFirstThreeDigits)-1 ) {
sFirstThreeDigits[iFirstThreeDigitsCount++] = sys_phone[0][i] ;
}
iNumberOfDigitsOfPhoneNumber ++ ;
} else if ( sys_phone[0][i] == ',' ) {
iCommaCount ++ ;
if ( iCommaCount > 1 ) {
msyslog( LOG_ERR, "refclock_jjy.c : jjy_start_telephone : phone in the ntpd.conf should be zero or one comma." ) ;
up->bInitError = TRUE ;
return 1 ;
}
iFirstThreeDigitsCount = 0 ;
iCommaPosition = i ;
} else if ( sys_phone[0][i] != '-' ) {
msyslog( LOG_ERR, "refclock_jjy.c : jjy_start_telephone : phone in the ntpd.conf should be a number or a hyphen." ) ;
up->bInitError = TRUE ;
return 1 ;
}
}
sFirstThreeDigits[iFirstThreeDigitsCount] = 0 ;
if ( iCommaCount == 1 ) {
if ( iCommaPosition != 1 || *sys_phone[0] != '0' ) {
msyslog( LOG_ERR, "refclock_jjy.c : jjy_start_telephone : Getting an outside line should be '0,'." ) ;
up->bInitError = TRUE ;
return 1 ;
}
}
if ( iNumberOfDigitsOfPhoneNumber - iCommaPosition < 6 || 10 < iNumberOfDigitsOfPhoneNumber - iCommaPosition ) {
/* Too short or too long */
msyslog( LOG_ERR, "refclock_jjy.c : jjy_start_telephone : phone=%s : Number of digits should be 6 to 10.", sys_phone[0] ) ;
up->bInitError = TRUE ;
return 1 ;
}
if ( strncmp( sFirstThreeDigits + iCommaPosition, "00" , 2 ) == 0
|| strncmp( sFirstThreeDigits + iCommaPosition, "10" , 2 ) == 0
|| strncmp( sFirstThreeDigits + iCommaPosition, "11" , 2 ) == 0
|| strncmp( sFirstThreeDigits + iCommaPosition, "12" , 2 ) == 0
|| strncmp( sFirstThreeDigits + iCommaPosition, "171", 3 ) == 0
|| strncmp( sFirstThreeDigits + iCommaPosition, "177", 3 ) == 0
|| ( sFirstThreeDigits[0] == '0' && sFirstThreeDigits[2] == '0' ) ) {
/* Not allowed because of emergency numbers or special service numbers */
msyslog( LOG_ERR, "refclock_jjy.c : jjy_start_telephone : phone=%s : First 2 or 3 digits are not allowed.", sys_phone[0] ) ;
up->bInitError = TRUE ;
return 1 ;
}
snprintf( sLog, sizeof(sLog), "phone=%s", sys_phone[0] ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, sLog ) ;
if ( peer->minpoll < 8 ) {
/* minpoll must be greater or equal to 8 ( 256 seconds = about 4 minutes ) */
int oldminpoll = peer->minpoll ;
peer->minpoll = 8 ;
if ( peer->ppoll < peer->minpoll ) {
peer->ppoll = peer->minpoll ;
}
if ( peer->maxpoll < peer->minpoll ) {
peer->maxpoll = peer->minpoll ;
}
snprintf( sLog, sizeof(sLog), "minpoll=%d -> %d", oldminpoll, peer->minpoll ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, sLog ) ;
}
return 0 ;
}
/**************************************************************************************************/
static int
jjy_receive_telephone ( struct recvbuf *rbufp )
{
#ifdef DEBUG
static const char *sFunctionName = "jjy_receive_telephone" ;
#endif
struct peer *peer;
struct refclockproc *pp ;
struct jjyunit *up ;
char *pBuf ;
int iLen ;
short iPreviousModemState ;
peer = rbufp->recv_peer ;
pp = peer->procptr ;
up = pp->unitptr ;
DEBUG_TELJJY_PRINTF( sFunctionName ) ;
if ( up->iClockState == TELJJY_STATE_IDLE
|| up->iClockState == TELJJY_STATE_DAILOUT
|| up->iClockState == TELJJY_STATE_BYE ) {
iPreviousModemState = getModemState( up ) ;
modem_receive ( rbufp ) ;
if ( iPreviousModemState != up->iModemState ) {
/* Modem state is changed just now. */
if ( isModemStateDisconnect( up->iModemState ) ) {
up->iClockEvent = TELJJY_EVENT_DISCONNECT ;
teljjy_control ( peer, pp, up ) ;
} else if ( isModemStateConnect( up->iModemState ) ) {
up->iClockEvent = TELJJY_EVENT_CONNECT ;
teljjy_control ( peer, pp, up ) ;
}
}
return JJY_RECEIVE_WAIT ;
}
if ( up->linediscipline == LDISC_RAW ) {
pBuf = up->sTextBuf ;
iLen = up->iTextBufLen ;
} else {
pBuf = pp->a_lastcode ;
iLen = pp->lencode ;
}
up->iTeljjySilentTimer = 0 ;
if ( iLen == 7 && strncmp( pBuf, "Name ? ", 7 ) == 0 ) { up->iClockEvent = TELJJY_EVENT_LOGIN ; }
else if ( iLen == 1 && strncmp( pBuf, ">" , 1 ) == 0 ) { up->iClockEvent = TELJJY_EVENT_PROMPT ; }
else if ( iLen >= 1 && strncmp( pBuf, "?" , 1 ) == 0 ) { up->iClockEvent = TELJJY_EVENT_ERROR ; }
else { up->iClockEvent = TELJJY_EVENT_DATA ; }
teljjy_control ( peer, pp, up ) ;
return JJY_RECEIVE_WAIT ;
}
/**************************************************************************************************/
static void
jjy_poll_telephone ( int unit, struct peer *peer )
{
#ifdef DEBUG
static const char *sFunctionName = "jjy_poll_telephone" ;
#endif
struct refclockproc *pp ;
struct jjyunit *up ;
pp = peer->procptr ;
up = pp->unitptr ;
DEBUG_TELJJY_PRINTF( sFunctionName ) ;
if ( up->iClockState == TELJJY_STATE_IDLE ) {
up->iRawBufLen = 0 ;
up->iLineBufLen = 0 ;
up->iTextBufLen = 0 ;
}
up->iClockEvent = TELJJY_EVENT_START ;
teljjy_control ( peer, pp, up ) ;
}
/**************************************************************************************************/
static void
jjy_timer_telephone ( int unit, struct peer *peer )
{
#ifdef DEBUG
static const char *sFunctionName = "jjy_timer_telephone" ;
#endif
struct refclockproc *pp ;
struct jjyunit *up ;
short iPreviousModemState ;
pp = peer->procptr ;
up = pp->unitptr ;
DEBUG_TELJJY_PRINTF( sFunctionName ) ;
if ( iTeljjySilentTimeout[up->iClockState] != 0 ) {
up->iTeljjySilentTimer++ ;
if ( iTeljjySilentTimeout[up->iClockState] <= up->iTeljjySilentTimer ) {
up->iClockEvent = TELJJY_EVENT_SILENT ;
teljjy_control ( peer, pp, up ) ;
}
}
if ( iTeljjyStateTimeout[up->iClockState] != 0 ) {
up->iTeljjyStateTimer++ ;
if ( iTeljjyStateTimeout[up->iClockState] <= up->iTeljjyStateTimer ) {
up->iClockEvent = TELJJY_EVENT_TIMEOUT ;
teljjy_control ( peer, pp, up ) ;
}
}
if ( isModemStateTimerOn( up ) ) {
iPreviousModemState = getModemState( up ) ;
modem_timer ( unit, peer ) ;
if ( iPreviousModemState != up->iModemState ) {
/* Modem state is changed just now. */
if ( isModemStateDisconnect( up->iModemState ) ) {
up->iClockEvent = TELJJY_EVENT_DISCONNECT ;
teljjy_control ( peer, pp, up ) ;
} else if ( isModemStateConnect( up->iModemState ) ) {
up->iClockEvent = TELJJY_EVENT_CONNECT ;
teljjy_control ( peer, pp, up ) ;
}
}
}
}
/**************************************************************************************************/
static void
teljjy_control ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
int i, rc ;
short iPostEvent = TELJJY_EVENT_NULL ;
DEBUG_TELJJY_PRINTF( "teljjy_control" ) ;
rc = (*pTeljjyHandler[up->iClockEvent][up->iClockState])( peer, pp, up ) ;
if ( rc == TELJJY_CHANGE_CLOCK_STATE ) {
iPostEvent = iTeljjyPostEvent[up->iClockEvent][up->iClockState] ;
#ifdef DEBUG
if ( debug ) {
printf( "refclock_jjy.c : teljjy_control : iClockState=%hd -> %hd iPostEvent=%hd\n",
up->iClockState, iTeljjyNextState[up->iClockEvent][up->iClockState], iPostEvent ) ;
}
#endif
up->iTeljjySilentTimer = 0 ;
if ( up->iClockState != iTeljjyNextState[up->iClockEvent][up->iClockState] ) {
/* Telephone JJY state is changing now */
up->iTeljjyStateTimer = 0 ;
up->bLineError = FALSE ;
up->iClockCommandSeq = 0 ;
up->iTimestampCount = 0 ;
up->iLoopbackCount = 0 ;
for ( i = 0 ; i < MAX_LOOPBACK ; i ++ ) {
up->bLoopbackTimeout[i] = FALSE ;
}
if (iTeljjyNextState[up->iClockEvent][up->iClockState] == TELJJY_STATE_IDLE ) {
/* Telephone JJY state is changing to IDLE just now */
up->iProcessState = JJY_PROCESS_STATE_DONE ;
}
}
up->iClockState = iTeljjyNextState[up->iClockEvent][up->iClockState] ;
}
if ( iPostEvent != TELJJY_EVENT_NULL ) {
up->iClockEvent = iPostEvent ;
teljjy_control ( peer, pp, up ) ;
}
up->iClockEvent = TELJJY_EVENT_NULL ;
}
/**************************************************************************************************/
static void
teljjy_setDelay ( struct peer *peer, struct jjyunit *up )
{
char sLog [ 60 ] ;
int milliSecond, microSecond ;
gettimeofday( &(up->delayTime[up->iLoopbackCount]), NULL ) ;
up->delayTime[up->iLoopbackCount].tv_sec -= up->sendTime[up->iLoopbackCount].tv_sec ;
up->delayTime[up->iLoopbackCount].tv_usec -= up->sendTime[up->iLoopbackCount].tv_usec ;
if ( up->delayTime[up->iLoopbackCount].tv_usec < 0 ) {
up->delayTime[up->iLoopbackCount].tv_sec -- ;
up->delayTime[up->iLoopbackCount].tv_usec += 1000000 ;
}
milliSecond = up->delayTime[up->iLoopbackCount].tv_usec / 1000 ;
microSecond = up->delayTime[up->iLoopbackCount].tv_usec - milliSecond * 1000 ;
milliSecond += up->delayTime[up->iLoopbackCount].tv_sec * 1000 ;
snprintf( sLog, sizeof(sLog), JJY_CLOCKSTATS_MESSAGE_LOOPBACK_DELAY,
milliSecond, microSecond ) ;
if ( milliSecond > TELJJY_LOOPBACK_DELAY_THRESHOLD ) {
/* Delay > 700 mS */
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_WARNING, sLog ) ;
} else {
/* Delay <= 700 mS */
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_INFORMATION, sLog ) ;
}
}
/**************************************************************************************************/
static int
teljjy_getDelay ( struct peer *peer, struct jjyunit *up )
{
struct timeval maxTime, minTime, averTime ;
int i ;
int minIndex = 0, maxIndex = 0, iAverCount = 0 ;
int iThresholdSecond, iThresholdMicroSecond ;
int iPercent ;
minTime.tv_sec = minTime.tv_usec = 0 ;
maxTime.tv_sec = maxTime.tv_usec = 0 ;
iThresholdSecond = TELJJY_LOOPBACK_DELAY_THRESHOLD / 1000 ;
iThresholdMicroSecond = ( TELJJY_LOOPBACK_DELAY_THRESHOLD - ( TELJJY_LOOPBACK_DELAY_THRESHOLD / 1000 ) * 1000 ) * 1000 ;
up->iLoopbackValidCount = 0 ;
for ( i = 0 ; i < MAX_LOOPBACK && i < up->iLoopbackCount ; i ++ ) {
if ( up->bLoopbackTimeout[i]
|| up->delayTime[i].tv_sec > iThresholdSecond
|| ( up->delayTime[i].tv_sec == iThresholdSecond
&& up->delayTime[i].tv_usec > iThresholdMicroSecond ) ) {
continue ;
}
if ( up->iLoopbackValidCount == 0 ) {
minTime.tv_sec = up->delayTime[i].tv_sec ;
minTime.tv_usec = up->delayTime[i].tv_usec ;
maxTime.tv_sec = up->delayTime[i].tv_sec ;
maxTime.tv_usec = up->delayTime[i].tv_usec ;
minIndex = maxIndex = i ;
} else if ( minTime.tv_sec > up->delayTime[i].tv_sec
|| ( minTime.tv_sec == up->delayTime[i].tv_sec
&& minTime.tv_usec > up->delayTime[i].tv_usec ) ) {
minTime.tv_sec = up->delayTime[i].tv_sec ;
minTime.tv_usec = up->delayTime[i].tv_usec ;
minIndex = i ;
} else if ( maxTime.tv_sec < up->delayTime[i].tv_sec
|| ( maxTime.tv_sec == up->delayTime[i].tv_sec
&& maxTime.tv_usec < up->delayTime[i].tv_usec ) ) {
maxTime.tv_sec = up->delayTime[i].tv_sec ;
maxTime.tv_usec = up->delayTime[i].tv_usec ;
maxIndex = i ;
}
up->iLoopbackValidCount ++ ;
}
if ( up->iLoopbackValidCount < 2 ) {
return -1 ;
}
averTime.tv_usec = 0;
for ( i = 0 ; i < MAX_LOOPBACK && i < up->iLoopbackCount ; i ++ ) {
if ( up->bLoopbackTimeout[i]
|| up->delayTime[i].tv_sec > iThresholdSecond
|| ( up->delayTime[i].tv_sec == iThresholdSecond
&& up->delayTime[i].tv_usec > iThresholdMicroSecond ) ) {
continue ;
}
if ( up->iLoopbackValidCount >= 3 && i == maxIndex ) {
continue ;
}
if ( up->iLoopbackValidCount >= 4 && i == minIndex ) {
continue ;
}
averTime.tv_usec += up->delayTime[i].tv_usec ;
iAverCount ++ ;
}
if ( iAverCount == 0 ) {
/* This is never happened. */
/* Previous for-if-for blocks assure iAverCount > 0. */
/* This code avoids a claim by the coverity scan tool. */
return -1 ;
}
/* mode 101 = 1%, mode 150 = 50%, mode 180 = 80% */
iPercent = ( peer->ttl - 100 ) ;
/* Average delay time in milli second */
return ( ( averTime.tv_usec / iAverCount ) * iPercent ) / 100000 ;
}
/******************************/
static int
teljjy_idle_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_TELJJY_PRINTF( "teljjy_idle_ignore" ) ;
return TELJJY_STAY_CLOCK_STATE ;
}
/******************************/
static int
teljjy_idle_dialout ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_TELJJY_PRINTF( "teljjy_idle_dialout" ) ;
modem_connect ( peer->refclkunit, peer ) ;
return TELJJY_CHANGE_CLOCK_STATE ;
}
/******************************/
static int
teljjy_dial_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_TELJJY_PRINTF( "teljjy_dial_ignore" ) ;
return TELJJY_STAY_CLOCK_STATE ;
}
/******************************/
static int
teljjy_dial_login ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_TELJJY_PRINTF( "teljjy_dial_login" ) ;
return TELJJY_CHANGE_CLOCK_STATE ;
}
/******************************/
static int
teljjy_dial_disc ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_TELJJY_PRINTF( "teljjy_dial_disc" ) ;
return TELJJY_CHANGE_CLOCK_STATE ;
}
/******************************/
static int
teljjy_login_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_TELJJY_PRINTF( "teljjy_login_ignore" ) ;
return TELJJY_STAY_CLOCK_STATE ;
}
/******************************/
static int
teljjy_login_disc ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_TELJJY_PRINTF( "teljjy_login_disc" ) ;
return TELJJY_CHANGE_CLOCK_STATE ;
}
/******************************/
static int
teljjy_login_conn ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
int i ;
DEBUG_TELJJY_PRINTF( "teljjy_login_conn" ) ;
up->bLineError = FALSE ;
up->iClockCommandSeq = 0 ;
up->iTimestampCount = 0 ;
up->iLoopbackCount = 0 ;
for ( i = 0 ; i < MAX_LOOPBACK ; i ++ ) {
up->bLoopbackTimeout[i] = FALSE ;
}
return TELJJY_CHANGE_CLOCK_STATE ;
}
/******************************/
static int
teljjy_login_login ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
const char * pCmd ;
int iCmdLen ;
DEBUG_TELJJY_PRINTF( "teljjy_login_login" ) ;
/* Send a guest user ID */
pCmd = "TJJY\r" ;
/* Send login ID */
iCmdLen = strlen( pCmd ) ;
if ( write( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
refclock_report( peer, CEVNT_FAULT ) ;
}
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
return TELJJY_STAY_CLOCK_STATE ;
}
/******************************/
static int
teljjy_login_silent ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_TELJJY_PRINTF( "teljjy_login_silent" ) ;
if ( write( pp->io.fd, "\r", 1 ) != 1 ) {
refclock_report( peer, CEVNT_FAULT ) ;
}
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, "\r" ) ;
up->iTeljjySilentTimer = 0 ;
return TELJJY_CHANGE_CLOCK_STATE ;
}
/******************************/
static int
teljjy_login_error ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_TELJJY_PRINTF( "teljjy_login_error" ) ;
return TELJJY_CHANGE_CLOCK_STATE ;
}
/******************************/
static int
teljjy_conn_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_TELJJY_PRINTF( "teljjy_conn_ignore" ) ;
return TELJJY_STAY_CLOCK_STATE ;
}
/******************************/
static int
teljjy_conn_disc ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_TELJJY_PRINTF( "teljjy_conn_disc" ) ;
return TELJJY_CHANGE_CLOCK_STATE ;
}
/******************************/
static int
teljjy_conn_send ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
const char * pCmd ;
int i, iLen, iNextClockState ;
DEBUG_TELJJY_PRINTF( "teljjy_conn_send" ) ;
if ( up->iClockCommandSeq > 0
&& teljjy_command_sequence[up->iClockCommandSeq].command == NULL ) {
/* Command sequence has been completed */
return TELJJY_CHANGE_CLOCK_STATE ;
}
if ( up->iClockCommandSeq == 0 && peer->ttl == 100 ) {
/* Skip loopback */
up->iClockCommandSeq = TELJJY_COMMAND_START_SKIP_LOOPBACK ;
} else if ( up->iClockCommandSeq == 0 && peer->ttl != 100 ) {
/* Loopback start */
up->iLoopbackCount = 0 ;
for ( i = 0 ; i < MAX_LOOPBACK ; i ++ ) {
up->bLoopbackTimeout[i] = FALSE ;
}
} else if ( up->iClockCommandSeq > 0 && peer->ttl != 100
&& teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_LOOPBACK
&& up->iLoopbackCount < MAX_LOOPBACK ) {
/* Loopback character comes */
#ifdef DEBUG
if ( debug ) {
printf( "refclock_jjy.c : teljjy_conn_send : iLoopbackCount=%d\n",
up->iLoopbackCount ) ;
}
#endif
teljjy_setDelay( peer, up ) ;
up->iLoopbackCount ++ ;
}
up->iClockCommandSeq++ ;
pCmd = teljjy_command_sequence[up->iClockCommandSeq].command ;
iLen = teljjy_command_sequence[up->iClockCommandSeq].commandLength ;
if ( pCmd != NULL ) {
if ( write( pp->io.fd, pCmd, iLen ) != iLen ) {
refclock_report( peer, CEVNT_FAULT ) ;
}
if ( teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_LOOPBACK ) {
/* Loopback character and timestamp */
gettimeofday( &(up->sendTime[up->iLoopbackCount]), NULL ) ;
up->bLoopbackMode = TRUE ;
} else {
/* Regular command */
up->bLoopbackMode = FALSE ;
}
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
if ( teljjy_command_sequence[up->iClockCommandSeq+1].command == NULL ) {
/* Last command of the command sequence */
iNextClockState = TELJJY_CHANGE_CLOCK_STATE ;
} else {
/* More commands to be issued */
iNextClockState = TELJJY_STAY_CLOCK_STATE ;
}
} else {
iNextClockState = TELJJY_CHANGE_CLOCK_STATE ;
}
return iNextClockState ;
}
/******************************/
static int
teljjy_conn_data ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
char *pBuf ;
int iLen, rc ;
char sLog [ 80 ] ;
char bAdjustment ;
DEBUG_TELJJY_PRINTF( "teljjy_conn_data" ) ;
if ( up->linediscipline == LDISC_RAW ) {
pBuf = up->sTextBuf ;
iLen = up->iTextBufLen ;
} else {
pBuf = pp->a_lastcode ;
iLen = pp->lencode ;
}
if ( teljjy_command_sequence[up->iClockCommandSeq].iEchobackReplyLength == iLen
&& teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_LOOPBACK
&& up->sTextBuf[0] == *(teljjy_command_sequence[up->iClockCommandSeq].command)
&& up->iLoopbackCount < MAX_LOOPBACK ) {
/* Loopback */
teljjy_setDelay( peer, up ) ;
up->iLoopbackCount ++ ;
} else if ( teljjy_command_sequence[up->iClockCommandSeq].iEchobackReplyLength == iLen
&& strncmp( pBuf, teljjy_command_sequence[up->iClockCommandSeq].command, iLen ) == 0 ) {
/* Maybe echoback */
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_INFORMATION, JJY_CLOCKSTATS_MESSAGE_ECHOBACK ) ;
} else if ( teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyLength == iLen
&& teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_4DATE ) {
/* 4DATE<CR> -> YYYYMMDD<CR> */
rc = sscanf ( pBuf, "%4d%2d%2d", &up->year, &up->month, &up->day ) ;
if ( rc != 3 || up->year < 2000 || 2099 <= up->year
|| up->month < 1 || 12 < up->month || up->day < 1 || 31 < up->day ) {
/* Invalid date */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATE,
rc, up->year, up->month, up->day ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
}
} else if ( teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyLength == iLen
&& teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_LEAPSEC
&& ( strncmp( pBuf, " 0", 2 ) == 0 || strncmp( pBuf, "+1", 2 ) == 0 || strncmp( pBuf, "-1", 2 ) == 0 ) ) {
/* LEAPSEC<CR> -> XX<CR> ( One of <SP>0, +1, -1 ) */
rc = sscanf ( pBuf, "%2d", &up->leapsecond ) ;
if ( rc != 1 || up->leapsecond < -1 || 1 < up->leapsecond ) {
/* Invalid leap second */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_LEAP,
pBuf ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
}
} else if ( teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyLength == iLen
&& teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_TIME ) {
/* TIME<CR> -> HHMMSS<CR> ( 3 times on second ) */
rc = sscanf ( pBuf, "%2d%2d%2d", &up->hour, &up->minute, &up->second ) ;
if ( rc != 3 || up->hour > 23 || up->minute > 59 || up->second > 60 ) {
/* Invalid time */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_TIME,
rc, up->hour, up->minute, up->second ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
up->bLineError = TRUE ;
}
up->iTimestamp[up->iTimestampCount] = ( up->hour * 60 + up->minute ) * 60 + up->second ;
up->iTimestampCount++ ;
if ( up->iTimestampCount == 6 && ! up->bLineError ) {
#if DEBUG
printf( "refclock_jjy.c : teljjy_conn_data : bLineError=%d iTimestamp=%d, %d, %d\n",
up->bLineError,
up->iTimestamp[3], up->iTimestamp[4], up->iTimestamp[5] ) ;
#endif
bAdjustment = TRUE ;
if ( peer->ttl == 100 ) {
/* mode=100 */
up->msecond = 0 ;
} else {
/* mode=101 to 110 */
up->msecond = teljjy_getDelay( peer, up ) ;
if (up->msecond < 0 ) {
up->msecond = 0 ;
bAdjustment = FALSE ;
}
}
if ( ( up->iTimestamp[3] - 15 ) <= up->iTimestamp[2]
&& up->iTimestamp[2] <= up->iTimestamp[3]
&& ( up->iTimestamp[3] + 1 ) == up->iTimestamp[4]
&& ( up->iTimestamp[4] + 1 ) == up->iTimestamp[5] ) {
/* Non over midnight */
jjy_synctime( peer, pp, up ) ;
if ( peer->ttl != 100 ) {
if ( bAdjustment ) {
snprintf( sLog, sizeof(sLog),
JJY_CLOCKSTATS_MESSAGE_DELAY_ADJUST,
up->msecond, up->iLoopbackValidCount, MAX_LOOPBACK ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_INFORMATION, sLog ) ;
} else {
snprintf( sLog, sizeof(sLog),
JJY_CLOCKSTATS_MESSAGE_DELAY_UNADJUST,
up->iLoopbackValidCount, MAX_LOOPBACK ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
}
}
}
}
} else if ( teljjy_command_sequence[up->iClockCommandSeq].iEchobackReplyLength != iLen
&& teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_LOOPBACK ) {
/* Loopback noise ( Unexpected replay ) */
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_IGNORE_REPLY,
pBuf ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_WARNING, sLog ) ;
} else {
up->bLineError = TRUE ;
snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_UNEXPECTED_REPLY,
pBuf ) ;
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
}
return TELJJY_STAY_CLOCK_STATE ;
}
/******************************/
static int
teljjy_conn_silent ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
const char * pCmd ;
DEBUG_TELJJY_PRINTF( "teljjy_conn_silent" ) ;
if ( up->iClockCommandSeq >= 1
&& up->iClockCommandSeq < TELJJY_COMMAND_START_SKIP_LOOPBACK ) {
/* Loopback */
#ifdef DEBUG
if ( debug ) {
printf( "refclock_jjy.c : teljjy_conn_silent : call teljjy_conn_send\n" ) ;
}
#endif
if ( teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_LOOPBACK ) {
up->bLoopbackTimeout[up->iLoopbackCount] = TRUE ;
}
up->iTeljjySilentTimer = 0 ;
return teljjy_conn_send( peer, pp, up ) ;
} else {
pCmd = "\r" ;
}
if ( write( pp->io.fd, pCmd, 1 ) != 1 ) {
refclock_report( peer, CEVNT_FAULT ) ;
}
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
up->iTeljjySilentTimer = 0 ;
return TELJJY_STAY_CLOCK_STATE ;
}
/******************************/
static int
teljjy_conn_error ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_TELJJY_PRINTF( "teljjy_conn_error" ) ;
return TELJJY_CHANGE_CLOCK_STATE ;
}
/******************************/
static int
teljjy_bye_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_TELJJY_PRINTF( "teljjy_bye_ignore" ) ;
return TELJJY_STAY_CLOCK_STATE ;
}
/******************************/
static int
teljjy_bye_disc ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_TELJJY_PRINTF( "teljjy_bye_disc" ) ;
return TELJJY_CHANGE_CLOCK_STATE ;
}
/******************************/
static int
teljjy_bye_modem ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_TELJJY_PRINTF( "teljjy_bye_modem" ) ;
modem_disconnect ( peer->refclkunit, peer ) ;
return TELJJY_STAY_CLOCK_STATE ;
}
/*################################################################################################*/
/*################################################################################################*/
/*## ##*/
/*## Modem control finite state machine ##*/
/*## ##*/
/*################################################################################################*/
/*################################################################################################*/
/* struct jjyunit.iModemState */
#define MODEM_STATE_DISCONNECT 0
#define MODEM_STATE_INITIALIZE 1
#define MODEM_STATE_DAILING 2
#define MODEM_STATE_CONNECT 3
#define MODEM_STATE_ESCAPE 4
/* struct jjyunit.iModemEvent */
#define MODEM_EVENT_NULL 0
#define MODEM_EVENT_INITIALIZE 1
#define MODEM_EVENT_DIALOUT 2
#define MODEM_EVENT_DISCONNECT 3
#define MODEM_EVENT_RESP_OK 4
#define MODEM_EVENT_RESP_CONNECT 5
#define MODEM_EVENT_RESP_RING 6
#define MODEM_EVENT_RESP_NO_CARRIER 7
#define MODEM_EVENT_RESP_ERROR 8
#define MODEM_EVENT_RESP_CONNECT_X 9
#define MODEM_EVENT_RESP_NO_DAILTONE 10
#define MODEM_EVENT_RESP_BUSY 11
#define MODEM_EVENT_RESP_NO_ANSWER 12
#define MODEM_EVENT_RESP_UNKNOWN 13
#define MODEM_EVENT_SILENT 14
#define MODEM_EVENT_TIMEOUT 15
/* Function prototypes */
static void modem_control ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_disc_ignore ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_disc_init ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_init_ignore ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_init_start ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_init_disc ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_init_resp00 ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_init_resp04 ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_dial_ignore ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_dial_dialout ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_dial_escape ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_dial_connect ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_dial_disc ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_conn_ignore ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_conn_escape ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_esc_ignore ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_esc_escape ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_esc_data ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_esc_silent ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int modem_esc_disc ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
static int ( *pModemHandler [ ] [ 5 ] ) ( struct peer *, struct refclockproc *, struct jjyunit * ) =
{ /*STATE_DISCONNECT STATE_INITIALIZE STATE_DAILING STATE_CONNECT STATE_ESCAPE */
/* NULL */ { modem_disc_ignore, modem_init_ignore, modem_dial_ignore , modem_conn_ignore, modem_esc_ignore },
/* INITIALIZE */ { modem_disc_init , modem_init_start , modem_dial_ignore , modem_conn_ignore, modem_esc_ignore },
/* DIALOUT */ { modem_disc_ignore, modem_init_ignore, modem_dial_dialout, modem_conn_ignore, modem_esc_ignore },
/* DISCONNECT */ { modem_disc_ignore, modem_init_disc , modem_dial_escape , modem_conn_escape, modem_esc_escape },
/* RESP: 0: OK */ { modem_disc_ignore, modem_init_resp00, modem_dial_ignore , modem_conn_ignore, modem_esc_data },
/* RESP: 1: CONNECT */ { modem_disc_ignore, modem_init_ignore, modem_dial_connect, modem_conn_ignore, modem_esc_data },
/* RESP: 2: RING */ { modem_disc_ignore, modem_init_ignore, modem_dial_ignore , modem_conn_ignore, modem_esc_data },
/* RESP: 3: NO CARRIER */ { modem_disc_ignore, modem_init_ignore, modem_dial_disc , modem_conn_ignore, modem_esc_data },
/* RESP: 4: ERROR */ { modem_disc_ignore, modem_init_resp04, modem_dial_disc , modem_conn_ignore, modem_esc_data },
/* RESP: 5: CONNECT */ { modem_disc_ignore, modem_init_ignore, modem_dial_connect, modem_conn_ignore, modem_esc_data },
/* RESP: 6: NO DAILTONE */ { modem_disc_ignore, modem_init_ignore, modem_dial_disc , modem_conn_ignore, modem_esc_data },
/* RESP: 7: BUSY */ { modem_disc_ignore, modem_init_ignore, modem_dial_disc , modem_conn_ignore, modem_esc_data },
/* RESP: 8: NO ANSWER */ { modem_disc_ignore, modem_init_ignore, modem_dial_disc , modem_conn_ignore, modem_esc_data },
/* RESP: 9: UNKNOWN */ { modem_disc_ignore, modem_init_ignore, modem_dial_ignore , modem_conn_ignore, modem_esc_data },
/* SILENT */ { modem_disc_ignore, modem_init_ignore, modem_dial_ignore , modem_conn_ignore, modem_esc_silent },
/* TIMEOUT */ { modem_disc_ignore, modem_init_disc , modem_dial_escape , modem_conn_escape, modem_esc_disc }
} ;
static short iModemNextState [ ] [ 5 ] =
{ /*STATE_DISCONNECT STATE_INITIALIZE STATE_DAILING STATE_CONNECT STATE_ESCAPE */
/* NULL */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DAILING , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
/* INITIALIZE */ { MODEM_STATE_INITIALIZE, MODEM_STATE_INITIALIZE, MODEM_STATE_DAILING , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
/* DIALOUT */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DAILING , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
/* DISCONNECT */ { MODEM_STATE_DISCONNECT, MODEM_STATE_DISCONNECT, MODEM_STATE_ESCAPE , MODEM_STATE_ESCAPE , MODEM_STATE_ESCAPE },
/* RESP: 0: OK */ { MODEM_STATE_DISCONNECT, MODEM_STATE_DAILING , MODEM_STATE_DAILING , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
/* RESP: 1: CONNECT */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_CONNECT , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
/* RESP: 2: RING */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DAILING , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
/* RESP: 3: NO CARRIER */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DISCONNECT, MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
/* RESP: 4: ERROR */ { MODEM_STATE_DISCONNECT, MODEM_STATE_DAILING , MODEM_STATE_DISCONNECT, MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
/* RESP: 5: CONNECT X */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_CONNECT , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
/* RESP: 6: NO DAILTONE */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DISCONNECT, MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
/* RESP: 7: BUSY */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DISCONNECT, MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
/* RESP: 8: NO ANSWER */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DISCONNECT, MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
/* RESP: 9: UNKNOWN */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DAILING , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
/* SILENT */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DAILING , MODEM_STATE_CONNECT, MODEM_STATE_DISCONNECT },
/* TIMEOUT */ { MODEM_STATE_DISCONNECT, MODEM_STATE_DISCONNECT, MODEM_STATE_ESCAPE , MODEM_STATE_ESCAPE , MODEM_STATE_DISCONNECT }
} ;
static short iModemPostEvent [ ] [ 5 ] =
{ /*STATE_DISCONNECT STATE_INITIALIZE STATE_DAILING STATE_CONNECT STATE_ESCAPE */
/* NULL */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
/* INITIALIZE */ { MODEM_EVENT_INITIALIZE, MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
/* DIALOUT */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
/* DISCONNECT */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_DISCONNECT, MODEM_EVENT_DISCONNECT, MODEM_EVENT_NULL },
/* RESP: 0: OK */ { MODEM_EVENT_NULL , MODEM_EVENT_DIALOUT, MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
/* RESP: 1: CONNECT */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
/* RESP: 2: RING */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
/* RESP: 3: NO CARRIER */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
/* RESP: 4: ERROR */ { MODEM_EVENT_NULL , MODEM_EVENT_DIALOUT, MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
/* RESP: 5: CONNECT X */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
/* RESP: 6: NO DAILTONE */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
/* RESP: 7: BUSY */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
/* RESP: 8: NO ANSWER */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
/* RESP: 9: UNKNOWN */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
/* SILENT */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
/* TIMEOUT */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_DISCONNECT, MODEM_EVENT_DISCONNECT, MODEM_EVENT_NULL }
} ;
static short iModemSilentTimeout [ 5 ] = { 0, 0, 0, 0, 5 } ;
static short iModemStateTimeout [ 5 ] = { 0, 20, 90, 0, 20 } ;
#define STAY_MODEM_STATE 0
#define CHANGE_MODEM_STATE 1
#ifdef DEBUG
#define DEBUG_MODEM_PRINTF(sFunc) { if ( debug ) { printf ( "refclock_jjy.c : %s : iModemState=%d iModemEvent=%d iModemSilentTimer=%d iModemStateTimer=%d\n", sFunc, up->iModemState, up->iModemEvent, up->iModemSilentTimer, up->iModemStateTimer ) ; } }
#else
#define DEBUG_MODEM_PRINTF(sFunc)
#endif
/**************************************************************************************************/
static short
getModemState ( struct jjyunit *up )
{
return up->iModemState ;
}
/**************************************************************************************************/
static int
isModemStateConnect ( short iCheckState )
{
return ( iCheckState == MODEM_STATE_CONNECT ) ;
}
/**************************************************************************************************/
static int
isModemStateDisconnect ( short iCheckState )
{
return ( iCheckState == MODEM_STATE_DISCONNECT ) ;
}
/**************************************************************************************************/
static int
isModemStateTimerOn ( struct jjyunit *up )
{
return ( iModemSilentTimeout[up->iModemState] != 0 || iModemStateTimeout[up->iModemState] != 0 ) ;
}
/**************************************************************************************************/
static void
modem_connect ( int unit, struct peer *peer )
{
struct refclockproc *pp;
struct jjyunit *up;
pp = peer->procptr ;
up = pp->unitptr ;
DEBUG_MODEM_PRINTF( "modem_connect" ) ;
up->iModemEvent = MODEM_EVENT_INITIALIZE ;
modem_control ( peer, pp, up ) ;
}
/**************************************************************************************************/
static void
modem_disconnect ( int unit, struct peer *peer )
{
struct refclockproc *pp;
struct jjyunit *up;
pp = peer->procptr ;
up = pp->unitptr ;
DEBUG_MODEM_PRINTF( "modem_disconnect" ) ;
up->iModemEvent = MODEM_EVENT_DISCONNECT ;
modem_control ( peer, pp, up ) ;
}
/**************************************************************************************************/
static int
modem_receive ( struct recvbuf *rbufp )
{
struct peer *peer;
struct jjyunit *up;
struct refclockproc *pp;
char *pBuf ;
size_t iLen ;
#ifdef DEBUG
static const char *sFunctionName = "modem_receive" ;
#endif
peer = rbufp->recv_peer ;
pp = peer->procptr ;
up = pp->unitptr ;
DEBUG_MODEM_PRINTF( sFunctionName ) ;
if ( up->linediscipline == LDISC_RAW ) {
pBuf = up->sTextBuf ;
iLen = up->iTextBufLen ;
} else {
pBuf = pp->a_lastcode ;
iLen = pp->lencode ;
}
if ( iLen == 2 && strncmp( pBuf, "OK" , 2 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_OK ; }
else if ( iLen == 7 && strncmp( pBuf, "CONNECT" , 7 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_CONNECT ; }
else if ( iLen == 4 && strncmp( pBuf, "RING" , 4 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_RING ; }
else if ( iLen == 10 && strncmp( pBuf, "NO CARRIER" , 10 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_NO_CARRIER ; }
else if ( iLen == 5 && strncmp( pBuf, "ERROR" , 5 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_ERROR ; }
else if ( iLen >= 8 && strncmp( pBuf, "CONNECT " , 8 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_CONNECT_X ; }
else if ( iLen == 11 && strncmp( pBuf, "NO DAILTONE", 11 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_NO_DAILTONE ; }
else if ( iLen == 4 && strncmp( pBuf, "BUSY" , 4 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_BUSY ; }
else if ( iLen == 9 && strncmp( pBuf, "NO ANSWER" , 9 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_NO_ANSWER ; }
else { up->iModemEvent = MODEM_EVENT_RESP_UNKNOWN ; }
#ifdef DEBUG
if ( debug ) {
char sResp [ 40 ] ;
size_t iCopyLen ;
iCopyLen = ( iLen <= sizeof(sResp)-1 ? iLen : sizeof(sResp)-1 ) ;
strncpy( sResp, pBuf, iLen <= sizeof(sResp)-1 ? iLen : sizeof(sResp)-1 ) ;
sResp[iCopyLen] = 0 ;
printf ( "refclock_jjy.c : modem_receive : iLen=%zu pBuf=[%s] iModemEvent=%d\n", iCopyLen, sResp, up->iModemEvent ) ;
}
#endif
modem_control ( peer, pp, up ) ;
return 0 ;
}
/**************************************************************************************************/
static void
modem_timer ( int unit, struct peer *peer )
{
struct refclockproc *pp ;
struct jjyunit *up ;
pp = peer->procptr ;
up = pp->unitptr ;
DEBUG_MODEM_PRINTF( "modem_timer" ) ;
if ( iModemSilentTimeout[up->iModemState] != 0 ) {
up->iModemSilentTimer++ ;
if ( iModemSilentTimeout[up->iModemState] <= up->iModemSilentTimer ) {
up->iModemEvent = MODEM_EVENT_SILENT ;
modem_control ( peer, pp, up ) ;
}
}
if ( iModemStateTimeout[up->iModemState] != 0 ) {
up->iModemStateTimer++ ;
if ( iModemStateTimeout[up->iModemState] <= up->iModemStateTimer ) {
up->iModemEvent = MODEM_EVENT_TIMEOUT ;
modem_control ( peer, pp, up ) ;
}
}
}
/**************************************************************************************************/
static void
modem_control ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
int rc ;
short iPostEvent = MODEM_EVENT_NULL ;
DEBUG_MODEM_PRINTF( "modem_control" ) ;
rc = (*pModemHandler[up->iModemEvent][up->iModemState])( peer, pp, up ) ;
if ( rc == CHANGE_MODEM_STATE ) {
iPostEvent = iModemPostEvent[up->iModemEvent][up->iModemState] ;
#ifdef DEBUG
if ( debug ) {
printf( "refclock_jjy.c : modem_control : iModemState=%d -> %d iPostEvent=%d\n",
up->iModemState, iModemNextState[up->iModemEvent][up->iModemState], iPostEvent ) ;
}
#endif
if ( up->iModemState != iModemNextState[up->iModemEvent][up->iModemState] ) {
up->iModemSilentCount = 0 ;
up->iModemStateTimer = 0 ;
up->iModemCommandSeq = 0 ;
}
up->iModemState = iModemNextState[up->iModemEvent][up->iModemState] ;
}
if ( iPostEvent != MODEM_EVENT_NULL ) {
up->iModemEvent = iPostEvent ;
modem_control ( peer, pp, up ) ;
}
up->iModemEvent = MODEM_EVENT_NULL ;
}
/******************************/
static int
modem_disc_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_MODEM_PRINTF( "modem_disc_ignore" ) ;
return STAY_MODEM_STATE ;
}
/******************************/
static int
modem_disc_init ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_MODEM_PRINTF( "modem_disc_init" ) ;
return CHANGE_MODEM_STATE ;
}
/******************************/
static int
modem_init_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_MODEM_PRINTF( "modem_init_ignore" ) ;
return STAY_MODEM_STATE ;
}
/******************************/
static int
modem_init_start ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_MODEM_PRINTF( "modem_init_start" ) ;
up->iModemCommandSeq = 0 ;
#ifdef DEBUG
if ( debug ) {
printf( "refclock_jjy.c : modem_init_start : call modem_init_resp00\n" ) ;
}
#endif
return modem_init_resp00( peer, pp, up ) ;
}
/******************************/
static int
modem_init_resp00 ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
const char * pCmd ;
char cBuf [ 46 ] ;
int iCmdLen ;
int iErrorCorrection, iSpeakerSwitch, iSpeakerVolume ;
int iNextModemState = STAY_MODEM_STATE ;
DEBUG_MODEM_PRINTF( "modem_init_resp00" ) ;
up->iModemCommandSeq++ ;
switch ( up->iModemCommandSeq ) {
case 1 :
/* En = Echoback 0:Off 1:On */
/* Qn = Result codes 0:On 1:Off */
/* Vn = Result codes 0:Numeric 1:Text */
pCmd = "ATE0Q0V1\r\n" ;
break ;
case 2 :
/* Mn = Speaker switch 0:Off 1:On until remote carrier detected 2:On */
if ( ( pp->sloppyclockflag & CLK_FLAG3 ) == 0 ) {
/* fudge 127.127.40.n flag3 0 */
iSpeakerSwitch = 0 ;
} else {
/* fudge 127.127.40.n flag3 1 */
iSpeakerSwitch = 2 ;
}
/* Ln = Speaker volume 0:Very low 1:Low 2:Middle 3:High */
if ( ( pp->sloppyclockflag & CLK_FLAG4 ) == 0 ) {
/* fudge 127.127.40.n flag4 0 */
iSpeakerVolume = 1 ;
} else {
/* fudge 127.127.40.n flag4 1 */
iSpeakerVolume = 2 ;
}
pCmd = cBuf ;
snprintf( cBuf, sizeof(cBuf), "ATM%dL%d\r\n", iSpeakerSwitch, iSpeakerVolume ) ;
break ;
case 3 :
/* &Kn = Flow control 4:XON/XOFF */
pCmd = "AT&K4\r\n" ;
break ;
case 4 :
/* +MS = Protocol V22B:1200,2400bpsiV.22bis) */
pCmd = "AT+MS=V22B\r\n" ;
break ;
case 5 :
/* %Cn = Data compression 0:No data compression */
pCmd = "AT%C0\r\n" ;
break ;
case 6 :
/* \Nn = Error correction 0:Normal mode 1:Direct mode 2:V42,MNP 3:V42,MNP,Normal */
if ( ( pp->sloppyclockflag & CLK_FLAG2 ) == 0 ) {
/* fudge 127.127.40.n flag2 0 */
iErrorCorrection = 0 ;
} else {
/* fudge 127.127.40.n flag2 1 */
iErrorCorrection = 3 ;
}
pCmd = cBuf ;
snprintf( cBuf, sizeof(cBuf), "AT\\N%d\r\n", iErrorCorrection ) ;
break ;
case 7 :
/* Hn = Hook 0:Hook-On ( Disconnect ) 1:Hook-Off ( Connect ) */
pCmd = "ATH1\r\n" ;
break ;
case 8 :
/* Initialize completion */
pCmd = NULL ;
iNextModemState = CHANGE_MODEM_STATE ;
break ;
default :
pCmd = NULL ;
break ;
}
if ( pCmd != NULL ) {
iCmdLen = strlen( pCmd ) ;
if ( write( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
refclock_report( peer, CEVNT_FAULT ) ;
}
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
}
return iNextModemState ;
}
/******************************/
static int
modem_init_resp04 ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_MODEM_PRINTF( "modem_init_resp04" ) ;
return modem_init_resp00( peer, pp, up ) ;
}
/******************************/
static int
modem_init_disc ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_MODEM_PRINTF( "modem_init_disc" ) ;
#ifdef DEBUG
if ( debug ) {
printf( "refclock_jjy.c : modem_init_disc : call modem_esc_disc\n" ) ;
}
#endif
return CHANGE_MODEM_STATE ;
}
/******************************/
static int
modem_dial_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_MODEM_PRINTF( "modem_dial_ignore" ) ;
return STAY_MODEM_STATE ;
}
/******************************/
static int
modem_dial_dialout ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
char sCmd [ 46 ] ;
int iCmdLen ;
char cToneOrPulse ;
DEBUG_MODEM_PRINTF( "modem_dial_dialout" ) ;
/* Tone or Pulse */
if ( ( pp->sloppyclockflag & CLK_FLAG1 ) == 0 ) {
/* fudge 127.127.40.n flag1 0 */
cToneOrPulse = 'T' ;
} else {
/* fudge 127.127.40.n flag1 1 */
cToneOrPulse = 'P' ;
}
/* Connect ( Dial number ) */
snprintf( sCmd, sizeof(sCmd), "ATDW%c%s\r\n", cToneOrPulse, *sys_phone ) ;
/* Send command */
iCmdLen = strlen( sCmd ) ;
if ( write( pp->io.fd, sCmd, iCmdLen ) != iCmdLen ) {
refclock_report( peer, CEVNT_FAULT ) ;
}
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, sCmd ) ;
return STAY_MODEM_STATE ;
}
/******************************/
static int
modem_dial_escape ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_MODEM_PRINTF( "modem_dial_escape" ) ;
#ifdef DEBUG
if ( debug ) {
printf( "refclock_jjy.c : modem_dial_escape : call modem_conn_escape\n" ) ;
}
#endif
return modem_conn_escape( peer, pp, up ) ;
}
/******************************/
static int
modem_dial_connect ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_MODEM_PRINTF( "modem_dial_connect" ) ;
return CHANGE_MODEM_STATE ;
}
/******************************/
static int
modem_dial_disc ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_MODEM_PRINTF( "modem_dial_disc" ) ;
#ifdef DEBUG
if ( debug ) {
printf( "refclock_jjy.c : modem_dial_disc : call modem_esc_disc\n" ) ;
}
#endif
modem_esc_disc( peer, pp, up ) ;
return CHANGE_MODEM_STATE ;
}
/******************************/
static int
modem_conn_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_MODEM_PRINTF( "modem_conn_ignore" ) ;
return STAY_MODEM_STATE ;
}
/******************************/
static int
modem_conn_escape ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_MODEM_PRINTF( "modem_conn_escape" ) ;
return CHANGE_MODEM_STATE ;
}
/******************************/
static int
modem_esc_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_MODEM_PRINTF( "modem_esc_ignore" ) ;
return STAY_MODEM_STATE ;
}
/******************************/
static int
modem_esc_escape ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
const char * pCmd ;
int iCmdLen ;
DEBUG_MODEM_PRINTF( "modem_esc_escape" ) ;
/* Escape command ( Go to command mode ) */
pCmd = "+++" ;
/* Send command */
iCmdLen = strlen( pCmd ) ;
if ( write( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
refclock_report( peer, CEVNT_FAULT ) ;
}
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
return STAY_MODEM_STATE ;
}
/******************************/
static int
modem_esc_data ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_MODEM_PRINTF( "modem_esc_data" ) ;
up->iModemSilentTimer = 0 ;
return STAY_MODEM_STATE ;
}
/******************************/
static int
modem_esc_silent ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
DEBUG_MODEM_PRINTF( "modem_esc_silent" ) ;
up->iModemSilentCount ++ ;
if ( up->iModemSilentCount < iModemStateTimeout[up->iModemState] / iModemSilentTimeout[up->iModemState] ) {
#ifdef DEBUG
if ( debug ) {
printf( "refclock_jjy.c : modem_esc_silent : call modem_esc_escape\n" ) ;
}
#endif
modem_esc_escape( peer, pp, up ) ;
up->iModemSilentTimer = 0 ;
return STAY_MODEM_STATE ;
}
#ifdef DEBUG
if ( debug ) {
printf( "refclock_jjy.c : modem_esc_silent : call modem_esc_disc\n" ) ;
}
#endif
return modem_esc_disc( peer, pp, up ) ;
}
/******************************/
static int
modem_esc_disc ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
{
const char * pCmd ;
int iCmdLen ;
DEBUG_MODEM_PRINTF( "modem_esc_disc" ) ;
/* Disconnect */
pCmd = "ATH0\r\n" ;
/* Send command */
iCmdLen = strlen( pCmd ) ;
if ( write( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
refclock_report( peer, CEVNT_FAULT ) ;
}
jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
return CHANGE_MODEM_STATE ;
}
/*################################################################################################*/
/*################################################################################################*/
/*## ##*/
/*## jjy_write_clockstats ##*/
/*## ##*/
/*################################################################################################*/
/*################################################################################################*/
static void
jjy_write_clockstats ( struct peer *peer, int iMark, const char *pData )
{
char sLog [ 100 ] ;
const char * pMark ;
int iMarkLen, iDataLen ;
switch ( iMark ) {
case JJY_CLOCKSTATS_MARK_JJY :
pMark = "JJY " ;
break ;
case JJY_CLOCKSTATS_MARK_SEND :
pMark = "--> " ;
break ;
case JJY_CLOCKSTATS_MARK_RECEIVE :
pMark = "<-- " ;
break ;
case JJY_CLOCKSTATS_MARK_INFORMATION :
pMark = "--- " ;
break ;
case JJY_CLOCKSTATS_MARK_ATTENTION :
pMark = "=== " ;
break ;
case JJY_CLOCKSTATS_MARK_WARNING :
pMark = "-W- " ;
break ;
case JJY_CLOCKSTATS_MARK_ERROR :
pMark = "-X- " ;
break ;
default :
pMark = "" ;
break ;
}
iDataLen = strlen( pData ) ;
iMarkLen = strlen( pMark ) ;
strcpy( sLog, pMark ) ; /* Harmless because of enough length */
printableString( sLog+iMarkLen, sizeof(sLog)-iMarkLen, pData, iDataLen ) ;
#ifdef DEBUG
if ( debug ) {
printf( "refclock_jjy.c : clockstats : %s\n", sLog ) ;
}
#endif
record_clock_stats( &peer->srcadr, sLog ) ;
}
/*################################################################################################*/
/*################################################################################################*/
/*## ##*/
/*## printableString ##*/
/*## ##*/
/*################################################################################################*/
/*################################################################################################*/
static void
printableString ( char *sOutput, int iOutputLen, const char *sInput, int iInputLen )
{
const char *printableControlChar[] = {
"<NUL>", "<SOH>", "<STX>", "<ETX>",
"<EOT>", "<ENQ>", "<ACK>", "<BEL>",
"<BS>" , "<HT>" , "<LF>" , "<VT>" ,
"<FF>" , "<CR>" , "<SO>" , "<SI>" ,
"<DLE>", "<DC1>", "<DC2>", "<DC3>",
"<DC4>", "<NAK>", "<SYN>", "<ETB>",
"<CAN>", "<EM>" , "<SUB>", "<ESC>",
"<FS>" , "<GS>" , "<RS>" , "<US>" ,
" " } ;
size_t i, j, n ;
size_t InputLen;
size_t OutputLen;
InputLen = (size_t)iInputLen;
OutputLen = (size_t)iOutputLen;
for ( i = j = 0 ; i < InputLen && j < OutputLen ; i ++ ) {
if ( isprint( (unsigned char)sInput[i] ) ) {
n = 1 ;
if ( j + 1 >= OutputLen )
break ;
sOutput[j] = sInput[i] ;
} else if ( ( sInput[i] & 0xFF ) <
COUNTOF(printableControlChar) ) {
n = strlen( printableControlChar[sInput[i] & 0xFF] ) ;
if ( j + n + 1 >= OutputLen )
break ;
strlcpy( sOutput + j,
printableControlChar[sInput[i] & 0xFF],
OutputLen - j ) ;
} else {
n = 5 ;
if ( j + n + 1 >= OutputLen )
break ;
snprintf( sOutput + j, OutputLen - j, "<x%X>",
sInput[i] & 0xFF ) ;
}
j += n ;
}
sOutput[min(j, (size_t)iOutputLen - 1)] = '\0' ;
}
/**************************************************************************************************/
#else
int refclock_jjy_bs ;
#endif /* REFCLOCK */
|