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