Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
/*	$NetBSD: refclock_gpsdjson.c,v 1.13 2020/05/25 20:47:25 christos Exp $	*/

/*
 * refclock_gpsdjson.c - clock driver as GPSD JSON client
 *	Juergen Perlinger (perlinger@ntp.org)
 *	Feb 11, 2014 for the NTP project.
 *      The contents of 'html/copyright.html' apply.
 *
 *	Heavily inspired by refclock_nmea.c
 *
 * Special thanks to Gary Miller and Hal Murray for their comments and
 * ideas.
 *
 * Note: This will currently NOT work with Windows due to some
 * limitations:
 *
 *  - There is no GPSD for Windows. (There is an unofficial port to
 *    cygwin, but Windows is not officially supported.)
 *
 *  - To work properly, this driver needs PPS and TPV/TOFF sentences
 *    from GPSD. I don't see how the cygwin port should deal with the
 *    PPS signal.
 *
 *  - The device name matching must be done in a different way for
 *    Windows. (Can be done with COMxx matching, as done for NMEA.)
 *
 * Apart from those minor hickups, once GPSD has been fully ported to
 * Windows, there's no reason why this should not work there ;-) If this
 * is ever to happen at all is a different question.
 *
 * ---------------------------------------------------------------------
 *
 * This driver works slightly different from most others, as the PPS
 * information (if available) is also coming from GPSD via the data
 * connection. This makes using both the PPS data and the serial data
 * easier, but OTOH it's not possible to use the ATOM driver to feed a
 * raw PPS stream to the core of NTPD.
 *
 * To go around this, the driver can use a secondary clock unit
 * (units>=128) that operate in tandem with the primary clock unit
 * (unit%128). The primary clock unit does all the IO stuff and data
 * decoding; if a a secondary unit is attached to a primary unit, this
 * secondary unit is feed with the PPS samples only and can act as a PPS
 * source to the clock selection.
 *
 * The drawback is that the primary unit must be present for the
 * secondary unit to work.
 *
 * This design is a compromise to reduce the IO load for both NTPD and
 * GPSD; it also ensures that data is transmitted and evaluated only
 * once on the side of NTPD.
 *
 * ---------------------------------------------------------------------
 *
 * trouble shooting hints:
 *
 *   Enable and check the clock stats. Check if there are bad replies;
 *   there should be none. If there are actually bad replies, then the
 *   driver cannot parse all JSON records from GPSD, and some record
 *   types are vital for the operation of the driver. This indicates a
 *   problem on the protocol level.
 *
 *   When started on the command line with a debug level >= 2, the
 *   driver dumps the raw received data and the parser input to
 *   stdout. Since the debug level is global, NTPD starts to create a
 *   *lot* of output. It makes sense to pipe it through '(f)grep
 *   GPSD_JSON' before writing the result to disk.
 *
 *   A bit less intrusive is using netcat or telnet to connect to GPSD
 *   and snoop what NTPD would get. If you try this, you have to send a
 *   WATCH command to GPSD:
 *
 * ?WATCH={"device":"/dev/gps0","enable":true,"json":true,"pps":true};<CRLF>
 *
 *   should show you what GPSD has to say to NTPD. Replace "/dev/gps0"
 *   with the device link used by GPSD, if necessary.
 */


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

#include "ntp_types.h"

#if defined(REFCLOCK) && defined(CLOCK_GPSDJSON) && !defined(SYS_WINNT)

/* =====================================================================
 * Get the little JSMN library directly into our guts. Use the 'parent
 * link' feature for maximum speed.
 */
#define JSMN_PARENT_LINKS
#include "../libjsmn/jsmn.c"

/* =====================================================================
 * JSON parsing stuff
 */

#define JSMN_MAXTOK	350
#define INVALID_TOKEN (-1)

typedef struct json_ctx {
	char        * buf;
	int           ntok;
	jsmntok_t     tok[JSMN_MAXTOK];
} json_ctx;

typedef int tok_ref;

/* Not all targets have 'long long', and not all of them have 'strtoll'.
 * Sigh. We roll our own integer number parser.
 */
#ifdef HAVE_LONG_LONG
typedef signed   long long int json_int;
typedef unsigned long long int json_uint;
#define JSON_INT_MAX LLONG_MAX
#define JSON_INT_MIN LLONG_MIN
#else
typedef signed   long int json_int;
typedef unsigned long int json_uint;
#define JSON_INT_MAX LONG_MAX
#define JSON_INT_MIN LONG_MIN
#endif

/* =====================================================================
 * header stuff we need
 */

#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/tcp.h>

#if defined(HAVE_SYS_POLL_H)
# include <sys/poll.h>
#elif defined(HAVE_SYS_SELECT_H)
# include <sys/select.h>
#else
# error need poll() or select()
#endif

#include "ntpd.h"
#include "ntp_io.h"
#include "ntp_unixtime.h"
#include "ntp_refclock.h"
#include "ntp_stdlib.h"
#include "ntp_calendar.h"
#include "timespecops.h"

/* get operation modes from mode word.

 * + SERIAL (default) evaluates only serial time information ('STI') as
 *   provided by TPV and TOFF records. TPV evaluation suffers from a
 *   bigger jitter than TOFF, sine it does not contain the receive time
 *   from GPSD and therefore the receive time of NTPD must be
 *   substituted for it. The network latency makes this a second rate
 *   guess.
 *
 *   If TOFF records are detected in the data stream, the timing
 *   information is gleaned from this record -- it contains the local
 *   receive time stamp from GPSD and therefore eliminates the
 *   transmission latency between GPSD and NTPD. The timing information
 *   from TPV is ignored once a TOFF is detected or expected.
 *
 *   TPV is still used to check the fix status, so the driver can stop
 *   feeding samples when GPSD says that the time information is
 *   effectively unreliable.
 *
 * + STRICT means only feed clock samples when a valid STI/PPS pair is
 *   available. Combines the reference time from STI with the pulse time
 *   from PPS. Masks the serial data jitter as long PPS is available,
 *   but can rapidly deteriorate once PPS drops out.
 *
 * + AUTO tries to use STI/PPS pairs if available for some time, and if
 *   this fails for too long switches back to STI only until the PPS
 *   signal becomes available again. See the HTML docs for this driver
 *   about the gotchas and why this is not the default.
 */
#define MODE_OP_MASK   0x03
#define MODE_OP_STI    0
#define MODE_OP_STRICT 1
#define MODE_OP_AUTO   2
#define MODE_OP_MAXVAL 2
#define MODE_OP_MODE(x)		((x) & MODE_OP_MASK)

#define	PRECISION	(-9)	/* precision assumed (about 2 ms) */
#define	PPS_PRECISION	(-20)	/* precision assumed (about 1 us) */
#define	REFID		"GPSD"	/* reference id */
#define	DESCRIPTION	"GPSD JSON client clock" /* who we are */

#define MAX_PDU_LEN	1600
#define TICKOVER_LOW	10
#define TICKOVER_HIGH	120
#define LOGTHROTTLE	3600

/* Primary channel PPS avilability dance:
 * Every good PPS sample gets us a credit of PPS_INCCOUNT points, every
 * bad/missing PPS sample costs us a debit of PPS_DECCOUNT points. When
 * the account reaches the upper limit we change to a mode where only
 * PPS-augmented samples are fed to the core; when the account drops to
 * zero we switch to a mode where TPV-only timestamps are fed to the
 * core.
 * This reduces the chance of rapid alternation between raw and
 * PPS-augmented time stamps.
 */
#define PPS_MAXCOUNT	60	/* upper limit of account  */
#define PPS_INCCOUNT     3	/* credit for good samples */
#define PPS_DECCOUNT     1	/* debit for bad samples   */

/* The secondary (PPS) channel uses a different strategy to avoid old
 * PPS samples in the median filter.
 */
#define PPS2_MAXCOUNT 10

#ifndef BOOL
# define BOOL int
#endif
#ifndef TRUE
# define TRUE 1
#endif
#ifndef FALSE
# define FALSE 0
#endif

#define PROTO_VERSION(hi,lo) \
	    ((((uint32_t)(hi) << 16) & 0xFFFF0000u) | \
	     ((uint32_t)(lo) & 0x0FFFFu))

/* some local typedefs: The NTPD formatting style cries for short type
 * names, and we provide them locally. Note:the suffix '_t' is reserved
 * for the standard; I use a capital T instead.
 */
typedef struct peer         peerT;
typedef struct refclockproc clockprocT;
typedef struct addrinfo     addrinfoT;

