clk_rawdcf.c revision 1.3.4.2 1 /* $NetBSD: clk_rawdcf.c,v 1.3.4.2 2015/04/23 18:53:02 snj Exp $ */
2
3 /*
4 * /src/NTP/REPOSITORY/ntp4-dev/libparse/clk_rawdcf.c,v 4.18 2006/06/22 18:40:01 kardel RELEASE_20060622_A
5 *
6 * clk_rawdcf.c,v 4.18 2006/06/22 18:40:01 kardel RELEASE_20060622_A
7 *
8 * Raw DCF77 pulse clock support
9 *
10 * Copyright (c) 1995-2006 by Frank Kardel <kardel <AT> ntp.org>
11 * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universitaet Erlangen-Nuernberg, Germany
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the author nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 */
38
39 #ifdef HAVE_CONFIG_H
40 # include <config.h>
41 #endif
42
43 #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_RAWDCF)
44
45 #include "ntp_fp.h"
46 #include "ntp_unixtime.h"
47 #include "ntp_calendar.h"
48
49 #include "parse.h"
50 #ifdef PARSESTREAM
51 # include <sys/parsestreams.h>
52 #endif
53
54 #ifndef PARSEKERNEL
55 # include "ntp_stdlib.h"
56 #endif
57
58 /*
59 * DCF77 raw time code
60 *
61 * From "Zur Zeit", Physikalisch-Technische Bundesanstalt (PTB), Braunschweig
62 * und Berlin, Maerz 1989
63 *
64 * Timecode transmission:
65 * AM:
66 * time marks are send every second except for the second before the
67 * next minute mark
68 * time marks consist of a reduction of transmitter power to 25%
69 * of the nominal level
70 * the falling edge is the time indication (on time)
71 * time marks of a 100ms duration constitute a logical 0
72 * time marks of a 200ms duration constitute a logical 1
73 * FM:
74 * see the spec. (basically a (non-)inverted psuedo random phase shift)
75 *
76 * Encoding:
77 * Second Contents
78 * 0 - 10 AM: free, FM: 0
79 * 11 - 14 free
80 * 15 R - "call bit" used to signalize irregularities in the control facilities
81 * (until 2003 indicated transmission via alternate antenna)
82 * 16 A1 - expect zone change (1 hour before)
83 * 17 - 18 Z1,Z2 - time zone
84 * 0 0 illegal
85 * 0 1 MEZ (MET)
86 * 1 0 MESZ (MED, MET DST)
87 * 1 1 illegal
88 * 19 A2 - expect leap insertion/deletion (1 hour before)
89 * 20 S - start of time code (1)
90 * 21 - 24 M1 - BCD (lsb first) Minutes
91 * 25 - 27 M10 - BCD (lsb first) 10 Minutes
92 * 28 P1 - Minute Parity (even)
93 * 29 - 32 H1 - BCD (lsb first) Hours
94 * 33 - 34 H10 - BCD (lsb first) 10 Hours
95 * 35 P2 - Hour Parity (even)
96 * 36 - 39 D1 - BCD (lsb first) Days
97 * 40 - 41 D10 - BCD (lsb first) 10 Days
98 * 42 - 44 DW - BCD (lsb first) day of week (1: Monday -> 7: Sunday)
99 * 45 - 49 MO - BCD (lsb first) Month
100 * 50 MO0 - 10 Months
101 * 51 - 53 Y1 - BCD (lsb first) Years
102 * 54 - 57 Y10 - BCD (lsb first) 10 Years
103 * 58 P3 - Date Parity (even)
104 * 59 - usually missing (minute indication), except for leap insertion
105 */
106
107 static parse_pps_fnc_t pps_rawdcf;
108 static parse_cvt_fnc_t cvt_rawdcf;
109 static parse_inp_fnc_t inp_rawdcf;
110
111 typedef struct last_tcode {
112 time_t tcode; /* last converted time code */
113 } last_tcode_t;
114
115 #define BUFFER_MAX 61
116
117 clockformat_t clock_rawdcf =
118 {
119 inp_rawdcf, /* DCF77 input handling */
120 cvt_rawdcf, /* raw dcf input conversion */
121 pps_rawdcf, /* examining PPS information */
122 0, /* no private configuration data */
123 "RAW DCF77 Timecode", /* direct decoding / time synthesis */
124
125 BUFFER_MAX, /* bit buffer */
126 sizeof(last_tcode_t)
127 };
128
129 static struct dcfparam
130 {
131 const unsigned char *onebits;
132 const unsigned char *zerobits;
133 } dcfparameter =
134 {
135 (const unsigned char *)"###############RADMLS1248124P124812P1248121241248112481248P??", /* 'ONE' representation */
136 (const unsigned char *)"--------------------s-------p------p----------------------p__" /* 'ZERO' representation */
137 };
138
139 static struct rawdcfcode
140 {
141 char offset; /* start bit */
142 } rawdcfcode[] =
143 {
144 { 0 }, { 15 }, { 16 }, { 17 }, { 19 }, { 20 }, { 21 }, { 25 }, { 28 }, { 29 },
145 { 33 }, { 35 }, { 36 }, { 40 }, { 42 }, { 45 }, { 49 }, { 50 }, { 54 }, { 58 }, { 59 }
146 };
147
148 #define DCF_M 0
149 #define DCF_R 1
150 #define DCF_A1 2
151 #define DCF_Z 3
152 #define DCF_A2 4
153 #define DCF_S 5
154 #define DCF_M1 6
155 #define DCF_M10 7
156 #define DCF_P1 8
157 #define DCF_H1 9
158 #define DCF_H10 10
159 #define DCF_P2 11
160 #define DCF_D1 12
161 #define DCF_D10 13
162 #define DCF_DW 14
163 #define DCF_MO 15
164 #define DCF_MO0 16
165 #define DCF_Y1 17
166 #define DCF_Y10 18
167 #define DCF_P3 19
168
169 static struct partab
170 {
171 char offset; /* start bit of parity field */
172 } partab[] =
173 {
174 { 21 }, { 29 }, { 36 }, { 59 }
175 };
176
177 #define DCF_P_P1 0
178 #define DCF_P_P2 1
179 #define DCF_P_P3 2
180
181 #define DCF_Z_MET 0x2
182 #define DCF_Z_MED 0x1
183
184 static u_long
185 ext_bf(
186 unsigned char *buf,
187 int idx,
188 const unsigned char *zero
189 )
190 {
191 u_long sum = 0;
192 int i, first;
193
194 first = rawdcfcode[idx].offset;
195
196 for (i = rawdcfcode[idx+1].offset - 1; i >= first; i--)
197 {
198 sum <<= 1;
199 sum |= (buf[i] != zero[i]);
200 }
201 return sum;
202 }
203
204 static unsigned
205 pcheck(
206 unsigned char *buf,
207 int idx,
208 const unsigned char *zero
209 )
210 {
211 int i,last;
212 unsigned psum = 1;
213
214 last = partab[idx+1].offset;
215
216 for (i = partab[idx].offset; i < last; i++)
217 psum ^= (buf[i] != zero[i]);
218
219 return psum;
220 }
221
222 static u_long
223 convert_rawdcf(
224 unsigned char *buffer,
225 int size,
226 struct dcfparam *dcfprm,
227 clocktime_t *clock_time
228 )
229 {
230 unsigned char *s = buffer;
231 const unsigned char *b = dcfprm->onebits;
232 const unsigned char *c = dcfprm->zerobits;
233 int i;
234
235 parseprintf(DD_RAWDCF,("parse: convert_rawdcf: \"%s\"\n", buffer));
236
237 if (size < 57)
238 {
239 #ifndef PARSEKERNEL
240 msyslog(LOG_ERR, "parse: convert_rawdcf: INCOMPLETE DATA - time code only has %d bits", size);
241 #endif
242 return CVT_NONE;
243 }
244
245 for (i = 0; i < size; i++)
246 {
247 if ((*s != *b) && (*s != *c))
248 {
249 /*
250 * we only have two types of bytes (ones and zeros)
251 */
252 #ifndef PARSEKERNEL
253 msyslog(LOG_ERR, "parse: convert_rawdcf: BAD DATA - no conversion");
254 #endif
255 return CVT_NONE;
256 }
257 if (*b) b++;
258 if (*c) c++;
259 s++;
260 }
261
262 /*
263 * check Start and Parity bits
264 */
265 if ((ext_bf(buffer, DCF_S, dcfprm->zerobits) == 1) &&
266 pcheck(buffer, DCF_P_P1, dcfprm->zerobits) &&
267 pcheck(buffer, DCF_P_P2, dcfprm->zerobits) &&
268 pcheck(buffer, DCF_P_P3, dcfprm->zerobits))
269 {
270 /*
271 * buffer OK
272 */
273 parseprintf(DD_RAWDCF,("parse: convert_rawdcf: parity check passed\n"));
274
275 clock_time->flags = PARSEB_S_ANTENNA|PARSEB_S_LEAP;
276 clock_time->utctime= 0;
277 clock_time->usecond= 0;
278 clock_time->second = 0;
279 clock_time->minute = ext_bf(buffer, DCF_M10, dcfprm->zerobits);
280 clock_time->minute = TIMES10(clock_time->minute) + ext_bf(buffer, DCF_M1, dcfprm->zerobits);
281 clock_time->hour = ext_bf(buffer, DCF_H10, dcfprm->zerobits);
282 clock_time->hour = TIMES10(clock_time->hour) + ext_bf(buffer, DCF_H1, dcfprm->zerobits);
283 clock_time->day = ext_bf(buffer, DCF_D10, dcfprm->zerobits);
284 clock_time->day = TIMES10(clock_time->day) + ext_bf(buffer, DCF_D1, dcfprm->zerobits);
285 clock_time->month = ext_bf(buffer, DCF_MO0, dcfprm->zerobits);
286 clock_time->month = TIMES10(clock_time->month) + ext_bf(buffer, DCF_MO, dcfprm->zerobits);
287 clock_time->year = ext_bf(buffer, DCF_Y10, dcfprm->zerobits);
288 clock_time->year = TIMES10(clock_time->year) + ext_bf(buffer, DCF_Y1, dcfprm->zerobits);
289
290 switch (ext_bf(buffer, DCF_Z, dcfprm->zerobits))
291 {
292 case DCF_Z_MET:
293 clock_time->utcoffset = -1*60*60;
294 break;
295
296 case DCF_Z_MED:
297 clock_time->flags |= PARSEB_DST;
298 clock_time->utcoffset = -2*60*60;
299 break;
300
301 default:
302 parseprintf(DD_RAWDCF,("parse: convert_rawdcf: BAD TIME ZONE\n"));
303 return CVT_FAIL|CVT_BADFMT;
304 }
305
306 if (ext_bf(buffer, DCF_A1, dcfprm->zerobits))
307 clock_time->flags |= PARSEB_ANNOUNCE;
308
309 if (ext_bf(buffer, DCF_A2, dcfprm->zerobits))
310 clock_time->flags |= PARSEB_LEAPADD; /* default: DCF77 data format deficiency */
311
312 if (ext_bf(buffer, DCF_R, dcfprm->zerobits))
313 clock_time->flags |= PARSEB_CALLBIT;
314
315 parseprintf(DD_RAWDCF,("parse: convert_rawdcf: TIME CODE OK: %d:%d, %d.%d.%d, flags 0x%lx\n",
316 (int)clock_time->hour, (int)clock_time->minute, (int)clock_time->day, (int)clock_time->month,(int) clock_time->year,
317 (u_long)clock_time->flags));
318 return CVT_OK;
319 }
320 else
321 {
322 /*
323 * bad format - not for us
324 */
325 #ifndef PARSEKERNEL
326 msyslog(LOG_ERR, "parse: convert_rawdcf: parity check FAILED for \"%s\"", buffer);
327 #endif
328 return CVT_FAIL|CVT_BADFMT;
329 }
330 }
331
332 /*
333 * parse_cvt_fnc_t cvt_rawdcf
334 * raw dcf input routine - needs to fix up 50 baud
335 * characters for 1/0 decision
336 */
337 static u_long
338 cvt_rawdcf(
339 unsigned char *buffer,
340 int size,
341 struct format *param,
342 clocktime_t *clock_time,
343 void *local
344 )
345 {
346 last_tcode_t *t = (last_tcode_t *)local;
347 unsigned char *s = (unsigned char *)buffer;
348 unsigned char *e = s + size;
349 const unsigned char *b = dcfparameter.onebits;
350 const unsigned char *c = dcfparameter.zerobits;
351 u_long rtc = CVT_NONE;
352 unsigned int i, lowmax, highmax, cutoff, span;
353 #define BITS 9
354 unsigned char histbuf[BITS];
355 /*
356 * the input buffer contains characters with runs of consecutive
357 * bits set. These set bits are an indication of the DCF77 pulse
358 * length. We assume that we receive the pulse at 50 Baud. Thus
359 * a 100ms pulse would generate a 4 bit train (20ms per bit and
360 * start bit)
361 * a 200ms pulse would create all zeroes (and probably a frame error)
362 */
363
364 for (i = 0; i < BITS; i++)
365 {
366 histbuf[i] = 0;
367 }
368
369 cutoff = 0;
370 lowmax = 0;
371
372 while (s < e)
373 {
374 unsigned int ch = *s ^ 0xFF;
375 /*
376 * these lines are left as an excercise to the reader 8-)
377 */
378 if (!((ch+1) & ch) || !*s)
379 {
380
381 for (i = 0; ch; i++)
382 {
383 ch >>= 1;
384 }
385
386 *s = (unsigned char) i;
387 histbuf[i]++;
388 cutoff += i;
389 lowmax++;
390 }
391 else
392 {
393 parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: character check for 0x%x@%d FAILED\n", *s, (int)(s - (unsigned char *)buffer)));
394 *s = (unsigned char)~0;
395 rtc = CVT_FAIL|CVT_BADFMT;
396 }
397 s++;
398 }
399
400 if (lowmax)
401 {
402 cutoff /= lowmax;
403 }
404 else
405 {
406 cutoff = 4; /* doesn't really matter - it'll fail anyway, but gives error output */
407 }
408
409 parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: average bit count: %d\n", cutoff));
410
411 lowmax = 0;
412 highmax = 0;
413
414 parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: histogram:"));
415 for (i = 0; i <= cutoff; i++)
416 {
417 lowmax+=histbuf[i] * i;
418 highmax += histbuf[i];
419 parseprintf(DD_RAWDCF,(" %d", histbuf[i]));
420 }
421 parseprintf(DD_RAWDCF, (" <M>"));
422
423 lowmax += highmax / 2;
424
425 if (highmax)
426 {
427 lowmax /= highmax;
428 }
429 else
430 {
431 lowmax = 0;
432 }
433
434 highmax = 0;
435 cutoff = 0;
436
437 for (; i < BITS; i++)
438 {
439 highmax+=histbuf[i] * i;
440 cutoff +=histbuf[i];
441 parseprintf(DD_RAWDCF,(" %d", histbuf[i]));
442 }
443 parseprintf(DD_RAWDCF,("\n"));
444
445 if (cutoff)
446 {
447 highmax /= cutoff;
448 }
449 else
450 {
451 highmax = BITS-1;
452 }
453
454 span = cutoff = lowmax;
455 for (i = lowmax; i <= highmax; i++)
456 {
457 if (histbuf[cutoff] > histbuf[i])
458 {
459 cutoff = i;
460 span = i;
461 }
462 else
463 if (histbuf[cutoff] == histbuf[i])
464 {
465 span = i;
466 }
467 }
468
469 cutoff = (cutoff + span) / 2;
470
471 parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: lower maximum %d, higher maximum %d, cutoff %d\n", lowmax, highmax, cutoff));
472
473 s = (unsigned char *)buffer;
474 while (s < e)
475 {
476 if (*s == (unsigned char)~0)
477 {
478 *s = '?';
479 }
480 else
481 {
482 *s = (*s >= cutoff) ? *b : *c;
483 }
484 s++;
485 if (*b) b++;
486 if (*c) c++;
487 }
488
489 if (rtc == CVT_NONE)
490 {
491 rtc = convert_rawdcf(buffer, size, &dcfparameter, clock_time);
492 if (rtc == CVT_OK)
493 {
494 time_t newtime;
495
496 newtime = parse_to_unixtime(clock_time, &rtc);
497 if ((rtc == CVT_OK) && t)
498 {
499 if ((newtime - t->tcode) == 60) /* guard against multi bit errors */
500 {
501 clock_time->utctime = newtime;
502 }
503 else
504 {
505 rtc = CVT_FAIL|CVT_BADTIME;
506 }
507 t->tcode = newtime;
508 }
509 }
510 }
511
512 return rtc;
513 }
514
515 /*
516 * parse_pps_fnc_t pps_rawdcf
517 *
518 * currently a very stupid version - should be extended to decode
519 * also ones and zeros (which is easy)
520 */
521 /*ARGSUSED*/
522 static u_long
523 pps_rawdcf(
524 parse_t *parseio,
525 int status,
526 timestamp_t *ptime
527 )
528 {
529 if (!status) /* negative edge for simpler wiring (Rx->DCD) */
530 {
531 parseio->parse_dtime.parse_ptime = *ptime;
532 parseio->parse_dtime.parse_state |= PARSEB_PPS|PARSEB_S_PPS;
533 }
534
535 return CVT_NONE;
536 }
537
538 static u_long
539 snt_rawdcf(
540 parse_t *parseio,
541 timestamp_t *ptime
542 )
543 {
544 if ((parseio->parse_dtime.parse_status & CVT_MASK) == CVT_OK)
545 {
546 parseio->parse_dtime.parse_stime = *ptime;
547
548 #ifdef PARSEKERNEL
549 parseio->parse_dtime.parse_time.tv.tv_sec++;
550 #else
551 parseio->parse_dtime.parse_time.fp.l_ui++;
552 #endif
553
554 parseprintf(DD_RAWDCF,("parse: snt_rawdcf: time stamp synthesized offset %d seconds\n", parseio->parse_index - 1));
555
556 return updatetimeinfo(parseio, parseio->parse_lstate);
557 }
558 return CVT_NONE;
559 }
560
561 /*
562 * parse_inp_fnc_t inp_rawdcf
563 *
564 * grab DCF77 data from input stream
565 */
566 static u_long
567 inp_rawdcf(
568 parse_t *parseio,
569 char ch,
570 timestamp_t *tstamp
571 )
572 {
573 static struct timeval timeout = { 1, 500000 }; /* 1.5 secongs denote second #60 */
574
575 parseprintf(DD_PARSE, ("inp_rawdcf(0x%lx, 0x%x, ...)\n", (long)parseio, ch));
576
577 parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
578
579 if (parse_timedout(parseio, tstamp, &timeout))
580 {
581 parseprintf(DD_PARSE, ("inp_rawdcf: time out seen\n"));
582
583 (void) parse_end(parseio);
584 (void) parse_addchar(parseio, ch);
585 return PARSE_INP_TIME;
586 }
587 else
588 {
589 unsigned int rtc;
590
591 rtc = parse_addchar(parseio, ch);
592 if (rtc == PARSE_INP_SKIP)
593 {
594 if (snt_rawdcf(parseio, tstamp) == CVT_OK)
595 return PARSE_INP_SYNTH;
596 }
597 return rtc;
598 }
599 }
600
601 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_RAWDCF) */
602 int clk_rawdcf_bs;
603 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_RAWDCF) */
604
605 /*
606 * History:
607 *
608 * clk_rawdcf.c,v
609 * Revision 4.18 2006/06/22 18:40:01 kardel
610 * clean up signedness (gcc 4)
611 *
612 * Revision 4.17 2006/01/22 16:01:55 kardel
613 * update version information
614 *
615 * Revision 4.16 2006/01/22 15:51:22 kardel
616 * generate reasonable timecode output on invalid input
617 *
618 * Revision 4.15 2005/08/06 19:17:06 kardel
619 * clean log output
620 *
621 * Revision 4.14 2005/08/06 17:39:40 kardel
622 * cleanup size handling wrt/ to buffer boundaries
623 *
624 * Revision 4.13 2005/04/16 17:32:10 kardel
625 * update copyright
626 *
627 * Revision 4.12 2004/11/14 15:29:41 kardel
628 * support PPSAPI, upgrade Copyright to Berkeley style
629 *
630 * Revision 4.9 1999/12/06 13:42:23 kardel
631 * transfer correctly converted time codes always into tcode
632 *
633 * Revision 4.8 1999/11/28 09:13:50 kardel
634 * RECON_4_0_98F
635 *
636 * Revision 4.7 1999/04/01 20:07:20 kardel
637 * added checking for minutie increment of timestamps in clk_rawdcf.c
638 *
639 * Revision 4.6 1998/06/14 21:09:37 kardel
640 * Sun acc cleanup
641 *
642 * Revision 4.5 1998/06/13 12:04:16 kardel
643 * fix SYSV clock name clash
644 *
645 * Revision 4.4 1998/06/12 15:22:28 kardel
646 * fix prototypes
647 *
648 * Revision 4.3 1998/06/06 18:33:36 kardel
649 * simplified condidional compile expression
650 *
651 * Revision 4.2 1998/05/24 11:04:18 kardel
652 * triggering PPS on negative edge for simpler wiring (Rx->DCD)
653 *
654 * Revision 4.1 1998/05/24 09:39:53 kardel
655 * implementation of the new IO handling model
656 *
657 * Revision 4.0 1998/04/10 19:45:30 kardel
658 * Start 4.0 release version numbering
659 *
660 * from V3 3.24 log info deleted 1998/04/11 kardel
661 *
662 */
663