clk_trimtaip.c revision 280849
1169691Skan/* 2169691Skan * /src/NTP/ntp4-dev/libparse/clk_trimtaip.c,v 4.11 2005/04/16 17:32:10 kardel RELEASE_20050508_A 3169691Skan * 4169691Skan * clk_trimtaip.c,v 4.11 2005/04/16 17:32:10 kardel RELEASE_20050508_A 5169691Skan * 6169691Skan * Trimble SV6 clock support - several collected codepieces 7169691Skan * 8169691Skan * Copyright (c) 1995-2005 by Frank Kardel <kardel <AT> ntp.org> 9169691Skan * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universit�t Erlangen-N�rnberg, Germany 10169691Skan * 11169691Skan * Redistribution and use in source and binary forms, with or without 12169691Skan * modification, are permitted provided that the following conditions 13169691Skan * are met: 14169691Skan * 1. Redistributions of source code must retain the above copyright 15169691Skan * notice, this list of conditions and the following disclaimer. 16169691Skan * 2. Redistributions in binary form must reproduce the above copyright 17169691Skan * notice, this list of conditions and the following disclaimer in the 18169691Skan * documentation and/or other materials provided with the distribution. 19169691Skan * 3. Neither the name of the author nor the names of its contributors 20169691Skan * may be used to endorse or promote products derived from this software 21169691Skan * without specific prior written permission. 22169691Skan * 23169691Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24169691Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25169691Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26169691Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27169691Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28169691Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29169691Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30169691Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31169691Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32169691Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33169691Skan * SUCH DAMAGE. 34169691Skan * 35169691Skan */ 36169691Skan 37169691Skan#ifdef HAVE_CONFIG_H 38169691Skan# include <config.h> 39169691Skan#endif 40169691Skan 41169691Skan#if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_TRIMTAIP) 42169691Skan 43169691Skan#include "ntp_fp.h" 44169691Skan#include "ntp_unixtime.h" 45169691Skan#include "ntp_calendar.h" 46169691Skan 47169691Skan#include "parse.h" 48169691Skan 49169691Skan#ifndef PARSESTREAM 50169691Skan#include "ntp_stdlib.h" 51169691Skan#include <stdio.h> 52169691Skan#else 53169691Skan#include "sys/parsestreams.h" 54169691Skanextern int printf (const char *, ...); 55169691Skan#endif 56169691Skan 57169691Skan/* 0000000000111111111122222222223333333 / char 58169691Skan * 0123456789012345678901234567890123456 \ posn 59169691Skan * >RTMhhmmssdddDDMMYYYYoodnnvrrrrr;*xx< Actual 60169691Skan * ----33445566600112222BB7__-_____--99- Parse 61169691Skan * >RTM 1 ;* <", Check 62169691Skan */ 63169691Skan 64169691Skan#define hexval(x) (('0' <= (x) && (x) <= '9') ? (x) - '0' : \ 65169691Skan ('a' <= (x) && (x) <= 'f') ? (x) - 'a' + 10 : \ 66169691Skan ('A' <= (x) && (x) <= 'F') ? (x) - 'A' + 10 : \ 67169691Skan -1) 68169691Skan#define O_USEC O_WDAY 69169691Skan#define O_GPSFIX O_FLAGS 70169691Skan#define O_CHKSUM O_UTCHOFFSET 71169691Skan static struct format trimsv6_fmt = 72169691Skan{ { { 13, 2 }, {15, 2}, { 17, 4}, /* Day, Month, Year */ 73169691Skan { 4, 2 }, { 6, 2}, { 8, 2}, /* Hour, Minute, Second */ 74 { 10, 3 }, {23, 1}, { 0, 0}, /* uSec, FIXes (WeekDAY, FLAGS, ZONE) */ 75 { 34, 2 }, { 0, 0}, { 21, 2}, /* cksum, -, utcS (UTC[HMS]OFFSET) */ 76}, 77 (const unsigned char *)">RTM 1 ;* <", 78 0 79}; 80 81static unsigned long cvt_trimtaip (unsigned char *, int, struct format *, clocktime_t *, void *); 82static unsigned long inp_trimtaip (parse_t *, unsigned int, timestamp_t *); 83 84clockformat_t clock_trimtaip = 85{ 86 inp_trimtaip, /* no input handling */ 87 cvt_trimtaip, /* Trimble conversion */ 88 pps_one, /* easy PPS monitoring */ 89 (void *)&trimsv6_fmt, /* conversion configuration */ 90 "Trimble TAIP", 91 37, /* string buffer */ 92 0 /* no private data */ 93}; 94 95static unsigned long 96cvt_trimtaip( 97 unsigned char *buffer, 98 int size, 99 struct format *format, 100 clocktime_t *clock_time, 101 void *local 102 ) 103{ 104 long gpsfix; 105 u_char calc_csum = 0; 106 long recv_csum; 107 int i; 108 109 if (!Strok(buffer, format->fixed_string)) return CVT_NONE; 110#define OFFS(x) format->field_offsets[(x)].offset 111#define STOI(x, y) \ 112 Stoi(&buffer[OFFS(x)], y, \ 113 format->field_offsets[(x)].length) 114 if ( STOI(O_DAY, &clock_time->day) || 115 STOI(O_MONTH, &clock_time->month) || 116 STOI(O_YEAR, &clock_time->year) || 117 STOI(O_HOUR, &clock_time->hour) || 118 STOI(O_MIN, &clock_time->minute) || 119 STOI(O_SEC, &clock_time->second) || 120 STOI(O_USEC, &clock_time->usecond)|| 121 STOI(O_GPSFIX, &gpsfix) 122 ) return CVT_FAIL|CVT_BADFMT; 123 124 clock_time->usecond *= 1000; 125 /* Check that the checksum is right */ 126 for (i=OFFS(O_CHKSUM)-1; i >= 0; i--) calc_csum ^= buffer[i]; 127 recv_csum = (hexval(buffer[OFFS(O_CHKSUM)]) << 4) | 128 hexval(buffer[OFFS(O_CHKSUM)+1]); 129 if (recv_csum < 0) return CVT_FAIL|CVT_BADTIME; 130 if (((u_char) recv_csum) != calc_csum) return CVT_FAIL|CVT_BADTIME; 131 132 clock_time->utcoffset = 0; 133 134 /* What should flags be set to ? */ 135 clock_time->flags = PARSEB_UTC; 136 137 /* if the current GPS fix is 9 (unknown), reject */ 138 if (0 > gpsfix || gpsfix > 9) clock_time->flags |= PARSEB_POWERUP; 139 140 return CVT_OK; 141} 142 143/* 144 * inp_trimtaip 145 * 146 * grep data from input stream 147 */ 148static u_long 149inp_trimtaip( 150 parse_t *parseio, 151 unsigned int ch, 152 timestamp_t *tstamp 153 ) 154{ 155 unsigned int rtc; 156 157 parseprintf(DD_PARSE, ("inp_trimtaip(0x%lx, 0x%x, ...)\n", (long)parseio, ch)); 158 159 switch (ch) 160 { 161 case '>': 162 parseprintf(DD_PARSE, ("inp_trimptaip: START seen\n")); 163 164 parseio->parse_index = 1; 165 parseio->parse_data[0] = ch; 166 parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */ 167 return PARSE_INP_SKIP; 168 169 case '<': 170 parseprintf(DD_PARSE, ("inp_trimtaip: END seen\n")); 171 if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP) 172 return parse_end(parseio); 173 else 174 return rtc; 175 176 177 default: 178 return parse_addchar(parseio, ch); 179 } 180} 181 182#else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTAIP) */ 183int clk_trimtaip_bs; 184#endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTAIP) */ 185 186/* 187 * History: 188 * 189 * clk_trimtaip.c,v 190 * Revision 4.11 2005/04/16 17:32:10 kardel 191 * update copyright 192 * 193 * Revision 4.10 2004/11/14 15:29:41 kardel 194 * support PPSAPI, upgrade Copyright to Berkeley style 195 * 196 * Revision 4.7 1999/11/28 09:13:51 kardel 197 * RECON_4_0_98F 198 * 199 * Revision 4.6 1998/08/16 18:46:27 kardel 200 * (clock_trimtaip =): changed format name 201 * 202 * Revision 4.5 1998/06/14 21:09:38 kardel 203 * Sun acc cleanup 204 * 205 * Revision 4.4 1998/06/13 12:06:57 kardel 206 * fix SYSV clock name clash 207 * 208 * Revision 4.3 1998/06/12 15:22:29 kardel 209 * fix prototypes 210 * 211 * Revision 4.2 1998/06/12 09:13:26 kardel 212 * conditional compile macros fixed 213 * printf prototype 214 * 215 * Revision 4.1 1998/05/24 09:39:54 kardel 216 * implementation of the new IO handling model 217 * 218 * Revision 4.0 1998/04/10 19:45:31 kardel 219 * Start 4.0 release version numbering 220 * 221 * from V3 1.4 log info deleted 1998/04/11 kardel 222 */ 223 224