/* =====================================================================
 * We use the same device name scheme as does the NMEA driver; since
 * GPSD supports the same links, we can select devices by a fixed name.
 */
static const char * s_dev_stem = "/dev/gps";

/* =====================================================================
 * forward declarations for transfer vector and the vector itself
 */

static	void	gpsd_init	(void);
static	int	gpsd_start	(int, peerT *);
static	void	gpsd_shutdown	(int, peerT *);
static	void	gpsd_receive	(struct recvbuf *);
static	void	gpsd_poll	(int, peerT *);
static	void	gpsd_control	(int, const struct refclockstat *,
				 struct refclockstat *, peerT *);
static	void	gpsd_timer	(int, peerT *);

static  int     myasprintf(char**, char const*, ...) NTP_PRINTF(2, 3);

static void     enter_opmode(peerT *peer, int mode);
static void	leave_opmode(peerT *peer, int mode);

struct refclock refclock_gpsdjson = {
	gpsd_start,		/* start up driver */
	gpsd_shutdown,		/* shut down driver */
	gpsd_poll,		/* transmit poll message */
	gpsd_control,		/* fudge control */
	gpsd_init,		/* initialize driver */
	noentry,		/* buginfo */
	gpsd_timer		/* called once per second */
};

/* =====================================================================
 * our local clock unit and data
 */
struct gpsd_unit;
typedef struct gpsd_unit gpsd_unitT;

struct gpsd_unit {
	/* links for sharing between master/slave units */
	gpsd_unitT *next_unit;
	size_t      refcount;

	/* data for the secondary PPS channel */
	peerT      *pps_peer;

	/* unit and operation modes */
	int      unit;
	int      mode;
	char    *logname;	/* cached name for log/print */
	char    * device;	/* device name of unit */

	/* current line protocol version */
	uint32_t proto_version;

	/* PPS time stamps primary + secondary channel */
	l_fp pps_local;	/* when we received the PPS message */
	l_fp pps_stamp;	/* related reference time */
	l_fp pps_recvt;	/* when GPSD detected the pulse */
	l_fp pps_stamp2;/* related reference time (secondary) */
	l_fp pps_recvt2;/* when GPSD detected the pulse (secondary)*/
	int  ppscount;	/* PPS counter (primary unit) */
	int  ppscount2;	/* PPS counter (secondary unit) */

	/* TPV or TOFF serial time information */
	l_fp sti_local;	/* when we received the TPV/TOFF message */
	l_fp sti_stamp;	/* effective GPS time stamp */
	l_fp sti_recvt;	/* when GPSD got the fix */

	/* precision estimates */
	int16_t	    sti_prec;	/* serial precision based on EPT */
	int16_t     pps_prec;	/* PPS precision from GPSD or above */

	/* fudge values for correction, mirrored as 'l_fp' */
	l_fp pps_fudge;		/* PPS fudge primary channel */
	l_fp pps_fudge2;	/* PPS fudge secondary channel */
	l_fp sti_fudge;		/* TPV/TOFF serial data fudge */

	/* Flags to indicate available data */
	int fl_nosync: 1;	/* GPSD signals bad quality */
	int fl_sti   : 1;	/* valid TPV/TOFF seen (have time) */
	int fl_pps   : 1;	/* valid pulse seen */
	int fl_pps2  : 1;	/* valid pulse seen for PPS channel */
	int fl_rawsti: 1;	/* permit raw TPV/TOFF time stamps */
	int fl_vers  : 1;	/* have protocol version */
	int fl_watch : 1;	/* watch reply seen */
	/* protocol flags */
	int pf_nsec  : 1;	/* have nanosec PPS info */
	int pf_toff  : 1;	/* have TOFF record for timing */

	/* admin stuff for sockets and device selection */
	int         fdt;	/* current connecting socket */
	addrinfoT * addr;	/* next address to try */
	u_int       tickover;	/* timeout countdown */
	u_int       tickpres;	/* timeout preset */

	/* tallies for the various events */
	u_int       tc_recv;	/* received known records */
	u_int       tc_breply;	/* bad replies / parsing errors */
	u_int       tc_nosync;	/* TPV / sample cycles w/o fix */
	u_int       tc_sti_recv;/* received serial time info records */
	u_int       tc_sti_used;/* used        --^-- */
	u_int       tc_pps_recv;/* received PPS timing info records */
	u_int       tc_pps_used;/* used        --^-- */

	/* log bloat throttle */
	u_int       logthrottle;/* seconds to next log slot */

	/* The parse context for the current record */
	json_ctx    json_parse;

	/* record assemby buffer and saved length */
	int  buflen;
	char buffer[MAX_PDU_LEN];
};

/* =====================================================================
 * static local helpers forward decls
 */
static void gpsd_init_socket(peerT * const peer);
static void gpsd_test_socket(peerT * const peer);
static void gpsd_stop_socket(peerT * const peer);

static void gpsd_parse(peerT * const peer,
		       const l_fp  * const rtime);
static BOOL convert_ascii_time(l_fp * fp, const char * gps_time);
static void save_ltc(clockprocT * const pp, const char * const tc);
static int  syslogok(clockprocT * const pp, gpsd_unitT * const up);
static void log_data(peerT *peer, const char *what,
		     const char *buf, size_t len);
static int16_t clamped_precision(int rawprec);

/* =====================================================================
 * local / static stuff
 */

static const char * const s_req_version =
    "?VERSION;\r\n";

/* We keep a static list of network addresses for 'localhost:gpsd' or a
 * fallback alias of it, and we try to connect to them in round-robin
 * fashion. The service lookup is done during the driver init
 * function to minmise the impact of 'getaddrinfo()'.
 *
 * Alas, the init function is called even if there are no clocks
 * configured for this driver. So it makes sense to defer the logging of
 * any errors or other notifications until the first clock unit is
 * started -- otherwise there might be syslog entries from a driver that
 * is not used at all.
 */
static addrinfoT  *s_gpsd_addr;
static gpsd_unitT *s_clock_units;

/* list of service/socket names we want to resolve against */
static const char * const s_svctab[][2] = {
	{ "localhost", "gpsd" },
	{ "localhost", "2947" },
	{ "127.0.0.1", "2947" },
	{ NULL, NULL }
};

/* list of address resolution errors and index of service entry that
 * finally worked.
 */
static int s_svcerr[sizeof(s_svctab)/sizeof(s_svctab[0])];
static int s_svcidx;

/* =====================================================================
 * log throttling
 */
static int/*BOOL*/
syslogok(
	clockprocT * const pp,
	gpsd_unitT * const up)
{
	int res = (0 != (pp->sloppyclockflag & CLK_FLAG3))
	       || (0           == up->logthrottle )
	       || (LOGTHROTTLE == up->logthrottle );
	if (res)
		up->logthrottle = LOGTHROTTLE;
	return res;
}

/* =====================================================================
 * the clock functions
 */

/* ---------------------------------------------------------------------
 * Init: This currently just gets the socket address for the GPS daemon
 */
static void
gpsd_init(void)
{
	addrinfoT   hints;
	int         rc, idx;

	memset(s_svcerr, 0, sizeof(s_svcerr));
	memset(&hints, 0, sizeof(hints));
	hints.ai_family   = AF_UNSPEC;
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_socktype = SOCK_STREAM;

	for (idx = 0; s_svctab[idx][0] && !s_gpsd_addr; idx++) {
		rc = getaddrinfo(s_svctab[idx][0], s_svctab[idx][1],
				 &hints, &s_gpsd_addr);
		s_svcerr[idx] = rc;
		if (0 == rc)
			break;
		s_gpsd_addr = NULL;
	}
	s_svcidx = idx;
}

/* ---------------------------------------------------------------------
 * Init Check: flush pending log messages and check if we can proceed
 */
static int/*BOOL*/
gpsd_init_check(void)
{
	int idx;

	/* Check if there is something to log */
	if (s_svcidx == 0)
		return (s_gpsd_addr != NULL);

	/* spool out the resolver errors */
	for (idx = 0; idx < s_svcidx; ++idx) {
		msyslog(LOG_WARNING,
			"GPSD_JSON: failed to resolve '%s:%s', rc=%d (%s)",
			s_svctab[idx][0], s_svctab[idx][1],
			s_svcerr[idx], gai_strerror(s_svcerr[idx]));
	}

	/* check if it was fatal, or if we can proceed */
	if (s_gpsd_addr == NULL)
		msyslog(LOG_ERR, "%s",
			"GPSD_JSON: failed to get socket address, giving up.");
	else if (idx != 0)
		msyslog(LOG_WARNING,
			"GPSD_JSON: using '%s:%s' instead of '%s:%s'",
			s_svctab[idx][0], s_svctab[idx][1],
			s_svctab[0][0], s_svctab[0][1]);

	/* make sure this gets logged only once and tell if we can
	 * proceed or not
	 */
	s_svcidx = 0;
	return (s_gpsd_addr != NULL);
}

