xref: /netbsd-src/external/bsd/ntp/dist/libparse/clk_dcf7000.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: clk_dcf7000.c,v 1.7 2024/08/18 20:47:17 christos Exp $	*/
2abb0f93cSkardel 
3abb0f93cSkardel /*
4abb0f93cSkardel  * /src/NTP/ntp4-dev/libparse/clk_dcf7000.c,v 4.10 2005/04/16 17:32:10 kardel RELEASE_20050508_A
5abb0f93cSkardel  *
6abb0f93cSkardel  * clk_dcf7000.c,v 4.10 2005/04/16 17:32:10 kardel RELEASE_20050508_A
7abb0f93cSkardel  *
8abb0f93cSkardel  * ELV DCF7000 module
9abb0f93cSkardel  *
10abb0f93cSkardel  * Copyright (c) 1995-2005 by Frank Kardel <kardel <AT> ntp.org>
117476e6e4Schristos  * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universitaet Erlangen-Nuernberg, Germany
12abb0f93cSkardel  *
13abb0f93cSkardel  * Redistribution and use in source and binary forms, with or without
14abb0f93cSkardel  * modification, are permitted provided that the following conditions
15abb0f93cSkardel  * are met:
16abb0f93cSkardel  * 1. Redistributions of source code must retain the above copyright
17abb0f93cSkardel  *    notice, this list of conditions and the following disclaimer.
18abb0f93cSkardel  * 2. Redistributions in binary form must reproduce the above copyright
19abb0f93cSkardel  *    notice, this list of conditions and the following disclaimer in the
20abb0f93cSkardel  *    documentation and/or other materials provided with the distribution.
21abb0f93cSkardel  * 3. Neither the name of the author nor the names of its contributors
22abb0f93cSkardel  *    may be used to endorse or promote products derived from this software
23abb0f93cSkardel  *    without specific prior written permission.
24abb0f93cSkardel  *
25abb0f93cSkardel  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26abb0f93cSkardel  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27abb0f93cSkardel  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28abb0f93cSkardel  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29abb0f93cSkardel  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30abb0f93cSkardel  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31abb0f93cSkardel  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32abb0f93cSkardel  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33abb0f93cSkardel  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34abb0f93cSkardel  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35abb0f93cSkardel  * SUCH DAMAGE.
36abb0f93cSkardel  *
37abb0f93cSkardel  */
38abb0f93cSkardel 
39abb0f93cSkardel #ifdef HAVE_CONFIG_H
40abb0f93cSkardel # include <config.h>
41abb0f93cSkardel #endif
42abb0f93cSkardel 
43abb0f93cSkardel #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_DCF7000)
44abb0f93cSkardel 
45abb0f93cSkardel #include "ntp_fp.h"
46abb0f93cSkardel #include "ntp_unixtime.h"
47abb0f93cSkardel #include "ntp_calendar.h"
48abb0f93cSkardel 
49abb0f93cSkardel #include "parse.h"
50abb0f93cSkardel 
51abb0f93cSkardel #ifndef PARSESTREAM
52abb0f93cSkardel #include "ntp_stdlib.h"
53abb0f93cSkardel #include <stdio.h>
54abb0f93cSkardel #else
55abb0f93cSkardel #include "sys/parsestreams.h"
56abb0f93cSkardel extern int printf (const char *, ...);
57abb0f93cSkardel #endif
58abb0f93cSkardel 
59abb0f93cSkardel static struct format dcf7000_fmt =
60abb0f93cSkardel {				/* ELV DCF7000 */
61abb0f93cSkardel 	{
62abb0f93cSkardel 		{  6, 2}, {  3, 2}, {  0, 2},
63abb0f93cSkardel 		{ 12, 2}, { 15, 2}, { 18, 2},
64abb0f93cSkardel 		{  9, 2}, { 21, 2},
65abb0f93cSkardel 	},
66abb0f93cSkardel 	(const unsigned char *)"  -  -  -  -  -  -  -  \r",
67abb0f93cSkardel 	0
68abb0f93cSkardel };
697476e6e4Schristos 
707476e6e4Schristos static parse_cvt_fnc_t cvt_dcf7000;
717476e6e4Schristos static parse_inp_fnc_t inp_dcf7000;
72abb0f93cSkardel 
73abb0f93cSkardel clockformat_t clock_dcf7000 =
74abb0f93cSkardel {
75abb0f93cSkardel   inp_dcf7000,			/* DCF7000 input handling */
76abb0f93cSkardel   cvt_dcf7000,			/* ELV DCF77 conversion */
77abb0f93cSkardel   0,				/* no direct PPS monitoring */
78abb0f93cSkardel   (void *)&dcf7000_fmt,		/* conversion configuration */
79abb0f93cSkardel   "ELV DCF7000",		/* ELV clock */
80abb0f93cSkardel   24,				/* string buffer */
817476e6e4Schristos   0				/* no private data (complete packets) */
82abb0f93cSkardel };
83abb0f93cSkardel 
84abb0f93cSkardel /*
857476e6e4Schristos  * parse_cvt_fnc_t cvt_dcf7000
86abb0f93cSkardel  *
87abb0f93cSkardel  * convert dcf7000 type format
88abb0f93cSkardel  */
89abb0f93cSkardel static u_long
90abb0f93cSkardel cvt_dcf7000(
91abb0f93cSkardel 	    unsigned char *buffer,
92abb0f93cSkardel 	    int            size,
93abb0f93cSkardel 	    struct format *format,
94abb0f93cSkardel 	    clocktime_t   *clock_time,
95abb0f93cSkardel 	    void          *local
96abb0f93cSkardel 	    )
97abb0f93cSkardel {
98abb0f93cSkardel 	if (!Strok(buffer, format->fixed_string))
99abb0f93cSkardel 	{
100abb0f93cSkardel 		return CVT_NONE;
101abb0f93cSkardel 	}
102abb0f93cSkardel 	else
103abb0f93cSkardel 	{
104abb0f93cSkardel 		if (Stoi(&buffer[format->field_offsets[O_DAY].offset], &clock_time->day,
105abb0f93cSkardel 			 format->field_offsets[O_DAY].length) ||
106abb0f93cSkardel 		    Stoi(&buffer[format->field_offsets[O_MONTH].offset], &clock_time->month,
107abb0f93cSkardel 			 format->field_offsets[O_MONTH].length) ||
108abb0f93cSkardel 		    Stoi(&buffer[format->field_offsets[O_YEAR].offset], &clock_time->year,
109abb0f93cSkardel 			 format->field_offsets[O_YEAR].length) ||
110abb0f93cSkardel 		    Stoi(&buffer[format->field_offsets[O_HOUR].offset], &clock_time->hour,
111abb0f93cSkardel 			 format->field_offsets[O_HOUR].length) ||
112abb0f93cSkardel 		    Stoi(&buffer[format->field_offsets[O_MIN].offset], &clock_time->minute,
113abb0f93cSkardel 			 format->field_offsets[O_MIN].length) ||
114abb0f93cSkardel 		    Stoi(&buffer[format->field_offsets[O_SEC].offset], &clock_time->second,
115abb0f93cSkardel 			 format->field_offsets[O_SEC].length))
116abb0f93cSkardel 		{
117abb0f93cSkardel 			return CVT_FAIL|CVT_BADFMT;
118abb0f93cSkardel 		}
119abb0f93cSkardel 		else
120abb0f93cSkardel 		{
121abb0f93cSkardel 			unsigned char *f = &buffer[format->field_offsets[O_FLAGS].offset];
122abb0f93cSkardel 			long flags;
123abb0f93cSkardel 
124abb0f93cSkardel 			clock_time->flags = 0;
125abb0f93cSkardel 			clock_time->usecond = 0;
126abb0f93cSkardel 
127abb0f93cSkardel 			if (Stoi(f, &flags, format->field_offsets[O_FLAGS].length))
128abb0f93cSkardel 			{
129abb0f93cSkardel 				return CVT_FAIL|CVT_BADFMT;
130abb0f93cSkardel 			}
131abb0f93cSkardel 			else
132abb0f93cSkardel 			{
133abb0f93cSkardel 				if (flags & 0x1)
134abb0f93cSkardel 				    clock_time->utcoffset = -2*60*60;
135abb0f93cSkardel 				else
136abb0f93cSkardel 				    clock_time->utcoffset = -1*60*60;
137abb0f93cSkardel 
138abb0f93cSkardel 				if (flags & 0x2)
139abb0f93cSkardel 				    clock_time->flags |= PARSEB_ANNOUNCE;
140abb0f93cSkardel 
141abb0f93cSkardel 				if (flags & 0x4)
142abb0f93cSkardel 				    clock_time->flags |= PARSEB_NOSYNC;
143abb0f93cSkardel 			}
144abb0f93cSkardel 			return CVT_OK;
145abb0f93cSkardel 		}
146abb0f93cSkardel 	}
147abb0f93cSkardel }
148abb0f93cSkardel 
149abb0f93cSkardel /*
1507476e6e4Schristos  * parse_inp_fnc_t inp_dcf700
151abb0f93cSkardel  *
1527476e6e4Schristos  * grab data from input stream
153abb0f93cSkardel  */
154abb0f93cSkardel static u_long
155abb0f93cSkardel inp_dcf7000(
156abb0f93cSkardel 	  parse_t      *parseio,
1577476e6e4Schristos 	  char         ch,
158abb0f93cSkardel 	  timestamp_t  *tstamp
159abb0f93cSkardel 	  )
160abb0f93cSkardel {
161abb0f93cSkardel 	unsigned int rtc;
162abb0f93cSkardel 
1638b8da087Schristos 	parseprintf(DD_PARSE, ("inp_dcf7000(0x%p, 0x%x, ...)\n", (void*)parseio, ch));
164abb0f93cSkardel 
165abb0f93cSkardel 	switch (ch)
166abb0f93cSkardel 	{
167abb0f93cSkardel 	case '\r':
168abb0f93cSkardel 		parseprintf(DD_PARSE, ("inp_dcf7000: EOL seen\n"));
169abb0f93cSkardel 		parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
170abb0f93cSkardel 		if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
171abb0f93cSkardel 			return parse_end(parseio);
172abb0f93cSkardel 		else
173abb0f93cSkardel 			return rtc;
174abb0f93cSkardel 
175abb0f93cSkardel 	default:
176abb0f93cSkardel 		return parse_addchar(parseio, ch);
177abb0f93cSkardel 	}
178abb0f93cSkardel }
179abb0f93cSkardel 
180abb0f93cSkardel #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_DCF7000) */
181*eabc0478Schristos NONEMPTY_TRANSLATION_UNIT
182abb0f93cSkardel #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_DCF7000) */
183abb0f93cSkardel 
184abb0f93cSkardel /*
185abb0f93cSkardel  * History:
186abb0f93cSkardel  *
187abb0f93cSkardel  * clk_dcf7000.c,v
188abb0f93cSkardel  * Revision 4.10  2005/04/16 17:32:10  kardel
189abb0f93cSkardel  * update copyright
190abb0f93cSkardel  *
191abb0f93cSkardel  * Revision 4.9  2004/11/14 15:29:41  kardel
192abb0f93cSkardel  * support PPSAPI, upgrade Copyright to Berkeley style
193abb0f93cSkardel  *
194abb0f93cSkardel  * Revision 4.6  1999/11/28 09:13:49  kardel
195abb0f93cSkardel  * RECON_4_0_98F
196abb0f93cSkardel  *
197abb0f93cSkardel  * Revision 4.5  1998/06/14 21:09:34  kardel
198abb0f93cSkardel  * Sun acc cleanup
199abb0f93cSkardel  *
200abb0f93cSkardel  * Revision 4.4  1998/06/13 12:01:59  kardel
201abb0f93cSkardel  * fix SYSV clock name clash
202abb0f93cSkardel  *
203abb0f93cSkardel  * Revision 4.3  1998/06/12 15:22:27  kardel
204abb0f93cSkardel  * fix prototypes
205abb0f93cSkardel  *
206abb0f93cSkardel  * Revision 4.2  1998/06/12 09:13:24  kardel
207abb0f93cSkardel  * conditional compile macros fixed
208abb0f93cSkardel  * printf prototype
209abb0f93cSkardel  *
210abb0f93cSkardel  * Revision 4.1  1998/05/24 09:39:51  kardel
211abb0f93cSkardel  * implementation of the new IO handling model
212abb0f93cSkardel  *
213abb0f93cSkardel  * Revision 4.0  1998/04/10 19:45:28  kardel
214abb0f93cSkardel  * Start 4.0 release version numbering
215abb0f93cSkardel  *
216abb0f93cSkardel  * from V3 3.18 log info deleted 1998/04/11 kardel
217abb0f93cSkardel  */
218