xref: /freebsd/contrib/ntp/ntpd/refclock_palisade.h (revision e43d33d286a1aa41b6fc6a209f28a18e8cd7437a)
1c0b746e5SOllivier Robert /*
2c0b746e5SOllivier Robert  * This software was developed by the Software and Component Technologies
3c0b746e5SOllivier Robert  * group of Trimble Navigation, Ltd.
4c0b746e5SOllivier Robert  *
5224ba2bdSOllivier Robert  * Copyright (c) 1997, 1998, 1999, 2000   Trimble Navigation Ltd.
6c0b746e5SOllivier Robert  * All rights reserved.
7c0b746e5SOllivier Robert  *
8c0b746e5SOllivier Robert  * Redistribution and use in source and binary forms, with or without
9c0b746e5SOllivier Robert  * modification, are permitted provided that the following conditions
10c0b746e5SOllivier Robert  * are met:
11c0b746e5SOllivier Robert  * 1. Redistributions of source code must retain the above copyright
12c0b746e5SOllivier Robert  *    notice, this list of conditions and the following disclaimer.
13c0b746e5SOllivier Robert  * 2. Redistributions in binary form must reproduce the above copyright
14c0b746e5SOllivier Robert  *    notice, this list of conditions and the following disclaimer in the
15c0b746e5SOllivier Robert  *    documentation and/or other materials provided with the distribution.
16c0b746e5SOllivier Robert  * 3. All advertising materials mentioning features or use of this software
17c0b746e5SOllivier Robert  *    must display the following acknowledgement:
18c0b746e5SOllivier Robert  *    This product includes software developed by Trimble Navigation, Ltd.
19c0b746e5SOllivier Robert  * 4. The name of Trimble Navigation Ltd. may not be used to endorse or
20c0b746e5SOllivier Robert  *    promote products derived from this software without specific prior
21c0b746e5SOllivier Robert  *    written permission.
22c0b746e5SOllivier Robert  *
23c0b746e5SOllivier Robert  * THIS SOFTWARE IS PROVIDED BY TRIMBLE NAVIGATION LTD. ``AS IS'' AND
24c0b746e5SOllivier Robert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25c0b746e5SOllivier Robert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26c0b746e5SOllivier Robert  * ARE DISCLAIMED.  IN NO EVENT SHALL TRIMBLE NAVIGATION LTD. BE LIABLE
27c0b746e5SOllivier Robert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28c0b746e5SOllivier Robert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29c0b746e5SOllivier Robert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30c0b746e5SOllivier Robert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31c0b746e5SOllivier Robert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32c0b746e5SOllivier Robert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33c0b746e5SOllivier Robert  * SUCH DAMAGE.
34c0b746e5SOllivier Robert  */
35c0b746e5SOllivier Robert 
36c0b746e5SOllivier Robert /*
37c0b746e5SOllivier Robert  * refclock_palisade - clock driver for the Trimble Palisade GPS
38c0b746e5SOllivier Robert  * timing receiver
39c0b746e5SOllivier Robert  *
40c0b746e5SOllivier Robert  * For detailed information on this program, please refer to the html
41c0b746e5SOllivier Robert  * Refclock 29 page accompanying the NTP distribution.
42c0b746e5SOllivier Robert  *
43c0b746e5SOllivier Robert  * for questions / bugs / comments, contact:
44c0b746e5SOllivier Robert  * sven_dietrich@trimble.com
45c0b746e5SOllivier Robert  *
46c0b746e5SOllivier Robert  * Sven-Thorsten Dietrich
47c0b746e5SOllivier Robert  * 645 North Mary Avenue
48c0b746e5SOllivier Robert  * Post Office Box 3642
49c0b746e5SOllivier Robert  * Sunnyvale, CA 94088-3642
50c0b746e5SOllivier Robert  *
51c0b746e5SOllivier Robert  */
52c0b746e5SOllivier Robert 
532b15cb3dSCy Schubert #ifndef REFCLOCK_PALISADE_H
542b15cb3dSCy Schubert #define REFCLOCK_PALISADE_H
55c0b746e5SOllivier Robert 
56c0b746e5SOllivier Robert #if defined HAVE_SYS_MODEM_H
57c0b746e5SOllivier Robert #include <sys/modem.h>
58ea906c41SOllivier Robert #ifndef __QNXNTO__
59c0b746e5SOllivier Robert #define TIOCMSET MCSETA
60c0b746e5SOllivier Robert #define TIOCMGET MCGETA
61c0b746e5SOllivier Robert #define TIOCM_RTS MRTS
62c0b746e5SOllivier Robert #endif
63ea906c41SOllivier Robert #endif
64c0b746e5SOllivier Robert 
65c0b746e5SOllivier Robert #ifdef HAVE_TERMIOS_H
66c0b746e5SOllivier Robert # ifdef TERMIOS_NEEDS__SVID3
67c0b746e5SOllivier Robert #  define _SVID3
68c0b746e5SOllivier Robert # endif
69c0b746e5SOllivier Robert # include <termios.h>
702b15cb3dSCy Schubert # include <sys/stat.h>
71c0b746e5SOllivier Robert # ifdef TERMIOS_NEEDS__SVID3
72c0b746e5SOllivier Robert #  undef _SVID3
73c0b746e5SOllivier Robert # endif
74c0b746e5SOllivier Robert #endif
75c0b746e5SOllivier Robert 
76c0b746e5SOllivier Robert #ifdef HAVE_SYS_IOCTL_H
77c0b746e5SOllivier Robert #include <sys/ioctl.h>
78c0b746e5SOllivier Robert #endif
79c0b746e5SOllivier Robert 
80c0b746e5SOllivier Robert #include "ntpd.h"
81c0b746e5SOllivier Robert #include "ntp_io.h"
82c0b746e5SOllivier Robert #include "ntp_control.h"
83c0b746e5SOllivier Robert #include "ntp_refclock.h"
84c0b746e5SOllivier Robert #include "ntp_unixtime.h"
85*2d4e511cSCy Schubert #include "ntp_calgps.h"
86c0b746e5SOllivier Robert #include "ntp_stdlib.h"
87c0b746e5SOllivier Robert 
88c0b746e5SOllivier Robert /*
89c0b746e5SOllivier Robert  * GPS Definitions
90c0b746e5SOllivier Robert  */
91c0b746e5SOllivier Robert #define	DESCRIPTION	"Trimble Palisade GPS" /* Long name */
92c0b746e5SOllivier Robert #define	PRECISION	(-20)	/* precision assumed (about 1 us) */
93c0b746e5SOllivier Robert #define	REFID		"GPS\0"	/* reference ID */
94224ba2bdSOllivier Robert #define TRMB_MINPOLL    4	/* 16 seconds */
95224ba2bdSOllivier Robert #define TRMB_MAXPOLL	5	/* 32 seconds */
96c0b746e5SOllivier Robert 
97c0b746e5SOllivier Robert /*
98c0b746e5SOllivier Robert  * I/O Definitions
99c0b746e5SOllivier Robert  */
100c0b746e5SOllivier Robert #define	DEVICE		"/dev/palisade%d" 	/* device name and unit */
101c0b746e5SOllivier Robert #define	SPEED232	B9600		  	/* uart speed (9600 baud) */
102*2d4e511cSCy Schubert #define	SPEED232COP	B38400		  	/* uart speed for Copernicus II (38400 baud) */
103c0b746e5SOllivier Robert 
104c0b746e5SOllivier Robert /*
105c0b746e5SOllivier Robert  * TSIP Report Definitions
106c0b746e5SOllivier Robert  */
107c0b746e5SOllivier Robert #define LENCODE_8F0B	74	/* Length of TSIP 8F-0B Packet & header */
108c0b746e5SOllivier Robert #define LENCODE_NTP     22	/* Length of Palisade NTP Packet */
109c0b746e5SOllivier Robert 
1102b15cb3dSCy Schubert #define LENCODE_8FAC    68      /* Length of Thunderbolt 8F-AC Position Packet*/
1112b15cb3dSCy Schubert #define LENCODE_8FAB    17      /* Length of Thunderbolt Primary Timing Packet*/
1122b15cb3dSCy Schubert 
113*2d4e511cSCy Schubert #define LENCODE_41      10      /* Length of Copernicus II GPS Time Packet*/
114*2d4e511cSCy Schubert 
115c0b746e5SOllivier Robert /* Allowed Sub-Packet ID's */
116c0b746e5SOllivier Robert #define PACKET_8F0B	0x0B
117c0b746e5SOllivier Robert #define PACKET_NTP	0xAD
118c0b746e5SOllivier Robert 
1192b15cb3dSCy Schubert /* Thunderbolt Packets */
1202b15cb3dSCy Schubert #define PACKET_8FAC     0xAC	/* Supplementary Thunderbolt Time Packet */
1212b15cb3dSCy Schubert #define PACKET_8FAB     0xAB	/* Primary Thunderbolt Time Packet */
1222b15cb3dSCy Schubert #define PACKET_6D	0x6D	/* Supplementary Thunderbolt Tracking Stats */
1232b15cb3dSCy Schubert #define PACKET_41	0x41	/* Thunderbolt I dont know what this packet is, it's not documented on my manual*/
1242b15cb3dSCy Schubert 
1252b15cb3dSCy Schubert /* Acutime Packets */
1262b15cb3dSCy Schubert #define PACKET_41A      0x41    /* GPS time */
1272b15cb3dSCy Schubert #define PACKET_46       0x46    /* Receiver Health */
1282b15cb3dSCy Schubert 
129c0b746e5SOllivier Robert #define DLE 0x10
130c0b746e5SOllivier Robert #define ETX 0x03
131c0b746e5SOllivier Robert 
132c0b746e5SOllivier Robert /* parse states */
133c0b746e5SOllivier Robert #define TSIP_PARSED_EMPTY       0
134c0b746e5SOllivier Robert #define TSIP_PARSED_FULL        1
135c0b746e5SOllivier Robert #define TSIP_PARSED_DLE_1       2
136c0b746e5SOllivier Robert #define TSIP_PARSED_DATA        3
137c0b746e5SOllivier Robert #define TSIP_PARSED_DLE_2       4
138c0b746e5SOllivier Robert 
139c0b746e5SOllivier Robert /*
140c0b746e5SOllivier Robert  * Leap-Insert and Leap-Delete are encoded as follows:
141c0b746e5SOllivier Robert  * 	PALISADE_UTC_TIME set   and PALISADE_LEAP_PENDING set: INSERT leap
142c0b746e5SOllivier Robert  */
143c0b746e5SOllivier Robert 
144c0b746e5SOllivier Robert #define PALISADE_LEAP_INPROGRESS 0x08 /* This is the leap flag			*/
145c0b746e5SOllivier Robert #define PALISADE_LEAP_WARNING    0x04 /* GPS Leap Warning (see ICD-200) */
146c0b746e5SOllivier Robert #define PALISADE_LEAP_PENDING    0x02 /* Leap Pending (24 hours)		*/
147c0b746e5SOllivier Robert #define PALISADE_UTC_TIME        0x01 /* UTC time available				*/
148c0b746e5SOllivier Robert 
149c0b746e5SOllivier Robert #define mb(_X_) (up->rpt_buf[(_X_ + 1)]) /* shortcut for buffer access	*/
150c0b746e5SOllivier Robert 
151c0b746e5SOllivier Robert /* Conversion Definitions */
152c0b746e5SOllivier Robert #define GPS_PI 		(3.1415926535898)
153c0b746e5SOllivier Robert #define	R2D		(180.0/GPS_PI)
154c0b746e5SOllivier Robert 
155c0b746e5SOllivier Robert /*
1562b15cb3dSCy Schubert  * Structure for build data packets for send (thunderbolt uses it only)
1572b15cb3dSCy Schubert  * taken from Markus Prosch
1582b15cb3dSCy Schubert  */
1592b15cb3dSCy Schubert struct packettx
1602b15cb3dSCy Schubert {
1612b15cb3dSCy Schubert 	short	size;
1622b15cb3dSCy Schubert 	u_char *data;
1632b15cb3dSCy Schubert };
1642b15cb3dSCy Schubert 
1652b15cb3dSCy Schubert /*
166c0b746e5SOllivier Robert  * Palisade unit control structure.
167c0b746e5SOllivier Robert  */
168c0b746e5SOllivier Robert struct palisade_unit {
169c0b746e5SOllivier Robert 	short		unit;		/* NTP refclock unit number */
170c0b746e5SOllivier Robert 	int 		polled;		/* flag to detect noreplies */
171c0b746e5SOllivier Robert 	char		leap_status;	/* leap second flag */
172c0b746e5SOllivier Robert 	char		rpt_status;	/* TSIP Parser State */
173c0b746e5SOllivier Robert 	short 		rpt_cnt;	/* TSIP packet length so far */
174c0b746e5SOllivier Robert 	char 		rpt_buf[BMAX]; 	/* packet assembly buffer */
1759c2daa00SOllivier Robert 	int		type;		/* Clock mode type */
176a25439b6SCy Schubert 	int		month;		/* for LEAP filter */
177c0b746e5SOllivier Robert };
178c0b746e5SOllivier Robert 
179c0b746e5SOllivier Robert /*
180c0b746e5SOllivier Robert  * Function prototypes
181c0b746e5SOllivier Robert  */
182c0b746e5SOllivier Robert 
1832b15cb3dSCy Schubert static	int	palisade_start		(int, struct peer *);
1842b15cb3dSCy Schubert static	void	palisade_shutdown	(int, struct peer *);
1852b15cb3dSCy Schubert static	void	palisade_receive	(struct peer *);
1862b15cb3dSCy Schubert static	void	palisade_poll		(int, struct peer *);
1872b15cb3dSCy Schubert static	void 	palisade_io		(struct recvbuf *);
1882b15cb3dSCy Schubert int 		palisade_configure	(int, struct peer *);
1892b15cb3dSCy Schubert int 		TSIP_decode		(struct peer *);
1902b15cb3dSCy Schubert long		HW_poll			(struct refclockproc *);
1912b15cb3dSCy Schubert static	double	getdbl 			(u_char *);
1922b15cb3dSCy Schubert static	short	getint 			(u_char *);
1932b15cb3dSCy Schubert static	int32	getlong			(u_char *);
194*2d4e511cSCy Schubert static  int32   getsingle		(u_char *);
195c0b746e5SOllivier Robert 
1962b15cb3dSCy Schubert 
1972b15cb3dSCy Schubert static  void	sendcmd			(struct packettx *buffer, int c);
1982b15cb3dSCy Schubert static  void	sendsupercmd		(struct packettx *buffer, int c1, int c2);
1992b15cb3dSCy Schubert static  void	sendbyte		(struct packettx *buffer, int b);
2002b15cb3dSCy Schubert static  void	sendint			(struct packettx *buffer, int a);
2012b15cb3dSCy Schubert static  int	sendetx			(struct packettx *buffer, int fd);
2022b15cb3dSCy Schubert static  void	init_thunderbolt	(int fd);
2032b15cb3dSCy Schubert static  void	init_acutime		(int fd);
2042b15cb3dSCy Schubert 
2052b15cb3dSCy Schubert #endif /* REFCLOCK_PALISADE_H */
206