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