154359Sroberto/*
2182007Sroberto * /src/NTP/ntp4-dev/libparse/clk_trimtaip.c,v 4.11 2005/04/16 17:32:10 kardel RELEASE_20050508_A
354359Sroberto *
4182007Sroberto * clk_trimtaip.c,v 4.11 2005/04/16 17:32:10 kardel RELEASE_20050508_A
554359Sroberto *
654359Sroberto * Trimble SV6 clock support - several collected codepieces
7182007Sroberto *
8182007Sroberto * Copyright (c) 1995-2005 by Frank Kardel <kardel <AT> ntp.org>
9282408Scy * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universitaet Erlangen-Nuernberg, Germany
10182007Sroberto *
11182007Sroberto * Redistribution and use in source and binary forms, with or without
12182007Sroberto * modification, are permitted provided that the following conditions
13182007Sroberto * are met:
14182007Sroberto * 1. Redistributions of source code must retain the above copyright
15182007Sroberto *    notice, this list of conditions and the following disclaimer.
16182007Sroberto * 2. Redistributions in binary form must reproduce the above copyright
17182007Sroberto *    notice, this list of conditions and the following disclaimer in the
18182007Sroberto *    documentation and/or other materials provided with the distribution.
19182007Sroberto * 3. Neither the name of the author nor the names of its contributors
20182007Sroberto *    may be used to endorse or promote products derived from this software
21182007Sroberto *    without specific prior written permission.
22182007Sroberto *
23182007Sroberto * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24182007Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25182007Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26182007Sroberto * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27182007Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28182007Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29182007Sroberto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30182007Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31182007Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32182007Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33182007Sroberto * SUCH DAMAGE.
34182007Sroberto *
3554359Sroberto */
3654359Sroberto
3754359Sroberto#ifdef HAVE_CONFIG_H
3854359Sroberto# include <config.h>
3954359Sroberto#endif
4054359Sroberto
4154359Sroberto#if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_TRIMTAIP)
4254359Sroberto
4354359Sroberto#include "ntp_fp.h"
4454359Sroberto#include "ntp_unixtime.h"
4554359Sroberto#include "ntp_calendar.h"
4654359Sroberto
4754359Sroberto#include "parse.h"
4854359Sroberto
4954359Sroberto#ifndef PARSESTREAM
5054359Sroberto#include "ntp_stdlib.h"
5154359Sroberto#include <stdio.h>
5254359Sroberto#else
5354359Sroberto#include "sys/parsestreams.h"
54280849Scyextern int printf (const char *, ...);
5554359Sroberto#endif
5654359Sroberto
5754359Sroberto/*	0000000000111111111122222222223333333	/ char
5854359Sroberto *	0123456789012345678901234567890123456	\ posn
5954359Sroberto *	>RTMhhmmssdddDDMMYYYYoodnnvrrrrr;*xx<	Actual
6054359Sroberto *	----33445566600112222BB7__-_____--99-	Parse
6154359Sroberto *	>RTM                      1     ;*  <",	Check
6254359Sroberto */
6354359Sroberto
6454359Sroberto#define	hexval(x) (('0' <= (x) && (x) <= '9') ? (x) - '0' : \
6554359Sroberto		   ('a' <= (x) && (x) <= 'f') ? (x) - 'a' + 10 : \
6654359Sroberto		   ('A' <= (x) && (x) <= 'F') ? (x) - 'A' + 10 : \
6754359Sroberto		   -1)
6854359Sroberto#define	O_USEC		O_WDAY
6954359Sroberto#define	O_GPSFIX	O_FLAGS
7054359Sroberto#define	O_CHKSUM	O_UTCHOFFSET
7154359Sroberto     static struct format trimsv6_fmt =
7254359Sroberto{ { { 13, 2 }, {15, 2}, { 17, 4}, /* Day, Month, Year */
7354359Sroberto    {  4, 2 }, { 6, 2}, {  8, 2}, /* Hour, Minute, Second */
7454359Sroberto    { 10, 3 }, {23, 1}, {  0, 0}, /* uSec, FIXes (WeekDAY, FLAGS, ZONE) */
7554359Sroberto    { 34, 2 }, { 0, 0}, { 21, 2}, /* cksum, -, utcS (UTC[HMS]OFFSET) */
7654359Sroberto},
7754359Sroberto  (const unsigned char *)">RTM                      1     ;*  <",
7854359Sroberto  0
7954359Sroberto};
8054359Sroberto
81282408Scystatic parse_cvt_fnc_t cvt_trimtaip;
82282408Scystatic parse_inp_fnc_t inp_trimtaip;
8354359Sroberto
8454359Srobertoclockformat_t clock_trimtaip =
8554359Sroberto{
8654359Sroberto  inp_trimtaip,			/* no input handling */
8754359Sroberto  cvt_trimtaip,			/* Trimble conversion */
8854359Sroberto  pps_one,			/* easy PPS monitoring */
8954359Sroberto  (void *)&trimsv6_fmt,		/* conversion configuration */
9054359Sroberto  "Trimble TAIP",
9154359Sroberto  37,				/* string buffer */
9254359Sroberto  0				/* no private data */
9354359Sroberto};
9454359Sroberto
95282408Scy/* parse_cvt_fnc_t cvt_trimtaip */
96282408Scystatic u_long
9754359Srobertocvt_trimtaip(
9854359Sroberto	     unsigned char *buffer,
9954359Sroberto	     int            size,
10054359Sroberto	     struct format *format,
10154359Sroberto	     clocktime_t   *clock_time,
10254359Sroberto	     void          *local
10354359Sroberto	     )
10454359Sroberto{
10554359Sroberto	long gpsfix;
10654359Sroberto	u_char calc_csum = 0;
10754359Sroberto	long   recv_csum;
10854359Sroberto	int	 i;
10954359Sroberto
11054359Sroberto	if (!Strok(buffer, format->fixed_string)) return CVT_NONE;
11154359Sroberto#define	OFFS(x) format->field_offsets[(x)].offset
11254359Sroberto#define	STOI(x, y) \
11354359Sroberto	Stoi(&buffer[OFFS(x)], y, \
11454359Sroberto	     format->field_offsets[(x)].length)
11554359Sroberto		if (	STOI(O_DAY,	&clock_time->day)	||
11654359Sroberto			STOI(O_MONTH,	&clock_time->month)	||
11754359Sroberto			STOI(O_YEAR,	&clock_time->year)	||
11854359Sroberto			STOI(O_HOUR,	&clock_time->hour)	||
11954359Sroberto			STOI(O_MIN,	&clock_time->minute)	||
12054359Sroberto			STOI(O_SEC,	&clock_time->second)	||
12154359Sroberto			STOI(O_USEC,	&clock_time->usecond)||
12254359Sroberto			STOI(O_GPSFIX,	&gpsfix)
12354359Sroberto			) return CVT_FAIL|CVT_BADFMT;
12454359Sroberto
12554359Sroberto	clock_time->usecond *= 1000;
12654359Sroberto	/* Check that the checksum is right */
12754359Sroberto	for (i=OFFS(O_CHKSUM)-1; i >= 0; i--) calc_csum ^= buffer[i];
12854359Sroberto	recv_csum =	(hexval(buffer[OFFS(O_CHKSUM)]) << 4) |
12954359Sroberto		hexval(buffer[OFFS(O_CHKSUM)+1]);
13054359Sroberto	if (recv_csum < 0) return CVT_FAIL|CVT_BADTIME;
13154359Sroberto	if (((u_char) recv_csum) != calc_csum) return CVT_FAIL|CVT_BADTIME;
13254359Sroberto
13354359Sroberto	clock_time->utcoffset = 0;
13454359Sroberto
13554359Sroberto	/* What should flags be set to ? */
13654359Sroberto	clock_time->flags = PARSEB_UTC;
13754359Sroberto
13854359Sroberto	/* if the current GPS fix is 9 (unknown), reject */
13954359Sroberto	if (0 > gpsfix || gpsfix > 9) clock_time->flags |= PARSEB_POWERUP;
14054359Sroberto
14154359Sroberto	return CVT_OK;
14254359Sroberto}
14354359Sroberto
14454359Sroberto/*
145282408Scy * parse_inp_fnc_t inp_trimtaip
14654359Sroberto *
147282408Scy * grab data from input stream
14854359Sroberto */
14954359Srobertostatic u_long
15054359Srobertoinp_trimtaip(
15154359Sroberto	     parse_t      *parseio,
152282408Scy	     char         ch,
15354359Sroberto	     timestamp_t  *tstamp
15454359Sroberto	  )
15554359Sroberto{
15654359Sroberto	unsigned int rtc;
157282408Scy
158293423Sdelphij	parseprintf(DD_PARSE, ("inp_trimtaip(0x%p, 0x%x, ...)\n", (void*)parseio, ch));
159282408Scy
16054359Sroberto	switch (ch)
16154359Sroberto	{
16254359Sroberto	case '>':
16354359Sroberto		parseprintf(DD_PARSE, ("inp_trimptaip: START seen\n"));
164282408Scy
16554359Sroberto		parseio->parse_index = 1;
16654359Sroberto		parseio->parse_data[0] = ch;
16754359Sroberto		parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
16854359Sroberto		return PARSE_INP_SKIP;
169282408Scy
17054359Sroberto	case '<':
17154359Sroberto		parseprintf(DD_PARSE, ("inp_trimtaip: END seen\n"));
17254359Sroberto		if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
17354359Sroberto			return parse_end(parseio);
17454359Sroberto		else
17554359Sroberto			return rtc;
17654359Sroberto
17754359Sroberto
17854359Sroberto	default:
17954359Sroberto		return parse_addchar(parseio, ch);
18054359Sroberto	}
18154359Sroberto}
18254359Sroberto
18354359Sroberto#else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTAIP) */
18454359Srobertoint clk_trimtaip_bs;
18554359Sroberto#endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTAIP) */
18654359Sroberto
18754359Sroberto/*
18854359Sroberto * History:
18954359Sroberto *
19054359Sroberto * clk_trimtaip.c,v
191182007Sroberto * Revision 4.11  2005/04/16 17:32:10  kardel
192182007Sroberto * update copyright
193182007Sroberto *
194182007Sroberto * Revision 4.10  2004/11/14 15:29:41  kardel
195182007Sroberto * support PPSAPI, upgrade Copyright to Berkeley style
196182007Sroberto *
19756746Sroberto * Revision 4.7  1999/11/28 09:13:51  kardel
19856746Sroberto * RECON_4_0_98F
19956746Sroberto *
20054359Sroberto * Revision 4.6  1998/08/16 18:46:27  kardel
20154359Sroberto * (clock_trimtaip =): changed format name
20254359Sroberto *
20354359Sroberto * Revision 4.5  1998/06/14 21:09:38  kardel
20454359Sroberto * Sun acc cleanup
20554359Sroberto *
20654359Sroberto * Revision 4.4  1998/06/13 12:06:57  kardel
20754359Sroberto * fix SYSV clock name clash
20854359Sroberto *
20954359Sroberto * Revision 4.3  1998/06/12 15:22:29  kardel
21054359Sroberto * fix prototypes
21154359Sroberto *
21254359Sroberto * Revision 4.2  1998/06/12 09:13:26  kardel
21354359Sroberto * conditional compile macros fixed
21454359Sroberto * printf prototype
21554359Sroberto *
21654359Sroberto * Revision 4.1  1998/05/24 09:39:54  kardel
21754359Sroberto * implementation of the new IO handling model
21854359Sroberto *
21954359Sroberto * Revision 4.0  1998/04/10 19:45:31  kardel
22054359Sroberto * Start 4.0 release version numbering
22154359Sroberto *
22254359Sroberto * from V3 1.4 log info deleted 1998/04/11 kardel
22354359Sroberto */
22454359Sroberto
225