1 /*        $NetBSD: refclock_palisade.h,v 1.6 2020/05/25 20:47:25 christos 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 #if defined HAVE_SYS_MODEM_H
59 #include <sys/modem.h>
60 #ifndef __QNXNTO__
61 #define TIOCMSET MCSETA
62 #define TIOCMGET MCGETA
63 #define TIOCM_RTS MRTS
64 #endif
65 #endif
66 
67 #ifdef HAVE_TERMIOS_H
68 # ifdef TERMIOS_NEEDS__SVID3
69 #  define _SVID3
70 # endif
71 # include <termios.h>
72 # include <sys/stat.h>
73 # ifdef TERMIOS_NEEDS__SVID3
74 #  undef _SVID3
75 # endif
76 #endif
77 
78 #ifdef HAVE_SYS_IOCTL_H
79 #include <sys/ioctl.h>
80 #endif
81 
82 #include "ntpd.h"
83 #include "ntp_io.h"
84 #include "ntp_control.h"
85 #include "ntp_refclock.h"
86 #include "ntp_unixtime.h"
87 #include "ntp_calgps.h"
88 #include "ntp_stdlib.h"
89 
90 /*
91  * GPS Definitions
92  */
93 #define   DESCRIPTION         "Trimble Palisade GPS" /* Long name */
94 #define   PRECISION (-20)     /* precision assumed (about 1 us) */
95 #define   REFID               "GPS\0"   /* reference ID */
96 #define TRMB_MINPOLL    4     /* 16 seconds */
97 #define TRMB_MAXPOLL          5         /* 32 seconds */
98 
99 /*
100  * I/O Definitions
101  */
102 #define   DEVICE              "/dev/palisade%d"   /* device name and unit */
103 #define   SPEED232  B9600                         /* uart speed (9600 baud) */
104 #define   SPEED232COP         B38400                        /* uart speed for Copernicus II (38400 baud) */
105 
106 /*
107  * TSIP Report Definitions
108  */
109 #define LENCODE_8F0B          74        /* Length of TSIP 8F-0B Packet & header */
110 #define LENCODE_NTP     22    /* Length of Palisade NTP Packet */
111 
112 #define LENCODE_8FAC    68      /* Length of Thunderbolt 8F-AC Position Packet*/
113 #define LENCODE_8FAB    17      /* Length of Thunderbolt Primary Timing Packet*/
114 
115 #define LENCODE_41      10      /* Length of Copernicus II GPS Time 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           int                 month;              /* for LEAP filter */
179 };
180 
181 /*
182  * Function prototypes
183  */
184 
185 static    int       palisade_start                (int, struct peer *);
186 static    void      palisade_shutdown   (int, struct peer *);
187 static    void      palisade_receive    (struct peer *);
188 static    void      palisade_poll                 (int, struct peer *);
189 static    void      palisade_io                   (struct recvbuf *);
190 int                 palisade_configure  (int, struct peer *);
191 int                 TSIP_decode                   (struct peer *);
192 long                HW_poll                       (struct refclockproc *);
193 static    double    getdbl                        (u_char *);
194 static    short     getint                        (u_char *);
195 static    int32     getlong                       (u_char *);
196 static  int32   getsingle               (u_char *);
197 
198 
199 static  void        sendcmd                       (struct packettx *buffer, int c);
200 static  void        sendsupercmd                  (struct packettx *buffer, int c1, int c2);
201 static  void        sendbyte            (struct packettx *buffer, int b);
202 static  void        sendint                       (struct packettx *buffer, int a);
203 static  int         sendetx                       (struct packettx *buffer, int fd);
204 static  void        init_thunderbolt    (int fd);
205 static  void        init_acutime                  (int fd);
206 
207 #endif /* REFCLOCK_PALISADE_H */
208