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