clk_varitext.c revision 82498
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_VARITEXT)
6/*
7 * clk_varitext.c,v 1.0 1997/01/19 A.McConnell
8 *
9 * Supports Varitext's Radio Clock
10 *
11 * Used the Meinberg/Computime clock as a template for Varitext Radio Clock
12 *
13 * Copyright (C) 1992-1996 by Frank Kardel
14 * Friedrich-Alexander Universitt Erlangen-Nrnberg, Germany
15 *
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 */
21
22#include "ntp_fp.h"
23#include "ntp_unixtime.h"
24#include "ntp_calendar.h"
25
26#include "parse.h"
27
28#ifndef PARSESTREAM
29# include "ntp_stdlib.h"
30# include <stdio.h>
31#else
32# include "sys/parsestreams.h"
33extern void printf P((const char *, ...));
34#endif
35
36static const u_char VT_INITIALISED      = 0x01;
37static const u_char VT_SYNCHRONISED     = 0x02;
38static const u_char VT_ALARM_STATE      = 0x04;
39static const u_char VT_BST              = 0x08;
40static const u_char VT_SEASON_CHANGE    = 0x10;
41static const u_char VT_LAST_TELEGRAM_OK = 0x20;
42
43/*
44 * The Varitext receiver sends a datagram in the following format every minute
45 *
46 * Timestamp	T:YY:MM:MD:WD:HH:MM:SSCRLFSTXXX
47 * Pos          0123456789012345678901 2 3 4567
48 *              0000000000111111111122 2 2 2222
49 * Parse        T:  :  :  :  :  :  :  \r\n
50 *
51 * T	Startcharacter "T" specifies start of the timestamp
52 * YY	Year MM	Month 1-12
53 * MD	Day of the month
54 * WD	Day of week
55 * HH	Hour
56 * MM   Minute
57 * SS   Second
58 * CR   Carriage return
59 * LF   Linefeed
60 * ST	Status character
61 *	Bit 0 - Set= Initialised; Reset=Time Invalid (DO NOT USE)
62 *	Bit 1 - Set= Synchronised; Reset= Unsynchronised
63 * 	Bit 2 - Set= Alarm state; Reset= No alarm
64 * 	Bit 3 - Set= BST; Reset= GMT
65 * 	Bit 4 - Set= Seasonal change in approx hour; Reset= No seasonal change expected
66 *	Bit 5 - Set= Last MSF telegram was OK; Reset= Last telegram was in error;
67 * 	Bit 6 - Always set
68 *	Bit 7 - Unused
69 * XXX	Checksum calculated using Fletcher's method (ignored for now).
70 */
71
72static struct format varitext_fmt =
73{
74  {
75    {8, 2},  {5,  2}, {2,  2},	/* day, month, year */
76    {14, 2}, {17, 2}, {20, 2},	/* hour, minute, second */
77    {11, 2}, {24, 1}		/* dayofweek, status */
78  },
79  (const unsigned char*)"T:  :  :  :  :  :  :  \r\n    ",
80  0
81};
82
83static u_long   cvt_varitext P((unsigned char *, int, struct format *, clocktime_t *, void *));
84static u_long   inp_varitext P((parse_t *, unsigned int, timestamp_t *));
85
86struct varitext {
87  unsigned char start_found;
88  unsigned char end_found;
89  unsigned char end_count;
90  unsigned char previous_ch;
91  timestamp_t   tstamp;
92};
93
94clockformat_t   clock_varitext =
95{
96  inp_varitext,			/* Because of the strange format we need to parse it ourselves */
97  cvt_varitext,			/* Varitext conversion */
98  0,				/* no PPS monitoring */
99  (void *)&varitext_fmt,	/* conversion configuration */
100  "Varitext Radio Clock",	/* Varitext Radio Clock */
101  30,				/* string buffer */
102  sizeof(struct varitext),	/* Private data size required to hold current parse state */
103};
104
105/*
106 * cvt_varitext
107 *
108 * convert simple type format
109 */
110static          u_long
111cvt_varitext(
112	     unsigned char	*buffer,
113	     int    		size,
114	     struct format	*format,
115	     clocktime_t	*clock_time,
116	     void		*local
117	     )
118{
119
120  if (!Strok(buffer, format->fixed_string)) {
121    return CVT_NONE;
122  } else {
123    if (Stoi(&buffer[format->field_offsets[O_DAY].offset], &clock_time->day,
124	     format->field_offsets[O_DAY].length) ||
125	Stoi(&buffer[format->field_offsets[O_MONTH].offset], &clock_time->month,
126	     format->field_offsets[O_MONTH].length) ||
127	Stoi(&buffer[format->field_offsets[O_YEAR].offset], &clock_time->year,
128	     format->field_offsets[O_YEAR].length) ||
129	Stoi(&buffer[format->field_offsets[O_HOUR].offset], &clock_time->hour,
130	     format->field_offsets[O_HOUR].length) ||
131	Stoi(&buffer[format->field_offsets[O_MIN].offset], &clock_time->minute,
132	     format->field_offsets[O_MIN].length) ||
133	Stoi(&buffer[format->field_offsets[O_SEC].offset], &clock_time->second,
134	     format->field_offsets[O_SEC].length)) {
135      return CVT_FAIL | CVT_BADFMT;
136    } else {
137      u_char *f = (u_char*) &buffer[format->field_offsets[O_FLAGS].offset];
138
139      clock_time->flags = 0;
140      clock_time->utcoffset = 0;
141
142      if (((*f) & VT_BST))	/* BST flag is set so set to indicate daylight saving time is active and utc offset */
143	{
144	  clock_time->utcoffset = -1*60*60;
145	  clock_time->flags |= PARSEB_DST;
146	}
147      /*
148	 if (!((*f) & VT_INITIALISED))  Clock not initialised
149	 clock_time->flags |= PARSEB_POWERUP;
150
151	 if (!((*f) & VT_SYNCHRONISED))   Clock not synchronised
152	 clock_time->flags |= PARSEB_NOSYNC;
153
154	 if (((*f) & VT_SEASON_CHANGE))  Seasonal change expected in the next hour
155	 clock_time->flags |= PARSEB_ANNOUNCE;
156	 */
157      return CVT_OK;
158    }
159  }
160}
161
162static u_long
163inp_varitext(
164	     parse_t	 *parseio,
165	     unsigned int ch,
166	     timestamp_t *tstamp
167	     )
168{
169  struct varitext *t = (struct varitext *)parseio->parse_pdata;
170  int    rtc;
171
172  parseprintf(DD_PARSE, ("inp_varitext(0x%lx, 0x%x, ...)\n", (long)parseio, ch));
173
174  if (!t)
175    return PARSE_INP_SKIP;	/* local data not allocated - sigh! */
176
177  if (ch == 'T')
178    t->tstamp = *tstamp;
179
180  if ((t->previous_ch == 'T') && (ch == ':'))
181    {
182      parseprintf(DD_PARSE, ("inp_varitext: START seen\n"));
183
184      parseio->parse_data[0] = 'T';
185      parseio->parse_index=1;
186      parseio->parse_dtime.parse_stime = t->tstamp; /* Time stamp at packet start */
187      t->start_found = 1;
188      t->end_found = 0;
189      t->end_count = 0;
190    }
191
192  if (t->start_found)
193    {
194      if ((rtc = parse_addchar(parseio, ch)) != PARSE_INP_SKIP)
195	{
196	  parseprintf(DD_PARSE, ("inp_varitext: ABORTED due to too many characters\n"));
197
198	  memset(t, 0, sizeof(struct varitext));
199	  return rtc;
200	}
201
202      if (t->end_found)
203	{
204	  if (++(t->end_count) == 4) /* Finally found the end of the message */
205	    {
206	      parseprintf(DD_PARSE, ("inp_varitext: END seen\n"));
207
208	      memset(t, 0, sizeof(struct varitext));
209	      if ((rtc = parse_addchar(parseio, 0)) == PARSE_INP_SKIP)
210		return parse_end(parseio);
211	      else
212		return rtc;
213	    }
214	}
215
216      if ((t->previous_ch == '\r') && (ch == '\n'))
217	{
218	  t->end_found = 1;
219	}
220
221    }
222
223  t->previous_ch = ch;
224
225  return PARSE_INP_SKIP;
226}
227
228#else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_VARITEXT) */
229int clk_varitext_bs;
230#endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_VARITEXT) */
231
232/*
233 * Revision 1.0  1997/06/02 13:16:30  McConnell
234 * File created
235 *
236 */
237