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_stdlib.h"
86 
87 /*
88  * GPS Definitions
89  */
90 #define	DESCRIPTION	"Trimble Palisade GPS" /* Long name */
91 #define	PRECISION	(-20)	/* precision assumed (about 1 us) */
92 #define	REFID		"GPS\0"	/* reference ID */
93 #define TRMB_MINPOLL    4	/* 16 seconds */
94 #define TRMB_MAXPOLL	5	/* 32 seconds */
95 
96 /*
97  * I/O Definitions
98  */
99 #define	DEVICE		"/dev/palisade%d" 	/* device name and unit */
100 #define	SPEED232	B9600		  	/* uart speed (9600 baud) */
101 
102 /*
103  * TSIP Report Definitions
104  */
105 #define LENCODE_8F0B	74	/* Length of TSIP 8F-0B Packet & header */
106 #define LENCODE_NTP     22	/* Length of Palisade NTP Packet */
107 
108 #define LENCODE_8FAC    68      /* Length of Thunderbolt 8F-AC Position Packet*/
109 #define LENCODE_8FAB    17      /* Length of Thunderbolt Primary Timing Packet*/
110 
111 /* Allowed Sub-Packet ID's */
112 #define PACKET_8F0B	0x0B
113 #define PACKET_NTP	0xAD
114 
115 /* Thunderbolt Packets */
116 #define PACKET_8FAC     0xAC	/* Supplementary Thunderbolt Time Packet */
117 #define PACKET_8FAB     0xAB	/* Primary Thunderbolt Time Packet */
118 #define PACKET_6D	0x6D	/* Supplementary Thunderbolt Tracking Stats */
119 #define PACKET_41	0x41	/* Thunderbolt I dont know what this packet is, it's not documented on my manual*/
120 
121 /* Acutime Packets */
122 #define PACKET_41A      0x41    /* GPS time */
123 #define PACKET_46       0x46    /* Receiver Health */
124 
125 #define DLE 0x10
126 #define ETX 0x03
127 
128 /* parse states */
129 #define TSIP_PARSED_EMPTY       0
130 #define TSIP_PARSED_FULL        1
131 #define TSIP_PARSED_DLE_1       2
132 #define TSIP_PARSED_DATA        3
133 #define TSIP_PARSED_DLE_2       4
134 
135 /*
136  * Leap-Insert and Leap-Delete are encoded as follows:
137  * 	PALISADE_UTC_TIME set   and PALISADE_LEAP_PENDING set: INSERT leap
138  */
139 
140 #define PALISADE_LEAP_INPROGRESS 0x08 /* This is the leap flag			*/
141 #define PALISADE_LEAP_WARNING    0x04 /* GPS Leap Warning (see ICD-200) */
142 #define PALISADE_LEAP_PENDING    0x02 /* Leap Pending (24 hours)		*/
143 #define PALISADE_UTC_TIME        0x01 /* UTC time available				*/
144 
145 #define mb(_X_) (up->rpt_buf[(_X_ + 1)]) /* shortcut for buffer access	*/
146 
147 /* Conversion Definitions */
148 #define GPS_PI 		(3.1415926535898)
149 #define	R2D		(180.0/GPS_PI)
150 
151 /*
152  * Structure for build data packets for send (thunderbolt uses it only)
153  * taken from Markus Prosch
154  */
155 struct packettx
156 {
157 	short	size;
158 	u_char *data;
159 };
160 
161 /*
162  * Palisade unit control structure.
163  */
164 struct palisade_unit {
165 	short		unit;		/* NTP refclock unit number */
166 	int 		polled;		/* flag to detect noreplies */
167 	char		leap_status;	/* leap second flag */
168 	char		rpt_status;	/* TSIP Parser State */
169 	short 		rpt_cnt;	/* TSIP packet length so far */
170 	char 		rpt_buf[BMAX]; 	/* packet assembly buffer */
171 	int		type;		/* Clock mode type */
172 	int		month;		/* for LEAP filter */
173 };
174 
175 /*
176  * Function prototypes
177  */
178 
179 static	int	palisade_start		(int, struct peer *);
180 static	void	palisade_shutdown	(int, struct peer *);
181 static	void	palisade_receive	(struct peer *);
182 static	void	palisade_poll		(int, struct peer *);
183 static	void 	palisade_io		(struct recvbuf *);
184 int 		palisade_configure	(int, struct peer *);
185 int 		TSIP_decode		(struct peer *);
186 long		HW_poll			(struct refclockproc *);
187 static	double	getdbl 			(u_char *);
188 static	short	getint 			(u_char *);
189 static	int32	getlong			(u_char *);
190 
191 
192 #ifdef PALISADE_SENDCMD_RESURRECTED
193 static  void	sendcmd			(struct packettx *buffer, int c);
194 #endif
195 static  void	sendsupercmd		(struct packettx *buffer, int c1, int c2);
196 static  void	sendbyte		(struct packettx *buffer, int b);
197 static  void	sendint			(struct packettx *buffer, int a);
198 static  int	sendetx			(struct packettx *buffer, int fd);
199 static  void	init_thunderbolt	(int fd);
200 static  void	init_acutime		(int fd);
201 
202 #endif /* REFCLOCK_PALISADE_H */
203