xref: /netbsd-src/external/bsd/ntp/dist/libparse/clk_varitext.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /*	$NetBSD: clk_varitext.c,v 1.1.1.1 2009/12/13 16:55:23 kardel Exp $	*/
2 
3 #ifdef HAVE_CONFIG_H
4 # include <config.h>
5 #endif
6 
7 #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_VARITEXT)
8 /*
9  * /src/NTP/ntp4-dev/libparse/clk_varitext.c,v 1.5 2005/04/16 17:32:10 kardel RELEASE_20050508_A
10  *
11  * clk_varitext.c,v 1.5 2005/04/16 17:32:10 kardel RELEASE_20050508_A
12  *
13  * Varitext code variant by A.McConnell 1997/01/19
14  *
15  * Supports Varitext's Radio Clock
16  *
17  * Used the Meinberg/Computime clock as a template for Varitext Radio Clock
18  *
19  * Codebase:
20  * Copyright (c) 1995-2005 by Frank Kardel <kardel <AT> ntp.org>
21  * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universit�t Erlangen-N�rnberg, Germany
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the above copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. Neither the name of the author nor the names of its contributors
32  *    may be used to endorse or promote products derived from this software
33  *    without specific prior written permission.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
36  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
39  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  * SUCH DAMAGE.
46  *
47  */
48 
49 #include "ntp_fp.h"
50 #include "ntp_unixtime.h"
51 #include "ntp_calendar.h"
52 
53 #include "parse.h"
54 
55 #ifndef PARSESTREAM
56 # include "ntp_stdlib.h"
57 # include <stdio.h>
58 #else
59 # include "sys/parsestreams.h"
60 extern int printf (const char *, ...);
61 #endif
62 
63 static const u_char VT_INITIALISED      = 0x01;
64 static const u_char VT_SYNCHRONISED     = 0x02;
65 static const u_char VT_ALARM_STATE      = 0x04;
66 static const u_char VT_BST              = 0x08;
67 static const u_char VT_SEASON_CHANGE    = 0x10;
68 static const u_char VT_LAST_TELEGRAM_OK = 0x20;
69 
70 /*
71  * The Varitext receiver sends a datagram in the following format every minute
72  *
73  * Timestamp	T:YY:MM:MD:WD:HH:MM:SSCRLFSTXXX
74  * Pos          0123456789012345678901 2 3 4567
75  *              0000000000111111111122 2 2 2222
76  * Parse        T:  :  :  :  :  :  :  \r\n
77  *
78  * T	Startcharacter "T" specifies start of the timestamp
79  * YY	Year MM	Month 1-12
80  * MD	Day of the month
81  * WD	Day of week
82  * HH	Hour
83  * MM   Minute
84  * SS   Second
85  * CR   Carriage return
86  * LF   Linefeed
87  * ST	Status character
88  *	Bit 0 - Set= Initialised; Reset=Time Invalid (DO NOT USE)
89  *	Bit 1 - Set= Synchronised; Reset= Unsynchronised
90  * 	Bit 2 - Set= Alarm state; Reset= No alarm
91  * 	Bit 3 - Set= BST; Reset= GMT
92  * 	Bit 4 - Set= Seasonal change in approx hour; Reset= No seasonal change expected
93  *	Bit 5 - Set= Last MSF telegram was OK; Reset= Last telegram was in error;
94  * 	Bit 6 - Always set
95  *	Bit 7 - Unused
96  * XXX	Checksum calculated using Fletcher's method (ignored for now).
97  */
98 
99 static struct format varitext_fmt =
100 {
101   {
102     {8, 2},  {5,  2}, {2,  2},	/* day, month, year */
103     {14, 2}, {17, 2}, {20, 2},	/* hour, minute, second */
104     {11, 2}, {24, 1}		/* dayofweek, status */
105   },
106   (const unsigned char*)"T:  :  :  :  :  :  :  \r\n    ",
107   0
108 };
109 
110 static u_long   cvt_varitext (unsigned char *, int, struct format *, clocktime_t *, void *);
111 static u_long   inp_varitext (parse_t *, unsigned int, timestamp_t *);
112 
113 struct varitext {
114   unsigned char start_found;
115   unsigned char end_found;
116   unsigned char end_count;
117   unsigned char previous_ch;
118   timestamp_t   tstamp;
119 };
120 
121 clockformat_t   clock_varitext =
122 {
123   inp_varitext,			/* Because of the strange format we need to parse it ourselves */
124   cvt_varitext,			/* Varitext conversion */
125   0,				/* no PPS monitoring */
126   (void *)&varitext_fmt,	/* conversion configuration */
127   "Varitext Radio Clock",	/* Varitext Radio Clock */
128   30,				/* string buffer */
129   sizeof(struct varitext),	/* Private data size required to hold current parse state */
130 };
131 
132 /*
133  * cvt_varitext
134  *
135  * convert simple type format
136  */
137 static          u_long
138 cvt_varitext(
139 	     unsigned char	*buffer,
140 	     int    		size,
141 	     struct format	*format,
142 	     clocktime_t	*clock_time,
143 	     void		*local
144 	     )
145 {
146 
147   if (!Strok(buffer, format->fixed_string)) {
148     return CVT_NONE;
149   } else {
150     if (Stoi(&buffer[format->field_offsets[O_DAY].offset], &clock_time->day,
151 	     format->field_offsets[O_DAY].length) ||
152 	Stoi(&buffer[format->field_offsets[O_MONTH].offset], &clock_time->month,
153 	     format->field_offsets[O_MONTH].length) ||
154 	Stoi(&buffer[format->field_offsets[O_YEAR].offset], &clock_time->year,
155 	     format->field_offsets[O_YEAR].length) ||
156 	Stoi(&buffer[format->field_offsets[O_HOUR].offset], &clock_time->hour,
157 	     format->field_offsets[O_HOUR].length) ||
158 	Stoi(&buffer[format->field_offsets[O_MIN].offset], &clock_time->minute,
159 	     format->field_offsets[O_MIN].length) ||
160 	Stoi(&buffer[format->field_offsets[O_SEC].offset], &clock_time->second,
161 	     format->field_offsets[O_SEC].length)) {
162       return CVT_FAIL | CVT_BADFMT;
163     } else {
164       u_char *f = (u_char*) &buffer[format->field_offsets[O_FLAGS].offset];
165 
166       clock_time->flags = 0;
167       clock_time->utcoffset = 0;
168 
169       if (((*f) & VT_BST))	/* BST flag is set so set to indicate daylight saving time is active and utc offset */
170 	{
171 	  clock_time->utcoffset = -1*60*60;
172 	  clock_time->flags |= PARSEB_DST;
173 	}
174       /*
175 	 if (!((*f) & VT_INITIALISED))  Clock not initialised
176 	 clock_time->flags |= PARSEB_POWERUP;
177 
178 	 if (!((*f) & VT_SYNCHRONISED))   Clock not synchronised
179 	 clock_time->flags |= PARSEB_NOSYNC;
180 
181 	 if (((*f) & VT_SEASON_CHANGE))  Seasonal change expected in the next hour
182 	 clock_time->flags |= PARSEB_ANNOUNCE;
183 	 */
184       return CVT_OK;
185     }
186   }
187 }
188 
189 static u_long
190 inp_varitext(
191 	     parse_t	 *parseio,
192 	     unsigned int ch,
193 	     timestamp_t *tstamp
194 	     )
195 {
196   struct varitext *t = (struct varitext *)parseio->parse_pdata;
197   int    rtc;
198 
199   parseprintf(DD_PARSE, ("inp_varitext(0x%lx, 0x%x, ...)\n", (long)parseio, ch));
200 
201   if (!t)
202     return PARSE_INP_SKIP;	/* local data not allocated - sigh! */
203 
204   if (ch == 'T')
205     t->tstamp = *tstamp;
206 
207   if ((t->previous_ch == 'T') && (ch == ':'))
208     {
209       parseprintf(DD_PARSE, ("inp_varitext: START seen\n"));
210 
211       parseio->parse_data[0] = 'T';
212       parseio->parse_index=1;
213       parseio->parse_dtime.parse_stime = t->tstamp; /* Time stamp at packet start */
214       t->start_found = 1;
215       t->end_found = 0;
216       t->end_count = 0;
217     }
218 
219   if (t->start_found)
220     {
221       if ((rtc = parse_addchar(parseio, ch)) != PARSE_INP_SKIP)
222 	{
223 	  parseprintf(DD_PARSE, ("inp_varitext: ABORTED due to too many characters\n"));
224 
225 	  memset(t, 0, sizeof(struct varitext));
226 	  return rtc;
227 	}
228 
229       if (t->end_found)
230 	{
231 	  if (++(t->end_count) == 4) /* Finally found the end of the message */
232 	    {
233 	      parseprintf(DD_PARSE, ("inp_varitext: END seen\n"));
234 
235 	      memset(t, 0, sizeof(struct varitext));
236 	      if ((rtc = parse_addchar(parseio, 0)) == PARSE_INP_SKIP)
237 		return parse_end(parseio);
238 	      else
239 		return rtc;
240 	    }
241 	}
242 
243       if ((t->previous_ch == '\r') && (ch == '\n'))
244 	{
245 	  t->end_found = 1;
246 	}
247 
248     }
249 
250   t->previous_ch = ch;
251 
252   return PARSE_INP_SKIP;
253 }
254 
255 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_VARITEXT) */
256 int clk_varitext_bs;
257 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_VARITEXT) */
258 
259 /*
260  * History:
261  *
262  * clk_varitext.c,v
263  * Revision 1.5  2005/04/16 17:32:10  kardel
264  * update copyright
265  *
266  * Revision 1.4  2004/11/14 15:29:41  kardel
267  * support PPSAPI, upgrade Copyright to Berkeley style
268  *
269  *
270  * Revision 1.0  1997/06/02 13:16:30  McConnell
271  * File created
272  *
273  */
274