Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / ntp / libparse / clk_dcf7000.c
1 /*
2  * /src/NTP/ntp-4/libparse/clk_dcf7000.c,v 4.6 1999/11/28 09:13:49 kardel RELEASE_19991128_A
3  *  
4  * clk_dcf7000.c,v 4.6 1999/11/28 09:13:49 kardel RELEASE_19991128_A
5  *
6  * ELV DCF7000 module
7  *
8  * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998 by Frank Kardel
9  * Friedrich-Alexander Universität Erlangen-Nürnberg, Germany
10  *                                    
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  *
15  */
16
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20
21 #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_DCF7000)
22
23 #include "ntp_fp.h"
24 #include "ntp_unixtime.h"
25 #include "ntp_calendar.h"
26
27 #include "parse.h"
28
29 #ifndef PARSESTREAM
30 #include "ntp_stdlib.h"
31 #include <stdio.h>
32 #else
33 #include "sys/parsestreams.h"
34 extern void printf P((const char *, ...));
35 #endif
36
37 static struct format dcf7000_fmt =
38 {                               /* ELV DCF7000 */
39         {
40                 {  6, 2}, {  3, 2}, {  0, 2},
41                 { 12, 2}, { 15, 2}, { 18, 2},
42                 {  9, 2}, { 21, 2},
43         },
44         (const unsigned char *)"  -  -  -  -  -  -  -  \r",
45         0
46 };    
47 static u_long cvt_dcf7000 P((unsigned char *, int, struct format *, clocktime_t *, void *));
48 static unsigned long inp_dcf7000 P((parse_t *, unsigned int, timestamp_t *));
49
50 clockformat_t clock_dcf7000 =
51 {
52   inp_dcf7000,                  /* DCF7000 input handling */
53   cvt_dcf7000,                  /* ELV DCF77 conversion */
54   0,                            /* no direct PPS monitoring */
55   (void *)&dcf7000_fmt,         /* conversion configuration */
56   "ELV DCF7000",                /* ELV clock */
57   24,                           /* string buffer */
58   0                             /* no private data (complete pakets) */
59 };
60
61 /*
62  * cvt_dcf7000
63  *
64  * convert dcf7000 type format
65  */
66 static u_long
67 cvt_dcf7000(
68             unsigned char *buffer,
69             int            size,
70             struct format *format,
71             clocktime_t   *clock_time,
72             void          *local
73             )
74 {
75         if (!Strok(buffer, format->fixed_string))
76         {
77                 return CVT_NONE;
78         }
79         else
80         {
81                 if (Stoi(&buffer[format->field_offsets[O_DAY].offset], &clock_time->day,
82                          format->field_offsets[O_DAY].length) ||
83                     Stoi(&buffer[format->field_offsets[O_MONTH].offset], &clock_time->month,
84                          format->field_offsets[O_MONTH].length) ||
85                     Stoi(&buffer[format->field_offsets[O_YEAR].offset], &clock_time->year,
86                          format->field_offsets[O_YEAR].length) ||
87                     Stoi(&buffer[format->field_offsets[O_HOUR].offset], &clock_time->hour,
88                          format->field_offsets[O_HOUR].length) ||
89                     Stoi(&buffer[format->field_offsets[O_MIN].offset], &clock_time->minute,
90                          format->field_offsets[O_MIN].length) ||
91                     Stoi(&buffer[format->field_offsets[O_SEC].offset], &clock_time->second,
92                          format->field_offsets[O_SEC].length))
93                 {
94                         return CVT_FAIL|CVT_BADFMT;
95                 }
96                 else
97                 {
98                         unsigned char *f = &buffer[format->field_offsets[O_FLAGS].offset];
99                         long flags;
100           
101                         clock_time->flags = 0;
102                         clock_time->usecond = 0;
103
104                         if (Stoi(f, &flags, format->field_offsets[O_FLAGS].length))
105                         {
106                                 return CVT_FAIL|CVT_BADFMT;
107                         }
108                         else
109                         {
110                                 if (flags & 0x1)
111                                     clock_time->utcoffset = -2*60*60;
112                                 else
113                                     clock_time->utcoffset = -1*60*60;
114
115                                 if (flags & 0x2)
116                                     clock_time->flags |= PARSEB_ANNOUNCE;
117
118                                 if (flags & 0x4)
119                                     clock_time->flags |= PARSEB_NOSYNC;
120                         }
121                         return CVT_OK;
122                 }
123         }
124 }
125
126 /*
127  * inp_dcf700
128  *
129  * grep data from input stream
130  */
131 static u_long
132 inp_dcf7000(
133           parse_t      *parseio,
134           unsigned int  ch,
135           timestamp_t  *tstamp
136           )
137 {
138         unsigned int rtc;
139         
140         parseprintf(DD_PARSE, ("inp_dcf7000(0x%lx, 0x%x, ...)\n", (long)parseio, ch));
141         
142         switch (ch)
143         {
144         case '\r':
145                 parseprintf(DD_PARSE, ("inp_dcf7000: EOL seen\n"));
146                 parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
147                 if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
148                         return parse_end(parseio);
149                 else
150                         return rtc;
151
152         default:
153                 return parse_addchar(parseio, ch);
154         }
155 }
156
157 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_DCF7000) */
158 int clk_dcf7000_bs;
159 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_DCF7000) */
160
161 /*
162  * History:
163  *
164  * clk_dcf7000.c,v
165  * Revision 4.6  1999/11/28 09:13:49  kardel
166  * RECON_4_0_98F
167  *
168  * Revision 4.5  1998/06/14 21:09:34  kardel
169  * Sun acc cleanup
170  *
171  * Revision 4.4  1998/06/13 12:01:59  kardel
172  * fix SYSV clock name clash
173  *
174  * Revision 4.3  1998/06/12 15:22:27  kardel
175  * fix prototypes
176  *
177  * Revision 4.2  1998/06/12 09:13:24  kardel
178  * conditional compile macros fixed
179  * printf prototype
180  *
181  * Revision 4.1  1998/05/24 09:39:51  kardel
182  * implementation of the new IO handling model
183  *
184  * Revision 4.0  1998/04/10 19:45:28  kardel
185  * Start 4.0 release version numbering
186  *
187  * from V3 3.18 log info deleted 1998/04/11 kardel
188  */