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