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