xref: /netbsd-src/external/bsd/ntp/dist/libparse/clk_rcc8000.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: clk_rcc8000.c,v 1.7 2024/08/18 20:47:17 christos Exp $	*/
2abb0f93cSkardel 
3abb0f93cSkardel /*
4abb0f93cSkardel  * /src/NTP/ntp4-dev/libparse/clk_rcc8000.c,v 4.9 2004/11/14 15:29:41 kardel RELEASE_20050508_A
5abb0f93cSkardel  *
6abb0f93cSkardel  * clk_rcc8000.c,v 4.9 2004/11/14 15:29:41 kardel RELEASE_20050508_A
7abb0f93cSkardel  *
8abb0f93cSkardel  * Radiocode Clocks Ltd RCC 8000 Intelligent Off-Air Master Clock support
9abb0f93cSkardel  *
10abb0f93cSkardel  * Created by R.E.Broughton from clk_trimtaip.c
11abb0f93cSkardel  *
12abb0f93cSkardel  * This program is distributed in the hope that it will be useful,
13abb0f93cSkardel  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14abb0f93cSkardel  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15abb0f93cSkardel  *
16abb0f93cSkardel  */
17abb0f93cSkardel 
18abb0f93cSkardel #if HAVE_CONFIG_H
19abb0f93cSkardel # include <config.h>
20abb0f93cSkardel #endif
21abb0f93cSkardel 
22abb0f93cSkardel #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_RCC8000)
23abb0f93cSkardel 
24abb0f93cSkardel #include "ntp_fp.h"
25abb0f93cSkardel #include "ntp_unixtime.h"
26abb0f93cSkardel #include "ntp_calendar.h"
27abb0f93cSkardel 
28abb0f93cSkardel #include "parse.h"
29abb0f93cSkardel 
30abb0f93cSkardel #ifndef PARSESTREAM
31abb0f93cSkardel #include "ntp_stdlib.h"
32abb0f93cSkardel #include <stdio.h>
33abb0f93cSkardel #else
34abb0f93cSkardel #include "sys/parsestreams.h"
35abb0f93cSkardel extern int printf (const char *, ...);
36abb0f93cSkardel #endif
37abb0f93cSkardel 
38abb0f93cSkardel /* Type II Serial Output format
39abb0f93cSkardel  *
40abb0f93cSkardel  *	0000000000111111111122222222223	/ char
41abb0f93cSkardel  *	0123456789012345678901234567890	\ posn
42abb0f93cSkardel  *	HH:MM:SS.XYZ DD/MM/YY DDD W Prn   Actual
43abb0f93cSkardel  *      33 44 55 666 00 11 22       7     Parse
44abb0f93cSkardel  *        :  :  .      /  /          rn   Check
45abb0f93cSkardel  *     "15:50:36.534 30/09/94 273 5 A\x0d\x0a"
46abb0f93cSkardel  *
47abb0f93cSkardel  * DDD - Day of year number
48abb0f93cSkardel  *   W - Day of week number (Sunday is 0)
49abb0f93cSkardel  * P is the Status. See comment below for details.
50abb0f93cSkardel  */
51abb0f93cSkardel 
52abb0f93cSkardel #define	O_USEC		O_WDAY
53abb0f93cSkardel static struct format rcc8000_fmt =
54abb0f93cSkardel { { { 13, 2 }, {16, 2}, { 19, 2}, /* Day, Month, Year */
55abb0f93cSkardel     {  0, 2 }, { 3, 2}, {  6, 2}, /* Hour, Minute, Second */
56abb0f93cSkardel     {  9, 3 }, {28, 1}, {  0, 0}, /* uSec, Status (Valid,Reject,BST,Leapyear) */  },
57abb0f93cSkardel   (const unsigned char *)"  :  :  .      /  /          \r\n",
58abb0f93cSkardel   /*"15:50:36.534 30/09/94 273 5 A\x0d\x0a" */
59abb0f93cSkardel   0
60abb0f93cSkardel };
61abb0f93cSkardel 
627476e6e4Schristos static parse_cvt_fnc_t cvt_rcc8000;
637476e6e4Schristos static parse_inp_fnc_t inp_rcc8000;
64abb0f93cSkardel 
65abb0f93cSkardel clockformat_t clock_rcc8000 =
66abb0f93cSkardel {
67abb0f93cSkardel   inp_rcc8000,			/* no input handling */
68abb0f93cSkardel   cvt_rcc8000,			/* Radiocode clock conversion */
69abb0f93cSkardel   0,				/* no direct PPS monitoring */
70abb0f93cSkardel   (void *)&rcc8000_fmt,		/* conversion configuration */
71abb0f93cSkardel   "Radiocode RCC8000",
72abb0f93cSkardel   31,				/* string buffer */
73abb0f93cSkardel   0				/* no private data */
74abb0f93cSkardel };
75abb0f93cSkardel 
767476e6e4Schristos /* parse_cvt_fnc_t cvt_rcc8000 */
777476e6e4Schristos static u_long
78abb0f93cSkardel cvt_rcc8000(
79abb0f93cSkardel 	    unsigned char *buffer,
80abb0f93cSkardel 	    int            size,
81abb0f93cSkardel 	    struct format *format,
82abb0f93cSkardel 	    clocktime_t   *clock_time,
83abb0f93cSkardel 	    void          *local
84abb0f93cSkardel 	    )
85abb0f93cSkardel {
86abb0f93cSkardel 	if (!Strok(buffer, format->fixed_string)) return CVT_NONE;
87abb0f93cSkardel #define	OFFS(x) format->field_offsets[(x)].offset
88abb0f93cSkardel #define STOI(x, y) Stoi(&buffer[OFFS(x)], y, format->field_offsets[(x)].length)
89abb0f93cSkardel 	if (	STOI(O_DAY,	&clock_time->day)	||
90abb0f93cSkardel 		STOI(O_MONTH,	&clock_time->month)	||
91abb0f93cSkardel 		STOI(O_YEAR,	&clock_time->year)	||
92abb0f93cSkardel 		STOI(O_HOUR,	&clock_time->hour)	||
93abb0f93cSkardel 		STOI(O_MIN,	&clock_time->minute)	||
94abb0f93cSkardel 		STOI(O_SEC,	&clock_time->second)	||
95abb0f93cSkardel 		STOI(O_USEC,	&clock_time->usecond)
96abb0f93cSkardel 		) return CVT_FAIL|CVT_BADFMT;
97abb0f93cSkardel 	clock_time->usecond *= 1000;
98abb0f93cSkardel 
99abb0f93cSkardel 	clock_time->utcoffset = 0;
100abb0f93cSkardel 
101abb0f93cSkardel #define RCCP buffer[28]
102abb0f93cSkardel 	/*
103abb0f93cSkardel 	 * buffer[28] is the ASCII representation of a hex character ( 0 through F )
104abb0f93cSkardel 	 *      The four bits correspond to:
105abb0f93cSkardel 	 *      8 - Valid Time
106abb0f93cSkardel 	 *      4 - Reject Code
107abb0f93cSkardel 	 *      2 - British Summer Time (receiver set to emit GMT all year.)
108abb0f93cSkardel 	 *      1 - Leap year
109abb0f93cSkardel 	 */
110abb0f93cSkardel #define RCC8000_VALID  0x8
111abb0f93cSkardel #define RCC8000_REJECT 0x4
112abb0f93cSkardel #define RCC8000_BST    0x2
113abb0f93cSkardel #define RCC8000_LEAPY  0x1
114abb0f93cSkardel 
115abb0f93cSkardel 	clock_time->flags = 0;
116abb0f93cSkardel 
117abb0f93cSkardel 	if ( (RCCP >= '0' && RCCP <= '9') || (RCCP >= 'A' && RCCP <= 'F') )
118abb0f93cSkardel 	{
119abb0f93cSkardel 		register int flag;
120abb0f93cSkardel 
121abb0f93cSkardel 		flag = (RCCP >= '0' && RCCP <= '9' ) ?  RCCP - '0' : RCCP - 'A' + 10;
122abb0f93cSkardel 
123abb0f93cSkardel 		if (!(flag & RCC8000_VALID))
124abb0f93cSkardel 		    clock_time->flags |= PARSEB_POWERUP;
125abb0f93cSkardel 
126abb0f93cSkardel 		clock_time->flags |= PARSEB_UTC; /* British special - guess why 8-) */
127abb0f93cSkardel 
128abb0f93cSkardel 		/* other flags not used */
129abb0f93cSkardel 	}
130abb0f93cSkardel 	return CVT_OK;
131abb0f93cSkardel }
132abb0f93cSkardel /*
1337476e6e4Schristos  * parse_inp_fnc_t inp_rcc8000
134abb0f93cSkardel  *
1357476e6e4Schristos  * grab data from input stream
136abb0f93cSkardel  */
137abb0f93cSkardel static u_long
138abb0f93cSkardel inp_rcc8000(
139abb0f93cSkardel 	    parse_t      *parseio,
1407476e6e4Schristos 	    char         ch,
141abb0f93cSkardel 	    timestamp_t  *tstamp
142abb0f93cSkardel 	  )
143abb0f93cSkardel {
144abb0f93cSkardel 	unsigned int rtc;
145abb0f93cSkardel 
1468b8da087Schristos 	parseprintf(DD_PARSE, ("inp_rcc8000(0x%p, 0x%x, ...)\n", (void*)parseio, ch));
147abb0f93cSkardel 
148abb0f93cSkardel 	switch (ch)
149abb0f93cSkardel 	{
150abb0f93cSkardel 	case '\n':
151abb0f93cSkardel 		parseprintf(DD_PARSE, ("inp_rcc8000: EOL seen\n"));
152abb0f93cSkardel 		if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
153abb0f93cSkardel 			return parse_end(parseio);
154abb0f93cSkardel 		else
155abb0f93cSkardel 			return rtc;
156abb0f93cSkardel 
157abb0f93cSkardel 
158abb0f93cSkardel 	default:
159abb0f93cSkardel 		if (parseio->parse_index == 0) /* take sample at start of message */
160abb0f93cSkardel 		{
161abb0f93cSkardel 			parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
162abb0f93cSkardel 		}
163abb0f93cSkardel 		return parse_addchar(parseio, ch);
164abb0f93cSkardel 	}
165abb0f93cSkardel }
166abb0f93cSkardel 
167abb0f93cSkardel #else  /* not (REFCLOCK && CLOCK_PARSE && CLOCK_RCC8000) */
168*eabc0478Schristos NONEMPTY_TRANSLATION_UNIT
169abb0f93cSkardel #endif  /* not (REFCLOCK && CLOCK_PARSE && CLOCK_RCC8000) */
170abb0f93cSkardel 
171abb0f93cSkardel /*
172abb0f93cSkardel  * History:
173abb0f93cSkardel  *
174abb0f93cSkardel  * clk_rcc8000.c,v
175abb0f93cSkardel  * Revision 4.9  2004/11/14 15:29:41  kardel
176abb0f93cSkardel  * support PPSAPI, upgrade Copyright to Berkeley style
177abb0f93cSkardel  *
178abb0f93cSkardel  * Revision 4.6  1999/11/28 09:13:51  kardel
179abb0f93cSkardel  * RECON_4_0_98F
180abb0f93cSkardel  *
181abb0f93cSkardel  * Revision 4.5  1998/06/14 21:09:38  kardel
182abb0f93cSkardel  * Sun acc cleanup
183abb0f93cSkardel  *
184abb0f93cSkardel  * Revision 4.4  1998/06/13 12:05:02  kardel
185abb0f93cSkardel  * fix SYSV clock name clash
186abb0f93cSkardel  *
187abb0f93cSkardel  * Revision 4.3  1998/06/12 15:22:29  kardel
188abb0f93cSkardel  * fix prototypes
189abb0f93cSkardel  *
190abb0f93cSkardel  * Revision 4.2  1998/06/12 09:13:25  kardel
191abb0f93cSkardel  * conditional compile macros fixed
192abb0f93cSkardel  * printf prototype
193abb0f93cSkardel  *
194abb0f93cSkardel  * Revision 4.1  1998/05/24 09:39:53  kardel
195abb0f93cSkardel  * implementation of the new IO handling model
196abb0f93cSkardel  *
197abb0f93cSkardel  * Revision 4.0  1998/04/10 19:45:30  kardel
198abb0f93cSkardel  * Start 4.0 release version numbering
199abb0f93cSkardel  *
200abb0f93cSkardel  * from V3 3.5 log info deleted 1998/04/11 kardel
201abb0f93cSkardel  */
202