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