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