1c0b746e5SOllivier Robert /*
2ea906c41SOllivier Robert * /src/NTP/ntp4-dev/libparse/clk_schmid.c,v 4.9 2005/04/16 17:32:10 kardel RELEASE_20050508_A
3c0b746e5SOllivier Robert *
4ea906c41SOllivier Robert * clk_schmid.c,v 4.9 2005/04/16 17:32:10 kardel RELEASE_20050508_A
5c0b746e5SOllivier Robert *
6c0b746e5SOllivier Robert * Schmid clock support
7ea906c41SOllivier Robert * based on information and testing from Adam W. Feigin et. al (Swisstime iis.ethz.ch)
8c0b746e5SOllivier Robert *
9276da39aSCy Schubert * Copyright (c) 1995-2015 by Frank Kardel <kardel <AT> ntp.org>
10a25439b6SCy Schubert * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universitaet Erlangen-Nuernberg, Germany
11c0b746e5SOllivier Robert *
12ea906c41SOllivier Robert * Redistribution and use in source and binary forms, with or without
13ea906c41SOllivier Robert * modification, are permitted provided that the following conditions
14ea906c41SOllivier Robert * are met:
15ea906c41SOllivier Robert * 1. Redistributions of source code must retain the above copyright
16ea906c41SOllivier Robert * notice, this list of conditions and the following disclaimer.
17ea906c41SOllivier Robert * 2. Redistributions in binary form must reproduce the above copyright
18ea906c41SOllivier Robert * notice, this list of conditions and the following disclaimer in the
19ea906c41SOllivier Robert * documentation and/or other materials provided with the distribution.
20ea906c41SOllivier Robert * 3. Neither the name of the author nor the names of its contributors
21ea906c41SOllivier Robert * may be used to endorse or promote products derived from this software
22ea906c41SOllivier Robert * without specific prior written permission.
23ea906c41SOllivier Robert *
24ea906c41SOllivier Robert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25ea906c41SOllivier Robert * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26ea906c41SOllivier Robert * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27ea906c41SOllivier Robert * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28ea906c41SOllivier Robert * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29ea906c41SOllivier Robert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30ea906c41SOllivier Robert * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31ea906c41SOllivier Robert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32ea906c41SOllivier Robert * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33ea906c41SOllivier Robert * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34ea906c41SOllivier Robert * SUCH DAMAGE.
35c0b746e5SOllivier Robert *
36c0b746e5SOllivier Robert */
37c0b746e5SOllivier Robert
38c0b746e5SOllivier Robert #if HAVE_CONFIG_H
39c0b746e5SOllivier Robert # include <config.h>
40c0b746e5SOllivier Robert #endif
41c0b746e5SOllivier Robert
42c0b746e5SOllivier Robert #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_SCHMID)
43c0b746e5SOllivier Robert
44c0b746e5SOllivier Robert #include "ntp_fp.h"
45c0b746e5SOllivier Robert #include "ntp_unixtime.h"
46c0b746e5SOllivier Robert #include "ntp_calendar.h"
47c0b746e5SOllivier Robert
48c0b746e5SOllivier Robert #include "parse.h"
49c0b746e5SOllivier Robert
50c0b746e5SOllivier Robert #ifndef PARSESTREAM
51c0b746e5SOllivier Robert #include "ntp_stdlib.h"
52c0b746e5SOllivier Robert #include <stdio.h>
53c0b746e5SOllivier Robert #else
54c0b746e5SOllivier Robert #include "sys/parsestreams.h"
552b15cb3dSCy Schubert extern int printf (const char *, ...);
56c0b746e5SOllivier Robert #endif
57c0b746e5SOllivier Robert
58c0b746e5SOllivier Robert /*
59c0b746e5SOllivier Robert * Description courtesy of Adam W. Feigin et. al (Swisstime iis.ethz.ch)
60c0b746e5SOllivier Robert *
61c0b746e5SOllivier Robert * The command to Schmid's DCF77 clock is a single byte; each bit
62c0b746e5SOllivier Robert * allows the user to select some part of the time string, as follows (the
63c0b746e5SOllivier Robert * output for the lsb is sent first).
64c0b746e5SOllivier Robert *
65c0b746e5SOllivier Robert * Bit 0: time in MEZ, 4 bytes *binary, not BCD*; hh.mm.ss.tenths
66c0b746e5SOllivier Robert * Bit 1: date 3 bytes *binary, not BCD: dd.mm.yy
67c0b746e5SOllivier Robert * Bit 2: week day, 1 byte (unused here)
68c0b746e5SOllivier Robert * Bit 3: time zone, 1 byte, 0=MET, 1=MEST. (unused here)
69c0b746e5SOllivier Robert * Bit 4: clock status, 1 byte, 0=time invalid,
70c0b746e5SOllivier Robert * 1=time from crystal backup,
71c0b746e5SOllivier Robert * 3=time from DCF77
72c0b746e5SOllivier Robert * Bit 5: transmitter status, 1 byte,
73c0b746e5SOllivier Robert * bit 0: backup antenna
74c0b746e5SOllivier Robert * bit 1: time zone change within 1h
75c0b746e5SOllivier Robert * bit 3,2: TZ 01=MEST, 10=MET
76c0b746e5SOllivier Robert * bit 4: leap second will be
77c0b746e5SOllivier Robert * added within one hour
78c0b746e5SOllivier Robert * bits 5-7: Zero
79c0b746e5SOllivier Robert * Bit 6: time in backup mode, units of 5 minutes (unused here)
80c0b746e5SOllivier Robert *
81c0b746e5SOllivier Robert */
82c0b746e5SOllivier Robert #define WS_TIME 0x01
83c0b746e5SOllivier Robert #define WS_SIGNAL 0x02
84c0b746e5SOllivier Robert
85a25439b6SCy Schubert #define WS_CALLBIT 0x01 /* "call bit" used to signalize irregularities in the control facilities */
86c0b746e5SOllivier Robert #define WS_ANNOUNCE 0x02
87c0b746e5SOllivier Robert #define WS_TZ 0x0c
88c0b746e5SOllivier Robert #define WS_MET 0x08
89c0b746e5SOllivier Robert #define WS_MEST 0x04
90c0b746e5SOllivier Robert #define WS_LEAP 0x10
91c0b746e5SOllivier Robert
92a25439b6SCy Schubert static parse_cvt_fnc_t cvt_schmid;
93a25439b6SCy Schubert static parse_inp_fnc_t inp_schmid;
94c0b746e5SOllivier Robert
95c0b746e5SOllivier Robert clockformat_t clock_schmid =
96c0b746e5SOllivier Robert {
97c0b746e5SOllivier Robert inp_schmid, /* no input handling */
98c0b746e5SOllivier Robert cvt_schmid, /* Schmid conversion */
99c0b746e5SOllivier Robert 0, /* not direct PPS monitoring */
100c0b746e5SOllivier Robert 0, /* conversion configuration */
101c0b746e5SOllivier Robert "Schmid", /* Schmid receiver */
102c0b746e5SOllivier Robert 12, /* binary data buffer */
103c0b746e5SOllivier Robert 0, /* no private data (complete messages) */
104c0b746e5SOllivier Robert };
105c0b746e5SOllivier Robert
106a25439b6SCy Schubert /* parse_cvt_fnc_t */
107c0b746e5SOllivier Robert static u_long
cvt_schmid(unsigned char * buffer,int size,struct format * format,clocktime_t * clock_time,void * local)108c0b746e5SOllivier Robert cvt_schmid(
109c0b746e5SOllivier Robert unsigned char *buffer,
110c0b746e5SOllivier Robert int size,
111c0b746e5SOllivier Robert struct format *format,
112c0b746e5SOllivier Robert clocktime_t *clock_time,
113c0b746e5SOllivier Robert void *local
114c0b746e5SOllivier Robert )
115c0b746e5SOllivier Robert {
116c0b746e5SOllivier Robert if ((size != 11) || (buffer[10] != (unsigned char)'\375'))
117c0b746e5SOllivier Robert {
118c0b746e5SOllivier Robert return CVT_NONE;
119c0b746e5SOllivier Robert }
120c0b746e5SOllivier Robert else
121c0b746e5SOllivier Robert {
122c0b746e5SOllivier Robert if (buffer[0] > 23 || buffer[1] > 59 || buffer[2] > 59 || buffer[3] > 9) /* Time */
123c0b746e5SOllivier Robert {
124c0b746e5SOllivier Robert return CVT_FAIL|CVT_BADTIME;
125c0b746e5SOllivier Robert }
126c0b746e5SOllivier Robert else
127c0b746e5SOllivier Robert if (buffer[4] < 1 || buffer[4] > 31 || buffer[5] < 1 || buffer[5] > 12
128c0b746e5SOllivier Robert || buffer[6] > 99)
129c0b746e5SOllivier Robert {
130c0b746e5SOllivier Robert return CVT_FAIL|CVT_BADDATE;
131c0b746e5SOllivier Robert }
132c0b746e5SOllivier Robert else
133c0b746e5SOllivier Robert {
134c0b746e5SOllivier Robert clock_time->hour = buffer[0];
135c0b746e5SOllivier Robert clock_time->minute = buffer[1];
136c0b746e5SOllivier Robert clock_time->second = buffer[2];
137c0b746e5SOllivier Robert clock_time->usecond = buffer[3] * 100000;
138c0b746e5SOllivier Robert clock_time->day = buffer[4];
139c0b746e5SOllivier Robert clock_time->month = buffer[5];
140c0b746e5SOllivier Robert clock_time->year = buffer[6];
141c0b746e5SOllivier Robert
142c0b746e5SOllivier Robert clock_time->flags = 0;
143c0b746e5SOllivier Robert
144c0b746e5SOllivier Robert switch (buffer[8] & WS_TZ)
145c0b746e5SOllivier Robert {
146c0b746e5SOllivier Robert case WS_MET:
147c0b746e5SOllivier Robert clock_time->utcoffset = -1*60*60;
148c0b746e5SOllivier Robert break;
149c0b746e5SOllivier Robert
150c0b746e5SOllivier Robert case WS_MEST:
151c0b746e5SOllivier Robert clock_time->utcoffset = -2*60*60;
152c0b746e5SOllivier Robert clock_time->flags |= PARSEB_DST;
153c0b746e5SOllivier Robert break;
154c0b746e5SOllivier Robert
155c0b746e5SOllivier Robert default:
156c0b746e5SOllivier Robert return CVT_FAIL|CVT_BADFMT;
157c0b746e5SOllivier Robert }
158c0b746e5SOllivier Robert
159c0b746e5SOllivier Robert if (!(buffer[7] & WS_TIME))
160c0b746e5SOllivier Robert {
161c0b746e5SOllivier Robert clock_time->flags |= PARSEB_POWERUP;
162c0b746e5SOllivier Robert }
163c0b746e5SOllivier Robert
164c0b746e5SOllivier Robert if (!(buffer[7] & WS_SIGNAL))
165c0b746e5SOllivier Robert {
166c0b746e5SOllivier Robert clock_time->flags |= PARSEB_NOSYNC;
167c0b746e5SOllivier Robert }
168c0b746e5SOllivier Robert
169c0b746e5SOllivier Robert if (buffer[7] & WS_SIGNAL)
170c0b746e5SOllivier Robert {
171a25439b6SCy Schubert if (buffer[8] & WS_CALLBIT)
172c0b746e5SOllivier Robert {
173a25439b6SCy Schubert clock_time->flags |= PARSEB_CALLBIT;
174c0b746e5SOllivier Robert }
175c0b746e5SOllivier Robert
176c0b746e5SOllivier Robert if (buffer[8] & WS_ANNOUNCE)
177c0b746e5SOllivier Robert {
178c0b746e5SOllivier Robert clock_time->flags |= PARSEB_ANNOUNCE;
179c0b746e5SOllivier Robert }
180c0b746e5SOllivier Robert
181c0b746e5SOllivier Robert if (buffer[8] & WS_LEAP)
182c0b746e5SOllivier Robert {
183c0b746e5SOllivier Robert clock_time->flags |= PARSEB_LEAPADD; /* default: DCF77 data format deficiency */
184c0b746e5SOllivier Robert }
185c0b746e5SOllivier Robert }
186c0b746e5SOllivier Robert
187276da39aSCy Schubert clock_time->flags |= PARSEB_S_LEAP|PARSEB_S_CALLBIT;
188c0b746e5SOllivier Robert
189c0b746e5SOllivier Robert return CVT_OK;
190c0b746e5SOllivier Robert }
191c0b746e5SOllivier Robert }
192c0b746e5SOllivier Robert }
193c0b746e5SOllivier Robert
194c0b746e5SOllivier Robert /*
195a25439b6SCy Schubert * parse_inp_fnc_t inp_schmid
196c0b746e5SOllivier Robert *
197a25439b6SCy Schubert * grab data from input stream
198c0b746e5SOllivier Robert */
199c0b746e5SOllivier Robert static u_long
inp_schmid(parse_t * parseio,char ch,timestamp_t * tstamp)200c0b746e5SOllivier Robert inp_schmid(
201c0b746e5SOllivier Robert parse_t *parseio,
202a25439b6SCy Schubert char ch,
203c0b746e5SOllivier Robert timestamp_t *tstamp
204c0b746e5SOllivier Robert )
205c0b746e5SOllivier Robert {
206c0b746e5SOllivier Robert unsigned int rtc;
207c0b746e5SOllivier Robert
2083311ff84SXin LI parseprintf(DD_PARSE, ("inp_schmid(0x%p, 0x%x, ...)\n", (void*)parseio, ch));
209c0b746e5SOllivier Robert
210a25439b6SCy Schubert switch ((uint8_t)ch)
211c0b746e5SOllivier Robert {
212c0b746e5SOllivier Robert case 0xFD: /* */
213a25439b6SCy Schubert parseprintf(DD_PARSE, ("inp_schmid: 0xFD seen\n"));
214c0b746e5SOllivier Robert if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
215c0b746e5SOllivier Robert return parse_end(parseio);
216c0b746e5SOllivier Robert else
217c0b746e5SOllivier Robert return rtc;
218c0b746e5SOllivier Robert
219c0b746e5SOllivier Robert default:
220c0b746e5SOllivier Robert return parse_addchar(parseio, ch);
221c0b746e5SOllivier Robert }
222c0b746e5SOllivier Robert }
223c0b746e5SOllivier Robert
224c0b746e5SOllivier Robert #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_SCHMID) */
225*f5f40dd6SCy Schubert NONEMPTY_TRANSLATION_UNIT
226c0b746e5SOllivier Robert #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_SCHMID) */
227c0b746e5SOllivier Robert
228c0b746e5SOllivier Robert /*
229c0b746e5SOllivier Robert * History:
230c0b746e5SOllivier Robert *
231c0b746e5SOllivier Robert * clk_schmid.c,v
232ea906c41SOllivier Robert * Revision 4.9 2005/04/16 17:32:10 kardel
233ea906c41SOllivier Robert * update copyright
234ea906c41SOllivier Robert *
235ea906c41SOllivier Robert * Revision 4.8 2004/11/14 15:29:41 kardel
236ea906c41SOllivier Robert * support PPSAPI, upgrade Copyright to Berkeley style
237ea906c41SOllivier Robert *
238a151a66cSOllivier Robert * Revision 4.5 1999/11/28 09:13:51 kardel
239a151a66cSOllivier Robert * RECON_4_0_98F
240a151a66cSOllivier Robert *
241c0b746e5SOllivier Robert * Revision 4.4 1998/06/13 12:06:03 kardel
242c0b746e5SOllivier Robert * fix SYSV clock name clash
243c0b746e5SOllivier Robert *
244c0b746e5SOllivier Robert * Revision 4.3 1998/06/12 15:22:29 kardel
245c0b746e5SOllivier Robert * fix prototypes
246c0b746e5SOllivier Robert *
247c0b746e5SOllivier Robert * Revision 4.2 1998/06/12 09:13:26 kardel
248c0b746e5SOllivier Robert * conditional compile macros fixed
249c0b746e5SOllivier Robert * printf prototype
250c0b746e5SOllivier Robert *
251c0b746e5SOllivier Robert * Revision 4.1 1998/05/24 09:39:53 kardel
252c0b746e5SOllivier Robert * implementation of the new IO handling model
253c0b746e5SOllivier Robert *
254c0b746e5SOllivier Robert * Revision 4.0 1998/04/10 19:45:31 kardel
255c0b746e5SOllivier Robert * Start 4.0 release version numbering
256c0b746e5SOllivier Robert *
257c0b746e5SOllivier Robert * from V3 3.22 log info deleted 1998/04/11 kardel
258c0b746e5SOllivier Robert */
259