2 * /src/NTP/REPOSITORY/ntp4-dev/libparse/clk_meinberg.c,v 4.12.2.1 2005/09/25 10:22:35 kardel RELEASE_20050925_A
4 * clk_meinberg.c,v 4.12.2.1 2005/09/25 10:22:35 kardel RELEASE_20050925_A
6 * Meinberg clock support
8 * Copyright (c) 1995-2005 by Frank Kardel <kardel <AT> ntp.org>
9 * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universität Erlangen-Nürnberg, Germany
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
41 #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_MEINBERG)
44 #include "ntp_unixtime.h"
45 #include "ntp_calendar.h"
47 #include "ntp_machine.h"
54 #include "sys/parsestreams.h"
57 #include "ntp_stdlib.h"
59 #include "ntp_stdlib.h"
61 #include "mbg_gps166.h"
66 * The Meinberg receiver every second sends a datagram of the following form
69 * <STX>D:<dd>.<mm>.<yy>;T:<w>;U:<hh>:<mm>:<ss>;<S><F><D><A><ETX>
70 * pos: 0 00 00 0 00 0 11 111 1 111 12 2 22 2 22 2 2 2 3 3 3
71 * 1 23 45 6 78 9 01 234 5 678 90 1 23 4 56 7 8 9 0 1 2
72 * <STX> = '\002' ASCII start of text
73 * <ETX> = '\003' ASCII end of text
74 * <dd>,<mm>,<yy> = day, month, year(2 digits!!)
75 * <w> = day of week (sunday= 0)
76 * <hh>,<mm>,<ss> = hour, minute, second
77 * <S> = '#' if never synced since powerup for DCF C51
78 * = '#' if not PZF sychronisation available for PZF 535/509
80 * <F> = '*' if time comes from internal quartz
81 * = ' ' if completely synched
82 * <D> = 'S' if daylight saving time is active
83 * = 'U' if time is represented in UTC
84 * = ' ' if no special condition exists
85 * <A> = '!' during the hour preceeding an daylight saving time
87 * = 'A' leap second insert warning
88 * = ' ' if no special condition exists
90 * Extended data format (PZFUERL for PZF type clocks)
92 * <STX><dd>.<mm>.<yy>; <w>; <hh>:<mm>:<ss>; <U><S><F><D><A><L><R><ETX>
93 * pos: 0 00 0 00 0 00 11 1 11 11 1 11 2 22 22 2 2 2 2 2 3 3 3
94 * 1 23 4 56 7 89 01 2 34 56 7 89 0 12 34 5 6 7 8 9 0 1 2
95 * <STX> = '\002' ASCII start of text
96 * <ETX> = '\003' ASCII end of text
97 * <dd>,<mm>,<yy> = day, month, year(2 digits!!)
98 * <w> = day of week (sunday= 0)
99 * <hh>,<mm>,<ss> = hour, minute, second
100 * <U> = 'U' UTC time display
101 * <S> = '#' if never synced since powerup else ' ' for DCF C51
102 * '#' if not PZF sychronisation available else ' ' for PZF 535/509
103 * <F> = '*' if time comes from internal quartz else ' '
104 * <D> = 'S' if daylight saving time is active else ' '
105 * <A> = '!' during the hour preceeding an daylight saving time
107 * <L> = 'A' LEAP second announcement
108 * <R> = 'R' alternate antenna
110 * Meinberg GPS166 receiver
112 * You must get the Uni-Erlangen firmware for the GPS receiver support
113 * to work to full satisfaction !
115 * <STX><dd>.<mm>.<yy>; <w>; <hh>:<mm>:<ss>; <+/-><00:00>; <U><S><F><D><A><L><R><L>; <position...><ETX>
117 * 000000000111111111122222222223333333333444444444455555555556666666
118 * 123456789012345678901234567890123456789012345678901234567890123456
119 * \x0209.07.93; 5; 08:48:26; +00:00; #*S!A L; 49.5736N 11.0280E 373m\x03
122 * <STX> = '\002' ASCII start of text
123 * <ETX> = '\003' ASCII end of text
124 * <dd>,<mm>,<yy> = day, month, year(2 digits!!)
125 * <w> = day of week (sunday= 0)
126 * <hh>,<mm>,<ss> = hour, minute, second
127 * <+/->,<00:00> = offset to UTC
128 * <S> = '#' if never synced since powerup else ' '
129 * <F> = '*' if position is not confirmed else ' '
130 * <D> = 'S' if daylight saving time is active else ' '
131 * <A> = '!' during the hour preceeding an daylight saving time
133 * <L> = 'A' LEAP second announcement
134 * <R> = 'R' alternate antenna (reminiscent of PZF535) usually ' '
135 * <L> = 'L' on 23:59:60
137 * Binary messages have a lead in for a fixed header of SOH
140 /*--------------------------------------------------------------*/
143 /* Purpose: Compute a checksum about a number of bytes */
145 /* Input: uchar *p address of the first byte */
146 /* short n the number of bytes */
150 /* Ret val: the checksum */
151 /*+-------------------------------------------------------------*/
159 unsigned long sum = 0;
162 for ( i = 0; i < n; i++ )
170 unsigned char **bufpp,
174 headerp->gps_cmd = get_lsb_short(bufpp);
175 headerp->gps_len = get_lsb_short(bufpp);
176 headerp->gps_data_csum = get_lsb_short(bufpp);
177 headerp->gps_hdr_csum = get_lsb_short(bufpp);
180 static struct format meinberg_fmt[] =
184 { 3, 2}, { 6, 2}, { 9, 2},
185 { 18, 2}, { 21, 2}, { 24, 2},
186 { 14, 1}, { 27, 4}, { 29, 1},
188 (const unsigned char *)"\2D: . . ;T: ;U: . . ; \3",
191 { /* special extended FAU Erlangen extended format */
193 { 1, 2}, { 4, 2}, { 7, 2},
194 { 14, 2}, { 17, 2}, { 20, 2},
195 { 11, 1}, { 25, 4}, { 27, 1},
197 (const unsigned char *)"\2 . . ; ; : : ; \3",
200 { /* special extended FAU Erlangen GPS format */
202 { 1, 2}, { 4, 2}, { 7, 2},
203 { 14, 2}, { 17, 2}, { 20, 2},
204 { 11, 1}, { 32, 7}, { 35, 1},
205 { 25, 2}, { 28, 2}, { 24, 1}
207 (const unsigned char *)"\2 . . ; ; : : ; : ; ; . . ",
212 static u_long cvt_meinberg P((unsigned char *, int, struct format *, clocktime_t *, void *));
213 static u_long cvt_mgps P((unsigned char *, int, struct format *, clocktime_t *, void *));
214 static u_long mbg_input P((parse_t *, unsigned int, timestamp_t *));
215 static u_long gps_input P((parse_t *, unsigned int, timestamp_t *));
219 unsigned short len; /* len to fill */
220 unsigned short phase; /* current input phase */
223 #define MBG_NONE 0 /* no data input */
224 #define MBG_HEADER 1 /* receiving header */
225 #define MBG_DATA 2 /* receiving data */
226 #define MBG_STRING 3 /* receiving standard data message */
228 clockformat_t clock_meinberg[] =
231 mbg_input, /* normal input handling */
232 cvt_meinberg, /* Meinberg conversion */
233 pps_one, /* easy PPS monitoring */
234 0, /* conversion configuration */
235 "Meinberg Standard", /* Meinberg simple format - beware */
236 32, /* string buffer */
237 0 /* no private data (complete pakets) */
240 mbg_input, /* normal input handling */
241 cvt_meinberg, /* Meinberg conversion */
242 pps_one, /* easy PPS monitoring */
243 0, /* conversion configuration */
244 "Meinberg Extended", /* Meinberg enhanced format */
245 32, /* string buffer */
246 0 /* no private data (complete pakets) */
249 gps_input, /* no input handling */
250 cvt_mgps, /* Meinberg GPS166 conversion */
251 pps_one, /* easy PPS monitoring */
252 (void *)&meinberg_fmt[2], /* conversion configuration */
253 "Meinberg GPS Extended", /* Meinberg FAU GPS format */
254 512, /* string buffer */
255 sizeof(struct msg_buf) /* no private data (complete pakets) */
262 * convert simple type format
266 unsigned char *buffer,
268 struct format *unused,
269 clocktime_t *clock_time,
273 struct format *format;
276 * select automagically correct data format
278 if (Strok(buffer, meinberg_fmt[0].fixed_string))
280 format = &meinberg_fmt[0];
284 if (Strok(buffer, meinberg_fmt[1].fixed_string))
286 format = &meinberg_fmt[1];
290 return CVT_FAIL|CVT_BADFMT;
297 if (Stoi(&buffer[format->field_offsets[O_DAY].offset], &clock_time->day,
298 format->field_offsets[O_DAY].length) ||
299 Stoi(&buffer[format->field_offsets[O_MONTH].offset], &clock_time->month,
300 format->field_offsets[O_MONTH].length) ||
301 Stoi(&buffer[format->field_offsets[O_YEAR].offset], &clock_time->year,
302 format->field_offsets[O_YEAR].length) ||
303 Stoi(&buffer[format->field_offsets[O_HOUR].offset], &clock_time->hour,
304 format->field_offsets[O_HOUR].length) ||
305 Stoi(&buffer[format->field_offsets[O_MIN].offset], &clock_time->minute,
306 format->field_offsets[O_MIN].length) ||
307 Stoi(&buffer[format->field_offsets[O_SEC].offset], &clock_time->second,
308 format->field_offsets[O_SEC].length))
310 return CVT_FAIL|CVT_BADFMT;
314 unsigned char *f = &buffer[format->field_offsets[O_FLAGS].offset];
316 clock_time->usecond = 0;
317 clock_time->flags = PARSEB_S_LEAP;
319 if (clock_time->second == 60)
320 clock_time->flags |= PARSEB_LEAPSECOND;
323 * in the extended timecode format we have also the
324 * indication that the timecode is in UTC
325 * for compatibilty reasons we start at the USUAL
326 * offset (POWERUP flag) and know that the UTC indication
327 * is the character before the powerup flag
329 if ((format->flags & MBG_EXTENDED) && (f[-1] == 'U'))
334 clock_time->utcoffset = 0; /* UTC */
335 clock_time->flags |= PARSEB_UTC;
340 * only calculate UTC offset if MET/MED is in time code
341 * or we have the old time code format, where we do not
342 * know whether it is UTC time or MET/MED
343 * pray that nobody switches to UTC in the *old* standard time code
344 * ROMS !!!! The new ROMS have 'U' at the ZONE field - good.
346 switch (buffer[format->field_offsets[O_ZONE].offset])
349 clock_time->utcoffset = -1*60*60; /* MET */
353 clock_time->utcoffset = -2*60*60; /* MED */
360 clock_time->utcoffset = 0; /* UTC */
361 clock_time->flags |= PARSEB_UTC;
365 return CVT_FAIL|CVT_BADFMT;
370 * gather status flags
372 if (buffer[format->field_offsets[O_ZONE].offset] == 'S')
373 clock_time->flags |= PARSEB_DST;
376 clock_time->flags |= PARSEB_POWERUP;
379 clock_time->flags |= PARSEB_NOSYNC;
382 clock_time->flags |= PARSEB_ANNOUNCE;
385 * oncoming leap second
386 * 'a' code not confirmed - earth is not
387 * expected to speed up
390 clock_time->flags |= PARSEB_LEAPADD;
393 clock_time->flags |= PARSEB_LEAPDEL;
396 if (format->flags & MBG_EXTENDED)
398 clock_time->flags |= PARSEB_S_ANTENNA;
401 * DCF77 does not encode the direction -
402 * so we take the current default -
405 clock_time->flags &= ~PARSEB_LEAPDEL;
408 clock_time->flags |= PARSEB_LEAPADD;
411 clock_time->flags |= PARSEB_ALTERNATE;
421 * grep data from input stream
432 parseprintf(DD_PARSE, ("mbg_input(0x%lx, 0x%x, ...)\n", (long)parseio, ch));
437 parseprintf(DD_PARSE, ("mbg_input: STX seen\n"));
439 parseio->parse_index = 1;
440 parseio->parse_data[0] = ch;
441 parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
442 return PARSE_INP_SKIP;
445 parseprintf(DD_PARSE, ("mbg_input: ETX seen\n"));
446 if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
447 return parse_end(parseio);
452 return parse_addchar(parseio, ch);
460 * convert Meinberg GPS format
464 unsigned char *buffer,
466 struct format *format,
467 clocktime_t *clock_time,
471 if (!Strok(buffer, format->fixed_string))
473 return cvt_meinberg(buffer, size, format, clock_time, local);
477 if (Stoi(&buffer[format->field_offsets[O_DAY].offset], &clock_time->day,
478 format->field_offsets[O_DAY].length) ||
479 Stoi(&buffer[format->field_offsets[O_MONTH].offset], &clock_time->month,
480 format->field_offsets[O_MONTH].length) ||
481 Stoi(&buffer[format->field_offsets[O_YEAR].offset], &clock_time->year,
482 format->field_offsets[O_YEAR].length) ||
483 Stoi(&buffer[format->field_offsets[O_HOUR].offset], &clock_time->hour,
484 format->field_offsets[O_HOUR].length) ||
485 Stoi(&buffer[format->field_offsets[O_MIN].offset], &clock_time->minute,
486 format->field_offsets[O_MIN].length) ||
487 Stoi(&buffer[format->field_offsets[O_SEC].offset], &clock_time->second,
488 format->field_offsets[O_SEC].length))
490 return CVT_FAIL|CVT_BADFMT;
495 unsigned char *f = &buffer[format->field_offsets[O_FLAGS].offset];
497 clock_time->flags = PARSEB_S_LEAP|PARSEB_S_POSITION;
499 clock_time->usecond = 0;
502 * calculate UTC offset
504 if (Stoi(&buffer[format->field_offsets[O_UTCHOFFSET].offset], &h,
505 format->field_offsets[O_UTCHOFFSET].length))
507 return CVT_FAIL|CVT_BADFMT;
511 if (Stoi(&buffer[format->field_offsets[O_UTCMOFFSET].offset], &clock_time->utcoffset,
512 format->field_offsets[O_UTCMOFFSET].length))
514 return CVT_FAIL|CVT_BADFMT;
517 clock_time->utcoffset += TIMES60(h);
518 clock_time->utcoffset = TIMES60(clock_time->utcoffset);
520 if (buffer[format->field_offsets[O_UTCSOFFSET].offset] != '-')
522 clock_time->utcoffset = -clock_time->utcoffset;
527 * gather status flags
529 if (buffer[format->field_offsets[O_ZONE].offset] == 'S')
530 clock_time->flags |= PARSEB_DST;
532 if (clock_time->utcoffset == 0)
533 clock_time->flags |= PARSEB_UTC;
536 * no sv's seen - no time & position
539 clock_time->flags |= PARSEB_POWERUP;
542 * at least one sv seen - time (for last position)
545 clock_time->flags |= PARSEB_NOSYNC;
547 if (!(clock_time->flags & PARSEB_POWERUP))
548 clock_time->flags |= PARSEB_POSITION;
551 * oncoming zone switch
554 clock_time->flags |= PARSEB_ANNOUNCE;
557 * oncoming leap second
558 * 'a' code not confirmed - earth is not
559 * expected to speed up
562 clock_time->flags |= PARSEB_LEAPADD;
565 clock_time->flags |= PARSEB_LEAPDEL;
572 * this is the leap second
574 if ((f[6] == 'L') || (clock_time->second == 60))
575 clock_time->flags |= PARSEB_LEAPSECOND;
585 * grep binary data from input stream
594 CSUM calc_csum; /* used to compare the incoming csums */
596 struct msg_buf *msg_buf;
598 msg_buf = (struct msg_buf *)parseio->parse_pdata;
600 parseprintf(DD_PARSE, ("gps_input(0x%lx, 0x%x, ...)\n", (long)parseio, ch));
603 return PARSE_INP_SKIP;
605 if ( msg_buf->phase == MBG_NONE )
606 { /* not receiving yet */
610 parseprintf(DD_PARSE, ("gps_input: SOH seen\n"));
612 msg_buf->len = sizeof( header ); /* prepare to receive msg header */
613 msg_buf->phase = MBG_HEADER; /* receiving header */
617 parseprintf(DD_PARSE, ("gps_input: STX seen\n"));
620 msg_buf->phase = MBG_STRING; /* prepare to receive ASCII ETX delimited message */
621 parseio->parse_index = 1;
622 parseio->parse_data[0] = ch;
626 return PARSE_INP_SKIP; /* keep searching */
629 parseio->parse_dtime.parse_msglen = 1; /* reset buffer pointer */
630 parseio->parse_dtime.parse_msg[0] = ch; /* fill in first character */
631 parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
632 return PARSE_INP_SKIP;
635 /* SOH/STX has already been received */
637 /* save incoming character in both buffers if needbe */
638 if ((msg_buf->phase == MBG_STRING) &&
639 (parseio->parse_index < parseio->parse_dsize))
640 parseio->parse_data[parseio->parse_index++] = ch;
642 parseio->parse_dtime.parse_msg[parseio->parse_dtime.parse_msglen++] = ch;
644 if (parseio->parse_dtime.parse_msglen > sizeof(parseio->parse_dtime.parse_msg))
646 msg_buf->phase = MBG_NONE; /* buffer overflow - discard */
647 parseio->parse_data[parseio->parse_index] = '\0';
648 memcpy(parseio->parse_ldata, parseio->parse_data, (unsigned)(parseio->parse_index+1));
649 parseio->parse_ldsize = parseio->parse_index;
650 return PARSE_INP_DATA;
653 switch (msg_buf->phase)
659 if ( msg_buf->len ) /* transfer not complete */
660 return PARSE_INP_SKIP;
662 parseprintf(DD_PARSE, ("gps_input: %s complete\n", (msg_buf->phase == MBG_DATA) ? "data" : "header"));
667 if ((ch == ETX) || (parseio->parse_index >= parseio->parse_dsize))
669 msg_buf->phase = MBG_NONE;
670 parseprintf(DD_PARSE, ("gps_input: string complete\n"));
671 parseio->parse_data[parseio->parse_index] = '\0';
672 memcpy(parseio->parse_ldata, parseio->parse_data, (unsigned)(parseio->parse_index+1));
673 parseio->parse_ldsize = parseio->parse_index;
674 parseio->parse_index = 0;
675 return PARSE_INP_TIME;
679 return PARSE_INP_SKIP;
683 /* cnt == 0, so the header or the whole message is complete */
685 if ( msg_buf->phase == MBG_HEADER )
686 { /* header complete now */
687 unsigned char *datap = parseio->parse_dtime.parse_msg + 1;
689 get_mbg_header(&datap, &header);
691 parseprintf(DD_PARSE, ("gps_input: header: cmd 0x%x, len %d, dcsum 0x%x, hcsum 0x%x\n",
692 (int)header.gps_cmd, (int)header.gps_len, (int)header.gps_data_csum,
693 (int)header.gps_hdr_csum));
696 calc_csum = mbg_csum( (unsigned char *) parseio->parse_dtime.parse_msg + 1, (unsigned short)6 );
698 if ( calc_csum != header.gps_hdr_csum )
700 parseprintf(DD_PARSE, ("gps_input: header checksum mismatch expected 0x%x, got 0x%x\n",
701 (int)calc_csum, (int)mbg_csum( (unsigned char *) parseio->parse_dtime.parse_msg, (unsigned short)6 )));
703 msg_buf->phase = MBG_NONE; /* back to hunting mode */
704 return PARSE_INP_DATA; /* invalid header checksum received - pass up for detection */
707 if ((header.gps_len == 0) || /* no data to wait for */
708 (header.gps_len >= (sizeof (parseio->parse_dtime.parse_msg) - sizeof(header) - 1))) /* blows anything we have space for */
710 msg_buf->phase = MBG_NONE; /* back to hunting mode */
711 return (header.gps_len == 0) ? PARSE_INP_DATA : PARSE_INP_SKIP; /* message complete/throwaway */
714 parseprintf(DD_PARSE, ("gps_input: expecting %d bytes of data message\n", (int)header.gps_len));
716 msg_buf->len = header.gps_len;/* save number of bytes to wait for */
717 msg_buf->phase = MBG_DATA; /* flag header already complete */
718 return PARSE_INP_SKIP;
721 parseprintf(DD_PARSE, ("gps_input: message data complete\n"));
723 /* Header and data have been received. The header checksum has been */
726 msg_buf->phase = MBG_NONE; /* back to hunting mode */
727 return PARSE_INP_DATA; /* message complete, must be evaluated */
730 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_MEINBERG) */
732 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_MEINBERG) */
738 * Revision 4.12.2.1 2005/09/25 10:22:35 kardel
739 * cleanup buffer bounds
741 * Revision 4.12 2005/04/16 17:32:10 kardel
744 * Revision 4.11 2004/11/14 15:29:41 kardel
745 * support PPSAPI, upgrade Copyright to Berkeley style
747 * Revision 4.8 1999/11/28 09:13:50 kardel
750 * Revision 4.7 1999/02/21 11:09:14 kardel
753 * Revision 4.6 1998/06/14 21:09:36 kardel
756 * Revision 4.5 1998/06/13 15:18:54 kardel
757 * fix mem*() to b*() function macro emulation
759 * Revision 4.4 1998/06/13 12:03:23 kardel
760 * fix SYSV clock name clash
762 * Revision 4.3 1998/06/12 15:22:28 kardel
765 * Revision 4.2 1998/05/24 16:14:42 kardel
766 * support current Meinberg standard data formats
768 * Revision 4.1 1998/05/24 09:39:52 kardel
769 * implementation of the new IO handling model
771 * Revision 4.0 1998/04/10 19:45:29 kardel
772 * Start 4.0 release version numbering
774 * from V3 3.23 - log info deleted 1998/04/11 kardel