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