/* ---------------------------------------------------------------------
 * Start: allocate a unit pointer and set up the runtime data
 */
static int
gpsd_start(
	int     unit,
	peerT * peer)
{
	clockprocT  * const pp = peer->procptr;
	gpsd_unitT  * up;
	gpsd_unitT ** uscan    = &s_clock_units;

	struct stat sb;

	/* check if we can proceed at all or if init failed */
	if ( ! gpsd_init_check())
		return FALSE;

	/* search for matching unit */
	while ((up = *uscan) != NULL && up->unit != (unit & 0x7F))
		uscan = &up->next_unit;
	if (up == NULL) {
		/* alloc unit, add to list and increment use count ASAP. */
		up = emalloc_zero(sizeof(*up));
		*uscan = up;
		++up->refcount;

		/* initialize the unit structure */
		up->logname  = estrdup(refnumtoa(&peer->srcadr));
		up->unit     = unit & 0x7F;
		up->fdt      = -1;
		up->addr     = s_gpsd_addr;
		up->tickpres = TICKOVER_LOW;

		/* Create the device name and check for a Character
		 * Device. It's assumed that GPSD was started with the
		 * same link, so the names match. (If this is not
		 * practicable, we will have to read the symlink, if
		 * any, so we can get the true device file.)
		 */
		if (-1 == myasprintf(&up->device, "%s%u",
				     s_dev_stem, up->unit)) {
			msyslog(LOG_ERR, "%s: clock device name too long",
				up->logname);
			goto dev_fail;
		}
		if (-1 == stat(up->device, &sb) || !S_ISCHR(sb.st_mode)) {
			msyslog(LOG_ERR, "%s: '%s' is not a character device",
				up->logname, up->device);
			goto dev_fail;
		}
	} else {
		/* All set up, just increment use count. */
		++up->refcount;
	}
	
	/* setup refclock processing */
	pp->unitptr = (caddr_t)up;
	pp->io.fd         = -1;
	pp->io.clock_recv = gpsd_receive;
	pp->io.srcclock   = peer;
	pp->io.datalen    = 0;
	pp->a_lastcode[0] = '\0';
	pp->lencode       = 0;
	pp->clockdesc     = DESCRIPTION;
	memcpy(&pp->refid, REFID, 4);

	/* Initialize miscellaneous variables */
	if (unit >= 128)
		peer->precision = PPS_PRECISION;
	else
		peer->precision = PRECISION;

	/* If the daemon name lookup failed, just give up now. */
	if (NULL == up->addr) {
		msyslog(LOG_ERR, "%s: no GPSD socket address, giving up",
			up->logname);
		goto dev_fail;
	}

	LOGIF(CLOCKINFO,
	      (LOG_NOTICE, "%s: startup, device is '%s'",
	       refnumtoa(&peer->srcadr), up->device));
	up->mode = MODE_OP_MODE(peer->ttl);
	if (up->mode > MODE_OP_MAXVAL)
		up->mode = 0;
	if (unit >= 128)
		up->pps_peer = peer;
	else
		enter_opmode(peer, up->mode);
	return TRUE;

dev_fail:
	/* On failure, remove all UNIT ressources and declare defeat. */

	INSIST (up);
	if (!--up->refcount) {
		*uscan = up->next_unit;
		free(up->device);
		free(up);
	}

	pp->unitptr = (caddr_t)NULL;
	return FALSE;
}

/* ------------------------------------------------------------------ */

static void
gpsd_shutdown(
	int     unit,
	peerT * peer)
{
	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;
	gpsd_unitT ** uscan   = &s_clock_units;

	UNUSED_ARG(unit);

	/* The unit pointer might have been removed already. */
	if (up == NULL)
		return;

	/* now check if we must close IO resources */
	if (peer != up->pps_peer) {
		if (-1 != pp->io.fd) {
			DPRINTF(1, ("%s: closing clock, fd=%d\n",
				    up->logname, pp->io.fd));
			io_closeclock(&pp->io);
			pp->io.fd = -1;
		}
		if (up->fdt != -1)
			close(up->fdt);
	}
	/* decrement use count and eventually remove this unit. */
	if (!--up->refcount) {
		/* unlink this unit */
		while (*uscan != NULL)
			if (*uscan == up)
				*uscan = up->next_unit;
			else
				uscan = &(*uscan)->next_unit;
		free(up->logname);
		free(up->device);
		free(up);
	}
	pp->unitptr = (caddr_t)NULL;
	LOGIF(CLOCKINFO,
	      (LOG_NOTICE, "%s: shutdown", refnumtoa(&peer->srcadr)));
}

/* ------------------------------------------------------------------ */

static void
gpsd_receive(
	struct recvbuf * rbufp)
{
	/* declare & init control structure ptrs */
	peerT	   * const peer = rbufp->recv_peer;
	clockprocT * const pp   = peer->procptr;
	gpsd_unitT * const up   = (gpsd_unitT *)pp->unitptr;

	const char *psrc, *esrc;
	char       *pdst, *edst, ch;

	/* log the data stream, if this is enabled */
	log_data(peer, "recv", (const char*)rbufp->recv_buffer,
		 (size_t)rbufp->recv_length);


	/* Since we're getting a raw stream data, we must assemble lines
	 * in our receive buffer. We can't use neither 'refclock_gtraw'
	 * not 'refclock_gtlin' here...  We process chars until we reach
	 * an EoL (that is, line feed) but we truncate the message if it
	 * does not fit the buffer.  GPSD might truncate messages, too,
	 * so dealing with truncated buffers is necessary anyway.
	 */
	psrc = (const char*)rbufp->recv_buffer;
	esrc = psrc + rbufp->recv_length;

	pdst = up->buffer + up->buflen;
	edst = pdst + sizeof(up->buffer) - 1; /* for trailing NUL */

	while (psrc != esrc) {
		ch = *psrc++;
		if (ch == '\n') {
			/* trim trailing whitespace & terminate buffer */
			while (pdst != up->buffer && pdst[-1] <= ' ')
				--pdst;
			*pdst = '\0';
			/* process data and reset buffer */
			up->buflen = pdst - up->buffer;
			gpsd_parse(peer, &rbufp->recv_time);
			pdst = up->buffer;
		} else if (pdst != edst) {
			/* add next char, ignoring leading whitespace */
			if (ch > ' ' || pdst != up->buffer)
				*pdst++ = ch;
		}
	}
	up->buflen   = pdst - up->buffer;
	up->tickover = TICKOVER_LOW;
}

/* ------------------------------------------------------------------ */

static void
poll_primary(
	peerT      * const peer ,
	clockprocT * const pp   ,
	gpsd_unitT * const up   )
{
	if (pp->coderecv != pp->codeproc) {
		/* all is well */
		pp->lastref = pp->lastrec;
		refclock_report(peer, CEVNT_NOMINAL);
		refclock_receive(peer);
	} else {
		/* Not working properly, admit to it. If we have no
		 * connection to GPSD, declare the clock as faulty. If
		 * there were bad replies, this is handled as the major
		 * cause, and everything else is just a timeout.
		 */
		peer->precision = PRECISION;
		if (-1 == pp->io.fd)
			refclock_report(peer, CEVNT_FAULT);
		else if (0 != up->tc_breply)
			refclock_report(peer, CEVNT_BADREPLY);
		else
			refclock_report(peer, CEVNT_TIMEOUT);
	}

	if (pp->sloppyclockflag & CLK_FLAG4)
		mprintf_clock_stats(
			&peer->srcadr,"%u %u %u %u %u %u %u",
			up->tc_recv,
			up->tc_breply, up->tc_nosync,
			up->tc_sti_recv, up->tc_sti_used,
			up->tc_pps_recv, up->tc_pps_used);

	/* clear tallies for next round */
	up->tc_breply   = 0;
	up->tc_recv     = 0;
	up->tc_nosync   = 0;
	up->tc_sti_recv = 0;
	up->tc_sti_used = 0;
	up->tc_pps_recv = 0;
	up->tc_pps_used = 0;
}

