xref: /netbsd-src/external/bsd/ntp/dist/libparse/clk_wharton.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: clk_wharton.c,v 1.6 2020/05/25 20:47:25 christos Exp $	*/
2 
3 /*
4  * /src/NTP/ntp4-dev/libparse/clk_wharton.c,v 4.2 2004/11/14 15:29:41 kardel RELEASE_20050508_A
5  *
6  * clk_wharton.c,v 4.2 2004/11/14 15:29:41 kardel RELEASE_20050508_A
7  *
8  * From Philippe De Muyter <phdm@macqel.be>, 1999
9  */
10 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13 
14 #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_WHARTON_400A)
15 /*
16  * Support for WHARTON 400A Series clock + 404.2 serial interface.
17  *
18  * Copyright (C) 1999, 2000 by Philippe De Muyter <phdm@macqel.be>
19  *
20  * This program is distributed in the hope that it will be useful, but WITHOUT
21  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  * FITNESS FOR A PARTICULAR PURPOSE.
23  *
24  */
25 
26 #include "ntp_fp.h"
27 #include "ascii.h"
28 #include "parse.h"
29 
30 #ifndef PARSESTREAM
31 #include "ntp_stdlib.h"
32 #include <stdio.h>
33 #else
34 #include "sys/parsestreams.h"
35 extern void printf (const char *, ...);
36 #endif
37 
38 /*
39  * In private e-mail alastair@wharton.co.uk said :
40  * "If you are going to use the 400A and 404.2 system [for ntp] I recommend
41  * that you set the 400A to output the message every second.  The start of
42  * transmission of the first byte of the message is synchronised to the
43  * second edge."
44  * The WHARTON 400A Series is able to send date/time serial messages
45  * in 7 output formats.  We use format 1 here because it is the shortest.
46  * For use with this driver, the WHARTON 400A Series clock must be set-up
47  * as follows :
48  *					Programmable	Selected
49  *					Option No	Option
50  *	BST or CET display		3		9 or 11
51  *	No external controller		7		0
52  *	Serial Output Format 1		9		1
53  *	Baud rate 9600 bps		10		96
54  *	Bit length 8 bits		11		8
55  *	Parity even			12		E
56  *
57  * WHARTON 400A Series output format 1 is as follows :
58  *
59  * Timestamp	STXssmmhhDDMMYYSETX
60  * Pos		0  12345678901234
61  *		0  00000000011111
62  *
63  *	STX	start transmission (ASCII 0x02)
64  *	ETX	end transmission (ASCII 0x03)
65  *	ss	Second expressed in reversed decimal (units then tens)
66  *	mm	Minute expressed in reversed decimal
67  *	hh	Hour expressed in reversed decimal
68  *	DD	Day of month expressed in reversed decimal
69  *	MM	Month expressed in reversed decimal (January is 1)
70  *	YY	Year (without century) expressed in reversed decimal
71  *	S	Status byte : 0x30 +
72  *			bit 0	0 = MSF source		1 = DCF source
73  *			bit 1	0 = Winter time		1 = Summer time
74  *			bit 2	0 = not synchronised	1 = synchronised
75  *			bit 3	0 = no early warning	1 = early warning
76  *
77  */
78 
79 static parse_cvt_fnc_t cvt_wharton_400a;
80 static parse_inp_fnc_t inp_wharton_400a;
81 
82 /*
83  * parse_cvt_fnc_t cvt_wharton_400a
84  *
85  * convert simple type format
86  */
87 static          u_long
88 cvt_wharton_400a(
89 	unsigned char *buffer,
90 	int            size,
91 	struct format *format,
92 	clocktime_t   *clock_time,
93 	void          *local
94 	)
95 {
96 	int	i;
97 
98 	/* The given `size' includes a terminating null-character. */
99 	if (size != 15 || buffer[0] != STX || buffer[14] != ETX
100 	    || buffer[13] < '0' || buffer[13] > ('0' + 0xf))
101 		return CVT_NONE;
102 	for (i = 1; i < 13; i += 1)
103 		if (buffer[i] < '0' || buffer[i] > '9')
104 			return CVT_NONE;
105 	clock_time->second = (buffer[2] - '0') * 10 + buffer[1] - '0';
106 	clock_time->minute = (buffer[4] - '0') * 10 + buffer[3] - '0';
107 	clock_time->hour   = (buffer[6] - '0') * 10 + buffer[5] - '0';
108 	clock_time->day    = (buffer[8] - '0') * 10 + buffer[7] - '0';
109 	clock_time->month  = (buffer[10] - '0') * 10 + buffer[9] - '0';
110 	clock_time->year   = (buffer[12] - '0') * 10 + buffer[11] - '0';
111 	clock_time->usecond = 0;
112 	if (buffer[13] & 0x1) /* We have CET time */
113 		clock_time->utcoffset = -1*60*60;
114 	else		/* We have BST time */
115 		clock_time->utcoffset = 0;
116 	if (buffer[13] & 0x2) {
117 		clock_time->flags |= PARSEB_DST;
118 		clock_time->utcoffset += -1*60*60;
119 	}
120 	if (!(buffer[13] & 0x4))
121 		clock_time->flags |= PARSEB_NOSYNC;
122 	if (buffer[13] & 0x8)
123 		clock_time->flags |= PARSEB_ANNOUNCE;
124 
125 	return CVT_OK;
126 }
127 
128 /*
129  * parse_inp_fnc_t inp_wharton_400a
130  *
131  * grab data from input stream
132  */
133 static u_long
134 inp_wharton_400a(
135 	      parse_t      *parseio,
136 	      char         ch,
137 	      timestamp_t  *tstamp
138 	      )
139 {
140 	unsigned int rtc;
141 
142 	parseprintf(DD_PARSE, ("inp_wharton_400a(0x%p, 0x%x, ...)\n", (void*)parseio, ch));
143 
144 	switch (ch)
145 	{
146 	case STX:
147 		parseprintf(DD_PARSE, ("inp_wharton_400a: STX seen\n"));
148 
149 		parseio->parse_index = 1;
150 		parseio->parse_data[0] = ch;
151 		parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
152 		return PARSE_INP_SKIP;
153 
154 	case ETX:
155 		parseprintf(DD_PARSE, ("inp_wharton_400a: ETX seen\n"));
156 		if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
157 			return parse_end(parseio);
158 		else
159 			return rtc;
160 
161 	default:
162 		return parse_addchar(parseio, ch);
163 	}
164 }
165 
166 clockformat_t   clock_wharton_400a =
167 {
168 	inp_wharton_400a,	/* input handling function */
169 	cvt_wharton_400a,	/* conversion function */
170 	0,			/* no PPS monitoring */
171 	0,			/* conversion configuration */
172 	"WHARTON 400A Series clock Output Format 1",	/* String format name */
173 	15,			/* string buffer */
174 	0			/* no private data (complete packets) */
175 };
176 
177 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_WHARTON_400A) */
178 int clk_wharton_400a_bs;
179 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_WHARTON_400A) */
180 
181 /*
182  * clk_wharton.c,v
183  * Revision 4.1  1999/02/28 15:27:24  kardel
184  * wharton clock integration
185  *
186  */
187