Home | History | Annotate | Line # | Download | only in libparse
clk_wharton.c revision 1.6
      1  1.6  christos /*	$NetBSD: clk_wharton.c,v 1.6 2020/05/25 20:47:25 christos Exp $	*/
      2  1.1    kardel 
      3  1.1    kardel /*
      4  1.1    kardel  * /src/NTP/ntp4-dev/libparse/clk_wharton.c,v 4.2 2004/11/14 15:29:41 kardel RELEASE_20050508_A
      5  1.1    kardel  *
      6  1.1    kardel  * clk_wharton.c,v 4.2 2004/11/14 15:29:41 kardel RELEASE_20050508_A
      7  1.1    kardel  *
      8  1.1    kardel  * From Philippe De Muyter <phdm (at) macqel.be>, 1999
      9  1.1    kardel  */
     10  1.1    kardel #ifdef HAVE_CONFIG_H
     11  1.1    kardel #include <config.h>
     12  1.1    kardel #endif
     13  1.1    kardel 
     14  1.1    kardel #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_WHARTON_400A)
     15  1.1    kardel /*
     16  1.1    kardel  * Support for WHARTON 400A Series clock + 404.2 serial interface.
     17  1.1    kardel  *
     18  1.1    kardel  * Copyright (C) 1999, 2000 by Philippe De Muyter <phdm (at) macqel.be>
     19  1.1    kardel  *
     20  1.1    kardel  * This program is distributed in the hope that it will be useful, but WITHOUT
     21  1.1    kardel  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     22  1.1    kardel  * FITNESS FOR A PARTICULAR PURPOSE.
     23  1.1    kardel  *
     24  1.1    kardel  */
     25  1.1    kardel 
     26  1.1    kardel #include "ntp_fp.h"
     27  1.1    kardel #include "ascii.h"
     28  1.1    kardel #include "parse.h"
     29  1.1    kardel 
     30  1.1    kardel #ifndef PARSESTREAM
     31  1.1    kardel #include "ntp_stdlib.h"
     32  1.1    kardel #include <stdio.h>
     33  1.1    kardel #else
     34  1.1    kardel #include "sys/parsestreams.h"
     35  1.1    kardel extern void printf (const char *, ...);
     36  1.1    kardel #endif
     37  1.1    kardel 
     38  1.1    kardel /*
     39  1.1    kardel  * In private e-mail alastair (at) wharton.co.uk said :
     40  1.1    kardel  * "If you are going to use the 400A and 404.2 system [for ntp] I recommend
     41  1.1    kardel  * that you set the 400A to output the message every second.  The start of
     42  1.1    kardel  * transmission of the first byte of the message is synchronised to the
     43  1.1    kardel  * second edge."
     44  1.1    kardel  * The WHARTON 400A Series is able to send date/time serial messages
     45  1.1    kardel  * in 7 output formats.  We use format 1 here because it is the shortest.
     46  1.1    kardel  * For use with this driver, the WHARTON 400A Series clock must be set-up
     47  1.1    kardel  * as follows :
     48  1.1    kardel  *					Programmable	Selected
     49  1.1    kardel  *					Option No	Option
     50  1.1    kardel  *	BST or CET display		3		9 or 11
     51  1.1    kardel  *	No external controller		7		0
     52  1.1    kardel  *	Serial Output Format 1		9		1
     53  1.1    kardel  *	Baud rate 9600 bps		10		96
     54  1.1    kardel  *	Bit length 8 bits		11		8
     55  1.1    kardel  *	Parity even			12		E
     56  1.1    kardel  *
     57  1.1    kardel  * WHARTON 400A Series output format 1 is as follows :
     58  1.1    kardel  *
     59  1.1    kardel  * Timestamp	STXssmmhhDDMMYYSETX
     60  1.1    kardel  * Pos		0  12345678901234
     61  1.1    kardel  *		0  00000000011111
     62  1.1    kardel  *
     63  1.1    kardel  *	STX	start transmission (ASCII 0x02)
     64  1.1    kardel  *	ETX	end transmission (ASCII 0x03)
     65  1.1    kardel  *	ss	Second expressed in reversed decimal (units then tens)
     66  1.1    kardel  *	mm	Minute expressed in reversed decimal
     67  1.1    kardel  *	hh	Hour expressed in reversed decimal
     68  1.1    kardel  *	DD	Day of month expressed in reversed decimal
     69  1.1    kardel  *	MM	Month expressed in reversed decimal (January is 1)
     70  1.1    kardel  *	YY	Year (without century) expressed in reversed decimal
     71  1.1    kardel  *	S	Status byte : 0x30 +
     72  1.1    kardel  *			bit 0	0 = MSF source		1 = DCF source
     73  1.1    kardel  *			bit 1	0 = Winter time		1 = Summer time
     74  1.1    kardel  *			bit 2	0 = not synchronised	1 = synchronised
     75  1.1    kardel  *			bit 3	0 = no early warning	1 = early warning
     76  1.1    kardel  *
     77  1.1    kardel  */
     78  1.1    kardel 
     79  1.3  christos static parse_cvt_fnc_t cvt_wharton_400a;
     80  1.3  christos static parse_inp_fnc_t inp_wharton_400a;
     81  1.3  christos 
     82  1.1    kardel /*
     83  1.3  christos  * parse_cvt_fnc_t cvt_wharton_400a
     84  1.1    kardel  *
     85  1.1    kardel  * convert simple type format
     86  1.1    kardel  */
     87  1.1    kardel static          u_long
     88  1.1    kardel cvt_wharton_400a(
     89  1.1    kardel 	unsigned char *buffer,
     90  1.1    kardel 	int            size,
     91  1.1    kardel 	struct format *format,
     92  1.1    kardel 	clocktime_t   *clock_time,
     93  1.1    kardel 	void          *local
     94  1.1    kardel 	)
     95  1.1    kardel {
     96  1.1    kardel 	int	i;
     97  1.1    kardel 
     98  1.1    kardel 	/* The given `size' includes a terminating null-character. */
     99  1.2  christos 	if (size != 15 || buffer[0] != STX || buffer[14] != ETX
    100  1.1    kardel 	    || buffer[13] < '0' || buffer[13] > ('0' + 0xf))
    101  1.1    kardel 		return CVT_NONE;
    102  1.1    kardel 	for (i = 1; i < 13; i += 1)
    103  1.1    kardel 		if (buffer[i] < '0' || buffer[i] > '9')
    104  1.1    kardel 			return CVT_NONE;
    105  1.1    kardel 	clock_time->second = (buffer[2] - '0') * 10 + buffer[1] - '0';
    106  1.1    kardel 	clock_time->minute = (buffer[4] - '0') * 10 + buffer[3] - '0';
    107  1.1    kardel 	clock_time->hour   = (buffer[6] - '0') * 10 + buffer[5] - '0';
    108  1.1    kardel 	clock_time->day    = (buffer[8] - '0') * 10 + buffer[7] - '0';
    109  1.1    kardel 	clock_time->month  = (buffer[10] - '0') * 10 + buffer[9] - '0';
    110  1.1    kardel 	clock_time->year   = (buffer[12] - '0') * 10 + buffer[11] - '0';
    111  1.1    kardel 	clock_time->usecond = 0;
    112  1.1    kardel 	if (buffer[13] & 0x1) /* We have CET time */
    113  1.1    kardel 		clock_time->utcoffset = -1*60*60;
    114  1.1    kardel 	else		/* We have BST time */
    115  1.1    kardel 		clock_time->utcoffset = 0;
    116  1.1    kardel 	if (buffer[13] & 0x2) {
    117  1.1    kardel 		clock_time->flags |= PARSEB_DST;
    118  1.1    kardel 		clock_time->utcoffset += -1*60*60;
    119  1.1    kardel 	}
    120  1.1    kardel 	if (!(buffer[13] & 0x4))
    121  1.1    kardel 		clock_time->flags |= PARSEB_NOSYNC;
    122  1.1    kardel 	if (buffer[13] & 0x8)
    123  1.1    kardel 		clock_time->flags |= PARSEB_ANNOUNCE;
    124  1.1    kardel 
    125  1.1    kardel 	return CVT_OK;
    126  1.1    kardel }
    127  1.1    kardel 
    128  1.1    kardel /*
    129  1.3  christos  * parse_inp_fnc_t inp_wharton_400a
    130  1.1    kardel  *
    131  1.3  christos  * grab data from input stream
    132  1.1    kardel  */
    133  1.1    kardel static u_long
    134  1.1    kardel inp_wharton_400a(
    135  1.1    kardel 	      parse_t      *parseio,
    136  1.3  christos 	      char         ch,
    137  1.1    kardel 	      timestamp_t  *tstamp
    138  1.1    kardel 	      )
    139  1.1    kardel {
    140  1.1    kardel 	unsigned int rtc;
    141  1.1    kardel 
    142  1.5  christos 	parseprintf(DD_PARSE, ("inp_wharton_400a(0x%p, 0x%x, ...)\n", (void*)parseio, ch));
    143  1.1    kardel 
    144  1.1    kardel 	switch (ch)
    145  1.1    kardel 	{
    146  1.1    kardel 	case STX:
    147  1.1    kardel 		parseprintf(DD_PARSE, ("inp_wharton_400a: STX seen\n"));
    148  1.1    kardel 
    149  1.1    kardel 		parseio->parse_index = 1;
    150  1.1    kardel 		parseio->parse_data[0] = ch;
    151  1.1    kardel 		parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
    152  1.1    kardel 		return PARSE_INP_SKIP;
    153  1.1    kardel 
    154  1.1    kardel 	case ETX:
    155  1.1    kardel 		parseprintf(DD_PARSE, ("inp_wharton_400a: ETX seen\n"));
    156  1.1    kardel 		if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
    157  1.1    kardel 			return parse_end(parseio);
    158  1.1    kardel 		else
    159  1.1    kardel 			return rtc;
    160  1.1    kardel 
    161  1.1    kardel 	default:
    162  1.1    kardel 		return parse_addchar(parseio, ch);
    163  1.1    kardel 	}
    164  1.1    kardel }
    165  1.1    kardel 
    166  1.1    kardel clockformat_t   clock_wharton_400a =
    167  1.1    kardel {
    168  1.1    kardel 	inp_wharton_400a,	/* input handling function */
    169  1.1    kardel 	cvt_wharton_400a,	/* conversion function */
    170  1.1    kardel 	0,			/* no PPS monitoring */
    171  1.1    kardel 	0,			/* conversion configuration */
    172  1.1    kardel 	"WHARTON 400A Series clock Output Format 1",	/* String format name */
    173  1.1    kardel 	15,			/* string buffer */
    174  1.3  christos 	0			/* no private data (complete packets) */
    175  1.1    kardel };
    176  1.1    kardel 
    177  1.1    kardel #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_WHARTON_400A) */
    178  1.1    kardel int clk_wharton_400a_bs;
    179  1.1    kardel #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_WHARTON_400A) */
    180  1.1    kardel 
    181  1.1    kardel /*
    182  1.1    kardel  * clk_wharton.c,v
    183  1.1    kardel  * Revision 4.1  1999/02/28 15:27:24  kardel
    184  1.1    kardel  * wharton clock integration
    185  1.1    kardel  *
    186  1.1    kardel  */
    187