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