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