static void
poll_secondary(
	peerT      * const peer ,
	clockprocT * const pp   ,
	gpsd_unitT * const up   )
{
	if (pp->coderecv != pp->codeproc) {
		/* all is well */
		pp->lastref = pp->lastrec;
		refclock_report(peer, CEVNT_NOMINAL);
		refclock_receive(peer);
	} else {
		peer->precision = PPS_PRECISION;
		peer->flags &= ~FLAG_PPS;
		refclock_report(peer, CEVNT_TIMEOUT);
	}
}

static void
gpsd_poll(
	int     unit,
	peerT * peer)
{
	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;

	++pp->polls;
	if (peer == up->pps_peer)
		poll_secondary(peer, pp, up);
	else
		poll_primary(peer, pp, up);
}

/* ------------------------------------------------------------------ */

static void
gpsd_control(
	int                         unit,
	const struct refclockstat * in_st,
	struct refclockstat       * out_st,
	peerT                     * peer  )
{
	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;

	if (peer == up->pps_peer) {
		DTOLFP(pp->fudgetime1, &up->pps_fudge2);
		if ( ! (pp->sloppyclockflag & CLK_FLAG1))
			peer->flags &= ~FLAG_PPS;
	} else {
		/* save preprocessed fudge times */
		DTOLFP(pp->fudgetime1, &up->pps_fudge);
		DTOLFP(pp->fudgetime2, &up->sti_fudge);

		if (MODE_OP_MODE(up->mode ^ peer->ttl)) {
			leave_opmode(peer, up->mode);
			up->mode = MODE_OP_MODE(peer->ttl);
			enter_opmode(peer, up->mode);
		}
	}
 }

/* ------------------------------------------------------------------ */

static void
timer_primary(
	peerT      * const peer ,
	clockprocT * const pp   ,
	gpsd_unitT * const up   )
{
	int rc;

	/* This is used for timeout handling. Nothing that needs
	 * sub-second precison happens here, so receive/connect/retry
	 * timeouts are simply handled by a count down, and then we
	 * decide what to do by the socket values.
	 *
	 * Note that the timer stays at zero here, unless some of the
	 * functions set it to another value.
	 */
	if (up->logthrottle)
		--up->logthrottle;
	if (up->tickover)
		--up->tickover;
	switch (up->tickover) {
	case 4:
		/* If we are connected to GPSD, try to get a live signal
		 * by querying the version. Otherwise just check the
		 * socket to become ready.
		 */
		if (-1 != pp->io.fd) {
			size_t rlen = strlen(s_req_version);
			DPRINTF(2, ("%s: timer livecheck: '%s'\n",
				    up->logname, s_req_version));
			log_data(peer, "send", s_req_version, rlen);
			rc = write(pp->io.fd, s_req_version, rlen);
			(void)rc;
		} else if (-1 != up->fdt) {
			gpsd_test_socket(peer);
		}
		break;

	case 0:
		if (-1 != pp->io.fd)
			gpsd_stop_socket(peer);
		else if (-1 != up->fdt)
			gpsd_test_socket(peer);
		else if (NULL != s_gpsd_addr)
			gpsd_init_socket(peer);
		break;

	default:
		if (-1 == pp->io.fd && -1 != up->fdt)
			gpsd_test_socket(peer);
	}
}

static void
timer_secondary(
	peerT      * const peer ,
	clockprocT * const pp   ,
	gpsd_unitT * const up   )
{
	/* Reduce the count by one. Flush sample buffer and clear PPS
	 * flag when this happens.
	 */
	up->ppscount2 = max(0, (up->ppscount2 - 1));
	if (0 == up->ppscount2) {
		if (pp->coderecv != pp->codeproc) {
			refclock_report(peer, CEVNT_TIMEOUT);
			pp->coderecv = pp->codeproc;
		}
		peer->flags &= ~FLAG_PPS;
	}
}

static void
gpsd_timer(
	int     unit,
	peerT * peer)
{
	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;

	if (peer == up->pps_peer)
		timer_secondary(peer, pp, up);
	else
		timer_primary(peer, pp, up);
}

/* =====================================================================
 * handle opmode switches
 */

static void
enter_opmode(
	peerT *peer,
	int    mode)
{
	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;

	DPRINTF(1, ("%s: enter operation mode %d\n",
		    up->logname, MODE_OP_MODE(mode)));

	if (MODE_OP_MODE(mode) == MODE_OP_AUTO) {
		up->fl_rawsti = 0;
		up->ppscount  = PPS_MAXCOUNT / 2;
	}
	up->fl_pps = 0;
	up->fl_sti = 0;
}

/* ------------------------------------------------------------------ */

static void
leave_opmode(
	peerT *peer,
	int    mode)
{
	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;

	DPRINTF(1, ("%s: leaving operation mode %d\n",
		    up->logname, MODE_OP_MODE(mode)));

	if (MODE_OP_MODE(mode) == MODE_OP_AUTO) {
		up->fl_rawsti = 0;
		up->ppscount  = 0;
	}
	up->fl_pps = 0;
	up->fl_sti = 0;
}

/* =====================================================================
 * operation mode specific evaluation
 */

static void
add_clock_sample(
	peerT      * const peer ,
	clockprocT * const pp   ,
	l_fp               stamp,
	l_fp               recvt)
{
	pp->lastref = stamp;
	if (pp->coderecv == pp->codeproc)
		refclock_report(peer, CEVNT_NOMINAL);
	refclock_process_offset(pp, stamp, recvt, 0.0);
}

/* ------------------------------------------------------------------ */

static void
eval_strict(
	peerT      * const peer ,
	clockprocT * const pp   ,
	gpsd_unitT * const up   )
{
	if (up->fl_sti && up->fl_pps) {
		/* use TPV reference time + PPS receive time */
		add_clock_sample(peer, pp, up->sti_stamp, up->pps_recvt);
		peer->precision = up->pps_prec;
		/* both packets consumed now... */
		up->fl_pps = 0;
		up->fl_sti = 0;
		++up->tc_sti_used;
	}
}

/* ------------------------------------------------------------------ */
/* PPS processing for the secondary channel. GPSD provides us with full
 * timing information, so there's no danger of PLL-locking to the wrong
 * second. The belts and suspenders needed for the raw ATOM clock are
 * unnecessary here.
 */
static void
eval_pps_secondary(
	peerT      * const peer ,
	clockprocT * const pp   ,
	gpsd_unitT * const up   )
{
	if (up->fl_pps2) {
		/* feed data */
		add_clock_sample(peer, pp, up->pps_stamp2, up->pps_recvt2);
		peer->precision = up->pps_prec;
		/* PPS peer flag logic */
		up->ppscount2 = min(PPS2_MAXCOUNT, (up->ppscount2 + 2));
		if ((PPS2_MAXCOUNT == up->ppscount2) &&
		    (pp->sloppyclockflag & CLK_FLAG1) )
			peer->flags |= FLAG_PPS;
		/* mark time stamp as burned... */
		up->fl_pps2 = 0;
		++up->tc_pps_used;
	}
}

/* ------------------------------------------------------------------ */

static void
eval_serial(
	peerT      * const peer ,
	clockprocT * const pp   ,
	gpsd_unitT * const up   )
{
	if (up->fl_sti) {
		add_clock_sample(peer, pp, up->sti_stamp, up->sti_recvt);
		peer->precision = up->sti_prec;
		/* mark time stamp as burned... */
		up->fl_sti = 0;
		++up->tc_sti_used;
	}
}

/* ------------------------------------------------------------------ */
static void
eval_auto(
	peerT      * const peer ,
	clockprocT * const pp   ,
	gpsd_unitT * const up   )
{
	/* If there's no TPV available, stop working here... */
	if (!up->fl_sti)
		return;

	/* check how to handle STI+PPS: Can PPS be used to augment STI
	 * (or vice versae), do we drop the sample because there is a
	 * temporary missing PPS signal, or do we feed on STI time
	 * stamps alone?
	 *
	 * Do a counter/threshold dance to decide how to proceed.
	 */
	if (up->fl_pps) {
		up->ppscount = min(PPS_MAXCOUNT,
				   (up->ppscount + PPS_INCCOUNT));
		if ((PPS_MAXCOUNT == up->ppscount) && up->fl_rawsti) {
			up->fl_rawsti = 0;
			msyslog(LOG_INFO,
				"%s: expect valid PPS from now",
				up->logname);
		}
	} else {
		up->ppscount = max(0, (up->ppscount - PPS_DECCOUNT));
		if ((0 == up->ppscount) && !up->fl_rawsti) {
			up->fl_rawsti = -1;
			msyslog(LOG_WARNING,
				"%s: use TPV alone from now",
				up->logname);
		}
	}

	/* now eventually feed the sample */
	if (up->fl_rawsti)
		eval_serial(peer, pp, up);
	else
		eval_strict(peer, pp, up);
}

