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