clk_wharton.c revision 302408
168651Skris/*
268651Skris * /src/NTP/ntp4-dev/libparse/clk_wharton.c,v 4.2 2004/11/14 15:29:41 kardel RELEASE_20050508_A
368651Skris *
468651Skris * clk_wharton.c,v 4.2 2004/11/14 15:29:41 kardel RELEASE_20050508_A
568651Skris *
668651Skris * From Philippe De Muyter <phdm@macqel.be>, 1999
768651Skris */
868651Skris#ifdef HAVE_CONFIG_H
968651Skris#include <config.h>
1068651Skris#endif
11160814Ssimon
12160814Ssimon#if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_WHARTON_400A)
1368651Skris/*
1468651Skris * Support for WHARTON 400A Series clock + 404.2 serial interface.
1568651Skris *
1668651Skris * Copyright (C) 1999, 2000 by Philippe De Muyter <phdm@macqel.be>
1768651Skris *
1868651Skris * This program is distributed in the hope that it will be useful, but WITHOUT
1976866Skris * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2068651Skris * FITNESS FOR A PARTICULAR PURPOSE.
2168651Skris *
2268651Skris */
2368651Skris
2468651Skris#include "ntp_fp.h"
2568651Skris#include "ascii.h"
2676866Skris#include "parse.h"
2776866Skris
2876866Skris#ifndef PARSESTREAM
2976866Skris#include "ntp_stdlib.h"
3076866Skris#include <stdio.h>
3176866Skris#else
3276866Skris#include "sys/parsestreams.h"
3376866Skrisextern void printf (const char *, ...);
3476866Skris#endif
3576866Skris
3676866Skris/*
3776866Skris * In private e-mail alastair@wharton.co.uk said :
3876866Skris * "If you are going to use the 400A and 404.2 system [for ntp] I recommend
3976866Skris * that you set the 400A to output the message every second.  The start of
4089837Skris * transmission of the first byte of the message is synchronised to the
4189837Skris * second edge."
4289837Skris * The WHARTON 400A Series is able to send date/time serial messages
4389837Skris * in 7 output formats.  We use format 1 here because it is the shortest.
4476866Skris * For use with this driver, the WHARTON 400A Series clock must be set-up
4589837Skris * as follows :
4689837Skris *					Programmable	Selected
4789837Skris *					Option No	Option
4889837Skris *	BST or CET display		3		9 or 11
4989837Skris *	No external controller		7		0
5089837Skris *	Serial Output Format 1		9		1
5168651Skris *	Baud rate 9600 bps		10		96
5268651Skris *	Bit length 8 bits		11		8
5368651Skris *	Parity even			12		E
5468651Skris *
5568651Skris * WHARTON 400A Series output format 1 is as follows :
5668651Skris *
5768651Skris * Timestamp	STXssmmhhDDMMYYSETX
5868651Skris * Pos		0  12345678901234
5968651Skris *		0  00000000011111
6068651Skris *
6168651Skris *	STX	start transmission (ASCII 0x02)
6268651Skris *	ETX	end transmission (ASCII 0x03)
6368651Skris *	ss	Second expressed in reversed decimal (units then tens)
6468651Skris *	mm	Minute expressed in reversed decimal
6568651Skris *	hh	Hour expressed in reversed decimal
6668651Skris *	DD	Day of month expressed in reversed decimal
6768651Skris *	MM	Month expressed in reversed decimal (January is 1)
6868651Skris *	YY	Year (without century) expressed in reversed decimal
6968651Skris *	S	Status byte : 0x30 +
7076866Skris *			bit 0	0 = MSF source		1 = DCF source
7168651Skris *			bit 1	0 = Winter time		1 = Summer time
7268651Skris *			bit 2	0 = not synchronised	1 = synchronised
7368651Skris *			bit 3	0 = no early warning	1 = early warning
74 *
75 */
76
77static parse_cvt_fnc_t cvt_wharton_400a;
78static parse_inp_fnc_t inp_wharton_400a;
79
80/*
81 * parse_cvt_fnc_t cvt_wharton_400a
82 *
83 * convert simple type format
84 */
85static          u_long
86cvt_wharton_400a(
87	unsigned char *buffer,
88	int            size,
89	struct format *format,
90	clocktime_t   *clock_time,
91	void          *local
92	)
93{
94	int	i;
95
96	/* The given `size' includes a terminating null-character. */
97	if (size != 15 || buffer[0] != STX || buffer[14] != ETX
98	    || buffer[13] < '0' || buffer[13] > ('0' + 0xf))
99		return CVT_NONE;
100	for (i = 1; i < 13; i += 1)
101		if (buffer[i] < '0' || buffer[i] > '9')
102			return CVT_NONE;
103	clock_time->second = (buffer[2] - '0') * 10 + buffer[1] - '0';
104	clock_time->minute = (buffer[4] - '0') * 10 + buffer[3] - '0';
105	clock_time->hour   = (buffer[6] - '0') * 10 + buffer[5] - '0';
106	clock_time->day    = (buffer[8] - '0') * 10 + buffer[7] - '0';
107	clock_time->month  = (buffer[10] - '0') * 10 + buffer[9] - '0';
108	clock_time->year   = (buffer[12] - '0') * 10 + buffer[11] - '0';
109	clock_time->usecond = 0;
110	if (buffer[13] & 0x1) /* We have CET time */
111		clock_time->utcoffset = -1*60*60;
112	else		/* We have BST time */
113		clock_time->utcoffset = 0;
114	if (buffer[13] & 0x2) {
115		clock_time->flags |= PARSEB_DST;
116		clock_time->utcoffset += -1*60*60;
117	}
118	if (!(buffer[13] & 0x4))
119		clock_time->flags |= PARSEB_NOSYNC;
120	if (buffer[13] & 0x8)
121		clock_time->flags |= PARSEB_ANNOUNCE;
122
123	return CVT_OK;
124}
125
126/*
127 * parse_inp_fnc_t inp_wharton_400a
128 *
129 * grab data from input stream
130 */
131static u_long
132inp_wharton_400a(
133	      parse_t      *parseio,
134	      char         ch,
135	      timestamp_t  *tstamp
136	      )
137{
138	unsigned int rtc;
139
140	parseprintf(DD_PARSE, ("inp_wharton_400a(0x%p, 0x%x, ...)\n", (void*)parseio, ch));
141
142	switch (ch)
143	{
144	case STX:
145		parseprintf(DD_PARSE, ("inp_wharton_400a: STX seen\n"));
146
147		parseio->parse_index = 1;
148		parseio->parse_data[0] = ch;
149		parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
150		return PARSE_INP_SKIP;
151
152	case ETX:
153		parseprintf(DD_PARSE, ("inp_wharton_400a: ETX seen\n"));
154		if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
155			return parse_end(parseio);
156		else
157			return rtc;
158
159	default:
160		return parse_addchar(parseio, ch);
161	}
162}
163
164clockformat_t   clock_wharton_400a =
165{
166	inp_wharton_400a,	/* input handling function */
167	cvt_wharton_400a,	/* conversion function */
168	0,			/* no PPS monitoring */
169	0,			/* conversion configuration */
170	"WHARTON 400A Series clock Output Format 1",	/* String format name */
171	15,			/* string buffer */
172	0			/* no private data (complete packets) */
173};
174
175#else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_WHARTON_400A) */
176int clk_wharton_400a_bs;
177#endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_WHARTON_400A) */
178
179/*
180 * clk_wharton.c,v
181 * Revision 4.1  1999/02/28 15:27:24  kardel
182 * wharton clock integration
183 *
184 */
185