xref: /freebsd/contrib/ntp/libparse/clk_trimtaip.c (revision f5f40dd63bc7acbb5312b26ac1ea1103c12352a6)
1c0b746e5SOllivier Robert /*
2ea906c41SOllivier Robert  * /src/NTP/ntp4-dev/libparse/clk_trimtaip.c,v 4.11 2005/04/16 17:32:10 kardel RELEASE_20050508_A
3c0b746e5SOllivier Robert  *
4ea906c41SOllivier Robert  * clk_trimtaip.c,v 4.11 2005/04/16 17:32:10 kardel RELEASE_20050508_A
5c0b746e5SOllivier Robert  *
6c0b746e5SOllivier Robert  * Trimble SV6 clock support - several collected codepieces
7ea906c41SOllivier Robert  *
8ea906c41SOllivier Robert  * Copyright (c) 1995-2005 by Frank Kardel <kardel <AT> ntp.org>
9a25439b6SCy Schubert  * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universitaet Erlangen-Nuernberg, Germany
10ea906c41SOllivier Robert  *
11ea906c41SOllivier Robert  * Redistribution and use in source and binary forms, with or without
12ea906c41SOllivier Robert  * modification, are permitted provided that the following conditions
13ea906c41SOllivier Robert  * are met:
14ea906c41SOllivier Robert  * 1. Redistributions of source code must retain the above copyright
15ea906c41SOllivier Robert  *    notice, this list of conditions and the following disclaimer.
16ea906c41SOllivier Robert  * 2. Redistributions in binary form must reproduce the above copyright
17ea906c41SOllivier Robert  *    notice, this list of conditions and the following disclaimer in the
18ea906c41SOllivier Robert  *    documentation and/or other materials provided with the distribution.
19ea906c41SOllivier Robert  * 3. Neither the name of the author nor the names of its contributors
20ea906c41SOllivier Robert  *    may be used to endorse or promote products derived from this software
21ea906c41SOllivier Robert  *    without specific prior written permission.
22ea906c41SOllivier Robert  *
23ea906c41SOllivier Robert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24ea906c41SOllivier Robert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ea906c41SOllivier Robert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ea906c41SOllivier Robert  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27ea906c41SOllivier Robert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ea906c41SOllivier Robert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ea906c41SOllivier Robert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ea906c41SOllivier Robert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ea906c41SOllivier Robert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ea906c41SOllivier Robert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ea906c41SOllivier Robert  * SUCH DAMAGE.
34ea906c41SOllivier Robert  *
35c0b746e5SOllivier Robert  */
36c0b746e5SOllivier Robert 
37c0b746e5SOllivier Robert #ifdef HAVE_CONFIG_H
38c0b746e5SOllivier Robert # include <config.h>
39c0b746e5SOllivier Robert #endif
40c0b746e5SOllivier Robert 
41c0b746e5SOllivier Robert #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_TRIMTAIP)
42c0b746e5SOllivier Robert 
43c0b746e5SOllivier Robert #include "ntp_fp.h"
44c0b746e5SOllivier Robert #include "ntp_unixtime.h"
45c0b746e5SOllivier Robert #include "ntp_calendar.h"
46c0b746e5SOllivier Robert 
47c0b746e5SOllivier Robert #include "parse.h"
48c0b746e5SOllivier Robert 
49c0b746e5SOllivier Robert #ifndef PARSESTREAM
50c0b746e5SOllivier Robert #include "ntp_stdlib.h"
51c0b746e5SOllivier Robert #include <stdio.h>
52c0b746e5SOllivier Robert #else
53c0b746e5SOllivier Robert #include "sys/parsestreams.h"
542b15cb3dSCy Schubert extern int printf (const char *, ...);
55c0b746e5SOllivier Robert #endif
56c0b746e5SOllivier Robert 
57c0b746e5SOllivier Robert /*	0000000000111111111122222222223333333	/ char
58c0b746e5SOllivier Robert  *	0123456789012345678901234567890123456	\ posn
59c0b746e5SOllivier Robert  *	>RTMhhmmssdddDDMMYYYYoodnnvrrrrr;*xx<	Actual
60c0b746e5SOllivier Robert  *	----33445566600112222BB7__-_____--99-	Parse
61c0b746e5SOllivier Robert  *	>RTM                      1     ;*  <",	Check
62c0b746e5SOllivier Robert  */
63c0b746e5SOllivier Robert 
64c0b746e5SOllivier Robert #define	hexval(x) (('0' <= (x) && (x) <= '9') ? (x) - '0' : \
65c0b746e5SOllivier Robert 		   ('a' <= (x) && (x) <= 'f') ? (x) - 'a' + 10 : \
66c0b746e5SOllivier Robert 		   ('A' <= (x) && (x) <= 'F') ? (x) - 'A' + 10 : \
67c0b746e5SOllivier Robert 		   -1)
68c0b746e5SOllivier Robert #define	O_USEC		O_WDAY
69c0b746e5SOllivier Robert #define	O_GPSFIX	O_FLAGS
70c0b746e5SOllivier Robert #define	O_CHKSUM	O_UTCHOFFSET
71c0b746e5SOllivier Robert      static struct format trimsv6_fmt =
72c0b746e5SOllivier Robert { { { 13, 2 }, {15, 2}, { 17, 4}, /* Day, Month, Year */
73c0b746e5SOllivier Robert     {  4, 2 }, { 6, 2}, {  8, 2}, /* Hour, Minute, Second */
74c0b746e5SOllivier Robert     { 10, 3 }, {23, 1}, {  0, 0}, /* uSec, FIXes (WeekDAY, FLAGS, ZONE) */
75c0b746e5SOllivier Robert     { 34, 2 }, { 0, 0}, { 21, 2}, /* cksum, -, utcS (UTC[HMS]OFFSET) */
76c0b746e5SOllivier Robert },
77c0b746e5SOllivier Robert   (const unsigned char *)">RTM                      1     ;*  <",
78c0b746e5SOllivier Robert   0
79c0b746e5SOllivier Robert };
80c0b746e5SOllivier Robert 
81a25439b6SCy Schubert static parse_cvt_fnc_t cvt_trimtaip;
82a25439b6SCy Schubert static parse_inp_fnc_t inp_trimtaip;
83c0b746e5SOllivier Robert 
84c0b746e5SOllivier Robert clockformat_t clock_trimtaip =
85c0b746e5SOllivier Robert {
86c0b746e5SOllivier Robert   inp_trimtaip,			/* no input handling */
87c0b746e5SOllivier Robert   cvt_trimtaip,			/* Trimble conversion */
88c0b746e5SOllivier Robert   pps_one,			/* easy PPS monitoring */
89c0b746e5SOllivier Robert   (void *)&trimsv6_fmt,		/* conversion configuration */
90c0b746e5SOllivier Robert   "Trimble TAIP",
91c0b746e5SOllivier Robert   37,				/* string buffer */
92c0b746e5SOllivier Robert   0				/* no private data */
93c0b746e5SOllivier Robert };
94c0b746e5SOllivier Robert 
95a25439b6SCy Schubert /* parse_cvt_fnc_t cvt_trimtaip */
96a25439b6SCy Schubert static u_long
97c0b746e5SOllivier Robert cvt_trimtaip(
98c0b746e5SOllivier Robert 	     unsigned char *buffer,
99c0b746e5SOllivier Robert 	     int            size,
100c0b746e5SOllivier Robert 	     struct format *format,
101c0b746e5SOllivier Robert 	     clocktime_t   *clock_time,
102c0b746e5SOllivier Robert 	     void          *local
103c0b746e5SOllivier Robert 	     )
104c0b746e5SOllivier Robert {
105c0b746e5SOllivier Robert 	long gpsfix;
106c0b746e5SOllivier Robert 	u_char calc_csum = 0;
107c0b746e5SOllivier Robert 	long   recv_csum;
108c0b746e5SOllivier Robert 	int	 i;
109c0b746e5SOllivier Robert 
110c0b746e5SOllivier Robert 	if (!Strok(buffer, format->fixed_string)) return CVT_NONE;
111c0b746e5SOllivier Robert #define	OFFS(x) format->field_offsets[(x)].offset
112c0b746e5SOllivier Robert #define	STOI(x, y) \
113c0b746e5SOllivier Robert 	Stoi(&buffer[OFFS(x)], y, \
114c0b746e5SOllivier Robert 	     format->field_offsets[(x)].length)
115c0b746e5SOllivier Robert 		if (	STOI(O_DAY,	&clock_time->day)	||
116c0b746e5SOllivier Robert 			STOI(O_MONTH,	&clock_time->month)	||
117c0b746e5SOllivier Robert 			STOI(O_YEAR,	&clock_time->year)	||
118c0b746e5SOllivier Robert 			STOI(O_HOUR,	&clock_time->hour)	||
119c0b746e5SOllivier Robert 			STOI(O_MIN,	&clock_time->minute)	||
120c0b746e5SOllivier Robert 			STOI(O_SEC,	&clock_time->second)	||
121c0b746e5SOllivier Robert 			STOI(O_USEC,	&clock_time->usecond)||
122c0b746e5SOllivier Robert 			STOI(O_GPSFIX,	&gpsfix)
123c0b746e5SOllivier Robert 			) return CVT_FAIL|CVT_BADFMT;
124c0b746e5SOllivier Robert 
125c0b746e5SOllivier Robert 	clock_time->usecond *= 1000;
126c0b746e5SOllivier Robert 	/* Check that the checksum is right */
127c0b746e5SOllivier Robert 	for (i=OFFS(O_CHKSUM)-1; i >= 0; i--) calc_csum ^= buffer[i];
128c0b746e5SOllivier Robert 	recv_csum =	(hexval(buffer[OFFS(O_CHKSUM)]) << 4) |
129c0b746e5SOllivier Robert 		hexval(buffer[OFFS(O_CHKSUM)+1]);
130c0b746e5SOllivier Robert 	if (recv_csum < 0) return CVT_FAIL|CVT_BADTIME;
131c0b746e5SOllivier Robert 	if (((u_char) recv_csum) != calc_csum) return CVT_FAIL|CVT_BADTIME;
132c0b746e5SOllivier Robert 
133c0b746e5SOllivier Robert 	clock_time->utcoffset = 0;
134c0b746e5SOllivier Robert 
135c0b746e5SOllivier Robert 	/* What should flags be set to ? */
136c0b746e5SOllivier Robert 	clock_time->flags = PARSEB_UTC;
137c0b746e5SOllivier Robert 
138c0b746e5SOllivier Robert 	/* if the current GPS fix is 9 (unknown), reject */
139c0b746e5SOllivier Robert 	if (0 > gpsfix || gpsfix > 9) clock_time->flags |= PARSEB_POWERUP;
140c0b746e5SOllivier Robert 
141c0b746e5SOllivier Robert 	return CVT_OK;
142c0b746e5SOllivier Robert }
143c0b746e5SOllivier Robert 
144c0b746e5SOllivier Robert /*
145a25439b6SCy Schubert  * parse_inp_fnc_t inp_trimtaip
146c0b746e5SOllivier Robert  *
147a25439b6SCy Schubert  * grab data from input stream
148c0b746e5SOllivier Robert  */
149c0b746e5SOllivier Robert static u_long
150c0b746e5SOllivier Robert inp_trimtaip(
151c0b746e5SOllivier Robert 	     parse_t      *parseio,
152a25439b6SCy Schubert 	     char         ch,
153c0b746e5SOllivier Robert 	     timestamp_t  *tstamp
154c0b746e5SOllivier Robert 	  )
155c0b746e5SOllivier Robert {
156c0b746e5SOllivier Robert 	unsigned int rtc;
157c0b746e5SOllivier Robert 
1583311ff84SXin LI 	parseprintf(DD_PARSE, ("inp_trimtaip(0x%p, 0x%x, ...)\n", (void*)parseio, ch));
159c0b746e5SOllivier Robert 
160c0b746e5SOllivier Robert 	switch (ch)
161c0b746e5SOllivier Robert 	{
162c0b746e5SOllivier Robert 	case '>':
163c0b746e5SOllivier Robert 		parseprintf(DD_PARSE, ("inp_trimptaip: START seen\n"));
164c0b746e5SOllivier Robert 
165c0b746e5SOllivier Robert 		parseio->parse_index = 1;
166c0b746e5SOllivier Robert 		parseio->parse_data[0] = ch;
167c0b746e5SOllivier Robert 		parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
168c0b746e5SOllivier Robert 		return PARSE_INP_SKIP;
169c0b746e5SOllivier Robert 
170c0b746e5SOllivier Robert 	case '<':
171c0b746e5SOllivier Robert 		parseprintf(DD_PARSE, ("inp_trimtaip: END seen\n"));
172c0b746e5SOllivier Robert 		if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
173c0b746e5SOllivier Robert 			return parse_end(parseio);
174c0b746e5SOllivier Robert 		else
175c0b746e5SOllivier Robert 			return rtc;
176c0b746e5SOllivier Robert 
177c0b746e5SOllivier Robert 
178c0b746e5SOllivier Robert 	default:
179c0b746e5SOllivier Robert 		return parse_addchar(parseio, ch);
180c0b746e5SOllivier Robert 	}
181c0b746e5SOllivier Robert }
182c0b746e5SOllivier Robert 
183c0b746e5SOllivier Robert #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTAIP) */
184*f5f40dd6SCy Schubert NONEMPTY_TRANSLATION_UNIT
185c0b746e5SOllivier Robert #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTAIP) */
186c0b746e5SOllivier Robert 
187c0b746e5SOllivier Robert /*
188c0b746e5SOllivier Robert  * History:
189c0b746e5SOllivier Robert  *
190c0b746e5SOllivier Robert  * clk_trimtaip.c,v
191ea906c41SOllivier Robert  * Revision 4.11  2005/04/16 17:32:10  kardel
192ea906c41SOllivier Robert  * update copyright
193ea906c41SOllivier Robert  *
194ea906c41SOllivier Robert  * Revision 4.10  2004/11/14 15:29:41  kardel
195ea906c41SOllivier Robert  * support PPSAPI, upgrade Copyright to Berkeley style
196ea906c41SOllivier Robert  *
197a151a66cSOllivier Robert  * Revision 4.7  1999/11/28 09:13:51  kardel
198a151a66cSOllivier Robert  * RECON_4_0_98F
199a151a66cSOllivier Robert  *
200c0b746e5SOllivier Robert  * Revision 4.6  1998/08/16 18:46:27  kardel
201c0b746e5SOllivier Robert  * (clock_trimtaip =): changed format name
202c0b746e5SOllivier Robert  *
203c0b746e5SOllivier Robert  * Revision 4.5  1998/06/14 21:09:38  kardel
204c0b746e5SOllivier Robert  * Sun acc cleanup
205c0b746e5SOllivier Robert  *
206c0b746e5SOllivier Robert  * Revision 4.4  1998/06/13 12:06:57  kardel
207c0b746e5SOllivier Robert  * fix SYSV clock name clash
208c0b746e5SOllivier Robert  *
209c0b746e5SOllivier Robert  * Revision 4.3  1998/06/12 15:22:29  kardel
210c0b746e5SOllivier Robert  * fix prototypes
211c0b746e5SOllivier Robert  *
212c0b746e5SOllivier Robert  * Revision 4.2  1998/06/12 09:13:26  kardel
213c0b746e5SOllivier Robert  * conditional compile macros fixed
214c0b746e5SOllivier Robert  * printf prototype
215c0b746e5SOllivier Robert  *
216c0b746e5SOllivier Robert  * Revision 4.1  1998/05/24 09:39:54  kardel
217c0b746e5SOllivier Robert  * implementation of the new IO handling model
218c0b746e5SOllivier Robert  *
219c0b746e5SOllivier Robert  * Revision 4.0  1998/04/10 19:45:31  kardel
220c0b746e5SOllivier Robert  * Start 4.0 release version numbering
221c0b746e5SOllivier Robert  *
222c0b746e5SOllivier Robert  * from V3 1.4 log info deleted 1998/04/11 kardel
223c0b746e5SOllivier Robert  */
224c0b746e5SOllivier Robert 
225