xref: /freebsd/contrib/ntp/libparse/clk_trimtaip.c (revision c98323078dede7579020518ec84cdcb478e5c142)
1 /*
2  * /src/NTP/ntp-4/libparse/clk_trimtaip.c,v 4.7 1999/11/28 09:13:51 kardel RELEASE_19991128_A
3  *
4  * clk_trimtaip.c,v 4.7 1999/11/28 09:13:51 kardel RELEASE_19991128_A
5  *
6  * Trimble SV6 clock support - several collected codepieces
7  */
8 
9 #ifdef HAVE_CONFIG_H
10 # include <config.h>
11 #endif
12 
13 #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_TRIMTAIP)
14 
15 #include "ntp_fp.h"
16 #include "ntp_unixtime.h"
17 #include "ntp_calendar.h"
18 
19 #include "parse.h"
20 
21 #ifndef PARSESTREAM
22 #include "ntp_stdlib.h"
23 #include <stdio.h>
24 #else
25 #include "sys/parsestreams.h"
26 extern void printf P((const char *, ...));
27 #endif
28 
29 /*	0000000000111111111122222222223333333	/ char
30  *	0123456789012345678901234567890123456	\ posn
31  *	>RTMhhmmssdddDDMMYYYYoodnnvrrrrr;*xx<	Actual
32  *	----33445566600112222BB7__-_____--99-	Parse
33  *	>RTM                      1     ;*  <",	Check
34  */
35 
36 #define	hexval(x) (('0' <= (x) && (x) <= '9') ? (x) - '0' : \
37 		   ('a' <= (x) && (x) <= 'f') ? (x) - 'a' + 10 : \
38 		   ('A' <= (x) && (x) <= 'F') ? (x) - 'A' + 10 : \
39 		   -1)
40 #define	O_USEC		O_WDAY
41 #define	O_GPSFIX	O_FLAGS
42 #define	O_CHKSUM	O_UTCHOFFSET
43      static struct format trimsv6_fmt =
44 { { { 13, 2 }, {15, 2}, { 17, 4}, /* Day, Month, Year */
45     {  4, 2 }, { 6, 2}, {  8, 2}, /* Hour, Minute, Second */
46     { 10, 3 }, {23, 1}, {  0, 0}, /* uSec, FIXes (WeekDAY, FLAGS, ZONE) */
47     { 34, 2 }, { 0, 0}, { 21, 2}, /* cksum, -, utcS (UTC[HMS]OFFSET) */
48 },
49   (const unsigned char *)">RTM                      1     ;*  <",
50   0
51 };
52 
53 static unsigned long cvt_trimtaip P((unsigned char *, int, struct format *, clocktime_t *, void *));
54 static unsigned long inp_trimtaip P((parse_t *, unsigned int, timestamp_t *));
55 
56 clockformat_t clock_trimtaip =
57 {
58   inp_trimtaip,			/* no input handling */
59   cvt_trimtaip,			/* Trimble conversion */
60   pps_one,			/* easy PPS monitoring */
61   (void *)&trimsv6_fmt,		/* conversion configuration */
62   "Trimble TAIP",
63   37,				/* string buffer */
64   0				/* no private data */
65 };
66 
67 static unsigned long
68 cvt_trimtaip(
69 	     unsigned char *buffer,
70 	     int            size,
71 	     struct format *format,
72 	     clocktime_t   *clock_time,
73 	     void          *local
74 	     )
75 {
76 	long gpsfix;
77 	u_char calc_csum = 0;
78 	long   recv_csum;
79 	int	 i;
80 
81 	if (!Strok(buffer, format->fixed_string)) return CVT_NONE;
82 #define	OFFS(x) format->field_offsets[(x)].offset
83 #define	STOI(x, y) \
84 	Stoi(&buffer[OFFS(x)], y, \
85 	     format->field_offsets[(x)].length)
86 		if (	STOI(O_DAY,	&clock_time->day)	||
87 			STOI(O_MONTH,	&clock_time->month)	||
88 			STOI(O_YEAR,	&clock_time->year)	||
89 			STOI(O_HOUR,	&clock_time->hour)	||
90 			STOI(O_MIN,	&clock_time->minute)	||
91 			STOI(O_SEC,	&clock_time->second)	||
92 			STOI(O_USEC,	&clock_time->usecond)||
93 			STOI(O_GPSFIX,	&gpsfix)
94 			) return CVT_FAIL|CVT_BADFMT;
95 
96 	clock_time->usecond *= 1000;
97 	/* Check that the checksum is right */
98 	for (i=OFFS(O_CHKSUM)-1; i >= 0; i--) calc_csum ^= buffer[i];
99 	recv_csum =	(hexval(buffer[OFFS(O_CHKSUM)]) << 4) |
100 		hexval(buffer[OFFS(O_CHKSUM)+1]);
101 	if (recv_csum < 0) return CVT_FAIL|CVT_BADTIME;
102 	if (((u_char) recv_csum) != calc_csum) return CVT_FAIL|CVT_BADTIME;
103 
104 	clock_time->utcoffset = 0;
105 
106 	/* What should flags be set to ? */
107 	clock_time->flags = PARSEB_UTC;
108 
109 	/* if the current GPS fix is 9 (unknown), reject */
110 	if (0 > gpsfix || gpsfix > 9) clock_time->flags |= PARSEB_POWERUP;
111 
112 	return CVT_OK;
113 }
114 
115 /*
116  * inp_trimtaip
117  *
118  * grep data from input stream
119  */
120 static u_long
121 inp_trimtaip(
122 	     parse_t      *parseio,
123 	     unsigned int  ch,
124 	     timestamp_t  *tstamp
125 	  )
126 {
127 	unsigned int rtc;
128 
129 	parseprintf(DD_PARSE, ("inp_trimtaip(0x%lx, 0x%x, ...)\n", (long)parseio, ch));
130 
131 	switch (ch)
132 	{
133 	case '>':
134 		parseprintf(DD_PARSE, ("inp_trimptaip: START seen\n"));
135 
136 		parseio->parse_index = 1;
137 		parseio->parse_data[0] = ch;
138 		parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
139 		return PARSE_INP_SKIP;
140 
141 	case '<':
142 		parseprintf(DD_PARSE, ("inp_trimtaip: END seen\n"));
143 		if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
144 			return parse_end(parseio);
145 		else
146 			return rtc;
147 
148 
149 	default:
150 		return parse_addchar(parseio, ch);
151 	}
152 }
153 
154 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTAIP) */
155 int clk_trimtaip_bs;
156 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTAIP) */
157 
158 /*
159  * History:
160  *
161  * clk_trimtaip.c,v
162  * Revision 4.7  1999/11/28 09:13:51  kardel
163  * RECON_4_0_98F
164  *
165  * Revision 4.6  1998/08/16 18:46:27  kardel
166  * (clock_trimtaip =): changed format name
167  *
168  * Revision 4.5  1998/06/14 21:09:38  kardel
169  * Sun acc cleanup
170  *
171  * Revision 4.4  1998/06/13 12:06:57  kardel
172  * fix SYSV clock name clash
173  *
174  * Revision 4.3  1998/06/12 15:22:29  kardel
175  * fix prototypes
176  *
177  * Revision 4.2  1998/06/12 09:13:26  kardel
178  * conditional compile macros fixed
179  * printf prototype
180  *
181  * Revision 4.1  1998/05/24 09:39:54  kardel
182  * implementation of the new IO handling model
183  *
184  * Revision 4.0  1998/04/10 19:45:31  kardel
185  * Start 4.0 release version numbering
186  *
187  * from V3 1.4 log info deleted 1998/04/11 kardel
188  */
189 
190