xref: /netbsd-src/external/bsd/ntp/dist/libparse/clk_wharton.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: clk_wharton.c,v 1.1.1.1 2009/12/13 16:55:22 kardel 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 /*
80  * cvt_wharton_400a
81  *
82  * convert simple type format
83  */
84 static          u_long
85 cvt_wharton_400a(
86 	unsigned char *buffer,
87 	int            size,
88 	struct format *format,
89 	clocktime_t   *clock_time,
90 	void          *local
91 	)
92 {
93 	int	i;
94 
95 	/* The given `size' includes a terminating null-character. */
96 	if (size != 16 || buffer[0] != STX || buffer[14] != ETX
97 	    || buffer[13] < '0' || buffer[13] > ('0' + 0xf))
98 		return CVT_NONE;
99 	for (i = 1; i < 13; i += 1)
100 		if (buffer[i] < '0' || buffer[i] > '9')
101 			return CVT_NONE;
102 	clock_time->second = (buffer[2] - '0') * 10 + buffer[1] - '0';
103 	clock_time->minute = (buffer[4] - '0') * 10 + buffer[3] - '0';
104 	clock_time->hour   = (buffer[6] - '0') * 10 + buffer[5] - '0';
105 	clock_time->day    = (buffer[8] - '0') * 10 + buffer[7] - '0';
106 	clock_time->month  = (buffer[10] - '0') * 10 + buffer[9] - '0';
107 	clock_time->year   = (buffer[12] - '0') * 10 + buffer[11] - '0';
108 	clock_time->usecond = 0;
109 	if (buffer[13] & 0x1) /* We have CET time */
110 		clock_time->utcoffset = -1*60*60;
111 	else		/* We have BST time */
112 		clock_time->utcoffset = 0;
113 	if (buffer[13] & 0x2) {
114 		clock_time->flags |= PARSEB_DST;
115 		clock_time->utcoffset += -1*60*60;
116 	}
117 	if (!(buffer[13] & 0x4))
118 		clock_time->flags |= PARSEB_NOSYNC;
119 	if (buffer[13] & 0x8)
120 		clock_time->flags |= PARSEB_ANNOUNCE;
121 
122 	return CVT_OK;
123 }
124 
125 /*
126  * inp_wharton_400a
127  *
128  * grep data from input stream
129  */
130 static u_long
131 inp_wharton_400a(
132 	      parse_t      *parseio,
133 	      unsigned int  ch,
134 	      timestamp_t  *tstamp
135 	      )
136 {
137 	unsigned int rtc;
138 
139 	parseprintf(DD_PARSE, ("inp_wharton_400a(0x%lx, 0x%x, ...)\n", (long)parseio, ch));
140 
141 	switch (ch)
142 	{
143 	case STX:
144 		parseprintf(DD_PARSE, ("inp_wharton_400a: STX seen\n"));
145 
146 		parseio->parse_index = 1;
147 		parseio->parse_data[0] = ch;
148 		parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
149 		return PARSE_INP_SKIP;
150 
151 	case ETX:
152 		parseprintf(DD_PARSE, ("inp_wharton_400a: ETX seen\n"));
153 		if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
154 			return parse_end(parseio);
155 		else
156 			return rtc;
157 
158 	default:
159 		return parse_addchar(parseio, ch);
160 	}
161 }
162 
163 clockformat_t   clock_wharton_400a =
164 {
165 	inp_wharton_400a,	/* input handling function */
166 	cvt_wharton_400a,	/* conversion function */
167 	0,			/* no PPS monitoring */
168 	0,			/* conversion configuration */
169 	"WHARTON 400A Series clock Output Format 1",	/* String format name */
170 	15,			/* string buffer */
171 	0			/* no private data (complete pakets) */
172 };
173 
174 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_WHARTON_400A) */
175 int clk_wharton_400a_bs;
176 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_WHARTON_400A) */
177 
178 /*
179  * clk_wharton.c,v
180  * Revision 4.1  1999/02/28 15:27:24  kardel
181  * wharton clock integration
182  *
183  */
184