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