/* =====================================================================
 * JSON parsing stuff
 */

/* ------------------------------------------------------------------ */
/* Parse a decimal integer with a possible sign. Works like 'strtoll()'
 * or 'strtol()', but with a fixed base of 10 and without eating away
 * leading whitespace. For the error codes, the handling of the end
 * pointer and the return values see 'strtol()'.
 */
static json_int
strtojint(
	const char *cp, char **ep)
{
	json_uint     accu, limit_lo, limit_hi;
	int           flags; /* bit 0: overflow; bit 1: sign */
	const char  * hold;

	/* pointer union to circumvent a tricky/sticky const issue */
	union {	const char * c; char * v; } vep;

	/* store initial value of 'cp' -- see 'strtol()' */
	vep.c = cp;

	/* Eat away an optional sign and set the limits accordingly: The
	 * high limit is the maximum absolute value that can be returned,
	 * and the low limit is the biggest value that does not cause an
	 * overflow when multiplied with 10. Avoid negation overflows.
	 */
	if (*cp == '-') {
		cp += 1;
		flags    = 2;
		limit_hi = (json_uint)-(JSON_INT_MIN + 1) + 1;
	} else {
		cp += (*cp == '+');
		flags    = 0;
		limit_hi = (json_uint)JSON_INT_MAX;
	}
	limit_lo = limit_hi / 10;

	/* Now try to convert a sequence of digits. */
	hold = cp;
	accu = 0;
	while (isdigit(*(const u_char*)cp)) {
		flags |= (accu > limit_lo);
		accu = accu * 10 + (*(const u_char*)cp++ - '0');
		flags |= (accu > limit_hi);
	}
	/* Check for empty conversion (no digits seen). */
	if (hold != cp)
		vep.c = cp;
	else
		errno = EINVAL;	/* accu is still zero */
	/* Check for range overflow */
	if (flags & 1) {
		errno = ERANGE;
		accu  = limit_hi;
	}
	/* If possible, store back the end-of-conversion pointer */
	if (ep)
		*ep = vep.v;
	/* If negative, return the negated result if the accu is not
	 * zero. Avoid negation overflows.
	 */
	if ((flags & 2) && accu)
		return -(json_int)(accu - 1) - 1;
	else
		return (json_int)accu;
}

/* ------------------------------------------------------------------ */

static tok_ref
json_token_skip(
	const json_ctx * ctx,
	tok_ref          tid)
{
	if (tid >= 0 && tid < ctx->ntok) {
		int len = ctx->tok[tid].size;
		/* For arrays and objects, the size is the number of
		 * ITEMS in the compound. Thats the number of objects in
		 * the array, and the number of key/value pairs for
		 * objects. In theory, the key must be a string, and we
		 * could simply skip one token before skipping the
		 * value, which can be anything. We're a bit paranoid
		 * and lazy at the same time: We simply double the
		 * number of tokens to skip and fall through into the
		 * array processing when encountering an object.
		 */
		switch (ctx->tok[tid].type) {
		case JSMN_OBJECT:
			len *= 2;
			/* FALLTHROUGH */
		case JSMN_ARRAY:
			for (++tid; len; --len)
				tid = json_token_skip(ctx, tid);
			break;
			
		default:
			++tid;
			break;
		}
		/* The next condition should never be true, but paranoia
		 * prevails...
		 */
		if (tid < 0 || tid > ctx->ntok)
			tid = ctx->ntok;
	}
	return tid;
}

/* ------------------------------------------------------------------ */

static int
json_object_lookup(
	const json_ctx * ctx ,
	tok_ref          tid ,
	const char     * key ,
	int              what)
{
	int len;

	if (tid < 0 || tid >= ctx->ntok ||
	    ctx->tok[tid].type != JSMN_OBJECT)
		return INVALID_TOKEN;
	
	len = ctx->tok[tid].size;
	for (++tid; len && tid+1 < ctx->ntok; --len) {
		if (ctx->tok[tid].type != JSMN_STRING) { /* Blooper! */
			tid = json_token_skip(ctx, tid); /* skip key */
			tid = json_token_skip(ctx, tid); /* skip val */
		} else if (strcmp(key, ctx->buf + ctx->tok[tid].start)) {
			tid = json_token_skip(ctx, tid+1); /* skip key+val */
		} else if (what < 0 || (u_int)what == ctx->tok[tid+1].type) {
			return tid + 1;
		} else {
			break;
		}
		/* if skipping ahead returned an error, bail out here. */
		if (tid < 0)
			break;
	}
	return INVALID_TOKEN;
}

/* ------------------------------------------------------------------ */

static const char*
json_object_lookup_primitive(
	const json_ctx * ctx,
	tok_ref          tid,
	const char     * key)
{
	tid = json_object_lookup(ctx, tid, key, JSMN_PRIMITIVE);
	if (INVALID_TOKEN  != tid)
		return ctx->buf + ctx->tok[tid].start;
	else
		return NULL;
}
/* ------------------------------------------------------------------ */
/* look up a boolean value. This essentially returns a tribool:
 * 0->false, 1->true, (-1)->error/undefined
 */
static int
json_object_lookup_bool(
	const json_ctx * ctx,
	tok_ref          tid,
	const char     * key)
{
	const char *cp;
	cp  = json_object_lookup_primitive(ctx, tid, key);
	switch ( cp ? *cp : '\0') {
	case 't': return  1;
	case 'f': return  0;
	default : return -1;
	}
}

/* ------------------------------------------------------------------ */

static const char*
json_object_lookup_string(
	const json_ctx * ctx,
	tok_ref          tid,
	const char     * key)
{
	tid = json_object_lookup(ctx, tid, key, JSMN_STRING);
	if (INVALID_TOKEN != tid)
		return ctx->buf + ctx->tok[tid].start;
	return NULL;
}

static const char*
json_object_lookup_string_default(
	const json_ctx * ctx,
	tok_ref          tid,
	const char     * key,
	const char     * def)
{
	tid = json_object_lookup(ctx, tid, key, JSMN_STRING);
	if (INVALID_TOKEN != tid)
		return ctx->buf + ctx->tok[tid].start;
	return def;
}

/* ------------------------------------------------------------------ */

static json_int
json_object_lookup_int(
	const json_ctx * ctx,
	tok_ref          tid,
	const char     * key)
{
	json_int     ret;
	const char * cp;
	char       * ep;

	cp = json_object_lookup_primitive(ctx, tid, key);
	if (NULL != cp) {
		ret = strtojint(cp, &ep);
		if (cp != ep && '\0' == *ep)
			return ret;
	} else {
		errno = EINVAL;
	}
	return 0;
}

static json_int
json_object_lookup_int_default(
	const json_ctx * ctx,
	tok_ref          tid,
	const char     * key,
	json_int         def)
{
	json_int     ret;
	const char * cp;
	char       * ep;

	cp = json_object_lookup_primitive(ctx, tid, key);
	if (NULL != cp) {
		ret = strtojint(cp, &ep);
		if (cp != ep && '\0' == *ep)
			return ret;
	}
	return def;
}

/* ------------------------------------------------------------------ */
#if 0 /* currently unused */
static double
json_object_lookup_float(
	const json_ctx * ctx,
	tok_ref          tid,
	const char     * key)
{
	double       ret;
	const char * cp;
	char       * ep;

	cp = json_object_lookup_primitive(ctx, tid, key);
	if (NULL != cp) {
		ret = strtod(cp, &ep);
		if (cp != ep && '\0' == *ep)
			return ret;
	} else {
		errno = EINVAL;
	}
	return 0.0;
}
#endif

static double
json_object_lookup_float_default(
	const json_ctx * ctx,
	tok_ref          tid,
	const char     * key,
	double           def)
{
	double       ret;
	const char * cp;
	char       * ep;

	cp = json_object_lookup_primitive(ctx, tid, key);
	if (NULL != cp) {
		ret = strtod(cp, &ep);
		if (cp != ep && '\0' == *ep)
			return ret;
	}
	return def;
}

/* ------------------------------------------------------------------ */

