190075Sobrien/*
2132718Skan * This software was developed by the Software and Component Technologies
390075Sobrien * group of Trimble Navigation, Ltd.
490075Sobrien *
590075Sobrien * Copyright (c) 1997, 1998, 1999, 2000   Trimble Navigation Ltd.
690075Sobrien * All rights reserved.
790075Sobrien *
890075Sobrien * Redistribution and use in source and binary forms, with or without
990075Sobrien * modification, are permitted provided that the following conditions
1090075Sobrien * are met:
1190075Sobrien * 1. Redistributions of source code must retain the above copyright
1290075Sobrien *    notice, this list of conditions and the following disclaimer.
1390075Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1490075Sobrien *    notice, this list of conditions and the following disclaimer in the
1590075Sobrien *    documentation and/or other materials provided with the distribution.
1690075Sobrien * 3. All advertising materials mentioning features or use of this software
1790075Sobrien *    must display the following acknowledgement:
1890075Sobrien *    This product includes software developed by Trimble Navigation, Ltd.
1990075Sobrien * 4. The name of Trimble Navigation Ltd. may not be used to endorse or
2090075Sobrien *    promote products derived from this software without specific prior
2190075Sobrien *    written permission.
2290075Sobrien *
2390075Sobrien * THIS SOFTWARE IS PROVIDED BY TRIMBLE NAVIGATION LTD. ``AS IS'' AND
2490075Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25132718Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26132718Skan * ARE DISCLAIMED.  IN NO EVENT SHALL TRIMBLE NAVIGATION LTD. BE LIABLE
2790075Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2890075Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2990075Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3090075Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3190075Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3290075Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3390075Sobrien * SUCH DAMAGE.
3490075Sobrien */
3590075Sobrien
3690075Sobrien/*
3790075Sobrien * refclock_palisade - clock driver for the Trimble Palisade GPS
3890075Sobrien * timing receiver
3990075Sobrien *
4090075Sobrien * For detailed information on this program, please refer to the html
4190075Sobrien * Refclock 29 page accompanying the NTP distribution.
4290075Sobrien *
4390075Sobrien * for questions / bugs / comments, contact:
4490075Sobrien * sven_dietrich@trimble.com
4590075Sobrien *
4690075Sobrien * Sven-Thorsten Dietrich
4790075Sobrien * 645 North Mary Avenue
4890075Sobrien * Post Office Box 3642
4990075Sobrien * Sunnyvale, CA 94088-3642
5090075Sobrien *
5190075Sobrien */
5290075Sobrien
5390075Sobrien#ifndef REFCLOCK_PALISADE_H
54132718Skan#define REFCLOCK_PALISADE_H
5590075Sobrien
5690075Sobrien#if defined HAVE_SYS_MODEM_H
5790075Sobrien#include <sys/modem.h>
5890075Sobrien#ifndef __QNXNTO__
5990075Sobrien#define TIOCMSET MCSETA
6090075Sobrien#define TIOCMGET MCGETA
6190075Sobrien#define TIOCM_RTS MRTS
6290075Sobrien#endif
6390075Sobrien#endif
6490075Sobrien
6590075Sobrien#ifdef HAVE_TERMIOS_H
6690075Sobrien# ifdef TERMIOS_NEEDS__SVID3
6790075Sobrien#  define _SVID3
6890075Sobrien# endif
6990075Sobrien# include <termios.h>
7090075Sobrien# include <sys/stat.h>
7190075Sobrien# ifdef TERMIOS_NEEDS__SVID3
7290075Sobrien#  undef _SVID3
7390075Sobrien# endif
7490075Sobrien#endif
7590075Sobrien
7690075Sobrien#ifdef HAVE_SYS_IOCTL_H
7790075Sobrien#include <sys/ioctl.h>
7890075Sobrien#endif
7990075Sobrien
8090075Sobrien#include "ntpd.h"
81132718Skan#include "ntp_io.h"
82132718Skan#include "ntp_control.h"
83132718Skan#include "ntp_refclock.h"
84132718Skan#include "ntp_unixtime.h"
85132718Skan#include "ntp_calgps.h"
86132718Skan#include "ntp_stdlib.h"
87132718Skan
88132718Skan/*
89132718Skan * GPS Definitions
90132718Skan */
91132718Skan#define	DESCRIPTION	"Trimble Palisade GPS" /* Long name */
92132718Skan#define	PRECISION	(-20)	/* precision assumed (about 1 us) */
93132718Skan#define	REFID		"GPS\0"	/* reference ID */
9490075Sobrien#define TRMB_MINPOLL    4	/* 16 seconds */
9590075Sobrien#define TRMB_MAXPOLL	5	/* 32 seconds */
9690075Sobrien
9790075Sobrien/*
9890075Sobrien * I/O Definitions
99132718Skan */
10090075Sobrien#define	DEVICE		"/dev/palisade%d" 	/* device name and unit */
10190075Sobrien#define	SPEED232	B9600		  	/* uart speed (9600 baud) */
10290075Sobrien#define	SPEED232COP	B38400		  	/* uart speed for Copernicus II (38400 baud) */
10390075Sobrien
104146895Skan/*
105146895Skan * TSIP Report Definitions
106146895Skan */
10790075Sobrien#define LENCODE_8F0B	74	/* Length of TSIP 8F-0B Packet & header */
10890075Sobrien#define LENCODE_NTP     22	/* Length of Palisade NTP Packet */
109146895Skan
11090075Sobrien#define LENCODE_8FAC    68      /* Length of Thunderbolt 8F-AC Position Packet*/
11190075Sobrien#define LENCODE_8FAB    17      /* Length of Thunderbolt Primary Timing Packet*/
11290075Sobrien
11390075Sobrien#define LENCODE_41      10      /* Length of Copernicus II GPS Time Packet*/
11490075Sobrien
11590075Sobrien/* Allowed Sub-Packet ID's */
11690075Sobrien#define PACKET_8F0B	0x0B
11790075Sobrien#define PACKET_NTP	0xAD
11890075Sobrien
11990075Sobrien/* Thunderbolt Packets */
12090075Sobrien#define PACKET_8FAC     0xAC	/* Supplementary Thunderbolt Time Packet */
12190075Sobrien#define PACKET_8FAB     0xAB	/* Primary Thunderbolt Time Packet */
12290075Sobrien#define PACKET_6D	0x6D	/* Supplementary Thunderbolt Tracking Stats */
12390075Sobrien#define PACKET_41	0x41	/* Thunderbolt I dont know what this packet is, it's not documented on my manual*/
12490075Sobrien
125132718Skan/* Acutime Packets */
12690075Sobrien#define PACKET_41A      0x41    /* GPS time */
12790075Sobrien#define PACKET_46       0x46    /* Receiver Health */
12890075Sobrien
12990075Sobrien#define DLE 0x10
13090075Sobrien#define ETX 0x03
13190075Sobrien
13290075Sobrien/* parse states */
13390075Sobrien#define TSIP_PARSED_EMPTY       0
13490075Sobrien#define TSIP_PARSED_FULL        1
13590075Sobrien#define TSIP_PARSED_DLE_1       2
13690075Sobrien#define TSIP_PARSED_DATA        3
13790075Sobrien#define TSIP_PARSED_DLE_2       4
13890075Sobrien
13990075Sobrien/*
14090075Sobrien * Leap-Insert and Leap-Delete are encoded as follows:
14190075Sobrien * 	PALISADE_UTC_TIME set   and PALISADE_LEAP_PENDING set: INSERT leap
14290075Sobrien */
14390075Sobrien
14490075Sobrien#define PALISADE_LEAP_INPROGRESS 0x08 /* This is the leap flag			*/
14590075Sobrien#define PALISADE_LEAP_WARNING    0x04 /* GPS Leap Warning (see ICD-200) */
14690075Sobrien#define PALISADE_LEAP_PENDING    0x02 /* Leap Pending (24 hours)		*/
14790075Sobrien#define PALISADE_UTC_TIME        0x01 /* UTC time available				*/
148132718Skan
149132718Skan#define mb(_X_) (up->rpt_buf[(_X_ + 1)]) /* shortcut for buffer access	*/
15090075Sobrien
15190075Sobrien/* Conversion Definitions */
15290075Sobrien#define GPS_PI 		(3.1415926535898)
15390075Sobrien#define	R2D		(180.0/GPS_PI)
15490075Sobrien
15590075Sobrien/*
156132718Skan * Structure for build data packets for send (thunderbolt uses it only)
15790075Sobrien * taken from Markus Prosch
15890075Sobrien */
15990075Sobrienstruct packettx
16090075Sobrien{
16190075Sobrien	short	size;
16290075Sobrien	u_char *data;
16390075Sobrien};
16490075Sobrien
16590075Sobrien/*
16690075Sobrien * Palisade unit control structure.
16790075Sobrien */
16890075Sobrienstruct palisade_unit {
16990075Sobrien	short		unit;		/* NTP refclock unit number */
17090075Sobrien	int 		polled;		/* flag to detect noreplies */
171117395Skan	char		leap_status;	/* leap second flag */
17290075Sobrien	char		rpt_status;	/* TSIP Parser State */
17390075Sobrien	short 		rpt_cnt;	/* TSIP packet length so far */
17490075Sobrien	char 		rpt_buf[BMAX]; 	/* packet assembly buffer */
17590075Sobrien	int		type;		/* Clock mode type */
17690075Sobrien	int		month;		/* for LEAP filter */
17790075Sobrien};
17890075Sobrien
17990075Sobrien/*
18090075Sobrien * Function prototypes
18190075Sobrien */
18290075Sobrien
18390075Sobrienstatic	int	palisade_start		(int, struct peer *);
18490075Sobrienstatic	void	palisade_shutdown	(int, struct peer *);
18590075Sobrienstatic	void	palisade_receive	(struct peer *);
18690075Sobrienstatic	void	palisade_poll		(int, struct peer *);
18790075Sobrienstatic	void 	palisade_io		(struct recvbuf *);
18890075Sobrienint 		palisade_configure	(int, struct peer *);
18990075Sobrienint 		TSIP_decode		(struct peer *);
19090075Sobrienlong		HW_poll			(struct refclockproc *);
191132718Skanstatic	double	getdbl 			(u_char *);
19290075Sobrienstatic	short	getint 			(u_char *);
19390075Sobrienstatic	int32	getlong			(u_char *);
19490075Sobrienstatic  int32   getsingle		(u_char *);
195117395Skan
19690075Sobrien
19790075Sobrienstatic  void	sendcmd			(struct packettx *buffer, int c);
19890075Sobrienstatic  void	sendsupercmd		(struct packettx *buffer, int c1, int c2);
19990075Sobrienstatic  void	sendbyte		(struct packettx *buffer, int b);
20090075Sobrienstatic  void	sendint			(struct packettx *buffer, int a);
201132718Skanstatic  int	sendetx			(struct packettx *buffer, int fd);
20290075Sobrienstatic  void	init_thunderbolt	(int fd);
203117395Skanstatic  void	init_acutime		(int fd);
20490075Sobrien
20590075Sobrien#endif /* REFCLOCK_PALISADE_H */
20690075Sobrien