xref: /freebsd/contrib/ntp/libparse/clk_hopf6021.c (revision 2be1a816)
1 /*
2  * /src/NTP/ntp-4/libparse/clk_hopf6021.c,v 4.7 1999/11/28 09:13:49 kardel RELEASE_19991128_A
3  *
4  * clk_hopf6021.c,v 4.7 1999/11/28 09:13:49 kardel RELEASE_19991128_A
5  *
6  * Radiocode Clocks HOPF Funkuhr 6021 mit serieller Schnittstelle
7  * base code version from 24th Nov 1995 - history at end
8  *
9  * Created by F.Schnekenbuehl <frank@comsys.dofn.de> from clk_rcc8000.c
10  * Nortel DASA Network Systems GmbH, Department: ND250
11  * A Joint venture of Daimler-Benz Aerospace and Nortel
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 
23 #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_HOPF6021)
24 
25 #include "ntp_fp.h"
26 #include "ntp_unixtime.h"
27 #include "ntp_calendar.h"
28 #include "ascii.h"
29 
30 #include "parse.h"
31 
32 #ifndef PARSESTREAM
33 #include "ntp_stdlib.h"
34 #include <stdio.h>
35 #else
36 #include "sys/parsestreams.h"
37 extern void printf P((const char *, ...));
38 #endif
39 
40 /*
41  * hopf Funkuhr 6021
42  *      used with 9600,8N1,
43  *      UTC ueber serielle Schnittstelle
44  *      Sekundenvorlauf ON
45  *      ETX zum Sekundenvorlauf ON
46  *      Datenstring 6021
47  *      Ausgabe Uhrzeit und Datum
48  *      Senden mit Steuerzeichen
49  *      Senden sekuendlich
50  */
51 
52 /*
53  *  Type 6021 Serial Output format
54  *
55  *      000000000011111111 / char
56  *      012345678901234567 \ position
57  *      sABHHMMSSDDMMYYnre  Actual
58  *       C4110046231195     Parse
59  *      s              enr  Check
60  *
61  *  s = STX (0x02), e = ETX (0x03)
62  *  n = NL  (0x0A), r = CR  (0x0D)
63  *
64  *  A B - Status and weekday
65  *
66  *  A - Status
67  *
68  *      8 4 2 1
69  *      x x x 0  - no announcement
70  *      x x x 1  - Summertime - wintertime - summertime announcement
71  *      x x 0 x  - Wintertime
72  *      x x 1 x  - Summertime
73  *      0 0 x x  - Time/Date invalid
74  *      0 1 x x  - Internal clock used
75  *      1 0 x x  - Radio clock
76  *      1 1 x x  - Radio clock highprecision
77  *
78  *  B - 8 4 2 1
79  *      0 x x x  - MESZ/MEZ
80  *      1 x x x  - UTC
81  *      x 0 0 1  - Monday
82  *      x 0 1 0  - Tuesday
83  *      x 0 1 1  - Wednesday
84  *      x 1 0 0  - Thursday
85  *      x 1 0 1  - Friday
86  *      x 1 1 0  - Saturday
87  *      x 1 1 1  - Sunday
88  */
89 
90 #define HOPF_DSTWARN	0x01	/* DST switch warning */
91 #define HOPF_DST	0x02	/* DST in effect */
92 
93 #define HOPF_MODE	0x0C	/* operation mode mask */
94 #define  HOPF_INVALID	0x00	/* no time code available */
95 #define  HOPF_INTERNAL	0x04	/* internal clock */
96 #define  HOPF_RADIO	0x08	/* radio clock */
97 #define  HOPF_RADIOHP	0x0C	/* high precision radio clock */
98 
99 #define HOPF_UTC	0x08	/* time code in UTC */
100 #define HOPF_WMASK	0x07	/* mask for weekday code */
101 
102 static struct format hopf6021_fmt =
103 {
104 	{
105 		{  9, 2 }, {11, 2}, { 13, 2}, /* Day, Month, Year */
106 		{  3, 2 }, { 5, 2}, {  7, 2}, /* Hour, Minute, Second */
107 		{  2, 1 }, { 1, 1}, {  0, 0}, /* Weekday, Flags, Zone */
108 		/* ... */
109 	},
110 	(const unsigned char *)"\002              \n\r\003",
111 	0
112 };
113 
114 #define OFFS(x) format->field_offsets[(x)].offset
115 #define STOI(x, y) Stoi(&buffer[OFFS(x)], y, format->field_offsets[(x)].length)
116 #define hexval(x) (('0' <= (x) && (x) <= '9') ? (x) - '0' : \
117 		   ('a' <= (x) && (x) <= 'f') ? (x) - 'a' + 10 : \
118 		   ('A' <= (x) && (x) <= 'F') ? (x) - 'A' + 10 : \
119 		   -1)
120 
121 static unsigned long cvt_hopf6021 P((unsigned char *, int, struct format *, clocktime_t *, void *));
122 static unsigned long inp_hopf6021 P((parse_t *, unsigned int, timestamp_t *));
123 
124 clockformat_t clock_hopf6021 =
125 {
126   inp_hopf6021,			/* HOPF 6021 input handling */
127   cvt_hopf6021,                 /* Radiocode clock conversion */
128   0,				/* no direct PPS monitoring */
129   (void *)&hopf6021_fmt,        /* conversion configuration */
130   "hopf Funkuhr 6021",          /* clock format name */
131   19,                           /* string buffer */
132   0                            /* private data length, no private data */
133 };
134 
135 static unsigned long
136 cvt_hopf6021(
137 	     unsigned char *buffer,
138 	     int            size,
139 	     struct format *format,
140 	     clocktime_t   *clock_time,
141 	     void          *local
142 	     )
143 {
144 	unsigned char status,weekday;
145 
146 	if (!Strok(buffer, format->fixed_string))
147 	{
148 		return CVT_NONE;
149 	}
150 
151 	if (  STOI(O_DAY,   &clock_time->day)    ||
152 	      STOI(O_MONTH, &clock_time->month)  ||
153 	      STOI(O_YEAR,  &clock_time->year)   ||
154 	      STOI(O_HOUR,  &clock_time->hour)   ||
155 	      STOI(O_MIN,   &clock_time->minute) ||
156 	      STOI(O_SEC,   &clock_time->second)
157 	      )
158 	{
159 		return CVT_FAIL|CVT_BADFMT;
160 	}
161 
162 	clock_time->usecond   = 0;
163 	clock_time->utcoffset = 0;
164 
165 	status = hexval(buffer[OFFS(O_FLAGS)]);
166 	weekday= hexval(buffer[OFFS(O_WDAY)]);
167 
168 	if ((status == 0xFF) || (weekday == 0xFF))
169 	{
170 		return CVT_FAIL|CVT_BADFMT;
171 	}
172 
173 	clock_time->flags  = 0;
174 
175 	if (weekday & HOPF_UTC)
176 	{
177 		clock_time->flags |= PARSEB_UTC;
178 	}
179 	else
180 	{
181 		if (status & HOPF_DST)
182 		{
183 			clock_time->flags     |= PARSEB_DST;
184 			clock_time->utcoffset  = -2*60*60; /* MET DST */
185 		}
186 		else
187 		{
188 			clock_time->utcoffset  = -1*60*60; /* MET */
189 		}
190 	}
191 
192 	clock_time->flags |= (status & HOPF_DSTWARN)  ? PARSEB_ANNOUNCE : 0;
193 
194 	switch (status & HOPF_MODE)
195 	{
196 	    case HOPF_INVALID:  /* Time/Date invalid */
197 		clock_time->flags |= PARSEB_POWERUP;
198 		break;
199 
200 	    case HOPF_INTERNAL: /* internal clock */
201 		clock_time->flags |= PARSEB_NOSYNC;
202 		break;
203 
204 	    case HOPF_RADIO:    /* Radio clock */
205 	    case HOPF_RADIOHP:  /* Radio clock high precision */
206 		break;
207 
208 	    default:
209 		return CVT_FAIL|CVT_BADFMT;
210 	}
211 
212 	return CVT_OK;
213 }
214 
215 /*
216  * inp_hopf6021
217  *
218  * grep data from input stream
219  */
220 static u_long
221 inp_hopf6021(
222 	     parse_t      *parseio,
223 	     unsigned int  ch,
224 	     timestamp_t  *tstamp
225 	  )
226 {
227 	unsigned int rtc;
228 
229 	parseprintf(DD_PARSE, ("inp_hopf6021(0x%lx, 0x%x, ...)\n", (long)parseio, ch));
230 
231 	switch (ch)
232 	{
233 	case ETX:
234 		parseprintf(DD_PARSE, ("inp_hopf6021: EOL seen\n"));
235 		parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
236 		if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
237 			return parse_end(parseio);
238 		else
239 			return rtc;
240 
241 	default:
242 		return parse_addchar(parseio, ch);
243 	}
244 }
245 
246 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_HOPF6021) */
247 int clk_hopf6021_bs;
248 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_HOPF6021) */
249 
250 /*
251  * History:
252  *
253  * clk_hopf6021.c,v
254  * Revision 4.7  1999/11/28 09:13:49  kardel
255  * RECON_4_0_98F
256  *
257  * Revision 4.6  1998/11/15 20:27:57  kardel
258  * Release 4.0.73e13 reconcilation
259  *
260  * Revision 4.5  1998/06/14 21:09:35  kardel
261  * Sun acc cleanup
262  *
263  * Revision 4.4  1998/06/13 12:02:38  kardel
264  * fix SYSV clock name clash
265  *
266  * Revision 4.3  1998/06/12 15:22:27  kardel
267  * fix prototypes
268  *
269  * Revision 4.2  1998/06/12 09:13:25  kardel
270  * conditional compile macros fixed
271  * printf prototype
272  *
273  * Revision 4.1  1998/05/24 09:39:52  kardel
274  * implementation of the new IO handling model
275  *
276  * Revision 4.0  1998/04/10 19:45:29  kardel
277  * Start 4.0 release version numbering
278  *
279  * from V3 3.6 log info deleted 1998/04/11 kardel
280  */
281