static BOOL
json_parse_record(
	json_ctx * ctx,
	char     * buf,
	size_t     len)
{
	jsmn_parser jsm;
	int         idx, rc;

	jsmn_init(&jsm);
	rc = jsmn_parse(&jsm, buf, len, ctx->tok, JSMN_MAXTOK);
	if (rc <= 0)
		return FALSE;
	ctx->buf  = buf;
	ctx->ntok = rc;

	if (JSMN_OBJECT != ctx->tok[0].type)
		return FALSE; /* not object!?! */

	/* Make all tokens NUL terminated by overwriting the
	 * terminator symbol. Makes string compares and number parsing a
	 * lot easier!
	 */
	for (idx = 0; idx < ctx->ntok; ++idx)
		if (ctx->tok[idx].end > ctx->tok[idx].start)
			ctx->buf[ctx->tok[idx].end] = '\0';
	return TRUE;
}


/* =====================================================================
 * static local helpers
 */
static BOOL
get_binary_time(
	l_fp       * const dest     ,
	json_ctx   * const jctx     ,
	const char * const time_name,
	const char * const frac_name,
	long               fscale   )
{
	BOOL            retv = FALSE;
	struct timespec ts;

	errno = 0;
	ts.tv_sec  = (time_t)json_object_lookup_int(jctx, 0, time_name);
	ts.tv_nsec = (long  )json_object_lookup_int(jctx, 0, frac_name);
	if (0 == errno) {
		ts.tv_nsec *= fscale;
		*dest = tspec_stamp_to_lfp(ts);
		retv  = TRUE;
	}
	return retv;
}

/* ------------------------------------------------------------------ */
/* Process a WATCH record
 *
 * Currently this is only used to recognise that the device is present
 * and that we're listed subscribers.
 */
static void
process_watch(
	peerT      * const peer ,
	json_ctx   * const jctx ,
	const l_fp * const rtime)
{
	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;

	const char * path;

	path = json_object_lookup_string(jctx, 0, "device");
	if (NULL == path || strcmp(path, up->device))
		return;

	if (json_object_lookup_bool(jctx, 0, "enable") > 0 &&
	    json_object_lookup_bool(jctx, 0, "json"  ) > 0  )
		up->fl_watch = -1;
	else
		up->fl_watch = 0;
	DPRINTF(2, ("%s: process_watch, enabled=%d\n",
		    up->logname, (up->fl_watch & 1)));
}

/* ------------------------------------------------------------------ */

static void
process_version(
	peerT      * const peer ,
	json_ctx   * const jctx ,
	const l_fp * const rtime)
{
	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;

	int    len;
	char * buf;
	const char *revision;
	const char *release;
	uint16_t    pvhi, pvlo;

	/* get protocol version number */
	revision = json_object_lookup_string_default(
		jctx, 0, "rev", "(unknown)");
	release  = json_object_lookup_string_default(
		jctx, 0, "release", "(unknown)");
	errno = 0;
	pvhi = (uint16_t)json_object_lookup_int(jctx, 0, "proto_major");
	pvlo = (uint16_t)json_object_lookup_int(jctx, 0, "proto_minor");

	if (0 == errno) {
		if ( ! up->fl_vers)
			msyslog(LOG_INFO,
				"%s: GPSD revision=%s release=%s protocol=%u.%u",
				up->logname, revision, release,
				pvhi, pvlo);
		up->proto_version = PROTO_VERSION(pvhi, pvlo);
		up->fl_vers = -1;
	} else {
		if (syslogok(pp, up))
			msyslog(LOG_INFO,
				"%s: could not evaluate version data",
				up->logname);
		return;
	}
	/* With the 3.9 GPSD protocol, '*_musec' vanished from the PPS
	 * record and was replace by '*_nsec'.
	 */
	up->pf_nsec = -(up->proto_version >= PROTO_VERSION(3,9));

	/* With the 3.10 protocol we can get TOFF records for better
	 * timing information.
	 */
	up->pf_toff = -(up->proto_version >= PROTO_VERSION(3,10));

	/* request watch for our GPS device if not yet watched.
	 *
	 * The version string is also sent as a life signal, if we have
	 * seen useable data. So if we're already watching the device,
	 * skip the request.
	 *
	 * Reuse the input buffer, which is no longer needed in the
	 * current cycle. Also assume that we can write the watch
	 * request in one sweep into the socket; since we do not do
	 * output otherwise, this should always work.  (Unless the
	 * TCP/IP window size gets lower than the length of the
	 * request. We handle that when it happens.)
	 */
	if (up->fl_watch)
		return;

	/* The logon string is actually the ?WATCH command of GPSD,
	 * using JSON data and selecting the GPS device name we created
	 * from our unit number. We have an old a newer version that
	 * request PPS (and TOFF) transmission.
	 */
	snprintf(up->buffer, sizeof(up->buffer),
		 "?WATCH={\"device\":\"%s\",\"enable\":true,\"json\":true%s};\r\n",
		 up->device, (up->pf_toff ? ",\"pps\":true" : ""));
	buf = up->buffer;
	len = strlen(buf);
	log_data(peer, "send", buf, len);
	if (len != write(pp->io.fd, buf, len) && (syslogok(pp, up))) {
		/* Note: if the server fails to read our request, the
		 * resulting data timeout will take care of the
		 * connection!
		 */
		msyslog(LOG_ERR, "%s: failed to write watch request (%m)",
			up->logname);
	}
}

/* ------------------------------------------------------------------ */

static void
process_tpv(
	peerT      * const peer ,
	json_ctx   * const jctx ,
	const l_fp * const rtime)
{
	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;

	const char * gps_time;
	int          gps_mode;
	double       ept;
	int          xlog2;

	gps_mode = (int)json_object_lookup_int_default(
		jctx, 0, "mode", 0);

	gps_time = json_object_lookup_string(
		jctx, 0, "time");

	/* accept time stamps only in 2d or 3d fix */
	if (gps_mode < 2 || NULL == gps_time) {
		/* receiver has no fix; tell about and avoid stale data */
		if ( ! up->pf_toff)
			++up->tc_sti_recv;
		++up->tc_nosync;
		up->fl_sti    = 0;
		up->fl_pps    = 0;
		up->fl_nosync = -1;
		return;
	}
	up->fl_nosync = 0;

	/* convert clock and set resulting ref time, but only if the
	 * TOFF sentence is *not* available
	 */
	if ( ! up->pf_toff) {
		++up->tc_sti_recv;
		/* save last time code to clock data */
		save_ltc(pp, gps_time);
		/* now parse the time string */
		if (convert_ascii_time(&up->sti_stamp, gps_time)) {
			DPRINTF(2, ("%s: process_tpv, stamp='%s',"
				    " recvt='%s' mode=%u\n",
				    up->logname,
				    gmprettydate(&up->sti_stamp),
				    gmprettydate(&up->sti_recvt),
				    gps_mode));

			/* have to use local receive time as substitute
			 * for the real receive time: TPV does not tell
			 * us.
			 */
			up->sti_local = *rtime;
			up->sti_recvt = *rtime;
			L_SUB(&up->sti_recvt, &up->sti_fudge);
			up->fl_sti = -1;
		} else {
			++up->tc_breply;
			up->fl_sti = 0;
		}
	}

	/* Set the precision from the GPSD data
	 * Use the ETP field for an estimation of the precision of the
	 * serial data. If ETP is not available, use the default serial
	 * data presion instead. (Note: The PPS branch has a different
	 * precision estimation, since it gets the proper value directly
	 * from GPSD!)
	 */
	ept = json_object_lookup_float_default(jctx, 0, "ept", 2.0e-3);
	ept = frexp(fabs(ept)*0.70710678, &xlog2); /* ~ sqrt(0.5) */
	if (ept < 0.25)
		xlog2 = INT_MIN;
	if (ept > 2.0)
		xlog2 = INT_MAX;
	up->sti_prec = clamped_precision(xlog2);
}

/* ------------------------------------------------------------------ */

