xref: /freebsd/contrib/ntp/libparse/clk_dcf7000.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /*
2  * /src/NTP/ntp-4/libparse/clk_dcf7000.c,v 4.6 1999/11/28 09:13:49 kardel RELEASE_19991128_A
3  *
4  * clk_dcf7000.c,v 4.6 1999/11/28 09:13:49 kardel RELEASE_19991128_A
5  *
6  * ELV DCF7000 module
7  *
8  * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998 by Frank Kardel
9  * Friedrich-Alexander Universit�t Erlangen-N�rnberg, Germany
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  *
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 
21 #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_DCF7000)
22 
23 #include <sys/types.h>
24 #include <sys/time.h>
25 
26 #include "ntp_fp.h"
27 #include "ntp_unixtime.h"
28 #include "ntp_calendar.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 static struct format dcf7000_fmt =
41 {				/* ELV DCF7000 */
42 	{
43 		{  6, 2}, {  3, 2}, {  0, 2},
44 		{ 12, 2}, { 15, 2}, { 18, 2},
45 		{  9, 2}, { 21, 2},
46 	},
47 	(const unsigned char *)"  -  -  -  -  -  -  -  \r",
48 	0
49 };
50 static u_long cvt_dcf7000 P((unsigned char *, int, struct format *, clocktime_t *, void *));
51 static unsigned long inp_dcf7000 P((parse_t *, unsigned int, timestamp_t *));
52 
53 clockformat_t clock_dcf7000 =
54 {
55   inp_dcf7000,			/* DCF7000 input handling */
56   cvt_dcf7000,			/* ELV DCF77 conversion */
57   0,				/* no direct PPS monitoring */
58   (void *)&dcf7000_fmt,		/* conversion configuration */
59   "ELV DCF7000",		/* ELV clock */
60   24,				/* string buffer */
61   0				/* no private data (complete pakets) */
62 };
63 
64 /*
65  * cvt_dcf7000
66  *
67  * convert dcf7000 type format
68  */
69 static u_long
70 cvt_dcf7000(
71 	    unsigned char *buffer,
72 	    int            size,
73 	    struct format *format,
74 	    clocktime_t   *clock_time,
75 	    void          *local
76 	    )
77 {
78 	if (!Strok(buffer, format->fixed_string))
79 	{
80 		return CVT_NONE;
81 	}
82 	else
83 	{
84 		if (Stoi(&buffer[format->field_offsets[O_DAY].offset], &clock_time->day,
85 			 format->field_offsets[O_DAY].length) ||
86 		    Stoi(&buffer[format->field_offsets[O_MONTH].offset], &clock_time->month,
87 			 format->field_offsets[O_MONTH].length) ||
88 		    Stoi(&buffer[format->field_offsets[O_YEAR].offset], &clock_time->year,
89 			 format->field_offsets[O_YEAR].length) ||
90 		    Stoi(&buffer[format->field_offsets[O_HOUR].offset], &clock_time->hour,
91 			 format->field_offsets[O_HOUR].length) ||
92 		    Stoi(&buffer[format->field_offsets[O_MIN].offset], &clock_time->minute,
93 			 format->field_offsets[O_MIN].length) ||
94 		    Stoi(&buffer[format->field_offsets[O_SEC].offset], &clock_time->second,
95 			 format->field_offsets[O_SEC].length))
96 		{
97 			return CVT_FAIL|CVT_BADFMT;
98 		}
99 		else
100 		{
101 			unsigned char *f = &buffer[format->field_offsets[O_FLAGS].offset];
102 			long flags;
103 
104 			clock_time->flags = 0;
105 			clock_time->usecond = 0;
106 
107 			if (Stoi(f, &flags, format->field_offsets[O_FLAGS].length))
108 			{
109 				return CVT_FAIL|CVT_BADFMT;
110 			}
111 			else
112 			{
113 				if (flags & 0x1)
114 				    clock_time->utcoffset = -2*60*60;
115 				else
116 				    clock_time->utcoffset = -1*60*60;
117 
118 				if (flags & 0x2)
119 				    clock_time->flags |= PARSEB_ANNOUNCE;
120 
121 				if (flags & 0x4)
122 				    clock_time->flags |= PARSEB_NOSYNC;
123 			}
124 			return CVT_OK;
125 		}
126 	}
127 }
128 
129 /*
130  * inp_dcf700
131  *
132  * grep data from input stream
133  */
134 static u_long
135 inp_dcf7000(
136 	  parse_t      *parseio,
137 	  unsigned int  ch,
138 	  timestamp_t  *tstamp
139 	  )
140 {
141 	unsigned int rtc;
142 
143 	parseprintf(DD_PARSE, ("inp_dcf7000(0x%x, 0x%x, ...)\n", (int)parseio, (int)ch));
144 
145 	switch (ch)
146 	{
147 	case '\r':
148 		parseprintf(DD_PARSE, ("inp_dcf7000: EOL seen\n"));
149 		parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
150 		if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
151 			return parse_end(parseio);
152 		else
153 			return rtc;
154 
155 	default:
156 		return parse_addchar(parseio, ch);
157 	}
158 }
159 
160 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_DCF7000) */
161 int clk_dcf7000_bs;
162 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_DCF7000) */
163 
164 /*
165  * History:
166  *
167  * clk_dcf7000.c,v
168  * Revision 4.6  1999/11/28 09:13:49  kardel
169  * RECON_4_0_98F
170  *
171  * Revision 4.5  1998/06/14 21:09:34  kardel
172  * Sun acc cleanup
173  *
174  * Revision 4.4  1998/06/13 12:01:59  kardel
175  * fix SYSV clock name clash
176  *
177  * Revision 4.3  1998/06/12 15:22:27  kardel
178  * fix prototypes
179  *
180  * Revision 4.2  1998/06/12 09:13:24  kardel
181  * conditional compile macros fixed
182  * printf prototype
183  *
184  * Revision 4.1  1998/05/24 09:39:51  kardel
185  * implementation of the new IO handling model
186  *
187  * Revision 4.0  1998/04/10 19:45:28  kardel
188  * Start 4.0 release version numbering
189  *
190  * from V3 3.18 log info deleted 1998/04/11 kardel
191  */
192