clk_trimtaip.c revision 280849
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> 9182007Sroberto * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universit�t Erlangen-N�rnberg, 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 81280849Scystatic unsigned long cvt_trimtaip (unsigned char *, int, struct format *, clocktime_t *, void *); 82280849Scystatic unsigned long inp_trimtaip (parse_t *, unsigned int, timestamp_t *); 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 9554359Srobertostatic unsigned long 9654359Srobertocvt_trimtaip( 9754359Sroberto unsigned char *buffer, 9854359Sroberto int size, 9954359Sroberto struct format *format, 10054359Sroberto clocktime_t *clock_time, 10154359Sroberto void *local 10254359Sroberto ) 10354359Sroberto{ 10454359Sroberto long gpsfix; 10554359Sroberto u_char calc_csum = 0; 10654359Sroberto long recv_csum; 10754359Sroberto int i; 10854359Sroberto 10954359Sroberto if (!Strok(buffer, format->fixed_string)) return CVT_NONE; 11054359Sroberto#define OFFS(x) format->field_offsets[(x)].offset 11154359Sroberto#define STOI(x, y) \ 11254359Sroberto Stoi(&buffer[OFFS(x)], y, \ 11354359Sroberto format->field_offsets[(x)].length) 11454359Sroberto if ( STOI(O_DAY, &clock_time->day) || 11554359Sroberto STOI(O_MONTH, &clock_time->month) || 11654359Sroberto STOI(O_YEAR, &clock_time->year) || 11754359Sroberto STOI(O_HOUR, &clock_time->hour) || 11854359Sroberto STOI(O_MIN, &clock_time->minute) || 11954359Sroberto STOI(O_SEC, &clock_time->second) || 12054359Sroberto STOI(O_USEC, &clock_time->usecond)|| 12154359Sroberto STOI(O_GPSFIX, &gpsfix) 12254359Sroberto ) return CVT_FAIL|CVT_BADFMT; 12354359Sroberto 12454359Sroberto clock_time->usecond *= 1000; 12554359Sroberto /* Check that the checksum is right */ 12654359Sroberto for (i=OFFS(O_CHKSUM)-1; i >= 0; i--) calc_csum ^= buffer[i]; 12754359Sroberto recv_csum = (hexval(buffer[OFFS(O_CHKSUM)]) << 4) | 12854359Sroberto hexval(buffer[OFFS(O_CHKSUM)+1]); 12954359Sroberto if (recv_csum < 0) return CVT_FAIL|CVT_BADTIME; 13054359Sroberto if (((u_char) recv_csum) != calc_csum) return CVT_FAIL|CVT_BADTIME; 13154359Sroberto 13254359Sroberto clock_time->utcoffset = 0; 13354359Sroberto 13454359Sroberto /* What should flags be set to ? */ 13554359Sroberto clock_time->flags = PARSEB_UTC; 13654359Sroberto 13754359Sroberto /* if the current GPS fix is 9 (unknown), reject */ 13854359Sroberto if (0 > gpsfix || gpsfix > 9) clock_time->flags |= PARSEB_POWERUP; 13954359Sroberto 14054359Sroberto return CVT_OK; 14154359Sroberto} 14254359Sroberto 14354359Sroberto/* 14454359Sroberto * inp_trimtaip 14554359Sroberto * 14654359Sroberto * grep data from input stream 14754359Sroberto */ 14854359Srobertostatic u_long 14954359Srobertoinp_trimtaip( 15054359Sroberto parse_t *parseio, 15154359Sroberto unsigned int ch, 15254359Sroberto timestamp_t *tstamp 15354359Sroberto ) 15454359Sroberto{ 15554359Sroberto unsigned int rtc; 15654359Sroberto 15782498Sroberto parseprintf(DD_PARSE, ("inp_trimtaip(0x%lx, 0x%x, ...)\n", (long)parseio, ch)); 15854359Sroberto 15954359Sroberto switch (ch) 16054359Sroberto { 16154359Sroberto case '>': 16254359Sroberto parseprintf(DD_PARSE, ("inp_trimptaip: START seen\n")); 16354359Sroberto 16454359Sroberto parseio->parse_index = 1; 16554359Sroberto parseio->parse_data[0] = ch; 16654359Sroberto parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */ 16754359Sroberto return PARSE_INP_SKIP; 16854359Sroberto 16954359Sroberto case '<': 17054359Sroberto parseprintf(DD_PARSE, ("inp_trimtaip: END seen\n")); 17154359Sroberto if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP) 17254359Sroberto return parse_end(parseio); 17354359Sroberto else 17454359Sroberto return rtc; 17554359Sroberto 17654359Sroberto 17754359Sroberto default: 17854359Sroberto return parse_addchar(parseio, ch); 17954359Sroberto } 18054359Sroberto} 18154359Sroberto 18254359Sroberto#else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTAIP) */ 18354359Srobertoint clk_trimtaip_bs; 18454359Sroberto#endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTAIP) */ 18554359Sroberto 18654359Sroberto/* 18754359Sroberto * History: 18854359Sroberto * 18954359Sroberto * clk_trimtaip.c,v 190182007Sroberto * Revision 4.11 2005/04/16 17:32:10 kardel 191182007Sroberto * update copyright 192182007Sroberto * 193182007Sroberto * Revision 4.10 2004/11/14 15:29:41 kardel 194182007Sroberto * support PPSAPI, upgrade Copyright to Berkeley style 195182007Sroberto * 19656746Sroberto * Revision 4.7 1999/11/28 09:13:51 kardel 19756746Sroberto * RECON_4_0_98F 19856746Sroberto * 19954359Sroberto * Revision 4.6 1998/08/16 18:46:27 kardel 20054359Sroberto * (clock_trimtaip =): changed format name 20154359Sroberto * 20254359Sroberto * Revision 4.5 1998/06/14 21:09:38 kardel 20354359Sroberto * Sun acc cleanup 20454359Sroberto * 20554359Sroberto * Revision 4.4 1998/06/13 12:06:57 kardel 20654359Sroberto * fix SYSV clock name clash 20754359Sroberto * 20854359Sroberto * Revision 4.3 1998/06/12 15:22:29 kardel 20954359Sroberto * fix prototypes 21054359Sroberto * 21154359Sroberto * Revision 4.2 1998/06/12 09:13:26 kardel 21254359Sroberto * conditional compile macros fixed 21354359Sroberto * printf prototype 21454359Sroberto * 21554359Sroberto * Revision 4.1 1998/05/24 09:39:54 kardel 21654359Sroberto * implementation of the new IO handling model 21754359Sroberto * 21854359Sroberto * Revision 4.0 1998/04/10 19:45:31 kardel 21954359Sroberto * Start 4.0 release version numbering 22054359Sroberto * 22154359Sroberto * from V3 1.4 log info deleted 1998/04/11 kardel 22254359Sroberto */ 22354359Sroberto 224