Merge from vendor branch GDB:
[dragonfly.git] / contrib / ntp / libparse / clk_computime.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_COMPUTIME)
6 /*
7  * /src/NTP/ntp-4/libparse/clk_computime.c,v 4.6 1999/11/28 09:13:49 kardel RELEASE_19991128_A
8  *
9  * clk_computime.c,v 4.6 1999/11/28 09:13:49 kardel RELEASE_19991128_A
10  * 
11  * Supports Diem's Computime Radio Clock
12  * 
13  * Used the Meinberg clock as a template for Diem's Computime Radio Clock
14  *
15  * adapted by Alois Camenzind <alois.camenzind@ubs.ch>
16  * 
17  * Copyright (C) 1992-1998 by Frank Kardel
18  * Friedrich-Alexander Universität Erlangen-Nürnberg, Germany
19  * 
20  * This program is distributed in the hope that it will be useful, but WITHOUT
21  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  * FITNESS FOR A PARTICULAR PURPOSE.
23  * 
24  */
25
26 #include "ntp_fp.h"
27 #include "ntp_unixtime.h"
28 #include "ntp_calendar.h"
29 #include "ntp_stdlib.h"
30
31 #include "parse.h"
32
33 #ifndef PARSESTREAM
34 #include <stdio.h>
35 #else
36 #include "sys/parsestreams.h"
37 extern void printf P((const char *, ...));
38 #endif
39
40 /*
41  * The Computime receiver sends a datagram in the following format every minute
42  * 
43  * Timestamp    T:YY:MM:MD:WD:HH:MM:SSCRLF 
44  * Pos          0123456789012345678901 2 3
45  *              0000000000111111111122 2 2
46  * Parse        T:  :  :  :  :  :  :  rn
47  * 
48  * T    Startcharacter "T" specifies start of the timestamp 
49  * YY   Year MM Month 1-12 
50  * MD   Day of the month 
51  * WD   Day of week 
52  * HH   Hour 
53  * MM   Minute 
54  * SS   Second
55  * CR   Carriage return 
56  * LF   Linefeed
57  * 
58  */
59
60 static struct format computime_fmt =
61 {
62         {
63                 {8, 2},  {5,  2}, {2,  2},      /* day, month, year */
64                 {14, 2}, {17, 2}, {20, 2},      /* hour, minute, second */
65                 {11, 2},                        /* dayofweek,  */
66         },
67         (const unsigned char *)"T:  :  :  :  :  :  :  \r\n",
68         0
69 };
70
71 static u_long cvt_computime P((unsigned char *, int, struct format *, clocktime_t *, void *));
72 static unsigned long inp_computime P((parse_t *, unsigned int, timestamp_t *));
73
74 clockformat_t   clock_computime =
75 {
76         inp_computime,          /* Computime input handling */
77         cvt_computime,          /* Computime conversion */
78         0,                      /* no PPS monitoring */
79         (void *)&computime_fmt, /* conversion configuration */
80         "Diem's Computime Radio Clock", /* Computime Radio Clock */
81         24,                     /* string buffer */
82         0                       /* no private data (complete pakets) */
83 };
84
85 /*
86  * cvt_computime
87  * 
88  * convert simple type format
89  */
90 static          u_long
91 cvt_computime(
92         unsigned char *buffer,
93         int            size,
94         struct format *format,
95         clocktime_t   *clock_time,
96         void          *local
97         )
98 {
99
100         if (!Strok(buffer, format->fixed_string)) { 
101                 return CVT_NONE;
102         } else {
103                 if (Stoi(&buffer[format->field_offsets[O_DAY].offset], &clock_time->day,
104                          format->field_offsets[O_DAY].length) ||
105                     Stoi(&buffer[format->field_offsets[O_MONTH].offset], &clock_time->month,
106                          format->field_offsets[O_MONTH].length) ||
107                     Stoi(&buffer[format->field_offsets[O_YEAR].offset], &clock_time->year,
108                          format->field_offsets[O_YEAR].length) ||
109                     Stoi(&buffer[format->field_offsets[O_HOUR].offset], &clock_time->hour,
110                          format->field_offsets[O_HOUR].length) ||
111                     Stoi(&buffer[format->field_offsets[O_MIN].offset], &clock_time->minute,
112                          format->field_offsets[O_MIN].length) ||
113                     Stoi(&buffer[format->field_offsets[O_SEC].offset], &clock_time->second,
114                          format->field_offsets[O_SEC].length)) { 
115                         return CVT_FAIL | CVT_BADFMT;
116                 } else {
117
118                         clock_time->flags = 0;
119                         clock_time->utcoffset = 0;      /* We have UTC time */
120
121                         return CVT_OK;
122                 }
123         }
124 }
125
126 /*
127  * inp_computime
128  *
129  * grep data from input stream
130  */
131 static u_long
132 inp_computime(
133               parse_t      *parseio,
134               unsigned int  ch,
135               timestamp_t  *tstamp
136               )
137 {
138         unsigned int rtc;
139         
140         parseprintf(DD_PARSE, ("inp_computime(0x%lx, 0x%x, ...)\n", (long)parseio, ch));
141         
142         switch (ch)
143         {
144         case 'T':
145                 parseprintf(DD_PARSE, ("inp_computime: START seen\n"));
146                 
147                 parseio->parse_index = 1;
148                 parseio->parse_data[0] = ch;
149                 parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
150                 return PARSE_INP_SKIP;
151           
152         case '\n':
153                 parseprintf(DD_PARSE, ("inp_computime: END seen\n"));
154                 if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
155                         return parse_end(parseio);
156                 else
157                         return rtc;
158
159         default:
160                 return parse_addchar(parseio, ch);
161         }
162 }
163
164 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_COMPUTIME) */
165 int clk_computime_bs;
166 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_COMPUTIME) */
167
168 /*
169  * clk_computime.c,v
170  * Revision 4.6  1999/11/28 09:13:49  kardel
171  * RECON_4_0_98F
172  *
173  * Revision 4.5  1998/06/14 21:09:34  kardel
174  * Sun acc cleanup
175  *
176  * Revision 4.4  1998/06/13 12:00:38  kardel
177  * fix SYSV clock name clash
178  *
179  * Revision 4.3  1998/06/12 15:22:26  kardel
180  * fix prototypes
181  *
182  * Revision 4.2  1998/06/12 09:13:24  kardel
183  * conditional compile macros fixed
184  * printf prototype
185  *
186  * Revision 4.1  1998/05/24 09:39:51  kardel
187  * implementation of the new IO handling model
188  *
189  * Revision 4.0  1998/04/10 19:45:27  kardel
190  * Start 4.0 release version numbering
191  *
192  * from V3 1.8 log info deleted 1998/04/11 kardel
193  */