static void
process_pps(
	peerT      * const peer ,
	json_ctx   * const jctx ,
	const l_fp * const rtime)
{
	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;

	int xlog2;

	++up->tc_pps_recv;

	/* Bail out if there's indication that time sync is bad or
	 * if we're explicitely requested to ignore PPS data.
	 */
	if (up->fl_nosync)
		return;

	up->pps_local = *rtime;
	/* Now grab the time values. 'clock_*' is the event time of the
	 * pulse measured on the local system clock; 'real_*' is the GPS
	 * reference time GPSD associated with the pulse.
	 */
	if (up->pf_nsec) {
		if ( ! get_binary_time(&up->pps_recvt2, jctx,
				       "clock_sec", "clock_nsec", 1))
			goto fail;
		if ( ! get_binary_time(&up->pps_stamp2, jctx,
				       "real_sec", "real_nsec", 1))
			goto fail;
	} else {
		if ( ! get_binary_time(&up->pps_recvt2, jctx,
				       "clock_sec", "clock_musec", 1000))
			goto fail;
		if ( ! get_binary_time(&up->pps_stamp2, jctx,
				       "real_sec", "real_musec", 1000))
			goto fail;
	}

	/* Try to read the precision field from the PPS record. If it's
	 * not there, take the precision from the serial data.
	 */
	xlog2 = json_object_lookup_int_default(
			jctx, 0, "precision", up->sti_prec);
	up->pps_prec = clamped_precision(xlog2);
	
	/* Get fudged receive times for primary & secondary unit */
	up->pps_recvt = up->pps_recvt2;
	L_SUB(&up->pps_recvt , &up->pps_fudge );
	L_SUB(&up->pps_recvt2, &up->pps_fudge2);
	pp->lastrec = up->pps_recvt;

	/* Map to nearest full second as reference time stamp for the
	 * primary channel. Sanity checks are done in evaluation step.
	 */
	up->pps_stamp = up->pps_recvt;
	L_ADDUF(&up->pps_stamp, 0x80000000u);
	up->pps_stamp.l_uf = 0;

	if (NULL != up->pps_peer)
		save_ltc(up->pps_peer->procptr,
			 gmprettydate(&up->pps_stamp2));
	DPRINTF(2, ("%s: PPS record processed,"
		    " stamp='%s', recvt='%s'\n",
		    up->logname,
		    gmprettydate(&up->pps_stamp2),
		    gmprettydate(&up->pps_recvt2)));
	
	up->fl_pps  = (0 != (pp->sloppyclockflag & CLK_FLAG2)) - 1;
	up->fl_pps2 = -1;
	return;

  fail:
	DPRINTF(1, ("%s: PPS record processing FAILED\n",
		    up->logname));
	++up->tc_breply;
}

/* ------------------------------------------------------------------ */

static void
process_toff(
	peerT      * const peer ,
	json_ctx   * const jctx ,
	const l_fp * const rtime)
{
	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;

	++up->tc_sti_recv;

	/* remember this! */
	up->pf_toff = -1;

	/* bail out if there's indication that time sync is bad */
	if (up->fl_nosync)
		return;

	if ( ! get_binary_time(&up->sti_recvt, jctx,
			       "clock_sec", "clock_nsec", 1))
			goto fail;
	if ( ! get_binary_time(&up->sti_stamp, jctx,
			       "real_sec", "real_nsec", 1))
			goto fail;
	L_SUB(&up->sti_recvt, &up->sti_fudge);
	up->sti_local = *rtime;
	up->fl_sti    = -1;

	save_ltc(pp, gmprettydate(&up->sti_stamp));
	DPRINTF(2, ("%s: TOFF record processed,"
		    " stamp='%s', recvt='%s'\n",
		    up->logname,
		    gmprettydate(&up->sti_stamp),
		    gmprettydate(&up->sti_recvt)));
	return;

  fail:
	DPRINTF(1, ("%s: TOFF record processing FAILED\n",
		    up->logname));
	++up->tc_breply;
}

/* ------------------------------------------------------------------ */

static void
gpsd_parse(
	peerT      * const peer ,
	const l_fp * const rtime)
{
	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;

	const char * clsid;

        DPRINTF(2, ("%s: gpsd_parse: time %s '%.*s'\n",
                    up->logname, ulfptoa(rtime, 6),
		    up->buflen, up->buffer));

	/* See if we can grab anything potentially useful. JSMN does not
	 * need a trailing NUL, but it needs the number of bytes to
	 * process. */
	if (!json_parse_record(&up->json_parse, up->buffer, up->buflen)) {
		++up->tc_breply;
		return;
	}
	
	/* Now dispatch over the objects we know */
	clsid = json_object_lookup_string(&up->json_parse, 0, "class");
	if (NULL == clsid) {
		++up->tc_breply;
		return;
	}

	if      (!strcmp("TPV", clsid))
		process_tpv(peer, &up->json_parse, rtime);
	else if (!strcmp("PPS", clsid))
		process_pps(peer, &up->json_parse, rtime);
	else if (!strcmp("TOFF", clsid))
		process_toff(peer, &up->json_parse, rtime);
	else if (!strcmp("VERSION", clsid))
		process_version(peer, &up->json_parse, rtime);
	else if (!strcmp("WATCH", clsid))
		process_watch(peer, &up->json_parse, rtime);
	else
		return; /* nothing we know about... */
	++up->tc_recv;

	/* if possible, feed the PPS side channel */
	if (up->pps_peer)
		eval_pps_secondary(
			up->pps_peer, up->pps_peer->procptr, up);

	/* check PPS vs. STI receive times:
	 * If STI is before PPS, then clearly the STI is too old. If PPS
	 * is before STI by more than one second, then PPS is too old.
	 * Weed out stale time stamps & flags.
	 */
	if (up->fl_pps && up->fl_sti) {
		l_fp diff;
		diff = up->sti_local;
		L_SUB(&diff, &up->pps_local);
		if (diff.l_i > 0)
			up->fl_pps = 0; /* pps too old */
		else if (diff.l_i < 0)
			up->fl_sti = 0; /* serial data too old */
	}

	/* dispatch to the mode-dependent processing functions */
	switch (up->mode) {
	default:
	case MODE_OP_STI:
		eval_serial(peer, pp, up);
		break;

	case MODE_OP_STRICT:
		eval_strict(peer, pp, up);
		break;

	case MODE_OP_AUTO:
		eval_auto(peer, pp, up);
		break;
	}
}

/* ------------------------------------------------------------------ */

static void
gpsd_stop_socket(
	peerT * const peer)
{
	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;

	if (-1 != pp->io.fd) {
		if (syslogok(pp, up))
			msyslog(LOG_INFO,
				"%s: closing socket to GPSD, fd=%d",
				up->logname, pp->io.fd);
		else
			DPRINTF(1, ("%s: closing socket to GPSD, fd=%d\n",
				    up->logname, pp->io.fd));
		io_closeclock(&pp->io);
		pp->io.fd = -1;
	}
	up->tickover = up->tickpres;
	up->tickpres = min(up->tickpres + 5, TICKOVER_HIGH);
	up->fl_vers  = 0;
	up->fl_sti   = 0;
	up->fl_pps   = 0;
	up->fl_watch = 0;
}

/* ------------------------------------------------------------------ */

static void
gpsd_init_socket(
	peerT * const peer)
{
	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;
	addrinfoT  * ai;
	int          rc;
	int          ov;

	/* draw next address to try */
	if (NULL == up->addr)
		up->addr = s_gpsd_addr;
	ai = up->addr;
	up->addr = ai->ai_next;

	/* try to create a matching socket */
	up->fdt = socket(
		ai->ai_family, ai->ai_socktype, ai->ai_protocol);
	if (-1 == up->fdt) {
		if (syslogok(pp, up))
			msyslog(LOG_ERR,
				"%s: cannot create GPSD socket: %m",
				up->logname);
		goto no_socket;
	}

	/* Make sure the socket is non-blocking. Connect/reconnect and
	 * IO happen in an event-driven environment, and synchronous
	 * operations wreak havoc on that.
	 */
	rc = fcntl(up->fdt, F_SETFL, O_NONBLOCK, 1);
	if (-1 == rc) {
		if (syslogok(pp, up))
			msyslog(LOG_ERR,
				"%s: cannot set GPSD socket to non-blocking: %m",
				up->logname);
		goto no_socket;
	}
	/* Disable nagling. The way both GPSD and NTPD handle the
	 * protocol makes it record-oriented, and in most cases
	 * complete records (JSON serialised objects) will be sent in
	 * one sweep. Nagling gives not much advantage but adds another
	 * delay, which can worsen the situation for some packets.
	 */
	ov = 1;
	rc = setsockopt(up->fdt, IPPROTO_TCP, TCP_NODELAY,
			(void *)&ov, sizeof(ov));
	if (-1 == rc) {
		if (syslogok(pp, up))
			msyslog(LOG_INFO,
				"%s: cannot disable TCP nagle: %m",
				up->logname);
	}

	/* Start a non-blocking connect. There might be a synchronous
	 * connection result we have to handle.
	 */
	rc = connect(up->fdt, ai->ai_addr, ai->ai_addrlen);
	if (-1 == rc) {
		if (errno == EINPROGRESS) {
			DPRINTF(1, ("%s: async connect pending, fd=%d\n",
				    up->logname, up->fdt));
			return;
		}

		if (syslogok(pp, up))
			msyslog(LOG_ERR,
				"%s: cannot connect GPSD socket: %m",
				up->logname);
		goto no_socket;
	}

	/* We had a successful synchronous connect, so we add the
	 * refclock processing ASAP. We still have to wait for the
	 * version string and apply the watch command later on, but we
	 * might as well get the show on the road now.
	 */
	DPRINTF(1, ("%s: new socket connection, fd=%d\n",
		    up->logname, up->fdt));

	pp->io.fd = up->fdt;
	up->fdt   = -1;
	if (0 == io_addclock(&pp->io)) {
		if (syslogok(pp, up))
			msyslog(LOG_ERR,
				"%s: failed to register with I/O engine",
				up->logname);
		goto no_socket;
	}

	return;

  no_socket:
	if (-1 != pp->io.fd)
		close(pp->io.fd);
	if (-1 != up->fdt)
		close(up->fdt);
	pp->io.fd    = -1;
	up->fdt      = -1;
	up->tickover = up->tickpres;
	up->tickpres = min(up->tickpres + 5, TICKOVER_HIGH);
}

