]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/parseutil/testdcf.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / ntp / parseutil / testdcf.c
1 /*
2  * /src/NTP/ntp4-dev/parseutil/testdcf.c,v 4.10 2005/08/06 14:18:43 kardel RELEASE_20050806_A
3  *
4  * testdcf.c,v 4.10 2005/08/06 14:18:43 kardel RELEASE_20050806_A
5  *
6  * simple DCF77 100/200ms pulse test program (via 50Baud serial line)
7  *
8  * Copyright (c) 1995-2015 by Frank Kardel <kardel <AT> ntp.org>
9  * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universitaet Erlangen-Nuernberg, Germany
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the author nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36
37 #include <config.h>
38 #include "ntp_stdlib.h"
39
40 #include <sys/ioctl.h>
41 #include <unistd.h>
42 #include <stdio.h>
43 #include <fcntl.h>
44 #include <termios.h>
45
46 /*
47  * state flags
48  */
49 #define DCFB_ANNOUNCE   0x0001 /* switch time zone warning (DST switch) */
50 #define DCFB_DST        0x0002 /* DST in effect */
51 #define DCFB_LEAP       0x0004 /* LEAP warning (1 hour prior to occurrence) */
52 #define DCFB_CALLBIT    0x0008 /* "call bit" used to signalize irregularities in the control facilities */
53
54 struct clocktime                /* clock time broken up from time code */
55 {
56         long wday;
57         long day;
58         long month;
59         long year;
60         long hour;
61         long minute;
62         long second;
63         long usecond;
64         long utcoffset; /* in minutes */
65         long flags;             /* current clock status */
66 };
67
68 typedef struct clocktime clocktime_t;
69
70 static char type(unsigned int);
71
72 #define TIMES10(_X_) (((_X_) << 3) + ((_X_) << 1))
73
74 /*
75  * parser related return/error codes
76  */
77 #define CVT_MASK        0x0000000F /* conversion exit code */
78 #define   CVT_NONE      0x00000001 /* format not applicable */
79 #define   CVT_FAIL      0x00000002 /* conversion failed - error code returned */
80 #define   CVT_OK        0x00000004 /* conversion succeeded */
81 #define CVT_BADFMT      0x00000010 /* general format error - (unparsable) */
82
83 /*
84  * DCF77 raw time code
85  *
86  * From "Zur Zeit", Physikalisch-Technische Bundesanstalt (PTB), Braunschweig
87  * und Berlin, Maerz 1989
88  *
89  * Timecode transmission:
90  * AM:
91  *      time marks are send every second except for the second before the
92  *      next minute mark
93  *      time marks consist of a reduction of transmitter power to 25%
94  *      of the nominal level
95  *      the falling edge is the time indication (on time)
96  *      time marks of a 100ms duration constitute a logical 0
97  *      time marks of a 200ms duration constitute a logical 1
98  * FM:
99  *      see the spec. (basically a (non-)inverted psuedo random phase shift)
100  *
101  * Encoding:
102  * Second       Contents
103  * 0  - 10      AM: free, FM: 0
104  * 11 - 14      free
105  * 15           R     - "call bit" used to signalize irregularities in the control facilities
106  *                      (until 2003 indicated transmission via alternate antenna)
107  * 16           A1    - expect zone change (1 hour before)
108  * 17 - 18      Z1,Z2 - time zone
109  *               0  0 illegal
110  *               0  1 MEZ  (MET)
111  *               1  0 MESZ (MED, MET DST)
112  *               1  1 illegal
113  * 19           A2    - expect leap insertion/deletion (1 hour before)
114  * 20           S     - start of time code (1)
115  * 21 - 24      M1    - BCD (lsb first) Minutes
116  * 25 - 27      M10   - BCD (lsb first) 10 Minutes
117  * 28           P1    - Minute Parity (even)
118  * 29 - 32      H1    - BCD (lsb first) Hours
119  * 33 - 34      H10   - BCD (lsb first) 10 Hours
120  * 35           P2    - Hour Parity (even)
121  * 36 - 39      D1    - BCD (lsb first) Days
122  * 40 - 41      D10   - BCD (lsb first) 10 Days
123  * 42 - 44      DW    - BCD (lsb first) day of week (1: Monday -> 7: Sunday)
124  * 45 - 49      MO    - BCD (lsb first) Month
125  * 50           MO0   - 10 Months
126  * 51 - 53      Y1    - BCD (lsb first) Years
127  * 54 - 57      Y10   - BCD (lsb first) 10 Years
128  * 58           P3    - Date Parity (even)
129  * 59                 - usually missing (minute indication), except for leap insertion
130  */
131
132 static char revision[] = "4.10";
133
134 static struct rawdcfcode
135 {
136         char offset;                    /* start bit */
137 } rawdcfcode[] =
138 {
139         {  0 }, { 15 }, { 16 }, { 17 }, { 19 }, { 20 }, { 21 }, { 25 }, { 28 }, { 29 },
140         { 33 }, { 35 }, { 36 }, { 40 }, { 42 }, { 45 }, { 49 }, { 50 }, { 54 }, { 58 }, { 59 }
141 };
142
143 #define DCF_M   0
144 #define DCF_R   1
145 #define DCF_A1  2
146 #define DCF_Z   3
147 #define DCF_A2  4
148 #define DCF_S   5
149 #define DCF_M1  6
150 #define DCF_M10 7
151 #define DCF_P1  8
152 #define DCF_H1  9
153 #define DCF_H10 10
154 #define DCF_P2  11
155 #define DCF_D1  12
156 #define DCF_D10 13
157 #define DCF_DW  14
158 #define DCF_MO  15
159 #define DCF_MO0 16
160 #define DCF_Y1  17
161 #define DCF_Y10 18
162 #define DCF_P3  19
163
164 static struct partab
165 {
166         char offset;                    /* start bit of parity field */
167 } partab[] =
168 {
169         { 21 }, { 29 }, { 36 }, { 59 }
170 };
171
172 #define DCF_P_P1        0
173 #define DCF_P_P2        1
174 #define DCF_P_P3        2
175
176 #define DCF_Z_MET 0x2
177 #define DCF_Z_MED 0x1
178
179 static unsigned long
180 ext_bf(
181         register unsigned char *buf,
182         register int   idx
183         )
184 {
185         register unsigned long sum = 0;
186         register int i, first;
187
188         first = rawdcfcode[idx].offset;
189
190         for (i = rawdcfcode[idx+1].offset - 1; i >= first; i--)
191         {
192                 sum <<= 1;
193                 sum |= (buf[i] != '-');
194         }
195         return sum;
196 }
197
198 static unsigned
199 pcheck(
200         register unsigned char *buf,
201         register int   idx
202         )
203 {
204         register int i,last;
205         register unsigned psum = 1;
206
207         last = partab[idx+1].offset;
208
209         for (i = partab[idx].offset; i < last; i++)
210             psum ^= (buf[i] != '-');
211
212         return psum;
213 }
214
215 static unsigned long
216 convert_rawdcf(
217         register unsigned char   *buffer,
218         register int              size,
219         register clocktime_t     *clock_time
220         )
221 {
222         if (size < 57)
223         {
224                 printf("%-30s", "*** INCOMPLETE");
225                 return CVT_NONE;
226         }
227
228         /*
229          * check Start and Parity bits
230          */
231         if ((ext_bf(buffer, DCF_S) == 1) &&
232             pcheck(buffer, DCF_P_P1) &&
233             pcheck(buffer, DCF_P_P2) &&
234             pcheck(buffer, DCF_P_P3))
235         {
236                 /*
237                  * buffer OK
238                  */
239
240                 clock_time->flags  = 0;
241                 clock_time->usecond= 0;
242                 clock_time->second = 0;
243                 clock_time->minute = ext_bf(buffer, DCF_M10);
244                 clock_time->minute = TIMES10(clock_time->minute) + ext_bf(buffer, DCF_M1);
245                 clock_time->hour   = ext_bf(buffer, DCF_H10);
246                 clock_time->hour   = TIMES10(clock_time->hour) + ext_bf(buffer, DCF_H1);
247                 clock_time->day    = ext_bf(buffer, DCF_D10);
248                 clock_time->day    = TIMES10(clock_time->day) + ext_bf(buffer, DCF_D1);
249                 clock_time->month  = ext_bf(buffer, DCF_MO0);
250                 clock_time->month  = TIMES10(clock_time->month) + ext_bf(buffer, DCF_MO);
251                 clock_time->year   = ext_bf(buffer, DCF_Y10);
252                 clock_time->year   = TIMES10(clock_time->year) + ext_bf(buffer, DCF_Y1);
253                 clock_time->wday   = ext_bf(buffer, DCF_DW);
254
255                 switch (ext_bf(buffer, DCF_Z))
256                 {
257                     case DCF_Z_MET:
258                         clock_time->utcoffset = -60;
259                         break;
260
261                     case DCF_Z_MED:
262                         clock_time->flags     |= DCFB_DST;
263                         clock_time->utcoffset  = -120;
264                         break;
265
266                     default:
267                         printf("%-30s", "*** BAD TIME ZONE");
268                         return CVT_FAIL|CVT_BADFMT;
269                 }
270
271                 if (ext_bf(buffer, DCF_A1))
272                     clock_time->flags |= DCFB_ANNOUNCE;
273
274                 if (ext_bf(buffer, DCF_A2))
275                     clock_time->flags |= DCFB_LEAP;
276
277                 if (ext_bf(buffer, DCF_R))
278                     clock_time->flags |= DCFB_CALLBIT;
279
280                 return CVT_OK;
281         }
282         else
283         {
284                 /*
285                  * bad format - not for us
286                  */
287                 printf("%-30s", "*** BAD FORMAT (invalid/parity)");
288                 return CVT_FAIL|CVT_BADFMT;
289         }
290 }
291
292 static char
293 type(
294         unsigned int c
295         )
296 {
297         c ^= 0xFF;
298         return (c >= 0xF);
299 }
300
301 static const char *wday[8] =
302 {
303         "??",
304         "Mo",
305         "Tu",
306         "We",
307         "Th",
308         "Fr",
309         "Sa",
310         "Su"
311 };
312
313 static char pat[] = "-\\|/";
314
315 #define LINES (24-2)    /* error lines after which the two headlines are repeated */
316
317 int
318 main(
319         int argc,
320         char *argv[]
321         )
322 {
323         if ((argc != 2) && (argc != 3))
324         {
325                 fprintf(stderr, "usage: %s [-f|-t|-ft|-tf] <device>\n", argv[0]);
326                 exit(1);
327         }
328         else
329         {
330                 unsigned char c;
331                 char *file;
332                 int fd;
333                 int offset = 15;
334                 int trace = 0;
335                 int errs = LINES+1;
336
337                 /*
338                  * SIMPLE(!) argument "parser"
339                  */
340                 if (argc == 3)
341                 {
342                         if (strcmp(argv[1], "-f") == 0)
343                             offset = 0;
344                         if (strcmp(argv[1], "-t") == 0)
345                             trace = 1;
346                         if ((strcmp(argv[1], "-ft") == 0) ||
347                             (strcmp(argv[1], "-tf") == 0))
348                         {
349                                 offset = 0;
350                                 trace = 1;
351                         }
352                         file = argv[2];
353                 }
354                 else
355                 {
356                         file = argv[1];
357                 }
358
359                 fd = open(file, O_RDONLY);
360                 if (fd == -1)
361                 {
362                         perror(file);
363                         exit(1);
364                 }
365                 else
366                 {
367                         int i;
368 #ifdef TIOCM_RTS
369                         int on = TIOCM_RTS;
370 #endif
371                         struct timeval t, tt, tlast;
372                         char buf[61];
373                         clocktime_t clock_time;
374                         struct termios term;
375                         int rtc = CVT_NONE;
376
377                         if (tcgetattr(fd,  &term) == -1)
378                         {
379                                 perror("tcgetattr");
380                                 exit(1);
381                         }
382
383                         memset(term.c_cc, 0, sizeof(term.c_cc));
384                         term.c_cc[VMIN] = 1;
385 #ifdef NO_PARENB_IGNPAR /* Was: defined(SYS_IRIX4) || defined (SYS_IRIX5) */
386                         /* somehow doesn't grok PARENB & IGNPAR (mj) */
387                         term.c_cflag = CS8|CREAD|CLOCAL;
388 #else
389                         term.c_cflag = CS8|CREAD|CLOCAL|PARENB;
390 #endif
391                         term.c_iflag = IGNPAR;
392                         term.c_oflag = 0;
393                         term.c_lflag = 0;
394
395                         cfsetispeed(&term, B50);
396                         cfsetospeed(&term, B50);
397
398                         if (tcsetattr(fd, TCSANOW, &term) == -1)
399                         {
400                                 perror("tcsetattr");
401                                 exit(1);
402                         }
403
404 #ifdef I_POP
405                         while (ioctl(fd, I_POP, 0) == 0)
406                             ;
407 #endif
408 #if defined(TIOCMBIC) && defined(TIOCM_RTS)
409                         if (ioctl(fd, TIOCMBIC, (caddr_t)&on) == -1)
410                         {
411                                 perror("TIOCM_RTS");
412                         }
413 #endif
414
415                         printf("  DCF77 monitor %s - Copyright (C) 1993-2005, Frank Kardel\n\n", revision);
416
417                         clock_time.hour = 0;
418                         clock_time.minute = 0;
419                         clock_time.day = 0;
420                         clock_time.wday = 0;
421                         clock_time.month = 0;
422                         clock_time.year = 0;
423                         clock_time.flags = 0;
424                         buf[60] = '\0';
425                         for ( i = 0; i < 60; i++)
426                             buf[i] = '.';
427
428                         gettimeofday(&tlast, 0L);
429                         i = 0;
430                         while (read(fd, &c, 1) == 1)
431                         {
432                                 gettimeofday(&t, 0L);
433                                 tt = t;
434                                 t.tv_sec -= tlast.tv_sec;
435                                 t.tv_usec -= tlast.tv_usec;
436                                 if (t.tv_usec < 0)
437                                 {
438                                         t.tv_usec += 1000000;
439                                         t.tv_sec  -= 1;
440                                 }
441
442                                 if (errs > LINES)
443                                 {
444                                         printf("  %s", &"PTB private....RADMLSMin....PHour..PMDay..DayMonthYear....P\n"[offset]);
445                                         printf("  %s", &"---------------RADMLS1248124P124812P1248121241248112481248P\n"[offset]);
446                                         errs = 0;
447                                 }
448
449                                 if (t.tv_sec > 1 ||
450                                     (t.tv_sec == 1 &&
451                                      t.tv_usec > 500000))
452                                 {
453                                         printf("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &buf[offset]);
454
455                                         if ((rtc = convert_rawdcf((unsigned char *)buf, i, &clock_time)) != CVT_OK)
456                                         {
457                                                 printf("\n");
458                                                 clock_time.hour = 0;
459                                                 clock_time.minute = 0;
460                                                 clock_time.day = 0;
461                                                 clock_time.wday = 0;
462                                                 clock_time.month = 0;
463                                                 clock_time.year = 0;
464                                                 clock_time.flags = 0;
465                                                 errs++;
466                                         }
467
468                                         if (((c^0xFF)+1) & (c^0xFF))
469                                             buf[0] = '?';
470                                         else
471                                             buf[0] = type(c) ? '#' : '-';
472
473                                         for ( i = 1; i < 60; i++)
474                                             buf[i] = '.';
475
476                                         i = 0;
477                                 }
478                                 else
479                                 {
480                                         if (((c^0xFF)+1) & (c^0xFF))
481                                             buf[i] = '?';
482                                         else
483                                             buf[i] = type(c) ? '#' : '-';
484
485                                         printf("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &buf[offset]);
486                                 }
487
488                                 if (rtc == CVT_OK)
489                                 {
490                                         printf("%s, %2d:%02d:%02d, %d.%02d.%02d, <%s%s%s%s>",
491                                                wday[clock_time.wday],
492                                                (int)clock_time.hour, (int)clock_time.minute, (int)i, (int)clock_time.day, (int)clock_time.month,
493                                                (int)clock_time.year,
494                                                (clock_time.flags & DCFB_CALLBIT) ? "R" : "_",
495                                                (clock_time.flags & DCFB_ANNOUNCE) ? "A" : "_",
496                                                (clock_time.flags & DCFB_DST) ? "D" : "_",
497                                                (clock_time.flags & DCFB_LEAP) ? "L" : "_"
498                                                );
499                                         if (trace && (i == 0))
500                                         {
501                                                 printf("\n");
502                                                 errs++;
503                                         }
504                                 }
505
506                                 printf("\r");
507
508                                 if (i < 60)
509                                 {
510                                         i++;
511                                 }
512
513                                 tlast = tt;
514
515                                 fflush(stdout);
516                         }
517                         close(fd);
518                 }
519         }
520         return 0;
521 }
522
523 /*
524  * History:
525  *
526  * testdcf.c,v
527  * Revision 4.10  2005/08/06 14:18:43  kardel
528  * cleanup warnings
529  *
530  * Revision 4.9  2005/08/06 14:14:38  kardel
531  * document revision on startup
532  *
533  * Revision 4.8  2005/08/06 14:10:08  kardel
534  * fix setting of baud rate
535  *
536  * Revision 4.7  2005/04/16 17:32:10  kardel
537  * update copyright
538  *
539  * Revision 4.6  2004/11/14 15:29:42  kardel
540  * support PPSAPI, upgrade Copyright to Berkeley style
541  *
542  */