#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#if defined(REFCLOCK) && defined(CLOCK_NMEA)
#include <stdio.h>
#include <ctype.h>
#include <sys/time.h>
#include "ntpd.h"
#include "ntp_io.h"
#include "ntp_refclock.h"
#include "ntp_stdlib.h"
#define DEVICE "/dev/gps%d"
#define SPEED232 B4800
#define PRECISION (-9)
#define DCD_PRECISION (-20)
#define REFID "GPS\0"
#define DESCRIPTION "NMEA GPS Clock"
#define LENNMEA 75
static int day1tab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static int day2tab[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct nmeaunit {
int pollcnt;
int polled;
l_fp tstamp;
};
static int nmea_start P((int, struct peer *));
static void nmea_shutdown P((int, struct peer *));
static void nmea_receive P((struct recvbuf *));
static void nmea_poll P((int, struct peer *));
static void gps_send P((int, const char *, struct peer *));
static char *field_parse P((char *, int));
struct refclock refclock_nmea = {
nmea_start,
nmea_shutdown,
nmea_poll,
noentry,
noentry,
noentry,
NOFLAGS
};
static int
nmea_start(
int unit,
struct peer *peer
)
{
register struct nmeaunit *up;
struct refclockproc *pp;
int fd;
char device[20];
(void)sprintf(device, DEVICE, unit);
if (!(fd = refclock_open(device, SPEED232, LDISC_CLK)))
return (0);
if (!(up = (struct nmeaunit *)
emalloc(sizeof(struct nmeaunit)))) {
(void) close(fd);
return (0);
}
memset((char *)up, 0, sizeof(struct nmeaunit));
pp = peer->procptr;
pp->io.clock_recv = nmea_receive;
pp->io.srcclock = (caddr_t)peer;
pp->io.datalen = 0;
pp->io.fd = fd;
if (!io_addclock(&pp->io)) {
(void) close(fd);
free(up);
return (0);
}
pp->unitptr = (caddr_t)up;
peer->precision = DCD_PRECISION;
pp->clockdesc = DESCRIPTION;
memcpy((char *)&pp->refid, REFID, 4);
up->pollcnt = 2;
gps_send(pp->io.fd,"$PMOTG,RMC,0000*1D\r\n", peer);
return (1);
}
static void
nmea_shutdown(
int unit,
struct peer *peer
)
{
register struct nmeaunit *up;
struct refclockproc *pp;
pp = peer->procptr;
up = (struct nmeaunit *)pp->unitptr;
io_closeclock(&pp->io);
free(up);
}
static void
nmea_receive(
struct recvbuf *rbufp
)
{
register struct nmeaunit *up;
struct refclockproc *pp;
struct peer *peer;
l_fp trtmp;
int month, day;
int i;
char *cp, *dp;
int cmdtype;
peer = (struct peer *)rbufp->recv_srcclock;
pp = peer->procptr;
up = (struct nmeaunit *)pp->unitptr;
pp->lencode = refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &trtmp);
if (pp->lencode == 0)
return;
pp->lastrec = up->tstamp = trtmp;
up->pollcnt = 2;
#ifdef DEBUG
if (debug)
printf("nmea: timecode %d %s\n", pp->lencode,
pp->a_lastcode);
#endif
#define GPRMC 0
#define GPXXX 1
cp = pp->a_lastcode;
cmdtype=0;
if(strncmp(cp,"$GPRMC",6)==0) {
cmdtype=GPRMC;
}
else if(strncmp(cp,"$GPXXX",6)==0) {
cmdtype=GPXXX;
}
else
return;
switch( cmdtype ) {
case GPRMC:
dp = field_parse(cp,1);
if( !isdigit((int)dp[0]) ||
!isdigit((int)dp[1]) ||
!isdigit((int)dp[2]) ||
!isdigit((int)dp[3]) ||
!isdigit((int)dp[4]) ||
!isdigit((int)dp[5])
) {
refclock_report(peer, CEVNT_BADREPLY);
return;
}
dp = field_parse(cp,2);
if( dp[0] != 'A') {
refclock_report(peer, CEVNT_BADREPLY);
return;
}
break;
case GPXXX:
return;
default:
return;
}
dp = field_parse(cp,9);
day = dp[0] - '0';
day = (day * 10) + dp[1] - '0';
month = dp[2] - '0';
month = (month * 10) + dp[3] - '0';
pp->year = dp[4] - '0';
pp->year = (pp->year * 10) + dp[5] - '0';
if (month < 1 || month > 12 || day < 1) {
refclock_report(peer, CEVNT_BADTIME);
return;
}
if (pp->year % 4) {
if (day > day1tab[month - 1]) {
refclock_report(peer, CEVNT_BADTIME);
return;
}
for (i = 0; i < month - 1; i++)
day += day1tab[i];
} else {
if (day > day2tab[month - 1]) {
refclock_report(peer, CEVNT_BADTIME);
return;
}
for (i = 0; i < month - 1; i++)
day += day2tab[i];
}
pp->day = day;
dp = field_parse(cp,1);
pp->hour = ((dp[0] - '0') * 10) + dp[1] - '0';
pp->minute = ((dp[2] - '0') * 10) + dp[3] - '0';
pp->second = ((dp[4] - '0') * 10) + dp[5] - '0';
pp->msec = 0;
if (pp->hour > 23 || pp->minute > 59 || pp->second > 59) {
refclock_report(peer, CEVNT_BADTIME);
return;
}
if (!refclock_process(pp)) {
refclock_report(peer, CEVNT_BADTIME);
return;
}
if (!up->polled)
return;
up->polled = 0;
refclock_receive(peer);
record_clock_stats(&peer->srcadr, pp->a_lastcode);
}
static void
nmea_poll(
int unit,
struct peer *peer
)
{
register struct nmeaunit *up;
struct refclockproc *pp;
pp = peer->procptr;
up = (struct nmeaunit *)pp->unitptr;
if (up->pollcnt == 0)
refclock_report(peer, CEVNT_TIMEOUT);
else
up->pollcnt--;
pp->polls++;
up->polled = 1;
gps_send(pp->io.fd,"$PMOTG,RMC,0000*1D\r\n", peer);
}
static void
gps_send(
int fd,
const char *cmd,
struct peer *peer
)
{
if (write(fd, cmd, strlen(cmd)) == -1) {
refclock_report(peer, CEVNT_FAULT);
}
}
static char *
field_parse(
char *cp,
int fn
)
{
char *tp;
int i = fn;
for (tp = cp; *tp != '\0'; tp++) {
if (*tp == ',')
i--;
if (i == 0)
break;
}
return (++tp);
}
#else
int refclock_nmea_bs;
#endif