1142425Snectar/*	$NetBSD: clk_computime.c,v 1.6 2020/05/25 20:47:25 christos Exp $	*/
2142425Snectar
3142425Snectar#ifdef HAVE_CONFIG_H
4142425Snectar# include <config.h>
5142425Snectar#endif
6142425Snectar
7142425Snectar#if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_COMPUTIME)
8142425Snectar/*
9142425Snectar * /src/NTP/ntp4-dev/libparse/clk_computime.c,v 4.10 2005/04/16 17:32:10 kardel RELEASE_20050508_A
10142425Snectar *
11142425Snectar * clk_computime.c,v 4.10 2005/04/16 17:32:10 kardel RELEASE_20050508_A
12142425Snectar *
13142425Snectar * Supports Diem's Computime Radio Clock
14290207Sjkim *
15290207Sjkim * Used the Meinberg clock as a template for Diem's Computime Radio Clock
16142425Snectar *
17142425Snectar * adapted by Alois Camenzind <alois.camenzind@ubs.ch>
18142425Snectar *
19142425Snectar * Copyright (c) 1995-2005 by Frank Kardel <kardel <AT> ntp.org>
20142425Snectar * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universitaet Erlangen-Nuernberg, Germany
21142425Snectar *
22160814Ssimon * Redistribution and use in source and binary forms, with or without
23160814Ssimon * modification, are permitted provided that the following conditions
24238405Sjkim * are met:
25238405Sjkim * 1. Redistributions of source code must retain the above copyright
26238405Sjkim *    notice, this list of conditions and the following disclaimer.
27142425Snectar * 2. Redistributions in binary form must reproduce the above copyright
28160814Ssimon *    notice, this list of conditions and the following disclaimer in the
29160814Ssimon *    documentation and/or other materials provided with the distribution.
30238405Sjkim * 3. Neither the name of the author nor the names of its contributors
31238405Sjkim *    may be used to endorse or promote products derived from this software
32290207Sjkim *    without specific prior written permission.
33142425Snectar *
34142425Snectar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
35142425Snectar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36142425Snectar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37142425Snectar * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
38142425Snectar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39142425Snectar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40142425Snectar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41142425Snectar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42142425Snectar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43142425Snectar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44142425Snectar * SUCH DAMAGE.
45142425Snectar *
46142425Snectar */
47238405Sjkim
48142425Snectar#include "ntp_fp.h"
49142425Snectar#include "ntp_unixtime.h"
50142425Snectar#include "ntp_calendar.h"
51290207Sjkim#include "ntp_stdlib.h"
52290207Sjkim
53290207Sjkim#include "parse.h"
54290207Sjkim
55290207Sjkim#ifndef PARSESTREAM
56290207Sjkim#include <stdio.h>
57142425Snectar#else
58142425Snectar#include "sys/parsestreams.h"
59142425Snectarextern int printf (const char *, ...);
60142425Snectar#endif
61142425Snectar
62142425Snectar/*
63142425Snectar * The Computime receiver sends a datagram in the following format every minute
64142425Snectar *
65142425Snectar * Timestamp	T:YY:MM:MD:WD:HH:MM:SSCRLF
66160814Ssimon * Pos          0123456789012345678901 2 3
67160814Ssimon *              0000000000111111111122 2 2
68142425Snectar * Parse        T:  :  :  :  :  :  :  rn
69142425Snectar *
70142425Snectar * T	Startcharacter "T" specifies start of the timestamp
71142425Snectar * YY	Year MM	Month 1-12
72142425Snectar * MD	Day of the month
73142425Snectar * WD	Day of week
74142425Snectar * HH	Hour
75142425Snectar * MM   Minute
76142425Snectar * SS   Second
77142425Snectar * CR   Carriage return
78142425Snectar * LF   Linefeed
79142425Snectar *
80142425Snectar */
81284283Sjkim
82284283Sjkimstatic struct format computime_fmt =
83142425Snectar{
84160814Ssimon	{
85142425Snectar		{8, 2},  {5,  2}, {2,  2},	/* day, month, year */
86142425Snectar		{14, 2}, {17, 2}, {20, 2},	/* hour, minute, second */
87142425Snectar		{11, 2},                        /* dayofweek,  */
88142425Snectar	},
89142425Snectar	(const unsigned char *)"T:  :  :  :  :  :  :  \r\n",
90142425Snectar	0
91142425Snectar};
92291719Sjkim
93142425Snectarstatic parse_cvt_fnc_t cvt_computime;
94142425Snectarstatic parse_inp_fnc_t inp_computime;
95142425Snectar
96160814Ssimonclockformat_t clock_computime =
97160814Ssimon{
98160814Ssimon	inp_computime,		/* Computime input handling */
99160814Ssimon	cvt_computime,		/* Computime conversion */
100160814Ssimon	0,			/* no PPS monitoring */
101160814Ssimon	(void *)&computime_fmt,	/* conversion configuration */
102160814Ssimon	"Diem's Computime Radio Clock",	/* Computime Radio Clock */
103160814Ssimon	24,			/* string buffer */
104238405Sjkim	0			/* no private data (complete packets) */
105238405Sjkim};
106238405Sjkim
107238405Sjkim/*
108238405Sjkim * parse_cvt_fnc_t cvt_computime
109238405Sjkim *
110238405Sjkim * convert simple type format
111238405Sjkim */
112160814Ssimonstatic u_long
113160814Ssimoncvt_computime(
114160814Ssimon	unsigned char *buffer,
115160814Ssimon	int            size,
116160814Ssimon	struct format *format,
117160814Ssimon	clocktime_t   *clock_time,
118160814Ssimon	void          *local
119238405Sjkim	)
120238405Sjkim{
121290207Sjkim
122290207Sjkim	if (!Strok(buffer, format->fixed_string)) {
123290207Sjkim		return CVT_NONE;
124290207Sjkim	} else {
125290207Sjkim		if (Stoi(&buffer[format->field_offsets[O_DAY].offset], &clock_time->day,
126290207Sjkim			 format->field_offsets[O_DAY].length) ||
127290207Sjkim		    Stoi(&buffer[format->field_offsets[O_MONTH].offset], &clock_time->month,
128290207Sjkim			 format->field_offsets[O_MONTH].length) ||
129238405Sjkim		    Stoi(&buffer[format->field_offsets[O_YEAR].offset], &clock_time->year,
130238405Sjkim			 format->field_offsets[O_YEAR].length) ||
131238405Sjkim		    Stoi(&buffer[format->field_offsets[O_HOUR].offset], &clock_time->hour,
132238405Sjkim			 format->field_offsets[O_HOUR].length) ||
133238405Sjkim		    Stoi(&buffer[format->field_offsets[O_MIN].offset], &clock_time->minute,
134306195Sjkim			 format->field_offsets[O_MIN].length) ||
135160814Ssimon		    Stoi(&buffer[format->field_offsets[O_SEC].offset], &clock_time->second,
136160814Ssimon			 format->field_offsets[O_SEC].length)) {
137160814Ssimon			return CVT_FAIL | CVT_BADFMT;
138160814Ssimon		} else {
139160814Ssimon
140160814Ssimon			clock_time->flags = 0;
141160814Ssimon			clock_time->utcoffset = 0;	/* We have UTC time */
142160814Ssimon
143160814Ssimon			return CVT_OK;
144160814Ssimon		}
145160814Ssimon	}
146160814Ssimon}
147160814Ssimon
148160814Ssimon/*
149160814Ssimon * parse_inp_fnc_t inp_computime
150160814Ssimon *
151160814Ssimon * grab data from input stream
152160814Ssimon */
153160814Ssimonstatic u_long
154160814Ssimoninp_computime(
155160814Ssimon	      parse_t      *parseio,
156160814Ssimon	      char         ch,
157160814Ssimon	      timestamp_t  *tstamp
158160814Ssimon	      )
159352193Sjkim{
160160814Ssimon	unsigned int rtc;
161160814Ssimon
162160814Ssimon	parseprintf(DD_PARSE, ("inp_computime(0x%p, 0x%x, ...)\n", (void*)parseio, ch));
163160814Ssimon
164160814Ssimon	switch (ch)
165160814Ssimon	{
166160814Ssimon	case 'T':
167142425Snectar		parseprintf(DD_PARSE, ("inp_computime: START seen\n"));
168160814Ssimon
169142425Snectar		parseio->parse_index = 1;
170142425Snectar		parseio->parse_data[0] = ch;
171142425Snectar		parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
172160814Ssimon		return PARSE_INP_SKIP;
173160814Ssimon
174160814Ssimon	case '\n':
175160814Ssimon		parseprintf(DD_PARSE, ("inp_computime: END seen\n"));
176160814Ssimon		if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
177160814Ssimon			return parse_end(parseio);
178160814Ssimon		else
179160814Ssimon			return rtc;
180160814Ssimon
181160814Ssimon	default:
182160814Ssimon		return parse_addchar(parseio, ch);
183160814Ssimon	}
184160814Ssimon}
185160814Ssimon
186160814Ssimon#else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_COMPUTIME) */
187160814Ssimonint clk_computime_bs;
188160814Ssimon#endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_COMPUTIME) */
189160814Ssimon
190160814Ssimon/*
191160814Ssimon * clk_computime.c,v
192160814Ssimon * Revision 4.10  2005/04/16 17:32:10  kardel
193160814Ssimon * update copyright
194160814Ssimon *
195160814Ssimon * Revision 4.9  2004/11/14 15:29:41  kardel
196160814Ssimon * support PPSAPI, upgrade Copyright to Berkeley style
197160814Ssimon *
198160814Ssimon * Revision 4.6  1999/11/28 09:13:49  kardel
199238405Sjkim * RECON_4_0_98F
200238405Sjkim *
201238405Sjkim * Revision 4.5  1998/06/14 21:09:34  kardel
202238405Sjkim * Sun acc cleanup
203238405Sjkim *
204238405Sjkim * Revision 4.4  1998/06/13 12:00:38  kardel
205238405Sjkim * fix SYSV clock name clash
206238405Sjkim *
207238405Sjkim * Revision 4.3  1998/06/12 15:22:26  kardel
208238405Sjkim * fix prototypes
209290207Sjkim *
210290207Sjkim * Revision 4.2  1998/06/12 09:13:24  kardel
211290207Sjkim * conditional compile macros fixed
212290207Sjkim * printf prototype
213290207Sjkim *
214290207Sjkim * Revision 4.1  1998/05/24 09:39:51  kardel
215290207Sjkim * implementation of the new IO handling model
216238405Sjkim *
217238405Sjkim * Revision 4.0  1998/04/10 19:45:27  kardel
218238405Sjkim * Start 4.0 release version numbering
219238405Sjkim *
220238405Sjkim * from V3 1.8 log info deleted 1998/04/11 kardel
221290207Sjkim */
222160814Ssimon