#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_TRIMTAIP)
#include "ntp_fp.h"
#include "ntp_unixtime.h"
#include "ntp_calendar.h"
#include "parse.h"
#ifndef PARSESTREAM
#include "ntp_stdlib.h"
#include <stdio.h>
#else
#include "sys/parsestreams.h"
extern void printf P((const char *, ...));
#endif
#define hexval(x) (('0' <= (x) && (x) <= '9') ? (x) - '0' : \
('a' <= (x) && (x) <= 'f') ? (x) - 'a' + 10 : \
('A' <= (x) && (x) <= 'F') ? (x) - 'A' + 10 : \
-1)
#define O_USEC O_WDAY
#define O_GPSFIX O_FLAGS
#define O_CHKSUM O_UTCHOFFSET
static struct format trimsv6_fmt =
{ { { 13, 2 }, {15, 2}, { 17, 4},
{ 4, 2 }, { 6, 2}, { 8, 2},
{ 10, 3 }, {23, 1}, { 0, 0},
{ 34, 2 }, { 0, 0}, { 21, 2},
},
(const unsigned char *)">RTM 1 ;* <",
0
};
static unsigned long cvt_trimtaip P((unsigned char *, int, struct format *, clocktime_t *, void *));
static unsigned long inp_trimtaip P((parse_t *, unsigned int, timestamp_t *));
clockformat_t clock_trimtaip =
{
inp_trimtaip,
cvt_trimtaip,
pps_one,
(void *)&trimsv6_fmt,
"Trimble TAIP",
37,
0
};
static unsigned long
cvt_trimtaip(
unsigned char *buffer,
int size,
struct format *format,
clocktime_t *clock_time,
void *local
)
{
long gpsfix;
u_char calc_csum = 0;
long recv_csum;
int i;
if (!Strok(buffer, format->fixed_string)) return CVT_NONE;
#define OFFS(x) format->field_offsets[(x)].offset
#define STOI(x, y) \
Stoi(&buffer[OFFS(x)], y, \
format->field_offsets[(x)].length)
if ( STOI(O_DAY, &clock_time->day) ||
STOI(O_MONTH, &clock_time->month) ||
STOI(O_YEAR, &clock_time->year) ||
STOI(O_HOUR, &clock_time->hour) ||
STOI(O_MIN, &clock_time->minute) ||
STOI(O_SEC, &clock_time->second) ||
STOI(O_USEC, &clock_time->usecond)||
STOI(O_GPSFIX, &gpsfix)
) return CVT_FAIL|CVT_BADFMT;
clock_time->usecond *= 1000;
for (i=OFFS(O_CHKSUM)-1; i >= 0; i--) calc_csum ^= buffer[i];
recv_csum = (hexval(buffer[OFFS(O_CHKSUM)]) << 4) |
hexval(buffer[OFFS(O_CHKSUM)+1]);
if (recv_csum < 0) return CVT_FAIL|CVT_BADTIME;
if (((u_char) recv_csum) != calc_csum) return CVT_FAIL|CVT_BADTIME;
clock_time->utcoffset = 0;
clock_time->flags = PARSEB_UTC;
if (0 > gpsfix || gpsfix > 9) clock_time->flags |= PARSEB_POWERUP;
return CVT_OK;
}
static u_long
inp_trimtaip(
parse_t *parseio,
unsigned int ch,
timestamp_t *tstamp
)
{
unsigned int rtc;
parseprintf(DD_PARSE, ("inp_trimtaip(0x%lx, 0x%x, ...)\n", (long)parseio, ch));
switch (ch)
{
case '>':
parseprintf(DD_PARSE, ("inp_trimptaip: START seen\n"));
parseio->parse_index = 1;
parseio->parse_data[0] = ch;
parseio->parse_dtime.parse_stime = *tstamp;
return PARSE_INP_SKIP;
case '<':
parseprintf(DD_PARSE, ("inp_trimtaip: END seen\n"));
if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
return parse_end(parseio);
else
return rtc;
default:
return parse_addchar(parseio, ch);
}
}
#else
int clk_trimtaip_bs;
#endif