/* ------------------------------------------------------------------ */

static void
gpsd_test_socket(
	peerT * const peer)
{
	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;

	int       ec, rc;
	socklen_t lc;

	/* Check if the non-blocking connect was finished by testing the
	 * socket for writeability. Use the 'poll()' API if available
	 * and 'select()' otherwise.
	 */
	DPRINTF(2, ("%s: check connect, fd=%d\n",
		    up->logname, up->fdt));

#if defined(HAVE_SYS_POLL_H)
	{
		struct pollfd pfd;

		pfd.events = POLLOUT;
		pfd.fd     = up->fdt;
		rc = poll(&pfd, 1, 0);
		if (1 != rc || !(pfd.revents & POLLOUT))
			return;
	}
#elif defined(HAVE_SYS_SELECT_H)
	{
		struct timeval tout;
		fd_set         wset;

		memset(&tout, 0, sizeof(tout));
		FD_ZERO(&wset);
		FD_SET(up->fdt, &wset);
		rc = select(up->fdt+1, NULL, &wset, NULL, &tout);
		if (0 == rc || !(FD_ISSET(up->fdt, &wset)))
			return;
	}
#else
# error Blooper! That should have been found earlier!
#endif

	/* next timeout is a full one... */
	up->tickover = TICKOVER_LOW;

	/* check for socket error */
	ec = 0;
	lc = sizeof(ec);
	rc = getsockopt(up->fdt, SOL_SOCKET, SO_ERROR, (void *)&ec, &lc);
	if (-1 == rc || 0 != ec) {
		const char *errtxt;
		if (0 == ec)
			ec = errno;
		errtxt = strerror(ec);
		if (syslogok(pp, up))
			msyslog(LOG_ERR,
				"%s: async connect to GPSD failed,"
				" fd=%d, ec=%d(%s)",
				up->logname, up->fdt, ec, errtxt);
		else
			DPRINTF(1, ("%s: async connect to GPSD failed,"
				" fd=%d, ec=%d(%s)\n",
				    up->logname, up->fdt, ec, errtxt));
		goto no_socket;
	} else {
		DPRINTF(1, ("%s: async connect to GPSD succeeded, fd=%d\n",
			    up->logname, up->fdt));
	}

	/* swap socket FDs, and make sure the clock was added */
	pp->io.fd = up->fdt;
	up->fdt   = -1;
	if (0 == io_addclock(&pp->io)) {
		if (syslogok(pp, up))
			msyslog(LOG_ERR,
				"%s: failed to register with I/O engine",
				up->logname);
		goto no_socket;
	}
	return;

  no_socket:
	if (-1 != up->fdt) {
		DPRINTF(1, ("%s: closing socket, fd=%d\n",
			    up->logname, up->fdt));
		close(up->fdt);
	}
	up->fdt      = -1;
	up->tickover = up->tickpres;
	up->tickpres = min(up->tickpres + 5, TICKOVER_HIGH);
}

/* =====================================================================
 * helper stuff
 */

/* -------------------------------------------------------------------
 * store a properly clamped precision value
 */
static int16_t
clamped_precision(
	int rawprec)
{
	if (rawprec > 0)
		rawprec = 0;
	if (rawprec < -32)
		rawprec = -32;
	return (int16_t)rawprec;
}

/* -------------------------------------------------------------------
 * Convert a GPSD timestamp (ISO8601 Format) to an l_fp
 */
static BOOL
convert_ascii_time(
	l_fp       * fp      ,
	const char * gps_time)
{
	char           *ep;
	struct tm       gd;
	struct timespec ts;
	uint32_t        dw;

	/* Use 'strptime' to take the brunt of the work, then parse
	 * the fractional part manually, starting with a digit weight of
	 * 10^8 nanoseconds.
	 */
	ts.tv_nsec = 0;
	ep = strptime(gps_time, "%Y-%m-%dT%H:%M:%S", &gd);
	if (NULL == ep)
		return FALSE; /* could not parse the mandatory stuff! */
	if (*ep == '.') {
		dw = 100000000u;
		while (isdigit(*(u_char*)++ep)) {
			ts.tv_nsec += (*(u_char*)ep - '0') * dw;
			dw /= 10u;
		}
	}
	if (ep[0] != 'Z' || ep[1] != '\0')
		return FALSE; /* trailing garbage */

	/* Now convert the whole thing into a 'l_fp'. We do not use
	 * 'mkgmtime()' since its not standard and going through the
	 * calendar routines is not much effort, either.
	 */
	ts.tv_sec = (ntpcal_tm_to_rd(&gd) - DAY_NTP_STARTS) * SECSPERDAY
	          + ntpcal_tm_to_daysec(&gd);
	*fp = tspec_intv_to_lfp(ts);

	return TRUE;
}

/* -------------------------------------------------------------------
 * Save the last timecode string, making sure it's properly truncated
 * if necessary and NUL terminated in any case.
 */
static void
save_ltc(
	clockprocT * const pp,
	const char * const tc)
{
	size_t len = 0;
	
	if (tc) {
		len = strlen(tc);
		if (len >= sizeof(pp->a_lastcode))
			len = sizeof(pp->a_lastcode) - 1;
		memcpy(pp->a_lastcode, tc, len);
	}
	pp->lencode = (u_short)len;
	pp->a_lastcode[len] = '\0';
}

/* -------------------------------------------------------------------
 * asprintf replacement... it's not available everywhere...
 */
static int
myasprintf(
	char      ** spp,
	char const * fmt,
	...             )
{
	size_t alen, plen;

	alen = 32;
	*spp = NULL;
	do {
		va_list va;

		alen += alen;
		free(*spp);
		*spp = (char*)malloc(alen);
		if (NULL == *spp)
			return -1;

		va_start(va, fmt);
		plen = (size_t)vsnprintf(*spp, alen, fmt, va);
		va_end(va);
	} while (plen >= alen);

	return (int)plen;
}

/* -------------------------------------------------------------------
 * dump a raw data buffer
 */

static char *
add_string(
	char *dp,
	char *ep,
	const char *sp)
{
	while (dp != ep && *sp)
		*dp++ = *sp++;
	return dp;
}

static void
log_data(
	peerT      *peer,
	const char *what,
	const char *buf ,
	size_t      len )
{
	/* we're running single threaded with regards to the clocks. */
	static char s_lbuf[2048];

	clockprocT * const pp = peer->procptr;
	gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;

	if (debug > 1) {
		const char *sptr = buf;
		const char *stop = buf + len;
		char       *dptr = s_lbuf;
		char       *dtop = s_lbuf + sizeof(s_lbuf) - 1; /* for NUL */

		while (sptr != stop && dptr != dtop) {
			u_char uch = (u_char)*sptr++;
			if (uch == '\\') {
				dptr = add_string(dptr, dtop, "\\\\");
			} else if (isprint(uch)) {
				*dptr++ = (char)uch;
			} else {
				char fbuf[6];
				snprintf(fbuf, sizeof(fbuf), "\\%03o", uch);
				dptr = add_string(dptr, dtop, fbuf);
			}
		}
		*dptr = '\0';
		mprintf("%s[%s]: '%s'\n", up->logname, what, s_lbuf);
	}
}

#else
NONEMPTY_TRANSLATION_UNIT
#endif /* REFCLOCK && CLOCK_GPSDJSON */